1 /* $OpenBSD: ssh-agent.c,v 1.162 2009/09/01 14:43:17 djm 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
;
322 blob
= buffer_get_string(&e
->request
, &blen
);
323 data
= buffer_get_string(&e
->request
, &dlen
);
325 flags
= buffer_get_int(&e
->request
);
326 odatafellows
= datafellows
;
327 if (flags
& SSH_AGENT_OLD_SIGNATURE
)
328 datafellows
= SSH_BUG_SIGBLOB
;
330 key
= key_from_blob(blob
, blen
);
332 Identity
*id
= lookup_identity(key
, 2);
333 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0))
334 ok
= key_sign(id
->key
, &signature
, &slen
, data
, dlen
);
339 buffer_put_char(&msg
, SSH2_AGENT_SIGN_RESPONSE
);
340 buffer_put_string(&msg
, signature
, slen
);
342 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
344 buffer_put_int(&e
->output
, buffer_len(&msg
));
345 buffer_append(&e
->output
, buffer_ptr(&msg
),
350 if (signature
!= NULL
)
352 datafellows
= odatafellows
;
357 process_remove_identity(SocketEntry
*e
, int version
)
366 key
= key_new(KEY_RSA1
);
367 bits
= buffer_get_int(&e
->request
);
368 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
369 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
371 if (bits
!= key_size(key
))
372 logit("Warning: identity keysize mismatch: actual %u, announced %u",
373 key_size(key
), bits
);
376 blob
= buffer_get_string(&e
->request
, &blen
);
377 key
= key_from_blob(blob
, blen
);
382 Identity
*id
= lookup_identity(key
, version
);
385 * We have this key. Free the old key. Since we
386 * don't want to leave empty slots in the middle of
387 * the array, we actually free the key there and move
388 * all the entries between the empty slot and the end
391 Idtab
*tab
= idtab_lookup(version
);
392 if (tab
->nentries
< 1)
393 fatal("process_remove_identity: "
394 "internal error: tab->nentries %d",
396 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
403 buffer_put_int(&e
->output
, 1);
404 buffer_put_char(&e
->output
,
405 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
409 process_remove_all_identities(SocketEntry
*e
, int version
)
411 Idtab
*tab
= idtab_lookup(version
);
414 /* Loop over all identities and clear the keys. */
415 for (id
= TAILQ_FIRST(&tab
->idlist
); id
;
416 id
= TAILQ_FIRST(&tab
->idlist
)) {
417 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
421 /* Mark that there are no identities. */
425 buffer_put_int(&e
->output
, 1);
426 buffer_put_char(&e
->output
, SSH_AGENT_SUCCESS
);
429 /* removes expired keys and returns number of seconds until the next expiry */
433 u_int deadline
= 0, now
= time(NULL
);
438 for (version
= 1; version
< 3; version
++) {
439 tab
= idtab_lookup(version
);
440 for (id
= TAILQ_FIRST(&tab
->idlist
); id
; id
= nxt
) {
441 nxt
= TAILQ_NEXT(id
, next
);
444 if (now
>= id
->death
) {
445 debug("expiring key '%s'", id
->comment
);
446 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
450 deadline
= (deadline
== 0) ? id
->death
:
451 MIN(deadline
, id
->death
);
454 if (deadline
== 0 || deadline
<= now
)
457 return (deadline
- now
);
461 process_add_identity(SocketEntry
*e
, int version
)
463 Idtab
*tab
= idtab_lookup(version
);
465 int type
, success
= 0, death
= 0, confirm
= 0;
466 char *type_name
, *comment
;
471 k
= key_new_private(KEY_RSA1
);
472 (void) buffer_get_int(&e
->request
); /* ignored */
473 buffer_get_bignum(&e
->request
, k
->rsa
->n
);
474 buffer_get_bignum(&e
->request
, k
->rsa
->e
);
475 buffer_get_bignum(&e
->request
, k
->rsa
->d
);
476 buffer_get_bignum(&e
->request
, k
->rsa
->iqmp
);
478 /* SSH and SSL have p and q swapped */
479 buffer_get_bignum(&e
->request
, k
->rsa
->q
); /* p */
480 buffer_get_bignum(&e
->request
, k
->rsa
->p
); /* q */
482 /* Generate additional parameters */
483 rsa_generate_additional_parameters(k
->rsa
);
486 type_name
= buffer_get_string(&e
->request
, NULL
);
487 type
= key_type_from_name(type_name
);
491 k
= key_new_private(type
);
492 buffer_get_bignum2(&e
->request
, k
->dsa
->p
);
493 buffer_get_bignum2(&e
->request
, k
->dsa
->q
);
494 buffer_get_bignum2(&e
->request
, k
->dsa
->g
);
495 buffer_get_bignum2(&e
->request
, k
->dsa
->pub_key
);
496 buffer_get_bignum2(&e
->request
, k
->dsa
->priv_key
);
499 k
= key_new_private(type
);
500 buffer_get_bignum2(&e
->request
, k
->rsa
->n
);
501 buffer_get_bignum2(&e
->request
, k
->rsa
->e
);
502 buffer_get_bignum2(&e
->request
, k
->rsa
->d
);
503 buffer_get_bignum2(&e
->request
, k
->rsa
->iqmp
);
504 buffer_get_bignum2(&e
->request
, k
->rsa
->p
);
505 buffer_get_bignum2(&e
->request
, k
->rsa
->q
);
507 /* Generate additional parameters */
508 rsa_generate_additional_parameters(k
->rsa
);
511 buffer_clear(&e
->request
);
516 /* enable blinding */
520 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
521 error("process_add_identity: RSA_blinding_on failed");
527 comment
= buffer_get_string(&e
->request
, NULL
);
532 while (buffer_len(&e
->request
)) {
533 switch ((type
= buffer_get_char(&e
->request
))) {
534 case SSH_AGENT_CONSTRAIN_LIFETIME
:
535 death
= time(NULL
) + buffer_get_int(&e
->request
);
537 case SSH_AGENT_CONSTRAIN_CONFIRM
:
541 error("process_add_identity: "
542 "Unknown constraint type %d", type
);
549 if (lifetime
&& !death
)
550 death
= time(NULL
) + lifetime
;
551 if ((id
= lookup_identity(k
, version
)) == NULL
) {
552 id
= xmalloc(sizeof(Identity
));
554 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
555 /* Increment the number of identities. */
561 id
->comment
= comment
;
563 id
->confirm
= confirm
;
565 buffer_put_int(&e
->output
, 1);
566 buffer_put_char(&e
->output
,
567 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
570 /* XXX todo: encrypt sensitive data with passphrase */
572 process_lock_agent(SocketEntry
*e
, int lock
)
577 passwd
= buffer_get_string(&e
->request
, NULL
);
578 if (locked
&& !lock
&& strcmp(passwd
, lock_passwd
) == 0) {
580 memset(lock_passwd
, 0, strlen(lock_passwd
));
584 } else if (!locked
&& lock
) {
586 lock_passwd
= xstrdup(passwd
);
589 memset(passwd
, 0, strlen(passwd
));
592 buffer_put_int(&e
->output
, 1);
593 buffer_put_char(&e
->output
,
594 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
598 no_identities(SocketEntry
*e
, u_int type
)
603 buffer_put_char(&msg
,
604 (type
== SSH_AGENTC_REQUEST_RSA_IDENTITIES
) ?
605 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
606 buffer_put_int(&msg
, 0);
607 buffer_put_int(&e
->output
, buffer_len(&msg
));
608 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
614 process_add_smartcard_key(SocketEntry
*e
)
616 char *sc_reader_id
= NULL
, *pin
;
617 int i
, type
, version
, success
= 0, death
= 0, confirm
= 0;
622 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
623 pin
= buffer_get_string(&e
->request
, NULL
);
625 while (buffer_len(&e
->request
)) {
626 switch ((type
= buffer_get_char(&e
->request
))) {
627 case SSH_AGENT_CONSTRAIN_LIFETIME
:
628 death
= time(NULL
) + buffer_get_int(&e
->request
);
630 case SSH_AGENT_CONSTRAIN_CONFIRM
:
634 error("process_add_smartcard_key: "
635 "Unknown constraint type %d", type
);
641 if (lifetime
&& !death
)
642 death
= time(NULL
) + lifetime
;
644 keys
= sc_get_keys(sc_reader_id
, pin
);
648 if (keys
== NULL
|| keys
[0] == NULL
) {
649 error("sc_get_keys failed");
652 for (i
= 0; keys
[i
] != NULL
; i
++) {
654 version
= k
->type
== KEY_RSA1
? 1 : 2;
655 tab
= idtab_lookup(version
);
656 if (lookup_identity(k
, version
) == NULL
) {
657 id
= xmalloc(sizeof(Identity
));
659 id
->comment
= sc_get_key_label(k
);
661 id
->confirm
= confirm
;
662 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
672 buffer_put_int(&e
->output
, 1);
673 buffer_put_char(&e
->output
,
674 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
678 process_remove_smartcard_key(SocketEntry
*e
)
680 char *sc_reader_id
= NULL
, *pin
;
681 int i
, version
, success
= 0;
682 Key
**keys
, *k
= NULL
;
686 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
687 pin
= buffer_get_string(&e
->request
, NULL
);
688 keys
= sc_get_keys(sc_reader_id
, pin
);
692 if (keys
== NULL
|| keys
[0] == NULL
) {
693 error("sc_get_keys failed");
696 for (i
= 0; keys
[i
] != NULL
; i
++) {
698 version
= k
->type
== KEY_RSA1
? 1 : 2;
699 if ((id
= lookup_identity(k
, version
)) != NULL
) {
700 tab
= idtab_lookup(version
);
701 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
711 buffer_put_int(&e
->output
, 1);
712 buffer_put_char(&e
->output
,
713 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
715 #endif /* SMARTCARD */
717 /* dispatch incoming messages */
720 process_message(SocketEntry
*e
)
725 if (buffer_len(&e
->input
) < 5)
726 return; /* Incomplete message. */
727 cp
= buffer_ptr(&e
->input
);
728 msg_len
= get_u32(cp
);
729 if (msg_len
> 256 * 1024) {
733 if (buffer_len(&e
->input
) < msg_len
+ 4)
736 /* move the current input to e->request */
737 buffer_consume(&e
->input
, 4);
738 buffer_clear(&e
->request
);
739 buffer_append(&e
->request
, buffer_ptr(&e
->input
), msg_len
);
740 buffer_consume(&e
->input
, msg_len
);
741 type
= buffer_get_char(&e
->request
);
743 /* check wheter agent is locked */
744 if (locked
&& type
!= SSH_AGENTC_UNLOCK
) {
745 buffer_clear(&e
->request
);
747 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
748 case SSH2_AGENTC_REQUEST_IDENTITIES
:
749 /* send empty lists */
750 no_identities(e
, type
);
753 /* send a fail message for all other request types */
754 buffer_put_int(&e
->output
, 1);
755 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
760 debug("type %d", type
);
762 case SSH_AGENTC_LOCK
:
763 case SSH_AGENTC_UNLOCK
:
764 process_lock_agent(e
, type
== SSH_AGENTC_LOCK
);
767 case SSH_AGENTC_RSA_CHALLENGE
:
768 process_authentication_challenge1(e
);
770 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
771 process_request_identities(e
, 1);
773 case SSH_AGENTC_ADD_RSA_IDENTITY
:
774 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED
:
775 process_add_identity(e
, 1);
777 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
778 process_remove_identity(e
, 1);
780 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
781 process_remove_all_identities(e
, 1);
784 case SSH2_AGENTC_SIGN_REQUEST
:
785 process_sign_request2(e
);
787 case SSH2_AGENTC_REQUEST_IDENTITIES
:
788 process_request_identities(e
, 2);
790 case SSH2_AGENTC_ADD_IDENTITY
:
791 case SSH2_AGENTC_ADD_ID_CONSTRAINED
:
792 process_add_identity(e
, 2);
794 case SSH2_AGENTC_REMOVE_IDENTITY
:
795 process_remove_identity(e
, 2);
797 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
798 process_remove_all_identities(e
, 2);
801 case SSH_AGENTC_ADD_SMARTCARD_KEY
:
802 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED
:
803 process_add_smartcard_key(e
);
805 case SSH_AGENTC_REMOVE_SMARTCARD_KEY
:
806 process_remove_smartcard_key(e
);
808 #endif /* SMARTCARD */
810 /* Unknown message. Respond with failure. */
811 error("Unknown message %d", type
);
812 buffer_clear(&e
->request
);
813 buffer_put_int(&e
->output
, 1);
814 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
820 new_socket(sock_type type
, int fd
)
822 u_int i
, old_alloc
, new_alloc
;
829 for (i
= 0; i
< sockets_alloc
; i
++)
830 if (sockets
[i
].type
== AUTH_UNUSED
) {
832 buffer_init(&sockets
[i
].input
);
833 buffer_init(&sockets
[i
].output
);
834 buffer_init(&sockets
[i
].request
);
835 sockets
[i
].type
= type
;
838 old_alloc
= sockets_alloc
;
839 new_alloc
= sockets_alloc
+ 10;
840 sockets
= xrealloc(sockets
, new_alloc
, sizeof(sockets
[0]));
841 for (i
= old_alloc
; i
< new_alloc
; i
++)
842 sockets
[i
].type
= AUTH_UNUSED
;
843 sockets_alloc
= new_alloc
;
844 sockets
[old_alloc
].fd
= fd
;
845 buffer_init(&sockets
[old_alloc
].input
);
846 buffer_init(&sockets
[old_alloc
].output
);
847 buffer_init(&sockets
[old_alloc
].request
);
848 sockets
[old_alloc
].type
= type
;
852 prepare_select(fd_set
**fdrp
, fd_set
**fdwp
, int *fdl
, u_int
*nallocp
,
853 struct timeval
**tvpp
)
855 u_int i
, sz
, deadline
;
857 static struct timeval tv
;
859 for (i
= 0; i
< sockets_alloc
; i
++) {
860 switch (sockets
[i
].type
) {
862 case AUTH_CONNECTION
:
863 n
= MAX(n
, sockets
[i
].fd
);
868 fatal("Unknown socket type %d", sockets
[i
].type
);
873 sz
= howmany(n
+1, NFDBITS
) * sizeof(fd_mask
);
874 if (*fdrp
== NULL
|| sz
> *nallocp
) {
884 debug("XXX shrink: %d < %d", n
, *fdl
);
886 memset(*fdrp
, 0, sz
);
887 memset(*fdwp
, 0, sz
);
889 for (i
= 0; i
< sockets_alloc
; i
++) {
890 switch (sockets
[i
].type
) {
892 case AUTH_CONNECTION
:
893 FD_SET(sockets
[i
].fd
, *fdrp
);
894 if (buffer_len(&sockets
[i
].output
) > 0)
895 FD_SET(sockets
[i
].fd
, *fdwp
);
902 if (parent_alive_interval
!= 0)
903 deadline
= (deadline
== 0) ? parent_alive_interval
:
904 MIN(deadline
, parent_alive_interval
);
908 tv
.tv_sec
= deadline
;
916 after_select(fd_set
*readset
, fd_set
*writeset
)
918 struct sockaddr_un sunaddr
;
926 for (i
= 0, orig_alloc
= sockets_alloc
; i
< orig_alloc
; i
++)
927 switch (sockets
[i
].type
) {
931 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
932 slen
= sizeof(sunaddr
);
933 sock
= accept(sockets
[i
].fd
,
934 (struct sockaddr
*)&sunaddr
, &slen
);
936 error("accept from AUTH_SOCKET: %s",
940 if (getpeereid(sock
, &euid
, &egid
) < 0) {
941 error("getpeereid %d failed: %s",
942 sock
, strerror(errno
));
946 if ((euid
!= 0) && (getuid() != euid
)) {
947 error("uid mismatch: "
948 "peer euid %u != uid %u",
949 (u_int
) euid
, (u_int
) getuid());
953 new_socket(AUTH_CONNECTION
, sock
);
956 case AUTH_CONNECTION
:
957 if (buffer_len(&sockets
[i
].output
) > 0 &&
958 FD_ISSET(sockets
[i
].fd
, writeset
)) {
959 len
= write(sockets
[i
].fd
,
960 buffer_ptr(&sockets
[i
].output
),
961 buffer_len(&sockets
[i
].output
));
962 if (len
== -1 && (errno
== EAGAIN
||
963 errno
== EWOULDBLOCK
||
967 close_socket(&sockets
[i
]);
970 buffer_consume(&sockets
[i
].output
, len
);
972 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
973 len
= read(sockets
[i
].fd
, buf
, sizeof(buf
));
974 if (len
== -1 && (errno
== EAGAIN
||
975 errno
== EWOULDBLOCK
||
979 close_socket(&sockets
[i
]);
982 buffer_append(&sockets
[i
].input
, buf
, len
);
983 process_message(&sockets
[i
]);
987 fatal("Unknown type %d", sockets
[i
].type
);
1009 cleanup_handler(int sig
)
1016 check_parent_exists(void)
1018 if (parent_pid
!= -1 && kill(parent_pid
, 0) < 0) {
1019 /* printf("Parent has died - Authentication agent exiting.\n"); */
1028 fprintf(stderr
, "usage: %s [options] [command [arg ...]]\n",
1030 fprintf(stderr
, "Options:\n");
1031 fprintf(stderr
, " -c Generate C-shell commands on stdout.\n");
1032 fprintf(stderr
, " -s Generate Bourne shell commands on stdout.\n");
1033 fprintf(stderr
, " -k Kill the current agent.\n");
1034 fprintf(stderr
, " -d Debug mode.\n");
1035 fprintf(stderr
, " -a socket Bind agent socket to given name.\n");
1036 fprintf(stderr
, " -t life Default identity lifetime (seconds).\n");
1041 main(int ac
, char **av
)
1043 int c_flag
= 0, d_flag
= 0, k_flag
= 0, s_flag
= 0;
1044 int sock
, fd
, ch
, result
, saved_errno
;
1046 char *shell
, *format
, *pidstr
, *agentsocket
= NULL
;
1047 fd_set
*readsetp
= NULL
, *writesetp
= NULL
;
1048 struct sockaddr_un sunaddr
;
1049 #ifdef HAVE_SETRLIMIT
1054 extern char *optarg
;
1056 char pidstrbuf
[1 + 3 * sizeof pid
];
1057 struct timeval
*tvp
= NULL
;
1060 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1067 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1068 /* Disable ptrace on Linux without sgid bit */
1069 prctl(PR_SET_DUMPABLE
, 0);
1072 SSLeay_add_all_algorithms();
1074 __progname
= ssh_get_progname(av
[0]);
1078 while ((ch
= getopt(ac
, av
, "cdksa:t:")) != -1) {
1099 agentsocket
= optarg
;
1102 if ((lifetime
= convtime(optarg
)) == -1) {
1103 fprintf(stderr
, "Invalid lifetime\n");
1114 if (ac
> 0 && (c_flag
|| k_flag
|| s_flag
|| d_flag
))
1117 if (ac
== 0 && !c_flag
&& !s_flag
) {
1118 shell
= getenv("SHELL");
1119 if (shell
!= NULL
&& (len
= strlen(shell
)) > 2 &&
1120 strncmp(shell
+ len
- 3, "csh", 3) == 0)
1124 const char *errstr
= NULL
;
1126 pidstr
= getenv(SSH_AGENTPID_ENV_NAME
);
1127 if (pidstr
== NULL
) {
1128 fprintf(stderr
, "%s not set, cannot kill agent\n",
1129 SSH_AGENTPID_ENV_NAME
);
1132 pid
= (int)strtonum(pidstr
, 2, INT_MAX
, &errstr
);
1135 "%s=\"%s\", which is not a good PID: %s\n",
1136 SSH_AGENTPID_ENV_NAME
, pidstr
, errstr
);
1139 if (kill(pid
, SIGTERM
) == -1) {
1143 format
= c_flag
? "unsetenv %s;\n" : "unset %s;\n";
1144 printf(format
, SSH_AUTHSOCKET_ENV_NAME
);
1145 printf(format
, SSH_AGENTPID_ENV_NAME
);
1146 printf("echo Agent pid %ld killed;\n", (long)pid
);
1149 parent_pid
= getpid();
1151 if (agentsocket
== NULL
) {
1152 /* Create private directory for agent socket */
1153 strlcpy(socket_dir
, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir
);
1154 if (mkdtemp(socket_dir
) == NULL
) {
1155 perror("mkdtemp: private socket dir");
1158 snprintf(socket_name
, sizeof socket_name
, "%s/agent.%ld", socket_dir
,
1161 /* Try to use specified agent socket */
1162 socket_dir
[0] = '\0';
1163 strlcpy(socket_name
, agentsocket
, sizeof socket_name
);
1167 * Create socket early so it will exist before command gets run from
1170 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1173 *socket_name
= '\0'; /* Don't unlink any existing file */
1176 memset(&sunaddr
, 0, sizeof(sunaddr
));
1177 sunaddr
.sun_family
= AF_UNIX
;
1178 strlcpy(sunaddr
.sun_path
, socket_name
, sizeof(sunaddr
.sun_path
));
1179 prev_mask
= umask(0177);
1180 if (bind(sock
, (struct sockaddr
*) &sunaddr
, sizeof(sunaddr
)) < 0) {
1182 *socket_name
= '\0'; /* Don't unlink any existing file */
1187 if (listen(sock
, SSH_LISTEN_BACKLOG
) < 0) {
1193 * Fork, and have the parent execute the command, if any, or present
1194 * the socket data. The child continues as the authentication agent.
1197 log_init(__progname
, SYSLOG_LEVEL_DEBUG1
, SYSLOG_FACILITY_AUTH
, 1);
1198 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1199 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1200 SSH_AUTHSOCKET_ENV_NAME
);
1201 printf("echo Agent pid %ld;\n", (long)parent_pid
);
1209 if (pid
!= 0) { /* Parent - execute the given command. */
1211 snprintf(pidstrbuf
, sizeof pidstrbuf
, "%ld", (long)pid
);
1213 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1214 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1215 SSH_AUTHSOCKET_ENV_NAME
);
1216 printf(format
, SSH_AGENTPID_ENV_NAME
, pidstrbuf
,
1217 SSH_AGENTPID_ENV_NAME
);
1218 printf("echo Agent pid %ld;\n", (long)pid
);
1221 if (setenv(SSH_AUTHSOCKET_ENV_NAME
, socket_name
, 1) == -1 ||
1222 setenv(SSH_AGENTPID_ENV_NAME
, pidstrbuf
, 1) == -1) {
1231 log_init(__progname
, SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_AUTH
, 0);
1233 if (setsid() == -1) {
1234 error("setsid: %s", strerror(errno
));
1239 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) != -1) {
1240 /* XXX might close listen socket */
1241 (void)dup2(fd
, STDIN_FILENO
);
1242 (void)dup2(fd
, STDOUT_FILENO
);
1243 (void)dup2(fd
, STDERR_FILENO
);
1248 #ifdef HAVE_SETRLIMIT
1249 /* deny core dumps, since memory contains unencrypted private keys */
1250 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
1251 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0) {
1252 error("setrlimit RLIMIT_CORE: %s", strerror(errno
));
1258 new_socket(AUTH_SOCKET
, sock
);
1260 parent_alive_interval
= 10;
1263 signal(SIGINT
, SIG_IGN
);
1264 signal(SIGPIPE
, SIG_IGN
);
1265 signal(SIGHUP
, cleanup_handler
);
1266 signal(SIGTERM
, cleanup_handler
);
1270 prepare_select(&readsetp
, &writesetp
, &max_fd
, &nalloc
, &tvp
);
1271 result
= select(max_fd
+ 1, readsetp
, writesetp
, NULL
, tvp
);
1272 saved_errno
= errno
;
1273 if (parent_alive_interval
!= 0)
1274 check_parent_exists();
1275 (void) reaper(); /* remove expired keys */
1277 if (saved_errno
== EINTR
)
1279 fatal("select: %s", strerror(saved_errno
));
1280 } else if (result
> 0)
1281 after_select(readsetp
, writesetp
);