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