Windows: Wix3 use ForceDeleteOnUninstall
[heimdal.git] / kdc / connect.c
blob9083b85f128e6a2abe8c3c784be9fc7c186a0217
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"
37 * a tuple describing on what to listen
40 struct port_desc{
41 int family;
42 int type;
43 int port;
46 /* the current ones */
48 static struct port_desc *ports;
49 static size_t num_ports;
52 * add `family, port, protocol' to the list with duplicate suppresion.
55 static void
56 add_port(krb5_context context,
57 int family, int port, const char *protocol)
59 int type;
60 size_t i;
62 if(strcmp(protocol, "udp") == 0)
63 type = SOCK_DGRAM;
64 else if(strcmp(protocol, "tcp") == 0)
65 type = SOCK_STREAM;
66 else
67 return;
68 for(i = 0; i < num_ports; i++){
69 if(ports[i].type == type
70 && ports[i].port == port
71 && ports[i].family == family)
72 return;
74 ports = realloc(ports, (num_ports + 1) * sizeof(*ports));
75 if (ports == NULL)
76 krb5_err (context, 1, errno, "realloc");
77 ports[num_ports].family = family;
78 ports[num_ports].type = type;
79 ports[num_ports].port = port;
80 num_ports++;
84 * add a triple but with service -> port lookup
85 * (this prints warnings for stuff that does not exist)
88 static void
89 add_port_service(krb5_context context,
90 int family, const char *service, int port,
91 const char *protocol)
93 port = krb5_getportbyname (context, service, protocol, port);
94 add_port (context, family, port, protocol);
98 * add the port with service -> port lookup or string -> number
99 * (no warning is printed)
102 static void
103 add_port_string (krb5_context context,
104 int family, const char *str, const char *protocol)
106 struct servent *sp;
107 int port;
109 sp = roken_getservbyname (str, protocol);
110 if (sp != NULL) {
111 port = sp->s_port;
112 } else {
113 char *end;
115 port = htons(strtol(str, &end, 0));
116 if (end == str)
117 return;
119 add_port (context, family, port, protocol);
123 * add the standard collection of ports for `family'
126 static void
127 add_standard_ports (krb5_context context,
128 krb5_kdc_configuration *config,
129 int family)
131 add_port_service(context, family, "kerberos", 88, "udp");
132 add_port_service(context, family, "kerberos", 88, "tcp");
133 add_port_service(context, family, "kerberos-sec", 88, "udp");
134 add_port_service(context, family, "kerberos-sec", 88, "tcp");
135 if(enable_http)
136 add_port_service(context, family, "http", 80, "tcp");
137 if(config->enable_kx509) {
138 add_port_service(context, family, "kca_service", 9878, "udp");
139 add_port_service(context, family, "kca_service", 9878, "tcp");
145 * parse the set of space-delimited ports in `str' and add them.
146 * "+" => all the standard ones
147 * otherwise it's port|service[/protocol]
150 static void
151 parse_ports(krb5_context context,
152 krb5_kdc_configuration *config,
153 const char *str)
155 char *pos = NULL;
156 char *p;
157 char *str_copy = strdup (str);
159 p = strtok_r(str_copy, " \t", &pos);
160 while(p != NULL) {
161 if(strcmp(p, "+") == 0) {
162 #ifdef HAVE_IPV6
163 add_standard_ports(context, config, AF_INET6);
164 #endif
165 add_standard_ports(context, config, AF_INET);
166 } else {
167 char *q = strchr(p, '/');
168 if(q){
169 *q++ = 0;
170 #ifdef HAVE_IPV6
171 add_port_string(context, AF_INET6, p, q);
172 #endif
173 add_port_string(context, AF_INET, p, q);
174 }else {
175 #ifdef HAVE_IPV6
176 add_port_string(context, AF_INET6, p, "udp");
177 add_port_string(context, AF_INET6, p, "tcp");
178 #endif
179 add_port_string(context, AF_INET, p, "udp");
180 add_port_string(context, AF_INET, p, "tcp");
184 p = strtok_r(NULL, " \t", &pos);
186 free (str_copy);
190 * every socket we listen on
193 struct descr {
194 krb5_socket_t s;
195 int type;
196 int port;
197 unsigned char *buf;
198 size_t size;
199 size_t len;
200 time_t timeout;
201 struct sockaddr_storage __ss;
202 struct sockaddr *sa;
203 socklen_t sock_len;
204 char addr_string[128];
207 static void
208 init_descr(struct descr *d)
210 memset(d, 0, sizeof(*d));
211 d->sa = (struct sockaddr *)&d->__ss;
212 d->s = rk_INVALID_SOCKET;
216 * re-initialize all `n' ->sa in `d'.
219 static void
220 reinit_descrs (struct descr *d, int n)
222 int i;
224 for (i = 0; i < n; ++i)
225 d[i].sa = (struct sockaddr *)&d[i].__ss;
229 * Create the socket (family, type, port) in `d'
232 static void
233 init_socket(krb5_context context,
234 krb5_kdc_configuration *config,
235 struct descr *d, krb5_address *a, int family, int type, int port)
237 krb5_error_code ret;
238 struct sockaddr_storage __ss;
239 struct sockaddr *sa = (struct sockaddr *)&__ss;
240 krb5_socklen_t sa_size = sizeof(__ss);
242 init_descr (d);
244 ret = krb5_addr2sockaddr (context, a, sa, &sa_size, port);
245 if (ret) {
246 krb5_warn(context, ret, "krb5_addr2sockaddr");
247 rk_closesocket(d->s);
248 d->s = rk_INVALID_SOCKET;
249 return;
252 if (sa->sa_family != family)
253 return;
255 d->s = socket(family, type, 0);
256 if(rk_IS_BAD_SOCKET(d->s)){
257 krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
258 d->s = rk_INVALID_SOCKET;
259 return;
261 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
263 int one = 1;
264 setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
266 #endif
267 d->type = type;
268 d->port = port;
270 if(rk_IS_SOCKET_ERROR(bind(d->s, sa, sa_size))){
271 char a_str[256];
272 size_t len;
274 krb5_print_address (a, a_str, sizeof(a_str), &len);
275 krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port));
276 rk_closesocket(d->s);
277 d->s = rk_INVALID_SOCKET;
278 return;
280 if(type == SOCK_STREAM && rk_IS_SOCKET_ERROR(listen(d->s, SOMAXCONN))){
281 char a_str[256];
282 size_t len;
284 krb5_print_address (a, a_str, sizeof(a_str), &len);
285 krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port));
286 rk_closesocket(d->s);
287 d->s = rk_INVALID_SOCKET;
288 return;
293 * Allocate descriptors for all the sockets that we should listen on
294 * and return the number of them.
297 static int
298 init_sockets(krb5_context context,
299 krb5_kdc_configuration *config,
300 struct descr **desc)
302 krb5_error_code ret;
303 size_t i, j;
304 struct descr *d;
305 int num = 0;
306 krb5_addresses addresses;
308 if (explicit_addresses.len) {
309 addresses = explicit_addresses;
310 } else {
311 ret = krb5_get_all_server_addrs (context, &addresses);
312 if (ret)
313 krb5_err (context, 1, ret, "krb5_get_all_server_addrs");
315 parse_ports(context, config, port_str);
316 d = malloc(addresses.len * num_ports * sizeof(*d));
317 if (d == NULL)
318 krb5_errx(context, 1, "malloc(%lu) failed",
319 (unsigned long)num_ports * sizeof(*d));
321 for (i = 0; i < num_ports; i++){
322 for (j = 0; j < addresses.len; ++j) {
323 init_socket(context, config, &d[num], &addresses.val[j],
324 ports[i].family, ports[i].type, ports[i].port);
325 if(d[num].s != rk_INVALID_SOCKET){
326 char a_str[80];
327 size_t len;
329 krb5_print_address (&addresses.val[j], a_str,
330 sizeof(a_str), &len);
332 kdc_log(context, config, 5, "listening on %s port %u/%s",
333 a_str,
334 ntohs(ports[i].port),
335 (ports[i].type == SOCK_STREAM) ? "tcp" : "udp");
336 /* XXX */
337 num++;
341 krb5_free_addresses (context, &addresses);
342 d = realloc(d, num * sizeof(*d));
343 if (d == NULL && num != 0)
344 krb5_errx(context, 1, "realloc(%lu) failed",
345 (unsigned long)num * sizeof(*d));
346 reinit_descrs (d, num);
347 *desc = d;
348 return num;
355 static const char *
356 descr_type(struct descr *d)
358 if (d->type == SOCK_DGRAM)
359 return "udp";
360 else if (d->type == SOCK_STREAM)
361 return "tcp";
362 return "unknown";
365 static void
366 addr_to_string(krb5_context context,
367 struct sockaddr *addr, size_t addr_len, char *str, size_t len)
369 krb5_address a;
370 if(krb5_sockaddr2address(context, addr, &a) == 0) {
371 if(krb5_print_address(&a, str, len, &len) == 0) {
372 krb5_free_address(context, &a);
373 return;
375 krb5_free_address(context, &a);
377 snprintf(str, len, "<family=%d>", addr->sa_family);
384 static void
385 send_reply(krb5_context context,
386 krb5_kdc_configuration *config,
387 krb5_boolean prependlength,
388 struct descr *d,
389 krb5_data *reply)
391 kdc_log(context, config, 5,
392 "sending %lu bytes to %s", (unsigned long)reply->length,
393 d->addr_string);
394 if(prependlength){
395 unsigned char l[4];
396 l[0] = (reply->length >> 24) & 0xff;
397 l[1] = (reply->length >> 16) & 0xff;
398 l[2] = (reply->length >> 8) & 0xff;
399 l[3] = reply->length & 0xff;
400 if(rk_IS_SOCKET_ERROR(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len))) {
401 kdc_log (context, config,
402 0, "sendto(%s): %s", d->addr_string,
403 strerror(rk_SOCK_ERRNO));
404 return;
407 if(rk_IS_SOCKET_ERROR(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len))) {
408 kdc_log (context, config, 0, "sendto(%s): %s", d->addr_string,
409 strerror(rk_SOCK_ERRNO));
410 return;
415 * Handle the request in `buf, len' to socket `d'
418 static void
419 do_request(krb5_context context,
420 krb5_kdc_configuration *config,
421 void *buf, size_t len, krb5_boolean prependlength,
422 struct descr *d)
424 krb5_error_code ret;
425 krb5_data reply;
426 int datagram_reply = (d->type == SOCK_DGRAM);
428 krb5_kdc_update_time(NULL);
430 krb5_data_zero(&reply);
431 ret = krb5_kdc_process_request(context, config,
432 buf, len, &reply, &prependlength,
433 d->addr_string, d->sa,
434 datagram_reply);
435 if(request_log)
436 krb5_kdc_save_request(context, request_log, buf, len, &reply, d->sa);
437 if(reply.length){
438 send_reply(context, config, prependlength, d, &reply);
439 krb5_data_free(&reply);
441 if(ret)
442 kdc_log(context, config, 0,
443 "Failed processing %lu byte request from %s",
444 (unsigned long)len, d->addr_string);
448 * Handle incoming data to the UDP socket in `d'
451 static void
452 handle_udp(krb5_context context,
453 krb5_kdc_configuration *config,
454 struct descr *d)
456 unsigned char *buf;
457 ssize_t n;
459 buf = malloc(max_request_udp);
460 if(buf == NULL){
461 kdc_log(context, config, 0, "Failed to allocate %lu bytes", (unsigned long)max_request_udp);
462 return;
465 d->sock_len = sizeof(d->__ss);
466 n = recvfrom(d->s, buf, max_request_udp, 0, d->sa, &d->sock_len);
467 if(rk_IS_SOCKET_ERROR(n))
468 krb5_warn(context, rk_SOCK_ERRNO, "recvfrom");
469 else {
470 addr_to_string (context, d->sa, d->sock_len,
471 d->addr_string, sizeof(d->addr_string));
472 if ((size_t)n == max_request_udp) {
473 krb5_data data;
474 krb5_warn(context, errno,
475 "recvfrom: truncated packet from %s, asking for TCP",
476 d->addr_string);
477 krb5_mk_error(context,
478 KRB5KRB_ERR_RESPONSE_TOO_BIG,
479 NULL,
480 NULL,
481 NULL,
482 NULL,
483 NULL,
484 NULL,
485 &data);
486 send_reply(context, config, FALSE, d, &data);
487 krb5_data_free(&data);
488 } else {
489 do_request(context, config, buf, n, FALSE, d);
492 free (buf);
495 static void
496 clear_descr(struct descr *d)
498 if(d->buf)
499 memset(d->buf, 0, d->size);
500 d->len = 0;
501 if(d->s != rk_INVALID_SOCKET)
502 rk_closesocket(d->s);
503 d->s = rk_INVALID_SOCKET;
507 /* remove HTTP %-quoting from buf */
508 static int
509 de_http(char *buf)
511 unsigned char *p, *q;
512 for(p = q = (unsigned char *)buf; *p; p++, q++) {
513 if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
514 unsigned int x;
515 if(sscanf((char *)p + 1, "%2x", &x) != 1)
516 return -1;
517 *q = x;
518 p += 2;
519 } else
520 *q = *p;
522 *q = '\0';
523 return 0;
526 #define TCP_TIMEOUT 4
529 * accept a new TCP connection on `d[parent]' and store it in `d[child]'
532 static void
533 add_new_tcp (krb5_context context,
534 krb5_kdc_configuration *config,
535 struct descr *d, int parent, int child)
537 krb5_socket_t s;
539 if (child == -1)
540 return;
542 d[child].sock_len = sizeof(d[child].__ss);
543 s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
544 if(rk_IS_BAD_SOCKET(s)) {
545 krb5_warn(context, rk_SOCK_ERRNO, "accept");
546 return;
549 #ifdef FD_SETSIZE
550 if (s >= FD_SETSIZE) {
551 krb5_warnx(context, "socket FD too large");
552 rk_closesocket (s);
553 return;
555 #endif
557 d[child].s = s;
558 d[child].timeout = time(NULL) + TCP_TIMEOUT;
559 d[child].type = SOCK_STREAM;
560 addr_to_string (context,
561 d[child].sa, d[child].sock_len,
562 d[child].addr_string, sizeof(d[child].addr_string));
566 * Grow `d' to handle at least `n'.
567 * Return != 0 if fails
570 static int
571 grow_descr (krb5_context context,
572 krb5_kdc_configuration *config,
573 struct descr *d, size_t n)
575 if (d->size - d->len < n) {
576 unsigned char *tmp;
577 size_t grow;
579 grow = max(1024, d->len + n);
580 if (d->size + grow > max_request_tcp) {
581 kdc_log(context, config, 0, "Request exceeds max request size (%lu bytes).",
582 (unsigned long)d->size + grow);
583 clear_descr(d);
584 return -1;
586 tmp = realloc (d->buf, d->size + grow);
587 if (tmp == NULL) {
588 kdc_log(context, config, 0, "Failed to re-allocate %lu bytes.",
589 (unsigned long)d->size + grow);
590 clear_descr(d);
591 return -1;
593 d->size += grow;
594 d->buf = tmp;
596 return 0;
600 * Try to handle the TCP data at `d->buf, d->len'.
601 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
604 static int
605 handle_vanilla_tcp (krb5_context context,
606 krb5_kdc_configuration *config,
607 struct descr *d)
609 krb5_storage *sp;
610 uint32_t len;
612 sp = krb5_storage_from_mem(d->buf, d->len);
613 if (sp == NULL) {
614 kdc_log (context, config, 0, "krb5_storage_from_mem failed");
615 return -1;
617 krb5_ret_uint32(sp, &len);
618 krb5_storage_free(sp);
619 if(d->len - 4 >= len) {
620 memmove(d->buf, d->buf + 4, d->len - 4);
621 d->len -= 4;
622 return 1;
624 return 0;
628 * Try to handle the TCP/HTTP data at `d->buf, d->len'.
629 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
632 static int
633 handle_http_tcp (krb5_context context,
634 krb5_kdc_configuration *config,
635 struct descr *d)
637 char *s, *p, *t;
638 void *data;
639 char *proto;
640 int len;
642 s = (char *)d->buf;
644 /* If its a multi line query, truncate off the first line */
645 p = strstr(s, "\r\n");
646 if (p)
647 *p = 0;
649 p = NULL;
650 t = strtok_r(s, " \t", &p);
651 if (t == NULL) {
652 kdc_log(context, config, 0,
653 "Missing HTTP operand (GET) request from %s", d->addr_string);
654 return -1;
657 t = strtok_r(NULL, " \t", &p);
658 if(t == NULL) {
659 kdc_log(context, config, 0,
660 "Missing HTTP GET data in request from %s", d->addr_string);
661 return -1;
664 data = malloc(strlen(t));
665 if (data == NULL) {
666 kdc_log(context, config, 0, "Failed to allocate %lu bytes",
667 (unsigned long)strlen(t));
668 return -1;
670 if(*t == '/')
671 t++;
672 if(de_http(t) != 0) {
673 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
674 kdc_log(context, config, 5, "HTTP request: %s", t);
675 free(data);
676 return -1;
678 proto = strtok_r(NULL, " \t", &p);
679 if (proto == NULL) {
680 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
681 free(data);
682 return -1;
684 len = base64_decode(t, data);
685 if(len <= 0){
686 const char *msg =
687 " 404 Not found\r\n"
688 "Server: Heimdal/" VERSION "\r\n"
689 "Cache-Control: no-cache\r\n"
690 "Pragma: no-cache\r\n"
691 "Content-type: text/html\r\n"
692 "Content-transfer-encoding: 8bit\r\n\r\n"
693 "<TITLE>404 Not found</TITLE>\r\n"
694 "<H1>404 Not found</H1>\r\n"
695 "That page doesn't exist, maybe you are looking for "
696 "<A HREF=\"http://www.h5l.org/\">Heimdal</A>?\r\n";
697 kdc_log(context, config, 0, "HTTP request from %s is non KDC request", d->addr_string);
698 kdc_log(context, config, 5, "HTTP request: %s", t);
699 free(data);
700 if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) {
701 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
702 d->addr_string, strerror(rk_SOCK_ERRNO));
703 return -1;
705 if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) {
706 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
707 d->addr_string, strerror(rk_SOCK_ERRNO));
708 return -1;
710 return -1;
713 const char *msg =
714 " 200 OK\r\n"
715 "Server: Heimdal/" VERSION "\r\n"
716 "Cache-Control: no-cache\r\n"
717 "Pragma: no-cache\r\n"
718 "Content-type: application/octet-stream\r\n"
719 "Content-transfer-encoding: binary\r\n\r\n";
720 if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) {
721 free(data);
722 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
723 d->addr_string, strerror(rk_SOCK_ERRNO));
724 return -1;
726 if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) {
727 free(data);
728 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
729 d->addr_string, strerror(rk_SOCK_ERRNO));
730 return -1;
733 if ((size_t)len > d->len)
734 len = d->len;
735 memcpy(d->buf, data, len);
736 d->len = len;
737 free(data);
738 return 1;
742 * Handle incoming data to the TCP socket in `d[index]'
745 static void
746 handle_tcp(krb5_context context,
747 krb5_kdc_configuration *config,
748 struct descr *d, int idx, int min_free)
750 unsigned char buf[1024];
751 int n;
752 int ret = 0;
754 if (d[idx].timeout == 0) {
755 add_new_tcp (context, config, d, idx, min_free);
756 return;
759 n = recvfrom(d[idx].s, buf, sizeof(buf), 0, NULL, NULL);
760 if(rk_IS_SOCKET_ERROR(n)){
761 krb5_warn(context, rk_SOCK_ERRNO, "recvfrom failed from %s to %s/%d",
762 d[idx].addr_string, descr_type(d + idx),
763 ntohs(d[idx].port));
764 return;
765 } else if (n == 0) {
766 krb5_warnx(context, "connection closed before end of data after %lu "
767 "bytes from %s to %s/%d", (unsigned long)d[idx].len,
768 d[idx].addr_string, descr_type(d + idx),
769 ntohs(d[idx].port));
770 clear_descr (d + idx);
771 return;
773 if (grow_descr (context, config, &d[idx], n))
774 return;
775 memcpy(d[idx].buf + d[idx].len, buf, n);
776 d[idx].len += n;
777 if(d[idx].len > 4 && d[idx].buf[0] == 0) {
778 ret = handle_vanilla_tcp (context, config, &d[idx]);
779 } else if(enable_http &&
780 d[idx].len >= 4 &&
781 strncmp((char *)d[idx].buf, "GET ", 4) == 0 &&
782 strncmp((char *)d[idx].buf + d[idx].len - 4,
783 "\r\n\r\n", 4) == 0) {
785 /* remove the trailing \r\n\r\n so the string is NUL terminated */
786 d[idx].buf[d[idx].len - 4] = '\0';
788 ret = handle_http_tcp (context, config, &d[idx]);
789 if (ret < 0)
790 clear_descr (d + idx);
791 } else if (d[idx].len > 4) {
792 kdc_log (context, config,
793 0, "TCP data of strange type from %s to %s/%d",
794 d[idx].addr_string, descr_type(d + idx),
795 ntohs(d[idx].port));
796 if (d[idx].buf[0] & 0x80) {
797 krb5_data reply;
799 kdc_log (context, config, 0, "TCP extension not supported");
801 ret = krb5_mk_error(context,
802 KRB5KRB_ERR_FIELD_TOOLONG,
803 NULL,
804 NULL,
805 NULL,
806 NULL,
807 NULL,
808 NULL,
809 &reply);
810 if (ret == 0) {
811 send_reply(context, config, TRUE, d + idx, &reply);
812 krb5_data_free(&reply);
815 clear_descr(d + idx);
816 return;
818 if (ret < 0)
819 return;
820 else if (ret == 1) {
821 do_request(context, config,
822 d[idx].buf, d[idx].len, TRUE, &d[idx]);
823 clear_descr(d + idx);
827 krb5_boolean
828 realloc_descrs(struct descr **d, unsigned int *ndescr)
830 struct descr *tmp;
831 size_t i;
833 tmp = realloc(*d, (*ndescr + 4) * sizeof(**d));
834 if(tmp == NULL)
835 return FALSE;
837 *d = tmp;
838 reinit_descrs (*d, *ndescr);
839 memset(*d + *ndescr, 0, 4 * sizeof(**d));
840 for(i = *ndescr; i < *ndescr + 4; i++)
841 init_descr (*d + i);
843 *ndescr += 4;
845 return TRUE;
849 next_min_free(krb5_context context, struct descr **d, unsigned int *ndescr)
851 size_t i;
852 int min_free;
854 for(i = 0; i < *ndescr; i++) {
855 int s = (*d + i)->s;
856 if(rk_IS_BAD_SOCKET(s))
857 return i;
860 min_free = *ndescr;
861 if(!realloc_descrs(d, ndescr)) {
862 min_free = -1;
863 krb5_warnx(context, "No memory");
866 return min_free;
869 void
870 loop(krb5_context context,
871 krb5_kdc_configuration *config)
873 struct descr *d;
874 unsigned int ndescr;
876 ndescr = init_sockets(context, config, &d);
877 if(ndescr <= 0)
878 krb5_errx(context, 1, "No sockets!");
879 kdc_log(context, config, 0, "KDC started");
880 while(exit_flag == 0){
881 struct timeval tmout;
882 fd_set fds;
883 int min_free = -1;
884 int max_fd = 0;
885 size_t i;
887 FD_ZERO(&fds);
888 for(i = 0; i < ndescr; i++) {
889 if(!rk_IS_BAD_SOCKET(d[i].s)){
890 if(d[i].type == SOCK_STREAM &&
891 d[i].timeout && d[i].timeout < time(NULL)) {
892 kdc_log(context, config, 1,
893 "TCP-connection from %s expired after %lu bytes",
894 d[i].addr_string, (unsigned long)d[i].len);
895 clear_descr(&d[i]);
896 continue;
898 #ifndef NO_LIMIT_FD_SETSIZE
899 if(max_fd < d[i].s)
900 max_fd = d[i].s;
901 #ifdef FD_SETSIZE
902 if (max_fd >= FD_SETSIZE)
903 krb5_errx(context, 1, "fd too large");
904 #endif
905 #endif
906 FD_SET(d[i].s, &fds);
910 tmout.tv_sec = TCP_TIMEOUT;
911 tmout.tv_usec = 0;
912 switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
913 case 0:
914 break;
915 case -1:
916 if (errno != EINTR)
917 krb5_warn(context, rk_SOCK_ERRNO, "select");
918 break;
919 default:
920 for(i = 0; i < ndescr; i++)
921 if(!rk_IS_BAD_SOCKET(d[i].s) && FD_ISSET(d[i].s, &fds)) {
922 min_free = next_min_free(context, &d, &ndescr);
924 if(d[i].type == SOCK_DGRAM)
925 handle_udp(context, config, &d[i]);
926 else if(d[i].type == SOCK_STREAM)
927 handle_tcp(context, config, d, i, min_free);
931 if (0);
932 #ifdef SIGXCPU
933 else if(exit_flag == SIGXCPU)
934 kdc_log(context, config, 0, "CPU time limit exceeded");
935 #endif
936 else if(exit_flag == SIGINT || exit_flag == SIGTERM)
937 kdc_log(context, config, 0, "Terminated");
938 else
939 kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag);
940 free (d);