1 /* $OpenBSD: ssh-agent.c,v 1.144 2006/07/17 01:31:10 stevesk Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * The authentication agent program.
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
14 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/types.h>
41 #include <sys/socket.h>
45 #include "openbsd-compat/sys-queue.h"
46 #include <sys/resource.h>
56 #include <openssl/evp.h>
57 #include <openssl/md5.h>
74 #if defined(HAVE_SYS_PRCTL_H)
75 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
92 u_int sockets_alloc = 0;
93 SocketEntry *sockets = NULL;
95 typedef struct identity {
96 TAILQ_ENTRY(identity) next;
105 TAILQ_HEAD(idqueue, identity) idlist;
108 /* private key table, one per protocol version */
113 /* pid of shell == parent of agent */
114 pid_t parent_pid = -1;
116 /* pathname and directory for AUTH_SOCKET */
117 char socket_name[MAXPATHLEN];
118 char socket_dir[MAXPATHLEN];
122 char *lock_passwd = NULL;
124 extern char *__progname;
126 /* Default lifetime (0 == forever) */
127 static int lifetime = 0;
130 close_socket(SocketEntry *e)
134 e->type = AUTH_UNUSED;
135 buffer_free(&e->input);
136 buffer_free(&e->output);
137 buffer_free(&e->request);
145 for (i = 0; i <=2; i++) {
146 TAILQ_INIT(&idtable[i].idlist);
147 idtable[i].nentries = 0;
151 /* return private key table for requested protocol version */
153 idtab_lookup(int version)
155 if (version < 1 || version > 2)
156 fatal("internal error, bad protocol version %d", version);
157 return &idtable[version];
161 free_identity(Identity *id)
168 /* return matching private key for given public key */
170 lookup_identity(Key *key, int version)
174 Idtab *tab = idtab_lookup(version);
175 TAILQ_FOREACH(id, &tab->idlist, next) {
176 if (key_equal(key, id->key))
182 /* Check confirmation of keysign request */
184 confirm_key(Identity *id)
189 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
190 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
198 /* send list of supported public keys to 'client' */
200 process_request_identities(SocketEntry *e, int version)
202 Idtab *tab = idtab_lookup(version);
207 buffer_put_char(&msg, (version == 1) ?
208 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
209 buffer_put_int(&msg, tab->nentries);
210 TAILQ_FOREACH(id, &tab->idlist, next) {
211 if (id->key->type == KEY_RSA1) {
212 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
213 buffer_put_bignum(&msg, id->key->rsa->e);
214 buffer_put_bignum(&msg, id->key->rsa->n);
218 key_to_blob(id->key, &blob, &blen);
219 buffer_put_string(&msg, blob, blen);
222 buffer_put_cstring(&msg, id->comment);
224 buffer_put_int(&e->output, buffer_len(&msg));
225 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
231 process_authentication_challenge1(SocketEntry *e)
233 u_char buf[32], mdbuf[16], session_id[16];
243 key = key_new(KEY_RSA1);
244 if ((challenge = BN_new()) == NULL)
245 fatal("process_authentication_challenge1: BN_new failed");
247 (void) buffer_get_int(&e->request); /* ignored */
248 buffer_get_bignum(&e->request, key->rsa->e);
249 buffer_get_bignum(&e->request, key->rsa->n);
250 buffer_get_bignum(&e->request, challenge);
252 /* Only protocol 1.1 is supported */
253 if (buffer_len(&e->request) == 0)
255 buffer_get(&e->request, session_id, 16);
256 response_type = buffer_get_int(&e->request);
257 if (response_type != 1)
260 id = lookup_identity(key, 1);
261 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
262 Key *private = id->key;
263 /* Decrypt the challenge using the private key. */
264 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
267 /* The response is MD5 of decrypted challenge plus session id. */
268 len = BN_num_bytes(challenge);
269 if (len <= 0 || len > 32) {
270 logit("process_authentication_challenge: bad challenge length %d", len);
274 BN_bn2bin(challenge, buf + 32 - len);
276 MD5_Update(&md, buf, 32);
277 MD5_Update(&md, session_id, 16);
278 MD5_Final(mdbuf, &md);
280 /* Send the response. */
281 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
282 for (i = 0; i < 16; i++)
283 buffer_put_char(&msg, mdbuf[i]);
288 /* Unknown identity or protocol error. Send failure. */
289 buffer_put_char(&msg, SSH_AGENT_FAILURE);
291 buffer_put_int(&e->output, buffer_len(&msg));
292 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
294 BN_clear_free(challenge);
300 process_sign_request2(SocketEntry *e)
302 u_char *blob, *data, *signature = NULL;
303 u_int blen, dlen, slen = 0;
304 extern int datafellows;
311 blob = buffer_get_string(&e->request, &blen);
312 data = buffer_get_string(&e->request, &dlen);
314 flags = buffer_get_int(&e->request);
315 if (flags & SSH_AGENT_OLD_SIGNATURE)
316 datafellows = SSH_BUG_SIGBLOB;
318 key = key_from_blob(blob, blen);
320 Identity *id = lookup_identity(key, 2);
321 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
322 ok = key_sign(id->key, &signature, &slen, data, dlen);
327 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
328 buffer_put_string(&msg, signature, slen);
330 buffer_put_char(&msg, SSH_AGENT_FAILURE);
332 buffer_put_int(&e->output, buffer_len(&msg));
333 buffer_append(&e->output, buffer_ptr(&msg),
338 if (signature != NULL)
344 process_remove_identity(SocketEntry *e, int version)
353 key = key_new(KEY_RSA1);
354 bits = buffer_get_int(&e->request);
355 buffer_get_bignum(&e->request, key->rsa->e);
356 buffer_get_bignum(&e->request, key->rsa->n);
358 if (bits != key_size(key))
359 logit("Warning: identity keysize mismatch: actual %u, announced %u",
360 key_size(key), bits);
363 blob = buffer_get_string(&e->request, &blen);
364 key = key_from_blob(blob, blen);
369 Identity *id = lookup_identity(key, version);
372 * We have this key. Free the old key. Since we
373 * don't want to leave empty slots in the middle of
374 * the array, we actually free the key there and move
375 * all the entries between the empty slot and the end
378 Idtab *tab = idtab_lookup(version);
379 if (tab->nentries < 1)
380 fatal("process_remove_identity: "
381 "internal error: tab->nentries %d",
383 TAILQ_REMOVE(&tab->idlist, id, next);
390 buffer_put_int(&e->output, 1);
391 buffer_put_char(&e->output,
392 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
396 process_remove_all_identities(SocketEntry *e, int version)
398 Idtab *tab = idtab_lookup(version);
401 /* Loop over all identities and clear the keys. */
402 for (id = TAILQ_FIRST(&tab->idlist); id;
403 id = TAILQ_FIRST(&tab->idlist)) {
404 TAILQ_REMOVE(&tab->idlist, id, next);
408 /* Mark that there are no identities. */
412 buffer_put_int(&e->output, 1);
413 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
419 u_int now = time(NULL);
424 for (version = 1; version < 3; version++) {
425 tab = idtab_lookup(version);
426 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
427 nxt = TAILQ_NEXT(id, next);
428 if (id->death != 0 && now >= id->death) {
429 TAILQ_REMOVE(&tab->idlist, id, next);
438 process_add_identity(SocketEntry *e, int version)
440 Idtab *tab = idtab_lookup(version);
441 int type, success = 0, death = 0, confirm = 0;
442 char *type_name, *comment;
447 k = key_new_private(KEY_RSA1);
448 (void) buffer_get_int(&e->request); /* ignored */
449 buffer_get_bignum(&e->request, k->rsa->n);
450 buffer_get_bignum(&e->request, k->rsa->e);
451 buffer_get_bignum(&e->request, k->rsa->d);
452 buffer_get_bignum(&e->request, k->rsa->iqmp);
454 /* SSH and SSL have p and q swapped */
455 buffer_get_bignum(&e->request, k->rsa->q); /* p */
456 buffer_get_bignum(&e->request, k->rsa->p); /* q */
458 /* Generate additional parameters */
459 rsa_generate_additional_parameters(k->rsa);
462 type_name = buffer_get_string(&e->request, NULL);
463 type = key_type_from_name(type_name);
467 k = key_new_private(type);
468 buffer_get_bignum2(&e->request, k->dsa->p);
469 buffer_get_bignum2(&e->request, k->dsa->q);
470 buffer_get_bignum2(&e->request, k->dsa->g);
471 buffer_get_bignum2(&e->request, k->dsa->pub_key);
472 buffer_get_bignum2(&e->request, k->dsa->priv_key);
475 k = key_new_private(type);
476 buffer_get_bignum2(&e->request, k->rsa->n);
477 buffer_get_bignum2(&e->request, k->rsa->e);
478 buffer_get_bignum2(&e->request, k->rsa->d);
479 buffer_get_bignum2(&e->request, k->rsa->iqmp);
480 buffer_get_bignum2(&e->request, k->rsa->p);
481 buffer_get_bignum2(&e->request, k->rsa->q);
483 /* Generate additional parameters */
484 rsa_generate_additional_parameters(k->rsa);
487 buffer_clear(&e->request);
492 /* enable blinding */
496 if (RSA_blinding_on(k->rsa, NULL) != 1) {
497 error("process_add_identity: RSA_blinding_on failed");
503 comment = buffer_get_string(&e->request, NULL);
509 while (buffer_len(&e->request)) {
510 switch (buffer_get_char(&e->request)) {
511 case SSH_AGENT_CONSTRAIN_LIFETIME:
512 death = time(NULL) + buffer_get_int(&e->request);
514 case SSH_AGENT_CONSTRAIN_CONFIRM:
521 if (lifetime && !death)
522 death = time(NULL) + lifetime;
523 if (lookup_identity(k, version) == NULL) {
524 Identity *id = xmalloc(sizeof(Identity));
526 id->comment = comment;
528 id->confirm = confirm;
529 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
530 /* Increment the number of identities. */
537 buffer_put_int(&e->output, 1);
538 buffer_put_char(&e->output,
539 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
542 /* XXX todo: encrypt sensitive data with passphrase */
544 process_lock_agent(SocketEntry *e, int lock)
549 passwd = buffer_get_string(&e->request, NULL);
550 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
552 memset(lock_passwd, 0, strlen(lock_passwd));
556 } else if (!locked && lock) {
558 lock_passwd = xstrdup(passwd);
561 memset(passwd, 0, strlen(passwd));
564 buffer_put_int(&e->output, 1);
565 buffer_put_char(&e->output,
566 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
570 no_identities(SocketEntry *e, u_int type)
575 buffer_put_char(&msg,
576 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
577 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
578 buffer_put_int(&msg, 0);
579 buffer_put_int(&e->output, buffer_len(&msg));
580 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
586 process_add_smartcard_key (SocketEntry *e)
588 char *sc_reader_id = NULL, *pin;
589 int i, version, success = 0, death = 0, confirm = 0;
594 sc_reader_id = buffer_get_string(&e->request, NULL);
595 pin = buffer_get_string(&e->request, NULL);
597 while (buffer_len(&e->request)) {
598 switch (buffer_get_char(&e->request)) {
599 case SSH_AGENT_CONSTRAIN_LIFETIME:
600 death = time(NULL) + buffer_get_int(&e->request);
602 case SSH_AGENT_CONSTRAIN_CONFIRM:
609 if (lifetime && !death)
610 death = time(NULL) + lifetime;
612 keys = sc_get_keys(sc_reader_id, pin);
616 if (keys == NULL || keys[0] == NULL) {
617 error("sc_get_keys failed");
620 for (i = 0; keys[i] != NULL; i++) {
622 version = k->type == KEY_RSA1 ? 1 : 2;
623 tab = idtab_lookup(version);
624 if (lookup_identity(k, version) == NULL) {
625 id = xmalloc(sizeof(Identity));
627 id->comment = sc_get_key_label(k);
629 id->confirm = confirm;
630 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
640 buffer_put_int(&e->output, 1);
641 buffer_put_char(&e->output,
642 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
646 process_remove_smartcard_key(SocketEntry *e)
648 char *sc_reader_id = NULL, *pin;
649 int i, version, success = 0;
650 Key **keys, *k = NULL;
654 sc_reader_id = buffer_get_string(&e->request, NULL);
655 pin = buffer_get_string(&e->request, NULL);
656 keys = sc_get_keys(sc_reader_id, pin);
660 if (keys == NULL || keys[0] == NULL) {
661 error("sc_get_keys failed");
664 for (i = 0; keys[i] != NULL; i++) {
666 version = k->type == KEY_RSA1 ? 1 : 2;
667 if ((id = lookup_identity(k, version)) != NULL) {
668 tab = idtab_lookup(version);
669 TAILQ_REMOVE(&tab->idlist, id, next);
679 buffer_put_int(&e->output, 1);
680 buffer_put_char(&e->output,
681 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
683 #endif /* SMARTCARD */
685 /* dispatch incoming messages */
688 process_message(SocketEntry *e)
696 if (buffer_len(&e->input) < 5)
697 return; /* Incomplete message. */
698 cp = buffer_ptr(&e->input);
699 msg_len = get_u32(cp);
700 if (msg_len > 256 * 1024) {
704 if (buffer_len(&e->input) < msg_len + 4)
707 /* move the current input to e->request */
708 buffer_consume(&e->input, 4);
709 buffer_clear(&e->request);
710 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
711 buffer_consume(&e->input, msg_len);
712 type = buffer_get_char(&e->request);
714 /* check wheter agent is locked */
715 if (locked && type != SSH_AGENTC_UNLOCK) {
716 buffer_clear(&e->request);
718 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
719 case SSH2_AGENTC_REQUEST_IDENTITIES:
720 /* send empty lists */
721 no_identities(e, type);
724 /* send a fail message for all other request types */
725 buffer_put_int(&e->output, 1);
726 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
731 debug("type %d", type);
733 case SSH_AGENTC_LOCK:
734 case SSH_AGENTC_UNLOCK:
735 process_lock_agent(e, type == SSH_AGENTC_LOCK);
738 case SSH_AGENTC_RSA_CHALLENGE:
739 process_authentication_challenge1(e);
741 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
742 process_request_identities(e, 1);
744 case SSH_AGENTC_ADD_RSA_IDENTITY:
745 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
746 process_add_identity(e, 1);
748 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
749 process_remove_identity(e, 1);
751 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
752 process_remove_all_identities(e, 1);
755 case SSH2_AGENTC_SIGN_REQUEST:
756 process_sign_request2(e);
758 case SSH2_AGENTC_REQUEST_IDENTITIES:
759 process_request_identities(e, 2);
761 case SSH2_AGENTC_ADD_IDENTITY:
762 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
763 process_add_identity(e, 2);
765 case SSH2_AGENTC_REMOVE_IDENTITY:
766 process_remove_identity(e, 2);
768 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
769 process_remove_all_identities(e, 2);
772 case SSH_AGENTC_ADD_SMARTCARD_KEY:
773 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
774 process_add_smartcard_key(e);
776 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
777 process_remove_smartcard_key(e);
779 #endif /* SMARTCARD */
781 /* Unknown message. Respond with failure. */
782 error("Unknown message %d", type);
783 buffer_clear(&e->request);
784 buffer_put_int(&e->output, 1);
785 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
791 new_socket(sock_type type, int fd)
793 u_int i, old_alloc, new_alloc;
800 for (i = 0; i < sockets_alloc; i++)
801 if (sockets[i].type == AUTH_UNUSED) {
803 buffer_init(&sockets[i].input);
804 buffer_init(&sockets[i].output);
805 buffer_init(&sockets[i].request);
806 sockets[i].type = type;
809 old_alloc = sockets_alloc;
810 new_alloc = sockets_alloc + 10;
811 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
812 for (i = old_alloc; i < new_alloc; i++)
813 sockets[i].type = AUTH_UNUSED;
814 sockets_alloc = new_alloc;
815 sockets[old_alloc].fd = fd;
816 buffer_init(&sockets[old_alloc].input);
817 buffer_init(&sockets[old_alloc].output);
818 buffer_init(&sockets[old_alloc].request);
819 sockets[old_alloc].type = type;
823 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
828 for (i = 0; i < sockets_alloc; i++) {
829 switch (sockets[i].type) {
831 case AUTH_CONNECTION:
832 n = MAX(n, sockets[i].fd);
837 fatal("Unknown socket type %d", sockets[i].type);
842 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
843 if (*fdrp == NULL || sz > *nallocp) {
853 debug("XXX shrink: %d < %d", n, *fdl);
855 memset(*fdrp, 0, sz);
856 memset(*fdwp, 0, sz);
858 for (i = 0; i < sockets_alloc; i++) {
859 switch (sockets[i].type) {
861 case AUTH_CONNECTION:
862 FD_SET(sockets[i].fd, *fdrp);
863 if (buffer_len(&sockets[i].output) > 0)
864 FD_SET(sockets[i].fd, *fdwp);
874 after_select(fd_set *readset, fd_set *writeset)
876 struct sockaddr_un sunaddr;
884 for (i = 0; i < sockets_alloc; i++)
885 switch (sockets[i].type) {
889 if (FD_ISSET(sockets[i].fd, readset)) {
890 slen = sizeof(sunaddr);
891 sock = accept(sockets[i].fd,
892 (struct sockaddr *)&sunaddr, &slen);
894 error("accept from AUTH_SOCKET: %s",
898 if (getpeereid(sock, &euid, &egid) < 0) {
899 error("getpeereid %d failed: %s",
900 sock, strerror(errno));
904 if ((euid != 0) && (getuid() != euid)) {
905 error("uid mismatch: "
906 "peer euid %u != uid %u",
907 (u_int) euid, (u_int) getuid());
911 new_socket(AUTH_CONNECTION, sock);
914 case AUTH_CONNECTION:
915 if (buffer_len(&sockets[i].output) > 0 &&
916 FD_ISSET(sockets[i].fd, writeset)) {
918 len = write(sockets[i].fd,
919 buffer_ptr(&sockets[i].output),
920 buffer_len(&sockets[i].output));
921 if (len == -1 && (errno == EAGAIN ||
927 close_socket(&sockets[i]);
930 buffer_consume(&sockets[i].output, len);
932 if (FD_ISSET(sockets[i].fd, readset)) {
934 len = read(sockets[i].fd, buf, sizeof(buf));
935 if (len == -1 && (errno == EAGAIN ||
941 close_socket(&sockets[i]);
944 buffer_append(&sockets[i].input, buf, len);
945 process_message(&sockets[i]);
949 fatal("Unknown type %d", sockets[i].type);
971 cleanup_handler(int sig)
979 check_parent_exists(int sig)
981 int save_errno = errno;
983 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
984 /* printf("Parent has died - Authentication agent exiting.\n"); */
985 cleanup_handler(sig); /* safe */
987 mysignal(SIGALRM, check_parent_exists);
995 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
997 fprintf(stderr, "Options:\n");
998 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
999 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1000 fprintf(stderr, " -k Kill the current agent.\n");
1001 fprintf(stderr, " -d Debug mode.\n");
1002 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
1003 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
1008 main(int ac, char **av)
1010 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1013 char *shell, *format, *pidstr, *agentsocket = NULL;
1014 fd_set *readsetp = NULL, *writesetp = NULL;
1015 struct sockaddr_un sunaddr;
1016 #ifdef HAVE_SETRLIMIT
1021 extern char *optarg;
1023 char pidstrbuf[1 + 3 * sizeof pid];
1025 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1032 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1033 /* Disable ptrace on Linux without sgid bit */
1034 prctl(PR_SET_DUMPABLE, 0);
1037 SSLeay_add_all_algorithms();
1039 __progname = ssh_get_progname(av[0]);
1043 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
1064 agentsocket = optarg;
1067 if ((lifetime = convtime(optarg)) == -1) {
1068 fprintf(stderr, "Invalid lifetime\n");
1079 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1082 if (ac == 0 && !c_flag && !s_flag) {
1083 shell = getenv("SHELL");
1084 if (shell != NULL &&
1085 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1089 const char *errstr = NULL;
1091 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1092 if (pidstr == NULL) {
1093 fprintf(stderr, "%s not set, cannot kill agent\n",
1094 SSH_AGENTPID_ENV_NAME);
1097 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1100 "%s=\"%s\", which is not a good PID: %s\n",
1101 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
1104 if (kill(pid, SIGTERM) == -1) {
1108 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1109 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1110 printf(format, SSH_AGENTPID_ENV_NAME);
1111 printf("echo Agent pid %ld killed;\n", (long)pid);
1114 parent_pid = getpid();
1116 if (agentsocket == NULL) {
1117 /* Create private directory for agent socket */
1118 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
1119 if (mkdtemp(socket_dir) == NULL) {
1120 perror("mkdtemp: private socket dir");
1123 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1126 /* Try to use specified agent socket */
1127 socket_dir[0] = '\0';
1128 strlcpy(socket_name, agentsocket, sizeof socket_name);
1132 * Create socket early so it will exist before command gets run from
1135 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1138 *socket_name = '\0'; /* Don't unlink any existing file */
1141 memset(&sunaddr, 0, sizeof(sunaddr));
1142 sunaddr.sun_family = AF_UNIX;
1143 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1144 prev_mask = umask(0177);
1145 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
1147 *socket_name = '\0'; /* Don't unlink any existing file */
1152 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1158 * Fork, and have the parent execute the command, if any, or present
1159 * the socket data. The child continues as the authentication agent.
1162 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1163 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1164 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1165 SSH_AUTHSOCKET_ENV_NAME);
1166 printf("echo Agent pid %ld;\n", (long)parent_pid);
1174 if (pid != 0) { /* Parent - execute the given command. */
1176 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1178 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1179 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1180 SSH_AUTHSOCKET_ENV_NAME);
1181 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1182 SSH_AGENTPID_ENV_NAME);
1183 printf("echo Agent pid %ld;\n", (long)pid);
1186 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1187 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1196 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1198 if (setsid() == -1) {
1199 error("setsid: %s", strerror(errno));
1204 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1205 /* XXX might close listen socket */
1206 (void)dup2(fd, STDIN_FILENO);
1207 (void)dup2(fd, STDOUT_FILENO);
1208 (void)dup2(fd, STDERR_FILENO);
1213 #ifdef HAVE_SETRLIMIT
1214 /* deny core dumps, since memory contains unencrypted private keys */
1215 rlim.rlim_cur = rlim.rlim_max = 0;
1216 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1217 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1223 new_socket(AUTH_SOCKET, sock);
1225 mysignal(SIGALRM, check_parent_exists);
1230 signal(SIGINT, SIG_IGN);
1231 signal(SIGPIPE, SIG_IGN);
1232 signal(SIGHUP, cleanup_handler);
1233 signal(SIGTERM, cleanup_handler);
1237 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1238 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1241 fatal("select: %s", strerror(errno));
1243 after_select(readsetp, writesetp);