blob: 992dbad8b0bfa913b2af1c5450d42745c486333d [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
Neels Hofmeyr2f758032019-11-20 00:37:07 +010026#include <osmocom/hlr/logging.h>
27#include <osmocom/hlr/db.h>
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +020028#include "db_bootstrap.h"
Harald Weltee72cf552016-04-28 07:18:49 +020029
Stefan Sperling638ba8c2018-12-04 15:07:29 +010030/* This constant is currently duplicated in sql/hlr.sql and must be kept in sync! */
Neels Hofmeyra8045da2019-10-31 01:19:44 +010031#define CURRENT_SCHEMA_VERSION 3
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +010032
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020033#define SEL_COLUMNS \
34 "id," \
35 "imsi," \
36 "msisdn," \
Oliver Smith81db3892019-01-09 12:03:51 +010037 "imei," \
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020038 "vlr_number," \
39 "sgsn_number," \
40 "sgsn_address," \
41 "periodic_lu_tmr," \
42 "periodic_rau_tau_tmr," \
43 "nam_cs," \
44 "nam_ps," \
45 "lmsi," \
46 "ms_purged_cs," \
Stefan Sperling638ba8c2018-12-04 15:07:29 +010047 "ms_purged_ps," \
48 "last_lu_seen"
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020049
Harald Weltee72cf552016-04-28 07:18:49 +020050static const char *stmt_sql[] = {
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020051 [DB_STMT_SEL_BY_IMSI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imsi = ?",
52 [DB_STMT_SEL_BY_MSISDN] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE msisdn = ?",
53 [DB_STMT_SEL_BY_ID] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE id = ?",
Oliver Smith81db3892019-01-09 12:03:51 +010054 [DB_STMT_SEL_BY_IMEI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imei = ?",
Neels Hofmeyrdd783052017-10-09 17:36:08 +020055 [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = $number WHERE id = $subscriber_id",
56 [DB_STMT_UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = $number WHERE id = $subscriber_id",
Oliver Smith81db3892019-01-09 12:03:51 +010057 [DB_STMT_UPD_IMEI_BY_IMSI] = "UPDATE subscriber SET imei = $imei WHERE imsi = $imsi",
Neels Hofmeyr0cac0a02017-10-09 17:47:21 +020058 [DB_STMT_AUC_BY_IMSI] =
59 "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn, ind_bitlen"
60 " FROM subscriber"
61 " LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id"
62 " LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id"
Neels Hofmeyrc5122f22017-10-09 23:12:57 +020063 " WHERE imsi = $imsi",
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020064 [DB_STMT_AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = $sqn WHERE subscriber_id = $subscriber_id",
Neels Hofmeyre50121e2017-10-09 17:48:51 +020065 [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs = $val WHERE imsi = $imsi",
66 [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps = $val WHERE imsi = $imsi",
Neels Hofmeyre8ccd502017-10-06 04:10:06 +020067 [DB_STMT_UPD_NAM_CS_BY_IMSI] = "UPDATE subscriber SET nam_cs = $val WHERE imsi = $imsi",
68 [DB_STMT_UPD_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps = $val WHERE imsi = $imsi",
Oliver Smithcd2af5e2019-03-06 13:17:39 +010069 [DB_STMT_SUBSCR_CREATE] = "INSERT INTO subscriber (imsi, nam_cs, nam_ps) VALUES ($imsi, $nam_cs, $nam_ps)",
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020070 [DB_STMT_DEL_BY_ID] = "DELETE FROM subscriber WHERE id = $subscriber_id",
71 [DB_STMT_SET_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = $msisdn WHERE imsi = $imsi",
Neels Hofmeyra820ea12018-12-02 19:46:46 +010072 [DB_STMT_DELETE_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = NULL WHERE imsi = $imsi",
Neels Hofmeyr1332a172017-10-10 02:25:00 +020073 [DB_STMT_AUC_2G_INSERT] =
74 "INSERT INTO auc_2g (subscriber_id, algo_id_2g, ki)"
75 " VALUES($subscriber_id, $algo_id_2g, $ki)",
76 [DB_STMT_AUC_2G_DELETE] = "DELETE FROM auc_2g WHERE subscriber_id = $subscriber_id",
77 [DB_STMT_AUC_3G_INSERT] =
78 "INSERT INTO auc_3g (subscriber_id, algo_id_3g, k, op, opc, ind_bitlen)"
79 " VALUES($subscriber_id, $algo_id_3g, $k, $op, $opc, $ind_bitlen)",
80 [DB_STMT_AUC_3G_DELETE] = "DELETE FROM auc_3g WHERE subscriber_id = $subscriber_id",
Stefan Sperling638ba8c2018-12-04 15:07:29 +010081 [DB_STMT_SET_LAST_LU_SEEN] = "UPDATE subscriber SET last_lu_seen = datetime($val, 'unixepoch') WHERE id = $subscriber_id",
Oliver Smith6b73fd92019-03-06 13:49:05 +010082 [DB_STMT_EXISTS_BY_IMSI] = "SELECT 1 FROM subscriber WHERE imsi = $imsi",
Vadim Yanitskiyc13599d2019-03-30 17:03:42 +070083 [DB_STMT_EXISTS_BY_MSISDN] = "SELECT 1 FROM subscriber WHERE msisdn = $msisdn",
Harald Weltee72cf552016-04-28 07:18:49 +020084};
85
86static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
87{
88 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
89}
90
91static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
92{
93 switch (type) {
94 case 0:
95 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
96 break;
97 case 1:
Max58d4a842017-02-20 11:06:12 +010098 LOGP(DDB, LOGL_DEBUG, "%s\n", stmt);
Harald Weltee72cf552016-04-28 07:18:49 +020099 break;
100 case 2:
101 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
102 break;
103 default:
104 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
105 break;
106 }
107}
108
Max00b37152017-02-20 11:09:27 +0100109/* remove bindings and reset statement to be re-executed */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200110void db_remove_reset(sqlite3_stmt *stmt)
Max00b37152017-02-20 11:09:27 +0100111{
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200112 sqlite3_clear_bindings(stmt);
Neels Hofmeyr4f3841c2017-11-07 13:24:44 +0100113 /* sqlite3_reset() just repeats an error code already evaluated during sqlite3_step(). */
114 /* coverity[CHECKED_RETURN] */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200115 sqlite3_reset(stmt);
Max00b37152017-02-20 11:09:27 +0100116}
117
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200118/** bind text arg and do proper cleanup in case of failure. If param_name is
119 * NULL, bind to the first parameter (useful for SQL statements that have only
120 * one parameter). */
121bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
Max00b37152017-02-20 11:09:27 +0100122{
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200123 int rc;
124 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
125 if (idx < 1) {
126 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
127 param_name);
128 return false;
129 }
130 rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
Max00b37152017-02-20 11:09:27 +0100131 if (rc != SQLITE_OK) {
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200132 LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
133 param_name ? param_name : "#1", rc);
Max00b37152017-02-20 11:09:27 +0100134 db_remove_reset(stmt);
135 return false;
136 }
Max00b37152017-02-20 11:09:27 +0100137 return true;
138}
139
Neels Hofmeyr28da26e2017-10-06 03:44:57 +0200140/** bind int arg and do proper cleanup in case of failure. If param_name is
141 * NULL, bind to the first parameter (useful for SQL statements that have only
142 * one parameter). */
143bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr)
144{
145 int rc;
146 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
147 if (idx < 1) {
148 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
149 param_name);
150 return false;
151 }
152 rc = sqlite3_bind_int(stmt, idx, nr);
153 if (rc != SQLITE_OK) {
154 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
155 param_name ? param_name : "#1", rc);
156 db_remove_reset(stmt);
157 return false;
158 }
159 return true;
160}
161
162/** bind int64 arg and do proper cleanup in case of failure. If param_name is
163 * NULL, bind to the first parameter (useful for SQL statements that have only
164 * one parameter). */
165bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr)
166{
167 int rc;
168 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
169 if (idx < 1) {
170 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
171 param_name);
172 return false;
173 }
174 rc = sqlite3_bind_int64(stmt, idx, nr);
175 if (rc != SQLITE_OK) {
176 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
177 param_name ? param_name : "#1", rc);
178 db_remove_reset(stmt);
179 return false;
180 }
181 return true;
182}
183
Harald Weltee72cf552016-04-28 07:18:49 +0200184void db_close(struct db_context *dbc)
185{
186 unsigned int i;
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700187 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200188
189 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
190 /* it is ok to call finalize on NULL */
191 sqlite3_finalize(dbc->stmt[i]);
192 }
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700193
194 /* Ask sqlite3 to close DB */
195 rc = sqlite3_close(dbc->db);
196 if (rc != SQLITE_OK) { /* Make sure it's actually closed! */
197 LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n",
198 rc, sqlite3_errmsg(dbc->db));
199 }
200
Harald Weltee72cf552016-04-28 07:18:49 +0200201 talloc_free(dbc);
202}
203
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100204static int db_run_statements(struct db_context *dbc, const char **statements, size_t statements_count)
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200205{
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100206 int rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200207 int i;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100208 for (i = 0; i < statements_count; i++) {
209 const char *stmt_str = statements[i];
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200210 sqlite3_stmt *stmt;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100211
212 rc = sqlite3_prepare_v2(dbc->db, stmt_str, -1, &stmt, NULL);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200213 if (rc != SQLITE_OK) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100214 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_str);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700215 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200216 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200217 rc = sqlite3_step(stmt);
218 db_remove_reset(stmt);
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700219 sqlite3_finalize(stmt);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200220 if (rc != SQLITE_DONE) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100221 LOGP(DDB, LOGL_ERROR, "SQL error: (%d) %s, during stmt '%s'",
222 rc, sqlite3_errmsg(dbc->db), stmt_str);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700223 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200224 }
225 }
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100226 return rc;
227}
228
229static int db_bootstrap(struct db_context *dbc)
230{
231 int rc = db_run_statements(dbc, stmt_bootstrap_sql, ARRAY_SIZE(stmt_bootstrap_sql));
232 if (rc != SQLITE_DONE) {
233 LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database\n");
234 return rc;
235 }
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700236 return SQLITE_OK;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200237}
238
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100239/* https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema */
240static bool db_table_exists(struct db_context *dbc, const char *table_name)
241{
242 const char *table_exists_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=?";
243 sqlite3_stmt *stmt;
244 int rc;
245
246 rc = sqlite3_prepare_v2(dbc->db, table_exists_sql, -1, &stmt, NULL);
247 if (rc != SQLITE_OK) {
248 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", table_exists_sql);
249 return false;
250 }
251
252 if (!db_bind_text(stmt, NULL, table_name))
253 return false;
254
255 rc = sqlite3_step(stmt);
256 db_remove_reset(stmt);
257 sqlite3_finalize(stmt);
258 return (rc == SQLITE_ROW);
259}
260
261/* Indicate whether the database is initialized with tables for schema version 0.
262 * We only check for the 'subscriber' table here because Neels said so. */
263static bool db_is_bootstrapped_v0(struct db_context *dbc)
264{
265 if (!db_table_exists(dbc, "subscriber")) {
266 LOGP(DDB, LOGL_DEBUG, "Table 'subscriber' not found in database '%s'\n", dbc->fname);
267 return false;
268 }
269
270 return true;
271}
272
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100273static int
274db_upgrade_v1(struct db_context *dbc)
275{
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100276 int rc;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100277 const char *statements[] = {
278 "ALTER TABLE subscriber ADD COLUMN last_lu_seen TIMESTAMP default NULL",
279 "PRAGMA user_version = 1",
280 };
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100281
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100282 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100283 if (rc != SQLITE_DONE) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100284 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 1\n");
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100285 return rc;
286 }
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100287 return rc;
288}
289
Oliver Smith81db3892019-01-09 12:03:51 +0100290static int db_upgrade_v2(struct db_context *dbc)
291{
Oliver Smith81db3892019-01-09 12:03:51 +0100292 int rc;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100293 const char *statements[] = {
294 "ALTER TABLE subscriber ADD COLUMN imei VARCHAR(14)",
295 "PRAGMA user_version = 2",
296 };
Oliver Smith81db3892019-01-09 12:03:51 +0100297
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100298 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
Oliver Smith81db3892019-01-09 12:03:51 +0100299 if (rc != SQLITE_DONE) {
Neels Hofmeyrf5459de2019-10-31 01:29:59 +0100300 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 2\n");
Oliver Smith81db3892019-01-09 12:03:51 +0100301 return rc;
302 }
Oliver Smith81db3892019-01-09 12:03:51 +0100303 return rc;
304}
305
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100306static int db_upgrade_v3(struct db_context *dbc)
307{
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100308 int rc;
309
310 /* A newer SQLite version would allow simply 'ATLER TABLE subscriber RENAME COLUMN hlr_number TO msc_number'.
311 * This is a really expensive workaround for that in order to cover earlier SQLite versions as well:
312 * Create a new table with the new column name and copy the data over (https://www.sqlite.org/faq.html#q11).
313 */
314#define SUBSCR_V3_CREATE \
315"(\n" \
316"-- OsmoHLR's DB scheme is modelled roughly after TS 23.008 version 13.3.0\n" \
317" id INTEGER PRIMARY KEY,\n" \
318" -- Chapter 2.1.1.1\n" \
319" imsi VARCHAR(15) UNIQUE NOT NULL,\n" \
320" -- Chapter 2.1.2\n" \
321" msisdn VARCHAR(15) UNIQUE,\n" \
322" -- Chapter 2.2.3: Most recent / current IMEISV\n" \
323" imeisv VARCHAR,\n" \
324" -- Chapter 2.1.9: Most recent / current IMEI\n" \
325" imei VARCHAR(14),\n" \
326" -- Chapter 2.4.5\n" \
327" vlr_number VARCHAR(15),\n" \
328" -- Chapter 2.4.6\n" \
329" msc_number VARCHAR(15),\n" \
330" -- Chapter 2.4.8.1\n" \
331" sgsn_number VARCHAR(15),\n" \
332" -- Chapter 2.13.10\n" \
333" sgsn_address VARCHAR,\n" \
334" -- Chapter 2.4.8.2\n" \
335" ggsn_number VARCHAR(15),\n" \
336" -- Chapter 2.4.9.2\n" \
337" gmlc_number VARCHAR(15),\n" \
338" -- Chapter 2.4.23\n" \
339" smsc_number VARCHAR(15),\n" \
340" -- Chapter 2.4.24\n" \
341" periodic_lu_tmr INTEGER,\n" \
342" -- Chapter 2.13.115\n" \
343" periodic_rau_tau_tmr INTEGER,\n" \
344" -- Chapter 2.1.1.2: network access mode\n" \
345" nam_cs BOOLEAN NOT NULL DEFAULT 1,\n" \
346" nam_ps BOOLEAN NOT NULL DEFAULT 1,\n" \
347" -- Chapter 2.1.8\n" \
348" lmsi INTEGER,\n" \
349 \
350" -- The below purged flags might not even be stored non-volatile,\n" \
351" -- refer to TS 23.012 Chapter 3.6.1.4\n" \
352" -- Chapter 2.7.5\n" \
353" ms_purged_cs BOOLEAN NOT NULL DEFAULT 0,\n" \
354" -- Chapter 2.7.6\n" \
355" ms_purged_ps BOOLEAN NOT NULL DEFAULT 0,\n" \
356 \
357" -- Timestamp of last location update seen from subscriber\n" \
358" -- The value is a string which encodes a UTC timestamp in granularity of seconds.\n" \
359" last_lu_seen TIMESTAMP default NULL\n" \
360")\n"
361
362#define SUBSCR_V2_COLUMN_NAMES \
363 "id," \
364 "imsi," \
365 "msisdn," \
366 "imeisv," \
367 "imei," \
368 "vlr_number," \
369 "hlr_number," \
370 "sgsn_number," \
371 "sgsn_address," \
372 "ggsn_number," \
373 "gmlc_number," \
374 "smsc_number," \
375 "periodic_lu_tmr," \
376 "periodic_rau_tau_tmr," \
377 "nam_cs," \
378 "nam_ps," \
379 "lmsi," \
380 "ms_purged_cs," \
381 "ms_purged_ps," \
382 "last_lu_seen"
383
384#define SUBSCR_V3_COLUMN_NAMES \
385 "id," \
386 "imsi," \
387 "msisdn," \
388 "imeisv," \
389 "imei," \
390 "vlr_number," \
391 "msc_number," \
392 "sgsn_number," \
393 "sgsn_address," \
394 "ggsn_number," \
395 "gmlc_number," \
396 "smsc_number," \
397 "periodic_lu_tmr," \
398 "periodic_rau_tau_tmr," \
399 "nam_cs," \
400 "nam_ps," \
401 "lmsi," \
402 "ms_purged_cs," \
403 "ms_purged_ps," \
404 "last_lu_seen"
405
406 const char *statements[] = {
407 "BEGIN TRANSACTION",
408 "CREATE TEMPORARY TABLE subscriber_backup" SUBSCR_V3_CREATE,
409 "INSERT INTO subscriber_backup SELECT " SUBSCR_V2_COLUMN_NAMES " FROM subscriber",
410 "DROP TABLE subscriber",
411 "CREATE TABLE subscriber" SUBSCR_V3_CREATE,
412 "INSERT INTO subscriber SELECT " SUBSCR_V3_COLUMN_NAMES " FROM subscriber_backup",
413 "DROP TABLE subscriber_backup",
414 "COMMIT",
415 "PRAGMA user_version = 3",
416 };
417
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100418 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
419 if (rc != SQLITE_DONE) {
420 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 3\n");
421 return rc;
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100422 }
423 return rc;
424}
425
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100426typedef int (*db_upgrade_func_t)(struct db_context *dbc);
427static db_upgrade_func_t db_upgrade_path[] = {
428 db_upgrade_v1,
429 db_upgrade_v2,
430 db_upgrade_v3,
431};
432
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100433static int db_get_user_version(struct db_context *dbc)
434{
435 const char *user_version_sql = "PRAGMA user_version";
436 sqlite3_stmt *stmt;
437 int version, rc;
438
439 rc = sqlite3_prepare_v2(dbc->db, user_version_sql, -1, &stmt, NULL);
440 if (rc != SQLITE_OK) {
441 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", user_version_sql);
442 return -1;
443 }
444 rc = sqlite3_step(stmt);
445 if (rc == SQLITE_ROW) {
446 version = sqlite3_column_int(stmt, 0);
447 } else {
448 LOGP(DDB, LOGL_ERROR, "SQL statement '%s' failed: %d\n", user_version_sql, rc);
449 version = -1;
450 }
451
452 db_remove_reset(stmt);
453 sqlite3_finalize(stmt);
454 return version;
455}
456
457struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite_logging, bool allow_upgrade)
Harald Weltee72cf552016-04-28 07:18:49 +0200458{
459 struct db_context *dbc = talloc_zero(ctx, struct db_context);
460 unsigned int i;
461 int rc;
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200462 bool has_sqlite_config_sqllog = false;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100463 int version;
Harald Weltee72cf552016-04-28 07:18:49 +0200464
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100465 LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
Harald Weltee72cf552016-04-28 07:18:49 +0200466 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
467 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
468
Vadim Yanitskiyfbd736e2018-07-31 22:40:30 +0700469#ifdef SQLITE_USE_TALLOC
470 /* Configure SQLite3 to use talloc memory allocator */
471 rc = db_sqlite3_use_talloc(ctx);
472 if (rc == SQLITE_OK) {
473 LOGP(DDB, LOGL_NOTICE, "SQLite3 is configured to use talloc\n");
474 } else {
475 LOGP(DDB, LOGL_ERROR, "Failed to configure SQLite3 "
476 "to use talloc, using default memory allocator\n");
477 }
478#endif
479
Harald Weltee72cf552016-04-28 07:18:49 +0200480 dbc->fname = talloc_strdup(dbc, fname);
481
482 for (i = 0; i < 0xfffff; i++) {
483 const char *o = sqlite3_compileoption_get(i);
484 if (!o)
485 break;
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200486 LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200487 if (!strcmp(o, "ENABLE_SQLLOG"))
488 has_sqlite_config_sqllog = true;
Harald Weltee72cf552016-04-28 07:18:49 +0200489 }
490
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100491 if (enable_sqlite_logging) {
492 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
493 if (rc != SQLITE_OK)
494 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
495 }
Harald Weltee72cf552016-04-28 07:18:49 +0200496
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200497 if (has_sqlite_config_sqllog) {
498 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
499 if (rc != SQLITE_OK)
500 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n");
501 } else
502 LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:"
503 " SQLite3 compiled without support for it\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200504
505 rc = sqlite3_open(dbc->fname, &dbc->db);
506 if (rc != SQLITE_OK) {
507 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
508 talloc_free(dbc);
509 return NULL;
510 }
511
512 /* enable extended result codes */
513 rc = sqlite3_extended_result_codes(dbc->db, 1);
514 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200515 LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200516
Harald Welteabd1a542016-05-03 18:50:41 +0200517 char *err_msg;
518 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
519 if (rc != SQLITE_OK)
520 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
521 err_msg);
522
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100523 version = db_get_user_version(dbc);
524 if (version < 0) {
525 LOGP(DDB, LOGL_ERROR, "Unable to read user version number from database '%s'\n", dbc->fname);
526 goto out_free;
527 }
528
529 /* An empty database will always report version zero. */
530 if (version == 0 && !db_is_bootstrapped_v0(dbc)) {
531 LOGP(DDB, LOGL_NOTICE, "Missing database tables detected; Bootstrapping database '%s'\n", dbc->fname);
532 rc = db_bootstrap(dbc);
533 if (rc != SQLITE_OK) {
534 LOGP(DDB, LOGL_ERROR, "Failed to bootstrap DB: (rc=%d) %s\n",
535 rc, sqlite3_errmsg(dbc->db));
536 goto out_free;
537 }
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100538 version = CURRENT_SCHEMA_VERSION;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100539 }
540
541 LOGP(DDB, LOGL_NOTICE, "Database '%s' has HLR DB schema version %d\n", dbc->fname, version);
542
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100543 for (; allow_upgrade && (version < ARRAY_SIZE(db_upgrade_path)); version++) {
544 db_upgrade_func_t upgrade_func = db_upgrade_path[version];
545 rc = upgrade_func(dbc);
546 if (rc != SQLITE_DONE) {
547 LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version %d: (rc=%d) %s\n",
548 version+1, rc, sqlite3_errmsg(dbc->db));
549 goto out_free;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100550 }
551 LOGP(DDB, LOGL_NOTICE, "Database '%s' has been upgraded to HLR DB schema version %d\n",
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100552 dbc->fname, version+1);
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100553 }
554
555 if (version != CURRENT_SCHEMA_VERSION) {
556 if (version < CURRENT_SCHEMA_VERSION) {
557 LOGP(DDB, LOGL_NOTICE, "HLR DB schema version %d is outdated\n", version);
558 if (!allow_upgrade) {
559 LOGP(DDB, LOGL_ERROR, "Not upgrading HLR database to schema version %d; "
560 "use the --db-upgrade option to allow HLR database upgrades\n",
561 CURRENT_SCHEMA_VERSION);
562 }
563 } else
564 LOGP(DDB, LOGL_ERROR, "HLR DB schema version %d is unknown\n", version);
565
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700566 goto out_free;
567 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200568
Harald Weltee72cf552016-04-28 07:18:49 +0200569 /* prepare all SQL statements */
570 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
571 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
572 &dbc->stmt[i], NULL);
573 if (rc != SQLITE_OK) {
574 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
575 goto out_free;
576 }
577 }
578
579 return dbc;
580out_free:
581 db_close(dbc);
582 return NULL;
583}