1 /* $OpenBSD: ssh-agent.c,v 1.31 2000/04/29 18:11:52 markus Exp $ */
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * Created: Wed Mar 29 03:46:59 1995 ylo
8 * The authentication agent program.
12 RCSID("$OpenBSD: ssh-agent.c,v 1.31 2000/04/29 18:11:52 markus Exp $");
24 #include <openssl/md5.h>
29 AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
35 unsigned int sockets_alloc = 0;
36 SocketEntry *sockets = NULL;
43 unsigned int num_identities = 0;
44 Identity *identities = NULL;
48 /* pid of shell == parent of agent */
49 pid_t parent_pid = -1;
51 /* pathname and directory for AUTH_SOCKET */
52 char socket_name[1024];
53 char socket_dir[1024];
55 #ifdef HAVE___PROGNAME
56 extern char *__progname;
57 #else /* HAVE___PROGNAME */
58 static const char *__progname = "ssh-agent";
59 #endif /* HAVE___PROGNAME */
62 process_request_identity(SocketEntry *e)
68 buffer_put_char(&msg, SSH_AGENT_RSA_IDENTITIES_ANSWER);
69 buffer_put_int(&msg, num_identities);
70 for (i = 0; i < num_identities; i++) {
71 buffer_put_int(&msg, BN_num_bits(identities[i].key->n));
72 buffer_put_bignum(&msg, identities[i].key->e);
73 buffer_put_bignum(&msg, identities[i].key->n);
74 buffer_put_string(&msg, identities[i].comment,
75 strlen(identities[i].comment));
77 buffer_put_int(&e->output, buffer_len(&msg));
78 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
83 process_authentication_challenge(SocketEntry *e)
86 BIGNUM *pub_e, *pub_n, *challenge;
89 unsigned char buf[32], mdbuf[16], session_id[16];
90 unsigned int response_type;
96 pub_bits = buffer_get_int(&e->input);
97 buffer_get_bignum(&e->input, pub_e);
98 buffer_get_bignum(&e->input, pub_n);
99 buffer_get_bignum(&e->input, challenge);
100 if (buffer_len(&e->input) == 0) {
101 /* Compatibility code for old servers. */
102 memset(session_id, 0, 16);
106 buffer_get(&e->input, (char *) session_id, 16);
107 response_type = buffer_get_int(&e->input);
109 for (i = 0; i < num_identities; i++)
110 if (pub_bits == BN_num_bits(identities[i].key->n) &&
111 BN_cmp(pub_e, identities[i].key->e) == 0 &&
112 BN_cmp(pub_n, identities[i].key->n) == 0) {
113 /* Decrypt the challenge using the private key. */
114 rsa_private_decrypt(challenge, challenge, identities[i].key);
116 /* Compute the desired response. */
117 switch (response_type) {
118 case 0:/* As of protocol 1.0 */
119 /* This response type is no longer supported. */
120 log("Compatibility with ssh protocol 1.0 no longer supported.");
121 buffer_put_char(&msg, SSH_AGENT_FAILURE);
124 case 1:/* As of protocol 1.1 */
125 /* The response is MD5 of decrypted challenge plus session id. */
126 len = BN_num_bytes(challenge);
128 if (len <= 0 || len > 32) {
129 fatal("process_authentication_challenge: "
130 "bad challenge length %d", len);
133 BN_bn2bin(challenge, buf + 32 - len);
135 MD5_Update(&md, buf, 32);
136 MD5_Update(&md, session_id, 16);
137 MD5_Final(mdbuf, &md);
141 fatal("process_authentication_challenge: bad response_type %d",
146 /* Send the response. */
147 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
148 for (i = 0; i < 16; i++)
149 buffer_put_char(&msg, mdbuf[i]);
153 /* Unknown identity. Send failure. */
154 buffer_put_char(&msg, SSH_AGENT_FAILURE);
156 buffer_put_int(&e->output, buffer_len(&msg));
157 buffer_append(&e->output, buffer_ptr(&msg),
160 BN_clear_free(pub_e);
161 BN_clear_free(pub_n);
162 BN_clear_free(challenge);
166 process_remove_identity(SocketEntry *e)
175 /* Get the key from the packet. */
176 bits = buffer_get_int(&e->input);
177 buffer_get_bignum(&e->input, dummy);
178 buffer_get_bignum(&e->input, n);
180 if (bits != BN_num_bits(n))
181 log("Warning: identity keysize mismatch: actual %d, announced %d",
182 BN_num_bits(n), bits);
184 /* Check if we have the key. */
185 for (i = 0; i < num_identities; i++)
186 if (BN_cmp(identities[i].key->n, n) == 0) {
188 * We have this key. Free the old key. Since we
189 * don\'t want to leave empty slots in the middle of
190 * the array, we actually free the key there and copy
191 * data from the last entry.
193 RSA_free(identities[i].key);
194 xfree(identities[i].comment);
195 if (i < num_identities - 1)
196 identities[i] = identities[num_identities - 1];
198 BN_clear_free(dummy);
202 buffer_put_int(&e->output, 1);
203 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
206 /* We did not have the key. */
211 buffer_put_int(&e->output, 1);
212 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
216 * Removes all identities from the agent.
219 process_remove_all_identities(SocketEntry *e)
223 /* Loop over all identities and clear the keys. */
224 for (i = 0; i < num_identities; i++) {
225 RSA_free(identities[i].key);
226 xfree(identities[i].comment);
229 /* Mark that there are no identities. */
233 buffer_put_int(&e->output, 1);
234 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
239 * Adds an identity to the agent.
242 process_add_identity(SocketEntry *e)
249 if (num_identities == 0)
250 identities = xmalloc(sizeof(Identity));
252 identities = xrealloc(identities, (num_identities + 1) * sizeof(Identity));
254 identities[num_identities].key = RSA_new();
255 k = identities[num_identities].key;
256 buffer_get_int(&e->input); /* bits */
258 buffer_get_bignum(&e->input, k->n);
260 buffer_get_bignum(&e->input, k->e);
262 buffer_get_bignum(&e->input, k->d);
264 buffer_get_bignum(&e->input, k->iqmp);
265 /* SSH and SSL have p and q swapped */
267 buffer_get_bignum(&e->input, k->q); /* p */
269 buffer_get_bignum(&e->input, k->p); /* q */
271 /* Generate additional parameters */
275 BN_sub(aux, k->q, BN_value_one());
277 BN_mod(k->dmq1, k->d, aux, ctx);
279 BN_sub(aux, k->p, BN_value_one());
281 BN_mod(k->dmp1, k->d, aux, ctx);
286 identities[num_identities].comment = buffer_get_string(&e->input, NULL);
288 /* Check if we already have the key. */
289 for (i = 0; i < num_identities; i++)
290 if (BN_cmp(identities[i].key->n, k->n) == 0) {
292 * We already have this key. Clear and free the new
293 * data and return success.
296 xfree(identities[num_identities].comment);
299 buffer_put_int(&e->output, 1);
300 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
303 /* Increment the number of identities. */
306 /* Send a success message. */
307 buffer_put_int(&e->output, 1);
308 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
312 process_message(SocketEntry *e)
314 unsigned int msg_len;
317 if (buffer_len(&e->input) < 5)
318 return; /* Incomplete message. */
319 cp = (unsigned char *) buffer_ptr(&e->input);
320 msg_len = GET_32BIT(cp);
321 if (msg_len > 256 * 1024) {
322 shutdown(e->fd, SHUT_RDWR);
324 e->type = AUTH_UNUSED;
327 if (buffer_len(&e->input) < msg_len + 4)
329 buffer_consume(&e->input, 4);
330 type = buffer_get_char(&e->input);
333 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
334 process_request_identity(e);
336 case SSH_AGENTC_RSA_CHALLENGE:
337 process_authentication_challenge(e);
339 case SSH_AGENTC_ADD_RSA_IDENTITY:
340 process_add_identity(e);
342 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
343 process_remove_identity(e);
345 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
346 process_remove_all_identities(e);
349 /* Unknown message. Respond with failure. */
350 error("Unknown message %d", type);
351 buffer_clear(&e->input);
352 buffer_put_int(&e->output, 1);
353 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
359 new_socket(int type, int fd)
361 unsigned int i, old_alloc;
362 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
363 error("fcntl O_NONBLOCK: %s", strerror(errno));
368 for (i = 0; i < sockets_alloc; i++)
369 if (sockets[i].type == AUTH_UNUSED) {
371 sockets[i].type = type;
372 buffer_init(&sockets[i].input);
373 buffer_init(&sockets[i].output);
376 old_alloc = sockets_alloc;
379 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
381 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
382 for (i = old_alloc; i < sockets_alloc; i++)
383 sockets[i].type = AUTH_UNUSED;
384 sockets[old_alloc].type = type;
385 sockets[old_alloc].fd = fd;
386 buffer_init(&sockets[old_alloc].input);
387 buffer_init(&sockets[old_alloc].output);
391 prepare_select(fd_set *readset, fd_set *writeset)
394 for (i = 0; i < sockets_alloc; i++)
395 switch (sockets[i].type) {
397 case AUTH_CONNECTION:
398 FD_SET(sockets[i].fd, readset);
399 if (buffer_len(&sockets[i].output) > 0)
400 FD_SET(sockets[i].fd, writeset);
405 fatal("Unknown socket type %d", sockets[i].type);
411 after_select(fd_set *readset, fd_set *writeset)
417 struct sockaddr_un sunaddr;
419 for (i = 0; i < sockets_alloc; i++)
420 switch (sockets[i].type) {
424 if (FD_ISSET(sockets[i].fd, readset)) {
425 slen = sizeof(sunaddr);
426 sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &slen);
428 perror("accept from AUTH_SOCKET");
431 new_socket(AUTH_CONNECTION, sock);
434 case AUTH_CONNECTION:
435 if (buffer_len(&sockets[i].output) > 0 &&
436 FD_ISSET(sockets[i].fd, writeset)) {
437 len = write(sockets[i].fd, buffer_ptr(&sockets[i].output),
438 buffer_len(&sockets[i].output));
440 shutdown(sockets[i].fd, SHUT_RDWR);
441 close(sockets[i].fd);
442 sockets[i].type = AUTH_UNUSED;
443 buffer_free(&sockets[i].input);
444 buffer_free(&sockets[i].output);
447 buffer_consume(&sockets[i].output, len);
449 if (FD_ISSET(sockets[i].fd, readset)) {
450 len = read(sockets[i].fd, buf, sizeof(buf));
452 shutdown(sockets[i].fd, SHUT_RDWR);
453 close(sockets[i].fd);
454 sockets[i].type = AUTH_UNUSED;
455 buffer_free(&sockets[i].input);
456 buffer_free(&sockets[i].output);
459 buffer_append(&sockets[i].input, buf, len);
460 process_message(&sockets[i]);
464 fatal("Unknown type %d", sockets[i].type);
469 check_parent_exists(int sig)
471 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
472 /* printf("Parent has died - Authentication agent exiting.\n"); */
475 signal(SIGALRM, check_parent_exists);
496 fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
497 fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
503 main(int ac, char **av)
505 fd_set readset, writeset;
506 int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
507 struct sockaddr_un sunaddr;
509 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
512 /* check if RSA support exists */
513 if (rsa_alive() == 0) {
515 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
519 #ifdef __GNU_LIBRARY__
520 while ((ch = getopt(ac, av, "+cks")) != -1) {
521 #else /* __GNU_LIBRARY__ */
522 while ((ch = getopt(ac, av, "cks")) != -1) {
523 #endif /* __GNU_LIBRARY__ */
545 if (ac > 0 && (c_flag || k_flag || s_flag))
548 if (ac == 0 && !c_flag && !k_flag && !s_flag) {
549 shell = getenv("SHELL");
550 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
554 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
555 if (pidstr == NULL) {
556 fprintf(stderr, "%s not set, cannot kill agent\n",
557 SSH_AGENTPID_ENV_NAME);
561 if (pid < 1) { /* XXX PID_MAX check too */
562 /* Yes, PID_MAX check please */
563 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
564 SSH_AGENTPID_ENV_NAME, pidstr);
567 if (kill(pid, SIGTERM) == -1) {
571 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
572 printf(format, SSH_AUTHSOCKET_ENV_NAME);
573 printf(format, SSH_AGENTPID_ENV_NAME);
574 printf("echo Agent pid %d killed;\n", pid);
577 parent_pid = getpid();
579 /* Create private directory for agent socket */
580 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
581 if (mkdtemp(socket_dir) == NULL) {
582 perror("mkdtemp: private socket dir");
585 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
589 * Create socket early so it will exist before command gets run from
592 sock = socket(AF_UNIX, SOCK_STREAM, 0);
597 memset(&sunaddr, 0, sizeof(sunaddr));
598 sunaddr.sun_family = AF_UNIX;
599 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
600 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
604 if (listen(sock, 5) < 0) {
609 * Fork, and have the parent execute the command, if any, or present
610 * the socket data. The child continues as the authentication agent.
617 if (pid != 0) { /* Parent - execute the given command. */
619 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
621 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
622 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
623 SSH_AUTHSOCKET_ENV_NAME);
624 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
625 SSH_AGENTPID_ENV_NAME);
626 printf("echo Agent pid %d;\n", pid);
629 setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1);
630 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1);
639 if (setsid() == -1) {
643 if (atexit(cleanup_socket) < 0) {
647 new_socket(AUTH_SOCKET, sock);
649 signal(SIGALRM, check_parent_exists);
652 signal(SIGINT, SIG_IGN);
653 signal(SIGPIPE, SIG_IGN);
654 signal(SIGHUP, cleanup_exit);
655 signal(SIGTERM, cleanup_exit);
659 prepare_select(&readset, &writeset);
660 if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) {
665 after_select(&readset, &writeset);