Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.68
1.68 ! markus 1: /* $OpenBSD: ssh-agent.c,v 1.67 2001/07/18 21:40:40 stevesk Exp $ */
1.15 markus 2:
1.1 deraadt 3: /*
1.22 deraadt 4: * Author: Tatu Ylonen <ylo@cs.hut.fi>
5: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6: * All rights reserved
7: * The authentication agent program.
1.33 markus 8: *
1.35 deraadt 9: * As far as I am concerned, the code I have written for this software
10: * can be used freely for any purpose. Any derived versions of this
11: * software must be clearly marked as such, and if the derived work is
12: * incompatible with the protocol description in the RFC file, it must be
13: * called by a name other than "ssh" or "Secure Shell".
14: *
1.56 markus 15: * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
1.35 deraadt 16: *
17: * Redistribution and use in source and binary forms, with or without
18: * modification, are permitted provided that the following conditions
19: * are met:
20: * 1. Redistributions of source code must retain the above copyright
21: * notice, this list of conditions and the following disclaimer.
22: * 2. Redistributions in binary form must reproduce the above copyright
23: * notice, this list of conditions and the following disclaimer in the
24: * documentation and/or other materials provided with the distribution.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.22 deraadt 36: */
1.1 deraadt 37:
38: #include "includes.h"
1.68 ! markus 39: RCSID("$OpenBSD: ssh-agent.c,v 1.67 2001/07/18 21:40:40 stevesk Exp $");
1.47 markus 40:
41: #include <openssl/evp.h>
42: #include <openssl/md5.h>
1.1 deraadt 43:
44: #include "ssh.h"
45: #include "rsa.h"
46: #include "buffer.h"
47: #include "bufaux.h"
48: #include "xmalloc.h"
49: #include "packet.h"
50: #include "getput.h"
51: #include "mpaux.h"
1.32 markus 52: #include "key.h"
53: #include "authfd.h"
1.47 markus 54: #include "cipher.h"
1.33 markus 55: #include "kex.h"
1.37 markus 56: #include "compat.h"
1.47 markus 57: #include "log.h"
1.7 deraadt 58:
1.59 markus 59: #ifdef SMARTCARD
60: #include <openssl/engine.h>
61: #include "scard.h"
62: #endif
63:
1.21 markus 64: typedef struct {
65: int fd;
66: enum {
67: AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
68: } type;
69: Buffer input;
70: Buffer output;
1.1 deraadt 71: } SocketEntry;
72:
1.45 markus 73: u_int sockets_alloc = 0;
1.1 deraadt 74: SocketEntry *sockets = NULL;
75:
1.21 markus 76: typedef struct {
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;
83: Identity *identities;
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;
104: for (i = 0; i <=2; i++){
105: idtable[i].identities = NULL;
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.55 itojun 120: static Key *
1.33 markus 121: lookup_private_key(Key *key, int *idx, int version)
122: {
123: int i;
124: Idtab *tab = idtab_lookup(version);
125: for (i = 0; i < tab->nentries; i++) {
126: if (key_equal(key, tab->identities[i].key)) {
127: if (idx != NULL)
128: *idx = i;
129: return tab->identities[i].key;
130: }
131: }
132: return NULL;
133: }
134:
135: /* send list of supported public keys to 'client' */
1.55 itojun 136: static void
1.33 markus 137: process_request_identities(SocketEntry *e, int version)
138: {
139: Idtab *tab = idtab_lookup(version);
1.21 markus 140: Buffer msg;
141: int i;
1.1 deraadt 142:
1.21 markus 143: buffer_init(&msg);
1.33 markus 144: buffer_put_char(&msg, (version == 1) ?
145: SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
146: buffer_put_int(&msg, tab->nentries);
147: for (i = 0; i < tab->nentries; i++) {
148: Identity *id = &tab->identities[i];
1.39 markus 149: if (id->key->type == KEY_RSA1) {
1.33 markus 150: buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
151: buffer_put_bignum(&msg, id->key->rsa->e);
152: buffer_put_bignum(&msg, id->key->rsa->n);
153: } else {
1.45 markus 154: u_char *blob;
155: u_int blen;
1.39 markus 156: key_to_blob(id->key, &blob, &blen);
1.33 markus 157: buffer_put_string(&msg, blob, blen);
158: xfree(blob);
159: }
160: buffer_put_cstring(&msg, id->comment);
1.21 markus 161: }
162: buffer_put_int(&e->output, buffer_len(&msg));
163: buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
164: buffer_free(&msg);
1.1 deraadt 165: }
166:
1.33 markus 167: /* ssh1 only */
1.55 itojun 168: static void
1.33 markus 169: process_authentication_challenge1(SocketEntry *e)
1.1 deraadt 170: {
1.33 markus 171: Key *key, *private;
172: BIGNUM *challenge;
173: int i, len;
1.21 markus 174: Buffer msg;
175: MD5_CTX md;
1.45 markus 176: u_char buf[32], mdbuf[16], session_id[16];
177: u_int response_type;
1.21 markus 178:
179: buffer_init(&msg);
1.39 markus 180: key = key_new(KEY_RSA1);
1.21 markus 181: challenge = BN_new();
1.33 markus 182:
183: buffer_get_int(&e->input); /* ignored */
184: buffer_get_bignum(&e->input, key->rsa->e);
185: buffer_get_bignum(&e->input, key->rsa->n);
1.21 markus 186: buffer_get_bignum(&e->input, challenge);
187:
1.33 markus 188: /* Only protocol 1.1 is supported */
189: if (buffer_len(&e->input) == 0)
190: goto failure;
191: buffer_get(&e->input, (char *) session_id, 16);
192: response_type = buffer_get_int(&e->input);
193: if (response_type != 1)
194: goto failure;
195:
196: private = lookup_private_key(key, NULL, 1);
197: if (private != NULL) {
198: /* Decrypt the challenge using the private key. */
1.49 markus 199: if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
200: goto failure;
1.33 markus 201:
202: /* The response is MD5 of decrypted challenge plus session id. */
203: len = BN_num_bytes(challenge);
204: if (len <= 0 || len > 32) {
205: log("process_authentication_challenge: bad challenge length %d", len);
206: goto failure;
207: }
208: memset(buf, 0, 32);
209: BN_bn2bin(challenge, buf + 32 - len);
210: MD5_Init(&md);
211: MD5_Update(&md, buf, 32);
212: MD5_Update(&md, session_id, 16);
213: MD5_Final(mdbuf, &md);
214:
215: /* Send the response. */
216: buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
217: for (i = 0; i < 16; i++)
218: buffer_put_char(&msg, mdbuf[i]);
219: goto send;
220: }
1.21 markus 221:
1.33 markus 222: failure:
223: /* Unknown identity or protocol error. Send failure. */
1.21 markus 224: buffer_put_char(&msg, SSH_AGENT_FAILURE);
225: send:
226: buffer_put_int(&e->output, buffer_len(&msg));
1.33 markus 227: buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
228: key_free(key);
229: BN_clear_free(challenge);
230: buffer_free(&msg);
231: }
232:
233: /* ssh2 only */
1.55 itojun 234: static void
1.33 markus 235: process_sign_request2(SocketEntry *e)
236: {
237: extern int datafellows;
238: Key *key, *private;
1.45 markus 239: u_char *blob, *data, *signature = NULL;
240: u_int blen, dlen, slen = 0;
1.37 markus 241: int flags;
1.33 markus 242: Buffer msg;
243: int ok = -1;
244:
245: datafellows = 0;
1.43 markus 246:
1.33 markus 247: blob = buffer_get_string(&e->input, &blen);
248: data = buffer_get_string(&e->input, &dlen);
1.37 markus 249:
250: flags = buffer_get_int(&e->input);
251: if (flags & SSH_AGENT_OLD_SIGNATURE)
252: datafellows = SSH_BUG_SIGBLOB;
1.33 markus 253:
1.39 markus 254: key = key_from_blob(blob, blen);
1.33 markus 255: if (key != NULL) {
256: private = lookup_private_key(key, NULL, 2);
257: if (private != NULL)
1.39 markus 258: ok = key_sign(private, &signature, &slen, data, dlen);
1.33 markus 259: }
260: key_free(key);
261: buffer_init(&msg);
262: if (ok == 0) {
263: buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
264: buffer_put_string(&msg, signature, slen);
265: } else {
266: buffer_put_char(&msg, SSH_AGENT_FAILURE);
267: }
268: buffer_put_int(&e->output, buffer_len(&msg));
1.21 markus 269: buffer_append(&e->output, buffer_ptr(&msg),
1.33 markus 270: buffer_len(&msg));
1.21 markus 271: buffer_free(&msg);
1.33 markus 272: xfree(data);
273: xfree(blob);
274: if (signature != NULL)
275: xfree(signature);
1.1 deraadt 276: }
277:
1.33 markus 278: /* shared */
1.55 itojun 279: static void
1.33 markus 280: process_remove_identity(SocketEntry *e, int version)
1.1 deraadt 281: {
1.33 markus 282: Key *key = NULL, *private;
1.45 markus 283: u_char *blob;
284: u_int blen;
285: u_int bits;
1.33 markus 286: int success = 0;
1.21 markus 287:
1.33 markus 288: switch(version){
289: case 1:
1.39 markus 290: key = key_new(KEY_RSA1);
1.33 markus 291: bits = buffer_get_int(&e->input);
292: buffer_get_bignum(&e->input, key->rsa->e);
293: buffer_get_bignum(&e->input, key->rsa->n);
294:
295: if (bits != key_size(key))
296: log("Warning: identity keysize mismatch: actual %d, announced %d",
1.46 markus 297: key_size(key), bits);
1.33 markus 298: break;
299: case 2:
300: blob = buffer_get_string(&e->input, &blen);
1.39 markus 301: key = key_from_blob(blob, blen);
1.33 markus 302: xfree(blob);
303: break;
304: }
305: if (key != NULL) {
306: int idx;
307: private = lookup_private_key(key, &idx, version);
308: if (private != NULL) {
1.23 markus 309: /*
310: * We have this key. Free the old key. Since we
311: * don\'t want to leave empty slots in the middle of
1.40 markus 312: * the array, we actually free the key there and move
313: * all the entries between the empty slot and the end
314: * of the array.
1.23 markus 315: */
1.33 markus 316: Idtab *tab = idtab_lookup(version);
317: key_free(tab->identities[idx].key);
318: xfree(tab->identities[idx].comment);
1.38 markus 319: if (tab->nentries < 1)
320: fatal("process_remove_identity: "
321: "internal error: tab->nentries %d",
322: tab->nentries);
1.40 markus 323: if (idx != tab->nentries - 1) {
324: int i;
325: for (i = idx; i < tab->nentries - 1; i++)
326: tab->identities[i] = tab->identities[i+1];
327: }
328: tab->identities[tab->nentries - 1].key = NULL;
329: tab->identities[tab->nentries - 1].comment = NULL;
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.45 markus 343: u_int i;
1.33 markus 344: Idtab *tab = idtab_lookup(version);
1.21 markus 345:
346: /* Loop over all identities and clear the keys. */
1.33 markus 347: for (i = 0; i < tab->nentries; i++) {
348: key_free(tab->identities[i].key);
349: xfree(tab->identities[i].comment);
1.21 markus 350: }
351:
352: /* Mark that there are no identities. */
1.33 markus 353: tab->nentries = 0;
1.21 markus 354:
355: /* Send success. */
356: buffer_put_int(&e->output, 1);
357: buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
358: return;
1.1 deraadt 359: }
360:
1.55 itojun 361: static void
1.33 markus 362: process_add_identity(SocketEntry *e, int version)
1.1 deraadt 363: {
1.33 markus 364: Key *k = NULL;
1.39 markus 365: char *type_name;
1.33 markus 366: char *comment;
1.39 markus 367: int type, success = 0;
1.33 markus 368: Idtab *tab = idtab_lookup(version);
369:
370: switch (version) {
371: case 1:
1.39 markus 372: k = key_new_private(KEY_RSA1);
1.46 markus 373: buffer_get_int(&e->input); /* ignored */
1.39 markus 374: buffer_get_bignum(&e->input, k->rsa->n);
375: buffer_get_bignum(&e->input, k->rsa->e);
376: buffer_get_bignum(&e->input, k->rsa->d);
377: buffer_get_bignum(&e->input, k->rsa->iqmp);
1.33 markus 378:
379: /* SSH and SSL have p and q swapped */
1.39 markus 380: buffer_get_bignum(&e->input, k->rsa->q); /* p */
381: buffer_get_bignum(&e->input, k->rsa->p); /* q */
1.33 markus 382:
383: /* Generate additional parameters */
1.60 markus 384: rsa_generate_additional_parameters(k->rsa);
1.33 markus 385: break;
386: case 2:
1.39 markus 387: type_name = buffer_get_string(&e->input, NULL);
1.46 markus 388: type = key_type_from_name(type_name);
1.39 markus 389: xfree(type_name);
390: switch(type) {
391: case KEY_DSA:
392: k = key_new_private(type);
393: buffer_get_bignum2(&e->input, k->dsa->p);
394: buffer_get_bignum2(&e->input, k->dsa->q);
395: buffer_get_bignum2(&e->input, k->dsa->g);
396: buffer_get_bignum2(&e->input, k->dsa->pub_key);
397: buffer_get_bignum2(&e->input, k->dsa->priv_key);
398: break;
399: case KEY_RSA:
400: k = key_new_private(type);
401: buffer_get_bignum2(&e->input, k->rsa->n);
402: buffer_get_bignum2(&e->input, k->rsa->e);
403: buffer_get_bignum2(&e->input, k->rsa->d);
404: buffer_get_bignum2(&e->input, k->rsa->iqmp);
405: buffer_get_bignum2(&e->input, k->rsa->p);
406: buffer_get_bignum2(&e->input, k->rsa->q);
407:
408: /* Generate additional parameters */
1.60 markus 409: rsa_generate_additional_parameters(k->rsa);
1.39 markus 410: break;
411: default:
1.33 markus 412: buffer_clear(&e->input);
413: goto send;
1.21 markus 414: }
1.33 markus 415: break;
416: }
417: comment = buffer_get_string(&e->input, NULL);
418: if (k == NULL) {
419: xfree(comment);
420: goto send;
421: }
422: success = 1;
423: if (lookup_private_key(k, NULL, version) == NULL) {
424: if (tab->nentries == 0)
425: tab->identities = xmalloc(sizeof(Identity));
426: else
427: tab->identities = xrealloc(tab->identities,
428: (tab->nentries + 1) * sizeof(Identity));
429: tab->identities[tab->nentries].key = k;
430: tab->identities[tab->nentries].comment = comment;
431: /* Increment the number of identities. */
432: tab->nentries++;
433: } else {
434: key_free(k);
435: xfree(comment);
436: }
437: send:
1.1 deraadt 438: buffer_put_int(&e->output, 1);
1.33 markus 439: buffer_put_char(&e->output,
440: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1 deraadt 441: }
442:
1.59 markus 443:
444: #ifdef SMARTCARD
445: static void
446: process_add_smartcard_key (SocketEntry *e)
447: {
448: Idtab *tab;
449: Key *n = NULL, *k = NULL;
450: int success = 0;
451: int sc_reader_num = 0;
452:
453: sc_reader_num = buffer_get_int(&e->input);
454:
455: k = sc_get_key(sc_reader_num);
456: if (k == NULL) {
457: error("sc_get_pubkey failed");
458: goto send;
459: }
460: success = 1;
461:
462: tab = idtab_lookup(1);
1.64 markus 463: k->type = KEY_RSA1;
1.59 markus 464: if (lookup_private_key(k, NULL, 1) == NULL) {
465: if (tab->nentries == 0)
466: tab->identities = xmalloc(sizeof(Identity));
467: else
468: tab->identities = xrealloc(tab->identities,
469: (tab->nentries + 1) * sizeof(Identity));
470: n = key_new(KEY_RSA1);
471: BN_copy(n->rsa->n, k->rsa->n);
472: BN_copy(n->rsa->e, k->rsa->e);
473: RSA_set_method(n->rsa, sc_get_engine());
474: tab->identities[tab->nentries].key = n;
475: tab->identities[tab->nentries].comment =
476: xstrdup("rsa1 smartcard");
477: tab->nentries++;
478: }
1.64 markus 479: k->type = KEY_RSA;
1.59 markus 480: tab = idtab_lookup(2);
481: if (lookup_private_key(k, NULL, 2) == NULL) {
482: if (tab->nentries == 0)
483: tab->identities = xmalloc(sizeof(Identity));
484: else
485: tab->identities = xrealloc(tab->identities,
486: (tab->nentries + 1) * sizeof(Identity));
487: n = key_new(KEY_RSA);
488: BN_copy(n->rsa->n, k->rsa->n);
489: BN_copy(n->rsa->e, k->rsa->e);
490: RSA_set_method(n->rsa, sc_get_engine());
491: tab->identities[tab->nentries].key = n;
492: tab->identities[tab->nentries].comment =
493: xstrdup("rsa smartcard");
494: tab->nentries++;
495: }
496: key_free(k);
497: send:
498: buffer_put_int(&e->output, 1);
499: buffer_put_char(&e->output,
500: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
501: }
502:
503: static void
504: process_remove_smartcard_key(SocketEntry *e)
505: {
506: Key *k = NULL, *private;
507: int idx;
508: int success = 0;
509: int sc_reader_num = 0;
510:
511: sc_reader_num = buffer_get_int(&e->input);
512:
513: if ((k = sc_get_key(sc_reader_num)) == NULL) {
514: error("sc_get_pubkey failed");
515: } else {
1.63 markus 516: k->type = KEY_RSA1;
1.59 markus 517: private = lookup_private_key(k, &idx, 1);
518: if (private != NULL) {
519: Idtab *tab = idtab_lookup(1);
520: key_free(tab->identities[idx].key);
521: xfree(tab->identities[idx].comment);
522: if (idx != tab->nentries)
523: tab->identities[idx] = tab->identities[tab->nentries];
524: tab->nentries--;
525: success = 1;
526: }
1.63 markus 527: k->type = KEY_RSA;
1.59 markus 528: private = lookup_private_key(k, &idx, 2);
529: if (private != NULL) {
530: Idtab *tab = idtab_lookup(2);
531: key_free(tab->identities[idx].key);
532: xfree(tab->identities[idx].comment);
533: if (idx != tab->nentries)
534: tab->identities[idx] = tab->identities[tab->nentries];
535: tab->nentries--;
536: success = 1;
537: }
538: key_free(k);
539: }
540:
541: buffer_put_int(&e->output, 1);
542: buffer_put_char(&e->output,
543: success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
544: }
545: #endif
546:
1.33 markus 547: /* dispatch incoming messages */
548:
1.55 itojun 549: static void
1.2 provos 550: process_message(SocketEntry *e)
1.1 deraadt 551: {
1.45 markus 552: u_int msg_len;
553: u_int type;
554: u_char *cp;
1.21 markus 555: if (buffer_len(&e->input) < 5)
556: return; /* Incomplete message. */
1.45 markus 557: cp = (u_char *) buffer_ptr(&e->input);
1.21 markus 558: msg_len = GET_32BIT(cp);
559: if (msg_len > 256 * 1024) {
560: shutdown(e->fd, SHUT_RDWR);
561: close(e->fd);
562: e->type = AUTH_UNUSED;
563: return;
564: }
565: if (buffer_len(&e->input) < msg_len + 4)
566: return;
567: buffer_consume(&e->input, 4);
568: type = buffer_get_char(&e->input);
569:
1.59 markus 570: debug("type %d", type);
1.21 markus 571: switch (type) {
1.33 markus 572: /* ssh1 */
573: case SSH_AGENTC_RSA_CHALLENGE:
574: process_authentication_challenge1(e);
575: break;
1.21 markus 576: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33 markus 577: process_request_identities(e, 1);
1.21 markus 578: break;
579: case SSH_AGENTC_ADD_RSA_IDENTITY:
1.33 markus 580: process_add_identity(e, 1);
1.21 markus 581: break;
582: case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33 markus 583: process_remove_identity(e, 1);
1.21 markus 584: break;
585: case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.33 markus 586: process_remove_all_identities(e, 1);
587: break;
588: /* ssh2 */
589: case SSH2_AGENTC_SIGN_REQUEST:
590: process_sign_request2(e);
591: break;
592: case SSH2_AGENTC_REQUEST_IDENTITIES:
593: process_request_identities(e, 2);
594: break;
595: case SSH2_AGENTC_ADD_IDENTITY:
596: process_add_identity(e, 2);
597: break;
598: case SSH2_AGENTC_REMOVE_IDENTITY:
599: process_remove_identity(e, 2);
600: break;
601: case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
602: process_remove_all_identities(e, 2);
1.21 markus 603: break;
1.59 markus 604: #ifdef SMARTCARD
605: case SSH_AGENTC_ADD_SMARTCARD_KEY:
606: process_add_smartcard_key(e);
607: break;
608: case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
609: process_remove_smartcard_key(e);
610: break;
611: #endif
1.21 markus 612: default:
613: /* Unknown message. Respond with failure. */
614: error("Unknown message %d", type);
615: buffer_clear(&e->input);
616: buffer_put_int(&e->output, 1);
617: buffer_put_char(&e->output, SSH_AGENT_FAILURE);
618: break;
619: }
1.1 deraadt 620: }
621:
1.55 itojun 622: static void
1.2 provos 623: new_socket(int type, int fd)
1.1 deraadt 624: {
1.45 markus 625: u_int i, old_alloc;
1.21 markus 626: if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
627: error("fcntl O_NONBLOCK: %s", strerror(errno));
628:
629: if (fd > max_fd)
630: max_fd = fd;
631:
632: for (i = 0; i < sockets_alloc; i++)
633: if (sockets[i].type == AUTH_UNUSED) {
634: sockets[i].fd = fd;
635: sockets[i].type = type;
636: buffer_init(&sockets[i].input);
637: buffer_init(&sockets[i].output);
638: return;
639: }
640: old_alloc = sockets_alloc;
641: sockets_alloc += 10;
642: if (sockets)
643: sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
644: else
645: sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
646: for (i = old_alloc; i < sockets_alloc; i++)
647: sockets[i].type = AUTH_UNUSED;
648: sockets[old_alloc].type = type;
649: sockets[old_alloc].fd = fd;
650: buffer_init(&sockets[old_alloc].input);
651: buffer_init(&sockets[old_alloc].output);
1.1 deraadt 652: }
653:
1.55 itojun 654: static int
1.66 markus 655: prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
1.1 deraadt 656: {
1.46 markus 657: u_int i, sz;
658: int n = 0;
659:
660: for (i = 0; i < sockets_alloc; i++) {
1.21 markus 661: switch (sockets[i].type) {
662: case AUTH_SOCKET:
663: case AUTH_CONNECTION:
1.46 markus 664: n = MAX(n, sockets[i].fd);
1.21 markus 665: break;
666: case AUTH_UNUSED:
667: break;
668: default:
669: fatal("Unknown socket type %d", sockets[i].type);
670: break;
671: }
1.46 markus 672: }
673:
674: sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1.66 markus 675: if (*fdrp == NULL || sz > *nallocp) {
1.46 markus 676: if (*fdrp)
1.54 stevesk 677: xfree(*fdrp);
1.46 markus 678: if (*fdwp)
1.54 stevesk 679: xfree(*fdwp);
1.46 markus 680: *fdrp = xmalloc(sz);
681: *fdwp = xmalloc(sz);
1.66 markus 682: *nallocp = sz;
1.46 markus 683: }
1.66 markus 684: if (n < *fdl)
685: debug("XXX shrink: %d < %d", n, *fdl);
686: *fdl = n;
1.46 markus 687: memset(*fdrp, 0, sz);
688: memset(*fdwp, 0, sz);
689:
690: for (i = 0; i < sockets_alloc; i++) {
691: switch (sockets[i].type) {
692: case AUTH_SOCKET:
693: case AUTH_CONNECTION:
694: FD_SET(sockets[i].fd, *fdrp);
695: if (buffer_len(&sockets[i].output) > 0)
696: FD_SET(sockets[i].fd, *fdwp);
697: break;
698: default:
699: break;
700: }
701: }
702: return (1);
1.21 markus 703: }
704:
1.55 itojun 705: static void
1.21 markus 706: after_select(fd_set *readset, fd_set *writeset)
707: {
1.45 markus 708: u_int i;
1.21 markus 709: int len, sock;
1.26 markus 710: socklen_t slen;
1.21 markus 711: char buf[1024];
712: struct sockaddr_un sunaddr;
713:
714: for (i = 0; i < sockets_alloc; i++)
715: switch (sockets[i].type) {
716: case AUTH_UNUSED:
717: break;
718: case AUTH_SOCKET:
719: if (FD_ISSET(sockets[i].fd, readset)) {
1.26 markus 720: slen = sizeof(sunaddr);
1.46 markus 721: sock = accept(sockets[i].fd,
722: (struct sockaddr *) &sunaddr, &slen);
1.21 markus 723: if (sock < 0) {
724: perror("accept from AUTH_SOCKET");
725: break;
726: }
727: new_socket(AUTH_CONNECTION, sock);
728: }
729: break;
730: case AUTH_CONNECTION:
731: if (buffer_len(&sockets[i].output) > 0 &&
732: FD_ISSET(sockets[i].fd, writeset)) {
1.52 deraadt 733: do {
734: len = write(sockets[i].fd,
735: buffer_ptr(&sockets[i].output),
736: buffer_len(&sockets[i].output));
737: if (len == -1 && (errno == EAGAIN ||
738: errno == EINTR))
739: continue;
740: break;
741: } while (1);
1.21 markus 742: if (len <= 0) {
743: shutdown(sockets[i].fd, SHUT_RDWR);
744: close(sockets[i].fd);
745: sockets[i].type = AUTH_UNUSED;
1.30 djm 746: buffer_free(&sockets[i].input);
747: buffer_free(&sockets[i].output);
1.21 markus 748: break;
749: }
750: buffer_consume(&sockets[i].output, len);
751: }
752: if (FD_ISSET(sockets[i].fd, readset)) {
1.52 deraadt 753: do {
754: len = read(sockets[i].fd, buf, sizeof(buf));
755: if (len == -1 && (errno == EAGAIN ||
756: errno == EINTR))
757: continue;
758: break;
759: } while (1);
1.21 markus 760: if (len <= 0) {
761: shutdown(sockets[i].fd, SHUT_RDWR);
762: close(sockets[i].fd);
763: sockets[i].type = AUTH_UNUSED;
1.30 djm 764: buffer_free(&sockets[i].input);
765: buffer_free(&sockets[i].output);
1.21 markus 766: break;
767: }
768: buffer_append(&sockets[i].input, buf, len);
769: process_message(&sockets[i]);
770: }
771: break;
772: default:
773: fatal("Unknown type %d", sockets[i].type);
774: }
1.1 deraadt 775: }
776:
1.55 itojun 777: static void
1.15 markus 778: cleanup_socket(void)
779: {
1.48 deraadt 780: if (socket_name[0])
781: unlink(socket_name);
782: if (socket_dir[0])
783: rmdir(socket_dir);
1.10 markus 784: }
785:
1.55 itojun 786: static void
1.15 markus 787: cleanup_exit(int i)
788: {
1.21 markus 789: cleanup_socket();
790: exit(i);
1.15 markus 791: }
792:
1.55 itojun 793: static void
1.48 deraadt 794: cleanup_handler(int sig)
795: {
796: cleanup_socket();
797: _exit(2);
1.68 ! markus 798: }
! 799:
! 800: static void
! 801: check_parent_exists(int sig)
! 802: {
! 803: int save_errno = errno;
! 804:
! 805: if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
! 806: /* printf("Parent has died - Authentication agent exiting.\n"); */
! 807: cleanup_handler(sig); /* safe */
! 808: }
! 809: signal(SIGALRM, check_parent_exists);
! 810: alarm(10);
! 811: errno = save_errno;
1.48 deraadt 812: }
813:
1.55 itojun 814: static void
1.50 itojun 815: usage(void)
1.15 markus 816: {
1.21 markus 817: fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
1.65 stevesk 818: fprintf(stderr, "Usage: %s [-c | -s] [-k] [-d] [command [args...]]\n",
1.46 markus 819: __progname);
1.21 markus 820: exit(1);
1.15 markus 821: }
822:
1.2 provos 823: int
824: main(int ac, char **av)
1.1 deraadt 825: {
1.66 markus 826: int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
1.21 markus 827: struct sockaddr_un sunaddr;
1.41 markus 828: struct rlimit rlim;
1.21 markus 829: pid_t pid;
830: char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
1.43 markus 831: extern int optind;
1.46 markus 832: fd_set *readsetp = NULL, *writesetp = NULL;
1.53 markus 833:
834: SSLeay_add_all_algorithms();
1.21 markus 835:
1.57 markus 836: while ((ch = getopt(ac, av, "cdks")) != -1) {
1.21 markus 837: switch (ch) {
838: case 'c':
839: if (s_flag)
840: usage();
841: c_flag++;
842: break;
843: case 'k':
844: k_flag++;
845: break;
846: case 's':
847: if (c_flag)
848: usage();
849: s_flag++;
850: break;
1.57 markus 851: case 'd':
852: if (d_flag)
853: usage();
854: d_flag++;
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.57 markus 866: if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_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:
896: /* Create private directory for agent socket */
897: strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
898: if (mkdtemp(socket_dir) == NULL) {
899: perror("mkdtemp: private socket dir");
900: exit(1);
901: }
902: snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
1.46 markus 903: parent_pid);
1.21 markus 904:
1.23 markus 905: /*
906: * Create socket early so it will exist before command gets run from
907: * the parent.
908: */
1.21 markus 909: sock = socket(AF_UNIX, SOCK_STREAM, 0);
910: if (sock < 0) {
911: perror("socket");
912: cleanup_exit(1);
913: }
914: memset(&sunaddr, 0, sizeof(sunaddr));
915: sunaddr.sun_family = AF_UNIX;
916: strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
917: if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
918: perror("bind");
919: cleanup_exit(1);
920: }
921: if (listen(sock, 5) < 0) {
922: perror("listen");
923: cleanup_exit(1);
924: }
1.46 markus 925:
1.23 markus 926: /*
927: * Fork, and have the parent execute the command, if any, or present
928: * the socket data. The child continues as the authentication agent.
929: */
1.57 markus 930: if (d_flag) {
931: log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
932: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
933: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
934: SSH_AUTHSOCKET_ENV_NAME);
935: printf("echo Agent pid %d;\n", parent_pid);
936: goto skip;
937: }
1.21 markus 938: pid = fork();
939: if (pid == -1) {
940: perror("fork");
941: exit(1);
942: }
943: if (pid != 0) { /* Parent - execute the given command. */
944: close(sock);
945: snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
946: if (ac == 0) {
947: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
948: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1.46 markus 949: SSH_AUTHSOCKET_ENV_NAME);
1.21 markus 950: printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1.46 markus 951: SSH_AGENTPID_ENV_NAME);
1.21 markus 952: printf("echo Agent pid %d;\n", pid);
953: exit(0);
954: }
1.36 deraadt 955: if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
956: setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
957: perror("setenv");
958: exit(1);
959: }
1.21 markus 960: execvp(av[0], av);
961: perror(av[0]);
962: exit(1);
963: }
1.67 stevesk 964:
965: if (setsid() == -1) {
966: perror("setsid");
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) {
978: perror("setrlimit rlimit_core failed");
1.21 markus 979: cleanup_exit(1);
980: }
1.57 markus 981:
982: skip:
1.21 markus 983: if (atexit(cleanup_socket) < 0) {
984: perror("atexit");
985: cleanup_exit(1);
986: }
987: new_socket(AUTH_SOCKET, sock);
988: if (ac > 0) {
989: signal(SIGALRM, check_parent_exists);
990: alarm(10);
991: }
1.33 markus 992: idtab_init();
1.61 markus 993: if (!d_flag)
1.57 markus 994: signal(SIGINT, SIG_IGN);
1.61 markus 995: signal(SIGPIPE, SIG_IGN);
1.48 deraadt 996: signal(SIGHUP, cleanup_handler);
997: signal(SIGTERM, cleanup_handler);
1.66 markus 998: nalloc = 0;
999:
1.21 markus 1000: while (1) {
1.66 markus 1001: prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1.46 markus 1002: if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1.21 markus 1003: if (errno == EINTR)
1004: continue;
1005: exit(1);
1006: }
1.46 markus 1007: after_select(readsetp, writesetp);
1.15 markus 1008: }
1.21 markus 1009: /* NOTREACHED */
1.1 deraadt 1010: }