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