Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.36
1.36 ! deraadt 1: /* $OpenBSD: ssh-agent.c,v 1.35 2000/09/07 20:27:54 deraadt Exp $ */
1.15 markus 2:
1.1 deraadt 3: /*
1.22 deraadt 4: * Author: Tatu Ylonen <ylo@cs.hut.fi>
5: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6: * All rights reserved
7: * The authentication agent program.
1.33 markus 8: *
1.35 deraadt 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".
14: *
1.33 markus 15: * SSH2 implementation,
16: * Copyright (c) 2000 Markus Friedl. All rights reserved.
1.35 deraadt 17: *
18: * Redistribution and use in source and binary forms, with or without
19: * modification, are permitted provided that the following conditions
20: * are met:
21: * 1. Redistributions of source code must retain the above copyright
22: * notice, this list of conditions and the following disclaimer.
23: * 2. Redistributions in binary form must reproduce the above copyright
24: * notice, this list of conditions and the following disclaimer in the
25: * documentation and/or other materials provided with the distribution.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.22 deraadt 37: */
1.1 deraadt 38:
39: #include "includes.h"
1.36 ! deraadt 40: RCSID("$OpenBSD: ssh-agent.c,v 1.35 2000/09/07 20:27:54 deraadt Exp $");
1.1 deraadt 41:
42: #include "ssh.h"
43: #include "rsa.h"
44: #include "buffer.h"
45: #include "bufaux.h"
46: #include "xmalloc.h"
47: #include "packet.h"
48: #include "getput.h"
49: #include "mpaux.h"
50:
1.33 markus 51: #include <openssl/evp.h>
1.27 markus 52: #include <openssl/md5.h>
1.32 markus 53: #include <openssl/dsa.h>
54: #include <openssl/rsa.h>
55: #include "key.h"
56: #include "authfd.h"
1.33 markus 57: #include "dsa.h"
58: #include "kex.h"
1.7 deraadt 59:
1.21 markus 60: typedef struct {
61: int fd;
62: enum {
63: AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
64: } type;
65: Buffer input;
66: Buffer output;
1.1 deraadt 67: } SocketEntry;
68:
69: unsigned int sockets_alloc = 0;
70: SocketEntry *sockets = NULL;
71:
1.21 markus 72: typedef struct {
1.33 markus 73: Key *key;
1.21 markus 74: char *comment;
1.1 deraadt 75: } Identity;
76:
1.33 markus 77: typedef struct {
78: int nentries;
79: Identity *identities;
80: } Idtab;
81:
82: /* private key table, one per protocol version */
83: Idtab idtable[3];
1.1 deraadt 84:
85: int max_fd = 0;
86:
1.11 markus 87: /* pid of shell == parent of agent */
1.29 deraadt 88: pid_t parent_pid = -1;
1.10 markus 89:
90: /* pathname and directory for AUTH_SOCKET */
91: char socket_name[1024];
92: char socket_dir[1024];
93:
1.20 markus 94: extern char *__progname;
95:
1.2 provos 96: void
1.33 markus 97: idtab_init(void)
1.1 deraadt 98: {
1.33 markus 99: int i;
100: for (i = 0; i <=2; i++){
101: idtable[i].identities = NULL;
102: idtable[i].nentries = 0;
103: }
104: }
105:
106: /* return private key table for requested protocol version */
107: Idtab *
108: idtab_lookup(int version)
109: {
110: if (version < 1 || version > 2)
111: fatal("internal error, bad protocol version %d", version);
112: return &idtable[version];
113: }
114:
115: /* return matching private key for given public key */
116: Key *
117: lookup_private_key(Key *key, int *idx, int version)
118: {
119: int i;
120: Idtab *tab = idtab_lookup(version);
121: for (i = 0; i < tab->nentries; i++) {
122: if (key_equal(key, tab->identities[i].key)) {
123: if (idx != NULL)
124: *idx = i;
125: return tab->identities[i].key;
126: }
127: }
128: return NULL;
129: }
130:
131: /* send list of supported public keys to 'client' */
132: void
133: process_request_identities(SocketEntry *e, int version)
134: {
135: Idtab *tab = idtab_lookup(version);
1.21 markus 136: Buffer msg;
137: int i;
1.1 deraadt 138:
1.21 markus 139: buffer_init(&msg);
1.33 markus 140: buffer_put_char(&msg, (version == 1) ?
141: SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
142: buffer_put_int(&msg, tab->nentries);
143: for (i = 0; i < tab->nentries; i++) {
144: Identity *id = &tab->identities[i];
145: if (id->key->type == KEY_RSA) {
146: buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
147: buffer_put_bignum(&msg, id->key->rsa->e);
148: buffer_put_bignum(&msg, id->key->rsa->n);
149: } else {
150: unsigned char *blob;
151: unsigned int blen;
152: dsa_make_key_blob(id->key, &blob, &blen);
153: buffer_put_string(&msg, blob, blen);
154: xfree(blob);
155: }
156: buffer_put_cstring(&msg, id->comment);
1.21 markus 157: }
158: buffer_put_int(&e->output, buffer_len(&msg));
159: buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
160: buffer_free(&msg);
1.1 deraadt 161: }
162:
1.33 markus 163: /* ssh1 only */
1.2 provos 164: void
1.33 markus 165: process_authentication_challenge1(SocketEntry *e)
1.1 deraadt 166: {
1.33 markus 167: Key *key, *private;
168: BIGNUM *challenge;
169: int i, len;
1.21 markus 170: Buffer msg;
171: MD5_CTX md;
172: unsigned char buf[32], mdbuf[16], session_id[16];
173: unsigned int response_type;
174:
175: buffer_init(&msg);
1.33 markus 176: key = key_new(KEY_RSA);
1.21 markus 177: challenge = BN_new();
1.33 markus 178:
179: buffer_get_int(&e->input); /* ignored */
180: buffer_get_bignum(&e->input, key->rsa->e);
181: buffer_get_bignum(&e->input, key->rsa->n);
1.21 markus 182: buffer_get_bignum(&e->input, challenge);
183:
1.33 markus 184: /* Only protocol 1.1 is supported */
185: if (buffer_len(&e->input) == 0)
186: goto failure;
187: buffer_get(&e->input, (char *) session_id, 16);
188: response_type = buffer_get_int(&e->input);
189: if (response_type != 1)
190: goto failure;
191:
192: private = lookup_private_key(key, NULL, 1);
193: if (private != NULL) {
194: /* Decrypt the challenge using the private key. */
195: rsa_private_decrypt(challenge, challenge, private->rsa);
196:
197: /* The response is MD5 of decrypted challenge plus session id. */
198: len = BN_num_bytes(challenge);
199: if (len <= 0 || len > 32) {
200: log("process_authentication_challenge: bad challenge length %d", len);
201: goto failure;
202: }
203: memset(buf, 0, 32);
204: BN_bn2bin(challenge, buf + 32 - len);
205: MD5_Init(&md);
206: MD5_Update(&md, buf, 32);
207: MD5_Update(&md, session_id, 16);
208: MD5_Final(mdbuf, &md);
209:
210: /* Send the response. */
211: buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
212: for (i = 0; i < 16; i++)
213: buffer_put_char(&msg, mdbuf[i]);
214: goto send;
215: }
1.21 markus 216:
1.33 markus 217: failure:
218: /* Unknown identity or protocol error. Send failure. */
1.21 markus 219: buffer_put_char(&msg, SSH_AGENT_FAILURE);
220: send:
221: buffer_put_int(&e->output, buffer_len(&msg));
1.33 markus 222: buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
223: key_free(key);
224: BN_clear_free(challenge);
225: buffer_free(&msg);
226: }
227:
228: /* ssh2 only */
229: void
230: process_sign_request2(SocketEntry *e)
231: {
232: extern int datafellows;
233: Key *key, *private;
234: unsigned char *blob, *data, *signature = NULL;
235: unsigned int blen, dlen, slen = 0;
236: Buffer msg;
237: int ok = -1;
238:
239: datafellows = 0;
240:
241: blob = buffer_get_string(&e->input, &blen);
242: data = buffer_get_string(&e->input, &dlen);
1.34 markus 243: buffer_get_int(&e->input); /* flags, unused */
1.33 markus 244:
245: key = dsa_key_from_blob(blob, blen);
246: if (key != NULL) {
247: private = lookup_private_key(key, NULL, 2);
248: if (private != NULL)
249: ok = dsa_sign(private, &signature, &slen, data, dlen);
250: }
251: key_free(key);
252: buffer_init(&msg);
253: if (ok == 0) {
254: buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
255: buffer_put_string(&msg, signature, slen);
256: } else {
257: buffer_put_char(&msg, SSH_AGENT_FAILURE);
258: }
259: buffer_put_int(&e->output, buffer_len(&msg));
1.21 markus 260: buffer_append(&e->output, buffer_ptr(&msg),
1.33 markus 261: buffer_len(&msg));
1.21 markus 262: buffer_free(&msg);
1.33 markus 263: xfree(data);
264: xfree(blob);
265: if (signature != NULL)
266: xfree(signature);
1.1 deraadt 267: }
268:
1.33 markus 269: /* shared */
1.2 provos 270: void
1.33 markus 271: process_remove_identity(SocketEntry *e, int version)
1.1 deraadt 272: {
1.33 markus 273: Key *key = NULL, *private;
274: unsigned char *blob;
275: unsigned int blen;
1.21 markus 276: unsigned int bits;
1.33 markus 277: int success = 0;
1.21 markus 278:
1.33 markus 279: switch(version){
280: case 1:
281: key = key_new(KEY_RSA);
282: bits = buffer_get_int(&e->input);
283: buffer_get_bignum(&e->input, key->rsa->e);
284: buffer_get_bignum(&e->input, key->rsa->n);
285:
286: if (bits != key_size(key))
287: log("Warning: identity keysize mismatch: actual %d, announced %d",
288: key_size(key), bits);
289: break;
290: case 2:
291: blob = buffer_get_string(&e->input, &blen);
292: key = dsa_key_from_blob(blob, blen);
293: xfree(blob);
294: break;
295: }
296: if (key != NULL) {
297: int idx;
298: private = lookup_private_key(key, &idx, version);
299: if (private != NULL) {
1.23 markus 300: /*
301: * We have this key. Free the old key. Since we
302: * don\'t want to leave empty slots in the middle of
303: * the array, we actually free the key there and copy
304: * data from the last entry.
305: */
1.33 markus 306: Idtab *tab = idtab_lookup(version);
307: key_free(tab->identities[idx].key);
308: xfree(tab->identities[idx].comment);
309: if (idx != tab->nentries)
310: tab->identities[idx] = tab->identities[tab->nentries];
311: tab->nentries--;
312: success = 1;
1.21 markus 313: }
1.33 markus 314: key_free(key);
315: }
1.1 deraadt 316: buffer_put_int(&e->output, 1);
1.33 markus 317: buffer_put_char(&e->output,
318: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1 deraadt 319: }
320:
1.2 provos 321: void
1.33 markus 322: process_remove_all_identities(SocketEntry *e, int version)
1.1 deraadt 323: {
1.21 markus 324: unsigned int i;
1.33 markus 325: Idtab *tab = idtab_lookup(version);
1.21 markus 326:
327: /* Loop over all identities and clear the keys. */
1.33 markus 328: for (i = 0; i < tab->nentries; i++) {
329: key_free(tab->identities[i].key);
330: xfree(tab->identities[i].comment);
1.21 markus 331: }
332:
333: /* Mark that there are no identities. */
1.33 markus 334: tab->nentries = 0;
1.21 markus 335:
336: /* Send success. */
337: buffer_put_int(&e->output, 1);
338: buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
339: return;
1.1 deraadt 340: }
341:
1.2 provos 342: void
1.33 markus 343: process_add_identity(SocketEntry *e, int version)
1.1 deraadt 344: {
1.33 markus 345: Key *k = NULL;
346: RSA *rsa;
1.21 markus 347: BIGNUM *aux;
348: BN_CTX *ctx;
1.33 markus 349: char *type;
350: char *comment;
351: int success = 0;
352: Idtab *tab = idtab_lookup(version);
353:
354: switch (version) {
355: case 1:
356: k = key_new(KEY_RSA);
357: rsa = k->rsa;
358:
359: /* allocate mem for private key */
360: /* XXX rsa->n and rsa->e are already allocated */
361: rsa->d = BN_new();
362: rsa->iqmp = BN_new();
363: rsa->q = BN_new();
364: rsa->p = BN_new();
365: rsa->dmq1 = BN_new();
366: rsa->dmp1 = BN_new();
367:
368: buffer_get_int(&e->input); /* ignored */
369:
370: buffer_get_bignum(&e->input, rsa->n);
371: buffer_get_bignum(&e->input, rsa->e);
372: buffer_get_bignum(&e->input, rsa->d);
373: buffer_get_bignum(&e->input, rsa->iqmp);
374:
375: /* SSH and SSL have p and q swapped */
376: buffer_get_bignum(&e->input, rsa->q); /* p */
377: buffer_get_bignum(&e->input, rsa->p); /* q */
378:
379: /* Generate additional parameters */
380: aux = BN_new();
381: ctx = BN_CTX_new();
1.21 markus 382:
1.33 markus 383: BN_sub(aux, rsa->q, BN_value_one());
384: BN_mod(rsa->dmq1, rsa->d, aux, ctx);
385:
386: BN_sub(aux, rsa->p, BN_value_one());
387: BN_mod(rsa->dmp1, rsa->d, aux, ctx);
1.21 markus 388:
1.33 markus 389: BN_clear_free(aux);
390: BN_CTX_free(ctx);
1.21 markus 391:
1.33 markus 392: break;
393: case 2:
394: type = buffer_get_string(&e->input, NULL);
395: if (strcmp(type, KEX_DSS)) {
396: buffer_clear(&e->input);
397: xfree(type);
398: goto send;
1.21 markus 399: }
1.33 markus 400: xfree(type);
1.1 deraadt 401:
1.33 markus 402: k = key_new(KEY_DSA);
403:
404: /* allocate mem for private key */
405: k->dsa->priv_key = BN_new();
406:
407: buffer_get_bignum2(&e->input, k->dsa->p);
408: buffer_get_bignum2(&e->input, k->dsa->q);
409: buffer_get_bignum2(&e->input, k->dsa->g);
410: buffer_get_bignum2(&e->input, k->dsa->pub_key);
411: buffer_get_bignum2(&e->input, k->dsa->priv_key);
412:
413: break;
414: }
415:
416: comment = buffer_get_string(&e->input, NULL);
417: if (k == NULL) {
418: xfree(comment);
419: goto send;
420: }
421: success = 1;
422: if (lookup_private_key(k, NULL, version) == NULL) {
423: if (tab->nentries == 0)
424: tab->identities = xmalloc(sizeof(Identity));
425: else
426: tab->identities = xrealloc(tab->identities,
427: (tab->nentries + 1) * sizeof(Identity));
428: tab->identities[tab->nentries].key = k;
429: tab->identities[tab->nentries].comment = comment;
430: /* Increment the number of identities. */
431: tab->nentries++;
432: } else {
433: key_free(k);
434: xfree(comment);
435: }
436: send:
1.1 deraadt 437: buffer_put_int(&e->output, 1);
1.33 markus 438: buffer_put_char(&e->output,
439: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1 deraadt 440: }
441:
1.33 markus 442: /* dispatch incoming messages */
443:
1.2 provos 444: void
445: process_message(SocketEntry *e)
1.1 deraadt 446: {
1.21 markus 447: unsigned int msg_len;
448: unsigned int type;
449: unsigned char *cp;
450: if (buffer_len(&e->input) < 5)
451: return; /* Incomplete message. */
452: cp = (unsigned char *) buffer_ptr(&e->input);
453: msg_len = GET_32BIT(cp);
454: if (msg_len > 256 * 1024) {
455: shutdown(e->fd, SHUT_RDWR);
456: close(e->fd);
457: e->type = AUTH_UNUSED;
458: return;
459: }
460: if (buffer_len(&e->input) < msg_len + 4)
461: return;
462: buffer_consume(&e->input, 4);
463: type = buffer_get_char(&e->input);
464:
465: switch (type) {
1.33 markus 466: /* ssh1 */
467: case SSH_AGENTC_RSA_CHALLENGE:
468: process_authentication_challenge1(e);
469: break;
1.21 markus 470: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33 markus 471: process_request_identities(e, 1);
1.21 markus 472: break;
473: case SSH_AGENTC_ADD_RSA_IDENTITY:
1.33 markus 474: process_add_identity(e, 1);
1.21 markus 475: break;
476: case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33 markus 477: process_remove_identity(e, 1);
1.21 markus 478: break;
479: case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.33 markus 480: process_remove_all_identities(e, 1);
481: break;
482: /* ssh2 */
483: case SSH2_AGENTC_SIGN_REQUEST:
484: process_sign_request2(e);
485: break;
486: case SSH2_AGENTC_REQUEST_IDENTITIES:
487: process_request_identities(e, 2);
488: break;
489: case SSH2_AGENTC_ADD_IDENTITY:
490: process_add_identity(e, 2);
491: break;
492: case SSH2_AGENTC_REMOVE_IDENTITY:
493: process_remove_identity(e, 2);
494: break;
495: case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
496: process_remove_all_identities(e, 2);
1.21 markus 497: break;
498: default:
499: /* Unknown message. Respond with failure. */
500: error("Unknown message %d", type);
501: buffer_clear(&e->input);
502: buffer_put_int(&e->output, 1);
503: buffer_put_char(&e->output, SSH_AGENT_FAILURE);
504: break;
505: }
1.1 deraadt 506: }
507:
1.2 provos 508: void
509: new_socket(int type, int fd)
1.1 deraadt 510: {
1.21 markus 511: unsigned int i, old_alloc;
512: if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
513: error("fcntl O_NONBLOCK: %s", strerror(errno));
514:
515: if (fd > max_fd)
516: max_fd = fd;
517:
518: for (i = 0; i < sockets_alloc; i++)
519: if (sockets[i].type == AUTH_UNUSED) {
520: sockets[i].fd = fd;
521: sockets[i].type = type;
522: buffer_init(&sockets[i].input);
523: buffer_init(&sockets[i].output);
524: return;
525: }
526: old_alloc = sockets_alloc;
527: sockets_alloc += 10;
528: if (sockets)
529: sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
530: else
531: sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
532: for (i = old_alloc; i < sockets_alloc; i++)
533: sockets[i].type = AUTH_UNUSED;
534: sockets[old_alloc].type = type;
535: sockets[old_alloc].fd = fd;
536: buffer_init(&sockets[old_alloc].input);
537: buffer_init(&sockets[old_alloc].output);
1.1 deraadt 538: }
539:
1.2 provos 540: void
541: prepare_select(fd_set *readset, fd_set *writeset)
1.1 deraadt 542: {
1.21 markus 543: unsigned int i;
544: for (i = 0; i < sockets_alloc; i++)
545: switch (sockets[i].type) {
546: case AUTH_SOCKET:
547: case AUTH_CONNECTION:
548: FD_SET(sockets[i].fd, readset);
549: if (buffer_len(&sockets[i].output) > 0)
550: FD_SET(sockets[i].fd, writeset);
551: break;
552: case AUTH_UNUSED:
553: break;
554: default:
555: fatal("Unknown socket type %d", sockets[i].type);
556: break;
557: }
558: }
559:
1.28 markus 560: void
1.21 markus 561: after_select(fd_set *readset, fd_set *writeset)
562: {
563: unsigned int i;
564: int len, sock;
1.26 markus 565: socklen_t slen;
1.21 markus 566: char buf[1024];
567: struct sockaddr_un sunaddr;
568:
569: for (i = 0; i < sockets_alloc; i++)
570: switch (sockets[i].type) {
571: case AUTH_UNUSED:
572: break;
573: case AUTH_SOCKET:
574: if (FD_ISSET(sockets[i].fd, readset)) {
1.26 markus 575: slen = sizeof(sunaddr);
576: sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &slen);
1.21 markus 577: if (sock < 0) {
578: perror("accept from AUTH_SOCKET");
579: break;
580: }
581: new_socket(AUTH_CONNECTION, sock);
582: }
583: break;
584: case AUTH_CONNECTION:
585: if (buffer_len(&sockets[i].output) > 0 &&
586: FD_ISSET(sockets[i].fd, writeset)) {
587: len = write(sockets[i].fd, buffer_ptr(&sockets[i].output),
588: buffer_len(&sockets[i].output));
589: if (len <= 0) {
590: shutdown(sockets[i].fd, SHUT_RDWR);
591: close(sockets[i].fd);
592: sockets[i].type = AUTH_UNUSED;
1.30 djm 593: buffer_free(&sockets[i].input);
594: buffer_free(&sockets[i].output);
1.21 markus 595: break;
596: }
597: buffer_consume(&sockets[i].output, len);
598: }
599: if (FD_ISSET(sockets[i].fd, readset)) {
600: len = read(sockets[i].fd, buf, sizeof(buf));
601: if (len <= 0) {
602: shutdown(sockets[i].fd, SHUT_RDWR);
603: close(sockets[i].fd);
604: sockets[i].type = AUTH_UNUSED;
1.30 djm 605: buffer_free(&sockets[i].input);
606: buffer_free(&sockets[i].output);
1.21 markus 607: break;
608: }
609: buffer_append(&sockets[i].input, buf, len);
610: process_message(&sockets[i]);
611: }
612: break;
613: default:
614: fatal("Unknown type %d", sockets[i].type);
615: }
1.1 deraadt 616: }
617:
1.6 deraadt 618: void
1.2 provos 619: check_parent_exists(int sig)
1.1 deraadt 620: {
1.29 deraadt 621: if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
1.21 markus 622: /* printf("Parent has died - Authentication agent exiting.\n"); */
623: exit(1);
624: }
625: signal(SIGALRM, check_parent_exists);
626: alarm(10);
1.1 deraadt 627: }
628:
1.15 markus 629: void
630: cleanup_socket(void)
631: {
1.21 markus 632: remove(socket_name);
633: rmdir(socket_dir);
1.10 markus 634: }
635:
1.15 markus 636: void
637: cleanup_exit(int i)
638: {
1.21 markus 639: cleanup_socket();
640: exit(i);
1.15 markus 641: }
642:
643: void
644: usage()
645: {
1.21 markus 646: fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
647: fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
648: __progname);
649: exit(1);
1.15 markus 650: }
651:
1.2 provos 652: int
653: main(int ac, char **av)
1.1 deraadt 654: {
1.21 markus 655: fd_set readset, writeset;
656: int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
657: struct sockaddr_un sunaddr;
658: pid_t pid;
659: char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
660:
661: /* check if RSA support exists */
662: if (rsa_alive() == 0) {
663: fprintf(stderr,
664: "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
665: __progname);
666: exit(1);
667: }
668: while ((ch = getopt(ac, av, "cks")) != -1) {
669: switch (ch) {
670: case 'c':
671: if (s_flag)
672: usage();
673: c_flag++;
674: break;
675: case 'k':
676: k_flag++;
677: break;
678: case 's':
679: if (c_flag)
680: usage();
681: s_flag++;
682: break;
683: default:
684: usage();
685: }
686: }
687: ac -= optind;
688: av += optind;
689:
690: if (ac > 0 && (c_flag || k_flag || s_flag))
691: usage();
692:
693: if (ac == 0 && !c_flag && !k_flag && !s_flag) {
694: shell = getenv("SHELL");
695: if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
696: c_flag = 1;
697: }
698: if (k_flag) {
699: pidstr = getenv(SSH_AGENTPID_ENV_NAME);
700: if (pidstr == NULL) {
701: fprintf(stderr, "%s not set, cannot kill agent\n",
702: SSH_AGENTPID_ENV_NAME);
703: exit(1);
704: }
705: pid = atoi(pidstr);
706: if (pid < 1) { /* XXX PID_MAX check too */
1.29 deraadt 707: /* Yes, PID_MAX check please */
1.21 markus 708: fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
709: SSH_AGENTPID_ENV_NAME, pidstr);
710: exit(1);
711: }
712: if (kill(pid, SIGTERM) == -1) {
713: perror("kill");
714: exit(1);
715: }
716: format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
717: printf(format, SSH_AUTHSOCKET_ENV_NAME);
718: printf(format, SSH_AGENTPID_ENV_NAME);
719: printf("echo Agent pid %d killed;\n", pid);
720: exit(0);
721: }
722: parent_pid = getpid();
723:
724: /* Create private directory for agent socket */
725: strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
726: if (mkdtemp(socket_dir) == NULL) {
727: perror("mkdtemp: private socket dir");
728: exit(1);
729: }
730: snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
731: parent_pid);
732:
1.23 markus 733: /*
734: * Create socket early so it will exist before command gets run from
735: * the parent.
736: */
1.21 markus 737: sock = socket(AF_UNIX, SOCK_STREAM, 0);
738: if (sock < 0) {
739: perror("socket");
740: cleanup_exit(1);
741: }
742: memset(&sunaddr, 0, sizeof(sunaddr));
743: sunaddr.sun_family = AF_UNIX;
744: strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
745: if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
746: perror("bind");
747: cleanup_exit(1);
748: }
749: if (listen(sock, 5) < 0) {
750: perror("listen");
751: cleanup_exit(1);
752: }
1.23 markus 753: /*
754: * Fork, and have the parent execute the command, if any, or present
755: * the socket data. The child continues as the authentication agent.
756: */
1.21 markus 757: pid = fork();
758: if (pid == -1) {
759: perror("fork");
760: exit(1);
761: }
762: if (pid != 0) { /* Parent - execute the given command. */
763: close(sock);
764: snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
765: if (ac == 0) {
766: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
767: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
768: SSH_AUTHSOCKET_ENV_NAME);
769: printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
770: SSH_AGENTPID_ENV_NAME);
771: printf("echo Agent pid %d;\n", pid);
772: exit(0);
773: }
1.36 ! deraadt 774: if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
! 775: setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
! 776: perror("setenv");
! 777: exit(1);
! 778: }
1.21 markus 779: execvp(av[0], av);
780: perror(av[0]);
781: exit(1);
782: }
783: close(0);
784: close(1);
785: close(2);
786:
787: if (setsid() == -1) {
788: perror("setsid");
789: cleanup_exit(1);
790: }
791: if (atexit(cleanup_socket) < 0) {
792: perror("atexit");
793: cleanup_exit(1);
794: }
795: new_socket(AUTH_SOCKET, sock);
796: if (ac > 0) {
797: signal(SIGALRM, check_parent_exists);
798: alarm(10);
799: }
1.33 markus 800: idtab_init();
1.21 markus 801: signal(SIGINT, SIG_IGN);
802: signal(SIGPIPE, SIG_IGN);
1.28 markus 803: signal(SIGHUP, cleanup_exit);
804: signal(SIGTERM, cleanup_exit);
1.21 markus 805: while (1) {
806: FD_ZERO(&readset);
807: FD_ZERO(&writeset);
808: prepare_select(&readset, &writeset);
809: if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) {
810: if (errno == EINTR)
811: continue;
812: exit(1);
813: }
814: after_select(&readset, &writeset);
1.15 markus 815: }
1.21 markus 816: /* NOTREACHED */
1.1 deraadt 817: }