blob: f546e461b5750a7f411b620247c27bb6d1d3ad27 [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
Stefan Sperling5c14c9c2018-12-07 12:30:21 +010020#define _POSIX_C_SOURCE 200809L /* for strptime(3) */
21/* These are needed as well due to the above _POSIX_C_SOURCE definition: */
22#define _DEFAULT_SOURCE /* for struct timezone */
23#define _XOPEN_SOURCE /* for clockid_t */
24
Harald Weltee687be52016-05-03 18:49:27 +020025#include <string.h>
Max00b37152017-02-20 11:09:27 +010026#include <errno.h>
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020027#include <inttypes.h>
Stefan Sperling638ba8c2018-12-04 15:07:29 +010028#include <time.h>
Harald Weltee687be52016-05-03 18:49:27 +020029
30#include <osmocom/core/utils.h>
31#include <osmocom/crypt/auth.h>
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020032#include <osmocom/gsm/gsm23003.h>
Harald Weltee687be52016-05-03 18:49:27 +020033
34#include <sqlite3.h>
35
36#include "logging.h"
Neels Hofmeyr00b1d432017-10-17 01:43:48 +020037#include "hlr.h"
Harald Weltee687be52016-05-03 18:49:27 +020038#include "db.h"
Neels Hofmeyr00b1d432017-10-17 01:43:48 +020039#include "gsup_server.h"
40#include "luop.h"
Harald Weltee687be52016-05-03 18:49:27 +020041
Neels Hofmeyr40aa61c2017-10-09 17:56:04 +020042#define LOGHLR(imsi, level, fmt, args ...) LOGP(DAUC, level, "IMSI='%s': " fmt, imsi, ## args)
Harald Weltee687be52016-05-03 18:49:27 +020043
Neels Hofmeyr16140f72017-10-25 19:17:18 +020044/*! Add new subscriber record to the HLR database.
45 * \param[in,out] dbc database context.
46 * \param[in] imsi ASCII string of IMSI digits, is validated.
47 * \returns 0 on success, -EINVAL on invalid IMSI, -EIO on database error.
48 */
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020049int db_subscr_create(struct db_context *dbc, const char *imsi)
50{
51 sqlite3_stmt *stmt;
52 int rc;
53
54 if (!osmo_imsi_str_valid(imsi)) {
55 LOGP(DAUC, LOGL_ERROR, "Cannot create subscriber: invalid IMSI: '%s'\n",
56 imsi);
57 return -EINVAL;
58 }
59
60 stmt = dbc->stmt[DB_STMT_SUBSCR_CREATE];
61
62 if (!db_bind_text(stmt, "$imsi", imsi))
63 return -EIO;
64
65 /* execute the statement */
66 rc = sqlite3_step(stmt);
67 db_remove_reset(stmt);
68 if (rc != SQLITE_DONE) {
69 LOGHLR(imsi, LOGL_ERROR, "Cannot create subscriber: SQL error: (%d) %s\n",
70 rc, sqlite3_errmsg(dbc->db));
71 return -EIO;
72 }
73
74 return 0;
75}
76
Neels Hofmeyr16140f72017-10-25 19:17:18 +020077/*! Completely delete a subscriber record from the HLR database.
78 * Also remove authentication data.
79 * Future todo: also drop from all other database tables, which aren't used yet
80 * at the time of writing this.
81 * \param[in,out] dbc database context.
82 * \param[in] subscr_id ID of the subscriber in the HLR db.
83 * \returns if the subscriber was found and removed, -EIO on database error,
84 * -ENOENT if no such subscriber data exists.
85 */
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020086int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id)
87{
88 int rc;
Neels Hofmeyr1332a172017-10-10 02:25:00 +020089 struct sub_auth_data_str aud;
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020090 int ret = 0;
91
92 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_DEL_BY_ID];
93
94 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
95 return -EIO;
96
97 /* execute the statement */
98 rc = sqlite3_step(stmt);
99 if (rc != SQLITE_DONE) {
100 LOGP(DAUC, LOGL_ERROR,
Stefan Sperling705b61b2018-12-07 12:44:50 +0100101 "Cannot delete subscriber ID=%" PRId64 ": SQL error: (%d) %s\n",
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200102 subscr_id, rc, sqlite3_errmsg(dbc->db));
103 db_remove_reset(stmt);
104 return -EIO;
105 }
106
107 /* verify execution result */
108 rc = sqlite3_changes(dbc->db);
109 if (!rc) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100110 LOGP(DAUC, LOGL_ERROR, "Cannot delete: no such subscriber: ID=%" PRId64 "\n",
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200111 subscr_id);
112 ret = -ENOENT;
113 } else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100114 LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%" PRId64
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200115 ": SQL modified %d rows (expected 1)\n", subscr_id, rc);
116 ret = -EIO;
117 }
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200118 db_remove_reset(stmt);
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200119
120 /* make sure to remove authentication data for this subscriber id, for
121 * both 2G and 3G. */
122
123 aud = (struct sub_auth_data_str){
124 .type = OSMO_AUTH_TYPE_GSM,
125 .algo = OSMO_AUTH_ALG_NONE,
126 };
127 rc = db_subscr_update_aud_by_id(dbc, subscr_id, &aud);
128 if (ret == -ENOENT && !rc)
129 ret = 0;
130
131 aud = (struct sub_auth_data_str){
132 .type = OSMO_AUTH_TYPE_UMTS,
133 .algo = OSMO_AUTH_ALG_NONE,
134 };
135 rc = db_subscr_update_aud_by_id(dbc, subscr_id, &aud);
136 if (ret == -ENOENT && !rc)
137 ret = 0;
138
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200139 return ret;
140}
141
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200142/*! Set a subscriber's MSISDN in the HLR database.
143 * \param[in,out] dbc database context.
Neels Hofmeyra820ea12018-12-02 19:46:46 +0100144 * \param[in] imsi ASCII string of IMSI digits, or NULL to remove the MSISDN.
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200145 * \param[in] msisdn ASCII string of MSISDN digits.
146 * \returns 0 on success, -EINVAL in case of invalid MSISDN string, -EIO on
147 * database failure, -ENOENT if no such subscriber exists.
148 */
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200149int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi,
150 const char *msisdn)
151{
152 int rc;
153 int ret = 0;
154
Neels Hofmeyra820ea12018-12-02 19:46:46 +0100155 if (msisdn && !osmo_msisdn_str_valid(msisdn)) {
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200156 LOGHLR(imsi, LOGL_ERROR,
157 "Cannot update subscriber: invalid MSISDN: '%s'\n",
158 msisdn);
159 return -EINVAL;
160 }
161
Neels Hofmeyra820ea12018-12-02 19:46:46 +0100162 sqlite3_stmt *stmt = dbc->stmt[
163 msisdn ? DB_STMT_SET_MSISDN_BY_IMSI : DB_STMT_DELETE_MSISDN_BY_IMSI];
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200164
165 if (!db_bind_text(stmt, "$imsi", imsi))
166 return -EIO;
Neels Hofmeyra820ea12018-12-02 19:46:46 +0100167 if (msisdn) {
168 if (!db_bind_text(stmt, "$msisdn", msisdn))
169 return -EIO;
170 }
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200171
172 /* execute the statement */
173 rc = sqlite3_step(stmt);
174 if (rc != SQLITE_DONE) {
175 LOGHLR(imsi, LOGL_ERROR,
176 "Cannot update subscriber's MSISDN: SQL error: (%d) %s\n",
177 rc, sqlite3_errmsg(dbc->db));
178 ret = -EIO;
179 goto out;
180 }
181
182 /* verify execution result */
183 rc = sqlite3_changes(dbc->db);
184 if (!rc) {
185 LOGP(DAUC, LOGL_ERROR, "Cannot update MSISDN: no such subscriber: IMSI='%s'\n",
186 imsi);
187 ret = -ENOENT;
188 goto out;
189 } else if (rc != 1) {
190 LOGHLR(imsi, LOGL_ERROR, "Update MSISDN: SQL modified %d rows (expected 1)\n", rc);
191 ret = -EIO;
192 }
193
194out:
195 db_remove_reset(stmt);
196 return ret;
197
198}
199
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200200/*! Insert or update 2G or 3G authentication tokens in the database.
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200201 * If aud->type is OSMO_AUTH_TYPE_GSM, the auc_2g table entry for the
202 * subscriber will be added or modified; if aud->algo is OSMO_AUTH_ALG_NONE,
203 * however, the auc_2g entry for the subscriber is deleted. If aud->type is
204 * OSMO_AUTH_TYPE_UMTS, the auc_3g table is updated; again, if aud->algo is
205 * OSMO_AUTH_ALG_NONE, the auc_3g entry is deleted.
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200206 * \param[in,out] dbc database context.
207 * \param[in] subscr_id DB ID of the subscriber.
208 * \param[in] aud Pointer to new auth data (in ASCII string form).
209 * \returns 0 on success, -EINVAL for invalid aud, -ENOENT for unknown
210 * subscr_id, -EIO for database errors.
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200211 */
212int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id,
213 const struct sub_auth_data_str *aud)
214{
215 sqlite3_stmt *stmt_del;
216 sqlite3_stmt *stmt_ins;
217 sqlite3_stmt *stmt;
218 const char *label;
219 int rc;
220 int ret = 0;
221
222 switch (aud->type) {
223 case OSMO_AUTH_TYPE_GSM:
224 label = "auc_2g";
225 stmt_del = dbc->stmt[DB_STMT_AUC_2G_DELETE];
226 stmt_ins = dbc->stmt[DB_STMT_AUC_2G_INSERT];
227
228 switch (aud->algo) {
229 case OSMO_AUTH_ALG_NONE:
230 case OSMO_AUTH_ALG_COMP128v1:
231 case OSMO_AUTH_ALG_COMP128v2:
232 case OSMO_AUTH_ALG_COMP128v3:
233 case OSMO_AUTH_ALG_XOR:
234 break;
235 case OSMO_AUTH_ALG_MILENAGE:
236 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
237 " auth algo not suited for 2G: %s\n",
238 osmo_auth_alg_name(aud->algo));
239 return -EINVAL;
240 default:
241 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
242 " Unknown auth algo: %d\n", aud->algo);
243 return -EINVAL;
244 }
245
246 if (aud->algo == OSMO_AUTH_ALG_NONE)
247 break;
248 if (!osmo_is_hexstr(aud->u.gsm.ki, 32, 32, true)) {
249 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
250 " Invalid KI: '%s'\n", aud->u.gsm.ki);
251 return -EINVAL;
252 }
253 break;
254
255 case OSMO_AUTH_TYPE_UMTS:
256 label = "auc_3g";
257 stmt_del = dbc->stmt[DB_STMT_AUC_3G_DELETE];
258 stmt_ins = dbc->stmt[DB_STMT_AUC_3G_INSERT];
259 switch (aud->algo) {
260 case OSMO_AUTH_ALG_NONE:
261 case OSMO_AUTH_ALG_MILENAGE:
262 break;
263 case OSMO_AUTH_ALG_COMP128v1:
264 case OSMO_AUTH_ALG_COMP128v2:
265 case OSMO_AUTH_ALG_COMP128v3:
266 case OSMO_AUTH_ALG_XOR:
267 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
268 " auth algo not suited for 3G: %s\n",
269 osmo_auth_alg_name(aud->algo));
270 return -EINVAL;
271 default:
272 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
273 " Unknown auth algo: %d\n", aud->algo);
274 return -EINVAL;
275 }
276
277 if (aud->algo == OSMO_AUTH_ALG_NONE)
278 break;
279 if (!osmo_is_hexstr(aud->u.umts.k, 32, 32, true)) {
280 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
281 " Invalid K: '%s'\n", aud->u.umts.k);
282 return -EINVAL;
283 }
284 if (!osmo_is_hexstr(aud->u.umts.opc, 32, 32, true)) {
285 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
286 " Invalid OP/OPC: '%s'\n", aud->u.umts.opc);
287 return -EINVAL;
288 }
289 if (aud->u.umts.ind_bitlen > OSMO_MILENAGE_IND_BITLEN_MAX) {
290 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
291 " Invalid ind_bitlen: %d\n", aud->u.umts.ind_bitlen);
292 return -EINVAL;
293 }
294 break;
295 default:
296 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
297 " unknown auth type: %d\n", aud->type);
298 return -EINVAL;
299 }
300
301 stmt = stmt_del;
302
303 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
304 return -EIO;
305
306 /* execute the statement */
307 rc = sqlite3_step(stmt);
308 if (rc != SQLITE_DONE) {
309 LOGP(DAUC, LOGL_ERROR,
310 "Cannot delete %s row: SQL error: (%d) %s\n",
311 label, rc, sqlite3_errmsg(dbc->db));
312 ret = -EIO;
313 goto out;
314 }
315
316 /* verify execution result */
317 rc = sqlite3_changes(dbc->db);
318 if (!rc)
319 /* Leave "no such entry" logging to the caller -- during
320 * db_subscr_delete_by_id(), we call this to make sure it is
321 * empty, and no entry is not an error then.*/
322 ret = -ENOENT;
323 else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100324 LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%" PRId64
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200325 " from %s: SQL modified %d rows (expected 1)\n",
326 subscr_id, label, rc);
327 ret = -EIO;
328 }
329
330 db_remove_reset(stmt);
331
332 /* Error situation? Return now. */
333 if (ret && ret != -ENOENT)
334 return ret;
335
336 /* Just delete requested? */
337 if (aud->algo == OSMO_AUTH_ALG_NONE)
338 return ret;
339
340 /* Don't return -ENOENT if inserting new data. */
341 ret = 0;
342
343 /* Insert new row */
344 stmt = stmt_ins;
345
346 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
347 return -EIO;
348
349 switch (aud->type) {
350 case OSMO_AUTH_TYPE_GSM:
351 if (!db_bind_int(stmt, "$algo_id_2g", aud->algo))
352 return -EIO;
353 if (!db_bind_text(stmt, "$ki", aud->u.gsm.ki))
354 return -EIO;
355 break;
356 case OSMO_AUTH_TYPE_UMTS:
357 if (!db_bind_int(stmt, "$algo_id_3g", aud->algo))
358 return -EIO;
359 if (!db_bind_text(stmt, "$k", aud->u.umts.k))
360 return -EIO;
361 if (!db_bind_text(stmt, "$op",
362 aud->u.umts.opc_is_op ? aud->u.umts.opc : NULL))
363 return -EIO;
364 if (!db_bind_text(stmt, "$opc",
365 aud->u.umts.opc_is_op ? NULL : aud->u.umts.opc))
366 return -EIO;
367 if (!db_bind_int(stmt, "$ind_bitlen", aud->u.umts.ind_bitlen))
368 return -EIO;
369 break;
370 default:
371 OSMO_ASSERT(false);
372 }
373
374 /* execute the statement */
375 rc = sqlite3_step(stmt);
376 if (rc != SQLITE_DONE) {
377 LOGP(DAUC, LOGL_ERROR,
378 "Cannot insert %s row: SQL error: (%d) %s\n",
379 label, rc, sqlite3_errmsg(dbc->db));
380 ret = -EIO;
381 goto out;
382 }
383
384out:
385 db_remove_reset(stmt);
386 return ret;
387}
388
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200389/* Common code for db_subscr_get_by_*() functions. */
390static int db_sel(struct db_context *dbc, sqlite3_stmt *stmt, struct hlr_subscriber *subscr,
391 const char **err)
Harald Weltee687be52016-05-03 18:49:27 +0200392{
Maxadc66482017-02-20 11:23:20 +0100393 int rc;
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200394 int ret = 0;
Stefan Sperling5c14c9c2018-12-07 12:30:21 +0100395 const char *last_lu_seen_str;
396 struct tm tm;
Harald Weltee687be52016-05-03 18:49:27 +0200397
398 /* execute the statement */
399 rc = sqlite3_step(stmt);
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200400 if (rc == SQLITE_DONE) {
401 ret = -ENOENT;
402 goto out;
403 }
Harald Weltee687be52016-05-03 18:49:27 +0200404 if (rc != SQLITE_ROW) {
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200405 ret = -EIO;
406 goto out;
Maxadc66482017-02-20 11:23:20 +0100407 }
408
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200409 if (!subscr)
410 goto out;
Harald Weltee687be52016-05-03 18:49:27 +0200411
Neels Hofmeyr2fb33f12018-12-26 01:49:53 +0100412 *subscr = hlr_subscriber_empty;
Neels Hofmeyrb6837e32017-10-10 23:20:26 +0200413
Harald Weltee687be52016-05-03 18:49:27 +0200414 /* obtain the various columns */
415 subscr->id = sqlite3_column_int64(stmt, 0);
Neels Hofmeyrdbced932017-10-27 02:57:51 +0200416 copy_sqlite3_text_to_buf(subscr->imsi, stmt, 1);
417 copy_sqlite3_text_to_buf(subscr->msisdn, stmt, 2);
Harald Welte99909272016-05-05 18:24:15 +0200418 /* FIXME: These should all be BLOBs as they might contain NUL */
Neels Hofmeyrdbced932017-10-27 02:57:51 +0200419 copy_sqlite3_text_to_buf(subscr->vlr_number, stmt, 3);
420 copy_sqlite3_text_to_buf(subscr->sgsn_number, stmt, 4);
421 copy_sqlite3_text_to_buf(subscr->sgsn_address, stmt, 5);
Harald Weltee687be52016-05-03 18:49:27 +0200422 subscr->periodic_lu_timer = sqlite3_column_int(stmt, 6);
423 subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 7);
424 subscr->nam_cs = sqlite3_column_int(stmt, 8);
425 subscr->nam_ps = sqlite3_column_int(stmt, 9);
426 subscr->lmsi = sqlite3_column_int(stmt, 10);
427 subscr->ms_purged_cs = sqlite3_column_int(stmt, 11);
428 subscr->ms_purged_ps = sqlite3_column_int(stmt, 12);
Stefan Sperling5c14c9c2018-12-07 12:30:21 +0100429 last_lu_seen_str = (const char *)sqlite3_column_text(stmt, 13);
430 if (last_lu_seen_str && last_lu_seen_str[0] != '\0') {
431 if (strptime(last_lu_seen_str, DB_LAST_LU_SEEN_FMT, &tm) == NULL) {
432 LOGP(DAUC, LOGL_ERROR, "Cannot parse last LU timestamp '%s' of subscriber with IMSI='%s': %s\n",
433 last_lu_seen_str, subscr->imsi, strerror(errno));
434 } else {
435 subscr->last_lu_seen = mktime(&tm);
436 if (subscr->last_lu_seen == -1) {
437 LOGP(DAUC, LOGL_ERROR, "Cannot convert LU timestamp '%s' to time_t: %s\n",
438 last_lu_seen_str, strerror(errno));
439 subscr->last_lu_seen = 0;
440 }
441 }
442 }
Harald Weltee687be52016-05-03 18:49:27 +0200443
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200444out:
Max00b37152017-02-20 11:09:27 +0100445 db_remove_reset(stmt);
Harald Weltee687be52016-05-03 18:49:27 +0200446
Neels Hofmeyr2fb33f12018-12-26 01:49:53 +0100447 if (ret == 0)
448 db_subscr_get_rat_types(dbc, subscr);
449
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200450 switch (ret) {
451 case 0:
452 *err = NULL;
453 break;
454 case -ENOENT:
455 *err = "No such subscriber";
456 break;
457 default:
458 *err = sqlite3_errmsg(dbc->db);
459 break;
460 }
461 return ret;
462}
463
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200464/*! Retrieve subscriber data from the HLR database.
465 * \param[in,out] dbc database context.
466 * \param[in] imsi ASCII string of IMSI digits.
467 * \param[out] subscr place retrieved data in this struct.
468 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
469 * database error.
470 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200471int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
472 struct hlr_subscriber *subscr)
473{
474 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI];
475 const char *err;
476 int rc;
477
478 if (!db_bind_text(stmt, NULL, imsi))
479 return -EIO;
480
481 rc = db_sel(dbc, stmt, subscr, &err);
482 if (rc)
483 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMSI='%s': %s\n",
484 imsi, err);
485 return rc;
486}
487
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200488/*! Retrieve subscriber data from the HLR database.
489 * \param[in,out] dbc database context.
490 * \param[in] msisdn ASCII string of MSISDN digits.
491 * \param[out] subscr place retrieved data in this struct.
492 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
493 * database error.
494 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200495int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
496 struct hlr_subscriber *subscr)
497{
498 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_MSISDN];
499 const char *err;
500 int rc;
501
502 if (!db_bind_text(stmt, NULL, msisdn))
503 return -EIO;
504
505 rc = db_sel(dbc, stmt, subscr, &err);
506 if (rc)
507 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: MSISDN='%s': %s\n",
508 msisdn, err);
509 return rc;
510}
511
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200512/*! Retrieve subscriber data from the HLR database.
513 * \param[in,out] dbc database context.
514 * \param[in] id ID of the subscriber in the HLR db.
515 * \param[out] subscr place retrieved data in this struct.
516 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
517 * database error.
518 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200519int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
520 struct hlr_subscriber *subscr)
521{
522 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_ID];
523 const char *err;
524 int rc;
525
526 if (!db_bind_int64(stmt, NULL, id))
527 return -EIO;
528
529 rc = db_sel(dbc, stmt, subscr, &err);
530 if (rc)
Stefan Sperling705b61b2018-12-07 12:44:50 +0100531 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: ID=%" PRId64 ": %s\n",
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200532 id, err);
533 return rc;
Harald Weltee687be52016-05-03 18:49:27 +0200534}
535
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200536/*! You should use hlr_subscr_nam() instead; enable or disable PS or CS for a
537 * subscriber without notifying GSUP clients.
538 * \param[in,out] dbc database context.
539 * \param[in] imsi ASCII string of IMSI digits.
540 * \param[in] nam_val True to enable CS/PS, false to disable.
541 * \param[in] is_ps when true, set nam_ps, else set nam_cs.
542 * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on
543 * database errors.
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200544 */
545int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps)
Max3ce36862017-02-20 11:18:04 +0100546{
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200547 sqlite3_stmt *stmt;
Max3ce36862017-02-20 11:18:04 +0100548 int rc;
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200549 int ret = 0;
Max3ce36862017-02-20 11:18:04 +0100550
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200551 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_NAM_PS_BY_IMSI
552 : DB_STMT_UPD_NAM_CS_BY_IMSI];
Max3ce36862017-02-20 11:18:04 +0100553
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200554 if (!db_bind_text(stmt, "$imsi", imsi))
555 return -EIO;
556 if (!db_bind_int(stmt, "$val", nam_val ? 1 : 0))
557 return -EIO;
558
559 /* execute the statement */
560 rc = sqlite3_step(stmt);
Max3ce36862017-02-20 11:18:04 +0100561 if (rc != SQLITE_DONE) {
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200562 LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL error: %s\n",
563 nam_val ? "enable" : "disable",
564 is_ps ? "PS" : "CS",
565 sqlite3_errmsg(dbc->db));
566 ret = -EIO;
567 goto out;
Max3ce36862017-02-20 11:18:04 +0100568 }
569
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200570 /* verify execution result */
571 rc = sqlite3_changes(dbc->db);
572 if (!rc) {
573 LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n",
574 nam_val ? "enable" : "disable",
575 is_ps ? "PS" : "CS",
576 imsi);
577 ret = -ENOENT;
578 goto out;
579 } else if (rc != 1) {
580 LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n",
581 nam_val ? "enable" : "disable",
582 is_ps ? "PS" : "CS",
Max3ce36862017-02-20 11:18:04 +0100583 rc);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200584 ret = -EIO;
Max3ce36862017-02-20 11:18:04 +0100585 }
586
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200587out:
Max3ce36862017-02-20 11:18:04 +0100588 db_remove_reset(stmt);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200589 return ret;
Max3ce36862017-02-20 11:18:04 +0100590}
591
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200592/*! Record a Location Updating in the database.
593 * \param[in,out] dbc database context.
594 * \param[in] subscr_id ID of the subscriber in the HLR db.
595 * \param[in] vlr_or_sgsn_number ASCII string of identifier digits.
596 * \param[in] is_ps when true, set sgsn_number, else set vlr_number.
597 * \returns 0 on success, -ENOENT when the given subscriber does not exist,
598 * -EIO on database errors.
599 */
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200600int db_subscr_lu(struct db_context *dbc, int64_t subscr_id,
601 const char *vlr_or_sgsn_number, bool is_ps)
Harald Weltee687be52016-05-03 18:49:27 +0200602{
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200603 sqlite3_stmt *stmt;
Harald Weltee687be52016-05-03 18:49:27 +0200604 int rc, ret = 0;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100605 struct timespec localtime;
Harald Weltee687be52016-05-03 18:49:27 +0200606
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200607 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_SGSN_BY_ID
608 : DB_STMT_UPD_VLR_BY_ID];
Harald Weltee687be52016-05-03 18:49:27 +0200609
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200610 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
611 return -EIO;
Harald Weltee687be52016-05-03 18:49:27 +0200612
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200613 if (!db_bind_text(stmt, "$number", vlr_or_sgsn_number))
614 return -EIO;
Harald Weltee687be52016-05-03 18:49:27 +0200615
616 /* execute the statement */
617 rc = sqlite3_step(stmt);
618 if (rc != SQLITE_DONE) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100619 LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64 ": SQL Error: %s\n",
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200620 is_ps? "SGSN" : "VLR", subscr_id, sqlite3_errmsg(dbc->db));
621 ret = -EIO;
622 goto out;
Harald Weltee687be52016-05-03 18:49:27 +0200623 }
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200624
625 /* verify execution result */
626 rc = sqlite3_changes(dbc->db);
627 if (!rc) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100628 LOGP(DAUC, LOGL_ERROR, "Cannot update %s number for subscriber ID=%" PRId64
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200629 ": no such subscriber\n",
630 is_ps? "SGSN" : "VLR", subscr_id);
631 ret = -ENOENT;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100632 goto out;
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200633 } else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100634 LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200635 ": SQL modified %d rows (expected 1)\n",
636 is_ps? "SGSN" : "VLR", subscr_id, rc);
637 ret = -EIO;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100638 goto out;
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200639 }
640
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100641 db_remove_reset(stmt);
642
643 if (osmo_clock_gettime(CLOCK_REALTIME, &localtime) != 0) {
644 LOGP(DAUC, LOGL_ERROR, "Cannot get the current time: (%d) %s\n", errno, strerror(errno));
645 ret = -errno;
646 goto out;
647 }
648
649 stmt = dbc->stmt[DB_STMT_SET_LAST_LU_SEEN];
650
651 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
652 return -EIO;
653 /* The timestamp will be converted to UTC by SQLite. */
654 if (!db_bind_int64(stmt, "$val", (int64_t)localtime.tv_sec)) {
655 ret = -EIO;
656 goto out;
657 }
658
659 rc = sqlite3_step(stmt);
660 if (rc != SQLITE_DONE) {
661 LOGP(DAUC, LOGL_ERROR,
Stefan Sperling705b61b2018-12-07 12:44:50 +0100662 "Cannot update LU timestamp for subscriber ID=%" PRId64 ": SQL error: (%d) %s\n",
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100663 subscr_id, rc, sqlite3_errmsg(dbc->db));
664 ret = -EIO;
665 goto out;
666 }
667
668 /* verify execution result */
669 rc = sqlite3_changes(dbc->db);
670 if (!rc) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100671 LOGP(DAUC, LOGL_ERROR, "Cannot update LU timestamp for subscriber ID=%" PRId64
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100672 ": no such subscriber\n", subscr_id);
673 ret = -ENOENT;
674 goto out;
675 } else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100676 LOGP(DAUC, LOGL_ERROR, "Update LU timestamp for subscriber ID=%" PRId64
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100677 ": SQL modified %d rows (expected 1)\n", subscr_id, rc);
678 ret = -EIO;
679 }
Harald Weltee687be52016-05-03 18:49:27 +0200680out:
Max00b37152017-02-20 11:09:27 +0100681 db_remove_reset(stmt);
Harald Weltee687be52016-05-03 18:49:27 +0200682 return ret;
683}
Harald Welteb18f0e02016-05-05 21:03:03 +0200684
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200685/*! Set the ms_purged_cs or ms_purged_ps values in the database.
686 * \param[in,out] dbc database context.
687 * \param[in] by_imsi ASCII string of IMSI digits.
688 * \param[in] purge_val true to purge, false to un-purge.
689 * \param[in] is_ps when true, set ms_purged_ps, else set ms_purged_cs.
690 * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on
691 * database errors.
692 */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200693int db_subscr_purge(struct db_context *dbc, const char *by_imsi,
694 bool purge_val, bool is_ps)
Harald Welteb18f0e02016-05-05 21:03:03 +0200695{
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200696 sqlite3_stmt *stmt;
697 int rc, ret = 0;
Harald Welteb18f0e02016-05-05 21:03:03 +0200698
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200699 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_PURGE_PS_BY_IMSI
700 : DB_STMT_UPD_PURGE_CS_BY_IMSI];
Harald Welteb18f0e02016-05-05 21:03:03 +0200701
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200702 if (!db_bind_text(stmt, "$imsi", by_imsi))
703 return -EIO;
704 if (!db_bind_int(stmt, "$val", purge_val ? 1 : 0))
705 return -EIO;
Harald Welteb18f0e02016-05-05 21:03:03 +0200706
707 /* execute the statement */
708 rc = sqlite3_step(stmt);
709 if (rc != SQLITE_DONE) {
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200710 LOGP(DAUC, LOGL_ERROR, "%s %s: SQL error: %s\n",
711 purge_val ? "purge" : "un-purge",
712 is_ps ? "PS" : "CS",
713 sqlite3_errmsg(dbc->db));
714 ret = -EIO;
715 goto out;
Harald Welteb18f0e02016-05-05 21:03:03 +0200716 }
Max00b37152017-02-20 11:09:27 +0100717
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200718 /* verify execution result */
719 rc = sqlite3_changes(dbc->db);
720 if (!rc) {
721 LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n",
722 purge_val ? "purge" : "un-purge",
723 is_ps ? "PS" : "CS",
724 by_imsi);
725 ret = -ENOENT;
726 goto out;
727 } else if (rc != 1) {
728 LOGHLR(by_imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n",
729 purge_val ? "purge" : "un-purge",
730 is_ps ? "PS" : "CS",
731 rc);
732 ret = -EIO;
733 }
734
735out:
Max00b37152017-02-20 11:09:27 +0100736 db_remove_reset(stmt);
Harald Welteb18f0e02016-05-05 21:03:03 +0200737
738 return ret;
739}
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200740
741/*! Update nam_cs/nam_ps in the db and trigger notifications to GSUP clients.
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200742 * \param[in,out] hlr Global hlr context.
743 * \param[in] subscr Subscriber from a fresh db_subscr_get_by_*() call.
744 * \param[in] nam_val True to enable CS/PS, false to disable.
745 * \param[in] is_ps True to enable/disable PS, false for CS.
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200746 * \returns 0 on success, ENOEXEC if there is no need to change, a negative
747 * value on error.
748 */
749int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps)
750{
751 int rc;
752 struct lu_operation *luop;
753 struct osmo_gsup_conn *co;
754 bool is_val = is_ps? subscr->nam_ps : subscr->nam_cs;
755
756 if (is_val == nam_val) {
757 LOGHLR(subscr->imsi, LOGL_DEBUG, "Already has the requested value when asked to %s %s\n",
758 nam_val ? "enable" : "disable", is_ps ? "PS" : "CS");
759 return ENOEXEC;
760 }
761
762 rc = db_subscr_nam(hlr->dbc, subscr->imsi, nam_val, is_ps);
763 if (rc)
764 return rc > 0? -rc : rc;
765
766 /* If we're disabling, send a notice out to the GSUP client that is
767 * responsible. Otherwise no need. */
768 if (nam_val)
769 return 0;
770
771 /* FIXME: only send to single SGSN where latest update for IMSI came from */
772 llist_for_each_entry(co, &hlr->gs->clients, list) {
773 luop = lu_op_alloc_conn(co);
774 if (!luop) {
775 LOGHLR(subscr->imsi, LOGL_ERROR,
776 "Cannot notify GSUP client, cannot allocate lu_operation,"
777 " for %s:%u\n",
778 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
779 co && co->conn && co->conn->server? co->conn->server->port : 0);
780 continue;
781 }
782 luop->subscr = *subscr;
783 lu_op_tx_del_subscr_data(luop);
784 lu_op_free(luop);
785 }
786 return 0;
787}
Neels Hofmeyr2fb33f12018-12-26 01:49:53 +0100788
789int db_subscr_set_rat_type_flag(struct db_context *dbc, int64_t subscr_id, enum osmo_rat_type rat, bool allowed)
790{
791 int rc;
792 int ret = 0;
793 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_UPD_RAT_FLAG];
794
795 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
796 return -EIO;
797
798 OSMO_ASSERT(rat >= 0 && rat < OSMO_RAT_COUNT);
799 if (!db_bind_text(stmt, "$rat", osmo_rat_type_name(rat)))
800 return -EIO;
801
802 if (!db_bind_int(stmt, "$allowed", allowed ? 1 : 0))
803 return -EIO;
804
805 /* execute the statement */
806 rc = sqlite3_step(stmt);
807 if (rc != SQLITE_DONE) {
808 LOGP(DDB, LOGL_ERROR, "%s %s: SQL error: %s\n",
809 allowed ? "enable" : "disable", osmo_rat_type_name(rat),
810 sqlite3_errmsg(dbc->db));
811 ret = -EIO;
812 goto out;
813 }
814
815 /* verify execution result */
816 rc = sqlite3_changes(dbc->db);
817 if (!rc) {
818 LOGP(DDB, LOGL_ERROR, "Cannot %s %s: no such subscriber: ID=%" PRIu64 "\n",
819 allowed ? "enable" : "disable", osmo_rat_type_name(rat),
820 subscr_id);
821 ret = -ENOENT;
822 goto out;
823 } else if (rc != 1) {
824 LOGP(DDB, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n",
825 allowed ? "enable" : "disable", osmo_rat_type_name(rat),
826 rc);
827 ret = -EIO;
828 }
829
830out:
831 db_remove_reset(stmt);
832 return ret;
833}
834
835int db_subscr_get_rat_types(struct db_context *dbc, struct hlr_subscriber *subscr)
836{
837 int rc;
838 int ret = 0;
839 int i;
840 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_RAT_BY_ID];
841
842 if (!db_bind_int64(stmt, "$subscriber_id", subscr->id))
843 return -EIO;
844
845 for (i = 0; i < OSMO_RAT_COUNT; i++)
846 subscr->rat_types[i] = true;
847
848 /* execute the statement */
849 while (1) {
850 enum osmo_rat_type rat;
851 bool allowed;
852
853 rc = sqlite3_step(stmt);
854
855 if (rc == SQLITE_DONE)
856 break;
857 if (rc != SQLITE_ROW)
858 return -rc;
859
860 rc = get_string_value(osmo_rat_type_names, (const char*)sqlite3_column_text(stmt, 0));
861 if (rc == -EINVAL) {
862 ret = -EINVAL;
863 goto out;
864 }
865 if (rc <= 0 || rc >= OSMO_RAT_COUNT) {
866 ret = -EINVAL;
867 goto out;
868 }
869 rat = rc;
870
871 allowed = sqlite3_column_int(stmt, 1);
872
873 subscr->rat_types[rat] = allowed;
874 LOGP(DAUC, LOGL_DEBUG, "db: imsi='%s' %s %s\n",
875 subscr->imsi, osmo_rat_type_name(rat), allowed ? "allowed" : "forbidden");
876 }
877
878out:
879 db_remove_reset(stmt);
880 return ret;
881}
882
883int hlr_subscr_rat_flag(struct hlr *hlr, struct hlr_subscriber *subscr, enum osmo_rat_type rat, bool allowed)
884{
885 int rc;
886 OSMO_ASSERT(rat >= 0 && rat < OSMO_RAT_COUNT);
887
888 db_subscr_get_rat_types(hlr->dbc, subscr);
889
890 if (subscr->rat_types[rat] == allowed) {
891 LOGHLR(subscr->imsi, LOGL_DEBUG, "Already has the requested value when asked to %s %s\n",
892 allowed ? "enable" : "disable", osmo_rat_type_name(rat));
893 return -ENOEXEC;
894 }
895
896 rc = db_subscr_set_rat_type_flag(hlr->dbc, subscr->id, rat, allowed);
897 if (rc)
898 return rc > 0? -rc : rc;
899
900 /* FIXME: If we're disabling, send message to VLR to detach subscriber */
901
902 return 0;
903}