Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 1 | /* (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 | |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 22 | #include <stdbool.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 23 | #include <sqlite3.h> |
Neels Hofmeyr | cd83b8a | 2017-10-06 04:20:37 +0200 | [diff] [blame] | 24 | #include <string.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 25 | |
| 26 | #include "logging.h" |
| 27 | #include "db.h" |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 28 | #include "db_bootstrap.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 29 | |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame^] | 30 | /* This constant is currently duplicated in sql/hlr.sql and must be kept in sync! */ |
| 31 | #define CURRENT_SCHEMA_VERSION 1 |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 32 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 33 | #define SEL_COLUMNS \ |
| 34 | "id," \ |
| 35 | "imsi," \ |
| 36 | "msisdn," \ |
| 37 | "vlr_number," \ |
| 38 | "sgsn_number," \ |
| 39 | "sgsn_address," \ |
| 40 | "periodic_lu_tmr," \ |
| 41 | "periodic_rau_tau_tmr," \ |
| 42 | "nam_cs," \ |
| 43 | "nam_ps," \ |
| 44 | "lmsi," \ |
| 45 | "ms_purged_cs," \ |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame^] | 46 | "ms_purged_ps," \ |
| 47 | "last_lu_seen" |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 48 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 49 | static const char *stmt_sql[] = { |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 50 | [DB_STMT_SEL_BY_IMSI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imsi = ?", |
| 51 | [DB_STMT_SEL_BY_MSISDN] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE msisdn = ?", |
| 52 | [DB_STMT_SEL_BY_ID] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE id = ?", |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 53 | [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = $number WHERE id = $subscriber_id", |
| 54 | [DB_STMT_UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = $number WHERE id = $subscriber_id", |
Neels Hofmeyr | 0cac0a0 | 2017-10-09 17:47:21 +0200 | [diff] [blame] | 55 | [DB_STMT_AUC_BY_IMSI] = |
| 56 | "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn, ind_bitlen" |
| 57 | " FROM subscriber" |
| 58 | " LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id" |
| 59 | " LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id" |
Neels Hofmeyr | c5122f2 | 2017-10-09 23:12:57 +0200 | [diff] [blame] | 60 | " WHERE imsi = $imsi", |
Neels Hofmeyr | 1cbdb70 | 2017-10-09 23:03:57 +0200 | [diff] [blame] | 61 | [DB_STMT_AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = $sqn WHERE subscriber_id = $subscriber_id", |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 62 | [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs = $val WHERE imsi = $imsi", |
| 63 | [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps = $val WHERE imsi = $imsi", |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 64 | [DB_STMT_UPD_NAM_CS_BY_IMSI] = "UPDATE subscriber SET nam_cs = $val WHERE imsi = $imsi", |
| 65 | [DB_STMT_UPD_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps = $val WHERE imsi = $imsi", |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 66 | [DB_STMT_SUBSCR_CREATE] = "INSERT INTO subscriber (imsi) VALUES ($imsi)", |
| 67 | [DB_STMT_DEL_BY_ID] = "DELETE FROM subscriber WHERE id = $subscriber_id", |
| 68 | [DB_STMT_SET_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = $msisdn WHERE imsi = $imsi", |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 69 | [DB_STMT_DELETE_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = NULL WHERE imsi = $imsi", |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 70 | [DB_STMT_AUC_2G_INSERT] = |
| 71 | "INSERT INTO auc_2g (subscriber_id, algo_id_2g, ki)" |
| 72 | " VALUES($subscriber_id, $algo_id_2g, $ki)", |
| 73 | [DB_STMT_AUC_2G_DELETE] = "DELETE FROM auc_2g WHERE subscriber_id = $subscriber_id", |
| 74 | [DB_STMT_AUC_3G_INSERT] = |
| 75 | "INSERT INTO auc_3g (subscriber_id, algo_id_3g, k, op, opc, ind_bitlen)" |
| 76 | " VALUES($subscriber_id, $algo_id_3g, $k, $op, $opc, $ind_bitlen)", |
| 77 | [DB_STMT_AUC_3G_DELETE] = "DELETE FROM auc_3g WHERE subscriber_id = $subscriber_id", |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame^] | 78 | [DB_STMT_SET_LAST_LU_SEEN] = "UPDATE subscriber SET last_lu_seen = datetime($val, 'unixepoch') WHERE id = $subscriber_id", |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | static void sql3_error_log_cb(void *arg, int err_code, const char *msg) |
| 82 | { |
| 83 | LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg); |
| 84 | } |
| 85 | |
| 86 | static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type) |
| 87 | { |
| 88 | switch (type) { |
| 89 | case 0: |
| 90 | LOGP(DDB, LOGL_DEBUG, "Opened database\n"); |
| 91 | break; |
| 92 | case 1: |
Max | 58d4a84 | 2017-02-20 11:06:12 +0100 | [diff] [blame] | 93 | LOGP(DDB, LOGL_DEBUG, "%s\n", stmt); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 94 | break; |
| 95 | case 2: |
| 96 | LOGP(DDB, LOGL_DEBUG, "Closed database\n"); |
| 97 | break; |
| 98 | default: |
| 99 | LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type); |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 104 | /* remove bindings and reset statement to be re-executed */ |
Neels Hofmeyr | d7d9697 | 2017-10-06 03:50:30 +0200 | [diff] [blame] | 105 | void db_remove_reset(sqlite3_stmt *stmt) |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 106 | { |
Neels Hofmeyr | d7d9697 | 2017-10-06 03:50:30 +0200 | [diff] [blame] | 107 | sqlite3_clear_bindings(stmt); |
Neels Hofmeyr | 4f3841c | 2017-11-07 13:24:44 +0100 | [diff] [blame] | 108 | /* sqlite3_reset() just repeats an error code already evaluated during sqlite3_step(). */ |
| 109 | /* coverity[CHECKED_RETURN] */ |
Neels Hofmeyr | d7d9697 | 2017-10-06 03:50:30 +0200 | [diff] [blame] | 110 | sqlite3_reset(stmt); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 113 | /** bind text arg and do proper cleanup in case of failure. If param_name is |
| 114 | * NULL, bind to the first parameter (useful for SQL statements that have only |
| 115 | * one parameter). */ |
| 116 | bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text) |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 117 | { |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 118 | int rc; |
| 119 | int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1; |
| 120 | if (idx < 1) { |
| 121 | LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n", |
| 122 | param_name); |
| 123 | return false; |
| 124 | } |
| 125 | rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 126 | if (rc != SQLITE_OK) { |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 127 | LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n", |
| 128 | param_name ? param_name : "#1", rc); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 129 | db_remove_reset(stmt); |
| 130 | return false; |
| 131 | } |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
Neels Hofmeyr | 28da26e | 2017-10-06 03:44:57 +0200 | [diff] [blame] | 135 | /** bind int arg and do proper cleanup in case of failure. If param_name is |
| 136 | * NULL, bind to the first parameter (useful for SQL statements that have only |
| 137 | * one parameter). */ |
| 138 | bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr) |
| 139 | { |
| 140 | int rc; |
| 141 | int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1; |
| 142 | if (idx < 1) { |
| 143 | LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n", |
| 144 | param_name); |
| 145 | return false; |
| 146 | } |
| 147 | rc = sqlite3_bind_int(stmt, idx, nr); |
| 148 | if (rc != SQLITE_OK) { |
| 149 | LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n", |
| 150 | param_name ? param_name : "#1", rc); |
| 151 | db_remove_reset(stmt); |
| 152 | return false; |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | /** bind int64 arg and do proper cleanup in case of failure. If param_name is |
| 158 | * NULL, bind to the first parameter (useful for SQL statements that have only |
| 159 | * one parameter). */ |
| 160 | bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr) |
| 161 | { |
| 162 | int rc; |
| 163 | int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1; |
| 164 | if (idx < 1) { |
| 165 | LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n", |
| 166 | param_name); |
| 167 | return false; |
| 168 | } |
| 169 | rc = sqlite3_bind_int64(stmt, idx, nr); |
| 170 | if (rc != SQLITE_OK) { |
| 171 | LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n", |
| 172 | param_name ? param_name : "#1", rc); |
| 173 | db_remove_reset(stmt); |
| 174 | return false; |
| 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 179 | void db_close(struct db_context *dbc) |
| 180 | { |
| 181 | unsigned int i; |
Vadim Yanitskiy | dc17e05 | 2018-07-30 20:14:16 +0700 | [diff] [blame] | 182 | int rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 183 | |
| 184 | for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) { |
| 185 | /* it is ok to call finalize on NULL */ |
| 186 | sqlite3_finalize(dbc->stmt[i]); |
| 187 | } |
Vadim Yanitskiy | dc17e05 | 2018-07-30 20:14:16 +0700 | [diff] [blame] | 188 | |
| 189 | /* Ask sqlite3 to close DB */ |
| 190 | rc = sqlite3_close(dbc->db); |
| 191 | if (rc != SQLITE_OK) { /* Make sure it's actually closed! */ |
| 192 | LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n", |
| 193 | rc, sqlite3_errmsg(dbc->db)); |
| 194 | } |
| 195 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 196 | talloc_free(dbc); |
| 197 | } |
| 198 | |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 199 | static int db_bootstrap(struct db_context *dbc) |
| 200 | { |
| 201 | int i; |
| 202 | for (i = 0; i < ARRAY_SIZE(stmt_bootstrap_sql); i++) { |
| 203 | int rc; |
| 204 | sqlite3_stmt *stmt; |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 205 | rc = sqlite3_prepare_v2(dbc->db, stmt_bootstrap_sql[i], -1, &stmt, NULL); |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 206 | if (rc != SQLITE_OK) { |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 207 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_bootstrap_sql[i]); |
Vadim Yanitskiy | 050eb1d | 2018-07-30 20:48:19 +0700 | [diff] [blame] | 208 | return rc; |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 211 | rc = sqlite3_step(stmt); |
| 212 | db_remove_reset(stmt); |
Vadim Yanitskiy | dc17e05 | 2018-07-30 20:14:16 +0700 | [diff] [blame] | 213 | sqlite3_finalize(stmt); |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 214 | if (rc != SQLITE_DONE) { |
| 215 | LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database: SQL error: (%d) %s," |
| 216 | " during stmt '%s'", |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 217 | rc, sqlite3_errmsg(dbc->db), stmt_bootstrap_sql[i]); |
Vadim Yanitskiy | 050eb1d | 2018-07-30 20:48:19 +0700 | [diff] [blame] | 218 | return rc; |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 219 | } |
| 220 | } |
Vadim Yanitskiy | 050eb1d | 2018-07-30 20:48:19 +0700 | [diff] [blame] | 221 | return SQLITE_OK; |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 224 | /* https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema */ |
| 225 | static bool db_table_exists(struct db_context *dbc, const char *table_name) |
| 226 | { |
| 227 | const char *table_exists_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=?"; |
| 228 | sqlite3_stmt *stmt; |
| 229 | int rc; |
| 230 | |
| 231 | rc = sqlite3_prepare_v2(dbc->db, table_exists_sql, -1, &stmt, NULL); |
| 232 | if (rc != SQLITE_OK) { |
| 233 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", table_exists_sql); |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | if (!db_bind_text(stmt, NULL, table_name)) |
| 238 | return false; |
| 239 | |
| 240 | rc = sqlite3_step(stmt); |
| 241 | db_remove_reset(stmt); |
| 242 | sqlite3_finalize(stmt); |
| 243 | return (rc == SQLITE_ROW); |
| 244 | } |
| 245 | |
| 246 | /* Indicate whether the database is initialized with tables for schema version 0. |
| 247 | * We only check for the 'subscriber' table here because Neels said so. */ |
| 248 | static bool db_is_bootstrapped_v0(struct db_context *dbc) |
| 249 | { |
| 250 | if (!db_table_exists(dbc, "subscriber")) { |
| 251 | LOGP(DDB, LOGL_DEBUG, "Table 'subscriber' not found in database '%s'\n", dbc->fname); |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | return true; |
| 256 | } |
| 257 | |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame^] | 258 | static int |
| 259 | db_upgrade_v1(struct db_context *dbc) |
| 260 | { |
| 261 | sqlite3_stmt *stmt; |
| 262 | int rc; |
| 263 | const char *update_stmt_sql = "ALTER TABLE subscriber ADD COLUMN last_lu_seen TIMESTAMP default NULL"; |
| 264 | const char *set_schema_version_sql = "PRAGMA user_version = 1"; |
| 265 | |
| 266 | rc = sqlite3_prepare_v2(dbc->db, update_stmt_sql, -1, &stmt, NULL); |
| 267 | if (rc != SQLITE_OK) { |
| 268 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", update_stmt_sql); |
| 269 | return rc; |
| 270 | } |
| 271 | rc = sqlite3_step(stmt); |
| 272 | db_remove_reset(stmt); |
| 273 | sqlite3_finalize(stmt); |
| 274 | if (rc != SQLITE_DONE) { |
| 275 | LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1); |
| 276 | return rc; |
| 277 | } |
| 278 | |
| 279 | rc = sqlite3_prepare_v2(dbc->db, set_schema_version_sql, -1, &stmt, NULL); |
| 280 | if (rc != SQLITE_OK) { |
| 281 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", set_schema_version_sql); |
| 282 | return rc; |
| 283 | } |
| 284 | rc = sqlite3_step(stmt); |
| 285 | if (rc != SQLITE_DONE) |
| 286 | LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version %d\n", 1); |
| 287 | |
| 288 | db_remove_reset(stmt); |
| 289 | sqlite3_finalize(stmt); |
| 290 | return rc; |
| 291 | } |
| 292 | |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 293 | static int db_get_user_version(struct db_context *dbc) |
| 294 | { |
| 295 | const char *user_version_sql = "PRAGMA user_version"; |
| 296 | sqlite3_stmt *stmt; |
| 297 | int version, rc; |
| 298 | |
| 299 | rc = sqlite3_prepare_v2(dbc->db, user_version_sql, -1, &stmt, NULL); |
| 300 | if (rc != SQLITE_OK) { |
| 301 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", user_version_sql); |
| 302 | return -1; |
| 303 | } |
| 304 | rc = sqlite3_step(stmt); |
| 305 | if (rc == SQLITE_ROW) { |
| 306 | version = sqlite3_column_int(stmt, 0); |
| 307 | } else { |
| 308 | LOGP(DDB, LOGL_ERROR, "SQL statement '%s' failed: %d\n", user_version_sql, rc); |
| 309 | version = -1; |
| 310 | } |
| 311 | |
| 312 | db_remove_reset(stmt); |
| 313 | sqlite3_finalize(stmt); |
| 314 | return version; |
| 315 | } |
| 316 | |
| 317 | struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite_logging, bool allow_upgrade) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 318 | { |
| 319 | struct db_context *dbc = talloc_zero(ctx, struct db_context); |
| 320 | unsigned int i; |
| 321 | int rc; |
Neels Hofmeyr | cd83b8a | 2017-10-06 04:20:37 +0200 | [diff] [blame] | 322 | bool has_sqlite_config_sqllog = false; |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 323 | int version; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 324 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 325 | LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 326 | LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION); |
| 327 | LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion()); |
| 328 | |
| 329 | dbc->fname = talloc_strdup(dbc, fname); |
| 330 | |
| 331 | for (i = 0; i < 0xfffff; i++) { |
| 332 | const char *o = sqlite3_compileoption_get(i); |
| 333 | if (!o) |
| 334 | break; |
Neels Hofmeyr | e9c0c5b | 2017-10-10 16:52:22 +0200 | [diff] [blame] | 335 | LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o); |
Neels Hofmeyr | cd83b8a | 2017-10-06 04:20:37 +0200 | [diff] [blame] | 336 | if (!strcmp(o, "ENABLE_SQLLOG")) |
| 337 | has_sqlite_config_sqllog = true; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Neels Hofmeyr | d3814b9 | 2017-11-21 12:28:07 +0100 | [diff] [blame] | 340 | if (enable_sqlite_logging) { |
| 341 | rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL); |
| 342 | if (rc != SQLITE_OK) |
| 343 | LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n"); |
| 344 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 345 | |
Neels Hofmeyr | cd83b8a | 2017-10-06 04:20:37 +0200 | [diff] [blame] | 346 | if (has_sqlite_config_sqllog) { |
| 347 | rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL); |
| 348 | if (rc != SQLITE_OK) |
| 349 | LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n"); |
| 350 | } else |
| 351 | LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:" |
| 352 | " SQLite3 compiled without support for it\n"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 353 | |
| 354 | rc = sqlite3_open(dbc->fname, &dbc->db); |
| 355 | if (rc != SQLITE_OK) { |
| 356 | LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc); |
| 357 | talloc_free(dbc); |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | /* enable extended result codes */ |
| 362 | rc = sqlite3_extended_result_codes(dbc->db, 1); |
| 363 | if (rc != SQLITE_OK) |
Neels Hofmeyr | e9c0c5b | 2017-10-10 16:52:22 +0200 | [diff] [blame] | 364 | LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 365 | |
Harald Welte | abd1a54 | 2016-05-03 18:50:41 +0200 | [diff] [blame] | 366 | char *err_msg; |
| 367 | rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg); |
| 368 | if (rc != SQLITE_OK) |
| 369 | LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n", |
| 370 | err_msg); |
| 371 | |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 372 | version = db_get_user_version(dbc); |
| 373 | if (version < 0) { |
| 374 | LOGP(DDB, LOGL_ERROR, "Unable to read user version number from database '%s'\n", dbc->fname); |
| 375 | goto out_free; |
| 376 | } |
| 377 | |
| 378 | /* An empty database will always report version zero. */ |
| 379 | if (version == 0 && !db_is_bootstrapped_v0(dbc)) { |
| 380 | LOGP(DDB, LOGL_NOTICE, "Missing database tables detected; Bootstrapping database '%s'\n", dbc->fname); |
| 381 | rc = db_bootstrap(dbc); |
| 382 | if (rc != SQLITE_OK) { |
| 383 | LOGP(DDB, LOGL_ERROR, "Failed to bootstrap DB: (rc=%d) %s\n", |
| 384 | rc, sqlite3_errmsg(dbc->db)); |
| 385 | goto out_free; |
| 386 | } |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame^] | 387 | version = CURRENT_SCHEMA_VERSION; |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | LOGP(DDB, LOGL_NOTICE, "Database '%s' has HLR DB schema version %d\n", dbc->fname, version); |
| 391 | |
| 392 | if (version < CURRENT_SCHEMA_VERSION && allow_upgrade) { |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame^] | 393 | switch (version) { |
| 394 | case 0: |
| 395 | rc = db_upgrade_v1(dbc); |
| 396 | if (rc != SQLITE_DONE) { |
| 397 | LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version 1: (rc=%d) %s\n", |
| 398 | rc, sqlite3_errmsg(dbc->db)); |
| 399 | goto out_free; |
| 400 | } |
| 401 | version = 1; |
| 402 | /* fall through */ |
| 403 | /* case N: ... */ |
| 404 | default: |
| 405 | break; |
| 406 | } |
| 407 | LOGP(DDB, LOGL_NOTICE, "Database '%s' has been upgraded to HLR DB schema version %d\n", |
| 408 | dbc->fname, version); |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | if (version != CURRENT_SCHEMA_VERSION) { |
| 412 | if (version < CURRENT_SCHEMA_VERSION) { |
| 413 | LOGP(DDB, LOGL_NOTICE, "HLR DB schema version %d is outdated\n", version); |
| 414 | if (!allow_upgrade) { |
| 415 | LOGP(DDB, LOGL_ERROR, "Not upgrading HLR database to schema version %d; " |
| 416 | "use the --db-upgrade option to allow HLR database upgrades\n", |
| 417 | CURRENT_SCHEMA_VERSION); |
| 418 | } |
| 419 | } else |
| 420 | LOGP(DDB, LOGL_ERROR, "HLR DB schema version %d is unknown\n", version); |
| 421 | |
Vadim Yanitskiy | 050eb1d | 2018-07-30 20:48:19 +0700 | [diff] [blame] | 422 | goto out_free; |
| 423 | } |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 424 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 425 | /* prepare all SQL statements */ |
| 426 | for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) { |
| 427 | rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1, |
| 428 | &dbc->stmt[i], NULL); |
| 429 | if (rc != SQLITE_OK) { |
| 430 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]); |
| 431 | goto out_free; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return dbc; |
| 436 | out_free: |
| 437 | db_close(dbc); |
| 438 | return NULL; |
| 439 | } |