1 /* $NetBSD: ssh-agent.c,v 1.1.1.2 2009/12/27 01:07:09 christos Exp $ */
2 /* $OpenBSD: ssh-agent.c,v 1.161 2009/03/23 19:38:04 tobias Exp $ */
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * The authentication agent program.
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
15 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 __RCSID("$NetBSD: ssh-agent.c,v 1.3 2009/12/17 15:55:16 agc Exp $");
40 #include <sys/types.h>
42 #include <sys/queue.h>
43 #include <sys/resource.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
47 #include <sys/param.h>
49 #include <openssl/evp.h>
50 #include <openssl/md5.h>
71 #include "getpeereid.h"
91 u_int sockets_alloc
= 0;
92 SocketEntry
*sockets
= NULL
;
94 typedef struct identity
{
95 TAILQ_ENTRY(identity
) next
;
104 TAILQ_HEAD(idqueue
, identity
) idlist
;
107 /* private key table, one per protocol version */
112 /* pid of shell == parent of agent */
113 pid_t parent_pid
= -1;
114 u_int parent_alive_interval
= 0;
116 /* pathname and directory for AUTH_SOCKET */
117 char socket_name
[MAXPATHLEN
];
118 char socket_dir
[MAXPATHLEN
];
122 char *lock_passwd
= NULL
;
124 extern char *__progname
;
126 /* Default lifetime (0 == forever) */
127 static int lifetime
= 0;
130 close_socket(SocketEntry
*e
)
134 e
->type
= AUTH_UNUSED
;
135 buffer_free(&e
->input
);
136 buffer_free(&e
->output
);
137 buffer_free(&e
->request
);
145 for (i
= 0; i
<=2; i
++) {
146 TAILQ_INIT(&idtable
[i
].idlist
);
147 idtable
[i
].nentries
= 0;
151 /* return private key table for requested protocol version */
153 idtab_lookup(int version
)
155 if (version
< 1 || version
> 2)
156 fatal("internal error, bad protocol version %d", version
);
157 return &idtable
[version
];
161 free_identity(Identity
*id
)
168 /* return matching private key for given public key */
170 lookup_identity(Key
*key
, int version
)
174 Idtab
*tab
= idtab_lookup(version
);
175 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
176 if (key_equal(key
, id
->key
))
182 /* Check confirmation of keysign request */
184 confirm_key(Identity
*id
)
189 p
= key_fingerprint(id
->key
, SSH_FP_MD5
, SSH_FP_HEX
);
190 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
198 /* send list of supported public keys to 'client' */
200 process_request_identities(SocketEntry
*e
, int version
)
202 Idtab
*tab
= idtab_lookup(version
);
207 buffer_put_char(&msg
, (version
== 1) ?
208 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
209 buffer_put_int(&msg
, tab
->nentries
);
210 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
211 if (id
->key
->type
== KEY_RSA1
) {
212 buffer_put_int(&msg
, BN_num_bits(id
->key
->rsa
->n
));
213 buffer_put_bignum(&msg
, id
->key
->rsa
->e
);
214 buffer_put_bignum(&msg
, id
->key
->rsa
->n
);
218 key_to_blob(id
->key
, &blob
, &blen
);
219 buffer_put_string(&msg
, blob
, blen
);
222 buffer_put_cstring(&msg
, id
->comment
);
224 buffer_put_int(&e
->output
, buffer_len(&msg
));
225 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
231 process_authentication_challenge1(SocketEntry
*e
)
233 u_char buf
[32], mdbuf
[16], session_id
[16];
243 key
= key_new(KEY_RSA1
);
244 if ((challenge
= BN_new()) == NULL
)
245 fatal("process_authentication_challenge1: BN_new failed");
247 (void) buffer_get_int(&e
->request
); /* ignored */
248 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
249 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
250 buffer_get_bignum(&e
->request
, challenge
);
252 /* Only protocol 1.1 is supported */
253 if (buffer_len(&e
->request
) == 0)
255 buffer_get(&e
->request
, session_id
, 16);
256 response_type
= buffer_get_int(&e
->request
);
257 if (response_type
!= 1)
260 id
= lookup_identity(key
, 1);
261 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0)) {
262 Key
*private = id
->key
;
263 /* Decrypt the challenge using the private key. */
264 if (rsa_private_decrypt(challenge
, challenge
, private->rsa
) <= 0)
267 /* The response is MD5 of decrypted challenge plus session id. */
268 len
= BN_num_bytes(challenge
);
269 if (len
<= 0 || len
> 32) {
270 logit("process_authentication_challenge: bad challenge length %d", len
);
274 BN_bn2bin(challenge
, buf
+ 32 - len
);
276 MD5_Update(&md
, buf
, 32);
277 MD5_Update(&md
, session_id
, 16);
278 MD5_Final(mdbuf
, &md
);
280 /* Send the response. */
281 buffer_put_char(&msg
, SSH_AGENT_RSA_RESPONSE
);
282 for (i
= 0; i
< 16; i
++)
283 buffer_put_char(&msg
, mdbuf
[i
]);
288 /* Unknown identity or protocol error. Send failure. */
289 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
291 buffer_put_int(&e
->output
, buffer_len(&msg
));
292 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
294 BN_clear_free(challenge
);
300 process_sign_request2(SocketEntry
*e
)
302 u_char
*blob
, *data
, *signature
= NULL
;
303 u_int blen
, dlen
, slen
= 0;
304 extern int datafellows
;
310 odatafellows
= datafellows
;
313 blob
= buffer_get_string(&e
->request
, &blen
);
314 data
= buffer_get_string(&e
->request
, &dlen
);
316 flags
= buffer_get_int(&e
->request
);
317 if (flags
& SSH_AGENT_OLD_SIGNATURE
)
318 datafellows
= SSH_BUG_SIGBLOB
;
320 key
= key_from_blob(blob
, blen
);
322 Identity
*id
= lookup_identity(key
, 2);
323 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0))
324 ok
= key_sign(id
->key
, &signature
, &slen
, data
, dlen
);
329 buffer_put_char(&msg
, SSH2_AGENT_SIGN_RESPONSE
);
330 buffer_put_string(&msg
, signature
, slen
);
332 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
334 buffer_put_int(&e
->output
, buffer_len(&msg
));
335 buffer_append(&e
->output
, buffer_ptr(&msg
),
340 if (signature
!= NULL
)
342 datafellows
= odatafellows
;
347 process_remove_identity(SocketEntry
*e
, int version
)
356 key
= key_new(KEY_RSA1
);
357 bits
= buffer_get_int(&e
->request
);
358 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
359 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
361 if (bits
!= key_size(key
))
362 logit("Warning: identity keysize mismatch: actual %u, announced %u",
363 key_size(key
), bits
);
366 blob
= buffer_get_string(&e
->request
, &blen
);
367 key
= key_from_blob(blob
, blen
);
372 Identity
*id
= lookup_identity(key
, version
);
375 * We have this key. Free the old key. Since we
376 * don't want to leave empty slots in the middle of
377 * the array, we actually free the key there and move
378 * all the entries between the empty slot and the end
381 Idtab
*tab
= idtab_lookup(version
);
382 if (tab
->nentries
< 1)
383 fatal("process_remove_identity: "
384 "internal error: tab->nentries %d",
386 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
393 buffer_put_int(&e
->output
, 1);
394 buffer_put_char(&e
->output
,
395 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
399 process_remove_all_identities(SocketEntry
*e
, int version
)
401 Idtab
*tab
= idtab_lookup(version
);
404 /* Loop over all identities and clear the keys. */
405 while ((id
= TAILQ_FIRST(&tab
->idlist
)) != NULL
) {
406 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
410 /* Mark that there are no identities. */
414 buffer_put_int(&e
->output
, 1);
415 buffer_put_char(&e
->output
, SSH_AGENT_SUCCESS
);
418 /* removes expired keys and returns number of seconds until the next expiry */
422 u_int deadline
= 0, now
= time(NULL
);
427 for (version
= 1; version
< 3; version
++) {
428 tab
= idtab_lookup(version
);
429 for (id
= TAILQ_FIRST(&tab
->idlist
); id
; id
= nxt
) {
430 nxt
= TAILQ_NEXT(id
, next
);
433 if (now
>= id
->death
) {
434 debug("expiring key '%s'", id
->comment
);
435 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
439 deadline
= (deadline
== 0) ? id
->death
:
440 MIN(deadline
, id
->death
);
443 if (deadline
== 0 || deadline
<= now
)
446 return (deadline
- now
);
450 process_add_identity(SocketEntry
*e
, int version
)
452 Idtab
*tab
= idtab_lookup(version
);
454 int type
, success
= 0, death
= 0, confirm
= 0;
455 char *type_name
, *comment
;
460 k
= key_new_private(KEY_RSA1
);
461 (void) buffer_get_int(&e
->request
); /* ignored */
462 buffer_get_bignum(&e
->request
, k
->rsa
->n
);
463 buffer_get_bignum(&e
->request
, k
->rsa
->e
);
464 buffer_get_bignum(&e
->request
, k
->rsa
->d
);
465 buffer_get_bignum(&e
->request
, k
->rsa
->iqmp
);
467 /* SSH and SSL have p and q swapped */
468 buffer_get_bignum(&e
->request
, k
->rsa
->q
); /* p */
469 buffer_get_bignum(&e
->request
, k
->rsa
->p
); /* q */
471 /* Generate additional parameters */
472 rsa_generate_additional_parameters(k
->rsa
);
475 type_name
= buffer_get_string(&e
->request
, NULL
);
476 type
= key_type_from_name(type_name
);
480 k
= key_new_private(type
);
481 buffer_get_bignum2(&e
->request
, k
->dsa
->p
);
482 buffer_get_bignum2(&e
->request
, k
->dsa
->q
);
483 buffer_get_bignum2(&e
->request
, k
->dsa
->g
);
484 buffer_get_bignum2(&e
->request
, k
->dsa
->pub_key
);
485 buffer_get_bignum2(&e
->request
, k
->dsa
->priv_key
);
488 k
= key_new_private(type
);
489 buffer_get_bignum2(&e
->request
, k
->rsa
->n
);
490 buffer_get_bignum2(&e
->request
, k
->rsa
->e
);
491 buffer_get_bignum2(&e
->request
, k
->rsa
->d
);
492 buffer_get_bignum2(&e
->request
, k
->rsa
->iqmp
);
493 buffer_get_bignum2(&e
->request
, k
->rsa
->p
);
494 buffer_get_bignum2(&e
->request
, k
->rsa
->q
);
496 /* Generate additional parameters */
497 rsa_generate_additional_parameters(k
->rsa
);
500 buffer_clear(&e
->request
);
505 /* enable blinding */
509 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
510 error("process_add_identity: RSA_blinding_on failed");
516 comment
= buffer_get_string(&e
->request
, NULL
);
521 while (buffer_len(&e
->request
)) {
522 switch ((type
= buffer_get_char(&e
->request
))) {
523 case SSH_AGENT_CONSTRAIN_LIFETIME
:
524 death
= time(NULL
) + buffer_get_int(&e
->request
);
526 case SSH_AGENT_CONSTRAIN_CONFIRM
:
530 error("process_add_identity: "
531 "Unknown constraint type %d", type
);
538 if (lifetime
&& !death
)
539 death
= time(NULL
) + lifetime
;
540 if ((id
= lookup_identity(k
, version
)) == NULL
) {
541 id
= xmalloc(sizeof(Identity
));
543 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
544 /* Increment the number of identities. */
550 id
->comment
= comment
;
552 id
->confirm
= confirm
;
554 buffer_put_int(&e
->output
, 1);
555 buffer_put_char(&e
->output
,
556 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
559 /* XXX todo: encrypt sensitive data with passphrase */
561 process_lock_agent(SocketEntry
*e
, int lock
)
566 passwd
= buffer_get_string(&e
->request
, NULL
);
567 if (locked
&& !lock
&& strcmp(passwd
, lock_passwd
) == 0) {
569 memset(lock_passwd
, 0, strlen(lock_passwd
));
573 } else if (!locked
&& lock
) {
575 lock_passwd
= xstrdup(passwd
);
578 memset(passwd
, 0, strlen(passwd
));
581 buffer_put_int(&e
->output
, 1);
582 buffer_put_char(&e
->output
,
583 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
587 no_identities(SocketEntry
*e
, u_int type
)
592 buffer_put_char(&msg
,
593 (type
== SSH_AGENTC_REQUEST_RSA_IDENTITIES
) ?
594 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
595 buffer_put_int(&msg
, 0);
596 buffer_put_int(&e
->output
, buffer_len(&msg
));
597 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
603 process_add_smartcard_key(SocketEntry
*e
)
605 char *sc_reader_id
= NULL
, *pin
;
606 int i
, type
, version
, success
= 0, death
= 0, confirm
= 0;
611 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
612 pin
= buffer_get_string(&e
->request
, NULL
);
614 while (buffer_len(&e
->request
)) {
615 switch ((type
= buffer_get_char(&e
->request
))) {
616 case SSH_AGENT_CONSTRAIN_LIFETIME
:
617 death
= time(NULL
) + buffer_get_int(&e
->request
);
619 case SSH_AGENT_CONSTRAIN_CONFIRM
:
623 error("process_add_smartcard_key: "
624 "Unknown constraint type %d", type
);
630 if (lifetime
&& !death
)
631 death
= time(NULL
) + lifetime
;
633 keys
= sc_get_keys(sc_reader_id
, pin
);
637 if (keys
== NULL
|| keys
[0] == NULL
) {
638 error("sc_get_keys failed");
641 for (i
= 0; keys
[i
] != NULL
; i
++) {
643 version
= k
->type
== KEY_RSA1
? 1 : 2;
644 tab
= idtab_lookup(version
);
645 if (lookup_identity(k
, version
) == NULL
) {
646 id
= xmalloc(sizeof(Identity
));
648 id
->comment
= sc_get_key_label(k
);
650 id
->confirm
= confirm
;
651 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
661 buffer_put_int(&e
->output
, 1);
662 buffer_put_char(&e
->output
,
663 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
667 process_remove_smartcard_key(SocketEntry
*e
)
669 char *sc_reader_id
= NULL
, *pin
;
670 int i
, version
, success
= 0;
671 Key
**keys
, *k
= NULL
;
675 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
676 pin
= buffer_get_string(&e
->request
, NULL
);
677 keys
= sc_get_keys(sc_reader_id
, pin
);
681 if (keys
== NULL
|| keys
[0] == NULL
) {
682 error("sc_get_keys failed");
685 for (i
= 0; keys
[i
] != NULL
; i
++) {
687 version
= k
->type
== KEY_RSA1
? 1 : 2;
688 if ((id
= lookup_identity(k
, version
)) != NULL
) {
689 tab
= idtab_lookup(version
);
690 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
700 buffer_put_int(&e
->output
, 1);
701 buffer_put_char(&e
->output
,
702 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
704 #endif /* SMARTCARD */
706 /* dispatch incoming messages */
709 process_message(SocketEntry
*e
)
714 if (buffer_len(&e
->input
) < 5)
715 return; /* Incomplete message. */
716 cp
= buffer_ptr(&e
->input
);
717 msg_len
= get_u32(cp
);
718 if (msg_len
> 256 * 1024) {
722 if (buffer_len(&e
->input
) < msg_len
+ 4)
725 /* move the current input to e->request */
726 buffer_consume(&e
->input
, 4);
727 buffer_clear(&e
->request
);
728 buffer_append(&e
->request
, buffer_ptr(&e
->input
), msg_len
);
729 buffer_consume(&e
->input
, msg_len
);
730 type
= buffer_get_char(&e
->request
);
732 /* check wheter agent is locked */
733 if (locked
&& type
!= SSH_AGENTC_UNLOCK
) {
734 buffer_clear(&e
->request
);
736 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
737 case SSH2_AGENTC_REQUEST_IDENTITIES
:
738 /* send empty lists */
739 no_identities(e
, type
);
742 /* send a fail message for all other request types */
743 buffer_put_int(&e
->output
, 1);
744 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
749 debug("type %d", type
);
751 case SSH_AGENTC_LOCK
:
752 case SSH_AGENTC_UNLOCK
:
753 process_lock_agent(e
, type
== SSH_AGENTC_LOCK
);
756 case SSH_AGENTC_RSA_CHALLENGE
:
757 process_authentication_challenge1(e
);
759 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
760 process_request_identities(e
, 1);
762 case SSH_AGENTC_ADD_RSA_IDENTITY
:
763 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED
:
764 process_add_identity(e
, 1);
766 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
767 process_remove_identity(e
, 1);
769 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
770 process_remove_all_identities(e
, 1);
773 case SSH2_AGENTC_SIGN_REQUEST
:
774 process_sign_request2(e
);
776 case SSH2_AGENTC_REQUEST_IDENTITIES
:
777 process_request_identities(e
, 2);
779 case SSH2_AGENTC_ADD_IDENTITY
:
780 case SSH2_AGENTC_ADD_ID_CONSTRAINED
:
781 process_add_identity(e
, 2);
783 case SSH2_AGENTC_REMOVE_IDENTITY
:
784 process_remove_identity(e
, 2);
786 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
787 process_remove_all_identities(e
, 2);
790 case SSH_AGENTC_ADD_SMARTCARD_KEY
:
791 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED
:
792 process_add_smartcard_key(e
);
794 case SSH_AGENTC_REMOVE_SMARTCARD_KEY
:
795 process_remove_smartcard_key(e
);
797 #endif /* SMARTCARD */
799 /* Unknown message. Respond with failure. */
800 error("Unknown message %d", type
);
801 buffer_clear(&e
->request
);
802 buffer_put_int(&e
->output
, 1);
803 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
809 new_socket(sock_type type
, int fd
)
811 u_int i
, old_alloc
, new_alloc
;
818 for (i
= 0; i
< sockets_alloc
; i
++)
819 if (sockets
[i
].type
== AUTH_UNUSED
) {
821 buffer_init(&sockets
[i
].input
);
822 buffer_init(&sockets
[i
].output
);
823 buffer_init(&sockets
[i
].request
);
824 sockets
[i
].type
= type
;
827 old_alloc
= sockets_alloc
;
828 new_alloc
= sockets_alloc
+ 10;
829 sockets
= xrealloc(sockets
, new_alloc
, sizeof(sockets
[0]));
830 for (i
= old_alloc
; i
< new_alloc
; i
++)
831 sockets
[i
].type
= AUTH_UNUSED
;
832 sockets_alloc
= new_alloc
;
833 sockets
[old_alloc
].fd
= fd
;
834 buffer_init(&sockets
[old_alloc
].input
);
835 buffer_init(&sockets
[old_alloc
].output
);
836 buffer_init(&sockets
[old_alloc
].request
);
837 sockets
[old_alloc
].type
= type
;
841 prepare_select(fd_set
**fdrp
, fd_set
**fdwp
, int *fdl
, u_int
*nallocp
,
842 struct timeval
**tvpp
)
844 u_int i
, sz
, deadline
;
846 static struct timeval tv
;
848 for (i
= 0; i
< sockets_alloc
; i
++) {
849 switch (sockets
[i
].type
) {
851 case AUTH_CONNECTION
:
852 n
= MAX(n
, sockets
[i
].fd
);
857 fatal("Unknown socket type %d", sockets
[i
].type
);
862 sz
= howmany(n
+1, NFDBITS
) * sizeof(fd_mask
);
863 if (*fdrp
== NULL
|| sz
> *nallocp
) {
873 debug("XXX shrink: %d < %d", n
, *fdl
);
875 memset(*fdrp
, 0, sz
);
876 memset(*fdwp
, 0, sz
);
878 for (i
= 0; i
< sockets_alloc
; i
++) {
879 switch (sockets
[i
].type
) {
881 case AUTH_CONNECTION
:
882 FD_SET(sockets
[i
].fd
, *fdrp
);
883 if (buffer_len(&sockets
[i
].output
) > 0)
884 FD_SET(sockets
[i
].fd
, *fdwp
);
891 if (parent_alive_interval
!= 0)
892 deadline
= (deadline
== 0) ? parent_alive_interval
:
893 MIN(deadline
, parent_alive_interval
);
897 tv
.tv_sec
= deadline
;
905 after_select(fd_set
*readset
, fd_set
*writeset
)
907 struct sockaddr_un sunaddr
;
915 for (i
= 0; i
< sockets_alloc
; i
++)
916 switch (sockets
[i
].type
) {
920 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
921 slen
= sizeof(sunaddr
);
922 sock
= accept(sockets
[i
].fd
,
923 (struct sockaddr
*)&sunaddr
, &slen
);
925 error("accept from AUTH_SOCKET: %s",
929 if (getpeereid(sock
, &euid
, &egid
) < 0) {
930 error("getpeereid %d failed: %s",
931 sock
, strerror(errno
));
935 if ((euid
!= 0) && (getuid() != euid
)) {
936 error("uid mismatch: "
937 "peer euid %u != uid %u",
938 (u_int
) euid
, (u_int
) getuid());
942 new_socket(AUTH_CONNECTION
, sock
);
945 case AUTH_CONNECTION
:
946 if (buffer_len(&sockets
[i
].output
) > 0 &&
947 FD_ISSET(sockets
[i
].fd
, writeset
)) {
949 len
= write(sockets
[i
].fd
,
950 buffer_ptr(&sockets
[i
].output
),
951 buffer_len(&sockets
[i
].output
));
952 if (len
== -1 && (errno
== EAGAIN
||
958 close_socket(&sockets
[i
]);
961 buffer_consume(&sockets
[i
].output
, len
);
963 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
965 len
= read(sockets
[i
].fd
, buf
, sizeof(buf
));
966 if (len
== -1 && (errno
== EAGAIN
||
972 close_socket(&sockets
[i
]);
975 buffer_append(&sockets
[i
].input
, buf
, len
);
976 process_message(&sockets
[i
]);
980 fatal("Unknown type %d", sockets
[i
].type
);
1002 cleanup_handler(int sig
)
1009 check_parent_exists(void)
1011 if (parent_pid
!= -1 && kill(parent_pid
, 0) < 0) {
1012 /* printf("Parent has died - Authentication agent exiting.\n"); */
1021 fprintf(stderr
, "usage: %s [options] [command [arg ...]]\n",
1023 fprintf(stderr
, "Options:\n");
1024 fprintf(stderr
, " -c Generate C-shell commands on stdout.\n");
1025 fprintf(stderr
, " -s Generate Bourne shell commands on stdout.\n");
1026 fprintf(stderr
, " -k Kill the current agent.\n");
1027 fprintf(stderr
, " -d Debug mode.\n");
1028 fprintf(stderr
, " -a socket Bind agent socket to given name.\n");
1029 fprintf(stderr
, " -t life Default identity lifetime (seconds).\n");
1034 main(int ac
, char **av
)
1036 int c_flag
= 0, d_flag
= 0, k_flag
= 0, s_flag
= 0;
1037 int sock
, fd
, ch
, result
, saved_errno
;
1039 char *shell
, *format
, *pidstr
, *agentsocket
= NULL
;
1040 fd_set
*readsetp
= NULL
, *writesetp
= NULL
;
1041 struct sockaddr_un sunaddr
;
1044 extern char *optarg
;
1046 char pidstrbuf
[1 + 3 * sizeof pid
];
1047 struct timeval
*tvp
= NULL
;
1050 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1057 SSLeay_add_all_algorithms();
1059 while ((ch
= getopt(ac
, av
, "cdksa:t:")) != -1) {
1080 agentsocket
= optarg
;
1083 if ((lifetime
= convtime(optarg
)) == -1) {
1084 fprintf(stderr
, "Invalid lifetime\n");
1095 if (ac
> 0 && (c_flag
|| k_flag
|| s_flag
|| d_flag
))
1098 if (ac
== 0 && !c_flag
&& !s_flag
) {
1099 shell
= getenv("SHELL");
1100 if (shell
!= NULL
&& (len
= strlen(shell
)) > 2 &&
1101 strncmp(shell
+ len
- 3, "csh", 3) == 0)
1105 const char *errstr
= NULL
;
1107 pidstr
= getenv(SSH_AGENTPID_ENV_NAME
);
1108 if (pidstr
== NULL
) {
1109 fprintf(stderr
, "%s not set, cannot kill agent\n",
1110 SSH_AGENTPID_ENV_NAME
);
1113 pid
= (int)strtonum(pidstr
, 2, INT_MAX
, &errstr
);
1116 "%s=\"%s\", which is not a good PID: %s\n",
1117 SSH_AGENTPID_ENV_NAME
, pidstr
, errstr
);
1120 if (kill(pid
, SIGTERM
) == -1) {
1124 format
= c_flag
? "unsetenv %s;\n" : "unset %s;\n";
1125 printf(format
, SSH_AUTHSOCKET_ENV_NAME
);
1126 printf(format
, SSH_AGENTPID_ENV_NAME
);
1127 printf("echo Agent pid %ld killed;\n", (long)pid
);
1130 parent_pid
= getpid();
1132 if (agentsocket
== NULL
) {
1133 /* Create private directory for agent socket */
1134 strlcpy(socket_dir
, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir
);
1135 if (mkdtemp(socket_dir
) == NULL
) {
1136 perror("mkdtemp: private socket dir");
1139 snprintf(socket_name
, sizeof socket_name
, "%s/agent.%ld", socket_dir
,
1142 /* Try to use specified agent socket */
1143 socket_dir
[0] = '\0';
1144 strlcpy(socket_name
, agentsocket
, sizeof socket_name
);
1148 * Create socket early so it will exist before command gets run from
1151 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1154 *socket_name
= '\0'; /* Don't unlink any existing file */
1157 memset(&sunaddr
, 0, sizeof(sunaddr
));
1158 sunaddr
.sun_family
= AF_UNIX
;
1159 strlcpy(sunaddr
.sun_path
, socket_name
, sizeof(sunaddr
.sun_path
));
1160 if (bind(sock
, (struct sockaddr
*)&sunaddr
, sizeof(sunaddr
)) < 0) {
1162 *socket_name
= '\0'; /* Don't unlink any existing file */
1165 if (listen(sock
, SSH_LISTEN_BACKLOG
) < 0) {
1171 * Fork, and have the parent execute the command, if any, or present
1172 * the socket data. The child continues as the authentication agent.
1175 log_init(__progname
, SYSLOG_LEVEL_DEBUG1
, SYSLOG_FACILITY_AUTH
, 1);
1176 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1177 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1178 SSH_AUTHSOCKET_ENV_NAME
);
1179 printf("echo Agent pid %ld;\n", (long)parent_pid
);
1187 if (pid
!= 0) { /* Parent - execute the given command. */
1189 snprintf(pidstrbuf
, sizeof pidstrbuf
, "%ld", (long)pid
);
1191 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1192 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1193 SSH_AUTHSOCKET_ENV_NAME
);
1194 printf(format
, SSH_AGENTPID_ENV_NAME
, pidstrbuf
,
1195 SSH_AGENTPID_ENV_NAME
);
1196 printf("echo Agent pid %ld;\n", (long)pid
);
1199 if (setenv(SSH_AUTHSOCKET_ENV_NAME
, socket_name
, 1) == -1 ||
1200 setenv(SSH_AGENTPID_ENV_NAME
, pidstrbuf
, 1) == -1) {
1209 log_init(__progname
, SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_AUTH
, 0);
1211 if (setsid() == -1) {
1212 error("setsid: %s", strerror(errno
));
1217 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) != -1) {
1218 /* XXX might close listen socket */
1219 (void)dup2(fd
, STDIN_FILENO
);
1220 (void)dup2(fd
, STDOUT_FILENO
);
1221 (void)dup2(fd
, STDERR_FILENO
);
1226 /* deny core dumps, since memory contains unencrypted private keys */
1227 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
1228 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0) {
1229 error("setrlimit RLIMIT_CORE: %s", strerror(errno
));
1234 new_socket(AUTH_SOCKET
, sock
);
1236 parent_alive_interval
= 10;
1239 signal(SIGINT
, SIG_IGN
);
1240 signal(SIGPIPE
, SIG_IGN
);
1241 signal(SIGHUP
, cleanup_handler
);
1242 signal(SIGTERM
, cleanup_handler
);
1246 prepare_select(&readsetp
, &writesetp
, &max_fd
, &nalloc
, &tvp
);
1247 result
= select(max_fd
+ 1, readsetp
, writesetp
, NULL
, tvp
);
1248 saved_errno
= errno
;
1249 if (parent_alive_interval
!= 0)
1250 check_parent_exists();
1251 (void) reaper(); /* remove expired keys */
1253 if (saved_errno
== EINTR
)
1255 fatal("select: %s", strerror(saved_errno
));
1256 } else if (result
> 0)
1257 after_select(readsetp
, writesetp
);