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