06fd123e53418379e48ac66eac3e2d44acb05623
[asterisk/asterisk.git] / config.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Configuration File Parser
5  * 
6  * Copyright (C) 1999-2004, 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 #include <asterisk/config.h>
21 #include <asterisk/config_pvt.h>
22 #include <asterisk/cli.h>
23 #include <asterisk/lock.h>
24 #include <asterisk/options.h>
25 #include <asterisk/logger.h>
26 #include <asterisk/utils.h>
27 #include "asterisk.h"
28 #include "astconf.h"
29
30
31 static int ast_cust_config=0;
32 struct ast_config *(*global_load_func)(const char *dbname, const char *table, const char *, struct ast_config *,struct ast_category **,struct ast_variable **,int);
33
34 static struct ast_config_map {
35         struct ast_config_map *next;
36         char *name;
37         char *driver;
38         char *database;
39         char *table;
40         char stuff[0];
41 } *maps = NULL;
42
43 AST_MUTEX_DEFINE_STATIC(ast_cust_config_lock);
44 static struct ast_config_reg *ast_cust_config_list;
45 static char *config_conf_file = "extconfig.conf";
46
47 void ast_destroy_realtime(struct ast_variable *v)
48 {
49         struct ast_variable *vn;
50         while(v) {
51                 vn = v;
52                 v = v->next;
53                 free(vn);
54         }
55 }
56
57 void ast_category_destroy(struct ast_category *cat)
58 {
59         ast_destroy_realtime(cat->root);
60         free(cat);
61 }
62
63 void ast_destroy(struct ast_config *ast)
64 {
65         struct ast_category *cat, *catn;
66
67         if (!ast)
68                 return;
69
70         cat = ast->root;
71         while(cat) {
72                 ast_destroy_realtime(cat->root);
73                 catn = cat;
74                 cat = cat->next;
75                 free(catn);
76         }
77         free(ast);
78 }
79
80 int ast_true(const char *s)
81 {
82         if (!s)
83                 return 0;
84         /* Determine if this is a true value */
85         if (!strcasecmp(s, "yes") ||
86             !strcasecmp(s, "true") ||
87                 !strcasecmp(s, "y") ||
88                 !strcasecmp(s, "t") ||
89                 !strcasecmp(s, "1") ||
90                 !strcasecmp(s, "on"))
91                         return -1;
92         return 0;
93 }
94
95 int ast_false(const char *s)
96 {
97         if (!s)
98                 return 0;
99         /* Determine if this is a false value */
100         if (!strcasecmp(s, "no") ||
101             !strcasecmp(s, "false") ||
102                 !strcasecmp(s, "n") ||
103                 !strcasecmp(s, "f") ||
104                 !strcasecmp(s, "0") ||
105                 !strcasecmp(s, "off"))
106                         return -1;
107         return 0;
108 }
109
110 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
111 {
112         struct ast_category *cat;
113         cat = config->root;
114         while(cat) {
115                 if (cat->name == category)
116                         return cat->root;
117                 cat = cat->next;
118         }
119         cat = config->root;
120         while(cat) {
121                 if (!strcasecmp(cat->name, category))
122                         return cat->root;
123                 cat = cat->next;
124         }
125         return NULL;
126 }
127
128 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
129 {
130         struct ast_variable *v;
131         if (category) {
132                 v = ast_variable_browse(config, category);
133                 while (v) {
134                         if (value == v->name)
135                                 return v->value;
136                         v=v->next;
137                 }
138                 v = ast_variable_browse(config, category);
139                 while (v) {
140                         if (!strcasecmp(value, v->name))
141                                 return v->value;
142                         v=v->next;
143                 }
144         } else {
145                 struct ast_category *cat;
146                 cat = config->root;
147                 while(cat) {
148                         v = cat->root;
149                         while (v) {
150                                 if (!strcasecmp(value, v->name))
151                                         return v->value;
152                                 v=v->next;
153                         }
154                         cat = cat->next;
155                 }
156         }
157         return NULL;
158 }
159
160 int ast_category_exist(struct ast_config *config, char *category_name)
161 {
162         struct ast_category *category = NULL;
163
164         category = config->root;
165
166         while(category) {
167                 if (!strcasecmp(category->name,category_name)) 
168                         return 1;
169                 category = category->next;
170         } 
171
172         return 0;
173 }
174
175
176 static struct ast_config_reg *get_ast_cust_config_keyword(const char *name, char *database, int dbsiz, char *table, int tabsiz) 
177 {
178         struct ast_config_reg *reg,*ret=NULL;
179         struct ast_config_map *map;
180
181         ast_mutex_lock(&ast_cust_config_lock);
182         map = maps;
183         while(map) {
184                 if (!strcasecmp(name, map->name)) {
185                         strncpy(database, map->database, dbsiz - 1);
186                         if (map->table)
187                                 strncpy(table, map->table, tabsiz - 1);
188                         else
189                                 strncpy(table, name, tabsiz - 1);
190                         break;
191                 }
192                 map = map->next;
193         }
194         if (map) {
195                 for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) {
196                         if (!strcmp(reg->name,map->driver))
197                                 ret=reg;
198                 }
199         }
200         ast_mutex_unlock(&ast_cust_config_lock);
201         return ret;
202 }
203
204 void ast_config_destroy_all(void) 
205 {
206         struct ast_config_reg *key;
207         ast_mutex_lock(&ast_cust_config_lock);
208         for (key=ast_cust_config_list;key;key=key->next) {
209                 ast_config_deregister(key);
210         }
211         ast_cust_config_list = NULL;
212         ast_mutex_unlock(&ast_cust_config_lock);
213 }
214
215 static struct ast_config_reg *get_config_registrations(void) 
216 {
217         return ast_cust_config_list;
218 }
219
220
221 static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel);
222
223 static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, const char *configfile, int includelevel )
224 {
225         char *c;
226         char *cur;
227         char *arg=NULL;
228         struct ast_variable *v;
229         int object;
230         /* Strip off lines using ; as comment */
231         c = strchr(buf, ';');
232         while (c) {
233                 if ((c == buf) || (*(c-1) != '\\')) {
234                         *c = '\0';
235                 } else {
236                         *(c-1) = ';';
237                         memmove(c, c + 1, strlen(c + 1));
238                 }
239                 c = strchr(c + 1, ';');
240         }
241         cur = ast_strip(buf);
242         if (!ast_strlen_zero(cur)) {
243                 /* Actually parse the entry */
244                 if (cur[0] == '[') {
245                         /* A category header */
246                         c = strchr(cur, ']');
247                         if (c) {
248                                 *c = 0;
249                                 *_tmpc = malloc(sizeof(struct ast_category));
250                                 if (!*_tmpc) {
251                                         ast_destroy(tmp);
252                                         ast_log(LOG_WARNING,
253                                                 "Out of memory, line %d\n", lineno);
254                                         return -1;
255                                 }
256                                 memset(*_tmpc, 0, sizeof(struct ast_category));
257                                 strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
258                                 (*_tmpc)->root =  NULL;
259                                 if (!tmp->prev)
260                                         tmp->root = *_tmpc;
261                                 else
262                                         tmp->prev->next = *_tmpc;
263
264                                 tmp->prev = *_tmpc;
265                                 *_last =  NULL;
266                         } else {
267                                 ast_log(LOG_WARNING, 
268                                         "parse error: no closing ']', line %d of %s\n", lineno, configfile);
269                         }
270                 } else if (cur[0] == '#') {
271                         /* A directive */
272                         cur++;
273                         c = cur;
274                         while(*c && (*c > 32)) c++;
275                         if (*c) {
276                                 *c = '\0';
277                                 c++;
278                                 /* Find real argument */
279                                 while(*c  && (*c < 33)) c++;
280                                 if (!*c)
281                                         c = NULL;
282                         } else 
283                                 c = NULL;
284                         if (!strcasecmp(cur, "include")) {
285                                 /* A #include */
286                                 if (c) {
287                                         while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
288                                         /* Get rid of leading mess */
289                                         cur = c;
290                                         while (!ast_strlen_zero(cur)) {
291                                                 c = cur + strlen(cur) - 1;
292                                                 if ((*c == '>') || (*c == '<') || (*c == '\"'))
293                                                         *c = '\0';
294                                                 else
295                                                         break;
296                                         }
297                                         
298                                         if((c = strchr(cur,':'))) {
299                                                 *c = '\0';
300                                                 c++;
301                                                 arg = c;
302                                         }
303                                         
304                                         if (includelevel < MAX_INCLUDE_LEVEL) {
305                                                 if(arg && cur) {
306                                                         ast_log(LOG_WARNING, "Including files with explicit config engine no longer permitted.  Please use extconfig.conf to specify all mappings\n");
307                                                 } else {
308                                                         __ast_load(cur, tmp, _tmpc, _last, includelevel + 1);
309                                                 }
310                                         } else
311                                                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
312                                 } else
313                                         ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
314                                 /* Strip off leading and trailing "'s and <>'s */
315                         }
316                         else 
317                                 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
318                 } else {
319                         /* Just a line (variable = value) */
320                         if (!*_tmpc) {
321                                 ast_log(LOG_WARNING,
322                                         "parse error: No category context for line %d of %s\n", lineno, configfile);
323                                 ast_destroy(tmp);
324                                 return -1;
325                         }
326                         c = strchr(cur, '=');
327                         if (c) {
328                                 *c = 0;
329                                 c++;
330                                 /* Ignore > in => */
331                                 if (*c== '>') {
332                                         object = 1;
333                                         c++;
334                                 } else
335                                         object = 0;
336                                 v = ast_new_variable(ast_strip(cur), ast_strip(c));
337                                 if (v) {
338                                         v->next = NULL;
339                                         v->lineno = lineno;
340                                         v->object = object;
341                                         /* Put and reset comments */
342                                         v->blanklines = 0;
343                                         if (*_last)
344                                                 (*_last)->next = v;
345                                         else
346                                                 (*_tmpc)->root = v;
347                                         *_last = v;
348                                 } else {
349                                         ast_destroy(tmp);
350                                         ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
351                                         return -1;
352                                 }
353                         } else {
354                                 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
355                         }
356                                                                                                                 
357                 }
358         }
359         return 0;
360 }
361
362 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
363 {
364         FILE *f;
365         char fn[256];
366         char date[256]="";
367         time_t t;
368         struct ast_variable *var;
369         struct ast_category *cat;
370         int blanklines = 0;
371         if (configfile[0] == '/') {
372                 strncpy(fn, configfile, sizeof(fn)-1);
373         } else {
374                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
375         }
376         time(&t);
377         strncpy(date, ctime(&t), sizeof(date) - 1);
378         if ((f = fopen(fn, "w"))) {
379                 if ((option_verbose > 1) && !option_debug)
380                         ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
381                 fprintf(f, ";!\n");
382                 fprintf(f, ";! Automatically generated configuration file\n");
383                 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
384                 fprintf(f, ";! Generator: %s\n", generator);
385                 fprintf(f, ";! Creation Date: %s", date);
386                 fprintf(f, ";!\n");
387                 cat = cfg->root;
388                 while(cat) {
389                         /* Dump section with any appropriate comment */
390                         fprintf(f, "[%s]\n", cat->name);
391                         var = cat->root;
392                         while(var) {
393                                 if (var->sameline) 
394                                         fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
395                                 else    
396                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
397                                 if (var->blanklines) {
398                                         blanklines = var->blanklines;
399                                         while (blanklines) {
400                                                 fprintf(f, "\n");
401                                                 blanklines--;
402                                         }
403                                 }
404                                         
405                                 var = var->next;
406                         }
407 #if 0
408                         /* Put an empty line */
409                         fprintf(f, "\n");
410 #endif
411                         cat = cat->next;
412                 }
413         } else {
414                 if (option_debug)
415                         printf("Unable to open for writing: %s\n", fn);
416                 else if (option_verbose > 1)
417                         printf( "Unable to write (%s)", strerror(errno));
418                 return -1;
419         }
420         fclose(f);
421         return 0;
422 }
423
424
425 struct ast_variable *ast_load_realtime(const char *family, ...)
426 {
427         struct ast_config_reg *reg;
428         char db[256]="";
429         char table[256]="";
430         struct ast_variable *res=NULL;
431         va_list ap;
432         va_start(ap, family);
433         reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
434         if (reg && reg->realtime_func) 
435                 res = reg->realtime_func(db, table, ap);
436         va_end(ap);
437         return res;
438 }
439
440 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
441 {
442         struct ast_config_reg *reg;
443         char db[256]="";
444         char table[256]="";
445         struct ast_config *res=NULL;
446         va_list ap;
447         va_start(ap, family);
448         reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
449         if (reg && reg->realtime_multi_func) 
450                 res = reg->realtime_multi_func(db, table, ap);
451         va_end(ap);
452         return res;
453 }
454
455 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
456 {
457         struct ast_config_reg *reg;
458         int res = -1;
459         char db[256]="";
460         char table[256]="";
461         va_list ap;
462         va_start(ap, lookup);
463         reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
464         if (reg && reg->update_func) 
465                 res = reg->update_func(db, table, keyfield, lookup, ap);
466         va_end(ap);
467         return res;
468 }
469
470 static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel)
471 {
472         char fn[256];
473         char buf[8192];
474         char db[256];
475         char table[256];
476         FILE *f;
477         int lineno=0;
478         int master=0;
479         struct ast_config_reg *reg=NULL;
480         struct ast_config *(*load_func)(const char *database, const char *table, const char *, struct ast_config *,struct ast_category **,struct ast_variable **,int);
481
482         load_func=NULL;
483         if (strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
484                 if (global_load_func) {
485                         load_func = global_load_func;
486                 } else {
487                         reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
488                         if (reg && reg->static_func) {
489                                 load_func = reg->static_func;
490                         } else {
491                                 reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
492                                 if (reg && reg->static_func)
493                                         global_load_func = load_func = reg->static_func;
494                         }
495                 }
496
497                 if (load_func) {
498                         ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
499                         tmp = load_func(db, table, configfile,tmp, _tmpc, _last, includelevel);
500             
501                         if (tmp)
502                                 return tmp;
503                 }
504         }
505
506         if (configfile[0] == '/') {
507                 strncpy(fn, configfile, sizeof(fn)-1);
508         } else {
509                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
510         }
511         if ((option_verbose > 1) && !option_debug) {
512                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
513                 fflush(stdout);
514         }
515         if ((f = fopen(fn, "r"))) {
516                 if (option_debug)
517                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
518                 else if (option_verbose > 1)
519                         ast_verbose( "Found\n");
520                 if (!tmp) {
521                         tmp = malloc(sizeof(struct ast_config));
522                         if (tmp)
523                                 memset(tmp, 0, sizeof(struct ast_config));
524
525                         master = 1;
526                 }
527                 if (!tmp) {
528                         ast_log(LOG_WARNING, "Out of memory\n");
529                         fclose(f);
530                         return NULL;
531                 }
532                 while(!feof(f)) {
533                         lineno++;
534                         if (fgets(buf, sizeof(buf), f)) {
535                                 if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel)) {
536                                         fclose(f);
537                                         return NULL;
538                                 }
539                         }
540                 }
541                 fclose(f);              
542         } else {
543                 if (option_debug)
544                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
545                 else if (option_verbose > 1)
546                         ast_verbose( "Not found (%s)\n", strerror(errno));
547         }
548         return tmp;
549 }
550
551 int ast_config_register(struct ast_config_reg *new) 
552 {
553         struct ast_config_reg *ptr;
554         ast_mutex_lock(&ast_cust_config_lock);
555         if (!ast_cust_config_list) {
556                 ast_cust_config_list = new;
557         } else {
558                 for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
559                         ptr->next = new;
560         }
561         ast_mutex_unlock(&ast_cust_config_lock);
562         ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
563         return 1;
564 }
565
566 int ast_config_deregister(struct ast_config_reg *del) 
567 {
568         struct ast_config_reg *ptr=NULL,*last=NULL;
569         ast_mutex_lock(&ast_cust_config_lock);
570         for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
571                 if (ptr == del) {
572                         if (last && ptr->next) {
573                                 last->next = ptr->next;
574                         } else if (last && ! ptr->next) {
575                                 last->next = NULL;
576                         } else if (!last && ptr->next) {
577                                 ast_cust_config_list = ptr->next;
578                         } else if (!last && !ptr->next) {
579                                 ast_cust_config_list = NULL;
580                         }
581                 }
582                 last = ptr;
583         }
584         ast_mutex_unlock(&ast_cust_config_lock);
585         return 0;
586 }
587
588 int ast_cust_config_active(void) {
589         return (ast_cust_config >0) ? 1 : 0;
590 }
591
592 struct ast_config *ast_load(char *configfile)
593 {
594         struct ast_category *tmpc=NULL;
595         struct ast_variable *last = NULL;
596
597         return __ast_load(configfile, NULL, &tmpc, &last, 0);
598 }
599
600 void ast_category_append(struct ast_config *config, struct ast_category *cat)
601 {
602         struct ast_category *prev = NULL;
603         cat->next = NULL;
604         if (config->root) {
605                 prev = config->root;
606                 while(prev->next) prev = prev->next;
607                 prev->next = cat;
608         } else
609                 config->root = cat;
610 }
611
612 char *ast_category_browse(struct ast_config *config, char *prev)
613 {       
614         struct ast_category *cat;
615         if (!prev) {
616                 if (config->root)
617                         return config->root->name;
618                 else
619                         return NULL;
620         }
621         cat = config->root;
622         while(cat) {
623                 if (cat->name == prev) {
624                         if (cat->next)
625                                 return cat->next->name;
626                         else
627                                 return NULL;
628                 }
629                 cat = cat->next;
630         }
631         cat = config->root;
632         while(cat) {
633                 if (!strcasecmp(cat->name, prev)) {
634                         if (cat->next)
635                                 return cat->next->name;
636                         else
637                                 return NULL;
638                 }
639                 cat = cat->next;
640         }
641         return NULL;
642 }
643
644
645 struct ast_config *ast_new_config(void) 
646 {
647         struct ast_config *config;
648         config = malloc(sizeof(struct ast_config));
649         memset(config,0,sizeof(struct ast_config));
650         return config;
651 }
652
653
654
655 struct ast_category *ast_new_category(char *name) 
656 {
657         struct ast_category *category;
658         category = malloc(sizeof(struct ast_category));
659         if (category) {
660                 memset(category,0,sizeof(struct ast_category));
661                 strncpy(category->name,name,sizeof(category->name) - 1);
662         }
663         return category;
664 }
665
666
667 struct ast_variable *ast_new_variable(char *name, char *value) 
668 {
669         struct ast_variable *variable;
670         int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
671         variable = malloc(length);
672         if (variable) {
673                 memset(variable, 0, length);
674                 variable->name = variable->stuff;
675                 variable->value = variable->stuff + strlen(name) + 1;           
676                 variable->object=0;
677                 strcpy(variable->name,name);
678                 strcpy(variable->value,value);
679         }
680         return variable;
681 }
682
683 int ast_cust_config_register(struct ast_config_reg *new) 
684 {
685         ast_config_register(new);
686         read_ast_cust_config();
687         return 1;
688 }
689 int ast_cust_config_deregister(struct ast_config_reg *new) 
690 {
691         ast_config_deregister(new);
692         read_ast_cust_config();
693         return 1;
694 }
695
696 static void clear_cust_keywords(void) 
697 {
698         struct ast_config_map *map, *prev;
699         ast_mutex_lock(&ast_cust_config_lock);
700         map = maps;
701         while(map) {
702                 prev = map;
703                 map = map->next;
704                 free(prev);
705         }
706         maps = NULL;
707         ast_mutex_unlock(&ast_cust_config_lock);
708 }
709
710 static int config_command(int fd, int argc, char **argv) 
711 {
712         struct ast_config_reg *key;
713         struct ast_config_map *map;
714         
715         ast_cli(fd,"\n\n");
716         ast_mutex_lock(&ast_cust_config_lock);
717         for (key=get_config_registrations();key;key=key->next) {
718                 ast_cli(fd,"\nConfig Engine: %s\n",key->name);
719                 map = maps;
720                 while(map) {
721                         if (!strcasecmp(map->driver, key->name))
722                                 ast_cli(fd,"===> %s (db=%s, table=%s)\n",map->name, map->database, map->table ? map->table : map->name);
723                         map = map->next;
724                 }
725         }
726         ast_mutex_unlock(&ast_cust_config_lock);
727         ast_cli(fd,"\n\n");
728         
729         return 0;
730 }
731
732 static struct ast_cli_entry config_command_struct = {
733   { "show","config","handles", NULL }, config_command,
734   "Show Config Handles", NULL };
735
736 int register_config_cli() 
737 {
738         return ast_cli_register(&config_command_struct);
739 }
740
741 int read_ast_cust_config(void) 
742 {
743         char *cfg = config_conf_file;
744         struct ast_config *config;
745         struct ast_variable *v;
746         struct ast_config_map *map;
747         int length;
748         char *driver, *table, *database, *stringp;
749
750         clear_cust_keywords();
751         config = ast_load(cfg);
752         if (config) {
753                 for (v = ast_variable_browse(config,"settings");v;v=v->next) {
754                         stringp = v->value;
755                         driver = strsep(&stringp, ",");
756                         database = strsep(&stringp, ",");
757                         table = strsep(&stringp, ",");
758                         
759                         if (!strcmp(v->name,config_conf_file) || !strcmp(v->name,"asterisk.conf")) {
760                                 ast_log(LOG_WARNING, "Cannot bind asterisk.conf or extconfig.conf!\n");
761                         } else if (driver && database) {
762                                 length = sizeof(struct ast_config_map);
763                                 length += strlen(v->name) + 1;
764                                 length += strlen(driver) + 1;
765                                 length += strlen(database) + 1;
766                                 if (table)
767                                         length += strlen(table) + 1;
768                                 map = malloc(length);
769                                 if (map) {
770                                         memset(map, 0, length);
771                                         map->name = map->stuff;
772                                         strcpy(map->name, v->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                                         } else
781                                                 map->table = NULL;
782                                         map->next = maps;
783                                         if (option_verbose > 1)
784                                                 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",map->name,map->driver, map->database, map->table ? map->table : map->name);
785                                         maps = map;
786                                 }
787                         }
788                 }
789                 
790                 ast_destroy(config);
791         }
792
793         return 0;
794 }