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