Let's also include aclocal.m4
[asterisk-bristuff.git] / main / config.c
blob6163f827a493986ffc6ce220aa590369578e7326
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Configuration File Parser
23 * \author Mark Spencer <markster@digium.com>
25 * Includes the Asterisk Realtime API - ARA
26 * See doc/realtime.txt and doc/extconfig.txt
29 #include "asterisk.h"
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <time.h>
39 #include <sys/stat.h>
40 #define AST_INCLUDE_GLOB 1
41 #ifdef AST_INCLUDE_GLOB
42 #if defined(__Darwin__) || defined(__CYGWIN__)
43 #define GLOB_ABORTED GLOB_ABEND
44 #endif
45 # include <glob.h>
46 #endif
48 #include "asterisk/config.h"
49 #include "asterisk/cli.h"
50 #include "asterisk/lock.h"
51 #include "asterisk/options.h"
52 #include "asterisk/logger.h"
53 #include "asterisk/utils.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/app.h"
57 #define MAX_NESTED_COMMENTS 128
58 #define COMMENT_START ";--"
59 #define COMMENT_END "--;"
60 #define COMMENT_META ';'
61 #define COMMENT_TAG '-'
63 static char *extconfig_conf = "extconfig.conf";
66 /*! \brief Structure to keep comments for rewriting configuration files */
67 struct ast_comment {
68 struct ast_comment *next;
69 char cmt[0];
72 #define CB_INCR 250
74 static void CB_INIT(char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
76 if (!(*comment_buffer)) {
77 *comment_buffer = ast_malloc(CB_INCR);
78 if (!(*comment_buffer))
79 return;
80 (*comment_buffer)[0] = 0;
81 *comment_buffer_size = CB_INCR;
82 *lline_buffer = ast_malloc(CB_INCR);
83 if (!(*lline_buffer))
84 return;
85 (*lline_buffer)[0] = 0;
86 *lline_buffer_size = CB_INCR;
87 } else {
88 (*comment_buffer)[0] = 0;
89 (*lline_buffer)[0] = 0;
93 static void CB_ADD(char **comment_buffer, int *comment_buffer_size, char *str)
95 int rem = *comment_buffer_size - strlen(*comment_buffer) - 1;
96 int siz = strlen(str);
97 if (rem < siz+1) {
98 *comment_buffer = ast_realloc(*comment_buffer, *comment_buffer_size + CB_INCR + siz + 1);
99 if (!(*comment_buffer))
100 return;
101 *comment_buffer_size += CB_INCR+siz+1;
103 strcat(*comment_buffer,str);
106 static void CB_ADD_LEN(char **comment_buffer, int *comment_buffer_size, char *str, int len)
108 int cbl = strlen(*comment_buffer) + 1;
109 int rem = *comment_buffer_size - cbl;
110 if (rem < len+1) {
111 *comment_buffer = ast_realloc(*comment_buffer, *comment_buffer_size + CB_INCR + len + 1);
112 if (!(*comment_buffer))
113 return;
114 *comment_buffer_size += CB_INCR+len+1;
116 strncat(*comment_buffer,str,len);
117 (*comment_buffer)[cbl+len-1] = 0;
120 static void LLB_ADD(char **lline_buffer, int *lline_buffer_size, char *str)
122 int rem = *lline_buffer_size - strlen(*lline_buffer) - 1;
123 int siz = strlen(str);
124 if (rem < siz+1) {
125 *lline_buffer = ast_realloc(*lline_buffer, *lline_buffer_size + CB_INCR + siz + 1);
126 if (!(*lline_buffer))
127 return;
128 *lline_buffer_size += CB_INCR + siz + 1;
130 strcat(*lline_buffer,str);
133 static void CB_RESET(char **comment_buffer, char **lline_buffer)
135 (*comment_buffer)[0] = 0;
136 (*lline_buffer)[0] = 0;
140 static struct ast_comment *ALLOC_COMMENT(const char *buffer)
142 struct ast_comment *x = ast_calloc(1,sizeof(struct ast_comment)+strlen(buffer)+1);
143 strcpy(x->cmt, buffer);
144 return x;
148 static struct ast_config_map {
149 struct ast_config_map *next;
150 char *name;
151 char *driver;
152 char *database;
153 char *table;
154 char stuff[0];
155 } *config_maps = NULL;
157 AST_MUTEX_DEFINE_STATIC(config_lock);
158 static struct ast_config_engine *config_engine_list;
160 #define MAX_INCLUDE_LEVEL 10
162 struct ast_category_template_instance {
163 char name[80]; /* redundant? */
164 const struct ast_category *inst;
165 AST_LIST_ENTRY(ast_category_template_instance) next;
168 struct ast_category {
169 char name[80];
170 int ignored; /*!< do not let user of the config see this category */
171 int include_level;
172 AST_LIST_HEAD_NOLOCK(template_instance_list, ast_category_template_instance) template_instances;
173 struct ast_comment *precomments;
174 struct ast_comment *sameline;
175 struct ast_variable *root;
176 struct ast_variable *last;
177 struct ast_category *next;
180 struct ast_config {
181 struct ast_category *root;
182 struct ast_category *last;
183 struct ast_category *current;
184 struct ast_category *last_browse; /*!< used to cache the last category supplied via category_browse */
185 int include_level;
186 int max_include_level;
189 struct ast_variable *ast_variable_new(const char *name, const char *value)
191 struct ast_variable *variable;
192 int name_len = strlen(name) + 1;
194 if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
195 variable->name = variable->stuff;
196 variable->value = variable->stuff + name_len;
197 strcpy(variable->name,name);
198 strcpy(variable->value,value);
201 return variable;
204 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
206 if (!variable)
207 return;
208 if (category->last)
209 category->last->next = variable;
210 else
211 category->root = variable;
212 category->last = variable;
213 while (category->last->next)
214 category->last = category->last->next;
217 void ast_variables_destroy(struct ast_variable *v)
219 struct ast_variable *vn;
221 while(v) {
222 vn = v;
223 v = v->next;
224 free(vn);
228 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
230 struct ast_category *cat = NULL;
232 if (category && config->last_browse && (config->last_browse->name == category))
233 cat = config->last_browse;
234 else
235 cat = ast_category_get(config, category);
237 return (cat) ? cat->root : NULL;
240 const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)
242 const char *tmp;
243 tmp = ast_variable_retrieve(cfg, cat, var);
244 if (!tmp)
245 tmp = ast_variable_retrieve(cfg, "general", var);
246 return tmp;
250 const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
252 struct ast_variable *v;
254 if (category) {
255 for (v = ast_variable_browse(config, category); v; v = v->next) {
256 if (!strcasecmp(variable, v->name))
257 return v->value;
259 } else {
260 struct ast_category *cat;
262 for (cat = config->root; cat; cat = cat->next)
263 for (v = cat->root; v; v = v->next)
264 if (!strcasecmp(variable, v->name))
265 return v->value;
268 return NULL;
271 static struct ast_variable *variable_clone(const struct ast_variable *old)
273 struct ast_variable *new = ast_variable_new(old->name, old->value);
275 if (new) {
276 new->lineno = old->lineno;
277 new->object = old->object;
278 new->blanklines = old->blanklines;
279 /* TODO: clone comments? */
282 return new;
285 static void move_variables(struct ast_category *old, struct ast_category *new)
287 struct ast_variable *var = old->root;
288 old->root = NULL;
289 #if 1
290 /* we can just move the entire list in a single op */
291 ast_variable_append(new, var);
292 #else
293 while (var) {
294 struct ast_variable *next = var->next;
295 var->next = NULL;
296 ast_variable_append(new, var);
297 var = next;
299 #endif
302 struct ast_category *ast_category_new(const char *name)
304 struct ast_category *category;
306 if ((category = ast_calloc(1, sizeof(*category))))
307 ast_copy_string(category->name, name, sizeof(category->name));
308 return category;
311 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
313 struct ast_category *cat;
315 /* try exact match first, then case-insensitive match */
316 for (cat = config->root; cat; cat = cat->next) {
317 if (cat->name == category_name && (ignored || !cat->ignored))
318 return cat;
321 for (cat = config->root; cat; cat = cat->next) {
322 if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
323 return cat;
326 return NULL;
329 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
331 return category_get(config, category_name, 0);
334 int ast_category_exist(const struct ast_config *config, const char *category_name)
336 return !!ast_category_get(config, category_name);
339 void ast_category_append(struct ast_config *config, struct ast_category *category)
341 if (config->last)
342 config->last->next = category;
343 else
344 config->root = category;
345 category->include_level = config->include_level;
346 config->last = category;
347 config->current = category;
350 static void ast_destroy_comments(struct ast_category *cat)
352 struct ast_comment *n, *p;
353 for (p=cat->precomments; p; p=n) {
354 n = p->next;
355 free(p);
357 for (p=cat->sameline; p; p=n) {
358 n = p->next;
359 free(p);
361 cat->precomments = NULL;
362 cat->sameline = NULL;
365 static void ast_destroy_template_list(struct ast_category *cat)
367 struct ast_category_template_instance *x;
368 AST_LIST_TRAVERSE_SAFE_BEGIN(&cat->template_instances, x, next) {
369 AST_LIST_REMOVE_CURRENT(&cat->template_instances, next);
370 free(x);
372 AST_LIST_TRAVERSE_SAFE_END;
375 void ast_category_destroy(struct ast_category *cat)
377 ast_variables_destroy(cat->root);
378 ast_destroy_comments(cat);
379 ast_destroy_template_list(cat);
380 free(cat);
383 static struct ast_category *next_available_category(struct ast_category *cat)
385 for (; cat && cat->ignored; cat = cat->next);
387 return cat;
390 struct ast_variable *ast_category_root(struct ast_config *config, char *cat)
392 struct ast_category *category = ast_category_get(config, cat);
393 if (category)
394 return category->root;
395 return NULL;
398 char *ast_category_browse(struct ast_config *config, const char *prev)
400 struct ast_category *cat = NULL;
402 if (prev && config->last_browse && (config->last_browse->name == prev))
403 cat = config->last_browse->next;
404 else if (!prev && config->root)
405 cat = config->root;
406 else if (prev) {
407 for (cat = config->root; cat; cat = cat->next) {
408 if (cat->name == prev) {
409 cat = cat->next;
410 break;
413 if (!cat) {
414 for (cat = config->root; cat; cat = cat->next) {
415 if (!strcasecmp(cat->name, prev)) {
416 cat = cat->next;
417 break;
423 if (cat)
424 cat = next_available_category(cat);
426 config->last_browse = cat;
427 return (cat) ? cat->name : NULL;
430 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
432 struct ast_variable *v;
434 v = cat->root;
435 cat->root = NULL;
436 cat->last = NULL;
438 return v;
441 void ast_category_rename(struct ast_category *cat, const char *name)
443 ast_copy_string(cat->name, name, sizeof(cat->name));
446 static void inherit_category(struct ast_category *new, const struct ast_category *base)
448 struct ast_variable *var;
449 struct ast_category_template_instance *x = ast_calloc(1,sizeof(struct ast_category_template_instance));
450 strcpy(x->name, base->name);
451 x->inst = base;
452 AST_LIST_INSERT_TAIL(&new->template_instances, x, next);
453 for (var = base->root; var; var = var->next)
454 ast_variable_append(new, variable_clone(var));
457 struct ast_config *ast_config_new(void)
459 struct ast_config *config;
461 if ((config = ast_calloc(1, sizeof(*config))))
462 config->max_include_level = MAX_INCLUDE_LEVEL;
463 return config;
466 int ast_variable_delete(struct ast_category *category, char *variable, char *match)
468 struct ast_variable *cur, *prev=NULL, *curn;
469 int res = -1;
470 cur = category->root;
471 while (cur) {
472 if (cur->name == variable) {
473 if (prev) {
474 prev->next = cur->next;
475 if (cur == category->last)
476 category->last = prev;
477 } else {
478 category->root = cur->next;
479 if (cur == category->last)
480 category->last = NULL;
482 cur->next = NULL;
483 ast_variables_destroy(cur);
484 return 0;
486 prev = cur;
487 cur = cur->next;
490 prev = NULL;
491 cur = category->root;
492 while (cur) {
493 curn = cur->next;
494 if (!strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match))) {
495 if (prev) {
496 prev->next = cur->next;
497 if (cur == category->last)
498 category->last = prev;
499 } else {
500 category->root = cur->next;
501 if (cur == category->last)
502 category->last = NULL;
504 cur->next = NULL;
505 ast_variables_destroy(cur);
506 res = 0;
507 } else
508 prev = cur;
510 cur = curn;
512 return res;
515 int ast_variable_update(struct ast_category *category, const char *variable,
516 const char *value, const char *match, unsigned int object)
518 struct ast_variable *cur, *prev=NULL, *newer;
520 if (!(newer = ast_variable_new(variable, value)))
521 return -1;
523 newer->object = object;
525 for (cur = category->root; cur; prev = cur, cur = cur->next) {
526 if (strcasecmp(cur->name, variable) ||
527 (!ast_strlen_zero(match) && strcasecmp(cur->value, match)))
528 continue;
530 newer->next = cur->next;
531 newer->object = cur->object || object;
532 if (prev)
533 prev->next = newer;
534 else
535 category->root = newer;
536 if (category->last == cur)
537 category->last = newer;
539 cur->next = NULL;
540 ast_variables_destroy(cur);
542 return 0;
545 if (prev)
546 prev->next = newer;
547 else
548 category->root = newer;
550 return 0;
553 int ast_category_delete(struct ast_config *cfg, char *category)
555 struct ast_category *prev=NULL, *cat;
556 cat = cfg->root;
557 while(cat) {
558 if (cat->name == category) {
559 if (prev) {
560 prev->next = cat->next;
561 if (cat == cfg->last)
562 cfg->last = prev;
563 } else {
564 cfg->root = cat->next;
565 if (cat == cfg->last)
566 cfg->last = NULL;
568 ast_category_destroy(cat);
569 return 0;
571 prev = cat;
572 cat = cat->next;
575 prev = NULL;
576 cat = cfg->root;
577 while(cat) {
578 if (!strcasecmp(cat->name, category)) {
579 if (prev) {
580 prev->next = cat->next;
581 if (cat == cfg->last)
582 cfg->last = prev;
583 } else {
584 cfg->root = cat->next;
585 if (cat == cfg->last)
586 cfg->last = NULL;
588 ast_category_destroy(cat);
589 return 0;
591 prev = cat;
592 cat = cat->next;
594 return -1;
597 void ast_config_destroy(struct ast_config *cfg)
599 struct ast_category *cat, *catn;
601 if (!cfg)
602 return;
604 cat = cfg->root;
605 while(cat) {
606 catn = cat;
607 cat = cat->next;
608 ast_category_destroy(catn);
610 free(cfg);
613 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
615 return cfg->current;
618 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
620 /* cast below is just to silence compiler warning about dropping "const" */
621 cfg->current = (struct ast_category *) cat;
624 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments,
625 char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
627 char *c;
628 char *cur = buf;
629 struct ast_variable *v;
630 char cmd[512], exec_file[512];
631 int object, do_exec, do_include;
633 /* Actually parse the entry */
634 if (cur[0] == '[') {
635 struct ast_category *newcat = NULL;
636 char *catname;
638 /* A category header */
639 c = strchr(cur, ']');
640 if (!c) {
641 ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
642 return -1;
644 *c++ = '\0';
645 cur++;
646 if (*c++ != '(')
647 c = NULL;
648 catname = cur;
649 if (!(*cat = newcat = ast_category_new(catname))) {
650 return -1;
652 /* add comments */
653 if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
654 newcat->precomments = ALLOC_COMMENT(*comment_buffer);
656 if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
657 newcat->sameline = ALLOC_COMMENT(*lline_buffer);
659 if( withcomments )
660 CB_RESET(comment_buffer, lline_buffer);
662 /* If there are options or categories to inherit from, process them now */
663 if (c) {
664 if (!(cur = strchr(c, ')'))) {
665 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
666 return -1;
668 *cur = '\0';
669 while ((cur = strsep(&c, ","))) {
670 if (!strcasecmp(cur, "!")) {
671 (*cat)->ignored = 1;
672 } else if (!strcasecmp(cur, "+")) {
673 *cat = category_get(cfg, catname, 1);
674 if (!(*cat)) {
675 if (newcat)
676 ast_category_destroy(newcat);
677 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
678 return -1;
680 if (newcat) {
681 move_variables(newcat, *cat);
682 ast_category_destroy(newcat);
683 newcat = NULL;
685 } else {
686 struct ast_category *base;
688 base = category_get(cfg, cur, 1);
689 if (!base) {
690 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
691 return -1;
693 inherit_category(*cat, base);
697 if (newcat)
698 ast_category_append(cfg, *cat);
699 } else if (cur[0] == '#') {
700 /* A directive */
701 cur++;
702 c = cur;
703 while(*c && (*c > 32)) c++;
704 if (*c) {
705 *c = '\0';
706 /* Find real argument */
707 c = ast_skip_blanks(c + 1);
708 if (!(*c))
709 c = NULL;
710 } else
711 c = NULL;
712 do_include = !strcasecmp(cur, "include");
713 if(!do_include)
714 do_exec = !strcasecmp(cur, "exec");
715 else
716 do_exec = 0;
717 if (do_exec && !ast_opt_exec_includes) {
718 ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
719 do_exec = 0;
721 if (do_include || do_exec) {
722 if (c) {
723 /* Strip off leading and trailing "'s and <>'s */
724 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
725 /* Get rid of leading mess */
726 cur = c;
727 while (!ast_strlen_zero(cur)) {
728 c = cur + strlen(cur) - 1;
729 if ((*c == '>') || (*c == '<') || (*c == '\"'))
730 *c = '\0';
731 else
732 break;
734 /* #exec </path/to/executable>
735 We create a tmp file, then we #include it, then we delete it. */
736 if (do_exec) {
737 snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d.%ld", (int)time(NULL), (long)pthread_self());
738 snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
739 ast_safe_system(cmd);
740 cur = exec_file;
741 } else
742 exec_file[0] = '\0';
743 /* A #include */
744 do_include = ast_config_internal_load(cur, cfg, withcomments) ? 1 : 0;
745 if(!ast_strlen_zero(exec_file))
746 unlink(exec_file);
747 if (!do_include) {
748 ast_log(LOG_ERROR, "*********************************************************\n");
749 ast_log(LOG_ERROR, "*********** YOU SHOULD REALLY READ THIS ERROR ***********\n");
750 ast_log(LOG_ERROR, "Future versions of Asterisk will treat a #include of a "
751 "file that does not exist as an error, and will fail to "
752 "load that configuration file. Please ensure that the "
753 "file '%s' exists, even if it is empty.\n", cur);
754 ast_log(LOG_ERROR, "*********** YOU SHOULD REALLY READ THIS ERROR ***********\n");
755 ast_log(LOG_ERROR, "*********************************************************\n");
756 return 0;
759 } else {
760 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
761 do_exec ? "exec" : "include",
762 do_exec ? "/path/to/executable" : "filename",
763 lineno,
764 configfile);
767 else
768 ast_log(LOG_WARNING, "Unknown directive '#%s' at line %d of %s\n", cur, lineno, configfile);
769 } else {
770 /* Just a line (variable = value) */
771 if (!(*cat)) {
772 ast_log(LOG_WARNING,
773 "parse error: No category context for line %d of %s\n", lineno, configfile);
774 return -1;
776 c = strchr(cur, '=');
777 if (c) {
778 *c = 0;
779 c++;
780 /* Ignore > in => */
781 if (*c== '>') {
782 object = 1;
783 c++;
784 } else
785 object = 0;
786 if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
787 v->lineno = lineno;
788 v->object = object;
789 /* Put and reset comments */
790 v->blanklines = 0;
791 ast_variable_append(*cat, v);
792 /* add comments */
793 if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
794 v->precomments = ALLOC_COMMENT(*comment_buffer);
796 if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
797 v->sameline = ALLOC_COMMENT(*lline_buffer);
799 if( withcomments )
800 CB_RESET(comment_buffer, lline_buffer);
802 } else {
803 return -1;
805 } else {
806 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
809 return 0;
812 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, int withcomments)
814 char fn[256];
815 #if defined(LOW_MEMORY)
816 char buf[512];
817 #else
818 char buf[8192];
819 #endif
820 char *new_buf, *comment_p, *process_buf;
821 FILE *f;
822 int lineno=0;
823 int comment = 0, nest[MAX_NESTED_COMMENTS];
824 struct ast_category *cat = NULL;
825 int count = 0;
826 struct stat statbuf;
827 /*! Growable string buffer */
828 char *comment_buffer=0; /*!< this will be a comment collector.*/
829 int comment_buffer_size=0; /*!< the amount of storage so far alloc'd for the comment_buffer */
831 char *lline_buffer=0; /*!< A buffer for stuff behind the ; */
832 int lline_buffer_size=0;
835 cat = ast_config_get_current_category(cfg);
837 if (filename[0] == '/') {
838 ast_copy_string(fn, filename, sizeof(fn));
839 } else {
840 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
843 if (withcomments) {
844 CB_INIT(&comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size);
845 if (!lline_buffer || !comment_buffer) {
846 ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
847 return NULL;
850 #ifdef AST_INCLUDE_GLOB
852 int glob_ret;
853 glob_t globbuf;
854 globbuf.gl_offs = 0; /* initialize it to silence gcc */
855 #ifdef SOLARIS
856 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
857 #else
858 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
859 #endif
860 if (glob_ret == GLOB_NOSPACE)
861 ast_log(LOG_WARNING,
862 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
863 else if (glob_ret == GLOB_ABORTED)
864 ast_log(LOG_WARNING,
865 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
866 else {
867 /* loop over expanded files */
868 int i;
869 for (i=0; i<globbuf.gl_pathc; i++) {
870 ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
871 #endif
872 do {
873 if (stat(fn, &statbuf))
874 continue;
876 if (!S_ISREG(statbuf.st_mode)) {
877 ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
878 continue;
880 if (option_verbose > 1) {
881 ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
882 fflush(stdout);
884 if (!(f = fopen(fn, "r"))) {
885 if (option_debug)
886 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
887 if (option_verbose > 1)
888 ast_verbose( "Not found (%s)\n", strerror(errno));
889 continue;
891 count++;
892 if (option_debug)
893 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
894 if (option_verbose > 1)
895 ast_verbose("Found\n");
896 while(!feof(f)) {
897 lineno++;
898 if (fgets(buf, sizeof(buf), f)) {
899 if ( withcomments ) {
900 CB_ADD(&comment_buffer, &comment_buffer_size, lline_buffer); /* add the current lline buffer to the comment buffer */
901 lline_buffer[0] = 0; /* erase the lline buffer */
904 new_buf = buf;
905 if (comment)
906 process_buf = NULL;
907 else
908 process_buf = buf;
910 while ((comment_p = strchr(new_buf, COMMENT_META))) {
911 if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
912 /* Escaped semicolons aren't comments. */
913 new_buf = comment_p + 1;
914 } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
915 /* Meta-Comment start detected ";--" */
916 if (comment < MAX_NESTED_COMMENTS) {
917 *comment_p = '\0';
918 new_buf = comment_p + 3;
919 comment++;
920 nest[comment-1] = lineno;
921 } else {
922 ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
924 } else if ((comment_p >= new_buf + 2) &&
925 (*(comment_p - 1) == COMMENT_TAG) &&
926 (*(comment_p - 2) == COMMENT_TAG)) {
927 /* Meta-Comment end detected */
928 comment--;
929 new_buf = comment_p + 1;
930 if (!comment) {
931 /* Back to non-comment now */
932 if (process_buf) {
933 /* Actually have to move what's left over the top, then continue */
934 char *oldptr;
935 oldptr = process_buf + strlen(process_buf);
936 if ( withcomments ) {
937 CB_ADD(&comment_buffer, &comment_buffer_size, ";");
938 CB_ADD_LEN(&comment_buffer, &comment_buffer_size, oldptr+1, new_buf-oldptr-1);
941 memmove(oldptr, new_buf, strlen(new_buf) + 1);
942 new_buf = oldptr;
943 } else
944 process_buf = new_buf;
946 } else {
947 if (!comment) {
948 /* If ; is found, and we are not nested in a comment,
949 we immediately stop all comment processing */
950 if ( withcomments ) {
951 LLB_ADD(&lline_buffer, &lline_buffer_size, comment_p);
953 *comment_p = '\0';
954 new_buf = comment_p;
955 } else
956 new_buf = comment_p + 1;
959 if( withcomments && comment && !process_buf )
961 CB_ADD(&comment_buffer, &comment_buffer_size, buf); /* the whole line is a comment, store it */
964 if (process_buf) {
965 char *buf = ast_strip(process_buf);
966 if (!ast_strlen_zero(buf)) {
967 if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size)) {
968 cfg = NULL;
969 break;
975 fclose(f);
976 } while(0);
977 if (comment) {
978 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment - 1]);
980 #ifdef AST_INCLUDE_GLOB
981 if (!cfg)
982 break;
984 globfree(&globbuf);
987 #endif
989 if (cfg && cfg->include_level == 1 && withcomments && comment_buffer) {
990 free(comment_buffer);
991 free(lline_buffer);
992 comment_buffer = NULL;
993 lline_buffer = NULL;
994 comment_buffer_size = 0;
995 lline_buffer_size = 0;
998 if (count == 0)
999 return NULL;
1001 return cfg;
1004 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
1006 FILE *f = NULL;
1007 int fd = -1;
1008 char fn[256], fntmp[256];
1009 char date[256]="";
1010 time_t t;
1011 struct ast_variable *var;
1012 struct ast_category *cat;
1013 struct ast_comment *cmt;
1014 struct stat s;
1015 int blanklines = 0;
1016 int stat_result = 0;
1018 if (configfile[0] == '/') {
1019 snprintf(fntmp, sizeof(fntmp), "%s.XXXXXX", configfile);
1020 ast_copy_string(fn, configfile, sizeof(fn));
1021 } else {
1022 snprintf(fntmp, sizeof(fntmp), "%s/%s.XXXXXX", ast_config_AST_CONFIG_DIR, configfile);
1023 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
1025 time(&t);
1026 ast_copy_string(date, ctime(&t), sizeof(date));
1027 if ((fd = mkstemp(fntmp)) > 0 && (f = fdopen(fd, "w")) != NULL) {
1028 if (option_verbose > 1)
1029 ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
1030 fprintf(f, ";!\n");
1031 fprintf(f, ";! Automatically generated configuration file\n");
1032 if (strcmp(configfile, fn))
1033 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
1034 else
1035 fprintf(f, ";! Filename: %s\n", configfile);
1036 fprintf(f, ";! Generator: %s\n", generator);
1037 fprintf(f, ";! Creation Date: %s", date);
1038 fprintf(f, ";!\n");
1039 cat = cfg->root;
1040 while(cat) {
1041 /* Dump section with any appropriate comment */
1042 for (cmt = cat->precomments; cmt; cmt=cmt->next)
1044 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1045 fprintf(f,"%s", cmt->cmt);
1047 if (!cat->precomments)
1048 fprintf(f,"\n");
1049 fprintf(f, "[%s]", cat->name);
1050 if (cat->ignored || !AST_LIST_EMPTY(&cat->template_instances)) {
1051 fprintf(f, "(");
1052 if (cat->ignored) {
1053 fprintf(f, "!");
1055 if (cat->ignored && !AST_LIST_EMPTY(&cat->template_instances)) {
1056 fprintf(f, ",");
1058 if (!AST_LIST_EMPTY(&cat->template_instances)) {
1059 struct ast_category_template_instance *x;
1060 AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
1061 fprintf(f,"%s",x->name);
1062 if (x != AST_LIST_LAST(&cat->template_instances))
1063 fprintf(f,",");
1066 fprintf(f, ")");
1068 for(cmt = cat->sameline; cmt; cmt=cmt->next)
1070 fprintf(f,"%s", cmt->cmt);
1072 if (!cat->sameline)
1073 fprintf(f,"\n");
1074 var = cat->root;
1075 while(var) {
1076 struct ast_category_template_instance *x;
1077 struct ast_variable *v2;
1078 int found = 0;
1079 AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
1081 for (v2 = x->inst->root; v2; v2 = v2->next) {
1082 if (!strcasecmp(var->name, v2->name))
1083 break;
1085 if (v2 && v2->value && !strcmp(v2->value, var->value)) {
1086 found = 1;
1087 break;
1090 if (found) {
1091 var = var->next;
1092 continue;
1094 for (cmt = var->precomments; cmt; cmt=cmt->next)
1096 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1097 fprintf(f,"%s", cmt->cmt);
1099 if (var->sameline)
1100 fprintf(f, "%s %s %s %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
1101 else
1102 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
1103 if (var->blanklines) {
1104 blanklines = var->blanklines;
1105 while (blanklines--)
1106 fprintf(f, "\n");
1109 var = var->next;
1111 #if 0
1112 /* Put an empty line */
1113 fprintf(f, "\n");
1114 #endif
1115 cat = cat->next;
1117 if ((option_verbose > 1) && !option_debug)
1118 ast_verbose("Saved\n");
1119 } else {
1120 if (option_debug)
1121 ast_log(LOG_DEBUG, "Unable to open for writing: %s (%s)\n", fn, strerror(errno));
1122 if (option_verbose > 1)
1123 ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno));
1124 if (fd > -1)
1125 close(fd);
1126 return -1;
1128 if (!(stat_result = stat(fn, &s))) {
1129 fchmod(fd, s.st_mode);
1131 fclose(f);
1132 if ((!stat_result && unlink(fn)) || link(fntmp, fn)) {
1133 if (option_debug)
1134 ast_log(LOG_DEBUG, "Unable to open for writing: %s (%s)\n", fn, strerror(errno));
1135 if (option_verbose > 1)
1136 ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno));
1137 unlink(fntmp);
1138 return -1;
1140 unlink(fntmp);
1141 return 0;
1144 static void clear_config_maps(void)
1146 struct ast_config_map *map;
1148 ast_mutex_lock(&config_lock);
1150 while (config_maps) {
1151 map = config_maps;
1152 config_maps = config_maps->next;
1153 free(map);
1156 ast_mutex_unlock(&config_lock);
1159 static int append_mapping(char *name, char *driver, char *database, char *table)
1161 struct ast_config_map *map;
1162 int length;
1164 length = sizeof(*map);
1165 length += strlen(name) + 1;
1166 length += strlen(driver) + 1;
1167 length += strlen(database) + 1;
1168 if (table)
1169 length += strlen(table) + 1;
1171 if (!(map = ast_calloc(1, length)))
1172 return -1;
1174 map->name = map->stuff;
1175 strcpy(map->name, name);
1176 map->driver = map->name + strlen(map->name) + 1;
1177 strcpy(map->driver, driver);
1178 map->database = map->driver + strlen(map->driver) + 1;
1179 strcpy(map->database, database);
1180 if (table) {
1181 map->table = map->database + strlen(map->database) + 1;
1182 strcpy(map->table, table);
1184 map->next = config_maps;
1186 if (option_verbose > 1)
1187 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
1188 map->name, map->driver, map->database, map->table ? map->table : map->name);
1190 config_maps = map;
1191 return 0;
1194 int read_config_maps(void)
1196 struct ast_config *config, *configtmp;
1197 struct ast_variable *v;
1198 char *driver, *table, *database, *stringp, *tmp;
1200 clear_config_maps();
1202 configtmp = ast_config_new();
1203 configtmp->max_include_level = 1;
1204 config = ast_config_internal_load(extconfig_conf, configtmp, 0);
1205 if (!config) {
1206 ast_config_destroy(configtmp);
1207 return 0;
1210 for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
1211 stringp = v->value;
1212 driver = strsep(&stringp, ",");
1214 if ((tmp = strchr(stringp, '\"')))
1215 stringp = tmp;
1217 /* check if the database text starts with a double quote */
1218 if (*stringp == '"') {
1219 stringp++;
1220 database = strsep(&stringp, "\"");
1221 strsep(&stringp, ",");
1222 } else {
1223 /* apparently this text has no quotes */
1224 database = strsep(&stringp, ",");
1227 table = strsep(&stringp, ",");
1229 if (!strcmp(v->name, extconfig_conf)) {
1230 ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
1231 continue;
1234 if (!strcmp(v->name, "asterisk.conf")) {
1235 ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
1236 continue;
1239 if (!strcmp(v->name, "logger.conf")) {
1240 ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
1241 continue;
1244 if (!driver || !database)
1245 continue;
1246 if (!strcasecmp(v->name, "sipfriends")) {
1247 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");
1248 append_mapping("sipusers", driver, database, table ? table : "sipfriends");
1249 append_mapping("sippeers", driver, database, table ? table : "sipfriends");
1250 } else if (!strcasecmp(v->name, "iaxfriends")) {
1251 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");
1252 append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
1253 append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
1254 } else
1255 append_mapping(v->name, driver, database, table);
1258 ast_config_destroy(config);
1259 return 0;
1262 int ast_config_engine_register(struct ast_config_engine *new)
1264 struct ast_config_engine *ptr;
1266 ast_mutex_lock(&config_lock);
1268 if (!config_engine_list) {
1269 config_engine_list = new;
1270 } else {
1271 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
1272 ptr->next = new;
1275 ast_mutex_unlock(&config_lock);
1276 ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
1278 return 1;
1281 int ast_config_engine_deregister(struct ast_config_engine *del)
1283 struct ast_config_engine *ptr, *last=NULL;
1285 ast_mutex_lock(&config_lock);
1287 for (ptr = config_engine_list; ptr; ptr=ptr->next) {
1288 if (ptr == del) {
1289 if (last)
1290 last->next = ptr->next;
1291 else
1292 config_engine_list = ptr->next;
1293 break;
1295 last = ptr;
1298 ast_mutex_unlock(&config_lock);
1300 return 0;
1303 /*! \brief Find realtime engine for realtime family */
1304 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz)
1306 struct ast_config_engine *eng, *ret = NULL;
1307 struct ast_config_map *map;
1309 ast_mutex_lock(&config_lock);
1311 for (map = config_maps; map; map = map->next) {
1312 if (!strcasecmp(family, map->name)) {
1313 if (database)
1314 ast_copy_string(database, map->database, dbsiz);
1315 if (table)
1316 ast_copy_string(table, map->table ? map->table : family, tabsiz);
1317 break;
1321 /* Check if the required driver (engine) exist */
1322 if (map) {
1323 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
1324 if (!strcasecmp(eng->name, map->driver))
1325 ret = eng;
1329 ast_mutex_unlock(&config_lock);
1331 /* if we found a mapping, but the engine is not available, then issue a warning */
1332 if (map && !ret)
1333 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
1335 return ret;
1338 static struct ast_config_engine text_file_engine = {
1339 .name = "text",
1340 .load_func = config_text_file_load,
1343 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
1345 char db[256];
1346 char table[256];
1347 struct ast_config_engine *loader = &text_file_engine;
1348 struct ast_config *result;
1350 /* The config file itself bumps include_level by 1 */
1351 if (cfg->max_include_level > 0 && cfg->include_level == cfg->max_include_level + 1) {
1352 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
1353 return NULL;
1356 cfg->include_level++;
1358 if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
1359 struct ast_config_engine *eng;
1361 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
1364 if (eng && eng->load_func) {
1365 loader = eng;
1366 } else {
1367 eng = find_engine("global", db, sizeof(db), table, sizeof(table));
1368 if (eng && eng->load_func)
1369 loader = eng;
1373 result = loader->load_func(db, table, filename, cfg, withcomments);
1375 if (result)
1376 result->include_level--;
1377 else
1378 cfg->include_level--;
1380 return result;
1383 struct ast_config *ast_config_load(const char *filename)
1385 struct ast_config *cfg;
1386 struct ast_config *result;
1388 cfg = ast_config_new();
1389 if (!cfg)
1390 return NULL;
1392 result = ast_config_internal_load(filename, cfg, 0);
1393 if (!result)
1394 ast_config_destroy(cfg);
1396 return result;
1399 struct ast_config *ast_config_load_with_comments(const char *filename)
1401 struct ast_config *cfg;
1402 struct ast_config *result;
1404 cfg = ast_config_new();
1405 if (!cfg)
1406 return NULL;
1408 result = ast_config_internal_load(filename, cfg, 1);
1409 if (!result)
1410 ast_config_destroy(cfg);
1412 return result;
1415 struct ast_variable *ast_load_realtime(const char *family, ...)
1417 struct ast_config_engine *eng;
1418 char db[256]="";
1419 char table[256]="";
1420 struct ast_variable *res=NULL;
1421 va_list ap;
1423 va_start(ap, family);
1424 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1425 if (eng && eng->realtime_func)
1426 res = eng->realtime_func(db, table, ap);
1427 va_end(ap);
1429 return res;
1432 /*! \brief Check if realtime engine is configured for family */
1433 int ast_check_realtime(const char *family)
1435 struct ast_config_engine *eng;
1437 eng = find_engine(family, NULL, 0, NULL, 0);
1438 if (eng)
1439 return 1;
1440 return 0;
1444 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
1446 struct ast_config_engine *eng;
1447 char db[256]="";
1448 char table[256]="";
1449 struct ast_config *res=NULL;
1450 va_list ap;
1452 va_start(ap, family);
1453 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1454 if (eng && eng->realtime_multi_func)
1455 res = eng->realtime_multi_func(db, table, ap);
1456 va_end(ap);
1458 return res;
1461 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1463 struct ast_config_engine *eng;
1464 int res = -1;
1465 char db[256]="";
1466 char table[256]="";
1467 va_list ap;
1469 va_start(ap, lookup);
1470 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1471 if (eng && eng->update_func)
1472 res = eng->update_func(db, table, keyfield, lookup, ap);
1473 va_end(ap);
1475 return res;
1478 static int config_command(int fd, int argc, char **argv)
1480 struct ast_config_engine *eng;
1481 struct ast_config_map *map;
1483 ast_mutex_lock(&config_lock);
1485 ast_cli(fd, "\n\n");
1486 for (eng = config_engine_list; eng; eng = eng->next) {
1487 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1488 for (map = config_maps; map; map = map->next)
1489 if (!strcasecmp(map->driver, eng->name)) {
1490 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1491 map->table ? map->table : map->name);
1494 ast_cli(fd,"\n\n");
1496 ast_mutex_unlock(&config_lock);
1498 return 0;
1501 static char show_config_help[] =
1502 "Usage: core show config mappings\n"
1503 " Shows the filenames to config engines.\n";
1505 static struct ast_cli_entry cli_show_config_mappings_deprecated = {
1506 { "show", "config", "mappings", NULL },
1507 config_command, NULL,
1508 NULL };
1510 static struct ast_cli_entry cli_config[] = {
1511 { { "core", "show", "config", "mappings", NULL },
1512 config_command, "Display config mappings (file names to config engines)",
1513 show_config_help, NULL, &cli_show_config_mappings_deprecated },
1516 int register_config_cli()
1518 ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));
1519 return 0;