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