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