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