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" |
| 28 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 29 | #define SEL_COLUMNS \ |
| 30 | "id," \ |
| 31 | "imsi," \ |
| 32 | "msisdn," \ |
| 33 | "vlr_number," \ |
| 34 | "sgsn_number," \ |
| 35 | "sgsn_address," \ |
| 36 | "periodic_lu_tmr," \ |
| 37 | "periodic_rau_tau_tmr," \ |
| 38 | "nam_cs," \ |
| 39 | "nam_ps," \ |
| 40 | "lmsi," \ |
| 41 | "ms_purged_cs," \ |
| 42 | "ms_purged_ps" |
| 43 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 44 | static const char *stmt_sql[] = { |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 45 | [DB_STMT_SEL_BY_IMSI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imsi = ?", |
| 46 | [DB_STMT_SEL_BY_MSISDN] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE msisdn = ?", |
| 47 | [DB_STMT_SEL_BY_ID] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE id = ?", |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 48 | [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = $number WHERE id = $subscriber_id", |
| 49 | [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] | 50 | [DB_STMT_AUC_BY_IMSI] = |
| 51 | "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn, ind_bitlen" |
| 52 | " FROM subscriber" |
| 53 | " LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id" |
| 54 | " LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id" |
Neels Hofmeyr | c5122f2 | 2017-10-09 23:12:57 +0200 | [diff] [blame] | 55 | " WHERE imsi = $imsi", |
Neels Hofmeyr | 1cbdb70 | 2017-10-09 23:03:57 +0200 | [diff] [blame] | 56 | [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] | 57 | [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs = $val WHERE imsi = $imsi", |
| 58 | [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] | 59 | [DB_STMT_UPD_NAM_CS_BY_IMSI] = "UPDATE subscriber SET nam_cs = $val WHERE imsi = $imsi", |
| 60 | [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] | 61 | [DB_STMT_SUBSCR_CREATE] = "INSERT INTO subscriber (imsi) VALUES ($imsi)", |
| 62 | [DB_STMT_DEL_BY_ID] = "DELETE FROM subscriber WHERE id = $subscriber_id", |
| 63 | [DB_STMT_SET_MSISDN_BY_IMSI] = "UPDATE subscriber SET msisdn = $msisdn WHERE imsi = $imsi", |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 64 | [DB_STMT_AUC_2G_INSERT] = |
| 65 | "INSERT INTO auc_2g (subscriber_id, algo_id_2g, ki)" |
| 66 | " VALUES($subscriber_id, $algo_id_2g, $ki)", |
| 67 | [DB_STMT_AUC_2G_DELETE] = "DELETE FROM auc_2g WHERE subscriber_id = $subscriber_id", |
| 68 | [DB_STMT_AUC_3G_INSERT] = |
| 69 | "INSERT INTO auc_3g (subscriber_id, algo_id_3g, k, op, opc, ind_bitlen)" |
| 70 | " VALUES($subscriber_id, $algo_id_3g, $k, $op, $opc, $ind_bitlen)", |
| 71 | [DB_STMT_AUC_3G_DELETE] = "DELETE FROM auc_3g WHERE subscriber_id = $subscriber_id", |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | static void sql3_error_log_cb(void *arg, int err_code, const char *msg) |
| 75 | { |
| 76 | LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg); |
| 77 | } |
| 78 | |
| 79 | static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type) |
| 80 | { |
| 81 | switch (type) { |
| 82 | case 0: |
| 83 | LOGP(DDB, LOGL_DEBUG, "Opened database\n"); |
| 84 | break; |
| 85 | case 1: |
Max | 58d4a84 | 2017-02-20 11:06:12 +0100 | [diff] [blame] | 86 | LOGP(DDB, LOGL_DEBUG, "%s\n", stmt); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 87 | break; |
| 88 | case 2: |
| 89 | LOGP(DDB, LOGL_DEBUG, "Closed database\n"); |
| 90 | break; |
| 91 | default: |
| 92 | LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 97 | /* remove bindings and reset statement to be re-executed */ |
Neels Hofmeyr | d7d9697 | 2017-10-06 03:50:30 +0200 | [diff] [blame] | 98 | void db_remove_reset(sqlite3_stmt *stmt) |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 99 | { |
Neels Hofmeyr | d7d9697 | 2017-10-06 03:50:30 +0200 | [diff] [blame] | 100 | sqlite3_clear_bindings(stmt); |
| 101 | sqlite3_reset(stmt); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 104 | /** bind text arg and do proper cleanup in case of failure. If param_name is |
| 105 | * NULL, bind to the first parameter (useful for SQL statements that have only |
| 106 | * one parameter). */ |
| 107 | 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] | 108 | { |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 109 | int rc; |
| 110 | int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1; |
| 111 | if (idx < 1) { |
| 112 | LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n", |
| 113 | param_name); |
| 114 | return false; |
| 115 | } |
| 116 | rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 117 | if (rc != SQLITE_OK) { |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 118 | LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n", |
| 119 | param_name ? param_name : "#1", rc); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 120 | db_remove_reset(stmt); |
| 121 | return false; |
| 122 | } |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
Neels Hofmeyr | 28da26e | 2017-10-06 03:44:57 +0200 | [diff] [blame] | 126 | /** bind int arg and do proper cleanup in case of failure. If param_name is |
| 127 | * NULL, bind to the first parameter (useful for SQL statements that have only |
| 128 | * one parameter). */ |
| 129 | bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr) |
| 130 | { |
| 131 | int rc; |
| 132 | int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1; |
| 133 | if (idx < 1) { |
| 134 | LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n", |
| 135 | param_name); |
| 136 | return false; |
| 137 | } |
| 138 | rc = sqlite3_bind_int(stmt, idx, nr); |
| 139 | if (rc != SQLITE_OK) { |
| 140 | LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n", |
| 141 | param_name ? param_name : "#1", rc); |
| 142 | db_remove_reset(stmt); |
| 143 | return false; |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | /** bind int64 arg and do proper cleanup in case of failure. If param_name is |
| 149 | * NULL, bind to the first parameter (useful for SQL statements that have only |
| 150 | * one parameter). */ |
| 151 | bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr) |
| 152 | { |
| 153 | int rc; |
| 154 | int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1; |
| 155 | if (idx < 1) { |
| 156 | LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n", |
| 157 | param_name); |
| 158 | return false; |
| 159 | } |
| 160 | rc = sqlite3_bind_int64(stmt, idx, nr); |
| 161 | if (rc != SQLITE_OK) { |
| 162 | LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n", |
| 163 | param_name ? param_name : "#1", rc); |
| 164 | db_remove_reset(stmt); |
| 165 | return false; |
| 166 | } |
| 167 | return true; |
| 168 | } |
| 169 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 170 | void db_close(struct db_context *dbc) |
| 171 | { |
| 172 | unsigned int i; |
| 173 | |
| 174 | for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) { |
| 175 | /* it is ok to call finalize on NULL */ |
| 176 | sqlite3_finalize(dbc->stmt[i]); |
| 177 | } |
| 178 | sqlite3_close(dbc->db); |
| 179 | talloc_free(dbc); |
| 180 | } |
| 181 | |
| 182 | struct db_context *db_open(void *ctx, const char *fname) |
| 183 | { |
| 184 | struct db_context *dbc = talloc_zero(ctx, struct db_context); |
| 185 | unsigned int i; |
| 186 | int rc; |
Neels Hofmeyr | cd83b8a | 2017-10-06 04:20:37 +0200 | [diff] [blame] | 187 | bool has_sqlite_config_sqllog = false; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 188 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 189 | LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 190 | LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION); |
| 191 | LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion()); |
| 192 | |
| 193 | dbc->fname = talloc_strdup(dbc, fname); |
| 194 | |
| 195 | for (i = 0; i < 0xfffff; i++) { |
| 196 | const char *o = sqlite3_compileoption_get(i); |
| 197 | if (!o) |
| 198 | break; |
Neels Hofmeyr | e9c0c5b | 2017-10-10 16:52:22 +0200 | [diff] [blame] | 199 | LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o); |
Neels Hofmeyr | cd83b8a | 2017-10-06 04:20:37 +0200 | [diff] [blame] | 200 | if (!strcmp(o, "ENABLE_SQLLOG")) |
| 201 | has_sqlite_config_sqllog = true; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL); |
| 205 | if (rc != SQLITE_OK) |
Neels Hofmeyr | e9c0c5b | 2017-10-10 16:52:22 +0200 | [diff] [blame] | 206 | LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 207 | |
Neels Hofmeyr | cd83b8a | 2017-10-06 04:20:37 +0200 | [diff] [blame] | 208 | if (has_sqlite_config_sqllog) { |
| 209 | rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL); |
| 210 | if (rc != SQLITE_OK) |
| 211 | LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n"); |
| 212 | } else |
| 213 | LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:" |
| 214 | " SQLite3 compiled without support for it\n"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 215 | |
| 216 | rc = sqlite3_open(dbc->fname, &dbc->db); |
| 217 | if (rc != SQLITE_OK) { |
| 218 | LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc); |
| 219 | talloc_free(dbc); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | /* enable extended result codes */ |
| 224 | rc = sqlite3_extended_result_codes(dbc->db, 1); |
| 225 | if (rc != SQLITE_OK) |
Neels Hofmeyr | e9c0c5b | 2017-10-10 16:52:22 +0200 | [diff] [blame] | 226 | LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 227 | |
Harald Welte | abd1a54 | 2016-05-03 18:50:41 +0200 | [diff] [blame] | 228 | char *err_msg; |
| 229 | rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg); |
| 230 | if (rc != SQLITE_OK) |
| 231 | LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n", |
| 232 | err_msg); |
| 233 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 234 | /* prepare all SQL statements */ |
| 235 | for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) { |
| 236 | rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1, |
| 237 | &dbc->stmt[i], NULL); |
| 238 | if (rc != SQLITE_OK) { |
| 239 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]); |
| 240 | goto out_free; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return dbc; |
| 245 | out_free: |
| 246 | db_close(dbc); |
| 247 | return NULL; |
| 248 | } |