blob: 4b0577fb1512fca310867e48d5cbe95fdb26b157 [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"
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +020028#include "db_bootstrap.h"
Harald Weltee72cf552016-04-28 07:18:49 +020029
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020030#define SEL_COLUMNS \
31 "id," \
32 "imsi," \
33 "msisdn," \
34 "vlr_number," \
35 "sgsn_number," \
36 "sgsn_address," \
37 "periodic_lu_tmr," \
38 "periodic_rau_tau_tmr," \
39 "nam_cs," \
40 "nam_ps," \
41 "lmsi," \
42 "ms_purged_cs," \
43 "ms_purged_ps"
44
Harald Weltee72cf552016-04-28 07:18:49 +020045static const char *stmt_sql[] = {
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020046 [DB_STMT_SEL_BY_IMSI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imsi = ?",
47 [DB_STMT_SEL_BY_MSISDN] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE msisdn = ?",
48 [DB_STMT_SEL_BY_ID] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE id = ?",
Neels Hofmeyrdd783052017-10-09 17:36:08 +020049 [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = $number WHERE id = $subscriber_id",
50 [DB_STMT_UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = $number WHERE id = $subscriber_id",
Neels Hofmeyr0cac0a02017-10-09 17:47:21 +020051 [DB_STMT_AUC_BY_IMSI] =
52 "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn, ind_bitlen"
53 " FROM subscriber"
54 " LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id"
55 " LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id"
Neels Hofmeyrc5122f22017-10-09 23:12:57 +020056 " WHERE imsi = $imsi",
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020057 [DB_STMT_AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = $sqn WHERE subscriber_id = $subscriber_id",
Neels Hofmeyre50121e2017-10-09 17:48:51 +020058 [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs = $val WHERE imsi = $imsi",
59 [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps = $val WHERE imsi = $imsi",
Neels Hofmeyre8ccd502017-10-06 04:10:06 +020060 [DB_STMT_UPD_NAM_CS_BY_IMSI] = "UPDATE subscriber SET nam_cs = $val WHERE imsi = $imsi",
61 [DB_STMT_UPD_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps = $val WHERE imsi = $imsi",
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020062 [DB_STMT_SUBSCR_CREATE] = "INSERT INTO subscriber (imsi) VALUES ($imsi)",
63 [DB_STMT_DEL_BY_ID] = "DELETE FROM subscriber WHERE id = $subscriber_id",
64 [DB_STMT_SET_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = $msisdn WHERE imsi = $imsi",
Neels Hofmeyr1332a172017-10-10 02:25:00 +020065 [DB_STMT_AUC_2G_INSERT] =
66 "INSERT INTO auc_2g (subscriber_id, algo_id_2g, ki)"
67 " VALUES($subscriber_id, $algo_id_2g, $ki)",
68 [DB_STMT_AUC_2G_DELETE] = "DELETE FROM auc_2g WHERE subscriber_id = $subscriber_id",
69 [DB_STMT_AUC_3G_INSERT] =
70 "INSERT INTO auc_3g (subscriber_id, algo_id_3g, k, op, opc, ind_bitlen)"
71 " VALUES($subscriber_id, $algo_id_3g, $k, $op, $opc, $ind_bitlen)",
72 [DB_STMT_AUC_3G_DELETE] = "DELETE FROM auc_3g WHERE subscriber_id = $subscriber_id",
Harald Weltee72cf552016-04-28 07:18:49 +020073};
74
75static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
76{
77 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
78}
79
80static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
81{
82 switch (type) {
83 case 0:
84 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
85 break;
86 case 1:
Max58d4a842017-02-20 11:06:12 +010087 LOGP(DDB, LOGL_DEBUG, "%s\n", stmt);
Harald Weltee72cf552016-04-28 07:18:49 +020088 break;
89 case 2:
90 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
91 break;
92 default:
93 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
94 break;
95 }
96}
97
Max00b37152017-02-20 11:09:27 +010098/* remove bindings and reset statement to be re-executed */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +020099void db_remove_reset(sqlite3_stmt *stmt)
Max00b37152017-02-20 11:09:27 +0100100{
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200101 sqlite3_clear_bindings(stmt);
Neels Hofmeyr4f3841c2017-11-07 13:24:44 +0100102 /* sqlite3_reset() just repeats an error code already evaluated during sqlite3_step(). */
103 /* coverity[CHECKED_RETURN] */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200104 sqlite3_reset(stmt);
Max00b37152017-02-20 11:09:27 +0100105}
106
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200107/** bind text arg and do proper cleanup in case of failure. If param_name is
108 * NULL, bind to the first parameter (useful for SQL statements that have only
109 * one parameter). */
110bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
Max00b37152017-02-20 11:09:27 +0100111{
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200112 int rc;
113 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
114 if (idx < 1) {
115 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
116 param_name);
117 return false;
118 }
119 rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
Max00b37152017-02-20 11:09:27 +0100120 if (rc != SQLITE_OK) {
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200121 LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
122 param_name ? param_name : "#1", rc);
Max00b37152017-02-20 11:09:27 +0100123 db_remove_reset(stmt);
124 return false;
125 }
Max00b37152017-02-20 11:09:27 +0100126 return true;
127}
128
Neels Hofmeyr28da26e2017-10-06 03:44:57 +0200129/** bind int arg and do proper cleanup in case of failure. If param_name is
130 * NULL, bind to the first parameter (useful for SQL statements that have only
131 * one parameter). */
132bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr)
133{
134 int rc;
135 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
136 if (idx < 1) {
137 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
138 param_name);
139 return false;
140 }
141 rc = sqlite3_bind_int(stmt, idx, nr);
142 if (rc != SQLITE_OK) {
143 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
144 param_name ? param_name : "#1", rc);
145 db_remove_reset(stmt);
146 return false;
147 }
148 return true;
149}
150
151/** bind int64 arg and do proper cleanup in case of failure. If param_name is
152 * NULL, bind to the first parameter (useful for SQL statements that have only
153 * one parameter). */
154bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr)
155{
156 int rc;
157 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
158 if (idx < 1) {
159 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
160 param_name);
161 return false;
162 }
163 rc = sqlite3_bind_int64(stmt, idx, nr);
164 if (rc != SQLITE_OK) {
165 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
166 param_name ? param_name : "#1", rc);
167 db_remove_reset(stmt);
168 return false;
169 }
170 return true;
171}
172
Harald Weltee72cf552016-04-28 07:18:49 +0200173void db_close(struct db_context *dbc)
174{
175 unsigned int i;
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700176 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200177
178 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
179 /* it is ok to call finalize on NULL */
180 sqlite3_finalize(dbc->stmt[i]);
181 }
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700182
183 /* Ask sqlite3 to close DB */
184 rc = sqlite3_close(dbc->db);
185 if (rc != SQLITE_OK) { /* Make sure it's actually closed! */
186 LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n",
187 rc, sqlite3_errmsg(dbc->db));
188 }
189
Harald Weltee72cf552016-04-28 07:18:49 +0200190 talloc_free(dbc);
191}
192
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200193static int db_bootstrap(struct db_context *dbc)
194{
195 int i;
196 for (i = 0; i < ARRAY_SIZE(stmt_bootstrap_sql); i++) {
197 int rc;
198 sqlite3_stmt *stmt;
199
200 rc = sqlite3_prepare_v2(dbc->db, stmt_bootstrap_sql[i], -1,
201 &stmt, NULL);
202 if (rc != SQLITE_OK) {
203 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n",
204 stmt_bootstrap_sql[i]);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700205 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200206 }
207
208 /* execute the statement */
209 rc = sqlite3_step(stmt);
210 db_remove_reset(stmt);
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700211 sqlite3_finalize(stmt);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200212 if (rc != SQLITE_DONE) {
213 LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database: SQL error: (%d) %s,"
214 " during stmt '%s'",
215 rc, sqlite3_errmsg(dbc->db),
216 stmt_bootstrap_sql[i]);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700217 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200218 }
219 }
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700220 return SQLITE_OK;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200221}
222
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100223struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite_logging)
Harald Weltee72cf552016-04-28 07:18:49 +0200224{
225 struct db_context *dbc = talloc_zero(ctx, struct db_context);
226 unsigned int i;
227 int rc;
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200228 bool has_sqlite_config_sqllog = false;
Harald Weltee72cf552016-04-28 07:18:49 +0200229
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100230 LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
Harald Weltee72cf552016-04-28 07:18:49 +0200231 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
232 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
233
234 dbc->fname = talloc_strdup(dbc, fname);
235
236 for (i = 0; i < 0xfffff; i++) {
237 const char *o = sqlite3_compileoption_get(i);
238 if (!o)
239 break;
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200240 LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200241 if (!strcmp(o, "ENABLE_SQLLOG"))
242 has_sqlite_config_sqllog = true;
Harald Weltee72cf552016-04-28 07:18:49 +0200243 }
244
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100245 if (enable_sqlite_logging) {
246 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
247 if (rc != SQLITE_OK)
248 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
249 }
Harald Weltee72cf552016-04-28 07:18:49 +0200250
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200251 if (has_sqlite_config_sqllog) {
252 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
253 if (rc != SQLITE_OK)
254 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n");
255 } else
256 LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:"
257 " SQLite3 compiled without support for it\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200258
259 rc = sqlite3_open(dbc->fname, &dbc->db);
260 if (rc != SQLITE_OK) {
261 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
262 talloc_free(dbc);
263 return NULL;
264 }
265
266 /* enable extended result codes */
267 rc = sqlite3_extended_result_codes(dbc->db, 1);
268 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200269 LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200270
Harald Welteabd1a542016-05-03 18:50:41 +0200271 char *err_msg;
272 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
273 if (rc != SQLITE_OK)
274 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
275 err_msg);
276
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700277 rc = db_bootstrap(dbc);
278 if (rc != SQLITE_OK) {
279 LOGP(DDB, LOGL_ERROR, "Failed to bootstrap DB: (rc=%d) %s\n",
280 rc, sqlite3_errmsg(dbc->db));
281 goto out_free;
282 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200283
Harald Weltee72cf552016-04-28 07:18:49 +0200284 /* prepare all SQL statements */
285 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
286 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
287 &dbc->stmt[i], NULL);
288 if (rc != SQLITE_OK) {
289 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
290 goto out_free;
291 }
292 }
293
294 return dbc;
295out_free:
296 db_close(dbc);
297 return NULL;
298}