1 /* $OpenBSD: ssh-agent.c,v 1.72 2001/08/03 10:31:30 jakob 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.72 2001/08/03 10:31:30 jakob Exp $");
41 #include <openssl/evp.h>
42 #include <openssl/md5.h>
60 #include <openssl/engine.h>
67 AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
73 u_int sockets_alloc = 0;
74 SocketEntry *sockets = NULL;
86 /* private key table, one per protocol version */
91 /* pid of shell == parent of agent */
92 pid_t parent_pid = -1;
94 /* pathname and directory for AUTH_SOCKET */
95 char socket_name[1024];
96 char socket_dir[1024];
98 #ifdef HAVE___PROGNAME
99 extern char *__progname;
108 for (i = 0; i <=2; i++){
109 idtable[i].identities = NULL;
110 idtable[i].nentries = 0;
114 /* return private key table for requested protocol version */
116 idtab_lookup(int version)
118 if (version < 1 || version > 2)
119 fatal("internal error, bad protocol version %d", version);
120 return &idtable[version];
123 /* return matching private key for given public key */
125 lookup_private_key(Key *key, int *idx, int version)
128 Idtab *tab = idtab_lookup(version);
129 for (i = 0; i < tab->nentries; i++) {
130 if (key_equal(key, tab->identities[i].key)) {
133 return tab->identities[i].key;
139 /* send list of supported public keys to 'client' */
141 process_request_identities(SocketEntry *e, int version)
143 Idtab *tab = idtab_lookup(version);
148 buffer_put_char(&msg, (version == 1) ?
149 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
150 buffer_put_int(&msg, tab->nentries);
151 for (i = 0; i < tab->nentries; i++) {
152 Identity *id = &tab->identities[i];
153 if (id->key->type == KEY_RSA1) {
154 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
155 buffer_put_bignum(&msg, id->key->rsa->e);
156 buffer_put_bignum(&msg, id->key->rsa->n);
160 key_to_blob(id->key, &blob, &blen);
161 buffer_put_string(&msg, blob, blen);
164 buffer_put_cstring(&msg, id->comment);
166 buffer_put_int(&e->output, buffer_len(&msg));
167 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
173 process_authentication_challenge1(SocketEntry *e)
180 u_char buf[32], mdbuf[16], session_id[16];
184 key = key_new(KEY_RSA1);
185 challenge = BN_new();
187 buffer_get_int(&e->input); /* ignored */
188 buffer_get_bignum(&e->input, key->rsa->e);
189 buffer_get_bignum(&e->input, key->rsa->n);
190 buffer_get_bignum(&e->input, challenge);
192 /* Only protocol 1.1 is supported */
193 if (buffer_len(&e->input) == 0)
195 buffer_get(&e->input, (char *) session_id, 16);
196 response_type = buffer_get_int(&e->input);
197 if (response_type != 1)
200 private = lookup_private_key(key, NULL, 1);
201 if (private != NULL) {
202 /* Decrypt the challenge using the private key. */
203 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
206 /* The response is MD5 of decrypted challenge plus session id. */
207 len = BN_num_bytes(challenge);
208 if (len <= 0 || len > 32) {
209 log("process_authentication_challenge: bad challenge length %d", len);
213 BN_bn2bin(challenge, buf + 32 - len);
215 MD5_Update(&md, buf, 32);
216 MD5_Update(&md, session_id, 16);
217 MD5_Final(mdbuf, &md);
219 /* Send the response. */
220 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
221 for (i = 0; i < 16; i++)
222 buffer_put_char(&msg, mdbuf[i]);
227 /* Unknown identity or protocol error. Send failure. */
228 buffer_put_char(&msg, SSH_AGENT_FAILURE);
230 buffer_put_int(&e->output, buffer_len(&msg));
231 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
233 BN_clear_free(challenge);
239 process_sign_request2(SocketEntry *e)
241 extern int datafellows;
243 u_char *blob, *data, *signature = NULL;
244 u_int blen, dlen, slen = 0;
251 blob = buffer_get_string(&e->input, &blen);
252 data = buffer_get_string(&e->input, &dlen);
254 flags = buffer_get_int(&e->input);
255 if (flags & SSH_AGENT_OLD_SIGNATURE)
256 datafellows = SSH_BUG_SIGBLOB;
258 key = key_from_blob(blob, blen);
260 private = lookup_private_key(key, NULL, 2);
262 ok = key_sign(private, &signature, &slen, data, dlen);
267 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
268 buffer_put_string(&msg, signature, slen);
270 buffer_put_char(&msg, SSH_AGENT_FAILURE);
272 buffer_put_int(&e->output, buffer_len(&msg));
273 buffer_append(&e->output, buffer_ptr(&msg),
278 if (signature != NULL)
284 process_remove_identity(SocketEntry *e, int version)
286 Key *key = NULL, *private;
294 key = key_new(KEY_RSA1);
295 bits = buffer_get_int(&e->input);
296 buffer_get_bignum(&e->input, key->rsa->e);
297 buffer_get_bignum(&e->input, key->rsa->n);
299 if (bits != key_size(key))
300 log("Warning: identity keysize mismatch: actual %d, announced %d",
301 key_size(key), bits);
304 blob = buffer_get_string(&e->input, &blen);
305 key = key_from_blob(blob, blen);
311 private = lookup_private_key(key, &idx, version);
312 if (private != NULL) {
314 * We have this key. Free the old key. Since we
315 * don\'t want to leave empty slots in the middle of
316 * the array, we actually free the key there and move
317 * all the entries between the empty slot and the end
320 Idtab *tab = idtab_lookup(version);
321 key_free(tab->identities[idx].key);
322 xfree(tab->identities[idx].comment);
323 if (tab->nentries < 1)
324 fatal("process_remove_identity: "
325 "internal error: tab->nentries %d",
327 if (idx != tab->nentries - 1) {
329 for (i = idx; i < tab->nentries - 1; i++)
330 tab->identities[i] = tab->identities[i+1];
332 tab->identities[tab->nentries - 1].key = NULL;
333 tab->identities[tab->nentries - 1].comment = NULL;
339 buffer_put_int(&e->output, 1);
340 buffer_put_char(&e->output,
341 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
345 process_remove_all_identities(SocketEntry *e, int version)
348 Idtab *tab = idtab_lookup(version);
350 /* Loop over all identities and clear the keys. */
351 for (i = 0; i < tab->nentries; i++) {
352 key_free(tab->identities[i].key);
353 xfree(tab->identities[i].comment);
356 /* Mark that there are no identities. */
360 buffer_put_int(&e->output, 1);
361 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
366 process_add_identity(SocketEntry *e, int version)
371 int type, success = 0;
372 Idtab *tab = idtab_lookup(version);
376 k = key_new_private(KEY_RSA1);
377 buffer_get_int(&e->input); /* ignored */
378 buffer_get_bignum(&e->input, k->rsa->n);
379 buffer_get_bignum(&e->input, k->rsa->e);
380 buffer_get_bignum(&e->input, k->rsa->d);
381 buffer_get_bignum(&e->input, k->rsa->iqmp);
383 /* SSH and SSL have p and q swapped */
384 buffer_get_bignum(&e->input, k->rsa->q); /* p */
385 buffer_get_bignum(&e->input, k->rsa->p); /* q */
387 /* Generate additional parameters */
388 rsa_generate_additional_parameters(k->rsa);
391 type_name = buffer_get_string(&e->input, NULL);
392 type = key_type_from_name(type_name);
396 k = key_new_private(type);
397 buffer_get_bignum2(&e->input, k->dsa->p);
398 buffer_get_bignum2(&e->input, k->dsa->q);
399 buffer_get_bignum2(&e->input, k->dsa->g);
400 buffer_get_bignum2(&e->input, k->dsa->pub_key);
401 buffer_get_bignum2(&e->input, k->dsa->priv_key);
404 k = key_new_private(type);
405 buffer_get_bignum2(&e->input, k->rsa->n);
406 buffer_get_bignum2(&e->input, k->rsa->e);
407 buffer_get_bignum2(&e->input, k->rsa->d);
408 buffer_get_bignum2(&e->input, k->rsa->iqmp);
409 buffer_get_bignum2(&e->input, k->rsa->p);
410 buffer_get_bignum2(&e->input, k->rsa->q);
412 /* Generate additional parameters */
413 rsa_generate_additional_parameters(k->rsa);
416 buffer_clear(&e->input);
421 comment = buffer_get_string(&e->input, NULL);
427 if (lookup_private_key(k, NULL, version) == NULL) {
428 if (tab->nentries == 0)
429 tab->identities = xmalloc(sizeof(Identity));
431 tab->identities = xrealloc(tab->identities,
432 (tab->nentries + 1) * sizeof(Identity));
433 tab->identities[tab->nentries].key = k;
434 tab->identities[tab->nentries].comment = comment;
435 /* Increment the number of identities. */
442 buffer_put_int(&e->output, 1);
443 buffer_put_char(&e->output,
444 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
450 process_add_smartcard_key (SocketEntry *e)
453 Key *n = NULL, *k = NULL;
454 char *sc_reader_id = NULL;
457 sc_reader_id = buffer_get_string(&e->input, NULL);
458 k = sc_get_key(sc_reader_id);
462 error("sc_get_pubkey failed");
467 tab = idtab_lookup(1);
469 if (lookup_private_key(k, NULL, 1) == NULL) {
470 if (tab->nentries == 0)
471 tab->identities = xmalloc(sizeof(Identity));
473 tab->identities = xrealloc(tab->identities,
474 (tab->nentries + 1) * sizeof(Identity));
475 n = key_new(KEY_RSA1);
476 BN_copy(n->rsa->n, k->rsa->n);
477 BN_copy(n->rsa->e, k->rsa->e);
478 RSA_set_method(n->rsa, sc_get_engine());
479 tab->identities[tab->nentries].key = n;
480 tab->identities[tab->nentries].comment =
481 xstrdup("rsa1 smartcard");
485 tab = idtab_lookup(2);
486 if (lookup_private_key(k, NULL, 2) == NULL) {
487 if (tab->nentries == 0)
488 tab->identities = xmalloc(sizeof(Identity));
490 tab->identities = xrealloc(tab->identities,
491 (tab->nentries + 1) * sizeof(Identity));
492 n = key_new(KEY_RSA);
493 BN_copy(n->rsa->n, k->rsa->n);
494 BN_copy(n->rsa->e, k->rsa->e);
495 RSA_set_method(n->rsa, sc_get_engine());
496 tab->identities[tab->nentries].key = n;
497 tab->identities[tab->nentries].comment =
498 xstrdup("rsa smartcard");
503 buffer_put_int(&e->output, 1);
504 buffer_put_char(&e->output,
505 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
509 process_remove_smartcard_key(SocketEntry *e)
511 Key *k = NULL, *private;
514 char *sc_reader_id = NULL;
516 sc_reader_id = buffer_get_string(&e->input, NULL);
517 k = sc_get_key(sc_reader_id);
521 error("sc_get_pubkey failed");
524 private = lookup_private_key(k, &idx, 1);
525 if (private != NULL) {
526 Idtab *tab = idtab_lookup(1);
527 key_free(tab->identities[idx].key);
528 xfree(tab->identities[idx].comment);
529 if (idx != tab->nentries)
530 tab->identities[idx] = tab->identities[tab->nentries];
535 private = lookup_private_key(k, &idx, 2);
536 if (private != NULL) {
537 Idtab *tab = idtab_lookup(2);
538 key_free(tab->identities[idx].key);
539 xfree(tab->identities[idx].comment);
540 if (idx != tab->nentries)
541 tab->identities[idx] = tab->identities[tab->nentries];
548 buffer_put_int(&e->output, 1);
549 buffer_put_char(&e->output,
550 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
552 #endif /* SMARTCARD */
554 /* dispatch incoming messages */
557 process_message(SocketEntry *e)
562 if (buffer_len(&e->input) < 5)
563 return; /* Incomplete message. */
564 cp = (u_char *) buffer_ptr(&e->input);
565 msg_len = GET_32BIT(cp);
566 if (msg_len > 256 * 1024) {
567 shutdown(e->fd, SHUT_RDWR);
569 e->type = AUTH_UNUSED;
572 if (buffer_len(&e->input) < msg_len + 4)
574 buffer_consume(&e->input, 4);
575 type = buffer_get_char(&e->input);
577 debug("type %d", type);
580 case SSH_AGENTC_RSA_CHALLENGE:
581 process_authentication_challenge1(e);
583 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
584 process_request_identities(e, 1);
586 case SSH_AGENTC_ADD_RSA_IDENTITY:
587 process_add_identity(e, 1);
589 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
590 process_remove_identity(e, 1);
592 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
593 process_remove_all_identities(e, 1);
596 case SSH2_AGENTC_SIGN_REQUEST:
597 process_sign_request2(e);
599 case SSH2_AGENTC_REQUEST_IDENTITIES:
600 process_request_identities(e, 2);
602 case SSH2_AGENTC_ADD_IDENTITY:
603 process_add_identity(e, 2);
605 case SSH2_AGENTC_REMOVE_IDENTITY:
606 process_remove_identity(e, 2);
608 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
609 process_remove_all_identities(e, 2);
612 case SSH_AGENTC_ADD_SMARTCARD_KEY:
613 process_add_smartcard_key(e);
615 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
616 process_remove_smartcard_key(e);
618 #endif /* SMARTCARD */
620 /* Unknown message. Respond with failure. */
621 error("Unknown message %d", type);
622 buffer_clear(&e->input);
623 buffer_put_int(&e->output, 1);
624 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
630 new_socket(int type, int fd)
633 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
634 error("fcntl O_NONBLOCK: %s", strerror(errno));
639 for (i = 0; i < sockets_alloc; i++)
640 if (sockets[i].type == AUTH_UNUSED) {
642 sockets[i].type = type;
643 buffer_init(&sockets[i].input);
644 buffer_init(&sockets[i].output);
647 old_alloc = sockets_alloc;
650 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
652 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
653 for (i = old_alloc; i < sockets_alloc; i++)
654 sockets[i].type = AUTH_UNUSED;
655 sockets[old_alloc].type = type;
656 sockets[old_alloc].fd = fd;
657 buffer_init(&sockets[old_alloc].input);
658 buffer_init(&sockets[old_alloc].output);
662 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
667 for (i = 0; i < sockets_alloc; i++) {
668 switch (sockets[i].type) {
670 case AUTH_CONNECTION:
671 n = MAX(n, sockets[i].fd);
676 fatal("Unknown socket type %d", sockets[i].type);
681 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
682 if (*fdrp == NULL || sz > *nallocp) {
692 debug("XXX shrink: %d < %d", n, *fdl);
694 memset(*fdrp, 0, sz);
695 memset(*fdwp, 0, sz);
697 for (i = 0; i < sockets_alloc; i++) {
698 switch (sockets[i].type) {
700 case AUTH_CONNECTION:
701 FD_SET(sockets[i].fd, *fdrp);
702 if (buffer_len(&sockets[i].output) > 0)
703 FD_SET(sockets[i].fd, *fdwp);
713 after_select(fd_set *readset, fd_set *writeset)
719 struct sockaddr_un sunaddr;
721 for (i = 0; i < sockets_alloc; i++)
722 switch (sockets[i].type) {
726 if (FD_ISSET(sockets[i].fd, readset)) {
727 slen = sizeof(sunaddr);
728 sock = accept(sockets[i].fd,
729 (struct sockaddr *) &sunaddr, &slen);
731 perror("accept from AUTH_SOCKET");
734 new_socket(AUTH_CONNECTION, sock);
737 case AUTH_CONNECTION:
738 if (buffer_len(&sockets[i].output) > 0 &&
739 FD_ISSET(sockets[i].fd, writeset)) {
741 len = write(sockets[i].fd,
742 buffer_ptr(&sockets[i].output),
743 buffer_len(&sockets[i].output));
744 if (len == -1 && (errno == EAGAIN ||
750 shutdown(sockets[i].fd, SHUT_RDWR);
751 close(sockets[i].fd);
752 sockets[i].type = AUTH_UNUSED;
753 buffer_free(&sockets[i].input);
754 buffer_free(&sockets[i].output);
757 buffer_consume(&sockets[i].output, len);
759 if (FD_ISSET(sockets[i].fd, readset)) {
761 len = read(sockets[i].fd, buf, sizeof(buf));
762 if (len == -1 && (errno == EAGAIN ||
768 shutdown(sockets[i].fd, SHUT_RDWR);
769 close(sockets[i].fd);
770 sockets[i].type = AUTH_UNUSED;
771 buffer_free(&sockets[i].input);
772 buffer_free(&sockets[i].output);
775 buffer_append(&sockets[i].input, buf, len);
776 process_message(&sockets[i]);
780 fatal("Unknown type %d", sockets[i].type);
801 cleanup_handler(int sig)
808 check_parent_exists(int sig)
810 int save_errno = errno;
812 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
813 /* printf("Parent has died - Authentication agent exiting.\n"); */
814 cleanup_handler(sig); /* safe */
816 signal(SIGALRM, check_parent_exists);
824 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
826 fprintf(stderr, "Options:\n");
827 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
828 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
829 fprintf(stderr, " -k Kill the current agent.\n");
830 fprintf(stderr, " -d Debug mode.\n");
835 main(int ac, char **av)
837 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
838 struct sockaddr_un sunaddr;
839 #ifdef HAVE_SETRLIMIT
846 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
848 fd_set *readsetp = NULL, *writesetp = NULL;
850 SSLeay_add_all_algorithms();
852 __progname = get_progname(av[0]);
856 #ifdef __GNU_LIBRARY__
857 while ((ch = getopt(ac, av, "+cdks")) != -1) {
858 #else /* __GNU_LIBRARY__ */
859 while ((ch = getopt(ac, av, "cdks")) != -1) {
860 #endif /* __GNU_LIBRARY__ */
887 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
890 if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) {
891 shell = getenv("SHELL");
892 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
896 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
897 if (pidstr == NULL) {
898 fprintf(stderr, "%s not set, cannot kill agent\n",
899 SSH_AGENTPID_ENV_NAME);
904 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
905 SSH_AGENTPID_ENV_NAME, pidstr);
908 if (kill(pid, SIGTERM) == -1) {
912 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
913 printf(format, SSH_AUTHSOCKET_ENV_NAME);
914 printf(format, SSH_AGENTPID_ENV_NAME);
915 printf("echo Agent pid %d killed;\n", pid);
918 parent_pid = getpid();
920 /* Create private directory for agent socket */
921 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
922 if (mkdtemp(socket_dir) == NULL) {
923 perror("mkdtemp: private socket dir");
926 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
930 * Create socket early so it will exist before command gets run from
933 sock = socket(AF_UNIX, SOCK_STREAM, 0);
938 memset(&sunaddr, 0, sizeof(sunaddr));
939 sunaddr.sun_family = AF_UNIX;
940 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
942 prev_mask = umask(0177);
944 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
954 if (listen(sock, 5) < 0) {
960 * Fork, and have the parent execute the command, if any, or present
961 * the socket data. The child continues as the authentication agent.
964 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
965 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
966 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
967 SSH_AUTHSOCKET_ENV_NAME);
968 printf("echo Agent pid %d;\n", parent_pid);
976 if (pid != 0) { /* Parent - execute the given command. */
978 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
980 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
981 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
982 SSH_AUTHSOCKET_ENV_NAME);
983 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
984 SSH_AGENTPID_ENV_NAME);
985 printf("echo Agent pid %d;\n", pid);
988 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
989 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
998 if (setsid() == -1) {
1008 #ifdef HAVE_SETRLIMIT
1009 /* deny core dumps, since memory contains unencrypted private keys */
1010 rlim.rlim_cur = rlim.rlim_max = 0;
1011 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1012 perror("setrlimit rlimit_core failed");
1018 if (atexit(cleanup_socket) < 0) {
1022 new_socket(AUTH_SOCKET, sock);
1024 signal(SIGALRM, check_parent_exists);
1029 signal(SIGINT, SIG_IGN);
1030 signal(SIGPIPE, SIG_IGN);
1031 signal(SIGHUP, cleanup_handler);
1032 signal(SIGTERM, cleanup_handler);
1036 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1037 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1042 after_select(readsetp, writesetp);