Issue #5637 - Realtime driver for PostgreSQL (mguesdon)
[asterisk/asterisk.git] / config.c
old mode 100755 (executable)
new mode 100644 (file)
index b38dc54..8afdd39
--- a/config.c
+++ b/config.c
@@ -1,14 +1,29 @@
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
- * Configuration File Parser
- * 
  * Copyright (C) 1999 - 2005, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
  * This program is free software, distributed under the terms of
- * the GNU General Public License
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Configuration File Parser
+ *
+ * \author Mark Spencer <markster@digium.com>
+ *
+ * Includes the Asterisk Realtime API - ARA
+ * See doc/realtime.txt and doc/extconfig.txt
  */
 
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
 #include <time.h>
+#include <sys/stat.h>
 #define AST_INCLUDE_GLOB 1
 #ifdef AST_INCLUDE_GLOB
-#ifdef __OSX__
+#if defined(__Darwin__) || defined(__CYGWIN__)
 #define GLOB_ABORTED GLOB_ABEND
 #endif
 # include <glob.h>
 #endif
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
 #include "asterisk/config.h"
 #include "asterisk/cli.h"
 #include "asterisk/lock.h"
@@ -33,8 +53,6 @@
 #include "asterisk/utils.h"
 #include "asterisk/channel.h"
 #include "asterisk/app.h"
-#include "asterisk.h"
-#include "astconf.h"
 
 #define MAX_NESTED_COMMENTS 128
 #define COMMENT_START ";--"
@@ -80,46 +98,14 @@ 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;
+       int name_len = strlen(name) + 1;        
 
-       int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
-       variable = malloc(length);
-       if (variable) {
-               memset(variable, 0, length);
+       if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
                variable->name = variable->stuff;
-               variable->value = variable->stuff + strlen(name) + 1;           
+               variable->value = variable->stuff + name_len;           
                strcpy(variable->name,name);
                strcpy(variable->value,value);
        }
@@ -167,12 +153,10 @@ char *ast_variable_retrieve(const struct ast_config *config, const char *categor
        struct ast_variable *v;
 
        if (category) {
-               for (v = ast_variable_browse(config, category); v; v = v->next)
-                       if (variable == v->name)
-                               return v->value;
-               for (v = ast_variable_browse(config, category); v; v = v->next)
+               for (v = ast_variable_browse(config, category); v; v = v->next) {
                        if (!strcasecmp(variable, v->name))
                                return v->value;
+               }
        } else {
                struct ast_category *cat;
 
@@ -217,10 +201,8 @@ struct ast_category *ast_category_new(const char *name)
 {
        struct ast_category *category;
 
-       category = malloc(sizeof(struct ast_category));
-       if (category) {
-               memset(category, 0, sizeof(struct ast_category));
-               strncpy(category->name, name, sizeof(category->name) - 1);
+       if ((category = ast_calloc(1, sizeof(*category)))) {            
+               ast_copy_string(category->name, name, sizeof(category->name));
        }
 
        return category;
@@ -323,7 +305,7 @@ 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)
@@ -343,9 +325,7 @@ struct ast_config *ast_config_new(void)
 {
        struct ast_config *config;
 
-       config = malloc(sizeof(*config));
-       if (config) {
-               memset(config, 0, sizeof(*config));
+       if ((config = ast_calloc(1, sizeof(*config)))) {
                config->max_include_level = MAX_INCLUDE_LEVEL;
        }
 
@@ -404,9 +384,7 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **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);
+               if (!(*cat = newcat = ast_category_new(catname))) {
                        return -1;
                }
                /* If there are options or categories to inherit from, process them now */
@@ -422,7 +400,7 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                                } else if (!strcasecmp(cur, "+")) {
                                        *cat = category_get(cfg, catname, 1);
                                        if (!*cat) {
-                                               ast_destroy(cfg);
+                                               ast_config_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);
@@ -454,9 +432,8 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                while(*c && (*c > 32)) c++;
                if (*c) {
                        *c = '\0';
-                       c++;
                        /* Find real argument */
-                       while(*c  && (*c < 33)) c++;
+                       c = ast_skip_blanks(c + 1);
                        if (!*c)
                                c = NULL;
                } else 
@@ -466,7 +443,7 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                        do_exec = !strcasecmp(cur, "exec");
                else
                        do_exec = 0;
-               if (do_exec && !option_exec_includes) {
+               if (do_exec && !ast_opt_exec_includes) {
                        ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
                        do_exec = 0;
                }
@@ -486,7 +463,7 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                                /* #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), pthread_self());
+                                       snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d.%ld", (int)time(NULL), (long)pthread_self());
                                        snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
                                        ast_safe_system(cmd);
                                        cur = exec_file;
@@ -497,7 +474,7 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                                if(!ast_strlen_zero(exec_file))
                                        unlink(exec_file);
                                if(!do_include)
-                                       return -1;
+                                       return 0;
 
                        } else {
                                ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n", 
@@ -526,15 +503,13 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                                c++;
                        } else
                                object = 0;
-                       v = ast_variable_new(ast_strip(cur), ast_strip(c));
-                       if (v) {
+                       if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
                                v->lineno = lineno;
                                v->object = object;
                                /* Put and reset comments */
                                v->blanklines = 0;
                                ast_variable_append(*cat, v);
                        } else {
-                               ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
                                return -1;
                        }
                } else {
@@ -554,11 +529,13 @@ 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;
+       struct stat statbuf;
        
        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);
        }
@@ -583,13 +560,28 @@ 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"))) {
+       do {
+               if (stat(fn, &statbuf))
+                       continue;
+
+               if (!S_ISREG(statbuf.st_mode)) {
+                       ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
+                       continue;
+               }
+               if ((option_verbose > 1) && !option_debug) {
+                       ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
+                       fflush(stdout);
+               }
+               if (!(f = fopen(fn, "r"))) {
+                       if (option_debug)
+                               ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
+                       else if (option_verbose > 1)
+                               ast_verbose( "Not found (%s)\n", strerror(errno));
+                       continue;
+               }
+               count++;
                if (option_debug)
                        ast_log(LOG_DEBUG, "Parsing %s\n", fn);
                else if (option_verbose > 1)
@@ -646,21 +638,17 @@ static struct ast_config *config_text_file_load(const char *database, const char
                                }
                                if (process_buf) {
                                        char *buf = ast_strip(process_buf);
-                                       if (!ast_strlen_zero(buf))
+                                       if (!ast_strlen_zero(buf)) {
                                                if (process_text_line(cfg, &cat, buf, lineno, filename)) {
                                                        cfg = NULL;
                                                        break;
                                                }
+                                       }
                                }
                        }
                }
                fclose(f);              
-       } else { /* can't open file */
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
-               else if (option_verbose > 1)
-                       ast_verbose( "Not found (%s)\n", strerror(errno));
-       }
+       } while(0);
        if (comment) {
                ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
        }
