1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/os/hst_realm.c */
4 * Copyright 1990,1991,2002,2008,2009 by the Massachusetts Institute of
5 * Technology. All Rights Reserved.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
27 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
32 * krb5_get_host_realm()
33 * krb5_get_fallback_host_realm()
34 * krb5int_clean_hostname()
35 * krb5_free_host_realm()
40 Figures out the Kerberos realm names for host, filling in a
41 pointer to an argv[] style list of names, terminated with a null pointer.
43 If host is NULL, the local host's realms are determined.
45 If there are no known realms for the host, the filled-in pointer is set
48 The pointer array and strings pointed to are all in allocated storage,
49 and should be freed by the caller when finished.
55 * Implementation notes:
57 * this implementation only provides one realm per host, using the same
58 * mapping file used in kerberos v4.
60 * Given a fully-qualified domain-style primary host name,
61 * return the name of the Kerberos realm for the host.
62 * If the hostname contains no discernable domain, or an error occurs,
63 * return the local realm name, as supplied by krb5_get_default_realm().
64 * If the hostname contains a domain, but no translation is found,
65 * the hostname's domain is converted to upper-case and returned.
67 * The format of each line of the translation file is:
68 * domain_name kerberos_realm
70 * host_name kerberos_realm
72 * domain_name should be of the form .XXX.YYY (e.g. .LCS.MIT.EDU)
73 * host names should be in the usual form (e.g. FOO.BAR.BAZ)
87 #include "fake-addrinfo.h"
89 static krb5_error_code
90 domain_heuristic(krb5_context context, const char *domain,
91 char **realm, int limit);
93 #ifdef KRB5_DNS_LOOKUP
97 #define MAXDNAME (16 * MAXHOSTNAMELEN)
99 #endif /* KRB5_DNS_LOOKUP */
101 krb5_error_code krb5int_translate_gai_error (int);
103 static krb5_error_code
104 get_fq_hostname(char *buf, size_t bufsize, const char *name)
106 struct addrinfo *ai, hints;
109 memset (&hints, 0, sizeof (hints));
110 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
111 err = getaddrinfo (name, 0, &hints, &ai);
113 return krb5int_translate_gai_error (err);
114 if (ai->ai_canonname == 0)
115 return KRB5_EAI_FAIL;
116 strncpy (buf, ai->ai_canonname, bufsize);
122 /* Get the local host name, try to make it fully-qualified.
123 Always return a null-terminated string.
124 Might return an error if gethostname fails. */
126 krb5int_get_fq_local_hostname(char *buf, size_t bufsiz)
129 if (gethostname (buf, bufsiz) == -1)
132 return get_fq_hostname (buf, bufsiz, buf);
135 krb5_error_code KRB5_CALLCONV
136 krb5_get_host_realm(krb5_context context, const char *host, char ***realmsp)
139 char *realm, *cp, *temp_realm;
140 krb5_error_code retval;
141 char local_host[MAXDNAME+1];
143 TRACE_GET_HOST_REALM(context, host);
145 retval = krb5int_clean_hostname(context, host, local_host, sizeof local_host);
150 Search for the best match for the host or domain.
151 Example: Given a host a.b.c.d, try to match on:
162 TRACE_GET_HOST_REALM_LOCALHOST(context, local_host);
163 realm = (char *)NULL;
166 TRACE_GET_HOST_REALM_DOMAIN_REALM_MAP(context, cp);
167 retval = profile_get_string(context->profile, KRB5_CONF_DOMAIN_REALM, cp,
168 0, (char *)NULL, &temp_realm);
171 if (temp_realm != (char *)NULL)
172 break; /* Match found */
174 /* Setup for another test */
178 cp = strchr(cp, '.');
182 TRACE_GET_HOST_REALM_TEMP_REALM(context, temp_realm);
183 realm = strdup(temp_realm);
185 profile_release_string(temp_realm);
188 profile_release_string(temp_realm);
191 if (realm == (char *)NULL) {
192 if (!(cp = strdup(KRB5_REFERRAL_REALM)))
197 if (!(retrealms = (char **)calloc(2, sizeof(*retrealms)))) {
198 if (realm != (char *)NULL)
203 retrealms[0] = realm;
206 TRACE_GET_HOST_REALM_RETURN(context, host, realm);
207 *realmsp = retrealms;
211 #if defined(_WIN32) && !defined(__CYGWIN32__)
212 # ifndef EAFNOSUPPORT
213 # define EAFNOSUPPORT WSAEAFNOSUPPORT
218 krb5int_translate_gai_error (int num)
221 #ifdef EAI_ADDRFAMILY
230 return KRB5_EAI_FAIL;
235 #if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME
237 return KRB5_EAI_NODATA;
240 return KRB5_EAI_NONAME;
241 #if defined(EAI_OVERFLOW)
243 return EINVAL; /* XXX */
246 return KRB5_EAI_SERVICE;
260 * Ganked from krb5_get_host_realm; handles determining a fallback realm
261 * to try in the case where referrals have failed and it's time to go
262 * look at TXT records or make a DNS-based assumption.
265 krb5_error_code KRB5_CALLCONV
266 krb5_get_fallback_host_realm(krb5_context context,
267 krb5_data *hdata, char ***realmsp)
271 krb5_error_code retval;
272 char local_host[MAXDNAME+1], host[MAXDNAME+1];
274 /* Convert what we hope is a hostname to a string. */
275 memcpy(host, hdata->data, hdata->length);
276 host[hdata->length]=0;
278 TRACE_GET_FALLBACK_HOST_REALM(context, host);
280 retval = krb5int_clean_hostname(context, host, local_host, sizeof local_host);
285 * Try looking up a _kerberos.<hostname> TXT record in DNS. This
286 * heuristic is turned off by default since, in the absence of
287 * secure DNS, it can allow an attacker to control the realm used
290 realm = (char *)NULL;
291 #ifdef KRB5_DNS_LOOKUP
292 if (_krb5_use_dns_realm(context)) {
295 retval = krb5_try_realm_txt_rr("_kerberos", cp, &realm);
299 } while (retval && cp && cp[0]);
301 #endif /* KRB5_DNS_LOOKUP */
304 * Next try searching the domain components as realms. This
305 * heuristic is also turned off by default. If DNS lookups for
306 * KDCs are enabled (as they are by default), an attacker could
307 * control which domain component is used as the realm for a host.
309 if (realm == (char *)NULL) {
313 code = profile_get_integer(context->profile, KRB5_CONF_LIBDEFAULTS,
314 KRB5_CONF_REALM_TRY_DOMAINS, 0, -1, &limit);
316 retval = domain_heuristic(context, local_host, &realm, limit);
323 * The next fallback--and the first one to apply with default
324 * configuration--is to use the upper-cased parent domain of the
325 * hostname, regardless of whether we can actually look it up as a
328 if (realm == (char *)NULL) {
329 cp = strchr(local_host, '.');
331 if (!(realm = strdup(cp + 1)))
333 for (cp = realm; *cp; cp++)
334 if (islower((int) (*cp)))
335 *cp = toupper((int) *cp);
340 * The final fallback--used when the fully-qualified hostname has
341 * only one component--is to use the local default realm.
343 if (realm == (char *)NULL) {
344 retval = krb5_get_default_realm(context, &realm);
349 if (!(retrealms = (char **)calloc(2, sizeof(*retrealms)))) {
350 if (realm != (char *)NULL)
355 retrealms[0] = realm;
358 TRACE_GET_FALLBACK_HOST_REALM_RETURN(context, host, realm);
359 *realmsp = retrealms;
364 * Common code for krb5_get_host_realm and krb5_get_fallback_host_realm
365 * to do basic sanity checks on supplied hostname.
368 krb5int_clean_hostname(krb5_context context,
369 const char *host, char *local_host, size_t lhsize)
372 krb5_error_code retval;
376 TRACE_CLEAN_HOSTNAME(context, host, lhsize, local_host);
378 /* Filter out numeric addresses if the caller utterly failed to
379 convert them to names. */
380 /* IPv4 - dotted quads only */
381 if (strspn(host, "01234567890.") == strlen(host)) {
382 /* All numbers and dots... if it's three dots, it's an
383 IP address, and we reject it. But "12345" could be
384 a local hostname, couldn't it? We'll just assume
385 that a name with three dots is not meant to be an
386 all-numeric hostname three all-numeric domains down
387 from the current domain. */
390 for (p = host; *p; p++)
394 return KRB5_ERR_NUMERIC_REALM;
396 if (strchr(host, ':'))
397 /* IPv6 numeric address form? Bye bye. */
398 return KRB5_ERR_NUMERIC_REALM;
400 /* Should probably error out if strlen(host) > MAXDNAME. */
401 strncpy(local_host, host, lhsize);
402 local_host[lhsize - 1] = '\0';
404 retval = krb5int_get_fq_local_hostname (local_host, lhsize);
409 /* fold to lowercase */
410 for (cp = local_host; *cp; cp++) {
411 if (isupper((unsigned char) (*cp)))
412 *cp = tolower((unsigned char) *cp);
414 l = strlen(local_host);
415 /* strip off trailing dot */
416 if (l && local_host[l-1] == '.')
419 TRACE_CLEAN_HOSTNAME_RETURN(context, host, lhsize, local_host);
424 * Walk through the components of a domain. At each stage determine
425 * if a KDC can be located for that domain. Return a realm
426 * corresponding to the upper-cased domain name for which a KDC was
427 * found or NULL if no KDC was found. Stop searching after limit
428 * labels have been removed from the domain (-1 means don't search at
429 * all, 0 means try only the full domain itself, 1 means also try the
430 * parent domain, etc.) or when we reach a parent with only one label.
432 static krb5_error_code
433 domain_heuristic(krb5_context context, const char *domain,
434 char **realm, int limit)
436 krb5_error_code retval = 0, r;
437 struct serverlist slist;
439 char *cp = NULL, *fqdn, *dot;
445 memset(&drealm, 0, sizeof (drealm));
446 fqdn = strdup(domain);
452 /* Upper case the domain (for use as a realm) */
453 for (cp = fqdn; *cp; cp++) {
454 if (islower((int)(*cp)))
455 *cp = toupper((int)*cp);
458 /* Search up to limit parents, as long as we have multiple labels. */
460 while (limit-- >= 0 && (dot = strchr(cp, '.')) != NULL) {
462 drealm.length = strlen(cp);
465 /* Find a kdc based on this part of the domain name. */
466 r = k5_locate_kdc(context, &drealm, &slist, FALSE, SOCK_DGRAM);
467 if (!r) { /* Found a KDC! */
468 k5_free_serverlist(&slist);
486 * Frees the storage taken by a realm list returned by krb5_get_host_realm.
489 krb5_error_code KRB5_CALLCONV
490 krb5_free_host_realm(krb5_context context, char *const *realmlist)
492 /* same format, so why duplicate code? */
493 return krb5_free_krbhst(context, realmlist);