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
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_udp
;
50 size_t max_request_tcp
;
53 * a tuple describing on what to listen
62 /* the current ones */
64 static struct port_desc
*ports
;
68 * add `family, port, protocol' to the list with duplicate suppresion.
72 add_port(krb5_context context
,
73 int family
, int port
, const char *protocol
)
78 if(strcmp(protocol
, "udp") == 0)
80 else if(strcmp(protocol
, "tcp") == 0)
84 for(i
= 0; i
< num_ports
; i
++){
85 if(ports
[i
].type
== type
86 && ports
[i
].port
== port
87 && ports
[i
].family
== family
)
90 ports
= realloc(ports
, (num_ports
+ 1) * sizeof(*ports
));
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
;
100 * add a triple but with service -> port lookup
101 * (this prints warnings for stuff that does not exist)
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)
119 add_port_string (krb5_context context
,
120 int family
, const char *str
, const char *protocol
)
125 sp
= roken_getservbyname (str
, protocol
);
131 port
= htons(strtol(str
, &end
, 0));
135 add_port (context
, family
, port
, protocol
);
139 * add the standard collection of ports for `family'
143 add_standard_ports (krb5_context context
,
144 krb5_kdc_configuration
*config
,
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");
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]
177 parse_ports(krb5_context context
,
178 krb5_kdc_configuration
*config
,
183 char *str_copy
= strdup (str
);
185 p
= strtok_r(str_copy
, " \t", &pos
);
187 if(strcmp(p
, "+") == 0) {
189 add_standard_ports(context
, config
, AF_INET6
);
191 add_standard_ports(context
, config
, AF_INET
);
193 char *q
= strchr(p
, '/');
197 add_port_string(context
, AF_INET6
, p
, q
);
199 add_port_string(context
, AF_INET
, p
, q
);
202 add_port_string(context
, AF_INET6
, p
, "udp");
203 add_port_string(context
, AF_INET6
, p
, "tcp");
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
);
216 * every socket we listen on
227 struct sockaddr_storage __ss
;
230 char addr_string
[128];
234 init_descr(struct descr
*d
)
236 memset(d
, 0, sizeof(*d
));
237 d
->sa
= (struct sockaddr
*)&d
->__ss
;
242 * re-initialize all `n' ->sa in `d'.
246 reinit_descrs (struct descr
*d
, int n
)
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'
259 init_socket(krb5_context context
,
260 krb5_kdc_configuration
*config
,
261 struct descr
*d
, krb5_address
*a
, int family
, int type
, int port
)
264 struct sockaddr_storage __ss
;
265 struct sockaddr
*sa
= (struct sockaddr
*)&__ss
;
266 krb5_socklen_t sa_size
= sizeof(__ss
);
270 ret
= krb5_addr2sockaddr (context
, a
, sa
, &sa_size
, port
);
272 krb5_warn(context
, ret
, "krb5_addr2sockaddr");
278 if (sa
->sa_family
!= family
)
281 d
->s
= socket(family
, type
, 0);
283 krb5_warn(context
, errno
, "socket(%d, %d, 0)", family
, type
);
287 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
290 setsockopt(d
->s
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&one
, sizeof(one
));
296 if(bind(d
->s
, sa
, sa_size
) < 0){
300 krb5_print_address (a
, a_str
, sizeof(a_str
), &len
);
301 krb5_warn(context
, errno
, "bind %s/%d", a_str
, ntohs(port
));
306 if(type
== SOCK_STREAM
&& listen(d
->s
, SOMAXCONN
) < 0){
310 krb5_print_address (a
, a_str
, sizeof(a_str
), &len
);
311 krb5_warn(context
, errno
, "listen %s/%d", a_str
, ntohs(port
));
319 * Allocate descriptors for all the sockets that we should listen on
320 * and return the number of them.
324 init_sockets(krb5_context context
,
325 krb5_kdc_configuration
*config
,
332 krb5_addresses addresses
;
334 if (explicit_addresses
.len
) {
335 addresses
= explicit_addresses
;
337 ret
= krb5_get_all_server_addrs (context
, &addresses
);
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
));
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
);
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",
360 ntohs(ports
[i
].port
),
361 (ports
[i
].type
== SOCK_STREAM
) ? "tcp" : "udp");
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
);
382 descr_type(struct descr
*d
)
384 if (d
->type
== SOCK_DGRAM
)
386 else if (d
->type
== SOCK_STREAM
)
392 addr_to_string(krb5_context context
,
393 struct sockaddr
*addr
, size_t addr_len
, char *str
, size_t len
)
396 if(krb5_sockaddr2address(context
, addr
, &a
) == 0) {
397 if(krb5_print_address(&a
, str
, len
, &len
) == 0) {
398 krb5_free_address(context
, &a
);
401 krb5_free_address(context
, &a
);
403 snprintf(str
, len
, "<family=%d>", addr
->sa_family
);
411 send_reply(krb5_context context
,
412 krb5_kdc_configuration
*config
,
413 krb5_boolean prependlength
,
417 kdc_log(context
, config
, 5,
418 "sending %lu bytes to %s", (unsigned long)reply
->length
,
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
));
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
));
440 * Handle the request in `buf, len' to socket `d'
444 do_request(krb5_context context
,
445 krb5_kdc_configuration
*config
,
446 void *buf
, size_t len
, krb5_boolean prependlength
,
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
,
461 krb5_kdc_save_request(context
, request_log
, buf
, len
, &reply
, d
->sa
);
463 send_reply(context
, config
, prependlength
, d
, &reply
);
464 krb5_data_free(&reply
);
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'
477 handle_udp(krb5_context context
,
478 krb5_kdc_configuration
*config
,
484 buf
= malloc(max_request_udp
);
486 kdc_log(context
, config
, 0, "Failed to allocate %lu bytes", (unsigned long)max_request_udp
);
490 d
->sock_len
= sizeof(d
->__ss
);
491 n
= recvfrom(d
->s
, buf
, max_request_udp
, 0, d
->sa
, &d
->sock_len
);
493 krb5_warn(context
, errno
, "recvfrom");
495 addr_to_string (context
, d
->sa
, d
->sock_len
,
496 d
->addr_string
, sizeof(d
->addr_string
));
497 if (n
== max_request_udp
) {
499 krb5_warn(context
, errno
,
500 "recvfrom: truncated packet from %s, asking for TCP",
502 krb5_mk_error(context
,
503 KRB5KRB_ERR_RESPONSE_TOO_BIG
,
511 send_reply(context
, config
, FALSE
, d
, &data
);
512 krb5_data_free(&data
);
514 do_request(context
, config
, buf
, n
, FALSE
, d
);
521 clear_descr(struct descr
*d
)
524 memset(d
->buf
, 0, d
->size
);
532 /* remove HTTP %-quoting from 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])) {
540 if(sscanf((char *)p
+ 1, "%2x", &x
) != 1)
551 #define TCP_TIMEOUT 4
554 * accept a new TCP connection on `d[parent]' and store it in `d[child]'
558 add_new_tcp (krb5_context context
,
559 krb5_kdc_configuration
*config
,
560 struct descr
*d
, int parent
, int child
)
567 d
[child
].sock_len
= sizeof(d
[child
].__ss
);
568 s
= accept(d
[parent
].s
, d
[child
].sa
, &d
[child
].sock_len
);
570 krb5_warn(context
, errno
, "accept");
574 if (s
>= FD_SETSIZE
) {
575 krb5_warnx(context
, "socket FD too large");
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
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
) {
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
);
609 tmp
= realloc (d
->buf
, d
->size
+ grow
);
611 kdc_log(context
, config
, 0, "Failed to re-allocate %lu bytes.",
612 (unsigned long)d
->size
+ grow
);
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.
628 handle_vanilla_tcp (krb5_context context
,
629 krb5_kdc_configuration
*config
,
635 sp
= krb5_storage_from_mem(d
->buf
, d
->len
);
637 kdc_log (context
, config
, 0, "krb5_storage_from_mem failed");
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);
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.
656 handle_http_tcp (krb5_context context
,
657 krb5_kdc_configuration
*config
,
667 p
= strstr(s
, "\r\n");
669 kdc_log(context
, config
, 0, "Malformed HTTP request from %s", d
->addr_string
);
675 t
= strtok_r(s
, " \t", &p
);
677 kdc_log(context
, config
, 0, "Malformed HTTP request from %s", d
->addr_string
);
680 t
= strtok_r(NULL
, " \t", &p
);
682 kdc_log(context
, config
, 0, "Malformed HTTP request from %s", d
->addr_string
);
685 data
= malloc(strlen(t
));
687 kdc_log(context
, config
, 0, "Failed to allocate %lu bytes",
688 (unsigned long)strlen(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
);
699 proto
= strtok_r(NULL
, " \t", &p
);
701 kdc_log(context
, config
, 0, "Malformed HTTP request from %s", d
->addr_string
);
705 len
= base64_decode(t
, data
);
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
);
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
));
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
));
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) {
743 kdc_log(context
, config
, 0, "HTTP write failed: %s: %s",
744 d
->addr_string
, strerror(errno
));
747 if (write(d
->s
, msg
, strlen(msg
)) < 0) {
749 kdc_log(context
, config
, 0, "HTTP write failed: %s: %s",
750 d
->addr_string
, strerror(errno
));
756 memcpy(d
->buf
, data
, len
);
763 * Handle incoming data to the TCP socket in `d[index]'
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];
775 if (d
[idx
].timeout
== 0) {
776 add_new_tcp (context
, config
, d
, idx
, min_free
);
780 n
= recvfrom(d
[idx
].s
, buf
, sizeof(buf
), 0, NULL
, NULL
);
782 krb5_warn(context
, errno
, "recvfrom failed from %s to %s/%d",
783 d
[idx
].addr_string
, descr_type(d
+ idx
),
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
),
791 clear_descr (d
+ idx
);
794 if (grow_descr (context
, config
, &d
[idx
], n
))
796 memcpy(d
[idx
].buf
+ d
[idx
].len
, buf
, 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
&&
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
]);
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
),
817 if (d
[idx
].buf
[0] & 0x80) {
820 kdc_log (context
, config
, 0, "TCP extension not supported");
822 ret
= krb5_mk_error(context
,
823 KRB5KRB_ERR_FIELD_TOOLONG
,
832 send_reply(context
, config
, TRUE
, d
+ idx
, &reply
);
833 krb5_data_free(&reply
);
836 clear_descr(d
+ idx
);
842 do_request(context
, config
,
843 d
[idx
].buf
, d
[idx
].len
, TRUE
, &d
[idx
]);
844 clear_descr(d
+ idx
);
849 loop(krb5_context context
,
850 krb5_kdc_configuration
*config
)
855 ndescr
= init_sockets(context
, config
, &d
);
857 krb5_errx(context
, 1, "No sockets!");
858 kdc_log(context
, config
, 0, "KDC started");
859 while(exit_flag
== 0){
860 struct timeval tmout
;
867 for(i
= 0; i
< ndescr
; i
++) {
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
);
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
)
887 tmp
= realloc(d
, (ndescr
+ 4) * sizeof(*d
));
889 krb5_warnx(context
, "No memory");
892 reinit_descrs (d
, ndescr
);
893 memset(d
+ ndescr
, 0, 4 * sizeof(*d
));
894 for(i
= ndescr
; i
< ndescr
+ 4; i
++)
901 tmout
.tv_sec
= TCP_TIMEOUT
;
903 switch(select(max_fd
+ 1, &fds
, 0, 0, &tmout
)){
908 krb5_warn(context
, errno
, "select");
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");
925 kdc_log(context
, config
, 0, "Unexpected exit reason: %d", exit_flag
);