xref: /freebsd/crypto/heimdal/kdc/connect.c (revision bbd80c285ead4d04e4b8b9e950164352819694ba)
1 /*
2  * Copyright (c) 1997-2002 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "kdc_locl.h"
35 
36 RCSID("$Id: connect.c,v 1.90 2003/02/18 15:39:10 lha Exp $");
37 
38 /*
39  * a tuple describing on what to listen
40  */
41 
42 struct port_desc{
43     int family;
44     int type;
45     int port;
46 };
47 
48 /* the current ones */
49 
50 static struct port_desc *ports;
51 static int num_ports;
52 
53 /*
54  * add `family, port, protocol' to the list with duplicate suppresion.
55  */
56 
57 static void
58 add_port(int family, int port, const char *protocol)
59 {
60     int type;
61     int i;
62 
63     if(strcmp(protocol, "udp") == 0)
64 	type = SOCK_DGRAM;
65     else if(strcmp(protocol, "tcp") == 0)
66 	type = SOCK_STREAM;
67     else
68 	return;
69     for(i = 0; i < num_ports; i++){
70 	if(ports[i].type == type
71 	   && ports[i].port == port
72 	   && ports[i].family == family)
73 	    return;
74     }
75     ports = realloc(ports, (num_ports + 1) * sizeof(*ports));
76     if (ports == NULL)
77 	krb5_err (context, 1, errno, "realloc");
78     ports[num_ports].family = family;
79     ports[num_ports].type   = type;
80     ports[num_ports].port   = port;
81     num_ports++;
82 }
83 
84 /*
85  * add a triple but with service -> port lookup
86  * (this prints warnings for stuff that does not exist)
87  */
88 
89 static void
90 add_port_service(int family, const char *service, int port,
91 		 const char *protocol)
92 {
93     port = krb5_getportbyname (context, service, protocol, port);
94     add_port (family, port, protocol);
95 }
96 
97 /*
98  * add the port with service -> port lookup or string -> number
99  * (no warning is printed)
100  */
101 
102 static void
103 add_port_string (int family, const char *port_str, const char *protocol)
104 {
105     struct servent *sp;
106     int port;
107 
108     sp = roken_getservbyname (port_str, protocol);
109     if (sp != NULL) {
110 	port = sp->s_port;
111     } else {
112 	char *end;
113 
114 	port = htons(strtol(port_str, &end, 0));
115 	if (end == port_str)
116 	    return;
117     }
118     add_port (family, port, protocol);
119 }
120 
121 /*
122  * add the standard collection of ports for `family'
123  */
124 
125 static void
126 add_standard_ports (int family)
127 {
128     add_port_service(family, "kerberos", 88, "udp");
129     add_port_service(family, "kerberos", 88, "tcp");
130     add_port_service(family, "kerberos-sec", 88, "udp");
131     add_port_service(family, "kerberos-sec", 88, "tcp");
132     if(enable_http)
133 	add_port_service(family, "http", 80, "tcp");
134     if(enable_524) {
135 	add_port_service(family, "krb524", 4444, "udp");
136 	add_port_service(family, "krb524", 4444, "tcp");
137     }
138 #ifdef KRB4
139     if(enable_v4) {
140 	add_port_service(family, "kerberos-iv", 750, "udp");
141 	add_port_service(family, "kerberos-iv", 750, "tcp");
142     }
143     if (enable_kaserver)
144 	add_port_service(family, "afs3-kaserver", 7004, "udp");
145 #endif
146 }
147 
148 /*
149  * parse the set of space-delimited ports in `str' and add them.
150  * "+" => all the standard ones
151  * otherwise it's port|service[/protocol]
152  */
153 
154 static void
155 parse_ports(const char *str)
156 {
157     char *pos = NULL;
158     char *p;
159     char *str_copy = strdup (str);
160 
161     p = strtok_r(str_copy, " \t", &pos);
162     while(p != NULL) {
163 	if(strcmp(p, "+") == 0) {
164 #ifdef HAVE_IPV6
165 	    add_standard_ports(AF_INET6);
166 #endif
167 	    add_standard_ports(AF_INET);
168 	} else {
169 	    char *q = strchr(p, '/');
170 	    if(q){
171 		*q++ = 0;
172 #ifdef HAVE_IPV6
173 		add_port_string(AF_INET6, p, q);
174 #endif
175 		add_port_string(AF_INET, p, q);
176 	    }else {
177 #ifdef HAVE_IPV6
178 		add_port_string(AF_INET6, p, "udp");
179 		add_port_string(AF_INET6, p, "tcp");
180 #endif
181 		add_port_string(AF_INET, p, "udp");
182 		add_port_string(AF_INET, p, "tcp");
183 	    }
184 	}
185 
186 	p = strtok_r(NULL, " \t", &pos);
187     }
188     free (str_copy);
189 }
190 
191 /*
192  * every socket we listen on
193  */
194 
195 struct descr {
196     int s;
197     int type;
198     unsigned char *buf;
199     size_t size;
200     size_t len;
201     time_t timeout;
202     struct sockaddr_storage __ss;
203     struct sockaddr *sa;
204     socklen_t sock_len;
205     char addr_string[128];
206 };
207 
208 static void
209 init_descr(struct descr *d)
210 {
211     memset(d, 0, sizeof(*d));
212     d->sa = (struct sockaddr *)&d->__ss;
213     d->s = -1;
214 }
215 
216 /*
217  * re-initialize all `n' ->sa in `d'.
218  */
219 
220 static void
221 reinit_descrs (struct descr *d, int n)
222 {
223     int i;
224 
225     for (i = 0; i < n; ++i)
226 	d[i].sa = (struct sockaddr *)&d[i].__ss;
227 }
228 
229 /*
230  * Create the socket (family, type, port) in `d'
231  */
232 
233 static void
234 init_socket(struct descr *d, krb5_address *a, int family, int type, int port)
235 {
236     krb5_error_code ret;
237     struct sockaddr_storage __ss;
238     struct sockaddr *sa = (struct sockaddr *)&__ss;
239     int sa_size = sizeof(__ss);
240 
241     init_descr (d);
242 
243     ret = krb5_addr2sockaddr (context, a, sa, &sa_size, port);
244     if (ret) {
245 	krb5_warn(context, ret, "krb5_addr2sockaddr");
246 	close(d->s);
247 	d->s = -1;
248 	return;
249     }
250 
251     if (sa->sa_family != family)
252 	return;
253 
254     d->s = socket(family, type, 0);
255     if(d->s < 0){
256 	krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
257 	d->s = -1;
258 	return;
259     }
260 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
261     {
262 	int one = 1;
263 	setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
264     }
265 #endif
266     d->type = type;
267 
268     if(bind(d->s, sa, sa_size) < 0){
269 	char a_str[256];
270 	size_t len;
271 
272 	krb5_print_address (a, a_str, sizeof(a_str), &len);
273 	krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port));
274 	close(d->s);
275 	d->s = -1;
276 	return;
277     }
278     if(type == SOCK_STREAM && listen(d->s, SOMAXCONN) < 0){
279 	char a_str[256];
280 	size_t len;
281 
282 	krb5_print_address (a, a_str, sizeof(a_str), &len);
283 	krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port));
284 	close(d->s);
285 	d->s = -1;
286 	return;
287     }
288 }
289 
290 /*
291  * Allocate descriptors for all the sockets that we should listen on
292  * and return the number of them.
293  */
294 
295 static int
296 init_sockets(struct descr **desc)
297 {
298     krb5_error_code ret;
299     int i, j;
300     struct descr *d;
301     int num = 0;
302     krb5_addresses addresses;
303 
304     if (explicit_addresses.len) {
305 	addresses = explicit_addresses;
306     } else {
307 	ret = krb5_get_all_server_addrs (context, &addresses);
308 	if (ret)
309 	    krb5_err (context, 1, ret, "krb5_get_all_server_addrs");
310     }
311     parse_ports(port_str);
312     d = malloc(addresses.len * num_ports * sizeof(*d));
313     if (d == NULL)
314 	krb5_errx(context, 1, "malloc(%lu) failed",
315 		  (unsigned long)num_ports * sizeof(*d));
316 
317     for (i = 0; i < num_ports; i++){
318 	for (j = 0; j < addresses.len; ++j) {
319 	    init_socket(&d[num], &addresses.val[j],
320 			ports[i].family, ports[i].type, ports[i].port);
321 	    if(d[num].s != -1){
322 		char a_str[80];
323 		size_t len;
324 
325 		krb5_print_address (&addresses.val[j], a_str,
326 				    sizeof(a_str), &len);
327 
328 		kdc_log(5, "listening on %s port %u/%s",
329 			a_str,
330 			ntohs(ports[i].port),
331 			(ports[i].type == SOCK_STREAM) ? "tcp" : "udp");
332 		/* XXX */
333 		num++;
334 	    }
335 	}
336     }
337     krb5_free_addresses (context, &addresses);
338     d = realloc(d, num * sizeof(*d));
339     if (d == NULL && num != 0)
340 	krb5_errx(context, 1, "realloc(%lu) failed",
341 		  (unsigned long)num * sizeof(*d));
342     reinit_descrs (d, num);
343     *desc = d;
344     return num;
345 }
346 
347 /*
348  * handle the request in `buf, len', from `addr' (or `from' as a string),
349  * sending a reply in `reply'.
350  */
351 
352 static int
353 process_request(unsigned char *buf,
354 		size_t len,
355 		krb5_data *reply,
356 		int *sendlength,
357 		const char *from,
358 		struct sockaddr *addr)
359 {
360     KDC_REQ req;
361     Ticket ticket;
362     krb5_error_code ret;
363     size_t i;
364 
365     gettimeofday(&now, NULL);
366     if(decode_AS_REQ(buf, len, &req, &i) == 0){
367 	ret = as_rep(&req, reply, from, addr);
368 	free_AS_REQ(&req);
369 	return ret;
370     }else if(decode_TGS_REQ(buf, len, &req, &i) == 0){
371 	ret = tgs_rep(&req, reply, from, addr);
372 	free_TGS_REQ(&req);
373 	return ret;
374     }else if(decode_Ticket(buf, len, &ticket, &i) == 0){
375 	ret = do_524(&ticket, reply, from, addr);
376 	free_Ticket(&ticket);
377 	return ret;
378 #ifdef KRB4
379     } else if(maybe_version4(buf, len)){
380 	*sendlength = 0; /* elbitapmoc sdrawkcab XXX */
381 	do_version4(buf, len, reply, from, (struct sockaddr_in*)addr);
382 	return 0;
383     } else if (enable_kaserver) {
384 	ret = do_kaserver (buf, len, reply, from, (struct sockaddr_in*)addr);
385 	return ret;
386 #endif
387     }
388 
389     return -1;
390 }
391 
392 static void
393 addr_to_string(struct sockaddr *addr, size_t addr_len, char *str, size_t len)
394 {
395     krb5_address a;
396     if(krb5_sockaddr2address(context, addr, &a) == 0) {
397 	if(krb5_print_address(&a, str, len, &len) == 0) {
398 	    krb5_free_address(context, &a);
399 	    return;
400 	}
401 	krb5_free_address(context, &a);
402     }
403     snprintf(str, len, "<family=%d>", addr->sa_family);
404 }
405 
406 /*
407  * Handle the request in `buf, len' to socket `d'
408  */
409 
410 static void
411 do_request(void *buf, size_t len, int sendlength,
412 	   struct descr *d)
413 {
414     krb5_error_code ret;
415     krb5_data reply;
416 
417     reply.length = 0;
418     ret = process_request(buf, len, &reply, &sendlength,
419 			  d->addr_string, d->sa);
420     if(reply.length){
421 	kdc_log(5, "sending %lu bytes to %s", (unsigned long)reply.length,
422 		d->addr_string);
423 	if(sendlength){
424 	    unsigned char len[4];
425 	    len[0] = (reply.length >> 24) & 0xff;
426 	    len[1] = (reply.length >> 16) & 0xff;
427 	    len[2] = (reply.length >> 8) & 0xff;
428 	    len[3] = reply.length & 0xff;
429 	    if(sendto(d->s, len, sizeof(len), 0, d->sa, d->sock_len) < 0) {
430 		kdc_log (0, "sendto(%s): %s", d->addr_string, strerror(errno));
431 		krb5_data_free(&reply);
432 		return;
433 	    }
434 	}
435 	if(sendto(d->s, reply.data, reply.length, 0, d->sa, d->sock_len) < 0) {
436 	    kdc_log (0, "sendto(%s): %s", d->addr_string, strerror(errno));
437 	    krb5_data_free(&reply);
438 	    return;
439 	}
440 	krb5_data_free(&reply);
441     }
442     if(ret)
443 	kdc_log(0, "Failed processing %lu byte request from %s",
444 		(unsigned long)len, d->addr_string);
445 }
446 
447 /*
448  * Handle incoming data to the UDP socket in `d'
449  */
450 
451 static void
452 handle_udp(struct descr *d)
453 {
454     unsigned char *buf;
455     int n;
456 
457     buf = malloc(max_request);
458     if(buf == NULL){
459 	kdc_log(0, "Failed to allocate %lu bytes", (unsigned long)max_request);
460 	return;
461     }
462 
463     d->sock_len = sizeof(d->__ss);
464     n = recvfrom(d->s, buf, max_request, 0, d->sa, &d->sock_len);
465     if(n < 0)
466 	krb5_warn(context, errno, "recvfrom");
467     else {
468 	addr_to_string (d->sa, d->sock_len,
469 			d->addr_string, sizeof(d->addr_string));
470 	do_request(buf, n, 0, d);
471     }
472     free (buf);
473 }
474 
475 static void
476 clear_descr(struct descr *d)
477 {
478     if(d->buf)
479 	memset(d->buf, 0, d->size);
480     d->len = 0;
481     if(d->s != -1)
482 	close(d->s);
483     d->s = -1;
484 }
485 
486 
487 /* remove HTTP %-quoting from buf */
488 static int
489 de_http(char *buf)
490 {
491     char *p, *q;
492     for(p = q = buf; *p; p++, q++) {
493 	if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
494 	    unsigned int x;
495 	    if(sscanf(p + 1, "%2x", &x) != 1)
496 		return -1;
497 	    *q = x;
498 	    p += 2;
499 	} else
500 	    *q = *p;
501     }
502     *q = '\0';
503     return 0;
504 }
505 
506 #define TCP_TIMEOUT 4
507 
508 /*
509  * accept a new TCP connection on `d[parent]' and store it in `d[child]'
510  */
511 
512 static void
513 add_new_tcp (struct descr *d, int parent, int child)
514 {
515     int s;
516 
517     if (child == -1)
518 	return;
519 
520     d[child].sock_len = sizeof(d[child].__ss);
521     s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
522     if(s < 0) {
523 	krb5_warn(context, errno, "accept");
524 	return;
525     }
526 
527     if (s >= FD_SETSIZE) {
528 	krb5_warnx(context, "socket FD too large");
529 	close (s);
530 	return;
531     }
532 
533     d[child].s = s;
534     d[child].timeout = time(NULL) + TCP_TIMEOUT;
535     d[child].type = SOCK_STREAM;
536     addr_to_string (d[child].sa, d[child].sock_len,
537 		    d[child].addr_string, sizeof(d[child].addr_string));
538 }
539 
540 /*
541  * Grow `d' to handle at least `n'.
542  * Return != 0 if fails
543  */
544 
545 static int
546 grow_descr (struct descr *d, size_t n)
547 {
548     if (d->size - d->len < n) {
549 	unsigned char *tmp;
550 
551 	d->size += max(1024, d->len + n);
552 	if (d->size >= max_request) {
553 	    kdc_log(0, "Request exceeds max request size (%lu bytes).",
554 		    (unsigned long)d->size);
555 	    clear_descr(d);
556 	    return -1;
557 	}
558 	tmp = realloc (d->buf, d->size);
559 	if (tmp == NULL) {
560 	    kdc_log(0, "Failed to re-allocate %lu bytes.",
561 		    (unsigned long)d->size);
562 	    clear_descr(d);
563 	    return -1;
564 	}
565 	d->buf = tmp;
566     }
567     return 0;
568 }
569 
570 /*
571  * Try to handle the TCP data at `d->buf, d->len'.
572  * Return -1 if failed, 0 if succesful, and 1 if data is complete.
573  */
574 
575 static int
576 handle_vanilla_tcp (struct descr *d)
577 {
578     krb5_storage *sp;
579     int32_t len;
580 
581     sp = krb5_storage_from_mem(d->buf, d->len);
582     if (sp == NULL) {
583 	kdc_log (0, "krb5_storage_from_mem failed");
584 	return -1;
585     }
586     krb5_ret_int32(sp, &len);
587     krb5_storage_free(sp);
588     if(d->len - 4 >= len) {
589 	memmove(d->buf, d->buf + 4, d->len - 4);
590 	return 1;
591     }
592     return 0;
593 }
594 
595 /*
596  * Try to handle the TCP/HTTP data at `d->buf, d->len'.
597  * Return -1 if failed, 0 if succesful, and 1 if data is complete.
598  */
599 
600 static int
601 handle_http_tcp (struct descr *d)
602 {
603     char *s, *p, *t;
604     void *data;
605     char *proto;
606     int len;
607 
608     s = (char *)d->buf;
609 
610     p = strstr(s, "\r\n");
611     if (p == NULL) {
612 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
613 	return -1;
614     }
615     *p = 0;
616 
617     p = NULL;
618     t = strtok_r(s, " \t", &p);
619     if (t == NULL) {
620 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
621 	return -1;
622     }
623     t = strtok_r(NULL, " \t", &p);
624     if(t == NULL) {
625 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
626 	return -1;
627     }
628     data = malloc(strlen(t));
629     if (data == NULL) {
630 	kdc_log(0, "Failed to allocate %lu bytes",
631 		(unsigned long)strlen(t));
632 	return -1;
633     }
634     if(*t == '/')
635 	t++;
636     if(de_http(t) != 0) {
637 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
638 	kdc_log(5, "Request: %s", t);
639 	free(data);
640 	return -1;
641     }
642     proto = strtok_r(NULL, " \t", &p);
643     if (proto == NULL) {
644 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
645 	free(data);
646 	return -1;
647     }
648     len = base64_decode(t, data);
649     if(len <= 0){
650 	const char *msg =
651 	    " 404 Not found\r\n"
652 	    "Server: Heimdal/" VERSION "\r\n"
653 	    "Cache-Control: no-cache\r\n"
654 	    "Pragma: no-cache\r\n"
655 	    "Content-type: text/html\r\n"
656 	    "Content-transfer-encoding: 8bit\r\n\r\n"
657 	    "<TITLE>404 Not found</TITLE>\r\n"
658 	    "<H1>404 Not found</H1>\r\n"
659 	    "That page doesn't exist, maybe you are looking for "
660 	    "<A HREF=\"http://www.pdc.kth.se/heimdal/\">Heimdal</A>?\r\n";
661 	write(d->s, proto, strlen(proto));
662 	write(d->s, msg, strlen(msg));
663 	kdc_log(0, "HTTP request from %s is non KDC request", d->addr_string);
664 	kdc_log(5, "Request: %s", t);
665 	free(data);
666 	return -1;
667     }
668     {
669 	const char *msg =
670 	    " 200 OK\r\n"
671 	    "Server: Heimdal/" VERSION "\r\n"
672 	    "Cache-Control: no-cache\r\n"
673 	    "Pragma: no-cache\r\n"
674 	    "Content-type: application/octet-stream\r\n"
675 	    "Content-transfer-encoding: binary\r\n\r\n";
676 	write(d->s, proto, strlen(proto));
677 	write(d->s, msg, strlen(msg));
678     }
679     memcpy(d->buf, data, len);
680     d->len = len;
681     free(data);
682     return 1;
683 }
684 
685 /*
686  * Handle incoming data to the TCP socket in `d[index]'
687  */
688 
689 static void
690 handle_tcp(struct descr *d, int index, int min_free)
691 {
692     unsigned char buf[1024];
693     int n;
694     int ret = 0;
695 
696     if (d[index].timeout == 0) {
697 	add_new_tcp (d, index, min_free);
698 	return;
699     }
700 
701     n = recvfrom(d[index].s, buf, sizeof(buf), 0, NULL, NULL);
702     if(n < 0){
703 	krb5_warn(context, errno, "recvfrom");
704 	return;
705     }
706     if (grow_descr (&d[index], n))
707 	return;
708     memcpy(d[index].buf + d[index].len, buf, n);
709     d[index].len += n;
710     if(d[index].len > 4 && d[index].buf[0] == 0) {
711 	ret = handle_vanilla_tcp (&d[index]);
712     } else if(enable_http &&
713 	      d[index].len >= 4 &&
714 	      strncmp((char *)d[index].buf, "GET ", 4) == 0 &&
715 	      strncmp((char *)d[index].buf + d[index].len - 4,
716 		      "\r\n\r\n", 4) == 0) {
717 	ret = handle_http_tcp (&d[index]);
718 	if (ret < 0)
719 	    clear_descr (d + index);
720     } else if (d[index].len > 4) {
721 	kdc_log (0, "TCP data of strange type from %s", d[index].addr_string);
722 	return;
723     }
724     if (ret < 0)
725 	return;
726     else if (ret == 1) {
727 	do_request(d[index].buf, d[index].len, 1, &d[index]);
728 	clear_descr(d + index);
729     }
730 }
731 
732 void
733 loop(void)
734 {
735     struct descr *d;
736     int ndescr;
737 
738     ndescr = init_sockets(&d);
739     if(ndescr <= 0)
740 	krb5_errx(context, 1, "No sockets!");
741     while(exit_flag == 0){
742 	struct timeval tmout;
743 	fd_set fds;
744 	int min_free = -1;
745 	int max_fd = 0;
746 	int i;
747 
748 	FD_ZERO(&fds);
749 	for(i = 0; i < ndescr; i++) {
750 	    if(d[i].s >= 0){
751 		if(d[i].type == SOCK_STREAM &&
752 		   d[i].timeout && d[i].timeout < time(NULL)) {
753 		    kdc_log(1, "TCP-connection from %s expired after %lu bytes",
754 			    d[i].addr_string, (unsigned long)d[i].len);
755 		    clear_descr(&d[i]);
756 		    continue;
757 		}
758 		if(max_fd < d[i].s)
759 		    max_fd = d[i].s;
760 		if (max_fd >= FD_SETSIZE)
761 		    krb5_errx(context, 1, "fd too large");
762 		FD_SET(d[i].s, &fds);
763 	    } else if(min_free < 0 || i < min_free)
764 		min_free = i;
765 	}
766 	if(min_free == -1){
767 	    struct descr *tmp;
768 	    tmp = realloc(d, (ndescr + 4) * sizeof(*d));
769 	    if(tmp == NULL)
770 		krb5_warnx(context, "No memory");
771 	    else {
772 		d = tmp;
773 		reinit_descrs (d, ndescr);
774 		memset(d + ndescr, 0, 4 * sizeof(*d));
775 		for(i = ndescr; i < ndescr + 4; i++)
776 		    init_descr (&d[i]);
777 		min_free = ndescr;
778 		ndescr += 4;
779 	    }
780 	}
781 
782 	tmout.tv_sec = TCP_TIMEOUT;
783 	tmout.tv_usec = 0;
784 	switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
785 	case 0:
786 	    break;
787 	case -1:
788 	    if (errno != EINTR)
789 		krb5_warn(context, errno, "select");
790 	    break;
791 	default:
792 	    for(i = 0; i < ndescr; i++)
793 		if(d[i].s >= 0 && FD_ISSET(d[i].s, &fds)) {
794 		    if(d[i].type == SOCK_DGRAM)
795 			handle_udp(&d[i]);
796 		    else if(d[i].type == SOCK_STREAM)
797 			handle_tcp(d, i, min_free);
798 		}
799 	}
800     }
801     free (d);
802 }
803