Line data Source code
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 0 : copy_txt_to_realms(krb5_context context,
53 : const char *domain,
54 : struct rk_resource_record *head,
55 : krb5_realm **realms)
56 : {
57 0 : struct rk_resource_record *rr;
58 0 : unsigned int n, i;
59 :
60 0 : for(n = 0, rr = head; rr; rr = rr->next)
61 0 : if (rr->type == rk_ns_t_txt)
62 0 : ++n;
63 :
64 0 : if (n == 0)
65 0 : return -1;
66 :
67 0 : *realms = malloc ((n + 1) * sizeof(krb5_realm));
68 0 : if (*realms == NULL)
69 0 : return krb5_enomem(context);;
70 :
71 0 : for (i = 0; i < n + 1; ++i)
72 0 : (*realms)[i] = NULL;
73 :
74 0 : for (i = 0, rr = head; rr; rr = rr->next) {
75 0 : if (rr->type == rk_ns_t_txt) {
76 0 : char *tmp = NULL;
77 0 : int invalid_tld = 1;
78 :
79 : /* Check for a gTLD controlled interruption */
80 0 : if (strcmp("Your DNS configuration needs immediate "
81 : "attention see https://icann.org/namecollision",
82 0 : rr->u.txt) != 0) {
83 0 : invalid_tld = 0;
84 0 : tmp = strdup(rr->u.txt);
85 : }
86 0 : if (tmp == NULL) {
87 0 : for (i = 0; i < n; ++i)
88 0 : free ((*realms)[i]);
89 0 : free (*realms);
90 0 : if (invalid_tld) {
91 0 : krb5_warnx(context,
92 : "Realm lookup failed: "
93 : "Domain '%s' needs immediate attention "
94 : "see https://icann.org/namecollision",
95 : domain);
96 0 : return KRB5_KDC_UNREACH;
97 : }
98 0 : return krb5_enomem(context);;
99 : }
100 0 : (*realms)[i] = tmp;
101 0 : ++i;
102 : }
103 : }
104 0 : return 0;
105 : }
106 :
107 : static int
108 0 : dns_find_realm(krb5_context context,
109 : const char *domain,
110 : krb5_realm **realms)
111 : {
112 0 : static const char *const default_labels[] = { "_kerberos", NULL };
113 0 : char dom[MAXHOSTNAMELEN];
114 0 : struct rk_dns_reply *r;
115 0 : const char *const *labels;
116 0 : char **config_labels;
117 0 : int i, ret = 0;
118 :
119 0 : config_labels = krb5_config_get_strings(context, NULL, "libdefaults",
120 : "dns_lookup_realm_labels", NULL);
121 0 : if(config_labels != NULL)
122 0 : labels = (const char *const *)config_labels;
123 : else
124 0 : labels = default_labels;
125 0 : if(*domain == '.')
126 0 : domain++;
127 0 : for (i = 0; labels[i] != NULL; i++) {
128 0 : ret = snprintf(dom, sizeof(dom), "%s.%s.", labels[i], domain);
129 0 : if(ret < 0 || (size_t)ret >= sizeof(dom)) {
130 0 : ret = krb5_enomem(context);
131 0 : goto out;
132 : }
133 0 : r = rk_dns_lookup(dom, "TXT");
134 0 : if(r != NULL) {
135 0 : ret = copy_txt_to_realms(context, domain, r->head, realms);
136 0 : rk_dns_free_data(r);
137 0 : if(ret == 0)
138 0 : goto out;
139 : }
140 : }
141 0 : krb5_set_error_message(context, KRB5_KDC_UNREACH,
142 : "Realm lookup failed: "
143 : "No DNS TXT record for %s",
144 : domain);
145 0 : ret = KRB5_KDC_UNREACH;
146 0 : out:
147 0 : if (config_labels)
148 0 : krb5_config_free_strings(config_labels);
149 0 : 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 39405 : config_find_realm(krb5_context context,
159 : const char *domain,
160 : krb5_realm **realms)
161 : {
162 40440 : char **tmp = krb5_config_get_strings (context, NULL,
163 : "domain_realm",
164 : domain,
165 : NULL);
166 :
167 39405 : if (tmp == NULL)
168 38370 : return -1;
169 0 : *realms = tmp;
170 0 : 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 25441 : _krb5_get_host_realm_int(krb5_context context,
182 : const char *host,
183 : krb5_boolean use_dns,
184 : krb5_realm **realms)
185 : {
186 1035 : const char *p, *q;
187 1035 : const char *port;
188 25441 : char *freeme = NULL;
189 1035 : krb5_boolean dns_locate_enable;
190 25441 : krb5_error_code ret = 0;
191 :
192 : /* Strip off any trailing ":port" suffix. */
193 25441 : port = strchr(host, ':');
194 25441 : if (port != NULL && port != host && port[1] != '\0') {
195 0 : host = freeme = strndup(host, port - host);
196 0 : if (host == NULL)
197 0 : return krb5_enomem(context);
198 : }
199 :
200 25441 : dns_locate_enable = krb5_config_get_bool_default(context, NULL, TRUE,
201 : "libdefaults", "dns_lookup_realm", NULL);
202 65881 : for (p = host; p != NULL && p[0] != '\0'; p = strchr (p + 1, '.')) {
203 39405 : if (config_find_realm(context, p, realms) == 0) {
204 0 : if (strcasecmp(*realms[0], "dns_locate") != 0)
205 0 : break;
206 0 : krb5_free_host_realm(context, *realms);
207 0 : *realms = NULL;
208 0 : if (!use_dns)
209 0 : continue;
210 0 : for (q = host; q != NULL; q = strchr(q + 1, '.'))
211 0 : if (dns_find_realm(context, q, realms) == 0)
212 0 : break;
213 0 : if (q)
214 0 : break;
215 39405 : } else if (use_dns && dns_locate_enable) {
216 0 : if (dns_find_realm(context, p, realms) == 0)
217 0 : break;
218 : }
219 : }
220 :
221 : /*
222 : * If 'p' is NULL, we did not find an explicit realm mapping in either the
223 : * configuration file or DNS. Try the hostname suffix -upcased- as a realm
224 : * as a last resort.
225 : *
226 : * NOTE: If we implement a KDC-specific variant of this function just for
227 : * referrals, we could check whether we have a cross-realm TGT for the
228 : * realm in question, and if not try the parent (loop again). Such a
229 : * variant would have to have access to the HDB, naturally.
230 : *
231 : * We should start by adding an argument to this function that
232 : * indicates whether this fallback here is desired (the KDC wouldn't
233 : * desire it). Then when the KDC gets KRB5_ERR_HOST_REALM_UNKNOWN
234 : * from this function, the KDC would search the HDB for cross-realm
235 : * krbtgt principals that denote a hierarchical path to a realm that
236 : * matches the host's domain suffix (or a suffix of it...).
237 : */
238 25441 : if (p == NULL) {
239 25441 : p = strchr(host, '.');
240 25441 : if (p != NULL) {
241 4420 : p++;
242 4420 : *realms = malloc(2 * sizeof(krb5_realm));
243 4420 : if (*realms != NULL &&
244 4420 : ((*realms)[0] = strdup(p)) != NULL) {
245 4420 : strupr((*realms)[0]);
246 4420 : (*realms)[1] = NULL;
247 : } else {
248 0 : free(*realms);
249 0 : ret = krb5_enomem(context);
250 : }
251 : } else {
252 21021 : krb5_set_error_message(context, KRB5_ERR_HOST_REALM_UNKNOWN,
253 21021 : N_("unable to find realm of host %s", ""),
254 : host);
255 21021 : ret = KRB5_ERR_HOST_REALM_UNKNOWN;
256 : }
257 : }
258 :
259 25441 : free(freeme);
260 25441 : return ret;
261 : }
262 :
263 : /*
264 : * Return the realm(s) of `host' as a NULL-terminated list in
265 : * `realms'. Free `realms' with krb5_free_host_realm().
266 : */
267 :
268 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
269 25441 : krb5_get_host_realm(krb5_context context,
270 : const char *targethost,
271 : krb5_realm **realms)
272 : {
273 25441 : const char *host = targethost;
274 1035 : char hostname[MAXHOSTNAMELEN];
275 1035 : krb5_error_code ret;
276 1035 : int use_dns;
277 :
278 25441 : if (host == NULL) {
279 10 : if (gethostname (hostname, sizeof(hostname))) {
280 0 : *realms = NULL;
281 0 : return errno;
282 : }
283 10 : host = hostname;
284 : }
285 :
286 : /*
287 : * If our local hostname is without components, don't even try to dns.
288 : */
289 :
290 25441 : use_dns = (strchr(host, '.') != NULL);
291 :
292 25441 : ret = _krb5_get_host_realm_int (context, host, use_dns, realms);
293 25441 : if (ret && targethost != NULL) {
294 : /*
295 : * If there was no realm mapping for the host (and we wasn't
296 : * looking for ourself), guess at the local realm, maybe our
297 : * KDC knows better then we do and we get a referral back.
298 : */
299 21021 : ret = krb5_get_default_realms(context, realms);
300 21021 : if (ret) {
301 0 : krb5_set_error_message(context, KRB5_ERR_HOST_REALM_UNKNOWN,
302 0 : N_("Unable to find realm of host %s", ""),
303 : host);
304 0 : return KRB5_ERR_HOST_REALM_UNKNOWN;
305 : }
306 : }
307 24406 : return ret;
308 : }
|