a4e718fbeafaf039a118d3216bfe40fe55d8bb71
[strongswan.git] / src / libcharon / plugins / sql / sql_config.c
1 /*
2 * Copyright (C) 2006-2008 Martin Willi
3 * Copyright (C) 2010 Andreas Steffen
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <string.h>
18
19 #include "sql_config.h"
20
21 #include <daemon.h>
22
23 typedef struct private_sql_config_t private_sql_config_t;
24
25 /**
26 * Private data of an sql_config_t object
27 */
28 struct private_sql_config_t {
29
30 /**
31 * Public part
32 */
33 sql_config_t public;
34
35 /**
36 * database connection
37 */
38 database_t *db;
39 };
40
41 /**
42 * Forward declaration
43 */
44 static peer_cfg_t *build_peer_cfg(private_sql_config_t *this, enumerator_t *e,
45 identification_t *me, identification_t *other);
46
47 /**
48 * Build a traffic selector from an SQL query
49 */
50 static traffic_selector_t *build_traffic_selector(private_sql_config_t *this,
51 enumerator_t *e, bool *local)
52 {
53 int type, protocol, start_port, end_port;
54 chunk_t start_addr, end_addr;
55 traffic_selector_t *ts;
56 enum {
57 TS_LOCAL = 0,
58 TS_REMOTE = 1,
59 TS_LOCAL_DYNAMIC = 2,
60 TS_REMOTE_DYNAMIC = 3,
61 } kind;
62
63 while (e->enumerate(e, &kind, &type, &protocol,
64 &start_addr, &end_addr, &start_port, &end_port))
65 {
66 *local = FALSE;
67 switch (kind)
68 {
69 case TS_LOCAL:
70 *local = TRUE;
71 /* FALL */
72 case TS_REMOTE:
73 ts = traffic_selector_create_from_bytes(protocol, type,
74 start_addr, start_port, end_addr, end_port);
75 break;
76 case TS_LOCAL_DYNAMIC:
77 *local = TRUE;
78 /* FALL */
79 case TS_REMOTE_DYNAMIC:
80 ts = traffic_selector_create_dynamic(protocol,
81 start_port, end_port);
82 break;
83 default:
84 continue;
85 }
86 if (ts)
87 {
88 return ts;
89 }
90 }
91 return NULL;
92 }
93
94 /**
95 * Add traffic selectors to a child config
96 */
97 static void add_traffic_selectors(private_sql_config_t *this,
98 child_cfg_t *child, int id)
99 {
100 enumerator_t *e;
101 traffic_selector_t *ts;
102 bool local;
103
104 e = this->db->query(this->db,
105 "SELECT kind, type, protocol, "
106 "start_addr, end_addr, start_port, end_port "
107 "FROM traffic_selectors JOIN child_config_traffic_selector "
108 "ON id = traffic_selector WHERE child_cfg = ?",
109 DB_INT, id,
110 DB_INT, DB_INT, DB_INT,
111 DB_BLOB, DB_BLOB, DB_INT, DB_INT);
112 if (e)
113 {
114 while ((ts = build_traffic_selector(this, e, &local)))
115 {
116 child->add_traffic_selector(child, local, ts);
117 }
118 e->destroy(e);
119 }
120 }
121
122 /**
123 * Add ESP proposals to a child config
124 */
125 static void add_esp_proposals(private_sql_config_t *this,
126 child_cfg_t *child, int id)
127 {
128 enumerator_t *e;
129 proposal_t *proposal;
130 char *prop;
131 bool use_default = TRUE;
132
133 e = this->db->query(this->db,
134 "SELECT proposal "
135 "FROM proposals JOIN child_config_proposal ON id = prop "
136 "WHERE child_cfg = ? ORDER BY prio",
137 DB_INT, id, DB_TEXT);
138 if (e)
139 {
140 while (e->enumerate(e, &prop))
141 {
142 proposal = proposal_create_from_string(PROTO_ESP, prop);
143 if (!proposal)
144 {
145 DBG1(DBG_CFG, "could not create ESP proposal from '%s'", prop);
146 break;
147 }
148 child->add_proposal(child, proposal);
149 use_default = FALSE;
150 }
151 e->destroy(e);
152 }
153 if (use_default)
154 {
155 child->add_proposal(child, proposal_create_default(PROTO_ESP));
156 }
157 }
158
159 /**
160 * Build a child config from an SQL query
161 */
162 static child_cfg_t *build_child_cfg(private_sql_config_t *this, enumerator_t *e)
163 {
164 int id, lifetime, rekeytime, jitter, hostaccess, mode, ipcomp;
165 int start, dpd, close;
166 char *name, *updown;
167 child_cfg_t *child_cfg;
168
169 if (e->enumerate(e, &id, &name, &lifetime, &rekeytime, &jitter, &updown,
170 &hostaccess, &mode, &start, &dpd, &close, &ipcomp))
171 {
172 lifetime_cfg_t lft = {
173 .time = { .life = lifetime, .rekey = rekeytime, .jitter = jitter }
174 };
175 child_cfg = child_cfg_create(name, &lft, updown, hostaccess, mode,
176 start, dpd, close, ipcomp, 0, 0, NULL, NULL);
177 add_esp_proposals(this, child_cfg, id);
178 add_traffic_selectors(this, child_cfg, id);
179 return child_cfg;
180 }
181 return NULL;
182 }
183
184 /**
185 * Add child configs to peer config
186 */
187 static void add_child_cfgs(private_sql_config_t *this, peer_cfg_t *peer, int id)
188 {
189 enumerator_t *e;
190 child_cfg_t *child_cfg;
191
192 e = this->db->query(this->db,
193 "SELECT id, name, lifetime, rekeytime, jitter, updown, "
194 "hostaccess, mode, start_action, dpd_action, close_action, ipcomp "
195 "FROM child_configs JOIN peer_config_child_config ON id = child_cfg "
196 "WHERE peer_cfg = ?",
197 DB_INT, id,
198 DB_INT, DB_TEXT, DB_INT, DB_INT, DB_INT, DB_TEXT,
199 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT, DB_INT);
200 if (e)
201 {
202 while ((child_cfg = build_child_cfg(this, e)))
203 {
204 peer->add_child_cfg(peer, child_cfg);
205 }
206 e->destroy(e);
207 }
208 }
209
210 /**
211 * Add IKE proposals to an IKE config
212 */
213 static void add_ike_proposals(private_sql_config_t *this,
214 ike_cfg_t *ike_cfg, int id)
215 {
216 enumerator_t *e;
217 proposal_t *proposal;
218 char *prop;
219 bool use_default = TRUE;
220
221 e = this->db->query(this->db,
222 "SELECT proposal "
223 "FROM proposals JOIN ike_config_proposal ON id = prop "
224 "WHERE ike_cfg = ? ORDER BY prio",
225 DB_INT, id, DB_TEXT);
226 if (e)
227 {
228 while (e->enumerate(e, &prop))
229 {
230 proposal = proposal_create_from_string(PROTO_IKE, prop);
231 if (!proposal)
232 {
233 DBG1(DBG_CFG, "could not create IKE proposal from '%s'", prop);
234 break;
235 }
236 ike_cfg->add_proposal(ike_cfg, proposal);
237 use_default = FALSE;
238 }
239 e->destroy(e);
240 }
241 if (use_default)
242 {
243 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
244 }
245 }
246
247 /**
248 * Build an IKE config from an SQL query
249 */
250 static ike_cfg_t *build_ike_cfg(private_sql_config_t *this, enumerator_t *e,
251 host_t *my_host, host_t *other_host)
252 {
253 int id, certreq, force_encap;
254 char *local, *remote;
255
256 while (e->enumerate(e, &id, &certreq, &force_encap, &local, &remote))
257 {
258 ike_cfg_t *ike_cfg;
259
260 ike_cfg = ike_cfg_create(certreq, force_encap,
261 local, IKEV2_UDP_PORT, remote, IKEV2_UDP_PORT);
262 add_ike_proposals(this, ike_cfg, id);
263 return ike_cfg;
264 }
265 return NULL;
266 }
267
268 /**
269 * Query an IKE config by its id
270 */
271 static ike_cfg_t* get_ike_cfg_by_id(private_sql_config_t *this, int id)
272 {
273 enumerator_t *e;
274 ike_cfg_t *ike_cfg = NULL;
275
276 e = this->db->query(this->db,
277 "SELECT id, certreq, force_encap, local, remote "
278 "FROM ike_configs WHERE id = ?",
279 DB_INT, id,
280 DB_INT, DB_INT, DB_INT, DB_TEXT, DB_TEXT);
281 if (e)
282 {
283 ike_cfg = build_ike_cfg(this, e, NULL, NULL);
284 e->destroy(e);
285 }
286 return ike_cfg;
287 }
288
289 /**
290 * Query a peer config by its id
291 */
292 static peer_cfg_t *get_peer_cfg_by_id(private_sql_config_t *this, int id)
293 {
294 enumerator_t *e;
295 peer_cfg_t *peer_cfg = NULL;
296
297 e = this->db->query(this->db,
298 "SELECT c.id, name, ike_cfg, l.type, l.data, r.type, r.data, "
299 "cert_policy, uniqueid, auth_method, eap_type, eap_vendor, "
300 "keyingtries, rekeytime, reauthtime, jitter, overtime, mobike, "
301 "dpd_delay, virtual, pool, "
302 "mediation, mediated_by, COALESCE(p.type, 0), p.data "
303 "FROM peer_configs AS c "
304 "JOIN identities AS l ON local_id = l.id "
305 "JOIN identities AS r ON remote_id = r.id "
306 "LEFT JOIN identities AS p ON peer_id = p.id "
307 "WHERE id = ?",
308 DB_INT, id,
309 DB_INT, DB_TEXT, DB_INT, DB_INT, DB_BLOB, DB_INT, DB_BLOB,
310 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
311 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
312 DB_INT, DB_TEXT, DB_TEXT,
313 DB_INT, DB_INT, DB_INT, DB_BLOB);
314 if (e)
315 {
316 peer_cfg = build_peer_cfg(this, e, NULL, NULL);
317 e->destroy(e);
318 }
319 return peer_cfg;
320 }
321
322 /**
323 * Build a peer config from an SQL query
324 */
325 static peer_cfg_t *build_peer_cfg(private_sql_config_t *this, enumerator_t *e,
326 identification_t *me, identification_t *other)
327 {
328 int id, ike_cfg, l_type, r_type,
329 cert_policy, uniqueid, auth_method, eap_type, eap_vendor, keyingtries,
330 rekeytime, reauthtime, jitter, overtime, mobike, dpd_delay,
331 mediation, mediated_by, p_type;
332 chunk_t l_data, r_data, p_data;
333 char *name, *virtual, *pool;
334
335 while (e->enumerate(e,
336 &id, &name, &ike_cfg, &l_type, &l_data, &r_type, &r_data,
337 &cert_policy, &uniqueid, &auth_method, &eap_type, &eap_vendor,
338 &keyingtries, &rekeytime, &reauthtime, &jitter, &overtime, &mobike,
339 &dpd_delay, &virtual, &pool,
340 &mediation, &mediated_by, &p_type, &p_data))
341 {
342 identification_t *local_id, *remote_id, *peer_id = NULL;
343 peer_cfg_t *peer_cfg, *mediated_cfg;
344 ike_cfg_t *ike;
345 host_t *vip = NULL;
346 auth_cfg_t *auth;
347
348 local_id = identification_create_from_encoding(l_type, l_data);
349 remote_id = identification_create_from_encoding(r_type, r_data);
350 if ((me && !me->matches(me, local_id)) ||
351 (other && !other->matches(other, remote_id)))
352 {
353 local_id->destroy(local_id);
354 remote_id->destroy(remote_id);
355 continue;
356 }
357 ike = get_ike_cfg_by_id(this, ike_cfg);
358 mediated_cfg = mediated_by ? get_peer_cfg_by_id(this, mediated_by) : NULL;
359 if (p_type)
360 {
361 peer_id = identification_create_from_encoding(p_type, p_data);
362 }
363 if (virtual)
364 {
365 vip = host_create_from_string(virtual, 0);
366 }
367 if (ike)
368 {
369 peer_cfg = peer_cfg_create(
370 name, 2, ike, cert_policy, uniqueid,
371 keyingtries, rekeytime, reauthtime, jitter, overtime,
372 mobike, dpd_delay, vip, pool,
373 mediation, mediated_cfg, peer_id);
374 auth = auth_cfg_create();
375 auth->add(auth, AUTH_RULE_AUTH_CLASS, auth_method);
376 auth->add(auth, AUTH_RULE_IDENTITY, local_id);
377 peer_cfg->add_auth_cfg(peer_cfg, auth, TRUE);
378 auth = auth_cfg_create();
379 auth->add(auth, AUTH_RULE_IDENTITY, remote_id);
380 if (eap_type)
381 {
382 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_EAP);
383 auth->add(auth, AUTH_RULE_EAP_TYPE, eap_type);
384 if (eap_vendor)
385 {
386 auth->add(auth, AUTH_RULE_EAP_VENDOR, eap_vendor);
387 }
388 }
389 peer_cfg->add_auth_cfg(peer_cfg, auth, FALSE);
390 add_child_cfgs(this, peer_cfg, id);
391 return peer_cfg;
392 }
393 DESTROY_IF(ike);
394 DESTROY_IF(mediated_cfg);
395 DESTROY_IF(peer_id);
396 DESTROY_IF(local_id);
397 DESTROY_IF(remote_id);
398 }
399 return NULL;
400 }
401
402 METHOD(backend_t, get_peer_cfg_by_name, peer_cfg_t*,
403 private_sql_config_t *this, char *name)
404 {
405 enumerator_t *e;
406 peer_cfg_t *peer_cfg = NULL;
407
408 e = this->db->query(this->db,
409 "SELECT c.id, name, ike_cfg, l.type, l.data, r.type, r.data, "
410 "cert_policy, uniqueid, auth_method, eap_type, eap_vendor, "
411 "keyingtries, rekeytime, reauthtime, jitter, overtime, mobike, "
412 "dpd_delay, virtual, pool, "
413 "mediation, mediated_by, COALESCE(p.type, 0), p.data "
414 "FROM peer_configs AS c "
415 "JOIN identities AS l ON local_id = l.id "
416 "JOIN identities AS r ON remote_id = r.id "
417 "LEFT JOIN identities AS p ON peer_id = p.id "
418 "WHERE ike_version = ? AND name = ?",
419 DB_INT, 2, DB_TEXT, name,
420 DB_INT, DB_TEXT, DB_INT, DB_INT, DB_BLOB, DB_INT, DB_BLOB,
421 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
422 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
423 DB_INT, DB_TEXT, DB_TEXT,
424 DB_INT, DB_INT, DB_INT, DB_BLOB);
425 if (e)
426 {
427 peer_cfg = build_peer_cfg(this, e, NULL, NULL);
428 e->destroy(e);
429 }
430 return peer_cfg;
431 }
432
433 typedef struct {
434 /** implements enumerator */
435 enumerator_t public;
436 /** reference to context */
437 private_sql_config_t *this;
438 /** filtering own host */
439 host_t *me;
440 /** filtering remote host */
441 host_t *other;
442 /** inner SQL enumerator */
443 enumerator_t *inner;
444 /** currently enumerated peer config */
445 ike_cfg_t *current;
446 } ike_enumerator_t;
447
448 /**
449 * Implementation of ike_enumerator_t.public.enumerate
450 */
451 static bool ike_enumerator_enumerate(ike_enumerator_t *this, ike_cfg_t **cfg)
452 {
453 DESTROY_IF(this->current);
454 this->current = build_ike_cfg(this->this, this->inner, this->me, this->other);
455 if (this->current)
456 {
457 *cfg = this->current;
458 return TRUE;
459 }
460 return FALSE;
461 }
462
463 /**
464 * Implementation of ike_enumerator_t.public.destroy
465 */
466 static void ike_enumerator_destroy(ike_enumerator_t *this)
467 {
468 DESTROY_IF(this->current);
469 this->inner->destroy(this->inner);
470 free(this);
471 }
472
473 METHOD(backend_t, create_ike_cfg_enumerator, enumerator_t*,
474 private_sql_config_t *this, host_t *me, host_t *other)
475 {
476 ike_enumerator_t *e = malloc_thing(ike_enumerator_t);
477
478 e->this = this;
479 e->me = me;
480 e->other = other;
481 e->current = NULL;
482 e->public.enumerate = (void*)ike_enumerator_enumerate;
483 e->public.destroy = (void*)ike_enumerator_destroy;
484
485 e->inner = this->db->query(this->db,
486 "SELECT id, certreq, force_encap, local, remote "
487 "FROM ike_configs",
488 DB_INT, DB_INT, DB_INT, DB_TEXT, DB_TEXT);
489 if (!e->inner)
490 {
491 free(e);
492 return NULL;
493 }
494 return &e->public;
495 }
496
497
498 typedef struct {
499 /** implements enumerator */
500 enumerator_t public;
501 /** reference to context */
502 private_sql_config_t *this;
503 /** filtering own identity */
504 identification_t *me;
505 /** filtering remote identity */
506 identification_t *other;
507 /** inner SQL enumerator */
508 enumerator_t *inner;
509 /** currently enumerated peer config */
510 peer_cfg_t *current;
511 } peer_enumerator_t;
512
513 /**
514 * Implementation of peer_enumerator_t.public.enumerate
515 */
516 static bool peer_enumerator_enumerate(peer_enumerator_t *this, peer_cfg_t **cfg)
517 {
518 DESTROY_IF(this->current);
519 this->current = build_peer_cfg(this->this, this->inner, this->me, this->other);
520 if (this->current)
521 {
522 *cfg = this->current;
523 return TRUE;
524 }
525 return FALSE;
526 }
527
528 /**
529 * Implementation of peer_enumerator_t.public.destroy
530 */
531 static void peer_enumerator_destroy(peer_enumerator_t *this)
532 {
533 DESTROY_IF(this->current);
534 this->inner->destroy(this->inner);
535 free(this);
536 }
537
538 METHOD(backend_t, create_peer_cfg_enumerator, enumerator_t*,
539 private_sql_config_t *this, identification_t *me, identification_t *other)
540 {
541 peer_enumerator_t *e = malloc_thing(peer_enumerator_t);
542
543 e->this = this;
544 e->me = me;
545 e->other = other;
546 e->current = NULL;
547 e->public.enumerate = (void*)peer_enumerator_enumerate;
548 e->public.destroy = (void*)peer_enumerator_destroy;
549
550 /* TODO: only get configs whose IDs match exactly or contain wildcards */
551 e->inner = this->db->query(this->db,
552 "SELECT c.id, name, ike_cfg, l.type, l.data, r.type, r.data, "
553 "cert_policy, uniqueid, auth_method, eap_type, eap_vendor, "
554 "keyingtries, rekeytime, reauthtime, jitter, overtime, mobike, "
555 "dpd_delay, virtual, pool, "
556 "mediation, mediated_by, COALESCE(p.type, 0), p.data "
557 "FROM peer_configs AS c "
558 "JOIN identities AS l ON local_id = l.id "
559 "JOIN identities AS r ON remote_id = r.id "
560 "LEFT JOIN identities AS p ON peer_id = p.id "
561 "WHERE ike_version = ?",
562 DB_INT, 2,
563 DB_INT, DB_TEXT, DB_INT, DB_INT, DB_BLOB, DB_INT, DB_BLOB,
564 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
565 DB_INT, DB_INT, DB_INT, DB_INT, DB_INT, DB_INT,
566 DB_INT, DB_TEXT, DB_TEXT,
567 DB_INT, DB_INT, DB_INT, DB_BLOB);
568 if (!e->inner)
569 {
570 free(e);
571 return NULL;
572 }
573 return &e->public;
574 }
575
576 METHOD(sql_config_t, destroy, void,
577 private_sql_config_t *this)
578 {
579 free(this);
580 }
581
582 /**
583 * Described in header.
584 */
585 sql_config_t *sql_config_create(database_t *db)
586 {
587 private_sql_config_t *this;
588
589 INIT(this,
590 .public = {
591 .backend = {
592 .create_peer_cfg_enumerator = _create_peer_cfg_enumerator,
593 .create_ike_cfg_enumerator = _create_ike_cfg_enumerator,
594 .get_peer_cfg_by_name = _get_peer_cfg_by_name,
595 },
596 .destroy = _destroy,
597 },
598 .db = db
599 );
600
601 return &this->public;
602 }
603