db.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief ASTdb Management
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \note DB3 is licensed under Sleepycat Public License and is thus incompatible
  25. * with GPL. To avoid having to make another exception (and complicate
  26. * licensing even further) we elect to use DB1 which is BSD licensed
  27. */
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/_private.h"
  31. #include "asterisk/paths.h" /* use ast_config_AST_DB */
  32. #include <sys/time.h>
  33. #include <signal.h>
  34. #include <dirent.h>
  35. #include "asterisk/channel.h"
  36. #include "asterisk/file.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/dsp.h"
  39. #include "asterisk/astdb.h"
  40. #include "asterisk/cli.h"
  41. #include "asterisk/utils.h"
  42. #include "asterisk/lock.h"
  43. #include "asterisk/manager.h"
  44. #include "db1-ast/include/db.h"
  45. static DB *astdb;
  46. AST_MUTEX_DEFINE_STATIC(dblock);
  47. static int dbinit(void)
  48. {
  49. if (!astdb && !(astdb = dbopen(ast_config_AST_DB, O_CREAT | O_RDWR, AST_FILE_MODE, DB_BTREE, NULL))) {
  50. ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB, strerror(errno));
  51. return -1;
  52. }
  53. return 0;
  54. }
  55. static inline int keymatch(const char *key, const char *prefix)
  56. {
  57. int preflen = strlen(prefix);
  58. if (!preflen)
  59. return 1;
  60. if (!strcasecmp(key, prefix))
  61. return 1;
  62. if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
  63. if (key[preflen] == '/')
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. static inline int subkeymatch(const char *key, const char *suffix)
  69. {
  70. int suffixlen = strlen(suffix);
  71. if (suffixlen) {
  72. const char *subkey = key + strlen(key) - suffixlen;
  73. if (subkey < key)
  74. return 0;
  75. if (!strcasecmp(subkey, suffix))
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. int ast_db_deltree(const char *family, const char *keytree)
  81. {
  82. char prefix[256];
  83. DBT key, data;
  84. char *keys;
  85. int res;
  86. int pass;
  87. int counter = 0;
  88. if (family) {
  89. if (keytree) {
  90. snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
  91. } else {
  92. snprintf(prefix, sizeof(prefix), "/%s", family);
  93. }
  94. } else if (keytree) {
  95. return -1;
  96. } else {
  97. prefix[0] = '\0';
  98. }
  99. ast_mutex_lock(&dblock);
  100. if (dbinit()) {
  101. ast_mutex_unlock(&dblock);
  102. return -1;
  103. }
  104. memset(&key, 0, sizeof(key));
  105. memset(&data, 0, sizeof(data));
  106. pass = 0;
  107. while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
  108. if (key.size) {
  109. keys = key.data;
  110. keys[key.size - 1] = '\0';
  111. } else {
  112. keys = "<bad key>";
  113. }
  114. if (keymatch(keys, prefix)) {
  115. astdb->del(astdb, &key, 0);
  116. counter++;
  117. }
  118. }
  119. astdb->sync(astdb, 0);
  120. ast_mutex_unlock(&dblock);
  121. return counter;
  122. }
  123. int ast_db_put(const char *family, const char *keys, const char *value)
  124. {
  125. char fullkey[256];
  126. DBT key, data;
  127. int res, fullkeylen;
  128. ast_mutex_lock(&dblock);
  129. if (dbinit()) {
  130. ast_mutex_unlock(&dblock);
  131. return -1;
  132. }
  133. fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
  134. memset(&key, 0, sizeof(key));
  135. memset(&data, 0, sizeof(data));
  136. key.data = fullkey;
  137. key.size = fullkeylen + 1;
  138. data.data = (char *) value;
  139. data.size = strlen(value) + 1;
  140. res = astdb->put(astdb, &key, &data, 0);
  141. astdb->sync(astdb, 0);
  142. ast_mutex_unlock(&dblock);
  143. if (res)
  144. ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
  145. return res;
  146. }
  147. int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
  148. {
  149. char fullkey[256] = "";
  150. DBT key, data;
  151. int res, fullkeylen;
  152. ast_mutex_lock(&dblock);
  153. if (dbinit()) {
  154. ast_mutex_unlock(&dblock);
  155. return -1;
  156. }
  157. fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
  158. memset(&key, 0, sizeof(key));
  159. memset(&data, 0, sizeof(data));
  160. memset(value, 0, valuelen);
  161. key.data = fullkey;
  162. key.size = fullkeylen + 1;
  163. res = astdb->get(astdb, &key, &data, 0);
  164. /* Be sure to NULL terminate our data either way */
  165. if (res) {
  166. ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
  167. } else {
  168. #if 0
  169. printf("Got value of size %d\n", data.size);
  170. #endif
  171. if (data.size) {
  172. ((char *)data.data)[data.size - 1] = '\0';
  173. /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
  174. ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
  175. } else {
  176. ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
  177. }
  178. }
  179. /* Data is not fully isolated for concurrency, so the lock must be extended
  180. * to after the copy to the output buffer. */
  181. ast_mutex_unlock(&dblock);
  182. return res;
  183. }
  184. int ast_db_del(const char *family, const char *keys)
  185. {
  186. char fullkey[256];
  187. DBT key;
  188. int res, fullkeylen;
  189. ast_mutex_lock(&dblock);
  190. if (dbinit()) {
  191. ast_mutex_unlock(&dblock);
  192. return -1;
  193. }
  194. fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
  195. memset(&key, 0, sizeof(key));
  196. key.data = fullkey;
  197. key.size = fullkeylen + 1;
  198. res = astdb->del(astdb, &key, 0);
  199. astdb->sync(astdb, 0);
  200. ast_mutex_unlock(&dblock);
  201. if (res) {
  202. ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
  203. }
  204. return res;
  205. }
  206. static char *handle_cli_database_put(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  207. {
  208. int res;
  209. switch (cmd) {
  210. case CLI_INIT:
  211. e->command = "database put";
  212. e->usage =
  213. "Usage: database put <family> <key> <value>\n"
  214. " Adds or updates an entry in the Asterisk database for\n"
  215. " a given family, key, and value.\n";
  216. return NULL;
  217. case CLI_GENERATE:
  218. return NULL;
  219. }
  220. if (a->argc != 5)
  221. return CLI_SHOWUSAGE;
  222. res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
  223. if (res) {
  224. ast_cli(a->fd, "Failed to update entry\n");
  225. } else {
  226. ast_cli(a->fd, "Updated database successfully\n");
  227. }
  228. return CLI_SUCCESS;
  229. }
  230. static char *handle_cli_database_get(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  231. {
  232. int res;
  233. char tmp[256];
  234. switch (cmd) {
  235. case CLI_INIT:
  236. e->command = "database get";
  237. e->usage =
  238. "Usage: database get <family> <key>\n"
  239. " Retrieves an entry in the Asterisk database for a given\n"
  240. " family and key.\n";
  241. return NULL;
  242. case CLI_GENERATE:
  243. return NULL;
  244. }
  245. if (a->argc != 4)
  246. return CLI_SHOWUSAGE;
  247. res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
  248. if (res) {
  249. ast_cli(a->fd, "Database entry not found.\n");
  250. } else {
  251. ast_cli(a->fd, "Value: %s\n", tmp);
  252. }
  253. return CLI_SUCCESS;
  254. }
  255. static char *handle_cli_database_del(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  256. {
  257. int res;
  258. switch (cmd) {
  259. case CLI_INIT:
  260. e->command = "database del";
  261. e->usage =
  262. "Usage: database del <family> <key>\n"
  263. " Deletes an entry in the Asterisk database for a given\n"
  264. " family and key.\n";
  265. return NULL;
  266. case CLI_GENERATE:
  267. return NULL;
  268. }
  269. if (a->argc != 4)
  270. return CLI_SHOWUSAGE;
  271. res = ast_db_del(a->argv[2], a->argv[3]);
  272. if (res) {
  273. ast_cli(a->fd, "Database entry does not exist.\n");
  274. } else {
  275. ast_cli(a->fd, "Database entry removed.\n");
  276. }
  277. return CLI_SUCCESS;
  278. }
  279. static char *handle_cli_database_deltree(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  280. {
  281. int res;
  282. switch (cmd) {
  283. case CLI_INIT:
  284. e->command = "database deltree";
  285. e->usage =
  286. "Usage: database deltree <family> [keytree]\n"
  287. " Deletes a family or specific keytree within a family\n"
  288. " in the Asterisk database.\n";
  289. return NULL;
  290. case CLI_GENERATE:
  291. return NULL;
  292. }
  293. if ((a->argc < 3) || (a->argc > 4))
  294. return CLI_SHOWUSAGE;
  295. if (a->argc == 4) {
  296. res = ast_db_deltree(a->argv[2], a->argv[3]);
  297. } else {
  298. res = ast_db_deltree(a->argv[2], NULL);
  299. }
  300. if (res < 0) {
  301. ast_cli(a->fd, "Database entries do not exist.\n");
  302. } else {
  303. ast_cli(a->fd, "%d database entries removed.\n",res);
  304. }
  305. return CLI_SUCCESS;
  306. }
  307. static char *handle_cli_database_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  308. {
  309. char prefix[256];
  310. DBT key, data;
  311. char *keys, *values;
  312. int res;
  313. int pass;
  314. int counter = 0;
  315. switch (cmd) {
  316. case CLI_INIT:
  317. e->command = "database show";
  318. e->usage =
  319. "Usage: database show [family [keytree]]\n"
  320. " Shows Asterisk database contents, optionally restricted\n"
  321. " to a given family, or family and keytree.\n";
  322. return NULL;
  323. case CLI_GENERATE:
  324. return NULL;
  325. }
  326. if (a->argc == 4) {
  327. /* Family and key tree */
  328. snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
  329. } else if (a->argc == 3) {
  330. /* Family only */
  331. snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
  332. } else if (a->argc == 2) {
  333. /* Neither */
  334. prefix[0] = '\0';
  335. } else {
  336. return CLI_SHOWUSAGE;
  337. }
  338. ast_mutex_lock(&dblock);
  339. if (dbinit()) {
  340. ast_mutex_unlock(&dblock);
  341. ast_cli(a->fd, "Database unavailable\n");
  342. return CLI_SUCCESS;
  343. }
  344. memset(&key, 0, sizeof(key));
  345. memset(&data, 0, sizeof(data));
  346. pass = 0;
  347. while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
  348. if (key.size) {
  349. keys = key.data;
  350. keys[key.size - 1] = '\0';
  351. } else {
  352. keys = "<bad key>";
  353. }
  354. if (data.size) {
  355. values = data.data;
  356. values[data.size - 1]='\0';
  357. } else {
  358. values = "<bad value>";
  359. }
  360. if (keymatch(keys, prefix)) {
  361. ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
  362. counter++;
  363. }
  364. }
  365. ast_mutex_unlock(&dblock);
  366. ast_cli(a->fd, "%d results found.\n", counter);
  367. return CLI_SUCCESS;
  368. }
  369. static char *handle_cli_database_showkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  370. {
  371. char suffix[256];
  372. DBT key, data;
  373. char *keys, *values;
  374. int res;
  375. int pass;
  376. int counter = 0;
  377. switch (cmd) {
  378. case CLI_INIT:
  379. e->command = "database showkey";
  380. e->usage =
  381. "Usage: database showkey <keytree>\n"
  382. " Shows Asterisk database contents, restricted to a given key.\n";
  383. return NULL;
  384. case CLI_GENERATE:
  385. return NULL;
  386. }
  387. if (a->argc == 3) {
  388. /* Key only */
  389. snprintf(suffix, sizeof(suffix), "/%s", a->argv[2]);
  390. } else {
  391. return CLI_SHOWUSAGE;
  392. }
  393. ast_mutex_lock(&dblock);
  394. if (dbinit()) {
  395. ast_mutex_unlock(&dblock);
  396. ast_cli(a->fd, "Database unavailable\n");
  397. return CLI_SUCCESS;
  398. }
  399. memset(&key, 0, sizeof(key));
  400. memset(&data, 0, sizeof(data));
  401. pass = 0;
  402. while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
  403. if (key.size) {
  404. keys = key.data;
  405. keys[key.size - 1] = '\0';
  406. } else {
  407. keys = "<bad key>";
  408. }
  409. if (data.size) {
  410. values = data.data;
  411. values[data.size - 1]='\0';
  412. } else {
  413. values = "<bad value>";
  414. }
  415. if (subkeymatch(keys, suffix)) {
  416. ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
  417. counter++;
  418. }
  419. }
  420. ast_mutex_unlock(&dblock);
  421. ast_cli(a->fd, "%d results found.\n", counter);
  422. return CLI_SUCCESS;
  423. }
  424. struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
  425. {
  426. char prefix[256];
  427. DBT key, data;
  428. char *keys, *values;
  429. int values_len;
  430. int res;
  431. int pass;
  432. struct ast_db_entry *last = NULL;
  433. struct ast_db_entry *cur, *ret=NULL;
  434. if (!ast_strlen_zero(family)) {
  435. if (!ast_strlen_zero(keytree)) {
  436. /* Family and key tree */
  437. snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
  438. } else {
  439. /* Family only */
  440. snprintf(prefix, sizeof(prefix), "/%s", family);
  441. }
  442. } else {
  443. prefix[0] = '\0';
  444. }
  445. ast_mutex_lock(&dblock);
  446. if (dbinit()) {
  447. ast_mutex_unlock(&dblock);
  448. ast_log(LOG_WARNING, "Database unavailable\n");
  449. return NULL;
  450. }
  451. memset(&key, 0, sizeof(key));
  452. memset(&data, 0, sizeof(data));
  453. pass = 0;
  454. while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
  455. if (key.size) {
  456. keys = key.data;
  457. keys[key.size - 1] = '\0';
  458. } else {
  459. keys = "<bad key>";
  460. }
  461. if (data.size) {
  462. values = data.data;
  463. values[data.size - 1] = '\0';
  464. } else {
  465. values = "<bad value>";
  466. }
  467. values_len = strlen(values) + 1;
  468. if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
  469. cur->next = NULL;
  470. cur->key = cur->data + values_len;
  471. strcpy(cur->data, values);
  472. strcpy(cur->key, keys);
  473. if (last) {
  474. last->next = cur;
  475. } else {
  476. ret = cur;
  477. }
  478. last = cur;
  479. }
  480. }
  481. ast_mutex_unlock(&dblock);
  482. return ret;
  483. }
  484. void ast_db_freetree(struct ast_db_entry *dbe)
  485. {
  486. struct ast_db_entry *last;
  487. while (dbe) {
  488. last = dbe;
  489. dbe = dbe->next;
  490. ast_free(last);
  491. }
  492. }
  493. struct ast_cli_entry cli_database[] = {
  494. AST_CLI_DEFINE(handle_cli_database_show, "Shows database contents"),
  495. AST_CLI_DEFINE(handle_cli_database_showkey, "Shows database contents"),
  496. AST_CLI_DEFINE(handle_cli_database_get, "Gets database value"),
  497. AST_CLI_DEFINE(handle_cli_database_put, "Adds/updates database value"),
  498. AST_CLI_DEFINE(handle_cli_database_del, "Removes database key/value"),
  499. AST_CLI_DEFINE(handle_cli_database_deltree, "Removes database keytree/values")
  500. };
  501. static int manager_dbput(struct mansession *s, const struct message *m)
  502. {
  503. const char *family = astman_get_header(m, "Family");
  504. const char *key = astman_get_header(m, "Key");
  505. const char *val = astman_get_header(m, "Val");
  506. int res;
  507. if (ast_strlen_zero(family)) {
  508. astman_send_error(s, m, "No family specified");
  509. return 0;
  510. }
  511. if (ast_strlen_zero(key)) {
  512. astman_send_error(s, m, "No key specified");
  513. return 0;
  514. }
  515. res = ast_db_put(family, key, S_OR(val, ""));
  516. if (res) {
  517. astman_send_error(s, m, "Failed to update entry");
  518. } else {
  519. astman_send_ack(s, m, "Updated database successfully");
  520. }
  521. return 0;
  522. }
  523. static int manager_dbget(struct mansession *s, const struct message *m)
  524. {
  525. const char *id = astman_get_header(m,"ActionID");
  526. char idText[256] = "";
  527. const char *family = astman_get_header(m, "Family");
  528. const char *key = astman_get_header(m, "Key");
  529. char tmp[256];
  530. int res;
  531. if (ast_strlen_zero(family)) {
  532. astman_send_error(s, m, "No family specified.");
  533. return 0;
  534. }
  535. if (ast_strlen_zero(key)) {
  536. astman_send_error(s, m, "No key specified.");
  537. return 0;
  538. }
  539. if (!ast_strlen_zero(id))
  540. snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
  541. res = ast_db_get(family, key, tmp, sizeof(tmp));
  542. if (res) {
  543. astman_send_error(s, m, "Database entry not found");
  544. } else {
  545. astman_send_ack(s, m, "Result will follow");
  546. astman_append(s, "Event: DBGetResponse\r\n"
  547. "Family: %s\r\n"
  548. "Key: %s\r\n"
  549. "Val: %s\r\n"
  550. "%s"
  551. "\r\n",
  552. family, key, tmp, idText);
  553. }
  554. return 0;
  555. }
  556. static int manager_dbdel(struct mansession *s, const struct message *m)
  557. {
  558. const char *family = astman_get_header(m, "Family");
  559. const char *key = astman_get_header(m, "Key");
  560. int res;
  561. if (ast_strlen_zero(family)) {
  562. astman_send_error(s, m, "No family specified.");
  563. return 0;
  564. }
  565. if (ast_strlen_zero(key)) {
  566. astman_send_error(s, m, "No key specified.");
  567. return 0;
  568. }
  569. res = ast_db_del(family, key);
  570. if (res)
  571. astman_send_error(s, m, "Database entry not found");
  572. else
  573. astman_send_ack(s, m, "Key deleted successfully");
  574. return 0;
  575. }
  576. static int manager_dbdeltree(struct mansession *s, const struct message *m)
  577. {
  578. const char *family = astman_get_header(m, "Family");
  579. const char *key = astman_get_header(m, "Key");
  580. int res;
  581. if (ast_strlen_zero(family)) {
  582. astman_send_error(s, m, "No family specified.");
  583. return 0;
  584. }
  585. if (!ast_strlen_zero(key))
  586. res = ast_db_deltree(family, key);
  587. else
  588. res = ast_db_deltree(family, NULL);
  589. if (res < 0)
  590. astman_send_error(s, m, "Database entry not found");
  591. else
  592. astman_send_ack(s, m, "Key tree deleted successfully");
  593. return 0;
  594. }
  595. int astdb_init(void)
  596. {
  597. dbinit();
  598. ast_cli_register_multiple(cli_database, sizeof(cli_database) / sizeof(struct ast_cli_entry));
  599. ast_manager_register("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget, "Get DB Entry");
  600. ast_manager_register("DBPut", EVENT_FLAG_SYSTEM, manager_dbput, "Put DB Entry");
  601. ast_manager_register("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel, "Delete DB Entry");
  602. ast_manager_register("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree, "Delete DB Tree");
  603. return 0;
  604. }