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