fix a flaw found while experimenting with structure alignment and padding; low-fence...
[asterisk-bristuff.git] / main / config.c
blob84ef8ad4e1aa9a8f708e8e53c805378b8ee1f5b0
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;
1017 if (configfile[0] == '/') {
1018 snprintf(fntmp, sizeof(fntmp), "%s.XXXXXX", configfile);
1019 ast_copy_string(fn, configfile, sizeof(fn));
1020 } else {
1021 snprintf(fntmp, sizeof(fntmp), "%s/%s.XXXXXX", ast_config_AST_CONFIG_DIR, configfile);
1022 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
1024 time(&t);
1025 ast_copy_string(date, ctime(&t), sizeof(date));
1026 if ((fd = mkstemp(fntmp)) > 0 && (f = fdopen(fd, "w")) != NULL) {
1027 if (option_verbose > 1)
1028 ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
1029 fprintf(f, ";!\n");
1030 fprintf(f, ";! Automatically generated configuration file\n");
1031 if (strcmp(configfile, fn))
1032 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
1033 else
1034 fprintf(f, ";! Filename: %s\n", configfile);
1035 fprintf(f, ";! Generator: %s\n", generator);
1036 fprintf(f, ";! Creation Date: %s", date);
1037 fprintf(f, ";!\n");
1038 cat = cfg->root;
1039 while(cat) {
1040 /* Dump section with any appropriate comment */
1041 for (cmt = cat->precomments; cmt; cmt=cmt->next)
1043 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1044 fprintf(f,"%s", cmt->cmt);
1046 if (!cat->precomments)
1047 fprintf(f,"\n");
1048 fprintf(f, "[%s]", cat->name);
1049 if (cat->ignored || !AST_LIST_EMPTY(&cat->template_instances)) {
1050 fprintf(f, "(");
1051 if (cat->ignored) {
1052 fprintf(f, "!");
1054 if (cat->ignored && !AST_LIST_EMPTY(&cat->template_instances)) {
1055 fprintf(f, ",");
1057 if (!AST_LIST_EMPTY(&cat->template_instances)) {
1058 struct ast_category_template_instance *x;
1059 AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
1060 fprintf(f,"%s",x->name);
1061 if (x != AST_LIST_LAST(&cat->template_instances))
1062 fprintf(f,",");
1065 fprintf(f, ")");
1067 for(cmt = cat->sameline; cmt; cmt=cmt->next)
1069 fprintf(f,"%s", cmt->cmt);
1071 if (!cat->sameline)
1072 fprintf(f,"\n");
1073 var = cat->root;
1074 while(var) {
1075 struct ast_category_template_instance *x;
1076 struct ast_variable *v2;
1077 int found = 0;
1078 AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
1080 for (v2 = x->inst->root; v2; v2 = v2->next) {
1081 if (!strcasecmp(var->name, v2->name))
1082 break;
1084 if (v2 && v2->value && !strcmp(v2->value, var->value)) {
1085 found = 1;
1086 break;
1089 if (found) {
1090 var = var->next;
1091 continue;
1093 for (cmt = var->precomments; cmt; cmt=cmt->next)
1095 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1096 fprintf(f,"%s", cmt->cmt);
1098 if (var->sameline)
1099 fprintf(f, "%s %s %s %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
1100 else
1101 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
1102 if (var->blanklines) {
1103 blanklines = var->blanklines;
1104 while (blanklines--)
1105 fprintf(f, "\n");
1108 var = var->next;
1110 #if 0
1111 /* Put an empty line */
1112 fprintf(f, "\n");
1113 #endif
1114 cat = cat->next;
1116 if ((option_verbose > 1) && !option_debug)
1117 ast_verbose("Saved\n");
1118 } else {
1119 if (option_debug)
1120 ast_log(LOG_DEBUG, "Unable to open for writing: %s (%s)\n", fn, strerror(errno));
1121 if (option_verbose > 1)
1122 ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno));
1123 if (fd > -1)
1124 close(fd);
1125 return -1;
1127 stat(fn, &s);
1128 fchmod(fd, s.st_mode);
1129 fclose(f);
1130 if (unlink(fn) || link(fntmp, fn)) {
1131 if (option_debug)
1132 ast_log(LOG_DEBUG, "Unable to open for writing: %s (%s)\n", fn, strerror(errno));
1133 if (option_verbose > 1)
1134 ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno));
1135 unlink(fntmp);
1136 return -1;
1138 unlink(fntmp);
1139 return 0;
1142 static void clear_config_maps(void)
1144 struct ast_config_map *map;
1146 ast_mutex_lock(&config_lock);
1148 while (config_maps) {
1149 map = config_maps;
1150 config_maps = config_maps->next;
1151 free(map);
1154 ast_mutex_unlock(&config_lock);
1157 static int append_mapping(char *name, char *driver, char *database, char *table)
1159 struct ast_config_map *map;
1160 int length;
1162 length = sizeof(*map);
1163 length += strlen(name) + 1;
1164 length += strlen(driver) + 1;
1165 length += strlen(database) + 1;
1166 if (table)
1167 length += strlen(table) + 1;
1169 if (!(map = ast_calloc(1, length)))
1170 return -1;
1172 map->name = map->stuff;
1173 strcpy(map->name, name);
1174 map->driver = map->name + strlen(map->name) + 1;
1175 strcpy(map->driver, driver);
1176 map->database = map->driver + strlen(map->driver) + 1;
1177 strcpy(map->database, database);
1178 if (table) {
1179 map->table = map->database + strlen(map->database) + 1;
1180 strcpy(map->table, table);
1182 map->next = config_maps;
1184 if (option_verbose > 1)
1185 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
1186 map->name, map->driver, map->database, map->table ? map->table : map->name);
1188 config_maps = map;
1189 return 0;
1192 int read_config_maps(void)
1194 struct ast_config *config, *configtmp;
1195 struct ast_variable *v;
1196 char *driver, *table, *database, *stringp, *tmp;
1198 clear_config_maps();
1200 configtmp = ast_config_new();
1201 configtmp->max_include_level = 1;
1202 config = ast_config_internal_load(extconfig_conf, configtmp, 0);
1203 if (!config) {
1204 ast_config_destroy(configtmp);
1205 return 0;
1208 for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
1209 stringp = v->value;
1210 driver = strsep(&stringp, ",");
1212 if ((tmp = strchr(stringp, '\"')))
1213 stringp = tmp;
1215 /* check if the database text starts with a double quote */
1216 if (*stringp == '"') {
1217 stringp++;
1218 database = strsep(&stringp, "\"");
1219 strsep(&stringp, ",");
1220 } else {
1221 /* apparently this text has no quotes */
1222 database = strsep(&stringp, ",");
1225 table = strsep(&stringp, ",");
1227 if (!strcmp(v->name, extconfig_conf)) {
1228 ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
1229 continue;
1232 if (!strcmp(v->name, "asterisk.conf")) {
1233 ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
1234 continue;
1237 if (!strcmp(v->name, "logger.conf")) {
1238 ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
1239 continue;
1242 if (!driver || !database)
1243 continue;
1244 if (!strcasecmp(v->name, "sipfriends")) {
1245 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");
1246 append_mapping("sipusers", driver, database, table ? table : "sipfriends");
1247 append_mapping("sippeers", driver, database, table ? table : "sipfriends");
1248 } else if (!strcasecmp(v->name, "iaxfriends")) {
1249 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");
1250 append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
1251 append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
1252 } else
1253 append_mapping(v->name, driver, database, table);
1256 ast_config_destroy(config);
1257 return 0;
1260 int ast_config_engine_register(struct ast_config_engine *new)
1262 struct ast_config_engine *ptr;
1264 ast_mutex_lock(&config_lock);
1266 if (!config_engine_list) {
1267 config_engine_list = new;
1268 } else {
1269 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
1270 ptr->next = new;
1273 ast_mutex_unlock(&config_lock);
1274 ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
1276 return 1;
1279 int ast_config_engine_deregister(struct ast_config_engine *del)
1281 struct ast_config_engine *ptr, *last=NULL;
1283 ast_mutex_lock(&config_lock);
1285 for (ptr = config_engine_list; ptr; ptr=ptr->next) {
1286 if (ptr == del) {
1287 if (last)
1288 last->next = ptr->next;
1289 else
1290 config_engine_list = ptr->next;
1291 break;
1293 last = ptr;
1296 ast_mutex_unlock(&config_lock);
1298 return 0;
1301 /*! \brief Find realtime engine for realtime family */
1302 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz)
1304 struct ast_config_engine *eng, *ret = NULL;
1305 struct ast_config_map *map;
1307 ast_mutex_lock(&config_lock);
1309 for (map = config_maps; map; map = map->next) {
1310 if (!strcasecmp(family, map->name)) {
1311 if (database)
1312 ast_copy_string(database, map->database, dbsiz);
1313 if (table)
1314 ast_copy_string(table, map->table ? map->table : family, tabsiz);
1315 break;
1319 /* Check if the required driver (engine) exist */
1320 if (map) {
1321 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
1322 if (!strcasecmp(eng->name, map->driver))
1323 ret = eng;
1327 ast_mutex_unlock(&config_lock);
1329 /* if we found a mapping, but the engine is not available, then issue a warning */
1330 if (map && !ret)
1331 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
1333 return ret;
1336 static struct ast_config_engine text_file_engine = {
1337 .name = "text",
1338 .load_func = config_text_file_load,
1341 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
1343 char db[256];
1344 char table[256];
1345 struct ast_config_engine *loader = &text_file_engine;
1346 struct ast_config *result;
1348 /* The config file itself bumps include_level by 1 */
1349 if (cfg->max_include_level > 0 && cfg->include_level == cfg->max_include_level + 1) {
1350 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
1351 return NULL;
1354 cfg->include_level++;
1356 if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
1357 struct ast_config_engine *eng;
1359 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
1362 if (eng && eng->load_func) {
1363 loader = eng;
1364 } else {
1365 eng = find_engine("global", db, sizeof(db), table, sizeof(table));
1366 if (eng && eng->load_func)
1367 loader = eng;
1371 result = loader->load_func(db, table, filename, cfg, withcomments);
1373 if (result)
1374 result->include_level--;
1375 else
1376 cfg->include_level--;
1378 return result;
1381 struct ast_config *ast_config_load(const char *filename)
1383 struct ast_config *cfg;
1384 struct ast_config *result;
1386 cfg = ast_config_new();
1387 if (!cfg)
1388 return NULL;
1390 result = ast_config_internal_load(filename, cfg, 0);
1391 if (!result)
1392 ast_config_destroy(cfg);
1394 return result;
1397 struct ast_config *ast_config_load_with_comments(const char *filename)
1399 struct ast_config *cfg;
1400 struct ast_config *result;
1402 cfg = ast_config_new();
1403 if (!cfg)
1404 return NULL;
1406 result = ast_config_internal_load(filename, cfg, 1);
1407 if (!result)
1408 ast_config_destroy(cfg);
1410 return result;
1413 struct ast_variable *ast_load_realtime(const char *family, ...)
1415 struct ast_config_engine *eng;
1416 char db[256]="";
1417 char table[256]="";
1418 struct ast_variable *res=NULL;
1419 va_list ap;
1421 va_start(ap, family);
1422 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1423 if (eng && eng->realtime_func)
1424 res = eng->realtime_func(db, table, ap);
1425 va_end(ap);
1427 return res;
1430 /*! \brief Check if realtime engine is configured for family */
1431 int ast_check_realtime(const char *family)
1433 struct ast_config_engine *eng;
1435 eng = find_engine(family, NULL, 0, NULL, 0);
1436 if (eng)
1437 return 1;
1438 return 0;
1442 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
1444 struct ast_config_engine *eng;
1445 char db[256]="";
1446 char table[256]="";
1447 struct ast_config *res=NULL;
1448 va_list ap;
1450 va_start(ap, family);
1451 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1452 if (eng && eng->realtime_multi_func)
1453 res = eng->realtime_multi_func(db, table, ap);
1454 va_end(ap);
1456 return res;
1459 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1461 struct ast_config_engine *eng;
1462 int res = -1;
1463 char db[256]="";
1464 char table[256]="";
1465 va_list ap;
1467 va_start(ap, lookup);
1468 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1469 if (eng && eng->update_func)
1470 res = eng->update_func(db, table, keyfield, lookup, ap);
1471 va_end(ap);
1473 return res;
1476 static int config_command(int fd, int argc, char **argv)
1478 struct ast_config_engine *eng;
1479 struct ast_config_map *map;
1481 ast_mutex_lock(&config_lock);
1483 ast_cli(fd, "\n\n");
1484 for (eng = config_engine_list; eng; eng = eng->next) {
1485 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1486 for (map = config_maps; map; map = map->next)
1487 if (!strcasecmp(map->driver, eng->name)) {
1488 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1489 map->table ? map->table : map->name);
1492 ast_cli(fd,"\n\n");
1494 ast_mutex_unlock(&config_lock);
1496 return 0;
1499 static char show_config_help[] =
1500 "Usage: core show config mappings\n"
1501 " Shows the filenames to config engines.\n";
1503 static struct ast_cli_entry cli_show_config_mappings_deprecated = {
1504 { "show", "config", "mappings", NULL },
1505 config_command, NULL,
1506 NULL };
1508 static struct ast_cli_entry cli_config[] = {
1509 { { "core", "show", "config", "mappings", NULL },
1510 config_command, "Display config mappings (file names to config engines)",
1511 show_config_help, NULL, &cli_show_config_mappings_deprecated },
1514 int register_config_cli()
1516 ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));
1517 return 0;