Debian Codegraph @

funcs/func_db.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2005-2006, Russell Bryant <[email protected]>
5 *
6 * func_db.c adapted from the old app_db.c, copyright by the following people
7 * Copyright (C) 2005, Mark Spencer <[email protected]>
8 * Copyright (C) 2003, Jefferson Noxon <[email protected]>
9 *
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
15 *
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
19 */
20
21 /*! \file
22 *
23 * \brief Functions for interaction with the Asterisk database
24 *
25 * \author Russell Bryant <[email protected]>
26 *
27 * \ingroup functions
28 */
29
30 /*** MODULEINFO
31 <support_level>core</support_level>
32 ***/
33
34 #include "asterisk.h"
35
36 #include <regex.h>
37
38 #include "asterisk/module.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/app.h"
43 #include "asterisk/astdb.h"
44
45 /*** DOCUMENTATION
46 <function name="DB" language="en_US">
47 <since>
48 <version>1.2.0</version>
49 </since>
50 <synopsis>
51 Read from or write to the Asterisk database.
52 </synopsis>
53 <syntax argsep="/">
54 <parameter name="family" required="true" />
55 <parameter name="key" required="true" />
56 </syntax>
57 <description>
58 <para>This function will read from or write a value to the Asterisk database. On a
59 read, this function returns the corresponding value from the database, or blank
60 if it does not exist. Reading a database value will also set the variable
61 DB_RESULT. If you wish to find out if an entry exists, use the DB_EXISTS
62 function.</para>
63 </description>
64 <see-also>
65 <ref type="application">DBdel</ref>
66 <ref type="function">DB_DELETE</ref>
67 <ref type="application">DBdeltree</ref>
68 <ref type="function">DB_EXISTS</ref>
69 </see-also>
70 </function>
71 <function name="DB_EXISTS" language="en_US">
72 <since>
73 <version>1.2.0</version>
74 </since>
75 <synopsis>
76 Check to see if a key exists in the Asterisk database.
77 </synopsis>
78 <syntax argsep="/">
79 <parameter name="family" required="true" />
80 <parameter name="key" required="true" />
81 </syntax>
82 <description>
83 <para>This function will check to see if a key exists in the Asterisk
84 database. If it exists, the function will return <literal>1</literal>. If not,
85 it will return <literal>0</literal>. Checking for existence of a database key will
86 also set the variable DB_RESULT to the key's value if it exists.</para>
87 </description>
88 <see-also>
89 <ref type="function">DB</ref>
90 </see-also>
91 </function>
92 <function name="DB_KEYS" language="en_US">
93 <since>
94 <version>10.0.0</version>
95 </since>
96 <synopsis>
97 Obtain a list of keys within the Asterisk database.
98 </synopsis>
99 <syntax>
100 <parameter name="prefix" />
101 </syntax>
102 <description>
103 <para>This function will return a comma-separated list of keys existing
104 at the prefix specified within the Asterisk database. If no argument is
105 provided, then a list of key families will be returned.</para>
106 </description>
107 <see-also>
108 <ref type="function">DB_KEYCOUNT</ref>
109 </see-also>
110 </function>
111 <function name="DB_KEYCOUNT" language="en_US">
112 <since>
113 <version>16.26.0</version>
114 <version>18.12.0</version>
115 <version>19.4.0</version>
116 </since>
117 <synopsis>
118 Obtain the number of keys at a prefix within the Asterisk database.
119 </synopsis>
120 <syntax>
121 <parameter name="prefix" />
122 </syntax>
123 <description>
124 <para>This function will return the number of keys that exist
125 at the prefix specified within the Asterisk database. If no argument is
126 provided, then the number of all key families will be returned.</para>
127 </description>
128 <see-also>
129 <ref type="function">DB_KEYS</ref>
130 </see-also>
131 </function>
132 <function name="DB_DELETE" language="en_US">
133 <since>
134 <version>1.4.0</version>
135 </since>
136 <synopsis>
137 Return a value from the database and delete it.
138 </synopsis>
139 <syntax argsep="/">
140 <parameter name="family" required="true" />
141 <parameter name="key" required="true" />
142 </syntax>
143 <description>
144 <para>This function will retrieve a value from the Asterisk database
145 and then remove that key from the database. <variable>DB_RESULT</variable>
146 will be set to the key's value if it exists.</para>
147 <note>
148 <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
149 is set to <literal>no</literal>, this function can only be read from the
150 dialplan, and not directly from external protocols. It can, however, be
151 executed as a write operation (<literal>DB_DELETE(family, key)=ignored</literal>)</para>
152 </note>
153 </description>
154 <see-also>
155 <ref type="application">DBdel</ref>
156 <ref type="function">DB</ref>
157 <ref type="application">DBdeltree</ref>
158 </see-also>
159 </function>
160 ***/
161
162 static int function_db_read(struct ast_channel *chan, const char *cmd,
163 char *parse, char *buf, size_t len)
164 {
165 AST_DECLARE_APP_ARGS(args,
166 AST_APP_ARG(family);
167 AST_APP_ARG(key);
168 );
169
170 buf[0] = '\0';
171
172 if (ast_strlen_zero(parse)) {
173 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
174 return -1;
175 }
176
177 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
178
179 if (args.argc < 2) {
180 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
181 return -1;
182 }
183
184 if (ast_db_get(args.family, args.key, buf, len - 1)) {
185 ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
186 } else {
187 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
188 }
189
190 return 0;
191 }
192
193 static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse,
194 const char *value)
195 {
196 AST_DECLARE_APP_ARGS(args,
197 AST_APP_ARG(family);
198 AST_APP_ARG(key);
199 );
200
201 if (ast_strlen_zero(parse)) {
202 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
203 return -1;
204 }
205
206 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
207
208 if (args.argc < 2) {
209 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
210 return -1;
211 }
212 /*
213 * When keynames are dynamically created using variables, if the variable is empty, this put bad data into the DB.
214 * In particular, a few cases: an empty key name, a key starting or ending with a /, and a key containing // two slashes.
215 * If this happens, allow it to go in, but warn the user of the issue and possible data corruption. */
216 if (ast_strlen_zero(args.key) || args.key[0] == '/' || args.key[strlen(args.key) - 1] == '/' || strstr(args.key, "//")) {
217 ast_log(LOG_WARNING, "DB: key '%s' seems malformed\n", args.key);
218 }
219 if (ast_db_put(args.family, args.key, value)) {
220 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
221 }
222
223 return 0;
224 }
225
226 static struct ast_custom_function db_function = {
227 .name = "DB",
228 .read = function_db_read,
229 .write = function_db_write,
230 };
231
232 static int function_db_exists(struct ast_channel *chan, const char *cmd,
233 char *parse, char *buf, size_t len)
234 {
235 AST_DECLARE_APP_ARGS(args,
236 AST_APP_ARG(family);
237 AST_APP_ARG(key);
238 );
239
240 buf[0] = '\0';
241
242 if (ast_strlen_zero(parse)) {
243 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
244 return -1;
245 }
246
247 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
248
249 if (args.argc < 2) {
250 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
251 return -1;
252 }
253
254 if (ast_db_get(args.family, args.key, buf, len - 1)) {
255 strcpy(buf, "0");
256 } else {
257 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
258 strcpy(buf, "1");
259 }
260
261 return 0;
262 }
263
264 static struct ast_custom_function db_exists_function = {
265 .name = "DB_EXISTS",
266 .read = function_db_exists,
267 .read_max = 2,
268 };
269
270 static int function_db_keys(struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **result, ssize_t maxlen)
271 {
272 size_t parselen = strlen(parse);
273 struct ast_db_entry *dbe, *orig_dbe;
274 struct ast_str *escape_buf = NULL;
275 const char *last = "";
276
277 /* Remove leading and trailing slashes */
278 while (parse[0] == '/') {
279 parse++;
280 parselen--;
281 }
282 while (parse[parselen - 1] == '/') {
283 parse[--parselen] = '\0';
284 }
285
286 ast_str_reset(*result);
287
288 /* Nothing within the database at that prefix? */
289 if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) {
290 return 0;
291 }
292
293 for (; dbe; dbe = dbe->next) {
294 /* Find the current component */
295 char *curkey = &dbe->key[parselen + 1], *slash;
296 if (*curkey == '/') {
297 curkey++;
298 }
299 /* Remove everything after the current component */
300 if ((slash = strchr(curkey, '/'))) {
301 *slash = '\0';
302 }
303
304 /* Skip duplicates */
305 if (!strcasecmp(last, curkey)) {
306 continue;
307 }
308 last = curkey;
309
310 if (orig_dbe != dbe) {
311 ast_str_append(result, maxlen, ",");
312 }
313 ast_str_append_escapecommas(result, maxlen, curkey, strlen(curkey));
314 }
315 ast_db_freetree(orig_dbe);
316 ast_free(escape_buf);
317 return 0;
318 }
319
320 static struct ast_custom_function db_keys_function = {
321 .name = "DB_KEYS",
322 .read2 = function_db_keys,
323 };
324
325 static int function_db_keycount(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
326 {
327 size_t parselen = strlen(parse);
328 struct ast_db_entry *dbe, *orig_dbe;
329 const char *last = "";
330 int keycount = 0;
331
332 /* Remove leading and trailing slashes */
333 while (parse[0] == '/') {
334 parse++;
335 parselen--;
336 }
337 while (parse[parselen - 1] == '/') {
338 parse[--parselen] = '\0';
339 }
340
341 /* Nothing within the database at that prefix? */
342 if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) {
343 snprintf(buf, len, "%d", keycount);
344 return 0;
345 }
346
347 for (; dbe; dbe = dbe->next) {
348 /* Find the current component */
349 char *curkey = &dbe->key[parselen + 1], *slash;
350 if (*curkey == '/') {
351 curkey++;
352 }
353 /* Remove everything after the current component */
354 if ((slash = strchr(curkey, '/'))) {
355 *slash = '\0';
356 }
357
358 /* Skip duplicates */
359 if (!strcasecmp(last, curkey)) {
360 continue;
361 }
362 last = curkey;
363
364 keycount++;
365 }
366 ast_db_freetree(orig_dbe);
367 snprintf(buf, len, "%d", keycount);
368 return 0;
369 }
370
371 static struct ast_custom_function db_keycount_function = {
372 .name = "DB_KEYCOUNT",
373 .read = function_db_keycount,
374 };
375
376 static int function_db_delete(struct ast_channel *chan, const char *cmd,
377 char *parse, char *buf, size_t len)
378 {
379 AST_DECLARE_APP_ARGS(args,
380 AST_APP_ARG(family);
381 AST_APP_ARG(key);
382 );
383
384 buf[0] = '\0';
385
386 if (ast_strlen_zero(parse)) {
387 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
388 return -1;
389 }
390
391 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
392
393 if (args.argc < 2) {
394 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
395 return -1;
396 }
397
398 if (ast_db_get(args.family, args.key, buf, len - 1)) {
399 ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
400 } else {
401 if (ast_db_del(args.family, args.key)) {
402 ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
403 }
404 }
405
406 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
407
408 return 0;
409 }
410
411 /*!
412 * \brief Wrapper to execute DB_DELETE from a write operation. Allows execution
413 * even if live_dangerously is disabled.
414 */
415 static int function_db_delete_write(struct ast_channel *chan, const char *cmd, char *parse,
416 const char *value)
417 {
418 /* Throwaway to hold the result from the read */
419 char buf[128];
420 return function_db_delete(chan, cmd, parse, buf, sizeof(buf));
421 }
422
423 static struct ast_custom_function db_delete_function = {
424 .name = "DB_DELETE",
425 .read = function_db_delete,
426 .write = function_db_delete_write,
427 };
428
429 static int unload_module(void)
430 {
431 int res = 0;
432
433 res |= ast_custom_function_unregister(&db_function);
434 res |= ast_custom_function_unregister(&db_exists_function);
435 res |= ast_custom_function_unregister(&db_delete_function);
436 res |= ast_custom_function_unregister(&db_keys_function);
437 res |= ast_custom_function_unregister(&db_keycount_function);
438
439 return res;
440 }
441
442 static int load_module(void)
443 {
444 int res = 0;
445
446 res |= ast_custom_function_register_escalating(&db_function, AST_CFE_BOTH);
447 res |= ast_custom_function_register(&db_exists_function);
448 res |= ast_custom_function_register_escalating(&db_delete_function, AST_CFE_READ);
449 res |= ast_custom_function_register(&db_keys_function);
450 res |= ast_custom_function_register(&db_keycount_function);
451
452 return res;
453 }
454
455 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");