2 * Copyright (c) 2006 - 2020 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
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
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
37 #include "common_plugin.h"
40 * Documentation for the Heimdal plugin system is in lib/krb5/plugin.c and
41 * lib/krb5/krb5-plugin.7.
47 * module - a category of plugin module, identified by subsystem
49 * dso - a library for a module containing a map of plugin
50 * types to plugins (e.g. "service_locator")
51 * plugin - a set of callbacks and state that follows the
52 * common plugin module definition (version, init, fini)
54 * Obviously it would have been clearer to use the term "module" rather than
55 * "DSO" given there is an internal "DSO", but "module" was already taken...
57 * modules := { module: dsos }
58 * dsos := { path, dsohandle, plugins-by-name }
59 * plugins-by-name := { plugin-name: [plug] }
60 * plug := { ftable, ctx }
63 /* global module use, use copy_modules() accessor to access */
64 static heim_dict_t __modules
;
66 static HEIMDAL_MUTEX modules_mutex
= HEIMDAL_MUTEX_INITIALIZER
;
69 copy_modules_once(void *context
)
71 heim_dict_t
*modules
= (heim_dict_t
*)context
;
73 *modules
= heim_dict_create(11);
74 heim_assert(*modules
, "plugin modules array allocation failure");
77 /* returns global modules list, refcount +1 */
81 static heim_base_once_t modules_once
= HEIM_BASE_ONCE_INIT
;
83 heim_base_once_f(&modules_once
, &__modules
, copy_modules_once
);
85 return heim_retain(__modules
);
88 /* returns named module, refcount +1 */
90 copy_module(const char *name
)
92 heim_string_t module_name
= heim_string_create(name
);
93 heim_dict_t modules
= copy_modules();
96 module
= heim_dict_copy_value(modules
, module_name
);
98 module
= heim_dict_create(11);
99 heim_dict_set_value(modules
, module_name
, module
);
102 heim_release(modules
);
103 heim_release(module_name
);
111 heim_dict_t plugins_by_name
;
115 static void HEIM_CALLCONV
116 dso_dealloc(void *ptr
)
118 struct heim_dso
*p
= ptr
;
120 heim_release(p
->path
);
121 heim_release(p
->plugins_by_name
);
124 dlclose(p
->dsohandle
);
128 /* returns internal "DSO" for name, refcount +1 */
129 static struct heim_dso
*
130 copy_internal_dso(const char *name
)
132 heim_string_t dso_name
= HSTR("__HEIMDAL_INTERNAL_DSO__");
133 heim_dict_t module
= copy_module(name
);
134 struct heim_dso
*dso
;
139 dso
= heim_dict_copy_value(module
, dso_name
);
141 dso
= heim_alloc(sizeof(*dso
), "heim-dso", dso_dealloc
);
143 dso
->path
= dso_name
;
144 dso
->plugins_by_name
= heim_dict_create(11);
146 heim_dict_set_value(module
, dso_name
, dso
);
149 heim_release(module
);
155 heim_plugin_common_ftable_const_p ftable
;
159 static void HEIM_CALLCONV
160 plugin_free(void *ptr
)
162 struct heim_plugin
*pl
= ptr
;
164 if (pl
->ftable
&& pl
->ftable
->fini
)
165 pl
->ftable
->fini(pl
->ctx
);
168 struct heim_plugin_register_ctx
{
174 plugin_register_check_dup(heim_object_t value
, void *ctx
, int *stop
)
176 struct heim_plugin_register_ctx
*pc
= ctx
;
177 struct heim_plugin
*pl
= value
;
179 if (pl
->ftable
== pc
->symbol
) {
186 * Register a plugin symbol name of specific type.
187 * @param context a Keberos context
188 * @param module name of plugin module (e.g., "krb5")
189 * @param name name of plugin symbol (e.g., "krb5_plugin_kuserok")
190 * @param ftable a pointer to a function pointer table
191 * @return In case of error a non zero error com_err error is returned
192 * and the Kerberos error string is set.
194 * @ingroup heim_support
198 heim_plugin_register(heim_context context
,
199 heim_pcontext pcontext
,
205 heim_array_t plugins
;
207 struct heim_dso
*dso
;
208 struct heim_plugin_register_ctx ctx
;
213 HEIMDAL_MUTEX_lock(&modules_mutex
);
215 dso
= copy_internal_dso(module
);
216 hname
= heim_string_create(name
);
217 plugins
= heim_dict_copy_value(dso
->plugins_by_name
, hname
);
219 heim_array_iterate_f(plugins
, &ctx
, plugin_register_check_dup
);
221 plugins
= heim_array_create();
222 heim_dict_set_value(dso
->plugins_by_name
, hname
, plugins
);
227 /* Note: refactored plugin API only supports common plugin layout */
228 struct heim_plugin
*pl
;
230 pl
= heim_alloc(sizeof(*pl
), "heim-plugin", plugin_free
);
232 ret
= heim_enomem(context
);
235 ret
= pl
->ftable
->init(pcontext
, &pl
->ctx
);
237 heim_array_append_value(plugins
, pl
);
238 heim_debug(context
, 5, "Registered %s plugin", name
);
244 HEIMDAL_MUTEX_unlock(&modules_mutex
);
248 heim_release(plugins
);
256 resolve_origin(const char *di
, const char *module
)
263 if (strncmp(di
, "$ORIGIN/", sizeof("$ORIGIN/") - 1) != 0 &&
264 strcmp(di
, "$ORIGIN") != 0)
267 di
+= sizeof("$ORIGIN") - 1;
269 if (dladdr(heim_plugin_register
, &dl_info
) == 0) {
272 /* dladdr() failed */
273 if (asprintf(&s
, LIBDIR
"/plugin/%s", module
) == -1)
278 dname
= dl_info
.dli_fname
;
280 p
= strrchr(dname
, '\\');
283 p
= strrchr(dname
, '/');
285 if (asprintf(&path
, "%.*s%s", (int) (p
- dname
), dname
, di
) == -1)
288 if (asprintf(&path
, "%s%s", dname
, di
) == -1)
296 if (strncmp(di
, "$ORIGIN/", sizeof("$ORIGIN/") - 1) != 0 &&
297 strcmp(di
, "$ORIGIN") != 0)
299 if (asprintf(&s
, LIBDIR
"/plugin/%s", module
) == -1)
302 #endif /* HAVE_DLADDR */
305 #endif /* HAVE_DLOPEN */
308 * Load plugins (new system) for the given module @module from the given
313 * @context A heim_context
314 * @module Name of plugin module (typically "krb5")
315 * @paths Array of directory paths where to look
318 heim_load_plugins(heim_context context
,
323 heim_string_t s
= heim_string_create(module
);
324 heim_dict_t mod
, modules
;
325 struct dirent
*entry
;
328 char *dirname
= NULL
;
332 size_t plugin_prefix_len
;
334 if (asprintf(&plugin_prefix
, "plugin_%s_", module
) == -1)
336 plugin_prefix_len
= (plugin_prefix
? strlen(plugin_prefix
) : 0);
339 HEIMDAL_MUTEX_lock(&modules_mutex
);
341 modules
= copy_modules();
343 mod
= heim_dict_copy_value(modules
, s
);
345 mod
= heim_dict_create(11);
347 HEIMDAL_MUTEX_unlock(&modules_mutex
);
349 heim_release(modules
);
350 heim_debug(context
, 5, "Load plugins for module %s failed", module
);
353 heim_dict_set_value(modules
, s
, mod
);
356 heim_release(modules
);
358 for (di
= paths
; *di
!= NULL
; di
++) {
360 dirname
= resolve_origin(*di
, module
);
361 if (dirname
== NULL
) {
362 heim_debug(context
, 10, "Could not resolve %s", *di
);
365 d
= opendir(dirname
);
367 heim_debug(context
, 10, "No such directory %s", dirname
);
372 heim_debug(context
, 10, "Load plugins for module %s; search %s (%s)",
373 module
, *di
, dirname
);
375 while ((entry
= readdir(d
)) != NULL
) {
376 char *n
= entry
->d_name
;
382 if (n
[0] == '.' && (n
[1] == '\0' || (n
[1] == '.' && n
[2] == '\0')))
388 * On Windows, plugins must be loaded from the same directory as
389 * heimdal.dll (typically the assembly directory) and must have
390 * the name form "plugin_<module>_<name>.dll".
395 if (strnicmp(n
, plugin_prefix
, plugin_prefix_len
) != 0)
397 ext
= strrchr(n
, '.');
398 if (ext
== NULL
|| stricmp(ext
, ".dll") != 0)
401 ret
= asprintf(&path
, "%s\\%s", dirname
, n
);
402 if (ret
< 0 || path
== NULL
)
407 { /* support loading bundles on MacOS */
408 size_t len
= strlen(n
);
409 if (len
> 7 && strcmp(&n
[len
- 7], ".bundle") == 0)
410 ret
= asprintf(&path
, "%s/%s/Contents/MacOS/%.*s", dirname
, n
, (int)(len
- 7), n
);
413 if (ret
< 0 || path
== NULL
)
414 ret
= asprintf(&path
, "%s/%s", dirname
, n
);
416 if (ret
< 0 || path
== NULL
)
419 spath
= heim_string_create(n
);
425 /* check if already cached */
426 p
= heim_dict_copy_value(mod
, spath
);
428 p
= heim_alloc(sizeof(*p
), "heim-dso", dso_dealloc
);
430 p
->dsohandle
= dlopen(path
, RTLD_LOCAL
|RTLD_LAZY
|RTLD_GROUP
);
431 if (p
&& p
->dsohandle
) {
432 p
->path
= heim_retain(spath
);
433 p
->plugins_by_name
= heim_dict_create(11);
434 heim_dict_set_value(mod
, spath
, p
);
435 heim_debug(context
, 10, "Load plugins for module %s; "
436 "found DSO %s", module
, path
);
446 HEIMDAL_MUTEX_unlock(&modules_mutex
);
452 #endif /* HAVE_DLOPEN */
456 * Unload plugins of the given @module name.
460 * @module Name of module whose plusins to unload.
463 heim_unload_plugins(heim_context context
, const char *module
)
465 heim_string_t sname
= heim_string_create(module
);
468 HEIMDAL_MUTEX_lock(&modules_mutex
);
470 modules
= copy_modules();
471 heim_dict_delete_key(modules
, sname
);
473 HEIMDAL_MUTEX_unlock(&modules_mutex
);
475 heim_release(modules
);
480 heim_context context
;
481 heim_pcontext pcontext
;
483 const struct heim_plugin_data
*caller
;
486 int32_t (HEIM_LIB_CALL
*func
)(void *, const void *, void *, void *);
489 int32_t plugin_no_handle_retval
;
494 * Add plugin from a DSO that exports the plugin structure directly. This is
495 * provided for backwards compatibility with prior versions of Heimdal, but it
496 * does not allow a module to export multiple plugins, nor does it allow
497 * instance validation.
500 add_dso_plugin_struct(heim_context context
,
501 heim_pcontext pcontext
,
507 heim_plugin_common_ftable_p cpm
;
508 struct heim_plugin
*pl
;
509 heim_array_t plugins
;
511 if (dsohandle
== NULL
)
514 /* suppress error here because we may be looking for a different plugin type */
515 cpm
= (heim_plugin_common_ftable_p
)dlsym(dsohandle
, name
);
517 heim_debug(context
, 15, "Symbol %s not found in %s", name
, dsopath
);
521 heim_warnx(context
, "plugin %s uses deprecated loading mechanism", dsopath
);
523 pl
= heim_alloc(sizeof(*pl
), "heim-plugin", plugin_free
);
525 ret
= cpm
->init(pcontext
, &pl
->ctx
);
527 heim_warn(context
, ret
, "plugin %s failed to initialize", dsopath
);
534 plugins
= heim_array_create();
535 heim_array_append_value(plugins
, pl
);
542 validate_plugin_deps(heim_context context
,
543 const struct heim_plugin_data
*caller
,
545 heim_get_instance_func_t get_instance
)
549 if (get_instance
== NULL
) {
550 heim_warnx(context
, "plugin %s omitted instance callback",
555 for (i
= 0; caller
->deps
[i
] != NULL
; i
++) {
556 uintptr_t heim_instance
, plugin_instance
;
558 heim_instance
= caller
->get_instance(caller
->deps
[i
]);
559 plugin_instance
= get_instance(caller
->deps
[i
]);
561 if (heim_instance
== 0 || plugin_instance
== 0)
564 if (heim_instance
!= plugin_instance
) {
565 heim_warnx(context
, "plugin %s library %s linked against different "
566 "instance of Heimdal (got %zu, us %zu)",
567 dsopath
, caller
->deps
[i
],
568 plugin_instance
, heim_instance
);
571 heim_debug(context
, 10, "Validated plugin library dependency %s for %s",
572 caller
->deps
[i
], dsopath
);
579 * New interface from Heimdal 8 where a DSO can export a load function
580 * that can return both a Heimdal instance identifier along with an
584 add_dso_plugins_load_fn(heim_context context
,
585 heim_pcontext pcontext
,
586 const struct heim_plugin_data
*caller
,
591 heim_array_t plugins
;
592 heim_plugin_load_t load_fn
;
595 heim_get_instance_func_t get_instance
;
597 heim_plugin_common_ftable_cp
*ftables
;
599 if (asprintf(&sym
, "%s_plugin_load", caller
->name
) == -1 || sym
== NULL
)
602 /* suppress error here because we may be looking for a different plugin type */
603 load_fn
= (heim_plugin_load_t
)dlsym(dsohandle
, sym
);
604 if (load_fn
== NULL
) {
605 heim_debug(context
, 15, "Symbol %s not found in %s", sym
, dsopath
);
610 ret
= load_fn(pcontext
, &get_instance
, &n_ftables
, &ftables
);
612 heim_warn(context
, ret
, "plugin %s failed to load", dsopath
);
615 /* fallback to loading structure directly */
616 return add_dso_plugin_struct(context
, pcontext
, dsopath
,
617 dsohandle
, caller
->name
);
620 if (!validate_plugin_deps(context
, caller
, dsopath
, get_instance
)) {
625 plugins
= heim_array_create();
627 for (i
= 0; i
< n_ftables
; i
++) {
628 heim_plugin_common_ftable_cp cpm
= ftables
[i
];
629 struct heim_plugin
*pl
;
631 pl
= heim_alloc(sizeof(*pl
), "heim-plugin", plugin_free
);
633 ret
= cpm
->init(pcontext
, &pl
->ctx
);
635 heim_warn(context
, ret
, "plugin %s[%zu] failed to initialize",
639 heim_array_append_value(plugins
, pl
);
644 heim_debug(context
, 15, "DSO %s loaded (%s)", dsopath
, sym
);
648 #endif /* HAVE_DLOPEN */
651 reduce_by_version(heim_object_t value
, void *ctx
, int *stop
)
653 struct iter_ctx
*s
= ctx
;
654 struct heim_plugin
*pl
= value
;
656 if (pl
->ftable
&& pl
->ftable
->minor_version
>= s
->caller
->min_version
)
657 heim_array_append_value(s
->result
, pl
);
661 search_modules(heim_object_t key
, heim_object_t value
, void *ctx
)
663 struct iter_ctx
*s
= ctx
;
664 struct heim_dso
*p
= value
;
665 heim_array_t plugins
= heim_dict_copy_value(p
->plugins_by_name
, s
->n
);
668 if (plugins
== NULL
&& p
->dsohandle
) {
669 const char *path
= heim_string_get_utf8(p
->path
);
671 plugins
= add_dso_plugins_load_fn(s
->context
,
677 heim_dict_set_value(p
->plugins_by_name
, s
->n
, plugins
);
678 heim_debug(s
->context
, 5, "Loaded %zu %s %s plugin%s from %s",
679 heim_array_get_length(plugins
),
680 s
->caller
->module
, s
->caller
->name
,
681 heim_array_get_length(plugins
) > 1 ? "s" : "",
685 #endif /* HAVE_DLOPEN */
688 heim_array_iterate_f(plugins
, s
, reduce_by_version
);
689 heim_release(plugins
);
694 eval_results(heim_object_t value
, void *ctx
, int *stop
)
696 struct heim_plugin
*pl
= value
;
697 struct iter_ctx
*s
= ctx
;
699 if (s
->ret
!= s
->plugin_no_handle_retval
)
702 s
->ret
= s
->func(s
->pcontext
, pl
->ftable
, pl
->ctx
, s
->userctx
);
703 if (s
->ret
!= s
->plugin_no_handle_retval
704 && !(s
->flags
& HEIM_PLUGIN_INVOKE_ALL
))
709 * Run plugins for the given @module (e.g., "krb5") and @name (e.g.,
710 * "kuserok"). Specifically, the @func is invoked once per-plugin with
711 * four arguments: the @context, the plugin symbol value (a pointer to a
712 * struct whose first three fields are the same as common_plugin_ftable),
713 * a context value produced by the plugin's init method, and @userctx.
715 * @func should unpack arguments for a plugin function and invoke it
716 * with arguments taken from @userctx. @func should save plugin
717 * outputs, if any, in @userctx.
719 * All loaded and registered plugins are invoked via @func until @func
720 * returns something other than @nohandle. Plugins that have nothing to
721 * do for the given arguments should return the same value as @nohandle.
725 * @context A heim_context
726 * @pcontext A context for the plugin, such as a krb5_context
727 * @module Name of module (typically "krb5")
728 * @name Name of pluggable interface (e.g., "kuserok")
729 * @min_version Lowest acceptable plugin minor version number
730 * @flags Flags (none defined at this time)
731 * @nohandle Flags (none defined at this time)
732 * @userctx Callback data for the callback function @func
733 * @func A callback function, invoked once per-plugin
735 * Outputs: None, other than the return value and such outputs as are
739 heim_plugin_run_f(heim_context context
,
740 heim_pcontext pcontext
,
741 const struct heim_plugin_data
*caller
,
745 int32_t (HEIM_LIB_CALL
*func
)(void *, const void *, void *, void *))
747 heim_string_t m
= heim_string_create(caller
->module
);
748 heim_dict_t modules
, dict
= NULL
;
752 s
.pcontext
= pcontext
;
754 s
.n
= heim_string_create(caller
->name
);
756 s
.result
= heim_array_create();
759 s
.plugin_no_handle_retval
= nohandle
;
762 HEIMDAL_MUTEX_lock(&modules_mutex
);
764 /* Get loaded plugins */
765 modules
= copy_modules();
766 dict
= heim_dict_copy_value(modules
, m
);
768 /* Add loaded plugins to s.result array */
770 heim_dict_iterate_f(dict
, &s
, search_modules
);
772 /* We don't need to hold modules_mutex during plugin invocation */
773 HEIMDAL_MUTEX_unlock(&modules_mutex
);
775 /* Invoke loaded plugins */
776 heim_array_iterate_f(s
.result
, &s
, eval_results
);
778 heim_release(s
.result
);
782 heim_release(modules
);