Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / crypto / dist / heimdal / kdc / connect.c
blobd1d9674742738be44d6ea4f0c65b7df47b69df1f
1 /*
2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "kdc_locl.h"
36 __RCSID("$Heimdal: connect.c 22434 2008-01-14 09:21:37Z lha $"
37 "$NetBSD$");
39 /* Should we enable the HTTP hack? */
40 int enable_http = -1;
42 /* Log over requests to the KDC */
43 const char *request_log;
45 /* A string describing on what ports to listen */
46 const char *port_str;
48 krb5_addresses explicit_addresses;
50 size_t max_request; /* maximal size of a request */
53 * a tuple describing on what to listen
56 struct port_desc{
57 int family;
58 int type;
59 int port;
62 /* the current ones */
64 static struct port_desc *ports;
65 static int num_ports;
68 * add `family, port, protocol' to the list with duplicate suppresion.
71 static void
72 add_port(krb5_context context,
73 int family, int port, const char *protocol)
75 int type;
76 int i;
78 if(strcmp(protocol, "udp") == 0)
79 type = SOCK_DGRAM;
80 else if(strcmp(protocol, "tcp") == 0)
81 type = SOCK_STREAM;
82 else
83 return;
84 for(i = 0; i < num_ports; i++){
85 if(ports[i].type == type
86 && ports[i].port == port
87 && ports[i].family == family)
88 return;
90 ports = realloc(ports, (num_ports + 1) * sizeof(*ports));
91 if (ports == NULL)
92 krb5_err (context, 1, errno, "realloc");
93 ports[num_ports].family = family;
94 ports[num_ports].type = type;
95 ports[num_ports].port = port;
96 num_ports++;
100 * add a triple but with service -> port lookup
101 * (this prints warnings for stuff that does not exist)
104 static void
105 add_port_service(krb5_context context,
106 int family, const char *service, int port,
107 const char *protocol)
109 port = krb5_getportbyname (context, service, protocol, port);
110 add_port (context, family, port, protocol);
114 * add the port with service -> port lookup or string -> number
115 * (no warning is printed)
118 static void
119 add_port_string (krb5_context context,
120 int family, const char *str, const char *protocol)
122 struct servent *sp;
123 int port;
125 sp = roken_getservbyname (str, protocol);
126 if (sp != NULL) {
127 port = sp->s_port;
128 } else {
129 char *end;
131 port = htons(strtol(str, &end, 0));
132 if (end == str)
133 return;
135 add_port (context, family, port, protocol);
139 * add the standard collection of ports for `family'
142 static void
143 add_standard_ports (krb5_context context,
144 krb5_kdc_configuration *config,
145 int family)
147 add_port_service(context, family, "kerberos", 88, "udp");
148 add_port_service(context, family, "kerberos", 88, "tcp");
149 add_port_service(context, family, "kerberos-sec", 88, "udp");
150 add_port_service(context, family, "kerberos-sec", 88, "tcp");
151 if(enable_http)
152 add_port_service(context, family, "http", 80, "tcp");
153 if(config->enable_524) {
154 add_port_service(context, family, "krb524", 4444, "udp");
155 add_port_service(context, family, "krb524", 4444, "tcp");
157 if(config->enable_v4) {
158 add_port_service(context, family, "kerberos-iv", 750, "udp");
159 add_port_service(context, family, "kerberos-iv", 750, "tcp");
161 if (config->enable_kaserver)
162 add_port_service(context, family, "afs3-kaserver", 7004, "udp");
163 if(config->enable_kx509) {
164 add_port_service(context, family, "kca_service", 9878, "udp");
165 add_port_service(context, family, "kca_service", 9878, "tcp");
171 * parse the set of space-delimited ports in `str' and add them.
172 * "+" => all the standard ones
173 * otherwise it's port|service[/protocol]
176 static void
177 parse_ports(krb5_context context,
178 krb5_kdc_configuration *config,
179 const char *str)
181 char *pos = NULL;
182 char *p;
183 char *str_copy = strdup (str);
185 p = strtok_r(str_copy, " \t", &pos);
186 while(p != NULL) {
187 if(strcmp(p, "+") == 0) {
188 #ifdef HAVE_IPV6
189 add_standard_ports(context, config, AF_INET6);
190 #endif
191 add_standard_ports(context, config, AF_INET);
192 } else {
193 char *q = strchr(p, '/');
194 if(q){
195 *q++ = 0;
196 #ifdef HAVE_IPV6
197 add_port_string(context, AF_INET6, p, q);
198 #endif
199 add_port_string(context, AF_INET, p, q);
200 }else {
201 #ifdef HAVE_IPV6
202 add_port_string(context, AF_INET6, p, "udp");
203 add_port_string(context, AF_INET6, p, "tcp");
204 #endif
205 add_port_string(context, AF_INET, p, "udp");
206 add_port_string(context, AF_INET, p, "tcp");
210 p = strtok_r(NULL, " \t", &pos);
212 free (str_copy);
216 * every socket we listen on
219 struct descr {
220 int s;
221 int type;
222 int port;
223 unsigned char *buf;
224 size_t size;
225 size_t len;
226 time_t timeout;
227 struct sockaddr_storage __ss;
228 struct sockaddr *sa;
229 socklen_t sock_len;
230 char addr_string[128];
233 static void
234 init_descr(struct descr *d)
236 memset(d, 0, sizeof(*d));
237 d->sa = (struct sockaddr *)&d->__ss;
238 d->s = -1;
242 * re-initialize all `n' ->sa in `d'.
245 static void
246 reinit_descrs (struct descr *d, int n)
248 int i;
250 for (i = 0; i < n; ++i)
251 d[i].sa = (struct sockaddr *)&d[i].__ss;
255 * Create the socket (family, type, port) in `d'
258 static void
259 init_socket(krb5_context context,
260 krb5_kdc_configuration *config,
261 struct descr *d, krb5_address *a, int family, int type, int port)
263 krb5_error_code ret;
264 struct sockaddr_storage __ss;
265 struct sockaddr *sa = (struct sockaddr *)&__ss;
266 krb5_socklen_t sa_size = sizeof(__ss);
268 init_descr (d);
270 ret = krb5_addr2sockaddr (context, a, sa, &sa_size, port);
271 if (ret) {
272 krb5_warn(context, ret, "krb5_addr2sockaddr");
273 close(d->s);
274 d->s = -1;
275 return;
278 if (sa->sa_family != family)
279 return;
281 d->s = socket(family, type, 0);
282 if(d->s < 0){
283 krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
284 d->s = -1;
285 return;
287 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
289 int one = 1;
290 setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
292 #endif
293 d->type = type;
294 d->port = port;
296 if(bind(d->s, sa, sa_size) < 0){
297 char a_str[256];
298 size_t len;
300 krb5_print_address (a, a_str, sizeof(a_str), &len);
301 krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port));
302 close(d->s);
303 d->s = -1;
304 return;
306 if(type == SOCK_STREAM && listen(d->s, SOMAXCONN) < 0){
307 char a_str[256];
308 size_t len;
310 krb5_print_address (a, a_str, sizeof(a_str), &len);
311 krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port));
312 close(d->s);
313 d->s = -1;
314 return;
319 * Allocate descriptors for all the sockets that we should listen on
320 * and return the number of them.
323 static int
324 init_sockets(krb5_context context,
325 krb5_kdc_configuration *config,
326 struct descr **desc)
328 krb5_error_code ret;
329 int i, j;
330 struct descr *d;
331 int num = 0;
332 krb5_addresses addresses;
334 if (explicit_addresses.len) {
335 addresses = explicit_addresses;
336 } else {
337 ret = krb5_get_all_server_addrs (context, &addresses);
338 if (ret)
339 krb5_err (context, 1, ret, "krb5_get_all_server_addrs");
341 parse_ports(context, config, port_str);
342 d = malloc(addresses.len * num_ports * sizeof(*d));
343 if (d == NULL)
344 krb5_errx(context, 1, "malloc(%lu) failed",
345 (unsigned long)num_ports * sizeof(*d));
347 for (i = 0; i < num_ports; i++){
348 for (j = 0; j < addresses.len; ++j) {
349 init_socket(context, config, &d[num], &addresses.val[j],
350 ports[i].family, ports[i].type, ports[i].port);
351 if(d[num].s != -1){
352 char a_str[80];
353 size_t len;
355 krb5_print_address (&addresses.val[j], a_str,
356 sizeof(a_str), &len);
358 kdc_log(context, config, 5, "listening on %s port %u/%s",
359 a_str,
360 ntohs(ports[i].port),
361 (ports[i].type == SOCK_STREAM) ? "tcp" : "udp");
362 /* XXX */
363 num++;
367 krb5_free_addresses (context, &addresses);
368 d = realloc(d, num * sizeof(*d));
369 if (d == NULL && num != 0)
370 krb5_errx(context, 1, "realloc(%lu) failed",
371 (unsigned long)num * sizeof(*d));
372 reinit_descrs (d, num);
373 *desc = d;
374 return num;
381 static const char *
382 descr_type(struct descr *d)
384 if (d->type == SOCK_DGRAM)
385 return "udp";
386 else if (d->type == SOCK_STREAM)
387 return "tcp";
388 return "unknown";
391 static void
392 addr_to_string(krb5_context context,
393 struct sockaddr *addr, size_t addr_len, char *str, size_t len)
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;
401 krb5_free_address(context, &a);
403 snprintf(str, len, "<family=%d>", addr->sa_family);
410 static void
411 send_reply(krb5_context context,
412 krb5_kdc_configuration *config,
413 krb5_boolean prependlength,
414 struct descr *d,
415 krb5_data *reply)
417 kdc_log(context, config, 5,
418 "sending %lu bytes to %s", (unsigned long)reply->length,
419 d->addr_string);
420 if(prependlength){
421 unsigned char l[4];
422 l[0] = (reply->length >> 24) & 0xff;
423 l[1] = (reply->length >> 16) & 0xff;
424 l[2] = (reply->length >> 8) & 0xff;
425 l[3] = reply->length & 0xff;
426 if(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len) < 0) {
427 kdc_log (context, config,
428 0, "sendto(%s): %s", d->addr_string, strerror(errno));
429 return;
432 if(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len) < 0) {
433 kdc_log (context, config,
434 0, "sendto(%s): %s", d->addr_string, strerror(errno));
435 return;
440 * Handle the request in `buf, len' to socket `d'
443 static void
444 do_request(krb5_context context,
445 krb5_kdc_configuration *config,
446 void *buf, size_t len, krb5_boolean prependlength,
447 struct descr *d)
449 krb5_error_code ret;
450 krb5_data reply;
451 int datagram_reply = (d->type == SOCK_DGRAM);
453 krb5_kdc_update_time(NULL);
455 krb5_data_zero(&reply);
456 ret = krb5_kdc_process_request(context, config,
457 buf, len, &reply, &prependlength,
458 d->addr_string, d->sa,
459 datagram_reply);
460 if(request_log)
461 krb5_kdc_save_request(context, request_log, buf, len, &reply, d->sa);
462 if(reply.length){
463 send_reply(context, config, prependlength, d, &reply);
464 krb5_data_free(&reply);
466 if(ret)
467 kdc_log(context, config, 0,
468 "Failed processing %lu byte request from %s",
469 (unsigned long)len, d->addr_string);
473 * Handle incoming data to the UDP socket in `d'
476 static void
477 handle_udp(krb5_context context,
478 krb5_kdc_configuration *config,
479 struct descr *d)
481 unsigned char *buf;
482 int n;
484 buf = malloc(max_request);
485 if(buf == NULL){
486 kdc_log(context, config, 0, "Failed to allocate %lu bytes", (unsigned long)max_request);
487 return;
490 d->sock_len = sizeof(d->__ss);
491 n = recvfrom(d->s, buf, max_request, 0, d->sa, &d->sock_len);
492 if(n < 0)
493 krb5_warn(context, errno, "recvfrom");
494 else {
495 addr_to_string (context, d->sa, d->sock_len,
496 d->addr_string, sizeof(d->addr_string));
497 do_request(context, config, buf, n, FALSE, d);
499 free (buf);
502 static void
503 clear_descr(struct descr *d)
505 if(d->buf)
506 memset(d->buf, 0, d->size);
507 d->len = 0;
508 if(d->s != -1)
509 close(d->s);
510 d->s = -1;
514 /* remove HTTP %-quoting from buf */
515 static int
516 de_http(char *buf)
518 unsigned char *p, *q;
519 for(p = q = (unsigned char *)buf; *p; p++, q++) {
520 if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
521 unsigned int x;
522 if(sscanf((char *)p + 1, "%2x", &x) != 1)
523 return -1;
524 *q = x;
525 p += 2;
526 } else
527 *q = *p;
529 *q = '\0';
530 return 0;
533 #define TCP_TIMEOUT 4
536 * accept a new TCP connection on `d[parent]' and store it in `d[child]'
539 static void
540 add_new_tcp (krb5_context context,
541 krb5_kdc_configuration *config,
542 struct descr *d, int parent, int child)
544 int s;
546 if (child == -1)
547 return;
549 d[child].sock_len = sizeof(d[child].__ss);
550 s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
551 if(s < 0) {
552 krb5_warn(context, errno, "accept");
553 return;
556 if (s >= FD_SETSIZE) {
557 krb5_warnx(context, "socket FD too large");
558 close (s);
559 return;
562 d[child].s = s;
563 d[child].timeout = time(NULL) + TCP_TIMEOUT;
564 d[child].type = SOCK_STREAM;
565 addr_to_string (context,
566 d[child].sa, d[child].sock_len,
567 d[child].addr_string, sizeof(d[child].addr_string));
571 * Grow `d' to handle at least `n'.
572 * Return != 0 if fails
575 static int
576 grow_descr (krb5_context context,
577 krb5_kdc_configuration *config,
578 struct descr *d, size_t n)
580 if (d->size - d->len < n) {
581 unsigned char *tmp;
582 size_t grow;
584 grow = max(1024, d->len + n);
585 if (d->size + grow > max_request) {
586 kdc_log(context, config, 0, "Request exceeds max request size (%lu bytes).",
587 (unsigned long)d->size + grow);
588 clear_descr(d);
589 return -1;
591 tmp = realloc (d->buf, d->size + grow);
592 if (tmp == NULL) {
593 kdc_log(context, config, 0, "Failed to re-allocate %lu bytes.",
594 (unsigned long)d->size + grow);
595 clear_descr(d);
596 return -1;
598 d->size += grow;
599 d->buf = tmp;
601 return 0;
605 * Try to handle the TCP data at `d->buf, d->len'.
606 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
609 static int
610 handle_vanilla_tcp (krb5_context context,
611 krb5_kdc_configuration *config,
612 struct descr *d)
614 krb5_storage *sp;
615 uint32_t len;
617 sp = krb5_storage_from_mem(d->buf, d->len);
618 if (sp == NULL) {
619 kdc_log (context, config, 0, "krb5_storage_from_mem failed");
620 return -1;
622 krb5_ret_uint32(sp, &len);
623 krb5_storage_free(sp);
624 if(d->len - 4 >= len) {
625 memmove(d->buf, d->buf + 4, d->len - 4);
626 d->len -= 4;
627 return 1;
629 return 0;
633 * Try to handle the TCP/HTTP data at `d->buf, d->len'.
634 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
637 static int
638 handle_http_tcp (krb5_context context,
639 krb5_kdc_configuration *config,
640 struct descr *d)
642 char *s, *p, *t;
643 void *data;
644 char *proto;
645 int len;
647 s = (char *)d->buf;
649 p = strstr(s, "\r\n");
650 if (p == NULL) {
651 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
652 return -1;
654 *p = 0;
656 p = NULL;
657 t = strtok_r(s, " \t", &p);
658 if (t == NULL) {
659 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
660 return -1;
662 t = strtok_r(NULL, " \t", &p);
663 if(t == NULL) {
664 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
665 return -1;
667 data = malloc(strlen(t));
668 if (data == NULL) {
669 kdc_log(context, config, 0, "Failed to allocate %lu bytes",
670 (unsigned long)strlen(t));
671 return -1;
673 if(*t == '/')
674 t++;
675 if(de_http(t) != 0) {
676 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
677 kdc_log(context, config, 5, "HTTP request: %s", t);
678 free(data);
679 return -1;
681 proto = strtok_r(NULL, " \t", &p);
682 if (proto == NULL) {
683 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
684 free(data);
685 return -1;
687 len = base64_decode(t, data);
688 if(len <= 0){
689 const char *msg =
690 " 404 Not found\r\n"
691 "Server: Heimdal/" VERSION "\r\n"
692 "Cache-Control: no-cache\r\n"
693 "Pragma: no-cache\r\n"
694 "Content-type: text/html\r\n"
695 "Content-transfer-encoding: 8bit\r\n\r\n"
696 "<TITLE>404 Not found</TITLE>\r\n"
697 "<H1>404 Not found</H1>\r\n"
698 "That page doesn't exist, maybe you are looking for "
699 "<A HREF=\"http://www.h5l.org/\">Heimdal</A>?\r\n";
700 kdc_log(context, config, 0, "HTTP request from %s is non KDC request", d->addr_string);
701 kdc_log(context, config, 5, "HTTP request: %s", t);
702 free(data);
703 if (write(d->s, proto, strlen(proto)) < 0) {
704 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
705 d->addr_string, strerror(errno));
706 return -1;
708 if (write(d->s, msg, strlen(msg)) < 0) {
709 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
710 d->addr_string, strerror(errno));
711 return -1;
713 return -1;
716 const char *msg =
717 " 200 OK\r\n"
718 "Server: Heimdal/" VERSION "\r\n"
719 "Cache-Control: no-cache\r\n"
720 "Pragma: no-cache\r\n"
721 "Content-type: application/octet-stream\r\n"
722 "Content-transfer-encoding: binary\r\n\r\n";
723 if (write(d->s, proto, strlen(proto)) < 0) {
724 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
725 d->addr_string, strerror(errno));
726 return -1;
728 if (write(d->s, msg, strlen(msg)) < 0) {
729 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
730 d->addr_string, strerror(errno));
731 return -1;
734 memcpy(d->buf, data, len);
735 d->len = len;
736 free(data);
737 return 1;
741 * Handle incoming data to the TCP socket in `d[index]'
744 static void
745 handle_tcp(krb5_context context,
746 krb5_kdc_configuration *config,
747 struct descr *d, int idx, int min_free)
749 unsigned char buf[1024];
750 int n;
751 int ret = 0;
753 if (d[idx].timeout == 0) {
754 add_new_tcp (context, config, d, idx, min_free);
755 return;
758 n = recvfrom(d[idx].s, buf, sizeof(buf), 0, NULL, NULL);
759 if(n < 0){
760 krb5_warn(context, errno, "recvfrom failed from %s to %s/%d",
761 d[idx].addr_string, descr_type(d + idx),
762 ntohs(d[idx].port));
763 return;
764 } else if (n == 0) {
765 krb5_warnx(context, "connection closed before end of data after %lu "
766 "bytes from %s to %s/%d", (unsigned long)d[idx].len,
767 d[idx].addr_string, descr_type(d + idx),
768 ntohs(d[idx].port));
769 clear_descr (d + idx);
770 return;
772 if (grow_descr (context, config, &d[idx], n))
773 return;
774 memcpy(d[idx].buf + d[idx].len, buf, n);
775 d[idx].len += n;
776 if(d[idx].len > 4 && d[idx].buf[0] == 0) {
777 ret = handle_vanilla_tcp (context, config, &d[idx]);
778 } else if(enable_http &&
779 d[idx].len >= 4 &&
780 strncmp((char *)d[idx].buf, "GET ", 4) == 0 &&
781 strncmp((char *)d[idx].buf + d[idx].len - 4,
782 "\r\n\r\n", 4) == 0) {
783 ret = handle_http_tcp (context, config, &d[idx]);
784 if (ret < 0)
785 clear_descr (d + idx);
786 } else if (d[idx].len > 4) {
787 kdc_log (context, config,
788 0, "TCP data of strange type from %s to %s/%d",
789 d[idx].addr_string, descr_type(d + idx),
790 ntohs(d[idx].port));
791 if (d[idx].buf[0] & 0x80) {
792 krb5_data reply;
794 kdc_log (context, config, 0, "TCP extension not supported");
796 ret = krb5_mk_error(context,
797 KRB5KRB_ERR_FIELD_TOOLONG,
798 NULL,
799 NULL,
800 NULL,
801 NULL,
802 NULL,
803 NULL,
804 &reply);
805 if (ret == 0) {
806 send_reply(context, config, TRUE, d + idx, &reply);
807 krb5_data_free(&reply);
810 clear_descr(d + idx);
811 return;
813 if (ret < 0)
814 return;
815 else if (ret == 1) {
816 do_request(context, config,
817 d[idx].buf, d[idx].len, TRUE, &d[idx]);
818 clear_descr(d + idx);
822 void
823 loop(krb5_context context,
824 krb5_kdc_configuration *config)
826 struct descr *d;
827 int ndescr;
829 ndescr = init_sockets(context, config, &d);
830 if(ndescr <= 0)
831 krb5_errx(context, 1, "No sockets!");
832 kdc_log(context, config, 0, "KDC started");
833 while(exit_flag == 0){
834 struct timeval tmout;
835 fd_set fds;
836 int min_free = -1;
837 int max_fd = 0;
838 int i;
840 FD_ZERO(&fds);
841 for(i = 0; i < ndescr; i++) {
842 if(d[i].s >= 0){
843 if(d[i].type == SOCK_STREAM &&
844 d[i].timeout && d[i].timeout < time(NULL)) {
845 kdc_log(context, config, 1,
846 "TCP-connection from %s expired after %lu bytes",
847 d[i].addr_string, (unsigned long)d[i].len);
848 clear_descr(&d[i]);
849 continue;
851 if(max_fd < d[i].s)
852 max_fd = d[i].s;
853 if (max_fd >= FD_SETSIZE)
854 krb5_errx(context, 1, "fd too large");
855 FD_SET(d[i].s, &fds);
856 } else if(min_free < 0 || i < min_free)
857 min_free = i;
859 if(min_free == -1){
860 struct descr *tmp;
861 tmp = realloc(d, (ndescr + 4) * sizeof(*d));
862 if(tmp == NULL)
863 krb5_warnx(context, "No memory");
864 else {
865 d = tmp;
866 reinit_descrs (d, ndescr);
867 memset(d + ndescr, 0, 4 * sizeof(*d));
868 for(i = ndescr; i < ndescr + 4; i++)
869 init_descr (&d[i]);
870 min_free = ndescr;
871 ndescr += 4;
875 tmout.tv_sec = TCP_TIMEOUT;
876 tmout.tv_usec = 0;
877 switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
878 case 0:
879 break;
880 case -1:
881 if (errno != EINTR)
882 krb5_warn(context, errno, "select");
883 break;
884 default:
885 for(i = 0; i < ndescr; i++)
886 if(d[i].s >= 0 && FD_ISSET(d[i].s, &fds)) {
887 if(d[i].type == SOCK_DGRAM)
888 handle_udp(context, config, &d[i]);
889 else if(d[i].type == SOCK_STREAM)
890 handle_tcp(context, config, d, i, min_free);
894 if(exit_flag == SIGXCPU)
895 kdc_log(context, config, 0, "CPU time limit exceeded");
896 else if(exit_flag == SIGINT || exit_flag == SIGTERM)
897 kdc_log(context, config, 0, "Terminated");
898 else
899 kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag);
900 free (d);