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