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