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.120 2004/08/11 21:43:05 avsm Exp $");
40 #include <openssl/evp.h>
41 #include <openssl/md5.h>
59 #if defined(HAVE_SYS_PRCTL_H)
60 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
77 u_int sockets_alloc = 0;
78 SocketEntry *sockets = NULL;
80 typedef struct identity {
81 TAILQ_ENTRY(identity) next;
90 TAILQ_HEAD(idqueue, identity) idlist;
93 /* private key table, one per protocol version */
98 /* pid of shell == parent of agent */
99 pid_t parent_pid = -1;
101 /* pathname and directory for AUTH_SOCKET */
102 char socket_name[1024];
103 char socket_dir[1024];
107 char *lock_passwd = NULL;
109 extern char *__progname;
111 /* Default lifetime (0 == forever) */
112 static int lifetime = 0;
115 close_socket(SocketEntry *e)
119 e->type = AUTH_UNUSED;
120 buffer_free(&e->input);
121 buffer_free(&e->output);
122 buffer_free(&e->request);
130 for (i = 0; i <=2; i++) {
131 TAILQ_INIT(&idtable[i].idlist);
132 idtable[i].nentries = 0;
136 /* return private key table for requested protocol version */
138 idtab_lookup(int version)
140 if (version < 1 || version > 2)
141 fatal("internal error, bad protocol version %d", version);
142 return &idtable[version];
146 free_identity(Identity *id)
153 /* return matching private key for given public key */
155 lookup_identity(Key *key, int version)
159 Idtab *tab = idtab_lookup(version);
160 TAILQ_FOREACH(id, &tab->idlist, next) {
161 if (key_equal(key, id->key))
167 /* Check confirmation of keysign request */
169 confirm_key(Identity *id)
171 char *p, prompt[1024];
174 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
175 snprintf(prompt, sizeof(prompt), "Allow use of key %s?\n"
176 "Key fingerprint %s.", id->comment, p);
178 p = read_passphrase(prompt, RP_ALLOW_EOF);
181 * Accept empty responses and responses consisting
182 * of the word "yes" as affirmative.
184 if (*p == '\0' || *p == '\n' || strcasecmp(p, "yes") == 0)
191 /* send list of supported public keys to 'client' */
193 process_request_identities(SocketEntry *e, int version)
195 Idtab *tab = idtab_lookup(version);
200 buffer_put_char(&msg, (version == 1) ?
201 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
202 buffer_put_int(&msg, tab->nentries);
203 TAILQ_FOREACH(id, &tab->idlist, next) {
204 if (id->key->type == KEY_RSA1) {
205 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
206 buffer_put_bignum(&msg, id->key->rsa->e);
207 buffer_put_bignum(&msg, id->key->rsa->n);
211 key_to_blob(id->key, &blob, &blen);
212 buffer_put_string(&msg, blob, blen);
215 buffer_put_cstring(&msg, id->comment);
217 buffer_put_int(&e->output, buffer_len(&msg));
218 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
224 process_authentication_challenge1(SocketEntry *e)
226 u_char buf[32], mdbuf[16], session_id[16];
236 key = key_new(KEY_RSA1);
237 if ((challenge = BN_new()) == NULL)
238 fatal("process_authentication_challenge1: BN_new failed");
240 (void) buffer_get_int(&e->request); /* ignored */
241 buffer_get_bignum(&e->request, key->rsa->e);
242 buffer_get_bignum(&e->request, key->rsa->n);
243 buffer_get_bignum(&e->request, challenge);
245 /* Only protocol 1.1 is supported */
246 if (buffer_len(&e->request) == 0)
248 buffer_get(&e->request, session_id, 16);
249 response_type = buffer_get_int(&e->request);
250 if (response_type != 1)
253 id = lookup_identity(key, 1);
254 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
255 Key *private = id->key;
256 /* Decrypt the challenge using the private key. */
257 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
260 /* The response is MD5 of decrypted challenge plus session id. */
261 len = BN_num_bytes(challenge);
262 if (len <= 0 || len > 32) {
263 logit("process_authentication_challenge: bad challenge length %d", len);
267 BN_bn2bin(challenge, buf + 32 - len);
269 MD5_Update(&md, buf, 32);
270 MD5_Update(&md, session_id, 16);
271 MD5_Final(mdbuf, &md);
273 /* Send the response. */
274 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
275 for (i = 0; i < 16; i++)
276 buffer_put_char(&msg, mdbuf[i]);
281 /* Unknown identity or protocol error. Send failure. */
282 buffer_put_char(&msg, SSH_AGENT_FAILURE);
284 buffer_put_int(&e->output, buffer_len(&msg));
285 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
287 BN_clear_free(challenge);
293 process_sign_request2(SocketEntry *e)
295 u_char *blob, *data, *signature = NULL;
296 u_int blen, dlen, slen = 0;
297 extern int datafellows;
304 blob = buffer_get_string(&e->request, &blen);
305 data = buffer_get_string(&e->request, &dlen);
307 flags = buffer_get_int(&e->request);
308 if (flags & SSH_AGENT_OLD_SIGNATURE)
309 datafellows = SSH_BUG_SIGBLOB;
311 key = key_from_blob(blob, blen);
313 Identity *id = lookup_identity(key, 2);
314 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
315 ok = key_sign(id->key, &signature, &slen, data, dlen);
320 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
321 buffer_put_string(&msg, signature, slen);
323 buffer_put_char(&msg, SSH_AGENT_FAILURE);
325 buffer_put_int(&e->output, buffer_len(&msg));
326 buffer_append(&e->output, buffer_ptr(&msg),
331 if (signature != NULL)
337 process_remove_identity(SocketEntry *e, int version)
346 key = key_new(KEY_RSA1);
347 bits = buffer_get_int(&e->request);
348 buffer_get_bignum(&e->request, key->rsa->e);
349 buffer_get_bignum(&e->request, key->rsa->n);
351 if (bits != key_size(key))
352 logit("Warning: identity keysize mismatch: actual %u, announced %u",
353 key_size(key), bits);
356 blob = buffer_get_string(&e->request, &blen);
357 key = key_from_blob(blob, blen);
362 Identity *id = lookup_identity(key, version);
365 * We have this key. Free the old key. Since we
366 * don\'t want to leave empty slots in the middle of
367 * the array, we actually free the key there and move
368 * all the entries between the empty slot and the end
371 Idtab *tab = idtab_lookup(version);
372 if (tab->nentries < 1)
373 fatal("process_remove_identity: "
374 "internal error: tab->nentries %d",
376 TAILQ_REMOVE(&tab->idlist, id, next);
383 buffer_put_int(&e->output, 1);
384 buffer_put_char(&e->output,
385 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
389 process_remove_all_identities(SocketEntry *e, int version)
391 Idtab *tab = idtab_lookup(version);
394 /* Loop over all identities and clear the keys. */
395 for (id = TAILQ_FIRST(&tab->idlist); id;
396 id = TAILQ_FIRST(&tab->idlist)) {
397 TAILQ_REMOVE(&tab->idlist, id, next);
401 /* Mark that there are no identities. */
405 buffer_put_int(&e->output, 1);
406 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
412 u_int now = time(NULL);
417 for (version = 1; version < 3; version++) {
418 tab = idtab_lookup(version);
419 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
420 nxt = TAILQ_NEXT(id, next);
421 if (id->death != 0 && now >= id->death) {
422 TAILQ_REMOVE(&tab->idlist, id, next);
431 process_add_identity(SocketEntry *e, int version)
433 Idtab *tab = idtab_lookup(version);
434 int type, success = 0, death = 0, confirm = 0;
435 char *type_name, *comment;
440 k = key_new_private(KEY_RSA1);
441 (void) buffer_get_int(&e->request); /* ignored */
442 buffer_get_bignum(&e->request, k->rsa->n);
443 buffer_get_bignum(&e->request, k->rsa->e);
444 buffer_get_bignum(&e->request, k->rsa->d);
445 buffer_get_bignum(&e->request, k->rsa->iqmp);
447 /* SSH and SSL have p and q swapped */
448 buffer_get_bignum(&e->request, k->rsa->q); /* p */
449 buffer_get_bignum(&e->request, k->rsa->p); /* q */
451 /* Generate additional parameters */
452 rsa_generate_additional_parameters(k->rsa);
455 type_name = buffer_get_string(&e->request, NULL);
456 type = key_type_from_name(type_name);
460 k = key_new_private(type);
461 buffer_get_bignum2(&e->request, k->dsa->p);
462 buffer_get_bignum2(&e->request, k->dsa->q);
463 buffer_get_bignum2(&e->request, k->dsa->g);
464 buffer_get_bignum2(&e->request, k->dsa->pub_key);
465 buffer_get_bignum2(&e->request, k->dsa->priv_key);
468 k = key_new_private(type);
469 buffer_get_bignum2(&e->request, k->rsa->n);
470 buffer_get_bignum2(&e->request, k->rsa->e);
471 buffer_get_bignum2(&e->request, k->rsa->d);
472 buffer_get_bignum2(&e->request, k->rsa->iqmp);
473 buffer_get_bignum2(&e->request, k->rsa->p);
474 buffer_get_bignum2(&e->request, k->rsa->q);
476 /* Generate additional parameters */
477 rsa_generate_additional_parameters(k->rsa);
480 buffer_clear(&e->request);
485 /* enable blinding */
489 if (RSA_blinding_on(k->rsa, NULL) != 1) {
490 error("process_add_identity: RSA_blinding_on failed");
496 comment = buffer_get_string(&e->request, NULL);
502 while (buffer_len(&e->request)) {
503 switch (buffer_get_char(&e->request)) {
504 case SSH_AGENT_CONSTRAIN_LIFETIME:
505 death = time(NULL) + buffer_get_int(&e->request);
507 case SSH_AGENT_CONSTRAIN_CONFIRM:
514 if (lifetime && !death)
515 death = time(NULL) + lifetime;
516 if (lookup_identity(k, version) == NULL) {
517 Identity *id = xmalloc(sizeof(Identity));
519 id->comment = comment;
521 id->confirm = confirm;
522 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
523 /* Increment the number of identities. */
530 buffer_put_int(&e->output, 1);
531 buffer_put_char(&e->output,
532 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
535 /* XXX todo: encrypt sensitive data with passphrase */
537 process_lock_agent(SocketEntry *e, int lock)
542 passwd = buffer_get_string(&e->request, NULL);
543 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
545 memset(lock_passwd, 0, strlen(lock_passwd));
549 } else if (!locked && lock) {
551 lock_passwd = xstrdup(passwd);
554 memset(passwd, 0, strlen(passwd));
557 buffer_put_int(&e->output, 1);
558 buffer_put_char(&e->output,
559 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
563 no_identities(SocketEntry *e, u_int type)
568 buffer_put_char(&msg,
569 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
570 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
571 buffer_put_int(&msg, 0);
572 buffer_put_int(&e->output, buffer_len(&msg));
573 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
579 process_add_smartcard_key (SocketEntry *e)
581 char *sc_reader_id = NULL, *pin;
582 int i, version, success = 0, death = 0, confirm = 0;
587 sc_reader_id = buffer_get_string(&e->request, NULL);
588 pin = buffer_get_string(&e->request, NULL);
590 while (buffer_len(&e->request)) {
591 switch (buffer_get_char(&e->request)) {
592 case SSH_AGENT_CONSTRAIN_LIFETIME:
593 death = time(NULL) + buffer_get_int(&e->request);
595 case SSH_AGENT_CONSTRAIN_CONFIRM:
602 if (lifetime && !death)
603 death = time(NULL) + lifetime;
605 keys = sc_get_keys(sc_reader_id, pin);
609 if (keys == NULL || keys[0] == NULL) {
610 error("sc_get_keys failed");
613 for (i = 0; keys[i] != NULL; i++) {
615 version = k->type == KEY_RSA1 ? 1 : 2;
616 tab = idtab_lookup(version);
617 if (lookup_identity(k, version) == NULL) {
618 id = xmalloc(sizeof(Identity));
620 id->comment = sc_get_key_label(k);
622 id->confirm = confirm;
623 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
633 buffer_put_int(&e->output, 1);
634 buffer_put_char(&e->output,
635 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
639 process_remove_smartcard_key(SocketEntry *e)
641 char *sc_reader_id = NULL, *pin;
642 int i, version, success = 0;
643 Key **keys, *k = NULL;
647 sc_reader_id = buffer_get_string(&e->request, NULL);
648 pin = buffer_get_string(&e->request, NULL);
649 keys = sc_get_keys(sc_reader_id, pin);
653 if (keys == NULL || keys[0] == NULL) {
654 error("sc_get_keys failed");
657 for (i = 0; keys[i] != NULL; i++) {
659 version = k->type == KEY_RSA1 ? 1 : 2;
660 if ((id = lookup_identity(k, version)) != NULL) {
661 tab = idtab_lookup(version);
662 TAILQ_REMOVE(&tab->idlist, id, next);
672 buffer_put_int(&e->output, 1);
673 buffer_put_char(&e->output,
674 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
676 #endif /* SMARTCARD */
678 /* dispatch incoming messages */
681 process_message(SocketEntry *e)
689 if (buffer_len(&e->input) < 5)
690 return; /* Incomplete message. */
691 cp = buffer_ptr(&e->input);
692 msg_len = GET_32BIT(cp);
693 if (msg_len > 256 * 1024) {
697 if (buffer_len(&e->input) < msg_len + 4)
700 /* move the current input to e->request */
701 buffer_consume(&e->input, 4);
702 buffer_clear(&e->request);
703 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
704 buffer_consume(&e->input, msg_len);
705 type = buffer_get_char(&e->request);
707 /* check wheter agent is locked */
708 if (locked && type != SSH_AGENTC_UNLOCK) {
709 buffer_clear(&e->request);
711 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
712 case SSH2_AGENTC_REQUEST_IDENTITIES:
713 /* send empty lists */
714 no_identities(e, type);
717 /* send a fail message for all other request types */
718 buffer_put_int(&e->output, 1);
719 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
724 debug("type %d", type);
726 case SSH_AGENTC_LOCK:
727 case SSH_AGENTC_UNLOCK:
728 process_lock_agent(e, type == SSH_AGENTC_LOCK);
731 case SSH_AGENTC_RSA_CHALLENGE:
732 process_authentication_challenge1(e);
734 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
735 process_request_identities(e, 1);
737 case SSH_AGENTC_ADD_RSA_IDENTITY:
738 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
739 process_add_identity(e, 1);
741 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
742 process_remove_identity(e, 1);
744 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
745 process_remove_all_identities(e, 1);
748 case SSH2_AGENTC_SIGN_REQUEST:
749 process_sign_request2(e);
751 case SSH2_AGENTC_REQUEST_IDENTITIES:
752 process_request_identities(e, 2);
754 case SSH2_AGENTC_ADD_IDENTITY:
755 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
756 process_add_identity(e, 2);
758 case SSH2_AGENTC_REMOVE_IDENTITY:
759 process_remove_identity(e, 2);
761 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
762 process_remove_all_identities(e, 2);
765 case SSH_AGENTC_ADD_SMARTCARD_KEY:
766 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
767 process_add_smartcard_key(e);
769 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
770 process_remove_smartcard_key(e);
772 #endif /* SMARTCARD */
774 /* Unknown message. Respond with failure. */
775 error("Unknown message %d", type);
776 buffer_clear(&e->request);
777 buffer_put_int(&e->output, 1);
778 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
784 new_socket(sock_type type, int fd)
786 u_int i, old_alloc, new_alloc;
793 for (i = 0; i < sockets_alloc; i++)
794 if (sockets[i].type == AUTH_UNUSED) {
796 buffer_init(&sockets[i].input);
797 buffer_init(&sockets[i].output);
798 buffer_init(&sockets[i].request);
799 sockets[i].type = type;
802 old_alloc = sockets_alloc;
803 new_alloc = sockets_alloc + 10;
805 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
807 sockets = xmalloc(new_alloc * sizeof(sockets[0]));
808 for (i = old_alloc; i < new_alloc; i++)
809 sockets[i].type = AUTH_UNUSED;
810 sockets_alloc = new_alloc;
811 sockets[old_alloc].fd = fd;
812 buffer_init(&sockets[old_alloc].input);
813 buffer_init(&sockets[old_alloc].output);
814 buffer_init(&sockets[old_alloc].request);
815 sockets[old_alloc].type = type;
819 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
824 for (i = 0; i < sockets_alloc; i++) {
825 switch (sockets[i].type) {
827 case AUTH_CONNECTION:
828 n = MAX(n, sockets[i].fd);
833 fatal("Unknown socket type %d", sockets[i].type);
838 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
839 if (*fdrp == NULL || sz > *nallocp) {
849 debug("XXX shrink: %d < %d", n, *fdl);
851 memset(*fdrp, 0, sz);
852 memset(*fdwp, 0, sz);
854 for (i = 0; i < sockets_alloc; i++) {
855 switch (sockets[i].type) {
857 case AUTH_CONNECTION:
858 FD_SET(sockets[i].fd, *fdrp);
859 if (buffer_len(&sockets[i].output) > 0)
860 FD_SET(sockets[i].fd, *fdwp);
870 after_select(fd_set *readset, fd_set *writeset)
872 struct sockaddr_un sunaddr;
880 for (i = 0; i < sockets_alloc; i++)
881 switch (sockets[i].type) {
885 if (FD_ISSET(sockets[i].fd, readset)) {
886 slen = sizeof(sunaddr);
887 sock = accept(sockets[i].fd,
888 (struct sockaddr *) &sunaddr, &slen);
890 error("accept from AUTH_SOCKET: %s",
894 if (getpeereid(sock, &euid, &egid) < 0) {
895 error("getpeereid %d failed: %s",
896 sock, strerror(errno));
900 if ((euid != 0) && (getuid() != euid)) {
901 error("uid mismatch: "
902 "peer euid %u != uid %u",
903 (u_int) euid, (u_int) getuid());
907 new_socket(AUTH_CONNECTION, sock);
910 case AUTH_CONNECTION:
911 if (buffer_len(&sockets[i].output) > 0 &&
912 FD_ISSET(sockets[i].fd, writeset)) {
914 len = write(sockets[i].fd,
915 buffer_ptr(&sockets[i].output),
916 buffer_len(&sockets[i].output));
917 if (len == -1 && (errno == EAGAIN ||
923 close_socket(&sockets[i]);
926 buffer_consume(&sockets[i].output, len);
928 if (FD_ISSET(sockets[i].fd, readset)) {
930 len = read(sockets[i].fd, buf, sizeof(buf));
931 if (len == -1 && (errno == EAGAIN ||
937 close_socket(&sockets[i]);
940 buffer_append(&sockets[i].input, buf, len);
941 process_message(&sockets[i]);
945 fatal("Unknown type %d", sockets[i].type);
966 cleanup_handler(int sig)
973 check_parent_exists(int sig)
975 int save_errno = errno;
977 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
978 /* printf("Parent has died - Authentication agent exiting.\n"); */
979 cleanup_handler(sig); /* safe */
981 mysignal(SIGALRM, check_parent_exists);
989 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
991 fprintf(stderr, "Options:\n");
992 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
993 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
994 fprintf(stderr, " -k Kill the current agent.\n");
995 fprintf(stderr, " -d Debug mode.\n");
996 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
997 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
1002 main(int ac, char **av)
1004 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1007 char *shell, *format, *pidstr, *agentsocket = NULL;
1008 fd_set *readsetp = NULL, *writesetp = NULL;
1009 struct sockaddr_un sunaddr;
1010 #ifdef HAVE_SETRLIMIT
1017 extern char *optarg;
1019 char pidstrbuf[1 + 3 * sizeof pid];
1025 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1026 /* Disable ptrace on Linux without sgid bit */
1027 prctl(PR_SET_DUMPABLE, 0);
1030 SSLeay_add_all_algorithms();
1032 __progname = ssh_get_progname(av[0]);
1036 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
1057 agentsocket = optarg;
1060 if ((lifetime = convtime(optarg)) == -1) {
1061 fprintf(stderr, "Invalid lifetime\n");
1072 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1075 if (ac == 0 && !c_flag && !s_flag) {
1076 shell = getenv("SHELL");
1077 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1081 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1082 if (pidstr == NULL) {
1083 fprintf(stderr, "%s not set, cannot kill agent\n",
1084 SSH_AGENTPID_ENV_NAME);
1089 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1090 SSH_AGENTPID_ENV_NAME, pidstr);
1093 if (kill(pid, SIGTERM) == -1) {
1097 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1098 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1099 printf(format, SSH_AGENTPID_ENV_NAME);
1100 printf("echo Agent pid %ld killed;\n", (long)pid);
1103 parent_pid = getpid();
1105 if (agentsocket == NULL) {
1106 /* Create private directory for agent socket */
1107 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
1108 if (mkdtemp(socket_dir) == NULL) {
1109 perror("mkdtemp: private socket dir");
1112 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1115 /* Try to use specified agent socket */
1116 socket_dir[0] = '\0';
1117 strlcpy(socket_name, agentsocket, sizeof socket_name);
1121 * Create socket early so it will exist before command gets run from
1124 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1129 memset(&sunaddr, 0, sizeof(sunaddr));
1130 sunaddr.sun_family = AF_UNIX;
1131 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1133 prev_mask = umask(0177);
1135 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1145 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1151 * Fork, and have the parent execute the command, if any, or present
1152 * the socket data. The child continues as the authentication agent.
1155 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1156 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1157 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1158 SSH_AUTHSOCKET_ENV_NAME);
1159 printf("echo Agent pid %ld;\n", (long)parent_pid);
1167 if (pid != 0) { /* Parent - execute the given command. */
1169 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1171 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1172 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1173 SSH_AUTHSOCKET_ENV_NAME);
1174 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1175 SSH_AGENTPID_ENV_NAME);
1176 printf("echo Agent pid %ld;\n", (long)pid);
1179 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1180 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1189 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1191 if (setsid() == -1) {
1192 error("setsid: %s", strerror(errno));
1197 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1198 /* XXX might close listen socket */
1199 (void)dup2(fd, STDIN_FILENO);
1200 (void)dup2(fd, STDOUT_FILENO);
1201 (void)dup2(fd, STDERR_FILENO);
1206 #ifdef HAVE_SETRLIMIT
1207 /* deny core dumps, since memory contains unencrypted private keys */
1208 rlim.rlim_cur = rlim.rlim_max = 0;
1209 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1210 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1216 new_socket(AUTH_SOCKET, sock);
1218 mysignal(SIGALRM, check_parent_exists);
1223 signal(SIGINT, SIG_IGN);
1224 signal(SIGPIPE, SIG_IGN);
1225 signal(SIGHUP, cleanup_handler);
1226 signal(SIGTERM, cleanup_handler);
1230 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1231 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1234 fatal("select: %s", strerror(errno));
1236 after_select(readsetp, writesetp);