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.88 2002/06/05 19:57:12 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;
83 TAILQ_HEAD(idqueue, identity) idlist;
86 /* private key table, one per protocol version */
91 /* pid of shell == parent of agent */
92 pid_t parent_pid = -1;
94 /* pathname and directory for AUTH_SOCKET */
95 char socket_name[1024];
96 char socket_dir[1024];
100 char *lock_passwd = NULL;
102 #ifdef HAVE___PROGNAME
103 extern char *__progname;
112 for (i = 0; i <=2; i++) {
113 TAILQ_INIT(&idtable[i].idlist);
114 idtable[i].nentries = 0;
118 /* return private key table for requested protocol version */
120 idtab_lookup(int version)
122 if (version < 1 || version > 2)
123 fatal("internal error, bad protocol version %d", version);
124 return &idtable[version];
127 /* return matching private key for given public key */
129 lookup_identity(Key *key, int version)
133 Idtab *tab = idtab_lookup(version);
134 TAILQ_FOREACH(id, &tab->idlist, next) {
135 if (key_equal(key, id->key))
142 free_identity(Identity *id)
149 /* send list of supported public keys to 'client' */
151 process_request_identities(SocketEntry *e, int version)
153 Idtab *tab = idtab_lookup(version);
158 buffer_put_char(&msg, (version == 1) ?
159 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
160 buffer_put_int(&msg, tab->nentries);
161 TAILQ_FOREACH(id, &tab->idlist, next) {
162 if (id->key->type == KEY_RSA1) {
163 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
164 buffer_put_bignum(&msg, id->key->rsa->e);
165 buffer_put_bignum(&msg, id->key->rsa->n);
169 key_to_blob(id->key, &blob, &blen);
170 buffer_put_string(&msg, blob, blen);
173 buffer_put_cstring(&msg, id->comment);
175 buffer_put_int(&e->output, buffer_len(&msg));
176 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
182 process_authentication_challenge1(SocketEntry *e)
190 u_char buf[32], mdbuf[16], session_id[16];
194 key = key_new(KEY_RSA1);
195 if ((challenge = BN_new()) == NULL)
196 fatal("process_authentication_challenge1: BN_new failed");
198 buffer_get_int(&e->request); /* ignored */
199 buffer_get_bignum(&e->request, key->rsa->e);
200 buffer_get_bignum(&e->request, key->rsa->n);
201 buffer_get_bignum(&e->request, challenge);
203 /* Only protocol 1.1 is supported */
204 if (buffer_len(&e->request) == 0)
206 buffer_get(&e->request, session_id, 16);
207 response_type = buffer_get_int(&e->request);
208 if (response_type != 1)
211 id = lookup_identity(key, 1);
213 Key *private = id->key;
214 /* Decrypt the challenge using the private key. */
215 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
218 /* The response is MD5 of decrypted challenge plus session id. */
219 len = BN_num_bytes(challenge);
220 if (len <= 0 || len > 32) {
221 log("process_authentication_challenge: bad challenge length %d", len);
225 BN_bn2bin(challenge, buf + 32 - len);
227 MD5_Update(&md, buf, 32);
228 MD5_Update(&md, session_id, 16);
229 MD5_Final(mdbuf, &md);
231 /* Send the response. */
232 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
233 for (i = 0; i < 16; i++)
234 buffer_put_char(&msg, mdbuf[i]);
239 /* Unknown identity or protocol error. Send failure. */
240 buffer_put_char(&msg, SSH_AGENT_FAILURE);
242 buffer_put_int(&e->output, buffer_len(&msg));
243 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
245 BN_clear_free(challenge);
251 process_sign_request2(SocketEntry *e)
253 extern int datafellows;
255 u_char *blob, *data, *signature = NULL;
256 u_int blen, dlen, slen = 0;
263 blob = buffer_get_string(&e->request, &blen);
264 data = buffer_get_string(&e->request, &dlen);
266 flags = buffer_get_int(&e->request);
267 if (flags & SSH_AGENT_OLD_SIGNATURE)
268 datafellows = SSH_BUG_SIGBLOB;
270 key = key_from_blob(blob, blen);
272 Identity *id = lookup_identity(key, 2);
274 ok = key_sign(id->key, &signature, &slen, data, dlen);
279 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
280 buffer_put_string(&msg, signature, slen);
282 buffer_put_char(&msg, SSH_AGENT_FAILURE);
284 buffer_put_int(&e->output, buffer_len(&msg));
285 buffer_append(&e->output, buffer_ptr(&msg),
290 if (signature != NULL)
296 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 %d, announced %d",
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);
371 process_add_identity(SocketEntry *e, int version)
376 int type, success = 0;
377 Idtab *tab = idtab_lookup(version);
381 k = key_new_private(KEY_RSA1);
382 buffer_get_int(&e->request); /* ignored */
383 buffer_get_bignum(&e->request, k->rsa->n);
384 buffer_get_bignum(&e->request, k->rsa->e);
385 buffer_get_bignum(&e->request, k->rsa->d);
386 buffer_get_bignum(&e->request, k->rsa->iqmp);
388 /* SSH and SSL have p and q swapped */
389 buffer_get_bignum(&e->request, k->rsa->q); /* p */
390 buffer_get_bignum(&e->request, k->rsa->p); /* q */
392 /* Generate additional parameters */
393 rsa_generate_additional_parameters(k->rsa);
396 type_name = buffer_get_string(&e->request, NULL);
397 type = key_type_from_name(type_name);
401 k = key_new_private(type);
402 buffer_get_bignum2(&e->request, k->dsa->p);
403 buffer_get_bignum2(&e->request, k->dsa->q);
404 buffer_get_bignum2(&e->request, k->dsa->g);
405 buffer_get_bignum2(&e->request, k->dsa->pub_key);
406 buffer_get_bignum2(&e->request, k->dsa->priv_key);
409 k = key_new_private(type);
410 buffer_get_bignum2(&e->request, k->rsa->n);
411 buffer_get_bignum2(&e->request, k->rsa->e);
412 buffer_get_bignum2(&e->request, k->rsa->d);
413 buffer_get_bignum2(&e->request, k->rsa->iqmp);
414 buffer_get_bignum2(&e->request, k->rsa->p);
415 buffer_get_bignum2(&e->request, k->rsa->q);
417 /* Generate additional parameters */
418 rsa_generate_additional_parameters(k->rsa);
421 buffer_clear(&e->request);
426 comment = buffer_get_string(&e->request, NULL);
432 if (lookup_identity(k, version) == NULL) {
433 Identity *id = xmalloc(sizeof(Identity));
435 id->comment = comment;
436 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
437 /* Increment the number of identities. */
444 buffer_put_int(&e->output, 1);
445 buffer_put_char(&e->output,
446 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
449 /* XXX todo: encrypt sensitive data with passphrase */
451 process_lock_agent(SocketEntry *e, int lock)
456 passwd = buffer_get_string(&e->request, NULL);
457 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
459 memset(lock_passwd, 0, strlen(lock_passwd));
463 } else if (!locked && lock) {
465 lock_passwd = xstrdup(passwd);
468 memset(passwd, 0, strlen(passwd));
471 buffer_put_int(&e->output, 1);
472 buffer_put_char(&e->output,
473 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
478 no_identities(SocketEntry *e, u_int type)
483 buffer_put_char(&msg,
484 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
485 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
486 buffer_put_int(&msg, 0);
487 buffer_put_int(&e->output, buffer_len(&msg));
488 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
494 process_add_smartcard_key (SocketEntry *e)
499 char *sc_reader_id = NULL, *pin;
500 int i, version, success = 0;
502 sc_reader_id = buffer_get_string(&e->request, NULL);
503 pin = buffer_get_string(&e->request, NULL);
504 keys = sc_get_keys(sc_reader_id, pin);
508 if (keys == NULL || keys[0] == NULL) {
509 error("sc_get_keys failed");
512 for (i = 0; keys[i] != NULL; i++) {
514 version = k->type == KEY_RSA1 ? 1 : 2;
515 tab = idtab_lookup(version);
516 if (lookup_identity(k, version) == NULL) {
517 id = xmalloc(sizeof(Identity));
519 id->comment = xstrdup("smartcard key");
520 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
530 buffer_put_int(&e->output, 1);
531 buffer_put_char(&e->output,
532 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
536 process_remove_smartcard_key(SocketEntry *e)
540 Key **keys, *k = NULL;
541 char *sc_reader_id = NULL, *pin;
542 int i, version, success = 0;
544 sc_reader_id = buffer_get_string(&e->request, NULL);
545 pin = buffer_get_string(&e->request, NULL);
546 keys = sc_get_keys(sc_reader_id, pin);
550 if (keys == NULL || keys[0] == NULL) {
551 error("sc_get_keys failed");
554 for (i = 0; keys[i] != NULL; i++) {
556 version = k->type == KEY_RSA1 ? 1 : 2;
557 if ((id = lookup_identity(k, version)) != NULL) {
558 tab = idtab_lookup(version);
559 TAILQ_REMOVE(&tab->idlist, id, next);
569 buffer_put_int(&e->output, 1);
570 buffer_put_char(&e->output,
571 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
573 #endif /* SMARTCARD */
575 /* dispatch incoming messages */
578 process_message(SocketEntry *e)
583 if (buffer_len(&e->input) < 5)
584 return; /* Incomplete message. */
585 cp = buffer_ptr(&e->input);
586 msg_len = GET_32BIT(cp);
587 if (msg_len > 256 * 1024) {
588 shutdown(e->fd, SHUT_RDWR);
590 e->type = AUTH_UNUSED;
591 buffer_free(&e->input);
592 buffer_free(&e->output);
593 buffer_free(&e->request);
596 if (buffer_len(&e->input) < msg_len + 4)
599 /* move the current input to e->request */
600 buffer_consume(&e->input, 4);
601 buffer_clear(&e->request);
602 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
603 buffer_consume(&e->input, msg_len);
604 type = buffer_get_char(&e->request);
606 /* check wheter agent is locked */
607 if (locked && type != SSH_AGENTC_UNLOCK) {
608 buffer_clear(&e->request);
610 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
611 case SSH2_AGENTC_REQUEST_IDENTITIES:
612 /* send empty lists */
613 no_identities(e, type);
616 /* send a fail message for all other request types */
617 buffer_put_int(&e->output, 1);
618 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
623 debug("type %d", type);
625 case SSH_AGENTC_LOCK:
626 case SSH_AGENTC_UNLOCK:
627 process_lock_agent(e, type == SSH_AGENTC_LOCK);
630 case SSH_AGENTC_RSA_CHALLENGE:
631 process_authentication_challenge1(e);
633 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
634 process_request_identities(e, 1);
636 case SSH_AGENTC_ADD_RSA_IDENTITY:
637 process_add_identity(e, 1);
639 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
640 process_remove_identity(e, 1);
642 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
643 process_remove_all_identities(e, 1);
646 case SSH2_AGENTC_SIGN_REQUEST:
647 process_sign_request2(e);
649 case SSH2_AGENTC_REQUEST_IDENTITIES:
650 process_request_identities(e, 2);
652 case SSH2_AGENTC_ADD_IDENTITY:
653 process_add_identity(e, 2);
655 case SSH2_AGENTC_REMOVE_IDENTITY:
656 process_remove_identity(e, 2);
658 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
659 process_remove_all_identities(e, 2);
662 case SSH_AGENTC_ADD_SMARTCARD_KEY:
663 process_add_smartcard_key(e);
665 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
666 process_remove_smartcard_key(e);
668 #endif /* SMARTCARD */
670 /* Unknown message. Respond with failure. */
671 error("Unknown message %d", type);
672 buffer_clear(&e->request);
673 buffer_put_int(&e->output, 1);
674 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
680 new_socket(sock_type type, int fd)
683 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
684 error("fcntl O_NONBLOCK: %s", strerror(errno));
689 for (i = 0; i < sockets_alloc; i++)
690 if (sockets[i].type == AUTH_UNUSED) {
692 sockets[i].type = type;
693 buffer_init(&sockets[i].input);
694 buffer_init(&sockets[i].output);
695 buffer_init(&sockets[i].request);
698 old_alloc = sockets_alloc;
701 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
703 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
704 for (i = old_alloc; i < sockets_alloc; i++)
705 sockets[i].type = AUTH_UNUSED;
706 sockets[old_alloc].type = type;
707 sockets[old_alloc].fd = fd;
708 buffer_init(&sockets[old_alloc].input);
709 buffer_init(&sockets[old_alloc].output);
710 buffer_init(&sockets[old_alloc].request);
714 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
719 for (i = 0; i < sockets_alloc; i++) {
720 switch (sockets[i].type) {
722 case AUTH_CONNECTION:
723 n = MAX(n, sockets[i].fd);
728 fatal("Unknown socket type %d", sockets[i].type);
733 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
734 if (*fdrp == NULL || sz > *nallocp) {
744 debug("XXX shrink: %d < %d", n, *fdl);
746 memset(*fdrp, 0, sz);
747 memset(*fdwp, 0, sz);
749 for (i = 0; i < sockets_alloc; i++) {
750 switch (sockets[i].type) {
752 case AUTH_CONNECTION:
753 FD_SET(sockets[i].fd, *fdrp);
754 if (buffer_len(&sockets[i].output) > 0)
755 FD_SET(sockets[i].fd, *fdwp);
765 after_select(fd_set *readset, fd_set *writeset)
771 struct sockaddr_un sunaddr;
773 for (i = 0; i < sockets_alloc; i++)
774 switch (sockets[i].type) {
778 if (FD_ISSET(sockets[i].fd, readset)) {
779 slen = sizeof(sunaddr);
780 sock = accept(sockets[i].fd,
781 (struct sockaddr *) &sunaddr, &slen);
783 error("accept from AUTH_SOCKET: %s",
787 new_socket(AUTH_CONNECTION, sock);
790 case AUTH_CONNECTION:
791 if (buffer_len(&sockets[i].output) > 0 &&
792 FD_ISSET(sockets[i].fd, writeset)) {
794 len = write(sockets[i].fd,
795 buffer_ptr(&sockets[i].output),
796 buffer_len(&sockets[i].output));
797 if (len == -1 && (errno == EAGAIN ||
803 shutdown(sockets[i].fd, SHUT_RDWR);
804 close(sockets[i].fd);
805 sockets[i].type = AUTH_UNUSED;
806 buffer_free(&sockets[i].input);
807 buffer_free(&sockets[i].output);
808 buffer_free(&sockets[i].request);
811 buffer_consume(&sockets[i].output, len);
813 if (FD_ISSET(sockets[i].fd, readset)) {
815 len = read(sockets[i].fd, buf, sizeof(buf));
816 if (len == -1 && (errno == EAGAIN ||
822 shutdown(sockets[i].fd, SHUT_RDWR);
823 close(sockets[i].fd);
824 sockets[i].type = AUTH_UNUSED;
825 buffer_free(&sockets[i].input);
826 buffer_free(&sockets[i].output);
827 buffer_free(&sockets[i].request);
830 buffer_append(&sockets[i].input, buf, len);
831 process_message(&sockets[i]);
835 fatal("Unknown type %d", sockets[i].type);
840 cleanup_socket(void *p)
851 cleanup_socket(NULL);
856 cleanup_handler(int sig)
858 cleanup_socket(NULL);
863 check_parent_exists(int sig)
865 int save_errno = errno;
867 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
868 /* printf("Parent has died - Authentication agent exiting.\n"); */
869 cleanup_handler(sig); /* safe */
871 signal(SIGALRM, check_parent_exists);
879 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
881 fprintf(stderr, "Options:\n");
882 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
883 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
884 fprintf(stderr, " -k Kill the current agent.\n");
885 fprintf(stderr, " -d Debug mode.\n");
886 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
891 main(int ac, char **av)
893 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
894 struct sockaddr_un sunaddr;
895 #ifdef HAVE_SETRLIMIT
902 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
903 char *agentsocket = NULL;
905 fd_set *readsetp = NULL, *writesetp = NULL;
907 SSLeay_add_all_algorithms();
909 __progname = get_progname(av[0]);
913 #ifdef __GNU_LIBRARY__
914 while ((ch = getopt(ac, av, "+cdksa:")) != -1) {
915 #else /* __GNU_LIBRARY__ */
916 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
917 #endif /* __GNU_LIBRARY__ */
938 agentsocket = optarg;
947 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
950 if (ac == 0 && !c_flag && !s_flag) {
951 shell = getenv("SHELL");
952 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
956 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
957 if (pidstr == NULL) {
958 fprintf(stderr, "%s not set, cannot kill agent\n",
959 SSH_AGENTPID_ENV_NAME);
964 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
965 SSH_AGENTPID_ENV_NAME, pidstr);
968 if (kill(pid, SIGTERM) == -1) {
972 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
973 printf(format, SSH_AUTHSOCKET_ENV_NAME);
974 printf(format, SSH_AGENTPID_ENV_NAME);
975 printf("echo Agent pid %d killed;\n", pid);
978 parent_pid = getpid();
980 if (agentsocket == NULL) {
981 /* Create private directory for agent socket */
982 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
983 if (mkdtemp(socket_dir) == NULL) {
984 perror("mkdtemp: private socket dir");
987 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
990 /* Try to use specified agent socket */
991 socket_dir[0] = '\0';
992 strlcpy(socket_name, agentsocket, sizeof socket_name);
996 * Create socket early so it will exist before command gets run from
999 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1004 memset(&sunaddr, 0, sizeof(sunaddr));
1005 sunaddr.sun_family = AF_UNIX;
1006 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1008 prev_mask = umask(0177);
1010 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1020 if (listen(sock, 5) < 0) {
1026 * Fork, and have the parent execute the command, if any, or present
1027 * the socket data. The child continues as the authentication agent.
1030 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1031 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1032 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1033 SSH_AUTHSOCKET_ENV_NAME);
1034 printf("echo Agent pid %d;\n", parent_pid);
1042 if (pid != 0) { /* Parent - execute the given command. */
1044 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
1046 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1047 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1048 SSH_AUTHSOCKET_ENV_NAME);
1049 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1050 SSH_AGENTPID_ENV_NAME);
1051 printf("echo Agent pid %d;\n", pid);
1054 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1055 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1064 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1066 if (setsid() == -1) {
1067 error("setsid: %s", strerror(errno));
1076 #ifdef HAVE_SETRLIMIT
1077 /* deny core dumps, since memory contains unencrypted private keys */
1078 rlim.rlim_cur = rlim.rlim_max = 0;
1079 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1080 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1086 fatal_add_cleanup(cleanup_socket, NULL);
1087 new_socket(AUTH_SOCKET, sock);
1089 signal(SIGALRM, check_parent_exists);
1094 signal(SIGINT, SIG_IGN);
1095 signal(SIGPIPE, SIG_IGN);
1096 signal(SIGHUP, cleanup_handler);
1097 signal(SIGTERM, cleanup_handler);
1101 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1102 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1105 fatal("select: %s", strerror(errno));
1107 after_select(readsetp, writesetp);