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_netmask", VALUE_ADDR
, INTERNAL_IP4_NETMASK
, 0 },
50 { "internal_ip6_netmask", VALUE_ADDR
, INTERNAL_IP6_NETMASK
, 0 },
51 { "netmask", VALUE_ADDR
, INTERNAL_IP4_NETMASK
,
52 INTERNAL_IP6_NETMASK
},
53 { "internal_ip4_dns", VALUE_ADDR
, INTERNAL_IP4_DNS
, 0 },
54 { "internal_ip6_dns", VALUE_ADDR
, INTERNAL_IP6_DNS
, 0 },
55 { "dns", VALUE_ADDR
, INTERNAL_IP4_DNS
,
57 { "internal_ip4_nbns", VALUE_ADDR
, INTERNAL_IP4_NBNS
, 0 },
58 { "internal_ip6_nbns", VALUE_ADDR
, INTERNAL_IP6_NBNS
, 0 },
59 { "nbns", VALUE_ADDR
, INTERNAL_IP4_NBNS
,
61 { "wins", VALUE_ADDR
, INTERNAL_IP4_NBNS
,
63 { "internal_ip4_dhcp", VALUE_ADDR
, INTERNAL_IP4_DHCP
, 0 },
64 { "internal_ip6_dhcp", VALUE_ADDR
, INTERNAL_IP6_DHCP
, 0 },
65 { "dhcp", VALUE_ADDR
, INTERNAL_IP4_DHCP
,
67 { "internal_ip4_server", VALUE_ADDR
, INTERNAL_IP4_SERVER
, 0 },
68 { "internal_ip6_server", VALUE_ADDR
, INTERNAL_IP6_SERVER
, 0 },
69 { "server", VALUE_ADDR
, INTERNAL_IP4_SERVER
,
70 INTERNAL_IP6_SERVER
},
71 { "application_version", VALUE_STRING
, APPLICATION_VERSION
, 0 },
72 { "version", VALUE_STRING
, APPLICATION_VERSION
, 0 },
73 { "unity_banner", VALUE_STRING
, UNITY_BANNER
, 0 },
74 { "banner", VALUE_STRING
, UNITY_BANNER
, 0 },
75 { "unity_def_domain", VALUE_STRING
, UNITY_DEF_DOMAIN
, 0 },
76 { "unity_splitdns_name", VALUE_STRING
, UNITY_SPLITDNS_NAME
, 0 },
77 { "unity_split_include", VALUE_SUBNET
, UNITY_SPLIT_INCLUDE
, 0 },
78 { "unity_local_lan", VALUE_SUBNET
, UNITY_LOCAL_LAN
, 0 },
82 * Determine the type of the attribute and its value
84 static bool parse_attributes(char *name
, char *value
, value_type_t
*value_type
,
85 configuration_attribute_type_t
*type
,
86 configuration_attribute_type_t
*type_ip6
,
89 host_t
*addr
= NULL
, *mask
= NULL
;
90 chunk_t addr_chunk
, mask_chunk
, blob_next
;
91 char *text
= "", *pos_addr
, *pos_mask
, *pos_next
, *endptr
;
97 *blob
= chunk_create(value
, strlen(value
));
98 *blob
= chunk_clone(*blob
);
101 *blob
= chunk_from_hex(chunk_create(value
, strlen(value
)), NULL
);
104 addr
= host_create_from_string(value
, 0);
107 fprintf(stderr
, "invalid IP address: '%s'.\n", value
);
110 addr_chunk
= addr
->get_address(addr
);
111 *blob
= chunk_clone(addr_chunk
);
120 pos_next
= strchr(pos_next
, ',');
126 pos_mask
= strchr(pos_addr
, '/');
127 if (pos_mask
== NULL
)
129 fprintf(stderr
, "invalid IPv4 subnet: '%s'.\n", pos_addr
);
135 addr
= host_create_from_string(pos_addr
, 0);
136 mask
= host_create_from_string(pos_mask
, 0);
137 if (addr
== NULL
|| addr
->get_family(addr
) != AF_INET
||
138 mask
== NULL
|| mask
->get_family(addr
) != AF_INET
)
140 fprintf(stderr
, "invalid IPv4 subnet: '%s/%s'.\n",
147 addr_chunk
= addr
->get_address(addr
);
148 mask_chunk
= mask
->get_address(mask
);
149 blob_next
= chunk_alloc(blob
->len
+ UNITY_NETWORK_LEN
);
150 memcpy(blob_next
.ptr
, blob
->ptr
, blob
->len
);
151 pos_addr
= blob_next
.ptr
+ blob
->len
;
152 memset(pos_addr
, 0x00, UNITY_NETWORK_LEN
);
153 memcpy(pos_addr
, addr_chunk
.ptr
, 4);
154 memcpy(pos_addr
+ 4, mask_chunk
.ptr
, 4);
167 /* init the attribute type */
171 for (i
= 0; i
< countof(attr_info
); i
++)
173 if (strcaseeq(name
, attr_info
[i
].keyword
))
175 *type
= attr_info
[i
].type
;
176 *type_ip6
= attr_info
[i
].type_ip6
;
178 if (*value_type
== VALUE_NONE
)
180 *value_type
= attr_info
[i
].value_type
;
184 if (*value_type
!= attr_info
[i
].value_type
&&
185 *value_type
!= VALUE_HEX
)
187 switch (attr_info
[i
].value_type
)
196 text
= "an IP address";
205 fprintf(stderr
, "the %s attribute requires %s value.\n",
212 if (*value_type
== VALUE_ADDR
)
214 *type
= (addr
->get_family(addr
) == AF_INET
) ?
215 attr_info
[i
].type
: attr_info
[i
].type_ip6
;
218 else if (*value_type
== VALUE_HEX
)
220 *value_type
= attr_info
[i
].value_type
;
222 if (*value_type
== VALUE_ADDR
)
226 *type
= attr_info
[i
].type_ip6
;
228 else if (blob
->len
!= 4)
230 fprintf(stderr
, "the %s attribute requires "
231 "a valid IP address.\n", name
);
244 /* is the attribute type numeric? */
245 *type
= strtol(name
, &endptr
, 10);
249 fprintf(stderr
, "the %s attribute is not recognized.\n", name
);
253 if (*type
< 1 || *type
> 32767)
255 fprintf(stderr
, "the attribute type must lie in the range 1..32767.\n");
259 if (*value_type
== VALUE_NONE
)
261 *value_type
= VALUE_HEX
;
267 * ipsec pool --addattr <type> --string|server|subnet - add attribute entry
269 void add_attr(char *name
, char *value
, value_type_t value_type
)
271 configuration_attribute_type_t type
, type_ip6
;
275 if (value_type
== VALUE_NONE
)
277 fprintf(stderr
, "the value of the %s attribute is missing.\n", name
);
281 if (!parse_attributes(name
, value
, &value_type
, &type
, &type_ip6
, &blob
))
286 success
= db
->execute(db
, NULL
,
287 "INSERT INTO attributes (type, value) VALUES (?, ?)",
288 DB_INT
, type
, DB_BLOB
, blob
) == 1;
293 printf("added %s attribute (%N).\n", name
,
294 configuration_attribute_type_names
, type
);
298 fprintf(stderr
, "adding %s attribute (%N) failed.\n", name
,
299 configuration_attribute_type_names
, type
);
305 * ipsec pool --delattr <type> --string|server|subnet - delete attribute entry
307 void del_attr(char *name
, char *value
, value_type_t value_type
)
309 configuration_attribute_type_t type
, type_ip6
, type_db
;
310 chunk_t blob
, blob_db
;
315 if (!parse_attributes(name
, value
, &value_type
, &type
, &type_ip6
, &blob
))
322 query
= db
->query(db
,
323 "SELECT id, type, value FROM attributes "
324 "WHERE type = ? AND value = ?",
325 DB_INT
, type
, DB_BLOB
, blob
,
326 DB_UINT
, DB_INT
, DB_BLOB
);
328 else if (type_ip6
== 0)
330 query
= db
->query(db
,
331 "SELECT id, type, value FROM attributes "
334 DB_UINT
, DB_INT
, DB_BLOB
);
338 query
= db
->query(db
,
339 "SELECT id, type, value FROM attributes "
340 "WHERE type = ? OR type = ?",
341 DB_INT
, type
, DB_INT
, type_ip6
,
342 DB_UINT
, DB_INT
, DB_BLOB
);
347 fprintf(stderr
, "deleting '%s' attribute (%N) failed.\n",
348 name
, configuration_attribute_type_names
, type
);
353 while (query
->enumerate(query
, &id
, &type_db
, &blob_db
))
355 host_t
*server
= NULL
;
359 if (value_type
== VALUE_ADDR
)
361 int family
= (type_db
== type_ip6
) ? AF_INET6
: AF_INET
;
363 server
= host_create_from_chunk(family
, blob_db
, 0);
366 if (db
->execute(db
, NULL
,
367 "DELETE FROM attributes WHERE id = ?",
372 fprintf(stderr
, "deleting %s server %H failed\n", name
, server
);
373 server
->destroy(server
);
375 else if (value_type
== VALUE_STRING
)
377 fprintf(stderr
, "deleting %s attribute (%N) with value '%.*s' failed.\n",
378 name
, configuration_attribute_type_names
, type
,
379 blob_db
.len
, blob_db
.ptr
);
384 fprintf(stderr
, "deleting %s attribute (%N) with value %#B failed.\n",
385 name
, configuration_attribute_type_names
, type
,
388 query
->destroy(query
);
394 printf("deleted %s server %H\n", name
, server
);
395 server
->destroy(server
);
397 else if (value_type
== VALUE_STRING
)
399 printf("deleted %s attribute (%N) with value '%.*s'.\n",
400 name
, configuration_attribute_type_names
, type
,
401 blob_db
.len
, blob_db
.ptr
);
405 printf("deleted %s attribute (%N) with value %#B.\n",
406 name
, configuration_attribute_type_names
, type
,
410 query
->destroy(query
);
418 fprintf(stderr
, "no %s attribute (%N) was found.\n", name
,
419 configuration_attribute_type_names
, type
);
423 fprintf(stderr
, "no %s attribute was found.\n", name
);
428 if (value_type
== VALUE_ADDR
)
430 host_t
*server
= host_create_from_chunk(AF_UNSPEC
, blob
, 0);
432 fprintf(stderr
, "the %s server %H was not found.\n", name
,
434 server
->destroy(server
);
438 fprintf(stderr
, "the %s attribute (%N) with value '%.*s' "
439 "was not found.\n", name
,
440 configuration_attribute_type_names
, type
,
449 * ipsec pool --statusattr - show all attribute entries
451 void status_attr(bool hexout
)
453 configuration_attribute_type_t type
;
454 value_type_t value_type
;
455 chunk_t value
, addr_chunk
, mask_chunk
;
456 enumerator_t
*enumerator
;
462 /* enumerate over all attributes */
463 enumerator
= db
->query(db
, "SELECT type, value FROM attributes ORDER BY type",
467 while (enumerator
->enumerate(enumerator
, &type
, &value
))
471 printf(" type description value\n");
474 snprintf(type_name
, sizeof(type_name
), "%N",
475 configuration_attribute_type_names
, type
);
476 if (type_name
[0] == '(')
480 printf("%5d %-20s ",type
, type_name
);
482 value_type
= VALUE_HEX
;
485 for (i
= 0; i
< countof(attr_info
); i
++)
487 if (type
== attr_info
[i
].type
)
489 value_type
= attr_info
[i
].value_type
;
497 addr
= host_create_from_chunk(AF_UNSPEC
, value
, 0);
500 printf(" %H\n", addr
);
505 /* value cannot be represented as an IP address */
506 printf(" %#B\n", &value
);
510 if (value
.len
% UNITY_NETWORK_LEN
== 0)
512 for (i
= 0; i
< value
.len
/ UNITY_NETWORK_LEN
; i
++)
514 addr_chunk
= chunk_create(value
.ptr
+ i
*UNITY_NETWORK_LEN
, 4);
515 addr
= host_create_from_chunk(AF_INET
, addr_chunk
, 0);
516 mask_chunk
= chunk_create(addr_chunk
.ptr
+ 4, 4);
517 mask
= host_create_from_chunk(AF_INET
, mask_chunk
, 0);
518 printf("%s%H/%H", (i
> 0) ?
"," : " ", addr
, mask
);
526 /* value cannot be represented as a list of subnets */
527 printf(" %#B\n", &value
);
531 printf("\"%.*s\"\n", value
.len
, value
.ptr
);
535 printf(" %#B\n", &value
);
538 enumerator
->destroy(enumerator
);
543 * ipsec pool --showattr - show all supported attribute keywords
549 for (i
= 0; i
< countof(attr_info
); i
++)
554 snprintf(value_name
, sizeof(value_name
), "%N",
555 value_type_names
, attr_info
[i
].value_type
);
557 printf("%-20s --%-6s (%N",
558 attr_info
[i
].keyword
, value_name
,
559 configuration_attribute_type_names
, attr_info
[i
].type
);
561 if (attr_info
[i
].type_ip6
)
564 configuration_attribute_type_names
, attr_info
[i
].type_ip6
);