]> andersk Git - openssh.git/blame - ssh-agent.c
- (djm) Add pointer to http://www.imasy.or.jp/~gotoh/connect.c to
[openssh.git] / ssh-agent.c
CommitLineData
188adeb2 1/* $OpenBSD: ssh-agent.c,v 1.37 2000/09/21 11:07:51 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 *
2e73a022 15 * SSH2 implementation,
16 * Copyright (c) 2000 Markus Friedl. All rights reserved.
bcbf86ec 17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5260325f 37 */
8efc0c15 38
39#include "includes.h"
188adeb2 40RCSID("$OpenBSD: ssh-agent.c,v 1.37 2000/09/21 11:07:51 markus Exp $");
8efc0c15 41
42#include "ssh.h"
43#include "rsa.h"
8efc0c15 44#include "buffer.h"
45#include "bufaux.h"
46#include "xmalloc.h"
47#include "packet.h"
48#include "getput.h"
49#include "mpaux.h"
50
2e73a022 51#include <openssl/evp.h>
8efc0c15 52#include <openssl/md5.h>
4c8722d9 53#include <openssl/dsa.h>
54#include <openssl/rsa.h>
55#include "key.h"
56#include "authfd.h"
2e73a022 57#include "dsa.h"
58#include "kex.h"
188adeb2 59#include "compat.h"
8efc0c15 60
5260325f 61typedef struct {
62 int fd;
63 enum {
64 AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
65 } type;
66 Buffer input;
67 Buffer output;
8efc0c15 68} SocketEntry;
69
70unsigned int sockets_alloc = 0;
71SocketEntry *sockets = NULL;
72
5260325f 73typedef struct {
2e73a022 74 Key *key;
5260325f 75 char *comment;
8efc0c15 76} Identity;
77
2e73a022 78typedef struct {
79 int nentries;
80 Identity *identities;
81} Idtab;
82
83/* private key table, one per protocol version */
84Idtab idtable[3];
8efc0c15 85
86int max_fd = 0;
87
88/* pid of shell == parent of agent */
9da5c3c9 89pid_t parent_pid = -1;
8efc0c15 90
91/* pathname and directory for AUTH_SOCKET */
92char socket_name[1024];
93char socket_dir[1024];
94
5260325f 95#ifdef HAVE___PROGNAME
96extern char *__progname;
97#else /* HAVE___PROGNAME */
3fd95d9a 98static const char *__progname = "ssh-agent";
5260325f 99#endif /* HAVE___PROGNAME */
100
8efc0c15 101void
2e73a022 102idtab_init(void)
103{
104 int i;
105 for (i = 0; i <=2; i++){
106 idtable[i].identities = NULL;
107 idtable[i].nentries = 0;
108 }
109}
110
111/* return private key table for requested protocol version */
112Idtab *
113idtab_lookup(int version)
114{
115 if (version < 1 || version > 2)
116 fatal("internal error, bad protocol version %d", version);
117 return &idtable[version];
118}
119
120/* return matching private key for given public key */
121Key *
122lookup_private_key(Key *key, int *idx, int version)
123{
124 int i;
125 Idtab *tab = idtab_lookup(version);
126 for (i = 0; i < tab->nentries; i++) {
127 if (key_equal(key, tab->identities[i].key)) {
128 if (idx != NULL)
129 *idx = i;
130 return tab->identities[i].key;
131 }
132 }
133 return NULL;
134}
135
136/* send list of supported public keys to 'client' */
137void
138process_request_identities(SocketEntry *e, int version)
8efc0c15 139{
2e73a022 140 Idtab *tab = idtab_lookup(version);
5260325f 141 Buffer msg;
142 int i;
143
144 buffer_init(&msg);
2e73a022 145 buffer_put_char(&msg, (version == 1) ?
146 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
147 buffer_put_int(&msg, tab->nentries);
148 for (i = 0; i < tab->nentries; i++) {
149 Identity *id = &tab->identities[i];
150 if (id->key->type == KEY_RSA) {
151 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
152 buffer_put_bignum(&msg, id->key->rsa->e);
153 buffer_put_bignum(&msg, id->key->rsa->n);
154 } else {
155 unsigned char *blob;
156 unsigned int blen;
157 dsa_make_key_blob(id->key, &blob, &blen);
158 buffer_put_string(&msg, blob, blen);
159 xfree(blob);
160 }
161 buffer_put_cstring(&msg, id->comment);
5260325f 162 }
163 buffer_put_int(&e->output, buffer_len(&msg));
164 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
165 buffer_free(&msg);
8efc0c15 166}
167
2e73a022 168/* ssh1 only */
8efc0c15 169void
2e73a022 170process_authentication_challenge1(SocketEntry *e)
8efc0c15 171{
2e73a022 172 Key *key, *private;
173 BIGNUM *challenge;
174 int i, len;
5260325f 175 Buffer msg;
176 MD5_CTX md;
177 unsigned char buf[32], mdbuf[16], session_id[16];
178 unsigned int response_type;
179
180 buffer_init(&msg);
2e73a022 181 key = key_new(KEY_RSA);
5260325f 182 challenge = BN_new();
5260325f 183
2e73a022 184 buffer_get_int(&e->input); /* ignored */
185 buffer_get_bignum(&e->input, key->rsa->e);
186 buffer_get_bignum(&e->input, key->rsa->n);
187 buffer_get_bignum(&e->input, challenge);
5260325f 188
2e73a022 189 /* Only protocol 1.1 is supported */
190 if (buffer_len(&e->input) == 0)
191 goto failure;
192 buffer_get(&e->input, (char *) session_id, 16);
193 response_type = buffer_get_int(&e->input);
194 if (response_type != 1)
195 goto failure;
196
197 private = lookup_private_key(key, NULL, 1);
198 if (private != NULL) {
199 /* Decrypt the challenge using the private key. */
200 rsa_private_decrypt(challenge, challenge, private->rsa);
201
202 /* The response is MD5 of decrypted challenge plus session id. */
203 len = BN_num_bytes(challenge);
204 if (len <= 0 || len > 32) {
205 log("process_authentication_challenge: bad challenge length %d", len);
206 goto failure;
5260325f 207 }
2e73a022 208 memset(buf, 0, 32);
209 BN_bn2bin(challenge, buf + 32 - len);
210 MD5_Init(&md);
211 MD5_Update(&md, buf, 32);
212 MD5_Update(&md, session_id, 16);
213 MD5_Final(mdbuf, &md);
214
215 /* Send the response. */
216 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
217 for (i = 0; i < 16; i++)
218 buffer_put_char(&msg, mdbuf[i]);
219 goto send;
220 }
221
222failure:
223 /* Unknown identity or protocol error. Send failure. */
5260325f 224 buffer_put_char(&msg, SSH_AGENT_FAILURE);
225send:
2e73a022 226 buffer_put_int(&e->output, buffer_len(&msg));
227 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
228 key_free(key);
229 BN_clear_free(challenge);
230 buffer_free(&msg);
231}
232
233/* ssh2 only */
234void
235process_sign_request2(SocketEntry *e)
236{
237 extern int datafellows;
238 Key *key, *private;
239 unsigned char *blob, *data, *signature = NULL;
240 unsigned int blen, dlen, slen = 0;
188adeb2 241 int flags;
2e73a022 242 Buffer msg;
243 int ok = -1;
244
245 datafellows = 0;
246
247 blob = buffer_get_string(&e->input, &blen);
248 data = buffer_get_string(&e->input, &dlen);
188adeb2 249
250 flags = buffer_get_int(&e->input);
251 if (flags & SSH_AGENT_OLD_SIGNATURE)
252 datafellows = SSH_BUG_SIGBLOB;
2e73a022 253
254 key = dsa_key_from_blob(blob, blen);
255 if (key != NULL) {
256 private = lookup_private_key(key, NULL, 2);
257 if (private != NULL)
258 ok = dsa_sign(private, &signature, &slen, data, dlen);
259 }
260 key_free(key);
261 buffer_init(&msg);
262 if (ok == 0) {
263 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
264 buffer_put_string(&msg, signature, slen);
265 } else {
266 buffer_put_char(&msg, SSH_AGENT_FAILURE);
267 }
5260325f 268 buffer_put_int(&e->output, buffer_len(&msg));
269 buffer_append(&e->output, buffer_ptr(&msg),
2e73a022 270 buffer_len(&msg));
5260325f 271 buffer_free(&msg);
2e73a022 272 xfree(data);
273 xfree(blob);
274 if (signature != NULL)
275 xfree(signature);
8efc0c15 276}
277
2e73a022 278/* shared */
8efc0c15 279void
2e73a022 280process_remove_identity(SocketEntry *e, int version)
8efc0c15 281{
2e73a022 282 Key *key = NULL, *private;
283 unsigned char *blob;
284 unsigned int blen;
5260325f 285 unsigned int bits;
2e73a022 286 int success = 0;
287
288 switch(version){
289 case 1:
290 key = key_new(KEY_RSA);
291 bits = buffer_get_int(&e->input);
292 buffer_get_bignum(&e->input, key->rsa->e);
293 buffer_get_bignum(&e->input, key->rsa->n);
294
295 if (bits != key_size(key))
296 log("Warning: identity keysize mismatch: actual %d, announced %d",
297 key_size(key), bits);
298 break;
299 case 2:
300 blob = buffer_get_string(&e->input, &blen);
301 key = dsa_key_from_blob(blob, blen);
302 xfree(blob);
303 break;
304 }
305 if (key != NULL) {
306 int idx;
307 private = lookup_private_key(key, &idx, version);
308 if (private != NULL) {
aa3378df 309 /*
310 * We have this key. Free the old key. Since we
311 * don\'t want to leave empty slots in the middle of
312 * the array, we actually free the key there and copy
313 * data from the last entry.
314 */
2e73a022 315 Idtab *tab = idtab_lookup(version);
316 key_free(tab->identities[idx].key);
317 xfree(tab->identities[idx].comment);
318 if (idx != tab->nentries)
319 tab->identities[idx] = tab->identities[tab->nentries];
320 tab->nentries--;
321 success = 1;
5260325f 322 }
2e73a022 323 key_free(key);
324 }
8efc0c15 325 buffer_put_int(&e->output, 1);
2e73a022 326 buffer_put_char(&e->output,
327 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
8efc0c15 328}
329
8efc0c15 330void
2e73a022 331process_remove_all_identities(SocketEntry *e, int version)
8efc0c15 332{
5260325f 333 unsigned int i;
2e73a022 334 Idtab *tab = idtab_lookup(version);
8efc0c15 335
5260325f 336 /* Loop over all identities and clear the keys. */
2e73a022 337 for (i = 0; i < tab->nentries; i++) {
338 key_free(tab->identities[i].key);
339 xfree(tab->identities[i].comment);
5260325f 340 }
8efc0c15 341
5260325f 342 /* Mark that there are no identities. */
2e73a022 343 tab->nentries = 0;
8efc0c15 344
345 /* Send success. */
346 buffer_put_int(&e->output, 1);
347 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
348 return;
5260325f 349}
350
5260325f 351void
2e73a022 352process_add_identity(SocketEntry *e, int version)
5260325f 353{
2e73a022 354 Key *k = NULL;
355 RSA *rsa;
5260325f 356 BIGNUM *aux;
357 BN_CTX *ctx;
2e73a022 358 char *type;
359 char *comment;
360 int success = 0;
361 Idtab *tab = idtab_lookup(version);
5260325f 362
2e73a022 363 switch (version) {
364 case 1:
365 k = key_new(KEY_RSA);
366 rsa = k->rsa;
5260325f 367
2e73a022 368 /* allocate mem for private key */
369 /* XXX rsa->n and rsa->e are already allocated */
370 rsa->d = BN_new();
371 rsa->iqmp = BN_new();
372 rsa->q = BN_new();
373 rsa->p = BN_new();
374 rsa->dmq1 = BN_new();
375 rsa->dmp1 = BN_new();
376
377 buffer_get_int(&e->input); /* ignored */
378
379 buffer_get_bignum(&e->input, rsa->n);
380 buffer_get_bignum(&e->input, rsa->e);
381 buffer_get_bignum(&e->input, rsa->d);
382 buffer_get_bignum(&e->input, rsa->iqmp);
383
384 /* SSH and SSL have p and q swapped */
385 buffer_get_bignum(&e->input, rsa->q); /* p */
386 buffer_get_bignum(&e->input, rsa->p); /* q */
387
388 /* Generate additional parameters */
389 aux = BN_new();
390 ctx = BN_CTX_new();
391
392 BN_sub(aux, rsa->q, BN_value_one());
393 BN_mod(rsa->dmq1, rsa->d, aux, ctx);
394
395 BN_sub(aux, rsa->p, BN_value_one());
396 BN_mod(rsa->dmp1, rsa->d, aux, ctx);
397
398 BN_clear_free(aux);
399 BN_CTX_free(ctx);
400
401 break;
402 case 2:
403 type = buffer_get_string(&e->input, NULL);
404 if (strcmp(type, KEX_DSS)) {
405 buffer_clear(&e->input);
406 xfree(type);
407 goto send;
5260325f 408 }
2e73a022 409 xfree(type);
5260325f 410
2e73a022 411 k = key_new(KEY_DSA);
412
413 /* allocate mem for private key */
414 k->dsa->priv_key = BN_new();
415
416 buffer_get_bignum2(&e->input, k->dsa->p);
417 buffer_get_bignum2(&e->input, k->dsa->q);
418 buffer_get_bignum2(&e->input, k->dsa->g);
419 buffer_get_bignum2(&e->input, k->dsa->pub_key);
420 buffer_get_bignum2(&e->input, k->dsa->priv_key);
421
422 break;
423 }
424
425 comment = buffer_get_string(&e->input, NULL);
426 if (k == NULL) {
427 xfree(comment);
428 goto send;
429 }
430 success = 1;
431 if (lookup_private_key(k, NULL, version) == NULL) {
432 if (tab->nentries == 0)
433 tab->identities = xmalloc(sizeof(Identity));
434 else
435 tab->identities = xrealloc(tab->identities,
436 (tab->nentries + 1) * sizeof(Identity));
437 tab->identities[tab->nentries].key = k;
438 tab->identities[tab->nentries].comment = comment;
439 /* Increment the number of identities. */
440 tab->nentries++;
441 } else {
442 key_free(k);
443 xfree(comment);
444 }
445send:
5260325f 446 buffer_put_int(&e->output, 1);
2e73a022 447 buffer_put_char(&e->output,
448 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
8efc0c15 449}
450
2e73a022 451/* dispatch incoming messages */
452
8efc0c15 453void
454process_message(SocketEntry *e)
455{
5260325f 456 unsigned int msg_len;
457 unsigned int type;
458 unsigned char *cp;
459 if (buffer_len(&e->input) < 5)
460 return; /* Incomplete message. */
461 cp = (unsigned char *) buffer_ptr(&e->input);
462 msg_len = GET_32BIT(cp);
463 if (msg_len > 256 * 1024) {
464 shutdown(e->fd, SHUT_RDWR);
465 close(e->fd);
466 e->type = AUTH_UNUSED;
467 return;
468 }
469 if (buffer_len(&e->input) < msg_len + 4)
470 return;
471 buffer_consume(&e->input, 4);
472 type = buffer_get_char(&e->input);
473
474 switch (type) {
2e73a022 475 /* ssh1 */
5260325f 476 case SSH_AGENTC_RSA_CHALLENGE:
2e73a022 477 process_authentication_challenge1(e);
478 break;
479 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
480 process_request_identities(e, 1);
5260325f 481 break;
482 case SSH_AGENTC_ADD_RSA_IDENTITY:
2e73a022 483 process_add_identity(e, 1);
5260325f 484 break;
485 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
2e73a022 486 process_remove_identity(e, 1);
5260325f 487 break;
488 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
2e73a022 489 process_remove_all_identities(e, 1);
490 break;
491 /* ssh2 */
492 case SSH2_AGENTC_SIGN_REQUEST:
493 process_sign_request2(e);
494 break;
495 case SSH2_AGENTC_REQUEST_IDENTITIES:
496 process_request_identities(e, 2);
497 break;
498 case SSH2_AGENTC_ADD_IDENTITY:
499 process_add_identity(e, 2);
500 break;
501 case SSH2_AGENTC_REMOVE_IDENTITY:
502 process_remove_identity(e, 2);
503 break;
504 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
505 process_remove_all_identities(e, 2);
5260325f 506 break;
507 default:
508 /* Unknown message. Respond with failure. */
509 error("Unknown message %d", type);
510 buffer_clear(&e->input);
511 buffer_put_int(&e->output, 1);
512 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
513 break;
514 }
8efc0c15 515}
516
517void
518new_socket(int type, int fd)
519{
5260325f 520 unsigned int i, old_alloc;
521 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
522 error("fcntl O_NONBLOCK: %s", strerror(errno));
523
524 if (fd > max_fd)
525 max_fd = fd;
526
527 for (i = 0; i < sockets_alloc; i++)
528 if (sockets[i].type == AUTH_UNUSED) {
529 sockets[i].fd = fd;
530 sockets[i].type = type;
531 buffer_init(&sockets[i].input);
532 buffer_init(&sockets[i].output);
533 return;
534 }
535 old_alloc = sockets_alloc;
536 sockets_alloc += 10;
537 if (sockets)
538 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
539 else
540 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
541 for (i = old_alloc; i < sockets_alloc; i++)
542 sockets[i].type = AUTH_UNUSED;
543 sockets[old_alloc].type = type;
544 sockets[old_alloc].fd = fd;
545 buffer_init(&sockets[old_alloc].input);
546 buffer_init(&sockets[old_alloc].output);
8efc0c15 547}
548
549void
550prepare_select(fd_set *readset, fd_set *writeset)
551{
5260325f 552 unsigned int i;
553 for (i = 0; i < sockets_alloc; i++)
554 switch (sockets[i].type) {
555 case AUTH_SOCKET:
556 case AUTH_CONNECTION:
557 FD_SET(sockets[i].fd, readset);
558 if (buffer_len(&sockets[i].output) > 0)
559 FD_SET(sockets[i].fd, writeset);
560 break;
561 case AUTH_UNUSED:
562 break;
563 default:
564 fatal("Unknown socket type %d", sockets[i].type);
565 break;
566 }
8efc0c15 567}
568
6ae2364d 569void
5260325f 570after_select(fd_set *readset, fd_set *writeset)
8efc0c15 571{
5260325f 572 unsigned int i;
573 int len, sock;
610cd5c6 574 socklen_t slen;
5260325f 575 char buf[1024];
576 struct sockaddr_un sunaddr;
577
578 for (i = 0; i < sockets_alloc; i++)
579 switch (sockets[i].type) {
580 case AUTH_UNUSED:
581 break;
582 case AUTH_SOCKET:
583 if (FD_ISSET(sockets[i].fd, readset)) {
610cd5c6 584 slen = sizeof(sunaddr);
585 sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &slen);
5260325f 586 if (sock < 0) {
587 perror("accept from AUTH_SOCKET");
588 break;
589 }
590 new_socket(AUTH_CONNECTION, sock);
591 }
592 break;
593 case AUTH_CONNECTION:
594 if (buffer_len(&sockets[i].output) > 0 &&
595 FD_ISSET(sockets[i].fd, writeset)) {
596 len = write(sockets[i].fd, buffer_ptr(&sockets[i].output),
597 buffer_len(&sockets[i].output));
598 if (len <= 0) {
599 shutdown(sockets[i].fd, SHUT_RDWR);
600 close(sockets[i].fd);
601 sockets[i].type = AUTH_UNUSED;
0ac7199f 602 buffer_free(&sockets[i].input);
603 buffer_free(&sockets[i].output);
5260325f 604 break;
605 }
606 buffer_consume(&sockets[i].output, len);
607 }
608 if (FD_ISSET(sockets[i].fd, readset)) {
609 len = read(sockets[i].fd, buf, sizeof(buf));
610 if (len <= 0) {
611 shutdown(sockets[i].fd, SHUT_RDWR);
612 close(sockets[i].fd);
613 sockets[i].type = AUTH_UNUSED;
0ac7199f 614 buffer_free(&sockets[i].input);
615 buffer_free(&sockets[i].output);
5260325f 616 break;
617 }
618 buffer_append(&sockets[i].input, buf, len);
619 process_message(&sockets[i]);
620 }
621 break;
622 default:
623 fatal("Unknown type %d", sockets[i].type);
624 }
8efc0c15 625}
626
627void
628check_parent_exists(int sig)
629{
9da5c3c9 630 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
5260325f 631 /* printf("Parent has died - Authentication agent exiting.\n"); */
632 exit(1);
633 }
634 signal(SIGALRM, check_parent_exists);
635 alarm(10);
8efc0c15 636}
637
dae3fa13 638void
639cleanup_socket(void)
640{
77bb0bca 641 unlink(socket_name);
5260325f 642 rmdir(socket_dir);
8efc0c15 643}
644
dae3fa13 645void
646cleanup_exit(int i)
647{
5260325f 648 cleanup_socket();
649 exit(i);
dae3fa13 650}
651
652void
653usage()
654{
5260325f 655 fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
656 fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
657 __progname);
658 exit(1);
dae3fa13 659}
660
8efc0c15 661int
662main(int ac, char **av)
663{
5260325f 664 fd_set readset, writeset;
665 int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
666 struct sockaddr_un sunaddr;
667 pid_t pid;
668 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
c04f75f1 669 extern int optind;
2e73a022 670
264dce47 671 init_rng();
2e73a022 672
5260325f 673 /* check if RSA support exists */
674 if (rsa_alive() == 0) {
675 fprintf(stderr,
676 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
677 __progname);
678 exit(1);
679 }
4e577b89 680#ifdef __GNU_LIBRARY__
681 while ((ch = getopt(ac, av, "+cks")) != -1) {
682#else /* __GNU_LIBRARY__ */
5260325f 683 while ((ch = getopt(ac, av, "cks")) != -1) {
4e577b89 684#endif /* __GNU_LIBRARY__ */
5260325f 685 switch (ch) {
686 case 'c':
687 if (s_flag)
688 usage();
689 c_flag++;
690 break;
691 case 'k':
692 k_flag++;
693 break;
694 case 's':
695 if (c_flag)
696 usage();
697 s_flag++;
698 break;
699 default:
700 usage();
701 }
dae3fa13 702 }
5260325f 703 ac -= optind;
704 av += optind;
705
706 if (ac > 0 && (c_flag || k_flag || s_flag))
707 usage();
708
709 if (ac == 0 && !c_flag && !k_flag && !s_flag) {
710 shell = getenv("SHELL");
711 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
712 c_flag = 1;
dae3fa13 713 }
5260325f 714 if (k_flag) {
715 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
716 if (pidstr == NULL) {
717 fprintf(stderr, "%s not set, cannot kill agent\n",
718 SSH_AGENTPID_ENV_NAME);
719 exit(1);
720 }
721 pid = atoi(pidstr);
722 if (pid < 1) { /* XXX PID_MAX check too */
9da5c3c9 723 /* Yes, PID_MAX check please */
5260325f 724 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
725 SSH_AGENTPID_ENV_NAME, pidstr);
726 exit(1);
727 }
728 if (kill(pid, SIGTERM) == -1) {
729 perror("kill");
730 exit(1);
731 }
732 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
733 printf(format, SSH_AUTHSOCKET_ENV_NAME);
734 printf(format, SSH_AGENTPID_ENV_NAME);
735 printf("echo Agent pid %d killed;\n", pid);
736 exit(0);
dae3fa13 737 }
5260325f 738 parent_pid = getpid();
739
740 /* Create private directory for agent socket */
741 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
742 if (mkdtemp(socket_dir) == NULL) {
743 perror("mkdtemp: private socket dir");
744 exit(1);
745 }
746 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
747 parent_pid);
748
aa3378df 749 /*
750 * Create socket early so it will exist before command gets run from
751 * the parent.
752 */
5260325f 753 sock = socket(AF_UNIX, SOCK_STREAM, 0);
754 if (sock < 0) {
755 perror("socket");
756 cleanup_exit(1);
757 }
758 memset(&sunaddr, 0, sizeof(sunaddr));
759 sunaddr.sun_family = AF_UNIX;
760 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
761 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
762 perror("bind");
763 cleanup_exit(1);
764 }
765 if (listen(sock, 5) < 0) {
766 perror("listen");
767 cleanup_exit(1);
dae3fa13 768 }
aa3378df 769 /*
770 * Fork, and have the parent execute the command, if any, or present
771 * the socket data. The child continues as the authentication agent.
772 */
5260325f 773 pid = fork();
774 if (pid == -1) {
775 perror("fork");
776 exit(1);
dae3fa13 777 }
5260325f 778 if (pid != 0) { /* Parent - execute the given command. */
779 close(sock);
780 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
781 if (ac == 0) {
782 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
783 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
784 SSH_AUTHSOCKET_ENV_NAME);
785 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
786 SSH_AGENTPID_ENV_NAME);
787 printf("echo Agent pid %d;\n", pid);
788 exit(0);
789 }
bcbf86ec 790 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
791 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
792 perror("setenv");
793 exit(1);
794 }
5260325f 795 execvp(av[0], av);
796 perror(av[0]);
797 exit(1);
798 }
799 close(0);
800 close(1);
801 close(2);
dae3fa13 802
5260325f 803 if (setsid() == -1) {
804 perror("setsid");
805 cleanup_exit(1);
806 }
807 if (atexit(cleanup_socket) < 0) {
808 perror("atexit");
809 cleanup_exit(1);
810 }
811 new_socket(AUTH_SOCKET, sock);
812 if (ac > 0) {
813 signal(SIGALRM, check_parent_exists);
814 alarm(10);
815 }
2e73a022 816 idtab_init();
5260325f 817 signal(SIGINT, SIG_IGN);
818 signal(SIGPIPE, SIG_IGN);
6ae2364d 819 signal(SIGHUP, cleanup_exit);
820 signal(SIGTERM, cleanup_exit);
5260325f 821 while (1) {
822 FD_ZERO(&readset);
823 FD_ZERO(&writeset);
824 prepare_select(&readset, &writeset);
825 if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) {
826 if (errno == EINTR)
827 continue;
828 exit(1);
829 }
830 after_select(&readset, &writeset);
8efc0c15 831 }
5260325f 832 /* NOTREACHED */
8efc0c15 833}
This page took 3.10798 seconds and 5 git commands to generate.