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