Adapted the config backends to the new lifetime configuration.
[strongswan.git] / src / charon / plugins / medcli / medcli_config.c
1 /*
2 * Copyright (C) 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 #define _GNU_SOURCE
17 #include <string.h>
18
19 #include "medcli_config.h"
20
21 #include <daemon.h>
22 #include <processing/jobs/callback_job.h>
23
24 typedef struct private_medcli_config_t private_medcli_config_t;
25
26 /**
27 * Private data of an medcli_config_t object
28 */
29 struct private_medcli_config_t {
30
31 /**
32 * Public part
33 */
34 medcli_config_t public;
35
36 /**
37 * database connection
38 */
39 database_t *db;
40
41 /**
42 * rekey time
43 */
44 int rekey;
45
46 /**
47 * dpd delay
48 */
49 int dpd;
50
51 /**
52 * default ike config
53 */
54 ike_cfg_t *ike;
55 };
56
57 /**
58 * create a traffic selector from a CIDR notation string
59 */
60 static traffic_selector_t *ts_from_string(char *str)
61 {
62 if (str)
63 {
64 int netbits = 32;
65 host_t *net;
66 char *pos;
67
68 str = strdupa(str);
69 pos = strchr(str, '/');
70 if (pos)
71 {
72 *pos++ = '\0';
73 netbits = atoi(pos);
74 }
75 else
76 {
77 if (strchr(str, ':'))
78 {
79 netbits = 128;
80 }
81 }
82 net = host_create_from_string(str, 0);
83 if (net)
84 {
85 return traffic_selector_create_from_subnet(net, netbits, 0, 0);
86 }
87 }
88 return traffic_selector_create_dynamic(0, 0, 65535);
89 }
90
91 /**
92 * implements backend_t.get_peer_cfg_by_name.
93 */
94 static peer_cfg_t *get_peer_cfg_by_name(private_medcli_config_t *this, char *name)
95 {
96 enumerator_t *e;
97 peer_cfg_t *peer_cfg, *med_cfg;
98 auth_cfg_t *auth;
99 ike_cfg_t *ike_cfg;
100 child_cfg_t *child_cfg;
101 chunk_t me, other;
102 lifetime_cfg_t *lifetime;
103 char *address, *local_net, *remote_net;
104
105 /* query mediation server config:
106 * - build ike_cfg/peer_cfg for mediation connection on-the-fly
107 */
108 e = this->db->query(this->db,
109 "SELECT Address, ClientConfig.KeyId, MediationServerConfig.KeyId "
110 "FROM MediationServerConfig JOIN ClientConfig",
111 DB_TEXT, DB_BLOB, DB_BLOB);
112 if (!e || !e->enumerate(e, &address, &me, &other))
113 {
114 DESTROY_IF(e);
115 return NULL;
116 }
117 ike_cfg = ike_cfg_create(FALSE, FALSE, "0.0.0.0", address);
118 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
119 med_cfg = peer_cfg_create(
120 "mediation", 2, ike_cfg,
121 CERT_NEVER_SEND, UNIQUE_REPLACE,
122 1, this->rekey*60, 0, /* keytries, rekey, reauth */
123 this->rekey*5, this->rekey*3, /* jitter, overtime */
124 TRUE, this->dpd, /* mobike, dpddelay */
125 NULL, NULL, /* vip, pool */
126 TRUE, NULL, NULL); /* mediation, med by, peer id */
127 e->destroy(e);
128
129 auth = auth_cfg_create();
130 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
131 auth->add(auth, AUTH_RULE_IDENTITY,
132 identification_create_from_encoding(ID_KEY_ID, me));
133 med_cfg->add_auth_cfg(med_cfg, auth, TRUE);
134 auth = auth_cfg_create();
135 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
136 auth->add(auth, AUTH_RULE_IDENTITY,
137 identification_create_from_encoding(ID_KEY_ID, other));
138 med_cfg->add_auth_cfg(med_cfg, auth, FALSE);
139
140 /* query mediated config:
141 * - use any-any ike_cfg
142 * - build peer_cfg on-the-fly using med_cfg
143 * - add a child_cfg
144 */
145 e = this->db->query(this->db,
146 "SELECT ClientConfig.KeyId, Connection.KeyId, "
147 "Connection.LocalSubnet, Connection.RemoteSubnet "
148 "FROM ClientConfig JOIN Connection "
149 "WHERE Active AND Alias = ?", DB_TEXT, name,
150 DB_BLOB, DB_BLOB, DB_TEXT, DB_TEXT);
151 if (!e || !e->enumerate(e, &me, &other, &local_net, &remote_net))
152 {
153 DESTROY_IF(e);
154 return NULL;
155 }
156 peer_cfg = peer_cfg_create(
157 name, 2, this->ike->get_ref(this->ike),
158 CERT_NEVER_SEND, UNIQUE_REPLACE,
159 1, this->rekey*60, 0, /* keytries, rekey, reauth */
160 this->rekey*5, this->rekey*3, /* jitter, overtime */
161 TRUE, this->dpd, /* mobike, dpddelay */
162 NULL, NULL, /* vip, pool */
163 FALSE, med_cfg, /* mediation, med by */
164 identification_create_from_encoding(ID_KEY_ID, other));
165
166 auth = auth_cfg_create();
167 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
168 auth->add(auth, AUTH_RULE_IDENTITY,
169 identification_create_from_encoding(ID_KEY_ID, me));
170 peer_cfg->add_auth_cfg(peer_cfg, auth, TRUE);
171 auth = auth_cfg_create();
172 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
173 auth->add(auth, AUTH_RULE_IDENTITY,
174 identification_create_from_encoding(ID_KEY_ID, other));
175 peer_cfg->add_auth_cfg(peer_cfg, auth, FALSE);
176
177 lifetime = lifetime_cfg_create_time(this->rekey * 60 + this->rekey,
178 this->rekey, this->rekey);
179
180 child_cfg = child_cfg_create(name, lifetime, NULL, TRUE,
181 MODE_TUNNEL, ACTION_NONE, ACTION_NONE, FALSE);
182 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
183 child_cfg->add_traffic_selector(child_cfg, TRUE, ts_from_string(local_net));
184 child_cfg->add_traffic_selector(child_cfg, FALSE, ts_from_string(remote_net));
185 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
186 e->destroy(e);
187 return peer_cfg;
188 }
189
190 /**
191 * Implementation of backend_t.create_ike_cfg_enumerator.
192 */
193 static enumerator_t* create_ike_cfg_enumerator(private_medcli_config_t *this,
194 host_t *me, host_t *other)
195 {
196 return enumerator_create_single(this->ike, NULL);
197 }
198
199 typedef struct {
200 /** implements enumerator */
201 enumerator_t public;
202 /** inner SQL enumerator */
203 enumerator_t *inner;
204 /** currently enumerated peer config */
205 peer_cfg_t *current;
206 /** ike cfg to use in peer cfg */
207 ike_cfg_t *ike;
208 /** rekey time */
209 int rekey;
210 /** dpd time */
211 int dpd;
212 } peer_enumerator_t;
213
214 /**
215 * Implementation of peer_enumerator_t.public.enumerate
216 */
217 static bool peer_enumerator_enumerate(peer_enumerator_t *this, peer_cfg_t **cfg)
218 {
219 char *name, *local_net, *remote_net;
220 chunk_t me, other;
221 child_cfg_t *child_cfg;
222 auth_cfg_t *auth;
223 lifetime_cfg_t *lifetime;
224
225 DESTROY_IF(this->current);
226 if (!this->inner->enumerate(this->inner, &name, &me, &other,
227 &local_net, &remote_net))
228 {
229 this->current = NULL;
230 return FALSE;
231 }
232 this->current = peer_cfg_create(
233 name, 2, this->ike->get_ref(this->ike),
234 CERT_NEVER_SEND, UNIQUE_REPLACE,
235 1, this->rekey*60, 0, /* keytries, rekey, reauth */
236 this->rekey*5, this->rekey*3, /* jitter, overtime */
237 TRUE, this->dpd, /* mobike, dpddelay */
238 NULL, NULL, /* vip, pool */
239 FALSE, NULL, NULL); /* mediation, med by, peer id */
240
241 auth = auth_cfg_create();
242 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
243 auth->add(auth, AUTH_RULE_IDENTITY,
244 identification_create_from_encoding(ID_KEY_ID, me));
245 this->current->add_auth_cfg(this->current, auth, TRUE);
246 auth = auth_cfg_create();
247 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
248 auth->add(auth, AUTH_RULE_IDENTITY,
249 identification_create_from_encoding(ID_KEY_ID, other));
250 this->current->add_auth_cfg(this->current, auth, FALSE);
251
252 lifetime = lifetime_cfg_create_time(this->rekey * 60 + this->rekey,
253 this->rekey, this->rekey);
254
255 child_cfg = child_cfg_create(name, lifetime, NULL, TRUE, MODE_TUNNEL,
256 ACTION_NONE, ACTION_NONE, FALSE);
257 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
258 child_cfg->add_traffic_selector(child_cfg, TRUE, ts_from_string(local_net));
259 child_cfg->add_traffic_selector(child_cfg, FALSE, ts_from_string(remote_net));
260 this->current->add_child_cfg(this->current, child_cfg);
261 *cfg = this->current;
262 return TRUE;
263 }
264
265 /**
266 * Implementation of peer_enumerator_t.public.destroy
267 */
268 static void peer_enumerator_destroy(peer_enumerator_t *this)
269 {
270 DESTROY_IF(this->current);
271 this->inner->destroy(this->inner);
272 free(this);
273 }
274
275 /**
276 * Implementation of backend_t.create_peer_cfg_enumerator.
277 */
278 static enumerator_t* create_peer_cfg_enumerator(private_medcli_config_t *this,
279 identification_t *me,
280 identification_t *other)
281 {
282 peer_enumerator_t *e = malloc_thing(peer_enumerator_t);
283
284 e->current = NULL;
285 e->ike = this->ike;
286 e->rekey = this->rekey;
287 e->dpd = this->dpd;
288 e->public.enumerate = (void*)peer_enumerator_enumerate;
289 e->public.destroy = (void*)peer_enumerator_destroy;
290
291 /* filter on IDs: NULL or ANY or matching KEY_ID */
292 e->inner = this->db->query(this->db,
293 "SELECT Alias, ClientConfig.KeyId, Connection.KeyId, "
294 "Connection.LocalSubnet, Connection.RemoteSubnet "
295 "FROM ClientConfig JOIN Connection "
296 "WHERE Active AND "
297 "(? OR ClientConfig.KeyId = ?) AND (? OR Connection.KeyId = ?)",
298 DB_INT, me == NULL || me->get_type(me) == ID_ANY,
299 DB_BLOB, me && me->get_type(me) == ID_KEY_ID ?
300 me->get_encoding(me) : chunk_empty,
301 DB_INT, other == NULL || other->get_type(other) == ID_ANY,
302 DB_BLOB, other && other->get_type(other) == ID_KEY_ID ?
303 other->get_encoding(other) : chunk_empty,
304 DB_TEXT, DB_BLOB, DB_BLOB, DB_TEXT, DB_TEXT);
305 if (!e->inner)
306 {
307 free(e);
308 return NULL;
309 }
310 return &e->public;
311 }
312
313 /**
314 * initiate a peer config
315 */
316 static job_requeue_t initiate_config(peer_cfg_t *peer_cfg)
317 {
318 enumerator_t *enumerator;
319 child_cfg_t *child_cfg = NULL;;
320
321 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
322 enumerator->enumerate(enumerator, &child_cfg);
323 if (child_cfg)
324 {
325 child_cfg->get_ref(child_cfg);
326 peer_cfg->get_ref(peer_cfg);
327 enumerator->destroy(enumerator);
328 charon->controller->initiate(charon->controller,
329 peer_cfg, child_cfg, NULL, NULL);
330 }
331 else
332 {
333 enumerator->destroy(enumerator);
334 }
335 return JOB_REQUEUE_NONE;
336 }
337
338 /**
339 * schedule initation of all "active" connections
340 */
341 static void schedule_autoinit(private_medcli_config_t *this)
342 {
343 enumerator_t *e;
344 char *name;
345
346 e = this->db->query(this->db, "SELECT Alias FROM Connection WHERE Active",
347 DB_TEXT);
348 if (e)
349 {
350 while (e->enumerate(e, &name))
351 {
352 peer_cfg_t *peer_cfg;
353
354 peer_cfg = get_peer_cfg_by_name(this, name);
355 if (peer_cfg)
356 {
357 /* schedule asynchronous initiation job */
358 charon->processor->queue_job(charon->processor,
359 (job_t*)callback_job_create(
360 (callback_job_cb_t)initiate_config,
361 peer_cfg, (void*)peer_cfg->destroy, NULL));
362 }
363 }
364 e->destroy(e);
365 }
366 }
367
368 /**
369 * Implementation of medcli_config_t.destroy.
370 */
371 static void destroy(private_medcli_config_t *this)
372 {
373 this->ike->destroy(this->ike);
374 free(this);
375 }
376
377 /**
378 * Described in header.
379 */
380 medcli_config_t *medcli_config_create(database_t *db)
381 {
382 private_medcli_config_t *this = malloc_thing(private_medcli_config_t);
383
384 this->public.backend.create_peer_cfg_enumerator = (enumerator_t*(*)(backend_t*, identification_t *me, identification_t *other))create_peer_cfg_enumerator;
385 this->public.backend.create_ike_cfg_enumerator = (enumerator_t*(*)(backend_t*, host_t *me, host_t *other))create_ike_cfg_enumerator;
386 this->public.backend.get_peer_cfg_by_name = (peer_cfg_t* (*)(backend_t*,char*))get_peer_cfg_by_name;
387 this->public.destroy = (void(*)(medcli_config_t*))destroy;
388
389 this->db = db;
390 this->rekey = lib->settings->get_time(lib->settings, "medcli.rekey", 1200);
391 this->dpd = lib->settings->get_time(lib->settings, "medcli.dpd", 300);
392 this->ike = ike_cfg_create(FALSE, FALSE, "0.0.0.0", "0.0.0.0");
393 this->ike->add_proposal(this->ike, proposal_create_default(PROTO_IKE));
394
395 schedule_autoinit(this);
396
397 return &this->public;
398 }
399