summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/os/hst_realm.c
blob: 3bcc7923ccb4cd4b43302f4ee7d6d2a322db2386 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* lib/krb5/os/hst_realm.c - Hostname to realm translation */
/*
 * Copyright 1990,1991,2002,2008,2009,2013 by the Massachusetts Institute of
 * Technology.  All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include "k5-int.h"
#include "os-proto.h"
#include <ctype.h>

#include "fake-addrinfo.h"

#ifdef KRB5_DNS_LOOKUP
#include "dnsglue.h"
#else
#ifndef MAXDNAME
#define MAXDNAME (16 * MAXHOSTNAMELEN)
#endif /* MAXDNAME */
#endif /* KRB5_DNS_LOOKUP */

#if defined(_WIN32) && !defined(__CYGWIN32__)
#ifndef EAFNOSUPPORT
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#endif
#endif

static krb5_error_code
krb5int_translate_gai_error(int num)
{
    switch (num) {
#ifdef EAI_ADDRFAMILY
    case EAI_ADDRFAMILY:
        return EAFNOSUPPORT;
#endif
    case EAI_AGAIN:
        return EAGAIN;
    case EAI_BADFLAGS:
        return EINVAL;
    case EAI_FAIL:
        return KRB5_EAI_FAIL;
    case EAI_FAMILY:
        return EAFNOSUPPORT;
    case EAI_MEMORY:
        return ENOMEM;
#if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME
    case EAI_NODATA:
        return KRB5_EAI_NODATA;
#endif
    case EAI_NONAME:
        return KRB5_EAI_NONAME;
#if defined(EAI_OVERFLOW)
    case EAI_OVERFLOW:
        return EINVAL;          /* XXX */
#endif
    case EAI_SERVICE:
        return KRB5_EAI_SERVICE;
    case EAI_SOCKTYPE:
        return EINVAL;
#ifdef EAI_SYSTEM
    case EAI_SYSTEM:
        return errno;
#endif
    }
    abort();
    return -1;
}

/* Get the canonical form of the local host name, using forward
 * canonicalization only. */
krb5_error_code
krb5int_get_fq_local_hostname(char *buf, size_t bufsize)
{
    struct addrinfo *ai, hints;
    int err;

    buf[0] = '\0';
    if (gethostname(buf, bufsize) == -1)
        return SOCKET_ERRNO;

    memset(&hints, 0, sizeof(hints));
    hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
    err = getaddrinfo(buf, NULL, &hints, &ai);
    if (err)
        return krb5int_translate_gai_error(err);
    if (ai->ai_canonname == NULL) {
        freeaddrinfo(ai);
        return KRB5_EAI_FAIL;
    }
    if (strlcpy(buf, ai->ai_canonname, bufsize) >= bufsize)
        return ENOMEM;
    freeaddrinfo(ai);
    return 0;
}

/* Return true if name appears to be an IPv4 or IPv6 address. */
static krb5_boolean
is_numeric_address(const char *name)
{
    int ndots = 0;
    const char *p;

    /* If name contains only numbers and three dots, consider it to be an IPv4
     * address. */
    if (strspn(name, "01234567890.") == strlen(name)) {
        for (p = name; *p; p++) {
            if (*p == '.')
                ndots++;
        }
        if (ndots == 3)
            return TRUE;
    }

    /* If name contains a colon, consider it to be an IPv6 address. */
    if (strchr(name, ':') != NULL)
        return TRUE;

    return FALSE;
}

/*
 * Search progressively shorter suffixes of host in the [domain_realms] section
 * of the profile to find the realm.  For example, given a host a.b.c, try to
 * match a.b.c, then .b.c, then b.c, then .c, then c.  If we don't find a
 * match, return success but set *realm_out to NULL.
 */
