]> andersk Git - openssh.git/blame - ssh-agent.c
- (bal) Cygwin special handling of empty passwords wrong. Patch by
[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"
ee900f87 38RCSID("$OpenBSD: ssh-agent.c,v 1.94 2002/06/15 01:27:48 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;
ee900f87 398 int type, success = 0, death = 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;
ee900f87 454 while (buffer_len(&e->request)) {
455 switch (buffer_get_char(&e->request)) {
456 case SSH_AGENT_CONSTRAIN_LIFETIME:
457 death = time(NULL) + buffer_get_int(&e->request);
458 break;
459 default:
460 break;
461 }
462 }
95f0a918 463 if (lookup_identity(k, version) == NULL) {
464 Identity *id = xmalloc(sizeof(Identity));
465 id->key = k;
466 id->comment = comment;
ee900f87 467 id->death = death;
95f0a918 468 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
2e73a022 469 /* Increment the number of identities. */
470 tab->nentries++;
471 } else {
472 key_free(k);
473 xfree(comment);
474 }
475send:
5260325f 476 buffer_put_int(&e->output, 1);
2e73a022 477 buffer_put_char(&e->output,
478 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
8efc0c15 479}
480
3db7f994 481/* XXX todo: encrypt sensitive data with passphrase */
482static void
483process_lock_agent(SocketEntry *e, int lock)
484{
485 char *passwd;
486 int success = 0;
487
488 passwd = buffer_get_string(&e->request, NULL);
489 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
490 locked = 0;
491 memset(lock_passwd, 0, strlen(lock_passwd));
492 xfree(lock_passwd);
493 lock_passwd = NULL;
494 success = 1;
495 } else if (!locked && lock) {
496 locked = 1;
497 lock_passwd = xstrdup(passwd);
498 success = 1;
499 }
500 memset(passwd, 0, strlen(passwd));
501 xfree(passwd);
502
503 buffer_put_int(&e->output, 1);
504 buffer_put_char(&e->output,
505 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
506 return;
507}
508
509static void
510no_identities(SocketEntry *e, u_int type)
511{
512 Buffer msg;
513
514 buffer_init(&msg);
515 buffer_put_char(&msg,
516 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
517 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
518 buffer_put_int(&msg, 0);
519 buffer_put_int(&e->output, buffer_len(&msg));
520 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
521 buffer_free(&msg);
522}
2b5fe3b8 523
524#ifdef SMARTCARD
525static void
526process_add_smartcard_key (SocketEntry *e)
527{
0659cace 528 Identity *id;
2b5fe3b8 529 Idtab *tab;
0659cace 530 Key **keys, *k;
76139bd8 531 char *sc_reader_id = NULL, *pin;
0659cace 532 int i, version, success = 0;
184eed6a 533
b9dd5ca1 534 sc_reader_id = buffer_get_string(&e->request, NULL);
535 pin = buffer_get_string(&e->request, NULL);
0659cace 536 keys = sc_get_keys(sc_reader_id, pin);
9ff6f66f 537 xfree(sc_reader_id);
76139bd8 538 xfree(pin);
2b5fe3b8 539
0659cace 540 if (keys == NULL || keys[0] == NULL) {
541 error("sc_get_keys failed");
2b5fe3b8 542 goto send;
543 }
0659cace 544 for (i = 0; keys[i] != NULL; i++) {
545 k = keys[i];
546 version = k->type == KEY_RSA1 ? 1 : 2;
547 tab = idtab_lookup(version);
548 if (lookup_identity(k, version) == NULL) {
549 id = xmalloc(sizeof(Identity));
550 id->key = k;
551 id->comment = xstrdup("smartcard key");
264572cc 552 id->death = 0;
0659cace 553 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
554 tab->nentries++;
555 success = 1;
556 } else {
557 key_free(k);
558 }
559 keys[i] = NULL;
2b5fe3b8 560 }
0659cace 561 xfree(keys);
2b5fe3b8 562send:
563 buffer_put_int(&e->output, 1);
564 buffer_put_char(&e->output,
565 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
566}
567
568static void
569process_remove_smartcard_key(SocketEntry *e)
570{
0659cace 571 Identity *id;
572 Idtab *tab;
573 Key **keys, *k = NULL;
76139bd8 574 char *sc_reader_id = NULL, *pin;
0659cace 575 int i, version, success = 0;
2b5fe3b8 576
b9dd5ca1 577 sc_reader_id = buffer_get_string(&e->request, NULL);
578 pin = buffer_get_string(&e->request, NULL);
0659cace 579 keys = sc_get_keys(sc_reader_id, pin);
9ff6f66f 580 xfree(sc_reader_id);
76139bd8 581 xfree(pin);
2b5fe3b8 582
0659cace 583 if (keys == NULL || keys[0] == NULL) {
584 error("sc_get_keys failed");
585 goto send;
586 }
587 for (i = 0; keys[i] != NULL; i++) {
588 k = keys[i];
589 version = k->type == KEY_RSA1 ? 1 : 2;
590 if ((id = lookup_identity(k, version)) != NULL) {
591 tab = idtab_lookup(version);
875ec275 592 TAILQ_REMOVE(&tab->idlist, id, next);
2b5fe3b8 593 tab->nentries--;
95f0a918 594 free_identity(id);
2b5fe3b8 595 success = 1;
596 }
597 key_free(k);
0659cace 598 keys[i] = NULL;
2b5fe3b8 599 }
0659cace 600 xfree(keys);
601send:
2b5fe3b8 602 buffer_put_int(&e->output, 1);
603 buffer_put_char(&e->output,
604 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
605}
3e984472 606#endif /* SMARTCARD */
2b5fe3b8 607
2e73a022 608/* dispatch incoming messages */
609
396c147e 610static void
8efc0c15 611process_message(SocketEntry *e)
612{
1e3b8b07 613 u_int msg_len;
614 u_int type;
615 u_char *cp;
264572cc 616
617 /* kill dead keys */
618 reaper();
619
5260325f 620 if (buffer_len(&e->input) < 5)
621 return; /* Incomplete message. */
20905a8e 622 cp = buffer_ptr(&e->input);
5260325f 623 msg_len = GET_32BIT(cp);
624 if (msg_len > 256 * 1024) {
625 shutdown(e->fd, SHUT_RDWR);
626 close(e->fd);
627 e->type = AUTH_UNUSED;
b9dd5ca1 628 buffer_free(&e->input);
629 buffer_free(&e->output);
630 buffer_free(&e->request);
5260325f 631 return;
632 }
633 if (buffer_len(&e->input) < msg_len + 4)
634 return;
b9dd5ca1 635
636 /* move the current input to e->request */
5260325f 637 buffer_consume(&e->input, 4);
b9dd5ca1 638 buffer_clear(&e->request);
639 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
640 buffer_consume(&e->input, msg_len);
641 type = buffer_get_char(&e->request);
5260325f 642
3db7f994 643 /* check wheter agent is locked */
644 if (locked && type != SSH_AGENTC_UNLOCK) {
645 buffer_clear(&e->request);
646 switch (type) {
647 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
648 case SSH2_AGENTC_REQUEST_IDENTITIES:
649 /* send empty lists */
650 no_identities(e, type);
651 break;
652 default:
653 /* send a fail message for all other request types */
654 buffer_put_int(&e->output, 1);
655 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
656 }
657 return;
658 }
659
2b5fe3b8 660 debug("type %d", type);
5260325f 661 switch (type) {
3db7f994 662 case SSH_AGENTC_LOCK:
663 case SSH_AGENTC_UNLOCK:
664 process_lock_agent(e, type == SSH_AGENTC_LOCK);
665 break;
2e73a022 666 /* ssh1 */
5260325f 667 case SSH_AGENTC_RSA_CHALLENGE:
2e73a022 668 process_authentication_challenge1(e);
669 break;
670 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
671 process_request_identities(e, 1);
5260325f 672 break;
673 case SSH_AGENTC_ADD_RSA_IDENTITY:
ee900f87 674 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
2e73a022 675 process_add_identity(e, 1);
5260325f 676 break;
677 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
2e73a022 678 process_remove_identity(e, 1);
5260325f 679 break;
680 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
2e73a022 681 process_remove_all_identities(e, 1);
682 break;
683 /* ssh2 */
684 case SSH2_AGENTC_SIGN_REQUEST:
685 process_sign_request2(e);
686 break;
687 case SSH2_AGENTC_REQUEST_IDENTITIES:
688 process_request_identities(e, 2);
689 break;
690 case SSH2_AGENTC_ADD_IDENTITY:
ee900f87 691 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
2e73a022 692 process_add_identity(e, 2);
693 break;
694 case SSH2_AGENTC_REMOVE_IDENTITY:
695 process_remove_identity(e, 2);
696 break;
697 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
698 process_remove_all_identities(e, 2);
5260325f 699 break;
2b5fe3b8 700#ifdef SMARTCARD
701 case SSH_AGENTC_ADD_SMARTCARD_KEY:
702 process_add_smartcard_key(e);
184eed6a 703 break;
2b5fe3b8 704 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
705 process_remove_smartcard_key(e);
184eed6a 706 break;
3e984472 707#endif /* SMARTCARD */
5260325f 708 default:
709 /* Unknown message. Respond with failure. */
710 error("Unknown message %d", type);
b9dd5ca1 711 buffer_clear(&e->request);
5260325f 712 buffer_put_int(&e->output, 1);
713 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
714 break;
715 }
8efc0c15 716}
717
396c147e 718static void
17a3011c 719new_socket(sock_type type, int fd)
8efc0c15 720{
1e3b8b07 721 u_int i, old_alloc;
5260325f 722 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
723 error("fcntl O_NONBLOCK: %s", strerror(errno));
724
725 if (fd > max_fd)
726 max_fd = fd;
727
728 for (i = 0; i < sockets_alloc; i++)
729 if (sockets[i].type == AUTH_UNUSED) {
730 sockets[i].fd = fd;
731 sockets[i].type = type;
732 buffer_init(&sockets[i].input);
733 buffer_init(&sockets[i].output);
b9dd5ca1 734 buffer_init(&sockets[i].request);
5260325f 735 return;
736 }
737 old_alloc = sockets_alloc;
738 sockets_alloc += 10;
739 if (sockets)
740 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
741 else
742 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
743 for (i = old_alloc; i < sockets_alloc; i++)
744 sockets[i].type = AUTH_UNUSED;
745 sockets[old_alloc].type = type;
746 sockets[old_alloc].fd = fd;
747 buffer_init(&sockets[old_alloc].input);
748 buffer_init(&sockets[old_alloc].output);
b9dd5ca1 749 buffer_init(&sockets[old_alloc].request);
8efc0c15 750}
751
396c147e 752static int
e364646f 753prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
8efc0c15 754{
42f11eb2 755 u_int i, sz;
756 int n = 0;
757
758 for (i = 0; i < sockets_alloc; i++) {
5260325f 759 switch (sockets[i].type) {
760 case AUTH_SOCKET:
761 case AUTH_CONNECTION:
42f11eb2 762 n = MAX(n, sockets[i].fd);
5260325f 763 break;
764 case AUTH_UNUSED:
765 break;
766 default:
767 fatal("Unknown socket type %d", sockets[i].type);
768 break;
769 }
42f11eb2 770 }
771
772 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
e364646f 773 if (*fdrp == NULL || sz > *nallocp) {
42f11eb2 774 if (*fdrp)
894c5fa6 775 xfree(*fdrp);
42f11eb2 776 if (*fdwp)
894c5fa6 777 xfree(*fdwp);
42f11eb2 778 *fdrp = xmalloc(sz);
779 *fdwp = xmalloc(sz);
e364646f 780 *nallocp = sz;
42f11eb2 781 }
e364646f 782 if (n < *fdl)
783 debug("XXX shrink: %d < %d", n, *fdl);
784 *fdl = n;
42f11eb2 785 memset(*fdrp, 0, sz);
786 memset(*fdwp, 0, sz);
787
788 for (i = 0; i < sockets_alloc; i++) {
789 switch (sockets[i].type) {
790 case AUTH_SOCKET:
791 case AUTH_CONNECTION:
792 FD_SET(sockets[i].fd, *fdrp);
793 if (buffer_len(&sockets[i].output) > 0)
794 FD_SET(sockets[i].fd, *fdwp);
795 break;
796 default:
797 break;
798 }
799 }
800 return (1);
8efc0c15 801}
802
396c147e 803static void
5260325f 804after_select(fd_set *readset, fd_set *writeset)
8efc0c15 805{
1e3b8b07 806 u_int i;
5260325f 807 int len, sock;
610cd5c6 808 socklen_t slen;
5260325f 809 char buf[1024];
810 struct sockaddr_un sunaddr;
811
812 for (i = 0; i < sockets_alloc; i++)
813 switch (sockets[i].type) {
814 case AUTH_UNUSED:
815 break;
816 case AUTH_SOCKET:
817 if (FD_ISSET(sockets[i].fd, readset)) {
610cd5c6 818 slen = sizeof(sunaddr);
42f11eb2 819 sock = accept(sockets[i].fd,
820 (struct sockaddr *) &sunaddr, &slen);
5260325f 821 if (sock < 0) {
fccfbe3b 822 error("accept from AUTH_SOCKET: %s",
823 strerror(errno));
5260325f 824 break;
825 }
826 new_socket(AUTH_CONNECTION, sock);
827 }
828 break;
829 case AUTH_CONNECTION:
830 if (buffer_len(&sockets[i].output) > 0 &&
831 FD_ISSET(sockets[i].fd, writeset)) {
bbc62e59 832 do {
833 len = write(sockets[i].fd,
834 buffer_ptr(&sockets[i].output),
835 buffer_len(&sockets[i].output));
836 if (len == -1 && (errno == EAGAIN ||
837 errno == EINTR))
838 continue;
839 break;
840 } while (1);
5260325f 841 if (len <= 0) {
842 shutdown(sockets[i].fd, SHUT_RDWR);
843 close(sockets[i].fd);
844 sockets[i].type = AUTH_UNUSED;
0ac7199f 845 buffer_free(&sockets[i].input);
846 buffer_free(&sockets[i].output);
b9dd5ca1 847 buffer_free(&sockets[i].request);
5260325f 848 break;
849 }
850 buffer_consume(&sockets[i].output, len);
851 }
852 if (FD_ISSET(sockets[i].fd, readset)) {
bbc62e59 853 do {
854 len = read(sockets[i].fd, buf, sizeof(buf));
855 if (len == -1 && (errno == EAGAIN ||
856 errno == EINTR))
857 continue;
858 break;
859 } while (1);
5260325f 860 if (len <= 0) {
861 shutdown(sockets[i].fd, SHUT_RDWR);
862 close(sockets[i].fd);
863 sockets[i].type = AUTH_UNUSED;
0ac7199f 864 buffer_free(&sockets[i].input);
865 buffer_free(&sockets[i].output);
b9dd5ca1 866 buffer_free(&sockets[i].request);
5260325f 867 break;
868 }
869 buffer_append(&sockets[i].input, buf, len);
870 process_message(&sockets[i]);
871 }
872 break;
873 default:
874 fatal("Unknown type %d", sockets[i].type);
875 }
8efc0c15 876}
877
396c147e 878static void
fccfbe3b 879cleanup_socket(void *p)
dae3fa13 880{
2f4b2e38 881 if (socket_name[0])
882 unlink(socket_name);
883 if (socket_dir[0])
884 rmdir(socket_dir);
8efc0c15 885}
886
396c147e 887static void
dae3fa13 888cleanup_exit(int i)
889{
fccfbe3b 890 cleanup_socket(NULL);
5260325f 891 exit(i);
dae3fa13 892}
893
396c147e 894static void
2f4b2e38 895cleanup_handler(int sig)
896{
fccfbe3b 897 cleanup_socket(NULL);
2f4b2e38 898 _exit(2);
899}
900
7efa8482 901static void
902check_parent_exists(int sig)
903{
904 int save_errno = errno;
905
906 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
907 /* printf("Parent has died - Authentication agent exiting.\n"); */
908 cleanup_handler(sig); /* safe */
909 }
910 signal(SIGALRM, check_parent_exists);
911 alarm(10);
912 errno = save_errno;
913}
914
396c147e 915static void
cc8aca8a 916usage(void)
dae3fa13 917{
33e766d2 918 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
0e3c1f95 919 __progname);
33e766d2 920 fprintf(stderr, "Options:\n");
921 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
922 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
923 fprintf(stderr, " -k Kill the current agent.\n");
924 fprintf(stderr, " -d Debug mode.\n");
3e7efb37 925 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
5260325f 926 exit(1);
dae3fa13 927}
928
8efc0c15 929int
930main(int ac, char **av)
931{
e364646f 932 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
5260325f 933 struct sockaddr_un sunaddr;
b03bd394 934#ifdef HAVE_SETRLIMIT
0b6fbf03 935 struct rlimit rlim;
c7ccfd39 936#endif
937#ifdef HAVE_CYGWIN
938 int prev_mask;
b03bd394 939#endif
5260325f 940 pid_t pid;
941 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
3e7efb37 942 char *agentsocket = NULL;
c04f75f1 943 extern int optind;
42f11eb2 944 fd_set *readsetp = NULL, *writesetp = NULL;
fcb975eb 945
457fc0c6 946 SSLeay_add_all_algorithms();
947
260d427b 948 __progname = get_progname(av[0]);
264dce47 949 init_rng();
e339aa53 950 seed_rng();
2b87da3b 951
4e577b89 952#ifdef __GNU_LIBRARY__
3e7efb37 953 while ((ch = getopt(ac, av, "+cdksa:")) != -1) {
4e577b89 954#else /* __GNU_LIBRARY__ */
3e7efb37 955 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
4e577b89 956#endif /* __GNU_LIBRARY__ */
5260325f 957 switch (ch) {
958 case 'c':
959 if (s_flag)
960 usage();
961 c_flag++;
962 break;
963 case 'k':
964 k_flag++;
965 break;
966 case 's':
967 if (c_flag)
968 usage();
969 s_flag++;
970 break;
ffdb5d70 971 case 'd':
972 if (d_flag)
973 usage();
974 d_flag++;
975 break;
3e7efb37 976 case 'a':
977 agentsocket = optarg;
978 break;
5260325f 979 default:
980 usage();
981 }
dae3fa13 982 }
5260325f 983 ac -= optind;
984 av += optind;
985
ffdb5d70 986 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
5260325f 987 usage();
988
b745a2f2 989 if (ac == 0 && !c_flag && !s_flag) {
5260325f 990 shell = getenv("SHELL");
991 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
992 c_flag = 1;
dae3fa13 993 }
5260325f 994 if (k_flag) {
995 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
996 if (pidstr == NULL) {
997 fprintf(stderr, "%s not set, cannot kill agent\n",
42f11eb2 998 SSH_AGENTPID_ENV_NAME);
5260325f 999 exit(1);
1000 }
1001 pid = atoi(pidstr);
42f11eb2 1002 if (pid < 1) {
5260325f 1003 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
42f11eb2 1004 SSH_AGENTPID_ENV_NAME, pidstr);
5260325f 1005 exit(1);
1006 }
1007 if (kill(pid, SIGTERM) == -1) {
1008 perror("kill");
1009 exit(1);
1010 }
1011 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1012 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1013 printf(format, SSH_AGENTPID_ENV_NAME);
c457707e 1014 printf("echo Agent pid %ld killed;\n", (long)pid);
5260325f 1015 exit(0);
dae3fa13 1016 }
5260325f 1017 parent_pid = getpid();
1018
3e7efb37 1019 if (agentsocket == NULL) {
1020 /* Create private directory for agent socket */
1021 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1022 if (mkdtemp(socket_dir) == NULL) {
1023 perror("mkdtemp: private socket dir");
1024 exit(1);
1025 }
c457707e 1026 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1027 (long)parent_pid);
3e7efb37 1028 } else {
1029 /* Try to use specified agent socket */
1030 socket_dir[0] = '\0';
1031 strlcpy(socket_name, agentsocket, sizeof socket_name);
5260325f 1032 }
5260325f 1033
aa3378df 1034 /*
1035 * Create socket early so it will exist before command gets run from
1036 * the parent.
1037 */
5260325f 1038 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1039 if (sock < 0) {
1040 perror("socket");
1041 cleanup_exit(1);
1042 }
1043 memset(&sunaddr, 0, sizeof(sunaddr));
1044 sunaddr.sun_family = AF_UNIX;
1045 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
c7ccfd39 1046#ifdef HAVE_CYGWIN
1047 prev_mask = umask(0177);
1048#endif
5260325f 1049 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1050 perror("bind");
c7ccfd39 1051#ifdef HAVE_CYGWIN
1052 umask(prev_mask);
1053#endif
5260325f 1054 cleanup_exit(1);
1055 }
c7ccfd39 1056#ifdef HAVE_CYGWIN
1057 umask(prev_mask);
1058#endif
5260325f 1059 if (listen(sock, 5) < 0) {
1060 perror("listen");
1061 cleanup_exit(1);
dae3fa13 1062 }
42f11eb2 1063
aa3378df 1064 /*
1065 * Fork, and have the parent execute the command, if any, or present
1066 * the socket data. The child continues as the authentication agent.
1067 */
ffdb5d70 1068 if (d_flag) {
1069 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1070 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1071 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1072 SSH_AUTHSOCKET_ENV_NAME);
c457707e 1073 printf("echo Agent pid %ld;\n", (long)parent_pid);
ffdb5d70 1074 goto skip;
1075 }
5260325f 1076 pid = fork();
1077 if (pid == -1) {
1078 perror("fork");
fccfbe3b 1079 cleanup_exit(1);
dae3fa13 1080 }
5260325f 1081 if (pid != 0) { /* Parent - execute the given command. */
1082 close(sock);
c457707e 1083 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
5260325f 1084 if (ac == 0) {
1085 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1086 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
42f11eb2 1087 SSH_AUTHSOCKET_ENV_NAME);
5260325f 1088 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
42f11eb2 1089 SSH_AGENTPID_ENV_NAME);
c457707e 1090 printf("echo Agent pid %ld;\n", (long)pid);
5260325f 1091 exit(0);
1092 }
bcbf86ec 1093 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1094 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1095 perror("setenv");
1096 exit(1);
1097 }
5260325f 1098 execvp(av[0], av);
1099 perror(av[0]);
1100 exit(1);
1101 }
fccfbe3b 1102 /* child */
1103 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
ec9f3450 1104
1105 if (setsid() == -1) {
fccfbe3b 1106 error("setsid: %s", strerror(errno));
ec9f3450 1107 cleanup_exit(1);
1108 }
1109
1110 (void)chdir("/");
5260325f 1111 close(0);
1112 close(1);
1113 close(2);
dae3fa13 1114
b03bd394 1115#ifdef HAVE_SETRLIMIT
0b6fbf03 1116 /* deny core dumps, since memory contains unencrypted private keys */
1117 rlim.rlim_cur = rlim.rlim_max = 0;
1118 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
fccfbe3b 1119 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
0b6fbf03 1120 cleanup_exit(1);
1121 }
b03bd394 1122#endif
ffdb5d70 1123
1124skip:
fccfbe3b 1125 fatal_add_cleanup(cleanup_socket, NULL);
5260325f 1126 new_socket(AUTH_SOCKET, sock);
1127 if (ac > 0) {
1128 signal(SIGALRM, check_parent_exists);
1129 alarm(10);
1130 }
2e73a022 1131 idtab_init();
1ee482c5 1132 if (!d_flag)
ffdb5d70 1133 signal(SIGINT, SIG_IGN);
1ee482c5 1134 signal(SIGPIPE, SIG_IGN);
2f4b2e38 1135 signal(SIGHUP, cleanup_handler);
1136 signal(SIGTERM, cleanup_handler);
e364646f 1137 nalloc = 0;
1138
5260325f 1139 while (1) {
e364646f 1140 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
42f11eb2 1141 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
5260325f 1142 if (errno == EINTR)
1143 continue;
fccfbe3b 1144 fatal("select: %s", strerror(errno));
5260325f 1145 }
42f11eb2 1146 after_select(readsetp, writesetp);
8efc0c15 1147 }
5260325f 1148 /* NOTREACHED */
8efc0c15 1149}
This page took 0.358412 seconds and 5 git commands to generate.