]> andersk Git - openssh.git/blame - ssh-agent.c
- jmc@cvs.openbsd.org 2006/02/19 20:12:25
[openssh.git] / ssh-agent.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * The authentication agent program.
2e73a022 6 *
bcbf86ec 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 *
a96070d4 13 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
bcbf86ec 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.
5260325f 34 */
8efc0c15 35
36#include "includes.h"
876faccd 37RCSID("$OpenBSD: ssh-agent.c,v 1.127 2006/02/08 14:31:30 stevesk Exp $");
38
9fd2a215 39#include "openbsd-compat/sys-queue.h"
876faccd 40#include <sys/resource.h>
a75f5360 41#ifdef HAVE_PATHS_H
6f61e0ec 42# include <paths.h>
a75f5360 43#endif
42f11eb2 44#include <openssl/evp.h>
45#include <openssl/md5.h>
8efc0c15 46
47#include "ssh.h"
48#include "rsa.h"
8efc0c15 49#include "buffer.h"
50#include "bufaux.h"
51#include "xmalloc.h"
8efc0c15 52#include "getput.h"
4c8722d9 53#include "key.h"
54#include "authfd.h"
188adeb2 55#include "compat.h"
42f11eb2 56#include "log.h"
c4087616 57#include "misc.h"
8efc0c15 58
2b5fe3b8 59#ifdef SMARTCARD
2b5fe3b8 60#include "scard.h"
dd2495cb 61#endif
2b5fe3b8 62
e7f6070d 63#if defined(HAVE_SYS_PRCTL_H)
64#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
65#endif
66
17a3011c 67typedef enum {
68 AUTH_UNUSED,
69 AUTH_SOCKET,
70 AUTH_CONNECTION
71} sock_type;
72
5260325f 73typedef struct {
74 int fd;
17a3011c 75 sock_type type;
5260325f 76 Buffer input;
77 Buffer output;
b9dd5ca1 78 Buffer request;
8efc0c15 79} SocketEntry;
80
1e3b8b07 81u_int sockets_alloc = 0;
8efc0c15 82SocketEntry *sockets = NULL;
83
95f0a918 84typedef struct identity {
85 TAILQ_ENTRY(identity) next;
2e73a022 86 Key *key;
5260325f 87 char *comment;
264572cc 88 u_int death;
c4087616 89 u_int confirm;
8efc0c15 90} Identity;
91
2e73a022 92typedef struct {
93 int nentries;
95f0a918 94 TAILQ_HEAD(idqueue, identity) idlist;
2e73a022 95} Idtab;
96
97/* private key table, one per protocol version */
98Idtab idtable[3];
8efc0c15 99
100int max_fd = 0;
101
102/* pid of shell == parent of agent */
9da5c3c9 103pid_t parent_pid = -1;
8efc0c15 104
105/* pathname and directory for AUTH_SOCKET */
106char socket_name[1024];
107char socket_dir[1024];
108
3db7f994 109/* locking */
110int locked = 0;
111char *lock_passwd = NULL;
112
5260325f 113extern char *__progname;
5260325f 114
494b267f 115/* Default lifetime (0 == forever) */
116static int lifetime = 0;
117
c1a4eef1 118static void
119close_socket(SocketEntry *e)
120{
c1a4eef1 121 close(e->fd);
122 e->fd = -1;
123 e->type = AUTH_UNUSED;
124 buffer_free(&e->input);
125 buffer_free(&e->output);
126 buffer_free(&e->request);
127}
128
396c147e 129static void
2e73a022 130idtab_init(void)
131{
132 int i;
e424e241 133
6aacefa7 134 for (i = 0; i <=2; i++) {
95f0a918 135 TAILQ_INIT(&idtable[i].idlist);
2e73a022 136 idtable[i].nentries = 0;
137 }
138}
139
140/* return private key table for requested protocol version */
396c147e 141static Idtab *
2e73a022 142idtab_lookup(int version)
143{
144 if (version < 1 || version > 2)
145 fatal("internal error, bad protocol version %d", version);
146 return &idtable[version];
147}
148
264572cc 149static void
150free_identity(Identity *id)
151{
152 key_free(id->key);
153 xfree(id->comment);
154 xfree(id);
155}
156
2e73a022 157/* return matching private key for given public key */
95f0a918 158static Identity *
159lookup_identity(Key *key, int version)
2e73a022 160{
95f0a918 161 Identity *id;
162
2e73a022 163 Idtab *tab = idtab_lookup(version);
95f0a918 164 TAILQ_FOREACH(id, &tab->idlist, next) {
165 if (key_equal(key, id->key))
166 return (id);
2e73a022 167 }
95f0a918 168 return (NULL);
169}
170
c4087616 171/* Check confirmation of keysign request */
172static int
173confirm_key(Identity *id)
174{
7a9c7a0b 175 char *p;
c4087616 176 int ret = -1;
177
178 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
7a9c7a0b 179 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
180 id->comment, p))
181 ret = 0;
c4087616 182 xfree(p);
7a9c7a0b 183
c4087616 184 return (ret);
185}
186
2e73a022 187/* send list of supported public keys to 'client' */
396c147e 188static void
2e73a022 189process_request_identities(SocketEntry *e, int version)
8efc0c15 190{
2e73a022 191 Idtab *tab = idtab_lookup(version);
95f0a918 192 Identity *id;
e424e241 193 Buffer msg;
5260325f 194
195 buffer_init(&msg);
2e73a022 196 buffer_put_char(&msg, (version == 1) ?
197 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
198 buffer_put_int(&msg, tab->nentries);
95f0a918 199 TAILQ_FOREACH(id, &tab->idlist, next) {
fa08c86b 200 if (id->key->type == KEY_RSA1) {
2e73a022 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 {
1e3b8b07 205 u_char *blob;
206 u_int blen;
fa08c86b 207 key_to_blob(id->key, &blob, &blen);
2e73a022 208 buffer_put_string(&msg, blob, blen);
209 xfree(blob);
210 }
211 buffer_put_cstring(&msg, id->comment);
5260325f 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);
8efc0c15 216}
217
2e73a022 218/* ssh1 only */
396c147e 219static void
2e73a022 220process_authentication_challenge1(SocketEntry *e)
8efc0c15 221{
e424e241 222 u_char buf[32], mdbuf[16], session_id[16];
223 u_int response_type;
2e73a022 224 BIGNUM *challenge;
e424e241 225 Identity *id;
2e73a022 226 int i, len;
5260325f 227 Buffer msg;
228 MD5_CTX md;
e424e241 229 Key *key;
5260325f 230
231 buffer_init(&msg);
fa08c86b 232 key = key_new(KEY_RSA1);
b775c6f2 233 if ((challenge = BN_new()) == NULL)
234 fatal("process_authentication_challenge1: BN_new failed");
5260325f 235
1169c3df 236 (void) buffer_get_int(&e->request); /* ignored */
b9dd5ca1 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);
5260325f 240
2e73a022 241 /* Only protocol 1.1 is supported */
b9dd5ca1 242 if (buffer_len(&e->request) == 0)
2e73a022 243 goto failure;
b9dd5ca1 244 buffer_get(&e->request, session_id, 16);
245 response_type = buffer_get_int(&e->request);
2e73a022 246 if (response_type != 1)
247 goto failure;
248
95f0a918 249 id = lookup_identity(key, 1);
c4087616 250 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
95f0a918 251 Key *private = id->key;
2e73a022 252 /* Decrypt the challenge using the private key. */
46aa2d1f 253 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
254 goto failure;
2e73a022 255
256 /* The response is MD5 of decrypted challenge plus session id. */
257 len = BN_num_bytes(challenge);
258 if (len <= 0 || len > 32) {
bbe88b6d 259 logit("process_authentication_challenge: bad challenge length %d", len);
2e73a022 260 goto failure;
5260325f 261 }
2e73a022 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 }
275
276failure:
277 /* Unknown identity or protocol error. Send failure. */
5260325f 278 buffer_put_char(&msg, SSH_AGENT_FAILURE);
279send:
2e73a022 280 buffer_put_int(&e->output, buffer_len(&msg));
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 */
396c147e 288static void
2e73a022 289process_sign_request2(SocketEntry *e)
290{
1e3b8b07 291 u_char *blob, *data, *signature = NULL;
292 u_int blen, dlen, slen = 0;
e424e241 293 extern int datafellows;
294 int ok = -1, flags;
2e73a022 295 Buffer msg;
e424e241 296 Key *key;
2e73a022 297
298 datafellows = 0;
227e8e86 299
b9dd5ca1 300 blob = buffer_get_string(&e->request, &blen);
301 data = buffer_get_string(&e->request, &dlen);
188adeb2 302
b9dd5ca1 303 flags = buffer_get_int(&e->request);
188adeb2 304 if (flags & SSH_AGENT_OLD_SIGNATURE)
305 datafellows = SSH_BUG_SIGBLOB;
2e73a022 306
fa08c86b 307 key = key_from_blob(blob, blen);
2e73a022 308 if (key != NULL) {
95f0a918 309 Identity *id = lookup_identity(key, 2);
c4087616 310 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
95f0a918 311 ok = key_sign(id->key, &signature, &slen, data, dlen);
2e73a022 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 }
5260325f 321 buffer_put_int(&e->output, buffer_len(&msg));
322 buffer_append(&e->output, buffer_ptr(&msg),
2e73a022 323 buffer_len(&msg));
5260325f 324 buffer_free(&msg);
2e73a022 325 xfree(data);
326 xfree(blob);
327 if (signature != NULL)
328 xfree(signature);
8efc0c15 329}
330
2e73a022 331/* shared */
396c147e 332static void
2e73a022 333process_remove_identity(SocketEntry *e, int version)
8efc0c15 334{
e424e241 335 u_int blen, bits;
336 int success = 0;
95f0a918 337 Key *key = NULL;
1e3b8b07 338 u_char *blob;
2e73a022 339
6aacefa7 340 switch (version) {
2e73a022 341 case 1:
fa08c86b 342 key = key_new(KEY_RSA1);
b9dd5ca1 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);
2e73a022 346
347 if (bits != key_size(key))
bbe88b6d 348 logit("Warning: identity keysize mismatch: actual %u, announced %u",
42f11eb2 349 key_size(key), bits);
2e73a022 350 break;
351 case 2:
b9dd5ca1 352 blob = buffer_get_string(&e->request, &blen);
fa08c86b 353 key = key_from_blob(blob, blen);
2e73a022 354 xfree(blob);
355 break;
356 }
357 if (key != NULL) {
95f0a918 358 Identity *id = lookup_identity(key, version);
359 if (id != NULL) {
aa3378df 360 /*
361 * We have this key. Free the old key. Since we
7b9b0103 362 * don't want to leave empty slots in the middle of
d343d900 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.
aa3378df 366 */
2e73a022 367 Idtab *tab = idtab_lookup(version);
fa08c86b 368 if (tab->nentries < 1)
369 fatal("process_remove_identity: "
370 "internal error: tab->nentries %d",
371 tab->nentries);
95f0a918 372 TAILQ_REMOVE(&tab->idlist, id, next);
373 free_identity(id);
2e73a022 374 tab->nentries--;
375 success = 1;
5260325f 376 }
2e73a022 377 key_free(key);
378 }
8efc0c15 379 buffer_put_int(&e->output, 1);
2e73a022 380 buffer_put_char(&e->output,
381 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
8efc0c15 382}
383
396c147e 384static void
2e73a022 385process_remove_all_identities(SocketEntry *e, int version)
8efc0c15 386{
2e73a022 387 Idtab *tab = idtab_lookup(version);
95f0a918 388 Identity *id;
8efc0c15 389
5260325f 390 /* Loop over all identities and clear the keys. */
95f0a918 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);
5260325f 395 }
8efc0c15 396
5260325f 397 /* Mark that there are no identities. */
2e73a022 398 tab->nentries = 0;
8efc0c15 399
400 /* Send success. */
401 buffer_put_int(&e->output, 1);
402 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
5260325f 403}
404
264572cc 405static void
406reaper(void)
407{
e424e241 408 u_int now = time(NULL);
264572cc 409 Identity *id, *nxt;
410 int version;
e424e241 411 Idtab *tab;
264572cc 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
396c147e 426static void
fa08c86b 427process_add_identity(SocketEntry *e, int version)
428{
2e73a022 429 Idtab *tab = idtab_lookup(version);
c4087616 430 int type, success = 0, death = 0, confirm = 0;
e424e241 431 char *type_name, *comment;
432 Key *k = NULL;
5260325f 433
2e73a022 434 switch (version) {
435 case 1:
fa08c86b 436 k = key_new_private(KEY_RSA1);
1169c3df 437 (void) buffer_get_int(&e->request); /* ignored */
b9dd5ca1 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);
2e73a022 442
443 /* SSH and SSL have p and q swapped */
b9dd5ca1 444 buffer_get_bignum(&e->request, k->rsa->q); /* p */
445 buffer_get_bignum(&e->request, k->rsa->p); /* q */
2e73a022 446
447 /* Generate additional parameters */
2b63e803 448 rsa_generate_additional_parameters(k->rsa);
2e73a022 449 break;
450 case 2:
b9dd5ca1 451 type_name = buffer_get_string(&e->request, NULL);
42f11eb2 452 type = key_type_from_name(type_name);
fa08c86b 453 xfree(type_name);
6aacefa7 454 switch (type) {
fa08c86b 455 case KEY_DSA:
456 k = key_new_private(type);
b9dd5ca1 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);
fa08c86b 462 break;
463 case KEY_RSA:
464 k = key_new_private(type);
b9dd5ca1 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);
fa08c86b 471
472 /* Generate additional parameters */
2b63e803 473 rsa_generate_additional_parameters(k->rsa);
fa08c86b 474 break;
475 default:
b9dd5ca1 476 buffer_clear(&e->request);
2e73a022 477 goto send;
5260325f 478 }
2e73a022 479 break;
480 }
cb0e1d67 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);
488 goto send;
489 }
490 break;
491 }
b9dd5ca1 492 comment = buffer_get_string(&e->request, NULL);
2e73a022 493 if (k == NULL) {
494 xfree(comment);
495 goto send;
496 }
497 success = 1;
ee900f87 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;
c4087616 503 case SSH_AGENT_CONSTRAIN_CONFIRM:
504 confirm = 1;
505 break;
ee900f87 506 default:
507 break;
508 }
509 }
494b267f 510 if (lifetime && !death)
511 death = time(NULL) + lifetime;
95f0a918 512 if (lookup_identity(k, version) == NULL) {
513 Identity *id = xmalloc(sizeof(Identity));
514 id->key = k;
515 id->comment = comment;
ee900f87 516 id->death = death;
c4087616 517 id->confirm = confirm;
95f0a918 518 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
2e73a022 519 /* Increment the number of identities. */
520 tab->nentries++;
521 } else {
522 key_free(k);
523 xfree(comment);
524 }
525send:
5260325f 526 buffer_put_int(&e->output, 1);
2e73a022 527 buffer_put_char(&e->output,
528 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
8efc0c15 529}
530
3db7f994 531/* XXX todo: encrypt sensitive data with passphrase */
532static void
533process_lock_agent(SocketEntry *e, int lock)
534{
3db7f994 535 int success = 0;
e424e241 536 char *passwd;
3db7f994 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);
7203d6bb 552
3db7f994 553 buffer_put_int(&e->output, 1);
554 buffer_put_char(&e->output,
555 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
3db7f994 556}
557
558static void
559no_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}
2b5fe3b8 572
573#ifdef SMARTCARD
574static void
575process_add_smartcard_key (SocketEntry *e)
576{
76139bd8 577 char *sc_reader_id = NULL, *pin;
ca719034 578 int i, version, success = 0, death = 0, confirm = 0;
e424e241 579 Key **keys, *k;
580 Identity *id;
581 Idtab *tab;
184eed6a 582
b9dd5ca1 583 sc_reader_id = buffer_get_string(&e->request, NULL);
584 pin = buffer_get_string(&e->request, NULL);
ca719034 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
0659cace 601 keys = sc_get_keys(sc_reader_id, pin);
9ff6f66f 602 xfree(sc_reader_id);
76139bd8 603 xfree(pin);
2b5fe3b8 604
0659cace 605 if (keys == NULL || keys[0] == NULL) {
606 error("sc_get_keys failed");
2b5fe3b8 607 goto send;
608 }
0659cace 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;
244e796f 616 id->comment = sc_get_key_label(k);
ca719034 617 id->death = death;
618 id->confirm = confirm;
0659cace 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;
2b5fe3b8 626 }
0659cace 627 xfree(keys);
2b5fe3b8 628send:
629 buffer_put_int(&e->output, 1);
630 buffer_put_char(&e->output,
631 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
632}
633
634static void
635process_remove_smartcard_key(SocketEntry *e)
636{
76139bd8 637 char *sc_reader_id = NULL, *pin;
0659cace 638 int i, version, success = 0;
e424e241 639 Key **keys, *k = NULL;
640 Identity *id;
641 Idtab *tab;
2b5fe3b8 642
b9dd5ca1 643 sc_reader_id = buffer_get_string(&e->request, NULL);
644 pin = buffer_get_string(&e->request, NULL);
0659cace 645 keys = sc_get_keys(sc_reader_id, pin);
9ff6f66f 646 xfree(sc_reader_id);
76139bd8 647 xfree(pin);
2b5fe3b8 648
0659cace 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);
875ec275 658 TAILQ_REMOVE(&tab->idlist, id, next);
2b5fe3b8 659 tab->nentries--;
95f0a918 660 free_identity(id);
2b5fe3b8 661 success = 1;
662 }
663 key_free(k);
0659cace 664 keys[i] = NULL;
2b5fe3b8 665 }
0659cace 666 xfree(keys);
667send:
2b5fe3b8 668 buffer_put_int(&e->output, 1);
669 buffer_put_char(&e->output,
670 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
671}
3e984472 672#endif /* SMARTCARD */
2b5fe3b8 673
2e73a022 674/* dispatch incoming messages */
675
396c147e 676static void
8efc0c15 677process_message(SocketEntry *e)
678{
e424e241 679 u_int msg_len, type;
1e3b8b07 680 u_char *cp;
264572cc 681
682 /* kill dead keys */
683 reaper();
684
5260325f 685 if (buffer_len(&e->input) < 5)
686 return; /* Incomplete message. */
20905a8e 687 cp = buffer_ptr(&e->input);
5260325f 688 msg_len = GET_32BIT(cp);
689 if (msg_len > 256 * 1024) {
c1a4eef1 690 close_socket(e);
5260325f 691 return;
692 }
693 if (buffer_len(&e->input) < msg_len + 4)
694 return;
b9dd5ca1 695
696 /* move the current input to e->request */
5260325f 697 buffer_consume(&e->input, 4);
b9dd5ca1 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);
5260325f 702
3db7f994 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
2b5fe3b8 720 debug("type %d", type);
5260325f 721 switch (type) {
3db7f994 722 case SSH_AGENTC_LOCK:
723 case SSH_AGENTC_UNLOCK:
724 process_lock_agent(e, type == SSH_AGENTC_LOCK);
725 break;
2e73a022 726 /* ssh1 */
5260325f 727 case SSH_AGENTC_RSA_CHALLENGE:
2e73a022 728 process_authentication_challenge1(e);
729 break;
730 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
731 process_request_identities(e, 1);
5260325f 732 break;
733 case SSH_AGENTC_ADD_RSA_IDENTITY:
ee900f87 734 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
2e73a022 735 process_add_identity(e, 1);
5260325f 736 break;
737 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
2e73a022 738 process_remove_identity(e, 1);
5260325f 739 break;
740 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
2e73a022 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:
ee900f87 751 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
2e73a022 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);
5260325f 759 break;
2b5fe3b8 760#ifdef SMARTCARD
761 case SSH_AGENTC_ADD_SMARTCARD_KEY:
ca719034 762 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
2b5fe3b8 763 process_add_smartcard_key(e);
184eed6a 764 break;
2b5fe3b8 765 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
766 process_remove_smartcard_key(e);
184eed6a 767 break;
3e984472 768#endif /* SMARTCARD */
5260325f 769 default:
770 /* Unknown message. Respond with failure. */
771 error("Unknown message %d", type);
b9dd5ca1 772 buffer_clear(&e->request);
5260325f 773 buffer_put_int(&e->output, 1);
774 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
775 break;
776 }
8efc0c15 777}
778
396c147e 779static void
17a3011c 780new_socket(sock_type type, int fd)
8efc0c15 781{
c46e584f 782 u_int i, old_alloc, new_alloc;
e424e241 783
170694d7 784 set_nonblock(fd);
5260325f 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;
5260325f 792 buffer_init(&sockets[i].input);
793 buffer_init(&sockets[i].output);
b9dd5ca1 794 buffer_init(&sockets[i].request);
c46e584f 795 sockets[i].type = type;
5260325f 796 return;
797 }
798 old_alloc = sockets_alloc;
c46e584f 799 new_alloc = sockets_alloc + 10;
5260325f 800 if (sockets)
c46e584f 801 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
5260325f 802 else
c46e584f 803 sockets = xmalloc(new_alloc * sizeof(sockets[0]));
804 for (i = old_alloc; i < new_alloc; i++)
5260325f 805 sockets[i].type = AUTH_UNUSED;
c46e584f 806 sockets_alloc = new_alloc;
5260325f 807 sockets[old_alloc].fd = fd;
808 buffer_init(&sockets[old_alloc].input);
809 buffer_init(&sockets[old_alloc].output);
b9dd5ca1 810 buffer_init(&sockets[old_alloc].request);
c46e584f 811 sockets[old_alloc].type = type;
8efc0c15 812}
813
396c147e 814static int
9a995072 815prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
8efc0c15 816{
42f11eb2 817 u_int i, sz;
818 int n = 0;
819
820 for (i = 0; i < sockets_alloc; i++) {
5260325f 821 switch (sockets[i].type) {
822 case AUTH_SOCKET:
823 case AUTH_CONNECTION:
42f11eb2 824 n = MAX(n, sockets[i].fd);
5260325f 825 break;
826 case AUTH_UNUSED:
827 break;
828 default:
829 fatal("Unknown socket type %d", sockets[i].type);
830 break;
831 }
42f11eb2 832 }
833
834 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
e364646f 835 if (*fdrp == NULL || sz > *nallocp) {
42f11eb2 836 if (*fdrp)
894c5fa6 837 xfree(*fdrp);
42f11eb2 838 if (*fdwp)
894c5fa6 839 xfree(*fdwp);
42f11eb2 840 *fdrp = xmalloc(sz);
841 *fdwp = xmalloc(sz);
e364646f 842 *nallocp = sz;
42f11eb2 843 }
e364646f 844 if (n < *fdl)
845 debug("XXX shrink: %d < %d", n, *fdl);
846 *fdl = n;
42f11eb2 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);
8efc0c15 863}
864
396c147e 865static void
5260325f 866after_select(fd_set *readset, fd_set *writeset)
8efc0c15 867{
e424e241 868 struct sockaddr_un sunaddr;
610cd5c6 869 socklen_t slen;
5260325f 870 char buf[1024];
e424e241 871 int len, sock;
872 u_int i;
87f4111f 873 uid_t euid;
874 gid_t egid;
5260325f 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)) {
610cd5c6 882 slen = sizeof(sunaddr);
42f11eb2 883 sock = accept(sockets[i].fd,
884 (struct sockaddr *) &sunaddr, &slen);
5260325f 885 if (sock < 0) {
fccfbe3b 886 error("accept from AUTH_SOCKET: %s",
887 strerror(errno));
5260325f 888 break;
889 }
87f4111f 890 if (getpeereid(sock, &euid, &egid) < 0) {
891 error("getpeereid %d failed: %s",
892 sock, strerror(errno));
893 close(sock);
894 break;
895 }
e139021d 896 if ((euid != 0) && (getuid() != euid)) {
87f4111f 897 error("uid mismatch: "
792e7d2d 898 "peer euid %u != uid %u",
899 (u_int) euid, (u_int) getuid());
87f4111f 900 close(sock);
901 break;
902 }
5260325f 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)) {
bbc62e59 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);
5260325f 918 if (len <= 0) {
c1a4eef1 919 close_socket(&sockets[i]);
5260325f 920 break;
921 }
922 buffer_consume(&sockets[i].output, len);
923 }
924 if (FD_ISSET(sockets[i].fd, readset)) {
bbc62e59 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);
5260325f 932 if (len <= 0) {
c1a4eef1 933 close_socket(&sockets[i]);
5260325f 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 }
8efc0c15 943}
944
396c147e 945static void
cd25664d 946cleanup_socket(void)
dae3fa13 947{
2f4b2e38 948 if (socket_name[0])
949 unlink(socket_name);
950 if (socket_dir[0])
951 rmdir(socket_dir);
8efc0c15 952}
953
2362db19 954void
dae3fa13 955cleanup_exit(int i)
956{
cd25664d 957 cleanup_socket();
958 _exit(i);
dae3fa13 959}
960
396c147e 961static void
2f4b2e38 962cleanup_handler(int sig)
963{
cd25664d 964 cleanup_socket();
2f4b2e38 965 _exit(2);
966}
967
7efa8482 968static void
969check_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 }
8d9bb5dd 977 mysignal(SIGALRM, check_parent_exists);
7efa8482 978 alarm(10);
979 errno = save_errno;
980}
981
396c147e 982static void
cc8aca8a 983usage(void)
dae3fa13 984{
33e766d2 985 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
0e3c1f95 986 __progname);
33e766d2 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");
3e7efb37 992 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
494b267f 993 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
5260325f 994 exit(1);
dae3fa13 995}
996
8efc0c15 997int
998main(int ac, char **av)
999{
c4087616 1000 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
9a995072 1001 int sock, fd, ch;
1002 u_int nalloc;
e424e241 1003 char *shell, *format, *pidstr, *agentsocket = NULL;
1004 fd_set *readsetp = NULL, *writesetp = NULL;
5260325f 1005 struct sockaddr_un sunaddr;
b03bd394 1006#ifdef HAVE_SETRLIMIT
0b6fbf03 1007 struct rlimit rlim;
c7ccfd39 1008#endif
c7ccfd39 1009 int prev_mask;
c04f75f1 1010 extern int optind;
ef3912be 1011 extern char *optarg;
e424e241 1012 pid_t pid;
1013 char pidstrbuf[1 + 3 * sizeof pid];
fcb975eb 1014
fd6168c1 1015 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1016 sanitise_stdfd();
1017
954640a4 1018 /* drop */
1019 setegid(getgid());
1020 setgid(getgid());
1021
e7f6070d 1022#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1023 /* Disable ptrace on Linux without sgid bit */
1024 prctl(PR_SET_DUMPABLE, 0);
1025#endif
1026
457fc0c6 1027 SSLeay_add_all_algorithms();
1028
fda04d7d 1029 __progname = ssh_get_progname(av[0]);
264dce47 1030 init_rng();
e339aa53 1031 seed_rng();
2b87da3b 1032
494b267f 1033 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
5260325f 1034 switch (ch) {
1035 case 'c':
1036 if (s_flag)
1037 usage();
1038 c_flag++;
1039 break;
1040 case 'k':
1041 k_flag++;
1042 break;
1043 case 's':
1044 if (c_flag)
1045 usage();
1046 s_flag++;
1047 break;
ffdb5d70 1048 case 'd':
1049 if (d_flag)
1050 usage();
1051 d_flag++;
1052 break;
3e7efb37 1053 case 'a':
1054 agentsocket = optarg;
1055 break;
494b267f 1056 case 't':
1057 if ((lifetime = convtime(optarg)) == -1) {
1058 fprintf(stderr, "Invalid lifetime\n");
1059 usage();
1060 }
1061 break;
5260325f 1062 default:
1063 usage();
1064 }
dae3fa13 1065 }
5260325f 1066 ac -= optind;
1067 av += optind;
1068
ffdb5d70 1069 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
5260325f 1070 usage();
1071
b745a2f2 1072 if (ac == 0 && !c_flag && !s_flag) {
5260325f 1073 shell = getenv("SHELL");
1074 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1075 c_flag = 1;
dae3fa13 1076 }
5260325f 1077 if (k_flag) {
1078 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1079 if (pidstr == NULL) {
1080 fprintf(stderr, "%s not set, cannot kill agent\n",
42f11eb2 1081 SSH_AGENTPID_ENV_NAME);
5260325f 1082 exit(1);
1083 }
1084 pid = atoi(pidstr);
42f11eb2 1085 if (pid < 1) {
5260325f 1086 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
42f11eb2 1087 SSH_AGENTPID_ENV_NAME, pidstr);
5260325f 1088 exit(1);
1089 }
1090 if (kill(pid, SIGTERM) == -1) {
1091 perror("kill");
1092 exit(1);
1093 }
1094 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1095 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1096 printf(format, SSH_AGENTPID_ENV_NAME);
c457707e 1097 printf("echo Agent pid %ld killed;\n", (long)pid);
5260325f 1098 exit(0);
dae3fa13 1099 }
5260325f 1100 parent_pid = getpid();
1101
3e7efb37 1102 if (agentsocket == NULL) {
1103 /* Create private directory for agent socket */
c88de854 1104 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
3e7efb37 1105 if (mkdtemp(socket_dir) == NULL) {
1106 perror("mkdtemp: private socket dir");
1107 exit(1);
1108 }
c457707e 1109 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1110 (long)parent_pid);
3e7efb37 1111 } else {
1112 /* Try to use specified agent socket */
1113 socket_dir[0] = '\0';
1114 strlcpy(socket_name, agentsocket, sizeof socket_name);
5260325f 1115 }
5260325f 1116
aa3378df 1117 /*
1118 * Create socket early so it will exist before command gets run from
1119 * the parent.
1120 */
5260325f 1121 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1122 if (sock < 0) {
1123 perror("socket");
8e8d8c82 1124 *socket_name = '\0'; /* Don't unlink any existing file */
5260325f 1125 cleanup_exit(1);
1126 }
1127 memset(&sunaddr, 0, sizeof(sunaddr));
1128 sunaddr.sun_family = AF_UNIX;
1129 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
c7ccfd39 1130 prev_mask = umask(0177);
5260325f 1131 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1132 perror("bind");
8e8d8c82 1133 *socket_name = '\0'; /* Don't unlink any existing file */
c7ccfd39 1134 umask(prev_mask);
5260325f 1135 cleanup_exit(1);
1136 }
c7ccfd39 1137 umask(prev_mask);
fce39749 1138 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
5260325f 1139 perror("listen");
1140 cleanup_exit(1);
dae3fa13 1141 }
42f11eb2 1142
aa3378df 1143 /*
1144 * Fork, and have the parent execute the command, if any, or present
1145 * the socket data. The child continues as the authentication agent.
1146 */
ffdb5d70 1147 if (d_flag) {
1148 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1149 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1150 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1151 SSH_AUTHSOCKET_ENV_NAME);
c457707e 1152 printf("echo Agent pid %ld;\n", (long)parent_pid);
ffdb5d70 1153 goto skip;
1154 }
5260325f 1155 pid = fork();
1156 if (pid == -1) {
1157 perror("fork");
fccfbe3b 1158 cleanup_exit(1);
dae3fa13 1159 }
5260325f 1160 if (pid != 0) { /* Parent - execute the given command. */
1161 close(sock);
c457707e 1162 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
5260325f 1163 if (ac == 0) {
1164 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1165 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
42f11eb2 1166 SSH_AUTHSOCKET_ENV_NAME);
5260325f 1167 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
42f11eb2 1168 SSH_AGENTPID_ENV_NAME);
c457707e 1169 printf("echo Agent pid %ld;\n", (long)pid);
5260325f 1170 exit(0);
1171 }
bcbf86ec 1172 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1173 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1174 perror("setenv");
1175 exit(1);
1176 }
5260325f 1177 execvp(av[0], av);
1178 perror(av[0]);
1179 exit(1);
1180 }
fccfbe3b 1181 /* child */
1182 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
ec9f3450 1183
1184 if (setsid() == -1) {
fccfbe3b 1185 error("setsid: %s", strerror(errno));
ec9f3450 1186 cleanup_exit(1);
1187 }
1188
1189 (void)chdir("/");
c4087616 1190 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1191 /* XXX might close listen socket */
1192 (void)dup2(fd, STDIN_FILENO);
1193 (void)dup2(fd, STDOUT_FILENO);
1194 (void)dup2(fd, STDERR_FILENO);
1195 if (fd > 2)
1196 close(fd);
1197 }
dae3fa13 1198
b03bd394 1199#ifdef HAVE_SETRLIMIT
0b6fbf03 1200 /* deny core dumps, since memory contains unencrypted private keys */
1201 rlim.rlim_cur = rlim.rlim_max = 0;
1202 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
fccfbe3b 1203 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
0b6fbf03 1204 cleanup_exit(1);
1205 }
b03bd394 1206#endif
ffdb5d70 1207
1208skip:
5260325f 1209 new_socket(AUTH_SOCKET, sock);
1210 if (ac > 0) {
8d9bb5dd 1211 mysignal(SIGALRM, check_parent_exists);
5260325f 1212 alarm(10);
1213 }
2e73a022 1214 idtab_init();
1ee482c5 1215 if (!d_flag)
ffdb5d70 1216 signal(SIGINT, SIG_IGN);
1ee482c5 1217 signal(SIGPIPE, SIG_IGN);
2f4b2e38 1218 signal(SIGHUP, cleanup_handler);
1219 signal(SIGTERM, cleanup_handler);
e364646f 1220 nalloc = 0;
1221
5260325f 1222 while (1) {
e364646f 1223 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
42f11eb2 1224 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
5260325f 1225 if (errno == EINTR)
1226 continue;
fccfbe3b 1227 fatal("select: %s", strerror(errno));
5260325f 1228 }
42f11eb2 1229 after_select(readsetp, writesetp);
8efc0c15 1230 }
5260325f 1231 /* NOTREACHED */
8efc0c15 1232}
This page took 3.83741 seconds and 5 git commands to generate.