xref: /freebsd/crypto/krb5/src/lib/krb5/os/hostrealm_registry.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/os/hostream_registry.c - registry hostrealm module */
3 /*
4  * Copyright (C) 2015 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 registry module for the hostrealm
35  * interface, which uses Windows registry configuration to determine the
36  * local default realm.
37  */
38 
39 #include "k5-int.h"
40 #include "os-proto.h"
41 #include <krb5/hostrealm_plugin.h>
42 
43 #ifdef _WIN32
44 /*
45  * Look up a default_realm entry starting from the given base key.
46  * The output *buf_out will be non-NULL if an entry was found; the
47  * caller is responsible for freeing *pbuffer.
48  *
49  * On success, return 0 and set *str_out to the default realm to be
50  * used.  Return KRB5_PLUGIN_NO_HANDLE if the registry key does not
51  * exist, or another code if it cannot be read.
52  */
53 static krb5_error_code
get_from_registry(HKEY hBaseKey,char ** str_out)54 get_from_registry(HKEY hBaseKey, char **str_out)
55 {
56     DWORD bsize = 0;
57     LONG rc;
58     krb5_error_code ret;
59     char *str = NULL;
60     const char *path = "Software\\MIT\\Kerberos5";
61     const char *value = "default_realm";
62 
63     *str_out = NULL;
64 
65     /* Call with zero size to determine the amount of storage needed. */
66     rc = RegGetValue(hBaseKey, path, value, RRF_RT_REG_SZ, NULL, str,
67                      &bsize);
68     if (rc == ERROR_FILE_NOT_FOUND)
69         return KRB5_PLUGIN_NO_HANDLE;
70     if (FAILED(rc) || bsize <= 0)
71         return EIO;
72     str = malloc(bsize);
73     if (str == NULL)
74         return ENOMEM;
75     rc = RegGetValue(hBaseKey, path, value, RRF_RT_REG_SZ, NULL, str,
76                      &bsize);
77     if (FAILED(rc)) {
78         ret = EIO;
79         goto cleanup;
80     }
81 
82     ret = 0;
83     *str_out = str;
84     str = NULL;
85 cleanup:
86     free(str);
87     return ret;
88 }
89 
90 /* Look up the default_realm variable in the
91  * {HKLM,HKCU}\Software\MIT\Kerberos5\default_realm registry values. */
92 static krb5_error_code
registry_default_realm(krb5_context context,krb5_hostrealm_moddata data,char *** realms_out)93 registry_default_realm(krb5_context context, krb5_hostrealm_moddata data,
94                        char ***realms_out)
95 {
96     krb5_error_code ret;
97     char *prof_realm;
98 
99     *realms_out = NULL;
100     ret = get_from_registry(HKEY_LOCAL_MACHINE, &prof_realm);
101     if (ret == KRB5_PLUGIN_NO_HANDLE)
102         ret = get_from_registry(HKEY_CURRENT_USER, &prof_realm);
103     if (ret)
104         return ret;
105     ret = k5_make_realmlist(prof_realm, realms_out);
106     free(prof_realm);
107     return ret;
108 }
109 #else /* _WIN32 */
110 static krb5_error_code
registry_default_realm(krb5_context context,krb5_hostrealm_moddata data,char *** realms_out)111 registry_default_realm(krb5_context context, krb5_hostrealm_moddata data,
112                        char ***realms_out)
113 {
114         return KRB5_PLUGIN_NO_HANDLE;
115 }
116 #endif /* _WIN32 */
117 
118 static void
registry_free_realmlist(krb5_context context,krb5_hostrealm_moddata data,char ** list)119 registry_free_realmlist(krb5_context context, krb5_hostrealm_moddata data,
120                        char **list)
121 {
122     krb5_free_host_realm(context, list);
123 }
124 
125 krb5_error_code
hostrealm_registry_initvt(krb5_context context,int maj_ver,int min_ver,krb5_plugin_vtable vtable)126 hostrealm_registry_initvt(krb5_context context, int maj_ver, int min_ver,
127                          krb5_plugin_vtable vtable)
128 {
129     krb5_hostrealm_vtable vt = (krb5_hostrealm_vtable)vtable;
130 
131     vt->name = "registry";
132     vt->default_realm = registry_default_realm;
133     vt->free_list = registry_free_realmlist;
134     return 0;
135 }
136