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