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 (struct rk_resource_record *head,
53 : krb5_realm **realms)
54 : {
55 : struct rk_resource_record *rr;
56 : unsigned int n, i;
57 :
58 0 : for(n = 0, rr = head; rr; rr = rr->next)
59 0 : if (rr->type == rk_ns_t_txt)
60 0 : ++n;
61 :
62 0 : if (n == 0)
63 0 : return -1;
64 :
65 0 : *realms = malloc ((n + 1) * sizeof(krb5_realm));
66 0 : if (*realms == NULL)
67 0 : return -1;
68 :
69 0 : for (i = 0; i < n + 1; ++i)
70 0 : (*realms)[i] = NULL;
71 :
72 0 : for (i = 0, rr = head; rr; rr = rr->next) {
73 0 : if (rr->type == rk_ns_t_txt) {
74 : char *tmp;
75 :
76 0 : tmp = strdup(rr->u.txt);
77 0 : if (tmp == NULL) {
78 0 : for (i = 0; i < n; ++i)
79 0 : free ((*realms)[i]);
80 0 : free (*realms);
81 0 : return -1;
82 : }
83 0 : (*realms)[i] = tmp;
84 0 : ++i;
85 : }
86 : }
87 0 : return 0;
88 : }
89 :
90 : static int
91 0 : dns_find_realm(krb5_context context,
92 : const char *domain,
93 : krb5_realm **realms)
94 : {
95 : static const char *default_labels[] = { "_kerberos", NULL };
96 : char dom[MAXHOSTNAMELEN];
97 : struct rk_dns_reply *r;
98 : const char **labels;
99 : char **config_labels;
100 : int i, ret;
101 :
102 0 : config_labels = krb5_config_get_strings(context, NULL, "libdefaults",
103 : "dns_lookup_realm_labels", NULL);
104 0 : if(config_labels != NULL)
105 0 : labels = (const char **)config_labels;
106 : else
107 0 : labels = default_labels;
108 0 : if(*domain == '.')
109 0 : domain++;
110 0 : for (i = 0; labels[i] != NULL; i++) {
111 0 : ret = snprintf(dom, sizeof(dom), "%s.%s.", labels[i], domain);
112 0 : if(ret < 0 || (size_t)ret >= sizeof(dom)) {
113 0 : if (config_labels)
114 0 : krb5_config_free_strings(config_labels);
115 0 : return -1;
116 : }
117 0 : r = rk_dns_lookup(dom, "TXT");
118 0 : if(r != NULL) {
119 0 : ret = copy_txt_to_realms (r->head, realms);
120 0 : rk_dns_free_data(r);
121 0 : if(ret == 0) {
122 0 : if (config_labels)
123 0 : krb5_config_free_strings(config_labels);
124 0 : return 0;
125 : }
126 : }
127 : }
128 0 : if (config_labels)
129 0 : krb5_config_free_strings(config_labels);
130 0 : return -1;
131 : }
132 :
133 : /*
134 : * Try to figure out what realms host in `domain' belong to from the
135 : * configuration file.
136 : */
137 :
138 : static int
139 37087 : config_find_realm(krb5_context context,
140 : const char *domain,
141 : krb5_realm **realms)
142 : {
143 37837 : char **tmp = krb5_config_get_strings (context, NULL,
144 : "domain_realm",
145 : domain,
146 : NULL);
147 :
148 37837 : if (tmp == NULL)
149 37087 : return -1;
150 0 : *realms = tmp;
151 0 : return 0;
152 : }
153 :
154 : /*
155 : * This function assumes that `host' is a FQDN (and doesn't handle the
156 : * special case of host == NULL either).
157 : * Try to find mapping in the config file or DNS and it that fails,
158 : * fall back to guessing
159 : */
160 :
161 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
162 22491 : _krb5_get_host_realm_int (krb5_context context,
163 : const char *host,
164 : krb5_boolean use_dns,
165 : krb5_realm **realms)
166 : {
167 : const char *p, *q;
168 : krb5_boolean dns_locate_enable;
169 :
170 22491 : dns_locate_enable = krb5_config_get_bool_default(context, NULL, TRUE,
171 : "libdefaults", "dns_lookup_realm", NULL);
172 60328 : for (p = host; p != NULL; p = strchr (p + 1, '.')) {
173 37837 : if(config_find_realm(context, p, realms) == 0) {
174 0 : if(strcasecmp(*realms[0], "dns_locate") == 0) {
175 0 : if(use_dns)
176 0 : for (q = host; q != NULL; q = strchr(q + 1, '.'))
177 0 : if(dns_find_realm(context, q, realms) == 0)
178 0 : return 0;
179 0 : continue;
180 : } else
181 0 : return 0;
182 : }
183 37837 : else if(use_dns && dns_locate_enable) {
184 0 : if(dns_find_realm(context, p, realms) == 0)
185 0 : return 0;
186 : }
187 : }
188 22491 : p = strchr(host, '.');
189 22491 : if(p != NULL) {
190 4764 : p++;
191 4764 : *realms = malloc(2 * sizeof(krb5_realm));
192 4764 : if (*realms == NULL) {
193 0 : krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
194 0 : return ENOMEM;
195 : }
196 :
197 4764 : (*realms)[0] = strdup(p);
198 4764 : if((*realms)[0] == NULL) {
199 0 : free(*realms);
200 0 : krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
201 0 : return ENOMEM;
202 : }
203 4764 : strupr((*realms)[0]);
204 4764 : (*realms)[1] = NULL;
205 4764 : return 0;
206 : }
207 17727 : krb5_set_error_message(context, KRB5_ERR_HOST_REALM_UNKNOWN,
208 17727 : N_("unable to find realm of host %s", ""),
209 : host);
210 17727 : return KRB5_ERR_HOST_REALM_UNKNOWN;
211 : }
212 :
213 : /*
214 : * Return the realm(s) of `host' as a NULL-terminated list in
215 : * `realms'. Free `realms' with krb5_free_host_realm().
216 : */
217 :
218 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
219 22491 : krb5_get_host_realm(krb5_context context,
220 : const char *targethost,
221 : krb5_realm **realms)
222 : {
223 22491 : const char *host = targethost;
224 : char hostname[MAXHOSTNAMELEN];
225 : krb5_error_code ret;
226 : int use_dns;
227 :
228 22491 : if (host == NULL) {
229 12 : if (gethostname (hostname, sizeof(hostname))) {
230 0 : *realms = NULL;
231 0 : return errno;
232 : }
233 12 : host = hostname;
234 : }
235 :
236 : /*
237 : * If our local hostname is without components, don't even try to dns.
238 : */
239 :
240 22491 : use_dns = (strchr(host, '.') != NULL);
241 :
242 22491 : ret = _krb5_get_host_realm_int (context, host, use_dns, realms);
243 22491 : if (ret && targethost != NULL) {
244 : /*
245 : * If there was no realm mapping for the host (and we wasn't
246 : * looking for ourself), guess at the local realm, maybe our
247 : * KDC knows better then we do and we get a referral back.
248 : */
249 17727 : ret = krb5_get_default_realms(context, realms);
250 17727 : if (ret) {
251 0 : krb5_set_error_message(context, KRB5_ERR_HOST_REALM_UNKNOWN,
252 0 : N_("Unable to find realm of host %s", ""),
253 : host);
254 0 : return KRB5_ERR_HOST_REALM_UNKNOWN;
255 : }
256 : }
257 21741 : return ret;
258 : }
|