blob: bc599bd3de0601f02ef727df4893c189b6528ab4 [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 Hofmeyrdd73ccf2019-12-12 04:04:53 +010031#define CURRENT_SCHEMA_VERSION 6
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," \
Neels Hofmeyr07e16022019-11-20 02:36:35 +010048 "last_lu_seen," \
Neels Hofmeyrf6c8f042019-11-25 03:59:50 +010049 "last_lu_seen_ps," \
50 "vlr_via_proxy," \
51 "sgsn_via_proxy"
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020052
Harald Weltee72cf552016-04-28 07:18:49 +020053static const char *stmt_sql[] = {
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020054 [DB_STMT_SEL_BY_IMSI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imsi = ?",
55 [DB_STMT_SEL_BY_MSISDN] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE msisdn = ?",
56 [DB_STMT_SEL_BY_ID] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE id = ?",
Oliver Smith81db3892019-01-09 12:03:51 +010057 [DB_STMT_SEL_BY_IMEI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imei = ?",
Neels Hofmeyrf6c8f042019-11-25 03:59:50 +010058 [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = $number, vlr_via_proxy = $proxy WHERE id = $subscriber_id",
59 [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 +010060 [DB_STMT_UPD_IMEI_BY_IMSI] = "UPDATE subscriber SET imei = $imei WHERE imsi = $imsi",
Neels Hofmeyr0cac0a02017-10-09 17:47:21 +020061 [DB_STMT_AUC_BY_IMSI] =
62 "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn, ind_bitlen"
63 " FROM subscriber"
64 " LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id"
65 " LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id"
Neels Hofmeyrc5122f22017-10-09 23:12:57 +020066 " WHERE imsi = $imsi",
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020067 [DB_STMT_AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = $sqn WHERE subscriber_id = $subscriber_id",
Neels Hofmeyre50121e2017-10-09 17:48:51 +020068 [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs = $val WHERE imsi = $imsi",
69 [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps = $val WHERE imsi = $imsi",
Neels Hofmeyre8ccd502017-10-06 04:10:06 +020070 [DB_STMT_UPD_NAM_CS_BY_IMSI] = "UPDATE subscriber SET nam_cs = $val WHERE imsi = $imsi",
71 [DB_STMT_UPD_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps = $val WHERE imsi = $imsi",
Oliver Smithcd2af5e2019-03-06 13:17:39 +010072 [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 +020073 [DB_STMT_DEL_BY_ID] = "DELETE FROM subscriber WHERE id = $subscriber_id",
74 [DB_STMT_SET_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = $msisdn WHERE imsi = $imsi",
Neels Hofmeyra820ea12018-12-02 19:46:46 +010075 [DB_STMT_DELETE_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = NULL WHERE imsi = $imsi",
Neels Hofmeyr1332a172017-10-10 02:25:00 +020076 [DB_STMT_AUC_2G_INSERT] =
77 "INSERT INTO auc_2g (subscriber_id, algo_id_2g, ki)"
78 " VALUES($subscriber_id, $algo_id_2g, $ki)",
79 [DB_STMT_AUC_2G_DELETE] = "DELETE FROM auc_2g WHERE subscriber_id = $subscriber_id",
80 [DB_STMT_AUC_3G_INSERT] =
81 "INSERT INTO auc_3g (subscriber_id, algo_id_3g, k, op, opc, ind_bitlen)"
82 " VALUES($subscriber_id, $algo_id_3g, $k, $op, $opc, $ind_bitlen)",
83 [DB_STMT_AUC_3G_DELETE] = "DELETE FROM auc_3g WHERE subscriber_id = $subscriber_id",
Stefan Sperling638ba8c2018-12-04 15:07:29 +010084 [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 +010085 [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 +010086 [DB_STMT_EXISTS_BY_IMSI] = "SELECT 1 FROM subscriber WHERE imsi = $imsi",
Vadim Yanitskiyc13599d2019-03-30 17:03:42 +070087 [DB_STMT_EXISTS_BY_MSISDN] = "SELECT 1 FROM subscriber WHERE msisdn = $msisdn",
Neels Hofmeyrdd73ccf2019-12-12 04:04:53 +010088 [DB_STMT_IND_ADD] = "INSERT INTO ind (vlr) VALUES ($vlr)",
89 [DB_STMT_IND_SELECT] = "SELECT ind FROM ind WHERE vlr = $vlr",
90 [DB_STMT_IND_DEL] = "DELETE FROM ind WHERE vlr = $vlr",
Harald Weltee72cf552016-04-28 07:18:49 +020091};
92
93static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
94{
95 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
96}
97
98static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
99{
100 switch (type) {
101 case 0:
102 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
103 break;
104 case 1:
Max58d4a842017-02-20 11:06:12 +0100105 LOGP(DDB, LOGL_DEBUG, "%s\n", stmt);
Harald Weltee72cf552016-04-28 07:18:49 +0200106 break;
107 case 2:
108 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
109 break;
110 default:
111 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
112 break;
113 }
114}
115
Max00b37152017-02-20 11:09:27 +0100116/* remove bindings and reset statement to be re-executed */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200117void db_remove_reset(sqlite3_stmt *stmt)
Max00b37152017-02-20 11:09:27 +0100118{
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200119 sqlite3_clear_bindings(stmt);
Neels Hofmeyr4f3841c2017-11-07 13:24:44 +0100120 /* sqlite3_reset() just repeats an error code already evaluated during sqlite3_step(). */
121 /* coverity[CHECKED_RETURN] */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200122 sqlite3_reset(stmt);
Max00b37152017-02-20 11:09:27 +0100123}
124
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200125/** bind text arg and do proper cleanup in case of failure. If param_name is
126 * NULL, bind to the first parameter (useful for SQL statements that have only
127 * one parameter). */
128bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
Max00b37152017-02-20 11:09:27 +0100129{
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200130 int rc;
131 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
132 if (idx < 1) {
133 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
134 param_name);
135 return false;
136 }
137 rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
Max00b37152017-02-20 11:09:27 +0100138 if (rc != SQLITE_OK) {
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200139 LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
140 param_name ? param_name : "#1", rc);
Max00b37152017-02-20 11:09:27 +0100141 db_remove_reset(stmt);
142 return false;
143 }
Max00b37152017-02-20 11:09:27 +0100144 return true;
145}
146
Neels Hofmeyr28da26e2017-10-06 03:44:57 +0200147/** bind int arg and do proper cleanup in case of failure. If param_name is
148 * NULL, bind to the first parameter (useful for SQL statements that have only
149 * one parameter). */
150bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr)
151{
152 int rc;
153 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
154 if (idx < 1) {
155 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
156 param_name);
157 return false;
158 }
159 rc = sqlite3_bind_int(stmt, idx, nr);
160 if (rc != SQLITE_OK) {
161 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
162 param_name ? param_name : "#1", rc);
163 db_remove_reset(stmt);
164 return false;
165 }
166 return true;
167}
168
169/** bind int64 arg and do proper cleanup in case of failure. If param_name is
170 * NULL, bind to the first parameter (useful for SQL statements that have only
171 * one parameter). */
172bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr)
173{
174 int rc;
175 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
176 if (idx < 1) {
177 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
178 param_name);
179 return false;
180 }
181 rc = sqlite3_bind_int64(stmt, idx, nr);
182 if (rc != SQLITE_OK) {
183 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
184 param_name ? param_name : "#1", rc);
185 db_remove_reset(stmt);
186 return false;
187 }
188 return true;
189}
190
Neels Hofmeyrf6c8f042019-11-25 03:59:50 +0100191bool db_bind_null(sqlite3_stmt *stmt, const char *param_name)
192{
193 int rc;
194 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
195 if (idx < 1) {
196 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
197 param_name);
198 return false;
199 }
200 rc = sqlite3_bind_null(stmt, idx);
201 if (rc != SQLITE_OK) {
202 LOGP(DDB, LOGL_ERROR, "Error binding NULL to SQL parameter %s: %d\n",
203 param_name ? param_name : "#1", rc);
204 db_remove_reset(stmt);
205 return false;
206 }
207 return true;
208}
209
Harald Weltee72cf552016-04-28 07:18:49 +0200210void db_close(struct db_context *dbc)
211{
212 unsigned int i;
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700213 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200214
215 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
216 /* it is ok to call finalize on NULL */
217 sqlite3_finalize(dbc->stmt[i]);
218 }
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700219
220 /* Ask sqlite3 to close DB */
221 rc = sqlite3_close(dbc->db);
222 if (rc != SQLITE_OK) { /* Make sure it's actually closed! */
223 LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n",
224 rc, sqlite3_errmsg(dbc->db));
225 }
226
Harald Weltee72cf552016-04-28 07:18:49 +0200227 talloc_free(dbc);
228}
229
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100230static int db_run_statements(struct db_context *dbc, const char **statements, size_t statements_count)
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200231{
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100232 int rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200233 int i;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100234 for (i = 0; i < statements_count; i++) {
235 const char *stmt_str = statements[i];
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200236 sqlite3_stmt *stmt;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100237
238 rc = sqlite3_prepare_v2(dbc->db, stmt_str, -1, &stmt, NULL);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200239 if (rc != SQLITE_OK) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100240 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_str);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700241 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200242 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200243 rc = sqlite3_step(stmt);
244 db_remove_reset(stmt);
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700245 sqlite3_finalize(stmt);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200246 if (rc != SQLITE_DONE) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100247 LOGP(DDB, LOGL_ERROR, "SQL error: (%d) %s, during stmt '%s'",
248 rc, sqlite3_errmsg(dbc->db), stmt_str);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700249 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200250 }
251 }
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100252 return rc;
253}
254
255static int db_bootstrap(struct db_context *dbc)
256{
257 int rc = db_run_statements(dbc, stmt_bootstrap_sql, ARRAY_SIZE(stmt_bootstrap_sql));
258 if (rc != SQLITE_DONE) {
259 LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database\n");
260 return rc;
261 }
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700262 return SQLITE_OK;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200263}
264
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100265/* https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema */
266static bool db_table_exists(struct db_context *dbc, const char *table_name)
267{
268 const char *table_exists_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=?";
269 sqlite3_stmt *stmt;
270 int rc;
271
272 rc = sqlite3_prepare_v2(dbc->db, table_exists_sql, -1, &stmt, NULL);
273 if (rc != SQLITE_OK) {
274 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", table_exists_sql);
275 return false;
276 }
277
278 if (!db_bind_text(stmt, NULL, table_name))
279 return false;
280
281 rc = sqlite3_step(stmt);
282 db_remove_reset(stmt);
283 sqlite3_finalize(stmt);
284 return (rc == SQLITE_ROW);
285}
286
287/* Indicate whether the database is initialized with tables for schema version 0.
288 * We only check for the 'subscriber' table here because Neels said so. */
289static bool db_is_bootstrapped_v0(struct db_context *dbc)
290{
291 if (!db_table_exists(dbc, "subscriber")) {
292 LOGP(DDB, LOGL_DEBUG, "Table 'subscriber' not found in database '%s'\n", dbc->fname);
293 return false;
294 }
295
296 return true;
297}
298
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100299static int
300db_upgrade_v1(struct db_context *dbc)
301{
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100302 int rc;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100303 const char *statements[] = {
304 "ALTER TABLE subscriber ADD COLUMN last_lu_seen TIMESTAMP default NULL",
305 "PRAGMA user_version = 1",
306 };
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100307
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100308 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100309 if (rc != SQLITE_DONE) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100310 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 1\n");
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100311 return rc;
312 }
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100313 return rc;
314}
315
Oliver Smith81db3892019-01-09 12:03:51 +0100316static int db_upgrade_v2(struct db_context *dbc)
317{
Oliver Smith81db3892019-01-09 12:03:51 +0100318 int rc;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100319 const char *statements[] = {
320 "ALTER TABLE subscriber ADD COLUMN imei VARCHAR(14)",
321 "PRAGMA user_version = 2",
322 };
Oliver Smith81db3892019-01-09 12:03:51 +0100323
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100324 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
Oliver Smith81db3892019-01-09 12:03:51 +0100325 if (rc != SQLITE_DONE) {
Neels Hofmeyrf5459de2019-10-31 01:29:59 +0100326 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 2\n");
Oliver Smith81db3892019-01-09 12:03:51 +0100327 return rc;
328 }
Oliver Smith81db3892019-01-09 12:03:51 +0100329 return rc;
330}
331
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100332static int db_upgrade_v3(struct db_context *dbc)
333{
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100334 int rc;
335
336 /* A newer SQLite version would allow simply 'ATLER TABLE subscriber RENAME COLUMN hlr_number TO msc_number'.
337 * This is a really expensive workaround for that in order to cover earlier SQLite versions as well:
338 * Create a new table with the new column name and copy the data over (https://www.sqlite.org/faq.html#q11).
339 */
340#define SUBSCR_V3_CREATE \
341"(\n" \
342"-- OsmoHLR's DB scheme is modelled roughly after TS 23.008 version 13.3.0\n" \
343" id INTEGER PRIMARY KEY,\n" \
344" -- Chapter 2.1.1.1\n" \
345" imsi VARCHAR(15) UNIQUE NOT NULL,\n" \
346" -- Chapter 2.1.2\n" \
347" msisdn VARCHAR(15) UNIQUE,\n" \
348" -- Chapter 2.2.3: Most recent / current IMEISV\n" \
349" imeisv VARCHAR,\n" \
350" -- Chapter 2.1.9: Most recent / current IMEI\n" \
351" imei VARCHAR(14),\n" \
352" -- Chapter 2.4.5\n" \
353" vlr_number VARCHAR(15),\n" \
354" -- Chapter 2.4.6\n" \
355" msc_number VARCHAR(15),\n" \
356" -- Chapter 2.4.8.1\n" \
357" sgsn_number VARCHAR(15),\n" \
358" -- Chapter 2.13.10\n" \
359" sgsn_address VARCHAR,\n" \
360" -- Chapter 2.4.8.2\n" \
361" ggsn_number VARCHAR(15),\n" \
362" -- Chapter 2.4.9.2\n" \
363" gmlc_number VARCHAR(15),\n" \
364" -- Chapter 2.4.23\n" \
365" smsc_number VARCHAR(15),\n" \
366" -- Chapter 2.4.24\n" \
367" periodic_lu_tmr INTEGER,\n" \
368" -- Chapter 2.13.115\n" \
369" periodic_rau_tau_tmr INTEGER,\n" \
370" -- Chapter 2.1.1.2: network access mode\n" \
371" nam_cs BOOLEAN NOT NULL DEFAULT 1,\n" \
372" nam_ps BOOLEAN NOT NULL DEFAULT 1,\n" \
373" -- Chapter 2.1.8\n" \
374" lmsi INTEGER,\n" \
375 \
376" -- The below purged flags might not even be stored non-volatile,\n" \
377" -- refer to TS 23.012 Chapter 3.6.1.4\n" \
378" -- Chapter 2.7.5\n" \
379" ms_purged_cs BOOLEAN NOT NULL DEFAULT 0,\n" \
380" -- Chapter 2.7.6\n" \
381" ms_purged_ps BOOLEAN NOT NULL DEFAULT 0,\n" \
382 \
383" -- Timestamp of last location update seen from subscriber\n" \
384" -- The value is a string which encodes a UTC timestamp in granularity of seconds.\n" \
385" last_lu_seen TIMESTAMP default NULL\n" \
386")\n"
387
388#define SUBSCR_V2_COLUMN_NAMES \
389 "id," \
390 "imsi," \
391 "msisdn," \
392 "imeisv," \
393 "imei," \
394 "vlr_number," \
395 "hlr_number," \
396 "sgsn_number," \
397 "sgsn_address," \
398 "ggsn_number," \
399 "gmlc_number," \
400 "smsc_number," \
401 "periodic_lu_tmr," \
402 "periodic_rau_tau_tmr," \
403 "nam_cs," \
404 "nam_ps," \
405 "lmsi," \
406 "ms_purged_cs," \
407 "ms_purged_ps," \
408 "last_lu_seen"
409
410#define SUBSCR_V3_COLUMN_NAMES \
411 "id," \
412 "imsi," \
413 "msisdn," \
414 "imeisv," \
415 "imei," \
416 "vlr_number," \
417 "msc_number," \
418 "sgsn_number," \
419 "sgsn_address," \
420 "ggsn_number," \
421 "gmlc_number," \
422 "smsc_number," \
423 "periodic_lu_tmr," \
424 "periodic_rau_tau_tmr," \
425 "nam_cs," \
426 "nam_ps," \
427 "lmsi," \
428 "ms_purged_cs," \
429 "ms_purged_ps," \
430 "last_lu_seen"
431
432 const char *statements[] = {
433 "BEGIN TRANSACTION",
434 "CREATE TEMPORARY TABLE subscriber_backup" SUBSCR_V3_CREATE,
435 "INSERT INTO subscriber_backup SELECT " SUBSCR_V2_COLUMN_NAMES " FROM subscriber",
436 "DROP TABLE subscriber",
437 "CREATE TABLE subscriber" SUBSCR_V3_CREATE,
438 "INSERT INTO subscriber SELECT " SUBSCR_V3_COLUMN_NAMES " FROM subscriber_backup",
439 "DROP TABLE subscriber_backup",
440 "COMMIT",
441 "PRAGMA user_version = 3",
442 };
443
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100444 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
445 if (rc != SQLITE_DONE) {
446 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 3\n");
447 return rc;
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100448 }
449 return rc;
450}
451
Neels Hofmeyr07e16022019-11-20 02:36:35 +0100452static int db_upgrade_v4(struct db_context *dbc)
453{
454 int rc;
455 const char *statements[] = {
456 "ALTER TABLE subscriber ADD COLUMN last_lu_seen_ps TIMESTAMP default NULL",
457 "PRAGMA user_version = 4",
458 };
459
460 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
461 if (rc != SQLITE_DONE) {
462 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 4\n");
463 return rc;
464 }
465 return rc;
466}
467
Neels Hofmeyrf6c8f042019-11-25 03:59:50 +0100468static int db_upgrade_v5(struct db_context *dbc)
469{
470 int rc;
471 const char *statements[] = {
472 "ALTER TABLE subscriber ADD COLUMN vlr_via_proxy VARCHAR",
473 "ALTER TABLE subscriber ADD COLUMN sgsn_via_proxy VARCHAR",
474 "PRAGMA user_version = 5",
475 };
476
477 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
478 if (rc != SQLITE_DONE) {
479 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 5\n");
480 return rc;
481 }
482 return rc;
483}
484
Neels Hofmeyrdd73ccf2019-12-12 04:04:53 +0100485static int db_upgrade_v6(struct db_context *dbc)
486{
487 int rc;
488 const char *statements[] = {
489 "CREATE TABLE ind (\n"
490 " -- 3G auth IND pool to be used for this VLR\n"
491 " ind INTEGER PRIMARY KEY,\n"
492 " -- VLR identification, usually the GSUP source_name\n"
493 " vlr TEXT NOT NULL,\n"
494 " UNIQUE (vlr)\n"
495 ")"
496 ,
497 "PRAGMA user_version = 6",
498 };
499
500 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
501 if (rc != SQLITE_DONE) {
502 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 6\n");
503 return rc;
504 }
505 return rc;
506}
507
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100508typedef int (*db_upgrade_func_t)(struct db_context *dbc);
509static db_upgrade_func_t db_upgrade_path[] = {
510 db_upgrade_v1,
511 db_upgrade_v2,
512 db_upgrade_v3,
Neels Hofmeyr07e16022019-11-20 02:36:35 +0100513 db_upgrade_v4,
Neels Hofmeyrf6c8f042019-11-25 03:59:50 +0100514 db_upgrade_v5,
Neels Hofmeyrdd73ccf2019-12-12 04:04:53 +0100515 db_upgrade_v6,
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100516};
517
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100518static int db_get_user_version(struct db_context *dbc)
519{
520 const char *user_version_sql = "PRAGMA user_version";
521 sqlite3_stmt *stmt;
522 int version, rc;
523
524 rc = sqlite3_prepare_v2(dbc->db, user_version_sql, -1, &stmt, NULL);
525 if (rc != SQLITE_OK) {
526 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", user_version_sql);
527 return -1;
528 }
529 rc = sqlite3_step(stmt);
530 if (rc == SQLITE_ROW) {
531 version = sqlite3_column_int(stmt, 0);
532 } else {
533 LOGP(DDB, LOGL_ERROR, "SQL statement '%s' failed: %d\n", user_version_sql, rc);
534 version = -1;
535 }
536
537 db_remove_reset(stmt);
538 sqlite3_finalize(stmt);
539 return version;
540}
541
542struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite_logging, bool allow_upgrade)
Harald Weltee72cf552016-04-28 07:18:49 +0200543{
544 struct db_context *dbc = talloc_zero(ctx, struct db_context);
545 unsigned int i;
546 int rc;
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200547 bool has_sqlite_config_sqllog = false;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100548 int version;
Harald Weltee72cf552016-04-28 07:18:49 +0200549
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100550 LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
Harald Weltee72cf552016-04-28 07:18:49 +0200551 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
552 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
553
Vadim Yanitskiyfbd736e2018-07-31 22:40:30 +0700554#ifdef SQLITE_USE_TALLOC
555 /* Configure SQLite3 to use talloc memory allocator */
556 rc = db_sqlite3_use_talloc(ctx);
557 if (rc == SQLITE_OK) {
558 LOGP(DDB, LOGL_NOTICE, "SQLite3 is configured to use talloc\n");
559 } else {
560 LOGP(DDB, LOGL_ERROR, "Failed to configure SQLite3 "
561 "to use talloc, using default memory allocator\n");
562 }
563#endif
564
Harald Weltee72cf552016-04-28 07:18:49 +0200565 dbc->fname = talloc_strdup(dbc, fname);
566
567 for (i = 0; i < 0xfffff; i++) {
568 const char *o = sqlite3_compileoption_get(i);
569 if (!o)
570 break;
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200571 LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200572 if (!strcmp(o, "ENABLE_SQLLOG"))
573 has_sqlite_config_sqllog = true;
Harald Weltee72cf552016-04-28 07:18:49 +0200574 }
575
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100576 if (enable_sqlite_logging) {
577 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
578 if (rc != SQLITE_OK)
579 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
580 }
Harald Weltee72cf552016-04-28 07:18:49 +0200581
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200582 if (has_sqlite_config_sqllog) {
583 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
584 if (rc != SQLITE_OK)
585 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n");
586 } else
587 LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:"
588 " SQLite3 compiled without support for it\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200589
590 rc = sqlite3_open(dbc->fname, &dbc->db);
591 if (rc != SQLITE_OK) {
592 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
593 talloc_free(dbc);
594 return NULL;
595 }
596
597 /* enable extended result codes */
598 rc = sqlite3_extended_result_codes(dbc->db, 1);
599 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200600 LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200601
Harald Welteabd1a542016-05-03 18:50:41 +0200602 char *err_msg;
603 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
Vadim Yanitskiy15ad7be2020-02-09 05:32:46 +0700604 if (rc != SQLITE_OK) {
Harald Welteabd1a542016-05-03 18:50:41 +0200605 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
606 err_msg);
Vadim Yanitskiy15ad7be2020-02-09 05:32:46 +0700607 sqlite3_free(err_msg);
608 }
Harald Welteabd1a542016-05-03 18:50:41 +0200609
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100610 version = db_get_user_version(dbc);
611 if (version < 0) {
612 LOGP(DDB, LOGL_ERROR, "Unable to read user version number from database '%s'\n", dbc->fname);
613 goto out_free;
614 }
615
616 /* An empty database will always report version zero. */
617 if (version == 0 && !db_is_bootstrapped_v0(dbc)) {
618 LOGP(DDB, LOGL_NOTICE, "Missing database tables detected; Bootstrapping database '%s'\n", dbc->fname);
619 rc = db_bootstrap(dbc);
620 if (rc != SQLITE_OK) {
621 LOGP(DDB, LOGL_ERROR, "Failed to bootstrap DB: (rc=%d) %s\n",
622 rc, sqlite3_errmsg(dbc->db));
623 goto out_free;
624 }
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100625 version = CURRENT_SCHEMA_VERSION;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100626 }
627
628 LOGP(DDB, LOGL_NOTICE, "Database '%s' has HLR DB schema version %d\n", dbc->fname, version);
629
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100630 for (; allow_upgrade && (version < ARRAY_SIZE(db_upgrade_path)); version++) {
631 db_upgrade_func_t upgrade_func = db_upgrade_path[version];
632 rc = upgrade_func(dbc);
633 if (rc != SQLITE_DONE) {
634 LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version %d: (rc=%d) %s\n",
635 version+1, rc, sqlite3_errmsg(dbc->db));
636 goto out_free;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100637 }
638 LOGP(DDB, LOGL_NOTICE, "Database '%s' has been upgraded to HLR DB schema version %d\n",
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100639 dbc->fname, version+1);
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100640 }
641
642 if (version != CURRENT_SCHEMA_VERSION) {
643 if (version < CURRENT_SCHEMA_VERSION) {
644 LOGP(DDB, LOGL_NOTICE, "HLR DB schema version %d is outdated\n", version);
645 if (!allow_upgrade) {
646 LOGP(DDB, LOGL_ERROR, "Not upgrading HLR database to schema version %d; "
647 "use the --db-upgrade option to allow HLR database upgrades\n",
648 CURRENT_SCHEMA_VERSION);
649 }
650 } else
651 LOGP(DDB, LOGL_ERROR, "HLR DB schema version %d is unknown\n", version);
652
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700653 goto out_free;
654 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200655
Harald Weltee72cf552016-04-28 07:18:49 +0200656 /* prepare all SQL statements */
657 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
658 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
659 &dbc->stmt[i], NULL);
660 if (rc != SQLITE_OK) {
661 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
662 goto out_free;
663 }
664 }
665
666 return dbc;
667out_free:
668 db_close(dbc);
669 return NULL;
670}