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