2 * Copyright (C) 2009-2010 Andreas Steffen
3 * Hochschule fuer Technik Rapperswil
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>.
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
20 #include <utils/host.h>
22 #include "pool_attributes.h"
23 #include "pool_usage.h"
26 * global database handle
28 extern database_t
*db
;
30 #define UNITY_NETWORK_LEN 14
32 ENUM(value_type_names
, VALUE_HEX
, VALUE_SUBNET
,
39 typedef struct attr_info_t attr_info_t
;
43 value_type_t value_type
;
44 configuration_attribute_type_t type
;
45 configuration_attribute_type_t type_ip6
;
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
,
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
,
57 { "wins", VALUE_ADDR
, INTERNAL_IP4_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
,
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 },
77 * Determine the type of the attribute and its value
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
,
84 host_t
*addr
= NULL
, *mask
= NULL
;
85 chunk_t addr_chunk
, mask_chunk
;
86 char *text
= "", *pos
, *endptr
;
92 *blob
= chunk_create(value
, strlen(value
));
93 *blob
= chunk_clone(*blob
);
96 *blob
= chunk_from_hex(chunk_create(value
, strlen(value
)), NULL
);
99 addr
= host_create_from_string(value
, 0);
102 fprintf(stderr
, "invalid IP address: '%s'.\n", value
);
105 addr_chunk
= addr
->get_address(addr
);
106 *blob
= chunk_clone(addr_chunk
);
109 pos
= strchr(value
, '/');
110 if (pos
== NULL
|| (value
- pos
) == strlen(value
))
112 fprintf(stderr
, "invalid IPv4 subnet: '%s'.\n", value
);
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
)
121 fprintf(stderr
, "invalid IPv4 subnet: '%s'.\n", value
);
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);
140 /* init the attribute type */
144 for (i
= 0; i
< countof(attr_info
); i
++)
146 if (strcaseeq(name
, attr_info
[i
].keyword
))
148 if (*value_type
== VALUE_NONE
)
150 *value_type
= attr_info
[i
].value_type
;
151 *type
= attr_info
[i
].type
;
152 *type_ip6
= attr_info
[i
].type_ip6
;
155 if (*value_type
!= attr_info
[i
].value_type
&&
156 *value_type
!= VALUE_HEX
)
158 switch (attr_info
[i
].value_type
)
167 text
= "an IP address";
176 fprintf(stderr
, "the %s attribute requires %s value.\n",
182 if (*value_type
== VALUE_ADDR
)
184 *type
= (addr
->get_family(addr
) == AF_INET
) ?
185 attr_info
[i
].type
: attr_info
[i
].type_ip6
;
188 if (*value_type
== VALUE_HEX
)
190 *value_type
= attr_info
[i
].value_type
;
192 if (*value_type
== VALUE_ADDR
)
196 *type
= attr_info
[i
].type
;
198 else if (blob
->len
== 16)
200 *type
= attr_info
[i
].type_ip6
;
204 fprintf(stderr
, "the %s attribute requires "
205 "a valid IP address.\n", name
);
212 *type
= attr_info
[i
].type
;
217 *type
= attr_info
[i
].type
;
226 /* is the attribute type numeric? */
227 *type
= strtol(name
, &endptr
, 10);
231 fprintf(stderr
, "the %s attribute is not recognized.\n", name
);
235 if (*value_type
== VALUE_NONE
)
237 *value_type
= VALUE_HEX
;
243 * ipsec pool --addattr <type> --string|server|subnet - add attribute entry
245 void add_attr(char *name
, char *value
, value_type_t value_type
)
247 configuration_attribute_type_t type
, type_ip6
;
251 if (value_type
== VALUE_NONE
)
253 fprintf(stderr
, "the value of the %s attribute is missing.\n", name
);
257 if (!parse_attributes(name
, value
, &value_type
, &type
, &type_ip6
, &blob
))
261 success
= db
->execute(db
, NULL
,
262 "INSERT INTO attributes (type, value) VALUES (?, ?)",
263 DB_INT
, type
, DB_BLOB
, blob
) == 1;
268 printf("added %s attribute (%N).\n", name
,
269 configuration_attribute_type_names
, type
);
273 fprintf(stderr
, "adding %s attribute (%N) failed.\n", name
,
274 configuration_attribute_type_names
, type
);
280 * ipsec pool --delattr <type> --string|server|subnet - delete attribute entry
282 void del_attr(char *name
, char *value
, value_type_t value_type
)
284 configuration_attribute_type_t type
, type_ip6
, type_db
;
285 chunk_t blob
, blob_db
;
290 if (!parse_attributes(name
, value
, &value_type
, &type
, &type_ip6
, &blob
))
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
);
302 else if (type_ip6
== 0)
304 query
= db
->query(db
,
305 "SELECT id, type, value FROM attributes "
308 DB_UINT
, DB_INT
, DB_BLOB
);
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
);
321 fprintf(stderr
, "deleting '%s' attribute (%N) failed.\n",
322 name
, configuration_attribute_type_names
, type
);
327 while (query
->enumerate(query
, &id
, &type_db
, &blob_db
))
329 host_t
*server
= NULL
;
333 if (value_type
== VALUE_ADDR
)
335 int family
= (type_db
== type_ip6
) ? AF_INET6
: AF_INET
;
337 server
= host_create_from_chunk(family
, blob_db
, 0);
340 if (db
->execute(db
, NULL
,
341 "DELETE FROM attributes WHERE id = ?",
346 fprintf(stderr
, "deleting %s server %H failed\n", name
, server
);
347 server
->destroy(server
);
349 else if (value_type
== VALUE_STRING
)
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
);
358 fprintf(stderr
, "deleting %s attribute (%N) with value %#B failed.\n",
359 name
, configuration_attribute_type_names
, type
,
362 query
->destroy(query
);
368 printf("deleted %s server %H\n", name
, server
);
369 server
->destroy(server
);
371 else if (value_type
== VALUE_STRING
)
373 printf("deleted %s attribute (%N) with value '%.*s'.\n",
374 name
, configuration_attribute_type_names
, type
,
375 blob_db
.len
, blob_db
.ptr
);
379 printf("deleted %s attribute (%N) with value %#B.\n",
380 name
, configuration_attribute_type_names
, type
,
384 query
->destroy(query
);
392 fprintf(stderr
, "no %s attribute (%N) was found.\n", name
,
393 configuration_attribute_type_names
, type
);
397 fprintf(stderr
, "no %s attribute was found.\n", name
);
402 if (value_type
== VALUE_ADDR
)
404 host_t
*server
= host_create_from_chunk(AF_UNSPEC
, blob
, 0);
406 fprintf(stderr
, "the %s server %H was not found.\n", name
,
408 server
->destroy(server
);
412 fprintf(stderr
, "the %s attribute (%N) with value '%*.s' "
413 "was not found.\n", name
,
414 configuration_attribute_type_names
, type
,
425 * ipsec pool --statusattr - show all attribute entries
427 void status_attr(void)
429 configuration_attribute_type_t type
;
431 enumerator_t
*enumerator
;
435 /* enumerate over all attributes */
436 enumerator
= db
->query(db
, "SELECT type, value FROM attributes ORDER BY type",
440 while (enumerator
->enumerate(enumerator
, &type
, &value
))
444 printf(" type description value\n");
447 snprintf(type_name
, sizeof(type_name
), "%N",
448 configuration_attribute_type_names
, type
);
449 if (type_name
[0] == '(')
453 printf("%5d %-20s %#B\n",type
, type_name
, &value
);
455 enumerator
->destroy(enumerator
);
460 * ipsec pool --showattr - show all supported attribute keywords
466 for (i
= 0; i
< countof(attr_info
); i
++)
471 snprintf(value_name
, sizeof(value_name
), "%N",
472 value_type_names
, attr_info
[i
].value_type
);
474 printf("%-19s --%-6s (%N",
475 attr_info
[i
].keyword
, value_name
,
476 configuration_attribute_type_names
, attr_info
[i
].type
);
478 if (attr_info
[i
].type_ip6
)
481 configuration_attribute_type_names
, attr_info
[i
].type_ip6
);