1 /* $OpenBSD: ssh-agent.c,v 1.77 2001/12/29 21:56:01 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.77 2001/12/29 21:56:01 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 if ((challenge = BN_new()) == NULL)
190 fatal("process_authentication_challenge1: BN_new failed");
192 buffer_get_int(&e->input); /* ignored */
193 buffer_get_bignum(&e->input, key->rsa->e);
194 buffer_get_bignum(&e->input, key->rsa->n);
195 buffer_get_bignum(&e->input, challenge);
197 /* Only protocol 1.1 is supported */
198 if (buffer_len(&e->input) == 0)
200 buffer_get(&e->input, (char *) session_id, 16);
201 response_type = buffer_get_int(&e->input);
202 if (response_type != 1)
205 private = lookup_private_key(key, NULL, 1);
206 if (private != NULL) {
207 /* Decrypt the challenge using the private key. */
208 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
211 /* The response is MD5 of decrypted challenge plus session id. */
212 len = BN_num_bytes(challenge);
213 if (len <= 0 || len > 32) {
214 log("process_authentication_challenge: bad challenge length %d", len);
218 BN_bn2bin(challenge, buf + 32 - len);
220 MD5_Update(&md, buf, 32);
221 MD5_Update(&md, session_id, 16);
222 MD5_Final(mdbuf, &md);
224 /* Send the response. */
225 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
226 for (i = 0; i < 16; i++)
227 buffer_put_char(&msg, mdbuf[i]);
232 /* Unknown identity or protocol error. Send failure. */
233 buffer_put_char(&msg, SSH_AGENT_FAILURE);
235 buffer_put_int(&e->output, buffer_len(&msg));
236 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
238 BN_clear_free(challenge);
244 process_sign_request2(SocketEntry *e)
246 extern int datafellows;
248 u_char *blob, *data, *signature = NULL;
249 u_int blen, dlen, slen = 0;
256 blob = buffer_get_string(&e->input, &blen);
257 data = buffer_get_string(&e->input, &dlen);
259 flags = buffer_get_int(&e->input);
260 if (flags & SSH_AGENT_OLD_SIGNATURE)
261 datafellows = SSH_BUG_SIGBLOB;
263 key = key_from_blob(blob, blen);
265 private = lookup_private_key(key, NULL, 2);
267 ok = key_sign(private, &signature, &slen, data, dlen);
272 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
273 buffer_put_string(&msg, signature, slen);
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),
283 if (signature != NULL)
289 process_remove_identity(SocketEntry *e, int version)
291 Key *key = NULL, *private;
299 key = key_new(KEY_RSA1);
300 bits = buffer_get_int(&e->input);
301 buffer_get_bignum(&e->input, key->rsa->e);
302 buffer_get_bignum(&e->input, key->rsa->n);
304 if (bits != key_size(key))
305 log("Warning: identity keysize mismatch: actual %d, announced %d",
306 key_size(key), bits);
309 blob = buffer_get_string(&e->input, &blen);
310 key = key_from_blob(blob, blen);
316 private = lookup_private_key(key, &idx, version);
317 if (private != NULL) {
319 * We have this key. Free the old key. Since we
320 * don\'t want to leave empty slots in the middle of
321 * the array, we actually free the key there and move
322 * all the entries between the empty slot and the end
325 Idtab *tab = idtab_lookup(version);
326 key_free(tab->identities[idx].key);
327 xfree(tab->identities[idx].comment);
328 if (tab->nentries < 1)
329 fatal("process_remove_identity: "
330 "internal error: tab->nentries %d",
332 if (idx != tab->nentries - 1) {
334 for (i = idx; i < tab->nentries - 1; i++)
335 tab->identities[i] = tab->identities[i+1];
337 tab->identities[tab->nentries - 1].key = NULL;
338 tab->identities[tab->nentries - 1].comment = NULL;
344 buffer_put_int(&e->output, 1);
345 buffer_put_char(&e->output,
346 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
350 process_remove_all_identities(SocketEntry *e, int version)
353 Idtab *tab = idtab_lookup(version);
355 /* Loop over all identities and clear the keys. */
356 for (i = 0; i < tab->nentries; i++) {
357 key_free(tab->identities[i].key);
358 xfree(tab->identities[i].comment);
361 /* Mark that there are no identities. */
365 buffer_put_int(&e->output, 1);
366 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
371 process_add_identity(SocketEntry *e, int version)
376 int type, success = 0;
377 Idtab *tab = idtab_lookup(version);
381 k = key_new_private(KEY_RSA1);
382 buffer_get_int(&e->input); /* ignored */
383 buffer_get_bignum(&e->input, k->rsa->n);
384 buffer_get_bignum(&e->input, k->rsa->e);
385 buffer_get_bignum(&e->input, k->rsa->d);
386 buffer_get_bignum(&e->input, k->rsa->iqmp);
388 /* SSH and SSL have p and q swapped */
389 buffer_get_bignum(&e->input, k->rsa->q); /* p */
390 buffer_get_bignum(&e->input, k->rsa->p); /* q */
392 /* Generate additional parameters */
393 rsa_generate_additional_parameters(k->rsa);
396 type_name = buffer_get_string(&e->input, NULL);
397 type = key_type_from_name(type_name);
401 k = key_new_private(type);
402 buffer_get_bignum2(&e->input, k->dsa->p);
403 buffer_get_bignum2(&e->input, k->dsa->q);
404 buffer_get_bignum2(&e->input, k->dsa->g);
405 buffer_get_bignum2(&e->input, k->dsa->pub_key);
406 buffer_get_bignum2(&e->input, k->dsa->priv_key);
409 k = key_new_private(type);
410 buffer_get_bignum2(&e->input, k->rsa->n);
411 buffer_get_bignum2(&e->input, k->rsa->e);
412 buffer_get_bignum2(&e->input, k->rsa->d);
413 buffer_get_bignum2(&e->input, k->rsa->iqmp);
414 buffer_get_bignum2(&e->input, k->rsa->p);
415 buffer_get_bignum2(&e->input, k->rsa->q);
417 /* Generate additional parameters */
418 rsa_generate_additional_parameters(k->rsa);
421 buffer_clear(&e->input);
426 comment = buffer_get_string(&e->input, NULL);
432 if (lookup_private_key(k, NULL, version) == NULL) {
433 if (tab->nentries == 0)
434 tab->identities = xmalloc(sizeof(Identity));
436 tab->identities = xrealloc(tab->identities,
437 (tab->nentries + 1) * sizeof(Identity));
438 tab->identities[tab->nentries].key = k;
439 tab->identities[tab->nentries].comment = comment;
440 /* Increment the number of identities. */
447 buffer_put_int(&e->output, 1);
448 buffer_put_char(&e->output,
449 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
455 process_add_smartcard_key (SocketEntry *e)
458 Key *n = NULL, *k = NULL;
459 char *sc_reader_id = NULL;
462 sc_reader_id = buffer_get_string(&e->input, NULL);
463 k = sc_get_key(sc_reader_id);
467 error("sc_get_pubkey failed");
472 tab = idtab_lookup(1);
474 if (lookup_private_key(k, NULL, 1) == NULL) {
475 if (tab->nentries == 0)
476 tab->identities = xmalloc(sizeof(Identity));
478 tab->identities = xrealloc(tab->identities,
479 (tab->nentries + 1) * sizeof(Identity));
480 n = key_new(KEY_RSA1);
481 BN_copy(n->rsa->n, k->rsa->n);
482 BN_copy(n->rsa->e, k->rsa->e);
483 RSA_set_method(n->rsa, sc_get_engine());
484 tab->identities[tab->nentries].key = n;
485 tab->identities[tab->nentries].comment =
486 xstrdup("rsa1 smartcard");
490 tab = idtab_lookup(2);
491 if (lookup_private_key(k, NULL, 2) == NULL) {
492 if (tab->nentries == 0)
493 tab->identities = xmalloc(sizeof(Identity));
495 tab->identities = xrealloc(tab->identities,
496 (tab->nentries + 1) * sizeof(Identity));
497 n = key_new(KEY_RSA);
498 BN_copy(n->rsa->n, k->rsa->n);
499 BN_copy(n->rsa->e, k->rsa->e);
500 RSA_set_method(n->rsa, sc_get_engine());
501 tab->identities[tab->nentries].key = n;
502 tab->identities[tab->nentries].comment =
503 xstrdup("rsa smartcard");
508 buffer_put_int(&e->output, 1);
509 buffer_put_char(&e->output,
510 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
514 process_remove_smartcard_key(SocketEntry *e)
516 Key *k = NULL, *private;
519 char *sc_reader_id = NULL;
521 sc_reader_id = buffer_get_string(&e->input, NULL);
522 k = sc_get_key(sc_reader_id);
526 error("sc_get_pubkey failed");
529 private = lookup_private_key(k, &idx, 1);
530 if (private != NULL) {
531 Idtab *tab = idtab_lookup(1);
532 key_free(tab->identities[idx].key);
533 xfree(tab->identities[idx].comment);
534 if (idx != tab->nentries)
535 tab->identities[idx] = tab->identities[tab->nentries];
540 private = lookup_private_key(k, &idx, 2);
541 if (private != NULL) {
542 Idtab *tab = idtab_lookup(2);
543 key_free(tab->identities[idx].key);
544 xfree(tab->identities[idx].comment);
545 if (idx != tab->nentries)
546 tab->identities[idx] = tab->identities[tab->nentries];
553 buffer_put_int(&e->output, 1);
554 buffer_put_char(&e->output,
555 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
557 #endif /* SMARTCARD */
559 /* dispatch incoming messages */
562 process_message(SocketEntry *e)
567 if (buffer_len(&e->input) < 5)
568 return; /* Incomplete message. */
569 cp = buffer_ptr(&e->input);
570 msg_len = GET_32BIT(cp);
571 if (msg_len > 256 * 1024) {
572 shutdown(e->fd, SHUT_RDWR);
574 e->type = AUTH_UNUSED;
577 if (buffer_len(&e->input) < msg_len + 4)
579 buffer_consume(&e->input, 4);
580 type = buffer_get_char(&e->input);
582 debug("type %d", type);
585 case SSH_AGENTC_RSA_CHALLENGE:
586 process_authentication_challenge1(e);
588 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
589 process_request_identities(e, 1);
591 case SSH_AGENTC_ADD_RSA_IDENTITY:
592 process_add_identity(e, 1);
594 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
595 process_remove_identity(e, 1);
597 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
598 process_remove_all_identities(e, 1);
601 case SSH2_AGENTC_SIGN_REQUEST:
602 process_sign_request2(e);
604 case SSH2_AGENTC_REQUEST_IDENTITIES:
605 process_request_identities(e, 2);
607 case SSH2_AGENTC_ADD_IDENTITY:
608 process_add_identity(e, 2);
610 case SSH2_AGENTC_REMOVE_IDENTITY:
611 process_remove_identity(e, 2);
613 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
614 process_remove_all_identities(e, 2);
617 case SSH_AGENTC_ADD_SMARTCARD_KEY:
618 process_add_smartcard_key(e);
620 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
621 process_remove_smartcard_key(e);
623 #endif /* SMARTCARD */
625 /* Unknown message. Respond with failure. */
626 error("Unknown message %d", type);
627 buffer_clear(&e->input);
628 buffer_put_int(&e->output, 1);
629 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
635 new_socket(sock_type type, int fd)
638 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
639 error("fcntl O_NONBLOCK: %s", strerror(errno));
644 for (i = 0; i < sockets_alloc; i++)
645 if (sockets[i].type == AUTH_UNUSED) {
647 sockets[i].type = type;
648 buffer_init(&sockets[i].input);
649 buffer_init(&sockets[i].output);
652 old_alloc = sockets_alloc;
655 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
657 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
658 for (i = old_alloc; i < sockets_alloc; i++)
659 sockets[i].type = AUTH_UNUSED;
660 sockets[old_alloc].type = type;
661 sockets[old_alloc].fd = fd;
662 buffer_init(&sockets[old_alloc].input);
663 buffer_init(&sockets[old_alloc].output);
667 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
672 for (i = 0; i < sockets_alloc; i++) {
673 switch (sockets[i].type) {
675 case AUTH_CONNECTION:
676 n = MAX(n, sockets[i].fd);
681 fatal("Unknown socket type %d", sockets[i].type);
686 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
687 if (*fdrp == NULL || sz > *nallocp) {
697 debug("XXX shrink: %d < %d", n, *fdl);
699 memset(*fdrp, 0, sz);
700 memset(*fdwp, 0, sz);
702 for (i = 0; i < sockets_alloc; i++) {
703 switch (sockets[i].type) {
705 case AUTH_CONNECTION:
706 FD_SET(sockets[i].fd, *fdrp);
707 if (buffer_len(&sockets[i].output) > 0)
708 FD_SET(sockets[i].fd, *fdwp);
718 after_select(fd_set *readset, fd_set *writeset)
724 struct sockaddr_un sunaddr;
726 for (i = 0; i < sockets_alloc; i++)
727 switch (sockets[i].type) {
731 if (FD_ISSET(sockets[i].fd, readset)) {
732 slen = sizeof(sunaddr);
733 sock = accept(sockets[i].fd,
734 (struct sockaddr *) &sunaddr, &slen);
736 perror("accept from AUTH_SOCKET");
739 new_socket(AUTH_CONNECTION, sock);
742 case AUTH_CONNECTION:
743 if (buffer_len(&sockets[i].output) > 0 &&
744 FD_ISSET(sockets[i].fd, writeset)) {
746 len = write(sockets[i].fd,
747 buffer_ptr(&sockets[i].output),
748 buffer_len(&sockets[i].output));
749 if (len == -1 && (errno == EAGAIN ||
755 shutdown(sockets[i].fd, SHUT_RDWR);
756 close(sockets[i].fd);
757 sockets[i].type = AUTH_UNUSED;
758 buffer_free(&sockets[i].input);
759 buffer_free(&sockets[i].output);
762 buffer_consume(&sockets[i].output, len);
764 if (FD_ISSET(sockets[i].fd, readset)) {
766 len = read(sockets[i].fd, buf, sizeof(buf));
767 if (len == -1 && (errno == EAGAIN ||
773 shutdown(sockets[i].fd, SHUT_RDWR);
774 close(sockets[i].fd);
775 sockets[i].type = AUTH_UNUSED;
776 buffer_free(&sockets[i].input);
777 buffer_free(&sockets[i].output);
780 buffer_append(&sockets[i].input, buf, len);
781 process_message(&sockets[i]);
785 fatal("Unknown type %d", sockets[i].type);
806 cleanup_handler(int sig)
813 check_parent_exists(int sig)
815 int save_errno = errno;
817 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
818 /* printf("Parent has died - Authentication agent exiting.\n"); */
819 cleanup_handler(sig); /* safe */
821 signal(SIGALRM, check_parent_exists);
829 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
831 fprintf(stderr, "Options:\n");
832 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
833 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
834 fprintf(stderr, " -k Kill the current agent.\n");
835 fprintf(stderr, " -d Debug mode.\n");
840 main(int ac, char **av)
842 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
843 struct sockaddr_un sunaddr;
844 #ifdef HAVE_SETRLIMIT
851 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
853 fd_set *readsetp = NULL, *writesetp = NULL;
855 SSLeay_add_all_algorithms();
857 __progname = get_progname(av[0]);
861 #ifdef __GNU_LIBRARY__
862 while ((ch = getopt(ac, av, "+cdks")) != -1) {
863 #else /* __GNU_LIBRARY__ */
864 while ((ch = getopt(ac, av, "cdks")) != -1) {
865 #endif /* __GNU_LIBRARY__ */
892 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
895 if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) {
896 shell = getenv("SHELL");
897 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
901 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
902 if (pidstr == NULL) {
903 fprintf(stderr, "%s not set, cannot kill agent\n",
904 SSH_AGENTPID_ENV_NAME);
909 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
910 SSH_AGENTPID_ENV_NAME, pidstr);
913 if (kill(pid, SIGTERM) == -1) {
917 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
918 printf(format, SSH_AUTHSOCKET_ENV_NAME);
919 printf(format, SSH_AGENTPID_ENV_NAME);
920 printf("echo Agent pid %d killed;\n", pid);
923 parent_pid = getpid();
925 /* Create private directory for agent socket */
926 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
927 if (mkdtemp(socket_dir) == NULL) {
928 perror("mkdtemp: private socket dir");
931 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
935 * Create socket early so it will exist before command gets run from
938 sock = socket(AF_UNIX, SOCK_STREAM, 0);
943 memset(&sunaddr, 0, sizeof(sunaddr));
944 sunaddr.sun_family = AF_UNIX;
945 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
947 prev_mask = umask(0177);
949 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
959 if (listen(sock, 5) < 0) {
965 * Fork, and have the parent execute the command, if any, or present
966 * the socket data. The child continues as the authentication agent.
969 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
970 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
971 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
972 SSH_AUTHSOCKET_ENV_NAME);
973 printf("echo Agent pid %d;\n", parent_pid);
981 if (pid != 0) { /* Parent - execute the given command. */
983 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
985 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
986 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
987 SSH_AUTHSOCKET_ENV_NAME);
988 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
989 SSH_AGENTPID_ENV_NAME);
990 printf("echo Agent pid %d;\n", pid);
993 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
994 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1003 if (setsid() == -1) {
1013 #ifdef HAVE_SETRLIMIT
1014 /* deny core dumps, since memory contains unencrypted private keys */
1015 rlim.rlim_cur = rlim.rlim_max = 0;
1016 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1017 perror("setrlimit rlimit_core failed");
1023 if (atexit(cleanup_socket) < 0) {
1027 new_socket(AUTH_SOCKET, sock);
1029 signal(SIGALRM, check_parent_exists);
1034 signal(SIGINT, SIG_IGN);
1035 signal(SIGPIPE, SIG_IGN);
1036 signal(SIGHUP, cleanup_handler);
1037 signal(SIGTERM, cleanup_handler);
1041 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1042 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1047 after_select(readsetp, writesetp);