Line data Source code
1 : /*
2 : * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 : * (Royal Institute of Technology, Stockholm, Sweden).
4 : * All rights reserved.
5 : *
6 : * Portions Copyright (c) 2018 AuriStor, Inc.
7 : *
8 : * Redistribution and use in source and binary forms, with or without
9 : * modification, are permitted provided that the following conditions
10 : * are met:
11 : *
12 : * 1. Redistributions of source code must retain the above copyright
13 : * notice, this list of conditions and the following disclaimer.
14 : *
15 : * 2. Redistributions in binary form must reproduce the above copyright
16 : * notice, this list of conditions and the following disclaimer in the
17 : * documentation and/or other materials provided with the distribution.
18 : *
19 : * 3. Neither the name of the Institute nor the names of its contributors
20 : * may be used to endorse or promote products derived from this software
21 : * without specific prior written permission.
22 : *
23 : * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 : * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 : * SUCH DAMAGE.
34 : */
35 :
36 : #include "krb5_locl.h"
37 : #include "common_plugin.h"
38 :
39 : /*
40 : * Definitions:
41 : *
42 : * module - a category of plugin module, identified by subsystem
43 : * (typically "krb5")
44 : * dso - a library for a module containing a map of plugin
45 : * types to plugins (e.g. "service_locator")
46 : * plugin - a set of callbacks and state that follows the
47 : * common plugin module definition (version, init, fini)
48 : *
49 : * Obviously it would have been clearer to use the term "module" rather than
50 : * "DSO" given there is an internal "DSO", but "module" was already taken...
51 : *
52 : * modules := { module: dsos }
53 : * dsos := { path, dsohandle, plugins-by-name }
54 : * plugins-by-name := { plugin-name: [plug] }
55 : * plug := { ftable, ctx }
56 : *
57 : * Some existing plugin consumers outside libkrb5 use the "krb5" module
58 : * namespace, but going forward the module should match the consumer library
59 : * name (e.g. libhdb should use the "hdb" module rather than "krb5").
60 : */
61 :
62 : /**
63 : * Register a plugin symbol name of specific type.
64 : * @param context a Keberos context
65 : * @param type type of plugin symbol
66 : * @param name name of plugin symbol
67 : * @param symbol a pointer to the named symbol
68 : * @return In case of error a non zero error com_err error is returned
69 : * and the Kerberos error string is set.
70 : *
71 : * @ingroup krb5_support
72 : */
73 :
74 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
75 33074 : krb5_plugin_register(krb5_context context,
76 : enum krb5_plugin_type type,
77 : const char *name,
78 : const void *symbol)
79 : {
80 : /*
81 : * It's not clear that PLUGIN_TYPE_FUNC was ever used or supported. It likely
82 : * would have caused _krb5_plugin_run_f() to crash as the previous implementation
83 : * assumed PLUGIN_TYPE_DATA.
84 : */
85 33074 : if (type != PLUGIN_TYPE_DATA) {
86 0 : krb5_warnx(context, "krb5_plugin_register: PLUGIN_TYPE_DATA no longer supported");
87 0 : return EINVAL;
88 : }
89 :
90 33074 : return heim_plugin_register(context->hcontext, (heim_pcontext)context,
91 : "krb5", name, symbol);
92 : }
93 :
94 : /**
95 : * Load plugins (new system) for the given module @name (typically
96 : * "krb5") from the given directory @paths.
97 : *
98 : * Inputs:
99 : *
100 : * @context A krb5_context
101 : * @name Name of plugin module (typically "krb5")
102 : * @paths Array of directory paths where to look
103 : */
104 : KRB5_LIB_FUNCTION void KRB5_LIB_CALL
105 33944 : _krb5_load_plugins(krb5_context context, const char *name, const char **paths)
106 : {
107 33944 : heim_load_plugins(context->hcontext, name, paths);
108 33944 : }
109 :
110 : /**
111 : * Unload plugins (new system)
112 : */
113 : KRB5_LIB_FUNCTION void KRB5_LIB_CALL
114 0 : _krb5_unload_plugins(krb5_context context, const char *name)
115 : {
116 0 : heim_unload_plugins(context->hcontext, name);
117 0 : }
118 :
119 : /**
120 : * Run plugins for the given @module (e.g., "krb5") and @name (e.g.,
121 : * "kuserok"). Specifically, the @func is invoked once per-plugin with
122 : * four arguments: the @context, the plugin symbol value (a pointer to a
123 : * struct whose first three fields are the same as common_plugin_ftable),
124 : * a context value produced by the plugin's init method, and @userctx.
125 : *
126 : * @func should unpack arguments for a plugin function and invoke it
127 : * with arguments taken from @userctx. @func should save plugin
128 : * outputs, if any, in @userctx.
129 : *
130 : * All loaded and registered plugins are invoked via @func until @func
131 : * returns something other than KRB5_PLUGIN_NO_HANDLE. Plugins that
132 : * have nothing to do for the given arguments should return
133 : * KRB5_PLUGIN_NO_HANDLE.
134 : *
135 : * Inputs:
136 : *
137 : * @context A krb5_context
138 : * @module Name of module (typically "krb5")
139 : * @name Name of pluggable interface (e.g., "kuserok")
140 : * @min_version Lowest acceptable plugin minor version number
141 : * @flags Flags (none defined at this time)
142 : * @userctx Callback data for the callback function @func
143 : * @func A callback function, invoked once per-plugin
144 : *
145 : * Outputs: None, other than the return value and such outputs as are
146 : * gathered by @func.
147 : */
148 : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
149 1400490 : _krb5_plugin_run_f(krb5_context context,
150 : const struct heim_plugin_data *caller,
151 : int flags,
152 : void *userctx,
153 : krb5_error_code (KRB5_LIB_CALL *func)(krb5_context, const void *, void *, void *))
154 : {
155 1400490 : int32_t (HEIM_LIB_CALL *func2)(void *, const void *, void *, void *) = (void *)func;
156 1400490 : return heim_plugin_run_f(context->hcontext, (heim_pcontext)context, caller,
157 : flags, KRB5_PLUGIN_NO_HANDLE, userctx, func2);
158 : }
159 :
160 : /**
161 : * Return a cookie identifying this instance of a library.
162 : *
163 : * Inputs:
164 : *
165 : * @context A krb5_context
166 : * @module Our library name or a library we depend on
167 : *
168 : * Outputs: The instance cookie
169 : *
170 : * @ingroup krb5_support
171 : */
172 :
173 : #ifdef WIN32
174 : static uintptr_t
175 : djb2(uintptr_t hash, unsigned char *str)
176 : {
177 : int c;
178 :
179 : while (c = *str++)
180 : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
181 :
182 : return hash;
183 : }
184 : #endif
185 :
186 : KRB5_LIB_FUNCTION uintptr_t KRB5_LIB_CALL
187 0 : krb5_get_instance(const char *libname)
188 : {
189 : #ifdef WIN32
190 : char *version;
191 : char *name;
192 : uintptr_t instance;
193 :
194 : if (win32_getLibraryVersion("heimdal", &name, &version))
195 : return 0;
196 : instance = djb2(5381, name);
197 : instance = djb2(instance, version);
198 : free(name);
199 : free(version);
200 : return instance;
201 : #else
202 0 : static const char *instance = "libkrb5";
203 :
204 0 : if (strcmp(libname, "krb5") == 0)
205 0 : return (uintptr_t)instance;
206 0 : return 0;
207 : #endif
208 : }
|