Adding DBG_LIB to all calls of libstrongswan's version of DBG*.
[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 #ifdef MONOLITHIC
54 /**
55 * load a single plugin in monolithic mode
56 */
57 static plugin_t* load_plugin(private_plugin_loader_t *this,
58 char *path, char *name)
59 {
60 char create[128];
61 plugin_t *plugin;
62 plugin_constructor_t constructor;
63
64 if (snprintf(create, sizeof(create), "%s_plugin_create",
65 name) >= sizeof(create))
66 {
67 return NULL;
68 }
69 translate(create, "-", "_");
70 constructor = dlsym(RTLD_DEFAULT, create);
71 if (constructor == NULL)
72 {
73 DBG1(DBG_LIB, "plugin '%s': failed to load - %s not found", name,
74 create);
75 return NULL;
76 }
77 plugin = constructor();
78 if (plugin == NULL)
79 {
80 DBG1(DBG_LIB, "plugin '%s': failed to load - %s returned NULL", name,
81 create);
82 return NULL;
83 }
84 DBG2(DBG_LIB, "plugin '%s': loaded successfully", name);
85
86 return plugin;
87 }
88 #else
89 /**
90 * load a single plugin
91 */
92 static plugin_t* load_plugin(private_plugin_loader_t *this,
93 char *path, char *name)
94 {
95 char create[128];
96 char file[PATH_MAX];
97 void *handle;
98 plugin_t *plugin;
99 plugin_constructor_t constructor;
100
101 if (snprintf(file, sizeof(file), "%s/libstrongswan-%s.so", path,
102 name) >= sizeof(file) ||
103 snprintf(create, sizeof(create), "%s_plugin_create",
104 name) >= sizeof(create))
105 {
106 return NULL;
107 }
108 translate(create, "-", "_");
109 if (lib->integrity)
110 {
111 if (!lib->integrity->check_file(lib->integrity, name, file))
112 {
113 DBG1(DBG_LIB, "plugin '%s': failed file integrity test of '%s'",
114 name, file);
115 return NULL;
116 }
117 }
118 handle = dlopen(file, RTLD_LAZY);
119 if (handle == NULL)
120 {
121 DBG1(DBG_LIB, "plugin '%s': failed to load '%s' - %s", name, file,
122 dlerror());
123 return NULL;
124 }
125 constructor = dlsym(handle, create);
126 if (constructor == NULL)
127 {
128 DBG1(DBG_LIB, "plugin '%s': failed to load - %s not found", name,
129 create);
130 dlclose(handle);
131 return NULL;
132 }
133 if (lib->integrity)
134 {
135 if (!lib->integrity->check_segment(lib->integrity, name, constructor))
136 {
137 DBG1(DBG_LIB, "plugin '%s': failed segment integrity test", name);
138 dlclose(handle);
139 return NULL;
140 }
141 DBG1(DBG_LIB, "plugin '%s': passed file and segment integrity tests",
142 name);
143 }
144 plugin = constructor();
145 if (plugin == NULL)
146 {
147 DBG1(DBG_LIB, "plugin '%s': failed to load - %s returned NULL", name,
148 create);
149 dlclose(handle);
150 return NULL;
151 }
152 DBG2(DBG_LIB, "plugin '%s': loaded successfully", name);
153
154 /* we do not store or free dlopen() handles, leak_detective requires
155 * the modules to keep loaded until leak report */
156 return plugin;
157 }
158 #endif
159
160 /**
161 * Implementation of plugin_loader_t.load_plugins.
162 */
163 static bool load(private_plugin_loader_t *this, char *path, char *list)
164 {
165 enumerator_t *enumerator;
166 char *token;
167 bool critical_failed = FALSE;
168
169 #ifndef MONOLITHIC
170 if (path == NULL)
171 {
172 path = PLUGINDIR;
173 }
174 #endif
175
176 enumerator = enumerator_create_token(list, " ", " ");
177 while (!critical_failed && enumerator->enumerate(enumerator, &token))
178 {
179 plugin_t *plugin;
180 bool critical = FALSE;
181 int len;
182
183 token = strdup(token);
184 len = strlen(token);
185 if (token[len-1] == '!')
186 {
187 critical = TRUE;
188 token[len-1] = '\0';
189 }
190 plugin = load_plugin(this, path, token);
191 if (plugin)
192 {
193 /* insert in front to destroy them in reverse order */
194 this->plugins->insert_last(this->plugins, plugin);
195 this->names->insert_last(this->names, token);
196 }
197 else
198 {
199 if (critical)
200 {
201 critical_failed = TRUE;
202 DBG1(DBG_LIB, "loading critical plugin '%s' failed", token);
203 }
204 free(token);
205 }
206 }
207 enumerator->destroy(enumerator);
208 return !critical_failed;
209 }
210
211 /**
212 * Implementation of plugin_loader_t.unload
213 */
214 static void unload(private_plugin_loader_t *this)
215 {
216 plugin_t *plugin;
217 char *name;
218
219 while (this->plugins->remove_first(this->plugins,
220 (void**)&plugin) == SUCCESS)
221 {
222 plugin->destroy(plugin);
223 }
224 while (this->names->remove_first(this->names, (void**)&name) == SUCCESS)
225 {
226 free(name);
227 }
228 }
229
230 /**
231 * Implementation of plugin_loader_t.create_plugin_enumerator
232 */
233 static enumerator_t* create_plugin_enumerator(private_plugin_loader_t *this)
234 {
235 return this->names->create_enumerator(this->names);
236 }
237
238 /**
239 * Implementation of plugin_loader_t.destroy
240 */
241 static void destroy(private_plugin_loader_t *this)
242 {
243 this->plugins->destroy_offset(this->plugins, offsetof(plugin_t, destroy));
244 this->names->destroy_function(this->names, free);
245 free(this);
246 }
247
248 /*
249 * see header file
250 */
251 plugin_loader_t *plugin_loader_create()
252 {
253 private_plugin_loader_t *this = malloc_thing(private_plugin_loader_t);
254
255 this->public.load = (bool(*)(plugin_loader_t*, char *path, char *prefix))load;
256 this->public.unload = (void(*)(plugin_loader_t*))unload;
257 this->public.create_plugin_enumerator = (enumerator_t*(*)(plugin_loader_t*))create_plugin_enumerator;
258 this->public.destroy = (void(*)(plugin_loader_t*))destroy;
259
260 this->plugins = linked_list_create();
261 this->names = linked_list_create();
262
263 return &this->public;
264 }
265