fixed sqlite_backend compilation to respect changes from [3238]
[strongswan.git] / src / charon / config / backends / sqlite_backend.c
1 /**
2 * @file sqlite_backend.c
3 *
4 * @brief Implementation of sqlite_backend_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <string.h>
24 #include <sqlite3.h>
25
26 #include "sqlite_backend.h"
27
28 #include <daemon.h>
29
30
31 typedef struct private_sqlite_backend_t private_sqlite_backend_t;
32
33 /**
34 * Private data of an sqlite_backend_t object
35 */
36 struct private_sqlite_backend_t {
37
38 /**
39 * Public part
40 */
41 sqlite_backend_t public;
42
43 /**
44 * SQLite database handle
45 */
46 sqlite3 *db;
47 };
48
49 /**
50 * implements backen_t.get_ike_cfg.
51 */
52 static ike_cfg_t *get_ike_cfg(private_sqlite_backend_t *this,
53 host_t *my_host, host_t *other_host)
54 {
55 return NULL;
56 }
57
58 /**
59 * add TS with child "id" to "child_cfg"
60 */
61 static void add_ts(private_sqlite_backend_t *this, child_cfg_t *child_cfg, int id)
62 {
63 sqlite3_stmt *stmt;
64
65 if (sqlite3_prepare_v2(this->db,
66 "SELECT type, protocol, start_addr, end_addr, start_port, end_port, kind "
67 "FROM traffic_selectors, child_config_traffic_selector "
68 "ON traffic_selectors.oid = child_config_traffic_selector.traffic_selector "
69 "WHERE child_config_traffic_selector.child_cfg = ?;",
70 -1, &stmt, NULL) == SQLITE_OK &&
71 sqlite3_bind_int(stmt, 1, id) == SQLITE_OK)
72 {
73 while (sqlite3_step(stmt) == SQLITE_ROW)
74 {
75 traffic_selector_t *ts;
76 bool local = FALSE;
77 enum {
78 TS_LOCAL = 0,
79 TS_REMOTE = 1,
80 TS_LOCAL_DYNAMIC = 2,
81 TS_REMOTE_DYNAMIC = 3,
82 } kind;
83
84 kind = sqlite3_column_int(stmt, 6);
85 switch (kind)
86 {
87 case TS_LOCAL:
88 local = TRUE;
89 /* FALL */
90 case TS_REMOTE:
91 ts = traffic_selector_create_from_string(
92 sqlite3_column_int(stmt, 1), /* protocol */
93 sqlite3_column_int(stmt, 0), /* type */
94 (char*)sqlite3_column_text(stmt, 2), /* from addr */
95 sqlite3_column_int(stmt, 4), /* from port */
96 (char*)sqlite3_column_text(stmt, 3), /* to addr */
97 sqlite3_column_int(stmt, 5)); /* to port */
98 break;
99 case TS_LOCAL_DYNAMIC:
100 local = TRUE;
101 /* FALL */
102 case TS_REMOTE_DYNAMIC:
103 ts = traffic_selector_create_dynamic(
104 sqlite3_column_int(stmt, 1), /* protocol */
105 sqlite3_column_int(stmt, 0), /* type */
106 sqlite3_column_int(stmt, 4), /* from port */
107 sqlite3_column_int(stmt, 5)); /* to port */
108 break;
109 default:
110 continue;
111 }
112 if (ts)
113 {
114 child_cfg->add_traffic_selector(child_cfg, local, ts);
115 }
116 }
117 }
118 sqlite3_finalize(stmt);
119 }
120
121 /**
122 * add childrens belonging to config with "id" to "peer_cfg"
123 */
124 static void add_children(private_sqlite_backend_t *this, peer_cfg_t *peer_cfg, int id)
125 {
126 sqlite3_stmt *stmt;
127 child_cfg_t *child_cfg;
128
129 if (sqlite3_prepare_v2(this->db,
130 "SELECT child_configs.oid, name, updown, hostaccess, mode, "
131 "lifetime, rekeytime, jitter "
132 "FROM child_configs, peer_config_child_config "
133 "ON child_configs.oid = peer_config_child_config.child_cfg "
134 "WHERE peer_config_child_config.peer_cfg = ?;",
135 -1, &stmt, NULL) == SQLITE_OK &&
136 sqlite3_bind_int(stmt, 1, id) == SQLITE_OK)
137 {
138 while (sqlite3_step(stmt) == SQLITE_ROW)
139 {
140 child_cfg = child_cfg_create(
141 (char*)sqlite3_column_text(stmt, 1), /* name */
142 sqlite3_column_int(stmt, 5), /* lifetime */
143 sqlite3_column_int(stmt, 6), /* rekeytime */
144 sqlite3_column_int(stmt, 7), /* jitter */
145 (char*)sqlite3_column_text(stmt, 2), /* updown */
146 sqlite3_column_int(stmt, 3), /* hostaccess */
147 sqlite3_column_int(stmt, 4)); /* mode */
148 add_ts(this, child_cfg, sqlite3_column_int(stmt, 0));
149 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
150 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
151 }
152 }
153 sqlite3_finalize(stmt);
154 }
155
156 /**
157 * processing function for get_peer_cfg and get_peer_cfg_by_name
158 */
159 static peer_cfg_t *process_peer_cfg_row(private_sqlite_backend_t *this,
160 sqlite3_stmt *stmt)
161 {
162 host_t *local_host, *remote_host, *local_vip = NULL, *remote_vip = NULL;
163 identification_t *local_id, *remote_id;
164 peer_cfg_t *peer_cfg;
165 ike_cfg_t *ike_cfg;
166
167 local_host = host_create_from_string((char*)sqlite3_column_text(stmt, 17), IKEV2_UDP_PORT);
168 remote_host = host_create_from_string((char*)sqlite3_column_text(stmt, 18), IKEV2_UDP_PORT);
169 if (sqlite3_column_text(stmt, 15))
170 {
171 local_vip = host_create_from_string((char*)sqlite3_column_text(stmt, 15), 0);
172 }
173 if (sqlite3_column_text(stmt, 16))
174 {
175 remote_vip = host_create_from_string((char*)sqlite3_column_text(stmt, 16), 0);
176 }
177 local_id = identification_create_from_string((char*)sqlite3_column_text(stmt, 2));
178 remote_id = identification_create_from_string((char*)sqlite3_column_text(stmt, 3));
179 if (local_host && remote_host && local_id && remote_id)
180 {
181 ike_cfg = ike_cfg_create(sqlite3_column_int(stmt, 19), FALSE,
182 local_host, remote_host);
183 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
184 peer_cfg = peer_cfg_create(
185 (char*)sqlite3_column_text(stmt, 1), /* name */
186 2, ike_cfg, local_id, remote_id, NULL, NULL, linked_list_create(),
187 sqlite3_column_int(stmt, 4), /* cert_policy */
188 sqlite3_column_int(stmt, 5), /* auth_method */
189 sqlite3_column_int(stmt, 6), /* eap_type */
190 sqlite3_column_int(stmt, 7), /* keyingtries */
191 sqlite3_column_int(stmt, 8), /* lifetime */
192 sqlite3_column_int(stmt, 9), /* rekeytime */
193 sqlite3_column_int(stmt, 10), /* jitter */
194 sqlite3_column_int(stmt, 13), /* reauth */
195 sqlite3_column_int(stmt, 14), /* mobike */
196 sqlite3_column_int(stmt, 11), /* dpd_delay */
197 sqlite3_column_int(stmt, 12), /* dpd_action */
198 local_vip, remote_vip, FALSE, NULL, NULL);
199 add_children(this, peer_cfg, sqlite3_column_int(stmt, 0));
200 return peer_cfg;
201 }
202
203 DESTROY_IF(local_host);
204 DESTROY_IF(remote_host);
205 DESTROY_IF(local_id);
206 DESTROY_IF(remote_id);
207 DESTROY_IF(local_vip);
208 DESTROY_IF(remote_vip);
209 return NULL;
210 }
211
212 /**
213 * implements backend_t.get_peer_cfg.
214 */
215 static peer_cfg_t *get_peer_cfg(private_sqlite_backend_t *this,
216 identification_t *my_id, identification_t *other_id,
217 ca_info_t *other_ca_info)
218 {
219 sqlite3_stmt *stmt;
220 char local[256], remote[256];
221 peer_cfg_t *peer_cfg = NULL;
222
223 snprintf(local, sizeof(local), "%D", my_id);
224 snprintf(remote, sizeof(remote), "%D", other_id);
225
226 if (sqlite3_prepare_v2(this->db,
227 "SELECT peer_configs.oid, name, local_id, remote_id, cert_policy, "
228 "auth_method, eap_type, keyingtries, lifetime, rekeytime, jitter, "
229 "dpd_delay, dpd_action, reauth, mobike, local_vip, remote_vip, "
230 "local, remote, certreq "
231 "FROM peer_configs, ike_configs "
232 "ON peer_configs.ike_cfg = ike_configs.oid "
233 "WHERE local_id = ? and remote_id = ?;", -1, &stmt, NULL) == SQLITE_OK &&
234 sqlite3_bind_text(stmt, 1, local, -1, SQLITE_STATIC) == SQLITE_OK &&
235 sqlite3_bind_text(stmt, 2, remote, -1, SQLITE_STATIC) == SQLITE_OK &&
236 sqlite3_step(stmt) == SQLITE_ROW)
237 {
238 peer_cfg = process_peer_cfg_row(this, stmt);
239 }
240 sqlite3_finalize(stmt);
241 return peer_cfg;
242 }
243
244 /**
245 * implements backend_t.get_peer_cfg_by_name.
246 */
247 static peer_cfg_t *get_peer_cfg_by_name(private_sqlite_backend_t *this, char *name)
248 {
249 sqlite3_stmt *stmt;
250 peer_cfg_t *peer_cfg = NULL;
251
252 if (sqlite3_prepare_v2(this->db,
253 "SELECT peer_configs.oid, name, local_id, remote_id, cert_policy, "
254 "auth_method, eap_type, keyingtries, lifetime, rekeytime, jitter, "
255 "dpd_delay, dpd_action, reauth, mobike, local_vip, remote_vip, "
256 "local, remote, certreq "
257 "FROM peer_configs, ike_configs "
258 "ON peer_configs.ike_cfg = ike_configs.oid "
259 "WHERE name = ? ;", -1, &stmt, NULL) == SQLITE_OK &&
260 sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC) == SQLITE_OK &&
261 sqlite3_step(stmt) == SQLITE_ROW)
262 {
263 peer_cfg = process_peer_cfg_row(this, stmt);
264 }
265 sqlite3_finalize(stmt);
266 return peer_cfg;
267 }
268
269 /**
270 * Implementation of backend_t.is_writable.
271 */
272 static bool is_writeable(private_sqlite_backend_t *this)
273 {
274 return FALSE;
275 }
276
277 /**
278 * Implementation of backend_t.destroy.
279 */
280 static void destroy(private_sqlite_backend_t *this)
281 {
282 sqlite3_close(this->db);
283 free(this);
284 }
285
286 /**
287 * Described in header.
288 */
289 backend_t *backend_create(void)
290 {
291 private_sqlite_backend_t *this = malloc_thing(private_sqlite_backend_t);
292
293 this->public.backend.get_ike_cfg = (ike_cfg_t* (*)(backend_t*, host_t*, host_t*))get_ike_cfg;
294 this->public.backend.get_peer_cfg = (peer_cfg_t* (*)(backend_t*,identification_t*,identification_t*,ca_info_t*))get_peer_cfg;
295 this->public.backend.get_peer_cfg_by_name = (peer_cfg_t* (*)(backend_t*,char*))get_peer_cfg_by_name;
296 this->public.backend.is_writeable = (bool(*) (backend_t*))is_writeable;
297 this->public.backend.destroy = (void (*)(backend_t*))destroy;
298
299 if (sqlite3_open(IPSEC_DIR "/manager.db", &this->db) != SQLITE_OK)
300 {
301 DBG1(DBG_CFG, "opening SQLite database '" IPSEC_DIR "/manager.db' failed.");
302 destroy(this);
303 return NULL;
304 }
305
306 return &this->public.backend;
307 }
308