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.96 2002/06/23 10:29:52 deraadt 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;
114 for (i = 0; i <=2; i++) {
115 TAILQ_INIT(&idtable[i].idlist);
116 idtable[i].nentries = 0;
120 /* return private key table for requested protocol version */
122 idtab_lookup(int version)
124 if (version < 1 || version > 2)
125 fatal("internal error, bad protocol version %d", version);
126 return &idtable[version];
130 free_identity(Identity *id)
137 /* return matching private key for given public key */
139 lookup_identity(Key *key, int version)
143 Idtab *tab = idtab_lookup(version);
144 TAILQ_FOREACH(id, &tab->idlist, next) {
145 if (key_equal(key, id->key))
151 /* send list of supported public keys to 'client' */
153 process_request_identities(SocketEntry *e, int version)
155 Idtab *tab = idtab_lookup(version);
160 buffer_put_char(&msg, (version == 1) ?
161 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
162 buffer_put_int(&msg, tab->nentries);
163 TAILQ_FOREACH(id, &tab->idlist, next) {
164 if (id->key->type == KEY_RSA1) {
165 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
166 buffer_put_bignum(&msg, id->key->rsa->e);
167 buffer_put_bignum(&msg, id->key->rsa->n);
171 key_to_blob(id->key, &blob, &blen);
172 buffer_put_string(&msg, blob, blen);
175 buffer_put_cstring(&msg, id->comment);
177 buffer_put_int(&e->output, buffer_len(&msg));
178 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
184 process_authentication_challenge1(SocketEntry *e)
186 u_char buf[32], mdbuf[16], session_id[16];
196 key = key_new(KEY_RSA1);
197 if ((challenge = BN_new()) == NULL)
198 fatal("process_authentication_challenge1: BN_new failed");
200 buffer_get_int(&e->request); /* ignored */
201 buffer_get_bignum(&e->request, key->rsa->e);
202 buffer_get_bignum(&e->request, key->rsa->n);
203 buffer_get_bignum(&e->request, challenge);
205 /* Only protocol 1.1 is supported */
206 if (buffer_len(&e->request) == 0)
208 buffer_get(&e->request, session_id, 16);
209 response_type = buffer_get_int(&e->request);
210 if (response_type != 1)
213 id = lookup_identity(key, 1);
215 Key *private = id->key;
216 /* Decrypt the challenge using the private key. */
217 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
220 /* The response is MD5 of decrypted challenge plus session id. */
221 len = BN_num_bytes(challenge);
222 if (len <= 0 || len > 32) {
223 log("process_authentication_challenge: bad challenge length %d", len);
227 BN_bn2bin(challenge, buf + 32 - len);
229 MD5_Update(&md, buf, 32);
230 MD5_Update(&md, session_id, 16);
231 MD5_Final(mdbuf, &md);
233 /* Send the response. */
234 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
235 for (i = 0; i < 16; i++)
236 buffer_put_char(&msg, mdbuf[i]);
241 /* Unknown identity or protocol error. Send failure. */
242 buffer_put_char(&msg, SSH_AGENT_FAILURE);
244 buffer_put_int(&e->output, buffer_len(&msg));
245 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
247 BN_clear_free(challenge);
253 process_sign_request2(SocketEntry *e)
255 u_char *blob, *data, *signature = NULL;
256 u_int blen, dlen, slen = 0;
257 extern int datafellows;
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)
306 key = key_new(KEY_RSA1);
307 bits = buffer_get_int(&e->request);
308 buffer_get_bignum(&e->request, key->rsa->e);
309 buffer_get_bignum(&e->request, key->rsa->n);
311 if (bits != key_size(key))
312 log("Warning: identity keysize mismatch: actual %u, announced %u",
313 key_size(key), bits);
316 blob = buffer_get_string(&e->request, &blen);
317 key = key_from_blob(blob, blen);
322 Identity *id = lookup_identity(key, version);
325 * We have this key. Free the old key. Since we
326 * don\'t want to leave empty slots in the middle of
327 * the array, we actually free the key there and move
328 * all the entries between the empty slot and the end
331 Idtab *tab = idtab_lookup(version);
332 if (tab->nentries < 1)
333 fatal("process_remove_identity: "
334 "internal error: tab->nentries %d",
336 TAILQ_REMOVE(&tab->idlist, id, next);
343 buffer_put_int(&e->output, 1);
344 buffer_put_char(&e->output,
345 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
349 process_remove_all_identities(SocketEntry *e, int version)
351 Idtab *tab = idtab_lookup(version);
354 /* Loop over all identities and clear the keys. */
355 for (id = TAILQ_FIRST(&tab->idlist); id;
356 id = TAILQ_FIRST(&tab->idlist)) {
357 TAILQ_REMOVE(&tab->idlist, id, next);
361 /* Mark that there are no identities. */
365 buffer_put_int(&e->output, 1);
366 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
372 u_int now = time(NULL);
377 for (version = 1; version < 3; version++) {
378 tab = idtab_lookup(version);
379 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
380 nxt = TAILQ_NEXT(id, next);
381 if (id->death != 0 && now >= id->death) {
382 TAILQ_REMOVE(&tab->idlist, id, next);
391 process_add_identity(SocketEntry *e, int version)
393 Idtab *tab = idtab_lookup(version);
394 int type, success = 0, death = 0;
395 char *type_name, *comment;
400 k = key_new_private(KEY_RSA1);
401 buffer_get_int(&e->request); /* ignored */
402 buffer_get_bignum(&e->request, k->rsa->n);
403 buffer_get_bignum(&e->request, k->rsa->e);
404 buffer_get_bignum(&e->request, k->rsa->d);
405 buffer_get_bignum(&e->request, k->rsa->iqmp);
407 /* SSH and SSL have p and q swapped */
408 buffer_get_bignum(&e->request, k->rsa->q); /* p */
409 buffer_get_bignum(&e->request, k->rsa->p); /* q */
411 /* Generate additional parameters */
412 rsa_generate_additional_parameters(k->rsa);
415 type_name = buffer_get_string(&e->request, NULL);
416 type = key_type_from_name(type_name);
420 k = key_new_private(type);
421 buffer_get_bignum2(&e->request, k->dsa->p);
422 buffer_get_bignum2(&e->request, k->dsa->q);
423 buffer_get_bignum2(&e->request, k->dsa->g);
424 buffer_get_bignum2(&e->request, k->dsa->pub_key);
425 buffer_get_bignum2(&e->request, k->dsa->priv_key);
428 k = key_new_private(type);
429 buffer_get_bignum2(&e->request, k->rsa->n);
430 buffer_get_bignum2(&e->request, k->rsa->e);
431 buffer_get_bignum2(&e->request, k->rsa->d);
432 buffer_get_bignum2(&e->request, k->rsa->iqmp);
433 buffer_get_bignum2(&e->request, k->rsa->p);
434 buffer_get_bignum2(&e->request, k->rsa->q);
436 /* Generate additional parameters */
437 rsa_generate_additional_parameters(k->rsa);
440 buffer_clear(&e->request);
445 comment = buffer_get_string(&e->request, NULL);
451 while (buffer_len(&e->request)) {
452 switch (buffer_get_char(&e->request)) {
453 case SSH_AGENT_CONSTRAIN_LIFETIME:
454 death = time(NULL) + buffer_get_int(&e->request);
460 if (lookup_identity(k, version) == NULL) {
461 Identity *id = xmalloc(sizeof(Identity));
463 id->comment = comment;
465 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
466 /* Increment the number of identities. */
473 buffer_put_int(&e->output, 1);
474 buffer_put_char(&e->output,
475 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
478 /* XXX todo: encrypt sensitive data with passphrase */
480 process_lock_agent(SocketEntry *e, int lock)
485 passwd = buffer_get_string(&e->request, NULL);
486 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
488 memset(lock_passwd, 0, strlen(lock_passwd));
492 } else if (!locked && lock) {
494 lock_passwd = xstrdup(passwd);
497 memset(passwd, 0, strlen(passwd));
500 buffer_put_int(&e->output, 1);
501 buffer_put_char(&e->output,
502 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
506 no_identities(SocketEntry *e, u_int type)
511 buffer_put_char(&msg,
512 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
513 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
514 buffer_put_int(&msg, 0);
515 buffer_put_int(&e->output, buffer_len(&msg));
516 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
522 process_add_smartcard_key (SocketEntry *e)
524 char *sc_reader_id = NULL, *pin;
525 int i, version, success = 0;
530 sc_reader_id = buffer_get_string(&e->request, NULL);
531 pin = buffer_get_string(&e->request, NULL);
532 keys = sc_get_keys(sc_reader_id, pin);
536 if (keys == NULL || keys[0] == NULL) {
537 error("sc_get_keys failed");
540 for (i = 0; keys[i] != NULL; i++) {
542 version = k->type == KEY_RSA1 ? 1 : 2;
543 tab = idtab_lookup(version);
544 if (lookup_identity(k, version) == NULL) {
545 id = xmalloc(sizeof(Identity));
547 id->comment = xstrdup("smartcard key");
549 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
559 buffer_put_int(&e->output, 1);
560 buffer_put_char(&e->output,
561 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
565 process_remove_smartcard_key(SocketEntry *e)
567 char *sc_reader_id = NULL, *pin;
568 int i, version, success = 0;
569 Key **keys, *k = NULL;
573 sc_reader_id = buffer_get_string(&e->request, NULL);
574 pin = buffer_get_string(&e->request, NULL);
575 keys = sc_get_keys(sc_reader_id, pin);
579 if (keys == NULL || keys[0] == NULL) {
580 error("sc_get_keys failed");
583 for (i = 0; keys[i] != NULL; i++) {
585 version = k->type == KEY_RSA1 ? 1 : 2;
586 if ((id = lookup_identity(k, version)) != NULL) {
587 tab = idtab_lookup(version);
588 TAILQ_REMOVE(&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);
602 #endif /* SMARTCARD */
604 /* dispatch incoming messages */
607 process_message(SocketEntry *e)
615 if (buffer_len(&e->input) < 5)
616 return; /* Incomplete message. */
617 cp = buffer_ptr(&e->input);
618 msg_len = GET_32BIT(cp);
619 if (msg_len > 256 * 1024) {
620 shutdown(e->fd, SHUT_RDWR);
623 e->type = AUTH_UNUSED;
624 buffer_free(&e->input);
625 buffer_free(&e->output);
626 buffer_free(&e->request);
629 if (buffer_len(&e->input) < msg_len + 4)
632 /* move the current input to e->request */
633 buffer_consume(&e->input, 4);
634 buffer_clear(&e->request);
635 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
636 buffer_consume(&e->input, msg_len);
637 type = buffer_get_char(&e->request);
639 /* check wheter agent is locked */
640 if (locked && type != SSH_AGENTC_UNLOCK) {
641 buffer_clear(&e->request);
643 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
644 case SSH2_AGENTC_REQUEST_IDENTITIES:
645 /* send empty lists */
646 no_identities(e, type);
649 /* send a fail message for all other request types */
650 buffer_put_int(&e->output, 1);
651 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
656 debug("type %d", type);
658 case SSH_AGENTC_LOCK:
659 case SSH_AGENTC_UNLOCK:
660 process_lock_agent(e, type == SSH_AGENTC_LOCK);
663 case SSH_AGENTC_RSA_CHALLENGE:
664 process_authentication_challenge1(e);
666 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
667 process_request_identities(e, 1);
669 case SSH_AGENTC_ADD_RSA_IDENTITY:
670 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
671 process_add_identity(e, 1);
673 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
674 process_remove_identity(e, 1);
676 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
677 process_remove_all_identities(e, 1);
680 case SSH2_AGENTC_SIGN_REQUEST:
681 process_sign_request2(e);
683 case SSH2_AGENTC_REQUEST_IDENTITIES:
684 process_request_identities(e, 2);
686 case SSH2_AGENTC_ADD_IDENTITY:
687 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
688 process_add_identity(e, 2);
690 case SSH2_AGENTC_REMOVE_IDENTITY:
691 process_remove_identity(e, 2);
693 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
694 process_remove_all_identities(e, 2);
697 case SSH_AGENTC_ADD_SMARTCARD_KEY:
698 process_add_smartcard_key(e);
700 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
701 process_remove_smartcard_key(e);
703 #endif /* SMARTCARD */
705 /* Unknown message. Respond with failure. */
706 error("Unknown message %d", type);
707 buffer_clear(&e->request);
708 buffer_put_int(&e->output, 1);
709 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
715 new_socket(sock_type type, int fd)
719 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
720 error("fcntl O_NONBLOCK: %s", strerror(errno));
725 for (i = 0; i < sockets_alloc; i++)
726 if (sockets[i].type == AUTH_UNUSED) {
728 sockets[i].type = type;
729 buffer_init(&sockets[i].input);
730 buffer_init(&sockets[i].output);
731 buffer_init(&sockets[i].request);
734 old_alloc = sockets_alloc;
737 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
739 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
740 for (i = old_alloc; i < sockets_alloc; i++)
741 sockets[i].type = AUTH_UNUSED;
742 sockets[old_alloc].type = type;
743 sockets[old_alloc].fd = fd;
744 buffer_init(&sockets[old_alloc].input);
745 buffer_init(&sockets[old_alloc].output);
746 buffer_init(&sockets[old_alloc].request);
750 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
755 for (i = 0; i < sockets_alloc; i++) {
756 switch (sockets[i].type) {
758 case AUTH_CONNECTION:
759 n = MAX(n, sockets[i].fd);
764 fatal("Unknown socket type %d", sockets[i].type);
769 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
770 if (*fdrp == NULL || sz > *nallocp) {
780 debug("XXX shrink: %d < %d", n, *fdl);
782 memset(*fdrp, 0, sz);
783 memset(*fdwp, 0, sz);
785 for (i = 0; i < sockets_alloc; i++) {
786 switch (sockets[i].type) {
788 case AUTH_CONNECTION:
789 FD_SET(sockets[i].fd, *fdrp);
790 if (buffer_len(&sockets[i].output) > 0)
791 FD_SET(sockets[i].fd, *fdwp);
801 after_select(fd_set *readset, fd_set *writeset)
803 struct sockaddr_un sunaddr;
809 for (i = 0; i < sockets_alloc; i++)
810 switch (sockets[i].type) {
814 if (FD_ISSET(sockets[i].fd, readset)) {
815 slen = sizeof(sunaddr);
816 sock = accept(sockets[i].fd,
817 (struct sockaddr *) &sunaddr, &slen);
819 error("accept from AUTH_SOCKET: %s",
823 new_socket(AUTH_CONNECTION, sock);
826 case AUTH_CONNECTION:
827 if (buffer_len(&sockets[i].output) > 0 &&
828 FD_ISSET(sockets[i].fd, writeset)) {
830 len = write(sockets[i].fd,
831 buffer_ptr(&sockets[i].output),
832 buffer_len(&sockets[i].output));
833 if (len == -1 && (errno == EAGAIN ||
839 shutdown(sockets[i].fd, SHUT_RDWR);
840 close(sockets[i].fd);
842 sockets[i].type = AUTH_UNUSED;
843 buffer_free(&sockets[i].input);
844 buffer_free(&sockets[i].output);
845 buffer_free(&sockets[i].request);
848 buffer_consume(&sockets[i].output, len);
850 if (FD_ISSET(sockets[i].fd, readset)) {
852 len = read(sockets[i].fd, buf, sizeof(buf));
853 if (len == -1 && (errno == EAGAIN ||
859 shutdown(sockets[i].fd, SHUT_RDWR);
860 close(sockets[i].fd);
862 sockets[i].type = AUTH_UNUSED;
863 buffer_free(&sockets[i].input);
864 buffer_free(&sockets[i].output);
865 buffer_free(&sockets[i].request);
868 buffer_append(&sockets[i].input, buf, len);
869 process_message(&sockets[i]);
873 fatal("Unknown type %d", sockets[i].type);
878 cleanup_socket(void *p)
889 cleanup_socket(NULL);
894 cleanup_handler(int sig)
896 cleanup_socket(NULL);
901 check_parent_exists(int sig)
903 int save_errno = errno;
905 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
906 /* printf("Parent has died - Authentication agent exiting.\n"); */
907 cleanup_handler(sig); /* safe */
909 signal(SIGALRM, check_parent_exists);
917 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
919 fprintf(stderr, "Options:\n");
920 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
921 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
922 fprintf(stderr, " -k Kill the current agent.\n");
923 fprintf(stderr, " -d Debug mode.\n");
924 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
929 main(int ac, char **av)
931 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
932 char *shell, *format, *pidstr, *agentsocket = NULL;
933 fd_set *readsetp = NULL, *writesetp = NULL;
934 struct sockaddr_un sunaddr;
935 #ifdef HAVE_SETRLIMIT
944 char pidstrbuf[1 + 3 * sizeof pid];
946 SSLeay_add_all_algorithms();
948 __progname = get_progname(av[0]);
952 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
973 agentsocket = optarg;
982 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
985 if (ac == 0 && !c_flag && !s_flag) {
986 shell = getenv("SHELL");
987 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
991 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
992 if (pidstr == NULL) {
993 fprintf(stderr, "%s not set, cannot kill agent\n",
994 SSH_AGENTPID_ENV_NAME);
999 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1000 SSH_AGENTPID_ENV_NAME, pidstr);
1003 if (kill(pid, SIGTERM) == -1) {
1007 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1008 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1009 printf(format, SSH_AGENTPID_ENV_NAME);
1010 printf("echo Agent pid %ld killed;\n", (long)pid);
1013 parent_pid = getpid();
1015 if (agentsocket == NULL) {
1016 /* Create private directory for agent socket */
1017 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1018 if (mkdtemp(socket_dir) == NULL) {
1019 perror("mkdtemp: private socket dir");
1022 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1025 /* Try to use specified agent socket */
1026 socket_dir[0] = '\0';
1027 strlcpy(socket_name, agentsocket, sizeof socket_name);
1031 * Create socket early so it will exist before command gets run from
1034 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1039 memset(&sunaddr, 0, sizeof(sunaddr));
1040 sunaddr.sun_family = AF_UNIX;
1041 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1043 prev_mask = umask(0177);
1045 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1055 if (listen(sock, 5) < 0) {
1061 * Fork, and have the parent execute the command, if any, or present
1062 * the socket data. The child continues as the authentication agent.
1065 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1066 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1067 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1068 SSH_AUTHSOCKET_ENV_NAME);
1069 printf("echo Agent pid %ld;\n", (long)parent_pid);
1077 if (pid != 0) { /* Parent - execute the given command. */
1079 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
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);
1084 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1085 SSH_AGENTPID_ENV_NAME);
1086 printf("echo Agent pid %ld;\n", (long)pid);
1089 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1090 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1099 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1101 if (setsid() == -1) {
1102 error("setsid: %s", strerror(errno));
1111 #ifdef HAVE_SETRLIMIT
1112 /* deny core dumps, since memory contains unencrypted private keys */
1113 rlim.rlim_cur = rlim.rlim_max = 0;
1114 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1115 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1121 fatal_add_cleanup(cleanup_socket, NULL);
1122 new_socket(AUTH_SOCKET, sock);
1124 signal(SIGALRM, check_parent_exists);
1129 signal(SIGINT, SIG_IGN);
1130 signal(SIGPIPE, SIG_IGN);
1131 signal(SIGHUP, cleanup_handler);
1132 signal(SIGTERM, cleanup_handler);
1136 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1137 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1140 fatal("select: %s", strerror(errno));
1142 after_select(readsetp, writesetp);