Removed an unnecessary include of a header that is not available on Mac OS X.
[strongswan.git] / src / libstrongswan / plugins / plugin_loader.c
index 44081d1..47c244b 100644 (file)
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * for more details.
- *
- * $Id$
  */
 
+#define _GNU_SOURCE
 #include "plugin_loader.h"
 
-#define _GNU_SOURCE
 #include <string.h>
 #include <dlfcn.h>
 #include <limits.h>
 #include <stdio.h>
 
 #include <debug.h>
+#include <integrity_checker.h>
 #include <utils/linked_list.h>
 #include <plugins/plugin.h>
 
@@ -43,6 +42,11 @@ struct private_plugin_loader_t {
         * list of loaded plugins
         */
        linked_list_t *plugins;
+       
+       /**
+        * names of loaded plugins
+        */
+       linked_list_t *names;
 };
 
 /**
@@ -58,6 +62,12 @@ static plugin_t* load_plugin(private_plugin_loader_t *this,
        
        snprintf(file, sizeof(file), "%s/libstrongswan-%s.so", path, name);
        
+       if (lib->integrity &&
+               !lib->integrity->check_file(lib->integrity, name, file))
+       {
+               DBG1("file integrity test of plugin '%s' failed", name);
+               return NULL;
+       }
        handle = dlopen(file, RTLD_LAZY);
        if (handle == NULL)
        {
@@ -71,6 +81,13 @@ static plugin_t* load_plugin(private_plugin_loader_t *this,
                dlclose(handle);
                return NULL;
        }
+       if (lib->integrity &&
+               !lib->integrity->check_segment(lib->integrity, name, constructor))
+       {
+               DBG1("segment integrity test of plugin '%s' failed", name);
+               dlclose(handle);
+               return NULL;
+       }
        plugin = constructor();
        if (plugin == NULL)
        {
@@ -91,37 +108,22 @@ static plugin_t* load_plugin(private_plugin_loader_t *this,
 static int load(private_plugin_loader_t *this, char *path, char *list)
 {
        plugin_t *plugin;
-       char *pos;
+       enumerator_t *enumerator;
+       char *token;
        int count = 0;
        
-       list = strdupa(list);
-       while (TRUE)
+       enumerator = enumerator_create_token(list, " ", " ");
+       while (enumerator->enumerate(enumerator, &token))
        {
-               pos = strchr(list, ' ');
-               if (pos)
-               {
-                       *pos++ = '\0';
-                       while (*pos == ' ')
-                       {
-                               pos++;
-                       }
-                       if (!*pos)
-                       {
-                               break;
-                       }
-               }
-               plugin = load_plugin(this, path, list);
+               plugin = load_plugin(this, path, token);
                if (plugin)
                {       /* insert in front to destroy them in reverse order */
                        this->plugins->insert_last(this->plugins, plugin);
+                       this->names->insert_last(this->names, strdup(token));
                        count++;
                }
-               if (!pos)
-               {
-                       break;
-               }
-               list = pos;
        }
+       enumerator->destroy(enumerator);
        return count;
 }
 
@@ -131,20 +133,34 @@ static int load(private_plugin_loader_t *this, char *path, char *list)
 static void unload(private_plugin_loader_t *this)
 {
        plugin_t *plugin;
+       char *name;
        
        while (this->plugins->remove_first(this->plugins,
                                                                           (void**)&plugin) == SUCCESS)
        {
                plugin->destroy(plugin);
        }
+       while (this->names->remove_first(this->names, (void**)&name) == SUCCESS)
+       {
+               free(name);
+       }
 }
 
 /**
+ * Implementation of plugin_loader_t.create_plugin_enumerator
+ */
+static enumerator_t* create_plugin_enumerator(private_plugin_loader_t *this)
+{
+       return this->names->create_enumerator(this->names);
+}       
+
+/**
  * Implementation of plugin_loader_t.destroy
  */
 static void destroy(private_plugin_loader_t *this)
 {
        this->plugins->destroy_offset(this->plugins, offsetof(plugin_t, destroy));
+       this->names->destroy_function(this->names, free);
        free(this);
 }
 
@@ -157,9 +173,11 @@ plugin_loader_t *plugin_loader_create()
        
        this->public.load = (int(*)(plugin_loader_t*, char *path, char *prefix))load;
        this->public.unload = (void(*)(plugin_loader_t*))unload;
+       this->public.create_plugin_enumerator = (enumerator_t*(*)(plugin_loader_t*))create_plugin_enumerator;
        this->public.destroy = (void(*)(plugin_loader_t*))destroy;
        
        this->plugins = linked_list_create();
+       this->names = linked_list_create();
        
        return &this->public;
 }