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