Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.94
1.1 deraadt 1: /*
1.22 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
5: * The authentication agent program.
1.33 markus 6: *
1.35 deraadt 7: * As far as I am concerned, the code I have written for this software
8: * can be used freely for any purpose. Any derived versions of this
9: * software must be clearly marked as such, and if the derived work is
10: * incompatible with the protocol description in the RFC file, it must be
11: * called by a name other than "ssh" or "Secure Shell".
12: *
1.56 markus 13: * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
1.35 deraadt 14: *
15: * Redistribution and use in source and binary forms, with or without
16: * modification, are permitted provided that the following conditions
17: * are met:
18: * 1. Redistributions of source code must retain the above copyright
19: * notice, this list of conditions and the following disclaimer.
20: * 2. Redistributions in binary form must reproduce the above copyright
21: * notice, this list of conditions and the following disclaimer in the
22: * documentation and/or other materials provided with the distribution.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.22 deraadt 34: */
1.1 deraadt 35:
36: #include "includes.h"
1.78 provos 37: #include <sys/queue.h>
1.94 ! markus 38: RCSID("$OpenBSD: ssh-agent.c,v 1.93 2002/06/15 00:07:38 markus Exp $");
1.47 markus 39:
40: #include <openssl/evp.h>
41: #include <openssl/md5.h>
1.1 deraadt 42:
43: #include "ssh.h"
44: #include "rsa.h"
45: #include "buffer.h"
46: #include "bufaux.h"
47: #include "xmalloc.h"
48: #include "getput.h"
1.32 markus 49: #include "key.h"
50: #include "authfd.h"
1.37 markus 51: #include "compat.h"
1.47 markus 52: #include "log.h"
1.7 deraadt 53:
1.59 markus 54: #ifdef SMARTCARD
55: #include "scard.h"
1.71 jakob 56: #endif
1.59 markus 57:
1.73 stevesk 58: typedef enum {
59: AUTH_UNUSED,
60: AUTH_SOCKET,
61: AUTH_CONNECTION
62: } sock_type;
63:
1.21 markus 64: typedef struct {
65: int fd;
1.73 stevesk 66: sock_type type;
1.21 markus 67: Buffer input;
68: Buffer output;
1.87 markus 69: Buffer request;
1.1 deraadt 70: } SocketEntry;
71:
1.45 markus 72: u_int sockets_alloc = 0;
1.1 deraadt 73: SocketEntry *sockets = NULL;
74:
1.78 provos 75: typedef struct identity {
76: TAILQ_ENTRY(identity) next;
1.33 markus 77: Key *key;
1.21 markus 78: char *comment;
1.89 markus 79: u_int death;
1.1 deraadt 80: } Identity;
81:
1.33 markus 82: typedef struct {
83: int nentries;
1.78 provos 84: TAILQ_HEAD(idqueue, identity) idlist;
1.33 markus 85: } Idtab;
86:
87: /* private key table, one per protocol version */
88: Idtab idtable[3];
1.1 deraadt 89:
90: int max_fd = 0;
91:
1.11 markus 92: /* pid of shell == parent of agent */
1.29 deraadt 93: pid_t parent_pid = -1;
1.10 markus 94:
95: /* pathname and directory for AUTH_SOCKET */
96: char socket_name[1024];
97: char socket_dir[1024];
98:
1.88 markus 99: /* locking */
100: int locked = 0;
101: char *lock_passwd = NULL;
102:
1.20 markus 103: extern char *__progname;
104:
1.55 itojun 105: static void
1.33 markus 106: idtab_init(void)
1.1 deraadt 107: {
1.33 markus 108: int i;
1.74 deraadt 109: for (i = 0; i <=2; i++) {
1.78 provos 110: TAILQ_INIT(&idtable[i].idlist);
1.33 markus 111: idtable[i].nentries = 0;
112: }
113: }
114:
115: /* return private key table for requested protocol version */
1.55 itojun 116: static Idtab *
1.33 markus 117: idtab_lookup(int version)
118: {
119: if (version < 1 || version > 2)
120: fatal("internal error, bad protocol version %d", version);
121: return &idtable[version];
122: }
123:
1.89 markus 124: static void
125: free_identity(Identity *id)
126: {
127: key_free(id->key);
128: xfree(id->comment);
129: xfree(id);
130: }
131:
1.33 markus 132: /* return matching private key for given public key */
1.78 provos 133: static Identity *
134: lookup_identity(Key *key, int version)
1.33 markus 135: {
1.78 provos 136: Identity *id;
137:
1.33 markus 138: Idtab *tab = idtab_lookup(version);
1.78 provos 139: TAILQ_FOREACH(id, &tab->idlist, next) {
140: if (key_equal(key, id->key))
141: return (id);
1.33 markus 142: }
1.78 provos 143: return (NULL);
144: }
145:
1.33 markus 146: /* send list of supported public keys to 'client' */
1.55 itojun 147: static void
1.33 markus 148: process_request_identities(SocketEntry *e, int version)
149: {
150: Idtab *tab = idtab_lookup(version);
1.21 markus 151: Buffer msg;
1.78 provos 152: Identity *id;
1.1 deraadt 153:
1.21 markus 154: buffer_init(&msg);
1.33 markus 155: buffer_put_char(&msg, (version == 1) ?
156: SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
157: buffer_put_int(&msg, tab->nentries);
1.78 provos 158: TAILQ_FOREACH(id, &tab->idlist, next) {
1.39 markus 159: if (id->key->type == KEY_RSA1) {
1.33 markus 160: buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
161: buffer_put_bignum(&msg, id->key->rsa->e);
162: buffer_put_bignum(&msg, id->key->rsa->n);
163: } else {
1.45 markus 164: u_char *blob;
165: u_int blen;
1.39 markus 166: key_to_blob(id->key, &blob, &blen);
1.33 markus 167: buffer_put_string(&msg, blob, blen);
168: xfree(blob);
169: }
170: buffer_put_cstring(&msg, id->comment);
1.21 markus 171: }
172: buffer_put_int(&e->output, buffer_len(&msg));
173: buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
174: buffer_free(&msg);
1.1 deraadt 175: }
176:
1.33 markus 177: /* ssh1 only */
1.55 itojun 178: static void
1.33 markus 179: process_authentication_challenge1(SocketEntry *e)
1.1 deraadt 180: {
1.78 provos 181: Identity *id;
182: Key *key;
1.33 markus 183: BIGNUM *challenge;
184: int i, len;
1.21 markus 185: Buffer msg;
186: MD5_CTX md;
1.45 markus 187: u_char buf[32], mdbuf[16], session_id[16];
188: u_int response_type;
1.21 markus 189:
190: buffer_init(&msg);
1.39 markus 191: key = key_new(KEY_RSA1);
1.76 markus 192: if ((challenge = BN_new()) == NULL)
193: fatal("process_authentication_challenge1: BN_new failed");
1.33 markus 194:
1.87 markus 195: buffer_get_int(&e->request); /* ignored */
196: buffer_get_bignum(&e->request, key->rsa->e);
197: buffer_get_bignum(&e->request, key->rsa->n);
198: buffer_get_bignum(&e->request, challenge);
1.21 markus 199:
1.33 markus 200: /* Only protocol 1.1 is supported */
1.87 markus 201: if (buffer_len(&e->request) == 0)
1.33 markus 202: goto failure;
1.87 markus 203: buffer_get(&e->request, session_id, 16);
204: response_type = buffer_get_int(&e->request);
1.33 markus 205: if (response_type != 1)
206: goto failure;
207:
1.78 provos 208: id = lookup_identity(key, 1);
209: if (id != NULL) {
210: Key *private = id->key;
1.33 markus 211: /* Decrypt the challenge using the private key. */
1.49 markus 212: if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
213: goto failure;
1.33 markus 214:
215: /* The response is MD5 of decrypted challenge plus session id. */
216: len = BN_num_bytes(challenge);
217: if (len <= 0 || len > 32) {
218: log("process_authentication_challenge: bad challenge length %d", len);
219: goto failure;
220: }
221: memset(buf, 0, 32);
222: BN_bn2bin(challenge, buf + 32 - len);
223: MD5_Init(&md);
224: MD5_Update(&md, buf, 32);
225: MD5_Update(&md, session_id, 16);
226: MD5_Final(mdbuf, &md);
227:
228: /* Send the response. */
229: buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
230: for (i = 0; i < 16; i++)
231: buffer_put_char(&msg, mdbuf[i]);
232: goto send;
233: }
1.21 markus 234:
1.33 markus 235: failure:
236: /* Unknown identity or protocol error. Send failure. */
1.21 markus 237: buffer_put_char(&msg, SSH_AGENT_FAILURE);
238: send:
239: buffer_put_int(&e->output, buffer_len(&msg));
1.33 markus 240: buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
241: key_free(key);
242: BN_clear_free(challenge);
243: buffer_free(&msg);
244: }
245:
246: /* ssh2 only */
1.55 itojun 247: static void
1.33 markus 248: process_sign_request2(SocketEntry *e)
249: {
250: extern int datafellows;
1.78 provos 251: Key *key;
1.45 markus 252: u_char *blob, *data, *signature = NULL;
253: u_int blen, dlen, slen = 0;
1.37 markus 254: int flags;
1.33 markus 255: Buffer msg;
256: int ok = -1;
257:
258: datafellows = 0;
1.43 markus 259:
1.87 markus 260: blob = buffer_get_string(&e->request, &blen);
261: data = buffer_get_string(&e->request, &dlen);
1.37 markus 262:
1.87 markus 263: flags = buffer_get_int(&e->request);
1.37 markus 264: if (flags & SSH_AGENT_OLD_SIGNATURE)
265: datafellows = SSH_BUG_SIGBLOB;
1.33 markus 266:
1.39 markus 267: key = key_from_blob(blob, blen);
1.33 markus 268: if (key != NULL) {
1.78 provos 269: Identity *id = lookup_identity(key, 2);
270: if (id != NULL)
271: ok = key_sign(id->key, &signature, &slen, data, dlen);
1.33 markus 272: }
273: key_free(key);
274: buffer_init(&msg);
275: if (ok == 0) {
276: buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
277: buffer_put_string(&msg, signature, slen);
278: } else {
279: buffer_put_char(&msg, SSH_AGENT_FAILURE);
280: }
281: buffer_put_int(&e->output, buffer_len(&msg));
1.21 markus 282: buffer_append(&e->output, buffer_ptr(&msg),
1.33 markus 283: buffer_len(&msg));
1.21 markus 284: buffer_free(&msg);
1.33 markus 285: xfree(data);
286: xfree(blob);
287: if (signature != NULL)
288: xfree(signature);
1.1 deraadt 289: }
290:
1.33 markus 291: /* shared */
1.55 itojun 292: static void
1.33 markus 293: process_remove_identity(SocketEntry *e, int version)
1.1 deraadt 294: {
1.78 provos 295: Key *key = NULL;
1.45 markus 296: u_char *blob;
297: u_int blen;
298: u_int bits;
1.33 markus 299: int success = 0;
1.21 markus 300:
1.74 deraadt 301: switch (version) {
1.33 markus 302: case 1:
1.39 markus 303: key = key_new(KEY_RSA1);
1.87 markus 304: bits = buffer_get_int(&e->request);
305: buffer_get_bignum(&e->request, key->rsa->e);
306: buffer_get_bignum(&e->request, key->rsa->n);
1.33 markus 307:
308: if (bits != key_size(key))
309: log("Warning: identity keysize mismatch: actual %d, announced %d",
1.46 markus 310: key_size(key), bits);
1.33 markus 311: break;
312: case 2:
1.87 markus 313: blob = buffer_get_string(&e->request, &blen);
1.39 markus 314: key = key_from_blob(blob, blen);
1.33 markus 315: xfree(blob);
316: break;
317: }
318: if (key != NULL) {
1.78 provos 319: Identity *id = lookup_identity(key, version);
320: if (id != NULL) {
1.23 markus 321: /*
322: * We have this key. Free the old key. Since we
323: * don\'t want to leave empty slots in the middle of
1.40 markus 324: * the array, we actually free the key there and move
325: * all the entries between the empty slot and the end
326: * of the array.
1.23 markus 327: */
1.33 markus 328: Idtab *tab = idtab_lookup(version);
1.38 markus 329: if (tab->nentries < 1)
330: fatal("process_remove_identity: "
331: "internal error: tab->nentries %d",
332: tab->nentries);
1.78 provos 333: TAILQ_REMOVE(&tab->idlist, id, next);
334: free_identity(id);
1.33 markus 335: tab->nentries--;
336: success = 1;
1.21 markus 337: }
1.33 markus 338: key_free(key);
339: }
1.1 deraadt 340: buffer_put_int(&e->output, 1);
1.33 markus 341: buffer_put_char(&e->output,
342: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1 deraadt 343: }
344:
1.55 itojun 345: static void
1.33 markus 346: process_remove_all_identities(SocketEntry *e, int version)
1.1 deraadt 347: {
1.33 markus 348: Idtab *tab = idtab_lookup(version);
1.78 provos 349: Identity *id;
1.21 markus 350:
351: /* Loop over all identities and clear the keys. */
1.78 provos 352: for (id = TAILQ_FIRST(&tab->idlist); id;
353: id = TAILQ_FIRST(&tab->idlist)) {
354: TAILQ_REMOVE(&tab->idlist, id, next);
355: free_identity(id);
1.21 markus 356: }
357:
358: /* Mark that there are no identities. */
1.33 markus 359: tab->nentries = 0;
1.21 markus 360:
361: /* Send success. */
362: buffer_put_int(&e->output, 1);
363: buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
364: return;
1.1 deraadt 365: }
366:
1.55 itojun 367: static void
1.89 markus 368: reaper(void)
369: {
370: Idtab *tab;
371: Identity *id, *nxt;
372: int version;
373: u_int now = time(NULL);
374:
375: for (version = 1; version < 3; version++) {
376: tab = idtab_lookup(version);
377: for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
378: nxt = TAILQ_NEXT(id, next);
379: if (id->death != 0 && now >= id->death) {
380: TAILQ_REMOVE(&tab->idlist, id, next);
381: free_identity(id);
382: tab->nentries--;
383: }
384: }
385: }
386: }
387:
388: static void
1.33 markus 389: process_add_identity(SocketEntry *e, int version)
1.1 deraadt 390: {
1.33 markus 391: Key *k = NULL;
1.39 markus 392: char *type_name;
1.33 markus 393: char *comment;
1.94 ! markus 394: int type, success = 0, death = 0;
1.33 markus 395: Idtab *tab = idtab_lookup(version);
396:
397: switch (version) {
398: case 1:
1.39 markus 399: k = key_new_private(KEY_RSA1);
1.87 markus 400: buffer_get_int(&e->request); /* ignored */
401: buffer_get_bignum(&e->request, k->rsa->n);
402: buffer_get_bignum(&e->request, k->rsa->e);
403: buffer_get_bignum(&e->request, k->rsa->d);
404: buffer_get_bignum(&e->request, k->rsa->iqmp);
1.33 markus 405:
406: /* SSH and SSL have p and q swapped */
1.87 markus 407: buffer_get_bignum(&e->request, k->rsa->q); /* p */
408: buffer_get_bignum(&e->request, k->rsa->p); /* q */
1.33 markus 409:
410: /* Generate additional parameters */
1.60 markus 411: rsa_generate_additional_parameters(k->rsa);
1.33 markus 412: break;
413: case 2:
1.87 markus 414: type_name = buffer_get_string(&e->request, NULL);
1.46 markus 415: type = key_type_from_name(type_name);
1.39 markus 416: xfree(type_name);
1.74 deraadt 417: switch (type) {
1.39 markus 418: case KEY_DSA:
419: k = key_new_private(type);
1.87 markus 420: buffer_get_bignum2(&e->request, k->dsa->p);
421: buffer_get_bignum2(&e->request, k->dsa->q);
422: buffer_get_bignum2(&e->request, k->dsa->g);
423: buffer_get_bignum2(&e->request, k->dsa->pub_key);
424: buffer_get_bignum2(&e->request, k->dsa->priv_key);
1.39 markus 425: break;
426: case KEY_RSA:
427: k = key_new_private(type);
1.87 markus 428: buffer_get_bignum2(&e->request, k->rsa->n);
429: buffer_get_bignum2(&e->request, k->rsa->e);
430: buffer_get_bignum2(&e->request, k->rsa->d);
431: buffer_get_bignum2(&e->request, k->rsa->iqmp);
432: buffer_get_bignum2(&e->request, k->rsa->p);
433: buffer_get_bignum2(&e->request, k->rsa->q);
1.39 markus 434:
435: /* Generate additional parameters */
1.60 markus 436: rsa_generate_additional_parameters(k->rsa);
1.39 markus 437: break;
438: default:
1.87 markus 439: buffer_clear(&e->request);
1.33 markus 440: goto send;
1.21 markus 441: }
1.33 markus 442: break;
443: }
1.87 markus 444: comment = buffer_get_string(&e->request, NULL);
1.33 markus 445: if (k == NULL) {
446: xfree(comment);
447: goto send;
448: }
449: success = 1;
1.94 ! markus 450: while (buffer_len(&e->request)) {
! 451: switch (buffer_get_char(&e->request)) {
! 452: case SSH_AGENT_CONSTRAIN_LIFETIME:
! 453: death = time(NULL) + buffer_get_int(&e->request);
! 454: break;
! 455: default:
! 456: break;
! 457: }
! 458: }
1.78 provos 459: if (lookup_identity(k, version) == NULL) {
460: Identity *id = xmalloc(sizeof(Identity));
461: id->key = k;
462: id->comment = comment;
1.94 ! markus 463: id->death = death;
1.78 provos 464: TAILQ_INSERT_TAIL(&tab->idlist, id, next);
1.33 markus 465: /* Increment the number of identities. */
466: tab->nentries++;
467: } else {
468: key_free(k);
469: xfree(comment);
470: }
471: send:
1.1 deraadt 472: buffer_put_int(&e->output, 1);
1.33 markus 473: buffer_put_char(&e->output,
474: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1 deraadt 475: }
476:
1.88 markus 477: /* XXX todo: encrypt sensitive data with passphrase */
478: static void
479: process_lock_agent(SocketEntry *e, int lock)
480: {
481: char *passwd;
482: int success = 0;
483:
484: passwd = buffer_get_string(&e->request, NULL);
485: if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
486: locked = 0;
487: memset(lock_passwd, 0, strlen(lock_passwd));
488: xfree(lock_passwd);
489: lock_passwd = NULL;
490: success = 1;
491: } else if (!locked && lock) {
492: locked = 1;
493: lock_passwd = xstrdup(passwd);
494: success = 1;
495: }
496: memset(passwd, 0, strlen(passwd));
497: xfree(passwd);
498:
499: buffer_put_int(&e->output, 1);
500: buffer_put_char(&e->output,
501: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
502: return;
503: }
504:
505: static void
506: no_identities(SocketEntry *e, u_int type)
507: {
508: Buffer msg;
509:
510: buffer_init(&msg);
511: buffer_put_char(&msg,
512: (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
513: SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
514: buffer_put_int(&msg, 0);
515: buffer_put_int(&e->output, buffer_len(&msg));
516: buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
517: buffer_free(&msg);
518: }
1.59 markus 519:
520: #ifdef SMARTCARD
521: static void
522: process_add_smartcard_key (SocketEntry *e)
523: {
1.84 markus 524: Identity *id;
1.59 markus 525: Idtab *tab;
1.84 markus 526: Key **keys, *k;
1.83 rees 527: char *sc_reader_id = NULL, *pin;
1.84 markus 528: int i, version, success = 0;
1.75 deraadt 529:
1.87 markus 530: sc_reader_id = buffer_get_string(&e->request, NULL);
531: pin = buffer_get_string(&e->request, NULL);
1.84 markus 532: keys = sc_get_keys(sc_reader_id, pin);
1.69 markus 533: xfree(sc_reader_id);
1.83 rees 534: xfree(pin);
1.59 markus 535:
1.84 markus 536: if (keys == NULL || keys[0] == NULL) {
537: error("sc_get_keys failed");
1.59 markus 538: goto send;
539: }
1.84 markus 540: for (i = 0; keys[i] != NULL; i++) {
541: k = keys[i];
542: version = k->type == KEY_RSA1 ? 1 : 2;
543: tab = idtab_lookup(version);
544: if (lookup_identity(k, version) == NULL) {
545: id = xmalloc(sizeof(Identity));
546: id->key = k;
547: id->comment = xstrdup("smartcard key");
1.89 markus 548: id->death = 0;
1.84 markus 549: TAILQ_INSERT_TAIL(&tab->idlist, id, next);
550: tab->nentries++;
551: success = 1;
552: } else {
553: key_free(k);
554: }
555: keys[i] = NULL;
1.59 markus 556: }
1.84 markus 557: xfree(keys);
1.59 markus 558: send:
559: buffer_put_int(&e->output, 1);
560: buffer_put_char(&e->output,
561: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
562: }
563:
564: static void
565: process_remove_smartcard_key(SocketEntry *e)
566: {
1.84 markus 567: Identity *id;
568: Idtab *tab;
569: Key **keys, *k = NULL;
1.83 rees 570: char *sc_reader_id = NULL, *pin;
1.84 markus 571: int i, version, success = 0;
1.59 markus 572:
1.87 markus 573: sc_reader_id = buffer_get_string(&e->request, NULL);
574: pin = buffer_get_string(&e->request, NULL);
1.84 markus 575: keys = sc_get_keys(sc_reader_id, pin);
1.69 markus 576: xfree(sc_reader_id);
1.83 rees 577: xfree(pin);
1.59 markus 578:
1.84 markus 579: if (keys == NULL || keys[0] == NULL) {
580: error("sc_get_keys failed");
581: goto send;
582: }
583: for (i = 0; keys[i] != NULL; i++) {
584: k = keys[i];
585: version = k->type == KEY_RSA1 ? 1 : 2;
586: if ((id = lookup_identity(k, version)) != NULL) {
587: tab = idtab_lookup(version);
1.90 markus 588: TAILQ_REMOVE(&tab->idlist, id, next);
1.59 markus 589: tab->nentries--;
1.78 provos 590: free_identity(id);
1.59 markus 591: success = 1;
592: }
593: key_free(k);
1.84 markus 594: keys[i] = NULL;
1.59 markus 595: }
1.84 markus 596: xfree(keys);
597: send:
1.59 markus 598: buffer_put_int(&e->output, 1);
599: buffer_put_char(&e->output,
600: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
601: }
1.70 jakob 602: #endif /* SMARTCARD */
1.59 markus 603:
1.33 markus 604: /* dispatch incoming messages */
605:
1.55 itojun 606: static void
1.2 provos 607: process_message(SocketEntry *e)
1.1 deraadt 608: {
1.45 markus 609: u_int msg_len;
610: u_int type;
611: u_char *cp;
1.89 markus 612:
613: /* kill dead keys */
614: reaper();
615:
1.21 markus 616: if (buffer_len(&e->input) < 5)
617: return; /* Incomplete message. */
1.77 stevesk 618: cp = buffer_ptr(&e->input);
1.21 markus 619: msg_len = GET_32BIT(cp);
620: if (msg_len > 256 * 1024) {
621: shutdown(e->fd, SHUT_RDWR);
622: close(e->fd);
623: e->type = AUTH_UNUSED;
1.87 markus 624: buffer_free(&e->input);
625: buffer_free(&e->output);
626: buffer_free(&e->request);
1.21 markus 627: return;
628: }
629: if (buffer_len(&e->input) < msg_len + 4)
630: return;
1.87 markus 631:
632: /* move the current input to e->request */
1.21 markus 633: buffer_consume(&e->input, 4);
1.87 markus 634: buffer_clear(&e->request);
635: buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
636: buffer_consume(&e->input, msg_len);
637: type = buffer_get_char(&e->request);
1.21 markus 638:
1.88 markus 639: /* check wheter agent is locked */
640: if (locked && type != SSH_AGENTC_UNLOCK) {
641: buffer_clear(&e->request);
642: switch (type) {
643: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
644: case SSH2_AGENTC_REQUEST_IDENTITIES:
645: /* send empty lists */
646: no_identities(e, type);
647: break;
648: default:
649: /* send a fail message for all other request types */
650: buffer_put_int(&e->output, 1);
651: buffer_put_char(&e->output, SSH_AGENT_FAILURE);
652: }
653: return;
654: }
655:
1.59 markus 656: debug("type %d", type);
1.21 markus 657: switch (type) {
1.88 markus 658: case SSH_AGENTC_LOCK:
659: case SSH_AGENTC_UNLOCK:
660: process_lock_agent(e, type == SSH_AGENTC_LOCK);
661: break;
1.33 markus 662: /* ssh1 */
663: case SSH_AGENTC_RSA_CHALLENGE:
664: process_authentication_challenge1(e);
665: break;
1.21 markus 666: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33 markus 667: process_request_identities(e, 1);
1.21 markus 668: break;
669: case SSH_AGENTC_ADD_RSA_IDENTITY:
1.94 ! markus 670: case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
1.33 markus 671: process_add_identity(e, 1);
1.21 markus 672: break;
673: case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33 markus 674: process_remove_identity(e, 1);
1.21 markus 675: break;
676: case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.33 markus 677: process_remove_all_identities(e, 1);
678: break;
679: /* ssh2 */
680: case SSH2_AGENTC_SIGN_REQUEST:
681: process_sign_request2(e);
682: break;
683: case SSH2_AGENTC_REQUEST_IDENTITIES:
684: process_request_identities(e, 2);
685: break;
686: case SSH2_AGENTC_ADD_IDENTITY:
1.94 ! markus 687: case SSH2_AGENTC_ADD_ID_CONSTRAINED:
1.33 markus 688: process_add_identity(e, 2);
689: break;
690: case SSH2_AGENTC_REMOVE_IDENTITY:
691: process_remove_identity(e, 2);
692: break;
693: case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
694: process_remove_all_identities(e, 2);
1.21 markus 695: break;
1.59 markus 696: #ifdef SMARTCARD
697: case SSH_AGENTC_ADD_SMARTCARD_KEY:
698: process_add_smartcard_key(e);
1.75 deraadt 699: break;
1.59 markus 700: case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
701: process_remove_smartcard_key(e);
1.75 deraadt 702: break;
1.70 jakob 703: #endif /* SMARTCARD */
1.21 markus 704: default:
705: /* Unknown message. Respond with failure. */
706: error("Unknown message %d", type);
1.87 markus 707: buffer_clear(&e->request);
1.21 markus 708: buffer_put_int(&e->output, 1);
709: buffer_put_char(&e->output, SSH_AGENT_FAILURE);
710: break;
711: }
1.1 deraadt 712: }
713:
1.55 itojun 714: static void
1.73 stevesk 715: new_socket(sock_type type, int fd)
1.1 deraadt 716: {
1.45 markus 717: u_int i, old_alloc;
1.21 markus 718: if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
719: error("fcntl O_NONBLOCK: %s", strerror(errno));
720:
721: if (fd > max_fd)
722: max_fd = fd;
723:
724: for (i = 0; i < sockets_alloc; i++)
725: if (sockets[i].type == AUTH_UNUSED) {
726: sockets[i].fd = fd;
727: sockets[i].type = type;
728: buffer_init(&sockets[i].input);
729: buffer_init(&sockets[i].output);
1.87 markus 730: buffer_init(&sockets[i].request);
1.21 markus 731: return;
732: }
733: old_alloc = sockets_alloc;
734: sockets_alloc += 10;
735: if (sockets)
736: sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
737: else
738: sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
739: for (i = old_alloc; i < sockets_alloc; i++)
740: sockets[i].type = AUTH_UNUSED;
741: sockets[old_alloc].type = type;
742: sockets[old_alloc].fd = fd;
743: buffer_init(&sockets[old_alloc].input);
744: buffer_init(&sockets[old_alloc].output);
1.87 markus 745: buffer_init(&sockets[old_alloc].request);
1.1 deraadt 746: }
747:
1.55 itojun 748: static int
1.66 markus 749: prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
1.1 deraadt 750: {
1.46 markus 751: u_int i, sz;
752: int n = 0;
753:
754: for (i = 0; i < sockets_alloc; i++) {
1.21 markus 755: switch (sockets[i].type) {
756: case AUTH_SOCKET:
757: case AUTH_CONNECTION:
1.46 markus 758: n = MAX(n, sockets[i].fd);
1.21 markus 759: break;
760: case AUTH_UNUSED:
761: break;
762: default:
763: fatal("Unknown socket type %d", sockets[i].type);
764: break;
765: }
1.46 markus 766: }
767:
768: sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1.66 markus 769: if (*fdrp == NULL || sz > *nallocp) {
1.46 markus 770: if (*fdrp)
1.54 stevesk 771: xfree(*fdrp);
1.46 markus 772: if (*fdwp)
1.54 stevesk 773: xfree(*fdwp);
1.46 markus 774: *fdrp = xmalloc(sz);
775: *fdwp = xmalloc(sz);
1.66 markus 776: *nallocp = sz;
1.46 markus 777: }
1.66 markus 778: if (n < *fdl)
779: debug("XXX shrink: %d < %d", n, *fdl);
780: *fdl = n;
1.46 markus 781: memset(*fdrp, 0, sz);
782: memset(*fdwp, 0, sz);
783:
784: for (i = 0; i < sockets_alloc; i++) {
785: switch (sockets[i].type) {
786: case AUTH_SOCKET:
787: case AUTH_CONNECTION:
788: FD_SET(sockets[i].fd, *fdrp);
789: if (buffer_len(&sockets[i].output) > 0)
790: FD_SET(sockets[i].fd, *fdwp);
791: break;
792: default:
793: break;
794: }
795: }
796: return (1);
1.21 markus 797: }
798:
1.55 itojun 799: static void
1.21 markus 800: after_select(fd_set *readset, fd_set *writeset)
801: {
1.45 markus 802: u_int i;
1.21 markus 803: int len, sock;
1.26 markus 804: socklen_t slen;
1.21 markus 805: char buf[1024];
806: struct sockaddr_un sunaddr;
807:
808: for (i = 0; i < sockets_alloc; i++)
809: switch (sockets[i].type) {
810: case AUTH_UNUSED:
811: break;
812: case AUTH_SOCKET:
813: if (FD_ISSET(sockets[i].fd, readset)) {
1.26 markus 814: slen = sizeof(sunaddr);
1.46 markus 815: sock = accept(sockets[i].fd,
816: (struct sockaddr *) &sunaddr, &slen);
1.21 markus 817: if (sock < 0) {
1.81 stevesk 818: error("accept from AUTH_SOCKET: %s",
819: strerror(errno));
1.21 markus 820: break;
821: }
822: new_socket(AUTH_CONNECTION, sock);
823: }
824: break;
825: case AUTH_CONNECTION:
826: if (buffer_len(&sockets[i].output) > 0 &&
827: FD_ISSET(sockets[i].fd, writeset)) {
1.52 deraadt 828: do {
829: len = write(sockets[i].fd,
830: buffer_ptr(&sockets[i].output),
831: buffer_len(&sockets[i].output));
832: if (len == -1 && (errno == EAGAIN ||
833: errno == EINTR))
834: continue;
835: break;
836: } while (1);
1.21 markus 837: if (len <= 0) {
838: shutdown(sockets[i].fd, SHUT_RDWR);
839: close(sockets[i].fd);
840: sockets[i].type = AUTH_UNUSED;
1.30 djm 841: buffer_free(&sockets[i].input);
842: buffer_free(&sockets[i].output);
1.87 markus 843: buffer_free(&sockets[i].request);
1.21 markus 844: break;
845: }
846: buffer_consume(&sockets[i].output, len);
847: }
848: if (FD_ISSET(sockets[i].fd, readset)) {
1.52 deraadt 849: do {
850: len = read(sockets[i].fd, buf, sizeof(buf));
851: if (len == -1 && (errno == EAGAIN ||
852: errno == EINTR))
853: continue;
854: break;
855: } while (1);
1.21 markus 856: if (len <= 0) {
857: shutdown(sockets[i].fd, SHUT_RDWR);
858: close(sockets[i].fd);
859: sockets[i].type = AUTH_UNUSED;
1.30 djm 860: buffer_free(&sockets[i].input);
861: buffer_free(&sockets[i].output);
1.87 markus 862: buffer_free(&sockets[i].request);
1.21 markus 863: break;
864: }
865: buffer_append(&sockets[i].input, buf, len);
866: process_message(&sockets[i]);
867: }
868: break;
869: default:
870: fatal("Unknown type %d", sockets[i].type);
871: }
1.1 deraadt 872: }
873:
1.55 itojun 874: static void
1.81 stevesk 875: cleanup_socket(void *p)
1.15 markus 876: {
1.48 deraadt 877: if (socket_name[0])
878: unlink(socket_name);
879: if (socket_dir[0])
880: rmdir(socket_dir);
1.10 markus 881: }
882:
1.55 itojun 883: static void
1.15 markus 884: cleanup_exit(int i)
885: {
1.81 stevesk 886: cleanup_socket(NULL);
1.21 markus 887: exit(i);
1.15 markus 888: }
889:
1.55 itojun 890: static void
1.48 deraadt 891: cleanup_handler(int sig)
892: {
1.81 stevesk 893: cleanup_socket(NULL);
1.48 deraadt 894: _exit(2);
1.68 markus 895: }
896:
897: static void
898: check_parent_exists(int sig)
899: {
900: int save_errno = errno;
901:
902: if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
903: /* printf("Parent has died - Authentication agent exiting.\n"); */
904: cleanup_handler(sig); /* safe */
905: }
906: signal(SIGALRM, check_parent_exists);
907: alarm(10);
908: errno = save_errno;
1.48 deraadt 909: }
910:
1.55 itojun 911: static void
1.50 itojun 912: usage(void)
1.15 markus 913: {
1.72 jakob 914: fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
1.46 markus 915: __progname);
1.72 jakob 916: fprintf(stderr, "Options:\n");
917: fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
918: fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
919: fprintf(stderr, " -k Kill the current agent.\n");
920: fprintf(stderr, " -d Debug mode.\n");
1.86 markus 921: fprintf(stderr, " -a socket Bind agent socket to given name.\n");
1.21 markus 922: exit(1);
1.15 markus 923: }
924:
1.2 provos 925: int
926: main(int ac, char **av)
1.1 deraadt 927: {
1.66 markus 928: int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
1.21 markus 929: struct sockaddr_un sunaddr;
1.41 markus 930: struct rlimit rlim;
1.21 markus 931: pid_t pid;
932: char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
1.86 markus 933: char *agentsocket = NULL;
1.43 markus 934: extern int optind;
1.46 markus 935: fd_set *readsetp = NULL, *writesetp = NULL;
1.53 markus 936:
937: SSLeay_add_all_algorithms();
1.21 markus 938:
1.86 markus 939: while ((ch = getopt(ac, av, "cdksa:")) != -1) {
1.21 markus 940: switch (ch) {
941: case 'c':
942: if (s_flag)
943: usage();
944: c_flag++;
945: break;
946: case 'k':
947: k_flag++;
948: break;
949: case 's':
950: if (c_flag)
951: usage();
952: s_flag++;
953: break;
1.57 markus 954: case 'd':
955: if (d_flag)
956: usage();
957: d_flag++;
958: break;
1.86 markus 959: case 'a':
960: agentsocket = optarg;
961: break;
1.21 markus 962: default:
963: usage();
964: }
965: }
966: ac -= optind;
967: av += optind;
968:
1.57 markus 969: if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1.21 markus 970: usage();
971:
1.85 markus 972: if (ac == 0 && !c_flag && !s_flag) {
1.21 markus 973: shell = getenv("SHELL");
974: if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
975: c_flag = 1;
976: }
977: if (k_flag) {
978: pidstr = getenv(SSH_AGENTPID_ENV_NAME);
979: if (pidstr == NULL) {
980: fprintf(stderr, "%s not set, cannot kill agent\n",
1.46 markus 981: SSH_AGENTPID_ENV_NAME);
1.21 markus 982: exit(1);
983: }
984: pid = atoi(pidstr);
1.46 markus 985: if (pid < 1) {
1.21 markus 986: fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1.46 markus 987: SSH_AGENTPID_ENV_NAME, pidstr);
1.21 markus 988: exit(1);
989: }
990: if (kill(pid, SIGTERM) == -1) {
991: perror("kill");
992: exit(1);
993: }
994: format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
995: printf(format, SSH_AUTHSOCKET_ENV_NAME);
996: printf(format, SSH_AGENTPID_ENV_NAME);
1.91 mpech 997: printf("echo Agent pid %ld killed;\n", (long)pid);
1.21 markus 998: exit(0);
999: }
1000: parent_pid = getpid();
1001:
1.86 markus 1002: if (agentsocket == NULL) {
1003: /* Create private directory for agent socket */
1004: strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1005: if (mkdtemp(socket_dir) == NULL) {
1006: perror("mkdtemp: private socket dir");
1007: exit(1);
1008: }
1.91 mpech 1009: snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1010: (long)parent_pid);
1.86 markus 1011: } else {
1012: /* Try to use specified agent socket */
1013: socket_dir[0] = '\0';
1014: strlcpy(socket_name, agentsocket, sizeof socket_name);
1.21 markus 1015: }
1016:
1.23 markus 1017: /*
1018: * Create socket early so it will exist before command gets run from
1019: * the parent.
1020: */
1.21 markus 1021: sock = socket(AF_UNIX, SOCK_STREAM, 0);
1022: if (sock < 0) {
1023: perror("socket");
1024: cleanup_exit(1);
1025: }
1026: memset(&sunaddr, 0, sizeof(sunaddr));
1027: sunaddr.sun_family = AF_UNIX;
1028: strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
1029: if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1030: perror("bind");
1031: cleanup_exit(1);
1032: }
1033: if (listen(sock, 5) < 0) {
1034: perror("listen");
1035: cleanup_exit(1);
1036: }
1.46 markus 1037:
1.23 markus 1038: /*
1039: * Fork, and have the parent execute the command, if any, or present
1040: * the socket data. The child continues as the authentication agent.
1041: */
1.57 markus 1042: if (d_flag) {
1043: log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1044: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1045: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1046: SSH_AUTHSOCKET_ENV_NAME);
1.91 mpech 1047: printf("echo Agent pid %ld;\n", (long)parent_pid);
1.57 markus 1048: goto skip;
1049: }
1.21 markus 1050: pid = fork();
1051: if (pid == -1) {
1052: perror("fork");
1.81 stevesk 1053: cleanup_exit(1);
1.21 markus 1054: }
1055: if (pid != 0) { /* Parent - execute the given command. */
1056: close(sock);
1.91 mpech 1057: snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1.21 markus 1058: if (ac == 0) {
1059: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1060: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1.46 markus 1061: SSH_AUTHSOCKET_ENV_NAME);
1.21 markus 1062: printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1.46 markus 1063: SSH_AGENTPID_ENV_NAME);
1.91 mpech 1064: printf("echo Agent pid %ld;\n", (long)pid);
1.21 markus 1065: exit(0);
1066: }
1.36 deraadt 1067: if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1068: setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1069: perror("setenv");
1070: exit(1);
1071: }
1.21 markus 1072: execvp(av[0], av);
1073: perror(av[0]);
1074: exit(1);
1075: }
1.81 stevesk 1076: /* child */
1077: log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1.67 stevesk 1078:
1079: if (setsid() == -1) {
1.81 stevesk 1080: error("setsid: %s", strerror(errno));
1.67 stevesk 1081: cleanup_exit(1);
1082: }
1083:
1084: (void)chdir("/");
1.21 markus 1085: close(0);
1086: close(1);
1087: close(2);
1088:
1.41 markus 1089: /* deny core dumps, since memory contains unencrypted private keys */
1090: rlim.rlim_cur = rlim.rlim_max = 0;
1091: if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1.81 stevesk 1092: error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1.21 markus 1093: cleanup_exit(1);
1094: }
1.57 markus 1095:
1096: skip:
1.81 stevesk 1097: fatal_add_cleanup(cleanup_socket, NULL);
1.21 markus 1098: new_socket(AUTH_SOCKET, sock);
1099: if (ac > 0) {
1100: signal(SIGALRM, check_parent_exists);
1101: alarm(10);
1102: }
1.33 markus 1103: idtab_init();
1.61 markus 1104: if (!d_flag)
1.57 markus 1105: signal(SIGINT, SIG_IGN);
1.61 markus 1106: signal(SIGPIPE, SIG_IGN);
1.48 deraadt 1107: signal(SIGHUP, cleanup_handler);
1108: signal(SIGTERM, cleanup_handler);
1.66 markus 1109: nalloc = 0;
1110:
1.21 markus 1111: while (1) {
1.66 markus 1112: prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1.46 markus 1113: if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1.21 markus 1114: if (errno == EINTR)
1115: continue;
1.81 stevesk 1116: fatal("select: %s", strerror(errno));
1.21 markus 1117: }
1.46 markus 1118: after_select(readsetp, writesetp);
1.15 markus 1119: }
1.21 markus 1120: /* NOTREACHED */
1.1 deraadt 1121: }