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