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.104 2002/09/12 19:11:52 stevesk 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;
110 close_socket(SocketEntry *e)
114 e->type = AUTH_UNUSED;
115 buffer_free(&e->input);
116 buffer_free(&e->output);
117 buffer_free(&e->request);
125 for (i = 0; i <=2; i++) {
126 TAILQ_INIT(&idtable[i].idlist);
127 idtable[i].nentries = 0;
131 /* return private key table for requested protocol version */
133 idtab_lookup(int version)
135 if (version < 1 || version > 2)
136 fatal("internal error, bad protocol version %d", version);
137 return &idtable[version];
141 free_identity(Identity *id)
148 /* return matching private key for given public key */
150 lookup_identity(Key *key, int version)
154 Idtab *tab = idtab_lookup(version);
155 TAILQ_FOREACH(id, &tab->idlist, next) {
156 if (key_equal(key, id->key))
162 /* send list of supported public keys to 'client' */
164 process_request_identities(SocketEntry *e, int version)
166 Idtab *tab = idtab_lookup(version);
171 buffer_put_char(&msg, (version == 1) ?
172 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
173 buffer_put_int(&msg, tab->nentries);
174 TAILQ_FOREACH(id, &tab->idlist, next) {
175 if (id->key->type == KEY_RSA1) {
176 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
177 buffer_put_bignum(&msg, id->key->rsa->e);
178 buffer_put_bignum(&msg, id->key->rsa->n);
182 key_to_blob(id->key, &blob, &blen);
183 buffer_put_string(&msg, blob, blen);
186 buffer_put_cstring(&msg, id->comment);
188 buffer_put_int(&e->output, buffer_len(&msg));
189 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
195 process_authentication_challenge1(SocketEntry *e)
197 u_char buf[32], mdbuf[16], session_id[16];
207 key = key_new(KEY_RSA1);
208 if ((challenge = BN_new()) == NULL)
209 fatal("process_authentication_challenge1: BN_new failed");
211 (void) buffer_get_int(&e->request); /* ignored */
212 buffer_get_bignum(&e->request, key->rsa->e);
213 buffer_get_bignum(&e->request, key->rsa->n);
214 buffer_get_bignum(&e->request, challenge);
216 /* Only protocol 1.1 is supported */
217 if (buffer_len(&e->request) == 0)
219 buffer_get(&e->request, session_id, 16);
220 response_type = buffer_get_int(&e->request);
221 if (response_type != 1)
224 id = lookup_identity(key, 1);
226 Key *private = id->key;
227 /* Decrypt the challenge using the private key. */
228 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
231 /* The response is MD5 of decrypted challenge plus session id. */
232 len = BN_num_bytes(challenge);
233 if (len <= 0 || len > 32) {
234 log("process_authentication_challenge: bad challenge length %d", len);
238 BN_bn2bin(challenge, buf + 32 - len);
240 MD5_Update(&md, buf, 32);
241 MD5_Update(&md, session_id, 16);
242 MD5_Final(mdbuf, &md);
244 /* Send the response. */
245 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
246 for (i = 0; i < 16; i++)
247 buffer_put_char(&msg, mdbuf[i]);
252 /* Unknown identity or protocol error. Send failure. */
253 buffer_put_char(&msg, SSH_AGENT_FAILURE);
255 buffer_put_int(&e->output, buffer_len(&msg));
256 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
258 BN_clear_free(challenge);
264 process_sign_request2(SocketEntry *e)
266 u_char *blob, *data, *signature = NULL;
267 u_int blen, dlen, slen = 0;
268 extern int datafellows;
275 blob = buffer_get_string(&e->request, &blen);
276 data = buffer_get_string(&e->request, &dlen);
278 flags = buffer_get_int(&e->request);
279 if (flags & SSH_AGENT_OLD_SIGNATURE)
280 datafellows = SSH_BUG_SIGBLOB;
282 key = key_from_blob(blob, blen);
284 Identity *id = lookup_identity(key, 2);
286 ok = key_sign(id->key, &signature, &slen, data, dlen);
291 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
292 buffer_put_string(&msg, signature, slen);
294 buffer_put_char(&msg, SSH_AGENT_FAILURE);
296 buffer_put_int(&e->output, buffer_len(&msg));
297 buffer_append(&e->output, buffer_ptr(&msg),
302 if (signature != NULL)
308 process_remove_identity(SocketEntry *e, int version)
317 key = key_new(KEY_RSA1);
318 bits = buffer_get_int(&e->request);
319 buffer_get_bignum(&e->request, key->rsa->e);
320 buffer_get_bignum(&e->request, key->rsa->n);
322 if (bits != key_size(key))
323 log("Warning: identity keysize mismatch: actual %u, announced %u",
324 key_size(key), bits);
327 blob = buffer_get_string(&e->request, &blen);
328 key = key_from_blob(blob, blen);
333 Identity *id = lookup_identity(key, version);
336 * We have this key. Free the old key. Since we
337 * don\'t want to leave empty slots in the middle of
338 * the array, we actually free the key there and move
339 * all the entries between the empty slot and the end
342 Idtab *tab = idtab_lookup(version);
343 if (tab->nentries < 1)
344 fatal("process_remove_identity: "
345 "internal error: tab->nentries %d",
347 TAILQ_REMOVE(&tab->idlist, id, next);
354 buffer_put_int(&e->output, 1);
355 buffer_put_char(&e->output,
356 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
360 process_remove_all_identities(SocketEntry *e, int version)
362 Idtab *tab = idtab_lookup(version);
365 /* Loop over all identities and clear the keys. */
366 for (id = TAILQ_FIRST(&tab->idlist); id;
367 id = TAILQ_FIRST(&tab->idlist)) {
368 TAILQ_REMOVE(&tab->idlist, id, next);
372 /* Mark that there are no identities. */
376 buffer_put_int(&e->output, 1);
377 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
383 u_int now = time(NULL);
388 for (version = 1; version < 3; version++) {
389 tab = idtab_lookup(version);
390 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
391 nxt = TAILQ_NEXT(id, next);
392 if (id->death != 0 && now >= id->death) {
393 TAILQ_REMOVE(&tab->idlist, id, next);
402 process_add_identity(SocketEntry *e, int version)
404 Idtab *tab = idtab_lookup(version);
405 int type, success = 0, death = 0;
406 char *type_name, *comment;
411 k = key_new_private(KEY_RSA1);
412 (void) buffer_get_int(&e->request); /* ignored */
413 buffer_get_bignum(&e->request, k->rsa->n);
414 buffer_get_bignum(&e->request, k->rsa->e);
415 buffer_get_bignum(&e->request, k->rsa->d);
416 buffer_get_bignum(&e->request, k->rsa->iqmp);
418 /* SSH and SSL have p and q swapped */
419 buffer_get_bignum(&e->request, k->rsa->q); /* p */
420 buffer_get_bignum(&e->request, k->rsa->p); /* q */
422 /* Generate additional parameters */
423 rsa_generate_additional_parameters(k->rsa);
426 type_name = buffer_get_string(&e->request, NULL);
427 type = key_type_from_name(type_name);
431 k = key_new_private(type);
432 buffer_get_bignum2(&e->request, k->dsa->p);
433 buffer_get_bignum2(&e->request, k->dsa->q);
434 buffer_get_bignum2(&e->request, k->dsa->g);
435 buffer_get_bignum2(&e->request, k->dsa->pub_key);
436 buffer_get_bignum2(&e->request, k->dsa->priv_key);
439 k = key_new_private(type);
440 buffer_get_bignum2(&e->request, k->rsa->n);
441 buffer_get_bignum2(&e->request, k->rsa->e);
442 buffer_get_bignum2(&e->request, k->rsa->d);
443 buffer_get_bignum2(&e->request, k->rsa->iqmp);
444 buffer_get_bignum2(&e->request, k->rsa->p);
445 buffer_get_bignum2(&e->request, k->rsa->q);
447 /* Generate additional parameters */
448 rsa_generate_additional_parameters(k->rsa);
451 buffer_clear(&e->request);
456 comment = buffer_get_string(&e->request, NULL);
462 while (buffer_len(&e->request)) {
463 switch (buffer_get_char(&e->request)) {
464 case SSH_AGENT_CONSTRAIN_LIFETIME:
465 death = time(NULL) + buffer_get_int(&e->request);
471 if (lookup_identity(k, version) == NULL) {
472 Identity *id = xmalloc(sizeof(Identity));
474 id->comment = comment;
476 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
477 /* Increment the number of identities. */
484 buffer_put_int(&e->output, 1);
485 buffer_put_char(&e->output,
486 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
489 /* XXX todo: encrypt sensitive data with passphrase */
491 process_lock_agent(SocketEntry *e, int lock)
496 passwd = buffer_get_string(&e->request, NULL);
497 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
499 memset(lock_passwd, 0, strlen(lock_passwd));
503 } else if (!locked && lock) {
505 lock_passwd = xstrdup(passwd);
508 memset(passwd, 0, strlen(passwd));
511 buffer_put_int(&e->output, 1);
512 buffer_put_char(&e->output,
513 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
517 no_identities(SocketEntry *e, u_int type)
522 buffer_put_char(&msg,
523 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
524 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
525 buffer_put_int(&msg, 0);
526 buffer_put_int(&e->output, buffer_len(&msg));
527 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
533 process_add_smartcard_key (SocketEntry *e)
535 char *sc_reader_id = NULL, *pin;
536 int i, version, success = 0;
541 sc_reader_id = buffer_get_string(&e->request, NULL);
542 pin = buffer_get_string(&e->request, NULL);
543 keys = sc_get_keys(sc_reader_id, pin);
547 if (keys == NULL || keys[0] == NULL) {
548 error("sc_get_keys failed");
551 for (i = 0; keys[i] != NULL; i++) {
553 version = k->type == KEY_RSA1 ? 1 : 2;
554 tab = idtab_lookup(version);
555 if (lookup_identity(k, version) == NULL) {
556 id = xmalloc(sizeof(Identity));
558 id->comment = xstrdup("smartcard key");
560 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
570 buffer_put_int(&e->output, 1);
571 buffer_put_char(&e->output,
572 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
576 process_remove_smartcard_key(SocketEntry *e)
578 char *sc_reader_id = NULL, *pin;
579 int i, version, success = 0;
580 Key **keys, *k = NULL;
584 sc_reader_id = buffer_get_string(&e->request, NULL);
585 pin = buffer_get_string(&e->request, NULL);
586 keys = sc_get_keys(sc_reader_id, pin);
590 if (keys == NULL || keys[0] == NULL) {
591 error("sc_get_keys failed");
594 for (i = 0; keys[i] != NULL; i++) {
596 version = k->type == KEY_RSA1 ? 1 : 2;
597 if ((id = lookup_identity(k, version)) != NULL) {
598 tab = idtab_lookup(version);
599 TAILQ_REMOVE(&tab->idlist, id, next);
609 buffer_put_int(&e->output, 1);
610 buffer_put_char(&e->output,
611 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
613 #endif /* SMARTCARD */
615 /* dispatch incoming messages */
618 process_message(SocketEntry *e)
626 if (buffer_len(&e->input) < 5)
627 return; /* Incomplete message. */
628 cp = buffer_ptr(&e->input);
629 msg_len = GET_32BIT(cp);
630 if (msg_len > 256 * 1024) {
634 if (buffer_len(&e->input) < msg_len + 4)
637 /* move the current input to e->request */
638 buffer_consume(&e->input, 4);
639 buffer_clear(&e->request);
640 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
641 buffer_consume(&e->input, msg_len);
642 type = buffer_get_char(&e->request);
644 /* check wheter agent is locked */
645 if (locked && type != SSH_AGENTC_UNLOCK) {
646 buffer_clear(&e->request);
648 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
649 case SSH2_AGENTC_REQUEST_IDENTITIES:
650 /* send empty lists */
651 no_identities(e, type);
654 /* send a fail message for all other request types */
655 buffer_put_int(&e->output, 1);
656 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
661 debug("type %d", type);
663 case SSH_AGENTC_LOCK:
664 case SSH_AGENTC_UNLOCK:
665 process_lock_agent(e, type == SSH_AGENTC_LOCK);
668 case SSH_AGENTC_RSA_CHALLENGE:
669 process_authentication_challenge1(e);
671 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
672 process_request_identities(e, 1);
674 case SSH_AGENTC_ADD_RSA_IDENTITY:
675 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
676 process_add_identity(e, 1);
678 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
679 process_remove_identity(e, 1);
681 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
682 process_remove_all_identities(e, 1);
685 case SSH2_AGENTC_SIGN_REQUEST:
686 process_sign_request2(e);
688 case SSH2_AGENTC_REQUEST_IDENTITIES:
689 process_request_identities(e, 2);
691 case SSH2_AGENTC_ADD_IDENTITY:
692 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
693 process_add_identity(e, 2);
695 case SSH2_AGENTC_REMOVE_IDENTITY:
696 process_remove_identity(e, 2);
698 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
699 process_remove_all_identities(e, 2);
702 case SSH_AGENTC_ADD_SMARTCARD_KEY:
703 process_add_smartcard_key(e);
705 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
706 process_remove_smartcard_key(e);
708 #endif /* SMARTCARD */
710 /* Unknown message. Respond with failure. */
711 error("Unknown message %d", type);
712 buffer_clear(&e->request);
713 buffer_put_int(&e->output, 1);
714 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
720 new_socket(sock_type type, int fd)
724 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
725 error("fcntl O_NONBLOCK: %s", strerror(errno));
730 for (i = 0; i < sockets_alloc; i++)
731 if (sockets[i].type == AUTH_UNUSED) {
733 sockets[i].type = type;
734 buffer_init(&sockets[i].input);
735 buffer_init(&sockets[i].output);
736 buffer_init(&sockets[i].request);
739 old_alloc = sockets_alloc;
742 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
744 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
745 for (i = old_alloc; i < sockets_alloc; i++)
746 sockets[i].type = AUTH_UNUSED;
747 sockets[old_alloc].type = type;
748 sockets[old_alloc].fd = fd;
749 buffer_init(&sockets[old_alloc].input);
750 buffer_init(&sockets[old_alloc].output);
751 buffer_init(&sockets[old_alloc].request);
755 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
760 for (i = 0; i < sockets_alloc; i++) {
761 switch (sockets[i].type) {
763 case AUTH_CONNECTION:
764 n = MAX(n, sockets[i].fd);
769 fatal("Unknown socket type %d", sockets[i].type);
774 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
775 if (*fdrp == NULL || sz > *nallocp) {
785 debug("XXX shrink: %d < %d", n, *fdl);
787 memset(*fdrp, 0, sz);
788 memset(*fdwp, 0, sz);
790 for (i = 0; i < sockets_alloc; i++) {
791 switch (sockets[i].type) {
793 case AUTH_CONNECTION:
794 FD_SET(sockets[i].fd, *fdrp);
795 if (buffer_len(&sockets[i].output) > 0)
796 FD_SET(sockets[i].fd, *fdwp);
806 after_select(fd_set *readset, fd_set *writeset)
808 struct sockaddr_un sunaddr;
816 for (i = 0; i < sockets_alloc; i++)
817 switch (sockets[i].type) {
821 if (FD_ISSET(sockets[i].fd, readset)) {
822 slen = sizeof(sunaddr);
823 sock = accept(sockets[i].fd,
824 (struct sockaddr *) &sunaddr, &slen);
826 error("accept from AUTH_SOCKET: %s",
830 if (getpeereid(sock, &euid, &egid) < 0) {
831 error("getpeereid %d failed: %s",
832 sock, strerror(errno));
836 if (getuid() != euid) {
837 error("uid mismatch: "
838 "peer euid %u != uid %u",
839 (u_int) euid, (u_int) getuid());
843 new_socket(AUTH_CONNECTION, sock);
846 case AUTH_CONNECTION:
847 if (buffer_len(&sockets[i].output) > 0 &&
848 FD_ISSET(sockets[i].fd, writeset)) {
850 len = write(sockets[i].fd,
851 buffer_ptr(&sockets[i].output),
852 buffer_len(&sockets[i].output));
853 if (len == -1 && (errno == EAGAIN ||
859 close_socket(&sockets[i]);
862 buffer_consume(&sockets[i].output, len);
864 if (FD_ISSET(sockets[i].fd, readset)) {
866 len = read(sockets[i].fd, buf, sizeof(buf));
867 if (len == -1 && (errno == EAGAIN ||
873 close_socket(&sockets[i]);
876 buffer_append(&sockets[i].input, buf, len);
877 process_message(&sockets[i]);
881 fatal("Unknown type %d", sockets[i].type);
886 cleanup_socket(void *p)
897 cleanup_socket(NULL);
902 cleanup_handler(int sig)
904 cleanup_socket(NULL);
909 check_parent_exists(int sig)
911 int save_errno = errno;
913 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
914 /* printf("Parent has died - Authentication agent exiting.\n"); */
915 cleanup_handler(sig); /* safe */
917 signal(SIGALRM, check_parent_exists);
925 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
927 fprintf(stderr, "Options:\n");
928 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
929 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
930 fprintf(stderr, " -k Kill the current agent.\n");
931 fprintf(stderr, " -d Debug mode.\n");
932 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
937 main(int ac, char **av)
939 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
940 char *shell, *format, *pidstr, *agentsocket = NULL;
941 fd_set *readsetp = NULL, *writesetp = NULL;
942 struct sockaddr_un sunaddr;
943 #ifdef HAVE_SETRLIMIT
952 char pidstrbuf[1 + 3 * sizeof pid];
958 SSLeay_add_all_algorithms();
960 __progname = get_progname(av[0]);
964 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
985 agentsocket = optarg;
994 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
997 if (ac == 0 && !c_flag && !s_flag) {
998 shell = getenv("SHELL");
999 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1003 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1004 if (pidstr == NULL) {
1005 fprintf(stderr, "%s not set, cannot kill agent\n",
1006 SSH_AGENTPID_ENV_NAME);
1011 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1012 SSH_AGENTPID_ENV_NAME, pidstr);
1015 if (kill(pid, SIGTERM) == -1) {
1019 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1020 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1021 printf(format, SSH_AGENTPID_ENV_NAME);
1022 printf("echo Agent pid %ld killed;\n", (long)pid);
1025 parent_pid = getpid();
1027 if (agentsocket == NULL) {
1028 /* Create private directory for agent socket */
1029 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1030 if (mkdtemp(socket_dir) == NULL) {
1031 perror("mkdtemp: private socket dir");
1034 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1037 /* Try to use specified agent socket */
1038 socket_dir[0] = '\0';
1039 strlcpy(socket_name, agentsocket, sizeof socket_name);
1043 * Create socket early so it will exist before command gets run from
1046 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1051 memset(&sunaddr, 0, sizeof(sunaddr));
1052 sunaddr.sun_family = AF_UNIX;
1053 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1055 prev_mask = umask(0177);
1057 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1067 if (listen(sock, 128) < 0) {
1073 * Fork, and have the parent execute the command, if any, or present
1074 * the socket data. The child continues as the authentication agent.
1077 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1078 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1079 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1080 SSH_AUTHSOCKET_ENV_NAME);
1081 printf("echo Agent pid %ld;\n", (long)parent_pid);
1089 if (pid != 0) { /* Parent - execute the given command. */
1091 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1093 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1094 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1095 SSH_AUTHSOCKET_ENV_NAME);
1096 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1097 SSH_AGENTPID_ENV_NAME);
1098 printf("echo Agent pid %ld;\n", (long)pid);
1101 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1102 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1111 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1113 if (setsid() == -1) {
1114 error("setsid: %s", strerror(errno));
1123 #ifdef HAVE_SETRLIMIT
1124 /* deny core dumps, since memory contains unencrypted private keys */
1125 rlim.rlim_cur = rlim.rlim_max = 0;
1126 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1127 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1133 fatal_add_cleanup(cleanup_socket, NULL);
1134 new_socket(AUTH_SOCKET, sock);
1136 signal(SIGALRM, check_parent_exists);
1141 signal(SIGINT, SIG_IGN);
1142 signal(SIGPIPE, SIG_IGN);
1143 signal(SIGHUP, cleanup_handler);
1144 signal(SIGTERM, cleanup_handler);
1148 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1149 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1152 fatal("select: %s", strerror(errno));
1154 after_select(readsetp, writesetp);