2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * Author: Tatu Ylonen <ylo@cs.hut.fi>
8 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * The authentication agent program.
12 * As far as I am concerned, the code I have written for this software
13 * can be used freely for any purpose. Any derived versions of this
14 * software must be clearly marked as such, and if the derived work is
15 * incompatible with the protocol description in the RFC file, it must be
16 * called by a name other than "ssh" or "Secure Shell".
18 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include "sys-queue.h"
43 RCSID("$OpenBSD: ssh-agent.c,v 1.159 2008/06/28 14:05:15 djm Exp $");
45 #ifdef HAVE_SOLARIS_PRIVILEGE
47 #endif /* HAVE_SOLARIS_PRIVILEGE */
49 #include <openssl/evp.h>
50 #include <openssl/md5.h>
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;
101 u_int parent_alive_interval
= 0;
103 /* pathname and directory for AUTH_SOCKET */
104 char socket_name
[MAXPATHLEN
];
105 char socket_dir
[MAXPATHLEN
];
109 char *lock_passwd
= NULL
;
111 #ifdef HAVE___PROGNAME
112 extern char *__progname
;
117 /* Default lifetime (0 == forever) */
118 static int lifetime
= 0;
121 close_socket(SocketEntry
*e
)
125 e
->type
= AUTH_UNUSED
;
126 buffer_free(&e
->input
);
127 buffer_free(&e
->output
);
128 buffer_free(&e
->request
);
136 for (i
= 0; i
<=2; i
++) {
137 TAILQ_INIT(&idtable
[i
].idlist
);
138 idtable
[i
].nentries
= 0;
142 /* return private key table for requested protocol version */
144 idtab_lookup(int version
)
146 if (version
< 1 || version
> 2)
147 fatal("internal error, bad protocol version %d", version
);
148 return &idtable
[version
];
152 free_identity(Identity
*id
)
159 /* return matching private key for given public key */
161 lookup_identity(Key
*key
, int version
)
165 Idtab
*tab
= idtab_lookup(version
);
166 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
167 if (key_equal(key
, id
->key
))
173 /* Check confirmation of keysign request */
175 confirm_key(Identity
*id
)
180 p
= key_fingerprint(id
->key
, SSH_FP_MD5
, SSH_FP_HEX
);
182 gettext("Allow use of key %s?\nKey fingerprint %s."),
190 /* send list of supported public keys to 'client' */
192 process_request_identities(SocketEntry
*e
, int version
)
194 Idtab
*tab
= idtab_lookup(version
);
199 buffer_put_char(&msg
, (version
== 1) ?
200 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
201 buffer_put_int(&msg
, tab
->nentries
);
202 TAILQ_FOREACH(id
, &tab
->idlist
, next
) {
203 if (id
->key
->type
== KEY_RSA1
) {
204 buffer_put_int(&msg
, BN_num_bits(id
->key
->rsa
->n
));
205 buffer_put_bignum(&msg
, id
->key
->rsa
->e
);
206 buffer_put_bignum(&msg
, id
->key
->rsa
->n
);
210 key_to_blob(id
->key
, &blob
, &blen
);
211 buffer_put_string(&msg
, blob
, blen
);
214 buffer_put_cstring(&msg
, id
->comment
);
216 buffer_put_int(&e
->output
, buffer_len(&msg
));
217 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
223 process_authentication_challenge1(SocketEntry
*e
)
225 u_char buf
[32], mdbuf
[16], session_id
[16];
235 key
= key_new(KEY_RSA1
);
236 if ((challenge
= BN_new()) == NULL
)
237 fatal("process_authentication_challenge1: BN_new failed");
239 (void) buffer_get_int(&e
->request
); /* ignored */
240 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
241 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
242 buffer_get_bignum(&e
->request
, challenge
);
244 /* Only protocol 1.1 is supported */
245 if (buffer_len(&e
->request
) == 0)
247 buffer_get(&e
->request
, session_id
, 16);
248 response_type
= buffer_get_int(&e
->request
);
249 if (response_type
!= 1)
252 id
= lookup_identity(key
, 1);
253 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0)) {
254 Key
*private = id
->key
;
255 /* Decrypt the challenge using the private key. */
256 if (rsa_private_decrypt(challenge
, challenge
, private->rsa
) <= 0)
259 /* The response is MD5 of decrypted challenge plus session id. */
260 len
= BN_num_bytes(challenge
);
261 if (len
<= 0 || len
> 32) {
262 log("process_authentication_challenge: bad challenge length %d", len
);
266 BN_bn2bin(challenge
, buf
+ 32 - len
);
268 MD5_Update(&md
, buf
, 32);
269 MD5_Update(&md
, session_id
, 16);
270 MD5_Final(mdbuf
, &md
);
272 /* Send the response. */
273 buffer_put_char(&msg
, SSH_AGENT_RSA_RESPONSE
);
274 for (i
= 0; i
< 16; i
++)
275 buffer_put_char(&msg
, mdbuf
[i
]);
280 /* Unknown identity or protocol error. Send failure. */
281 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
283 buffer_put_int(&e
->output
, buffer_len(&msg
));
284 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
286 BN_clear_free(challenge
);
292 process_sign_request2(SocketEntry
*e
)
294 u_char
*blob
, *data
, *signature
= NULL
;
295 u_int blen
, dlen
, slen
= 0;
296 extern uint32_t datafellows
;
304 blob
= buffer_get_string(&e
->request
, &blen
);
305 data
= buffer_get_string(&e
->request
, &dlen
);
307 flags
= buffer_get_int(&e
->request
);
308 odatafellows
= datafellows
;
309 if (flags
& SSH_AGENT_OLD_SIGNATURE
)
310 datafellows
= SSH_BUG_SIGBLOB
;
312 key
= key_from_blob(blob
, blen
);
314 Identity
*id
= lookup_identity(key
, 2);
315 if (id
!= NULL
&& (!id
->confirm
|| confirm_key(id
) == 0))
316 ok
= key_sign(id
->key
, &signature
, &slen
, data
, dlen
);
321 buffer_put_char(&msg
, SSH2_AGENT_SIGN_RESPONSE
);
322 buffer_put_string(&msg
, signature
, slen
);
324 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
326 buffer_put_int(&e
->output
, buffer_len(&msg
));
327 buffer_append(&e
->output
, buffer_ptr(&msg
),
332 if (signature
!= NULL
)
334 datafellows
= odatafellows
;
339 process_remove_identity(SocketEntry
*e
, int version
)
348 key
= key_new(KEY_RSA1
);
349 bits
= buffer_get_int(&e
->request
);
350 buffer_get_bignum(&e
->request
, key
->rsa
->e
);
351 buffer_get_bignum(&e
->request
, key
->rsa
->n
);
353 if (bits
!= key_size(key
))
354 log("Warning: identity keysize mismatch: actual %u, announced %u",
355 key_size(key
), bits
);
358 blob
= buffer_get_string(&e
->request
, &blen
);
359 key
= key_from_blob(blob
, blen
);
364 Identity
*id
= lookup_identity(key
, version
);
367 * We have this key. Free the old key. Since we
368 * don't want to leave empty slots in the middle of
369 * the array, we actually free the key there and move
370 * all the entries between the empty slot and the end
373 Idtab
*tab
= idtab_lookup(version
);
374 if (tab
->nentries
< 1)
375 fatal("process_remove_identity: "
376 "internal error: tab->nentries %d",
378 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
385 buffer_put_int(&e
->output
, 1);
386 buffer_put_char(&e
->output
,
387 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
391 process_remove_all_identities(SocketEntry
*e
, int version
)
393 Idtab
*tab
= idtab_lookup(version
);
396 /* Loop over all identities and clear the keys. */
397 for (id
= TAILQ_FIRST(&tab
->idlist
); id
;
398 id
= TAILQ_FIRST(&tab
->idlist
)) {
399 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
403 /* Mark that there are no identities. */
407 buffer_put_int(&e
->output
, 1);
408 buffer_put_char(&e
->output
, SSH_AGENT_SUCCESS
);
411 /* removes expired keys and returns number of seconds until the next expiry */
415 u_int deadline
= 0, now
= time(NULL
);
420 for (version
= 1; version
< 3; version
++) {
421 tab
= idtab_lookup(version
);
422 for (id
= TAILQ_FIRST(&tab
->idlist
); id
; id
= nxt
) {
423 nxt
= TAILQ_NEXT(id
, next
);
426 if (now
>= id
->death
) {
427 debug("expiring key '%s'", id
->comment
);
428 TAILQ_REMOVE(&tab
->idlist
, id
, next
);
432 deadline
= (deadline
== 0) ? id
->death
:
433 MIN(deadline
, id
->death
);
436 if (deadline
== 0 || deadline
<= now
)
439 return (deadline
- now
);
443 process_add_identity(SocketEntry
*e
, int version
)
445 Idtab
*tab
= idtab_lookup(version
);
447 int type
, success
= 0, death
= 0, confirm
= 0;
448 char *type_name
, *comment
;
453 k
= key_new_private(KEY_RSA1
);
454 (void) buffer_get_int(&e
->request
); /* ignored */
455 buffer_get_bignum(&e
->request
, k
->rsa
->n
);
456 buffer_get_bignum(&e
->request
, k
->rsa
->e
);
457 buffer_get_bignum(&e
->request
, k
->rsa
->d
);
458 buffer_get_bignum(&e
->request
, k
->rsa
->iqmp
);
460 /* SSH and SSL have p and q swapped */
461 buffer_get_bignum(&e
->request
, k
->rsa
->q
); /* p */
462 buffer_get_bignum(&e
->request
, k
->rsa
->p
); /* q */
464 /* Generate additional parameters */
465 rsa_generate_additional_parameters(k
->rsa
);
468 type_name
= buffer_get_string(&e
->request
, NULL
);
469 type
= key_type_from_name(type_name
);
473 k
= key_new_private(type
);
474 buffer_get_bignum2(&e
->request
, k
->dsa
->p
);
475 buffer_get_bignum2(&e
->request
, k
->dsa
->q
);
476 buffer_get_bignum2(&e
->request
, k
->dsa
->g
);
477 buffer_get_bignum2(&e
->request
, k
->dsa
->pub_key
);
478 buffer_get_bignum2(&e
->request
, k
->dsa
->priv_key
);
481 k
= key_new_private(type
);
482 buffer_get_bignum2(&e
->request
, k
->rsa
->n
);
483 buffer_get_bignum2(&e
->request
, k
->rsa
->e
);
484 buffer_get_bignum2(&e
->request
, k
->rsa
->d
);
485 buffer_get_bignum2(&e
->request
, k
->rsa
->iqmp
);
486 buffer_get_bignum2(&e
->request
, k
->rsa
->p
);
487 buffer_get_bignum2(&e
->request
, k
->rsa
->q
);
489 /* Generate additional parameters */
490 rsa_generate_additional_parameters(k
->rsa
);
493 buffer_clear(&e
->request
);
498 /* enable blinding */
502 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
503 error("process_add_identity: RSA_blinding_on failed");
509 comment
= buffer_get_string(&e
->request
, NULL
);
514 while (buffer_len(&e
->request
)) {
515 switch ((type
= 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
:
523 error("process_add_identity: "
524 "Unknown constraint type %d", type
);
531 if (lifetime
&& !death
)
532 death
= time(NULL
) + lifetime
;
533 if ((id
= lookup_identity(k
, version
)) == NULL
) {
534 id
= xmalloc(sizeof(Identity
));
536 TAILQ_INSERT_TAIL(&tab
->idlist
, id
, next
);
537 /* Increment the number of identities. */
543 id
->comment
= comment
;
545 id
->confirm
= confirm
;
547 buffer_put_int(&e
->output
, 1);
548 buffer_put_char(&e
->output
,
549 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
552 /* XXX todo: encrypt sensitive data with passphrase */
554 process_lock_agent(SocketEntry
*e
, int lock
)
559 passwd
= buffer_get_string(&e
->request
, NULL
);
560 if (locked
&& !lock
&& strcmp(passwd
, lock_passwd
) == 0) {
562 memset(lock_passwd
, 0, strlen(lock_passwd
));
566 } else if (!locked
&& lock
) {
568 lock_passwd
= xstrdup(passwd
);
571 memset(passwd
, 0, strlen(passwd
));
574 buffer_put_int(&e
->output
, 1);
575 buffer_put_char(&e
->output
,
576 success
? SSH_AGENT_SUCCESS
: SSH_AGENT_FAILURE
);
580 no_identities(SocketEntry
*e
, u_int type
)
585 buffer_put_char(&msg
,
586 (type
== SSH_AGENTC_REQUEST_RSA_IDENTITIES
) ?
587 SSH_AGENT_RSA_IDENTITIES_ANSWER
: SSH2_AGENT_IDENTITIES_ANSWER
);
588 buffer_put_int(&msg
, 0);
589 buffer_put_int(&e
->output
, buffer_len(&msg
));
590 buffer_append(&e
->output
, buffer_ptr(&msg
), buffer_len(&msg
));
594 /* dispatch incoming messages */
597 process_message(SocketEntry
*e
)
602 if (buffer_len(&e
->input
) < 5)
603 return; /* Incomplete message. */
604 cp
= buffer_ptr(&e
->input
);
605 msg_len
= get_u32(cp
);
606 if (msg_len
> 256 * 1024) {
610 if (buffer_len(&e
->input
) < msg_len
+ 4)
613 /* move the current input to e->request */
614 buffer_consume(&e
->input
, 4);
615 buffer_clear(&e
->request
);
616 buffer_append(&e
->request
, buffer_ptr(&e
->input
), msg_len
);
617 buffer_consume(&e
->input
, msg_len
);
618 type
= buffer_get_char(&e
->request
);
620 /* check wheter agent is locked */
621 if (locked
&& type
!= SSH_AGENTC_UNLOCK
) {
622 buffer_clear(&e
->request
);
624 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
625 case SSH2_AGENTC_REQUEST_IDENTITIES
:
626 /* send empty lists */
627 no_identities(e
, type
);
630 /* send a fail message for all other request types */
631 buffer_put_int(&e
->output
, 1);
632 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
637 debug("type %d", type
);
639 case SSH_AGENTC_LOCK
:
640 case SSH_AGENTC_UNLOCK
:
641 process_lock_agent(e
, type
== SSH_AGENTC_LOCK
);
644 case SSH_AGENTC_RSA_CHALLENGE
:
645 process_authentication_challenge1(e
);
647 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
648 process_request_identities(e
, 1);
650 case SSH_AGENTC_ADD_RSA_IDENTITY
:
651 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED
:
652 process_add_identity(e
, 1);
654 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
655 process_remove_identity(e
, 1);
657 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
658 process_remove_all_identities(e
, 1);
661 case SSH2_AGENTC_SIGN_REQUEST
:
662 process_sign_request2(e
);
664 case SSH2_AGENTC_REQUEST_IDENTITIES
:
665 process_request_identities(e
, 2);
667 case SSH2_AGENTC_ADD_IDENTITY
:
668 case SSH2_AGENTC_ADD_ID_CONSTRAINED
:
669 process_add_identity(e
, 2);
671 case SSH2_AGENTC_REMOVE_IDENTITY
:
672 process_remove_identity(e
, 2);
674 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
675 process_remove_all_identities(e
, 2);
678 /* Unknown message. Respond with failure. */
679 error("Unknown message %d", type
);
680 buffer_clear(&e
->request
);
681 buffer_put_int(&e
->output
, 1);
682 buffer_put_char(&e
->output
, SSH_AGENT_FAILURE
);
688 new_socket(sock_type type
, int fd
)
690 u_int i
, old_alloc
, new_alloc
;
697 for (i
= 0; i
< sockets_alloc
; i
++)
698 if (sockets
[i
].type
== AUTH_UNUSED
) {
700 buffer_init(&sockets
[i
].input
);
701 buffer_init(&sockets
[i
].output
);
702 buffer_init(&sockets
[i
].request
);
703 sockets
[i
].type
= type
;
706 old_alloc
= sockets_alloc
;
707 new_alloc
= sockets_alloc
+ 10;
708 sockets
= xrealloc(sockets
, new_alloc
* sizeof(sockets
[0]));
709 for (i
= old_alloc
; i
< new_alloc
; i
++)
710 sockets
[i
].type
= AUTH_UNUSED
;
711 sockets_alloc
= new_alloc
;
712 sockets
[old_alloc
].fd
= fd
;
713 buffer_init(&sockets
[old_alloc
].input
);
714 buffer_init(&sockets
[old_alloc
].output
);
715 buffer_init(&sockets
[old_alloc
].request
);
716 sockets
[old_alloc
].type
= type
;
720 prepare_select(fd_set
**fdrp
, fd_set
**fdwp
, int *fdl
, u_int
*nallocp
,
721 struct timeval
**tvpp
)
723 u_int i
, sz
, deadline
;
725 static struct timeval tv
;
727 for (i
= 0; i
< sockets_alloc
; i
++) {
728 switch (sockets
[i
].type
) {
730 case AUTH_CONNECTION
:
731 n
= MAX(n
, sockets
[i
].fd
);
736 fatal("Unknown socket type %d", sockets
[i
].type
);
741 sz
= howmany(n
+1, NFDBITS
) * sizeof(fd_mask
);
742 if (*fdrp
== NULL
|| sz
> *nallocp
) {
752 debug("XXX shrink: %d < %d", n
, *fdl
);
754 memset(*fdrp
, 0, sz
);
755 memset(*fdwp
, 0, sz
);
757 for (i
= 0; i
< sockets_alloc
; i
++) {
758 switch (sockets
[i
].type
) {
760 case AUTH_CONNECTION
:
761 FD_SET(sockets
[i
].fd
, *fdrp
);
762 if (buffer_len(&sockets
[i
].output
) > 0)
763 FD_SET(sockets
[i
].fd
, *fdwp
);
770 if (parent_alive_interval
!= 0)
771 deadline
= (deadline
== 0) ? parent_alive_interval
:
772 MIN(deadline
, parent_alive_interval
);
776 tv
.tv_sec
= deadline
;
784 after_select(fd_set
*readset
, fd_set
*writeset
)
786 struct sockaddr_un sunaddr
;
794 for (i
= 0; i
< sockets_alloc
; i
++)
795 switch (sockets
[i
].type
) {
799 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
800 slen
= sizeof(sunaddr
);
801 sock
= accept(sockets
[i
].fd
,
802 (struct sockaddr
*)&sunaddr
, &slen
);
804 error("accept from AUTH_SOCKET: %s",
808 if (getpeereid(sock
, &euid
, &egid
) < 0) {
809 error("getpeereid %d failed: %s",
810 sock
, strerror(errno
));
814 if ((euid
!= 0) && (getuid() != euid
)) {
815 error("uid mismatch: "
816 "peer euid %u != uid %u",
817 (u_int
) euid
, (u_int
) getuid());
821 new_socket(AUTH_CONNECTION
, sock
);
824 case AUTH_CONNECTION
:
825 if (buffer_len(&sockets
[i
].output
) > 0 &&
826 FD_ISSET(sockets
[i
].fd
, writeset
)) {
828 len
= write(sockets
[i
].fd
,
829 buffer_ptr(&sockets
[i
].output
),
830 buffer_len(&sockets
[i
].output
));
831 if (len
== -1 && (errno
== EAGAIN
||
833 errno
== EWOULDBLOCK
))
838 close_socket(&sockets
[i
]);
841 buffer_consume(&sockets
[i
].output
, len
);
843 if (FD_ISSET(sockets
[i
].fd
, readset
)) {
845 len
= read(sockets
[i
].fd
, buf
, sizeof(buf
));
846 if (len
== -1 && (errno
== EAGAIN
||
848 errno
== EWOULDBLOCK
))
853 close_socket(&sockets
[i
]);
856 buffer_append(&sockets
[i
].input
, buf
, len
);
857 process_message(&sockets
[i
]);
861 fatal("Unknown type %d", sockets
[i
].type
);
883 cleanup_handler(int sig
)
890 check_parent_exists(void)
892 #ifdef HAVE_SOLARIS_PRIVILEGE
894 * We can not simply use "kill(ppid, 0) < 0" to detect if the parent
895 * has exited when the child process no longer has the
896 * PRIV_PROC_SESSION privilege.
898 if (parent_pid
!= -1 && getppid() != parent_pid
) {
900 if (parent_pid
!= -1 && kill(parent_pid
, 0) < 0) {
903 /* printf("Parent has died - Authentication agent exiting.\n"); */
913 gettext("Usage: %s [options] [command [args ...]]\n"
915 " -c Generate C-shell commands on stdout.\n"
916 " -s Generate Bourne shell commands on stdout.\n"
917 " -k Kill the current agent.\n"
919 " -a socket Bind agent socket to given name.\n"
920 " -t life Default identity lifetime (seconds).\n"),
926 main(int ac
, char **av
)
928 int c_flag
= 0, d_flag
= 0, k_flag
= 0, s_flag
= 0;
929 int sock
, fd
, ch
, result
, saved_errno
;
931 char *shell
, *pidstr
, *agentsocket
= NULL
;
933 fd_set
*readsetp
= NULL
, *writesetp
= NULL
;
934 struct sockaddr_un sunaddr
;
935 #ifdef HAVE_SETRLIMIT
942 char pidstrbuf
[1 + 3 * sizeof pid
];
943 struct timeval
*tvp
= NULL
;
944 #ifdef HAVE_SOLARIS_PRIVILEGE
946 #endif /* HAVE_SOLARIS_PRIVILEGE */
948 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
955 SSLeay_add_all_algorithms();
957 __progname
= get_progname(av
[0]);
961 while ((ch
= getopt(ac
, av
, "cdksa:t:")) != -1) {
982 agentsocket
= optarg
;
985 if ((lifetime
= convtime(optarg
)) == -1) {
986 fprintf(stderr
, gettext("Invalid lifetime\n"));
997 if (ac
> 0 && (c_flag
|| k_flag
|| s_flag
|| d_flag
))
1000 if (ac
== 0 && !c_flag
&& !s_flag
) {
1001 shell
= getenv("SHELL");
1002 if (shell
!= NULL
&&
1003 strncmp(shell
+ strlen(shell
) - 3, "csh", 3) == 0)
1007 pidstr
= getenv(SSH_AGENTPID_ENV_NAME
);
1008 if (pidstr
== NULL
) {
1010 gettext("%s not set, cannot kill agent\n"),
1011 SSH_AGENTPID_ENV_NAME
);
1017 gettext("%s not set, cannot kill agent\n"),
1018 SSH_AGENTPID_ENV_NAME
);
1021 if (kill(pid
, SIGTERM
) == -1) {
1025 format
= c_flag
? "unsetenv %s;\n" : "unset %s;\n";
1026 printf(format
, SSH_AUTHSOCKET_ENV_NAME
);
1027 printf(format
, SSH_AGENTPID_ENV_NAME
);
1029 printf(gettext("Agent pid %ld killed;\n"), (long)pid
);
1032 parent_pid
= getpid();
1034 if (agentsocket
== NULL
) {
1035 /* Create private directory for agent socket */
1036 strlcpy(socket_dir
, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir
);
1037 if (mkdtemp(socket_dir
) == NULL
) {
1038 perror("mkdtemp: private socket dir");
1041 snprintf(socket_name
, sizeof socket_name
, "%s/agent.%ld", socket_dir
,
1044 /* Try to use specified agent socket */
1045 socket_dir
[0] = '\0';
1046 strlcpy(socket_name
, agentsocket
, sizeof socket_name
);
1050 * Create socket early so it will exist before command gets run from
1053 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1056 *socket_name
= '\0'; /* Don't unlink any existing file */
1059 memset(&sunaddr
, 0, sizeof(sunaddr
));
1060 sunaddr
.sun_family
= AF_UNIX
;
1061 strlcpy(sunaddr
.sun_path
, socket_name
, sizeof(sunaddr
.sun_path
));
1062 prev_mask
= umask(0177);
1063 if (bind(sock
, (struct sockaddr
*) &sunaddr
, sizeof(sunaddr
)) < 0) {
1065 *socket_name
= '\0'; /* Don't unlink any existing file */
1070 if (listen(sock
, SSH_LISTEN_BACKLOG
) < 0) {
1076 * Fork, and have the parent execute the command, if any, or present
1077 * the socket data. The child continues as the authentication agent.
1080 log_init(__progname
, SYSLOG_LEVEL_DEBUG1
, SYSLOG_FACILITY_AUTH
, 1);
1081 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1082 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1083 SSH_AUTHSOCKET_ENV_NAME
);
1085 printf(gettext("Agent pid %ld;\n"), (long)parent_pid
);
1093 if (pid
!= 0) { /* Parent - execute the given command. */
1095 snprintf(pidstrbuf
, sizeof pidstrbuf
, "%ld", (long)pid
);
1097 format
= c_flag
? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1098 printf(format
, SSH_AUTHSOCKET_ENV_NAME
, socket_name
,
1099 SSH_AUTHSOCKET_ENV_NAME
);
1100 printf(format
, SSH_AGENTPID_ENV_NAME
, pidstrbuf
,
1101 SSH_AGENTPID_ENV_NAME
);
1103 printf(gettext("Agent pid %ld;\n"), (long)pid
);
1106 if (setenv(SSH_AUTHSOCKET_ENV_NAME
, socket_name
, 1) == -1 ||
1107 setenv(SSH_AGENTPID_ENV_NAME
, pidstrbuf
, 1) == -1) {
1116 log_init(__progname
, SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_AUTH
, 0);
1118 #ifdef HAVE_SOLARIS_PRIVILEGE
1120 * Drop unneeded privs, including basic ones like fork/exec.
1122 * Idiom: remove from 'basic' privs we know we don't want,
1123 * invert the result and remove the resulting set from P.
1125 * None of the priv_delset() calls below, nor the setppriv call
1126 * below can fail, so their return values are not checked.
1128 if ((myprivs
= priv_str_to_set("basic", ",", NULL
)) == NULL
)
1129 fatal("priv_str_to_set failed: %m");
1130 (void) priv_delset(myprivs
, PRIV_PROC_EXEC
);
1131 (void) priv_delset(myprivs
, PRIV_PROC_FORK
);
1132 (void) priv_delset(myprivs
, PRIV_FILE_LINK_ANY
);
1133 (void) priv_delset(myprivs
, PRIV_PROC_INFO
);
1134 (void) priv_delset(myprivs
, PRIV_PROC_SESSION
);
1135 priv_inverse(myprivs
);
1136 (void) setppriv(PRIV_OFF
, PRIV_PERMITTED
, myprivs
);
1137 (void) priv_freeset(myprivs
);
1138 #endif /* HAVE_SOLARIS_PRIVILEGE */
1140 if (setsid() == -1) {
1141 error("setsid: %s", strerror(errno
));
1146 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) != -1) {
1147 /* XXX might close listen socket */
1148 (void)dup2(fd
, STDIN_FILENO
);
1149 (void)dup2(fd
, STDOUT_FILENO
);
1150 (void)dup2(fd
, STDERR_FILENO
);
1155 #ifdef HAVE_SETRLIMIT
1156 /* deny core dumps, since memory contains unencrypted private keys */
1157 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
1158 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0) {
1159 error("setrlimit RLIMIT_CORE: %s", strerror(errno
));
1165 new_socket(AUTH_SOCKET
, sock
);
1167 parent_alive_interval
= 10;
1170 signal(SIGINT
, SIG_IGN
);
1171 signal(SIGPIPE
, SIG_IGN
);
1172 signal(SIGHUP
, cleanup_handler
);
1173 signal(SIGTERM
, cleanup_handler
);
1177 prepare_select(&readsetp
, &writesetp
, &max_fd
, &nalloc
, &tvp
);
1178 result
= select(max_fd
+ 1, readsetp
, writesetp
, NULL
, tvp
);
1179 saved_errno
= errno
;
1180 if (parent_alive_interval
!= 0)
1181 check_parent_exists();
1182 (void) reaper(); /* remove expired keys */
1184 if (saved_errno
== EINTR
)
1186 fatal("select: %s", strerror(saved_errno
));
1187 } else if (result
> 0)
1188 after_select(readsetp
, writesetp
);
1191 return (0); /* keep lint happy */