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