blob: 8286ba8c7ec07df70f4a3d5d05f2aaffa5a4e773 [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>
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +020024#include <string.h>
Harald Weltee72cf552016-04-28 07:18:49 +020025
26#include "logging.h"
27#include "db.h"
28
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020029#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 Weltee72cf552016-04-28 07:18:49 +020044static const char *stmt_sql[] = {
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020045 [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 Hofmeyr4bde9492017-10-06 03:09:34 +020048 [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = ? WHERE id = ?",
49 [DB_STMT_UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = ? WHERE id = ?",
Neels Hofmeyr0cac0a02017-10-09 17:47:21 +020050 [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"
55 " WHERE imsi = ?",
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020056 [DB_STMT_AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = ? WHERE subscriber_id = ?",
57 [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs=1 WHERE imsi = ?",
58 [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps=1 WHERE imsi = ?",
Neels Hofmeyre8ccd502017-10-06 04:10:06 +020059 [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 Hofmeyrf7c3e6e2017-10-09 17:55:16 +020061 [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",
Harald Weltee72cf552016-04-28 07:18:49 +020064};
65
66static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
67{
68 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
69}
70
71static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
72{
73 switch (type) {
74 case 0:
75 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
76 break;
77 case 1:
Max58d4a842017-02-20 11:06:12 +010078 LOGP(DDB, LOGL_DEBUG, "%s\n", stmt);
Harald Weltee72cf552016-04-28 07:18:49 +020079 break;
80 case 2:
81 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
82 break;
83 default:
84 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
85 break;
86 }
87}
88
Max00b37152017-02-20 11:09:27 +010089/* remove bindings and reset statement to be re-executed */
Neels Hofmeyrd7d96972017-10-06 03:50:30 +020090void db_remove_reset(sqlite3_stmt *stmt)
Max00b37152017-02-20 11:09:27 +010091{
Neels Hofmeyrd7d96972017-10-06 03:50:30 +020092 sqlite3_clear_bindings(stmt);
93 sqlite3_reset(stmt);
Max00b37152017-02-20 11:09:27 +010094}
95
Neels Hofmeyrf3144592017-10-06 03:40:52 +020096/** bind text arg and do proper cleanup in case of failure. If param_name is
97 * NULL, bind to the first parameter (useful for SQL statements that have only
98 * one parameter). */
99bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
Max00b37152017-02-20 11:09:27 +0100100{
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200101 int rc;
102 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
103 if (idx < 1) {
104 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
105 param_name);
106 return false;
107 }
108 rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
Max00b37152017-02-20 11:09:27 +0100109 if (rc != SQLITE_OK) {
Neels Hofmeyrf3144592017-10-06 03:40:52 +0200110 LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
111 param_name ? param_name : "#1", rc);
Max00b37152017-02-20 11:09:27 +0100112 db_remove_reset(stmt);
113 return false;
114 }
Max00b37152017-02-20 11:09:27 +0100115 return true;
116}
117
Neels Hofmeyr28da26e2017-10-06 03:44:57 +0200118/** bind int arg and do proper cleanup in case of failure. If param_name is
119 * NULL, bind to the first parameter (useful for SQL statements that have only
120 * one parameter). */
121bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr)
122{
123 int rc;
124 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
125 if (idx < 1) {
126 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
127 param_name);
128 return false;
129 }
130 rc = sqlite3_bind_int(stmt, idx, nr);
131 if (rc != SQLITE_OK) {
132 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
133 param_name ? param_name : "#1", rc);
134 db_remove_reset(stmt);
135 return false;
136 }
137 return true;
138}
139
140/** bind int64 arg and do proper cleanup in case of failure. If param_name is
141 * NULL, bind to the first parameter (useful for SQL statements that have only
142 * one parameter). */
143bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr)
144{
145 int rc;
146 int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
147 if (idx < 1) {
148 LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
149 param_name);
150 return false;
151 }
152 rc = sqlite3_bind_int64(stmt, idx, nr);
153 if (rc != SQLITE_OK) {
154 LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
155 param_name ? param_name : "#1", rc);
156 db_remove_reset(stmt);
157 return false;
158 }
159 return true;
160}
161
Harald Weltee72cf552016-04-28 07:18:49 +0200162void db_close(struct db_context *dbc)
163{
164 unsigned int i;
165
166 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
167 /* it is ok to call finalize on NULL */
168 sqlite3_finalize(dbc->stmt[i]);
169 }
170 sqlite3_close(dbc->db);
171 talloc_free(dbc);
172}
173
174struct db_context *db_open(void *ctx, const char *fname)
175{
176 struct db_context *dbc = talloc_zero(ctx, struct db_context);
177 unsigned int i;
178 int rc;
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200179 bool has_sqlite_config_sqllog = false;
Harald Weltee72cf552016-04-28 07:18:49 +0200180
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100181 LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
Harald Weltee72cf552016-04-28 07:18:49 +0200182 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
183 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
184
185 dbc->fname = talloc_strdup(dbc, fname);
186
187 for (i = 0; i < 0xfffff; i++) {
188 const char *o = sqlite3_compileoption_get(i);
189 if (!o)
190 break;
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200191 LOGP(DDB, LOGL_DEBUG, "SQLite3 compiled with '%s'\n", o);
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200192 if (!strcmp(o, "ENABLE_SQLLOG"))
193 has_sqlite_config_sqllog = true;
Harald Weltee72cf552016-04-28 07:18:49 +0200194 }
195
196 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
197 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200198 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 error log callback\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200199
Neels Hofmeyrcd83b8a2017-10-06 04:20:37 +0200200 if (has_sqlite_config_sqllog) {
201 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
202 if (rc != SQLITE_OK)
203 LOGP(DDB, LOGL_NOTICE, "Unable to set SQLite3 SQL log callback\n");
204 } else
205 LOGP(DDB, LOGL_DEBUG, "Not setting SQL log callback:"
206 " SQLite3 compiled without support for it\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200207
208 rc = sqlite3_open(dbc->fname, &dbc->db);
209 if (rc != SQLITE_OK) {
210 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
211 talloc_free(dbc);
212 return NULL;
213 }
214
215 /* enable extended result codes */
216 rc = sqlite3_extended_result_codes(dbc->db, 1);
217 if (rc != SQLITE_OK)
Neels Hofmeyre9c0c5b2017-10-10 16:52:22 +0200218 LOGP(DDB, LOGL_ERROR, "Unable to enable SQLite3 extended result codes\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200219
Harald Welteabd1a542016-05-03 18:50:41 +0200220 char *err_msg;
221 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
222 if (rc != SQLITE_OK)
223 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
224 err_msg);
225
Harald Weltee72cf552016-04-28 07:18:49 +0200226 /* prepare all SQL statements */
227 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
228 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
229 &dbc->stmt[i], NULL);
230 if (rc != SQLITE_OK) {
231 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
232 goto out_free;
233 }
234 }
235
236 return dbc;
237out_free:
238 db_close(dbc);
239 return NULL;
240}