xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/krbhst.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: krbhst.c,v 1.1.1.2 2014/04/24 12:45:50 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2001 - 2003 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "krb5_locl.h"
37ebfedea0SLionel Sambuc #include <krb5/resolve.h>
38ebfedea0SLionel Sambuc #include "locate_plugin.h"
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc static int
string_to_proto(const char * string)41ebfedea0SLionel Sambuc string_to_proto(const char *string)
42ebfedea0SLionel Sambuc {
43ebfedea0SLionel Sambuc     if(strcasecmp(string, "udp") == 0)
44ebfedea0SLionel Sambuc 	return KRB5_KRBHST_UDP;
45ebfedea0SLionel Sambuc     else if(strcasecmp(string, "tcp") == 0)
46ebfedea0SLionel Sambuc 	return KRB5_KRBHST_TCP;
47ebfedea0SLionel Sambuc     else if(strcasecmp(string, "http") == 0)
48ebfedea0SLionel Sambuc 	return KRB5_KRBHST_HTTP;
49ebfedea0SLionel Sambuc     return -1;
50ebfedea0SLionel Sambuc }
51ebfedea0SLionel Sambuc 
52ebfedea0SLionel Sambuc /*
53ebfedea0SLionel Sambuc  * set `res' and `count' to the result of looking up SRV RR in DNS for
54ebfedea0SLionel Sambuc  * `proto', `proto', `realm' using `dns_type'.
55ebfedea0SLionel Sambuc  * if `port' != 0, force that port number
56ebfedea0SLionel Sambuc  */
57ebfedea0SLionel Sambuc 
58ebfedea0SLionel Sambuc static krb5_error_code
srv_find_realm(krb5_context context,krb5_krbhst_info *** res,int * count,const char * realm,const char * dns_type,const char * proto,const char * service,int port)59ebfedea0SLionel Sambuc srv_find_realm(krb5_context context, krb5_krbhst_info ***res, int *count,
60ebfedea0SLionel Sambuc 	       const char *realm, const char *dns_type,
61ebfedea0SLionel Sambuc 	       const char *proto, const char *service, int port)
62ebfedea0SLionel Sambuc {
63ebfedea0SLionel Sambuc     char domain[1024];
64ebfedea0SLionel Sambuc     struct rk_dns_reply *r;
65ebfedea0SLionel Sambuc     struct rk_resource_record *rr;
66ebfedea0SLionel Sambuc     int num_srv;
67ebfedea0SLionel Sambuc     int proto_num;
68ebfedea0SLionel Sambuc     int def_port;
69ebfedea0SLionel Sambuc 
70ebfedea0SLionel Sambuc     *res = NULL;
71ebfedea0SLionel Sambuc     *count = 0;
72ebfedea0SLionel Sambuc 
73ebfedea0SLionel Sambuc     proto_num = string_to_proto(proto);
74ebfedea0SLionel Sambuc     if(proto_num < 0) {
75ebfedea0SLionel Sambuc 	krb5_set_error_message(context, EINVAL,
76ebfedea0SLionel Sambuc 			       N_("unknown protocol `%s' to lookup", ""),
77ebfedea0SLionel Sambuc 			       proto);
78ebfedea0SLionel Sambuc 	return EINVAL;
79ebfedea0SLionel Sambuc     }
80ebfedea0SLionel Sambuc 
81ebfedea0SLionel Sambuc     if(proto_num == KRB5_KRBHST_HTTP)
82ebfedea0SLionel Sambuc 	def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
83ebfedea0SLionel Sambuc     else if(port == 0)
84ebfedea0SLionel Sambuc 	def_port = ntohs(krb5_getportbyname (context, service, proto, 88));
85ebfedea0SLionel Sambuc     else
86ebfedea0SLionel Sambuc 	def_port = port;
87ebfedea0SLionel Sambuc 
88ebfedea0SLionel Sambuc     snprintf(domain, sizeof(domain), "_%s._%s.%s.", service, proto, realm);
89ebfedea0SLionel Sambuc 
90ebfedea0SLionel Sambuc     r = rk_dns_lookup(domain, dns_type);
91ebfedea0SLionel Sambuc     if(r == NULL) {
92ebfedea0SLionel Sambuc 	_krb5_debug(context, 0,
93ebfedea0SLionel Sambuc 		    "DNS lookup failed domain: %s", domain);
94ebfedea0SLionel Sambuc 	return KRB5_KDC_UNREACH;
95ebfedea0SLionel Sambuc     }
96ebfedea0SLionel Sambuc 
97ebfedea0SLionel Sambuc     for(num_srv = 0, rr = r->head; rr; rr = rr->next)
98ebfedea0SLionel Sambuc 	if(rr->type == rk_ns_t_srv)
99ebfedea0SLionel Sambuc 	    num_srv++;
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc     *res = malloc(num_srv * sizeof(**res));
102ebfedea0SLionel Sambuc     if(*res == NULL) {
103ebfedea0SLionel Sambuc 	rk_dns_free_data(r);
104ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM,
105ebfedea0SLionel Sambuc 			       N_("malloc: out of memory", ""));
106ebfedea0SLionel Sambuc 	return ENOMEM;
107ebfedea0SLionel Sambuc     }
108ebfedea0SLionel Sambuc 
109ebfedea0SLionel Sambuc     rk_dns_srv_order(r);
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc     for(num_srv = 0, rr = r->head; rr; rr = rr->next)
112ebfedea0SLionel Sambuc 	if(rr->type == rk_ns_t_srv) {
113ebfedea0SLionel Sambuc 	    krb5_krbhst_info *hi;
114ebfedea0SLionel Sambuc 	    size_t len = strlen(rr->u.srv->target);
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc 	    hi = calloc(1, sizeof(*hi) + len);
117ebfedea0SLionel Sambuc 	    if(hi == NULL) {
118ebfedea0SLionel Sambuc 		rk_dns_free_data(r);
119ebfedea0SLionel Sambuc 		while(--num_srv >= 0)
120ebfedea0SLionel Sambuc 		    free((*res)[num_srv]);
121ebfedea0SLionel Sambuc 		free(*res);
122ebfedea0SLionel Sambuc 		*res = NULL;
123ebfedea0SLionel Sambuc 		return ENOMEM;
124ebfedea0SLionel Sambuc 	    }
125ebfedea0SLionel Sambuc 	    (*res)[num_srv++] = hi;
126ebfedea0SLionel Sambuc 
127ebfedea0SLionel Sambuc 	    hi->proto = proto_num;
128ebfedea0SLionel Sambuc 
129ebfedea0SLionel Sambuc 	    hi->def_port = def_port;
130ebfedea0SLionel Sambuc 	    if (port != 0)
131ebfedea0SLionel Sambuc 		hi->port = port;
132ebfedea0SLionel Sambuc 	    else
133ebfedea0SLionel Sambuc 		hi->port = rr->u.srv->port;
134ebfedea0SLionel Sambuc 
135ebfedea0SLionel Sambuc 	    strlcpy(hi->hostname, rr->u.srv->target, len + 1);
136ebfedea0SLionel Sambuc 	}
137ebfedea0SLionel Sambuc 
138ebfedea0SLionel Sambuc     *count = num_srv;
139ebfedea0SLionel Sambuc 
140ebfedea0SLionel Sambuc     rk_dns_free_data(r);
141ebfedea0SLionel Sambuc     return 0;
142ebfedea0SLionel Sambuc }
143ebfedea0SLionel Sambuc 
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc struct krb5_krbhst_data {
146ebfedea0SLionel Sambuc     char *realm;
147ebfedea0SLionel Sambuc     unsigned int flags;
148ebfedea0SLionel Sambuc     int def_port;
149ebfedea0SLionel Sambuc     int port;			/* hardwired port number if != 0 */
150ebfedea0SLionel Sambuc #define KD_CONFIG		 1
151ebfedea0SLionel Sambuc #define KD_SRV_UDP		 2
152ebfedea0SLionel Sambuc #define KD_SRV_TCP		 4
153ebfedea0SLionel Sambuc #define KD_SRV_HTTP		 8
154ebfedea0SLionel Sambuc #define KD_FALLBACK		16
155ebfedea0SLionel Sambuc #define KD_CONFIG_EXISTS	32
156ebfedea0SLionel Sambuc #define KD_LARGE_MSG		64
157ebfedea0SLionel Sambuc #define KD_PLUGIN	       128
158ebfedea0SLionel Sambuc     krb5_error_code (*get_next)(krb5_context, struct krb5_krbhst_data *,
159ebfedea0SLionel Sambuc 				krb5_krbhst_info**);
160ebfedea0SLionel Sambuc 
161ebfedea0SLionel Sambuc     unsigned int fallback_count;
162ebfedea0SLionel Sambuc 
163ebfedea0SLionel Sambuc     struct krb5_krbhst_info *hosts, **index, **end;
164ebfedea0SLionel Sambuc };
165ebfedea0SLionel Sambuc 
166ebfedea0SLionel Sambuc static krb5_boolean
krbhst_empty(const struct krb5_krbhst_data * kd)167ebfedea0SLionel Sambuc krbhst_empty(const struct krb5_krbhst_data *kd)
168ebfedea0SLionel Sambuc {
169ebfedea0SLionel Sambuc     return kd->index == &kd->hosts;
170ebfedea0SLionel Sambuc }
171ebfedea0SLionel Sambuc 
172ebfedea0SLionel Sambuc /*
173ebfedea0SLionel Sambuc  * Return the default protocol for the `kd' (either TCP or UDP)
174ebfedea0SLionel Sambuc  */
175ebfedea0SLionel Sambuc 
176ebfedea0SLionel Sambuc static int
krbhst_get_default_proto(struct krb5_krbhst_data * kd)177ebfedea0SLionel Sambuc krbhst_get_default_proto(struct krb5_krbhst_data *kd)
178ebfedea0SLionel Sambuc {
179ebfedea0SLionel Sambuc     if (kd->flags & KD_LARGE_MSG)
180ebfedea0SLionel Sambuc 	return KRB5_KRBHST_TCP;
181ebfedea0SLionel Sambuc     return KRB5_KRBHST_UDP;
182ebfedea0SLionel Sambuc }
183ebfedea0SLionel Sambuc 
184ebfedea0SLionel Sambuc /*
185ebfedea0SLionel Sambuc  *
186ebfedea0SLionel Sambuc  */
187ebfedea0SLionel Sambuc 
188ebfedea0SLionel Sambuc const char *
_krb5_krbhst_get_realm(krb5_krbhst_handle handle)189ebfedea0SLionel Sambuc _krb5_krbhst_get_realm(krb5_krbhst_handle handle)
190ebfedea0SLionel Sambuc {
191ebfedea0SLionel Sambuc     return handle->realm;
192ebfedea0SLionel Sambuc }
193ebfedea0SLionel Sambuc 
194ebfedea0SLionel Sambuc /*
195ebfedea0SLionel Sambuc  * parse `spec' into a krb5_krbhst_info, defaulting the port to `def_port'
196ebfedea0SLionel Sambuc  * and forcing it to `port' if port != 0
197ebfedea0SLionel Sambuc  */
198ebfedea0SLionel Sambuc 
199ebfedea0SLionel Sambuc static struct krb5_krbhst_info*
parse_hostspec(krb5_context context,struct krb5_krbhst_data * kd,const char * spec,int def_port,int port)200ebfedea0SLionel Sambuc parse_hostspec(krb5_context context, struct krb5_krbhst_data *kd,
201ebfedea0SLionel Sambuc 	       const char *spec, int def_port, int port)
202ebfedea0SLionel Sambuc {
203ebfedea0SLionel Sambuc     const char *p = spec, *q;
204ebfedea0SLionel Sambuc     struct krb5_krbhst_info *hi;
205ebfedea0SLionel Sambuc 
206ebfedea0SLionel Sambuc     hi = calloc(1, sizeof(*hi) + strlen(spec));
207ebfedea0SLionel Sambuc     if(hi == NULL)
208ebfedea0SLionel Sambuc 	return NULL;
209ebfedea0SLionel Sambuc 
210ebfedea0SLionel Sambuc     hi->proto = krbhst_get_default_proto(kd);
211ebfedea0SLionel Sambuc 
212ebfedea0SLionel Sambuc     if(strncmp(p, "http://", 7) == 0){
213ebfedea0SLionel Sambuc 	hi->proto = KRB5_KRBHST_HTTP;
214ebfedea0SLionel Sambuc 	p += 7;
215ebfedea0SLionel Sambuc     } else if(strncmp(p, "http/", 5) == 0) {
216ebfedea0SLionel Sambuc 	hi->proto = KRB5_KRBHST_HTTP;
217ebfedea0SLionel Sambuc 	p += 5;
218ebfedea0SLionel Sambuc 	def_port = ntohs(krb5_getportbyname (context, "http", "tcp", 80));
219ebfedea0SLionel Sambuc     }else if(strncmp(p, "tcp/", 4) == 0){
220ebfedea0SLionel Sambuc 	hi->proto = KRB5_KRBHST_TCP;
221ebfedea0SLionel Sambuc 	p += 4;
222ebfedea0SLionel Sambuc     } else if(strncmp(p, "udp/", 4) == 0) {
223ebfedea0SLionel Sambuc 	p += 4;
224ebfedea0SLionel Sambuc     }
225ebfedea0SLionel Sambuc 
226ebfedea0SLionel Sambuc     if (p[0] == '[' && (q = strchr(p, ']')) != NULL) {
227ebfedea0SLionel Sambuc 	/* if address looks like [foo:bar] or [foo:bar]: its a ipv6
228ebfedea0SLionel Sambuc 	   adress, strip of [] */
229ebfedea0SLionel Sambuc 	memcpy(hi->hostname, &p[1], q - p - 1);
230ebfedea0SLionel Sambuc 	hi->hostname[q - p - 1] = '\0';
231ebfedea0SLionel Sambuc 	p = q + 1;
232ebfedea0SLionel Sambuc 	/* get trailing : */
233ebfedea0SLionel Sambuc 	if (p[0] == ':')
234ebfedea0SLionel Sambuc 	    p++;
235ebfedea0SLionel Sambuc     } else if(strsep_copy(&p, ":", hi->hostname, strlen(spec) + 1) < 0) {
236ebfedea0SLionel Sambuc 	/* copy everything before : */
237ebfedea0SLionel Sambuc 	free(hi);
238ebfedea0SLionel Sambuc 	return NULL;
239ebfedea0SLionel Sambuc     }
240ebfedea0SLionel Sambuc     /* get rid of trailing /, and convert to lower case */
241ebfedea0SLionel Sambuc     hi->hostname[strcspn(hi->hostname, "/")] = '\0';
242ebfedea0SLionel Sambuc     strlwr(hi->hostname);
243ebfedea0SLionel Sambuc 
244ebfedea0SLionel Sambuc     hi->port = hi->def_port = def_port;
245ebfedea0SLionel Sambuc     if(p != NULL && p[0]) {
246ebfedea0SLionel Sambuc 	char *end;
247ebfedea0SLionel Sambuc 	hi->port = strtol(p, &end, 0);
248ebfedea0SLionel Sambuc 	if(end == p) {
249ebfedea0SLionel Sambuc 	    free(hi);
250ebfedea0SLionel Sambuc 	    return NULL;
251ebfedea0SLionel Sambuc 	}
252ebfedea0SLionel Sambuc     }
253ebfedea0SLionel Sambuc     if (port)
254ebfedea0SLionel Sambuc 	hi->port = port;
255ebfedea0SLionel Sambuc     return hi;
256ebfedea0SLionel Sambuc }
257ebfedea0SLionel Sambuc 
258ebfedea0SLionel Sambuc void
_krb5_free_krbhst_info(krb5_krbhst_info * hi)259ebfedea0SLionel Sambuc _krb5_free_krbhst_info(krb5_krbhst_info *hi)
260ebfedea0SLionel Sambuc {
261ebfedea0SLionel Sambuc     if (hi->ai != NULL)
262ebfedea0SLionel Sambuc 	freeaddrinfo(hi->ai);
263ebfedea0SLionel Sambuc     free(hi);
264ebfedea0SLionel Sambuc }
265ebfedea0SLionel Sambuc 
266ebfedea0SLionel Sambuc krb5_error_code
_krb5_krbhost_info_move(krb5_context context,krb5_krbhst_info * from,krb5_krbhst_info ** to)267ebfedea0SLionel Sambuc _krb5_krbhost_info_move(krb5_context context,
268ebfedea0SLionel Sambuc 			krb5_krbhst_info *from,
269ebfedea0SLionel Sambuc 			krb5_krbhst_info **to)
270ebfedea0SLionel Sambuc {
271ebfedea0SLionel Sambuc     size_t hostnamelen = strlen(from->hostname);
272ebfedea0SLionel Sambuc     /* trailing NUL is included in structure */
273ebfedea0SLionel Sambuc     *to = calloc(1, sizeof(**to) + hostnamelen);
274ebfedea0SLionel Sambuc     if(*to == NULL) {
275ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM,
276ebfedea0SLionel Sambuc 			       N_("malloc: out of memory", ""));
277ebfedea0SLionel Sambuc 	return ENOMEM;
278ebfedea0SLionel Sambuc     }
279ebfedea0SLionel Sambuc 
280ebfedea0SLionel Sambuc     (*to)->proto = from->proto;
281ebfedea0SLionel Sambuc     (*to)->port = from->port;
282ebfedea0SLionel Sambuc     (*to)->def_port = from->def_port;
283ebfedea0SLionel Sambuc     (*to)->ai = from->ai;
284ebfedea0SLionel Sambuc     from->ai = NULL;
285ebfedea0SLionel Sambuc     (*to)->next = NULL;
286ebfedea0SLionel Sambuc     memcpy((*to)->hostname, from->hostname, hostnamelen + 1);
287ebfedea0SLionel Sambuc     return 0;
288ebfedea0SLionel Sambuc }
289ebfedea0SLionel Sambuc 
290ebfedea0SLionel Sambuc 
291ebfedea0SLionel Sambuc static void
append_host_hostinfo(struct krb5_krbhst_data * kd,struct krb5_krbhst_info * host)292ebfedea0SLionel Sambuc append_host_hostinfo(struct krb5_krbhst_data *kd, struct krb5_krbhst_info *host)
293ebfedea0SLionel Sambuc {
294ebfedea0SLionel Sambuc     struct krb5_krbhst_info *h;
295ebfedea0SLionel Sambuc 
296ebfedea0SLionel Sambuc     for(h = kd->hosts; h; h = h->next)
297ebfedea0SLionel Sambuc 	if(h->proto == host->proto &&
298ebfedea0SLionel Sambuc 	   h->port == host->port &&
299ebfedea0SLionel Sambuc 	   strcmp(h->hostname, host->hostname) == 0) {
300ebfedea0SLionel Sambuc 	    _krb5_free_krbhst_info(host);
301ebfedea0SLionel Sambuc 	    return;
302ebfedea0SLionel Sambuc 	}
303ebfedea0SLionel Sambuc     *kd->end = host;
304ebfedea0SLionel Sambuc     kd->end = &host->next;
305ebfedea0SLionel Sambuc }
306ebfedea0SLionel Sambuc 
307ebfedea0SLionel Sambuc static krb5_error_code
append_host_string(krb5_context context,struct krb5_krbhst_data * kd,const char * host,int def_port,int port)308ebfedea0SLionel Sambuc append_host_string(krb5_context context, struct krb5_krbhst_data *kd,
309ebfedea0SLionel Sambuc 		   const char *host, int def_port, int port)
310ebfedea0SLionel Sambuc {
311ebfedea0SLionel Sambuc     struct krb5_krbhst_info *hi;
312ebfedea0SLionel Sambuc 
313ebfedea0SLionel Sambuc     hi = parse_hostspec(context, kd, host, def_port, port);
314ebfedea0SLionel Sambuc     if(hi == NULL)
315ebfedea0SLionel Sambuc 	return ENOMEM;
316ebfedea0SLionel Sambuc 
317ebfedea0SLionel Sambuc     append_host_hostinfo(kd, hi);
318ebfedea0SLionel Sambuc     return 0;
319ebfedea0SLionel Sambuc }
320ebfedea0SLionel Sambuc 
321ebfedea0SLionel Sambuc /*
322ebfedea0SLionel Sambuc  * return a readable representation of `host' in `hostname, hostlen'
323ebfedea0SLionel Sambuc  */
324ebfedea0SLionel Sambuc 
325ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_krbhst_format_string(krb5_context context,const krb5_krbhst_info * host,char * hostname,size_t hostlen)326ebfedea0SLionel Sambuc krb5_krbhst_format_string(krb5_context context, const krb5_krbhst_info *host,
327ebfedea0SLionel Sambuc 			  char *hostname, size_t hostlen)
328ebfedea0SLionel Sambuc {
329ebfedea0SLionel Sambuc     const char *proto = "";
330ebfedea0SLionel Sambuc     char portstr[7] = "";
331ebfedea0SLionel Sambuc     if(host->proto == KRB5_KRBHST_TCP)
332ebfedea0SLionel Sambuc 	proto = "tcp/";
333ebfedea0SLionel Sambuc     else if(host->proto == KRB5_KRBHST_HTTP)
334ebfedea0SLionel Sambuc 	proto = "http://";
335ebfedea0SLionel Sambuc     if(host->port != host->def_port)
336ebfedea0SLionel Sambuc 	snprintf(portstr, sizeof(portstr), ":%d", host->port);
337ebfedea0SLionel Sambuc     snprintf(hostname, hostlen, "%s%s%s", proto, host->hostname, portstr);
338ebfedea0SLionel Sambuc     return 0;
339ebfedea0SLionel Sambuc }
340ebfedea0SLionel Sambuc 
341ebfedea0SLionel Sambuc /*
342ebfedea0SLionel Sambuc  * create a getaddrinfo `hints' based on `proto'
343ebfedea0SLionel Sambuc  */
344ebfedea0SLionel Sambuc 
345ebfedea0SLionel Sambuc static void
make_hints(struct addrinfo * hints,int proto)346ebfedea0SLionel Sambuc make_hints(struct addrinfo *hints, int proto)
347ebfedea0SLionel Sambuc {
348ebfedea0SLionel Sambuc     memset(hints, 0, sizeof(*hints));
349ebfedea0SLionel Sambuc     hints->ai_family = AF_UNSPEC;
350ebfedea0SLionel Sambuc     switch(proto) {
351ebfedea0SLionel Sambuc     case KRB5_KRBHST_UDP :
352ebfedea0SLionel Sambuc 	hints->ai_socktype = SOCK_DGRAM;
353ebfedea0SLionel Sambuc 	break;
354ebfedea0SLionel Sambuc     case KRB5_KRBHST_HTTP :
355ebfedea0SLionel Sambuc     case KRB5_KRBHST_TCP :
356ebfedea0SLionel Sambuc 	hints->ai_socktype = SOCK_STREAM;
357ebfedea0SLionel Sambuc 	break;
358ebfedea0SLionel Sambuc     }
359ebfedea0SLionel Sambuc }
360ebfedea0SLionel Sambuc 
361ebfedea0SLionel Sambuc /**
362ebfedea0SLionel Sambuc  * Return an `struct addrinfo *' for a KDC host.
363ebfedea0SLionel Sambuc  *
364ebfedea0SLionel Sambuc  * Returns an the struct addrinfo in in that corresponds to the
365ebfedea0SLionel Sambuc  * information in `host'.  free:ing is handled by krb5_krbhst_free, so
366ebfedea0SLionel Sambuc  * the returned ai must not be released.
367ebfedea0SLionel Sambuc  *
368ebfedea0SLionel Sambuc  * @ingroup krb5
369ebfedea0SLionel Sambuc  */
370ebfedea0SLionel Sambuc 
371ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_krbhst_get_addrinfo(krb5_context context,krb5_krbhst_info * host,struct addrinfo ** ai)372ebfedea0SLionel Sambuc krb5_krbhst_get_addrinfo(krb5_context context, krb5_krbhst_info *host,
373ebfedea0SLionel Sambuc 			 struct addrinfo **ai)
374ebfedea0SLionel Sambuc {
375ebfedea0SLionel Sambuc     int ret = 0;
376ebfedea0SLionel Sambuc 
377ebfedea0SLionel Sambuc     if (host->ai == NULL) {
378ebfedea0SLionel Sambuc 	struct addrinfo hints;
379ebfedea0SLionel Sambuc 	char portstr[NI_MAXSERV];
380ebfedea0SLionel Sambuc 	char *hostname = host->hostname;
381ebfedea0SLionel Sambuc 
382ebfedea0SLionel Sambuc 	snprintf (portstr, sizeof(portstr), "%d", host->port);
383ebfedea0SLionel Sambuc 	make_hints(&hints, host->proto);
384ebfedea0SLionel Sambuc 
385ebfedea0SLionel Sambuc 	/**
386ebfedea0SLionel Sambuc 	 * First try this as an IP address, this allows us to add a
387ebfedea0SLionel Sambuc 	 * dot at the end to stop using the search domains.
388ebfedea0SLionel Sambuc 	 */
389ebfedea0SLionel Sambuc 
390ebfedea0SLionel Sambuc 	hints.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV;
391ebfedea0SLionel Sambuc 
392ebfedea0SLionel Sambuc 	ret = getaddrinfo(host->hostname, portstr, &hints, &host->ai);
393ebfedea0SLionel Sambuc 	if (ret == 0)
394ebfedea0SLionel Sambuc 	    goto out;
395ebfedea0SLionel Sambuc 
396ebfedea0SLionel Sambuc 	/**
397ebfedea0SLionel Sambuc 	 * If the hostname contains a dot, assumes it's a FQDN and
398ebfedea0SLionel Sambuc 	 * don't use search domains since that might be painfully slow
399ebfedea0SLionel Sambuc 	 * when machine is disconnected from that network.
400ebfedea0SLionel Sambuc 	 */
401ebfedea0SLionel Sambuc 
402ebfedea0SLionel Sambuc 	hints.ai_flags &= ~(AI_NUMERICHOST);
403ebfedea0SLionel Sambuc 
404ebfedea0SLionel Sambuc 	if (strchr(hostname, '.') && hostname[strlen(hostname) - 1] != '.') {
405ebfedea0SLionel Sambuc 	    ret = asprintf(&hostname, "%s.", host->hostname);
406ebfedea0SLionel Sambuc 	    if (ret < 0 || hostname == NULL)
407ebfedea0SLionel Sambuc 		return ENOMEM;
408ebfedea0SLionel Sambuc 	}
409ebfedea0SLionel Sambuc 
410ebfedea0SLionel Sambuc 	ret = getaddrinfo(hostname, portstr, &hints, &host->ai);
411ebfedea0SLionel Sambuc 	if (hostname != host->hostname)
412ebfedea0SLionel Sambuc 	    free(hostname);
413ebfedea0SLionel Sambuc 	if (ret) {
414ebfedea0SLionel Sambuc 	    ret = krb5_eai_to_heim_errno(ret, errno);
415ebfedea0SLionel Sambuc 	    goto out;
416ebfedea0SLionel Sambuc 	}
417ebfedea0SLionel Sambuc     }
418ebfedea0SLionel Sambuc  out:
419ebfedea0SLionel Sambuc     *ai = host->ai;
420ebfedea0SLionel Sambuc     return ret;
421ebfedea0SLionel Sambuc }
422ebfedea0SLionel Sambuc 
423ebfedea0SLionel Sambuc static krb5_boolean
get_next(struct krb5_krbhst_data * kd,krb5_krbhst_info ** host)424ebfedea0SLionel Sambuc get_next(struct krb5_krbhst_data *kd, krb5_krbhst_info **host)
425ebfedea0SLionel Sambuc {
426ebfedea0SLionel Sambuc     struct krb5_krbhst_info *hi = *kd->index;
427ebfedea0SLionel Sambuc     if(hi != NULL) {
428ebfedea0SLionel Sambuc 	*host = hi;
429ebfedea0SLionel Sambuc 	kd->index = &(*kd->index)->next;
430ebfedea0SLionel Sambuc 	return TRUE;
431ebfedea0SLionel Sambuc     }
432ebfedea0SLionel Sambuc     return FALSE;
433ebfedea0SLionel Sambuc }
434ebfedea0SLionel Sambuc 
435ebfedea0SLionel Sambuc static void
srv_get_hosts(krb5_context context,struct krb5_krbhst_data * kd,const char * proto,const char * service)436ebfedea0SLionel Sambuc srv_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
437ebfedea0SLionel Sambuc 	      const char *proto, const char *service)
438ebfedea0SLionel Sambuc {
439ebfedea0SLionel Sambuc     krb5_error_code ret;
440ebfedea0SLionel Sambuc     krb5_krbhst_info **res;
441ebfedea0SLionel Sambuc     int count, i;
442ebfedea0SLionel Sambuc 
443ebfedea0SLionel Sambuc     ret = srv_find_realm(context, &res, &count, kd->realm, "SRV", proto, service,
444ebfedea0SLionel Sambuc 			 kd->port);
445ebfedea0SLionel Sambuc     _krb5_debug(context, 2, "searching DNS for realm %s %s.%s -> %d",
446ebfedea0SLionel Sambuc 		kd->realm, proto, service, ret);
447ebfedea0SLionel Sambuc     if (ret)
448ebfedea0SLionel Sambuc 	return;
449ebfedea0SLionel Sambuc     for(i = 0; i < count; i++)
450ebfedea0SLionel Sambuc 	append_host_hostinfo(kd, res[i]);
451ebfedea0SLionel Sambuc     free(res);
452ebfedea0SLionel Sambuc }
453ebfedea0SLionel Sambuc 
454ebfedea0SLionel Sambuc /*
455ebfedea0SLionel Sambuc  * read the configuration for `conf_string', defaulting to kd->def_port and
456ebfedea0SLionel Sambuc  * forcing it to `kd->port' if kd->port != 0
457ebfedea0SLionel Sambuc  */
458ebfedea0SLionel Sambuc 
459ebfedea0SLionel Sambuc static void
config_get_hosts(krb5_context context,struct krb5_krbhst_data * kd,const char * conf_string)460ebfedea0SLionel Sambuc config_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
461ebfedea0SLionel Sambuc 		 const char *conf_string)
462ebfedea0SLionel Sambuc {
463ebfedea0SLionel Sambuc     int i;
464ebfedea0SLionel Sambuc     char **hostlist;
465ebfedea0SLionel Sambuc     hostlist = krb5_config_get_strings(context, NULL,
466ebfedea0SLionel Sambuc 				       "realms", kd->realm, conf_string, NULL);
467ebfedea0SLionel Sambuc 
468ebfedea0SLionel Sambuc     _krb5_debug(context, 2, "configuration file for realm %s%s found",
469ebfedea0SLionel Sambuc 		kd->realm, hostlist ? "" : " not");
470ebfedea0SLionel Sambuc 
471ebfedea0SLionel Sambuc     if(hostlist == NULL)
472ebfedea0SLionel Sambuc 	return;
473ebfedea0SLionel Sambuc     kd->flags |= KD_CONFIG_EXISTS;
474ebfedea0SLionel Sambuc     for(i = 0; hostlist && hostlist[i] != NULL; i++)
475ebfedea0SLionel Sambuc 	append_host_string(context, kd, hostlist[i], kd->def_port, kd->port);
476ebfedea0SLionel Sambuc 
477ebfedea0SLionel Sambuc     krb5_config_free_strings(hostlist);
478ebfedea0SLionel Sambuc }
479ebfedea0SLionel Sambuc 
480ebfedea0SLionel Sambuc /*
481ebfedea0SLionel Sambuc  * as a fallback, look for `serv_string.kd->realm' (typically
482ebfedea0SLionel Sambuc  * kerberos.REALM, kerberos-1.REALM, ...
483ebfedea0SLionel Sambuc  * `port' is the default port for the service, and `proto' the
484ebfedea0SLionel Sambuc  * protocol
485ebfedea0SLionel Sambuc  */
486ebfedea0SLionel Sambuc 
487ebfedea0SLionel Sambuc static krb5_error_code
fallback_get_hosts(krb5_context context,struct krb5_krbhst_data * kd,const char * serv_string,int port,int proto)488ebfedea0SLionel Sambuc fallback_get_hosts(krb5_context context, struct krb5_krbhst_data *kd,
489ebfedea0SLionel Sambuc 		   const char *serv_string, int port, int proto)
490ebfedea0SLionel Sambuc {
491ebfedea0SLionel Sambuc     char *host = NULL;
492ebfedea0SLionel Sambuc     int ret;
493ebfedea0SLionel Sambuc     struct addrinfo *ai;
494ebfedea0SLionel Sambuc     struct addrinfo hints;
495ebfedea0SLionel Sambuc     char portstr[NI_MAXSERV];
496ebfedea0SLionel Sambuc 
497ebfedea0SLionel Sambuc     _krb5_debug(context, 2, "fallback lookup %d for realm %s (service %s)",
498ebfedea0SLionel Sambuc 		kd->fallback_count, kd->realm, serv_string);
499ebfedea0SLionel Sambuc 
500ebfedea0SLionel Sambuc     /*
501ebfedea0SLionel Sambuc      * Don't try forever in case the DNS server keep returning us
502ebfedea0SLionel Sambuc      * entries (like wildcard entries or the .nu TLD)
503ebfedea0SLionel Sambuc      */
504ebfedea0SLionel Sambuc     if(kd->fallback_count >= 5) {
505ebfedea0SLionel Sambuc 	kd->flags |= KD_FALLBACK;
506ebfedea0SLionel Sambuc 	return 0;
507ebfedea0SLionel Sambuc     }
508ebfedea0SLionel Sambuc 
509ebfedea0SLionel Sambuc     if(kd->fallback_count == 0)
510ebfedea0SLionel Sambuc 	ret = asprintf(&host, "%s.%s.", serv_string, kd->realm);
511ebfedea0SLionel Sambuc     else
512ebfedea0SLionel Sambuc 	ret = asprintf(&host, "%s-%d.%s.",
513ebfedea0SLionel Sambuc 		       serv_string, kd->fallback_count, kd->realm);
514ebfedea0SLionel Sambuc 
515ebfedea0SLionel Sambuc     if (ret < 0 || host == NULL)
516ebfedea0SLionel Sambuc 	return ENOMEM;
517ebfedea0SLionel Sambuc 
518ebfedea0SLionel Sambuc     make_hints(&hints, proto);
519ebfedea0SLionel Sambuc     snprintf(portstr, sizeof(portstr), "%d", port);
520ebfedea0SLionel Sambuc     ret = getaddrinfo(host, portstr, &hints, &ai);
521ebfedea0SLionel Sambuc     if (ret) {
522ebfedea0SLionel Sambuc 	/* no more hosts, so we're done here */
523ebfedea0SLionel Sambuc 	free(host);
524ebfedea0SLionel Sambuc 	kd->flags |= KD_FALLBACK;
525ebfedea0SLionel Sambuc     } else {
526ebfedea0SLionel Sambuc 	struct krb5_krbhst_info *hi;
527ebfedea0SLionel Sambuc 	size_t hostlen = strlen(host);
528ebfedea0SLionel Sambuc 
529ebfedea0SLionel Sambuc 	hi = calloc(1, sizeof(*hi) + hostlen);
530ebfedea0SLionel Sambuc 	if(hi == NULL) {
531ebfedea0SLionel Sambuc 	    free(host);
532ebfedea0SLionel Sambuc 	    return ENOMEM;
533ebfedea0SLionel Sambuc 	}
534ebfedea0SLionel Sambuc 
535ebfedea0SLionel Sambuc 	hi->proto = proto;
536ebfedea0SLionel Sambuc 	hi->port  = hi->def_port = port;
537ebfedea0SLionel Sambuc 	hi->ai    = ai;
538ebfedea0SLionel Sambuc 	memmove(hi->hostname, host, hostlen);
539ebfedea0SLionel Sambuc 	hi->hostname[hostlen] = '\0';
540ebfedea0SLionel Sambuc 	free(host);
541ebfedea0SLionel Sambuc 	append_host_hostinfo(kd, hi);
542ebfedea0SLionel Sambuc 	kd->fallback_count++;
543ebfedea0SLionel Sambuc     }
544ebfedea0SLionel Sambuc     return 0;
545ebfedea0SLionel Sambuc }
546ebfedea0SLionel Sambuc 
547ebfedea0SLionel Sambuc /*
548ebfedea0SLionel Sambuc  * Fetch hosts from plugin
549ebfedea0SLionel Sambuc  */
550ebfedea0SLionel Sambuc 
551ebfedea0SLionel Sambuc static krb5_error_code
add_locate(void * ctx,int type,struct sockaddr * addr)552ebfedea0SLionel Sambuc add_locate(void *ctx, int type, struct sockaddr *addr)
553ebfedea0SLionel Sambuc {
554ebfedea0SLionel Sambuc     struct krb5_krbhst_info *hi;
555ebfedea0SLionel Sambuc     struct krb5_krbhst_data *kd = ctx;
556ebfedea0SLionel Sambuc     char host[NI_MAXHOST], port[NI_MAXSERV];
557ebfedea0SLionel Sambuc     struct addrinfo hints, *ai;
558ebfedea0SLionel Sambuc     socklen_t socklen;
559ebfedea0SLionel Sambuc     size_t hostlen;
560ebfedea0SLionel Sambuc     int ret;
561ebfedea0SLionel Sambuc 
562ebfedea0SLionel Sambuc     socklen = socket_sockaddr_size(addr);
563ebfedea0SLionel Sambuc 
564ebfedea0SLionel Sambuc     ret = getnameinfo(addr, socklen, host, sizeof(host), port, sizeof(port),
565ebfedea0SLionel Sambuc 		      NI_NUMERICHOST|NI_NUMERICSERV);
566ebfedea0SLionel Sambuc     if (ret != 0)
567ebfedea0SLionel Sambuc 	return 0;
568ebfedea0SLionel Sambuc 
569ebfedea0SLionel Sambuc     make_hints(&hints, krbhst_get_default_proto(kd));
570ebfedea0SLionel Sambuc     ret = getaddrinfo(host, port, &hints, &ai);
571ebfedea0SLionel Sambuc     if (ret)
572ebfedea0SLionel Sambuc 	return 0;
573ebfedea0SLionel Sambuc 
574ebfedea0SLionel Sambuc     hostlen = strlen(host);
575ebfedea0SLionel Sambuc 
576ebfedea0SLionel Sambuc     hi = calloc(1, sizeof(*hi) + hostlen);
577ebfedea0SLionel Sambuc     if(hi == NULL)
578ebfedea0SLionel Sambuc 	return ENOMEM;
579ebfedea0SLionel Sambuc 
580ebfedea0SLionel Sambuc     hi->proto = krbhst_get_default_proto(kd);
581ebfedea0SLionel Sambuc     hi->port  = hi->def_port = socket_get_port(addr);
582ebfedea0SLionel Sambuc     hi->ai    = ai;
583ebfedea0SLionel Sambuc     memmove(hi->hostname, host, hostlen);
584ebfedea0SLionel Sambuc     hi->hostname[hostlen] = '\0';
585ebfedea0SLionel Sambuc     append_host_hostinfo(kd, hi);
586ebfedea0SLionel Sambuc 
587ebfedea0SLionel Sambuc     return 0;
588ebfedea0SLionel Sambuc }
589ebfedea0SLionel Sambuc 
590ebfedea0SLionel Sambuc static void
plugin_get_hosts(krb5_context context,struct krb5_krbhst_data * kd,enum locate_service_type type)591ebfedea0SLionel Sambuc plugin_get_hosts(krb5_context context,
592ebfedea0SLionel Sambuc 		 struct krb5_krbhst_data *kd,
593ebfedea0SLionel Sambuc 		 enum locate_service_type type)
594ebfedea0SLionel Sambuc {
595ebfedea0SLionel Sambuc     struct krb5_plugin *list = NULL, *e;
596ebfedea0SLionel Sambuc     krb5_error_code ret;
597ebfedea0SLionel Sambuc 
598ebfedea0SLionel Sambuc     ret = _krb5_plugin_find(context, PLUGIN_TYPE_DATA,
599ebfedea0SLionel Sambuc 			    KRB5_PLUGIN_LOCATE, &list);
600ebfedea0SLionel Sambuc     if(ret != 0 || list == NULL)
601ebfedea0SLionel Sambuc 	return;
602ebfedea0SLionel Sambuc 
603ebfedea0SLionel Sambuc     for (e = list; e != NULL; e = _krb5_plugin_get_next(e)) {
604ebfedea0SLionel Sambuc 	krb5plugin_service_locate_ftable *service;
605ebfedea0SLionel Sambuc 	void *ctx;
606ebfedea0SLionel Sambuc 
607ebfedea0SLionel Sambuc 	service = _krb5_plugin_get_symbol(e);
608ebfedea0SLionel Sambuc 	if (service->minor_version != 0)
609ebfedea0SLionel Sambuc 	    continue;
610ebfedea0SLionel Sambuc 
611ebfedea0SLionel Sambuc 	(*service->init)(context, &ctx);
612ebfedea0SLionel Sambuc 	ret = (*service->lookup)(ctx, type, kd->realm, 0, 0, add_locate, kd);
613ebfedea0SLionel Sambuc 	(*service->fini)(ctx);
614ebfedea0SLionel Sambuc 	if (ret && ret != KRB5_PLUGIN_NO_HANDLE) {
615ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
616ebfedea0SLionel Sambuc 				   N_("Locate plugin failed to lookup realm %s: %d", ""),
617ebfedea0SLionel Sambuc 				   kd->realm, ret);
618ebfedea0SLionel Sambuc 	    break;
619ebfedea0SLionel Sambuc 	} else if (ret == 0) {
620ebfedea0SLionel Sambuc 	    _krb5_debug(context, 2, "plugin found result for realm %s", kd->realm);
621ebfedea0SLionel Sambuc 	    kd->flags |= KD_CONFIG_EXISTS;
622ebfedea0SLionel Sambuc 	}
623ebfedea0SLionel Sambuc 
624ebfedea0SLionel Sambuc     }
625ebfedea0SLionel Sambuc     _krb5_plugin_free(list);
626ebfedea0SLionel Sambuc }
627ebfedea0SLionel Sambuc 
628ebfedea0SLionel Sambuc /*
629ebfedea0SLionel Sambuc  *
630ebfedea0SLionel Sambuc  */
631ebfedea0SLionel Sambuc 
632ebfedea0SLionel Sambuc static krb5_error_code
kdc_get_next(krb5_context context,struct krb5_krbhst_data * kd,krb5_krbhst_info ** host)633ebfedea0SLionel Sambuc kdc_get_next(krb5_context context,
634ebfedea0SLionel Sambuc 	     struct krb5_krbhst_data *kd,
635ebfedea0SLionel Sambuc 	     krb5_krbhst_info **host)
636ebfedea0SLionel Sambuc {
637ebfedea0SLionel Sambuc     krb5_error_code ret;
638ebfedea0SLionel Sambuc 
639ebfedea0SLionel Sambuc     if ((kd->flags & KD_PLUGIN) == 0) {
640ebfedea0SLionel Sambuc 	plugin_get_hosts(context, kd, locate_service_kdc);
641ebfedea0SLionel Sambuc 	kd->flags |= KD_PLUGIN;
642ebfedea0SLionel Sambuc 	if(get_next(kd, host))
643ebfedea0SLionel Sambuc 	    return 0;
644ebfedea0SLionel Sambuc     }
645ebfedea0SLionel Sambuc 
646ebfedea0SLionel Sambuc     if((kd->flags & KD_CONFIG) == 0) {
647ebfedea0SLionel Sambuc 	config_get_hosts(context, kd, "kdc");
648ebfedea0SLionel Sambuc 	kd->flags |= KD_CONFIG;
649ebfedea0SLionel Sambuc 	if(get_next(kd, host))
650ebfedea0SLionel Sambuc 	    return 0;
651ebfedea0SLionel Sambuc     }
652ebfedea0SLionel Sambuc 
653ebfedea0SLionel Sambuc     if (kd->flags & KD_CONFIG_EXISTS) {
654ebfedea0SLionel Sambuc 	_krb5_debug(context, 1,
655ebfedea0SLionel Sambuc 		    "Configuration exists for realm %s, wont go to DNS",
656ebfedea0SLionel Sambuc 		    kd->realm);
657ebfedea0SLionel Sambuc 	return KRB5_KDC_UNREACH;
658ebfedea0SLionel Sambuc     }
659ebfedea0SLionel Sambuc 
660ebfedea0SLionel Sambuc     if(context->srv_lookup) {
661ebfedea0SLionel Sambuc 	if((kd->flags & KD_SRV_UDP) == 0 && (kd->flags & KD_LARGE_MSG) == 0) {
662ebfedea0SLionel Sambuc 	    srv_get_hosts(context, kd, "udp", "kerberos");
663ebfedea0SLionel Sambuc 	    kd->flags |= KD_SRV_UDP;
664ebfedea0SLionel Sambuc 	    if(get_next(kd, host))
665ebfedea0SLionel Sambuc 		return 0;
666ebfedea0SLionel Sambuc 	}
667ebfedea0SLionel Sambuc 
668ebfedea0SLionel Sambuc 	if((kd->flags & KD_SRV_TCP) == 0) {
669ebfedea0SLionel Sambuc 	    srv_get_hosts(context, kd, "tcp", "kerberos");
670ebfedea0SLionel Sambuc 	    kd->flags |= KD_SRV_TCP;
671ebfedea0SLionel Sambuc 	    if(get_next(kd, host))
672ebfedea0SLionel Sambuc 		return 0;
673ebfedea0SLionel Sambuc 	}
674ebfedea0SLionel Sambuc 	if((kd->flags & KD_SRV_HTTP) == 0) {
675ebfedea0SLionel Sambuc 	    srv_get_hosts(context, kd, "http", "kerberos");
676ebfedea0SLionel Sambuc 	    kd->flags |= KD_SRV_HTTP;
677ebfedea0SLionel Sambuc 	    if(get_next(kd, host))
678ebfedea0SLionel Sambuc 		return 0;
679ebfedea0SLionel Sambuc 	}
680ebfedea0SLionel Sambuc     }
681ebfedea0SLionel Sambuc 
682ebfedea0SLionel Sambuc     while((kd->flags & KD_FALLBACK) == 0) {
683ebfedea0SLionel Sambuc 	ret = fallback_get_hosts(context, kd, "kerberos",
684ebfedea0SLionel Sambuc 				 kd->def_port,
685ebfedea0SLionel Sambuc 				 krbhst_get_default_proto(kd));
686ebfedea0SLionel Sambuc 	if(ret)
687ebfedea0SLionel Sambuc 	    return ret;
688ebfedea0SLionel Sambuc 	if(get_next(kd, host))
689ebfedea0SLionel Sambuc 	    return 0;
690ebfedea0SLionel Sambuc     }
691ebfedea0SLionel Sambuc 
692ebfedea0SLionel Sambuc     _krb5_debug(context, 0, "No KDC entries found for %s", kd->realm);
693ebfedea0SLionel Sambuc 
694ebfedea0SLionel Sambuc     return KRB5_KDC_UNREACH; /* XXX */
695ebfedea0SLionel Sambuc }
696ebfedea0SLionel Sambuc 
697ebfedea0SLionel Sambuc static krb5_error_code
admin_get_next(krb5_context context,struct krb5_krbhst_data * kd,krb5_krbhst_info ** host)698ebfedea0SLionel Sambuc admin_get_next(krb5_context context,
699ebfedea0SLionel Sambuc 	       struct krb5_krbhst_data *kd,
700ebfedea0SLionel Sambuc 	       krb5_krbhst_info **host)
701ebfedea0SLionel Sambuc {
702ebfedea0SLionel Sambuc     krb5_error_code ret;
703ebfedea0SLionel Sambuc 
704ebfedea0SLionel Sambuc     if ((kd->flags & KD_PLUGIN) == 0) {
705ebfedea0SLionel Sambuc 	plugin_get_hosts(context, kd, locate_service_kadmin);
706ebfedea0SLionel Sambuc 	kd->flags |= KD_PLUGIN;
707ebfedea0SLionel Sambuc 	if(get_next(kd, host))
708ebfedea0SLionel Sambuc 	    return 0;
709ebfedea0SLionel Sambuc     }
710ebfedea0SLionel Sambuc 
711ebfedea0SLionel Sambuc     if((kd->flags & KD_CONFIG) == 0) {
712ebfedea0SLionel Sambuc 	config_get_hosts(context, kd, "admin_server");
713ebfedea0SLionel Sambuc 	kd->flags |= KD_CONFIG;
714ebfedea0SLionel Sambuc 	if(get_next(kd, host))
715ebfedea0SLionel Sambuc 	    return 0;
716ebfedea0SLionel Sambuc     }
717ebfedea0SLionel Sambuc 
718ebfedea0SLionel Sambuc     if (kd->flags & KD_CONFIG_EXISTS) {
719ebfedea0SLionel Sambuc 	_krb5_debug(context, 1,
720ebfedea0SLionel Sambuc 		    "Configuration exists for realm %s, wont go to DNS",
721ebfedea0SLionel Sambuc 		    kd->realm);
722ebfedea0SLionel Sambuc 	return KRB5_KDC_UNREACH;
723ebfedea0SLionel Sambuc     }
724ebfedea0SLionel Sambuc 
725ebfedea0SLionel Sambuc     if(context->srv_lookup) {
726ebfedea0SLionel Sambuc 	if((kd->flags & KD_SRV_TCP) == 0) {
727ebfedea0SLionel Sambuc 	    srv_get_hosts(context, kd, "tcp", "kerberos-adm");
728ebfedea0SLionel Sambuc 	    kd->flags |= KD_SRV_TCP;
729ebfedea0SLionel Sambuc 	    if(get_next(kd, host))
730ebfedea0SLionel Sambuc 		return 0;
731ebfedea0SLionel Sambuc 	}
732ebfedea0SLionel Sambuc     }
733ebfedea0SLionel Sambuc 
734ebfedea0SLionel Sambuc     if (krbhst_empty(kd)
735ebfedea0SLionel Sambuc 	&& (kd->flags & KD_FALLBACK) == 0) {
736ebfedea0SLionel Sambuc 	ret = fallback_get_hosts(context, kd, "kerberos",
737ebfedea0SLionel Sambuc 				 kd->def_port,
738ebfedea0SLionel Sambuc 				 krbhst_get_default_proto(kd));
739ebfedea0SLionel Sambuc 	if(ret)
740ebfedea0SLionel Sambuc 	    return ret;
741ebfedea0SLionel Sambuc 	kd->flags |= KD_FALLBACK;
742ebfedea0SLionel Sambuc 	if(get_next(kd, host))
743ebfedea0SLionel Sambuc 	    return 0;
744ebfedea0SLionel Sambuc     }
745ebfedea0SLionel Sambuc 
746ebfedea0SLionel Sambuc     _krb5_debug(context, 0, "No admin entries found for realm %s", kd->realm);
747ebfedea0SLionel Sambuc 
748ebfedea0SLionel Sambuc     return KRB5_KDC_UNREACH;	/* XXX */
749ebfedea0SLionel Sambuc }
750ebfedea0SLionel Sambuc 
751ebfedea0SLionel Sambuc static krb5_error_code
kpasswd_get_next(krb5_context context,struct krb5_krbhst_data * kd,krb5_krbhst_info ** host)752ebfedea0SLionel Sambuc kpasswd_get_next(krb5_context context,
753ebfedea0SLionel Sambuc 		 struct krb5_krbhst_data *kd,
754ebfedea0SLionel Sambuc 		 krb5_krbhst_info **host)
755ebfedea0SLionel Sambuc {
756ebfedea0SLionel Sambuc     krb5_error_code ret;
757ebfedea0SLionel Sambuc 
758ebfedea0SLionel Sambuc     if ((kd->flags & KD_PLUGIN) == 0) {
759ebfedea0SLionel Sambuc 	plugin_get_hosts(context, kd, locate_service_kpasswd);
760ebfedea0SLionel Sambuc 	kd->flags |= KD_PLUGIN;
761ebfedea0SLionel Sambuc 	if(get_next(kd, host))
762ebfedea0SLionel Sambuc 	    return 0;
763ebfedea0SLionel Sambuc     }
764ebfedea0SLionel Sambuc 
765ebfedea0SLionel Sambuc     if((kd->flags & KD_CONFIG) == 0) {
766ebfedea0SLionel Sambuc 	config_get_hosts(context, kd, "kpasswd_server");
767ebfedea0SLionel Sambuc 	kd->flags |= KD_CONFIG;
768ebfedea0SLionel Sambuc 	if(get_next(kd, host))
769ebfedea0SLionel Sambuc 	    return 0;
770ebfedea0SLionel Sambuc     }
771ebfedea0SLionel Sambuc 
772ebfedea0SLionel Sambuc     if (kd->flags & KD_CONFIG_EXISTS) {
773ebfedea0SLionel Sambuc 	_krb5_debug(context, 1,
774ebfedea0SLionel Sambuc 		    "Configuration exists for realm %s, wont go to DNS",
775ebfedea0SLionel Sambuc 		    kd->realm);
776ebfedea0SLionel Sambuc 	return KRB5_KDC_UNREACH;
777ebfedea0SLionel Sambuc     }
778ebfedea0SLionel Sambuc 
779ebfedea0SLionel Sambuc     if(context->srv_lookup) {
780ebfedea0SLionel Sambuc 	if((kd->flags & KD_SRV_UDP) == 0) {
781ebfedea0SLionel Sambuc 	    srv_get_hosts(context, kd, "udp", "kpasswd");
782ebfedea0SLionel Sambuc 	    kd->flags |= KD_SRV_UDP;
783ebfedea0SLionel Sambuc 	    if(get_next(kd, host))
784ebfedea0SLionel Sambuc 		return 0;
785ebfedea0SLionel Sambuc 	}
786ebfedea0SLionel Sambuc 	if((kd->flags & KD_SRV_TCP) == 0) {
787ebfedea0SLionel Sambuc 	    srv_get_hosts(context, kd, "tcp", "kpasswd");
788ebfedea0SLionel Sambuc 	    kd->flags |= KD_SRV_TCP;
789ebfedea0SLionel Sambuc 	    if(get_next(kd, host))
790ebfedea0SLionel Sambuc 		return 0;
791ebfedea0SLionel Sambuc 	}
792ebfedea0SLionel Sambuc     }
793ebfedea0SLionel Sambuc 
794ebfedea0SLionel Sambuc     /* no matches -> try admin */
795ebfedea0SLionel Sambuc 
796ebfedea0SLionel Sambuc     if (krbhst_empty(kd)) {
797ebfedea0SLionel Sambuc 	kd->flags = 0;
798ebfedea0SLionel Sambuc 	kd->port  = kd->def_port;
799ebfedea0SLionel Sambuc 	kd->get_next = admin_get_next;
800ebfedea0SLionel Sambuc 	ret = (*kd->get_next)(context, kd, host);
801ebfedea0SLionel Sambuc 	if (ret == 0)
802ebfedea0SLionel Sambuc 	    (*host)->proto = krbhst_get_default_proto(kd);
803ebfedea0SLionel Sambuc 	return ret;
804ebfedea0SLionel Sambuc     }
805ebfedea0SLionel Sambuc 
806ebfedea0SLionel Sambuc     _krb5_debug(context, 0, "No kpasswd entries found for realm %s", kd->realm);
807ebfedea0SLionel Sambuc 
808ebfedea0SLionel Sambuc     return KRB5_KDC_UNREACH;
809ebfedea0SLionel Sambuc }
810ebfedea0SLionel Sambuc 
811ebfedea0SLionel Sambuc static krb5_error_code
krb524_get_next(krb5_context context,struct krb5_krbhst_data * kd,krb5_krbhst_info ** host)812ebfedea0SLionel Sambuc krb524_get_next(krb5_context context,
813ebfedea0SLionel Sambuc 		struct krb5_krbhst_data *kd,
814ebfedea0SLionel Sambuc 		krb5_krbhst_info **host)
815ebfedea0SLionel Sambuc {
816ebfedea0SLionel Sambuc     if ((kd->flags & KD_PLUGIN) == 0) {
817ebfedea0SLionel Sambuc 	plugin_get_hosts(context, kd, locate_service_krb524);
818ebfedea0SLionel Sambuc 	kd->flags |= KD_PLUGIN;
819ebfedea0SLionel Sambuc 	if(get_next(kd, host))
820ebfedea0SLionel Sambuc 	    return 0;
821ebfedea0SLionel Sambuc     }
822ebfedea0SLionel Sambuc 
823ebfedea0SLionel Sambuc     if((kd->flags & KD_CONFIG) == 0) {
824ebfedea0SLionel Sambuc 	config_get_hosts(context, kd, "krb524_server");
825ebfedea0SLionel Sambuc 	if(get_next(kd, host))
826ebfedea0SLionel Sambuc 	    return 0;
827ebfedea0SLionel Sambuc 	kd->flags |= KD_CONFIG;
828ebfedea0SLionel Sambuc     }
829ebfedea0SLionel Sambuc 
830ebfedea0SLionel Sambuc     if (kd->flags & KD_CONFIG_EXISTS) {
831ebfedea0SLionel Sambuc 	_krb5_debug(context, 1,
832ebfedea0SLionel Sambuc 		    "Configuration exists for realm %s, wont go to DNS",
833ebfedea0SLionel Sambuc 		    kd->realm);
834ebfedea0SLionel Sambuc 	return KRB5_KDC_UNREACH;
835ebfedea0SLionel Sambuc     }
836ebfedea0SLionel Sambuc 
837ebfedea0SLionel Sambuc     if(context->srv_lookup) {
838ebfedea0SLionel Sambuc 	if((kd->flags & KD_SRV_UDP) == 0) {
839ebfedea0SLionel Sambuc 	    srv_get_hosts(context, kd, "udp", "krb524");
840ebfedea0SLionel Sambuc 	    kd->flags |= KD_SRV_UDP;
841ebfedea0SLionel Sambuc 	    if(get_next(kd, host))
842ebfedea0SLionel Sambuc 		return 0;
843ebfedea0SLionel Sambuc 	}
844ebfedea0SLionel Sambuc 
845ebfedea0SLionel Sambuc 	if((kd->flags & KD_SRV_TCP) == 0) {
846ebfedea0SLionel Sambuc 	    srv_get_hosts(context, kd, "tcp", "krb524");
847ebfedea0SLionel Sambuc 	    kd->flags |= KD_SRV_TCP;
848ebfedea0SLionel Sambuc 	    if(get_next(kd, host))
849ebfedea0SLionel Sambuc 		return 0;
850ebfedea0SLionel Sambuc 	}
851ebfedea0SLionel Sambuc     }
852ebfedea0SLionel Sambuc 
853ebfedea0SLionel Sambuc     /* no matches -> try kdc */
854ebfedea0SLionel Sambuc 
855ebfedea0SLionel Sambuc     if (krbhst_empty(kd)) {
856ebfedea0SLionel Sambuc 	kd->flags = 0;
857ebfedea0SLionel Sambuc 	kd->port  = kd->def_port;
858ebfedea0SLionel Sambuc 	kd->get_next = kdc_get_next;
859ebfedea0SLionel Sambuc 	return (*kd->get_next)(context, kd, host);
860ebfedea0SLionel Sambuc     }
861ebfedea0SLionel Sambuc 
862ebfedea0SLionel Sambuc     _krb5_debug(context, 0, "No kpasswd entries found for realm %s", kd->realm);
863ebfedea0SLionel Sambuc 
864ebfedea0SLionel Sambuc     return KRB5_KDC_UNREACH;
865ebfedea0SLionel Sambuc }
866ebfedea0SLionel Sambuc 
867ebfedea0SLionel Sambuc static struct krb5_krbhst_data*
common_init(krb5_context context,const char * service,const char * realm,int flags)868ebfedea0SLionel Sambuc common_init(krb5_context context,
869ebfedea0SLionel Sambuc 	    const char *service,
870ebfedea0SLionel Sambuc 	    const char *realm,
871ebfedea0SLionel Sambuc 	    int flags)
872ebfedea0SLionel Sambuc {
873ebfedea0SLionel Sambuc     struct krb5_krbhst_data *kd;
874ebfedea0SLionel Sambuc 
875ebfedea0SLionel Sambuc     if((kd = calloc(1, sizeof(*kd))) == NULL)
876ebfedea0SLionel Sambuc 	return NULL;
877ebfedea0SLionel Sambuc 
878ebfedea0SLionel Sambuc     if((kd->realm = strdup(realm)) == NULL) {
879ebfedea0SLionel Sambuc 	free(kd);
880ebfedea0SLionel Sambuc 	return NULL;
881ebfedea0SLionel Sambuc     }
882ebfedea0SLionel Sambuc 
883ebfedea0SLionel Sambuc     _krb5_debug(context, 2, "Trying to find service %s for realm %s flags %x",
884ebfedea0SLionel Sambuc 		service, realm, flags);
885ebfedea0SLionel Sambuc 
886ebfedea0SLionel Sambuc     /* For 'realms' without a . do not even think of going to DNS */
887ebfedea0SLionel Sambuc     if (!strchr(realm, '.'))
888ebfedea0SLionel Sambuc 	kd->flags |= KD_CONFIG_EXISTS;
889ebfedea0SLionel Sambuc 
890ebfedea0SLionel Sambuc     if (flags & KRB5_KRBHST_FLAGS_LARGE_MSG)
891ebfedea0SLionel Sambuc 	kd->flags |= KD_LARGE_MSG;
892ebfedea0SLionel Sambuc     kd->end = kd->index = &kd->hosts;
893ebfedea0SLionel Sambuc     return kd;
894ebfedea0SLionel Sambuc }
895ebfedea0SLionel Sambuc 
896ebfedea0SLionel Sambuc /*
897ebfedea0SLionel Sambuc  * initialize `handle' to look for hosts of type `type' in realm `realm'
898ebfedea0SLionel Sambuc  */
899ebfedea0SLionel Sambuc 
900ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_krbhst_init(krb5_context context,const char * realm,unsigned int type,krb5_krbhst_handle * handle)901ebfedea0SLionel Sambuc krb5_krbhst_init(krb5_context context,
902ebfedea0SLionel Sambuc 		 const char *realm,
903ebfedea0SLionel Sambuc 		 unsigned int type,
904ebfedea0SLionel Sambuc 		 krb5_krbhst_handle *handle)
905ebfedea0SLionel Sambuc {
906ebfedea0SLionel Sambuc     return krb5_krbhst_init_flags(context, realm, type, 0, handle);
907ebfedea0SLionel Sambuc }
908ebfedea0SLionel Sambuc 
909ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_krbhst_init_flags(krb5_context context,const char * realm,unsigned int type,int flags,krb5_krbhst_handle * handle)910ebfedea0SLionel Sambuc krb5_krbhst_init_flags(krb5_context context,
911ebfedea0SLionel Sambuc 		       const char *realm,
912ebfedea0SLionel Sambuc 		       unsigned int type,
913ebfedea0SLionel Sambuc 		       int flags,
914ebfedea0SLionel Sambuc 		       krb5_krbhst_handle *handle)
915ebfedea0SLionel Sambuc {
916ebfedea0SLionel Sambuc     struct krb5_krbhst_data *kd;
917ebfedea0SLionel Sambuc     krb5_error_code (*next)(krb5_context, struct krb5_krbhst_data *,
918ebfedea0SLionel Sambuc 			    krb5_krbhst_info **);
919ebfedea0SLionel Sambuc     int def_port;
920ebfedea0SLionel Sambuc     const char *service;
921ebfedea0SLionel Sambuc 
922ebfedea0SLionel Sambuc     switch(type) {
923ebfedea0SLionel Sambuc     case KRB5_KRBHST_KDC:
924ebfedea0SLionel Sambuc 	next = kdc_get_next;
925ebfedea0SLionel Sambuc 	def_port = ntohs(krb5_getportbyname (context, "kerberos", "udp", 88));
926ebfedea0SLionel Sambuc 	service = "kdc";
927ebfedea0SLionel Sambuc 	break;
928ebfedea0SLionel Sambuc     case KRB5_KRBHST_ADMIN:
929ebfedea0SLionel Sambuc 	next = admin_get_next;
930ebfedea0SLionel Sambuc 	def_port = ntohs(krb5_getportbyname (context, "kerberos-adm",
931ebfedea0SLionel Sambuc 					     "tcp", 749));
932ebfedea0SLionel Sambuc 	service = "admin";
933ebfedea0SLionel Sambuc 	break;
934ebfedea0SLionel Sambuc     case KRB5_KRBHST_CHANGEPW:
935ebfedea0SLionel Sambuc 	next = kpasswd_get_next;
936ebfedea0SLionel Sambuc 	def_port = ntohs(krb5_getportbyname (context, "kpasswd", "udp",
937ebfedea0SLionel Sambuc 					     KPASSWD_PORT));
938ebfedea0SLionel Sambuc 	service = "change_password";
939ebfedea0SLionel Sambuc 	break;
940ebfedea0SLionel Sambuc     case KRB5_KRBHST_KRB524:
941ebfedea0SLionel Sambuc 	next = krb524_get_next;
942ebfedea0SLionel Sambuc 	def_port = ntohs(krb5_getportbyname (context, "krb524", "udp", 4444));
943ebfedea0SLionel Sambuc 	service = "524";
944ebfedea0SLionel Sambuc 	break;
945ebfedea0SLionel Sambuc     default:
946ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOTTY,
947ebfedea0SLionel Sambuc 			       N_("unknown krbhst type (%u)", ""), type);
948ebfedea0SLionel Sambuc 	return ENOTTY;
949ebfedea0SLionel Sambuc     }
950ebfedea0SLionel Sambuc     if((kd = common_init(context, service, realm, flags)) == NULL)
951ebfedea0SLionel Sambuc 	return ENOMEM;
952ebfedea0SLionel Sambuc     kd->get_next = next;
953ebfedea0SLionel Sambuc     kd->def_port = def_port;
954ebfedea0SLionel Sambuc     *handle = kd;
955ebfedea0SLionel Sambuc     return 0;
956ebfedea0SLionel Sambuc }
957ebfedea0SLionel Sambuc 
958ebfedea0SLionel Sambuc /*
959ebfedea0SLionel Sambuc  * return the next host information from `handle' in `host'
960ebfedea0SLionel Sambuc  */
961ebfedea0SLionel Sambuc 
962ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_krbhst_next(krb5_context context,krb5_krbhst_handle handle,krb5_krbhst_info ** host)963ebfedea0SLionel Sambuc krb5_krbhst_next(krb5_context context,
964ebfedea0SLionel Sambuc 		 krb5_krbhst_handle handle,
965ebfedea0SLionel Sambuc 		 krb5_krbhst_info **host)
966ebfedea0SLionel Sambuc {
967ebfedea0SLionel Sambuc     if(get_next(handle, host))
968ebfedea0SLionel Sambuc 	return 0;
969ebfedea0SLionel Sambuc 
970ebfedea0SLionel Sambuc     return (*handle->get_next)(context, handle, host);
971ebfedea0SLionel Sambuc }
972ebfedea0SLionel Sambuc 
973ebfedea0SLionel Sambuc /*
974ebfedea0SLionel Sambuc  * return the next host information from `handle' as a host name
975ebfedea0SLionel Sambuc  * in `hostname' (or length `hostlen)
976ebfedea0SLionel Sambuc  */
977ebfedea0SLionel Sambuc 
978ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_krbhst_next_as_string(krb5_context context,krb5_krbhst_handle handle,char * hostname,size_t hostlen)979ebfedea0SLionel Sambuc krb5_krbhst_next_as_string(krb5_context context,
980ebfedea0SLionel Sambuc 			   krb5_krbhst_handle handle,
981ebfedea0SLionel Sambuc 			   char *hostname,
982ebfedea0SLionel Sambuc 			   size_t hostlen)
983ebfedea0SLionel Sambuc {
984ebfedea0SLionel Sambuc     krb5_error_code ret;
985ebfedea0SLionel Sambuc     krb5_krbhst_info *host;
986ebfedea0SLionel Sambuc     ret = krb5_krbhst_next(context, handle, &host);
987ebfedea0SLionel Sambuc     if(ret)
988ebfedea0SLionel Sambuc 	return ret;
989ebfedea0SLionel Sambuc     return krb5_krbhst_format_string(context, host, hostname, hostlen);
990ebfedea0SLionel Sambuc }
991ebfedea0SLionel Sambuc 
992ebfedea0SLionel Sambuc 
993ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_krbhst_reset(krb5_context context,krb5_krbhst_handle handle)994ebfedea0SLionel Sambuc krb5_krbhst_reset(krb5_context context, krb5_krbhst_handle handle)
995ebfedea0SLionel Sambuc {
996ebfedea0SLionel Sambuc     handle->index = &handle->hosts;
997ebfedea0SLionel Sambuc }
998ebfedea0SLionel Sambuc 
999ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_krbhst_free(krb5_context context,krb5_krbhst_handle handle)1000ebfedea0SLionel Sambuc krb5_krbhst_free(krb5_context context, krb5_krbhst_handle handle)
1001ebfedea0SLionel Sambuc {
1002ebfedea0SLionel Sambuc     krb5_krbhst_info *h, *next;
1003ebfedea0SLionel Sambuc 
1004ebfedea0SLionel Sambuc     if (handle == NULL)
1005ebfedea0SLionel Sambuc 	return;
1006ebfedea0SLionel Sambuc 
1007ebfedea0SLionel Sambuc     for (h = handle->hosts; h != NULL; h = next) {
1008ebfedea0SLionel Sambuc 	next = h->next;
1009ebfedea0SLionel Sambuc 	_krb5_free_krbhst_info(h);
1010ebfedea0SLionel Sambuc     }
1011ebfedea0SLionel Sambuc 
1012ebfedea0SLionel Sambuc     free(handle->realm);
1013ebfedea0SLionel Sambuc     free(handle);
1014ebfedea0SLionel Sambuc }
1015ebfedea0SLionel Sambuc 
1016ebfedea0SLionel Sambuc /* backwards compatibility ahead */
1017ebfedea0SLionel Sambuc 
1018ebfedea0SLionel Sambuc static krb5_error_code
gethostlist(krb5_context context,const char * realm,unsigned int type,char *** hostlist)1019ebfedea0SLionel Sambuc gethostlist(krb5_context context, const char *realm,
1020ebfedea0SLionel Sambuc 	    unsigned int type, char ***hostlist)
1021ebfedea0SLionel Sambuc {
1022ebfedea0SLionel Sambuc     krb5_error_code ret;
1023ebfedea0SLionel Sambuc     int nhost = 0;
1024ebfedea0SLionel Sambuc     krb5_krbhst_handle handle;
1025ebfedea0SLionel Sambuc     char host[MAXHOSTNAMELEN];
1026ebfedea0SLionel Sambuc     krb5_krbhst_info *hostinfo;
1027ebfedea0SLionel Sambuc 
1028ebfedea0SLionel Sambuc     ret = krb5_krbhst_init(context, realm, type, &handle);
1029ebfedea0SLionel Sambuc     if (ret)
1030ebfedea0SLionel Sambuc 	return ret;
1031ebfedea0SLionel Sambuc 
1032ebfedea0SLionel Sambuc     while(krb5_krbhst_next(context, handle, &hostinfo) == 0)
1033ebfedea0SLionel Sambuc 	nhost++;
1034ebfedea0SLionel Sambuc     if(nhost == 0) {
1035ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5_KDC_UNREACH,
1036ebfedea0SLionel Sambuc 			       N_("No KDC found for realm %s", ""), realm);
1037ebfedea0SLionel Sambuc 	return KRB5_KDC_UNREACH;
1038ebfedea0SLionel Sambuc     }
1039ebfedea0SLionel Sambuc     *hostlist = calloc(nhost + 1, sizeof(**hostlist));
1040ebfedea0SLionel Sambuc     if(*hostlist == NULL) {
1041ebfedea0SLionel Sambuc 	krb5_krbhst_free(context, handle);
1042ebfedea0SLionel Sambuc 	return ENOMEM;
1043ebfedea0SLionel Sambuc     }
1044ebfedea0SLionel Sambuc 
1045ebfedea0SLionel Sambuc     krb5_krbhst_reset(context, handle);
1046ebfedea0SLionel Sambuc     nhost = 0;
1047ebfedea0SLionel Sambuc     while(krb5_krbhst_next_as_string(context, handle,
1048ebfedea0SLionel Sambuc 				     host, sizeof(host)) == 0) {
1049ebfedea0SLionel Sambuc 	if(((*hostlist)[nhost++] = strdup(host)) == NULL) {
1050ebfedea0SLionel Sambuc 	    krb5_free_krbhst(context, *hostlist);
1051ebfedea0SLionel Sambuc 	    krb5_krbhst_free(context, handle);
1052ebfedea0SLionel Sambuc 	    return ENOMEM;
1053ebfedea0SLionel Sambuc 	}
1054ebfedea0SLionel Sambuc     }
1055ebfedea0SLionel Sambuc     (*hostlist)[nhost] = NULL;
1056ebfedea0SLionel Sambuc     krb5_krbhst_free(context, handle);
1057ebfedea0SLionel Sambuc     return 0;
1058ebfedea0SLionel Sambuc }
1059ebfedea0SLionel Sambuc 
1060ebfedea0SLionel Sambuc /*
1061ebfedea0SLionel Sambuc  * return an malloced list of kadmin-hosts for `realm' in `hostlist'
1062ebfedea0SLionel Sambuc  */
1063ebfedea0SLionel Sambuc 
1064ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_krb_admin_hst(krb5_context context,const krb5_realm * realm,char *** hostlist)1065ebfedea0SLionel Sambuc krb5_get_krb_admin_hst (krb5_context context,
1066ebfedea0SLionel Sambuc 			const krb5_realm *realm,
1067ebfedea0SLionel Sambuc 			char ***hostlist)
1068ebfedea0SLionel Sambuc {
1069ebfedea0SLionel Sambuc     return gethostlist(context, *realm, KRB5_KRBHST_ADMIN, hostlist);
1070ebfedea0SLionel Sambuc }
1071ebfedea0SLionel Sambuc 
1072ebfedea0SLionel Sambuc /*
1073ebfedea0SLionel Sambuc  * return an malloced list of changepw-hosts for `realm' in `hostlist'
1074ebfedea0SLionel Sambuc  */
1075ebfedea0SLionel Sambuc 
1076ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_krb_changepw_hst(krb5_context context,const krb5_realm * realm,char *** hostlist)1077ebfedea0SLionel Sambuc krb5_get_krb_changepw_hst (krb5_context context,
1078ebfedea0SLionel Sambuc 			   const krb5_realm *realm,
1079ebfedea0SLionel Sambuc 			   char ***hostlist)
1080ebfedea0SLionel Sambuc {
1081ebfedea0SLionel Sambuc     return gethostlist(context, *realm, KRB5_KRBHST_CHANGEPW, hostlist);
1082ebfedea0SLionel Sambuc }
1083ebfedea0SLionel Sambuc 
1084ebfedea0SLionel Sambuc /*
1085ebfedea0SLionel Sambuc  * return an malloced list of 524-hosts for `realm' in `hostlist'
1086ebfedea0SLionel Sambuc  */
1087ebfedea0SLionel Sambuc 
1088ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_krb524hst(krb5_context context,const krb5_realm * realm,char *** hostlist)1089ebfedea0SLionel Sambuc krb5_get_krb524hst (krb5_context context,
1090ebfedea0SLionel Sambuc 		    const krb5_realm *realm,
1091ebfedea0SLionel Sambuc 		    char ***hostlist)
1092ebfedea0SLionel Sambuc {
1093ebfedea0SLionel Sambuc     return gethostlist(context, *realm, KRB5_KRBHST_KRB524, hostlist);
1094ebfedea0SLionel Sambuc }
1095ebfedea0SLionel Sambuc 
1096ebfedea0SLionel Sambuc 
1097ebfedea0SLionel Sambuc /*
1098ebfedea0SLionel Sambuc  * return an malloced list of KDC's for `realm' in `hostlist'
1099ebfedea0SLionel Sambuc  */
1100ebfedea0SLionel Sambuc 
1101ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_krbhst(krb5_context context,const krb5_realm * realm,char *** hostlist)1102ebfedea0SLionel Sambuc krb5_get_krbhst (krb5_context context,
1103ebfedea0SLionel Sambuc 		 const krb5_realm *realm,
1104ebfedea0SLionel Sambuc 		 char ***hostlist)
1105ebfedea0SLionel Sambuc {
1106ebfedea0SLionel Sambuc     return gethostlist(context, *realm, KRB5_KRBHST_KDC, hostlist);
1107ebfedea0SLionel Sambuc }
1108ebfedea0SLionel Sambuc 
1109ebfedea0SLionel Sambuc /*
1110ebfedea0SLionel Sambuc  * free all the memory allocated in `hostlist'
1111ebfedea0SLionel Sambuc  */
1112ebfedea0SLionel Sambuc 
1113ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_free_krbhst(krb5_context context,char ** hostlist)1114ebfedea0SLionel Sambuc krb5_free_krbhst (krb5_context context,
1115ebfedea0SLionel Sambuc 		  char **hostlist)
1116ebfedea0SLionel Sambuc {
1117ebfedea0SLionel Sambuc     char **p;
1118ebfedea0SLionel Sambuc 
1119ebfedea0SLionel Sambuc     for (p = hostlist; *p; ++p)
1120ebfedea0SLionel Sambuc 	free (*p);
1121ebfedea0SLionel Sambuc     free (hostlist);
1122ebfedea0SLionel Sambuc     return 0;
1123ebfedea0SLionel Sambuc }
1124