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