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