xref: /freebsd/crypto/heimdal/kdc/connect.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
1 /*
2  * Copyright (c) 1997-2001 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.82 2001/05/14 06:18:11 assar 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 #endif
144 #ifdef KASERVER
145     if (enable_kaserver)
146 	add_port_service(family, "afs3-kaserver", 7004, "udp");
147 #endif
148 }
149 
150 /*
151  * parse the set of space-delimited ports in `str' and add them.
152  * "+" => all the standard ones
153  * otherwise it's port|service[/protocol]
154  */
155 
156 static void
157 parse_ports(const char *str)
158 {
159     char *pos = NULL;
160     char *p;
161     char *str_copy = strdup (str);
162 
163     p = strtok_r(str_copy, " \t", &pos);
164     while(p != NULL) {
165 	if(strcmp(p, "+") == 0) {
166 #ifdef HAVE_IPV6
167 	    add_standard_ports(AF_INET6);
168 #endif
169 	    add_standard_ports(AF_INET);
170 	} else {
171 	    char *q = strchr(p, '/');
172 	    if(q){
173 		*q++ = 0;
174 #ifdef HAVE_IPV6
175 		add_port_string(AF_INET6, p, q);
176 #endif
177 		add_port_string(AF_INET, p, q);
178 	    }else {
179 #ifdef HAVE_IPV6
180 		add_port_string(AF_INET6, p, "udp");
181 		add_port_string(AF_INET6, p, "tcp");
182 #endif
183 		add_port_string(AF_INET, p, "udp");
184 		add_port_string(AF_INET, p, "tcp");
185 	    }
186 	}
187 
188 	p = strtok_r(NULL, " \t", &pos);
189     }
190     free (str_copy);
191 }
192 
193 /*
194  * every socket we listen on
195  */
196 
197 struct descr {
198     int s;
199     int type;
200     unsigned char *buf;
201     size_t size;
202     size_t len;
203     time_t timeout;
204     struct sockaddr_storage __ss;
205     struct sockaddr *sa;
206     socklen_t sock_len;
207     char addr_string[128];
208 };
209 
210 static void
211 init_descr(struct descr *d)
212 {
213     memset(d, 0, sizeof(*d));
214     d->sa = (struct sockaddr *)&d->__ss;
215     d->s = -1;
216 }
217 
218 /*
219  * re-intialize all `n' ->sa in `d'.
220  */
221 
222 static void
223 reinit_descrs (struct descr *d, int n)
224 {
225     int i;
226 
227     for (i = 0; i < n; ++i)
228 	d[i].sa = (struct sockaddr *)&d[i].__ss;
229 }
230 
231 /*
232  * Create the socket (family, type, port) in `d'
233  */
234 
235 static void
236 init_socket(struct descr *d, krb5_address *a, int family, int type, int port)
237 {
238     krb5_error_code ret;
239     struct sockaddr_storage __ss;
240     struct sockaddr *sa = (struct sockaddr *)&__ss;
241     int sa_size;
242 
243     init_descr (d);
244 
245     ret = krb5_addr2sockaddr (context, a, sa, &sa_size, port);
246     if (ret) {
247 	krb5_warn(context, ret, "krb5_addr2sockaddr");
248 	close(d->s);
249 	d->s = -1;
250 	return;
251     }
252 
253     if (sa->sa_family != family)
254 	return;
255 
256     d->s = socket(family, type, 0);
257     if(d->s < 0){
258 	krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
259 	d->s = -1;
260 	return;
261     }
262 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
263     {
264 	int one = 1;
265 	setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
266     }
267 #endif
268     d->type = type;
269 
270     if(bind(d->s, sa, sa_size) < 0){
271 	char a_str[256];
272 	size_t len;
273 
274 	krb5_print_address (a, a_str, sizeof(a_str), &len);
275 	krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port));
276 	close(d->s);
277 	d->s = -1;
278 	return;
279     }
280     if(type == SOCK_STREAM && listen(d->s, SOMAXCONN) < 0){
281 	char a_str[256];
282 	size_t len;
283 
284 	krb5_print_address (a, a_str, sizeof(a_str), &len);
285 	krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port));
286 	close(d->s);
287 	d->s = -1;
288 	return;
289     }
290 }
291 
292 /*
293  * Allocate descriptors for all the sockets that we should listen on
294  * and return the number of them.
295  */
296 
297 static int
298 init_sockets(struct descr **desc)
299 {
300     krb5_error_code ret;
301     int i, j;
302     struct descr *d;
303     int num = 0;
304     krb5_addresses addresses;
305 
306     if (explicit_addresses.len) {
307 	addresses = explicit_addresses;
308     } else {
309 	ret = krb5_get_all_server_addrs (context, &addresses);
310 	if (ret)
311 	    krb5_err (context, 1, ret, "krb5_get_all_server_addrs");
312     }
313     parse_ports(port_str);
314     d = malloc(addresses.len * num_ports * sizeof(*d));
315     if (d == NULL)
316 	krb5_errx(context, 1, "malloc(%lu) failed",
317 		  (unsigned long)num_ports * sizeof(*d));
318 
319     for (i = 0; i < num_ports; i++){
320 	for (j = 0; j < addresses.len; ++j) {
321 	    init_socket(&d[num], &addresses.val[j],
322 			ports[i].family, ports[i].type, ports[i].port);
323 	    if(d[num].s != -1){
324 		char a_str[80];
325 		size_t len;
326 
327 		krb5_print_address (&addresses.val[j], a_str,
328 				    sizeof(a_str), &len);
329 
330 		kdc_log(5, "listening on %s port %u/%s",
331 			a_str,
332 			ntohs(ports[i].port),
333 			(ports[i].type == SOCK_STREAM) ? "tcp" : "udp");
334 		/* XXX */
335 		num++;
336 	    }
337 	}
338     }
339     krb5_free_addresses (context, &addresses);
340     d = realloc(d, num * sizeof(*d));
341     if (d == NULL && num != 0)
342 	krb5_errx(context, 1, "realloc(%lu) failed",
343 		  (unsigned long)num * sizeof(*d));
344     reinit_descrs (d, num);
345     *desc = d;
346     return num;
347 }
348 
349 /*
350  * handle the request in `buf, len', from `addr' (or `from' as a string),
351  * sending a reply in `reply'.
352  */
353 
354 static int
355 process_request(unsigned char *buf,
356 		size_t len,
357 		krb5_data *reply,
358 		int *sendlength,
359 		const char *from,
360 		struct sockaddr *addr)
361 {
362     KDC_REQ req;
363 #ifdef KRB4
364     Ticket ticket;
365 #endif
366     krb5_error_code ret;
367     size_t i;
368 
369     gettimeofday(&now, NULL);
370     if(decode_AS_REQ(buf, len, &req, &i) == 0){
371 	ret = as_rep(&req, reply, from, addr);
372 	free_AS_REQ(&req);
373 	return ret;
374     }else if(decode_TGS_REQ(buf, len, &req, &i) == 0){
375 	ret = tgs_rep(&req, reply, from, addr);
376 	free_TGS_REQ(&req);
377 	return ret;
378     }
379 #ifdef KRB4
380     else if(maybe_version4(buf, len)){
381 	*sendlength = 0; /* elbitapmoc sdrawkcab XXX */
382 	do_version4(buf, len, reply, from, (struct sockaddr_in*)addr);
383 	return 0;
384     }else if(decode_Ticket(buf, len, &ticket, &i) == 0){
385 	ret = do_524(&ticket, reply, from, addr);
386 	free_Ticket(&ticket);
387 	return ret;
388     }
389 #endif
390 #ifdef KASERVER
391     else if (enable_kaserver) {
392 	ret = do_kaserver (buf, len, reply, from, (struct sockaddr_in*)addr);
393 	return ret;
394     }
395 #endif
396 
397     return -1;
398 }
399 
400 static void
401 addr_to_string(struct sockaddr *addr, size_t addr_len, char *str, size_t len)
402 {
403     krb5_address a;
404     krb5_sockaddr2address(context, addr, &a);
405     if(krb5_print_address(&a, str, len, &len) == 0) {
406 	krb5_free_address(context, &a);
407 	return;
408     }
409     krb5_free_address(context, &a);
410     snprintf(str, len, "<family=%d>", addr->sa_family);
411 }
412 
413 /*
414  * Handle the request in `buf, len' to socket `d'
415  */
416 
417 static void
418 do_request(void *buf, size_t len, int sendlength,
419 	   struct descr *d)
420 {
421     krb5_error_code ret;
422     krb5_data reply;
423 
424     reply.length = 0;
425     ret = process_request(buf, len, &reply, &sendlength,
426 			  d->addr_string, d->sa);
427     if(reply.length){
428 	kdc_log(5, "sending %d bytes to %s", reply.length, d->addr_string);
429 	if(sendlength){
430 	    unsigned char len[4];
431 	    len[0] = (reply.length >> 24) & 0xff;
432 	    len[1] = (reply.length >> 16) & 0xff;
433 	    len[2] = (reply.length >> 8) & 0xff;
434 	    len[3] = reply.length & 0xff;
435 	    if(sendto(d->s, len, sizeof(len), 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 	}
441 	if(sendto(d->s, reply.data, reply.length, 0, d->sa, d->sock_len) < 0) {
442 	    kdc_log (0, "sendto(%s): %s", d->addr_string, strerror(errno));
443 	    krb5_data_free(&reply);
444 	    return;
445 	}
446 	krb5_data_free(&reply);
447     }
448     if(ret)
449 	kdc_log(0, "Failed processing %lu byte request from %s",
450 		(unsigned long)len, d->addr_string);
451 }
452 
453 /*
454  * Handle incoming data to the UDP socket in `d'
455  */
456 
457 static void
458 handle_udp(struct descr *d)
459 {
460     unsigned char *buf;
461     int n;
462 
463     buf = malloc(max_request);
464     if(buf == NULL){
465 	kdc_log(0, "Failed to allocate %lu bytes", (unsigned long)max_request);
466 	return;
467     }
468 
469     d->sock_len = sizeof(d->__ss);
470     n = recvfrom(d->s, buf, max_request, 0, d->sa, &d->sock_len);
471     if(n < 0)
472 	krb5_warn(context, errno, "recvfrom");
473     else {
474 	addr_to_string (d->sa, d->sock_len,
475 			d->addr_string, sizeof(d->addr_string));
476 	do_request(buf, n, 0, d);
477     }
478     free (buf);
479 }
480 
481 static void
482 clear_descr(struct descr *d)
483 {
484     if(d->buf)
485 	memset(d->buf, 0, d->size);
486     d->len = 0;
487     if(d->s != -1)
488 	close(d->s);
489     d->s = -1;
490 }
491 
492 
493 /* remove HTTP %-quoting from buf */
494 static int
495 de_http(char *buf)
496 {
497     char *p, *q;
498     for(p = q = buf; *p; p++, q++) {
499 	if(*p == '%') {
500 	    unsigned int x;
501 	    if(sscanf(p + 1, "%2x", &x) != 1)
502 		return -1;
503 	    *q = x;
504 	    p += 2;
505 	} else
506 	    *q = *p;
507     }
508     *q = '\0';
509     return 0;
510 }
511 
512 #define TCP_TIMEOUT 4
513 
514 /*
515  * accept a new TCP connection on `d[parent]' and store it in `d[child]'
516  */
517 
518 static void
519 add_new_tcp (struct descr *d, int parent, int child)
520 {
521     int s;
522 
523     if (child == -1)
524 	return;
525 
526     d[child].sock_len = sizeof(d[child].__ss);
527     s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
528     if(s < 0) {
529 	krb5_warn(context, errno, "accept");
530 	return;
531     }
532 
533     if (s >= FD_SETSIZE) {
534 	krb5_warnx(context, "socket FD too large");
535 	close (s);
536 	return;
537     }
538 
539     d[child].s = s;
540     d[child].timeout = time(NULL) + TCP_TIMEOUT;
541     d[child].type = SOCK_STREAM;
542     addr_to_string (d[child].sa, d[child].sock_len,
543 		    d[child].addr_string, sizeof(d[child].addr_string));
544 }
545 
546 /*
547  * Grow `d' to handle at least `n'.
548  * Return != 0 if fails
549  */
550 
551 static int
552 grow_descr (struct descr *d, size_t n)
553 {
554     if (d->size - d->len < n) {
555 	unsigned char *tmp;
556 
557 	d->size += max(1024, d->len + n);
558 	if (d->size >= max_request) {
559 	    kdc_log(0, "Request exceeds max request size (%lu bytes).",
560 		    (unsigned long)d->size);
561 	    clear_descr(d);
562 	    return -1;
563 	}
564 	tmp = realloc (d->buf, d->size);
565 	if (tmp == NULL) {
566 	    kdc_log(0, "Failed to re-allocate %lu bytes.",
567 		    (unsigned long)d->size);
568 	    clear_descr(d);
569 	    return -1;
570 	}
571 	d->buf = tmp;
572     }
573     return 0;
574 }
575 
576 /*
577  * Try to handle the TCP data at `d->buf, d->len'.
578  * Return -1 if failed, 0 if succesful, and 1 if data is complete.
579  */
580 
581 static int
582 handle_vanilla_tcp (struct descr *d)
583 {
584     krb5_storage *sp;
585     int32_t len;
586 
587     sp = krb5_storage_from_mem(d->buf, d->len);
588     if (sp == NULL) {
589 	kdc_log (0, "krb5_storage_from_mem failed");
590 	return -1;
591     }
592     krb5_ret_int32(sp, &len);
593     krb5_storage_free(sp);
594     if(d->len - 4 >= len) {
595 	memmove(d->buf, d->buf + 4, d->len - 4);
596 	return 1;
597     }
598     return 0;
599 }
600 
601 /*
602  * Try to handle the TCP/HTTP data at `d->buf, d->len'.
603  * Return -1 if failed, 0 if succesful, and 1 if data is complete.
604  */
605 
606 static int
607 handle_http_tcp (struct descr *d)
608 {
609     char *s, *p, *t;
610     void *data;
611     char *proto;
612     int len;
613 
614     s = (char *)d->buf;
615 
616     p = strstr(s, "\r\n");
617     if (p == NULL) {
618 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
619 	return -1;
620     }
621     *p = 0;
622 
623     p = NULL;
624     t = strtok_r(s, " \t", &p);
625     if (t == NULL) {
626 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
627 	return -1;
628     }
629     t = strtok_r(NULL, " \t", &p);
630     if(t == NULL) {
631 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
632 	return -1;
633     }
634     data = malloc(strlen(t));
635     if (data == NULL) {
636 	kdc_log(0, "Failed to allocate %lu bytes",
637 		(unsigned long)strlen(t));
638 	return -1;
639     }
640     if(*t == '/')
641 	t++;
642     if(de_http(t) != 0) {
643 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
644 	kdc_log(5, "Request: %s", t);
645 	free(data);
646 	return -1;
647     }
648     proto = strtok_r(NULL, " \t", &p);
649     if (proto == NULL) {
650 	kdc_log(0, "Malformed HTTP request from %s", d->addr_string);
651 	free(data);
652 	return -1;
653     }
654     len = base64_decode(t, data);
655     if(len <= 0){
656 	const char *msg =
657 	    " 404 Not found\r\n"
658 	    "Server: Heimdal/" VERSION "\r\n"
659 	    "Content-type: text/html\r\n"
660 	    "Content-transfer-encoding: 8bit\r\n\r\n"
661 	    "<TITLE>404 Not found</TITLE>\r\n"
662 	    "<H1>404 Not found</H1>\r\n"
663 	    "That page doesn't exist, maybe you are looking for "
664 	    "<A HREF=\"http://www.pdc.kth.se/heimdal\">Heimdal</A>?\r\n";
665 	write(d->s, proto, strlen(proto));
666 	write(d->s, msg, strlen(msg));
667 	kdc_log(0, "HTTP request from %s is non KDC request", d->addr_string);
668 	kdc_log(5, "Request: %s", t);
669 	free(data);
670 	return -1;
671     }
672     {
673 	const char *msg =
674 	    " 200 OK\r\n"
675 	    "Server: Heimdal/" VERSION "\r\n"
676 	    "Content-type: application/octet-stream\r\n"
677 	    "Content-transfer-encoding: binary\r\n\r\n";
678 	write(d->s, proto, strlen(proto));
679 	write(d->s, msg, strlen(msg));
680     }
681     memcpy(d->buf, data, len);
682     d->len = len;
683     free(data);
684     return 1;
685 }
686 
687 /*
688  * Handle incoming data to the TCP socket in `d[index]'
689  */
690 
691 static void
692 handle_tcp(struct descr *d, int index, int min_free)
693 {
694     unsigned char buf[1024];
695     int n;
696     int ret = 0;
697 
698     if (d[index].timeout == 0) {
699 	add_new_tcp (d, index, min_free);
700 	return;
701     }
702 
703     n = recvfrom(d[index].s, buf, sizeof(buf), 0, NULL, NULL);
704     if(n < 0){
705 	krb5_warn(context, errno, "recvfrom");
706 	return;
707     }
708     if (grow_descr (&d[index], n))
709 	return;
710     memcpy(d[index].buf + d[index].len, buf, n);
711     d[index].len += n;
712     if(d[index].len > 4 && d[index].buf[0] == 0) {
713 	ret = handle_vanilla_tcp (&d[index]);
714     } else if(enable_http &&
715 	      d[index].len >= 4 &&
716 	      strncmp((char *)d[index].buf, "GET ", 4) == 0 &&
717 	      strncmp((char *)d[index].buf + d[index].len - 4,
718 		      "\r\n\r\n", 4) == 0) {
719 	ret = handle_http_tcp (&d[index]);
720 	if (ret < 0)
721 	    clear_descr (d + index);
722     } else if (d[index].len > 4) {
723 	kdc_log (0, "TCP data of strange type from %s", d[index].addr_string);
724 	return;
725     }
726     if (ret < 0)
727 	return;
728     else if (ret == 1) {
729 	do_request(d[index].buf, d[index].len, 1, &d[index]);
730 	clear_descr(d + index);
731     }
732 }
733 
734 void
735 loop(void)
736 {
737     struct descr *d;
738     int ndescr;
739 
740     ndescr = init_sockets(&d);
741     if(ndescr <= 0)
742 	krb5_errx(context, 1, "No sockets!");
743     while(exit_flag == 0){
744 	struct timeval tmout;
745 	fd_set fds;
746 	int min_free = -1;
747 	int max_fd = 0;
748 	int i;
749 
750 	FD_ZERO(&fds);
751 	for(i = 0; i < ndescr; i++) {
752 	    if(d[i].s >= 0){
753 		if(d[i].type == SOCK_STREAM &&
754 		   d[i].timeout && d[i].timeout < time(NULL)) {
755 		    kdc_log(1, "TCP-connection from %s expired after %lu bytes",
756 			    d[i].addr_string, (unsigned long)d[i].len);
757 		    clear_descr(&d[i]);
758 		    continue;
759 		}
760 		if(max_fd < d[i].s)
761 		    max_fd = d[i].s;
762 		if (max_fd >= FD_SETSIZE)
763 		    krb5_errx(context, 1, "fd too large");
764 		FD_SET(d[i].s, &fds);
765 	    } else if(min_free < 0 || i < min_free)
766 		min_free = i;
767 	}
768 	if(min_free == -1){
769 	    struct descr *tmp;
770 	    tmp = realloc(d, (ndescr + 4) * sizeof(*d));
771 	    if(tmp == NULL)
772 		krb5_warnx(context, "No memory");
773 	    else {
774 		d = tmp;
775 		reinit_descrs (d, ndescr);
776 		memset(d + ndescr, 0, 4 * sizeof(*d));
777 		for(i = ndescr; i < ndescr + 4; i++)
778 		    init_descr (&d[i]);
779 		min_free = ndescr;
780 		ndescr += 4;
781 	    }
782 	}
783 
784 	tmout.tv_sec = TCP_TIMEOUT;
785 	tmout.tv_usec = 0;
786 	switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
787 	case 0:
788 	    break;
789 	case -1:
790 	    if (errno != EINTR)
791 		krb5_warn(context, errno, "select");
792 	    break;
793 	default:
794 	    for(i = 0; i < ndescr; i++)
795 		if(d[i].s >= 0 && FD_ISSET(d[i].s, &fds)) {
796 		    if(d[i].type == SOCK_DGRAM)
797 			handle_udp(&d[i]);
798 		    else if(d[i].type == SOCK_STREAM)
799 			handle_tcp(d, i, min_free);
800 		}
801 	}
802     }
803     free (d);
804 }
805