1 /* $OpenBSD: ssh-agent.c,v 1.157 2007/09/25 23:48:57 canacar 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>
54 #include "openbsd-compat/openssl-compat.h"
83 #if defined(HAVE_SYS_PRCTL_H)
84 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
101 u_int sockets_alloc
= 0;
102 SocketEntry
*sockets
= NULL
;
104 typedef struct identity
{
105 TAILQ_ENTRY(identity
) next
;
114 TAILQ_HEAD(idqueue
, identity
) idlist
;
117 /* private key table, one per protocol version */
122 /* pid of shell == parent of agent */
123 pid_t parent_pid
= -1;
124 u_int parent_alive_interval
= 0;
126 /* pathname and directory for AUTH_SOCKET */
127 char socket_name
[MAXPATHLEN
];
128 char socket_dir
[MAXPATHLEN
];
132 char *lock_passwd
= NULL
;
134 extern char *__progname
;
136 /* Default lifetime (0 == forever) */
137 static int lifetime
= 0;
140 close_socket(SocketEntry
*e
)
144 e
->type
= AUTH_UNUSED
;
145 buffer_free(&e
->input
);
146 buffer_free(&e
->output
);
147 buffer_free(&e
->request
);
155 for (i
= 0; i
<=2; i
++) {
156 TAILQ_INIT(&idtable
[i
].idlist
);
157 idtable
[i
].nentries
= 0;
161 /* return private key table for requested protocol version */
163 idtab_lookup(int version
)
165 if (version
< 1 || version
> 2)
166 fatal("internal error, bad protocol version %d", version
);
167 return &idtable
[version
];
171 free_identity(Identity
*id
)
178 /* return matching private key for given public key */
180 lookup_identity(Key
*key
, int version
)
184 Idtab
*tab
= idtab_lookup(version
);
185 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
186 if (key_equal(key
, id
->key
))
192 /* Check confirmation of keysign request */
194 confirm_key(Identity
*id
)
199 p
= key_fingerprint(id
->key
, SSH_FP_MD5
, SSH_FP_HEX
);
200 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
208 /* send list of supported public keys to 'client' */
210 process_request_identities(SocketEntry
*e
, int version
)
212 Idtab
*tab
= idtab_lookup(version
);
217 buffer_put_char(&msg
, (version
== 1) ?
218 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
219 buffer_put_int(&msg
, tab
->nentries
);
220 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
221 if (id
->key
->type
== KEY_RSA1
) {
222 buffer_put_int(&msg
, BN_num_bits(id
->key
->rsa
->n
));
223 buffer_put_bignum(&msg
, id
->key
->rsa
->e
);
224 buffer_put_bignum(&msg
, id
->key
->rsa
->n
);
228 key_to_blob(id
->key
, &blob
, &blen
);
229 buffer_put_string(&msg
, blob
, blen
);
232 buffer_put_cstring(&msg
, id
->comment
);
234 buffer_put_int(&e
->output
, buffer_len(&msg
));
235 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
241 process_authentication_challenge1(SocketEntry
*e
)
243 u_char buf
[32], mdbuf
[16], session_id
[16];
253 key
= key_new(KEY_RSA1
);
254 if ((challenge
= BN_new()) == NULL
)
255 fatal("process_authentication_challenge1: BN_new failed");
257 (void) buffer_get_int(&e
->request
); /* ignored */
258 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
259 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
260 buffer_get_bignum(&e
->request
, challenge
);
262 /* Only protocol 1.1 is supported */
263 if (buffer_len(&e
->request
) == 0)
265 buffer_get(&e
->request
, session_id
, 16);
266 response_type
= buffer_get_int(&e
->request
);
267 if (response_type
!= 1)
270 id
= lookup_identity(key
, 1);
271 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0)) {
272 Key
*private = id
->key
;
273 /* Decrypt the challenge using the private key. */
274 if (rsa_private_decrypt(challenge
, challenge
, private->rsa
) <= 0)
277 /* The response is MD5 of decrypted challenge plus session id. */
278 len
= BN_num_bytes(challenge
);
279 if (len
<= 0 || len
> 32) {
280 logit("process_authentication_challenge: bad challenge length %d", len
);
284 BN_bn2bin(challenge
, buf
+ 32 - len
);
286 MD5_Update(&md
, buf
, 32);
287 MD5_Update(&md
, session_id
, 16);
288 MD5_Final(mdbuf
, &md
);
290 /* Send the response. */
291 buffer_put_char(&msg
, SSH_AGENT_RSA_RESPONSE
);
292 for (i
= 0; i
< 16; i
++)
293 buffer_put_char(&msg
, mdbuf
[i
]);
298 /* Unknown identity or protocol error. Send failure. */
299 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
301 buffer_put_int(&e
->output
, buffer_len(&msg
));
302 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
304 BN_clear_free(challenge
);
310 process_sign_request2(SocketEntry
*e
)
312 u_char
*blob
, *data
, *signature
= NULL
;
313 u_int blen
, dlen
, slen
= 0;
314 extern int datafellows
;
321 blob
= buffer_get_string(&e
->request
, &blen
);
322 data
= buffer_get_string(&e
->request
, &dlen
);
324 flags
= buffer_get_int(&e
->request
);
325 if (flags
& SSH_AGENT_OLD_SIGNATURE
)
326 datafellows
= SSH_BUG_SIGBLOB
;
328 key
= key_from_blob(blob
, blen
);
330 Identity
*id
= lookup_identity(key
, 2);
331 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0))
332 ok
= key_sign(id
->key
, &signature
, &slen
, data
, dlen
);
337 buffer_put_char(&msg
, SSH2_AGENT_SIGN_RESPONSE
);
338 buffer_put_string(&msg
, signature
, slen
);
340 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
342 buffer_put_int(&e
->output
, buffer_len(&msg
));
343 buffer_append(&e
->output
, buffer_ptr(&msg
),
348 if (signature
!= NULL
)
354 process_remove_identity(SocketEntry
*e
, int version
)
363 key
= key_new(KEY_RSA1
);
364 bits
= buffer_get_int(&e
->request
);
365 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
366 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
368 if (bits
!= key_size(key
))
369 logit("Warning: identity keysize mismatch: actual %u, announced %u",
370 key_size(key
), bits
);
373 blob
= buffer_get_string(&e
->request
, &blen
);
374 key
= key_from_blob(blob
, blen
);
379 Identity
*id
= lookup_identity(key
, version
);
382 * We have this key. Free the old key. Since we
383 * don't want to leave empty slots in the middle of
384 * the array, we actually free the key there and move
385 * all the entries between the empty slot and the end
388 Idtab
*tab
= idtab_lookup(version
);
389 if (tab
->nentries
< 1)
390 fatal("process_remove_identity: "
391 "internal error: tab->nentries %d",
393 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
400 buffer_put_int(&e
->output
, 1);
401 buffer_put_char(&e
->output
,
402 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
406 process_remove_all_identities(SocketEntry
*e
, int version
)
408 Idtab
*tab
= idtab_lookup(version
);
411 /* Loop over all identities and clear the keys. */
412 for (id
= TAILQ_FIRST(&tab
->idlist
); id
;
413 id
= TAILQ_FIRST(&tab
->idlist
)) {
414 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
418 /* Mark that there are no identities. */
422 buffer_put_int(&e
->output
, 1);
423 buffer_put_char(&e
->output
, SSH_AGENT_SUCCESS
);
426 /* removes expired keys and returns number of seconds until the next expiry */
430 u_int deadline
= 0, now
= time(NULL
);
435 for (version
= 1; version
< 3; version
++) {
436 tab
= idtab_lookup(version
);
437 for (id
= TAILQ_FIRST(&tab
->idlist
); id
; id
= nxt
) {
438 nxt
= TAILQ_NEXT(id
, next
);
441 if (now
>= id
->death
) {
442 debug("expiring key '%s'", id
->comment
);
443 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
447 deadline
= (deadline
== 0) ? id
->death
:
448 MIN(deadline
, id
->death
);
451 if (deadline
== 0 || deadline
<= now
)
454 return (deadline
- now
);
458 process_add_identity(SocketEntry
*e
, int version
)
460 Idtab
*tab
= idtab_lookup(version
);
462 int type
, success
= 0, death
= 0, confirm
= 0;
463 char *type_name
, *comment
;
468 k
= key_new_private(KEY_RSA1
);
469 (void) buffer_get_int(&e
->request
); /* ignored */
470 buffer_get_bignum(&e
->request
, k
->rsa
->n
);
471 buffer_get_bignum(&e
->request
, k
->rsa
->e
);
472 buffer_get_bignum(&e
->request
, k
->rsa
->d
);
473 buffer_get_bignum(&e
->request
, k
->rsa
->iqmp
);
475 /* SSH and SSL have p and q swapped */
476 buffer_get_bignum(&e
->request
, k
->rsa
->q
); /* p */
477 buffer_get_bignum(&e
->request
, k
->rsa
->p
); /* q */
479 /* Generate additional parameters */
480 rsa_generate_additional_parameters(k
->rsa
);
483 type_name
= buffer_get_string(&e
->request
, NULL
);
484 type
= key_type_from_name(type_name
);
488 k
= key_new_private(type
);
489 buffer_get_bignum2(&e
->request
, k
->dsa
->p
);
490 buffer_get_bignum2(&e
->request
, k
->dsa
->q
);
491 buffer_get_bignum2(&e
->request
, k
->dsa
->g
);
492 buffer_get_bignum2(&e
->request
, k
->dsa
->pub_key
);
493 buffer_get_bignum2(&e
->request
, k
->dsa
->priv_key
);
496 k
= key_new_private(type
);
497 buffer_get_bignum2(&e
->request
, k
->rsa
->n
);
498 buffer_get_bignum2(&e
->request
, k
->rsa
->e
);
499 buffer_get_bignum2(&e
->request
, k
->rsa
->d
);
500 buffer_get_bignum2(&e
->request
, k
->rsa
->iqmp
);
501 buffer_get_bignum2(&e
->request
, k
->rsa
->p
);
502 buffer_get_bignum2(&e
->request
, k
->rsa
->q
);
504 /* Generate additional parameters */
505 rsa_generate_additional_parameters(k
->rsa
);
508 buffer_clear(&e
->request
);
513 /* enable blinding */
517 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
518 error("process_add_identity: RSA_blinding_on failed");
524 comment
= buffer_get_string(&e
->request
, NULL
);
530 while (buffer_len(&e
->request
)) {
531 switch (buffer_get_char(&e
->request
)) {
532 case SSH_AGENT_CONSTRAIN_LIFETIME
:
533 death
= time(NULL
) + buffer_get_int(&e
->request
);
535 case SSH_AGENT_CONSTRAIN_CONFIRM
:
542 if (lifetime
&& !death
)
543 death
= time(NULL
) + lifetime
;
544 if ((id
= lookup_identity(k
, version
)) == NULL
) {
545 id
= xmalloc(sizeof(Identity
));
547 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
548 /* Increment the number of identities. */
554 id
->comment
= comment
;
556 id
->confirm
= confirm
;
558 buffer_put_int(&e
->output
, 1);
559 buffer_put_char(&e
->output
,
560 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
563 /* XXX todo: encrypt sensitive data with passphrase */
565 process_lock_agent(SocketEntry
*e
, int lock
)
570 passwd
= buffer_get_string(&e
->request
, NULL
);
571 if (locked
&& !lock
&& strcmp(passwd
, lock_passwd
) == 0) {
573 memset(lock_passwd
, 0, strlen(lock_passwd
));
577 } else if (!locked
&& lock
) {
579 lock_passwd
= xstrdup(passwd
);
582 memset(passwd
, 0, strlen(passwd
));
585 buffer_put_int(&e
->output
, 1);
586 buffer_put_char(&e
->output
,
587 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
591 no_identities(SocketEntry
*e
, u_int type
)
596 buffer_put_char(&msg
,
597 (type
== SSH_AGENTC_REQUEST_RSA_IDENTITIES
) ?
598 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
599 buffer_put_int(&msg
, 0);
600 buffer_put_int(&e
->output
, buffer_len(&msg
));
601 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
607 process_add_smartcard_key (SocketEntry
*e
)
609 char *sc_reader_id
= NULL
, *pin
;
610 int i
, version
, success
= 0, death
= 0, confirm
= 0;
615 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
616 pin
= buffer_get_string(&e
->request
, NULL
);
618 while (buffer_len(&e
->request
)) {
619 switch (buffer_get_char(&e
->request
)) {
620 case SSH_AGENT_CONSTRAIN_LIFETIME
:
621 death
= time(NULL
) + buffer_get_int(&e
->request
);
623 case SSH_AGENT_CONSTRAIN_CONFIRM
:
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
;
1042 #ifdef HAVE_SETRLIMIT
1047 extern char *optarg
;
1049 char pidstrbuf
[1 + 3 * sizeof pid
];
1050 struct timeval
*tvp
= NULL
;
1052 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1060 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1061 /* Disable ptrace on Linux without sgid bit */
1062 prctl(PR_SET_DUMPABLE
, 0);
1065 SSLeay_add_all_algorithms();
1067 __progname
= ssh_get_progname(av
[0]);
1071 while ((ch
= getopt(ac
, av
, "cdksa:t:")) != -1) {
1092 agentsocket
= optarg
;
1095 if ((lifetime
= convtime(optarg
)) == -1) {
1096 fprintf(stderr
, "Invalid lifetime\n");
1107 if (ac
> 0 && (c_flag
|| k_flag
|| s_flag
|| d_flag
))
1110 if (ac
== 0 && !c_flag
&& !s_flag
) {
1111 shell
= getenv("SHELL");
1112 if (shell
!= NULL
&&
1113 strncmp(shell
+ strlen(shell
) - 3, "csh", 3) == 0)
1117 const char *errstr
= NULL
;
1119 pidstr
= getenv(SSH_AGENTPID_ENV_NAME
);
1120 if (pidstr
== NULL
) {
1121 fprintf(stderr
, "%s not set, cannot kill agent\n",
1122 SSH_AGENTPID_ENV_NAME
);
1125 pid
= (int)strtonum(pidstr
, 2, INT_MAX
, &errstr
);
1128 "%s=\"%s\", which is not a good PID: %s\n",
1129 SSH_AGENTPID_ENV_NAME
, pidstr
, errstr
);
1132 if (kill(pid
, SIGTERM
) == -1) {
1136 format
= c_flag
? "unsetenv %s;\n" : "unset %s;\n";
1137 printf(format
, SSH_AUTHSOCKET_ENV_NAME
);
1138 printf(format
, SSH_AGENTPID_ENV_NAME
);
1139 printf("echo Agent pid %ld killed;\n", (long)pid
);
1142 parent_pid
= getpid();
1144 if (agentsocket
== NULL
) {
1145 /* Create private directory for agent socket */
1146 strlcpy(socket_dir
, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir
);
1147 if (mkdtemp(socket_dir
) == NULL
) {
1148 perror("mkdtemp: private socket dir");
1151 snprintf(socket_name
, sizeof socket_name
, "%s/agent.%ld", socket_dir
,
1154 /* Try to use specified agent socket */
1155 socket_dir
[0] = '\0';
1156 strlcpy(socket_name
, agentsocket
, sizeof socket_name
);
1160 * Create socket early so it will exist before command gets run from
1163 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1166 *socket_name
= '\0'; /* Don't unlink any existing file */
1169 memset(&sunaddr
, 0, sizeof(sunaddr
));
1170 sunaddr
.sun_family
= AF_UNIX
;
1171 strlcpy(sunaddr
.sun_path
, socket_name
, sizeof(sunaddr
.sun_path
));
1172 prev_mask
= umask(0177);
1173 if (bind(sock
, (struct sockaddr
*) &sunaddr
, sizeof(sunaddr
)) < 0) {
1175 *socket_name
= '\0'; /* Don't unlink any existing file */
1180 if (listen(sock
, SSH_LISTEN_BACKLOG
) < 0) {
1186 * Fork, and have the parent execute the command, if any, or present
1187 * the socket data. The child continues as the authentication agent.
1190 log_init(__progname
, SYSLOG_LEVEL_DEBUG1
, SYSLOG_FACILITY_AUTH
, 1);
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("echo Agent pid %ld;\n", (long)parent_pid
);
1202 if (pid
!= 0) { /* Parent - execute the given command. */
1204 snprintf(pidstrbuf
, sizeof pidstrbuf
, "%ld", (long)pid
);
1206 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1207 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1208 SSH_AUTHSOCKET_ENV_NAME
);
1209 printf(format
, SSH_AGENTPID_ENV_NAME
, pidstrbuf
,
1210 SSH_AGENTPID_ENV_NAME
);
1211 printf("echo Agent pid %ld;\n", (long)pid
);
1214 if (setenv(SSH_AUTHSOCKET_ENV_NAME
, socket_name
, 1) == -1 ||
1215 setenv(SSH_AGENTPID_ENV_NAME
, pidstrbuf
, 1) == -1) {
1224 log_init(__progname
, SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_AUTH
, 0);
1226 if (setsid() == -1) {
1227 error("setsid: %s", strerror(errno
));
1232 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) != -1) {
1233 /* XXX might close listen socket */
1234 (void)dup2(fd
, STDIN_FILENO
);
1235 (void)dup2(fd
, STDOUT_FILENO
);
1236 (void)dup2(fd
, STDERR_FILENO
);
1241 #ifdef HAVE_SETRLIMIT
1242 /* deny core dumps, since memory contains unencrypted private keys */
1243 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
1244 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0) {
1245 error("setrlimit RLIMIT_CORE: %s", strerror(errno
));
1251 new_socket(AUTH_SOCKET
, sock
);
1253 parent_alive_interval
= 10;
1256 signal(SIGINT
, SIG_IGN
);
1257 signal(SIGPIPE
, SIG_IGN
);
1258 signal(SIGHUP
, cleanup_handler
);
1259 signal(SIGTERM
, cleanup_handler
);
1263 prepare_select(&readsetp
, &writesetp
, &max_fd
, &nalloc
, &tvp
);
1264 result
= select(max_fd
+ 1, readsetp
, writesetp
, NULL
, tvp
);
1265 saved_errno
= errno
;
1266 if (parent_alive_interval
!= 0)
1267 check_parent_exists();
1268 (void) reaper(); /* remove expired keys */
1270 if (saved_errno
== EINTR
)
1272 fatal("select: %s", strerror(saved_errno
));
1273 } else if (result
> 0)
1274 after_select(readsetp
, writesetp
);