2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
36 RCSID("$Id: connect.c 22434 2008-01-14 09:21:37Z lha $");
38 /* Should we enable the HTTP hack? */
41 /* Log over requests to the KDC */
42 const char *request_log;
44 /* A string describing on what ports to listen */
47 krb5_addresses explicit_addresses;
49 size_t max_request; /* maximal size of a request */
52 * a tuple describing on what to listen
61 /* the current ones */
63 static struct port_desc *ports;
67 * add `family, port, protocol' to the list with duplicate suppresion.
71 add_port(krb5_context context,
72 int family, int port, const char *protocol)
77 if(strcmp(protocol, "udp") == 0)
79 else if(strcmp(protocol, "tcp") == 0)
83 for(i = 0; i < num_ports; i++){
84 if(ports[i].type == type
85 && ports[i].port == port
86 && ports[i].family == family)
89 ports = realloc(ports, (num_ports + 1) * sizeof(*ports));
91 krb5_err (context, 1, errno, "realloc");
92 ports[num_ports].family = family;
93 ports[num_ports].type = type;
94 ports[num_ports].port = port;
99 * add a triple but with service -> port lookup
100 * (this prints warnings for stuff that does not exist)
104 add_port_service(krb5_context context,
105 int family, const char *service, int port,
106 const char *protocol)
108 port = krb5_getportbyname (context, service, protocol, port);
109 add_port (context, family, port, protocol);
113 * add the port with service -> port lookup or string -> number
114 * (no warning is printed)
118 add_port_string (krb5_context context,
119 int family, const char *str, const char *protocol)
124 sp = roken_getservbyname (str, protocol);
130 port = htons(strtol(str, &end, 0));
134 add_port (context, family, port, protocol);
138 * add the standard collection of ports for `family'
142 add_standard_ports (krb5_context context,
143 krb5_kdc_configuration *config,
146 add_port_service(context, family, "kerberos", 88, "udp");
147 add_port_service(context, family, "kerberos", 88, "tcp");
148 add_port_service(context, family, "kerberos-sec", 88, "udp");
149 add_port_service(context, family, "kerberos-sec", 88, "tcp");
151 add_port_service(context, family, "http", 80, "tcp");
152 if(config->enable_524) {
153 add_port_service(context, family, "krb524", 4444, "udp");
154 add_port_service(context, family, "krb524", 4444, "tcp");
156 if(config->enable_v4) {
157 add_port_service(context, family, "kerberos-iv", 750, "udp");
158 add_port_service(context, family, "kerberos-iv", 750, "tcp");
160 if (config->enable_kaserver)
161 add_port_service(context, family, "afs3-kaserver", 7004, "udp");
162 if(config->enable_kx509) {
163 add_port_service(context, family, "kca_service", 9878, "udp");
164 add_port_service(context, family, "kca_service", 9878, "tcp");
170 * parse the set of space-delimited ports in `str' and add them.
171 * "+" => all the standard ones
172 * otherwise it's port|service[/protocol]
176 parse_ports(krb5_context context,
177 krb5_kdc_configuration *config,
182 char *str_copy = strdup (str);
184 p = strtok_r(str_copy, " \t", &pos);
186 if(strcmp(p, "+") == 0) {
188 add_standard_ports(context, config, AF_INET6);
190 add_standard_ports(context, config, AF_INET);
192 char *q = strchr(p, '/');
196 add_port_string(context, AF_INET6, p, q);
198 add_port_string(context, AF_INET, p, q);
201 add_port_string(context, AF_INET6, p, "udp");
202 add_port_string(context, AF_INET6, p, "tcp");
204 add_port_string(context, AF_INET, p, "udp");
205 add_port_string(context, AF_INET, p, "tcp");
209 p = strtok_r(NULL, " \t", &pos);
215 * every socket we listen on
226 struct sockaddr_storage __ss;
229 char addr_string[128];
233 init_descr(struct descr *d)
235 memset(d, 0, sizeof(*d));
236 d->sa = (struct sockaddr *)&d->__ss;
241 * re-initialize all `n' ->sa in `d'.
245 reinit_descrs (struct descr *d, int n)
249 for (i = 0; i < n; ++i)
250 d[i].sa = (struct sockaddr *)&d[i].__ss;
254 * Create the socket (family, type, port) in `d'
258 init_socket(krb5_context context,
259 krb5_kdc_configuration *config,
260 struct descr *d, krb5_address *a, int family, int type, int port)
263 struct sockaddr_storage __ss;
264 struct sockaddr *sa = (struct sockaddr *)&__ss;
265 krb5_socklen_t sa_size = sizeof(__ss);
269 ret = krb5_addr2sockaddr (context, a, sa, &sa_size, port);
271 krb5_warn(context, ret, "krb5_addr2sockaddr");
277 if (sa->sa_family != family)
280 d->s = socket(family, type, 0);
282 krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
286 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
289 setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
295 if(bind(d->s, sa, sa_size) < 0){
299 krb5_print_address (a, a_str, sizeof(a_str), &len);
300 krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port));
305 if(type == SOCK_STREAM && listen(d->s, SOMAXCONN) < 0){
309 krb5_print_address (a, a_str, sizeof(a_str), &len);
310 krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port));
318 * Allocate descriptors for all the sockets that we should listen on
319 * and return the number of them.
323 init_sockets(krb5_context context,
324 krb5_kdc_configuration *config,
331 krb5_addresses addresses;
333 if (explicit_addresses.len) {
334 addresses = explicit_addresses;
336 ret = krb5_get_all_server_addrs (context, &addresses);
338 krb5_err (context, 1, ret, "krb5_get_all_server_addrs");
340 parse_ports(context, config, port_str);
341 d = malloc(addresses.len * num_ports * sizeof(*d));
343 krb5_errx(context, 1, "malloc(%lu) failed",
344 (unsigned long)num_ports * sizeof(*d));
346 for (i = 0; i < num_ports; i++){
347 for (j = 0; j < addresses.len; ++j) {
348 init_socket(context, config, &d[num], &addresses.val[j],
349 ports[i].family, ports[i].type, ports[i].port);
354 krb5_print_address (&addresses.val[j], a_str,
355 sizeof(a_str), &len);
357 kdc_log(context, config, 5, "listening on %s port %u/%s",
359 ntohs(ports[i].port),
360 (ports[i].type == SOCK_STREAM) ? "tcp" : "udp");
366 krb5_free_addresses (context, &addresses);
367 d = realloc(d, num * sizeof(*d));
368 if (d == NULL && num != 0)
369 krb5_errx(context, 1, "realloc(%lu) failed",
370 (unsigned long)num * sizeof(*d));
371 reinit_descrs (d, num);
381 descr_type(struct descr *d)
383 if (d->type == SOCK_DGRAM)
385 else if (d->type == SOCK_STREAM)
391 addr_to_string(krb5_context context,
392 struct sockaddr *addr, size_t addr_len, char *str, size_t len)
395 if(krb5_sockaddr2address(context, addr, &a) == 0) {
396 if(krb5_print_address(&a, str, len, &len) == 0) {
397 krb5_free_address(context, &a);
400 krb5_free_address(context, &a);
402 snprintf(str, len, "<family=%d>", addr->sa_family);
410 send_reply(krb5_context context,
411 krb5_kdc_configuration *config,
412 krb5_boolean prependlength,
416 kdc_log(context, config, 5,
417 "sending %lu bytes to %s", (unsigned long)reply->length,
421 l[0] = (reply->length >> 24) & 0xff;
422 l[1] = (reply->length >> 16) & 0xff;
423 l[2] = (reply->length >> 8) & 0xff;
424 l[3] = reply->length & 0xff;
425 if(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len) < 0) {
426 kdc_log (context, config,
427 0, "sendto(%s): %s", d->addr_string, strerror(errno));
431 if(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len) < 0) {
432 kdc_log (context, config,
433 0, "sendto(%s): %s", d->addr_string, strerror(errno));
439 * Handle the request in `buf, len' to socket `d'
443 do_request(krb5_context context,
444 krb5_kdc_configuration *config,
445 void *buf, size_t len, krb5_boolean prependlength,
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,
460 krb5_kdc_save_request(context, request_log, buf, len, &reply, d->sa);
462 send_reply(context, config, prependlength, d, &reply);
463 krb5_data_free(&reply);
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'
476 handle_udp(krb5_context context,
477 krb5_kdc_configuration *config,
483 buf = malloc(max_request);
485 kdc_log(context, config, 0, "Failed to allocate %lu bytes", (unsigned long)max_request);
489 d->sock_len = sizeof(d->__ss);
490 n = recvfrom(d->s, buf, max_request, 0, d->sa, &d->sock_len);
492 krb5_warn(context, errno, "recvfrom");
494 addr_to_string (context, d->sa, d->sock_len,
495 d->addr_string, sizeof(d->addr_string));
496 do_request(context, config, buf, n, FALSE, d);
502 clear_descr(struct descr *d)
505 memset(d->buf, 0, d->size);
513 /* remove HTTP %-quoting from buf */
517 unsigned char *p, *q;
518 for(p = q = (unsigned char *)buf; *p; p++, q++) {
519 if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
521 if(sscanf((char *)p + 1, "%2x", &x) != 1)
532 #define TCP_TIMEOUT 4
535 * accept a new TCP connection on `d[parent]' and store it in `d[child]'
539 add_new_tcp (krb5_context context,
540 krb5_kdc_configuration *config,
541 struct descr *d, int parent, int child)
548 d[child].sock_len = sizeof(d[child].__ss);
549 s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
551 krb5_warn(context, errno, "accept");
555 if (s >= FD_SETSIZE) {
556 krb5_warnx(context, "socket FD too large");
562 d[child].timeout = time(NULL) + TCP_TIMEOUT;
563 d[child].type = SOCK_STREAM;
564 addr_to_string (context,
565 d[child].sa, d[child].sock_len,
566 d[child].addr_string, sizeof(d[child].addr_string));
570 * Grow `d' to handle at least `n'.
571 * Return != 0 if fails
575 grow_descr (krb5_context context,
576 krb5_kdc_configuration *config,
577 struct descr *d, size_t n)
579 if (d->size - d->len < n) {
583 grow = max(1024, d->len + n);
584 if (d->size + grow > max_request) {
585 kdc_log(context, config, 0, "Request exceeds max request size (%lu bytes).",
586 (unsigned long)d->size + grow);
590 tmp = realloc (d->buf, d->size + grow);
592 kdc_log(context, config, 0, "Failed to re-allocate %lu bytes.",
593 (unsigned long)d->size + grow);
604 * Try to handle the TCP data at `d->buf, d->len'.
605 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
609 handle_vanilla_tcp (krb5_context context,
610 krb5_kdc_configuration *config,
616 sp = krb5_storage_from_mem(d->buf, d->len);
618 kdc_log (context, config, 0, "krb5_storage_from_mem failed");
621 krb5_ret_uint32(sp, &len);
622 krb5_storage_free(sp);
623 if(d->len - 4 >= len) {
624 memmove(d->buf, d->buf + 4, d->len - 4);
632 * Try to handle the TCP/HTTP data at `d->buf, d->len'.
633 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
637 handle_http_tcp (krb5_context context,
638 krb5_kdc_configuration *config,
648 p = strstr(s, "\r\n");
650 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
656 t = strtok_r(s, " \t", &p);
658 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
661 t = strtok_r(NULL, " \t", &p);
663 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
666 data = malloc(strlen(t));
668 kdc_log(context, config, 0, "Failed to allocate %lu bytes",
669 (unsigned long)strlen(t));
674 if(de_http(t) != 0) {
675 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
676 kdc_log(context, config, 5, "HTTP request: %s", t);
680 proto = strtok_r(NULL, " \t", &p);
682 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
686 len = base64_decode(t, data);
690 "Server: Heimdal/" VERSION "\r\n"
691 "Cache-Control: no-cache\r\n"
692 "Pragma: no-cache\r\n"
693 "Content-type: text/html\r\n"
694 "Content-transfer-encoding: 8bit\r\n\r\n"
695 "<TITLE>404 Not found</TITLE>\r\n"
696 "<H1>404 Not found</H1>\r\n"
697 "That page doesn't exist, maybe you are looking for "
698 "<A HREF=\"http://www.h5l.org/\">Heimdal</A>?\r\n";
699 kdc_log(context, config, 0, "HTTP request from %s is non KDC request", d->addr_string);
700 kdc_log(context, config, 5, "HTTP request: %s", t);
702 if (write(d->s, proto, strlen(proto)) < 0) {
703 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
704 d->addr_string, strerror(errno));
707 if (write(d->s, msg, strlen(msg)) < 0) {
708 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
709 d->addr_string, strerror(errno));
717 "Server: Heimdal/" VERSION "\r\n"
718 "Cache-Control: no-cache\r\n"
719 "Pragma: no-cache\r\n"
720 "Content-type: application/octet-stream\r\n"
721 "Content-transfer-encoding: binary\r\n\r\n";
722 if (write(d->s, proto, strlen(proto)) < 0) {
723 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
724 d->addr_string, strerror(errno));
727 if (write(d->s, msg, strlen(msg)) < 0) {
728 kdc_log(context, config, 0, "HTTP write failed: %s: %s",
729 d->addr_string, strerror(errno));
733 memcpy(d->buf, data, len);
740 * Handle incoming data to the TCP socket in `d[index]'
744 handle_tcp(krb5_context context,
745 krb5_kdc_configuration *config,
746 struct descr *d, int idx, int min_free)
748 unsigned char buf[1024];
752 if (d[idx].timeout == 0) {
753 add_new_tcp (context, config, d, idx, min_free);
757 n = recvfrom(d[idx].s, buf, sizeof(buf), 0, NULL, NULL);
759 krb5_warn(context, errno, "recvfrom failed from %s to %s/%d",
760 d[idx].addr_string, descr_type(d + idx),
764 krb5_warnx(context, "connection closed before end of data after %lu "
765 "bytes from %s to %s/%d", (unsigned long)d[idx].len,
766 d[idx].addr_string, descr_type(d + idx),
768 clear_descr (d + idx);
771 if (grow_descr (context, config, &d[idx], n))
773 memcpy(d[idx].buf + d[idx].len, buf, n);
775 if(d[idx].len > 4 && d[idx].buf[0] == 0) {
776 ret = handle_vanilla_tcp (context, config, &d[idx]);
777 } else if(enable_http &&
779 strncmp((char *)d[idx].buf, "GET ", 4) == 0 &&
780 strncmp((char *)d[idx].buf + d[idx].len - 4,
781 "\r\n\r\n", 4) == 0) {
782 ret = handle_http_tcp (context, config, &d[idx]);
784 clear_descr (d + idx);
785 } else if (d[idx].len > 4) {
786 kdc_log (context, config,
787 0, "TCP data of strange type from %s to %s/%d",
788 d[idx].addr_string, descr_type(d + idx),
790 if (d[idx].buf[0] & 0x80) {
793 kdc_log (context, config, 0, "TCP extension not supported");
795 ret = krb5_mk_error(context,
796 KRB5KRB_ERR_FIELD_TOOLONG,
805 send_reply(context, config, TRUE, d + idx, &reply);
806 krb5_data_free(&reply);
809 clear_descr(d + idx);
815 do_request(context, config,
816 d[idx].buf, d[idx].len, TRUE, &d[idx]);
817 clear_descr(d + idx);
822 loop(krb5_context context,
823 krb5_kdc_configuration *config)
828 ndescr = init_sockets(context, config, &d);
830 krb5_errx(context, 1, "No sockets!");
831 kdc_log(context, config, 0, "KDC started");
832 while(exit_flag == 0){
833 struct timeval tmout;
840 for(i = 0; i < ndescr; i++) {
842 if(d[i].type == SOCK_STREAM &&
843 d[i].timeout && d[i].timeout < time(NULL)) {
844 kdc_log(context, config, 1,
845 "TCP-connection from %s expired after %lu bytes",
846 d[i].addr_string, (unsigned long)d[i].len);
852 if (max_fd >= FD_SETSIZE)
853 krb5_errx(context, 1, "fd too large");
854 FD_SET(d[i].s, &fds);
855 } else if(min_free < 0 || i < min_free)
860 tmp = realloc(d, (ndescr + 4) * sizeof(*d));
862 krb5_warnx(context, "No memory");
865 reinit_descrs (d, ndescr);
866 memset(d + ndescr, 0, 4 * sizeof(*d));
867 for(i = ndescr; i < ndescr + 4; i++)
874 tmout.tv_sec = TCP_TIMEOUT;
876 switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
881 krb5_warn(context, errno, "select");
884 for(i = 0; i < ndescr; i++)
885 if(d[i].s >= 0 && FD_ISSET(d[i].s, &fds)) {
886 if(d[i].type == SOCK_DGRAM)
887 handle_udp(context, config, &d[i]);
888 else if(d[i].type == SOCK_STREAM)
889 handle_tcp(context, config, d, i, min_free);
893 if(exit_flag == SIGXCPU)
894 kdc_log(context, config, 0, "CPU time limit exceeded");
895 else if(exit_flag == SIGINT || exit_flag == SIGTERM)
896 kdc_log(context, config, 0, "Terminated");
898 kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag);