xref: /freebsd/crypto/heimdal/kdc/connect.c (revision 71fe318b852b8dfb3e799cb12ef184750f7f8eac)
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.86 2002/08/12 13:29:48 joda 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 #ifdef KRB4
135     if(enable_v4) {
136 	add_port_service(family, "kerberos-iv", 750, "udp");
137 	add_port_service(family, "kerberos-iv", 750, "tcp");
138     }
139     if(enable_524) {
140 	add_port_service(family, "krb524", 4444, "udp");
141 	add_port_service(family, "krb524", 4444, "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-intialize 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;
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 #ifdef KRB4
362     Ticket ticket;
363 #endif
364     krb5_error_code ret;
365     size_t i;
366 
367     gettimeofday(&now, NULL);
368     if(decode_AS_REQ(buf, len, &req, &i) == 0){
369 	ret = as_rep(&req, reply, from, addr);
370 	free_AS_REQ(&req);
371 	return ret;
372     }else if(decode_TGS_REQ(buf, len, &req, &i) == 0){
373 	ret = tgs_rep(&req, reply, from, addr);
374 	free_TGS_REQ(&req);
375 	return ret;
376     }
377 #ifdef KRB4
378     else if(maybe_version4(buf, len)){
379 	*sendlength = 0; /* elbitapmoc sdrawkcab XXX */
380 	do_version4(buf, len, reply, from, (struct sockaddr_in*)addr);
381 	return 0;
382     }else if(decode_Ticket(buf, len, &ticket, &i) == 0){
383 	ret = do_524(&ticket, reply, from, addr);
384 	free_Ticket(&ticket);
385 	return ret;
386     } else if (enable_kaserver) {
387 	ret = do_kaserver (buf, len, reply, from, (struct sockaddr_in*)addr);
388 	return ret;
389     }
390 #endif
391 
392     return -1;
393 }
394 
395 static void
396 addr_to_string(struct sockaddr *addr, size_t addr_len, char *str, size_t len)
397 {
398     krb5_address a;
399     if(krb5_sockaddr2address(context, addr, &a) == 0) {
400 	if(krb5_print_address(&a, str, len, &len) == 0) {
401 	    krb5_free_address(context, &a);
402 	    return;
403 	}
404 	krb5_free_address(context, &a);
405     }
406     snprintf(str, len, "<family=%d>", addr->sa_family);
407 }
408 
409 /*
410  * Handle the request in `buf, len' to socket `d'
411  */
412 
413 static void
414 do_request(void *buf, size_t len, int sendlength,
415 	   struct descr *d)
416 {
417     krb5_error_code ret;
418     krb5_data reply;
419 
420     reply.length = 0;
421     ret = process_request(buf, len, &reply, &sendlength,
422 			  d->addr_string, d->sa);
423     if(reply.length){
424 	kdc_log(5, "sending %lu bytes to %s", (unsigned long)reply.length,
425 		d->addr_string);
426 	if(sendlength){
427 	    unsigned char len[4];
428 	    len[0] = (reply.length >> 24) & 0xff;
429 	    len[1] = (reply.length >> 16) & 0xff;
430 	    len[2] = (reply.length >> 8) & 0xff;
431 	    len[3] = reply.length & 0xff;
432 	    if(sendto(d->s, len, sizeof(len), 0, d->sa, d->sock_len) < 0) {
433 		kdc_log (0, "sendto(%s): %s", d->addr_string, strerror(errno));
434 		krb5_data_free(&reply);
435 		return;
436 	    }
437 	}
438 	if(sendto(d->s, reply.data, reply.length, 0, d->sa, d->sock_len) < 0) {
439 	    kdc_log (0, "sendto(%s): %s", d->addr_string, strerror(errno));
440 	    krb5_data_free(&reply);
441 	    return;
442 	}
443 	krb5_data_free(&reply);
444     }
445     if(ret)
446 	kdc_log(0, "Failed processing %lu byte request from %s",
447 		(unsigned long)len, d->addr_string);
448 }
449 
450 /*
451  * Handle incoming data to the UDP socket in `d'
452  */
453 
454 static void
455 handle_udp(struct descr *d)
456 {
457     unsigned char *buf;
458     int n;
459 
460     buf = malloc(max_request);
461     if(buf == NULL){
462 	kdc_log(0, "Failed to allocate %lu bytes", (unsigned long)max_request);
463 	return;
464     }
465 
466     d->sock_len = sizeof(d->__ss);
467     n = recvfrom(d->s, buf, max_request, 0, d->sa, &d->sock_len);
468     if(n < 0)
469 	krb5_warn(context, errno, "recvfrom");
470     else {
471 	addr_to_string (d->sa, d->sock_len,
472 			d->addr_string, sizeof(d->addr_string));
473 	do_request(buf, n, 0, d);
474     }
475     free (buf);
476 }
477 
478 static void
479 clear_descr(struct descr *d)
480 {
481     if(d->buf)
482 	memset(d->buf, 0, d->size);
483     d->len = 0;
484     if(d->s != -1)
485 	close(d->s);
486     d->s = -1;
487 }
488 
489 
490 /* remove HTTP %-quoting from buf */
491 static int
492 de_http(char *buf)
493 {
494     char *p, *q;
495     for(p = q = buf; *p; p++, q++) {
496 	if(*p == '%') {
497 	    unsigned int x;
498 	    if(sscanf(p + 1, "%2x", &x) != 1)
499 		return -1;
500 	    *q = x;
501 	    p += 2;
502 	} else
503 	    *q = *p;
504     }
505     *q = '\0';
506     return 0;
507 }
508 
509 #define TCP_TIMEOUT 4
510 
511 /*
512  * accept a new TCP connection on `d[parent]' and store it in `d[child]'
513  */
514 
515 static void
516 add_new_tcp (struct descr *d, int parent, int child)
517 {
518     int s;
519 
520     if (child == -1)
521 	return;
522 
523     d[child].sock_len = sizeof(d[child].__ss);
524     s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
525     if(s < 0) {
526 	krb5_warn(context, errno, "accept");
527 	return;
528     }
529 
530     if (s >= FD_SETSIZE) {
531 	krb5_warnx(context, "socket FD too large");
532 	close (s);
533 	return;
534     }
535 
536     d[child].s = s;
537     d[child].timeout = time(NULL) + TCP_TIMEOUT;
538     d[child].type = SOCK_STREAM;
539     addr_to_string (d[child].sa, d[child].sock_len,
540 		    d[child].addr_string, sizeof(d[child].addr_string));
541 }
542 
543 /*
544  * Grow `d' to handle at least `n'.
545  * Return != 0 if fails
546  */
547 
548 static int
549 grow_descr (struct descr *d, size_t n)
550 {
551     if (d->size - d->len < n) {
552 	unsigned char *tmp;
553 
554 	d->size += max(1024, d->len + n);
555 	if (d->size >= max_request) {
556 	    kdc_log(0, "Request exceeds max request size (%lu bytes).",
557 		    (unsigned long)d->size);
558 	    clear_descr(d);
559 	    return -1;
560 	}
561 	tmp = realloc (d->buf, d->size);
562 	if (tmp == NULL) {
563 	    kdc_log(0, "Failed to re-allocate %lu bytes.",
564 		    (unsigned long)d->size);
565 	    clear_descr(d);
566 	    return -1;
567 	}
568 	d->buf = tmp;
569     }
570     return 0;
571 }
572 
573 /*
574  * Try to handle the TCP data at `d->buf, d->len'.
575  * Return -1 if failed, 0 if succesful, and 1 if data is complete.
576  */
577 
578 static int
579 handle_vanilla_tcp (struct descr *d)
580 {
581     krb5_storage *sp;
582     int32_t len;
583 
584     sp = krb5_storage_from_mem(d->buf, d->len);
585     if (sp == NULL) {
586 	kdc_log (0, "krb5_storage_from_mem failed");
587 	return -1;
588     }
589     krb5_ret_int32(sp, &len);
590     krb5_storage_free(sp);
591     if(d->len - 4 >= len) {
592 	memmove(d->buf, d->buf + 4, d->len - 4);
593 	return 1;
594     }
595     return 0;
596 }
597 
598 /*
599  * Try to handle the TCP/HTTP data at `d->buf, d->len'.
600  * Return -1 if failed, 0 if succesful, and 1 if data is complete.
601  */
602 
603 static int
604 handle_http_tcp (struct descr *d)
605 {
606     char *s, *p, *t;
607     void *data;
608     char *proto;
609     int len;
610 
611     s = (char *)d->buf;
612 
613     p = strstr(s, "\r\n");
614     if (p == NULL) {
615 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
616 	return -1;
617     }
618     *p = 0;
619 
620     p = NULL;
621     t = strtok_r(s, " \t", &p);
622     if (t == NULL) {
623 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
624 	return -1;
625     }
626     t = strtok_r(NULL, " \t", &p);
627     if(t == NULL) {
628 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
629 	return -1;
630     }
631     data = malloc(strlen(t));
632     if (data == NULL) {
633 	kdc_log(0, "Failed to allocate %lu bytes",
634 		(unsigned long)strlen(t));
635 	return -1;
636     }
637     if(*t == '/')
638 	t++;
639     if(de_http(t) != 0) {
640 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
641 	kdc_log(5, "Request: %s", t);
642 	free(data);
643 	return -1;
644     }
645     proto = strtok_r(NULL, " \t", &p);
646     if (proto == NULL) {
647 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
648 	free(data);
649 	return -1;
650     }
651     len = base64_decode(t, data);
652     if(len <= 0){
653 	const char *msg =
654 	    " 404 Not found\r\n"
655 	    "Server: Heimdal/" VERSION "\r\n"
656 	    "Cache-Control: no-cache\r\n"
657 	    "Pragma: no-cache\r\n"
658 	    "Content-type: text/html\r\n"
659 	    "Content-transfer-encoding: 8bit\r\n\r\n"
660 	    "<TITLE>404 Not found</TITLE>\r\n"
661 	    "<H1>404 Not found</H1>\r\n"
662 	    "That page doesn't exist, maybe you are looking for "
663 	    "<A HREF=\"http://www.pdc.kth.se/heimdal/\">Heimdal</A>?\r\n";
664 	write(d->s, proto, strlen(proto));
665 	write(d->s, msg, strlen(msg));
666 	kdc_log(0, "HTTP request from %s is non KDC request", d->addr_string);
667 	kdc_log(5, "Request: %s", t);
668 	free(data);
669 	return -1;
670     }
671     {
672 	const char *msg =
673 	    " 200 OK\r\n"
674 	    "Server: Heimdal/" VERSION "\r\n"
675 	    "Cache-Control: no-cache\r\n"
676 	    "Pragma: no-cache\r\n"
677 	    "Content-type: application/octet-stream\r\n"
678 	    "Content-transfer-encoding: binary\r\n\r\n";
679 	write(d->s, proto, strlen(proto));
680 	write(d->s, msg, strlen(msg));
681     }
682     memcpy(d->buf, data, len);
683     d->len = len;
684     free(data);
685     return 1;
686 }
687 
688 /*
689  * Handle incoming data to the TCP socket in `d[index]'
690  */
691 
692 static void
693 handle_tcp(struct descr *d, int index, int min_free)
694 {
695     unsigned char buf[1024];
696     int n;
697     int ret = 0;
698 
699     if (d[index].timeout == 0) {
700 	add_new_tcp (d, index, min_free);
701 	return;
702     }
703 
704     n = recvfrom(d[index].s, buf, sizeof(buf), 0, NULL, NULL);
705     if(n < 0){
706 	krb5_warn(context, errno, "recvfrom");
707 	return;
708     }
709     if (grow_descr (&d[index], n))
710 	return;
711     memcpy(d[index].buf + d[index].len, buf, n);
712     d[index].len += n;
713     if(d[index].len > 4 && d[index].buf[0] == 0) {
714 	ret = handle_vanilla_tcp (&d[index]);
715     } else if(enable_http &&
716 	      d[index].len >= 4 &&
717 	      strncmp((char *)d[index].buf, "GET ", 4) == 0 &&
718 	      strncmp((char *)d[index].buf + d[index].len - 4,
719 		      "\r\n\r\n", 4) == 0) {
720 	ret = handle_http_tcp (&d[index]);
721 	if (ret < 0)
722 	    clear_descr (d + index);
723     } else if (d[index].len > 4) {
724 	kdc_log (0, "TCP data of strange type from %s", d[index].addr_string);
725 	return;
726     }
727     if (ret < 0)
728 	return;
729     else if (ret == 1) {
730 	do_request(d[index].buf, d[index].len, 1, &d[index]);
731 	clear_descr(d + index);
732     }
733 }
734 
735 void
736 loop(void)
737 {
738     struct descr *d;
739     int ndescr;
740 
741     ndescr = init_sockets(&d);
742     if(ndescr <= 0)
743 	krb5_errx(context, 1, "No sockets!");
744     while(exit_flag == 0){
745 	struct timeval tmout;
746 	fd_set fds;
747 	int min_free = -1;
748 	int max_fd = 0;
749 	int i;
750 
751 	FD_ZERO(&fds);
752 	for(i = 0; i < ndescr; i++) {
753 	    if(d[i].s >= 0){
754 		if(d[i].type == SOCK_STREAM &&
755 		   d[i].timeout && d[i].timeout < time(NULL)) {
756 		    kdc_log(1, "TCP-connection from %s expired after %lu bytes",
757 			    d[i].addr_string, (unsigned long)d[i].len);
758 		    clear_descr(&d[i]);
759 		    continue;
760 		}
761 		if(max_fd < d[i].s)
762 		    max_fd = d[i].s;
763 		if (max_fd >= FD_SETSIZE)
764 		    krb5_errx(context, 1, "fd too large");
765 		FD_SET(d[i].s, &fds);
766 	    } else if(min_free < 0 || i < min_free)
767 		min_free = i;
768 	}
769 	if(min_free == -1){
770 	    struct descr *tmp;
771 	    tmp = realloc(d, (ndescr + 4) * sizeof(*d));
772 	    if(tmp == NULL)
773 		krb5_warnx(context, "No memory");
774 	    else {
775 		d = tmp;
776 		reinit_descrs (d, ndescr);
777 		memset(d + ndescr, 0, 4 * sizeof(*d));
778 		for(i = ndescr; i < ndescr + 4; i++)
779 		    init_descr (&d[i]);
780 		min_free = ndescr;
781 		ndescr += 4;
782 	    }
783 	}
784 
785 	tmout.tv_sec = TCP_TIMEOUT;
786 	tmout.tv_usec = 0;
787 	switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
788 	case 0:
789 	    break;
790 	case -1:
791 	    if (errno != EINTR)
792 		krb5_warn(context, errno, "select");
793 	    break;
794 	default:
795 	    for(i = 0; i < ndescr; i++)
796 		if(d[i].s >= 0 && FD_ISSET(d[i].s, &fds)) {
797 		    if(d[i].type == SOCK_DGRAM)
798 			handle_udp(&d[i]);
799 		    else if(d[i].type == SOCK_STREAM)
800 			handle_tcp(d, i, min_free);
801 		}
802 	}
803     }
804     free (d);
805 }
806