Adapted the config backends to the new lifetime configuration.
[strongswan.git] / src / charon / plugins / sql / sql_config.c
1 /*
2 * Copyright (C) 2006-2008 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 <string.h>
17
18 #include "sql_config.h"
19
20 #include <daemon.h>
21
22 typedef struct private_sql_config_t private_sql_config_t;
23
24 /**
25 * Private data of an sql_config_t object
26 */
27 struct private_sql_config_t {
28
29 /**
30 * Public part
31 */
32 sql_config_t public;
33
34 /**
35 * database connection
36 */
37 database_t *db;
38 };
39
40 /**
41 * forward declaration
42 */
43 static peer_cfg_t *build_peer_cfg(private_sql_config_t *this, enumerator_t *e,
44 identification_t *me, identification_t *other);
45
46 /**
47 * build a traffic selector from a SQL query
48 */
49 static traffic_selector_t *build_traffic_selector(private_sql_config_t *this,
50 enumerator_t *e, bool *local)
51 {
52 int type, protocol, start_port, end_port;
53 chunk_t start_addr, end_addr;
54 traffic_selector_t *ts;
55 enum {
56 TS_LOCAL = 0,
57 TS_REMOTE = 1,
58 TS_LOCAL_DYNAMIC = 2,
59 TS_REMOTE_DYNAMIC = 3,
60 } kind;
61
62 while (e->enumerate(e, &kind, &type, &protocol,
63 &start_addr, &end_addr, &start_port, &end_port))
64 {
65 *local = FALSE;
66 switch (kind)
67 {
68 case TS_LOCAL:
69 *local = TRUE;
70 /* FALL */
71 case TS_REMOTE:
72 ts = traffic_selector_create_from_bytes(protocol, type,
73 start_addr, start_port, end_addr, end_port);
74 break;
75 case TS_LOCAL_DYNAMIC:
76 *local = TRUE;
77 /* FALL */
78 case TS_REMOTE_DYNAMIC:
79 ts = traffic_selector_create_dynamic(protocol,
80 start_port, end_port);
81 break;
82 default:
83 continue;
84 }
85 if (ts)
86 {
87 return ts;
88 }
89 }
90 return NULL;
91 }
92
93 /**
94 * Add traffic selectors to a child config
95 */
96 static void add_traffic_selectors(private_sql_config_t *this,
97 child_cfg_t *child, int id)
98 {
99 enumerator_t *e;
100 traffic_selector_t *ts;
101 bool local;
102
103 e = this->db->query(this->db,
104 "SELECT kind, type, protocol, "
105 "start_addr, end_addr, start_port, end_port "
106 "FROM traffic_selectors JOIN child_config_traffic_selector "
107 "ON id = traffic_selector WHERE child_cfg = ?",
108 DB_INT, id,
109 DB_INT, DB_INT, DB_INT,
110 DB_BLOB, DB_BLOB, DB_INT, DB_INT);
111 if (e)
112 {
113 while ((ts = build_traffic_selector(this, e, &local)))
114 {
115 child->add_traffic_selector(child, local, ts);
116 }
117 e->destroy(e);
118 }
119 }
120
121 /**
122 * build a Child configuration from a SQL query
123 */
124 static child_cfg_t *build_child_cfg(private_sql_config_t *this, enumerator_t *e)
125 {
126 int id, lifetime, rekeytime, jitter, hostaccess, mode, dpd, close, ipcomp;
127 char *name, *updown;
128 child_cfg_t *child_cfg;
129
130 if (e->enumerate(e, &id, &name, &lifetime, &rekeytime, &jitter,
131 &updown, &hostaccess, &mode, &dpd, &close, &ipcomp))
132 {
133 lifetime_cfg_t *lft = lifetime_cfg_create_time(lifetime, rekeytime,
134 jitter);
135 child_cfg = child_cfg_create(name, lft, updown, hostaccess, mode,
136 dpd, close, ipcomp);
137 /* TODO: read proposal from db */
138 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
139 add_traffic_selectors(this, child_cfg, id);
140 return child_cfg;
141 }
142 return NULL;
143 }
144
145 /**
146 * Add child configs to peer config
147 */
148 static void add_child_cfgs(private_sql_config_t *this, peer_cfg_t *peer, int id)
149 {
150 enumerator_t *e;
151 child_cfg_t *child_cfg;
152
153 e = this->db->query(this->db,
154 "SELECT id, name, lifetime, rekeytime, jitter, "
155 "updown, hostaccess, mode, dpd_action, close_action, ipcomp "
156 "FROM child_configs JOIN peer_config_child_config ON id = child_cfg "
157 "WHERE peer_cfg = ?",
158 DB_INT, id,
159 DB_INT, DB_TEXT, DB_INT, DB_INT, DB_INT,
160 DB_TEXT, DB_INT, DB_INT, DB_INT, DB_INT, DB_INT);
161 if (e)
162 {
163 while ((child_cfg = build_child_cfg(this, e)))
164 {
165 peer->add_child_cfg(peer, child_cfg);
166 }
167 e->destroy(e);
168 }
169 }
170
171 /**
172 * build a ike configuration from a SQL query
173 */
174 static ike_cfg_t *build_ike_cfg(private_sql_config_t *this, enumerator_t *e,
175 host_t *my_host, host_t *other_host)
176 {
177 int certreq, force_encap;
178 char *local, *remote;
179
180 while (e->enumerate(e, &certreq, &force_encap, &local, &remote))
181 {
182 ike_cfg_t *ike_cfg;
183
184 ike_cfg = ike_cfg_create(certreq, force_encap, local, remote);
185 /* TODO: read proposal from db */
186 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
187 return ike_cfg;
188 }
189 return NULL;
190 }
191
192 /**
193 * Query a IKE config by its id
194 */
195 static ike_cfg_t* get_ike_cfg_by_id(private_sql_config_t *this, int id)
196 {
197 enumerator_t *e;
198 ike_cfg_t *ike_cfg = NULL;
199
200 e = this->db->query(this->db,
201 "SELECT certreq, force_encap, local, remote "
202 "FROM ike_configs WHERE id = ?",
203 DB_INT, id,
204 DB_INT, DB_INT, DB_TEXT, DB_TEXT);
205 if (e)
206 {
207 ike_cfg = build_ike_cfg(this, e, NULL, NULL);
208 e->destroy(e);
209 }
210 return ike_cfg;
211 }
212
213 /**
214 * Query a peer config by its id
215 */
216 static peer_cfg_t *get_peer_cfg_by_id(private_sql_config_t *this, int id)
217 {
218 enumerator_t *e;
219 peer_cfg_t *peer_cfg = NULL;
220
221 e = this->db->query(this->db,
222 "SELECT c.id, name, ike_cfg, l.type, l.data, r.type, r.data, "
223 "cert_policy, uniqueid, auth_method, eap_type, eap_vendor, "
224 "keyingtries, rekeytime, reauthtime, jitter, overtime, mobike, "
225 "dpd_delay, virtual, pool, "
226 "mediation, mediated_by, COALESCE(p.type, 0), p.data "
227 "FROM peer_configs AS c "
228 "JOIN identities AS l ON local_id = l.id "
229 "JOIN identities AS r ON remote_id = r.id "
230 "LEFT JOIN identities AS p ON peer_id = p.id "
231 "WHERE id = ?",
232 DB_INT, id,
233 DB_INT, DB_TEXT, DB_INT, DB_INT, DB_BLOB, DB_INT, DB_BLOB,
234 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
235 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
236 DB_INT, DB_TEXT, DB_TEXT,
237 DB_INT, DB_INT, DB_INT, DB_BLOB);
238 if (e)
239 {
240 peer_cfg = build_peer_cfg(this, e, NULL, NULL);
241 e->destroy(e);
242 }
243 return peer_cfg;
244 }
245
246 /**
247 * build a peer configuration from a SQL query
248 */
249 static peer_cfg_t *build_peer_cfg(private_sql_config_t *this, enumerator_t *e,
250 identification_t *me, identification_t *other)
251 {
252 int id, ike_cfg, l_type, r_type,
253 cert_policy, uniqueid, auth_method, eap_type, eap_vendor, keyingtries,
254 rekeytime, reauthtime, jitter, overtime, mobike, dpd_delay,
255 mediation, mediated_by, p_type;
256 chunk_t l_data, r_data, p_data;
257 char *name, *virtual, *pool;
258
259 while (e->enumerate(e,
260 &id, &name, &ike_cfg, &l_type, &l_data, &r_type, &r_data,
261 &cert_policy, &uniqueid, &auth_method, &eap_type, &eap_vendor,
262 &keyingtries, &rekeytime, &reauthtime, &jitter, &overtime, &mobike,
263 &dpd_delay, &virtual, &pool,
264 &mediation, &mediated_by, &p_type, &p_data))
265 {
266 identification_t *local_id, *remote_id, *peer_id = NULL;
267 peer_cfg_t *peer_cfg, *mediated_cfg;
268 ike_cfg_t *ike;
269 host_t *vip = NULL;
270 auth_cfg_t *auth;
271
272 local_id = identification_create_from_encoding(l_type, l_data);
273 remote_id = identification_create_from_encoding(r_type, r_data);
274 if ((me && !me->matches(me, local_id)) ||
275 (other && !other->matches(other, remote_id)))
276 {
277 local_id->destroy(local_id);
278 remote_id->destroy(remote_id);
279 continue;
280 }
281 ike = get_ike_cfg_by_id(this, ike_cfg);
282 mediated_cfg = mediated_by ? get_peer_cfg_by_id(this, mediated_by) : NULL;
283 if (p_type)
284 {
285 peer_id = identification_create_from_encoding(p_type, p_data);
286 }
287 if (virtual)
288 {
289 vip = host_create_from_string(virtual, 0);
290 }
291 if (ike)
292 {
293 peer_cfg = peer_cfg_create(
294 name, 2, ike, cert_policy, uniqueid,
295 keyingtries, rekeytime, reauthtime, jitter, overtime,
296 mobike, dpd_delay, vip, pool,
297 mediation, mediated_cfg, peer_id);
298 auth = auth_cfg_create();
299 auth->add(auth, AUTH_RULE_AUTH_CLASS, auth_method);
300 auth->add(auth, AUTH_RULE_IDENTITY, local_id);
301 peer_cfg->add_auth_cfg(peer_cfg, auth, TRUE);
302 auth = auth_cfg_create();
303 auth->add(auth, AUTH_RULE_IDENTITY, remote_id);
304 if (eap_type)
305 {
306 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_EAP);
307 auth->add(auth, AUTH_RULE_EAP_TYPE, eap_type);
308 if (eap_vendor)
309 {
310 auth->add(auth, AUTH_RULE_EAP_VENDOR, eap_vendor);
311 }
312 }
313 peer_cfg->add_auth_cfg(peer_cfg, auth, FALSE);
314 add_child_cfgs(this, peer_cfg, id);
315 return peer_cfg;
316 }
317 DESTROY_IF(ike);
318 DESTROY_IF(mediated_cfg);
319 DESTROY_IF(peer_id);
320 DESTROY_IF(local_id);
321 DESTROY_IF(remote_id);
322 }
323 return NULL;
324 }
325
326 /**
327 * implements backend_t.get_peer_cfg_by_name.
328 */
329 static peer_cfg_t *get_peer_cfg_by_name(private_sql_config_t *this, char *name)
330 {
331 enumerator_t *e;
332 peer_cfg_t *peer_cfg = NULL;
333
334 e = this->db->query(this->db,
335 "SELECT c.id, name, ike_cfg, l.type, l.data, r.type, r.data, "
336 "cert_policy, uniqueid, auth_method, eap_type, eap_vendor, "
337 "keyingtries, rekeytime, reauthtime, jitter, overtime, mobike, "
338 "dpd_delay, virtual, pool, "
339 "mediation, mediated_by, COALESCE(p.type, 0), p.data "
340 "FROM peer_configs AS c "
341 "JOIN identities AS l ON local_id = l.id "
342 "JOIN identities AS r ON remote_id = r.id "
343 "LEFT JOIN identities AS p ON peer_id = p.id "
344 "WHERE ike_version = ? AND name = ?",
345 DB_INT, 2, DB_TEXT, name,
346 DB_INT, DB_TEXT, DB_INT, DB_INT, DB_BLOB, DB_INT, DB_BLOB,
347 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
348 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
349 DB_INT, DB_TEXT, DB_TEXT,
350 DB_INT, DB_INT, DB_INT, DB_BLOB);
351 if (e)
352 {
353 peer_cfg = build_peer_cfg(this, e, NULL, NULL);
354 e->destroy(e);
355 }
356 return peer_cfg;
357 }
358
359 typedef struct {
360 /** implements enumerator */
361 enumerator_t public;
362 /** reference to context */
363 private_sql_config_t *this;
364 /** filtering own host */
365 host_t *me;
366 /** filtering remote host */
367 host_t *other;
368 /** inner SQL enumerator */
369 enumerator_t *inner;
370 /** currently enumerated peer config */
371 ike_cfg_t *current;
372 } ike_enumerator_t;
373
374 /**
375 * Implementation of ike_enumerator_t.public.enumerate
376 */
377 static bool ike_enumerator_enumerate(ike_enumerator_t *this, ike_cfg_t **cfg)
378 {
379 DESTROY_IF(this->current);
380 this->current = build_ike_cfg(this->this, this->inner, this->me, this->other);
381 if (this->current)
382 {
383 *cfg = this->current;
384 return TRUE;
385 }
386 return FALSE;
387 }
388
389 /**
390 * Implementation of ike_enumerator_t.public.destroy
391 */
392 static void ike_enumerator_destroy(ike_enumerator_t *this)
393 {
394 DESTROY_IF(this->current);
395 this->inner->destroy(this->inner);
396 free(this);
397 }
398
399 /**
400 * Implementation of backend_t.create_ike_cfg_enumerator.
401 */
402 static enumerator_t* create_ike_cfg_enumerator(private_sql_config_t *this,
403 host_t *me, host_t *other)
404 {
405 ike_enumerator_t *e = malloc_thing(ike_enumerator_t);
406
407 e->this = this;
408 e->me = me;
409 e->other = other;
410 e->current = NULL;
411 e->public.enumerate = (void*)ike_enumerator_enumerate;
412 e->public.destroy = (void*)ike_enumerator_destroy;
413
414 e->inner = this->db->query(this->db,
415 "SELECT certreq, force_encap, local, remote "
416 "FROM ike_configs",
417 DB_INT, DB_INT, DB_TEXT, DB_TEXT);
418 if (!e->inner)
419 {
420 free(e);
421 return NULL;
422 }
423 return &e->public;
424 }
425
426
427 typedef struct {
428 /** implements enumerator */
429 enumerator_t public;
430 /** reference to context */
431 private_sql_config_t *this;
432 /** filtering own identity */
433 identification_t *me;
434 /** filtering remote identity */
435 identification_t *other;
436 /** inner SQL enumerator */
437 enumerator_t *inner;
438 /** currently enumerated peer config */
439 peer_cfg_t *current;
440 } peer_enumerator_t;
441
442 /**
443 * Implementation of peer_enumerator_t.public.enumerate
444 */
445 static bool peer_enumerator_enumerate(peer_enumerator_t *this, peer_cfg_t **cfg)
446 {
447 DESTROY_IF(this->current);
448 this->current = build_peer_cfg(this->this, this->inner, this->me, this->other);
449 if (this->current)
450 {
451 *cfg = this->current;
452 return TRUE;
453 }
454 return FALSE;
455 }
456
457 /**
458 * Implementation of peer_enumerator_t.public.destroy
459 */
460 static void peer_enumerator_destroy(peer_enumerator_t *this)
461 {
462 DESTROY_IF(this->current);
463 this->inner->destroy(this->inner);
464 free(this);
465 }
466
467 /**
468 * Implementation of backend_t.create_peer_cfg_enumerator.
469 */
470 static enumerator_t* create_peer_cfg_enumerator(private_sql_config_t *this,
471 identification_t *me,
472 identification_t *other)
473 {
474 peer_enumerator_t *e = malloc_thing(peer_enumerator_t);
475
476 e->this = this;
477 e->me = me;
478 e->other = other;
479 e->current = NULL;
480 e->public.enumerate = (void*)peer_enumerator_enumerate;
481 e->public.destroy = (void*)peer_enumerator_destroy;
482
483 /* TODO: only get configs whose IDs match exactly or contain wildcards */
484 e->inner = this->db->query(this->db,
485 "SELECT c.id, name, ike_cfg, l.type, l.data, r.type, r.data, "
486 "cert_policy, uniqueid, auth_method, eap_type, eap_vendor, "
487 "keyingtries, rekeytime, reauthtime, jitter, overtime, mobike, "
488 "dpd_delay, virtual, pool, "
489 "mediation, mediated_by, COALESCE(p.type, 0), p.data "
490 "FROM peer_configs AS c "
491 "JOIN identities AS l ON local_id = l.id "
492 "JOIN identities AS r ON remote_id = r.id "
493 "LEFT JOIN identities AS p ON peer_id = p.id "
494 "WHERE ike_version = ?",
495 DB_INT, 2,
496 DB_INT, DB_TEXT, DB_INT, DB_INT, DB_BLOB, DB_INT, DB_BLOB,
497 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
498 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
499 DB_INT, DB_TEXT, DB_TEXT,
500 DB_INT, DB_INT, DB_INT, DB_BLOB);
501 if (!e->inner)
502 {
503 free(e);
504 return NULL;
505 }
506 return &e->public;
507 }
508
509 /**
510 * Implementation of sql_config_t.destroy.
511 */
512 static void destroy(private_sql_config_t *this)
513 {
514 free(this);
515 }
516
517 /**
518 * Described in header.
519 */
520 sql_config_t *sql_config_create(database_t *db)
521 {
522 private_sql_config_t *this = malloc_thing(private_sql_config_t);
523
524 this->public.backend.create_peer_cfg_enumerator = (enumerator_t*(*)(backend_t*, identification_t *me, identification_t *other))create_peer_cfg_enumerator;
525 this->public.backend.create_ike_cfg_enumerator = (enumerator_t*(*)(backend_t*, host_t *me, host_t *other))create_ike_cfg_enumerator;
526 this->public.backend.get_peer_cfg_by_name = (peer_cfg_t* (*)(backend_t*,char*))get_peer_cfg_by_name;
527 this->public.destroy = (void(*)(sql_config_t*))destroy;
528
529 this->db = db;
530
531 return &this->public;
532 }
533