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