1 /* $OpenBSD: ssh-agent.c,v 1.73 2001/11/17 19:14:34 stevesk Exp $ */
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * The authentication agent program.
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
15 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 RCSID("$OpenBSD: ssh-agent.c,v 1.73 2001/11/17 19:14:34 stevesk Exp $");
41 #include <openssl/evp.h>
42 #include <openssl/md5.h>
60 #include <openssl/engine.h>
77 u_int sockets_alloc = 0;
78 SocketEntry *sockets = NULL;
90 /* private key table, one per protocol version */
95 /* pid of shell == parent of agent */
96 pid_t parent_pid = -1;
98 /* pathname and directory for AUTH_SOCKET */
99 char socket_name[1024];
100 char socket_dir[1024];
102 #ifdef HAVE___PROGNAME
103 extern char *__progname;
112 for (i = 0; i <=2; i++){
113 idtable[i].identities = NULL;
114 idtable[i].nentries = 0;
118 /* return private key table for requested protocol version */
120 idtab_lookup(int version)
122 if (version < 1 || version > 2)
123 fatal("internal error, bad protocol version %d", version);
124 return &idtable[version];
127 /* return matching private key for given public key */
129 lookup_private_key(Key *key, int *idx, int version)
132 Idtab *tab = idtab_lookup(version);
133 for (i = 0; i < tab->nentries; i++) {
134 if (key_equal(key, tab->identities[i].key)) {
137 return tab->identities[i].key;
143 /* send list of supported public keys to 'client' */
145 process_request_identities(SocketEntry *e, int version)
147 Idtab *tab = idtab_lookup(version);
152 buffer_put_char(&msg, (version == 1) ?
153 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
154 buffer_put_int(&msg, tab->nentries);
155 for (i = 0; i < tab->nentries; i++) {
156 Identity *id = &tab->identities[i];
157 if (id->key->type == KEY_RSA1) {
158 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
159 buffer_put_bignum(&msg, id->key->rsa->e);
160 buffer_put_bignum(&msg, id->key->rsa->n);
164 key_to_blob(id->key, &blob, &blen);
165 buffer_put_string(&msg, blob, blen);
168 buffer_put_cstring(&msg, id->comment);
170 buffer_put_int(&e->output, buffer_len(&msg));
171 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
177 process_authentication_challenge1(SocketEntry *e)
184 u_char buf[32], mdbuf[16], session_id[16];
188 key = key_new(KEY_RSA1);
189 challenge = BN_new();
191 buffer_get_int(&e->input); /* ignored */
192 buffer_get_bignum(&e->input, key->rsa->e);
193 buffer_get_bignum(&e->input, key->rsa->n);
194 buffer_get_bignum(&e->input, challenge);
196 /* Only protocol 1.1 is supported */
197 if (buffer_len(&e->input) == 0)
199 buffer_get(&e->input, (char *) session_id, 16);
200 response_type = buffer_get_int(&e->input);
201 if (response_type != 1)
204 private = lookup_private_key(key, NULL, 1);
205 if (private != NULL) {
206 /* Decrypt the challenge using the private key. */
207 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
210 /* The response is MD5 of decrypted challenge plus session id. */
211 len = BN_num_bytes(challenge);
212 if (len <= 0 || len > 32) {
213 log("process_authentication_challenge: bad challenge length %d", len);
217 BN_bn2bin(challenge, buf + 32 - len);
219 MD5_Update(&md, buf, 32);
220 MD5_Update(&md, session_id, 16);
221 MD5_Final(mdbuf, &md);
223 /* Send the response. */
224 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
225 for (i = 0; i < 16; i++)
226 buffer_put_char(&msg, mdbuf[i]);
231 /* Unknown identity or protocol error. Send failure. */
232 buffer_put_char(&msg, SSH_AGENT_FAILURE);
234 buffer_put_int(&e->output, buffer_len(&msg));
235 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
237 BN_clear_free(challenge);
243 process_sign_request2(SocketEntry *e)
245 extern int datafellows;
247 u_char *blob, *data, *signature = NULL;
248 u_int blen, dlen, slen = 0;
255 blob = buffer_get_string(&e->input, &blen);
256 data = buffer_get_string(&e->input, &dlen);
258 flags = buffer_get_int(&e->input);
259 if (flags & SSH_AGENT_OLD_SIGNATURE)
260 datafellows = SSH_BUG_SIGBLOB;
262 key = key_from_blob(blob, blen);
264 private = lookup_private_key(key, NULL, 2);
266 ok = key_sign(private, &signature, &slen, data, dlen);
271 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
272 buffer_put_string(&msg, signature, slen);
274 buffer_put_char(&msg, SSH_AGENT_FAILURE);
276 buffer_put_int(&e->output, buffer_len(&msg));
277 buffer_append(&e->output, buffer_ptr(&msg),
282 if (signature != NULL)
288 process_remove_identity(SocketEntry *e, int version)
290 Key *key = NULL, *private;
298 key = key_new(KEY_RSA1);
299 bits = buffer_get_int(&e->input);
300 buffer_get_bignum(&e->input, key->rsa->e);
301 buffer_get_bignum(&e->input, key->rsa->n);
303 if (bits != key_size(key))
304 log("Warning: identity keysize mismatch: actual %d, announced %d",
305 key_size(key), bits);
308 blob = buffer_get_string(&e->input, &blen);
309 key = key_from_blob(blob, blen);
315 private = lookup_private_key(key, &idx, version);
316 if (private != NULL) {
318 * We have this key. Free the old key. Since we
319 * don\'t want to leave empty slots in the middle of
320 * the array, we actually free the key there and move
321 * all the entries between the empty slot and the end
324 Idtab *tab = idtab_lookup(version);
325 key_free(tab->identities[idx].key);
326 xfree(tab->identities[idx].comment);
327 if (tab->nentries < 1)
328 fatal("process_remove_identity: "
329 "internal error: tab->nentries %d",
331 if (idx != tab->nentries - 1) {
333 for (i = idx; i < tab->nentries - 1; i++)
334 tab->identities[i] = tab->identities[i+1];
336 tab->identities[tab->nentries - 1].key = NULL;
337 tab->identities[tab->nentries - 1].comment = NULL;
343 buffer_put_int(&e->output, 1);
344 buffer_put_char(&e->output,
345 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
349 process_remove_all_identities(SocketEntry *e, int version)
352 Idtab *tab = idtab_lookup(version);
354 /* Loop over all identities and clear the keys. */
355 for (i = 0; i < tab->nentries; i++) {
356 key_free(tab->identities[i].key);
357 xfree(tab->identities[i].comment);
360 /* Mark that there are no identities. */
364 buffer_put_int(&e->output, 1);
365 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
370 process_add_identity(SocketEntry *e, int version)
375 int type, success = 0;
376 Idtab *tab = idtab_lookup(version);
380 k = key_new_private(KEY_RSA1);
381 buffer_get_int(&e->input); /* ignored */
382 buffer_get_bignum(&e->input, k->rsa->n);
383 buffer_get_bignum(&e->input, k->rsa->e);
384 buffer_get_bignum(&e->input, k->rsa->d);
385 buffer_get_bignum(&e->input, k->rsa->iqmp);
387 /* SSH and SSL have p and q swapped */
388 buffer_get_bignum(&e->input, k->rsa->q); /* p */
389 buffer_get_bignum(&e->input, k->rsa->p); /* q */
391 /* Generate additional parameters */
392 rsa_generate_additional_parameters(k->rsa);
395 type_name = buffer_get_string(&e->input, NULL);
396 type = key_type_from_name(type_name);
400 k = key_new_private(type);
401 buffer_get_bignum2(&e->input, k->dsa->p);
402 buffer_get_bignum2(&e->input, k->dsa->q);
403 buffer_get_bignum2(&e->input, k->dsa->g);
404 buffer_get_bignum2(&e->input, k->dsa->pub_key);
405 buffer_get_bignum2(&e->input, k->dsa->priv_key);
408 k = key_new_private(type);
409 buffer_get_bignum2(&e->input, k->rsa->n);
410 buffer_get_bignum2(&e->input, k->rsa->e);
411 buffer_get_bignum2(&e->input, k->rsa->d);
412 buffer_get_bignum2(&e->input, k->rsa->iqmp);
413 buffer_get_bignum2(&e->input, k->rsa->p);
414 buffer_get_bignum2(&e->input, k->rsa->q);
416 /* Generate additional parameters */
417 rsa_generate_additional_parameters(k->rsa);
420 buffer_clear(&e->input);
425 comment = buffer_get_string(&e->input, NULL);
431 if (lookup_private_key(k, NULL, version) == NULL) {
432 if (tab->nentries == 0)
433 tab->identities = xmalloc(sizeof(Identity));
435 tab->identities = xrealloc(tab->identities,
436 (tab->nentries + 1) * sizeof(Identity));
437 tab->identities[tab->nentries].key = k;
438 tab->identities[tab->nentries].comment = comment;
439 /* Increment the number of identities. */
446 buffer_put_int(&e->output, 1);
447 buffer_put_char(&e->output,
448 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
454 process_add_smartcard_key (SocketEntry *e)
457 Key *n = NULL, *k = NULL;
458 char *sc_reader_id = NULL;
461 sc_reader_id = buffer_get_string(&e->input, NULL);
462 k = sc_get_key(sc_reader_id);
466 error("sc_get_pubkey failed");
471 tab = idtab_lookup(1);
473 if (lookup_private_key(k, NULL, 1) == NULL) {
474 if (tab->nentries == 0)
475 tab->identities = xmalloc(sizeof(Identity));
477 tab->identities = xrealloc(tab->identities,
478 (tab->nentries + 1) * sizeof(Identity));
479 n = key_new(KEY_RSA1);
480 BN_copy(n->rsa->n, k->rsa->n);
481 BN_copy(n->rsa->e, k->rsa->e);
482 RSA_set_method(n->rsa, sc_get_engine());
483 tab->identities[tab->nentries].key = n;
484 tab->identities[tab->nentries].comment =
485 xstrdup("rsa1 smartcard");
489 tab = idtab_lookup(2);
490 if (lookup_private_key(k, NULL, 2) == NULL) {
491 if (tab->nentries == 0)
492 tab->identities = xmalloc(sizeof(Identity));
494 tab->identities = xrealloc(tab->identities,
495 (tab->nentries + 1) * sizeof(Identity));
496 n = key_new(KEY_RSA);
497 BN_copy(n->rsa->n, k->rsa->n);
498 BN_copy(n->rsa->e, k->rsa->e);
499 RSA_set_method(n->rsa, sc_get_engine());
500 tab->identities[tab->nentries].key = n;
501 tab->identities[tab->nentries].comment =
502 xstrdup("rsa smartcard");
507 buffer_put_int(&e->output, 1);
508 buffer_put_char(&e->output,
509 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
513 process_remove_smartcard_key(SocketEntry *e)
515 Key *k = NULL, *private;
518 char *sc_reader_id = NULL;
520 sc_reader_id = buffer_get_string(&e->input, NULL);
521 k = sc_get_key(sc_reader_id);
525 error("sc_get_pubkey failed");
528 private = lookup_private_key(k, &idx, 1);
529 if (private != NULL) {
530 Idtab *tab = idtab_lookup(1);
531 key_free(tab->identities[idx].key);
532 xfree(tab->identities[idx].comment);
533 if (idx != tab->nentries)
534 tab->identities[idx] = tab->identities[tab->nentries];
539 private = lookup_private_key(k, &idx, 2);
540 if (private != NULL) {
541 Idtab *tab = idtab_lookup(2);
542 key_free(tab->identities[idx].key);
543 xfree(tab->identities[idx].comment);
544 if (idx != tab->nentries)
545 tab->identities[idx] = tab->identities[tab->nentries];
552 buffer_put_int(&e->output, 1);
553 buffer_put_char(&e->output,
554 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
556 #endif /* SMARTCARD */
558 /* dispatch incoming messages */
561 process_message(SocketEntry *e)
566 if (buffer_len(&e->input) < 5)
567 return; /* Incomplete message. */
568 cp = (u_char *) buffer_ptr(&e->input);
569 msg_len = GET_32BIT(cp);
570 if (msg_len > 256 * 1024) {
571 shutdown(e->fd, SHUT_RDWR);
573 e->type = AUTH_UNUSED;
576 if (buffer_len(&e->input) < msg_len + 4)
578 buffer_consume(&e->input, 4);
579 type = buffer_get_char(&e->input);
581 debug("type %d", type);
584 case SSH_AGENTC_RSA_CHALLENGE:
585 process_authentication_challenge1(e);
587 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
588 process_request_identities(e, 1);
590 case SSH_AGENTC_ADD_RSA_IDENTITY:
591 process_add_identity(e, 1);
593 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
594 process_remove_identity(e, 1);
596 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
597 process_remove_all_identities(e, 1);
600 case SSH2_AGENTC_SIGN_REQUEST:
601 process_sign_request2(e);
603 case SSH2_AGENTC_REQUEST_IDENTITIES:
604 process_request_identities(e, 2);
606 case SSH2_AGENTC_ADD_IDENTITY:
607 process_add_identity(e, 2);
609 case SSH2_AGENTC_REMOVE_IDENTITY:
610 process_remove_identity(e, 2);
612 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
613 process_remove_all_identities(e, 2);
616 case SSH_AGENTC_ADD_SMARTCARD_KEY:
617 process_add_smartcard_key(e);
619 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
620 process_remove_smartcard_key(e);
622 #endif /* SMARTCARD */
624 /* Unknown message. Respond with failure. */
625 error("Unknown message %d", type);
626 buffer_clear(&e->input);
627 buffer_put_int(&e->output, 1);
628 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
634 new_socket(sock_type type, int fd)
637 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
638 error("fcntl O_NONBLOCK: %s", strerror(errno));
643 for (i = 0; i < sockets_alloc; i++)
644 if (sockets[i].type == AUTH_UNUSED) {
646 sockets[i].type = type;
647 buffer_init(&sockets[i].input);
648 buffer_init(&sockets[i].output);
651 old_alloc = sockets_alloc;
654 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
656 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
657 for (i = old_alloc; i < sockets_alloc; i++)
658 sockets[i].type = AUTH_UNUSED;
659 sockets[old_alloc].type = type;
660 sockets[old_alloc].fd = fd;
661 buffer_init(&sockets[old_alloc].input);
662 buffer_init(&sockets[old_alloc].output);
666 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
671 for (i = 0; i < sockets_alloc; i++) {
672 switch (sockets[i].type) {
674 case AUTH_CONNECTION:
675 n = MAX(n, sockets[i].fd);
680 fatal("Unknown socket type %d", sockets[i].type);
685 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
686 if (*fdrp == NULL || sz > *nallocp) {
696 debug("XXX shrink: %d < %d", n, *fdl);
698 memset(*fdrp, 0, sz);
699 memset(*fdwp, 0, sz);
701 for (i = 0; i < sockets_alloc; i++) {
702 switch (sockets[i].type) {
704 case AUTH_CONNECTION:
705 FD_SET(sockets[i].fd, *fdrp);
706 if (buffer_len(&sockets[i].output) > 0)
707 FD_SET(sockets[i].fd, *fdwp);
717 after_select(fd_set *readset, fd_set *writeset)
723 struct sockaddr_un sunaddr;
725 for (i = 0; i < sockets_alloc; i++)
726 switch (sockets[i].type) {
730 if (FD_ISSET(sockets[i].fd, readset)) {
731 slen = sizeof(sunaddr);
732 sock = accept(sockets[i].fd,
733 (struct sockaddr *) &sunaddr, &slen);
735 perror("accept from AUTH_SOCKET");
738 new_socket(AUTH_CONNECTION, sock);
741 case AUTH_CONNECTION:
742 if (buffer_len(&sockets[i].output) > 0 &&
743 FD_ISSET(sockets[i].fd, writeset)) {
745 len = write(sockets[i].fd,
746 buffer_ptr(&sockets[i].output),
747 buffer_len(&sockets[i].output));
748 if (len == -1 && (errno == EAGAIN ||
754 shutdown(sockets[i].fd, SHUT_RDWR);
755 close(sockets[i].fd);
756 sockets[i].type = AUTH_UNUSED;
757 buffer_free(&sockets[i].input);
758 buffer_free(&sockets[i].output);
761 buffer_consume(&sockets[i].output, len);
763 if (FD_ISSET(sockets[i].fd, readset)) {
765 len = read(sockets[i].fd, buf, sizeof(buf));
766 if (len == -1 && (errno == EAGAIN ||
772 shutdown(sockets[i].fd, SHUT_RDWR);
773 close(sockets[i].fd);
774 sockets[i].type = AUTH_UNUSED;
775 buffer_free(&sockets[i].input);
776 buffer_free(&sockets[i].output);
779 buffer_append(&sockets[i].input, buf, len);
780 process_message(&sockets[i]);
784 fatal("Unknown type %d", sockets[i].type);
805 cleanup_handler(int sig)
812 check_parent_exists(int sig)
814 int save_errno = errno;
816 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
817 /* printf("Parent has died - Authentication agent exiting.\n"); */
818 cleanup_handler(sig); /* safe */
820 signal(SIGALRM, check_parent_exists);
828 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
830 fprintf(stderr, "Options:\n");
831 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
832 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
833 fprintf(stderr, " -k Kill the current agent.\n");
834 fprintf(stderr, " -d Debug mode.\n");
839 main(int ac, char **av)
841 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
842 struct sockaddr_un sunaddr;
843 #ifdef HAVE_SETRLIMIT
850 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
852 fd_set *readsetp = NULL, *writesetp = NULL;
854 SSLeay_add_all_algorithms();
856 __progname = get_progname(av[0]);
860 #ifdef __GNU_LIBRARY__
861 while ((ch = getopt(ac, av, "+cdks")) != -1) {
862 #else /* __GNU_LIBRARY__ */
863 while ((ch = getopt(ac, av, "cdks")) != -1) {
864 #endif /* __GNU_LIBRARY__ */
891 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
894 if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) {
895 shell = getenv("SHELL");
896 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
900 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
901 if (pidstr == NULL) {
902 fprintf(stderr, "%s not set, cannot kill agent\n",
903 SSH_AGENTPID_ENV_NAME);
908 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
909 SSH_AGENTPID_ENV_NAME, pidstr);
912 if (kill(pid, SIGTERM) == -1) {
916 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
917 printf(format, SSH_AUTHSOCKET_ENV_NAME);
918 printf(format, SSH_AGENTPID_ENV_NAME);
919 printf("echo Agent pid %d killed;\n", pid);
922 parent_pid = getpid();
924 /* Create private directory for agent socket */
925 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
926 if (mkdtemp(socket_dir) == NULL) {
927 perror("mkdtemp: private socket dir");
930 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
934 * Create socket early so it will exist before command gets run from
937 sock = socket(AF_UNIX, SOCK_STREAM, 0);
942 memset(&sunaddr, 0, sizeof(sunaddr));
943 sunaddr.sun_family = AF_UNIX;
944 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
946 prev_mask = umask(0177);
948 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
958 if (listen(sock, 5) < 0) {
964 * Fork, and have the parent execute the command, if any, or present
965 * the socket data. The child continues as the authentication agent.
968 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
969 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
970 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
971 SSH_AUTHSOCKET_ENV_NAME);
972 printf("echo Agent pid %d;\n", parent_pid);
980 if (pid != 0) { /* Parent - execute the given command. */
982 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
984 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
985 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
986 SSH_AUTHSOCKET_ENV_NAME);
987 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
988 SSH_AGENTPID_ENV_NAME);
989 printf("echo Agent pid %d;\n", pid);
992 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
993 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1002 if (setsid() == -1) {
1012 #ifdef HAVE_SETRLIMIT
1013 /* deny core dumps, since memory contains unencrypted private keys */
1014 rlim.rlim_cur = rlim.rlim_max = 0;
1015 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1016 perror("setrlimit rlimit_core failed");
1022 if (atexit(cleanup_socket) < 0) {
1026 new_socket(AUTH_SOCKET, sock);
1028 signal(SIGALRM, check_parent_exists);
1033 signal(SIGINT, SIG_IGN);
1034 signal(SIGPIPE, SIG_IGN);
1035 signal(SIGHUP, cleanup_handler);
1036 signal(SIGTERM, cleanup_handler);
1040 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1041 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1046 after_select(readsetp, writesetp);