blob: d2564e6d711203b3938e9385662aff3caa07fb8c [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
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 Hofmeyr7750d2c2017-10-24 23:26:27 +0200204static int db_bootstrap(struct db_context *dbc)
205{
206 int i;
207 for (i = 0; i < ARRAY_SIZE(stmt_bootstrap_sql); i++) {
208 int rc;
209 sqlite3_stmt *stmt;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100210 rc = sqlite3_prepare_v2(dbc->db, stmt_bootstrap_sql[i], -1, &stmt, NULL);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200211 if (rc != SQLITE_OK) {
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100212 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_bootstrap_sql[i]);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700213 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200214 }
215
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200216 rc = sqlite3_step(stmt);
217 db_remove_reset(stmt);
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700218 sqlite3_finalize(stmt);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200219 if (rc != SQLITE_DONE) {
220 LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database: SQL error: (%d) %s,"
221 " during stmt '%s'",
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100222 rc, sqlite3_errmsg(dbc->db), stmt_bootstrap_sql[i]);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700223 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200224 }
225 }
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700226 return SQLITE_OK;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200227}
228
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100229/* https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema */
230static bool db_table_exists(struct db_context *dbc, const char *table_name)
231{
232 const char *table_exists_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=?";
233 sqlite3_stmt *stmt;
234 int rc;
235
236 rc = sqlite3_prepare_v2(dbc->db, table_exists_sql, -1, &stmt, NULL);
237 if (rc != SQLITE_OK) {
238 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", table_exists_sql);
239 return false;
240 }
241
242 if (!db_bind_text(stmt, NULL, table_name))
243 return false;
244
245 rc = sqlite3_step(stmt);
246 db_remove_reset(stmt);
247 sqlite3_finalize(stmt);
248 return (rc == SQLITE_ROW);
249}
250
251/* Indicate whether the database is initialized with tables for schema version 0.
252 * We only check for the 'subscriber' table here because Neels said so. */
253static bool db_is_bootstrapped_v0(struct db_context *dbc)
254{
255 if (!db_table_exists(dbc, "subscriber")) {
256 LOGP(DDB, LOGL_DEBUG, "Table 'subscriber' not found in database '%s'\n", dbc->fname);
257 return false;
258 }
259
260 return true;
261}
262
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100263static int
264db_upgrade_v1(struct db_context *dbc)
265{
266 sqlite3_stmt *stmt;
267 int rc;
268 const char *update_stmt_sql = "ALTER TABLE subscriber ADD COLUMN last_lu_seen TIMESTAMP default NULL";
269 const char *set_schema_version_sql = "PRAGMA user_version = 1";
270
271 rc = sqlite3_prepare_v2(dbc->db, update_stmt_sql, -1, &stmt, NULL);
272 if (rc != SQLITE_OK) {
273 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", update_stmt_sql);
274 return rc;
275 }
276 rc = sqlite3_step(stmt);
277 db_remove_reset(stmt);
278 sqlite3_finalize(stmt);
279 if (rc != SQLITE_DONE) {
280 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1);
281 return rc;
282 }
283
284 rc = sqlite3_prepare_v2(dbc->db, set_schema_version_sql, -1, &stmt, NULL);
285 if (rc != SQLITE_OK) {
286 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", set_schema_version_sql);
287 return rc;
288 }
289 rc = sqlite3_step(stmt);
290 if (rc != SQLITE_DONE)
291 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1);
292
293 db_remove_reset(stmt);
294 sqlite3_finalize(stmt);
295 return rc;
296}
297
Oliver Smith81db3892019-01-09 12:03:51 +0100298static int db_upgrade_v2(struct db_context *dbc)
299{
300 sqlite3_stmt *stmt;
301 int rc;
Neels Hofmeyra9f8a4b2019-10-31 04:26:18 +0100302 const char *update_stmt_sql = "ALTER TABLE subscriber ADD COLUMN imei VARCHAR(14)";
Oliver Smith81db3892019-01-09 12:03:51 +0100303 const char *set_schema_version_sql = "PRAGMA user_version = 2";
304
305 rc = sqlite3_prepare_v2(dbc->db, update_stmt_sql, -1, &stmt, NULL);
306 if (rc != SQLITE_OK) {
307 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", update_stmt_sql);
308 return rc;
309 }
310 rc = sqlite3_step(stmt);
311 db_remove_reset(stmt);
312 sqlite3_finalize(stmt);
313 if (rc != SQLITE_DONE) {
Neels Hofmeyrf5459de2019-10-31 01:29:59 +0100314 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 2\n");
Oliver Smith81db3892019-01-09 12:03:51 +0100315 return rc;
316 }
317
318 rc = sqlite3_prepare_v2(dbc->db, set_schema_version_sql, -1, &stmt, NULL);
319 if (rc != SQLITE_OK) {
320 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", set_schema_version_sql);
321 return rc;
322 }
323 rc = sqlite3_step(stmt);
324 if (rc != SQLITE_DONE)
Neels Hofmeyrf5459de2019-10-31 01:29:59 +0100325 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 2\n");
Oliver Smith81db3892019-01-09 12:03:51 +0100326
327 db_remove_reset(stmt);
328 sqlite3_finalize(stmt);
329 return rc;
330}
331
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100332static int db_upgrade_v3(struct db_context *dbc)
333{
334 sqlite3_stmt *stmt;
335 int rc;
336
337 /* A newer SQLite version would allow simply 'ATLER TABLE subscriber RENAME COLUMN hlr_number TO msc_number'.
338 * This is a really expensive workaround for that in order to cover earlier SQLite versions as well:
339 * Create a new table with the new column name and copy the data over (https://www.sqlite.org/faq.html#q11).
340 */
341#define SUBSCR_V3_CREATE \
342"(\n" \
343"-- OsmoHLR's DB scheme is modelled roughly after TS 23.008 version 13.3.0\n" \
344" id INTEGER PRIMARY KEY,\n" \
345" -- Chapter 2.1.1.1\n" \
346" imsi VARCHAR(15) UNIQUE NOT NULL,\n" \
347" -- Chapter 2.1.2\n" \
348" msisdn VARCHAR(15) UNIQUE,\n" \
349" -- Chapter 2.2.3: Most recent / current IMEISV\n" \
350" imeisv VARCHAR,\n" \
351" -- Chapter 2.1.9: Most recent / current IMEI\n" \
352" imei VARCHAR(14),\n" \
353" -- Chapter 2.4.5\n" \
354" vlr_number VARCHAR(15),\n" \
355" -- Chapter 2.4.6\n" \
356" msc_number VARCHAR(15),\n" \
357" -- Chapter 2.4.8.1\n" \
358" sgsn_number VARCHAR(15),\n" \
359" -- Chapter 2.13.10\n" \
360" sgsn_address VARCHAR,\n" \
361" -- Chapter 2.4.8.2\n" \
362" ggsn_number VARCHAR(15),\n" \
363" -- Chapter 2.4.9.2\n" \
364" gmlc_number VARCHAR(15),\n" \
365" -- Chapter 2.4.23\n" \
366" smsc_number VARCHAR(15),\n" \
367" -- Chapter 2.4.24\n" \
368" periodic_lu_tmr INTEGER,\n" \
369" -- Chapter 2.13.115\n" \
370" periodic_rau_tau_tmr INTEGER,\n" \
371" -- Chapter 2.1.1.2: network access mode\n" \
372" nam_cs BOOLEAN NOT NULL DEFAULT 1,\n" \
373" nam_ps BOOLEAN NOT NULL DEFAULT 1,\n" \
374" -- Chapter 2.1.8\n" \
375" lmsi INTEGER,\n" \
376 \
377" -- The below purged flags might not even be stored non-volatile,\n" \
378" -- refer to TS 23.012 Chapter 3.6.1.4\n" \
379" -- Chapter 2.7.5\n" \
380" ms_purged_cs BOOLEAN NOT NULL DEFAULT 0,\n" \
381" -- Chapter 2.7.6\n" \
382" ms_purged_ps BOOLEAN NOT NULL DEFAULT 0,\n" \
383 \
384" -- Timestamp of last location update seen from subscriber\n" \
385" -- The value is a string which encodes a UTC timestamp in granularity of seconds.\n" \
386" last_lu_seen TIMESTAMP default NULL\n" \
387")\n"
388
389#define SUBSCR_V2_COLUMN_NAMES \
390 "id," \
391 "imsi," \
392 "msisdn," \
393 "imeisv," \
394 "imei," \
395 "vlr_number," \
396 "hlr_number," \
397 "sgsn_number," \
398 "sgsn_address," \
399 "ggsn_number," \
400 "gmlc_number," \
401 "smsc_number," \
402 "periodic_lu_tmr," \
403 "periodic_rau_tau_tmr," \
404 "nam_cs," \
405 "nam_ps," \
406 "lmsi," \
407 "ms_purged_cs," \
408 "ms_purged_ps," \
409 "last_lu_seen"
410
411#define SUBSCR_V3_COLUMN_NAMES \
412 "id," \
413 "imsi," \
414 "msisdn," \
415 "imeisv," \
416 "imei," \
417 "vlr_number," \
418 "msc_number," \
419 "sgsn_number," \
420 "sgsn_address," \
421 "ggsn_number," \
422 "gmlc_number," \
423 "smsc_number," \
424 "periodic_lu_tmr," \
425 "periodic_rau_tau_tmr," \
426 "nam_cs," \
427 "nam_ps," \
428 "lmsi," \
429 "ms_purged_cs," \
430 "ms_purged_ps," \
431 "last_lu_seen"
432
433 const char *statements[] = {
434 "BEGIN TRANSACTION",
435 "CREATE TEMPORARY TABLE subscriber_backup" SUBSCR_V3_CREATE,
436 "INSERT INTO subscriber_backup SELECT " SUBSCR_V2_COLUMN_NAMES " FROM subscriber",
437 "DROP TABLE subscriber",
438 "CREATE TABLE subscriber" SUBSCR_V3_CREATE,
439 "INSERT INTO subscriber SELECT " SUBSCR_V3_COLUMN_NAMES " FROM subscriber_backup",
440 "DROP TABLE subscriber_backup",
441 "COMMIT",
442 "PRAGMA user_version = 3",
443 };
444
445 int i;
446 for (i = 0; i < ARRAY_SIZE(statements); i++) {
447 const char *update_stmt_sql = statements[i];
448
449 rc = sqlite3_prepare_v2(dbc->db, update_stmt_sql, -1, &stmt, NULL);
450 if (rc != SQLITE_OK) {
451 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", update_stmt_sql);
452 return rc;
453 }
454 rc = sqlite3_step(stmt);
455 db_remove_reset(stmt);
456 sqlite3_finalize(stmt);
457 if (rc != SQLITE_DONE) {
458 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 3\n");
459 return rc;
460 }
461
462 }
463 return rc;
464}
465
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100466static int db_get_user_version(struct db_context *dbc)
467{
468 const char *user_version_sql = "PRAGMA user_version";
469 sqlite3_stmt *stmt;
470 int version, rc;
471
472 rc = sqlite3_prepare_v2(dbc->db, user_version_sql, -1, &stmt, NULL);
473 if (rc != SQLITE_OK) {
474 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", user_version_sql);
475 return -1;
476 }
477 rc = sqlite3_step(stmt);
478 if (rc == SQLITE_ROW) {
479 version = sqlite3_column_int(stmt, 0);
480 } else {
481 LOGP(DDB, LOGL_ERROR, "SQL statement '%s' failed: %d\n", user_version_sql, rc);
482 version = -1;
483 }
484
485 db_remove_reset(stmt);
486 sqlite3_finalize(stmt);
487 return version;
488}
489
490struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite_logging, bool allow_upgrade)
Harald Weltee72cf552016-04-28 07:18:49 +0200491{
492 struct db_context *dbc = talloc_zero(ctx, struct db_context);
493 unsigned int i;
494 int rc;
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200495 bool has_sqlite_config_sqllog = false;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100496 int version;
Harald Weltee72cf552016-04-28 07:18:49 +0200497
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100498 LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
Harald Weltee72cf552016-04-28 07:18:49 +0200499 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
500 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
501
Vadim Yanitskiyfbd736e2018-07-31 22:40:30 +0700502#ifdef SQLITE_USE_TALLOC
503 /* Configure SQLite3 to use talloc memory allocator */
504 rc = db_sqlite3_use_talloc(ctx);
505 if (rc == SQLITE_OK) {
506 LOGP(DDB, LOGL_NOTICE, "SQLite3 is configured to use talloc\n");
507 } else {
508 LOGP(DDB, LOGL_ERROR, "Failed to configure SQLite3 "
509 "to use talloc, using default memory allocator\n");
510 }
511#endif
512
Harald Weltee72cf552016-04-28 07:18:49 +0200513 dbc->fname = talloc_strdup(dbc, fname);
514
515 for (i = 0; i < 0xfffff; i++) {
516 const char *o = sqlite3_compileoption_get(i);
517 if (!o)
518 break;
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200519 LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200520 if (!strcmp(o, "ENABLE_SQLLOG"))
521 has_sqlite_config_sqllog = true;
Harald Weltee72cf552016-04-28 07:18:49 +0200522 }
523
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100524 if (enable_sqlite_logging) {
525 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
526 if (rc != SQLITE_OK)
527 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
528 }
Harald Weltee72cf552016-04-28 07:18:49 +0200529
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200530 if (has_sqlite_config_sqllog) {
531 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
532 if (rc != SQLITE_OK)
533 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n");
534 } else
535 LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:"
536 " SQLite3 compiled without support for it\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200537
538 rc = sqlite3_open(dbc->fname, &dbc->db);
539 if (rc != SQLITE_OK) {
540 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
541 talloc_free(dbc);
542 return NULL;
543 }
544
545 /* enable extended result codes */
546 rc = sqlite3_extended_result_codes(dbc->db, 1);
547 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200548 LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200549
Harald Welteabd1a542016-05-03 18:50:41 +0200550 char *err_msg;
551 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
552 if (rc != SQLITE_OK)
553 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
554 err_msg);
555
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100556 version = db_get_user_version(dbc);
557 if (version < 0) {
558 LOGP(DDB, LOGL_ERROR, "Unable to read user version number from database '%s'\n", dbc->fname);
559 goto out_free;
560 }
561
562 /* An empty database will always report version zero. */
563 if (version == 0 && !db_is_bootstrapped_v0(dbc)) {
564 LOGP(DDB, LOGL_NOTICE, "Missing database tables detected; Bootstrapping database '%s'\n", dbc->fname);
565 rc = db_bootstrap(dbc);
566 if (rc != SQLITE_OK) {
567 LOGP(DDB, LOGL_ERROR, "Failed to bootstrap DB: (rc=%d) %s\n",
568 rc, sqlite3_errmsg(dbc->db));
569 goto out_free;
570 }
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100571 version = CURRENT_SCHEMA_VERSION;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100572 }
573
574 LOGP(DDB, LOGL_NOTICE, "Database '%s' has HLR DB schema version %d\n", dbc->fname, version);
575
576 if (version < CURRENT_SCHEMA_VERSION && allow_upgrade) {
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100577 switch (version) {
578 case 0:
579 rc = db_upgrade_v1(dbc);
580 if (rc != SQLITE_DONE) {
581 LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version 1: (rc=%d) %s\n",
582 rc, sqlite3_errmsg(dbc->db));
583 goto out_free;
584 }
585 version = 1;
586 /* fall through */
Oliver Smith81db3892019-01-09 12:03:51 +0100587 case 1:
588 rc = db_upgrade_v2(dbc);
589 if (rc != SQLITE_DONE) {
590 LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version 2: (rc=%d) %s\n",
591 rc, sqlite3_errmsg(dbc->db));
592 goto out_free;
593 }
594 version = 2;
595 /* fall through */
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100596 case 2:
597 rc = db_upgrade_v3(dbc);
598 if (rc != SQLITE_DONE) {
599 LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version 3: (rc=%d) %s\n",
600 rc, sqlite3_errmsg(dbc->db));
601 goto out_free;
602 }
603 version = 3;
604 /* fall through */
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100605 /* case N: ... */
606 default:
607 break;
608 }
609 LOGP(DDB, LOGL_NOTICE, "Database '%s' has been upgraded to HLR DB schema version %d\n",
610 dbc->fname, version);
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100611 }
612
613 if (version != CURRENT_SCHEMA_VERSION) {
614 if (version < CURRENT_SCHEMA_VERSION) {
615 LOGP(DDB, LOGL_NOTICE, "HLR DB schema version %d is outdated\n", version);
616 if (!allow_upgrade) {
617 LOGP(DDB, LOGL_ERROR, "Not upgrading HLR database to schema version %d; "
618 "use the --db-upgrade option to allow HLR database upgrades\n",
619 CURRENT_SCHEMA_VERSION);
620 }
621 } else
622 LOGP(DDB, LOGL_ERROR, "HLR DB schema version %d is unknown\n", version);
623
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700624 goto out_free;
625 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200626
Harald Weltee72cf552016-04-28 07:18:49 +0200627 /* prepare all SQL statements */
628 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
629 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
630 &dbc->stmt[i], NULL);
631 if (rc != SQLITE_OK) {
632 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
633 goto out_free;
634 }
635 }
636
637 return dbc;
638out_free:
639 db_close(dbc);
640 return NULL;
641}