streamlined debug output of integrity tests
[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"
70 " 'libstrongswan-%s.so'", name, name);
71 return NULL;
72 }
73 DBG1("plugin '%s': passed file integrity test of"
74 " 'libstrongswan-%s.so'", name, name);
75 }
76 handle = dlopen(file, RTLD_LAZY);
77 if (handle == NULL)
78 {
79 DBG1("plugin '%s': failed to load '%s' - %s", name, file, dlerror());
80 return NULL;
81 }
82 constructor = dlsym(handle, "plugin_create");
83 if (constructor == NULL)
84 {
85 DBG1("plugin '%s': failed to load - no plugin_create() function", name);
86 dlclose(handle);
87 return NULL;
88 }
89 if (lib->integrity)
90 {
91 if (!lib->integrity->check_segment(lib->integrity, name, constructor))
92 {
93 DBG1("plugin '%s': failed segment integrity test", name);
94 dlclose(handle);
95 return NULL;
96 }
97 DBG1("plugin '%s': passed segment integrity test", name);
98 }
99 plugin = constructor();
100 if (plugin == NULL)
101 {
102 DBG1("plugin '%s': failed to load - plugin_create() returned NULL", name);
103 dlclose(handle);
104 return NULL;
105 }
106 DBG2("plugin '%s': loaded successfully", name);
107
108 /* we do not store or free dlopen() handles, leak_detective requires
109 * the modules to keep loaded until leak report */
110 return plugin;
111 }
112
113 /**
114 * Implementation of plugin_loader_t.load_plugins.
115 */
116 static int load(private_plugin_loader_t *this, char *path, char *list)
117 {
118 plugin_t *plugin;
119 enumerator_t *enumerator;
120 char *token;
121 int count = 0;
122
123 enumerator = enumerator_create_token(list, " ", " ");
124 while (enumerator->enumerate(enumerator, &token))
125 {
126 plugin = load_plugin(this, path, token);
127 if (plugin)
128 { /* insert in front to destroy them in reverse order */
129 this->plugins->insert_last(this->plugins, plugin);
130 this->names->insert_last(this->names, strdup(token));
131 count++;
132 }
133 }
134 enumerator->destroy(enumerator);
135 return count;
136 }
137
138 /**
139 * Implementation of plugin_loader_t.unload
140 */
141 static void unload(private_plugin_loader_t *this)
142 {
143 plugin_t *plugin;
144 char *name;
145
146 while (this->plugins->remove_first(this->plugins,
147 (void**)&plugin) == SUCCESS)
148 {
149 plugin->destroy(plugin);
150 }
151 while (this->names->remove_first(this->names, (void**)&name) == SUCCESS)
152 {
153 free(name);
154 }
155 }
156
157 /**
158 * Implementation of plugin_loader_t.create_plugin_enumerator
159 */
160 static enumerator_t* create_plugin_enumerator(private_plugin_loader_t *this)
161 {
162 return this->names->create_enumerator(this->names);
163 }
164
165 /**
166 * Implementation of plugin_loader_t.destroy
167 */
168 static void destroy(private_plugin_loader_t *this)
169 {
170 this->plugins->destroy_offset(this->plugins, offsetof(plugin_t, destroy));
171 this->names->destroy_function(this->names, free);
172 free(this);
173 }
174
175 /*
176 * see header file
177 */
178 plugin_loader_t *plugin_loader_create()
179 {
180 private_plugin_loader_t *this = malloc_thing(private_plugin_loader_t);
181
182 this->public.load = (int(*)(plugin_loader_t*, char *path, char *prefix))load;
183 this->public.unload = (void(*)(plugin_loader_t*))unload;
184 this->public.create_plugin_enumerator = (enumerator_t*(*)(plugin_loader_t*))create_plugin_enumerator;
185 this->public.destroy = (void(*)(plugin_loader_t*))destroy;
186
187 this->plugins = linked_list_create();
188 this->names = linked_list_create();
189
190 return &this->public;
191 }
192