fix 64bit issue with time_t from database
[strongswan.git] / src / libstrongswan / plugins / attr_sql / pool.c
1 /*
2 * Copyright (C) 2008 Martin Willi
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 <getopt.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <time.h>
21
22 #include <debug.h>
23 #include <library.h>
24 #include <utils/host.h>
25 #include <utils/identification.h>
26 #include <attributes/attributes.h>
27
28 /**
29 * global database handle
30 */
31 database_t *db;
32
33 /**
34 * --start/--end/--server addresses of various subcommands
35 */
36 host_t *start = NULL, *end = NULL, *server = NULL;
37
38 /**
39 * instead of a pool handle a DNS or NBNS attribute
40 */
41 static bool is_attribute(char *name)
42 {
43 return strcaseeq(name, "dns") || strcaseeq(name, "nbns") ||
44 strcaseeq(name, "wins");
45 }
46
47 /**
48 * determine configuration attribute type
49 */
50 static configuration_attribute_type_t get_attribute_type(char *name, host_t* addr)
51 {
52 if (strcaseeq(name, "dns"))
53 {
54 return (addr->get_family(addr) == AF_INET) ? INTERNAL_IP4_DNS :
55 INTERNAL_IP6_DNS;
56 }
57 else
58 {
59 return (addr->get_family(addr) == AF_INET) ? INTERNAL_IP4_NBNS :
60 INTERNAL_IP6_NBNS;
61 }
62 }
63
64 /**
65 * calculate the size of a pool using start and end address chunk
66 */
67 static u_int get_pool_size(chunk_t start, chunk_t end)
68 {
69 u_int *start_ptr, *end_ptr;
70
71 if (start.len < sizeof(u_int) || end.len < sizeof(u_int))
72 {
73 return 0;
74 }
75 start_ptr = (u_int*)(start.ptr + start.len - sizeof(u_int));
76 end_ptr = (u_int*)(end.ptr + end.len - sizeof(u_int));
77 return ntohl(*end_ptr) - ntohl(*start_ptr) + 1;
78 }
79
80 /**
81 * print usage info
82 */
83 static void usage(void)
84 {
85 printf("\
86 Usage:\n\
87 ipsec pool --status|--add|--del|--resize|--purge [options]\n\
88 \n\
89 ipsec pool --status\n\
90 Show a list of installed pools with statistics.\n\
91 \n\
92 ipsec pool --add <name> --start <start> --end <end> [--timeout <timeout>]\n\
93 Add a new pool to the database.\n\
94 name: Name of the pool, as used in ipsec.conf rightsourceip=%%name\n\
95 start: Start address of the pool\n\
96 end: End address of the pool\n\
97 timeout: Lease time in hours, 0 for static leases\n\
98 \n\
99 ipsec pool --add dns|nbns|wins --server <server>\n\
100 Add a new DNS or NBNS server to the database.\n\
101 server: IP address of the name server\n\
102 \n\
103 ipsec pool --del <name>\n\
104 Delete a pool from the database.\n\
105 name: Name of the pool to delete\n\
106 \n\
107 ipsec pool --del dns|nbns|wins [--server <server>]\n\
108 Delete a specific or all DNS or NBNS servers from the database.\n\
109 server: IP address of the name server to delete\n\
110 \n\
111 ipsec pool --resize <name> --end <end>\n\
112 Grow or shrink an existing pool.\n\
113 name: Name of the pool to resize\n\
114 end: New end address for the pool\n\
115 \n\
116 ipsec pool --leases [--filter <filter>] [--utc]\n\
117 Show lease information using filters:\n\
118 filter: Filter string containing comma separated key=value filters,\n\
119 e.g. id=alice@strongswan.org,addr=1.1.1.1\n\
120 pool: name of the pool\n\
121 id: assigned identity of the lease\n\
122 addr: lease IP address\n\
123 tstamp: UNIX timestamp when lease was valid, as integer\n\
124 status: status of the lease: online|valid|expired\n\
125 utc: Show times in UTC instead of local time\n\
126 \n\
127 ipsec pool --purge <name>\n\
128 Delete lease history of a pool:\n\
129 name: Name of the pool to purge\n\
130 \n");
131 exit(0);
132 }
133
134 /**
135 * ipsec pool --status - show pool overview
136 */
137 static void status(void)
138 {
139 enumerator_t *ns, *pool, *lease;
140 host_t *server;
141 chunk_t value;
142 bool found = FALSE;
143
144 /* enumerate IPv4 DNS servers */
145 ns = db->query(db, "SELECT value FROM attributes WHERE type = ?",
146 DB_INT, INTERNAL_IP4_DNS, DB_BLOB);
147 if (ns)
148 {
149 while (ns->enumerate(ns, &value))
150 {
151 if (!found)
152 {
153 printf("dns servers:");
154 found = TRUE;
155 }
156 server = host_create_from_chunk(AF_INET, value, 0);
157 if (server)
158 {
159 printf(" %H", server);
160 server->destroy(server);
161 }
162 }
163 ns->destroy(ns);
164 }
165
166 /* enumerate IPv6 DNS servers */
167 ns = db->query(db, "SELECT value FROM attributes WHERE type = ?",
168 DB_INT, INTERNAL_IP6_DNS, DB_BLOB);
169 if (ns)
170 {
171 while (ns->enumerate(ns, &value))
172 {
173 if (!found)
174 {
175 printf("dns servers:");
176 found = TRUE;
177 }
178 server = host_create_from_chunk(AF_INET6, value, 0);
179 if (server)
180 {
181 printf(" %H", server);
182 server->destroy(server);
183 }
184 }
185 ns->destroy(ns);
186 }
187 if (found)
188 {
189 printf("\n");
190 }
191 else
192 {
193 printf("no dns servers found.\n");
194 }
195 found = FALSE;
196
197 /* enumerate IPv4 NBNS servers */
198 ns = db->query(db, "SELECT value FROM attributes WHERE type = ?",
199 DB_INT, INTERNAL_IP4_NBNS, DB_BLOB);
200 if (ns)
201 {
202 while (ns->enumerate(ns, &value))
203 {
204 if (!found)
205 {
206 printf("nbns servers:");
207 found = TRUE;
208 }
209 server = host_create_from_chunk(AF_INET, value, 0);
210 if (server)
211 {
212 printf(" %H", server);
213 server->destroy(server);
214 }
215 }
216 ns->destroy(ns);
217 }
218
219 /* enumerate IPv6 NBNS servers */
220 ns = db->query(db, "SELECT value FROM attributes WHERE type = ?",
221 DB_INT, INTERNAL_IP6_NBNS, DB_BLOB);
222 if (ns)
223 {
224 while (ns->enumerate(ns, &value))
225 {
226 if (!found)
227 {
228 printf("nbns servers:");
229 found = TRUE;
230 }
231 server = host_create_from_chunk(AF_INET6, value, 0);
232 if (server)
233 {
234 printf(" %H", server);
235 server->destroy(server);
236 }
237 }
238 ns->destroy(ns);
239 }
240 if (found)
241 {
242 printf("\n");
243 }
244 else
245 {
246 printf("no nbns servers found.\n");
247 }
248 found = FALSE;
249
250 pool = db->query(db, "SELECT id, name, start, end, timeout FROM pools",
251 DB_INT, DB_TEXT, DB_BLOB, DB_BLOB, DB_UINT);
252 if (pool)
253 {
254 char *name;
255 chunk_t start_chunk, end_chunk;
256 host_t *start, *end;
257 u_int id, timeout, online = 0, used = 0, size = 0;
258
259 while (pool->enumerate(pool, &id, &name,
260 &start_chunk, &end_chunk, &timeout))
261 {
262 if (!found)
263 {
264 printf("%8s %15s %15s %8s %6s %11s %11s\n", "name", "start",
265 "end", "timeout", "size", "online", "usage");
266 found = TRUE;
267 }
268
269 start = host_create_from_chunk(AF_UNSPEC, start_chunk, 0);
270 end = host_create_from_chunk(AF_UNSPEC, end_chunk, 0);
271 size = get_pool_size(start_chunk, end_chunk);
272 printf("%8s %15H %15H ", name, start, end);
273 if (timeout)
274 {
275 printf("%7dh ", timeout/3600);
276 }
277 else
278 {
279 printf("%8s ", "static");
280 }
281 printf("%6d ", size);
282 /* get number of online hosts */
283 lease = db->query(db, "SELECT COUNT(*) FROM addresses "
284 "WHERE pool = ? AND released = 0",
285 DB_UINT, id, DB_INT);
286 if (lease)
287 {
288 lease->enumerate(lease, &online);
289 lease->destroy(lease);
290 }
291 printf("%5d (%2d%%) ", online, online*100/size);
292 /* get number of online or valid lieases */
293 lease = db->query(db, "SELECT COUNT(*) FROM addresses "
294 "WHERE addresses.pool = ? "
295 "AND ((? AND acquired != 0) "
296 " OR released = 0 OR released > ?) ",
297 DB_UINT, id, DB_UINT, !timeout,
298 DB_UINT, time(NULL) - timeout, DB_UINT);
299 if (lease)
300 {
301 lease->enumerate(lease, &used);
302 lease->destroy(lease);
303 }
304 printf("%5d (%2d%%) ", used, used*100/size);
305
306 printf("\n");
307 DESTROY_IF(start);
308 DESTROY_IF(end);
309 }
310 pool->destroy(pool);
311 }
312 if (!found)
313 {
314 printf("no pools found.\n");
315 }
316 exit(0);
317 }
318
319 /**
320 * ipsec pool --add - add a new pool
321 */
322 static void add(char *name, host_t *start, host_t *end, int timeout)
323 {
324 chunk_t start_addr, end_addr, cur_addr;
325 u_int id, count;
326
327 start_addr = start->get_address(start);
328 end_addr = end->get_address(end);
329 cur_addr = chunk_clonea(start_addr);
330 count = get_pool_size(start_addr, end_addr);
331
332 if (start_addr.len != end_addr.len ||
333 memcmp(start_addr.ptr, end_addr.ptr, start_addr.len) > 0)
334 {
335 fprintf(stderr, "invalid start/end pair specified.\n");
336 exit(-1);
337 }
338 if (db->execute(db, &id,
339 "INSERT INTO pools (name, start, end, timeout) "
340 "VALUES (?, ?, ?, ?)",
341 DB_TEXT, name, DB_BLOB, start_addr,
342 DB_BLOB, end_addr, DB_INT, timeout*3600) != 1)
343 {
344 fprintf(stderr, "creating pool failed.\n");
345 exit(-1);
346 }
347 printf("allocating %d addresses... ", count);
348 fflush(stdout);
349 if (db->get_driver(db) == DB_SQLITE)
350 { /* run population in a transaction for sqlite */
351 db->execute(db, NULL, "BEGIN TRANSACTION");
352 }
353 while (TRUE)
354 {
355 db->execute(db, NULL,
356 "INSERT INTO addresses (pool, address, identity, acquired, released) "
357 "VALUES (?, ?, ?, ?, ?)",
358 DB_UINT, id, DB_BLOB, cur_addr, DB_UINT, 0, DB_UINT, 0, DB_UINT, 1);
359 if (chunk_equals(cur_addr, end_addr))
360 {
361 break;
362 }
363 chunk_increment(cur_addr);
364 }
365 if (db->get_driver(db) == DB_SQLITE)
366 {
367 db->execute(db, NULL, "END TRANSACTION");
368 }
369 printf("done.\n", count);
370
371 exit(0);
372 }
373
374 /**
375 * ipsec pool --add dns|nbns|wins - add a DNS or NBNS server entry
376 */
377 static void add_attr(char *name, host_t *server)
378 {
379 configuration_attribute_type_t type;
380 chunk_t value;
381
382 type = get_attribute_type(name, server);
383 value = server->get_address(server);
384 if (db->execute(db, NULL,
385 "INSERT INTO attributes (type, value) VALUES (?, ?)",
386 DB_INT, type, DB_BLOB, value) != 1)
387 {
388 fprintf(stderr, "adding %s server %H failed.\n", name, server);
389 exit(-1);
390 }
391 printf("added %s server %H\n", name, server);
392 exit(0);
393 }
394
395 /**
396 * ipsec pool --del - delete a pool
397 */
398 static void del(char *name)
399 {
400 enumerator_t *query;
401 u_int id;
402 bool found = FALSE;
403
404 query = db->query(db, "SELECT id FROM pools WHERE name = ?",
405 DB_TEXT, name, DB_UINT);
406 if (!query)
407 {
408 fprintf(stderr, "deleting pool failed.\n");
409 exit(-1);
410 }
411 while (query->enumerate(query, &id))
412 {
413 found = TRUE;
414 if (db->execute(db, NULL,
415 "DELETE FROM leases WHERE address IN ("
416 " SELECT id FROM addresses WHERE pool = ?)", DB_UINT, id) < 0 ||
417 db->execute(db, NULL,
418 "DELETE FROM addresses WHERE pool = ?", DB_UINT, id) < 0 ||
419 db->execute(db, NULL,
420 "DELETE FROM pools WHERE id = ?", DB_UINT, id) < 0)
421 {
422 fprintf(stderr, "deleting pool failed.\n");
423 query->destroy(query);
424 exit(-1);
425 }
426 }
427 query->destroy(query);
428 if (!found)
429 {
430 fprintf(stderr, "pool '%s' not found.\n", name);
431 exit(-1);
432 }
433 exit(0);
434 }
435
436 /**
437 * ipsec pool --del dns|nbns|wins - delete a DNS or NBNS server entry
438 */
439 static void del_attr(char *name, host_t *server)
440 {
441 configuration_attribute_type_t type;
442 chunk_t value;
443 u_int id;
444 enumerator_t *query;
445 bool found = FALSE;
446
447 if (server)
448 {
449 type = get_attribute_type(name, server);
450 value = server->get_address(server);
451 query = db->query(db,
452 "SELECT id, type, value FROM attributes "
453 "WHERE type = ? AND value = ?",
454 DB_INT, type, DB_BLOB, value,
455 DB_UINT, DB_INT, DB_BLOB);
456 }
457 else
458 {
459 configuration_attribute_type_t type_ip4, type_ip6;
460
461 if (strcaseeq(name, "dns"))
462 {
463 type_ip4 = INTERNAL_IP4_DNS;
464 type_ip6 = INTERNAL_IP6_DNS;
465 }
466 else
467 {
468 type_ip4 = INTERNAL_IP4_NBNS;
469 type_ip6 = INTERNAL_IP6_NBNS;
470 }
471
472 query = db->query(db,
473 "SELECT id, type, value FROM attributes "
474 "WHERE type = ? OR type = ?",
475 DB_INT, type_ip4, DB_INT, type_ip6,
476 DB_UINT, DB_INT, DB_BLOB);
477 }
478 if (!query)
479 {
480 fprintf(stderr, "deleting %s servers failed.\n", name);
481 exit(-1);
482 }
483
484 while (query->enumerate(query, &id, &type, &value))
485 {
486 int family;
487 host_t *host;
488
489 found = TRUE;
490 family = (type == INTERNAL_IP4_DNS || type == INTERNAL_IP4_NBNS) ?
491 AF_INET : AF_INET6;
492 host = host_create_from_chunk(family, value, 0);
493 if (db->execute(db, NULL,
494 "DELETE FROM attributes WHERE id = ?",
495 DB_UINT, id) != 1)
496 {
497 fprintf(stderr, "deleting %s server %H failed\n", name, host);
498 query->destroy(query);
499 DESTROY_IF(host);
500 exit(-1);
501 }
502 printf("deleted %s server %H\n", name, host);
503 DESTROY_IF(host);
504 }
505 query->destroy(query);
506
507 if (!found)
508 {
509 printf("no matching %s servers found\n", name);
510 exit(-1);
511 }
512 exit(0);
513 }
514
515 /**
516 * ipsec pool --resize - resize a pool if (db->execute(db, NULL,
517 "DELETE FROM attributes WHERE type = ? AND value = ?",
518 DB_INT, type, DB_BLOB, value) != 1)
519 {
520 fprintf(stderr, "deleting %s server %H failed\n", name, server);
521 exit(-1);
522 }
523 printf("deleted %s server %H\n", name, server);
524 if (db->execute(db, NULL,
525 "DELETE FROM attributes WHERE type = ? AND value = ?",
526 DB_INT, type, DB_BLOB, value) != 1)
527 {
528 fprintf(stderr, "deleting %s server %H failed\n", name, server);
529 exit(-1);
530 }
531 printf("deleted %s server %H\n", name, server);
532
533 */
534 static void resize(char *name, host_t *end)
535 {
536 enumerator_t *query;
537 chunk_t old_addr, new_addr, cur_addr;
538 u_int id, count;
539
540 new_addr = end->get_address(end);
541
542 query = db->query(db, "SELECT id, end FROM pools WHERE name = ?",
543 DB_TEXT, name, DB_UINT, DB_BLOB);
544 if (!query || !query->enumerate(query, &id, &old_addr))
545 {
546 DESTROY_IF(query);
547 fprintf(stderr, "resizing pool failed.\n");
548 exit(-1);
549 }
550 if (old_addr.len != new_addr.len ||
551 memcmp(new_addr.ptr, old_addr.ptr, old_addr.len) < 0)
552 {
553 fprintf(stderr, "shrinking of pools not supported.\n");
554 query->destroy(query);
555 exit(-1);
556 }
557 cur_addr = chunk_clonea(old_addr);
558 count = get_pool_size(old_addr, new_addr) - 1;
559 query->destroy(query);
560
561 if (db->execute(db, NULL,
562 "UPDATE pools SET end = ? WHERE name = ?",
563 DB_BLOB, new_addr, DB_TEXT, name) <= 0)
564 {
565 fprintf(stderr, "pool '%s' not found.\n", name);
566 exit(-1);
567 }
568
569 printf("allocating %d new addresses... ", count);
570 fflush(stdout);
571 if (db->get_driver(db) == DB_SQLITE)
572 { /* run population in a transaction for sqlite */
573 db->execute(db, NULL, "BEGIN TRANSACTION");
574 }
575 while (count-- > 0)
576 {
577 chunk_increment(cur_addr);
578 db->execute(db, NULL,
579 "INSERT INTO addresses (pool, address, identity, acquired, released) "
580 "VALUES (?, ?, ?, ?, ?)",
581 DB_UINT, id, DB_BLOB, cur_addr, DB_UINT, 0, DB_UINT, 0, DB_UINT, 1);
582 }
583 if (db->get_driver(db) == DB_SQLITE)
584 {
585 db->execute(db, NULL, "END TRANSACTION");
586 }
587 printf("done.\n", count);
588
589 exit(0);
590 }
591
592 /**
593 * create the lease query using the filter string
594 */
595 static enumerator_t *create_lease_query(char *filter)
596 {
597 enumerator_t *query;
598 identification_t *id = NULL;
599 host_t *addr = NULL;
600 u_int tstamp = 0;
601 bool online = FALSE, valid = FALSE, expired = FALSE;
602 char *value, *pos, *pool = NULL;
603 enum {
604 FIL_POOL = 0,
605 FIL_ID,
606 FIL_ADDR,
607 FIL_TSTAMP,
608 FIL_STATE,
609 };
610 char *const token[] = {
611 [FIL_POOL] = "pool",
612 [FIL_ID] = "id",
613 [FIL_ADDR] = "addr",
614 [FIL_TSTAMP] = "tstamp",
615 [FIL_STATE] = "status",
616 NULL
617 };
618
619 /* if the filter string contains a distinguished name as a ID, we replace
620 * ", " by "/ " in order to not confuse the getsubopt parser */
621 pos = filter;
622 while ((pos = strchr(pos, ',')))
623 {
624 if (pos[1] == ' ')
625 {
626 pos[0] = '/';
627 }
628 pos++;
629 }
630
631 while (filter && *filter != '\0')
632 {
633 switch (getsubopt(&filter, token, &value))
634 {
635 case FIL_POOL:
636 if (value)
637 {
638 pool = value;
639 }
640 break;
641 case FIL_ID:
642 if (value)
643 {
644 id = identification_create_from_string(value);
645 }
646 break;
647 case FIL_ADDR:
648 if (value)
649 {
650 addr = host_create_from_string(value, 0);
651 }
652 if (!addr)
653 {
654 fprintf(stderr, "invalid 'addr' in filter string.\n");
655 exit(-1);
656 }
657 break;
658 case FIL_TSTAMP:
659 if (value)
660 {
661 tstamp = atoi(value);
662 }
663 if (tstamp == 0)
664 {
665 online = TRUE;
666 }
667 break;
668 case FIL_STATE:
669 if (value)
670 {
671 if (streq(value, "online"))
672 {
673 online = TRUE;
674 }
675 else if (streq(value, "valid"))
676 {
677 valid = TRUE;
678 }
679 else if (streq(value, "expired"))
680 {
681 expired = TRUE;
682 }
683 else
684 {
685 fprintf(stderr, "invalid 'state' in filter string.\n");
686 exit(-1);
687 }
688 }
689 break;
690 default:
691 fprintf(stderr, "invalid filter string.\n");
692 exit(-1);
693 break;
694 }
695 }
696 query = db->query(db,
697 "SELECT name, addresses.address, identities.type, "
698 "identities.data, leases.acquired, leases.released, timeout "
699 "FROM leases JOIN addresses ON leases.address = addresses.id "
700 "JOIN pools ON addresses.pool = pools.id "
701 "JOIN identities ON leases.identity = identities.id "
702 "WHERE (? OR name = ?) "
703 "AND (? OR (identities.type = ? AND identities.data = ?)) "
704 "AND (? OR addresses.address = ?) "
705 "AND (? OR (? >= leases.acquired AND (? <= leases.released))) "
706 "AND (? OR leases.released > ? - timeout) "
707 "AND (? OR leases.released < ? - timeout) "
708 "AND ? "
709 "UNION "
710 "SELECT name, address, identities.type, identities.data, "
711 "acquired, released, timeout FROM addresses "
712 "JOIN pools ON addresses.pool = pools.id "
713 "JOIN identities ON addresses.identity = identities.id "
714 "WHERE ? AND released = 0 "
715 "AND (? OR name = ?) "
716 "AND (? OR (identities.type = ? AND identities.data = ?)) "
717 "AND (? OR address = ?)",
718 DB_INT, pool == NULL, DB_TEXT, pool,
719 DB_INT, id == NULL,
720 DB_INT, id ? id->get_type(id) : 0,
721 DB_BLOB, id ? id->get_encoding(id) : chunk_empty,
722 DB_INT, addr == NULL,
723 DB_BLOB, addr ? addr->get_address(addr) : chunk_empty,
724 DB_INT, tstamp == 0, DB_UINT, tstamp, DB_UINT, tstamp,
725 DB_INT, !valid, DB_INT, time(NULL),
726 DB_INT, !expired, DB_INT, time(NULL),
727 DB_INT, !online,
728 /* union */
729 DB_INT, !(valid || expired),
730 DB_INT, pool == NULL, DB_TEXT, pool,
731 DB_INT, id == NULL,
732 DB_INT, id ? id->get_type(id) : 0,
733 DB_BLOB, id ? id->get_encoding(id) : chunk_empty,
734 DB_INT, addr == NULL,
735 DB_BLOB, addr ? addr->get_address(addr) : chunk_empty,
736 /* res */
737 DB_TEXT, DB_BLOB, DB_INT, DB_BLOB, DB_UINT, DB_UINT, DB_UINT);
738 /* id and addr leak but we can't destroy them until query is destroyed. */
739 return query;
740 }
741
742 /**
743 * ipsec pool --leases - show lease information of a pool
744 */
745 static void leases(char *filter, bool utc)
746 {
747 enumerator_t *query;
748 chunk_t address_chunk, identity_chunk;
749 int identity_type;
750 char *name;
751 u_int db_acquired, db_released, db_timeout;
752 time_t acquired, released, timeout;
753 host_t *address;
754 identification_t *identity;
755 bool found = FALSE;
756
757 query = create_lease_query(filter);
758 if (!query)
759 {
760 fprintf(stderr, "querying leases failed.\n");
761 exit(-1);
762 }
763 while (query->enumerate(query, &name, &address_chunk, &identity_type,
764 &identity_chunk, &db_acquired, &db_released, &db_timeout))
765 {
766 if (!found)
767 {
768 int len = utc ? 25 : 21;
769
770 found = TRUE;
771 printf("%-8s %-15s %-7s %-*s %-*s %s\n",
772 "name", "address", "status", len, "start", len, "end", "identity");
773 }
774 address = host_create_from_chunk(AF_UNSPEC, address_chunk, 0);
775 identity = identification_create_from_encoding(identity_type, identity_chunk);
776
777 /* u_int is not always equal to time_t */
778 acquired = (time_t)db_acquired;
779 released = (time_t)db_released;
780 timeout = (time_t)db_timeout;
781
782 printf("%-8s %-15H ", name, address);
783 if (released == 0)
784 {
785 printf("%-7s ", "online");
786 }
787 else if (timeout == 0)
788 {
789 printf("%-7s ", "static");
790 }
791 else if (released >= time(NULL) - timeout)
792 {
793 printf("%-7s ", "valid");
794 }
795 else
796 {
797 printf("%-7s ", "expired");
798 }
799
800 printf(" %T ", &acquired, utc);
801 if (released)
802 {
803 printf("%T ", &released, utc);
804 }
805 else
806 {
807 printf(" ");
808 if (utc)
809 {
810 printf(" ");
811 }
812 }
813 printf("%Y\n", identity);
814 DESTROY_IF(address);
815 identity->destroy(identity);
816 }
817 query->destroy(query);
818 if (!found)
819 {
820 fprintf(stderr, "no matching leases found.\n");
821 exit(-1);
822 }
823 exit(0);
824 }
825
826 /**
827 * ipsec pool --purge - delete expired leases
828 */
829 static void purge(char *name)
830 {
831 int purged = 0;
832
833 purged = db->execute(db, NULL,
834 "DELETE FROM leases WHERE address IN ("
835 " SELECT id FROM addresses WHERE pool IN ("
836 " SELECT id FROM pools WHERE name = ?))",
837 DB_TEXT, name);
838 if (purged < 0)
839 {
840 fprintf(stderr, "purging pool '%s' failed.\n", name);
841 exit(-1);
842 }
843 fprintf(stderr, "purged %d leases in pool '%s'.\n", purged, name);
844 exit(0);
845 }
846
847 /**
848 * atexit handler to close db on shutdown
849 */
850 static void cleanup(void)
851 {
852 db->destroy(db);
853 DESTROY_IF(start);
854 DESTROY_IF(end);
855 DESTROY_IF(server);
856 }
857
858 int main(int argc, char *argv[])
859 {
860 char *uri, *name = "", *filter = "";
861 int timeout = 0;
862 bool utc = FALSE;
863 enum {
864 OP_USAGE,
865 OP_STATUS,
866 OP_ADD,
867 OP_ADD_ATTR,
868 OP_DEL,
869 OP_DEL_ATTR,
870 OP_RESIZE,
871 OP_LEASES,
872 OP_PURGE
873 } operation = OP_USAGE;
874
875 atexit(library_deinit);
876
877 /* initialize library */
878 if (!library_init(NULL))
879 {
880 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
881 }
882 if (lib->integrity &&
883 !lib->integrity->check_file(lib->integrity, "pool", argv[0]))
884 {
885 fprintf(stderr, "integrity check of pool failed\n");
886 exit(SS_RC_DAEMON_INTEGRITY);
887 }
888 if (!lib->plugins->load(lib->plugins, NULL,
889 lib->settings->get_str(lib->settings, "pool.load", PLUGINS)))
890 {
891 exit(SS_RC_INITIALIZATION_FAILED);
892 }
893
894 uri = lib->settings->get_str(lib->settings, "libstrongswan.plugins.attr-sql.database", NULL);
895 if (!uri)
896 {
897 fprintf(stderr, "database URI libstrongswan.plugins.attr-sql.database not set.\n");
898 exit(SS_RC_INITIALIZATION_FAILED);
899 }
900 db = lib->db->create(lib->db, uri);
901 if (!db)
902 {
903 fprintf(stderr, "opening database failed.\n");
904 exit(SS_RC_INITIALIZATION_FAILED);
905 }
906 atexit(cleanup);
907
908 while (TRUE)
909 {
910 int c;
911
912 struct option long_opts[] = {
913 { "help", no_argument, NULL, 'h' },
914
915 { "utc", no_argument, NULL, 'u' },
916 { "status", no_argument, NULL, 'w' },
917 { "add", required_argument, NULL, 'a' },
918 { "del", required_argument, NULL, 'd' },
919 { "resize", required_argument, NULL, 'r' },
920 { "leases", no_argument, NULL, 'l' },
921 { "purge", required_argument, NULL, 'p' },
922
923 { "start", required_argument, NULL, 's' },
924 { "end", required_argument, NULL, 'e' },
925 { "timeout", required_argument, NULL, 't' },
926 { "filter", required_argument, NULL, 'f' },
927 { "server", required_argument, NULL, 'v' },
928 { 0,0,0,0 }
929 };
930
931 c = getopt_long(argc, argv, "", long_opts, NULL);
932 switch (c)
933 {
934 case EOF:
935 break;
936 case 'h':
937 break;
938 case 'w':
939 operation = OP_STATUS;
940 break;
941 case 'u':
942 utc = TRUE;
943 continue;
944 case 'a':
945 name = optarg;
946 operation = is_attribute(name) ? OP_ADD_ATTR : OP_ADD;
947 continue;
948 case 'd':
949 name = optarg;
950 operation = is_attribute(name) ? OP_DEL_ATTR : OP_DEL;
951 continue;
952 case 'r':
953 name = optarg;
954 operation = OP_RESIZE;
955 continue;
956 case 'l':
957 operation = OP_LEASES;
958 continue;
959 case 'p':
960 name = optarg;
961 operation = OP_PURGE;
962 continue;
963 case 's':
964 start = host_create_from_string(optarg, 0);
965 if (start == NULL)
966 {
967 fprintf(stderr, "invalid start address: '%s'.\n", optarg);
968 operation = OP_USAGE;
969 break;
970 }
971 continue;
972 case 'e':
973 end = host_create_from_string(optarg, 0);
974 if (end == NULL)
975 {
976 fprintf(stderr, "invalid end address: '%s'.\n", optarg);
977 operation = OP_USAGE;
978 break;
979 }
980 continue;
981 case 't':
982 timeout = atoi(optarg);
983 if (timeout == 0 && strcmp(optarg, "0") != 0)
984 {
985 fprintf(stderr, "invalid timeout '%s'.\n", optarg);
986 operation = OP_USAGE;
987 break;
988 }
989 continue;
990 case 'f':
991 filter = optarg;
992 continue;
993 case 'v':
994 server = host_create_from_string(optarg, 0);
995 if (server == NULL)
996 {
997 fprintf(stderr, "invalid server address: '%s'.\n", optarg);
998 operation = OP_USAGE;
999 break;
1000 }
1001 continue;
1002 default:
1003 operation = OP_USAGE;
1004 break;
1005 }
1006 break;
1007 }
1008
1009 switch (operation)
1010 {
1011 case OP_USAGE:
1012 usage();
1013 break;
1014 case OP_STATUS:
1015 status();
1016 break;
1017 case OP_ADD:
1018 if (start == NULL || end == NULL)
1019 {
1020 fprintf(stderr, "missing arguments.\n");
1021 usage();
1022 }
1023 add(name, start, end, timeout);
1024 break;
1025 case OP_ADD_ATTR:
1026 if (server == NULL)
1027 {
1028 fprintf(stderr, "missing arguments.\n");
1029 usage();
1030 }
1031 add_attr(name, server);
1032 break;
1033 case OP_DEL:
1034 del(name);
1035 break;
1036 case OP_DEL_ATTR:
1037 del_attr(name, server);
1038 break;
1039 case OP_RESIZE:
1040 if (end == NULL)
1041 {
1042 fprintf(stderr, "missing arguments.\n");
1043 usage();
1044 }
1045 resize(name, end);
1046 break;
1047 case OP_LEASES:
1048 leases(filter, utc);
1049 break;
1050 case OP_PURGE:
1051 purge(name);
1052 break;
1053 }
1054 exit(0);
1055 }
1056