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