]> granicus.if.org Git - p11-kit/commitdiff
Added p11_kit_module_get_filename()
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Fri, 10 Jul 2015 12:31:57 +0000 (14:31 +0200)
committerStef Walter <stefw@redhat.com>
Tue, 14 Jul 2015 19:31:34 +0000 (21:31 +0200)
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 <stefw@redhat.com>
 * Fixed up whitespace

p11-kit/modules.c
p11-kit/p11-kit.h
p11-kit/test-modules.c

index 38c752b5faa88cb5be260a11a4ffadcdeb0fa61d..bbeeef68ce982cdcfa8a164cfacf8405addfb725 100644 (file)
@@ -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
+ *     <code>NULL</code> 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)
index f99f7ed1a516a88cc90a1bffa09d51f4e14c425d..a266c35b409c27a34cc2e8e194b9d9e98161a706 100644 (file)
@@ -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);
index f27450293619ffe1c75f1e0bf1e6b8bbff59b207..837e7ff9d9db7674d71a6e9ddb0df583fccf656a 100644 (file)
@@ -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 <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <libgen.h>
 
 #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");