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