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