db.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Channel Management
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. /* DB3 is licensed under Sleepycat Public License and is thus incompatible
  14. with GPL. To avoid having to make another exception (and complicate
  15. licensing even further) we elect to use DB1 which is BSD licensed */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/time.h>
  20. #include <signal.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <dirent.h>
  24. #include <asterisk/channel.h>
  25. #include <asterisk/file.h>
  26. #include <asterisk/app.h>
  27. #include <asterisk/dsp.h>
  28. #include <asterisk/logger.h>
  29. #include <asterisk/options.h>
  30. #include <asterisk/astdb.h>
  31. #include <asterisk/cli.h>
  32. #include <asterisk/utils.h>
  33. #include <asterisk/lock.h>
  34. #include "db1-ast/include/db.h"
  35. #include "asterisk.h"
  36. #include "astconf.h"
  37. static DB *astdb;
  38. AST_MUTEX_DEFINE_STATIC(dblock);
  39. static int dbinit(void)
  40. {
  41. if (!astdb) {
  42. if (!(astdb = dbopen((char *)ast_config_AST_DB, O_CREAT | O_RDWR, 0664, DB_BTREE, NULL))) {
  43. ast_log(LOG_WARNING, "Unable to open Asterisk database\n");
  44. }
  45. }
  46. if (astdb)
  47. return 0;
  48. return -1;
  49. }
  50. static inline int keymatch(const char *key, const char *prefix)
  51. {
  52. int preflen = strlen(prefix);
  53. if (!preflen)
  54. return 1;
  55. if (!strcasecmp(key, prefix))
  56. return 1;
  57. if ((strlen(key) > preflen) &&
  58. !strncasecmp(key, prefix, preflen)) {
  59. if (key[preflen] == '/')
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. int ast_db_deltree(const char *family, const char *keytree)
  65. {
  66. char prefix[256];
  67. DBT key, data;
  68. char *keys;
  69. int res;
  70. int pass;
  71. if (family) {
  72. if (keytree)
  73. snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
  74. else
  75. snprintf(prefix, sizeof(prefix), "/%s", family);
  76. } else if (keytree)
  77. return -1;
  78. else
  79. prefix[0] = '\0';
  80. ast_mutex_lock(&dblock);
  81. if (dbinit())
  82. return -1;
  83. memset(&key, 0, sizeof(key));
  84. memset(&data, 0, sizeof(data));
  85. pass = 0;
  86. while(!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
  87. if (key.size) {
  88. keys = key.data;
  89. keys[key.size - 1] = '\0';
  90. } else
  91. keys = "<bad key>";
  92. if (keymatch(keys, prefix)) {
  93. astdb->del(astdb, &key, 0);
  94. }
  95. }
  96. astdb->sync(astdb, 0);
  97. ast_mutex_unlock(&dblock);
  98. return 0;
  99. }
  100. int ast_db_put(const char *family, const char *keys, char *value)
  101. {
  102. char fullkey[256];
  103. DBT key, data;
  104. int res, fullkeylen;
  105. ast_mutex_lock(&dblock);
  106. if (dbinit()) {
  107. ast_mutex_unlock(&dblock);
  108. return -1;
  109. }
  110. fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
  111. memset(&key, 0, sizeof(key));
  112. memset(&data, 0, sizeof(data));
  113. key.data = fullkey;
  114. key.size = fullkeylen + 1;
  115. data.data = value;
  116. data.size = strlen(value) + 1;
  117. res = astdb->put(astdb, &key, &data, 0);
  118. astdb->sync(astdb, 0);
  119. ast_mutex_unlock(&dblock);
  120. if (res)
  121. ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
  122. return res;
  123. }
  124. int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
  125. {
  126. char fullkey[256]="";
  127. DBT key, data;
  128. int res, fullkeylen;
  129. ast_mutex_lock(&dblock);
  130. if (dbinit()) {
  131. ast_mutex_unlock(&dblock);
  132. return -1;
  133. }
  134. fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
  135. memset(&key, 0, sizeof(key));
  136. memset(&data, 0, sizeof(data));
  137. memset(value, 0, valuelen);
  138. key.data = fullkey;
  139. key.size = fullkeylen + 1;
  140. res = astdb->get(astdb, &key, &data, 0);
  141. ast_mutex_unlock(&dblock);
  142. /* Be sure to NULL terminate our data either way */
  143. if (res) {
  144. ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
  145. } else {
  146. #if 0
  147. printf("Got value of size %d\n", data.size);
  148. #endif
  149. if (data.size) {
  150. ((char *)data.data)[data.size - 1] = '\0';
  151. /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
  152. strncpy(value, data.data, (valuelen > data.size) ? data.size : valuelen);
  153. value[valuelen - 1] = '\0';
  154. } else {
  155. ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
  156. }
  157. }
  158. return res;
  159. }
  160. int ast_db_del(const char *family, const char *keys)
  161. {
  162. char fullkey[256];
  163. DBT key;
  164. int res, fullkeylen;
  165. ast_mutex_lock(&dblock);
  166. if (dbinit()) {
  167. ast_mutex_unlock(&dblock);
  168. return -1;
  169. }
  170. fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
  171. memset(&key, 0, sizeof(key));
  172. key.data = fullkey;
  173. key.size = fullkeylen + 1;
  174. res = astdb->del(astdb, &key, 0);
  175. astdb->sync(astdb, 0);
  176. ast_mutex_unlock(&dblock);
  177. if (res)
  178. ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
  179. return res;
  180. }
  181. static int database_put(int fd, int argc, char *argv[])
  182. {
  183. int res;
  184. if (argc != 5)
  185. return RESULT_SHOWUSAGE;
  186. res = ast_db_put(argv[2], argv[3], argv[4]);
  187. if (res)
  188. ast_cli(fd, "Failed to update entry\n");
  189. else
  190. ast_cli(fd, "Updated database successfully\n");
  191. return RESULT_SUCCESS;
  192. }
  193. static int database_get(int fd, int argc, char *argv[])
  194. {
  195. int res;
  196. char tmp[256];
  197. if (argc != 4)
  198. return RESULT_SHOWUSAGE;
  199. res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
  200. if (res)
  201. ast_cli(fd, "Database entry not found.\n");
  202. else
  203. ast_cli(fd, "Value: %s\n", tmp);
  204. return RESULT_SUCCESS;
  205. }
  206. static int database_del(int fd, int argc, char *argv[])
  207. {
  208. int res;
  209. if (argc != 4)
  210. return RESULT_SHOWUSAGE;
  211. res = ast_db_del(argv[2], argv[3]);
  212. if (res)
  213. ast_cli(fd, "Database entry does not exist.\n");
  214. else
  215. ast_cli(fd, "Database entry removed.\n");
  216. return RESULT_SUCCESS;
  217. }
  218. static int database_deltree(int fd, int argc, char *argv[])
  219. {
  220. int res;
  221. if ((argc < 3) || (argc > 4))
  222. return RESULT_SHOWUSAGE;
  223. if (argc == 4)
  224. res = ast_db_deltree(argv[2], argv[3]);
  225. else
  226. res = ast_db_deltree(argv[2], NULL);
  227. if (res)
  228. ast_cli(fd, "Database entries do not exist.\n");
  229. else
  230. ast_cli(fd, "Database entries removed.\n");
  231. return RESULT_SUCCESS;
  232. }
  233. static int database_show(int fd, int argc, char *argv[])
  234. {
  235. char prefix[256];
  236. DBT key, data;
  237. char *keys, *values;
  238. int res;
  239. int pass;
  240. if (argc == 4) {
  241. /* Family and key tree */
  242. snprintf(prefix, sizeof(prefix), "/%s/%s", argv[2], argv[3]);
  243. } else if (argc == 3) {
  244. /* Family only */
  245. snprintf(prefix, sizeof(prefix), "/%s", argv[2]);
  246. } else if (argc == 2) {
  247. /* Neither */
  248. prefix[0] = '\0';
  249. } else
  250. return RESULT_SHOWUSAGE;
  251. ast_mutex_lock(&dblock);
  252. if (dbinit()) {
  253. ast_mutex_unlock(&dblock);
  254. ast_cli(fd, "Database unavailable\n");
  255. return RESULT_SUCCESS;
  256. }
  257. memset(&key, 0, sizeof(key));
  258. memset(&data, 0, sizeof(data));
  259. pass = 0;
  260. while(!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
  261. if (key.size) {
  262. keys = key.data;
  263. keys[key.size - 1] = '\0';
  264. } else
  265. keys = "<bad key>";
  266. if (data.size) {
  267. values = data.data;
  268. values[data.size - 1]='\0';
  269. } else
  270. values = "<bad value>";
  271. if (keymatch(keys, prefix)) {
  272. ast_cli(fd, "%-50s: %-25s\n", keys, values);
  273. }
  274. }
  275. ast_mutex_unlock(&dblock);
  276. return RESULT_SUCCESS;
  277. }
  278. struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
  279. {
  280. char prefix[256];
  281. DBT key, data;
  282. char *keys, *values;
  283. int res;
  284. int pass;
  285. struct ast_db_entry *last = NULL;
  286. struct ast_db_entry *cur, *ret=NULL;
  287. if (family && !ast_strlen_zero(family)) {
  288. if (keytree && !ast_strlen_zero(keytree))
  289. /* Family and key tree */
  290. snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix);
  291. else
  292. /* Family only */
  293. snprintf(prefix, sizeof(prefix), "/%s", family);
  294. } else
  295. prefix[0] = '\0';
  296. ast_mutex_lock(&dblock);
  297. if (dbinit()) {
  298. ast_mutex_unlock(&dblock);
  299. ast_log(LOG_WARNING, "Database unavailable\n");
  300. return NULL;
  301. }
  302. memset(&key, 0, sizeof(key));
  303. memset(&data, 0, sizeof(data));
  304. pass = 0;
  305. while(!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
  306. if (key.size) {
  307. keys = key.data;
  308. keys[key.size - 1] = '\0';
  309. } else
  310. keys = "<bad key>";
  311. if (data.size) {
  312. values = data.data;
  313. values[data.size - 1]='\0';
  314. } else
  315. values = "<bad value>";
  316. if (keymatch(keys, prefix)) {
  317. cur = malloc(sizeof(struct ast_db_entry) + strlen(keys) + strlen(values) + 2);
  318. if (cur) {
  319. cur->next = NULL;
  320. cur->key = cur->data + strlen(values) + 1;
  321. strcpy(cur->data, values);
  322. strcpy(cur->key, keys);
  323. if (last)
  324. last->next = cur;
  325. else
  326. ret = cur;
  327. last = cur;
  328. }
  329. }
  330. }
  331. ast_mutex_unlock(&dblock);
  332. return ret;
  333. }
  334. void ast_db_freetree(struct ast_db_entry *dbe)
  335. {
  336. struct ast_db_entry *last;
  337. while(dbe) {
  338. last = dbe;
  339. dbe = dbe->next;
  340. free(last);
  341. }
  342. }
  343. static char database_show_usage[] =
  344. "Usage: database show [family [keytree]]\n"
  345. " Shows Asterisk database contents, optionally restricted\n"
  346. "to a given family, or family and keytree.\n";
  347. static char database_put_usage[] =
  348. "Usage: database put <family> <key> <value>\n"
  349. " Adds or updates an entry in the Asterisk database for\n"
  350. "a given family, key, and value.\n";
  351. static char database_get_usage[] =
  352. "Usage: database get <family> <key>\n"
  353. " Retrieves an entry in the Asterisk database for a given\n"
  354. "family and key.\n";
  355. static char database_del_usage[] =
  356. "Usage: database del <family> <key>\n"
  357. " Deletes an entry in the Asterisk database for a given\n"
  358. "family and key.\n";
  359. static char database_deltree_usage[] =
  360. "Usage: database deltree <family> [keytree]\n"
  361. " Deletes a family or specific keytree within a family\n"
  362. "in the Asterisk database.\n";
  363. struct ast_cli_entry cli_database_show =
  364. { { "database", "show", NULL }, database_show, "Shows database contents", database_show_usage };
  365. struct ast_cli_entry cli_database_get =
  366. { { "database", "get", NULL }, database_get, "Gets database value", database_get_usage };
  367. struct ast_cli_entry cli_database_put =
  368. { { "database", "put", NULL }, database_put, "Adds/updates database value", database_put_usage };
  369. struct ast_cli_entry cli_database_del =
  370. { { "database", "del", NULL }, database_del, "Removes database key/value", database_del_usage };
  371. struct ast_cli_entry cli_database_deltree =
  372. { { "database", "deltree", NULL }, database_deltree, "Removes database keytree/values", database_deltree_usage };
  373. int astdb_init(void)
  374. {
  375. dbinit();
  376. ast_cli_register(&cli_database_show);
  377. ast_cli_register(&cli_database_get);
  378. ast_cli_register(&cli_database_put);
  379. ast_cli_register(&cli_database_del);
  380. ast_cli_register(&cli_database_deltree);
  381. return 0;
  382. }