static krb5_error_code
search_domain_realm(krb5_context context, const char *host, char **realm_out)
{
    krb5_error_code ret;
    const char *p;
    char *prof_realm;

    *realm_out = NULL;
    for (p = host; p != NULL; p = (*p == '.') ? p + 1 : strchr(p, '.')) {
        ret = profile_get_string(context->profile, KRB5_CONF_DOMAIN_REALM, p,
                                 NULL, NULL, &prof_realm);
        if (ret)
            return ret;
        if (prof_realm != NULL) {
            *realm_out = strdup(prof_realm);
            profile_release_string(prof_realm);
            if (*realm_out == NULL)
                return ENOMEM;
            return 0;
        }
    }
    return 0;
}

krb5_error_code KRB5_CALLCONV
krb5_get_host_realm(krb5_context context, const char *host, char ***realmsp)
{
    char **retrealms, *realm = NULL;
    krb5_error_code ret;
    char cleanname[MAXDNAME + 1];

    *realmsp = NULL;

    ret = k5_clean_hostname(context, host, cleanname, sizeof(cleanname));
    if (ret)
        return ret;

    /* Search the [domain_realm] profile section unless the hostname looks like
     * an IP address. */
    if (!is_numeric_address(cleanname))
        ret = search_domain_realm(context, cleanname, &realm);

    /* If we didn't find a match, return the referral realm. */
    if (realm == NULL) {
        realm = strdup(KRB5_REFERRAL_REALM);
        if (realm == NULL)
            return ENOMEM;
    }

    retrealms = calloc(2, sizeof(*retrealms));
    if (retrealms == NULL) {
        free(realm);
        return ENOMEM;
    }

    retrealms[0] = realm;
    retrealms[1] = 0;

    TRACE_GET_HOST_REALM_RETURN(context, host, realm);
    *realmsp = retrealms;
    return 0;
}

/*
 * Walk through the components of a domain.  At each stage determine if a KDC
 * can be located for that domain.  Return a realm corresponding to the
 * upper-cased domain name for which a KDC was found or NULL if no KDC was
 * found.  Stop searching after limit labels have been removed from the domain
 * (-1 means don't search at all, 0 means try only the full domain itself, 1
 * means also try the parent domain, etc.) or when we reach a parent with only
 * one label.
 */
static krb5_error_code
domain_heuristic(krb5_context context, const char *domain, char **realm,
                 int limit)
{
    krb5_error_code ret = 0, r;
    struct serverlist slist;
    krb5_data drealm;
    char *p = NULL, *fqdn, *dot;

    *realm = NULL;
    if (limit < 0)
        return 0;

    memset(&drealm, 0, sizeof(drealm));
    fqdn = strdup(domain);
    if (fqdn == NULL) {
        ret = ENOMEM;
        goto cleanup;
    }

    /* Upper case the domain (for use as a realm). */
    for (p = fqdn; *p != '\0'; p++) {
        if (islower((unsigned char)*p))
            *p = toupper((unsigned char)*p);
    }

    /* Search up to limit parents, as long as we have multiple labels. */
    p = fqdn;
    while (limit-- >= 0 && (dot = strchr(p, '.')) != NULL) {
        /* Find a KDC based on this part of the domain name. */
        drealm = string2data(p);
        r = k5_locate_kdc(context, &drealm, &slist, FALSE, SOCK_DGRAM);
        if (r == 0) {
            k5_free_serverlist(&slist);
            *realm = strdup(p);
            if (*realm == NULL) {
                ret = ENOMEM;
                goto cleanup;
            }
            break;
        }

        p = dot + 1;
    }

cleanup:
    free(fqdn);
    return ret;
}

/*
 * Determine the fallback realm.  Used by krb5_get_credentials after referral
 * processing has failed, to look at TXT records or make a DNS-based
 * assumption.
 */
