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