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.120 2004/08/11 21:43:05 avsm Exp $");
41 #include <openssl/evp.h>
42 #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 extern char *__progname;
112 /* Default lifetime (0 == forever) */
113 static int lifetime = 0;
116 close_socket(SocketEntry *e)
120 e->type = AUTH_UNUSED;
121 buffer_free(&e->input);
122 buffer_free(&e->output);
123 buffer_free(&e->request);
131 for (i = 0; i <=2; i++) {
132 TAILQ_INIT(&idtable[i].idlist);
133 idtable[i].nentries = 0;
137 /* return private key table for requested protocol version */
139 idtab_lookup(int version)
141 if (version < 1 || version > 2)
142 fatal("internal error, bad protocol version %d", version);
143 return &idtable[version];
147 free_identity(Identity *id)
154 /* return matching private key for given public key */
156 lookup_identity(Key *key, int version)
160 Idtab *tab = idtab_lookup(version);
161 TAILQ_FOREACH(id, &tab->idlist, next) {
162 if (key_equal(key, id->key))
168 /* Check confirmation of keysign request */
170 confirm_key(Identity *id)
172 char *p, prompt[1024];
175 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
176 snprintf(prompt, sizeof(prompt), "Allow use of key %s?\n"
177 "Key fingerprint %s.", id->comment, p);
179 p = read_passphrase(prompt, RP_ALLOW_EOF);
182 * Accept empty responses and responses consisting
183 * of the word "yes" as affirmative.
185 if (*p == '\0' || *p == '\n' || strcasecmp(p, "yes") == 0)
192 /* send list of supported public keys to 'client' */
194 process_request_identities(SocketEntry *e, int version)
196 Idtab *tab = idtab_lookup(version);
201 buffer_put_char(&msg, (version == 1) ?
202 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
203 buffer_put_int(&msg, tab->nentries);
204 TAILQ_FOREACH(id, &tab->idlist, next) {
205 if (id->key->type == KEY_RSA1) {
206 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
207 buffer_put_bignum(&msg, id->key->rsa->e);
208 buffer_put_bignum(&msg, id->key->rsa->n);
212 key_to_blob(id->key, &blob, &blen);
213 buffer_put_string(&msg, blob, blen);
216 buffer_put_cstring(&msg, id->comment);
218 buffer_put_int(&e->output, buffer_len(&msg));
219 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
225 process_authentication_challenge1(SocketEntry *e)
227 u_char buf[32], mdbuf[16], session_id[16];
237 key = key_new(KEY_RSA1);
238 if ((challenge = BN_new()) == NULL)
239 fatal("process_authentication_challenge1: BN_new failed");
241 (void) buffer_get_int(&e->request); /* ignored */
242 buffer_get_bignum(&e->request, key->rsa->e);
243 buffer_get_bignum(&e->request, key->rsa->n);
244 buffer_get_bignum(&e->request, challenge);
246 /* Only protocol 1.1 is supported */
247 if (buffer_len(&e->request) == 0)
249 buffer_get(&e->request, session_id, 16);
250 response_type = buffer_get_int(&e->request);
251 if (response_type != 1)
254 id = lookup_identity(key, 1);
255 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
256 Key *private = id->key;
257 /* Decrypt the challenge using the private key. */
258 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
261 /* The response is MD5 of decrypted challenge plus session id. */
262 len = BN_num_bytes(challenge);
263 if (len <= 0 || len > 32) {
264 logit("process_authentication_challenge: bad challenge length %d", len);
268 BN_bn2bin(challenge, buf + 32 - len);
270 MD5_Update(&md, buf, 32);
271 MD5_Update(&md, session_id, 16);
272 MD5_Final(mdbuf, &md);
274 /* Send the response. */
275 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
276 for (i = 0; i < 16; i++)
277 buffer_put_char(&msg, mdbuf[i]);
282 /* Unknown identity or protocol error. Send failure. */
283 buffer_put_char(&msg, SSH_AGENT_FAILURE);
285 buffer_put_int(&e->output, buffer_len(&msg));
286 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
288 BN_clear_free(challenge);
294 process_sign_request2(SocketEntry *e)
296 u_char *blob, *data, *signature = NULL;
297 u_int blen, dlen, slen = 0;
298 extern int datafellows;
305 blob = buffer_get_string(&e->request, &blen);
306 data = buffer_get_string(&e->request, &dlen);
308 flags = buffer_get_int(&e->request);
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)
338 process_remove_identity(SocketEntry *e, int version)
347 key = key_new(KEY_RSA1);
348 bits = buffer_get_int(&e->request);
349 buffer_get_bignum(&e->request, key->rsa->e);
350 buffer_get_bignum(&e->request, key->rsa->n);
352 if (bits != key_size(key))
353 logit("Warning: identity keysize mismatch: actual %u, announced %u",
354 key_size(key), bits);
357 blob = buffer_get_string(&e->request, &blen);
358 key = key_from_blob(blob, blen);
363 Identity *id = lookup_identity(key, version);
366 * We have this key. Free the old key. Since we
367 * don\'t want to leave empty slots in the middle of
368 * the array, we actually free the key there and move
369 * all the entries between the empty slot and the end
372 Idtab *tab = idtab_lookup(version);
373 if (tab->nentries < 1)
374 fatal("process_remove_identity: "
375 "internal error: tab->nentries %d",
377 TAILQ_REMOVE(&tab->idlist, id, next);
384 buffer_put_int(&e->output, 1);
385 buffer_put_char(&e->output,
386 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
390 process_remove_all_identities(SocketEntry *e, int version)
392 Idtab *tab = idtab_lookup(version);
395 /* Loop over all identities and clear the keys. */
396 for (id = TAILQ_FIRST(&tab->idlist); id;
397 id = TAILQ_FIRST(&tab->idlist)) {
398 TAILQ_REMOVE(&tab->idlist, id, next);
402 /* Mark that there are no identities. */
406 buffer_put_int(&e->output, 1);
407 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
413 u_int now = time(NULL);
418 for (version = 1; version < 3; version++) {
419 tab = idtab_lookup(version);
420 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
421 nxt = TAILQ_NEXT(id, next);
422 if (id->death != 0 && now >= id->death) {
423 TAILQ_REMOVE(&tab->idlist, id, next);
432 process_add_identity(SocketEntry *e, int version)
434 Idtab *tab = idtab_lookup(version);
435 int type, success = 0, death = 0, confirm = 0;
436 char *type_name, *comment;
441 k = key_new_private(KEY_RSA1);
442 (void) buffer_get_int(&e->request); /* ignored */
443 buffer_get_bignum(&e->request, k->rsa->n);
444 buffer_get_bignum(&e->request, k->rsa->e);
445 buffer_get_bignum(&e->request, k->rsa->d);
446 buffer_get_bignum(&e->request, k->rsa->iqmp);
448 /* SSH and SSL have p and q swapped */
449 buffer_get_bignum(&e->request, k->rsa->q); /* p */
450 buffer_get_bignum(&e->request, k->rsa->p); /* q */
452 /* Generate additional parameters */
453 rsa_generate_additional_parameters(k->rsa);
456 type_name = buffer_get_string(&e->request, NULL);
457 type = key_type_from_name(type_name);
461 k = key_new_private(type);
462 buffer_get_bignum2(&e->request, k->dsa->p);
463 buffer_get_bignum2(&e->request, k->dsa->q);
464 buffer_get_bignum2(&e->request, k->dsa->g);
465 buffer_get_bignum2(&e->request, k->dsa->pub_key);
466 buffer_get_bignum2(&e->request, k->dsa->priv_key);
469 k = key_new_private(type);
470 buffer_get_bignum2(&e->request, k->rsa->n);
471 buffer_get_bignum2(&e->request, k->rsa->e);
472 buffer_get_bignum2(&e->request, k->rsa->d);
473 buffer_get_bignum2(&e->request, k->rsa->iqmp);
474 buffer_get_bignum2(&e->request, k->rsa->p);
475 buffer_get_bignum2(&e->request, k->rsa->q);
477 /* Generate additional parameters */
478 rsa_generate_additional_parameters(k->rsa);
481 buffer_clear(&e->request);
486 /* enable blinding */
490 if (RSA_blinding_on(k->rsa, NULL) != 1) {
491 error("process_add_identity: RSA_blinding_on failed");
497 comment = buffer_get_string(&e->request, NULL);
503 while (buffer_len(&e->request)) {
504 switch (buffer_get_char(&e->request)) {
505 case SSH_AGENT_CONSTRAIN_LIFETIME:
506 death = time(NULL) + buffer_get_int(&e->request);
508 case SSH_AGENT_CONSTRAIN_CONFIRM:
515 if (lifetime && !death)
516 death = time(NULL) + lifetime;
517 if (lookup_identity(k, version) == NULL) {
518 Identity *id = xmalloc(sizeof(Identity));
520 id->comment = comment;
522 id->confirm = confirm;
523 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
524 /* Increment the number of identities. */
531 buffer_put_int(&e->output, 1);
532 buffer_put_char(&e->output,
533 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
536 /* XXX todo: encrypt sensitive data with passphrase */
538 process_lock_agent(SocketEntry *e, int lock)
543 passwd = buffer_get_string(&e->request, NULL);
544 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
546 memset(lock_passwd, 0, strlen(lock_passwd));
550 } else if (!locked && lock) {
552 lock_passwd = xstrdup(passwd);
555 memset(passwd, 0, strlen(passwd));
558 buffer_put_int(&e->output, 1);
559 buffer_put_char(&e->output,
560 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
564 no_identities(SocketEntry *e, u_int type)
569 buffer_put_char(&msg,
570 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
571 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
572 buffer_put_int(&msg, 0);
573 buffer_put_int(&e->output, buffer_len(&msg));
574 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
580 process_add_smartcard_key (SocketEntry *e)
582 char *sc_reader_id = NULL, *pin;
583 int i, version, success = 0, death = 0, confirm = 0;
588 sc_reader_id = buffer_get_string(&e->request, NULL);
589 pin = buffer_get_string(&e->request, NULL);
591 while (buffer_len(&e->request)) {
592 switch (buffer_get_char(&e->request)) {
593 case SSH_AGENT_CONSTRAIN_LIFETIME:
594 death = time(NULL) + buffer_get_int(&e->request);
596 case SSH_AGENT_CONSTRAIN_CONFIRM:
603 if (lifetime && !death)
604 death = time(NULL) + lifetime;
606 keys = sc_get_keys(sc_reader_id, pin);
610 if (keys == NULL || keys[0] == NULL) {
611 error("sc_get_keys failed");
614 for (i = 0; keys[i] != NULL; i++) {
616 version = k->type == KEY_RSA1 ? 1 : 2;
617 tab = idtab_lookup(version);
618 if (lookup_identity(k, version) == NULL) {
619 id = xmalloc(sizeof(Identity));
621 id->comment = sc_get_key_label(k);
623 id->confirm = confirm;
624 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
634 buffer_put_int(&e->output, 1);
635 buffer_put_char(&e->output,
636 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
640 process_remove_smartcard_key(SocketEntry *e)
642 char *sc_reader_id = NULL, *pin;
643 int i, version, success = 0;
644 Key **keys, *k = NULL;
648 sc_reader_id = buffer_get_string(&e->request, NULL);
649 pin = buffer_get_string(&e->request, NULL);
650 keys = sc_get_keys(sc_reader_id, pin);
654 if (keys == NULL || keys[0] == NULL) {
655 error("sc_get_keys failed");
658 for (i = 0; keys[i] != NULL; i++) {
660 version = k->type == KEY_RSA1 ? 1 : 2;
661 if ((id = lookup_identity(k, version)) != NULL) {
662 tab = idtab_lookup(version);
663 TAILQ_REMOVE(&tab->idlist, id, next);
673 buffer_put_int(&e->output, 1);
674 buffer_put_char(&e->output,
675 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
677 #endif /* SMARTCARD */
679 /* dispatch incoming messages */
682 process_message(SocketEntry *e)
690 if (buffer_len(&e->input) < 5)
691 return; /* Incomplete message. */
692 cp = buffer_ptr(&e->input);
693 msg_len = GET_32BIT(cp);
694 if (msg_len > 256 * 1024) {
698 if (buffer_len(&e->input) < msg_len + 4)
701 /* move the current input to e->request */
702 buffer_consume(&e->input, 4);
703 buffer_clear(&e->request);
704 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
705 buffer_consume(&e->input, msg_len);
706 type = buffer_get_char(&e->request);
708 /* check wheter agent is locked */
709 if (locked && type != SSH_AGENTC_UNLOCK) {
710 buffer_clear(&e->request);
712 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
713 case SSH2_AGENTC_REQUEST_IDENTITIES:
714 /* send empty lists */
715 no_identities(e, type);
718 /* send a fail message for all other request types */
719 buffer_put_int(&e->output, 1);
720 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
725 debug("type %d", type);
727 case SSH_AGENTC_LOCK:
728 case SSH_AGENTC_UNLOCK:
729 process_lock_agent(e, type == SSH_AGENTC_LOCK);
732 case SSH_AGENTC_RSA_CHALLENGE:
733 process_authentication_challenge1(e);
735 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
736 process_request_identities(e, 1);
738 case SSH_AGENTC_ADD_RSA_IDENTITY:
739 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
740 process_add_identity(e, 1);
742 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
743 process_remove_identity(e, 1);
745 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
746 process_remove_all_identities(e, 1);
749 case SSH2_AGENTC_SIGN_REQUEST:
750 process_sign_request2(e);
752 case SSH2_AGENTC_REQUEST_IDENTITIES:
753 process_request_identities(e, 2);
755 case SSH2_AGENTC_ADD_IDENTITY:
756 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
757 process_add_identity(e, 2);
759 case SSH2_AGENTC_REMOVE_IDENTITY:
760 process_remove_identity(e, 2);
762 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
763 process_remove_all_identities(e, 2);
766 case SSH_AGENTC_ADD_SMARTCARD_KEY:
767 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
768 process_add_smartcard_key(e);
770 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
771 process_remove_smartcard_key(e);
773 #endif /* SMARTCARD */
775 /* Unknown message. Respond with failure. */
776 error("Unknown message %d", type);
777 buffer_clear(&e->request);
778 buffer_put_int(&e->output, 1);
779 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
785 new_socket(sock_type type, int fd)
787 u_int i, old_alloc, new_alloc;
794 for (i = 0; i < sockets_alloc; i++)
795 if (sockets[i].type == AUTH_UNUSED) {
797 buffer_init(&sockets[i].input);
798 buffer_init(&sockets[i].output);
799 buffer_init(&sockets[i].request);
800 sockets[i].type = type;
803 old_alloc = sockets_alloc;
804 new_alloc = sockets_alloc + 10;
806 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
808 sockets = xmalloc(new_alloc * sizeof(sockets[0]));
809 for (i = old_alloc; i < new_alloc; i++)
810 sockets[i].type = AUTH_UNUSED;
811 sockets_alloc = new_alloc;
812 sockets[old_alloc].fd = fd;
813 buffer_init(&sockets[old_alloc].input);
814 buffer_init(&sockets[old_alloc].output);
815 buffer_init(&sockets[old_alloc].request);
816 sockets[old_alloc].type = type;
820 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
825 for (i = 0; i < sockets_alloc; i++) {
826 switch (sockets[i].type) {
828 case AUTH_CONNECTION:
829 n = MAX(n, sockets[i].fd);
834 fatal("Unknown socket type %d", sockets[i].type);
839 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
840 if (*fdrp == NULL || sz > *nallocp) {
850 debug("XXX shrink: %d < %d", n, *fdl);
852 memset(*fdrp, 0, sz);
853 memset(*fdwp, 0, sz);
855 for (i = 0; i < sockets_alloc; i++) {
856 switch (sockets[i].type) {
858 case AUTH_CONNECTION:
859 FD_SET(sockets[i].fd, *fdrp);
860 if (buffer_len(&sockets[i].output) > 0)
861 FD_SET(sockets[i].fd, *fdwp);
871 after_select(fd_set *readset, fd_set *writeset)
873 struct sockaddr_un sunaddr;
881 for (i = 0; i < sockets_alloc; i++)
882 switch (sockets[i].type) {
886 if (FD_ISSET(sockets[i].fd, readset)) {
887 slen = sizeof(sunaddr);
888 sock = accept(sockets[i].fd,
889 (struct sockaddr *) &sunaddr, &slen);
891 error("accept from AUTH_SOCKET: %s",
895 if (getpeereid(sock, &euid, &egid) < 0) {
896 error("getpeereid %d failed: %s",
897 sock, strerror(errno));
901 if ((euid != 0) && (getuid() != euid)) {
902 error("uid mismatch: "
903 "peer euid %u != uid %u",
904 (u_int) euid, (u_int) getuid());
908 new_socket(AUTH_CONNECTION, sock);
911 case AUTH_CONNECTION:
912 if (buffer_len(&sockets[i].output) > 0 &&
913 FD_ISSET(sockets[i].fd, writeset)) {
915 len = write(sockets[i].fd,
916 buffer_ptr(&sockets[i].output),
917 buffer_len(&sockets[i].output));
918 if (len == -1 && (errno == EAGAIN ||
924 close_socket(&sockets[i]);
927 buffer_consume(&sockets[i].output, len);
929 if (FD_ISSET(sockets[i].fd, readset)) {
931 len = read(sockets[i].fd, buf, sizeof(buf));
932 if (len == -1 && (errno == EAGAIN ||
938 close_socket(&sockets[i]);
941 buffer_append(&sockets[i].input, buf, len);
942 process_message(&sockets[i]);
946 fatal("Unknown type %d", sockets[i].type);
967 cleanup_handler(int sig)
974 check_parent_exists(int sig)
976 int save_errno = errno;
978 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
979 /* printf("Parent has died - Authentication agent exiting.\n"); */
980 cleanup_handler(sig); /* safe */
982 mysignal(SIGALRM, check_parent_exists);
990 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
992 fprintf(stderr, "Options:\n");
993 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
994 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
995 fprintf(stderr, " -k Kill the current agent.\n");
996 fprintf(stderr, " -d Debug mode.\n");
997 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
998 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
1003 main(int ac, char **av)
1005 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1008 char *shell, *format, *pidstr, *agentsocket = NULL;
1009 fd_set *readsetp = NULL, *writesetp = NULL;
1010 struct sockaddr_un sunaddr;
1011 #ifdef HAVE_SETRLIMIT
1018 extern char *optarg;
1020 char pidstrbuf[1 + 3 * sizeof pid];
1027 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1028 /* Disable ptrace on Linux without sgid bit */
1029 prctl(PR_SET_DUMPABLE, 0);
1032 SSLeay_add_all_algorithms();
1034 __progname = ssh_get_progname(av[0]);
1038 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
1059 agentsocket = optarg;
1062 if ((lifetime = convtime(optarg)) == -1) {
1063 fprintf(stderr, "Invalid lifetime\n");
1074 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1077 if (ac == 0 && !c_flag && !s_flag) {
1078 shell = getenv("SHELL");
1079 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1083 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1084 if (pidstr == NULL) {
1085 fprintf(stderr, "%s not set, cannot kill agent\n",
1086 SSH_AGENTPID_ENV_NAME);
1091 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1092 SSH_AGENTPID_ENV_NAME, pidstr);
1095 if (kill(pid, SIGTERM) == -1) {
1099 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1100 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1101 printf(format, SSH_AGENTPID_ENV_NAME);
1102 printf("echo Agent pid %ld killed;\n", (long)pid);
1105 parent_pid = getpid();
1107 if (agentsocket == NULL) {
1108 /* Create private directory for agent socket */
1109 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
1110 if (mkdtemp(socket_dir) == NULL) {
1111 perror("mkdtemp: private socket dir");
1114 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1117 /* Try to use specified agent socket */
1118 socket_dir[0] = '\0';
1119 strlcpy(socket_name, agentsocket, sizeof socket_name);
1123 * Create socket early so it will exist before command gets run from
1126 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1131 memset(&sunaddr, 0, sizeof(sunaddr));
1132 sunaddr.sun_family = AF_UNIX;
1133 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1135 prev_mask = umask(0177);
1137 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1147 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1153 * Fork, and have the parent execute the command, if any, or present
1154 * the socket data. The child continues as the authentication agent.
1157 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1158 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1159 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1160 SSH_AUTHSOCKET_ENV_NAME);
1161 printf("echo Agent pid %ld;\n", (long)parent_pid);
1169 if (pid != 0) { /* Parent - execute the given command. */
1171 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1173 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1174 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1175 SSH_AUTHSOCKET_ENV_NAME);
1176 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1177 SSH_AGENTPID_ENV_NAME);
1178 printf("echo Agent pid %ld;\n", (long)pid);
1181 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1182 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1191 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1193 if (setsid() == -1) {
1194 error("setsid: %s", strerror(errno));
1199 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1200 /* XXX might close listen socket */
1201 (void)dup2(fd, STDIN_FILENO);
1202 (void)dup2(fd, STDOUT_FILENO);
1203 (void)dup2(fd, STDERR_FILENO);
1208 #ifdef HAVE_SETRLIMIT
1209 /* deny core dumps, since memory contains unencrypted private keys */
1210 rlim.rlim_cur = rlim.rlim_max = 0;
1211 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1212 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1218 new_socket(AUTH_SOCKET, sock);
1220 mysignal(SIGALRM, check_parent_exists);
1225 signal(SIGINT, SIG_IGN);
1226 signal(SIGPIPE, SIG_IGN);
1227 signal(SIGHUP, cleanup_handler);
1228 signal(SIGTERM, cleanup_handler);
1232 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1233 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1236 fatal("select: %s", strerror(errno));
1238 after_select(readsetp, writesetp);