]>
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" | |
93c3b6de | 37 | RCSID("$OpenBSD: ssh-agent.c,v 1.82 2002/03/04 17:27:39 stevesk Exp $"); |
42f11eb2 | 38 | |
187cd1fa | 39 | #if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H) |
40 | #include <sys/queue.h> | |
41 | #else | |
42 | #include "openbsd-compat/fake-queue.h" | |
43 | #endif | |
44 | ||
42f11eb2 | 45 | #include <openssl/evp.h> |
46 | #include <openssl/md5.h> | |
8efc0c15 | 47 | |
48 | #include "ssh.h" | |
49 | #include "rsa.h" | |
8efc0c15 | 50 | #include "buffer.h" |
51 | #include "bufaux.h" | |
52 | #include "xmalloc.h" | |
8efc0c15 | 53 | #include "getput.h" |
4c8722d9 | 54 | #include "key.h" |
55 | #include "authfd.h" | |
188adeb2 | 56 | #include "compat.h" |
42f11eb2 | 57 | #include "log.h" |
8efc0c15 | 58 | |
2b5fe3b8 | 59 | #ifdef SMARTCARD |
60 | #include <openssl/engine.h> | |
61 | #include "scard.h" | |
dd2495cb | 62 | #endif |
2b5fe3b8 | 63 | |
17a3011c | 64 | typedef enum { |
65 | AUTH_UNUSED, | |
66 | AUTH_SOCKET, | |
67 | AUTH_CONNECTION | |
68 | } sock_type; | |
69 | ||
5260325f | 70 | typedef struct { |
71 | int fd; | |
17a3011c | 72 | sock_type type; |
5260325f | 73 | Buffer input; |
74 | Buffer output; | |
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; |
8efc0c15 | 84 | } Identity; |
85 | ||
2e73a022 | 86 | typedef struct { |
87 | int nentries; | |
95f0a918 | 88 | TAILQ_HEAD(idqueue, identity) idlist; |
2e73a022 | 89 | } Idtab; |
90 | ||
91 | /* private key table, one per protocol version */ | |
92 | Idtab idtable[3]; | |
8efc0c15 | 93 | |
94 | int max_fd = 0; | |
95 | ||
96 | /* pid of shell == parent of agent */ | |
9da5c3c9 | 97 | pid_t parent_pid = -1; |
8efc0c15 | 98 | |
99 | /* pathname and directory for AUTH_SOCKET */ | |
100 | char socket_name[1024]; | |
101 | char socket_dir[1024]; | |
102 | ||
5260325f | 103 | #ifdef HAVE___PROGNAME |
104 | extern char *__progname; | |
260d427b | 105 | #else |
106 | char *__progname; | |
107 | #endif | |
5260325f | 108 | |
396c147e | 109 | static void |
2e73a022 | 110 | idtab_init(void) |
111 | { | |
112 | int i; | |
6aacefa7 | 113 | for (i = 0; i <=2; i++) { |
95f0a918 | 114 | TAILQ_INIT(&idtable[i].idlist); |
2e73a022 | 115 | idtable[i].nentries = 0; |
116 | } | |
117 | } | |
118 | ||
119 | /* return private key table for requested protocol version */ | |
396c147e | 120 | static Idtab * |
2e73a022 | 121 | idtab_lookup(int version) |
122 | { | |
123 | if (version < 1 || version > 2) | |
124 | fatal("internal error, bad protocol version %d", version); | |
125 | return &idtable[version]; | |
126 | } | |
127 | ||
128 | /* return matching private key for given public key */ | |
95f0a918 | 129 | static Identity * |
130 | lookup_identity(Key *key, int version) | |
2e73a022 | 131 | { |
95f0a918 | 132 | Identity *id; |
133 | ||
2e73a022 | 134 | Idtab *tab = idtab_lookup(version); |
95f0a918 | 135 | TAILQ_FOREACH(id, &tab->idlist, next) { |
136 | if (key_equal(key, id->key)) | |
137 | return (id); | |
2e73a022 | 138 | } |
95f0a918 | 139 | return (NULL); |
140 | } | |
141 | ||
142 | static void | |
143 | free_identity(Identity *id) | |
144 | { | |
145 | key_free(id->key); | |
146 | xfree(id->comment); | |
147 | xfree(id); | |
2e73a022 | 148 | } |
149 | ||
150 | /* send list of supported public keys to 'client' */ | |
396c147e | 151 | static void |
2e73a022 | 152 | process_request_identities(SocketEntry *e, int version) |
8efc0c15 | 153 | { |
2e73a022 | 154 | Idtab *tab = idtab_lookup(version); |
5260325f | 155 | Buffer msg; |
95f0a918 | 156 | Identity *id; |
5260325f | 157 | |
158 | buffer_init(&msg); | |
2e73a022 | 159 | buffer_put_char(&msg, (version == 1) ? |
160 | SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); | |
161 | buffer_put_int(&msg, tab->nentries); | |
95f0a918 | 162 | TAILQ_FOREACH(id, &tab->idlist, next) { |
fa08c86b | 163 | if (id->key->type == KEY_RSA1) { |
2e73a022 | 164 | buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); |
165 | buffer_put_bignum(&msg, id->key->rsa->e); | |
166 | buffer_put_bignum(&msg, id->key->rsa->n); | |
167 | } else { | |
1e3b8b07 | 168 | u_char *blob; |
169 | u_int blen; | |
fa08c86b | 170 | key_to_blob(id->key, &blob, &blen); |
2e73a022 | 171 | buffer_put_string(&msg, blob, blen); |
172 | xfree(blob); | |
173 | } | |
174 | buffer_put_cstring(&msg, id->comment); | |
5260325f | 175 | } |
176 | buffer_put_int(&e->output, buffer_len(&msg)); | |
177 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); | |
178 | buffer_free(&msg); | |
8efc0c15 | 179 | } |
180 | ||
2e73a022 | 181 | /* ssh1 only */ |
396c147e | 182 | static void |
2e73a022 | 183 | process_authentication_challenge1(SocketEntry *e) |
8efc0c15 | 184 | { |
95f0a918 | 185 | Identity *id; |
186 | Key *key; | |
2e73a022 | 187 | BIGNUM *challenge; |
188 | int i, len; | |
5260325f | 189 | Buffer msg; |
190 | MD5_CTX md; | |
1e3b8b07 | 191 | u_char buf[32], mdbuf[16], session_id[16]; |
192 | u_int response_type; | |
5260325f | 193 | |
194 | buffer_init(&msg); | |
fa08c86b | 195 | key = key_new(KEY_RSA1); |
b775c6f2 | 196 | if ((challenge = BN_new()) == NULL) |
197 | fatal("process_authentication_challenge1: BN_new failed"); | |
5260325f | 198 | |
2e73a022 | 199 | buffer_get_int(&e->input); /* ignored */ |
200 | buffer_get_bignum(&e->input, key->rsa->e); | |
201 | buffer_get_bignum(&e->input, key->rsa->n); | |
202 | buffer_get_bignum(&e->input, challenge); | |
5260325f | 203 | |
2e73a022 | 204 | /* Only protocol 1.1 is supported */ |
205 | if (buffer_len(&e->input) == 0) | |
206 | goto failure; | |
e6207598 | 207 | buffer_get(&e->input, session_id, 16); |
2e73a022 | 208 | response_type = buffer_get_int(&e->input); |
209 | if (response_type != 1) | |
210 | goto failure; | |
211 | ||
95f0a918 | 212 | id = lookup_identity(key, 1); |
213 | if (id != NULL) { | |
214 | Key *private = id->key; | |
2e73a022 | 215 | /* Decrypt the challenge using the private key. */ |
46aa2d1f | 216 | if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) |
217 | goto failure; | |
2e73a022 | 218 | |
219 | /* The response is MD5 of decrypted challenge plus session id. */ | |
220 | len = BN_num_bytes(challenge); | |
221 | if (len <= 0 || len > 32) { | |
222 | log("process_authentication_challenge: bad challenge length %d", len); | |
223 | goto failure; | |
5260325f | 224 | } |
2e73a022 | 225 | memset(buf, 0, 32); |
226 | BN_bn2bin(challenge, buf + 32 - len); | |
227 | MD5_Init(&md); | |
228 | MD5_Update(&md, buf, 32); | |
229 | MD5_Update(&md, session_id, 16); | |
230 | MD5_Final(mdbuf, &md); | |
231 | ||
232 | /* Send the response. */ | |
233 | buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); | |
234 | for (i = 0; i < 16; i++) | |
235 | buffer_put_char(&msg, mdbuf[i]); | |
236 | goto send; | |
237 | } | |
238 | ||
239 | failure: | |
240 | /* Unknown identity or protocol error. Send failure. */ | |
5260325f | 241 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
242 | send: | |
2e73a022 | 243 | buffer_put_int(&e->output, buffer_len(&msg)); |
244 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); | |
245 | key_free(key); | |
246 | BN_clear_free(challenge); | |
247 | buffer_free(&msg); | |
248 | } | |
249 | ||
250 | /* ssh2 only */ | |
396c147e | 251 | static void |
2e73a022 | 252 | process_sign_request2(SocketEntry *e) |
253 | { | |
254 | extern int datafellows; | |
95f0a918 | 255 | Key *key; |
1e3b8b07 | 256 | u_char *blob, *data, *signature = NULL; |
257 | u_int blen, dlen, slen = 0; | |
188adeb2 | 258 | int flags; |
2e73a022 | 259 | Buffer msg; |
260 | int ok = -1; | |
261 | ||
262 | datafellows = 0; | |
227e8e86 | 263 | |
2e73a022 | 264 | blob = buffer_get_string(&e->input, &blen); |
265 | data = buffer_get_string(&e->input, &dlen); | |
188adeb2 | 266 | |
267 | flags = buffer_get_int(&e->input); | |
268 | if (flags & SSH_AGENT_OLD_SIGNATURE) | |
269 | datafellows = SSH_BUG_SIGBLOB; | |
2e73a022 | 270 | |
fa08c86b | 271 | key = key_from_blob(blob, blen); |
2e73a022 | 272 | if (key != NULL) { |
95f0a918 | 273 | Identity *id = lookup_identity(key, 2); |
274 | if (id != NULL) | |
275 | ok = key_sign(id->key, &signature, &slen, data, dlen); | |
2e73a022 | 276 | } |
277 | key_free(key); | |
278 | buffer_init(&msg); | |
279 | if (ok == 0) { | |
280 | buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); | |
281 | buffer_put_string(&msg, signature, slen); | |
282 | } else { | |
283 | buffer_put_char(&msg, SSH_AGENT_FAILURE); | |
284 | } | |
5260325f | 285 | buffer_put_int(&e->output, buffer_len(&msg)); |
286 | buffer_append(&e->output, buffer_ptr(&msg), | |
2e73a022 | 287 | buffer_len(&msg)); |
5260325f | 288 | buffer_free(&msg); |
2e73a022 | 289 | xfree(data); |
290 | xfree(blob); | |
291 | if (signature != NULL) | |
292 | xfree(signature); | |
8efc0c15 | 293 | } |
294 | ||
2e73a022 | 295 | /* shared */ |
396c147e | 296 | static void |
2e73a022 | 297 | process_remove_identity(SocketEntry *e, int version) |
8efc0c15 | 298 | { |
95f0a918 | 299 | Key *key = NULL; |
1e3b8b07 | 300 | u_char *blob; |
301 | u_int blen; | |
302 | u_int bits; | |
2e73a022 | 303 | int success = 0; |
304 | ||
6aacefa7 | 305 | switch (version) { |
2e73a022 | 306 | case 1: |
fa08c86b | 307 | key = key_new(KEY_RSA1); |
2e73a022 | 308 | bits = buffer_get_int(&e->input); |
309 | buffer_get_bignum(&e->input, key->rsa->e); | |
310 | buffer_get_bignum(&e->input, key->rsa->n); | |
311 | ||
312 | if (bits != key_size(key)) | |
313 | log("Warning: identity keysize mismatch: actual %d, announced %d", | |
42f11eb2 | 314 | key_size(key), bits); |
2e73a022 | 315 | break; |
316 | case 2: | |
317 | blob = buffer_get_string(&e->input, &blen); | |
fa08c86b | 318 | key = key_from_blob(blob, blen); |
2e73a022 | 319 | xfree(blob); |
320 | break; | |
321 | } | |
322 | if (key != NULL) { | |
95f0a918 | 323 | Identity *id = lookup_identity(key, version); |
324 | if (id != NULL) { | |
aa3378df | 325 | /* |
326 | * We have this key. Free the old key. Since we | |
327 | * don\'t want to leave empty slots in the middle of | |
d343d900 | 328 | * the array, we actually free the key there and move |
329 | * all the entries between the empty slot and the end | |
330 | * of the array. | |
aa3378df | 331 | */ |
2e73a022 | 332 | Idtab *tab = idtab_lookup(version); |
fa08c86b | 333 | if (tab->nentries < 1) |
334 | fatal("process_remove_identity: " | |
335 | "internal error: tab->nentries %d", | |
336 | tab->nentries); | |
95f0a918 | 337 | TAILQ_REMOVE(&tab->idlist, id, next); |
338 | free_identity(id); | |
2e73a022 | 339 | tab->nentries--; |
340 | success = 1; | |
5260325f | 341 | } |
2e73a022 | 342 | key_free(key); |
343 | } | |
8efc0c15 | 344 | buffer_put_int(&e->output, 1); |
2e73a022 | 345 | buffer_put_char(&e->output, |
346 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
8efc0c15 | 347 | } |
348 | ||
396c147e | 349 | static void |
2e73a022 | 350 | process_remove_all_identities(SocketEntry *e, int version) |
8efc0c15 | 351 | { |
2e73a022 | 352 | Idtab *tab = idtab_lookup(version); |
95f0a918 | 353 | Identity *id; |
8efc0c15 | 354 | |
5260325f | 355 | /* Loop over all identities and clear the keys. */ |
95f0a918 | 356 | for (id = TAILQ_FIRST(&tab->idlist); id; |
357 | id = TAILQ_FIRST(&tab->idlist)) { | |
358 | TAILQ_REMOVE(&tab->idlist, id, next); | |
359 | free_identity(id); | |
5260325f | 360 | } |
8efc0c15 | 361 | |
5260325f | 362 | /* Mark that there are no identities. */ |
2e73a022 | 363 | tab->nentries = 0; |
8efc0c15 | 364 | |
365 | /* Send success. */ | |
366 | buffer_put_int(&e->output, 1); | |
367 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); | |
368 | return; | |
5260325f | 369 | } |
370 | ||
396c147e | 371 | static void |
fa08c86b | 372 | process_add_identity(SocketEntry *e, int version) |
373 | { | |
374 | Key *k = NULL; | |
375 | char *type_name; | |
2e73a022 | 376 | char *comment; |
fa08c86b | 377 | int type, success = 0; |
2e73a022 | 378 | Idtab *tab = idtab_lookup(version); |
5260325f | 379 | |
2e73a022 | 380 | switch (version) { |
381 | case 1: | |
fa08c86b | 382 | k = key_new_private(KEY_RSA1); |
42f11eb2 | 383 | buffer_get_int(&e->input); /* ignored */ |
fa08c86b | 384 | buffer_get_bignum(&e->input, k->rsa->n); |
385 | buffer_get_bignum(&e->input, k->rsa->e); | |
386 | buffer_get_bignum(&e->input, k->rsa->d); | |
387 | buffer_get_bignum(&e->input, k->rsa->iqmp); | |
2e73a022 | 388 | |
389 | /* SSH and SSL have p and q swapped */ | |
fa08c86b | 390 | buffer_get_bignum(&e->input, k->rsa->q); /* p */ |
391 | buffer_get_bignum(&e->input, k->rsa->p); /* q */ | |
2e73a022 | 392 | |
393 | /* Generate additional parameters */ | |
2b63e803 | 394 | rsa_generate_additional_parameters(k->rsa); |
2e73a022 | 395 | break; |
396 | case 2: | |
fa08c86b | 397 | type_name = buffer_get_string(&e->input, NULL); |
42f11eb2 | 398 | type = key_type_from_name(type_name); |
fa08c86b | 399 | xfree(type_name); |
6aacefa7 | 400 | switch (type) { |
fa08c86b | 401 | case KEY_DSA: |
402 | k = key_new_private(type); | |
403 | buffer_get_bignum2(&e->input, k->dsa->p); | |
404 | buffer_get_bignum2(&e->input, k->dsa->q); | |
405 | buffer_get_bignum2(&e->input, k->dsa->g); | |
406 | buffer_get_bignum2(&e->input, k->dsa->pub_key); | |
407 | buffer_get_bignum2(&e->input, k->dsa->priv_key); | |
408 | break; | |
409 | case KEY_RSA: | |
410 | k = key_new_private(type); | |
411 | buffer_get_bignum2(&e->input, k->rsa->n); | |
412 | buffer_get_bignum2(&e->input, k->rsa->e); | |
413 | buffer_get_bignum2(&e->input, k->rsa->d); | |
414 | buffer_get_bignum2(&e->input, k->rsa->iqmp); | |
415 | buffer_get_bignum2(&e->input, k->rsa->p); | |
416 | buffer_get_bignum2(&e->input, k->rsa->q); | |
417 | ||
418 | /* Generate additional parameters */ | |
2b63e803 | 419 | rsa_generate_additional_parameters(k->rsa); |
fa08c86b | 420 | break; |
421 | default: | |
2e73a022 | 422 | buffer_clear(&e->input); |
2e73a022 | 423 | goto send; |
5260325f | 424 | } |
2e73a022 | 425 | break; |
426 | } | |
2e73a022 | 427 | comment = buffer_get_string(&e->input, NULL); |
428 | if (k == NULL) { | |
429 | xfree(comment); | |
430 | goto send; | |
431 | } | |
432 | success = 1; | |
95f0a918 | 433 | if (lookup_identity(k, version) == NULL) { |
434 | Identity *id = xmalloc(sizeof(Identity)); | |
435 | id->key = k; | |
436 | id->comment = comment; | |
437 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); | |
2e73a022 | 438 | /* Increment the number of identities. */ |
439 | tab->nentries++; | |
440 | } else { | |
441 | key_free(k); | |
442 | xfree(comment); | |
443 | } | |
444 | send: | |
5260325f | 445 | buffer_put_int(&e->output, 1); |
2e73a022 | 446 | buffer_put_char(&e->output, |
447 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
8efc0c15 | 448 | } |
449 | ||
2b5fe3b8 | 450 | |
451 | #ifdef SMARTCARD | |
452 | static void | |
453 | process_add_smartcard_key (SocketEntry *e) | |
454 | { | |
455 | Idtab *tab; | |
456 | Key *n = NULL, *k = NULL; | |
9ff6f66f | 457 | char *sc_reader_id = NULL; |
2b5fe3b8 | 458 | int success = 0; |
184eed6a | 459 | |
9ff6f66f | 460 | sc_reader_id = buffer_get_string(&e->input, NULL); |
461 | k = sc_get_key(sc_reader_id); | |
462 | xfree(sc_reader_id); | |
2b5fe3b8 | 463 | |
2b5fe3b8 | 464 | if (k == NULL) { |
465 | error("sc_get_pubkey failed"); | |
466 | goto send; | |
467 | } | |
468 | success = 1; | |
469 | ||
470 | tab = idtab_lookup(1); | |
a0e0f486 | 471 | k->type = KEY_RSA1; |
95f0a918 | 472 | if (lookup_identity(k, 1) == NULL) { |
473 | Identity *id = xmalloc(sizeof(Identity)); | |
2b5fe3b8 | 474 | n = key_new(KEY_RSA1); |
475 | BN_copy(n->rsa->n, k->rsa->n); | |
476 | BN_copy(n->rsa->e, k->rsa->e); | |
477 | RSA_set_method(n->rsa, sc_get_engine()); | |
95f0a918 | 478 | id->key = n; |
479 | id->comment = xstrdup("rsa1 smartcard"); | |
480 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); | |
2b5fe3b8 | 481 | tab->nentries++; |
482 | } | |
a0e0f486 | 483 | k->type = KEY_RSA; |
2b5fe3b8 | 484 | tab = idtab_lookup(2); |
95f0a918 | 485 | if (lookup_identity(k, 2) == NULL) { |
486 | Identity *id = xmalloc(sizeof(Identity)); | |
2b5fe3b8 | 487 | n = key_new(KEY_RSA); |
488 | BN_copy(n->rsa->n, k->rsa->n); | |
489 | BN_copy(n->rsa->e, k->rsa->e); | |
490 | RSA_set_method(n->rsa, sc_get_engine()); | |
95f0a918 | 491 | id->key = n; |
492 | id->comment = xstrdup("rsa smartcard"); | |
493 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); | |
2b5fe3b8 | 494 | tab->nentries++; |
495 | } | |
496 | key_free(k); | |
497 | send: | |
498 | buffer_put_int(&e->output, 1); | |
499 | buffer_put_char(&e->output, | |
500 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
501 | } | |
502 | ||
503 | static void | |
504 | process_remove_smartcard_key(SocketEntry *e) | |
505 | { | |
95f0a918 | 506 | Key *k = NULL; |
2b5fe3b8 | 507 | int success = 0; |
9ff6f66f | 508 | char *sc_reader_id = NULL; |
2b5fe3b8 | 509 | |
9ff6f66f | 510 | sc_reader_id = buffer_get_string(&e->input, NULL); |
511 | k = sc_get_key(sc_reader_id); | |
512 | xfree(sc_reader_id); | |
2b5fe3b8 | 513 | |
9ff6f66f | 514 | if (k == NULL) { |
2b5fe3b8 | 515 | error("sc_get_pubkey failed"); |
516 | } else { | |
95f0a918 | 517 | Identity *id; |
77261db4 | 518 | k->type = KEY_RSA1; |
95f0a918 | 519 | id = lookup_identity(k, 1); |
520 | if (id != NULL) { | |
2b5fe3b8 | 521 | Idtab *tab = idtab_lookup(1); |
95f0a918 | 522 | TAILQ_REMOVE(&tab->idlist, id, next); |
523 | free_identity(id); | |
2b5fe3b8 | 524 | tab->nentries--; |
525 | success = 1; | |
526 | } | |
77261db4 | 527 | k->type = KEY_RSA; |
95f0a918 | 528 | id = lookup_identity(k, 2); |
529 | if (id != NULL) { | |
2b5fe3b8 | 530 | Idtab *tab = idtab_lookup(2); |
95f0a918 | 531 | TAILQ_REMOVE(&tab->idlist, id, next); |
532 | free_identity(id); | |
2b5fe3b8 | 533 | tab->nentries--; |
534 | success = 1; | |
535 | } | |
536 | key_free(k); | |
537 | } | |
538 | ||
539 | buffer_put_int(&e->output, 1); | |
540 | buffer_put_char(&e->output, | |
541 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
542 | } | |
3e984472 | 543 | #endif /* SMARTCARD */ |
2b5fe3b8 | 544 | |
2e73a022 | 545 | /* dispatch incoming messages */ |
546 | ||
396c147e | 547 | static void |
8efc0c15 | 548 | process_message(SocketEntry *e) |
549 | { | |
1e3b8b07 | 550 | u_int msg_len; |
551 | u_int type; | |
552 | u_char *cp; | |
5260325f | 553 | if (buffer_len(&e->input) < 5) |
554 | return; /* Incomplete message. */ | |
20905a8e | 555 | cp = buffer_ptr(&e->input); |
5260325f | 556 | msg_len = GET_32BIT(cp); |
557 | if (msg_len > 256 * 1024) { | |
558 | shutdown(e->fd, SHUT_RDWR); | |
559 | close(e->fd); | |
560 | e->type = AUTH_UNUSED; | |
561 | return; | |
562 | } | |
563 | if (buffer_len(&e->input) < msg_len + 4) | |
564 | return; | |
565 | buffer_consume(&e->input, 4); | |
566 | type = buffer_get_char(&e->input); | |
567 | ||
2b5fe3b8 | 568 | debug("type %d", type); |
5260325f | 569 | switch (type) { |
2e73a022 | 570 | /* ssh1 */ |
5260325f | 571 | case SSH_AGENTC_RSA_CHALLENGE: |
2e73a022 | 572 | process_authentication_challenge1(e); |
573 | break; | |
574 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: | |
575 | process_request_identities(e, 1); | |
5260325f | 576 | break; |
577 | case SSH_AGENTC_ADD_RSA_IDENTITY: | |
2e73a022 | 578 | process_add_identity(e, 1); |
5260325f | 579 | break; |
580 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: | |
2e73a022 | 581 | process_remove_identity(e, 1); |
5260325f | 582 | break; |
583 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: | |
2e73a022 | 584 | process_remove_all_identities(e, 1); |
585 | break; | |
586 | /* ssh2 */ | |
587 | case SSH2_AGENTC_SIGN_REQUEST: | |
588 | process_sign_request2(e); | |
589 | break; | |
590 | case SSH2_AGENTC_REQUEST_IDENTITIES: | |
591 | process_request_identities(e, 2); | |
592 | break; | |
593 | case SSH2_AGENTC_ADD_IDENTITY: | |
594 | process_add_identity(e, 2); | |
595 | break; | |
596 | case SSH2_AGENTC_REMOVE_IDENTITY: | |
597 | process_remove_identity(e, 2); | |
598 | break; | |
599 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: | |
600 | process_remove_all_identities(e, 2); | |
5260325f | 601 | break; |
2b5fe3b8 | 602 | #ifdef SMARTCARD |
603 | case SSH_AGENTC_ADD_SMARTCARD_KEY: | |
604 | process_add_smartcard_key(e); | |
184eed6a | 605 | break; |
2b5fe3b8 | 606 | case SSH_AGENTC_REMOVE_SMARTCARD_KEY: |
607 | process_remove_smartcard_key(e); | |
184eed6a | 608 | break; |
3e984472 | 609 | #endif /* SMARTCARD */ |
5260325f | 610 | default: |
611 | /* Unknown message. Respond with failure. */ | |
612 | error("Unknown message %d", type); | |
613 | buffer_clear(&e->input); | |
614 | buffer_put_int(&e->output, 1); | |
615 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); | |
616 | break; | |
617 | } | |
8efc0c15 | 618 | } |
619 | ||
396c147e | 620 | static void |
17a3011c | 621 | new_socket(sock_type type, int fd) |
8efc0c15 | 622 | { |
1e3b8b07 | 623 | u_int i, old_alloc; |
5260325f | 624 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) |
625 | error("fcntl O_NONBLOCK: %s", strerror(errno)); | |
626 | ||
627 | if (fd > max_fd) | |
628 | max_fd = fd; | |
629 | ||
630 | for (i = 0; i < sockets_alloc; i++) | |
631 | if (sockets[i].type == AUTH_UNUSED) { | |
632 | sockets[i].fd = fd; | |
633 | sockets[i].type = type; | |
634 | buffer_init(&sockets[i].input); | |
635 | buffer_init(&sockets[i].output); | |
636 | return; | |
637 | } | |
638 | old_alloc = sockets_alloc; | |
639 | sockets_alloc += 10; | |
640 | if (sockets) | |
641 | sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); | |
642 | else | |
643 | sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); | |
644 | for (i = old_alloc; i < sockets_alloc; i++) | |
645 | sockets[i].type = AUTH_UNUSED; | |
646 | sockets[old_alloc].type = type; | |
647 | sockets[old_alloc].fd = fd; | |
648 | buffer_init(&sockets[old_alloc].input); | |
649 | buffer_init(&sockets[old_alloc].output); | |
8efc0c15 | 650 | } |
651 | ||
396c147e | 652 | static int |
e364646f | 653 | prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp) |
8efc0c15 | 654 | { |
42f11eb2 | 655 | u_int i, sz; |
656 | int n = 0; | |
657 | ||
658 | for (i = 0; i < sockets_alloc; i++) { | |
5260325f | 659 | switch (sockets[i].type) { |
660 | case AUTH_SOCKET: | |
661 | case AUTH_CONNECTION: | |
42f11eb2 | 662 | n = MAX(n, sockets[i].fd); |
5260325f | 663 | break; |
664 | case AUTH_UNUSED: | |
665 | break; | |
666 | default: | |
667 | fatal("Unknown socket type %d", sockets[i].type); | |
668 | break; | |
669 | } | |
42f11eb2 | 670 | } |
671 | ||
672 | sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); | |
e364646f | 673 | if (*fdrp == NULL || sz > *nallocp) { |
42f11eb2 | 674 | if (*fdrp) |
894c5fa6 | 675 | xfree(*fdrp); |
42f11eb2 | 676 | if (*fdwp) |
894c5fa6 | 677 | xfree(*fdwp); |
42f11eb2 | 678 | *fdrp = xmalloc(sz); |
679 | *fdwp = xmalloc(sz); | |
e364646f | 680 | *nallocp = sz; |
42f11eb2 | 681 | } |
e364646f | 682 | if (n < *fdl) |
683 | debug("XXX shrink: %d < %d", n, *fdl); | |
684 | *fdl = n; | |
42f11eb2 | 685 | memset(*fdrp, 0, sz); |
686 | memset(*fdwp, 0, sz); | |
687 | ||
688 | for (i = 0; i < sockets_alloc; i++) { | |
689 | switch (sockets[i].type) { | |
690 | case AUTH_SOCKET: | |
691 | case AUTH_CONNECTION: | |
692 | FD_SET(sockets[i].fd, *fdrp); | |
693 | if (buffer_len(&sockets[i].output) > 0) | |
694 | FD_SET(sockets[i].fd, *fdwp); | |
695 | break; | |
696 | default: | |
697 | break; | |
698 | } | |
699 | } | |
700 | return (1); | |
8efc0c15 | 701 | } |
702 | ||
396c147e | 703 | static void |
5260325f | 704 | after_select(fd_set *readset, fd_set *writeset) |
8efc0c15 | 705 | { |
1e3b8b07 | 706 | u_int i; |
5260325f | 707 | int len, sock; |
610cd5c6 | 708 | socklen_t slen; |
5260325f | 709 | char buf[1024]; |
710 | struct sockaddr_un sunaddr; | |
711 | ||
712 | for (i = 0; i < sockets_alloc; i++) | |
713 | switch (sockets[i].type) { | |
714 | case AUTH_UNUSED: | |
715 | break; | |
716 | case AUTH_SOCKET: | |
717 | if (FD_ISSET(sockets[i].fd, readset)) { | |
610cd5c6 | 718 | slen = sizeof(sunaddr); |
42f11eb2 | 719 | sock = accept(sockets[i].fd, |
720 | (struct sockaddr *) &sunaddr, &slen); | |
5260325f | 721 | if (sock < 0) { |
fccfbe3b | 722 | error("accept from AUTH_SOCKET: %s", |
723 | strerror(errno)); | |
5260325f | 724 | break; |
725 | } | |
726 | new_socket(AUTH_CONNECTION, sock); | |
727 | } | |
728 | break; | |
729 | case AUTH_CONNECTION: | |
730 | if (buffer_len(&sockets[i].output) > 0 && | |
731 | FD_ISSET(sockets[i].fd, writeset)) { | |
bbc62e59 | 732 | do { |
733 | len = write(sockets[i].fd, | |
734 | buffer_ptr(&sockets[i].output), | |
735 | buffer_len(&sockets[i].output)); | |
736 | if (len == -1 && (errno == EAGAIN || | |
737 | errno == EINTR)) | |
738 | continue; | |
739 | break; | |
740 | } while (1); | |
5260325f | 741 | if (len <= 0) { |
742 | shutdown(sockets[i].fd, SHUT_RDWR); | |
743 | close(sockets[i].fd); | |
744 | sockets[i].type = AUTH_UNUSED; | |
0ac7199f | 745 | buffer_free(&sockets[i].input); |
746 | buffer_free(&sockets[i].output); | |
5260325f | 747 | break; |
748 | } | |
749 | buffer_consume(&sockets[i].output, len); | |
750 | } | |
751 | if (FD_ISSET(sockets[i].fd, readset)) { | |
bbc62e59 | 752 | do { |
753 | len = read(sockets[i].fd, buf, sizeof(buf)); | |
754 | if (len == -1 && (errno == EAGAIN || | |
755 | errno == EINTR)) | |
756 | continue; | |
757 | break; | |
758 | } while (1); | |
5260325f | 759 | if (len <= 0) { |
760 | shutdown(sockets[i].fd, SHUT_RDWR); | |
761 | close(sockets[i].fd); | |
762 | sockets[i].type = AUTH_UNUSED; | |
0ac7199f | 763 | buffer_free(&sockets[i].input); |
764 | buffer_free(&sockets[i].output); | |
5260325f | 765 | break; |
766 | } | |
767 | buffer_append(&sockets[i].input, buf, len); | |
768 | process_message(&sockets[i]); | |
769 | } | |
770 | break; | |
771 | default: | |
772 | fatal("Unknown type %d", sockets[i].type); | |
773 | } | |
8efc0c15 | 774 | } |
775 | ||
396c147e | 776 | static void |
fccfbe3b | 777 | cleanup_socket(void *p) |
dae3fa13 | 778 | { |
2f4b2e38 | 779 | if (socket_name[0]) |
780 | unlink(socket_name); | |
781 | if (socket_dir[0]) | |
782 | rmdir(socket_dir); | |
8efc0c15 | 783 | } |
784 | ||
396c147e | 785 | static void |
dae3fa13 | 786 | cleanup_exit(int i) |
787 | { | |
fccfbe3b | 788 | cleanup_socket(NULL); |
5260325f | 789 | exit(i); |
dae3fa13 | 790 | } |
791 | ||
396c147e | 792 | static void |
2f4b2e38 | 793 | cleanup_handler(int sig) |
794 | { | |
fccfbe3b | 795 | cleanup_socket(NULL); |
2f4b2e38 | 796 | _exit(2); |
797 | } | |
798 | ||
7efa8482 | 799 | static void |
800 | check_parent_exists(int sig) | |
801 | { | |
802 | int save_errno = errno; | |
803 | ||
804 | if (parent_pid != -1 && kill(parent_pid, 0) < 0) { | |
805 | /* printf("Parent has died - Authentication agent exiting.\n"); */ | |
806 | cleanup_handler(sig); /* safe */ | |
807 | } | |
808 | signal(SIGALRM, check_parent_exists); | |
809 | alarm(10); | |
810 | errno = save_errno; | |
811 | } | |
812 | ||
396c147e | 813 | static void |
cc8aca8a | 814 | usage(void) |
dae3fa13 | 815 | { |
33e766d2 | 816 | fprintf(stderr, "Usage: %s [options] [command [args ...]]\n", |
0e3c1f95 | 817 | __progname); |
33e766d2 | 818 | fprintf(stderr, "Options:\n"); |
819 | fprintf(stderr, " -c Generate C-shell commands on stdout.\n"); | |
820 | fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n"); | |
821 | fprintf(stderr, " -k Kill the current agent.\n"); | |
822 | fprintf(stderr, " -d Debug mode.\n"); | |
5260325f | 823 | exit(1); |
dae3fa13 | 824 | } |
825 | ||
8efc0c15 | 826 | int |
827 | main(int ac, char **av) | |
828 | { | |
e364646f | 829 | int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc; |
5260325f | 830 | struct sockaddr_un sunaddr; |
b03bd394 | 831 | #ifdef HAVE_SETRLIMIT |
0b6fbf03 | 832 | struct rlimit rlim; |
c7ccfd39 | 833 | #endif |
834 | #ifdef HAVE_CYGWIN | |
835 | int prev_mask; | |
b03bd394 | 836 | #endif |
5260325f | 837 | pid_t pid; |
838 | char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid]; | |
c04f75f1 | 839 | extern int optind; |
42f11eb2 | 840 | fd_set *readsetp = NULL, *writesetp = NULL; |
fcb975eb | 841 | |
457fc0c6 | 842 | SSLeay_add_all_algorithms(); |
843 | ||
260d427b | 844 | __progname = get_progname(av[0]); |
264dce47 | 845 | init_rng(); |
e339aa53 | 846 | seed_rng(); |
2b87da3b | 847 | |
4e577b89 | 848 | #ifdef __GNU_LIBRARY__ |
ffdb5d70 | 849 | while ((ch = getopt(ac, av, "+cdks")) != -1) { |
4e577b89 | 850 | #else /* __GNU_LIBRARY__ */ |
ffdb5d70 | 851 | while ((ch = getopt(ac, av, "cdks")) != -1) { |
4e577b89 | 852 | #endif /* __GNU_LIBRARY__ */ |
5260325f | 853 | switch (ch) { |
854 | case 'c': | |
855 | if (s_flag) | |
856 | usage(); | |
857 | c_flag++; | |
858 | break; | |
859 | case 'k': | |
860 | k_flag++; | |
861 | break; | |
862 | case 's': | |
863 | if (c_flag) | |
864 | usage(); | |
865 | s_flag++; | |
866 | break; | |
ffdb5d70 | 867 | case 'd': |
868 | if (d_flag) | |
869 | usage(); | |
870 | d_flag++; | |
871 | break; | |
5260325f | 872 | default: |
873 | usage(); | |
874 | } | |
dae3fa13 | 875 | } |
5260325f | 876 | ac -= optind; |
877 | av += optind; | |
878 | ||
ffdb5d70 | 879 | if (ac > 0 && (c_flag || k_flag || s_flag || d_flag)) |
5260325f | 880 | usage(); |
881 | ||
ffdb5d70 | 882 | if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) { |
5260325f | 883 | shell = getenv("SHELL"); |
884 | if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) | |
885 | c_flag = 1; | |
dae3fa13 | 886 | } |
5260325f | 887 | if (k_flag) { |
888 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); | |
889 | if (pidstr == NULL) { | |
890 | fprintf(stderr, "%s not set, cannot kill agent\n", | |
42f11eb2 | 891 | SSH_AGENTPID_ENV_NAME); |
5260325f | 892 | exit(1); |
893 | } | |
894 | pid = atoi(pidstr); | |
42f11eb2 | 895 | if (pid < 1) { |
5260325f | 896 | fprintf(stderr, "%s=\"%s\", which is not a good PID\n", |
42f11eb2 | 897 | SSH_AGENTPID_ENV_NAME, pidstr); |
5260325f | 898 | exit(1); |
899 | } | |
900 | if (kill(pid, SIGTERM) == -1) { | |
901 | perror("kill"); | |
902 | exit(1); | |
903 | } | |
904 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; | |
905 | printf(format, SSH_AUTHSOCKET_ENV_NAME); | |
906 | printf(format, SSH_AGENTPID_ENV_NAME); | |
907 | printf("echo Agent pid %d killed;\n", pid); | |
908 | exit(0); | |
dae3fa13 | 909 | } |
5260325f | 910 | parent_pid = getpid(); |
911 | ||
912 | /* Create private directory for agent socket */ | |
913 | strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir); | |
914 | if (mkdtemp(socket_dir) == NULL) { | |
915 | perror("mkdtemp: private socket dir"); | |
916 | exit(1); | |
917 | } | |
918 | snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir, | |
42f11eb2 | 919 | parent_pid); |
5260325f | 920 | |
aa3378df | 921 | /* |
922 | * Create socket early so it will exist before command gets run from | |
923 | * the parent. | |
924 | */ | |
5260325f | 925 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
926 | if (sock < 0) { | |
927 | perror("socket"); | |
928 | cleanup_exit(1); | |
929 | } | |
930 | memset(&sunaddr, 0, sizeof(sunaddr)); | |
931 | sunaddr.sun_family = AF_UNIX; | |
932 | strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); | |
c7ccfd39 | 933 | #ifdef HAVE_CYGWIN |
934 | prev_mask = umask(0177); | |
935 | #endif | |
5260325f | 936 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { |
937 | perror("bind"); | |
c7ccfd39 | 938 | #ifdef HAVE_CYGWIN |
939 | umask(prev_mask); | |
940 | #endif | |
5260325f | 941 | cleanup_exit(1); |
942 | } | |
c7ccfd39 | 943 | #ifdef HAVE_CYGWIN |
944 | umask(prev_mask); | |
945 | #endif | |
5260325f | 946 | if (listen(sock, 5) < 0) { |
947 | perror("listen"); | |
948 | cleanup_exit(1); | |
dae3fa13 | 949 | } |
42f11eb2 | 950 | |
aa3378df | 951 | /* |
952 | * Fork, and have the parent execute the command, if any, or present | |
953 | * the socket data. The child continues as the authentication agent. | |
954 | */ | |
ffdb5d70 | 955 | if (d_flag) { |
956 | log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1); | |
957 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; | |
958 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, | |
959 | SSH_AUTHSOCKET_ENV_NAME); | |
960 | printf("echo Agent pid %d;\n", parent_pid); | |
961 | goto skip; | |
962 | } | |
5260325f | 963 | pid = fork(); |
964 | if (pid == -1) { | |
965 | perror("fork"); | |
fccfbe3b | 966 | cleanup_exit(1); |
dae3fa13 | 967 | } |
5260325f | 968 | if (pid != 0) { /* Parent - execute the given command. */ |
969 | close(sock); | |
970 | snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid); | |
971 | if (ac == 0) { | |
972 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; | |
973 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, | |
42f11eb2 | 974 | SSH_AUTHSOCKET_ENV_NAME); |
5260325f | 975 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, |
42f11eb2 | 976 | SSH_AGENTPID_ENV_NAME); |
5260325f | 977 | printf("echo Agent pid %d;\n", pid); |
978 | exit(0); | |
979 | } | |
bcbf86ec | 980 | if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || |
981 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { | |
982 | perror("setenv"); | |
983 | exit(1); | |
984 | } | |
5260325f | 985 | execvp(av[0], av); |
986 | perror(av[0]); | |
987 | exit(1); | |
988 | } | |
fccfbe3b | 989 | /* child */ |
990 | log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0); | |
ec9f3450 | 991 | |
992 | if (setsid() == -1) { | |
fccfbe3b | 993 | error("setsid: %s", strerror(errno)); |
ec9f3450 | 994 | cleanup_exit(1); |
995 | } | |
996 | ||
997 | (void)chdir("/"); | |
5260325f | 998 | close(0); |
999 | close(1); | |
1000 | close(2); | |
dae3fa13 | 1001 | |
b03bd394 | 1002 | #ifdef HAVE_SETRLIMIT |
0b6fbf03 | 1003 | /* deny core dumps, since memory contains unencrypted private keys */ |
1004 | rlim.rlim_cur = rlim.rlim_max = 0; | |
1005 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) { | |
fccfbe3b | 1006 | error("setrlimit RLIMIT_CORE: %s", strerror(errno)); |
0b6fbf03 | 1007 | cleanup_exit(1); |
1008 | } | |
b03bd394 | 1009 | #endif |
ffdb5d70 | 1010 | |
1011 | skip: | |
fccfbe3b | 1012 | fatal_add_cleanup(cleanup_socket, NULL); |
5260325f | 1013 | new_socket(AUTH_SOCKET, sock); |
1014 | if (ac > 0) { | |
1015 | signal(SIGALRM, check_parent_exists); | |
1016 | alarm(10); | |
1017 | } | |
2e73a022 | 1018 | idtab_init(); |
1ee482c5 | 1019 | if (!d_flag) |
ffdb5d70 | 1020 | signal(SIGINT, SIG_IGN); |
1ee482c5 | 1021 | signal(SIGPIPE, SIG_IGN); |
2f4b2e38 | 1022 | signal(SIGHUP, cleanup_handler); |
1023 | signal(SIGTERM, cleanup_handler); | |
e364646f | 1024 | nalloc = 0; |
1025 | ||
5260325f | 1026 | while (1) { |
e364646f | 1027 | prepare_select(&readsetp, &writesetp, &max_fd, &nalloc); |
42f11eb2 | 1028 | if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) { |
5260325f | 1029 | if (errno == EINTR) |
1030 | continue; | |
fccfbe3b | 1031 | fatal("select: %s", strerror(errno)); |
5260325f | 1032 | } |
42f11eb2 | 1033 | after_select(readsetp, writesetp); |
8efc0c15 | 1034 | } |
5260325f | 1035 | /* NOTREACHED */ |
8efc0c15 | 1036 | } |