Remove status_response callbacks where they are not needed.
[asterisk/asterisk.git] / main / db.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief ASTdb Management
22  *
23  * \author Mark Spencer <markster@digium.com> 
24  *
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 
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
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>
39
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"
50
51 /*** DOCUMENTATION
52         <manager name="DBGet" language="en_US">
53                 <synopsis>
54                         Get DB Entry.
55                 </synopsis>
56                 <syntax>
57                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
58                         <parameter name="Family" required="true" />
59                         <parameter name="Key" required="true" />
60                 </syntax>
61                 <description>
62                 </description>
63         </manager>
64         <manager name="DBPut" language="en_US">
65                 <synopsis>
66                         Put DB entry.
67                 </synopsis>
68                 <syntax>
69                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
70                         <parameter name="Family" required="true" />
71                         <parameter name="Key" required="true" />
72                         <parameter name="Val" />
73                 </syntax>
74                 <description>
75                 </description>
76         </manager>
77         <manager name="DBDel" language="en_US">
78                 <synopsis>
79                         Delete DB entry.
80                 </synopsis>
81                 <syntax>
82                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
83                         <parameter name="Family" required="true" />
84                         <parameter name="Key" required="true" />
85                 </syntax>
86                 <description>
87                 </description>
88         </manager>
89         <manager name="DBDelTree" language="en_US">
90                 <synopsis>
91                         Delete DB Tree.
92                 </synopsis>
93                 <syntax>
94                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
95                         <parameter name="Family" required="true" />
96                         <parameter name="Key" />
97                 </syntax>
98                 <description>
99                 </description>
100         </manager>
101  ***/
102
103 static DB *astdb;
104 AST_MUTEX_DEFINE_STATIC(dblock);
105
106 static int dbinit(void) 
107 {
108         if (!astdb && !(astdb = dbopen(ast_config_AST_DB, O_CREAT | O_RDWR, AST_FILE_MODE, DB_BTREE, NULL))) {
109                 ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB, strerror(errno));
110                 return -1;
111         }
112         return 0;
113 }
114
115
116 static inline int keymatch(const char *key, const char *prefix)
117 {
118         int preflen = strlen(prefix);
119         if (!preflen)
120                 return 1;
121         if (!strcasecmp(key, prefix))
122                 return 1;
123         if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
124                 if (key[preflen] == '/')
125                         return 1;
126         }
127         return 0;
128 }
129
130 static inline int subkeymatch(const char *key, const char *suffix)
131 {
132         int suffixlen = strlen(suffix);
133         if (suffixlen) {
134                 const char *subkey = key + strlen(key) - suffixlen;
135                 if (subkey < key)
136                         return 0;
137                 if (!strcasecmp(subkey, suffix))
138                         return 1;
139         }
140         return 0;
141 }
142
143 int ast_db_deltree(const char *family, const char *keytree)
144 {
145         char prefix[256];
146         DBT key, data;
147         char *keys;
148         int res;
149         int pass;
150         int counter = 0;
151         
152         if (family) {
153                 if (keytree) {
154                         snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
155                 } else {
156                         snprintf(prefix, sizeof(prefix), "/%s", family);
157                 }
158         } else if (keytree) {
159                 return -1;
160         } else {
161                 prefix[0] = '\0';
162         }
163         
164         ast_mutex_lock(&dblock);
165         if (dbinit()) {
166                 ast_mutex_unlock(&dblock);
167                 return -1;
168         }
169         
170         memset(&key, 0, sizeof(key));
171         memset(&data, 0, sizeof(data));
172         pass = 0;
173         while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
174                 if (key.size) {
175                         keys = key.data;
176                         keys[key.size - 1] = '\0';
177                 } else {
178                         keys = "<bad key>";
179                 }
180                 if (keymatch(keys, prefix)) {
181                         astdb->del(astdb, &key, 0);
182                         counter++;
183                 }
184         }
185         astdb->sync(astdb, 0);
186         ast_mutex_unlock(&dblock);
187         return counter;
188 }
189
190 int ast_db_put(const char *family, const char *keys, const char *value)
191 {
192         char fullkey[256];
193         DBT key, data;
194         int res, fullkeylen;
195
196         ast_mutex_lock(&dblock);
197         if (dbinit()) {
198                 ast_mutex_unlock(&dblock);
199                 return -1;
200         }
201
202         fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
203         memset(&key, 0, sizeof(key));
204         memset(&data, 0, sizeof(data));
205         key.data = fullkey;
206         key.size = fullkeylen + 1;
207         data.data = (char *) value;
208         data.size = strlen(value) + 1;
209         res = astdb->put(astdb, &key, &data, 0);
210         astdb->sync(astdb, 0);
211         ast_mutex_unlock(&dblock);
212         if (res)
213                 ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
214         return res;
215 }
216
217 int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
218 {
219         char fullkey[256] = "";
220         DBT key, data;
221         int res, fullkeylen;
222
223         ast_mutex_lock(&dblock);
224         if (dbinit()) {
225                 ast_mutex_unlock(&dblock);
226                 return -1;
227         }
228
229         fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
230         memset(&key, 0, sizeof(key));
231         memset(&data, 0, sizeof(data));
232         memset(value, 0, valuelen);
233         key.data = fullkey;
234         key.size = fullkeylen + 1;
235
236         res = astdb->get(astdb, &key, &data, 0);
237
238         /* Be sure to NULL terminate our data either way */
239         if (res) {
240                 ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
241         } else {
242 #if 0
243                 printf("Got value of size %d\n", data.size);
244 #endif
245                 if (data.size) {
246                         ((char *)data.data)[data.size - 1] = '\0';
247                         /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
248                         ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
249                 } else {
250                         ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
251                 }
252         }
253
254         /* Data is not fully isolated for concurrency, so the lock must be extended
255          * to after the copy to the output buffer. */
256         ast_mutex_unlock(&dblock);
257
258         return res;
259 }
260
261 int ast_db_del(const char *family, const char *keys)
262 {
263         char fullkey[256];
264         DBT key;
265         int res, fullkeylen;
266
267         ast_mutex_lock(&dblock);
268         if (dbinit()) {
269                 ast_mutex_unlock(&dblock);
270                 return -1;
271         }
272         
273         fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
274         memset(&key, 0, sizeof(key));
275         key.data = fullkey;
276         key.size = fullkeylen + 1;
277         
278         res = astdb->del(astdb, &key, 0);
279         astdb->sync(astdb, 0);
280         
281         ast_mutex_unlock(&dblock);
282
283         if (res) {
284                 ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
285         }
286         return res;
287 }
288
289 static char *handle_cli_database_put(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
290 {
291         int res;
292
293         switch (cmd) {
294         case CLI_INIT:
295                 e->command = "database put";
296                 e->usage =
297                         "Usage: database put <family> <key> <value>\n"
298                         "       Adds or updates an entry in the Asterisk database for\n"
299                         "       a given family, key, and value.\n";
300                 return NULL;
301         case CLI_GENERATE:
302                 return NULL;
303         }
304
305         if (a->argc != 5)
306                 return CLI_SHOWUSAGE;
307         res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
308         if (res)  {
309                 ast_cli(a->fd, "Failed to update entry\n");
310         } else {
311                 ast_cli(a->fd, "Updated database successfully\n");
312         }
313         return CLI_SUCCESS;
314 }
315
316 static char *handle_cli_database_get(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
317 {
318         int res;
319         char tmp[256];
320
321         switch (cmd) {
322         case CLI_INIT:
323                 e->command = "database get";
324                 e->usage =
325                         "Usage: database get <family> <key>\n"
326                         "       Retrieves an entry in the Asterisk database for a given\n"
327                         "       family and key.\n";
328                 return NULL;
329         case CLI_GENERATE:
330                 return NULL;
331         }
332
333         if (a->argc != 4)
334                 return CLI_SHOWUSAGE;
335         res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
336         if (res) {
337                 ast_cli(a->fd, "Database entry not found.\n");
338         } else {
339                 ast_cli(a->fd, "Value: %s\n", tmp);
340         }
341         return CLI_SUCCESS;
342 }
343
344 static char *handle_cli_database_del(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
345 {
346         int res;
347
348         switch (cmd) {
349         case CLI_INIT:
350                 e->command = "database del";
351                 e->usage =
352                         "Usage: database del <family> <key>\n"
353                         "       Deletes an entry in the Asterisk database for a given\n"
354                         "       family and key.\n";
355                 return NULL;
356         case CLI_GENERATE:
357                 return NULL;
358         }
359
360         if (a->argc != 4)
361                 return CLI_SHOWUSAGE;
362         res = ast_db_del(a->argv[2], a->argv[3]);
363         if (res) {
364                 ast_cli(a->fd, "Database entry does not exist.\n");
365         } else {
366                 ast_cli(a->fd, "Database entry removed.\n");
367         }
368         return CLI_SUCCESS;
369 }
370
371 static char *handle_cli_database_deltree(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
372 {
373         int res;
374
375         switch (cmd) {
376         case CLI_INIT:
377                 e->command = "database deltree";
378                 e->usage =
379                         "Usage: database deltree <family> [keytree]\n"
380                         "       Deletes a family or specific keytree within a family\n"
381                         "       in the Asterisk database.\n";
382                 return NULL;
383         case CLI_GENERATE:
384                 return NULL;
385         }
386
387         if ((a->argc < 3) || (a->argc > 4))
388                 return CLI_SHOWUSAGE;
389         if (a->argc == 4) {
390                 res = ast_db_deltree(a->argv[2], a->argv[3]);
391         } else {
392                 res = ast_db_deltree(a->argv[2], NULL);
393         }
394         if (res < 0) {
395                 ast_cli(a->fd, "Database entries do not exist.\n");
396         } else {
397                 ast_cli(a->fd, "%d database entries removed.\n",res);
398         }
399         return CLI_SUCCESS;
400 }
401
402 static char *handle_cli_database_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
403 {
404         char prefix[256];
405         DBT key, data;
406         char *keys, *values;
407         int res;
408         int pass;
409         int counter = 0;
410
411         switch (cmd) {
412         case CLI_INIT:
413                 e->command = "database show";
414                 e->usage =
415                         "Usage: database show [family [keytree]]\n"
416                         "       Shows Asterisk database contents, optionally restricted\n"
417                         "       to a given family, or family and keytree.\n";
418                 return NULL;
419         case CLI_GENERATE:
420                 return NULL;
421         }
422
423         if (a->argc == 4) {
424                 /* Family and key tree */
425                 snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
426         } else if (a->argc == 3) {
427                 /* Family only */
428                 snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
429         } else if (a->argc == 2) {
430                 /* Neither */
431                 prefix[0] = '\0';
432         } else {
433                 return CLI_SHOWUSAGE;
434         }
435         ast_mutex_lock(&dblock);
436         if (dbinit()) {
437                 ast_mutex_unlock(&dblock);
438                 ast_cli(a->fd, "Database unavailable\n");
439                 return CLI_SUCCESS;     
440         }
441         memset(&key, 0, sizeof(key));
442         memset(&data, 0, sizeof(data));
443         pass = 0;
444         while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
445                 if (key.size) {
446                         keys = key.data;
447                         keys[key.size - 1] = '\0';
448                 } else {
449                         keys = "<bad key>";
450                 }
451                 if (data.size) {
452                         values = data.data;
453                         values[data.size - 1]='\0';
454                 } else {
455                         values = "<bad value>";
456                 }
457                 if (keymatch(keys, prefix)) {
458                         ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
459                         counter++;
460                 }
461         }
462         ast_mutex_unlock(&dblock);
463         ast_cli(a->fd, "%d results found.\n", counter);
464         return CLI_SUCCESS;     
465 }
466
467 static char *handle_cli_database_showkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
468 {
469         char suffix[256];
470         DBT key, data;
471         char *keys, *values;
472         int res;
473         int pass;
474         int counter = 0;
475
476         switch (cmd) {
477         case CLI_INIT:
478                 e->command = "database showkey";
479                 e->usage =
480                         "Usage: database showkey <keytree>\n"
481                         "       Shows Asterisk database contents, restricted to a given key.\n";
482                 return NULL;
483         case CLI_GENERATE:
484                 return NULL;
485         }
486
487         if (a->argc == 3) {
488                 /* Key only */
489                 snprintf(suffix, sizeof(suffix), "/%s", a->argv[2]);
490         } else {
491                 return CLI_SHOWUSAGE;
492         }
493         ast_mutex_lock(&dblock);
494         if (dbinit()) {
495                 ast_mutex_unlock(&dblock);
496                 ast_cli(a->fd, "Database unavailable\n");
497                 return CLI_SUCCESS;     
498         }
499         memset(&key, 0, sizeof(key));
500         memset(&data, 0, sizeof(data));
501         pass = 0;
502         while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
503                 if (key.size) {
504                         keys = key.data;
505                         keys[key.size - 1] = '\0';
506                 } else {
507                         keys = "<bad key>";
508                 }
509                 if (data.size) {
510                         values = data.data;
511                         values[data.size - 1]='\0';
512                 } else {
513                         values = "<bad value>";
514                 }
515                 if (subkeymatch(keys, suffix)) {
516                         ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
517                         counter++;
518                 }
519         }
520         ast_mutex_unlock(&dblock);
521         ast_cli(a->fd, "%d results found.\n", counter);
522         return CLI_SUCCESS;     
523 }
524
525 struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
526 {
527         char prefix[256];
528         DBT key, data;
529         char *keys, *values;
530         int values_len;
531         int res;
532         int pass;
533         struct ast_db_entry *last = NULL;
534         struct ast_db_entry *cur, *ret=NULL;
535
536         if (!ast_strlen_zero(family)) {
537                 if (!ast_strlen_zero(keytree)) {
538                         /* Family and key tree */
539                         snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
540                 } else {
541                         /* Family only */
542                         snprintf(prefix, sizeof(prefix), "/%s", family);
543                 }
544         } else {
545                 prefix[0] = '\0';
546         }
547         ast_mutex_lock(&dblock);
548         if (dbinit()) {
549                 ast_mutex_unlock(&dblock);
550                 ast_log(LOG_WARNING, "Database unavailable\n");
551                 return NULL;    
552         }
553         memset(&key, 0, sizeof(key));
554         memset(&data, 0, sizeof(data));
555         pass = 0;
556         while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
557                 if (key.size) {
558                         keys = key.data;
559                         keys[key.size - 1] = '\0';
560                 } else {
561                         keys = "<bad key>";
562                 }
563                 if (data.size) {
564                         values = data.data;
565                         values[data.size - 1] = '\0';
566                 } else {
567                         values = "<bad value>";
568                 }
569                 values_len = strlen(values) + 1;
570                 if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
571                         cur->next = NULL;
572                         cur->key = cur->data + values_len;
573                         strcpy(cur->data, values);
574                         strcpy(cur->key, keys);
575                         if (last) {
576                                 last->next = cur;
577                         } else {
578                                 ret = cur;
579                         }
580                         last = cur;
581                 }
582         }
583         ast_mutex_unlock(&dblock);
584         return ret;     
585 }
586
587 void ast_db_freetree(struct ast_db_entry *dbe)
588 {
589         struct ast_db_entry *last;
590         while (dbe) {
591                 last = dbe;
592                 dbe = dbe->next;
593                 ast_free(last);
594         }
595 }
596
597 static struct ast_cli_entry cli_database[] = {
598         AST_CLI_DEFINE(handle_cli_database_show,    "Shows database contents"),
599         AST_CLI_DEFINE(handle_cli_database_showkey, "Shows database contents"),
600         AST_CLI_DEFINE(handle_cli_database_get,     "Gets database value"),
601         AST_CLI_DEFINE(handle_cli_database_put,     "Adds/updates database value"),
602         AST_CLI_DEFINE(handle_cli_database_del,     "Removes database key/value"),
603         AST_CLI_DEFINE(handle_cli_database_deltree, "Removes database keytree/values")
604 };
605
606 static int manager_dbput(struct mansession *s, const struct message *m)
607 {
608         const char *family = astman_get_header(m, "Family");
609         const char *key = astman_get_header(m, "Key");
610         const char *val = astman_get_header(m, "Val");
611         int res;
612
613         if (ast_strlen_zero(family)) {
614                 astman_send_error(s, m, "No family specified");
615                 return 0;
616         }
617         if (ast_strlen_zero(key)) {
618                 astman_send_error(s, m, "No key specified");
619                 return 0;
620         }
621
622         res = ast_db_put(family, key, S_OR(val, ""));
623         if (res) {
624                 astman_send_error(s, m, "Failed to update entry");
625         } else {
626                 astman_send_ack(s, m, "Updated database successfully");
627         }
628         return 0;
629 }
630
631 static int manager_dbget(struct mansession *s, const struct message *m)
632 {
633         const char *id = astman_get_header(m,"ActionID");
634         char idText[256] = "";
635         const char *family = astman_get_header(m, "Family");
636         const char *key = astman_get_header(m, "Key");
637         char tmp[256];
638         int res;
639
640         if (ast_strlen_zero(family)) {
641                 astman_send_error(s, m, "No family specified.");
642                 return 0;
643         }
644         if (ast_strlen_zero(key)) {
645                 astman_send_error(s, m, "No key specified.");
646                 return 0;
647         }
648
649         if (!ast_strlen_zero(id))
650                 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
651
652         res = ast_db_get(family, key, tmp, sizeof(tmp));
653         if (res) {
654                 astman_send_error(s, m, "Database entry not found");
655         } else {
656                 astman_send_ack(s, m, "Result will follow");
657                 astman_append(s, "Event: DBGetResponse\r\n"
658                                 "Family: %s\r\n"
659                                 "Key: %s\r\n"
660                                 "Val: %s\r\n"
661                                 "%s"
662                                 "\r\n",
663                                 family, key, tmp, idText);
664         }
665         return 0;
666 }
667
668 static int manager_dbdel(struct mansession *s, const struct message *m)
669 {
670         const char *family = astman_get_header(m, "Family");
671         const char *key = astman_get_header(m, "Key");
672         int res;
673
674         if (ast_strlen_zero(family)) {
675                 astman_send_error(s, m, "No family specified.");
676                 return 0;
677         }
678
679         if (ast_strlen_zero(key)) {
680                 astman_send_error(s, m, "No key specified.");
681                 return 0;
682         }
683
684         res = ast_db_del(family, key);
685         if (res)
686                 astman_send_error(s, m, "Database entry not found");
687         else
688                 astman_send_ack(s, m, "Key deleted successfully");
689
690         return 0;
691 }
692
693 static int manager_dbdeltree(struct mansession *s, const struct message *m)
694 {
695         const char *family = astman_get_header(m, "Family");
696         const char *key = astman_get_header(m, "Key");
697         int res;
698
699         if (ast_strlen_zero(family)) {
700                 astman_send_error(s, m, "No family specified.");
701                 return 0;
702         }
703
704         if (!ast_strlen_zero(key))
705                 res = ast_db_deltree(family, key);
706         else
707                 res = ast_db_deltree(family, NULL);
708
709         if (res < 0)
710                 astman_send_error(s, m, "Database entry not found");
711         else
712                 astman_send_ack(s, m, "Key tree deleted successfully");
713         
714         return 0;
715 }
716
717 int astdb_init(void)
718 {
719         dbinit();
720         ast_cli_register_multiple(cli_database, ARRAY_LEN(cli_database));
721         ast_manager_register_xml("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget);
722         ast_manager_register_xml("DBPut", EVENT_FLAG_SYSTEM, manager_dbput);
723         ast_manager_register_xml("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel);
724         ast_manager_register_xml("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree);
725         return 0;
726 }