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