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