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