blob: e38bdaa886cdd0d632d75d748c86243934f88687 [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 Hofmeyr07e16022019-11-20 02:36:35 +010033#define CURRENT_SCHEMA_VERSION 4
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," \
51 "last_lu_seen_ps" \
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 Hofmeyrdd783052017-10-09 17:36:08 +020058 [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = $number WHERE id = $subscriber_id",
59 [DB_STMT_UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = $number 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",
Harald Weltee72cf552016-04-28 07:18:49 +020088};
89
90static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
91{
92 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
93}
94
95static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
96{
97 switch (type) {
98 case 0:
99 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
100 break;
101 case 1:
Max58d4a842017-02-20 11:06:12 +0100102 LOGP(DDB, LOGL_DEBUG, "%s\n", stmt);
Harald Weltee72cf552016-04-28 07:18:49 +0200103 break;
104 case 2:
105 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
106 break;
107 default:
108 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
109 break;
110 }
111}
112
Max00b37152017-02-20 11:09:27 +0100113/* remove bindings and reset statement to be re-executed */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200114void db_remove_reset(sqlite3_stmt *stmt)
Max00b37152017-02-20 11:09:27 +0100115{
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200116 sqlite3_clear_bindings(stmt);
Neels Hofmeyr4f3841c2017-11-07 13:24:44 +0100117 /* sqlite3_reset() just repeats an error code already evaluated during sqlite3_step(). */
118 /* coverity[CHECKED_RETURN] */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200119 sqlite3_reset(stmt);
Max00b37152017-02-20 11:09:27 +0100120}
121
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200122/** bind text arg and do proper cleanup in case of failure. If param_name is
123 * NULL, bind to the first parameter (useful for SQL statements that have only
124 * one parameter). */
125bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
Max00b37152017-02-20 11:09:27 +0100126{
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200127 int rc;
128 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
129 if (idx < 1) {
130 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
131 param_name);
132 return false;
133 }
134 rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
Max00b37152017-02-20 11:09:27 +0100135 if (rc != SQLITE_OK) {
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200136 LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
137 param_name ? param_name : "#1", rc);
Max00b37152017-02-20 11:09:27 +0100138 db_remove_reset(stmt);
139 return false;
140 }
Max00b37152017-02-20 11:09:27 +0100141 return true;
142}
143
Neels Hofmeyr28da26e2017-10-06 03:44:57 +0200144/** bind int arg and do proper cleanup in case of failure. If param_name is
145 * NULL, bind to the first parameter (useful for SQL statements that have only
146 * one parameter). */
147bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr)
148{
149 int rc;
150 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
151 if (idx < 1) {
152 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
153 param_name);
154 return false;
155 }
156 rc = sqlite3_bind_int(stmt, idx, nr);
157 if (rc != SQLITE_OK) {
158 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
159 param_name ? param_name : "#1", rc);
160 db_remove_reset(stmt);
161 return false;
162 }
163 return true;
164}
165
166/** bind int64 arg and do proper cleanup in case of failure. If param_name is
167 * NULL, bind to the first parameter (useful for SQL statements that have only
168 * one parameter). */
169bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr)
170{
171 int rc;
172 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
173 if (idx < 1) {
174 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
175 param_name);
176 return false;
177 }
178 rc = sqlite3_bind_int64(stmt, idx, nr);
179 if (rc != SQLITE_OK) {
180 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
181 param_name ? param_name : "#1", rc);
182 db_remove_reset(stmt);
183 return false;
184 }
185 return true;
186}
187
Harald Weltee72cf552016-04-28 07:18:49 +0200188void db_close(struct db_context *dbc)
189{
190 unsigned int i;
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700191 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200192
193 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
194 /* it is ok to call finalize on NULL */
195 sqlite3_finalize(dbc->stmt[i]);
196 }
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700197
198 /* Ask sqlite3 to close DB */
199 rc = sqlite3_close(dbc->db);
200 if (rc != SQLITE_OK) { /* Make sure it's actually closed! */
201 LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n",
202 rc, sqlite3_errmsg(dbc->db));
203 }
204
Harald Weltee72cf552016-04-28 07:18:49 +0200205 talloc_free(dbc);
206}
207
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100208static int db_run_statements(struct db_context *dbc, const char **statements, size_t statements_count)
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200209{
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100210 int rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200211 int i;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100212 for (i = 0; i < statements_count; i++) {
213 const char *stmt_str = statements[i];
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200214 sqlite3_stmt *stmt;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100215
216 rc = sqlite3_prepare_v2(dbc->db, stmt_str, -1, &stmt, NULL);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200217 if (rc != SQLITE_OK) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100218 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_str);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700219 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200220 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200221 rc = sqlite3_step(stmt);
222 db_remove_reset(stmt);
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700223 sqlite3_finalize(stmt);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200224 if (rc != SQLITE_DONE) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100225 LOGP(DDB, LOGL_ERROR, "SQL error: (%d) %s, during stmt '%s'",
226 rc, sqlite3_errmsg(dbc->db), stmt_str);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700227 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200228 }
229 }
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100230 return rc;
231}
232
233static int db_bootstrap(struct db_context *dbc)
234{
235 int rc = db_run_statements(dbc, stmt_bootstrap_sql, ARRAY_SIZE(stmt_bootstrap_sql));
236 if (rc != SQLITE_DONE) {
237 LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database\n");
238 return rc;
239 }
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700240 return SQLITE_OK;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200241}
242
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100243/* https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema */
244static bool db_table_exists(struct db_context *dbc, const char *table_name)
245{
246 const char *table_exists_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=?";
247 sqlite3_stmt *stmt;
248 int rc;
249
250 rc = sqlite3_prepare_v2(dbc->db, table_exists_sql, -1, &stmt, NULL);
251 if (rc != SQLITE_OK) {
252 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", table_exists_sql);
253 return false;
254 }
255
256 if (!db_bind_text(stmt, NULL, table_name))
257 return false;
258
259 rc = sqlite3_step(stmt);
260 db_remove_reset(stmt);
261 sqlite3_finalize(stmt);
262 return (rc == SQLITE_ROW);
263}
264
265/* Indicate whether the database is initialized with tables for schema version 0.
266 * We only check for the 'subscriber' table here because Neels said so. */
267static bool db_is_bootstrapped_v0(struct db_context *dbc)
268{
269 if (!db_table_exists(dbc, "subscriber")) {
270 LOGP(DDB, LOGL_DEBUG, "Table 'subscriber' not found in database '%s'\n", dbc->fname);
271 return false;
272 }
273
274 return true;
275}
276
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100277static int
278db_upgrade_v1(struct db_context *dbc)
279{
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100280 int rc;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100281 const char *statements[] = {
282 "ALTER TABLE subscriber ADD COLUMN last_lu_seen TIMESTAMP default NULL",
283 "PRAGMA user_version = 1",
284 };
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100285
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100286 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100287 if (rc != SQLITE_DONE) {
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100288 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 1\n");
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100289 return rc;
290 }
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100291 return rc;
292}
293
Oliver Smith81db3892019-01-09 12:03:51 +0100294static int db_upgrade_v2(struct db_context *dbc)
295{
Oliver Smith81db3892019-01-09 12:03:51 +0100296 int rc;
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100297 const char *statements[] = {
298 "ALTER TABLE subscriber ADD COLUMN imei VARCHAR(14)",
299 "PRAGMA user_version = 2",
300 };
Oliver Smith81db3892019-01-09 12:03:51 +0100301
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100302 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
Oliver Smith81db3892019-01-09 12:03:51 +0100303 if (rc != SQLITE_DONE) {
Neels Hofmeyrf5459de2019-10-31 01:29:59 +0100304 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 2\n");
Oliver Smith81db3892019-01-09 12:03:51 +0100305 return rc;
306 }
Oliver Smith81db3892019-01-09 12:03:51 +0100307 return rc;
308}
309
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100310static int db_upgrade_v3(struct db_context *dbc)
311{
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100312 int rc;
313
314 /* A newer SQLite version would allow simply 'ATLER TABLE subscriber RENAME COLUMN hlr_number TO msc_number'.
315 * This is a really expensive workaround for that in order to cover earlier SQLite versions as well:
316 * Create a new table with the new column name and copy the data over (https://www.sqlite.org/faq.html#q11).
317 */
318#define SUBSCR_V3_CREATE \
319"(\n" \
320"-- OsmoHLR's DB scheme is modelled roughly after TS 23.008 version 13.3.0\n" \
321" id INTEGER PRIMARY KEY,\n" \
322" -- Chapter 2.1.1.1\n" \
323" imsi VARCHAR(15) UNIQUE NOT NULL,\n" \
324" -- Chapter 2.1.2\n" \
325" msisdn VARCHAR(15) UNIQUE,\n" \
326" -- Chapter 2.2.3: Most recent / current IMEISV\n" \
327" imeisv VARCHAR,\n" \
328" -- Chapter 2.1.9: Most recent / current IMEI\n" \
329" imei VARCHAR(14),\n" \
330" -- Chapter 2.4.5\n" \
331" vlr_number VARCHAR(15),\n" \
332" -- Chapter 2.4.6\n" \
333" msc_number VARCHAR(15),\n" \
334" -- Chapter 2.4.8.1\n" \
335" sgsn_number VARCHAR(15),\n" \
336" -- Chapter 2.13.10\n" \
337" sgsn_address VARCHAR,\n" \
338" -- Chapter 2.4.8.2\n" \
339" ggsn_number VARCHAR(15),\n" \
340" -- Chapter 2.4.9.2\n" \
341" gmlc_number VARCHAR(15),\n" \
342" -- Chapter 2.4.23\n" \
343" smsc_number VARCHAR(15),\n" \
344" -- Chapter 2.4.24\n" \
345" periodic_lu_tmr INTEGER,\n" \
346" -- Chapter 2.13.115\n" \
347" periodic_rau_tau_tmr INTEGER,\n" \
348" -- Chapter 2.1.1.2: network access mode\n" \
349" nam_cs BOOLEAN NOT NULL DEFAULT 1,\n" \
350" nam_ps BOOLEAN NOT NULL DEFAULT 1,\n" \
351" -- Chapter 2.1.8\n" \
352" lmsi INTEGER,\n" \
353 \
354" -- The below purged flags might not even be stored non-volatile,\n" \
355" -- refer to TS 23.012 Chapter 3.6.1.4\n" \
356" -- Chapter 2.7.5\n" \
357" ms_purged_cs BOOLEAN NOT NULL DEFAULT 0,\n" \
358" -- Chapter 2.7.6\n" \
359" ms_purged_ps BOOLEAN NOT NULL DEFAULT 0,\n" \
360 \
361" -- Timestamp of last location update seen from subscriber\n" \
362" -- The value is a string which encodes a UTC timestamp in granularity of seconds.\n" \
363" last_lu_seen TIMESTAMP default NULL\n" \
364")\n"
365
366#define SUBSCR_V2_COLUMN_NAMES \
367 "id," \
368 "imsi," \
369 "msisdn," \
370 "imeisv," \
371 "imei," \
372 "vlr_number," \
373 "hlr_number," \
374 "sgsn_number," \
375 "sgsn_address," \
376 "ggsn_number," \
377 "gmlc_number," \
378 "smsc_number," \
379 "periodic_lu_tmr," \
380 "periodic_rau_tau_tmr," \
381 "nam_cs," \
382 "nam_ps," \
383 "lmsi," \
384 "ms_purged_cs," \
385 "ms_purged_ps," \
386 "last_lu_seen"
387
388#define SUBSCR_V3_COLUMN_NAMES \
389 "id," \
390 "imsi," \
391 "msisdn," \
392 "imeisv," \
393 "imei," \
394 "vlr_number," \
395 "msc_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 const char *statements[] = {
411 "BEGIN TRANSACTION",
412 "CREATE TEMPORARY TABLE subscriber_backup" SUBSCR_V3_CREATE,
413 "INSERT INTO subscriber_backup SELECT " SUBSCR_V2_COLUMN_NAMES " FROM subscriber",
414 "DROP TABLE subscriber",
415 "CREATE TABLE subscriber" SUBSCR_V3_CREATE,
416 "INSERT INTO subscriber SELECT " SUBSCR_V3_COLUMN_NAMES " FROM subscriber_backup",
417 "DROP TABLE subscriber_backup",
418 "COMMIT",
419 "PRAGMA user_version = 3",
420 };
421
Neels Hofmeyr7f4dd112019-10-31 21:23:10 +0100422 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
423 if (rc != SQLITE_DONE) {
424 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 3\n");
425 return rc;
Neels Hofmeyra8045da2019-10-31 01:19:44 +0100426 }
427 return rc;
428}
429
Neels Hofmeyr07e16022019-11-20 02:36:35 +0100430static int db_upgrade_v4(struct db_context *dbc)
431{
432 int rc;
433 const char *statements[] = {
434 "ALTER TABLE subscriber ADD COLUMN last_lu_seen_ps TIMESTAMP default NULL",
435 "PRAGMA user_version = 4",
436 };
437
438 rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements));
439 if (rc != SQLITE_DONE) {
440 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 4\n");
441 return rc;
442 }
443 return rc;
444}
445
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100446typedef int (*db_upgrade_func_t)(struct db_context *dbc);
447static db_upgrade_func_t db_upgrade_path[] = {
448 db_upgrade_v1,
449 db_upgrade_v2,
450 db_upgrade_v3,
Neels Hofmeyr07e16022019-11-20 02:36:35 +0100451 db_upgrade_v4,
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100452};
453
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100454static int db_get_user_version(struct db_context *dbc)
455{
456 const char *user_version_sql = "PRAGMA user_version";
457 sqlite3_stmt *stmt;
458 int version, rc;
459
460 rc = sqlite3_prepare_v2(dbc->db, user_version_sql, -1, &stmt, NULL);
461 if (rc != SQLITE_OK) {
462 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", user_version_sql);
463 return -1;
464 }
465 rc = sqlite3_step(stmt);
466 if (rc == SQLITE_ROW) {
467 version = sqlite3_column_int(stmt, 0);
468 } else {
469 LOGP(DDB, LOGL_ERROR, "SQL statement '%s' failed: %d\n", user_version_sql, rc);
470 version = -1;
471 }
472
473 db_remove_reset(stmt);
474 sqlite3_finalize(stmt);
475 return version;
476}
477
478struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite_logging, bool allow_upgrade)
Harald Weltee72cf552016-04-28 07:18:49 +0200479{
480 struct db_context *dbc = talloc_zero(ctx, struct db_context);
481 unsigned int i;
482 int rc;
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200483 bool has_sqlite_config_sqllog = false;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100484 int version;
Harald Weltee72cf552016-04-28 07:18:49 +0200485
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100486 LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
Harald Weltee72cf552016-04-28 07:18:49 +0200487 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
488 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
489
Vadim Yanitskiyfbd736e2018-07-31 22:40:30 +0700490#ifdef SQLITE_USE_TALLOC
491 /* Configure SQLite3 to use talloc memory allocator */
492 rc = db_sqlite3_use_talloc(ctx);
493 if (rc == SQLITE_OK) {
494 LOGP(DDB, LOGL_NOTICE, "SQLite3 is configured to use talloc\n");
495 } else {
496 LOGP(DDB, LOGL_ERROR, "Failed to configure SQLite3 "
497 "to use talloc, using default memory allocator\n");
498 }
499#endif
500
Harald Weltee72cf552016-04-28 07:18:49 +0200501 dbc->fname = talloc_strdup(dbc, fname);
502
503 for (i = 0; i < 0xfffff; i++) {
504 const char *o = sqlite3_compileoption_get(i);
505 if (!o)
506 break;
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200507 LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200508 if (!strcmp(o, "ENABLE_SQLLOG"))
509 has_sqlite_config_sqllog = true;
Harald Weltee72cf552016-04-28 07:18:49 +0200510 }
511
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100512 if (enable_sqlite_logging) {
513 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
514 if (rc != SQLITE_OK)
515 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
516 }
Harald Weltee72cf552016-04-28 07:18:49 +0200517
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200518 if (has_sqlite_config_sqllog) {
519 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
520 if (rc != SQLITE_OK)
521 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n");
522 } else
523 LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:"
524 " SQLite3 compiled without support for it\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200525
526 rc = sqlite3_open(dbc->fname, &dbc->db);
527 if (rc != SQLITE_OK) {
528 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
529 talloc_free(dbc);
530 return NULL;
531 }
532
533 /* enable extended result codes */
534 rc = sqlite3_extended_result_codes(dbc->db, 1);
535 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200536 LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200537
Harald Welteabd1a542016-05-03 18:50:41 +0200538 char *err_msg;
539 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
540 if (rc != SQLITE_OK)
541 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
542 err_msg);
543
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100544 version = db_get_user_version(dbc);
545 if (version < 0) {
546 LOGP(DDB, LOGL_ERROR, "Unable to read user version number from database '%s'\n", dbc->fname);
547 goto out_free;
548 }
549
550 /* An empty database will always report version zero. */
551 if (version == 0 && !db_is_bootstrapped_v0(dbc)) {
552 LOGP(DDB, LOGL_NOTICE, "Missing database tables detected; Bootstrapping database '%s'\n", dbc->fname);
553 rc = db_bootstrap(dbc);
554 if (rc != SQLITE_OK) {
555 LOGP(DDB, LOGL_ERROR, "Failed to bootstrap DB: (rc=%d) %s\n",
556 rc, sqlite3_errmsg(dbc->db));
557 goto out_free;
558 }
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100559 version = CURRENT_SCHEMA_VERSION;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100560 }
561
562 LOGP(DDB, LOGL_NOTICE, "Database '%s' has HLR DB schema version %d\n", dbc->fname, version);
563
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100564 for (; allow_upgrade && (version < ARRAY_SIZE(db_upgrade_path)); version++) {
565 db_upgrade_func_t upgrade_func = db_upgrade_path[version];
566 rc = upgrade_func(dbc);
567 if (rc != SQLITE_DONE) {
568 LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version %d: (rc=%d) %s\n",
569 version+1, rc, sqlite3_errmsg(dbc->db));
570 goto out_free;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100571 }
572 LOGP(DDB, LOGL_NOTICE, "Database '%s' has been upgraded to HLR DB schema version %d\n",
Neels Hofmeyr981e1262019-11-05 02:00:19 +0100573 dbc->fname, version+1);
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100574 }
575
576 if (version != CURRENT_SCHEMA_VERSION) {
577 if (version < CURRENT_SCHEMA_VERSION) {
578 LOGP(DDB, LOGL_NOTICE, "HLR DB schema version %d is outdated\n", version);
579 if (!allow_upgrade) {
580 LOGP(DDB, LOGL_ERROR, "Not upgrading HLR database to schema version %d; "
581 "use the --db-upgrade option to allow HLR database upgrades\n",
582 CURRENT_SCHEMA_VERSION);
583 }
584 } else
585 LOGP(DDB, LOGL_ERROR, "HLR DB schema version %d is unknown\n", version);
586
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700587 goto out_free;
588 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200589
Harald Weltee72cf552016-04-28 07:18:49 +0200590 /* prepare all SQL statements */
591 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
592 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
593 &dbc->stmt[i], NULL);
594 if (rc != SQLITE_OK) {
595 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
596 goto out_free;
597 }
598 }
599
600 return dbc;
601out_free:
602 db_close(dbc);
603 return NULL;
604}