blob: 179eba83b0e78a8285efd6975a1c9f1dec02b90d [file] [log] [blame]
Harald Weltee72cf552016-04-28 07:18:49 +02001/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20#include <osmocom/core/utils.h>
21
Max00b37152017-02-20 11:09:27 +010022#include <stdbool.h>
Harald Weltee72cf552016-04-28 07:18:49 +020023#include <sqlite3.h>
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +020024#include <string.h>
Harald Weltee72cf552016-04-28 07:18:49 +020025
26#include "logging.h"
27#include "db.h"
28
29static const char *stmt_sql[] = {
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020030 [DB_STMT_SEL_BY_IMSI] = "SELECT id,imsi,msisdn,vlr_number,sgsn_number,sgsn_address,periodic_lu_tmr,periodic_rau_tau_tmr,nam_cs,nam_ps,lmsi,ms_purged_cs,ms_purged_ps FROM subscriber WHERE imsi = ?",
31 [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = ? WHERE id = ?",
32 [DB_STMT_UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = ? WHERE id = ?",
Neels Hofmeyr0cac0a02017-10-09 17:47:21 +020033 [DB_STMT_AUC_BY_IMSI] =
34 "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn, ind_bitlen"
35 " FROM subscriber"
36 " LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id"
37 " LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id"
38 " WHERE imsi = ?",
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020039 [DB_STMT_AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = ? WHERE subscriber_id = ?",
40 [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs=1 WHERE imsi = ?",
41 [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps=1 WHERE imsi = ?",
42 [DB_STMT_SET_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps=1 WHERE imsi = ?",
43 [DB_STMT_UNSET_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps=0 WHERE imsi = ?",
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020044 [DB_STMT_SUBSCR_CREATE] = "INSERT INTO subscriber (imsi) VALUES ($imsi)",
45 [DB_STMT_DEL_BY_ID] = "DELETE FROM subscriber WHERE id = $subscriber_id",
46 [DB_STMT_SET_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = $msisdn WHERE imsi = $imsi",
Harald Weltee72cf552016-04-28 07:18:49 +020047};
48
49static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
50{
51 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
52}
53
54static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
55{
56 switch (type) {
57 case 0:
58 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
59 break;
60 case 1:
Max58d4a842017-02-20 11:06:12 +010061 LOGP(DDB, LOGL_DEBUG, "%s\n", stmt);
Harald Weltee72cf552016-04-28 07:18:49 +020062 break;
63 case 2:
64 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
65 break;
66 default:
67 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
68 break;
69 }
70}
71
Max00b37152017-02-20 11:09:27 +010072/* remove bindings and reset statement to be re-executed */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +020073void db_remove_reset(sqlite3_stmt *stmt)
Max00b37152017-02-20 11:09:27 +010074{
Neels Hofmeyrd7d96972017-10-06 03:50:30 +020075 sqlite3_clear_bindings(stmt);
76 sqlite3_reset(stmt);
Max00b37152017-02-20 11:09:27 +010077}
78
Neels Hofmeyrf3144592017-10-06 03:40:52 +020079/** bind text arg and do proper cleanup in case of failure. If param_name is
80 * NULL, bind to the first parameter (useful for SQL statements that have only
81 * one parameter). */
82bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
Max00b37152017-02-20 11:09:27 +010083{
Neels Hofmeyrf3144592017-10-06 03:40:52 +020084 int rc;
85 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
86 if (idx < 1) {
87 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
88 param_name);
89 return false;
90 }
91 rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
Max00b37152017-02-20 11:09:27 +010092 if (rc != SQLITE_OK) {
Neels Hofmeyrf3144592017-10-06 03:40:52 +020093 LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
94 param_name ? param_name : "#1", rc);
Max00b37152017-02-20 11:09:27 +010095 db_remove_reset(stmt);
96 return false;
97 }
Max00b37152017-02-20 11:09:27 +010098 return true;
99}
100
Neels Hofmeyr28da26e2017-10-06 03:44:57 +0200101/** bind int arg and do proper cleanup in case of failure. If param_name is
102 * NULL, bind to the first parameter (useful for SQL statements that have only
103 * one parameter). */
104bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr)
105{
106 int rc;
107 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
108 if (idx < 1) {
109 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
110 param_name);
111 return false;
112 }
113 rc = sqlite3_bind_int(stmt, idx, nr);
114 if (rc != SQLITE_OK) {
115 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
116 param_name ? param_name : "#1", rc);
117 db_remove_reset(stmt);
118 return false;
119 }
120 return true;
121}
122
123/** bind int64 arg and do proper cleanup in case of failure. If param_name is
124 * NULL, bind to the first parameter (useful for SQL statements that have only
125 * one parameter). */
126bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr)
127{
128 int rc;
129 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
130 if (idx < 1) {
131 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
132 param_name);
133 return false;
134 }
135 rc = sqlite3_bind_int64(stmt, idx, nr);
136 if (rc != SQLITE_OK) {
137 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
138 param_name ? param_name : "#1", rc);
139 db_remove_reset(stmt);
140 return false;
141 }
142 return true;
143}
144
Harald Weltee72cf552016-04-28 07:18:49 +0200145void db_close(struct db_context *dbc)
146{
147 unsigned int i;
148
149 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
150 /* it is ok to call finalize on NULL */
151 sqlite3_finalize(dbc->stmt[i]);
152 }
153 sqlite3_close(dbc->db);
154 talloc_free(dbc);
155}
156
157struct db_context *db_open(void *ctx, const char *fname)
158{
159 struct db_context *dbc = talloc_zero(ctx, struct db_context);
160 unsigned int i;
161 int rc;
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200162 bool has_sqlite_config_sqllog = false;
Harald Weltee72cf552016-04-28 07:18:49 +0200163
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100164 LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
Harald Weltee72cf552016-04-28 07:18:49 +0200165 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
166 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
167
168 dbc->fname = talloc_strdup(dbc, fname);
169
170 for (i = 0; i < 0xfffff; i++) {
171 const char *o = sqlite3_compileoption_get(i);
172 if (!o)
173 break;
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200174 LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200175 if (!strcmp(o, "ENABLE_SQLLOG"))
176 has_sqlite_config_sqllog = true;
Harald Weltee72cf552016-04-28 07:18:49 +0200177 }
178
179 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
180 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200181 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200182
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200183 if (has_sqlite_config_sqllog) {
184 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
185 if (rc != SQLITE_OK)
186 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n");
187 } else
188 LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:"
189 " SQLite3 compiled without support for it\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200190
191 rc = sqlite3_open(dbc->fname, &dbc->db);
192 if (rc != SQLITE_OK) {
193 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
194 talloc_free(dbc);
195 return NULL;
196 }
197
198 /* enable extended result codes */
199 rc = sqlite3_extended_result_codes(dbc->db, 1);
200 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200201 LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200202
Harald Welteabd1a542016-05-03 18:50:41 +0200203 char *err_msg;
204 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
205 if (rc != SQLITE_OK)
206 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
207 err_msg);
208
Harald Weltee72cf552016-04-28 07:18:49 +0200209 /* prepare all SQL statements */
210 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
211 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
212 &dbc->stmt[i], NULL);
213 if (rc != SQLITE_OK) {
214 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
215 goto out_free;
216 }
217 }
218
219 return dbc;
220out_free:
221 db_close(dbc);
222 return NULL;
223}