Home | History | Annotate | Line # | Download | only in kdc
connect.c revision 1.1.1.2.10.1
      1  1.1.1.2.10.1  bouyer /*	$NetBSD: connect.c,v 1.1.1.2.10.1 2017/04/21 16:50:44 bouyer Exp $	*/
      2           1.1   elric 
      3           1.1   elric /*
      4           1.1   elric  * Copyright (c) 1997-2005 Kungliga Tekniska Hgskolan
      5           1.1   elric  * (Royal Institute of Technology, Stockholm, Sweden).
      6           1.1   elric  * All rights reserved.
      7           1.1   elric  *
      8           1.1   elric  * Redistribution and use in source and binary forms, with or without
      9           1.1   elric  * modification, are permitted provided that the following conditions
     10           1.1   elric  * are met:
     11           1.1   elric  *
     12           1.1   elric  * 1. Redistributions of source code must retain the above copyright
     13           1.1   elric  *    notice, this list of conditions and the following disclaimer.
     14           1.1   elric  *
     15           1.1   elric  * 2. Redistributions in binary form must reproduce the above copyright
     16           1.1   elric  *    notice, this list of conditions and the following disclaimer in the
     17           1.1   elric  *    documentation and/or other materials provided with the distribution.
     18           1.1   elric  *
     19           1.1   elric  * 3. Neither the name of the Institute nor the names of its contributors
     20           1.1   elric  *    may be used to endorse or promote products derived from this software
     21           1.1   elric  *    without specific prior written permission.
     22           1.1   elric  *
     23           1.1   elric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
     24           1.1   elric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25           1.1   elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26           1.1   elric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
     27           1.1   elric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28           1.1   elric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29           1.1   elric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30           1.1   elric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31           1.1   elric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32           1.1   elric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33           1.1   elric  * SUCH DAMAGE.
     34           1.1   elric  */
     35           1.1   elric 
     36           1.1   elric #include "kdc_locl.h"
     37           1.1   elric 
     38           1.1   elric /*
     39           1.1   elric  * a tuple describing on what to listen
     40           1.1   elric  */
     41           1.1   elric 
     42           1.1   elric struct port_desc{
     43           1.1   elric     int family;
     44           1.1   elric     int type;
     45           1.1   elric     int port;
     46           1.1   elric };
     47           1.1   elric 
     48           1.1   elric /* the current ones */
     49           1.1   elric 
     50           1.1   elric static struct port_desc *ports;
     51       1.1.1.2  pettai static size_t num_ports;
     52  1.1.1.2.10.1  bouyer static pid_t bonjour_pid = -1;
     53           1.1   elric 
     54           1.1   elric /*
     55           1.1   elric  * add `family, port, protocol' to the list with duplicate suppresion.
     56           1.1   elric  */
     57           1.1   elric 
     58           1.1   elric static void
     59           1.1   elric add_port(krb5_context context,
     60           1.1   elric 	 int family, int port, const char *protocol)
     61           1.1   elric {
     62           1.1   elric     int type;
     63       1.1.1.2  pettai     size_t i;
     64           1.1   elric 
     65           1.1   elric     if(strcmp(protocol, "udp") == 0)
     66           1.1   elric 	type = SOCK_DGRAM;
     67           1.1   elric     else if(strcmp(protocol, "tcp") == 0)
     68           1.1   elric 	type = SOCK_STREAM;
     69           1.1   elric     else
     70           1.1   elric 	return;
     71           1.1   elric     for(i = 0; i < num_ports; i++){
     72           1.1   elric 	if(ports[i].type == type
     73           1.1   elric 	   && ports[i].port == port
     74           1.1   elric 	   && ports[i].family == family)
     75           1.1   elric 	    return;
     76           1.1   elric     }
     77           1.1   elric     ports = realloc(ports, (num_ports + 1) * sizeof(*ports));
     78           1.1   elric     if (ports == NULL)
     79           1.1   elric 	krb5_err (context, 1, errno, "realloc");
     80           1.1   elric     ports[num_ports].family = family;
     81           1.1   elric     ports[num_ports].type   = type;
     82           1.1   elric     ports[num_ports].port   = port;
     83           1.1   elric     num_ports++;
     84           1.1   elric }
     85           1.1   elric 
     86           1.1   elric /*
     87           1.1   elric  * add a triple but with service -> port lookup
     88           1.1   elric  * (this prints warnings for stuff that does not exist)
     89           1.1   elric  */
     90           1.1   elric 
     91           1.1   elric static void
     92           1.1   elric add_port_service(krb5_context context,
     93           1.1   elric 		 int family, const char *service, int port,
     94           1.1   elric 		 const char *protocol)
     95           1.1   elric {
     96           1.1   elric     port = krb5_getportbyname (context, service, protocol, port);
     97           1.1   elric     add_port (context, family, port, protocol);
     98           1.1   elric }
     99           1.1   elric 
    100           1.1   elric /*
    101           1.1   elric  * add the port with service -> port lookup or string -> number
    102           1.1   elric  * (no warning is printed)
    103           1.1   elric  */
    104           1.1   elric 
    105           1.1   elric static void
    106           1.1   elric add_port_string (krb5_context context,
    107           1.1   elric 		 int family, const char *str, const char *protocol)
    108           1.1   elric {
    109           1.1   elric     struct servent *sp;
    110           1.1   elric     int port;
    111           1.1   elric 
    112           1.1   elric     sp = roken_getservbyname (str, protocol);
    113           1.1   elric     if (sp != NULL) {
    114           1.1   elric 	port = sp->s_port;
    115           1.1   elric     } else {
    116           1.1   elric 	char *end;
    117           1.1   elric 
    118           1.1   elric 	port = htons(strtol(str, &end, 0));
    119           1.1   elric 	if (end == str)
    120           1.1   elric 	    return;
    121           1.1   elric     }
    122           1.1   elric     add_port (context, family, port, protocol);
    123           1.1   elric }
    124           1.1   elric 
    125           1.1   elric /*
    126           1.1   elric  * add the standard collection of ports for `family'
    127           1.1   elric  */
    128           1.1   elric 
    129           1.1   elric static void
    130       1.1.1.2  pettai add_standard_ports (krb5_context context,
    131           1.1   elric 		    krb5_kdc_configuration *config,
    132           1.1   elric 		    int family)
    133           1.1   elric {
    134           1.1   elric     add_port_service(context, family, "kerberos", 88, "udp");
    135           1.1   elric     add_port_service(context, family, "kerberos", 88, "tcp");
    136           1.1   elric     add_port_service(context, family, "kerberos-sec", 88, "udp");
    137           1.1   elric     add_port_service(context, family, "kerberos-sec", 88, "tcp");
    138           1.1   elric     if(enable_http)
    139           1.1   elric 	add_port_service(context, family, "http", 80, "tcp");
    140           1.1   elric     if(config->enable_kx509) {
    141           1.1   elric 	add_port_service(context, family, "kca_service", 9878, "udp");
    142           1.1   elric 	add_port_service(context, family, "kca_service", 9878, "tcp");
    143           1.1   elric     }
    144           1.1   elric 
    145           1.1   elric }
    146           1.1   elric 
    147           1.1   elric /*
    148           1.1   elric  * parse the set of space-delimited ports in `str' and add them.
    149           1.1   elric  * "+" => all the standard ones
    150           1.1   elric  * otherwise it's port|service[/protocol]
    151           1.1   elric  */
    152           1.1   elric 
    153           1.1   elric static void
    154       1.1.1.2  pettai parse_ports(krb5_context context,
    155           1.1   elric 	    krb5_kdc_configuration *config,
    156           1.1   elric 	    const char *str)
    157           1.1   elric {
    158           1.1   elric     char *pos = NULL;
    159           1.1   elric     char *p;
    160           1.1   elric     char *str_copy = strdup (str);
    161           1.1   elric 
    162           1.1   elric     p = strtok_r(str_copy, " \t", &pos);
    163           1.1   elric     while(p != NULL) {
    164           1.1   elric 	if(strcmp(p, "+") == 0) {
    165           1.1   elric #ifdef HAVE_IPV6
    166           1.1   elric 	    add_standard_ports(context, config, AF_INET6);
    167           1.1   elric #endif
    168           1.1   elric 	    add_standard_ports(context, config, AF_INET);
    169           1.1   elric 	} else {
    170           1.1   elric 	    char *q = strchr(p, '/');
    171           1.1   elric 	    if(q){
    172           1.1   elric 		*q++ = 0;
    173           1.1   elric #ifdef HAVE_IPV6
    174           1.1   elric 		add_port_string(context, AF_INET6, p, q);
    175           1.1   elric #endif
    176           1.1   elric 		add_port_string(context, AF_INET, p, q);
    177           1.1   elric 	    }else {
    178           1.1   elric #ifdef HAVE_IPV6
    179           1.1   elric 		add_port_string(context, AF_INET6, p, "udp");
    180           1.1   elric 		add_port_string(context, AF_INET6, p, "tcp");
    181           1.1   elric #endif
    182           1.1   elric 		add_port_string(context, AF_INET, p, "udp");
    183           1.1   elric 		add_port_string(context, AF_INET, p, "tcp");
    184           1.1   elric 	    }
    185           1.1   elric 	}
    186       1.1.1.2  pettai 
    187           1.1   elric 	p = strtok_r(NULL, " \t", &pos);
    188           1.1   elric     }
    189           1.1   elric     free (str_copy);
    190           1.1   elric }
    191           1.1   elric 
    192           1.1   elric /*
    193           1.1   elric  * every socket we listen on
    194           1.1   elric  */
    195           1.1   elric 
    196           1.1   elric struct descr {
    197           1.1   elric     krb5_socket_t s;
    198           1.1   elric     int type;
    199           1.1   elric     int port;
    200           1.1   elric     unsigned char *buf;
    201           1.1   elric     size_t size;
    202           1.1   elric     size_t len;
    203           1.1   elric     time_t timeout;
    204           1.1   elric     struct sockaddr_storage __ss;
    205           1.1   elric     struct sockaddr *sa;
    206           1.1   elric     socklen_t sock_len;
    207           1.1   elric     char addr_string[128];
    208           1.1   elric };
    209           1.1   elric 
    210           1.1   elric static void
    211           1.1   elric init_descr(struct descr *d)
    212           1.1   elric {
    213           1.1   elric     memset(d, 0, sizeof(*d));
    214           1.1   elric     d->sa = (struct sockaddr *)&d->__ss;
    215           1.1   elric     d->s = rk_INVALID_SOCKET;
    216           1.1   elric }
    217           1.1   elric 
    218           1.1   elric /*
    219           1.1   elric  * re-initialize all `n' ->sa in `d'.
    220           1.1   elric  */
    221           1.1   elric 
    222           1.1   elric static void
    223           1.1   elric reinit_descrs (struct descr *d, int n)
    224           1.1   elric {
    225           1.1   elric     int i;
    226           1.1   elric 
    227           1.1   elric     for (i = 0; i < n; ++i)
    228           1.1   elric 	d[i].sa = (struct sockaddr *)&d[i].__ss;
    229           1.1   elric }
    230           1.1   elric 
    231           1.1   elric /*
    232           1.1   elric  * Create the socket (family, type, port) in `d'
    233           1.1   elric  */
    234           1.1   elric 
    235           1.1   elric static void
    236           1.1   elric init_socket(krb5_context context,
    237           1.1   elric 	    krb5_kdc_configuration *config,
    238           1.1   elric 	    struct descr *d, krb5_address *a, int family, int type, int port)
    239           1.1   elric {
    240           1.1   elric     krb5_error_code ret;
    241           1.1   elric     struct sockaddr_storage __ss;
    242           1.1   elric     struct sockaddr *sa = (struct sockaddr *)&__ss;
    243           1.1   elric     krb5_socklen_t sa_size = sizeof(__ss);
    244           1.1   elric 
    245           1.1   elric     init_descr (d);
    246           1.1   elric 
    247           1.1   elric     ret = krb5_addr2sockaddr (context, a, sa, &sa_size, port);
    248           1.1   elric     if (ret) {
    249           1.1   elric 	krb5_warn(context, ret, "krb5_addr2sockaddr");
    250           1.1   elric 	rk_closesocket(d->s);
    251           1.1   elric 	d->s = rk_INVALID_SOCKET;
    252           1.1   elric 	return;
    253           1.1   elric     }
    254           1.1   elric 
    255           1.1   elric     if (sa->sa_family != family)
    256           1.1   elric 	return;
    257           1.1   elric 
    258           1.1   elric     d->s = socket(family, type, 0);
    259           1.1   elric     if(rk_IS_BAD_SOCKET(d->s)){
    260           1.1   elric 	krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
    261           1.1   elric 	d->s = rk_INVALID_SOCKET;
    262           1.1   elric 	return;
    263           1.1   elric     }
    264  1.1.1.2.10.1  bouyer     rk_cloexec(d->s);
    265           1.1   elric #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
    266           1.1   elric     {
    267           1.1   elric 	int one = 1;
    268           1.1   elric 	setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
    269           1.1   elric     }
    270           1.1   elric #endif
    271           1.1   elric     d->type = type;
    272           1.1   elric     d->port = port;
    273           1.1   elric 
    274  1.1.1.2.10.1  bouyer     socket_set_nonblocking(d->s, 1);
    275  1.1.1.2.10.1  bouyer 
    276           1.1   elric     if(rk_IS_SOCKET_ERROR(bind(d->s, sa, sa_size))){
    277           1.1   elric 	char a_str[256];
    278           1.1   elric 	size_t len;
    279           1.1   elric 
    280           1.1   elric 	krb5_print_address (a, a_str, sizeof(a_str), &len);
    281           1.1   elric 	krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port));
    282           1.1   elric 	rk_closesocket(d->s);
    283           1.1   elric 	d->s = rk_INVALID_SOCKET;
    284           1.1   elric 	return;
    285           1.1   elric     }
    286           1.1   elric     if(type == SOCK_STREAM && rk_IS_SOCKET_ERROR(listen(d->s, SOMAXCONN))){
    287           1.1   elric 	char a_str[256];
    288           1.1   elric 	size_t len;
    289           1.1   elric 
    290           1.1   elric 	krb5_print_address (a, a_str, sizeof(a_str), &len);
    291           1.1   elric 	krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port));
    292           1.1   elric 	rk_closesocket(d->s);
    293           1.1   elric 	d->s = rk_INVALID_SOCKET;
    294           1.1   elric 	return;
    295           1.1   elric     }
    296           1.1   elric }
    297           1.1   elric 
    298           1.1   elric /*
    299           1.1   elric  * Allocate descriptors for all the sockets that we should listen on
    300           1.1   elric  * and return the number of them.
    301           1.1   elric  */
    302           1.1   elric 
    303           1.1   elric static int
    304           1.1   elric init_sockets(krb5_context context,
    305           1.1   elric 	     krb5_kdc_configuration *config,
    306           1.1   elric 	     struct descr **desc)
    307           1.1   elric {
    308           1.1   elric     krb5_error_code ret;
    309       1.1.1.2  pettai     size_t i, j;
    310           1.1   elric     struct descr *d;
    311           1.1   elric     int num = 0;
    312           1.1   elric     krb5_addresses addresses;
    313           1.1   elric 
    314           1.1   elric     if (explicit_addresses.len) {
    315           1.1   elric 	addresses = explicit_addresses;
    316           1.1   elric     } else {
    317           1.1   elric 	ret = krb5_get_all_server_addrs (context, &addresses);
    318           1.1   elric 	if (ret)
    319           1.1   elric 	    krb5_err (context, 1, ret, "krb5_get_all_server_addrs");
    320           1.1   elric     }
    321           1.1   elric     parse_ports(context, config, port_str);
    322           1.1   elric     d = malloc(addresses.len * num_ports * sizeof(*d));
    323           1.1   elric     if (d == NULL)
    324           1.1   elric 	krb5_errx(context, 1, "malloc(%lu) failed",
    325           1.1   elric 		  (unsigned long)num_ports * sizeof(*d));
    326           1.1   elric 
    327           1.1   elric     for (i = 0; i < num_ports; i++){
    328           1.1   elric 	for (j = 0; j < addresses.len; ++j) {
    329           1.1   elric 	    init_socket(context, config, &d[num], &addresses.val[j],
    330           1.1   elric 			ports[i].family, ports[i].type, ports[i].port);
    331           1.1   elric 	    if(d[num].s != rk_INVALID_SOCKET){
    332           1.1   elric 		char a_str[80];
    333           1.1   elric 		size_t len;
    334           1.1   elric 
    335           1.1   elric 		krb5_print_address (&addresses.val[j], a_str,
    336           1.1   elric 				    sizeof(a_str), &len);
    337           1.1   elric 
    338           1.1   elric 		kdc_log(context, config, 5, "listening on %s port %u/%s",
    339           1.1   elric 			a_str,
    340           1.1   elric 			ntohs(ports[i].port),
    341           1.1   elric 			(ports[i].type == SOCK_STREAM) ? "tcp" : "udp");
    342           1.1   elric 		/* XXX */
    343           1.1   elric 		num++;
    344           1.1   elric 	    }
    345           1.1   elric 	}
    346           1.1   elric     }
    347           1.1   elric     krb5_free_addresses (context, &addresses);
    348           1.1   elric     d = realloc(d, num * sizeof(*d));
    349           1.1   elric     if (d == NULL && num != 0)
    350           1.1   elric 	krb5_errx(context, 1, "realloc(%lu) failed",
    351           1.1   elric 		  (unsigned long)num * sizeof(*d));
    352           1.1   elric     reinit_descrs (d, num);
    353           1.1   elric     *desc = d;
    354           1.1   elric     return num;
    355           1.1   elric }
    356           1.1   elric 
    357           1.1   elric /*
    358           1.1   elric  *
    359           1.1   elric  */
    360           1.1   elric 
    361           1.1   elric static const char *
    362           1.1   elric descr_type(struct descr *d)
    363           1.1   elric {
    364           1.1   elric     if (d->type == SOCK_DGRAM)
    365           1.1   elric 	return "udp";
    366           1.1   elric     else if (d->type == SOCK_STREAM)
    367           1.1   elric 	return "tcp";
    368           1.1   elric     return "unknown";
    369           1.1   elric }
    370           1.1   elric 
    371           1.1   elric static void
    372       1.1.1.2  pettai addr_to_string(krb5_context context,
    373           1.1   elric 	       struct sockaddr *addr, size_t addr_len, char *str, size_t len)
    374           1.1   elric {
    375           1.1   elric     krb5_address a;
    376           1.1   elric     if(krb5_sockaddr2address(context, addr, &a) == 0) {
    377           1.1   elric 	if(krb5_print_address(&a, str, len, &len) == 0) {
    378           1.1   elric 	    krb5_free_address(context, &a);
    379           1.1   elric 	    return;
    380           1.1   elric 	}
    381           1.1   elric 	krb5_free_address(context, &a);
    382           1.1   elric     }
    383           1.1   elric     snprintf(str, len, "<family=%d>", addr->sa_family);
    384           1.1   elric }
    385           1.1   elric 
    386           1.1   elric /*
    387           1.1   elric  *
    388           1.1   elric  */
    389           1.1   elric 
    390           1.1   elric static void
    391           1.1   elric send_reply(krb5_context context,
    392           1.1   elric 	   krb5_kdc_configuration *config,
    393           1.1   elric 	   krb5_boolean prependlength,
    394           1.1   elric 	   struct descr *d,
    395           1.1   elric 	   krb5_data *reply)
    396           1.1   elric {
    397           1.1   elric     kdc_log(context, config, 5,
    398           1.1   elric 	    "sending %lu bytes to %s", (unsigned long)reply->length,
    399           1.1   elric 	    d->addr_string);
    400           1.1   elric     if(prependlength){
    401           1.1   elric 	unsigned char l[4];
    402           1.1   elric 	l[0] = (reply->length >> 24) & 0xff;
    403           1.1   elric 	l[1] = (reply->length >> 16) & 0xff;
    404           1.1   elric 	l[2] = (reply->length >> 8) & 0xff;
    405           1.1   elric 	l[3] = reply->length & 0xff;
    406           1.1   elric 	if(rk_IS_SOCKET_ERROR(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len))) {
    407           1.1   elric 	    kdc_log (context, config,
    408           1.1   elric 		     0, "sendto(%s): %s", d->addr_string,
    409           1.1   elric 		     strerror(rk_SOCK_ERRNO));
    410           1.1   elric 	    return;
    411           1.1   elric 	}
    412           1.1   elric     }
    413           1.1   elric     if(rk_IS_SOCKET_ERROR(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len))) {
    414           1.1   elric 	kdc_log (context, config, 0, "sendto(%s): %s", d->addr_string,
    415           1.1   elric 		 strerror(rk_SOCK_ERRNO));
    416           1.1   elric 	return;
    417           1.1   elric     }
    418           1.1   elric }
    419           1.1   elric 
    420           1.1   elric /*
    421           1.1   elric  * Handle the request in `buf, len' to socket `d'
    422           1.1   elric  */
    423           1.1   elric 
    424           1.1   elric static void
    425           1.1   elric do_request(krb5_context context,
    426           1.1   elric 	   krb5_kdc_configuration *config,
    427           1.1   elric 	   void *buf, size_t len, krb5_boolean prependlength,
    428           1.1   elric 	   struct descr *d)
    429           1.1   elric {
    430           1.1   elric     krb5_error_code ret;
    431           1.1   elric     krb5_data reply;
    432           1.1   elric     int datagram_reply = (d->type == SOCK_DGRAM);
    433           1.1   elric 
    434           1.1   elric     krb5_kdc_update_time(NULL);
    435           1.1   elric 
    436           1.1   elric     krb5_data_zero(&reply);
    437           1.1   elric     ret = krb5_kdc_process_request(context, config,
    438           1.1   elric 				   buf, len, &reply, &prependlength,
    439           1.1   elric 				   d->addr_string, d->sa,
    440           1.1   elric 				   datagram_reply);
    441           1.1   elric     if(request_log)
    442           1.1   elric 	krb5_kdc_save_request(context, request_log, buf, len, &reply, d->sa);
    443           1.1   elric     if(reply.length){
    444           1.1   elric 	send_reply(context, config, prependlength, d, &reply);
    445           1.1   elric 	krb5_data_free(&reply);
    446           1.1   elric     }
    447           1.1   elric     if(ret)
    448           1.1   elric 	kdc_log(context, config, 0,
    449           1.1   elric 		"Failed processing %lu byte request from %s",
    450           1.1   elric 		(unsigned long)len, d->addr_string);
    451           1.1   elric }
    452           1.1   elric 
    453           1.1   elric /*
    454           1.1   elric  * Handle incoming data to the UDP socket in `d'
    455           1.1   elric  */
    456           1.1   elric 
    457           1.1   elric static void
    458           1.1   elric handle_udp(krb5_context context,
    459           1.1   elric 	   krb5_kdc_configuration *config,
    460           1.1   elric 	   struct descr *d)
    461           1.1   elric {
    462           1.1   elric     unsigned char *buf;
    463       1.1.1.2  pettai     ssize_t n;
    464           1.1   elric 
    465           1.1   elric     buf = malloc(max_request_udp);
    466  1.1.1.2.10.1  bouyer     if (buf == NULL){
    467  1.1.1.2.10.1  bouyer 	kdc_log(context, config, 0, "Failed to allocate %lu bytes",
    468  1.1.1.2.10.1  bouyer 	        (unsigned long)max_request_udp);
    469           1.1   elric 	return;
    470           1.1   elric     }
    471           1.1   elric 
    472           1.1   elric     d->sock_len = sizeof(d->__ss);
    473           1.1   elric     n = recvfrom(d->s, buf, max_request_udp, 0, d->sa, &d->sock_len);
    474  1.1.1.2.10.1  bouyer     if (rk_IS_SOCKET_ERROR(n)) {
    475  1.1.1.2.10.1  bouyer 	if (rk_SOCK_ERRNO != EAGAIN && rk_SOCK_ERRNO != EINTR)
    476  1.1.1.2.10.1  bouyer 	    krb5_warn(context, rk_SOCK_ERRNO, "recvfrom");
    477  1.1.1.2.10.1  bouyer     } else {
    478           1.1   elric 	addr_to_string (context, d->sa, d->sock_len,
    479           1.1   elric 			d->addr_string, sizeof(d->addr_string));
    480       1.1.1.2  pettai 	if ((size_t)n == max_request_udp) {
    481           1.1   elric 	    krb5_data data;
    482           1.1   elric 	    krb5_warn(context, errno,
    483           1.1   elric 		      "recvfrom: truncated packet from %s, asking for TCP",
    484           1.1   elric 		      d->addr_string);
    485           1.1   elric 	    krb5_mk_error(context,
    486           1.1   elric 			  KRB5KRB_ERR_RESPONSE_TOO_BIG,
    487           1.1   elric 			  NULL,
    488           1.1   elric 			  NULL,
    489           1.1   elric 			  NULL,
    490           1.1   elric 			  NULL,
    491           1.1   elric 			  NULL,
    492           1.1   elric 			  NULL,
    493           1.1   elric 			  &data);
    494           1.1   elric 	    send_reply(context, config, FALSE, d, &data);
    495           1.1   elric 	    krb5_data_free(&data);
    496           1.1   elric 	} else {
    497           1.1   elric 	    do_request(context, config, buf, n, FALSE, d);
    498           1.1   elric 	}
    499           1.1   elric     }
    500           1.1   elric     free (buf);
    501           1.1   elric }
    502           1.1   elric 
    503           1.1   elric static void
    504           1.1   elric clear_descr(struct descr *d)
    505           1.1   elric {
    506           1.1   elric     if(d->buf)
    507           1.1   elric 	memset(d->buf, 0, d->size);
    508           1.1   elric     d->len = 0;
    509           1.1   elric     if(d->s != rk_INVALID_SOCKET)
    510           1.1   elric 	rk_closesocket(d->s);
    511           1.1   elric     d->s = rk_INVALID_SOCKET;
    512           1.1   elric }
    513           1.1   elric 
    514           1.1   elric 
    515           1.1   elric /* remove HTTP %-quoting from buf */
    516           1.1   elric static int
    517           1.1   elric de_http(char *buf)
    518           1.1   elric {
    519           1.1   elric     unsigned char *p, *q;
    520           1.1   elric     for(p = q = (unsigned char *)buf; *p; p++, q++) {
    521           1.1   elric 	if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
    522           1.1   elric 	    unsigned int x;
    523           1.1   elric 	    if(sscanf((char *)p + 1, "%2x", &x) != 1)
    524           1.1   elric 		return -1;
    525           1.1   elric 	    *q = x;
    526           1.1   elric 	    p += 2;
    527           1.1   elric 	} else
    528           1.1   elric 	    *q = *p;
    529           1.1   elric     }
    530           1.1   elric     *q = '\0';
    531           1.1   elric     return 0;
    532           1.1   elric }
    533           1.1   elric 
    534           1.1   elric #define TCP_TIMEOUT 4
    535           1.1   elric 
    536           1.1   elric /*
    537           1.1   elric  * accept a new TCP connection on `d[parent]' and store it in `d[child]'
    538           1.1   elric  */
    539           1.1   elric 
    540           1.1   elric static void
    541           1.1   elric add_new_tcp (krb5_context context,
    542           1.1   elric 	     krb5_kdc_configuration *config,
    543           1.1   elric 	     struct descr *d, int parent, int child)
    544           1.1   elric {
    545           1.1   elric     krb5_socket_t s;
    546           1.1   elric 
    547           1.1   elric     if (child == -1)
    548           1.1   elric 	return;
    549           1.1   elric 
    550           1.1   elric     d[child].sock_len = sizeof(d[child].__ss);
    551           1.1   elric     s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
    552           1.1   elric     if(rk_IS_BAD_SOCKET(s)) {
    553  1.1.1.2.10.1  bouyer 	if (rk_SOCK_ERRNO != EAGAIN && rk_SOCK_ERRNO != EINTR)
    554  1.1.1.2.10.1  bouyer 	    krb5_warn(context, rk_SOCK_ERRNO, "accept");
    555           1.1   elric 	return;
    556           1.1   elric     }
    557           1.1   elric 
    558           1.1   elric #ifdef FD_SETSIZE
    559           1.1   elric     if (s >= FD_SETSIZE) {
    560           1.1   elric 	krb5_warnx(context, "socket FD too large");
    561           1.1   elric 	rk_closesocket (s);
    562           1.1   elric 	return;
    563           1.1   elric     }
    564           1.1   elric #endif
    565           1.1   elric 
    566           1.1   elric     d[child].s = s;
    567           1.1   elric     d[child].timeout = time(NULL) + TCP_TIMEOUT;
    568           1.1   elric     d[child].type = SOCK_STREAM;
    569           1.1   elric     addr_to_string (context,
    570           1.1   elric 		    d[child].sa, d[child].sock_len,
    571           1.1   elric 		    d[child].addr_string, sizeof(d[child].addr_string));
    572           1.1   elric }
    573           1.1   elric 
    574           1.1   elric /*
    575           1.1   elric  * Grow `d' to handle at least `n'.
    576           1.1   elric  * Return != 0 if fails
    577           1.1   elric  */
    578           1.1   elric 
    579           1.1   elric static int
    580           1.1   elric grow_descr (krb5_context context,
    581           1.1   elric 	    krb5_kdc_configuration *config,
    582           1.1   elric 	    struct descr *d, size_t n)
    583           1.1   elric {
    584           1.1   elric     if (d->size - d->len < n) {
    585           1.1   elric 	unsigned char *tmp;
    586           1.1   elric 	size_t grow;
    587           1.1   elric 
    588           1.1   elric 	grow = max(1024, d->len + n);
    589           1.1   elric 	if (d->size + grow > max_request_tcp) {
    590           1.1   elric 	    kdc_log(context, config, 0, "Request exceeds max request size (%lu bytes).",
    591           1.1   elric 		    (unsigned long)d->size + grow);
    592           1.1   elric 	    clear_descr(d);
    593           1.1   elric 	    return -1;
    594           1.1   elric 	}
    595           1.1   elric 	tmp = realloc (d->buf, d->size + grow);
    596           1.1   elric 	if (tmp == NULL) {
    597           1.1   elric 	    kdc_log(context, config, 0, "Failed to re-allocate %lu bytes.",
    598           1.1   elric 		    (unsigned long)d->size + grow);
    599           1.1   elric 	    clear_descr(d);
    600           1.1   elric 	    return -1;
    601           1.1   elric 	}
    602           1.1   elric 	d->size += grow;
    603           1.1   elric 	d->buf = tmp;
    604           1.1   elric     }
    605           1.1   elric     return 0;
    606           1.1   elric }
    607           1.1   elric 
    608           1.1   elric /*
    609           1.1   elric  * Try to handle the TCP data at `d->buf, d->len'.
    610           1.1   elric  * Return -1 if failed, 0 if succesful, and 1 if data is complete.
    611           1.1   elric  */
    612           1.1   elric 
    613           1.1   elric static int
    614           1.1   elric handle_vanilla_tcp (krb5_context context,
    615           1.1   elric 		    krb5_kdc_configuration *config,
    616           1.1   elric 		    struct descr *d)
    617           1.1   elric {
    618           1.1   elric     krb5_storage *sp;
    619           1.1   elric     uint32_t len;
    620           1.1   elric 
    621           1.1   elric     sp = krb5_storage_from_mem(d->buf, d->len);
    622           1.1   elric     if (sp == NULL) {
    623           1.1   elric 	kdc_log (context, config, 0, "krb5_storage_from_mem failed");
    624           1.1   elric 	return -1;
    625           1.1   elric     }
    626           1.1   elric     krb5_ret_uint32(sp, &len);
    627           1.1   elric     krb5_storage_free(sp);
    628           1.1   elric     if(d->len - 4 >= len) {
    629           1.1   elric 	memmove(d->buf, d->buf + 4, d->len - 4);
    630           1.1   elric 	d->len -= 4;
    631           1.1   elric 	return 1;
    632           1.1   elric     }
    633           1.1   elric     return 0;
    634           1.1   elric }
    635           1.1   elric 
    636           1.1   elric /*
    637           1.1   elric  * Try to handle the TCP/HTTP data at `d->buf, d->len'.
    638           1.1   elric  * Return -1 if failed, 0 if succesful, and 1 if data is complete.
    639           1.1   elric  */
    640           1.1   elric 
    641           1.1   elric static int
    642           1.1   elric handle_http_tcp (krb5_context context,
    643           1.1   elric 		 krb5_kdc_configuration *config,
    644           1.1   elric 		 struct descr *d)
    645           1.1   elric {
    646           1.1   elric     char *s, *p, *t;
    647           1.1   elric     void *data;
    648           1.1   elric     char *proto;
    649           1.1   elric     int len;
    650           1.1   elric 
    651           1.1   elric     s = (char *)d->buf;
    652           1.1   elric 
    653           1.1   elric     /* If its a multi line query, truncate off the first line */
    654           1.1   elric     p = strstr(s, "\r\n");
    655           1.1   elric     if (p)
    656           1.1   elric 	*p = 0;
    657           1.1   elric 
    658           1.1   elric     p = NULL;
    659           1.1   elric     t = strtok_r(s, " \t", &p);
    660           1.1   elric     if (t == NULL) {
    661           1.1   elric 	kdc_log(context, config, 0,
    662           1.1   elric 		"Missing HTTP operand (GET) request from %s", d->addr_string);
    663           1.1   elric 	return -1;
    664           1.1   elric     }
    665           1.1   elric 
    666           1.1   elric     t = strtok_r(NULL, " \t", &p);
    667           1.1   elric     if(t == NULL) {
    668           1.1   elric 	kdc_log(context, config, 0,
    669           1.1   elric 		"Missing HTTP GET data in request from %s", d->addr_string);
    670           1.1   elric 	return -1;
    671           1.1   elric     }
    672           1.1   elric 
    673           1.1   elric     data = malloc(strlen(t));
    674           1.1   elric     if (data == NULL) {
    675           1.1   elric 	kdc_log(context, config, 0, "Failed to allocate %lu bytes",
    676           1.1   elric 		(unsigned long)strlen(t));
    677           1.1   elric 	return -1;
    678           1.1   elric     }
    679           1.1   elric     if(*t == '/')
    680           1.1   elric 	t++;
    681           1.1   elric     if(de_http(t) != 0) {
    682           1.1   elric 	kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
    683           1.1   elric 	kdc_log(context, config, 5, "HTTP request: %s", t);
    684           1.1   elric 	free(data);
    685           1.1   elric 	return -1;
    686           1.1   elric     }
    687           1.1   elric     proto = strtok_r(NULL, " \t", &p);
    688           1.1   elric     if (proto == NULL) {
    689           1.1   elric 	kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
    690           1.1   elric 	free(data);
    691           1.1   elric 	return -1;
    692           1.1   elric     }
    693  1.1.1.2.10.1  bouyer     len = rk_base64_decode(t, data);
    694           1.1   elric     if(len <= 0){
    695           1.1   elric 	const char *msg =
    696           1.1   elric 	    " 404 Not found\r\n"
    697           1.1   elric 	    "Server: Heimdal/" VERSION "\r\n"
    698           1.1   elric 	    "Cache-Control: no-cache\r\n"
    699           1.1   elric 	    "Pragma: no-cache\r\n"
    700           1.1   elric 	    "Content-type: text/html\r\n"
    701           1.1   elric 	    "Content-transfer-encoding: 8bit\r\n\r\n"
    702           1.1   elric 	    "<TITLE>404 Not found</TITLE>\r\n"
    703           1.1   elric 	    "<H1>404 Not found</H1>\r\n"
    704           1.1   elric 	    "That page doesn't exist, maybe you are looking for "
    705           1.1   elric 	    "<A HREF=\"http://www.h5l.org/\">Heimdal</A>?\r\n";
    706           1.1   elric 	kdc_log(context, config, 0, "HTTP request from %s is non KDC request", d->addr_string);
    707           1.1   elric 	kdc_log(context, config, 5, "HTTP request: %s", t);
    708           1.1   elric 	free(data);
    709           1.1   elric 	if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) {
    710           1.1   elric 	    kdc_log(context, config, 0, "HTTP write failed: %s: %s",
    711           1.1   elric 		    d->addr_string, strerror(rk_SOCK_ERRNO));
    712           1.1   elric 	    return -1;
    713           1.1   elric 	}
    714           1.1   elric 	if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) {
    715           1.1   elric 	    kdc_log(context, config, 0, "HTTP write failed: %s: %s",
    716           1.1   elric 		    d->addr_string, strerror(rk_SOCK_ERRNO));
    717           1.1   elric 	    return -1;
    718           1.1   elric 	}
    719           1.1   elric 	return -1;
    720           1.1   elric     }
    721           1.1   elric     {
    722           1.1   elric 	const char *msg =
    723           1.1   elric 	    " 200 OK\r\n"
    724           1.1   elric 	    "Server: Heimdal/" VERSION "\r\n"
    725           1.1   elric 	    "Cache-Control: no-cache\r\n"
    726           1.1   elric 	    "Pragma: no-cache\r\n"
    727           1.1   elric 	    "Content-type: application/octet-stream\r\n"
    728           1.1   elric 	    "Content-transfer-encoding: binary\r\n\r\n";
    729           1.1   elric 	if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) {
    730           1.1   elric 	    free(data);
    731           1.1   elric 	    kdc_log(context, config, 0, "HTTP write failed: %s: %s",
    732           1.1   elric 		    d->addr_string, strerror(rk_SOCK_ERRNO));
    733           1.1   elric 	    return -1;
    734           1.1   elric 	}
    735           1.1   elric 	if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) {
    736           1.1   elric 	    free(data);
    737           1.1   elric 	    kdc_log(context, config, 0, "HTTP write failed: %s: %s",
    738           1.1   elric 		    d->addr_string, strerror(rk_SOCK_ERRNO));
    739           1.1   elric 	    return -1;
    740           1.1   elric 	}
    741           1.1   elric     }
    742       1.1.1.2  pettai     if ((size_t)len > d->len)
    743           1.1   elric         len = d->len;
    744           1.1   elric     memcpy(d->buf, data, len);
    745           1.1   elric     d->len = len;
    746           1.1   elric     free(data);
    747           1.1   elric     return 1;
    748           1.1   elric }
    749           1.1   elric 
    750           1.1   elric /*
    751           1.1   elric  * Handle incoming data to the TCP socket in `d[index]'
    752           1.1   elric  */
    753           1.1   elric 
    754           1.1   elric static void
    755           1.1   elric handle_tcp(krb5_context context,
    756           1.1   elric 	   krb5_kdc_configuration *config,
    757           1.1   elric 	   struct descr *d, int idx, int min_free)
    758           1.1   elric {
    759           1.1   elric     unsigned char buf[1024];
    760           1.1   elric     int n;
    761           1.1   elric     int ret = 0;
    762           1.1   elric 
    763           1.1   elric     if (d[idx].timeout == 0) {
    764           1.1   elric 	add_new_tcp (context, config, d, idx, min_free);
    765           1.1   elric 	return;
    766           1.1   elric     }
    767           1.1   elric 
    768           1.1   elric     n = recvfrom(d[idx].s, buf, sizeof(buf), 0, NULL, NULL);
    769           1.1   elric     if(rk_IS_SOCKET_ERROR(n)){
    770           1.1   elric 	krb5_warn(context, rk_SOCK_ERRNO, "recvfrom failed from %s to %s/%d",
    771           1.1   elric 		  d[idx].addr_string, descr_type(d + idx),
    772           1.1   elric 		  ntohs(d[idx].port));
    773           1.1   elric 	return;
    774           1.1   elric     } else if (n == 0) {
    775           1.1   elric 	krb5_warnx(context, "connection closed before end of data after %lu "
    776           1.1   elric 		   "bytes from %s to %s/%d", (unsigned long)d[idx].len,
    777           1.1   elric 		   d[idx].addr_string, descr_type(d + idx),
    778           1.1   elric 		   ntohs(d[idx].port));
    779           1.1   elric 	clear_descr (d + idx);
    780           1.1   elric 	return;
    781           1.1   elric     }
    782           1.1   elric     if (grow_descr (context, config, &d[idx], n))
    783           1.1   elric 	return;
    784           1.1   elric     memcpy(d[idx].buf + d[idx].len, buf, n);
    785           1.1   elric     d[idx].len += n;
    786           1.1   elric     if(d[idx].len > 4 && d[idx].buf[0] == 0) {
    787           1.1   elric 	ret = handle_vanilla_tcp (context, config, &d[idx]);
    788           1.1   elric     } else if(enable_http &&
    789           1.1   elric 	      d[idx].len >= 4 &&
    790           1.1   elric 	      strncmp((char *)d[idx].buf, "GET ", 4) == 0 &&
    791           1.1   elric 	      strncmp((char *)d[idx].buf + d[idx].len - 4,
    792           1.1   elric 		      "\r\n\r\n", 4) == 0) {
    793           1.1   elric 
    794           1.1   elric         /* remove the trailing \r\n\r\n so the string is NUL terminated */
    795           1.1   elric         d[idx].buf[d[idx].len - 4] = '\0';
    796           1.1   elric 
    797           1.1   elric 	ret = handle_http_tcp (context, config, &d[idx]);
    798           1.1   elric 	if (ret < 0)
    799           1.1   elric 	    clear_descr (d + idx);
    800           1.1   elric     } else if (d[idx].len > 4) {
    801           1.1   elric 	kdc_log (context, config,
    802           1.1   elric 		 0, "TCP data of strange type from %s to %s/%d",
    803           1.1   elric 		 d[idx].addr_string, descr_type(d + idx),
    804           1.1   elric 		 ntohs(d[idx].port));
    805           1.1   elric 	if (d[idx].buf[0] & 0x80) {
    806           1.1   elric 	    krb5_data reply;
    807           1.1   elric 
    808           1.1   elric 	    kdc_log (context, config, 0, "TCP extension not supported");
    809           1.1   elric 
    810           1.1   elric 	    ret = krb5_mk_error(context,
    811           1.1   elric 				KRB5KRB_ERR_FIELD_TOOLONG,
    812           1.1   elric 				NULL,
    813           1.1   elric 				NULL,
    814           1.1   elric 				NULL,
    815           1.1   elric 				NULL,
    816           1.1   elric 				NULL,
    817           1.1   elric 				NULL,
    818           1.1   elric 				&reply);
    819           1.1   elric 	    if (ret == 0) {
    820           1.1   elric 		send_reply(context, config, TRUE, d + idx, &reply);
    821           1.1   elric 		krb5_data_free(&reply);
    822           1.1   elric 	    }
    823           1.1   elric 	}
    824           1.1   elric 	clear_descr(d + idx);
    825           1.1   elric 	return;
    826           1.1   elric     }
    827           1.1   elric     if (ret < 0)
    828           1.1   elric 	return;
    829           1.1   elric     else if (ret == 1) {
    830           1.1   elric 	do_request(context, config,
    831           1.1   elric 		   d[idx].buf, d[idx].len, TRUE, &d[idx]);
    832           1.1   elric 	clear_descr(d + idx);
    833           1.1   elric     }
    834           1.1   elric }
    835           1.1   elric 
    836  1.1.1.2.10.1  bouyer #ifdef HAVE_FORK
    837  1.1.1.2.10.1  bouyer static void
    838  1.1.1.2.10.1  bouyer handle_islive(int fd)
    839  1.1.1.2.10.1  bouyer {
    840  1.1.1.2.10.1  bouyer     char buf;
    841  1.1.1.2.10.1  bouyer     int ret;
    842  1.1.1.2.10.1  bouyer 
    843  1.1.1.2.10.1  bouyer     ret = read(fd, &buf, 1);
    844  1.1.1.2.10.1  bouyer     if (ret != 1)
    845  1.1.1.2.10.1  bouyer 	exit_flag = -1;
    846  1.1.1.2.10.1  bouyer }
    847  1.1.1.2.10.1  bouyer #endif
    848  1.1.1.2.10.1  bouyer 
    849       1.1.1.2  pettai krb5_boolean
    850       1.1.1.2  pettai realloc_descrs(struct descr **d, unsigned int *ndescr)
    851       1.1.1.2  pettai {
    852       1.1.1.2  pettai     struct descr *tmp;
    853       1.1.1.2  pettai     size_t i;
    854       1.1.1.2  pettai 
    855       1.1.1.2  pettai     tmp = realloc(*d, (*ndescr + 4) * sizeof(**d));
    856       1.1.1.2  pettai     if(tmp == NULL)
    857       1.1.1.2  pettai         return FALSE;
    858       1.1.1.2  pettai 
    859       1.1.1.2  pettai     *d = tmp;
    860       1.1.1.2  pettai     reinit_descrs (*d, *ndescr);
    861       1.1.1.2  pettai     memset(*d + *ndescr, 0, 4 * sizeof(**d));
    862       1.1.1.2  pettai     for(i = *ndescr; i < *ndescr + 4; i++)
    863       1.1.1.2  pettai         init_descr (*d + i);
    864       1.1.1.2  pettai 
    865       1.1.1.2  pettai     *ndescr += 4;
    866       1.1.1.2  pettai 
    867       1.1.1.2  pettai     return TRUE;
    868       1.1.1.2  pettai }
    869       1.1.1.2  pettai 
    870       1.1.1.2  pettai int
    871       1.1.1.2  pettai next_min_free(krb5_context context, struct descr **d, unsigned int *ndescr)
    872       1.1.1.2  pettai {
    873       1.1.1.2  pettai     size_t i;
    874       1.1.1.2  pettai     int min_free;
    875       1.1.1.2  pettai 
    876       1.1.1.2  pettai     for(i = 0; i < *ndescr; i++) {
    877       1.1.1.2  pettai         int s = (*d + i)->s;
    878       1.1.1.2  pettai         if(rk_IS_BAD_SOCKET(s))
    879       1.1.1.2  pettai             return i;
    880       1.1.1.2  pettai     }
    881       1.1.1.2  pettai 
    882       1.1.1.2  pettai     min_free = *ndescr;
    883       1.1.1.2  pettai     if(!realloc_descrs(d, ndescr)) {
    884       1.1.1.2  pettai         min_free = -1;
    885       1.1.1.2  pettai         krb5_warnx(context, "No memory");
    886       1.1.1.2  pettai     }
    887       1.1.1.2  pettai 
    888       1.1.1.2  pettai     return min_free;
    889       1.1.1.2  pettai }
    890       1.1.1.2  pettai 
    891  1.1.1.2.10.1  bouyer static void
    892  1.1.1.2.10.1  bouyer loop(krb5_context context, krb5_kdc_configuration *config,
    893  1.1.1.2.10.1  bouyer      struct descr *d, unsigned int ndescr, int islive)
    894           1.1   elric {
    895           1.1   elric 
    896  1.1.1.2.10.1  bouyer     while (exit_flag == 0) {
    897           1.1   elric 	struct timeval tmout;
    898           1.1   elric 	fd_set fds;
    899           1.1   elric 	int min_free = -1;
    900           1.1   elric 	int max_fd = 0;
    901       1.1.1.2  pettai 	size_t i;
    902           1.1   elric 
    903           1.1   elric 	FD_ZERO(&fds);
    904  1.1.1.2.10.1  bouyer         if (islive > -1) {
    905  1.1.1.2.10.1  bouyer             FD_SET(islive, &fds);
    906  1.1.1.2.10.1  bouyer             max_fd = islive;
    907  1.1.1.2.10.1  bouyer         }
    908  1.1.1.2.10.1  bouyer 	for (i = 0; i < ndescr; i++) {
    909  1.1.1.2.10.1  bouyer 	    if (!rk_IS_BAD_SOCKET(d[i].s)) {
    910  1.1.1.2.10.1  bouyer 		if (d[i].type == SOCK_STREAM &&
    911           1.1   elric 		   d[i].timeout && d[i].timeout < time(NULL)) {
    912           1.1   elric 		    kdc_log(context, config, 1,
    913           1.1   elric 			    "TCP-connection from %s expired after %lu bytes",
    914           1.1   elric 			    d[i].addr_string, (unsigned long)d[i].len);
    915           1.1   elric 		    clear_descr(&d[i]);
    916           1.1   elric 		    continue;
    917           1.1   elric 		}
    918           1.1   elric #ifndef NO_LIMIT_FD_SETSIZE
    919  1.1.1.2.10.1  bouyer 		if (max_fd < d[i].s)
    920           1.1   elric 		    max_fd = d[i].s;
    921           1.1   elric #ifdef FD_SETSIZE
    922           1.1   elric 		if (max_fd >= FD_SETSIZE)
    923           1.1   elric 		    krb5_errx(context, 1, "fd too large");
    924           1.1   elric #endif
    925           1.1   elric #endif
    926           1.1   elric 		FD_SET(d[i].s, &fds);
    927           1.1   elric 	    }
    928           1.1   elric 	}
    929           1.1   elric 
    930           1.1   elric 	tmout.tv_sec = TCP_TIMEOUT;
    931           1.1   elric 	tmout.tv_usec = 0;
    932           1.1   elric 	switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
    933           1.1   elric 	case 0:
    934           1.1   elric 	    break;
    935           1.1   elric 	case -1:
    936           1.1   elric 	    if (errno != EINTR)
    937           1.1   elric 		krb5_warn(context, rk_SOCK_ERRNO, "select");
    938           1.1   elric 	    break;
    939           1.1   elric 	default:
    940  1.1.1.2.10.1  bouyer #ifdef HAVE_FORK
    941  1.1.1.2.10.1  bouyer 	    if (islive > -1 && FD_ISSET(islive, &fds))
    942  1.1.1.2.10.1  bouyer 		handle_islive(islive);
    943  1.1.1.2.10.1  bouyer #endif
    944  1.1.1.2.10.1  bouyer 	    for (i = 0; i < ndescr; i++)
    945  1.1.1.2.10.1  bouyer 		if (!rk_IS_BAD_SOCKET(d[i].s) && FD_ISSET(d[i].s, &fds)) {
    946  1.1.1.2.10.1  bouyer 		    min_free = next_min_free(context, &d, &ndescr);
    947  1.1.1.2.10.1  bouyer 
    948  1.1.1.2.10.1  bouyer 		    if (d[i].type == SOCK_DGRAM)
    949  1.1.1.2.10.1  bouyer 			handle_udp(context, config, &d[i]);
    950  1.1.1.2.10.1  bouyer 		    else if (d[i].type == SOCK_STREAM)
    951  1.1.1.2.10.1  bouyer 			handle_tcp(context, config, d, i, min_free);
    952           1.1   elric 		}
    953           1.1   elric 	}
    954           1.1   elric     }
    955  1.1.1.2.10.1  bouyer 
    956  1.1.1.2.10.1  bouyer     switch (exit_flag) {
    957  1.1.1.2.10.1  bouyer     case -1:
    958  1.1.1.2.10.1  bouyer 	kdc_log(context, config, 0,
    959  1.1.1.2.10.1  bouyer                 "KDC worker process exiting because KDC master exited.");
    960  1.1.1.2.10.1  bouyer 	break;
    961           1.1   elric #ifdef SIGXCPU
    962  1.1.1.2.10.1  bouyer     case SIGXCPU:
    963           1.1   elric 	kdc_log(context, config, 0, "CPU time limit exceeded");
    964  1.1.1.2.10.1  bouyer 	break;
    965           1.1   elric #endif
    966  1.1.1.2.10.1  bouyer     case SIGINT:
    967  1.1.1.2.10.1  bouyer     case SIGTERM:
    968           1.1   elric 	kdc_log(context, config, 0, "Terminated");
    969  1.1.1.2.10.1  bouyer 	break;
    970  1.1.1.2.10.1  bouyer     default:
    971           1.1   elric 	kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag);
    972  1.1.1.2.10.1  bouyer 	break;
    973  1.1.1.2.10.1  bouyer     }
    974  1.1.1.2.10.1  bouyer }
    975  1.1.1.2.10.1  bouyer 
    976  1.1.1.2.10.1  bouyer #ifdef __APPLE__
    977  1.1.1.2.10.1  bouyer static void
    978  1.1.1.2.10.1  bouyer bonjour_kid(krb5_context context, krb5_kdc_configuration *config, const char *argv0, int *islive)
    979  1.1.1.2.10.1  bouyer {
    980  1.1.1.2.10.1  bouyer     char buf;
    981  1.1.1.2.10.1  bouyer 
    982  1.1.1.2.10.1  bouyer     if (do_bonjour > 0) {
    983  1.1.1.2.10.1  bouyer 	bonjour_announce(context, config);
    984  1.1.1.2.10.1  bouyer 
    985  1.1.1.2.10.1  bouyer 	while (read(0, &buf, 1) == 1)
    986  1.1.1.2.10.1  bouyer 	    continue;
    987  1.1.1.2.10.1  bouyer 	_exit(0);
    988  1.1.1.2.10.1  bouyer     }
    989  1.1.1.2.10.1  bouyer 
    990  1.1.1.2.10.1  bouyer     if ((bonjour_pid = fork()) != 0)
    991  1.1.1.2.10.1  bouyer 	return;
    992  1.1.1.2.10.1  bouyer 
    993  1.1.1.2.10.1  bouyer     close(islive[0]);
    994  1.1.1.2.10.1  bouyer     if (dup2(islive[1], 0) == -1)
    995  1.1.1.2.10.1  bouyer 	err(1, "failed to announce with bonjour (dup)");
    996  1.1.1.2.10.1  bouyer     if (islive[1] != 0)
    997  1.1.1.2.10.1  bouyer         close(islive[1]);
    998  1.1.1.2.10.1  bouyer     execlp(argv0, "kdc", "--bonjour", NULL);
    999  1.1.1.2.10.1  bouyer     err(1, "failed to announce with bonjour (exec)");
   1000  1.1.1.2.10.1  bouyer }
   1001  1.1.1.2.10.1  bouyer #endif
   1002  1.1.1.2.10.1  bouyer 
   1003  1.1.1.2.10.1  bouyer #ifdef HAVE_FORK
   1004  1.1.1.2.10.1  bouyer static void
   1005  1.1.1.2.10.1  bouyer kill_kids(pid_t *pids, int max_kids, int sig)
   1006  1.1.1.2.10.1  bouyer {
   1007  1.1.1.2.10.1  bouyer     int i;
   1008  1.1.1.2.10.1  bouyer 
   1009  1.1.1.2.10.1  bouyer     for (i=0; i < max_kids; i++)
   1010  1.1.1.2.10.1  bouyer 	if (pids[i] > 0)
   1011  1.1.1.2.10.1  bouyer 	    kill(sig, pids[i]);
   1012  1.1.1.2.10.1  bouyer     if (bonjour_pid > 0)
   1013  1.1.1.2.10.1  bouyer         kill(sig, bonjour_pid);
   1014  1.1.1.2.10.1  bouyer }
   1015  1.1.1.2.10.1  bouyer 
   1016  1.1.1.2.10.1  bouyer static int
   1017  1.1.1.2.10.1  bouyer reap_kid(krb5_context context, krb5_kdc_configuration *config,
   1018  1.1.1.2.10.1  bouyer 	 pid_t *pids, int max_kids, int options)
   1019  1.1.1.2.10.1  bouyer {
   1020  1.1.1.2.10.1  bouyer     pid_t pid;
   1021  1.1.1.2.10.1  bouyer     char *what;
   1022  1.1.1.2.10.1  bouyer     int status;
   1023  1.1.1.2.10.1  bouyer     int i = 0; /* quiet warnings */
   1024  1.1.1.2.10.1  bouyer 
   1025  1.1.1.2.10.1  bouyer     pid = waitpid(-1, &status, options);
   1026  1.1.1.2.10.1  bouyer     if (pid < 1)
   1027  1.1.1.2.10.1  bouyer 	return 0;
   1028  1.1.1.2.10.1  bouyer 
   1029  1.1.1.2.10.1  bouyer     if (pid != bonjour_pid) {
   1030  1.1.1.2.10.1  bouyer         for (i=0; i < max_kids; i++) {
   1031  1.1.1.2.10.1  bouyer             if (pids[i] == pid)
   1032  1.1.1.2.10.1  bouyer                 break;
   1033  1.1.1.2.10.1  bouyer         }
   1034  1.1.1.2.10.1  bouyer 
   1035  1.1.1.2.10.1  bouyer         if (i == max_kids) {
   1036  1.1.1.2.10.1  bouyer             /* XXXrcd: this should not happen, have to do something, though */
   1037  1.1.1.2.10.1  bouyer             return 0;
   1038  1.1.1.2.10.1  bouyer         }
   1039  1.1.1.2.10.1  bouyer     }
   1040  1.1.1.2.10.1  bouyer 
   1041  1.1.1.2.10.1  bouyer     if (pid == bonjour_pid)
   1042  1.1.1.2.10.1  bouyer         what = "bonjour";
   1043  1.1.1.2.10.1  bouyer     else
   1044  1.1.1.2.10.1  bouyer         what = "worker";
   1045  1.1.1.2.10.1  bouyer     if (WIFEXITED(status))
   1046  1.1.1.2.10.1  bouyer         kdc_log(context, config, 0, "KDC reaped %s process: %d, exit status: %d",
   1047  1.1.1.2.10.1  bouyer                 what, (int)pid, WEXITSTATUS(status));
   1048  1.1.1.2.10.1  bouyer     else if (WIFSIGNALED(status))
   1049  1.1.1.2.10.1  bouyer         kdc_log(context, config, 0, "KDC reaped %s process: %d, term signal %d%s",
   1050  1.1.1.2.10.1  bouyer                 what, (int)pid, WTERMSIG(status),
   1051  1.1.1.2.10.1  bouyer                 WCOREDUMP(status) ? " (core dumped)" : "");
   1052  1.1.1.2.10.1  bouyer     else
   1053  1.1.1.2.10.1  bouyer         kdc_log(context, config, 0, "KDC reaped %s process: %d",
   1054  1.1.1.2.10.1  bouyer                 what, (int)pid);
   1055  1.1.1.2.10.1  bouyer     if (pid == bonjour_pid) {
   1056  1.1.1.2.10.1  bouyer         bonjour_pid = (pid_t)-1;
   1057  1.1.1.2.10.1  bouyer         return 0;
   1058  1.1.1.2.10.1  bouyer     } else {
   1059  1.1.1.2.10.1  bouyer         pids[i] = (pid_t)-1;
   1060  1.1.1.2.10.1  bouyer         return 1;
   1061  1.1.1.2.10.1  bouyer     }
   1062  1.1.1.2.10.1  bouyer }
   1063  1.1.1.2.10.1  bouyer 
   1064  1.1.1.2.10.1  bouyer static int
   1065  1.1.1.2.10.1  bouyer reap_kids(krb5_context context, krb5_kdc_configuration *config,
   1066  1.1.1.2.10.1  bouyer 	  pid_t *pids, int max_kids)
   1067  1.1.1.2.10.1  bouyer {
   1068  1.1.1.2.10.1  bouyer     int reaped = 0;
   1069  1.1.1.2.10.1  bouyer 
   1070  1.1.1.2.10.1  bouyer     for (;;) {
   1071  1.1.1.2.10.1  bouyer 	if (reap_kid(context, config, pids, max_kids, WNOHANG) == 0)
   1072  1.1.1.2.10.1  bouyer 	    break;
   1073  1.1.1.2.10.1  bouyer 	reaped++;
   1074  1.1.1.2.10.1  bouyer     }
   1075  1.1.1.2.10.1  bouyer 
   1076  1.1.1.2.10.1  bouyer     return reaped;
   1077  1.1.1.2.10.1  bouyer }
   1078  1.1.1.2.10.1  bouyer 
   1079  1.1.1.2.10.1  bouyer static void
   1080  1.1.1.2.10.1  bouyer select_sleep(int microseconds)
   1081  1.1.1.2.10.1  bouyer {
   1082  1.1.1.2.10.1  bouyer     struct timeval tv;
   1083  1.1.1.2.10.1  bouyer 
   1084  1.1.1.2.10.1  bouyer     tv.tv_sec = microseconds / 1000000;
   1085  1.1.1.2.10.1  bouyer     tv.tv_usec = microseconds % 1000000;
   1086  1.1.1.2.10.1  bouyer     select(0, NULL, NULL, NULL, &tv);
   1087  1.1.1.2.10.1  bouyer }
   1088  1.1.1.2.10.1  bouyer #endif
   1089  1.1.1.2.10.1  bouyer 
   1090  1.1.1.2.10.1  bouyer void
   1091  1.1.1.2.10.1  bouyer start_kdc(krb5_context context,
   1092  1.1.1.2.10.1  bouyer 	  krb5_kdc_configuration *config, const char *argv0)
   1093  1.1.1.2.10.1  bouyer {
   1094  1.1.1.2.10.1  bouyer     struct timeval tv1;
   1095  1.1.1.2.10.1  bouyer     struct timeval tv2;
   1096  1.1.1.2.10.1  bouyer     struct descr *d;
   1097  1.1.1.2.10.1  bouyer     unsigned int ndescr;
   1098  1.1.1.2.10.1  bouyer     pid_t pid = -1;
   1099  1.1.1.2.10.1  bouyer #ifdef HAVE_FORK
   1100  1.1.1.2.10.1  bouyer     pid_t *pids;
   1101  1.1.1.2.10.1  bouyer     int max_kdcs = config->num_kdc_processes;
   1102  1.1.1.2.10.1  bouyer     int num_kdcs = 0;
   1103  1.1.1.2.10.1  bouyer     int i;
   1104  1.1.1.2.10.1  bouyer     int islive[2];
   1105  1.1.1.2.10.1  bouyer #endif
   1106  1.1.1.2.10.1  bouyer 
   1107  1.1.1.2.10.1  bouyer #ifdef __APPLE__
   1108  1.1.1.2.10.1  bouyer     if (do_bonjour > 0)
   1109  1.1.1.2.10.1  bouyer         bonjour_kid(context, config, argv0, NULL);
   1110  1.1.1.2.10.1  bouyer #endif
   1111  1.1.1.2.10.1  bouyer 
   1112  1.1.1.2.10.1  bouyer #ifdef HAVE_FORK
   1113  1.1.1.2.10.1  bouyer #ifdef _SC_NPROCESSORS_ONLN
   1114  1.1.1.2.10.1  bouyer     if (max_kdcs < 1)
   1115  1.1.1.2.10.1  bouyer 	max_kdcs = sysconf(_SC_NPROCESSORS_ONLN);
   1116  1.1.1.2.10.1  bouyer #endif
   1117  1.1.1.2.10.1  bouyer 
   1118  1.1.1.2.10.1  bouyer     if (max_kdcs < 1)
   1119  1.1.1.2.10.1  bouyer 	max_kdcs = 1;
   1120  1.1.1.2.10.1  bouyer 
   1121  1.1.1.2.10.1  bouyer     pids = calloc(max_kdcs, sizeof(*pids));
   1122  1.1.1.2.10.1  bouyer     if (!pids)
   1123  1.1.1.2.10.1  bouyer 	krb5_err(context, 1, errno, "malloc");
   1124  1.1.1.2.10.1  bouyer 
   1125  1.1.1.2.10.1  bouyer     /*
   1126  1.1.1.2.10.1  bouyer      * We open a socketpair of which we hand one end to each of our kids.
   1127  1.1.1.2.10.1  bouyer      * When we exit, for whatever reason, the children will notice an EOF
   1128  1.1.1.2.10.1  bouyer      * on their end and be able to cleanly exit.
   1129  1.1.1.2.10.1  bouyer      */
   1130  1.1.1.2.10.1  bouyer 
   1131  1.1.1.2.10.1  bouyer     if (socketpair(PF_LOCAL, SOCK_STREAM, 0, islive) == -1)
   1132  1.1.1.2.10.1  bouyer 	krb5_errx(context, 1, "socketpair");
   1133  1.1.1.2.10.1  bouyer     socket_set_nonblocking(islive[1], 1);
   1134  1.1.1.2.10.1  bouyer #endif
   1135  1.1.1.2.10.1  bouyer 
   1136  1.1.1.2.10.1  bouyer     ndescr = init_sockets(context, config, &d);
   1137  1.1.1.2.10.1  bouyer     if(ndescr <= 0)
   1138  1.1.1.2.10.1  bouyer 	krb5_errx(context, 1, "No sockets!");
   1139  1.1.1.2.10.1  bouyer 
   1140  1.1.1.2.10.1  bouyer #ifdef HAVE_FORK
   1141  1.1.1.2.10.1  bouyer 
   1142  1.1.1.2.10.1  bouyer # ifdef __APPLE__
   1143  1.1.1.2.10.1  bouyer     if (do_bonjour < 0)
   1144  1.1.1.2.10.1  bouyer         bonjour_kid(context, config, argv0, islive);
   1145  1.1.1.2.10.1  bouyer # endif
   1146  1.1.1.2.10.1  bouyer 
   1147  1.1.1.2.10.1  bouyer     kdc_log(context, config, 0, "KDC started master process pid=%d", getpid());
   1148  1.1.1.2.10.1  bouyer #else
   1149  1.1.1.2.10.1  bouyer     kdc_log(context, config, 0, "KDC started pid=%d", getpid());
   1150  1.1.1.2.10.1  bouyer #endif
   1151  1.1.1.2.10.1  bouyer 
   1152  1.1.1.2.10.1  bouyer     roken_detach_finish(NULL, daemon_child);
   1153  1.1.1.2.10.1  bouyer 
   1154  1.1.1.2.10.1  bouyer     tv1.tv_sec  = 0;
   1155  1.1.1.2.10.1  bouyer     tv1.tv_usec = 0;
   1156  1.1.1.2.10.1  bouyer 
   1157  1.1.1.2.10.1  bouyer #ifdef HAVE_FORK
   1158  1.1.1.2.10.1  bouyer     if (!testing_flag) {
   1159  1.1.1.2.10.1  bouyer         /* Note that we might never execute the body of this loop */
   1160  1.1.1.2.10.1  bouyer         while (exit_flag == 0) {
   1161  1.1.1.2.10.1  bouyer 
   1162  1.1.1.2.10.1  bouyer             /* Slow down the creation of KDCs... */
   1163  1.1.1.2.10.1  bouyer 
   1164  1.1.1.2.10.1  bouyer             gettimeofday(&tv2, NULL);
   1165  1.1.1.2.10.1  bouyer             if (tv1.tv_sec == tv2.tv_sec && tv2.tv_usec - tv1.tv_usec < 25000) {
   1166  1.1.1.2.10.1  bouyer #if 0	/* XXXrcd: should print a message... */
   1167  1.1.1.2.10.1  bouyer                 kdc_log(context, config, 0, "Spawning KDCs too quickly, "
   1168  1.1.1.2.10.1  bouyer                     "pausing for 50ms");
   1169  1.1.1.2.10.1  bouyer #endif
   1170  1.1.1.2.10.1  bouyer                 select_sleep(12500);
   1171  1.1.1.2.10.1  bouyer                 continue;
   1172  1.1.1.2.10.1  bouyer             }
   1173  1.1.1.2.10.1  bouyer 
   1174  1.1.1.2.10.1  bouyer             if (num_kdcs >= max_kdcs) {
   1175  1.1.1.2.10.1  bouyer                 num_kdcs -= reap_kid(context, config, pids, max_kdcs, 0);
   1176  1.1.1.2.10.1  bouyer                 continue;
   1177  1.1.1.2.10.1  bouyer             }
   1178  1.1.1.2.10.1  bouyer 
   1179  1.1.1.2.10.1  bouyer             if (num_kdcs > 0)
   1180  1.1.1.2.10.1  bouyer                 num_kdcs -= reap_kids(context, config, pids, max_kdcs);
   1181  1.1.1.2.10.1  bouyer 
   1182  1.1.1.2.10.1  bouyer             pid = fork();
   1183  1.1.1.2.10.1  bouyer             switch (pid) {
   1184  1.1.1.2.10.1  bouyer             case 0:
   1185  1.1.1.2.10.1  bouyer                 close(islive[0]);
   1186  1.1.1.2.10.1  bouyer                 loop(context, config, d, ndescr, islive[1]);
   1187  1.1.1.2.10.1  bouyer                 exit(0);
   1188  1.1.1.2.10.1  bouyer             case -1:
   1189  1.1.1.2.10.1  bouyer                 /* XXXrcd: hmmm, do something useful?? */
   1190  1.1.1.2.10.1  bouyer                 kdc_log(context, config, 0,
   1191  1.1.1.2.10.1  bouyer                         "KDC master process could not fork worker process");
   1192  1.1.1.2.10.1  bouyer                 sleep(10);
   1193  1.1.1.2.10.1  bouyer                 break;
   1194  1.1.1.2.10.1  bouyer             default:
   1195  1.1.1.2.10.1  bouyer                 for (i=0; i < max_kdcs; i++) {
   1196  1.1.1.2.10.1  bouyer                     if (pids[i] == 0) {
   1197  1.1.1.2.10.1  bouyer                         pids[i] = pid;
   1198  1.1.1.2.10.1  bouyer                         break;
   1199  1.1.1.2.10.1  bouyer                     }
   1200  1.1.1.2.10.1  bouyer                 }
   1201  1.1.1.2.10.1  bouyer                 kdc_log(context, config, 0, "KDC worker process started: %d",
   1202  1.1.1.2.10.1  bouyer                         pid);
   1203  1.1.1.2.10.1  bouyer                 num_kdcs++;
   1204  1.1.1.2.10.1  bouyer                 gettimeofday(&tv1, NULL);
   1205  1.1.1.2.10.1  bouyer                 break;
   1206  1.1.1.2.10.1  bouyer             }
   1207  1.1.1.2.10.1  bouyer         }
   1208  1.1.1.2.10.1  bouyer 
   1209  1.1.1.2.10.1  bouyer         /* Closing these sockets should cause the kids to die... */
   1210  1.1.1.2.10.1  bouyer 
   1211  1.1.1.2.10.1  bouyer         close(islive[0]);
   1212  1.1.1.2.10.1  bouyer         close(islive[1]);
   1213  1.1.1.2.10.1  bouyer 
   1214  1.1.1.2.10.1  bouyer         /* Close our listener sockets before terminating workers */
   1215  1.1.1.2.10.1  bouyer         for (i = 0; i < ndescr; ++i)
   1216  1.1.1.2.10.1  bouyer             clear_descr(&d[i]);
   1217  1.1.1.2.10.1  bouyer 
   1218  1.1.1.2.10.1  bouyer         gettimeofday(&tv1, NULL);
   1219  1.1.1.2.10.1  bouyer         tv2 = tv1;
   1220  1.1.1.2.10.1  bouyer 
   1221  1.1.1.2.10.1  bouyer         /* Reap every 10ms, terminate stragglers once a second, give up after 10 */
   1222  1.1.1.2.10.1  bouyer         for (;;) {
   1223  1.1.1.2.10.1  bouyer             struct timeval tv3;
   1224  1.1.1.2.10.1  bouyer             num_kdcs -= reap_kids(context, config, pids, max_kdcs);
   1225  1.1.1.2.10.1  bouyer             if (num_kdcs == 0 && bonjour_pid <= 0)
   1226  1.1.1.2.10.1  bouyer                 goto end;
   1227  1.1.1.2.10.1  bouyer             /*
   1228  1.1.1.2.10.1  bouyer              * Using select to sleep will fail with EINTR if we receive a
   1229  1.1.1.2.10.1  bouyer              * SIGCHLD.  This is desirable.
   1230  1.1.1.2.10.1  bouyer              */
   1231  1.1.1.2.10.1  bouyer             select_sleep(10000);
   1232  1.1.1.2.10.1  bouyer             gettimeofday(&tv3, NULL);
   1233  1.1.1.2.10.1  bouyer             if (tv3.tv_sec - tv1.tv_sec > 10 ||
   1234  1.1.1.2.10.1  bouyer                 (tv3.tv_sec - tv1.tv_sec == 10 && tv3.tv_usec >= tv1.tv_usec))
   1235  1.1.1.2.10.1  bouyer                 break;
   1236  1.1.1.2.10.1  bouyer             if (tv3.tv_sec - tv2.tv_sec > 1 ||
   1237  1.1.1.2.10.1  bouyer                 (tv3.tv_sec - tv2.tv_sec == 1 && tv3.tv_usec >= tv2.tv_usec)) {
   1238  1.1.1.2.10.1  bouyer                 kill_kids(pids, max_kdcs, SIGTERM);
   1239  1.1.1.2.10.1  bouyer                 tv2 = tv3;
   1240  1.1.1.2.10.1  bouyer             }
   1241  1.1.1.2.10.1  bouyer         }
   1242  1.1.1.2.10.1  bouyer 
   1243  1.1.1.2.10.1  bouyer         /* Kill stragglers and reap every 200ms, give up after 15s */
   1244  1.1.1.2.10.1  bouyer         for (;;) {
   1245  1.1.1.2.10.1  bouyer             kill_kids(pids, max_kdcs, SIGKILL);
   1246  1.1.1.2.10.1  bouyer             num_kdcs -= reap_kids(context, config, pids, max_kdcs);
   1247  1.1.1.2.10.1  bouyer             if (num_kdcs == 0 && bonjour_pid <= 0)
   1248  1.1.1.2.10.1  bouyer                 break;
   1249  1.1.1.2.10.1  bouyer             select_sleep(200000);
   1250  1.1.1.2.10.1  bouyer             gettimeofday(&tv2, NULL);
   1251  1.1.1.2.10.1  bouyer             if (tv2.tv_sec - tv1.tv_sec > 15 ||
   1252  1.1.1.2.10.1  bouyer                 (tv2.tv_sec - tv1.tv_sec == 15 && tv2.tv_usec >= tv1.tv_usec))
   1253  1.1.1.2.10.1  bouyer                 break;
   1254  1.1.1.2.10.1  bouyer         }
   1255  1.1.1.2.10.1  bouyer 
   1256  1.1.1.2.10.1  bouyer      end:
   1257  1.1.1.2.10.1  bouyer         kdc_log(context, config, 0, "KDC master process exiting", pid);
   1258  1.1.1.2.10.1  bouyer         free(pids);
   1259  1.1.1.2.10.1  bouyer     } else {
   1260  1.1.1.2.10.1  bouyer         loop(context, config, d, ndescr, -1);
   1261  1.1.1.2.10.1  bouyer         kdc_log(context, config, 0, "KDC exiting", pid);
   1262  1.1.1.2.10.1  bouyer     }
   1263  1.1.1.2.10.1  bouyer #else
   1264  1.1.1.2.10.1  bouyer     loop(context, config, d, ndescr, -1);
   1265  1.1.1.2.10.1  bouyer     kdc_log(context, config, 0, "KDC exiting", pid);
   1266  1.1.1.2.10.1  bouyer #endif
   1267  1.1.1.2.10.1  bouyer 
   1268  1.1.1.2.10.1  bouyer     free(d);
   1269           1.1   elric }
   1270