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