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