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 <networking/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_split_exclude", VALUE_SUBNET
, UNITY_LOCAL_LAN
, 0 },
79 { "unity_local_lan", VALUE_SUBNET
, UNITY_LOCAL_LAN
, 0 },
83 * Determine the type of the attribute and its value
85 static bool parse_attributes(char *name
, char *value
, value_type_t
*value_type
,
86 configuration_attribute_type_t
*type
,
87 configuration_attribute_type_t
*type_ip6
,
90 host_t
*addr
= NULL
, *mask
= NULL
;
91 chunk_t addr_chunk
, mask_chunk
, blob_next
;
92 char *text
= "", *pos_addr
, *pos_mask
, *pos_next
, *endptr
;
98 *blob
= chunk_create(value
, strlen(value
));
99 *blob
= chunk_clone(*blob
);
102 *blob
= chunk_from_hex(chunk_create(value
, strlen(value
)), NULL
);
105 addr
= host_create_from_string(value
, 0);
108 fprintf(stderr
, "invalid IP address: '%s'.\n", value
);
111 addr_chunk
= addr
->get_address(addr
);
112 *blob
= chunk_clone(addr_chunk
);
121 pos_next
= strchr(pos_next
, ',');
127 pos_mask
= strchr(pos_addr
, '/');
128 if (pos_mask
== NULL
)
130 fprintf(stderr
, "invalid IPv4 subnet: '%s'.\n", pos_addr
);
136 addr
= host_create_from_string(pos_addr
, 0);
137 mask
= host_create_from_string(pos_mask
, 0);
138 if (addr
== NULL
|| addr
->get_family(addr
) != AF_INET
||
139 mask
== NULL
|| mask
->get_family(addr
) != AF_INET
)
141 fprintf(stderr
, "invalid IPv4 subnet: '%s/%s'.\n",
148 addr_chunk
= addr
->get_address(addr
);
149 mask_chunk
= mask
->get_address(mask
);
150 blob_next
= chunk_alloc(blob
->len
+ UNITY_NETWORK_LEN
);
151 memcpy(blob_next
.ptr
, blob
->ptr
, blob
->len
);
152 pos_addr
= blob_next
.ptr
+ blob
->len
;
153 memset(pos_addr
, 0x00, UNITY_NETWORK_LEN
);
154 memcpy(pos_addr
, addr_chunk
.ptr
, 4);
155 memcpy(pos_addr
+ 4, mask_chunk
.ptr
, 4);
169 /* init the attribute type */
173 for (i
= 0; i
< countof(attr_info
); i
++)
175 if (strcaseeq(name
, attr_info
[i
].keyword
))
177 *type
= attr_info
[i
].type
;
178 *type_ip6
= attr_info
[i
].type_ip6
;
180 if (*value_type
== VALUE_NONE
)
182 *value_type
= attr_info
[i
].value_type
;
186 if (*value_type
!= attr_info
[i
].value_type
&&
187 *value_type
!= VALUE_HEX
)
189 switch (attr_info
[i
].value_type
)
198 text
= "an IP address";
207 fprintf(stderr
, "the %s attribute requires %s value.\n",
214 if (*value_type
== VALUE_ADDR
)
216 *type
= (addr
->get_family(addr
) == AF_INET
) ?
217 attr_info
[i
].type
: attr_info
[i
].type_ip6
;
220 else if (*value_type
== VALUE_HEX
)
222 *value_type
= attr_info
[i
].value_type
;
224 if (*value_type
== VALUE_ADDR
)
228 *type
= attr_info
[i
].type_ip6
;
230 else if (blob
->len
!= 4)
232 fprintf(stderr
, "the %s attribute requires "
233 "a valid IP address.\n", name
);
246 /* is the attribute type numeric? */
247 *type
= strtol(name
, &endptr
, 10);
251 fprintf(stderr
, "the %s attribute is not recognized.\n", name
);
255 if (*type
< 1 || *type
> 32767)
257 fprintf(stderr
, "the attribute type must lie in the range 1..32767.\n");
261 if (*value_type
== VALUE_NONE
)
263 *value_type
= VALUE_HEX
;
269 * Lookup/insert an attribute pool by name
271 static u_int
get_attr_pool(char *name
)
276 /* look for an existing attribute pool in the table */
277 e
= db
->query(db
, "SELECT id FROM attribute_pools WHERE name = ?",
278 DB_TEXT
, name
, DB_UINT
);
279 if (e
&& e
->enumerate(e
, &row
))
285 /* not found, insert new one */
286 if (db
->execute(db
, &row
, "INSERT INTO attribute_pools (name) VALUES (?)",
289 fprintf(stderr
, "creating attribute pool '%s' failed.\n", name
);
296 * Lookup/insert an identity
298 u_int
get_identity(identification_t
*id
)
303 /* look for peer identity in the identities table */
304 e
= db
->query(db
, "SELECT id FROM identities WHERE type = ? AND data = ?",
305 DB_INT
, id
->get_type(id
), DB_BLOB
, id
->get_encoding(id
), DB_UINT
);
306 if (e
&& e
->enumerate(e
, &row
))
312 /* not found, insert new one */
313 if (db
->execute(db
, &row
, "INSERT INTO identities (type,data) VALUES (?,?)",
314 DB_INT
, id
->get_type(id
), DB_BLOB
, id
->get_encoding(id
)) != 1)
316 fprintf(stderr
, "creating id '%Y' failed.\n", id
);
323 * ipsec pool --addattr <type> - add attribute entry
325 void add_attr(char *name
, char *pool
, char *identity
,
326 char *value
, value_type_t value_type
)
328 configuration_attribute_type_t type
, type_ip6
;
329 u_int pool_id
= 0, identity_id
= 0;
330 char id_pool_str
[128] = "";
336 pool_id
= get_attr_pool(pool
);
344 identification_t
*id
;
346 id
= identification_create_from_string(identity
);
347 identity_id
= get_identity(id
);
349 if (identity_id
== 0)
353 snprintf(id_pool_str
, sizeof(id_pool_str
),
354 " for '%s' in pool '%s'", identity
, pool
);
358 snprintf(id_pool_str
, sizeof(id_pool_str
), " in pool '%s'", pool
);
362 if (value_type
== VALUE_NONE
)
364 fprintf(stderr
, "the value of the %s attribute is missing.\n", name
);
367 if (!parse_attributes(name
, value
, &value_type
, &type
, &type_ip6
, &blob
))
372 success
= db
->execute(db
, NULL
,
373 "INSERT INTO attributes (identity, pool, type, value) "
374 "VALUES (?, ?, ?, ?)", DB_UINT
, identity_id
, DB_UINT
, pool_id
,
375 DB_INT
, type
, DB_BLOB
, blob
) == 1;
380 printf("added %s attribute (%N)%s.\n", name
,
381 configuration_attribute_type_names
, type
, id_pool_str
);
385 fprintf(stderr
, "adding %s attribute (%N)%s failed.\n", name
,
386 configuration_attribute_type_names
, type
, id_pool_str
);
391 * ipsec pool --delattr <type> - delete attribute entry
393 void del_attr(char *name
, char *pool
, char *identity
,
394 char *value
, value_type_t value_type
)
396 configuration_attribute_type_t type
, type_ip6
, type_db
;
397 u_int pool_id
= 0, identity_id
= 0;
398 char id_pool_str
[128] = "";
399 chunk_t blob
, blob_db
;
406 pool_id
= get_attr_pool(pool
);
414 identification_t
*id
;
416 id
= identification_create_from_string(identity
);
417 identity_id
= get_identity(id
);
419 if (identity_id
== 0)
423 snprintf(id_pool_str
, sizeof(id_pool_str
),
424 " for '%s' in pool '%s'", identity
, pool
);
428 snprintf(id_pool_str
, sizeof(id_pool_str
), " in pool '%s'", pool
);
432 if (!parse_attributes(name
, value
, &value_type
, &type
, &type_ip6
, &blob
))
439 query
= db
->query(db
,
440 "SELECT id, type, value FROM attributes "
441 "WHERE identity = ? AND pool = ? AND type = ? AND value = ?",
442 DB_UINT
, identity_id
, DB_UINT
, pool_id
, DB_INT
, type
,
443 DB_BLOB
, blob
, DB_UINT
, DB_INT
, DB_BLOB
);
445 else if (type_ip6
== 0)
447 query
= db
->query(db
,
448 "SELECT id, type, value FROM attributes "
449 "WHERE identity = ? AND pool = ? AND type = ?",
450 DB_UINT
, identity_id
, DB_UINT
, pool_id
, DB_INT
, type
,
451 DB_UINT
, DB_INT
, DB_BLOB
);
455 query
= db
->query(db
,
456 "SELECT id, type, value FROM attributes "
457 "WHERE identity = ? AND pool = ? AND (type = ? OR type = ?)",
458 DB_UINT
, identity_id
, DB_UINT
, pool_id
, DB_INT
, type
,
459 DB_INT
, type_ip6
, DB_UINT
, DB_INT
, DB_BLOB
);
464 fprintf(stderr
, "deleting '%s' attribute (%N)%s failed.\n",
465 name
, configuration_attribute_type_names
, type
, id_pool_str
);
470 while (query
->enumerate(query
, &id
, &type_db
, &blob_db
))
472 host_t
*server
= NULL
;
476 if (value_type
== VALUE_ADDR
)
478 int family
= (type_db
== type_ip6
) ? AF_INET6
: AF_INET
;
480 server
= host_create_from_chunk(family
, blob_db
, 0);
483 if (db
->execute(db
, NULL
,
484 "DELETE FROM attributes WHERE id = ?",
489 fprintf(stderr
, "deleting %s server %H%s failed\n",
490 name
, server
, id_pool_str
);
491 server
->destroy(server
);
493 else if (value_type
== VALUE_STRING
)
495 fprintf(stderr
, "deleting %s attribute (%N) with value '%.*s'%s failed.\n",
496 name
, configuration_attribute_type_names
, type
,
497 (int)blob_db
.len
, blob_db
.ptr
, id_pool_str
);
502 fprintf(stderr
, "deleting %s attribute (%N) with value %#B%s failed.\n",
503 name
, configuration_attribute_type_names
, type
,
504 &blob_db
, id_pool_str
);
506 query
->destroy(query
);
512 printf("deleted %s server %H%s\n", name
, server
, id_pool_str
);
513 server
->destroy(server
);
515 else if (value_type
== VALUE_STRING
)
517 printf("deleted %s attribute (%N) with value '%.*s'%s.\n",
518 name
, configuration_attribute_type_names
, type
,
519 (int)blob_db
.len
, blob_db
.ptr
, id_pool_str
);
523 printf("deleted %s attribute (%N) with value %#B%s.\n",
524 name
, configuration_attribute_type_names
, type
,
525 &blob_db
, id_pool_str
);
528 query
->destroy(query
);
536 fprintf(stderr
, "no %s attribute (%N) was found%s.\n", name
,
537 configuration_attribute_type_names
, type
, id_pool_str
);
541 fprintf(stderr
, "no %s attribute%s was found.\n",
547 if (value_type
== VALUE_ADDR
)
549 host_t
*server
= host_create_from_chunk(AF_UNSPEC
, blob
, 0);
551 fprintf(stderr
, "the %s server %H%s was not found.\n", name
,
552 server
, id_pool_str
);
553 server
->destroy(server
);
557 fprintf(stderr
, "the %s attribute (%N) with value '%.*s'%s "
558 "was not found.\n", name
,
559 configuration_attribute_type_names
, type
,
560 (int)blob
.len
, blob
.ptr
, id_pool_str
);
568 * ipsec pool --statusattr - show all attribute entries
570 void status_attr(bool hexout
)
572 configuration_attribute_type_t type
;
573 value_type_t value_type
;
574 chunk_t value
, addr_chunk
, mask_chunk
, identity_chunk
;
575 identification_t
*identity
;
576 enumerator_t
*enumerator
;
580 int i
, identity_type
;
583 /* enumerate over all attributes */
584 enumerator
= db
->query(db
,
585 "SELECT attributes.type, attribute_pools.name, "
586 "identities.type, identities.data, attributes.value "
588 "LEFT OUTER JOIN identities "
589 "ON attributes.identity = identities.id "
590 "LEFT OUTER JOIN attribute_pools "
591 "ON attributes.pool = attribute_pools.id "
592 "ORDER BY attributes.type, attribute_pools.name, "
593 "identities.type, identities.data, attributes.value",
594 DB_INT
, DB_TEXT
, DB_INT
, DB_BLOB
, DB_BLOB
);
597 while (enumerator
->enumerate(enumerator
, &type
,&pool_name
,
598 &identity_type
, &identity_chunk
, &value
))
602 printf(" type description pool "
603 " identity value\n");
606 snprintf(type_name
, sizeof(type_name
), "%N",
607 configuration_attribute_type_names
, type
);
608 if (type_name
[0] == '(')
612 printf("%5d %-20s ",type
, type_name
);
614 printf(" %-10s ", (pool_name ? pool_name
: ""));
618 identity
= identification_create_from_encoding(identity_type
, identity_chunk
);
619 printf(" %-20.20Y ", identity
);
620 identity
->destroy(identity
);
627 value_type
= VALUE_HEX
;
630 for (i
= 0; i
< countof(attr_info
); i
++)
632 if (type
== attr_info
[i
].type
)
634 value_type
= attr_info
[i
].value_type
;
642 addr
= host_create_from_chunk(AF_UNSPEC
, value
, 0);
645 printf(" %H\n", addr
);
650 /* value cannot be represented as an IP address */
651 printf(" %#B\n", &value
);
655 if (value
.len
% UNITY_NETWORK_LEN
== 0)
657 for (i
= 0; i
< value
.len
/ UNITY_NETWORK_LEN
; i
++)
659 addr_chunk
= chunk_create(value
.ptr
+ i
*UNITY_NETWORK_LEN
, 4);
660 addr
= host_create_from_chunk(AF_INET
, addr_chunk
, 0);
661 mask_chunk
= chunk_create(addr_chunk
.ptr
+ 4, 4);
662 mask
= host_create_from_chunk(AF_INET
, mask_chunk
, 0);
663 printf("%s%H/%H", (i
> 0) ?
"," : " ", addr
, mask
);
671 /* value cannot be represented as a list of subnets */
672 printf(" %#B\n", &value
);
676 printf("\"%.*s\"\n", (int)value
.len
, value
.ptr
);
680 printf(" %#B\n", &value
);
683 enumerator
->destroy(enumerator
);
688 * ipsec pool --showattr - show all supported attribute keywords
694 for (i
= 0; i
< countof(attr_info
); i
++)
699 snprintf(value_name
, sizeof(value_name
), "%N",
700 value_type_names
, attr_info
[i
].value_type
);
702 printf("%-20s --%-6s (%N",
703 attr_info
[i
].keyword
, value_name
,
704 configuration_attribute_type_names
, attr_info
[i
].type
);
706 if (attr_info
[i
].type_ip6
)
709 configuration_attribute_type_names
, attr_info
[i
].type_ip6
);