when a PRI call must be moved to a different B channel at the request of the other...
[asterisk-bristuff.git] / main / config.c
blob2740640a5563ccd5dfdba9af8c7270ade6e65c49
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 char buf[8192];
816 char *new_buf, *comment_p, *process_buf;
817 FILE *f;
818 int lineno=0;
819 int comment = 0, nest[MAX_NESTED_COMMENTS];
820 struct ast_category *cat = NULL;
821 int count = 0;
822 struct stat statbuf;
823 /*! Growable string buffer */
824 char *comment_buffer=0; /*!< this will be a comment collector.*/
825 int comment_buffer_size=0; /*!< the amount of storage so far alloc'd for the comment_buffer */
827 char *lline_buffer=0; /*!< A buffer for stuff behind the ; */
828 int lline_buffer_size=0;
831 cat = ast_config_get_current_category(cfg);
833 if (filename[0] == '/') {
834 ast_copy_string(fn, filename, sizeof(fn));
835 } else {
836 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
839 if (withcomments) {
840 CB_INIT(&comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size);
841 if (!lline_buffer || !comment_buffer) {
842 ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
843 return NULL;
846 #ifdef AST_INCLUDE_GLOB
848 int glob_ret;
849 glob_t globbuf;
850 globbuf.gl_offs = 0; /* initialize it to silence gcc */
851 #ifdef SOLARIS
852 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
853 #else
854 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
855 #endif
856 if (glob_ret == GLOB_NOSPACE)
857 ast_log(LOG_WARNING,
858 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
859 else if (glob_ret == GLOB_ABORTED)
860 ast_log(LOG_WARNING,
861 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
862 else {
863 /* loop over expanded files */
864 int i;
865 for (i=0; i<globbuf.gl_pathc; i++) {
866 ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
867 #endif
868 do {
869 if (stat(fn, &statbuf))
870 continue;
872 if (!S_ISREG(statbuf.st_mode)) {
873 ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
874 continue;
876 if (option_verbose > 1) {
877 ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
878 fflush(stdout);
880 if (!(f = fopen(fn, "r"))) {
881 if (option_debug)
882 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
883 if (option_verbose > 1)
884 ast_verbose( "Not found (%s)\n", strerror(errno));
885 continue;
887 count++;
888 if (option_debug)
889 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
890 if (option_verbose > 1)
891 ast_verbose("Found\n");
892 while(!feof(f)) {
893 lineno++;
894 if (fgets(buf, sizeof(buf), f)) {
895 if ( withcomments ) {
896 CB_ADD(&comment_buffer, &comment_buffer_size, lline_buffer); /* add the current lline buffer to the comment buffer */
897 lline_buffer[0] = 0; /* erase the lline buffer */
900 new_buf = buf;
901 if (comment)
902 process_buf = NULL;
903 else
904 process_buf = buf;
906 while ((comment_p = strchr(new_buf, COMMENT_META))) {
907 if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
908 /* Escaped semicolons aren't comments. */
909 new_buf = comment_p + 1;
910 } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
911 /* Meta-Comment start detected ";--" */
912 if (comment < MAX_NESTED_COMMENTS) {
913 *comment_p = '\0';
914 new_buf = comment_p + 3;
915 comment++;
916 nest[comment-1] = lineno;
917 } else {
918 ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
920 } else if ((comment_p >= new_buf + 2) &&
921 (*(comment_p - 1) == COMMENT_TAG) &&
922 (*(comment_p - 2) == COMMENT_TAG)) {
923 /* Meta-Comment end detected */
924 comment--;
925 new_buf = comment_p + 1;
926 if (!comment) {
927 /* Back to non-comment now */
928 if (process_buf) {
929 /* Actually have to move what's left over the top, then continue */
930 char *oldptr;
931 oldptr = process_buf + strlen(process_buf);
932 if ( withcomments ) {
933 CB_ADD(&comment_buffer, &comment_buffer_size, ";");
934 CB_ADD_LEN(&comment_buffer, &comment_buffer_size, oldptr+1, new_buf-oldptr-1);
937 memmove(oldptr, new_buf, strlen(new_buf) + 1);
938 new_buf = oldptr;
939 } else
940 process_buf = new_buf;
942 } else {
943 if (!comment) {
944 /* If ; is found, and we are not nested in a comment,
945 we immediately stop all comment processing */
946 if ( withcomments ) {
947 LLB_ADD(&lline_buffer, &lline_buffer_size, comment_p);
949 *comment_p = '\0';
950 new_buf = comment_p;
951 } else
952 new_buf = comment_p + 1;
955 if( withcomments && comment && !process_buf )
957 CB_ADD(&comment_buffer, &comment_buffer_size, buf); /* the whole line is a comment, store it */
960 if (process_buf) {
961 char *buf = ast_strip(process_buf);
962 if (!ast_strlen_zero(buf)) {
963 if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size)) {
964 cfg = NULL;
965 break;
971 fclose(f);
972 } while(0);
973 if (comment) {
974 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment - 1]);
976 #ifdef AST_INCLUDE_GLOB
977 if (!cfg)
978 break;
980 globfree(&globbuf);
983 #endif
985 if (cfg && cfg->include_level == 1 && withcomments && comment_buffer) {
986 free(comment_buffer);
987 free(lline_buffer);
988 comment_buffer = NULL;
989 lline_buffer = NULL;
990 comment_buffer_size = 0;
991 lline_buffer_size = 0;
994 if (count == 0)
995 return NULL;
997 return cfg;
1000 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
1002 FILE *f = NULL;
1003 int fd = -1;
1004 char fn[256], fntmp[256];
1005 char date[256]="";
1006 time_t t;
1007 struct ast_variable *var;
1008 struct ast_category *cat;
1009 struct ast_comment *cmt;
1010 struct stat s;
1011 int blanklines = 0;
1013 if (configfile[0] == '/') {
1014 snprintf(fntmp, sizeof(fntmp), "%s.XXXXXX", configfile);
1015 ast_copy_string(fn, configfile, sizeof(fn));
1016 } else {
1017 snprintf(fntmp, sizeof(fntmp), "%s/%s.XXXXXX", ast_config_AST_CONFIG_DIR, configfile);
1018 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
1020 time(&t);
1021 ast_copy_string(date, ctime(&t), sizeof(date));
1022 if ((fd = mkstemp(fntmp)) > 0 && (f = fdopen(fd, "w")) != NULL) {
1023 if (option_verbose > 1)
1024 ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
1025 fprintf(f, ";!\n");
1026 fprintf(f, ";! Automatically generated configuration file\n");
1027 if (strcmp(configfile, fn))
1028 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
1029 else
1030 fprintf(f, ";! Filename: %s\n", configfile);
1031 fprintf(f, ";! Generator: %s\n", generator);
1032 fprintf(f, ";! Creation Date: %s", date);
1033 fprintf(f, ";!\n");
1034 cat = cfg->root;
1035 while(cat) {
1036 /* Dump section with any appropriate comment */
1037 for (cmt = cat->precomments; cmt; cmt=cmt->next)
1039 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1040 fprintf(f,"%s", cmt->cmt);
1042 if (!cat->precomments)
1043 fprintf(f,"\n");
1044 fprintf(f, "[%s]", cat->name);
1045 if (cat->ignored)
1046 fprintf(f, "(!)");
1047 if (!AST_LIST_EMPTY(&cat->template_instances)) {
1048 struct ast_category_template_instance *x;
1049 fprintf(f, "(");
1050 AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
1051 fprintf(f,"%s",x->name);
1052 if (x != AST_LIST_LAST(&cat->template_instances))
1053 fprintf(f,",");
1055 fprintf(f, ")");
1057 for(cmt = cat->sameline; cmt; cmt=cmt->next)
1059 fprintf(f,"%s", cmt->cmt);
1061 if (!cat->sameline)
1062 fprintf(f,"\n");
1063 var = cat->root;
1064 while(var) {
1065 struct ast_category_template_instance *x;
1066 int found = 0;
1067 AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
1068 const char *pvalue = ast_variable_retrieve(cfg, x->name, var->name);
1069 if (pvalue && !strcmp(pvalue, var->value)) {
1070 found = 1;
1071 break;
1074 if (found) {
1075 var = var->next;
1076 continue;
1078 for (cmt = var->precomments; cmt; cmt=cmt->next)
1080 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1081 fprintf(f,"%s", cmt->cmt);
1083 if (var->sameline)
1084 fprintf(f, "%s %s %s %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
1085 else
1086 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
1087 if (var->blanklines) {
1088 blanklines = var->blanklines;
1089 while (blanklines--)
1090 fprintf(f, "\n");
1093 var = var->next;
1095 #if 0
1096 /* Put an empty line */
1097 fprintf(f, "\n");
1098 #endif
1099 cat = cat->next;
1101 if ((option_verbose > 1) && !option_debug)
1102 ast_verbose("Saved\n");
1103 } else {
1104 if (option_debug)
1105 ast_log(LOG_DEBUG, "Unable to open for writing: %s (%s)\n", fn, strerror(errno));
1106 if (option_verbose > 1)
1107 ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno));
1108 if (fd > -1)
1109 close(fd);
1110 return -1;
1112 stat(fn, &s);
1113 fchmod(fd, s.st_mode);
1114 fclose(f);
1115 if (unlink(fn) || link(fntmp, fn)) {
1116 if (option_debug)
1117 ast_log(LOG_DEBUG, "Unable to open for writing: %s (%s)\n", fn, strerror(errno));
1118 if (option_verbose > 1)
1119 ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno));
1120 unlink(fntmp);
1121 return -1;
1123 unlink(fntmp);
1124 return 0;
1127 static void clear_config_maps(void)
1129 struct ast_config_map *map;
1131 ast_mutex_lock(&config_lock);
1133 while (config_maps) {
1134 map = config_maps;
1135 config_maps = config_maps->next;
1136 free(map);
1139 ast_mutex_unlock(&config_lock);
1142 static int append_mapping(char *name, char *driver, char *database, char *table)
1144 struct ast_config_map *map;
1145 int length;
1147 length = sizeof(*map);
1148 length += strlen(name) + 1;
1149 length += strlen(driver) + 1;
1150 length += strlen(database) + 1;
1151 if (table)
1152 length += strlen(table) + 1;
1154 if (!(map = ast_calloc(1, length)))
1155 return -1;
1157 map->name = map->stuff;
1158 strcpy(map->name, name);
1159 map->driver = map->name + strlen(map->name) + 1;
1160 strcpy(map->driver, driver);
1161 map->database = map->driver + strlen(map->driver) + 1;
1162 strcpy(map->database, database);
1163 if (table) {
1164 map->table = map->database + strlen(map->database) + 1;
1165 strcpy(map->table, table);
1167 map->next = config_maps;
1169 if (option_verbose > 1)
1170 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
1171 map->name, map->driver, map->database, map->table ? map->table : map->name);
1173 config_maps = map;
1174 return 0;
1177 int read_config_maps(void)
1179 struct ast_config *config, *configtmp;
1180 struct ast_variable *v;
1181 char *driver, *table, *database, *stringp, *tmp;
1183 clear_config_maps();
1185 configtmp = ast_config_new();
1186 configtmp->max_include_level = 1;
1187 config = ast_config_internal_load(extconfig_conf, configtmp, 0);
1188 if (!config) {
1189 ast_config_destroy(configtmp);
1190 return 0;
1193 for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
1194 stringp = v->value;
1195 driver = strsep(&stringp, ",");
1197 if ((tmp = strchr(stringp, '\"')))
1198 stringp = tmp;
1200 /* check if the database text starts with a double quote */
1201 if (*stringp == '"') {
1202 stringp++;
1203 database = strsep(&stringp, "\"");
1204 strsep(&stringp, ",");
1205 } else {
1206 /* apparently this text has no quotes */
1207 database = strsep(&stringp, ",");
1210 table = strsep(&stringp, ",");
1212 if (!strcmp(v->name, extconfig_conf)) {
1213 ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
1214 continue;
1217 if (!strcmp(v->name, "asterisk.conf")) {
1218 ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
1219 continue;
1222 if (!strcmp(v->name, "logger.conf")) {
1223 ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
1224 continue;
1227 if (!driver || !database)
1228 continue;
1229 if (!strcasecmp(v->name, "sipfriends")) {
1230 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");
1231 append_mapping("sipusers", driver, database, table ? table : "sipfriends");
1232 append_mapping("sippeers", driver, database, table ? table : "sipfriends");
1233 } else if (!strcasecmp(v->name, "iaxfriends")) {
1234 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");
1235 append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
1236 append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
1237 } else
1238 append_mapping(v->name, driver, database, table);
1241 ast_config_destroy(config);
1242 return 0;
1245 int ast_config_engine_register(struct ast_config_engine *new)
1247 struct ast_config_engine *ptr;
1249 ast_mutex_lock(&config_lock);
1251 if (!config_engine_list) {
1252 config_engine_list = new;
1253 } else {
1254 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
1255 ptr->next = new;
1258 ast_mutex_unlock(&config_lock);
1259 ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
1261 return 1;
1264 int ast_config_engine_deregister(struct ast_config_engine *del)
1266 struct ast_config_engine *ptr, *last=NULL;
1268 ast_mutex_lock(&config_lock);
1270 for (ptr = config_engine_list; ptr; ptr=ptr->next) {
1271 if (ptr == del) {
1272 if (last)
1273 last->next = ptr->next;
1274 else
1275 config_engine_list = ptr->next;
1276 break;
1278 last = ptr;
1281 ast_mutex_unlock(&config_lock);
1283 return 0;
1286 /*! \brief Find realtime engine for realtime family */
1287 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz)
1289 struct ast_config_engine *eng, *ret = NULL;
1290 struct ast_config_map *map;
1292 ast_mutex_lock(&config_lock);
1294 for (map = config_maps; map; map = map->next) {
1295 if (!strcasecmp(family, map->name)) {
1296 if (database)
1297 ast_copy_string(database, map->database, dbsiz);
1298 if (table)
1299 ast_copy_string(table, map->table ? map->table : family, tabsiz);
1300 break;
1304 /* Check if the required driver (engine) exist */
1305 if (map) {
1306 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
1307 if (!strcasecmp(eng->name, map->driver))
1308 ret = eng;
1312 ast_mutex_unlock(&config_lock);
1314 /* if we found a mapping, but the engine is not available, then issue a warning */
1315 if (map && !ret)
1316 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
1318 return ret;
1321 static struct ast_config_engine text_file_engine = {
1322 .name = "text",
1323 .load_func = config_text_file_load,
1326 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
1328 char db[256];
1329 char table[256];
1330 struct ast_config_engine *loader = &text_file_engine;
1331 struct ast_config *result;
1333 /* The config file itself bumps include_level by 1 */
1334 if (cfg->max_include_level > 0 && cfg->include_level == cfg->max_include_level + 1) {
1335 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
1336 return NULL;
1339 cfg->include_level++;
1341 if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
1342 struct ast_config_engine *eng;
1344 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
1347 if (eng && eng->load_func) {
1348 loader = eng;
1349 } else {
1350 eng = find_engine("global", db, sizeof(db), table, sizeof(table));
1351 if (eng && eng->load_func)
1352 loader = eng;
1356 result = loader->load_func(db, table, filename, cfg, withcomments);
1358 if (result)
1359 result->include_level--;
1360 else
1361 cfg->include_level--;
1363 return result;
1366 struct ast_config *ast_config_load(const char *filename)
1368 struct ast_config *cfg;
1369 struct ast_config *result;
1371 cfg = ast_config_new();
1372 if (!cfg)
1373 return NULL;
1375 result = ast_config_internal_load(filename, cfg, 0);
1376 if (!result)
1377 ast_config_destroy(cfg);
1379 return result;
1382 struct ast_config *ast_config_load_with_comments(const char *filename)
1384 struct ast_config *cfg;
1385 struct ast_config *result;
1387 cfg = ast_config_new();
1388 if (!cfg)
1389 return NULL;
1391 result = ast_config_internal_load(filename, cfg, 1);
1392 if (!result)
1393 ast_config_destroy(cfg);
1395 return result;
1398 struct ast_variable *ast_load_realtime(const char *family, ...)
1400 struct ast_config_engine *eng;
1401 char db[256]="";
1402 char table[256]="";
1403 struct ast_variable *res=NULL;
1404 va_list ap;
1406 va_start(ap, family);
1407 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1408 if (eng && eng->realtime_func)
1409 res = eng->realtime_func(db, table, ap);
1410 va_end(ap);
1412 return res;
1415 /*! \brief Check if realtime engine is configured for family */
1416 int ast_check_realtime(const char *family)
1418 struct ast_config_engine *eng;
1420 eng = find_engine(family, NULL, 0, NULL, 0);
1421 if (eng)
1422 return 1;
1423 return 0;
1427 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
1429 struct ast_config_engine *eng;
1430 char db[256]="";
1431 char table[256]="";
1432 struct ast_config *res=NULL;
1433 va_list ap;
1435 va_start(ap, family);
1436 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1437 if (eng && eng->realtime_multi_func)
1438 res = eng->realtime_multi_func(db, table, ap);
1439 va_end(ap);
1441 return res;
1444 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1446 struct ast_config_engine *eng;
1447 int res = -1;
1448 char db[256]="";
1449 char table[256]="";
1450 va_list ap;
1452 va_start(ap, lookup);
1453 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1454 if (eng && eng->update_func)
1455 res = eng->update_func(db, table, keyfield, lookup, ap);
1456 va_end(ap);
1458 return res;
1461 static int config_command(int fd, int argc, char **argv)
1463 struct ast_config_engine *eng;
1464 struct ast_config_map *map;
1466 ast_mutex_lock(&config_lock);
1468 ast_cli(fd, "\n\n");
1469 for (eng = config_engine_list; eng; eng = eng->next) {
1470 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1471 for (map = config_maps; map; map = map->next)
1472 if (!strcasecmp(map->driver, eng->name)) {
1473 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1474 map->table ? map->table : map->name);
1477 ast_cli(fd,"\n\n");
1479 ast_mutex_unlock(&config_lock);
1481 return 0;
1484 static char show_config_help[] =
1485 "Usage: core show config mappings\n"
1486 " Shows the filenames to config engines.\n";
1488 static struct ast_cli_entry cli_show_config_mappings_deprecated = {
1489 { "show", "config", "mappings", NULL },
1490 config_command, NULL,
1491 NULL };
1493 static struct ast_cli_entry cli_config[] = {
1494 { { "core", "show", "config", "mappings", NULL },
1495 config_command, "Display config mappings (file names to config engines)",
1496 show_config_help, NULL, &cli_show_config_mappings_deprecated },
1499 int register_config_cli()
1501 ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));
1502 return 0;