1 /* $OpenBSD: ssh-agent.c,v 1.155 2007/03/19 12:16:42 dtucker Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * The authentication agent program.
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
14 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #ifdef HAVE_SYS_TIME_H
45 # include <sys/time.h>
50 #include "openbsd-compat/sys-queue.h"
52 #include <openssl/evp.h>
53 #include <openssl/md5.h>
82 #if defined(HAVE_SYS_PRCTL_H)
83 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
100 u_int sockets_alloc
= 0;
101 SocketEntry
*sockets
= NULL
;
103 typedef struct identity
{
104 TAILQ_ENTRY(identity
) next
;
113 TAILQ_HEAD(idqueue
, identity
) idlist
;
116 /* private key table, one per protocol version */
121 /* pid of shell == parent of agent */
122 pid_t parent_pid
= -1;
123 u_int parent_alive_interval
= 0;
125 /* pathname and directory for AUTH_SOCKET */
126 char socket_name
[MAXPATHLEN
];
127 char socket_dir
[MAXPATHLEN
];
131 char *lock_passwd
= NULL
;
133 extern char *__progname
;
135 /* Default lifetime (0 == forever) */
136 static int lifetime
= 0;
139 close_socket(SocketEntry
*e
)
143 e
->type
= AUTH_UNUSED
;
144 buffer_free(&e
->input
);
145 buffer_free(&e
->output
);
146 buffer_free(&e
->request
);
154 for (i
= 0; i
<=2; i
++) {
155 TAILQ_INIT(&idtable
[i
].idlist
);
156 idtable
[i
].nentries
= 0;
160 /* return private key table for requested protocol version */
162 idtab_lookup(int version
)
164 if (version
< 1 || version
> 2)
165 fatal("internal error, bad protocol version %d", version
);
166 return &idtable
[version
];
170 free_identity(Identity
*id
)
177 /* return matching private key for given public key */
179 lookup_identity(Key
*key
, int version
)
183 Idtab
*tab
= idtab_lookup(version
);
184 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
185 if (key_equal(key
, id
->key
))
191 /* Check confirmation of keysign request */
193 confirm_key(Identity
*id
)
198 p
= key_fingerprint(id
->key
, SSH_FP_MD5
, SSH_FP_HEX
);
199 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
207 /* send list of supported public keys to 'client' */
209 process_request_identities(SocketEntry
*e
, int version
)
211 Idtab
*tab
= idtab_lookup(version
);
216 buffer_put_char(&msg
, (version
== 1) ?
217 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
218 buffer_put_int(&msg
, tab
->nentries
);
219 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
220 if (id
->key
->type
== KEY_RSA1
) {
221 buffer_put_int(&msg
, BN_num_bits(id
->key
->rsa
->n
));
222 buffer_put_bignum(&msg
, id
->key
->rsa
->e
);
223 buffer_put_bignum(&msg
, id
->key
->rsa
->n
);
227 key_to_blob(id
->key
, &blob
, &blen
);
228 buffer_put_string(&msg
, blob
, blen
);
231 buffer_put_cstring(&msg
, id
->comment
);
233 buffer_put_int(&e
->output
, buffer_len(&msg
));
234 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
240 process_authentication_challenge1(SocketEntry
*e
)
242 u_char buf
[32], mdbuf
[16], session_id
[16];
252 key
= key_new(KEY_RSA1
);
253 if ((challenge
= BN_new()) == NULL
)
254 fatal("process_authentication_challenge1: BN_new failed");
256 (void) buffer_get_int(&e
->request
); /* ignored */
257 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
258 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
259 buffer_get_bignum(&e
->request
, challenge
);
261 /* Only protocol 1.1 is supported */
262 if (buffer_len(&e
->request
) == 0)
264 buffer_get(&e
->request
, session_id
, 16);
265 response_type
= buffer_get_int(&e
->request
);
266 if (response_type
!= 1)
269 id
= lookup_identity(key
, 1);
270 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0)) {
271 Key
*private = id
->key
;
272 /* Decrypt the challenge using the private key. */
273 if (rsa_private_decrypt(challenge
, challenge
, private->rsa
) <= 0)
276 /* The response is MD5 of decrypted challenge plus session id. */
277 len
= BN_num_bytes(challenge
);
278 if (len
<= 0 || len
> 32) {
279 logit("process_authentication_challenge: bad challenge length %d", len
);
283 BN_bn2bin(challenge
, buf
+ 32 - len
);
285 MD5_Update(&md
, buf
, 32);
286 MD5_Update(&md
, session_id
, 16);
287 MD5_Final(mdbuf
, &md
);
289 /* Send the response. */
290 buffer_put_char(&msg
, SSH_AGENT_RSA_RESPONSE
);
291 for (i
= 0; i
< 16; i
++)
292 buffer_put_char(&msg
, mdbuf
[i
]);
297 /* Unknown identity or protocol error. Send failure. */
298 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
300 buffer_put_int(&e
->output
, buffer_len(&msg
));
301 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
303 BN_clear_free(challenge
);
309 process_sign_request2(SocketEntry
*e
)
311 u_char
*blob
, *data
, *signature
= NULL
;
312 u_int blen
, dlen
, slen
= 0;
313 extern int datafellows
;
320 blob
= buffer_get_string(&e
->request
, &blen
);
321 data
= buffer_get_string(&e
->request
, &dlen
);
323 flags
= buffer_get_int(&e
->request
);
324 if (flags
& SSH_AGENT_OLD_SIGNATURE
)
325 datafellows
= SSH_BUG_SIGBLOB
;
327 key
= key_from_blob(blob
, blen
);
329 Identity
*id
= lookup_identity(key
, 2);
330 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0))
331 ok
= key_sign(id
->key
, &signature
, &slen
, data
, dlen
);
336 buffer_put_char(&msg
, SSH2_AGENT_SIGN_RESPONSE
);
337 buffer_put_string(&msg
, signature
, slen
);
339 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
341 buffer_put_int(&e
->output
, buffer_len(&msg
));
342 buffer_append(&e
->output
, buffer_ptr(&msg
),
347 if (signature
!= NULL
)
353 process_remove_identity(SocketEntry
*e
, int version
)
362 key
= key_new(KEY_RSA1
);
363 bits
= buffer_get_int(&e
->request
);
364 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
365 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
367 if (bits
!= key_size(key
))
368 logit("Warning: identity keysize mismatch: actual %u, announced %u",
369 key_size(key
), bits
);
372 blob
= buffer_get_string(&e
->request
, &blen
);
373 key
= key_from_blob(blob
, blen
);
378 Identity
*id
= lookup_identity(key
, version
);
381 * We have this key. Free the old key. Since we
382 * don't want to leave empty slots in the middle of
383 * the array, we actually free the key there and move
384 * all the entries between the empty slot and the end
387 Idtab
*tab
= idtab_lookup(version
);
388 if (tab
->nentries
< 1)
389 fatal("process_remove_identity: "
390 "internal error: tab->nentries %d",
392 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
399 buffer_put_int(&e
->output
, 1);
400 buffer_put_char(&e
->output
,
401 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
405 process_remove_all_identities(SocketEntry
*e
, int version
)
407 Idtab
*tab
= idtab_lookup(version
);
410 /* Loop over all identities and clear the keys. */
411 for (id
= TAILQ_FIRST(&tab
->idlist
); id
;
412 id
= TAILQ_FIRST(&tab
->idlist
)) {
413 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
417 /* Mark that there are no identities. */
421 buffer_put_int(&e
->output
, 1);
422 buffer_put_char(&e
->output
, SSH_AGENT_SUCCESS
);
425 /* removes expired keys and returns number of seconds until the next expiry */
429 u_int deadline
= 0, now
= time(NULL
);
434 for (version
= 1; version
< 3; version
++) {
435 tab
= idtab_lookup(version
);
436 for (id
= TAILQ_FIRST(&tab
->idlist
); id
; id
= nxt
) {
437 nxt
= TAILQ_NEXT(id
, next
);
440 if (now
>= id
->death
) {
441 debug("expiring key '%s'", id
->comment
);
442 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
446 deadline
= (deadline
== 0) ? id
->death
:
447 MIN(deadline
, id
->death
);
450 if (deadline
== 0 || deadline
<= now
)
453 return (deadline
- now
);
457 process_add_identity(SocketEntry
*e
, int version
)
459 Idtab
*tab
= idtab_lookup(version
);
460 int type
, success
= 0, death
= 0, confirm
= 0;
461 char *type_name
, *comment
;
466 k
= key_new_private(KEY_RSA1
);
467 (void) buffer_get_int(&e
->request
); /* ignored */
468 buffer_get_bignum(&e
->request
, k
->rsa
->n
);
469 buffer_get_bignum(&e
->request
, k
->rsa
->e
);
470 buffer_get_bignum(&e
->request
, k
->rsa
->d
);
471 buffer_get_bignum(&e
->request
, k
->rsa
->iqmp
);
473 /* SSH and SSL have p and q swapped */
474 buffer_get_bignum(&e
->request
, k
->rsa
->q
); /* p */
475 buffer_get_bignum(&e
->request
, k
->rsa
->p
); /* q */
477 /* Generate additional parameters */
478 rsa_generate_additional_parameters(k
->rsa
);
481 type_name
= buffer_get_string(&e
->request
, NULL
);
482 type
= key_type_from_name(type_name
);
486 k
= key_new_private(type
);
487 buffer_get_bignum2(&e
->request
, k
->dsa
->p
);
488 buffer_get_bignum2(&e
->request
, k
->dsa
->q
);
489 buffer_get_bignum2(&e
->request
, k
->dsa
->g
);
490 buffer_get_bignum2(&e
->request
, k
->dsa
->pub_key
);
491 buffer_get_bignum2(&e
->request
, k
->dsa
->priv_key
);
494 k
= key_new_private(type
);
495 buffer_get_bignum2(&e
->request
, k
->rsa
->n
);
496 buffer_get_bignum2(&e
->request
, k
->rsa
->e
);
497 buffer_get_bignum2(&e
->request
, k
->rsa
->d
);
498 buffer_get_bignum2(&e
->request
, k
->rsa
->iqmp
);
499 buffer_get_bignum2(&e
->request
, k
->rsa
->p
);
500 buffer_get_bignum2(&e
->request
, k
->rsa
->q
);
502 /* Generate additional parameters */
503 rsa_generate_additional_parameters(k
->rsa
);
506 buffer_clear(&e
->request
);
511 /* enable blinding */
515 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
516 error("process_add_identity: RSA_blinding_on failed");
522 comment
= buffer_get_string(&e
->request
, NULL
);
528 while (buffer_len(&e
->request
)) {
529 switch (buffer_get_char(&e
->request
)) {
530 case SSH_AGENT_CONSTRAIN_LIFETIME
:
531 death
= time(NULL
) + buffer_get_int(&e
->request
);
533 case SSH_AGENT_CONSTRAIN_CONFIRM
:
540 if (lifetime
&& !death
)
541 death
= time(NULL
) + lifetime
;
542 if (lookup_identity(k
, version
) == NULL
) {
543 Identity
*id
= xmalloc(sizeof(Identity
));
545 id
->comment
= comment
;
547 id
->confirm
= confirm
;
548 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
549 /* Increment the number of identities. */
556 buffer_put_int(&e
->output
, 1);
557 buffer_put_char(&e
->output
,
558 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
561 /* XXX todo: encrypt sensitive data with passphrase */
563 process_lock_agent(SocketEntry
*e
, int lock
)
568 passwd
= buffer_get_string(&e
->request
, NULL
);
569 if (locked
&& !lock
&& strcmp(passwd
, lock_passwd
) == 0) {
571 memset(lock_passwd
, 0, strlen(lock_passwd
));
575 } else if (!locked
&& lock
) {
577 lock_passwd
= xstrdup(passwd
);
580 memset(passwd
, 0, strlen(passwd
));
583 buffer_put_int(&e
->output
, 1);
584 buffer_put_char(&e
->output
,
585 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
589 no_identities(SocketEntry
*e
, u_int type
)
594 buffer_put_char(&msg
,
595 (type
== SSH_AGENTC_REQUEST_RSA_IDENTITIES
) ?
596 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
597 buffer_put_int(&msg
, 0);
598 buffer_put_int(&e
->output
, buffer_len(&msg
));
599 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
605 process_add_smartcard_key (SocketEntry
*e
)
607 char *sc_reader_id
= NULL
, *pin
;
608 int i
, version
, success
= 0, death
= 0, confirm
= 0;
613 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
614 pin
= buffer_get_string(&e
->request
, NULL
);
616 while (buffer_len(&e
->request
)) {
617 switch (buffer_get_char(&e
->request
)) {
618 case SSH_AGENT_CONSTRAIN_LIFETIME
:
619 death
= time(NULL
) + buffer_get_int(&e
->request
);
621 case SSH_AGENT_CONSTRAIN_CONFIRM
:
628 if (lifetime
&& !death
)
629 death
= time(NULL
) + lifetime
;
631 keys
= sc_get_keys(sc_reader_id
, pin
);
635 if (keys
== NULL
|| keys
[0] == NULL
) {
636 error("sc_get_keys failed");
639 for (i
= 0; keys
[i
] != NULL
; i
++) {
641 version
= k
->type
== KEY_RSA1
? 1 : 2;
642 tab
= idtab_lookup(version
);
643 if (lookup_identity(k
, version
) == NULL
) {
644 id
= xmalloc(sizeof(Identity
));
646 id
->comment
= sc_get_key_label(k
);
648 id
->confirm
= confirm
;
649 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
659 buffer_put_int(&e
->output
, 1);
660 buffer_put_char(&e
->output
,
661 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
665 process_remove_smartcard_key(SocketEntry
*e
)
667 char *sc_reader_id
= NULL
, *pin
;
668 int i
, version
, success
= 0;
669 Key
**keys
, *k
= NULL
;
673 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
674 pin
= buffer_get_string(&e
->request
, NULL
);
675 keys
= sc_get_keys(sc_reader_id
, pin
);
679 if (keys
== NULL
|| keys
[0] == NULL
) {
680 error("sc_get_keys failed");
683 for (i
= 0; keys
[i
] != NULL
; i
++) {
685 version
= k
->type
== KEY_RSA1
? 1 : 2;
686 if ((id
= lookup_identity(k
, version
)) != NULL
) {
687 tab
= idtab_lookup(version
);
688 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
698 buffer_put_int(&e
->output
, 1);
699 buffer_put_char(&e
->output
,
700 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
702 #endif /* SMARTCARD */
704 /* dispatch incoming messages */
707 process_message(SocketEntry
*e
)
712 if (buffer_len(&e
->input
) < 5)
713 return; /* Incomplete message. */
714 cp
= buffer_ptr(&e
->input
);
715 msg_len
= get_u32(cp
);
716 if (msg_len
> 256 * 1024) {
720 if (buffer_len(&e
->input
) < msg_len
+ 4)
723 /* move the current input to e->request */
724 buffer_consume(&e
->input
, 4);
725 buffer_clear(&e
->request
);
726 buffer_append(&e
->request
, buffer_ptr(&e
->input
), msg_len
);
727 buffer_consume(&e
->input
, msg_len
);
728 type
= buffer_get_char(&e
->request
);
730 /* check wheter agent is locked */
731 if (locked
&& type
!= SSH_AGENTC_UNLOCK
) {
732 buffer_clear(&e
->request
);
734 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
735 case SSH2_AGENTC_REQUEST_IDENTITIES
:
736 /* send empty lists */
737 no_identities(e
, type
);
740 /* send a fail message for all other request types */
741 buffer_put_int(&e
->output
, 1);
742 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
747 debug("type %d", type
);
749 case SSH_AGENTC_LOCK
:
750 case SSH_AGENTC_UNLOCK
:
751 process_lock_agent(e
, type
== SSH_AGENTC_LOCK
);
754 case SSH_AGENTC_RSA_CHALLENGE
:
755 process_authentication_challenge1(e
);
757 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
758 process_request_identities(e
, 1);
760 case SSH_AGENTC_ADD_RSA_IDENTITY
:
761 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED
:
762 process_add_identity(e
, 1);
764 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
765 process_remove_identity(e
, 1);
767 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
768 process_remove_all_identities(e
, 1);
771 case SSH2_AGENTC_SIGN_REQUEST
:
772 process_sign_request2(e
);
774 case SSH2_AGENTC_REQUEST_IDENTITIES
:
775 process_request_identities(e
, 2);
777 case SSH2_AGENTC_ADD_IDENTITY
:
778 case SSH2_AGENTC_ADD_ID_CONSTRAINED
:
779 process_add_identity(e
, 2);
781 case SSH2_AGENTC_REMOVE_IDENTITY
:
782 process_remove_identity(e
, 2);
784 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
785 process_remove_all_identities(e
, 2);
788 case SSH_AGENTC_ADD_SMARTCARD_KEY
:
789 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED
:
790 process_add_smartcard_key(e
);
792 case SSH_AGENTC_REMOVE_SMARTCARD_KEY
:
793 process_remove_smartcard_key(e
);
795 #endif /* SMARTCARD */
797 /* Unknown message. Respond with failure. */
798 error("Unknown message %d", type
);
799 buffer_clear(&e
->request
);
800 buffer_put_int(&e
->output
, 1);
801 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
807 new_socket(sock_type type
, int fd
)
809 u_int i
, old_alloc
, new_alloc
;
816 for (i
= 0; i
< sockets_alloc
; i
++)
817 if (sockets
[i
].type
== AUTH_UNUSED
) {
819 buffer_init(&sockets
[i
].input
);
820 buffer_init(&sockets
[i
].output
);
821 buffer_init(&sockets
[i
].request
);
822 sockets
[i
].type
= type
;
825 old_alloc
= sockets_alloc
;
826 new_alloc
= sockets_alloc
+ 10;
827 sockets
= xrealloc(sockets
, new_alloc
, sizeof(sockets
[0]));
828 for (i
= old_alloc
; i
< new_alloc
; i
++)
829 sockets
[i
].type
= AUTH_UNUSED
;
830 sockets_alloc
= new_alloc
;
831 sockets
[old_alloc
].fd
= fd
;
832 buffer_init(&sockets
[old_alloc
].input
);
833 buffer_init(&sockets
[old_alloc
].output
);
834 buffer_init(&sockets
[old_alloc
].request
);
835 sockets
[old_alloc
].type
= type
;
839 prepare_select(fd_set
**fdrp
, fd_set
**fdwp
, int *fdl
, u_int
*nallocp
,
840 struct timeval
**tvpp
)
842 u_int i
, sz
, deadline
;
844 static struct timeval tv
;
846 for (i
= 0; i
< sockets_alloc
; i
++) {
847 switch (sockets
[i
].type
) {
849 case AUTH_CONNECTION
:
850 n
= MAX(n
, sockets
[i
].fd
);
855 fatal("Unknown socket type %d", sockets
[i
].type
);
860 sz
= howmany(n
+1, NFDBITS
) * sizeof(fd_mask
);
861 if (*fdrp
== NULL
|| sz
> *nallocp
) {
871 debug("XXX shrink: %d < %d", n
, *fdl
);
873 memset(*fdrp
, 0, sz
);
874 memset(*fdwp
, 0, sz
);
876 for (i
= 0; i
< sockets_alloc
; i
++) {
877 switch (sockets
[i
].type
) {
879 case AUTH_CONNECTION
:
880 FD_SET(sockets
[i
].fd
, *fdrp
);
881 if (buffer_len(&sockets
[i
].output
) > 0)
882 FD_SET(sockets
[i
].fd
, *fdwp
);
889 if (parent_alive_interval
!= 0)
890 deadline
= (deadline
== 0) ? parent_alive_interval
:
891 MIN(deadline
, parent_alive_interval
);
895 tv
.tv_sec
= deadline
;
903 after_select(fd_set
*readset
, fd_set
*writeset
)
905 struct sockaddr_un sunaddr
;
913 for (i
= 0; i
< sockets_alloc
; i
++)
914 switch (sockets
[i
].type
) {
918 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
919 slen
= sizeof(sunaddr
);
920 sock
= accept(sockets
[i
].fd
,
921 (struct sockaddr
*)&sunaddr
, &slen
);
923 error("accept from AUTH_SOCKET: %s",
927 if (getpeereid(sock
, &euid
, &egid
) < 0) {
928 error("getpeereid %d failed: %s",
929 sock
, strerror(errno
));
933 if ((euid
!= 0) && (getuid() != euid
)) {
934 error("uid mismatch: "
935 "peer euid %u != uid %u",
936 (u_int
) euid
, (u_int
) getuid());
940 new_socket(AUTH_CONNECTION
, sock
);
943 case AUTH_CONNECTION
:
944 if (buffer_len(&sockets
[i
].output
) > 0 &&
945 FD_ISSET(sockets
[i
].fd
, writeset
)) {
947 len
= write(sockets
[i
].fd
,
948 buffer_ptr(&sockets
[i
].output
),
949 buffer_len(&sockets
[i
].output
));
950 if (len
== -1 && (errno
== EAGAIN
||
956 close_socket(&sockets
[i
]);
959 buffer_consume(&sockets
[i
].output
, len
);
961 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
963 len
= read(sockets
[i
].fd
, buf
, sizeof(buf
));
964 if (len
== -1 && (errno
== EAGAIN
||
970 close_socket(&sockets
[i
]);
973 buffer_append(&sockets
[i
].input
, buf
, len
);
974 process_message(&sockets
[i
]);
978 fatal("Unknown type %d", sockets
[i
].type
);
1000 cleanup_handler(int sig
)
1007 check_parent_exists(void)
1009 if (parent_pid
!= -1 && kill(parent_pid
, 0) < 0) {
1010 /* printf("Parent has died - Authentication agent exiting.\n"); */
1019 fprintf(stderr
, "Usage: %s [options] [command [args ...]]\n",
1021 fprintf(stderr
, "Options:\n");
1022 fprintf(stderr
, " -c Generate C-shell commands on stdout.\n");
1023 fprintf(stderr
, " -s Generate Bourne shell commands on stdout.\n");
1024 fprintf(stderr
, " -k Kill the current agent.\n");
1025 fprintf(stderr
, " -d Debug mode.\n");
1026 fprintf(stderr
, " -a socket Bind agent socket to given name.\n");
1027 fprintf(stderr
, " -t life Default identity lifetime (seconds).\n");
1032 main(int ac
, char **av
)
1034 int c_flag
= 0, d_flag
= 0, k_flag
= 0, s_flag
= 0;
1035 int sock
, fd
, ch
, result
, saved_errno
;
1037 char *shell
, *format
, *pidstr
, *agentsocket
= NULL
;
1038 fd_set
*readsetp
= NULL
, *writesetp
= NULL
;
1039 struct sockaddr_un sunaddr
;
1040 #ifdef HAVE_SETRLIMIT
1045 extern char *optarg
;
1047 char pidstrbuf
[1 + 3 * sizeof pid
];
1048 struct timeval
*tvp
= NULL
;
1050 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1057 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1058 /* Disable ptrace on Linux without sgid bit */
1059 prctl(PR_SET_DUMPABLE
, 0);
1062 SSLeay_add_all_algorithms();
1064 __progname
= ssh_get_progname(av
[0]);
1068 while ((ch
= getopt(ac
, av
, "cdksa:t:")) != -1) {
1089 agentsocket
= optarg
;
1092 if ((lifetime
= convtime(optarg
)) == -1) {
1093 fprintf(stderr
, "Invalid lifetime\n");
1104 if (ac
> 0 && (c_flag
|| k_flag
|| s_flag
|| d_flag
))
1107 if (ac
== 0 && !c_flag
&& !s_flag
) {
1108 shell
= getenv("SHELL");
1109 if (shell
!= NULL
&&
1110 strncmp(shell
+ strlen(shell
) - 3, "csh", 3) == 0)
1114 const char *errstr
= NULL
;
1116 pidstr
= getenv(SSH_AGENTPID_ENV_NAME
);
1117 if (pidstr
== NULL
) {
1118 fprintf(stderr
, "%s not set, cannot kill agent\n",
1119 SSH_AGENTPID_ENV_NAME
);
1122 pid
= (int)strtonum(pidstr
, 2, INT_MAX
, &errstr
);
1125 "%s=\"%s\", which is not a good PID: %s\n",
1126 SSH_AGENTPID_ENV_NAME
, pidstr
, errstr
);
1129 if (kill(pid
, SIGTERM
) == -1) {
1133 format
= c_flag
? "unsetenv %s;\n" : "unset %s;\n";
1134 printf(format
, SSH_AUTHSOCKET_ENV_NAME
);
1135 printf(format
, SSH_AGENTPID_ENV_NAME
);
1136 printf("echo Agent pid %ld killed;\n", (long)pid
);
1139 parent_pid
= getpid();
1141 if (agentsocket
== NULL
) {
1142 /* Create private directory for agent socket */
1143 strlcpy(socket_dir
, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir
);
1144 if (mkdtemp(socket_dir
) == NULL
) {
1145 perror("mkdtemp: private socket dir");
1148 snprintf(socket_name
, sizeof socket_name
, "%s/agent.%ld", socket_dir
,
1151 /* Try to use specified agent socket */
1152 socket_dir
[0] = '\0';
1153 strlcpy(socket_name
, agentsocket
, sizeof socket_name
);
1157 * Create socket early so it will exist before command gets run from
1160 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1163 *socket_name
= '\0'; /* Don't unlink any existing file */
1166 memset(&sunaddr
, 0, sizeof(sunaddr
));
1167 sunaddr
.sun_family
= AF_UNIX
;
1168 strlcpy(sunaddr
.sun_path
, socket_name
, sizeof(sunaddr
.sun_path
));
1169 prev_mask
= umask(0177);
1170 if (bind(sock
, (struct sockaddr
*) &sunaddr
, sizeof(sunaddr
)) < 0) {
1172 *socket_name
= '\0'; /* Don't unlink any existing file */
1177 if (listen(sock
, SSH_LISTEN_BACKLOG
) < 0) {
1183 * Fork, and have the parent execute the command, if any, or present
1184 * the socket data. The child continues as the authentication agent.
1187 log_init(__progname
, SYSLOG_LEVEL_DEBUG1
, SYSLOG_FACILITY_AUTH
, 1);
1188 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1189 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1190 SSH_AUTHSOCKET_ENV_NAME
);
1191 printf("echo Agent pid %ld;\n", (long)parent_pid
);
1199 if (pid
!= 0) { /* Parent - execute the given command. */
1201 snprintf(pidstrbuf
, sizeof pidstrbuf
, "%ld", (long)pid
);
1203 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1204 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1205 SSH_AUTHSOCKET_ENV_NAME
);
1206 printf(format
, SSH_AGENTPID_ENV_NAME
, pidstrbuf
,
1207 SSH_AGENTPID_ENV_NAME
);
1208 printf("echo Agent pid %ld;\n", (long)pid
);
1211 if (setenv(SSH_AUTHSOCKET_ENV_NAME
, socket_name
, 1) == -1 ||
1212 setenv(SSH_AGENTPID_ENV_NAME
, pidstrbuf
, 1) == -1) {
1221 log_init(__progname
, SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_AUTH
, 0);
1223 if (setsid() == -1) {
1224 error("setsid: %s", strerror(errno
));
1229 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) != -1) {
1230 /* XXX might close listen socket */
1231 (void)dup2(fd
, STDIN_FILENO
);
1232 (void)dup2(fd
, STDOUT_FILENO
);
1233 (void)dup2(fd
, STDERR_FILENO
);
1238 #ifdef HAVE_SETRLIMIT
1239 /* deny core dumps, since memory contains unencrypted private keys */
1240 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
1241 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0) {
1242 error("setrlimit RLIMIT_CORE: %s", strerror(errno
));
1248 new_socket(AUTH_SOCKET
, sock
);
1250 parent_alive_interval
= 10;
1253 signal(SIGINT
, SIG_IGN
);
1254 signal(SIGPIPE
, SIG_IGN
);
1255 signal(SIGHUP
, cleanup_handler
);
1256 signal(SIGTERM
, cleanup_handler
);
1260 prepare_select(&readsetp
, &writesetp
, &max_fd
, &nalloc
, &tvp
);
1261 result
= select(max_fd
+ 1, readsetp
, writesetp
, NULL
, tvp
);
1262 saved_errno
= errno
;
1263 if (parent_alive_interval
!= 0)
1264 check_parent_exists();
1265 (void) reaper(); /* remove expired keys */
1267 if (saved_errno
== EINTR
)
1269 fatal("select: %s", strerror(saved_errno
));
1270 } else if (result
> 0)
1271 after_select(readsetp
, writesetp
);