Home | History | Annotate | Line # | Download | only in kdc
connect.c revision 1.1
      1  1.1  elric /*	$NetBSD: connect.c,v 1.1 2011/04/13 18:14:36 elric 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 /* Should we enable the HTTP hack? */
     39  1.1  elric int enable_http = -1;
     40  1.1  elric 
     41  1.1  elric /* Log over requests to the KDC */
     42  1.1  elric const char *request_log;
     43  1.1  elric 
     44  1.1  elric /* A string describing on what ports to listen */
     45  1.1  elric const char *port_str;
     46  1.1  elric 
     47  1.1  elric krb5_addresses explicit_addresses;
     48  1.1  elric 
     49  1.1  elric size_t max_request_udp;
     50  1.1  elric size_t max_request_tcp;
     51  1.1  elric 
     52  1.1  elric /*
     53  1.1  elric  * a tuple describing on what to listen
     54  1.1  elric  */
     55  1.1  elric 
     56  1.1  elric struct port_desc{
     57  1.1  elric     int family;
     58  1.1  elric     int type;
     59  1.1  elric     int port;
     60  1.1  elric };
     61  1.1  elric 
     62  1.1  elric /* the current ones */
     63  1.1  elric 
     64  1.1  elric static struct port_desc *ports;
     65  1.1  elric static int num_ports;
     66  1.1  elric 
     67  1.1  elric /*
     68  1.1  elric  * add `family, port, protocol' to the list with duplicate suppresion.
     69  1.1  elric  */
     70  1.1  elric 
     71  1.1  elric static void
     72  1.1  elric add_port(krb5_context context,
     73  1.1  elric 	 int family, int port, const char *protocol)
     74  1.1  elric {
     75  1.1  elric     int type;
     76  1.1  elric     int i;
     77  1.1  elric 
     78  1.1  elric     if(strcmp(protocol, "udp") == 0)
     79  1.1  elric 	type = SOCK_DGRAM;
     80  1.1  elric     else if(strcmp(protocol, "tcp") == 0)
     81  1.1  elric 	type = SOCK_STREAM;
     82  1.1  elric     else
     83  1.1  elric 	return;
     84  1.1  elric     for(i = 0; i < num_ports; i++){
     85  1.1  elric 	if(ports[i].type == type
     86  1.1  elric 	   && ports[i].port == port
     87  1.1  elric 	   && ports[i].family == family)
     88  1.1  elric 	    return;
     89  1.1  elric     }
     90  1.1  elric     ports = realloc(ports, (num_ports + 1) * sizeof(*ports));
     91  1.1  elric     if (ports == NULL)
     92  1.1  elric 	krb5_err (context, 1, errno, "realloc");
     93  1.1  elric     ports[num_ports].family = family;
     94  1.1  elric     ports[num_ports].type   = type;
     95  1.1  elric     ports[num_ports].port   = port;
     96  1.1  elric     num_ports++;
     97  1.1  elric }
     98  1.1  elric 
     99  1.1  elric /*
    100  1.1  elric  * add a triple but with service -> port lookup
    101  1.1  elric  * (this prints warnings for stuff that does not exist)
    102  1.1  elric  */
    103  1.1  elric 
    104  1.1  elric static void
    105  1.1  elric add_port_service(krb5_context context,
    106  1.1  elric 		 int family, const char *service, int port,
    107  1.1  elric 		 const char *protocol)
    108  1.1  elric {
    109  1.1  elric     port = krb5_getportbyname (context, service, protocol, port);
    110  1.1  elric     add_port (context, family, port, protocol);
    111  1.1  elric }
    112  1.1  elric 
    113  1.1  elric /*
    114  1.1  elric  * add the port with service -> port lookup or string -> number
    115  1.1  elric  * (no warning is printed)
    116  1.1  elric  */
    117  1.1  elric 
    118  1.1  elric static void
    119  1.1  elric add_port_string (krb5_context context,
    120  1.1  elric 		 int family, const char *str, const char *protocol)
    121  1.1  elric {
    122  1.1  elric     struct servent *sp;
    123  1.1  elric     int port;
    124  1.1  elric 
    125  1.1  elric     sp = roken_getservbyname (str, protocol);
    126  1.1  elric     if (sp != NULL) {
    127  1.1  elric 	port = sp->s_port;
    128  1.1  elric     } else {
    129  1.1  elric 	char *end;
    130  1.1  elric 
    131  1.1  elric 	port = htons(strtol(str, &end, 0));
    132  1.1  elric 	if (end == str)
    133  1.1  elric 	    return;
    134  1.1  elric     }
    135  1.1  elric     add_port (context, family, port, protocol);
    136  1.1  elric }
    137  1.1  elric 
    138  1.1  elric /*
    139  1.1  elric  * add the standard collection of ports for `family'
    140  1.1  elric  */
    141  1.1  elric 
    142  1.1  elric static void
    143  1.1  elric add_standard_ports (krb5_context context,
    144  1.1  elric 		    krb5_kdc_configuration *config,
    145  1.1  elric 		    int family)
    146  1.1  elric {
    147  1.1  elric     add_port_service(context, family, "kerberos", 88, "udp");
    148  1.1  elric     add_port_service(context, family, "kerberos", 88, "tcp");
    149  1.1  elric     add_port_service(context, family, "kerberos-sec", 88, "udp");
    150  1.1  elric     add_port_service(context, family, "kerberos-sec", 88, "tcp");
    151  1.1  elric     if(enable_http)
    152  1.1  elric 	add_port_service(context, family, "http", 80, "tcp");
    153  1.1  elric     if(config->enable_524) {
    154  1.1  elric 	add_port_service(context, family, "krb524", 4444, "udp");
    155  1.1  elric 	add_port_service(context, family, "krb524", 4444, "tcp");
    156  1.1  elric     }
    157  1.1  elric     if(config->enable_v4) {
    158  1.1  elric 	add_port_service(context, family, "kerberos-iv", 750, "udp");
    159  1.1  elric 	add_port_service(context, family, "kerberos-iv", 750, "tcp");
    160  1.1  elric     }
    161  1.1  elric     if (config->enable_kaserver)
    162  1.1  elric 	add_port_service(context, family, "afs3-kaserver", 7004, "udp");
    163  1.1  elric     if(config->enable_kx509) {
    164  1.1  elric 	add_port_service(context, family, "kca_service", 9878, "udp");
    165  1.1  elric 	add_port_service(context, family, "kca_service", 9878, "tcp");
    166  1.1  elric     }
    167  1.1  elric 
    168  1.1  elric }
    169  1.1  elric 
    170  1.1  elric /*
    171  1.1  elric  * parse the set of space-delimited ports in `str' and add them.
    172  1.1  elric  * "+" => all the standard ones
    173  1.1  elric  * otherwise it's port|service[/protocol]
    174  1.1  elric  */
    175  1.1  elric 
    176  1.1  elric static void
    177  1.1  elric parse_ports(krb5_context context,
    178  1.1  elric 	    krb5_kdc_configuration *config,
    179  1.1  elric 	    const char *str)
    180  1.1  elric {
    181  1.1  elric     char *pos = NULL;
    182  1.1  elric     char *p;
    183  1.1  elric     char *str_copy = strdup (str);
    184  1.1  elric 
    185  1.1  elric     p = strtok_r(str_copy, " \t", &pos);
    186  1.1  elric     while(p != NULL) {
    187  1.1  elric 	if(strcmp(p, "+") == 0) {
    188  1.1  elric #ifdef HAVE_IPV6
    189  1.1  elric 	    add_standard_ports(context, config, AF_INET6);
    190  1.1  elric #endif
    191  1.1  elric 	    add_standard_ports(context, config, AF_INET);
    192  1.1  elric 	} else {
    193  1.1  elric 	    char *q = strchr(p, '/');
    194  1.1  elric 	    if(q){
    195  1.1  elric 		*q++ = 0;
    196  1.1  elric #ifdef HAVE_IPV6
    197  1.1  elric 		add_port_string(context, AF_INET6, p, q);
    198  1.1  elric #endif
    199  1.1  elric 		add_port_string(context, AF_INET, p, q);
    200  1.1  elric 	    }else {
    201  1.1  elric #ifdef HAVE_IPV6
    202  1.1  elric 		add_port_string(context, AF_INET6, p, "udp");
    203  1.1  elric 		add_port_string(context, AF_INET6, p, "tcp");
    204  1.1  elric #endif
    205  1.1  elric 		add_port_string(context, AF_INET, p, "udp");
    206  1.1  elric 		add_port_string(context, AF_INET, p, "tcp");
    207  1.1  elric 	    }
    208  1.1  elric 	}
    209  1.1  elric 
    210  1.1  elric 	p = strtok_r(NULL, " \t", &pos);
    211  1.1  elric     }
    212  1.1  elric     free (str_copy);
    213  1.1  elric }
    214  1.1  elric 
    215  1.1  elric /*
    216  1.1  elric  * every socket we listen on
    217  1.1  elric  */
    218  1.1  elric 
    219  1.1  elric struct descr {
    220  1.1  elric     krb5_socket_t s;
    221  1.1  elric     int type;
    222  1.1  elric     int port;
    223  1.1  elric     unsigned char *buf;
    224  1.1  elric     size_t size;
    225  1.1  elric     size_t len;
    226  1.1  elric     time_t timeout;
    227  1.1  elric     struct sockaddr_storage __ss;
    228  1.1  elric     struct sockaddr *sa;
    229  1.1  elric     socklen_t sock_len;
    230  1.1  elric     char addr_string[128];
    231  1.1  elric };
    232  1.1  elric 
    233  1.1  elric static void
    234  1.1  elric init_descr(struct descr *d)
    235  1.1  elric {
    236  1.1  elric     memset(d, 0, sizeof(*d));
    237  1.1  elric     d->sa = (struct sockaddr *)&d->__ss;
    238  1.1  elric     d->s = rk_INVALID_SOCKET;
    239  1.1  elric }
    240  1.1  elric 
    241  1.1  elric /*
    242  1.1  elric  * re-initialize all `n' ->sa in `d'.
    243  1.1  elric  */
    244  1.1  elric 
    245  1.1  elric static void
    246  1.1  elric reinit_descrs (struct descr *d, int n)
    247  1.1  elric {
    248  1.1  elric     int i;
    249  1.1  elric 
    250  1.1  elric     for (i = 0; i < n; ++i)
    251  1.1  elric 	d[i].sa = (struct sockaddr *)&d[i].__ss;
    252  1.1  elric }
    253  1.1  elric 
    254  1.1  elric /*
    255  1.1  elric  * Create the socket (family, type, port) in `d'
    256  1.1  elric  */
    257  1.1  elric 
    258  1.1  elric static void
    259  1.1  elric init_socket(krb5_context context,
    260  1.1  elric 	    krb5_kdc_configuration *config,
    261  1.1  elric 	    struct descr *d, krb5_address *a, int family, int type, int port)
    262  1.1  elric {
    263  1.1  elric     krb5_error_code ret;
    264  1.1  elric     struct sockaddr_storage __ss;
    265  1.1  elric     struct sockaddr *sa = (struct sockaddr *)&__ss;
    266  1.1  elric     krb5_socklen_t sa_size = sizeof(__ss);
    267  1.1  elric 
    268  1.1  elric     init_descr (d);
    269  1.1  elric 
    270  1.1  elric     ret = krb5_addr2sockaddr (context, a, sa, &sa_size, port);
    271  1.1  elric     if (ret) {
    272  1.1  elric 	krb5_warn(context, ret, "krb5_addr2sockaddr");
    273  1.1  elric 	rk_closesocket(d->s);
    274  1.1  elric 	d->s = rk_INVALID_SOCKET;
    275  1.1  elric 	return;
    276  1.1  elric     }
    277  1.1  elric 
    278  1.1  elric     if (sa->sa_family != family)
    279  1.1  elric 	return;
    280  1.1  elric 
    281  1.1  elric     d->s = socket(family, type, 0);
    282  1.1  elric     if(rk_IS_BAD_SOCKET(d->s)){
    283  1.1  elric 	krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
    284  1.1  elric 	d->s = rk_INVALID_SOCKET;
    285  1.1  elric 	return;
    286  1.1  elric     }
    287  1.1  elric #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
    288  1.1  elric     {
    289  1.1  elric 	int one = 1;
    290  1.1  elric 	setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
    291  1.1  elric     }
    292  1.1  elric #endif
    293  1.1  elric     d->type = type;
    294  1.1  elric     d->port = port;
    295  1.1  elric 
    296  1.1  elric     if(rk_IS_SOCKET_ERROR(bind(d->s, sa, sa_size))){
    297  1.1  elric 	char a_str[256];
    298  1.1  elric 	size_t len;
    299  1.1  elric 
    300  1.1  elric 	krb5_print_address (a, a_str, sizeof(a_str), &len);
    301  1.1  elric 	krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port));
    302  1.1  elric 	rk_closesocket(d->s);
    303  1.1  elric 	d->s = rk_INVALID_SOCKET;
    304  1.1  elric 	return;
    305  1.1  elric     }
    306  1.1  elric     if(type == SOCK_STREAM && rk_IS_SOCKET_ERROR(listen(d->s, SOMAXCONN))){
    307  1.1  elric 	char a_str[256];
    308  1.1  elric 	size_t len;
    309  1.1  elric 
    310  1.1  elric 	krb5_print_address (a, a_str, sizeof(a_str), &len);
    311  1.1  elric 	krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port));
    312  1.1  elric 	rk_closesocket(d->s);
    313  1.1  elric 	d->s = rk_INVALID_SOCKET;
    314  1.1  elric 	return;
    315  1.1  elric     }
    316  1.1  elric }
    317  1.1  elric 
    318  1.1  elric /*
    319  1.1  elric  * Allocate descriptors for all the sockets that we should listen on
    320  1.1  elric  * and return the number of them.
    321  1.1  elric  */
    322  1.1  elric 
    323  1.1  elric static int
    324  1.1  elric init_sockets(krb5_context context,
    325  1.1  elric 	     krb5_kdc_configuration *config,
    326  1.1  elric 	     struct descr **desc)
    327  1.1  elric {
    328  1.1  elric     krb5_error_code ret;
    329  1.1  elric     int i, j;
    330  1.1  elric     struct descr *d;
    331  1.1  elric     int num = 0;
    332  1.1  elric     krb5_addresses addresses;
    333  1.1  elric 
    334  1.1  elric     if (explicit_addresses.len) {
    335  1.1  elric 	addresses = explicit_addresses;
    336  1.1  elric     } else {
    337  1.1  elric 	ret = krb5_get_all_server_addrs (context, &addresses);
    338  1.1  elric 	if (ret)
    339  1.1  elric 	    krb5_err (context, 1, ret, "krb5_get_all_server_addrs");
    340  1.1  elric     }
    341  1.1  elric     parse_ports(context, config, port_str);
    342  1.1  elric     d = malloc(addresses.len * num_ports * sizeof(*d));
    343  1.1  elric     if (d == NULL)
    344  1.1  elric 	krb5_errx(context, 1, "malloc(%lu) failed",
    345  1.1  elric 		  (unsigned long)num_ports * sizeof(*d));
    346  1.1  elric 
    347  1.1  elric     for (i = 0; i < num_ports; i++){
    348  1.1  elric 	for (j = 0; j < addresses.len; ++j) {
    349  1.1  elric 	    init_socket(context, config, &d[num], &addresses.val[j],
    350  1.1  elric 			ports[i].family, ports[i].type, ports[i].port);
    351  1.1  elric 	    if(d[num].s != rk_INVALID_SOCKET){
    352  1.1  elric 		char a_str[80];
    353  1.1  elric 		size_t len;
    354  1.1  elric 
    355  1.1  elric 		krb5_print_address (&addresses.val[j], a_str,
    356  1.1  elric 				    sizeof(a_str), &len);
    357  1.1  elric 
    358  1.1  elric 		kdc_log(context, config, 5, "listening on %s port %u/%s",
    359  1.1  elric 			a_str,
    360  1.1  elric 			ntohs(ports[i].port),
    361  1.1  elric 			(ports[i].type == SOCK_STREAM) ? "tcp" : "udp");
    362  1.1  elric 		/* XXX */
    363  1.1  elric 		num++;
    364  1.1  elric 	    }
    365  1.1  elric 	}
    366  1.1  elric     }
    367  1.1  elric     krb5_free_addresses (context, &addresses);
    368  1.1  elric     d = realloc(d, num * sizeof(*d));
    369  1.1  elric     if (d == NULL && num != 0)
    370  1.1  elric 	krb5_errx(context, 1, "realloc(%lu) failed",
    371  1.1  elric 		  (unsigned long)num * sizeof(*d));
    372  1.1  elric     reinit_descrs (d, num);
    373  1.1  elric     *desc = d;
    374  1.1  elric     return num;
    375  1.1  elric }
    376  1.1  elric 
    377  1.1  elric /*
    378  1.1  elric  *
    379  1.1  elric  */
    380  1.1  elric 
    381  1.1  elric static const char *
    382  1.1  elric descr_type(struct descr *d)
    383  1.1  elric {
    384  1.1  elric     if (d->type == SOCK_DGRAM)
    385  1.1  elric 	return "udp";
    386  1.1  elric     else if (d->type == SOCK_STREAM)
    387  1.1  elric 	return "tcp";
    388  1.1  elric     return "unknown";
    389  1.1  elric }
    390  1.1  elric 
    391  1.1  elric static void
    392  1.1  elric addr_to_string(krb5_context context,
    393  1.1  elric 	       struct sockaddr *addr, size_t addr_len, char *str, size_t len)
    394  1.1  elric {
    395  1.1  elric     krb5_address a;
    396  1.1  elric     if(krb5_sockaddr2address(context, addr, &a) == 0) {
    397  1.1  elric 	if(krb5_print_address(&a, str, len, &len) == 0) {
    398  1.1  elric 	    krb5_free_address(context, &a);
    399  1.1  elric 	    return;
    400  1.1  elric 	}
    401  1.1  elric 	krb5_free_address(context, &a);
    402  1.1  elric     }
    403  1.1  elric     snprintf(str, len, "<family=%d>", addr->sa_family);
    404  1.1  elric }
    405  1.1  elric 
    406  1.1  elric /*
    407  1.1  elric  *
    408  1.1  elric  */
    409  1.1  elric 
    410  1.1  elric static void
    411  1.1  elric send_reply(krb5_context context,
    412  1.1  elric 	   krb5_kdc_configuration *config,
    413  1.1  elric 	   krb5_boolean prependlength,
    414  1.1  elric 	   struct descr *d,
    415  1.1  elric 	   krb5_data *reply)
    416  1.1  elric {
    417  1.1  elric     kdc_log(context, config, 5,
    418  1.1  elric 	    "sending %lu bytes to %s", (unsigned long)reply->length,
    419  1.1  elric 	    d->addr_string);
    420  1.1  elric     if(prependlength){
    421  1.1  elric 	unsigned char l[4];
    422  1.1  elric 	l[0] = (reply->length >> 24) & 0xff;
    423  1.1  elric 	l[1] = (reply->length >> 16) & 0xff;
    424  1.1  elric 	l[2] = (reply->length >> 8) & 0xff;
    425  1.1  elric 	l[3] = reply->length & 0xff;
    426  1.1  elric 	if(rk_IS_SOCKET_ERROR(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len))) {
    427  1.1  elric 	    kdc_log (context, config,
    428  1.1  elric 		     0, "sendto(%s): %s", d->addr_string,
    429  1.1  elric 		     strerror(rk_SOCK_ERRNO));
    430  1.1  elric 	    return;
    431  1.1  elric 	}
    432  1.1  elric     }
    433  1.1  elric     if(rk_IS_SOCKET_ERROR(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len))) {
    434  1.1  elric 	kdc_log (context, config, 0, "sendto(%s): %s", d->addr_string,
    435  1.1  elric 		 strerror(rk_SOCK_ERRNO));
    436  1.1  elric 	return;
    437  1.1  elric     }
    438  1.1  elric }
    439  1.1  elric 
    440  1.1  elric /*
    441  1.1  elric  * Handle the request in `buf, len' to socket `d'
    442  1.1  elric  */
    443  1.1  elric 
    444  1.1  elric static void
    445  1.1  elric do_request(krb5_context context,
    446  1.1  elric 	   krb5_kdc_configuration *config,
    447  1.1  elric 	   void *buf, size_t len, krb5_boolean prependlength,
    448  1.1  elric 	   struct descr *d)
    449  1.1  elric {
    450  1.1  elric     krb5_error_code ret;
    451  1.1  elric     krb5_data reply;
    452  1.1  elric     int datagram_reply = (d->type == SOCK_DGRAM);
    453  1.1  elric 
    454  1.1  elric     krb5_kdc_update_time(NULL);
    455  1.1  elric 
    456  1.1  elric     krb5_data_zero(&reply);
    457  1.1  elric     ret = krb5_kdc_process_request(context, config,
    458  1.1  elric 				   buf, len, &reply, &prependlength,
    459  1.1  elric 				   d->addr_string, d->sa,
    460  1.1  elric 				   datagram_reply);
    461  1.1  elric     if(request_log)
    462  1.1  elric 	krb5_kdc_save_request(context, request_log, buf, len, &reply, d->sa);
    463  1.1  elric     if(reply.length){
    464  1.1  elric 	send_reply(context, config, prependlength, d, &reply);
    465  1.1  elric 	krb5_data_free(&reply);
    466  1.1  elric     }
    467  1.1  elric     if(ret)
    468  1.1  elric 	kdc_log(context, config, 0,
    469  1.1  elric 		"Failed processing %lu byte request from %s",
    470  1.1  elric 		(unsigned long)len, d->addr_string);
    471  1.1  elric }
    472  1.1  elric 
    473  1.1  elric /*
    474  1.1  elric  * Handle incoming data to the UDP socket in `d'
    475  1.1  elric  */
    476  1.1  elric 
    477  1.1  elric static void
    478  1.1  elric handle_udp(krb5_context context,
    479  1.1  elric 	   krb5_kdc_configuration *config,
    480  1.1  elric 	   struct descr *d)
    481  1.1  elric {
    482  1.1  elric     unsigned char *buf;
    483  1.1  elric     int n;
    484  1.1  elric 
    485  1.1  elric     buf = malloc(max_request_udp);
    486  1.1  elric     if(buf == NULL){
    487  1.1  elric 	kdc_log(context, config, 0, "Failed to allocate %lu bytes", (unsigned long)max_request_udp);
    488  1.1  elric 	return;
    489  1.1  elric     }
    490  1.1  elric 
    491  1.1  elric     d->sock_len = sizeof(d->__ss);
    492  1.1  elric     n = recvfrom(d->s, buf, max_request_udp, 0, d->sa, &d->sock_len);
    493  1.1  elric     if(rk_IS_SOCKET_ERROR(n))
    494  1.1  elric 	krb5_warn(context, rk_SOCK_ERRNO, "recvfrom");
    495  1.1  elric     else {
    496  1.1  elric 	addr_to_string (context, d->sa, d->sock_len,
    497  1.1  elric 			d->addr_string, sizeof(d->addr_string));
    498  1.1  elric 	if (n == max_request_udp) {
    499  1.1  elric 	    krb5_data data;
    500  1.1  elric 	    krb5_warn(context, errno,
    501  1.1  elric 		      "recvfrom: truncated packet from %s, asking for TCP",
    502  1.1  elric 		      d->addr_string);
    503  1.1  elric 	    krb5_mk_error(context,
    504  1.1  elric 			  KRB5KRB_ERR_RESPONSE_TOO_BIG,
    505  1.1  elric 			  NULL,
    506  1.1  elric 			  NULL,
    507  1.1  elric 			  NULL,
    508  1.1  elric 			  NULL,
    509  1.1  elric 			  NULL,
    510  1.1  elric 			  NULL,
    511  1.1  elric 			  &data);
    512  1.1  elric 	    send_reply(context, config, FALSE, d, &data);
    513  1.1  elric 	    krb5_data_free(&data);
    514  1.1  elric 	} else {
    515  1.1  elric 	    do_request(context, config, buf, n, FALSE, d);
    516  1.1  elric 	}
    517  1.1  elric     }
    518  1.1  elric     free (buf);
    519  1.1  elric }
    520  1.1  elric 
    521  1.1  elric static void
    522  1.1  elric clear_descr(struct descr *d)
    523  1.1  elric {
    524  1.1  elric     if(d->buf)
    525  1.1  elric 	memset(d->buf, 0, d->size);
    526  1.1  elric     d->len = 0;
    527  1.1  elric     if(d->s != rk_INVALID_SOCKET)
    528  1.1  elric 	rk_closesocket(d->s);
    529  1.1  elric     d->s = rk_INVALID_SOCKET;
    530  1.1  elric }
    531  1.1  elric 
    532  1.1  elric 
    533  1.1  elric /* remove HTTP %-quoting from buf */
    534  1.1  elric static int
    535  1.1  elric de_http(char *buf)
    536  1.1  elric {
    537  1.1  elric     unsigned char *p, *q;
    538  1.1  elric     for(p = q = (unsigned char *)buf; *p; p++, q++) {
    539  1.1  elric 	if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
    540  1.1  elric 	    unsigned int x;
    541  1.1  elric 	    if(sscanf((char *)p + 1, "%2x", &x) != 1)
    542  1.1  elric 		return -1;
    543  1.1  elric 	    *q = x;
    544  1.1  elric 	    p += 2;
    545  1.1  elric 	} else
    546  1.1  elric 	    *q = *p;
    547  1.1  elric     }
    548  1.1  elric     *q = '\0';
    549  1.1  elric     return 0;
    550  1.1  elric }
    551  1.1  elric 
    552  1.1  elric #define TCP_TIMEOUT 4
    553  1.1  elric 
    554  1.1  elric /*
    555  1.1  elric  * accept a new TCP connection on `d[parent]' and store it in `d[child]'
    556  1.1  elric  */
    557  1.1  elric 
    558  1.1  elric static void
    559  1.1  elric add_new_tcp (krb5_context context,
    560  1.1  elric 	     krb5_kdc_configuration *config,
    561  1.1  elric 	     struct descr *d, int parent, int child)
    562  1.1  elric {
    563  1.1  elric     krb5_socket_t s;
    564  1.1  elric 
    565  1.1  elric     if (child == -1)
    566  1.1  elric 	return;
    567  1.1  elric 
    568  1.1  elric     d[child].sock_len = sizeof(d[child].__ss);
    569  1.1  elric     s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
    570  1.1  elric     if(rk_IS_BAD_SOCKET(s)) {
    571  1.1  elric 	krb5_warn(context, rk_SOCK_ERRNO, "accept");
    572  1.1  elric 	return;
    573  1.1  elric     }
    574  1.1  elric 
    575  1.1  elric #ifdef FD_SETSIZE
    576  1.1  elric     if (s >= FD_SETSIZE) {
    577  1.1  elric 	krb5_warnx(context, "socket FD too large");
    578  1.1  elric 	rk_closesocket (s);
    579  1.1  elric 	return;
    580  1.1  elric     }
    581  1.1  elric #endif
    582  1.1  elric 
    583  1.1  elric     d[child].s = s;
    584  1.1  elric     d[child].timeout = time(NULL) + TCP_TIMEOUT;
    585  1.1  elric     d[child].type = SOCK_STREAM;
    586  1.1  elric     addr_to_string (context,
    587  1.1  elric 		    d[child].sa, d[child].sock_len,
    588  1.1  elric 		    d[child].addr_string, sizeof(d[child].addr_string));
    589  1.1  elric }
    590  1.1  elric 
    591  1.1  elric /*
    592  1.1  elric  * Grow `d' to handle at least `n'.
    593  1.1  elric  * Return != 0 if fails
    594  1.1  elric  */
    595  1.1  elric 
    596  1.1  elric static int
    597  1.1  elric grow_descr (krb5_context context,
    598  1.1  elric 	    krb5_kdc_configuration *config,
    599  1.1  elric 	    struct descr *d, size_t n)
    600  1.1  elric {
    601  1.1  elric     if (d->size - d->len < n) {
    602  1.1  elric 	unsigned char *tmp;
    603  1.1  elric 	size_t grow;
    604  1.1  elric 
    605  1.1  elric 	grow = max(1024, d->len + n);
    606  1.1  elric 	if (d->size + grow > max_request_tcp) {
    607  1.1  elric 	    kdc_log(context, config, 0, "Request exceeds max request size (%lu bytes).",
    608  1.1  elric 		    (unsigned long)d->size + grow);
    609  1.1  elric 	    clear_descr(d);
    610  1.1  elric 	    return -1;
    611  1.1  elric 	}
    612  1.1  elric 	tmp = realloc (d->buf, d->size + grow);
    613  1.1  elric 	if (tmp == NULL) {
    614  1.1  elric 	    kdc_log(context, config, 0, "Failed to re-allocate %lu bytes.",
    615  1.1  elric 		    (unsigned long)d->size + grow);
    616  1.1  elric 	    clear_descr(d);
    617  1.1  elric 	    return -1;
    618  1.1  elric 	}
    619  1.1  elric 	d->size += grow;
    620  1.1  elric 	d->buf = tmp;
    621  1.1  elric     }
    622  1.1  elric     return 0;
    623  1.1  elric }
    624  1.1  elric 
    625  1.1  elric /*
    626  1.1  elric  * Try to handle the TCP data at `d->buf, d->len'.
    627  1.1  elric  * Return -1 if failed, 0 if succesful, and 1 if data is complete.
    628  1.1  elric  */
    629  1.1  elric 
    630  1.1  elric static int
    631  1.1  elric handle_vanilla_tcp (krb5_context context,
    632  1.1  elric 		    krb5_kdc_configuration *config,
    633  1.1  elric 		    struct descr *d)
    634  1.1  elric {
    635  1.1  elric     krb5_storage *sp;
    636  1.1  elric     uint32_t len;
    637  1.1  elric 
    638  1.1  elric     sp = krb5_storage_from_mem(d->buf, d->len);
    639  1.1  elric     if (sp == NULL) {
    640  1.1  elric 	kdc_log (context, config, 0, "krb5_storage_from_mem failed");
    641  1.1  elric 	return -1;
    642  1.1  elric     }
    643  1.1  elric     krb5_ret_uint32(sp, &len);
    644  1.1  elric     krb5_storage_free(sp);
    645  1.1  elric     if(d->len - 4 >= len) {
    646  1.1  elric 	memmove(d->buf, d->buf + 4, d->len - 4);
    647  1.1  elric 	d->len -= 4;
    648  1.1  elric 	return 1;
    649  1.1  elric     }
    650  1.1  elric     return 0;
    651  1.1  elric }
    652  1.1  elric 
    653  1.1  elric /*
    654  1.1  elric  * Try to handle the TCP/HTTP data at `d->buf, d->len'.
    655  1.1  elric  * Return -1 if failed, 0 if succesful, and 1 if data is complete.
    656  1.1  elric  */
    657  1.1  elric 
    658  1.1  elric static int
    659  1.1  elric handle_http_tcp (krb5_context context,
    660  1.1  elric 		 krb5_kdc_configuration *config,
    661  1.1  elric 		 struct descr *d)
    662  1.1  elric {
    663  1.1  elric     char *s, *p, *t;
    664  1.1  elric     void *data;
    665  1.1  elric     char *proto;
    666  1.1  elric     int len;
    667  1.1  elric 
    668  1.1  elric     s = (char *)d->buf;
    669  1.1  elric 
    670  1.1  elric     /* If its a multi line query, truncate off the first line */
    671  1.1  elric     p = strstr(s, "\r\n");
    672  1.1  elric     if (p)
    673  1.1  elric 	*p = 0;
    674  1.1  elric 
    675  1.1  elric     p = NULL;
    676  1.1  elric     t = strtok_r(s, " \t", &p);
    677  1.1  elric     if (t == NULL) {
    678  1.1  elric 	kdc_log(context, config, 0,
    679  1.1  elric 		"Missing HTTP operand (GET) request from %s", d->addr_string);
    680  1.1  elric 	return -1;
    681  1.1  elric     }
    682  1.1  elric 
    683  1.1  elric     t = strtok_r(NULL, " \t", &p);
    684  1.1  elric     if(t == NULL) {
    685  1.1  elric 	kdc_log(context, config, 0,
    686  1.1  elric 		"Missing HTTP GET data in request from %s", d->addr_string);
    687  1.1  elric 	return -1;
    688  1.1  elric     }
    689  1.1  elric 
    690  1.1  elric     data = malloc(strlen(t));
    691  1.1  elric     if (data == NULL) {
    692  1.1  elric 	kdc_log(context, config, 0, "Failed to allocate %lu bytes",
    693  1.1  elric 		(unsigned long)strlen(t));
    694  1.1  elric 	return -1;
    695  1.1  elric     }
    696  1.1  elric     if(*t == '/')
    697  1.1  elric 	t++;
    698  1.1  elric     if(de_http(t) != 0) {
    699  1.1  elric 	kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
    700  1.1  elric 	kdc_log(context, config, 5, "HTTP request: %s", t);
    701  1.1  elric 	free(data);
    702  1.1  elric 	return -1;
    703  1.1  elric     }
    704  1.1  elric     proto = strtok_r(NULL, " \t", &p);
    705  1.1  elric     if (proto == NULL) {
    706  1.1  elric 	kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
    707  1.1  elric 	free(data);
    708  1.1  elric 	return -1;
    709  1.1  elric     }
    710  1.1  elric     len = base64_decode(t, data);
    711  1.1  elric     if(len <= 0){
    712  1.1  elric 	const char *msg =
    713  1.1  elric 	    " 404 Not found\r\n"
    714  1.1  elric 	    "Server: Heimdal/" VERSION "\r\n"
    715  1.1  elric 	    "Cache-Control: no-cache\r\n"
    716  1.1  elric 	    "Pragma: no-cache\r\n"
    717  1.1  elric 	    "Content-type: text/html\r\n"
    718  1.1  elric 	    "Content-transfer-encoding: 8bit\r\n\r\n"
    719  1.1  elric 	    "<TITLE>404 Not found</TITLE>\r\n"
    720  1.1  elric 	    "<H1>404 Not found</H1>\r\n"
    721  1.1  elric 	    "That page doesn't exist, maybe you are looking for "
    722  1.1  elric 	    "<A HREF=\"http://www.h5l.org/\">Heimdal</A>?\r\n";
    723  1.1  elric 	kdc_log(context, config, 0, "HTTP request from %s is non KDC request", d->addr_string);
    724  1.1  elric 	kdc_log(context, config, 5, "HTTP request: %s", t);
    725  1.1  elric 	free(data);
    726  1.1  elric 	if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) {
    727  1.1  elric 	    kdc_log(context, config, 0, "HTTP write failed: %s: %s",
    728  1.1  elric 		    d->addr_string, strerror(rk_SOCK_ERRNO));
    729  1.1  elric 	    return -1;
    730  1.1  elric 	}
    731  1.1  elric 	if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) {
    732  1.1  elric 	    kdc_log(context, config, 0, "HTTP write failed: %s: %s",
    733  1.1  elric 		    d->addr_string, strerror(rk_SOCK_ERRNO));
    734  1.1  elric 	    return -1;
    735  1.1  elric 	}
    736  1.1  elric 	return -1;
    737  1.1  elric     }
    738  1.1  elric     {
    739  1.1  elric 	const char *msg =
    740  1.1  elric 	    " 200 OK\r\n"
    741  1.1  elric 	    "Server: Heimdal/" VERSION "\r\n"
    742  1.1  elric 	    "Cache-Control: no-cache\r\n"
    743  1.1  elric 	    "Pragma: no-cache\r\n"
    744  1.1  elric 	    "Content-type: application/octet-stream\r\n"
    745  1.1  elric 	    "Content-transfer-encoding: binary\r\n\r\n";
    746  1.1  elric 	if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) {
    747  1.1  elric 	    free(data);
    748  1.1  elric 	    kdc_log(context, config, 0, "HTTP write failed: %s: %s",
    749  1.1  elric 		    d->addr_string, strerror(rk_SOCK_ERRNO));
    750  1.1  elric 	    return -1;
    751  1.1  elric 	}
    752  1.1  elric 	if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) {
    753  1.1  elric 	    free(data);
    754  1.1  elric 	    kdc_log(context, config, 0, "HTTP write failed: %s: %s",
    755  1.1  elric 		    d->addr_string, strerror(rk_SOCK_ERRNO));
    756  1.1  elric 	    return -1;
    757  1.1  elric 	}
    758  1.1  elric     }
    759  1.1  elric     if (len > d->len)
    760  1.1  elric         len = d->len;
    761  1.1  elric     memcpy(d->buf, data, len);
    762  1.1  elric     d->len = len;
    763  1.1  elric     free(data);
    764  1.1  elric     return 1;
    765  1.1  elric }
    766  1.1  elric 
    767  1.1  elric /*
    768  1.1  elric  * Handle incoming data to the TCP socket in `d[index]'
    769  1.1  elric  */
    770  1.1  elric 
    771  1.1  elric static void
    772  1.1  elric handle_tcp(krb5_context context,
    773  1.1  elric 	   krb5_kdc_configuration *config,
    774  1.1  elric 	   struct descr *d, int idx, int min_free)
    775  1.1  elric {
    776  1.1  elric     unsigned char buf[1024];
    777  1.1  elric     int n;
    778  1.1  elric     int ret = 0;
    779  1.1  elric 
    780  1.1  elric     if (d[idx].timeout == 0) {
    781  1.1  elric 	add_new_tcp (context, config, d, idx, min_free);
    782  1.1  elric 	return;
    783  1.1  elric     }
    784  1.1  elric 
    785  1.1  elric     n = recvfrom(d[idx].s, buf, sizeof(buf), 0, NULL, NULL);
    786  1.1  elric     if(rk_IS_SOCKET_ERROR(n)){
    787  1.1  elric 	krb5_warn(context, rk_SOCK_ERRNO, "recvfrom failed from %s to %s/%d",
    788  1.1  elric 		  d[idx].addr_string, descr_type(d + idx),
    789  1.1  elric 		  ntohs(d[idx].port));
    790  1.1  elric 	return;
    791  1.1  elric     } else if (n == 0) {
    792  1.1  elric 	krb5_warnx(context, "connection closed before end of data after %lu "
    793  1.1  elric 		   "bytes from %s to %s/%d", (unsigned long)d[idx].len,
    794  1.1  elric 		   d[idx].addr_string, descr_type(d + idx),
    795  1.1  elric 		   ntohs(d[idx].port));
    796  1.1  elric 	clear_descr (d + idx);
    797  1.1  elric 	return;
    798  1.1  elric     }
    799  1.1  elric     if (grow_descr (context, config, &d[idx], n))
    800  1.1  elric 	return;
    801  1.1  elric     memcpy(d[idx].buf + d[idx].len, buf, n);
    802  1.1  elric     d[idx].len += n;
    803  1.1  elric     if(d[idx].len > 4 && d[idx].buf[0] == 0) {
    804  1.1  elric 	ret = handle_vanilla_tcp (context, config, &d[idx]);
    805  1.1  elric     } else if(enable_http &&
    806  1.1  elric 	      d[idx].len >= 4 &&
    807  1.1  elric 	      strncmp((char *)d[idx].buf, "GET ", 4) == 0 &&
    808  1.1  elric 	      strncmp((char *)d[idx].buf + d[idx].len - 4,
    809  1.1  elric 		      "\r\n\r\n", 4) == 0) {
    810  1.1  elric 
    811  1.1  elric         /* remove the trailing \r\n\r\n so the string is NUL terminated */
    812  1.1  elric         d[idx].buf[d[idx].len - 4] = '\0';
    813  1.1  elric 
    814  1.1  elric 	ret = handle_http_tcp (context, config, &d[idx]);
    815  1.1  elric 	if (ret < 0)
    816  1.1  elric 	    clear_descr (d + idx);
    817  1.1  elric     } else if (d[idx].len > 4) {
    818  1.1  elric 	kdc_log (context, config,
    819  1.1  elric 		 0, "TCP data of strange type from %s to %s/%d",
    820  1.1  elric 		 d[idx].addr_string, descr_type(d + idx),
    821  1.1  elric 		 ntohs(d[idx].port));
    822  1.1  elric 	if (d[idx].buf[0] & 0x80) {
    823  1.1  elric 	    krb5_data reply;
    824  1.1  elric 
    825  1.1  elric 	    kdc_log (context, config, 0, "TCP extension not supported");
    826  1.1  elric 
    827  1.1  elric 	    ret = krb5_mk_error(context,
    828  1.1  elric 				KRB5KRB_ERR_FIELD_TOOLONG,
    829  1.1  elric 				NULL,
    830  1.1  elric 				NULL,
    831  1.1  elric 				NULL,
    832  1.1  elric 				NULL,
    833  1.1  elric 				NULL,
    834  1.1  elric 				NULL,
    835  1.1  elric 				&reply);
    836  1.1  elric 	    if (ret == 0) {
    837  1.1  elric 		send_reply(context, config, TRUE, d + idx, &reply);
    838  1.1  elric 		krb5_data_free(&reply);
    839  1.1  elric 	    }
    840  1.1  elric 	}
    841  1.1  elric 	clear_descr(d + idx);
    842  1.1  elric 	return;
    843  1.1  elric     }
    844  1.1  elric     if (ret < 0)
    845  1.1  elric 	return;
    846  1.1  elric     else if (ret == 1) {
    847  1.1  elric 	do_request(context, config,
    848  1.1  elric 		   d[idx].buf, d[idx].len, TRUE, &d[idx]);
    849  1.1  elric 	clear_descr(d + idx);
    850  1.1  elric     }
    851  1.1  elric }
    852  1.1  elric 
    853  1.1  elric void
    854  1.1  elric loop(krb5_context context,
    855  1.1  elric      krb5_kdc_configuration *config)
    856  1.1  elric {
    857  1.1  elric     struct descr *d;
    858  1.1  elric     unsigned int ndescr;
    859  1.1  elric 
    860  1.1  elric     ndescr = init_sockets(context, config, &d);
    861  1.1  elric     if(ndescr <= 0)
    862  1.1  elric 	krb5_errx(context, 1, "No sockets!");
    863  1.1  elric     kdc_log(context, config, 0, "KDC started");
    864  1.1  elric     while(exit_flag == 0){
    865  1.1  elric 	struct timeval tmout;
    866  1.1  elric 	fd_set fds;
    867  1.1  elric 	int min_free = -1;
    868  1.1  elric 	int max_fd = 0;
    869  1.1  elric 	int i;
    870  1.1  elric 
    871  1.1  elric 	FD_ZERO(&fds);
    872  1.1  elric 	for(i = 0; i < ndescr; i++) {
    873  1.1  elric 	    if(!rk_IS_BAD_SOCKET(d[i].s)){
    874  1.1  elric 		if(d[i].type == SOCK_STREAM &&
    875  1.1  elric 		   d[i].timeout && d[i].timeout < time(NULL)) {
    876  1.1  elric 		    kdc_log(context, config, 1,
    877  1.1  elric 			    "TCP-connection from %s expired after %lu bytes",
    878  1.1  elric 			    d[i].addr_string, (unsigned long)d[i].len);
    879  1.1  elric 		    clear_descr(&d[i]);
    880  1.1  elric 		    continue;
    881  1.1  elric 		}
    882  1.1  elric #ifndef NO_LIMIT_FD_SETSIZE
    883  1.1  elric 		if(max_fd < d[i].s)
    884  1.1  elric 		    max_fd = d[i].s;
    885  1.1  elric #ifdef FD_SETSIZE
    886  1.1  elric 		if (max_fd >= FD_SETSIZE)
    887  1.1  elric 		    krb5_errx(context, 1, "fd too large");
    888  1.1  elric #endif
    889  1.1  elric #endif
    890  1.1  elric 		FD_SET(d[i].s, &fds);
    891  1.1  elric 	    } else if(min_free < 0 || i < min_free)
    892  1.1  elric 		min_free = i;
    893  1.1  elric 	}
    894  1.1  elric 	if(min_free == -1){
    895  1.1  elric 	    struct descr *tmp;
    896  1.1  elric 	    tmp = realloc(d, (ndescr + 4) * sizeof(*d));
    897  1.1  elric 	    if(tmp == NULL)
    898  1.1  elric 		krb5_warnx(context, "No memory");
    899  1.1  elric 	    else {
    900  1.1  elric 		d = tmp;
    901  1.1  elric 		reinit_descrs (d, ndescr);
    902  1.1  elric 		memset(d + ndescr, 0, 4 * sizeof(*d));
    903  1.1  elric 		for(i = ndescr; i < ndescr + 4; i++)
    904  1.1  elric 		    init_descr (&d[i]);
    905  1.1  elric 		min_free = ndescr;
    906  1.1  elric 		ndescr += 4;
    907  1.1  elric 	    }
    908  1.1  elric 	}
    909  1.1  elric 
    910  1.1  elric 	tmout.tv_sec = TCP_TIMEOUT;
    911  1.1  elric 	tmout.tv_usec = 0;
    912  1.1  elric 	switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
    913  1.1  elric 	case 0:
    914  1.1  elric 	    break;
    915  1.1  elric 	case -1:
    916  1.1  elric 	    if (errno != EINTR)
    917  1.1  elric 		krb5_warn(context, rk_SOCK_ERRNO, "select");
    918  1.1  elric 	    break;
    919  1.1  elric 	default:
    920  1.1  elric 	    for(i = 0; i < ndescr; i++)
    921  1.1  elric 		if(!rk_IS_BAD_SOCKET(d[i].s) && FD_ISSET(d[i].s, &fds)) {
    922  1.1  elric 		    if(d[i].type == SOCK_DGRAM)
    923  1.1  elric 			handle_udp(context, config, &d[i]);
    924  1.1  elric 		    else if(d[i].type == SOCK_STREAM)
    925  1.1  elric 			handle_tcp(context, config, d, i, min_free);
    926  1.1  elric 		}
    927  1.1  elric 	}
    928  1.1  elric     }
    929  1.1  elric     if (0);
    930  1.1  elric #ifdef SIGXCPU
    931  1.1  elric     else if(exit_flag == SIGXCPU)
    932  1.1  elric 	kdc_log(context, config, 0, "CPU time limit exceeded");
    933  1.1  elric #endif
    934  1.1  elric     else if(exit_flag == SIGINT || exit_flag == SIGTERM)
    935  1.1  elric 	kdc_log(context, config, 0, "Terminated");
    936  1.1  elric     else
    937  1.1  elric 	kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag);
    938  1.1  elric     free (d);
    939  1.1  elric }
    940