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