xref: /freebsd/crypto/heimdal/lib/krb5/krbhst.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * Copyright (c) 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 "krb5_locl.h"
35 #include <resolve.h>
36 
37 RCSID("$Id: krbhst.c,v 1.43.2.1 2003/04/22 15:00:38 lha Exp $");
38 
39 static int
40 string_to_proto(const char *string)
41 {
42     if(strcasecmp(string, "udp") == 0)
43 	return KRB5_KRBHST_UDP;
44     else if(strcasecmp(string, "tcp") == 0)
45 	return KRB5_KRBHST_TCP;
46     else if(strcasecmp(string, "http") == 0)
47 	return KRB5_KRBHST_HTTP;
48     return -1;
49 }
50 
51 /*
52  * set `res' and `count' to the result of looking up SRV RR in DNS for
53  * `proto', `proto', `realm' using `dns_type'.
54  * if `port' != 0, force that port number
55  */
56 
57 static krb5_error_code
58 srv_find_realm(krb5_context context, krb5_krbhst_info ***res, int *count,
59 	       const char *realm, const char *dns_type,
60 	       const char *proto, const char *service, int port)
61 {
62     char domain[1024];
63     struct dns_reply *r;
64     struct resource_record *rr;
65     int num_srv;
66     int proto_num;
67     int def_port;
68 
69     proto_num = string_to_proto(proto);
70     if(proto_num < 0) {
71 	krb5_set_error_string(context, "unknown protocol `%s'", proto);
72 	return EINVAL;
73     }
74 
75     if(proto_num == KRB5_KRBHST_HTTP)
76 	def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
77     else if(port == 0)
78 	def_port = ntohs(krb5_getportbyname (context, service, proto, 88));
79     else
80 	def_port = port;
81 
82     snprintf(domain, sizeof(domain), "_%s._%s.%s.", service, proto, realm);
83 
84     r = dns_lookup(domain, dns_type);
85     if(r == NULL) {
86 	*res = NULL;
87 	*count = 0;
88 	return KRB5_KDC_UNREACH;
89     }
90 
91     for(num_srv = 0, rr = r->head; rr; rr = rr->next)
92 	if(rr->type == T_SRV)
93 	    num_srv++;
94 
95     *res = malloc(num_srv * sizeof(**res));
96     if(*res == NULL) {
97 	dns_free_data(r);
98 	krb5_set_error_string(context, "malloc: out of memory");
99 	return ENOMEM;
100     }
101 
102     dns_srv_order(r);
103 
104     for(num_srv = 0, rr = r->head; rr; rr = rr->next)
105 	if(rr->type == T_SRV) {
106 	    krb5_krbhst_info *hi;
107 	    size_t len = strlen(rr->u.srv->target);
108 
109 	    hi = calloc(1, sizeof(*hi) + len);
110 	    if(hi == NULL) {
111 		dns_free_data(r);
112 		while(--num_srv >= 0)
113 		    free((*res)[num_srv]);
114 		free(*res);
115 		return ENOMEM;
116 	    }
117 	    (*res)[num_srv++] = hi;
118 
119 	    hi->proto = proto_num;
120 
121 	    hi->def_port = def_port;
122 	    if (port != 0)
123 		hi->port = port;
124 	    else
125 		hi->port = rr->u.srv->port;
126 
127 	    strlcpy(hi->hostname, rr->u.srv->target, len + 1);
128 	}
129 
130     *count = num_srv;
131 
132     dns_free_data(r);
133     return 0;
134 }
135 
136 
137 struct krb5_krbhst_data {
138     char *realm;
139     unsigned int flags;
140     int def_port;
141     int port;			/* hardwired port number if != 0 */
142 #define KD_CONFIG		1
143 #define KD_SRV_UDP		2
144 #define KD_SRV_TCP		4
145 #define KD_SRV_HTTP		8
146 #define KD_FALLBACK	       16
147 #define KD_CONFIG_EXISTS       32
148 
149     krb5_error_code (*get_next)(krb5_context, struct krb5_krbhst_data *,
150 				krb5_krbhst_info**);
151 
152     unsigned int fallback_count;
153 
154     struct krb5_krbhst_info *hosts, **index, **end;
155 };
156 
157 static krb5_boolean
158 krbhst_empty(const struct krb5_krbhst_data *kd)
159 {
160     return kd->index == &kd->hosts;
161 }
162 
163 /*
164  * parse `spec' into a krb5_krbhst_info, defaulting the port to `def_port'
165  * and forcing it to `port' if port != 0
166  */
167 
168 static struct krb5_krbhst_info*
169 parse_hostspec(krb5_context context, const char *spec, int def_port, int port)
170 {
171     const char *p = spec;
172     struct krb5_krbhst_info *hi;
173 
174     hi = calloc(1, sizeof(*hi) + strlen(spec));
175     if(hi == NULL)
176 	return NULL;
177 
178     hi->proto = KRB5_KRBHST_UDP;
179 
180     if(strncmp(p, "http://", 7) == 0){
181 	hi->proto = KRB5_KRBHST_HTTP;
182 	p += 7;
183     } else if(strncmp(p, "http/", 5) == 0) {
184 	hi->proto = KRB5_KRBHST_HTTP;
185 	p += 5;
186 	def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
187     }else if(strncmp(p, "tcp/", 4) == 0){
188 	hi->proto = KRB5_KRBHST_TCP;
189 	p += 4;
190     } else if(strncmp(p, "udp/", 4) == 0) {
191 	p += 4;
192     }
193 
194     if(strsep_copy(&p, ":", hi->hostname, strlen(spec) + 1) < 0) {
195 	free(hi);
196 	return NULL;
197     }
198     /* get rid of trailing /, and convert to lower case */
199     hi->hostname[strcspn(hi->hostname, "/")] = '\0';
200     strlwr(hi->hostname);
201 
202     hi->port = hi->def_port = def_port;
203     if(p != NULL) {
204 	char *end;
205 	hi->port = strtol(p, &end, 0);
206 	if(end == p) {
207 	    free(hi);
208 	    return NULL;
209 	}
210     }
211     if (port)
212 	hi->port = port;
213     return hi;
214 }
215 
216 static void
217 free_krbhst_info(krb5_krbhst_info *hi)
218 {
219     if (hi->ai != NULL)
220 	freeaddrinfo(hi->ai);
221     free(hi);
222 }
223 
224 static void
225 append_host_hostinfo(struct krb5_krbhst_data *kd, struct krb5_krbhst_info *host)
226 {
227     struct krb5_krbhst_info *h;
228 
229     for(h = kd->hosts; h; h = h->next)
230 	if(h->proto == host->proto &&
231 	   h->port == host->port &&
232 	   strcmp(h->hostname, host->hostname) == 0) {
233 	    free_krbhst_info(host);
234 	    return;
235 	}
236     *kd->end = host;
237     kd->end = &host->next;
238 }
239 
240 static krb5_error_code
241 append_host_string(krb5_context context, struct krb5_krbhst_data *kd,
242 		   const char *host, int def_port, int port)
243 {
244     struct krb5_krbhst_info *hi;
245 
246     hi = parse_hostspec(context, host, def_port, port);
247     if(hi == NULL)
248 	return ENOMEM;
249 
250     append_host_hostinfo(kd, hi);
251     return 0;
252 }
253 
254 /*
255  * return a readable representation of `host' in `hostname, hostlen'
256  */
257 
258 krb5_error_code
259 krb5_krbhst_format_string(krb5_context context, const krb5_krbhst_info *host,
260 			  char *hostname, size_t hostlen)
261 {
262     const char *proto = "";
263     char portstr[7] = "";
264     if(host->proto == KRB5_KRBHST_TCP)
265 	proto = "tcp/";
266     else if(host->proto == KRB5_KRBHST_HTTP)
267 	proto = "http://";
268     if(host->port != host->def_port)
269 	snprintf(portstr, sizeof(portstr), ":%d", host->port);
270     snprintf(hostname, hostlen, "%s%s%s", proto, host->hostname, portstr);
271     return 0;
272 }
273 
274 /*
275  * create a getaddrinfo `hints' based on `proto'
276  */
277 
278 static void
279 make_hints(struct addrinfo *hints, int proto)
280 {
281     memset(hints, 0, sizeof(*hints));
282     hints->ai_family = AF_UNSPEC;
283     switch(proto) {
284     case KRB5_KRBHST_UDP :
285 	hints->ai_socktype = SOCK_DGRAM;
286 	break;
287     case KRB5_KRBHST_HTTP :
288     case KRB5_KRBHST_TCP :
289 	hints->ai_socktype = SOCK_STREAM;
290 	break;
291     }
292 }
293 
294 /*
295  * return an `struct addrinfo *' in `ai' corresponding to the information
296  * in `host'.  free:ing is handled by krb5_krbhst_free.
297  */
298 
299 krb5_error_code
300 krb5_krbhst_get_addrinfo(krb5_context context, krb5_krbhst_info *host,
301 			 struct addrinfo **ai)
302 {
303     struct addrinfo hints;
304     char portstr[NI_MAXSERV];
305     int ret;
306 
307     if (host->ai == NULL) {
308 	make_hints(&hints, host->proto);
309 	snprintf (portstr, sizeof(portstr), "%d", host->port);
310 	ret = getaddrinfo(host->hostname, portstr, &hints, &host->ai);
311 	if (ret)
312 	    return krb5_eai_to_heim_errno(ret, errno);
313     }
314     *ai = host->ai;
315     return 0;
316 }
317 
318 static krb5_boolean
319 get_next(struct krb5_krbhst_data *kd, krb5_krbhst_info **host)
320 {
321     struct krb5_krbhst_info *hi = *kd->index;
322     if(hi != NULL) {
323 	*host = hi;
324 	kd->index = &(*kd->index)->next;
325 	return TRUE;
326     }
327     return FALSE;
328 }
329 
330 static void
331 srv_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
332 	const char *proto, const char *service)
333 {
334     krb5_krbhst_info **res;
335     int count, i;
336 
337     srv_find_realm(context, &res, &count, kd->realm, "SRV", proto, service,
338 		   kd->port);
339     for(i = 0; i < count; i++)
340 	append_host_hostinfo(kd, res[i]);
341     free(res);
342 }
343 
344 /*
345  * read the configuration for `conf_string', defaulting to kd->def_port and
346  * forcing it to `kd->port' if kd->port != 0
347  */
348 
349 static void
350 config_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
351 		 const char *conf_string)
352 {
353     int i;
354 
355     char **hostlist;
356     hostlist = krb5_config_get_strings(context, NULL,
357 				       "realms", kd->realm, conf_string, NULL);
358 
359     if(hostlist == NULL)
360 	return;
361     kd->flags |= KD_CONFIG_EXISTS;
362     for(i = 0; hostlist && hostlist[i] != NULL; i++)
363 	append_host_string(context, kd, hostlist[i], kd->def_port, kd->port);
364 
365     krb5_config_free_strings(hostlist);
366 }
367 
368 /*
369  * as a fallback, look for `serv_string.kd->realm' (typically
370  * kerberos.REALM, kerberos-1.REALM, ...
371  * `port' is the default port for the service, and `proto' the
372  * protocol
373  */
374 
375 static krb5_error_code
376 fallback_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
377 		   const char *serv_string, int port, int proto)
378 {
379     char *host;
380     int ret;
381     struct addrinfo *ai;
382     struct addrinfo hints;
383     char portstr[NI_MAXSERV];
384 
385     if(kd->fallback_count == 0)
386 	asprintf(&host, "%s.%s.", serv_string, kd->realm);
387     else
388 	asprintf(&host, "%s-%d.%s.",
389 		 serv_string, kd->fallback_count, kd->realm);
390 
391     if (host == NULL)
392 	return ENOMEM;
393 
394     make_hints(&hints, proto);
395     snprintf(portstr, sizeof(portstr), "%d", port);
396     ret = getaddrinfo(host, portstr, &hints, &ai);
397     if (ret) {
398 	/* no more hosts, so we're done here */
399 	free(host);
400 	kd->flags |= KD_FALLBACK;
401     } else {
402 	struct krb5_krbhst_info *hi;
403 	size_t hostlen = strlen(host);
404 
405 	hi = calloc(1, sizeof(*hi) + hostlen);
406 	if(hi == NULL) {
407 	    free(host);
408 	    return ENOMEM;
409 	}
410 
411 	hi->proto = proto;
412 	hi->port  = hi->def_port = port;
413 	hi->ai    = ai;
414 	memmove(hi->hostname, host, hostlen - 1);
415 	hi->hostname[hostlen - 1] = '\0';
416 	free(host);
417 	append_host_hostinfo(kd, hi);
418 	kd->fallback_count++;
419     }
420     return 0;
421 }
422 
423 static krb5_error_code
424 kdc_get_next(krb5_context context,
425 	     struct krb5_krbhst_data *kd,
426 	     krb5_krbhst_info **host)
427 {
428     krb5_error_code ret;
429 
430     if((kd->flags & KD_CONFIG) == 0) {
431 	config_get_hosts(context, kd, "kdc");
432 	kd->flags |= KD_CONFIG;
433 	if(get_next(kd, host))
434 	    return 0;
435     }
436 
437     if (kd->flags & KD_CONFIG_EXISTS)
438 	return KRB5_KDC_UNREACH; /* XXX */
439 
440     if(context->srv_lookup) {
441 	if((kd->flags & KD_SRV_UDP) == 0) {
442 	    srv_get_hosts(context, kd, "udp", "kerberos");
443 	    kd->flags |= KD_SRV_UDP;
444 	    if(get_next(kd, host))
445 		return 0;
446 	}
447 
448 	if((kd->flags & KD_SRV_TCP) == 0) {
449 	    srv_get_hosts(context, kd, "tcp", "kerberos");
450 	    kd->flags |= KD_SRV_TCP;
451 	    if(get_next(kd, host))
452 		return 0;
453 	}
454 	if((kd->flags & KD_SRV_HTTP) == 0) {
455 	    srv_get_hosts(context, kd, "http", "kerberos");
456 	    kd->flags |= KD_SRV_HTTP;
457 	    if(get_next(kd, host))
458 		return 0;
459 	}
460     }
461 
462     while((kd->flags & KD_FALLBACK) == 0) {
463 	ret = fallback_get_hosts(context, kd, "kerberos",
464 				 kd->def_port, KRB5_KRBHST_UDP);
465 	if(ret)
466 	    return ret;
467 	if(get_next(kd, host))
468 	    return 0;
469     }
470 
471     return KRB5_KDC_UNREACH; /* XXX */
472 }
473 
474 static krb5_error_code
475 admin_get_next(krb5_context context,
476 	       struct krb5_krbhst_data *kd,
477 	       krb5_krbhst_info **host)
478 {
479     krb5_error_code ret;
480 
481     if((kd->flags & KD_CONFIG) == 0) {
482 	config_get_hosts(context, kd, "admin_server");
483 	kd->flags |= KD_CONFIG;
484 	if(get_next(kd, host))
485 	    return 0;
486     }
487 
488     if (kd->flags & KD_CONFIG_EXISTS)
489 	return KRB5_KDC_UNREACH; /* XXX */
490 
491     if(context->srv_lookup) {
492 	if((kd->flags & KD_SRV_TCP) == 0) {
493 	    srv_get_hosts(context, kd, "tcp", "kerberos-adm");
494 	    kd->flags |= KD_SRV_TCP;
495 	    if(get_next(kd, host))
496 		return 0;
497 	}
498     }
499 
500     if (krbhst_empty(kd)
501 	&& (kd->flags & KD_FALLBACK) == 0) {
502 	ret = fallback_get_hosts(context, kd, "kerberos",
503 				 kd->def_port, KRB5_KRBHST_UDP);
504 	if(ret)
505 	    return ret;
506 	kd->flags |= KD_FALLBACK;
507 	if(get_next(kd, host))
508 	    return 0;
509     }
510 
511     return KRB5_KDC_UNREACH;	/* XXX */
512 }
513 
514 static krb5_error_code
515 kpasswd_get_next(krb5_context context,
516 		 struct krb5_krbhst_data *kd,
517 		 krb5_krbhst_info **host)
518 {
519     krb5_error_code ret;
520 
521     if((kd->flags & KD_CONFIG) == 0) {
522 	config_get_hosts(context, kd, "kpasswd_server");
523 	if(get_next(kd, host))
524 	    return 0;
525     }
526 
527     if (kd->flags & KD_CONFIG_EXISTS)
528 	return KRB5_KDC_UNREACH; /* XXX */
529 
530     if(context->srv_lookup) {
531 	if((kd->flags & KD_SRV_UDP) == 0) {
532 	    srv_get_hosts(context, kd, "udp", "kpasswd");
533 	    kd->flags |= KD_SRV_UDP;
534 	    if(get_next(kd, host))
535 		return 0;
536 	}
537     }
538 
539     /* no matches -> try admin */
540 
541     if (krbhst_empty(kd)) {
542 	kd->flags = 0;
543 	kd->port  = kd->def_port;
544 	kd->get_next = admin_get_next;
545 	ret = (*kd->get_next)(context, kd, host);
546 	if (ret == 0)
547 	    (*host)->proto = KRB5_KRBHST_UDP;
548 	return ret;
549     }
550 
551     return KRB5_KDC_UNREACH; /* XXX */
552 }
553 
554 static krb5_error_code
555 krb524_get_next(krb5_context context,
556 		struct krb5_krbhst_data *kd,
557 		krb5_krbhst_info **host)
558 {
559     if((kd->flags & KD_CONFIG) == 0) {
560 	config_get_hosts(context, kd, "krb524_server");
561 	if(get_next(kd, host))
562 	    return 0;
563 	kd->flags |= KD_CONFIG;
564     }
565 
566     if (kd->flags & KD_CONFIG_EXISTS)
567 	return KRB5_KDC_UNREACH; /* XXX */
568 
569     if(context->srv_lookup) {
570 	if((kd->flags & KD_SRV_UDP) == 0) {
571 	    srv_get_hosts(context, kd, "udp", "krb524");
572 	    kd->flags |= KD_SRV_UDP;
573 	    if(get_next(kd, host))
574 		return 0;
575 	}
576 
577 	if((kd->flags & KD_SRV_TCP) == 0) {
578 	    srv_get_hosts(context, kd, "tcp", "krb524");
579 	    kd->flags |= KD_SRV_TCP;
580 	    if(get_next(kd, host))
581 		return 0;
582 	}
583     }
584 
585     /* no matches -> try kdc */
586 
587     if (krbhst_empty(kd)) {
588 	kd->flags = 0;
589 	kd->port  = kd->def_port;
590 	kd->get_next = kdc_get_next;
591 	return (*kd->get_next)(context, kd, host);
592     }
593 
594     return KRB5_KDC_UNREACH; /* XXX */
595 }
596 
597 static struct krb5_krbhst_data*
598 common_init(krb5_context context,
599 	    const char *realm)
600 {
601     struct krb5_krbhst_data *kd;
602 
603     if((kd = calloc(1, sizeof(*kd))) == NULL)
604 	return NULL;
605 
606     if((kd->realm = strdup(realm)) == NULL) {
607 	free(kd);
608 	return NULL;
609     }
610 
611     kd->end = kd->index = &kd->hosts;
612     return kd;
613 }
614 
615 /*
616  * initialize `handle' to look for hosts of type `type' in realm `realm'
617  */
618 
619 krb5_error_code
620 krb5_krbhst_init(krb5_context context,
621 		 const char *realm,
622 		 unsigned int type,
623 		 krb5_krbhst_handle *handle)
624 {
625     struct krb5_krbhst_data *kd;
626     krb5_error_code (*get_next)(krb5_context, struct krb5_krbhst_data *,
627 				krb5_krbhst_info **);
628     int def_port;
629 
630     switch(type) {
631     case KRB5_KRBHST_KDC:
632 	get_next = kdc_get_next;
633 	def_port = ntohs(krb5_getportbyname (context, "kerberos", "udp", 88));
634 	break;
635     case KRB5_KRBHST_ADMIN:
636 	get_next = admin_get_next;
637 	def_port = ntohs(krb5_getportbyname (context, "kerberos-adm",
638 					     "tcp", 749));
639 	break;
640     case KRB5_KRBHST_CHANGEPW:
641 	get_next = kpasswd_get_next;
642 	def_port = ntohs(krb5_getportbyname (context, "kpasswd", "udp",
643 					     KPASSWD_PORT));
644 	break;
645     case KRB5_KRBHST_KRB524:
646 	get_next = krb524_get_next;
647 	def_port = ntohs(krb5_getportbyname (context, "krb524", "udp", 4444));
648 	break;
649     default:
650 	krb5_set_error_string(context, "unknown krbhst type (%u)", type);
651 	return ENOTTY;
652     }
653     if((kd = common_init(context, realm)) == NULL)
654 	return ENOMEM;
655     kd->get_next = get_next;
656     kd->def_port = def_port;
657     *handle = kd;
658     return 0;
659 }
660 
661 /*
662  * return the next host information from `handle' in `host'
663  */
664 
665 krb5_error_code
666 krb5_krbhst_next(krb5_context context,
667 		 krb5_krbhst_handle handle,
668 		 krb5_krbhst_info **host)
669 {
670     if(get_next(handle, host))
671 	return 0;
672 
673     return (*handle->get_next)(context, handle, host);
674 }
675 
676 /*
677  * return the next host information from `handle' as a host name
678  * in `hostname' (or length `hostlen)
679  */
680 
681 krb5_error_code
682 krb5_krbhst_next_as_string(krb5_context context,
683 			   krb5_krbhst_handle handle,
684 			   char *hostname,
685 			   size_t hostlen)
686 {
687     krb5_error_code ret;
688     krb5_krbhst_info *host;
689     ret = krb5_krbhst_next(context, handle, &host);
690     if(ret)
691 	return ret;
692     return krb5_krbhst_format_string(context, host, hostname, hostlen);
693 }
694 
695 
696 void
697 krb5_krbhst_reset(krb5_context context, krb5_krbhst_handle handle)
698 {
699     handle->index = &handle->hosts;
700 }
701 
702 void
703 krb5_krbhst_free(krb5_context context, krb5_krbhst_handle handle)
704 {
705     krb5_krbhst_info *h, *next;
706 
707     if (handle == NULL)
708 	return;
709 
710     for (h = handle->hosts; h != NULL; h = next) {
711 	next = h->next;
712 	free_krbhst_info(h);
713     }
714 
715     free(handle->realm);
716     free(handle);
717 }
718 
719 /* backwards compatibility ahead */
720 
721 static krb5_error_code
722 gethostlist(krb5_context context, const char *realm,
723 	    unsigned int type, char ***hostlist)
724 {
725     krb5_error_code ret;
726     int nhost = 0;
727     krb5_krbhst_handle handle;
728     char host[MAXHOSTNAMELEN];
729     krb5_krbhst_info *hostinfo;
730 
731     ret = krb5_krbhst_init(context, realm, type, &handle);
732     if (ret)
733 	return ret;
734 
735     while(krb5_krbhst_next(context, handle, &hostinfo) == 0)
736 	nhost++;
737     if(nhost == 0)
738 	return KRB5_KDC_UNREACH;
739     *hostlist = calloc(nhost + 1, sizeof(**hostlist));
740     if(*hostlist == NULL) {
741 	krb5_krbhst_free(context, handle);
742 	return ENOMEM;
743     }
744 
745     krb5_krbhst_reset(context, handle);
746     nhost = 0;
747     while(krb5_krbhst_next_as_string(context, handle,
748 				     host, sizeof(host)) == 0) {
749 	if(((*hostlist)[nhost++] = strdup(host)) == NULL) {
750 	    krb5_free_krbhst(context, *hostlist);
751 	    krb5_krbhst_free(context, handle);
752 	    return ENOMEM;
753 	}
754     }
755     (*hostlist)[nhost++] = NULL;
756     krb5_krbhst_free(context, handle);
757     return 0;
758 }
759 
760 /*
761  * return an malloced list of kadmin-hosts for `realm' in `hostlist'
762  */
763 
764 krb5_error_code
765 krb5_get_krb_admin_hst (krb5_context context,
766 			const krb5_realm *realm,
767 			char ***hostlist)
768 {
769     return gethostlist(context, *realm, KRB5_KRBHST_ADMIN, hostlist);
770 }
771 
772 /*
773  * return an malloced list of changepw-hosts for `realm' in `hostlist'
774  */
775 
776 krb5_error_code
777 krb5_get_krb_changepw_hst (krb5_context context,
778 			   const krb5_realm *realm,
779 			   char ***hostlist)
780 {
781     return gethostlist(context, *realm, KRB5_KRBHST_CHANGEPW, hostlist);
782 }
783 
784 /*
785  * return an malloced list of 524-hosts for `realm' in `hostlist'
786  */
787 
788 krb5_error_code
789 krb5_get_krb524hst (krb5_context context,
790 		    const krb5_realm *realm,
791 		    char ***hostlist)
792 {
793     return gethostlist(context, *realm, KRB5_KRBHST_KRB524, hostlist);
794 }
795 
796 
797 /*
798  * return an malloced list of KDC's for `realm' in `hostlist'
799  */
800 
801 krb5_error_code
802 krb5_get_krbhst (krb5_context context,
803 		 const krb5_realm *realm,
804 		 char ***hostlist)
805 {
806     return gethostlist(context, *realm, KRB5_KRBHST_KDC, hostlist);
807 }
808 
809 /*
810  * free all the memory allocated in `hostlist'
811  */
812 
813 krb5_error_code
814 krb5_free_krbhst (krb5_context context,
815 		  char **hostlist)
816 {
817     char **p;
818 
819     for (p = hostlist; *p; ++p)
820 	free (*p);
821     free (hostlist);
822     return 0;
823 }
824