]>
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" |
58ae9cb8 | 38 | RCSID("$OpenBSD: ssh-agent.c,v 1.118 2004/05/08 00:21:31 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 | #ifdef HAVE___PROGNAME |
110 | extern char *__progname; | |
260d427b | 111 | #else |
112 | char *__progname; | |
113 | #endif | |
5260325f | 114 | |
494b267f | 115 | /* Default lifetime (0 == forever) */ |
116 | static int lifetime = 0; | |
117 | ||
c1a4eef1 | 118 | static void |
119 | close_socket(SocketEntry *e) | |
120 | { | |
c1a4eef1 | 121 | close(e->fd); |
122 | e->fd = -1; | |
123 | e->type = AUTH_UNUSED; | |
124 | buffer_free(&e->input); | |
125 | buffer_free(&e->output); | |
126 | buffer_free(&e->request); | |
127 | } | |
128 | ||
396c147e | 129 | static void |
2e73a022 | 130 | idtab_init(void) |
131 | { | |
132 | int i; | |
e424e241 | 133 | |
6aacefa7 | 134 | for (i = 0; i <=2; i++) { |
95f0a918 | 135 | TAILQ_INIT(&idtable[i].idlist); |
2e73a022 | 136 | idtable[i].nentries = 0; |
137 | } | |
138 | } | |
139 | ||
140 | /* return private key table for requested protocol version */ | |
396c147e | 141 | static Idtab * |
2e73a022 | 142 | idtab_lookup(int version) |
143 | { | |
144 | if (version < 1 || version > 2) | |
145 | fatal("internal error, bad protocol version %d", version); | |
146 | return &idtable[version]; | |
147 | } | |
148 | ||
264572cc | 149 | static void |
150 | free_identity(Identity *id) | |
151 | { | |
152 | key_free(id->key); | |
153 | xfree(id->comment); | |
154 | xfree(id); | |
155 | } | |
156 | ||
2e73a022 | 157 | /* return matching private key for given public key */ |
95f0a918 | 158 | static Identity * |
159 | lookup_identity(Key *key, int version) | |
2e73a022 | 160 | { |
95f0a918 | 161 | Identity *id; |
162 | ||
2e73a022 | 163 | Idtab *tab = idtab_lookup(version); |
95f0a918 | 164 | TAILQ_FOREACH(id, &tab->idlist, next) { |
165 | if (key_equal(key, id->key)) | |
166 | return (id); | |
2e73a022 | 167 | } |
95f0a918 | 168 | return (NULL); |
169 | } | |
170 | ||
c4087616 | 171 | /* Check confirmation of keysign request */ |
172 | static int | |
173 | confirm_key(Identity *id) | |
174 | { | |
175 | char *p, prompt[1024]; | |
176 | int ret = -1; | |
177 | ||
178 | p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX); | |
179 | snprintf(prompt, sizeof(prompt), "Allow use of key %s?\n" | |
180 | "Key fingerprint %s.", id->comment, p); | |
181 | xfree(p); | |
182 | p = read_passphrase(prompt, RP_ALLOW_EOF); | |
183 | if (p != NULL) { | |
184 | /* | |
aff51935 | 185 | * Accept empty responses and responses consisting |
c4087616 | 186 | * of the word "yes" as affirmative. |
187 | */ | |
188 | if (*p == '\0' || *p == '\n' || strcasecmp(p, "yes") == 0) | |
189 | ret = 0; | |
190 | xfree(p); | |
191 | } | |
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); |
2e73a022 | 320 | } |
321 | key_free(key); | |
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 | |
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); |
5260325f | 696 | msg_len = GET_32BIT(cp); |
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 | |
5260325f | 792 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) |
793 | error("fcntl O_NONBLOCK: %s", strerror(errno)); | |
794 | ||
795 | if (fd > max_fd) | |
796 | max_fd = fd; | |
797 | ||
798 | for (i = 0; i < sockets_alloc; i++) | |
799 | if (sockets[i].type == AUTH_UNUSED) { | |
800 | sockets[i].fd = fd; | |
5260325f | 801 | buffer_init(&sockets[i].input); |
802 | buffer_init(&sockets[i].output); | |
b9dd5ca1 | 803 | buffer_init(&sockets[i].request); |
c46e584f | 804 | sockets[i].type = type; |
5260325f | 805 | return; |
806 | } | |
807 | old_alloc = sockets_alloc; | |
c46e584f | 808 | new_alloc = sockets_alloc + 10; |
5260325f | 809 | if (sockets) |
c46e584f | 810 | sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0])); |
5260325f | 811 | else |
c46e584f | 812 | sockets = xmalloc(new_alloc * sizeof(sockets[0])); |
813 | for (i = old_alloc; i < new_alloc; i++) | |
5260325f | 814 | sockets[i].type = AUTH_UNUSED; |
c46e584f | 815 | sockets_alloc = new_alloc; |
5260325f | 816 | sockets[old_alloc].fd = fd; |
817 | buffer_init(&sockets[old_alloc].input); | |
818 | buffer_init(&sockets[old_alloc].output); | |
b9dd5ca1 | 819 | buffer_init(&sockets[old_alloc].request); |
c46e584f | 820 | sockets[old_alloc].type = type; |
8efc0c15 | 821 | } |
822 | ||
396c147e | 823 | static int |
e364646f | 824 | prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp) |
8efc0c15 | 825 | { |
42f11eb2 | 826 | u_int i, sz; |
827 | int n = 0; | |
828 | ||
829 | for (i = 0; i < sockets_alloc; i++) { | |
5260325f | 830 | switch (sockets[i].type) { |
831 | case AUTH_SOCKET: | |
832 | case AUTH_CONNECTION: | |
42f11eb2 | 833 | n = MAX(n, sockets[i].fd); |
5260325f | 834 | break; |
835 | case AUTH_UNUSED: | |
836 | break; | |
837 | default: | |
838 | fatal("Unknown socket type %d", sockets[i].type); | |
839 | break; | |
840 | } | |
42f11eb2 | 841 | } |
842 | ||
843 | sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); | |
e364646f | 844 | if (*fdrp == NULL || sz > *nallocp) { |
42f11eb2 | 845 | if (*fdrp) |
894c5fa6 | 846 | xfree(*fdrp); |
42f11eb2 | 847 | if (*fdwp) |
894c5fa6 | 848 | xfree(*fdwp); |
42f11eb2 | 849 | *fdrp = xmalloc(sz); |
850 | *fdwp = xmalloc(sz); | |
e364646f | 851 | *nallocp = sz; |
42f11eb2 | 852 | } |
e364646f | 853 | if (n < *fdl) |
854 | debug("XXX shrink: %d < %d", n, *fdl); | |
855 | *fdl = n; | |
42f11eb2 | 856 | memset(*fdrp, 0, sz); |
857 | memset(*fdwp, 0, sz); | |
858 | ||
859 | for (i = 0; i < sockets_alloc; i++) { | |
860 | switch (sockets[i].type) { | |
861 | case AUTH_SOCKET: | |
862 | case AUTH_CONNECTION: | |
863 | FD_SET(sockets[i].fd, *fdrp); | |
864 | if (buffer_len(&sockets[i].output) > 0) | |
865 | FD_SET(sockets[i].fd, *fdwp); | |
866 | break; | |
867 | default: | |
868 | break; | |
869 | } | |
870 | } | |
871 | return (1); | |
8efc0c15 | 872 | } |
873 | ||
396c147e | 874 | static void |
5260325f | 875 | after_select(fd_set *readset, fd_set *writeset) |
8efc0c15 | 876 | { |
e424e241 | 877 | struct sockaddr_un sunaddr; |
610cd5c6 | 878 | socklen_t slen; |
5260325f | 879 | char buf[1024]; |
e424e241 | 880 | int len, sock; |
881 | u_int i; | |
87f4111f | 882 | uid_t euid; |
883 | gid_t egid; | |
5260325f | 884 | |
885 | for (i = 0; i < sockets_alloc; i++) | |
886 | switch (sockets[i].type) { | |
887 | case AUTH_UNUSED: | |
888 | break; | |
889 | case AUTH_SOCKET: | |
890 | if (FD_ISSET(sockets[i].fd, readset)) { | |
610cd5c6 | 891 | slen = sizeof(sunaddr); |
42f11eb2 | 892 | sock = accept(sockets[i].fd, |
893 | (struct sockaddr *) &sunaddr, &slen); | |
5260325f | 894 | if (sock < 0) { |
fccfbe3b | 895 | error("accept from AUTH_SOCKET: %s", |
896 | strerror(errno)); | |
5260325f | 897 | break; |
898 | } | |
87f4111f | 899 | if (getpeereid(sock, &euid, &egid) < 0) { |
900 | error("getpeereid %d failed: %s", | |
901 | sock, strerror(errno)); | |
902 | close(sock); | |
903 | break; | |
904 | } | |
e139021d | 905 | if ((euid != 0) && (getuid() != euid)) { |
87f4111f | 906 | error("uid mismatch: " |
792e7d2d | 907 | "peer euid %u != uid %u", |
908 | (u_int) euid, (u_int) getuid()); | |
87f4111f | 909 | close(sock); |
910 | break; | |
911 | } | |
5260325f | 912 | new_socket(AUTH_CONNECTION, sock); |
913 | } | |
914 | break; | |
915 | case AUTH_CONNECTION: | |
916 | if (buffer_len(&sockets[i].output) > 0 && | |
917 | FD_ISSET(sockets[i].fd, writeset)) { | |
bbc62e59 | 918 | do { |
919 | len = write(sockets[i].fd, | |
920 | buffer_ptr(&sockets[i].output), | |
921 | buffer_len(&sockets[i].output)); | |
922 | if (len == -1 && (errno == EAGAIN || | |
923 | errno == EINTR)) | |
924 | continue; | |
925 | break; | |
926 | } while (1); | |
5260325f | 927 | if (len <= 0) { |
c1a4eef1 | 928 | close_socket(&sockets[i]); |
5260325f | 929 | break; |
930 | } | |
931 | buffer_consume(&sockets[i].output, len); | |
932 | } | |
933 | if (FD_ISSET(sockets[i].fd, readset)) { | |
bbc62e59 | 934 | do { |
935 | len = read(sockets[i].fd, buf, sizeof(buf)); | |
936 | if (len == -1 && (errno == EAGAIN || | |
937 | errno == EINTR)) | |
938 | continue; | |
939 | break; | |
940 | } while (1); | |
5260325f | 941 | if (len <= 0) { |
c1a4eef1 | 942 | close_socket(&sockets[i]); |
5260325f | 943 | break; |
944 | } | |
945 | buffer_append(&sockets[i].input, buf, len); | |
946 | process_message(&sockets[i]); | |
947 | } | |
948 | break; | |
949 | default: | |
950 | fatal("Unknown type %d", sockets[i].type); | |
951 | } | |
8efc0c15 | 952 | } |
953 | ||
396c147e | 954 | static void |
cd25664d | 955 | cleanup_socket(void) |
dae3fa13 | 956 | { |
2f4b2e38 | 957 | if (socket_name[0]) |
958 | unlink(socket_name); | |
959 | if (socket_dir[0]) | |
960 | rmdir(socket_dir); | |
8efc0c15 | 961 | } |
962 | ||
2362db19 | 963 | void |
dae3fa13 | 964 | cleanup_exit(int i) |
965 | { | |
cd25664d | 966 | cleanup_socket(); |
967 | _exit(i); | |
dae3fa13 | 968 | } |
969 | ||
396c147e | 970 | static void |
2f4b2e38 | 971 | cleanup_handler(int sig) |
972 | { | |
cd25664d | 973 | cleanup_socket(); |
2f4b2e38 | 974 | _exit(2); |
975 | } | |
976 | ||
7efa8482 | 977 | static void |
978 | check_parent_exists(int sig) | |
979 | { | |
980 | int save_errno = errno; | |
981 | ||
982 | if (parent_pid != -1 && kill(parent_pid, 0) < 0) { | |
983 | /* printf("Parent has died - Authentication agent exiting.\n"); */ | |
984 | cleanup_handler(sig); /* safe */ | |
985 | } | |
8d9bb5dd | 986 | mysignal(SIGALRM, check_parent_exists); |
7efa8482 | 987 | alarm(10); |
988 | errno = save_errno; | |
989 | } | |
990 | ||
396c147e | 991 | static void |
cc8aca8a | 992 | usage(void) |
dae3fa13 | 993 | { |
33e766d2 | 994 | fprintf(stderr, "Usage: %s [options] [command [args ...]]\n", |
0e3c1f95 | 995 | __progname); |
33e766d2 | 996 | fprintf(stderr, "Options:\n"); |
997 | fprintf(stderr, " -c Generate C-shell commands on stdout.\n"); | |
998 | fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n"); | |
999 | fprintf(stderr, " -k Kill the current agent.\n"); | |
1000 | fprintf(stderr, " -d Debug mode.\n"); | |
3e7efb37 | 1001 | fprintf(stderr, " -a socket Bind agent socket to given name.\n"); |
494b267f | 1002 | fprintf(stderr, " -t life Default identity lifetime (seconds).\n"); |
5260325f | 1003 | exit(1); |
dae3fa13 | 1004 | } |
1005 | ||
8efc0c15 | 1006 | int |
1007 | main(int ac, char **av) | |
1008 | { | |
c4087616 | 1009 | int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0; |
1010 | int sock, fd, ch, nalloc; | |
e424e241 | 1011 | char *shell, *format, *pidstr, *agentsocket = NULL; |
1012 | fd_set *readsetp = NULL, *writesetp = NULL; | |
5260325f | 1013 | struct sockaddr_un sunaddr; |
b03bd394 | 1014 | #ifdef HAVE_SETRLIMIT |
0b6fbf03 | 1015 | struct rlimit rlim; |
c7ccfd39 | 1016 | #endif |
1017 | #ifdef HAVE_CYGWIN | |
1018 | int prev_mask; | |
b03bd394 | 1019 | #endif |
c04f75f1 | 1020 | extern int optind; |
ef3912be | 1021 | extern char *optarg; |
e424e241 | 1022 | pid_t pid; |
1023 | char pidstrbuf[1 + 3 * sizeof pid]; | |
fcb975eb | 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"); |
1081 | if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) | |
1082 | c_flag = 1; | |
dae3fa13 | 1083 | } |
5260325f | 1084 | if (k_flag) { |
1085 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); | |
1086 | if (pidstr == NULL) { | |
1087 | fprintf(stderr, "%s not set, cannot kill agent\n", | |
42f11eb2 | 1088 | SSH_AGENTPID_ENV_NAME); |
5260325f | 1089 | exit(1); |
1090 | } | |
1091 | pid = atoi(pidstr); | |
42f11eb2 | 1092 | if (pid < 1) { |
5260325f | 1093 | fprintf(stderr, "%s=\"%s\", which is not a good PID\n", |
42f11eb2 | 1094 | SSH_AGENTPID_ENV_NAME, pidstr); |
5260325f | 1095 | exit(1); |
1096 | } | |
1097 | if (kill(pid, SIGTERM) == -1) { | |
1098 | perror("kill"); | |
1099 | exit(1); | |
1100 | } | |
1101 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; | |
1102 | printf(format, SSH_AUTHSOCKET_ENV_NAME); | |
1103 | printf(format, SSH_AGENTPID_ENV_NAME); | |
c457707e | 1104 | printf("echo Agent pid %ld killed;\n", (long)pid); |
5260325f | 1105 | exit(0); |
dae3fa13 | 1106 | } |
5260325f | 1107 | parent_pid = getpid(); |
1108 | ||
3e7efb37 | 1109 | if (agentsocket == NULL) { |
1110 | /* Create private directory for agent socket */ | |
c88de854 | 1111 | strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir); |
3e7efb37 | 1112 | if (mkdtemp(socket_dir) == NULL) { |
1113 | perror("mkdtemp: private socket dir"); | |
1114 | exit(1); | |
1115 | } | |
c457707e | 1116 | snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir, |
1117 | (long)parent_pid); | |
3e7efb37 | 1118 | } else { |
1119 | /* Try to use specified agent socket */ | |
1120 | socket_dir[0] = '\0'; | |
1121 | strlcpy(socket_name, agentsocket, sizeof socket_name); | |
5260325f | 1122 | } |
5260325f | 1123 | |
aa3378df | 1124 | /* |
1125 | * Create socket early so it will exist before command gets run from | |
1126 | * the parent. | |
1127 | */ | |
5260325f | 1128 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
1129 | if (sock < 0) { | |
1130 | perror("socket"); | |
1131 | cleanup_exit(1); | |
1132 | } | |
1133 | memset(&sunaddr, 0, sizeof(sunaddr)); | |
1134 | sunaddr.sun_family = AF_UNIX; | |
1135 | strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); | |
c7ccfd39 | 1136 | #ifdef HAVE_CYGWIN |
1137 | prev_mask = umask(0177); | |
1138 | #endif | |
5260325f | 1139 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { |
1140 | perror("bind"); | |
c7ccfd39 | 1141 | #ifdef HAVE_CYGWIN |
1142 | umask(prev_mask); | |
1143 | #endif | |
5260325f | 1144 | cleanup_exit(1); |
1145 | } | |
c7ccfd39 | 1146 | #ifdef HAVE_CYGWIN |
1147 | umask(prev_mask); | |
1148 | #endif | |
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 | } |