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