Remove version argument on peer_cfg constructor, use ike_cfg version instead
[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(IKEV2, FALSE, FALSE,
122 "0.0.0.0", FALSE,
123 charon->socket->get_port(charon->socket, FALSE),
124 address, FALSE, IKEV2_UDP_PORT);
125 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
126 med_cfg = peer_cfg_create(
127 "mediation", ike_cfg,
128 CERT_NEVER_SEND, UNIQUE_REPLACE,
129 1, this->rekey*60, 0, /* keytries, rekey, reauth */
130 this->rekey*5, this->rekey*3, /* jitter, overtime */
131 TRUE, FALSE, /* mobike, aggressive */
132 this->dpd, 0, /* DPD delay, timeout */
133 TRUE, NULL, NULL); /* mediation, med by, peer id */
134 e->destroy(e);
135
136 auth = auth_cfg_create();
137 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
138 auth->add(auth, AUTH_RULE_IDENTITY,
139 identification_create_from_encoding(ID_KEY_ID, me));
140 med_cfg->add_auth_cfg(med_cfg, auth, TRUE);
141 auth = auth_cfg_create();
142 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
143 auth->add(auth, AUTH_RULE_IDENTITY,
144 identification_create_from_encoding(ID_KEY_ID, other));
145 med_cfg->add_auth_cfg(med_cfg, auth, FALSE);
146
147 /* query mediated config:
148 * - use any-any ike_cfg
149 * - build peer_cfg on-the-fly using med_cfg
150 * - add a child_cfg
151 */
152 e = this->db->query(this->db,
153 "SELECT ClientConfig.KeyId, Connection.KeyId, "
154 "Connection.LocalSubnet, Connection.RemoteSubnet "
155 "FROM ClientConfig JOIN Connection "
156 "WHERE Active AND Alias = ?", DB_TEXT, name,
157 DB_BLOB, DB_BLOB, DB_TEXT, DB_TEXT);
158 if (!e || !e->enumerate(e, &me, &other, &local_net, &remote_net))
159 {
160 DESTROY_IF(e);
161 return NULL;
162 }
163 peer_cfg = peer_cfg_create(
164 name, this->ike->get_ref(this->ike),
165 CERT_NEVER_SEND, UNIQUE_REPLACE,
166 1, this->rekey*60, 0, /* keytries, rekey, reauth */
167 this->rekey*5, this->rekey*3, /* jitter, overtime */
168 TRUE, FALSE, /* mobike, aggressive */
169 this->dpd, 0, /* DPD delay, timeout */
170 FALSE, med_cfg, /* mediation, med by */
171 identification_create_from_encoding(ID_KEY_ID, other));
172
173 auth = auth_cfg_create();
174 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
175 auth->add(auth, AUTH_RULE_IDENTITY,
176 identification_create_from_encoding(ID_KEY_ID, me));
177 peer_cfg->add_auth_cfg(peer_cfg, auth, TRUE);
178 auth = auth_cfg_create();
179 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
180 auth->add(auth, AUTH_RULE_IDENTITY,
181 identification_create_from_encoding(ID_KEY_ID, other));
182 peer_cfg->add_auth_cfg(peer_cfg, auth, FALSE);
183
184 child_cfg = child_cfg_create(name, &lifetime, NULL, TRUE, MODE_TUNNEL,
185 ACTION_NONE, ACTION_NONE, ACTION_NONE, FALSE,
186 0, 0, NULL, NULL, 0);
187 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
188 child_cfg->add_traffic_selector(child_cfg, TRUE, ts_from_string(local_net));
189 child_cfg->add_traffic_selector(child_cfg, FALSE, ts_from_string(remote_net));
190 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
191 e->destroy(e);
192 return peer_cfg;
193 }
194
195 METHOD(backend_t, create_ike_cfg_enumerator, enumerator_t*,
196 private_medcli_config_t *this, host_t *me, host_t *other)
197 {
198 return enumerator_create_single(this->ike, NULL);
199 }
200
201 typedef struct {
202 /** implements enumerator */
203 enumerator_t public;
204 /** inner SQL enumerator */
205 enumerator_t *inner;
206 /** currently enumerated peer config */
207 peer_cfg_t *current;
208 /** ike cfg to use in peer cfg */
209 ike_cfg_t *ike;
210 /** rekey time */
211 int rekey;
212 /** dpd time */
213 int dpd;
214 } peer_enumerator_t;
215
216 METHOD(enumerator_t, peer_enumerator_enumerate, bool,
217 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 .time = {
225 .life = this->rekey * 60 + this->rekey,
226 .rekey = this->rekey,
227 .jitter = this->rekey
228 }
229 };
230
231 DESTROY_IF(this->current);
232 if (!this->inner->enumerate(this->inner, &name, &me, &other,
233 &local_net, &remote_net))
234 {
235 this->current = NULL;
236 return FALSE;
237 }
238 this->current = peer_cfg_create(
239 name, this->ike->get_ref(this->ike),
240 CERT_NEVER_SEND, UNIQUE_REPLACE,
241 1, this->rekey*60, 0, /* keytries, rekey, reauth */
242 this->rekey*5, this->rekey*3, /* jitter, overtime */
243 TRUE, FALSE, /* mobike, aggressive */
244 this->dpd, 0, /* DPD delay, timeout */
245 FALSE, NULL, NULL); /* mediation, med by, peer id */
246
247 auth = auth_cfg_create();
248 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
249 auth->add(auth, AUTH_RULE_IDENTITY,
250 identification_create_from_encoding(ID_KEY_ID, me));
251 this->current->add_auth_cfg(this->current, auth, TRUE);
252 auth = auth_cfg_create();
253 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
254 auth->add(auth, AUTH_RULE_IDENTITY,
255 identification_create_from_encoding(ID_KEY_ID, other));
256 this->current->add_auth_cfg(this->current, auth, FALSE);
257
258 child_cfg = child_cfg_create(name, &lifetime, NULL, TRUE, MODE_TUNNEL,
259 ACTION_NONE, ACTION_NONE, ACTION_NONE, FALSE,
260 0, 0, NULL, NULL, 0);
261 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
262 child_cfg->add_traffic_selector(child_cfg, TRUE, ts_from_string(local_net));
263 child_cfg->add_traffic_selector(child_cfg, FALSE, ts_from_string(remote_net));
264 this->current->add_child_cfg(this->current, child_cfg);
265 *cfg = this->current;
266 return TRUE;
267 }
268
269 METHOD(enumerator_t, peer_enumerator_destroy, void,
270 peer_enumerator_t *this)
271 {
272 DESTROY_IF(this->current);
273 this->inner->destroy(this->inner);
274 free(this);
275 }
276
277 METHOD(backend_t, create_peer_cfg_enumerator, enumerator_t*,
278 private_medcli_config_t *this, identification_t *me,
279 identification_t *other)
280 {
281 peer_enumerator_t *e;
282
283 INIT(e,
284 .public = {
285 .enumerate = (void*)_peer_enumerator_enumerate,
286 .destroy = _peer_enumerator_destroy,
287 },
288 .ike = this->ike,
289 .rekey = this->rekey,
290 .dpd = this->dpd,
291 );
292
293 /* filter on IDs: NULL or ANY or matching KEY_ID */
294 e->inner = this->db->query(this->db,
295 "SELECT Alias, ClientConfig.KeyId, Connection.KeyId, "
296 "Connection.LocalSubnet, Connection.RemoteSubnet "
297 "FROM ClientConfig JOIN Connection "
298 "WHERE Active AND "
299 "(? OR ClientConfig.KeyId = ?) AND (? OR Connection.KeyId = ?)",
300 DB_INT, me == NULL || me->get_type(me) == ID_ANY,
301 DB_BLOB, me && me->get_type(me) == ID_KEY_ID ?
302 me->get_encoding(me) : chunk_empty,
303 DB_INT, other == NULL || other->get_type(other) == ID_ANY,
304 DB_BLOB, other && other->get_type(other) == ID_KEY_ID ?
305 other->get_encoding(other) : chunk_empty,
306 DB_TEXT, DB_BLOB, DB_BLOB, DB_TEXT, DB_TEXT);
307 if (!e->inner)
308 {
309 free(e);
310 return NULL;
311 }
312 return &e->public;
313 }
314
315 /**
316 * initiate a peer config
317 */
318 static job_requeue_t initiate_config(peer_cfg_t *peer_cfg)
319 {
320 enumerator_t *enumerator;
321 child_cfg_t *child_cfg = NULL;;
322
323 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
324 enumerator->enumerate(enumerator, &child_cfg);
325 if (child_cfg)
326 {
327 child_cfg->get_ref(child_cfg);
328 peer_cfg->get_ref(peer_cfg);
329 enumerator->destroy(enumerator);
330 charon->controller->initiate(charon->controller,
331 peer_cfg, child_cfg, NULL, NULL, 0);
332 }
333 else
334 {
335 enumerator->destroy(enumerator);
336 }
337 return JOB_REQUEUE_NONE;
338 }
339
340 /**
341 * schedule initiation of all "active" connections
342 */
343 static void schedule_autoinit(private_medcli_config_t *this)
344 {
345 enumerator_t *e;
346 char *name;
347
348 e = this->db->query(this->db, "SELECT Alias FROM Connection WHERE Active",
349 DB_TEXT);
350 if (e)
351 {
352 while (e->enumerate(e, &name))
353 {
354 peer_cfg_t *peer_cfg;
355
356 peer_cfg = get_peer_cfg_by_name(this, name);
357 if (peer_cfg)
358 {
359 /* schedule asynchronous initiation job */
360 lib->processor->queue_job(lib->processor,
361 (job_t*)callback_job_create(
362 (callback_job_cb_t)initiate_config,
363 peer_cfg, (void*)peer_cfg->destroy, NULL));
364 }
365 }
366 e->destroy(e);
367 }
368 }
369
370 METHOD(medcli_config_t, destroy, void,
371 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;
383
384 INIT(this,
385 .public = {
386 .backend = {
387 .create_peer_cfg_enumerator = _create_peer_cfg_enumerator,
388 .create_ike_cfg_enumerator = _create_ike_cfg_enumerator,
389 .get_peer_cfg_by_name = _get_peer_cfg_by_name,
390 },
391 .destroy = _destroy,
392 },
393 .db = db,
394 .rekey = lib->settings->get_time(lib->settings, "medcli.rekey", 1200),
395 .dpd = lib->settings->get_time(lib->settings, "medcli.dpd", 300),
396 .ike = ike_cfg_create(IKEV2, FALSE, FALSE,
397 "0.0.0.0", FALSE,
398 charon->socket->get_port(charon->socket, FALSE),
399 "0.0.0.0", FALSE, IKEV2_UDP_PORT),
400 );
401 this->ike->add_proposal(this->ike, proposal_create_default(PROTO_IKE));
402
403 schedule_autoinit(this);
404
405 return &this->public;
406 }
407