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