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