1 /* $OpenBSD: ssh-agent.c,v 1.153 2006/10/06 02:29:19 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>
82 #if defined(HAVE_SYS_PRCTL_H)
83 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
100 u_int sockets_alloc
= 0;
101 SocketEntry
*sockets
= NULL
;
103 typedef struct identity
{
104 TAILQ_ENTRY(identity
) next
;
113 TAILQ_HEAD(idqueue
, identity
) idlist
;
116 /* private key table, one per protocol version */
121 /* pid of shell == parent of agent */
122 pid_t parent_pid
= -1;
124 /* pathname and directory for AUTH_SOCKET */
125 char socket_name
[MAXPATHLEN
];
126 char socket_dir
[MAXPATHLEN
];
130 char *lock_passwd
= NULL
;
132 extern char *__progname
;
134 /* Default lifetime (0 == forever) */
135 static int lifetime
= 0;
138 close_socket(SocketEntry
*e
)
142 e
->type
= AUTH_UNUSED
;
143 buffer_free(&e
->input
);
144 buffer_free(&e
->output
);
145 buffer_free(&e
->request
);
153 for (i
= 0; i
<=2; i
++) {
154 TAILQ_INIT(&idtable
[i
].idlist
);
155 idtable
[i
].nentries
= 0;
159 /* return private key table for requested protocol version */
161 idtab_lookup(int version
)
163 if (version
< 1 || version
> 2)
164 fatal("internal error, bad protocol version %d", version
);
165 return &idtable
[version
];
169 free_identity(Identity
*id
)
176 /* return matching private key for given public key */
178 lookup_identity(Key
*key
, int version
)
182 Idtab
*tab
= idtab_lookup(version
);
183 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
184 if (key_equal(key
, id
->key
))
190 /* Check confirmation of keysign request */
192 confirm_key(Identity
*id
)
197 p
= key_fingerprint(id
->key
, SSH_FP_MD5
, SSH_FP_HEX
);
198 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
206 /* send list of supported public keys to 'client' */
208 process_request_identities(SocketEntry
*e
, int version
)
210 Idtab
*tab
= idtab_lookup(version
);
215 buffer_put_char(&msg
, (version
== 1) ?
216 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
217 buffer_put_int(&msg
, tab
->nentries
);
218 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
219 if (id
->key
->type
== KEY_RSA1
) {
220 buffer_put_int(&msg
, BN_num_bits(id
->key
->rsa
->n
));
221 buffer_put_bignum(&msg
, id
->key
->rsa
->e
);
222 buffer_put_bignum(&msg
, id
->key
->rsa
->n
);
226 key_to_blob(id
->key
, &blob
, &blen
);
227 buffer_put_string(&msg
, blob
, blen
);
230 buffer_put_cstring(&msg
, id
->comment
);
232 buffer_put_int(&e
->output
, buffer_len(&msg
));
233 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
239 process_authentication_challenge1(SocketEntry
*e
)
241 u_char buf
[32], mdbuf
[16], session_id
[16];
251 key
= key_new(KEY_RSA1
);
252 if ((challenge
= BN_new()) == NULL
)
253 fatal("process_authentication_challenge1: BN_new failed");
255 (void) buffer_get_int(&e
->request
); /* ignored */
256 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
257 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
258 buffer_get_bignum(&e
->request
, challenge
);
260 /* Only protocol 1.1 is supported */
261 if (buffer_len(&e
->request
) == 0)
263 buffer_get(&e
->request
, session_id
, 16);
264 response_type
= buffer_get_int(&e
->request
);
265 if (response_type
!= 1)
268 id
= lookup_identity(key
, 1);
269 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0)) {
270 Key
*private = id
->key
;
271 /* Decrypt the challenge using the private key. */
272 if (rsa_private_decrypt(challenge
, challenge
, private->rsa
) <= 0)
275 /* The response is MD5 of decrypted challenge plus session id. */
276 len
= BN_num_bytes(challenge
);
277 if (len
<= 0 || len
> 32) {
278 logit("process_authentication_challenge: bad challenge length %d", len
);
282 BN_bn2bin(challenge
, buf
+ 32 - len
);
284 MD5_Update(&md
, buf
, 32);
285 MD5_Update(&md
, session_id
, 16);
286 MD5_Final(mdbuf
, &md
);
288 /* Send the response. */
289 buffer_put_char(&msg
, SSH_AGENT_RSA_RESPONSE
);
290 for (i
= 0; i
< 16; i
++)
291 buffer_put_char(&msg
, mdbuf
[i
]);
296 /* Unknown identity or protocol error. Send failure. */
297 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
299 buffer_put_int(&e
->output
, buffer_len(&msg
));
300 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
302 BN_clear_free(challenge
);
308 process_sign_request2(SocketEntry
*e
)
310 u_char
*blob
, *data
, *signature
= NULL
;
311 u_int blen
, dlen
, slen
= 0;
312 extern int datafellows
;
319 blob
= buffer_get_string(&e
->request
, &blen
);
320 data
= buffer_get_string(&e
->request
, &dlen
);
322 flags
= buffer_get_int(&e
->request
);
323 if (flags
& SSH_AGENT_OLD_SIGNATURE
)
324 datafellows
= SSH_BUG_SIGBLOB
;
326 key
= key_from_blob(blob
, blen
);
328 Identity
*id
= lookup_identity(key
, 2);
329 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0))
330 ok
= key_sign(id
->key
, &signature
, &slen
, data
, dlen
);
335 buffer_put_char(&msg
, SSH2_AGENT_SIGN_RESPONSE
);
336 buffer_put_string(&msg
, signature
, slen
);
338 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
340 buffer_put_int(&e
->output
, buffer_len(&msg
));
341 buffer_append(&e
->output
, buffer_ptr(&msg
),
346 if (signature
!= NULL
)
352 process_remove_identity(SocketEntry
*e
, int version
)
361 key
= key_new(KEY_RSA1
);
362 bits
= buffer_get_int(&e
->request
);
363 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
364 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
366 if (bits
!= key_size(key
))
367 logit("Warning: identity keysize mismatch: actual %u, announced %u",
368 key_size(key
), bits
);
371 blob
= buffer_get_string(&e
->request
, &blen
);
372 key
= key_from_blob(blob
, blen
);
377 Identity
*id
= lookup_identity(key
, version
);
380 * We have this key. Free the old key. Since we
381 * don't want to leave empty slots in the middle of
382 * the array, we actually free the key there and move
383 * all the entries between the empty slot and the end
386 Idtab
*tab
= idtab_lookup(version
);
387 if (tab
->nentries
< 1)
388 fatal("process_remove_identity: "
389 "internal error: tab->nentries %d",
391 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
398 buffer_put_int(&e
->output
, 1);
399 buffer_put_char(&e
->output
,
400 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
404 process_remove_all_identities(SocketEntry
*e
, int version
)
406 Idtab
*tab
= idtab_lookup(version
);
409 /* Loop over all identities and clear the keys. */
410 for (id
= TAILQ_FIRST(&tab
->idlist
); id
;
411 id
= TAILQ_FIRST(&tab
->idlist
)) {
412 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
416 /* Mark that there are no identities. */
420 buffer_put_int(&e
->output
, 1);
421 buffer_put_char(&e
->output
, SSH_AGENT_SUCCESS
);
427 u_int now
= time(NULL
);
432 for (version
= 1; version
< 3; version
++) {
433 tab
= idtab_lookup(version
);
434 for (id
= TAILQ_FIRST(&tab
->idlist
); id
; id
= nxt
) {
435 nxt
= TAILQ_NEXT(id
, next
);
436 if (id
->death
!= 0 && now
>= id
->death
) {
437 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
446 process_add_identity(SocketEntry
*e
, int version
)
448 Idtab
*tab
= idtab_lookup(version
);
449 int type
, success
= 0, death
= 0, confirm
= 0;
450 char *type_name
, *comment
;
455 k
= key_new_private(KEY_RSA1
);
456 (void) buffer_get_int(&e
->request
); /* ignored */
457 buffer_get_bignum(&e
->request
, k
->rsa
->n
);
458 buffer_get_bignum(&e
->request
, k
->rsa
->e
);
459 buffer_get_bignum(&e
->request
, k
->rsa
->d
);
460 buffer_get_bignum(&e
->request
, k
->rsa
->iqmp
);
462 /* SSH and SSL have p and q swapped */
463 buffer_get_bignum(&e
->request
, k
->rsa
->q
); /* p */
464 buffer_get_bignum(&e
->request
, k
->rsa
->p
); /* q */
466 /* Generate additional parameters */
467 rsa_generate_additional_parameters(k
->rsa
);
470 type_name
= buffer_get_string(&e
->request
, NULL
);
471 type
= key_type_from_name(type_name
);
475 k
= key_new_private(type
);
476 buffer_get_bignum2(&e
->request
, k
->dsa
->p
);
477 buffer_get_bignum2(&e
->request
, k
->dsa
->q
);
478 buffer_get_bignum2(&e
->request
, k
->dsa
->g
);
479 buffer_get_bignum2(&e
->request
, k
->dsa
->pub_key
);
480 buffer_get_bignum2(&e
->request
, k
->dsa
->priv_key
);
483 k
= key_new_private(type
);
484 buffer_get_bignum2(&e
->request
, k
->rsa
->n
);
485 buffer_get_bignum2(&e
->request
, k
->rsa
->e
);
486 buffer_get_bignum2(&e
->request
, k
->rsa
->d
);
487 buffer_get_bignum2(&e
->request
, k
->rsa
->iqmp
);
488 buffer_get_bignum2(&e
->request
, k
->rsa
->p
);
489 buffer_get_bignum2(&e
->request
, k
->rsa
->q
);
491 /* Generate additional parameters */
492 rsa_generate_additional_parameters(k
->rsa
);
495 buffer_clear(&e
->request
);
500 /* enable blinding */
504 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
505 error("process_add_identity: RSA_blinding_on failed");
511 comment
= buffer_get_string(&e
->request
, NULL
);
517 while (buffer_len(&e
->request
)) {
518 switch (buffer_get_char(&e
->request
)) {
519 case SSH_AGENT_CONSTRAIN_LIFETIME
:
520 death
= time(NULL
) + buffer_get_int(&e
->request
);
522 case SSH_AGENT_CONSTRAIN_CONFIRM
:
529 if (lifetime
&& !death
)
530 death
= time(NULL
) + lifetime
;
531 if (lookup_identity(k
, version
) == NULL
) {
532 Identity
*id
= xmalloc(sizeof(Identity
));
534 id
->comment
= comment
;
536 id
->confirm
= confirm
;
537 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
538 /* Increment the number of identities. */
545 buffer_put_int(&e
->output
, 1);
546 buffer_put_char(&e
->output
,
547 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
550 /* XXX todo: encrypt sensitive data with passphrase */
552 process_lock_agent(SocketEntry
*e
, int lock
)
557 passwd
= buffer_get_string(&e
->request
, NULL
);
558 if (locked
&& !lock
&& strcmp(passwd
, lock_passwd
) == 0) {
560 memset(lock_passwd
, 0, strlen(lock_passwd
));
564 } else if (!locked
&& lock
) {
566 lock_passwd
= xstrdup(passwd
);
569 memset(passwd
, 0, strlen(passwd
));
572 buffer_put_int(&e
->output
, 1);
573 buffer_put_char(&e
->output
,
574 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
578 no_identities(SocketEntry
*e
, u_int type
)
583 buffer_put_char(&msg
,
584 (type
== SSH_AGENTC_REQUEST_RSA_IDENTITIES
) ?
585 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
586 buffer_put_int(&msg
, 0);
587 buffer_put_int(&e
->output
, buffer_len(&msg
));
588 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
594 process_add_smartcard_key (SocketEntry
*e
)
596 char *sc_reader_id
= NULL
, *pin
;
597 int i
, version
, success
= 0, death
= 0, confirm
= 0;
602 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
603 pin
= buffer_get_string(&e
->request
, NULL
);
605 while (buffer_len(&e
->request
)) {
606 switch (buffer_get_char(&e
->request
)) {
607 case SSH_AGENT_CONSTRAIN_LIFETIME
:
608 death
= time(NULL
) + buffer_get_int(&e
->request
);
610 case SSH_AGENT_CONSTRAIN_CONFIRM
:
617 if (lifetime
&& !death
)
618 death
= time(NULL
) + lifetime
;
620 keys
= sc_get_keys(sc_reader_id
, pin
);
624 if (keys
== NULL
|| keys
[0] == NULL
) {
625 error("sc_get_keys failed");
628 for (i
= 0; keys
[i
] != NULL
; i
++) {
630 version
= k
->type
== KEY_RSA1
? 1 : 2;
631 tab
= idtab_lookup(version
);
632 if (lookup_identity(k
, version
) == NULL
) {
633 id
= xmalloc(sizeof(Identity
));
635 id
->comment
= sc_get_key_label(k
);
637 id
->confirm
= confirm
;
638 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
648 buffer_put_int(&e
->output
, 1);
649 buffer_put_char(&e
->output
,
650 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
654 process_remove_smartcard_key(SocketEntry
*e
)
656 char *sc_reader_id
= NULL
, *pin
;
657 int i
, version
, success
= 0;
658 Key
**keys
, *k
= NULL
;
662 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
663 pin
= buffer_get_string(&e
->request
, NULL
);
664 keys
= sc_get_keys(sc_reader_id
, pin
);
668 if (keys
== NULL
|| keys
[0] == NULL
) {
669 error("sc_get_keys failed");
672 for (i
= 0; keys
[i
] != NULL
; i
++) {
674 version
= k
->type
== KEY_RSA1
? 1 : 2;
675 if ((id
= lookup_identity(k
, version
)) != NULL
) {
676 tab
= idtab_lookup(version
);
677 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
687 buffer_put_int(&e
->output
, 1);
688 buffer_put_char(&e
->output
,
689 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
691 #endif /* SMARTCARD */
693 /* dispatch incoming messages */
696 process_message(SocketEntry
*e
)
704 if (buffer_len(&e
->input
) < 5)
705 return; /* Incomplete message. */
706 cp
= buffer_ptr(&e
->input
);
707 msg_len
= get_u32(cp
);
708 if (msg_len
> 256 * 1024) {
712 if (buffer_len(&e
->input
) < msg_len
+ 4)
715 /* move the current input to e->request */
716 buffer_consume(&e
->input
, 4);
717 buffer_clear(&e
->request
);
718 buffer_append(&e
->request
, buffer_ptr(&e
->input
), msg_len
);
719 buffer_consume(&e
->input
, msg_len
);
720 type
= buffer_get_char(&e
->request
);
722 /* check wheter agent is locked */
723 if (locked
&& type
!= SSH_AGENTC_UNLOCK
) {
724 buffer_clear(&e
->request
);
726 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
727 case SSH2_AGENTC_REQUEST_IDENTITIES
:
728 /* send empty lists */
729 no_identities(e
, type
);
732 /* send a fail message for all other request types */
733 buffer_put_int(&e
->output
, 1);
734 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
739 debug("type %d", type
);
741 case SSH_AGENTC_LOCK
:
742 case SSH_AGENTC_UNLOCK
:
743 process_lock_agent(e
, type
== SSH_AGENTC_LOCK
);
746 case SSH_AGENTC_RSA_CHALLENGE
:
747 process_authentication_challenge1(e
);
749 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
750 process_request_identities(e
, 1);
752 case SSH_AGENTC_ADD_RSA_IDENTITY
:
753 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED
:
754 process_add_identity(e
, 1);
756 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
757 process_remove_identity(e
, 1);
759 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
760 process_remove_all_identities(e
, 1);
763 case SSH2_AGENTC_SIGN_REQUEST
:
764 process_sign_request2(e
);
766 case SSH2_AGENTC_REQUEST_IDENTITIES
:
767 process_request_identities(e
, 2);
769 case SSH2_AGENTC_ADD_IDENTITY
:
770 case SSH2_AGENTC_ADD_ID_CONSTRAINED
:
771 process_add_identity(e
, 2);
773 case SSH2_AGENTC_REMOVE_IDENTITY
:
774 process_remove_identity(e
, 2);
776 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
777 process_remove_all_identities(e
, 2);
780 case SSH_AGENTC_ADD_SMARTCARD_KEY
:
781 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED
:
782 process_add_smartcard_key(e
);
784 case SSH_AGENTC_REMOVE_SMARTCARD_KEY
:
785 process_remove_smartcard_key(e
);
787 #endif /* SMARTCARD */
789 /* Unknown message. Respond with failure. */
790 error("Unknown message %d", type
);
791 buffer_clear(&e
->request
);
792 buffer_put_int(&e
->output
, 1);
793 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
799 new_socket(sock_type type
, int fd
)
801 u_int i
, old_alloc
, new_alloc
;
808 for (i
= 0; i
< sockets_alloc
; i
++)
809 if (sockets
[i
].type
== AUTH_UNUSED
) {
811 buffer_init(&sockets
[i
].input
);
812 buffer_init(&sockets
[i
].output
);
813 buffer_init(&sockets
[i
].request
);
814 sockets
[i
].type
= type
;
817 old_alloc
= sockets_alloc
;
818 new_alloc
= sockets_alloc
+ 10;
819 sockets
= xrealloc(sockets
, new_alloc
, sizeof(sockets
[0]));
820 for (i
= old_alloc
; i
< new_alloc
; i
++)
821 sockets
[i
].type
= AUTH_UNUSED
;
822 sockets_alloc
= new_alloc
;
823 sockets
[old_alloc
].fd
= fd
;
824 buffer_init(&sockets
[old_alloc
].input
);
825 buffer_init(&sockets
[old_alloc
].output
);
826 buffer_init(&sockets
[old_alloc
].request
);
827 sockets
[old_alloc
].type
= type
;
831 prepare_select(fd_set
**fdrp
, fd_set
**fdwp
, int *fdl
, u_int
*nallocp
)
836 for (i
= 0; i
< sockets_alloc
; i
++) {
837 switch (sockets
[i
].type
) {
839 case AUTH_CONNECTION
:
840 n
= MAX(n
, sockets
[i
].fd
);
845 fatal("Unknown socket type %d", sockets
[i
].type
);
850 sz
= howmany(n
+1, NFDBITS
) * sizeof(fd_mask
);
851 if (*fdrp
== NULL
|| sz
> *nallocp
) {
861 debug("XXX shrink: %d < %d", n
, *fdl
);
863 memset(*fdrp
, 0, sz
);
864 memset(*fdwp
, 0, sz
);
866 for (i
= 0; i
< sockets_alloc
; i
++) {
867 switch (sockets
[i
].type
) {
869 case AUTH_CONNECTION
:
870 FD_SET(sockets
[i
].fd
, *fdrp
);
871 if (buffer_len(&sockets
[i
].output
) > 0)
872 FD_SET(sockets
[i
].fd
, *fdwp
);
882 after_select(fd_set
*readset
, fd_set
*writeset
)
884 struct sockaddr_un sunaddr
;
892 for (i
= 0; i
< sockets_alloc
; i
++)
893 switch (sockets
[i
].type
) {
897 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
898 slen
= sizeof(sunaddr
);
899 sock
= accept(sockets
[i
].fd
,
900 (struct sockaddr
*)&sunaddr
, &slen
);
902 error("accept from AUTH_SOCKET: %s",
906 if (getpeereid(sock
, &euid
, &egid
) < 0) {
907 error("getpeereid %d failed: %s",
908 sock
, strerror(errno
));
912 if ((euid
!= 0) && (getuid() != euid
)) {
913 error("uid mismatch: "
914 "peer euid %u != uid %u",
915 (u_int
) euid
, (u_int
) getuid());
919 new_socket(AUTH_CONNECTION
, sock
);
922 case AUTH_CONNECTION
:
923 if (buffer_len(&sockets
[i
].output
) > 0 &&
924 FD_ISSET(sockets
[i
].fd
, writeset
)) {
926 len
= write(sockets
[i
].fd
,
927 buffer_ptr(&sockets
[i
].output
),
928 buffer_len(&sockets
[i
].output
));
929 if (len
== -1 && (errno
== EAGAIN
||
935 close_socket(&sockets
[i
]);
938 buffer_consume(&sockets
[i
].output
, len
);
940 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
942 len
= read(sockets
[i
].fd
, buf
, sizeof(buf
));
943 if (len
== -1 && (errno
== EAGAIN
||
949 close_socket(&sockets
[i
]);
952 buffer_append(&sockets
[i
].input
, buf
, len
);
953 process_message(&sockets
[i
]);
957 fatal("Unknown type %d", sockets
[i
].type
);
979 cleanup_handler(int sig
)
987 check_parent_exists(int sig
)
989 int save_errno
= errno
;
991 if (parent_pid
!= -1 && kill(parent_pid
, 0) < 0) {
992 /* printf("Parent has died - Authentication agent exiting.\n"); */
993 cleanup_handler(sig
); /* safe */
995 mysignal(SIGALRM
, check_parent_exists
);
1003 fprintf(stderr
, "Usage: %s [options] [command [args ...]]\n",
1005 fprintf(stderr
, "Options:\n");
1006 fprintf(stderr
, " -c Generate C-shell commands on stdout.\n");
1007 fprintf(stderr
, " -s Generate Bourne shell commands on stdout.\n");
1008 fprintf(stderr
, " -k Kill the current agent.\n");
1009 fprintf(stderr
, " -d Debug mode.\n");
1010 fprintf(stderr
, " -a socket Bind agent socket to given name.\n");
1011 fprintf(stderr
, " -t life Default identity lifetime (seconds).\n");
1016 main(int ac
, char **av
)
1018 int c_flag
= 0, d_flag
= 0, k_flag
= 0, s_flag
= 0;
1021 char *shell
, *format
, *pidstr
, *agentsocket
= NULL
;
1022 fd_set
*readsetp
= NULL
, *writesetp
= NULL
;
1023 struct sockaddr_un sunaddr
;
1024 #ifdef HAVE_SETRLIMIT
1029 extern char *optarg
;
1031 char pidstrbuf
[1 + 3 * sizeof pid
];
1033 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1040 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1041 /* Disable ptrace on Linux without sgid bit */
1042 prctl(PR_SET_DUMPABLE
, 0);
1045 SSLeay_add_all_algorithms();
1047 __progname
= ssh_get_progname(av
[0]);
1051 while ((ch
= getopt(ac
, av
, "cdksa:t:")) != -1) {
1072 agentsocket
= optarg
;
1075 if ((lifetime
= convtime(optarg
)) == -1) {
1076 fprintf(stderr
, "Invalid lifetime\n");
1087 if (ac
> 0 && (c_flag
|| k_flag
|| s_flag
|| d_flag
))
1090 if (ac
== 0 && !c_flag
&& !s_flag
) {
1091 shell
= getenv("SHELL");
1092 if (shell
!= NULL
&&
1093 strncmp(shell
+ strlen(shell
) - 3, "csh", 3) == 0)
1097 const char *errstr
= NULL
;
1099 pidstr
= getenv(SSH_AGENTPID_ENV_NAME
);
1100 if (pidstr
== NULL
) {
1101 fprintf(stderr
, "%s not set, cannot kill agent\n",
1102 SSH_AGENTPID_ENV_NAME
);
1105 pid
= (int)strtonum(pidstr
, 2, INT_MAX
, &errstr
);
1108 "%s=\"%s\", which is not a good PID: %s\n",
1109 SSH_AGENTPID_ENV_NAME
, pidstr
, errstr
);
1112 if (kill(pid
, SIGTERM
) == -1) {
1116 format
= c_flag
? "unsetenv %s;\n" : "unset %s;\n";
1117 printf(format
, SSH_AUTHSOCKET_ENV_NAME
);
1118 printf(format
, SSH_AGENTPID_ENV_NAME
);
1119 printf("echo Agent pid %ld killed;\n", (long)pid
);
1122 parent_pid
= getpid();
1124 if (agentsocket
== NULL
) {
1125 /* Create private directory for agent socket */
1126 strlcpy(socket_dir
, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir
);
1127 if (mkdtemp(socket_dir
) == NULL
) {
1128 perror("mkdtemp: private socket dir");
1131 snprintf(socket_name
, sizeof socket_name
, "%s/agent.%ld", socket_dir
,
1134 /* Try to use specified agent socket */
1135 socket_dir
[0] = '\0';
1136 strlcpy(socket_name
, agentsocket
, sizeof socket_name
);
1140 * Create socket early so it will exist before command gets run from
1143 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1146 *socket_name
= '\0'; /* Don't unlink any existing file */
1149 memset(&sunaddr
, 0, sizeof(sunaddr
));
1150 sunaddr
.sun_family
= AF_UNIX
;
1151 strlcpy(sunaddr
.sun_path
, socket_name
, sizeof(sunaddr
.sun_path
));
1152 prev_mask
= umask(0177);
1153 if (bind(sock
, (struct sockaddr
*) &sunaddr
, sizeof(sunaddr
)) < 0) {
1155 *socket_name
= '\0'; /* Don't unlink any existing file */
1160 if (listen(sock
, SSH_LISTEN_BACKLOG
) < 0) {
1166 * Fork, and have the parent execute the command, if any, or present
1167 * the socket data. The child continues as the authentication agent.
1170 log_init(__progname
, SYSLOG_LEVEL_DEBUG1
, SYSLOG_FACILITY_AUTH
, 1);
1171 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1172 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1173 SSH_AUTHSOCKET_ENV_NAME
);
1174 printf("echo Agent pid %ld;\n", (long)parent_pid
);
1182 if (pid
!= 0) { /* Parent - execute the given command. */
1184 snprintf(pidstrbuf
, sizeof pidstrbuf
, "%ld", (long)pid
);
1186 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1187 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1188 SSH_AUTHSOCKET_ENV_NAME
);
1189 printf(format
, SSH_AGENTPID_ENV_NAME
, pidstrbuf
,
1190 SSH_AGENTPID_ENV_NAME
);
1191 printf("echo Agent pid %ld;\n", (long)pid
);
1194 if (setenv(SSH_AUTHSOCKET_ENV_NAME
, socket_name
, 1) == -1 ||
1195 setenv(SSH_AGENTPID_ENV_NAME
, pidstrbuf
, 1) == -1) {
1204 log_init(__progname
, SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_AUTH
, 0);
1206 if (setsid() == -1) {
1207 error("setsid: %s", strerror(errno
));
1212 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) != -1) {
1213 /* XXX might close listen socket */
1214 (void)dup2(fd
, STDIN_FILENO
);
1215 (void)dup2(fd
, STDOUT_FILENO
);
1216 (void)dup2(fd
, STDERR_FILENO
);
1221 #ifdef HAVE_SETRLIMIT
1222 /* deny core dumps, since memory contains unencrypted private keys */
1223 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
1224 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0) {
1225 error("setrlimit RLIMIT_CORE: %s", strerror(errno
));
1231 new_socket(AUTH_SOCKET
, sock
);
1233 mysignal(SIGALRM
, check_parent_exists
);
1238 signal(SIGINT
, SIG_IGN
);
1239 signal(SIGPIPE
, SIG_IGN
);
1240 signal(SIGHUP
, cleanup_handler
);
1241 signal(SIGTERM
, cleanup_handler
);
1245 prepare_select(&readsetp
, &writesetp
, &max_fd
, &nalloc
);
1246 if (select(max_fd
+ 1, readsetp
, writesetp
, NULL
, NULL
) < 0) {
1249 fatal("select: %s", strerror(errno
));
1251 after_select(readsetp
, writesetp
);