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