prototype implemementation of an sqlite configuration backend
[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 ca_info_t *other_ca_info)
86 {
87 backend_t *backend;
88 peer_cfg_t *config = NULL;
89 iterator_t *iterator = this->backends->create_iterator(this->backends, TRUE);
90 while (config == NULL && iterator->iterate(iterator, (void**)&backend))
91 {
92 config = backend->get_peer_cfg(backend, my_id, other_id, other_ca_info);
93 }
94 iterator->destroy(iterator);
95 return config;
96 }
97
98 /**
99 * implements backend_manager_t.get_peer_cfg_by_name.
100 */
101 static peer_cfg_t *get_peer_cfg_by_name(private_backend_manager_t *this, char *name)
102 {
103 backend_t *backend;
104 peer_cfg_t *config = NULL;
105 iterator_t *iterator = this->backends->create_iterator(this->backends, TRUE);
106 while (config == NULL && iterator->iterate(iterator, (void**)&backend))
107 {
108 config = backend->get_peer_cfg_by_name(backend, name);
109 }
110 iterator->destroy(iterator);
111 return config;
112 }
113
114 /**
115 * implements backend_manager_t.add_peer_cfg.
116 */
117 static void add_peer_cfg(private_backend_manager_t *this, peer_cfg_t *config)
118 {
119 writeable_backend_t *backend;
120
121 if (this->writeable->get_first(this->writeable, (void**)&backend) == SUCCESS)
122 {
123 backend->add_cfg(backend, config);
124 }
125 }
126
127 /**
128 * implements backend_manager_t.create_iterator.
129 */
130 static iterator_t* create_iterator(private_backend_manager_t *this)
131 {
132 writeable_backend_t *backend;
133
134 if (this->writeable->get_first(this->writeable, (void**)&backend) == SUCCESS)
135 {
136 return backend->create_iterator(backend);
137 }
138 /* give out an empty iterator if we have no writable backend*/
139 return this->writeable->create_iterator(this->writeable, TRUE);
140 }
141
142 /**
143 * load the configuration backend modules
144 */
145 static void load_backends(private_backend_manager_t *this)
146 {
147 struct dirent* entry;
148 DIR* dir;
149
150 dir = opendir(IPSEC_BACKENDDIR);
151 if (dir == NULL)
152 {
153 DBG1(DBG_CFG, "error opening backend modules directory "IPSEC_BACKENDDIR);
154 return;
155 }
156
157 DBG1(DBG_CFG, "loading backend modules from '"IPSEC_BACKENDDIR"'");
158
159 while ((entry = readdir(dir)) != NULL)
160 {
161 char file[256];
162 backend_t *backend;
163 backend_constructor_t constructor;
164 void *handle;
165 char *ending;
166
167 snprintf(file, sizeof(file), IPSEC_BACKENDDIR"/%s", entry->d_name);
168
169 ending = entry->d_name + strlen(entry->d_name) - 3;
170 if (ending <= entry->d_name || !streq(ending, ".so"))
171 {
172 /* skip anything which does not look like a library */
173 DBG2(DBG_CFG, " skipping %s, doesn't look like a library",
174 entry->d_name);
175 continue;
176 }
177 /* try to load the library */
178 handle = dlopen(file, RTLD_LAZY);
179 if (handle == NULL)
180 {
181 DBG1(DBG_CFG, " opening backend module %s failed: %s",
182 entry->d_name, dlerror());
183 continue;
184 }
185 constructor = dlsym(handle, "backend_create");
186 if (constructor == NULL)
187 {
188 DBG1(DBG_CFG, " backend module %s has no backend_create() "
189 "function, skipped", entry->d_name);
190 dlclose(handle);
191 continue;
192 }
193
194 backend = constructor();
195 if (backend == NULL)
196 {
197 DBG1(DBG_CFG, " unable to create instance of backend "
198 "module %s, skipped", entry->d_name);
199 dlclose(handle);
200 continue;
201 }
202 DBG1(DBG_CFG, " loaded backend module successfully from %s", entry->d_name);
203 this->backends->insert_last(this->backends, backend);
204 if (backend->is_writeable(backend))
205 {
206 this->writeable->insert_last(this->writeable, backend);
207 }
208 this->handles->insert_last(this->handles, handle);
209 }
210 closedir(dir);
211 }
212
213 /**
214 * Implementation of backend_manager_t.destroy.
215 */
216 static void destroy(private_backend_manager_t *this)
217 {
218 this->backends->destroy_offset(this->backends, offsetof(backend_t, destroy));
219 this->writeable->destroy(this->writeable);
220 this->handles->destroy_function(this->handles, (void*)dlclose);
221 free(this);
222 }
223
224 /*
225 * Described in header-file
226 */
227 backend_manager_t *backend_manager_create()
228 {
229 private_backend_manager_t *this = malloc_thing(private_backend_manager_t);
230
231 this->public.get_ike_cfg = (ike_cfg_t* (*)(backend_manager_t*, host_t*, host_t*))get_ike_cfg;
232 this->public.get_peer_cfg = (peer_cfg_t* (*)(backend_manager_t*,identification_t*,identification_t*,ca_info_t*))get_peer_cfg;
233 this->public.get_peer_cfg_by_name = (peer_cfg_t* (*)(backend_manager_t*,char*))get_peer_cfg_by_name;
234 this->public.add_peer_cfg = (void (*)(backend_manager_t*,peer_cfg_t*))add_peer_cfg;
235 this->public.create_iterator = (iterator_t* (*)(backend_manager_t*))create_iterator;
236 this->public.destroy = (void (*)(backend_manager_t*))destroy;
237
238 this->backends = linked_list_create();
239 this->writeable = linked_list_create();
240 this->handles = linked_list_create();
241
242 load_backends(this);
243
244 return &this->public;
245 }
246