removing svn keyword $Id$ from all files
[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 ike_cfg_t *ike_cfg;
99 child_cfg_t *child_cfg;
100 chunk_t me, other;
101 char *address, *local_net, *remote_net;
102
103 /* query mediation server config:
104 * - build ike_cfg/peer_cfg for mediation connection on-the-fly
105 */
106 e = this->db->query(this->db,
107 "SELECT Address, ClientConfig.KeyId, MediationServerConfig.KeyId "
108 "FROM MediationServerConfig JOIN ClientConfig",
109 DB_TEXT, DB_BLOB, DB_BLOB);
110 if (!e || !e->enumerate(e, &address, &me, &other))
111 {
112 DESTROY_IF(e);
113 return NULL;
114 }
115 ike_cfg = ike_cfg_create(FALSE, FALSE, "0.0.0.0", address);
116 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
117 med_cfg = peer_cfg_create(
118 "mediation", 2, ike_cfg,
119 identification_create_from_encoding(ID_KEY_ID, me),
120 identification_create_from_encoding(ID_KEY_ID, other),
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 /* query mediated config:
130 * - use any-any ike_cfg
131 * - build peer_cfg on-the-fly using med_cfg
132 * - add a child_cfg
133 */
134 e = this->db->query(this->db,
135 "SELECT ClientConfig.KeyId, Connection.KeyId, "
136 "Connection.LocalSubnet, Connection.RemoteSubnet "
137 "FROM ClientConfig JOIN Connection "
138 "WHERE Active AND Alias = ?", DB_TEXT, name,
139 DB_BLOB, DB_BLOB, DB_TEXT, DB_TEXT);
140 if (!e || !e->enumerate(e, &me, &other, &local_net, &remote_net))
141 {
142 DESTROY_IF(e);
143 return NULL;
144 }
145 peer_cfg = peer_cfg_create(
146 name, 2, this->ike->get_ref(this->ike),
147 identification_create_from_encoding(ID_KEY_ID, me),
148 identification_create_from_encoding(ID_KEY_ID, other),
149 CERT_NEVER_SEND, UNIQUE_REPLACE,
150 1, this->rekey*60, 0, /* keytries, rekey, reauth */
151 this->rekey*5, this->rekey*3, /* jitter, overtime */
152 TRUE, this->dpd, /* mobike, dpddelay */
153 NULL, NULL, /* vip, pool */
154 FALSE, med_cfg, /* mediation, med by */
155 identification_create_from_encoding(ID_KEY_ID, other));
156
157 child_cfg = child_cfg_create(name, this->rekey*60 + this->rekey,
158 this->rekey*60, this->rekey, NULL, TRUE,
159 MODE_TUNNEL, ACTION_NONE, ACTION_NONE, FALSE);
160 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
161 child_cfg->add_traffic_selector(child_cfg, TRUE, ts_from_string(local_net));
162 child_cfg->add_traffic_selector(child_cfg, FALSE, ts_from_string(remote_net));
163 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
164 e->destroy(e);
165 return peer_cfg;
166 }
167
168 /**
169 * Implementation of backend_t.create_ike_cfg_enumerator.
170 */
171 static enumerator_t* create_ike_cfg_enumerator(private_medcli_config_t *this,
172 host_t *me, host_t *other)
173 {
174 return enumerator_create_single(this->ike, NULL);
175 }
176
177 typedef struct {
178 /** implements enumerator */
179 enumerator_t public;
180 /** inner SQL enumerator */
181 enumerator_t *inner;
182 /** currently enumerated peer config */
183 peer_cfg_t *current;
184 /** ike cfg to use in peer cfg */
185 ike_cfg_t *ike;
186 /** rekey time */
187 int rekey;
188 /** dpd time */
189 int dpd;
190 } peer_enumerator_t;
191
192 /**
193 * Implementation of peer_enumerator_t.public.enumerate
194 */
195 static bool peer_enumerator_enumerate(peer_enumerator_t *this, peer_cfg_t **cfg)
196 {
197 char *name, *local_net, *remote_net;
198 chunk_t me, other;
199 child_cfg_t *child_cfg;
200
201 DESTROY_IF(this->current);
202 if (!this->inner->enumerate(this->inner, &name, &me, &other,
203 &local_net, &remote_net))
204 {
205 this->current = NULL;
206 return FALSE;
207 }
208 this->current = peer_cfg_create(
209 name, 2, this->ike->get_ref(this->ike),
210 identification_create_from_encoding(ID_KEY_ID, me),
211 identification_create_from_encoding(ID_KEY_ID, other),
212 CERT_NEVER_SEND, UNIQUE_REPLACE,
213 1, this->rekey*60, 0, /* keytries, rekey, reauth */
214 this->rekey*5, this->rekey*3, /* jitter, overtime */
215 TRUE, this->dpd, /* mobike, dpddelay */
216 NULL, NULL, /* vip, pool */
217 FALSE, NULL, NULL); /* mediation, med by, peer id */
218 child_cfg = child_cfg_create(
219 name, this->rekey*60 + this->rekey,
220 this->rekey*60, this->rekey, NULL, TRUE,
221 MODE_TUNNEL, ACTION_NONE, ACTION_NONE, FALSE);
222 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
223 child_cfg->add_traffic_selector(child_cfg, TRUE, ts_from_string(local_net));
224 child_cfg->add_traffic_selector(child_cfg, FALSE, ts_from_string(remote_net));
225 this->current->add_child_cfg(this->current, child_cfg);
226 *cfg = this->current;
227 return TRUE;
228 }
229
230 /**
231 * Implementation of peer_enumerator_t.public.destroy
232 */
233 static void peer_enumerator_destroy(peer_enumerator_t *this)
234 {
235 DESTROY_IF(this->current);
236 this->inner->destroy(this->inner);
237 free(this);
238 }
239
240 /**
241 * Implementation of backend_t.create_peer_cfg_enumerator.
242 */
243 static enumerator_t* create_peer_cfg_enumerator(private_medcli_config_t *this,
244 identification_t *me,
245 identification_t *other)
246 {
247 peer_enumerator_t *e = malloc_thing(peer_enumerator_t);
248
249 e->current = NULL;
250 e->ike = this->ike;
251 e->rekey = this->rekey;
252 e->dpd = this->dpd;
253 e->public.enumerate = (void*)peer_enumerator_enumerate;
254 e->public.destroy = (void*)peer_enumerator_destroy;
255
256 /* filter on IDs: NULL or ANY or matching KEY_ID */
257 e->inner = this->db->query(this->db,
258 "SELECT Alias, ClientConfig.KeyId, Connection.KeyId, "
259 "Connection.LocalSubnet, Connection.RemoteSubnet "
260 "FROM ClientConfig JOIN Connection "
261 "WHERE Active AND "
262 "(? OR ClientConfig.KeyId = ?) AND (? OR Connection.KeyId = ?)",
263 DB_INT, me == NULL || me->get_type(me) == ID_ANY,
264 DB_BLOB, me && me->get_type(me) == ID_KEY_ID ?
265 me->get_encoding(me) : chunk_empty,
266 DB_INT, other == NULL || other->get_type(other) == ID_ANY,
267 DB_BLOB, other && other->get_type(other) == ID_KEY_ID ?
268 other->get_encoding(other) : chunk_empty,
269 DB_TEXT, DB_BLOB, DB_BLOB, DB_TEXT, DB_TEXT);
270 if (!e->inner)
271 {
272 free(e);
273 return NULL;
274 }
275 return &e->public;
276 }
277
278 /**
279 * initiate a peer config
280 */
281 static job_requeue_t initiate_config(peer_cfg_t *peer_cfg)
282 {
283 enumerator_t *enumerator;
284 child_cfg_t *child_cfg = NULL;;
285
286 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
287 enumerator->enumerate(enumerator, &child_cfg);
288 if (child_cfg)
289 {
290 child_cfg->get_ref(child_cfg);
291 peer_cfg->get_ref(peer_cfg);
292 enumerator->destroy(enumerator);
293 charon->controller->initiate(charon->controller,
294 peer_cfg, child_cfg, NULL, NULL);
295 }
296 else
297 {
298 enumerator->destroy(enumerator);
299 }
300 return JOB_REQUEUE_NONE;
301 }
302
303 /**
304 * schedule initation of all "active" connections
305 */
306 static void schedule_autoinit(private_medcli_config_t *this)
307 {
308 enumerator_t *e;
309 char *name;
310
311 e = this->db->query(this->db, "SELECT Alias FROM Connection WHERE Active",
312 DB_TEXT);
313 if (e)
314 {
315 while (e->enumerate(e, &name))
316 {
317 peer_cfg_t *peer_cfg;
318
319 peer_cfg = get_peer_cfg_by_name(this, name);
320 if (peer_cfg)
321 {
322 /* schedule asynchronous initiation job */
323 charon->processor->queue_job(charon->processor,
324 (job_t*)callback_job_create(
325 (callback_job_cb_t)initiate_config,
326 peer_cfg, (void*)peer_cfg->destroy, NULL));
327 }
328 }
329 e->destroy(e);
330 }
331 }
332
333 /**
334 * Implementation of medcli_config_t.destroy.
335 */
336 static void destroy(private_medcli_config_t *this)
337 {
338 this->ike->destroy(this->ike);
339 free(this);
340 }
341
342 /**
343 * Described in header.
344 */
345 medcli_config_t *medcli_config_create(database_t *db)
346 {
347 private_medcli_config_t *this = malloc_thing(private_medcli_config_t);
348
349 this->public.backend.create_peer_cfg_enumerator = (enumerator_t*(*)(backend_t*, identification_t *me, identification_t *other))create_peer_cfg_enumerator;
350 this->public.backend.create_ike_cfg_enumerator = (enumerator_t*(*)(backend_t*, host_t *me, host_t *other))create_ike_cfg_enumerator;
351 this->public.backend.get_peer_cfg_by_name = (peer_cfg_t* (*)(backend_t*,char*))get_peer_cfg_by_name;
352 this->public.destroy = (void(*)(medcli_config_t*))destroy;
353
354 this->db = db;
355 this->rekey = lib->settings->get_time(lib->settings, "medcli.rekey", 1200);
356 this->dpd = lib->settings->get_time(lib->settings, "medcli.dpd", 300);
357 this->ike = ike_cfg_create(FALSE, FALSE, "0.0.0.0", "0.0.0.0");
358 this->ike->add_proposal(this->ike, proposal_create_default(PROTO_IKE));
359
360 schedule_autoinit(this);
361
362 return &this->public;
363 }
364