]>
Commit | Line | Data |
---|---|---|
bcbf86ec | 1 | /* $OpenBSD: ssh-agent.c,v 1.36 2000/09/15 07:13:49 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" | |
bcbf86ec | 40 | RCSID("$OpenBSD: ssh-agent.c,v 1.36 2000/09/15 07:13:49 deraadt 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" | |
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; | |
96 | #else /* HAVE___PROGNAME */ | |
3fd95d9a | 97 | static const char *__progname = "ssh-agent"; |
5260325f | 98 | #endif /* HAVE___PROGNAME */ |
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]; | |
149 | if (id->key->type == KEY_RSA) { | |
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; | |
156 | dsa_make_key_blob(id->key, &blob, &blen); | |
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); | |
2e73a022 | 180 | key = key_new(KEY_RSA); |
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; | |
240 | Buffer msg; | |
241 | int ok = -1; | |
242 | ||
243 | datafellows = 0; | |
244 | ||
245 | blob = buffer_get_string(&e->input, &blen); | |
246 | data = buffer_get_string(&e->input, &dlen); | |
b5e300c2 | 247 | buffer_get_int(&e->input); /* flags, unused */ |
2e73a022 | 248 | |
249 | key = dsa_key_from_blob(blob, blen); | |
250 | if (key != NULL) { | |
251 | private = lookup_private_key(key, NULL, 2); | |
252 | if (private != NULL) | |
253 | ok = dsa_sign(private, &signature, &slen, data, dlen); | |
254 | } | |
255 | key_free(key); | |
256 | buffer_init(&msg); | |
257 | if (ok == 0) { | |
258 | buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); | |
259 | buffer_put_string(&msg, signature, slen); | |
260 | } else { | |
261 | buffer_put_char(&msg, SSH_AGENT_FAILURE); | |
262 | } | |
5260325f | 263 | buffer_put_int(&e->output, buffer_len(&msg)); |
264 | buffer_append(&e->output, buffer_ptr(&msg), | |
2e73a022 | 265 | buffer_len(&msg)); |
5260325f | 266 | buffer_free(&msg); |
2e73a022 | 267 | xfree(data); |
268 | xfree(blob); | |
269 | if (signature != NULL) | |
270 | xfree(signature); | |
8efc0c15 | 271 | } |
272 | ||
2e73a022 | 273 | /* shared */ |
8efc0c15 | 274 | void |
2e73a022 | 275 | process_remove_identity(SocketEntry *e, int version) |
8efc0c15 | 276 | { |
2e73a022 | 277 | Key *key = NULL, *private; |
278 | unsigned char *blob; | |
279 | unsigned int blen; | |
5260325f | 280 | unsigned int bits; |
2e73a022 | 281 | int success = 0; |
282 | ||
283 | switch(version){ | |
284 | case 1: | |
285 | key = key_new(KEY_RSA); | |
286 | bits = buffer_get_int(&e->input); | |
287 | buffer_get_bignum(&e->input, key->rsa->e); | |
288 | buffer_get_bignum(&e->input, key->rsa->n); | |
289 | ||
290 | if (bits != key_size(key)) | |
291 | log("Warning: identity keysize mismatch: actual %d, announced %d", | |
292 | key_size(key), bits); | |
293 | break; | |
294 | case 2: | |
295 | blob = buffer_get_string(&e->input, &blen); | |
296 | key = dsa_key_from_blob(blob, blen); | |
297 | xfree(blob); | |
298 | break; | |
299 | } | |
300 | if (key != NULL) { | |
301 | int idx; | |
302 | private = lookup_private_key(key, &idx, version); | |
303 | if (private != NULL) { | |
aa3378df | 304 | /* |
305 | * We have this key. Free the old key. Since we | |
306 | * don\'t want to leave empty slots in the middle of | |
307 | * the array, we actually free the key there and copy | |
308 | * data from the last entry. | |
309 | */ | |
2e73a022 | 310 | Idtab *tab = idtab_lookup(version); |
311 | key_free(tab->identities[idx].key); | |
312 | xfree(tab->identities[idx].comment); | |
313 | if (idx != tab->nentries) | |
314 | tab->identities[idx] = tab->identities[tab->nentries]; | |
315 | tab->nentries--; | |
316 | success = 1; | |
5260325f | 317 | } |
2e73a022 | 318 | key_free(key); |
319 | } | |
8efc0c15 | 320 | buffer_put_int(&e->output, 1); |
2e73a022 | 321 | buffer_put_char(&e->output, |
322 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
8efc0c15 | 323 | } |
324 | ||
8efc0c15 | 325 | void |
2e73a022 | 326 | process_remove_all_identities(SocketEntry *e, int version) |
8efc0c15 | 327 | { |
5260325f | 328 | unsigned int i; |
2e73a022 | 329 | Idtab *tab = idtab_lookup(version); |
8efc0c15 | 330 | |
5260325f | 331 | /* Loop over all identities and clear the keys. */ |
2e73a022 | 332 | for (i = 0; i < tab->nentries; i++) { |
333 | key_free(tab->identities[i].key); | |
334 | xfree(tab->identities[i].comment); | |
5260325f | 335 | } |
8efc0c15 | 336 | |
5260325f | 337 | /* Mark that there are no identities. */ |
2e73a022 | 338 | tab->nentries = 0; |
8efc0c15 | 339 | |
340 | /* Send success. */ | |
341 | buffer_put_int(&e->output, 1); | |
342 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); | |
343 | return; | |
5260325f | 344 | } |
345 | ||
5260325f | 346 | void |
2e73a022 | 347 | process_add_identity(SocketEntry *e, int version) |
5260325f | 348 | { |
2e73a022 | 349 | Key *k = NULL; |
350 | RSA *rsa; | |
5260325f | 351 | BIGNUM *aux; |
352 | BN_CTX *ctx; | |
2e73a022 | 353 | char *type; |
354 | char *comment; | |
355 | int success = 0; | |
356 | Idtab *tab = idtab_lookup(version); | |
5260325f | 357 | |
2e73a022 | 358 | switch (version) { |
359 | case 1: | |
360 | k = key_new(KEY_RSA); | |
361 | rsa = k->rsa; | |
5260325f | 362 | |
2e73a022 | 363 | /* allocate mem for private key */ |
364 | /* XXX rsa->n and rsa->e are already allocated */ | |
365 | rsa->d = BN_new(); | |
366 | rsa->iqmp = BN_new(); | |
367 | rsa->q = BN_new(); | |
368 | rsa->p = BN_new(); | |
369 | rsa->dmq1 = BN_new(); | |
370 | rsa->dmp1 = BN_new(); | |
371 | ||
372 | buffer_get_int(&e->input); /* ignored */ | |
373 | ||
374 | buffer_get_bignum(&e->input, rsa->n); | |
375 | buffer_get_bignum(&e->input, rsa->e); | |
376 | buffer_get_bignum(&e->input, rsa->d); | |
377 | buffer_get_bignum(&e->input, rsa->iqmp); | |
378 | ||
379 | /* SSH and SSL have p and q swapped */ | |
380 | buffer_get_bignum(&e->input, rsa->q); /* p */ | |
381 | buffer_get_bignum(&e->input, rsa->p); /* q */ | |
382 | ||
383 | /* Generate additional parameters */ | |
384 | aux = BN_new(); | |
385 | ctx = BN_CTX_new(); | |
386 | ||
387 | BN_sub(aux, rsa->q, BN_value_one()); | |
388 | BN_mod(rsa->dmq1, rsa->d, aux, ctx); | |
389 | ||
390 | BN_sub(aux, rsa->p, BN_value_one()); | |
391 | BN_mod(rsa->dmp1, rsa->d, aux, ctx); | |
392 | ||
393 | BN_clear_free(aux); | |
394 | BN_CTX_free(ctx); | |
395 | ||
396 | break; | |
397 | case 2: | |
398 | type = buffer_get_string(&e->input, NULL); | |
399 | if (strcmp(type, KEX_DSS)) { | |
400 | buffer_clear(&e->input); | |
401 | xfree(type); | |
402 | goto send; | |
5260325f | 403 | } |
2e73a022 | 404 | xfree(type); |
5260325f | 405 | |
2e73a022 | 406 | k = key_new(KEY_DSA); |
407 | ||
408 | /* allocate mem for private key */ | |
409 | k->dsa->priv_key = BN_new(); | |
410 | ||
411 | buffer_get_bignum2(&e->input, k->dsa->p); | |
412 | buffer_get_bignum2(&e->input, k->dsa->q); | |
413 | buffer_get_bignum2(&e->input, k->dsa->g); | |
414 | buffer_get_bignum2(&e->input, k->dsa->pub_key); | |
415 | buffer_get_bignum2(&e->input, k->dsa->priv_key); | |
416 | ||
417 | break; | |
418 | } | |
419 | ||
420 | comment = buffer_get_string(&e->input, NULL); | |
421 | if (k == NULL) { | |
422 | xfree(comment); | |
423 | goto send; | |
424 | } | |
425 | success = 1; | |
426 | if (lookup_private_key(k, NULL, version) == NULL) { | |
427 | if (tab->nentries == 0) | |
428 | tab->identities = xmalloc(sizeof(Identity)); | |
429 | else | |
430 | tab->identities = xrealloc(tab->identities, | |
431 | (tab->nentries + 1) * sizeof(Identity)); | |
432 | tab->identities[tab->nentries].key = k; | |
433 | tab->identities[tab->nentries].comment = comment; | |
434 | /* Increment the number of identities. */ | |
435 | tab->nentries++; | |
436 | } else { | |
437 | key_free(k); | |
438 | xfree(comment); | |
439 | } | |
440 | send: | |
5260325f | 441 | buffer_put_int(&e->output, 1); |
2e73a022 | 442 | buffer_put_char(&e->output, |
443 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
8efc0c15 | 444 | } |
445 | ||
2e73a022 | 446 | /* dispatch incoming messages */ |
447 | ||
8efc0c15 | 448 | void |
449 | process_message(SocketEntry *e) | |
450 | { | |
5260325f | 451 | unsigned int msg_len; |
452 | unsigned int type; | |
453 | unsigned char *cp; | |
454 | if (buffer_len(&e->input) < 5) | |
455 | return; /* Incomplete message. */ | |
456 | cp = (unsigned char *) buffer_ptr(&e->input); | |
457 | msg_len = GET_32BIT(cp); | |
458 | if (msg_len > 256 * 1024) { | |
459 | shutdown(e->fd, SHUT_RDWR); | |
460 | close(e->fd); | |
461 | e->type = AUTH_UNUSED; | |
462 | return; | |
463 | } | |
464 | if (buffer_len(&e->input) < msg_len + 4) | |
465 | return; | |
466 | buffer_consume(&e->input, 4); | |
467 | type = buffer_get_char(&e->input); | |
468 | ||
469 | switch (type) { | |
2e73a022 | 470 | /* ssh1 */ |
5260325f | 471 | case SSH_AGENTC_RSA_CHALLENGE: |
2e73a022 | 472 | process_authentication_challenge1(e); |
473 | break; | |
474 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: | |
475 | process_request_identities(e, 1); | |
5260325f | 476 | break; |
477 | case SSH_AGENTC_ADD_RSA_IDENTITY: | |
2e73a022 | 478 | process_add_identity(e, 1); |
5260325f | 479 | break; |
480 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: | |
2e73a022 | 481 | process_remove_identity(e, 1); |
5260325f | 482 | break; |
483 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: | |
2e73a022 | 484 | process_remove_all_identities(e, 1); |
485 | break; | |
486 | /* ssh2 */ | |
487 | case SSH2_AGENTC_SIGN_REQUEST: | |
488 | process_sign_request2(e); | |
489 | break; | |
490 | case SSH2_AGENTC_REQUEST_IDENTITIES: | |
491 | process_request_identities(e, 2); | |
492 | break; | |
493 | case SSH2_AGENTC_ADD_IDENTITY: | |
494 | process_add_identity(e, 2); | |
495 | break; | |
496 | case SSH2_AGENTC_REMOVE_IDENTITY: | |
497 | process_remove_identity(e, 2); | |
498 | break; | |
499 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: | |
500 | process_remove_all_identities(e, 2); | |
5260325f | 501 | break; |
502 | default: | |
503 | /* Unknown message. Respond with failure. */ | |
504 | error("Unknown message %d", type); | |
505 | buffer_clear(&e->input); | |
506 | buffer_put_int(&e->output, 1); | |
507 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); | |
508 | break; | |
509 | } | |
8efc0c15 | 510 | } |
511 | ||
512 | void | |
513 | new_socket(int type, int fd) | |
514 | { | |
5260325f | 515 | unsigned int i, old_alloc; |
516 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) | |
517 | error("fcntl O_NONBLOCK: %s", strerror(errno)); | |
518 | ||
519 | if (fd > max_fd) | |
520 | max_fd = fd; | |
521 | ||
522 | for (i = 0; i < sockets_alloc; i++) | |
523 | if (sockets[i].type == AUTH_UNUSED) { | |
524 | sockets[i].fd = fd; | |
525 | sockets[i].type = type; | |
526 | buffer_init(&sockets[i].input); | |
527 | buffer_init(&sockets[i].output); | |
528 | return; | |
529 | } | |
530 | old_alloc = sockets_alloc; | |
531 | sockets_alloc += 10; | |
532 | if (sockets) | |
533 | sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); | |
534 | else | |
535 | sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); | |
536 | for (i = old_alloc; i < sockets_alloc; i++) | |
537 | sockets[i].type = AUTH_UNUSED; | |
538 | sockets[old_alloc].type = type; | |
539 | sockets[old_alloc].fd = fd; | |
540 | buffer_init(&sockets[old_alloc].input); | |
541 | buffer_init(&sockets[old_alloc].output); | |
8efc0c15 | 542 | } |
543 | ||
544 | void | |
545 | prepare_select(fd_set *readset, fd_set *writeset) | |
546 | { | |
5260325f | 547 | unsigned int i; |
548 | for (i = 0; i < sockets_alloc; i++) | |
549 | switch (sockets[i].type) { | |
550 | case AUTH_SOCKET: | |
551 | case AUTH_CONNECTION: | |
552 | FD_SET(sockets[i].fd, readset); | |
553 | if (buffer_len(&sockets[i].output) > 0) | |
554 | FD_SET(sockets[i].fd, writeset); | |
555 | break; | |
556 | case AUTH_UNUSED: | |
557 | break; | |
558 | default: | |
559 | fatal("Unknown socket type %d", sockets[i].type); | |
560 | break; | |
561 | } | |
8efc0c15 | 562 | } |
563 | ||
6ae2364d | 564 | void |
5260325f | 565 | after_select(fd_set *readset, fd_set *writeset) |
8efc0c15 | 566 | { |
5260325f | 567 | unsigned int i; |
568 | int len, sock; | |
610cd5c6 | 569 | socklen_t slen; |
5260325f | 570 | char buf[1024]; |
571 | struct sockaddr_un sunaddr; | |
572 | ||
573 | for (i = 0; i < sockets_alloc; i++) | |
574 | switch (sockets[i].type) { | |
575 | case AUTH_UNUSED: | |
576 | break; | |
577 | case AUTH_SOCKET: | |
578 | if (FD_ISSET(sockets[i].fd, readset)) { | |
610cd5c6 | 579 | slen = sizeof(sunaddr); |
580 | sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &slen); | |
5260325f | 581 | if (sock < 0) { |
582 | perror("accept from AUTH_SOCKET"); | |
583 | break; | |
584 | } | |
585 | new_socket(AUTH_CONNECTION, sock); | |
586 | } | |
587 | break; | |
588 | case AUTH_CONNECTION: | |
589 | if (buffer_len(&sockets[i].output) > 0 && | |
590 | FD_ISSET(sockets[i].fd, writeset)) { | |
591 | len = write(sockets[i].fd, buffer_ptr(&sockets[i].output), | |
592 | buffer_len(&sockets[i].output)); | |
593 | if (len <= 0) { | |
594 | shutdown(sockets[i].fd, SHUT_RDWR); | |
595 | close(sockets[i].fd); | |
596 | sockets[i].type = AUTH_UNUSED; | |
0ac7199f | 597 | buffer_free(&sockets[i].input); |
598 | buffer_free(&sockets[i].output); | |
5260325f | 599 | break; |
600 | } | |
601 | buffer_consume(&sockets[i].output, len); | |
602 | } | |
603 | if (FD_ISSET(sockets[i].fd, readset)) { | |
604 | len = read(sockets[i].fd, buf, sizeof(buf)); | |
605 | if (len <= 0) { | |
606 | shutdown(sockets[i].fd, SHUT_RDWR); | |
607 | close(sockets[i].fd); | |
608 | sockets[i].type = AUTH_UNUSED; | |
0ac7199f | 609 | buffer_free(&sockets[i].input); |
610 | buffer_free(&sockets[i].output); | |
5260325f | 611 | break; |
612 | } | |
613 | buffer_append(&sockets[i].input, buf, len); | |
614 | process_message(&sockets[i]); | |
615 | } | |
616 | break; | |
617 | default: | |
618 | fatal("Unknown type %d", sockets[i].type); | |
619 | } | |
8efc0c15 | 620 | } |
621 | ||
622 | void | |
623 | check_parent_exists(int sig) | |
624 | { | |
9da5c3c9 | 625 | if (parent_pid != -1 && kill(parent_pid, 0) < 0) { |
5260325f | 626 | /* printf("Parent has died - Authentication agent exiting.\n"); */ |
627 | exit(1); | |
628 | } | |
629 | signal(SIGALRM, check_parent_exists); | |
630 | alarm(10); | |
8efc0c15 | 631 | } |
632 | ||
dae3fa13 | 633 | void |
634 | cleanup_socket(void) | |
635 | { | |
5260325f | 636 | remove(socket_name); |
637 | rmdir(socket_dir); | |
8efc0c15 | 638 | } |
639 | ||
dae3fa13 | 640 | void |
641 | cleanup_exit(int i) | |
642 | { | |
5260325f | 643 | cleanup_socket(); |
644 | exit(i); | |
dae3fa13 | 645 | } |
646 | ||
647 | void | |
648 | usage() | |
649 | { | |
5260325f | 650 | fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION); |
651 | fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n", | |
652 | __progname); | |
653 | exit(1); | |
dae3fa13 | 654 | } |
655 | ||
8efc0c15 | 656 | int |
657 | main(int ac, char **av) | |
658 | { | |
5260325f | 659 | fd_set readset, writeset; |
660 | int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch; | |
661 | struct sockaddr_un sunaddr; | |
662 | pid_t pid; | |
663 | char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid]; | |
c04f75f1 | 664 | extern int optind; |
2e73a022 | 665 | |
264dce47 | 666 | init_rng(); |
2e73a022 | 667 | |
5260325f | 668 | /* check if RSA support exists */ |
669 | if (rsa_alive() == 0) { | |
670 | fprintf(stderr, | |
671 | "%s: no RSA support in libssl and libcrypto. See ssl(8).\n", | |
672 | __progname); | |
673 | exit(1); | |
674 | } | |
4e577b89 | 675 | #ifdef __GNU_LIBRARY__ |
676 | while ((ch = getopt(ac, av, "+cks")) != -1) { | |
677 | #else /* __GNU_LIBRARY__ */ | |
5260325f | 678 | while ((ch = getopt(ac, av, "cks")) != -1) { |
4e577b89 | 679 | #endif /* __GNU_LIBRARY__ */ |
5260325f | 680 | switch (ch) { |
681 | case 'c': | |
682 | if (s_flag) | |
683 | usage(); | |
684 | c_flag++; | |
685 | break; | |
686 | case 'k': | |
687 | k_flag++; | |
688 | break; | |
689 | case 's': | |
690 | if (c_flag) | |
691 | usage(); | |
692 | s_flag++; | |
693 | break; | |
694 | default: | |
695 | usage(); | |
696 | } | |
dae3fa13 | 697 | } |
5260325f | 698 | ac -= optind; |
699 | av += optind; | |
700 | ||
701 | if (ac > 0 && (c_flag || k_flag || s_flag)) | |
702 | usage(); | |
703 | ||
704 | if (ac == 0 && !c_flag && !k_flag && !s_flag) { | |
705 | shell = getenv("SHELL"); | |
706 | if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) | |
707 | c_flag = 1; | |
dae3fa13 | 708 | } |
5260325f | 709 | if (k_flag) { |
710 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); | |
711 | if (pidstr == NULL) { | |
712 | fprintf(stderr, "%s not set, cannot kill agent\n", | |
713 | SSH_AGENTPID_ENV_NAME); | |
714 | exit(1); | |
715 | } | |
716 | pid = atoi(pidstr); | |
717 | if (pid < 1) { /* XXX PID_MAX check too */ | |
9da5c3c9 | 718 | /* Yes, PID_MAX check please */ |
5260325f | 719 | fprintf(stderr, "%s=\"%s\", which is not a good PID\n", |
720 | SSH_AGENTPID_ENV_NAME, pidstr); | |
721 | exit(1); | |
722 | } | |
723 | if (kill(pid, SIGTERM) == -1) { | |
724 | perror("kill"); | |
725 | exit(1); | |
726 | } | |
727 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; | |
728 | printf(format, SSH_AUTHSOCKET_ENV_NAME); | |
729 | printf(format, SSH_AGENTPID_ENV_NAME); | |
730 | printf("echo Agent pid %d killed;\n", pid); | |
731 | exit(0); | |
dae3fa13 | 732 | } |
5260325f | 733 | parent_pid = getpid(); |
734 | ||
735 | /* Create private directory for agent socket */ | |
736 | strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir); | |
737 | if (mkdtemp(socket_dir) == NULL) { | |
738 | perror("mkdtemp: private socket dir"); | |
739 | exit(1); | |
740 | } | |
741 | snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir, | |
742 | parent_pid); | |
743 | ||
aa3378df | 744 | /* |
745 | * Create socket early so it will exist before command gets run from | |
746 | * the parent. | |
747 | */ | |
5260325f | 748 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
749 | if (sock < 0) { | |
750 | perror("socket"); | |
751 | cleanup_exit(1); | |
752 | } | |
753 | memset(&sunaddr, 0, sizeof(sunaddr)); | |
754 | sunaddr.sun_family = AF_UNIX; | |
755 | strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); | |
756 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { | |
757 | perror("bind"); | |
758 | cleanup_exit(1); | |
759 | } | |
760 | if (listen(sock, 5) < 0) { | |
761 | perror("listen"); | |
762 | cleanup_exit(1); | |
dae3fa13 | 763 | } |
aa3378df | 764 | /* |
765 | * Fork, and have the parent execute the command, if any, or present | |
766 | * the socket data. The child continues as the authentication agent. | |
767 | */ | |
5260325f | 768 | pid = fork(); |
769 | if (pid == -1) { | |
770 | perror("fork"); | |
771 | exit(1); | |
dae3fa13 | 772 | } |
5260325f | 773 | if (pid != 0) { /* Parent - execute the given command. */ |
774 | close(sock); | |
775 | snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid); | |
776 | if (ac == 0) { | |
777 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; | |
778 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, | |
779 | SSH_AUTHSOCKET_ENV_NAME); | |
780 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, | |
781 | SSH_AGENTPID_ENV_NAME); | |
782 | printf("echo Agent pid %d;\n", pid); | |
783 | exit(0); | |
784 | } | |
bcbf86ec | 785 | if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || |
786 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { | |
787 | perror("setenv"); | |
788 | exit(1); | |
789 | } | |
5260325f | 790 | execvp(av[0], av); |
791 | perror(av[0]); | |
792 | exit(1); | |
793 | } | |
794 | close(0); | |
795 | close(1); | |
796 | close(2); | |
dae3fa13 | 797 | |
5260325f | 798 | if (setsid() == -1) { |
799 | perror("setsid"); | |
800 | cleanup_exit(1); | |
801 | } | |
802 | if (atexit(cleanup_socket) < 0) { | |
803 | perror("atexit"); | |
804 | cleanup_exit(1); | |
805 | } | |
806 | new_socket(AUTH_SOCKET, sock); | |
807 | if (ac > 0) { | |
808 | signal(SIGALRM, check_parent_exists); | |
809 | alarm(10); | |
810 | } | |
2e73a022 | 811 | idtab_init(); |
5260325f | 812 | signal(SIGINT, SIG_IGN); |
813 | signal(SIGPIPE, SIG_IGN); | |
6ae2364d | 814 | signal(SIGHUP, cleanup_exit); |
815 | signal(SIGTERM, cleanup_exit); | |
5260325f | 816 | while (1) { |
817 | FD_ZERO(&readset); | |
818 | FD_ZERO(&writeset); | |
819 | prepare_select(&readset, &writeset); | |
820 | if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) { | |
821 | if (errno == EINTR) | |
822 | continue; | |
823 | exit(1); | |
824 | } | |
825 | after_select(&readset, &writeset); | |
8efc0c15 | 826 | } |
5260325f | 827 | /* NOTREACHED */ |
8efc0c15 | 828 | } |