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