get_host_realm.c revision 178825
154359Sroberto/*
2182007Sroberto * Copyright (c) 1997 - 2005 Kungliga Tekniska H�gskolan
3182007Sroberto * (Royal Institute of Technology, Stockholm, Sweden).
454359Sroberto * All rights reserved.
5280849Scy *
654359Sroberto * Redistribution and use in source and binary forms, with or without
754359Sroberto * modification, are permitted provided that the following conditions
854359Sroberto * are met:
954359Sroberto *
1054359Sroberto * 1. Redistributions of source code must retain the above copyright
1154359Sroberto *    notice, this list of conditions and the following disclaimer.
1254359Sroberto *
1354359Sroberto * 2. Redistributions in binary form must reproduce the above copyright
1454359Sroberto *    notice, this list of conditions and the following disclaimer in the
1554359Sroberto *    documentation and/or other materials provided with the distribution.
1654359Sroberto *
1754359Sroberto * 3. Neither the name of the Institute nor the names of its contributors
1854359Sroberto *    may be used to endorse or promote products derived from this software
1954359Sroberto *    without specific prior written permission.
2054359Sroberto *
2154359Sroberto * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2254359Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2354359Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2454359Sroberto * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2554359Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2654359Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2754359Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2854359Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2954359Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3054359Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3154359Sroberto * SUCH DAMAGE.
3254359Sroberto */
3354359Sroberto
3454359Sroberto#include "krb5_locl.h"
3554359Sroberto#include <resolve.h>
3654359Sroberto
3754359SrobertoRCSID("$Id: get_host_realm.c 18541 2006-10-17 19:28:36Z lha $");
3854359Sroberto
3954359Sroberto/* To automagically find the correct realm of a host (without
4054359Sroberto * [domain_realm] in krb5.conf) add a text record for your domain with
4154359Sroberto * the name of your realm, like this:
4254359Sroberto *
4354359Sroberto * _kerberos	IN	TXT	"FOO.SE"
4454359Sroberto *
4554359Sroberto * The search is recursive, so you can add entries for specific
4654359Sroberto * hosts. To find the realm of host a.b.c, it first tries
4754359Sroberto * _kerberos.a.b.c, then _kerberos.b.c and so on.
4854359Sroberto *
4954359Sroberto * This method is described in draft-ietf-cat-krb-dns-locate-03.txt.
5054359Sroberto *
51280849Scy */
5254359Sroberto
5354359Srobertostatic int
5454359Srobertocopy_txt_to_realms (struct resource_record *head,
5554359Sroberto		    krb5_realm **realms)
5654359Sroberto{
5754359Sroberto    struct resource_record *rr;
5854359Sroberto    int n, i;
5954359Sroberto
6054359Sroberto    for(n = 0, rr = head; rr; rr = rr->next)
6154359Sroberto	if (rr->type == T_TXT)
6254359Sroberto	    ++n;
6354359Sroberto
6454359Sroberto    if (n == 0)
6554359Sroberto	return -1;
6654359Sroberto
6754359Sroberto    *realms = malloc ((n + 1) * sizeof(krb5_realm));
6854359Sroberto    if (*realms == NULL)
6954359Sroberto	return -1;
7054359Sroberto
7154359Sroberto    for (i = 0; i < n + 1; ++i)
7254359Sroberto	(*realms)[i] = NULL;
7354359Sroberto
7454359Sroberto    for (i = 0, rr = head; rr; rr = rr->next) {
7554359Sroberto	if (rr->type == T_TXT) {
7654359Sroberto	    char *tmp;
7754359Sroberto
7854359Sroberto	    tmp = strdup(rr->u.txt);
7954359Sroberto	    if (tmp == NULL) {
8054359Sroberto		for (i = 0; i < n; ++i)
8154359Sroberto		    free ((*realms)[i]);
8254359Sroberto		free (*realms);
8354359Sroberto		return -1;
8454359Sroberto	    }
8554359Sroberto	    (*realms)[i] = tmp;
8654359Sroberto	    ++i;
8754359Sroberto	}
8854359Sroberto    }
8954359Sroberto    return 0;
9054359Sroberto}
9154359Sroberto
9254359Srobertostatic int
93280849Scydns_find_realm(krb5_context context,
9454359Sroberto	       const char *domain,
95	       krb5_realm **realms)
96{
97    static const char *default_labels[] = { "_kerberos", NULL };
98    char dom[MAXHOSTNAMELEN];
99    struct dns_reply *r;
100    const char **labels;
101    char **config_labels;
102    int i, ret;
103
104    config_labels = krb5_config_get_strings(context, NULL, "libdefaults",
105					    "dns_lookup_realm_labels", NULL);
106    if(config_labels != NULL)
107	labels = (const char **)config_labels;
108    else
109	labels = default_labels;
110    if(*domain == '.')
111	domain++;
112    for (i = 0; labels[i] != NULL; i++) {
113	ret = snprintf(dom, sizeof(dom), "%s.%s.", labels[i], domain);
114	if(ret < 0 || ret >= sizeof(dom)) {
115	    if (config_labels)
116		krb5_config_free_strings(config_labels);
117	    return -1;
118	}
119    	r = dns_lookup(dom, "TXT");
120    	if(r != NULL) {
121	    ret = copy_txt_to_realms (r->head, realms);
122	    dns_free_data(r);
123	    if(ret == 0) {
124		if (config_labels)
125		    krb5_config_free_strings(config_labels);
126		return 0;
127	    }
128	}
129    }
130    if (config_labels)
131	krb5_config_free_strings(config_labels);
132    return -1;
133}
134
135/*
136 * Try to figure out what realms host in `domain' belong to from the
137 * configuration file.
138 */
139
140static int
141config_find_realm(krb5_context context,
142		  const char *domain,
143		  krb5_realm **realms)
144{
145    char **tmp = krb5_config_get_strings (context, NULL,
146					  "domain_realm",
147					  domain,
148					  NULL);
149
150    if (tmp == NULL)
151	return -1;
152    *realms = tmp;
153    return 0;
154}
155
156/*
157 * This function assumes that `host' is a FQDN (and doesn't handle the
158 * special case of host == NULL either).
159 * Try to find mapping in the config file or DNS and it that fails,
160 * fall back to guessing
161 */
162
163krb5_error_code KRB5_LIB_FUNCTION
164_krb5_get_host_realm_int (krb5_context context,
165			  const char *host,
166			  krb5_boolean use_dns,
167			  krb5_realm **realms)
168{
169    const char *p, *q;
170    krb5_boolean dns_locate_enable;
171
172    dns_locate_enable = krb5_config_get_bool_default(context, NULL, TRUE,
173	"libdefaults", "dns_lookup_realm", NULL);
174    for (p = host; p != NULL; p = strchr (p + 1, '.')) {
175	if(config_find_realm(context, p, realms) == 0) {
176	    if(strcasecmp(*realms[0], "dns_locate") == 0) {
177		if(use_dns)
178		    for (q = host; q != NULL; q = strchr(q + 1, '.'))
179			if(dns_find_realm(context, q, realms) == 0)
180			    return 0;
181		continue;
182	    } else
183	    	return 0;
184	}
185	else if(use_dns && dns_locate_enable) {
186	    if(dns_find_realm(context, p, realms) == 0)
187		return 0;
188	}
189    }
190    p = strchr(host, '.');
191    if(p != NULL) {
192	p++;
193	*realms = malloc(2 * sizeof(krb5_realm));
194	if (*realms == NULL) {
195	    krb5_set_error_string(context, "malloc: out of memory");
196	    return ENOMEM;
197	}
198
199	(*realms)[0] = strdup(p);
200	if((*realms)[0] == NULL) {
201	    free(*realms);
202	    krb5_set_error_string(context, "malloc: out of memory");
203	    return ENOMEM;
204	}
205	strupr((*realms)[0]);
206	(*realms)[1] = NULL;
207	return 0;
208    }
209    krb5_set_error_string(context, "unable to find realm of host %s", host);
210    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
218krb5_error_code KRB5_LIB_FUNCTION
219krb5_get_host_realm(krb5_context context,
220		    const char *targethost,
221		    krb5_realm **realms)
222{
223    const char *host = targethost;
224    char hostname[MAXHOSTNAMELEN];
225    krb5_error_code ret;
226    int use_dns;
227
228    if (host == NULL) {
229	if (gethostname (hostname, sizeof(hostname))) {
230	    *realms = NULL;
231	    return errno;
232	}
233	host = hostname;
234    }
235
236    /*
237     * If our local hostname is without components, don't even try to dns.
238     */
239
240    use_dns = (strchr(host, '.') != NULL);
241
242    ret = _krb5_get_host_realm_int (context, host, use_dns, realms);
243    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	ret = krb5_get_default_realms(context, realms);
250	if (ret) {
251	    krb5_set_error_string(context, "Unable to find realm of host %s",
252				  host);
253	    return KRB5_ERR_HOST_REALM_UNKNOWN;
254	}
255    }
256    return ret;
257}
258