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