No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / get_host_realm.c
blob767d1d2fcfd21adf8ff859da67a95cd3ae57c67f
1 /*
2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "krb5_locl.h"
35 #include <resolve.h>
37 __RCSID("$Heimdal: get_host_realm.c 18541 2006-10-17 19:28:36Z lha $"
38 "$NetBSD$");
40 /* To automagically find the correct realm of a host (without
41 * [domain_realm] in krb5.conf) add a text record for your domain with
42 * the name of your realm, like this:
44 * _kerberos IN TXT "FOO.SE"
46 * The search is recursive, so you can add entries for specific
47 * hosts. To find the realm of host a.b.c, it first tries
48 * _kerberos.a.b.c, then _kerberos.b.c and so on.
50 * This method is described in draft-ietf-cat-krb-dns-locate-03.txt.
54 static int
55 copy_txt_to_realms (struct resource_record *head,
56 krb5_realm **realms)
58 struct resource_record *rr;
59 int n, i;
61 for(n = 0, rr = head; rr; rr = rr->next)
62 if (rr->type == T_TXT)
63 ++n;
65 if (n == 0)
66 return -1;
68 *realms = malloc ((n + 1) * sizeof(krb5_realm));
69 if (*realms == NULL)
70 return -1;
72 for (i = 0; i < n + 1; ++i)
73 (*realms)[i] = NULL;
75 for (i = 0, rr = head; rr; rr = rr->next) {
76 if (rr->type == T_TXT) {
77 char *tmp;
79 tmp = strdup(rr->u.txt);
80 if (tmp == NULL) {
81 for (i = 0; i < n; ++i)
82 free ((*realms)[i]);
83 free (*realms);
84 return -1;
86 (*realms)[i] = tmp;
87 ++i;
90 return 0;
93 static int
94 dns_find_realm(krb5_context context,
95 const char *domain,
96 krb5_realm **realms)
98 static const char *default_labels[] = { "_kerberos", NULL };
99 char dom[MAXHOSTNAMELEN];
100 struct dns_reply *r;
101 const char **labels;
102 char **config_labels;
103 int i, ret;
105 config_labels = krb5_config_get_strings(context, NULL, "libdefaults",
106 "dns_lookup_realm_labels", NULL);
107 if(config_labels != NULL)
108 labels = (const char **)config_labels;
109 else
110 labels = default_labels;
111 if(*domain == '.')
112 domain++;
113 for (i = 0; labels[i] != NULL; i++) {
114 ret = snprintf(dom, sizeof(dom), "%s.%s.", labels[i], domain);
115 if(ret < 0 || ret >= sizeof(dom)) {
116 if (config_labels)
117 krb5_config_free_strings(config_labels);
118 return -1;
120 r = dns_lookup(dom, "TXT");
121 if(r != NULL) {
122 ret = copy_txt_to_realms (r->head, realms);
123 dns_free_data(r);
124 if(ret == 0) {
125 if (config_labels)
126 krb5_config_free_strings(config_labels);
127 return 0;
131 if (config_labels)
132 krb5_config_free_strings(config_labels);
133 return -1;
137 * Try to figure out what realms host in `domain' belong to from the
138 * configuration file.
141 static int
142 config_find_realm(krb5_context context,
143 const char *domain,
144 krb5_realm **realms)
146 char **tmp = krb5_config_get_strings (context, NULL,
147 "domain_realm",
148 domain,
149 NULL);
151 if (tmp == NULL)
152 return -1;
153 *realms = tmp;
154 return 0;
158 * This function assumes that `host' is a FQDN (and doesn't handle the
159 * special case of host == NULL either).
160 * Try to find mapping in the config file or DNS and it that fails,
161 * fall back to guessing
164 krb5_error_code KRB5_LIB_FUNCTION
165 _krb5_get_host_realm_int (krb5_context context,
166 const char *host,
167 krb5_boolean use_dns,
168 krb5_realm **realms)
170 const char *p, *q;
171 krb5_boolean dns_locate_enable;
173 dns_locate_enable = krb5_config_get_bool_default(context, NULL, TRUE,
174 "libdefaults", "dns_lookup_realm", NULL);
175 for (p = host; p != NULL; p = strchr (p + 1, '.')) {
176 if(config_find_realm(context, p, realms) == 0) {
177 if(strcasecmp(*realms[0], "dns_locate") == 0) {
178 if(use_dns)
179 for (q = host; q != NULL; q = strchr(q + 1, '.'))
180 if(dns_find_realm(context, q, realms) == 0)
181 return 0;
182 continue;
183 } else
184 return 0;
186 else if(use_dns && dns_locate_enable) {
187 if(dns_find_realm(context, p, realms) == 0)
188 return 0;
191 p = strchr(host, '.');
192 if(p != NULL) {
193 p++;
194 *realms = malloc(2 * sizeof(krb5_realm));
195 if (*realms == NULL) {
196 krb5_set_error_string(context, "malloc: out of memory");
197 return ENOMEM;
200 (*realms)[0] = strdup(p);
201 if((*realms)[0] == NULL) {
202 free(*realms);
203 krb5_set_error_string(context, "malloc: out of memory");
204 return ENOMEM;
206 strupr((*realms)[0]);
207 (*realms)[1] = NULL;
208 return 0;
210 krb5_set_error_string(context, "unable to find realm of host %s", host);
211 return KRB5_ERR_HOST_REALM_UNKNOWN;
215 * Return the realm(s) of `host' as a NULL-terminated list in
216 * `realms'. Free `realms' with krb5_free_host_realm().
219 krb5_error_code KRB5_LIB_FUNCTION
220 krb5_get_host_realm(krb5_context context,
221 const char *targethost,
222 krb5_realm **realms)
224 const char *host = targethost;
225 char hostname[MAXHOSTNAMELEN];
226 krb5_error_code ret;
227 int use_dns;
229 if (host == NULL) {
230 if (gethostname (hostname, sizeof(hostname))) {
231 *realms = NULL;
232 return errno;
234 host = hostname;
238 * If our local hostname is without components, don't even try to dns.
241 use_dns = (strchr(host, '.') != NULL);
243 ret = _krb5_get_host_realm_int (context, host, use_dns, realms);
244 if (ret && targethost != NULL) {
246 * If there was no realm mapping for the host (and we wasn't
247 * looking for ourself), guess at the local realm, maybe our
248 * KDC knows better then we do and we get a referral back.
250 ret = krb5_get_default_realms(context, realms);
251 if (ret) {
252 krb5_set_error_string(context, "Unable to find realm of host %s",
253 host);
254 return KRB5_ERR_HOST_REALM_UNKNOWN;
257 return ret;