blob: 3cbd9c9ec5077b01fcc6fccf3488be650135be9e [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>
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010025#include <errno.h>
Harald Weltee72cf552016-04-28 07:18:49 +020026
Neels Hofmeyr2f758032019-11-20 00:37:07 +010027#include <osmocom/hlr/logging.h>
28#include <osmocom/hlr/db.h>
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010029
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +020030#include "db_bootstrap.h"
Harald Weltee72cf552016-04-28 07:18:49 +020031
Stefan Sperling638ba8c2018-12-04 15:07:29 +010032/* This constant is currently duplicated in sql/hlr.sql and must be kept in sync! */
Neels Hofmeyrf0c02ad2019-11-25 03:59:50 +010033#define CURRENT_SCHEMA_VERSION 5
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +010034
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020035#define SEL_COLUMNS \
36 "id," \
37 "imsi," \
38 "msisdn," \
Oliver Smith81db3892019-01-09 12:03:51 +010039 "imei," \
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020040 "vlr_number," \
41 "sgsn_number," \
42 "sgsn_address," \
43 "periodic_lu_tmr," \
44 "periodic_rau_tau_tmr," \
45 "nam_cs," \
46 "nam_ps," \
47 "lmsi," \
48 "ms_purged_cs," \
Stefan Sperling638ba8c2018-12-04 15:07:29 +010049 "ms_purged_ps," \
Neels Hofmeyr07e16022019-11-20 02:36:35 +010050 "last_lu_seen," \
Neels Hofmeyrf0c02ad2019-11-25 03:59:50 +010051 "last_lu_seen_ps," \
52 "vlr_via_proxy," \
53 "sgsn_via_proxy"
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020054
Harald Weltee72cf552016-04-28 07:18:49 +020055static const char *stmt_sql[] = {
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020056 [DB_STMT_SEL_BY_IMSI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imsi = ?",
57 [DB_STMT_SEL_BY_MSISDN] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE msisdn = ?",
58 [DB_STMT_SEL_BY_ID] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE id = ?",
Oliver Smith81db3892019-01-09 12:03:51 +010059 [DB_STMT_SEL_BY_IMEI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imei = ?",
Neels Hofmeyrf0c02ad2019-11-25 03:59:50 +010060 [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = $number, vlr_via_proxy = $proxy WHERE id = $subscriber_id",
61 [DB_STMT_UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = $number, sgsn_via_proxy = $proxy WHERE id = $subscriber_id",
Oliver Smith81db3892019-01-09 12:03:51 +010062 [DB_STMT_UPD_IMEI_BY_IMSI] = "UPDATE subscriber SET imei = $imei WHERE imsi = $imsi",
Neels Hofmeyr0cac0a02017-10-09 17:47:21 +020063 [DB_STMT_AUC_BY_IMSI] =
64 "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn, ind_bitlen"
65 " FROM subscriber"
66 " LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id"
67 " LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id"
Neels Hofmeyrc5122f22017-10-09 23:12:57 +020068 " WHERE imsi = $imsi",
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020069 [DB_STMT_AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = $sqn WHERE subscriber_id = $subscriber_id",
Neels Hofmeyre50121e2017-10-09 17:48:51 +020070 [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs = $val WHERE imsi = $imsi",
71 [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps = $val WHERE imsi = $imsi",
Neels Hofmeyre8ccd502017-10-06 04:10:06 +020072 [DB_STMT_UPD_NAM_CS_BY_IMSI] = "UPDATE subscriber SET nam_cs = $val WHERE imsi = $imsi",
73 [DB_STMT_UPD_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps = $val WHERE imsi = $imsi",
Oliver Smithcd2af5e2019-03-06 13:17:39 +010074 [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 +020075 [DB_STMT_DEL_BY_ID] = "DELETE FROM subscriber WHERE id = $subscriber_id",
76 [DB_STMT_SET_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = $msisdn WHERE imsi = $imsi",
Neels Hofmeyra820ea12018-12-02 19:46:46 +010077 [DB_STMT_DELETE_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = NULL WHERE imsi = $imsi",
Neels Hofmeyr1332a172017-10-10 02:25:00 +020078 [DB_STMT_AUC_2G_INSERT] =
79 "INSERT INTO auc_2g (subscriber_id, algo_id_2g, ki)"
80 " VALUES($subscriber_id, $algo_id_2g, $ki)",
81 [DB_STMT_AUC_2G_DELETE] = "DELETE FROM auc_2g WHERE subscriber_id = $subscriber_id",
82 [DB_STMT_AUC_3G_INSERT] =
83 "INSERT INTO auc_3g (subscriber_id, algo_id_3g, k, op, opc, ind_bitlen)"
84 " VALUES($subscriber_id, $algo_id_3g, $k, $op, $opc, $ind_bitlen)",
85 [DB_STMT_AUC_3G_DELETE] = "DELETE FROM auc_3g WHERE subscriber_id = $subscriber_id",
Stefan Sperling638ba8c2018-12-04 15:07:29 +010086 [DB_STMT_SET_LAST_LU_SEEN] = "UPDATE subscriber SET last_lu_seen = datetime($val, 'unixepoch') WHERE id = $subscriber_id",
Neels Hofmeyr07e16022019-11-20 02:36:35 +010087 [DB_STMT_SET_LAST_LU_SEEN_PS] = "UPDATE subscriber SET last_lu_seen_ps = datetime($val, 'unixepoch') WHERE id = $subscriber_id",
Oliver Smith6b73fd92019-03-06 13:49:05 +010088 [DB_STMT_EXISTS_BY_IMSI] = "SELECT 1 FROM subscriber WHERE imsi = $imsi",
Vadim Yanitskiyc13599d2019-03-30 17:03:42 +070089 [DB_STMT_EXISTS_BY_MSISDN] = "SELECT 1 FROM subscriber WHERE msisdn = $msisdn",
Harald Weltee72cf552016-04-28 07:18:49 +020090};
91
92static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
93{
94 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
95}
96
97static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
98{
99 switch (type) {
100 case 0:
101 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
102 break;
103 case 1:
Max58d4a842017-02-20 11:06:12 +0100104 LOGP(DDB, LOGL_DEBUG, "%s\n", stmt);
Harald Weltee72cf552016-04-28 07:18:49 +0200105 break;
106 case 2:
107 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
108 break;
109 default:
110 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
111 break;
112 }
113}
114
Max00b37152017-02-20 11:09:27 +0100115/* remove bindings and reset statement to be re-executed */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200116void db_remove_reset(sqlite3_stmt *stmt)
Max00b37152017-02-20 11:09:27 +0100117{
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200118 sqlite3_clear_bindings(stmt);
Neels Hofmeyr4f3841c2017-11-07 13:24:44 +0100119 /* sqlite3_reset() just repeats an error code already evaluated during sqlite3_step(). */
120 /* coverity[CHECKED_RETURN] */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200121 sqlite3_reset(stmt);
Max00b37152017-02-20 11:09:27 +0100122}
123
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200124/** bind text arg and do proper cleanup in case of failure. If param_name is
125 * NULL, bind to the first parameter (useful for SQL statements that have only
126 * one parameter). */
127bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
Max00b37152017-02-20 11:09:27 +0100128{
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200129 int rc;
130 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
131 if (idx < 1) {
132 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
133 param_name);
134 return false;
135 }
136 rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
Max00b37152017-02-20 11:09:27 +0100137 if (rc != SQLITE_OK) {
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200138 LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
139 param_name ? param_name : "#1", rc);
Max00b37152017-02-20 11:09:27 +0100140 db_remove_reset(stmt);
141 return false;
142 }
Max00b37152017-02-20 11:09:27 +0100143 return true;
144}
145
Neels Hofmeyr28da26e2017-10-06 03:44:57 +0200146/** bind int arg and do proper cleanup in case of failure. If param_name is
147 * NULL, bind to the first parameter (useful for SQL statements that have only
148 * one parameter). */
149bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr)
150{
151 int rc;
152 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
153 if (idx < 1) {
154 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
155 param_name);
156 return false;
157 }
158 rc = sqlite3_bind_int(stmt, idx, nr);
159 if (rc != SQLITE_OK) {
160 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
161 param_name ? param_name : "#1", rc);
162 db_remove_reset(stmt);
163 return false;
164 }
165 return true;
166}
167
168/** bind int64 arg and do proper cleanup in case of failure. If param_name is
169 * NULL, bind to the first parameter (useful for SQL statements that have only
170 * one parameter). */
171bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr)
172{
173 int rc;
174 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
175 if (idx < 1) {
176 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
177 param_name);
178 return false;
179 }
180 rc = sqlite3_bind_int64(stmt, idx, nr);
181 if (rc != SQLITE_OK) {
182 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
183 param_name ? param_name : "#1", rc);
184 db_remove_reset(stmt);
185 return false;
186 }
187 return true;
188}
189
Neels Hofmeyrf0c02ad2019-11-25 03:59:50 +0100190bool db_bind_null(sqlite3_stmt *stmt, const char *param_name)
191{
192 int rc;
193 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
194 if (idx < 1) {
195 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
196 param_name);
197 return false;
198 }
199 rc = sqlite3_bind_null(stmt, idx);
200 if (rc != SQLITE_OK) {
201 LOGP(DDB, LOGL_ERROR, "Error binding NULL to SQL parameter %s: %d\n",
202 param_name ? param_name : "#1", rc);
203 db_remove_reset(stmt);
204 return false;
205 }
206 return true;
207}
208
Harald Weltee72cf552016-04-28 07:18:49 +0200209void db_close(struct db_context *dbc)
210{
211 unsigned int i;
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700212 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200213
214 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
215 /* it is ok to call finalize on NULL */
216 sqlite3_finalize(dbc->stmt[i]);
217 }
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700218
219 /* Ask sqlite3 to close DB */
220 rc = sqlite3_close(dbc->db);
221 if (rc != SQLITE_OK) { /* Make sure it's actually closed! */
222 LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n",
223 rc, sqlite3_errmsg(dbc->db));
224 }
225
Harald Weltee72cf552016-04-28 07:18:49 +0200226 talloc_free(dbc);
227}
228
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100229static int db_run_statements(struct db_context *dbc, const char **statements, size_t statements_count)
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200230{
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100231 int rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200232 int i;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100233 for (i = 0; i < statements_count; i++) {
234 const char *stmt_str = statements[i];
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200235 sqlite3_stmt *stmt;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100236
237 rc = sqlite3_prepare_v2(dbc->db, stmt_str, -1, &stmt, NULL);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200238 if (rc != SQLITE_OK) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100239 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_str);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700240 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200241 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200242 rc = sqlite3_step(stmt);
243 db_remove_reset(stmt);
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700244 sqlite3_finalize(stmt);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200245 if (rc != SQLITE_DONE) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100246 LOGP(DDB, LOGL_ERROR, "SQL error: (%d) %s, during stmt '%s'",
247 rc, sqlite3_errmsg(dbc->db), stmt_str);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700248 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200249 }
250 }
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100251 return rc;
252}
253
254static int db_bootstrap(struct db_context *dbc)
255{
256 int rc = db_run_statements(dbc, stmt_bootstrap_sql, ARRAY_SIZE(stmt_bootstrap_sql));
257 if (rc != SQLITE_DONE) {
258 LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database\n");
259 return rc;
260 }
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700261 return SQLITE_OK;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200262}
263
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100264/* https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema */
265static bool db_table_exists(struct db_context *dbc, const char *table_name)
266{
267 const char *table_exists_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=?";
268 sqlite3_stmt *stmt;
269 int rc;
270
271 rc = sqlite3_prepare_v2(dbc->db, table_exists_sql, -1, &stmt, NULL);
272 if (rc != SQLITE_OK) {
273 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", table_exists_sql);
274 return false;
275 }
276
277 if (!db_bind_text(stmt, NULL, table_name))
278 return false;
279
280 rc = sqlite3_step(stmt);
281 db_remove_reset(stmt);
282 sqlite3_finalize(stmt);
283 return (rc == SQLITE_ROW);
284}
285
286/* Indicate whether the database is initialized with tables for schema version 0.
287 * We only check for the 'subscriber' table here because Neels said so. */
288static bool db_is_bootstrapped_v0(struct db_context *dbc)
289{
290 if (!db_table_exists(dbc, "subscriber")) {
291 LOGP(DDB, LOGL_DEBUG, "Table 'subscriber' not found in database '%s'\n", dbc->fname);
292 return false;
293 }
294
295 return true;
296}
297
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100298static int
299db_upgrade_v1(struct db_context *dbc)
300{
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100301 int rc;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100302 const char *statements[] = {
303 "ALTER TABLE subscriber ADD COLUMN last_lu_seen TIMESTAMP default NULL",
304 "PRAGMA user_version = 1",
305 };
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100306
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100307 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100308 if (rc != SQLITE_DONE) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100309 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 1\n");
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100310 return rc;
311 }
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100312 return rc;
313}
314
Oliver Smith81db3892019-01-09 12:03:51 +0100315static int db_upgrade_v2(struct db_context *dbc)
316{
Oliver Smith81db3892019-01-09 12:03:51 +0100317 int rc;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100318 const char *statements[] = {
319 "ALTER TABLE subscriber ADD COLUMN imei VARCHAR(14)",
320 "PRAGMA user_version = 2",
321 };
Oliver Smith81db3892019-01-09 12:03:51 +0100322
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100323 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
Oliver Smith81db3892019-01-09 12:03:51 +0100324 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 return rc;
327 }
Oliver Smith81db3892019-01-09 12:03:51 +0100328 return rc;
329}
330
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100331static int db_upgrade_v3(struct db_context *dbc)
332{
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100333 int rc;
334
335 /* A newer SQLite version would allow simply 'ATLER TABLE subscriber RENAME COLUMN hlr_number TO msc_number'.
336 * This is a really expensive workaround for that in order to cover earlier SQLite versions as well:
337 * Create a new table with the new column name and copy the data over (https://www.sqlite.org/faq.html#q11).
338 */
339#define SUBSCR_V3_CREATE \
340"(\n" \
341"-- OsmoHLR's DB scheme is modelled roughly after TS 23.008 version 13.3.0\n" \
342" id INTEGER PRIMARY KEY,\n" \
343" -- Chapter 2.1.1.1\n" \
344" imsi VARCHAR(15) UNIQUE NOT NULL,\n" \
345" -- Chapter 2.1.2\n" \
346" msisdn VARCHAR(15) UNIQUE,\n" \
347" -- Chapter 2.2.3: Most recent / current IMEISV\n" \
348" imeisv VARCHAR,\n" \
349" -- Chapter 2.1.9: Most recent / current IMEI\n" \
350" imei VARCHAR(14),\n" \
351" -- Chapter 2.4.5\n" \
352" vlr_number VARCHAR(15),\n" \
353" -- Chapter 2.4.6\n" \
354" msc_number VARCHAR(15),\n" \
355" -- Chapter 2.4.8.1\n" \
356" sgsn_number VARCHAR(15),\n" \
357" -- Chapter 2.13.10\n" \
358" sgsn_address VARCHAR,\n" \
359" -- Chapter 2.4.8.2\n" \
360" ggsn_number VARCHAR(15),\n" \
361" -- Chapter 2.4.9.2\n" \
362" gmlc_number VARCHAR(15),\n" \
363" -- Chapter 2.4.23\n" \
364" smsc_number VARCHAR(15),\n" \
365" -- Chapter 2.4.24\n" \
366" periodic_lu_tmr INTEGER,\n" \
367" -- Chapter 2.13.115\n" \
368" periodic_rau_tau_tmr INTEGER,\n" \
369" -- Chapter 2.1.1.2: network access mode\n" \
370" nam_cs BOOLEAN NOT NULL DEFAULT 1,\n" \
371" nam_ps BOOLEAN NOT NULL DEFAULT 1,\n" \
372" -- Chapter 2.1.8\n" \
373" lmsi INTEGER,\n" \
374 \
375" -- The below purged flags might not even be stored non-volatile,\n" \
376" -- refer to TS 23.012 Chapter 3.6.1.4\n" \
377" -- Chapter 2.7.5\n" \
378" ms_purged_cs BOOLEAN NOT NULL DEFAULT 0,\n" \
379" -- Chapter 2.7.6\n" \
380" ms_purged_ps BOOLEAN NOT NULL DEFAULT 0,\n" \
381 \
382" -- Timestamp of last location update seen from subscriber\n" \
383" -- The value is a string which encodes a UTC timestamp in granularity of seconds.\n" \
384" last_lu_seen TIMESTAMP default NULL\n" \
385")\n"
386
387#define SUBSCR_V2_COLUMN_NAMES \
388 "id," \
389 "imsi," \
390 "msisdn," \
391 "imeisv," \
392 "imei," \
393 "vlr_number," \
394 "hlr_number," \
395 "sgsn_number," \
396 "sgsn_address," \
397 "ggsn_number," \
398 "gmlc_number," \
399 "smsc_number," \
400 "periodic_lu_tmr," \
401 "periodic_rau_tau_tmr," \
402 "nam_cs," \
403 "nam_ps," \
404 "lmsi," \
405 "ms_purged_cs," \
406 "ms_purged_ps," \
407 "last_lu_seen"
408
409#define SUBSCR_V3_COLUMN_NAMES \
410 "id," \
411 "imsi," \
412 "msisdn," \
413 "imeisv," \
414 "imei," \
415 "vlr_number," \
416 "msc_number," \
417 "sgsn_number," \
418 "sgsn_address," \
419 "ggsn_number," \
420 "gmlc_number," \
421 "smsc_number," \
422 "periodic_lu_tmr," \
423 "periodic_rau_tau_tmr," \
424 "nam_cs," \
425 "nam_ps," \
426 "lmsi," \
427 "ms_purged_cs," \
428 "ms_purged_ps," \
429 "last_lu_seen"
430
431 const char *statements[] = {
432 "BEGIN TRANSACTION",
433 "CREATE TEMPORARY TABLE subscriber_backup" SUBSCR_V3_CREATE,
434 "INSERT INTO subscriber_backup SELECT " SUBSCR_V2_COLUMN_NAMES " FROM subscriber",
435 "DROP TABLE subscriber",
436 "CREATE TABLE subscriber" SUBSCR_V3_CREATE,
437 "INSERT INTO subscriber SELECT " SUBSCR_V3_COLUMN_NAMES " FROM subscriber_backup",
438 "DROP TABLE subscriber_backup",
439 "COMMIT",
440 "PRAGMA user_version = 3",
441 };
442
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100443 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
444 if (rc != SQLITE_DONE) {
445 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 3\n");
446 return rc;
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100447 }
448 return rc;
449}
450
Neels Hofmeyr07e16022019-11-20 02:36:35 +0100451static int db_upgrade_v4(struct db_context *dbc)
452{
453 int rc;
454 const char *statements[] = {
455 "ALTER TABLE subscriber ADD COLUMN last_lu_seen_ps TIMESTAMP default NULL",
456 "PRAGMA user_version = 4",
457 };
458
459 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
460 if (rc != SQLITE_DONE) {
461 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 4\n");
462 return rc;
463 }
464 return rc;
465}
466
Neels Hofmeyrf0c02ad2019-11-25 03:59:50 +0100467static int db_upgrade_v5(struct db_context *dbc)
468{
469 int rc;
470 const char *statements[] = {
471 "ALTER TABLE subscriber ADD COLUMN vlr_via_proxy VARCHAR",
472 "ALTER TABLE subscriber ADD COLUMN sgsn_via_proxy VARCHAR",
473 "PRAGMA user_version = 5",
474 };
475
476 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
477 if (rc != SQLITE_DONE) {
478 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 5\n");
479 return rc;
480 }
481 return rc;
482}
483
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100484typedef int (*db_upgrade_func_t)(struct db_context *dbc);
485static db_upgrade_func_t db_upgrade_path[] = {
486 db_upgrade_v1,
487 db_upgrade_v2,
488 db_upgrade_v3,
Neels Hofmeyr07e16022019-11-20 02:36:35 +0100489 db_upgrade_v4,
Neels Hofmeyrf0c02ad2019-11-25 03:59:50 +0100490 db_upgrade_v5,
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100491};
492
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100493static int db_get_user_version(struct db_context *dbc)
494{
495 const char *user_version_sql = "PRAGMA user_version";
496 sqlite3_stmt *stmt;
497 int version, rc;
498
499 rc = sqlite3_prepare_v2(dbc->db, user_version_sql, -1, &stmt, NULL);
500 if (rc != SQLITE_OK) {
501 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", user_version_sql);
502 return -1;
503 }
504 rc = sqlite3_step(stmt);
505 if (rc == SQLITE_ROW) {
506 version = sqlite3_column_int(stmt, 0);
507 } else {
508 LOGP(DDB, LOGL_ERROR, "SQL statement '%s' failed: %d\n", user_version_sql, rc);
509 version = -1;
510 }
511
512 db_remove_reset(stmt);
513 sqlite3_finalize(stmt);
514 return version;
515}
516
517struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite_logging, bool allow_upgrade)
Harald Weltee72cf552016-04-28 07:18:49 +0200518{
519 struct db_context *dbc = talloc_zero(ctx, struct db_context);
520 unsigned int i;
521 int rc;
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200522 bool has_sqlite_config_sqllog = false;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100523 int version;
Harald Weltee72cf552016-04-28 07:18:49 +0200524
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100525 LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
Harald Weltee72cf552016-04-28 07:18:49 +0200526 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
527 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
528
Vadim Yanitskiyfbd736e2018-07-31 22:40:30 +0700529#ifdef SQLITE_USE_TALLOC
530 /* Configure SQLite3 to use talloc memory allocator */
531 rc = db_sqlite3_use_talloc(ctx);
532 if (rc == SQLITE_OK) {
533 LOGP(DDB, LOGL_NOTICE, "SQLite3 is configured to use talloc\n");
534 } else {
535 LOGP(DDB, LOGL_ERROR, "Failed to configure SQLite3 "
536 "to use talloc, using default memory allocator\n");
537 }
538#endif
539
Harald Weltee72cf552016-04-28 07:18:49 +0200540 dbc->fname = talloc_strdup(dbc, fname);
541
542 for (i = 0; i < 0xfffff; i++) {
543 const char *o = sqlite3_compileoption_get(i);
544 if (!o)
545 break;
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200546 LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200547 if (!strcmp(o, "ENABLE_SQLLOG"))
548 has_sqlite_config_sqllog = true;
Harald Weltee72cf552016-04-28 07:18:49 +0200549 }
550
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100551 if (enable_sqlite_logging) {
552 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
553 if (rc != SQLITE_OK)
554 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
555 }
Harald Weltee72cf552016-04-28 07:18:49 +0200556
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200557 if (has_sqlite_config_sqllog) {
558 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
559 if (rc != SQLITE_OK)
560 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n");
561 } else
562 LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:"
563 " SQLite3 compiled without support for it\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200564
565 rc = sqlite3_open(dbc->fname, &dbc->db);
566 if (rc != SQLITE_OK) {
567 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
568 talloc_free(dbc);
569 return NULL;
570 }
571
572 /* enable extended result codes */
573 rc = sqlite3_extended_result_codes(dbc->db, 1);
574 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200575 LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200576
Harald Welteabd1a542016-05-03 18:50:41 +0200577 char *err_msg;
578 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
579 if (rc != SQLITE_OK)
580 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
581 err_msg);
582
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100583 version = db_get_user_version(dbc);
584 if (version < 0) {
585 LOGP(DDB, LOGL_ERROR, "Unable to read user version number from database '%s'\n", dbc->fname);
586 goto out_free;
587 }
588
589 /* An empty database will always report version zero. */
590 if (version == 0 && !db_is_bootstrapped_v0(dbc)) {
591 LOGP(DDB, LOGL_NOTICE, "Missing database tables detected; Bootstrapping database '%s'\n", dbc->fname);
592 rc = db_bootstrap(dbc);
593 if (rc != SQLITE_OK) {
594 LOGP(DDB, LOGL_ERROR, "Failed to bootstrap DB: (rc=%d) %s\n",
595 rc, sqlite3_errmsg(dbc->db));
596 goto out_free;
597 }
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100598 version = CURRENT_SCHEMA_VERSION;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100599 }
600
601 LOGP(DDB, LOGL_NOTICE, "Database '%s' has HLR DB schema version %d\n", dbc->fname, version);
602
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100603 for (; allow_upgrade && (version < ARRAY_SIZE(db_upgrade_path)); version++) {
604 db_upgrade_func_t upgrade_func = db_upgrade_path[version];
605 rc = upgrade_func(dbc);
606 if (rc != SQLITE_DONE) {
607 LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version %d: (rc=%d) %s\n",
608 version+1, rc, sqlite3_errmsg(dbc->db));
609 goto out_free;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100610 }
611 LOGP(DDB, LOGL_NOTICE, "Database '%s' has been upgraded to HLR DB schema version %d\n",
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100612 dbc->fname, version+1);
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100613 }
614
615 if (version != CURRENT_SCHEMA_VERSION) {
616 if (version < CURRENT_SCHEMA_VERSION) {
617 LOGP(DDB, LOGL_NOTICE, "HLR DB schema version %d is outdated\n", version);
618 if (!allow_upgrade) {
619 LOGP(DDB, LOGL_ERROR, "Not upgrading HLR database to schema version %d; "
620 "use the --db-upgrade option to allow HLR database upgrades\n",
621 CURRENT_SCHEMA_VERSION);
622 }
623 } else
624 LOGP(DDB, LOGL_ERROR, "HLR DB schema version %d is unknown\n", version);
625
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700626 goto out_free;
627 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200628
Harald Weltee72cf552016-04-28 07:18:49 +0200629 /* prepare all SQL statements */
630 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
631 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
632 &dbc->stmt[i], NULL);
633 if (rc != SQLITE_OK) {
634 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
635 goto out_free;
636 }
637 }
638
639 return dbc;
640out_free:
641 db_close(dbc);
642 return NULL;
643}