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