streamlined integrity test output some more
[strongswan.git] / src / libstrongswan / plugins / plugin_loader.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17 #include "plugin_loader.h"
18
19 #include <string.h>
20 #include <dlfcn.h>
21 #include <limits.h>
22 #include <stdio.h>
23
24 #include <debug.h>
25 #include <integrity_checker.h>
26 #include <utils/linked_list.h>
27 #include <plugins/plugin.h>
28
29 typedef struct private_plugin_loader_t private_plugin_loader_t;
30
31 /**
32 * private data of plugin_loader
33 */
34 struct private_plugin_loader_t {
35
36 /**
37 * public functions
38 */
39 plugin_loader_t public;
40
41 /**
42 * list of loaded plugins
43 */
44 linked_list_t *plugins;
45
46 /**
47 * names of loaded plugins
48 */
49 linked_list_t *names;
50 };
51
52 /**
53 * load a single plugin
54 */
55 static plugin_t* load_plugin(private_plugin_loader_t *this,
56 char *path, char *name)
57 {
58 char file[PATH_MAX];
59 void *handle;
60 plugin_t *plugin;
61 plugin_constructor_t constructor;
62
63 snprintf(file, sizeof(file), "%s/libstrongswan-%s.so", path, name);
64
65 if (lib->integrity)
66 {
67 if (!lib->integrity->check_file(lib->integrity, name, file))
68 {
69 DBG1("plugin '%s': failed file integrity test of '%s'", name, file);
70 return NULL;
71 }
72 }
73 handle = dlopen(file, RTLD_LAZY);
74 if (handle == NULL)
75 {
76 DBG1("plugin '%s': failed to load '%s' - %s", name, file, dlerror());
77 return NULL;
78 }
79 constructor = dlsym(handle, "plugin_create");
80 if (constructor == NULL)
81 {
82 DBG1("plugin '%s': failed to load - no plugin_create() function", name);
83 dlclose(handle);
84 return NULL;
85 }
86 if (lib->integrity)
87 {
88 if (!lib->integrity->check_segment(lib->integrity, name, constructor))
89 {
90 DBG1("plugin '%s': failed segment integrity test", name);
91 dlclose(handle);
92 return NULL;
93 }
94 DBG1("plugin '%s': passed file and segment integrity tests", name);
95 }
96 plugin = constructor();
97 if (plugin == NULL)
98 {
99 DBG1("plugin '%s': failed to load - plugin_create() returned NULL", name);
100 dlclose(handle);
101 return NULL;
102 }
103 DBG2("plugin '%s': loaded successfully", name);
104
105 /* we do not store or free dlopen() handles, leak_detective requires
106 * the modules to keep loaded until leak report */
107 return plugin;
108 }
109
110 /**
111 * Implementation of plugin_loader_t.load_plugins.
112 */
113 static int load(private_plugin_loader_t *this, char *path, char *list)
114 {
115 plugin_t *plugin;
116 enumerator_t *enumerator;
117 char *token;
118 int count = 0;
119
120 enumerator = enumerator_create_token(list, " ", " ");
121 while (enumerator->enumerate(enumerator, &token))
122 {
123 plugin = load_plugin(this, path, token);
124 if (plugin)
125 { /* insert in front to destroy them in reverse order */
126 this->plugins->insert_last(this->plugins, plugin);
127 this->names->insert_last(this->names, strdup(token));
128 count++;
129 }
130 }
131 enumerator->destroy(enumerator);
132 return count;
133 }
134
135 /**
136 * Implementation of plugin_loader_t.unload
137 */
138 static void unload(private_plugin_loader_t *this)
139 {
140 plugin_t *plugin;
141 char *name;
142
143 while (this->plugins->remove_first(this->plugins,
144 (void**)&plugin) == SUCCESS)
145 {
146 plugin->destroy(plugin);
147 }
148 while (this->names->remove_first(this->names, (void**)&name) == SUCCESS)
149 {
150 free(name);
151 }
152 }
153
154 /**
155 * Implementation of plugin_loader_t.create_plugin_enumerator
156 */
157 static enumerator_t* create_plugin_enumerator(private_plugin_loader_t *this)
158 {
159 return this->names->create_enumerator(this->names);
160 }
161
162 /**
163 * Implementation of plugin_loader_t.destroy
164 */
165 static void destroy(private_plugin_loader_t *this)
166 {
167 this->plugins->destroy_offset(this->plugins, offsetof(plugin_t, destroy));
168 this->names->destroy_function(this->names, free);
169 free(this);
170 }
171
172 /*
173 * see header file
174 */
175 plugin_loader_t *plugin_loader_create()
176 {
177 private_plugin_loader_t *this = malloc_thing(private_plugin_loader_t);
178
179 this->public.load = (int(*)(plugin_loader_t*, char *path, char *prefix))load;
180 this->public.unload = (void(*)(plugin_loader_t*))unload;
181 this->public.create_plugin_enumerator = (enumerator_t*(*)(plugin_loader_t*))create_plugin_enumerator;
182 this->public.destroy = (void(*)(plugin_loader_t*))destroy;
183
184 this->plugins = linked_list_create();
185 this->names = linked_list_create();
186
187 return &this->public;
188 }
189