LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/krb5 - get_host_realm.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 44 135 32.6 %
Date: 2024-04-21 15:09:00 Functions: 3 5 60.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1997 - 2005 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             : /* To automagically find the correct realm of a host (without
      38             :  * [domain_realm] in krb5.conf) add a text record for your domain with
      39             :  * the name of your realm, like this:
      40             :  *
      41             :  * _kerberos    IN      TXT     "FOO.SE"
      42             :  *
      43             :  * The search is recursive, so you can add entries for specific
      44             :  * hosts. To find the realm of host a.b.c, it first tries
      45             :  * _kerberos.a.b.c, then _kerberos.b.c and so on.
      46             :  *
      47             :  * This method is described in draft-ietf-cat-krb-dns-locate-03.txt.
      48             :  *
      49             :  */
      50             : 
      51             : static int
      52           0 : copy_txt_to_realms(krb5_context context,
      53             :                    const char *domain,
      54             :                    struct rk_resource_record *head,
      55             :                    krb5_realm **realms)
      56             : {
      57           0 :     struct rk_resource_record *rr;
      58           0 :     unsigned int n, i;
      59             : 
      60           0 :     for(n = 0, rr = head; rr; rr = rr->next)
      61           0 :         if (rr->type == rk_ns_t_txt)
      62           0 :             ++n;
      63             : 
      64           0 :     if (n == 0)
      65           0 :         return -1;
      66             : 
      67           0 :     *realms = malloc ((n + 1) * sizeof(krb5_realm));
      68           0 :     if (*realms == NULL)
      69           0 :         return krb5_enomem(context);;
      70             : 
      71           0 :     for (i = 0; i < n + 1; ++i)
      72           0 :         (*realms)[i] = NULL;
      73             : 
      74           0 :     for (i = 0, rr = head; rr; rr = rr->next) {
      75           0 :         if (rr->type == rk_ns_t_txt) {
      76           0 :             char *tmp = NULL;
      77           0 :             int invalid_tld = 1;
      78             : 
      79             :             /* Check for a gTLD controlled interruption */
      80           0 :             if (strcmp("Your DNS configuration needs immediate "
      81             :                         "attention see https://icann.org/namecollision",
      82           0 :                         rr->u.txt) != 0) {
      83           0 :                 invalid_tld = 0;
      84           0 :                 tmp = strdup(rr->u.txt);
      85             :             }
      86           0 :             if (tmp == NULL) {
      87           0 :                 for (i = 0; i < n; ++i)
      88           0 :                     free ((*realms)[i]);
      89           0 :                 free (*realms);
      90           0 :                 if (invalid_tld) {
      91           0 :                     krb5_warnx(context,
      92             :                                "Realm lookup failed: "
      93             :                                "Domain '%s' needs immediate attention "
      94             :                                "see https://icann.org/namecollision",
      95             :                                 domain);
      96           0 :                     return KRB5_KDC_UNREACH;
      97             :                 }
      98           0 :                 return krb5_enomem(context);;
      99             :             }
     100           0 :             (*realms)[i] = tmp;
     101           0 :             ++i;
     102             :         }
     103             :     }
     104           0 :     return 0;
     105             : }
     106             : 
     107             : static int
     108           0 : dns_find_realm(krb5_context context,
     109             :                const char *domain,
     110             :                krb5_realm **realms)
     111             : {
     112           0 :     static const char *const default_labels[] = { "_kerberos", NULL };
     113           0 :     char dom[MAXHOSTNAMELEN];
     114           0 :     struct rk_dns_reply *r;
     115           0 :     const char *const *labels;
     116           0 :     char **config_labels;
     117           0 :     int i, ret = 0;
     118             : 
     119           0 :     if (krb5_config_get_bool(context, NULL, "libdefaults", "block_dns",
     120             :             NULL)) {
     121           0 :         ret = KRB5_KDC_UNREACH;
     122           0 :         krb5_set_error_message(context, ret,
     123             :             "Realm lookup failed: DNS blocked");
     124           0 :         return ret;
     125             :     }
     126             : 
     127           0 :     config_labels = krb5_config_get_strings(context, NULL, "libdefaults",
     128             :                                             "dns_lookup_realm_labels", NULL);
     129           0 :     if(config_labels != NULL)
     130           0 :         labels = (const char *const *)config_labels;
     131             :     else
     132           0 :         labels = default_labels;
     133           0 :     if(*domain == '.')
     134           0 :         domain++;
     135           0 :     for (i = 0; labels[i] != NULL; i++) {
     136           0 :         ret = snprintf(dom, sizeof(dom), "%s.%s.", labels[i], domain);
     137           0 :         if(ret < 0 || (size_t)ret >= sizeof(dom)) {
     138           0 :             ret = krb5_enomem(context);
     139           0 :             goto out;
     140             :         }
     141           0 :         r = rk_dns_lookup(dom, "TXT");
     142           0 :         if(r != NULL) {
     143           0 :             ret = copy_txt_to_realms(context, domain, r->head, realms);
     144           0 :             rk_dns_free_data(r);
     145           0 :             if(ret == 0)
     146           0 :                 goto out;
     147             :         }
     148             :     }
     149           0 :     krb5_set_error_message(context, KRB5_KDC_UNREACH,
     150             :                             "Realm lookup failed: "
     151             :                             "No DNS TXT record for %s",
     152             :                             domain);
     153           0 :     ret = KRB5_KDC_UNREACH;
     154           0 : out:
     155           0 :     if (config_labels)
     156           0 :         krb5_config_free_strings(config_labels);
     157           0 :     return ret;
     158             : }
     159             : 
     160             : /*
     161             :  * Try to figure out what realms host in `domain' belong to from the
     162             :  * configuration file.
     163             :  */
     164             : 
     165             : static int
     166       39141 : config_find_realm(krb5_context context,
     167             :                   const char *domain,
     168             :                   krb5_realm **realms)
     169             : {
     170       40176 :     char **tmp = krb5_config_get_strings (context, NULL,
     171             :                                           "domain_realm",
     172             :                                           domain,
     173             :                                           NULL);
     174             : 
     175       39141 :     if (tmp == NULL)
     176       38106 :         return -1;
     177           0 :     *realms = tmp;
     178           0 :     return 0;
     179             : }
     180             : 
     181             : /*
     182             :  * This function assumes that `host' is a FQDN (and doesn't handle the
     183             :  * special case of host == NULL either).
     184             :  * Try to find mapping in the config file or DNS and it that fails,
     185             :  * fall back to guessing
     186             :  */
     187             : 
     188             : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
     189       25532 : _krb5_get_host_realm_int(krb5_context context,
     190             :                          const char *host,
     191             :                          krb5_boolean use_dns,
     192             :                          krb5_realm **realms)
     193             : {
     194        1035 :     const char *p, *q;
     195        1035 :     const char *port;
     196       25532 :     char *freeme = NULL;
     197        1035 :     krb5_boolean dns_locate_enable;
     198       25532 :     krb5_error_code ret = 0;
     199             : 
     200             :     /* Strip off any trailing ":port" suffix. */
     201       25532 :     port = strchr(host, ':');
     202       25532 :     if (port != NULL && port != host && port[1] != '\0') {
     203           0 :         host = freeme = strndup(host, port - host);
     204           0 :         if (host == NULL)
     205           0 :             return krb5_enomem(context);
     206             :     }
     207             : 
     208       25532 :     dns_locate_enable = krb5_config_get_bool_default(context, NULL, TRUE,
     209             :         "libdefaults", "dns_lookup_realm", NULL);
     210       65708 :     for (p = host; p != NULL && p[0] != '\0'; p = strchr (p + 1, '.')) {
     211       39141 :         if (config_find_realm(context, p, realms) == 0) {
     212           0 :             if (strcasecmp(*realms[0], "dns_locate") != 0)
     213           0 :                 break;
     214           0 :             krb5_free_host_realm(context, *realms);
     215           0 :             *realms = NULL;
     216           0 :             if (!use_dns)
     217           0 :                 continue;
     218           0 :             for (q = host; q != NULL; q = strchr(q + 1, '.'))
     219           0 :                 if (dns_find_realm(context, q, realms) == 0)
     220           0 :                     break;
     221           0 :             if (q)
     222           0 :                 break;
     223       39141 :         } else if (use_dns && dns_locate_enable) {
     224           0 :             if (dns_find_realm(context, p, realms) == 0)
     225           0 :                 break;
     226             :         }
     227             :     }
     228             : 
     229             :     /*
     230             :      * If 'p' is NULL, we did not find an explicit realm mapping in either the
     231             :      * configuration file or DNS.  Try the hostname suffix -upcased- as a realm
     232             :      * as a last resort.
     233             :      *
     234             :      * NOTE: If we implement a KDC-specific variant of this function just for
     235             :      *       referrals, we could check whether we have a cross-realm TGT for the
     236             :      *       realm in question, and if not try the parent (loop again).  Such a
     237             :      *       variant would have to have access to the HDB, naturally.
     238             :      *
     239             :      *       We should start by adding an argument to this function that
     240             :      *       indicates whether this fallback here is desired (the KDC wouldn't
     241             :      *       desire it).  Then when the KDC gets KRB5_ERR_HOST_REALM_UNKNOWN
     242             :      *       from this function, the KDC would search the HDB for cross-realm
     243             :      *       krbtgt principals that denote a hierarchical path to a realm that
     244             :      *       matches the host's domain suffix (or a suffix of it...).
     245             :      */
     246       25532 :     if (p == NULL) {
     247       25532 :         p = strchr(host, '.');
     248       25532 :         if (p != NULL) {
     249        4331 :             p++;
     250        4331 :             *realms = malloc(2 * sizeof(krb5_realm));
     251        4331 :             if (*realms != NULL &&
     252        4331 :                 ((*realms)[0] = strdup(p)) != NULL) {
     253        4331 :                 strupr((*realms)[0]);
     254        4331 :                 (*realms)[1] = NULL;
     255             :             } else {
     256           0 :                 free(*realms);
     257           0 :                 ret = krb5_enomem(context);
     258             :             }
     259             :         } else {
     260       21201 :             krb5_set_error_message(context, KRB5_ERR_HOST_REALM_UNKNOWN,
     261       21201 :                                    N_("unable to find realm of host %s", ""),
     262             :                                    host);
     263       21201 :             ret = KRB5_ERR_HOST_REALM_UNKNOWN;
     264             :         }
     265             :     }
     266             : 
     267       25532 :     free(freeme);
     268       25532 :     return ret;
     269             : }
     270             : 
     271             : /*
     272             :  * Return the realm(s) of `host' as a NULL-terminated list in
     273             :  * `realms'. Free `realms' with krb5_free_host_realm().
     274             :  */
     275             : 
     276             : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
     277       25532 : krb5_get_host_realm(krb5_context context,
     278             :                     const char *targethost,
     279             :                     krb5_realm **realms)
     280             : {
     281       25532 :     const char *host = targethost;
     282        1035 :     char hostname[MAXHOSTNAMELEN];
     283        1035 :     krb5_error_code ret;
     284        1035 :     int use_dns;
     285             : 
     286       25532 :     if (host == NULL) {
     287          10 :         if (gethostname (hostname, sizeof(hostname))) {
     288           0 :             *realms = NULL;
     289           0 :             return errno;
     290             :         }
     291          10 :         host = hostname;
     292             :     }
     293             : 
     294             :     /*
     295             :      * If our local hostname is without components, don't even try to dns.
     296             :      */
     297             : 
     298       25532 :     use_dns = (strchr(host, '.') != NULL);
     299             : 
     300       25532 :     ret = _krb5_get_host_realm_int (context, host, use_dns, realms);
     301       25532 :     if (ret && targethost != NULL) {
     302             :         /*
     303             :          * If there was no realm mapping for the host (and we wasn't
     304             :          * looking for ourself), guess at the local realm, maybe our
     305             :          * KDC knows better then we do and we get a referral back.
     306             :          */
     307       21201 :         ret = krb5_get_default_realms(context, realms);
     308       21201 :         if (ret) {
     309           0 :             krb5_set_error_message(context, KRB5_ERR_HOST_REALM_UNKNOWN,
     310           0 :                                    N_("Unable to find realm of host %s", ""),
     311             :                                    host);
     312           0 :             return KRB5_ERR_HOST_REALM_UNKNOWN;
     313             :         }
     314             :     }
     315       24497 :     return ret;
     316             : }

Generated by: LCOV version 1.14