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
37 * a tuple describing on what to listen
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.
56 add_port(krb5_context context
,
57 int family
, int port
, const char *protocol
)
62 if(strcmp(protocol
, "udp") == 0)
64 else if(strcmp(protocol
, "tcp") == 0)
68 for(i
= 0; i
< num_ports
; i
++){
69 if(ports
[i
].type
== type
70 && ports
[i
].port
== port
71 && ports
[i
].family
== family
)
74 ports
= realloc(ports
, (num_ports
+ 1) * sizeof(*ports
));
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
;
84 * add a triple but with service -> port lookup
85 * (this prints warnings for stuff that does not exist)
89 add_port_service(krb5_context context
,
90 int family
, const char *service
, int port
,
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)
103 add_port_string (krb5_context context
,
104 int family
, const char *str
, const char *protocol
)
109 sp
= roken_getservbyname (str
, protocol
);
115 port
= htons(strtol(str
, &end
, 0));
119 add_port (context
, family
, port
, protocol
);
123 * add the standard collection of ports for `family'
127 add_standard_ports (krb5_context context
,
128 krb5_kdc_configuration
*config
,
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");
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]
151 parse_ports(krb5_context context
,
152 krb5_kdc_configuration
*config
,
157 char *str_copy
= strdup (str
);
159 p
= strtok_r(str_copy
, " \t", &pos
);
161 if(strcmp(p
, "+") == 0) {
163 add_standard_ports(context
, config
, AF_INET6
);
165 add_standard_ports(context
, config
, AF_INET
);
167 char *q
= strchr(p
, '/');
171 add_port_string(context
, AF_INET6
, p
, q
);
173 add_port_string(context
, AF_INET
, p
, q
);
176 add_port_string(context
, AF_INET6
, p
, "udp");
177 add_port_string(context
, AF_INET6
, p
, "tcp");
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
);
190 * every socket we listen on
201 struct sockaddr_storage __ss
;
204 char addr_string
[128];
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'.
220 reinit_descrs (struct descr
*d
, int n
)
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'
233 init_socket(krb5_context context
,
234 krb5_kdc_configuration
*config
,
235 struct descr
*d
, krb5_address
*a
, int family
, int type
, int port
)
238 struct sockaddr_storage __ss
;
239 struct sockaddr
*sa
= (struct sockaddr
*)&__ss
;
240 krb5_socklen_t sa_size
= sizeof(__ss
);
244 ret
= krb5_addr2sockaddr (context
, a
, sa
, &sa_size
, port
);
246 krb5_warn(context
, ret
, "krb5_addr2sockaddr");
247 rk_closesocket(d
->s
);
248 d
->s
= rk_INVALID_SOCKET
;
252 if (sa
->sa_family
!= family
)
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
;
261 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
264 setsockopt(d
->s
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&one
, sizeof(one
));
270 if(rk_IS_SOCKET_ERROR(bind(d
->s
, sa
, sa_size
))){
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
;
280 if(type
== SOCK_STREAM
&& rk_IS_SOCKET_ERROR(listen(d
->s
, SOMAXCONN
))){
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
;
293 * Allocate descriptors for all the sockets that we should listen on
294 * and return the number of them.
298 init_sockets(krb5_context context
,
299 krb5_kdc_configuration
*config
,
306 krb5_addresses addresses
;
308 if (explicit_addresses
.len
) {
309 addresses
= explicit_addresses
;
311 ret
= krb5_get_all_server_addrs (context
, &addresses
);
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
));
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
){
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",
334 ntohs(ports
[i
].port
),
335 (ports
[i
].type
== SOCK_STREAM
) ? "tcp" : "udp");
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
);
356 descr_type(struct descr
*d
)
358 if (d
->type
== SOCK_DGRAM
)
360 else if (d
->type
== SOCK_STREAM
)
366 addr_to_string(krb5_context context
,
367 struct sockaddr
*addr
, size_t addr_len
, char *str
, size_t len
)
370 if(krb5_sockaddr2address(context
, addr
, &a
) == 0) {
371 if(krb5_print_address(&a
, str
, len
, &len
) == 0) {
372 krb5_free_address(context
, &a
);
375 krb5_free_address(context
, &a
);
377 snprintf(str
, len
, "<family=%d>", addr
->sa_family
);
385 send_reply(krb5_context context
,
386 krb5_kdc_configuration
*config
,
387 krb5_boolean prependlength
,
391 kdc_log(context
, config
, 5,
392 "sending %lu bytes to %s", (unsigned long)reply
->length
,
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
));
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
));
415 * Handle the request in `buf, len' to socket `d'
419 do_request(krb5_context context
,
420 krb5_kdc_configuration
*config
,
421 void *buf
, size_t len
, krb5_boolean prependlength
,
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
,
436 krb5_kdc_save_request(context
, request_log
, buf
, len
, &reply
, d
->sa
);
438 send_reply(context
, config
, prependlength
, d
, &reply
);
439 krb5_data_free(&reply
);
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'
452 handle_udp(krb5_context context
,
453 krb5_kdc_configuration
*config
,
459 buf
= malloc(max_request_udp
);
461 kdc_log(context
, config
, 0, "Failed to allocate %lu bytes", (unsigned long)max_request_udp
);
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");
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
) {
474 krb5_warn(context
, errno
,
475 "recvfrom: truncated packet from %s, asking for TCP",
477 krb5_mk_error(context
,
478 KRB5KRB_ERR_RESPONSE_TOO_BIG
,
486 send_reply(context
, config
, FALSE
, d
, &data
);
487 krb5_data_free(&data
);
489 do_request(context
, config
, buf
, n
, FALSE
, d
);
496 clear_descr(struct descr
*d
)
499 memset(d
->buf
, 0, d
->size
);
501 if(d
->s
!= rk_INVALID_SOCKET
)
502 rk_closesocket(d
->s
);
503 d
->s
= rk_INVALID_SOCKET
;
507 /* remove HTTP %-quoting from 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])) {
515 if(sscanf((char *)p
+ 1, "%2x", &x
) != 1)
526 #define TCP_TIMEOUT 4
529 * accept a new TCP connection on `d[parent]' and store it in `d[child]'
533 add_new_tcp (krb5_context context
,
534 krb5_kdc_configuration
*config
,
535 struct descr
*d
, int parent
, int child
)
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");
550 if (s
>= FD_SETSIZE
) {
551 krb5_warnx(context
, "socket FD too large");
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
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
) {
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
);
586 tmp
= realloc (d
->buf
, d
->size
+ grow
);
588 kdc_log(context
, config
, 0, "Failed to re-allocate %lu bytes.",
589 (unsigned long)d
->size
+ grow
);
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.
605 handle_vanilla_tcp (krb5_context context
,
606 krb5_kdc_configuration
*config
,
612 sp
= krb5_storage_from_mem(d
->buf
, d
->len
);
614 kdc_log (context
, config
, 0, "krb5_storage_from_mem failed");
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);
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.
633 handle_http_tcp (krb5_context context
,
634 krb5_kdc_configuration
*config
,
644 /* If its a multi line query, truncate off the first line */
645 p
= strstr(s
, "\r\n");
650 t
= strtok_r(s
, " \t", &p
);
652 kdc_log(context
, config
, 0,
653 "Missing HTTP operand (GET) request from %s", d
->addr_string
);
657 t
= strtok_r(NULL
, " \t", &p
);
659 kdc_log(context
, config
, 0,
660 "Missing HTTP GET data in request from %s", d
->addr_string
);
664 data
= malloc(strlen(t
));
666 kdc_log(context
, config
, 0, "Failed to allocate %lu bytes",
667 (unsigned long)strlen(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
);
678 proto
= strtok_r(NULL
, " \t", &p
);
680 kdc_log(context
, config
, 0, "Malformed HTTP request from %s", d
->addr_string
);
684 len
= base64_decode(t
, data
);
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
);
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
));
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
));
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))) {
722 kdc_log(context
, config
, 0, "HTTP write failed: %s: %s",
723 d
->addr_string
, strerror(rk_SOCK_ERRNO
));
726 if (rk_IS_SOCKET_ERROR(send(d
->s
, msg
, strlen(msg
), 0))) {
728 kdc_log(context
, config
, 0, "HTTP write failed: %s: %s",
729 d
->addr_string
, strerror(rk_SOCK_ERRNO
));
733 if ((size_t)len
> d
->len
)
735 memcpy(d
->buf
, data
, len
);
742 * Handle incoming data to the TCP socket in `d[index]'
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];
754 if (d
[idx
].timeout
== 0) {
755 add_new_tcp (context
, config
, d
, idx
, min_free
);
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
),
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
),
770 clear_descr (d
+ idx
);
773 if (grow_descr (context
, config
, &d
[idx
], n
))
775 memcpy(d
[idx
].buf
+ d
[idx
].len
, buf
, 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
&&
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
]);
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
),
796 if (d
[idx
].buf
[0] & 0x80) {
799 kdc_log (context
, config
, 0, "TCP extension not supported");
801 ret
= krb5_mk_error(context
,
802 KRB5KRB_ERR_FIELD_TOOLONG
,
811 send_reply(context
, config
, TRUE
, d
+ idx
, &reply
);
812 krb5_data_free(&reply
);
815 clear_descr(d
+ idx
);
821 do_request(context
, config
,
822 d
[idx
].buf
, d
[idx
].len
, TRUE
, &d
[idx
]);
823 clear_descr(d
+ idx
);
828 loop(krb5_context context
,
829 krb5_kdc_configuration
*config
)
834 ndescr
= init_sockets(context
, config
, &d
);
836 krb5_errx(context
, 1, "No sockets!");
837 kdc_log(context
, config
, 0, "KDC started");
838 while(exit_flag
== 0){
839 struct timeval tmout
;
846 for(i
= 0; i
< ndescr
; i
++) {
847 if(!rk_IS_BAD_SOCKET(d
[i
].s
)){
848 if(d
[i
].type
== SOCK_STREAM
&&
849 d
[i
].timeout
&& d
[i
].timeout
< time(NULL
)) {
850 kdc_log(context
, config
, 1,
851 "TCP-connection from %s expired after %lu bytes",
852 d
[i
].addr_string
, (unsigned long)d
[i
].len
);
856 #ifndef NO_LIMIT_FD_SETSIZE
860 if (max_fd
>= FD_SETSIZE
)
861 krb5_errx(context
, 1, "fd too large");
864 FD_SET(d
[i
].s
, &fds
);
865 } else if(min_free
< 0 || i
< (size_t)min_free
)
870 tmp
= realloc(d
, (ndescr
+ 4) * sizeof(*d
));
872 krb5_warnx(context
, "No memory");
875 reinit_descrs (d
, ndescr
);
876 memset(d
+ ndescr
, 0, 4 * sizeof(*d
));
877 for(i
= ndescr
; i
< ndescr
+ 4; i
++)
884 tmout
.tv_sec
= TCP_TIMEOUT
;
886 switch(select(max_fd
+ 1, &fds
, 0, 0, &tmout
)){
891 krb5_warn(context
, rk_SOCK_ERRNO
, "select");
894 for(i
= 0; i
< ndescr
; i
++)
895 if(!rk_IS_BAD_SOCKET(d
[i
].s
) && FD_ISSET(d
[i
].s
, &fds
)) {
896 if(d
[i
].type
== SOCK_DGRAM
)
897 handle_udp(context
, config
, &d
[i
]);
898 else if(d
[i
].type
== SOCK_STREAM
)
899 handle_tcp(context
, config
, d
, i
, min_free
);
905 else if(exit_flag
== SIGXCPU
)
906 kdc_log(context
, config
, 0, "CPU time limit exceeded");
908 else if(exit_flag
== SIGINT
|| exit_flag
== SIGTERM
)
909 kdc_log(context
, config
, 0, "Terminated");
911 kdc_log(context
, config
, 0, "Unexpected exit reason: %d", exit_flag
);