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/fake-queue.h"
38 RCSID("$OpenBSD: ssh-agent.c,v 1.92 2002/06/15 00:01:36 markus Exp $");
40 #include <openssl/evp.h>
41 #include <openssl/md5.h>
72 u_int sockets_alloc = 0;
73 SocketEntry *sockets = NULL;
75 typedef struct identity {
76 TAILQ_ENTRY(identity) next;
84 TAILQ_HEAD(idqueue, identity) idlist;
87 /* private key table, one per protocol version */
92 /* pid of shell == parent of agent */
93 pid_t parent_pid = -1;
95 /* pathname and directory for AUTH_SOCKET */
96 char socket_name[1024];
97 char socket_dir[1024];
101 char *lock_passwd = NULL;
103 #ifdef HAVE___PROGNAME
104 extern char *__progname;
113 for (i = 0; i <=2; i++) {
114 TAILQ_INIT(&idtable[i].idlist);
115 idtable[i].nentries = 0;
119 /* return private key table for requested protocol version */
121 idtab_lookup(int version)
123 if (version < 1 || version > 2)
124 fatal("internal error, bad protocol version %d", version);
125 return &idtable[version];
129 free_identity(Identity *id)
136 /* return matching private key for given public key */
138 lookup_identity(Key *key, int version)
142 Idtab *tab = idtab_lookup(version);
143 TAILQ_FOREACH(id, &tab->idlist, next) {
144 if (key_equal(key, id->key))
150 /* send list of supported public keys to 'client' */
152 process_request_identities(SocketEntry *e, int version)
154 Idtab *tab = idtab_lookup(version);
159 buffer_put_char(&msg, (version == 1) ?
160 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
161 buffer_put_int(&msg, tab->nentries);
162 TAILQ_FOREACH(id, &tab->idlist, next) {
163 if (id->key->type == KEY_RSA1) {
164 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
165 buffer_put_bignum(&msg, id->key->rsa->e);
166 buffer_put_bignum(&msg, id->key->rsa->n);
170 key_to_blob(id->key, &blob, &blen);
171 buffer_put_string(&msg, blob, blen);
174 buffer_put_cstring(&msg, id->comment);
176 buffer_put_int(&e->output, buffer_len(&msg));
177 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
183 process_authentication_challenge1(SocketEntry *e)
191 u_char buf[32], mdbuf[16], session_id[16];
195 key = key_new(KEY_RSA1);
196 if ((challenge = BN_new()) == NULL)
197 fatal("process_authentication_challenge1: BN_new failed");
199 buffer_get_int(&e->request); /* ignored */
200 buffer_get_bignum(&e->request, key->rsa->e);
201 buffer_get_bignum(&e->request, key->rsa->n);
202 buffer_get_bignum(&e->request, challenge);
204 /* Only protocol 1.1 is supported */
205 if (buffer_len(&e->request) == 0)
207 buffer_get(&e->request, session_id, 16);
208 response_type = buffer_get_int(&e->request);
209 if (response_type != 1)
212 id = lookup_identity(key, 1);
214 Key *private = id->key;
215 /* Decrypt the challenge using the private key. */
216 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
219 /* The response is MD5 of decrypted challenge plus session id. */
220 len = BN_num_bytes(challenge);
221 if (len <= 0 || len > 32) {
222 log("process_authentication_challenge: bad challenge length %d", len);
226 BN_bn2bin(challenge, buf + 32 - len);
228 MD5_Update(&md, buf, 32);
229 MD5_Update(&md, session_id, 16);
230 MD5_Final(mdbuf, &md);
232 /* Send the response. */
233 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
234 for (i = 0; i < 16; i++)
235 buffer_put_char(&msg, mdbuf[i]);
240 /* Unknown identity or protocol error. Send failure. */
241 buffer_put_char(&msg, SSH_AGENT_FAILURE);
243 buffer_put_int(&e->output, buffer_len(&msg));
244 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
246 BN_clear_free(challenge);
252 process_sign_request2(SocketEntry *e)
254 extern int datafellows;
256 u_char *blob, *data, *signature = NULL;
257 u_int blen, dlen, slen = 0;
264 blob = buffer_get_string(&e->request, &blen);
265 data = buffer_get_string(&e->request, &dlen);
267 flags = buffer_get_int(&e->request);
268 if (flags & SSH_AGENT_OLD_SIGNATURE)
269 datafellows = SSH_BUG_SIGBLOB;
271 key = key_from_blob(blob, blen);
273 Identity *id = lookup_identity(key, 2);
275 ok = key_sign(id->key, &signature, &slen, data, dlen);
280 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
281 buffer_put_string(&msg, signature, slen);
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),
291 if (signature != NULL)
297 process_remove_identity(SocketEntry *e, int version)
307 key = key_new(KEY_RSA1);
308 bits = buffer_get_int(&e->request);
309 buffer_get_bignum(&e->request, key->rsa->e);
310 buffer_get_bignum(&e->request, key->rsa->n);
312 if (bits != key_size(key))
313 log("Warning: identity keysize mismatch: actual %d, announced %d",
314 key_size(key), bits);
317 blob = buffer_get_string(&e->request, &blen);
318 key = key_from_blob(blob, blen);
323 Identity *id = lookup_identity(key, version);
326 * We have this key. Free the old key. Since we
327 * don\'t want to leave empty slots in the middle of
328 * the array, we actually free the key there and move
329 * all the entries between the empty slot and the end
332 Idtab *tab = idtab_lookup(version);
333 if (tab->nentries < 1)
334 fatal("process_remove_identity: "
335 "internal error: tab->nentries %d",
337 TAILQ_REMOVE(&tab->idlist, id, next);
344 buffer_put_int(&e->output, 1);
345 buffer_put_char(&e->output,
346 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
350 process_remove_all_identities(SocketEntry *e, int version)
352 Idtab *tab = idtab_lookup(version);
355 /* Loop over all identities and clear the keys. */
356 for (id = TAILQ_FIRST(&tab->idlist); id;
357 id = TAILQ_FIRST(&tab->idlist)) {
358 TAILQ_REMOVE(&tab->idlist, id, next);
362 /* Mark that there are no identities. */
366 buffer_put_int(&e->output, 1);
367 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
377 u_int now = time(NULL);
379 for (version = 1; version < 3; version++) {
380 tab = idtab_lookup(version);
381 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
382 nxt = TAILQ_NEXT(id, next);
383 if (id->death != 0 && now >= id->death) {
384 TAILQ_REMOVE(&tab->idlist, id, next);
393 process_add_identity(SocketEntry *e, int version)
398 int type, success = 0;
399 Idtab *tab = idtab_lookup(version);
403 k = key_new_private(KEY_RSA1);
404 buffer_get_int(&e->request); /* ignored */
405 buffer_get_bignum(&e->request, k->rsa->n);
406 buffer_get_bignum(&e->request, k->rsa->e);
407 buffer_get_bignum(&e->request, k->rsa->d);
408 buffer_get_bignum(&e->request, k->rsa->iqmp);
410 /* SSH and SSL have p and q swapped */
411 buffer_get_bignum(&e->request, k->rsa->q); /* p */
412 buffer_get_bignum(&e->request, k->rsa->p); /* q */
414 /* Generate additional parameters */
415 rsa_generate_additional_parameters(k->rsa);
418 type_name = buffer_get_string(&e->request, NULL);
419 type = key_type_from_name(type_name);
423 k = key_new_private(type);
424 buffer_get_bignum2(&e->request, k->dsa->p);
425 buffer_get_bignum2(&e->request, k->dsa->q);
426 buffer_get_bignum2(&e->request, k->dsa->g);
427 buffer_get_bignum2(&e->request, k->dsa->pub_key);
428 buffer_get_bignum2(&e->request, k->dsa->priv_key);
431 k = key_new_private(type);
432 buffer_get_bignum2(&e->request, k->rsa->n);
433 buffer_get_bignum2(&e->request, k->rsa->e);
434 buffer_get_bignum2(&e->request, k->rsa->d);
435 buffer_get_bignum2(&e->request, k->rsa->iqmp);
436 buffer_get_bignum2(&e->request, k->rsa->p);
437 buffer_get_bignum2(&e->request, k->rsa->q);
439 /* Generate additional parameters */
440 rsa_generate_additional_parameters(k->rsa);
443 buffer_clear(&e->request);
448 comment = buffer_get_string(&e->request, NULL);
454 if (lookup_identity(k, version) == NULL) {
455 Identity *id = xmalloc(sizeof(Identity));
457 id->comment = comment;
459 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
460 /* Increment the number of identities. */
467 buffer_put_int(&e->output, 1);
468 buffer_put_char(&e->output,
469 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
473 process_contrain_identity(SocketEntry *e, int version)
477 u_int blen, bits, death = 0;
482 key = key_new(KEY_RSA1);
483 bits = buffer_get_int(&e->request);
484 buffer_get_bignum(&e->request, key->rsa->e);
485 buffer_get_bignum(&e->request, key->rsa->n);
489 blob = buffer_get_string(&e->request, &blen);
490 key = key_from_blob(blob, blen);
494 while (buffer_len(&e->request)) {
495 switch (buffer_get_char(&e->request)) {
496 case SSH_AGENT_CONTRAIN_LIFETIME:
497 death = time(NULL) + buffer_get_int(&e->request);
504 Identity *id = lookup_identity(key, version);
505 if (id != NULL && id->death == 0 && death != 0) {
511 buffer_put_int(&e->output, 1);
512 buffer_put_char(&e->output,
513 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
516 /* XXX todo: encrypt sensitive data with passphrase */
518 process_lock_agent(SocketEntry *e, int lock)
523 passwd = buffer_get_string(&e->request, NULL);
524 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
526 memset(lock_passwd, 0, strlen(lock_passwd));
530 } else if (!locked && lock) {
532 lock_passwd = xstrdup(passwd);
535 memset(passwd, 0, strlen(passwd));
538 buffer_put_int(&e->output, 1);
539 buffer_put_char(&e->output,
540 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
545 no_identities(SocketEntry *e, u_int type)
550 buffer_put_char(&msg,
551 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
552 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
553 buffer_put_int(&msg, 0);
554 buffer_put_int(&e->output, buffer_len(&msg));
555 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
561 process_add_smartcard_key (SocketEntry *e)
566 char *sc_reader_id = NULL, *pin;
567 int i, version, success = 0;
569 sc_reader_id = buffer_get_string(&e->request, NULL);
570 pin = buffer_get_string(&e->request, NULL);
571 keys = sc_get_keys(sc_reader_id, pin);
575 if (keys == NULL || keys[0] == NULL) {
576 error("sc_get_keys failed");
579 for (i = 0; keys[i] != NULL; i++) {
581 version = k->type == KEY_RSA1 ? 1 : 2;
582 tab = idtab_lookup(version);
583 if (lookup_identity(k, version) == NULL) {
584 id = xmalloc(sizeof(Identity));
586 id->comment = xstrdup("smartcard key");
588 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
598 buffer_put_int(&e->output, 1);
599 buffer_put_char(&e->output,
600 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
604 process_remove_smartcard_key(SocketEntry *e)
608 Key **keys, *k = NULL;
609 char *sc_reader_id = NULL, *pin;
610 int i, version, success = 0;
612 sc_reader_id = buffer_get_string(&e->request, NULL);
613 pin = buffer_get_string(&e->request, NULL);
614 keys = sc_get_keys(sc_reader_id, pin);
618 if (keys == NULL || keys[0] == NULL) {
619 error("sc_get_keys failed");
622 for (i = 0; keys[i] != NULL; i++) {
624 version = k->type == KEY_RSA1 ? 1 : 2;
625 if ((id = lookup_identity(k, version)) != NULL) {
626 tab = idtab_lookup(version);
627 TAILQ_REMOVE(&tab->idlist, id, next);
637 buffer_put_int(&e->output, 1);
638 buffer_put_char(&e->output,
639 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
641 #endif /* SMARTCARD */
643 /* dispatch incoming messages */
646 process_message(SocketEntry *e)
655 if (buffer_len(&e->input) < 5)
656 return; /* Incomplete message. */
657 cp = buffer_ptr(&e->input);
658 msg_len = GET_32BIT(cp);
659 if (msg_len > 256 * 1024) {
660 shutdown(e->fd, SHUT_RDWR);
662 e->type = AUTH_UNUSED;
663 buffer_free(&e->input);
664 buffer_free(&e->output);
665 buffer_free(&e->request);
668 if (buffer_len(&e->input) < msg_len + 4)
671 /* move the current input to e->request */
672 buffer_consume(&e->input, 4);
673 buffer_clear(&e->request);
674 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
675 buffer_consume(&e->input, msg_len);
676 type = buffer_get_char(&e->request);
678 /* check wheter agent is locked */
679 if (locked && type != SSH_AGENTC_UNLOCK) {
680 buffer_clear(&e->request);
682 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
683 case SSH2_AGENTC_REQUEST_IDENTITIES:
684 /* send empty lists */
685 no_identities(e, type);
688 /* send a fail message for all other request types */
689 buffer_put_int(&e->output, 1);
690 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
695 debug("type %d", type);
697 case SSH_AGENTC_LOCK:
698 case SSH_AGENTC_UNLOCK:
699 process_lock_agent(e, type == SSH_AGENTC_LOCK);
702 case SSH_AGENTC_RSA_CHALLENGE:
703 process_authentication_challenge1(e);
705 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
706 process_request_identities(e, 1);
708 case SSH_AGENTC_ADD_RSA_IDENTITY:
709 process_add_identity(e, 1);
711 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
712 process_remove_identity(e, 1);
714 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
715 process_remove_all_identities(e, 1);
717 case SSH_AGENTC_CONTRAIN_IDENTITY1:
718 process_contrain_identity(e, 1);
721 case SSH2_AGENTC_SIGN_REQUEST:
722 process_sign_request2(e);
724 case SSH2_AGENTC_REQUEST_IDENTITIES:
725 process_request_identities(e, 2);
727 case SSH2_AGENTC_ADD_IDENTITY:
728 process_add_identity(e, 2);
730 case SSH2_AGENTC_REMOVE_IDENTITY:
731 process_remove_identity(e, 2);
733 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
734 process_remove_all_identities(e, 2);
736 case SSH_AGENTC_CONTRAIN_IDENTITY:
737 process_contrain_identity(e, 2);
740 case SSH_AGENTC_ADD_SMARTCARD_KEY:
741 process_add_smartcard_key(e);
743 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
744 process_remove_smartcard_key(e);
746 #endif /* SMARTCARD */
748 /* Unknown message. Respond with failure. */
749 error("Unknown message %d", type);
750 buffer_clear(&e->request);
751 buffer_put_int(&e->output, 1);
752 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
758 new_socket(sock_type type, int fd)
761 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
762 error("fcntl O_NONBLOCK: %s", strerror(errno));
767 for (i = 0; i < sockets_alloc; i++)
768 if (sockets[i].type == AUTH_UNUSED) {
770 sockets[i].type = type;
771 buffer_init(&sockets[i].input);
772 buffer_init(&sockets[i].output);
773 buffer_init(&sockets[i].request);
776 old_alloc = sockets_alloc;
779 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
781 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
782 for (i = old_alloc; i < sockets_alloc; i++)
783 sockets[i].type = AUTH_UNUSED;
784 sockets[old_alloc].type = type;
785 sockets[old_alloc].fd = fd;
786 buffer_init(&sockets[old_alloc].input);
787 buffer_init(&sockets[old_alloc].output);
788 buffer_init(&sockets[old_alloc].request);
792 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
797 for (i = 0; i < sockets_alloc; i++) {
798 switch (sockets[i].type) {
800 case AUTH_CONNECTION:
801 n = MAX(n, sockets[i].fd);
806 fatal("Unknown socket type %d", sockets[i].type);
811 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
812 if (*fdrp == NULL || sz > *nallocp) {
822 debug("XXX shrink: %d < %d", n, *fdl);
824 memset(*fdrp, 0, sz);
825 memset(*fdwp, 0, sz);
827 for (i = 0; i < sockets_alloc; i++) {
828 switch (sockets[i].type) {
830 case AUTH_CONNECTION:
831 FD_SET(sockets[i].fd, *fdrp);
832 if (buffer_len(&sockets[i].output) > 0)
833 FD_SET(sockets[i].fd, *fdwp);
843 after_select(fd_set *readset, fd_set *writeset)
849 struct sockaddr_un sunaddr;
851 for (i = 0; i < sockets_alloc; i++)
852 switch (sockets[i].type) {
856 if (FD_ISSET(sockets[i].fd, readset)) {
857 slen = sizeof(sunaddr);
858 sock = accept(sockets[i].fd,
859 (struct sockaddr *) &sunaddr, &slen);
861 error("accept from AUTH_SOCKET: %s",
865 new_socket(AUTH_CONNECTION, sock);
868 case AUTH_CONNECTION:
869 if (buffer_len(&sockets[i].output) > 0 &&
870 FD_ISSET(sockets[i].fd, writeset)) {
872 len = write(sockets[i].fd,
873 buffer_ptr(&sockets[i].output),
874 buffer_len(&sockets[i].output));
875 if (len == -1 && (errno == EAGAIN ||
881 shutdown(sockets[i].fd, SHUT_RDWR);
882 close(sockets[i].fd);
883 sockets[i].type = AUTH_UNUSED;
884 buffer_free(&sockets[i].input);
885 buffer_free(&sockets[i].output);
886 buffer_free(&sockets[i].request);
889 buffer_consume(&sockets[i].output, len);
891 if (FD_ISSET(sockets[i].fd, readset)) {
893 len = read(sockets[i].fd, buf, sizeof(buf));
894 if (len == -1 && (errno == EAGAIN ||
900 shutdown(sockets[i].fd, SHUT_RDWR);
901 close(sockets[i].fd);
902 sockets[i].type = AUTH_UNUSED;
903 buffer_free(&sockets[i].input);
904 buffer_free(&sockets[i].output);
905 buffer_free(&sockets[i].request);
908 buffer_append(&sockets[i].input, buf, len);
909 process_message(&sockets[i]);
913 fatal("Unknown type %d", sockets[i].type);
918 cleanup_socket(void *p)
929 cleanup_socket(NULL);
934 cleanup_handler(int sig)
936 cleanup_socket(NULL);
941 check_parent_exists(int sig)
943 int save_errno = errno;
945 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
946 /* printf("Parent has died - Authentication agent exiting.\n"); */
947 cleanup_handler(sig); /* safe */
949 signal(SIGALRM, check_parent_exists);
957 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
959 fprintf(stderr, "Options:\n");
960 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
961 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
962 fprintf(stderr, " -k Kill the current agent.\n");
963 fprintf(stderr, " -d Debug mode.\n");
964 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
969 main(int ac, char **av)
971 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
972 struct sockaddr_un sunaddr;
973 #ifdef HAVE_SETRLIMIT
980 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
981 char *agentsocket = NULL;
983 fd_set *readsetp = NULL, *writesetp = NULL;
985 SSLeay_add_all_algorithms();
987 __progname = get_progname(av[0]);
991 #ifdef __GNU_LIBRARY__
992 while ((ch = getopt(ac, av, "+cdksa:")) != -1) {
993 #else /* __GNU_LIBRARY__ */
994 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
995 #endif /* __GNU_LIBRARY__ */
1016 agentsocket = optarg;
1025 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1028 if (ac == 0 && !c_flag && !s_flag) {
1029 shell = getenv("SHELL");
1030 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1034 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1035 if (pidstr == NULL) {
1036 fprintf(stderr, "%s not set, cannot kill agent\n",
1037 SSH_AGENTPID_ENV_NAME);
1042 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1043 SSH_AGENTPID_ENV_NAME, pidstr);
1046 if (kill(pid, SIGTERM) == -1) {
1050 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1051 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1052 printf(format, SSH_AGENTPID_ENV_NAME);
1053 printf("echo Agent pid %ld killed;\n", (long)pid);
1056 parent_pid = getpid();
1058 if (agentsocket == NULL) {
1059 /* Create private directory for agent socket */
1060 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1061 if (mkdtemp(socket_dir) == NULL) {
1062 perror("mkdtemp: private socket dir");
1065 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1068 /* Try to use specified agent socket */
1069 socket_dir[0] = '\0';
1070 strlcpy(socket_name, agentsocket, sizeof socket_name);
1074 * Create socket early so it will exist before command gets run from
1077 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1082 memset(&sunaddr, 0, sizeof(sunaddr));
1083 sunaddr.sun_family = AF_UNIX;
1084 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1086 prev_mask = umask(0177);
1088 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1098 if (listen(sock, 5) < 0) {
1104 * Fork, and have the parent execute the command, if any, or present
1105 * the socket data. The child continues as the authentication agent.
1108 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1109 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1110 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1111 SSH_AUTHSOCKET_ENV_NAME);
1112 printf("echo Agent pid %ld;\n", (long)parent_pid);
1120 if (pid != 0) { /* Parent - execute the given command. */
1122 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1124 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1125 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1126 SSH_AUTHSOCKET_ENV_NAME);
1127 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1128 SSH_AGENTPID_ENV_NAME);
1129 printf("echo Agent pid %ld;\n", (long)pid);
1132 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1133 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1142 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1144 if (setsid() == -1) {
1145 error("setsid: %s", strerror(errno));
1154 #ifdef HAVE_SETRLIMIT
1155 /* deny core dumps, since memory contains unencrypted private keys */
1156 rlim.rlim_cur = rlim.rlim_max = 0;
1157 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1158 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1164 fatal_add_cleanup(cleanup_socket, NULL);
1165 new_socket(AUTH_SOCKET, sock);
1167 signal(SIGALRM, check_parent_exists);
1172 signal(SIGINT, SIG_IGN);
1173 signal(SIGPIPE, SIG_IGN);
1174 signal(SIGHUP, cleanup_handler);
1175 signal(SIGTERM, cleanup_handler);
1179 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1180 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1183 fatal("select: %s", strerror(errno));
1185 after_select(readsetp, writesetp);