Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.57
1.57 ! markus 1: /* $OpenBSD: ssh-agent.c,v 1.56 2001/06/25 08:25:40 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.56 markus 15: * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
1.35 deraadt 16: *
17: * Redistribution and use in source and binary forms, with or without
18: * modification, are permitted provided that the following conditions
19: * are met:
20: * 1. Redistributions of source code must retain the above copyright
21: * notice, this list of conditions and the following disclaimer.
22: * 2. Redistributions in binary form must reproduce the above copyright
23: * notice, this list of conditions and the following disclaimer in the
24: * documentation and/or other materials provided with the distribution.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.22 deraadt 36: */
1.1 deraadt 37:
38: #include "includes.h"
1.57 ! markus 39: RCSID("$OpenBSD: ssh-agent.c,v 1.56 2001/06/25 08:25:40 markus Exp $");
1.47 markus 40:
41: #include <openssl/evp.h>
42: #include <openssl/md5.h>
1.1 deraadt 43:
44: #include "ssh.h"
45: #include "rsa.h"
46: #include "buffer.h"
47: #include "bufaux.h"
48: #include "xmalloc.h"
49: #include "packet.h"
50: #include "getput.h"
51: #include "mpaux.h"
1.32 markus 52: #include "key.h"
53: #include "authfd.h"
1.47 markus 54: #include "cipher.h"
1.33 markus 55: #include "kex.h"
1.37 markus 56: #include "compat.h"
1.47 markus 57: #include "log.h"
1.7 deraadt 58:
1.21 markus 59: typedef struct {
60: int fd;
61: enum {
62: AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
63: } type;
64: Buffer input;
65: Buffer output;
1.1 deraadt 66: } SocketEntry;
67:
1.45 markus 68: u_int sockets_alloc = 0;
1.1 deraadt 69: SocketEntry *sockets = NULL;
70:
1.21 markus 71: typedef struct {
1.33 markus 72: Key *key;
1.21 markus 73: char *comment;
1.1 deraadt 74: } Identity;
75:
1.33 markus 76: typedef struct {
77: int nentries;
78: Identity *identities;
79: } Idtab;
80:
81: /* private key table, one per protocol version */
82: Idtab idtable[3];
1.1 deraadt 83:
84: int max_fd = 0;
85:
1.11 markus 86: /* pid of shell == parent of agent */
1.29 deraadt 87: pid_t parent_pid = -1;
1.10 markus 88:
89: /* pathname and directory for AUTH_SOCKET */
90: char socket_name[1024];
91: char socket_dir[1024];
92:
1.20 markus 93: extern char *__progname;
94:
1.55 itojun 95: static void
1.33 markus 96: idtab_init(void)
1.1 deraadt 97: {
1.33 markus 98: int i;
99: for (i = 0; i <=2; i++){
100: idtable[i].identities = NULL;
101: idtable[i].nentries = 0;
102: }
103: }
104:
105: /* return private key table for requested protocol version */
1.55 itojun 106: static Idtab *
1.33 markus 107: idtab_lookup(int version)
108: {
109: if (version < 1 || version > 2)
110: fatal("internal error, bad protocol version %d", version);
111: return &idtable[version];
112: }
113:
114: /* return matching private key for given public key */
1.55 itojun 115: static Key *
1.33 markus 116: lookup_private_key(Key *key, int *idx, int version)
117: {
118: int i;
119: Idtab *tab = idtab_lookup(version);
120: for (i = 0; i < tab->nentries; i++) {
121: if (key_equal(key, tab->identities[i].key)) {
122: if (idx != NULL)
123: *idx = i;
124: return tab->identities[i].key;
125: }
126: }
127: return NULL;
128: }
129:
130: /* send list of supported public keys to 'client' */
1.55 itojun 131: static void
1.33 markus 132: process_request_identities(SocketEntry *e, int version)
133: {
134: Idtab *tab = idtab_lookup(version);
1.21 markus 135: Buffer msg;
136: int i;
1.1 deraadt 137:
1.21 markus 138: buffer_init(&msg);
1.33 markus 139: buffer_put_char(&msg, (version == 1) ?
140: SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
141: buffer_put_int(&msg, tab->nentries);
142: for (i = 0; i < tab->nentries; i++) {
143: Identity *id = &tab->identities[i];
1.39 markus 144: if (id->key->type == KEY_RSA1) {
1.33 markus 145: buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
146: buffer_put_bignum(&msg, id->key->rsa->e);
147: buffer_put_bignum(&msg, id->key->rsa->n);
148: } else {
1.45 markus 149: u_char *blob;
150: u_int blen;
1.39 markus 151: key_to_blob(id->key, &blob, &blen);
1.33 markus 152: buffer_put_string(&msg, blob, blen);
153: xfree(blob);
154: }
155: buffer_put_cstring(&msg, id->comment);
1.21 markus 156: }
157: buffer_put_int(&e->output, buffer_len(&msg));
158: buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
159: buffer_free(&msg);
1.1 deraadt 160: }
161:
1.33 markus 162: /* ssh1 only */
1.55 itojun 163: static void
1.33 markus 164: process_authentication_challenge1(SocketEntry *e)
1.1 deraadt 165: {
1.33 markus 166: Key *key, *private;
167: BIGNUM *challenge;
168: int i, len;
1.21 markus 169: Buffer msg;
170: MD5_CTX md;
1.45 markus 171: u_char buf[32], mdbuf[16], session_id[16];
172: u_int response_type;
1.21 markus 173:
174: buffer_init(&msg);
1.39 markus 175: key = key_new(KEY_RSA1);
1.21 markus 176: challenge = BN_new();
1.33 markus 177:
178: buffer_get_int(&e->input); /* ignored */
179: buffer_get_bignum(&e->input, key->rsa->e);
180: buffer_get_bignum(&e->input, key->rsa->n);
1.21 markus 181: buffer_get_bignum(&e->input, challenge);
182:
1.33 markus 183: /* Only protocol 1.1 is supported */
184: if (buffer_len(&e->input) == 0)
185: goto failure;
186: buffer_get(&e->input, (char *) session_id, 16);
187: response_type = buffer_get_int(&e->input);
188: if (response_type != 1)
189: goto failure;
190:
191: private = lookup_private_key(key, NULL, 1);
192: if (private != NULL) {
193: /* Decrypt the challenge using the private key. */
1.49 markus 194: if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
195: goto failure;
1.33 markus 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 */
1.55 itojun 229: static void
1.33 markus 230: process_sign_request2(SocketEntry *e)
231: {
232: extern int datafellows;
233: Key *key, *private;
1.45 markus 234: u_char *blob, *data, *signature = NULL;
235: u_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.55 itojun 274: static void
1.33 markus 275: process_remove_identity(SocketEntry *e, int version)
1.1 deraadt 276: {
1.33 markus 277: Key *key = NULL, *private;
1.45 markus 278: u_char *blob;
279: u_int blen;
280: u_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",
1.46 markus 292: key_size(key), bits);
1.33 markus 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.55 itojun 335: static void
1.33 markus 336: process_remove_all_identities(SocketEntry *e, int version)
1.1 deraadt 337: {
1.45 markus 338: u_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.55 itojun 356: static void
1.33 markus 357: process_add_identity(SocketEntry *e, int version)
1.1 deraadt 358: {
1.33 markus 359: Key *k = NULL;
1.39 markus 360: char *type_name;
1.33 markus 361: char *comment;
1.39 markus 362: int type, success = 0;
1.33 markus 363: Idtab *tab = idtab_lookup(version);
364:
365: switch (version) {
366: case 1:
1.39 markus 367: k = key_new_private(KEY_RSA1);
1.46 markus 368: buffer_get_int(&e->input); /* ignored */
1.39 markus 369: buffer_get_bignum(&e->input, k->rsa->n);
370: buffer_get_bignum(&e->input, k->rsa->e);
371: buffer_get_bignum(&e->input, k->rsa->d);
372: buffer_get_bignum(&e->input, k->rsa->iqmp);
1.33 markus 373:
374: /* SSH and SSL have p and q swapped */
1.39 markus 375: buffer_get_bignum(&e->input, k->rsa->q); /* p */
376: buffer_get_bignum(&e->input, k->rsa->p); /* q */
1.33 markus 377:
378: /* Generate additional parameters */
1.39 markus 379: generate_additional_parameters(k->rsa);
1.33 markus 380: break;
381: case 2:
1.39 markus 382: type_name = buffer_get_string(&e->input, NULL);
1.46 markus 383: type = key_type_from_name(type_name);
1.39 markus 384: xfree(type_name);
385: switch(type) {
386: case KEY_DSA:
387: k = key_new_private(type);
388: buffer_get_bignum2(&e->input, k->dsa->p);
389: buffer_get_bignum2(&e->input, k->dsa->q);
390: buffer_get_bignum2(&e->input, k->dsa->g);
391: buffer_get_bignum2(&e->input, k->dsa->pub_key);
392: buffer_get_bignum2(&e->input, k->dsa->priv_key);
393: break;
394: case KEY_RSA:
395: k = key_new_private(type);
396: buffer_get_bignum2(&e->input, k->rsa->n);
397: buffer_get_bignum2(&e->input, k->rsa->e);
398: buffer_get_bignum2(&e->input, k->rsa->d);
399: buffer_get_bignum2(&e->input, k->rsa->iqmp);
400: buffer_get_bignum2(&e->input, k->rsa->p);
401: buffer_get_bignum2(&e->input, k->rsa->q);
402:
403: /* Generate additional parameters */
404: generate_additional_parameters(k->rsa);
405: break;
406: default:
1.33 markus 407: buffer_clear(&e->input);
408: goto send;
1.21 markus 409: }
1.33 markus 410: break;
411: }
412: comment = buffer_get_string(&e->input, NULL);
413: if (k == NULL) {
414: xfree(comment);
415: goto send;
416: }
417: success = 1;
418: if (lookup_private_key(k, NULL, version) == NULL) {
419: if (tab->nentries == 0)
420: tab->identities = xmalloc(sizeof(Identity));
421: else
422: tab->identities = xrealloc(tab->identities,
423: (tab->nentries + 1) * sizeof(Identity));
424: tab->identities[tab->nentries].key = k;
425: tab->identities[tab->nentries].comment = comment;
426: /* Increment the number of identities. */
427: tab->nentries++;
428: } else {
429: key_free(k);
430: xfree(comment);
431: }
432: send:
1.1 deraadt 433: buffer_put_int(&e->output, 1);
1.33 markus 434: buffer_put_char(&e->output,
435: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1 deraadt 436: }
437:
1.33 markus 438: /* dispatch incoming messages */
439:
1.55 itojun 440: static void
1.2 provos 441: process_message(SocketEntry *e)
1.1 deraadt 442: {
1.45 markus 443: u_int msg_len;
444: u_int type;
445: u_char *cp;
1.21 markus 446: if (buffer_len(&e->input) < 5)
447: return; /* Incomplete message. */
1.45 markus 448: cp = (u_char *) buffer_ptr(&e->input);
1.21 markus 449: msg_len = GET_32BIT(cp);
450: if (msg_len > 256 * 1024) {
451: shutdown(e->fd, SHUT_RDWR);
452: close(e->fd);
453: e->type = AUTH_UNUSED;
454: return;
455: }
456: if (buffer_len(&e->input) < msg_len + 4)
457: return;
458: buffer_consume(&e->input, 4);
459: type = buffer_get_char(&e->input);
460:
461: switch (type) {
1.33 markus 462: /* ssh1 */
463: case SSH_AGENTC_RSA_CHALLENGE:
464: process_authentication_challenge1(e);
465: break;
1.21 markus 466: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33 markus 467: process_request_identities(e, 1);
1.21 markus 468: break;
469: case SSH_AGENTC_ADD_RSA_IDENTITY:
1.33 markus 470: process_add_identity(e, 1);
1.21 markus 471: break;
472: case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33 markus 473: process_remove_identity(e, 1);
1.21 markus 474: break;
475: case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.33 markus 476: process_remove_all_identities(e, 1);
477: break;
478: /* ssh2 */
479: case SSH2_AGENTC_SIGN_REQUEST:
480: process_sign_request2(e);
481: break;
482: case SSH2_AGENTC_REQUEST_IDENTITIES:
483: process_request_identities(e, 2);
484: break;
485: case SSH2_AGENTC_ADD_IDENTITY:
486: process_add_identity(e, 2);
487: break;
488: case SSH2_AGENTC_REMOVE_IDENTITY:
489: process_remove_identity(e, 2);
490: break;
491: case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
492: process_remove_all_identities(e, 2);
1.21 markus 493: break;
494: default:
495: /* Unknown message. Respond with failure. */
496: error("Unknown message %d", type);
497: buffer_clear(&e->input);
498: buffer_put_int(&e->output, 1);
499: buffer_put_char(&e->output, SSH_AGENT_FAILURE);
500: break;
501: }
1.1 deraadt 502: }
503:
1.55 itojun 504: static void
1.2 provos 505: new_socket(int type, int fd)
1.1 deraadt 506: {
1.45 markus 507: u_int i, old_alloc;
1.21 markus 508: if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
509: error("fcntl O_NONBLOCK: %s", strerror(errno));
510:
511: if (fd > max_fd)
512: max_fd = fd;
513:
514: for (i = 0; i < sockets_alloc; i++)
515: if (sockets[i].type == AUTH_UNUSED) {
516: sockets[i].fd = fd;
517: sockets[i].type = type;
518: buffer_init(&sockets[i].input);
519: buffer_init(&sockets[i].output);
520: return;
521: }
522: old_alloc = sockets_alloc;
523: sockets_alloc += 10;
524: if (sockets)
525: sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
526: else
527: sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
528: for (i = old_alloc; i < sockets_alloc; i++)
529: sockets[i].type = AUTH_UNUSED;
530: sockets[old_alloc].type = type;
531: sockets[old_alloc].fd = fd;
532: buffer_init(&sockets[old_alloc].input);
533: buffer_init(&sockets[old_alloc].output);
1.1 deraadt 534: }
535:
1.55 itojun 536: static int
1.46 markus 537: prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl)
1.1 deraadt 538: {
1.46 markus 539: u_int i, sz;
540: int n = 0;
541:
542: for (i = 0; i < sockets_alloc; i++) {
1.21 markus 543: switch (sockets[i].type) {
544: case AUTH_SOCKET:
545: case AUTH_CONNECTION:
1.46 markus 546: n = MAX(n, sockets[i].fd);
1.21 markus 547: break;
548: case AUTH_UNUSED:
549: break;
550: default:
551: fatal("Unknown socket type %d", sockets[i].type);
552: break;
553: }
1.46 markus 554: }
555:
556: sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
557: if (*fdrp == NULL || n > *fdl) {
558: if (*fdrp)
1.54 stevesk 559: xfree(*fdrp);
1.46 markus 560: if (*fdwp)
1.54 stevesk 561: xfree(*fdwp);
1.46 markus 562: *fdrp = xmalloc(sz);
563: *fdwp = xmalloc(sz);
564: *fdl = n;
565: }
566: memset(*fdrp, 0, sz);
567: memset(*fdwp, 0, sz);
568:
569: for (i = 0; i < sockets_alloc; i++) {
570: switch (sockets[i].type) {
571: case AUTH_SOCKET:
572: case AUTH_CONNECTION:
573: FD_SET(sockets[i].fd, *fdrp);
574: if (buffer_len(&sockets[i].output) > 0)
575: FD_SET(sockets[i].fd, *fdwp);
576: break;
577: default:
578: break;
579: }
580: }
581: return (1);
1.21 markus 582: }
583:
1.55 itojun 584: static void
1.21 markus 585: after_select(fd_set *readset, fd_set *writeset)
586: {
1.45 markus 587: u_int i;
1.21 markus 588: int len, sock;
1.26 markus 589: socklen_t slen;
1.21 markus 590: char buf[1024];
591: struct sockaddr_un sunaddr;
592:
593: for (i = 0; i < sockets_alloc; i++)
594: switch (sockets[i].type) {
595: case AUTH_UNUSED:
596: break;
597: case AUTH_SOCKET:
598: if (FD_ISSET(sockets[i].fd, readset)) {
1.26 markus 599: slen = sizeof(sunaddr);
1.46 markus 600: sock = accept(sockets[i].fd,
601: (struct sockaddr *) &sunaddr, &slen);
1.21 markus 602: if (sock < 0) {
603: perror("accept from AUTH_SOCKET");
604: break;
605: }
606: new_socket(AUTH_CONNECTION, sock);
607: }
608: break;
609: case AUTH_CONNECTION:
610: if (buffer_len(&sockets[i].output) > 0 &&
611: FD_ISSET(sockets[i].fd, writeset)) {
1.52 deraadt 612: do {
613: len = write(sockets[i].fd,
614: buffer_ptr(&sockets[i].output),
615: buffer_len(&sockets[i].output));
616: if (len == -1 && (errno == EAGAIN ||
617: errno == EINTR))
618: continue;
619: break;
620: } while (1);
1.21 markus 621: if (len <= 0) {
622: shutdown(sockets[i].fd, SHUT_RDWR);
623: close(sockets[i].fd);
624: sockets[i].type = AUTH_UNUSED;
1.30 djm 625: buffer_free(&sockets[i].input);
626: buffer_free(&sockets[i].output);
1.21 markus 627: break;
628: }
629: buffer_consume(&sockets[i].output, len);
630: }
631: if (FD_ISSET(sockets[i].fd, readset)) {
1.52 deraadt 632: do {
633: len = read(sockets[i].fd, buf, sizeof(buf));
634: if (len == -1 && (errno == EAGAIN ||
635: errno == EINTR))
636: continue;
637: break;
638: } while (1);
1.21 markus 639: if (len <= 0) {
640: shutdown(sockets[i].fd, SHUT_RDWR);
641: close(sockets[i].fd);
642: sockets[i].type = AUTH_UNUSED;
1.30 djm 643: buffer_free(&sockets[i].input);
644: buffer_free(&sockets[i].output);
1.21 markus 645: break;
646: }
647: buffer_append(&sockets[i].input, buf, len);
648: process_message(&sockets[i]);
649: }
650: break;
651: default:
652: fatal("Unknown type %d", sockets[i].type);
653: }
1.1 deraadt 654: }
655:
1.55 itojun 656: static void
1.2 provos 657: check_parent_exists(int sig)
1.1 deraadt 658: {
1.46 markus 659: int save_errno = errno;
660:
1.29 deraadt 661: if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
1.21 markus 662: /* printf("Parent has died - Authentication agent exiting.\n"); */
663: exit(1);
664: }
665: signal(SIGALRM, check_parent_exists);
666: alarm(10);
1.46 markus 667: errno = save_errno;
1.1 deraadt 668: }
669:
1.55 itojun 670: static void
1.15 markus 671: cleanup_socket(void)
672: {
1.48 deraadt 673: if (socket_name[0])
674: unlink(socket_name);
675: if (socket_dir[0])
676: rmdir(socket_dir);
1.10 markus 677: }
678:
1.55 itojun 679: static void
1.15 markus 680: cleanup_exit(int i)
681: {
1.21 markus 682: cleanup_socket();
683: exit(i);
1.15 markus 684: }
685:
1.55 itojun 686: static void
1.48 deraadt 687: cleanup_handler(int sig)
688: {
689: cleanup_socket();
690: _exit(2);
691: }
692:
1.55 itojun 693: static void
1.50 itojun 694: usage(void)
1.15 markus 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.57 ! markus 705: int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch;
1.21 markus 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.53 markus 712:
713: SSLeay_add_all_algorithms();
1.21 markus 714:
1.57 ! markus 715: while ((ch = getopt(ac, av, "cdks")) != -1) {
1.21 markus 716: switch (ch) {
717: case 'c':
718: if (s_flag)
719: usage();
720: c_flag++;
721: break;
722: case 'k':
723: k_flag++;
724: break;
725: case 's':
726: if (c_flag)
727: usage();
728: s_flag++;
729: break;
1.57 ! markus 730: case 'd':
! 731: if (d_flag)
! 732: usage();
! 733: d_flag++;
! 734: break;
1.21 markus 735: default:
736: usage();
737: }
738: }
739: ac -= optind;
740: av += optind;
741:
1.57 ! markus 742: if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1.21 markus 743: usage();
744:
1.57 ! markus 745: if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) {
1.21 markus 746: shell = getenv("SHELL");
747: if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
748: c_flag = 1;
749: }
750: if (k_flag) {
751: pidstr = getenv(SSH_AGENTPID_ENV_NAME);
752: if (pidstr == NULL) {
753: fprintf(stderr, "%s not set, cannot kill agent\n",
1.46 markus 754: SSH_AGENTPID_ENV_NAME);
1.21 markus 755: exit(1);
756: }
757: pid = atoi(pidstr);
1.46 markus 758: if (pid < 1) {
1.21 markus 759: fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1.46 markus 760: SSH_AGENTPID_ENV_NAME, pidstr);
1.21 markus 761: exit(1);
762: }
763: if (kill(pid, SIGTERM) == -1) {
764: perror("kill");
765: exit(1);
766: }
767: format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
768: printf(format, SSH_AUTHSOCKET_ENV_NAME);
769: printf(format, SSH_AGENTPID_ENV_NAME);
770: printf("echo Agent pid %d killed;\n", pid);
771: exit(0);
772: }
773: parent_pid = getpid();
774:
775: /* Create private directory for agent socket */
776: strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
777: if (mkdtemp(socket_dir) == NULL) {
778: perror("mkdtemp: private socket dir");
779: exit(1);
780: }
781: snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
1.46 markus 782: parent_pid);
1.21 markus 783:
1.23 markus 784: /*
785: * Create socket early so it will exist before command gets run from
786: * the parent.
787: */
1.21 markus 788: sock = socket(AF_UNIX, SOCK_STREAM, 0);
789: if (sock < 0) {
790: perror("socket");
791: cleanup_exit(1);
792: }
793: memset(&sunaddr, 0, sizeof(sunaddr));
794: sunaddr.sun_family = AF_UNIX;
795: strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
796: if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
797: perror("bind");
798: cleanup_exit(1);
799: }
800: if (listen(sock, 5) < 0) {
801: perror("listen");
802: cleanup_exit(1);
803: }
1.46 markus 804:
1.23 markus 805: /*
806: * Fork, and have the parent execute the command, if any, or present
807: * the socket data. The child continues as the authentication agent.
808: */
1.57 ! markus 809: if (d_flag) {
! 810: log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
! 811: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
! 812: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
! 813: SSH_AUTHSOCKET_ENV_NAME);
! 814: printf("echo Agent pid %d;\n", parent_pid);
! 815: goto skip;
! 816: }
1.21 markus 817: pid = fork();
818: if (pid == -1) {
819: perror("fork");
820: exit(1);
821: }
822: if (pid != 0) { /* Parent - execute the given command. */
823: close(sock);
824: snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
825: if (ac == 0) {
826: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
827: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1.46 markus 828: SSH_AUTHSOCKET_ENV_NAME);
1.21 markus 829: printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1.46 markus 830: SSH_AGENTPID_ENV_NAME);
1.21 markus 831: printf("echo Agent pid %d;\n", pid);
832: exit(0);
833: }
1.36 deraadt 834: if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
835: setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
836: perror("setenv");
837: exit(1);
838: }
1.21 markus 839: execvp(av[0], av);
840: perror(av[0]);
841: exit(1);
842: }
843: close(0);
844: close(1);
845: close(2);
846:
1.41 markus 847: /* deny core dumps, since memory contains unencrypted private keys */
848: rlim.rlim_cur = rlim.rlim_max = 0;
849: if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
850: perror("setrlimit rlimit_core failed");
851: cleanup_exit(1);
852: }
1.21 markus 853: if (setsid() == -1) {
854: perror("setsid");
855: cleanup_exit(1);
856: }
1.57 ! markus 857:
! 858: skip:
1.21 markus 859: if (atexit(cleanup_socket) < 0) {
860: perror("atexit");
861: cleanup_exit(1);
862: }
863: new_socket(AUTH_SOCKET, sock);
864: if (ac > 0) {
865: signal(SIGALRM, check_parent_exists);
866: alarm(10);
867: }
1.33 markus 868: idtab_init();
1.57 ! markus 869: if (!d_flag) {
! 870: signal(SIGINT, SIG_IGN);
! 871: signal(SIGPIPE, SIG_IGN);
! 872: }
1.48 deraadt 873: signal(SIGHUP, cleanup_handler);
874: signal(SIGTERM, cleanup_handler);
1.21 markus 875: while (1) {
1.46 markus 876: prepare_select(&readsetp, &writesetp, &max_fd);
877: if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1.21 markus 878: if (errno == EINTR)
879: continue;
880: exit(1);
881: }
1.46 markus 882: after_select(readsetp, writesetp);
1.15 markus 883: }
1.21 markus 884: /* NOTREACHED */
1.1 deraadt 885: }