Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / lib / krb5 / os / hostrealm_profile.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/os/hostream_profile.c - profile hostrealm module */
3 /*
4  * Copyright (C) 1990,1991,2002,2008,2009,2013 by the Massachusetts Institute
5  * of Technology.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /*
34  * This file implements the built-in profile module for the hostrealm
35  * interface, which uses profile configuration to determine the local default
36  * realm or the authoritative realm of a host.
37  */
38
39 #include "k5-int.h"
40 #include "os-proto.h"
41 #include <krb5/hostrealm_plugin.h>
42
43 /*
44  * Search progressively shorter suffixes of host in the [domain_realms] section
45  * of the profile to find the realm.  For example, given a host a.b.c, try to
46  * match a.b.c, then .b.c, then b.c, then .c, then c.  If we don't find a
47  * match, return success but set *realm_out to NULL.
48  */
49 static krb5_error_code
50 profile_host_realm(krb5_context context, krb5_hostrealm_moddata data,
51                    const char *host, char ***realms_out)
52 {
53     krb5_error_code ret;
54     const char *p;
55     char *prof_realm;
56
57     *realms_out = NULL;
58
59     /* Don't look up IP addresses in [domain_realms]. */
60     if (k5_is_numeric_address(host))
61         return KRB5_PLUGIN_NO_HANDLE;
62
63     /* Look for the host and each suffix in the [domain_realms] section. */
64     for (p = host; p != NULL; p = (*p == '.') ? p + 1 : strchr(p, '.')) {
65         ret = profile_get_string(context->profile, KRB5_CONF_DOMAIN_REALM, p,
66                                  NULL, NULL, &prof_realm);
67         if (ret)
68             return ret;
69         if (prof_realm != NULL) {
70             ret = k5_make_realmlist(prof_realm, realms_out);
71             profile_release_string(prof_realm);
72             return ret;
73         }
74     }
75     return KRB5_PLUGIN_NO_HANDLE;
76 }
77
78 /* Look up the default_realm variable in the [libdefaults] section of the
79  * profile. */
80 static krb5_error_code
81 profile_default_realm(krb5_context context, krb5_hostrealm_moddata data,
82                       char ***realms_out)
83 {
84     krb5_error_code ret;
85     char *prof_realm;
86
87     *realms_out = NULL;
88     ret = profile_get_string(context->profile, KRB5_CONF_LIBDEFAULTS,
89                              KRB5_CONF_DEFAULT_REALM, NULL, NULL, &prof_realm);
90     if (ret)
91         return ret;
92     if (prof_realm == NULL)
93         return KRB5_PLUGIN_NO_HANDLE;
94     ret = k5_make_realmlist(prof_realm, realms_out);
95     profile_release_string(prof_realm);
96     return ret;
97 }
98
99 static void
100 profile_free_realmlist(krb5_context context, krb5_hostrealm_moddata data,
101                        char **list)
102 {
103     krb5_free_host_realm(context, list);
104 }
105
106 krb5_error_code
107 hostrealm_profile_initvt(krb5_context context, int maj_ver, int min_ver,
108                          krb5_plugin_vtable vtable)
109 {
110     krb5_hostrealm_vtable vt = (krb5_hostrealm_vtable)vtable;
111
112     vt->name = "profile";
113     vt->host_realm = profile_host_realm;
114     vt->default_realm = profile_default_realm;
115     vt->free_list = profile_free_realmlist;
116     return 0;
117 }