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