2 * Copyright (C) 2008 Martin Willi
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
26 #include <utils/host.h>
27 #include <utils/identification.h>
28 #include <attributes/attributes.h>
30 #include "pool_attributes.h"
31 #include "pool_usage.h"
34 * global database handle
39 * --start/--end addresses of various subcommands
41 host_t
*start
= NULL
, *end
= NULL
;
44 * whether --add should --replace an existing pool
46 bool replace_pool
= FALSE
;
49 * forward declarations
51 static void del(char *name
);
52 static void do_args(int argc
, char *argv
[]);
55 * nesting counter for database transaction functions
57 int nested_transaction
= 0;
60 * start a database transaction
62 static void begin_transaction()
64 if (db
->get_driver(db
) == DB_SQLITE
)
66 if (!nested_transaction
)
68 db
->execute(db
, NULL
, "BEGIN EXCLUSIVE TRANSACTION");
75 * commit a database transaction
77 static void commit_transaction()
79 if (db
->get_driver(db
) == DB_SQLITE
)
82 if (!nested_transaction
)
84 db
->execute(db
, NULL
, "END TRANSACTION");
90 * Create or replace a pool by name
92 static u_int
create_pool(char *name
, chunk_t start
, chunk_t end
, int timeout
)
97 e
= db
->query(db
, "SELECT id FROM pools WHERE name = ?",
98 DB_TEXT
, name
, DB_UINT
);
99 if (e
&& e
->enumerate(e
, &pool
))
101 if (replace_pool
== FALSE
)
103 fprintf(stderr
, "pool '%s' exists.\n", name
);
110 if (db
->execute(db
, &pool
,
111 "INSERT INTO pools (name, start, end, timeout) VALUES (?, ?, ?, ?)",
112 DB_TEXT
, name
, DB_BLOB
, start
, DB_BLOB
, end
,
113 DB_INT
, timeout
*3600) != 1)
115 fprintf(stderr
, "creating pool failed.\n");
123 * instead of a pool handle a DNS or NBNS attribute
125 static bool is_attribute(char *name
)
127 return strcaseeq(name
, "dns") || strcaseeq(name
, "nbns") ||
128 strcaseeq(name
, "wins");
132 * calculate the size of a pool using start and end address chunk
134 static u_int
get_pool_size(chunk_t start
, chunk_t end
)
136 u_int
*start_ptr
, *end_ptr
;
138 if (start
.len
< sizeof(u_int
) || end
.len
< sizeof(u_int
))
142 start_ptr
= (u_int
*)(start
.ptr
+ start
.len
- sizeof(u_int
));
143 end_ptr
= (u_int
*)(end
.ptr
+ end
.len
- sizeof(u_int
));
144 return ntohl(*end_ptr
) - ntohl(*start_ptr
) + 1;
148 * ipsec pool --status - show pool overview
150 static void status(void)
152 enumerator_t
*ns
, *pool
, *lease
;
157 /* enumerate IPv4 DNS servers */
158 ns
= db
->query(db
, "SELECT value FROM attributes WHERE type = ?",
159 DB_INT
, INTERNAL_IP4_DNS
, DB_BLOB
);
162 while (ns
->enumerate(ns
, &value
))
166 printf("dns servers:");
169 server
= host_create_from_chunk(AF_INET
, value
, 0);
172 printf(" %H", server
);
173 server
->destroy(server
);
179 /* enumerate IPv6 DNS servers */
180 ns
= db
->query(db
, "SELECT value FROM attributes WHERE type = ?",
181 DB_INT
, INTERNAL_IP6_DNS
, DB_BLOB
);
184 while (ns
->enumerate(ns
, &value
))
188 printf("dns servers:");
191 server
= host_create_from_chunk(AF_INET6
, value
, 0);
194 printf(" %H", server
);
195 server
->destroy(server
);
206 printf("no dns servers found.\n");
210 /* enumerate IPv4 NBNS servers */
211 ns
= db
->query(db
, "SELECT value FROM attributes WHERE type = ?",
212 DB_INT
, INTERNAL_IP4_NBNS
, DB_BLOB
);
215 while (ns
->enumerate(ns
, &value
))
219 printf("nbns servers:");
222 server
= host_create_from_chunk(AF_INET
, value
, 0);
225 printf(" %H", server
);
226 server
->destroy(server
);
232 /* enumerate IPv6 NBNS servers */
233 ns
= db
->query(db
, "SELECT value FROM attributes WHERE type = ?",
234 DB_INT
, INTERNAL_IP6_NBNS
, DB_BLOB
);
237 while (ns
->enumerate(ns
, &value
))
241 printf("nbns servers:");
244 server
= host_create_from_chunk(AF_INET6
, value
, 0);
247 printf(" %H", server
);
248 server
->destroy(server
);
259 printf("no nbns servers found.\n");
263 pool
= db
->query(db
, "SELECT id, name, start, end, timeout FROM pools",
264 DB_INT
, DB_TEXT
, DB_BLOB
, DB_BLOB
, DB_UINT
);
268 chunk_t start_chunk
, end_chunk
;
270 u_int id
, timeout
, online
= 0, used
= 0, size
= 0;
272 while (pool
->enumerate(pool
, &id
, &name
,
273 &start_chunk
, &end_chunk
, &timeout
))
277 printf("%8s %15s %15s %8s %6s %11s %11s\n", "name", "start",
278 "end", "timeout", "size", "online", "usage");
282 start
= host_create_from_chunk(AF_UNSPEC
, start_chunk
, 0);
283 end
= host_create_from_chunk(AF_UNSPEC
, end_chunk
, 0);
284 if (start
->is_anyaddr(start
) && end
->is_anyaddr(end
))
286 printf("%8s %15s %15s ", name
, "n/a", "n/a");
290 printf("%8s %15H %15H ", name
, start
, end
);
294 printf("%7dh ", timeout
/3600);
298 printf("%8s ", "static");
300 /* get total number of hosts in the pool */
301 lease
= db
->query(db
, "SELECT COUNT(*) FROM addresses "
302 "WHERE pool = ?", DB_UINT
, id
, DB_INT
);
305 lease
->enumerate(lease
, &size
);
306 lease
->destroy(lease
);
308 printf("%6d ", size
);
309 /* get number of online hosts */
310 lease
= db
->query(db
, "SELECT COUNT(*) FROM addresses "
311 "WHERE pool = ? AND released = 0",
312 DB_UINT
, id
, DB_INT
);
315 lease
->enumerate(lease
, &online
);
316 lease
->destroy(lease
);
318 printf("%5d (%2d%%) ", online
, online
*100/size
);
319 /* get number of online or valid lieases */
320 lease
= db
->query(db
, "SELECT COUNT(*) FROM addresses "
321 "WHERE addresses.pool = ? "
322 "AND ((? AND acquired != 0) "
323 " OR released = 0 OR released > ?) ",
324 DB_UINT
, id
, DB_UINT
, !timeout
,
325 DB_UINT
, time(NULL
) - timeout
, DB_UINT
);
328 lease
->enumerate(lease
, &used
);
329 lease
->destroy(lease
);
331 printf("%5d (%2d%%) ", used
, used
*100/size
);
341 printf("no pools found.\n");
346 * ipsec pool --add - add a new pool
348 static void add(char *name
, host_t
*start
, host_t
*end
, int timeout
)
350 chunk_t start_addr
, end_addr
, cur_addr
;
353 start_addr
= start
->get_address(start
);
354 end_addr
= end
->get_address(end
);
355 cur_addr
= chunk_clonea(start_addr
);
356 count
= get_pool_size(start_addr
, end_addr
);
358 if (start_addr
.len
!= end_addr
.len
||
359 memcmp(start_addr
.ptr
, end_addr
.ptr
, start_addr
.len
) > 0)
361 fprintf(stderr
, "invalid start/end pair specified.\n");
364 id
= create_pool(name
, start_addr
, end_addr
, timeout
);
365 printf("allocating %d addresses... ", count
);
367 /* run population in a transaction for sqlite */
371 db
->execute(db
, NULL
,
372 "INSERT INTO addresses (pool, address, identity, acquired, released) "
373 "VALUES (?, ?, ?, ?, ?)",
374 DB_UINT
, id
, DB_BLOB
, cur_addr
, DB_UINT
, 0, DB_UINT
, 0, DB_UINT
, 1);
375 if (chunk_equals(cur_addr
, end_addr
))
379 chunk_increment(cur_addr
);
381 commit_transaction();
382 printf("done.\n", count
);
385 static bool add_address(u_int pool_id
, char *address_str
, int *family
)
390 char *pos_eq
= strchr(address_str
, '=');
394 identification_t
*id
= identification_create_from_string(pos_eq
+ 1);
396 /* look for peer identity in the identities table */
398 "SELECT id FROM identities WHERE type = ? AND data = ?",
399 DB_INT
, id
->get_type(id
), DB_BLOB
, id
->get_encoding(id
),
402 if (!e
|| !e
->enumerate(e
, &user_id
))
404 /* not found, insert new one */
405 if (db
->execute(db
, &user_id
,
406 "INSERT INTO identities (type, data) VALUES (?, ?)",
407 DB_INT
, id
->get_type(id
),
408 DB_BLOB
, id
->get_encoding(id
)) != 1)
410 fprintf(stderr
, "creating id '%s' failed.\n", pos_eq
+ 1);
419 address
= host_create_from_string(address_str
, 0);
422 fprintf(stderr
, "invalid address '%s'.\n", address_str
);
425 if (family
&& *family
&& *family
!= address
->get_family(address
))
427 fprintf(stderr
, "invalid address family '%s'.\n", address_str
);
431 if (db
->execute(db
, NULL
,
432 "INSERT INTO addresses "
433 "(pool, address, identity, acquired, released) "
434 "VALUES (?, ?, ?, ?, ?)",
435 DB_UINT
, pool_id
, DB_BLOB
, address
->get_address(address
),
436 DB_UINT
, user_id
, DB_UINT
, 0, DB_UINT
, 1) != 1)
438 fprintf(stderr
, "inserting address '%s' failed.\n", address_str
);
441 *family
= address
->get_family(address
);
442 address
->destroy(address
);
447 static void add_addresses(char *pool
, char *path
, int timeout
)
449 u_int pool_id
, count
= 0;
450 int family
= AF_UNSPEC
;
451 char address_str
[512];
455 /* run population in a transaction for sqlite */
458 addr
= host_create_from_string("%any", 0);
459 pool_id
= create_pool(pool
, addr
->get_address(addr
),
460 addr
->get_address(addr
), timeout
);
463 file
= (strcmp(path
, "-") == 0 ? stdin
: fopen(path
, "r"));
466 fprintf(stderr
, "opening '%s' failed: %s\n", path
, strerror(errno
));
470 printf("starting allocation... ");
473 while (fgets(address_str
, sizeof(address_str
), file
))
475 size_t addr_len
= strlen(address_str
);
476 char *last_chr
= address_str
+ addr_len
- 1;
477 if (*last_chr
== '\n')
485 if (add_address(pool_id
, address_str
, &family
) == FALSE
)
497 commit_transaction();
499 printf("%d addresses done.\n", count
);
503 * ipsec pool --del - delete a pool
505 static void del(char *name
)
511 query
= db
->query(db
, "SELECT id FROM pools WHERE name = ?",
512 DB_TEXT
, name
, DB_UINT
);
515 fprintf(stderr
, "deleting pool failed.\n");
518 while (query
->enumerate(query
, &id
))
521 if (db
->execute(db
, NULL
,
522 "DELETE FROM leases WHERE address IN ("
523 " SELECT id FROM addresses WHERE pool = ?)", DB_UINT
, id
) < 0 ||
524 db
->execute(db
, NULL
,
525 "DELETE FROM addresses WHERE pool = ?", DB_UINT
, id
) < 0 ||
526 db
->execute(db
, NULL
,
527 "DELETE FROM pools WHERE id = ?", DB_UINT
, id
) < 0)
529 fprintf(stderr
, "deleting pool failed.\n");
530 query
->destroy(query
);
534 query
->destroy(query
);
537 fprintf(stderr
, "pool '%s' not found.\n", name
);
543 * ipsec pool --resize - resize a pool
545 static void resize(char *name
, host_t
*end
)
548 chunk_t old_addr
, new_addr
, cur_addr
;
552 new_addr
= end
->get_address(end
);
554 query
= db
->query(db
, "SELECT id, end FROM pools WHERE name = ?",
555 DB_TEXT
, name
, DB_UINT
, DB_BLOB
);
556 if (!query
|| !query
->enumerate(query
, &id
, &old_addr
))
559 fprintf(stderr
, "resizing pool failed.\n");
562 if (old_addr
.len
!= new_addr
.len
||
563 memcmp(new_addr
.ptr
, old_addr
.ptr
, old_addr
.len
) < 0)
565 fprintf(stderr
, "shrinking of pools not supported.\n");
566 query
->destroy(query
);
569 cur_addr
= chunk_clonea(old_addr
);
570 count
= get_pool_size(old_addr
, new_addr
) - 1;
571 query
->destroy(query
);
573 /* Check whether pool is resizable */
574 old_end
= host_create_from_chunk(AF_UNSPEC
, old_addr
, 0);
575 if (old_end
&& old_end
->is_anyaddr(old_end
))
577 fprintf(stderr
, "pool is not resizable.\n");
578 old_end
->destroy(old_end
);
583 if (db
->execute(db
, NULL
,
584 "UPDATE pools SET end = ? WHERE name = ?",
585 DB_BLOB
, new_addr
, DB_TEXT
, name
) <= 0)
587 fprintf(stderr
, "pool '%s' not found.\n", name
);
591 printf("allocating %d new addresses... ", count
);
593 /* run population in a transaction for sqlite */
597 chunk_increment(cur_addr
);
598 db
->execute(db
, NULL
,
599 "INSERT INTO addresses (pool, address, identity, acquired, released) "
600 "VALUES (?, ?, ?, ?, ?)",
601 DB_UINT
, id
, DB_BLOB
, cur_addr
, DB_UINT
, 0, DB_UINT
, 0, DB_UINT
, 1);
603 commit_transaction();
604 printf("done.\n", count
);
609 * create the lease query using the filter string
611 static enumerator_t
*create_lease_query(char *filter
)
614 identification_t
*id
= NULL
;
617 bool online
= FALSE
, valid
= FALSE
, expired
= FALSE
;
618 char *value
, *pos
, *pool
= NULL
;
626 char *const token
[] = {
630 [FIL_TSTAMP
] = "tstamp",
631 [FIL_STATE
] = "status",
635 /* if the filter string contains a distinguished name as a ID, we replace
636 * ", " by "/ " in order to not confuse the getsubopt parser */
638 while ((pos
= strchr(pos
, ',')))
647 while (filter
&& *filter
!= '\0')
649 switch (getsubopt(&filter
, token
, &value
))
660 id
= identification_create_from_string(value
);
666 addr
= host_create_from_string(value
, 0);
670 fprintf(stderr
, "invalid 'addr' in filter string.\n");
677 tstamp
= atoi(value
);
687 if (streq(value
, "online"))
691 else if (streq(value
, "valid"))
695 else if (streq(value
, "expired"))
701 fprintf(stderr
, "invalid 'state' in filter string.\n");
707 fprintf(stderr
, "invalid filter string.\n");
712 query
= db
->query(db
,
713 "SELECT name, addresses.address, identities.type, "
714 "identities.data, leases.acquired, leases.released, timeout "
715 "FROM leases JOIN addresses ON leases.address = addresses.id "
716 "JOIN pools ON addresses.pool = pools.id "
717 "JOIN identities ON leases.identity = identities.id "
718 "WHERE (? OR name = ?) "
719 "AND (? OR (identities.type = ? AND identities.data = ?)) "
720 "AND (? OR addresses.address = ?) "
721 "AND (? OR (? >= leases.acquired AND (? <= leases.released))) "
722 "AND (? OR leases.released > ? - timeout) "
723 "AND (? OR leases.released < ? - timeout) "
726 "SELECT name, address, identities.type, identities.data, "
727 "acquired, released, timeout FROM addresses "
728 "JOIN pools ON addresses.pool = pools.id "
729 "JOIN identities ON addresses.identity = identities.id "
730 "WHERE ? AND released = 0 "
731 "AND (? OR name = ?) "
732 "AND (? OR (identities.type = ? AND identities.data = ?)) "
733 "AND (? OR address = ?)",
734 DB_INT
, pool
== NULL
, DB_TEXT
, pool
,
736 DB_INT
, id ? id
->get_type(id
) : 0,
737 DB_BLOB
, id ? id
->get_encoding(id
) : chunk_empty
,
738 DB_INT
, addr
== NULL
,
739 DB_BLOB
, addr ? addr
->get_address(addr
) : chunk_empty
,
740 DB_INT
, tstamp
== 0, DB_UINT
, tstamp
, DB_UINT
, tstamp
,
741 DB_INT
, !valid
, DB_INT
, time(NULL
),
742 DB_INT
, !expired
, DB_INT
, time(NULL
),
745 DB_INT
, !(valid
|| expired
),
746 DB_INT
, pool
== NULL
, DB_TEXT
, pool
,
748 DB_INT
, id ? id
->get_type(id
) : 0,
749 DB_BLOB
, id ? id
->get_encoding(id
) : chunk_empty
,
750 DB_INT
, addr
== NULL
,
751 DB_BLOB
, addr ? addr
->get_address(addr
) : chunk_empty
,
753 DB_TEXT
, DB_BLOB
, DB_INT
, DB_BLOB
, DB_UINT
, DB_UINT
, DB_UINT
);
754 /* id and addr leak but we can't destroy them until query is destroyed. */
759 * ipsec pool --leases - show lease information of a pool
761 static void leases(char *filter
, bool utc
)
764 chunk_t address_chunk
, identity_chunk
;
767 u_int db_acquired
, db_released
, db_timeout
;
768 time_t acquired
, released
, timeout
;
770 identification_t
*identity
;
773 query
= create_lease_query(filter
);
776 fprintf(stderr
, "querying leases failed.\n");
779 while (query
->enumerate(query
, &name
, &address_chunk
, &identity_type
,
780 &identity_chunk
, &db_acquired
, &db_released
, &db_timeout
))
784 int len
= utc ?
25 : 21;
787 printf("%-8s %-15s %-7s %-*s %-*s %s\n",
788 "name", "address", "status", len
, "start", len
, "end", "identity");
790 address
= host_create_from_chunk(AF_UNSPEC
, address_chunk
, 0);
791 identity
= identification_create_from_encoding(identity_type
, identity_chunk
);
793 /* u_int is not always equal to time_t */
794 acquired
= (time_t)db_acquired
;
795 released
= (time_t)db_released
;
796 timeout
= (time_t)db_timeout
;
798 printf("%-8s %-15H ", name
, address
);
801 printf("%-7s ", "online");
803 else if (timeout
== 0)
805 printf("%-7s ", "static");
807 else if (released
>= time(NULL
) - timeout
)
809 printf("%-7s ", "valid");
813 printf("%-7s ", "expired");
816 printf(" %T ", &acquired
, utc
);
819 printf("%T ", &released
, utc
);
829 printf("%Y\n", identity
);
831 identity
->destroy(identity
);
833 query
->destroy(query
);
836 fprintf(stderr
, "no matching leases found.\n");
842 * ipsec pool --purge - delete expired leases
844 static void purge(char *name
)
848 purged
= db
->execute(db
, NULL
,
849 "DELETE FROM leases WHERE address IN ("
850 " SELECT id FROM addresses WHERE pool IN ("
851 " SELECT id FROM pools WHERE name = ?))",
855 fprintf(stderr
, "purging pool '%s' failed.\n", name
);
858 fprintf(stderr
, "purged %d leases in pool '%s'.\n", purged
, name
);
863 static void argv_add(char **argv
, int argc
, char *value
)
865 if (argc
>= ARGV_SIZE
)
867 fprintf(stderr
, "too many arguments: %s\n", value
);
874 * ipsec pool --batch - read commands from a file
876 static void batch(char *argv0
, char *name
)
880 FILE *file
= strncmp(name
, "-", 1) == 0 ? stdin
: fopen(name
, "r");
883 fprintf(stderr
, "opening '%s' failed: %s\n", name
, strerror(errno
));
888 while (fgets(command
, sizeof(command
), file
))
890 char *argv
[ARGV_SIZE
], *start
;
892 size_t cmd_len
= strlen(command
);
894 /* ignore empty lines */
895 if (cmd_len
== 1 && *(command
+ cmd_len
- 1) == '\n')
900 /* parse command into argv */
902 argv_add(argv
, argc
++, argv0
);
903 for (i
= 0; i
< cmd_len
; ++i
)
905 if (command
[i
] == ' ' || command
[i
] == '\n')
907 if (command
+ i
== start
)
909 /* ignore leading whitespace */
914 argv_add(argv
, argc
++, start
);
915 start
= command
+ i
+ 1;
918 if (strlen(start
) > 0)
920 argv_add(argv
, argc
++, start
);
922 argv_add(argv
, argc
, NULL
);
926 commit_transaction();
935 * atexit handler to close db on shutdown
937 static void cleanup(void)
944 static void do_args(int argc
, char *argv
[])
946 char *name
= "", *value
= "", *filter
= "", *addresses
= NULL
;
947 value_type_t value_type
= VALUE_NONE
;
949 bool utc
= FALSE
, hexout
= FALSE
;
965 } operation
= OP_UNDEF
;
967 /* reinit getopt state */
974 struct option long_opts
[] = {
975 { "help", no_argument
, NULL
, 'h' },
977 { "utc", no_argument
, NULL
, 'u' },
978 { "status", no_argument
, NULL
, 'w' },
979 { "add", required_argument
, NULL
, 'a' },
980 { "replace", required_argument
, NULL
, 'c' },
981 { "del", required_argument
, NULL
, 'd' },
982 { "resize", required_argument
, NULL
, 'r' },
983 { "leases", no_argument
, NULL
, 'l' },
984 { "purge", required_argument
, NULL
, 'p' },
985 { "statusattr", no_argument
, NULL
, '1' },
986 { "addattr", required_argument
, NULL
, '2' },
987 { "delattr", required_argument
, NULL
, '3' },
988 { "showattr", no_argument
, NULL
, '4' },
989 { "batch", required_argument
, NULL
, 'b' },
991 { "start", required_argument
, NULL
, 's' },
992 { "end", required_argument
, NULL
, 'e' },
993 { "addresses", required_argument
, NULL
, 'y' },
994 { "timeout", required_argument
, NULL
, 't' },
995 { "filter", required_argument
, NULL
, 'f' },
996 { "addr", required_argument
, NULL
, 'v' },
997 { "mask", required_argument
, NULL
, 'v' },
998 { "server", required_argument
, NULL
, 'v' },
999 { "subnet", required_argument
, NULL
, 'n' },
1000 { "string", required_argument
, NULL
, 'g' },
1001 { "hex", required_argument
, NULL
, 'x' },
1002 { "hexout", no_argument
, NULL
, '5' },
1006 c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
1012 operation
= OP_USAGE
;
1015 operation
= OP_STATUS
;
1018 operation
= OP_STATUS_ATTR
;
1023 replace_pool
= TRUE
;
1027 operation
= is_attribute(name
) ? OP_ADD_ATTR
: OP_ADD
;
1028 if (replace_pool
&& operation
== OP_ADD_ATTR
)
1030 fprintf(stderr
, "invalid pool name: "
1031 "reserved for '%s' attribute.\n", optarg
);
1038 operation
= OP_ADD_ATTR
;
1042 operation
= is_attribute(name
) ? OP_DEL_ATTR
: OP_DEL
;
1046 operation
= OP_DEL_ATTR
;
1049 operation
= OP_SHOW_ATTR
;
1053 operation
= OP_RESIZE
;
1056 operation
= OP_LEASES
;
1060 operation
= OP_PURGE
;
1064 if (operation
== OP_BATCH
)
1066 fprintf(stderr
, "--batch commands can not be nested\n");
1069 operation
= OP_BATCH
;
1073 start
= host_create_from_string(optarg
, 0);
1076 fprintf(stderr
, "invalid start address: '%s'.\n", optarg
);
1083 end
= host_create_from_string(optarg
, 0);
1086 fprintf(stderr
, "invalid end address: '%s'.\n", optarg
);
1092 timeout
= atoi(optarg
);
1093 if (timeout
== 0 && strcmp(optarg
, "0") != 0)
1095 fprintf(stderr
, "invalid timeout '%s'.\n", optarg
);
1107 value_type
= VALUE_STRING
;
1111 value_type
= VALUE_SUBNET
;
1115 value_type
= VALUE_ADDR
;
1119 value_type
= VALUE_HEX
;
1141 case OP_STATUS_ATTR
:
1142 status_attr(hexout
);
1145 if (addresses
!= NULL
)
1147 add_addresses(name
, addresses
, timeout
);
1149 else if (start
!= NULL
&& end
!= NULL
)
1151 add(name
, start
, end
, timeout
);
1155 fprintf(stderr
, "missing arguments.\n");
1161 if (value_type
== VALUE_NONE
)
1163 fprintf(stderr
, "missing arguments.\n");
1167 add_attr(name
, value
, value_type
);
1174 del_attr(name
, value
, value_type
);
1182 fprintf(stderr
, "missing arguments.\n");
1189 leases(filter
, utc
);
1197 fprintf(stderr
, "missing arguments.\n");
1201 batch(argv
[0], name
);
1209 int main(int argc
, char *argv
[])
1213 atexit(library_deinit
);
1215 /* initialize library */
1216 if (!library_init(NULL
))
1218 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY
);
1220 if (lib
->integrity
&&
1221 !lib
->integrity
->check_file(lib
->integrity
, "pool", argv
[0]))
1223 fprintf(stderr
, "integrity check of pool failed\n");
1224 exit(SS_RC_DAEMON_INTEGRITY
);
1226 if (!lib
->plugins
->load(lib
->plugins
, NULL
,
1227 lib
->settings
->get_str(lib
->settings
, "pool.load", PLUGINS
)))
1229 exit(SS_RC_INITIALIZATION_FAILED
);
1232 uri
= lib
->settings
->get_str(lib
->settings
, "libhydra.plugins.attr-sql.database", NULL
);
1235 fprintf(stderr
, "database URI libhydra.plugins.attr-sql.database not set.\n");
1236 exit(SS_RC_INITIALIZATION_FAILED
);
1238 db
= lib
->db
->create(lib
->db
, uri
);
1241 fprintf(stderr
, "opening database failed.\n");
1242 exit(SS_RC_INITIALIZATION_FAILED
);
1246 do_args(argc
, argv
);