1 /* 2 * Copyright (c) 1997-2004 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "kdc_locl.h" 35 36 RCSID("$Id: connect.c,v 1.90.2.2 2004/04/02 20:50:53 lha Exp $"); 37 38 /* 39 * a tuple describing on what to listen 40 */ 41 42 struct port_desc{ 43 int family; 44 int type; 45 int port; 46 }; 47 48 /* the current ones */ 49 50 static struct port_desc *ports; 51 static int num_ports; 52 53 /* 54 * add `family, port, protocol' to the list with duplicate suppresion. 55 */ 56 57 static void 58 add_port(int family, int port, const char *protocol) 59 { 60 int type; 61 int i; 62 63 if(strcmp(protocol, "udp") == 0) 64 type = SOCK_DGRAM; 65 else if(strcmp(protocol, "tcp") == 0) 66 type = SOCK_STREAM; 67 else 68 return; 69 for(i = 0; i < num_ports; i++){ 70 if(ports[i].type == type 71 && ports[i].port == port 72 && ports[i].family == family) 73 return; 74 } 75 ports = realloc(ports, (num_ports + 1) * sizeof(*ports)); 76 if (ports == NULL) 77 krb5_err (context, 1, errno, "realloc"); 78 ports[num_ports].family = family; 79 ports[num_ports].type = type; 80 ports[num_ports].port = port; 81 num_ports++; 82 } 83 84 /* 85 * add a triple but with service -> port lookup 86 * (this prints warnings for stuff that does not exist) 87 */ 88 89 static void 90 add_port_service(int family, const char *service, int port, 91 const char *protocol) 92 { 93 port = krb5_getportbyname (context, service, protocol, port); 94 add_port (family, port, protocol); 95 } 96 97 /* 98 * add the port with service -> port lookup or string -> number 99 * (no warning is printed) 100 */ 101 102 static void 103 add_port_string (int family, const char *port_str, const char *protocol) 104 { 105 struct servent *sp; 106 int port; 107 108 sp = roken_getservbyname (port_str, protocol); 109 if (sp != NULL) { 110 port = sp->s_port; 111 } else { 112 char *end; 113 114 port = htons(strtol(port_str, &end, 0)); 115 if (end == port_str) 116 return; 117 } 118 add_port (family, port, protocol); 119 } 120 121 /* 122 * add the standard collection of ports for `family' 123 */ 124 125 static void 126 add_standard_ports (int family) 127 { 128 add_port_service(family, "kerberos", 88, "udp"); 129 add_port_service(family, "kerberos", 88, "tcp"); 130 add_port_service(family, "kerberos-sec", 88, "udp"); 131 add_port_service(family, "kerberos-sec", 88, "tcp"); 132 if(enable_http) 133 add_port_service(family, "http", 80, "tcp"); 134 if(enable_524) { 135 add_port_service(family, "krb524", 4444, "udp"); 136 add_port_service(family, "krb524", 4444, "tcp"); 137 } 138 #ifdef KRB4 139 if(enable_v4) { 140 add_port_service(family, "kerberos-iv", 750, "udp"); 141 add_port_service(family, "kerberos-iv", 750, "tcp"); 142 } 143 if (enable_kaserver) 144 add_port_service(family, "afs3-kaserver", 7004, "udp"); 145 #endif 146 } 147 148 /* 149 * parse the set of space-delimited ports in `str' and add them. 150 * "+" => all the standard ones 151 * otherwise it's port|service[/protocol] 152 */ 153 154 static void 155 parse_ports(const char *str) 156 { 157 char *pos = NULL; 158 char *p; 159 char *str_copy = strdup (str); 160 161 p = strtok_r(str_copy, " \t", &pos); 162 while(p != NULL) { 163 if(strcmp(p, "+") == 0) { 164 #ifdef HAVE_IPV6 165 add_standard_ports(AF_INET6); 166 #endif 167 add_standard_ports(AF_INET); 168 } else { 169 char *q = strchr(p, '/'); 170 if(q){ 171 *q++ = 0; 172 #ifdef HAVE_IPV6 173 add_port_string(AF_INET6, p, q); 174 #endif 175 add_port_string(AF_INET, p, q); 176 }else { 177 #ifdef HAVE_IPV6 178 add_port_string(AF_INET6, p, "udp"); 179 add_port_string(AF_INET6, p, "tcp"); 180 #endif 181 add_port_string(AF_INET, p, "udp"); 182 add_port_string(AF_INET, p, "tcp"); 183 } 184 } 185 186 p = strtok_r(NULL, " \t", &pos); 187 } 188 free (str_copy); 189 } 190 191 /* 192 * every socket we listen on 193 */ 194 195 struct descr { 196 int s; 197 int type; 198 unsigned char *buf; 199 size_t size; 200 size_t len; 201 time_t timeout; 202 struct sockaddr_storage __ss; 203 struct sockaddr *sa; 204 socklen_t sock_len; 205 char addr_string[128]; 206 }; 207 208 static void 209 init_descr(struct descr *d) 210 { 211 memset(d, 0, sizeof(*d)); 212 d->sa = (struct sockaddr *)&d->__ss; 213 d->s = -1; 214 } 215 216 /* 217 * re-initialize all `n' ->sa in `d'. 218 */ 219 220 static void 221 reinit_descrs (struct descr *d, int n) 222 { 223 int i; 224 225 for (i = 0; i < n; ++i) 226 d[i].sa = (struct sockaddr *)&d[i].__ss; 227 } 228 229 /* 230 * Create the socket (family, type, port) in `d' 231 */ 232 233 static void 234 init_socket(struct descr *d, krb5_address *a, int family, int type, int port) 235 { 236 krb5_error_code ret; 237 struct sockaddr_storage __ss; 238 struct sockaddr *sa = (struct sockaddr *)&__ss; 239 int sa_size = sizeof(__ss); 240 241 init_descr (d); 242 243 ret = krb5_addr2sockaddr (context, a, sa, &sa_size, port); 244 if (ret) { 245 krb5_warn(context, ret, "krb5_addr2sockaddr"); 246 close(d->s); 247 d->s = -1; 248 return; 249 } 250 251 if (sa->sa_family != family) 252 return; 253 254 d->s = socket(family, type, 0); 255 if(d->s < 0){ 256 krb5_warn(context, errno, "socket(%d, %d, 0)", family, type); 257 d->s = -1; 258 return; 259 } 260 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR) 261 { 262 int one = 1; 263 setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one)); 264 } 265 #endif 266 d->type = type; 267 268 if(bind(d->s, sa, sa_size) < 0){ 269 char a_str[256]; 270 size_t len; 271 272 krb5_print_address (a, a_str, sizeof(a_str), &len); 273 krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port)); 274 close(d->s); 275 d->s = -1; 276 return; 277 } 278 if(type == SOCK_STREAM && listen(d->s, SOMAXCONN) < 0){ 279 char a_str[256]; 280 size_t len; 281 282 krb5_print_address (a, a_str, sizeof(a_str), &len); 283 krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port)); 284 close(d->s); 285 d->s = -1; 286 return; 287 } 288 } 289 290 /* 291 * Allocate descriptors for all the sockets that we should listen on 292 * and return the number of them. 293 */ 294 295 static int 296 init_sockets(struct descr **desc) 297 { 298 krb5_error_code ret; 299 int i, j; 300 struct descr *d; 301 int num = 0; 302 krb5_addresses addresses; 303 304 if (explicit_addresses.len) { 305 addresses = explicit_addresses; 306 } else { 307 ret = krb5_get_all_server_addrs (context, &addresses); 308 if (ret) 309 krb5_err (context, 1, ret, "krb5_get_all_server_addrs"); 310 } 311 parse_ports(port_str); 312 d = malloc(addresses.len * num_ports * sizeof(*d)); 313 if (d == NULL) 314 krb5_errx(context, 1, "malloc(%lu) failed", 315 (unsigned long)num_ports * sizeof(*d)); 316 317 for (i = 0; i < num_ports; i++){ 318 for (j = 0; j < addresses.len; ++j) { 319 init_socket(&d[num], &addresses.val[j], 320 ports[i].family, ports[i].type, ports[i].port); 321 if(d[num].s != -1){ 322 char a_str[80]; 323 size_t len; 324 325 krb5_print_address (&addresses.val[j], a_str, 326 sizeof(a_str), &len); 327 328 kdc_log(5, "listening on %s port %u/%s", 329 a_str, 330 ntohs(ports[i].port), 331 (ports[i].type == SOCK_STREAM) ? "tcp" : "udp"); 332 /* XXX */ 333 num++; 334 } 335 } 336 } 337 krb5_free_addresses (context, &addresses); 338 d = realloc(d, num * sizeof(*d)); 339 if (d == NULL && num != 0) 340 krb5_errx(context, 1, "realloc(%lu) failed", 341 (unsigned long)num * sizeof(*d)); 342 reinit_descrs (d, num); 343 *desc = d; 344 return num; 345 } 346 347 /* 348 * handle the request in `buf, len', from `addr' (or `from' as a string), 349 * sending a reply in `reply'. 350 */ 351 352 static int 353 process_request(unsigned char *buf, 354 size_t len, 355 krb5_data *reply, 356 int *sendlength, 357 const char *from, 358 struct sockaddr *addr) 359 { 360 KDC_REQ req; 361 Ticket ticket; 362 krb5_error_code ret; 363 size_t i; 364 365 gettimeofday(&now, NULL); 366 if(decode_AS_REQ(buf, len, &req, &i) == 0){ 367 ret = as_rep(&req, reply, from, addr); 368 free_AS_REQ(&req); 369 return ret; 370 }else if(decode_TGS_REQ(buf, len, &req, &i) == 0){ 371 ret = tgs_rep(&req, reply, from, addr); 372 free_TGS_REQ(&req); 373 return ret; 374 }else if(decode_Ticket(buf, len, &ticket, &i) == 0){ 375 ret = do_524(&ticket, reply, from, addr); 376 free_Ticket(&ticket); 377 return ret; 378 #ifdef KRB4 379 } else if(maybe_version4(buf, len)){ 380 *sendlength = 0; /* elbitapmoc sdrawkcab XXX */ 381 do_version4(buf, len, reply, from, (struct sockaddr_in*)addr); 382 return 0; 383 } else if (enable_kaserver) { 384 ret = do_kaserver (buf, len, reply, from, (struct sockaddr_in*)addr); 385 return ret; 386 #endif 387 } 388 389 return -1; 390 } 391 392 static void 393 addr_to_string(struct sockaddr *addr, size_t addr_len, char *str, size_t len) 394 { 395 krb5_address a; 396 if(krb5_sockaddr2address(context, addr, &a) == 0) { 397 if(krb5_print_address(&a, str, len, &len) == 0) { 398 krb5_free_address(context, &a); 399 return; 400 } 401 krb5_free_address(context, &a); 402 } 403 snprintf(str, len, "<family=%d>", addr->sa_family); 404 } 405 406 /* 407 * Handle the request in `buf, len' to socket `d' 408 */ 409 410 static void 411 do_request(void *buf, size_t len, int sendlength, 412 struct descr *d) 413 { 414 krb5_error_code ret; 415 krb5_data reply; 416 417 reply.length = 0; 418 ret = process_request(buf, len, &reply, &sendlength, 419 d->addr_string, d->sa); 420 if(reply.length){ 421 kdc_log(5, "sending %lu bytes to %s", (unsigned long)reply.length, 422 d->addr_string); 423 if(sendlength){ 424 unsigned char len[4]; 425 len[0] = (reply.length >> 24) & 0xff; 426 len[1] = (reply.length >> 16) & 0xff; 427 len[2] = (reply.length >> 8) & 0xff; 428 len[3] = reply.length & 0xff; 429 if(sendto(d->s, len, sizeof(len), 0, d->sa, d->sock_len) < 0) { 430 kdc_log (0, "sendto(%s): %s", d->addr_string, strerror(errno)); 431 krb5_data_free(&reply); 432 return; 433 } 434 } 435 if(sendto(d->s, reply.data, reply.length, 0, d->sa, d->sock_len) < 0) { 436 kdc_log (0, "sendto(%s): %s", d->addr_string, strerror(errno)); 437 krb5_data_free(&reply); 438 return; 439 } 440 krb5_data_free(&reply); 441 } 442 if(ret) 443 kdc_log(0, "Failed processing %lu byte request from %s", 444 (unsigned long)len, d->addr_string); 445 } 446 447 /* 448 * Handle incoming data to the UDP socket in `d' 449 */ 450 451 static void 452 handle_udp(struct descr *d) 453 { 454 unsigned char *buf; 455 int n; 456 457 buf = malloc(max_request); 458 if(buf == NULL){ 459 kdc_log(0, "Failed to allocate %lu bytes", (unsigned long)max_request); 460 return; 461 } 462 463 d->sock_len = sizeof(d->__ss); 464 n = recvfrom(d->s, buf, max_request, 0, d->sa, &d->sock_len); 465 if(n < 0) 466 krb5_warn(context, errno, "recvfrom"); 467 else { 468 addr_to_string (d->sa, d->sock_len, 469 d->addr_string, sizeof(d->addr_string)); 470 do_request(buf, n, 0, d); 471 } 472 free (buf); 473 } 474 475 static void 476 clear_descr(struct descr *d) 477 { 478 if(d->buf) 479 memset(d->buf, 0, d->size); 480 d->len = 0; 481 if(d->s != -1) 482 close(d->s); 483 d->s = -1; 484 } 485 486 487 /* remove HTTP %-quoting from buf */ 488 static int 489 de_http(char *buf) 490 { 491 char *p, *q; 492 for(p = q = buf; *p; p++, q++) { 493 if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) { 494 unsigned int x; 495 if(sscanf(p + 1, "%2x", &x) != 1) 496 return -1; 497 *q = x; 498 p += 2; 499 } else 500 *q = *p; 501 } 502 *q = '\0'; 503 return 0; 504 } 505 506 #define TCP_TIMEOUT 4 507 508 /* 509 * accept a new TCP connection on `d[parent]' and store it in `d[child]' 510 */ 511 512 static void 513 add_new_tcp (struct descr *d, int parent, int child) 514 { 515 int s; 516 517 if (child == -1) 518 return; 519 520 d[child].sock_len = sizeof(d[child].__ss); 521 s = accept(d[parent].s, d[child].sa, &d[child].sock_len); 522 if(s < 0) { 523 krb5_warn(context, errno, "accept"); 524 return; 525 } 526 527 if (s >= FD_SETSIZE) { 528 krb5_warnx(context, "socket FD too large"); 529 close (s); 530 return; 531 } 532 533 d[child].s = s; 534 d[child].timeout = time(NULL) + TCP_TIMEOUT; 535 d[child].type = SOCK_STREAM; 536 addr_to_string (d[child].sa, d[child].sock_len, 537 d[child].addr_string, sizeof(d[child].addr_string)); 538 } 539 540 /* 541 * Grow `d' to handle at least `n'. 542 * Return != 0 if fails 543 */ 544 545 static int 546 grow_descr (struct descr *d, size_t n) 547 { 548 if (d->size - d->len < n) { 549 unsigned char *tmp; 550 size_t grow; 551 552 grow = max(1024, d->len + n); 553 if (d->size + grow > max_request) { 554 kdc_log(0, "Request exceeds max request size (%lu bytes).", 555 (unsigned long)d->size + grow); 556 clear_descr(d); 557 return -1; 558 } 559 tmp = realloc (d->buf, d->size + grow); 560 if (tmp == NULL) { 561 kdc_log(0, "Failed to re-allocate %lu bytes.", 562 (unsigned long)d->size + grow); 563 clear_descr(d); 564 return -1; 565 } 566 d->size += grow; 567 d->buf = tmp; 568 } 569 return 0; 570 } 571 572 /* 573 * Try to handle the TCP data at `d->buf, d->len'. 574 * Return -1 if failed, 0 if succesful, and 1 if data is complete. 575 */ 576 577 static int 578 handle_vanilla_tcp (struct descr *d) 579 { 580 krb5_storage *sp; 581 int32_t len; 582 583 sp = krb5_storage_from_mem(d->buf, d->len); 584 if (sp == NULL) { 585 kdc_log (0, "krb5_storage_from_mem failed"); 586 return -1; 587 } 588 krb5_ret_int32(sp, &len); 589 krb5_storage_free(sp); 590 if(d->len - 4 >= len) { 591 memmove(d->buf, d->buf + 4, d->len - 4); 592 return 1; 593 } 594 return 0; 595 } 596 597 /* 598 * Try to handle the TCP/HTTP data at `d->buf, d->len'. 599 * Return -1 if failed, 0 if succesful, and 1 if data is complete. 600 */ 601 602 static int 603 handle_http_tcp (struct descr *d) 604 { 605 char *s, *p, *t; 606 void *data; 607 char *proto; 608 int len; 609 610 s = (char *)d->buf; 611 612 p = strstr(s, "\r\n"); 613 if (p == NULL) { 614 kdc_log(0, "Malformed HTTP request from %s", d->addr_string); 615 return -1; 616 } 617 *p = 0; 618 619 p = NULL; 620 t = strtok_r(s, " \t", &p); 621 if (t == NULL) { 622 kdc_log(0, "Malformed HTTP request from %s", d->addr_string); 623 return -1; 624 } 625 t = strtok_r(NULL, " \t", &p); 626 if(t == NULL) { 627 kdc_log(0, "Malformed HTTP request from %s", d->addr_string); 628 return -1; 629 } 630 data = malloc(strlen(t)); 631 if (data == NULL) { 632 kdc_log(0, "Failed to allocate %lu bytes", 633 (unsigned long)strlen(t)); 634 return -1; 635 } 636 if(*t == '/') 637 t++; 638 if(de_http(t) != 0) { 639 kdc_log(0, "Malformed HTTP request from %s", d->addr_string); 640 kdc_log(5, "Request: %s", t); 641 free(data); 642 return -1; 643 } 644 proto = strtok_r(NULL, " \t", &p); 645 if (proto == NULL) { 646 kdc_log(0, "Malformed HTTP request from %s", d->addr_string); 647 free(data); 648 return -1; 649 } 650 len = base64_decode(t, data); 651 if(len <= 0){ 652 const char *msg = 653 " 404 Not found\r\n" 654 "Server: Heimdal/" VERSION "\r\n" 655 "Cache-Control: no-cache\r\n" 656 "Pragma: no-cache\r\n" 657 "Content-type: text/html\r\n" 658 "Content-transfer-encoding: 8bit\r\n\r\n" 659 "<TITLE>404 Not found</TITLE>\r\n" 660 "<H1>404 Not found</H1>\r\n" 661 "That page doesn't exist, maybe you are looking for " 662 "<A HREF=\"http://www.pdc.kth.se/heimdal/\">Heimdal</A>?\r\n"; 663 write(d->s, proto, strlen(proto)); 664 write(d->s, msg, strlen(msg)); 665 kdc_log(0, "HTTP request from %s is non KDC request", d->addr_string); 666 kdc_log(5, "Request: %s", t); 667 free(data); 668 return -1; 669 } 670 { 671 const char *msg = 672 " 200 OK\r\n" 673 "Server: Heimdal/" VERSION "\r\n" 674 "Cache-Control: no-cache\r\n" 675 "Pragma: no-cache\r\n" 676 "Content-type: application/octet-stream\r\n" 677 "Content-transfer-encoding: binary\r\n\r\n"; 678 write(d->s, proto, strlen(proto)); 679 write(d->s, msg, strlen(msg)); 680 } 681 memcpy(d->buf, data, len); 682 d->len = len; 683 free(data); 684 return 1; 685 } 686 687 /* 688 * Handle incoming data to the TCP socket in `d[index]' 689 */ 690 691 static void 692 handle_tcp(struct descr *d, int index, int min_free) 693 { 694 unsigned char buf[1024]; 695 int n; 696 int ret = 0; 697 698 if (d[index].timeout == 0) { 699 add_new_tcp (d, index, min_free); 700 return; 701 } 702 703 n = recvfrom(d[index].s, buf, sizeof(buf), 0, NULL, NULL); 704 if(n < 0){ 705 krb5_warn(context, errno, "recvfrom"); 706 return; 707 } else if (n == 0) { 708 krb5_warnx(context, "connection closed before end of data after %lu " 709 "bytes from %s", 710 (unsigned long)d[index].len, d[index].addr_string); 711 clear_descr (d + index); 712 return; 713 } 714 if (grow_descr (&d[index], n)) 715 return; 716 memcpy(d[index].buf + d[index].len, buf, n); 717 d[index].len += n; 718 if(d[index].len > 4 && d[index].buf[0] == 0) { 719 ret = handle_vanilla_tcp (&d[index]); 720 } else if(enable_http && 721 d[index].len >= 4 && 722 strncmp((char *)d[index].buf, "GET ", 4) == 0 && 723 strncmp((char *)d[index].buf + d[index].len - 4, 724 "\r\n\r\n", 4) == 0) { 725 ret = handle_http_tcp (&d[index]); 726 if (ret < 0) 727 clear_descr (d + index); 728 } else if (d[index].len > 4) { 729 kdc_log (0, "TCP data of strange type from %s", d[index].addr_string); 730 return; 731 } 732 if (ret < 0) 733 return; 734 else if (ret == 1) { 735 do_request(d[index].buf, d[index].len, 1, &d[index]); 736 clear_descr(d + index); 737 } 738 } 739 740 void 741 loop(void) 742 { 743 struct descr *d; 744 int ndescr; 745 746 ndescr = init_sockets(&d); 747 if(ndescr <= 0) 748 krb5_errx(context, 1, "No sockets!"); 749 while(exit_flag == 0){ 750 struct timeval tmout; 751 fd_set fds; 752 int min_free = -1; 753 int max_fd = 0; 754 int i; 755 756 FD_ZERO(&fds); 757 for(i = 0; i < ndescr; i++) { 758 if(d[i].s >= 0){ 759 if(d[i].type == SOCK_STREAM && 760 d[i].timeout && d[i].timeout < time(NULL)) { 761 kdc_log(1, "TCP-connection from %s expired after %lu bytes", 762 d[i].addr_string, (unsigned long)d[i].len); 763 clear_descr(&d[i]); 764 continue; 765 } 766 if(max_fd < d[i].s) 767 max_fd = d[i].s; 768 if (max_fd >= FD_SETSIZE) 769 krb5_errx(context, 1, "fd too large"); 770 FD_SET(d[i].s, &fds); 771 } else if(min_free < 0 || i < min_free) 772 min_free = i; 773 } 774 if(min_free == -1){ 775 struct descr *tmp; 776 tmp = realloc(d, (ndescr + 4) * sizeof(*d)); 777 if(tmp == NULL) 778 krb5_warnx(context, "No memory"); 779 else { 780 d = tmp; 781 reinit_descrs (d, ndescr); 782 memset(d + ndescr, 0, 4 * sizeof(*d)); 783 for(i = ndescr; i < ndescr + 4; i++) 784 init_descr (&d[i]); 785 min_free = ndescr; 786 ndescr += 4; 787 } 788 } 789 790 tmout.tv_sec = TCP_TIMEOUT; 791 tmout.tv_usec = 0; 792 switch(select(max_fd + 1, &fds, 0, 0, &tmout)){ 793 case 0: 794 break; 795 case -1: 796 if (errno != EINTR) 797 krb5_warn(context, errno, "select"); 798 break; 799 default: 800 for(i = 0; i < ndescr; i++) 801 if(d[i].s >= 0 && FD_ISSET(d[i].s, &fds)) { 802 if(d[i].type == SOCK_DGRAM) 803 handle_udp(&d[i]); 804 else if(d[i].type == SOCK_STREAM) 805 handle_tcp(d, i, min_free); 806 } 807 } 808 } 809 free (d); 810 } 811