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