Support for withheld or missing CID string
[asterisk-bristuff.git] / main / db.c
blobef381c092a837c6f248f1ee8eb2e569a386cb0a7
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 <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/time.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include <unistd.h>
41 #include <dirent.h>
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"
56 #ifdef __CYGWIN__
57 #define dbopen __dbopen
58 #endif
60 static DB *astdb;
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, 0664, DB_BTREE, NULL))) {
66 ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB, strerror(errno));
67 return -1;
69 return 0;
73 static inline int keymatch(const char *key, const char *prefix)
75 int preflen = strlen(prefix);
76 if (!preflen)
77 return 1;
78 if (!strcasecmp(key, prefix))
79 return 1;
80 if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
81 if (key[preflen] == '/')
82 return 1;
84 return 0;
87 static inline int subkeymatch(const char *key, const char *suffix)
89 int suffixlen = strlen(suffix);
90 if (suffixlen) {
91 const char *subkey = key + strlen(key) - suffixlen;
92 if (subkey < key)
93 return 0;
94 if (!strcasecmp(subkey, suffix))
95 return 1;
97 return 0;
100 int ast_db_deltree(const char *family, const char *keytree)
102 char prefix[256];
103 DBT key, data;
104 char *keys;
105 int res;
106 int pass;
108 if (family) {
109 if (keytree) {
110 snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
111 } else {
112 snprintf(prefix, sizeof(prefix), "/%s", family);
114 } else if (keytree) {
115 return -1;
116 } else {
117 prefix[0] = '\0';
120 ast_mutex_lock(&dblock);
121 if (dbinit()) {
122 ast_mutex_unlock(&dblock);
123 return -1;
126 memset(&key, 0, sizeof(key));
127 memset(&data, 0, sizeof(data));
128 pass = 0;
129 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
130 if (key.size) {
131 keys = key.data;
132 keys[key.size - 1] = '\0';
133 } else {
134 keys = "<bad key>";
136 if (keymatch(keys, prefix)) {
137 astdb->del(astdb, &key, 0);
140 astdb->sync(astdb, 0);
141 ast_mutex_unlock(&dblock);
142 return 0;
145 int ast_db_put(const char *family, const char *keys, char *value)
147 char fullkey[256];
148 DBT key, data;
149 int res, fullkeylen;
151 ast_mutex_lock(&dblock);
152 if (dbinit()) {
153 ast_mutex_unlock(&dblock);
154 return -1;
157 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
158 memset(&key, 0, sizeof(key));
159 memset(&data, 0, sizeof(data));
160 key.data = fullkey;
161 key.size = fullkeylen + 1;
162 data.data = 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);
167 if (res)
168 ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
169 return res;
172 int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
174 char fullkey[256] = "";
175 DBT key, data;
176 int res, fullkeylen;
178 ast_mutex_lock(&dblock);
179 if (dbinit()) {
180 ast_mutex_unlock(&dblock);
181 return -1;
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);
188 key.data = fullkey;
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 */
196 if (res) {
197 if (option_debug)
198 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
199 } else {
200 #if 0
201 printf("Got value of size %d\n", data.size);
202 #endif
203 if (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);
207 } else {
208 ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
211 return res;
214 int ast_db_del(const char *family, const char *keys)
216 char fullkey[256];
217 DBT key;
218 int res, fullkeylen;
220 ast_mutex_lock(&dblock);
221 if (dbinit()) {
222 ast_mutex_unlock(&dblock);
223 return -1;
226 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
227 memset(&key, 0, sizeof(key));
228 key.data = fullkey;
229 key.size = fullkeylen + 1;
231 res = astdb->del(astdb, &key, 0);
232 astdb->sync(astdb, 0);
234 ast_mutex_unlock(&dblock);
236 if (res && option_debug)
237 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
238 return res;
241 static int database_put(int fd, int argc, char *argv[])
243 int res;
244 if (argc != 5)
245 return RESULT_SHOWUSAGE;
246 res = ast_db_put(argv[2], argv[3], argv[4]);
247 if (res) {
248 ast_cli(fd, "Failed to update entry\n");
249 } else {
250 ast_cli(fd, "Updated database successfully\n");
252 return RESULT_SUCCESS;
255 static int database_get(int fd, int argc, char *argv[])
257 int res;
258 char tmp[256];
259 if (argc != 4)
260 return RESULT_SHOWUSAGE;
261 res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
262 if (res) {
263 ast_cli(fd, "Database entry not found.\n");
264 } else {
265 ast_cli(fd, "Value: %s\n", tmp);
267 return RESULT_SUCCESS;
270 static int database_del(int fd, int argc, char *argv[])
272 int res;
273 if (argc != 4)
274 return RESULT_SHOWUSAGE;
275 res = ast_db_del(argv[2], argv[3]);
276 if (res) {
277 ast_cli(fd, "Database entry does not exist.\n");
278 } else {
279 ast_cli(fd, "Database entry removed.\n");
281 return RESULT_SUCCESS;
284 static int database_deltree(int fd, int argc, char *argv[])
286 int res;
287 if ((argc < 3) || (argc > 4))
288 return RESULT_SHOWUSAGE;
289 if (argc == 4) {
290 res = ast_db_deltree(argv[2], argv[3]);
291 } else {
292 res = ast_db_deltree(argv[2], NULL);
294 if (res) {
295 ast_cli(fd, "Database entries do not exist.\n");
296 } else {
297 ast_cli(fd, "Database entries removed.\n");
299 return RESULT_SUCCESS;
302 static int database_show(int fd, int argc, char *argv[])
304 char prefix[256];
305 DBT key, data;
306 char *keys, *values;
307 int res;
308 int pass;
310 if (argc == 4) {
311 /* Family and key tree */
312 snprintf(prefix, sizeof(prefix), "/%s/%s", argv[2], argv[3]);
313 } else if (argc == 3) {
314 /* Family only */
315 snprintf(prefix, sizeof(prefix), "/%s", argv[2]);
316 } else if (argc == 2) {
317 /* Neither */
318 prefix[0] = '\0';
319 } else {
320 return RESULT_SHOWUSAGE;
322 ast_mutex_lock(&dblock);
323 if (dbinit()) {
324 ast_mutex_unlock(&dblock);
325 ast_cli(fd, "Database unavailable\n");
326 return RESULT_SUCCESS;
328 memset(&key, 0, sizeof(key));
329 memset(&data, 0, sizeof(data));
330 pass = 0;
331 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
332 if (key.size) {
333 keys = key.data;
334 keys[key.size - 1] = '\0';
335 } else {
336 keys = "<bad key>";
338 if (data.size) {
339 values = data.data;
340 values[data.size - 1]='\0';
341 } else {
342 values = "<bad value>";
344 if (keymatch(keys, prefix)) {
345 ast_cli(fd, "%-50s: %-25s\n", keys, values);
348 ast_mutex_unlock(&dblock);
349 return RESULT_SUCCESS;
352 static int database_showkey(int fd, int argc, char *argv[])
354 char suffix[256];
355 DBT key, data;
356 char *keys, *values;
357 int res;
358 int pass;
360 if (argc == 3) {
361 /* Key only */
362 snprintf(suffix, sizeof(suffix), "/%s", argv[2]);
363 } else {
364 return RESULT_SHOWUSAGE;
366 ast_mutex_lock(&dblock);
367 if (dbinit()) {
368 ast_mutex_unlock(&dblock);
369 ast_cli(fd, "Database unavailable\n");
370 return RESULT_SUCCESS;
372 memset(&key, 0, sizeof(key));
373 memset(&data, 0, sizeof(data));
374 pass = 0;
375 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
376 if (key.size) {
377 keys = key.data;
378 keys[key.size - 1] = '\0';
379 } else {
380 keys = "<bad key>";
382 if (data.size) {
383 values = data.data;
384 values[data.size - 1]='\0';
385 } else {
386 values = "<bad value>";
388 if (subkeymatch(keys, suffix)) {
389 ast_cli(fd, "%-50s: %-25s\n", keys, values);
392 ast_mutex_unlock(&dblock);
393 return RESULT_SUCCESS;
396 struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
398 char prefix[256];
399 DBT key, data;
400 char *keys, *values;
401 int values_len;
402 int res;
403 int pass;
404 struct ast_db_entry *last = NULL;
405 struct ast_db_entry *cur, *ret=NULL;
407 if (!ast_strlen_zero(family)) {
408 if (!ast_strlen_zero(keytree)) {
409 /* Family and key tree */
410 snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix);
411 } else {
412 /* Family only */
413 snprintf(prefix, sizeof(prefix), "/%s", family);
415 } else {
416 prefix[0] = '\0';
418 ast_mutex_lock(&dblock);
419 if (dbinit()) {
420 ast_mutex_unlock(&dblock);
421 ast_log(LOG_WARNING, "Database unavailable\n");
422 return NULL;
424 memset(&key, 0, sizeof(key));
425 memset(&data, 0, sizeof(data));
426 pass = 0;
427 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
428 if (key.size) {
429 keys = key.data;
430 keys[key.size - 1] = '\0';
431 } else {
432 keys = "<bad key>";
434 if (data.size) {
435 values = data.data;
436 values[data.size - 1] = '\0';
437 } else {
438 values = "<bad value>";
440 values_len = strlen(values) + 1;
441 if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
442 cur->next = NULL;
443 cur->key = cur->data + values_len;
444 strcpy(cur->data, values);
445 strcpy(cur->key, keys);
446 if (last) {
447 last->next = cur;
448 } else {
449 ret = cur;
451 last = cur;
454 ast_mutex_unlock(&dblock);
455 return ret;
458 void ast_db_freetree(struct ast_db_entry *dbe)
460 struct ast_db_entry *last;
461 while (dbe) {
462 last = dbe;
463 dbe = dbe->next;
464 free(last);
468 static char database_show_usage[] =
469 "Usage: database show [family [keytree]]\n"
470 " Shows Asterisk database contents, optionally restricted\n"
471 "to a given family, or family and keytree.\n";
473 static char database_showkey_usage[] =
474 "Usage: database showkey <keytree>\n"
475 " Shows Asterisk database contents, restricted to a given key.\n";
477 static char database_put_usage[] =
478 "Usage: database put <family> <key> <value>\n"
479 " Adds or updates an entry in the Asterisk database for\n"
480 "a given family, key, and value.\n";
482 static char database_get_usage[] =
483 "Usage: database get <family> <key>\n"
484 " Retrieves an entry in the Asterisk database for a given\n"
485 "family and key.\n";
487 static char database_del_usage[] =
488 "Usage: database del <family> <key>\n"
489 " Deletes an entry in the Asterisk database for a given\n"
490 "family and key.\n";
492 static char database_deltree_usage[] =
493 "Usage: database deltree <family> [keytree]\n"
494 " Deletes a family or specific keytree within a family\n"
495 "in the Asterisk database.\n";
497 struct ast_cli_entry cli_database[] = {
498 { { "database", "show", NULL },
499 database_show, "Shows database contents",
500 database_show_usage },
502 { { "database", "showkey", NULL },
503 database_showkey, "Shows database contents",
504 database_showkey_usage },
506 { { "database", "get", NULL },
507 database_get, "Gets database value",
508 database_get_usage },
510 { { "database", "put", NULL },
511 database_put, "Adds/updates database value",
512 database_put_usage },
514 { { "database", "del", NULL },
515 database_del, "Removes database key/value",
516 database_del_usage },
518 { { "database", "deltree", NULL },
519 database_deltree, "Removes database keytree/values",
520 database_deltree_usage },
523 static int manager_dbput(struct mansession *s, const struct message *m)
525 const char *family = astman_get_header(m, "Family");
526 const char *key = astman_get_header(m, "Key");
527 const char *val = astman_get_header(m, "Val");
528 int res;
530 if (ast_strlen_zero(family)) {
531 astman_send_error(s, m, "No family specified");
532 return 0;
534 if (ast_strlen_zero(key)) {
535 astman_send_error(s, m, "No key specified");
536 return 0;
539 res = ast_db_put(family, key, (char *) S_OR(val, ""));
540 if (res) {
541 astman_send_error(s, m, "Failed to update entry");
542 } else {
543 astman_send_ack(s, m, "Updated database successfully");
545 return 0;
548 static int manager_dbget(struct mansession *s, const struct message *m)
550 const char *id = astman_get_header(m,"ActionID");
551 char idText[256] = "";
552 const char *family = astman_get_header(m, "Family");
553 const char *key = astman_get_header(m, "Key");
554 char tmp[256];
555 int res;
557 if (ast_strlen_zero(family)) {
558 astman_send_error(s, m, "No family specified.");
559 return 0;
561 if (ast_strlen_zero(key)) {
562 astman_send_error(s, m, "No key specified.");
563 return 0;
566 if (!ast_strlen_zero(id))
567 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
569 res = ast_db_get(family, key, tmp, sizeof(tmp));
570 if (res) {
571 astman_send_error(s, m, "Database entry not found");
572 } else {
573 astman_send_ack(s, m, "Result will follow");
574 astman_append(s, "Event: DBGetResponse\r\n"
575 "Family: %s\r\n"
576 "Key: %s\r\n"
577 "Val: %s\r\n"
578 "%s"
579 "\r\n",
580 family, key, tmp, idText);
582 return 0;
585 int astdb_init(void)
587 dbinit();
588 ast_cli_register_multiple(cli_database, sizeof(cli_database) / sizeof(struct ast_cli_entry));
589 ast_manager_register("DBGet", EVENT_FLAG_SYSTEM, manager_dbget, "Get DB Entry");
590 ast_manager_register("DBPut", EVENT_FLAG_SYSTEM, manager_dbput, "Put DB Entry");
591 return 0;