Remove unused structure
[heimdal.git] / kdc / connect.c
blobc9b0ec5382ee453d6dffbd8afbb40be2faaf76ce
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("$Id$");
38 /* Should we enable the HTTP hack? */
39 int enable_http = -1;
41 /* Log over requests to the KDC */
42 const char *request_log;
44 /* A string describing on what ports to listen */
45 const char *port_str;
47 krb5_addresses explicit_addresses;
49 size_t max_request_udp;
50 size_t max_request_tcp;
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_udp);
485 if(buf == NULL){
486 kdc_log(context, config, 0, "Failed to allocate %lu bytes", (unsigned long)max_request_udp);
487 return;
490 d->sock_len = sizeof(d->__ss);
491 n = recvfrom(d->s, buf, max_request_udp, 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 if (n == max_request_udp) {
498 krb5_data data;
499 krb5_warn(context, errno,
500 "recvfrom: truncated packet from %s, asking for TCP",
501 d->addr_string);
502 krb5_mk_error(context,
503 KRB5KRB_ERR_RESPONSE_TOO_BIG,
504 NULL,
505 NULL,
506 NULL,
507 NULL,
508 NULL,
509 NULL,
510 &data);
511 send_reply(context, config, FALSE, d, &data);
512 krb5_data_free(&data);
513 } else {
514 do_request(context, config, buf, n, FALSE, d);
517 free (buf);
520 static void
521 clear_descr(struct descr *d)
523 if(d->buf)
524 memset(d->buf, 0, d->size);
525 d->len = 0;
526 if(d->s != -1)
527 close(d->s);
528 d->s = -1;
532 /* remove HTTP %-quoting from buf */
533 static int
534 de_http(char *buf)
536 unsigned char *p, *q;
537 for(p = q = (unsigned char *)buf; *p; p++, q++) {
538 if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
539 unsigned int x;
540 if(sscanf((char *)p + 1, "%2x", &x) != 1)
541 return -1;
542 *q = x;
543 p += 2;
544 } else
545 *q = *p;
547 *q = '\0';
548 return 0;
551 #define TCP_TIMEOUT 4
554 * accept a new TCP connection on `d[parent]' and store it in `d[child]'
557 static void
558 add_new_tcp (krb5_context context,
559 krb5_kdc_configuration *config,
560 struct descr *d, int parent, int child)
562 int s;
564 if (child == -1)
565 return;
567 d[child].sock_len = sizeof(d[child].__ss);
568 s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
569 if(s < 0) {
570 krb5_warn(context, errno, "accept");
571 return;
574 if (s >= FD_SETSIZE) {
575 krb5_warnx(context, "socket FD too large");
576 close (s);
577 return;
580 d[child].s = s;
581 d[child].timeout = time(NULL) + TCP_TIMEOUT;
582 d[child].type = SOCK_STREAM;
583 addr_to_string (context,
584 d[child].sa, d[child].sock_len,
585 d[child].addr_string, sizeof(d[child].addr_string));
589 * Grow `d' to handle at least `n'.
590 * Return != 0 if fails
593 static int
594 grow_descr (krb5_context context,
595 krb5_kdc_configuration *config,
596 struct descr *d, size_t n)
598 if (d->size - d->len < n) {
599 unsigned char *tmp;
600 size_t grow;
602 grow = max(1024, d->len + n);
603 if (d->size + grow > max_request_tcp) {
604 kdc_log(context, config, 0, "Request exceeds max request size (%lu bytes).",
605 (unsigned long)d->size + grow);
606 clear_descr(d);
607 return -1;
609 tmp = realloc (d->buf, d->size + grow);
610 if (tmp == NULL) {
611 kdc_log(context, config, 0, "Failed to re-allocate %lu bytes.",
612 (unsigned long)d->size + grow);
613 clear_descr(d);
614 return -1;
616 d->size += grow;
617 d->buf = tmp;
619 return 0;
623 * Try to handle the TCP data at `d->buf, d->len'.
624 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
627 static int
628 handle_vanilla_tcp (krb5_context context,
629 krb5_kdc_configuration *config,
630 struct descr *d)
632 krb5_storage *sp;
633 uint32_t len;
635 sp = krb5_storage_from_mem(d->buf, d->len);
636 if (sp == NULL) {
637 kdc_log (context, config, 0, "krb5_storage_from_mem failed");
638 return -1;
640 krb5_ret_uint32(sp, &len);
641 krb5_storage_free(sp);
642 if(d->len - 4 >= len) {
643 memmove(d->buf, d->buf + 4, d->len - 4);
644 d->len -= 4;
645 return 1;
647 return 0;
651 * Try to handle the TCP/HTTP data at `d->buf, d->len'.
652 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
655 static int
656 handle_http_tcp (krb5_context context,
657 krb5_kdc_configuration *config,
658 struct descr *d)
660 char *s, *p, *t;
661 void *data;
662 char *proto;
663 int len;
665 s = (char *)d->buf;
667 p = strstr(s, "\r\n");
668 if (p == NULL) {
669 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
670 return -1;
672 *p = 0;
674 p = NULL;
675 t = strtok_r(s, " \t", &p);
676 if (t == NULL) {
677 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
678 return -1;
680 t = strtok_r(NULL, " \t", &p);
681 if(t == NULL) {
682 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
683 return -1;
685 data = malloc(strlen(t));
686 if (data == NULL) {
687 kdc_log(context, config, 0, "Failed to allocate %lu bytes",
688 (unsigned long)strlen(t));
689 return -1;
691 if(*t == '/')
692 t++;
693 if(de_http(t) != 0) {
694 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
695 kdc_log(context, config, 5, "HTTP request: %s", t);
696 free(data);
697 return -1;
699 proto = strtok_r(NULL, " \t", &p);
700 if (proto == NULL) {
701 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
702 free(data);
703 return -1;
705 len = base64_decode(t, data);
706 if(len <= 0){
707 const char *msg =
708 " 404 Not found\r\n"
709 "Server: Heimdal/" VERSION "\r\n"
710 "Cache-Control: no-cache\r\n"
711 "Pragma: no-cache\r\n"
712 "Content-type: text/html\r\n"
713 "Content-transfer-encoding: 8bit\r\n\r\n"
714 "<TITLE>404 Not found</TITLE>\r\n"
715 "<H1>404 Not found</H1>\r\n"
716 "That page doesn't exist, maybe you are looking for "
717 "<A HREF=\"http://www.h5l.org/\">Heimdal</A>?\r\n";
718 kdc_log(context, config, 0, "HTTP request from %s is non KDC request", d->addr_string);
719 kdc_log(context, config, 5, "HTTP request: %s", t);
720 free(data);
721 if (write(d->s, proto, strlen(proto)) < 0) {
722 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
723 d->addr_string, strerror(errno));
724 return -1;
726 if (write(d->s, msg, strlen(msg)) < 0) {
727 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
728 d->addr_string, strerror(errno));
729 return -1;
731 return -1;
734 const char *msg =
735 " 200 OK\r\n"
736 "Server: Heimdal/" VERSION "\r\n"
737 "Cache-Control: no-cache\r\n"
738 "Pragma: no-cache\r\n"
739 "Content-type: application/octet-stream\r\n"
740 "Content-transfer-encoding: binary\r\n\r\n";
741 if (write(d->s, proto, strlen(proto)) < 0) {
742 free(data);
743 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
744 d->addr_string, strerror(errno));
745 return -1;
747 if (write(d->s, msg, strlen(msg)) < 0) {
748 free(data);
749 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
750 d->addr_string, strerror(errno));
751 return -1;
754 if (len > d->len)
755 len = d->len;
756 memcpy(d->buf, data, len);
757 d->len = len;
758 free(data);
759 return 1;
763 * Handle incoming data to the TCP socket in `d[index]'
766 static void
767 handle_tcp(krb5_context context,
768 krb5_kdc_configuration *config,
769 struct descr *d, int idx, int min_free)
771 unsigned char buf[1024];
772 int n;
773 int ret = 0;
775 if (d[idx].timeout == 0) {
776 add_new_tcp (context, config, d, idx, min_free);
777 return;
780 n = recvfrom(d[idx].s, buf, sizeof(buf), 0, NULL, NULL);
781 if(n < 0){
782 krb5_warn(context, errno, "recvfrom failed from %s to %s/%d",
783 d[idx].addr_string, descr_type(d + idx),
784 ntohs(d[idx].port));
785 return;
786 } else if (n == 0) {
787 krb5_warnx(context, "connection closed before end of data after %lu "
788 "bytes from %s to %s/%d", (unsigned long)d[idx].len,
789 d[idx].addr_string, descr_type(d + idx),
790 ntohs(d[idx].port));
791 clear_descr (d + idx);
792 return;
794 if (grow_descr (context, config, &d[idx], n))
795 return;
796 memcpy(d[idx].buf + d[idx].len, buf, n);
797 d[idx].len += n;
798 if(d[idx].len > 4 && d[idx].buf[0] == 0) {
799 ret = handle_vanilla_tcp (context, config, &d[idx]);
800 } else if(enable_http &&
801 d[idx].len >= 4 &&
802 strncmp((char *)d[idx].buf, "GET ", 4) == 0 &&
803 strncmp((char *)d[idx].buf + d[idx].len - 4,
804 "\r\n\r\n", 4) == 0) {
806 /* remove the trailing \r\n\r\n so the string is NUL terminated */
807 d[idx].buf[d[idx].len - 4] = '\0';
809 ret = handle_http_tcp (context, config, &d[idx]);
810 if (ret < 0)
811 clear_descr (d + idx);
812 } else if (d[idx].len > 4) {
813 kdc_log (context, config,
814 0, "TCP data of strange type from %s to %s/%d",
815 d[idx].addr_string, descr_type(d + idx),
816 ntohs(d[idx].port));
817 if (d[idx].buf[0] & 0x80) {
818 krb5_data reply;
820 kdc_log (context, config, 0, "TCP extension not supported");
822 ret = krb5_mk_error(context,
823 KRB5KRB_ERR_FIELD_TOOLONG,
824 NULL,
825 NULL,
826 NULL,
827 NULL,
828 NULL,
829 NULL,
830 &reply);
831 if (ret == 0) {
832 send_reply(context, config, TRUE, d + idx, &reply);
833 krb5_data_free(&reply);
836 clear_descr(d + idx);
837 return;
839 if (ret < 0)
840 return;
841 else if (ret == 1) {
842 do_request(context, config,
843 d[idx].buf, d[idx].len, TRUE, &d[idx]);
844 clear_descr(d + idx);
848 void
849 loop(krb5_context context,
850 krb5_kdc_configuration *config)
852 struct descr *d;
853 unsigned int ndescr;
855 ndescr = init_sockets(context, config, &d);
856 if(ndescr <= 0)
857 krb5_errx(context, 1, "No sockets!");
858 kdc_log(context, config, 0, "KDC started");
859 while(exit_flag == 0){
860 struct timeval tmout;
861 fd_set fds;
862 int min_free = -1;
863 int max_fd = 0;
864 int i;
866 FD_ZERO(&fds);
867 for(i = 0; i < ndescr; i++) {
868 if(d[i].s >= 0){
869 if(d[i].type == SOCK_STREAM &&
870 d[i].timeout && d[i].timeout < time(NULL)) {
871 kdc_log(context, config, 1,
872 "TCP-connection from %s expired after %lu bytes",
873 d[i].addr_string, (unsigned long)d[i].len);
874 clear_descr(&d[i]);
875 continue;
877 if(max_fd < d[i].s)
878 max_fd = d[i].s;
879 if (max_fd >= FD_SETSIZE)
880 krb5_errx(context, 1, "fd too large");
881 FD_SET(d[i].s, &fds);
882 } else if(min_free < 0 || i < min_free)
883 min_free = i;
885 if(min_free == -1){
886 struct descr *tmp;
887 tmp = realloc(d, (ndescr + 4) * sizeof(*d));
888 if(tmp == NULL)
889 krb5_warnx(context, "No memory");
890 else {
891 d = tmp;
892 reinit_descrs (d, ndescr);
893 memset(d + ndescr, 0, 4 * sizeof(*d));
894 for(i = ndescr; i < ndescr + 4; i++)
895 init_descr (&d[i]);
896 min_free = ndescr;
897 ndescr += 4;
901 tmout.tv_sec = TCP_TIMEOUT;
902 tmout.tv_usec = 0;
903 switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
904 case 0:
905 break;
906 case -1:
907 if (errno != EINTR)
908 krb5_warn(context, errno, "select");
909 break;
910 default:
911 for(i = 0; i < ndescr; i++)
912 if(d[i].s >= 0 && FD_ISSET(d[i].s, &fds)) {
913 if(d[i].type == SOCK_DGRAM)
914 handle_udp(context, config, &d[i]);
915 else if(d[i].type == SOCK_STREAM)
916 handle_tcp(context, config, d, i, min_free);
920 if(exit_flag == SIGXCPU)
921 kdc_log(context, config, 0, "CPU time limit exceeded");
922 else if(exit_flag == SIGINT || exit_flag == SIGTERM)
923 kdc_log(context, config, 0, "Terminated");
924 else
925 kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag);
926 free (d);