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