1 /* $OpenBSD: ssh-agent.c,v 1.143 2006/07/11 20:07:25 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>
55 #include <openssl/evp.h>
56 #include <openssl/md5.h>
73 #if defined(HAVE_SYS_PRCTL_H)
74 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
91 u_int sockets_alloc = 0;
92 SocketEntry *sockets = NULL;
94 typedef struct identity {
95 TAILQ_ENTRY(identity) next;
104 TAILQ_HEAD(idqueue, identity) idlist;
107 /* private key table, one per protocol version */
112 /* pid of shell == parent of agent */
113 pid_t parent_pid = -1;
115 /* pathname and directory for AUTH_SOCKET */
116 char socket_name[MAXPATHLEN];
117 char socket_dir[MAXPATHLEN];
121 char *lock_passwd = NULL;
123 extern char *__progname;
125 /* Default lifetime (0 == forever) */
126 static int lifetime = 0;
129 close_socket(SocketEntry *e)
133 e->type = AUTH_UNUSED;
134 buffer_free(&e->input);
135 buffer_free(&e->output);
136 buffer_free(&e->request);
144 for (i = 0; i <=2; i++) {
145 TAILQ_INIT(&idtable[i].idlist);
146 idtable[i].nentries = 0;
150 /* return private key table for requested protocol version */
152 idtab_lookup(int version)
154 if (version < 1 || version > 2)
155 fatal("internal error, bad protocol version %d", version);
156 return &idtable[version];
160 free_identity(Identity *id)
167 /* return matching private key for given public key */
169 lookup_identity(Key *key, int version)
173 Idtab *tab = idtab_lookup(version);
174 TAILQ_FOREACH(id, &tab->idlist, next) {
175 if (key_equal(key, id->key))
181 /* Check confirmation of keysign request */
183 confirm_key(Identity *id)
188 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
189 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
197 /* send list of supported public keys to 'client' */
199 process_request_identities(SocketEntry *e, int version)
201 Idtab *tab = idtab_lookup(version);
206 buffer_put_char(&msg, (version == 1) ?
207 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
208 buffer_put_int(&msg, tab->nentries);
209 TAILQ_FOREACH(id, &tab->idlist, next) {
210 if (id->key->type == KEY_RSA1) {
211 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
212 buffer_put_bignum(&msg, id->key->rsa->e);
213 buffer_put_bignum(&msg, id->key->rsa->n);
217 key_to_blob(id->key, &blob, &blen);
218 buffer_put_string(&msg, blob, blen);
221 buffer_put_cstring(&msg, id->comment);
223 buffer_put_int(&e->output, buffer_len(&msg));
224 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
230 process_authentication_challenge1(SocketEntry *e)
232 u_char buf[32], mdbuf[16], session_id[16];
242 key = key_new(KEY_RSA1);
243 if ((challenge = BN_new()) == NULL)
244 fatal("process_authentication_challenge1: BN_new failed");
246 (void) buffer_get_int(&e->request); /* ignored */
247 buffer_get_bignum(&e->request, key->rsa->e);
248 buffer_get_bignum(&e->request, key->rsa->n);
249 buffer_get_bignum(&e->request, challenge);
251 /* Only protocol 1.1 is supported */
252 if (buffer_len(&e->request) == 0)
254 buffer_get(&e->request, session_id, 16);
255 response_type = buffer_get_int(&e->request);
256 if (response_type != 1)
259 id = lookup_identity(key, 1);
260 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
261 Key *private = id->key;
262 /* Decrypt the challenge using the private key. */
263 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
266 /* The response is MD5 of decrypted challenge plus session id. */
267 len = BN_num_bytes(challenge);
268 if (len <= 0 || len > 32) {
269 logit("process_authentication_challenge: bad challenge length %d", len);
273 BN_bn2bin(challenge, buf + 32 - len);
275 MD5_Update(&md, buf, 32);
276 MD5_Update(&md, session_id, 16);
277 MD5_Final(mdbuf, &md);
279 /* Send the response. */
280 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
281 for (i = 0; i < 16; i++)
282 buffer_put_char(&msg, mdbuf[i]);
287 /* Unknown identity or protocol error. Send failure. */
288 buffer_put_char(&msg, SSH_AGENT_FAILURE);
290 buffer_put_int(&e->output, buffer_len(&msg));
291 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
293 BN_clear_free(challenge);
299 process_sign_request2(SocketEntry *e)
301 u_char *blob, *data, *signature = NULL;
302 u_int blen, dlen, slen = 0;
303 extern int datafellows;
310 blob = buffer_get_string(&e->request, &blen);
311 data = buffer_get_string(&e->request, &dlen);
313 flags = buffer_get_int(&e->request);
314 if (flags & SSH_AGENT_OLD_SIGNATURE)
315 datafellows = SSH_BUG_SIGBLOB;
317 key = key_from_blob(blob, blen);
319 Identity *id = lookup_identity(key, 2);
320 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
321 ok = key_sign(id->key, &signature, &slen, data, dlen);
326 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
327 buffer_put_string(&msg, signature, slen);
329 buffer_put_char(&msg, SSH_AGENT_FAILURE);
331 buffer_put_int(&e->output, buffer_len(&msg));
332 buffer_append(&e->output, buffer_ptr(&msg),
337 if (signature != NULL)
343 process_remove_identity(SocketEntry *e, int version)
352 key = key_new(KEY_RSA1);
353 bits = buffer_get_int(&e->request);
354 buffer_get_bignum(&e->request, key->rsa->e);
355 buffer_get_bignum(&e->request, key->rsa->n);
357 if (bits != key_size(key))
358 logit("Warning: identity keysize mismatch: actual %u, announced %u",
359 key_size(key), bits);
362 blob = buffer_get_string(&e->request, &blen);
363 key = key_from_blob(blob, blen);
368 Identity *id = lookup_identity(key, version);
371 * We have this key. Free the old key. Since we
372 * don't want to leave empty slots in the middle of
373 * the array, we actually free the key there and move
374 * all the entries between the empty slot and the end
377 Idtab *tab = idtab_lookup(version);
378 if (tab->nentries < 1)
379 fatal("process_remove_identity: "
380 "internal error: tab->nentries %d",
382 TAILQ_REMOVE(&tab->idlist, id, next);
389 buffer_put_int(&e->output, 1);
390 buffer_put_char(&e->output,
391 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
395 process_remove_all_identities(SocketEntry *e, int version)
397 Idtab *tab = idtab_lookup(version);
400 /* Loop over all identities and clear the keys. */
401 for (id = TAILQ_FIRST(&tab->idlist); id;
402 id = TAILQ_FIRST(&tab->idlist)) {
403 TAILQ_REMOVE(&tab->idlist, id, next);
407 /* Mark that there are no identities. */
411 buffer_put_int(&e->output, 1);
412 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
418 u_int now = time(NULL);
423 for (version = 1; version < 3; version++) {
424 tab = idtab_lookup(version);
425 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
426 nxt = TAILQ_NEXT(id, next);
427 if (id->death != 0 && now >= id->death) {
428 TAILQ_REMOVE(&tab->idlist, id, next);
437 process_add_identity(SocketEntry *e, int version)
439 Idtab *tab = idtab_lookup(version);
440 int type, success = 0, death = 0, confirm = 0;
441 char *type_name, *comment;
446 k = key_new_private(KEY_RSA1);
447 (void) buffer_get_int(&e->request); /* ignored */
448 buffer_get_bignum(&e->request, k->rsa->n);
449 buffer_get_bignum(&e->request, k->rsa->e);
450 buffer_get_bignum(&e->request, k->rsa->d);
451 buffer_get_bignum(&e->request, k->rsa->iqmp);
453 /* SSH and SSL have p and q swapped */
454 buffer_get_bignum(&e->request, k->rsa->q); /* p */
455 buffer_get_bignum(&e->request, k->rsa->p); /* q */
457 /* Generate additional parameters */
458 rsa_generate_additional_parameters(k->rsa);
461 type_name = buffer_get_string(&e->request, NULL);
462 type = key_type_from_name(type_name);
466 k = key_new_private(type);
467 buffer_get_bignum2(&e->request, k->dsa->p);
468 buffer_get_bignum2(&e->request, k->dsa->q);
469 buffer_get_bignum2(&e->request, k->dsa->g);
470 buffer_get_bignum2(&e->request, k->dsa->pub_key);
471 buffer_get_bignum2(&e->request, k->dsa->priv_key);
474 k = key_new_private(type);
475 buffer_get_bignum2(&e->request, k->rsa->n);
476 buffer_get_bignum2(&e->request, k->rsa->e);
477 buffer_get_bignum2(&e->request, k->rsa->d);
478 buffer_get_bignum2(&e->request, k->rsa->iqmp);
479 buffer_get_bignum2(&e->request, k->rsa->p);
480 buffer_get_bignum2(&e->request, k->rsa->q);
482 /* Generate additional parameters */
483 rsa_generate_additional_parameters(k->rsa);
486 buffer_clear(&e->request);
491 /* enable blinding */
495 if (RSA_blinding_on(k->rsa, NULL) != 1) {
496 error("process_add_identity: RSA_blinding_on failed");
502 comment = buffer_get_string(&e->request, NULL);
508 while (buffer_len(&e->request)) {
509 switch (buffer_get_char(&e->request)) {
510 case SSH_AGENT_CONSTRAIN_LIFETIME:
511 death = time(NULL) + buffer_get_int(&e->request);
513 case SSH_AGENT_CONSTRAIN_CONFIRM:
520 if (lifetime && !death)
521 death = time(NULL) + lifetime;
522 if (lookup_identity(k, version) == NULL) {
523 Identity *id = xmalloc(sizeof(Identity));
525 id->comment = comment;
527 id->confirm = confirm;
528 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
529 /* Increment the number of identities. */
536 buffer_put_int(&e->output, 1);
537 buffer_put_char(&e->output,
538 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
541 /* XXX todo: encrypt sensitive data with passphrase */
543 process_lock_agent(SocketEntry *e, int lock)
548 passwd = buffer_get_string(&e->request, NULL);
549 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
551 memset(lock_passwd, 0, strlen(lock_passwd));
555 } else if (!locked && lock) {
557 lock_passwd = xstrdup(passwd);
560 memset(passwd, 0, strlen(passwd));
563 buffer_put_int(&e->output, 1);
564 buffer_put_char(&e->output,
565 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
569 no_identities(SocketEntry *e, u_int type)
574 buffer_put_char(&msg,
575 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
576 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
577 buffer_put_int(&msg, 0);
578 buffer_put_int(&e->output, buffer_len(&msg));
579 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
585 process_add_smartcard_key (SocketEntry *e)
587 char *sc_reader_id = NULL, *pin;
588 int i, version, success = 0, death = 0, confirm = 0;
593 sc_reader_id = buffer_get_string(&e->request, NULL);
594 pin = buffer_get_string(&e->request, NULL);
596 while (buffer_len(&e->request)) {
597 switch (buffer_get_char(&e->request)) {
598 case SSH_AGENT_CONSTRAIN_LIFETIME:
599 death = time(NULL) + buffer_get_int(&e->request);
601 case SSH_AGENT_CONSTRAIN_CONFIRM:
608 if (lifetime && !death)
609 death = time(NULL) + lifetime;
611 keys = sc_get_keys(sc_reader_id, pin);
615 if (keys == NULL || keys[0] == NULL) {
616 error("sc_get_keys failed");
619 for (i = 0; keys[i] != NULL; i++) {
621 version = k->type == KEY_RSA1 ? 1 : 2;
622 tab = idtab_lookup(version);
623 if (lookup_identity(k, version) == NULL) {
624 id = xmalloc(sizeof(Identity));
626 id->comment = sc_get_key_label(k);
628 id->confirm = confirm;
629 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
639 buffer_put_int(&e->output, 1);
640 buffer_put_char(&e->output,
641 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
645 process_remove_smartcard_key(SocketEntry *e)
647 char *sc_reader_id = NULL, *pin;
648 int i, version, success = 0;
649 Key **keys, *k = NULL;
653 sc_reader_id = buffer_get_string(&e->request, NULL);
654 pin = buffer_get_string(&e->request, NULL);
655 keys = sc_get_keys(sc_reader_id, pin);
659 if (keys == NULL || keys[0] == NULL) {
660 error("sc_get_keys failed");
663 for (i = 0; keys[i] != NULL; i++) {
665 version = k->type == KEY_RSA1 ? 1 : 2;
666 if ((id = lookup_identity(k, version)) != NULL) {
667 tab = idtab_lookup(version);
668 TAILQ_REMOVE(&tab->idlist, id, next);
678 buffer_put_int(&e->output, 1);
679 buffer_put_char(&e->output,
680 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
682 #endif /* SMARTCARD */
684 /* dispatch incoming messages */
687 process_message(SocketEntry *e)
695 if (buffer_len(&e->input) < 5)
696 return; /* Incomplete message. */
697 cp = buffer_ptr(&e->input);
698 msg_len = get_u32(cp);
699 if (msg_len > 256 * 1024) {
703 if (buffer_len(&e->input) < msg_len + 4)
706 /* move the current input to e->request */
707 buffer_consume(&e->input, 4);
708 buffer_clear(&e->request);
709 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
710 buffer_consume(&e->input, msg_len);
711 type = buffer_get_char(&e->request);
713 /* check wheter agent is locked */
714 if (locked && type != SSH_AGENTC_UNLOCK) {
715 buffer_clear(&e->request);
717 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
718 case SSH2_AGENTC_REQUEST_IDENTITIES:
719 /* send empty lists */
720 no_identities(e, type);
723 /* send a fail message for all other request types */
724 buffer_put_int(&e->output, 1);
725 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
730 debug("type %d", type);
732 case SSH_AGENTC_LOCK:
733 case SSH_AGENTC_UNLOCK:
734 process_lock_agent(e, type == SSH_AGENTC_LOCK);
737 case SSH_AGENTC_RSA_CHALLENGE:
738 process_authentication_challenge1(e);
740 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
741 process_request_identities(e, 1);
743 case SSH_AGENTC_ADD_RSA_IDENTITY:
744 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
745 process_add_identity(e, 1);
747 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
748 process_remove_identity(e, 1);
750 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
751 process_remove_all_identities(e, 1);
754 case SSH2_AGENTC_SIGN_REQUEST:
755 process_sign_request2(e);
757 case SSH2_AGENTC_REQUEST_IDENTITIES:
758 process_request_identities(e, 2);
760 case SSH2_AGENTC_ADD_IDENTITY:
761 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
762 process_add_identity(e, 2);
764 case SSH2_AGENTC_REMOVE_IDENTITY:
765 process_remove_identity(e, 2);
767 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
768 process_remove_all_identities(e, 2);
771 case SSH_AGENTC_ADD_SMARTCARD_KEY:
772 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
773 process_add_smartcard_key(e);
775 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
776 process_remove_smartcard_key(e);
778 #endif /* SMARTCARD */
780 /* Unknown message. Respond with failure. */
781 error("Unknown message %d", type);
782 buffer_clear(&e->request);
783 buffer_put_int(&e->output, 1);
784 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
790 new_socket(sock_type type, int fd)
792 u_int i, old_alloc, new_alloc;
799 for (i = 0; i < sockets_alloc; i++)
800 if (sockets[i].type == AUTH_UNUSED) {
802 buffer_init(&sockets[i].input);
803 buffer_init(&sockets[i].output);
804 buffer_init(&sockets[i].request);
805 sockets[i].type = type;
808 old_alloc = sockets_alloc;
809 new_alloc = sockets_alloc + 10;
810 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
811 for (i = old_alloc; i < new_alloc; i++)
812 sockets[i].type = AUTH_UNUSED;
813 sockets_alloc = new_alloc;
814 sockets[old_alloc].fd = fd;
815 buffer_init(&sockets[old_alloc].input);
816 buffer_init(&sockets[old_alloc].output);
817 buffer_init(&sockets[old_alloc].request);
818 sockets[old_alloc].type = type;
822 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
827 for (i = 0; i < sockets_alloc; i++) {
828 switch (sockets[i].type) {
830 case AUTH_CONNECTION:
831 n = MAX(n, sockets[i].fd);
836 fatal("Unknown socket type %d", sockets[i].type);
841 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
842 if (*fdrp == NULL || sz > *nallocp) {
852 debug("XXX shrink: %d < %d", n, *fdl);
854 memset(*fdrp, 0, sz);
855 memset(*fdwp, 0, sz);
857 for (i = 0; i < sockets_alloc; i++) {
858 switch (sockets[i].type) {
860 case AUTH_CONNECTION:
861 FD_SET(sockets[i].fd, *fdrp);
862 if (buffer_len(&sockets[i].output) > 0)
863 FD_SET(sockets[i].fd, *fdwp);
873 after_select(fd_set *readset, fd_set *writeset)
875 struct sockaddr_un sunaddr;
883 for (i = 0; i < sockets_alloc; i++)
884 switch (sockets[i].type) {
888 if (FD_ISSET(sockets[i].fd, readset)) {
889 slen = sizeof(sunaddr);
890 sock = accept(sockets[i].fd,
891 (struct sockaddr *)&sunaddr, &slen);
893 error("accept from AUTH_SOCKET: %s",
897 if (getpeereid(sock, &euid, &egid) < 0) {
898 error("getpeereid %d failed: %s",
899 sock, strerror(errno));
903 if ((euid != 0) && (getuid() != euid)) {
904 error("uid mismatch: "
905 "peer euid %u != uid %u",
906 (u_int) euid, (u_int) getuid());
910 new_socket(AUTH_CONNECTION, sock);
913 case AUTH_CONNECTION:
914 if (buffer_len(&sockets[i].output) > 0 &&
915 FD_ISSET(sockets[i].fd, writeset)) {
917 len = write(sockets[i].fd,
918 buffer_ptr(&sockets[i].output),
919 buffer_len(&sockets[i].output));
920 if (len == -1 && (errno == EAGAIN ||
926 close_socket(&sockets[i]);
929 buffer_consume(&sockets[i].output, len);
931 if (FD_ISSET(sockets[i].fd, readset)) {
933 len = read(sockets[i].fd, buf, sizeof(buf));
934 if (len == -1 && (errno == EAGAIN ||
940 close_socket(&sockets[i]);
943 buffer_append(&sockets[i].input, buf, len);
944 process_message(&sockets[i]);
948 fatal("Unknown type %d", sockets[i].type);
970 cleanup_handler(int sig)
978 check_parent_exists(int sig)
980 int save_errno = errno;
982 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
983 /* printf("Parent has died - Authentication agent exiting.\n"); */
984 cleanup_handler(sig); /* safe */
986 mysignal(SIGALRM, check_parent_exists);
994 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
996 fprintf(stderr, "Options:\n");
997 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
998 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
999 fprintf(stderr, " -k Kill the current agent.\n");
1000 fprintf(stderr, " -d Debug mode.\n");
1001 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
1002 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
1007 main(int ac, char **av)
1009 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1012 char *shell, *format, *pidstr, *agentsocket = NULL;
1013 fd_set *readsetp = NULL, *writesetp = NULL;
1014 struct sockaddr_un sunaddr;
1015 #ifdef HAVE_SETRLIMIT
1020 extern char *optarg;
1022 char pidstrbuf[1 + 3 * sizeof pid];
1024 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1031 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1032 /* Disable ptrace on Linux without sgid bit */
1033 prctl(PR_SET_DUMPABLE, 0);
1036 SSLeay_add_all_algorithms();
1038 __progname = ssh_get_progname(av[0]);
1042 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
1063 agentsocket = optarg;
1066 if ((lifetime = convtime(optarg)) == -1) {
1067 fprintf(stderr, "Invalid lifetime\n");
1078 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1081 if (ac == 0 && !c_flag && !s_flag) {
1082 shell = getenv("SHELL");
1083 if (shell != NULL &&
1084 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1088 const char *errstr = NULL;
1090 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1091 if (pidstr == NULL) {
1092 fprintf(stderr, "%s not set, cannot kill agent\n",
1093 SSH_AGENTPID_ENV_NAME);
1096 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1099 "%s=\"%s\", which is not a good PID: %s\n",
1100 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
1103 if (kill(pid, SIGTERM) == -1) {
1107 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1108 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1109 printf(format, SSH_AGENTPID_ENV_NAME);
1110 printf("echo Agent pid %ld killed;\n", (long)pid);
1113 parent_pid = getpid();
1115 if (agentsocket == NULL) {
1116 /* Create private directory for agent socket */
1117 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
1118 if (mkdtemp(socket_dir) == NULL) {
1119 perror("mkdtemp: private socket dir");
1122 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1125 /* Try to use specified agent socket */
1126 socket_dir[0] = '\0';
1127 strlcpy(socket_name, agentsocket, sizeof socket_name);
1131 * Create socket early so it will exist before command gets run from
1134 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1137 *socket_name = '\0'; /* Don't unlink any existing file */
1140 memset(&sunaddr, 0, sizeof(sunaddr));
1141 sunaddr.sun_family = AF_UNIX;
1142 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1143 prev_mask = umask(0177);
1144 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
1146 *socket_name = '\0'; /* Don't unlink any existing file */
1151 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1157 * Fork, and have the parent execute the command, if any, or present
1158 * the socket data. The child continues as the authentication agent.
1161 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1162 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1163 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1164 SSH_AUTHSOCKET_ENV_NAME);
1165 printf("echo Agent pid %ld;\n", (long)parent_pid);
1173 if (pid != 0) { /* Parent - execute the given command. */
1175 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1177 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1178 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1179 SSH_AUTHSOCKET_ENV_NAME);
1180 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1181 SSH_AGENTPID_ENV_NAME);
1182 printf("echo Agent pid %ld;\n", (long)pid);
1185 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1186 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1195 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1197 if (setsid() == -1) {
1198 error("setsid: %s", strerror(errno));
1203 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1204 /* XXX might close listen socket */
1205 (void)dup2(fd, STDIN_FILENO);
1206 (void)dup2(fd, STDOUT_FILENO);
1207 (void)dup2(fd, STDERR_FILENO);
1212 #ifdef HAVE_SETRLIMIT
1213 /* deny core dumps, since memory contains unencrypted private keys */
1214 rlim.rlim_cur = rlim.rlim_max = 0;
1215 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1216 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1222 new_socket(AUTH_SOCKET, sock);
1224 mysignal(SIGALRM, check_parent_exists);
1229 signal(SIGINT, SIG_IGN);
1230 signal(SIGPIPE, SIG_IGN);
1231 signal(SIGHUP, cleanup_handler);
1232 signal(SIGTERM, cleanup_handler);
1236 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1237 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1240 fatal("select: %s", strerror(errno));
1242 after_select(readsetp, writesetp);