Make kdc build on windows
[heimdal.git] / kdc / connect.c
blob318179b380b85211516b2ca528b02ac69ba5b9ca
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 krb5_socket_t 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 = rk_INVALID_SOCKET;
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 rk_closesocket(d->s);
274 d->s = rk_INVALID_SOCKET;
275 return;
278 if (sa->sa_family != family)
279 return;
281 d->s = socket(family, type, 0);
282 if(rk_IS_BAD_SOCKET(d->s)){
283 krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
284 d->s = rk_INVALID_SOCKET;
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(rk_IS_SOCKET_ERROR(bind(d->s, sa, sa_size))){
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 rk_closesocket(d->s);
303 d->s = rk_INVALID_SOCKET;
304 return;
306 if(type == SOCK_STREAM && rk_IS_SOCKET_ERROR(listen(d->s, SOMAXCONN))){
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 rk_closesocket(d->s);
313 d->s = rk_INVALID_SOCKET;
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 != rk_INVALID_SOCKET){
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(rk_IS_SOCKET_ERROR(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len))) {
427 kdc_log (context, config,
428 0, "sendto(%s): %s", d->addr_string,
429 strerror(rk_SOCK_ERRNO));
430 return;
433 if(rk_IS_SOCKET_ERROR(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len))) {
434 kdc_log (context, config, 0, "sendto(%s): %s", d->addr_string,
435 strerror(rk_SOCK_ERRNO));
436 return;
441 * Handle the request in `buf, len' to socket `d'
444 static void
445 do_request(krb5_context context,
446 krb5_kdc_configuration *config,
447 void *buf, size_t len, krb5_boolean prependlength,
448 struct descr *d)
450 krb5_error_code ret;
451 krb5_data reply;
452 int datagram_reply = (d->type == SOCK_DGRAM);
454 krb5_kdc_update_time(NULL);
456 krb5_data_zero(&reply);
457 ret = krb5_kdc_process_request(context, config,
458 buf, len, &reply, &prependlength,
459 d->addr_string, d->sa,
460 datagram_reply);
461 if(request_log)
462 krb5_kdc_save_request(context, request_log, buf, len, &reply, d->sa);
463 if(reply.length){
464 send_reply(context, config, prependlength, d, &reply);
465 krb5_data_free(&reply);
467 if(ret)
468 kdc_log(context, config, 0,
469 "Failed processing %lu byte request from %s",
470 (unsigned long)len, d->addr_string);
474 * Handle incoming data to the UDP socket in `d'
477 static void
478 handle_udp(krb5_context context,
479 krb5_kdc_configuration *config,
480 struct descr *d)
482 unsigned char *buf;
483 int n;
485 buf = malloc(max_request_udp);
486 if(buf == NULL){
487 kdc_log(context, config, 0, "Failed to allocate %lu bytes", (unsigned long)max_request_udp);
488 return;
491 d->sock_len = sizeof(d->__ss);
492 n = recvfrom(d->s, buf, max_request_udp, 0, d->sa, &d->sock_len);
493 if(rk_IS_SOCKET_ERROR(n))
494 krb5_warn(context, rk_SOCK_ERRNO, "recvfrom");
495 else {
496 addr_to_string (context, d->sa, d->sock_len,
497 d->addr_string, sizeof(d->addr_string));
498 if (n == max_request_udp) {
499 krb5_data data;
500 krb5_warn(context, errno,
501 "recvfrom: truncated packet from %s, asking for TCP",
502 d->addr_string);
503 krb5_mk_error(context,
504 KRB5KRB_ERR_RESPONSE_TOO_BIG,
505 NULL,
506 NULL,
507 NULL,
508 NULL,
509 NULL,
510 NULL,
511 &data);
512 send_reply(context, config, FALSE, d, &data);
513 krb5_data_free(&data);
514 } else {
515 do_request(context, config, buf, n, FALSE, d);
518 free (buf);
521 static void
522 clear_descr(struct descr *d)
524 if(d->buf)
525 memset(d->buf, 0, d->size);
526 d->len = 0;
527 if(d->s != rk_INVALID_SOCKET)
528 rk_closesocket(d->s);
529 d->s = rk_INVALID_SOCKET;
533 /* remove HTTP %-quoting from buf */
534 static int
535 de_http(char *buf)
537 unsigned char *p, *q;
538 for(p = q = (unsigned char *)buf; *p; p++, q++) {
539 if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
540 unsigned int x;
541 if(sscanf((char *)p + 1, "%2x", &x) != 1)
542 return -1;
543 *q = x;
544 p += 2;
545 } else
546 *q = *p;
548 *q = '\0';
549 return 0;
552 #define TCP_TIMEOUT 4
555 * accept a new TCP connection on `d[parent]' and store it in `d[child]'
558 static void
559 add_new_tcp (krb5_context context,
560 krb5_kdc_configuration *config,
561 struct descr *d, int parent, int child)
563 krb5_socket_t s;
565 if (child == -1)
566 return;
568 d[child].sock_len = sizeof(d[child].__ss);
569 s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
570 if(rk_IS_BAD_SOCKET(s)) {
571 krb5_warn(context, rk_SOCK_ERRNO, "accept");
572 return;
575 #ifdef FD_SETSIZE
576 if (s >= FD_SETSIZE) {
577 krb5_warnx(context, "socket FD too large");
578 rk_closesocket (s);
579 return;
581 #endif
583 d[child].s = s;
584 d[child].timeout = time(NULL) + TCP_TIMEOUT;
585 d[child].type = SOCK_STREAM;
586 addr_to_string (context,
587 d[child].sa, d[child].sock_len,
588 d[child].addr_string, sizeof(d[child].addr_string));
592 * Grow `d' to handle at least `n'.
593 * Return != 0 if fails
596 static int
597 grow_descr (krb5_context context,
598 krb5_kdc_configuration *config,
599 struct descr *d, size_t n)
601 if (d->size - d->len < n) {
602 unsigned char *tmp;
603 size_t grow;
605 grow = max(1024, d->len + n);
606 if (d->size + grow > max_request_tcp) {
607 kdc_log(context, config, 0, "Request exceeds max request size (%lu bytes).",
608 (unsigned long)d->size + grow);
609 clear_descr(d);
610 return -1;
612 tmp = realloc (d->buf, d->size + grow);
613 if (tmp == NULL) {
614 kdc_log(context, config, 0, "Failed to re-allocate %lu bytes.",
615 (unsigned long)d->size + grow);
616 clear_descr(d);
617 return -1;
619 d->size += grow;
620 d->buf = tmp;
622 return 0;
626 * Try to handle the TCP data at `d->buf, d->len'.
627 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
630 static int
631 handle_vanilla_tcp (krb5_context context,
632 krb5_kdc_configuration *config,
633 struct descr *d)
635 krb5_storage *sp;
636 uint32_t len;
638 sp = krb5_storage_from_mem(d->buf, d->len);
639 if (sp == NULL) {
640 kdc_log (context, config, 0, "krb5_storage_from_mem failed");
641 return -1;
643 krb5_ret_uint32(sp, &len);
644 krb5_storage_free(sp);
645 if(d->len - 4 >= len) {
646 memmove(d->buf, d->buf + 4, d->len - 4);
647 d->len -= 4;
648 return 1;
650 return 0;
654 * Try to handle the TCP/HTTP data at `d->buf, d->len'.
655 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
658 static int
659 handle_http_tcp (krb5_context context,
660 krb5_kdc_configuration *config,
661 struct descr *d)
663 char *s, *p, *t;
664 void *data;
665 char *proto;
666 int len;
668 s = (char *)d->buf;
670 p = strstr(s, "\r\n");
671 if (p == NULL) {
672 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
673 return -1;
675 *p = 0;
677 p = NULL;
678 t = strtok_r(s, " \t", &p);
679 if (t == NULL) {
680 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
681 return -1;
683 t = strtok_r(NULL, " \t", &p);
684 if(t == NULL) {
685 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
686 return -1;
688 data = malloc(strlen(t));
689 if (data == NULL) {
690 kdc_log(context, config, 0, "Failed to allocate %lu bytes",
691 (unsigned long)strlen(t));
692 return -1;
694 if(*t == '/')
695 t++;
696 if(de_http(t) != 0) {
697 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
698 kdc_log(context, config, 5, "HTTP request: %s", t);
699 free(data);
700 return -1;
702 proto = strtok_r(NULL, " \t", &p);
703 if (proto == NULL) {
704 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
705 free(data);
706 return -1;
708 len = base64_decode(t, data);
709 if(len <= 0){
710 const char *msg =
711 " 404 Not found\r\n"
712 "Server: Heimdal/" VERSION "\r\n"
713 "Cache-Control: no-cache\r\n"
714 "Pragma: no-cache\r\n"
715 "Content-type: text/html\r\n"
716 "Content-transfer-encoding: 8bit\r\n\r\n"
717 "<TITLE>404 Not found</TITLE>\r\n"
718 "<H1>404 Not found</H1>\r\n"
719 "That page doesn't exist, maybe you are looking for "
720 "<A HREF=\"http://www.h5l.org/\">Heimdal</A>?\r\n";
721 kdc_log(context, config, 0, "HTTP request from %s is non KDC request", d->addr_string);
722 kdc_log(context, config, 5, "HTTP request: %s", t);
723 free(data);
724 if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) {
725 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
726 d->addr_string, strerror(rk_SOCK_ERRNO));
727 return -1;
729 if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) {
730 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
731 d->addr_string, strerror(rk_SOCK_ERRNO));
732 return -1;
734 return -1;
737 const char *msg =
738 " 200 OK\r\n"
739 "Server: Heimdal/" VERSION "\r\n"
740 "Cache-Control: no-cache\r\n"
741 "Pragma: no-cache\r\n"
742 "Content-type: application/octet-stream\r\n"
743 "Content-transfer-encoding: binary\r\n\r\n";
744 if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) {
745 free(data);
746 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
747 d->addr_string, strerror(rk_SOCK_ERRNO));
748 return -1;
750 if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) {
751 free(data);
752 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
753 d->addr_string, strerror(rk_SOCK_ERRNO));
754 return -1;
757 if (len > d->len)
758 len = d->len;
759 memcpy(d->buf, data, len);
760 d->len = len;
761 free(data);
762 return 1;
766 * Handle incoming data to the TCP socket in `d[index]'
769 static void
770 handle_tcp(krb5_context context,
771 krb5_kdc_configuration *config,
772 struct descr *d, int idx, int min_free)
774 unsigned char buf[1024];
775 int n;
776 int ret = 0;
778 if (d[idx].timeout == 0) {
779 add_new_tcp (context, config, d, idx, min_free);
780 return;
783 n = recvfrom(d[idx].s, buf, sizeof(buf), 0, NULL, NULL);
784 if(rk_IS_SOCKET_ERROR(n)){
785 krb5_warn(context, rk_SOCK_ERRNO, "recvfrom failed from %s to %s/%d",
786 d[idx].addr_string, descr_type(d + idx),
787 ntohs(d[idx].port));
788 return;
789 } else if (n == 0) {
790 krb5_warnx(context, "connection closed before end of data after %lu "
791 "bytes from %s to %s/%d", (unsigned long)d[idx].len,
792 d[idx].addr_string, descr_type(d + idx),
793 ntohs(d[idx].port));
794 clear_descr (d + idx);
795 return;
797 if (grow_descr (context, config, &d[idx], n))
798 return;
799 memcpy(d[idx].buf + d[idx].len, buf, n);
800 d[idx].len += n;
801 if(d[idx].len > 4 && d[idx].buf[0] == 0) {
802 ret = handle_vanilla_tcp (context, config, &d[idx]);
803 } else if(enable_http &&
804 d[idx].len >= 4 &&
805 strncmp((char *)d[idx].buf, "GET ", 4) == 0 &&
806 strncmp((char *)d[idx].buf + d[idx].len - 4,
807 "\r\n\r\n", 4) == 0) {
809 /* remove the trailing \r\n\r\n so the string is NUL terminated */
810 d[idx].buf[d[idx].len - 4] = '\0';
812 ret = handle_http_tcp (context, config, &d[idx]);
813 if (ret < 0)
814 clear_descr (d + idx);
815 } else if (d[idx].len > 4) {
816 kdc_log (context, config,
817 0, "TCP data of strange type from %s to %s/%d",
818 d[idx].addr_string, descr_type(d + idx),
819 ntohs(d[idx].port));
820 if (d[idx].buf[0] & 0x80) {
821 krb5_data reply;
823 kdc_log (context, config, 0, "TCP extension not supported");
825 ret = krb5_mk_error(context,
826 KRB5KRB_ERR_FIELD_TOOLONG,
827 NULL,
828 NULL,
829 NULL,
830 NULL,
831 NULL,
832 NULL,
833 &reply);
834 if (ret == 0) {
835 send_reply(context, config, TRUE, d + idx, &reply);
836 krb5_data_free(&reply);
839 clear_descr(d + idx);
840 return;
842 if (ret < 0)
843 return;
844 else if (ret == 1) {
845 do_request(context, config,
846 d[idx].buf, d[idx].len, TRUE, &d[idx]);
847 clear_descr(d + idx);
851 void
852 loop(krb5_context context,
853 krb5_kdc_configuration *config)
855 struct descr *d;
856 unsigned int ndescr;
858 ndescr = init_sockets(context, config, &d);
859 if(ndescr <= 0)
860 krb5_errx(context, 1, "No sockets!");
861 kdc_log(context, config, 0, "KDC started");
862 while(exit_flag == 0){
863 struct timeval tmout;
864 fd_set fds;
865 int min_free = -1;
866 int max_fd = 0;
867 int i;
869 FD_ZERO(&fds);
870 for(i = 0; i < ndescr; i++) {
871 if(!rk_IS_BAD_SOCKET(d[i].s)){
872 if(d[i].type == SOCK_STREAM &&
873 d[i].timeout && d[i].timeout < time(NULL)) {
874 kdc_log(context, config, 1,
875 "TCP-connection from %s expired after %lu bytes",
876 d[i].addr_string, (unsigned long)d[i].len);
877 clear_descr(&d[i]);
878 continue;
880 if(max_fd < d[i].s)
881 max_fd = d[i].s;
882 #ifdef FD_SETSIZE
883 if (max_fd >= FD_SETSIZE)
884 krb5_errx(context, 1, "fd too large");
885 #endif
886 FD_SET(d[i].s, &fds);
887 } else if(min_free < 0 || i < min_free)
888 min_free = i;
890 if(min_free == -1){
891 struct descr *tmp;
892 tmp = realloc(d, (ndescr + 4) * sizeof(*d));
893 if(tmp == NULL)
894 krb5_warnx(context, "No memory");
895 else {
896 d = tmp;
897 reinit_descrs (d, ndescr);
898 memset(d + ndescr, 0, 4 * sizeof(*d));
899 for(i = ndescr; i < ndescr + 4; i++)
900 init_descr (&d[i]);
901 min_free = ndescr;
902 ndescr += 4;
906 tmout.tv_sec = TCP_TIMEOUT;
907 tmout.tv_usec = 0;
908 switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
909 case 0:
910 break;
911 case -1:
912 if (errno != EINTR)
913 krb5_warn(context, rk_SOCK_ERRNO, "select");
914 break;
915 default:
916 for(i = 0; i < ndescr; i++)
917 if(!rk_IS_BAD_SOCKET(d[i].s) && FD_ISSET(d[i].s, &fds)) {
918 if(d[i].type == SOCK_DGRAM)
919 handle_udp(context, config, &d[i]);
920 else if(d[i].type == SOCK_STREAM)
921 handle_tcp(context, config, d, i, min_free);
925 if (0);
926 #ifdef SIGXCPU
927 else if(exit_flag == SIGXCPU)
928 kdc_log(context, config, 0, "CPU time limit exceeded");
929 #endif
930 else if(exit_flag == SIGINT || exit_flag == SIGTERM)
931 kdc_log(context, config, 0, "Terminated");
932 else
933 kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag);
934 free (d);