cef3d61cf121ad46f13a94bc2e8f6067a8ec9c79
[asterisk/asterisk.git] / config.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Configuration File Parser
5  * 
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <time.h>
20 #define AST_INCLUDE_GLOB 1
21 #ifdef AST_INCLUDE_GLOB
22 #ifdef __Darwin__
23 #define GLOB_ABORTED GLOB_ABEND
24 #endif
25 # include <glob.h>
26 #endif
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/config.h"
33 #include "asterisk/cli.h"
34 #include "asterisk/lock.h"
35 #include "asterisk/options.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/app.h"
40
41 #define MAX_NESTED_COMMENTS 128
42 #define COMMENT_START ";--"
43 #define COMMENT_END "--;"
44 #define COMMENT_META ';'
45 #define COMMENT_TAG '-'
46
47 static char *extconfig_conf = "extconfig.conf";
48
49 static struct ast_config_map {
50         struct ast_config_map *next;
51         char *name;
52         char *driver;
53         char *database;
54         char *table;
55         char stuff[0];
56 } *config_maps = NULL;
57
58 AST_MUTEX_DEFINE_STATIC(config_lock);
59 static struct ast_config_engine *config_engine_list;
60
61 #define MAX_INCLUDE_LEVEL 10
62
63 struct ast_comment {
64         struct ast_comment *next;
65         char cmt[0];
66 };
67
68 struct ast_category {
69         char name[80];
70         int ignored;                    /* do not let user of the config see this category */
71         struct ast_variable *root;
72         struct ast_variable *last;
73         struct ast_category *next;
74 };
75
76 struct ast_config {
77         struct ast_category *root;
78         struct ast_category *last;
79         struct ast_category *current;
80         struct ast_category *last_browse;               /* used to cache the last category supplied via category_browse */
81         int include_level;
82         int max_include_level;
83 };
84
85 struct ast_variable *ast_variable_new(const char *name, const char *value) 
86 {
87         struct ast_variable *variable;
88
89         int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
90         variable = malloc(length);
91         if (variable) {
92                 memset(variable, 0, length);
93                 variable->name = variable->stuff;
94                 variable->value = variable->stuff + strlen(name) + 1;           
95                 strcpy(variable->name,name);
96                 strcpy(variable->value,value);
97         }
98
99         return variable;
100 }
101
102 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
103 {
104         if (category->last)
105                 category->last->next = variable;
106         else
107                 category->root = variable;
108         category->last = variable;
109 }
110
111 void ast_variables_destroy(struct ast_variable *v)
112 {
113         struct ast_variable *vn;
114
115         while(v) {
116                 vn = v;
117                 v = v->next;
118                 free(vn);
119         }
120 }
121
122 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
123 {
124         struct ast_category *cat = NULL;
125
126         if (category && config->last_browse && (config->last_browse->name == category))
127                 cat = config->last_browse;
128         else
129                 cat = ast_category_get(config, category);
130
131         if (cat)
132                 return cat->root;
133         else
134                 return NULL;
135 }
136
137 char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
138 {
139         struct ast_variable *v;
140
141         if (category) {
142                 for (v = ast_variable_browse(config, category); v; v = v->next)
143                         if (variable == v->name)
144                                 return v->value;
145                 for (v = ast_variable_browse(config, category); v; v = v->next)
146                         if (!strcasecmp(variable, v->name))
147                                 return v->value;
148         } else {
149                 struct ast_category *cat;
150
151                 for (cat = config->root; cat; cat = cat->next)
152                         for (v = cat->root; v; v = v->next)
153                                 if (!strcasecmp(variable, v->name))
154                                         return v->value;
155         }
156
157         return NULL;
158 }
159
160 static struct ast_variable *variable_clone(const struct ast_variable *old)
161 {
162         struct ast_variable *new = ast_variable_new(old->name, old->value);
163
164         if (new) {
165                 new->lineno = old->lineno;
166                 new->object = old->object;
167                 new->blanklines = old->blanklines;
168                 /* TODO: clone comments? */
169         }
170
171         return new;
172 }
173  
174 static void move_variables(struct ast_category *old, struct ast_category *new)
175 {
176         struct ast_variable *var;
177         struct ast_variable *next;
178
179         next = old->root;
180         old->root = NULL;
181         for (var = next; var; var = next) {
182                 next = var->next;
183                 var->next = NULL;
184                 ast_variable_append(new, var);
185         }
186 }
187
188 struct ast_category *ast_category_new(const char *name) 
189 {
190         struct ast_category *category;
191
192         category = malloc(sizeof(struct ast_category));
193         if (category) {
194                 memset(category, 0, sizeof(struct ast_category));
195                 strncpy(category->name, name, sizeof(category->name) - 1);
196         }
197
198         return category;
199 }
200
201 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
202 {
203         struct ast_category *cat;
204
205         for (cat = config->root; cat; cat = cat->next) {
206                 if (cat->name == category_name && (ignored || !cat->ignored))
207                         return cat;
208         }
209
210         for (cat = config->root; cat; cat = cat->next) {
211                 if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
212                         return cat;
213         }
214
215         return NULL;
216 }
217
218 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
219 {
220         return category_get(config, category_name, 0);
221 }
222
223 int ast_category_exist(const struct ast_config *config, const char *category_name)
224 {
225         return !!ast_category_get(config, category_name);
226 }
227
228 void ast_category_append(struct ast_config *config, struct ast_category *category)
229 {
230         if (config->last)
231                 config->last->next = category;
232         else
233                 config->root = category;
234         config->last = category;
235         config->current = category;
236 }
237
238 void ast_category_destroy(struct ast_category *cat)
239 {
240         ast_variables_destroy(cat->root);
241         free(cat);
242 }
243
244 static struct ast_category *next_available_category(struct ast_category *cat)
245 {
246         for (; cat && cat->ignored; cat = cat->next);
247
248         return cat;
249 }
250
251 char *ast_category_browse(struct ast_config *config, const char *prev)
252 {       
253         struct ast_category *cat = NULL;
254
255         if (prev && config->last_browse && (config->last_browse->name == prev))
256                 cat = config->last_browse->next;
257         else if (!prev && config->root)
258                         cat = config->root;
259         else if (prev) {
260                 for (cat = config->root; cat; cat = cat->next) {
261                         if (cat->name == prev) {
262                                 cat = cat->next;
263                                 break;
264                         }
265                 }
266                 if (!cat) {
267                         for (cat = config->root; cat; cat = cat->next) {
268                                 if (!strcasecmp(cat->name, prev)) {
269                                         cat = cat->next;
270                                         break;
271                                 }
272                         }
273                 }
274         }
275         
276         if (cat)
277                 cat = next_available_category(cat);
278
279         config->last_browse = cat;
280         if (cat)
281                 return cat->name;
282         else
283                 return NULL;
284 }
285
286 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
287 {
288         struct ast_variable *v;
289
290         v = cat->root;
291         cat->root = NULL;
292
293         return v;
294 }
295
296 void ast_category_rename(struct ast_category *cat, const char *name)
297 {
298         strncpy(cat->name, name, sizeof(cat->name) - 1);
299 }
300
301 static void inherit_category(struct ast_category *new, const struct ast_category *base)
302 {
303         struct ast_variable *var;
304
305         for (var = base->root; var; var = var->next) {
306                 struct ast_variable *v;
307                 
308                 v = variable_clone(var);
309                 if (v)
310                         ast_variable_append(new, v);
311         }
312 }
313
314 struct ast_config *ast_config_new(void) 
315 {
316         struct ast_config *config;
317
318         config = malloc(sizeof(*config));
319         if (config) {
320                 memset(config, 0, sizeof(*config));
321                 config->max_include_level = MAX_INCLUDE_LEVEL;
322         }
323
324         return config;
325 }
326
327 void ast_config_destroy(struct ast_config *cfg)
328 {
329         struct ast_category *cat, *catn;
330
331         if (!cfg)
332                 return;
333
334         cat = cfg->root;
335         while(cat) {
336                 ast_variables_destroy(cat->root);
337                 catn = cat;
338                 cat = cat->next;
339                 free(catn);
340         }
341         free(cfg);
342 }
343
344 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
345 {
346         return cfg->current;
347 }
348
349 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
350 {
351         /* cast below is just to silence compiler warning about dropping "const" */
352         cfg->current = (struct ast_category *) cat;
353 }
354
355 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile)
356 {
357         char *c;
358         char *cur = buf;
359         struct ast_variable *v;
360         char cmd[512], exec_file[512];
361         int object, do_exec, do_include;
362
363         /* Actually parse the entry */
364         if (cur[0] == '[') {
365                 struct ast_category *newcat = NULL;
366                 char *catname;
367
368                 /* A category header */
369                 c = strchr(cur, ']');
370                 if (!c) {
371                         ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
372                         return -1;
373                 }
374                 *c++ = '\0';
375                 cur++;
376                 if (*c++ != '(')
377                         c = NULL;
378                 catname = cur;
379                 *cat = newcat = ast_category_new(catname);
380                 if (!newcat) {
381                         ast_log(LOG_WARNING, "Out of memory, line %d of %s\n", lineno, configfile);
382                         return -1;
383                 }
384                 /* If there are options or categories to inherit from, process them now */
385                 if (c) {
386                         if (!(cur = strchr(c, ')'))) {
387                                 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
388                                 return -1;
389                         }
390                         *cur = '\0';
391                         while ((cur = strsep(&c, ","))) {
392                                 if (!strcasecmp(cur, "!")) {
393                                         (*cat)->ignored = 1;
394                                 } else if (!strcasecmp(cur, "+")) {
395                                         *cat = category_get(cfg, catname, 1);
396                                         if (!*cat) {
397                                                 ast_destroy(cfg);
398                                                 if (newcat)
399                                                         ast_category_destroy(newcat);
400                                                 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
401                                                 return -1;
402                                         }
403                                         if (newcat) {
404                                                 move_variables(newcat, *cat);
405                                                 ast_category_destroy(newcat);
406                                                 newcat = NULL;
407                                         }
408                                 } else {
409                                         struct ast_category *base;
410                                 
411                                         base = category_get(cfg, cur, 1);
412                                         if (!base) {
413                                                 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
414                                                 return -1;
415                                         }
416                                         inherit_category(*cat, base);
417                                 }
418                         }
419                 }
420                 if (newcat)
421                         ast_category_append(cfg, *cat);
422         } else if (cur[0] == '#') {
423                 /* A directive */
424                 cur++;
425                 c = cur;
426                 while(*c && (*c > 32)) c++;
427                 if (*c) {
428                         *c = '\0';
429                         c++;
430                         /* Find real argument */
431                         while(*c  && (*c < 33)) c++;
432                         if (!*c)
433                                 c = NULL;
434                 } else 
435                         c = NULL;
436                 do_include = !strcasecmp(cur, "include");
437                 if(!do_include)
438                         do_exec = !strcasecmp(cur, "exec");
439                 else
440                         do_exec = 0;
441                 if (do_exec && !option_exec_includes) {
442                         ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
443                         do_exec = 0;
444                 }
445                 if (do_include || do_exec) {
446                         if (c) {
447                                 /* Strip off leading and trailing "'s and <>'s */
448                                 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
449                                 /* Get rid of leading mess */
450                                 cur = c;
451                                 while (!ast_strlen_zero(cur)) {
452                                         c = cur + strlen(cur) - 1;
453                                         if ((*c == '>') || (*c == '<') || (*c == '\"'))
454                                                 *c = '\0';
455                                         else
456                                                 break;
457                                 }
458                                 /* #exec </path/to/executable>
459                                    We create a tmp file, then we #include it, then we delete it. */
460                                 if (do_exec) { 
461                                         snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%ld.%ld", time(NULL), (long)pthread_self());
462                                         snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
463                                         ast_safe_system(cmd);
464                                         cur = exec_file;
465                                 } else
466                                         exec_file[0] = '\0';
467                                 /* A #include */
468                                 do_include = ast_config_internal_load(cur, cfg) ? 1 : 0;
469                                 if(!ast_strlen_zero(exec_file))
470                                         unlink(exec_file);
471                                 if(!do_include)
472                                         return 0;
473
474                         } else {
475                                 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n", 
476                                                 do_exec ? "exec" : "include",
477                                                 do_exec ? "/path/to/executable" : "filename",
478                                                 lineno,
479                                                 configfile);
480                         }
481                 }
482                 else 
483                         ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
484         } else {
485                 /* Just a line (variable = value) */
486                 if (!*cat) {
487                         ast_log(LOG_WARNING,
488                                 "parse error: No category context for line %d of %s\n", lineno, configfile);
489                         return -1;
490                 }
491                 c = strchr(cur, '=');
492                 if (c) {
493                         *c = 0;
494                         c++;
495                         /* Ignore > in => */
496                         if (*c== '>') {
497                                 object = 1;
498                                 c++;
499                         } else
500                                 object = 0;
501                         v = ast_variable_new(ast_strip(cur), ast_strip(c));
502                         if (v) {
503                                 v->lineno = lineno;
504                                 v->object = object;
505                                 /* Put and reset comments */
506                                 v->blanklines = 0;
507                                 ast_variable_append(*cat, v);
508                         } else {
509                                 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
510                                 return -1;
511                         }
512                 } else {
513                         ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
514                 }
515
516         }
517         return 0;
518 }
519
520 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg)
521 {
522         char fn[256];
523         char buf[8192];
524         char *new_buf, *comment_p, *process_buf;
525         FILE *f;
526         int lineno=0;
527         int comment = 0, nest[MAX_NESTED_COMMENTS];
528         struct ast_category *cat = NULL;
529         int count = 0;
530         
531         cat = ast_config_get_current_category(cfg);
532
533         if (filename[0] == '/') {
534                 strncpy(fn, filename, sizeof(fn)-1);
535         } else {
536                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
537         }
538
539 #ifdef AST_INCLUDE_GLOB
540         {
541                 int glob_ret;
542                 glob_t globbuf;
543                 globbuf.gl_offs = 0;    /* initialize it to silence gcc */
544 #ifdef SOLARIS
545                 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
546 #else
547                 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
548 #endif
549                 if (glob_ret == GLOB_NOSPACE)
550                         ast_log(LOG_WARNING,
551                                 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
552                 else if (glob_ret  == GLOB_ABORTED)
553                         ast_log(LOG_WARNING,
554                                 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
555                 else  {
556                         /* loop over expanded files */
557                         int i;
558                         for (i=0; i<globbuf.gl_pathc; i++) {
559                                 strncpy(fn, globbuf.gl_pathv[i], sizeof(fn)-1);
560 #endif
561         if ((option_verbose > 1) && !option_debug) {
562                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
563                 fflush(stdout);
564         }
565         if ((f = fopen(fn, "r"))) {
566                 count++;
567                 if (option_debug)
568                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
569                 else if (option_verbose > 1)
570                         ast_verbose("Found\n");
571                 while(!feof(f)) {
572                         lineno++;
573                         if (fgets(buf, sizeof(buf), f)) {
574                                 new_buf = buf;
575                                 if (comment)
576                                         process_buf = NULL;
577                                 else
578                                         process_buf = buf;
579                                 while ((comment_p = strchr(new_buf, COMMENT_META))) {
580                                         if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
581                                                 /* Yuck, gotta memmove */
582                                                 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
583                                                 new_buf = comment_p;
584                                         } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
585                                                 /* Meta-Comment start detected ";--" */
586                                                 if (comment < MAX_NESTED_COMMENTS) {
587                                                         *comment_p = '\0';
588                                                         new_buf = comment_p + 3;
589                                                         comment++;
590                                                         nest[comment-1] = lineno;
591                                                 } else {
592                                                         ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
593                                                 }
594                                         } else if ((comment_p >= new_buf + 2) &&
595                                                    (*(comment_p - 1) == COMMENT_TAG) &&
596                                                    (*(comment_p - 2) == COMMENT_TAG)) {
597                                                 /* Meta-Comment end detected */
598                                                 comment--;
599                                                 new_buf = comment_p + 1;
600                                                 if (!comment) {
601                                                         /* Back to non-comment now */
602                                                         if (process_buf) {
603                                                                 /* Actually have to move what's left over the top, then continue */
604                                                                 char *oldptr;
605                                                                 oldptr = process_buf + strlen(process_buf);
606                                                                 memmove(oldptr, new_buf, strlen(new_buf) + 1);
607                                                                 new_buf = oldptr;
608                                                         } else
609                                                                 process_buf = new_buf;
610                                                 }
611                                         } else {
612                                                 if (!comment) {
613                                                         /* If ; is found, and we are not nested in a comment, 
614                                                            we immediately stop all comment processing */
615                                                         *comment_p = '\0'; 
616                                                         new_buf = comment_p;
617                                                 } else
618                                                         new_buf = comment_p + 1;
619                                         }
620                                 }
621                                 if (process_buf) {
622                                         char *buf = ast_strip(process_buf);
623                                         if (!ast_strlen_zero(buf))
624                                                 if (process_text_line(cfg, &cat, buf, lineno, filename)) {
625                                                         cfg = NULL;
626                                                         break;
627                                                 }
628                                 }
629                         }
630                 }
631                 fclose(f);              
632         } else { /* can't open file */
633                 if (option_debug)
634                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
635                 else if (option_verbose > 1)
636                         ast_verbose( "Not found (%s)\n", strerror(errno));
637         }
638         if (comment) {
639                 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
640         }
641 #ifdef AST_INCLUDE_GLOB
642                                         if (!cfg)
643                                                 break;
644                                 }
645                                 globfree(&globbuf);
646                         }
647                 }
648 #endif
649         if (count == 0)
650                 return NULL;
651
652         return cfg;
653 }
654
655 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
656 {
657         FILE *f;
658         char fn[256];
659         char date[256]="";
660         time_t t;
661         struct ast_variable *var;
662         struct ast_category *cat;
663         int blanklines = 0;
664
665         if (configfile[0] == '/') {
666                 ast_copy_string(fn, configfile, sizeof(fn));
667         } else {
668                 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
669         }
670         time(&t);
671         strncpy(date, ctime(&t), sizeof(date) - 1);
672         if ((f = fopen(fn, "w"))) {
673                 if ((option_verbose > 1) && !option_debug)
674                         ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
675                 fprintf(f, ";!\n");
676                 fprintf(f, ";! Automatically generated configuration file\n");
677                 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
678                 fprintf(f, ";! Generator: %s\n", generator);
679                 fprintf(f, ";! Creation Date: %s", date);
680                 fprintf(f, ";!\n");
681                 cat = cfg->root;
682                 while(cat) {
683                         /* Dump section with any appropriate comment */
684                         fprintf(f, "[%s]\n", cat->name);
685                         var = cat->root;
686                         while(var) {
687                                 if (var->sameline) 
688                                         fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
689                                 else    
690                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
691                                 if (var->blanklines) {
692                                         blanklines = var->blanklines;
693                                         while (blanklines--)
694                                                 fprintf(f, "\n");
695                                 }
696                                         
697                                 var = var->next;
698                         }
699 #if 0
700                         /* Put an empty line */
701                         fprintf(f, "\n");
702 #endif
703                         cat = cat->next;
704                 }
705         } else {
706                 if (option_debug)
707                         printf("Unable to open for writing: %s\n", fn);
708                 else if (option_verbose > 1)
709                         printf( "Unable to write (%s)", strerror(errno));
710                 return -1;
711         }
712         fclose(f);
713         return 0;
714 }
715
716 static void clear_config_maps(void) 
717 {
718         struct ast_config_map *map;
719
720         ast_mutex_lock(&config_lock);
721
722         while (config_maps) {
723                 map = config_maps;
724                 config_maps = config_maps->next;
725                 free(map);
726         }
727                 
728         ast_mutex_unlock(&config_lock);
729 }
730
731 static int append_mapping(char *name, char *driver, char *database, char *table)
732 {
733         struct ast_config_map *map;
734         int length;
735
736         length = sizeof(*map);
737         length += strlen(name) + 1;
738         length += strlen(driver) + 1;
739         length += strlen(database) + 1;
740         if (table)
741                 length += strlen(table) + 1;
742         map = malloc(length);
743
744         if (!map)
745                 return -1;
746
747         memset(map, 0, length);
748         map->name = map->stuff;
749         strcpy(map->name, name);
750         map->driver = map->name + strlen(map->name) + 1;
751         strcpy(map->driver, driver);
752         map->database = map->driver + strlen(map->driver) + 1;
753         strcpy(map->database, database);
754         if (table) {
755                 map->table = map->database + strlen(map->database) + 1;
756                 strcpy(map->table, table);
757         }
758         map->next = config_maps;
759
760         if (option_verbose > 1)
761                 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
762                             map->name, map->driver, map->database, map->table ? map->table : map->name);
763
764         config_maps = map;
765         return 0;
766 }
767
768 void read_config_maps(void) 
769 {
770         struct ast_config *config, *configtmp;
771         struct ast_variable *v;
772         char *driver, *table, *database, *stringp;
773
774         clear_config_maps();
775
776         configtmp = ast_config_new();
777         configtmp->max_include_level = 1;
778         config = ast_config_internal_load(extconfig_conf, configtmp);
779         if (!config) {
780                 ast_config_destroy(configtmp);
781                 return;
782         }
783
784         for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
785                 stringp = v->value;
786                 driver = strsep(&stringp, ",");
787                 database = strsep(&stringp, ",");
788                 table = strsep(&stringp, ",");
789                         
790                 if (!strcmp(v->name, extconfig_conf)) {
791                         ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
792                         continue;
793                 }
794
795                 if (!strcmp(v->name, "asterisk.conf")) {
796                         ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
797                         continue;
798                 }
799
800                 if (!strcmp(v->name, "logger.conf")) {
801                         ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
802                         continue;
803                 }
804
805                 if (!driver || !database)
806                         continue;
807                 if (!strcasecmp(v->name, "sipfriends")) {
808                         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");
809                         append_mapping("sipusers", driver, database, table ? table : "sipfriends");
810                         append_mapping("sippeers", driver, database, table ? table : "sipfriends");
811                 } else if (!strcasecmp(v->name, "iaxfriends")) {
812                         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");
813                         append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
814                         append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
815                 } else 
816                         append_mapping(v->name, driver, database, table);
817         }
818                 
819         ast_config_destroy(config);
820 }
821
822 int ast_config_engine_register(struct ast_config_engine *new) 
823 {
824         struct ast_config_engine *ptr;
825
826         ast_mutex_lock(&config_lock);
827
828         if (!config_engine_list) {
829                 config_engine_list = new;
830         } else {
831                 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
832                 ptr->next = new;
833         }
834
835         ast_mutex_unlock(&config_lock);
836         ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
837
838         return 1;
839 }
840
841 int ast_config_engine_deregister(struct ast_config_engine *del) 
842 {
843         struct ast_config_engine *ptr, *last=NULL;
844
845         ast_mutex_lock(&config_lock);
846
847         for (ptr = config_engine_list; ptr; ptr=ptr->next) {
848                 if (ptr == del) {
849                         if (last)
850                                 last->next = ptr->next;
851                         else
852                                 config_engine_list = ptr->next;
853                         break;
854                 }
855                 last = ptr;
856         }
857
858         ast_mutex_unlock(&config_lock);
859
860         return 0;
861 }
862
863 static struct ast_config_engine *find_engine(const char *filename, char *database, int dbsiz, char *table, int tabsiz) 
864 {
865         struct ast_config_engine *eng, *ret = NULL;
866         struct ast_config_map *map;
867
868         ast_mutex_lock(&config_lock);
869
870         for (map = config_maps; map; map = map->next) {
871                 if (!strcasecmp(filename, map->name)) {
872                         ast_copy_string(database, map->database, dbsiz);
873                         ast_copy_string(table, map->table ? map->table : filename, tabsiz);
874                         break;
875                 }
876         }
877         if (map) {
878                 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
879                         if (!strcasecmp(eng->name, map->driver))
880                                 ret = eng;
881                 }
882         }
883
884         ast_mutex_unlock(&config_lock);
885         
886         /* if we found a mapping, but the engine is not available, then issue a warning */
887         if (map && !ret)
888                 ast_log(LOG_WARNING, "Mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
889
890         return ret;
891 }
892
893 static struct ast_config_engine text_file_engine = {
894         .name = "text",
895         .load_func = config_text_file_load,
896 };
897
898 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg)
899 {
900         char db[256];
901         char table[256];
902         struct ast_config_engine *loader = &text_file_engine;
903         struct ast_config *result;
904
905         if (cfg->include_level == cfg->max_include_level) {
906                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
907                 return NULL;
908         }
909
910         cfg->include_level++;
911
912         if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
913                 struct ast_config_engine *eng;
914
915                 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
916
917
918                 if (eng && eng->load_func) {
919                         loader = eng;
920                 } else {
921                         eng = find_engine("global", db, sizeof(db), table, sizeof(table));
922                         if (eng && eng->load_func)
923                                 loader = eng;
924                 }
925         }
926
927         result = loader->load_func(db, table, filename, cfg);
928
929         if (result)
930                 result->include_level--;
931
932         return result;
933 }
934
935 struct ast_config *ast_config_load(const char *filename)
936 {
937         struct ast_config *cfg;
938         struct ast_config *result;
939
940         cfg = ast_config_new();
941         if (!cfg)
942                 return NULL;
943
944         result = ast_config_internal_load(filename, cfg);
945         if (!result)
946                 ast_config_destroy(cfg);
947
948         return result;
949 }
950
951 struct ast_variable *ast_load_realtime(const char *family, ...)
952 {
953         struct ast_config_engine *eng;
954         char db[256]="";
955         char table[256]="";
956         struct ast_variable *res=NULL;
957         va_list ap;
958
959         va_start(ap, family);
960         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
961         if (eng && eng->realtime_func) 
962                 res = eng->realtime_func(db, table, ap);
963         va_end(ap);
964
965         return res;
966 }
967
968 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
969 {
970         struct ast_config_engine *eng;
971         char db[256]="";
972         char table[256]="";
973         struct ast_config *res=NULL;
974         va_list ap;
975
976         va_start(ap, family);
977         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
978         if (eng && eng->realtime_multi_func) 
979                 res = eng->realtime_multi_func(db, table, ap);
980         va_end(ap);
981
982         return res;
983 }
984
985 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
986 {
987         struct ast_config_engine *eng;
988         int res = -1;
989         char db[256]="";
990         char table[256]="";
991         va_list ap;
992
993         va_start(ap, lookup);
994         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
995         if (eng && eng->update_func) 
996                 res = eng->update_func(db, table, keyfield, lookup, ap);
997         va_end(ap);
998
999         return res;
1000 }
1001
1002 static int config_command(int fd, int argc, char **argv) 
1003 {
1004         struct ast_config_engine *eng;
1005         struct ast_config_map *map;
1006         
1007         ast_mutex_lock(&config_lock);
1008
1009         ast_cli(fd, "\n\n");
1010         for (eng = config_engine_list; eng; eng = eng->next) {
1011                 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1012                 for (map = config_maps; map; map = map->next)
1013                         if (!strcasecmp(map->driver, eng->name)) {
1014                                 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1015                                         map->table ? map->table : map->name);
1016                         }
1017         }
1018         ast_cli(fd,"\n\n");
1019         
1020         ast_mutex_unlock(&config_lock);
1021
1022         return 0;
1023 }
1024
1025 static char show_config_help[] =
1026         "Usage: show config mappings\n"
1027         "       Shows the filenames to config engines.\n";
1028
1029 static struct ast_cli_entry config_command_struct = {
1030         { "show", "config", "mappings", NULL }, config_command, "Show Config mappings (file names to config engines)", show_config_help, NULL
1031 };
1032
1033 int register_config_cli() 
1034 {
1035         return ast_cli_register(&config_command_struct);
1036 }