1 /* $OpenBSD: ssh-agent.c,v 1.147 2006/07/25 02:59:21 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>
41 #include <sys/socket.h>
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
48 #include "openbsd-compat/sys-queue.h"
49 #include <sys/resource.h>
51 #include <openssl/evp.h>
52 #include <openssl/md5.h>
79 #if defined(HAVE_SYS_PRCTL_H)
80 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
97 u_int sockets_alloc
= 0;
98 SocketEntry
*sockets
= NULL
;
100 typedef struct identity
{
101 TAILQ_ENTRY(identity
) next
;
110 TAILQ_HEAD(idqueue
, identity
) idlist
;
113 /* private key table, one per protocol version */
118 /* pid of shell == parent of agent */
119 pid_t parent_pid
= -1;
121 /* pathname and directory for AUTH_SOCKET */
122 char socket_name
[MAXPATHLEN
];
123 char socket_dir
[MAXPATHLEN
];
127 char *lock_passwd
= NULL
;
129 extern char *__progname
;
131 /* Default lifetime (0 == forever) */
132 static int lifetime
= 0;
135 close_socket(SocketEntry
*e
)
139 e
->type
= AUTH_UNUSED
;
140 buffer_free(&e
->input
);
141 buffer_free(&e
->output
);
142 buffer_free(&e
->request
);
150 for (i
= 0; i
<=2; i
++) {
151 TAILQ_INIT(&idtable
[i
].idlist
);
152 idtable
[i
].nentries
= 0;
156 /* return private key table for requested protocol version */
158 idtab_lookup(int version
)
160 if (version
< 1 || version
> 2)
161 fatal("internal error, bad protocol version %d", version
);
162 return &idtable
[version
];
166 free_identity(Identity
*id
)
173 /* return matching private key for given public key */
175 lookup_identity(Key
*key
, int version
)
179 Idtab
*tab
= idtab_lookup(version
);
180 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
181 if (key_equal(key
, id
->key
))
187 /* Check confirmation of keysign request */
189 confirm_key(Identity
*id
)
194 p
= key_fingerprint(id
->key
, SSH_FP_MD5
, SSH_FP_HEX
);
195 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
203 /* send list of supported public keys to 'client' */
205 process_request_identities(SocketEntry
*e
, int version
)
207 Idtab
*tab
= idtab_lookup(version
);
212 buffer_put_char(&msg
, (version
== 1) ?
213 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
214 buffer_put_int(&msg
, tab
->nentries
);
215 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
216 if (id
->key
->type
== KEY_RSA1
) {
217 buffer_put_int(&msg
, BN_num_bits(id
->key
->rsa
->n
));
218 buffer_put_bignum(&msg
, id
->key
->rsa
->e
);
219 buffer_put_bignum(&msg
, id
->key
->rsa
->n
);
223 key_to_blob(id
->key
, &blob
, &blen
);
224 buffer_put_string(&msg
, blob
, blen
);
227 buffer_put_cstring(&msg
, id
->comment
);
229 buffer_put_int(&e
->output
, buffer_len(&msg
));
230 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
236 process_authentication_challenge1(SocketEntry
*e
)
238 u_char buf
[32], mdbuf
[16], session_id
[16];
248 key
= key_new(KEY_RSA1
);
249 if ((challenge
= BN_new()) == NULL
)
250 fatal("process_authentication_challenge1: BN_new failed");
252 (void) buffer_get_int(&e
->request
); /* ignored */
253 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
254 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
255 buffer_get_bignum(&e
->request
, challenge
);
257 /* Only protocol 1.1 is supported */
258 if (buffer_len(&e
->request
) == 0)
260 buffer_get(&e
->request
, session_id
, 16);
261 response_type
= buffer_get_int(&e
->request
);
262 if (response_type
!= 1)
265 id
= lookup_identity(key
, 1);
266 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0)) {
267 Key
*private = id
->key
;
268 /* Decrypt the challenge using the private key. */
269 if (rsa_private_decrypt(challenge
, challenge
, private->rsa
) <= 0)
272 /* The response is MD5 of decrypted challenge plus session id. */
273 len
= BN_num_bytes(challenge
);
274 if (len
<= 0 || len
> 32) {
275 logit("process_authentication_challenge: bad challenge length %d", len
);
279 BN_bn2bin(challenge
, buf
+ 32 - len
);
281 MD5_Update(&md
, buf
, 32);
282 MD5_Update(&md
, session_id
, 16);
283 MD5_Final(mdbuf
, &md
);
285 /* Send the response. */
286 buffer_put_char(&msg
, SSH_AGENT_RSA_RESPONSE
);
287 for (i
= 0; i
< 16; i
++)
288 buffer_put_char(&msg
, mdbuf
[i
]);
293 /* Unknown identity or protocol error. Send failure. */
294 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
296 buffer_put_int(&e
->output
, buffer_len(&msg
));
297 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
299 BN_clear_free(challenge
);
305 process_sign_request2(SocketEntry
*e
)
307 u_char
*blob
, *data
, *signature
= NULL
;
308 u_int blen
, dlen
, slen
= 0;
309 extern int datafellows
;
316 blob
= buffer_get_string(&e
->request
, &blen
);
317 data
= buffer_get_string(&e
->request
, &dlen
);
319 flags
= buffer_get_int(&e
->request
);
320 if (flags
& SSH_AGENT_OLD_SIGNATURE
)
321 datafellows
= SSH_BUG_SIGBLOB
;
323 key
= key_from_blob(blob
, blen
);
325 Identity
*id
= lookup_identity(key
, 2);
326 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0))
327 ok
= key_sign(id
->key
, &signature
, &slen
, data
, dlen
);
332 buffer_put_char(&msg
, SSH2_AGENT_SIGN_RESPONSE
);
333 buffer_put_string(&msg
, signature
, slen
);
335 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
337 buffer_put_int(&e
->output
, buffer_len(&msg
));
338 buffer_append(&e
->output
, buffer_ptr(&msg
),
343 if (signature
!= NULL
)
349 process_remove_identity(SocketEntry
*e
, int version
)
358 key
= key_new(KEY_RSA1
);
359 bits
= buffer_get_int(&e
->request
);
360 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
361 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
363 if (bits
!= key_size(key
))
364 logit("Warning: identity keysize mismatch: actual %u, announced %u",
365 key_size(key
), bits
);
368 blob
= buffer_get_string(&e
->request
, &blen
);
369 key
= key_from_blob(blob
, blen
);
374 Identity
*id
= lookup_identity(key
, version
);
377 * We have this key. Free the old key. Since we
378 * don't want to leave empty slots in the middle of
379 * the array, we actually free the key there and move
380 * all the entries between the empty slot and the end
383 Idtab
*tab
= idtab_lookup(version
);
384 if (tab
->nentries
< 1)
385 fatal("process_remove_identity: "
386 "internal error: tab->nentries %d",
388 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
395 buffer_put_int(&e
->output
, 1);
396 buffer_put_char(&e
->output
,
397 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
401 process_remove_all_identities(SocketEntry
*e
, int version
)
403 Idtab
*tab
= idtab_lookup(version
);
406 /* Loop over all identities and clear the keys. */
407 for (id
= TAILQ_FIRST(&tab
->idlist
); id
;
408 id
= TAILQ_FIRST(&tab
->idlist
)) {
409 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
413 /* Mark that there are no identities. */
417 buffer_put_int(&e
->output
, 1);
418 buffer_put_char(&e
->output
, SSH_AGENT_SUCCESS
);
424 u_int now
= time(NULL
);
429 for (version
= 1; version
< 3; version
++) {
430 tab
= idtab_lookup(version
);
431 for (id
= TAILQ_FIRST(&tab
->idlist
); id
; id
= nxt
) {
432 nxt
= TAILQ_NEXT(id
, next
);
433 if (id
->death
!= 0 && now
>= id
->death
) {
434 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
443 process_add_identity(SocketEntry
*e
, int version
)
445 Idtab
*tab
= idtab_lookup(version
);
446 int type
, success
= 0, death
= 0, confirm
= 0;
447 char *type_name
, *comment
;
452 k
= key_new_private(KEY_RSA1
);
453 (void) buffer_get_int(&e
->request
); /* ignored */
454 buffer_get_bignum(&e
->request
, k
->rsa
->n
);
455 buffer_get_bignum(&e
->request
, k
->rsa
->e
);
456 buffer_get_bignum(&e
->request
, k
->rsa
->d
);
457 buffer_get_bignum(&e
->request
, k
->rsa
->iqmp
);
459 /* SSH and SSL have p and q swapped */
460 buffer_get_bignum(&e
->request
, k
->rsa
->q
); /* p */
461 buffer_get_bignum(&e
->request
, k
->rsa
->p
); /* q */
463 /* Generate additional parameters */
464 rsa_generate_additional_parameters(k
->rsa
);
467 type_name
= buffer_get_string(&e
->request
, NULL
);
468 type
= key_type_from_name(type_name
);
472 k
= key_new_private(type
);
473 buffer_get_bignum2(&e
->request
, k
->dsa
->p
);
474 buffer_get_bignum2(&e
->request
, k
->dsa
->q
);
475 buffer_get_bignum2(&e
->request
, k
->dsa
->g
);
476 buffer_get_bignum2(&e
->request
, k
->dsa
->pub_key
);
477 buffer_get_bignum2(&e
->request
, k
->dsa
->priv_key
);
480 k
= key_new_private(type
);
481 buffer_get_bignum2(&e
->request
, k
->rsa
->n
);
482 buffer_get_bignum2(&e
->request
, k
->rsa
->e
);
483 buffer_get_bignum2(&e
->request
, k
->rsa
->d
);
484 buffer_get_bignum2(&e
->request
, k
->rsa
->iqmp
);
485 buffer_get_bignum2(&e
->request
, k
->rsa
->p
);
486 buffer_get_bignum2(&e
->request
, k
->rsa
->q
);
488 /* Generate additional parameters */
489 rsa_generate_additional_parameters(k
->rsa
);
492 buffer_clear(&e
->request
);
497 /* enable blinding */
501 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
502 error("process_add_identity: RSA_blinding_on failed");
508 comment
= buffer_get_string(&e
->request
, NULL
);
514 while (buffer_len(&e
->request
)) {
515 switch (buffer_get_char(&e
->request
)) {
516 case SSH_AGENT_CONSTRAIN_LIFETIME
:
517 death
= time(NULL
) + buffer_get_int(&e
->request
);
519 case SSH_AGENT_CONSTRAIN_CONFIRM
:
526 if (lifetime
&& !death
)
527 death
= time(NULL
) + lifetime
;
528 if (lookup_identity(k
, version
) == NULL
) {
529 Identity
*id
= xmalloc(sizeof(Identity
));
531 id
->comment
= comment
;
533 id
->confirm
= confirm
;
534 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
535 /* Increment the number of identities. */
542 buffer_put_int(&e
->output
, 1);
543 buffer_put_char(&e
->output
,
544 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
547 /* XXX todo: encrypt sensitive data with passphrase */
549 process_lock_agent(SocketEntry
*e
, int lock
)
554 passwd
= buffer_get_string(&e
->request
, NULL
);
555 if (locked
&& !lock
&& strcmp(passwd
, lock_passwd
) == 0) {
557 memset(lock_passwd
, 0, strlen(lock_passwd
));
561 } else if (!locked
&& lock
) {
563 lock_passwd
= xstrdup(passwd
);
566 memset(passwd
, 0, strlen(passwd
));
569 buffer_put_int(&e
->output
, 1);
570 buffer_put_char(&e
->output
,
571 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
575 no_identities(SocketEntry
*e
, u_int type
)
580 buffer_put_char(&msg
,
581 (type
== SSH_AGENTC_REQUEST_RSA_IDENTITIES
) ?
582 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
583 buffer_put_int(&msg
, 0);
584 buffer_put_int(&e
->output
, buffer_len(&msg
));
585 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
591 process_add_smartcard_key (SocketEntry
*e
)
593 char *sc_reader_id
= NULL
, *pin
;
594 int i
, version
, success
= 0, death
= 0, confirm
= 0;
599 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
600 pin
= buffer_get_string(&e
->request
, NULL
);
602 while (buffer_len(&e
->request
)) {
603 switch (buffer_get_char(&e
->request
)) {
604 case SSH_AGENT_CONSTRAIN_LIFETIME
:
605 death
= time(NULL
) + buffer_get_int(&e
->request
);
607 case SSH_AGENT_CONSTRAIN_CONFIRM
:
614 if (lifetime
&& !death
)
615 death
= time(NULL
) + lifetime
;
617 keys
= sc_get_keys(sc_reader_id
, pin
);
621 if (keys
== NULL
|| keys
[0] == NULL
) {
622 error("sc_get_keys failed");
625 for (i
= 0; keys
[i
] != NULL
; i
++) {
627 version
= k
->type
== KEY_RSA1
? 1 : 2;
628 tab
= idtab_lookup(version
);
629 if (lookup_identity(k
, version
) == NULL
) {
630 id
= xmalloc(sizeof(Identity
));
632 id
->comment
= sc_get_key_label(k
);
634 id
->confirm
= confirm
;
635 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
645 buffer_put_int(&e
->output
, 1);
646 buffer_put_char(&e
->output
,
647 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
651 process_remove_smartcard_key(SocketEntry
*e
)
653 char *sc_reader_id
= NULL
, *pin
;
654 int i
, version
, success
= 0;
655 Key
**keys
, *k
= NULL
;
659 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
660 pin
= buffer_get_string(&e
->request
, NULL
);
661 keys
= sc_get_keys(sc_reader_id
, pin
);
665 if (keys
== NULL
|| keys
[0] == NULL
) {
666 error("sc_get_keys failed");
669 for (i
= 0; keys
[i
] != NULL
; i
++) {
671 version
= k
->type
== KEY_RSA1
? 1 : 2;
672 if ((id
= lookup_identity(k
, version
)) != NULL
) {
673 tab
= idtab_lookup(version
);
674 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
684 buffer_put_int(&e
->output
, 1);
685 buffer_put_char(&e
->output
,
686 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
688 #endif /* SMARTCARD */
690 /* dispatch incoming messages */
693 process_message(SocketEntry
*e
)
701 if (buffer_len(&e
->input
) < 5)
702 return; /* Incomplete message. */
703 cp
= buffer_ptr(&e
->input
);
704 msg_len
= get_u32(cp
);
705 if (msg_len
> 256 * 1024) {
709 if (buffer_len(&e
->input
) < msg_len
+ 4)
712 /* move the current input to e->request */
713 buffer_consume(&e
->input
, 4);
714 buffer_clear(&e
->request
);
715 buffer_append(&e
->request
, buffer_ptr(&e
->input
), msg_len
);
716 buffer_consume(&e
->input
, msg_len
);
717 type
= buffer_get_char(&e
->request
);
719 /* check wheter agent is locked */
720 if (locked
&& type
!= SSH_AGENTC_UNLOCK
) {
721 buffer_clear(&e
->request
);
723 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
724 case SSH2_AGENTC_REQUEST_IDENTITIES
:
725 /* send empty lists */
726 no_identities(e
, type
);
729 /* send a fail message for all other request types */
730 buffer_put_int(&e
->output
, 1);
731 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
736 debug("type %d", type
);
738 case SSH_AGENTC_LOCK
:
739 case SSH_AGENTC_UNLOCK
:
740 process_lock_agent(e
, type
== SSH_AGENTC_LOCK
);
743 case SSH_AGENTC_RSA_CHALLENGE
:
744 process_authentication_challenge1(e
);
746 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
747 process_request_identities(e
, 1);
749 case SSH_AGENTC_ADD_RSA_IDENTITY
:
750 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED
:
751 process_add_identity(e
, 1);
753 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
754 process_remove_identity(e
, 1);
756 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
757 process_remove_all_identities(e
, 1);
760 case SSH2_AGENTC_SIGN_REQUEST
:
761 process_sign_request2(e
);
763 case SSH2_AGENTC_REQUEST_IDENTITIES
:
764 process_request_identities(e
, 2);
766 case SSH2_AGENTC_ADD_IDENTITY
:
767 case SSH2_AGENTC_ADD_ID_CONSTRAINED
:
768 process_add_identity(e
, 2);
770 case SSH2_AGENTC_REMOVE_IDENTITY
:
771 process_remove_identity(e
, 2);
773 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
774 process_remove_all_identities(e
, 2);
777 case SSH_AGENTC_ADD_SMARTCARD_KEY
:
778 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED
:
779 process_add_smartcard_key(e
);
781 case SSH_AGENTC_REMOVE_SMARTCARD_KEY
:
782 process_remove_smartcard_key(e
);
784 #endif /* SMARTCARD */
786 /* Unknown message. Respond with failure. */
787 error("Unknown message %d", type
);
788 buffer_clear(&e
->request
);
789 buffer_put_int(&e
->output
, 1);
790 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
796 new_socket(sock_type type
, int fd
)
798 u_int i
, old_alloc
, new_alloc
;
805 for (i
= 0; i
< sockets_alloc
; i
++)
806 if (sockets
[i
].type
== AUTH_UNUSED
) {
808 buffer_init(&sockets
[i
].input
);
809 buffer_init(&sockets
[i
].output
);
810 buffer_init(&sockets
[i
].request
);
811 sockets
[i
].type
= type
;
814 old_alloc
= sockets_alloc
;
815 new_alloc
= sockets_alloc
+ 10;
816 sockets
= xrealloc(sockets
, new_alloc
, sizeof(sockets
[0]));
817 for (i
= old_alloc
; i
< new_alloc
; i
++)
818 sockets
[i
].type
= AUTH_UNUSED
;
819 sockets_alloc
= new_alloc
;
820 sockets
[old_alloc
].fd
= fd
;
821 buffer_init(&sockets
[old_alloc
].input
);
822 buffer_init(&sockets
[old_alloc
].output
);
823 buffer_init(&sockets
[old_alloc
].request
);
824 sockets
[old_alloc
].type
= type
;
828 prepare_select(fd_set
**fdrp
, fd_set
**fdwp
, int *fdl
, u_int
*nallocp
)
833 for (i
= 0; i
< sockets_alloc
; i
++) {
834 switch (sockets
[i
].type
) {
836 case AUTH_CONNECTION
:
837 n
= MAX(n
, sockets
[i
].fd
);
842 fatal("Unknown socket type %d", sockets
[i
].type
);
847 sz
= howmany(n
+1, NFDBITS
) * sizeof(fd_mask
);
848 if (*fdrp
== NULL
|| sz
> *nallocp
) {
858 debug("XXX shrink: %d < %d", n
, *fdl
);
860 memset(*fdrp
, 0, sz
);
861 memset(*fdwp
, 0, sz
);
863 for (i
= 0; i
< sockets_alloc
; i
++) {
864 switch (sockets
[i
].type
) {
866 case AUTH_CONNECTION
:
867 FD_SET(sockets
[i
].fd
, *fdrp
);
868 if (buffer_len(&sockets
[i
].output
) > 0)
869 FD_SET(sockets
[i
].fd
, *fdwp
);
879 after_select(fd_set
*readset
, fd_set
*writeset
)
881 struct sockaddr_un sunaddr
;
889 for (i
= 0; i
< sockets_alloc
; i
++)
890 switch (sockets
[i
].type
) {
894 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
895 slen
= sizeof(sunaddr
);
896 sock
= accept(sockets
[i
].fd
,
897 (struct sockaddr
*)&sunaddr
, &slen
);
899 error("accept from AUTH_SOCKET: %s",
903 if (getpeereid(sock
, &euid
, &egid
) < 0) {
904 error("getpeereid %d failed: %s",
905 sock
, strerror(errno
));
909 if ((euid
!= 0) && (getuid() != euid
)) {
910 error("uid mismatch: "
911 "peer euid %u != uid %u",
912 (u_int
) euid
, (u_int
) getuid());
916 new_socket(AUTH_CONNECTION
, sock
);
919 case AUTH_CONNECTION
:
920 if (buffer_len(&sockets
[i
].output
) > 0 &&
921 FD_ISSET(sockets
[i
].fd
, writeset
)) {
923 len
= write(sockets
[i
].fd
,
924 buffer_ptr(&sockets
[i
].output
),
925 buffer_len(&sockets
[i
].output
));
926 if (len
== -1 && (errno
== EAGAIN
||
932 close_socket(&sockets
[i
]);
935 buffer_consume(&sockets
[i
].output
, len
);
937 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
939 len
= read(sockets
[i
].fd
, buf
, sizeof(buf
));
940 if (len
== -1 && (errno
== EAGAIN
||
946 close_socket(&sockets
[i
]);
949 buffer_append(&sockets
[i
].input
, buf
, len
);
950 process_message(&sockets
[i
]);
954 fatal("Unknown type %d", sockets
[i
].type
);
976 cleanup_handler(int sig
)
984 check_parent_exists(int sig
)
986 int save_errno
= errno
;
988 if (parent_pid
!= -1 && kill(parent_pid
, 0) < 0) {
989 /* printf("Parent has died - Authentication agent exiting.\n"); */
990 cleanup_handler(sig
); /* safe */
992 mysignal(SIGALRM
, check_parent_exists
);
1000 fprintf(stderr
, "Usage: %s [options] [command [args ...]]\n",
1002 fprintf(stderr
, "Options:\n");
1003 fprintf(stderr
, " -c Generate C-shell commands on stdout.\n");
1004 fprintf(stderr
, " -s Generate Bourne shell commands on stdout.\n");
1005 fprintf(stderr
, " -k Kill the current agent.\n");
1006 fprintf(stderr
, " -d Debug mode.\n");
1007 fprintf(stderr
, " -a socket Bind agent socket to given name.\n");
1008 fprintf(stderr
, " -t life Default identity lifetime (seconds).\n");
1013 main(int ac
, char **av
)
1015 int c_flag
= 0, d_flag
= 0, k_flag
= 0, s_flag
= 0;
1018 char *shell
, *format
, *pidstr
, *agentsocket
= NULL
;
1019 fd_set
*readsetp
= NULL
, *writesetp
= NULL
;
1020 struct sockaddr_un sunaddr
;
1021 #ifdef HAVE_SETRLIMIT
1026 extern char *optarg
;
1028 char pidstrbuf
[1 + 3 * sizeof pid
];
1030 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1037 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1038 /* Disable ptrace on Linux without sgid bit */
1039 prctl(PR_SET_DUMPABLE
, 0);
1042 SSLeay_add_all_algorithms();
1044 __progname
= ssh_get_progname(av
[0]);
1048 while ((ch
= getopt(ac
, av
, "cdksa:t:")) != -1) {
1069 agentsocket
= optarg
;
1072 if ((lifetime
= convtime(optarg
)) == -1) {
1073 fprintf(stderr
, "Invalid lifetime\n");
1084 if (ac
> 0 && (c_flag
|| k_flag
|| s_flag
|| d_flag
))
1087 if (ac
== 0 && !c_flag
&& !s_flag
) {
1088 shell
= getenv("SHELL");
1089 if (shell
!= NULL
&&
1090 strncmp(shell
+ strlen(shell
) - 3, "csh", 3) == 0)
1094 const char *errstr
= NULL
;
1096 pidstr
= getenv(SSH_AGENTPID_ENV_NAME
);
1097 if (pidstr
== NULL
) {
1098 fprintf(stderr
, "%s not set, cannot kill agent\n",
1099 SSH_AGENTPID_ENV_NAME
);
1102 pid
= (int)strtonum(pidstr
, 2, INT_MAX
, &errstr
);
1105 "%s=\"%s\", which is not a good PID: %s\n",
1106 SSH_AGENTPID_ENV_NAME
, pidstr
, errstr
);
1109 if (kill(pid
, SIGTERM
) == -1) {
1113 format
= c_flag
? "unsetenv %s;\n" : "unset %s;\n";
1114 printf(format
, SSH_AUTHSOCKET_ENV_NAME
);
1115 printf(format
, SSH_AGENTPID_ENV_NAME
);
1116 printf("echo Agent pid %ld killed;\n", (long)pid
);
1119 parent_pid
= getpid();
1121 if (agentsocket
== NULL
) {
1122 /* Create private directory for agent socket */
1123 strlcpy(socket_dir
, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir
);
1124 if (mkdtemp(socket_dir
) == NULL
) {
1125 perror("mkdtemp: private socket dir");
1128 snprintf(socket_name
, sizeof socket_name
, "%s/agent.%ld", socket_dir
,
1131 /* Try to use specified agent socket */
1132 socket_dir
[0] = '\0';
1133 strlcpy(socket_name
, agentsocket
, sizeof socket_name
);
1137 * Create socket early so it will exist before command gets run from
1140 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1143 *socket_name
= '\0'; /* Don't unlink any existing file */
1146 memset(&sunaddr
, 0, sizeof(sunaddr
));
1147 sunaddr
.sun_family
= AF_UNIX
;
1148 strlcpy(sunaddr
.sun_path
, socket_name
, sizeof(sunaddr
.sun_path
));
1149 prev_mask
= umask(0177);
1150 if (bind(sock
, (struct sockaddr
*) &sunaddr
, sizeof(sunaddr
)) < 0) {
1152 *socket_name
= '\0'; /* Don't unlink any existing file */
1157 if (listen(sock
, SSH_LISTEN_BACKLOG
) < 0) {
1163 * Fork, and have the parent execute the command, if any, or present
1164 * the socket data. The child continues as the authentication agent.
1167 log_init(__progname
, SYSLOG_LEVEL_DEBUG1
, SYSLOG_FACILITY_AUTH
, 1);
1168 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1169 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1170 SSH_AUTHSOCKET_ENV_NAME
);
1171 printf("echo Agent pid %ld;\n", (long)parent_pid
);
1179 if (pid
!= 0) { /* Parent - execute the given command. */
1181 snprintf(pidstrbuf
, sizeof pidstrbuf
, "%ld", (long)pid
);
1183 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1184 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1185 SSH_AUTHSOCKET_ENV_NAME
);
1186 printf(format
, SSH_AGENTPID_ENV_NAME
, pidstrbuf
,
1187 SSH_AGENTPID_ENV_NAME
);
1188 printf("echo Agent pid %ld;\n", (long)pid
);
1191 if (setenv(SSH_AUTHSOCKET_ENV_NAME
, socket_name
, 1) == -1 ||
1192 setenv(SSH_AGENTPID_ENV_NAME
, pidstrbuf
, 1) == -1) {
1201 log_init(__progname
, SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_AUTH
, 0);
1203 if (setsid() == -1) {
1204 error("setsid: %s", strerror(errno
));
1209 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) != -1) {
1210 /* XXX might close listen socket */
1211 (void)dup2(fd
, STDIN_FILENO
);
1212 (void)dup2(fd
, STDOUT_FILENO
);
1213 (void)dup2(fd
, STDERR_FILENO
);
1218 #ifdef HAVE_SETRLIMIT
1219 /* deny core dumps, since memory contains unencrypted private keys */
1220 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
1221 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0) {
1222 error("setrlimit RLIMIT_CORE: %s", strerror(errno
));
1228 new_socket(AUTH_SOCKET
, sock
);
1230 mysignal(SIGALRM
, check_parent_exists
);
1235 signal(SIGINT
, SIG_IGN
);
1236 signal(SIGPIPE
, SIG_IGN
);
1237 signal(SIGHUP
, cleanup_handler
);
1238 signal(SIGTERM
, cleanup_handler
);
1242 prepare_select(&readsetp
, &writesetp
, &max_fd
, &nalloc
);
1243 if (select(max_fd
+ 1, readsetp
, writesetp
, NULL
, NULL
) < 0) {
1246 fatal("select: %s", strerror(errno
));
1248 after_select(readsetp
, writesetp
);