1 /* $OpenBSD: ssh-agent.c,v 1.140 2006/06/13 02:17:07 djm 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>
44 #include "openbsd-compat/sys-queue.h"
45 #include <sys/resource.h>
51 #include <openssl/evp.h>
52 #include <openssl/md5.h>
69 #if defined(HAVE_SYS_PRCTL_H)
70 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
87 u_int sockets_alloc = 0;
88 SocketEntry *sockets = NULL;
90 typedef struct identity {
91 TAILQ_ENTRY(identity) next;
100 TAILQ_HEAD(idqueue, identity) idlist;
103 /* private key table, one per protocol version */
108 /* pid of shell == parent of agent */
109 pid_t parent_pid = -1;
111 /* pathname and directory for AUTH_SOCKET */
112 char socket_name[MAXPATHLEN];
113 char socket_dir[MAXPATHLEN];
117 char *lock_passwd = NULL;
119 extern char *__progname;
121 /* Default lifetime (0 == forever) */
122 static int lifetime = 0;
125 close_socket(SocketEntry *e)
129 e->type = AUTH_UNUSED;
130 buffer_free(&e->input);
131 buffer_free(&e->output);
132 buffer_free(&e->request);
140 for (i = 0; i <=2; i++) {
141 TAILQ_INIT(&idtable[i].idlist);
142 idtable[i].nentries = 0;
146 /* return private key table for requested protocol version */
148 idtab_lookup(int version)
150 if (version < 1 || version > 2)
151 fatal("internal error, bad protocol version %d", version);
152 return &idtable[version];
156 free_identity(Identity *id)
163 /* return matching private key for given public key */
165 lookup_identity(Key *key, int version)
169 Idtab *tab = idtab_lookup(version);
170 TAILQ_FOREACH(id, &tab->idlist, next) {
171 if (key_equal(key, id->key))
177 /* Check confirmation of keysign request */
179 confirm_key(Identity *id)
184 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
185 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
193 /* send list of supported public keys to 'client' */
195 process_request_identities(SocketEntry *e, int version)
197 Idtab *tab = idtab_lookup(version);
202 buffer_put_char(&msg, (version == 1) ?
203 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
204 buffer_put_int(&msg, tab->nentries);
205 TAILQ_FOREACH(id, &tab->idlist, next) {
206 if (id->key->type == KEY_RSA1) {
207 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
208 buffer_put_bignum(&msg, id->key->rsa->e);
209 buffer_put_bignum(&msg, id->key->rsa->n);
213 key_to_blob(id->key, &blob, &blen);
214 buffer_put_string(&msg, blob, blen);
217 buffer_put_cstring(&msg, id->comment);
219 buffer_put_int(&e->output, buffer_len(&msg));
220 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
226 process_authentication_challenge1(SocketEntry *e)
228 u_char buf[32], mdbuf[16], session_id[16];
238 key = key_new(KEY_RSA1);
239 if ((challenge = BN_new()) == NULL)
240 fatal("process_authentication_challenge1: BN_new failed");
242 (void) buffer_get_int(&e->request); /* ignored */
243 buffer_get_bignum(&e->request, key->rsa->e);
244 buffer_get_bignum(&e->request, key->rsa->n);
245 buffer_get_bignum(&e->request, challenge);
247 /* Only protocol 1.1 is supported */
248 if (buffer_len(&e->request) == 0)
250 buffer_get(&e->request, session_id, 16);
251 response_type = buffer_get_int(&e->request);
252 if (response_type != 1)
255 id = lookup_identity(key, 1);
256 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
257 Key *private = id->key;
258 /* Decrypt the challenge using the private key. */
259 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
262 /* The response is MD5 of decrypted challenge plus session id. */
263 len = BN_num_bytes(challenge);
264 if (len <= 0 || len > 32) {
265 logit("process_authentication_challenge: bad challenge length %d", len);
269 BN_bn2bin(challenge, buf + 32 - len);
271 MD5_Update(&md, buf, 32);
272 MD5_Update(&md, session_id, 16);
273 MD5_Final(mdbuf, &md);
275 /* Send the response. */
276 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
277 for (i = 0; i < 16; i++)
278 buffer_put_char(&msg, mdbuf[i]);
283 /* Unknown identity or protocol error. Send failure. */
284 buffer_put_char(&msg, SSH_AGENT_FAILURE);
286 buffer_put_int(&e->output, buffer_len(&msg));
287 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
289 BN_clear_free(challenge);
295 process_sign_request2(SocketEntry *e)
297 u_char *blob, *data, *signature = NULL;
298 u_int blen, dlen, slen = 0;
299 extern int datafellows;
306 blob = buffer_get_string(&e->request, &blen);
307 data = buffer_get_string(&e->request, &dlen);
309 flags = buffer_get_int(&e->request);
310 if (flags & SSH_AGENT_OLD_SIGNATURE)
311 datafellows = SSH_BUG_SIGBLOB;
313 key = key_from_blob(blob, blen);
315 Identity *id = lookup_identity(key, 2);
316 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
317 ok = key_sign(id->key, &signature, &slen, data, dlen);
322 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
323 buffer_put_string(&msg, signature, slen);
325 buffer_put_char(&msg, SSH_AGENT_FAILURE);
327 buffer_put_int(&e->output, buffer_len(&msg));
328 buffer_append(&e->output, buffer_ptr(&msg),
333 if (signature != NULL)
339 process_remove_identity(SocketEntry *e, int version)
348 key = key_new(KEY_RSA1);
349 bits = buffer_get_int(&e->request);
350 buffer_get_bignum(&e->request, key->rsa->e);
351 buffer_get_bignum(&e->request, key->rsa->n);
353 if (bits != key_size(key))
354 logit("Warning: identity keysize mismatch: actual %u, announced %u",
355 key_size(key), bits);
358 blob = buffer_get_string(&e->request, &blen);
359 key = key_from_blob(blob, blen);
364 Identity *id = lookup_identity(key, version);
367 * We have this key. Free the old key. Since we
368 * don't want to leave empty slots in the middle of
369 * the array, we actually free the key there and move
370 * all the entries between the empty slot and the end
373 Idtab *tab = idtab_lookup(version);
374 if (tab->nentries < 1)
375 fatal("process_remove_identity: "
376 "internal error: tab->nentries %d",
378 TAILQ_REMOVE(&tab->idlist, id, next);
385 buffer_put_int(&e->output, 1);
386 buffer_put_char(&e->output,
387 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
391 process_remove_all_identities(SocketEntry *e, int version)
393 Idtab *tab = idtab_lookup(version);
396 /* Loop over all identities and clear the keys. */
397 for (id = TAILQ_FIRST(&tab->idlist); id;
398 id = TAILQ_FIRST(&tab->idlist)) {
399 TAILQ_REMOVE(&tab->idlist, id, next);
403 /* Mark that there are no identities. */
407 buffer_put_int(&e->output, 1);
408 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
414 u_int now = time(NULL);
419 for (version = 1; version < 3; version++) {
420 tab = idtab_lookup(version);
421 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
422 nxt = TAILQ_NEXT(id, next);
423 if (id->death != 0 && now >= id->death) {
424 TAILQ_REMOVE(&tab->idlist, id, next);
433 process_add_identity(SocketEntry *e, int version)
435 Idtab *tab = idtab_lookup(version);
436 int type, success = 0, death = 0, confirm = 0;
437 char *type_name, *comment;
442 k = key_new_private(KEY_RSA1);
443 (void) buffer_get_int(&e->request); /* ignored */
444 buffer_get_bignum(&e->request, k->rsa->n);
445 buffer_get_bignum(&e->request, k->rsa->e);
446 buffer_get_bignum(&e->request, k->rsa->d);
447 buffer_get_bignum(&e->request, k->rsa->iqmp);
449 /* SSH and SSL have p and q swapped */
450 buffer_get_bignum(&e->request, k->rsa->q); /* p */
451 buffer_get_bignum(&e->request, k->rsa->p); /* q */
453 /* Generate additional parameters */
454 rsa_generate_additional_parameters(k->rsa);
457 type_name = buffer_get_string(&e->request, NULL);
458 type = key_type_from_name(type_name);
462 k = key_new_private(type);
463 buffer_get_bignum2(&e->request, k->dsa->p);
464 buffer_get_bignum2(&e->request, k->dsa->q);
465 buffer_get_bignum2(&e->request, k->dsa->g);
466 buffer_get_bignum2(&e->request, k->dsa->pub_key);
467 buffer_get_bignum2(&e->request, k->dsa->priv_key);
470 k = key_new_private(type);
471 buffer_get_bignum2(&e->request, k->rsa->n);
472 buffer_get_bignum2(&e->request, k->rsa->e);
473 buffer_get_bignum2(&e->request, k->rsa->d);
474 buffer_get_bignum2(&e->request, k->rsa->iqmp);
475 buffer_get_bignum2(&e->request, k->rsa->p);
476 buffer_get_bignum2(&e->request, k->rsa->q);
478 /* Generate additional parameters */
479 rsa_generate_additional_parameters(k->rsa);
482 buffer_clear(&e->request);
487 /* enable blinding */
491 if (RSA_blinding_on(k->rsa, NULL) != 1) {
492 error("process_add_identity: RSA_blinding_on failed");
498 comment = buffer_get_string(&e->request, NULL);
504 while (buffer_len(&e->request)) {
505 switch (buffer_get_char(&e->request)) {
506 case SSH_AGENT_CONSTRAIN_LIFETIME:
507 death = time(NULL) + buffer_get_int(&e->request);
509 case SSH_AGENT_CONSTRAIN_CONFIRM:
516 if (lifetime && !death)
517 death = time(NULL) + lifetime;
518 if (lookup_identity(k, version) == NULL) {
519 Identity *id = xmalloc(sizeof(Identity));
521 id->comment = comment;
523 id->confirm = confirm;
524 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
525 /* Increment the number of identities. */
532 buffer_put_int(&e->output, 1);
533 buffer_put_char(&e->output,
534 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
537 /* XXX todo: encrypt sensitive data with passphrase */
539 process_lock_agent(SocketEntry *e, int lock)
544 passwd = buffer_get_string(&e->request, NULL);
545 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
547 memset(lock_passwd, 0, strlen(lock_passwd));
551 } else if (!locked && lock) {
553 lock_passwd = xstrdup(passwd);
556 memset(passwd, 0, strlen(passwd));
559 buffer_put_int(&e->output, 1);
560 buffer_put_char(&e->output,
561 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
565 no_identities(SocketEntry *e, u_int type)
570 buffer_put_char(&msg,
571 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
572 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
573 buffer_put_int(&msg, 0);
574 buffer_put_int(&e->output, buffer_len(&msg));
575 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
581 process_add_smartcard_key (SocketEntry *e)
583 char *sc_reader_id = NULL, *pin;
584 int i, version, success = 0, death = 0, confirm = 0;
589 sc_reader_id = buffer_get_string(&e->request, NULL);
590 pin = buffer_get_string(&e->request, NULL);
592 while (buffer_len(&e->request)) {
593 switch (buffer_get_char(&e->request)) {
594 case SSH_AGENT_CONSTRAIN_LIFETIME:
595 death = time(NULL) + buffer_get_int(&e->request);
597 case SSH_AGENT_CONSTRAIN_CONFIRM:
604 if (lifetime && !death)
605 death = time(NULL) + lifetime;
607 keys = sc_get_keys(sc_reader_id, pin);
611 if (keys == NULL || keys[0] == NULL) {
612 error("sc_get_keys failed");
615 for (i = 0; keys[i] != NULL; i++) {
617 version = k->type == KEY_RSA1 ? 1 : 2;
618 tab = idtab_lookup(version);
619 if (lookup_identity(k, version) == NULL) {
620 id = xmalloc(sizeof(Identity));
622 id->comment = sc_get_key_label(k);
624 id->confirm = confirm;
625 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
635 buffer_put_int(&e->output, 1);
636 buffer_put_char(&e->output,
637 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
641 process_remove_smartcard_key(SocketEntry *e)
643 char *sc_reader_id = NULL, *pin;
644 int i, version, success = 0;
645 Key **keys, *k = NULL;
649 sc_reader_id = buffer_get_string(&e->request, NULL);
650 pin = buffer_get_string(&e->request, NULL);
651 keys = sc_get_keys(sc_reader_id, pin);
655 if (keys == NULL || keys[0] == NULL) {
656 error("sc_get_keys failed");
659 for (i = 0; keys[i] != NULL; i++) {
661 version = k->type == KEY_RSA1 ? 1 : 2;
662 if ((id = lookup_identity(k, version)) != NULL) {
663 tab = idtab_lookup(version);
664 TAILQ_REMOVE(&tab->idlist, id, next);
674 buffer_put_int(&e->output, 1);
675 buffer_put_char(&e->output,
676 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
678 #endif /* SMARTCARD */
680 /* dispatch incoming messages */
683 process_message(SocketEntry *e)
691 if (buffer_len(&e->input) < 5)
692 return; /* Incomplete message. */
693 cp = buffer_ptr(&e->input);
694 msg_len = get_u32(cp);
695 if (msg_len > 256 * 1024) {
699 if (buffer_len(&e->input) < msg_len + 4)
702 /* move the current input to e->request */
703 buffer_consume(&e->input, 4);
704 buffer_clear(&e->request);
705 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
706 buffer_consume(&e->input, msg_len);
707 type = buffer_get_char(&e->request);
709 /* check wheter agent is locked */
710 if (locked && type != SSH_AGENTC_UNLOCK) {
711 buffer_clear(&e->request);
713 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
714 case SSH2_AGENTC_REQUEST_IDENTITIES:
715 /* send empty lists */
716 no_identities(e, type);
719 /* send a fail message for all other request types */
720 buffer_put_int(&e->output, 1);
721 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
726 debug("type %d", type);
728 case SSH_AGENTC_LOCK:
729 case SSH_AGENTC_UNLOCK:
730 process_lock_agent(e, type == SSH_AGENTC_LOCK);
733 case SSH_AGENTC_RSA_CHALLENGE:
734 process_authentication_challenge1(e);
736 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
737 process_request_identities(e, 1);
739 case SSH_AGENTC_ADD_RSA_IDENTITY:
740 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
741 process_add_identity(e, 1);
743 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
744 process_remove_identity(e, 1);
746 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
747 process_remove_all_identities(e, 1);
750 case SSH2_AGENTC_SIGN_REQUEST:
751 process_sign_request2(e);
753 case SSH2_AGENTC_REQUEST_IDENTITIES:
754 process_request_identities(e, 2);
756 case SSH2_AGENTC_ADD_IDENTITY:
757 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
758 process_add_identity(e, 2);
760 case SSH2_AGENTC_REMOVE_IDENTITY:
761 process_remove_identity(e, 2);
763 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
764 process_remove_all_identities(e, 2);
767 case SSH_AGENTC_ADD_SMARTCARD_KEY:
768 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
769 process_add_smartcard_key(e);
771 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
772 process_remove_smartcard_key(e);
774 #endif /* SMARTCARD */
776 /* Unknown message. Respond with failure. */
777 error("Unknown message %d", type);
778 buffer_clear(&e->request);
779 buffer_put_int(&e->output, 1);
780 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
786 new_socket(sock_type type, int fd)
788 u_int i, old_alloc, new_alloc;
795 for (i = 0; i < sockets_alloc; i++)
796 if (sockets[i].type == AUTH_UNUSED) {
798 buffer_init(&sockets[i].input);
799 buffer_init(&sockets[i].output);
800 buffer_init(&sockets[i].request);
801 sockets[i].type = type;
804 old_alloc = sockets_alloc;
805 new_alloc = sockets_alloc + 10;
806 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
807 for (i = old_alloc; i < new_alloc; i++)
808 sockets[i].type = AUTH_UNUSED;
809 sockets_alloc = new_alloc;
810 sockets[old_alloc].fd = fd;
811 buffer_init(&sockets[old_alloc].input);
812 buffer_init(&sockets[old_alloc].output);
813 buffer_init(&sockets[old_alloc].request);
814 sockets[old_alloc].type = type;
818 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
823 for (i = 0; i < sockets_alloc; i++) {
824 switch (sockets[i].type) {
826 case AUTH_CONNECTION:
827 n = MAX(n, sockets[i].fd);
832 fatal("Unknown socket type %d", sockets[i].type);
837 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
838 if (*fdrp == NULL || sz > *nallocp) {
848 debug("XXX shrink: %d < %d", n, *fdl);
850 memset(*fdrp, 0, sz);
851 memset(*fdwp, 0, sz);
853 for (i = 0; i < sockets_alloc; i++) {
854 switch (sockets[i].type) {
856 case AUTH_CONNECTION:
857 FD_SET(sockets[i].fd, *fdrp);
858 if (buffer_len(&sockets[i].output) > 0)
859 FD_SET(sockets[i].fd, *fdwp);
869 after_select(fd_set *readset, fd_set *writeset)
871 struct sockaddr_un sunaddr;
879 for (i = 0; i < sockets_alloc; i++)
880 switch (sockets[i].type) {
884 if (FD_ISSET(sockets[i].fd, readset)) {
885 slen = sizeof(sunaddr);
886 sock = accept(sockets[i].fd,
887 (struct sockaddr *)&sunaddr, &slen);
889 error("accept from AUTH_SOCKET: %s",
893 if (getpeereid(sock, &euid, &egid) < 0) {
894 error("getpeereid %d failed: %s",
895 sock, strerror(errno));
899 if ((euid != 0) && (getuid() != euid)) {
900 error("uid mismatch: "
901 "peer euid %u != uid %u",
902 (u_int) euid, (u_int) getuid());
906 new_socket(AUTH_CONNECTION, sock);
909 case AUTH_CONNECTION:
910 if (buffer_len(&sockets[i].output) > 0 &&
911 FD_ISSET(sockets[i].fd, writeset)) {
913 len = write(sockets[i].fd,
914 buffer_ptr(&sockets[i].output),
915 buffer_len(&sockets[i].output));
916 if (len == -1 && (errno == EAGAIN ||
922 close_socket(&sockets[i]);
925 buffer_consume(&sockets[i].output, len);
927 if (FD_ISSET(sockets[i].fd, readset)) {
929 len = read(sockets[i].fd, buf, sizeof(buf));
930 if (len == -1 && (errno == EAGAIN ||
936 close_socket(&sockets[i]);
939 buffer_append(&sockets[i].input, buf, len);
940 process_message(&sockets[i]);
944 fatal("Unknown type %d", sockets[i].type);
966 cleanup_handler(int sig)
974 check_parent_exists(int sig)
976 int save_errno = errno;
978 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
979 /* printf("Parent has died - Authentication agent exiting.\n"); */
980 cleanup_handler(sig); /* safe */
982 mysignal(SIGALRM, check_parent_exists);
990 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
992 fprintf(stderr, "Options:\n");
993 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
994 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
995 fprintf(stderr, " -k Kill the current agent.\n");
996 fprintf(stderr, " -d Debug mode.\n");
997 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
998 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
1003 main(int ac, char **av)
1005 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1008 char *shell, *format, *pidstr, *agentsocket = NULL;
1009 fd_set *readsetp = NULL, *writesetp = NULL;
1010 struct sockaddr_un sunaddr;
1011 #ifdef HAVE_SETRLIMIT
1016 extern char *optarg;
1018 char pidstrbuf[1 + 3 * sizeof pid];
1020 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1027 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1028 /* Disable ptrace on Linux without sgid bit */
1029 prctl(PR_SET_DUMPABLE, 0);
1032 SSLeay_add_all_algorithms();
1034 __progname = ssh_get_progname(av[0]);
1038 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
1059 agentsocket = optarg;
1062 if ((lifetime = convtime(optarg)) == -1) {
1063 fprintf(stderr, "Invalid lifetime\n");
1074 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1077 if (ac == 0 && !c_flag && !s_flag) {
1078 shell = getenv("SHELL");
1079 if (shell != NULL &&
1080 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1084 const char *errstr = NULL;
1086 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1087 if (pidstr == NULL) {
1088 fprintf(stderr, "%s not set, cannot kill agent\n",
1089 SSH_AGENTPID_ENV_NAME);
1092 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1095 "%s=\"%s\", which is not a good PID: %s\n",
1096 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
1099 if (kill(pid, SIGTERM) == -1) {
1103 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1104 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1105 printf(format, SSH_AGENTPID_ENV_NAME);
1106 printf("echo Agent pid %ld killed;\n", (long)pid);
1109 parent_pid = getpid();
1111 if (agentsocket == NULL) {
1112 /* Create private directory for agent socket */
1113 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
1114 if (mkdtemp(socket_dir) == NULL) {
1115 perror("mkdtemp: private socket dir");
1118 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1121 /* Try to use specified agent socket */
1122 socket_dir[0] = '\0';
1123 strlcpy(socket_name, agentsocket, sizeof socket_name);
1127 * Create socket early so it will exist before command gets run from
1130 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1133 *socket_name = '\0'; /* Don't unlink any existing file */
1136 memset(&sunaddr, 0, sizeof(sunaddr));
1137 sunaddr.sun_family = AF_UNIX;
1138 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1139 prev_mask = umask(0177);
1140 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
1142 *socket_name = '\0'; /* Don't unlink any existing file */
1147 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1153 * Fork, and have the parent execute the command, if any, or present
1154 * the socket data. The child continues as the authentication agent.
1157 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1158 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1159 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1160 SSH_AUTHSOCKET_ENV_NAME);
1161 printf("echo Agent pid %ld;\n", (long)parent_pid);
1169 if (pid != 0) { /* Parent - execute the given command. */
1171 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1173 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1174 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1175 SSH_AUTHSOCKET_ENV_NAME);
1176 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1177 SSH_AGENTPID_ENV_NAME);
1178 printf("echo Agent pid %ld;\n", (long)pid);
1181 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1182 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1191 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1193 if (setsid() == -1) {
1194 error("setsid: %s", strerror(errno));
1199 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1200 /* XXX might close listen socket */
1201 (void)dup2(fd, STDIN_FILENO);
1202 (void)dup2(fd, STDOUT_FILENO);
1203 (void)dup2(fd, STDERR_FILENO);
1208 #ifdef HAVE_SETRLIMIT
1209 /* deny core dumps, since memory contains unencrypted private keys */
1210 rlim.rlim_cur = rlim.rlim_max = 0;
1211 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1212 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1218 new_socket(AUTH_SOCKET, sock);
1220 mysignal(SIGALRM, check_parent_exists);
1225 signal(SIGINT, SIG_IGN);
1226 signal(SIGPIPE, SIG_IGN);
1227 signal(SIGHUP, cleanup_handler);
1228 signal(SIGTERM, cleanup_handler);
1232 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1233 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1236 fatal("select: %s", strerror(errno));
1238 after_select(readsetp, writesetp);