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.106 2003/01/21 18:14:36 marc 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;
109 /* Default lifetime (0 == forever) */
110 static int lifetime = 0;
113 close_socket(SocketEntry *e)
117 e->type = AUTH_UNUSED;
118 buffer_free(&e->input);
119 buffer_free(&e->output);
120 buffer_free(&e->request);
128 for (i = 0; i <=2; i++) {
129 TAILQ_INIT(&idtable[i].idlist);
130 idtable[i].nentries = 0;
134 /* return private key table for requested protocol version */
136 idtab_lookup(int version)
138 if (version < 1 || version > 2)
139 fatal("internal error, bad protocol version %d", version);
140 return &idtable[version];
144 free_identity(Identity *id)
151 /* return matching private key for given public key */
153 lookup_identity(Key *key, int version)
157 Idtab *tab = idtab_lookup(version);
158 TAILQ_FOREACH(id, &tab->idlist, next) {
159 if (key_equal(key, id->key))
165 /* send list of supported public keys to 'client' */
167 process_request_identities(SocketEntry *e, int version)
169 Idtab *tab = idtab_lookup(version);
174 buffer_put_char(&msg, (version == 1) ?
175 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
176 buffer_put_int(&msg, tab->nentries);
177 TAILQ_FOREACH(id, &tab->idlist, next) {
178 if (id->key->type == KEY_RSA1) {
179 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
180 buffer_put_bignum(&msg, id->key->rsa->e);
181 buffer_put_bignum(&msg, id->key->rsa->n);
185 key_to_blob(id->key, &blob, &blen);
186 buffer_put_string(&msg, blob, blen);
189 buffer_put_cstring(&msg, id->comment);
191 buffer_put_int(&e->output, buffer_len(&msg));
192 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
198 process_authentication_challenge1(SocketEntry *e)
200 u_char buf[32], mdbuf[16], session_id[16];
210 key = key_new(KEY_RSA1);
211 if ((challenge = BN_new()) == NULL)
212 fatal("process_authentication_challenge1: BN_new failed");
214 (void) buffer_get_int(&e->request); /* ignored */
215 buffer_get_bignum(&e->request, key->rsa->e);
216 buffer_get_bignum(&e->request, key->rsa->n);
217 buffer_get_bignum(&e->request, challenge);
219 /* Only protocol 1.1 is supported */
220 if (buffer_len(&e->request) == 0)
222 buffer_get(&e->request, session_id, 16);
223 response_type = buffer_get_int(&e->request);
224 if (response_type != 1)
227 id = lookup_identity(key, 1);
229 Key *private = id->key;
230 /* Decrypt the challenge using the private key. */
231 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
234 /* The response is MD5 of decrypted challenge plus session id. */
235 len = BN_num_bytes(challenge);
236 if (len <= 0 || len > 32) {
237 log("process_authentication_challenge: bad challenge length %d", len);
241 BN_bn2bin(challenge, buf + 32 - len);
243 MD5_Update(&md, buf, 32);
244 MD5_Update(&md, session_id, 16);
245 MD5_Final(mdbuf, &md);
247 /* Send the response. */
248 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
249 for (i = 0; i < 16; i++)
250 buffer_put_char(&msg, mdbuf[i]);
255 /* Unknown identity or protocol error. Send failure. */
256 buffer_put_char(&msg, SSH_AGENT_FAILURE);
258 buffer_put_int(&e->output, buffer_len(&msg));
259 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
261 BN_clear_free(challenge);
267 process_sign_request2(SocketEntry *e)
269 u_char *blob, *data, *signature = NULL;
270 u_int blen, dlen, slen = 0;
271 extern int datafellows;
278 blob = buffer_get_string(&e->request, &blen);
279 data = buffer_get_string(&e->request, &dlen);
281 flags = buffer_get_int(&e->request);
282 if (flags & SSH_AGENT_OLD_SIGNATURE)
283 datafellows = SSH_BUG_SIGBLOB;
285 key = key_from_blob(blob, blen);
287 Identity *id = lookup_identity(key, 2);
289 ok = key_sign(id->key, &signature, &slen, data, dlen);
294 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
295 buffer_put_string(&msg, signature, slen);
297 buffer_put_char(&msg, SSH_AGENT_FAILURE);
299 buffer_put_int(&e->output, buffer_len(&msg));
300 buffer_append(&e->output, buffer_ptr(&msg),
305 if (signature != NULL)
311 process_remove_identity(SocketEntry *e, int version)
320 key = key_new(KEY_RSA1);
321 bits = buffer_get_int(&e->request);
322 buffer_get_bignum(&e->request, key->rsa->e);
323 buffer_get_bignum(&e->request, key->rsa->n);
325 if (bits != key_size(key))
326 log("Warning: identity keysize mismatch: actual %u, announced %u",
327 key_size(key), bits);
330 blob = buffer_get_string(&e->request, &blen);
331 key = key_from_blob(blob, blen);
336 Identity *id = lookup_identity(key, version);
339 * We have this key. Free the old key. Since we
340 * don\'t want to leave empty slots in the middle of
341 * the array, we actually free the key there and move
342 * all the entries between the empty slot and the end
345 Idtab *tab = idtab_lookup(version);
346 if (tab->nentries < 1)
347 fatal("process_remove_identity: "
348 "internal error: tab->nentries %d",
350 TAILQ_REMOVE(&tab->idlist, id, next);
357 buffer_put_int(&e->output, 1);
358 buffer_put_char(&e->output,
359 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
363 process_remove_all_identities(SocketEntry *e, int version)
365 Idtab *tab = idtab_lookup(version);
368 /* Loop over all identities and clear the keys. */
369 for (id = TAILQ_FIRST(&tab->idlist); id;
370 id = TAILQ_FIRST(&tab->idlist)) {
371 TAILQ_REMOVE(&tab->idlist, id, next);
375 /* Mark that there are no identities. */
379 buffer_put_int(&e->output, 1);
380 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
386 u_int now = time(NULL);
391 for (version = 1; version < 3; version++) {
392 tab = idtab_lookup(version);
393 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
394 nxt = TAILQ_NEXT(id, next);
395 if (id->death != 0 && now >= id->death) {
396 TAILQ_REMOVE(&tab->idlist, id, next);
405 process_add_identity(SocketEntry *e, int version)
407 Idtab *tab = idtab_lookup(version);
408 int type, success = 0, death = 0;
409 char *type_name, *comment;
414 k = key_new_private(KEY_RSA1);
415 (void) buffer_get_int(&e->request); /* ignored */
416 buffer_get_bignum(&e->request, k->rsa->n);
417 buffer_get_bignum(&e->request, k->rsa->e);
418 buffer_get_bignum(&e->request, k->rsa->d);
419 buffer_get_bignum(&e->request, k->rsa->iqmp);
421 /* SSH and SSL have p and q swapped */
422 buffer_get_bignum(&e->request, k->rsa->q); /* p */
423 buffer_get_bignum(&e->request, k->rsa->p); /* q */
425 /* Generate additional parameters */
426 rsa_generate_additional_parameters(k->rsa);
429 type_name = buffer_get_string(&e->request, NULL);
430 type = key_type_from_name(type_name);
434 k = key_new_private(type);
435 buffer_get_bignum2(&e->request, k->dsa->p);
436 buffer_get_bignum2(&e->request, k->dsa->q);
437 buffer_get_bignum2(&e->request, k->dsa->g);
438 buffer_get_bignum2(&e->request, k->dsa->pub_key);
439 buffer_get_bignum2(&e->request, k->dsa->priv_key);
442 k = key_new_private(type);
443 buffer_get_bignum2(&e->request, k->rsa->n);
444 buffer_get_bignum2(&e->request, k->rsa->e);
445 buffer_get_bignum2(&e->request, k->rsa->d);
446 buffer_get_bignum2(&e->request, k->rsa->iqmp);
447 buffer_get_bignum2(&e->request, k->rsa->p);
448 buffer_get_bignum2(&e->request, k->rsa->q);
450 /* Generate additional parameters */
451 rsa_generate_additional_parameters(k->rsa);
454 buffer_clear(&e->request);
459 comment = buffer_get_string(&e->request, NULL);
465 while (buffer_len(&e->request)) {
466 switch (buffer_get_char(&e->request)) {
467 case SSH_AGENT_CONSTRAIN_LIFETIME:
468 death = time(NULL) + buffer_get_int(&e->request);
474 if (lifetime && !death)
475 death = time(NULL) + lifetime;
476 if (lookup_identity(k, version) == NULL) {
477 Identity *id = xmalloc(sizeof(Identity));
479 id->comment = comment;
481 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
482 /* Increment the number of identities. */
489 buffer_put_int(&e->output, 1);
490 buffer_put_char(&e->output,
491 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
494 /* XXX todo: encrypt sensitive data with passphrase */
496 process_lock_agent(SocketEntry *e, int lock)
501 passwd = buffer_get_string(&e->request, NULL);
502 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
504 memset(lock_passwd, 0, strlen(lock_passwd));
508 } else if (!locked && lock) {
510 lock_passwd = xstrdup(passwd);
513 memset(passwd, 0, strlen(passwd));
516 buffer_put_int(&e->output, 1);
517 buffer_put_char(&e->output,
518 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
522 no_identities(SocketEntry *e, u_int type)
527 buffer_put_char(&msg,
528 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
529 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
530 buffer_put_int(&msg, 0);
531 buffer_put_int(&e->output, buffer_len(&msg));
532 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
538 process_add_smartcard_key (SocketEntry *e)
540 char *sc_reader_id = NULL, *pin;
541 int i, version, success = 0;
546 sc_reader_id = buffer_get_string(&e->request, NULL);
547 pin = buffer_get_string(&e->request, NULL);
548 keys = sc_get_keys(sc_reader_id, pin);
552 if (keys == NULL || keys[0] == NULL) {
553 error("sc_get_keys failed");
556 for (i = 0; keys[i] != NULL; i++) {
558 version = k->type == KEY_RSA1 ? 1 : 2;
559 tab = idtab_lookup(version);
560 if (lookup_identity(k, version) == NULL) {
561 id = xmalloc(sizeof(Identity));
563 id->comment = xstrdup("smartcard key");
565 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
575 buffer_put_int(&e->output, 1);
576 buffer_put_char(&e->output,
577 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
581 process_remove_smartcard_key(SocketEntry *e)
583 char *sc_reader_id = NULL, *pin;
584 int i, version, success = 0;
585 Key **keys, *k = NULL;
589 sc_reader_id = buffer_get_string(&e->request, NULL);
590 pin = buffer_get_string(&e->request, NULL);
591 keys = sc_get_keys(sc_reader_id, pin);
595 if (keys == NULL || keys[0] == NULL) {
596 error("sc_get_keys failed");
599 for (i = 0; keys[i] != NULL; i++) {
601 version = k->type == KEY_RSA1 ? 1 : 2;
602 if ((id = lookup_identity(k, version)) != NULL) {
603 tab = idtab_lookup(version);
604 TAILQ_REMOVE(&tab->idlist, id, next);
614 buffer_put_int(&e->output, 1);
615 buffer_put_char(&e->output,
616 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
618 #endif /* SMARTCARD */
620 /* dispatch incoming messages */
623 process_message(SocketEntry *e)
631 if (buffer_len(&e->input) < 5)
632 return; /* Incomplete message. */
633 cp = buffer_ptr(&e->input);
634 msg_len = GET_32BIT(cp);
635 if (msg_len > 256 * 1024) {
639 if (buffer_len(&e->input) < msg_len + 4)
642 /* move the current input to e->request */
643 buffer_consume(&e->input, 4);
644 buffer_clear(&e->request);
645 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
646 buffer_consume(&e->input, msg_len);
647 type = buffer_get_char(&e->request);
649 /* check wheter agent is locked */
650 if (locked && type != SSH_AGENTC_UNLOCK) {
651 buffer_clear(&e->request);
653 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
654 case SSH2_AGENTC_REQUEST_IDENTITIES:
655 /* send empty lists */
656 no_identities(e, type);
659 /* send a fail message for all other request types */
660 buffer_put_int(&e->output, 1);
661 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
666 debug("type %d", type);
668 case SSH_AGENTC_LOCK:
669 case SSH_AGENTC_UNLOCK:
670 process_lock_agent(e, type == SSH_AGENTC_LOCK);
673 case SSH_AGENTC_RSA_CHALLENGE:
674 process_authentication_challenge1(e);
676 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
677 process_request_identities(e, 1);
679 case SSH_AGENTC_ADD_RSA_IDENTITY:
680 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
681 process_add_identity(e, 1);
683 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
684 process_remove_identity(e, 1);
686 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
687 process_remove_all_identities(e, 1);
690 case SSH2_AGENTC_SIGN_REQUEST:
691 process_sign_request2(e);
693 case SSH2_AGENTC_REQUEST_IDENTITIES:
694 process_request_identities(e, 2);
696 case SSH2_AGENTC_ADD_IDENTITY:
697 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
698 process_add_identity(e, 2);
700 case SSH2_AGENTC_REMOVE_IDENTITY:
701 process_remove_identity(e, 2);
703 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
704 process_remove_all_identities(e, 2);
707 case SSH_AGENTC_ADD_SMARTCARD_KEY:
708 process_add_smartcard_key(e);
710 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
711 process_remove_smartcard_key(e);
713 #endif /* SMARTCARD */
715 /* Unknown message. Respond with failure. */
716 error("Unknown message %d", type);
717 buffer_clear(&e->request);
718 buffer_put_int(&e->output, 1);
719 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
725 new_socket(sock_type type, int fd)
729 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
730 error("fcntl O_NONBLOCK: %s", strerror(errno));
735 for (i = 0; i < sockets_alloc; i++)
736 if (sockets[i].type == AUTH_UNUSED) {
738 sockets[i].type = type;
739 buffer_init(&sockets[i].input);
740 buffer_init(&sockets[i].output);
741 buffer_init(&sockets[i].request);
744 old_alloc = sockets_alloc;
747 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
749 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
750 for (i = old_alloc; i < sockets_alloc; i++)
751 sockets[i].type = AUTH_UNUSED;
752 sockets[old_alloc].type = type;
753 sockets[old_alloc].fd = fd;
754 buffer_init(&sockets[old_alloc].input);
755 buffer_init(&sockets[old_alloc].output);
756 buffer_init(&sockets[old_alloc].request);
760 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
765 for (i = 0; i < sockets_alloc; i++) {
766 switch (sockets[i].type) {
768 case AUTH_CONNECTION:
769 n = MAX(n, sockets[i].fd);
774 fatal("Unknown socket type %d", sockets[i].type);
779 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
780 if (*fdrp == NULL || sz > *nallocp) {
790 debug("XXX shrink: %d < %d", n, *fdl);
792 memset(*fdrp, 0, sz);
793 memset(*fdwp, 0, sz);
795 for (i = 0; i < sockets_alloc; i++) {
796 switch (sockets[i].type) {
798 case AUTH_CONNECTION:
799 FD_SET(sockets[i].fd, *fdrp);
800 if (buffer_len(&sockets[i].output) > 0)
801 FD_SET(sockets[i].fd, *fdwp);
811 after_select(fd_set *readset, fd_set *writeset)
813 struct sockaddr_un sunaddr;
821 for (i = 0; i < sockets_alloc; i++)
822 switch (sockets[i].type) {
826 if (FD_ISSET(sockets[i].fd, readset)) {
827 slen = sizeof(sunaddr);
828 sock = accept(sockets[i].fd,
829 (struct sockaddr *) &sunaddr, &slen);
831 error("accept from AUTH_SOCKET: %s",
835 if (getpeereid(sock, &euid, &egid) < 0) {
836 error("getpeereid %d failed: %s",
837 sock, strerror(errno));
841 if ((euid != 0) && (getuid() != euid)) {
842 error("uid mismatch: "
843 "peer euid %u != uid %u",
844 (u_int) euid, (u_int) getuid());
848 new_socket(AUTH_CONNECTION, sock);
851 case AUTH_CONNECTION:
852 if (buffer_len(&sockets[i].output) > 0 &&
853 FD_ISSET(sockets[i].fd, writeset)) {
855 len = write(sockets[i].fd,
856 buffer_ptr(&sockets[i].output),
857 buffer_len(&sockets[i].output));
858 if (len == -1 && (errno == EAGAIN ||
864 close_socket(&sockets[i]);
867 buffer_consume(&sockets[i].output, len);
869 if (FD_ISSET(sockets[i].fd, readset)) {
871 len = read(sockets[i].fd, buf, sizeof(buf));
872 if (len == -1 && (errno == EAGAIN ||
878 close_socket(&sockets[i]);
881 buffer_append(&sockets[i].input, buf, len);
882 process_message(&sockets[i]);
886 fatal("Unknown type %d", sockets[i].type);
891 cleanup_socket(void *p)
902 cleanup_socket(NULL);
907 cleanup_handler(int sig)
909 cleanup_socket(NULL);
914 check_parent_exists(int sig)
916 int save_errno = errno;
918 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
919 /* printf("Parent has died - Authentication agent exiting.\n"); */
920 cleanup_handler(sig); /* safe */
922 signal(SIGALRM, check_parent_exists);
930 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
932 fprintf(stderr, "Options:\n");
933 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
934 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
935 fprintf(stderr, " -k Kill the current agent.\n");
936 fprintf(stderr, " -d Debug mode.\n");
937 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
938 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
943 main(int ac, char **av)
945 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
946 char *shell, *format, *pidstr, *agentsocket = NULL;
947 fd_set *readsetp = NULL, *writesetp = NULL;
948 struct sockaddr_un sunaddr;
949 #ifdef HAVE_SETRLIMIT
958 char pidstrbuf[1 + 3 * sizeof pid];
964 SSLeay_add_all_algorithms();
966 __progname = get_progname(av[0]);
970 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
991 agentsocket = optarg;
994 if ((lifetime = convtime(optarg)) == -1) {
995 fprintf(stderr, "Invalid lifetime\n");
1006 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1009 if (ac == 0 && !c_flag && !s_flag) {
1010 shell = getenv("SHELL");
1011 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1015 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1016 if (pidstr == NULL) {
1017 fprintf(stderr, "%s not set, cannot kill agent\n",
1018 SSH_AGENTPID_ENV_NAME);
1023 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1024 SSH_AGENTPID_ENV_NAME, pidstr);
1027 if (kill(pid, SIGTERM) == -1) {
1031 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1032 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1033 printf(format, SSH_AGENTPID_ENV_NAME);
1034 printf("echo Agent pid %ld killed;\n", (long)pid);
1037 parent_pid = getpid();
1039 if (agentsocket == NULL) {
1040 /* Create private directory for agent socket */
1041 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1042 if (mkdtemp(socket_dir) == NULL) {
1043 perror("mkdtemp: private socket dir");
1046 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1049 /* Try to use specified agent socket */
1050 socket_dir[0] = '\0';
1051 strlcpy(socket_name, agentsocket, sizeof socket_name);
1055 * Create socket early so it will exist before command gets run from
1058 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1063 memset(&sunaddr, 0, sizeof(sunaddr));
1064 sunaddr.sun_family = AF_UNIX;
1065 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1067 prev_mask = umask(0177);
1069 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1079 if (listen(sock, 128) < 0) {
1085 * Fork, and have the parent execute the command, if any, or present
1086 * the socket data. The child continues as the authentication agent.
1089 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1090 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1091 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1092 SSH_AUTHSOCKET_ENV_NAME);
1093 printf("echo Agent pid %ld;\n", (long)parent_pid);
1101 if (pid != 0) { /* Parent - execute the given command. */
1103 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1105 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1106 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1107 SSH_AUTHSOCKET_ENV_NAME);
1108 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1109 SSH_AGENTPID_ENV_NAME);
1110 printf("echo Agent pid %ld;\n", (long)pid);
1113 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1114 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1123 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1125 if (setsid() == -1) {
1126 error("setsid: %s", strerror(errno));
1135 #ifdef HAVE_SETRLIMIT
1136 /* deny core dumps, since memory contains unencrypted private keys */
1137 rlim.rlim_cur = rlim.rlim_max = 0;
1138 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1139 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1145 fatal_add_cleanup(cleanup_socket, NULL);
1146 new_socket(AUTH_SOCKET, sock);
1148 signal(SIGALRM, check_parent_exists);
1153 signal(SIGINT, SIG_IGN);
1154 signal(SIGPIPE, SIG_IGN);
1155 signal(SIGHUP, cleanup_handler);
1156 signal(SIGTERM, cleanup_handler);
1160 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1161 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1164 fatal("select: %s", strerror(errno));
1166 after_select(readsetp, writesetp);