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