]> andersk Git - openssh.git/blame - ssh-agent.c
- (djm) Sync sys/tree.h with OpenBSD -current. Rename tree.h and
[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"
9fd2a215 37#include "openbsd-compat/sys-queue.h"
87f4111f 38RCSID("$OpenBSD: ssh-agent.c,v 1.103 2002/09/10 20:24:47 markus Exp $");
187cd1fa 39
42f11eb2 40#include <openssl/evp.h>
41#include <openssl/md5.h>
8efc0c15 42
43#include "ssh.h"
44#include "rsa.h"
8efc0c15 45#include "buffer.h"
46#include "bufaux.h"
47#include "xmalloc.h"
8efc0c15 48#include "getput.h"
4c8722d9 49#include "key.h"
50#include "authfd.h"
188adeb2 51#include "compat.h"
42f11eb2 52#include "log.h"
8efc0c15 53
2b5fe3b8 54#ifdef SMARTCARD
2b5fe3b8 55#include "scard.h"
dd2495cb 56#endif
2b5fe3b8 57
17a3011c 58typedef enum {
59 AUTH_UNUSED,
60 AUTH_SOCKET,
61 AUTH_CONNECTION
62} sock_type;
63
5260325f 64typedef struct {
65 int fd;
17a3011c 66 sock_type type;
5260325f 67 Buffer input;
68 Buffer output;
b9dd5ca1 69 Buffer request;
8efc0c15 70} SocketEntry;
71
1e3b8b07 72u_int sockets_alloc = 0;
8efc0c15 73SocketEntry *sockets = NULL;
74
95f0a918 75typedef struct identity {
76 TAILQ_ENTRY(identity) next;
2e73a022 77 Key *key;
5260325f 78 char *comment;
264572cc 79 u_int death;
8efc0c15 80} Identity;
81
2e73a022 82typedef struct {
83 int nentries;
95f0a918 84 TAILQ_HEAD(idqueue, identity) idlist;
2e73a022 85} Idtab;
86
87/* private key table, one per protocol version */
88Idtab idtable[3];
8efc0c15 89
90int max_fd = 0;
91
92/* pid of shell == parent of agent */
9da5c3c9 93pid_t parent_pid = -1;
8efc0c15 94
95/* pathname and directory for AUTH_SOCKET */
96char socket_name[1024];
97char socket_dir[1024];
98
3db7f994 99/* locking */
100int locked = 0;
101char *lock_passwd = NULL;
102
5260325f 103#ifdef HAVE___PROGNAME
104extern char *__progname;
260d427b 105#else
106char *__progname;
107#endif
5260325f 108
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;
87f4111f 813 uid_t euid;
814 gid_t egid;
5260325f 815
816 for (i = 0; i < sockets_alloc; i++)
817 switch (sockets[i].type) {
818 case AUTH_UNUSED:
819 break;
820 case AUTH_SOCKET:
821 if (FD_ISSET(sockets[i].fd, readset)) {
610cd5c6 822 slen = sizeof(sunaddr);
42f11eb2 823 sock = accept(sockets[i].fd,
824 (struct sockaddr *) &sunaddr, &slen);
5260325f 825 if (sock < 0) {
fccfbe3b 826 error("accept from AUTH_SOCKET: %s",
827 strerror(errno));
5260325f 828 break;
829 }
87f4111f 830 if (getpeereid(sock, &euid, &egid) < 0) {
831 error("getpeereid %d failed: %s",
832 sock, strerror(errno));
833 close(sock);
834 break;
835 }
836 if (getuid() != euid) {
837 error("uid mismatch: "
838 "peer euid %d != uid %d",
839 (int) euid, (int) getuid());
840 close(sock);
841 break;
842 }
5260325f 843 new_socket(AUTH_CONNECTION, sock);
844 }
845 break;
846 case AUTH_CONNECTION:
847 if (buffer_len(&sockets[i].output) > 0 &&
848 FD_ISSET(sockets[i].fd, writeset)) {
bbc62e59 849 do {
850 len = write(sockets[i].fd,
851 buffer_ptr(&sockets[i].output),
852 buffer_len(&sockets[i].output));
853 if (len == -1 && (errno == EAGAIN ||
854 errno == EINTR))
855 continue;
856 break;
857 } while (1);
5260325f 858 if (len <= 0) {
c1a4eef1 859 close_socket(&sockets[i]);
5260325f 860 break;
861 }
862 buffer_consume(&sockets[i].output, len);
863 }
864 if (FD_ISSET(sockets[i].fd, readset)) {
bbc62e59 865 do {
866 len = read(sockets[i].fd, buf, sizeof(buf));
867 if (len == -1 && (errno == EAGAIN ||
868 errno == EINTR))
869 continue;
870 break;
871 } while (1);
5260325f 872 if (len <= 0) {
c1a4eef1 873 close_socket(&sockets[i]);
5260325f 874 break;
875 }
876 buffer_append(&sockets[i].input, buf, len);
877 process_message(&sockets[i]);
878 }
879 break;
880 default:
881 fatal("Unknown type %d", sockets[i].type);
882 }
8efc0c15 883}
884
396c147e 885static void
fccfbe3b 886cleanup_socket(void *p)
dae3fa13 887{
2f4b2e38 888 if (socket_name[0])
889 unlink(socket_name);
890 if (socket_dir[0])
891 rmdir(socket_dir);
8efc0c15 892}
893
396c147e 894static void
dae3fa13 895cleanup_exit(int i)
896{
fccfbe3b 897 cleanup_socket(NULL);
5260325f 898 exit(i);
dae3fa13 899}
900
396c147e 901static void
2f4b2e38 902cleanup_handler(int sig)
903{
fccfbe3b 904 cleanup_socket(NULL);
2f4b2e38 905 _exit(2);
906}
907
7efa8482 908static void
909check_parent_exists(int sig)
910{
911 int save_errno = errno;
912
913 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
914 /* printf("Parent has died - Authentication agent exiting.\n"); */
915 cleanup_handler(sig); /* safe */
916 }
917 signal(SIGALRM, check_parent_exists);
918 alarm(10);
919 errno = save_errno;
920}
921
396c147e 922static void
cc8aca8a 923usage(void)
dae3fa13 924{
33e766d2 925 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
0e3c1f95 926 __progname);
33e766d2 927 fprintf(stderr, "Options:\n");
928 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
929 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
930 fprintf(stderr, " -k Kill the current agent.\n");
931 fprintf(stderr, " -d Debug mode.\n");
3e7efb37 932 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
5260325f 933 exit(1);
dae3fa13 934}
935
8efc0c15 936int
937main(int ac, char **av)
938{
e364646f 939 int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
e424e241 940 char *shell, *format, *pidstr, *agentsocket = NULL;
941 fd_set *readsetp = NULL, *writesetp = NULL;
5260325f 942 struct sockaddr_un sunaddr;
b03bd394 943#ifdef HAVE_SETRLIMIT
0b6fbf03 944 struct rlimit rlim;
c7ccfd39 945#endif
946#ifdef HAVE_CYGWIN
947 int prev_mask;
b03bd394 948#endif
c04f75f1 949 extern int optind;
ef3912be 950 extern char *optarg;
e424e241 951 pid_t pid;
952 char pidstrbuf[1 + 3 * sizeof pid];
fcb975eb 953
954640a4 954 /* drop */
955 setegid(getgid());
956 setgid(getgid());
957
457fc0c6 958 SSLeay_add_all_algorithms();
959
260d427b 960 __progname = get_progname(av[0]);
264dce47 961 init_rng();
e339aa53 962 seed_rng();
2b87da3b 963
3e7efb37 964 while ((ch = getopt(ac, av, "cdksa:")) != -1) {
5260325f 965 switch (ch) {
966 case 'c':
967 if (s_flag)
968 usage();
969 c_flag++;
970 break;
971 case 'k':
972 k_flag++;
973 break;
974 case 's':
975 if (c_flag)
976 usage();
977 s_flag++;
978 break;
ffdb5d70 979 case 'd':
980 if (d_flag)
981 usage();
982 d_flag++;
983 break;
3e7efb37 984 case 'a':
985 agentsocket = optarg;
986 break;
5260325f 987 default:
988 usage();
989 }
dae3fa13 990 }
5260325f 991 ac -= optind;
992 av += optind;
993
ffdb5d70 994 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
5260325f 995 usage();
996
b745a2f2 997 if (ac == 0 && !c_flag && !s_flag) {
5260325f 998 shell = getenv("SHELL");
999 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1000 c_flag = 1;
dae3fa13 1001 }
5260325f 1002 if (k_flag) {
1003 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1004 if (pidstr == NULL) {
1005 fprintf(stderr, "%s not set, cannot kill agent\n",
42f11eb2 1006 SSH_AGENTPID_ENV_NAME);
5260325f 1007 exit(1);
1008 }
1009 pid = atoi(pidstr);
42f11eb2 1010 if (pid < 1) {
5260325f 1011 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
42f11eb2 1012 SSH_AGENTPID_ENV_NAME, pidstr);
5260325f 1013 exit(1);
1014 }
1015 if (kill(pid, SIGTERM) == -1) {
1016 perror("kill");
1017 exit(1);
1018 }
1019 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1020 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1021 printf(format, SSH_AGENTPID_ENV_NAME);
c457707e 1022 printf("echo Agent pid %ld killed;\n", (long)pid);
5260325f 1023 exit(0);
dae3fa13 1024 }
5260325f 1025 parent_pid = getpid();
1026
3e7efb37 1027 if (agentsocket == NULL) {
1028 /* Create private directory for agent socket */
1029 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1030 if (mkdtemp(socket_dir) == NULL) {
1031 perror("mkdtemp: private socket dir");
1032 exit(1);
1033 }
c457707e 1034 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1035 (long)parent_pid);
3e7efb37 1036 } else {
1037 /* Try to use specified agent socket */
1038 socket_dir[0] = '\0';
1039 strlcpy(socket_name, agentsocket, sizeof socket_name);
5260325f 1040 }
5260325f 1041
aa3378df 1042 /*
1043 * Create socket early so it will exist before command gets run from
1044 * the parent.
1045 */
5260325f 1046 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1047 if (sock < 0) {
1048 perror("socket");
1049 cleanup_exit(1);
1050 }
1051 memset(&sunaddr, 0, sizeof(sunaddr));
1052 sunaddr.sun_family = AF_UNIX;
1053 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
c7ccfd39 1054#ifdef HAVE_CYGWIN
1055 prev_mask = umask(0177);
1056#endif
5260325f 1057 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1058 perror("bind");
c7ccfd39 1059#ifdef HAVE_CYGWIN
1060 umask(prev_mask);
1061#endif
5260325f 1062 cleanup_exit(1);
1063 }
c7ccfd39 1064#ifdef HAVE_CYGWIN
1065 umask(prev_mask);
1066#endif
00e41835 1067 if (listen(sock, 128) < 0) {
5260325f 1068 perror("listen");
1069 cleanup_exit(1);
dae3fa13 1070 }
42f11eb2 1071
aa3378df 1072 /*
1073 * Fork, and have the parent execute the command, if any, or present
1074 * the socket data. The child continues as the authentication agent.
1075 */
ffdb5d70 1076 if (d_flag) {
1077 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1078 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1079 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1080 SSH_AUTHSOCKET_ENV_NAME);
c457707e 1081 printf("echo Agent pid %ld;\n", (long)parent_pid);
ffdb5d70 1082 goto skip;
1083 }
5260325f 1084 pid = fork();
1085 if (pid == -1) {
1086 perror("fork");
fccfbe3b 1087 cleanup_exit(1);
dae3fa13 1088 }
5260325f 1089 if (pid != 0) { /* Parent - execute the given command. */
1090 close(sock);
c457707e 1091 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
5260325f 1092 if (ac == 0) {
1093 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1094 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
42f11eb2 1095 SSH_AUTHSOCKET_ENV_NAME);
5260325f 1096 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
42f11eb2 1097 SSH_AGENTPID_ENV_NAME);
c457707e 1098 printf("echo Agent pid %ld;\n", (long)pid);
5260325f 1099 exit(0);
1100 }
bcbf86ec 1101 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1102 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1103 perror("setenv");
1104 exit(1);
1105 }
5260325f 1106 execvp(av[0], av);
1107 perror(av[0]);
1108 exit(1);
1109 }
fccfbe3b 1110 /* child */
1111 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
ec9f3450 1112
1113 if (setsid() == -1) {
fccfbe3b 1114 error("setsid: %s", strerror(errno));
ec9f3450 1115 cleanup_exit(1);
1116 }
1117
1118 (void)chdir("/");
5260325f 1119 close(0);
1120 close(1);
1121 close(2);
dae3fa13 1122
b03bd394 1123#ifdef HAVE_SETRLIMIT
0b6fbf03 1124 /* deny core dumps, since memory contains unencrypted private keys */
1125 rlim.rlim_cur = rlim.rlim_max = 0;
1126 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
fccfbe3b 1127 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
0b6fbf03 1128 cleanup_exit(1);
1129 }
b03bd394 1130#endif
ffdb5d70 1131
1132skip:
fccfbe3b 1133 fatal_add_cleanup(cleanup_socket, NULL);
5260325f 1134 new_socket(AUTH_SOCKET, sock);
1135 if (ac > 0) {
1136 signal(SIGALRM, check_parent_exists);
1137 alarm(10);
1138 }
2e73a022 1139 idtab_init();
1ee482c5 1140 if (!d_flag)
ffdb5d70 1141 signal(SIGINT, SIG_IGN);
1ee482c5 1142 signal(SIGPIPE, SIG_IGN);
2f4b2e38 1143 signal(SIGHUP, cleanup_handler);
1144 signal(SIGTERM, cleanup_handler);
e364646f 1145 nalloc = 0;
1146
5260325f 1147 while (1) {
e364646f 1148 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
42f11eb2 1149 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
5260325f 1150 if (errno == EINTR)
1151 continue;
fccfbe3b 1152 fatal("select: %s", strerror(errno));
5260325f 1153 }
42f11eb2 1154 after_select(readsetp, writesetp);
8efc0c15 1155 }
5260325f 1156 /* NOTREACHED */
8efc0c15 1157}
This page took 0.395385 seconds and 5 git commands to generate.