52314aa89d17bfdad91429d97d3f6d37cac98668
[strongswan.git] / src / libhydra / plugins / attr_sql / pool_attributes.c
1 /*
2 * Copyright (C) 2009-2010 Andreas Steffen
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
16 #define _GNU_SOURCE
17 #include <string.h>
18
19 #include <library.h>
20 #include <utils/host.h>
21
22 #include "pool_attributes.h"
23 #include "pool_usage.h"
24
25 /**
26 * global database handle
27 */
28 extern database_t *db;
29
30 #define UNITY_NETWORK_LEN 14
31
32 ENUM(value_type_names, VALUE_HEX, VALUE_SUBNET,
33 "hex",
34 "string",
35 "server",
36 "subnet"
37 );
38
39 typedef struct attr_info_t attr_info_t;
40
41 struct attr_info_t {
42 char* keyword;
43 value_type_t value_type;
44 configuration_attribute_type_t type;
45 configuration_attribute_type_t type_ip6;
46 };
47
48 static const attr_info_t attr_info[] = {
49 { "internal_ip4_dns", VALUE_ADDR, INTERNAL_IP4_DNS, 0 },
50 { "internal_ip6_dns", VALUE_ADDR, INTERNAL_IP6_DNS, 0 },
51 { "dns", VALUE_ADDR, INTERNAL_IP4_DNS,
52 INTERNAL_IP6_DNS },
53 { "internal_ip4_nbns", VALUE_ADDR, INTERNAL_IP4_NBNS, 0 },
54 { "internal_ip6_nbns", VALUE_ADDR, INTERNAL_IP6_NBNS, 0 },
55 { "nbns", VALUE_ADDR, INTERNAL_IP4_NBNS,
56 INTERNAL_IP6_NBNS },
57 { "wins", VALUE_ADDR, INTERNAL_IP4_NBNS,
58 INTERNAL_IP6_NBNS },
59 { "internal_ip4_dhcp", VALUE_ADDR, INTERNAL_IP4_DHCP, 0 },
60 { "internal_ip6_dhcp", VALUE_ADDR, INTERNAL_IP6_DHCP, 0 },
61 { "dhcp", VALUE_ADDR, INTERNAL_IP4_DHCP,
62 INTERNAL_IP6_DHCP },
63 { "internal_ip4_server", VALUE_ADDR, INTERNAL_IP4_SERVER, 0 },
64 { "internal_ip6_server", VALUE_ADDR, INTERNAL_IP6_SERVER, 0 },
65 { "server", VALUE_ADDR, INTERNAL_IP4_SERVER,
66 INTERNAL_IP6_SERVER },
67 { "application_version", VALUE_STRING, APPLICATION_VERSION, 0 },
68 { "version", VALUE_STRING, APPLICATION_VERSION, 0 },
69 { "unity_banner", VALUE_STRING, UNITY_BANNER, 0 },
70 { "banner", VALUE_STRING, UNITY_BANNER, 0 },
71 { "unity_splitdns_name", VALUE_STRING, UNITY_SPLITDNS_NAME, 0 },
72 { "unity_split_include", VALUE_SUBNET, UNITY_SPLIT_INCLUDE, 0 },
73 { "unity_local_lan", VALUE_SUBNET, UNITY_LOCAL_LAN, 0 },
74 };
75
76 /**
77 * Determine the type of the attribute and its value
78 */
79 static bool parse_attributes(char *name, char *value, value_type_t *value_type,
80 configuration_attribute_type_t *type,
81 configuration_attribute_type_t *type_ip6,
82 chunk_t *blob)
83 {
84 host_t *addr = NULL, *mask = NULL;
85 chunk_t addr_chunk, mask_chunk;
86 char *text = "", *pos, *endptr;
87 int i;
88
89 switch (*value_type)
90 {
91 case VALUE_STRING:
92 *blob = chunk_create(value, strlen(value));
93 *blob = chunk_clone(*blob);
94 break;
95 case VALUE_HEX:
96 *blob = chunk_from_hex(chunk_create(value, strlen(value)), NULL);
97 break;
98 case VALUE_ADDR:
99 addr = host_create_from_string(value, 0);
100 if (addr == NULL)
101 {
102 fprintf(stderr, "invalid IP address: '%s'.\n", value);
103 return FALSE;
104 }
105 addr_chunk = addr->get_address(addr);
106 *blob = chunk_clone(addr_chunk);
107 break;
108 case VALUE_SUBNET:
109 pos = strchr(value, '/');
110 if (pos == NULL || (value - pos) == strlen(value))
111 {
112 fprintf(stderr, "invalid IPv4 subnet: '%s'.\n", value);
113 return FALSE;
114 }
115 *pos = '\0';
116 addr = host_create_from_string(value, 0);
117 mask = host_create_from_string(pos+1, 0);
118 if (addr == NULL || addr->get_family(addr) != AF_INET ||
119 mask == NULL || mask->get_family(addr) != AF_INET)
120 {
121 fprintf(stderr, "invalid IPv4 subnet: '%s'.\n", value);
122 DESTROY_IF(addr);
123 DESTROY_IF(mask);
124 return FALSE;
125 }
126 addr_chunk = addr->get_address(addr);
127 mask_chunk = mask->get_address(mask);
128 *blob = chunk_alloc(UNITY_NETWORK_LEN);
129 memset(blob->ptr, 0x00, UNITY_NETWORK_LEN);
130 memcpy(blob->ptr, addr_chunk.ptr, 4);
131 memcpy(blob->ptr + 4, mask_chunk.ptr, 4);
132 addr->destroy(addr);
133 mask->destroy(mask);
134 break;
135 case VALUE_NONE:
136 *blob = chunk_empty;
137 break;
138 }
139
140 /* init the attribute type */
141 *type = 0;
142 *type_ip6 = 0;
143
144 for (i = 0; i < countof(attr_info); i++)
145 {
146 if (strcaseeq(name, attr_info[i].keyword))
147 {
148 if (*value_type == VALUE_NONE)
149 {
150 *value_type = attr_info[i].value_type;
151 *type = attr_info[i].type;
152 *type_ip6 = attr_info[i].type_ip6;
153 return TRUE;
154 }
155 if (*value_type != attr_info[i].value_type &&
156 *value_type != VALUE_HEX)
157 {
158 switch (attr_info[i].value_type)
159 {
160 case VALUE_STRING:
161 text = "a string";
162 break;
163 case VALUE_HEX:
164 text = "a hex";
165 break;
166 case VALUE_ADDR:
167 text = "an IP address";
168 break;
169 case VALUE_SUBNET:
170 text = "a subnet";
171 break;
172 case VALUE_NONE:
173 text = "no";
174 break;
175 }
176 fprintf(stderr, "the %s attribute requires %s value.\n",
177 name, text);
178 DESTROY_IF(addr);
179 free(blob->ptr);
180 return FALSE;
181 }
182 if (*value_type == VALUE_ADDR)
183 {
184 *type = (addr->get_family(addr) == AF_INET) ?
185 attr_info[i].type : attr_info[i].type_ip6;
186 addr->destroy(addr);
187 }
188 if (*value_type == VALUE_HEX)
189 {
190 *value_type = attr_info[i].value_type;
191
192 if (*value_type == VALUE_ADDR)
193 {
194 if (blob->len == 4)
195 {
196 *type = attr_info[i].type;
197 }
198 else if (blob->len == 16)
199 {
200 *type = attr_info[i].type_ip6;
201 }
202 else
203 {
204 fprintf(stderr, "the %s attribute requires "
205 "a valid IP address.\n", name);
206 free(blob->ptr);
207 return FALSE;
208 }
209 }
210 else
211 {
212 *type = attr_info[i].type;
213 }
214 }
215 else
216 {
217 *type = attr_info[i].type;
218 }
219 return TRUE;
220 }
221 }
222
223 /* clean up */
224 DESTROY_IF(addr);
225
226 /* is the attribute type numeric? */
227 *type = strtol(name, &endptr, 10);
228
229 if (*endptr != '\0')
230 {
231 fprintf(stderr, "the %s attribute is not recognized.\n", name);
232 free(blob->ptr);
233 return FALSE;
234 }
235 if (*value_type == VALUE_NONE)
236 {
237 *value_type = VALUE_HEX;
238 }
239 return TRUE;
240 }
241
242 /**
243 * ipsec pool --addattr <type> --string|server|subnet - add attribute entry
244 */
245 void add_attr(char *name, char *value, value_type_t value_type)
246 {
247 configuration_attribute_type_t type, type_ip6;
248 chunk_t blob;
249 bool success;
250
251 if (value_type == VALUE_NONE)
252 {
253 fprintf(stderr, "the value of the %s attribute is missing.\n", name);
254 usage();
255 exit(EXIT_FAILURE);
256 }
257 if (!parse_attributes(name, value, &value_type, &type, &type_ip6, &blob))
258 {
259 exit(EXIT_FAILURE);
260 }
261 success = db->execute(db, NULL,
262 "INSERT INTO attributes (type, value) VALUES (?, ?)",
263 DB_INT, type, DB_BLOB, blob) == 1;
264 free(blob.ptr);
265
266 if (success)
267 {
268 printf("added %s attribute (%N).\n", name,
269 configuration_attribute_type_names, type);
270 }
271 else
272 {
273 fprintf(stderr, "adding %s attribute (%N) failed.\n", name,
274 configuration_attribute_type_names, type);
275 exit(EXIT_FAILURE);
276 }
277 }
278
279 /**
280 * ipsec pool --delattr <type> --string|server|subnet - delete attribute entry
281 */
282 void del_attr(char *name, char *value, value_type_t value_type)
283 {
284 configuration_attribute_type_t type, type_ip6, type_db;
285 chunk_t blob, blob_db;
286 u_int id;
287 enumerator_t *query;
288 bool found = FALSE;
289
290 if (!parse_attributes(name, value, &value_type, &type, &type_ip6, &blob))
291 {
292 exit(EXIT_FAILURE);
293 }
294 if (blob.len > 0)
295 {
296 query = db->query(db,
297 "SELECT id, type, value FROM attributes "
298 "WHERE type = ? AND value = ?",
299 DB_INT, type, DB_BLOB, blob,
300 DB_UINT, DB_INT, DB_BLOB);
301 }
302 else if (type_ip6 == 0)
303 {
304 query = db->query(db,
305 "SELECT id, type, value FROM attributes "
306 "WHERE type = ?",
307 DB_INT, type,
308 DB_UINT, DB_INT, DB_BLOB);
309 }
310 else
311 {
312 query = db->query(db,
313 "SELECT id, type, value FROM attributes "
314 "WHERE type = ? OR type = ?",
315 DB_INT, type, DB_INT, type_ip6,
316 DB_UINT, DB_INT, DB_BLOB);
317 }
318
319 if (!query)
320 {
321 fprintf(stderr, "deleting '%s' attribute (%N) failed.\n",
322 name, configuration_attribute_type_names, type);
323 free(blob.ptr);
324 exit(EXIT_FAILURE);
325 }
326
327 while (query->enumerate(query, &id, &type_db, &blob_db))
328 {
329 host_t *server = NULL;
330
331 found = TRUE;
332
333 if (value_type == VALUE_ADDR)
334 {
335 int family = (type_db == type_ip6) ? AF_INET6 : AF_INET;
336
337 server = host_create_from_chunk(family, blob_db, 0);
338 }
339
340 if (db->execute(db, NULL,
341 "DELETE FROM attributes WHERE id = ?",
342 DB_UINT, id) != 1)
343 {
344 if (server)
345 {
346 fprintf(stderr, "deleting %s server %H failed\n", name, server);
347 server->destroy(server);
348 }
349 else if (value_type == VALUE_STRING)
350 {
351 fprintf(stderr, "deleting %s attribute (%N) with value '%.*s' failed.\n",
352 name, configuration_attribute_type_names, type,
353 blob_db.len, blob_db.ptr);
354 }
355
356 else
357 {
358 fprintf(stderr, "deleting %s attribute (%N) with value %#B failed.\n",
359 name, configuration_attribute_type_names, type,
360 &blob_db);
361 }
362 query->destroy(query);
363 free(blob.ptr);
364 exit(EXIT_FAILURE);
365 }
366 if (server)
367 {
368 printf("deleted %s server %H\n", name, server);
369 server->destroy(server);
370 }
371 else if (value_type == VALUE_STRING)
372 {
373 printf("deleted %s attribute (%N) with value '%.*s'.\n",
374 name, configuration_attribute_type_names, type,
375 blob_db.len, blob_db.ptr);
376 }
377 else
378 {
379 printf("deleted %s attribute (%N) with value %#B.\n",
380 name, configuration_attribute_type_names, type,
381 &blob_db);
382 }
383 }
384 query->destroy(query);
385
386 if (!found)
387 {
388 if (blob.len == 0)
389 {
390 if (type_ip6 == 0)
391 {
392 fprintf(stderr, "no %s attribute (%N) was found.\n", name,
393 configuration_attribute_type_names, type);
394 }
395 else
396 {
397 fprintf(stderr, "no %s attribute was found.\n", name);
398 }
399 }
400 else
401 {
402 if (value_type == VALUE_ADDR)
403 {
404 host_t *server = host_create_from_chunk(AF_UNSPEC, blob, 0);
405
406 fprintf(stderr, "the %s server %H was not found.\n", name,
407 server);
408 server->destroy(server);
409 }
410 else
411 {
412 fprintf(stderr, "the %s attribute (%N) with value '%*.s' "
413 "was not found.\n", name,
414 configuration_attribute_type_names, type,
415 blob.len, blob.ptr);
416 }
417 }
418 free(blob.ptr);
419 exit(EXIT_FAILURE);
420 }
421 free(blob.ptr);
422 }
423
424 /**
425 * ipsec pool --statusattr - show all attribute entries
426 */
427 void status_attr(void)
428 {
429 configuration_attribute_type_t type;
430 chunk_t value;
431 enumerator_t *enumerator;
432 char type_name[30];
433 bool first = TRUE;
434
435 /* enumerate over all attributes */
436 enumerator = db->query(db, "SELECT type, value FROM attributes ORDER BY type",
437 DB_INT, DB_BLOB);
438 if (enumerator)
439 {
440 while (enumerator->enumerate(enumerator, &type, &value))
441 {
442 if (first)
443 {
444 printf(" type description value\n");
445 first = FALSE;
446 }
447 snprintf(type_name, sizeof(type_name), "%N",
448 configuration_attribute_type_names, type);
449 if (type_name[0] == '(')
450 {
451 type_name[0] = '\0';
452 }
453 printf("%5d %-20s %#B\n",type, type_name, &value);
454 }
455 enumerator->destroy(enumerator);
456 }
457 }
458
459 /**
460 * ipsec pool --showattr - show all supported attribute keywords
461 */
462 void show_attr(void)
463 {
464 int i;
465
466 for (i = 0; i < countof(attr_info); i++)
467 {
468 char value_name[10];
469
470
471 snprintf(value_name, sizeof(value_name), "%N",
472 value_type_names, attr_info[i].value_type);
473
474 printf("%-19s --%-6s (%N",
475 attr_info[i].keyword, value_name,
476 configuration_attribute_type_names, attr_info[i].type);
477
478 if (attr_info[i].type_ip6)
479 {
480 printf(", %N)\n",
481 configuration_attribute_type_names, attr_info[i].type_ip6);
482 }
483 else
484 {
485 printf(")\n");
486 }
487 }
488 }
489