Asterisk - The Open Source Telephony Project GIT-master-f36a736
Functions | Variables
func_db.c File Reference

Functions for interaction with the Asterisk database. More...

#include "asterisk.h"
#include <regex.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/astdb.h"
Include dependency graph for func_db.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int function_db_delete (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
 
static int function_db_delete_write (struct ast_channel *chan, const char *cmd, char *parse, const char *value)
 Wrapper to execute DB_DELETE from a write operation. Allows execution even if live_dangerously is disabled. More...
 
static int function_db_exists (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
 
static int function_db_keycount (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
 
static int function_db_keys (struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **result, ssize_t maxlen)
 
static int function_db_read (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
 
static int function_db_write (struct ast_channel *chan, const char *cmd, char *parse, const char *value)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Database (astdb) related dialplan functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_custom_function db_delete_function
 
static struct ast_custom_function db_exists_function
 
static struct ast_custom_function db_function
 
static struct ast_custom_function db_keycount_function
 
static struct ast_custom_function db_keys_function
 

Detailed Description

Functions for interaction with the Asterisk database.

Author
Russell Bryant russe.nosp@m.lb@c.nosp@m.lemso.nosp@m.n.ed.nosp@m.u

Definition in file func_db.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 438 of file func_db.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 438 of file func_db.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 438 of file func_db.c.

◆ function_db_delete()

static int function_db_delete ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
)
static

Definition at line 359 of file func_db.c.

361{
363 AST_APP_ARG(family);
364 AST_APP_ARG(key);
365 );
366
367 buf[0] = '\0';
368
369 if (ast_strlen_zero(parse)) {
370 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
371 return -1;
372 }
373
374 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
375
376 if (args.argc < 2) {
377 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
378 return -1;
379 }
380
381 if (ast_db_get(args.family, args.key, buf, len - 1)) {
382 ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
383 } else {
384 if (ast_db_del(args.family, args.key)) {
385 ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
386 }
387 }
388
389 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
390
391 return 0;
392}
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:427
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: main/db.c:478
#define ast_log
Definition: astobj2.c:42
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the 'nonstandard' argument separation process for an application.
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
const char * args

References args, AST_APP_ARG, ast_db_del(), ast_db_get(), ast_debug, AST_DECLARE_APP_ARGS, ast_log, AST_NONSTANDARD_APP_ARGS, ast_strlen_zero(), buf, len(), LOG_WARNING, and pbx_builtin_setvar_helper().

Referenced by function_db_delete_write().

◆ function_db_delete_write()

static int function_db_delete_write ( struct ast_channel chan,
const char *  cmd,
char *  parse,
const char *  value 
)
static

Wrapper to execute DB_DELETE from a write operation. Allows execution even if live_dangerously is disabled.

Definition at line 398 of file func_db.c.

400{
401 /* Throwaway to hold the result from the read */
402 char buf[128];
403 return function_db_delete(chan, cmd, parse, buf, sizeof(buf));
404}
static int function_db_delete(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_db.c:359

References buf, and function_db_delete().

◆ function_db_exists()

static int function_db_exists ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
)
static

Definition at line 215 of file func_db.c.

217{
219 AST_APP_ARG(family);
220 AST_APP_ARG(key);
221 );
222
223 buf[0] = '\0';
224
225 if (ast_strlen_zero(parse)) {
226 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
227 return -1;
228 }
229
230 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
231
232 if (args.argc < 2) {
233 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
234 return -1;
235 }
236
237 if (ast_db_get(args.family, args.key, buf, len - 1)) {
238 strcpy(buf, "0");
239 } else {
240 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
241 strcpy(buf, "1");
242 }
243
244 return 0;
245}

References args, AST_APP_ARG, ast_db_get(), AST_DECLARE_APP_ARGS, ast_log, AST_NONSTANDARD_APP_ARGS, ast_strlen_zero(), buf, len(), LOG_WARNING, and pbx_builtin_setvar_helper().

◆ function_db_keycount()

static int function_db_keycount ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
)
static

Definition at line 308 of file func_db.c.

