blob: 1dc44150643658e7b0f58e2a88d8ce14628c1989 [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>
Neels Hofmeyrad868e22019-11-20 02:36:45 +010031#include <osmocom/core/timer.h>
Harald Weltee687be52016-05-03 18:49:27 +020032#include <osmocom/crypt/auth.h>
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020033#include <osmocom/gsm/gsm23003.h>
Harald Weltee687be52016-05-03 18:49:27 +020034
35#include <sqlite3.h>
36
Neels Hofmeyr2f758032019-11-20 00:37:07 +010037#include <osmocom/hlr/logging.h>
38#include <osmocom/hlr/hlr.h>
39#include <osmocom/hlr/db.h>
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010040#include <osmocom/gsupclient/cni_peer_id.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.
Oliver Smithcd2af5e2019-03-06 13:17:39 +010047 * \param[in] flags Bitmask of DB_SUBSCR_FLAG_*.
Pau Espin Pedrold456fce2022-06-17 17:56:56 +020048 * \returns 0 on success, -EINVAL on invalid IMSI, -EEXIST if subscriber with
49 * provided imsi already exists, -EIO on other database errors.
Neels Hofmeyr16140f72017-10-25 19:17:18 +020050 */
Oliver Smithcd2af5e2019-03-06 13:17:39 +010051int db_subscr_create(struct db_context *dbc, const char *imsi, uint8_t flags)
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020052{
53 sqlite3_stmt *stmt;
54 int rc;
55
56 if (!osmo_imsi_str_valid(imsi)) {
57 LOGP(DAUC, LOGL_ERROR, "Cannot create subscriber: invalid IMSI: '%s'\n",
58 imsi);
59 return -EINVAL;
60 }
61
62 stmt = dbc->stmt[DB_STMT_SUBSCR_CREATE];
63
64 if (!db_bind_text(stmt, "$imsi", imsi))
65 return -EIO;
Oliver Smithcd2af5e2019-03-06 13:17:39 +010066 if (!db_bind_int(stmt, "$nam_cs", (flags & DB_SUBSCR_FLAG_NAM_CS) != 0))
67 return -EIO;
68 if (!db_bind_int(stmt, "$nam_ps", (flags & DB_SUBSCR_FLAG_NAM_PS) != 0))
69 return -EIO;
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020070
71 /* execute the statement */
72 rc = sqlite3_step(stmt);
73 db_remove_reset(stmt);
74 if (rc != SQLITE_DONE) {
75 LOGHLR(imsi, LOGL_ERROR, "Cannot create subscriber: SQL error: (%d) %s\n",
76 rc, sqlite3_errmsg(dbc->db));
Pau Espin Pedrold456fce2022-06-17 17:56:56 +020077 if (rc == SQLITE_CONSTRAINT_UNIQUE)
78 return -EEXIST;
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020079 return -EIO;
80 }
81
82 return 0;
83}
84
Neels Hofmeyr16140f72017-10-25 19:17:18 +020085/*! Completely delete a subscriber record from the HLR database.
86 * Also remove authentication data.
87 * Future todo: also drop from all other database tables, which aren't used yet
88 * at the time of writing this.
89 * \param[in,out] dbc database context.
90 * \param[in] subscr_id ID of the subscriber in the HLR db.
91 * \returns if the subscriber was found and removed, -EIO on database error,
92 * -ENOENT if no such subscriber data exists.
93 */
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020094int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id)
95{
96 int rc;
Neels Hofmeyr1332a172017-10-10 02:25:00 +020097 struct sub_auth_data_str aud;
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020098 int ret = 0;
99
100 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_DEL_BY_ID];
101
102 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
103 return -EIO;
104
105 /* execute the statement */
106 rc = sqlite3_step(stmt);
107 if (rc != SQLITE_DONE) {
108 LOGP(DAUC, LOGL_ERROR,
Stefan Sperling705b61b2018-12-07 12:44:50 +0100109 "Cannot delete subscriber ID=%" PRId64 ": SQL error: (%d) %s\n",
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200110 subscr_id, rc, sqlite3_errmsg(dbc->db));
111 db_remove_reset(stmt);
112 return -EIO;
113 }
114
115 /* verify execution result */
116 rc = sqlite3_changes(dbc->db);
117 if (!rc) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100118 LOGP(DAUC, LOGL_ERROR, "Cannot delete: no such subscriber: ID=%" PRId64 "\n",
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200119 subscr_id);
120 ret = -ENOENT;
121 } else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100122 LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%" PRId64
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200123 ": SQL modified %d rows (expected 1)\n", subscr_id, rc);
124 ret = -EIO;
125 }
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200126 db_remove_reset(stmt);
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200127
128 /* make sure to remove authentication data for this subscriber id, for
129 * both 2G and 3G. */
130
131 aud = (struct sub_auth_data_str){
132 .type = OSMO_AUTH_TYPE_GSM,
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
139 aud = (struct sub_auth_data_str){
140 .type = OSMO_AUTH_TYPE_UMTS,
141 .algo = OSMO_AUTH_ALG_NONE,
142 };
143 rc = db_subscr_update_aud_by_id(dbc, subscr_id, &aud);
144 if (ret == -ENOENT && !rc)
145 ret = 0;
146
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200147 return ret;
148}
149
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200150/*! Set a subscriber's MSISDN in the HLR database.
151 * \param[in,out] dbc database context.
Oliver Smith2dc7d962019-01-15 14:14:51 +0100152 * \param[in] imsi ASCII string of IMSI digits
153 * \param[in] msisdn ASCII string of MSISDN digits, or NULL to remove the MSISDN.
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200154 * \returns 0 on success, -EINVAL in case of invalid MSISDN string, -EIO on
155 * database failure, -ENOENT if no such subscriber exists.
156 */
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200157int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi,
158 const char *msisdn)
159{
160 int rc;
161 int ret = 0;
162
Neels Hofmeyra820ea12018-12-02 19:46:46 +0100163 if (msisdn && !osmo_msisdn_str_valid(msisdn)) {
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200164 LOGHLR(imsi, LOGL_ERROR,
165 "Cannot update subscriber: invalid MSISDN: '%s'\n",
166 msisdn);
167 return -EINVAL;
168 }
169
Neels Hofmeyra820ea12018-12-02 19:46:46 +0100170 sqlite3_stmt *stmt = dbc->stmt[
171 msisdn ? DB_STMT_SET_MSISDN_BY_IMSI : DB_STMT_DELETE_MSISDN_BY_IMSI];
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200172
173 if (!db_bind_text(stmt, "$imsi", imsi))
174 return -EIO;
Neels Hofmeyra820ea12018-12-02 19:46:46 +0100175 if (msisdn) {
176 if (!db_bind_text(stmt, "$msisdn", msisdn))
177 return -EIO;
178 }
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200179
180 /* execute the statement */
181 rc = sqlite3_step(stmt);
182 if (rc != SQLITE_DONE) {
183 LOGHLR(imsi, LOGL_ERROR,
184 "Cannot update subscriber's MSISDN: SQL error: (%d) %s\n",
185 rc, sqlite3_errmsg(dbc->db));
186 ret = -EIO;
187 goto out;
188 }
189
190 /* verify execution result */
191 rc = sqlite3_changes(dbc->db);
192 if (!rc) {
193 LOGP(DAUC, LOGL_ERROR, "Cannot update MSISDN: no such subscriber: IMSI='%s'\n",
194 imsi);
195 ret = -ENOENT;
196 goto out;
197 } else if (rc != 1) {
198 LOGHLR(imsi, LOGL_ERROR, "Update MSISDN: SQL modified %d rows (expected 1)\n", rc);
199 ret = -EIO;
200 }
201
202out:
203 db_remove_reset(stmt);
204 return ret;
205
206}
207
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200208/*! Insert or update 2G or 3G authentication tokens in the database.
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200209 * If aud->type is OSMO_AUTH_TYPE_GSM, the auc_2g table entry for the
210 * subscriber will be added or modified; if aud->algo is OSMO_AUTH_ALG_NONE,
211 * however, the auc_2g entry for the subscriber is deleted. If aud->type is
212 * OSMO_AUTH_TYPE_UMTS, the auc_3g table is updated; again, if aud->algo is
213 * OSMO_AUTH_ALG_NONE, the auc_3g entry is deleted.
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200214 * \param[in,out] dbc database context.
215 * \param[in] subscr_id DB ID of the subscriber.
216 * \param[in] aud Pointer to new auth data (in ASCII string form).
217 * \returns 0 on success, -EINVAL for invalid aud, -ENOENT for unknown
218 * subscr_id, -EIO for database errors.
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200219 */
220int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id,
221 const struct sub_auth_data_str *aud)
222{
223 sqlite3_stmt *stmt_del;
224 sqlite3_stmt *stmt_ins;
225 sqlite3_stmt *stmt;
226 const char *label;
227 int rc;
228 int ret = 0;
229
230 switch (aud->type) {
231 case OSMO_AUTH_TYPE_GSM:
232 label = "auc_2g";
233 stmt_del = dbc->stmt[DB_STMT_AUC_2G_DELETE];
234 stmt_ins = dbc->stmt[DB_STMT_AUC_2G_INSERT];
235
236 switch (aud->algo) {
237 case OSMO_AUTH_ALG_NONE:
238 case OSMO_AUTH_ALG_COMP128v1:
239 case OSMO_AUTH_ALG_COMP128v2:
240 case OSMO_AUTH_ALG_COMP128v3:
241 case OSMO_AUTH_ALG_XOR:
242 break;
243 case OSMO_AUTH_ALG_MILENAGE:
244 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
245 " auth algo not suited for 2G: %s\n",
246 osmo_auth_alg_name(aud->algo));
247 return -EINVAL;
248 default:
249 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
250 " Unknown auth algo: %d\n", aud->algo);
251 return -EINVAL;
252 }
253
254 if (aud->algo == OSMO_AUTH_ALG_NONE)
255 break;
256 if (!osmo_is_hexstr(aud->u.gsm.ki, 32, 32, true)) {
257 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
258 " Invalid KI: '%s'\n", aud->u.gsm.ki);
259 return -EINVAL;
260 }
261 break;
262
263 case OSMO_AUTH_TYPE_UMTS:
264 label = "auc_3g";
265 stmt_del = dbc->stmt[DB_STMT_AUC_3G_DELETE];
266 stmt_ins = dbc->stmt[DB_STMT_AUC_3G_INSERT];
267 switch (aud->algo) {
268 case OSMO_AUTH_ALG_NONE:
269 case OSMO_AUTH_ALG_MILENAGE:
Harald Welte6e237d32020-12-28 01:01:31 +0100270 case OSMO_AUTH_ALG_XOR:
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200271 break;
272 case OSMO_AUTH_ALG_COMP128v1:
273 case OSMO_AUTH_ALG_COMP128v2:
274 case OSMO_AUTH_ALG_COMP128v3:
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200275 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
276 " auth algo not suited for 3G: %s\n",
277 osmo_auth_alg_name(aud->algo));
278 return -EINVAL;
279 default:
280 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
281 " Unknown auth algo: %d\n", aud->algo);
282 return -EINVAL;
283 }
284
285 if (aud->algo == OSMO_AUTH_ALG_NONE)
286 break;
287 if (!osmo_is_hexstr(aud->u.umts.k, 32, 32, true)) {
288 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
289 " Invalid K: '%s'\n", aud->u.umts.k);
290 return -EINVAL;
291 }
292 if (!osmo_is_hexstr(aud->u.umts.opc, 32, 32, true)) {
293 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
294 " Invalid OP/OPC: '%s'\n", aud->u.umts.opc);
295 return -EINVAL;
296 }
297 if (aud->u.umts.ind_bitlen > OSMO_MILENAGE_IND_BITLEN_MAX) {
298 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
299 " Invalid ind_bitlen: %d\n", aud->u.umts.ind_bitlen);
300 return -EINVAL;
301 }
302 break;
303 default:
304 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
305 " unknown auth type: %d\n", aud->type);
306 return -EINVAL;
307 }
308
309 stmt = stmt_del;
310
311 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
312 return -EIO;
313
314 /* execute the statement */
315 rc = sqlite3_step(stmt);
316 if (rc != SQLITE_DONE) {
317 LOGP(DAUC, LOGL_ERROR,
318 "Cannot delete %s row: SQL error: (%d) %s\n",
319 label, rc, sqlite3_errmsg(dbc->db));
320 ret = -EIO;
321 goto out;
322 }
323
324 /* verify execution result */
325 rc = sqlite3_changes(dbc->db);
326 if (!rc)
327 /* Leave "no such entry" logging to the caller -- during
328 * db_subscr_delete_by_id(), we call this to make sure it is
329 * empty, and no entry is not an error then.*/
330 ret = -ENOENT;
331 else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100332 LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%" PRId64
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200333 " from %s: SQL modified %d rows (expected 1)\n",
334 subscr_id, label, rc);
335 ret = -EIO;
336 }
337
338 db_remove_reset(stmt);
339
340 /* Error situation? Return now. */
341 if (ret && ret != -ENOENT)
342 return ret;
343
344 /* Just delete requested? */
345 if (aud->algo == OSMO_AUTH_ALG_NONE)
346 return ret;
347
348 /* Don't return -ENOENT if inserting new data. */
349 ret = 0;
350
351 /* Insert new row */
352 stmt = stmt_ins;
353
354 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
355 return -EIO;
356
357 switch (aud->type) {
358 case OSMO_AUTH_TYPE_GSM:
359 if (!db_bind_int(stmt, "$algo_id_2g", aud->algo))
360 return -EIO;
361 if (!db_bind_text(stmt, "$ki", aud->u.gsm.ki))
362 return -EIO;
363 break;
364 case OSMO_AUTH_TYPE_UMTS:
365 if (!db_bind_int(stmt, "$algo_id_3g", aud->algo))
366 return -EIO;
367 if (!db_bind_text(stmt, "$k", aud->u.umts.k))
368 return -EIO;
369 if (!db_bind_text(stmt, "$op",
370 aud->u.umts.opc_is_op ? aud->u.umts.opc : NULL))
371 return -EIO;
372 if (!db_bind_text(stmt, "$opc",
373 aud->u.umts.opc_is_op ? NULL : aud->u.umts.opc))
374 return -EIO;
375 if (!db_bind_int(stmt, "$ind_bitlen", aud->u.umts.ind_bitlen))
376 return -EIO;
377 break;
378 default:
379 OSMO_ASSERT(false);
380 }
381
382 /* execute the statement */
383 rc = sqlite3_step(stmt);
384 if (rc != SQLITE_DONE) {
385 LOGP(DAUC, LOGL_ERROR,
386 "Cannot insert %s row: SQL error: (%d) %s\n",
387 label, rc, sqlite3_errmsg(dbc->db));
388 ret = -EIO;
389 goto out;
390 }
391
392out:
393 db_remove_reset(stmt);
394 return ret;
395}
396
Oliver Smith81db3892019-01-09 12:03:51 +0100397/*! Set a subscriber's IMEI in the HLR database.
398 * \param[in,out] dbc database context.
399 * \param[in] imsi ASCII string of IMSI digits
400 * \param[in] imei ASCII string of identifier digits, or NULL to remove the IMEI.
401 * \returns 0 on success, -ENOENT when the given subscriber does not exist,
402 * -EIO on database errors.
403 */
404int db_subscr_update_imei_by_imsi(struct db_context *dbc, const char* imsi, const char *imei)
405{
406 int rc, ret = 0;
407 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_UPD_IMEI_BY_IMSI];
408
409 if (imei && !osmo_imei_str_valid(imei, false)) {
410 LOGP(DAUC, LOGL_ERROR, "Cannot update subscriber IMSI='%s': invalid IMEI: '%s'\n", imsi, imei);
411 return -EINVAL;
412 }
413
414 if (!db_bind_text(stmt, "$imsi", imsi))
415 return -EIO;
416 if (imei && !db_bind_text(stmt, "$imei", imei))
417 return -EIO;
418
419 /* execute the statement */
420 rc = sqlite3_step(stmt);
421 if (rc != SQLITE_DONE) {
422 LOGP(DAUC, LOGL_ERROR, "Update IMEI for subscriber IMSI='%s': SQL Error: %s\n", imsi,
423 sqlite3_errmsg(dbc->db));
424 ret = -EIO;
425 goto out;
426 }
427
428 /* verify execution result */
429 rc = sqlite3_changes(dbc->db);
430 if (!rc) {
431 LOGP(DAUC, LOGL_ERROR, "Cannot update IMEI for subscriber IMSI='%s': no such subscriber\n", imsi);
432 ret = -ENOENT;
433 } else if (rc != 1) {
434 LOGP(DAUC, LOGL_ERROR, "Update IMEI for subscriber IMSI='%s': SQL modified %d rows (expected 1)\n",
435 imsi, rc);
436 ret = -EIO;
437 }
438
439out:
440 db_remove_reset(stmt);
441 return ret;
442}
443
Neels Hofmeyr07e16022019-11-20 02:36:35 +0100444static void parse_last_lu_seen(time_t *dst, const char *last_lu_seen_str, const char *imsi, const char *label)
445{
446 struct tm tm = {0};
447 time_t val;
448 if (!last_lu_seen_str || last_lu_seen_str[0] == '\0')
449 return;
450
451 if (strptime(last_lu_seen_str, DB_LAST_LU_SEEN_FMT, &tm) == NULL) {
452 LOGP(DAUC, LOGL_ERROR, "IMSI-%s: Last LU Seen %s: Cannot parse timestamp '%s'\n",
453 imsi, label, last_lu_seen_str);
454 return;
455 }
456
457 errno = 0;
458 val = mktime(&tm);
459 if (val == -1) {
460 LOGP(DAUC, LOGL_ERROR, "IMSI-%s: Last LU Seen %s: Cannot convert timestamp '%s' to time_t: %s\n",
461 imsi, label, last_lu_seen_str, strerror(errno));
462 val = 0;
463 }
464
465 *dst = val;
466}
467
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200468/* Common code for db_subscr_get_by_*() functions. */
469static int db_sel(struct db_context *dbc, sqlite3_stmt *stmt, struct hlr_subscriber *subscr,
470 const char **err)
Harald Weltee687be52016-05-03 18:49:27 +0200471{
Maxadc66482017-02-20 11:23:20 +0100472 int rc;
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200473 int ret = 0;
Harald Weltee687be52016-05-03 18:49:27 +0200474
475 /* execute the statement */
476 rc = sqlite3_step(stmt);
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200477 if (rc == SQLITE_DONE) {
478 ret = -ENOENT;
479 goto out;
480 }
Harald Weltee687be52016-05-03 18:49:27 +0200481 if (rc != SQLITE_ROW) {
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200482 ret = -EIO;
483 goto out;
Maxadc66482017-02-20 11:23:20 +0100484 }
485
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200486 if (!subscr)
487 goto out;
Harald Weltee687be52016-05-03 18:49:27 +0200488
Neels Hofmeyrb6837e32017-10-10 23:20:26 +0200489 *subscr = (struct hlr_subscriber){};
490
Harald Weltee687be52016-05-03 18:49:27 +0200491 /* obtain the various columns */
492 subscr->id = sqlite3_column_int64(stmt, 0);
Neels Hofmeyrdbced932017-10-27 02:57:51 +0200493 copy_sqlite3_text_to_buf(subscr->imsi, stmt, 1);
494 copy_sqlite3_text_to_buf(subscr->msisdn, stmt, 2);
Oliver Smith81db3892019-01-09 12:03:51 +0100495 copy_sqlite3_text_to_buf(subscr->imei, stmt, 3);
Harald Welte99909272016-05-05 18:24:15 +0200496 /* FIXME: These should all be BLOBs as they might contain NUL */
Oliver Smith81db3892019-01-09 12:03:51 +0100497 copy_sqlite3_text_to_buf(subscr->vlr_number, stmt, 4);
498 copy_sqlite3_text_to_buf(subscr->sgsn_number, stmt, 5);
499 copy_sqlite3_text_to_buf(subscr->sgsn_address, stmt, 6);
500 subscr->periodic_lu_timer = sqlite3_column_int(stmt, 7);
501 subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 8);
502 subscr->nam_cs = sqlite3_column_int(stmt, 9);
503 subscr->nam_ps = sqlite3_column_int(stmt, 10);
504 subscr->lmsi = sqlite3_column_int(stmt, 11);
505 subscr->ms_purged_cs = sqlite3_column_int(stmt, 12);
506 subscr->ms_purged_ps = sqlite3_column_int(stmt, 13);
Neels Hofmeyr07e16022019-11-20 02:36:35 +0100507 parse_last_lu_seen(&subscr->last_lu_seen, (const char *)sqlite3_column_text(stmt, 14),
508 subscr->imsi, "CS");
509 parse_last_lu_seen(&subscr->last_lu_seen_ps, (const char *)sqlite3_column_text(stmt, 15),
510 subscr->imsi, "PS");
Neels Hofmeyr04c23752019-11-25 03:59:50 +0100511 copy_sqlite3_text_to_ipa_name(&subscr->vlr_via_proxy, stmt, 16);
512 copy_sqlite3_text_to_ipa_name(&subscr->sgsn_via_proxy, stmt, 17);
Harald Weltee687be52016-05-03 18:49:27 +0200513
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200514out:
Max00b37152017-02-20 11:09:27 +0100515 db_remove_reset(stmt);
Harald Weltee687be52016-05-03 18:49:27 +0200516
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200517 switch (ret) {
518 case 0:
519 *err = NULL;
520 break;
521 case -ENOENT:
522 *err = "No such subscriber";
523 break;
524 default:
525 *err = sqlite3_errmsg(dbc->db);
526 break;
527 }
528 return ret;
529}
530
Oliver Smith6b73fd92019-03-06 13:49:05 +0100531/*! Check if a subscriber exists in the HLR database.
532 * \param[in, out] dbc database context.
533 * \param[in] imsi ASCII string of IMSI digits.
534 * \returns 0 if it exists, -ENOENT if it does not exist, -EIO on database error.
535 */
536int db_subscr_exists_by_imsi(struct db_context *dbc, const char *imsi) {
537 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_EXISTS_BY_IMSI];
538 const char *err;
539 int rc;
540
541 if (!db_bind_text(stmt, NULL, imsi))
542 return -EIO;
543
544 rc = sqlite3_step(stmt);
545 db_remove_reset(stmt);
546 if (rc == SQLITE_ROW)
547 return 0; /* exists */
548 if (rc == SQLITE_DONE)
549 return -ENOENT; /* does not exist */
550
551 err = sqlite3_errmsg(dbc->db);
552 LOGP(DAUC, LOGL_ERROR, "Failed to check if subscriber exists by IMSI='%s': %s\n", imsi, err);
553 return rc;
554}
555
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200556/*! Retrieve subscriber data from the HLR database.
557 * \param[in,out] dbc database context.
558 * \param[in] imsi ASCII string of IMSI digits.
559 * \param[out] subscr place retrieved data in this struct.
560 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
561 * database error.
562 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200563int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
564 struct hlr_subscriber *subscr)
565{
566 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI];
567 const char *err;
568 int rc;
569
570 if (!db_bind_text(stmt, NULL, imsi))
571 return -EIO;
572
573 rc = db_sel(dbc, stmt, subscr, &err);
Neels Hofmeyr0d82a872019-11-25 05:37:53 +0100574 if (rc && rc != -ENOENT)
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200575 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMSI='%s': %s\n",
576 imsi, err);
577 return rc;
578}
579
Vadim Yanitskiyc13599d2019-03-30 17:03:42 +0700580/*! Check if a subscriber exists in the HLR database.
581 * \param[in, out] dbc database context.
582 * \param[in] msisdn ASCII string of MSISDN digits.
583 * \returns 0 if it exists, -ENOENT if it does not exist, -EIO on database error.
584 */
585int db_subscr_exists_by_msisdn(struct db_context *dbc, const char *msisdn)
586{
587 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_EXISTS_BY_MSISDN];
588 const char *err;
589 int rc;
590
591 if (!db_bind_text(stmt, NULL, msisdn))
592 return -EIO;
593
594 rc = sqlite3_step(stmt);
595 db_remove_reset(stmt);
596 if (rc == SQLITE_ROW)
597 return 0; /* exists */
598 if (rc == SQLITE_DONE)
599 return -ENOENT; /* does not exist */
600
601 err = sqlite3_errmsg(dbc->db);
602 LOGP(DAUC, LOGL_ERROR, "Failed to check if subscriber exists "
603 "by MSISDN='%s': %s\n", msisdn, err);
604 return rc;
605}
606
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200607/*! Retrieve subscriber data from the HLR database.
608 * \param[in,out] dbc database context.
609 * \param[in] msisdn ASCII string of MSISDN digits.
610 * \param[out] subscr place retrieved data in this struct.
611 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
612 * database error.
613 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200614int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
615 struct hlr_subscriber *subscr)
616{
617 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_MSISDN];
618 const char *err;
619 int rc;
620
621 if (!db_bind_text(stmt, NULL, msisdn))
622 return -EIO;
623
624 rc = db_sel(dbc, stmt, subscr, &err);
Neels Hofmeyr0d82a872019-11-25 05:37:53 +0100625 if (rc && rc != -ENOENT)
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200626 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: MSISDN='%s': %s\n",
627 msisdn, err);
628 return rc;
629}
630
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200631/*! Retrieve subscriber data from the HLR database.
632 * \param[in,out] dbc database context.
Keith89fda302021-01-19 07:01:33 +0100633 * \param[in] filter_type ASCII string of identifier type to search.
634 * \param[in] filter ASCII string to search.
635 * \param[in] get_cb pointer to call back function for data.
636 * \param[in,out] data pointer to pass to callback function.
637 * \param[in,out] count counter for number of matched subscribers.
638 * \param[in,our] err
639 * \returns 0 on success, -ENOENT if no subscriber was found, -EIO on
640 * database error.
641 */
642int db_subscrs_get(struct db_context *dbc, const char *filter_type, const char *filter,
643 void (*get_cb)(struct hlr_subscriber *subscr, void *data), void *data,
644 int *count, const char **err)
645{
646 sqlite3_stmt *stmt;
647 char search[256];
648 int rc;
649 struct hlr_subscriber subscr;
650 bool show_ls = false;
651
652 if (!filter_type) {
653 stmt = dbc->stmt[DB_STMT_SEL_ALL];
Keithca8e6ef2021-05-07 05:59:21 +0200654 } else if (strcmp(filter_type, "imei") == 0) {
655 stmt = dbc->stmt[DB_STMT_SEL_FILTER_IMEI];
Keith89fda302021-01-19 07:01:33 +0100656 } else if (strcmp(filter_type, "imsi") == 0) {
657 stmt = dbc->stmt[DB_STMT_SEL_FILTER_IMSI];
658 } else if (strcmp(filter_type, "msisdn") == 0) {
659 stmt = dbc->stmt[DB_STMT_SEL_FILTER_MSISDN];
660 } else if (strcmp(filter_type, "cs") == 0) {
661 stmt = dbc->stmt[DB_STMT_SEL_FILTER_CS];
662 } else if (strcmp(filter_type, "ps") == 0) {
663 stmt = dbc->stmt[DB_STMT_SEL_FILTER_PS];
664 } else if (strcmp(filter_type, "last_lu_seen") == 0) {
665 show_ls = true;
666 stmt = dbc->stmt[DB_STMT_SEL_ALL_ORDER_LAST_SEEN];
667 } else {
668 return -EIO;
669 }
670
Keithb5a56762021-01-30 13:49:15 -0600671 if (filter_type && filter && strcmp(filter_type, "last_lu_seen") != 0) {
Keith89fda302021-01-19 07:01:33 +0100672 if (strcmp(filter, "on") == 0) {
673 sprintf(search, "%s", "1");
674 } else if (strcmp(filter, "off") == 0) {
675 sprintf(search, "%s", "0");
676 } else {
677 sprintf(search, "%%%s%%", filter);
678 }
679 if (!db_bind_text(stmt, "$search", search)) {
680 *err = sqlite3_errmsg(dbc->db);
681 return -EIO;
682 }
683 }
684
685 rc = sqlite3_step(stmt);
686
687 if (rc == SQLITE_DONE) {
688 db_remove_reset(stmt);
689 *err = "No matching subscriber(s)";
690 return -ENOENT;
691 }
692
693 while (rc == SQLITE_ROW) {
694 subscr = (struct hlr_subscriber){
695 .id = sqlite3_column_int64(stmt, 0),};
696 copy_sqlite3_text_to_buf(subscr.imsi, stmt, 1);
697 copy_sqlite3_text_to_buf(subscr.msisdn, stmt, 2);
698 copy_sqlite3_text_to_buf(subscr.imei, stmt, 3);
699 subscr.nam_cs = sqlite3_column_int(stmt, 9);
700 subscr.nam_ps = sqlite3_column_int(stmt, 10);
701 if (show_ls)
702 parse_last_lu_seen(&subscr.last_lu_seen, (const char *)sqlite3_column_text(stmt, 14),
703 subscr.imsi, "CS");
704 get_cb(&subscr, data);
705 rc = sqlite3_step(stmt);
706 (*count)++;
707 }
708
709 db_remove_reset(stmt);
710 if (rc != SQLITE_DONE) {
711 *err = sqlite3_errmsg(dbc->db);
Keith89fda302021-01-19 07:01:33 +0100712 LOGP(DAUC, LOGL_ERROR, "Cannot read subscribers from db:: %s\n", *err);
713 return rc;
714 }
Keithb5a56762021-01-30 13:49:15 -0600715 *err = NULL;
716 return 0;
Keith89fda302021-01-19 07:01:33 +0100717}
718
719/*! Retrieve subscriber data from the HLR database.
720 * \param[in,out] dbc database context.
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200721 * \param[in] id ID of the subscriber in the HLR db.
722 * \param[out] subscr place retrieved data in this struct.
723 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
724 * database error.
725 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200726int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
727 struct hlr_subscriber *subscr)
728{
729 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_ID];
730 const char *err;
731 int rc;
732
733 if (!db_bind_int64(stmt, NULL, id))
734 return -EIO;
735
736 rc = db_sel(dbc, stmt, subscr, &err);
Neels Hofmeyr0d82a872019-11-25 05:37:53 +0100737 if (rc && rc != -ENOENT)
Stefan Sperling705b61b2018-12-07 12:44:50 +0100738 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: ID=%" PRId64 ": %s\n",
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200739 id, err);
740 return rc;
Harald Weltee687be52016-05-03 18:49:27 +0200741}
742
Oliver Smith81db3892019-01-09 12:03:51 +0100743/*! Retrieve subscriber data from the HLR database.
744 * \param[in,out] dbc database context.
745 * \param[in] imei ASCII string of identifier digits
746 * \param[out] subscr place retrieved data in this struct.
747 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
748 * database error.
749 */
750int db_subscr_get_by_imei(struct db_context *dbc, const char *imei, struct hlr_subscriber *subscr)
751{
752 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMEI];
753 const char *err;
754 int rc;
755
756 if (!db_bind_text(stmt, NULL, imei))
757 return -EIO;
758
759 rc = db_sel(dbc, stmt, subscr, &err);
Neels Hofmeyr0d82a872019-11-25 05:37:53 +0100760 if (rc && rc != -ENOENT)
Oliver Smith81db3892019-01-09 12:03:51 +0100761 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMEI=%s: %s\n", imei, err);
762 return rc;
763}
764
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200765/*! You should use hlr_subscr_nam() instead; enable or disable PS or CS for a
766 * subscriber without notifying GSUP clients.
767 * \param[in,out] dbc database context.
768 * \param[in] imsi ASCII string of IMSI digits.
769 * \param[in] nam_val True to enable CS/PS, false to disable.
770 * \param[in] is_ps when true, set nam_ps, else set nam_cs.
771 * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on
772 * database errors.
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200773 */
774int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps)
Max3ce36862017-02-20 11:18:04 +0100775{
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200776 sqlite3_stmt *stmt;
Max3ce36862017-02-20 11:18:04 +0100777 int rc;
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200778 int ret = 0;
Max3ce36862017-02-20 11:18:04 +0100779
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200780 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_NAM_PS_BY_IMSI
781 : DB_STMT_UPD_NAM_CS_BY_IMSI];
Max3ce36862017-02-20 11:18:04 +0100782
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200783 if (!db_bind_text(stmt, "$imsi", imsi))
784 return -EIO;
785 if (!db_bind_int(stmt, "$val", nam_val ? 1 : 0))
786 return -EIO;
787
788 /* execute the statement */
789 rc = sqlite3_step(stmt);
Max3ce36862017-02-20 11:18:04 +0100790 if (rc != SQLITE_DONE) {
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200791 LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL error: %s\n",
792 nam_val ? "enable" : "disable",
793 is_ps ? "PS" : "CS",
794 sqlite3_errmsg(dbc->db));
795 ret = -EIO;
796 goto out;
Max3ce36862017-02-20 11:18:04 +0100797 }
798
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200799 /* verify execution result */
800 rc = sqlite3_changes(dbc->db);
801 if (!rc) {
802 LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n",
803 nam_val ? "enable" : "disable",
804 is_ps ? "PS" : "CS",
805 imsi);
806 ret = -ENOENT;
807 goto out;
808 } else if (rc != 1) {
809 LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n",
810 nam_val ? "enable" : "disable",
811 is_ps ? "PS" : "CS",
Max3ce36862017-02-20 11:18:04 +0100812 rc);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200813 ret = -EIO;
Max3ce36862017-02-20 11:18:04 +0100814 }
815
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200816out:
Max3ce36862017-02-20 11:18:04 +0100817 db_remove_reset(stmt);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200818 return ret;
Max3ce36862017-02-20 11:18:04 +0100819}
820
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200821/*! Record a Location Updating in the database.
822 * \param[in,out] dbc database context.
823 * \param[in] subscr_id ID of the subscriber in the HLR db.
824 * \param[in] vlr_or_sgsn_number ASCII string of identifier digits.
825 * \param[in] is_ps when true, set sgsn_number, else set vlr_number.
826 * \returns 0 on success, -ENOENT when the given subscriber does not exist,
827 * -EIO on database errors.
828 */
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200829int db_subscr_lu(struct db_context *dbc, int64_t subscr_id,
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100830 const struct osmo_ipa_name *vlr_name, bool is_ps,
831 const struct osmo_ipa_name *via_proxy)
Harald Weltee687be52016-05-03 18:49:27 +0200832{
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200833 sqlite3_stmt *stmt;
Harald Weltee687be52016-05-03 18:49:27 +0200834 int rc, ret = 0;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100835 struct timespec localtime;
Harald Weltee687be52016-05-03 18:49:27 +0200836
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200837 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_SGSN_BY_ID
838 : DB_STMT_UPD_VLR_BY_ID];
Harald Weltee687be52016-05-03 18:49:27 +0200839
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200840 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
841 return -EIO;
Harald Weltee687be52016-05-03 18:49:27 +0200842
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100843 if (!db_bind_text(stmt, "$number", (char*)vlr_name->val))
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200844 return -EIO;
Harald Weltee687be52016-05-03 18:49:27 +0200845
Neels Hofmeyr04c23752019-11-25 03:59:50 +0100846 if (via_proxy && via_proxy->len) {
847 if (!db_bind_text(stmt, "$proxy", (char*)via_proxy->val))
848 return -EIO;
849 } else {
850 if (!db_bind_null(stmt, "$proxy"))
851 return -EIO;
852 }
853
Harald Weltee687be52016-05-03 18:49:27 +0200854 /* execute the statement */
855 rc = sqlite3_step(stmt);
856 if (rc != SQLITE_DONE) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100857 LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64 ": SQL Error: %s\n",
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200858 is_ps? "SGSN" : "VLR", subscr_id, sqlite3_errmsg(dbc->db));
859 ret = -EIO;
860 goto out;
Harald Weltee687be52016-05-03 18:49:27 +0200861 }
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200862
863 /* verify execution result */
864 rc = sqlite3_changes(dbc->db);
865 if (!rc) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100866 LOGP(DAUC, LOGL_ERROR, "Cannot update %s number for subscriber ID=%" PRId64
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200867 ": no such subscriber\n",
868 is_ps? "SGSN" : "VLR", subscr_id);
869 ret = -ENOENT;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100870 goto out;
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200871 } else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100872 LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200873 ": SQL modified %d rows (expected 1)\n",
874 is_ps? "SGSN" : "VLR", subscr_id, rc);
875 ret = -EIO;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100876 goto out;
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200877 }
878
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100879 db_remove_reset(stmt);
880
881 if (osmo_clock_gettime(CLOCK_REALTIME, &localtime) != 0) {
882 LOGP(DAUC, LOGL_ERROR, "Cannot get the current time: (%d) %s\n", errno, strerror(errno));
883 ret = -errno;
884 goto out;
885 }
886
Neels Hofmeyr07e16022019-11-20 02:36:35 +0100887 stmt = dbc->stmt[is_ps? DB_STMT_SET_LAST_LU_SEEN_PS : DB_STMT_SET_LAST_LU_SEEN];
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100888
889 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
890 return -EIO;
891 /* The timestamp will be converted to UTC by SQLite. */
892 if (!db_bind_int64(stmt, "$val", (int64_t)localtime.tv_sec)) {
893 ret = -EIO;
894 goto out;
895 }
896
897 rc = sqlite3_step(stmt);
898 if (rc != SQLITE_DONE) {
899 LOGP(DAUC, LOGL_ERROR,
Stefan Sperling705b61b2018-12-07 12:44:50 +0100900 "Cannot update LU timestamp for subscriber ID=%" PRId64 ": SQL error: (%d) %s\n",
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100901 subscr_id, rc, sqlite3_errmsg(dbc->db));
902 ret = -EIO;
903 goto out;
904 }
905
906 /* verify execution result */
907 rc = sqlite3_changes(dbc->db);
908 if (!rc) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100909 LOGP(DAUC, LOGL_ERROR, "Cannot update LU timestamp for subscriber ID=%" PRId64
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100910 ": no such subscriber\n", subscr_id);
911 ret = -ENOENT;
912 goto out;
913 } else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100914 LOGP(DAUC, LOGL_ERROR, "Update LU timestamp for subscriber ID=%" PRId64
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100915 ": SQL modified %d rows (expected 1)\n", subscr_id, rc);
916 ret = -EIO;
917 }
Harald Weltee687be52016-05-03 18:49:27 +0200918out:
Max00b37152017-02-20 11:09:27 +0100919 db_remove_reset(stmt);
Harald Weltee687be52016-05-03 18:49:27 +0200920 return ret;
921}
Harald Welteb18f0e02016-05-05 21:03:03 +0200922
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200923/*! Set the ms_purged_cs or ms_purged_ps values in the database.
924 * \param[in,out] dbc database context.
925 * \param[in] by_imsi ASCII string of IMSI digits.
926 * \param[in] purge_val true to purge, false to un-purge.
927 * \param[in] is_ps when true, set ms_purged_ps, else set ms_purged_cs.
928 * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on
929 * database errors.
930 */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200931int db_subscr_purge(struct db_context *dbc, const char *by_imsi,
932 bool purge_val, bool is_ps)
Harald Welteb18f0e02016-05-05 21:03:03 +0200933{
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200934 sqlite3_stmt *stmt;
935 int rc, ret = 0;
Harald Welteb18f0e02016-05-05 21:03:03 +0200936
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200937 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_PURGE_PS_BY_IMSI
938 : DB_STMT_UPD_PURGE_CS_BY_IMSI];
Harald Welteb18f0e02016-05-05 21:03:03 +0200939
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200940 if (!db_bind_text(stmt, "$imsi", by_imsi))
941 return -EIO;
942 if (!db_bind_int(stmt, "$val", purge_val ? 1 : 0))
943 return -EIO;
Harald Welteb18f0e02016-05-05 21:03:03 +0200944
945 /* execute the statement */
946 rc = sqlite3_step(stmt);
947 if (rc != SQLITE_DONE) {
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200948 LOGP(DAUC, LOGL_ERROR, "%s %s: SQL error: %s\n",
949 purge_val ? "purge" : "un-purge",
950 is_ps ? "PS" : "CS",
951 sqlite3_errmsg(dbc->db));
952 ret = -EIO;
953 goto out;
Harald Welteb18f0e02016-05-05 21:03:03 +0200954 }
Max00b37152017-02-20 11:09:27 +0100955
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200956 /* verify execution result */
957 rc = sqlite3_changes(dbc->db);
958 if (!rc) {
959 LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n",
960 purge_val ? "purge" : "un-purge",
961 is_ps ? "PS" : "CS",
962 by_imsi);
963 ret = -ENOENT;
964 goto out;
965 } else if (rc != 1) {
966 LOGHLR(by_imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n",
967 purge_val ? "purge" : "un-purge",
968 is_ps ? "PS" : "CS",
969 rc);
970 ret = -EIO;
971 }
972
973out:
Max00b37152017-02-20 11:09:27 +0100974 db_remove_reset(stmt);
Harald Welteb18f0e02016-05-05 21:03:03 +0200975
976 return ret;
977}
Neels Hofmeyr3f9d1972019-12-12 04:04:53 +0100978
979static int _db_ind_run(struct db_context *dbc, sqlite3_stmt *stmt, const char *vlr, bool reset)
980{
981 int rc;
982
983 if (!db_bind_text(stmt, "$vlr", vlr))
984 return -EIO;
985
986 /* execute the statement */
987 rc = sqlite3_step(stmt);
988 if (reset)
989 db_remove_reset(stmt);
990 return rc;
991}
992
993static int _db_ind_add(struct db_context *dbc, const char *vlr)
994{
995 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_IND_ADD];
996 if (_db_ind_run(dbc, stmt, vlr, true) != SQLITE_DONE) {
997 LOGP(DDB, LOGL_ERROR, "Cannot create IND entry for %s\n", osmo_quote_str_c(OTC_SELECT, vlr, -1));
998 return -EIO;
999 }
1000 return 0;
1001}
1002
1003static int _db_ind_del(struct db_context *dbc, const char *vlr)
1004{
1005 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_IND_DEL];
1006 _db_ind_run(dbc, stmt, vlr, true);
1007 /* We don't really care about the result. If it didn't exist, then that was the goal anyway. */
1008 return 0;
1009}
1010
1011static int _db_ind_get(struct db_context *dbc, const char *vlr, unsigned int *ind)
1012{
1013 int ret = 0;
1014 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_IND_SELECT];
1015 int rc = _db_ind_run(dbc, stmt, vlr, false);
1016 if (rc == SQLITE_DONE) {
1017 /* Does not exist yet */
1018 ret = -ENOENT;
1019 goto out;
1020 } else if (rc != SQLITE_ROW) {
1021 LOGP(DDB, LOGL_ERROR, "Error executing SQL: %d\n", rc);
1022 ret = -EIO;
1023 goto out;
1024 }
1025
1026 OSMO_ASSERT(ind);
1027 *ind = sqlite3_column_int64(stmt, 0);
1028out:
1029 db_remove_reset(stmt);
1030 return ret;
1031}
1032
1033int _db_ind(struct db_context *dbc, const struct osmo_cni_peer_id *vlr,
1034 unsigned int *ind, bool del)
1035{
1036 const char *vlr_name = NULL;
1037 int rc;
1038
1039 switch (vlr->type) {
1040 case OSMO_CNI_PEER_ID_IPA_NAME:
1041 if (vlr->ipa_name.len < 2 || vlr->ipa_name.val[vlr->ipa_name.len - 1] != '\0') {
1042 LOGP(DDB, LOGL_ERROR, "Expecting VLR ipa_name to be zero terminated; found %s\n",
1043 osmo_ipa_name_to_str(&vlr->ipa_name));
1044 return -ENOTSUP;
1045 }
1046 vlr_name = (const char*)vlr->ipa_name.val;
1047 break;
1048 default:
1049 LOGP(DDB, LOGL_ERROR, "Unsupported osmo_cni_peer_id type: %s\n",
1050 osmo_cni_peer_id_type_name(vlr->type));
1051 return -ENOTSUP;
1052 }
1053
1054 if (del)
1055 return _db_ind_del(dbc, vlr_name);
1056
1057 rc = _db_ind_get(dbc, vlr_name, ind);
1058 if (!rc)
1059 return 0;
1060
1061 /* Does not exist yet, create. */
1062 rc = _db_ind_add(dbc, vlr_name);
1063 if (rc) {
1064 LOGP(DDB, LOGL_ERROR, "Error creating IND entry for %s\n", osmo_quote_str_c(OTC_SELECT, vlr_name, -1));
1065 return rc;
1066 }
1067
1068 /* To be sure, query again from scratch. */
1069 return _db_ind_get(dbc, vlr_name, ind);
1070}
1071
1072int db_ind(struct db_context *dbc, const struct osmo_cni_peer_id *vlr, unsigned int *ind)
1073{
1074 return _db_ind(dbc, vlr, ind, false);
1075}
1076
1077int db_ind_del(struct db_context *dbc, const struct osmo_cni_peer_id *vlr)
1078{
1079 return _db_ind(dbc, vlr, NULL, true);
1080}