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.126 2006/02/08 12:15:27 stevesk Exp $");
43 #include <openssl/evp.h>
44 #include <openssl/md5.h>
62 #if defined(HAVE_SYS_PRCTL_H)
63 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
80 u_int sockets_alloc = 0;
81 SocketEntry *sockets = NULL;
83 typedef struct identity {
84 TAILQ_ENTRY(identity) next;
93 TAILQ_HEAD(idqueue, identity) idlist;
96 /* private key table, one per protocol version */
101 /* pid of shell == parent of agent */
102 pid_t parent_pid = -1;
104 /* pathname and directory for AUTH_SOCKET */
105 char socket_name[1024];
106 char socket_dir[1024];
110 char *lock_passwd = NULL;
112 extern char *__progname;
114 /* Default lifetime (0 == forever) */
115 static int lifetime = 0;
118 close_socket(SocketEntry *e)
122 e->type = AUTH_UNUSED;
123 buffer_free(&e->input);
124 buffer_free(&e->output);
125 buffer_free(&e->request);
133 for (i = 0; i <=2; i++) {
134 TAILQ_INIT(&idtable[i].idlist);
135 idtable[i].nentries = 0;
139 /* return private key table for requested protocol version */
141 idtab_lookup(int version)
143 if (version < 1 || version > 2)
144 fatal("internal error, bad protocol version %d", version);
145 return &idtable[version];
149 free_identity(Identity *id)
156 /* return matching private key for given public key */
158 lookup_identity(Key *key, int version)
162 Idtab *tab = idtab_lookup(version);
163 TAILQ_FOREACH(id, &tab->idlist, next) {
164 if (key_equal(key, id->key))
170 /* Check confirmation of keysign request */
172 confirm_key(Identity *id)
177 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
178 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
186 /* send list of supported public keys to 'client' */
188 process_request_identities(SocketEntry *e, int version)
190 Idtab *tab = idtab_lookup(version);
195 buffer_put_char(&msg, (version == 1) ?
196 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
197 buffer_put_int(&msg, tab->nentries);
198 TAILQ_FOREACH(id, &tab->idlist, next) {
199 if (id->key->type == KEY_RSA1) {
200 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
201 buffer_put_bignum(&msg, id->key->rsa->e);
202 buffer_put_bignum(&msg, id->key->rsa->n);
206 key_to_blob(id->key, &blob, &blen);
207 buffer_put_string(&msg, blob, blen);
210 buffer_put_cstring(&msg, id->comment);
212 buffer_put_int(&e->output, buffer_len(&msg));
213 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
219 process_authentication_challenge1(SocketEntry *e)
221 u_char buf[32], mdbuf[16], session_id[16];
231 key = key_new(KEY_RSA1);
232 if ((challenge = BN_new()) == NULL)
233 fatal("process_authentication_challenge1: BN_new failed");
235 (void) buffer_get_int(&e->request); /* ignored */
236 buffer_get_bignum(&e->request, key->rsa->e);
237 buffer_get_bignum(&e->request, key->rsa->n);
238 buffer_get_bignum(&e->request, challenge);
240 /* Only protocol 1.1 is supported */
241 if (buffer_len(&e->request) == 0)
243 buffer_get(&e->request, session_id, 16);
244 response_type = buffer_get_int(&e->request);
245 if (response_type != 1)
248 id = lookup_identity(key, 1);
249 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
250 Key *private = id->key;
251 /* Decrypt the challenge using the private key. */
252 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
255 /* The response is MD5 of decrypted challenge plus session id. */
256 len = BN_num_bytes(challenge);
257 if (len <= 0 || len > 32) {
258 logit("process_authentication_challenge: bad challenge length %d", len);
262 BN_bn2bin(challenge, buf + 32 - len);
264 MD5_Update(&md, buf, 32);
265 MD5_Update(&md, session_id, 16);
266 MD5_Final(mdbuf, &md);
268 /* Send the response. */
269 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
270 for (i = 0; i < 16; i++)
271 buffer_put_char(&msg, mdbuf[i]);
276 /* Unknown identity or protocol error. Send failure. */
277 buffer_put_char(&msg, SSH_AGENT_FAILURE);
279 buffer_put_int(&e->output, buffer_len(&msg));
280 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
282 BN_clear_free(challenge);
288 process_sign_request2(SocketEntry *e)
290 u_char *blob, *data, *signature = NULL;
291 u_int blen, dlen, slen = 0;
292 extern int datafellows;
299 blob = buffer_get_string(&e->request, &blen);
300 data = buffer_get_string(&e->request, &dlen);
302 flags = buffer_get_int(&e->request);
303 if (flags & SSH_AGENT_OLD_SIGNATURE)
304 datafellows = SSH_BUG_SIGBLOB;
306 key = key_from_blob(blob, blen);
308 Identity *id = lookup_identity(key, 2);
309 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
310 ok = key_sign(id->key, &signature, &slen, data, dlen);
315 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
316 buffer_put_string(&msg, signature, slen);
318 buffer_put_char(&msg, SSH_AGENT_FAILURE);
320 buffer_put_int(&e->output, buffer_len(&msg));
321 buffer_append(&e->output, buffer_ptr(&msg),
326 if (signature != NULL)
332 process_remove_identity(SocketEntry *e, int version)
341 key = key_new(KEY_RSA1);
342 bits = buffer_get_int(&e->request);
343 buffer_get_bignum(&e->request, key->rsa->e);
344 buffer_get_bignum(&e->request, key->rsa->n);
346 if (bits != key_size(key))
347 logit("Warning: identity keysize mismatch: actual %u, announced %u",
348 key_size(key), bits);
351 blob = buffer_get_string(&e->request, &blen);
352 key = key_from_blob(blob, blen);
357 Identity *id = lookup_identity(key, version);
360 * We have this key. Free the old key. Since we
361 * don't want to leave empty slots in the middle of
362 * the array, we actually free the key there and move
363 * all the entries between the empty slot and the end
366 Idtab *tab = idtab_lookup(version);
367 if (tab->nentries < 1)
368 fatal("process_remove_identity: "
369 "internal error: tab->nentries %d",
371 TAILQ_REMOVE(&tab->idlist, id, next);
378 buffer_put_int(&e->output, 1);
379 buffer_put_char(&e->output,
380 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
384 process_remove_all_identities(SocketEntry *e, int version)
386 Idtab *tab = idtab_lookup(version);
389 /* Loop over all identities and clear the keys. */
390 for (id = TAILQ_FIRST(&tab->idlist); id;
391 id = TAILQ_FIRST(&tab->idlist)) {
392 TAILQ_REMOVE(&tab->idlist, id, next);
396 /* Mark that there are no identities. */
400 buffer_put_int(&e->output, 1);
401 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
407 u_int now = time(NULL);
412 for (version = 1; version < 3; version++) {
413 tab = idtab_lookup(version);
414 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
415 nxt = TAILQ_NEXT(id, next);
416 if (id->death != 0 && now >= id->death) {
417 TAILQ_REMOVE(&tab->idlist, id, next);
426 process_add_identity(SocketEntry *e, int version)
428 Idtab *tab = idtab_lookup(version);
429 int type, success = 0, death = 0, confirm = 0;
430 char *type_name, *comment;
435 k = key_new_private(KEY_RSA1);
436 (void) buffer_get_int(&e->request); /* ignored */
437 buffer_get_bignum(&e->request, k->rsa->n);
438 buffer_get_bignum(&e->request, k->rsa->e);
439 buffer_get_bignum(&e->request, k->rsa->d);
440 buffer_get_bignum(&e->request, k->rsa->iqmp);
442 /* SSH and SSL have p and q swapped */
443 buffer_get_bignum(&e->request, k->rsa->q); /* p */
444 buffer_get_bignum(&e->request, k->rsa->p); /* q */
446 /* Generate additional parameters */
447 rsa_generate_additional_parameters(k->rsa);
450 type_name = buffer_get_string(&e->request, NULL);
451 type = key_type_from_name(type_name);
455 k = key_new_private(type);
456 buffer_get_bignum2(&e->request, k->dsa->p);
457 buffer_get_bignum2(&e->request, k->dsa->q);
458 buffer_get_bignum2(&e->request, k->dsa->g);
459 buffer_get_bignum2(&e->request, k->dsa->pub_key);
460 buffer_get_bignum2(&e->request, k->dsa->priv_key);
463 k = key_new_private(type);
464 buffer_get_bignum2(&e->request, k->rsa->n);
465 buffer_get_bignum2(&e->request, k->rsa->e);
466 buffer_get_bignum2(&e->request, k->rsa->d);
467 buffer_get_bignum2(&e->request, k->rsa->iqmp);
468 buffer_get_bignum2(&e->request, k->rsa->p);
469 buffer_get_bignum2(&e->request, k->rsa->q);
471 /* Generate additional parameters */
472 rsa_generate_additional_parameters(k->rsa);
475 buffer_clear(&e->request);
480 /* enable blinding */
484 if (RSA_blinding_on(k->rsa, NULL) != 1) {
485 error("process_add_identity: RSA_blinding_on failed");
491 comment = buffer_get_string(&e->request, NULL);
497 while (buffer_len(&e->request)) {
498 switch (buffer_get_char(&e->request)) {
499 case SSH_AGENT_CONSTRAIN_LIFETIME:
500 death = time(NULL) + buffer_get_int(&e->request);
502 case SSH_AGENT_CONSTRAIN_CONFIRM:
509 if (lifetime && !death)
510 death = time(NULL) + lifetime;
511 if (lookup_identity(k, version) == NULL) {
512 Identity *id = xmalloc(sizeof(Identity));
514 id->comment = comment;
516 id->confirm = confirm;
517 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
518 /* Increment the number of identities. */
525 buffer_put_int(&e->output, 1);
526 buffer_put_char(&e->output,
527 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
530 /* XXX todo: encrypt sensitive data with passphrase */
532 process_lock_agent(SocketEntry *e, int lock)
537 passwd = buffer_get_string(&e->request, NULL);
538 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
540 memset(lock_passwd, 0, strlen(lock_passwd));
544 } else if (!locked && lock) {
546 lock_passwd = xstrdup(passwd);
549 memset(passwd, 0, strlen(passwd));
552 buffer_put_int(&e->output, 1);
553 buffer_put_char(&e->output,
554 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
558 no_identities(SocketEntry *e, u_int type)
563 buffer_put_char(&msg,
564 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
565 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
566 buffer_put_int(&msg, 0);
567 buffer_put_int(&e->output, buffer_len(&msg));
568 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
574 process_add_smartcard_key (SocketEntry *e)
576 char *sc_reader_id = NULL, *pin;
577 int i, version, success = 0, death = 0, confirm = 0;
582 sc_reader_id = buffer_get_string(&e->request, NULL);
583 pin = buffer_get_string(&e->request, NULL);
585 while (buffer_len(&e->request)) {
586 switch (buffer_get_char(&e->request)) {
587 case SSH_AGENT_CONSTRAIN_LIFETIME:
588 death = time(NULL) + buffer_get_int(&e->request);
590 case SSH_AGENT_CONSTRAIN_CONFIRM:
597 if (lifetime && !death)
598 death = time(NULL) + lifetime;
600 keys = sc_get_keys(sc_reader_id, pin);
604 if (keys == NULL || keys[0] == NULL) {
605 error("sc_get_keys failed");
608 for (i = 0; keys[i] != NULL; i++) {
610 version = k->type == KEY_RSA1 ? 1 : 2;
611 tab = idtab_lookup(version);
612 if (lookup_identity(k, version) == NULL) {
613 id = xmalloc(sizeof(Identity));
615 id->comment = sc_get_key_label(k);
617 id->confirm = confirm;
618 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
628 buffer_put_int(&e->output, 1);
629 buffer_put_char(&e->output,
630 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
634 process_remove_smartcard_key(SocketEntry *e)
636 char *sc_reader_id = NULL, *pin;
637 int i, version, success = 0;
638 Key **keys, *k = NULL;
642 sc_reader_id = buffer_get_string(&e->request, NULL);
643 pin = buffer_get_string(&e->request, NULL);
644 keys = sc_get_keys(sc_reader_id, pin);
648 if (keys == NULL || keys[0] == NULL) {
649 error("sc_get_keys failed");
652 for (i = 0; keys[i] != NULL; i++) {
654 version = k->type == KEY_RSA1 ? 1 : 2;
655 if ((id = lookup_identity(k, version)) != NULL) {
656 tab = idtab_lookup(version);
657 TAILQ_REMOVE(&tab->idlist, id, next);
667 buffer_put_int(&e->output, 1);
668 buffer_put_char(&e->output,
669 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
671 #endif /* SMARTCARD */
673 /* dispatch incoming messages */
676 process_message(SocketEntry *e)
684 if (buffer_len(&e->input) < 5)
685 return; /* Incomplete message. */
686 cp = buffer_ptr(&e->input);
687 msg_len = GET_32BIT(cp);
688 if (msg_len > 256 * 1024) {
692 if (buffer_len(&e->input) < msg_len + 4)
695 /* move the current input to e->request */
696 buffer_consume(&e->input, 4);
697 buffer_clear(&e->request);
698 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
699 buffer_consume(&e->input, msg_len);
700 type = buffer_get_char(&e->request);
702 /* check wheter agent is locked */
703 if (locked && type != SSH_AGENTC_UNLOCK) {
704 buffer_clear(&e->request);
706 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
707 case SSH2_AGENTC_REQUEST_IDENTITIES:
708 /* send empty lists */
709 no_identities(e, type);
712 /* send a fail message for all other request types */
713 buffer_put_int(&e->output, 1);
714 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
719 debug("type %d", type);
721 case SSH_AGENTC_LOCK:
722 case SSH_AGENTC_UNLOCK:
723 process_lock_agent(e, type == SSH_AGENTC_LOCK);
726 case SSH_AGENTC_RSA_CHALLENGE:
727 process_authentication_challenge1(e);
729 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
730 process_request_identities(e, 1);
732 case SSH_AGENTC_ADD_RSA_IDENTITY:
733 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
734 process_add_identity(e, 1);
736 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
737 process_remove_identity(e, 1);
739 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
740 process_remove_all_identities(e, 1);
743 case SSH2_AGENTC_SIGN_REQUEST:
744 process_sign_request2(e);
746 case SSH2_AGENTC_REQUEST_IDENTITIES:
747 process_request_identities(e, 2);
749 case SSH2_AGENTC_ADD_IDENTITY:
750 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
751 process_add_identity(e, 2);
753 case SSH2_AGENTC_REMOVE_IDENTITY:
754 process_remove_identity(e, 2);
756 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
757 process_remove_all_identities(e, 2);
760 case SSH_AGENTC_ADD_SMARTCARD_KEY:
761 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
762 process_add_smartcard_key(e);
764 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
765 process_remove_smartcard_key(e);
767 #endif /* SMARTCARD */
769 /* Unknown message. Respond with failure. */
770 error("Unknown message %d", type);
771 buffer_clear(&e->request);
772 buffer_put_int(&e->output, 1);
773 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
779 new_socket(sock_type type, int fd)
781 u_int i, old_alloc, new_alloc;
788 for (i = 0; i < sockets_alloc; i++)
789 if (sockets[i].type == AUTH_UNUSED) {
791 buffer_init(&sockets[i].input);
792 buffer_init(&sockets[i].output);
793 buffer_init(&sockets[i].request);
794 sockets[i].type = type;
797 old_alloc = sockets_alloc;
798 new_alloc = sockets_alloc + 10;
800 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
802 sockets = xmalloc(new_alloc * sizeof(sockets[0]));
803 for (i = old_alloc; i < new_alloc; i++)
804 sockets[i].type = AUTH_UNUSED;
805 sockets_alloc = new_alloc;
806 sockets[old_alloc].fd = fd;
807 buffer_init(&sockets[old_alloc].input);
808 buffer_init(&sockets[old_alloc].output);
809 buffer_init(&sockets[old_alloc].request);
810 sockets[old_alloc].type = type;
814 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
819 for (i = 0; i < sockets_alloc; i++) {
820 switch (sockets[i].type) {
822 case AUTH_CONNECTION:
823 n = MAX(n, sockets[i].fd);
828 fatal("Unknown socket type %d", sockets[i].type);
833 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
834 if (*fdrp == NULL || sz > *nallocp) {
844 debug("XXX shrink: %d < %d", n, *fdl);
846 memset(*fdrp, 0, sz);
847 memset(*fdwp, 0, sz);
849 for (i = 0; i < sockets_alloc; i++) {
850 switch (sockets[i].type) {
852 case AUTH_CONNECTION:
853 FD_SET(sockets[i].fd, *fdrp);
854 if (buffer_len(&sockets[i].output) > 0)
855 FD_SET(sockets[i].fd, *fdwp);
865 after_select(fd_set *readset, fd_set *writeset)
867 struct sockaddr_un sunaddr;
875 for (i = 0; i < sockets_alloc; i++)
876 switch (sockets[i].type) {
880 if (FD_ISSET(sockets[i].fd, readset)) {
881 slen = sizeof(sunaddr);
882 sock = accept(sockets[i].fd,
883 (struct sockaddr *) &sunaddr, &slen);
885 error("accept from AUTH_SOCKET: %s",
889 if (getpeereid(sock, &euid, &egid) < 0) {
890 error("getpeereid %d failed: %s",
891 sock, strerror(errno));
895 if ((euid != 0) && (getuid() != euid)) {
896 error("uid mismatch: "
897 "peer euid %u != uid %u",
898 (u_int) euid, (u_int) getuid());
902 new_socket(AUTH_CONNECTION, sock);
905 case AUTH_CONNECTION:
906 if (buffer_len(&sockets[i].output) > 0 &&
907 FD_ISSET(sockets[i].fd, writeset)) {
909 len = write(sockets[i].fd,
910 buffer_ptr(&sockets[i].output),
911 buffer_len(&sockets[i].output));
912 if (len == -1 && (errno == EAGAIN ||
918 close_socket(&sockets[i]);
921 buffer_consume(&sockets[i].output, len);
923 if (FD_ISSET(sockets[i].fd, readset)) {
925 len = read(sockets[i].fd, buf, sizeof(buf));
926 if (len == -1 && (errno == EAGAIN ||
932 close_socket(&sockets[i]);
935 buffer_append(&sockets[i].input, buf, len);
936 process_message(&sockets[i]);
940 fatal("Unknown type %d", sockets[i].type);
961 cleanup_handler(int sig)
968 check_parent_exists(int sig)
970 int save_errno = errno;
972 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
973 /* printf("Parent has died - Authentication agent exiting.\n"); */
974 cleanup_handler(sig); /* safe */
976 mysignal(SIGALRM, check_parent_exists);
984 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
986 fprintf(stderr, "Options:\n");
987 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
988 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
989 fprintf(stderr, " -k Kill the current agent.\n");
990 fprintf(stderr, " -d Debug mode.\n");
991 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
992 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
997 main(int ac, char **av)
999 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1002 char *shell, *format, *pidstr, *agentsocket = NULL;
1003 fd_set *readsetp = NULL, *writesetp = NULL;
1004 struct sockaddr_un sunaddr;
1005 #ifdef HAVE_SETRLIMIT
1010 extern char *optarg;
1012 char pidstrbuf[1 + 3 * sizeof pid];
1014 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1021 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1022 /* Disable ptrace on Linux without sgid bit */
1023 prctl(PR_SET_DUMPABLE, 0);
1026 SSLeay_add_all_algorithms();
1028 __progname = ssh_get_progname(av[0]);
1032 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
1053 agentsocket = optarg;
1056 if ((lifetime = convtime(optarg)) == -1) {
1057 fprintf(stderr, "Invalid lifetime\n");
1068 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1071 if (ac == 0 && !c_flag && !s_flag) {
1072 shell = getenv("SHELL");
1073 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1077 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1078 if (pidstr == NULL) {
1079 fprintf(stderr, "%s not set, cannot kill agent\n",
1080 SSH_AGENTPID_ENV_NAME);
1085 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1086 SSH_AGENTPID_ENV_NAME, pidstr);
1089 if (kill(pid, SIGTERM) == -1) {
1093 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1094 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1095 printf(format, SSH_AGENTPID_ENV_NAME);
1096 printf("echo Agent pid %ld killed;\n", (long)pid);
1099 parent_pid = getpid();
1101 if (agentsocket == NULL) {
1102 /* Create private directory for agent socket */
1103 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
1104 if (mkdtemp(socket_dir) == NULL) {
1105 perror("mkdtemp: private socket dir");
1108 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1111 /* Try to use specified agent socket */
1112 socket_dir[0] = '\0';
1113 strlcpy(socket_name, agentsocket, sizeof socket_name);
1117 * Create socket early so it will exist before command gets run from
1120 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1123 *socket_name = '\0'; /* Don't unlink any existing file */
1126 memset(&sunaddr, 0, sizeof(sunaddr));
1127 sunaddr.sun_family = AF_UNIX;
1128 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1129 prev_mask = umask(0177);
1130 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1132 *socket_name = '\0'; /* Don't unlink any existing file */
1137 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1143 * Fork, and have the parent execute the command, if any, or present
1144 * the socket data. The child continues as the authentication agent.
1147 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1148 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1149 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1150 SSH_AUTHSOCKET_ENV_NAME);
1151 printf("echo Agent pid %ld;\n", (long)parent_pid);
1159 if (pid != 0) { /* Parent - execute the given command. */
1161 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
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(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1167 SSH_AGENTPID_ENV_NAME);
1168 printf("echo Agent pid %ld;\n", (long)pid);
1171 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1172 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1181 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1183 if (setsid() == -1) {
1184 error("setsid: %s", strerror(errno));
1189 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1190 /* XXX might close listen socket */
1191 (void)dup2(fd, STDIN_FILENO);
1192 (void)dup2(fd, STDOUT_FILENO);
1193 (void)dup2(fd, STDERR_FILENO);
1198 #ifdef HAVE_SETRLIMIT
1199 /* deny core dumps, since memory contains unencrypted private keys */
1200 rlim.rlim_cur = rlim.rlim_max = 0;
1201 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1202 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1208 new_socket(AUTH_SOCKET, sock);
1210 mysignal(SIGALRM, check_parent_exists);
1215 signal(SIGINT, SIG_IGN);
1216 signal(SIGPIPE, SIG_IGN);
1217 signal(SIGHUP, cleanup_handler);
1218 signal(SIGTERM, cleanup_handler);
1222 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1223 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1226 fatal("select: %s", strerror(errno));
1228 after_select(readsetp, writesetp);