| 1 |
/* |
| 2 |
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this software |
| 19 |
* without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 22 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 25 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "krb5_locl.h" |
| 35 |
#include <resolve.h> |
| 36 |
|
| 37 |
/* To automagically find the correct realm of a host (without |
| 38 |
* [domain_realm] in krb5.conf) add a text record for your domain with |
| 39 |
* the name of your realm, like this: |
| 40 |
* |
| 41 |
* _kerberos IN TXT "FOO.SE" |
| 42 |
* |
| 43 |
* The search is recursive, so you can add entries for specific |
| 44 |
* hosts. To find the realm of host a.b.c, it first tries |
| 45 |
* _kerberos.a.b.c, then _kerberos.b.c and so on. |
| 46 |
* |
| 47 |
* This method is described in draft-ietf-cat-krb-dns-locate-03.txt. |
| 48 |
* |
| 49 |
*/ |
| 50 |
|
| 51 |
static int |
| 52 |
copy_txt_to_realms(krb5_context context, |
| 53 |
const char *domain, |
| 54 |
struct rk_resource_record *head, |
| 55 |
krb5_realm **realms) |
| 56 |
{ |
| 57 |
struct rk_resource_record *rr; |
| 58 |
unsigned int n, i; |
| 59 |
|
| 60 |
for(n = 0, rr = head; rr; rr = rr->next) |
| 61 |
if (rr->type == rk_ns_t_txt) |
| 62 |
++n; |
| 63 |
|
| 64 |
if (n == 0) |
| 65 |
return -1; |
| 66 |
|
| 67 |
*realms = malloc ((n + 1) * sizeof(krb5_realm)); |
| 68 |
if (*realms == NULL) |
| 69 |
return krb5_enomem(context);; |
| 70 |
|
| 71 |
for (i = 0; i < n + 1; ++i) |
| 72 |
(*realms)[i] = NULL; |
| 73 |
|
| 74 |
for (i = 0, rr = head; rr; rr = rr->next) { |
| 75 |
if (rr->type == rk_ns_t_txt) { |
| 76 |
char *tmp = NULL; |
| 77 |
int invalid_tld = 1; |
| 78 |
|
| 79 |
/* Check for a gTLD controlled interruption */ |
| 80 |
if (strcmp("Your DNS configuration needs immediate " |
| 81 |
"attention see https://icann.org/namecollision", |
| 82 |
rr->u.txt) != 0) { |
| 83 |
invalid_tld = 0; |
| 84 |
tmp = strdup(rr->u.txt); |
| 85 |
} |
| 86 |
if (tmp == NULL) { |
| 87 |
for (i = 0; i < n; ++i) |
| 88 |
free ((*realms)[i]); |
| 89 |
free (*realms); |
| 90 |
if (invalid_tld) { |
| 91 |
krb5_warnx(context, |
| 92 |
"Realm lookup failed: " |
| 93 |
"Domain '%s' needs immediate attention " |
| 94 |
"see https://icann.org/namecollision", |
| 95 |
domain); |
| 96 |
return KRB5_KDC_UNREACH; |
| 97 |
} |
| 98 |
return krb5_enomem(context);; |
| 99 |
} |
| 100 |
(*realms)[i] = tmp; |
| 101 |
++i; |
| 102 |
} |
| 103 |
} |
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
static int |
| 108 |
dns_find_realm(krb5_context context, |
| 109 |
const char *domain, |
| 110 |
krb5_realm **realms) |
| 111 |
{ |
| 112 |
static const char *default_labels[] = { "_kerberos", NULL }; |
| •113 |
char dom[MaxHostNameLen]; |
| 114 |
struct rk_dns_reply *r; |
| 115 |
const char **labels; |
| 116 |
char **config_labels; |
| 117 |
int i, ret = 0; |
| 118 |
|
| 119 |
config_labels = krb5_config_get_strings(context, NULL, "libdefaults", |
| 120 |
"dns_lookup_realm_labels", NULL); |
| 121 |
if(config_labels != NULL) |
| 122 |
labels = (const char **)config_labels; |
| 123 |
else |
| 124 |
labels = default_labels; |
| 125 |
if(*domain == '.') |
| 126 |
domain++; |
| 127 |
for (i = 0; labels[i] != NULL; i++) { |
| 128 |
ret = snprintf(dom, sizeof(dom), "%s.%s.", labels[i], domain); |
| 129 |
if(ret < 0 || (size_t)ret >= sizeof(dom)) { |
| 130 |
ret = krb5_enomem(context); |
| 131 |
goto out; |
| 132 |
} |
| 133 |
r = rk_dns_lookup(dom, "TXT"); |
| 134 |
if(r != NULL) { |
| 135 |
ret = copy_txt_to_realms(context, domain, r->head, realms); |
| 136 |
rk_dns_free_data(r); |
| 137 |
if(ret == 0) |
| 138 |
goto out; |
| 139 |
} |
| 140 |
} |
| 141 |
krb5_set_error_message(context, KRB5_KDC_UNREACH, |
| 142 |
"Realm lookup failed: " |
| 143 |
"No DNS TXT record for %s", |
| 144 |
domain); |
| 145 |
ret = KRB5_KDC_UNREACH; |
| 146 |
out: |
| 147 |
if (config_labels) |
| 148 |
krb5_config_free_strings(config_labels); |
| 149 |
return ret; |
| 150 |
} |
| 151 |
|
| 152 |
/* |
| 153 |
* Try to figure out what realms host in `domain' belong to from the |
| 154 |
* configuration file. |
| 155 |
*/ |
| 156 |
|
| 157 |
static int |
| 158 |
config_find_realm(krb5_context context, |
| 159 |
const char *domain, |
| 160 |
krb5_realm **realms) |
| 161 |
{ |
| 162 |
char **tmp = krb5_config_get_strings (context, NULL, |
| 163 |
"domain_realm", |
| 164 |
domain, |
| 165 |
NULL); |
| 166 |
|
| 167 |
if (tmp == NULL) |
| 168 |
return -1; |
| 169 |
*realms = tmp; |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
/* |
| 174 |
* This function assumes that `host' is a FQDN (and doesn't handle the |
| 175 |
* special case of host == NULL either). |
| 176 |
* Try to find mapping in the config file or DNS and it that fails, |
| 177 |
* fall back to guessing |
| 178 |
*/ |
| 179 |
|
| 180 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 181 |
_krb5_get_host_realm_int(krb5_context context, |
| 182 |
const char *host, |
| 183 |
krb5_boolean use_dns, |
| 184 |
krb5_realm **realms) |
| 185 |
{ |
| 186 |
const char *p, *q; |
| 187 |
const char *port; |
| 188 |
krb5_boolean dns_locate_enable; |
| 189 |
krb5_error_code ret = 0; |
| 190 |
|
| 191 |
/* Strip off any trailing ":port" suffix. */ |
| 192 |
port = strchr(host, ':'); |
| 193 |
if (port != NULL) { |
| 194 |
host = strndup(host, port - host); |
| 195 |
if (host == NULL) |
| 196 |
return krb5_enomem(context); |
| 197 |
} |
| 198 |
|
| 199 |
dns_locate_enable = krb5_config_get_bool_default(context, NULL, TRUE, |
| 200 |
"libdefaults", "dns_lookup_realm", NULL); |
| 201 |
for (p = host; p != NULL; p = strchr (p + 1, '.')) { |
| 202 |
if (config_find_realm(context, p, realms) == 0) { |
| 203 |
if (strcasecmp(*realms[0], "dns_locate") != 0) |
| 204 |
break; |
| 205 |
krb5_free_host_realm(context, *realms); |
| 206 |
*realms = NULL; |
| 207 |
if (!use_dns) |
| 208 |
continue; |
| 209 |
for (q = host; q != NULL; q = strchr(q + 1, '.')) |
| 210 |
if (dns_find_realm(context, q, realms) == 0) |
| 211 |
break; |
| 212 |
if (q) |
| 213 |
break; |
| 214 |
} else if (use_dns && dns_locate_enable) { |
| 215 |
if (dns_find_realm(context, p, realms) == 0) |
| 216 |
break; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
* If 'p' is NULL, we did not find an explicit realm mapping in either the |
| 222 |
* configuration file or DNS. Try the hostname suffix as a last resort. |
| 223 |
* |
| 224 |
* XXX: If we implement a KDC-specific variant of this function just for |
| 225 |
* referrals, we could check whether we have a cross-realm TGT for the |
| 226 |
* realm in question, and if not try the parent (loop again). |
| 227 |
*/ |
| 228 |
if (p == NULL) { |
| 229 |
p = strchr(host, '.'); |
| 230 |
if (p != NULL) { |
| 231 |
p++; |
| 232 |
*realms = malloc(2 * sizeof(krb5_realm)); |
| 233 |
if (*realms != NULL && |
| 234 |
((*realms)[0] = strdup(p)) != NULL) { |
| 235 |
strupr((*realms)[0]); |
| 236 |
(*realms)[1] = NULL; |
| 237 |
} else { |
| 238 |
free(*realms); |
| 239 |
ret = krb5_enomem(context); |
| 240 |
} |
| 241 |
} else { |
| 242 |
krb5_set_error_message(context, KRB5_ERR_HOST_REALM_UNKNOWN, |
| 243 |
N_("unable to find realm of host %s", ""), |
| 244 |
host); |
| 245 |
ret = KRB5_ERR_HOST_REALM_UNKNOWN; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/* If 'port' is not NULL, we have a copy of 'host' to free. */ |
| 250 |
if (port) |
| 251 |
free((void *)host); |
| 252 |
return ret; |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
* Return the realm(s) of `host' as a NULL-terminated list in |
| 257 |
* `realms'. Free `realms' with krb5_free_host_realm(). |
| 258 |
*/ |
| 259 |
|
| 260 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 261 |
krb5_get_host_realm(krb5_context context, |
| 262 |
const char *targethost, |
| 263 |
krb5_realm **realms) |
| 264 |
{ |
| 265 |
const char *host = targethost; |
| •266 |
char hostname[MaxHostNameLen]; |
| 267 |
krb5_error_code ret; |
| 268 |
int use_dns; |
| 269 |
|
| 270 |
if (host == NULL) { |
| 271 |
if (gethostname (hostname, sizeof(hostname))) { |
| 272 |
*realms = NULL; |
| 273 |
return errno; |
| 274 |
} |
| 275 |
host = hostname; |
| 276 |
} |
| 277 |
|
| 278 |
/* |
| 279 |
* If our local hostname is without components, don't even try to dns. |
| 280 |
*/ |
| 281 |
|
| 282 |
use_dns = (strchr(host, '.') != NULL); |
| 283 |
|
| 284 |
ret = _krb5_get_host_realm_int (context, host, use_dns, realms); |
| 285 |
if (ret && targethost != NULL) { |
| 286 |
/* |
| 287 |
* If there was no realm mapping for the host (and we wasn't |
| 288 |
* looking for ourself), guess at the local realm, maybe our |
| 289 |
* KDC knows better then we do and we get a referral back. |
| 290 |
*/ |
| 291 |
ret = krb5_get_default_realms(context, realms); |
| 292 |
if (ret) { |
| 293 |
krb5_set_error_message(context, KRB5_ERR_HOST_REALM_UNKNOWN, |
| 294 |
N_("Unable to find realm of host %s", ""), |
| 295 |
host); |
| 296 |
return KRB5_ERR_HOST_REALM_UNKNOWN; |
| 297 |
} |
| 298 |
} |
| 299 |
return ret; |
| 300 |
} |