]>
Commit | Line | Data |
---|---|---|
894c5fa6 | 1 | /* $OpenBSD: ssh-agent.c,v 1.54 2001/04/03 13:56:11 stevesk 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, |
f3c7c613 | 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" | |
894c5fa6 | 40 | RCSID("$OpenBSD: ssh-agent.c,v 1.54 2001/04/03 13:56:11 stevesk 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 | 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 | ||
1e3b8b07 | 69 | u_int sockets_alloc = 0; |
8efc0c15 | 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 | |
42f11eb2 | 100 | int prepare_select(fd_set **, fd_set **, int *); |
101 | ||
8efc0c15 | 102 | void |
2e73a022 | 103 | idtab_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 */ | |
113 | Idtab * | |
114 | idtab_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 */ | |
122 | Key * | |
123 | lookup_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' */ | |
138 | void | |
139 | process_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 | 170 | void |
2e73a022 | 171 | process_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. */ | |
46aa2d1f | 201 | if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) |
202 | goto failure; | |
2e73a022 | 203 | |
204 | /* The response is MD5 of decrypted challenge plus session id. */ | |
205 | len = BN_num_bytes(challenge); | |
206 | if (len <= 0 || len > 32) { | |
207 | log("process_authentication_challenge: bad challenge length %d", len); | |
208 | goto failure; | |
5260325f | 209 | } |
2e73a022 | 210 | memset(buf, 0, 32); |
211 | BN_bn2bin(challenge, buf + 32 - len); | |
212 | MD5_Init(&md); | |
213 | MD5_Update(&md, buf, 32); | |
214 | MD5_Update(&md, session_id, 16); | |
215 | MD5_Final(mdbuf, &md); | |
216 | ||
217 | /* Send the response. */ | |
218 | buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); | |
219 | for (i = 0; i < 16; i++) | |
220 | buffer_put_char(&msg, mdbuf[i]); | |
221 | goto send; | |
222 | } | |
223 | ||
224 | failure: | |
225 | /* Unknown identity or protocol error. Send failure. */ | |
5260325f | 226 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
227 | send: | |
2e73a022 | 228 | buffer_put_int(&e->output, buffer_len(&msg)); |
229 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); | |
230 | key_free(key); | |
231 | BN_clear_free(challenge); | |
232 | buffer_free(&msg); | |
233 | } | |
234 | ||
235 | /* ssh2 only */ | |
236 | void | |
237 | process_sign_request2(SocketEntry *e) | |
238 | { | |
239 | extern int datafellows; | |
240 | Key *key, *private; | |
1e3b8b07 | 241 | u_char *blob, *data, *signature = NULL; |
242 | u_int blen, dlen, slen = 0; | |
188adeb2 | 243 | int flags; |
2e73a022 | 244 | Buffer msg; |
245 | int ok = -1; | |
246 | ||
247 | datafellows = 0; | |
227e8e86 | 248 | |
2e73a022 | 249 | blob = buffer_get_string(&e->input, &blen); |
250 | data = buffer_get_string(&e->input, &dlen); | |
188adeb2 | 251 | |
252 | flags = buffer_get_int(&e->input); | |
253 | if (flags & SSH_AGENT_OLD_SIGNATURE) | |
254 | datafellows = SSH_BUG_SIGBLOB; | |
2e73a022 | 255 | |
fa08c86b | 256 | key = key_from_blob(blob, blen); |
2e73a022 | 257 | if (key != NULL) { |
258 | private = lookup_private_key(key, NULL, 2); | |
259 | if (private != NULL) | |
fa08c86b | 260 | ok = key_sign(private, &signature, &slen, data, dlen); |
2e73a022 | 261 | } |
262 | key_free(key); | |
263 | buffer_init(&msg); | |
264 | if (ok == 0) { | |
265 | buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); | |
266 | buffer_put_string(&msg, signature, slen); | |
267 | } else { | |
268 | buffer_put_char(&msg, SSH_AGENT_FAILURE); | |
269 | } | |
5260325f | 270 | buffer_put_int(&e->output, buffer_len(&msg)); |
271 | buffer_append(&e->output, buffer_ptr(&msg), | |
2e73a022 | 272 | buffer_len(&msg)); |
5260325f | 273 | buffer_free(&msg); |
2e73a022 | 274 | xfree(data); |
275 | xfree(blob); | |
276 | if (signature != NULL) | |
277 | xfree(signature); | |
8efc0c15 | 278 | } |
279 | ||
2e73a022 | 280 | /* shared */ |
8efc0c15 | 281 | void |
2e73a022 | 282 | process_remove_identity(SocketEntry *e, int version) |
8efc0c15 | 283 | { |
2e73a022 | 284 | Key *key = NULL, *private; |
1e3b8b07 | 285 | u_char *blob; |
286 | u_int blen; | |
287 | u_int bits; | |
2e73a022 | 288 | int success = 0; |
289 | ||
290 | switch(version){ | |
291 | case 1: | |
fa08c86b | 292 | key = key_new(KEY_RSA1); |
2e73a022 | 293 | bits = buffer_get_int(&e->input); |
294 | buffer_get_bignum(&e->input, key->rsa->e); | |
295 | buffer_get_bignum(&e->input, key->rsa->n); | |
296 | ||
297 | if (bits != key_size(key)) | |
298 | log("Warning: identity keysize mismatch: actual %d, announced %d", | |
42f11eb2 | 299 | key_size(key), bits); |
2e73a022 | 300 | break; |
301 | case 2: | |
302 | blob = buffer_get_string(&e->input, &blen); | |
fa08c86b | 303 | key = key_from_blob(blob, blen); |
2e73a022 | 304 | xfree(blob); |
305 | break; | |
306 | } | |
307 | if (key != NULL) { | |
308 | int idx; | |
309 | private = lookup_private_key(key, &idx, version); | |
310 | if (private != NULL) { | |
aa3378df | 311 | /* |
312 | * We have this key. Free the old key. Since we | |
313 | * don\'t want to leave empty slots in the middle of | |
d343d900 | 314 | * the array, we actually free the key there and move |
315 | * all the entries between the empty slot and the end | |
316 | * of the array. | |
aa3378df | 317 | */ |
2e73a022 | 318 | Idtab *tab = idtab_lookup(version); |
319 | key_free(tab->identities[idx].key); | |
320 | xfree(tab->identities[idx].comment); | |
fa08c86b | 321 | if (tab->nentries < 1) |
322 | fatal("process_remove_identity: " | |
323 | "internal error: tab->nentries %d", | |
324 | tab->nentries); | |
d343d900 | 325 | if (idx != tab->nentries - 1) { |
326 | int i; | |
327 | for (i = idx; i < tab->nentries - 1; i++) | |
328 | tab->identities[i] = tab->identities[i+1]; | |
329 | } | |
330 | tab->identities[tab->nentries - 1].key = NULL; | |
331 | tab->identities[tab->nentries - 1].comment = NULL; | |
2e73a022 | 332 | tab->nentries--; |
333 | success = 1; | |
5260325f | 334 | } |
2e73a022 | 335 | key_free(key); |
336 | } | |
8efc0c15 | 337 | buffer_put_int(&e->output, 1); |
2e73a022 | 338 | buffer_put_char(&e->output, |
339 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
8efc0c15 | 340 | } |
341 | ||
8efc0c15 | 342 | void |
2e73a022 | 343 | process_remove_all_identities(SocketEntry *e, int version) |
8efc0c15 | 344 | { |
1e3b8b07 | 345 | u_int i; |
2e73a022 | 346 | Idtab *tab = idtab_lookup(version); |
8efc0c15 | 347 | |
5260325f | 348 | /* Loop over all identities and clear the keys. */ |
2e73a022 | 349 | for (i = 0; i < tab->nentries; i++) { |
350 | key_free(tab->identities[i].key); | |
351 | xfree(tab->identities[i].comment); | |
5260325f | 352 | } |
8efc0c15 | 353 | |
5260325f | 354 | /* Mark that there are no identities. */ |
2e73a022 | 355 | tab->nentries = 0; |
8efc0c15 | 356 | |
357 | /* Send success. */ | |
358 | buffer_put_int(&e->output, 1); | |
359 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); | |
360 | return; | |
5260325f | 361 | } |
362 | ||
fa08c86b | 363 | void |
364 | process_add_identity(SocketEntry *e, int version) | |
365 | { | |
366 | Key *k = NULL; | |
367 | char *type_name; | |
2e73a022 | 368 | char *comment; |
fa08c86b | 369 | int type, success = 0; |
2e73a022 | 370 | Idtab *tab = idtab_lookup(version); |
5260325f | 371 | |
2e73a022 | 372 | switch (version) { |
373 | case 1: | |
fa08c86b | 374 | k = key_new_private(KEY_RSA1); |
42f11eb2 | 375 | buffer_get_int(&e->input); /* ignored */ |
fa08c86b | 376 | buffer_get_bignum(&e->input, k->rsa->n); |
377 | buffer_get_bignum(&e->input, k->rsa->e); | |
378 | buffer_get_bignum(&e->input, k->rsa->d); | |
379 | buffer_get_bignum(&e->input, k->rsa->iqmp); | |
2e73a022 | 380 | |
381 | /* SSH and SSL have p and q swapped */ | |
fa08c86b | 382 | buffer_get_bignum(&e->input, k->rsa->q); /* p */ |
383 | buffer_get_bignum(&e->input, k->rsa->p); /* q */ | |
2e73a022 | 384 | |
385 | /* Generate additional parameters */ | |
fa08c86b | 386 | generate_additional_parameters(k->rsa); |
2e73a022 | 387 | break; |
388 | case 2: | |
fa08c86b | 389 | type_name = buffer_get_string(&e->input, NULL); |
42f11eb2 | 390 | type = key_type_from_name(type_name); |
fa08c86b | 391 | xfree(type_name); |
392 | switch(type) { | |
393 | case KEY_DSA: | |
394 | k = key_new_private(type); | |
395 | buffer_get_bignum2(&e->input, k->dsa->p); | |
396 | buffer_get_bignum2(&e->input, k->dsa->q); | |
397 | buffer_get_bignum2(&e->input, k->dsa->g); | |
398 | buffer_get_bignum2(&e->input, k->dsa->pub_key); | |
399 | buffer_get_bignum2(&e->input, k->dsa->priv_key); | |
400 | break; | |
401 | case KEY_RSA: | |
402 | k = key_new_private(type); | |
403 | buffer_get_bignum2(&e->input, k->rsa->n); | |
404 | buffer_get_bignum2(&e->input, k->rsa->e); | |
405 | buffer_get_bignum2(&e->input, k->rsa->d); | |
406 | buffer_get_bignum2(&e->input, k->rsa->iqmp); | |
407 | buffer_get_bignum2(&e->input, k->rsa->p); | |
408 | buffer_get_bignum2(&e->input, k->rsa->q); | |
409 | ||
410 | /* Generate additional parameters */ | |
411 | generate_additional_parameters(k->rsa); | |
412 | break; | |
413 | default: | |
2e73a022 | 414 | buffer_clear(&e->input); |
2e73a022 | 415 | goto send; |
5260325f | 416 | } |
2e73a022 | 417 | break; |
418 | } | |
2e73a022 | 419 | comment = buffer_get_string(&e->input, NULL); |
420 | if (k == NULL) { | |
421 | xfree(comment); | |
422 | goto send; | |
423 | } | |
424 | success = 1; | |
425 | if (lookup_private_key(k, NULL, version) == NULL) { | |
426 | if (tab->nentries == 0) | |
427 | tab->identities = xmalloc(sizeof(Identity)); | |
428 | else | |
429 | tab->identities = xrealloc(tab->identities, | |
430 | (tab->nentries + 1) * sizeof(Identity)); | |
431 | tab->identities[tab->nentries].key = k; | |
432 | tab->identities[tab->nentries].comment = comment; | |
433 | /* Increment the number of identities. */ | |
434 | tab->nentries++; | |
435 | } else { | |
436 | key_free(k); | |
437 | xfree(comment); | |
438 | } | |
439 | send: | |
5260325f | 440 | buffer_put_int(&e->output, 1); |
2e73a022 | 441 | buffer_put_char(&e->output, |
442 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
8efc0c15 | 443 | } |
444 | ||
2e73a022 | 445 | /* dispatch incoming messages */ |
446 | ||
8efc0c15 | 447 | void |
448 | process_message(SocketEntry *e) | |
449 | { | |
1e3b8b07 | 450 | u_int msg_len; |
451 | u_int type; | |
452 | u_char *cp; | |
5260325f | 453 | if (buffer_len(&e->input) < 5) |
454 | return; /* Incomplete message. */ | |
1e3b8b07 | 455 | cp = (u_char *) buffer_ptr(&e->input); |
5260325f | 456 | msg_len = GET_32BIT(cp); |
457 | if (msg_len > 256 * 1024) { | |
458 | shutdown(e->fd, SHUT_RDWR); | |
459 | close(e->fd); | |
460 | e->type = AUTH_UNUSED; | |
461 | return; | |
462 | } | |
463 | if (buffer_len(&e->input) < msg_len + 4) | |
464 | return; | |
465 | buffer_consume(&e->input, 4); | |
466 | type = buffer_get_char(&e->input); | |
467 | ||
468 | switch (type) { | |
2e73a022 | 469 | /* ssh1 */ |
5260325f | 470 | case SSH_AGENTC_RSA_CHALLENGE: |
2e73a022 | 471 | process_authentication_challenge1(e); |
472 | break; | |
473 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: | |
474 | process_request_identities(e, 1); | |
5260325f | 475 | break; |
476 | case SSH_AGENTC_ADD_RSA_IDENTITY: | |
2e73a022 | 477 | process_add_identity(e, 1); |
5260325f | 478 | break; |
479 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: | |
2e73a022 | 480 | process_remove_identity(e, 1); |
5260325f | 481 | break; |
482 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: | |
2e73a022 | 483 | process_remove_all_identities(e, 1); |
484 | break; | |
485 | /* ssh2 */ | |
486 | case SSH2_AGENTC_SIGN_REQUEST: | |
487 | process_sign_request2(e); | |
488 | break; | |
489 | case SSH2_AGENTC_REQUEST_IDENTITIES: | |
490 | process_request_identities(e, 2); | |
491 | break; | |
492 | case SSH2_AGENTC_ADD_IDENTITY: | |
493 | process_add_identity(e, 2); | |
494 | break; | |
495 | case SSH2_AGENTC_REMOVE_IDENTITY: | |
496 | process_remove_identity(e, 2); | |
497 | break; | |
498 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: | |
499 | process_remove_all_identities(e, 2); | |
5260325f | 500 | break; |
501 | default: | |
502 | /* Unknown message. Respond with failure. */ | |
503 | error("Unknown message %d", type); | |
504 | buffer_clear(&e->input); | |
505 | buffer_put_int(&e->output, 1); | |
506 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); | |
507 | break; | |
508 | } | |
8efc0c15 | 509 | } |
510 | ||
511 | void | |
512 | new_socket(int type, int fd) | |
513 | { | |
1e3b8b07 | 514 | u_int i, old_alloc; |
5260325f | 515 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) |
516 | error("fcntl O_NONBLOCK: %s", strerror(errno)); | |
517 | ||
518 | if (fd > max_fd) | |
519 | max_fd = fd; | |
520 | ||
521 | for (i = 0; i < sockets_alloc; i++) | |
522 | if (sockets[i].type == AUTH_UNUSED) { | |
523 | sockets[i].fd = fd; | |
524 | sockets[i].type = type; | |
525 | buffer_init(&sockets[i].input); | |
526 | buffer_init(&sockets[i].output); | |
527 | return; | |
528 | } | |
529 | old_alloc = sockets_alloc; | |
530 | sockets_alloc += 10; | |
531 | if (sockets) | |
532 | sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); | |
533 | else | |
534 | sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); | |
535 | for (i = old_alloc; i < sockets_alloc; i++) | |
536 | sockets[i].type = AUTH_UNUSED; | |
537 | sockets[old_alloc].type = type; | |
538 | sockets[old_alloc].fd = fd; | |
539 | buffer_init(&sockets[old_alloc].input); | |
540 | buffer_init(&sockets[old_alloc].output); | |
8efc0c15 | 541 | } |
542 | ||
42f11eb2 | 543 | int |
544 | prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl) | |
8efc0c15 | 545 | { |
42f11eb2 | 546 | u_int i, sz; |
547 | int n = 0; | |
548 | ||
549 | for (i = 0; i < sockets_alloc; i++) { | |
5260325f | 550 | switch (sockets[i].type) { |
551 | case AUTH_SOCKET: | |
552 | case AUTH_CONNECTION: | |
42f11eb2 | 553 | n = MAX(n, sockets[i].fd); |
5260325f | 554 | break; |
555 | case AUTH_UNUSED: | |
556 | break; | |
557 | default: | |
558 | fatal("Unknown socket type %d", sockets[i].type); | |
559 | break; | |
560 | } | |
42f11eb2 | 561 | } |
562 | ||
563 | sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); | |
564 | if (*fdrp == NULL || n > *fdl) { | |
565 | if (*fdrp) | |
894c5fa6 | 566 | xfree(*fdrp); |
42f11eb2 | 567 | if (*fdwp) |
894c5fa6 | 568 | xfree(*fdwp); |
42f11eb2 | 569 | *fdrp = xmalloc(sz); |
570 | *fdwp = xmalloc(sz); | |
571 | *fdl = n; | |
572 | } | |
573 | memset(*fdrp, 0, sz); | |
574 | memset(*fdwp, 0, sz); | |
575 | ||
576 | for (i = 0; i < sockets_alloc; i++) { | |
577 | switch (sockets[i].type) { | |
578 | case AUTH_SOCKET: | |
579 | case AUTH_CONNECTION: | |
580 | FD_SET(sockets[i].fd, *fdrp); | |
581 | if (buffer_len(&sockets[i].output) > 0) | |
582 | FD_SET(sockets[i].fd, *fdwp); | |
583 | break; | |
584 | default: | |
585 | break; | |
586 | } | |
587 | } | |
588 | return (1); | |
8efc0c15 | 589 | } |
590 | ||
6ae2364d | 591 | void |
5260325f | 592 | after_select(fd_set *readset, fd_set *writeset) |
8efc0c15 | 593 | { |
1e3b8b07 | 594 | u_int i; |
5260325f | 595 | int len, sock; |
610cd5c6 | 596 | socklen_t slen; |
5260325f | 597 | char buf[1024]; |
598 | struct sockaddr_un sunaddr; | |
599 | ||
600 | for (i = 0; i < sockets_alloc; i++) | |
601 | switch (sockets[i].type) { | |
602 | case AUTH_UNUSED: | |
603 | break; | |
604 | case AUTH_SOCKET: | |
605 | if (FD_ISSET(sockets[i].fd, readset)) { | |
610cd5c6 | 606 | slen = sizeof(sunaddr); |
42f11eb2 | 607 | sock = accept(sockets[i].fd, |
608 | (struct sockaddr *) &sunaddr, &slen); | |
5260325f | 609 | if (sock < 0) { |
610 | perror("accept from AUTH_SOCKET"); | |
611 | break; | |
612 | } | |
613 | new_socket(AUTH_CONNECTION, sock); | |
614 | } | |
615 | break; | |
616 | case AUTH_CONNECTION: | |
617 | if (buffer_len(&sockets[i].output) > 0 && | |
618 | FD_ISSET(sockets[i].fd, writeset)) { | |
bbc62e59 | 619 | do { |
620 | len = write(sockets[i].fd, | |
621 | buffer_ptr(&sockets[i].output), | |
622 | buffer_len(&sockets[i].output)); | |
623 | if (len == -1 && (errno == EAGAIN || | |
624 | errno == EINTR)) | |
625 | continue; | |
626 | break; | |
627 | } while (1); | |
5260325f | 628 | if (len <= 0) { |
629 | shutdown(sockets[i].fd, SHUT_RDWR); | |
630 | close(sockets[i].fd); | |
631 | sockets[i].type = AUTH_UNUSED; | |
0ac7199f | 632 | buffer_free(&sockets[i].input); |
633 | buffer_free(&sockets[i].output); | |
5260325f | 634 | break; |
635 | } | |
636 | buffer_consume(&sockets[i].output, len); | |
637 | } | |
638 | if (FD_ISSET(sockets[i].fd, readset)) { | |
bbc62e59 | 639 | do { |
640 | len = read(sockets[i].fd, buf, sizeof(buf)); | |
641 | if (len == -1 && (errno == EAGAIN || | |
642 | errno == EINTR)) | |
643 | continue; | |
644 | break; | |
645 | } while (1); | |
5260325f | 646 | if (len <= 0) { |
647 | shutdown(sockets[i].fd, SHUT_RDWR); | |
648 | close(sockets[i].fd); | |
649 | sockets[i].type = AUTH_UNUSED; | |
0ac7199f | 650 | buffer_free(&sockets[i].input); |
651 | buffer_free(&sockets[i].output); | |
5260325f | 652 | break; |
653 | } | |
654 | buffer_append(&sockets[i].input, buf, len); | |
655 | process_message(&sockets[i]); | |
656 | } | |
657 | break; | |
658 | default: | |
659 | fatal("Unknown type %d", sockets[i].type); | |
660 | } | |
8efc0c15 | 661 | } |
662 | ||
663 | void | |
664 | check_parent_exists(int sig) | |
665 | { | |
42f11eb2 | 666 | int save_errno = errno; |
667 | ||
9da5c3c9 | 668 | if (parent_pid != -1 && kill(parent_pid, 0) < 0) { |
5260325f | 669 | /* printf("Parent has died - Authentication agent exiting.\n"); */ |
670 | exit(1); | |
671 | } | |
672 | signal(SIGALRM, check_parent_exists); | |
673 | alarm(10); | |
42f11eb2 | 674 | errno = save_errno; |
8efc0c15 | 675 | } |
676 | ||
dae3fa13 | 677 | void |
678 | cleanup_socket(void) | |
679 | { | |
2f4b2e38 | 680 | if (socket_name[0]) |
681 | unlink(socket_name); | |
682 | if (socket_dir[0]) | |
683 | rmdir(socket_dir); | |
8efc0c15 | 684 | } |
685 | ||
dae3fa13 | 686 | void |
687 | cleanup_exit(int i) | |
688 | { | |
5260325f | 689 | cleanup_socket(); |
690 | exit(i); | |
dae3fa13 | 691 | } |
692 | ||
2f4b2e38 | 693 | void |
694 | cleanup_handler(int sig) | |
695 | { | |
696 | cleanup_socket(); | |
697 | _exit(2); | |
698 | } | |
699 | ||
dae3fa13 | 700 | void |
cc8aca8a | 701 | usage(void) |
dae3fa13 | 702 | { |
5260325f | 703 | fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION); |
704 | fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n", | |
0e3c1f95 | 705 | __progname); |
5260325f | 706 | exit(1); |
dae3fa13 | 707 | } |
708 | ||
8efc0c15 | 709 | int |
710 | main(int ac, char **av) | |
711 | { | |
5260325f | 712 | int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch; |
713 | struct sockaddr_un sunaddr; | |
b03bd394 | 714 | #ifdef HAVE_SETRLIMIT |
0b6fbf03 | 715 | struct rlimit rlim; |
c7ccfd39 | 716 | #endif |
717 | #ifdef HAVE_CYGWIN | |
718 | int prev_mask; | |
b03bd394 | 719 | #endif |
5260325f | 720 | pid_t pid; |
721 | char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid]; | |
c04f75f1 | 722 | extern int optind; |
42f11eb2 | 723 | fd_set *readsetp = NULL, *writesetp = NULL; |
fcb975eb | 724 | |
457fc0c6 | 725 | SSLeay_add_all_algorithms(); |
726 | ||
260d427b | 727 | __progname = get_progname(av[0]); |
264dce47 | 728 | init_rng(); |
e339aa53 | 729 | seed_rng(); |
2b87da3b | 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)); | |
c7ccfd39 | 811 | #ifdef HAVE_CYGWIN |
812 | prev_mask = umask(0177); | |
813 | #endif | |
5260325f | 814 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { |
815 | perror("bind"); | |
c7ccfd39 | 816 | #ifdef HAVE_CYGWIN |
817 | umask(prev_mask); | |
818 | #endif | |
5260325f | 819 | cleanup_exit(1); |
820 | } | |
c7ccfd39 | 821 | #ifdef HAVE_CYGWIN |
822 | umask(prev_mask); | |
823 | #endif | |
5260325f | 824 | if (listen(sock, 5) < 0) { |
825 | perror("listen"); | |
826 | cleanup_exit(1); | |
dae3fa13 | 827 | } |
42f11eb2 | 828 | |
aa3378df | 829 | /* |
830 | * Fork, and have the parent execute the command, if any, or present | |
831 | * the socket data. The child continues as the authentication agent. | |
832 | */ | |
5260325f | 833 | pid = fork(); |
834 | if (pid == -1) { | |
835 | perror("fork"); | |
836 | exit(1); | |
dae3fa13 | 837 | } |
5260325f | 838 | if (pid != 0) { /* Parent - execute the given command. */ |
839 | close(sock); | |
840 | snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid); | |
841 | if (ac == 0) { | |
842 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; | |
843 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, | |
42f11eb2 | 844 | SSH_AUTHSOCKET_ENV_NAME); |
5260325f | 845 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, |
42f11eb2 | 846 | SSH_AGENTPID_ENV_NAME); |
5260325f | 847 | printf("echo Agent pid %d;\n", pid); |
848 | exit(0); | |
849 | } | |
bcbf86ec | 850 | if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || |
851 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { | |
852 | perror("setenv"); | |
853 | exit(1); | |
854 | } | |
5260325f | 855 | execvp(av[0], av); |
856 | perror(av[0]); | |
857 | exit(1); | |
858 | } | |
859 | close(0); | |
860 | close(1); | |
861 | close(2); | |
dae3fa13 | 862 | |
b03bd394 | 863 | #ifdef HAVE_SETRLIMIT |
0b6fbf03 | 864 | /* deny core dumps, since memory contains unencrypted private keys */ |
865 | rlim.rlim_cur = rlim.rlim_max = 0; | |
866 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) { | |
867 | perror("setrlimit rlimit_core failed"); | |
868 | cleanup_exit(1); | |
869 | } | |
b03bd394 | 870 | #endif |
5260325f | 871 | if (setsid() == -1) { |
872 | perror("setsid"); | |
873 | cleanup_exit(1); | |
874 | } | |
875 | if (atexit(cleanup_socket) < 0) { | |
876 | perror("atexit"); | |
877 | cleanup_exit(1); | |
878 | } | |
879 | new_socket(AUTH_SOCKET, sock); | |
880 | if (ac > 0) { | |
881 | signal(SIGALRM, check_parent_exists); | |
882 | alarm(10); | |
883 | } | |
2e73a022 | 884 | idtab_init(); |
5260325f | 885 | signal(SIGINT, SIG_IGN); |
886 | signal(SIGPIPE, SIG_IGN); | |
2f4b2e38 | 887 | signal(SIGHUP, cleanup_handler); |
888 | signal(SIGTERM, cleanup_handler); | |
5260325f | 889 | while (1) { |
42f11eb2 | 890 | prepare_select(&readsetp, &writesetp, &max_fd); |
891 | if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) { | |
5260325f | 892 | if (errno == EINTR) |
893 | continue; | |
894 | exit(1); | |
895 | } | |
42f11eb2 | 896 | after_select(readsetp, writesetp); |
8efc0c15 | 897 | } |
5260325f | 898 | /* NOTREACHED */ |
8efc0c15 | 899 | } |