186273b6ecc3172941cf0f70462d82af46050bc2
[strongswan.git] / src / charon / config / backend_manager.c
1 /**
2 * @file backend_manager.c
3 *
4 * @brief Implementation of backend_manager_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "backend_manager.h"
24
25 #include <sys/types.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <dlfcn.h>
29
30 #include <daemon.h>
31 #include <utils/linked_list.h>
32 #include <config/backends/writeable_backend.h>
33
34
35 typedef struct private_backend_manager_t private_backend_manager_t;
36
37 /**
38 * Private data of an backend_manager_t object.
39 */
40 struct private_backend_manager_t {
41
42 /**
43 * Public part of backend_manager_t object.
44 */
45 backend_manager_t public;
46
47 /**
48 * list of registered backends
49 */
50 linked_list_t *backends;
51
52 /**
53 * Additional list of writable backends.
54 */
55 linked_list_t *writeable;
56
57 /**
58 * List of dlopen() handles we used to open backends
59 */
60 linked_list_t *handles;
61 };
62
63 /**
64 * implements backend_manager_t.get_ike_cfg.
65 */
66 static ike_cfg_t *get_ike_cfg(private_backend_manager_t *this,
67 host_t *my_host, host_t *other_host)
68 {
69 backend_t *backend;
70 ike_cfg_t *config = NULL;
71 iterator_t *iterator = this->backends->create_iterator(this->backends, TRUE);
72 while (config == NULL && iterator->iterate(iterator, (void**)&backend))
73 {
74 config = backend->get_ike_cfg(backend, my_host, other_host);
75 }
76 iterator->destroy(iterator);
77 return config;
78 }
79
80 /**
81 * implements backend_manager_t.get_peer_cfg.
82 */
83 static peer_cfg_t *get_peer_cfg(private_backend_manager_t *this,
84 identification_t *my_id, identification_t *other_id,
85 identification_t *other_ca, char *other_group,
86 host_t *my_host, host_t *other_host)
87 {
88 backend_t *backend;
89 peer_cfg_t *config = NULL;
90 iterator_t *iterator = this->backends->create_iterator(this->backends, TRUE);
91 while (config == NULL && iterator->iterate(iterator, (void**)&backend))
92 {
93 config = backend->get_peer_cfg(backend, my_id, other_id, other_ca,
94 other_group, my_host, other_host);
95 }
96 iterator->destroy(iterator);
97 return config;
98 }
99
100 /**
101 * implements backend_manager_t.add_peer_cfg.
102 */
103 static void add_peer_cfg(private_backend_manager_t *this, peer_cfg_t *config)
104 {
105 writeable_backend_t *backend;
106
107 if (this->writeable->get_first(this->writeable, (void**)&backend) == SUCCESS)
108 {
109 backend->add_cfg(backend, config);
110 }
111 }
112
113 /**
114 * implements backend_manager_t.create_iterator.
115 */
116 static iterator_t* create_iterator(private_backend_manager_t *this)
117 {
118 writeable_backend_t *backend;
119
120 if (this->writeable->get_first(this->writeable, (void**)&backend) == SUCCESS)
121 {
122 return backend->create_iterator(backend);
123 }
124 /* give out an empty iterator if we have no writable backend*/
125 return this->writeable->create_iterator(this->writeable, TRUE);
126 }
127
128 /**
129 * load the configuration backend modules
130 */
131 static void load_backends(private_backend_manager_t *this)
132 {
133 struct dirent* entry;
134 struct stat stb;
135 DIR* dir;
136
137 if (stat(IPSEC_BACKENDDIR, &stb) == -1 || !(stb.st_mode & S_IFDIR))
138 {
139 DBG1(DBG_CFG, "error opening backend modules directory "IPSEC_BACKENDDIR);
140 return;
141 }
142
143 dir = opendir(IPSEC_BACKENDDIR);
144 if (dir == NULL)
145 {
146 DBG1(DBG_CFG, "error opening backend modules directory "IPSEC_BACKENDDIR);
147 return;
148 }
149
150 DBG1(DBG_CFG, "loading backend modules from '"IPSEC_BACKENDDIR"'");
151
152 while ((entry = readdir(dir)) != NULL)
153 {
154 char file[256];
155 backend_t *backend;
156 backend_constructor_t constructor;
157 void *handle;
158 char *ending;
159
160 snprintf(file, sizeof(file), IPSEC_BACKENDDIR"/%s", entry->d_name);
161
162 if (stat(file, &stb) == -1 || !(stb.st_mode & S_IFREG))
163 {
164 DBG2(DBG_CFG, " skipping %s, doesn't look like a file",
165 entry->d_name);
166 continue;
167 }
168 ending = entry->d_name + strlen(entry->d_name) - 3;
169 if (ending <= entry->d_name || !streq(ending, ".so"))
170 {
171 /* skip anything which does not look like a library */
172 DBG2(DBG_CFG, " skipping %s, doesn't look like a library",
173 entry->d_name);
174 continue;
175 }
176 /* try to load the library */
177 handle = dlopen(file, RTLD_LAZY);
178 if (handle == NULL)
179 {
180 DBG1(DBG_CFG, " opening backend module %s failed: %s",
181 entry->d_name, dlerror());
182 continue;
183 }
184 constructor = dlsym(handle, "backend_create");
185 if (constructor == NULL)
186 {
187 DBG1(DBG_CFG, " backend module %s has no backend_create() "
188 "function, skipped", entry->d_name);
189 dlclose(handle);
190 continue;
191 }
192
193 backend = constructor();
194 if (backend == NULL)
195 {
196 DBG1(DBG_CFG, " unable to create instance of backend "
197 "module %s, skipped", entry->d_name);
198 dlclose(handle);
199 continue;
200 }
201 DBG1(DBG_CFG, " loaded backend module successfully from %s", entry->d_name);
202 this->backends->insert_last(this->backends, backend);
203 if (backend->is_writeable(backend))
204 {
205 this->writeable->insert_last(this->writeable, backend);
206 }
207 this->handles->insert_last(this->handles, handle);
208 }
209 closedir(dir);
210 }
211
212 /**
213 * Implementation of backend_manager_t.destroy.
214 */
215 static void destroy(private_backend_manager_t *this)
216 {
217 this->backends->destroy_offset(this->backends, offsetof(backend_t, destroy));
218 this->writeable->destroy(this->writeable);
219 this->handles->destroy_function(this->handles, (void*)dlclose);
220 free(this);
221 }
222
223 /*
224 * Described in header-file
225 */
226 backend_manager_t *backend_manager_create()
227 {
228 private_backend_manager_t *this = malloc_thing(private_backend_manager_t);
229
230 this->public.get_ike_cfg = (ike_cfg_t*(*)(backend_manager_t*, host_t *, host_t *))get_ike_cfg;
231 this->public.get_peer_cfg = (peer_cfg_t*(*)(backend_manager_t*, identification_t *, identification_t *))get_peer_cfg;
232 this->public.add_peer_cfg = (void(*)(backend_manager_t*, peer_cfg_t*))add_peer_cfg;
233 this->public.create_iterator = (iterator_t*(*)(backend_manager_t*))create_iterator;
234 this->public.destroy = (void(*)(backend_manager_t*))destroy;
235
236 this->backends = linked_list_create();
237 this->writeable = linked_list_create();
238 this->handles = linked_list_create();
239
240 load_backends(this);
241
242 return &this->public;
243 }
244