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