Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.52
1.52 ! deraadt 1: /* $OpenBSD: ssh-agent.c,v 1.51 2001/03/02 18:54:31 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,
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.52 ! deraadt 40: RCSID("$OpenBSD: ssh-agent.c,v 1.51 2001/03/02 18:54:31 deraadt 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.39 markus 360: generate_additional_parameters(RSA *rsa)
361: {
362: BIGNUM *aux;
363: BN_CTX *ctx;
364: /* Generate additional parameters */
365: aux = BN_new();
366: ctx = BN_CTX_new();
367:
368: BN_sub(aux, rsa->q, BN_value_one());
369: BN_mod(rsa->dmq1, rsa->d, aux, ctx);
370:
371: BN_sub(aux, rsa->p, BN_value_one());
372: BN_mod(rsa->dmp1, rsa->d, aux, ctx);
373:
374: BN_clear_free(aux);
375: BN_CTX_free(ctx);
376: }
377:
378: void
1.33 markus 379: process_add_identity(SocketEntry *e, int version)
1.1 deraadt 380: {
1.33 markus 381: Key *k = NULL;
1.39 markus 382: char *type_name;
1.33 markus 383: char *comment;
1.39 markus 384: int type, success = 0;
1.33 markus 385: Idtab *tab = idtab_lookup(version);
386:
387: switch (version) {
388: case 1:
1.39 markus 389: k = key_new_private(KEY_RSA1);
1.46 markus 390: buffer_get_int(&e->input); /* ignored */
1.39 markus 391: buffer_get_bignum(&e->input, k->rsa->n);
392: buffer_get_bignum(&e->input, k->rsa->e);
393: buffer_get_bignum(&e->input, k->rsa->d);
394: buffer_get_bignum(&e->input, k->rsa->iqmp);
1.33 markus 395:
396: /* SSH and SSL have p and q swapped */
1.39 markus 397: buffer_get_bignum(&e->input, k->rsa->q); /* p */
398: buffer_get_bignum(&e->input, k->rsa->p); /* q */
1.33 markus 399:
400: /* Generate additional parameters */
1.39 markus 401: generate_additional_parameters(k->rsa);
1.33 markus 402: break;
403: case 2:
1.39 markus 404: type_name = buffer_get_string(&e->input, NULL);
1.46 markus 405: type = key_type_from_name(type_name);
1.39 markus 406: xfree(type_name);
407: switch(type) {
408: case KEY_DSA:
409: k = key_new_private(type);
410: buffer_get_bignum2(&e->input, k->dsa->p);
411: buffer_get_bignum2(&e->input, k->dsa->q);
412: buffer_get_bignum2(&e->input, k->dsa->g);
413: buffer_get_bignum2(&e->input, k->dsa->pub_key);
414: buffer_get_bignum2(&e->input, k->dsa->priv_key);
415: break;
416: case KEY_RSA:
417: k = key_new_private(type);
418: buffer_get_bignum2(&e->input, k->rsa->n);
419: buffer_get_bignum2(&e->input, k->rsa->e);
420: buffer_get_bignum2(&e->input, k->rsa->d);
421: buffer_get_bignum2(&e->input, k->rsa->iqmp);
422: buffer_get_bignum2(&e->input, k->rsa->p);
423: buffer_get_bignum2(&e->input, k->rsa->q);
424:
425: /* Generate additional parameters */
426: generate_additional_parameters(k->rsa);
427: break;
428: default:
1.33 markus 429: buffer_clear(&e->input);
430: goto send;
1.21 markus 431: }
1.33 markus 432: break;
433: }
434: comment = buffer_get_string(&e->input, NULL);
435: if (k == NULL) {
436: xfree(comment);
437: goto send;
438: }
439: success = 1;
440: if (lookup_private_key(k, NULL, version) == NULL) {
441: if (tab->nentries == 0)
442: tab->identities = xmalloc(sizeof(Identity));
443: else
444: tab->identities = xrealloc(tab->identities,
445: (tab->nentries + 1) * sizeof(Identity));
446: tab->identities[tab->nentries].key = k;
447: tab->identities[tab->nentries].comment = comment;
448: /* Increment the number of identities. */
449: tab->nentries++;
450: } else {
451: key_free(k);
452: xfree(comment);
453: }
454: send:
1.1 deraadt 455: buffer_put_int(&e->output, 1);
1.33 markus 456: buffer_put_char(&e->output,
457: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1 deraadt 458: }
459:
1.33 markus 460: /* dispatch incoming messages */
461:
1.2 provos 462: void
463: process_message(SocketEntry *e)
1.1 deraadt 464: {
1.45 markus 465: u_int msg_len;
466: u_int type;
467: u_char *cp;
1.21 markus 468: if (buffer_len(&e->input) < 5)
469: return; /* Incomplete message. */
1.45 markus 470: cp = (u_char *) buffer_ptr(&e->input);
1.21 markus 471: msg_len = GET_32BIT(cp);
472: if (msg_len > 256 * 1024) {
473: shutdown(e->fd, SHUT_RDWR);
474: close(e->fd);
475: e->type = AUTH_UNUSED;
476: return;
477: }
478: if (buffer_len(&e->input) < msg_len + 4)
479: return;
480: buffer_consume(&e->input, 4);
481: type = buffer_get_char(&e->input);
482:
483: switch (type) {
1.33 markus 484: /* ssh1 */
485: case SSH_AGENTC_RSA_CHALLENGE:
486: process_authentication_challenge1(e);
487: break;
1.21 markus 488: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33 markus 489: process_request_identities(e, 1);
1.21 markus 490: break;
491: case SSH_AGENTC_ADD_RSA_IDENTITY:
1.33 markus 492: process_add_identity(e, 1);
1.21 markus 493: break;
494: case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33 markus 495: process_remove_identity(e, 1);
1.21 markus 496: break;
497: case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.33 markus 498: process_remove_all_identities(e, 1);
499: break;
500: /* ssh2 */
501: case SSH2_AGENTC_SIGN_REQUEST:
502: process_sign_request2(e);
503: break;
504: case SSH2_AGENTC_REQUEST_IDENTITIES:
505: process_request_identities(e, 2);
506: break;
507: case SSH2_AGENTC_ADD_IDENTITY:
508: process_add_identity(e, 2);
509: break;
510: case SSH2_AGENTC_REMOVE_IDENTITY:
511: process_remove_identity(e, 2);
512: break;
513: case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
514: process_remove_all_identities(e, 2);
1.21 markus 515: break;
516: default:
517: /* Unknown message. Respond with failure. */
518: error("Unknown message %d", type);
519: buffer_clear(&e->input);
520: buffer_put_int(&e->output, 1);
521: buffer_put_char(&e->output, SSH_AGENT_FAILURE);
522: break;
523: }
1.1 deraadt 524: }
525:
1.2 provos 526: void
527: new_socket(int type, int fd)
1.1 deraadt 528: {
1.45 markus 529: u_int i, old_alloc;
1.21 markus 530: if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
531: error("fcntl O_NONBLOCK: %s", strerror(errno));
532:
533: if (fd > max_fd)
534: max_fd = fd;
535:
536: for (i = 0; i < sockets_alloc; i++)
537: if (sockets[i].type == AUTH_UNUSED) {
538: sockets[i].fd = fd;
539: sockets[i].type = type;
540: buffer_init(&sockets[i].input);
541: buffer_init(&sockets[i].output);
542: return;
543: }
544: old_alloc = sockets_alloc;
545: sockets_alloc += 10;
546: if (sockets)
547: sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
548: else
549: sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
550: for (i = old_alloc; i < sockets_alloc; i++)
551: sockets[i].type = AUTH_UNUSED;
552: sockets[old_alloc].type = type;
553: sockets[old_alloc].fd = fd;
554: buffer_init(&sockets[old_alloc].input);
555: buffer_init(&sockets[old_alloc].output);
1.1 deraadt 556: }
557:
1.46 markus 558: int
559: prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl)
1.1 deraadt 560: {
1.46 markus 561: u_int i, sz;
562: int n = 0;
563:
564: for (i = 0; i < sockets_alloc; i++) {
1.21 markus 565: switch (sockets[i].type) {
566: case AUTH_SOCKET:
567: case AUTH_CONNECTION:
1.46 markus 568: n = MAX(n, sockets[i].fd);
1.21 markus 569: break;
570: case AUTH_UNUSED:
571: break;
572: default:
573: fatal("Unknown socket type %d", sockets[i].type);
574: break;
575: }
1.46 markus 576: }
577:
578: sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
579: if (*fdrp == NULL || n > *fdl) {
580: if (*fdrp)
581: free(*fdrp);
582: if (*fdwp)
583: free(*fdwp);
584: *fdrp = xmalloc(sz);
585: *fdwp = xmalloc(sz);
586: *fdl = n;
587: }
588: memset(*fdrp, 0, sz);
589: memset(*fdwp, 0, sz);
590:
591: for (i = 0; i < sockets_alloc; i++) {
592: switch (sockets[i].type) {
593: case AUTH_SOCKET:
594: case AUTH_CONNECTION:
595: FD_SET(sockets[i].fd, *fdrp);
596: if (buffer_len(&sockets[i].output) > 0)
597: FD_SET(sockets[i].fd, *fdwp);
598: break;
599: default:
600: break;
601: }
602: }
603: return (1);
1.21 markus 604: }
605:
1.28 markus 606: void
1.21 markus 607: after_select(fd_set *readset, fd_set *writeset)
608: {
1.45 markus 609: u_int i;
1.21 markus 610: int len, sock;
1.26 markus 611: socklen_t slen;
1.21 markus 612: char buf[1024];
613: struct sockaddr_un sunaddr;
614:
615: for (i = 0; i < sockets_alloc; i++)
616: switch (sockets[i].type) {
617: case AUTH_UNUSED:
618: break;
619: case AUTH_SOCKET:
620: if (FD_ISSET(sockets[i].fd, readset)) {
1.26 markus 621: slen = sizeof(sunaddr);
1.46 markus 622: sock = accept(sockets[i].fd,
623: (struct sockaddr *) &sunaddr, &slen);
1.21 markus 624: if (sock < 0) {
625: perror("accept from AUTH_SOCKET");
626: break;
627: }
628: new_socket(AUTH_CONNECTION, sock);
629: }
630: break;
631: case AUTH_CONNECTION:
632: if (buffer_len(&sockets[i].output) > 0 &&
633: FD_ISSET(sockets[i].fd, writeset)) {
1.52 ! deraadt 634: do {
! 635: len = write(sockets[i].fd,
! 636: buffer_ptr(&sockets[i].output),
! 637: buffer_len(&sockets[i].output));
! 638: if (len == -1 && (errno == EAGAIN ||
! 639: errno == EINTR))
! 640: continue;
! 641: break;
! 642: } while (1);
1.21 markus 643: if (len <= 0) {
644: shutdown(sockets[i].fd, SHUT_RDWR);
645: close(sockets[i].fd);
646: sockets[i].type = AUTH_UNUSED;
1.30 djm 647: buffer_free(&sockets[i].input);
648: buffer_free(&sockets[i].output);
1.21 markus 649: break;
650: }
651: buffer_consume(&sockets[i].output, len);
652: }
653: if (FD_ISSET(sockets[i].fd, readset)) {
1.52 ! deraadt 654: do {
! 655: len = read(sockets[i].fd, buf, sizeof(buf));
! 656: if (len == -1 && (errno == EAGAIN ||
! 657: errno == EINTR))
! 658: continue;
! 659: break;
! 660: } while (1);
1.21 markus 661: if (len <= 0) {
662: shutdown(sockets[i].fd, SHUT_RDWR);
663: close(sockets[i].fd);
664: sockets[i].type = AUTH_UNUSED;
1.30 djm 665: buffer_free(&sockets[i].input);
666: buffer_free(&sockets[i].output);
1.21 markus 667: break;
668: }
669: buffer_append(&sockets[i].input, buf, len);
670: process_message(&sockets[i]);
671: }
672: break;
673: default:
674: fatal("Unknown type %d", sockets[i].type);
675: }
1.1 deraadt 676: }
677:
1.6 deraadt 678: void
1.2 provos 679: check_parent_exists(int sig)
1.1 deraadt 680: {
1.46 markus 681: int save_errno = errno;
682:
1.29 deraadt 683: if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
1.21 markus 684: /* printf("Parent has died - Authentication agent exiting.\n"); */
685: exit(1);
686: }
687: signal(SIGALRM, check_parent_exists);
688: alarm(10);
1.46 markus 689: errno = save_errno;
1.1 deraadt 690: }
691:
1.15 markus 692: void
693: cleanup_socket(void)
694: {
1.48 deraadt 695: if (socket_name[0])
696: unlink(socket_name);
697: if (socket_dir[0])
698: rmdir(socket_dir);
1.10 markus 699: }
700:
1.15 markus 701: void
702: cleanup_exit(int i)
703: {
1.21 markus 704: cleanup_socket();
705: exit(i);
1.15 markus 706: }
707:
708: void
1.48 deraadt 709: cleanup_handler(int sig)
710: {
711: cleanup_socket();
712: _exit(2);
713: }
714:
715: void
1.50 itojun 716: usage(void)
1.15 markus 717: {
1.21 markus 718: fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
719: fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
1.46 markus 720: __progname);
1.21 markus 721: exit(1);
1.15 markus 722: }
723:
1.2 provos 724: int
725: main(int ac, char **av)
1.1 deraadt 726: {
1.21 markus 727: int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
728: struct sockaddr_un sunaddr;
1.41 markus 729: struct rlimit rlim;
1.21 markus 730: pid_t pid;
731: char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
1.43 markus 732: extern int optind;
1.46 markus 733: fd_set *readsetp = NULL, *writesetp = NULL;
1.21 markus 734:
735: while ((ch = getopt(ac, av, "cks")) != -1) {
736: switch (ch) {
737: case 'c':
738: if (s_flag)
739: usage();
740: c_flag++;
741: break;
742: case 'k':
743: k_flag++;
744: break;
745: case 's':
746: if (c_flag)
747: usage();
748: s_flag++;
749: break;
750: default:
751: usage();
752: }
753: }
754: ac -= optind;
755: av += optind;
756:
757: if (ac > 0 && (c_flag || k_flag || s_flag))
758: usage();
759:
760: if (ac == 0 && !c_flag && !k_flag && !s_flag) {
761: shell = getenv("SHELL");
762: if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
763: c_flag = 1;
764: }
765: if (k_flag) {
766: pidstr = getenv(SSH_AGENTPID_ENV_NAME);
767: if (pidstr == NULL) {
768: fprintf(stderr, "%s not set, cannot kill agent\n",
1.46 markus 769: SSH_AGENTPID_ENV_NAME);
1.21 markus 770: exit(1);
771: }
772: pid = atoi(pidstr);
1.46 markus 773: if (pid < 1) {
1.21 markus 774: fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1.46 markus 775: SSH_AGENTPID_ENV_NAME, pidstr);
1.21 markus 776: exit(1);
777: }
778: if (kill(pid, SIGTERM) == -1) {
779: perror("kill");
780: exit(1);
781: }
782: format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
783: printf(format, SSH_AUTHSOCKET_ENV_NAME);
784: printf(format, SSH_AGENTPID_ENV_NAME);
785: printf("echo Agent pid %d killed;\n", pid);
786: exit(0);
787: }
788: parent_pid = getpid();
789:
790: /* Create private directory for agent socket */
791: strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
792: if (mkdtemp(socket_dir) == NULL) {
793: perror("mkdtemp: private socket dir");
794: exit(1);
795: }
796: snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
1.46 markus 797: parent_pid);
1.21 markus 798:
1.23 markus 799: /*
800: * Create socket early so it will exist before command gets run from
801: * the parent.
802: */
1.21 markus 803: sock = socket(AF_UNIX, SOCK_STREAM, 0);
804: if (sock < 0) {
805: perror("socket");
806: cleanup_exit(1);
807: }
808: memset(&sunaddr, 0, sizeof(sunaddr));
809: sunaddr.sun_family = AF_UNIX;
810: strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
811: if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
812: perror("bind");
813: cleanup_exit(1);
814: }
815: if (listen(sock, 5) < 0) {
816: perror("listen");
817: cleanup_exit(1);
818: }
1.46 markus 819:
1.23 markus 820: /*
821: * Fork, and have the parent execute the command, if any, or present
822: * the socket data. The child continues as the authentication agent.
823: */
1.21 markus 824: pid = fork();
825: if (pid == -1) {
826: perror("fork");
827: exit(1);
828: }
829: if (pid != 0) { /* Parent - execute the given command. */
830: close(sock);
831: snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
832: if (ac == 0) {
833: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
834: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1.46 markus 835: SSH_AUTHSOCKET_ENV_NAME);
1.21 markus 836: printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1.46 markus 837: SSH_AGENTPID_ENV_NAME);
1.21 markus 838: printf("echo Agent pid %d;\n", pid);
839: exit(0);
840: }
1.36 deraadt 841: if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
842: setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
843: perror("setenv");
844: exit(1);
845: }
1.21 markus 846: execvp(av[0], av);
847: perror(av[0]);
848: exit(1);
849: }
850: close(0);
851: close(1);
852: close(2);
853:
1.41 markus 854: /* deny core dumps, since memory contains unencrypted private keys */
855: rlim.rlim_cur = rlim.rlim_max = 0;
856: if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
857: perror("setrlimit rlimit_core failed");
858: cleanup_exit(1);
859: }
1.21 markus 860: if (setsid() == -1) {
861: perror("setsid");
862: cleanup_exit(1);
863: }
864: if (atexit(cleanup_socket) < 0) {
865: perror("atexit");
866: cleanup_exit(1);
867: }
868: new_socket(AUTH_SOCKET, sock);
869: if (ac > 0) {
870: signal(SIGALRM, check_parent_exists);
871: alarm(10);
872: }
1.33 markus 873: idtab_init();
1.21 markus 874: signal(SIGINT, SIG_IGN);
875: signal(SIGPIPE, SIG_IGN);
1.48 deraadt 876: signal(SIGHUP, cleanup_handler);
877: signal(SIGTERM, cleanup_handler);
1.21 markus 878: while (1) {
1.46 markus 879: prepare_select(&readsetp, &writesetp, &max_fd);
880: if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1.21 markus 881: if (errno == EINTR)
882: continue;
883: exit(1);
884: }
1.46 markus 885: after_select(readsetp, writesetp);
1.15 markus 886: }
1.21 markus 887: /* NOTREACHED */
1.1 deraadt 888: }