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.122 2004/10/29 22:53:56 djm Exp $");
41 #include <openssl/evp.h>
42 #include <openssl/md5.h>
60 #if defined(HAVE_SYS_PRCTL_H)
61 #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
78 u_int sockets_alloc = 0;
79 SocketEntry *sockets = NULL;
81 typedef struct identity {
82 TAILQ_ENTRY(identity) next;
91 TAILQ_HEAD(idqueue, identity) idlist;
94 /* private key table, one per protocol version */
99 /* pid of shell == parent of agent */
100 pid_t parent_pid = -1;
102 /* pathname and directory for AUTH_SOCKET */
103 char socket_name[1024];
104 char socket_dir[1024];
108 char *lock_passwd = NULL;
110 extern char *__progname;
112 /* Default lifetime (0 == forever) */
113 static int lifetime = 0;
116 close_socket(SocketEntry *e)
120 e->type = AUTH_UNUSED;
121 buffer_free(&e->input);
122 buffer_free(&e->output);
123 buffer_free(&e->request);
131 for (i = 0; i <=2; i++) {
132 TAILQ_INIT(&idtable[i].idlist);
133 idtable[i].nentries = 0;
137 /* return private key table for requested protocol version */
139 idtab_lookup(int version)
141 if (version < 1 || version > 2)
142 fatal("internal error, bad protocol version %d", version);
143 return &idtable[version];
147 free_identity(Identity *id)
154 /* return matching private key for given public key */
156 lookup_identity(Key *key, int version)
160 Idtab *tab = idtab_lookup(version);
161 TAILQ_FOREACH(id, &tab->idlist, next) {
162 if (key_equal(key, id->key))
168 /* Check confirmation of keysign request */
170 confirm_key(Identity *id)
175 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
176 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
184 /* send list of supported public keys to 'client' */
186 process_request_identities(SocketEntry *e, int version)
188 Idtab *tab = idtab_lookup(version);
193 buffer_put_char(&msg, (version == 1) ?
194 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
195 buffer_put_int(&msg, tab->nentries);
196 TAILQ_FOREACH(id, &tab->idlist, next) {
197 if (id->key->type == KEY_RSA1) {
198 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
199 buffer_put_bignum(&msg, id->key->rsa->e);
200 buffer_put_bignum(&msg, id->key->rsa->n);
204 key_to_blob(id->key, &blob, &blen);
205 buffer_put_string(&msg, blob, blen);
208 buffer_put_cstring(&msg, id->comment);
210 buffer_put_int(&e->output, buffer_len(&msg));
211 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
217 process_authentication_challenge1(SocketEntry *e)
219 u_char buf[32], mdbuf[16], session_id[16];
229 key = key_new(KEY_RSA1);
230 if ((challenge = BN_new()) == NULL)
231 fatal("process_authentication_challenge1: BN_new failed");
233 (void) buffer_get_int(&e->request); /* ignored */
234 buffer_get_bignum(&e->request, key->rsa->e);
235 buffer_get_bignum(&e->request, key->rsa->n);
236 buffer_get_bignum(&e->request, challenge);
238 /* Only protocol 1.1 is supported */
239 if (buffer_len(&e->request) == 0)
241 buffer_get(&e->request, session_id, 16);
242 response_type = buffer_get_int(&e->request);
243 if (response_type != 1)
246 id = lookup_identity(key, 1);
247 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
248 Key *private = id->key;
249 /* Decrypt the challenge using the private key. */
250 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
253 /* The response is MD5 of decrypted challenge plus session id. */
254 len = BN_num_bytes(challenge);
255 if (len <= 0 || len > 32) {
256 logit("process_authentication_challenge: bad challenge length %d", len);
260 BN_bn2bin(challenge, buf + 32 - len);
262 MD5_Update(&md, buf, 32);
263 MD5_Update(&md, session_id, 16);
264 MD5_Final(mdbuf, &md);
266 /* Send the response. */
267 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
268 for (i = 0; i < 16; i++)
269 buffer_put_char(&msg, mdbuf[i]);
274 /* Unknown identity or protocol error. Send failure. */
275 buffer_put_char(&msg, SSH_AGENT_FAILURE);
277 buffer_put_int(&e->output, buffer_len(&msg));
278 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
280 BN_clear_free(challenge);
286 process_sign_request2(SocketEntry *e)
288 u_char *blob, *data, *signature = NULL;
289 u_int blen, dlen, slen = 0;
290 extern int datafellows;
297 blob = buffer_get_string(&e->request, &blen);
298 data = buffer_get_string(&e->request, &dlen);
300 flags = buffer_get_int(&e->request);
301 if (flags & SSH_AGENT_OLD_SIGNATURE)
302 datafellows = SSH_BUG_SIGBLOB;
304 key = key_from_blob(blob, blen);
306 Identity *id = lookup_identity(key, 2);
307 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
308 ok = key_sign(id->key, &signature, &slen, data, dlen);
313 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
314 buffer_put_string(&msg, signature, slen);
316 buffer_put_char(&msg, SSH_AGENT_FAILURE);
318 buffer_put_int(&e->output, buffer_len(&msg));
319 buffer_append(&e->output, buffer_ptr(&msg),
324 if (signature != NULL)
330 process_remove_identity(SocketEntry *e, int version)
339 key = key_new(KEY_RSA1);
340 bits = buffer_get_int(&e->request);
341 buffer_get_bignum(&e->request, key->rsa->e);
342 buffer_get_bignum(&e->request, key->rsa->n);
344 if (bits != key_size(key))
345 logit("Warning: identity keysize mismatch: actual %u, announced %u",
346 key_size(key), bits);
349 blob = buffer_get_string(&e->request, &blen);
350 key = key_from_blob(blob, blen);
355 Identity *id = lookup_identity(key, version);
358 * We have this key. Free the old key. Since we
359 * don\'t want to leave empty slots in the middle of
360 * the array, we actually free the key there and move
361 * all the entries between the empty slot and the end
364 Idtab *tab = idtab_lookup(version);
365 if (tab->nentries < 1)
366 fatal("process_remove_identity: "
367 "internal error: tab->nentries %d",
369 TAILQ_REMOVE(&tab->idlist, id, next);
376 buffer_put_int(&e->output, 1);
377 buffer_put_char(&e->output,
378 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
382 process_remove_all_identities(SocketEntry *e, int version)
384 Idtab *tab = idtab_lookup(version);
387 /* Loop over all identities and clear the keys. */
388 for (id = TAILQ_FIRST(&tab->idlist); id;
389 id = TAILQ_FIRST(&tab->idlist)) {
390 TAILQ_REMOVE(&tab->idlist, id, next);
394 /* Mark that there are no identities. */
398 buffer_put_int(&e->output, 1);
399 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
405 u_int now = time(NULL);
410 for (version = 1; version < 3; version++) {
411 tab = idtab_lookup(version);
412 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
413 nxt = TAILQ_NEXT(id, next);
414 if (id->death != 0 && now >= id->death) {
415 TAILQ_REMOVE(&tab->idlist, id, next);
424 process_add_identity(SocketEntry *e, int version)
426 Idtab *tab = idtab_lookup(version);
427 int type, success = 0, death = 0, confirm = 0;
428 char *type_name, *comment;
433 k = key_new_private(KEY_RSA1);
434 (void) buffer_get_int(&e->request); /* ignored */
435 buffer_get_bignum(&e->request, k->rsa->n);
436 buffer_get_bignum(&e->request, k->rsa->e);
437 buffer_get_bignum(&e->request, k->rsa->d);
438 buffer_get_bignum(&e->request, k->rsa->iqmp);
440 /* SSH and SSL have p and q swapped */
441 buffer_get_bignum(&e->request, k->rsa->q); /* p */
442 buffer_get_bignum(&e->request, k->rsa->p); /* q */
444 /* Generate additional parameters */
445 rsa_generate_additional_parameters(k->rsa);
448 type_name = buffer_get_string(&e->request, NULL);
449 type = key_type_from_name(type_name);
453 k = key_new_private(type);
454 buffer_get_bignum2(&e->request, k->dsa->p);
455 buffer_get_bignum2(&e->request, k->dsa->q);
456 buffer_get_bignum2(&e->request, k->dsa->g);
457 buffer_get_bignum2(&e->request, k->dsa->pub_key);
458 buffer_get_bignum2(&e->request, k->dsa->priv_key);
461 k = key_new_private(type);
462 buffer_get_bignum2(&e->request, k->rsa->n);
463 buffer_get_bignum2(&e->request, k->rsa->e);
464 buffer_get_bignum2(&e->request, k->rsa->d);
465 buffer_get_bignum2(&e->request, k->rsa->iqmp);
466 buffer_get_bignum2(&e->request, k->rsa->p);
467 buffer_get_bignum2(&e->request, k->rsa->q);
469 /* Generate additional parameters */
470 rsa_generate_additional_parameters(k->rsa);
473 buffer_clear(&e->request);
478 /* enable blinding */
482 if (RSA_blinding_on(k->rsa, NULL) != 1) {
483 error("process_add_identity: RSA_blinding_on failed");
489 comment = buffer_get_string(&e->request, NULL);
495 while (buffer_len(&e->request)) {
496 switch (buffer_get_char(&e->request)) {
497 case SSH_AGENT_CONSTRAIN_LIFETIME:
498 death = time(NULL) + buffer_get_int(&e->request);
500 case SSH_AGENT_CONSTRAIN_CONFIRM:
507 if (lifetime && !death)
508 death = time(NULL) + lifetime;
509 if (lookup_identity(k, version) == NULL) {
510 Identity *id = xmalloc(sizeof(Identity));
512 id->comment = comment;
514 id->confirm = confirm;
515 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
516 /* Increment the number of identities. */
523 buffer_put_int(&e->output, 1);
524 buffer_put_char(&e->output,
525 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
528 /* XXX todo: encrypt sensitive data with passphrase */
530 process_lock_agent(SocketEntry *e, int lock)
535 passwd = buffer_get_string(&e->request, NULL);
536 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
538 memset(lock_passwd, 0, strlen(lock_passwd));
542 } else if (!locked && lock) {
544 lock_passwd = xstrdup(passwd);
547 memset(passwd, 0, strlen(passwd));
550 buffer_put_int(&e->output, 1);
551 buffer_put_char(&e->output,
552 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
556 no_identities(SocketEntry *e, u_int type)
561 buffer_put_char(&msg,
562 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
563 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
564 buffer_put_int(&msg, 0);
565 buffer_put_int(&e->output, buffer_len(&msg));
566 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
572 process_add_smartcard_key (SocketEntry *e)
574 char *sc_reader_id = NULL, *pin;
575 int i, version, success = 0, death = 0, confirm = 0;
580 sc_reader_id = buffer_get_string(&e->request, NULL);
581 pin = buffer_get_string(&e->request, NULL);
583 while (buffer_len(&e->request)) {
584 switch (buffer_get_char(&e->request)) {
585 case SSH_AGENT_CONSTRAIN_LIFETIME:
586 death = time(NULL) + buffer_get_int(&e->request);
588 case SSH_AGENT_CONSTRAIN_CONFIRM:
595 if (lifetime && !death)
596 death = time(NULL) + lifetime;
598 keys = sc_get_keys(sc_reader_id, pin);
602 if (keys == NULL || keys[0] == NULL) {
603 error("sc_get_keys failed");
606 for (i = 0; keys[i] != NULL; i++) {
608 version = k->type == KEY_RSA1 ? 1 : 2;
609 tab = idtab_lookup(version);
610 if (lookup_identity(k, version) == NULL) {
611 id = xmalloc(sizeof(Identity));
613 id->comment = sc_get_key_label(k);
615 id->confirm = confirm;
616 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
626 buffer_put_int(&e->output, 1);
627 buffer_put_char(&e->output,
628 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
632 process_remove_smartcard_key(SocketEntry *e)
634 char *sc_reader_id = NULL, *pin;
635 int i, version, success = 0;
636 Key **keys, *k = NULL;
640 sc_reader_id = buffer_get_string(&e->request, NULL);
641 pin = buffer_get_string(&e->request, NULL);
642 keys = sc_get_keys(sc_reader_id, pin);
646 if (keys == NULL || keys[0] == NULL) {
647 error("sc_get_keys failed");
650 for (i = 0; keys[i] != NULL; i++) {
652 version = k->type == KEY_RSA1 ? 1 : 2;
653 if ((id = lookup_identity(k, version)) != NULL) {
654 tab = idtab_lookup(version);
655 TAILQ_REMOVE(&tab->idlist, id, next);
665 buffer_put_int(&e->output, 1);
666 buffer_put_char(&e->output,
667 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
669 #endif /* SMARTCARD */
671 /* dispatch incoming messages */
674 process_message(SocketEntry *e)
682 if (buffer_len(&e->input) < 5)
683 return; /* Incomplete message. */
684 cp = buffer_ptr(&e->input);
685 msg_len = GET_32BIT(cp);
686 if (msg_len > 256 * 1024) {
690 if (buffer_len(&e->input) < msg_len + 4)
693 /* move the current input to e->request */
694 buffer_consume(&e->input, 4);
695 buffer_clear(&e->request);
696 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
697 buffer_consume(&e->input, msg_len);
698 type = buffer_get_char(&e->request);
700 /* check wheter agent is locked */
701 if (locked && type != SSH_AGENTC_UNLOCK) {
702 buffer_clear(&e->request);
704 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
705 case SSH2_AGENTC_REQUEST_IDENTITIES:
706 /* send empty lists */
707 no_identities(e, type);
710 /* send a fail message for all other request types */
711 buffer_put_int(&e->output, 1);
712 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
717 debug("type %d", type);
719 case SSH_AGENTC_LOCK:
720 case SSH_AGENTC_UNLOCK:
721 process_lock_agent(e, type == SSH_AGENTC_LOCK);
724 case SSH_AGENTC_RSA_CHALLENGE:
725 process_authentication_challenge1(e);
727 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
728 process_request_identities(e, 1);
730 case SSH_AGENTC_ADD_RSA_IDENTITY:
731 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
732 process_add_identity(e, 1);
734 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
735 process_remove_identity(e, 1);
737 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
738 process_remove_all_identities(e, 1);
741 case SSH2_AGENTC_SIGN_REQUEST:
742 process_sign_request2(e);
744 case SSH2_AGENTC_REQUEST_IDENTITIES:
745 process_request_identities(e, 2);
747 case SSH2_AGENTC_ADD_IDENTITY:
748 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
749 process_add_identity(e, 2);
751 case SSH2_AGENTC_REMOVE_IDENTITY:
752 process_remove_identity(e, 2);
754 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
755 process_remove_all_identities(e, 2);
758 case SSH_AGENTC_ADD_SMARTCARD_KEY:
759 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
760 process_add_smartcard_key(e);
762 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
763 process_remove_smartcard_key(e);
765 #endif /* SMARTCARD */
767 /* Unknown message. Respond with failure. */
768 error("Unknown message %d", type);
769 buffer_clear(&e->request);
770 buffer_put_int(&e->output, 1);
771 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
777 new_socket(sock_type type, int fd)
779 u_int i, old_alloc, new_alloc;
786 for (i = 0; i < sockets_alloc; i++)
787 if (sockets[i].type == AUTH_UNUSED) {
789 buffer_init(&sockets[i].input);
790 buffer_init(&sockets[i].output);
791 buffer_init(&sockets[i].request);
792 sockets[i].type = type;
795 old_alloc = sockets_alloc;
796 new_alloc = sockets_alloc + 10;
798 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
800 sockets = xmalloc(new_alloc * sizeof(sockets[0]));
801 for (i = old_alloc; i < new_alloc; i++)
802 sockets[i].type = AUTH_UNUSED;
803 sockets_alloc = new_alloc;
804 sockets[old_alloc].fd = fd;
805 buffer_init(&sockets[old_alloc].input);
806 buffer_init(&sockets[old_alloc].output);
807 buffer_init(&sockets[old_alloc].request);
808 sockets[old_alloc].type = type;
812 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
817 for (i = 0; i < sockets_alloc; i++) {
818 switch (sockets[i].type) {
820 case AUTH_CONNECTION:
821 n = MAX(n, sockets[i].fd);
826 fatal("Unknown socket type %d", sockets[i].type);
831 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
832 if (*fdrp == NULL || sz > *nallocp) {
842 debug("XXX shrink: %d < %d", n, *fdl);
844 memset(*fdrp, 0, sz);
845 memset(*fdwp, 0, sz);
847 for (i = 0; i < sockets_alloc; i++) {
848 switch (sockets[i].type) {
850 case AUTH_CONNECTION:
851 FD_SET(sockets[i].fd, *fdrp);
852 if (buffer_len(&sockets[i].output) > 0)
853 FD_SET(sockets[i].fd, *fdwp);
863 after_select(fd_set *readset, fd_set *writeset)
865 struct sockaddr_un sunaddr;
873 for (i = 0; i < sockets_alloc; i++)
874 switch (sockets[i].type) {
878 if (FD_ISSET(sockets[i].fd, readset)) {
879 slen = sizeof(sunaddr);
880 sock = accept(sockets[i].fd,
881 (struct sockaddr *) &sunaddr, &slen);
883 error("accept from AUTH_SOCKET: %s",
887 if (getpeereid(sock, &euid, &egid) < 0) {
888 error("getpeereid %d failed: %s",
889 sock, strerror(errno));
893 if ((euid != 0) && (getuid() != euid)) {
894 error("uid mismatch: "
895 "peer euid %u != uid %u",
896 (u_int) euid, (u_int) getuid());
900 new_socket(AUTH_CONNECTION, sock);
903 case AUTH_CONNECTION:
904 if (buffer_len(&sockets[i].output) > 0 &&
905 FD_ISSET(sockets[i].fd, writeset)) {
907 len = write(sockets[i].fd,
908 buffer_ptr(&sockets[i].output),
909 buffer_len(&sockets[i].output));
910 if (len == -1 && (errno == EAGAIN ||
916 close_socket(&sockets[i]);
919 buffer_consume(&sockets[i].output, len);
921 if (FD_ISSET(sockets[i].fd, readset)) {
923 len = read(sockets[i].fd, buf, sizeof(buf));
924 if (len == -1 && (errno == EAGAIN ||
930 close_socket(&sockets[i]);
933 buffer_append(&sockets[i].input, buf, len);
934 process_message(&sockets[i]);
938 fatal("Unknown type %d", sockets[i].type);
959 cleanup_handler(int sig)
966 check_parent_exists(int sig)
968 int save_errno = errno;
970 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
971 /* printf("Parent has died - Authentication agent exiting.\n"); */
972 cleanup_handler(sig); /* safe */
974 mysignal(SIGALRM, check_parent_exists);
982 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
984 fprintf(stderr, "Options:\n");
985 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
986 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
987 fprintf(stderr, " -k Kill the current agent.\n");
988 fprintf(stderr, " -d Debug mode.\n");
989 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
990 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
995 main(int ac, char **av)
997 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1000 char *shell, *format, *pidstr, *agentsocket = NULL;
1001 fd_set *readsetp = NULL, *writesetp = NULL;
1002 struct sockaddr_un sunaddr;
1003 #ifdef HAVE_SETRLIMIT
1008 extern char *optarg;
1010 char pidstrbuf[1 + 3 * sizeof pid];
1017 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1018 /* Disable ptrace on Linux without sgid bit */
1019 prctl(PR_SET_DUMPABLE, 0);
1022 SSLeay_add_all_algorithms();
1024 __progname = ssh_get_progname(av[0]);
1028 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
1049 agentsocket = optarg;
1052 if ((lifetime = convtime(optarg)) == -1) {
1053 fprintf(stderr, "Invalid lifetime\n");
1064 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1067 if (ac == 0 && !c_flag && !s_flag) {
1068 shell = getenv("SHELL");
1069 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1073 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1074 if (pidstr == NULL) {
1075 fprintf(stderr, "%s not set, cannot kill agent\n",
1076 SSH_AGENTPID_ENV_NAME);
1081 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1082 SSH_AGENTPID_ENV_NAME, pidstr);
1085 if (kill(pid, SIGTERM) == -1) {
1089 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1090 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1091 printf(format, SSH_AGENTPID_ENV_NAME);
1092 printf("echo Agent pid %ld killed;\n", (long)pid);
1095 parent_pid = getpid();
1097 if (agentsocket == NULL) {
1098 /* Create private directory for agent socket */
1099 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
1100 if (mkdtemp(socket_dir) == NULL) {
1101 perror("mkdtemp: private socket dir");
1104 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1107 /* Try to use specified agent socket */
1108 socket_dir[0] = '\0';
1109 strlcpy(socket_name, agentsocket, sizeof socket_name);
1113 * Create socket early so it will exist before command gets run from
1116 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1119 *socket_name = '\0'; /* Don't unlink any existing file */
1122 memset(&sunaddr, 0, sizeof(sunaddr));
1123 sunaddr.sun_family = AF_UNIX;
1124 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1125 prev_mask = umask(0177);
1126 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1128 *socket_name = '\0'; /* Don't unlink any existing file */
1133 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1139 * Fork, and have the parent execute the command, if any, or present
1140 * the socket data. The child continues as the authentication agent.
1143 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1144 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1145 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1146 SSH_AUTHSOCKET_ENV_NAME);
1147 printf("echo Agent pid %ld;\n", (long)parent_pid);
1155 if (pid != 0) { /* Parent - execute the given command. */
1157 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1159 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1160 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1161 SSH_AUTHSOCKET_ENV_NAME);
1162 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1163 SSH_AGENTPID_ENV_NAME);
1164 printf("echo Agent pid %ld;\n", (long)pid);
1167 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1168 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1177 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1179 if (setsid() == -1) {
1180 error("setsid: %s", strerror(errno));
1185 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1186 /* XXX might close listen socket */
1187 (void)dup2(fd, STDIN_FILENO);
1188 (void)dup2(fd, STDOUT_FILENO);
1189 (void)dup2(fd, STDERR_FILENO);
1194 #ifdef HAVE_SETRLIMIT
1195 /* deny core dumps, since memory contains unencrypted private keys */
1196 rlim.rlim_cur = rlim.rlim_max = 0;
1197 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1198 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1204 new_socket(AUTH_SOCKET, sock);
1206 mysignal(SIGALRM, check_parent_exists);
1211 signal(SIGINT, SIG_IGN);
1212 signal(SIGPIPE, SIG_IGN);
1213 signal(SIGHUP, cleanup_handler);
1214 signal(SIGTERM, cleanup_handler);
1218 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1219 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1222 fatal("select: %s", strerror(errno));
1224 after_select(readsetp, writesetp);