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