]>
Commit | Line | Data |
---|---|---|
e6207598 | 1 | /* $OpenBSD: ssh-agent.c,v 1.79 2002/01/18 18:14:17 stevesk Exp $ */ |
dae3fa13 | 2 | |
8efc0c15 | 3 | /* |
5260325f | 4 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
5 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
6 | * All rights reserved | |
5260325f | 7 | * The authentication agent program. |
2e73a022 | 8 | * |
bcbf86ec | 9 | * As far as I am concerned, the code I have written for this software |
10 | * can be used freely for any purpose. Any derived versions of this | |
11 | * software must be clearly marked as such, and if the derived work is | |
12 | * incompatible with the protocol description in the RFC file, it must be | |
13 | * called by a name other than "ssh" or "Secure Shell". | |
14 | * | |
a96070d4 | 15 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
bcbf86ec | 16 | * |
17 | * Redistribution and use in source and binary forms, with or without | |
18 | * modification, are permitted provided that the following conditions | |
19 | * are met: | |
20 | * 1. Redistributions of source code must retain the above copyright | |
21 | * notice, this list of conditions and the following disclaimer. | |
22 | * 2. Redistributions in binary form must reproduce the above copyright | |
23 | * notice, this list of conditions and the following disclaimer in the | |
24 | * documentation and/or other materials provided with the distribution. | |
25 | * | |
26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
27 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
28 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
29 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
30 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
31 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
35 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
5260325f | 36 | */ |
8efc0c15 | 37 | |
38 | #include "includes.h" | |
95f0a918 | 39 | #include <sys/queue.h> |
e6207598 | 40 | RCSID("$OpenBSD: ssh-agent.c,v 1.79 2002/01/18 18:14:17 stevesk Exp $"); |
42f11eb2 | 41 | |
42 | #include <openssl/evp.h> | |
43 | #include <openssl/md5.h> | |
8efc0c15 | 44 | |
45 | #include "ssh.h" | |
46 | #include "rsa.h" | |
8efc0c15 | 47 | #include "buffer.h" |
48 | #include "bufaux.h" | |
49 | #include "xmalloc.h" | |
50 | #include "packet.h" | |
51 | #include "getput.h" | |
52 | #include "mpaux.h" | |
4c8722d9 | 53 | #include "key.h" |
54 | #include "authfd.h" | |
42f11eb2 | 55 | #include "cipher.h" |
2e73a022 | 56 | #include "kex.h" |
188adeb2 | 57 | #include "compat.h" |
42f11eb2 | 58 | #include "log.h" |
8efc0c15 | 59 | |
2b5fe3b8 | 60 | #ifdef SMARTCARD |
61 | #include <openssl/engine.h> | |
62 | #include "scard.h" | |
dd2495cb | 63 | #endif |
2b5fe3b8 | 64 | |
17a3011c | 65 | typedef enum { |
66 | AUTH_UNUSED, | |
67 | AUTH_SOCKET, | |
68 | AUTH_CONNECTION | |
69 | } sock_type; | |
70 | ||
5260325f | 71 | typedef struct { |
72 | int fd; | |
17a3011c | 73 | sock_type type; |
5260325f | 74 | Buffer input; |
75 | Buffer output; | |
8efc0c15 | 76 | } SocketEntry; |
77 | ||
1e3b8b07 | 78 | u_int sockets_alloc = 0; |
8efc0c15 | 79 | SocketEntry *sockets = NULL; |
80 | ||
95f0a918 | 81 | typedef struct identity { |
82 | TAILQ_ENTRY(identity) next; | |
2e73a022 | 83 | Key *key; |
5260325f | 84 | char *comment; |
8efc0c15 | 85 | } Identity; |
86 | ||
2e73a022 | 87 | typedef struct { |
88 | int nentries; | |
95f0a918 | 89 | TAILQ_HEAD(idqueue, identity) idlist; |
2e73a022 | 90 | } Idtab; |
91 | ||
92 | /* private key table, one per protocol version */ | |
93 | Idtab idtable[3]; | |
8efc0c15 | 94 | |
95 | int max_fd = 0; | |
96 | ||
97 | /* pid of shell == parent of agent */ | |
9da5c3c9 | 98 | pid_t parent_pid = -1; |
8efc0c15 | 99 | |
100 | /* pathname and directory for AUTH_SOCKET */ | |
101 | char socket_name[1024]; | |
102 | char socket_dir[1024]; | |
103 | ||
5260325f | 104 | #ifdef HAVE___PROGNAME |
105 | extern char *__progname; | |
260d427b | 106 | #else |
107 | char *__progname; | |
108 | #endif | |
5260325f | 109 | |
396c147e | 110 | static void |
2e73a022 | 111 | idtab_init(void) |
112 | { | |
113 | int i; | |
6aacefa7 | 114 | for (i = 0; i <=2; i++) { |
95f0a918 | 115 | TAILQ_INIT(&idtable[i].idlist); |
2e73a022 | 116 | idtable[i].nentries = 0; |
117 | } | |
118 | } | |
119 | ||
120 | /* return private key table for requested protocol version */ | |
396c147e | 121 | static Idtab * |
2e73a022 | 122 | idtab_lookup(int version) |
123 | { | |
124 | if (version < 1 || version > 2) | |
125 | fatal("internal error, bad protocol version %d", version); | |
126 | return &idtable[version]; | |
127 | } | |
128 | ||
129 | /* return matching private key for given public key */ | |
95f0a918 | 130 | static Identity * |
131 | lookup_identity(Key *key, int version) | |
2e73a022 | 132 | { |
95f0a918 | 133 | Identity *id; |
134 | ||
2e73a022 | 135 | Idtab *tab = idtab_lookup(version); |
95f0a918 | 136 | TAILQ_FOREACH(id, &tab->idlist, next) { |
137 | if (key_equal(key, id->key)) | |
138 | return (id); | |
2e73a022 | 139 | } |
95f0a918 | 140 | return (NULL); |
141 | } | |
142 | ||
143 | static void | |
144 | free_identity(Identity *id) | |
145 | { | |
146 | key_free(id->key); | |
147 | xfree(id->comment); | |
148 | xfree(id); | |
2e73a022 | 149 | } |
150 | ||
151 | /* send list of supported public keys to 'client' */ | |
396c147e | 152 | static void |
2e73a022 | 153 | process_request_identities(SocketEntry *e, int version) |
8efc0c15 | 154 | { |
2e73a022 | 155 | Idtab *tab = idtab_lookup(version); |
5260325f | 156 | Buffer msg; |
95f0a918 | 157 | Identity *id; |
5260325f | 158 | |
159 | buffer_init(&msg); | |
2e73a022 | 160 | buffer_put_char(&msg, (version == 1) ? |
161 | SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); | |
162 | buffer_put_int(&msg, tab->nentries); | |
95f0a918 | 163 | TAILQ_FOREACH(id, &tab->idlist, next) { |
fa08c86b | 164 | if (id->key->type == KEY_RSA1) { |
2e73a022 | 165 | buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); |
166 | buffer_put_bignum(&msg, id->key->rsa->e); | |
167 | buffer_put_bignum(&msg, id->key->rsa->n); | |
168 | } else { | |
1e3b8b07 | 169 | u_char *blob; |
170 | u_int blen; | |
fa08c86b | 171 | key_to_blob(id->key, &blob, &blen); |
2e73a022 | 172 | buffer_put_string(&msg, blob, blen); |
173 | xfree(blob); | |
174 | } | |
175 | buffer_put_cstring(&msg, id->comment); | |
5260325f | 176 | } |
177 | buffer_put_int(&e->output, buffer_len(&msg)); | |
178 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); | |
179 | buffer_free(&msg); | |
8efc0c15 | 180 | } |
181 | ||
2e73a022 | 182 | /* ssh1 only */ |
396c147e | 183 | static void |
2e73a022 | 184 | process_authentication_challenge1(SocketEntry *e) |
8efc0c15 | 185 | { |
95f0a918 | 186 | Identity *id; |
187 | Key *key; | |
2e73a022 | 188 | BIGNUM *challenge; |
189 | int i, len; | |
5260325f | 190 | Buffer msg; |
191 | MD5_CTX md; | |
1e3b8b07 | 192 | u_char buf[32], mdbuf[16], session_id[16]; |
193 | u_int response_type; | |
5260325f | 194 | |
195 | buffer_init(&msg); | |
fa08c86b | 196 | key = key_new(KEY_RSA1); |
b775c6f2 | 197 | if ((challenge = BN_new()) == NULL) |
198 | fatal("process_authentication_challenge1: BN_new failed"); | |
5260325f | 199 | |
2e73a022 | 200 | buffer_get_int(&e->input); /* ignored */ |
201 | buffer_get_bignum(&e->input, key->rsa->e); | |
202 | buffer_get_bignum(&e->input, key->rsa->n); | |
203 | buffer_get_bignum(&e->input, challenge); | |
5260325f | 204 | |
2e73a022 | 205 | /* Only protocol 1.1 is supported */ |
206 | if (buffer_len(&e->input) == 0) | |
207 | goto failure; | |
e6207598 | 208 | buffer_get(&e->input, session_id, 16); |
2e73a022 | 209 | response_type = buffer_get_int(&e->input); |
210 | if (response_type != 1) | |
211 | goto failure; | |
212 | ||
95f0a918 | 213 | id = lookup_identity(key, 1); |
214 | if (id != NULL) { | |
215 | Key *private = id->key; | |
2e73a022 | 216 | /* Decrypt the challenge using the private key. */ |
46aa2d1f | 217 | if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) |
218 | goto failure; | |
2e73a022 | 219 | |
220 | /* The response is MD5 of decrypted challenge plus session id. */ | |
221 | len = BN_num_bytes(challenge); | |
222 | if (len <= 0 || len > 32) { | |
223 | log("process_authentication_challenge: bad challenge length %d", len); | |
224 | goto failure; | |
5260325f | 225 | } |
2e73a022 | 226 | memset(buf, 0, 32); |
227 | BN_bn2bin(challenge, buf + 32 - len); | |
228 | MD5_Init(&md); | |
229 | MD5_Update(&md, buf, 32); | |
230 | MD5_Update(&md, session_id, 16); | |
231 | MD5_Final(mdbuf, &md); | |
232 | ||
233 | /* Send the response. */ | |
234 | buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); | |
235 | for (i = 0; i < 16; i++) | |
236 | buffer_put_char(&msg, mdbuf[i]); | |
237 | goto send; | |
238 | } | |
239 | ||
240 | failure: | |
241 | /* Unknown identity or protocol error. Send failure. */ | |
5260325f | 242 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
243 | send: | |
2e73a022 | 244 | buffer_put_int(&e->output, buffer_len(&msg)); |
245 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); | |
246 | key_free(key); | |
247 | BN_clear_free(challenge); | |
248 | buffer_free(&msg); | |
249 | } | |
250 | ||
251 | /* ssh2 only */ | |
396c147e | 252 | static void |
2e73a022 | 253 | process_sign_request2(SocketEntry *e) |
254 | { | |
255 | extern int datafellows; | |
95f0a918 | 256 | Key *key; |
1e3b8b07 | 257 | u_char *blob, *data, *signature = NULL; |
258 | u_int blen, dlen, slen = 0; | |
188adeb2 | 259 | int flags; |
2e73a022 | 260 | Buffer msg; |
261 | int ok = -1; | |
262 | ||
263 | datafellows = 0; | |
227e8e86 | 264 | |
2e73a022 | 265 | blob = buffer_get_string(&e->input, &blen); |
266 | data = buffer_get_string(&e->input, &dlen); | |
188adeb2 | 267 | |
268 | flags = buffer_get_int(&e->input); | |
269 | if (flags & SSH_AGENT_OLD_SIGNATURE) | |
270 | datafellows = SSH_BUG_SIGBLOB; | |
2e73a022 | 271 | |
fa08c86b | 272 | key = key_from_blob(blob, blen); |
2e73a022 | 273 | if (key != NULL) { |
95f0a918 | 274 | Identity *id = lookup_identity(key, 2); |
275 | if (id != NULL) | |
276 | ok = key_sign(id->key, &signature, &slen, data, dlen); | |
2e73a022 | 277 | } |
278 | key_free(key); | |
279 | buffer_init(&msg); | |
280 | if (ok == 0) { | |
281 | buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); | |
282 | buffer_put_string(&msg, signature, slen); | |
283 | } else { | |
284 | buffer_put_char(&msg, SSH_AGENT_FAILURE); | |
285 | } | |
5260325f | 286 | buffer_put_int(&e->output, buffer_len(&msg)); |
287 | buffer_append(&e->output, buffer_ptr(&msg), | |
2e73a022 | 288 | buffer_len(&msg)); |
5260325f | 289 | buffer_free(&msg); |
2e73a022 | 290 | xfree(data); |
291 | xfree(blob); | |
292 | if (signature != NULL) | |
293 | xfree(signature); | |
8efc0c15 | 294 | } |
295 | ||
2e73a022 | 296 | /* shared */ |
396c147e | 297 | static void |
2e73a022 | 298 | process_remove_identity(SocketEntry *e, int version) |
8efc0c15 | 299 | { |
95f0a918 | 300 | Key *key = NULL; |
1e3b8b07 | 301 | u_char *blob; |
302 | u_int blen; | |
303 | u_int bits; | |
2e73a022 | 304 | int success = 0; |
305 | ||
6aacefa7 | 306 | switch (version) { |
2e73a022 | 307 | case 1: |
fa08c86b | 308 | key = key_new(KEY_RSA1); |
2e73a022 | 309 | bits = buffer_get_int(&e->input); |
310 | buffer_get_bignum(&e->input, key->rsa->e); | |
311 | buffer_get_bignum(&e->input, key->rsa->n); | |
312 | ||
313 | if (bits != key_size(key)) | |
314 | log("Warning: identity keysize mismatch: actual %d, announced %d", | |
42f11eb2 | 315 | key_size(key), bits); |
2e73a022 | 316 | break; |
317 | case 2: | |
318 | blob = buffer_get_string(&e->input, &blen); | |
fa08c86b | 319 | key = key_from_blob(blob, blen); |
2e73a022 | 320 | xfree(blob); |
321 | break; | |
322 | } | |
323 | if (key != NULL) { | |
95f0a918 | 324 | Identity *id = lookup_identity(key, version); |
325 | if (id != NULL) { | |
aa3378df | 326 | /* |
327 | * We have this key. Free the old key. Since we | |
328 | * don\'t want to leave empty slots in the middle of | |
d343d900 | 329 | * the array, we actually free the key there and move |
330 | * all the entries between the empty slot and the end | |
331 | * of the array. | |
aa3378df | 332 | */ |
2e73a022 | 333 | Idtab *tab = idtab_lookup(version); |
fa08c86b | 334 | if (tab->nentries < 1) |
335 | fatal("process_remove_identity: " | |
336 | "internal error: tab->nentries %d", | |
337 | tab->nentries); | |
95f0a918 | 338 | TAILQ_REMOVE(&tab->idlist, id, next); |
339 | free_identity(id); | |
2e73a022 | 340 | tab->nentries--; |
341 | success = 1; | |
5260325f | 342 | } |
2e73a022 | 343 | key_free(key); |
344 | } | |
8efc0c15 | 345 | buffer_put_int(&e->output, 1); |
2e73a022 | 346 | buffer_put_char(&e->output, |
347 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
8efc0c15 | 348 | } |
349 | ||
396c147e | 350 | static void |
2e73a022 | 351 | process_remove_all_identities(SocketEntry *e, int version) |
8efc0c15 | 352 | { |
2e73a022 | 353 | Idtab *tab = idtab_lookup(version); |
95f0a918 | 354 | Identity *id; |
8efc0c15 | 355 | |
5260325f | 356 | /* Loop over all identities and clear the keys. */ |
95f0a918 | 357 | for (id = TAILQ_FIRST(&tab->idlist); id; |
358 | id = TAILQ_FIRST(&tab->idlist)) { | |
359 | TAILQ_REMOVE(&tab->idlist, id, next); | |
360 | free_identity(id); | |
5260325f | 361 | } |
8efc0c15 | 362 | |
5260325f | 363 | /* Mark that there are no identities. */ |
2e73a022 | 364 | tab->nentries = 0; |
8efc0c15 | 365 | |
366 | /* Send success. */ | |
367 | buffer_put_int(&e->output, 1); | |
368 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); | |
369 | return; | |
5260325f | 370 | } |
371 | ||
396c147e | 372 | static void |
fa08c86b | 373 | process_add_identity(SocketEntry *e, int version) |
374 | { | |
375 | Key *k = NULL; | |
376 | char *type_name; | |
2e73a022 | 377 | char *comment; |
fa08c86b | 378 | int type, success = 0; |
2e73a022 | 379 | Idtab *tab = idtab_lookup(version); |
5260325f | 380 | |
2e73a022 | 381 | switch (version) { |
382 | case 1: | |
fa08c86b | 383 | k = key_new_private(KEY_RSA1); |
42f11eb2 | 384 | buffer_get_int(&e->input); /* ignored */ |
fa08c86b | 385 | buffer_get_bignum(&e->input, k->rsa->n); |
386 | buffer_get_bignum(&e->input, k->rsa->e); | |
387 | buffer_get_bignum(&e->input, k->rsa->d); | |
388 | buffer_get_bignum(&e->input, k->rsa->iqmp); | |
2e73a022 | 389 | |
390 | /* SSH and SSL have p and q swapped */ | |
fa08c86b | 391 | buffer_get_bignum(&e->input, k->rsa->q); /* p */ |
392 | buffer_get_bignum(&e->input, k->rsa->p); /* q */ | |
2e73a022 | 393 | |
394 | /* Generate additional parameters */ | |
2b63e803 | 395 | rsa_generate_additional_parameters(k->rsa); |
2e73a022 | 396 | break; |
397 | case 2: | |
fa08c86b | 398 | type_name = buffer_get_string(&e->input, NULL); |
42f11eb2 | 399 | type = key_type_from_name(type_name); |
fa08c86b | 400 | xfree(type_name); |
6aacefa7 | 401 | switch (type) { |
fa08c86b | 402 | case KEY_DSA: |
403 | k = key_new_private(type); | |
404 | buffer_get_bignum2(&e->input, k->dsa->p); | |
405 | buffer_get_bignum2(&e->input, k->dsa->q); | |
406 | buffer_get_bignum2(&e->input, k->dsa->g); | |
407 | buffer_get_bignum2(&e->input, k->dsa->pub_key); | |
408 | buffer_get_bignum2(&e->input, k->dsa->priv_key); | |
409 | break; | |
410 | case KEY_RSA: | |
411 | k = key_new_private(type); | |
412 | buffer_get_bignum2(&e->input, k->rsa->n); | |
413 | buffer_get_bignum2(&e->input, k->rsa->e); | |
414 | buffer_get_bignum2(&e->input, k->rsa->d); | |
415 | buffer_get_bignum2(&e->input, k->rsa->iqmp); | |
416 | buffer_get_bignum2(&e->input, k->rsa->p); | |
417 | buffer_get_bignum2(&e->input, k->rsa->q); | |
418 | ||
419 | /* Generate additional parameters */ | |
2b63e803 | 420 | rsa_generate_additional_parameters(k->rsa); |
fa08c86b | 421 | break; |
422 | default: | |
2e73a022 | 423 | buffer_clear(&e->input); |
2e73a022 | 424 | goto send; |
5260325f | 425 | } |
2e73a022 | 426 | break; |
427 | } | |
2e73a022 | 428 | comment = buffer_get_string(&e->input, NULL); |
429 | if (k == NULL) { | |
430 | xfree(comment); | |
431 | goto send; | |
432 | } | |
433 | success = 1; | |
95f0a918 | 434 | if (lookup_identity(k, version) == NULL) { |
435 | Identity *id = xmalloc(sizeof(Identity)); | |
436 | id->key = k; | |
437 | id->comment = comment; | |
438 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); | |
2e73a022 | 439 | /* Increment the number of identities. */ |
440 | tab->nentries++; | |
441 | } else { | |
442 | key_free(k); | |
443 | xfree(comment); | |
444 | } | |
445 | send: | |
5260325f | 446 | buffer_put_int(&e->output, 1); |
2e73a022 | 447 | buffer_put_char(&e->output, |
448 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
8efc0c15 | 449 | } |
450 | ||
2b5fe3b8 | 451 | |
452 | #ifdef SMARTCARD | |
453 | static void | |
454 | process_add_smartcard_key (SocketEntry *e) | |
455 | { | |
456 | Idtab *tab; | |
457 | Key *n = NULL, *k = NULL; | |
9ff6f66f | 458 | char *sc_reader_id = NULL; |
2b5fe3b8 | 459 | int success = 0; |
184eed6a | 460 | |
9ff6f66f | 461 | sc_reader_id = buffer_get_string(&e->input, NULL); |
462 | k = sc_get_key(sc_reader_id); | |
463 | xfree(sc_reader_id); | |
2b5fe3b8 | 464 | |
2b5fe3b8 | 465 | if (k == NULL) { |
466 | error("sc_get_pubkey failed"); | |
467 | goto send; | |
468 | } | |
469 | success = 1; | |
470 | ||
471 | tab = idtab_lookup(1); | |
a0e0f486 | 472 | k->type = KEY_RSA1; |
95f0a918 | 473 | if (lookup_identity(k, 1) == NULL) { |
474 | Identity *id = xmalloc(sizeof(Identity)); | |
2b5fe3b8 | 475 | n = key_new(KEY_RSA1); |
476 | BN_copy(n->rsa->n, k->rsa->n); | |
477 | BN_copy(n->rsa->e, k->rsa->e); | |
478 | RSA_set_method(n->rsa, sc_get_engine()); | |
95f0a918 | 479 | id->key = n; |
480 | id->comment = xstrdup("rsa1 smartcard"); | |
481 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); | |
2b5fe3b8 | 482 | tab->nentries++; |
483 | } | |
a0e0f486 | 484 | k->type = KEY_RSA; |
2b5fe3b8 | 485 | tab = idtab_lookup(2); |
95f0a918 | 486 | if (lookup_identity(k, 2) == NULL) { |
487 | Identity *id = xmalloc(sizeof(Identity)); | |
2b5fe3b8 | 488 | n = key_new(KEY_RSA); |
489 | BN_copy(n->rsa->n, k->rsa->n); | |
490 | BN_copy(n->rsa->e, k->rsa->e); | |
491 | RSA_set_method(n->rsa, sc_get_engine()); | |
95f0a918 | 492 | id->key = n; |
493 | id->comment = xstrdup("rsa smartcard"); | |
494 | TAILQ_INSERT_TAIL(&tab->idlist, id, next); | |
2b5fe3b8 | 495 | tab->nentries++; |
496 | } | |
497 | key_free(k); | |
498 | send: | |
499 | buffer_put_int(&e->output, 1); | |
500 | buffer_put_char(&e->output, | |
501 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
502 | } | |
503 | ||
504 | static void | |
505 | process_remove_smartcard_key(SocketEntry *e) | |
506 | { | |
95f0a918 | 507 | Key *k = NULL; |
2b5fe3b8 | 508 | int success = 0; |
9ff6f66f | 509 | char *sc_reader_id = NULL; |
2b5fe3b8 | 510 | |
9ff6f66f | 511 | sc_reader_id = buffer_get_string(&e->input, NULL); |
512 | k = sc_get_key(sc_reader_id); | |
513 | xfree(sc_reader_id); | |
2b5fe3b8 | 514 | |
9ff6f66f | 515 | if (k == NULL) { |
2b5fe3b8 | 516 | error("sc_get_pubkey failed"); |
517 | } else { | |
95f0a918 | 518 | Identity *id; |
77261db4 | 519 | k->type = KEY_RSA1; |
95f0a918 | 520 | id = lookup_identity(k, 1); |
521 | if (id != NULL) { | |
2b5fe3b8 | 522 | Idtab *tab = idtab_lookup(1); |
95f0a918 | 523 | TAILQ_REMOVE(&tab->idlist, id, next); |
524 | free_identity(id); | |
2b5fe3b8 | 525 | tab->nentries--; |
526 | success = 1; | |
527 | } | |
77261db4 | 528 | k->type = KEY_RSA; |
95f0a918 | 529 | id = lookup_identity(k, 2); |
530 | if (id != NULL) { | |
2b5fe3b8 | 531 | Idtab *tab = idtab_lookup(2); |
95f0a918 | 532 | TAILQ_REMOVE(&tab->idlist, id, next); |
533 | free_identity(id); | |
2b5fe3b8 | 534 | tab->nentries--; |
535 | success = 1; | |
536 | } | |
537 | key_free(k); | |
538 | } | |
539 | ||
540 | buffer_put_int(&e->output, 1); | |
541 | buffer_put_char(&e->output, | |
542 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); | |
543 | } | |
3e984472 | 544 | #endif /* SMARTCARD */ |
2b5fe3b8 | 545 | |
2e73a022 | 546 | /* dispatch incoming messages */ |
547 | ||
396c147e | 548 | static void |
8efc0c15 | 549 | process_message(SocketEntry *e) |
550 | { | |
1e3b8b07 | 551 | u_int msg_len; |
552 | u_int type; | |
553 | u_char *cp; | |
5260325f | 554 | if (buffer_len(&e->input) < 5) |
555 | return; /* Incomplete message. */ | |
20905a8e | 556 | cp = buffer_ptr(&e->input); |
5260325f | 557 | msg_len = GET_32BIT(cp); |
558 | if (msg_len > 256 * 1024) { | |
559 | shutdown(e->fd, SHUT_RDWR); | |
560 | close(e->fd); | |
561 | e->type = AUTH_UNUSED; | |
562 | return; | |
563 | } | |
564 | if (buffer_len(&e->input) < msg_len + 4) | |
565 | return; | |
566 | buffer_consume(&e->input, 4); | |
567 | type = buffer_get_char(&e->input); | |
568 | ||
2b5fe3b8 | 569 | debug("type %d", type); |
5260325f | 570 | switch (type) { |
2e73a022 | 571 | /* ssh1 */ |
5260325f | 572 | case SSH_AGENTC_RSA_CHALLENGE: |
2e73a022 | 573 | process_authentication_challenge1(e); |
574 | break; | |
575 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: | |
576 | process_request_identities(e, 1); | |
5260325f | 577 | break; |
578 | case SSH_AGENTC_ADD_RSA_IDENTITY: | |
2e73a022 | 579 | process_add_identity(e, 1); |
5260325f | 580 | break; |
581 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: | |
2e73a022 | 582 | process_remove_identity(e, 1); |
5260325f | 583 | break; |
584 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: | |
2e73a022 | 585 | process_remove_all_identities(e, 1); |
586 | break; | |
587 | /* ssh2 */ | |
588 | case SSH2_AGENTC_SIGN_REQUEST: | |
589 | process_sign_request2(e); | |
590 | break; | |
591 | case SSH2_AGENTC_REQUEST_IDENTITIES: | |
592 | process_request_identities(e, 2); | |
593 | break; | |
594 | case SSH2_AGENTC_ADD_IDENTITY: | |
595 | process_add_identity(e, 2); | |
596 | break; | |
597 | case SSH2_AGENTC_REMOVE_IDENTITY: | |
598 | process_remove_identity(e, 2); | |
599 | break; | |
600 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: | |
601 | process_remove_all_identities(e, 2); | |
5260325f | 602 | break; |
2b5fe3b8 | 603 | #ifdef SMARTCARD |
604 | case SSH_AGENTC_ADD_SMARTCARD_KEY: | |
605 | process_add_smartcard_key(e); | |
184eed6a | 606 | break; |
2b5fe3b8 | 607 | case SSH_AGENTC_REMOVE_SMARTCARD_KEY: |
608 | process_remove_smartcard_key(e); | |
184eed6a | 609 | break; |
3e984472 | 610 | #endif /* SMARTCARD */ |
5260325f | 611 | default: |
612 | /* Unknown message. Respond with failure. */ | |
613 | error("Unknown message %d", type); | |
614 | buffer_clear(&e->input); | |
615 | buffer_put_int(&e->output, 1); | |
616 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); | |
617 | break; | |
618 | } | |
8efc0c15 | 619 | } |
620 | ||
396c147e | 621 | static void |
17a3011c | 622 | new_socket(sock_type type, int fd) |
8efc0c15 | 623 | { |
1e3b8b07 | 624 | u_int i, old_alloc; |
5260325f | 625 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) |
626 | error("fcntl O_NONBLOCK: %s", strerror(errno)); | |
627 | ||
628 | if (fd > max_fd) | |
629 | max_fd = fd; | |
630 | ||
631 | for (i = 0; i < sockets_alloc; i++) | |
632 | if (sockets[i].type == AUTH_UNUSED) { | |
633 | sockets[i].fd = fd; | |
634 | sockets[i].type = type; | |
635 | buffer_init(&sockets[i].input); | |
636 | buffer_init(&sockets[i].output); | |
637 | return; | |
638 | } | |
639 | old_alloc = sockets_alloc; | |
640 | sockets_alloc += 10; | |
641 | if (sockets) | |
642 | sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); | |
643 | else | |
644 | sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); | |
645 | for (i = old_alloc; i < sockets_alloc; i++) | |
646 | sockets[i].type = AUTH_UNUSED; | |
647 | sockets[old_alloc].type = type; | |
648 | sockets[old_alloc].fd = fd; | |
649 | buffer_init(&sockets[old_alloc].input); | |
650 | buffer_init(&sockets[old_alloc].output); | |
8efc0c15 | 651 | } |
652 | ||
396c147e | 653 | static int |
e364646f | 654 | prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp) |
8efc0c15 | 655 | { |
42f11eb2 | 656 | u_int i, sz; |
657 | int n = 0; | |
658 | ||
659 | for (i = 0; i < sockets_alloc; i++) { | |
5260325f | 660 | switch (sockets[i].type) { |
661 | case AUTH_SOCKET: | |
662 | case AUTH_CONNECTION: | |
42f11eb2 | 663 | n = MAX(n, sockets[i].fd); |
5260325f | 664 | break; |
665 | case AUTH_UNUSED: | |
666 | break; | |
667 | default: | |
668 | fatal("Unknown socket type %d", sockets[i].type); | |
669 | break; | |
670 | } | |
42f11eb2 | 671 | } |
672 | ||
673 | sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); | |
e364646f | 674 | if (*fdrp == NULL || sz > *nallocp) { |
42f11eb2 | 675 | if (*fdrp) |
894c5fa6 | 676 | xfree(*fdrp); |
42f11eb2 | 677 | if (*fdwp) |
894c5fa6 | 678 | xfree(*fdwp); |
42f11eb2 | 679 | *fdrp = xmalloc(sz); |
680 | *fdwp = xmalloc(sz); | |
e364646f | 681 | *nallocp = sz; |
42f11eb2 | 682 | } |
e364646f | 683 | if (n < *fdl) |
684 | debug("XXX shrink: %d < %d", n, *fdl); | |
685 | *fdl = n; | |
42f11eb2 | 686 | memset(*fdrp, 0, sz); |
687 | memset(*fdwp, 0, sz); | |
688 | ||
689 | for (i = 0; i < sockets_alloc; i++) { | |
690 | switch (sockets[i].type) { | |
691 | case AUTH_SOCKET: | |
692 | case AUTH_CONNECTION: | |
693 | FD_SET(sockets[i].fd, *fdrp); | |
694 | if (buffer_len(&sockets[i].output) > 0) | |
695 | FD_SET(sockets[i].fd, *fdwp); | |
696 | break; | |
697 | default: | |
698 | break; | |
699 | } | |
700 | } | |
701 | return (1); | |
8efc0c15 | 702 | } |
703 | ||
396c147e | 704 | static void |
5260325f | 705 | after_select(fd_set *readset, fd_set *writeset) |
8efc0c15 | 706 | { |
1e3b8b07 | 707 | u_int i; |
5260325f | 708 | int len, sock; |
610cd5c6 | 709 | socklen_t slen; |
5260325f | 710 | char buf[1024]; |
711 | struct sockaddr_un sunaddr; | |
712 | ||
713 | for (i = 0; i < sockets_alloc; i++) | |
714 | switch (sockets[i].type) { | |
715 | case AUTH_UNUSED: | |
716 | break; | |
717 | case AUTH_SOCKET: | |
718 | if (FD_ISSET(sockets[i].fd, readset)) { | |
610cd5c6 | 719 | slen = sizeof(sunaddr); |
42f11eb2 | 720 | sock = accept(sockets[i].fd, |
721 | (struct sockaddr *) &sunaddr, &slen); | |
5260325f | 722 | if (sock < 0) { |
723 | perror("accept from AUTH_SOCKET"); | |
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 |
dae3fa13 | 777 | cleanup_socket(void) |
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 | { | |
5260325f | 788 | cleanup_socket(); |
789 | exit(i); | |
dae3fa13 | 790 | } |
791 | ||
396c147e | 792 | static void |
2f4b2e38 | 793 | cleanup_handler(int sig) |
794 | { | |
795 | cleanup_socket(); | |
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"); | |
966 | 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 | } | |
ec9f3450 | 989 | |
990 | if (setsid() == -1) { | |
991 | perror("setsid"); | |
992 | cleanup_exit(1); | |
993 | } | |
994 | ||
995 | (void)chdir("/"); | |
5260325f | 996 | close(0); |
997 | close(1); | |
998 | close(2); | |
dae3fa13 | 999 | |
b03bd394 | 1000 | #ifdef HAVE_SETRLIMIT |
0b6fbf03 | 1001 | /* deny core dumps, since memory contains unencrypted private keys */ |
1002 | rlim.rlim_cur = rlim.rlim_max = 0; | |
1003 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) { | |
1004 | perror("setrlimit rlimit_core failed"); | |
1005 | cleanup_exit(1); | |
1006 | } | |
b03bd394 | 1007 | #endif |
ffdb5d70 | 1008 | |
1009 | skip: | |
5260325f | 1010 | if (atexit(cleanup_socket) < 0) { |
1011 | perror("atexit"); | |
1012 | cleanup_exit(1); | |
1013 | } | |
1014 | new_socket(AUTH_SOCKET, sock); | |
1015 | if (ac > 0) { | |
1016 | signal(SIGALRM, check_parent_exists); | |
1017 | alarm(10); | |
1018 | } | |
2e73a022 | 1019 | idtab_init(); |
1ee482c5 | 1020 | if (!d_flag) |
ffdb5d70 | 1021 | signal(SIGINT, SIG_IGN); |
1ee482c5 | 1022 | signal(SIGPIPE, SIG_IGN); |
2f4b2e38 | 1023 | signal(SIGHUP, cleanup_handler); |
1024 | signal(SIGTERM, cleanup_handler); | |
e364646f | 1025 | nalloc = 0; |
1026 | ||
5260325f | 1027 | while (1) { |
e364646f | 1028 | prepare_select(&readsetp, &writesetp, &max_fd, &nalloc); |
42f11eb2 | 1029 | if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) { |
5260325f | 1030 | if (errno == EINTR) |
1031 | continue; | |
1032 | exit(1); | |
1033 | } | |
42f11eb2 | 1034 | after_select(readsetp, writesetp); |
8efc0c15 | 1035 | } |
5260325f | 1036 | /* NOTREACHED */ |
8efc0c15 | 1037 | } |