@@ -672,6 +660,8 @@ static struct ast_config *config_text_file_load(const char *database, const char
                        }
                }
 #endif
+       if (count == 0)
+               return NULL;
 
        return cfg;
 }
@@ -687,13 +677,17 @@ 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));
+#ifdef __CYGWIN__      
+       if ((f = fopen(fn, "w+"))) {
+#else
        if ((f = fopen(fn, "w"))) {
+#endif     
                if ((option_verbose > 1) && !option_debug)
                        ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
                fprintf(f, ";!\n");
@@ -763,12 +757,10 @@ static int append_mapping(char *name, char *driver, char *database, char *table)
        length += strlen(database) + 1;
        if (table)
                length += strlen(table) + 1;
-       map = malloc(length);
 
-       if (!map)
+       if (!(map = ast_calloc(1, length)))
                return -1;
 
-       memset(map, 0, length);
        map->name = map->stuff;
        strcpy(map->name, name);
        map->driver = map->name + strlen(map->name) + 1;
@@ -789,28 +781,50 @@ static int append_mapping(char *name, char *driver, char *database, char *table)
        return 0;
 }
 
-void read_config_maps(void) 
+int read_config_maps(void) 
 {
-       struct ast_config *config;
+       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)
-               return;
+       configtmp = ast_config_new();
+       configtmp->max_include_level = 1;
+       config = ast_config_internal_load(extconfig_conf, configtmp);
+       if (!config) {
+               ast_config_destroy(configtmp);
+               return 0;
+       }
 
        for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
                stringp = v->value;
                driver = strsep(&stringp, ",");
-               database = strsep(&stringp, ",");
+
+               /* check if the database text starts with a double quote */
+               if (*stringp == '"') {
+                       stringp++;
+                       database = strsep(&stringp, "\"");
+                       strsep(&stringp, ",");
+               } else {
+                       /* apparently this text has no quotes */
+                       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 (!strcmp(v->name, "asterisk.conf")) {
+                       ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
+                       continue;
+               }
+
+               if (!strcmp(v->name, "logger.conf")) {
+                       ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
                        continue;
                }
 
@@ -829,6 +843,7 @@ void read_config_maps(void)
        }
                
        ast_config_destroy(config);
+       return 0;
 }
 
 int ast_config_engine_register(struct ast_config_engine *new) 
@@ -872,34 +887,37 @@ int ast_config_engine_deregister(struct ast_config_engine *del)
        return 0;
 }
 
-static struct ast_config_engine *find_engine(const char *filename, char *database, int dbsiz, char *table, int tabsiz) 
+/*! \brief Find realtime engine for realtime family */
+static struct ast_config_engine *find_engine(const char *family, 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) {
-               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);
+       for (map = config_maps; map; map = map->next) {
+               if (!strcasecmp(family, map->name)) {
+                       if (database)
+                               ast_copy_string(database, map->database, dbsiz);
+                       if (table)
+                               ast_copy_string(table, map->table ? map->table : family, tabsiz);
                        break;
                }
-               map = map->next;
        }
+
+       /* Check if the required driver (engine) exist */
        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, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
 
        return ret;
 }
@@ -979,6 +997,18 @@ struct ast_variable *ast_load_realtime(const char *family, ...)
        return res;
 }
 
+/*! \brief Check if realtime engine is configured for family */
+int ast_check_realtime(const char *family)
+{
+       struct ast_config_engine *eng;
+
+       eng = find_engine(family, NULL, 0, NULL, 0);
+       if (eng)
+               return 1;
+       return 0;
+
+}
+
 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
 {
        struct ast_config_engine *eng;
@@ -1027,7 +1057,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");