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