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.
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
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43 #include "asterisk/channel.h"
44 #include "asterisk/file.h"
45 #include "asterisk/app.h"
46 #include "asterisk/dsp.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/options.h"
49 #include "asterisk/astdb.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/utils.h"
52 #include "asterisk/lock.h"
53 #include "asterisk/manager.h"
54 #include "db1-ast/include/db.h"
57 #define dbopen __dbopen
61 AST_MUTEX_DEFINE_STATIC(dblock);
63 static int dbinit(void)
65 if (!astdb && !(astdb = dbopen((char *)ast_config_AST_DB, O_CREAT | O_RDWR, AST_FILE_MODE, DB_BTREE, NULL))) {
66 ast_log(LOG_WARNING, "Unable to open Asterisk database\n");
73 static inline int keymatch(const char *key, const char *prefix)
75 int preflen = strlen(prefix);
78 if (!strcasecmp(key, prefix))
80 if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
81 if (key[preflen] == '/')
87 static inline int subkeymatch(const char *key, const char *suffix)
89 int suffixlen = strlen(suffix);
91 const char *subkey = key + strlen(key) - suffixlen;
94 if (!strcasecmp(subkey, suffix))
100 int ast_db_deltree(const char *family, const char *keytree)
110 snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
112 snprintf(prefix, sizeof(prefix), "/%s", family);
114 } else if (keytree) {
120 ast_mutex_lock(&dblock);
122 ast_mutex_unlock(&dblock);
126 memset(&key, 0, sizeof(key));
127 memset(&data, 0, sizeof(data));
129 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
132 keys[key.size - 1] = '\0';
136 if (keymatch(keys, prefix)) {
137 astdb->del(astdb, &key, 0);
140 astdb->sync(astdb, 0);
141 ast_mutex_unlock(&dblock);
145 int ast_db_put(const char *family, const char *keys, const char *value)
151 ast_mutex_lock(&dblock);
153 ast_mutex_unlock(&dblock);
157 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
158 memset(&key, 0, sizeof(key));
159 memset(&data, 0, sizeof(data));
161 key.size = fullkeylen + 1;
162 data.data = (char *) value;
163 data.size = strlen(value) + 1;
164 res = astdb->put(astdb, &key, &data, 0);
165 astdb->sync(astdb, 0);
166 ast_mutex_unlock(&dblock);
168 ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
172 int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
174 char fullkey[256] = "";
178 ast_mutex_lock(&dblock);
180 ast_mutex_unlock(&dblock);
184 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
185 memset(&key, 0, sizeof(key));
186 memset(&data, 0, sizeof(data));
187 memset(value, 0, valuelen);
189 key.size = fullkeylen + 1;
191 res = astdb->get(astdb, &key, &data, 0);
193 ast_mutex_unlock(&dblock);
195 /* Be sure to NULL terminate our data either way */
198 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
201 printf("Got value of size %d\n", data.size);
204 ((char *)data.data)[data.size - 1] = '\0';
205 /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
206 ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
208 ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
214 int ast_db_del(const char *family, const char *keys)
220 ast_mutex_lock(&dblock);
222 ast_mutex_unlock(&dblock);
226 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
227 memset(&key, 0, sizeof(key));
229 key.size = fullkeylen + 1;
231 res = astdb->del(astdb, &key, 0);
232 astdb->sync(astdb, 0);
234 ast_mutex_unlock(&dblock);
238 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
243 static int database_put(int fd, int argc, char *argv[])
247 return RESULT_SHOWUSAGE;
248 res = ast_db_put(argv[2], argv[3], argv[4]);
250 ast_cli(fd, "Failed to update entry\n");
252 ast_cli(fd, "Updated database successfully\n");
254 return RESULT_SUCCESS;
257 static int database_get(int fd, int argc, char *argv[])
262 return RESULT_SHOWUSAGE;
263 res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
265 ast_cli(fd, "Database entry not found.\n");
267 ast_cli(fd, "Value: %s\n", tmp);
269 return RESULT_SUCCESS;
272 static int database_del(int fd, int argc, char *argv[])
276 return RESULT_SHOWUSAGE;
277 res = ast_db_del(argv[2], argv[3]);
279 ast_cli(fd, "Database entry does not exist.\n");
281 ast_cli(fd, "Database entry removed.\n");
283 return RESULT_SUCCESS;
286 static int database_deltree(int fd, int argc, char *argv[])
289 if ((argc < 3) || (argc > 4))
290 return RESULT_SHOWUSAGE;
292 res = ast_db_deltree(argv[2], argv[3]);
294 res = ast_db_deltree(argv[2], NULL);
297 ast_cli(fd, "Database entries do not exist.\n");
299 ast_cli(fd, "Database entries removed.\n");
301 return RESULT_SUCCESS;
304 static int database_show(int fd, int argc, char *argv[])
313 /* Family and key tree */
314 snprintf(prefix, sizeof(prefix), "/%s/%s", argv[2], argv[3]);
315 } else if (argc == 3) {
317 snprintf(prefix, sizeof(prefix), "/%s", argv[2]);
318 } else if (argc == 2) {
322 return RESULT_SHOWUSAGE;
324 ast_mutex_lock(&dblock);
326 ast_mutex_unlock(&dblock);
327 ast_cli(fd, "Database unavailable\n");
328 return RESULT_SUCCESS;
330 memset(&key, 0, sizeof(key));
331 memset(&data, 0, sizeof(data));
333 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
336 keys[key.size - 1] = '\0';
342 values[data.size - 1]='\0';
344 values = "<bad value>";
346 if (keymatch(keys, prefix)) {
347 ast_cli(fd, "%-50s: %-25s\n", keys, values);
350 ast_mutex_unlock(&dblock);
351 return RESULT_SUCCESS;
354 static int database_showkey(int fd, int argc, char *argv[])
364 snprintf(suffix, sizeof(suffix), "/%s", argv[2]);
366 return RESULT_SHOWUSAGE;
368 ast_mutex_lock(&dblock);
370 ast_mutex_unlock(&dblock);
371 ast_cli(fd, "Database unavailable\n");
372 return RESULT_SUCCESS;
374 memset(&key, 0, sizeof(key));
375 memset(&data, 0, sizeof(data));
377 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
380 keys[key.size - 1] = '\0';
386 values[data.size - 1]='\0';
388 values = "<bad value>";
390 if (subkeymatch(keys, suffix)) {
391 ast_cli(fd, "%-50s: %-25s\n", keys, values);
394 ast_mutex_unlock(&dblock);
395 return RESULT_SUCCESS;
398 struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
406 struct ast_db_entry *last = NULL;
407 struct ast_db_entry *cur, *ret=NULL;
409 if (!ast_strlen_zero(family)) {
410 if (!ast_strlen_zero(keytree)) {
411 /* Family and key tree */
412 snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix);
415 snprintf(prefix, sizeof(prefix), "/%s", family);
420 ast_mutex_lock(&dblock);
422 ast_mutex_unlock(&dblock);
423 ast_log(LOG_WARNING, "Database unavailable\n");
426 memset(&key, 0, sizeof(key));
427 memset(&data, 0, sizeof(data));
429 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
432 keys[key.size - 1] = '\0';
438 values[data.size - 1] = '\0';
440 values = "<bad value>";
442 values_len = strlen(values) + 1;
443 if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
445 cur->key = cur->data + values_len;
446 strcpy(cur->data, values);
447 strcpy(cur->key, keys);
456 ast_mutex_unlock(&dblock);
460 void ast_db_freetree(struct ast_db_entry *dbe)
462 struct ast_db_entry *last;
470 static const char database_show_usage[] =
471 "Usage: database show [family [keytree]]\n"
472 " Shows Asterisk database contents, optionally restricted\n"
473 "to a given family, or family and keytree.\n";
475 static const char database_showkey_usage[] =
476 "Usage: database showkey <keytree>\n"
477 " Shows Asterisk database contents, restricted to a given key.\n";
479 static const char database_put_usage[] =
480 "Usage: database put <family> <key> <value>\n"
481 " Adds or updates an entry in the Asterisk database for\n"
482 "a given family, key, and value.\n";
484 static const char database_get_usage[] =
485 "Usage: database get <family> <key>\n"
486 " Retrieves an entry in the Asterisk database for a given\n"
489 static const char database_del_usage[] =
490 "Usage: database del <family> <key>\n"
491 " Deletes an entry in the Asterisk database for a given\n"
494 static const char database_deltree_usage[] =
495 "Usage: database deltree <family> [keytree]\n"
496 " Deletes a family or specific keytree within a family\n"
497 "in the Asterisk database.\n";
499 struct ast_cli_entry cli_database[] = {
500 { { "database", "show", NULL },
501 database_show, "Shows database contents",
502 database_show_usage },
504 { { "database", "showkey", NULL },
505 database_showkey, "Shows database contents",
506 database_showkey_usage },
508 { { "database", "get", NULL },
509 database_get, "Gets database value",
510 database_get_usage },
512 { { "database", "put", NULL },
513 database_put, "Adds/updates database value",
514 database_put_usage },
516 { { "database", "del", NULL },
517 database_del, "Removes database key/value",
518 database_del_usage },
520 { { "database", "deltree", NULL },
521 database_deltree, "Removes database keytree/values",
522 database_deltree_usage },
525 static int manager_dbput(struct mansession *s, const struct message *m)
527 const char *family = astman_get_header(m, "Family");
528 const char *key = astman_get_header(m, "Key");
529 const char *val = astman_get_header(m, "Val");
532 if (ast_strlen_zero(family)) {
533 astman_send_error(s, m, "No family specified");
536 if (ast_strlen_zero(key)) {
537 astman_send_error(s, m, "No key specified");
540 if (ast_strlen_zero(val)) {
541 astman_send_error(s, m, "No val specified");
545 res = ast_db_put(family, key, val);
547 astman_send_error(s, m, "Failed to update entry");
549 astman_send_ack(s, m, "Updated database successfully");
554 static int manager_dbget(struct mansession *s, const struct message *m)
556 const char *id = astman_get_header(m,"ActionID");
557 char idText[256] = "";
558 const char *family = astman_get_header(m, "Family");
559 const char *key = astman_get_header(m, "Key");
563 if (ast_strlen_zero(family)) {
564 astman_send_error(s, m, "No family specified.");
567 if (ast_strlen_zero(key)) {
568 astman_send_error(s, m, "No key specified.");
572 if (!ast_strlen_zero(id))
573 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
575 res = ast_db_get(family, key, tmp, sizeof(tmp));
577 astman_send_error(s, m, "Database entry not found");
579 astman_send_ack(s, m, "Result will follow");
580 astman_append(s, "Event: DBGetResponse\r\n"
586 family, key, tmp, idText);
591 static int manager_dbdel(struct mansession *s, const struct message *m)
593 const char *family = astman_get_header(m, "Family");
594 const char *key = astman_get_header(m, "Key");
597 if (ast_strlen_zero(family)) {
598 astman_send_error(s, m, "No family specified.");
602 if (ast_strlen_zero(key)) {
603 astman_send_error(s, m, "No key specified.");
607 res = ast_db_del(family, key);
609 astman_send_error(s, m, "Database entry not found");
611 astman_send_ack(s, m, "Key deleted successfully");
616 static int manager_dbdeltree(struct mansession *s, const struct message *m)
618 const char *family = astman_get_header(m, "Family");
619 const char *key = astman_get_header(m, "Key");
622 if (ast_strlen_zero(family)) {
623 astman_send_error(s, m, "No family specified.");
627 if (!ast_strlen_zero(key))
628 res = ast_db_deltree(family, key);
630 res = ast_db_deltree(family, NULL);
633 astman_send_error(s, m, "Database entry not found");
635 astman_send_ack(s, m, "Key tree deleted successfully");
643 ast_cli_register_multiple(cli_database, sizeof(cli_database) / sizeof(struct ast_cli_entry));
644 ast_manager_register("DBGet", EVENT_FLAG_SYSTEM, manager_dbget, "Get DB Entry");
645 ast_manager_register("DBPut", EVENT_FLAG_SYSTEM, manager_dbput, "Put DB Entry");
646 ast_manager_register("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel, "Delete DB Entry");
647 ast_manager_register("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree, "Delete DB Tree");