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