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