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