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