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.122 2004/10/29 22:53:56 djm Exp $");
40 #include <openssl/evp.h>
41 #include <openssl/md5.h>
59 #if defined(HAVE_SYS_PRCTL_H)
60 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
77 u_int sockets_alloc
= 0;
78 SocketEntry
*sockets
= NULL
;
80 typedef struct identity
{
81 TAILQ_ENTRY(identity
) next
;
90 TAILQ_HEAD(idqueue
, identity
) idlist
;
93 /* private key table, one per protocol version */
98 /* pid of shell == parent of agent */
99 pid_t parent_pid
= -1;
101 /* pathname and directory for AUTH_SOCKET */
102 char socket_name
[1024];
103 char socket_dir
[1024];
107 char *lock_passwd
= NULL
;
109 extern char *__progname
;
111 /* Default lifetime (0 == forever) */
112 static int lifetime
= 0;
115 close_socket(SocketEntry
*e
)
119 e
->type
= AUTH_UNUSED
;
120 buffer_free(&e
->input
);
121 buffer_free(&e
->output
);
122 buffer_free(&e
->request
);
130 for (i
= 0; i
<=2; i
++) {
131 TAILQ_INIT(&idtable
[i
].idlist
);
132 idtable
[i
].nentries
= 0;
136 /* return private key table for requested protocol version */
138 idtab_lookup(int version
)
140 if (version
< 1 || version
> 2)
141 fatal("internal error, bad protocol version %d", version
);
142 return &idtable
[version
];
146 free_identity(Identity
*id
)
153 /* return matching private key for given public key */
155 lookup_identity(Key
*key
, int version
)
159 Idtab
*tab
= idtab_lookup(version
);
160 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
161 if (key_equal(key
, id
->key
))
167 /* Check confirmation of keysign request */
169 confirm_key(Identity
*id
)
174 p
= key_fingerprint(id
->key
, SSH_FP_MD5
, SSH_FP_HEX
);
175 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
183 /* send list of supported public keys to 'client' */
185 process_request_identities(SocketEntry
*e
, int version
)
187 Idtab
*tab
= idtab_lookup(version
);
192 buffer_put_char(&msg
, (version
== 1) ?
193 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
194 buffer_put_int(&msg
, tab
->nentries
);
195 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
196 if (id
->key
->type
== KEY_RSA1
) {
197 buffer_put_int(&msg
, BN_num_bits(id
->key
->rsa
->n
));
198 buffer_put_bignum(&msg
, id
->key
->rsa
->e
);
199 buffer_put_bignum(&msg
, id
->key
->rsa
->n
);
203 key_to_blob(id
->key
, &blob
, &blen
);
204 buffer_put_string(&msg
, blob
, blen
);
207 buffer_put_cstring(&msg
, id
->comment
);
209 buffer_put_int(&e
->output
, buffer_len(&msg
));
210 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
216 process_authentication_challenge1(SocketEntry
*e
)
218 u_char buf
[32], mdbuf
[16], session_id
[16];
228 key
= key_new(KEY_RSA1
);
229 if ((challenge
= BN_new()) == NULL
)
230 fatal("process_authentication_challenge1: BN_new failed");
232 (void) buffer_get_int(&e
->request
); /* ignored */
233 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
234 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
235 buffer_get_bignum(&e
->request
, challenge
);
237 /* Only protocol 1.1 is supported */
238 if (buffer_len(&e
->request
) == 0)
240 buffer_get(&e
->request
, session_id
, 16);
241 response_type
= buffer_get_int(&e
->request
);
242 if (response_type
!= 1)
245 id
= lookup_identity(key
, 1);
246 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0)) {
247 Key
*private = id
->key
;
248 /* Decrypt the challenge using the private key. */
249 if (rsa_private_decrypt(challenge
, challenge
, private->rsa
) <= 0)
252 /* The response is MD5 of decrypted challenge plus session id. */
253 len
= BN_num_bytes(challenge
);
254 if (len
<= 0 || len
> 32) {
255 logit("process_authentication_challenge: bad challenge length %d", len
);
259 BN_bn2bin(challenge
, buf
+ 32 - len
);
261 MD5_Update(&md
, buf
, 32);
262 MD5_Update(&md
, session_id
, 16);
263 MD5_Final(mdbuf
, &md
);
265 /* Send the response. */
266 buffer_put_char(&msg
, SSH_AGENT_RSA_RESPONSE
);
267 for (i
= 0; i
< 16; i
++)
268 buffer_put_char(&msg
, mdbuf
[i
]);
273 /* Unknown identity or protocol error. Send failure. */
274 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
276 buffer_put_int(&e
->output
, buffer_len(&msg
));
277 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
279 BN_clear_free(challenge
);
285 process_sign_request2(SocketEntry
*e
)
287 u_char
*blob
, *data
, *signature
= NULL
;
288 u_int blen
, dlen
, slen
= 0;
289 extern int datafellows
;
296 blob
= buffer_get_string(&e
->request
, &blen
);
297 data
= buffer_get_string(&e
->request
, &dlen
);
299 flags
= buffer_get_int(&e
->request
);
300 if (flags
& SSH_AGENT_OLD_SIGNATURE
)
301 datafellows
= SSH_BUG_SIGBLOB
;
303 key
= key_from_blob(blob
, blen
);
305 Identity
*id
= lookup_identity(key
, 2);
306 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0))
307 ok
= key_sign(id
->key
, &signature
, &slen
, data
, dlen
);
312 buffer_put_char(&msg
, SSH2_AGENT_SIGN_RESPONSE
);
313 buffer_put_string(&msg
, signature
, slen
);
315 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
317 buffer_put_int(&e
->output
, buffer_len(&msg
));
318 buffer_append(&e
->output
, buffer_ptr(&msg
),
323 if (signature
!= NULL
)
329 process_remove_identity(SocketEntry
*e
, int version
)
338 key
= key_new(KEY_RSA1
);
339 bits
= buffer_get_int(&e
->request
);
340 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
341 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
343 if (bits
!= key_size(key
))
344 logit("Warning: identity keysize mismatch: actual %u, announced %u",
345 key_size(key
), bits
);
348 blob
= buffer_get_string(&e
->request
, &blen
);
349 key
= key_from_blob(blob
, blen
);
354 Identity
*id
= lookup_identity(key
, version
);
357 * We have this key. Free the old key. Since we
358 * don\'t want to leave empty slots in the middle of
359 * the array, we actually free the key there and move
360 * all the entries between the empty slot and the end
363 Idtab
*tab
= idtab_lookup(version
);
364 if (tab
->nentries
< 1)
365 fatal("process_remove_identity: "
366 "internal error: tab->nentries %d",
368 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
375 buffer_put_int(&e
->output
, 1);
376 buffer_put_char(&e
->output
,
377 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
381 process_remove_all_identities(SocketEntry
*e
, int version
)
383 Idtab
*tab
= idtab_lookup(version
);
386 /* Loop over all identities and clear the keys. */
387 for (id
= TAILQ_FIRST(&tab
->idlist
); id
;
388 id
= TAILQ_FIRST(&tab
->idlist
)) {
389 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
393 /* Mark that there are no identities. */
397 buffer_put_int(&e
->output
, 1);
398 buffer_put_char(&e
->output
, SSH_AGENT_SUCCESS
);
404 u_int now
= time(NULL
);
409 for (version
= 1; version
< 3; version
++) {
410 tab
= idtab_lookup(version
);
411 for (id
= TAILQ_FIRST(&tab
->idlist
); id
; id
= nxt
) {
412 nxt
= TAILQ_NEXT(id
, next
);
413 if (id
->death
!= 0 && now
>= id
->death
) {
414 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
423 process_add_identity(SocketEntry
*e
, int version
)
425 Idtab
*tab
= idtab_lookup(version
);
426 int type
, success
= 0, death
= 0, confirm
= 0;
427 char *type_name
, *comment
;
432 k
= key_new_private(KEY_RSA1
);
433 (void) buffer_get_int(&e
->request
); /* ignored */
434 buffer_get_bignum(&e
->request
, k
->rsa
->n
);
435 buffer_get_bignum(&e
->request
, k
->rsa
->e
);
436 buffer_get_bignum(&e
->request
, k
->rsa
->d
);
437 buffer_get_bignum(&e
->request
, k
->rsa
->iqmp
);
439 /* SSH and SSL have p and q swapped */
440 buffer_get_bignum(&e
->request
, k
->rsa
->q
); /* p */
441 buffer_get_bignum(&e
->request
, k
->rsa
->p
); /* q */
443 /* Generate additional parameters */
444 rsa_generate_additional_parameters(k
->rsa
);
447 type_name
= buffer_get_string(&e
->request
, NULL
);
448 type
= key_type_from_name(type_name
);
452 k
= key_new_private(type
);
453 buffer_get_bignum2(&e
->request
, k
->dsa
->p
);
454 buffer_get_bignum2(&e
->request
, k
->dsa
->q
);
455 buffer_get_bignum2(&e
->request
, k
->dsa
->g
);
456 buffer_get_bignum2(&e
->request
, k
->dsa
->pub_key
);
457 buffer_get_bignum2(&e
->request
, k
->dsa
->priv_key
);
460 k
= key_new_private(type
);
461 buffer_get_bignum2(&e
->request
, k
->rsa
->n
);
462 buffer_get_bignum2(&e
->request
, k
->rsa
->e
);
463 buffer_get_bignum2(&e
->request
, k
->rsa
->d
);
464 buffer_get_bignum2(&e
->request
, k
->rsa
->iqmp
);
465 buffer_get_bignum2(&e
->request
, k
->rsa
->p
);
466 buffer_get_bignum2(&e
->request
, k
->rsa
->q
);
468 /* Generate additional parameters */
469 rsa_generate_additional_parameters(k
->rsa
);
472 buffer_clear(&e
->request
);
477 /* enable blinding */
481 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
482 error("process_add_identity: RSA_blinding_on failed");
488 comment
= buffer_get_string(&e
->request
, NULL
);
494 while (buffer_len(&e
->request
)) {
495 switch (buffer_get_char(&e
->request
)) {
496 case SSH_AGENT_CONSTRAIN_LIFETIME
:
497 death
= time(NULL
) + buffer_get_int(&e
->request
);
499 case SSH_AGENT_CONSTRAIN_CONFIRM
:
506 if (lifetime
&& !death
)
507 death
= time(NULL
) + lifetime
;
508 if (lookup_identity(k
, version
) == NULL
) {
509 Identity
*id
= xmalloc(sizeof(Identity
));
511 id
->comment
= comment
;
513 id
->confirm
= confirm
;
514 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
515 /* Increment the number of identities. */
522 buffer_put_int(&e
->output
, 1);
523 buffer_put_char(&e
->output
,
524 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
527 /* XXX todo: encrypt sensitive data with passphrase */
529 process_lock_agent(SocketEntry
*e
, int lock
)
534 passwd
= buffer_get_string(&e
->request
, NULL
);
535 if (locked
&& !lock
&& strcmp(passwd
, lock_passwd
) == 0) {
537 memset(lock_passwd
, 0, strlen(lock_passwd
));
541 } else if (!locked
&& lock
) {
543 lock_passwd
= xstrdup(passwd
);
546 memset(passwd
, 0, strlen(passwd
));
549 buffer_put_int(&e
->output
, 1);
550 buffer_put_char(&e
->output
,
551 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
555 no_identities(SocketEntry
*e
, u_int type
)
560 buffer_put_char(&msg
,
561 (type
== SSH_AGENTC_REQUEST_RSA_IDENTITIES
) ?
562 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
563 buffer_put_int(&msg
, 0);
564 buffer_put_int(&e
->output
, buffer_len(&msg
));
565 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
571 process_add_smartcard_key (SocketEntry
*e
)
573 char *sc_reader_id
= NULL
, *pin
;
574 int i
, version
, success
= 0, death
= 0, confirm
= 0;
579 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
580 pin
= buffer_get_string(&e
->request
, NULL
);
582 while (buffer_len(&e
->request
)) {
583 switch (buffer_get_char(&e
->request
)) {
584 case SSH_AGENT_CONSTRAIN_LIFETIME
:
585 death
= time(NULL
) + buffer_get_int(&e
->request
);
587 case SSH_AGENT_CONSTRAIN_CONFIRM
:
594 if (lifetime
&& !death
)
595 death
= time(NULL
) + lifetime
;
597 keys
= sc_get_keys(sc_reader_id
, pin
);
601 if (keys
== NULL
|| keys
[0] == NULL
) {
602 error("sc_get_keys failed");
605 for (i
= 0; keys
[i
] != NULL
; i
++) {
607 version
= k
->type
== KEY_RSA1
? 1 : 2;
608 tab
= idtab_lookup(version
);
609 if (lookup_identity(k
, version
) == NULL
) {
610 id
= xmalloc(sizeof(Identity
));
612 id
->comment
= sc_get_key_label(k
);
614 id
->confirm
= confirm
;
615 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
625 buffer_put_int(&e
->output
, 1);
626 buffer_put_char(&e
->output
,
627 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
631 process_remove_smartcard_key(SocketEntry
*e
)
633 char *sc_reader_id
= NULL
, *pin
;
634 int i
, version
, success
= 0;
635 Key
**keys
, *k
= NULL
;
639 sc_reader_id
= buffer_get_string(&e
->request
, NULL
);
640 pin
= buffer_get_string(&e
->request
, NULL
);
641 keys
= sc_get_keys(sc_reader_id
, pin
);
645 if (keys
== NULL
|| keys
[0] == NULL
) {
646 error("sc_get_keys failed");
649 for (i
= 0; keys
[i
] != NULL
; i
++) {
651 version
= k
->type
== KEY_RSA1
? 1 : 2;
652 if ((id
= lookup_identity(k
, version
)) != NULL
) {
653 tab
= idtab_lookup(version
);
654 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
664 buffer_put_int(&e
->output
, 1);
665 buffer_put_char(&e
->output
,
666 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
668 #endif /* SMARTCARD */
670 /* dispatch incoming messages */
673 process_message(SocketEntry
*e
)
681 if (buffer_len(&e
->input
) < 5)
682 return; /* Incomplete message. */
683 cp
= buffer_ptr(&e
->input
);
684 msg_len
= GET_32BIT(cp
);
685 if (msg_len
> 256 * 1024) {
689 if (buffer_len(&e
->input
) < msg_len
+ 4)
692 /* move the current input to e->request */
693 buffer_consume(&e
->input
, 4);
694 buffer_clear(&e
->request
);
695 buffer_append(&e
->request
, buffer_ptr(&e
->input
), msg_len
);
696 buffer_consume(&e
->input
, msg_len
);
697 type
= buffer_get_char(&e
->request
);
699 /* check wheter agent is locked */
700 if (locked
&& type
!= SSH_AGENTC_UNLOCK
) {
701 buffer_clear(&e
->request
);
703 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
704 case SSH2_AGENTC_REQUEST_IDENTITIES
:
705 /* send empty lists */
706 no_identities(e
, type
);
709 /* send a fail message for all other request types */
710 buffer_put_int(&e
->output
, 1);
711 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
716 debug("type %d", type
);
718 case SSH_AGENTC_LOCK
:
719 case SSH_AGENTC_UNLOCK
:
720 process_lock_agent(e
, type
== SSH_AGENTC_LOCK
);
723 case SSH_AGENTC_RSA_CHALLENGE
:
724 process_authentication_challenge1(e
);
726 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
727 process_request_identities(e
, 1);
729 case SSH_AGENTC_ADD_RSA_IDENTITY
:
730 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED
:
731 process_add_identity(e
, 1);
733 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
734 process_remove_identity(e
, 1);
736 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
737 process_remove_all_identities(e
, 1);
740 case SSH2_AGENTC_SIGN_REQUEST
:
741 process_sign_request2(e
);
743 case SSH2_AGENTC_REQUEST_IDENTITIES
:
744 process_request_identities(e
, 2);
746 case SSH2_AGENTC_ADD_IDENTITY
:
747 case SSH2_AGENTC_ADD_ID_CONSTRAINED
:
748 process_add_identity(e
, 2);
750 case SSH2_AGENTC_REMOVE_IDENTITY
:
751 process_remove_identity(e
, 2);
753 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
754 process_remove_all_identities(e
, 2);
757 case SSH_AGENTC_ADD_SMARTCARD_KEY
:
758 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED
:
759 process_add_smartcard_key(e
);
761 case SSH_AGENTC_REMOVE_SMARTCARD_KEY
:
762 process_remove_smartcard_key(e
);
764 #endif /* SMARTCARD */
766 /* Unknown message. Respond with failure. */
767 error("Unknown message %d", type
);
768 buffer_clear(&e
->request
);
769 buffer_put_int(&e
->output
, 1);
770 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
776 new_socket(sock_type type
, int fd
)
778 u_int i
, old_alloc
, new_alloc
;
785 for (i
= 0; i
< sockets_alloc
; i
++)
786 if (sockets
[i
].type
== AUTH_UNUSED
) {
788 buffer_init(&sockets
[i
].input
);
789 buffer_init(&sockets
[i
].output
);
790 buffer_init(&sockets
[i
].request
);
791 sockets
[i
].type
= type
;
794 old_alloc
= sockets_alloc
;
795 new_alloc
= sockets_alloc
+ 10;
797 sockets
= xrealloc(sockets
, new_alloc
* sizeof(sockets
[0]));
799 sockets
= xmalloc(new_alloc
* sizeof(sockets
[0]));
800 for (i
= old_alloc
; i
< new_alloc
; i
++)
801 sockets
[i
].type
= AUTH_UNUSED
;
802 sockets_alloc
= new_alloc
;
803 sockets
[old_alloc
].fd
= fd
;
804 buffer_init(&sockets
[old_alloc
].input
);
805 buffer_init(&sockets
[old_alloc
].output
);
806 buffer_init(&sockets
[old_alloc
].request
);
807 sockets
[old_alloc
].type
= type
;
811 prepare_select(fd_set
**fdrp
, fd_set
**fdwp
, int *fdl
, u_int
*nallocp
)
816 for (i
= 0; i
< sockets_alloc
; i
++) {
817 switch (sockets
[i
].type
) {
819 case AUTH_CONNECTION
:
820 n
= MAX(n
, sockets
[i
].fd
);
825 fatal("Unknown socket type %d", sockets
[i
].type
);
830 sz
= howmany(n
+1, NFDBITS
) * sizeof(fd_mask
);
831 if (*fdrp
== NULL
|| sz
> *nallocp
) {
841 debug("XXX shrink: %d < %d", n
, *fdl
);
843 memset(*fdrp
, 0, sz
);
844 memset(*fdwp
, 0, sz
);
846 for (i
= 0; i
< sockets_alloc
; i
++) {
847 switch (sockets
[i
].type
) {
849 case AUTH_CONNECTION
:
850 FD_SET(sockets
[i
].fd
, *fdrp
);
851 if (buffer_len(&sockets
[i
].output
) > 0)
852 FD_SET(sockets
[i
].fd
, *fdwp
);
862 after_select(fd_set
*readset
, fd_set
*writeset
)
864 struct sockaddr_un sunaddr
;
872 for (i
= 0; i
< sockets_alloc
; i
++)
873 switch (sockets
[i
].type
) {
877 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
878 slen
= sizeof(sunaddr
);
879 sock
= accept(sockets
[i
].fd
,
880 (struct sockaddr
*) &sunaddr
, &slen
);
882 error("accept from AUTH_SOCKET: %s",
886 if (getpeereid(sock
, &euid
, &egid
) < 0) {
887 error("getpeereid %d failed: %s",
888 sock
, strerror(errno
));
892 if ((euid
!= 0) && (getuid() != euid
)) {
893 error("uid mismatch: "
894 "peer euid %u != uid %u",
895 (u_int
) euid
, (u_int
) getuid());
899 new_socket(AUTH_CONNECTION
, sock
);
902 case AUTH_CONNECTION
:
903 if (buffer_len(&sockets
[i
].output
) > 0 &&
904 FD_ISSET(sockets
[i
].fd
, writeset
)) {
906 len
= write(sockets
[i
].fd
,
907 buffer_ptr(&sockets
[i
].output
),
908 buffer_len(&sockets
[i
].output
));
909 if (len
== -1 && (errno
== EAGAIN
||
915 close_socket(&sockets
[i
]);
918 buffer_consume(&sockets
[i
].output
, len
);
920 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
922 len
= read(sockets
[i
].fd
, buf
, sizeof(buf
));
923 if (len
== -1 && (errno
== EAGAIN
||
929 close_socket(&sockets
[i
]);
932 buffer_append(&sockets
[i
].input
, buf
, len
);
933 process_message(&sockets
[i
]);
937 fatal("Unknown type %d", sockets
[i
].type
);
958 cleanup_handler(int sig
)
965 check_parent_exists(int sig
)
967 int save_errno
= errno
;
969 if (parent_pid
!= -1 && kill(parent_pid
, 0) < 0) {
970 /* printf("Parent has died - Authentication agent exiting.\n"); */
971 cleanup_handler(sig
); /* safe */
973 mysignal(SIGALRM
, check_parent_exists
);
981 fprintf(stderr
, "Usage: %s [options] [command [args ...]]\n",
983 fprintf(stderr
, "Options:\n");
984 fprintf(stderr
, " -c Generate C-shell commands on stdout.\n");
985 fprintf(stderr
, " -s Generate Bourne shell commands on stdout.\n");
986 fprintf(stderr
, " -k Kill the current agent.\n");
987 fprintf(stderr
, " -d Debug mode.\n");
988 fprintf(stderr
, " -a socket Bind agent socket to given name.\n");
989 fprintf(stderr
, " -t life Default identity lifetime (seconds).\n");
994 main(int ac
, char **av
)
996 int c_flag
= 0, d_flag
= 0, k_flag
= 0, s_flag
= 0;
999 char *shell
, *format
, *pidstr
, *agentsocket
= NULL
;
1000 fd_set
*readsetp
= NULL
, *writesetp
= NULL
;
1001 struct sockaddr_un sunaddr
;
1002 #ifdef HAVE_SETRLIMIT
1007 extern char *optarg
;
1009 char pidstrbuf
[1 + 3 * sizeof pid
];
1015 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1016 /* Disable ptrace on Linux without sgid bit */
1017 prctl(PR_SET_DUMPABLE
, 0);
1020 SSLeay_add_all_algorithms();
1022 __progname
= ssh_get_progname(av
[0]);
1026 while ((ch
= getopt(ac
, av
, "cdksa:t:")) != -1) {
1047 agentsocket
= optarg
;
1050 if ((lifetime
= convtime(optarg
)) == -1) {
1051 fprintf(stderr
, "Invalid lifetime\n");
1062 if (ac
> 0 && (c_flag
|| k_flag
|| s_flag
|| d_flag
))
1065 if (ac
== 0 && !c_flag
&& !s_flag
) {
1066 shell
= getenv("SHELL");
1067 if (shell
!= NULL
&& strncmp(shell
+ strlen(shell
) - 3, "csh", 3) == 0)
1071 pidstr
= getenv(SSH_AGENTPID_ENV_NAME
);
1072 if (pidstr
== NULL
) {
1073 fprintf(stderr
, "%s not set, cannot kill agent\n",
1074 SSH_AGENTPID_ENV_NAME
);
1079 fprintf(stderr
, "%s=\"%s\", which is not a good PID\n",
1080 SSH_AGENTPID_ENV_NAME
, pidstr
);
1083 if (kill(pid
, SIGTERM
) == -1) {
1087 format
= c_flag
? "unsetenv %s;\n" : "unset %s;\n";
1088 printf(format
, SSH_AUTHSOCKET_ENV_NAME
);
1089 printf(format
, SSH_AGENTPID_ENV_NAME
);
1090 printf("echo Agent pid %ld killed;\n", (long)pid
);
1093 parent_pid
= getpid();
1095 if (agentsocket
== NULL
) {
1096 /* Create private directory for agent socket */
1097 strlcpy(socket_dir
, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir
);
1098 if (mkdtemp(socket_dir
) == NULL
) {
1099 perror("mkdtemp: private socket dir");
1102 snprintf(socket_name
, sizeof socket_name
, "%s/agent.%ld", socket_dir
,
1105 /* Try to use specified agent socket */
1106 socket_dir
[0] = '\0';
1107 strlcpy(socket_name
, agentsocket
, sizeof socket_name
);
1111 * Create socket early so it will exist before command gets run from
1114 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1117 *socket_name
= '\0'; /* Don't unlink any existing file */
1120 memset(&sunaddr
, 0, sizeof(sunaddr
));
1121 sunaddr
.sun_family
= AF_UNIX
;
1122 strlcpy(sunaddr
.sun_path
, socket_name
, sizeof(sunaddr
.sun_path
));
1123 prev_mask
= umask(0177);
1124 if (bind(sock
, (struct sockaddr
*) & sunaddr
, sizeof(sunaddr
)) < 0) {
1126 *socket_name
= '\0'; /* Don't unlink any existing file */
1131 if (listen(sock
, SSH_LISTEN_BACKLOG
) < 0) {
1137 * Fork, and have the parent execute the command, if any, or present
1138 * the socket data. The child continues as the authentication agent.
1141 log_init(__progname
, SYSLOG_LEVEL_DEBUG1
, SYSLOG_FACILITY_AUTH
, 1);
1142 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1143 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1144 SSH_AUTHSOCKET_ENV_NAME
);
1145 printf("echo Agent pid %ld;\n", (long)parent_pid
);
1153 if (pid
!= 0) { /* Parent - execute the given command. */
1155 snprintf(pidstrbuf
, sizeof pidstrbuf
, "%ld", (long)pid
);
1157 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1158 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1159 SSH_AUTHSOCKET_ENV_NAME
);
1160 printf(format
, SSH_AGENTPID_ENV_NAME
, pidstrbuf
,
1161 SSH_AGENTPID_ENV_NAME
);
1162 printf("echo Agent pid %ld;\n", (long)pid
);
1165 if (setenv(SSH_AUTHSOCKET_ENV_NAME
, socket_name
, 1) == -1 ||
1166 setenv(SSH_AGENTPID_ENV_NAME
, pidstrbuf
, 1) == -1) {
1175 log_init(__progname
, SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_AUTH
, 0);
1177 if (setsid() == -1) {
1178 error("setsid: %s", strerror(errno
));
1183 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) != -1) {
1184 /* XXX might close listen socket */
1185 (void)dup2(fd
, STDIN_FILENO
);
1186 (void)dup2(fd
, STDOUT_FILENO
);
1187 (void)dup2(fd
, STDERR_FILENO
);
1192 #ifdef HAVE_SETRLIMIT
1193 /* deny core dumps, since memory contains unencrypted private keys */
1194 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
1195 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0) {
1196 error("setrlimit RLIMIT_CORE: %s", strerror(errno
));
1202 new_socket(AUTH_SOCKET
, sock
);
1204 mysignal(SIGALRM
, check_parent_exists
);
1209 signal(SIGINT
, SIG_IGN
);
1210 signal(SIGPIPE
, SIG_IGN
);
1211 signal(SIGHUP
, cleanup_handler
);
1212 signal(SIGTERM
, cleanup_handler
);
1216 prepare_select(&readsetp
, &writesetp
, &max_fd
, &nalloc
);
1217 if (select(max_fd
+ 1, readsetp
, writesetp
, NULL
, NULL
) < 0) {
1220 fatal("select: %s", strerror(errno
));
1222 after_select(readsetp
, writesetp
);