2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * The authentication agent program.
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
13 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "openbsd-compat/sys-queue.h"
38 RCSID("$OpenBSD: ssh-agent.c,v 1.117 2003/12/02 17:01:15 markus Exp $");
40 #include <openssl/evp.h>
41 #include <openssl/md5.h>
60 #if defined(HAVE_SYS_PRCTL_H)
61 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
78 u_int sockets_alloc
= 0;
79 SocketEntry
*sockets
= NULL
;
81 typedef struct identity
{
82 TAILQ_ENTRY(identity
) next
;
91 TAILQ_HEAD(idqueue
, identity
) idlist
;
94 /* private key table, one per protocol version */
99 /* pid of shell == parent of agent */
100 pid_t parent_pid
= -1;
102 /* pathname and directory for AUTH_SOCKET */
103 char socket_name
[1024];
104 char socket_dir
[1024];
108 char *lock_passwd
= NULL
;
110 #ifdef HAVE___PROGNAME
111 extern char *__progname
;
116 /* Default lifetime (0 == forever) */
117 static int lifetime
= 0;
120 close_socket(SocketEntry
*e
)
124 e
->type
= AUTH_UNUSED
;
125 buffer_free(&e
->input
);
126 buffer_free(&e
->output
);
127 buffer_free(&e
->request
);
135 for (i
= 0; i
<=2; i
++) {
136 TAILQ_INIT(&idtable
[i
].idlist
);
137 idtable
[i
].nentries
= 0;
141 /* return private key table for requested protocol version */
143 idtab_lookup(int version
)
145 if (version
< 1 || version
> 2)
146 fatal("internal error, bad protocol version %d", version
);
147 return &idtable
[version
];
151 free_identity(Identity
*id
)
158 /* return matching private key for given public key */
160 lookup_identity(Key
*key
, int version
)
164 Idtab
*tab
= idtab_lookup(version
);
165 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
166 if (key_equal(key
, id
->key
))
172 /* Check confirmation of keysign request */
174 confirm_key(Identity
*id
)
176 char *p
, prompt
[1024];
179 p
= key_fingerprint(id
->key
, SSH_FP_MD5
, SSH_FP_HEX
);
180 snprintf(prompt
, sizeof(prompt
), "Allow use of key %s?\n"
181 "Key fingerprint %s.", id
->comment
, p
);
183 p
= read_passphrase(prompt
, RP_ALLOW_EOF
);
186 * Accept empty responses and responses consisting
187 * of the word "yes" as affirmative.
189 if (*p
== '\0' || *p
== '\n' || strcasecmp(p
, "yes") == 0)
196 /* send list of supported public keys to 'client' */
198 process_request_identities(SocketEntry
*e
, int version
)
200 Idtab
*tab
= idtab_lookup(version
);
205 buffer_put_char(&msg
, (version
== 1) ?
206 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
207 buffer_put_int(&msg
, tab
->nentries
);
208 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
209 if (id
->key
->type
== KEY_RSA1
) {
210 buffer_put_int(&msg
, BN_num_bits(id
->key
->rsa
->n
));
211 buffer_put_bignum(&msg
, id
->key
->rsa
->e
);
212 buffer_put_bignum(&msg
, id
->key
->rsa
->n
);
216 key_to_blob(id
->key
, &blob
, &blen
);
217 buffer_put_string(&msg
, blob
, blen
);
220 buffer_put_cstring(&msg
, id
->comment
);
222 buffer_put_int(&e
->output
, buffer_len(&msg
));
223 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
229 process_authentication_challenge1(SocketEntry
*e
)
231 u_char buf
[32], mdbuf
[16], session_id
[16];
241 key
= key_new(KEY_RSA1
);
242 if ((challenge
= BN_new()) == NULL
)
243 fatal("process_authentication_challenge1: BN_new failed");
245 (void) buffer_get_int(&e
->request
); /* ignored */
246 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
247 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
248 buffer_get_bignum(&e
->request
, challenge
);
250 /* Only protocol 1.1 is supported */
251 if (buffer_len(&e
->request
) == 0)
253 buffer_get(&e
->request
, session_id
, 16);
254 response_type
= buffer_get_int(&e
->request
);
255 if (response_type
!= 1)
258 id
= lookup_identity(key
, 1);
259 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0)) {
260 Key
*private = id
->key
;
261 /* Decrypt the challenge using the private key. */
262 if (rsa_private_decrypt(challenge
, challenge
, private->rsa
) <= 0)
265 /* The response is MD5 of decrypted challenge plus session id. */
266 len
= BN_num_bytes(challenge
);
267 if (len
<= 0 || len
> 32) {
268 logit("process_authentication_challenge: bad challenge length %d", len
);
272 BN_bn2bin(challenge
, buf
+ 32 - len
);
274 MD5_Update(&md
, buf
, 32);
275 MD5_Update(&md
, session_id
, 16);
276 MD5_Final(mdbuf
, &md
);
278 /* Send the response. */
279 buffer_put_char(&msg
, SSH_AGENT_RSA_RESPONSE
);
280 for (i
= 0; i
< 16; i
++)
281 buffer_put_char(&msg
, mdbuf
[i
]);
286 /* Unknown identity or protocol error. Send failure. */
287 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
289 buffer_put_int(&e
->output
, buffer_len(&msg
));
290 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
292 BN_clear_free(challenge
);
298 process_sign_request2(SocketEntry
*e
)
300 u_char
*blob
, *data
, *signature
= NULL
;
301 u_int blen
, dlen
, slen
= 0;
302 extern int datafellows
;
309 blob
= buffer_get_string(&e
->request
, &blen
);
310 data
= buffer_get_string(&e
->request
, &dlen
);
312 flags
= buffer_get_int(&e
->request
);
313 if (flags
& SSH_AGENT_OLD_SIGNATURE
)
314 datafellows
= SSH_BUG_SIGBLOB
;
316 key
= key_from_blob(blob
, blen
);
318 Identity
*id
= lookup_identity(key
, 2);
319 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0))
320 ok
= key_sign(id
->key
, &signature
, &slen
, data
, dlen
);
325 buffer_put_char(&msg
, SSH2_AGENT_SIGN_RESPONSE
);
326 buffer_put_string(&msg
, signature
, slen
);
328 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
330 buffer_put_int(&e
->output
, buffer_len(&msg
));
331 buffer_append(&e
->output
, buffer_ptr(&msg
),
336 if (signature
!= NULL
)
342 process_remove_identity(SocketEntry
*e
, int version
)
351 key
= key_new(KEY_RSA1
);
352 bits
= buffer_get_int(&e
->request
);
353 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
354 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
356 if (bits
!= key_size(key
))
357 logit("Warning: identity keysize mismatch: actual %u, announced %u",
358 key_size(key
), bits
);
361 blob
= buffer_get_string(&e
->request
, &blen
);
362 key
= key_from_blob(blob
, blen
);
367 Identity
*id
= lookup_identity(key
, version
);
370 * We have this key. Free the old key. Since we
371 * don\'t want to leave empty slots in the middle of
372 * the array, we actually free the key there and move
373 * all the entries between the empty slot and the end
376 Idtab
*tab
= idtab_lookup(version
);
377 if (tab
->nentries
< 1)
378 fatal("process_remove_identity: "
379 "internal error: tab->nentries %d",
381 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
388 buffer_put_int(&e
->output
, 1);
389 buffer_put_char(&e
->output
,
390 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
394 process_remove_all_identities(SocketEntry
*e
, int version
)
396 Idtab
*tab
= idtab_lookup(version
);
399 /* Loop over all identities and clear the keys. */
400 for (id
= TAILQ_FIRST(&tab
->idlist
); id
;
401 id
= TAILQ_FIRST(&tab
->idlist
)) {
402 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
406 /* Mark that there are no identities. */
410 buffer_put_int(&e
->output
, 1);
411 buffer_put_char(&e
->output
, SSH_AGENT_SUCCESS
);
417 u_int now
= time(NULL
);
422 for (version
= 1; version
< 3; version
++) {
423 tab
= idtab_lookup(version
);
424 for (id
= TAILQ_FIRST(&tab
->idlist
); id
; id
= nxt
) {
425 nxt
= TAILQ_NEXT(id
, next
);
426 if (id
->death
!= 0 && now
>= id
->death
) {
427 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
436 process_add_identity(SocketEntry
*e
, int version
)
438 Idtab
*tab
= idtab_lookup(version
);
439 int type
, success
= 0, death
= 0, confirm
= 0;
440 char *type_name
, *comment
;
445 k
= key_new_private(KEY_RSA1
);
446 (void) buffer_get_int(&e
->request
); /* ignored */
447 buffer_get_bignum(&e
->request
, k
->rsa
->n
);
448 buffer_get_bignum(&e
->request
, k
->rsa
->e
);
449 buffer_get_bignum(&e
->request
, k
->rsa
->d
);
450 buffer_get_bignum(&e
->request
, k
->rsa
->iqmp
);
452 /* SSH and SSL have p and q swapped */
453 buffer_get_bignum(&e
->request
, k
->rsa
->q
); /* p */
454 buffer_get_bignum(&e
->request
, k
->rsa
->p
); /* q */
456 /* Generate additional parameters */
457 rsa_generate_additional_parameters(k
->rsa
);
460 type_name
= buffer_get_string(&e
->request
, NULL
);
461 type
= key_type_from_name(type_name
);
465 k
= key_new_private(type
);
466 buffer_get_bignum2(&e
->request
, k
->dsa
->p
);
467 buffer_get_bignum2(&e
->request
, k
->dsa
->q
);
468 buffer_get_bignum2(&e
->request
, k
->dsa
->g
);
469 buffer_get_bignum2(&e
->request
, k
->dsa
->pub_key
);
470 buffer_get_bignum2(&e
->request
, k
->dsa
->priv_key
);
473 k
= key_new_private(type
);
474 buffer_get_bignum2(&e
->request
, k
->rsa
->n
);
475 buffer_get_bignum2(&e
->request
, k
->rsa
->e
);
476 buffer_get_bignum2(&e
->request
, k
->rsa
->d
);
477 buffer_get_bignum2(&e
->request
, k
->rsa
->iqmp
);
478 buffer_get_bignum2(&e
->request
, k
->rsa
->p
);
479 buffer_get_bignum2(&e
->request
, k
->rsa
->q
);
481 /* Generate additional parameters */
482 rsa_generate_additional_parameters(k
->rsa
);
485 buffer_clear(&e
->request
);
490 /* enable blinding */
494 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
495 error("process_add_identity: RSA_blinding_on failed");
501 comment
= buffer_get_string(&e
->request
, NULL
);
507 while (buffer_len(&e
->request
)) {
508 switch (buffer_get_char(&e
->request
)) {
509 case SSH_AGENT_CONSTRAIN_LIFETIME
:
510 death
= time(NULL
) + buffer_get_int(&e
->request
);
512 case SSH_AGENT_CONSTRAIN_CONFIRM
:
519 if (lifetime
&& !death
)
520 death
= time(NULL
) + lifetime
;
521 if (lookup_identity(k
, version
) == NULL
) {
522 Identity
*id
= xmalloc(sizeof(Identity
));
524 id
->comment
= comment
;
526 id
->confirm
= confirm
;
527 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
528 /* Increment the number of identities. */
535 buffer_put_int(&e
->output
, 1);
536 buffer_put_char(&e
->output
,
537 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
540 /* XXX todo: encrypt sensitive data with passphrase */
542 process_lock_agent(SocketEntry
*e
, int lock
)
547 passwd
= buffer_get_string(&e
->request
, NULL
);
548 if (locked
&& !lock
&& strcmp(passwd
, lock_passwd
) == 0) {
550 memset(lock_passwd
, 0, strlen(lock_passwd
));
554 } else if (!locked
&& lock
) {
556 lock_passwd
= xstrdup(passwd
);
559 memset(passwd
, 0, strlen(passwd
));
562 buffer_put_int(&e
->output
, 1);
563 buffer_put_char(&e
->output
,
564 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
568 no_identities(SocketEntry
*e
, u_int type
)
573 buffer_put_char(&msg
,
574 (type
== SSH_AGENTC_REQUEST_RSA_IDENTITIES
) ?
575 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
576 buffer_put_int(&msg
, 0);
577 buffer_put_int(&e
->output
, buffer_len(&msg
));
578 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
584 process_add_smartcard_key (SocketEntry
*e
)
586 char *sc_reader_id
= NULL
, *pin
;
587 int i
, version
, success
= 0, death
= 0, confirm
= 0;
592 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
593 pin
= buffer_get_string(&e
->request
, NULL
);
595 while (buffer_len(&e
->request
)) {
596 switch (buffer_get_char(&e
->request
)) {
597 case SSH_AGENT_CONSTRAIN_LIFETIME
:
598 death
= time(NULL
) + buffer_get_int(&e
->request
);
600 case SSH_AGENT_CONSTRAIN_CONFIRM
:
607 if (lifetime
&& !death
)
608 death
= time(NULL
) + lifetime
;
610 keys
= sc_get_keys(sc_reader_id
, pin
);
614 if (keys
== NULL
|| keys
[0] == NULL
) {
615 error("sc_get_keys failed");
618 for (i
= 0; keys
[i
] != NULL
; i
++) {
620 version
= k
->type
== KEY_RSA1
? 1 : 2;
621 tab
= idtab_lookup(version
);
622 if (lookup_identity(k
, version
) == NULL
) {
623 id
= xmalloc(sizeof(Identity
));
625 id
->comment
= sc_get_key_label(k
);
627 id
->confirm
= confirm
;
628 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
638 buffer_put_int(&e
->output
, 1);
639 buffer_put_char(&e
->output
,
640 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
644 process_remove_smartcard_key(SocketEntry
*e
)
646 char *sc_reader_id
= NULL
, *pin
;
647 int i
, version
, success
= 0;
648 Key
**keys
, *k
= NULL
;
652 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
653 pin
= buffer_get_string(&e
->request
, NULL
);
654 keys
= sc_get_keys(sc_reader_id
, pin
);
658 if (keys
== NULL
|| keys
[0] == NULL
) {
659 error("sc_get_keys failed");
662 for (i
= 0; keys
[i
] != NULL
; i
++) {
664 version
= k
->type
== KEY_RSA1
? 1 : 2;
665 if ((id
= lookup_identity(k
, version
)) != NULL
) {
666 tab
= idtab_lookup(version
);
667 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
677 buffer_put_int(&e
->output
, 1);
678 buffer_put_char(&e
->output
,
679 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
681 #endif /* SMARTCARD */
683 /* dispatch incoming messages */
686 process_message(SocketEntry
*e
)
694 if (buffer_len(&e
->input
) < 5)
695 return; /* Incomplete message. */
696 cp
= buffer_ptr(&e
->input
);
697 msg_len
= GET_32BIT(cp
);
698 if (msg_len
> 256 * 1024) {
702 if (buffer_len(&e
->input
) < msg_len
+ 4)
705 /* move the current input to e->request */
706 buffer_consume(&e
->input
, 4);
707 buffer_clear(&e
->request
);
708 buffer_append(&e
->request
, buffer_ptr(&e
->input
), msg_len
);
709 buffer_consume(&e
->input
, msg_len
);
710 type
= buffer_get_char(&e
->request
);
712 /* check wheter agent is locked */
713 if (locked
&& type
!= SSH_AGENTC_UNLOCK
) {
714 buffer_clear(&e
->request
);
716 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
717 case SSH2_AGENTC_REQUEST_IDENTITIES
:
718 /* send empty lists */
719 no_identities(e
, type
);
722 /* send a fail message for all other request types */
723 buffer_put_int(&e
->output
, 1);
724 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
729 debug("type %d", type
);
731 case SSH_AGENTC_LOCK
:
732 case SSH_AGENTC_UNLOCK
:
733 process_lock_agent(e
, type
== SSH_AGENTC_LOCK
);
736 case SSH_AGENTC_RSA_CHALLENGE
:
737 process_authentication_challenge1(e
);
739 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
740 process_request_identities(e
, 1);
742 case SSH_AGENTC_ADD_RSA_IDENTITY
:
743 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED
:
744 process_add_identity(e
, 1);
746 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
747 process_remove_identity(e
, 1);
749 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
750 process_remove_all_identities(e
, 1);
753 case SSH2_AGENTC_SIGN_REQUEST
:
754 process_sign_request2(e
);
756 case SSH2_AGENTC_REQUEST_IDENTITIES
:
757 process_request_identities(e
, 2);
759 case SSH2_AGENTC_ADD_IDENTITY
:
760 case SSH2_AGENTC_ADD_ID_CONSTRAINED
:
761 process_add_identity(e
, 2);
763 case SSH2_AGENTC_REMOVE_IDENTITY
:
764 process_remove_identity(e
, 2);
766 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
767 process_remove_all_identities(e
, 2);
770 case SSH_AGENTC_ADD_SMARTCARD_KEY
:
771 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED
:
772 process_add_smartcard_key(e
);
774 case SSH_AGENTC_REMOVE_SMARTCARD_KEY
:
775 process_remove_smartcard_key(e
);
777 #endif /* SMARTCARD */
779 /* Unknown message. Respond with failure. */
780 error("Unknown message %d", type
);
781 buffer_clear(&e
->request
);
782 buffer_put_int(&e
->output
, 1);
783 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
789 new_socket(sock_type type
, int fd
)
791 u_int i
, old_alloc
, new_alloc
;
793 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
) < 0)
794 error("fcntl O_NONBLOCK: %s", strerror(errno
));
799 for (i
= 0; i
< sockets_alloc
; i
++)
800 if (sockets
[i
].type
== AUTH_UNUSED
) {
802 buffer_init(&sockets
[i
].input
);
803 buffer_init(&sockets
[i
].output
);
804 buffer_init(&sockets
[i
].request
);
805 sockets
[i
].type
= type
;
808 old_alloc
= sockets_alloc
;
809 new_alloc
= sockets_alloc
+ 10;
811 sockets
= xrealloc(sockets
, new_alloc
* sizeof(sockets
[0]));
813 sockets
= xmalloc(new_alloc
* sizeof(sockets
[0]));
814 for (i
= old_alloc
; i
< new_alloc
; i
++)
815 sockets
[i
].type
= AUTH_UNUSED
;
816 sockets_alloc
= new_alloc
;
817 sockets
[old_alloc
].fd
= fd
;
818 buffer_init(&sockets
[old_alloc
].input
);
819 buffer_init(&sockets
[old_alloc
].output
);
820 buffer_init(&sockets
[old_alloc
].request
);
821 sockets
[old_alloc
].type
= type
;
825 prepare_select(fd_set
**fdrp
, fd_set
**fdwp
, int *fdl
, int *nallocp
)
830 for (i
= 0; i
< sockets_alloc
; i
++) {
831 switch (sockets
[i
].type
) {
833 case AUTH_CONNECTION
:
834 n
= MAX(n
, sockets
[i
].fd
);
839 fatal("Unknown socket type %d", sockets
[i
].type
);
844 sz
= howmany(n
+1, NFDBITS
) * sizeof(fd_mask
);
845 if (*fdrp
== NULL
|| sz
> *nallocp
) {
855 debug("XXX shrink: %d < %d", n
, *fdl
);
857 memset(*fdrp
, 0, sz
);
858 memset(*fdwp
, 0, sz
);
860 for (i
= 0; i
< sockets_alloc
; i
++) {
861 switch (sockets
[i
].type
) {
863 case AUTH_CONNECTION
:
864 FD_SET(sockets
[i
].fd
, *fdrp
);
865 if (buffer_len(&sockets
[i
].output
) > 0)
866 FD_SET(sockets
[i
].fd
, *fdwp
);
876 after_select(fd_set
*readset
, fd_set
*writeset
)
878 struct sockaddr_un sunaddr
;
886 for (i
= 0; i
< sockets_alloc
; i
++)
887 switch (sockets
[i
].type
) {
891 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
892 slen
= sizeof(sunaddr
);
893 sock
= accept(sockets
[i
].fd
,
894 (struct sockaddr
*) &sunaddr
, &slen
);
896 error("accept from AUTH_SOCKET: %s",
900 if (getpeereid(sock
, &euid
, &egid
) < 0) {
901 error("getpeereid %d failed: %s",
902 sock
, strerror(errno
));
906 if ((euid
!= 0) && (getuid() != euid
)) {
907 error("uid mismatch: "
908 "peer euid %u != uid %u",
909 (u_int
) euid
, (u_int
) getuid());
913 new_socket(AUTH_CONNECTION
, sock
);
916 case AUTH_CONNECTION
:
917 if (buffer_len(&sockets
[i
].output
) > 0 &&
918 FD_ISSET(sockets
[i
].fd
, writeset
)) {
920 len
= write(sockets
[i
].fd
,
921 buffer_ptr(&sockets
[i
].output
),
922 buffer_len(&sockets
[i
].output
));
923 if (len
== -1 && (errno
== EAGAIN
||
929 close_socket(&sockets
[i
]);
932 buffer_consume(&sockets
[i
].output
, len
);
934 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
936 len
= read(sockets
[i
].fd
, buf
, sizeof(buf
));
937 if (len
== -1 && (errno
== EAGAIN
||
943 close_socket(&sockets
[i
]);
946 buffer_append(&sockets
[i
].input
, buf
, len
);
947 process_message(&sockets
[i
]);
951 fatal("Unknown type %d", sockets
[i
].type
);
972 cleanup_handler(int sig
)
979 check_parent_exists(int sig
)
981 int save_errno
= errno
;
983 if (parent_pid
!= -1 && kill(parent_pid
, 0) < 0) {
984 /* printf("Parent has died - Authentication agent exiting.\n"); */
985 cleanup_handler(sig
); /* safe */
987 mysignal(SIGALRM
, check_parent_exists
);
995 fprintf(stderr
, "Usage: %s [options] [command [args ...]]\n",
997 fprintf(stderr
, "Options:\n");
998 fprintf(stderr
, " -c Generate C-shell commands on stdout.\n");
999 fprintf(stderr
, " -s Generate Bourne shell commands on stdout.\n");
1000 fprintf(stderr
, " -k Kill the current agent.\n");
1001 fprintf(stderr
, " -d Debug mode.\n");
1002 fprintf(stderr
, " -a socket Bind agent socket to given name.\n");
1003 fprintf(stderr
, " -t life Default identity lifetime (seconds).\n");
1008 main(int ac
, char **av
)
1010 int c_flag
= 0, d_flag
= 0, k_flag
= 0, s_flag
= 0;
1011 int sock
, fd
, ch
, nalloc
;
1012 char *shell
, *format
, *pidstr
, *agentsocket
= NULL
;
1013 fd_set
*readsetp
= NULL
, *writesetp
= NULL
;
1014 struct sockaddr_un sunaddr
;
1015 #ifdef HAVE_SETRLIMIT
1022 extern char *optarg
;
1024 char pidstrbuf
[1 + 3 * sizeof pid
];
1030 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1031 /* Disable ptrace on Linux without sgid bit */
1032 prctl(PR_SET_DUMPABLE
, 0);
1035 SSLeay_add_all_algorithms();
1037 __progname
= ssh_get_progname(av
[0]);
1041 while ((ch
= getopt(ac
, av
, "cdksa:t:")) != -1) {
1062 agentsocket
= optarg
;
1065 if ((lifetime
= convtime(optarg
)) == -1) {
1066 fprintf(stderr
, "Invalid lifetime\n");
1077 if (ac
> 0 && (c_flag
|| k_flag
|| s_flag
|| d_flag
))
1080 if (ac
== 0 && !c_flag
&& !s_flag
) {
1081 shell
= getenv("SHELL");
1082 if (shell
!= NULL
&& strncmp(shell
+ strlen(shell
) - 3, "csh", 3) == 0)
1086 pidstr
= getenv(SSH_AGENTPID_ENV_NAME
);
1087 if (pidstr
== NULL
) {
1088 fprintf(stderr
, "%s not set, cannot kill agent\n",
1089 SSH_AGENTPID_ENV_NAME
);
1094 fprintf(stderr
, "%s=\"%s\", which is not a good PID\n",
1095 SSH_AGENTPID_ENV_NAME
, pidstr
);
1098 if (kill(pid
, SIGTERM
) == -1) {
1102 format
= c_flag
? "unsetenv %s;\n" : "unset %s;\n";
1103 printf(format
, SSH_AUTHSOCKET_ENV_NAME
);
1104 printf(format
, SSH_AGENTPID_ENV_NAME
);
1105 printf("echo Agent pid %ld killed;\n", (long)pid
);
1108 parent_pid
= getpid();
1110 if (agentsocket
== NULL
) {
1111 /* Create private directory for agent socket */
1112 strlcpy(socket_dir
, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir
);
1113 if (mkdtemp(socket_dir
) == NULL
) {
1114 perror("mkdtemp: private socket dir");
1117 snprintf(socket_name
, sizeof socket_name
, "%s/agent.%ld", socket_dir
,
1120 /* Try to use specified agent socket */
1121 socket_dir
[0] = '\0';
1122 strlcpy(socket_name
, agentsocket
, sizeof socket_name
);
1126 * Create socket early so it will exist before command gets run from
1129 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1134 memset(&sunaddr
, 0, sizeof(sunaddr
));
1135 sunaddr
.sun_family
= AF_UNIX
;
1136 strlcpy(sunaddr
.sun_path
, socket_name
, sizeof(sunaddr
.sun_path
));
1138 prev_mask
= umask(0177);
1140 if (bind(sock
, (struct sockaddr
*) & sunaddr
, sizeof(sunaddr
)) < 0) {
1150 if (listen(sock
, SSH_LISTEN_BACKLOG
) < 0) {
1156 * Fork, and have the parent execute the command, if any, or present
1157 * the socket data. The child continues as the authentication agent.
1160 log_init(__progname
, SYSLOG_LEVEL_DEBUG1
, SYSLOG_FACILITY_AUTH
, 1);
1161 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1162 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1163 SSH_AUTHSOCKET_ENV_NAME
);
1164 printf("echo Agent pid %ld;\n", (long)parent_pid
);
1172 if (pid
!= 0) { /* Parent - execute the given command. */
1174 snprintf(pidstrbuf
, sizeof pidstrbuf
, "%ld", (long)pid
);
1176 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1177 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1178 SSH_AUTHSOCKET_ENV_NAME
);
1179 printf(format
, SSH_AGENTPID_ENV_NAME
, pidstrbuf
,
1180 SSH_AGENTPID_ENV_NAME
);
1181 printf("echo Agent pid %ld;\n", (long)pid
);
1184 if (setenv(SSH_AUTHSOCKET_ENV_NAME
, socket_name
, 1) == -1 ||
1185 setenv(SSH_AGENTPID_ENV_NAME
, pidstrbuf
, 1) == -1) {
1194 log_init(__progname
, SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_AUTH
, 0);
1196 if (setsid() == -1) {
1197 error("setsid: %s", strerror(errno
));
1202 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) != -1) {
1203 /* XXX might close listen socket */
1204 (void)dup2(fd
, STDIN_FILENO
);
1205 (void)dup2(fd
, STDOUT_FILENO
);
1206 (void)dup2(fd
, STDERR_FILENO
);
1211 #ifdef HAVE_SETRLIMIT
1212 /* deny core dumps, since memory contains unencrypted private keys */
1213 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
1214 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0) {
1215 error("setrlimit RLIMIT_CORE: %s", strerror(errno
));
1221 new_socket(AUTH_SOCKET
, sock
);
1223 mysignal(SIGALRM
, check_parent_exists
);
1228 signal(SIGINT
, SIG_IGN
);
1229 signal(SIGPIPE
, SIG_IGN
);
1230 signal(SIGHUP
, cleanup_handler
);
1231 signal(SIGTERM
, cleanup_handler
);
1235 prepare_select(&readsetp
, &writesetp
, &max_fd
, &nalloc
);
1236 if (select(max_fd
+ 1, readsetp
, writesetp
, NULL
, NULL
) < 0) {
1239 fatal("select: %s", strerror(errno
));
1241 after_select(readsetp
, writesetp
);