krb5_error_code KRB5_CALLCONV
krb5_get_fallback_host_realm(krb5_context context, krb5_data *hdata,
                             char ***realmsp)
{
    char **retrealms, *p, *realm = NULL, *host, cleanname[MAXDNAME + 1];
    krb5_error_code ret;
    int limit;
    errcode_t code;
    krb5_boolean is_numeric;

    *realmsp = NULL;

    /* Convert hdata into a string and clean it up. */
    host = k5memdup0(hdata->data, hdata->length, &ret);
    if (host == NULL)
        return ret;
    ret = k5_clean_hostname(context, host, cleanname, sizeof(cleanname));
    free(host);
    if (ret)
        return ret;
    is_numeric = is_numeric_address(cleanname);

    /*
     * Try looking up a _kerberos.<hostname> TXT record in DNS.  This heuristic
     * is turned off by default since, in the absence of secure DNS, it can
     * allow an attacker to control the realm used for a host.
     */
#ifdef KRB5_DNS_LOOKUP
    if (_krb5_use_dns_realm(context) && !is_numeric) {
        p = cleanname;
        do {
            ret = krb5_try_realm_txt_rr("_kerberos", p, &realm);
            p = strchr(p, '.');
            if (p != NULL)
                p++;
        } while (ret && p != NULL && *p != '\0');
    }
#endif /* KRB5_DNS_LOOKUP */

    /*
     * Next try searching the domain components as realms.  This heuristic is
     * also turned off by default.  If DNS lookups for KDCs are enabled (as
     * they are by default), an attacker could control which domain component
     * is used as the realm for a host.
     */
    if (realm == NULL && !is_numeric) {
        code = profile_get_integer(context->profile, KRB5_CONF_LIBDEFAULTS,
                                   KRB5_CONF_REALM_TRY_DOMAINS, 0, -1, &limit);
        if (code == 0) {
            ret = domain_heuristic(context, cleanname, &realm, limit);
            if (ret)
                return ret;
        }
    }

    /*
     * The next fallback--and the first one to apply with default
     * configuration--is to use the upper-cased parent domain of the hostname,
     * regardless of whether we can actually look it up as a realm.
     */
    if (realm == NULL && !is_numeric) {
        p = strchr(cleanname, '.');
        if (p) {
            realm = strdup(p + 1);
            if (realm == NULL)
                return ENOMEM;
            for (p = realm; *p != '\0'; p++) {
                if (islower((unsigned char)*p))
                    *p = toupper((unsigned char)*p);
            }
        }
    }

    /*
     * The final fallback--used when the fully-qualified hostname has only one
     * component--is to use the local default realm.
     */
    if (realm == NULL) {
        ret = krb5_get_default_realm(context, &realm);
        if (ret)
            return ret;
    }

    retrealms = calloc(2, sizeof(*retrealms));
    if (retrealms == NULL) {
        free(realm);
        return ENOMEM;
    }

    retrealms[0] = realm;
    retrealms[1] = 0;

    TRACE_GET_FALLBACK_HOST_REALM_RETURN(context, host, realm);
    *realmsp = retrealms;
    return 0;
}

/* Common code for krb5_get_host_realm and krb5_get_fallback_host_realm
 * to do basic sanity checks on supplied hostname. */
krb5_error_code
k5_clean_hostname(krb5_context context, const char *host, char *cleanname,
                  size_t lhsize)
{
    char *p;
    krb5_error_code ret;
    size_t l;

    cleanname[0] = '\0';
    if (host) {
        if (strlcpy(cleanname, host, lhsize) >= lhsize)
            return ENOMEM;
    } else {
        ret = krb5int_get_fq_local_hostname(cleanname, lhsize);
        if (ret)
            return ret;
    }

    /* Fold to lowercase. */
    for (p = cleanname; *p; p++) {
        if (isupper((unsigned char)*p))
            *p = tolower((unsigned char)*p);
    }

    /* Strip off trailing dot. */
    l = strlen(cleanname);
    if (l > 0 && cleanname[l - 1] == '.')
        cleanname[l - 1] = '\0';

    return 0;
}

krb5_error_code KRB5_CALLCONV
krb5_free_host_realm(krb5_context context, char *const *realmlist)
{
    char *const *p;

    if (realmlist == NULL)
        return 0;
    for (p = realmlist; *p; p++)
        free(*p);
    free((char **)realmlist);
    return 0;
}