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 | |
Neels Hofmeyr | 2f75803 | 2019-11-20 00:37:07 +0100 | [diff] [blame] | 26 | #include <osmocom/hlr/logging.h> |
| 27 | #include <osmocom/hlr/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! */ |
Neels Hofmeyr | 04c2375 | 2019-11-25 03:59:50 +0100 | [diff] [blame] | 31 | #define CURRENT_SCHEMA_VERSION 5 |
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," \ |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 48 | "last_lu_seen," \ |
Neels Hofmeyr | 04c2375 | 2019-11-25 03:59:50 +0100 | [diff] [blame] | 49 | "last_lu_seen_ps," \ |
| 50 | "vlr_via_proxy," \ |
| 51 | "sgsn_via_proxy" |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 52 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 53 | static const char *stmt_sql[] = { |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 54 | [DB_STMT_SEL_BY_IMSI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imsi = ?", |
| 55 | [DB_STMT_SEL_BY_MSISDN] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE msisdn = ?", |
| 56 | [DB_STMT_SEL_BY_ID] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE id = ?", |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 57 | [DB_STMT_SEL_BY_IMEI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imei = ?", |
Neels Hofmeyr | 04c2375 | 2019-11-25 03:59:50 +0100 | [diff] [blame] | 58 | [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = $number, vlr_via_proxy = $proxy WHERE id = $subscriber_id", |
| 59 | [DB_STMT_UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = $number, sgsn_via_proxy = $proxy WHERE id = $subscriber_id", |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 60 | [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] | 61 | [DB_STMT_AUC_BY_IMSI] = |
| 62 | "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn, ind_bitlen" |
| 63 | " FROM subscriber" |
| 64 | " LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id" |
| 65 | " LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id" |
Neels Hofmeyr | c5122f2 | 2017-10-09 23:12:57 +0200 | [diff] [blame] | 66 | " WHERE imsi = $imsi", |
Neels Hofmeyr | 1cbdb70 | 2017-10-09 23:03:57 +0200 | [diff] [blame] | 67 | [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] | 68 | [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs = $val WHERE imsi = $imsi", |
| 69 | [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps = $val WHERE imsi = $imsi", |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 70 | [DB_STMT_UPD_NAM_CS_BY_IMSI] = "UPDATE subscriber SET nam_cs = $val WHERE imsi = $imsi", |
| 71 | [DB_STMT_UPD_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps = $val WHERE imsi = $imsi", |
Oliver Smith | cd2af5e | 2019-03-06 13:17:39 +0100 | [diff] [blame] | 72 | [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] | 73 | [DB_STMT_DEL_BY_ID] = "DELETE FROM subscriber WHERE id = $subscriber_id", |
| 74 | [DB_STMT_SET_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = $msisdn WHERE imsi = $imsi", |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 75 | [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] | 76 | [DB_STMT_AUC_2G_INSERT] = |
| 77 | "INSERT INTO auc_2g (subscriber_id, algo_id_2g, ki)" |
| 78 | " VALUES($subscriber_id, $algo_id_2g, $ki)", |
| 79 | [DB_STMT_AUC_2G_DELETE] = "DELETE FROM auc_2g WHERE subscriber_id = $subscriber_id", |
| 80 | [DB_STMT_AUC_3G_INSERT] = |
| 81 | "INSERT INTO auc_3g (subscriber_id, algo_id_3g, k, op, opc, ind_bitlen)" |
| 82 | " VALUES($subscriber_id, $algo_id_3g, $k, $op, $opc, $ind_bitlen)", |
| 83 | [DB_STMT_AUC_3G_DELETE] = "DELETE FROM auc_3g WHERE subscriber_id = $subscriber_id", |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 84 | [DB_STMT_SET_LAST_LU_SEEN] = "UPDATE subscriber SET last_lu_seen = datetime($val, 'unixepoch') WHERE id = $subscriber_id", |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 85 | [DB_STMT_SET_LAST_LU_SEEN_PS] = "UPDATE subscriber SET last_lu_seen_ps = datetime($val, 'unixepoch') WHERE id = $subscriber_id", |
Oliver Smith | 6b73fd9 | 2019-03-06 13:49:05 +0100 | [diff] [blame] | 86 | [DB_STMT_EXISTS_BY_IMSI] = "SELECT 1 FROM subscriber WHERE imsi = $imsi", |
Vadim Yanitskiy | c13599d | 2019-03-30 17:03:42 +0700 | [diff] [blame] | 87 | [DB_STMT_EXISTS_BY_MSISDN] = "SELECT 1 FROM subscriber WHERE msisdn = $msisdn", |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | static void sql3_error_log_cb(void *arg, int err_code, const char *msg) |
| 91 | { |
| 92 | LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg); |
| 93 | } |
| 94 | |
| 95 | static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type) |
| 96 | { |
| 97 | switch (type) { |
| 98 | case 0: |
| 99 | LOGP(DDB, LOGL_DEBUG, "Opened database\n"); |
| 100 | break; |
| 101 | case 1: |
Max | 58d4a84 | 2017-02-20 11:06:12 +0100 | [diff] [blame] | 102 | LOGP(DDB, LOGL_DEBUG, "%s\n", stmt); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 103 | break; |
| 104 | case 2: |
| 105 | LOGP(DDB, LOGL_DEBUG, "Closed database\n"); |
| 106 | break; |
| 107 | default: |
| 108 | LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type); |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 113 | /* remove bindings and reset statement to be re-executed */ |
Neels Hofmeyr | d7d9697 | 2017-10-06 03:50:30 +0200 | [diff] [blame] | 114 | void db_remove_reset(sqlite3_stmt *stmt) |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 115 | { |
Neels Hofmeyr | d7d9697 | 2017-10-06 03:50:30 +0200 | [diff] [blame] | 116 | sqlite3_clear_bindings(stmt); |
Neels Hofmeyr | 4f3841c | 2017-11-07 13:24:44 +0100 | [diff] [blame] | 117 | /* sqlite3_reset() just repeats an error code already evaluated during sqlite3_step(). */ |
| 118 | /* coverity[CHECKED_RETURN] */ |
Neels Hofmeyr | d7d9697 | 2017-10-06 03:50:30 +0200 | [diff] [blame] | 119 | sqlite3_reset(stmt); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 122 | /** bind text arg and do proper cleanup in case of failure. If param_name is |
| 123 | * NULL, bind to the first parameter (useful for SQL statements that have only |
| 124 | * one parameter). */ |
| 125 | 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] | 126 | { |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 127 | int rc; |
| 128 | int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1; |
| 129 | if (idx < 1) { |
| 130 | LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n", |
| 131 | param_name); |
| 132 | return false; |
| 133 | } |
| 134 | rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 135 | if (rc != SQLITE_OK) { |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 136 | LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n", |
| 137 | param_name ? param_name : "#1", rc); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 138 | db_remove_reset(stmt); |
| 139 | return false; |
| 140 | } |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
Neels Hofmeyr | 28da26e | 2017-10-06 03:44:57 +0200 | [diff] [blame] | 144 | /** bind int arg and do proper cleanup in case of failure. If param_name is |
| 145 | * NULL, bind to the first parameter (useful for SQL statements that have only |
| 146 | * one parameter). */ |
| 147 | bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr) |
| 148 | { |
| 149 | int rc; |
| 150 | int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1; |
| 151 | if (idx < 1) { |
| 152 | LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n", |
| 153 | param_name); |
| 154 | return false; |
| 155 | } |
| 156 | rc = sqlite3_bind_int(stmt, idx, nr); |
| 157 | if (rc != SQLITE_OK) { |
| 158 | LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n", |
| 159 | param_name ? param_name : "#1", rc); |
| 160 | db_remove_reset(stmt); |
| 161 | return false; |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | /** bind int64 arg and do proper cleanup in case of failure. If param_name is |
| 167 | * NULL, bind to the first parameter (useful for SQL statements that have only |
| 168 | * one parameter). */ |
| 169 | bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr) |
| 170 | { |
| 171 | int rc; |
| 172 | int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1; |
| 173 | if (idx < 1) { |
| 174 | LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n", |
| 175 | param_name); |
| 176 | return false; |
| 177 | } |
| 178 | rc = sqlite3_bind_int64(stmt, idx, nr); |
| 179 | if (rc != SQLITE_OK) { |
| 180 | LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n", |
| 181 | param_name ? param_name : "#1", rc); |
| 182 | db_remove_reset(stmt); |
| 183 | return false; |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
Neels Hofmeyr | 04c2375 | 2019-11-25 03:59:50 +0100 | [diff] [blame] | 188 | bool db_bind_null(sqlite3_stmt *stmt, const char *param_name) |
| 189 | { |
| 190 | int rc; |
| 191 | int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1; |
| 192 | if (idx < 1) { |
| 193 | LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n", |
| 194 | param_name); |
| 195 | return false; |
| 196 | } |
| 197 | rc = sqlite3_bind_null(stmt, idx); |
| 198 | if (rc != SQLITE_OK) { |
| 199 | LOGP(DDB, LOGL_ERROR, "Error binding NULL to SQL parameter %s: %d\n", |
| 200 | param_name ? param_name : "#1", rc); |
| 201 | db_remove_reset(stmt); |
| 202 | return false; |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 207 | void db_close(struct db_context *dbc) |
| 208 | { |
| 209 | unsigned int i; |
Vadim Yanitskiy | dc17e05 | 2018-07-30 20:14:16 +0700 | [diff] [blame] | 210 | int rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 211 | |
| 212 | for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) { |
| 213 | /* it is ok to call finalize on NULL */ |
| 214 | sqlite3_finalize(dbc->stmt[i]); |
| 215 | } |
Vadim Yanitskiy | dc17e05 | 2018-07-30 20:14:16 +0700 | [diff] [blame] | 216 | |
| 217 | /* Ask sqlite3 to close DB */ |
| 218 | rc = sqlite3_close(dbc->db); |
| 219 | if (rc != SQLITE_OK) { /* Make sure it's actually closed! */ |
| 220 | LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n", |
| 221 | rc, sqlite3_errmsg(dbc->db)); |
| 222 | } |
| 223 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 224 | talloc_free(dbc); |
| 225 | } |
| 226 | |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 227 | static int db_run_statements(struct db_context *dbc, const char **statements, size_t statements_count) |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 228 | { |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 229 | int rc; |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 230 | int i; |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 231 | for (i = 0; i < statements_count; i++) { |
| 232 | const char *stmt_str = statements[i]; |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 233 | sqlite3_stmt *stmt; |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 234 | |
| 235 | rc = sqlite3_prepare_v2(dbc->db, stmt_str, -1, &stmt, NULL); |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 236 | if (rc != SQLITE_OK) { |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 237 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_str); |
Vadim Yanitskiy | 050eb1d | 2018-07-30 20:48:19 +0700 | [diff] [blame] | 238 | return rc; |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 239 | } |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 240 | rc = sqlite3_step(stmt); |
| 241 | db_remove_reset(stmt); |
Vadim Yanitskiy | dc17e05 | 2018-07-30 20:14:16 +0700 | [diff] [blame] | 242 | sqlite3_finalize(stmt); |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 243 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 244 | LOGP(DDB, LOGL_ERROR, "SQL error: (%d) %s, during stmt '%s'", |
| 245 | rc, sqlite3_errmsg(dbc->db), stmt_str); |
Vadim Yanitskiy | 050eb1d | 2018-07-30 20:48:19 +0700 | [diff] [blame] | 246 | return rc; |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 247 | } |
| 248 | } |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 249 | return rc; |
| 250 | } |
| 251 | |
| 252 | static int db_bootstrap(struct db_context *dbc) |
| 253 | { |
| 254 | int rc = db_run_statements(dbc, stmt_bootstrap_sql, ARRAY_SIZE(stmt_bootstrap_sql)); |
| 255 | if (rc != SQLITE_DONE) { |
| 256 | LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database\n"); |
| 257 | return rc; |
| 258 | } |
Vadim Yanitskiy | 050eb1d | 2018-07-30 20:48:19 +0700 | [diff] [blame] | 259 | return SQLITE_OK; |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 262 | /* https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema */ |
| 263 | static bool db_table_exists(struct db_context *dbc, const char *table_name) |
| 264 | { |
| 265 | const char *table_exists_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=?"; |
| 266 | sqlite3_stmt *stmt; |
| 267 | int rc; |
| 268 | |
| 269 | rc = sqlite3_prepare_v2(dbc->db, table_exists_sql, -1, &stmt, NULL); |
| 270 | if (rc != SQLITE_OK) { |
| 271 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", table_exists_sql); |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | if (!db_bind_text(stmt, NULL, table_name)) |
| 276 | return false; |
| 277 | |
| 278 | rc = sqlite3_step(stmt); |
| 279 | db_remove_reset(stmt); |
| 280 | sqlite3_finalize(stmt); |
| 281 | return (rc == SQLITE_ROW); |
| 282 | } |
| 283 | |
| 284 | /* Indicate whether the database is initialized with tables for schema version 0. |
| 285 | * We only check for the 'subscriber' table here because Neels said so. */ |
| 286 | static bool db_is_bootstrapped_v0(struct db_context *dbc) |
| 287 | { |
| 288 | if (!db_table_exists(dbc, "subscriber")) { |
| 289 | LOGP(DDB, LOGL_DEBUG, "Table 'subscriber' not found in database '%s'\n", dbc->fname); |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 296 | static int |
| 297 | db_upgrade_v1(struct db_context *dbc) |
| 298 | { |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 299 | int rc; |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 300 | const char *statements[] = { |
| 301 | "ALTER TABLE subscriber ADD COLUMN last_lu_seen TIMESTAMP default NULL", |
| 302 | "PRAGMA user_version = 1", |
| 303 | }; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 304 | |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 305 | rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements)); |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 306 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 307 | LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 1\n"); |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 308 | return rc; |
| 309 | } |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 310 | return rc; |
| 311 | } |
| 312 | |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 313 | static int db_upgrade_v2(struct db_context *dbc) |
| 314 | { |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 315 | int rc; |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 316 | const char *statements[] = { |
| 317 | "ALTER TABLE subscriber ADD COLUMN imei VARCHAR(14)", |
| 318 | "PRAGMA user_version = 2", |
| 319 | }; |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 320 | |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 321 | rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements)); |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 322 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | f5459de | 2019-10-31 01:29:59 +0100 | [diff] [blame] | 323 | LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 2\n"); |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 324 | return rc; |
| 325 | } |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 326 | return rc; |
| 327 | } |
| 328 | |
Neels Hofmeyr | a8045da | 2019-10-31 01:19:44 +0100 | [diff] [blame] | 329 | static int db_upgrade_v3(struct db_context *dbc) |
| 330 | { |
Neels Hofmeyr | a8045da | 2019-10-31 01:19:44 +0100 | [diff] [blame] | 331 | int rc; |
| 332 | |
| 333 | /* A newer SQLite version would allow simply 'ATLER TABLE subscriber RENAME COLUMN hlr_number TO msc_number'. |
| 334 | * This is a really expensive workaround for that in order to cover earlier SQLite versions as well: |
| 335 | * Create a new table with the new column name and copy the data over (https://www.sqlite.org/faq.html#q11). |
| 336 | */ |
| 337 | #define SUBSCR_V3_CREATE \ |
| 338 | "(\n" \ |
| 339 | "-- OsmoHLR's DB scheme is modelled roughly after TS 23.008 version 13.3.0\n" \ |
| 340 | " id INTEGER PRIMARY KEY,\n" \ |
| 341 | " -- Chapter 2.1.1.1\n" \ |
| 342 | " imsi VARCHAR(15) UNIQUE NOT NULL,\n" \ |
| 343 | " -- Chapter 2.1.2\n" \ |
| 344 | " msisdn VARCHAR(15) UNIQUE,\n" \ |
| 345 | " -- Chapter 2.2.3: Most recent / current IMEISV\n" \ |
| 346 | " imeisv VARCHAR,\n" \ |
| 347 | " -- Chapter 2.1.9: Most recent / current IMEI\n" \ |
| 348 | " imei VARCHAR(14),\n" \ |
| 349 | " -- Chapter 2.4.5\n" \ |
| 350 | " vlr_number VARCHAR(15),\n" \ |
| 351 | " -- Chapter 2.4.6\n" \ |
| 352 | " msc_number VARCHAR(15),\n" \ |
| 353 | " -- Chapter 2.4.8.1\n" \ |
| 354 | " sgsn_number VARCHAR(15),\n" \ |
| 355 | " -- Chapter 2.13.10\n" \ |
| 356 | " sgsn_address VARCHAR,\n" \ |
| 357 | " -- Chapter 2.4.8.2\n" \ |
| 358 | " ggsn_number VARCHAR(15),\n" \ |
| 359 | " -- Chapter 2.4.9.2\n" \ |
| 360 | " gmlc_number VARCHAR(15),\n" \ |
| 361 | " -- Chapter 2.4.23\n" \ |
| 362 | " smsc_number VARCHAR(15),\n" \ |
| 363 | " -- Chapter 2.4.24\n" \ |
| 364 | " periodic_lu_tmr INTEGER,\n" \ |
| 365 | " -- Chapter 2.13.115\n" \ |
| 366 | " periodic_rau_tau_tmr INTEGER,\n" \ |
| 367 | " -- Chapter 2.1.1.2: network access mode\n" \ |
| 368 | " nam_cs BOOLEAN NOT NULL DEFAULT 1,\n" \ |
| 369 | " nam_ps BOOLEAN NOT NULL DEFAULT 1,\n" \ |
| 370 | " -- Chapter 2.1.8\n" \ |
| 371 | " lmsi INTEGER,\n" \ |
| 372 | \ |
| 373 | " -- The below purged flags might not even be stored non-volatile,\n" \ |
| 374 | " -- refer to TS 23.012 Chapter 3.6.1.4\n" \ |
| 375 | " -- Chapter 2.7.5\n" \ |
| 376 | " ms_purged_cs BOOLEAN NOT NULL DEFAULT 0,\n" \ |
| 377 | " -- Chapter 2.7.6\n" \ |
| 378 | " ms_purged_ps BOOLEAN NOT NULL DEFAULT 0,\n" \ |
| 379 | \ |
| 380 | " -- Timestamp of last location update seen from subscriber\n" \ |
| 381 | " -- The value is a string which encodes a UTC timestamp in granularity of seconds.\n" \ |
| 382 | " last_lu_seen TIMESTAMP default NULL\n" \ |
| 383 | ")\n" |
| 384 | |
| 385 | #define SUBSCR_V2_COLUMN_NAMES \ |
| 386 | "id," \ |
| 387 | "imsi," \ |
| 388 | "msisdn," \ |
| 389 | "imeisv," \ |
| 390 | "imei," \ |
| 391 | "vlr_number," \ |
| 392 | "hlr_number," \ |
| 393 | "sgsn_number," \ |
| 394 | "sgsn_address," \ |
| 395 | "ggsn_number," \ |
| 396 | "gmlc_number," \ |
| 397 | "smsc_number," \ |
| 398 | "periodic_lu_tmr," \ |
| 399 | "periodic_rau_tau_tmr," \ |
| 400 | "nam_cs," \ |
| 401 | "nam_ps," \ |
| 402 | "lmsi," \ |
| 403 | "ms_purged_cs," \ |
| 404 | "ms_purged_ps," \ |
| 405 | "last_lu_seen" |
| 406 | |
| 407 | #define SUBSCR_V3_COLUMN_NAMES \ |
| 408 | "id," \ |
| 409 | "imsi," \ |
| 410 | "msisdn," \ |
| 411 | "imeisv," \ |
| 412 | "imei," \ |
| 413 | "vlr_number," \ |
| 414 | "msc_number," \ |
| 415 | "sgsn_number," \ |
| 416 | "sgsn_address," \ |
| 417 | "ggsn_number," \ |
| 418 | "gmlc_number," \ |
| 419 | "smsc_number," \ |
| 420 | "periodic_lu_tmr," \ |
| 421 | "periodic_rau_tau_tmr," \ |
| 422 | "nam_cs," \ |
| 423 | "nam_ps," \ |
| 424 | "lmsi," \ |
| 425 | "ms_purged_cs," \ |
| 426 | "ms_purged_ps," \ |
| 427 | "last_lu_seen" |
| 428 | |
| 429 | const char *statements[] = { |
| 430 | "BEGIN TRANSACTION", |
| 431 | "CREATE TEMPORARY TABLE subscriber_backup" SUBSCR_V3_CREATE, |
| 432 | "INSERT INTO subscriber_backup SELECT " SUBSCR_V2_COLUMN_NAMES " FROM subscriber", |
| 433 | "DROP TABLE subscriber", |
| 434 | "CREATE TABLE subscriber" SUBSCR_V3_CREATE, |
| 435 | "INSERT INTO subscriber SELECT " SUBSCR_V3_COLUMN_NAMES " FROM subscriber_backup", |
| 436 | "DROP TABLE subscriber_backup", |
| 437 | "COMMIT", |
| 438 | "PRAGMA user_version = 3", |
| 439 | }; |
| 440 | |
Neels Hofmeyr | 7f4dd11 | 2019-10-31 21:23:10 +0100 | [diff] [blame] | 441 | rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements)); |
| 442 | if (rc != SQLITE_DONE) { |
| 443 | LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 3\n"); |
| 444 | return rc; |
Neels Hofmeyr | a8045da | 2019-10-31 01:19:44 +0100 | [diff] [blame] | 445 | } |
| 446 | return rc; |
| 447 | } |
| 448 | |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 449 | static int db_upgrade_v4(struct db_context *dbc) |
| 450 | { |
| 451 | int rc; |
| 452 | const char *statements[] = { |
| 453 | "ALTER TABLE subscriber ADD COLUMN last_lu_seen_ps TIMESTAMP default NULL", |
| 454 | "PRAGMA user_version = 4", |
| 455 | }; |
| 456 | |
| 457 | rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements)); |
| 458 | if (rc != SQLITE_DONE) { |
| 459 | LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 4\n"); |
| 460 | return rc; |
| 461 | } |
| 462 | return rc; |
| 463 | } |
| 464 | |
Neels Hofmeyr | 04c2375 | 2019-11-25 03:59:50 +0100 | [diff] [blame] | 465 | static int db_upgrade_v5(struct db_context *dbc) |
| 466 | { |
| 467 | int rc; |
| 468 | const char *statements[] = { |
| 469 | "ALTER TABLE subscriber ADD COLUMN vlr_via_proxy VARCHAR", |
| 470 | "ALTER TABLE subscriber ADD COLUMN sgsn_via_proxy VARCHAR", |
| 471 | "PRAGMA user_version = 5", |
| 472 | }; |
| 473 | |
| 474 | rc = db_run_statements(dbc, statements, ARRAY_SIZE(statements)); |
| 475 | if (rc != SQLITE_DONE) { |
| 476 | LOGP(DDB, LOGL_ERROR, "Unable to update HLR database schema to version 5\n"); |
| 477 | return rc; |
| 478 | } |
| 479 | return rc; |
| 480 | } |
| 481 | |
Neels Hofmeyr | 981e126 | 2019-11-05 02:00:19 +0100 | [diff] [blame] | 482 | typedef int (*db_upgrade_func_t)(struct db_context *dbc); |
| 483 | static db_upgrade_func_t db_upgrade_path[] = { |
| 484 | db_upgrade_v1, |
| 485 | db_upgrade_v2, |
| 486 | db_upgrade_v3, |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 487 | db_upgrade_v4, |
Neels Hofmeyr | 04c2375 | 2019-11-25 03:59:50 +0100 | [diff] [blame] | 488 | db_upgrade_v5, |
Neels Hofmeyr | 981e126 | 2019-11-05 02:00:19 +0100 | [diff] [blame] | 489 | }; |
| 490 | |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 491 | static int db_get_user_version(struct db_context *dbc) |
| 492 | { |
| 493 | const char *user_version_sql = "PRAGMA user_version"; |
| 494 | sqlite3_stmt *stmt; |
| 495 | int version, rc; |
| 496 | |
| 497 | rc = sqlite3_prepare_v2(dbc->db, user_version_sql, -1, &stmt, NULL); |
| 498 | if (rc != SQLITE_OK) { |
| 499 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", user_version_sql); |
| 500 | return -1; |
| 501 | } |
| 502 | rc = sqlite3_step(stmt); |
| 503 | if (rc == SQLITE_ROW) { |
| 504 | version = sqlite3_column_int(stmt, 0); |
| 505 | } else { |
| 506 | LOGP(DDB, LOGL_ERROR, "SQL statement '%s' failed: %d\n", user_version_sql, rc); |
| 507 | version = -1; |
| 508 | } |
| 509 | |
| 510 | db_remove_reset(stmt); |
| 511 | sqlite3_finalize(stmt); |
| 512 | return version; |
| 513 | } |
| 514 | |
| 515 | 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] | 516 | { |
| 517 | struct db_context *dbc = talloc_zero(ctx, struct db_context); |
| 518 | unsigned int i; |
| 519 | int rc; |
Neels Hofmeyr | cd83b8a | 2017-10-06 04:20:37 +0200 | [diff] [blame] | 520 | bool has_sqlite_config_sqllog = false; |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 521 | int version; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 522 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 523 | LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 524 | LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION); |
| 525 | LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion()); |
| 526 | |
Vadim Yanitskiy | fbd736e | 2018-07-31 22:40:30 +0700 | [diff] [blame] | 527 | #ifdef SQLITE_USE_TALLOC |
| 528 | /* Configure SQLite3 to use talloc memory allocator */ |
| 529 | rc = db_sqlite3_use_talloc(ctx); |
| 530 | if (rc == SQLITE_OK) { |
| 531 | LOGP(DDB, LOGL_NOTICE, "SQLite3 is configured to use talloc\n"); |
| 532 | } else { |
| 533 | LOGP(DDB, LOGL_ERROR, "Failed to configure SQLite3 " |
| 534 | "to use talloc, using default memory allocator\n"); |
| 535 | } |
| 536 | #endif |
| 537 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 538 | dbc->fname = talloc_strdup(dbc, fname); |
| 539 | |
| 540 | for (i = 0; i < 0xfffff; i++) { |
| 541 | const char *o = sqlite3_compileoption_get(i); |
| 542 | if (!o) |
| 543 | break; |
Neels Hofmeyr | e9c0c5b | 2017-10-10 16:52:22 +0200 | [diff] [blame] | 544 | LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o); |
Neels Hofmeyr | cd83b8a | 2017-10-06 04:20:37 +0200 | [diff] [blame] | 545 | if (!strcmp(o, "ENABLE_SQLLOG")) |
| 546 | has_sqlite_config_sqllog = true; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 547 | } |
| 548 | |
Neels Hofmeyr | d3814b9 | 2017-11-21 12:28:07 +0100 | [diff] [blame] | 549 | if (enable_sqlite_logging) { |
| 550 | rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL); |
| 551 | if (rc != SQLITE_OK) |
| 552 | LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n"); |
| 553 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 554 | |
Neels Hofmeyr | cd83b8a | 2017-10-06 04:20:37 +0200 | [diff] [blame] | 555 | if (has_sqlite_config_sqllog) { |
| 556 | rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL); |
| 557 | if (rc != SQLITE_OK) |
| 558 | LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n"); |
| 559 | } else |
| 560 | LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:" |
| 561 | " SQLite3 compiled without support for it\n"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 562 | |
| 563 | rc = sqlite3_open(dbc->fname, &dbc->db); |
| 564 | if (rc != SQLITE_OK) { |
| 565 | LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc); |
| 566 | talloc_free(dbc); |
| 567 | return NULL; |
| 568 | } |
| 569 | |
| 570 | /* enable extended result codes */ |
| 571 | rc = sqlite3_extended_result_codes(dbc->db, 1); |
| 572 | if (rc != SQLITE_OK) |
Neels Hofmeyr | e9c0c5b | 2017-10-10 16:52:22 +0200 | [diff] [blame] | 573 | LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 574 | |
Harald Welte | abd1a54 | 2016-05-03 18:50:41 +0200 | [diff] [blame] | 575 | char *err_msg; |
| 576 | rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg); |
Vadim Yanitskiy | 15ad7be | 2020-02-09 05:32:46 +0700 | [diff] [blame] | 577 | if (rc != SQLITE_OK) { |
Harald Welte | abd1a54 | 2016-05-03 18:50:41 +0200 | [diff] [blame] | 578 | LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n", |
| 579 | err_msg); |
Vadim Yanitskiy | 15ad7be | 2020-02-09 05:32:46 +0700 | [diff] [blame] | 580 | sqlite3_free(err_msg); |
| 581 | } |
Harald Welte | abd1a54 | 2016-05-03 18:50:41 +0200 | [diff] [blame] | 582 | |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 583 | version = db_get_user_version(dbc); |
| 584 | if (version < 0) { |
| 585 | LOGP(DDB, LOGL_ERROR, "Unable to read user version number from database '%s'\n", dbc->fname); |
| 586 | goto out_free; |
| 587 | } |
| 588 | |
| 589 | /* An empty database will always report version zero. */ |
| 590 | if (version == 0 && !db_is_bootstrapped_v0(dbc)) { |
| 591 | LOGP(DDB, LOGL_NOTICE, "Missing database tables detected; Bootstrapping database '%s'\n", dbc->fname); |
| 592 | rc = db_bootstrap(dbc); |
| 593 | if (rc != SQLITE_OK) { |
| 594 | LOGP(DDB, LOGL_ERROR, "Failed to bootstrap DB: (rc=%d) %s\n", |
| 595 | rc, sqlite3_errmsg(dbc->db)); |
| 596 | goto out_free; |
| 597 | } |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 598 | version = CURRENT_SCHEMA_VERSION; |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | LOGP(DDB, LOGL_NOTICE, "Database '%s' has HLR DB schema version %d\n", dbc->fname, version); |
| 602 | |
Neels Hofmeyr | 981e126 | 2019-11-05 02:00:19 +0100 | [diff] [blame] | 603 | for (; allow_upgrade && (version < ARRAY_SIZE(db_upgrade_path)); version++) { |
| 604 | db_upgrade_func_t upgrade_func = db_upgrade_path[version]; |
| 605 | rc = upgrade_func(dbc); |
| 606 | if (rc != SQLITE_DONE) { |
| 607 | LOGP(DDB, LOGL_ERROR, "Failed to upgrade HLR DB schema to version %d: (rc=%d) %s\n", |
| 608 | version+1, rc, sqlite3_errmsg(dbc->db)); |
| 609 | goto out_free; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 610 | } |
| 611 | LOGP(DDB, LOGL_NOTICE, "Database '%s' has been upgraded to HLR DB schema version %d\n", |
Neels Hofmeyr | 981e126 | 2019-11-05 02:00:19 +0100 | [diff] [blame] | 612 | dbc->fname, version+1); |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | if (version != CURRENT_SCHEMA_VERSION) { |
| 616 | if (version < CURRENT_SCHEMA_VERSION) { |
| 617 | LOGP(DDB, LOGL_NOTICE, "HLR DB schema version %d is outdated\n", version); |
| 618 | if (!allow_upgrade) { |
| 619 | LOGP(DDB, LOGL_ERROR, "Not upgrading HLR database to schema version %d; " |
| 620 | "use the --db-upgrade option to allow HLR database upgrades\n", |
| 621 | CURRENT_SCHEMA_VERSION); |
| 622 | } |
| 623 | } else |
| 624 | LOGP(DDB, LOGL_ERROR, "HLR DB schema version %d is unknown\n", version); |
| 625 | |
Vadim Yanitskiy | 050eb1d | 2018-07-30 20:48:19 +0700 | [diff] [blame] | 626 | goto out_free; |
| 627 | } |
Neels Hofmeyr | 7750d2c | 2017-10-24 23:26:27 +0200 | [diff] [blame] | 628 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 629 | /* prepare all SQL statements */ |
| 630 | for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) { |
| 631 | rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1, |
| 632 | &dbc->stmt[i], NULL); |
| 633 | if (rc != SQLITE_OK) { |
| 634 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]); |
| 635 | goto out_free; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | return dbc; |
| 640 | out_free: |
| 641 | db_close(dbc); |
| 642 | return NULL; |
| 643 | } |