first simple prototype of a UCI configuration plugin for OpenWRT
[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 * $Id$
18 */
19
20 #include "uci_config.h"
21 #include "uci_parser.h"
22
23 #include <daemon.h>
24
25 typedef struct private_uci_config_t private_uci_config_t;
26
27 /**
28 * Private data of an uci_config_t object
29 */
30 struct private_uci_config_t {
31
32 /**
33 * Public part
34 */
35 uci_config_t public;
36
37 /**
38 * UCI parser context
39 */
40 uci_parser_t *parser;
41 };
42
43 /**
44 * enumerator implementation for create_peer_cfg_enumerator
45 */
46 typedef struct {
47 /** implements enumerator */
48 enumerator_t public;
49 /** currently enumerated peer config */
50 peer_cfg_t *peer_cfg;
51 /** inner uci_parser section enumerator */
52 enumerator_t *inner;
53 } peer_enumerator_t;
54
55 /**
56 * Implementation of peer_enumerator_t.public.enumerate
57 */
58 static bool peer_enumerator_enumerate(peer_enumerator_t *this, peer_cfg_t **cfg)
59 {
60 char *name, *local_id, *remote_ip;
61 child_cfg_t *child_cfg;
62 ike_cfg_t *ike_cfg;
63
64 /* defaults */
65 name = "unnamed";
66 local_id = "%any";
67 remote_ip = "0.0.0.0";
68
69 if (this->inner->enumerate(this->inner, &name, &local_id, &remote_ip))
70 {
71 DESTROY_IF(this->peer_cfg);
72 ike_cfg = ike_cfg_create(FALSE, FALSE, "0.0.0.0", remote_ip);
73 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
74 this->peer_cfg = peer_cfg_create(
75 name, 2, ike_cfg,
76 identification_create_from_string(local_id),
77 identification_create_from_encoding(ID_ANY, chunk_empty),
78 CERT_SEND_IF_ASKED, UNIQUE_NO, CONF_AUTH_PSK,
79 0, 0, /* EAP method, vendor */
80 1, 3600*12, 0, /* keytries, rekey, reauth */
81 3600, 1800, /* jitter, overtime */
82 TRUE, 60, /* mobike, dpddelay */
83 NULL, NULL, /* vip, pool */
84 FALSE, NULL, NULL); /* mediation, med by, peer id */
85 child_cfg = child_cfg_create(
86 name, 3600*4, 3600*3, 360, NULL, TRUE,
87 MODE_TUNNEL, ACTION_NONE, ACTION_NONE, FALSE);
88 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
89 child_cfg->add_traffic_selector(child_cfg, TRUE,
90 traffic_selector_create_dynamic(0, 0, 65535));
91 child_cfg->add_traffic_selector(child_cfg, FALSE,
92 traffic_selector_create_dynamic(0, 0, 65535));
93 this->peer_cfg->add_child_cfg(this->peer_cfg, child_cfg);
94 *cfg = this->peer_cfg;
95 return TRUE;
96 }
97 return FALSE;
98 }
99
100 /**
101 * Implementation of peer_enumerator_t.public.destroy
102 */
103 static void peer_enumerator_destroy(peer_enumerator_t *this)
104 {
105 DESTROY_IF(this->peer_cfg);
106 this->inner->destroy(this->inner);
107 free(this);
108 }
109
110 /**
111 * Implementation of backend_t.create_peer_cfg_enumerator.
112 */
113 static enumerator_t* create_peer_cfg_enumerator(private_uci_config_t *this,
114 identification_t *me,
115 identification_t *other)
116 {
117 peer_enumerator_t *e = malloc_thing(peer_enumerator_t);
118
119 e->public.enumerate = (void*)peer_enumerator_enumerate;
120 e->public.destroy = (void*)peer_enumerator_destroy;
121 e->peer_cfg = NULL;
122 e->inner = this->parser->create_section_enumerator(this->parser,
123 "local_id", "remote_ip", NULL);
124 if (!e->inner)
125 {
126 free(e);
127 return NULL;
128 }
129 return &e->public;
130 }
131
132 /**
133 * enumerator implementation for create_ike_cfg_enumerator
134 */
135 typedef struct {
136 /** implements enumerator */
137 enumerator_t public;
138 /** currently enumerated ike config */
139 ike_cfg_t *ike_cfg;
140 /** inner uci_parser section enumerator */
141 enumerator_t *inner;
142 } ike_enumerator_t;
143
144 /**
145 * Implementation of peer_enumerator_t.public.enumerate
146 */
147 static bool ike_enumerator_enumerate(ike_enumerator_t *this, ike_cfg_t **cfg)
148 {
149 char *name, *remote_ip;
150
151 /* defaults */
152 name = "unnamed";
153 remote_ip = "0.0.0.0";
154
155 if (this->inner->enumerate(this->inner, &name, &remote_ip))
156 {
157 DESTROY_IF(this->ike_cfg);
158 this->ike_cfg = ike_cfg_create(FALSE, FALSE, "0.0.0.0", remote_ip);
159 this->ike_cfg->add_proposal(this->ike_cfg,
160 proposal_create_default(PROTO_IKE));
161
162 *cfg = this->ike_cfg;
163 return TRUE;
164 }
165 return FALSE;
166 }
167
168 /**
169 * Implementation of ike_enumerator_t.public.destroy
170 */
171 static void ike_enumerator_destroy(ike_enumerator_t *this)
172 {
173 DESTROY_IF(this->ike_cfg);
174 this->inner->destroy(this->inner);
175 free(this);
176 }
177
178 /**
179 * Implementation of backend_t.create_ike_cfg_enumerator.
180 */
181 static enumerator_t* create_ike_cfg_enumerator(private_uci_config_t *this,
182 host_t *me, host_t *other)
183 {
184 ike_enumerator_t *e = malloc_thing(ike_enumerator_t);
185
186 e->public.enumerate = (void*)ike_enumerator_enumerate;
187 e->public.destroy = (void*)ike_enumerator_destroy;
188 e->ike_cfg = NULL;
189 e->inner = this->parser->create_section_enumerator(this->parser,
190 "remote_ip", NULL);
191 if (!e->inner)
192 {
193 free(e);
194 return NULL;
195 }
196 return &e->public;
197 }
198
199 /**
200 * implements backend_t.get_peer_cfg_by_name.
201 */
202 static peer_cfg_t *get_peer_cfg_by_name(private_uci_config_t *this, char *name)
203 {
204 enumerator_t *enumerator;
205 peer_cfg_t *current, *found = NULL;
206
207 enumerator = create_peer_cfg_enumerator(this, NULL, NULL);
208 if (enumerator)
209 {
210 while (enumerator->enumerate(enumerator, &current))
211 {
212 if (streq(name, current->get_name(current)))
213 {
214 found = current->get_ref(current);
215 break;
216 }
217 }
218 enumerator->destroy(enumerator);
219 }
220 return found;
221 }
222
223 /**
224 * Implementation of uci_config_t.destroy.
225 */
226 static void destroy(private_uci_config_t *this)
227 {
228 free(this);
229 }
230
231 /**
232 * Described in header.
233 */
234 uci_config_t *uci_config_create(uci_parser_t *parser)
235 {
236 private_uci_config_t *this = malloc_thing(private_uci_config_t);
237
238 this->public.backend.create_peer_cfg_enumerator = (enumerator_t*(*)(backend_t*, identification_t *me, identification_t *other))create_peer_cfg_enumerator;
239 this->public.backend.create_ike_cfg_enumerator = (enumerator_t*(*)(backend_t*, host_t *me, host_t *other))create_ike_cfg_enumerator;
240 this->public.backend.get_peer_cfg_by_name = (peer_cfg_t* (*)(backend_t*,char*))get_peer_cfg_by_name;
241 this->public.destroy = (void(*)(uci_config_t*))destroy;
242
243 this->parser = parser;
244
245 return &this->public;
246 }
247