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