kafs: Fix a warning
[heimdal.git] / lib / krb5 / plugin.c
blob2dc22d18a0545036cc28839ffcc32eaff785da56
1 /*
2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2018 AuriStor, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 #include "krb5_locl.h"
37 #include "common_plugin.h"
40 * Definitions:
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)
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...
52 * modules := { module: dsos }
53 * dsos := { path, dsohandle, plugins-by-name }
54 * plugins-by-name := { plugin-name: [plug] }
55 * plug := { ftable, ctx }
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").
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.
71 * @ingroup krb5_support
74 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
75 krb5_plugin_register(krb5_context context,
76 enum krb5_plugin_type type,
77 const char *name,
78 void *symbol)
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.
85 if (type != PLUGIN_TYPE_DATA) {
86 krb5_warnx(context, "krb5_plugin_register: PLUGIN_TYPE_DATA no longer supported");
87 return EINVAL;
90 return heim_plugin_register(context->hcontext, (heim_pcontext)context,
91 "krb5", name, symbol);
94 /**
95 * Load plugins (new system) for the given module @name (typically
96 * "krb5") from the given directory @paths.
98 * Inputs:
100 * @context A krb5_context
101 * @name Name of plugin module (typically "krb5")
102 * @paths Array of directory paths where to look
104 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
105 _krb5_load_plugins(krb5_context context, const char *name, const char **paths)
107 heim_load_plugins(context->hcontext, name, paths);
111 * Unload plugins (new system)
113 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
114 _krb5_unload_plugins(krb5_context context, const char *name)
116 heim_unload_plugins(context->hcontext, name);
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.
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.
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.
135 * Inputs:
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
145 * Outputs: None, other than the return value and such outputs as are
146 * gathered by @func.
148 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
149 _krb5_plugin_run_f(krb5_context context,
150 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 *))
155 int32_t (HEIM_LIB_CALL *func2)(void *, const void *, void *, void *) = (void *)func;
156 return heim_plugin_run_f(context->hcontext, (heim_pcontext)context, caller,
157 flags, KRB5_PLUGIN_NO_HANDLE, userctx, func2);
161 * Return a cookie identifying this instance of a library.
163 * Inputs:
165 * @context A krb5_context
166 * @module Our library name or a library we depend on
168 * Outputs: The instance cookie
170 * @ingroup krb5_support
173 #ifdef WIN32
174 static uintptr_t
175 djb2(uintptr_t hash, unsigned char *str)
177 int c;
179 while (c = *str++)
180 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
182 return hash;
184 #endif
186 KRB5_LIB_FUNCTION uintptr_t KRB5_LIB_CALL
187 krb5_get_instance(const char *libname)
189 #ifdef WIN32
190 char *version;
191 char *name;
192 uintptr_t instance;
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 static const char *instance = "libkrb5";
204 if (strcmp(libname, "krb5") == 0)
205 return (uintptr_t)instance;
206 return 0;
207 #endif