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