blob: 2c6b243a74db043a2c5ffa245c6aa0b7ae25e675 [file] [log] [blame]
Harald Weltee687be52016-05-03 18:49:27 +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 <string.h>
Max00b37152017-02-20 11:09:27 +010021#include <errno.h>
Harald Weltee687be52016-05-03 18:49:27 +020022
23#include <osmocom/core/utils.h>
24#include <osmocom/crypt/auth.h>
25
26#include <sqlite3.h>
27
28#include "logging.h"
29#include "db.h"
30
31#define LOGHLR(imsi, level, fmt, args ...) LOGP(DAUC, level, "%s: " fmt, imsi, ## args)
32
33#define SL3_TXT(x, stmt, idx) do { \
34 const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \
35 if (_txt) \
36 strncpy(x, _txt, sizeof(x)); \
37 x[sizeof(x)-1] = '\0'; \
38 } while (0)
39
40int db_subscr_get(struct db_context *dbc, const char *imsi,
41 struct hlr_subscriber *subscr)
42{
43 sqlite3_stmt *stmt = dbc->stmt[SEL_BY_IMSI];
44 int rc, ret = 0;
45
Max00b37152017-02-20 11:09:27 +010046 if (!db_bind_imsi(stmt, imsi))
47 return -EINVAL;
Harald Weltee687be52016-05-03 18:49:27 +020048
49 /* execute the statement */
50 rc = sqlite3_step(stmt);
51 if (rc != SQLITE_ROW) {
52 LOGHLR(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc);
Max00b37152017-02-20 11:09:27 +010053 ret = -ENOEXEC;
Harald Weltee687be52016-05-03 18:49:27 +020054 goto out;
55 }
56
57 /* obtain the various columns */
58 subscr->id = sqlite3_column_int64(stmt, 0);
59 SL3_TXT(subscr->imsi, stmt, 1);
60 SL3_TXT(subscr->msisdn, stmt, 2);
Harald Welte99909272016-05-05 18:24:15 +020061 /* FIXME: These should all be BLOBs as they might contain NUL */
Harald Weltee687be52016-05-03 18:49:27 +020062 SL3_TXT(subscr->vlr_number, stmt, 3);
63 SL3_TXT(subscr->sgsn_number, stmt, 4);
64 SL3_TXT(subscr->sgsn_address, stmt, 5);
65 subscr->periodic_lu_timer = sqlite3_column_int(stmt, 6);
66 subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 7);
67 subscr->nam_cs = sqlite3_column_int(stmt, 8);
68 subscr->nam_ps = sqlite3_column_int(stmt, 9);
69 subscr->lmsi = sqlite3_column_int(stmt, 10);
70 subscr->ms_purged_cs = sqlite3_column_int(stmt, 11);
71 subscr->ms_purged_ps = sqlite3_column_int(stmt, 12);
72
73out:
Max00b37152017-02-20 11:09:27 +010074 db_remove_reset(stmt);
Harald Weltee687be52016-05-03 18:49:27 +020075
76 return ret;
77}
78
Max3ce36862017-02-20 11:18:04 +010079int db_subscr_ps(struct db_context *dbc, const char *imsi, bool enable)
80{
81 sqlite3_stmt *stmt =
82 dbc->stmt[enable ? SET_NAM_PS_BY_IMSI : UNSET_NAM_PS_BY_IMSI];
83 int rc;
84
85 if (!db_bind_imsi(stmt, imsi))
86 return -EINVAL;
87
88 rc = sqlite3_step(stmt); /* execute the statement */
89 if (rc != SQLITE_DONE) {
90 LOGHLR(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc);
91 rc = -ENOEXEC;
92 }
93
94 rc = sqlite3_changes(dbc->db); /* verify execution result */
95 if (rc != 1) {
96 LOGHLR(imsi, LOGL_ERROR, "SQL modified %d rows (expected 1)\n",
97 rc);
98 rc = -EINVAL;
99 }
100
101 db_remove_reset(stmt);
102 return rc;
103}
104
Harald Weltee687be52016-05-03 18:49:27 +0200105int db_subscr_lu(struct db_context *dbc,
106 const struct hlr_subscriber *subscr,
107 const char *vlr_or_sgsn_number, bool lu_is_ps)
108{
109 sqlite3_stmt *stmt = dbc->stmt[UPD_VLR_BY_ID];
110 const char *txt;
111 int rc, ret = 0;
112
113 if (lu_is_ps) {
114 stmt = dbc->stmt[UPD_SGSN_BY_ID];
115 txt = subscr->sgsn_number;
116 } else {
117 stmt = dbc->stmt[UPD_VLR_BY_ID];
118 txt = subscr->vlr_number;
119 }
120
121 rc = sqlite3_bind_int64(stmt, 1, subscr->id);
122 if (rc != SQLITE_OK) {
123 LOGP(DAUC, LOGL_ERROR, "Error binding ID: %d\n", rc);
Max00b37152017-02-20 11:09:27 +0100124 return -EINVAL;
Harald Weltee687be52016-05-03 18:49:27 +0200125 }
126
127 rc = sqlite3_bind_text(stmt, 2, txt, -1, SQLITE_STATIC);
128 if (rc != SQLITE_OK) {
129 LOGP(DAUC, LOGL_ERROR, "Error binding VLR/SGSN Number: %d\n", rc);
Max00b37152017-02-20 11:09:27 +0100130 ret = -EBADMSG;
Harald Weltee687be52016-05-03 18:49:27 +0200131 goto out;
132 }
133
134 /* execute the statement */
135 rc = sqlite3_step(stmt);
136 if (rc != SQLITE_DONE) {
137 LOGP(DAUC, LOGL_ERROR, "Error updating SQN: %d\n", rc);
Max00b37152017-02-20 11:09:27 +0100138 ret = -ENOEXEC;
Harald Weltee687be52016-05-03 18:49:27 +0200139 }
140out:
Max00b37152017-02-20 11:09:27 +0100141 db_remove_reset(stmt);
Harald Weltee687be52016-05-03 18:49:27 +0200142
143 return ret;
144}
Harald Welteb18f0e02016-05-05 21:03:03 +0200145
146int db_subscr_purge(struct db_context *dbc, const char *imsi, bool is_ps)
147{
148 sqlite3_stmt *stmt = dbc->stmt[UPD_VLR_BY_ID];
149 int rc, ret = 1;
150
151 if (is_ps)
152 stmt = dbc->stmt[UPD_PURGE_PS_BY_IMSI];
153 else
154 stmt = dbc->stmt[UPD_PURGE_CS_BY_IMSI];
155
Max00b37152017-02-20 11:09:27 +0100156 if (!db_bind_imsi(stmt, imsi))
157 return -EINVAL;
Harald Welteb18f0e02016-05-05 21:03:03 +0200158
159 /* execute the statement */
160 rc = sqlite3_step(stmt);
161 if (rc != SQLITE_DONE) {
162 LOGP(DAUC, LOGL_ERROR, "Error setting Purged: %d\n", rc);
Max00b37152017-02-20 11:09:27 +0100163 ret = -ENOEXEC;
Harald Welteb18f0e02016-05-05 21:03:03 +0200164 }
165 /* FIXME: return 0 in case IMSI not known */
Max00b37152017-02-20 11:09:27 +0100166
167 db_remove_reset(stmt);
Harald Welteb18f0e02016-05-05 21:03:03 +0200168
169 return ret;
170}