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> |
| 24 | |
| 25 | #include "logging.h" |
| 26 | #include "db.h" |
| 27 | |
| 28 | static const char *stmt_sql[] = { |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 29 | [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 = ?", |
| 30 | [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = ? WHERE id = ?", |
| 31 | [DB_STMT_UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = ? WHERE id = ?", |
| 32 | [DB_STMT_AUC_BY_IMSI] = "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn, ind_bitlen FROM subscriber LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id WHERE imsi = ?", |
| 33 | [DB_STMT_AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = ? WHERE subscriber_id = ?", |
| 34 | [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs=1 WHERE imsi = ?", |
| 35 | [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps=1 WHERE imsi = ?", |
| 36 | [DB_STMT_SET_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps=1 WHERE imsi = ?", |
| 37 | [DB_STMT_UNSET_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps=0 WHERE imsi = ?", |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static void sql3_error_log_cb(void *arg, int err_code, const char *msg) |
| 41 | { |
| 42 | LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg); |
| 43 | } |
| 44 | |
| 45 | static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type) |
| 46 | { |
| 47 | switch (type) { |
| 48 | case 0: |
| 49 | LOGP(DDB, LOGL_DEBUG, "Opened database\n"); |
| 50 | break; |
| 51 | case 1: |
Max | 58d4a84 | 2017-02-20 11:06:12 +0100 | [diff] [blame] | 52 | LOGP(DDB, LOGL_DEBUG, "%s\n", stmt); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 53 | break; |
| 54 | case 2: |
| 55 | LOGP(DDB, LOGL_DEBUG, "Closed database\n"); |
| 56 | break; |
| 57 | default: |
| 58 | LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 63 | /* remove bindings and reset statement to be re-executed */ |
| 64 | bool db_remove_reset(sqlite3_stmt *stmt) |
| 65 | { |
| 66 | int rc = sqlite3_clear_bindings(stmt); |
| 67 | if (rc != SQLITE_OK) { |
| 68 | LOGP(DDB, LOGL_ERROR, "Error clerearing bindings: %d\n", rc); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | rc = sqlite3_reset(stmt); |
| 73 | if (rc != SQLITE_OK) { |
| 74 | LOGP(DDB, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc); |
| 75 | return false; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | /* bind IMSI and do proper cleanup in case of failure */ |
| 81 | bool db_bind_imsi(sqlite3_stmt *stmt, const char *imsi) |
| 82 | { |
| 83 | int rc = sqlite3_bind_text(stmt, 1, imsi, -1, SQLITE_STATIC); |
| 84 | if (rc != SQLITE_OK) { |
| 85 | LOGP(DDB, LOGL_ERROR, "Error binding IMSI %s: %d\n", imsi, rc); |
| 86 | db_remove_reset(stmt); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 93 | void db_close(struct db_context *dbc) |
| 94 | { |
| 95 | unsigned int i; |
| 96 | |
| 97 | for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) { |
| 98 | /* it is ok to call finalize on NULL */ |
| 99 | sqlite3_finalize(dbc->stmt[i]); |
| 100 | } |
| 101 | sqlite3_close(dbc->db); |
| 102 | talloc_free(dbc); |
| 103 | } |
| 104 | |
| 105 | struct db_context *db_open(void *ctx, const char *fname) |
| 106 | { |
| 107 | struct db_context *dbc = talloc_zero(ctx, struct db_context); |
| 108 | unsigned int i; |
| 109 | int rc; |
| 110 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 111 | LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 112 | LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION); |
| 113 | LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion()); |
| 114 | |
| 115 | dbc->fname = talloc_strdup(dbc, fname); |
| 116 | |
| 117 | for (i = 0; i < 0xfffff; i++) { |
| 118 | const char *o = sqlite3_compileoption_get(i); |
| 119 | if (!o) |
| 120 | break; |
| 121 | LOGP(DDB, LOGL_DEBUG, "SQlite3 compiled with '%s'\n", o); |
| 122 | } |
| 123 | |
| 124 | rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL); |
| 125 | if (rc != SQLITE_OK) |
| 126 | LOGP(DDB, LOGL_NOTICE, "Unable to set SQlite3 error log callback\n"); |
| 127 | |
| 128 | rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL); |
| 129 | if (rc != SQLITE_OK) |
| 130 | LOGP(DDB, LOGL_NOTICE, "Unable to set SQlite3 SQL statement log callback\n"); |
| 131 | |
| 132 | rc = sqlite3_open(dbc->fname, &dbc->db); |
| 133 | if (rc != SQLITE_OK) { |
| 134 | LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc); |
| 135 | talloc_free(dbc); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | /* enable extended result codes */ |
| 140 | rc = sqlite3_extended_result_codes(dbc->db, 1); |
| 141 | if (rc != SQLITE_OK) |
| 142 | LOGP(DDB, LOGL_ERROR, "Unable to enable SQlite3 extended result codes\n"); |
| 143 | |
Harald Welte | abd1a54 | 2016-05-03 18:50:41 +0200 | [diff] [blame] | 144 | char *err_msg; |
| 145 | rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg); |
| 146 | if (rc != SQLITE_OK) |
| 147 | LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n", |
| 148 | err_msg); |
| 149 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 150 | /* prepare all SQL statements */ |
| 151 | for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) { |
| 152 | rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1, |
| 153 | &dbc->stmt[i], NULL); |
| 154 | if (rc != SQLITE_OK) { |
| 155 | LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]); |
| 156 | goto out_free; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return dbc; |
| 161 | out_free: |
| 162 | db_close(dbc); |
| 163 | return NULL; |
| 164 | } |