Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.44
1.44 ! markus 1: /* $OpenBSD: ssh-agent.c,v 1.43 2000/12/13 23:25:44 markus 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.44 ! markus 40: RCSID("$OpenBSD: ssh-agent.c,v 1.43 2000/12/13 23:25:44 markus 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 "kex.h"
1.37 markus 58: #include "compat.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];
1.39 markus 145: if (id->key->type == KEY_RSA1) {
1.33 markus 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;
1.39 markus 152: key_to_blob(id->key, &blob, &blen);
1.33 markus 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.39 markus 176: key = key_new(KEY_RSA1);
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;
1.37 markus 236: int flags;
1.33 markus 237: Buffer msg;
238: int ok = -1;
239:
240: datafellows = 0;
1.43 markus 241:
1.33 markus 242: blob = buffer_get_string(&e->input, &blen);
243: data = buffer_get_string(&e->input, &dlen);
1.37 markus 244:
245: flags = buffer_get_int(&e->input);
246: if (flags & SSH_AGENT_OLD_SIGNATURE)
247: datafellows = SSH_BUG_SIGBLOB;
1.33 markus 248:
1.39 markus 249: key = key_from_blob(blob, blen);
1.33 markus 250: if (key != NULL) {
251: private = lookup_private_key(key, NULL, 2);
252: if (private != NULL)
1.39 markus 253: ok = key_sign(private, &signature, &slen, data, dlen);
1.33 markus 254: }
255: key_free(key);
256: buffer_init(&msg);
257: if (ok == 0) {
258: buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
259: buffer_put_string(&msg, signature, slen);
260: } else {
261: buffer_put_char(&msg, SSH_AGENT_FAILURE);
262: }
263: buffer_put_int(&e->output, buffer_len(&msg));
1.21 markus 264: buffer_append(&e->output, buffer_ptr(&msg),
1.33 markus 265: buffer_len(&msg));
1.21 markus 266: buffer_free(&msg);
1.33 markus 267: xfree(data);
268: xfree(blob);
269: if (signature != NULL)
270: xfree(signature);
1.1 deraadt 271: }
272:
1.33 markus 273: /* shared */
1.2 provos 274: void
1.33 markus 275: process_remove_identity(SocketEntry *e, int version)
1.1 deraadt 276: {
1.33 markus 277: Key *key = NULL, *private;
278: unsigned char *blob;
279: unsigned int blen;
1.21 markus 280: unsigned int bits;
1.33 markus 281: int success = 0;
1.21 markus 282:
1.33 markus 283: switch(version){
284: case 1:
1.39 markus 285: key = key_new(KEY_RSA1);
1.33 markus 286: bits = buffer_get_int(&e->input);
287: buffer_get_bignum(&e->input, key->rsa->e);
288: buffer_get_bignum(&e->input, key->rsa->n);
289:
290: if (bits != key_size(key))
291: log("Warning: identity keysize mismatch: actual %d, announced %d",
292: key_size(key), bits);
293: break;
294: case 2:
295: blob = buffer_get_string(&e->input, &blen);
1.39 markus 296: key = key_from_blob(blob, blen);
1.33 markus 297: xfree(blob);
298: break;
299: }
300: if (key != NULL) {
301: int idx;
302: private = lookup_private_key(key, &idx, version);
303: if (private != NULL) {
1.23 markus 304: /*
305: * We have this key. Free the old key. Since we
306: * don\'t want to leave empty slots in the middle of
1.40 markus 307: * the array, we actually free the key there and move
308: * all the entries between the empty slot and the end
309: * of the array.
1.23 markus 310: */
1.33 markus 311: Idtab *tab = idtab_lookup(version);
312: key_free(tab->identities[idx].key);
313: xfree(tab->identities[idx].comment);
1.38 markus 314: if (tab->nentries < 1)
315: fatal("process_remove_identity: "
316: "internal error: tab->nentries %d",
317: tab->nentries);
1.40 markus 318: if (idx != tab->nentries - 1) {
319: int i;
320: for (i = idx; i < tab->nentries - 1; i++)
321: tab->identities[i] = tab->identities[i+1];
322: }
323: tab->identities[tab->nentries - 1].key = NULL;
324: tab->identities[tab->nentries - 1].comment = NULL;
1.33 markus 325: tab->nentries--;
326: success = 1;
1.21 markus 327: }
1.33 markus 328: key_free(key);
329: }
1.1 deraadt 330: buffer_put_int(&e->output, 1);
1.33 markus 331: buffer_put_char(&e->output,
332: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1 deraadt 333: }
334:
1.2 provos 335: void
1.33 markus 336: process_remove_all_identities(SocketEntry *e, int version)
1.1 deraadt 337: {
1.21 markus 338: unsigned int i;
1.33 markus 339: Idtab *tab = idtab_lookup(version);
1.21 markus 340:
341: /* Loop over all identities and clear the keys. */
1.33 markus 342: for (i = 0; i < tab->nentries; i++) {
343: key_free(tab->identities[i].key);
344: xfree(tab->identities[i].comment);
1.21 markus 345: }
346:
347: /* Mark that there are no identities. */
1.33 markus 348: tab->nentries = 0;
1.21 markus 349:
350: /* Send success. */
351: buffer_put_int(&e->output, 1);
352: buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
353: return;
1.1 deraadt 354: }
355:
1.2 provos 356: void
1.39 markus 357: generate_additional_parameters(RSA *rsa)
358: {
359: BIGNUM *aux;
360: BN_CTX *ctx;
361: /* Generate additional parameters */
362: aux = BN_new();
363: ctx = BN_CTX_new();
364:
365: BN_sub(aux, rsa->q, BN_value_one());
366: BN_mod(rsa->dmq1, rsa->d, aux, ctx);
367:
368: BN_sub(aux, rsa->p, BN_value_one());
369: BN_mod(rsa->dmp1, rsa->d, aux, ctx);
370:
371: BN_clear_free(aux);
372: BN_CTX_free(ctx);
373: }
374:
375: void
1.33 markus 376: process_add_identity(SocketEntry *e, int version)
1.1 deraadt 377: {
1.33 markus 378: Key *k = NULL;
1.39 markus 379: char *type_name;
1.33 markus 380: char *comment;
1.39 markus 381: int type, success = 0;
1.33 markus 382: Idtab *tab = idtab_lookup(version);
383:
384: switch (version) {
385: case 1:
1.39 markus 386: k = key_new_private(KEY_RSA1);
387: buffer_get_int(&e->input); /* ignored */
388: buffer_get_bignum(&e->input, k->rsa->n);
389: buffer_get_bignum(&e->input, k->rsa->e);
390: buffer_get_bignum(&e->input, k->rsa->d);
391: buffer_get_bignum(&e->input, k->rsa->iqmp);
1.33 markus 392:
393: /* SSH and SSL have p and q swapped */
1.39 markus 394: buffer_get_bignum(&e->input, k->rsa->q); /* p */
395: buffer_get_bignum(&e->input, k->rsa->p); /* q */
1.33 markus 396:
397: /* Generate additional parameters */
1.39 markus 398: generate_additional_parameters(k->rsa);
1.33 markus 399: break;
400: case 2:
1.39 markus 401: type_name = buffer_get_string(&e->input, NULL);
402: type = key_type_from_name(type_name);
403: xfree(type_name);
404: switch(type) {
405: case KEY_DSA:
406: k = key_new_private(type);
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: break;
413: case KEY_RSA:
414: k = key_new_private(type);
415: buffer_get_bignum2(&e->input, k->rsa->n);
416: buffer_get_bignum2(&e->input, k->rsa->e);
417: buffer_get_bignum2(&e->input, k->rsa->d);
418: buffer_get_bignum2(&e->input, k->rsa->iqmp);
419: buffer_get_bignum2(&e->input, k->rsa->p);
420: buffer_get_bignum2(&e->input, k->rsa->q);
421:
422: /* Generate additional parameters */
423: generate_additional_parameters(k->rsa);
424: break;
425: default:
1.33 markus 426: buffer_clear(&e->input);
427: goto send;
1.21 markus 428: }
1.33 markus 429: break;
430: }
431: comment = buffer_get_string(&e->input, NULL);
432: if (k == NULL) {
433: xfree(comment);
434: goto send;
435: }
436: success = 1;
437: if (lookup_private_key(k, NULL, version) == NULL) {
438: if (tab->nentries == 0)
439: tab->identities = xmalloc(sizeof(Identity));
440: else
441: tab->identities = xrealloc(tab->identities,
442: (tab->nentries + 1) * sizeof(Identity));
443: tab->identities[tab->nentries].key = k;
444: tab->identities[tab->nentries].comment = comment;
445: /* Increment the number of identities. */
446: tab->nentries++;
447: } else {
448: key_free(k);
449: xfree(comment);
450: }
451: send:
1.1 deraadt 452: buffer_put_int(&e->output, 1);
1.33 markus 453: buffer_put_char(&e->output,
454: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1 deraadt 455: }
456:
1.33 markus 457: /* dispatch incoming messages */
458:
1.2 provos 459: void
460: process_message(SocketEntry *e)
1.1 deraadt 461: {
1.21 markus 462: unsigned int msg_len;
463: unsigned int type;
464: unsigned char *cp;
465: if (buffer_len(&e->input) < 5)
466: return; /* Incomplete message. */
467: cp = (unsigned char *) buffer_ptr(&e->input);
468: msg_len = GET_32BIT(cp);
469: if (msg_len > 256 * 1024) {
470: shutdown(e->fd, SHUT_RDWR);
471: close(e->fd);
472: e->type = AUTH_UNUSED;
473: return;
474: }
475: if (buffer_len(&e->input) < msg_len + 4)
476: return;
477: buffer_consume(&e->input, 4);
478: type = buffer_get_char(&e->input);
479:
480: switch (type) {
1.33 markus 481: /* ssh1 */
482: case SSH_AGENTC_RSA_CHALLENGE:
483: process_authentication_challenge1(e);
484: break;
1.21 markus 485: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33 markus 486: process_request_identities(e, 1);
1.21 markus 487: break;
488: case SSH_AGENTC_ADD_RSA_IDENTITY:
1.33 markus 489: process_add_identity(e, 1);
1.21 markus 490: break;
491: case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33 markus 492: process_remove_identity(e, 1);
1.21 markus 493: break;
494: case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.33 markus 495: process_remove_all_identities(e, 1);
496: break;
497: /* ssh2 */
498: case SSH2_AGENTC_SIGN_REQUEST:
499: process_sign_request2(e);
500: break;
501: case SSH2_AGENTC_REQUEST_IDENTITIES:
502: process_request_identities(e, 2);
503: break;
504: case SSH2_AGENTC_ADD_IDENTITY:
505: process_add_identity(e, 2);
506: break;
507: case SSH2_AGENTC_REMOVE_IDENTITY:
508: process_remove_identity(e, 2);
509: break;
510: case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
511: process_remove_all_identities(e, 2);
1.21 markus 512: break;
513: default:
514: /* Unknown message. Respond with failure. */
515: error("Unknown message %d", type);
516: buffer_clear(&e->input);
517: buffer_put_int(&e->output, 1);
518: buffer_put_char(&e->output, SSH_AGENT_FAILURE);
519: break;
520: }
1.1 deraadt 521: }
522:
1.2 provos 523: void
524: new_socket(int type, int fd)
1.1 deraadt 525: {
1.21 markus 526: unsigned int i, old_alloc;
527: if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
528: error("fcntl O_NONBLOCK: %s", strerror(errno));
529:
530: if (fd > max_fd)
531: max_fd = fd;
532:
533: for (i = 0; i < sockets_alloc; i++)
534: if (sockets[i].type == AUTH_UNUSED) {
535: sockets[i].fd = fd;
536: sockets[i].type = type;
537: buffer_init(&sockets[i].input);
538: buffer_init(&sockets[i].output);
539: return;
540: }
541: old_alloc = sockets_alloc;
542: sockets_alloc += 10;
543: if (sockets)
544: sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
545: else
546: sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
547: for (i = old_alloc; i < sockets_alloc; i++)
548: sockets[i].type = AUTH_UNUSED;
549: sockets[old_alloc].type = type;
550: sockets[old_alloc].fd = fd;
551: buffer_init(&sockets[old_alloc].input);
552: buffer_init(&sockets[old_alloc].output);
1.1 deraadt 553: }
554:
1.2 provos 555: void
556: prepare_select(fd_set *readset, fd_set *writeset)
1.1 deraadt 557: {
1.21 markus 558: unsigned int i;
559: for (i = 0; i < sockets_alloc; i++)
560: switch (sockets[i].type) {
561: case AUTH_SOCKET:
562: case AUTH_CONNECTION:
563: FD_SET(sockets[i].fd, readset);
564: if (buffer_len(&sockets[i].output) > 0)
565: FD_SET(sockets[i].fd, writeset);
566: break;
567: case AUTH_UNUSED:
568: break;
569: default:
570: fatal("Unknown socket type %d", sockets[i].type);
571: break;
572: }
573: }
574:
1.28 markus 575: void
1.21 markus 576: after_select(fd_set *readset, fd_set *writeset)
577: {
578: unsigned int i;
579: int len, sock;
1.26 markus 580: socklen_t slen;
1.21 markus 581: char buf[1024];
582: struct sockaddr_un sunaddr;
583:
584: for (i = 0; i < sockets_alloc; i++)
585: switch (sockets[i].type) {
586: case AUTH_UNUSED:
587: break;
588: case AUTH_SOCKET:
589: if (FD_ISSET(sockets[i].fd, readset)) {
1.26 markus 590: slen = sizeof(sunaddr);
591: sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &slen);
1.21 markus 592: if (sock < 0) {
593: perror("accept from AUTH_SOCKET");
594: break;
595: }
596: new_socket(AUTH_CONNECTION, sock);
597: }
598: break;
599: case AUTH_CONNECTION:
600: if (buffer_len(&sockets[i].output) > 0 &&
601: FD_ISSET(sockets[i].fd, writeset)) {
602: len = write(sockets[i].fd, buffer_ptr(&sockets[i].output),
603: buffer_len(&sockets[i].output));
604: if (len <= 0) {
605: shutdown(sockets[i].fd, SHUT_RDWR);
606: close(sockets[i].fd);
607: sockets[i].type = AUTH_UNUSED;
1.30 djm 608: buffer_free(&sockets[i].input);
609: buffer_free(&sockets[i].output);
1.21 markus 610: break;
611: }
612: buffer_consume(&sockets[i].output, len);
613: }
614: if (FD_ISSET(sockets[i].fd, readset)) {
615: len = read(sockets[i].fd, buf, sizeof(buf));
616: if (len <= 0) {
617: shutdown(sockets[i].fd, SHUT_RDWR);
618: close(sockets[i].fd);
619: sockets[i].type = AUTH_UNUSED;
1.30 djm 620: buffer_free(&sockets[i].input);
621: buffer_free(&sockets[i].output);
1.21 markus 622: break;
623: }
624: buffer_append(&sockets[i].input, buf, len);
625: process_message(&sockets[i]);
626: }
627: break;
628: default:
629: fatal("Unknown type %d", sockets[i].type);
630: }
1.1 deraadt 631: }
632:
1.6 deraadt 633: void
1.2 provos 634: check_parent_exists(int sig)
1.1 deraadt 635: {
1.29 deraadt 636: if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
1.21 markus 637: /* printf("Parent has died - Authentication agent exiting.\n"); */
638: exit(1);
639: }
640: signal(SIGALRM, check_parent_exists);
641: alarm(10);
1.1 deraadt 642: }
643:
1.15 markus 644: void
645: cleanup_socket(void)
646: {
1.44 ! markus 647: unlink(socket_name);
1.21 markus 648: rmdir(socket_dir);
1.10 markus 649: }
650:
1.15 markus 651: void
652: cleanup_exit(int i)
653: {
1.21 markus 654: cleanup_socket();
655: exit(i);
1.15 markus 656: }
657:
658: void
659: usage()
660: {
1.21 markus 661: fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
662: fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
663: __progname);
664: exit(1);
1.15 markus 665: }
666:
1.2 provos 667: int
668: main(int ac, char **av)
1.1 deraadt 669: {
1.21 markus 670: fd_set readset, writeset;
671: int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
672: struct sockaddr_un sunaddr;
1.41 markus 673: struct rlimit rlim;
1.21 markus 674: pid_t pid;
675: char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
1.43 markus 676: extern int optind;
1.21 markus 677:
678: while ((ch = getopt(ac, av, "cks")) != -1) {
679: switch (ch) {
680: case 'c':
681: if (s_flag)
682: usage();
683: c_flag++;
684: break;
685: case 'k':
686: k_flag++;
687: break;
688: case 's':
689: if (c_flag)
690: usage();
691: s_flag++;
692: break;
693: default:
694: usage();
695: }
696: }
697: ac -= optind;
698: av += optind;
699:
700: if (ac > 0 && (c_flag || k_flag || s_flag))
701: usage();
702:
703: if (ac == 0 && !c_flag && !k_flag && !s_flag) {
704: shell = getenv("SHELL");
705: if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
706: c_flag = 1;
707: }
708: if (k_flag) {
709: pidstr = getenv(SSH_AGENTPID_ENV_NAME);
710: if (pidstr == NULL) {
711: fprintf(stderr, "%s not set, cannot kill agent\n",
712: SSH_AGENTPID_ENV_NAME);
713: exit(1);
714: }
715: pid = atoi(pidstr);
716: if (pid < 1) { /* XXX PID_MAX check too */
1.29 deraadt 717: /* Yes, PID_MAX check please */
1.21 markus 718: fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
719: SSH_AGENTPID_ENV_NAME, pidstr);
720: exit(1);
721: }
722: if (kill(pid, SIGTERM) == -1) {
723: perror("kill");
724: exit(1);
725: }
726: format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
727: printf(format, SSH_AUTHSOCKET_ENV_NAME);
728: printf(format, SSH_AGENTPID_ENV_NAME);
729: printf("echo Agent pid %d killed;\n", pid);
730: exit(0);
731: }
732: parent_pid = getpid();
733:
734: /* Create private directory for agent socket */
735: strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
736: if (mkdtemp(socket_dir) == NULL) {
737: perror("mkdtemp: private socket dir");
738: exit(1);
739: }
740: snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
741: parent_pid);
742:
1.23 markus 743: /*
744: * Create socket early so it will exist before command gets run from
745: * the parent.
746: */
1.21 markus 747: sock = socket(AF_UNIX, SOCK_STREAM, 0);
748: if (sock < 0) {
749: perror("socket");
750: cleanup_exit(1);
751: }
752: memset(&sunaddr, 0, sizeof(sunaddr));
753: sunaddr.sun_family = AF_UNIX;
754: strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
755: if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
756: perror("bind");
757: cleanup_exit(1);
758: }
759: if (listen(sock, 5) < 0) {
760: perror("listen");
761: cleanup_exit(1);
762: }
1.23 markus 763: /*
764: * Fork, and have the parent execute the command, if any, or present
765: * the socket data. The child continues as the authentication agent.
766: */
1.21 markus 767: pid = fork();
768: if (pid == -1) {
769: perror("fork");
770: exit(1);
771: }
772: if (pid != 0) { /* Parent - execute the given command. */
773: close(sock);
774: snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
775: if (ac == 0) {
776: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
777: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
778: SSH_AUTHSOCKET_ENV_NAME);
779: printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
780: SSH_AGENTPID_ENV_NAME);
781: printf("echo Agent pid %d;\n", pid);
782: exit(0);
783: }
1.36 deraadt 784: if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
785: setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
786: perror("setenv");
787: exit(1);
788: }
1.21 markus 789: execvp(av[0], av);
790: perror(av[0]);
791: exit(1);
792: }
793: close(0);
794: close(1);
795: close(2);
796:
1.41 markus 797: /* deny core dumps, since memory contains unencrypted private keys */
798: rlim.rlim_cur = rlim.rlim_max = 0;
799: if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
800: perror("setrlimit rlimit_core failed");
801: cleanup_exit(1);
802: }
1.21 markus 803: if (setsid() == -1) {
804: perror("setsid");
805: cleanup_exit(1);
806: }
807: if (atexit(cleanup_socket) < 0) {
808: perror("atexit");
809: cleanup_exit(1);
810: }
811: new_socket(AUTH_SOCKET, sock);
812: if (ac > 0) {
813: signal(SIGALRM, check_parent_exists);
814: alarm(10);
815: }
1.33 markus 816: idtab_init();
1.21 markus 817: signal(SIGINT, SIG_IGN);
818: signal(SIGPIPE, SIG_IGN);
1.28 markus 819: signal(SIGHUP, cleanup_exit);
820: signal(SIGTERM, cleanup_exit);
1.21 markus 821: while (1) {
822: FD_ZERO(&readset);
823: FD_ZERO(&writeset);
824: prepare_select(&readset, &writeset);
825: if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) {
826: if (errno == EINTR)
827: continue;
828: exit(1);
829: }
830: after_select(&readset, &writeset);
1.15 markus 831: }
1.21 markus 832: /* NOTREACHED */
1.1 deraadt 833: }