309{
310 size_t parselen = strlen(parse);
311 struct ast_db_entry *dbe, *orig_dbe;
312 const char *last = "";
313 int keycount = 0;
314
315 /* Remove leading and trailing slashes */
316 while (parse[0] == '/') {
317 parse++;
318 parselen--;
319 }
320 while (parse[parselen - 1] == '/') {
321 parse[--parselen] = '\0';
322 }
323
324 /* Nothing within the database at that prefix? */
325 if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) {
326 snprintf(buf, len, "%d", keycount);
327 return 0;
328 }
329
330 for (; dbe; dbe = dbe->next) {
331 /* Find the current component */
332 char *curkey = &dbe->key[parselen + 1], *slash;
333 if (*curkey == '/') {
334 curkey++;
335 }
336 /* Remove everything after the current component */
337 if ((slash = strchr(curkey, '/'))) {
338 *slash = '\0';
339 }
340
341 /* Skip duplicates */
342 if (!strcasecmp(last, curkey)) {
343 continue;
344 }
345 last = curkey;
346
347 keycount++;
348 }
349 ast_db_freetree(orig_dbe);
350 snprintf(buf, len, "%d", keycount);
351 return 0;
352}
struct sla_ringing_trunk * last
Definition: app_sla.c:332
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree.
Definition: main/db.c:641
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition: main/db.c:701
#define NULL
Definition: resample.c:96
Definition: astdb.h:31
struct ast_db_entry * next
Definition: astdb.h:32
char * key
Definition: astdb.h:33

References ast_db_freetree(), ast_db_gettree(), buf, ast_db_entry::key, last, len(), ast_db_entry::next, and NULL.

◆ function_db_keys()

static int function_db_keys ( struct ast_channel chan,
const char *  cmd,
char *  parse,
struct ast_str **  result,
ssize_t  maxlen 
)
static

Definition at line 253 of file func_db.c.

254{
255 size_t parselen = strlen(parse);
256 struct ast_db_entry *dbe, *orig_dbe;
257 struct ast_str *escape_buf = NULL;
258 const char *last = "";
259
260 /* Remove leading and trailing slashes */
261 while (parse[0] == '/') {
262 parse++;
263 parselen--;
264 }
265 while (parse[parselen - 1] == '/') {
266 parse[--parselen] = '\0';
267 }
268
270
271 /* Nothing within the database at that prefix? */
272 if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) {
273 return 0;
274 }
275
276 for (; dbe; dbe = dbe->next) {
277 /* Find the current component */
278 char *curkey = &dbe->key[parselen + 1], *slash;
279 if (*curkey == '/') {
280 curkey++;
281 }
282 /* Remove everything after the current component */
283 if ((slash = strchr(curkey, '/'))) {
284 *slash = '\0';
285 }
286
287 /* Skip duplicates */
288 if (!strcasecmp(last, curkey)) {
289 continue;
290 }
291 last = curkey;
292
293 if (orig_dbe != dbe) {
294 ast_str_append(result, maxlen, ",");
295 }
296 ast_str_append_escapecommas(result, maxlen, curkey, strlen(curkey));
297 }
298 ast_db_freetree(orig_dbe);
299 ast_free(escape_buf);
300 return 0;
301}
#define ast_free(a)
Definition: astmm.h:180
static PGresult * result
Definition: cel_pgsql.c:84
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_append_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc)
Append a non-NULL terminated substring to the end of a dynamic string, with escaping of commas.
Definition: strings.h:1076
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:693
Support for dynamic strings.
Definition: strings.h:623

References ast_db_freetree(), ast_db_gettree(), ast_free, ast_str_append(), ast_str_append_escapecommas(), ast_str_reset(), ast_db_entry::key, last, ast_db_entry::next, NULL, and result.

◆ function_db_read()

static int function_db_read ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
)
static

Definition at line 145 of file func_db.c.

147{
149 AST_APP_ARG(family);
150 AST_APP_ARG(key);
151 );
152
153 buf[0] = '\0';
154
155 if (ast_strlen_zero(parse)) {
156 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
157 return -1;
158 }
159
160 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
161
162 if (args.argc < 2) {
163 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
164 return -1;
165 }
166
167 if (ast_db_get(args.family, args.key, buf, len - 1)) {
168 ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
169 } else {
170 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
171 }
172
173 return 0;
174}

References args, AST_APP_ARG, ast_db_get(), ast_debug, AST_DECLARE_APP_ARGS, ast_log, AST_NONSTANDARD_APP_ARGS, ast_strlen_zero(), buf, len(), LOG_WARNING, and pbx_builtin_setvar_helper().

