Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
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.
 
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 = ASTERISK_GPL_KEY , .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 455 of file func_db.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 455 of file func_db.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 455 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 376 of file func_db.c.

378{
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}
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition db.c:421
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition db.c:472
#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 struct @519 args
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65

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 415 of file func_db.c.

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}
static int function_db_delete(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition func_db.c:376

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 232 of file func_db.c.

234{
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}

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 325 of file func_db.c.

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}
struct sla_ringing_trunk * last
Definition app_sla.c:338
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree.
Definition db.c:635
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition db.c:695
#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 270 of file func_db.c.

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
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}
#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 162 of file func_db.c.

164{
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}

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 193 of file func_db.c.

195{
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}
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition db.c:335
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 442 of file func_db.c.

443{
444 int res = 0;
445
451
452 return res;
453}
static struct ast_custom_function db_function
Definition func_db.c:226
static struct ast_custom_function db_keys_function
Definition func_db.c:320
static struct ast_custom_function db_delete_function
Definition func_db.c:423
static struct ast_custom_function db_exists_function
Definition func_db.c:264
static struct ast_custom_function db_keycount_function
Definition func_db.c:371
#define ast_custom_function_register_escalating(acf, escalation)
Register a custom function which requires escalated privileges.
Definition pbx.h:1571
#define ast_custom_function_register(acf)
Register a custom function.
Definition pbx.h:1562
@ AST_CFE_READ
Definition pbx.h:1554
@ AST_CFE_BOTH
Definition pbx.h:1556

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 429 of file func_db.c.

430{
431 int res = 0;
432
438
439 return res;
440}
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 = ASTERISK_GPL_KEY , .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 455 of file func_db.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 455 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:415

Definition at line 423 of file func_db.c.

423 {
424 .name = "DB_DELETE",
425 .read = function_db_delete,
427};

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:232

Definition at line 264 of file func_db.c.

264 {
265 .name = "DB_EXISTS",
266 .read = function_db_exists,
267 .read_max = 2,
268};

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:162
static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse, const char *value)
Definition func_db.c:193

Definition at line 226 of file func_db.c.

226 {
227 .name = "DB",
228 .read = function_db_read,
229 .write = function_db_write,
230};

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:325

Definition at line 371 of file func_db.c.

371 {
372 .name = "DB_KEYCOUNT",
373 .read = function_db_keycount,
374};

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:270

Definition at line 320 of file func_db.c.

320 {
321 .name = "DB_KEYS",
322 .read2 = function_db_keys,
323};

Referenced by load_module(), and unload_module().