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