5654fc51ef8f6e695da2cb77942825e5f72b46f0
[strongswan.git] / src / libcharon / 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 * HSR 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 traffic_selector_t *ts;
91
92 ts = traffic_selector_create_from_cidr(string, 0, 0, 65535);
93 if (ts)
94 {
95 return ts;
96 }
97 }
98 return traffic_selector_create_dynamic(0, 0, 65535);
99 }
100
101 /**
102 * create a rekey time from a string with hours, with fallback
103 */
104 static u_int create_rekey(char *string)
105 {
106 u_int rekey = 0;
107
108 if (string)
109 {
110 rekey = atoi(string);
111 if (rekey)
112 {
113 return rekey * 3600;
114 }
115 }
116 /* every 12 hours */
117 return 12 * 3600;
118 }
119
120 METHOD(enumerator_t, peer_enumerator_enumerate, bool,
121 peer_enumerator_t *this, va_list args)
122 {
123 char *name, *ike_proposal, *esp_proposal, *ike_rekey, *esp_rekey;
124 char *local_id, *local_addr, *local_net;
125 char *remote_id, *remote_addr, *remote_net;
126 peer_cfg_t **cfg;
127 child_cfg_t *child_cfg;
128 ike_cfg_t *ike_cfg;
129 auth_cfg_t *auth;
130 peer_cfg_create_t peer = {
131 .cert_policy = CERT_SEND_IF_ASKED,
132 .unique = UNIQUE_NO,
133 .keyingtries = 1,
134 .jitter_time = 1800,
135 .over_time = 900,
136 .dpd = 60,
137 };
138 child_cfg_create_t child = {
139 .lifetime = {
140 .time = {
141 .life = create_rekey(esp_rekey) + 300,
142 .rekey = create_rekey(esp_rekey),
143 .jitter = 300
144 },
145 },
146 .mode = MODE_TUNNEL,
147 };
148
149 VA_ARGS_VGET(args, cfg);
150
151 /* defaults */
152 name = "unnamed";
153 local_id = NULL;
154 remote_id = NULL;
155 local_addr = "0.0.0.0";
156 remote_addr = "0.0.0.0";
157 local_net = NULL;
158 remote_net = NULL;
159 ike_proposal = NULL;
160 esp_proposal = NULL;
161 ike_rekey = NULL;
162 esp_rekey = NULL;
163
164 if (this->inner->enumerate(this->inner, &name, &local_id, &remote_id,
165 &local_addr, &remote_addr, &local_net, &remote_net,
166 &ike_proposal, &esp_proposal, &ike_rekey, &esp_rekey))
167 {
168 DESTROY_IF(this->peer_cfg);
169 ike_cfg = ike_cfg_create(IKEV2, FALSE, FALSE, local_addr,
170 charon->socket->get_port(charon->socket, FALSE),
171 remote_addr, IKEV2_UDP_PORT,
172 FRAGMENTATION_NO, 0);
173 ike_cfg->add_proposal(ike_cfg, create_proposal(ike_proposal, PROTO_IKE));
174 peer.rekey_time = create_rekey(ike_rekey);
175 this->peer_cfg = peer_cfg_create(name, ike_cfg, &peer);
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
191 child_cfg = child_cfg_create(name, &child);
192 child_cfg->add_proposal(child_cfg, create_proposal(esp_proposal, PROTO_ESP));
193 child_cfg->add_traffic_selector(child_cfg, TRUE, create_ts(local_net));
194 child_cfg->add_traffic_selector(child_cfg, FALSE, create_ts(remote_net));
195 this->peer_cfg->add_child_cfg(this->peer_cfg, child_cfg);
196 *cfg = this->peer_cfg;
197 return TRUE;
198 }
199 return FALSE;
200 }
201
202
203 METHOD(enumerator_t, peer_enumerator_destroy, void,
204 peer_enumerator_t *this)
205 {
206 DESTROY_IF(this->peer_cfg);
207 this->inner->destroy(this->inner);
208 free(this);
209 }
210
211 METHOD(backend_t, create_peer_cfg_enumerator, enumerator_t*,
212 private_uci_config_t *this, identification_t *me, identification_t *other)
213 {
214 peer_enumerator_t *e;
215
216 INIT(e,
217 .public = {
218 .enumerate = enumerator_enumerate_default,
219 .venumerate = _peer_enumerator_enumerate,
220 .destroy = _peer_enumerator_destroy,
221 },
222 .inner = this->parser->create_section_enumerator(this->parser,
223 "local_id", "remote_id", "local_addr", "remote_addr",
224 "local_net", "remote_net", "ike_proposal", "esp_proposal",
225 "ike_rekey", "esp_rekey", NULL),
226 );
227 if (!e->inner)
228 {
229 free(e);
230 return NULL;
231 }
232 return &e->public;
233 }
234
235 /**
236 * enumerator implementation for create_ike_cfg_enumerator
237 */
238 typedef struct {
239 /** implements enumerator */
240 enumerator_t public;
241 /** currently enumerated ike config */
242 ike_cfg_t *ike_cfg;
243 /** inner uci_parser section enumerator */
244 enumerator_t *inner;
245 } ike_enumerator_t;
246
247 METHOD(enumerator_t, ike_enumerator_enumerate, bool,
248 ike_enumerator_t *this, va_list args)
249 {
250 ike_cfg_t **cfg;
251 char *local_addr, *remote_addr, *ike_proposal;
252
253 VA_ARGS_VGET(args, cfg);
254
255 /* defaults */
256 local_addr = "0.0.0.0";
257 remote_addr = "0.0.0.0";
258 ike_proposal = NULL;
259
260 if (this->inner->enumerate(this->inner, NULL,
261 &local_addr, &remote_addr, &ike_proposal))
262 {
263 DESTROY_IF(this->ike_cfg);
264 this->ike_cfg = ike_cfg_create(IKEV2, FALSE, FALSE, local_addr,
265 charon->socket->get_port(charon->socket, FALSE),
266 remote_addr, IKEV2_UDP_PORT,
267 FRAGMENTATION_NO, 0);
268 this->ike_cfg->add_proposal(this->ike_cfg,
269 create_proposal(ike_proposal, PROTO_IKE));
270
271 *cfg = this->ike_cfg;
272 return TRUE;
273 }
274 return FALSE;
275 }
276
277 METHOD(enumerator_t, ike_enumerator_destroy, void,
278 ike_enumerator_t *this)
279 {
280 DESTROY_IF(this->ike_cfg);
281 this->inner->destroy(this->inner);
282 free(this);
283 }
284
285 METHOD(backend_t, create_ike_cfg_enumerator, enumerator_t*,
286 private_uci_config_t *this, host_t *me, host_t *other)
287 {
288 ike_enumerator_t *e;
289
290 INIT(e,
291 .public = {
292 .enumerate = enumerator_enumerate_default,
293 .venumerate = _ike_enumerator_enumerate,
294 .destroy = _ike_enumerator_destroy,
295 },
296 .inner = this->parser->create_section_enumerator(this->parser,
297 "local_addr", "remote_addr", "ike_proposal", NULL),
298 );
299 if (!e->inner)
300 {
301 free(e);
302 return NULL;
303 }
304 return &e->public;
305 }
306
307 METHOD(backend_t, get_peer_cfg_by_name, peer_cfg_t*,
308 private_uci_config_t *this, char *name)
309 {
310 enumerator_t *enumerator;
311 peer_cfg_t *current, *found = NULL;
312
313 enumerator = create_peer_cfg_enumerator(this, NULL, NULL);
314 if (enumerator)
315 {
316 while (enumerator->enumerate(enumerator, &current))
317 {
318 if (streq(name, current->get_name(current)))
319 {
320 found = current->get_ref(current);
321 break;
322 }
323 }
324 enumerator->destroy(enumerator);
325 }
326 return found;
327 }
328
329 METHOD(uci_config_t, destroy, void,
330 private_uci_config_t *this)
331 {
332 free(this);
333 }
334
335 /**
336 * Described in header.
337 */
338 uci_config_t *uci_config_create(uci_parser_t *parser)
339 {
340 private_uci_config_t *this;
341
342 INIT(this,
343 .public = {
344 .backend = {
345 .create_peer_cfg_enumerator = _create_peer_cfg_enumerator,
346 .create_ike_cfg_enumerator = _create_ike_cfg_enumerator,
347 .get_peer_cfg_by_name = _get_peer_cfg_by_name,
348 },
349 .destroy = _destroy,
350 },
351 .parser = parser,
352 );
353
354 return &this->public;
355 }