formatting fixes (bug #4736)
[asterisk/asterisk.git] / config.c
index ebb2e11..8b9e441 100755 (executable)
--- a/config.c
+++ b/config.c
 #include <time.h>
 #define AST_INCLUDE_GLOB 1
 #ifdef AST_INCLUDE_GLOB
-#ifdef __OSX__
+#ifdef __Darwin__
 #define GLOB_ABORTED GLOB_ABEND
 #endif
 # include <glob.h>
 #endif
-#include <asterisk/config.h>
-#include <asterisk/cli.h>
-#include <asterisk/lock.h>
-#include <asterisk/options.h>
-#include <asterisk/logger.h>
-#include <asterisk/utils.h>
+
 #include "asterisk.h"
-#include "astconf.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/config.h"
+#include "asterisk/cli.h"
+#include "asterisk/lock.h"
+#include "asterisk/options.h"
+#include "asterisk/logger.h"
+#include "asterisk/utils.h"
+#include "asterisk/channel.h"
+#include "asterisk/app.h"
 
 #define MAX_NESTED_COMMENTS 128
 #define COMMENT_START ";--"
@@ -62,6 +67,7 @@ struct ast_comment {
 
 struct ast_category {
        char name[80];
+       int ignored;                    /* do not let user of the config see this category */
        struct ast_variable *root;
        struct ast_variable *last;
        struct ast_category *next;
@@ -76,36 +82,6 @@ struct ast_config {
        int max_include_level;
 };
 
-int ast_true(const char *s)
-{
-       if (!s)
-               return 0;
-       /* Determine if this is a true value */
-       if (!strcasecmp(s, "yes") ||
-           !strcasecmp(s, "true") ||
-           !strcasecmp(s, "y") ||
-           !strcasecmp(s, "t") ||
-           !strcasecmp(s, "1") ||
-           !strcasecmp(s, "on"))
-               return -1;
-       return 0;
-}
-
-int ast_false(const char *s)
-{
-       if (!s)
-               return 0;
-       /* Determine if this is a false value */
-       if (!strcasecmp(s, "no") ||
-           !strcasecmp(s, "false") ||
-           !strcasecmp(s, "n") ||
-           !strcasecmp(s, "f") ||
-           !strcasecmp(s, "0") ||
-           !strcasecmp(s, "off"))
-               return -1;
-       return 0;
-}
-
 struct ast_variable *ast_variable_new(const char *name, const char *value) 
 {
        struct ast_variable *variable;
@@ -181,6 +157,34 @@ char *ast_variable_retrieve(const struct ast_config *config, const char *categor
        return NULL;
 }
 
+static struct ast_variable *variable_clone(const struct ast_variable *old)
+{
+       struct ast_variable *new = ast_variable_new(old->name, old->value);
+
+       if (new) {
+               new->lineno = old->lineno;
+               new->object = old->object;
+               new->blanklines = old->blanklines;
+               /* TODO: clone comments? */
+       }
+
+       return new;
+}
+static void move_variables(struct ast_category *old, struct ast_category *new)
+{
+       struct ast_variable *var;
+       struct ast_variable *next;
+
+       next = old->root;
+       old->root = NULL;
+       for (var = next; var; var = next) {
+               next = var->next;
+               var->next = NULL;
+               ast_variable_append(new, var);
+       }
+}
+
 struct ast_category *ast_category_new(const char *name) 
 {
        struct ast_category *category;
@@ -188,29 +192,34 @@ struct ast_category *ast_category_new(const char *name)
        category = malloc(sizeof(struct ast_category));
        if (category) {
                memset(category, 0, sizeof(struct ast_category));
-               strncpy(category->name, name, sizeof(category->name) - 1);
+               ast_copy_string(category->name, name, sizeof(category->name));
        }
 
        return category;
 }
 
-struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
+static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
 {
        struct ast_category *cat;
 
        for (cat = config->root; cat; cat = cat->next) {
-               if (cat->name == category_name)
+               if (cat->name == category_name && (ignored || !cat->ignored))
                        return cat;
        }
 
        for (cat = config->root; cat; cat = cat->next) {
-               if (!strcasecmp(cat->name, category_name))
+               if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
                        return cat;
        }
 
        return NULL;
 }
 
+struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
+{
+       return category_get(config, category_name, 0);
+}
+
 int ast_category_exist(const struct ast_config *config, const char *category_name)
 {
        return !!ast_category_get(config, category_name);
@@ -232,6 +241,13 @@ void ast_category_destroy(struct ast_category *cat)
        free(cat);
 }
 
+static struct ast_category *next_available_category(struct ast_category *cat)
+{
+       for (; cat && cat->ignored; cat = cat->next);
+
+       return cat;
+}
+
 char *ast_category_browse(struct ast_config *config, const char *prev)
 {      
        struct ast_category *cat = NULL;
@@ -256,6 +272,9 @@ char *ast_category_browse(struct ast_config *config, const char *prev)
                        }
                }
        }
+       
+       if (cat)
+               cat = next_available_category(cat);
 
        config->last_browse = cat;
        if (cat)
@@ -276,7 +295,20 @@ struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
 
 void ast_category_rename(struct ast_category *cat, const char *name)
 {
-       strncpy(cat->name, name, sizeof(cat->name) - 1);
+       ast_copy_string(cat->name, name, sizeof(cat->name));
+}
+
+static void inherit_category(struct ast_category *new, const struct ast_category *base)
+{
+       struct ast_variable *var;
+
+       for (var = base->root; var; var = var->next) {
+               struct ast_variable *v;
+               
+               v = variable_clone(var);
+               if (v)
+                       ast_variable_append(new, v);
+       }
 }
 
 struct ast_config *ast_config_new(void) 
@@ -323,30 +355,70 @@ void ast_config_set_current_category(struct ast_config *cfg, const struct ast_ca
 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile)
 {
        char *c;
-       char *cur;
+       char *cur = buf;
        struct ast_variable *v;
-       int object;
-
-       cur = ast_strip(buf);
-       if (ast_strlen_zero(cur))
-               return 0;
+       char cmd[512], exec_file[512];
+       int object, do_exec, do_include;
 
        /* Actually parse the entry */
        if (cur[0] == '[') {
+               struct ast_category *newcat = NULL;
+               char *catname;
+
                /* A category header */
                c = strchr(cur, ']');
                if (!c) {
                        ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
                        return -1;
                }
-               *c = '\0';
+               *c++ = '\0';
                cur++;
-               *cat = ast_category_new(cur);
-               if (!*cat) {
+               if (*c++ != '(')
+                       c = NULL;
+               catname = cur;
+               *cat = newcat = ast_category_new(catname);
+               if (!newcat) {
                        ast_log(LOG_WARNING, "Out of memory, line %d of %s\n", lineno, configfile);
                        return -1;
                }
-               ast_category_append(cfg, *cat);
+               /* If there are options or categories to inherit from, process them now */
+               if (c) {
+                       if (!(cur = strchr(c, ')'))) {
+                               ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
+                               return -1;
+                       }
+                       *cur = '\0';
+                       while ((cur = strsep(&c, ","))) {
+                               if (!strcasecmp(cur, "!")) {
+                                       (*cat)->ignored = 1;
+                               } else if (!strcasecmp(cur, "+")) {
+                                       *cat = category_get(cfg, catname, 1);
+                                       if (!*cat) {
+                                               ast_destroy(cfg);
+                                               if (newcat)
+                                                       ast_category_destroy(newcat);
+                                               ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
+                                               return -1;
+                                       }
+                                       if (newcat) {
+                                               move_variables(newcat, *cat);
+                                               ast_category_destroy(newcat);
+                                               newcat = NULL;
+                                       }
+                               } else {
+                                       struct ast_category *base;
+                               
+                                       base = category_get(cfg, cur, 1);
+                                       if (!base) {
+                                               ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
+                                               return -1;
+                                       }
+                                       inherit_category(*cat, base);
+                               }
+                       }
+               }
+               if (newcat)
+                       ast_category_append(cfg, *cat);
        } else if (cur[0] == '#') {
                /* A directive */
                cur++;
@@ -361,8 +433,16 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                                c = NULL;
                } else 
                        c = NULL;
-               if (!strcasecmp(cur, "include")) {
-                       /* A #include */
+               do_include = !strcasecmp(cur, "include");
+               if(!do_include)
+                       do_exec = !strcasecmp(cur, "exec");
+               else
+                       do_exec = 0;
+               if (do_exec && !option_exec_includes) {
+                       ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
+                       do_exec = 0;
+               }
+               if (do_include || do_exec) {
                        if (c) {
                                /* Strip off leading and trailing "'s and <>'s */
                                while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
@@ -375,11 +455,29 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                                        else
                                                break;
                                }
+                               /* #exec </path/to/executable>
+                                  We create a tmp file, then we #include it, then we delete it. */
+                               if (do_exec) { 
+                                       snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%ld.%ld", time(NULL), (long)pthread_self());
+                                       snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
+                                       ast_safe_system(cmd);
+                                       cur = exec_file;
+                               } else
+                                       exec_file[0] = '\0';
+                               /* A #include */
+                               do_include = ast_config_internal_load(cur, cfg) ? 1 : 0;
+                               if(!ast_strlen_zero(exec_file))
+                                       unlink(exec_file);
+                               if(!do_include)
+                                       return 0;
 
-                               if (!ast_config_internal_load(cur, cfg))
-                                       return -1;
-                       } else
-                               ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
+                       } else {
+                               ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n", 
+                                               do_exec ? "exec" : "include",
+                                               do_exec ? "/path/to/executable" : "filename",
+                                               lineno,
+                                               configfile);
+                       }
                }
                else 
                        ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
@@ -428,11 +526,12 @@ static struct ast_config *config_text_file_load(const char *database, const char
        int lineno=0;
        int comment = 0, nest[MAX_NESTED_COMMENTS];
        struct ast_category *cat = NULL;
+       int count = 0;
        
        cat = ast_config_get_current_category(cfg);
 
        if (filename[0] == '/') {
-               strncpy(fn, filename, sizeof(fn)-1);
+               ast_copy_string(fn, filename, sizeof(fn));
        } else {
                snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
        }
@@ -457,16 +556,17 @@ static struct ast_config *config_text_file_load(const char *database, const char
                        /* loop over expanded files */
                        int i;
                        for (i=0; i<globbuf.gl_pathc; i++) {
-                               strncpy(fn, globbuf.gl_pathv[i], sizeof(fn)-1);
+                               ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
 #endif
        if ((option_verbose > 1) && !option_debug) {
                ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
                fflush(stdout);
        }
        if ((f = fopen(fn, "r"))) {
+               count++;
                if (option_debug)
                        ast_log(LOG_DEBUG, "Parsing %s\n", fn);
-               else if (option_verbose > 2)
+               else if (option_verbose > 1)
                        ast_verbose("Found\n");
                while(!feof(f)) {
                        lineno++;
@@ -518,9 +618,13 @@ static struct ast_config *config_text_file_load(const char *database, const char
                                                        new_buf = comment_p + 1;
                                        }
                                }
-                               if (process_buf && process_text_line(cfg, &cat, process_buf, lineno, filename)) {
-                                       cfg = NULL;
-                                       break;
+                               if (process_buf) {
+                                       char *buf = ast_strip(process_buf);
+                                       if (!ast_strlen_zero(buf))
+                                               if (process_text_line(cfg, &cat, buf, lineno, filename)) {
+                                                       cfg = NULL;
+                                                       break;
+                                               }
                                }
                        }
                }
@@ -528,7 +632,7 @@ static struct ast_config *config_text_file_load(const char *database, const char
        } else { /* can't open file */
                if (option_debug)
                        ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
-               else if (option_verbose > 2)
+               else if (option_verbose > 1)
                        ast_verbose( "Not found (%s)\n", strerror(errno));
        }
        if (comment) {
@@ -542,6 +646,8 @@ static struct ast_config *config_text_file_load(const char *database, const char
                        }
                }
 #endif
+       if (count == 0)
+               return NULL;
 
        return cfg;
 }
@@ -557,12 +663,12 @@ int config_text_file_save(const char *configfile, const struct ast_config *cfg,
        int blanklines = 0;
 
        if (configfile[0] == '/') {
-               strncpy(fn, configfile, sizeof(fn)-1);
+               ast_copy_string(fn, configfile, sizeof(fn));
        } else {
-               snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
+               snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
        }
        time(&t);
-       strncpy(date, ctime(&t), sizeof(date) - 1);
+       ast_copy_string(date, ctime(&t), sizeof(date));
        if ((f = fopen(fn, "w"))) {
                if ((option_verbose > 1) && !option_debug)
                        ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
@@ -622,21 +728,58 @@ static void clear_config_maps(void)
        ast_mutex_unlock(&config_lock);
 }
 
-void read_config_maps(void) 
+static int append_mapping(char *name, char *driver, char *database, char *table)
 {
-       struct ast_config *config;
-       struct ast_variable *v;
        struct ast_config_map *map;
        int length;
+
+       length = sizeof(*map);
+       length += strlen(name) + 1;
+       length += strlen(driver) + 1;
+       length += strlen(database) + 1;
+       if (table)
+               length += strlen(table) + 1;
+       map = malloc(length);
+
+       if (!map)
+               return -1;
+
+       memset(map, 0, length);
+       map->name = map->stuff;
+       strcpy(map->name, name);
+       map->driver = map->name + strlen(map->name) + 1;
+       strcpy(map->driver, driver);
+       map->database = map->driver + strlen(map->driver) + 1;
+       strcpy(map->database, database);
+       if (table) {
+               map->table = map->database + strlen(map->database) + 1;
+               strcpy(map->table, table);
+       }
+       map->next = config_maps;
+
+       if (option_verbose > 1)
+               ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
+                           map->name, map->driver, map->database, map->table ? map->table : map->name);
+
+       config_maps = map;
+       return 0;
+}
+
+void read_config_maps(void) 
+{
+       struct ast_config *config, *configtmp;
+       struct ast_variable *v;
        char *driver, *table, *database, *stringp;
 
        clear_config_maps();
 
-       config = ast_config_new();
-       config->max_include_level = 1;
-       config = ast_config_internal_load(extconfig_conf, config);
-       if (!config)
+       configtmp = ast_config_new();
+       configtmp->max_include_level = 1;
+       config = ast_config_internal_load(extconfig_conf, configtmp);
+       if (!config) {
+               ast_config_destroy(configtmp);
                return;
+       }
 
        for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
                stringp = v->value;
@@ -644,43 +787,33 @@ void read_config_maps(void)
                database = strsep(&stringp, ",");
                table = strsep(&stringp, ",");
                        
-               if (!strcmp(v->name, extconfig_conf) || !strcmp(v->name, "asterisk.conf")) {
-                       ast_log(LOG_WARNING, "Cannot bind asterisk.conf or extconfig.conf!\n");
+               if (!strcmp(v->name, extconfig_conf)) {
+                       ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
                        continue;
                }
 
-               if (!driver || !database)
+               if (!strcmp(v->name, "asterisk.conf")) {
+                       ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
                        continue;
+               }
 
-               length = sizeof(*map);
-               length += strlen(v->name) + 1;
-               length += strlen(driver) + 1;
-               length += strlen(database) + 1;
-               if (table)
-                       length += strlen(table) + 1;
-               map = malloc(length);
-
-               if (!map)
+               if (!strcmp(v->name, "logger.conf")) {
+                       ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
                        continue;
-
-               memset(map, 0, length);
-               map->name = map->stuff;
-               strcpy(map->name, v->name);
-               map->driver = map->name + strlen(map->name) + 1;
-               strcpy(map->driver, driver);
-               map->database = map->driver + strlen(map->driver) + 1;
-               strcpy(map->database, database);
-               if (table) {
-                       map->table = map->database + strlen(map->database) + 1;
-                       strcpy(map->table, table);
                }
-               map->next = config_maps;
-
-               if (option_verbose > 1)
-                       ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
-                                   map->name, map->driver, map->database, map->table ? map->table : map->name);
 
-               config_maps = map;
+               if (!driver || !database)
+                       continue;
+               if (!strcasecmp(v->name, "sipfriends")) {
+                       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");
+                       append_mapping("sipusers", driver, database, table ? table : "sipfriends");
+                       append_mapping("sippeers", driver, database, table ? table : "sipfriends");
+               } else if (!strcasecmp(v->name, "iaxfriends")) {
+                       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");
+                       append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
+                       append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
+               } else 
+                       append_mapping(v->name, driver, database, table);
        }
                
        ast_config_destroy(config);
@@ -729,32 +862,30 @@ int ast_config_engine_deregister(struct ast_config_engine *del)
 
 static struct ast_config_engine *find_engine(const char *filename, char *database, int dbsiz, char *table, int tabsiz) 
 {
-       struct ast_config_engine *eng, *ret=NULL;
+       struct ast_config_engine *eng, *ret = NULL;
        struct ast_config_map *map;
 
        ast_mutex_lock(&config_lock);
 
-       map = config_maps;
-       while (map) {
+       for (map = config_maps; map; map = map->next) {
                if (!strcasecmp(filename, map->name)) {
-                       strncpy(database, map->database, dbsiz-1);
-                       if (map->table)
-                               strncpy(table, map->table, tabsiz-1);
-                       else
-                               strncpy(table, filename, tabsiz-1);
+                       ast_copy_string(database, map->database, dbsiz);
+                       ast_copy_string(table, map->table ? map->table : filename, tabsiz);
                        break;
                }
-               map = map->next;
        }
        if (map) {
-               for (eng = config_engine_list; eng; eng = eng->next) {
-                       if (!strcmp(eng->name, map->driver)) {
+               for (eng = config_engine_list; !ret && eng; eng = eng->next) {
+                       if (!strcasecmp(eng->name, map->driver))
                                ret = eng;
-                               break;
-                       }
                }
        }
+
        ast_mutex_unlock(&config_lock);
+       
+       /* if we found a mapping, but the engine is not available, then issue a warning */
+       if (map && !ret)
+               ast_log(LOG_WARNING, "Mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
 
        return ret;
 }
@@ -782,6 +913,8 @@ struct ast_config *ast_config_internal_load(const char *filename, struct ast_con
                struct ast_config_engine *eng;
 
                eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
+
+
                if (eng && eng->load_func) {
                        loader = eng;
                } else {
@@ -792,6 +925,7 @@ struct ast_config *ast_config_internal_load(const char *filename, struct ast_con
        }
 
        result = loader->load_func(db, table, filename, cfg);
+
        if (result)
                result->include_level--;
 
@@ -879,7 +1013,6 @@ static int config_command(int fd, int argc, char **argv)
                        if (!strcasecmp(map->driver, eng->name)) {
                                ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
                                        map->table ? map->table : map->name);
-                               break;
                        }
        }
        ast_cli(fd,"\n\n");
@@ -889,8 +1022,12 @@ static int config_command(int fd, int argc, char **argv)
        return 0;
 }
 
+static char show_config_help[] =
+       "Usage: show config mappings\n"
+       "       Shows the filenames to config engines.\n";
+
 static struct ast_cli_entry config_command_struct = {
-       { "show", "config", "mappings" }, config_command, "Show Config mappings (file names to config engines)"
+       { "show", "config", "mappings", NULL }, config_command, "Show Config mappings (file names to config engines)", show_config_help, NULL
 };
 
 int register_config_cli()