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