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