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
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/config.h"
33 #include "asterisk/cli.h"
34 #include "asterisk/lock.h"
35 #include "asterisk/options.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/app.h"
41 #define MAX_NESTED_COMMENTS 128
42 #define COMMENT_START ";--"
43 #define COMMENT_END "--;"
44 #define COMMENT_META ';'
45 #define COMMENT_TAG '-'
47 static char *extconfig_conf = "extconfig.conf";
49 static struct ast_config_map {
50 struct ast_config_map *next;
56 } *config_maps = NULL;
58 AST_MUTEX_DEFINE_STATIC(config_lock);
59 static struct ast_config_engine *config_engine_list;
61 #define MAX_INCLUDE_LEVEL 10
64 struct ast_comment *next;
70 int ignored; /* do not let user of the config see this category */
71 struct ast_variable *root;
72 struct ast_variable *last;
73 struct ast_category *next;
77 struct ast_category *root;
78 struct ast_category *last;
79 struct ast_category *current;
80 struct ast_category *last_browse; /* used to cache the last category supplied via category_browse */
82 int max_include_level;
85 int ast_true(const char *s)
89 /* Determine if this is a true value */
90 if (!strcasecmp(s, "yes") ||
91 !strcasecmp(s, "true") ||
92 !strcasecmp(s, "y") ||
93 !strcasecmp(s, "t") ||
94 !strcasecmp(s, "1") ||
100 int ast_false(const char *s)
104 /* Determine if this is a false value */
105 if (!strcasecmp(s, "no") ||
106 !strcasecmp(s, "false") ||
107 !strcasecmp(s, "n") ||
108 !strcasecmp(s, "f") ||
109 !strcasecmp(s, "0") ||
110 !strcasecmp(s, "off"))
115 struct ast_variable *ast_variable_new(const char *name, const char *value)
117 struct ast_variable *variable;
119 int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
120 variable = malloc(length);
122 memset(variable, 0, length);
123 variable->name = variable->stuff;
124 variable->value = variable->stuff + strlen(name) + 1;
125 strcpy(variable->name,name);
126 strcpy(variable->value,value);
132 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
135 category->last->next = variable;
137 category->root = variable;
138 category->last = variable;
141 void ast_variables_destroy(struct ast_variable *v)
143 struct ast_variable *vn;
152 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
154 struct ast_category *cat = NULL;
156 if (category && config->last_browse && (config->last_browse->name == category))
157 cat = config->last_browse;
159 cat = ast_category_get(config, category);
167 char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
169 struct ast_variable *v;
172 for (v = ast_variable_browse(config, category); v; v = v->next)
173 if (variable == v->name)
175 for (v = ast_variable_browse(config, category); v; v = v->next)
176 if (!strcasecmp(variable, v->name))
179 struct ast_category *cat;
181 for (cat = config->root; cat; cat = cat->next)
182 for (v = cat->root; v; v = v->next)
183 if (!strcasecmp(variable, v->name))
190 static struct ast_variable *variable_clone(const struct ast_variable *old)
192 struct ast_variable *new = ast_variable_new(old->name, old->value);
195 new->lineno = old->lineno;
196 new->object = old->object;
197 new->blanklines = old->blanklines;
198 /* TODO: clone comments? */
204 static void move_variables(struct ast_category *old, struct ast_category *new)
206 struct ast_variable *var;
207 struct ast_variable *next;
211 for (var = next; var; var = next) {
214 ast_variable_append(new, var);
218 struct ast_category *ast_category_new(const char *name)
220 struct ast_category *category;
222 category = malloc(sizeof(struct ast_category));
224 memset(category, 0, sizeof(struct ast_category));
225 strncpy(category->name, name, sizeof(category->name) - 1);
231 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
233 struct ast_category *cat;
235 for (cat = config->root; cat; cat = cat->next) {
236 if (cat->name == category_name && (ignored || !cat->ignored))
240 for (cat = config->root; cat; cat = cat->next) {
241 if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
248 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
250 return category_get(config, category_name, 0);
253 int ast_category_exist(const struct ast_config *config, const char *category_name)
255 return !!ast_category_get(config, category_name);
258 void ast_category_append(struct ast_config *config, struct ast_category *category)
261 config->last->next = category;
263 config->root = category;
264 config->last = category;
265 config->current = category;
268 void ast_category_destroy(struct ast_category *cat)
270 ast_variables_destroy(cat->root);
274 static struct ast_category *next_available_category(struct ast_category *cat)
276 for (; cat && cat->ignored; cat = cat->next);
281 char *ast_category_browse(struct ast_config *config, const char *prev)
283 struct ast_category *cat = NULL;
285 if (prev && config->last_browse && (config->last_browse->name == prev))
286 cat = config->last_browse->next;
287 else if (!prev && config->root)
290 for (cat = config->root; cat; cat = cat->next) {
291 if (cat->name == prev) {
297 for (cat = config->root; cat; cat = cat->next) {
298 if (!strcasecmp(cat->name, prev)) {
307 cat = next_available_category(cat);
309 config->last_browse = cat;
316 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
318 struct ast_variable *v;
326 void ast_category_rename(struct ast_category *cat, const char *name)
328 strncpy(cat->name, name, sizeof(cat->name) - 1);
331 static void inherit_category(struct ast_category *new, const struct ast_category *base)
333 struct ast_variable *var;
335 for (var = base->root; var; var = var->next) {
336 struct ast_variable *v;
338 v = variable_clone(var);
340 ast_variable_append(new, v);
344 struct ast_config *ast_config_new(void)
346 struct ast_config *config;
348 config = malloc(sizeof(*config));
350 memset(config, 0, sizeof(*config));
351 config->max_include_level = MAX_INCLUDE_LEVEL;
357 void ast_config_destroy(struct ast_config *cfg)
359 struct ast_category *cat, *catn;
366 ast_variables_destroy(cat->root);
374 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
379 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
381 /* cast below is just to silence compiler warning about dropping "const" */
382 cfg->current = (struct ast_category *) cat;
385 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile)
389 struct ast_variable *v;
390 char cmd[512], exec_file[512];
391 int object, do_exec, do_include;
393 /* Actually parse the entry */
395 struct ast_category *newcat = NULL;
398 /* A category header */
399 c = strchr(cur, ']');
401 ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
409 *cat = newcat = ast_category_new(catname);
411 ast_log(LOG_WARNING, "Out of memory, line %d of %s\n", lineno, configfile);
414 /* If there are options or categories to inherit from, process them now */
416 if (!(cur = strchr(c, ')'))) {
417 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
421 while ((cur = strsep(&c, ","))) {
422 if (!strcasecmp(cur, "!")) {
424 } else if (!strcasecmp(cur, "+")) {
425 *cat = category_get(cfg, catname, 1);
429 ast_category_destroy(newcat);
430 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
434 move_variables(newcat, *cat);
435 ast_category_destroy(newcat);
439 struct ast_category *base;
441 base = category_get(cfg, cur, 1);
443 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
446 inherit_category(*cat, base);
451 ast_category_append(cfg, *cat);
452 } else if (cur[0] == '#') {
456 while(*c && (*c > 32)) c++;
460 /* Find real argument */
461 while(*c && (*c < 33)) c++;
466 do_include = !strcasecmp(cur, "include");
468 do_exec = !strcasecmp(cur, "exec");
471 if (do_exec && !option_exec_includes) {
472 ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
475 if (do_include || do_exec) {
477 /* Strip off leading and trailing "'s and <>'s */
478 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
479 /* Get rid of leading mess */
481 while (!ast_strlen_zero(cur)) {
482 c = cur + strlen(cur) - 1;
483 if ((*c == '>') || (*c == '<') || (*c == '\"'))
488 /* #exec </path/to/executable>
489 We create a tmp file, then we #include it, then we delete it. */
491 snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%ld.%ld", time(NULL), (long)pthread_self());
492 snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
493 ast_safe_system(cmd);
498 do_include = ast_config_internal_load(cur, cfg) ? 1 : 0;
499 if(!ast_strlen_zero(exec_file))
505 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
506 do_exec ? "exec" : "include",
507 do_exec ? "/path/to/executable" : "filename",
513 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
515 /* Just a line (variable = value) */
518 "parse error: No category context for line %d of %s\n", lineno, configfile);
521 c = strchr(cur, '=');
531 v = ast_variable_new(ast_strip(cur), ast_strip(c));
535 /* Put and reset comments */
537 ast_variable_append(*cat, v);
539 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
543 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
550 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg)
554 char *new_buf, *comment_p, *process_buf;
557 int comment = 0, nest[MAX_NESTED_COMMENTS];
558 struct ast_category *cat = NULL;
561 cat = ast_config_get_current_category(cfg);
563 if (filename[0] == '/') {
564 strncpy(fn, filename, sizeof(fn)-1);
566 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
569 #ifdef AST_INCLUDE_GLOB
573 globbuf.gl_offs = 0; /* initialize it to silence gcc */
575 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
577 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
579 if (glob_ret == GLOB_NOSPACE)
581 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
582 else if (glob_ret == GLOB_ABORTED)
584 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
586 /* loop over expanded files */
588 for (i=0; i<globbuf.gl_pathc; i++) {
589 strncpy(fn, globbuf.gl_pathv[i], sizeof(fn)-1);
591 if ((option_verbose > 1) && !option_debug) {
592 ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
595 if ((f = fopen(fn, "r"))) {
598 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
599 else if (option_verbose > 1)
600 ast_verbose("Found\n");
603 if (fgets(buf, sizeof(buf), f)) {
609 while ((comment_p = strchr(new_buf, COMMENT_META))) {
610 if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
611 /* Yuck, gotta memmove */
612 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
614 } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
615 /* Meta-Comment start detected ";--" */
616 if (comment < MAX_NESTED_COMMENTS) {
618 new_buf = comment_p + 3;
620 nest[comment-1] = lineno;
622 ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
624 } else if ((comment_p >= new_buf + 2) &&
625 (*(comment_p - 1) == COMMENT_TAG) &&
626 (*(comment_p - 2) == COMMENT_TAG)) {
627 /* Meta-Comment end detected */
629 new_buf = comment_p + 1;
631 /* Back to non-comment now */
633 /* Actually have to move what's left over the top, then continue */
635 oldptr = process_buf + strlen(process_buf);
636 memmove(oldptr, new_buf, strlen(new_buf) + 1);
639 process_buf = new_buf;
643 /* If ; is found, and we are not nested in a comment,
644 we immediately stop all comment processing */
648 new_buf = comment_p + 1;
652 char *buf = ast_strip(process_buf);
653 if (!ast_strlen_zero(buf))
654 if (process_text_line(cfg, &cat, buf, lineno, filename)) {
662 } else { /* can't open file */
664 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
665 else if (option_verbose > 1)
666 ast_verbose( "Not found (%s)\n", strerror(errno));
669 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
671 #ifdef AST_INCLUDE_GLOB
685 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
691 struct ast_variable *var;
692 struct ast_category *cat;
695 if (configfile[0] == '/') {
696 ast_copy_string(fn, configfile, sizeof(fn));
698 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
701 strncpy(date, ctime(&t), sizeof(date) - 1);
702 if ((f = fopen(fn, "w"))) {
703 if ((option_verbose > 1) && !option_debug)
704 ast_verbose( VERBOSE_PREFIX_2 "Saving '%s': ", fn);
706 fprintf(f, ";! Automatically generated configuration file\n");
707 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
708 fprintf(f, ";! Generator: %s\n", generator);
709 fprintf(f, ";! Creation Date: %s", date);
713 /* Dump section with any appropriate comment */
714 fprintf(f, "[%s]\n", cat->name);
718 fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
720 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
721 if (var->blanklines) {
722 blanklines = var->blanklines;
730 /* Put an empty line */
737 printf("Unable to open for writing: %s\n", fn);
738 else if (option_verbose > 1)
739 printf( "Unable to write (%s)", strerror(errno));
746 static void clear_config_maps(void)
748 struct ast_config_map *map;
750 ast_mutex_lock(&config_lock);
752 while (config_maps) {
754 config_maps = config_maps->next;
758 ast_mutex_unlock(&config_lock);
761 static int append_mapping(char *name, char *driver, char *database, char *table)
763 struct ast_config_map *map;
766 length = sizeof(*map);
767 length += strlen(name) + 1;
768 length += strlen(driver) + 1;
769 length += strlen(database) + 1;
771 length += strlen(table) + 1;
772 map = malloc(length);
777 memset(map, 0, length);
778 map->name = map->stuff;
779 strcpy(map->name, name);
780 map->driver = map->name + strlen(map->name) + 1;
781 strcpy(map->driver, driver);
782 map->database = map->driver + strlen(map->driver) + 1;
783 strcpy(map->database, database);
785 map->table = map->database + strlen(map->database) + 1;
786 strcpy(map->table, table);
788 map->next = config_maps;
790 if (option_verbose > 1)
791 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
792 map->name, map->driver, map->database, map->table ? map->table : map->name);
798 void read_config_maps(void)
800 struct ast_config *config, *configtmp;
801 struct ast_variable *v;
802 char *driver, *table, *database, *stringp;
806 configtmp = ast_config_new();
807 configtmp->max_include_level = 1;
808 config = ast_config_internal_load(extconfig_conf, configtmp);
810 ast_config_destroy(configtmp);
814 for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
816 driver = strsep(&stringp, ",");
817 database = strsep(&stringp, ",");
818 table = strsep(&stringp, ",");
820 if (!strcmp(v->name, extconfig_conf) || !strcmp(v->name, "asterisk.conf")) {
821 ast_log(LOG_WARNING, "Cannot bind asterisk.conf or extconfig.conf!\n");
825 if (!driver || !database)
827 if (!strcasecmp(v->name, "sipfriends")) {
828 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");
829 append_mapping("sipusers", driver, database, table ? table : "sipfriends");
830 append_mapping("sippeers", driver, database, table ? table : "sipfriends");
831 } else if (!strcasecmp(v->name, "iaxfriends")) {
832 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");
833 append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
834 append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
836 append_mapping(v->name, driver, database, table);
839 ast_config_destroy(config);
842 int ast_config_engine_register(struct ast_config_engine *new)
844 struct ast_config_engine *ptr;
846 ast_mutex_lock(&config_lock);
848 if (!config_engine_list) {
849 config_engine_list = new;
851 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
855 ast_mutex_unlock(&config_lock);
856 ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
861 int ast_config_engine_deregister(struct ast_config_engine *del)
863 struct ast_config_engine *ptr, *last=NULL;
865 ast_mutex_lock(&config_lock);
867 for (ptr = config_engine_list; ptr; ptr=ptr->next) {
870 last->next = ptr->next;
872 config_engine_list = ptr->next;
878 ast_mutex_unlock(&config_lock);
883 static struct ast_config_engine *find_engine(const char *filename, char *database, int dbsiz, char *table, int tabsiz)
885 struct ast_config_engine *eng, *ret=NULL;
886 struct ast_config_map *map;
888 ast_mutex_lock(&config_lock);
892 if (!strcasecmp(filename, map->name)) {
893 strncpy(database, map->database, dbsiz-1);
895 strncpy(table, map->table, tabsiz-1);
897 strncpy(table, filename, tabsiz-1);
903 for (eng = config_engine_list; eng; eng = eng->next) {
904 if (!strcmp(eng->name, map->driver)) {
910 ast_mutex_unlock(&config_lock);
915 static struct ast_config_engine text_file_engine = {
917 .load_func = config_text_file_load,
920 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg)
924 struct ast_config_engine *loader = &text_file_engine;
925 struct ast_config *result;
927 if (cfg->include_level == cfg->max_include_level) {
928 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
932 cfg->include_level++;
934 if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
935 struct ast_config_engine *eng;
937 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
940 if (eng && eng->load_func) {
943 eng = find_engine("global", db, sizeof(db), table, sizeof(table));
944 if (eng && eng->load_func)
949 result = loader->load_func(db, table, filename, cfg);
952 result->include_level--;
957 struct ast_config *ast_config_load(const char *filename)
959 struct ast_config *cfg;
960 struct ast_config *result;
962 cfg = ast_config_new();
966 result = ast_config_internal_load(filename, cfg);
968 ast_config_destroy(cfg);
973 struct ast_variable *ast_load_realtime(const char *family, ...)
975 struct ast_config_engine *eng;
978 struct ast_variable *res=NULL;
981 va_start(ap, family);
982 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
983 if (eng && eng->realtime_func)
984 res = eng->realtime_func(db, table, ap);
990 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
992 struct ast_config_engine *eng;
995 struct ast_config *res=NULL;
998 va_start(ap, family);
999 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1000 if (eng && eng->realtime_multi_func)
1001 res = eng->realtime_multi_func(db, table, ap);
1007 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1009 struct ast_config_engine *eng;
1015 va_start(ap, lookup);
1016 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1017 if (eng && eng->update_func)
1018 res = eng->update_func(db, table, keyfield, lookup, ap);
1024 static int config_command(int fd, int argc, char **argv)
1026 struct ast_config_engine *eng;
1027 struct ast_config_map *map;
1029 ast_mutex_lock(&config_lock);
1031 ast_cli(fd, "\n\n");
1032 for (eng = config_engine_list; eng; eng = eng->next) {
1033 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1034 for (map = config_maps; map; map = map->next)
1035 if (!strcasecmp(map->driver, eng->name)) {
1036 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1037 map->table ? map->table : map->name);
1043 ast_mutex_unlock(&config_lock);
1048 static char show_config_help[] =
1049 "Usage: show config mappings\n"
1050 " Shows the filenames to config engines.\n";
1052 static struct ast_cli_entry config_command_struct = {
1053 { "show", "config", "mappings", NULL }, config_command, "Show Config mappings (file names to config engines)", show_config_help, NULL
1056 int register_config_cli()
1058 return ast_cli_register(&config_command_struct);