Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.87
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.87 ! markus 38: RCSID("$OpenBSD: ssh-agent.c,v 1.86 2002/06/05 16:08:07 markus Exp $");
1.47 markus 39:
40: #include <openssl/evp.h>
41: #include <openssl/md5.h>
1.1 deraadt 42:
43: #include "ssh.h"
44: #include "rsa.h"
45: #include "buffer.h"
46: #include "bufaux.h"
47: #include "xmalloc.h"
48: #include "getput.h"
1.32 markus 49: #include "key.h"
50: #include "authfd.h"
1.37 markus 51: #include "compat.h"
1.47 markus 52: #include "log.h"
1.7 deraadt 53:
1.59 markus 54: #ifdef SMARTCARD
55: #include "scard.h"
1.71 jakob 56: #endif
1.59 markus 57:
1.73 stevesk 58: typedef enum {
59: AUTH_UNUSED,
60: AUTH_SOCKET,
61: AUTH_CONNECTION
62: } sock_type;
63:
1.21 markus 64: typedef struct {
65: int fd;
1.73 stevesk 66: sock_type type;
1.21 markus 67: Buffer input;
68: Buffer output;
1.87 ! markus 69: Buffer request;
1.1 deraadt 70: } SocketEntry;
71:
1.45 markus 72: u_int sockets_alloc = 0;
1.1 deraadt 73: SocketEntry *sockets = NULL;
74:
1.78 provos 75: typedef struct identity {
76: TAILQ_ENTRY(identity) next;
1.33 markus 77: Key *key;
1.21 markus 78: char *comment;
1.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:
1.87 ! markus 190: buffer_get_int(&e->request); /* ignored */
! 191: buffer_get_bignum(&e->request, key->rsa->e);
! 192: buffer_get_bignum(&e->request, key->rsa->n);
! 193: buffer_get_bignum(&e->request, challenge);
1.21 markus 194:
1.33 markus 195: /* Only protocol 1.1 is supported */
1.87 ! markus 196: if (buffer_len(&e->request) == 0)
1.33 markus 197: goto failure;
1.87 ! markus 198: buffer_get(&e->request, session_id, 16);
! 199: response_type = buffer_get_int(&e->request);
1.33 markus 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.87 ! markus 255: blob = buffer_get_string(&e->request, &blen);
! 256: data = buffer_get_string(&e->request, &dlen);
1.37 markus 257:
1.87 ! markus 258: flags = buffer_get_int(&e->request);
1.37 markus 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.87 ! markus 299: bits = buffer_get_int(&e->request);
! 300: buffer_get_bignum(&e->request, key->rsa->e);
! 301: buffer_get_bignum(&e->request, key->rsa->n);
1.33 markus 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:
1.87 ! markus 308: blob = buffer_get_string(&e->request, &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.87 ! markus 374: buffer_get_int(&e->request); /* ignored */
! 375: buffer_get_bignum(&e->request, k->rsa->n);
! 376: buffer_get_bignum(&e->request, k->rsa->e);
! 377: buffer_get_bignum(&e->request, k->rsa->d);
! 378: buffer_get_bignum(&e->request, k->rsa->iqmp);
1.33 markus 379:
380: /* SSH and SSL have p and q swapped */
1.87 ! markus 381: buffer_get_bignum(&e->request, k->rsa->q); /* p */
! 382: buffer_get_bignum(&e->request, 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.87 ! markus 388: type_name = buffer_get_string(&e->request, 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);
1.87 ! markus 394: buffer_get_bignum2(&e->request, k->dsa->p);
! 395: buffer_get_bignum2(&e->request, k->dsa->q);
! 396: buffer_get_bignum2(&e->request, k->dsa->g);
! 397: buffer_get_bignum2(&e->request, k->dsa->pub_key);
! 398: buffer_get_bignum2(&e->request, k->dsa->priv_key);
1.39 markus 399: break;
400: case KEY_RSA:
401: k = key_new_private(type);
1.87 ! markus 402: buffer_get_bignum2(&e->request, k->rsa->n);
! 403: buffer_get_bignum2(&e->request, k->rsa->e);
! 404: buffer_get_bignum2(&e->request, k->rsa->d);
! 405: buffer_get_bignum2(&e->request, k->rsa->iqmp);
! 406: buffer_get_bignum2(&e->request, k->rsa->p);
! 407: buffer_get_bignum2(&e->request, k->rsa->q);
1.39 markus 408:
409: /* Generate additional parameters */
1.60 markus 410: rsa_generate_additional_parameters(k->rsa);
1.39 markus 411: break;
412: default:
1.87 ! markus 413: buffer_clear(&e->request);
1.33 markus 414: goto send;
1.21 markus 415: }
1.33 markus 416: break;
417: }
1.87 ! markus 418: comment = buffer_get_string(&e->request, NULL);
1.33 markus 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: {
1.84 markus 446: Identity *id;
1.59 markus 447: Idtab *tab;
1.84 markus 448: Key **keys, *k;
1.83 rees 449: char *sc_reader_id = NULL, *pin;
1.84 markus 450: int i, version, success = 0;
1.75 deraadt 451:
1.87 ! markus 452: sc_reader_id = buffer_get_string(&e->request, NULL);
! 453: pin = buffer_get_string(&e->request, NULL);
1.84 markus 454: keys = sc_get_keys(sc_reader_id, pin);
1.69 markus 455: xfree(sc_reader_id);
1.83 rees 456: xfree(pin);
1.59 markus 457:
1.84 markus 458: if (keys == NULL || keys[0] == NULL) {
459: error("sc_get_keys failed");
1.59 markus 460: goto send;
461: }
1.84 markus 462: for (i = 0; keys[i] != NULL; i++) {
463: k = keys[i];
464: version = k->type == KEY_RSA1 ? 1 : 2;
465: tab = idtab_lookup(version);
466: if (lookup_identity(k, version) == NULL) {
467: id = xmalloc(sizeof(Identity));
468: id->key = k;
469: id->comment = xstrdup("smartcard key");
470: TAILQ_INSERT_TAIL(&tab->idlist, id, next);
471: tab->nentries++;
472: success = 1;
473: } else {
474: key_free(k);
475: }
476: keys[i] = NULL;
1.59 markus 477: }
1.84 markus 478: xfree(keys);
1.59 markus 479: send:
480: buffer_put_int(&e->output, 1);
481: buffer_put_char(&e->output,
482: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
483: }
484:
485: static void
486: process_remove_smartcard_key(SocketEntry *e)
487: {
1.84 markus 488: Identity *id;
489: Idtab *tab;
490: Key **keys, *k = NULL;
1.83 rees 491: char *sc_reader_id = NULL, *pin;
1.84 markus 492: int i, version, success = 0;
1.59 markus 493:
1.87 ! markus 494: sc_reader_id = buffer_get_string(&e->request, NULL);
! 495: pin = buffer_get_string(&e->request, NULL);
1.84 markus 496: keys = sc_get_keys(sc_reader_id, pin);
1.69 markus 497: xfree(sc_reader_id);
1.83 rees 498: xfree(pin);
1.59 markus 499:
1.84 markus 500: if (keys == NULL || keys[0] == NULL) {
501: error("sc_get_keys failed");
502: goto send;
503: }
504: for (i = 0; keys[i] != NULL; i++) {
505: k = keys[i];
506: version = k->type == KEY_RSA1 ? 1 : 2;
507: if ((id = lookup_identity(k, version)) != NULL) {
508: tab = idtab_lookup(version);
509: TAILQ_REMOVE(&tab->idlist, id, next);
1.59 markus 510: tab->nentries--;
1.78 provos 511: free_identity(id);
1.59 markus 512: success = 1;
513: }
514: key_free(k);
1.84 markus 515: keys[i] = NULL;
1.59 markus 516: }
1.84 markus 517: xfree(keys);
518: send:
1.59 markus 519: buffer_put_int(&e->output, 1);
520: buffer_put_char(&e->output,
521: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
522: }
1.70 jakob 523: #endif /* SMARTCARD */
1.59 markus 524:
1.33 markus 525: /* dispatch incoming messages */
526:
1.55 itojun 527: static void
1.2 provos 528: process_message(SocketEntry *e)
1.1 deraadt 529: {
1.45 markus 530: u_int msg_len;
531: u_int type;
532: u_char *cp;
1.21 markus 533: if (buffer_len(&e->input) < 5)
534: return; /* Incomplete message. */
1.77 stevesk 535: cp = buffer_ptr(&e->input);
1.21 markus 536: msg_len = GET_32BIT(cp);
537: if (msg_len > 256 * 1024) {
538: shutdown(e->fd, SHUT_RDWR);
539: close(e->fd);
540: e->type = AUTH_UNUSED;
1.87 ! markus 541: buffer_free(&e->input);
! 542: buffer_free(&e->output);
! 543: buffer_free(&e->request);
1.21 markus 544: return;
545: }
546: if (buffer_len(&e->input) < msg_len + 4)
547: return;
1.87 ! markus 548:
! 549: /* move the current input to e->request */
1.21 markus 550: buffer_consume(&e->input, 4);
1.87 ! markus 551: buffer_clear(&e->request);
! 552: buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
! 553: buffer_consume(&e->input, msg_len);
! 554: type = buffer_get_char(&e->request);
1.21 markus 555:
1.59 markus 556: debug("type %d", type);
1.21 markus 557: switch (type) {
1.33 markus 558: /* ssh1 */
559: case SSH_AGENTC_RSA_CHALLENGE:
560: process_authentication_challenge1(e);
561: break;
1.21 markus 562: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33 markus 563: process_request_identities(e, 1);
1.21 markus 564: break;
565: case SSH_AGENTC_ADD_RSA_IDENTITY:
1.33 markus 566: process_add_identity(e, 1);
1.21 markus 567: break;
568: case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33 markus 569: process_remove_identity(e, 1);
1.21 markus 570: break;
571: case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.33 markus 572: process_remove_all_identities(e, 1);
573: break;
574: /* ssh2 */
575: case SSH2_AGENTC_SIGN_REQUEST:
576: process_sign_request2(e);
577: break;
578: case SSH2_AGENTC_REQUEST_IDENTITIES:
579: process_request_identities(e, 2);
580: break;
581: case SSH2_AGENTC_ADD_IDENTITY:
582: process_add_identity(e, 2);
583: break;
584: case SSH2_AGENTC_REMOVE_IDENTITY:
585: process_remove_identity(e, 2);
586: break;
587: case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
588: process_remove_all_identities(e, 2);
1.21 markus 589: break;
1.59 markus 590: #ifdef SMARTCARD
591: case SSH_AGENTC_ADD_SMARTCARD_KEY:
592: process_add_smartcard_key(e);
1.75 deraadt 593: break;
1.59 markus 594: case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
595: process_remove_smartcard_key(e);
1.75 deraadt 596: break;
1.70 jakob 597: #endif /* SMARTCARD */
1.21 markus 598: default:
599: /* Unknown message. Respond with failure. */
600: error("Unknown message %d", type);
1.87 ! markus 601: buffer_clear(&e->request);
1.21 markus 602: buffer_put_int(&e->output, 1);
603: buffer_put_char(&e->output, SSH_AGENT_FAILURE);
604: break;
605: }
1.1 deraadt 606: }
607:
1.55 itojun 608: static void
1.73 stevesk 609: new_socket(sock_type type, int fd)
1.1 deraadt 610: {
1.45 markus 611: u_int i, old_alloc;
1.21 markus 612: if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
613: error("fcntl O_NONBLOCK: %s", strerror(errno));
614:
615: if (fd > max_fd)
616: max_fd = fd;
617:
618: for (i = 0; i < sockets_alloc; i++)
619: if (sockets[i].type == AUTH_UNUSED) {
620: sockets[i].fd = fd;
621: sockets[i].type = type;
622: buffer_init(&sockets[i].input);
623: buffer_init(&sockets[i].output);
1.87 ! markus 624: buffer_init(&sockets[i].request);
1.21 markus 625: return;
626: }
627: old_alloc = sockets_alloc;
628: sockets_alloc += 10;
629: if (sockets)
630: sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
631: else
632: sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
633: for (i = old_alloc; i < sockets_alloc; i++)
634: sockets[i].type = AUTH_UNUSED;
635: sockets[old_alloc].type = type;
636: sockets[old_alloc].fd = fd;
637: buffer_init(&sockets[old_alloc].input);
638: buffer_init(&sockets[old_alloc].output);
1.87 ! markus 639: buffer_init(&sockets[old_alloc].request);
1.1 deraadt 640: }
641:
1.55 itojun 642: static int
1.66 markus 643: prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
1.1 deraadt 644: {
1.46 markus 645: u_int i, sz;
646: int n = 0;
647:
648: for (i = 0; i < sockets_alloc; i++) {
1.21 markus 649: switch (sockets[i].type) {
650: case AUTH_SOCKET:
651: case AUTH_CONNECTION:
1.46 markus 652: n = MAX(n, sockets[i].fd);
1.21 markus 653: break;
654: case AUTH_UNUSED:
655: break;
656: default:
657: fatal("Unknown socket type %d", sockets[i].type);
658: break;
659: }
1.46 markus 660: }
661:
662: sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1.66 markus 663: if (*fdrp == NULL || sz > *nallocp) {
1.46 markus 664: if (*fdrp)
1.54 stevesk 665: xfree(*fdrp);
1.46 markus 666: if (*fdwp)
1.54 stevesk 667: xfree(*fdwp);
1.46 markus 668: *fdrp = xmalloc(sz);
669: *fdwp = xmalloc(sz);
1.66 markus 670: *nallocp = sz;
1.46 markus 671: }
1.66 markus 672: if (n < *fdl)
673: debug("XXX shrink: %d < %d", n, *fdl);
674: *fdl = n;
1.46 markus 675: memset(*fdrp, 0, sz);
676: memset(*fdwp, 0, sz);
677:
678: for (i = 0; i < sockets_alloc; i++) {
679: switch (sockets[i].type) {
680: case AUTH_SOCKET:
681: case AUTH_CONNECTION:
682: FD_SET(sockets[i].fd, *fdrp);
683: if (buffer_len(&sockets[i].output) > 0)
684: FD_SET(sockets[i].fd, *fdwp);
685: break;
686: default:
687: break;
688: }
689: }
690: return (1);
1.21 markus 691: }
692:
1.55 itojun 693: static void
1.21 markus 694: after_select(fd_set *readset, fd_set *writeset)
695: {
1.45 markus 696: u_int i;
1.21 markus 697: int len, sock;
1.26 markus 698: socklen_t slen;
1.21 markus 699: char buf[1024];
700: struct sockaddr_un sunaddr;
701:
702: for (i = 0; i < sockets_alloc; i++)
703: switch (sockets[i].type) {
704: case AUTH_UNUSED:
705: break;
706: case AUTH_SOCKET:
707: if (FD_ISSET(sockets[i].fd, readset)) {
1.26 markus 708: slen = sizeof(sunaddr);
1.46 markus 709: sock = accept(sockets[i].fd,
710: (struct sockaddr *) &sunaddr, &slen);
1.21 markus 711: if (sock < 0) {
1.81 stevesk 712: error("accept from AUTH_SOCKET: %s",
713: strerror(errno));
1.21 markus 714: break;
715: }
716: new_socket(AUTH_CONNECTION, sock);
717: }
718: break;
719: case AUTH_CONNECTION:
720: if (buffer_len(&sockets[i].output) > 0 &&
721: FD_ISSET(sockets[i].fd, writeset)) {
1.52 deraadt 722: do {
723: len = write(sockets[i].fd,
724: buffer_ptr(&sockets[i].output),
725: buffer_len(&sockets[i].output));
726: if (len == -1 && (errno == EAGAIN ||
727: errno == EINTR))
728: continue;
729: break;
730: } while (1);
1.21 markus 731: if (len <= 0) {
732: shutdown(sockets[i].fd, SHUT_RDWR);
733: close(sockets[i].fd);
734: sockets[i].type = AUTH_UNUSED;
1.30 djm 735: buffer_free(&sockets[i].input);
736: buffer_free(&sockets[i].output);
1.87 ! markus 737: buffer_free(&sockets[i].request);
1.21 markus 738: break;
739: }
740: buffer_consume(&sockets[i].output, len);
741: }
742: if (FD_ISSET(sockets[i].fd, readset)) {
1.52 deraadt 743: do {
744: len = read(sockets[i].fd, buf, sizeof(buf));
745: if (len == -1 && (errno == EAGAIN ||
746: errno == EINTR))
747: continue;
748: break;
749: } while (1);
1.21 markus 750: if (len <= 0) {
751: shutdown(sockets[i].fd, SHUT_RDWR);
752: close(sockets[i].fd);
753: sockets[i].type = AUTH_UNUSED;
1.30 djm 754: buffer_free(&sockets[i].input);
755: buffer_free(&sockets[i].output);
1.87 ! markus 756: buffer_free(&sockets[i].request);
1.21 markus 757: break;
758: }
759: buffer_append(&sockets[i].input, buf, len);
760: process_message(&sockets[i]);
761: }
762: break;
763: default:
764: fatal("Unknown type %d", sockets[i].type);
765: }
1.1 deraadt 766: }
767:
1.55 itojun 768: static void
1.81 stevesk 769: cleanup_socket(void *p)
1.15 markus 770: {
1.48 deraadt 771: if (socket_name[0])
772: unlink(socket_name);
773: if (socket_dir[0])
774: rmdir(socket_dir);
1.10 markus 775: }
776:
1.55 itojun 777: static void
1.15 markus 778: cleanup_exit(int i)
779: {
1.81 stevesk 780: cleanup_socket(NULL);
1.21 markus 781: exit(i);
1.15 markus 782: }
783:
1.55 itojun 784: static void
1.48 deraadt 785: cleanup_handler(int sig)
786: {
1.81 stevesk 787: cleanup_socket(NULL);
1.48 deraadt 788: _exit(2);
1.68 markus 789: }
790:
791: static void
792: check_parent_exists(int sig)
793: {
794: int save_errno = errno;
795:
796: if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
797: /* printf("Parent has died - Authentication agent exiting.\n"); */
798: cleanup_handler(sig); /* safe */
799: }
800: signal(SIGALRM, check_parent_exists);
801: alarm(10);
802: errno = save_errno;
1.48 deraadt 803: }
804:
1.55 itojun 805: static void
1.50 itojun 806: usage(void)
1.15 markus 807: {
1.72 jakob 808: fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
1.46 markus 809: __progname);
1.72 jakob 810: fprintf(stderr, "Options:\n");
811: fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
812: fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
813: fprintf(stderr, " -k Kill the current agent.\n");
814: fprintf(stderr, " -d Debug mode.\n");
1.86 markus 815: fprintf(stderr, " -a socket Bind agent socket to given name.\n");
1.21 markus 816: exit(1);
1.15 markus 817: }
818:
1.2 provos 819: int
820: main(int ac, char **av)
1.1 deraadt 821: {
1.66 markus 822: int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
1.21 markus 823: struct sockaddr_un sunaddr;
1.41 markus 824: struct rlimit rlim;
1.21 markus 825: pid_t pid;
826: char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
1.86 markus 827: char *agentsocket = NULL;
1.43 markus 828: extern int optind;
1.46 markus 829: fd_set *readsetp = NULL, *writesetp = NULL;
1.53 markus 830:
831: SSLeay_add_all_algorithms();
1.21 markus 832:
1.86 markus 833: while ((ch = getopt(ac, av, "cdksa:")) != -1) {
1.21 markus 834: switch (ch) {
835: case 'c':
836: if (s_flag)
837: usage();
838: c_flag++;
839: break;
840: case 'k':
841: k_flag++;
842: break;
843: case 's':
844: if (c_flag)
845: usage();
846: s_flag++;
847: break;
1.57 markus 848: case 'd':
849: if (d_flag)
850: usage();
851: d_flag++;
852: break;
1.86 markus 853: case 'a':
854: agentsocket = optarg;
855: break;
1.21 markus 856: default:
857: usage();
858: }
859: }
860: ac -= optind;
861: av += optind;
862:
1.57 markus 863: if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1.21 markus 864: usage();
865:
1.85 markus 866: if (ac == 0 && !c_flag && !s_flag) {
1.21 markus 867: shell = getenv("SHELL");
868: if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
869: c_flag = 1;
870: }
871: if (k_flag) {
872: pidstr = getenv(SSH_AGENTPID_ENV_NAME);
873: if (pidstr == NULL) {
874: fprintf(stderr, "%s not set, cannot kill agent\n",
1.46 markus 875: SSH_AGENTPID_ENV_NAME);
1.21 markus 876: exit(1);
877: }
878: pid = atoi(pidstr);
1.46 markus 879: if (pid < 1) {
1.21 markus 880: fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1.46 markus 881: SSH_AGENTPID_ENV_NAME, pidstr);
1.21 markus 882: exit(1);
883: }
884: if (kill(pid, SIGTERM) == -1) {
885: perror("kill");
886: exit(1);
887: }
888: format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
889: printf(format, SSH_AUTHSOCKET_ENV_NAME);
890: printf(format, SSH_AGENTPID_ENV_NAME);
891: printf("echo Agent pid %d killed;\n", pid);
892: exit(0);
893: }
894: parent_pid = getpid();
895:
1.86 markus 896: if (agentsocket == NULL) {
897: /* Create private directory for agent socket */
898: strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
899: if (mkdtemp(socket_dir) == NULL) {
900: perror("mkdtemp: private socket dir");
901: exit(1);
902: }
903: snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
904: parent_pid);
905: } else {
906: /* Try to use specified agent socket */
907: socket_dir[0] = '\0';
908: strlcpy(socket_name, agentsocket, sizeof socket_name);
1.21 markus 909: }
910:
1.23 markus 911: /*
912: * Create socket early so it will exist before command gets run from
913: * the parent.
914: */
1.21 markus 915: sock = socket(AF_UNIX, SOCK_STREAM, 0);
916: if (sock < 0) {
917: perror("socket");
918: cleanup_exit(1);
919: }
920: memset(&sunaddr, 0, sizeof(sunaddr));
921: sunaddr.sun_family = AF_UNIX;
922: strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
923: if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
924: perror("bind");
925: cleanup_exit(1);
926: }
927: if (listen(sock, 5) < 0) {
928: perror("listen");
929: cleanup_exit(1);
930: }
1.46 markus 931:
1.23 markus 932: /*
933: * Fork, and have the parent execute the command, if any, or present
934: * the socket data. The child continues as the authentication agent.
935: */
1.57 markus 936: if (d_flag) {
937: log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
938: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
939: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
940: SSH_AUTHSOCKET_ENV_NAME);
941: printf("echo Agent pid %d;\n", parent_pid);
942: goto skip;
943: }
1.21 markus 944: pid = fork();
945: if (pid == -1) {
946: perror("fork");
1.81 stevesk 947: cleanup_exit(1);
1.21 markus 948: }
949: if (pid != 0) { /* Parent - execute the given command. */
950: close(sock);
951: snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
952: if (ac == 0) {
953: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
954: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1.46 markus 955: SSH_AUTHSOCKET_ENV_NAME);
1.21 markus 956: printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1.46 markus 957: SSH_AGENTPID_ENV_NAME);
1.21 markus 958: printf("echo Agent pid %d;\n", pid);
959: exit(0);
960: }
1.36 deraadt 961: if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
962: setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
963: perror("setenv");
964: exit(1);
965: }
1.21 markus 966: execvp(av[0], av);
967: perror(av[0]);
968: exit(1);
969: }
1.81 stevesk 970: /* child */
971: log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1.67 stevesk 972:
973: if (setsid() == -1) {
1.81 stevesk 974: error("setsid: %s", strerror(errno));
1.67 stevesk 975: cleanup_exit(1);
976: }
977:
978: (void)chdir("/");
1.21 markus 979: close(0);
980: close(1);
981: close(2);
982:
1.41 markus 983: /* deny core dumps, since memory contains unencrypted private keys */
984: rlim.rlim_cur = rlim.rlim_max = 0;
985: if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1.81 stevesk 986: error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1.21 markus 987: cleanup_exit(1);
988: }
1.57 markus 989:
990: skip:
1.81 stevesk 991: fatal_add_cleanup(cleanup_socket, NULL);
1.21 markus 992: new_socket(AUTH_SOCKET, sock);
993: if (ac > 0) {
994: signal(SIGALRM, check_parent_exists);
995: alarm(10);
996: }
1.33 markus 997: idtab_init();
1.61 markus 998: if (!d_flag)
1.57 markus 999: signal(SIGINT, SIG_IGN);
1.61 markus 1000: signal(SIGPIPE, SIG_IGN);
1.48 deraadt 1001: signal(SIGHUP, cleanup_handler);
1002: signal(SIGTERM, cleanup_handler);
1.66 markus 1003: nalloc = 0;
1004:
1.21 markus 1005: while (1) {
1.66 markus 1006: prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1.46 markus 1007: if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1.21 markus 1008: if (errno == EINTR)
1009: continue;
1.81 stevesk 1010: fatal("select: %s", strerror(errno));
1.21 markus 1011: }
1.46 markus 1012: after_select(readsetp, writesetp);
1.15 markus 1013: }
1.21 markus 1014: /* NOTREACHED */
1.1 deraadt 1015: }