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