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.105 2002/10/01 20:34:12 markus Exp $");
39 RCSID("$FreeBSD: src/crypto/openssh/ssh-agent.c,v 1.2.2.10 2003/02/11 08:27:40 des Exp $");
40 RCSID("$DragonFly: src/crypto/openssh/Attic/ssh-agent.c,v 1.2 2003/06/17 04:24:36 dillon Exp $");
42 #include <openssl/evp.h>
43 #include <openssl/md5.h>
74 u_int sockets_alloc = 0;
75 SocketEntry *sockets = NULL;
77 typedef struct identity {
78 TAILQ_ENTRY(identity) next;
86 TAILQ_HEAD(idqueue, identity) idlist;
89 /* private key table, one per protocol version */
94 /* pid of shell == parent of agent */
95 pid_t parent_pid = -1;
97 /* pathname and directory for AUTH_SOCKET */
98 char socket_name[1024];
99 char socket_dir[1024];
103 char *lock_passwd = NULL;
105 #ifdef HAVE___PROGNAME
106 extern char *__progname;
112 close_socket(SocketEntry *e)
116 e->type = AUTH_UNUSED;
117 buffer_free(&e->input);
118 buffer_free(&e->output);
119 buffer_free(&e->request);
127 for (i = 0; i <=2; i++) {
128 TAILQ_INIT(&idtable[i].idlist);
129 idtable[i].nentries = 0;
133 /* return private key table for requested protocol version */
135 idtab_lookup(int version)
137 if (version < 1 || version > 2)
138 fatal("internal error, bad protocol version %d", version);
139 return &idtable[version];
143 free_identity(Identity *id)
150 /* return matching private key for given public key */
152 lookup_identity(Key *key, int version)
156 Idtab *tab = idtab_lookup(version);
157 TAILQ_FOREACH(id, &tab->idlist, next) {
158 if (key_equal(key, id->key))
164 /* send list of supported public keys to 'client' */
166 process_request_identities(SocketEntry *e, int version)
168 Idtab *tab = idtab_lookup(version);
173 buffer_put_char(&msg, (version == 1) ?
174 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
175 buffer_put_int(&msg, tab->nentries);
176 TAILQ_FOREACH(id, &tab->idlist, next) {
177 if (id->key->type == KEY_RSA1) {
178 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
179 buffer_put_bignum(&msg, id->key->rsa->e);
180 buffer_put_bignum(&msg, id->key->rsa->n);
184 key_to_blob(id->key, &blob, &blen);
185 buffer_put_string(&msg, blob, blen);
188 buffer_put_cstring(&msg, id->comment);
190 buffer_put_int(&e->output, buffer_len(&msg));
191 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
197 process_authentication_challenge1(SocketEntry *e)
199 u_char buf[32], mdbuf[16], session_id[16];
209 key = key_new(KEY_RSA1);
210 if ((challenge = BN_new()) == NULL)
211 fatal("process_authentication_challenge1: BN_new failed");
213 (void) buffer_get_int(&e->request); /* ignored */
214 buffer_get_bignum(&e->request, key->rsa->e);
215 buffer_get_bignum(&e->request, key->rsa->n);
216 buffer_get_bignum(&e->request, challenge);
218 /* Only protocol 1.1 is supported */
219 if (buffer_len(&e->request) == 0)
221 buffer_get(&e->request, session_id, 16);
222 response_type = buffer_get_int(&e->request);
223 if (response_type != 1)
226 id = lookup_identity(key, 1);
228 Key *private = id->key;
229 /* Decrypt the challenge using the private key. */
230 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
233 /* The response is MD5 of decrypted challenge plus session id. */
234 len = BN_num_bytes(challenge);
235 if (len <= 0 || len > 32) {
236 log("process_authentication_challenge: bad challenge length %d", len);
240 BN_bn2bin(challenge, buf + 32 - len);
242 MD5_Update(&md, buf, 32);
243 MD5_Update(&md, session_id, 16);
244 MD5_Final(mdbuf, &md);
246 /* Send the response. */
247 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
248 for (i = 0; i < 16; i++)
249 buffer_put_char(&msg, mdbuf[i]);
254 /* Unknown identity or protocol error. Send failure. */
255 buffer_put_char(&msg, SSH_AGENT_FAILURE);
257 buffer_put_int(&e->output, buffer_len(&msg));
258 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
260 BN_clear_free(challenge);
266 process_sign_request2(SocketEntry *e)
268 u_char *blob, *data, *signature = NULL;
269 u_int blen, dlen, slen = 0;
270 extern int datafellows;
277 blob = buffer_get_string(&e->request, &blen);
278 data = buffer_get_string(&e->request, &dlen);
280 flags = buffer_get_int(&e->request);
281 if (flags & SSH_AGENT_OLD_SIGNATURE)
282 datafellows = SSH_BUG_SIGBLOB;
284 key = key_from_blob(blob, blen);
286 Identity *id = lookup_identity(key, 2);
288 ok = key_sign(id->key, &signature, &slen, data, dlen);
293 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
294 buffer_put_string(&msg, signature, slen);
296 buffer_put_char(&msg, SSH_AGENT_FAILURE);
298 buffer_put_int(&e->output, buffer_len(&msg));
299 buffer_append(&e->output, buffer_ptr(&msg),
304 if (signature != NULL)
310 process_remove_identity(SocketEntry *e, int version)
319 key = key_new(KEY_RSA1);
320 bits = buffer_get_int(&e->request);
321 buffer_get_bignum(&e->request, key->rsa->e);
322 buffer_get_bignum(&e->request, key->rsa->n);
324 if (bits != key_size(key))
325 log("Warning: identity keysize mismatch: actual %u, announced %u",
326 key_size(key), bits);
329 blob = buffer_get_string(&e->request, &blen);
330 key = key_from_blob(blob, blen);
335 Identity *id = lookup_identity(key, version);
338 * We have this key. Free the old key. Since we
339 * don\'t want to leave empty slots in the middle of
340 * the array, we actually free the key there and move
341 * all the entries between the empty slot and the end
344 Idtab *tab = idtab_lookup(version);
345 if (tab->nentries < 1)
346 fatal("process_remove_identity: "
347 "internal error: tab->nentries %d",
349 TAILQ_REMOVE(&tab->idlist, id, next);
356 buffer_put_int(&e->output, 1);
357 buffer_put_char(&e->output,
358 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
362 process_remove_all_identities(SocketEntry *e, int version)
364 Idtab *tab = idtab_lookup(version);
367 /* Loop over all identities and clear the keys. */
368 for (id = TAILQ_FIRST(&tab->idlist); id;
369 id = TAILQ_FIRST(&tab->idlist)) {
370 TAILQ_REMOVE(&tab->idlist, id, next);
374 /* Mark that there are no identities. */
378 buffer_put_int(&e->output, 1);
379 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
385 u_int now = time(NULL);
390 for (version = 1; version < 3; version++) {
391 tab = idtab_lookup(version);
392 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
393 nxt = TAILQ_NEXT(id, next);
394 if (id->death != 0 && now >= id->death) {
395 TAILQ_REMOVE(&tab->idlist, id, next);
404 process_add_identity(SocketEntry *e, int version)
406 Idtab *tab = idtab_lookup(version);
407 int type, success = 0, death = 0;
408 char *type_name, *comment;
413 k = key_new_private(KEY_RSA1);
414 (void) buffer_get_int(&e->request); /* ignored */
415 buffer_get_bignum(&e->request, k->rsa->n);
416 buffer_get_bignum(&e->request, k->rsa->e);
417 buffer_get_bignum(&e->request, k->rsa->d);
418 buffer_get_bignum(&e->request, k->rsa->iqmp);
420 /* SSH and SSL have p and q swapped */
421 buffer_get_bignum(&e->request, k->rsa->q); /* p */
422 buffer_get_bignum(&e->request, k->rsa->p); /* q */
424 /* Generate additional parameters */
425 rsa_generate_additional_parameters(k->rsa);
428 type_name = buffer_get_string(&e->request, NULL);
429 type = key_type_from_name(type_name);
433 k = key_new_private(type);
434 buffer_get_bignum2(&e->request, k->dsa->p);
435 buffer_get_bignum2(&e->request, k->dsa->q);
436 buffer_get_bignum2(&e->request, k->dsa->g);
437 buffer_get_bignum2(&e->request, k->dsa->pub_key);
438 buffer_get_bignum2(&e->request, k->dsa->priv_key);
441 k = key_new_private(type);
442 buffer_get_bignum2(&e->request, k->rsa->n);
443 buffer_get_bignum2(&e->request, k->rsa->e);
444 buffer_get_bignum2(&e->request, k->rsa->d);
445 buffer_get_bignum2(&e->request, k->rsa->iqmp);
446 buffer_get_bignum2(&e->request, k->rsa->p);
447 buffer_get_bignum2(&e->request, k->rsa->q);
449 /* Generate additional parameters */
450 rsa_generate_additional_parameters(k->rsa);
453 buffer_clear(&e->request);
458 comment = buffer_get_string(&e->request, NULL);
464 while (buffer_len(&e->request)) {
465 switch (buffer_get_char(&e->request)) {
466 case SSH_AGENT_CONSTRAIN_LIFETIME:
467 death = time(NULL) + buffer_get_int(&e->request);
473 if (lookup_identity(k, version) == NULL) {
474 Identity *id = xmalloc(sizeof(Identity));
476 id->comment = comment;
478 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
479 /* Increment the number of identities. */
486 buffer_put_int(&e->output, 1);
487 buffer_put_char(&e->output,
488 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
491 /* XXX todo: encrypt sensitive data with passphrase */
493 process_lock_agent(SocketEntry *e, int lock)
498 passwd = buffer_get_string(&e->request, NULL);
499 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
501 memset(lock_passwd, 0, strlen(lock_passwd));
505 } else if (!locked && lock) {
507 lock_passwd = xstrdup(passwd);
510 memset(passwd, 0, strlen(passwd));
513 buffer_put_int(&e->output, 1);
514 buffer_put_char(&e->output,
515 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
519 no_identities(SocketEntry *e, u_int type)
524 buffer_put_char(&msg,
525 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
526 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
527 buffer_put_int(&msg, 0);
528 buffer_put_int(&e->output, buffer_len(&msg));
529 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
535 process_add_smartcard_key (SocketEntry *e)
537 char *sc_reader_id = NULL, *pin;
538 int i, version, success = 0;
543 sc_reader_id = buffer_get_string(&e->request, NULL);
544 pin = buffer_get_string(&e->request, NULL);
545 keys = sc_get_keys(sc_reader_id, pin);
549 if (keys == NULL || keys[0] == NULL) {
550 error("sc_get_keys failed");
553 for (i = 0; keys[i] != NULL; i++) {
555 version = k->type == KEY_RSA1 ? 1 : 2;
556 tab = idtab_lookup(version);
557 if (lookup_identity(k, version) == NULL) {
558 id = xmalloc(sizeof(Identity));
560 id->comment = xstrdup("smartcard key");
562 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
572 buffer_put_int(&e->output, 1);
573 buffer_put_char(&e->output,
574 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
578 process_remove_smartcard_key(SocketEntry *e)
580 char *sc_reader_id = NULL, *pin;
581 int i, version, success = 0;
582 Key **keys, *k = NULL;
586 sc_reader_id = buffer_get_string(&e->request, NULL);
587 pin = buffer_get_string(&e->request, NULL);
588 keys = sc_get_keys(sc_reader_id, pin);
592 if (keys == NULL || keys[0] == NULL) {
593 error("sc_get_keys failed");
596 for (i = 0; keys[i] != NULL; i++) {
598 version = k->type == KEY_RSA1 ? 1 : 2;
599 if ((id = lookup_identity(k, version)) != NULL) {
600 tab = idtab_lookup(version);
601 TAILQ_REMOVE(&tab->idlist, id, next);
611 buffer_put_int(&e->output, 1);
612 buffer_put_char(&e->output,
613 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
615 #endif /* SMARTCARD */
617 /* dispatch incoming messages */
620 process_message(SocketEntry *e)
628 if (buffer_len(&e->input) < 5)
629 return; /* Incomplete message. */
630 cp = buffer_ptr(&e->input);
631 msg_len = GET_32BIT(cp);
632 if (msg_len > 256 * 1024) {
636 if (buffer_len(&e->input) < msg_len + 4)
639 /* move the current input to e->request */
640 buffer_consume(&e->input, 4);
641 buffer_clear(&e->request);
642 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
643 buffer_consume(&e->input, msg_len);
644 type = buffer_get_char(&e->request);
646 /* check wheter agent is locked */
647 if (locked && type != SSH_AGENTC_UNLOCK) {
648 buffer_clear(&e->request);
650 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
651 case SSH2_AGENTC_REQUEST_IDENTITIES:
652 /* send empty lists */
653 no_identities(e, type);
656 /* send a fail message for all other request types */
657 buffer_put_int(&e->output, 1);
658 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
663 debug("type %d", type);
665 case SSH_AGENTC_LOCK:
666 case SSH_AGENTC_UNLOCK:
667 process_lock_agent(e, type == SSH_AGENTC_LOCK);
670 case SSH_AGENTC_RSA_CHALLENGE:
671 process_authentication_challenge1(e);
673 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
674 process_request_identities(e, 1);
676 case SSH_AGENTC_ADD_RSA_IDENTITY:
677 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
678 process_add_identity(e, 1);
680 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
681 process_remove_identity(e, 1);
683 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
684 process_remove_all_identities(e, 1);
687 case SSH2_AGENTC_SIGN_REQUEST:
688 process_sign_request2(e);
690 case SSH2_AGENTC_REQUEST_IDENTITIES:
691 process_request_identities(e, 2);
693 case SSH2_AGENTC_ADD_IDENTITY:
694 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
695 process_add_identity(e, 2);
697 case SSH2_AGENTC_REMOVE_IDENTITY:
698 process_remove_identity(e, 2);
700 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
701 process_remove_all_identities(e, 2);
704 case SSH_AGENTC_ADD_SMARTCARD_KEY:
705 process_add_smartcard_key(e);
707 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
708 process_remove_smartcard_key(e);
710 #endif /* SMARTCARD */
712 /* Unknown message. Respond with failure. */
713 error("Unknown message %d", type);
714 buffer_clear(&e->request);
715 buffer_put_int(&e->output, 1);
716 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
722 new_socket(sock_type type, int fd)
726 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
727 error("fcntl O_NONBLOCK: %s", strerror(errno));
732 for (i = 0; i < sockets_alloc; i++)
733 if (sockets[i].type == AUTH_UNUSED) {
735 sockets[i].type = type;
736 buffer_init(&sockets[i].input);
737 buffer_init(&sockets[i].output);
738 buffer_init(&sockets[i].request);
741 old_alloc = sockets_alloc;
744 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
746 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
747 for (i = old_alloc; i < sockets_alloc; i++)
748 sockets[i].type = AUTH_UNUSED;
749 sockets[old_alloc].type = type;
750 sockets[old_alloc].fd = fd;
751 buffer_init(&sockets[old_alloc].input);
752 buffer_init(&sockets[old_alloc].output);
753 buffer_init(&sockets[old_alloc].request);
757 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
762 for (i = 0; i < sockets_alloc; i++) {
763 switch (sockets[i].type) {
765 case AUTH_CONNECTION:
766 n = MAX(n, sockets[i].fd);
771 fatal("Unknown socket type %d", sockets[i].type);
776 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
777 if (*fdrp == NULL || sz > *nallocp) {
787 debug("XXX shrink: %d < %d", n, *fdl);
789 memset(*fdrp, 0, sz);
790 memset(*fdwp, 0, sz);
792 for (i = 0; i < sockets_alloc; i++) {
793 switch (sockets[i].type) {
795 case AUTH_CONNECTION:
796 FD_SET(sockets[i].fd, *fdrp);
797 if (buffer_len(&sockets[i].output) > 0)
798 FD_SET(sockets[i].fd, *fdwp);
808 after_select(fd_set *readset, fd_set *writeset)
810 struct sockaddr_un sunaddr;
818 for (i = 0; i < sockets_alloc; i++)
819 switch (sockets[i].type) {
823 if (FD_ISSET(sockets[i].fd, readset)) {
824 slen = sizeof(sunaddr);
825 sock = accept(sockets[i].fd,
826 (struct sockaddr *) &sunaddr, &slen);
828 error("accept from AUTH_SOCKET: %s",
832 if (getpeereid(sock, &euid, &egid) < 0) {
833 error("getpeereid %d failed: %s",
834 sock, strerror(errno));
838 if ((euid != 0) && (getuid() != euid)) {
839 error("uid mismatch: "
840 "peer euid %u != uid %u",
841 (u_int) euid, (u_int) getuid());
845 new_socket(AUTH_CONNECTION, sock);
848 case AUTH_CONNECTION:
849 if (buffer_len(&sockets[i].output) > 0 &&
850 FD_ISSET(sockets[i].fd, writeset)) {
852 len = write(sockets[i].fd,
853 buffer_ptr(&sockets[i].output),
854 buffer_len(&sockets[i].output));
855 if (len == -1 && (errno == EAGAIN ||
861 close_socket(&sockets[i]);
864 buffer_consume(&sockets[i].output, len);
866 if (FD_ISSET(sockets[i].fd, readset)) {
868 len = read(sockets[i].fd, buf, sizeof(buf));
869 if (len == -1 && (errno == EAGAIN ||
875 close_socket(&sockets[i]);
878 buffer_append(&sockets[i].input, buf, len);
879 process_message(&sockets[i]);
883 fatal("Unknown type %d", sockets[i].type);
888 cleanup_socket(void *p)
899 cleanup_socket(NULL);
904 cleanup_handler(int sig)
906 cleanup_socket(NULL);
911 check_parent_exists(int sig)
913 int save_errno = errno;
915 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
916 /* printf("Parent has died - Authentication agent exiting.\n"); */
917 cleanup_handler(sig); /* safe */
919 signal(SIGALRM, check_parent_exists);
927 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
929 fprintf(stderr, "Options:\n");
930 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
931 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
932 fprintf(stderr, " -k Kill the current agent.\n");
933 fprintf(stderr, " -d Debug mode.\n");
934 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
939 main(int ac, char **av)
941 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
942 char *shell, *format, *pidstr, *agentsocket = NULL;
943 fd_set *readsetp = NULL, *writesetp = NULL;
944 struct sockaddr_un sunaddr;
945 #ifdef HAVE_SETRLIMIT
954 char pidstrbuf[1 + 3 * sizeof pid];
961 SSLeay_add_all_algorithms();
963 __progname = get_progname(av[0]);
967 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
988 agentsocket = optarg;
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 && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1006 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1007 if (pidstr == NULL) {
1008 fprintf(stderr, "%s not set, cannot kill agent\n",
1009 SSH_AGENTPID_ENV_NAME);
1014 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1015 SSH_AGENTPID_ENV_NAME, pidstr);
1018 if (kill(pid, SIGTERM) == -1) {
1022 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1023 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1024 printf(format, SSH_AGENTPID_ENV_NAME);
1025 printf("echo Agent pid %ld killed;\n", (long)pid);
1028 parent_pid = getpid();
1030 if (agentsocket == NULL) {
1031 /* Create private directory for agent socket */
1032 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1033 if (mkdtemp(socket_dir) == NULL) {
1034 perror("mkdtemp: private socket dir");
1037 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1040 /* Try to use specified agent socket */
1041 socket_dir[0] = '\0';
1042 strlcpy(socket_name, agentsocket, sizeof socket_name);
1046 * Create socket early so it will exist before command gets run from
1049 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1054 memset(&sunaddr, 0, sizeof(sunaddr));
1055 sunaddr.sun_family = AF_UNIX;
1056 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1058 prev_mask = umask(0177);
1060 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1070 if (listen(sock, 128) < 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);
1084 printf("echo Agent pid %ld;\n", (long)parent_pid);
1092 if (pid != 0) { /* Parent - execute the given command. */
1094 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1096 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1097 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1098 SSH_AUTHSOCKET_ENV_NAME);
1099 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1100 SSH_AGENTPID_ENV_NAME);
1101 printf("echo Agent pid %ld;\n", (long)pid);
1104 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1105 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1114 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1116 if (setsid() == -1) {
1117 error("setsid: %s", strerror(errno));
1126 #ifdef HAVE_SETRLIMIT
1127 /* deny core dumps, since memory contains unencrypted private keys */
1128 rlim.rlim_cur = rlim.rlim_max = 0;
1129 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1130 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1136 fatal_add_cleanup(cleanup_socket, NULL);
1137 new_socket(AUTH_SOCKET, sock);
1139 signal(SIGALRM, check_parent_exists);
1144 signal(SIGINT, SIG_IGN);
1145 signal(SIGPIPE, SIG_IGN);
1146 signal(SIGHUP, cleanup_handler);
1147 signal(SIGTERM, cleanup_handler);
1151 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1152 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1155 fatal("select: %s", strerror(errno));
1157 after_select(readsetp, writesetp);