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 socket_set_nonblocking(d
->s
, 1);
272 if(rk_IS_SOCKET_ERROR(bind(d
->s
, sa
, sa_size
))){
276 krb5_print_address (a
, a_str
, sizeof(a_str
), &len
);
277 krb5_warn(context
, errno
, "bind %s/%d", a_str
, ntohs(port
));
278 rk_closesocket(d
->s
);
279 d
->s
= rk_INVALID_SOCKET
;
282 if(type
== SOCK_STREAM
&& rk_IS_SOCKET_ERROR(listen(d
->s
, SOMAXCONN
))){
286 krb5_print_address (a
, a_str
, sizeof(a_str
), &len
);
287 krb5_warn(context
, errno
, "listen %s/%d", a_str
, ntohs(port
));
288 rk_closesocket(d
->s
);
289 d
->s
= rk_INVALID_SOCKET
;
295 * Allocate descriptors for all the sockets that we should listen on
296 * and return the number of them.
300 init_sockets(krb5_context context
,
301 krb5_kdc_configuration
*config
,
308 krb5_addresses addresses
;
310 if (explicit_addresses
.len
) {
311 addresses
= explicit_addresses
;
313 ret
= krb5_get_all_server_addrs (context
, &addresses
);
315 krb5_err (context
, 1, ret
, "krb5_get_all_server_addrs");
317 parse_ports(context
, config
, port_str
);
318 d
= malloc(addresses
.len
* num_ports
* sizeof(*d
));
320 krb5_errx(context
, 1, "malloc(%lu) failed",
321 (unsigned long)num_ports
* sizeof(*d
));
323 for (i
= 0; i
< num_ports
; i
++){
324 for (j
= 0; j
< addresses
.len
; ++j
) {
325 init_socket(context
, config
, &d
[num
], &addresses
.val
[j
],
326 ports
[i
].family
, ports
[i
].type
, ports
[i
].port
);
327 if(d
[num
].s
!= rk_INVALID_SOCKET
){
331 krb5_print_address (&addresses
.val
[j
], a_str
,
332 sizeof(a_str
), &len
);
334 kdc_log(context
, config
, 5, "listening on %s port %u/%s",
336 ntohs(ports
[i
].port
),
337 (ports
[i
].type
== SOCK_STREAM
) ? "tcp" : "udp");
343 krb5_free_addresses (context
, &addresses
);
344 d
= realloc(d
, num
* sizeof(*d
));
345 if (d
== NULL
&& num
!= 0)
346 krb5_errx(context
, 1, "realloc(%lu) failed",
347 (unsigned long)num
* sizeof(*d
));
348 reinit_descrs (d
, num
);
358 descr_type(struct descr
*d
)
360 if (d
->type
== SOCK_DGRAM
)
362 else if (d
->type
== SOCK_STREAM
)
368 addr_to_string(krb5_context context
,
369 struct sockaddr
*addr
, size_t addr_len
, char *str
, size_t len
)
372 if(krb5_sockaddr2address(context
, addr
, &a
) == 0) {
373 if(krb5_print_address(&a
, str
, len
, &len
) == 0) {
374 krb5_free_address(context
, &a
);
377 krb5_free_address(context
, &a
);
379 snprintf(str
, len
, "<family=%d>", addr
->sa_family
);
387 send_reply(krb5_context context
,
388 krb5_kdc_configuration
*config
,
389 krb5_boolean prependlength
,
393 kdc_log(context
, config
, 5,
394 "sending %lu bytes to %s", (unsigned long)reply
->length
,
398 l
[0] = (reply
->length
>> 24) & 0xff;
399 l
[1] = (reply
->length
>> 16) & 0xff;
400 l
[2] = (reply
->length
>> 8) & 0xff;
401 l
[3] = reply
->length
& 0xff;
402 if(rk_IS_SOCKET_ERROR(sendto(d
->s
, l
, sizeof(l
), 0, d
->sa
, d
->sock_len
))) {
403 kdc_log (context
, config
,
404 0, "sendto(%s): %s", d
->addr_string
,
405 strerror(rk_SOCK_ERRNO
));
409 if(rk_IS_SOCKET_ERROR(sendto(d
->s
, reply
->data
, reply
->length
, 0, d
->sa
, d
->sock_len
))) {
410 kdc_log (context
, config
, 0, "sendto(%s): %s", d
->addr_string
,
411 strerror(rk_SOCK_ERRNO
));
417 * Handle the request in `buf, len' to socket `d'
421 do_request(krb5_context context
,
422 krb5_kdc_configuration
*config
,
423 void *buf
, size_t len
, krb5_boolean prependlength
,
428 int datagram_reply
= (d
->type
== SOCK_DGRAM
);
430 krb5_kdc_update_time(NULL
);
432 krb5_data_zero(&reply
);
433 ret
= krb5_kdc_process_request(context
, config
,
434 buf
, len
, &reply
, &prependlength
,
435 d
->addr_string
, d
->sa
,
438 krb5_kdc_save_request(context
, request_log
, buf
, len
, &reply
, d
->sa
);
440 send_reply(context
, config
, prependlength
, d
, &reply
);
441 krb5_data_free(&reply
);
444 kdc_log(context
, config
, 0,
445 "Failed processing %lu byte request from %s",
446 (unsigned long)len
, d
->addr_string
);
450 * Handle incoming data to the UDP socket in `d'
454 handle_udp(krb5_context context
,
455 krb5_kdc_configuration
*config
,
461 buf
= malloc(max_request_udp
);
463 kdc_log(context
, config
, 0, "Failed to allocate %lu bytes",
464 (unsigned long)max_request_udp
);
468 d
->sock_len
= sizeof(d
->__ss
);
469 n
= recvfrom(d
->s
, buf
, max_request_udp
, 0, d
->sa
, &d
->sock_len
);
470 if (rk_IS_SOCKET_ERROR(n
)) {
471 if (rk_SOCK_ERRNO
!= EAGAIN
&& rk_SOCK_ERRNO
!= EINTR
)
472 krb5_warn(context
, rk_SOCK_ERRNO
, "recvfrom");
474 addr_to_string (context
, d
->sa
, d
->sock_len
,
475 d
->addr_string
, sizeof(d
->addr_string
));
476 if ((size_t)n
== max_request_udp
) {
478 krb5_warn(context
, errno
,
479 "recvfrom: truncated packet from %s, asking for TCP",
481 krb5_mk_error(context
,
482 KRB5KRB_ERR_RESPONSE_TOO_BIG
,
490 send_reply(context
, config
, FALSE
, d
, &data
);
491 krb5_data_free(&data
);
493 do_request(context
, config
, buf
, n
, FALSE
, d
);
500 clear_descr(struct descr
*d
)
503 memset(d
->buf
, 0, d
->size
);
505 if(d
->s
!= rk_INVALID_SOCKET
)
506 rk_closesocket(d
->s
);
507 d
->s
= rk_INVALID_SOCKET
;
511 /* remove HTTP %-quoting from buf */
515 unsigned char *p
, *q
;
516 for(p
= q
= (unsigned char *)buf
; *p
; p
++, q
++) {
517 if(*p
== '%' && isxdigit(p
[1]) && isxdigit(p
[2])) {
519 if(sscanf((char *)p
+ 1, "%2x", &x
) != 1)
530 #define TCP_TIMEOUT 4
533 * accept a new TCP connection on `d[parent]' and store it in `d[child]'
537 add_new_tcp (krb5_context context
,
538 krb5_kdc_configuration
*config
,
539 struct descr
*d
, int parent
, int child
)
546 d
[child
].sock_len
= sizeof(d
[child
].__ss
);
547 s
= accept(d
[parent
].s
, d
[child
].sa
, &d
[child
].sock_len
);
548 if(rk_IS_BAD_SOCKET(s
)) {
549 if (rk_SOCK_ERRNO
!= EAGAIN
&& rk_SOCK_ERRNO
!= EINTR
)
550 krb5_warn(context
, rk_SOCK_ERRNO
, "accept");
555 if (s
>= FD_SETSIZE
) {
556 krb5_warnx(context
, "socket FD too large");
563 d
[child
].timeout
= time(NULL
) + TCP_TIMEOUT
;
564 d
[child
].type
= SOCK_STREAM
;
565 addr_to_string (context
,
566 d
[child
].sa
, d
[child
].sock_len
,
567 d
[child
].addr_string
, sizeof(d
[child
].addr_string
));
571 * Grow `d' to handle at least `n'.
572 * Return != 0 if fails
576 grow_descr (krb5_context context
,
577 krb5_kdc_configuration
*config
,
578 struct descr
*d
, size_t n
)
580 if (d
->size
- d
->len
< n
) {
584 grow
= max(1024, d
->len
+ n
);
585 if (d
->size
+ grow
> max_request_tcp
) {
586 kdc_log(context
, config
, 0, "Request exceeds max request size (%lu bytes).",
587 (unsigned long)d
->size
+ grow
);
591 tmp
= realloc (d
->buf
, d
->size
+ grow
);
593 kdc_log(context
, config
, 0, "Failed to re-allocate %lu bytes.",
594 (unsigned long)d
->size
+ grow
);
605 * Try to handle the TCP data at `d->buf, d->len'.
606 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
610 handle_vanilla_tcp (krb5_context context
,
611 krb5_kdc_configuration
*config
,
617 sp
= krb5_storage_from_mem(d
->buf
, d
->len
);
619 kdc_log (context
, config
, 0, "krb5_storage_from_mem failed");
622 krb5_ret_uint32(sp
, &len
);
623 krb5_storage_free(sp
);
624 if(d
->len
- 4 >= len
) {
625 memmove(d
->buf
, d
->buf
+ 4, d
->len
- 4);
633 * Try to handle the TCP/HTTP data at `d->buf, d->len'.
634 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
638 handle_http_tcp (krb5_context context
,
639 krb5_kdc_configuration
*config
,
649 /* If its a multi line query, truncate off the first line */
650 p
= strstr(s
, "\r\n");
655 t
= strtok_r(s
, " \t", &p
);
657 kdc_log(context
, config
, 0,
658 "Missing HTTP operand (GET) request from %s", d
->addr_string
);
662 t
= strtok_r(NULL
, " \t", &p
);
664 kdc_log(context
, config
, 0,
665 "Missing HTTP GET data in request from %s", d
->addr_string
);
669 data
= malloc(strlen(t
));
671 kdc_log(context
, config
, 0, "Failed to allocate %lu bytes",
672 (unsigned long)strlen(t
));
677 if(de_http(t
) != 0) {
678 kdc_log(context
, config
, 0, "Malformed HTTP request from %s", d
->addr_string
);
679 kdc_log(context
, config
, 5, "HTTP request: %s", t
);
683 proto
= strtok_r(NULL
, " \t", &p
);
685 kdc_log(context
, config
, 0, "Malformed HTTP request from %s", d
->addr_string
);
689 len
= rk_base64_decode(t
, data
);
693 "Server: Heimdal/" VERSION
"\r\n"
694 "Cache-Control: no-cache\r\n"
695 "Pragma: no-cache\r\n"
696 "Content-type: text/html\r\n"
697 "Content-transfer-encoding: 8bit\r\n\r\n"
698 "<TITLE>404 Not found</TITLE>\r\n"
699 "<H1>404 Not found</H1>\r\n"
700 "That page doesn't exist, maybe you are looking for "
701 "<A HREF=\"http://www.h5l.org/\">Heimdal</A>?\r\n";
702 kdc_log(context
, config
, 0, "HTTP request from %s is non KDC request", d
->addr_string
);
703 kdc_log(context
, config
, 5, "HTTP request: %s", t
);
705 if (rk_IS_SOCKET_ERROR(send(d
->s
, proto
, strlen(proto
), 0))) {
706 kdc_log(context
, config
, 0, "HTTP write failed: %s: %s",
707 d
->addr_string
, strerror(rk_SOCK_ERRNO
));
710 if (rk_IS_SOCKET_ERROR(send(d
->s
, msg
, strlen(msg
), 0))) {
711 kdc_log(context
, config
, 0, "HTTP write failed: %s: %s",
712 d
->addr_string
, strerror(rk_SOCK_ERRNO
));
720 "Server: Heimdal/" VERSION
"\r\n"
721 "Cache-Control: no-cache\r\n"
722 "Pragma: no-cache\r\n"
723 "Content-type: application/octet-stream\r\n"
724 "Content-transfer-encoding: binary\r\n\r\n";
725 if (rk_IS_SOCKET_ERROR(send(d
->s
, proto
, strlen(proto
), 0))) {
727 kdc_log(context
, config
, 0, "HTTP write failed: %s: %s",
728 d
->addr_string
, strerror(rk_SOCK_ERRNO
));
731 if (rk_IS_SOCKET_ERROR(send(d
->s
, msg
, strlen(msg
), 0))) {
733 kdc_log(context
, config
, 0, "HTTP write failed: %s: %s",
734 d
->addr_string
, strerror(rk_SOCK_ERRNO
));
738 if ((size_t)len
> d
->len
)
740 memcpy(d
->buf
, data
, len
);
747 * Handle incoming data to the TCP socket in `d[index]'
751 handle_tcp(krb5_context context
,
752 krb5_kdc_configuration
*config
,
753 struct descr
*d
, int idx
, int min_free
)
755 unsigned char buf
[1024];
759 if (d
[idx
].timeout
== 0) {
760 add_new_tcp (context
, config
, d
, idx
, min_free
);
764 n
= recvfrom(d
[idx
].s
, buf
, sizeof(buf
), 0, NULL
, NULL
);
765 if(rk_IS_SOCKET_ERROR(n
)){
766 krb5_warn(context
, rk_SOCK_ERRNO
, "recvfrom failed from %s to %s/%d",
767 d
[idx
].addr_string
, descr_type(d
+ idx
),
771 krb5_warnx(context
, "connection closed before end of data after %lu "
772 "bytes from %s to %s/%d", (unsigned long)d
[idx
].len
,
773 d
[idx
].addr_string
, descr_type(d
+ idx
),
775 clear_descr (d
+ idx
);
778 if (grow_descr (context
, config
, &d
[idx
], n
))
780 memcpy(d
[idx
].buf
+ d
[idx
].len
, buf
, n
);
782 if(d
[idx
].len
> 4 && d
[idx
].buf
[0] == 0) {
783 ret
= handle_vanilla_tcp (context
, config
, &d
[idx
]);
784 } else if(enable_http
&&
786 strncmp((char *)d
[idx
].buf
, "GET ", 4) == 0 &&
787 strncmp((char *)d
[idx
].buf
+ d
[idx
].len
- 4,
788 "\r\n\r\n", 4) == 0) {
790 /* remove the trailing \r\n\r\n so the string is NUL terminated */
791 d
[idx
].buf
[d
[idx
].len
- 4] = '\0';
793 ret
= handle_http_tcp (context
, config
, &d
[idx
]);
795 clear_descr (d
+ idx
);
796 } else if (d
[idx
].len
> 4) {
797 kdc_log (context
, config
,
798 0, "TCP data of strange type from %s to %s/%d",
799 d
[idx
].addr_string
, descr_type(d
+ idx
),
801 if (d
[idx
].buf
[0] & 0x80) {
804 kdc_log (context
, config
, 0, "TCP extension not supported");
806 ret
= krb5_mk_error(context
,
807 KRB5KRB_ERR_FIELD_TOOLONG
,
816 send_reply(context
, config
, TRUE
, d
+ idx
, &reply
);
817 krb5_data_free(&reply
);
820 clear_descr(d
+ idx
);
826 do_request(context
, config
,
827 d
[idx
].buf
, d
[idx
].len
, TRUE
, &d
[idx
]);
828 clear_descr(d
+ idx
);
834 handle_islive(int fd
)
839 ret
= read(fd
, buf
, 2);
846 realloc_descrs(struct descr
**d
, unsigned int *ndescr
)
851 tmp
= realloc(*d
, (*ndescr
+ 4) * sizeof(**d
));
856 reinit_descrs (*d
, *ndescr
);
857 memset(*d
+ *ndescr
, 0, 4 * sizeof(**d
));
858 for(i
= *ndescr
; i
< *ndescr
+ 4; i
++)
867 next_min_free(krb5_context context
, struct descr
**d
, unsigned int *ndescr
)
872 for(i
= 0; i
< *ndescr
; i
++) {
874 if(rk_IS_BAD_SOCKET(s
))
879 if(!realloc_descrs(d
, ndescr
)) {
881 krb5_warnx(context
, "No memory");
888 loop(krb5_context context
, krb5_kdc_configuration
*config
,
889 struct descr
*d
, unsigned int ndescr
, int islive
)
892 while (exit_flag
== 0) {
893 struct timeval tmout
;
901 FD_SET(islive
, &fds
);
904 for (i
= 0; i
< ndescr
; i
++) {
905 if (!rk_IS_BAD_SOCKET(d
[i
].s
)) {
906 if (d
[i
].type
== SOCK_STREAM
&&
907 d
[i
].timeout
&& d
[i
].timeout
< time(NULL
)) {
908 kdc_log(context
, config
, 1,
909 "TCP-connection from %s expired after %lu bytes",
910 d
[i
].addr_string
, (unsigned long)d
[i
].len
);
914 #ifndef NO_LIMIT_FD_SETSIZE
918 if (max_fd
>= FD_SETSIZE
)
919 krb5_errx(context
, 1, "fd too large");
922 FD_SET(d
[i
].s
, &fds
);
926 tmout
.tv_sec
= TCP_TIMEOUT
;
928 switch(select(max_fd
+ 1, &fds
, 0, 0, &tmout
)){
933 krb5_warn(context
, rk_SOCK_ERRNO
, "select");
937 if (islive
> -1 && FD_ISSET(islive
, &fds
))
938 handle_islive(islive
);
940 for (i
= 0; i
< ndescr
; i
++)
941 if (!rk_IS_BAD_SOCKET(d
[i
].s
) && FD_ISSET(d
[i
].s
, &fds
)) {
942 min_free
= next_min_free(context
, &d
, &ndescr
);
944 if (d
[i
].type
== SOCK_DGRAM
)
945 handle_udp(context
, config
, &d
[i
]);
946 else if (d
[i
].type
== SOCK_STREAM
)
947 handle_tcp(context
, config
, d
, i
, min_free
);
954 kdc_log(context
, config
, 0,
955 "KDC worker process exiting because KDC master exited.");
959 kdc_log(context
, config
, 0, "CPU time limit exceeded");
964 kdc_log(context
, config
, 0, "Terminated");
967 kdc_log(context
, config
, 0, "Unexpected exit reason: %d", exit_flag
);
974 bonjour_kid(krb5_context context
, krb5_kdc_configuration
*config
, int islive
)
980 bonjour_announce(context
, config
);
982 while (read(islive
, buf
, 1) != 0)
990 kill_kids(pid_t
*pids
, int max_kids
, int sig
)
994 for (i
=0; i
< max_kids
; i
++)
1000 reap_kid(krb5_context context
, krb5_kdc_configuration
*config
,
1001 pid_t
*pids
, int max_kids
, int options
)
1007 pid
= waitpid(-1, &status
, options
);
1011 for (i
=0; i
< max_kids
; i
++) {
1016 if (i
== max_kids
) {
1017 /* XXXrcd: this should not happen, have to do something, though */
1021 /* XXXrcd: should likely log exit code and the like */
1022 kdc_log(context
, config
, 0, "KDC worker process reaped: %d", pid
);
1023 pids
[i
] = (pid_t
)-1;
1029 reap_kids(krb5_context context
, krb5_kdc_configuration
*config
,
1030 pid_t
*pids
, int max_kids
)
1035 if (reap_kid(context
, config
, pids
, max_kids
, WNOHANG
) == 0)
1044 select_sleep(int microseconds
)
1048 tv
.tv_sec
= microseconds
/ 1000000;
1049 tv
.tv_usec
= microseconds
% 1000000;
1050 select(0, NULL
, NULL
, NULL
, &tv
);
1055 start_kdc(krb5_context context
,
1056 krb5_kdc_configuration
*config
)
1061 unsigned int ndescr
;
1065 int max_kdcs
= config
->num_kdc_processes
;
1072 #ifdef _SC_NPROCESSORS_ONLN
1074 max_kdcs
= sysconf(_SC_NPROCESSORS_ONLN
);
1080 pids
= malloc(max_kdcs
* sizeof(*pids
));
1082 krb5_err(context
, 1, errno
, "malloc");
1083 memset(pids
, 0x0, max_kdcs
* sizeof(*pids
));
1086 * We open a socketpair of which we hand one end to each of our kids.
1087 * When we exit, for whatever reason, the children will notice an EOF
1088 * on their end and be able to cleanly exit.
1091 if (socketpair(PF_LOCAL
, SOCK_STREAM
, 0, islive
) == -1)
1092 krb5_errx(context
, 1, "socketpair");
1093 socket_set_nonblocking(islive
[1], 1);
1096 ndescr
= init_sockets(context
, config
, &d
);
1098 krb5_errx(context
, 1, "No sockets!");
1103 bonjour_kid(context
, config
, islive
[1]); /* fork()s */
1106 kdc_log(context
, config
, 0, "KDC started master process pid=%d", getpid());
1108 kdc_log(context
, config
, 0, "KDC started pid=%d", getpid());
1111 roken_detach_finish(NULL
, daemon_child
);
1117 while (exit_flag
== 0) {
1119 /* Slow down the creation of KDCs... */
1121 gettimeofday(&tv2
, NULL
);
1122 if (tv1
.tv_sec
== tv2
.tv_sec
&& tv2
.tv_usec
- tv1
.tv_usec
< 25000) {
1123 #if 0 /* XXXrcd: should print a message... */
1124 kdc_log(context
, config
, 0, "Spawning KDCs too quickly, "
1125 "pausing for 50ms");
1127 select_sleep(12500);
1131 if (num_kdcs
>= max_kdcs
) {
1132 num_kdcs
-= reap_kid(context
, config
, pids
, max_kdcs
, 0);
1137 num_kdcs
-= reap_kids(context
, config
, pids
, max_kdcs
);
1143 loop(context
, config
, d
, ndescr
, islive
[1]);
1146 /* XXXrcd: hmmm, do something useful?? */
1147 kdc_log(context
, config
, 0,
1148 "KDC master process could not fork worker process");
1152 for (i
=0; i
< max_kdcs
; i
++)
1156 kdc_log(context
, config
, 0, "KDC worker process started: %d", pid
);
1158 gettimeofday(&tv1
, NULL
);
1163 /* Closing these sockets should cause the kids to die... */
1168 gettimeofday(&tv1
, NULL
);
1171 num_kdcs
-= reap_kids(context
, config
, pids
, max_kdcs
);
1175 * Using select to sleep will fail with EINTR if we receive a
1176 * SIGCHLD. This is desirable.
1178 select_sleep(200000);
1179 kill_kids(pids
, max_kdcs
, SIGTERM
);
1180 gettimeofday(&tv2
, NULL
);
1181 if (tv2
.tv_sec
- tv1
.tv_sec
> 10)
1186 kill_kids(pids
, max_kdcs
, SIGKILL
);
1187 num_kdcs
-= reap_kids(context
, config
, pids
, max_kdcs
);
1190 select_sleep(200000);
1191 gettimeofday(&tv2
, NULL
);
1192 if (tv2
.tv_sec
- tv1
.tv_sec
> 15)
1196 kdc_log(context
, config
, 0, "KDC master process exiting", pid
);
1199 loop(context
, config
, d
, ndescr
, -1);
1200 kdc_log(context
, config
, 0, "KDC exiting", pid
);