1 /* $OpenBSD: ssh-agent.c,v 1.152 2006/08/04 20:46:05 stevesk 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>
81 #if defined(HAVE_SYS_PRCTL_H)
82 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
99 u_int sockets_alloc
= 0;
100 SocketEntry
*sockets
= NULL
;
102 typedef struct identity
{
103 TAILQ_ENTRY(identity
) next
;
112 TAILQ_HEAD(idqueue
, identity
) idlist
;
115 /* private key table, one per protocol version */
120 /* pid of shell == parent of agent */
121 pid_t parent_pid
= -1;
123 /* pathname and directory for AUTH_SOCKET */
124 char socket_name
[MAXPATHLEN
];
125 char socket_dir
[MAXPATHLEN
];
129 char *lock_passwd
= NULL
;
131 extern char *__progname
;
133 /* Default lifetime (0 == forever) */
134 static int lifetime
= 0;
137 close_socket(SocketEntry
*e
)
141 e
->type
= AUTH_UNUSED
;
142 buffer_free(&e
->input
);
143 buffer_free(&e
->output
);
144 buffer_free(&e
->request
);
152 for (i
= 0; i
<=2; i
++) {
153 TAILQ_INIT(&idtable
[i
].idlist
);
154 idtable
[i
].nentries
= 0;
158 /* return private key table for requested protocol version */
160 idtab_lookup(int version
)
162 if (version
< 1 || version
> 2)
163 fatal("internal error, bad protocol version %d", version
);
164 return &idtable
[version
];
168 free_identity(Identity
*id
)
175 /* return matching private key for given public key */
177 lookup_identity(Key
*key
, int version
)
181 Idtab
*tab
= idtab_lookup(version
);
182 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
183 if (key_equal(key
, id
->key
))
189 /* Check confirmation of keysign request */
191 confirm_key(Identity
*id
)
196 p
= key_fingerprint(id
->key
, SSH_FP_MD5
, SSH_FP_HEX
);
197 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
205 /* send list of supported public keys to 'client' */
207 process_request_identities(SocketEntry
*e
, int version
)
209 Idtab
*tab
= idtab_lookup(version
);
214 buffer_put_char(&msg
, (version
== 1) ?
215 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
216 buffer_put_int(&msg
, tab
->nentries
);
217 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
218 if (id
->key
->type
== KEY_RSA1
) {
219 buffer_put_int(&msg
, BN_num_bits(id
->key
->rsa
->n
));
220 buffer_put_bignum(&msg
, id
->key
->rsa
->e
);
221 buffer_put_bignum(&msg
, id
->key
->rsa
->n
);
225 key_to_blob(id
->key
, &blob
, &blen
);
226 buffer_put_string(&msg
, blob
, blen
);
229 buffer_put_cstring(&msg
, id
->comment
);
231 buffer_put_int(&e
->output
, buffer_len(&msg
));
232 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
238 process_authentication_challenge1(SocketEntry
*e
)
240 u_char buf
[32], mdbuf
[16], session_id
[16];
250 key
= key_new(KEY_RSA1
);
251 if ((challenge
= BN_new()) == NULL
)
252 fatal("process_authentication_challenge1: BN_new failed");
254 (void) buffer_get_int(&e
->request
); /* ignored */
255 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
256 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
257 buffer_get_bignum(&e
->request
, challenge
);
259 /* Only protocol 1.1 is supported */
260 if (buffer_len(&e
->request
) == 0)
262 buffer_get(&e
->request
, session_id
, 16);
263 response_type
= buffer_get_int(&e
->request
);
264 if (response_type
!= 1)
267 id
= lookup_identity(key
, 1);
268 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0)) {
269 Key
*private = id
->key
;
270 /* Decrypt the challenge using the private key. */
271 if (rsa_private_decrypt(challenge
, challenge
, private->rsa
) <= 0)
274 /* The response is MD5 of decrypted challenge plus session id. */
275 len
= BN_num_bytes(challenge
);
276 if (len
<= 0 || len
> 32) {
277 logit("process_authentication_challenge: bad challenge length %d", len
);
281 BN_bn2bin(challenge
, buf
+ 32 - len
);
283 MD5_Update(&md
, buf
, 32);
284 MD5_Update(&md
, session_id
, 16);
285 MD5_Final(mdbuf
, &md
);
287 /* Send the response. */
288 buffer_put_char(&msg
, SSH_AGENT_RSA_RESPONSE
);
289 for (i
= 0; i
< 16; i
++)
290 buffer_put_char(&msg
, mdbuf
[i
]);
295 /* Unknown identity or protocol error. Send failure. */
296 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
298 buffer_put_int(&e
->output
, buffer_len(&msg
));
299 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
301 BN_clear_free(challenge
);
307 process_sign_request2(SocketEntry
*e
)
309 u_char
*blob
, *data
, *signature
= NULL
;
310 u_int blen
, dlen
, slen
= 0;
311 extern int datafellows
;
318 blob
= buffer_get_string(&e
->request
, &blen
);
319 data
= buffer_get_string(&e
->request
, &dlen
);
321 flags
= buffer_get_int(&e
->request
);
322 if (flags
& SSH_AGENT_OLD_SIGNATURE
)
323 datafellows
= SSH_BUG_SIGBLOB
;
325 key
= key_from_blob(blob
, blen
);
327 Identity
*id
= lookup_identity(key
, 2);
328 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0))
329 ok
= key_sign(id
->key
, &signature
, &slen
, data
, dlen
);
334 buffer_put_char(&msg
, SSH2_AGENT_SIGN_RESPONSE
);
335 buffer_put_string(&msg
, signature
, slen
);
337 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
339 buffer_put_int(&e
->output
, buffer_len(&msg
));
340 buffer_append(&e
->output
, buffer_ptr(&msg
),
345 if (signature
!= NULL
)
351 process_remove_identity(SocketEntry
*e
, int version
)
360 key
= key_new(KEY_RSA1
);
361 bits
= buffer_get_int(&e
->request
);
362 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
363 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
365 if (bits
!= key_size(key
))
366 logit("Warning: identity keysize mismatch: actual %u, announced %u",
367 key_size(key
), bits
);
370 blob
= buffer_get_string(&e
->request
, &blen
);
371 key
= key_from_blob(blob
, blen
);
376 Identity
*id
= lookup_identity(key
, version
);
379 * We have this key. Free the old key. Since we
380 * don't want to leave empty slots in the middle of
381 * the array, we actually free the key there and move
382 * all the entries between the empty slot and the end
385 Idtab
*tab
= idtab_lookup(version
);
386 if (tab
->nentries
< 1)
387 fatal("process_remove_identity: "
388 "internal error: tab->nentries %d",
390 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
397 buffer_put_int(&e
->output
, 1);
398 buffer_put_char(&e
->output
,
399 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
403 process_remove_all_identities(SocketEntry
*e
, int version
)
405 Idtab
*tab
= idtab_lookup(version
);
408 /* Loop over all identities and clear the keys. */
409 for (id
= TAILQ_FIRST(&tab
->idlist
); id
;
410 id
= TAILQ_FIRST(&tab
->idlist
)) {
411 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
415 /* Mark that there are no identities. */
419 buffer_put_int(&e
->output
, 1);
420 buffer_put_char(&e
->output
, SSH_AGENT_SUCCESS
);
426 u_int now
= time(NULL
);
431 for (version
= 1; version
< 3; version
++) {
432 tab
= idtab_lookup(version
);
433 for (id
= TAILQ_FIRST(&tab
->idlist
); id
; id
= nxt
) {
434 nxt
= TAILQ_NEXT(id
, next
);
435 if (id
->death
!= 0 && now
>= id
->death
) {
436 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
445 process_add_identity(SocketEntry
*e
, int version
)
447 Idtab
*tab
= idtab_lookup(version
);
448 int type
, success
= 0, death
= 0, confirm
= 0;
449 char *type_name
, *comment
;
454 k
= key_new_private(KEY_RSA1
);
455 (void) buffer_get_int(&e
->request
); /* ignored */
456 buffer_get_bignum(&e
->request
, k
->rsa
->n
);
457 buffer_get_bignum(&e
->request
, k
->rsa
->e
);
458 buffer_get_bignum(&e
->request
, k
->rsa
->d
);
459 buffer_get_bignum(&e
->request
, k
->rsa
->iqmp
);
461 /* SSH and SSL have p and q swapped */
462 buffer_get_bignum(&e
->request
, k
->rsa
->q
); /* p */
463 buffer_get_bignum(&e
->request
, k
->rsa
->p
); /* q */
465 /* Generate additional parameters */
466 rsa_generate_additional_parameters(k
->rsa
);
469 type_name
= buffer_get_string(&e
->request
, NULL
);
470 type
= key_type_from_name(type_name
);
474 k
= key_new_private(type
);
475 buffer_get_bignum2(&e
->request
, k
->dsa
->p
);
476 buffer_get_bignum2(&e
->request
, k
->dsa
->q
);
477 buffer_get_bignum2(&e
->request
, k
->dsa
->g
);
478 buffer_get_bignum2(&e
->request
, k
->dsa
->pub_key
);
479 buffer_get_bignum2(&e
->request
, k
->dsa
->priv_key
);
482 k
= key_new_private(type
);
483 buffer_get_bignum2(&e
->request
, k
->rsa
->n
);
484 buffer_get_bignum2(&e
->request
, k
->rsa
->e
);
485 buffer_get_bignum2(&e
->request
, k
->rsa
->d
);
486 buffer_get_bignum2(&e
->request
, k
->rsa
->iqmp
);
487 buffer_get_bignum2(&e
->request
, k
->rsa
->p
);
488 buffer_get_bignum2(&e
->request
, k
->rsa
->q
);
490 /* Generate additional parameters */
491 rsa_generate_additional_parameters(k
->rsa
);
494 buffer_clear(&e
->request
);
499 /* enable blinding */
503 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
504 error("process_add_identity: RSA_blinding_on failed");
510 comment
= buffer_get_string(&e
->request
, NULL
);
516 while (buffer_len(&e
->request
)) {
517 switch (buffer_get_char(&e
->request
)) {
518 case SSH_AGENT_CONSTRAIN_LIFETIME
:
519 death
= time(NULL
) + buffer_get_int(&e
->request
);
521 case SSH_AGENT_CONSTRAIN_CONFIRM
:
528 if (lifetime
&& !death
)
529 death
= time(NULL
) + lifetime
;
530 if (lookup_identity(k
, version
) == NULL
) {
531 Identity
*id
= xmalloc(sizeof(Identity
));
533 id
->comment
= comment
;
535 id
->confirm
= confirm
;
536 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
537 /* Increment the number of identities. */
544 buffer_put_int(&e
->output
, 1);
545 buffer_put_char(&e
->output
,
546 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
549 /* XXX todo: encrypt sensitive data with passphrase */
551 process_lock_agent(SocketEntry
*e
, int lock
)
556 passwd
= buffer_get_string(&e
->request
, NULL
);
557 if (locked
&& !lock
&& strcmp(passwd
, lock_passwd
) == 0) {
559 memset(lock_passwd
, 0, strlen(lock_passwd
));
563 } else if (!locked
&& lock
) {
565 lock_passwd
= xstrdup(passwd
);
568 memset(passwd
, 0, strlen(passwd
));
571 buffer_put_int(&e
->output
, 1);
572 buffer_put_char(&e
->output
,
573 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
577 no_identities(SocketEntry
*e
, u_int type
)
582 buffer_put_char(&msg
,
583 (type
== SSH_AGENTC_REQUEST_RSA_IDENTITIES
) ?
584 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
585 buffer_put_int(&msg
, 0);
586 buffer_put_int(&e
->output
, buffer_len(&msg
));
587 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
593 process_add_smartcard_key (SocketEntry
*e
)
595 char *sc_reader_id
= NULL
, *pin
;
596 int i
, version
, success
= 0, death
= 0, confirm
= 0;
601 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
602 pin
= buffer_get_string(&e
->request
, NULL
);
604 while (buffer_len(&e
->request
)) {
605 switch (buffer_get_char(&e
->request
)) {
606 case SSH_AGENT_CONSTRAIN_LIFETIME
:
607 death
= time(NULL
) + buffer_get_int(&e
->request
);
609 case SSH_AGENT_CONSTRAIN_CONFIRM
:
616 if (lifetime
&& !death
)
617 death
= time(NULL
) + lifetime
;
619 keys
= sc_get_keys(sc_reader_id
, pin
);
623 if (keys
== NULL
|| keys
[0] == NULL
) {
624 error("sc_get_keys failed");
627 for (i
= 0; keys
[i
] != NULL
; i
++) {
629 version
= k
->type
== KEY_RSA1
? 1 : 2;
630 tab
= idtab_lookup(version
);
631 if (lookup_identity(k
, version
) == NULL
) {
632 id
= xmalloc(sizeof(Identity
));
634 id
->comment
= sc_get_key_label(k
);
636 id
->confirm
= confirm
;
637 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
647 buffer_put_int(&e
->output
, 1);
648 buffer_put_char(&e
->output
,
649 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
653 process_remove_smartcard_key(SocketEntry
*e
)
655 char *sc_reader_id
= NULL
, *pin
;
656 int i
, version
, success
= 0;
657 Key
**keys
, *k
= NULL
;
661 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
662 pin
= buffer_get_string(&e
->request
, NULL
);
663 keys
= sc_get_keys(sc_reader_id
, pin
);
667 if (keys
== NULL
|| keys
[0] == NULL
) {
668 error("sc_get_keys failed");
671 for (i
= 0; keys
[i
] != NULL
; i
++) {
673 version
= k
->type
== KEY_RSA1
? 1 : 2;
674 if ((id
= lookup_identity(k
, version
)) != NULL
) {
675 tab
= idtab_lookup(version
);
676 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
686 buffer_put_int(&e
->output
, 1);
687 buffer_put_char(&e
->output
,
688 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
690 #endif /* SMARTCARD */
692 /* dispatch incoming messages */
695 process_message(SocketEntry
*e
)
703 if (buffer_len(&e
->input
) < 5)
704 return; /* Incomplete message. */
705 cp
= buffer_ptr(&e
->input
);
706 msg_len
= get_u32(cp
);
707 if (msg_len
> 256 * 1024) {
711 if (buffer_len(&e
->input
) < msg_len
+ 4)
714 /* move the current input to e->request */
715 buffer_consume(&e
->input
, 4);
716 buffer_clear(&e
->request
);
717 buffer_append(&e
->request
, buffer_ptr(&e
->input
), msg_len
);
718 buffer_consume(&e
->input
, msg_len
);
719 type
= buffer_get_char(&e
->request
);
721 /* check wheter agent is locked */
722 if (locked
&& type
!= SSH_AGENTC_UNLOCK
) {
723 buffer_clear(&e
->request
);
725 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
726 case SSH2_AGENTC_REQUEST_IDENTITIES
:
727 /* send empty lists */
728 no_identities(e
, type
);
731 /* send a fail message for all other request types */
732 buffer_put_int(&e
->output
, 1);
733 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
738 debug("type %d", type
);
740 case SSH_AGENTC_LOCK
:
741 case SSH_AGENTC_UNLOCK
:
742 process_lock_agent(e
, type
== SSH_AGENTC_LOCK
);
745 case SSH_AGENTC_RSA_CHALLENGE
:
746 process_authentication_challenge1(e
);
748 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
749 process_request_identities(e
, 1);
751 case SSH_AGENTC_ADD_RSA_IDENTITY
:
752 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED
:
753 process_add_identity(e
, 1);
755 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
756 process_remove_identity(e
, 1);
758 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
759 process_remove_all_identities(e
, 1);
762 case SSH2_AGENTC_SIGN_REQUEST
:
763 process_sign_request2(e
);
765 case SSH2_AGENTC_REQUEST_IDENTITIES
:
766 process_request_identities(e
, 2);
768 case SSH2_AGENTC_ADD_IDENTITY
:
769 case SSH2_AGENTC_ADD_ID_CONSTRAINED
:
770 process_add_identity(e
, 2);
772 case SSH2_AGENTC_REMOVE_IDENTITY
:
773 process_remove_identity(e
, 2);
775 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
776 process_remove_all_identities(e
, 2);
779 case SSH_AGENTC_ADD_SMARTCARD_KEY
:
780 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED
:
781 process_add_smartcard_key(e
);
783 case SSH_AGENTC_REMOVE_SMARTCARD_KEY
:
784 process_remove_smartcard_key(e
);
786 #endif /* SMARTCARD */
788 /* Unknown message. Respond with failure. */
789 error("Unknown message %d", type
);
790 buffer_clear(&e
->request
);
791 buffer_put_int(&e
->output
, 1);
792 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
798 new_socket(sock_type type
, int fd
)
800 u_int i
, old_alloc
, new_alloc
;
807 for (i
= 0; i
< sockets_alloc
; i
++)
808 if (sockets
[i
].type
== AUTH_UNUSED
) {
810 buffer_init(&sockets
[i
].input
);
811 buffer_init(&sockets
[i
].output
);
812 buffer_init(&sockets
[i
].request
);
813 sockets
[i
].type
= type
;
816 old_alloc
= sockets_alloc
;
817 new_alloc
= sockets_alloc
+ 10;
818 sockets
= xrealloc(sockets
, new_alloc
, sizeof(sockets
[0]));
819 for (i
= old_alloc
; i
< new_alloc
; i
++)
820 sockets
[i
].type
= AUTH_UNUSED
;
821 sockets_alloc
= new_alloc
;
822 sockets
[old_alloc
].fd
= fd
;
823 buffer_init(&sockets
[old_alloc
].input
);
824 buffer_init(&sockets
[old_alloc
].output
);
825 buffer_init(&sockets
[old_alloc
].request
);
826 sockets
[old_alloc
].type
= type
;
830 prepare_select(fd_set
**fdrp
, fd_set
**fdwp
, int *fdl
, u_int
*nallocp
)
835 for (i
= 0; i
< sockets_alloc
; i
++) {
836 switch (sockets
[i
].type
) {
838 case AUTH_CONNECTION
:
839 n
= MAX(n
, sockets
[i
].fd
);
844 fatal("Unknown socket type %d", sockets
[i
].type
);
849 sz
= howmany(n
+1, NFDBITS
) * sizeof(fd_mask
);
850 if (*fdrp
== NULL
|| sz
> *nallocp
) {
860 debug("XXX shrink: %d < %d", n
, *fdl
);
862 memset(*fdrp
, 0, sz
);
863 memset(*fdwp
, 0, sz
);
865 for (i
= 0; i
< sockets_alloc
; i
++) {
866 switch (sockets
[i
].type
) {
868 case AUTH_CONNECTION
:
869 FD_SET(sockets
[i
].fd
, *fdrp
);
870 if (buffer_len(&sockets
[i
].output
) > 0)
871 FD_SET(sockets
[i
].fd
, *fdwp
);
881 after_select(fd_set
*readset
, fd_set
*writeset
)
883 struct sockaddr_un sunaddr
;
891 for (i
= 0; i
< sockets_alloc
; i
++)
892 switch (sockets
[i
].type
) {
896 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
897 slen
= sizeof(sunaddr
);
898 sock
= accept(sockets
[i
].fd
,
899 (struct sockaddr
*)&sunaddr
, &slen
);
901 error("accept from AUTH_SOCKET: %s",
905 if (getpeereid(sock
, &euid
, &egid
) < 0) {
906 error("getpeereid %d failed: %s",
907 sock
, strerror(errno
));
911 if ((euid
!= 0) && (getuid() != euid
)) {
912 error("uid mismatch: "
913 "peer euid %u != uid %u",
914 (u_int
) euid
, (u_int
) getuid());
918 new_socket(AUTH_CONNECTION
, sock
);
921 case AUTH_CONNECTION
:
922 if (buffer_len(&sockets
[i
].output
) > 0 &&
923 FD_ISSET(sockets
[i
].fd
, writeset
)) {
925 len
= write(sockets
[i
].fd
,
926 buffer_ptr(&sockets
[i
].output
),
927 buffer_len(&sockets
[i
].output
));
928 if (len
== -1 && (errno
== EAGAIN
||
934 close_socket(&sockets
[i
]);
937 buffer_consume(&sockets
[i
].output
, len
);
939 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
941 len
= read(sockets
[i
].fd
, buf
, sizeof(buf
));
942 if (len
== -1 && (errno
== EAGAIN
||
948 close_socket(&sockets
[i
]);
951 buffer_append(&sockets
[i
].input
, buf
, len
);
952 process_message(&sockets
[i
]);
956 fatal("Unknown type %d", sockets
[i
].type
);
978 cleanup_handler(int sig
)
986 check_parent_exists(int sig
)
988 int save_errno
= errno
;
990 if (parent_pid
!= -1 && kill(parent_pid
, 0) < 0) {
991 /* printf("Parent has died - Authentication agent exiting.\n"); */
992 cleanup_handler(sig
); /* safe */
994 mysignal(SIGALRM
, check_parent_exists
);
1002 fprintf(stderr
, "Usage: %s [options] [command [args ...]]\n",
1004 fprintf(stderr
, "Options:\n");
1005 fprintf(stderr
, " -c Generate C-shell commands on stdout.\n");
1006 fprintf(stderr
, " -s Generate Bourne shell commands on stdout.\n");
1007 fprintf(stderr
, " -k Kill the current agent.\n");
1008 fprintf(stderr
, " -d Debug mode.\n");
1009 fprintf(stderr
, " -a socket Bind agent socket to given name.\n");
1010 fprintf(stderr
, " -t life Default identity lifetime (seconds).\n");
1015 main(int ac
, char **av
)
1017 int c_flag
= 0, d_flag
= 0, k_flag
= 0, s_flag
= 0;
1020 char *shell
, *format
, *pidstr
, *agentsocket
= NULL
;
1021 fd_set
*readsetp
= NULL
, *writesetp
= NULL
;
1022 struct sockaddr_un sunaddr
;
1023 #ifdef HAVE_SETRLIMIT
1028 extern char *optarg
;
1030 char pidstrbuf
[1 + 3 * sizeof pid
];
1032 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1039 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1040 /* Disable ptrace on Linux without sgid bit */
1041 prctl(PR_SET_DUMPABLE
, 0);
1044 SSLeay_add_all_algorithms();
1046 __progname
= ssh_get_progname(av
[0]);
1050 while ((ch
= getopt(ac
, av
, "cdksa:t:")) != -1) {
1071 agentsocket
= optarg
;
1074 if ((lifetime
= convtime(optarg
)) == -1) {
1075 fprintf(stderr
, "Invalid lifetime\n");
1086 if (ac
> 0 && (c_flag
|| k_flag
|| s_flag
|| d_flag
))
1089 if (ac
== 0 && !c_flag
&& !s_flag
) {
1090 shell
= getenv("SHELL");
1091 if (shell
!= NULL
&&
1092 strncmp(shell
+ strlen(shell
) - 3, "csh", 3) == 0)
1096 const char *errstr
= NULL
;
1098 pidstr
= getenv(SSH_AGENTPID_ENV_NAME
);
1099 if (pidstr
== NULL
) {
1100 fprintf(stderr
, "%s not set, cannot kill agent\n",
1101 SSH_AGENTPID_ENV_NAME
);
1104 pid
= (int)strtonum(pidstr
, 2, INT_MAX
, &errstr
);
1107 "%s=\"%s\", which is not a good PID: %s\n",
1108 SSH_AGENTPID_ENV_NAME
, pidstr
, errstr
);
1111 if (kill(pid
, SIGTERM
) == -1) {
1115 format
= c_flag
? "unsetenv %s;\n" : "unset %s;\n";
1116 printf(format
, SSH_AUTHSOCKET_ENV_NAME
);
1117 printf(format
, SSH_AGENTPID_ENV_NAME
);
1118 printf("echo Agent pid %ld killed;\n", (long)pid
);
1121 parent_pid
= getpid();
1123 if (agentsocket
== NULL
) {
1124 /* Create private directory for agent socket */
1125 strlcpy(socket_dir
, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir
);
1126 if (mkdtemp(socket_dir
) == NULL
) {
1127 perror("mkdtemp: private socket dir");
1130 snprintf(socket_name
, sizeof socket_name
, "%s/agent.%ld", socket_dir
,
1133 /* Try to use specified agent socket */
1134 socket_dir
[0] = '\0';
1135 strlcpy(socket_name
, agentsocket
, sizeof socket_name
);
1139 * Create socket early so it will exist before command gets run from
1142 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1145 *socket_name
= '\0'; /* Don't unlink any existing file */
1148 memset(&sunaddr
, 0, sizeof(sunaddr
));
1149 sunaddr
.sun_family
= AF_UNIX
;
1150 strlcpy(sunaddr
.sun_path
, socket_name
, sizeof(sunaddr
.sun_path
));
1151 prev_mask
= umask(0177);
1152 if (bind(sock
, (struct sockaddr
*) &sunaddr
, sizeof(sunaddr
)) < 0) {
1154 *socket_name
= '\0'; /* Don't unlink any existing file */
1159 if (listen(sock
, SSH_LISTEN_BACKLOG
) < 0) {
1165 * Fork, and have the parent execute the command, if any, or present
1166 * the socket data. The child continues as the authentication agent.
1169 log_init(__progname
, SYSLOG_LEVEL_DEBUG1
, SYSLOG_FACILITY_AUTH
, 1);
1170 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1171 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1172 SSH_AUTHSOCKET_ENV_NAME
);
1173 printf("echo Agent pid %ld;\n", (long)parent_pid
);
1181 if (pid
!= 0) { /* Parent - execute the given command. */
1183 snprintf(pidstrbuf
, sizeof pidstrbuf
, "%ld", (long)pid
);
1185 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1186 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1187 SSH_AUTHSOCKET_ENV_NAME
);
1188 printf(format
, SSH_AGENTPID_ENV_NAME
, pidstrbuf
,
1189 SSH_AGENTPID_ENV_NAME
);
1190 printf("echo Agent pid %ld;\n", (long)pid
);
1193 if (setenv(SSH_AUTHSOCKET_ENV_NAME
, socket_name
, 1) == -1 ||
1194 setenv(SSH_AGENTPID_ENV_NAME
, pidstrbuf
, 1) == -1) {
1203 log_init(__progname
, SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_AUTH
, 0);
1205 if (setsid() == -1) {
1206 error("setsid: %s", strerror(errno
));
1211 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) != -1) {
1212 /* XXX might close listen socket */
1213 (void)dup2(fd
, STDIN_FILENO
);
1214 (void)dup2(fd
, STDOUT_FILENO
);
1215 (void)dup2(fd
, STDERR_FILENO
);
1220 #ifdef HAVE_SETRLIMIT
1221 /* deny core dumps, since memory contains unencrypted private keys */
1222 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
1223 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0) {
1224 error("setrlimit RLIMIT_CORE: %s", strerror(errno
));
1230 new_socket(AUTH_SOCKET
, sock
);
1232 mysignal(SIGALRM
, check_parent_exists
);
1237 signal(SIGINT
, SIG_IGN
);
1238 signal(SIGPIPE
, SIG_IGN
);
1239 signal(SIGHUP
, cleanup_handler
);
1240 signal(SIGTERM
, cleanup_handler
);
1244 prepare_select(&readsetp
, &writesetp
, &max_fd
, &nalloc
);
1245 if (select(max_fd
+ 1, readsetp
, writesetp
, NULL
, NULL
) < 0) {
1248 fatal("select: %s", strerror(errno
));
1250 after_select(readsetp
, writesetp
);