]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | |
3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
4 | * All rights reserved | |
5 | * The authentication agent program. | |
6 | * | |
7 | * As far as I am concerned, the code I have written for this software | |
8 | * can be used freely for any purpose. Any derived versions of this | |
9 | * software must be clearly marked as such, and if the derived work is | |
10 | * incompatible with the protocol description in the RFC file, it must be | |
11 | * called by a name other than "ssh" or "Secure Shell". | |
12 | * | |
13 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. | |
14 | * | |
15 | * Redistribution and use in source and binary forms, with or without | |
16 | * modification, are permitted provided that the following conditions | |
17 | * are met: | |
18 | * 1. Redistributions of source code must retain the above copyright | |
19 | * notice, this list of conditions and the following disclaimer. | |
20 | * 2. Redistributions in binary form must reproduce the above copyright | |
21 | * notice, this list of conditions and the following disclaimer in the | |
22 | * documentation and/or other materials provided with the distribution. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
25 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
26 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
27 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
29 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
33 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
34 | */ | |
35 | ||
36 | #include "includes.h" | |
37 | #include "openbsd-compat/sys-queue.h" | |
38 | RCSID("$OpenBSD: ssh-agent.c,v 1.126 2006/02/08 12:15:27 stevesk Exp $"); | |
39 | ||
40 | #ifdef HAVE_PATHS_H | |
41 | # include <paths.h> | |
42 | #endif | |
43 | #include <openssl/evp.h> | |
44 | #include <openssl/md5.h> | |
45 | ||
46 | #include "ssh.h" | |
47 | #include "rsa.h" | |
48 | #include "buffer.h" | |
49 | #include "bufaux.h" | |
50 | #include "xmalloc.h" | |
51 | #include "getput.h" | |
52 | #include "key.h" | |
53 | #include "authfd.h" | |
54 | #include "compat.h" | |
55 | #include "log.h" | |
56 | #include "misc.h" | |
57 | ||
58 | #ifdef SMARTCARD | |
59 | #include "scard.h" | |
60 | #endif | |
61 | ||
62 | #if defined(HAVE_SYS_PRCTL_H) | |
63 | #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */ | |
64 | #endif | |
65 | ||
66 | typedef enum { | |
67 | AUTH_UNUSED, | |
68 | AUTH_SOCKET, | |
69 | AUTH_CONNECTION | |
70 | } sock_type; | |
71 | ||
72 | typedef struct { | |
73 | int fd; | |
74 | sock_type type; | |
75 | Buffer input; | |
76 | Buffer output; | |
77 | Buffer request; | |
78 | } SocketEntry; | |
79 | ||
80 | u_int sockets_alloc = 0; | |
81 | SocketEntry *sockets = NULL; | |
82 | ||
83 | typedef struct identity { | |
84 | TAILQ_ENTRY(identity) next; | |
85 | Key *key; | |
86 | char *comment; | |
87 | u_int death; | |
88 | u_int confirm; | |
89 | } Identity; | |
90 | ||
91 | typedef struct { | |
92 | int nentries; | |
93 | TAILQ_HEAD(idqueue, identity) idlist; | |
94 | } Idtab; | |
95 | ||
96 | /* private key table, one per protocol version */ | |
97 | Idtab idtable[3]; | |
98 | ||
99 | int max_fd = 0; | |
100 | ||
101 | /* pid of shell == parent of agent */ | |
102 | pid_t parent_pid = -1; | |
103 | ||
104 | /* pathname and directory for AUTH_SOCKET */ | |
105 | char socket_name[1024]; | |
106 | char socket_dir[1024]; | |
107 | ||
108 | /* locking */ | |
109 | int locked = 0; | |
110 | char *lock_passwd = NULL; | |
111 | ||
112 | extern char *__progname; | |
113 | ||
114 | /* Default lifetime (0 == forever) */ | |
115 | static int lifetime = 0; | |
116 | ||
117 | static void | |
118 | close_socket(SocketEntry *e) | |
119 | { | |
120 | close(e->fd); | |
121 | e->fd = -1; | |
122 | e->type = AUTH_UNUSED; | |
123 | buffer_free(&e->input); | |
124 | buffer_free(&e->output); | |
125 | buffer_free(&e->request); | |
126 | } | |
127 | ||
128 | static void | |
129 | idtab_init(void) | |
130 | { | |
131 | int i; | |
132 | ||
133 | for (i = 0; i <=2; i++) { | |
134 | TAILQ_INIT(&idtable[i].idlist); | |
135 | idtable[i].nentries = 0; | |
136 | } | |
137 | } | |
138 | ||
139 | /* return private key table for requested protocol version */ | |
140 | static Idtab * | |
141 | idtab_lookup(int version) | |
142 | { | |
143 | if (version < 1 || version > 2) | |
144 | fatal("internal error, bad protocol version %d", version); | |
145 | return &idtable[version]; | |
146 | } | |
147 | ||
148 | static void | |
149 | free_identity(Identity *id) | |
150 | { | |
151 | key_free(id->key); | |
152 | xfree(id->comment); | |
153 | xfree(id); | |
154 | } | |
155 | ||
156 | /* return matching private key for given public key */ | |
157 | static Identity * | |
158 | lookup_identity(Key *key, int version) | |
159 | { | |
160 | Identity *id; | |
161 | ||
162 | Idtab *tab = idtab_lookup(version); | |
163 | TAILQ_FOREACH(id, &tab->idlist, next) { | |
164 | if (key_equal(key, id->key)) | |
165 | return (id); | |
166 | } | |
167 | return (NULL); | |
168 | } | |
169 | ||
170 | /* Check confirmation of keysign request */ | |
171 | static int | |
172 | confirm_key(Identity *id) | |
173 | { | |
174 | char *p; | |
175 | int ret = -1; | |
176 | ||
177 | p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX); | |
178 | if (ask_permission("Allow use of key %s?\nKey fingerprint %s.", | |
179 | id->comment, p)) | |
180 | ret = 0; | |
181 | xfree(p); | |
182 | ||
183 | return (ret); | |
184 | } | |
185 | ||
186 | /* send list of supported public keys to 'client' */ | |
187 | static void | |
188 | process_request_identities(SocketEntry *e, int version) | |
189 | { | |
190 | Idtab *tab = idtab_lookup(version); | |
191 | Identity *id; | |
192 | Buffer msg; | |
193 | ||
194 | buffer_init(&msg); | |
195 | buffer_put_char(&msg, (version == 1) ? | |
196 | SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); | |
197 | buffer_put_int(&msg, tab->nentries); | |
198 | TAILQ_FOREACH(id, &tab->idlist, next) { | |
199 | if (id->key->type == KEY_RSA1) { | |
200 | buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); | |
201 | buffer_put_bignum(&msg, id->key->rsa->e); | |
202 | buffer_put_bignum(&msg, id->key->rsa->n); | |
203 | } else { | |
204 | u_char *blob; | |
205 | u_int blen; | |
206 | key_to_blob(id->key, &blob, &blen); | |
207 | buffer_put_string(&msg, blob, blen); | |
208 | xfree(blob); | |
209 | } | |
210 | buffer_put_cstring(&msg, id->comment); | |
211 | } | |
212 | buffer_put_int(&e->output, buffer_len(&msg)); | |
213 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); | |
214 | buffer_free(&msg); | |
215 | } | |
216 | ||
217 | /* ssh1 only */ | |
218 | static void | |
219 | process_authentication_challenge1(SocketEntry *e) | |
220 | { | |
221 | u_char buf[32], mdbuf[16], session_id[16]; | |
222 | u_int response_type; | |
223 | BIGNUM *challenge; | |
224 | Identity *id; | |
225 | int i, len; | |
226 | Buffer msg; | |
227 | MD5_CTX md; | |
228 | Key *key; | |
229 | ||
230 | buffer_init(&msg); | |
231 | key = key_new(KEY_RSA1); | |
232 | if ((challenge = BN_new()) == NULL) | |
233 | fatal("process_authentication_challenge1: BN_new failed"); | |
234 | ||
235 | (void) buffer_get_int(&e->request); /* ignored */ | |
236 | buffer_get_bignum(&e->request, key->rsa->e); | |
237 | buffer_get_bignum(&e->request, key->rsa->n); | |
238 | buffer_get_bignum(&e->request, challenge); | |
239 | ||
240 | /* Only protocol 1.1 is supported */ | |
241 | if (buffer_len(&e->request) == 0) | |
242 | goto failure; | |
243 | buffer_get(&e->request, session_id, 16); | |
244 | response_type = buffer_get_int(&e->request); | |
245 | if (response_type != 1) | |
246 | goto failure; | |
247 | ||
248 | id = lookup_identity(key, 1); | |
249 | if (id != NULL && (!id->confirm || confirm_key(id) == 0)) { | |
250 | Key *private = id->key; | |
251 | /* Decrypt the challenge using the private key. */ | |
252 | if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) | |
253 | goto failure; | |
254 | ||
255 | /* The response is MD5 of decrypted challenge plus session id. */ | |
256 | len = BN_num_bytes(challenge); | |
257 | if (len <= 0 || len > 32) { | |
258 | logit("process_authentication_challenge: bad challenge length %d", len); | |
259 | goto failure; | |
260 | } | |
261 | memset(buf, 0, 32); | |
262 | BN_bn2bin(challenge, buf + 32 - len); | |
263 | MD5_Init(&md); | |
264 | MD5_Update(&md, buf, 32); | |
265 | MD5_Update(&md, session_id, 16); | |
266 | MD5_Final(mdbuf, &md); | |
267 | ||
268 | /* Send the response. */ | |
269 | buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); | |
270 | for (i = 0; i < 16; i++) | |
271 | buffer_put_char(&msg, mdbuf[i]); | |
272 | goto send; | |
273 | } | |
274 | ||
275 | failure: | |
276 | /* Unknown identity or protocol error. Send failure. */ | |
277 | buffer_put_char(&msg, SSH_AGENT_FAILURE); | |
278 | send: | |
279 | buffer_put_int(&e->output, buffer_len(&msg)); | |
280 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); | |
281 | key_free(key); | |
282 | BN_clear_free(challenge); | |
283 | buffer_free(&msg); | |
284 | } | |
285 | ||
286 | /* ssh2 only */ | |
287 | static void | |
288 | process_sign_request2(SocketEntry *e) | |
289 | { | |
290 | u_char *blob, *data, *signature = NULL; | |
291 | u_int blen, dlen, slen = 0; | |
292 | extern int datafellows; | |
293 | int ok = -1, flags; | |
294 | Buffer msg; | |
295 | Key *key; | |
296 | ||
297 | datafellows = 0; | |
298 | ||
299 | blob = buffer_get_string(&e->request, &blen); | |
300 | data = buffer_get_string(&e->request, &dlen); | |
301 | ||
302 | flags = buffer_get_int(&e->request); | |
303 | if (flags & SSH_AGENT_OLD_SIGNATURE) | |
304 | datafellows = SSH_BUG_SIGBLOB; | |
305 | ||
306 | key = key_from_blob(blob, blen); | |
307 | if (key != NULL) { | |
308 | Identity *id = lookup_identity(key, 2); | |
309 | if (id != NULL && (!id->confirm || confirm_key(id) == 0)) | |
310 | ok = key_sign(id->key, &signature, &slen, data, dlen); | |
311 | } | |
312 | key_free(key); | |
313 | buffer_init(&msg); | |
314 | if (ok == 0) { | |
315 | buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); | |
316 | buffer_put_string(&msg, signature, slen); | |
317 | } else { | |
318 | buffer_put_char(&msg, SSH_AGENT_FAILURE); | |
319 | } | |
320 | buffer_put_int(&e->output, buffer_len(&msg)); | |
321 | buffer_append(&e->output, buffer_ptr(&msg), | |
322 | buffer_len(&msg)); | |
323 | buffer_free(&msg); | |
324 | xfree(data); | |
325 | xfree(blob); | |
326 | if (signature != NULL) | |
327 | xfree(signature); | |
328 | } | |
329 | ||
330 | /* shared */ | |
331 | static void | |
332 | process_remove_identity(SocketEntry *e, int version) | |
333 | { | |
334 | u_int blen, bits; | |
335 | int success = 0; | |
336 | Key *key = NULL; | |
337 | u_char *blob; | |
338 | ||
339 | switch (version) { | |
340 | case 1: | |
341 | key = key_new(KEY_RSA1); | |
342 | bits = buffer_get_int(&e->request); | |
343 | buffer_get_bignum(&e->request, key->rsa->e); | |
344 | buffer_get_bignum(&e->request, key->rsa->n); | |
345 | ||
346 | if (bits != key_size(key)) | |
347 | logit("Warning: identity keysize mismatch: actual %u, announced %u", | |
348 | key_size(key), bits); | |
349 | break; | |
350 | case 2: | |
351 | blob = buffer_get_string(&e->request, &blen); | |
352 | key = key_from_blob(blob, blen); | |
353 | xfree(blob); | |
354 | break; | |
355 | } | |
356 | if (key != NULL) { | |
357 | Identity *id = lookup_identity(key, version); | |
358 | if (id != NULL) { | |
359 | /* | |
360 | * We have this key. Free the old key. Since we | |
361 | * don't want to leave empty slots in the middle of | |
362 | * the array, we actually free the key there and move | |
363 | * all the entries between the empty slot and the end | |
364 | * of the array. | |
365 | */ | |
366 | Idtab *tab = idtab_lookup(version); | |
367 | if (tab->nentries < 1) | |
368 | fatal("process_remove_identity: " | |
369 | "internal error: tab->nentries %d", | |
370 | tab->nentries); | |
371 | TAILQ_REMOVE(&tab->idlist, id, next); | |
372 | free_identity(id); | |
373 | tab->nentries--; | |
374 | success = 1; | |
375 | } | |
376 | key_free(key); | |
377 | } | |
378 | buffer_put_int(&e->output, 1); | |
379 | buffer_put_char(&e->output, | |
380 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
381 | } | |
382 | ||
383 | static void | |
384 | process_remove_all_identities(SocketEntry *e, int version) | |
385 | { | |
386 | Idtab *tab = idtab_lookup(version); | |
387 | Identity *id; | |
388 | ||
389 | /* Loop over all identities and clear the keys. */ | |
390 | for (id = TAILQ_FIRST(&tab->idlist); id; | |
391 | id = TAILQ_FIRST(&tab->idlist)) { | |
392 | TAILQ_REMOVE(&tab->idlist, id, next); | |
393 | free_identity(id); | |
394 | } | |
395 | ||
396 | /* Mark that there are no identities. */ | |
397 | tab->nentries = 0; | |
398 | ||
399 | /* Send success. */ | |
400 | buffer_put_int(&e->output, 1); | |
401 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); | |
402 | } | |
403 | ||
404 | static void | |
405 | reaper(void) | |
406 | { | |
407 | u_int now = time(NULL); | |
408 | Identity *id, *nxt; | |
409 | int version; | |
410 | Idtab *tab; | |
411 | ||
412 | for (version = 1; version < 3; version++) { | |
413 | tab = idtab_lookup(version); | |
414 | for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) { | |
415 | nxt = TAILQ_NEXT(id, next); | |
416 | if (id->death != 0 && now >= id->death) { | |
417 | TAILQ_REMOVE(&tab->idlist, id, next); | |
418 | free_identity(id); | |
419 | tab->nentries--; | |
420 | } | |
421 | } | |
422 | } | |
423 | } | |
424 | ||
425 | static void | |
426 | process_add_identity(SocketEntry *e, int version) | |
427 | { | |
428 | Idtab *tab = idtab_lookup(version); | |
429 | int type, success = 0, death = 0, confirm = 0; | |
430 | char *type_name, *comment; | |
431 | Key *k = NULL; | |
432 | ||
433 | switch (version) { | |
434 | case 1: | |
435 | k = key_new_private(KEY_RSA1); | |
436 | (void) buffer_get_int(&e->request); /* ignored */ | |
437 | buffer_get_bignum(&e->request, k->rsa->n); | |
438 | buffer_get_bignum(&e->request, k->rsa->e); | |
439 | buffer_get_bignum(&e->request, k->rsa->d); | |
440 | buffer_get_bignum(&e->request, k->rsa->iqmp); | |
441 | ||
442 | /* SSH and SSL have p and q swapped */ | |
443 | buffer_get_bignum(&e->request, k->rsa->q); /* p */ | |
444 | buffer_get_bignum(&e->request, k->rsa->p); /* q */ | |
445 | ||
446 | /* Generate additional parameters */ | |
447 | rsa_generate_additional_parameters(k->rsa); | |
448 | break; | |
449 | case 2: | |
450 | type_name = buffer_get_string(&e->request, NULL); | |
451 | type = key_type_from_name(type_name); | |
452 | xfree(type_name); | |
453 | switch (type) { | |
454 | case KEY_DSA: | |
455 | k = key_new_private(type); | |
456 | buffer_get_bignum2(&e->request, k->dsa->p); | |
457 | buffer_get_bignum2(&e->request, k->dsa->q); | |
458 | buffer_get_bignum2(&e->request, k->dsa->g); | |
459 | buffer_get_bignum2(&e->request, k->dsa->pub_key); | |
460 | buffer_get_bignum2(&e->request, k->dsa->priv_key); | |
461 | break; | |
462 | case KEY_RSA: | |
463 | k = key_new_private(type); | |
464 | buffer_get_bignum2(&e->request, k->rsa->n); | |
465 | buffer_get_bignum2(&e->request, k->rsa->e); | |
466 | buffer_get_bignum2(&e->request, k->rsa->d); | |
467 | buffer_get_bignum2(&e->request, k->rsa->iqmp); | |
468 | buffer_get_bignum2(&e->request, k->rsa->p); | |
469 | buffer_get_bignum2(&e->request, k->rsa->q); | |
470 | ||
471 | /* Generate additional parameters */ | |
472 | rsa_generate_additional_parameters(k->rsa); | |
473 | break; | |
474 | default: | |
475 | buffer_clear(&e->request); | |
476 | goto send; | |
477 | } | |
478 | break; | |
479 | } | |
480 | /* enable blinding */ | |
481 | switch (k->type) { | |
482 | case KEY_RSA: | |
483 | case KEY_RSA1: | |
484 | if (RSA_blinding_on(k->rsa, NULL) != 1) { | |
485 | error("process_add_identity: RSA_blinding_on failed"); | |
486 | key_free(k); | |
487 | goto send; | |
488 | } | |
489 | break; | |
490 | } | |
491 | comment = buffer_get_string(&e->request, NULL); | |
492 | if (k == NULL) { | |
493 | xfree(comment); | |
494 | goto send; | |
495 | } | |
496 | success = 1; | |
497 | while (buffer_len(&e->request)) { | |
498 | switch (buffer_get_char(&e->request)) { | |
499 | case SSH_AGENT_CONSTRAIN_LIFETIME: | |
500 | death = time(NULL) + buffer_get_int(&e->request); | |
501 | break; | |
502 | case SSH_AGENT_CONSTRAIN_CONFIRM: | |
503 | confirm = 1; | |
504 | break; | |
505 | default: | |
506 | break; | |
507 | } | |
508 | } | |
509 | if (lifetime && !death) | |
510 | death = time(NULL) + lifetime; | |
511 | if (lookup_identity(k, version) == NULL) { | |
512 | Identity *id = xmalloc(sizeof(Identity)); | |
513 | id->key = k; | |
514 | id->comment = comment; | |
515 | id->death = death; | |
516 | id->confirm = confirm; | |
517 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); | |
518 | /* Increment the number of identities. */ | |
519 | tab->nentries++; | |
520 | } else { | |
521 | key_free(k); | |
522 | xfree(comment); | |
523 | } | |
524 | send: | |
525 | buffer_put_int(&e->output, 1); | |
526 | buffer_put_char(&e->output, | |
527 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
528 | } | |
529 | ||
530 | /* XXX todo: encrypt sensitive data with passphrase */ | |
531 | static void | |
532 | process_lock_agent(SocketEntry *e, int lock) | |
533 | { | |
534 | int success = 0; | |
535 | char *passwd; | |
536 | ||
537 | passwd = buffer_get_string(&e->request, NULL); | |
538 | if (locked && !lock && strcmp(passwd, lock_passwd) == 0) { | |
539 | locked = 0; | |
540 | memset(lock_passwd, 0, strlen(lock_passwd)); | |
541 | xfree(lock_passwd); | |
542 | lock_passwd = NULL; | |
543 | success = 1; | |
544 | } else if (!locked && lock) { | |
545 | locked = 1; | |
546 | lock_passwd = xstrdup(passwd); | |
547 | success = 1; | |
548 | } | |
549 | memset(passwd, 0, strlen(passwd)); | |
550 | xfree(passwd); | |
551 | ||
552 | buffer_put_int(&e->output, 1); | |
553 | buffer_put_char(&e->output, | |
554 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
555 | } | |
556 | ||
557 | static void | |
558 | no_identities(SocketEntry *e, u_int type) | |
559 | { | |
560 | Buffer msg; | |
561 | ||
562 | buffer_init(&msg); | |
563 | buffer_put_char(&msg, | |
564 | (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ? | |
565 | SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); | |
566 | buffer_put_int(&msg, 0); | |
567 | buffer_put_int(&e->output, buffer_len(&msg)); | |
568 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); | |
569 | buffer_free(&msg); | |
570 | } | |
571 | ||
572 | #ifdef SMARTCARD | |
573 | static void | |
574 | process_add_smartcard_key (SocketEntry *e) | |
575 | { | |
576 | char *sc_reader_id = NULL, *pin; | |
577 | int i, version, success = 0, death = 0, confirm = 0; | |
578 | Key **keys, *k; | |
579 | Identity *id; | |
580 | Idtab *tab; | |
581 | ||
582 | sc_reader_id = buffer_get_string(&e->request, NULL); | |
583 | pin = buffer_get_string(&e->request, NULL); | |
584 | ||
585 | while (buffer_len(&e->request)) { | |
586 | switch (buffer_get_char(&e->request)) { | |
587 | case SSH_AGENT_CONSTRAIN_LIFETIME: | |
588 | death = time(NULL) + buffer_get_int(&e->request); | |
589 | break; | |
590 | case SSH_AGENT_CONSTRAIN_CONFIRM: | |
591 | confirm = 1; | |
592 | break; | |
593 | default: | |
594 | break; | |
595 | } | |
596 | } | |
597 | if (lifetime && !death) | |
598 | death = time(NULL) + lifetime; | |
599 | ||
600 | keys = sc_get_keys(sc_reader_id, pin); | |
601 | xfree(sc_reader_id); | |
602 | xfree(pin); | |
603 | ||
604 | if (keys == NULL || keys[0] == NULL) { | |
605 | error("sc_get_keys failed"); | |
606 | goto send; | |
607 | } | |
608 | for (i = 0; keys[i] != NULL; i++) { | |
609 | k = keys[i]; | |
610 | version = k->type == KEY_RSA1 ? 1 : 2; | |
611 | tab = idtab_lookup(version); | |
612 | if (lookup_identity(k, version) == NULL) { | |
613 | id = xmalloc(sizeof(Identity)); | |
614 | id->key = k; | |
615 | id->comment = sc_get_key_label(k); | |
616 | id->death = death; | |
617 | id->confirm = confirm; | |
618 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); | |
619 | tab->nentries++; | |
620 | success = 1; | |
621 | } else { | |
622 | key_free(k); | |
623 | } | |
624 | keys[i] = NULL; | |
625 | } | |
626 | xfree(keys); | |
627 | send: | |
628 | buffer_put_int(&e->output, 1); | |
629 | buffer_put_char(&e->output, | |
630 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
631 | } | |
632 | ||
633 | static void | |
634 | process_remove_smartcard_key(SocketEntry *e) | |
635 | { | |
636 | char *sc_reader_id = NULL, *pin; | |
637 | int i, version, success = 0; | |
638 | Key **keys, *k = NULL; | |
639 | Identity *id; | |
640 | Idtab *tab; | |
641 | ||
642 | sc_reader_id = buffer_get_string(&e->request, NULL); | |
643 | pin = buffer_get_string(&e->request, NULL); | |
644 | keys = sc_get_keys(sc_reader_id, pin); | |
645 | xfree(sc_reader_id); | |
646 | xfree(pin); | |
647 | ||
648 | if (keys == NULL || keys[0] == NULL) { | |
649 | error("sc_get_keys failed"); | |
650 | goto send; | |
651 | } | |
652 | for (i = 0; keys[i] != NULL; i++) { | |
653 | k = keys[i]; | |
654 | version = k->type == KEY_RSA1 ? 1 : 2; | |
655 | if ((id = lookup_identity(k, version)) != NULL) { | |
656 | tab = idtab_lookup(version); | |
657 | TAILQ_REMOVE(&tab->idlist, id, next); | |
658 | tab->nentries--; | |
659 | free_identity(id); | |
660 | success = 1; | |
661 | } | |
662 | key_free(k); | |
663 | keys[i] = NULL; | |
664 | } | |
665 | xfree(keys); | |
666 | send: | |
667 | buffer_put_int(&e->output, 1); | |
668 | buffer_put_char(&e->output, | |
669 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
670 | } | |
671 | #endif /* SMARTCARD */ | |
672 | ||
673 | /* dispatch incoming messages */ | |
674 | ||
675 | static void | |
676 | process_message(SocketEntry *e) | |
677 | { | |
678 | u_int msg_len, type; | |
679 | u_char *cp; | |
680 | ||
681 | /* kill dead keys */ | |
682 | reaper(); | |
683 | ||
684 | if (buffer_len(&e->input) < 5) | |
685 | return; /* Incomplete message. */ | |
686 | cp = buffer_ptr(&e->input); | |
687 | msg_len = GET_32BIT(cp); | |
688 | if (msg_len > 256 * 1024) { | |
689 | close_socket(e); | |
690 | return; | |
691 | } | |
692 | if (buffer_len(&e->input) < msg_len + 4) | |
693 | return; | |
694 | ||
695 | /* move the current input to e->request */ | |
696 | buffer_consume(&e->input, 4); | |
697 | buffer_clear(&e->request); | |
698 | buffer_append(&e->request, buffer_ptr(&e->input), msg_len); | |
699 | buffer_consume(&e->input, msg_len); | |
700 | type = buffer_get_char(&e->request); | |
701 | ||
702 | /* check wheter agent is locked */ | |
703 | if (locked && type != SSH_AGENTC_UNLOCK) { | |
704 | buffer_clear(&e->request); | |
705 | switch (type) { | |
706 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: | |
707 | case SSH2_AGENTC_REQUEST_IDENTITIES: | |
708 | /* send empty lists */ | |
709 | no_identities(e, type); | |
710 | break; | |
711 | default: | |
712 | /* send a fail message for all other request types */ | |
713 | buffer_put_int(&e->output, 1); | |
714 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); | |
715 | } | |
716 | return; | |
717 | } | |
718 | ||
719 | debug("type %d", type); | |
720 | switch (type) { | |
721 | case SSH_AGENTC_LOCK: | |
722 | case SSH_AGENTC_UNLOCK: | |
723 | process_lock_agent(e, type == SSH_AGENTC_LOCK); | |
724 | break; | |
725 | /* ssh1 */ | |
726 | case SSH_AGENTC_RSA_CHALLENGE: | |
727 | process_authentication_challenge1(e); | |
728 | break; | |
729 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: | |
730 | process_request_identities(e, 1); | |
731 | break; | |
732 | case SSH_AGENTC_ADD_RSA_IDENTITY: | |
733 | case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED: | |
734 | process_add_identity(e, 1); | |
735 | break; | |
736 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: | |
737 | process_remove_identity(e, 1); | |
738 | break; | |
739 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: | |
740 | process_remove_all_identities(e, 1); | |
741 | break; | |
742 | /* ssh2 */ | |
743 | case SSH2_AGENTC_SIGN_REQUEST: | |
744 | process_sign_request2(e); | |
745 | break; | |
746 | case SSH2_AGENTC_REQUEST_IDENTITIES: | |
747 | process_request_identities(e, 2); | |
748 | break; | |
749 | case SSH2_AGENTC_ADD_IDENTITY: | |
750 | case SSH2_AGENTC_ADD_ID_CONSTRAINED: | |
751 | process_add_identity(e, 2); | |
752 | break; | |
753 | case SSH2_AGENTC_REMOVE_IDENTITY: | |
754 | process_remove_identity(e, 2); | |
755 | break; | |
756 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: | |
757 | process_remove_all_identities(e, 2); | |
758 | break; | |
759 | #ifdef SMARTCARD | |
760 | case SSH_AGENTC_ADD_SMARTCARD_KEY: | |
761 | case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED: | |
762 | process_add_smartcard_key(e); | |
763 | break; | |
764 | case SSH_AGENTC_REMOVE_SMARTCARD_KEY: | |
765 | process_remove_smartcard_key(e); | |
766 | break; | |
767 | #endif /* SMARTCARD */ | |
768 | default: | |
769 | /* Unknown message. Respond with failure. */ | |
770 | error("Unknown message %d", type); | |
771 | buffer_clear(&e->request); | |
772 | buffer_put_int(&e->output, 1); | |
773 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); | |
774 | break; | |
775 | } | |
776 | } | |
777 | ||
778 | static void | |
779 | new_socket(sock_type type, int fd) | |
780 | { | |
781 | u_int i, old_alloc, new_alloc; | |
782 | ||
783 | set_nonblock(fd); | |
784 | ||
785 | if (fd > max_fd) | |
786 | max_fd = fd; | |
787 | ||
788 | for (i = 0; i < sockets_alloc; i++) | |
789 | if (sockets[i].type == AUTH_UNUSED) { | |
790 | sockets[i].fd = fd; | |
791 | buffer_init(&sockets[i].input); | |
792 | buffer_init(&sockets[i].output); | |
793 | buffer_init(&sockets[i].request); | |
794 | sockets[i].type = type; | |
795 | return; | |
796 | } | |
797 | old_alloc = sockets_alloc; | |
798 | new_alloc = sockets_alloc + 10; | |
799 | if (sockets) | |
800 | sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0])); | |
801 | else | |
802 | sockets = xmalloc(new_alloc * sizeof(sockets[0])); | |
803 | for (i = old_alloc; i < new_alloc; i++) | |
804 | sockets[i].type = AUTH_UNUSED; | |
805 | sockets_alloc = new_alloc; | |
806 | sockets[old_alloc].fd = fd; | |
807 | buffer_init(&sockets[old_alloc].input); | |
808 | buffer_init(&sockets[old_alloc].output); | |
809 | buffer_init(&sockets[old_alloc].request); | |
810 | sockets[old_alloc].type = type; | |
811 | } | |
812 | ||
813 | static int | |
814 | prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp) | |
815 | { | |
816 | u_int i, sz; | |
817 | int n = 0; | |
818 | ||
819 | for (i = 0; i < sockets_alloc; i++) { | |
820 | switch (sockets[i].type) { | |
821 | case AUTH_SOCKET: | |
822 | case AUTH_CONNECTION: | |
823 | n = MAX(n, sockets[i].fd); | |
824 | break; | |
825 | case AUTH_UNUSED: | |
826 | break; | |
827 | default: | |
828 | fatal("Unknown socket type %d", sockets[i].type); | |
829 | break; | |
830 | } | |
831 | } | |
832 | ||
833 | sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); | |
834 | if (*fdrp == NULL || sz > *nallocp) { | |
835 | if (*fdrp) | |
836 | xfree(*fdrp); | |
837 | if (*fdwp) | |
838 | xfree(*fdwp); | |
839 | *fdrp = xmalloc(sz); | |
840 | *fdwp = xmalloc(sz); | |
841 | *nallocp = sz; | |
842 | } | |
843 | if (n < *fdl) | |
844 | debug("XXX shrink: %d < %d", n, *fdl); | |
845 | *fdl = n; | |
846 | memset(*fdrp, 0, sz); | |
847 | memset(*fdwp, 0, sz); | |
848 | ||
849 | for (i = 0; i < sockets_alloc; i++) { | |
850 | switch (sockets[i].type) { | |
851 | case AUTH_SOCKET: | |
852 | case AUTH_CONNECTION: | |
853 | FD_SET(sockets[i].fd, *fdrp); | |
854 | if (buffer_len(&sockets[i].output) > 0) | |
855 | FD_SET(sockets[i].fd, *fdwp); | |
856 | break; | |
857 | default: | |
858 | break; | |
859 | } | |
860 | } | |
861 | return (1); | |
862 | } | |
863 | ||
864 | static void | |
865 | after_select(fd_set *readset, fd_set *writeset) | |
866 | { | |
867 | struct sockaddr_un sunaddr; | |
868 | socklen_t slen; | |
869 | char buf[1024]; | |
870 | int len, sock; | |
871 | u_int i; | |
872 | uid_t euid; | |
873 | gid_t egid; | |
874 | ||
875 | for (i = 0; i < sockets_alloc; i++) | |
876 | switch (sockets[i].type) { | |
877 | case AUTH_UNUSED: | |
878 | break; | |
879 | case AUTH_SOCKET: | |
880 | if (FD_ISSET(sockets[i].fd, readset)) { | |
881 | slen = sizeof(sunaddr); | |
882 | sock = accept(sockets[i].fd, | |
883 | (struct sockaddr *) &sunaddr, &slen); | |
884 | if (sock < 0) { | |
885 | error("accept from AUTH_SOCKET: %s", | |
886 | strerror(errno)); | |
887 | break; | |
888 | } | |
889 | if (getpeereid(sock, &euid, &egid) < 0) { | |
890 | error("getpeereid %d failed: %s", | |
891 | sock, strerror(errno)); | |
892 | close(sock); | |
893 | break; | |
894 | } | |
895 | if ((euid != 0) && (getuid() != euid)) { | |
896 | error("uid mismatch: " | |
897 | "peer euid %u != uid %u", | |
898 | (u_int) euid, (u_int) getuid()); | |
899 | close(sock); | |
900 | break; | |
901 | } | |
902 | new_socket(AUTH_CONNECTION, sock); | |
903 | } | |
904 | break; | |
905 | case AUTH_CONNECTION: | |
906 | if (buffer_len(&sockets[i].output) > 0 && | |
907 | FD_ISSET(sockets[i].fd, writeset)) { | |
908 | do { | |
909 | len = write(sockets[i].fd, | |
910 | buffer_ptr(&sockets[i].output), | |
911 | buffer_len(&sockets[i].output)); | |
912 | if (len == -1 && (errno == EAGAIN || | |
913 | errno == EINTR)) | |
914 | continue; | |
915 | break; | |
916 | } while (1); | |
917 | if (len <= 0) { | |
918 | close_socket(&sockets[i]); | |
919 | break; | |
920 | } | |
921 | buffer_consume(&sockets[i].output, len); | |
922 | } | |
923 | if (FD_ISSET(sockets[i].fd, readset)) { | |
924 | do { | |
925 | len = read(sockets[i].fd, buf, sizeof(buf)); | |
926 | if (len == -1 && (errno == EAGAIN || | |
927 | errno == EINTR)) | |
928 | continue; | |
929 | break; | |
930 | } while (1); | |
931 | if (len <= 0) { | |
932 | close_socket(&sockets[i]); | |
933 | break; | |
934 | } | |
935 | buffer_append(&sockets[i].input, buf, len); | |
936 | process_message(&sockets[i]); | |
937 | } | |
938 | break; | |
939 | default: | |
940 | fatal("Unknown type %d", sockets[i].type); | |
941 | } | |
942 | } | |
943 | ||
944 | static void | |
945 | cleanup_socket(void) | |
946 | { | |
947 | if (socket_name[0]) | |
948 | unlink(socket_name); | |
949 | if (socket_dir[0]) | |
950 | rmdir(socket_dir); | |
951 | } | |
952 | ||
953 | void | |
954 | cleanup_exit(int i) | |
955 | { | |
956 | cleanup_socket(); | |
957 | _exit(i); | |
958 | } | |
959 | ||
960 | static void | |
961 | cleanup_handler(int sig) | |
962 | { | |
963 | cleanup_socket(); | |
964 | _exit(2); | |
965 | } | |
966 | ||
967 | static void | |
968 | check_parent_exists(int sig) | |
969 | { | |
970 | int save_errno = errno; | |
971 | ||
972 | if (parent_pid != -1 && kill(parent_pid, 0) < 0) { | |
973 | /* printf("Parent has died - Authentication agent exiting.\n"); */ | |
974 | cleanup_handler(sig); /* safe */ | |
975 | } | |
976 | mysignal(SIGALRM, check_parent_exists); | |
977 | alarm(10); | |
978 | errno = save_errno; | |
979 | } | |
980 | ||
981 | static void | |
982 | usage(void) | |
983 | { | |
984 | fprintf(stderr, "Usage: %s [options] [command [args ...]]\n", | |
985 | __progname); | |
986 | fprintf(stderr, "Options:\n"); | |
987 | fprintf(stderr, " -c Generate C-shell commands on stdout.\n"); | |
988 | fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n"); | |
989 | fprintf(stderr, " -k Kill the current agent.\n"); | |
990 | fprintf(stderr, " -d Debug mode.\n"); | |
991 | fprintf(stderr, " -a socket Bind agent socket to given name.\n"); | |
992 | fprintf(stderr, " -t life Default identity lifetime (seconds).\n"); | |
993 | exit(1); | |
994 | } | |
995 | ||
996 | int | |
997 | main(int ac, char **av) | |
998 | { | |
999 | int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0; | |
1000 | int sock, fd, ch; | |
1001 | u_int nalloc; | |
1002 | char *shell, *format, *pidstr, *agentsocket = NULL; | |
1003 | fd_set *readsetp = NULL, *writesetp = NULL; | |
1004 | struct sockaddr_un sunaddr; | |
1005 | #ifdef HAVE_SETRLIMIT | |
1006 | struct rlimit rlim; | |
1007 | #endif | |
1008 | int prev_mask; | |
1009 | extern int optind; | |
1010 | extern char *optarg; | |
1011 | pid_t pid; | |
1012 | char pidstrbuf[1 + 3 * sizeof pid]; | |
1013 | ||
1014 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ | |
1015 | sanitise_stdfd(); | |
1016 | ||
1017 | /* drop */ | |
1018 | setegid(getgid()); | |
1019 | setgid(getgid()); | |
1020 | ||
1021 | #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE) | |
1022 | /* Disable ptrace on Linux without sgid bit */ | |
1023 | prctl(PR_SET_DUMPABLE, 0); | |
1024 | #endif | |
1025 | ||
1026 | SSLeay_add_all_algorithms(); | |
1027 | ||
1028 | __progname = ssh_get_progname(av[0]); | |
1029 | init_rng(); | |
1030 | seed_rng(); | |
1031 | ||
1032 | while ((ch = getopt(ac, av, "cdksa:t:")) != -1) { | |
1033 | switch (ch) { | |
1034 | case 'c': | |
1035 | if (s_flag) | |
1036 | usage(); | |
1037 | c_flag++; | |
1038 | break; | |
1039 | case 'k': | |
1040 | k_flag++; | |
1041 | break; | |
1042 | case 's': | |
1043 | if (c_flag) | |
1044 | usage(); | |
1045 | s_flag++; | |
1046 | break; | |
1047 | case 'd': | |
1048 | if (d_flag) | |
1049 | usage(); | |
1050 | d_flag++; | |
1051 | break; | |
1052 | case 'a': | |
1053 | agentsocket = optarg; | |
1054 | break; | |
1055 | case 't': | |
1056 | if ((lifetime = convtime(optarg)) == -1) { | |
1057 | fprintf(stderr, "Invalid lifetime\n"); | |
1058 | usage(); | |
1059 | } | |
1060 | break; | |
1061 | default: | |
1062 | usage(); | |
1063 | } | |
1064 | } | |
1065 | ac -= optind; | |
1066 | av += optind; | |
1067 | ||
1068 | if (ac > 0 && (c_flag || k_flag || s_flag || d_flag)) | |
1069 | usage(); | |
1070 | ||
1071 | if (ac == 0 && !c_flag && !s_flag) { | |
1072 | shell = getenv("SHELL"); | |
1073 | if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) | |
1074 | c_flag = 1; | |
1075 | } | |
1076 | if (k_flag) { | |
1077 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); | |
1078 | if (pidstr == NULL) { | |
1079 | fprintf(stderr, "%s not set, cannot kill agent\n", | |
1080 | SSH_AGENTPID_ENV_NAME); | |
1081 | exit(1); | |
1082 | } | |
1083 | pid = atoi(pidstr); | |
1084 | if (pid < 1) { | |
1085 | fprintf(stderr, "%s=\"%s\", which is not a good PID\n", | |
1086 | SSH_AGENTPID_ENV_NAME, pidstr); | |
1087 | exit(1); | |
1088 | } | |
1089 | if (kill(pid, SIGTERM) == -1) { | |
1090 | perror("kill"); | |
1091 | exit(1); | |
1092 | } | |
1093 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; | |
1094 | printf(format, SSH_AUTHSOCKET_ENV_NAME); | |
1095 | printf(format, SSH_AGENTPID_ENV_NAME); | |
1096 | printf("echo Agent pid %ld killed;\n", (long)pid); | |
1097 | exit(0); | |
1098 | } | |
1099 | parent_pid = getpid(); | |
1100 | ||
1101 | if (agentsocket == NULL) { | |
1102 | /* Create private directory for agent socket */ | |
1103 | strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir); | |
1104 | if (mkdtemp(socket_dir) == NULL) { | |
1105 | perror("mkdtemp: private socket dir"); | |
1106 | exit(1); | |
1107 | } | |
1108 | snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir, | |
1109 | (long)parent_pid); | |
1110 | } else { | |
1111 | /* Try to use specified agent socket */ | |
1112 | socket_dir[0] = '\0'; | |
1113 | strlcpy(socket_name, agentsocket, sizeof socket_name); | |
1114 | } | |
1115 | ||
1116 | /* | |
1117 | * Create socket early so it will exist before command gets run from | |
1118 | * the parent. | |
1119 | */ | |
1120 | sock = socket(AF_UNIX, SOCK_STREAM, 0); | |
1121 | if (sock < 0) { | |
1122 | perror("socket"); | |
1123 | *socket_name = '\0'; /* Don't unlink any existing file */ | |
1124 | cleanup_exit(1); | |
1125 | } | |
1126 | memset(&sunaddr, 0, sizeof(sunaddr)); | |
1127 | sunaddr.sun_family = AF_UNIX; | |
1128 | strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); | |
1129 | prev_mask = umask(0177); | |
1130 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { | |
1131 | perror("bind"); | |
1132 | *socket_name = '\0'; /* Don't unlink any existing file */ | |
1133 | umask(prev_mask); | |
1134 | cleanup_exit(1); | |
1135 | } | |
1136 | umask(prev_mask); | |
1137 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { | |
1138 | perror("listen"); | |
1139 | cleanup_exit(1); | |
1140 | } | |
1141 | ||
1142 | /* | |
1143 | * Fork, and have the parent execute the command, if any, or present | |
1144 | * the socket data. The child continues as the authentication agent. | |
1145 | */ | |
1146 | if (d_flag) { | |
1147 | log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1); | |
1148 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; | |
1149 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, | |
1150 | SSH_AUTHSOCKET_ENV_NAME); | |
1151 | printf("echo Agent pid %ld;\n", (long)parent_pid); | |
1152 | goto skip; | |
1153 | } | |
1154 | pid = fork(); | |
1155 | if (pid == -1) { | |
1156 | perror("fork"); | |
1157 | cleanup_exit(1); | |
1158 | } | |
1159 | if (pid != 0) { /* Parent - execute the given command. */ | |
1160 | close(sock); | |
1161 | snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid); | |
1162 | if (ac == 0) { | |
1163 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; | |
1164 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, | |
1165 | SSH_AUTHSOCKET_ENV_NAME); | |
1166 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, | |
1167 | SSH_AGENTPID_ENV_NAME); | |
1168 | printf("echo Agent pid %ld;\n", (long)pid); | |
1169 | exit(0); | |
1170 | } | |
1171 | if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || | |
1172 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { | |
1173 | perror("setenv"); | |
1174 | exit(1); | |
1175 | } | |
1176 | execvp(av[0], av); | |
1177 | perror(av[0]); | |
1178 | exit(1); | |
1179 | } | |
1180 | /* child */ | |
1181 | log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0); | |
1182 | ||
1183 | if (setsid() == -1) { | |
1184 | error("setsid: %s", strerror(errno)); | |
1185 | cleanup_exit(1); | |
1186 | } | |
1187 | ||
1188 | (void)chdir("/"); | |
1189 | if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { | |
1190 | /* XXX might close listen socket */ | |
1191 | (void)dup2(fd, STDIN_FILENO); | |
1192 | (void)dup2(fd, STDOUT_FILENO); | |
1193 | (void)dup2(fd, STDERR_FILENO); | |
1194 | if (fd > 2) | |
1195 | close(fd); | |
1196 | } | |
1197 | ||
1198 | #ifdef HAVE_SETRLIMIT | |
1199 | /* deny core dumps, since memory contains unencrypted private keys */ | |
1200 | rlim.rlim_cur = rlim.rlim_max = 0; | |
1201 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) { | |
1202 | error("setrlimit RLIMIT_CORE: %s", strerror(errno)); | |
1203 | cleanup_exit(1); | |
1204 | } | |
1205 | #endif | |
1206 | ||
1207 | skip: | |
1208 | new_socket(AUTH_SOCKET, sock); | |
1209 | if (ac > 0) { | |
1210 | mysignal(SIGALRM, check_parent_exists); | |
1211 | alarm(10); | |
1212 | } | |
1213 | idtab_init(); | |
1214 | if (!d_flag) | |
1215 | signal(SIGINT, SIG_IGN); | |
1216 | signal(SIGPIPE, SIG_IGN); | |
1217 | signal(SIGHUP, cleanup_handler); | |
1218 | signal(SIGTERM, cleanup_handler); | |
1219 | nalloc = 0; | |
1220 | ||
1221 | while (1) { | |
1222 | prepare_select(&readsetp, &writesetp, &max_fd, &nalloc); | |
1223 | if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) { | |
1224 | if (errno == EINTR) | |
1225 | continue; | |
1226 | fatal("select: %s", strerror(errno)); | |
1227 | } | |
1228 | after_select(readsetp, writesetp); | |
1229 | } | |
1230 | /* NOTREACHED */ | |
1231 | } |