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