Merged revisions 116463 via svnmerge from
[asterisk-bristuff.git] / main / db.c
blob94bf4f0012c55d515fd3484f99ca6050b4ac86d6
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief ASTdb Management
23 * \author Mark Spencer <markster@digium.com>
25 * \note DB3 is licensed under Sleepycat Public License and is thus incompatible
26 * with GPL. To avoid having to make another exception (and complicate
27 * licensing even further) we elect to use DB1 which is BSD licensed
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/_private.h"
35 #include "asterisk/paths.h" /* use ast_config_AST_DB */
36 #include <sys/time.h>
37 #include <signal.h>
38 #include <dirent.h>
40 #include "asterisk/channel.h"
41 #include "asterisk/file.h"
42 #include "asterisk/app.h"
43 #include "asterisk/dsp.h"
44 #include "asterisk/astdb.h"
45 #include "asterisk/cli.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/lock.h"
48 #include "asterisk/manager.h"
49 #include "db1-ast/include/db.h"
51 static DB *astdb;
52 AST_MUTEX_DEFINE_STATIC(dblock);
54 static int dbinit(void)
56 if (!astdb && !(astdb = dbopen(ast_config_AST_DB, O_CREAT | O_RDWR, AST_FILE_MODE, DB_BTREE, NULL))) {
57 ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB, strerror(errno));
58 return -1;
60 return 0;
64 static inline int keymatch(const char *key, const char *prefix)
66 int preflen = strlen(prefix);
67 if (!preflen)
68 return 1;
69 if (!strcasecmp(key, prefix))
70 return 1;
71 if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
72 if (key[preflen] == '/')
73 return 1;
75 return 0;
78 static inline int subkeymatch(const char *key, const char *suffix)
80 int suffixlen = strlen(suffix);
81 if (suffixlen) {
82 const char *subkey = key + strlen(key) - suffixlen;
83 if (subkey < key)
84 return 0;
85 if (!strcasecmp(subkey, suffix))
86 return 1;
88 return 0;
91 int ast_db_deltree(const char *family, const char *keytree)
93 char prefix[256];
94 DBT key, data;
95 char *keys;
96 int res;
97 int pass;
98 int counter = 0;
100 if (family) {
101 if (keytree) {
102 snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
103 } else {
104 snprintf(prefix, sizeof(prefix), "/%s", family);
106 } else if (keytree) {
107 return -1;
108 } else {
109 prefix[0] = '\0';
112 ast_mutex_lock(&dblock);
113 if (dbinit()) {
114 ast_mutex_unlock(&dblock);
115 return -1;
118 memset(&key, 0, sizeof(key));
119 memset(&data, 0, sizeof(data));
120 pass = 0;
121 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
122 if (key.size) {
123 keys = key.data;
124 keys[key.size - 1] = '\0';
125 } else {
126 keys = "<bad key>";
128 if (keymatch(keys, prefix)) {
129 astdb->del(astdb, &key, 0);
130 counter++;
133 astdb->sync(astdb, 0);
134 ast_mutex_unlock(&dblock);
135 return counter;
138 int ast_db_put(const char *family, const char *keys, const char *value)
140 char fullkey[256];
141 DBT key, data;
142 int res, fullkeylen;
144 ast_mutex_lock(&dblock);
145 if (dbinit()) {
146 ast_mutex_unlock(&dblock);
147 return -1;
150 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
151 memset(&key, 0, sizeof(key));
152 memset(&data, 0, sizeof(data));
153 key.data = fullkey;
154 key.size = fullkeylen + 1;
155 data.data = (char *) value;
156 data.size = strlen(value) + 1;
157 res = astdb->put(astdb, &key, &data, 0);
158 astdb->sync(astdb, 0);
159 ast_mutex_unlock(&dblock);
160 if (res)
161 ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
162 return res;
165 int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
167 char fullkey[256] = "";
168 DBT key, data;
169 int res, fullkeylen;
171 ast_mutex_lock(&dblock);
172 if (dbinit()) {
173 ast_mutex_unlock(&dblock);
174 return -1;
177 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
178 memset(&key, 0, sizeof(key));
179 memset(&data, 0, sizeof(data));
180 memset(value, 0, valuelen);
181 key.data = fullkey;
182 key.size = fullkeylen + 1;
184 res = astdb->get(astdb, &key, &data, 0);
186 ast_mutex_unlock(&dblock);
188 /* Be sure to NULL terminate our data either way */
189 if (res) {
190 ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
191 } else {
192 #if 0
193 printf("Got value of size %d\n", data.size);
194 #endif
195 if (data.size) {
196 ((char *)data.data)[data.size - 1] = '\0';
197 /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
198 ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
199 } else {
200 ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
203 return res;
206 int ast_db_del(const char *family, const char *keys)
208 char fullkey[256];
209 DBT key;
210 int res, fullkeylen;
212 ast_mutex_lock(&dblock);
213 if (dbinit()) {
214 ast_mutex_unlock(&dblock);
215 return -1;
218 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
219 memset(&key, 0, sizeof(key));
220 key.data = fullkey;
221 key.size = fullkeylen + 1;
223 res = astdb->del(astdb, &key, 0);
224 astdb->sync(astdb, 0);
226 ast_mutex_unlock(&dblock);
228 if (res) {
229 ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
231 return res;
234 static char *handle_cli_database_put(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
236 int res;
238 switch (cmd) {
239 case CLI_INIT:
240 e->command = "database put";
241 e->usage =
242 "Usage: database put <family> <key> <value>\n"
243 " Adds or updates an entry in the Asterisk database for\n"
244 " a given family, key, and value.\n";
245 return NULL;
246 case CLI_GENERATE:
247 return NULL;
250 if (a->argc != 5)
251 return CLI_SHOWUSAGE;
252 res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
253 if (res) {
254 ast_cli(a->fd, "Failed to update entry\n");
255 } else {
256 ast_cli(a->fd, "Updated database successfully\n");
258 return CLI_SUCCESS;
261 static char *handle_cli_database_get(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
263 int res;
264 char tmp[256];
266 switch (cmd) {
267 case CLI_INIT:
268 e->command = "database get";
269 e->usage =
270 "Usage: database get <family> <key>\n"
271 " Retrieves an entry in the Asterisk database for a given\n"
272 " family and key.\n";
273 return NULL;
274 case CLI_GENERATE:
275 return NULL;
278 if (a->argc != 4)
279 return CLI_SHOWUSAGE;
280 res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
281 if (res) {
282 ast_cli(a->fd, "Database entry not found.\n");
283 } else {
284 ast_cli(a->fd, "Value: %s\n", tmp);
286 return CLI_SUCCESS;
289 static char *handle_cli_database_del(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
291 int res;
293 switch (cmd) {
294 case CLI_INIT:
295 e->command = "database del";
296 e->usage =
297 "Usage: database del <family> <key>\n"
298 " Deletes an entry in the Asterisk database for a given\n"
299 " family and key.\n";
300 return NULL;
301 case CLI_GENERATE:
302 return NULL;
305 if (a->argc != 4)
306 return CLI_SHOWUSAGE;
307 res = ast_db_del(a->argv[2], a->argv[3]);
308 if (res) {
309 ast_cli(a->fd, "Database entry does not exist.\n");
310 } else {
311 ast_cli(a->fd, "Database entry removed.\n");
313 return CLI_SUCCESS;
316 static char *handle_cli_database_deltree(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
318 int res;
320 switch (cmd) {
321 case CLI_INIT:
322 e->command = "database deltree";
323 e->usage =
324 "Usage: database deltree <family> [keytree]\n"
325 " Deletes a family or specific keytree within a family\n"
326 " in the Asterisk database.\n";
327 return NULL;
328 case CLI_GENERATE:
329 return NULL;
332 if ((a->argc < 3) || (a->argc > 4))
333 return CLI_SHOWUSAGE;
334 if (a->argc == 4) {
335 res = ast_db_deltree(a->argv[2], a->argv[3]);
336 } else {
337 res = ast_db_deltree(a->argv[2], NULL);
339 if (res < 0) {
340 ast_cli(a->fd, "Database entries do not exist.\n");
341 } else {
342 ast_cli(a->fd, "%d database entries removed.\n",res);
344 return CLI_SUCCESS;
347 static char *handle_cli_database_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
349 char prefix[256];
350 DBT key, data;
351 char *keys, *values;
352 int res;
353 int pass;
354 int counter = 0;
356 switch (cmd) {
357 case CLI_INIT:
358 e->command = "database show";
359 e->usage =
360 "Usage: database show [family [keytree]]\n"
361 " Shows Asterisk database contents, optionally restricted\n"
362 " to a given family, or family and keytree.\n";
363 return NULL;
364 case CLI_GENERATE:
365 return NULL;
368 if (a->argc == 4) {
369 /* Family and key tree */
370 snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
371 } else if (a->argc == 3) {
372 /* Family only */
373 snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
374 } else if (a->argc == 2) {
375 /* Neither */
376 prefix[0] = '\0';
377 } else {
378 return CLI_SHOWUSAGE;
380 ast_mutex_lock(&dblock);
381 if (dbinit()) {
382 ast_mutex_unlock(&dblock);
383 ast_cli(a->fd, "Database unavailable\n");
384 return CLI_SUCCESS;
386 memset(&key, 0, sizeof(key));
387 memset(&data, 0, sizeof(data));
388 pass = 0;
389 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
390 if (key.size) {
391 keys = key.data;
392 keys[key.size - 1] = '\0';
393 } else {
394 keys = "<bad key>";
396 if (data.size) {
397 values = data.data;
398 values[data.size - 1]='\0';
399 } else {
400 values = "<bad value>";
402 if (keymatch(keys, prefix)) {
403 ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
404 counter++;
407 ast_mutex_unlock(&dblock);
408 ast_cli(a->fd, "%d results found.\n", counter);
409 return CLI_SUCCESS;
412 static char *handle_cli_database_showkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
414 char suffix[256];
415 DBT key, data;
416 char *keys, *values;
417 int res;
418 int pass;
419 int counter = 0;
421 switch (cmd) {
422 case CLI_INIT:
423 e->command = "database showkey";
424 e->usage =
425 "Usage: database showkey <keytree>\n"
426 " Shows Asterisk database contents, restricted to a given key.\n";
427 return NULL;
428 case CLI_GENERATE:
429 return NULL;
432 if (a->argc == 3) {
433 /* Key only */
434 snprintf(suffix, sizeof(suffix), "/%s", a->argv[2]);
435 } else {
436 return CLI_SHOWUSAGE;
438 ast_mutex_lock(&dblock);
439 if (dbinit()) {
440 ast_mutex_unlock(&dblock);
441 ast_cli(a->fd, "Database unavailable\n");
442 return CLI_SUCCESS;
444 memset(&key, 0, sizeof(key));
445 memset(&data, 0, sizeof(data));
446 pass = 0;
447 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
448 if (key.size) {
449 keys = key.data;
450 keys[key.size - 1] = '\0';
451 } else {
452 keys = "<bad key>";
454 if (data.size) {
455 values = data.data;
456 values[data.size - 1]='\0';
457 } else {
458 values = "<bad value>";
460 if (subkeymatch(keys, suffix)) {
461 ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
462 counter++;
465 ast_mutex_unlock(&dblock);
466 ast_cli(a->fd, "%d results found.\n", counter);
467 return CLI_SUCCESS;
470 struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
472 char prefix[256];
473 DBT key, data;
474 char *keys, *values;
475 int values_len;
476 int res;
477 int pass;
478 struct ast_db_entry *last = NULL;
479 struct ast_db_entry *cur, *ret=NULL;
481 if (!ast_strlen_zero(family)) {
482 if (!ast_strlen_zero(keytree)) {
483 /* Family and key tree */
484 snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix);
485 } else {
486 /* Family only */
487 snprintf(prefix, sizeof(prefix), "/%s", family);
489 } else {
490 prefix[0] = '\0';
492 ast_mutex_lock(&dblock);
493 if (dbinit()) {
494 ast_mutex_unlock(&dblock);
495 ast_log(LOG_WARNING, "Database unavailable\n");
496 return NULL;
498 memset(&key, 0, sizeof(key));
499 memset(&data, 0, sizeof(data));
500 pass = 0;
501 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
502 if (key.size) {
503 keys = key.data;
504 keys[key.size - 1] = '\0';
505 } else {
506 keys = "<bad key>";
508 if (data.size) {
509 values = data.data;
510 values[data.size - 1] = '\0';
511 } else {
512 values = "<bad value>";
514 values_len = strlen(values) + 1;
515 if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
516 cur->next = NULL;
517 cur->key = cur->data + values_len;
518 strcpy(cur->data, values);
519 strcpy(cur->key, keys);
520 if (last) {
521 last->next = cur;
522 } else {
523 ret = cur;
525 last = cur;
528 ast_mutex_unlock(&dblock);
529 return ret;
532 void ast_db_freetree(struct ast_db_entry *dbe)
534 struct ast_db_entry *last;
535 while (dbe) {
536 last = dbe;
537 dbe = dbe->next;
538 ast_free(last);
542 struct ast_cli_entry cli_database[] = {
543 AST_CLI_DEFINE(handle_cli_database_show, "Shows database contents"),
544 AST_CLI_DEFINE(handle_cli_database_showkey, "Shows database contents"),
545 AST_CLI_DEFINE(handle_cli_database_get, "Gets database value"),
546 AST_CLI_DEFINE(handle_cli_database_put, "Adds/updates database value"),
547 AST_CLI_DEFINE(handle_cli_database_del, "Removes database key/value"),
548 AST_CLI_DEFINE(handle_cli_database_deltree, "Removes database keytree/values")
551 static int manager_dbput(struct mansession *s, const struct message *m)
553 const char *family = astman_get_header(m, "Family");
554 const char *key = astman_get_header(m, "Key");
555 const char *val = astman_get_header(m, "Val");
556 int res;
558 if (ast_strlen_zero(family)) {
559 astman_send_error(s, m, "No family specified");
560 return 0;
562 if (ast_strlen_zero(key)) {
563 astman_send_error(s, m, "No key specified");
564 return 0;
567 res = ast_db_put(family, key, S_OR(val, ""));
568 if (res) {
569 astman_send_error(s, m, "Failed to update entry");
570 } else {
571 astman_send_ack(s, m, "Updated database successfully");
573 return 0;
576 static int manager_dbget(struct mansession *s, const struct message *m)
578 const char *id = astman_get_header(m,"ActionID");
579 char idText[256] = "";
580 const char *family = astman_get_header(m, "Family");
581 const char *key = astman_get_header(m, "Key");
582 char tmp[256];
583 int res;
585 if (ast_strlen_zero(family)) {
586 astman_send_error(s, m, "No family specified.");
587 return 0;
589 if (ast_strlen_zero(key)) {
590 astman_send_error(s, m, "No key specified.");
591 return 0;
594 if (!ast_strlen_zero(id))
595 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
597 res = ast_db_get(family, key, tmp, sizeof(tmp));
598 if (res) {
599 astman_send_error(s, m, "Database entry not found");
600 } else {
601 astman_send_ack(s, m, "Result will follow");
602 astman_append(s, "Event: DBGetResponse\r\n"
603 "Family: %s\r\n"
604 "Key: %s\r\n"
605 "Val: %s\r\n"
606 "%s"
607 "\r\n",
608 family, key, tmp, idText);
610 return 0;
613 static int manager_dbdel(struct mansession *s, const struct message *m)
615 const char *family = astman_get_header(m, "Family");
616 const char *key = astman_get_header(m, "Key");
617 int res;
619 if (ast_strlen_zero(family)) {
620 astman_send_error(s, m, "No family specified.");
621 return 0;
624 if (ast_strlen_zero(key)) {
625 astman_send_error(s, m, "No key specified.");
626 return 0;
629 res = ast_db_del(family, key);
630 if (res)
631 astman_send_error(s, m, "Database entry not found");
632 else
633 astman_send_ack(s, m, "Key deleted successfully");
635 return 0;
638 static int manager_dbdeltree(struct mansession *s, const struct message *m)
640 const char *family = astman_get_header(m, "Family");
641 const char *key = astman_get_header(m, "Key");
642 int res;
644 if (ast_strlen_zero(family)) {
645 astman_send_error(s, m, "No family specified.");
646 return 0;
649 if (!ast_strlen_zero(key))
650 res = ast_db_deltree(family, key);
651 else
652 res = ast_db_deltree(family, NULL);
654 if (res < 0)
655 astman_send_error(s, m, "Database entry not found");
656 else
657 astman_send_ack(s, m, "Key tree deleted successfully");
659 return 0;
662 int astdb_init(void)
664 dbinit();
665 ast_cli_register_multiple(cli_database, sizeof(cli_database) / sizeof(struct ast_cli_entry));
666 ast_manager_register("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget, "Get DB Entry");
667 ast_manager_register("DBPut", EVENT_FLAG_SYSTEM, manager_dbput, "Put DB Entry");
668 ast_manager_register("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel, "Delete DB Entry");
669 ast_manager_register("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree, "Delete DB Tree");
670 return 0;