Wed Aug 7 17:15:37 2019

Asterisk developer's documentation


cdr_sqlite3_custom.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2007, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com> and others.
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*!
00020  * \file
00021  * \brief Custom SQLite3 CDR records.
00022  *
00023  * \author Adapted by Alejandro Rios <alejandro.rios@avatar.com.co> and
00024  *  Russell Bryant <russell@digium.com> from
00025  *  cdr_mysql_custom by Edward Eastman <ed@dm3.co.uk>,
00026  * and cdr_sqlite by Holger Schurig <hs4233@mail.mn-solutions.de>
00027  *
00028  *
00029  * \arg See also \ref AstCDR
00030  *
00031  *
00032  * \ingroup cdr_drivers
00033  */
00034 
00035 /*** MODULEINFO
00036    <depend>sqlite3</depend>
00037    <support_level>extended</support_level>
00038  ***/
00039 
00040 #include "asterisk.h"
00041 
00042 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 416336 $")
00043 
00044 #include <sqlite3.h>
00045 
00046 #include "asterisk/paths.h"   /* use ast_config_AST_LOG_DIR */
00047 #include "asterisk/channel.h"
00048 #include "asterisk/cdr.h"
00049 #include "asterisk/module.h"
00050 #include "asterisk/config.h"
00051 #include "asterisk/pbx.h"
00052 #include "asterisk/utils.h"
00053 #include "asterisk/cli.h"
00054 #include "asterisk/app.h"
00055 
00056 AST_MUTEX_DEFINE_STATIC(lock);
00057 
00058 static const char config_file[] = "cdr_sqlite3_custom.conf";
00059 
00060 static const char desc[] = "Customizable SQLite3 CDR Backend";
00061 static const char name[] = "cdr_sqlite3_custom";
00062 static sqlite3 *db = NULL;
00063 
00064 static char table[80];
00065 static char *columns;
00066 
00067 struct values {
00068    AST_LIST_ENTRY(values) list;
00069    char expression[1];
00070 };
00071 
00072 static AST_LIST_HEAD_STATIC(sql_values, values);
00073 
00074 static void free_config(int reload);
00075 
00076 static int load_column_config(const char *tmp)
00077 {
00078    char *col = NULL;
00079    char *cols = NULL, *save = NULL;
00080    char *escaped = NULL;
00081    struct ast_str *column_string = NULL;
00082 
00083    if (ast_strlen_zero(tmp)) {
00084       ast_log(LOG_WARNING, "Column names not specified. Module not loaded.\n");
00085       return -1;
00086    }
00087    if (!(column_string = ast_str_create(1024))) {
00088       ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
00089       return -1;
00090    }
00091    if (!(save = cols = ast_strdup(tmp))) {
00092       ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
00093       ast_free(column_string);
00094       return -1;
00095    }
00096    while ((col = strsep(&cols, ","))) {
00097       col = ast_strip(col);
00098       escaped = sqlite3_mprintf("%q", col);
00099       if (!escaped) {
00100          ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s.'\n", col, table);
00101          ast_free(column_string);
00102          ast_free(save);
00103          return -1;
00104       }
00105       ast_str_append(&column_string, 0, "%s%s", ast_str_strlen(column_string) ? "," : "", escaped);
00106       sqlite3_free(escaped);
00107    }
00108    if (!(columns = ast_strdup(ast_str_buffer(column_string)))) {
00109       ast_log(LOG_ERROR, "Out of memory copying columns string for table '%s.'\n", table);
00110       ast_free(column_string);
00111       ast_free(save);
00112       return -1;
00113    }
00114    ast_free(column_string);
00115    ast_free(save);
00116 
00117    return 0;
00118 }
00119 
00120 static int load_values_config(const char *tmp)
00121 {
00122    char *vals = NULL, *save = NULL;
00123    struct values *value = NULL;
00124    int i;
00125    AST_DECLARE_APP_ARGS(val,
00126       AST_APP_ARG(ues)[200]; /* More than 200 columns in this CDR?  Yeah, right... */
00127    );
00128 
00129    if (ast_strlen_zero(tmp)) {
00130       ast_log(LOG_WARNING, "Values not specified. Module not loaded.\n");
00131       return -1;
00132    }
00133    if (!(save = vals = ast_strdup(tmp))) {
00134       ast_log(LOG_ERROR, "Out of memory creating temporary buffer for value '%s'\n", tmp);
00135       return -1;
00136    }
00137    AST_STANDARD_RAW_ARGS(val, vals);
00138    for (i = 0; i < val.argc; i++) {
00139       /* Strip the single quotes off if they are there */
00140       char *v = ast_strip_quoted(val.ues[i], "'", "'");
00141       value = ast_calloc(sizeof(char), sizeof(*value) + strlen(v));
00142       if (!value) {
00143          ast_log(LOG_ERROR, "Out of memory creating entry for value '%s'\n", v);
00144          ast_free(save);
00145          return -1;
00146       }
00147       strcpy(value->expression, v); /* SAFE */
00148       AST_LIST_INSERT_TAIL(&sql_values, value, list);
00149    }
00150    ast_free(save);
00151 
00152    return 0;
00153 }
00154 
00155 static int load_config(int reload)
00156 {
00157    struct ast_config *cfg;
00158    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00159    struct ast_variable *mappingvar;
00160    const char *tmp;
00161 
00162    if ((cfg = ast_config_load(config_file, config_flags)) == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
00163       ast_log(LOG_WARNING, "Failed to %sload configuration file. %s\n", reload ? "re" : "", reload ? "" : "Module not activated.");
00164       return -1;
00165    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
00166       return 0;
00167    }
00168 
00169    if (reload) {
00170       free_config(1);
00171    }
00172 
00173    if (!(mappingvar = ast_variable_browse(cfg, "master"))) {
00174       /* Nothing configured */
00175       ast_config_destroy(cfg);
00176       return -1;
00177    }
00178 
00179    /* Mapping must have a table name */
00180    if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "master", "table"))) {
00181       ast_copy_string(table, tmp, sizeof(table));
00182    } else {
00183       ast_log(LOG_WARNING, "Table name not specified.  Assuming cdr.\n");
00184       strcpy(table, "cdr");
00185    }
00186 
00187    /* Columns */
00188    if (load_column_config(ast_variable_retrieve(cfg, "master", "columns"))) {
00189       ast_config_destroy(cfg);
00190       free_config(0);
00191       return -1;
00192    }
00193 
00194    /* Values */
00195    if (load_values_config(ast_variable_retrieve(cfg, "master", "values"))) {
00196       ast_config_destroy(cfg);
00197       free_config(0);
00198       return -1;
00199    }
00200 
00201    ast_verb(3, "cdr_sqlite3_custom: Logging CDR records to table '%s' in 'master.db'\n", table);
00202 
00203    ast_config_destroy(cfg);
00204 
00205    return 0;
00206 }
00207 
00208 static void free_config(int reload)
00209 {
00210    struct values *value;
00211 
00212    if (!reload && db) {
00213       sqlite3_close(db);
00214       db = NULL;
00215    }
00216 
00217    if (columns) {
00218       ast_free(columns);
00219       columns = NULL;
00220    }
00221 
00222    while ((value = AST_LIST_REMOVE_HEAD(&sql_values, list))) {
00223       ast_free(value);
00224    }
00225 }
00226 
00227 static int write_cdr(struct ast_cdr *cdr)
00228 {
00229    int res = 0;
00230    char *error = NULL;
00231    char *sql = NULL;
00232 
00233    if (db == NULL) {
00234       /* Should not have loaded, but be failsafe. */
00235       return 0;
00236    }
00237 
00238    ast_mutex_lock(&lock);
00239 
00240    { /* Make it obvious that only sql should be used outside of this block */
00241       char *escaped;
00242       char subst_buf[2048];
00243       struct values *value;
00244       struct ast_channel *dummy;
00245       struct ast_str *value_string = ast_str_create(1024);
00246 
00247       dummy = ast_dummy_channel_alloc();
00248       if (!dummy) {
00249          ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
00250          ast_free(value_string);
00251          ast_mutex_unlock(&lock);
00252          return 0;
00253       }
00254       dummy->cdr = ast_cdr_dup(cdr);
00255       AST_LIST_TRAVERSE(&sql_values, value, list) {
00256          pbx_substitute_variables_helper(dummy, value->expression, subst_buf, sizeof(subst_buf) - 1);
00257          escaped = sqlite3_mprintf("%q", subst_buf);
00258          ast_str_append(&value_string, 0, "%s'%s'", ast_str_strlen(value_string) ? "," : "", escaped);
00259          sqlite3_free(escaped);
00260       }
00261       sql = sqlite3_mprintf("INSERT INTO %q (%s) VALUES (%s)", table, columns, ast_str_buffer(value_string));
00262       ast_debug(1, "About to log: %s\n", sql);
00263       ast_channel_unref(dummy);
00264       ast_free(value_string);
00265    }
00266 
00267    if (sqlite3_exec(db, sql, NULL, NULL, &error) != SQLITE_OK) {
00268       ast_log(LOG_ERROR, "%s. SQL: %s.\n", error, sql);
00269       sqlite3_free(error);
00270    }
00271 
00272    if (sql) {
00273       sqlite3_free(sql);
00274    }
00275 
00276    ast_mutex_unlock(&lock);
00277 
00278    return res;
00279 }
00280 
00281 static int unload_module(void)
00282 {
00283    ast_cdr_unregister(name);
00284 
00285    free_config(0);
00286 
00287    return 0;
00288 }
00289 
00290 static int load_module(void)
00291 {
00292    char *error;
00293    char filename[PATH_MAX];
00294    int res;
00295    char *sql;
00296 
00297    if (load_config(0)) {
00298       return AST_MODULE_LOAD_DECLINE;
00299    }
00300 
00301    /* is the database there? */
00302    snprintf(filename, sizeof(filename), "%s/master.db", ast_config_AST_LOG_DIR);
00303    res = sqlite3_open(filename, &db);
00304    if (res != SQLITE_OK) {
00305       ast_log(LOG_ERROR, "Could not open database %s.\n", filename);
00306       free_config(0);
00307       return AST_MODULE_LOAD_DECLINE;
00308    }
00309    sqlite3_busy_timeout(db, 1000);
00310    /* is the table there? */
00311    sql = sqlite3_mprintf("SELECT COUNT(AcctId) FROM %q;", table);
00312    res = sqlite3_exec(db, sql, NULL, NULL, NULL);
00313    sqlite3_free(sql);
00314    if (res != SQLITE_OK) {
00315       /* We don't use %q for the column list here since we already escaped when building it */
00316       sql = sqlite3_mprintf("CREATE TABLE %q (AcctId INTEGER PRIMARY KEY, %s)", table, columns);
00317       res = sqlite3_exec(db, sql, NULL, NULL, &error);
00318       sqlite3_free(sql);
00319       if (res != SQLITE_OK) {
00320          ast_log(LOG_WARNING, "Unable to create table '%s': %s.\n", table, error);
00321          sqlite3_free(error);
00322          free_config(0);
00323          return AST_MODULE_LOAD_DECLINE;
00324       }
00325    }
00326 
00327    res = ast_cdr_register(name, desc, write_cdr);
00328    if (res) {
00329       ast_log(LOG_ERROR, "Unable to register custom SQLite3 CDR handling\n");
00330       free_config(0);
00331       return AST_MODULE_LOAD_DECLINE;
00332    }
00333 
00334    return AST_MODULE_LOAD_SUCCESS;
00335 }
00336 
00337 static int reload(void)
00338 {
00339    int res = 0;
00340 
00341    ast_mutex_lock(&lock);
00342    res = load_config(1);
00343    ast_mutex_unlock(&lock);
00344 
00345    return res;
00346 }
00347 
00348 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SQLite3 Custom CDR Module",
00349    .load = load_module,
00350    .unload = unload_module,
00351    .reload = reload,
00352    .load_pri = AST_MODPRI_CDR_DRIVER,
00353 );

Generated on 7 Aug 2019 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1