2 * Asterisk -- A telephony toolkit for Linux.
4 * Configuration File Parser
6 * Copyright (C) 1999 - 2005, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
20 #define AST_INCLUDE_GLOB 1
21 #ifdef AST_INCLUDE_GLOB
23 #define GLOB_ABORTED GLOB_ABEND
27 #include <asterisk/config.h>
28 #include <asterisk/cli.h>
29 #include <asterisk/lock.h>
30 #include <asterisk/options.h>
31 #include <asterisk/logger.h>
32 #include <asterisk/utils.h>
33 #include <asterisk/channel.h>
34 #include <asterisk/app.h>
38 #define MAX_NESTED_COMMENTS 128
39 #define COMMENT_START ";--"
40 #define COMMENT_END "--;"
41 #define COMMENT_META ';'
42 #define COMMENT_TAG '-'
44 static char *extconfig_conf = "extconfig.conf";
46 static struct ast_config_map {
47 struct ast_config_map *next;
53 } *config_maps = NULL;
55 AST_MUTEX_DEFINE_STATIC(config_lock);
56 static struct ast_config_engine *config_engine_list;
58 #define MAX_INCLUDE_LEVEL 10
61 struct ast_comment *next;
67 int ignored; /* do not let user of the config see this category */
68 struct ast_variable *root;
69 struct ast_variable *last;
70 struct ast_category *next;
74 struct ast_category *root;
75 struct ast_category *last;
76 struct ast_category *current;
77 struct ast_category *last_browse; /* used to cache the last category supplied via category_browse */
79 int max_include_level;
82 int ast_true(const char *s)
86 /* Determine if this is a true value */
87 if (!strcasecmp(s, "yes") ||
88 !strcasecmp(s, "true") ||
89 !strcasecmp(s, "y") ||
90 !strcasecmp(s, "t") ||
91 !strcasecmp(s, "1") ||
97 int ast_false(const char *s)
101 /* Determine if this is a false value */
102 if (!strcasecmp(s, "no") ||
103 !strcasecmp(s, "false") ||
104 !strcasecmp(s, "n") ||
105 !strcasecmp(s, "f") ||
106 !strcasecmp(s, "0") ||
107 !strcasecmp(s, "off"))
112 struct ast_variable *ast_variable_new(const char *name, const char *value)
114 struct ast_variable *variable;
116 int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
117 variable = malloc(length);
119 memset(variable, 0, length);
120 variable->name = variable->stuff;
121 variable->value = variable->stuff + strlen(name) + 1;
122 strcpy(variable->name,name);
123 strcpy(variable->value,value);
129 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
132 category->last->next = variable;
134 category->root = variable;
135 category->last = variable;
138 void ast_variables_destroy(struct ast_variable *v)
140 struct ast_variable *vn;
149 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
151 struct ast_category *cat = NULL;
153 if (category && config->last_browse && (config->last_browse->name == category))
154 cat = config->last_browse;
156 cat = ast_category_get(config, category);
164 char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
166 struct ast_variable *v;
169 for (v = ast_variable_browse(config, category); v; v = v->next)
170 if (variable == v->name)
172 for (v = ast_variable_browse(config, category); v; v = v->next)
173 if (!strcasecmp(variable, v->name))
176 struct ast_category *cat;
178 for (cat = config->root; cat; cat = cat->next)
179 for (v = cat->root; v; v = v->next)
180 if (!strcasecmp(variable, v->name))
187 static struct ast_variable *variable_clone(const struct ast_variable *old)
189 struct ast_variable *new = ast_variable_new(old->name, old->value);
192 new->lineno = old->lineno;
193 new->object = old->object;
194 new->blanklines = old->blanklines;
195 /* TODO: clone comments? */
201 static void move_variables(struct ast_category *old, struct ast_category *new)
203 struct ast_variable *var;
204 struct ast_variable *next;
208 for (var = next; var; var = next) {
211 ast_variable_append(new, var);
215 struct ast_category *ast_category_new(const char *name)
217 struct ast_category *category;
219 category = malloc(sizeof(struct ast_category));
221 memset(category, 0, sizeof(struct ast_category));
222 strncpy(category->name, name, sizeof(category->name) - 1);
228 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
230 struct ast_category *cat;
232 for (cat = config->root; cat; cat = cat->next) {
233 if (cat->name == category_name && (ignored || !cat->ignored))
237 for (cat = config->root; cat; cat = cat->next) {
238 if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
245 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
247 return category_get(config, category_name, 0);
250 int ast_category_exist(const struct ast_config *config, const char *category_name)
252 return !!ast_category_get(config, category_name);
255 void ast_category_append(struct ast_config *config, struct ast_category *category)
258 config->last->next = category;
260 config->root = category;
261 config->last = category;
262 config->current = category;
265 void ast_category_destroy(struct ast_category *cat)
267 ast_variables_destroy(cat->root);
271 static struct ast_category *next_available_category(struct ast_category *cat)
273 for (; cat && cat->ignored; cat = cat->next);
278 char *ast_category_browse(struct ast_config *config, const char *prev)
280 struct ast_category *cat = NULL;
282 if (prev && config->last_browse && (config->last_browse->name == prev))
283 cat = config->last_browse->next;
284 else if (!prev && config->root)
287 for (cat = config->root; cat; cat = cat->next) {
288 if (cat->name == prev) {
294 for (cat = config->root; cat; cat = cat->next) {
295 if (!strcasecmp(cat->name, prev)) {
304 cat = next_available_category(cat);
306 config->last_browse = cat;
313 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
315 struct ast_variable *v;
323 void ast_category_rename(struct ast_category *cat, const char *name)
325 strncpy(cat->name, name, sizeof(cat->name) - 1);
328 static void inherit_category(struct ast_category *new, const struct ast_category *base)
330 struct ast_variable *var;
332 for (var = base->root; var; var = var->next) {
333 struct ast_variable *v;
335 v = variable_clone(var);
337 ast_variable_append(new, v);
341 struct ast_config *ast_config_new(void)
343 struct ast_config *config;
345 config = malloc(sizeof(*config));
347 memset(config, 0, sizeof(*config));
348 config->max_include_level = MAX_INCLUDE_LEVEL;
354 void ast_config_destroy(struct ast_config *cfg)
356 struct ast_category *cat, *catn;
363 ast_variables_destroy(cat->root);
371 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
376 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
378 /* cast below is just to silence compiler warning about dropping "const" */
379 cfg->current = (struct ast_category *) cat;
382 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile)
386 struct ast_variable *v;
387 char cmd[512], exec_file[512];
388 int object, do_exec, do_include;
390 /* Actually parse the entry */
392 struct ast_category *newcat = NULL;
395 /* A category header */
396 c = strchr(cur, ']');
398 ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
406 *cat = newcat = ast_category_new(catname);
408 ast_log(LOG_WARNING, "Out of memory, line %d of %s\n", lineno, configfile);
411 /* If there are options or categories to inherit from, process them now */
413 if (!(cur = strchr(c, ')'))) {
414 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
418 while ((cur = strsep(&c, ","))) {
419 if (!strcasecmp(cur, "!")) {
421 } else if (!strcasecmp(cur, "+")) {
422 *cat = category_get(cfg, catname, 1);
426 ast_category_destroy(newcat);
427 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
431 move_variables(newcat, *cat);
432 ast_category_destroy(newcat);
436 struct ast_category *base;
438 base = category_get(cfg, cur, 1);
440 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
443 inherit_category(*cat, base);
448 ast_category_append(cfg, *cat);
449 } else if (cur[0] == '#') {
453 while(*c && (*c > 32)) c++;
457 /* Find real argument */
458 while(*c && (*c < 33)) c++;
463 do_include = !strcasecmp(cur, "include");
465 do_exec = !strcasecmp(cur, "exec");
468 if (do_exec && !option_exec_includes) {
469 ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
472 if (do_include || do_exec) {
474 /* Strip off leading and trailing "'s and <>'s */
475 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
476 /* Get rid of leading mess */
478 while (!ast_strlen_zero(cur)) {
479 c = cur + strlen(cur) - 1;
480 if ((*c == '>') || (*c == '<') || (*c == '\"'))
485 /* #exec </path/to/executable>
486 We create a tmp file, then we #include it, then we delete it. */
488 snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%ld.%ld", time(NULL), pthread_self());
489 snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
490 ast_safe_system(cmd);
495 do_include = ast_config_internal_load(cur, cfg) ? 1 : 0;
496 if(!ast_strlen_zero(exec_file))
502 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
503 do_exec ? "exec" : "include",
504 do_exec ? "/path/to/executable" : "filename",
510 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
512 /* Just a line (variable = value) */
515 "parse error: No category context for line %d of %s\n", lineno, configfile);
518 c = strchr(cur, '=');
528 v = ast_variable_new(ast_strip(cur), ast_strip(c));
532 /* Put and reset comments */
534 ast_variable_append(*cat, v);
536 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
540 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
547 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg)
551 char *new_buf, *comment_p, *process_buf;
554 int comment = 0, nest[MAX_NESTED_COMMENTS];
555 struct ast_category *cat = NULL;
557 cat = ast_config_get_current_category(cfg);
559 if (filename[0] == '/') {
560 strncpy(fn, filename, sizeof(fn)-1);
562 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
565 #ifdef AST_INCLUDE_GLOB
569 globbuf.gl_offs = 0; /* initialize it to silence gcc */
571 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
573 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
575 if (glob_ret == GLOB_NOSPACE)
577 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
578 else if (glob_ret == GLOB_ABORTED)
580 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
582 /* loop over expanded files */
584 for (i=0; i<globbuf.gl_pathc; i++) {
585 strncpy(fn, globbuf.gl_pathv[i], sizeof(fn)-1);
587 if ((option_verbose > 1) && !option_debug) {
588 ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
591 if ((f = fopen(fn, "r"))) {
593 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
594 else if (option_verbose > 1)
595 ast_verbose("Found\n");
598 if (fgets(buf, sizeof(buf), f)) {
604 while ((comment_p = strchr(new_buf, COMMENT_META))) {
605 if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
606 /* Yuck, gotta memmove */
607 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
609 } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
610 /* Meta-Comment start detected ";--" */
611 if (comment < MAX_NESTED_COMMENTS) {
613 new_buf = comment_p + 3;
615 nest[comment-1] = lineno;
617 ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
619 } else if ((comment_p >= new_buf + 2) &&
620 (*(comment_p - 1) == COMMENT_TAG) &&
621 (*(comment_p - 2) == COMMENT_TAG)) {
622 /* Meta-Comment end detected */
624 new_buf = comment_p + 1;
626 /* Back to non-comment now */
628 /* Actually have to move what's left over the top, then continue */
630 oldptr = process_buf + strlen(process_buf);
631 memmove(oldptr, new_buf, strlen(new_buf) + 1);
634 process_buf = new_buf;
638 /* If ; is found, and we are not nested in a comment,
639 we immediately stop all comment processing */
643 new_buf = comment_p + 1;
647 char *buf = ast_strip(process_buf);
648 if (!ast_strlen_zero(buf))
649 if (process_text_line(cfg, &cat, buf, lineno, filename)) {
657 } else { /* can't open file */
659 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
660 else if (option_verbose > 1)
661 ast_verbose( "Not found (%s)\n", strerror(errno));
664 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
666 #ifdef AST_INCLUDE_GLOB
678 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
684 struct ast_variable *var;
685 struct ast_category *cat;
688 if (configfile[0] == '/') {
689 strncpy(fn, configfile, sizeof(fn)-1);
691 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
694 strncpy(date, ctime(&t), sizeof(date) - 1);
695 if ((f = fopen(fn, "w"))) {
696 if ((option_verbose > 1) && !option_debug)
697 ast_verbose( VERBOSE_PREFIX_2 "Saving '%s': ", fn);
699 fprintf(f, ";! Automatically generated configuration file\n");
700 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
701 fprintf(f, ";! Generator: %s\n", generator);
702 fprintf(f, ";! Creation Date: %s", date);
706 /* Dump section with any appropriate comment */
707 fprintf(f, "[%s]\n", cat->name);
711 fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
713 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
714 if (var->blanklines) {
715 blanklines = var->blanklines;
723 /* Put an empty line */
730 printf("Unable to open for writing: %s\n", fn);
731 else if (option_verbose > 1)
732 printf( "Unable to write (%s)", strerror(errno));
739 static void clear_config_maps(void)
741 struct ast_config_map *map;
743 ast_mutex_lock(&config_lock);
745 while (config_maps) {
747 config_maps = config_maps->next;
751 ast_mutex_unlock(&config_lock);
754 static int append_mapping(char *name, char *driver, char *database, char *table)
756 struct ast_config_map *map;
759 length = sizeof(*map);
760 length += strlen(name) + 1;
761 length += strlen(driver) + 1;
762 length += strlen(database) + 1;
764 length += strlen(table) + 1;
765 map = malloc(length);
770 memset(map, 0, length);
771 map->name = map->stuff;
772 strcpy(map->name, name);
773 map->driver = map->name + strlen(map->name) + 1;
774 strcpy(map->driver, driver);
775 map->database = map->driver + strlen(map->driver) + 1;
776 strcpy(map->database, database);
778 map->table = map->database + strlen(map->database) + 1;
779 strcpy(map->table, table);
781 map->next = config_maps;
783 if (option_verbose > 1)
784 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
785 map->name, map->driver, map->database, map->table ? map->table : map->name);
791 void read_config_maps(void)
793 struct ast_config *config;
794 struct ast_variable *v;
795 char *driver, *table, *database, *stringp;
799 config = ast_config_new();
800 config->max_include_level = 1;
801 config = ast_config_internal_load(extconfig_conf, config);
805 for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
807 driver = strsep(&stringp, ",");
808 database = strsep(&stringp, ",");
809 table = strsep(&stringp, ",");
811 if (!strcmp(v->name, extconfig_conf) || !strcmp(v->name, "asterisk.conf")) {
812 ast_log(LOG_WARNING, "Cannot bind asterisk.conf or extconfig.conf!\n");
816 if (!driver || !database)
818 if (!strcasecmp(v->name, "sipfriends")) {
819 ast_log(LOG_WARNING, "The 'sipfriends' table is obsolete, update your config to use sipusers and sippeers, though they can point to the same table.\n");
820 append_mapping("sipusers", driver, database, table ? table : "sipfriends");
821 append_mapping("sippeers", driver, database, table ? table : "sipfriends");
822 } else if (!strcasecmp(v->name, "iaxfriends")) {
823 ast_log(LOG_WARNING, "The 'iaxfriends' table is obsolete, update your config to use iaxusers and iaxpeers, though they can point to the same table.\n");
824 append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
825 append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
827 append_mapping(v->name, driver, database, table);
830 ast_config_destroy(config);
833 int ast_config_engine_register(struct ast_config_engine *new)
835 struct ast_config_engine *ptr;
837 ast_mutex_lock(&config_lock);
839 if (!config_engine_list) {
840 config_engine_list = new;
842 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
846 ast_mutex_unlock(&config_lock);
847 ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
852 int ast_config_engine_deregister(struct ast_config_engine *del)
854 struct ast_config_engine *ptr, *last=NULL;
856 ast_mutex_lock(&config_lock);
858 for (ptr = config_engine_list; ptr; ptr=ptr->next) {
861 last->next = ptr->next;
863 config_engine_list = ptr->next;
869 ast_mutex_unlock(&config_lock);
874 static struct ast_config_engine *find_engine(const char *filename, char *database, int dbsiz, char *table, int tabsiz)
876 struct ast_config_engine *eng, *ret=NULL;
877 struct ast_config_map *map;
879 ast_mutex_lock(&config_lock);
883 if (!strcasecmp(filename, map->name)) {
884 strncpy(database, map->database, dbsiz-1);
886 strncpy(table, map->table, tabsiz-1);
888 strncpy(table, filename, tabsiz-1);
894 for (eng = config_engine_list; eng; eng = eng->next) {
895 if (!strcmp(eng->name, map->driver)) {
901 ast_mutex_unlock(&config_lock);
906 static struct ast_config_engine text_file_engine = {
908 .load_func = config_text_file_load,
911 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg)
915 struct ast_config_engine *loader = &text_file_engine;
916 struct ast_config *result;
918 if (cfg->include_level == cfg->max_include_level) {
919 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
923 cfg->include_level++;
925 if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
926 struct ast_config_engine *eng;
928 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
931 if (eng && eng->load_func) {
934 eng = find_engine("global", db, sizeof(db), table, sizeof(table));
935 if (eng && eng->load_func)
940 result = loader->load_func(db, table, filename, cfg);
943 result->include_level--;
948 struct ast_config *ast_config_load(const char *filename)
950 struct ast_config *cfg;
951 struct ast_config *result;
953 cfg = ast_config_new();
957 result = ast_config_internal_load(filename, cfg);
959 ast_config_destroy(cfg);
964 struct ast_variable *ast_load_realtime(const char *family, ...)
966 struct ast_config_engine *eng;
969 struct ast_variable *res=NULL;
972 va_start(ap, family);
973 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
974 if (eng && eng->realtime_func)
975 res = eng->realtime_func(db, table, ap);
981 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
983 struct ast_config_engine *eng;
986 struct ast_config *res=NULL;
989 va_start(ap, family);
990 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
991 if (eng && eng->realtime_multi_func)
992 res = eng->realtime_multi_func(db, table, ap);
998 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1000 struct ast_config_engine *eng;
1006 va_start(ap, lookup);
1007 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1008 if (eng && eng->update_func)
1009 res = eng->update_func(db, table, keyfield, lookup, ap);
1015 static int config_command(int fd, int argc, char **argv)
1017 struct ast_config_engine *eng;
1018 struct ast_config_map *map;
1020 ast_mutex_lock(&config_lock);
1022 ast_cli(fd, "\n\n");
1023 for (eng = config_engine_list; eng; eng = eng->next) {
1024 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1025 for (map = config_maps; map; map = map->next)
1026 if (!strcasecmp(map->driver, eng->name)) {
1027 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1028 map->table ? map->table : map->name);
1034 ast_mutex_unlock(&config_lock);
1039 static char show_config_help[] =
1040 "Usage: show config mappings\n"
1041 " Shows the filenames to config engines.\n";
1043 static struct ast_cli_entry config_command_struct = {
1044 { "show", "config", "mappings", NULL }, config_command, "Show Config mappings (file names to config engines)", show_config_help, NULL
1047 int register_config_cli()
1049 return ast_cli_register(&config_command_struct);