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 $");
41 #include <openssl/evp.h>
42 #include <openssl/md5.h>
73 u_int sockets_alloc = 0;
74 SocketEntry *sockets = NULL;
76 typedef struct identity {
77 TAILQ_ENTRY(identity) next;
85 TAILQ_HEAD(idqueue, identity) idlist;
88 /* private key table, one per protocol version */
93 /* pid of shell == parent of agent */
94 pid_t parent_pid = -1;
96 /* pathname and directory for AUTH_SOCKET */
97 char socket_name[1024];
98 char socket_dir[1024];
102 char *lock_passwd = NULL;
104 #ifdef HAVE___PROGNAME
105 extern char *__progname;
111 close_socket(SocketEntry *e)
115 e->type = AUTH_UNUSED;
116 buffer_free(&e->input);
117 buffer_free(&e->output);
118 buffer_free(&e->request);
126 for (i = 0; i <=2; i++) {
127 TAILQ_INIT(&idtable[i].idlist);
128 idtable[i].nentries = 0;
132 /* return private key table for requested protocol version */
134 idtab_lookup(int version)
136 if (version < 1 || version > 2)
137 fatal("internal error, bad protocol version %d", version);
138 return &idtable[version];
142 free_identity(Identity *id)
149 /* return matching private key for given public key */
151 lookup_identity(Key *key, int version)
155 Idtab *tab = idtab_lookup(version);
156 TAILQ_FOREACH(id, &tab->idlist, next) {
157 if (key_equal(key, id->key))
163 /* send list of supported public keys to 'client' */
165 process_request_identities(SocketEntry *e, int version)
167 Idtab *tab = idtab_lookup(version);
172 buffer_put_char(&msg, (version == 1) ?
173 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
174 buffer_put_int(&msg, tab->nentries);
175 TAILQ_FOREACH(id, &tab->idlist, next) {
176 if (id->key->type == KEY_RSA1) {
177 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
178 buffer_put_bignum(&msg, id->key->rsa->e);
179 buffer_put_bignum(&msg, id->key->rsa->n);
183 key_to_blob(id->key, &blob, &blen);
184 buffer_put_string(&msg, blob, blen);
187 buffer_put_cstring(&msg, id->comment);
189 buffer_put_int(&e->output, buffer_len(&msg));
190 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
196 process_authentication_challenge1(SocketEntry *e)
198 u_char buf[32], mdbuf[16], session_id[16];
208 key = key_new(KEY_RSA1);
209 if ((challenge = BN_new()) == NULL)
210 fatal("process_authentication_challenge1: BN_new failed");
212 (void) buffer_get_int(&e->request); /* ignored */
213 buffer_get_bignum(&e->request, key->rsa->e);
214 buffer_get_bignum(&e->request, key->rsa->n);
215 buffer_get_bignum(&e->request, challenge);
217 /* Only protocol 1.1 is supported */
218 if (buffer_len(&e->request) == 0)
220 buffer_get(&e->request, session_id, 16);
221 response_type = buffer_get_int(&e->request);
222 if (response_type != 1)
225 id = lookup_identity(key, 1);
227 Key *private = id->key;
228 /* Decrypt the challenge using the private key. */
229 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
232 /* The response is MD5 of decrypted challenge plus session id. */
233 len = BN_num_bytes(challenge);
234 if (len <= 0 || len > 32) {
235 log("process_authentication_challenge: bad challenge length %d", len);
239 BN_bn2bin(challenge, buf + 32 - len);
241 MD5_Update(&md, buf, 32);
242 MD5_Update(&md, session_id, 16);
243 MD5_Final(mdbuf, &md);
245 /* Send the response. */
246 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
247 for (i = 0; i < 16; i++)
248 buffer_put_char(&msg, mdbuf[i]);
253 /* Unknown identity or protocol error. Send failure. */
254 buffer_put_char(&msg, SSH_AGENT_FAILURE);
256 buffer_put_int(&e->output, buffer_len(&msg));
257 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
259 BN_clear_free(challenge);
265 process_sign_request2(SocketEntry *e)
267 u_char *blob, *data, *signature = NULL;
268 u_int blen, dlen, slen = 0;
269 extern int datafellows;
276 blob = buffer_get_string(&e->request, &blen);
277 data = buffer_get_string(&e->request, &dlen);
279 flags = buffer_get_int(&e->request);
280 if (flags & SSH_AGENT_OLD_SIGNATURE)
281 datafellows = SSH_BUG_SIGBLOB;
283 key = key_from_blob(blob, blen);
285 Identity *id = lookup_identity(key, 2);
287 ok = key_sign(id->key, &signature, &slen, data, dlen);
292 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
293 buffer_put_string(&msg, signature, slen);
295 buffer_put_char(&msg, SSH_AGENT_FAILURE);
297 buffer_put_int(&e->output, buffer_len(&msg));
298 buffer_append(&e->output, buffer_ptr(&msg),
303 if (signature != NULL)
309 process_remove_identity(SocketEntry *e, int version)
318 key = key_new(KEY_RSA1);
319 bits = buffer_get_int(&e->request);
320 buffer_get_bignum(&e->request, key->rsa->e);
321 buffer_get_bignum(&e->request, key->rsa->n);
323 if (bits != key_size(key))
324 log("Warning: identity keysize mismatch: actual %u, announced %u",
325 key_size(key), bits);
328 blob = buffer_get_string(&e->request, &blen);
329 key = key_from_blob(blob, blen);
334 Identity *id = lookup_identity(key, version);
337 * We have this key. Free the old key. Since we
338 * don\'t want to leave empty slots in the middle of
339 * the array, we actually free the key there and move
340 * all the entries between the empty slot and the end
343 Idtab *tab = idtab_lookup(version);
344 if (tab->nentries < 1)
345 fatal("process_remove_identity: "
346 "internal error: tab->nentries %d",
348 TAILQ_REMOVE(&tab->idlist, id, next);
355 buffer_put_int(&e->output, 1);
356 buffer_put_char(&e->output,
357 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
361 process_remove_all_identities(SocketEntry *e, int version)
363 Idtab *tab = idtab_lookup(version);
366 /* Loop over all identities and clear the keys. */
367 for (id = TAILQ_FIRST(&tab->idlist); id;
368 id = TAILQ_FIRST(&tab->idlist)) {
369 TAILQ_REMOVE(&tab->idlist, id, next);
373 /* Mark that there are no identities. */
377 buffer_put_int(&e->output, 1);
378 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
384 u_int now = time(NULL);
389 for (version = 1; version < 3; version++) {
390 tab = idtab_lookup(version);
391 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
392 nxt = TAILQ_NEXT(id, next);
393 if (id->death != 0 && now >= id->death) {
394 TAILQ_REMOVE(&tab->idlist, id, next);
403 process_add_identity(SocketEntry *e, int version)
405 Idtab *tab = idtab_lookup(version);
406 int type, success = 0, death = 0;
407 char *type_name, *comment;
412 k = key_new_private(KEY_RSA1);
413 (void) buffer_get_int(&e->request); /* ignored */
414 buffer_get_bignum(&e->request, k->rsa->n);
415 buffer_get_bignum(&e->request, k->rsa->e);
416 buffer_get_bignum(&e->request, k->rsa->d);
417 buffer_get_bignum(&e->request, k->rsa->iqmp);
419 /* SSH and SSL have p and q swapped */
420 buffer_get_bignum(&e->request, k->rsa->q); /* p */
421 buffer_get_bignum(&e->request, k->rsa->p); /* q */
423 /* Generate additional parameters */
424 rsa_generate_additional_parameters(k->rsa);
427 type_name = buffer_get_string(&e->request, NULL);
428 type = key_type_from_name(type_name);
432 k = key_new_private(type);
433 buffer_get_bignum2(&e->request, k->dsa->p);
434 buffer_get_bignum2(&e->request, k->dsa->q);
435 buffer_get_bignum2(&e->request, k->dsa->g);
436 buffer_get_bignum2(&e->request, k->dsa->pub_key);
437 buffer_get_bignum2(&e->request, k->dsa->priv_key);
440 k = key_new_private(type);
441 buffer_get_bignum2(&e->request, k->rsa->n);
442 buffer_get_bignum2(&e->request, k->rsa->e);
443 buffer_get_bignum2(&e->request, k->rsa->d);
444 buffer_get_bignum2(&e->request, k->rsa->iqmp);
445 buffer_get_bignum2(&e->request, k->rsa->p);
446 buffer_get_bignum2(&e->request, k->rsa->q);
448 /* Generate additional parameters */
449 rsa_generate_additional_parameters(k->rsa);
452 buffer_clear(&e->request);
457 comment = buffer_get_string(&e->request, NULL);
463 while (buffer_len(&e->request)) {
464 switch (buffer_get_char(&e->request)) {
465 case SSH_AGENT_CONSTRAIN_LIFETIME:
466 death = time(NULL) + buffer_get_int(&e->request);
472 if (lookup_identity(k, version) == NULL) {
473 Identity *id = xmalloc(sizeof(Identity));
475 id->comment = comment;
477 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
478 /* Increment the number of identities. */
485 buffer_put_int(&e->output, 1);
486 buffer_put_char(&e->output,
487 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
490 /* XXX todo: encrypt sensitive data with passphrase */
492 process_lock_agent(SocketEntry *e, int lock)
497 passwd = buffer_get_string(&e->request, NULL);
498 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
500 memset(lock_passwd, 0, strlen(lock_passwd));
504 } else if (!locked && lock) {
506 lock_passwd = xstrdup(passwd);
509 memset(passwd, 0, strlen(passwd));
512 buffer_put_int(&e->output, 1);
513 buffer_put_char(&e->output,
514 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
518 no_identities(SocketEntry *e, u_int type)
523 buffer_put_char(&msg,
524 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
525 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
526 buffer_put_int(&msg, 0);
527 buffer_put_int(&e->output, buffer_len(&msg));
528 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
534 process_add_smartcard_key (SocketEntry *e)
536 char *sc_reader_id = NULL, *pin;
537 int i, version, success = 0;
542 sc_reader_id = buffer_get_string(&e->request, NULL);
543 pin = buffer_get_string(&e->request, NULL);
544 keys = sc_get_keys(sc_reader_id, pin);
548 if (keys == NULL || keys[0] == NULL) {
549 error("sc_get_keys failed");
552 for (i = 0; keys[i] != NULL; i++) {
554 version = k->type == KEY_RSA1 ? 1 : 2;
555 tab = idtab_lookup(version);
556 if (lookup_identity(k, version) == NULL) {
557 id = xmalloc(sizeof(Identity));
559 id->comment = xstrdup("smartcard key");
561 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
571 buffer_put_int(&e->output, 1);
572 buffer_put_char(&e->output,
573 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
577 process_remove_smartcard_key(SocketEntry *e)
579 char *sc_reader_id = NULL, *pin;
580 int i, version, success = 0;
581 Key **keys, *k = NULL;
585 sc_reader_id = buffer_get_string(&e->request, NULL);
586 pin = buffer_get_string(&e->request, NULL);
587 keys = sc_get_keys(sc_reader_id, pin);
591 if (keys == NULL || keys[0] == NULL) {
592 error("sc_get_keys failed");
595 for (i = 0; keys[i] != NULL; i++) {
597 version = k->type == KEY_RSA1 ? 1 : 2;
598 if ((id = lookup_identity(k, version)) != NULL) {
599 tab = idtab_lookup(version);
600 TAILQ_REMOVE(&tab->idlist, id, next);
610 buffer_put_int(&e->output, 1);
611 buffer_put_char(&e->output,
612 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
614 #endif /* SMARTCARD */
616 /* dispatch incoming messages */
619 process_message(SocketEntry *e)
627 if (buffer_len(&e->input) < 5)
628 return; /* Incomplete message. */
629 cp = buffer_ptr(&e->input);
630 msg_len = GET_32BIT(cp);
631 if (msg_len > 256 * 1024) {
635 if (buffer_len(&e->input) < msg_len + 4)
638 /* move the current input to e->request */
639 buffer_consume(&e->input, 4);
640 buffer_clear(&e->request);
641 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
642 buffer_consume(&e->input, msg_len);
643 type = buffer_get_char(&e->request);
645 /* check wheter agent is locked */
646 if (locked && type != SSH_AGENTC_UNLOCK) {
647 buffer_clear(&e->request);
649 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
650 case SSH2_AGENTC_REQUEST_IDENTITIES:
651 /* send empty lists */
652 no_identities(e, type);
655 /* send a fail message for all other request types */
656 buffer_put_int(&e->output, 1);
657 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
662 debug("type %d", type);
664 case SSH_AGENTC_LOCK:
665 case SSH_AGENTC_UNLOCK:
666 process_lock_agent(e, type == SSH_AGENTC_LOCK);
669 case SSH_AGENTC_RSA_CHALLENGE:
670 process_authentication_challenge1(e);
672 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
673 process_request_identities(e, 1);
675 case SSH_AGENTC_ADD_RSA_IDENTITY:
676 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
677 process_add_identity(e, 1);
679 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
680 process_remove_identity(e, 1);
682 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
683 process_remove_all_identities(e, 1);
686 case SSH2_AGENTC_SIGN_REQUEST:
687 process_sign_request2(e);
689 case SSH2_AGENTC_REQUEST_IDENTITIES:
690 process_request_identities(e, 2);
692 case SSH2_AGENTC_ADD_IDENTITY:
693 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
694 process_add_identity(e, 2);
696 case SSH2_AGENTC_REMOVE_IDENTITY:
697 process_remove_identity(e, 2);
699 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
700 process_remove_all_identities(e, 2);
703 case SSH_AGENTC_ADD_SMARTCARD_KEY:
704 process_add_smartcard_key(e);
706 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
707 process_remove_smartcard_key(e);
709 #endif /* SMARTCARD */
711 /* Unknown message. Respond with failure. */
712 error("Unknown message %d", type);
713 buffer_clear(&e->request);
714 buffer_put_int(&e->output, 1);
715 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
721 new_socket(sock_type type, int fd)
725 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
726 error("fcntl O_NONBLOCK: %s", strerror(errno));
731 for (i = 0; i < sockets_alloc; i++)
732 if (sockets[i].type == AUTH_UNUSED) {
734 sockets[i].type = type;
735 buffer_init(&sockets[i].input);
736 buffer_init(&sockets[i].output);
737 buffer_init(&sockets[i].request);
740 old_alloc = sockets_alloc;
743 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
745 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
746 for (i = old_alloc; i < sockets_alloc; i++)
747 sockets[i].type = AUTH_UNUSED;
748 sockets[old_alloc].type = type;
749 sockets[old_alloc].fd = fd;
750 buffer_init(&sockets[old_alloc].input);
751 buffer_init(&sockets[old_alloc].output);
752 buffer_init(&sockets[old_alloc].request);
756 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
761 for (i = 0; i < sockets_alloc; i++) {
762 switch (sockets[i].type) {
764 case AUTH_CONNECTION:
765 n = MAX(n, sockets[i].fd);
770 fatal("Unknown socket type %d", sockets[i].type);
775 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
776 if (*fdrp == NULL || sz > *nallocp) {
786 debug("XXX shrink: %d < %d", n, *fdl);
788 memset(*fdrp, 0, sz);
789 memset(*fdwp, 0, sz);
791 for (i = 0; i < sockets_alloc; i++) {
792 switch (sockets[i].type) {
794 case AUTH_CONNECTION:
795 FD_SET(sockets[i].fd, *fdrp);
796 if (buffer_len(&sockets[i].output) > 0)
797 FD_SET(sockets[i].fd, *fdwp);
807 after_select(fd_set *readset, fd_set *writeset)
809 struct sockaddr_un sunaddr;
817 for (i = 0; i < sockets_alloc; i++)
818 switch (sockets[i].type) {
822 if (FD_ISSET(sockets[i].fd, readset)) {
823 slen = sizeof(sunaddr);
824 sock = accept(sockets[i].fd,
825 (struct sockaddr *) &sunaddr, &slen);
827 error("accept from AUTH_SOCKET: %s",
831 if (getpeereid(sock, &euid, &egid) < 0) {
832 error("getpeereid %d failed: %s",
833 sock, strerror(errno));
837 if ((euid != 0) && (getuid() != euid)) {
838 error("uid mismatch: "
839 "peer euid %u != uid %u",
840 (u_int) euid, (u_int) getuid());
844 new_socket(AUTH_CONNECTION, sock);
847 case AUTH_CONNECTION:
848 if (buffer_len(&sockets[i].output) > 0 &&
849 FD_ISSET(sockets[i].fd, writeset)) {
851 len = write(sockets[i].fd,
852 buffer_ptr(&sockets[i].output),
853 buffer_len(&sockets[i].output));
854 if (len == -1 && (errno == EAGAIN ||
860 close_socket(&sockets[i]);
863 buffer_consume(&sockets[i].output, len);
865 if (FD_ISSET(sockets[i].fd, readset)) {
867 len = read(sockets[i].fd, buf, sizeof(buf));
868 if (len == -1 && (errno == EAGAIN ||
874 close_socket(&sockets[i]);
877 buffer_append(&sockets[i].input, buf, len);
878 process_message(&sockets[i]);
882 fatal("Unknown type %d", sockets[i].type);
887 cleanup_socket(void *p)
898 cleanup_socket(NULL);
903 cleanup_handler(int sig)
905 cleanup_socket(NULL);
910 check_parent_exists(int sig)
912 int save_errno = errno;
914 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
915 /* printf("Parent has died - Authentication agent exiting.\n"); */
916 cleanup_handler(sig); /* safe */
918 signal(SIGALRM, check_parent_exists);
926 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
928 fprintf(stderr, "Options:\n");
929 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
930 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
931 fprintf(stderr, " -k Kill the current agent.\n");
932 fprintf(stderr, " -d Debug mode.\n");
933 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
938 main(int ac, char **av)
940 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
941 char *shell, *format, *pidstr, *agentsocket = NULL;
942 fd_set *readsetp = NULL, *writesetp = NULL;
943 struct sockaddr_un sunaddr;
944 #ifdef HAVE_SETRLIMIT
953 char pidstrbuf[1 + 3 * sizeof pid];
960 SSLeay_add_all_algorithms();
962 __progname = get_progname(av[0]);
966 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
987 agentsocket = optarg;
996 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
999 if (ac == 0 && !c_flag && !s_flag) {
1000 shell = getenv("SHELL");
1001 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1005 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1006 if (pidstr == NULL) {
1007 fprintf(stderr, "%s not set, cannot kill agent\n",
1008 SSH_AGENTPID_ENV_NAME);
1013 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1014 SSH_AGENTPID_ENV_NAME, pidstr);
1017 if (kill(pid, SIGTERM) == -1) {
1021 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1022 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1023 printf(format, SSH_AGENTPID_ENV_NAME);
1024 printf("echo Agent pid %ld killed;\n", (long)pid);
1027 parent_pid = getpid();
1029 if (agentsocket == NULL) {
1030 /* Create private directory for agent socket */
1031 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1032 if (mkdtemp(socket_dir) == NULL) {
1033 perror("mkdtemp: private socket dir");
1036 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1039 /* Try to use specified agent socket */
1040 socket_dir[0] = '\0';
1041 strlcpy(socket_name, agentsocket, sizeof socket_name);
1045 * Create socket early so it will exist before command gets run from
1048 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1053 memset(&sunaddr, 0, sizeof(sunaddr));
1054 sunaddr.sun_family = AF_UNIX;
1055 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1057 prev_mask = umask(0177);
1059 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1069 if (listen(sock, 128) < 0) {
1075 * Fork, and have the parent execute the command, if any, or present
1076 * the socket data. The child continues as the authentication agent.
1079 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1080 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1081 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1082 SSH_AUTHSOCKET_ENV_NAME);
1083 printf("echo Agent pid %ld;\n", (long)parent_pid);
1091 if (pid != 0) { /* Parent - execute the given command. */
1093 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1095 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1096 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1097 SSH_AUTHSOCKET_ENV_NAME);
1098 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1099 SSH_AGENTPID_ENV_NAME);
1100 printf("echo Agent pid %ld;\n", (long)pid);
1103 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1104 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1113 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1115 if (setsid() == -1) {
1116 error("setsid: %s", strerror(errno));
1125 #ifdef HAVE_SETRLIMIT
1126 /* deny core dumps, since memory contains unencrypted private keys */
1127 rlim.rlim_cur = rlim.rlim_max = 0;
1128 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1129 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1135 fatal_add_cleanup(cleanup_socket, NULL);
1136 new_socket(AUTH_SOCKET, sock);
1138 signal(SIGALRM, check_parent_exists);
1143 signal(SIGINT, SIG_IGN);
1144 signal(SIGPIPE, SIG_IGN);
1145 signal(SIGHUP, cleanup_handler);
1146 signal(SIGTERM, cleanup_handler);
1150 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1151 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1154 fatal("select: %s", strerror(errno));
1156 after_select(readsetp, writesetp);