aboutsummaryrefslogtreecommitdiffstats
path: root/cdr/cdr_sqlite3_custom.c
blob: 7eff53d8d0936f200ae4dbc9be085dcb51abe563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 1999 - 2007, Digium, Inc.
 *
 * Mark Spencer <markster@digium.com> and others.
 *
 * 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 Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Custom SQLite3 CDR records.
 *
 * \author Adapted by Alejandro Rios <alejandro.rios@avatar.com.co> and
 *  Russell Bryant <russell@digium.com> from 
 *  cdr_mysql_custom by Edward Eastman <ed@dm3.co.uk>,
 *	and cdr_sqlite by Holger Schurig <hs4233@mail.mn-solutions.de>
 *	
 *
 * \arg See also \ref AstCDR
 *
 *
 * \ingroup cdr_drivers
 */

/*** MODULEINFO
	<depend>sqlite3</depend>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include <time.h>
#include <sqlite3.h>

#include "asterisk/paths.h"	/* use ast_config_AST_LOG_DIR */
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/cli.h"

AST_MUTEX_DEFINE_STATIC(lock);

static const char config_file[] = "cdr_sqlite3_custom.conf";

static char *desc = "Customizable SQLite3 CDR Backend";
static char *name = "cdr_sqlite3_custom";
static sqlite3 *db = NULL;

static char table[80];
static char columns[1024];
static char values[1024];

static int load_config(int reload)
{
	struct ast_config *cfg;
	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
	struct ast_variable *mappingvar;
	const char *tmp;

	if (!(cfg = ast_config_load(config_file, config_flags))) {
		if (reload)
			ast_log(LOG_WARNING, "%s: Failed to reload configuration file.\n", name);
		else {
			ast_log(LOG_WARNING,
					"%s: Failed to load configuration file. Module not activated.\n",
					name);
		}
		return -1;
	} else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
		return 0;

	ast_mutex_lock(&lock);

	if (!(mappingvar = ast_variable_browse(cfg, "master"))) {
		/* nothing configured */
		ast_mutex_unlock(&lock);
		ast_config_destroy(cfg);
		return 0;
	}
	
	/* Mapping must have a table name */
	tmp = ast_variable_retrieve(cfg, "master", "table");
	if (!ast_strlen_zero(tmp))
		ast_copy_string(table, tmp, sizeof(table));
	else {
		ast_log(LOG_WARNING, "%s: Table name not specified.  Assuming cdr.\n", name);
		strcpy(table, "cdr");
	}

	tmp = ast_variable_retrieve(cfg, "master", "columns");
	if (!ast_strlen_zero(tmp))
		ast_copy_string(columns, tmp, sizeof(columns));
	else {
		ast_log(LOG_WARNING, "%s: Column names not specified. Module not loaded.\n",
				name);
		ast_mutex_unlock(&lock);
		ast_config_destroy(cfg);
		return -1;
	}

	tmp = ast_variable_retrieve(cfg, "master", "values");
	if (!ast_strlen_zero(tmp))
		ast_copy_string(values, tmp, sizeof(values));
	else {
		ast_log(LOG_WARNING, "%s: Values not specified. Module not loaded.\n", name);
		ast_mutex_unlock(&lock);
		ast_config_destroy(cfg);
		return -1;
	}

	ast_mutex_unlock(&lock);

	ast_config_destroy(cfg);

	return 0;
}

/* assumues 'to' buffer is at least strlen(from) * 2 + 1 bytes */
static int do_escape(char *to, const char *from)
{
	char *out = to;

	for (; *from; from++) {
		if (*from == '\'' || *from == '\\')
			*out++ = *from;
		*out++ = *from;
	}
	*out = '\0';

	return 0;
}

static int sqlite3_log(struct ast_cdr *cdr)
{
	int res = 0;
	char *zErr = 0;
	char *sql_cmd;
	struct ast_channel dummy = { 0, };
	int count;

	{ /* Make it obvious that only sql_cmd should be used outside of this block */
		char *sql_tmp_cmd;
		char sql_insert_cmd[2048];
		sql_tmp_cmd = sqlite3_mprintf("INSERT INTO %q (%q) VALUES (%q)", table, columns, values);
		dummy.cdr = cdr;
		pbx_substitute_variables_helper(&dummy, sql_tmp_cmd, sql_insert_cmd, sizeof(sql_insert_cmd) - 1);
		sqlite3_free(sql_tmp_cmd);
		sql_cmd = alloca(strlen(sql_insert_cmd) * 2 + 1);
		do_escape(sql_cmd, sql_insert_cmd);
	}

	ast_mutex_lock(&lock);

	for (count = 0; count < 5; count++) {
		res = sqlite3_exec(db, sql_cmd, NULL, NULL, &zErr);
		if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
			break;
		usleep(200);
	}

	if (zErr) {
		ast_log(LOG_ERROR, "%s: %s. sentence: %s.\n", name, zErr, sql_cmd);
		sqlite3_free(zErr);
	}

	ast_mutex_unlock(&lock);

	return res;
}

static int unload_module(void)
{
	if (db)
		sqlite3_close(db);

	ast_cdr_unregister(name);

	return 0;
}

static int load_module(void)
{
	char *zErr;
	char fn[PATH_MAX];
	int res;
	char *sql_cmd;

	if (!load_config(0)) {
		res = ast_cdr_register(name, desc, sqlite3_log);
		if (res) {
			ast_log(LOG_ERROR, "%s: Unable to register custom SQLite3 CDR handling\n", name);
			return AST_MODULE_LOAD_DECLINE;
		}
	} else
		return AST_MODULE_LOAD_DECLINE;

	/* is the database there? */
	snprintf(fn, sizeof(fn), "%s/master.db", ast_config_AST_LOG_DIR);
	res = sqlite3_open(fn, &db);
	if (!db) {
		ast_log(LOG_ERROR, "%s: Could not open database %s.\n", name, fn);
		sqlite3_free(zErr);
		return AST_MODULE_LOAD_DECLINE;
	}

	/* is the table there? */
	sql_cmd = sqlite3_mprintf("SELECT COUNT(AcctId) FROM %q;", table);
	res = sqlite3_exec(db, sql_cmd, NULL, NULL, NULL);
	sqlite3_free(sql_cmd);
	if (res) {
		sql_cmd = sqlite3_mprintf("CREATE TABLE %q (AcctId INTEGER PRIMARY KEY,%q)", table, columns);
		res = sqlite3_exec(db, sql_cmd, NULL, NULL, &zErr);
		sqlite3_free(sql_cmd);
		if (zErr) {
			ast_log(LOG_WARNING, "%s: %s.\n", name, zErr);
			sqlite3_free(zErr);
			return 0;
		}

		if (res) {
			ast_log(LOG_ERROR, "%s: Unable to create table '%s': %s.\n", name, table, zErr);
			sqlite3_free(zErr);
			if (db)
				sqlite3_close(db);
			return AST_MODULE_LOAD_DECLINE;
		}
	}

	return 0;
}

static int reload(void)
{
	return load_config(1);
}

AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "SQLite3 Custom CDR Module",
	.load = load_module,
	.unload = unload_module,
	.reload = reload,
);