Adapted the config backends to the new lifetime configuration.
[strongswan.git] / src / charon / plugins / uci / uci_config.c
1 /*
2 * Copyright (C) 2008 Thomas Kallenberg
3 * Copyright (C) 2008 Tobias Brunner
4 * Copyright (C) 2008 Martin Willi
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #define _GNU_SOURCE
19 #include <string.h>
20
21 #include "uci_config.h"
22 #include "uci_parser.h"
23
24 #include <daemon.h>
25
26 typedef struct private_uci_config_t private_uci_config_t;
27
28 /**
29 * Private data of an uci_config_t object
30 */
31 struct private_uci_config_t {
32
33 /**
34 * Public part
35 */
36 uci_config_t public;
37
38 /**
39 * UCI parser context
40 */
41 uci_parser_t *parser;
42 };
43
44 /**
45 * enumerator implementation for create_peer_cfg_enumerator
46 */
47 typedef struct {
48 /** implements enumerator */
49 enumerator_t public;
50 /** currently enumerated peer config */
51 peer_cfg_t *peer_cfg;
52 /** inner uci_parser section enumerator */
53 enumerator_t *inner;
54 } peer_enumerator_t;
55
56 /**
57 * create a proposal from a string, with fallback to default
58 */
59 static proposal_t *create_proposal(char *string, protocol_id_t proto)
60 {
61 proposal_t *proposal = NULL;
62
63 if (string)
64 {
65 proposal = proposal_create_from_string(proto, string);
66 }
67 if (!proposal)
68 { /* UCI default is aes/sha1 only */
69 if (proto == PROTO_IKE)
70 {
71 proposal = proposal_create_from_string(proto,
72 "aes128-aes192-aes256-sha1-modp1536-modp2048");
73 }
74 else
75 {
76 proposal = proposal_create_from_string(proto,
77 "aes128-aes192-aes256-sha1");
78 }
79 }
80 return proposal;
81 }
82
83 /**
84 * create an traffic selector, fallback to dynamic
85 */
86 static traffic_selector_t *create_ts(char *string)
87 {
88 if (string)
89 {
90 int netbits = 32;
91 host_t *net;
92 char *pos;
93
94 string = strdupa(string);
95 pos = strchr(string, '/');
96 if (pos)
97 {
98 *pos++ = '\0';
99 netbits = atoi(pos);
100 }
101 else
102 {
103 if (strchr(string, ':'))
104 {
105 netbits = 128;
106 }
107 }
108 net = host_create_from_string(string, 0);
109 if (net)
110 {
111 return traffic_selector_create_from_subnet(net, netbits, 0, 0);
112 }
113 }
114 return traffic_selector_create_dynamic(0, 0, 65535);
115 }
116
117 /**
118 * create a rekey time from a string with hours, with fallback
119 */
120 static u_int create_rekey(char *string)
121 {
122 u_int rekey = 0;
123
124 if (string)
125 {
126 rekey = atoi(string);
127 if (rekey)
128 {
129 return rekey * 3600;
130 }
131 }
132 /* every 12 hours */
133 return 12 * 3600;
134 }
135
136 /**
137 * Implementation of peer_enumerator_t.public.enumerate
138 */
139 static bool peer_enumerator_enumerate(peer_enumerator_t *this, peer_cfg_t **cfg)
140 {
141 char *name, *ike_proposal, *esp_proposal, *ike_rekey, *esp_rekey;
142 char *local_id, *local_addr, *local_net;
143 char *remote_id, *remote_addr, *remote_net;
144 child_cfg_t *child_cfg;
145 lifetime_cfg_t *lifetime;
146 ike_cfg_t *ike_cfg;
147 auth_cfg_t *auth;
148
149 /* defaults */
150 name = "unnamed";
151 local_id = NULL;
152 remote_id = NULL;
153 local_addr = "0.0.0.0";
154 remote_addr = "0.0.0.0";
155 local_net = NULL;
156 remote_net = NULL;
157 ike_proposal = NULL;
158 esp_proposal = NULL;
159 ike_rekey = NULL;
160 esp_rekey = NULL;
161
162 if (this->inner->enumerate(this->inner, &name, &local_id, &remote_id,
163 &local_addr, &remote_addr, &local_net, &remote_net,
164 &ike_proposal, &esp_proposal, &ike_rekey, &esp_rekey))
165 {
166 DESTROY_IF(this->peer_cfg);
167 ike_cfg = ike_cfg_create(FALSE, FALSE, local_addr, remote_addr);
168 ike_cfg->add_proposal(ike_cfg, create_proposal(ike_proposal, PROTO_IKE));
169 this->peer_cfg = peer_cfg_create(
170 name, 2, ike_cfg, CERT_SEND_IF_ASKED, UNIQUE_NO,
171 1, create_rekey(ike_rekey), 0, /* keytries, rekey, reauth */
172 1800, 900, /* jitter, overtime */
173 TRUE, 60, /* mobike, dpddelay */
174 NULL, NULL, /* vip, pool */
175 FALSE, NULL, NULL); /* mediation, med by, peer id */
176 auth = auth_cfg_create();
177 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PSK);
178 auth->add(auth, AUTH_RULE_IDENTITY,
179 identification_create_from_string(local_id));
180 this->peer_cfg->add_auth_cfg(this->peer_cfg, auth, TRUE);
181
182 auth = auth_cfg_create();
183 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PSK);
184 if (remote_id)
185 {
186 auth->add(auth, AUTH_RULE_IDENTITY,
187 identification_create_from_string(remote_id));
188 }
189 this->peer_cfg->add_auth_cfg(this->peer_cfg, auth, FALSE);
190 lifetime = lifetime_cfg_create_time(create_rekey(esp_rekey) + 300,
191 create_rekey(esp_rekey), 300);
192 child_cfg = child_cfg_create(name, lifetime, NULL, TRUE, MODE_TUNNEL,
193 ACTION_NONE, ACTION_NONE, FALSE);
194 child_cfg->add_proposal(child_cfg, create_proposal(esp_proposal, PROTO_ESP));
195 child_cfg->add_traffic_selector(child_cfg, TRUE, create_ts(local_net));
196 child_cfg->add_traffic_selector(child_cfg, FALSE, create_ts(remote_net));
197 this->peer_cfg->add_child_cfg(this->peer_cfg, child_cfg);
198 *cfg = this->peer_cfg;
199 return TRUE;
200 }
201 return FALSE;
202 }
203
204 /**
205 * Implementation of peer_enumerator_t.public.destroy
206 */
207 static void peer_enumerator_destroy(peer_enumerator_t *this)
208 {
209 DESTROY_IF(this->peer_cfg);
210 this->inner->destroy(this->inner);
211 free(this);
212 }
213
214 /**
215 * Implementation of backend_t.create_peer_cfg_enumerator.
216 */
217 static enumerator_t* create_peer_cfg_enumerator(private_uci_config_t *this,
218 identification_t *me,
219 identification_t *other)
220 {
221 peer_enumerator_t *e = malloc_thing(peer_enumerator_t);
222
223 e->public.enumerate = (void*)peer_enumerator_enumerate;
224 e->public.destroy = (void*)peer_enumerator_destroy;
225 e->peer_cfg = NULL;
226 e->inner = this->parser->create_section_enumerator(this->parser,
227 "local_id", "remote_id", "local_addr", "remote_addr",
228 "local_net", "remote_net", "ike_proposal", "esp_proposal",
229 "ike_rekey", "esp_rekey", NULL);
230 if (!e->inner)
231 {
232 free(e);
233 return NULL;
234 }
235 return &e->public;
236 }
237
238 /**
239 * enumerator implementation for create_ike_cfg_enumerator
240 */
241 typedef struct {
242 /** implements enumerator */
243 enumerator_t public;
244 /** currently enumerated ike config */
245 ike_cfg_t *ike_cfg;
246 /** inner uci_parser section enumerator */
247 enumerator_t *inner;
248 } ike_enumerator_t;
249
250 /**
251 * Implementation of peer_enumerator_t.public.enumerate
252 */
253 static bool ike_enumerator_enumerate(ike_enumerator_t *this, ike_cfg_t **cfg)
254 {
255 char *local_addr, *remote_addr, *ike_proposal;
256
257 /* defaults */
258 local_addr = "0.0.0.0";
259 remote_addr = "0.0.0.0";
260 ike_proposal = NULL;
261
262 if (this->inner->enumerate(this->inner, NULL,
263 &local_addr, &remote_addr, &ike_proposal))
264 {
265 DESTROY_IF(this->ike_cfg);
266 this->ike_cfg = ike_cfg_create(FALSE, FALSE, local_addr, remote_addr);
267 this->ike_cfg->add_proposal(this->ike_cfg,
268 create_proposal(ike_proposal, PROTO_IKE));
269
270 *cfg = this->ike_cfg;
271 return TRUE;
272 }
273 return FALSE;
274 }
275
276 /**
277 * Implementation of ike_enumerator_t.public.destroy
278 */
279 static void ike_enumerator_destroy(ike_enumerator_t *this)
280 {
281 DESTROY_IF(this->ike_cfg);
282 this->inner->destroy(this->inner);
283 free(this);
284 }
285
286 /**
287 * Implementation of backend_t.create_ike_cfg_enumerator.
288 */
289 static enumerator_t* create_ike_cfg_enumerator(private_uci_config_t *this,
290 host_t *me, host_t *other)
291 {
292 ike_enumerator_t *e = malloc_thing(ike_enumerator_t);
293
294 e->public.enumerate = (void*)ike_enumerator_enumerate;
295 e->public.destroy = (void*)ike_enumerator_destroy;
296 e->ike_cfg = NULL;
297 e->inner = this->parser->create_section_enumerator(this->parser,
298 "local_addr", "remote_addr", "ike_proposal", NULL);
299 if (!e->inner)
300 {
301 free(e);
302 return NULL;
303 }
304 return &e->public;
305 }
306
307 /**
308 * implements backend_t.get_peer_cfg_by_name.
309 */
310 static peer_cfg_t *get_peer_cfg_by_name(private_uci_config_t *this, char *name)
311 {
312 enumerator_t *enumerator;
313 peer_cfg_t *current, *found = NULL;
314
315 enumerator = create_peer_cfg_enumerator(this, NULL, NULL);
316 if (enumerator)
317 {
318 while (enumerator->enumerate(enumerator, &current))
319 {
320 if (streq(name, current->get_name(current)))
321 {
322 found = current->get_ref(current);
323 break;
324 }
325 }
326 enumerator->destroy(enumerator);
327 }
328 return found;
329 }
330
331 /**
332 * Implementation of uci_config_t.destroy.
333 */
334 static void destroy(private_uci_config_t *this)
335 {
336 free(this);
337 }
338
339 /**
340 * Described in header.
341 */
342 uci_config_t *uci_config_create(uci_parser_t *parser)
343 {
344 private_uci_config_t *this = malloc_thing(private_uci_config_t);
345
346 this->public.backend.create_peer_cfg_enumerator = (enumerator_t*(*)(backend_t*, identification_t *me, identification_t *other))create_peer_cfg_enumerator;
347 this->public.backend.create_ike_cfg_enumerator = (enumerator_t*(*)(backend_t*, host_t *me, host_t *other))create_ike_cfg_enumerator;
348 this->public.backend.get_peer_cfg_by_name = (peer_cfg_t* (*)(backend_t*,char*))get_peer_cfg_by_name;
349 this->public.destroy = (void(*)(uci_config_t*))destroy;
350 this->parser = parser;
351
352 return &this->public;
353 }
354