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