From 406803044f61fcbd491749a5530b39beed270dd2 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Fri, 10 Jul 2015 14:31:57 +0200 Subject: Added p11_kit_module_get_filename() That function allows to obtain the filename used by the PKCS #11 module. That is the filename used by dlopen(). Note that we don't provide p11_kit_module_for_filename() because it would have to deal with filename equivalences. Signed-off-by: Stef Walter * Fixed up whitespace --- p11-kit/modules.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ p11-kit/p11-kit.h | 1 + p11-kit/test-modules.c | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/p11-kit/modules.c b/p11-kit/modules.c index 38c752b..bbeeef6 100644 --- a/p11-kit/modules.c +++ b/p11-kit/modules.c @@ -146,6 +146,7 @@ typedef struct _Module { /* Registered modules */ char *name; + char *filename; p11_dict *config; bool critical; @@ -256,6 +257,7 @@ free_module_unlocked (void *data) p11_mutex_uninit (&mod->initialize_mutex); p11_dict_free (mod->config); free (mod->name); + free (mod->filename); free (mod); } @@ -363,6 +365,8 @@ load_module_from_file_inlock (const char *name, p11_debug ("loading module %s%sfrom path: %s", name ? name : "", name ? " " : "", path); + mod->filename = strdup (path); + rv = dlopen_and_get_function_list (mod, path, &funcs); free (expand); @@ -410,6 +414,7 @@ setup_module_for_remote_inlock (const char *name, return CKR_DEVICE_ERROR; } + mod->filename = NULL; mod->loaded_module = rpc; mod->loaded_destroy = p11_rpc_transport_free; @@ -1154,6 +1159,46 @@ p11_kit_module_get_name (CK_FUNCTION_LIST *module) return name; } +/** + * p11_kit_module_get_filename: + * @module: pointer to a loaded module + * + * Get the configured name of the PKCS\#11 module. + * + * Configured modules are loaded by p11_kit_modules_load(). The module + * passed to this function can be either managed or unmanaged. Non + * configured modules will return %NULL. + * + * Use free() to release the return value when you're done with it. + * + * Returns: a newly allocated string containing the module name, or + * NULL if the module is not a configured module + */ +char * +p11_kit_module_get_filename (CK_FUNCTION_LIST *module) +{ + Module *mod; + char *name = NULL; + + return_val_if_fail (module != NULL, NULL); + + p11_library_init_once (); + + p11_lock (); + + p11_message_clear (); + + if (gl.modules) { + mod = module_for_functions_inlock (module); + if (mod && mod->filename) + name = strdup (mod->filename); + } + + p11_unlock (); + + return name; +} + static const char * module_get_option_inlock (Module *mod, const char *option) diff --git a/p11-kit/p11-kit.h b/p11-kit/p11-kit.h index f99f7ed..a266c35 100644 --- a/p11-kit/p11-kit.h +++ b/p11-kit/p11-kit.h @@ -78,6 +78,7 @@ void p11_kit_modules_finalize_and_release (CK_FUNCTION_LIST ** CK_FUNCTION_LIST * p11_kit_module_for_name (CK_FUNCTION_LIST **modules, const char *name); +char * p11_kit_module_get_filename (CK_FUNCTION_LIST *module); char * p11_kit_module_get_name (CK_FUNCTION_LIST *module); int p11_kit_module_get_flags (CK_FUNCTION_LIST *module); diff --git a/p11-kit/test-modules.c b/p11-kit/test-modules.c index f274502..837e7ff 100644 --- a/p11-kit/test-modules.c +++ b/p11-kit/test-modules.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012 Red Hat Inc + * Copyright (c) 2012, 2015 Red Hat Inc * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -39,6 +39,7 @@ #include #include #include +#include #include "debug.h" #include "library.h" @@ -126,6 +127,25 @@ lookup_module_with_name (CK_FUNCTION_LIST_PTR_PTR modules, return match; } +static CK_FUNCTION_LIST_PTR +lookup_module_with_filename (CK_FUNCTION_LIST_PTR_PTR modules, + const char *name) +{ + CK_FUNCTION_LIST_PTR match = NULL; + char *module_name; + int i; + + for (i = 0; match == NULL && modules[i] != NULL; i++) { + module_name = p11_kit_module_get_filename (modules[i]); + assert_ptr_not_null (module_name); + if (strcmp (basename(module_name), name) == 0) + match = modules[i]; + free (module_name); + } + + return match; +} + static void test_disable (void) { @@ -156,6 +176,23 @@ test_disable (void) p11_kit_set_progname (NULL); } +static void +test_filename (void) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + + /* + * The module four should be present, as we don't match any prognames + * that it has disabled. + */ + + modules = initialize_and_get_modules (); +#ifndef _WIN32 + assert (lookup_module_with_filename (modules, "mock-four.so") != NULL); +#endif + finalize_and_free_modules (modules); +} + static void test_disable_later (void) { @@ -398,6 +435,7 @@ main (int argc, { p11_library_init (); + p11_test (test_filename, "/modules/test_filename"); p11_test (test_no_duplicates, "/modules/test_no_duplicates"); p11_test (test_disable, "/modules/test_disable"); p11_test (test_disable_later, "/modules/test_disable_later"); -- cgit v1.2.3