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