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