]> andersk Git - openssh.git/blame - ssh-agent.c
- stevesk@cvs.openbsd.org 2006/07/22 19:08:54
[openssh.git] / ssh-agent.c
CommitLineData
b0f6943a 1/* $OpenBSD: ssh-agent.c,v 1.145 2006/07/22 19:08:54 stevesk Exp $ */
8efc0c15 2/*
5260325f 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
5260325f 6 * The authentication agent program.
2e73a022 7 *
bcbf86ec 8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
a96070d4 14 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
bcbf86ec 15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5260325f 35 */
8efc0c15 36
37#include "includes.h"
cebb4c24 38
39#include <sys/types.h>
3e598f1a 40#include <sys/stat.h>
5b04a8bf 41#include <sys/socket.h>
cebb4c24 42#ifdef HAVE_SYS_UN_H
43# include <sys/un.h>
44#endif
9fd2a215 45#include "openbsd-compat/sys-queue.h"
876faccd 46#include <sys/resource.h>
5b04a8bf 47
028094f4 48#include <errno.h>
d3221cca 49#include <fcntl.h>
a75f5360 50#ifdef HAVE_PATHS_H
6f61e0ec 51# include <paths.h>
a75f5360 52#endif
ada68823 53#include <signal.h>
b0f6943a 54#include <time.h>
5188ba17 55#include <unistd.h>
ada68823 56
42f11eb2 57#include <openssl/evp.h>
58#include <openssl/md5.h>
8efc0c15 59
60#include "ssh.h"
61#include "rsa.h"
8efc0c15 62#include "buffer.h"
63#include "bufaux.h"
64#include "xmalloc.h"
4c8722d9 65#include "key.h"
66#include "authfd.h"
188adeb2 67#include "compat.h"
42f11eb2 68#include "log.h"
c4087616 69#include "misc.h"
8efc0c15 70
2b5fe3b8 71#ifdef SMARTCARD
2b5fe3b8 72#include "scard.h"
dd2495cb 73#endif
2b5fe3b8 74
e7f6070d 75#if defined(HAVE_SYS_PRCTL_H)
76#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
77#endif
78
17a3011c 79typedef enum {
80 AUTH_UNUSED,
81 AUTH_SOCKET,
82 AUTH_CONNECTION
83} sock_type;
84
5260325f 85typedef struct {
86 int fd;
17a3011c 87 sock_type type;
5260325f 88 Buffer input;
89 Buffer output;
b9dd5ca1 90 Buffer request;
8efc0c15 91} SocketEntry;
92
1e3b8b07 93u_int sockets_alloc = 0;
8efc0c15 94SocketEntry *sockets = NULL;
95
95f0a918 96typedef struct identity {
97 TAILQ_ENTRY(identity) next;
2e73a022 98 Key *key;
5260325f 99 char *comment;
264572cc 100 u_int death;
c4087616 101 u_int confirm;
8efc0c15 102} Identity;
103
2e73a022 104typedef struct {
105 int nentries;
95f0a918 106 TAILQ_HEAD(idqueue, identity) idlist;
2e73a022 107} Idtab;
108
109/* private key table, one per protocol version */
110Idtab idtable[3];
8efc0c15 111
112int max_fd = 0;
113
114/* pid of shell == parent of agent */
9da5c3c9 115pid_t parent_pid = -1;
8efc0c15 116
117/* pathname and directory for AUTH_SOCKET */
52e3daed 118char socket_name[MAXPATHLEN];
119char socket_dir[MAXPATHLEN];
8efc0c15 120
3db7f994 121/* locking */
122int locked = 0;
123char *lock_passwd = NULL;
124
5260325f 125extern char *__progname;
5260325f 126
494b267f 127/* Default lifetime (0 == forever) */
128static int lifetime = 0;
129
c1a4eef1 130static void
131close_socket(SocketEntry *e)
132{
c1a4eef1 133 close(e->fd);
134 e->fd = -1;
135 e->type = AUTH_UNUSED;
136 buffer_free(&e->input);
137 buffer_free(&e->output);
138 buffer_free(&e->request);
139}
140
396c147e 141static void
2e73a022 142idtab_init(void)
143{
144 int i;
e424e241 145
6aacefa7 146 for (i = 0; i <=2; i++) {
95f0a918 147 TAILQ_INIT(&idtable[i].idlist);
2e73a022 148 idtable[i].nentries = 0;
149 }
150}
151
152/* return private key table for requested protocol version */
396c147e 153static Idtab *
2e73a022 154idtab_lookup(int version)
155{
156 if (version < 1 || version > 2)
157 fatal("internal error, bad protocol version %d", version);
158 return &idtable[version];
159}
160
264572cc 161static void
162free_identity(Identity *id)
163{
164 key_free(id->key);
165 xfree(id->comment);
166 xfree(id);
167}
168
2e73a022 169/* return matching private key for given public key */
95f0a918 170static Identity *
171lookup_identity(Key *key, int version)
2e73a022 172{
95f0a918 173 Identity *id;
174
2e73a022 175 Idtab *tab = idtab_lookup(version);
95f0a918 176 TAILQ_FOREACH(id, &tab->idlist, next) {
177 if (key_equal(key, id->key))
178 return (id);
2e73a022 179 }
95f0a918 180 return (NULL);
181}
182
c4087616 183/* Check confirmation of keysign request */
184static int
185confirm_key(Identity *id)
186{
7a9c7a0b 187 char *p;
c4087616 188 int ret = -1;
189
190 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
7a9c7a0b 191 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
192 id->comment, p))
193 ret = 0;
c4087616 194 xfree(p);
7a9c7a0b 195
c4087616 196 return (ret);
197}
198
2e73a022 199/* send list of supported public keys to 'client' */
396c147e 200static void
2e73a022 201process_request_identities(SocketEntry *e, int version)
8efc0c15 202{
2e73a022 203 Idtab *tab = idtab_lookup(version);
95f0a918 204 Identity *id;
e424e241 205 Buffer msg;
5260325f 206
207 buffer_init(&msg);
2e73a022 208 buffer_put_char(&msg, (version == 1) ?
209 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
210 buffer_put_int(&msg, tab->nentries);
95f0a918 211 TAILQ_FOREACH(id, &tab->idlist, next) {
fa08c86b 212 if (id->key->type == KEY_RSA1) {
2e73a022 213 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
214 buffer_put_bignum(&msg, id->key->rsa->e);
215 buffer_put_bignum(&msg, id->key->rsa->n);
216 } else {
1e3b8b07 217 u_char *blob;
218 u_int blen;
fa08c86b 219 key_to_blob(id->key, &blob, &blen);
2e73a022 220 buffer_put_string(&msg, blob, blen);
221 xfree(blob);
222 }
223 buffer_put_cstring(&msg, id->comment);
5260325f 224 }
225 buffer_put_int(&e->output, buffer_len(&msg));
226 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
227 buffer_free(&msg);
8efc0c15 228}
229
2e73a022 230/* ssh1 only */
396c147e 231static void
2e73a022 232process_authentication_challenge1(SocketEntry *e)
8efc0c15 233{
e424e241 234 u_char buf[32], mdbuf[16], session_id[16];
235 u_int response_type;
2e73a022 236 BIGNUM *challenge;
e424e241 237 Identity *id;
2e73a022 238 int i, len;
5260325f 239 Buffer msg;
240 MD5_CTX md;
e424e241 241 Key *key;
5260325f 242
243 buffer_init(&msg);
fa08c86b 244 key = key_new(KEY_RSA1);
b775c6f2 245 if ((challenge = BN_new()) == NULL)
246 fatal("process_authentication_challenge1: BN_new failed");
5260325f 247
1169c3df 248 (void) buffer_get_int(&e->request); /* ignored */
b9dd5ca1 249 buffer_get_bignum(&e->request, key->rsa->e);
250 buffer_get_bignum(&e->request, key->rsa->n);
251 buffer_get_bignum(&e->request, challenge);
5260325f 252
2e73a022 253 /* Only protocol 1.1 is supported */
b9dd5ca1 254 if (buffer_len(&e->request) == 0)
2e73a022 255 goto failure;
b9dd5ca1 256 buffer_get(&e->request, session_id, 16);
257 response_type = buffer_get_int(&e->request);
2e73a022 258 if (response_type != 1)
259 goto failure;
260
95f0a918 261 id = lookup_identity(key, 1);
c4087616 262 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
95f0a918 263 Key *private = id->key;
2e73a022 264 /* Decrypt the challenge using the private key. */
46aa2d1f 265 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
266 goto failure;
2e73a022 267
268 /* The response is MD5 of decrypted challenge plus session id. */
269 len = BN_num_bytes(challenge);
270 if (len <= 0 || len > 32) {
bbe88b6d 271 logit("process_authentication_challenge: bad challenge length %d", len);
2e73a022 272 goto failure;
5260325f 273 }
2e73a022 274 memset(buf, 0, 32);
275 BN_bn2bin(challenge, buf + 32 - len);
276 MD5_Init(&md);
277 MD5_Update(&md, buf, 32);
278 MD5_Update(&md, session_id, 16);
279 MD5_Final(mdbuf, &md);
280
281 /* Send the response. */
282 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
283 for (i = 0; i < 16; i++)
284 buffer_put_char(&msg, mdbuf[i]);
285 goto send;
286 }
287
288failure:
289 /* Unknown identity or protocol error. Send failure. */
5260325f 290 buffer_put_char(&msg, SSH_AGENT_FAILURE);
291send:
2e73a022 292 buffer_put_int(&e->output, buffer_len(&msg));
293 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
294 key_free(key);
295 BN_clear_free(challenge);
296 buffer_free(&msg);
297}
298
299/* ssh2 only */
396c147e 300static void
2e73a022 301process_sign_request2(SocketEntry *e)
302{
1e3b8b07 303 u_char *blob, *data, *signature = NULL;
304 u_int blen, dlen, slen = 0;
e424e241 305 extern int datafellows;
306 int ok = -1, flags;
2e73a022 307 Buffer msg;
e424e241 308 Key *key;
2e73a022 309
310 datafellows = 0;
227e8e86 311
b9dd5ca1 312 blob = buffer_get_string(&e->request, &blen);
313 data = buffer_get_string(&e->request, &dlen);
188adeb2 314
b9dd5ca1 315 flags = buffer_get_int(&e->request);
188adeb2 316 if (flags & SSH_AGENT_OLD_SIGNATURE)
317 datafellows = SSH_BUG_SIGBLOB;
2e73a022 318
fa08c86b 319 key = key_from_blob(blob, blen);
2e73a022 320 if (key != NULL) {
95f0a918 321 Identity *id = lookup_identity(key, 2);
c4087616 322 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
95f0a918 323 ok = key_sign(id->key, &signature, &slen, data, dlen);
2b8dc5e3 324 key_free(key);
2e73a022 325 }
2e73a022 326 buffer_init(&msg);
327 if (ok == 0) {
328 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
329 buffer_put_string(&msg, signature, slen);
330 } else {
331 buffer_put_char(&msg, SSH_AGENT_FAILURE);
332 }
5260325f 333 buffer_put_int(&e->output, buffer_len(&msg));
334 buffer_append(&e->output, buffer_ptr(&msg),
2e73a022 335 buffer_len(&msg));
5260325f 336 buffer_free(&msg);
2e73a022 337 xfree(data);
338 xfree(blob);
339 if (signature != NULL)
340 xfree(signature);
8efc0c15 341}
342
2e73a022 343/* shared */
396c147e 344static void
2e73a022 345process_remove_identity(SocketEntry *e, int version)
8efc0c15 346{
e424e241 347 u_int blen, bits;
348 int success = 0;
95f0a918 349 Key *key = NULL;
1e3b8b07 350 u_char *blob;
2e73a022 351
6aacefa7 352 switch (version) {
2e73a022 353 case 1:
fa08c86b 354 key = key_new(KEY_RSA1);
b9dd5ca1 355 bits = buffer_get_int(&e->request);
356 buffer_get_bignum(&e->request, key->rsa->e);
357 buffer_get_bignum(&e->request, key->rsa->n);
2e73a022 358
359 if (bits != key_size(key))
bbe88b6d 360 logit("Warning: identity keysize mismatch: actual %u, announced %u",
42f11eb2 361 key_size(key), bits);
2e73a022 362 break;
363 case 2:
b9dd5ca1 364 blob = buffer_get_string(&e->request, &blen);
fa08c86b 365 key = key_from_blob(blob, blen);
2e73a022 366 xfree(blob);
367 break;
368 }
369 if (key != NULL) {
95f0a918 370 Identity *id = lookup_identity(key, version);
371 if (id != NULL) {
aa3378df 372 /*
373 * We have this key. Free the old key. Since we
7b9b0103 374 * don't want to leave empty slots in the middle of
d343d900 375 * the array, we actually free the key there and move
376 * all the entries between the empty slot and the end
377 * of the array.
aa3378df 378 */
2e73a022 379 Idtab *tab = idtab_lookup(version);
fa08c86b 380 if (tab->nentries < 1)
381 fatal("process_remove_identity: "
382 "internal error: tab->nentries %d",
383 tab->nentries);
95f0a918 384 TAILQ_REMOVE(&tab->idlist, id, next);
385 free_identity(id);
2e73a022 386 tab->nentries--;
387 success = 1;
5260325f 388 }
2e73a022 389 key_free(key);
390 }
8efc0c15 391 buffer_put_int(&e->output, 1);
2e73a022 392 buffer_put_char(&e->output,
393 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
8efc0c15 394}
395
396c147e 396static void
2e73a022 397process_remove_all_identities(SocketEntry *e, int version)
8efc0c15 398{
2e73a022 399 Idtab *tab = idtab_lookup(version);
95f0a918 400 Identity *id;
8efc0c15 401
5260325f 402 /* Loop over all identities and clear the keys. */
95f0a918 403 for (id = TAILQ_FIRST(&tab->idlist); id;
404 id = TAILQ_FIRST(&tab->idlist)) {
405 TAILQ_REMOVE(&tab->idlist, id, next);
406 free_identity(id);
5260325f 407 }
8efc0c15 408
5260325f 409 /* Mark that there are no identities. */
2e73a022 410 tab->nentries = 0;
8efc0c15 411
412 /* Send success. */
413 buffer_put_int(&e->output, 1);
414 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
5260325f 415}
416
264572cc 417static void
418reaper(void)
419{
e424e241 420 u_int now = time(NULL);
264572cc 421 Identity *id, *nxt;
422 int version;
e424e241 423 Idtab *tab;
264572cc 424
425 for (version = 1; version < 3; version++) {
426 tab = idtab_lookup(version);
427 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
428 nxt = TAILQ_NEXT(id, next);
429 if (id->death != 0 && now >= id->death) {
430 TAILQ_REMOVE(&tab->idlist, id, next);
431 free_identity(id);
432 tab->nentries--;
433 }
434 }
435 }
436}
437
396c147e 438static void
fa08c86b 439process_add_identity(SocketEntry *e, int version)
440{
2e73a022 441 Idtab *tab = idtab_lookup(version);
c4087616 442 int type, success = 0, death = 0, confirm = 0;
e424e241 443 char *type_name, *comment;
444 Key *k = NULL;
5260325f 445
2e73a022 446 switch (version) {
447 case 1:
fa08c86b 448 k = key_new_private(KEY_RSA1);
1169c3df 449 (void) buffer_get_int(&e->request); /* ignored */
b9dd5ca1 450 buffer_get_bignum(&e->request, k->rsa->n);
451 buffer_get_bignum(&e->request, k->rsa->e);
452 buffer_get_bignum(&e->request, k->rsa->d);
453 buffer_get_bignum(&e->request, k->rsa->iqmp);
2e73a022 454
455 /* SSH and SSL have p and q swapped */
b9dd5ca1 456 buffer_get_bignum(&e->request, k->rsa->q); /* p */
457 buffer_get_bignum(&e->request, k->rsa->p); /* q */
2e73a022 458
459 /* Generate additional parameters */
2b63e803 460 rsa_generate_additional_parameters(k->rsa);
2e73a022 461 break;
462 case 2:
b9dd5ca1 463 type_name = buffer_get_string(&e->request, NULL);
42f11eb2 464 type = key_type_from_name(type_name);
fa08c86b 465 xfree(type_name);
6aacefa7 466 switch (type) {
fa08c86b 467 case KEY_DSA:
468 k = key_new_private(type);
b9dd5ca1 469 buffer_get_bignum2(&e->request, k->dsa->p);
470 buffer_get_bignum2(&e->request, k->dsa->q);
471 buffer_get_bignum2(&e->request, k->dsa->g);
472 buffer_get_bignum2(&e->request, k->dsa->pub_key);
473 buffer_get_bignum2(&e->request, k->dsa->priv_key);
fa08c86b 474 break;
475 case KEY_RSA:
476 k = key_new_private(type);
b9dd5ca1 477 buffer_get_bignum2(&e->request, k->rsa->n);
478 buffer_get_bignum2(&e->request, k->rsa->e);
479 buffer_get_bignum2(&e->request, k->rsa->d);
480 buffer_get_bignum2(&e->request, k->rsa->iqmp);
481 buffer_get_bignum2(&e->request, k->rsa->p);
482 buffer_get_bignum2(&e->request, k->rsa->q);
fa08c86b 483
484 /* Generate additional parameters */
2b63e803 485 rsa_generate_additional_parameters(k->rsa);
fa08c86b 486 break;
487 default:
b9dd5ca1 488 buffer_clear(&e->request);
2e73a022 489 goto send;
5260325f 490 }
2e73a022 491 break;
492 }
cb0e1d67 493 /* enable blinding */
494 switch (k->type) {
495 case KEY_RSA:
496 case KEY_RSA1:
497 if (RSA_blinding_on(k->rsa, NULL) != 1) {
498 error("process_add_identity: RSA_blinding_on failed");
499 key_free(k);
500 goto send;
501 }
502 break;
503 }
b9dd5ca1 504 comment = buffer_get_string(&e->request, NULL);
2e73a022 505 if (k == NULL) {
506 xfree(comment);
507 goto send;
508 }
509 success = 1;
ee900f87 510 while (buffer_len(&e->request)) {
511 switch (buffer_get_char(&e->request)) {
512 case SSH_AGENT_CONSTRAIN_LIFETIME:
513 death = time(NULL) + buffer_get_int(&e->request);
514 break;
c4087616 515 case SSH_AGENT_CONSTRAIN_CONFIRM:
516 confirm = 1;
517 break;
ee900f87 518 default:
519 break;
520 }
521 }
494b267f 522 if (lifetime && !death)
523 death = time(NULL) + lifetime;
95f0a918 524 if (lookup_identity(k, version) == NULL) {
525 Identity *id = xmalloc(sizeof(Identity));
526 id->key = k;
527 id->comment = comment;
ee900f87 528 id->death = death;
c4087616 529 id->confirm = confirm;
95f0a918 530 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
2e73a022 531 /* Increment the number of identities. */
532 tab->nentries++;
533 } else {
534 key_free(k);
535 xfree(comment);
536 }
537send:
5260325f 538 buffer_put_int(&e->output, 1);
2e73a022 539 buffer_put_char(&e->output,
540 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
8efc0c15 541}
542
3db7f994 543/* XXX todo: encrypt sensitive data with passphrase */
544static void
545process_lock_agent(SocketEntry *e, int lock)
546{
3db7f994 547 int success = 0;
e424e241 548 char *passwd;
3db7f994 549
550 passwd = buffer_get_string(&e->request, NULL);
551 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
552 locked = 0;
553 memset(lock_passwd, 0, strlen(lock_passwd));
554 xfree(lock_passwd);
555 lock_passwd = NULL;
556 success = 1;
557 } else if (!locked && lock) {
558 locked = 1;
559 lock_passwd = xstrdup(passwd);
560 success = 1;
561 }
562 memset(passwd, 0, strlen(passwd));
563 xfree(passwd);
7203d6bb 564
3db7f994 565 buffer_put_int(&e->output, 1);
566 buffer_put_char(&e->output,
567 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
3db7f994 568}
569
570static void
571no_identities(SocketEntry *e, u_int type)
572{
573 Buffer msg;
574
575 buffer_init(&msg);
576 buffer_put_char(&msg,
577 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
578 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
579 buffer_put_int(&msg, 0);
580 buffer_put_int(&e->output, buffer_len(&msg));
581 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
582 buffer_free(&msg);
583}
2b5fe3b8 584
585#ifdef SMARTCARD
586static void
587process_add_smartcard_key (SocketEntry *e)
588{
76139bd8 589 char *sc_reader_id = NULL, *pin;
ca719034 590 int i, version, success = 0, death = 0, confirm = 0;
e424e241 591 Key **keys, *k;
592 Identity *id;
593 Idtab *tab;
184eed6a 594
b9dd5ca1 595 sc_reader_id = buffer_get_string(&e->request, NULL);
596 pin = buffer_get_string(&e->request, NULL);
ca719034 597
598 while (buffer_len(&e->request)) {
599 switch (buffer_get_char(&e->request)) {
600 case SSH_AGENT_CONSTRAIN_LIFETIME:
601 death = time(NULL) + buffer_get_int(&e->request);
602 break;
603 case SSH_AGENT_CONSTRAIN_CONFIRM:
604 confirm = 1;
605 break;
606 default:
607 break;
608 }
609 }
610 if (lifetime && !death)
611 death = time(NULL) + lifetime;
612
0659cace 613 keys = sc_get_keys(sc_reader_id, pin);
9ff6f66f 614 xfree(sc_reader_id);
76139bd8 615 xfree(pin);
2b5fe3b8 616
0659cace 617 if (keys == NULL || keys[0] == NULL) {
618 error("sc_get_keys failed");
2b5fe3b8 619 goto send;
620 }
0659cace 621 for (i = 0; keys[i] != NULL; i++) {
622 k = keys[i];
623 version = k->type == KEY_RSA1 ? 1 : 2;
624 tab = idtab_lookup(version);
625 if (lookup_identity(k, version) == NULL) {
626 id = xmalloc(sizeof(Identity));
627 id->key = k;
244e796f 628 id->comment = sc_get_key_label(k);
ca719034 629 id->death = death;
630 id->confirm = confirm;
0659cace 631 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
632 tab->nentries++;
633 success = 1;
634 } else {
635 key_free(k);
636 }
637 keys[i] = NULL;
2b5fe3b8 638 }
0659cace 639 xfree(keys);
2b5fe3b8 640send:
641 buffer_put_int(&e->output, 1);
642 buffer_put_char(&e->output,
643 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
644}
645
646static void
647process_remove_smartcard_key(SocketEntry *e)
648{
76139bd8 649 char *sc_reader_id = NULL, *pin;
0659cace 650 int i, version, success = 0;
e424e241 651 Key **keys, *k = NULL;
652 Identity *id;
653 Idtab *tab;
2b5fe3b8 654
b9dd5ca1 655 sc_reader_id = buffer_get_string(&e->request, NULL);
656 pin = buffer_get_string(&e->request, NULL);
0659cace 657 keys = sc_get_keys(sc_reader_id, pin);
9ff6f66f 658 xfree(sc_reader_id);
76139bd8 659 xfree(pin);
2b5fe3b8 660
0659cace 661 if (keys == NULL || keys[0] == NULL) {
662 error("sc_get_keys failed");
663 goto send;
664 }
665 for (i = 0; keys[i] != NULL; i++) {
666 k = keys[i];
667 version = k->type == KEY_RSA1 ? 1 : 2;
668 if ((id = lookup_identity(k, version)) != NULL) {
669 tab = idtab_lookup(version);
875ec275 670 TAILQ_REMOVE(&tab->idlist, id, next);
2b5fe3b8 671 tab->nentries--;
95f0a918 672 free_identity(id);
2b5fe3b8 673 success = 1;
674 }
675 key_free(k);
0659cace 676 keys[i] = NULL;
2b5fe3b8 677 }
0659cace 678 xfree(keys);
679send:
2b5fe3b8 680 buffer_put_int(&e->output, 1);
681 buffer_put_char(&e->output,
682 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
683}
3e984472 684#endif /* SMARTCARD */
2b5fe3b8 685
2e73a022 686/* dispatch incoming messages */
687
396c147e 688static void
8efc0c15 689process_message(SocketEntry *e)
690{
e424e241 691 u_int msg_len, type;
1e3b8b07 692 u_char *cp;
264572cc 693
694 /* kill dead keys */
695 reaper();
696
5260325f 697 if (buffer_len(&e->input) < 5)
698 return; /* Incomplete message. */
20905a8e 699 cp = buffer_ptr(&e->input);
51e7a012 700 msg_len = get_u32(cp);
5260325f 701 if (msg_len > 256 * 1024) {
c1a4eef1 702 close_socket(e);
5260325f 703 return;
704 }
705 if (buffer_len(&e->input) < msg_len + 4)
706 return;
b9dd5ca1 707
708 /* move the current input to e->request */
5260325f 709 buffer_consume(&e->input, 4);
b9dd5ca1 710 buffer_clear(&e->request);
711 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
712 buffer_consume(&e->input, msg_len);
713 type = buffer_get_char(&e->request);
5260325f 714
3db7f994 715 /* check wheter agent is locked */
716 if (locked && type != SSH_AGENTC_UNLOCK) {
717 buffer_clear(&e->request);
718 switch (type) {
719 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
720 case SSH2_AGENTC_REQUEST_IDENTITIES:
721 /* send empty lists */
722 no_identities(e, type);
723 break;
724 default:
725 /* send a fail message for all other request types */
726 buffer_put_int(&e->output, 1);
727 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
728 }
729 return;
730 }
731
2b5fe3b8 732 debug("type %d", type);
5260325f 733 switch (type) {
3db7f994 734 case SSH_AGENTC_LOCK:
735 case SSH_AGENTC_UNLOCK:
736 process_lock_agent(e, type == SSH_AGENTC_LOCK);
737 break;
2e73a022 738 /* ssh1 */
5260325f 739 case SSH_AGENTC_RSA_CHALLENGE:
2e73a022 740 process_authentication_challenge1(e);
741 break;
742 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
743 process_request_identities(e, 1);
5260325f 744 break;
745 case SSH_AGENTC_ADD_RSA_IDENTITY:
ee900f87 746 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
2e73a022 747 process_add_identity(e, 1);
5260325f 748 break;
749 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
2e73a022 750 process_remove_identity(e, 1);
5260325f 751 break;
752 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
2e73a022 753 process_remove_all_identities(e, 1);
754 break;
755 /* ssh2 */
756 case SSH2_AGENTC_SIGN_REQUEST:
757 process_sign_request2(e);
758 break;
759 case SSH2_AGENTC_REQUEST_IDENTITIES:
760 process_request_identities(e, 2);
761 break;
762 case SSH2_AGENTC_ADD_IDENTITY:
ee900f87 763 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
2e73a022 764 process_add_identity(e, 2);
765 break;
766 case SSH2_AGENTC_REMOVE_IDENTITY:
767 process_remove_identity(e, 2);
768 break;
769 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
770 process_remove_all_identities(e, 2);
5260325f 771 break;
2b5fe3b8 772#ifdef SMARTCARD
773 case SSH_AGENTC_ADD_SMARTCARD_KEY:
ca719034 774 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
2b5fe3b8 775 process_add_smartcard_key(e);
184eed6a 776 break;
2b5fe3b8 777 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
778 process_remove_smartcard_key(e);
184eed6a 779 break;
3e984472 780#endif /* SMARTCARD */
5260325f 781 default:
782 /* Unknown message. Respond with failure. */
783 error("Unknown message %d", type);
b9dd5ca1 784 buffer_clear(&e->request);
5260325f 785 buffer_put_int(&e->output, 1);
786 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
787 break;
788 }
8efc0c15 789}
790
396c147e 791static void
17a3011c 792new_socket(sock_type type, int fd)
8efc0c15 793{
c46e584f 794 u_int i, old_alloc, new_alloc;
e424e241 795
170694d7 796 set_nonblock(fd);
5260325f 797
798 if (fd > max_fd)
799 max_fd = fd;
800
801 for (i = 0; i < sockets_alloc; i++)
802 if (sockets[i].type == AUTH_UNUSED) {
803 sockets[i].fd = fd;
5260325f 804 buffer_init(&sockets[i].input);
805 buffer_init(&sockets[i].output);
b9dd5ca1 806 buffer_init(&sockets[i].request);
c46e584f 807 sockets[i].type = type;
5260325f 808 return;
809 }
810 old_alloc = sockets_alloc;
c46e584f 811 new_alloc = sockets_alloc + 10;
c5d10563 812 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
c46e584f 813 for (i = old_alloc; i < new_alloc; i++)
5260325f 814 sockets[i].type = AUTH_UNUSED;
c46e584f 815 sockets_alloc = new_alloc;
5260325f 816 sockets[old_alloc].fd = fd;
817 buffer_init(&sockets[old_alloc].input);
818 buffer_init(&sockets[old_alloc].output);
b9dd5ca1 819 buffer_init(&sockets[old_alloc].request);
c46e584f 820 sockets[old_alloc].type = type;
8efc0c15 821}
822
396c147e 823static int
9a995072 824prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
8efc0c15 825{
42f11eb2 826 u_int i, sz;
827 int n = 0;
828
829 for (i = 0; i < sockets_alloc; i++) {
5260325f 830 switch (sockets[i].type) {
831 case AUTH_SOCKET:
832 case AUTH_CONNECTION:
42f11eb2 833 n = MAX(n, sockets[i].fd);
5260325f 834 break;
835 case AUTH_UNUSED:
836 break;
837 default:
838 fatal("Unknown socket type %d", sockets[i].type);
839 break;
840 }
42f11eb2 841 }
842
843 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
e364646f 844 if (*fdrp == NULL || sz > *nallocp) {
42f11eb2 845 if (*fdrp)
894c5fa6 846 xfree(*fdrp);
42f11eb2 847 if (*fdwp)
894c5fa6 848 xfree(*fdwp);
42f11eb2 849 *fdrp = xmalloc(sz);
850 *fdwp = xmalloc(sz);
e364646f 851 *nallocp = sz;
42f11eb2 852 }
e364646f 853 if (n < *fdl)
854 debug("XXX shrink: %d < %d", n, *fdl);
855 *fdl = n;
42f11eb2 856 memset(*fdrp, 0, sz);
857 memset(*fdwp, 0, sz);
858
859 for (i = 0; i < sockets_alloc; i++) {
860 switch (sockets[i].type) {
861 case AUTH_SOCKET:
862 case AUTH_CONNECTION:
863 FD_SET(sockets[i].fd, *fdrp);
864 if (buffer_len(&sockets[i].output) > 0)
865 FD_SET(sockets[i].fd, *fdwp);
866 break;
867 default:
868 break;
869 }
870 }
871 return (1);
8efc0c15 872}
873
396c147e 874static void
5260325f 875after_select(fd_set *readset, fd_set *writeset)
8efc0c15 876{
e424e241 877 struct sockaddr_un sunaddr;
610cd5c6 878 socklen_t slen;
5260325f 879 char buf[1024];
e424e241 880 int len, sock;
881 u_int i;
87f4111f 882 uid_t euid;
883 gid_t egid;
5260325f 884
885 for (i = 0; i < sockets_alloc; i++)
886 switch (sockets[i].type) {
887 case AUTH_UNUSED:
888 break;
889 case AUTH_SOCKET:
890 if (FD_ISSET(sockets[i].fd, readset)) {
610cd5c6 891 slen = sizeof(sunaddr);
42f11eb2 892 sock = accept(sockets[i].fd,
32596c7b 893 (struct sockaddr *)&sunaddr, &slen);
5260325f 894 if (sock < 0) {
fccfbe3b 895 error("accept from AUTH_SOCKET: %s",
896 strerror(errno));
5260325f 897 break;
898 }
87f4111f 899 if (getpeereid(sock, &euid, &egid) < 0) {
900 error("getpeereid %d failed: %s",
901 sock, strerror(errno));
902 close(sock);
903 break;
904 }
e139021d 905 if ((euid != 0) && (getuid() != euid)) {
87f4111f 906 error("uid mismatch: "
792e7d2d 907 "peer euid %u != uid %u",
908 (u_int) euid, (u_int) getuid());
87f4111f 909 close(sock);
910 break;
911 }
5260325f 912 new_socket(AUTH_CONNECTION, sock);
913 }
914 break;
915 case AUTH_CONNECTION:
916 if (buffer_len(&sockets[i].output) > 0 &&
917 FD_ISSET(sockets[i].fd, writeset)) {
bbc62e59 918 do {
919 len = write(sockets[i].fd,
920 buffer_ptr(&sockets[i].output),
921 buffer_len(&sockets[i].output));
922 if (len == -1 && (errno == EAGAIN ||
923 errno == EINTR))
924 continue;
925 break;
926 } while (1);
5260325f 927 if (len <= 0) {
c1a4eef1 928 close_socket(&sockets[i]);
5260325f 929 break;
930 }
931 buffer_consume(&sockets[i].output, len);
932 }
933 if (FD_ISSET(sockets[i].fd, readset)) {
bbc62e59 934 do {
935 len = read(sockets[i].fd, buf, sizeof(buf));
936 if (len == -1 && (errno == EAGAIN ||
937 errno == EINTR))
938 continue;
939 break;
940 } while (1);
5260325f 941 if (len <= 0) {
c1a4eef1 942 close_socket(&sockets[i]);
5260325f 943 break;
944 }
945 buffer_append(&sockets[i].input, buf, len);
946 process_message(&sockets[i]);
947 }
948 break;
949 default:
950 fatal("Unknown type %d", sockets[i].type);
951 }
8efc0c15 952}
953
396c147e 954static void
cd25664d 955cleanup_socket(void)
dae3fa13 956{
2f4b2e38 957 if (socket_name[0])
958 unlink(socket_name);
959 if (socket_dir[0])
960 rmdir(socket_dir);
8efc0c15 961}
962
2362db19 963void
dae3fa13 964cleanup_exit(int i)
965{
cd25664d 966 cleanup_socket();
967 _exit(i);
dae3fa13 968}
969
8d0b0353 970/*ARGSUSED*/
396c147e 971static void
2f4b2e38 972cleanup_handler(int sig)
973{
cd25664d 974 cleanup_socket();
2f4b2e38 975 _exit(2);
976}
977
8d0b0353 978/*ARGSUSED*/
7efa8482 979static void
980check_parent_exists(int sig)
981{
982 int save_errno = errno;
983
984 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
985 /* printf("Parent has died - Authentication agent exiting.\n"); */
986 cleanup_handler(sig); /* safe */
987 }
8d9bb5dd 988 mysignal(SIGALRM, check_parent_exists);
7efa8482 989 alarm(10);
990 errno = save_errno;
991}
992
396c147e 993static void
cc8aca8a 994usage(void)
dae3fa13 995{
33e766d2 996 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
0e3c1f95 997 __progname);
33e766d2 998 fprintf(stderr, "Options:\n");
999 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1000 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1001 fprintf(stderr, " -k Kill the current agent.\n");
1002 fprintf(stderr, " -d Debug mode.\n");
3e7efb37 1003 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
494b267f 1004 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
5260325f 1005 exit(1);
dae3fa13 1006}
1007
8efc0c15 1008int
1009main(int ac, char **av)
1010{
c4087616 1011 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
9a995072 1012 int sock, fd, ch;
1013 u_int nalloc;
e424e241 1014 char *shell, *format, *pidstr, *agentsocket = NULL;
1015 fd_set *readsetp = NULL, *writesetp = NULL;
5260325f 1016 struct sockaddr_un sunaddr;
b03bd394 1017#ifdef HAVE_SETRLIMIT
0b6fbf03 1018 struct rlimit rlim;
c7ccfd39 1019#endif
c7ccfd39 1020 int prev_mask;
c04f75f1 1021 extern int optind;
ef3912be 1022 extern char *optarg;
e424e241 1023 pid_t pid;
1024 char pidstrbuf[1 + 3 * sizeof pid];
fcb975eb 1025
fd6168c1 1026 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1027 sanitise_stdfd();
1028
954640a4 1029 /* drop */
1030 setegid(getgid());
1031 setgid(getgid());
1032
e7f6070d 1033#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1034 /* Disable ptrace on Linux without sgid bit */
1035 prctl(PR_SET_DUMPABLE, 0);
1036#endif
1037
457fc0c6 1038 SSLeay_add_all_algorithms();
1039
fda04d7d 1040 __progname = ssh_get_progname(av[0]);
264dce47 1041 init_rng();
e339aa53 1042 seed_rng();
2b87da3b 1043
494b267f 1044 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
5260325f 1045 switch (ch) {
1046 case 'c':
1047 if (s_flag)
1048 usage();
1049 c_flag++;
1050 break;
1051 case 'k':
1052 k_flag++;
1053 break;
1054 case 's':
1055 if (c_flag)
1056 usage();
1057 s_flag++;
1058 break;
ffdb5d70 1059 case 'd':
1060 if (d_flag)
1061 usage();
1062 d_flag++;
1063 break;
3e7efb37 1064 case 'a':
1065 agentsocket = optarg;
1066 break;
494b267f 1067 case 't':
1068 if ((lifetime = convtime(optarg)) == -1) {
1069 fprintf(stderr, "Invalid lifetime\n");
1070 usage();
1071 }
1072 break;
5260325f 1073 default:
1074 usage();
1075 }
dae3fa13 1076 }
5260325f 1077 ac -= optind;
1078 av += optind;
1079
ffdb5d70 1080 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
5260325f 1081 usage();
1082
b745a2f2 1083 if (ac == 0 && !c_flag && !s_flag) {
5260325f 1084 shell = getenv("SHELL");
cfaa5405 1085 if (shell != NULL &&
1086 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
5260325f 1087 c_flag = 1;
dae3fa13 1088 }
5260325f 1089 if (k_flag) {
cfaa5405 1090 const char *errstr = NULL;
1091
5260325f 1092 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1093 if (pidstr == NULL) {
1094 fprintf(stderr, "%s not set, cannot kill agent\n",
42f11eb2 1095 SSH_AGENTPID_ENV_NAME);
5260325f 1096 exit(1);
1097 }
cfaa5405 1098 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1099 if (errstr) {
1100 fprintf(stderr,
1101 "%s=\"%s\", which is not a good PID: %s\n",
1102 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
5260325f 1103 exit(1);
1104 }
1105 if (kill(pid, SIGTERM) == -1) {
1106 perror("kill");
1107 exit(1);
1108 }
1109 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1110 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1111 printf(format, SSH_AGENTPID_ENV_NAME);
c457707e 1112 printf("echo Agent pid %ld killed;\n", (long)pid);
5260325f 1113 exit(0);
dae3fa13 1114 }
5260325f 1115 parent_pid = getpid();
1116
3e7efb37 1117 if (agentsocket == NULL) {
1118 /* Create private directory for agent socket */
c88de854 1119 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
3e7efb37 1120 if (mkdtemp(socket_dir) == NULL) {
1121 perror("mkdtemp: private socket dir");
1122 exit(1);
1123 }
c457707e 1124 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1125 (long)parent_pid);
3e7efb37 1126 } else {
1127 /* Try to use specified agent socket */
1128 socket_dir[0] = '\0';
1129 strlcpy(socket_name, agentsocket, sizeof socket_name);
5260325f 1130 }
5260325f 1131
aa3378df 1132 /*
1133 * Create socket early so it will exist before command gets run from
1134 * the parent.
1135 */
5260325f 1136 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1137 if (sock < 0) {
1138 perror("socket");
8e8d8c82 1139 *socket_name = '\0'; /* Don't unlink any existing file */
5260325f 1140 cleanup_exit(1);
1141 }
1142 memset(&sunaddr, 0, sizeof(sunaddr));
1143 sunaddr.sun_family = AF_UNIX;
1144 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
c7ccfd39 1145 prev_mask = umask(0177);
32596c7b 1146 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
5260325f 1147 perror("bind");
8e8d8c82 1148 *socket_name = '\0'; /* Don't unlink any existing file */
c7ccfd39 1149 umask(prev_mask);
5260325f 1150 cleanup_exit(1);
1151 }
c7ccfd39 1152 umask(prev_mask);
fce39749 1153 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
5260325f 1154 perror("listen");
1155 cleanup_exit(1);
dae3fa13 1156 }
42f11eb2 1157
aa3378df 1158 /*
1159 * Fork, and have the parent execute the command, if any, or present
1160 * the socket data. The child continues as the authentication agent.
1161 */
ffdb5d70 1162 if (d_flag) {
1163 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1164 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1165 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1166 SSH_AUTHSOCKET_ENV_NAME);
c457707e 1167 printf("echo Agent pid %ld;\n", (long)parent_pid);
ffdb5d70 1168 goto skip;
1169 }
5260325f 1170 pid = fork();
1171 if (pid == -1) {
1172 perror("fork");
fccfbe3b 1173 cleanup_exit(1);
dae3fa13 1174 }
5260325f 1175 if (pid != 0) { /* Parent - execute the given command. */
1176 close(sock);
c457707e 1177 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
5260325f 1178 if (ac == 0) {
1179 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1180 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
42f11eb2 1181 SSH_AUTHSOCKET_ENV_NAME);
5260325f 1182 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
42f11eb2 1183 SSH_AGENTPID_ENV_NAME);
c457707e 1184 printf("echo Agent pid %ld;\n", (long)pid);
5260325f 1185 exit(0);
1186 }
bcbf86ec 1187 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1188 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1189 perror("setenv");
1190 exit(1);
1191 }
5260325f 1192 execvp(av[0], av);
1193 perror(av[0]);
1194 exit(1);
1195 }
fccfbe3b 1196 /* child */
1197 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
ec9f3450 1198
1199 if (setsid() == -1) {
fccfbe3b 1200 error("setsid: %s", strerror(errno));
ec9f3450 1201 cleanup_exit(1);
1202 }
1203
1204 (void)chdir("/");
c4087616 1205 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1206 /* XXX might close listen socket */
1207 (void)dup2(fd, STDIN_FILENO);
1208 (void)dup2(fd, STDOUT_FILENO);
1209 (void)dup2(fd, STDERR_FILENO);
1210 if (fd > 2)
1211 close(fd);
1212 }
dae3fa13 1213
b03bd394 1214#ifdef HAVE_SETRLIMIT
0b6fbf03 1215 /* deny core dumps, since memory contains unencrypted private keys */
1216 rlim.rlim_cur = rlim.rlim_max = 0;
1217 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
fccfbe3b 1218 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
0b6fbf03 1219 cleanup_exit(1);
1220 }
b03bd394 1221#endif
ffdb5d70 1222
1223skip:
5260325f 1224 new_socket(AUTH_SOCKET, sock);
1225 if (ac > 0) {
8d9bb5dd 1226 mysignal(SIGALRM, check_parent_exists);
5260325f 1227 alarm(10);
1228 }
2e73a022 1229 idtab_init();
1ee482c5 1230 if (!d_flag)
ffdb5d70 1231 signal(SIGINT, SIG_IGN);
1ee482c5 1232 signal(SIGPIPE, SIG_IGN);
2f4b2e38 1233 signal(SIGHUP, cleanup_handler);
1234 signal(SIGTERM, cleanup_handler);
e364646f 1235 nalloc = 0;
1236
5260325f 1237 while (1) {
e364646f 1238 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
42f11eb2 1239 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
5260325f 1240 if (errno == EINTR)
1241 continue;
fccfbe3b 1242 fatal("select: %s", strerror(errno));
5260325f 1243 }
42f11eb2 1244 after_select(readsetp, writesetp);
8efc0c15 1245 }
5260325f 1246 /* NOTREACHED */
8efc0c15 1247}
This page took 0.490038 seconds and 5 git commands to generate.