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