◆ function_db_write()

static int function_db_write ( struct ast_channel chan,
const char *  cmd,
char *  parse,
const char *  value 
)
static

Definition at line 176 of file func_db.c.

178{
180 AST_APP_ARG(family);
181 AST_APP_ARG(key);
182 );
183
184 if (ast_strlen_zero(parse)) {
185 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
186 return -1;
187 }
188
189 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
190
191 if (args.argc < 2) {
192 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
193 return -1;
194 }
195 /*
196 * When keynames are dynamically created using variables, if the variable is empty, this put bad data into the DB.
197 * In particular, a few cases: an empty key name, a key starting or ending with a /, and a key containing // two slashes.
198 * If this happens, allow it to go in, but warn the user of the issue and possible data corruption. */
199 if (ast_strlen_zero(args.key) || args.key[0] == '/' || args.key[strlen(args.key) - 1] == '/' || strstr(args.key, "//")) {
200 ast_log(LOG_WARNING, "DB: key '%s' seems malformed\n", args.key);
201 }
202 if (ast_db_put(args.family, args.key, value)) {
203 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
204 }
205
206 return 0;
207}
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:341
int value
Definition: syslog.c:37

References args, AST_APP_ARG, ast_db_put(), AST_DECLARE_APP_ARGS, ast_log, AST_NONSTANDARD_APP_ARGS, ast_strlen_zero(), LOG_WARNING, and value.

◆ load_module()

static int load_module ( void  )
static

Definition at line 425 of file func_db.c.

426{
427 int res = 0;
428
434
435 return res;
436}
static struct ast_custom_function db_function
Definition: func_db.c:209
static struct ast_custom_function db_keys_function
Definition: func_db.c:303
static struct ast_custom_function db_delete_function
Definition: func_db.c:406
static struct ast_custom_function db_exists_function
Definition: func_db.c:247
static struct ast_custom_function db_keycount_function
Definition: func_db.c:354
#define ast_custom_function_register_escalating(acf, escalation)
Register a custom function which requires escalated privileges.
Definition: pbx.h:1567
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
@ AST_CFE_READ
Definition: pbx.h:1550
@ AST_CFE_BOTH
Definition: pbx.h:1552

References AST_CFE_BOTH, AST_CFE_READ, ast_custom_function_register, ast_custom_function_register_escalating, db_delete_function, db_exists_function, db_function, db_keycount_function, and db_keys_function.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 412 of file func_db.c.

413{
414 int res = 0;
415
421
422 return res;
423}
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.

References ast_custom_function_unregister(), db_delete_function, db_exists_function, db_function, db_keycount_function, and db_keys_function.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Database (astdb) related dialplan functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 438 of file func_db.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 438 of file func_db.c.

◆ db_delete_function

struct ast_custom_function db_delete_function
static
Initial value:
= {
.name = "DB_DELETE",
}
static int function_db_delete_write(struct ast_channel *chan, const char *cmd, char *parse, const char *value)
Wrapper to execute DB_DELETE from a write operation. Allows execution even if live_dangerously is dis...
Definition: func_db.c:398

Definition at line 406 of file func_db.c.

Referenced by load_module(), and unload_module().

◆ db_exists_function

struct ast_custom_function db_exists_function
static
Initial value:
= {
.name = "DB_EXISTS",
.read_max = 2,
}
static int function_db_exists(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_db.c:215

Definition at line 247 of file func_db.c.

Referenced by load_module(), and unload_module().

◆ db_function

struct ast_custom_function db_function
static
Initial value:
= {
.name = "DB",
}
static int function_db_read(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_db.c:145
static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse, const char *value)
Definition: func_db.c:176

Definition at line 209 of file func_db.c.

Referenced by load_module(), and unload_module().

◆ db_keycount_function

struct ast_custom_function db_keycount_function
static
Initial value:
= {
.name = "DB_KEYCOUNT",
}
static int function_db_keycount(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_db.c:308

Definition at line 354 of file func_db.c.

Referenced by load_module(), and unload_module().

◆ db_keys_function

struct ast_custom_function db_keys_function
static
Initial value:
= {
.name = "DB_KEYS",
.read2 = function_db_keys,
}
static int function_db_keys(struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **result, ssize_t maxlen)
Definition: func_db.c:253

Definition at line 303 of file func_db.c.

Referenced by load_module(), and unload_module().