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