1 /* $OpenBSD: ssh-agent.c,v 1.141 2006/07/08 21:47:12 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>
53 #include <openssl/evp.h>
54 #include <openssl/md5.h>
71 #if defined(HAVE_SYS_PRCTL_H)
72 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
89 u_int sockets_alloc = 0;
90 SocketEntry *sockets = NULL;
92 typedef struct identity {
93 TAILQ_ENTRY(identity) next;
102 TAILQ_HEAD(idqueue, identity) idlist;
105 /* private key table, one per protocol version */
110 /* pid of shell == parent of agent */
111 pid_t parent_pid = -1;
113 /* pathname and directory for AUTH_SOCKET */
114 char socket_name[MAXPATHLEN];
115 char socket_dir[MAXPATHLEN];
119 char *lock_passwd = NULL;
121 extern char *__progname;
123 /* Default lifetime (0 == forever) */
124 static int lifetime = 0;
127 close_socket(SocketEntry *e)
131 e->type = AUTH_UNUSED;
132 buffer_free(&e->input);
133 buffer_free(&e->output);
134 buffer_free(&e->request);
142 for (i = 0; i <=2; i++) {
143 TAILQ_INIT(&idtable[i].idlist);
144 idtable[i].nentries = 0;
148 /* return private key table for requested protocol version */
150 idtab_lookup(int version)
152 if (version < 1 || version > 2)
153 fatal("internal error, bad protocol version %d", version);
154 return &idtable[version];
158 free_identity(Identity *id)
165 /* return matching private key for given public key */
167 lookup_identity(Key *key, int version)
171 Idtab *tab = idtab_lookup(version);
172 TAILQ_FOREACH(id, &tab->idlist, next) {
173 if (key_equal(key, id->key))
179 /* Check confirmation of keysign request */
181 confirm_key(Identity *id)
186 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
187 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
195 /* send list of supported public keys to 'client' */
197 process_request_identities(SocketEntry *e, int version)
199 Idtab *tab = idtab_lookup(version);
204 buffer_put_char(&msg, (version == 1) ?
205 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
206 buffer_put_int(&msg, tab->nentries);
207 TAILQ_FOREACH(id, &tab->idlist, next) {
208 if (id->key->type == KEY_RSA1) {
209 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
210 buffer_put_bignum(&msg, id->key->rsa->e);
211 buffer_put_bignum(&msg, id->key->rsa->n);
215 key_to_blob(id->key, &blob, &blen);
216 buffer_put_string(&msg, blob, blen);
219 buffer_put_cstring(&msg, id->comment);
221 buffer_put_int(&e->output, buffer_len(&msg));
222 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
228 process_authentication_challenge1(SocketEntry *e)
230 u_char buf[32], mdbuf[16], session_id[16];
240 key = key_new(KEY_RSA1);
241 if ((challenge = BN_new()) == NULL)
242 fatal("process_authentication_challenge1: BN_new failed");
244 (void) buffer_get_int(&e->request); /* ignored */
245 buffer_get_bignum(&e->request, key->rsa->e);
246 buffer_get_bignum(&e->request, key->rsa->n);
247 buffer_get_bignum(&e->request, challenge);
249 /* Only protocol 1.1 is supported */
250 if (buffer_len(&e->request) == 0)
252 buffer_get(&e->request, session_id, 16);
253 response_type = buffer_get_int(&e->request);
254 if (response_type != 1)
257 id = lookup_identity(key, 1);
258 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
259 Key *private = id->key;
260 /* Decrypt the challenge using the private key. */
261 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
264 /* The response is MD5 of decrypted challenge plus session id. */
265 len = BN_num_bytes(challenge);
266 if (len <= 0 || len > 32) {
267 logit("process_authentication_challenge: bad challenge length %d", len);
271 BN_bn2bin(challenge, buf + 32 - len);
273 MD5_Update(&md, buf, 32);
274 MD5_Update(&md, session_id, 16);
275 MD5_Final(mdbuf, &md);
277 /* Send the response. */
278 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
279 for (i = 0; i < 16; i++)
280 buffer_put_char(&msg, mdbuf[i]);
285 /* Unknown identity or protocol error. Send failure. */
286 buffer_put_char(&msg, SSH_AGENT_FAILURE);
288 buffer_put_int(&e->output, buffer_len(&msg));
289 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
291 BN_clear_free(challenge);
297 process_sign_request2(SocketEntry *e)
299 u_char *blob, *data, *signature = NULL;
300 u_int blen, dlen, slen = 0;
301 extern int datafellows;
308 blob = buffer_get_string(&e->request, &blen);
309 data = buffer_get_string(&e->request, &dlen);
311 flags = buffer_get_int(&e->request);
312 if (flags & SSH_AGENT_OLD_SIGNATURE)
313 datafellows = SSH_BUG_SIGBLOB;
315 key = key_from_blob(blob, blen);
317 Identity *id = lookup_identity(key, 2);
318 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
319 ok = key_sign(id->key, &signature, &slen, data, dlen);
324 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
325 buffer_put_string(&msg, signature, slen);
327 buffer_put_char(&msg, SSH_AGENT_FAILURE);
329 buffer_put_int(&e->output, buffer_len(&msg));
330 buffer_append(&e->output, buffer_ptr(&msg),
335 if (signature != NULL)
341 process_remove_identity(SocketEntry *e, int version)
350 key = key_new(KEY_RSA1);
351 bits = buffer_get_int(&e->request);
352 buffer_get_bignum(&e->request, key->rsa->e);
353 buffer_get_bignum(&e->request, key->rsa->n);
355 if (bits != key_size(key))
356 logit("Warning: identity keysize mismatch: actual %u, announced %u",
357 key_size(key), bits);
360 blob = buffer_get_string(&e->request, &blen);
361 key = key_from_blob(blob, blen);
366 Identity *id = lookup_identity(key, version);
369 * We have this key. Free the old key. Since we
370 * don't want to leave empty slots in the middle of
371 * the array, we actually free the key there and move
372 * all the entries between the empty slot and the end
375 Idtab *tab = idtab_lookup(version);
376 if (tab->nentries < 1)
377 fatal("process_remove_identity: "
378 "internal error: tab->nentries %d",
380 TAILQ_REMOVE(&tab->idlist, id, next);
387 buffer_put_int(&e->output, 1);
388 buffer_put_char(&e->output,
389 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
393 process_remove_all_identities(SocketEntry *e, int version)
395 Idtab *tab = idtab_lookup(version);
398 /* Loop over all identities and clear the keys. */
399 for (id = TAILQ_FIRST(&tab->idlist); id;
400 id = TAILQ_FIRST(&tab->idlist)) {
401 TAILQ_REMOVE(&tab->idlist, id, next);
405 /* Mark that there are no identities. */
409 buffer_put_int(&e->output, 1);
410 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
416 u_int now = time(NULL);
421 for (version = 1; version < 3; version++) {
422 tab = idtab_lookup(version);
423 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
424 nxt = TAILQ_NEXT(id, next);
425 if (id->death != 0 && now >= id->death) {
426 TAILQ_REMOVE(&tab->idlist, id, next);
435 process_add_identity(SocketEntry *e, int version)
437 Idtab *tab = idtab_lookup(version);
438 int type, success = 0, death = 0, confirm = 0;
439 char *type_name, *comment;
444 k = key_new_private(KEY_RSA1);
445 (void) buffer_get_int(&e->request); /* ignored */
446 buffer_get_bignum(&e->request, k->rsa->n);
447 buffer_get_bignum(&e->request, k->rsa->e);
448 buffer_get_bignum(&e->request, k->rsa->d);
449 buffer_get_bignum(&e->request, k->rsa->iqmp);
451 /* SSH and SSL have p and q swapped */
452 buffer_get_bignum(&e->request, k->rsa->q); /* p */
453 buffer_get_bignum(&e->request, k->rsa->p); /* q */
455 /* Generate additional parameters */
456 rsa_generate_additional_parameters(k->rsa);
459 type_name = buffer_get_string(&e->request, NULL);
460 type = key_type_from_name(type_name);
464 k = key_new_private(type);
465 buffer_get_bignum2(&e->request, k->dsa->p);
466 buffer_get_bignum2(&e->request, k->dsa->q);
467 buffer_get_bignum2(&e->request, k->dsa->g);
468 buffer_get_bignum2(&e->request, k->dsa->pub_key);
469 buffer_get_bignum2(&e->request, k->dsa->priv_key);
472 k = key_new_private(type);
473 buffer_get_bignum2(&e->request, k->rsa->n);
474 buffer_get_bignum2(&e->request, k->rsa->e);
475 buffer_get_bignum2(&e->request, k->rsa->d);
476 buffer_get_bignum2(&e->request, k->rsa->iqmp);
477 buffer_get_bignum2(&e->request, k->rsa->p);
478 buffer_get_bignum2(&e->request, k->rsa->q);
480 /* Generate additional parameters */
481 rsa_generate_additional_parameters(k->rsa);
484 buffer_clear(&e->request);
489 /* enable blinding */
493 if (RSA_blinding_on(k->rsa, NULL) != 1) {
494 error("process_add_identity: RSA_blinding_on failed");
500 comment = buffer_get_string(&e->request, NULL);
506 while (buffer_len(&e->request)) {
507 switch (buffer_get_char(&e->request)) {
508 case SSH_AGENT_CONSTRAIN_LIFETIME:
509 death = time(NULL) + buffer_get_int(&e->request);
511 case SSH_AGENT_CONSTRAIN_CONFIRM:
518 if (lifetime && !death)
519 death = time(NULL) + lifetime;
520 if (lookup_identity(k, version) == NULL) {
521 Identity *id = xmalloc(sizeof(Identity));
523 id->comment = comment;
525 id->confirm = confirm;
526 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
527 /* Increment the number of identities. */
534 buffer_put_int(&e->output, 1);
535 buffer_put_char(&e->output,
536 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
539 /* XXX todo: encrypt sensitive data with passphrase */
541 process_lock_agent(SocketEntry *e, int lock)
546 passwd = buffer_get_string(&e->request, NULL);
547 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
549 memset(lock_passwd, 0, strlen(lock_passwd));
553 } else if (!locked && lock) {
555 lock_passwd = xstrdup(passwd);
558 memset(passwd, 0, strlen(passwd));
561 buffer_put_int(&e->output, 1);
562 buffer_put_char(&e->output,
563 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
567 no_identities(SocketEntry *e, u_int type)
572 buffer_put_char(&msg,
573 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
574 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
575 buffer_put_int(&msg, 0);
576 buffer_put_int(&e->output, buffer_len(&msg));
577 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
583 process_add_smartcard_key (SocketEntry *e)
585 char *sc_reader_id = NULL, *pin;
586 int i, version, success = 0, death = 0, confirm = 0;
591 sc_reader_id = buffer_get_string(&e->request, NULL);
592 pin = buffer_get_string(&e->request, NULL);
594 while (buffer_len(&e->request)) {
595 switch (buffer_get_char(&e->request)) {
596 case SSH_AGENT_CONSTRAIN_LIFETIME:
597 death = time(NULL) + buffer_get_int(&e->request);
599 case SSH_AGENT_CONSTRAIN_CONFIRM:
606 if (lifetime && !death)
607 death = time(NULL) + lifetime;
609 keys = sc_get_keys(sc_reader_id, pin);
613 if (keys == NULL || keys[0] == NULL) {
614 error("sc_get_keys failed");
617 for (i = 0; keys[i] != NULL; i++) {
619 version = k->type == KEY_RSA1 ? 1 : 2;
620 tab = idtab_lookup(version);
621 if (lookup_identity(k, version) == NULL) {
622 id = xmalloc(sizeof(Identity));
624 id->comment = sc_get_key_label(k);
626 id->confirm = confirm;
627 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
637 buffer_put_int(&e->output, 1);
638 buffer_put_char(&e->output,
639 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
643 process_remove_smartcard_key(SocketEntry *e)
645 char *sc_reader_id = NULL, *pin;
646 int i, version, success = 0;
647 Key **keys, *k = NULL;
651 sc_reader_id = buffer_get_string(&e->request, NULL);
652 pin = buffer_get_string(&e->request, NULL);
653 keys = sc_get_keys(sc_reader_id, pin);
657 if (keys == NULL || keys[0] == NULL) {
658 error("sc_get_keys failed");
661 for (i = 0; keys[i] != NULL; i++) {
663 version = k->type == KEY_RSA1 ? 1 : 2;
664 if ((id = lookup_identity(k, version)) != NULL) {
665 tab = idtab_lookup(version);
666 TAILQ_REMOVE(&tab->idlist, id, next);
676 buffer_put_int(&e->output, 1);
677 buffer_put_char(&e->output,
678 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
680 #endif /* SMARTCARD */
682 /* dispatch incoming messages */
685 process_message(SocketEntry *e)
693 if (buffer_len(&e->input) < 5)
694 return; /* Incomplete message. */
695 cp = buffer_ptr(&e->input);
696 msg_len = get_u32(cp);
697 if (msg_len > 256 * 1024) {
701 if (buffer_len(&e->input) < msg_len + 4)
704 /* move the current input to e->request */
705 buffer_consume(&e->input, 4);
706 buffer_clear(&e->request);
707 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
708 buffer_consume(&e->input, msg_len);
709 type = buffer_get_char(&e->request);
711 /* check wheter agent is locked */
712 if (locked && type != SSH_AGENTC_UNLOCK) {
713 buffer_clear(&e->request);
715 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
716 case SSH2_AGENTC_REQUEST_IDENTITIES:
717 /* send empty lists */
718 no_identities(e, type);
721 /* send a fail message for all other request types */
722 buffer_put_int(&e->output, 1);
723 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
728 debug("type %d", type);
730 case SSH_AGENTC_LOCK:
731 case SSH_AGENTC_UNLOCK:
732 process_lock_agent(e, type == SSH_AGENTC_LOCK);
735 case SSH_AGENTC_RSA_CHALLENGE:
736 process_authentication_challenge1(e);
738 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
739 process_request_identities(e, 1);
741 case SSH_AGENTC_ADD_RSA_IDENTITY:
742 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
743 process_add_identity(e, 1);
745 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
746 process_remove_identity(e, 1);
748 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
749 process_remove_all_identities(e, 1);
752 case SSH2_AGENTC_SIGN_REQUEST:
753 process_sign_request2(e);
755 case SSH2_AGENTC_REQUEST_IDENTITIES:
756 process_request_identities(e, 2);
758 case SSH2_AGENTC_ADD_IDENTITY:
759 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
760 process_add_identity(e, 2);
762 case SSH2_AGENTC_REMOVE_IDENTITY:
763 process_remove_identity(e, 2);
765 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
766 process_remove_all_identities(e, 2);
769 case SSH_AGENTC_ADD_SMARTCARD_KEY:
770 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
771 process_add_smartcard_key(e);
773 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
774 process_remove_smartcard_key(e);
776 #endif /* SMARTCARD */
778 /* Unknown message. Respond with failure. */
779 error("Unknown message %d", type);
780 buffer_clear(&e->request);
781 buffer_put_int(&e->output, 1);
782 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
788 new_socket(sock_type type, int fd)
790 u_int i, old_alloc, new_alloc;
797 for (i = 0; i < sockets_alloc; i++)
798 if (sockets[i].type == AUTH_UNUSED) {
800 buffer_init(&sockets[i].input);
801 buffer_init(&sockets[i].output);
802 buffer_init(&sockets[i].request);
803 sockets[i].type = type;
806 old_alloc = sockets_alloc;
807 new_alloc = sockets_alloc + 10;
808 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
809 for (i = old_alloc; i < new_alloc; i++)
810 sockets[i].type = AUTH_UNUSED;
811 sockets_alloc = new_alloc;
812 sockets[old_alloc].fd = fd;
813 buffer_init(&sockets[old_alloc].input);
814 buffer_init(&sockets[old_alloc].output);
815 buffer_init(&sockets[old_alloc].request);
816 sockets[old_alloc].type = type;
820 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
825 for (i = 0; i < sockets_alloc; i++) {
826 switch (sockets[i].type) {
828 case AUTH_CONNECTION:
829 n = MAX(n, sockets[i].fd);
834 fatal("Unknown socket type %d", sockets[i].type);
839 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
840 if (*fdrp == NULL || sz > *nallocp) {
850 debug("XXX shrink: %d < %d", n, *fdl);
852 memset(*fdrp, 0, sz);
853 memset(*fdwp, 0, sz);
855 for (i = 0; i < sockets_alloc; i++) {
856 switch (sockets[i].type) {
858 case AUTH_CONNECTION:
859 FD_SET(sockets[i].fd, *fdrp);
860 if (buffer_len(&sockets[i].output) > 0)
861 FD_SET(sockets[i].fd, *fdwp);
871 after_select(fd_set *readset, fd_set *writeset)
873 struct sockaddr_un sunaddr;
881 for (i = 0; i < sockets_alloc; i++)
882 switch (sockets[i].type) {
886 if (FD_ISSET(sockets[i].fd, readset)) {
887 slen = sizeof(sunaddr);
888 sock = accept(sockets[i].fd,
889 (struct sockaddr *)&sunaddr, &slen);
891 error("accept from AUTH_SOCKET: %s",
895 if (getpeereid(sock, &euid, &egid) < 0) {
896 error("getpeereid %d failed: %s",
897 sock, strerror(errno));
901 if ((euid != 0) && (getuid() != euid)) {
902 error("uid mismatch: "
903 "peer euid %u != uid %u",
904 (u_int) euid, (u_int) getuid());
908 new_socket(AUTH_CONNECTION, sock);
911 case AUTH_CONNECTION:
912 if (buffer_len(&sockets[i].output) > 0 &&
913 FD_ISSET(sockets[i].fd, writeset)) {
915 len = write(sockets[i].fd,
916 buffer_ptr(&sockets[i].output),
917 buffer_len(&sockets[i].output));
918 if (len == -1 && (errno == EAGAIN ||
924 close_socket(&sockets[i]);
927 buffer_consume(&sockets[i].output, len);
929 if (FD_ISSET(sockets[i].fd, readset)) {
931 len = read(sockets[i].fd, buf, sizeof(buf));
932 if (len == -1 && (errno == EAGAIN ||
938 close_socket(&sockets[i]);
941 buffer_append(&sockets[i].input, buf, len);
942 process_message(&sockets[i]);
946 fatal("Unknown type %d", sockets[i].type);
968 cleanup_handler(int sig)
976 check_parent_exists(int sig)
978 int save_errno = errno;
980 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
981 /* printf("Parent has died - Authentication agent exiting.\n"); */
982 cleanup_handler(sig); /* safe */
984 mysignal(SIGALRM, check_parent_exists);
992 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
994 fprintf(stderr, "Options:\n");
995 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
996 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
997 fprintf(stderr, " -k Kill the current agent.\n");
998 fprintf(stderr, " -d Debug mode.\n");
999 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
1000 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
1005 main(int ac, char **av)
1007 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1010 char *shell, *format, *pidstr, *agentsocket = NULL;
1011 fd_set *readsetp = NULL, *writesetp = NULL;
1012 struct sockaddr_un sunaddr;
1013 #ifdef HAVE_SETRLIMIT
1018 extern char *optarg;
1020 char pidstrbuf[1 + 3 * sizeof pid];
1022 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1029 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1030 /* Disable ptrace on Linux without sgid bit */
1031 prctl(PR_SET_DUMPABLE, 0);
1034 SSLeay_add_all_algorithms();
1036 __progname = ssh_get_progname(av[0]);
1040 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
1061 agentsocket = optarg;
1064 if ((lifetime = convtime(optarg)) == -1) {
1065 fprintf(stderr, "Invalid lifetime\n");
1076 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1079 if (ac == 0 && !c_flag && !s_flag) {
1080 shell = getenv("SHELL");
1081 if (shell != NULL &&
1082 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1086 const char *errstr = NULL;
1088 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1089 if (pidstr == NULL) {
1090 fprintf(stderr, "%s not set, cannot kill agent\n",
1091 SSH_AGENTPID_ENV_NAME);
1094 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1097 "%s=\"%s\", which is not a good PID: %s\n",
1098 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
1101 if (kill(pid, SIGTERM) == -1) {
1105 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1106 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1107 printf(format, SSH_AGENTPID_ENV_NAME);
1108 printf("echo Agent pid %ld killed;\n", (long)pid);
1111 parent_pid = getpid();
1113 if (agentsocket == NULL) {
1114 /* Create private directory for agent socket */
1115 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
1116 if (mkdtemp(socket_dir) == NULL) {
1117 perror("mkdtemp: private socket dir");
1120 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1123 /* Try to use specified agent socket */
1124 socket_dir[0] = '\0';
1125 strlcpy(socket_name, agentsocket, sizeof socket_name);
1129 * Create socket early so it will exist before command gets run from
1132 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1135 *socket_name = '\0'; /* Don't unlink any existing file */
1138 memset(&sunaddr, 0, sizeof(sunaddr));
1139 sunaddr.sun_family = AF_UNIX;
1140 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1141 prev_mask = umask(0177);
1142 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
1144 *socket_name = '\0'; /* Don't unlink any existing file */
1149 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1155 * Fork, and have the parent execute the command, if any, or present
1156 * the socket data. The child continues as the authentication agent.
1159 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1160 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1161 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1162 SSH_AUTHSOCKET_ENV_NAME);
1163 printf("echo Agent pid %ld;\n", (long)parent_pid);
1171 if (pid != 0) { /* Parent - execute the given command. */
1173 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1175 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1176 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1177 SSH_AUTHSOCKET_ENV_NAME);
1178 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1179 SSH_AGENTPID_ENV_NAME);
1180 printf("echo Agent pid %ld;\n", (long)pid);
1183 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1184 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1193 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1195 if (setsid() == -1) {
1196 error("setsid: %s", strerror(errno));
1201 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1202 /* XXX might close listen socket */
1203 (void)dup2(fd, STDIN_FILENO);
1204 (void)dup2(fd, STDOUT_FILENO);
1205 (void)dup2(fd, STDERR_FILENO);
1210 #ifdef HAVE_SETRLIMIT
1211 /* deny core dumps, since memory contains unencrypted private keys */
1212 rlim.rlim_cur = rlim.rlim_max = 0;
1213 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1214 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1220 new_socket(AUTH_SOCKET, sock);
1222 mysignal(SIGALRM, check_parent_exists);
1227 signal(SIGINT, SIG_IGN);
1228 signal(SIGPIPE, SIG_IGN);
1229 signal(SIGHUP, cleanup_handler);
1230 signal(SIGTERM, cleanup_handler);
1234 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1235 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1238 fatal("select: %s", strerror(errno));
1240 after_select(readsetp, writesetp);