don't undefined SLIST
[heimdal.git] / kdc / connect.c
blob8736286399c6b276903eebabebffa5b73798fd64
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 /* Should we enable the HTTP hack? */
37 int enable_http = -1;
39 /* Log over requests to the KDC */
40 const char *request_log;
42 /* A string describing on what ports to listen */
43 const char *port_str;
45 krb5_addresses explicit_addresses;
47 size_t max_request_udp;
48 size_t max_request_tcp;
51 * a tuple describing on what to listen
54 struct port_desc{
55 int family;
56 int type;
57 int port;
60 /* the current ones */
62 static struct port_desc *ports;
63 static int num_ports;
66 * add `family, port, protocol' to the list with duplicate suppresion.
69 static void
70 add_port(krb5_context context,
71 int family, int port, const char *protocol)
73 int type;
74 int i;
76 if(strcmp(protocol, "udp") == 0)
77 type = SOCK_DGRAM;
78 else if(strcmp(protocol, "tcp") == 0)
79 type = SOCK_STREAM;
80 else
81 return;
82 for(i = 0; i < num_ports; i++){
83 if(ports[i].type == type
84 && ports[i].port == port
85 && ports[i].family == family)
86 return;
88 ports = realloc(ports, (num_ports + 1) * sizeof(*ports));
89 if (ports == NULL)
90 krb5_err (context, 1, errno, "realloc");
91 ports[num_ports].family = family;
92 ports[num_ports].type = type;
93 ports[num_ports].port = port;
94 num_ports++;
98 * add a triple but with service -> port lookup
99 * (this prints warnings for stuff that does not exist)
102 static void
103 add_port_service(krb5_context context,
104 int family, const char *service, int port,
105 const char *protocol)
107 port = krb5_getportbyname (context, service, protocol, port);
108 add_port (context, family, port, protocol);
112 * add the port with service -> port lookup or string -> number
113 * (no warning is printed)
116 static void
117 add_port_string (krb5_context context,
118 int family, const char *str, const char *protocol)
120 struct servent *sp;
121 int port;
123 sp = roken_getservbyname (str, protocol);
124 if (sp != NULL) {
125 port = sp->s_port;
126 } else {
127 char *end;
129 port = htons(strtol(str, &end, 0));
130 if (end == str)
131 return;
133 add_port (context, family, port, protocol);
137 * add the standard collection of ports for `family'
140 static void
141 add_standard_ports (krb5_context context,
142 krb5_kdc_configuration *config,
143 int family)
145 add_port_service(context, family, "kerberos", 88, "udp");
146 add_port_service(context, family, "kerberos", 88, "tcp");
147 add_port_service(context, family, "kerberos-sec", 88, "udp");
148 add_port_service(context, family, "kerberos-sec", 88, "tcp");
149 if(enable_http)
150 add_port_service(context, family, "http", 80, "tcp");
151 if(config->enable_524) {
152 add_port_service(context, family, "krb524", 4444, "udp");
153 add_port_service(context, family, "krb524", 4444, "tcp");
155 if(config->enable_v4) {
156 add_port_service(context, family, "kerberos-iv", 750, "udp");
157 add_port_service(context, family, "kerberos-iv", 750, "tcp");
159 if (config->enable_kaserver)
160 add_port_service(context, family, "afs3-kaserver", 7004, "udp");
161 if(config->enable_kx509) {
162 add_port_service(context, family, "kca_service", 9878, "udp");
163 add_port_service(context, family, "kca_service", 9878, "tcp");
169 * parse the set of space-delimited ports in `str' and add them.
170 * "+" => all the standard ones
171 * otherwise it's port|service[/protocol]
174 static void
175 parse_ports(krb5_context context,
176 krb5_kdc_configuration *config,
177 const char *str)
179 char *pos = NULL;
180 char *p;
181 char *str_copy = strdup (str);
183 p = strtok_r(str_copy, " \t", &pos);
184 while(p != NULL) {
185 if(strcmp(p, "+") == 0) {
186 #ifdef HAVE_IPV6
187 add_standard_ports(context, config, AF_INET6);
188 #endif
189 add_standard_ports(context, config, AF_INET);
190 } else {
191 char *q = strchr(p, '/');
192 if(q){
193 *q++ = 0;
194 #ifdef HAVE_IPV6
195 add_port_string(context, AF_INET6, p, q);
196 #endif
197 add_port_string(context, AF_INET, p, q);
198 }else {
199 #ifdef HAVE_IPV6
200 add_port_string(context, AF_INET6, p, "udp");
201 add_port_string(context, AF_INET6, p, "tcp");
202 #endif
203 add_port_string(context, AF_INET, p, "udp");
204 add_port_string(context, AF_INET, p, "tcp");
208 p = strtok_r(NULL, " \t", &pos);
210 free (str_copy);
214 * every socket we listen on
217 struct descr {
218 krb5_socket_t s;
219 int type;
220 int port;
221 unsigned char *buf;
222 size_t size;
223 size_t len;
224 time_t timeout;
225 struct sockaddr_storage __ss;
226 struct sockaddr *sa;
227 socklen_t sock_len;
228 char addr_string[128];
231 static void
232 init_descr(struct descr *d)
234 memset(d, 0, sizeof(*d));
235 d->sa = (struct sockaddr *)&d->__ss;
236 d->s = rk_INVALID_SOCKET;
240 * re-initialize all `n' ->sa in `d'.
243 static void
244 reinit_descrs (struct descr *d, int n)
246 int i;
248 for (i = 0; i < n; ++i)
249 d[i].sa = (struct sockaddr *)&d[i].__ss;
253 * Create the socket (family, type, port) in `d'
256 static void
257 init_socket(krb5_context context,
258 krb5_kdc_configuration *config,
259 struct descr *d, krb5_address *a, int family, int type, int port)
261 krb5_error_code ret;
262 struct sockaddr_storage __ss;
263 struct sockaddr *sa = (struct sockaddr *)&__ss;
264 krb5_socklen_t sa_size = sizeof(__ss);
266 init_descr (d);
268 ret = krb5_addr2sockaddr (context, a, sa, &sa_size, port);
269 if (ret) {
270 krb5_warn(context, ret, "krb5_addr2sockaddr");
271 rk_closesocket(d->s);
272 d->s = rk_INVALID_SOCKET;
273 return;
276 if (sa->sa_family != family)
277 return;
279 d->s = socket(family, type, 0);
280 if(rk_IS_BAD_SOCKET(d->s)){
281 krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
282 d->s = rk_INVALID_SOCKET;
283 return;
285 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
287 int one = 1;
288 setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
290 #endif
291 d->type = type;
292 d->port = port;
294 if(rk_IS_SOCKET_ERROR(bind(d->s, sa, sa_size))){
295 char a_str[256];
296 size_t len;
298 krb5_print_address (a, a_str, sizeof(a_str), &len);
299 krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port));
300 rk_closesocket(d->s);
301 d->s = rk_INVALID_SOCKET;
302 return;
304 if(type == SOCK_STREAM && rk_IS_SOCKET_ERROR(listen(d->s, SOMAXCONN))){
305 char a_str[256];
306 size_t len;
308 krb5_print_address (a, a_str, sizeof(a_str), &len);
309 krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port));
310 rk_closesocket(d->s);
311 d->s = rk_INVALID_SOCKET;
312 return;
317 * Allocate descriptors for all the sockets that we should listen on
318 * and return the number of them.
321 static int
322 init_sockets(krb5_context context,
323 krb5_kdc_configuration *config,
324 struct descr **desc)
326 krb5_error_code ret;
327 int i, j;
328 struct descr *d;
329 int num = 0;
330 krb5_addresses addresses;
332 if (explicit_addresses.len) {
333 addresses = explicit_addresses;
334 } else {
335 ret = krb5_get_all_server_addrs (context, &addresses);
336 if (ret)
337 krb5_err (context, 1, ret, "krb5_get_all_server_addrs");
339 parse_ports(context, config, port_str);
340 d = malloc(addresses.len * num_ports * sizeof(*d));
341 if (d == NULL)
342 krb5_errx(context, 1, "malloc(%lu) failed",
343 (unsigned long)num_ports * sizeof(*d));
345 for (i = 0; i < num_ports; i++){
346 for (j = 0; j < addresses.len; ++j) {
347 init_socket(context, config, &d[num], &addresses.val[j],
348 ports[i].family, ports[i].type, ports[i].port);
349 if(d[num].s != rk_INVALID_SOCKET){
350 char a_str[80];
351 size_t len;
353 krb5_print_address (&addresses.val[j], a_str,
354 sizeof(a_str), &len);
356 kdc_log(context, config, 5, "listening on %s port %u/%s",
357 a_str,
358 ntohs(ports[i].port),
359 (ports[i].type == SOCK_STREAM) ? "tcp" : "udp");
360 /* XXX */
361 num++;
365 krb5_free_addresses (context, &addresses);
366 d = realloc(d, num * sizeof(*d));
367 if (d == NULL && num != 0)
368 krb5_errx(context, 1, "realloc(%lu) failed",
369 (unsigned long)num * sizeof(*d));
370 reinit_descrs (d, num);
371 *desc = d;
372 return num;
379 static const char *
380 descr_type(struct descr *d)
382 if (d->type == SOCK_DGRAM)
383 return "udp";
384 else if (d->type == SOCK_STREAM)
385 return "tcp";
386 return "unknown";
389 static void
390 addr_to_string(krb5_context context,
391 struct sockaddr *addr, size_t addr_len, char *str, size_t len)
393 krb5_address a;
394 if(krb5_sockaddr2address(context, addr, &a) == 0) {
395 if(krb5_print_address(&a, str, len, &len) == 0) {
396 krb5_free_address(context, &a);
397 return;
399 krb5_free_address(context, &a);
401 snprintf(str, len, "<family=%d>", addr->sa_family);
408 static void
409 send_reply(krb5_context context,
410 krb5_kdc_configuration *config,
411 krb5_boolean prependlength,
412 struct descr *d,
413 krb5_data *reply)
415 kdc_log(context, config, 5,
416 "sending %lu bytes to %s", (unsigned long)reply->length,
417 d->addr_string);
418 if(prependlength){
419 unsigned char l[4];
420 l[0] = (reply->length >> 24) & 0xff;
421 l[1] = (reply->length >> 16) & 0xff;
422 l[2] = (reply->length >> 8) & 0xff;
423 l[3] = reply->length & 0xff;
424 if(rk_IS_SOCKET_ERROR(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len))) {
425 kdc_log (context, config,
426 0, "sendto(%s): %s", d->addr_string,
427 strerror(rk_SOCK_ERRNO));
428 return;
431 if(rk_IS_SOCKET_ERROR(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len))) {
432 kdc_log (context, config, 0, "sendto(%s): %s", d->addr_string,
433 strerror(rk_SOCK_ERRNO));
434 return;
439 * Handle the request in `buf, len' to socket `d'
442 static void
443 do_request(krb5_context context,
444 krb5_kdc_configuration *config,
445 void *buf, size_t len, krb5_boolean prependlength,
446 struct descr *d)
448 krb5_error_code ret;
449 krb5_data reply;
450 int datagram_reply = (d->type == SOCK_DGRAM);
452 krb5_kdc_update_time(NULL);
454 krb5_data_zero(&reply);
455 ret = krb5_kdc_process_request(context, config,
456 buf, len, &reply, &prependlength,
457 d->addr_string, d->sa,
458 datagram_reply);
459 if(request_log)
460 krb5_kdc_save_request(context, request_log, buf, len, &reply, d->sa);
461 if(reply.length){
462 send_reply(context, config, prependlength, d, &reply);
463 krb5_data_free(&reply);
465 if(ret)
466 kdc_log(context, config, 0,
467 "Failed processing %lu byte request from %s",
468 (unsigned long)len, d->addr_string);
472 * Handle incoming data to the UDP socket in `d'
475 static void
476 handle_udp(krb5_context context,
477 krb5_kdc_configuration *config,
478 struct descr *d)
480 unsigned char *buf;
481 int n;
483 buf = malloc(max_request_udp);
484 if(buf == NULL){
485 kdc_log(context, config, 0, "Failed to allocate %lu bytes", (unsigned long)max_request_udp);
486 return;
489 d->sock_len = sizeof(d->__ss);
490 n = recvfrom(d->s, buf, max_request_udp, 0, d->sa, &d->sock_len);
491 if(rk_IS_SOCKET_ERROR(n))
492 krb5_warn(context, rk_SOCK_ERRNO, "recvfrom");
493 else {
494 addr_to_string (context, d->sa, d->sock_len,
495 d->addr_string, sizeof(d->addr_string));
496 if (n == max_request_udp) {
497 krb5_data data;
498 krb5_warn(context, errno,
499 "recvfrom: truncated packet from %s, asking for TCP",
500 d->addr_string);
501 krb5_mk_error(context,
502 KRB5KRB_ERR_RESPONSE_TOO_BIG,
503 NULL,
504 NULL,
505 NULL,
506 NULL,
507 NULL,
508 NULL,
509 &data);
510 send_reply(context, config, FALSE, d, &data);
511 krb5_data_free(&data);
512 } else {
513 do_request(context, config, buf, n, FALSE, d);
516 free (buf);
519 static void
520 clear_descr(struct descr *d)
522 if(d->buf)
523 memset(d->buf, 0, d->size);
524 d->len = 0;
525 if(d->s != rk_INVALID_SOCKET)
526 rk_closesocket(d->s);
527 d->s = rk_INVALID_SOCKET;
531 /* remove HTTP %-quoting from buf */
532 static int
533 de_http(char *buf)
535 unsigned char *p, *q;
536 for(p = q = (unsigned char *)buf; *p; p++, q++) {
537 if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
538 unsigned int x;
539 if(sscanf((char *)p + 1, "%2x", &x) != 1)
540 return -1;
541 *q = x;
542 p += 2;
543 } else
544 *q = *p;
546 *q = '\0';
547 return 0;
550 #define TCP_TIMEOUT 4
553 * accept a new TCP connection on `d[parent]' and store it in `d[child]'
556 static void
557 add_new_tcp (krb5_context context,
558 krb5_kdc_configuration *config,
559 struct descr *d, int parent, int child)
561 krb5_socket_t s;
563 if (child == -1)
564 return;
566 d[child].sock_len = sizeof(d[child].__ss);
567 s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
568 if(rk_IS_BAD_SOCKET(s)) {
569 krb5_warn(context, rk_SOCK_ERRNO, "accept");
570 return;
573 #ifdef FD_SETSIZE
574 if (s >= FD_SETSIZE) {
575 krb5_warnx(context, "socket FD too large");
576 rk_closesocket (s);
577 return;
579 #endif
581 d[child].s = s;
582 d[child].timeout = time(NULL) + TCP_TIMEOUT;
583 d[child].type = SOCK_STREAM;
584 addr_to_string (context,
585 d[child].sa, d[child].sock_len,
586 d[child].addr_string, sizeof(d[child].addr_string));
590 * Grow `d' to handle at least `n'.
591 * Return != 0 if fails
594 static int
595 grow_descr (krb5_context context,
596 krb5_kdc_configuration *config,
597 struct descr *d, size_t n)
599 if (d->size - d->len < n) {
600 unsigned char *tmp;
601 size_t grow;
603 grow = max(1024, d->len + n);
604 if (d->size + grow > max_request_tcp) {
605 kdc_log(context, config, 0, "Request exceeds max request size (%lu bytes).",
606 (unsigned long)d->size + grow);
607 clear_descr(d);
608 return -1;
610 tmp = realloc (d->buf, d->size + grow);
611 if (tmp == NULL) {
612 kdc_log(context, config, 0, "Failed to re-allocate %lu bytes.",
613 (unsigned long)d->size + grow);
614 clear_descr(d);
615 return -1;
617 d->size += grow;
618 d->buf = tmp;
620 return 0;
624 * Try to handle the TCP data at `d->buf, d->len'.
625 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
628 static int
629 handle_vanilla_tcp (krb5_context context,
630 krb5_kdc_configuration *config,
631 struct descr *d)
633 krb5_storage *sp;
634 uint32_t len;
636 sp = krb5_storage_from_mem(d->buf, d->len);
637 if (sp == NULL) {
638 kdc_log (context, config, 0, "krb5_storage_from_mem failed");
639 return -1;
641 krb5_ret_uint32(sp, &len);
642 krb5_storage_free(sp);
643 if(d->len - 4 >= len) {
644 memmove(d->buf, d->buf + 4, d->len - 4);
645 d->len -= 4;
646 return 1;
648 return 0;
652 * Try to handle the TCP/HTTP data at `d->buf, d->len'.
653 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
656 static int
657 handle_http_tcp (krb5_context context,
658 krb5_kdc_configuration *config,
659 struct descr *d)
661 char *s, *p, *t;
662 void *data;
663 char *proto;
664 int len;
666 s = (char *)d->buf;
668 /* If its a multi line query, truncate off the first line */
669 p = strstr(s, "\r\n");
670 if (p)
671 *p = 0;
673 p = NULL;
674 t = strtok_r(s, " \t", &p);
675 if (t == NULL) {
676 kdc_log(context, config, 0,
677 "Missing HTTP operand (GET) request from %s", d->addr_string);
678 return -1;
681 t = strtok_r(NULL, " \t", &p);
682 if(t == NULL) {
683 kdc_log(context, config, 0,
684 "Missing HTTP GET data in request from %s", d->addr_string);
685 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 #ifndef NO_LIMIT_FD_SETSIZE
881 if(max_fd < d[i].s)
882 max_fd = d[i].s;
883 #ifdef FD_SETSIZE
884 if (max_fd >= FD_SETSIZE)
885 krb5_errx(context, 1, "fd too large");
886 #endif
887 #endif
888 FD_SET(d[i].s, &fds);
889 } else if(min_free < 0 || i < min_free)
890 min_free = i;
892 if(min_free == -1){
893 struct descr *tmp;
894 tmp = realloc(d, (ndescr + 4) * sizeof(*d));
895 if(tmp == NULL)
896 krb5_warnx(context, "No memory");
897 else {
898 d = tmp;
899 reinit_descrs (d, ndescr);
900 memset(d + ndescr, 0, 4 * sizeof(*d));
901 for(i = ndescr; i < ndescr + 4; i++)
902 init_descr (&d[i]);
903 min_free = ndescr;
904 ndescr += 4;
908 tmout.tv_sec = TCP_TIMEOUT;
909 tmout.tv_usec = 0;
910 switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
911 case 0:
912 break;
913 case -1:
914 if (errno != EINTR)
915 krb5_warn(context, rk_SOCK_ERRNO, "select");
916 break;
917 default:
918 for(i = 0; i < ndescr; i++)
919 if(!rk_IS_BAD_SOCKET(d[i].s) && FD_ISSET(d[i].s, &fds)) {
920 if(d[i].type == SOCK_DGRAM)
921 handle_udp(context, config, &d[i]);
922 else if(d[i].type == SOCK_STREAM)
923 handle_tcp(context, config, d, i, min_free);
927 if (0);
928 #ifdef SIGXCPU
929 else if(exit_flag == SIGXCPU)
930 kdc_log(context, config, 0, "CPU time limit exceeded");
931 #endif
932 else if(exit_flag == SIGINT || exit_flag == SIGTERM)
933 kdc_log(context, config, 0, "Terminated");
934 else
935 kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag);
936 free (d);