7fa0152bd57a89d77105ef9375003eccf4fe5a38
[strongswan.git] / src / libcharon / 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 METHOD(backend_t, get_peer_cfg_by_name, peer_cfg_t*,
92 private_medcli_config_t *this, char *name)
93 {
94 enumerator_t *e;
95 peer_cfg_t *peer_cfg, *med_cfg;
96 auth_cfg_t *auth;
97 ike_cfg_t *ike_cfg;
98 child_cfg_t *child_cfg;
99 chunk_t me, other;
100 char *address, *local_net, *remote_net;
101 lifetime_cfg_t lifetime = {
102 .time = {
103 .life = this->rekey * 60 + this->rekey,
104 .rekey = this->rekey,
105 .jitter = this->rekey
106 }
107 };
108
109 /* query mediation server config:
110 * - build ike_cfg/peer_cfg for mediation connection on-the-fly
111 */
112 e = this->db->query(this->db,
113 "SELECT Address, ClientConfig.KeyId, MediationServerConfig.KeyId "
114 "FROM MediationServerConfig JOIN ClientConfig",
115 DB_TEXT, DB_BLOB, DB_BLOB);
116 if (!e || !e->enumerate(e, &address, &me, &other))
117 {
118 DESTROY_IF(e);
119 return NULL;
120 }
121 ike_cfg = ike_cfg_create(FALSE, FALSE,
122 "0.0.0.0", IKEV2_UDP_PORT, address, IKEV2_UDP_PORT);
123 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
124 med_cfg = peer_cfg_create(
125 "mediation", IKEV2, ike_cfg,
126 CERT_NEVER_SEND, UNIQUE_REPLACE,
127 1, this->rekey*60, 0, /* keytries, rekey, reauth */
128 this->rekey*5, this->rekey*3, /* jitter, overtime */
129 TRUE, FALSE, this->dpd, /* mobike, aggressive, dpddelay */
130 NULL, NULL, /* vip, pool */
131 TRUE, NULL, NULL); /* mediation, med by, peer id */
132 e->destroy(e);
133
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, me));
138 med_cfg->add_auth_cfg(med_cfg, auth, TRUE);
139 auth = auth_cfg_create();
140 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
141 auth->add(auth, AUTH_RULE_IDENTITY,
142 identification_create_from_encoding(ID_KEY_ID, other));
143 med_cfg->add_auth_cfg(med_cfg, auth, FALSE);
144
145 /* query mediated config:
146 * - use any-any ike_cfg
147 * - build peer_cfg on-the-fly using med_cfg
148 * - add a child_cfg
149 */
150 e = this->db->query(this->db,
151 "SELECT ClientConfig.KeyId, Connection.KeyId, "
152 "Connection.LocalSubnet, Connection.RemoteSubnet "
153 "FROM ClientConfig JOIN Connection "
154 "WHERE Active AND Alias = ?", DB_TEXT, name,
155 DB_BLOB, DB_BLOB, DB_TEXT, DB_TEXT);
156 if (!e || !e->enumerate(e, &me, &other, &local_net, &remote_net))
157 {
158 DESTROY_IF(e);
159 return NULL;
160 }
161 peer_cfg = peer_cfg_create(
162 name, IKEV2, this->ike->get_ref(this->ike),
163 CERT_NEVER_SEND, UNIQUE_REPLACE,
164 1, this->rekey*60, 0, /* keytries, rekey, reauth */
165 this->rekey*5, this->rekey*3, /* jitter, overtime */
166 TRUE, FALSE, this->dpd, /* mobike, aggressive, dpddelay */
167 NULL, NULL, /* vip, pool */
168 FALSE, med_cfg, /* mediation, med by */
169 identification_create_from_encoding(ID_KEY_ID, other));
170
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, me));
175 peer_cfg->add_auth_cfg(peer_cfg, auth, TRUE);
176 auth = auth_cfg_create();
177 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
178 auth->add(auth, AUTH_RULE_IDENTITY,
179 identification_create_from_encoding(ID_KEY_ID, other));
180 peer_cfg->add_auth_cfg(peer_cfg, auth, FALSE);
181
182 child_cfg = child_cfg_create(name, &lifetime, NULL, TRUE, MODE_TUNNEL,
183 ACTION_NONE, ACTION_NONE, ACTION_NONE, FALSE,
184 0, 0, NULL, NULL, 0);
185 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
186 child_cfg->add_traffic_selector(child_cfg, TRUE, ts_from_string(local_net));
187 child_cfg->add_traffic_selector(child_cfg, FALSE, ts_from_string(remote_net));
188 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
189 e->destroy(e);
190 return peer_cfg;
191 }
192
193 METHOD(backend_t, create_ike_cfg_enumerator, enumerator_t*,
194 private_medcli_config_t *this, 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 METHOD(enumerator_t, peer_enumerator_enumerate, bool,
215 peer_enumerator_t *this, peer_cfg_t **cfg)
216 {
217 char *name, *local_net, *remote_net;
218 chunk_t me, other;
219 child_cfg_t *child_cfg;
220 auth_cfg_t *auth;
221 lifetime_cfg_t lifetime = {
222 .time = {
223 .life = this->rekey * 60 + this->rekey,
224 .rekey = this->rekey,
225 .jitter = this->rekey
226 }
227 };
228
229 DESTROY_IF(this->current);
230 if (!this->inner->enumerate(this->inner, &name, &me, &other,
231 &local_net, &remote_net))
232 {
233 this->current = NULL;
234 return FALSE;
235 }
236 this->current = peer_cfg_create(
237 name, IKEV2, this->ike->get_ref(this->ike),
238 CERT_NEVER_SEND, UNIQUE_REPLACE,
239 1, this->rekey*60, 0, /* keytries, rekey, reauth */
240 this->rekey*5, this->rekey*3, /* jitter, overtime */
241 TRUE, FALSE, this->dpd, /* mobike, aggr., dpddelay */
242 NULL, NULL, /* vip, pool */
243 FALSE, NULL, NULL); /* mediation, med by, peer id */
244
245 auth = auth_cfg_create();
246 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
247 auth->add(auth, AUTH_RULE_IDENTITY,
248 identification_create_from_encoding(ID_KEY_ID, me));
249 this->current->add_auth_cfg(this->current, auth, TRUE);
250 auth = auth_cfg_create();
251 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
252 auth->add(auth, AUTH_RULE_IDENTITY,
253 identification_create_from_encoding(ID_KEY_ID, other));
254 this->current->add_auth_cfg(this->current, auth, FALSE);
255
256 child_cfg = child_cfg_create(name, &lifetime, NULL, TRUE, MODE_TUNNEL,
257 ACTION_NONE, ACTION_NONE, ACTION_NONE, FALSE,
258 0, 0, NULL, NULL, 0);
259 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
260 child_cfg->add_traffic_selector(child_cfg, TRUE, ts_from_string(local_net));
261 child_cfg->add_traffic_selector(child_cfg, FALSE, ts_from_string(remote_net));
262 this->current->add_child_cfg(this->current, child_cfg);
263 *cfg = this->current;
264 return TRUE;
265 }
266
267 METHOD(enumerator_t, peer_enumerator_destroy, void,
268 peer_enumerator_t *this)
269 {
270 DESTROY_IF(this->current);
271 this->inner->destroy(this->inner);
272 free(this);
273 }
274
275 METHOD(backend_t, create_peer_cfg_enumerator, enumerator_t*,
276 private_medcli_config_t *this, identification_t *me,
277 identification_t *other)
278 {
279 peer_enumerator_t *e;
280
281 INIT(e,
282 .public = {
283 .enumerate = (void*)_peer_enumerator_enumerate,
284 .destroy = _peer_enumerator_destroy,
285 },
286 .ike = this->ike,
287 .rekey = this->rekey,
288 .dpd = this->dpd,
289 );
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, 0);
330 }
331 else
332 {
333 enumerator->destroy(enumerator);
334 }
335 return JOB_REQUEUE_NONE;
336 }
337
338 /**
339 * schedule initiation 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 lib->processor->queue_job(lib->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 METHOD(medcli_config_t, destroy, void,
369 private_medcli_config_t *this)
370 {
371 this->ike->destroy(this->ike);
372 free(this);
373 }
374
375 /**
376 * Described in header.
377 */
378 medcli_config_t *medcli_config_create(database_t *db)
379 {
380 private_medcli_config_t *this;
381
382 INIT(this,
383 .public = {
384 .backend = {
385 .create_peer_cfg_enumerator = _create_peer_cfg_enumerator,
386 .create_ike_cfg_enumerator = _create_ike_cfg_enumerator,
387 .get_peer_cfg_by_name = _get_peer_cfg_by_name,
388 },
389 .destroy = _destroy,
390 },
391 .db = db,
392 .rekey = lib->settings->get_time(lib->settings, "medcli.rekey", 1200),
393 .dpd = lib->settings->get_time(lib->settings, "medcli.dpd", 300),
394 .ike = ike_cfg_create(FALSE, FALSE, "0.0.0.0", IKEV2_UDP_PORT,
395 "0.0.0.0", IKEV2_UDP_PORT),
396 );
397 this->ike->add_proposal(this->ike, proposal_create_default(PROTO_IKE));
398
399 schedule_autoinit(this);
400
401 return &this->public;
402 }
403