1bf02e43e4b173b65efec7476622aee94731ff47
[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
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 DBG1(DBG_CFG, "mediation server id: %B", &other);
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 * Implementation of medcli_config_t.destroy.
285 */
286 static void destroy(private_medcli_config_t *this)
287 {
288 this->ike->destroy(this->ike);
289 free(this);
290 }
291
292 /**
293 * Described in header.
294 */
295 medcli_config_t *medcli_config_create(database_t *db)
296 {
297 private_medcli_config_t *this = malloc_thing(private_medcli_config_t);
298
299 this->public.backend.create_peer_cfg_enumerator = (enumerator_t*(*)(backend_t*, identification_t *me, identification_t *other))create_peer_cfg_enumerator;
300 this->public.backend.create_ike_cfg_enumerator = (enumerator_t*(*)(backend_t*, host_t *me, host_t *other))create_ike_cfg_enumerator;
301 this->public.backend.get_peer_cfg_by_name = (peer_cfg_t* (*)(backend_t*,char*))get_peer_cfg_by_name;
302 this->public.destroy = (void(*)(medcli_config_t*))destroy;
303
304 this->db = db;
305 this->rekey = lib->settings->get_int(lib->settings,
306 "medclient.rekey", 20) * 60;
307 this->dpd = lib->settings->get_int(lib->settings, "medclient.dpd", 300);
308 this->ike = ike_cfg_create(FALSE, FALSE, "0.0.0.0", "0.0.0.0");
309 this->ike->add_proposal(this->ike, proposal_create_default(PROTO_IKE));
310
311 return &this->public;
312 }
313