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