blob: 770c3a472c1bb6b15280fa886ce17580658dd7f2 [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",
Harald Weltee72cf552016-04-28 07:18:49 +020082};
83
84static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
85{
86 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
87}
88
89static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
90{
91 switch (type) {
92 case 0:
93 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
94 break;
95 case 1:
Max58d4a842017-02-20 11:06:12 +010096 LOGP(DDB, LOGL_DEBUG, "%s\n", stmt);
Harald Weltee72cf552016-04-28 07:18:49 +020097 break;
98 case 2:
99 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
100 break;
101 default:
102 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
103 break;
104 }
105}
106
Max00b37152017-02-20 11:09:27 +0100107/* remove bindings and reset statement to be re-executed */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200108void db_remove_reset(sqlite3_stmt *stmt)
Max00b37152017-02-20 11:09:27 +0100109{
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200110 sqlite3_clear_bindings(stmt);
Neels Hofmeyr4f3841c2017-11-07 13:24:44 +0100111 /* sqlite3_reset() just repeats an error code already evaluated during sqlite3_step(). */
112 /* coverity[CHECKED_RETURN] */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +0200113 sqlite3_reset(stmt);
Max00b37152017-02-20 11:09:27 +0100114}
115
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200116/** bind text arg and do proper cleanup in case of failure. If param_name is
117 * NULL, bind to the first parameter (useful for SQL statements that have only
118 * one parameter). */
119bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
Max00b37152017-02-20 11:09:27 +0100120{
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200121 int rc;
122 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
123 if (idx < 1) {
124 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
125 param_name);
126 return false;
127 }
128 rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
Max00b37152017-02-20 11:09:27 +0100129 if (rc != SQLITE_OK) {
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200130 LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
131 param_name ? param_name : "#1", rc);
Max00b37152017-02-20 11:09:27 +0100132 db_remove_reset(stmt);
133 return false;
134 }
Max00b37152017-02-20 11:09:27 +0100135 return true;
136}
137
Neels Hofmeyr28da26e2017-10-06 03:44:57 +0200138/** bind int arg and do proper cleanup in case of failure. If param_name is
139 * NULL, bind to the first parameter (useful for SQL statements that have only
140 * one parameter). */
141bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr)
142{
143 int rc;
144 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
145 if (idx < 1) {
146 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
147 param_name);
148 return false;
149 }
150 rc = sqlite3_bind_int(stmt, idx, nr);
151 if (rc != SQLITE_OK) {
152 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
153 param_name ? param_name : "#1", rc);
154 db_remove_reset(stmt);
155 return false;
156 }
157 return true;
158}
159
160/** bind int64 arg and do proper cleanup in case of failure. If param_name is
161 * NULL, bind to the first parameter (useful for SQL statements that have only
162 * one parameter). */
163bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr)
164{
165 int rc;
166 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
167 if (idx < 1) {
168 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
169 param_name);
170 return false;
171 }
172 rc = sqlite3_bind_int64(stmt, idx, nr);
173 if (rc != SQLITE_OK) {
174 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
175 param_name ? param_name : "#1", rc);
176 db_remove_reset(stmt);
177 return false;
178 }
179 return true;
180}
181
Harald Weltee72cf552016-04-28 07:18:49 +0200182void db_close(struct db_context *dbc)
183{
184 unsigned int i;
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700185 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200186
187 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
188 /* it is ok to call finalize on NULL */
189 sqlite3_finalize(dbc->stmt[i]);
190 }
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700191
192 /* Ask sqlite3 to close DB */
193 rc = sqlite3_close(dbc->db);
194 if (rc != SQLITE_OK) { /* Make sure it's actually closed! */
195 LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n",
196 rc, sqlite3_errmsg(dbc->db));
197 }
198
Harald Weltee72cf552016-04-28 07:18:49 +0200199 talloc_free(dbc);
200}
201
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200202static int db_bootstrap(struct db_context *dbc)
203{
204 int i;
205 for (i = 0; i < ARRAY_SIZE(stmt_bootstrap_sql); i++) {
206 int rc;
207 sqlite3_stmt *stmt;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100208 rc = sqlite3_prepare_v2(dbc->db, stmt_bootstrap_sql[i], -1, &stmt, NULL);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200209 if (rc != SQLITE_OK) {
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100210 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_bootstrap_sql[i]);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700211 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200212 }
213
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200214 rc = sqlite3_step(stmt);
215 db_remove_reset(stmt);
Vadim Yanitskiydc17e052018-07-30 20:14:16 +0700216 sqlite3_finalize(stmt);
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200217 if (rc != SQLITE_DONE) {
218 LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database: SQL error: (%d) %s,"
219 " during stmt '%s'",
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100220 rc, sqlite3_errmsg(dbc->db), stmt_bootstrap_sql[i]);
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700221 return rc;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200222 }
223 }
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700224 return SQLITE_OK;
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200225}
226
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100227/* https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema */
228static bool db_table_exists(struct db_context *dbc, const char *table_name)
229{
230 const char *table_exists_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=?";
231 sqlite3_stmt *stmt;
232 int rc;
233
234 rc = sqlite3_prepare_v2(dbc->db, table_exists_sql, -1, &stmt, NULL);
235 if (rc != SQLITE_OK) {
236 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", table_exists_sql);
237 return false;
238 }
239
240 if (!db_bind_text(stmt, NULL, table_name))
241 return false;
242
243 rc = sqlite3_step(stmt);
244 db_remove_reset(stmt);
245 sqlite3_finalize(stmt);
246 return (rc == SQLITE_ROW);
247}
248
249/* Indicate whether the database is initialized with tables for schema version 0.
250 * We only check for the 'subscriber' table here because Neels said so. */
251static bool db_is_bootstrapped_v0(struct db_context *dbc)
252{
253 if (!db_table_exists(dbc, "subscriber")) {
254 LOGP(DDB, LOGL_DEBUG, "Table 'subscriber' not found in database '%s'\n", dbc->fname);
255 return false;
256 }
257
258 return true;
259}
260
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100261static int
262db_upgrade_v1(struct db_context *dbc)
263{
264 sqlite3_stmt *stmt;
265 int rc;
266 const char *update_stmt_sql = "ALTER TABLE subscriber ADD COLUMN last_lu_seen TIMESTAMP default NULL";
267 const char *set_schema_version_sql = "PRAGMA user_version = 1";
268
269 rc = sqlite3_prepare_v2(dbc->db, update_stmt_sql, -1, &stmt, NULL);
270 if (rc != SQLITE_OK) {
271 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", update_stmt_sql);
272 return rc;
273 }
274 rc = sqlite3_step(stmt);
275 db_remove_reset(stmt);
276 sqlite3_finalize(stmt);
277 if (rc != SQLITE_DONE) {
278 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1);
279 return rc;
280 }
281
282 rc = sqlite3_prepare_v2(dbc->db, set_schema_version_sql, -1, &stmt, NULL);
283 if (rc != SQLITE_OK) {
284 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", set_schema_version_sql);
285 return rc;
286 }
287 rc = sqlite3_step(stmt);
288 if (rc != SQLITE_DONE)
289 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1);
290
291 db_remove_reset(stmt);
292 sqlite3_finalize(stmt);
293 return rc;
294}
295
Oliver Smith81db3892019-01-09 12:03:51 +0100296static int db_upgrade_v2(struct db_context *dbc)
297{
298 sqlite3_stmt *stmt;
299 int rc;
300 const char *update_stmt_sql = "ALTER TABLE subscriber ADD COLUMN imei VARCHAR(14) default NULL";
301 const char *set_schema_version_sql = "PRAGMA user_version = 2";
302
303 rc = sqlite3_prepare_v2(dbc->db, update_stmt_sql, -1, &stmt, NULL);
304 if (rc != SQLITE_OK) {
305 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", update_stmt_sql);
306 return rc;
307 }
308 rc = sqlite3_step(stmt);
309 db_remove_reset(stmt);
310 sqlite3_finalize(stmt);
311 if (rc != SQLITE_DONE) {
312 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1);
313 return rc;
314 }
315
316 rc = sqlite3_prepare_v2(dbc->db, set_schema_version_sql, -1, &stmt, NULL);
317 if (rc != SQLITE_OK) {
318 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", set_schema_version_sql);
319 return rc;
320 }
321 rc = sqlite3_step(stmt);
322 if (rc != SQLITE_DONE)
323 LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1);
324
325 db_remove_reset(stmt);
326 sqlite3_finalize(stmt);
327 return rc;
328}
329
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100330static int db_get_user_version(struct db_context *dbc)
331{
332 const char *user_version_sql = "PRAGMA user_version";
333 sqlite3_stmt *stmt;
334 int version, rc;
335
336 rc = sqlite3_prepare_v2(dbc->db, user_version_sql, -1, &stmt, NULL);
337 if (rc != SQLITE_OK) {
338 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", user_version_sql);
339 return -1;
340 }
341 rc = sqlite3_step(stmt);
342 if (rc == SQLITE_ROW) {
343 version = sqlite3_column_int(stmt, 0);
344 } else {
345 LOGP(DDB, LOGL_ERROR, "SQL statement '%s' failed: %d\n", user_version_sql, rc);
346 version = -1;
347 }
348
349 db_remove_reset(stmt);
350 sqlite3_finalize(stmt);
351 return version;
352}
353
354struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite_logging, bool allow_upgrade)
Harald Weltee72cf552016-04-28 07:18:49 +0200355{
356 struct db_context *dbc = talloc_zero(ctx, struct db_context);
357 unsigned int i;
358 int rc;
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200359 bool has_sqlite_config_sqllog = false;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100360 int version;
Harald Weltee72cf552016-04-28 07:18:49 +0200361
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100362 LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
Harald Weltee72cf552016-04-28 07:18:49 +0200363 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
364 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
365
366 dbc->fname = talloc_strdup(dbc, fname);
367
368 for (i = 0; i < 0xfffff; i++) {
369 const char *o = sqlite3_compileoption_get(i);
370 if (!o)
371 break;
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200372 LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200373 if (!strcmp(o, "ENABLE_SQLLOG"))
374 has_sqlite_config_sqllog = true;
Harald Weltee72cf552016-04-28 07:18:49 +0200375 }
376
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100377 if (enable_sqlite_logging) {
378 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
379 if (rc != SQLITE_OK)
380 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
381 }
Harald Weltee72cf552016-04-28 07:18:49 +0200382
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200383 if (has_sqlite_config_sqllog) {
384 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
385 if (rc != SQLITE_OK)
386 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n");
387 } else
388 LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:"
389 " SQLite3 compiled without support for it\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200390
391 rc = sqlite3_open(dbc->fname, &dbc->db);
392 if (rc != SQLITE_OK) {
393 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
394 talloc_free(dbc);
395 return NULL;
396 }
397
398 /* enable extended result codes */
399 rc = sqlite3_extended_result_codes(dbc->db, 1);
400 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200401 LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200402
Harald Welteabd1a542016-05-03 18:50:41 +0200403 char *err_msg;
404 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
405 if (rc != SQLITE_OK)
406 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
407 err_msg);
408
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100409 version = db_get_user_version(dbc);
410 if (version < 0) {
411 LOGP(DDB, LOGL_ERROR, "Unable to read user version number from database '%s'\n", dbc->fname);
412 goto out_free;
413 }
414
415 /* An empty database will always report version zero. */
416 if (version == 0 && !db_is_bootstrapped_v0(dbc)) {
417 LOGP(DDB, LOGL_NOTICE, "Missing database tables detected; Bootstrapping database '%s'\n", dbc->fname);
418 rc = db_bootstrap(dbc);
419 if (rc != SQLITE_OK) {
420 LOGP(DDB, LOGL_ERROR, "Failed to bootstrap DB: (rc=%d) %s\n",
421 rc, sqlite3_errmsg(dbc->db));
422 goto out_free;
423 }
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100424 version = CURRENT_SCHEMA_VERSION;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100425 }
426
427 LOGP(DDB, LOGL_NOTICE, "Database '%s' has HLR DB schema version %d\n", dbc->fname, version);
428
429 if (version < CURRENT_SCHEMA_VERSION && allow_upgrade) {
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100430 switch (version) {
431 case 0:
432 rc = db_upgrade_v1(dbc);
433 if (rc != SQLITE_DONE) {
434 LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version 1: (rc=%d) %s\n",
435 rc, sqlite3_errmsg(dbc->db));
436 goto out_free;
437 }
438 version = 1;
439 /* fall through */
Oliver Smith81db3892019-01-09 12:03:51 +0100440 case 1:
441 rc = db_upgrade_v2(dbc);
442 if (rc != SQLITE_DONE) {
443 LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version 2: (rc=%d) %s\n",
444 rc, sqlite3_errmsg(dbc->db));
445 goto out_free;
446 }
447 version = 2;
448 /* fall through */
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100449 /* case N: ... */
450 default:
451 break;
452 }
453 LOGP(DDB, LOGL_NOTICE, "Database '%s' has been upgraded to HLR DB schema version %d\n",
454 dbc->fname, version);
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100455 }
456
457 if (version != CURRENT_SCHEMA_VERSION) {
458 if (version < CURRENT_SCHEMA_VERSION) {
459 LOGP(DDB, LOGL_NOTICE, "HLR DB schema version %d is outdated\n", version);
460 if (!allow_upgrade) {
461 LOGP(DDB, LOGL_ERROR, "Not upgrading HLR database to schema version %d; "
462 "use the --db-upgrade option to allow HLR database upgrades\n",
463 CURRENT_SCHEMA_VERSION);
464 }
465 } else
466 LOGP(DDB, LOGL_ERROR, "HLR DB schema version %d is unknown\n", version);
467
Vadim Yanitskiy050eb1d2018-07-30 20:48:19 +0700468 goto out_free;
469 }
Neels Hofmeyr7750d2c2017-10-24 23:26:27 +0200470
Harald Weltee72cf552016-04-28 07:18:49 +0200471 /* prepare all SQL statements */
472 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
473 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
474 &dbc->stmt[i], NULL);
475 if (rc != SQLITE_OK) {
476 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
477 goto out_free;
478 }
479 }
480
481 return dbc;
482out_free:
483 db_close(dbc);
484 return NULL;
485}