blob: 9cad263dce81a140b060948ba9cbe92332ccb796 [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! */
Oliver Smith81db3892019-01-09 12:03:51 +010031#define CURRENT_SCHEMA_VERSION 2
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",
Harald Weltee72cf552016-04-28 07:18:49 +020083};
84
85static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
86{
87 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
88}
89
90static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
91{
92 switch (type) {
93 case 0:
94 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
95 break;
96 case 1:
Max58d4a842017-02-20 11:06:12 +010097 LOGP(DDB, LOGL_DEBUG, "%s\n", stmt);
Harald Weltee72cf552016-04-28 07:18:49 +020098 break;
99 case 2:
100 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
101 break;
102 default:
103 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
104 break;
105 }
106}
107
Max00b37152017-02-20 11:09:27 +0100108/* remove bindings and reset statement to be re-executed */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200109void db_remove_reset(sqlite3_stmt *stmt)
Max00b37152017-02-20 11:09:27 +0100110{
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200111 sqlite3_clear_bindings(stmt);
Neels Hofmeyr4f3841c2017-11-07 13:24:44 +0100112 /* sqlite3_reset() just repeats an error code already evaluated during sqlite3_step(). */
113 /* coverity[CHECKED_RETURN] */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200114 sqlite3_reset(stmt);
Max00b37152017-02-20 11:09:27 +0100115}
116
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200117/** bind text arg and do proper cleanup in case of failure. If param_name is
118 * NULL, bind to the first parameter (useful for SQL statements that have only
119 * one parameter). */
120bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
Max00b37152017-02-20 11:09:27 +0100121{
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200122 int rc;
123 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
124 if (idx < 1) {
125 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
126 param_name);
127 return false;
128 }
129 rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
Max00b37152017-02-20 11:09:27 +0100130 if (rc != SQLITE_OK) {
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200131 LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
132 param_name ? param_name : "#1", rc);
Max00b37152017-02-20 11:09:27 +0100133 db_remove_reset(stmt);
134 return false;
135 }
Max00b37152017-02-20 11:09:27 +0100136 return true;
137}
138
Neels Hofmeyr28da26e2017-10-06 03:44:57 +0200139/** bind int arg and do proper cleanup in case of failure. If param_name is
140 * NULL, bind to the first parameter (useful for SQL statements that have only
141 * one parameter). */
142bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr)
143{
144 int rc;
145 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
146 if (idx < 1) {
147 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
148 param_name);
149 return false;
150 }
151 rc = sqlite3_bind_int(stmt, idx, nr);
152 if (rc != SQLITE_OK) {
153 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
154 param_name ? param_name : "#1", rc);
155 db_remove_reset(stmt);
156 return false;
157 }
158 return true;
159}
160
161/** bind int64 arg and do proper cleanup in case of failure. If param_name is
162 * NULL, bind to the first parameter (useful for SQL statements that have only
163 * one parameter). */
164bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr)
165{
166 int rc;
167 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
168 if (idx < 1) {
169 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
170 param_name);
171 return false;
172 }
173 rc = sqlite3_bind_int64(stmt, idx, nr);
174 if (rc != SQLITE_OK) {
175 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
176 param_name ? param_name : "#1", rc);
177 db_remove_reset(stmt);
178 return false;
179 }
180 return true;
181}
182
Harald Weltee72cf552016-04-28 07:18:49 +0200183void db_close(struct db_context *dbc)
184{
185 unsigned int i;
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700186 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200187
188 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
189 /* it is ok to call finalize on NULL */
190 sqlite3_finalize(dbc->stmt[i]);
191 }
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700192
193 /* Ask sqlite3 to close DB */
194 rc = sqlite3_close(dbc->db);
195 if (rc != SQLITE_OK) { /* Make sure it's actually closed! */
196 LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n",
197 rc, sqlite3_errmsg(dbc->db));
198 }
199
Harald Weltee72cf552016-04-28 07:18:49 +0200200 talloc_free(dbc);
201}
202
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200203static int db_bootstrap(struct db_context *dbc)
204{
205 int i;
206 for (i = 0; i < ARRAY_SIZE(stmt_bootstrap_sql); i++) {
207 int rc;
208 sqlite3_stmt *stmt;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100209 rc = sqlite3_prepare_v2(dbc->db, stmt_bootstrap_sql[i], -1, &stmt, NULL);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200210 if (rc != SQLITE_OK) {
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100211 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_bootstrap_sql[i]);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700212 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200213 }
214
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200215 rc = sqlite3_step(stmt);
216 db_remove_reset(stmt);
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700217 sqlite3_finalize(stmt);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200218 if (rc != SQLITE_DONE) {
219 LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database: SQL error: (%d) %s,"
220 " during stmt '%s'",
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100221 rc, sqlite3_errmsg(dbc->db), stmt_bootstrap_sql[i]);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700222 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200223 }
224 }
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700225 return SQLITE_OK;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200226}
227
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100228/* https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema */
229static bool db_table_exists(struct db_context *dbc, const char *table_name)
230{
231 const char *table_exists_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=?";
232 sqlite3_stmt *stmt;
233 int rc;
234
235 rc = sqlite3_prepare_v2(dbc->db, table_exists_sql, -1, &stmt, NULL);
236 if (rc != SQLITE_OK) {
237 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", table_exists_sql);
238 return false;
239 }
240
241 if (!db_bind_text(stmt, NULL, table_name))
242 return false;
243
244 rc = sqlite3_step(stmt);
245 db_remove_reset(stmt);
246 sqlite3_finalize(stmt);
247 return (rc == SQLITE_ROW);
248}
249
250/* Indicate whether the database is initialized with tables for schema version 0.
251 * We only check for the 'subscriber' table here because Neels said so. */
252static bool db_is_bootstrapped_v0(struct db_context *dbc)
253{
254 if (!db_table_exists(dbc, "subscriber")) {
255 LOGP(DDB, LOGL_DEBUG, "Table 'subscriber' not found in database '%s'\n", dbc->fname);
256 return false;
257 }
258
259 return true;
260}
261
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100262static int
263db_upgrade_v1(struct db_context *dbc)
264{
265 sqlite3_stmt *stmt;
266 int rc;
267 const char *update_stmt_sql = "ALTER TABLE subscriber ADD COLUMN last_lu_seen TIMESTAMP default NULL";
268 const char *set_schema_version_sql = "PRAGMA user_version = 1";
269
270 rc = sqlite3_prepare_v2(dbc->db, update_stmt_sql, -1, &stmt, NULL);
271 if (rc != SQLITE_OK) {
272 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", update_stmt_sql);
273 return rc;
274 }
275 rc = sqlite3_step(stmt);
276 db_remove_reset(stmt);
277 sqlite3_finalize(stmt);
278 if (rc != SQLITE_DONE) {
279 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1);
280 return rc;
281 }
282
283 rc = sqlite3_prepare_v2(dbc->db, set_schema_version_sql, -1, &stmt, NULL);
284 if (rc != SQLITE_OK) {
285 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", set_schema_version_sql);
286 return rc;
287 }
288 rc = sqlite3_step(stmt);
289 if (rc != SQLITE_DONE)
290 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1);
291
292 db_remove_reset(stmt);
293 sqlite3_finalize(stmt);
294 return rc;
295}
296
Oliver Smith81db3892019-01-09 12:03:51 +0100297static int db_upgrade_v2(struct db_context *dbc)
298{
299 sqlite3_stmt *stmt;
300 int rc;
301 const char *update_stmt_sql = "ALTER TABLE subscriber ADD COLUMN imei VARCHAR(14) default NULL";
302 const char *set_schema_version_sql = "PRAGMA user_version = 2";
303
304 rc = sqlite3_prepare_v2(dbc->db, update_stmt_sql, -1, &stmt, NULL);
305 if (rc != SQLITE_OK) {
306 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", update_stmt_sql);
307 return rc;
308 }
309 rc = sqlite3_step(stmt);
310 db_remove_reset(stmt);
311 sqlite3_finalize(stmt);
312 if (rc != SQLITE_DONE) {
313 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1);
314 return rc;
315 }
316
317 rc = sqlite3_prepare_v2(dbc->db, set_schema_version_sql, -1, &stmt, NULL);
318 if (rc != SQLITE_OK) {
319 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", set_schema_version_sql);
320 return rc;
321 }
322 rc = sqlite3_step(stmt);
323 if (rc != SQLITE_DONE)
324 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1);
325
326 db_remove_reset(stmt);
327 sqlite3_finalize(stmt);
328 return rc;
329}
330
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100331static int db_get_user_version(struct db_context *dbc)
332{
333 const char *user_version_sql = "PRAGMA user_version";
334 sqlite3_stmt *stmt;
335 int version, rc;
336
337 rc = sqlite3_prepare_v2(dbc->db, user_version_sql, -1, &stmt, NULL);
338 if (rc != SQLITE_OK) {
339 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", user_version_sql);
340 return -1;
341 }
342 rc = sqlite3_step(stmt);
343 if (rc == SQLITE_ROW) {
344 version = sqlite3_column_int(stmt, 0);
345 } else {
346 LOGP(DDB, LOGL_ERROR, "SQL statement '%s' failed: %d\n", user_version_sql, rc);
347 version = -1;
348 }
349
350 db_remove_reset(stmt);
351 sqlite3_finalize(stmt);
352 return version;
353}
354
355struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite_logging, bool allow_upgrade)
Harald Weltee72cf552016-04-28 07:18:49 +0200356{
357 struct db_context *dbc = talloc_zero(ctx, struct db_context);
358 unsigned int i;
359 int rc;
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200360 bool has_sqlite_config_sqllog = false;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100361 int version;
Harald Weltee72cf552016-04-28 07:18:49 +0200362
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100363 LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
Harald Weltee72cf552016-04-28 07:18:49 +0200364 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
365 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
366
367 dbc->fname = talloc_strdup(dbc, fname);
368
369 for (i = 0; i < 0xfffff; i++) {
370 const char *o = sqlite3_compileoption_get(i);
371 if (!o)
372 break;
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200373 LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200374 if (!strcmp(o, "ENABLE_SQLLOG"))
375 has_sqlite_config_sqllog = true;
Harald Weltee72cf552016-04-28 07:18:49 +0200376 }
377
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100378 if (enable_sqlite_logging) {
379 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
380 if (rc != SQLITE_OK)
381 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
382 }
Harald Weltee72cf552016-04-28 07:18:49 +0200383
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200384 if (has_sqlite_config_sqllog) {
385 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
386 if (rc != SQLITE_OK)
387 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n");
388 } else
389 LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:"
390 " SQLite3 compiled without support for it\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200391
392 rc = sqlite3_open(dbc->fname, &dbc->db);
393 if (rc != SQLITE_OK) {
394 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
395 talloc_free(dbc);
396 return NULL;
397 }
398
399 /* enable extended result codes */
400 rc = sqlite3_extended_result_codes(dbc->db, 1);
401 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200402 LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200403
Harald Welteabd1a542016-05-03 18:50:41 +0200404 char *err_msg;
405 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
406 if (rc != SQLITE_OK)
407 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
408 err_msg);
409
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100410 version = db_get_user_version(dbc);
411 if (version < 0) {
412 LOGP(DDB, LOGL_ERROR, "Unable to read user version number from database '%s'\n", dbc->fname);
413 goto out_free;
414 }
415
416 /* An empty database will always report version zero. */
417 if (version == 0 && !db_is_bootstrapped_v0(dbc)) {
418 LOGP(DDB, LOGL_NOTICE, "Missing database tables detected; Bootstrapping database '%s'\n", dbc->fname);
419 rc = db_bootstrap(dbc);
420 if (rc != SQLITE_OK) {
421 LOGP(DDB, LOGL_ERROR, "Failed to bootstrap DB: (rc=%d) %s\n",
422 rc, sqlite3_errmsg(dbc->db));
423 goto out_free;
424 }
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100425 version = CURRENT_SCHEMA_VERSION;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100426 }
427
428 LOGP(DDB, LOGL_NOTICE, "Database '%s' has HLR DB schema version %d\n", dbc->fname, version);
429
430 if (version < CURRENT_SCHEMA_VERSION && allow_upgrade) {
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100431 switch (version) {
432 case 0:
433 rc = db_upgrade_v1(dbc);
434 if (rc != SQLITE_DONE) {
435 LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version 1: (rc=%d) %s\n",
436 rc, sqlite3_errmsg(dbc->db));
437 goto out_free;
438 }
439 version = 1;
440 /* fall through */
Oliver Smith81db3892019-01-09 12:03:51 +0100441 case 1:
442 rc = db_upgrade_v2(dbc);
443 if (rc != SQLITE_DONE) {
444 LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version 2: (rc=%d) %s\n",
445 rc, sqlite3_errmsg(dbc->db));
446 goto out_free;
447 }
448 version = 2;
449 /* fall through */
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100450 /* case N: ... */
451 default:
452 break;
453 }
454 LOGP(DDB, LOGL_NOTICE, "Database '%s' has been upgraded to HLR DB schema version %d\n",
455 dbc->fname, version);
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100456 }
457
458 if (version != CURRENT_SCHEMA_VERSION) {
459 if (version < CURRENT_SCHEMA_VERSION) {
460 LOGP(DDB, LOGL_NOTICE, "HLR DB schema version %d is outdated\n", version);
461 if (!allow_upgrade) {
462 LOGP(DDB, LOGL_ERROR, "Not upgrading HLR database to schema version %d; "
463 "use the --db-upgrade option to allow HLR database upgrades\n",
464 CURRENT_SCHEMA_VERSION);
465 }
466 } else
467 LOGP(DDB, LOGL_ERROR, "HLR DB schema version %d is unknown\n", version);
468
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700469 goto out_free;
470 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200471
Harald Weltee72cf552016-04-28 07:18:49 +0200472 /* prepare all SQL statements */
473 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
474 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
475 &dbc->stmt[i], NULL);
476 if (rc != SQLITE_OK) {
477 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
478 goto out_free;
479 }
480 }
481
482 return dbc;
483out_free:
484 db_close(dbc);
485 return NULL;
486}