pool.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /*
  2. * Copyright (C) 2011-2017 Tobias Brunner
  3. * Copyright (C) 2008 Martin Willi
  4. * HSR Hochschule fuer Technik Rapperswil
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. */
  16. #define _GNU_SOURCE
  17. #include <getopt.h>
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <time.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <utils/debug.h>
  24. #include <library.h>
  25. #include <collections/array.h>
  26. #include <networking/host.h>
  27. #include <utils/identification.h>
  28. #include <attributes/attributes.h>
  29. #include "pool_attributes.h"
  30. #include "pool_usage.h"
  31. /**
  32. * global database handle
  33. */
  34. database_t *db;
  35. /**
  36. * --start/--end addresses of various subcommands
  37. */
  38. host_t *start = NULL, *end = NULL;
  39. /**
  40. * whether --add should --replace an existing pool
  41. */
  42. bool replace_pool = FALSE;
  43. /**
  44. * forward declarations
  45. */
  46. static void del(char *name);
  47. static void do_args(int argc, char *argv[]);
  48. /**
  49. * Create or replace a pool by name
  50. */
  51. static u_int create_pool(char *name, chunk_t start, chunk_t end, u_int timeout)
  52. {
  53. enumerator_t *e;
  54. int pool;
  55. bool exists;
  56. e = db->query(db, "SELECT id FROM pools WHERE name = ?",
  57. DB_TEXT, name, DB_UINT);
  58. exists = e && e->enumerate(e, &pool);
  59. DESTROY_IF(e);
  60. if (exists)
  61. {
  62. if (!replace_pool)
  63. {
  64. fprintf(stderr, "pool '%s' exists.\n", name);
  65. exit(EXIT_FAILURE);
  66. }
  67. del(name);
  68. }
  69. if (db->execute(db, &pool,
  70. "INSERT INTO pools (name, start, end, timeout) VALUES (?, ?, ?, ?)",
  71. DB_TEXT, name, DB_BLOB, start, DB_BLOB, end,
  72. DB_UINT, timeout) != 1)
  73. {
  74. fprintf(stderr, "creating pool failed.\n");
  75. exit(EXIT_FAILURE);
  76. }
  77. return pool;
  78. }
  79. /**
  80. * instead of a pool handle a DNS or NBNS attribute
  81. */
  82. static bool is_attribute(char *name)
  83. {
  84. return strcaseeq(name, "dns") || strcaseeq(name, "nbns") ||
  85. strcaseeq(name, "wins");
  86. }
  87. /**
  88. * calculate the size of a pool using start and end address chunk
  89. */
  90. static u_int get_pool_size(chunk_t start, chunk_t end)
  91. {
  92. u_int *start_ptr, *end_ptr;
  93. if (start.len < sizeof(u_int) || end.len < sizeof(u_int))
  94. {
  95. return 0;
  96. }
  97. start_ptr = (u_int*)(start.ptr + start.len - sizeof(u_int));
  98. end_ptr = (u_int*)(end.ptr + end.len - sizeof(u_int));
  99. return ntohl(*end_ptr) - ntohl(*start_ptr) + 1;
  100. }
  101. /**
  102. * ipsec pool --status - show pool overview
  103. */
  104. static void status(void)
  105. {
  106. enumerator_t *ns, *pool, *lease;
  107. host_t *server;
  108. chunk_t value;
  109. bool found = FALSE;
  110. /* enumerate IPv4 DNS servers */
  111. ns = db->query(db, "SELECT value FROM attributes WHERE type = ?",
  112. DB_INT, INTERNAL_IP4_DNS, DB_BLOB);
  113. if (ns)
  114. {
  115. while (ns->enumerate(ns, &value))
  116. {
  117. if (!found)
  118. {
  119. printf("dns servers:");
  120. found = TRUE;
  121. }
  122. server = host_create_from_chunk(AF_INET, value, 0);
  123. if (server)
  124. {
  125. printf(" %H", server);
  126. server->destroy(server);
  127. }
  128. }
  129. ns->destroy(ns);
  130. }
  131. /* enumerate IPv6 DNS servers */
  132. ns = db->query(db, "SELECT value FROM attributes WHERE type = ?",
  133. DB_INT, INTERNAL_IP6_DNS, DB_BLOB);
  134. if (ns)
  135. {
  136. while (ns->enumerate(ns, &value))
  137. {
  138. if (!found)
  139. {
  140. printf("dns servers:");
  141. found = TRUE;
  142. }
  143. server = host_create_from_chunk(AF_INET6, value, 0);
  144. if (server)
  145. {
  146. printf(" %H", server);
  147. server->destroy(server);
  148. }
  149. }
  150. ns->destroy(ns);
  151. }
  152. if (found)
  153. {
  154. printf("\n");
  155. }
  156. else
  157. {
  158. printf("no dns servers found.\n");
  159. }
  160. found = FALSE;
  161. /* enumerate IPv4 NBNS servers */
  162. ns = db->query(db, "SELECT value FROM attributes WHERE type = ?",
  163. DB_INT, INTERNAL_IP4_NBNS, DB_BLOB);
  164. if (ns)
  165. {
  166. while (ns->enumerate(ns, &value))
  167. {
  168. if (!found)
  169. {
  170. printf("nbns servers:");
  171. found = TRUE;
  172. }
  173. server = host_create_from_chunk(AF_INET, value, 0);
  174. if (server)
  175. {
  176. printf(" %H", server);
  177. server->destroy(server);
  178. }
  179. }
  180. ns->destroy(ns);
  181. }
  182. /* enumerate IPv6 NBNS servers */
  183. ns = db->query(db, "SELECT value FROM attributes WHERE type = ?",
  184. DB_INT, INTERNAL_IP6_NBNS, DB_BLOB);
  185. if (ns)
  186. {
  187. while (ns->enumerate(ns, &value))
  188. {
  189. if (!found)
  190. {
  191. printf("nbns servers:");
  192. found = TRUE;
  193. }
  194. server = host_create_from_chunk(AF_INET6, value, 0);
  195. if (server)
  196. {
  197. printf(" %H", server);
  198. server->destroy(server);
  199. }
  200. }
  201. ns->destroy(ns);
  202. }
  203. if (found)
  204. {
  205. printf("\n");
  206. }
  207. else
  208. {
  209. printf("no nbns servers found.\n");
  210. }
  211. found = FALSE;
  212. pool = db->query(db, "SELECT id, name, start, end, timeout FROM pools",
  213. DB_INT, DB_TEXT, DB_BLOB, DB_BLOB, DB_UINT);
  214. if (pool)
  215. {
  216. char *name;
  217. chunk_t start_chunk, end_chunk;
  218. host_t *start, *end;
  219. u_int id, timeout, online = 0, used = 0, size = 0;
  220. while (pool->enumerate(pool, &id, &name,
  221. &start_chunk, &end_chunk, &timeout))
  222. {
  223. if (!found)
  224. {
  225. printf("%8s %15s %15s %8s %6s %11s %11s\n", "name", "start",
  226. "end", "timeout", "size", "online", "usage");
  227. found = TRUE;
  228. }
  229. start = host_create_from_chunk(AF_UNSPEC, start_chunk, 0);
  230. end = host_create_from_chunk(AF_UNSPEC, end_chunk, 0);
  231. if (start->is_anyaddr(start) && end->is_anyaddr(end))
  232. {
  233. printf("%8s %15s %15s ", name, "n/a", "n/a");
  234. }
  235. else
  236. {
  237. printf("%8s %15H %15H ", name, start, end);
  238. }
  239. if (timeout)
  240. {
  241. if (timeout >= 60 * 300)
  242. {
  243. printf("%7dh ", timeout/3600);
  244. }
  245. else if (timeout >= 300)
  246. {
  247. printf("%7dm ", timeout/60);
  248. }
  249. else
  250. {
  251. printf("%7ds ", timeout);
  252. }
  253. }
  254. else
  255. {
  256. printf("%8s ", "static");
  257. }
  258. /* get total number of hosts in the pool */
  259. lease = db->query(db, "SELECT COUNT(*) FROM addresses "
  260. "WHERE pool = ?", DB_UINT, id, DB_INT);
  261. if (lease)
  262. {
  263. lease->enumerate(lease, &size);
  264. lease->destroy(lease);
  265. }
  266. if (!size)
  267. { /* empty pool */
  268. printf("%6d %11s %11s ", 0, "n/a", "n/a");
  269. goto next_pool;
  270. }
  271. printf("%6d ", size);
  272. /* get number of online hosts */
  273. lease = db->query(db, "SELECT COUNT(*) FROM addresses "
  274. "WHERE pool = ? AND released = 0",
  275. DB_UINT, id, DB_INT);
  276. if (lease)
  277. {
  278. lease->enumerate(lease, &online);
  279. lease->destroy(lease);
  280. }
  281. printf("%5d (%2d%%) ", online, online*100/size);
  282. /* get number of online or valid leases */
  283. lease = db->query(db, "SELECT COUNT(*) FROM addresses "
  284. "WHERE addresses.pool = ? "
  285. "AND ((? AND acquired != 0) "
  286. " OR released = 0 OR released > ?) ",
  287. DB_UINT, id, DB_UINT, !timeout,
  288. DB_UINT, time(NULL) - timeout, DB_UINT);
  289. if (lease)
  290. {
  291. lease->enumerate(lease, &used);
  292. lease->destroy(lease);
  293. }
  294. printf("%5d (%2d%%) ", used, used*100/size);
  295. next_pool:
  296. printf("\n");
  297. DESTROY_IF(start);
  298. DESTROY_IF(end);
  299. }
  300. pool->destroy(pool);
  301. }
  302. if (!found)
  303. {
  304. printf("no pools found.\n");
  305. }
  306. }
  307. /**
  308. * ipsec pool --add - add a new pool
  309. */
  310. static void add(char *name, host_t *start, host_t *end, u_int timeout)
  311. {
  312. chunk_t start_addr, end_addr, cur_addr;
  313. u_int id, count;
  314. start_addr = start->get_address(start);
  315. end_addr = end->get_address(end);
  316. cur_addr = chunk_clonea(start_addr);
  317. count = get_pool_size(start_addr, end_addr);
  318. if (start_addr.len != end_addr.len ||
  319. memcmp(start_addr.ptr, end_addr.ptr, start_addr.len) > 0)
  320. {
  321. fprintf(stderr, "invalid start/end pair specified.\n");
  322. exit(EXIT_FAILURE);
  323. }
  324. id = create_pool(name, start_addr, end_addr, timeout);
  325. printf("allocating %d addresses... ", count);
  326. fflush(stdout);
  327. db->transaction(db, FALSE);
  328. while (TRUE)
  329. {
  330. db->execute(db, NULL,
  331. "INSERT INTO addresses (pool, address, identity, acquired, released) "
  332. "VALUES (?, ?, ?, ?, ?)",
  333. DB_UINT, id, DB_BLOB, cur_addr, DB_UINT, 0, DB_UINT, 0, DB_UINT, 1);
  334. if (chunk_equals(cur_addr, end_addr))
  335. {
  336. break;
  337. }
  338. chunk_increment(cur_addr);
  339. }
  340. db->commit(db);
  341. printf("done.\n");
  342. }
  343. static bool add_address(u_int pool_id, char *address_str, int *family)
  344. {
  345. host_t *address;
  346. int user_id = 0;
  347. char *pos_eq = strchr(address_str, '=');
  348. if (pos_eq != NULL)
  349. {
  350. identification_t *id = identification_create_from_string(pos_eq + 1);
  351. user_id = get_identity(id);
  352. id->destroy(id);
  353. if (user_id == 0)
  354. {
  355. return FALSE;
  356. }
  357. *pos_eq = '\0';
  358. }
  359. address = host_create_from_string(address_str, 0);
  360. if (address == NULL)
  361. {
  362. fprintf(stderr, "invalid address '%s'.\n", address_str);
  363. return FALSE;
  364. }
  365. if (family && *family != AF_UNSPEC &&
  366. *family != address->get_family(address))
  367. {
  368. fprintf(stderr, "invalid address family '%s'.\n", address_str);
  369. address->destroy(address);
  370. return FALSE;
  371. }
  372. if (db->execute(db, NULL,
  373. "INSERT INTO addresses "
  374. "(pool, address, identity, acquired, released) "
  375. "VALUES (?, ?, ?, ?, ?)",
  376. DB_UINT, pool_id, DB_BLOB, address->get_address(address),
  377. DB_UINT, user_id, DB_UINT, 0, DB_UINT, 1) != 1)
  378. {
  379. fprintf(stderr, "inserting address '%s' failed.\n", address_str);
  380. address->destroy(address);
  381. return FALSE;
  382. }
  383. if (family)
  384. {
  385. *family = address->get_family(address);
  386. }
  387. address->destroy(address);
  388. return TRUE;
  389. }
  390. static void add_addresses(char *pool, char *path, u_int timeout)
  391. {
  392. u_int pool_id, count = 0;
  393. int family = AF_UNSPEC;
  394. char address_str[512];
  395. host_t *addr;
  396. FILE *file;
  397. db->transaction(db, FALSE);
  398. addr = host_create_from_string("%any", 0);
  399. pool_id = create_pool(pool, addr->get_address(addr),
  400. addr->get_address(addr), timeout);
  401. addr->destroy(addr);
  402. file = (strcmp(path, "-") == 0 ? stdin : fopen(path, "r"));
  403. if (file == NULL)
  404. {
  405. fprintf(stderr, "opening '%s' failed: %s\n", path, strerror(errno));
  406. exit(-1);
  407. }
  408. printf("starting allocation... ");
  409. fflush(stdout);
  410. while (fgets(address_str, sizeof(address_str), file))
  411. {
  412. size_t addr_len = strlen(address_str);
  413. char *last_chr = address_str + addr_len - 1;
  414. if (*last_chr == '\n')
  415. {
  416. if (addr_len == 1)
  417. { /* end of input */
  418. break;
  419. }
  420. *last_chr = '\0';
  421. }
  422. if (add_address(pool_id, address_str, &family) == FALSE)
  423. {
  424. if (file != stdin)
  425. {
  426. fclose(file);
  427. }
  428. exit(EXIT_FAILURE);
  429. }
  430. ++count;
  431. }
  432. if (file != stdin)
  433. {
  434. fclose(file);
  435. }
  436. if (family == AF_INET6)
  437. { /* update address family if necessary */
  438. addr = host_create_from_string("%any6", 0);
  439. if (db->execute(db, NULL,
  440. "UPDATE pools SET start = ?, end = ? WHERE id = ?",
  441. DB_BLOB, addr->get_address(addr),
  442. DB_BLOB, addr->get_address(addr), DB_UINT, pool_id) <= 0)
  443. {
  444. addr->destroy(addr);
  445. fprintf(stderr, "updating pool address family failed.\n");
  446. exit(EXIT_FAILURE);
  447. }
  448. addr->destroy(addr);
  449. }
  450. db->commit(db);
  451. printf("%d addresses done.\n", count);
  452. }
  453. /**
  454. * ipsec pool --del - delete a pool
  455. */
  456. static void del(char *name)
  457. {
  458. enumerator_t *query;
  459. u_int id;
  460. bool found = FALSE;
  461. query = db->query(db, "SELECT id FROM pools WHERE name = ?",
  462. DB_TEXT, name, DB_UINT);
  463. if (!query)
  464. {
  465. fprintf(stderr, "deleting pool failed.\n");
  466. exit(EXIT_FAILURE);
  467. }
  468. while (query->enumerate(query, &id))
  469. {
  470. found = TRUE;
  471. if (db->execute(db, NULL,
  472. "DELETE FROM leases WHERE address IN ("
  473. " SELECT id FROM addresses WHERE pool = ?)", DB_UINT, id) < 0 ||
  474. db->execute(db, NULL,
  475. "DELETE FROM addresses WHERE pool = ?", DB_UINT, id) < 0 ||
  476. db->execute(db, NULL,
  477. "DELETE FROM pools WHERE id = ?", DB_UINT, id) < 0)
  478. {
  479. fprintf(stderr, "deleting pool failed.\n");
  480. query->destroy(query);
  481. exit(EXIT_FAILURE);
  482. }
  483. }
  484. query->destroy(query);
  485. if (!found)
  486. {
  487. fprintf(stderr, "pool '%s' not found.\n", name);
  488. exit(EXIT_FAILURE);
  489. }
  490. }
  491. /**
  492. * ipsec pool --resize - resize a pool
  493. */
  494. static void resize(char *name, host_t *end)
  495. {
  496. enumerator_t *query;
  497. chunk_t old_addr, new_addr, cur_addr;
  498. u_int id, count;
  499. host_t *old_end;
  500. new_addr = end->get_address(end);
  501. query = db->query(db, "SELECT id, end FROM pools WHERE name = ?",
  502. DB_TEXT, name, DB_UINT, DB_BLOB);
  503. if (!query || !query->enumerate(query, &id, &old_addr))
  504. {
  505. DESTROY_IF(query);
  506. fprintf(stderr, "resizing pool failed.\n");
  507. exit(EXIT_FAILURE);
  508. }
  509. if (old_addr.len != new_addr.len ||
  510. memcmp(new_addr.ptr, old_addr.ptr, old_addr.len) < 0)
  511. {
  512. fprintf(stderr, "shrinking of pools not supported.\n");
  513. query->destroy(query);
  514. exit(EXIT_FAILURE);
  515. }
  516. cur_addr = chunk_clonea(old_addr);
  517. count = get_pool_size(old_addr, new_addr) - 1;
  518. query->destroy(query);
  519. /* Check whether pool is resizable */
  520. old_end = host_create_from_chunk(AF_UNSPEC, old_addr, 0);
  521. if (old_end && old_end->is_anyaddr(old_end))
  522. {
  523. fprintf(stderr, "pool is not resizable.\n");
  524. old_end->destroy(old_end);
  525. exit(EXIT_FAILURE);
  526. }
  527. DESTROY_IF(old_end);
  528. db->transaction(db, FALSE);
  529. if (db->execute(db, NULL,
  530. "UPDATE pools SET end = ? WHERE name = ?",
  531. DB_BLOB, new_addr, DB_TEXT, name) <= 0)
  532. {
  533. fprintf(stderr, "pool '%s' not found.\n", name);
  534. exit(EXIT_FAILURE);
  535. }
  536. printf("allocating %d new addresses... ", count);
  537. fflush(stdout);
  538. while (count-- > 0)
  539. {
  540. chunk_increment(cur_addr);
  541. db->execute(db, NULL,
  542. "INSERT INTO addresses (pool, address, identity, acquired, released) "
  543. "VALUES (?, ?, ?, ?, ?)",
  544. DB_UINT, id, DB_BLOB, cur_addr, DB_UINT, 0, DB_UINT, 0, DB_UINT, 1);
  545. }
  546. db->commit(db);
  547. printf("done.\n");
  548. }
  549. /**
  550. * create the lease query using the filter string
  551. */
  552. static enumerator_t *create_lease_query(char *filter, array_t **to_free)
  553. {
  554. enumerator_t *query;
  555. chunk_t id_chunk = chunk_empty, addr_chunk = chunk_empty;
  556. id_type_t id_type = 0;
  557. u_int tstamp = 0;
  558. bool online = FALSE, valid = FALSE, expired = FALSE;
  559. char *value, *pos, *pool = NULL;
  560. enum {
  561. FIL_POOL = 0,
  562. FIL_ID,
  563. FIL_ADDR,
  564. FIL_TSTAMP,
  565. FIL_STATE,
  566. };
  567. char *const token[] = {
  568. [FIL_POOL] = "pool",
  569. [FIL_ID] = "id",
  570. [FIL_ADDR] = "addr",
  571. [FIL_TSTAMP] = "tstamp",
  572. [FIL_STATE] = "status",
  573. NULL
  574. };
  575. /* if the filter string contains a distinguished name as a ID, we replace
  576. * ", " by "/ " in order to not confuse the getsubopt parser */
  577. pos = filter;
  578. while ((pos = strchr(pos, ',')))
  579. {
  580. if (pos[1] == ' ')
  581. {
  582. pos[0] = '/';
  583. }
  584. pos++;
  585. }
  586. while (filter && *filter != '\0')
  587. {
  588. switch (getsubopt(&filter, token, &value))
  589. {
  590. case FIL_POOL:
  591. if (value)
  592. {
  593. pool = value;
  594. }
  595. break;
  596. case FIL_ID:
  597. if (value)
  598. {
  599. identification_t *id;
  600. id = identification_create_from_string(value);
  601. id_type = id->get_type(id);
  602. id_chunk = chunk_clone(id->get_encoding(id));
  603. array_insert_create(to_free, ARRAY_TAIL, id_chunk.ptr);
  604. id->destroy(id);
  605. }
  606. break;
  607. case FIL_ADDR:
  608. if (value)
  609. {
  610. host_t *addr;
  611. addr = host_create_from_string(value, 0);
  612. if (!addr)
  613. {
  614. fprintf(stderr, "invalid 'addr' in filter string.\n");
  615. exit(EXIT_FAILURE);
  616. }
  617. addr_chunk = chunk_clone(addr->get_address(addr));
  618. array_insert_create(to_free, ARRAY_TAIL, addr_chunk.ptr);
  619. addr->destroy(addr);
  620. }
  621. break;
  622. case FIL_TSTAMP:
  623. if (value)
  624. {
  625. tstamp = atoi(value);
  626. }
  627. if (tstamp == 0)
  628. {
  629. online = TRUE;
  630. }
  631. break;
  632. case FIL_STATE:
  633. if (value)
  634. {
  635. if (streq(value, "online"))
  636. {
  637. online = TRUE;
  638. }
  639. else if (streq(value, "valid"))
  640. {
  641. valid = TRUE;
  642. }
  643. else if (streq(value, "expired"))
  644. {
  645. expired = TRUE;
  646. }
  647. else
  648. {
  649. fprintf(stderr, "invalid 'state' in filter string.\n");
  650. exit(EXIT_FAILURE);
  651. }
  652. }
  653. break;
  654. default:
  655. fprintf(stderr, "invalid filter string.\n");
  656. exit(EXIT_FAILURE);
  657. }
  658. }
  659. query = db->query(db,
  660. "SELECT name, addresses.address, identities.type, "
  661. "identities.data, leases.acquired, leases.released, timeout "
  662. "FROM leases JOIN addresses ON leases.address = addresses.id "
  663. "JOIN pools ON addresses.pool = pools.id "
  664. "JOIN identities ON leases.identity = identities.id "
  665. "WHERE (? OR name = ?) "
  666. "AND (? OR (identities.type = ? AND identities.data = ?)) "
  667. "AND (? OR addresses.address = ?) "
  668. "AND (? OR (? >= leases.acquired AND (? <= leases.released))) "
  669. "AND (? OR leases.released > ? - timeout) "
  670. "AND (? OR leases.released < ? - timeout) "
  671. "AND ? "
  672. "UNION "
  673. "SELECT name, address, identities.type, identities.data, "
  674. "acquired, released, timeout FROM addresses "
  675. "JOIN pools ON addresses.pool = pools.id "
  676. "JOIN identities ON addresses.identity = identities.id "
  677. "WHERE ? AND released = 0 "
  678. "AND (? OR name = ?) "
  679. "AND (? OR (identities.type = ? AND identities.data = ?)) "
  680. "AND (? OR address = ?)",
  681. DB_INT, pool == NULL, DB_TEXT, pool,
  682. DB_INT, !id_chunk.ptr,
  683. DB_INT, id_type,
  684. DB_BLOB, id_chunk,
  685. DB_INT, !addr_chunk.ptr,
  686. DB_BLOB, addr_chunk,
  687. DB_INT, tstamp == 0, DB_UINT, tstamp, DB_UINT, tstamp,
  688. DB_INT, !valid, DB_INT, time(NULL),
  689. DB_INT, !expired, DB_INT, time(NULL),
  690. DB_INT, !online,
  691. /* union */
  692. DB_INT, !(valid || expired),
  693. DB_INT, pool == NULL, DB_TEXT, pool,
  694. DB_INT, !id_chunk.ptr,
  695. DB_INT, id_type,
  696. DB_BLOB, id_chunk,
  697. DB_INT, !addr_chunk.ptr,
  698. DB_BLOB, addr_chunk,
  699. /* res */
  700. DB_TEXT, DB_BLOB, DB_INT, DB_BLOB, DB_UINT, DB_UINT, DB_UINT);
  701. return query;
  702. }
  703. /**
  704. * ipsec pool --leases - show lease information of a pool
  705. */
  706. static void leases(char *filter, bool utc)
  707. {
  708. enumerator_t *query;
  709. array_t *to_free = NULL;
  710. chunk_t address_chunk, identity_chunk;
  711. int identity_type;
  712. char *name;
  713. u_int db_acquired, db_released, db_timeout;
  714. time_t acquired, released, timeout;
  715. host_t *address;
  716. identification_t *identity;
  717. bool found = FALSE;
  718. query = create_lease_query(filter, &to_free);
  719. if (!query)
  720. {
  721. fprintf(stderr, "querying leases failed.\n");
  722. exit(EXIT_FAILURE);
  723. }
  724. while (query->enumerate(query, &name, &address_chunk, &identity_type,
  725. &identity_chunk, &db_acquired, &db_released, &db_timeout))
  726. {
  727. if (!found)
  728. {
  729. int len = utc ? 25 : 21;
  730. found = TRUE;
  731. printf("%-8s %-15s %-7s %-*s %-*s %s\n",
  732. "name", "address", "status", len, "start", len, "end", "identity");
  733. }
  734. address = host_create_from_chunk(AF_UNSPEC, address_chunk, 0);
  735. identity = identification_create_from_encoding(identity_type, identity_chunk);
  736. /* u_int is not always equal to time_t */
  737. acquired = (time_t)db_acquired;
  738. released = (time_t)db_released;
  739. timeout = (time_t)db_timeout;
  740. printf("%-8s %-15H ", name, address);
  741. if (released == 0)
  742. {
  743. printf("%-7s ", "online");
  744. }
  745. else if (timeout == 0)
  746. {
  747. printf("%-7s ", "static");
  748. }
  749. else if (released >= time(NULL) - timeout)
  750. {
  751. printf("%-7s ", "valid");
  752. }
  753. else
  754. {
  755. printf("%-7s ", "expired");
  756. }
  757. printf(" %T ", &acquired, utc);
  758. if (released)
  759. {
  760. printf("%T ", &released, utc);
  761. }
  762. else
  763. {
  764. printf(" ");
  765. if (utc)
  766. {
  767. printf(" ");
  768. }
  769. }
  770. printf("%Y\n", identity);
  771. DESTROY_IF(address);
  772. identity->destroy(identity);
  773. }
  774. query->destroy(query);
  775. if (to_free)
  776. {
  777. array_destroy_function(to_free, (void*)free, NULL);
  778. }
  779. if (!found)
  780. {
  781. fprintf(stderr, "no matching leases found.\n");
  782. exit(EXIT_FAILURE);
  783. }
  784. }
  785. /**
  786. * ipsec pool --purge - delete expired leases
  787. */
  788. static void purge(char *name)
  789. {
  790. int purged = 0;
  791. purged = db->execute(db, NULL,
  792. "DELETE FROM leases WHERE address IN ("
  793. " SELECT id FROM addresses WHERE pool IN ("
  794. " SELECT id FROM pools WHERE name = ?))",
  795. DB_TEXT, name);
  796. if (purged < 0)
  797. {
  798. fprintf(stderr, "purging pool '%s' failed.\n", name);
  799. exit(EXIT_FAILURE);
  800. }
  801. fprintf(stderr, "purged %d leases in pool '%s'.\n", purged, name);
  802. }
  803. #define ARGV_SIZE 32
  804. static void argv_add(char **argv, int argc, char *value)
  805. {
  806. if (argc >= ARGV_SIZE)
  807. {
  808. fprintf(stderr, "too many arguments: %s\n", value);
  809. exit(EXIT_FAILURE);
  810. }
  811. argv[argc] = value;
  812. }
  813. /**
  814. * ipsec pool --batch - read commands from a file
  815. */
  816. static void batch(char *argv0, char *name)
  817. {
  818. char command[512];
  819. FILE *file = strncmp(name, "-", 1) == 0 ? stdin : fopen(name, "r");
  820. if (file == NULL)
  821. {
  822. fprintf(stderr, "opening '%s' failed: %s\n", name, strerror(errno));
  823. exit(EXIT_FAILURE);
  824. }
  825. db->transaction(db, FALSE);
  826. while (fgets(command, sizeof(command), file))
  827. {
  828. char *argv[ARGV_SIZE], *start;
  829. int i, argc = 0;
  830. size_t cmd_len = strlen(command);
  831. /* ignore empty lines */
  832. if (cmd_len == 1 && *(command + cmd_len - 1) == '\n')
  833. {
  834. continue;
  835. }
  836. /* parse command into argv */
  837. start = command;
  838. argv_add(argv, argc++, argv0);
  839. for (i = 0; i < cmd_len; ++i)
  840. {
  841. if (command[i] == ' ' || command[i] == '\n')
  842. {
  843. if (command + i == start)
  844. {
  845. /* ignore leading whitespace */
  846. ++start;
  847. continue;
  848. }
  849. command[i] = '\0';
  850. argv_add(argv, argc++, start);
  851. start = command + i + 1;
  852. }
  853. }
  854. if (strlen(start) > 0)
  855. {
  856. argv_add(argv, argc++, start);
  857. }
  858. argv_add(argv, argc, NULL);
  859. do_args(argc, argv);
  860. }
  861. db->commit(db);
  862. if (file != stdin)
  863. {
  864. fclose(file);
  865. }
  866. }
  867. /**
  868. * atexit handler to close db on shutdown
  869. */
  870. static void cleanup(void)
  871. {
  872. db->destroy(db);
  873. DESTROY_IF(start);
  874. DESTROY_IF(end);
  875. }
  876. static void do_args(int argc, char *argv[])
  877. {
  878. char *name = "", *value = "", *filter = "";
  879. char *pool = NULL, *identity = NULL, *addresses = NULL;
  880. value_type_t value_type = VALUE_NONE;
  881. time_t timeout = 0;
  882. bool utc = FALSE, hexout = FALSE;
  883. enum {
  884. OP_UNDEF,
  885. OP_USAGE,
  886. OP_STATUS,
  887. OP_STATUS_ATTR,
  888. OP_ADD,
  889. OP_ADD_ATTR,
  890. OP_DEL,
  891. OP_DEL_ATTR,
  892. OP_SHOW_ATTR,
  893. OP_RESIZE,
  894. OP_LEASES,
  895. OP_PURGE,
  896. OP_BATCH
  897. } operation = OP_UNDEF;
  898. /* reinit getopt state */
  899. optind = 0;
  900. while (TRUE)
  901. {
  902. int c;
  903. struct option long_opts[] = {
  904. { "help", no_argument, NULL, 'h' },
  905. { "utc", no_argument, NULL, 'u' },
  906. { "status", no_argument, NULL, 'w' },
  907. { "add", required_argument, NULL, 'a' },
  908. { "replace", required_argument, NULL, 'c' },
  909. { "del", required_argument, NULL, 'd' },
  910. { "resize", required_argument, NULL, 'r' },
  911. { "leases", no_argument, NULL, 'l' },
  912. { "purge", required_argument, NULL, 'p' },
  913. { "statusattr", no_argument, NULL, '1' },
  914. { "addattr", required_argument, NULL, '2' },
  915. { "delattr", required_argument, NULL, '3' },
  916. { "showattr", no_argument, NULL, '4' },
  917. { "batch", required_argument, NULL, 'b' },
  918. { "start", required_argument, NULL, 's' },
  919. { "end", required_argument, NULL, 'e' },
  920. { "addresses", required_argument, NULL, 'y' },
  921. { "timeout", required_argument, NULL, 't' },
  922. { "filter", required_argument, NULL, 'f' },
  923. { "addr", required_argument, NULL, 'v' },
  924. { "mask", required_argument, NULL, 'v' },
  925. { "server", required_argument, NULL, 'v' },
  926. { "subnet", required_argument, NULL, 'n' },
  927. { "string", required_argument, NULL, 'g' },
  928. { "hex", required_argument, NULL, 'x' },
  929. { "hexout", no_argument, NULL, '5' },
  930. { "pool", required_argument, NULL, '6' },
  931. { "identity", required_argument, NULL, '7' },
  932. { 0,0,0,0 }
  933. };
  934. c = getopt_long(argc, argv, "", long_opts, NULL);
  935. switch (c)
  936. {
  937. case EOF:
  938. break;
  939. case 'h':
  940. operation = OP_USAGE;
  941. break;
  942. case 'w':
  943. operation = OP_STATUS;
  944. break;
  945. case '1':
  946. operation = OP_STATUS_ATTR;
  947. break;
  948. case 'u':
  949. utc = TRUE;
  950. continue;
  951. case 'c':
  952. replace_pool = TRUE;
  953. /* fallthrough */
  954. case 'a':
  955. name = optarg;
  956. operation = is_attribute(name) ? OP_ADD_ATTR : OP_ADD;
  957. if (replace_pool && operation == OP_ADD_ATTR)
  958. {
  959. fprintf(stderr, "invalid pool name: "
  960. "reserved for '%s' attribute.\n", optarg);
  961. usage();
  962. exit(EXIT_FAILURE);
  963. }
  964. continue;
  965. case '2':
  966. name = optarg;
  967. operation = OP_ADD_ATTR;
  968. continue;
  969. case 'd':
  970. name = optarg;
  971. operation = is_attribute(name) ? OP_DEL_ATTR : OP_DEL;
  972. continue;
  973. case '3':
  974. name = optarg;
  975. operation = OP_DEL_ATTR;
  976. continue;
  977. case '4':
  978. operation = OP_SHOW_ATTR;
  979. continue;
  980. case 'r':
  981. name = optarg;
  982. operation = OP_RESIZE;
  983. continue;
  984. case 'l':
  985. operation = OP_LEASES;
  986. continue;
  987. case 'p':
  988. name = optarg;
  989. operation = OP_PURGE;
  990. continue;
  991. case 'b':
  992. name = optarg;
  993. if (operation == OP_BATCH)
  994. {
  995. fprintf(stderr, "--batch commands can not be nested\n");
  996. exit(EXIT_FAILURE);
  997. }
  998. operation = OP_BATCH;
  999. continue;
  1000. case 's':
  1001. DESTROY_IF(start);
  1002. start = host_create_from_string(optarg, 0);
  1003. if (start == NULL)
  1004. {
  1005. fprintf(stderr, "invalid start address: '%s'.\n", optarg);
  1006. usage();
  1007. exit(EXIT_FAILURE);
  1008. }
  1009. continue;
  1010. case 'e':
  1011. DESTROY_IF(end);
  1012. end = host_create_from_string(optarg, 0);
  1013. if (end == NULL)
  1014. {
  1015. fprintf(stderr, "invalid end address: '%s'.\n", optarg);
  1016. usage();
  1017. exit(EXIT_FAILURE);
  1018. }
  1019. continue;
  1020. case 't':
  1021. if (!timespan_from_string(optarg, "h", &timeout))
  1022. {
  1023. fprintf(stderr, "invalid timeout '%s'.\n", optarg);
  1024. usage();
  1025. exit(EXIT_FAILURE);
  1026. }
  1027. continue;
  1028. case 'f':
  1029. filter = optarg;
  1030. continue;
  1031. case 'y':
  1032. addresses = optarg;
  1033. continue;
  1034. case 'g':
  1035. value_type = VALUE_STRING;
  1036. value = optarg;
  1037. continue;
  1038. case 'n':
  1039. value_type = VALUE_SUBNET;
  1040. value = optarg;
  1041. continue;
  1042. case 'v':
  1043. value_type = VALUE_ADDR;
  1044. value = optarg;
  1045. continue;
  1046. case 'x':
  1047. value_type = VALUE_HEX;
  1048. value = optarg;
  1049. continue;
  1050. case '5':
  1051. hexout = TRUE;
  1052. continue;
  1053. case '6':
  1054. pool = optarg;
  1055. continue;
  1056. case '7':
  1057. identity = optarg;
  1058. continue;
  1059. default:
  1060. usage();
  1061. exit(EXIT_FAILURE);
  1062. }
  1063. break;
  1064. }
  1065. switch (operation)
  1066. {
  1067. case OP_USAGE:
  1068. usage();
  1069. break;
  1070. case OP_STATUS:
  1071. status();
  1072. break;
  1073. case OP_STATUS_ATTR:
  1074. status_attr(hexout);
  1075. break;
  1076. case OP_ADD:
  1077. if (addresses != NULL)
  1078. {
  1079. add_addresses(name, addresses, timeout);
  1080. }
  1081. else if (start != NULL && end != NULL)
  1082. {
  1083. add(name, start, end, timeout);
  1084. }
  1085. else
  1086. {
  1087. fprintf(stderr, "missing arguments.\n");
  1088. usage();
  1089. exit(EXIT_FAILURE);
  1090. }
  1091. break;
  1092. case OP_ADD_ATTR:
  1093. if (value_type == VALUE_NONE)
  1094. {
  1095. fprintf(stderr, "missing arguments.\n");
  1096. usage();
  1097. exit(EXIT_FAILURE);
  1098. }
  1099. if (identity && !pool)
  1100. {
  1101. fprintf(stderr, "--identity option can't be used without --pool.\n");
  1102. usage();
  1103. exit(EXIT_FAILURE);
  1104. }
  1105. add_attr(name, pool, identity, value, value_type);
  1106. break;
  1107. case OP_DEL:
  1108. del(name);
  1109. break;
  1110. case OP_DEL_ATTR:
  1111. if (identity && !pool)
  1112. {
  1113. fprintf(stderr, "--identity option can't be used without --pool.\n");
  1114. usage();
  1115. exit(EXIT_FAILURE);
  1116. }
  1117. del_attr(name, pool, identity, value, value_type);
  1118. break;
  1119. case OP_SHOW_ATTR:
  1120. show_attr();
  1121. break;
  1122. case OP_RESIZE:
  1123. if (end == NULL)
  1124. {
  1125. fprintf(stderr, "missing arguments.\n");
  1126. usage();
  1127. exit(EXIT_FAILURE);
  1128. }
  1129. resize(name, end);
  1130. break;
  1131. case OP_LEASES:
  1132. leases(filter, utc);
  1133. break;
  1134. case OP_PURGE:
  1135. purge(name);
  1136. break;
  1137. case OP_BATCH:
  1138. if (name == NULL)
  1139. {
  1140. fprintf(stderr, "missing arguments.\n");
  1141. usage();
  1142. exit(EXIT_FAILURE);
  1143. }
  1144. batch(argv[0], name);
  1145. break;
  1146. default:
  1147. usage();
  1148. exit(EXIT_FAILURE);
  1149. }
  1150. }
  1151. int main(int argc, char *argv[])
  1152. {
  1153. char *uri;
  1154. atexit(library_deinit);
  1155. /* initialize library */
  1156. if (!library_init(NULL, "pool"))
  1157. {
  1158. exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
  1159. }
  1160. if (lib->integrity &&
  1161. !lib->integrity->check_file(lib->integrity, "pool", argv[0]))
  1162. {
  1163. fprintf(stderr, "integrity check of pool failed\n");
  1164. exit(SS_RC_DAEMON_INTEGRITY);
  1165. }
  1166. if (!lib->plugins->load(lib->plugins,
  1167. lib->settings->get_str(lib->settings, "pool.load", PLUGINS)))
  1168. {
  1169. exit(SS_RC_INITIALIZATION_FAILED);
  1170. }
  1171. /* TODO: make database URI or setting key configurable via command line */
  1172. uri = lib->settings->get_str(lib->settings,
  1173. "pool.database",
  1174. lib->settings->get_str(lib->settings,
  1175. "charon.plugins.attr-sql.database",
  1176. lib->settings->get_str(lib->settings,
  1177. "libhydra.plugins.attr-sql.database", NULL)));
  1178. if (!uri)
  1179. {
  1180. fprintf(stderr, "database URI pool.database not set.\n");
  1181. exit(SS_RC_INITIALIZATION_FAILED);
  1182. }
  1183. db = lib->db->create(lib->db, uri);
  1184. if (!db)
  1185. {
  1186. fprintf(stderr, "opening database failed.\n");
  1187. exit(SS_RC_INITIALIZATION_FAILED);
  1188. }
  1189. atexit(cleanup);
  1190. do_args(argc, argv);
  1191. exit(EXIT_SUCCESS);
  1192. }