xref: /freebsd/crypto/heimdal/lib/krb5/get_host_realm.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * Copyright (c) 1997, 1998, 1999 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 RCSID("$Id: get_host_realm.c,v 1.25 1999/12/11 23:14:07 assar Exp $");
38 
39 /* To automagically find the correct realm of a host (without
40  * [domain_realm] in krb5.conf) add a text record for your domain with
41  * the name of your realm, like this:
42  *
43  * krb5-realm	IN	TXT	FOO.SE
44  *
45  * The search is recursive, so you can add entries for specific
46  * hosts. To find the realm of host a.b.c, it first tries
47  * krb5-realm.a.b.c, then krb5-realm.b.c and so on.
48  *
49  * Also supported is _kerberos (following draft-ietf-cat-krb-dns-locate-01.txt)
50  *
51  */
52 
53 static int
54 copy_txt_to_realms (struct resource_record *head,
55 		    krb5_realm **realms)
56 {
57     struct resource_record *rr;
58     int n, i;
59 
60     for(n = 0, rr = head; rr; rr = rr->next)
61 	if (rr->type == T_TXT)
62 	    ++n;
63 
64     if (n == 0)
65 	return -1;
66 
67     *realms = malloc ((n + 1) * sizeof(krb5_realm));
68     if (*realms == NULL)
69 	return -1;
70 
71     for (i = 0; i < n + 1; ++i)
72 	(*realms)[i] = NULL;
73 
74     for (i = 0, rr = head; rr; rr = rr->next) {
75 	if (rr->type == T_TXT) {
76 	    char *tmp;
77 
78 	    tmp = strdup(rr->u.txt);
79 	    if (tmp == NULL) {
80 		for (i = 0; i < n; ++i)
81 		    free ((*realms)[i]);
82 		free (*realms);
83 		return -1;
84 	    }
85 	    (*realms)[i] = tmp;
86 	    ++i;
87 	}
88     }
89     return 0;
90 }
91 
92 static int
93 dns_find_realm(krb5_context context,
94 	       const char *domain,
95 	       const char *dom_string,
96 	       krb5_realm **realms)
97 {
98     char dom[MAXHOSTNAMELEN];
99     struct dns_reply *r;
100     int ret;
101 
102     if(*domain == '.')
103 	domain++;
104     snprintf(dom, sizeof(dom), "%s.%s.", dom_string, domain);
105     r = dns_lookup(dom, "TXT");
106     if(r == NULL)
107 	return -1;
108 
109     ret = copy_txt_to_realms (r->head, realms);
110     dns_free_data(r);
111     return ret;
112 }
113 
114 /*
115  * Try to figure out what realms host in `domain' belong to from the
116  * configuration file.
117  */
118 
119 static int
120 config_find_realm(krb5_context context,
121 		  const char *domain,
122 		  krb5_realm **realms)
123 {
124     char **tmp = krb5_config_get_strings (context, NULL,
125 					  "domain_realm",
126 					  domain,
127 					  NULL);
128 
129     if (tmp == NULL)
130 	return -1;
131     *realms = tmp;
132     return 0;
133 }
134 
135 /*
136  * This function assumes that `host' is a FQDN (and doesn't handle the
137  * special case of host == NULL either).
138  * Try to find mapping in the config file or DNS and it that fails,
139  * fall back to guessing
140  */
141 
142 krb5_error_code
143 krb5_get_host_realm_int (krb5_context context,
144 			 const char *host,
145 			 krb5_realm **realms)
146 {
147     const char *p;
148 
149     for (p = host; p != NULL; p = strchr (p + 1, '.')) {
150 	if(config_find_realm(context, p, realms) == 0)
151 	    return 0;
152 	else if(dns_find_realm(context, p, "krb5-realm", realms) == 0)
153 	    return 0;
154 	else if(dns_find_realm(context, p, "_kerberos", realms) == 0)
155 	    return 0;
156     }
157     p = strchr(host, '.');
158     if(p != NULL) {
159 	p++;
160 	*realms = malloc(2 * sizeof(krb5_realm));
161 	if (*realms == NULL)
162 	    return ENOMEM;
163 
164 	(*realms)[0] = strdup(p);
165 	if((*realms)[0] == NULL) {
166 	    free(*realms);
167 	    return ENOMEM;
168 	}
169 	strupr((*realms)[0]);
170 	(*realms)[1] = NULL;
171 	return 0;
172     }
173     return KRB5_ERR_HOST_REALM_UNKNOWN;
174 }
175 
176 /*
177  * Return the realm(s) of `host' as a NULL-terminated list in `realms'.
178  */
179 
180 krb5_error_code
181 krb5_get_host_realm(krb5_context context,
182 		    const char *host,
183 		    krb5_realm **realms)
184 {
185     char hostname[MAXHOSTNAMELEN];
186 
187     if (host == NULL) {
188 	if (gethostname (hostname, sizeof(hostname)))
189 	    return errno;
190 	host = hostname;
191     }
192 
193     return krb5_get_host_realm_int (context, host, realms);
194 }
195