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