00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 182452 $")
00033
00034 #include "asterisk/_private.h"
00035 #include "asterisk/paths.h"
00036 #include <sys/time.h>
00037 #include <signal.h>
00038 #include <dirent.h>
00039
00040 #include "asterisk/channel.h"
00041 #include "asterisk/file.h"
00042 #include "asterisk/app.h"
00043 #include "asterisk/dsp.h"
00044 #include "asterisk/astdb.h"
00045 #include "asterisk/cli.h"
00046 #include "asterisk/utils.h"
00047 #include "asterisk/lock.h"
00048 #include "asterisk/manager.h"
00049 #include "db1-ast/include/db.h"
00050
00051 static DB *astdb;
00052 AST_MUTEX_DEFINE_STATIC(dblock);
00053
00054 static int dbinit(void)
00055 {
00056 if (!astdb && !(astdb = dbopen(ast_config_AST_DB, O_CREAT | O_RDWR, AST_FILE_MODE, DB_BTREE, NULL))) {
00057 ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB, strerror(errno));
00058 return -1;
00059 }
00060 return 0;
00061 }
00062
00063
00064 static inline int keymatch(const char *key, const char *prefix)
00065 {
00066 int preflen = strlen(prefix);
00067 if (!preflen)
00068 return 1;
00069 if (!strcasecmp(key, prefix))
00070 return 1;
00071 if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
00072 if (key[preflen] == '/')
00073 return 1;
00074 }
00075 return 0;
00076 }
00077
00078 static inline int subkeymatch(const char *key, const char *suffix)
00079 {
00080 int suffixlen = strlen(suffix);
00081 if (suffixlen) {
00082 const char *subkey = key + strlen(key) - suffixlen;
00083 if (subkey < key)
00084 return 0;
00085 if (!strcasecmp(subkey, suffix))
00086 return 1;
00087 }
00088 return 0;
00089 }
00090
00091 int ast_db_deltree(const char *family, const char *keytree)
00092 {
00093 char prefix[256];
00094 DBT key, data;
00095 char *keys;
00096 int res;
00097 int pass;
00098 int counter = 0;
00099
00100 if (family) {
00101 if (keytree) {
00102 snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00103 } else {
00104 snprintf(prefix, sizeof(prefix), "/%s", family);
00105 }
00106 } else if (keytree) {
00107 return -1;
00108 } else {
00109 prefix[0] = '\0';
00110 }
00111
00112 ast_mutex_lock(&dblock);
00113 if (dbinit()) {
00114 ast_mutex_unlock(&dblock);
00115 return -1;
00116 }
00117
00118 memset(&key, 0, sizeof(key));
00119 memset(&data, 0, sizeof(data));
00120 pass = 0;
00121 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00122 if (key.size) {
00123 keys = key.data;
00124 keys[key.size - 1] = '\0';
00125 } else {
00126 keys = "<bad key>";
00127 }
00128 if (keymatch(keys, prefix)) {
00129 astdb->del(astdb, &key, 0);
00130 counter++;
00131 }
00132 }
00133 astdb->sync(astdb, 0);
00134 ast_mutex_unlock(&dblock);
00135 return counter;
00136 }
00137
00138 int ast_db_put(const char *family, const char *keys, const char *value)
00139 {
00140 char fullkey[256];
00141 DBT key, data;
00142 int res, fullkeylen;
00143
00144 ast_mutex_lock(&dblock);
00145 if (dbinit()) {
00146 ast_mutex_unlock(&dblock);
00147 return -1;
00148 }
00149
00150 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00151 memset(&key, 0, sizeof(key));
00152 memset(&data, 0, sizeof(data));
00153 key.data = fullkey;
00154 key.size = fullkeylen + 1;
00155 data.data = (char *) value;
00156 data.size = strlen(value) + 1;
00157 res = astdb->put(astdb, &key, &data, 0);
00158 astdb->sync(astdb, 0);
00159 ast_mutex_unlock(&dblock);
00160 if (res)
00161 ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
00162 return res;
00163 }
00164
00165 int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
00166 {
00167 char fullkey[256] = "";
00168 DBT key, data;
00169 int res, fullkeylen;
00170
00171 ast_mutex_lock(&dblock);
00172 if (dbinit()) {
00173 ast_mutex_unlock(&dblock);
00174 return -1;
00175 }
00176
00177 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00178 memset(&key, 0, sizeof(key));
00179 memset(&data, 0, sizeof(data));
00180 memset(value, 0, valuelen);
00181 key.data = fullkey;
00182 key.size = fullkeylen + 1;
00183
00184 res = astdb->get(astdb, &key, &data, 0);
00185
00186
00187 if (res) {
00188 ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
00189 } else {
00190 #if 0
00191 printf("Got value of size %d\n", data.size);
00192 #endif
00193 if (data.size) {
00194 ((char *)data.data)[data.size - 1] = '\0';
00195
00196 ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
00197 } else {
00198 ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
00199 }
00200 }
00201
00202
00203
00204 ast_mutex_unlock(&dblock);
00205
00206 return res;
00207 }
00208
00209 int ast_db_del(const char *family, const char *keys)
00210 {
00211 char fullkey[256];
00212 DBT key;
00213 int res, fullkeylen;
00214
00215 ast_mutex_lock(&dblock);
00216 if (dbinit()) {
00217 ast_mutex_unlock(&dblock);
00218 return -1;
00219 }
00220
00221 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00222 memset(&key, 0, sizeof(key));
00223 key.data = fullkey;
00224 key.size = fullkeylen + 1;
00225
00226 res = astdb->del(astdb, &key, 0);
00227 astdb->sync(astdb, 0);
00228
00229 ast_mutex_unlock(&dblock);
00230
00231 if (res) {
00232 ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
00233 }
00234 return res;
00235 }
00236
00237 static char *handle_cli_database_put(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00238 {
00239 int res;
00240
00241 switch (cmd) {
00242 case CLI_INIT:
00243 e->command = "database put";
00244 e->usage =
00245 "Usage: database put <family> <key> <value>\n"
00246 " Adds or updates an entry in the Asterisk database for\n"
00247 " a given family, key, and value.\n";
00248 return NULL;
00249 case CLI_GENERATE:
00250 return NULL;
00251 }
00252
00253 if (a->argc != 5)
00254 return CLI_SHOWUSAGE;
00255 res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
00256 if (res) {
00257 ast_cli(a->fd, "Failed to update entry\n");
00258 } else {
00259 ast_cli(a->fd, "Updated database successfully\n");
00260 }
00261 return CLI_SUCCESS;
00262 }
00263
00264 static char *handle_cli_database_get(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00265 {
00266 int res;
00267 char tmp[256];
00268
00269 switch (cmd) {
00270 case CLI_INIT:
00271 e->command = "database get";
00272 e->usage =
00273 "Usage: database get <family> <key>\n"
00274 " Retrieves an entry in the Asterisk database for a given\n"
00275 " family and key.\n";
00276 return NULL;
00277 case CLI_GENERATE:
00278 return NULL;
00279 }
00280
00281 if (a->argc != 4)
00282 return CLI_SHOWUSAGE;
00283 res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
00284 if (res) {
00285 ast_cli(a->fd, "Database entry not found.\n");
00286 } else {
00287 ast_cli(a->fd, "Value: %s\n", tmp);
00288 }
00289 return CLI_SUCCESS;
00290 }
00291
00292 static char *handle_cli_database_del(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00293 {
00294 int res;
00295
00296 switch (cmd) {
00297 case CLI_INIT:
00298 e->command = "database del";
00299 e->usage =
00300 "Usage: database del <family> <key>\n"
00301 " Deletes an entry in the Asterisk database for a given\n"
00302 " family and key.\n";
00303 return NULL;
00304 case CLI_GENERATE:
00305 return NULL;
00306 }
00307
00308 if (a->argc != 4)
00309 return CLI_SHOWUSAGE;
00310 res = ast_db_del(a->argv[2], a->argv[3]);
00311 if (res) {
00312 ast_cli(a->fd, "Database entry does not exist.\n");
00313 } else {
00314 ast_cli(a->fd, "Database entry removed.\n");
00315 }
00316 return CLI_SUCCESS;
00317 }
00318
00319 static char *handle_cli_database_deltree(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00320 {
00321 int res;
00322
00323 switch (cmd) {
00324 case CLI_INIT:
00325 e->command = "database deltree";
00326 e->usage =
00327 "Usage: database deltree <family> [keytree]\n"
00328 " Deletes a family or specific keytree within a family\n"
00329 " in the Asterisk database.\n";
00330 return NULL;
00331 case CLI_GENERATE:
00332 return NULL;
00333 }
00334
00335 if ((a->argc < 3) || (a->argc > 4))
00336 return CLI_SHOWUSAGE;
00337 if (a->argc == 4) {
00338 res = ast_db_deltree(a->argv[2], a->argv[3]);
00339 } else {
00340 res = ast_db_deltree(a->argv[2], NULL);
00341 }
00342 if (res < 0) {
00343 ast_cli(a->fd, "Database entries do not exist.\n");
00344 } else {
00345 ast_cli(a->fd, "%d database entries removed.\n",res);
00346 }
00347 return CLI_SUCCESS;
00348 }
00349
00350 static char *handle_cli_database_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00351 {
00352 char prefix[256];
00353 DBT key, data;
00354 char *keys, *values;
00355 int res;
00356 int pass;
00357 int counter = 0;
00358
00359 switch (cmd) {
00360 case CLI_INIT:
00361 e->command = "database show";
00362 e->usage =
00363 "Usage: database show [family [keytree]]\n"
00364 " Shows Asterisk database contents, optionally restricted\n"
00365 " to a given family, or family and keytree.\n";
00366 return NULL;
00367 case CLI_GENERATE:
00368 return NULL;
00369 }
00370
00371 if (a->argc == 4) {
00372
00373 snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
00374 } else if (a->argc == 3) {
00375
00376 snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
00377 } else if (a->argc == 2) {
00378
00379 prefix[0] = '\0';
00380 } else {
00381 return CLI_SHOWUSAGE;
00382 }
00383 ast_mutex_lock(&dblock);
00384 if (dbinit()) {
00385 ast_mutex_unlock(&dblock);
00386 ast_cli(a->fd, "Database unavailable\n");
00387 return CLI_SUCCESS;
00388 }
00389 memset(&key, 0, sizeof(key));
00390 memset(&data, 0, sizeof(data));
00391 pass = 0;
00392 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00393 if (key.size) {
00394 keys = key.data;
00395 keys[key.size - 1] = '\0';
00396 } else {
00397 keys = "<bad key>";
00398 }
00399 if (data.size) {
00400 values = data.data;
00401 values[data.size - 1]='\0';
00402 } else {
00403 values = "<bad value>";
00404 }
00405 if (keymatch(keys, prefix)) {
00406 ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
00407 counter++;
00408 }
00409 }
00410 ast_mutex_unlock(&dblock);
00411 ast_cli(a->fd, "%d results found.\n", counter);
00412 return CLI_SUCCESS;
00413 }
00414
00415 static char *handle_cli_database_showkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00416 {
00417 char suffix[256];
00418 DBT key, data;
00419 char *keys, *values;
00420 int res;
00421 int pass;
00422 int counter = 0;
00423
00424 switch (cmd) {
00425 case CLI_INIT:
00426 e->command = "database showkey";
00427 e->usage =
00428 "Usage: database showkey <keytree>\n"
00429 " Shows Asterisk database contents, restricted to a given key.\n";
00430 return NULL;
00431 case CLI_GENERATE:
00432 return NULL;
00433 }
00434
00435 if (a->argc == 3) {
00436
00437 snprintf(suffix, sizeof(suffix), "/%s", a->argv[2]);
00438 } else {
00439 return CLI_SHOWUSAGE;
00440 }
00441 ast_mutex_lock(&dblock);
00442 if (dbinit()) {
00443 ast_mutex_unlock(&dblock);
00444 ast_cli(a->fd, "Database unavailable\n");
00445 return CLI_SUCCESS;
00446 }
00447 memset(&key, 0, sizeof(key));
00448 memset(&data, 0, sizeof(data));
00449 pass = 0;
00450 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00451 if (key.size) {
00452 keys = key.data;
00453 keys[key.size - 1] = '\0';
00454 } else {
00455 keys = "<bad key>";
00456 }
00457 if (data.size) {
00458 values = data.data;
00459 values[data.size - 1]='\0';
00460 } else {
00461 values = "<bad value>";
00462 }
00463 if (subkeymatch(keys, suffix)) {
00464 ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
00465 counter++;
00466 }
00467 }
00468 ast_mutex_unlock(&dblock);
00469 ast_cli(a->fd, "%d results found.\n", counter);
00470 return CLI_SUCCESS;
00471 }
00472
00473 struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
00474 {
00475 char prefix[256];
00476 DBT key, data;
00477 char *keys, *values;
00478 int values_len;
00479 int res;
00480 int pass;
00481 struct ast_db_entry *last = NULL;
00482 struct ast_db_entry *cur, *ret=NULL;
00483
00484 if (!ast_strlen_zero(family)) {
00485 if (!ast_strlen_zero(keytree)) {
00486
00487 snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00488 } else {
00489
00490 snprintf(prefix, sizeof(prefix), "/%s", family);
00491 }
00492 } else {
00493 prefix[0] = '\0';
00494 }
00495 ast_mutex_lock(&dblock);
00496 if (dbinit()) {
00497 ast_mutex_unlock(&dblock);
00498 ast_log(LOG_WARNING, "Database unavailable\n");
00499 return NULL;
00500 }
00501 memset(&key, 0, sizeof(key));
00502 memset(&data, 0, sizeof(data));
00503 pass = 0;
00504 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00505 if (key.size) {
00506 keys = key.data;
00507 keys[key.size - 1] = '\0';
00508 } else {
00509 keys = "<bad key>";
00510 }
00511 if (data.size) {
00512 values = data.data;
00513 values[data.size - 1] = '\0';
00514 } else {
00515 values = "<bad value>";
00516 }
00517 values_len = strlen(values) + 1;
00518 if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
00519 cur->next = NULL;
00520 cur->key = cur->data + values_len;
00521 strcpy(cur->data, values);
00522 strcpy(cur->key, keys);
00523 if (last) {
00524 last->next = cur;
00525 } else {
00526 ret = cur;
00527 }
00528 last = cur;
00529 }
00530 }
00531 ast_mutex_unlock(&dblock);
00532 return ret;
00533 }
00534
00535 void ast_db_freetree(struct ast_db_entry *dbe)
00536 {
00537 struct ast_db_entry *last;
00538 while (dbe) {
00539 last = dbe;
00540 dbe = dbe->next;
00541 ast_free(last);
00542 }
00543 }
00544
00545 struct ast_cli_entry cli_database[] = {
00546 AST_CLI_DEFINE(handle_cli_database_show, "Shows database contents"),
00547 AST_CLI_DEFINE(handle_cli_database_showkey, "Shows database contents"),
00548 AST_CLI_DEFINE(handle_cli_database_get, "Gets database value"),
00549 AST_CLI_DEFINE(handle_cli_database_put, "Adds/updates database value"),
00550 AST_CLI_DEFINE(handle_cli_database_del, "Removes database key/value"),
00551 AST_CLI_DEFINE(handle_cli_database_deltree, "Removes database keytree/values")
00552 };
00553
00554 static int manager_dbput(struct mansession *s, const struct message *m)
00555 {
00556 const char *family = astman_get_header(m, "Family");
00557 const char *key = astman_get_header(m, "Key");
00558 const char *val = astman_get_header(m, "Val");
00559 int res;
00560
00561 if (ast_strlen_zero(family)) {
00562 astman_send_error(s, m, "No family specified");
00563 return 0;
00564 }
00565 if (ast_strlen_zero(key)) {
00566 astman_send_error(s, m, "No key specified");
00567 return 0;
00568 }
00569
00570 res = ast_db_put(family, key, S_OR(val, ""));
00571 if (res) {
00572 astman_send_error(s, m, "Failed to update entry");
00573 } else {
00574 astman_send_ack(s, m, "Updated database successfully");
00575 }
00576 return 0;
00577 }
00578
00579 static int manager_dbget(struct mansession *s, const struct message *m)
00580 {
00581 const char *id = astman_get_header(m,"ActionID");
00582 char idText[256] = "";
00583 const char *family = astman_get_header(m, "Family");
00584 const char *key = astman_get_header(m, "Key");
00585 char tmp[256];
00586 int res;
00587
00588 if (ast_strlen_zero(family)) {
00589 astman_send_error(s, m, "No family specified.");
00590 return 0;
00591 }
00592 if (ast_strlen_zero(key)) {
00593 astman_send_error(s, m, "No key specified.");
00594 return 0;
00595 }
00596
00597 if (!ast_strlen_zero(id))
00598 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
00599
00600 res = ast_db_get(family, key, tmp, sizeof(tmp));
00601 if (res) {
00602 astman_send_error(s, m, "Database entry not found");
00603 } else {
00604 astman_send_ack(s, m, "Result will follow");
00605 astman_append(s, "Event: DBGetResponse\r\n"
00606 "Family: %s\r\n"
00607 "Key: %s\r\n"
00608 "Val: %s\r\n"
00609 "%s"
00610 "\r\n",
00611 family, key, tmp, idText);
00612 }
00613 return 0;
00614 }
00615
00616 static int manager_dbdel(struct mansession *s, const struct message *m)
00617 {
00618 const char *family = astman_get_header(m, "Family");
00619 const char *key = astman_get_header(m, "Key");
00620 int res;
00621
00622 if (ast_strlen_zero(family)) {
00623 astman_send_error(s, m, "No family specified.");
00624 return 0;
00625 }
00626
00627 if (ast_strlen_zero(key)) {
00628 astman_send_error(s, m, "No key specified.");
00629 return 0;
00630 }
00631
00632 res = ast_db_del(family, key);
00633 if (res)
00634 astman_send_error(s, m, "Database entry not found");
00635 else
00636 astman_send_ack(s, m, "Key deleted successfully");
00637
00638 return 0;
00639 }
00640
00641 static int manager_dbdeltree(struct mansession *s, const struct message *m)
00642 {
00643 const char *family = astman_get_header(m, "Family");
00644 const char *key = astman_get_header(m, "Key");
00645 int res;
00646
00647 if (ast_strlen_zero(family)) {
00648 astman_send_error(s, m, "No family specified.");
00649 return 0;
00650 }
00651
00652 if (!ast_strlen_zero(key))
00653 res = ast_db_deltree(family, key);
00654 else
00655 res = ast_db_deltree(family, NULL);
00656
00657 if (res < 0)
00658 astman_send_error(s, m, "Database entry not found");
00659 else
00660 astman_send_ack(s, m, "Key tree deleted successfully");
00661
00662 return 0;
00663 }
00664
00665 int astdb_init(void)
00666 {
00667 dbinit();
00668 ast_cli_register_multiple(cli_database, sizeof(cli_database) / sizeof(struct ast_cli_entry));
00669 ast_manager_register("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget, "Get DB Entry");
00670 ast_manager_register("DBPut", EVENT_FLAG_SYSTEM, manager_dbput, "Put DB Entry");
00671 ast_manager_register("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel, "Delete DB Entry");
00672 ast_manager_register("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree, "Delete DB Tree");
00673 return 0;
00674 }