blob: 0ece61b5d42f7b5b29bfcb2f8cd70524fceaf104 [file] [log] [blame]
Harald Weltee72cf552016-04-28 07:18:49 +02001/* (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
Max00b37152017-02-20 11:09:27 +010022#include <stdbool.h>
Harald Weltee72cf552016-04-28 07:18:49 +020023#include <sqlite3.h>
24
25#include "logging.h"
26#include "db.h"
27
28static const char *stmt_sql[] = {
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020029 [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 = ?",
Neels Hofmeyr0cac0a02017-10-09 17:47:21 +020032 [DB_STMT_AUC_BY_IMSI] =
33 "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn, ind_bitlen"
34 " FROM subscriber"
35 " LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id"
36 " LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id"
37 " WHERE imsi = ?",
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020038 [DB_STMT_AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = ? WHERE subscriber_id = ?",
39 [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs=1 WHERE imsi = ?",
40 [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps=1 WHERE imsi = ?",
41 [DB_STMT_SET_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps=1 WHERE imsi = ?",
42 [DB_STMT_UNSET_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps=0 WHERE imsi = ?",
Harald Weltee72cf552016-04-28 07:18:49 +020043};
44
45static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
46{
47 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
48}
49
50static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
51{
52 switch (type) {
53 case 0:
54 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
55 break;
56 case 1:
Max58d4a842017-02-20 11:06:12 +010057 LOGP(DDB, LOGL_DEBUG, "%s\n", stmt);
Harald Weltee72cf552016-04-28 07:18:49 +020058 break;
59 case 2:
60 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
61 break;
62 default:
63 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
64 break;
65 }
66}
67
Max00b37152017-02-20 11:09:27 +010068/* remove bindings and reset statement to be re-executed */
69bool db_remove_reset(sqlite3_stmt *stmt)
70{
71 int rc = sqlite3_clear_bindings(stmt);
72 if (rc != SQLITE_OK) {
73 LOGP(DDB, LOGL_ERROR, "Error clerearing bindings: %d\n", rc);
74 return false;
75 }
76
77 rc = sqlite3_reset(stmt);
78 if (rc != SQLITE_OK) {
79 LOGP(DDB, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc);
80 return false;
81 }
82 return true;
83}
84
Neels Hofmeyrf3144592017-10-06 03:40:52 +020085/** bind text arg and do proper cleanup in case of failure. If param_name is
86 * NULL, bind to the first parameter (useful for SQL statements that have only
87 * one parameter). */
88bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
Max00b37152017-02-20 11:09:27 +010089{
Neels Hofmeyrf3144592017-10-06 03:40:52 +020090 int rc;
91 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
92 if (idx < 1) {
93 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
94 param_name);
95 return false;
96 }
97 rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
Max00b37152017-02-20 11:09:27 +010098 if (rc != SQLITE_OK) {
Neels Hofmeyrf3144592017-10-06 03:40:52 +020099 LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
100 param_name ? param_name : "#1", rc);
Max00b37152017-02-20 11:09:27 +0100101 db_remove_reset(stmt);
102 return false;
103 }
Max00b37152017-02-20 11:09:27 +0100104 return true;
105}
106
Harald Weltee72cf552016-04-28 07:18:49 +0200107void db_close(struct db_context *dbc)
108{
109 unsigned int i;
110
111 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
112 /* it is ok to call finalize on NULL */
113 sqlite3_finalize(dbc->stmt[i]);
114 }
115 sqlite3_close(dbc->db);
116 talloc_free(dbc);
117}
118
119struct db_context *db_open(void *ctx, const char *fname)
120{
121 struct db_context *dbc = talloc_zero(ctx, struct db_context);
122 unsigned int i;
123 int rc;
124
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100125 LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
Harald Weltee72cf552016-04-28 07:18:49 +0200126 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
127 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
128
129 dbc->fname = talloc_strdup(dbc, fname);
130
131 for (i = 0; i < 0xfffff; i++) {
132 const char *o = sqlite3_compileoption_get(i);
133 if (!o)
134 break;
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200135 LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
Harald Weltee72cf552016-04-28 07:18:49 +0200136 }
137
138 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
139 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200140 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200141
142 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
143 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200144 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL statement log callback\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200145
146 rc = sqlite3_open(dbc->fname, &dbc->db);
147 if (rc != SQLITE_OK) {
148 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
149 talloc_free(dbc);
150 return NULL;
151 }
152
153 /* enable extended result codes */
154 rc = sqlite3_extended_result_codes(dbc->db, 1);
155 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200156 LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200157
Harald Welteabd1a542016-05-03 18:50:41 +0200158 char *err_msg;
159 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
160 if (rc != SQLITE_OK)
161 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
162 err_msg);
163
Harald Weltee72cf552016-04-28 07:18:49 +0200164 /* prepare all SQL statements */
165 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
166 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
167 &dbc->stmt[i], NULL);
168 if (rc != SQLITE_OK) {
169 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
170 goto out_free;
171 }
172 }
173
174 return dbc;
175out_free:
176 db_close(dbc);
177 return NULL;
178}