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