7b69eda1cebc7fffe8f323e7cc7e5b01303c86be
[strongswan.git] / src / libcharon / config / backend_manager.c
1 /*
2 * Copyright (C) 2007-2009 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 #include "backend_manager.h"
17
18 #include <sys/types.h>
19
20 #include <daemon.h>
21 #include <utils/linked_list.h>
22 #include <threading/rwlock.h>
23
24
25 typedef struct private_backend_manager_t private_backend_manager_t;
26
27 /**
28 * Private data of an backend_manager_t object.
29 */
30 struct private_backend_manager_t {
31
32 /**
33 * Public part of backend_manager_t object.
34 */
35 backend_manager_t public;
36
37 /**
38 * list of registered backends
39 */
40 linked_list_t *backends;
41
42 /**
43 * rwlock for backends
44 */
45 rwlock_t *lock;
46 };
47
48 /**
49 * match of an ike_cfg
50 */
51 typedef enum ike_cfg_match_t {
52 MATCH_NONE = 0x00,
53 MATCH_ANY = 0x01,
54 MATCH_ME = 0x04,
55 MATCH_OTHER = 0x08,
56 } ike_cfg_match_t;
57
58 /**
59 * data to pass nested IKE enumerator
60 */
61 typedef struct {
62 private_backend_manager_t *this;
63 host_t *me;
64 host_t *other;
65 } ike_data_t;
66
67 /**
68 * inner enumerator constructor for IKE cfgs
69 */
70 static enumerator_t *ike_enum_create(backend_t *backend, ike_data_t *data)
71 {
72 return backend->create_ike_cfg_enumerator(backend, data->me, data->other);
73 }
74
75 /**
76 * get a match of a candidate ike_cfg for two hosts
77 */
78 static ike_cfg_match_t get_ike_match(ike_cfg_t *cand, host_t *me, host_t *other)
79 {
80 host_t *me_cand, *other_cand;
81 ike_cfg_match_t match = MATCH_NONE;
82
83 if (me)
84 {
85 me_cand = host_create_from_dns(cand->get_my_addr(cand),
86 me->get_family(me), 0);
87 if (!me_cand)
88 {
89 return MATCH_NONE;
90 }
91 if (me_cand->ip_equals(me_cand, me))
92 {
93 match += MATCH_ME;
94 }
95 else if (me_cand->is_anyaddr(me_cand))
96 {
97 match += MATCH_ANY;
98 }
99 me_cand->destroy(me_cand);
100 }
101 else
102 {
103 match += MATCH_ANY;
104 }
105
106 if (other)
107 {
108 other_cand = host_create_from_dns(cand->get_other_addr(cand),
109 other->get_family(other), 0);
110 if (!other_cand)
111 {
112 return MATCH_NONE;
113 }
114 if (other_cand->ip_equals(other_cand, other))
115 {
116 match += MATCH_OTHER;
117 }
118 else if (other_cand->is_anyaddr(other_cand))
119 {
120 match += MATCH_ANY;
121 }
122 other_cand->destroy(other_cand);
123 }
124 else
125 {
126 match += MATCH_ANY;
127 }
128 return match;
129 }
130
131 METHOD(backend_manager_t, get_ike_cfg, ike_cfg_t*,
132 private_backend_manager_t *this, host_t *me, host_t *other)
133 {
134 ike_cfg_t *current, *found = NULL;
135 enumerator_t *enumerator;
136 ike_cfg_match_t match, best = MATCH_ANY;
137 ike_data_t *data;
138
139 data = malloc_thing(ike_data_t);
140 data->this = this;
141 data->me = me;
142 data->other = other;
143
144 DBG2(DBG_CFG, "looking for an ike config for %H...%H", me, other);
145
146 this->lock->read_lock(this->lock);
147 enumerator = enumerator_create_nested(
148 this->backends->create_enumerator(this->backends),
149 (void*)ike_enum_create, data, (void*)free);
150 while (enumerator->enumerate(enumerator, (void**)&current))
151 {
152 match = get_ike_match(current, me, other);
153
154 if (match)
155 {
156 DBG2(DBG_CFG, " candidate: %s...%s, prio %d",
157 current->get_my_addr(current),
158 current->get_other_addr(current), match);
159 if (match > best)
160 {
161 DESTROY_IF(found);
162 found = current;
163 found->get_ref(found);
164 best = match;
165 }
166 }
167 }
168 enumerator->destroy(enumerator);
169 this->lock->unlock(this->lock);
170 if (found)
171 {
172 DBG2(DBG_CFG, "found matching ike config: %s...%s with prio %d",
173 found->get_my_addr(found), found->get_other_addr(found), best);
174 }
175 return found;
176 }
177
178 /**
179 * Get the best ID match in one of the configs auth_cfg
180 */
181 static id_match_t get_peer_match(identification_t *id,
182 peer_cfg_t *cfg, bool local)
183 {
184 enumerator_t *enumerator;
185 auth_cfg_t *auth;
186 identification_t *candidate;
187 id_match_t match = ID_MATCH_NONE;
188
189 if (!id)
190 {
191 return ID_MATCH_ANY;
192 }
193
194 /* compare first auth config only */
195 enumerator = cfg->create_auth_cfg_enumerator(cfg, local);
196 if (enumerator->enumerate(enumerator, &auth))
197 {
198 candidate = auth->get(auth, AUTH_RULE_IDENTITY);
199 if (candidate)
200 {
201 match = id->matches(id, candidate);
202 /* match vice-versa, as the proposed IDr might be ANY */
203 if (!match)
204 {
205 match = candidate->matches(candidate, id);
206 }
207 }
208 else
209 {
210 match = ID_MATCH_ANY;
211 }
212 }
213 enumerator->destroy(enumerator);
214 return match;
215 }
216
217 /**
218 * data to pass nested peer enumerator
219 */
220 typedef struct {
221 rwlock_t *lock;
222 identification_t *me;
223 identification_t *other;
224 } peer_data_t;
225
226 /**
227 * list element to help sorting
228 */
229 typedef struct {
230 id_match_t match_peer;
231 ike_cfg_match_t match_ike;
232 peer_cfg_t *cfg;
233 } match_entry_t;
234
235 /**
236 * inner enumerator constructor for peer cfgs
237 */
238 static enumerator_t *peer_enum_create(backend_t *backend, peer_data_t *data)
239 {
240 return backend->create_peer_cfg_enumerator(backend, data->me, data->other);
241 }
242
243 /**
244 * unlock/cleanup peer enumerator
245 */
246 static void peer_enum_destroy(peer_data_t *data)
247 {
248 data->lock->unlock(data->lock);
249 free(data);
250 }
251
252 /**
253 * convert enumerator value from match_entry to config
254 */
255 static bool peer_enum_filter(linked_list_t *configs,
256 match_entry_t **in, peer_cfg_t **out)
257 {
258 *out = (*in)->cfg;
259 return TRUE;
260 }
261
262 /**
263 * Clean up temporary config list
264 */
265 static void peer_enum_filter_destroy(linked_list_t *configs)
266 {
267 match_entry_t *entry;
268
269 while (configs->remove_last(configs, (void**)&entry) == SUCCESS)
270 {
271 entry->cfg->destroy(entry->cfg);
272 free(entry);
273 }
274 configs->destroy(configs);
275 }
276
277 /**
278 * Insert entry into match-sorted list, using helper
279 */
280 static void insert_sorted(match_entry_t *entry, linked_list_t *list,
281 linked_list_t *helper)
282 {
283 match_entry_t *current;
284
285 while (list->remove_first(list, (void**)&current) == SUCCESS)
286 {
287 helper->insert_last(helper, current);
288 }
289 while (helper->remove_first(helper, (void**)&current) == SUCCESS)
290 {
291 if (entry && (
292 (entry->match_ike > current->match_ike &&
293 entry->match_peer >= current->match_peer) ||
294 (entry->match_ike >= current->match_ike &&
295 entry->match_peer > current->match_peer)))
296 {
297 list->insert_last(list, entry);
298 entry = NULL;
299 }
300 list->insert_last(list, current);
301 }
302 if (entry)
303 {
304 list->insert_last(list, entry);
305 }
306 }
307
308 METHOD(backend_manager_t, create_peer_cfg_enumerator, enumerator_t*,
309 private_backend_manager_t *this, host_t *me, host_t *other,
310 identification_t *my_id, identification_t *other_id)
311 {
312 enumerator_t *enumerator;
313 peer_data_t *data;
314 peer_cfg_t *cfg;
315 linked_list_t *configs, *helper;
316
317 data = malloc_thing(peer_data_t);
318 data->lock = this->lock;
319 data->me = my_id;
320 data->other = other_id;
321
322 /* create a sorted list with all matches */
323 this->lock->read_lock(this->lock);
324 enumerator = enumerator_create_nested(
325 this->backends->create_enumerator(this->backends),
326 (void*)peer_enum_create, data, (void*)peer_enum_destroy);
327
328 if (!me && !other && !my_id && !other_id)
329 { /* shortcut if we are doing a "listall" */
330 return enumerator;
331 }
332
333 DBG1(DBG_CFG, "looking for peer configs matching %H[%Y]...%H[%Y]",
334 me, my_id, other, other_id);
335
336 configs = linked_list_create();
337 /* only once allocated helper list for sorting */
338 helper = linked_list_create();
339 while (enumerator->enumerate(enumerator, &cfg))
340 {
341 id_match_t match_peer_me, match_peer_other;
342 ike_cfg_match_t match_ike;
343 match_entry_t *entry;
344
345 match_peer_me = get_peer_match(my_id, cfg, TRUE);
346 match_peer_other = get_peer_match(other_id, cfg, FALSE);
347 match_ike = get_ike_match(cfg->get_ike_cfg(cfg), me, other);
348
349 if (match_peer_me && match_peer_other && match_ike)
350 {
351 DBG2(DBG_CFG, " candidate \"%s\", match: %d/%d/%d (me/other/ike)",
352 cfg->get_name(cfg), match_peer_me, match_peer_other, match_ike);
353
354 entry = malloc_thing(match_entry_t);
355 entry->match_peer = match_peer_me + match_peer_other;
356 entry->match_ike = match_ike;
357 entry->cfg = cfg->get_ref(cfg);
358 insert_sorted(entry, configs, helper);
359 }
360 }
361 enumerator->destroy(enumerator);
362 helper->destroy(helper);
363
364 return enumerator_create_filter(configs->create_enumerator(configs),
365 (void*)peer_enum_filter, configs,
366 (void*)peer_enum_filter_destroy);
367 }
368
369 METHOD(backend_manager_t, get_peer_cfg_by_name, peer_cfg_t*,
370 private_backend_manager_t *this, char *name)
371 {
372 backend_t *backend;
373 peer_cfg_t *config = NULL;
374 enumerator_t *enumerator;
375
376 this->lock->read_lock(this->lock);
377 enumerator = this->backends->create_enumerator(this->backends);
378 while (config == NULL && enumerator->enumerate(enumerator, (void**)&backend))
379 {
380 config = backend->get_peer_cfg_by_name(backend, name);
381 }
382 enumerator->destroy(enumerator);
383 this->lock->unlock(this->lock);
384 return config;
385 }
386
387 METHOD(backend_manager_t, remove_backend, void,
388 private_backend_manager_t *this, backend_t *backend)
389 {
390 this->lock->write_lock(this->lock);
391 this->backends->remove(this->backends, backend, NULL);
392 this->lock->unlock(this->lock);
393 }
394
395 METHOD(backend_manager_t, add_backend, void,
396 private_backend_manager_t *this, backend_t *backend)
397 {
398 this->lock->write_lock(this->lock);
399 this->backends->insert_last(this->backends, backend);
400 this->lock->unlock(this->lock);
401 }
402
403 METHOD(backend_manager_t, destroy, void,
404 private_backend_manager_t *this)
405 {
406 this->backends->destroy(this->backends);
407 this->lock->destroy(this->lock);
408 free(this);
409 }
410
411 /*
412 * Described in header-file
413
414 */
415 backend_manager_t *backend_manager_create()
416 {
417 private_backend_manager_t *this;
418
419 INIT(this,
420 .public = {
421 .get_ike_cfg = _get_ike_cfg,
422 .get_peer_cfg_by_name = _get_peer_cfg_by_name,
423 .create_peer_cfg_enumerator = _create_peer_cfg_enumerator,
424 .add_backend = _add_backend,
425 .remove_backend = _remove_backend,
426 .destroy = _destroy,
427 },
428 .backends = linked_list_create(),
429 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
430 );
431
432 return &this->public;
433 }
434