blob: baa04567b74ca5e4741dbebcd5af0139e024b86f [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>
21
22#include <osmocom/core/utils.h>
23#include <osmocom/crypt/auth.h>
24
25#include <sqlite3.h>
26
27#include "logging.h"
28#include "db.h"
29
30#define LOGHLR(imsi, level, fmt, args ...) LOGP(DAUC, level, "%s: " fmt, imsi, ## args)
31
32#define SL3_TXT(x, stmt, idx) do { \
33 const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \
34 if (_txt) \
35 strncpy(x, _txt, sizeof(x)); \
36 x[sizeof(x)-1] = '\0'; \
37 } while (0)
38
39int db_subscr_get(struct db_context *dbc, const char *imsi,
40 struct hlr_subscriber *subscr)
41{
42 sqlite3_stmt *stmt = dbc->stmt[SEL_BY_IMSI];
43 int rc, ret = 0;
44
45 rc = sqlite3_bind_text(stmt, 1, imsi, -1, SQLITE_STATIC);
46 if (rc != SQLITE_OK) {
47 LOGHLR(imsi, LOGL_ERROR, "Error binding IMSI: %d\n", rc);
48 return -1;
49 }
50
51 /* execute the statement */
52 rc = sqlite3_step(stmt);
53 if (rc != SQLITE_ROW) {
54 LOGHLR(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc);
55 ret = -2;
56 goto out;
57 }
58
59 /* obtain the various columns */
60 subscr->id = sqlite3_column_int64(stmt, 0);
61 SL3_TXT(subscr->imsi, stmt, 1);
62 SL3_TXT(subscr->msisdn, stmt, 2);
Harald Welte99909272016-05-05 18:24:15 +020063 /* FIXME: These should all be BLOBs as they might contain NUL */
Harald Weltee687be52016-05-03 18:49:27 +020064 SL3_TXT(subscr->vlr_number, stmt, 3);
65 SL3_TXT(subscr->sgsn_number, stmt, 4);
66 SL3_TXT(subscr->sgsn_address, stmt, 5);
67 subscr->periodic_lu_timer = sqlite3_column_int(stmt, 6);
68 subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 7);
69 subscr->nam_cs = sqlite3_column_int(stmt, 8);
70 subscr->nam_ps = sqlite3_column_int(stmt, 9);
71 subscr->lmsi = sqlite3_column_int(stmt, 10);
72 subscr->ms_purged_cs = sqlite3_column_int(stmt, 11);
73 subscr->ms_purged_ps = sqlite3_column_int(stmt, 12);
74
75out:
76 /* remove bindings and reset statement to be re-executed */
77 rc = sqlite3_clear_bindings(stmt);
78 if (rc != SQLITE_OK) {
79 LOGP(DAUC, LOGL_ERROR, "Error clerearing bindings: %d\n", rc);
80 }
81 rc = sqlite3_reset(stmt);
82 if (rc != SQLITE_OK) {
83 LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc);
84 }
85
86 return ret;
87}
88
89int db_subscr_lu(struct db_context *dbc,
90 const struct hlr_subscriber *subscr,
91 const char *vlr_or_sgsn_number, bool lu_is_ps)
92{
93 sqlite3_stmt *stmt = dbc->stmt[UPD_VLR_BY_ID];
94 const char *txt;
95 int rc, ret = 0;
96
97 if (lu_is_ps) {
98 stmt = dbc->stmt[UPD_SGSN_BY_ID];
99 txt = subscr->sgsn_number;
100 } else {
101 stmt = dbc->stmt[UPD_VLR_BY_ID];
102 txt = subscr->vlr_number;
103 }
104
105 rc = sqlite3_bind_int64(stmt, 1, subscr->id);
106 if (rc != SQLITE_OK) {
107 LOGP(DAUC, LOGL_ERROR, "Error binding ID: %d\n", rc);
108 return -1;
109 }
110
111 rc = sqlite3_bind_text(stmt, 2, txt, -1, SQLITE_STATIC);
112 if (rc != SQLITE_OK) {
113 LOGP(DAUC, LOGL_ERROR, "Error binding VLR/SGSN Number: %d\n", rc);
114 ret = -2;
115 goto out;
116 }
117
118 /* execute the statement */
119 rc = sqlite3_step(stmt);
120 if (rc != SQLITE_DONE) {
121 LOGP(DAUC, LOGL_ERROR, "Error updating SQN: %d\n", rc);
122 ret = -3;
123 goto out;
124 }
125out:
126 /* remove bindings and reset statement to be re-executed */
127 rc = sqlite3_clear_bindings(stmt);
128 if (rc != SQLITE_OK) {
129 LOGP(DAUC, LOGL_ERROR, "Error clerearing bindings: %d\n", rc);
130 }
131 rc = sqlite3_reset(stmt);
132 if (rc != SQLITE_OK) {
133 LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc);
134 }
135
136 return ret;
137}