blob: c97cd8253e4dee1aea01be4679cba67dd3a71687 [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 Hofmeyrb6837e32017-10-10 23:20:26 +0200412 *subscr = (struct hlr_subscriber){};
413
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 Hofmeyr9c2bbc82017-10-09 17:30:32 +0200447 switch (ret) {
448 case 0:
449 *err = NULL;
450 break;
451 case -ENOENT:
452 *err = "No such subscriber";
453 break;
454 default:
455 *err = sqlite3_errmsg(dbc->db);
456 break;
457 }
458 return ret;
459}
460
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200461/*! Retrieve subscriber data from the HLR database.
462 * \param[in,out] dbc database context.
463 * \param[in] imsi ASCII string of IMSI digits.
464 * \param[out] subscr place retrieved data in this struct.
465 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
466 * database error.
467 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200468int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
469 struct hlr_subscriber *subscr)
470{
471 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI];
472 const char *err;
473 int rc;
474
475 if (!db_bind_text(stmt, NULL, imsi))
476 return -EIO;
477
478 rc = db_sel(dbc, stmt, subscr, &err);
479 if (rc)
480 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMSI='%s': %s\n",
481 imsi, err);
482 return rc;
483}
484
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200485/*! Retrieve subscriber data from the HLR database.
486 * \param[in,out] dbc database context.
487 * \param[in] msisdn ASCII string of MSISDN digits.
488 * \param[out] subscr place retrieved data in this struct.
489 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
490 * database error.
491 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200492int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
493 struct hlr_subscriber *subscr)
494{
495 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_MSISDN];
496 const char *err;
497 int rc;
498
499 if (!db_bind_text(stmt, NULL, msisdn))
500 return -EIO;
501
502 rc = db_sel(dbc, stmt, subscr, &err);
503 if (rc)
504 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: MSISDN='%s': %s\n",
505 msisdn, err);
506 return rc;
507}
508
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200509/*! Retrieve subscriber data from the HLR database.
510 * \param[in,out] dbc database context.
511 * \param[in] id ID of the subscriber in the HLR db.
512 * \param[out] subscr place retrieved data in this struct.
513 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
514 * database error.
515 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200516int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
517 struct hlr_subscriber *subscr)
518{
519 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_ID];
520 const char *err;
521 int rc;
522
523 if (!db_bind_int64(stmt, NULL, id))
524 return -EIO;
525
526 rc = db_sel(dbc, stmt, subscr, &err);
527 if (rc)
Stefan Sperling705b61b2018-12-07 12:44:50 +0100528 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: ID=%" PRId64 ": %s\n",
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200529 id, err);
530 return rc;
Harald Weltee687be52016-05-03 18:49:27 +0200531}
532
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200533/*! You should use hlr_subscr_nam() instead; enable or disable PS or CS for a
534 * subscriber without notifying GSUP clients.
535 * \param[in,out] dbc database context.
536 * \param[in] imsi ASCII string of IMSI digits.
537 * \param[in] nam_val True to enable CS/PS, false to disable.
538 * \param[in] is_ps when true, set nam_ps, else set nam_cs.
539 * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on
540 * database errors.
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200541 */
542int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps)
Max3ce36862017-02-20 11:18:04 +0100543{
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200544 sqlite3_stmt *stmt;
Max3ce36862017-02-20 11:18:04 +0100545 int rc;
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200546 int ret = 0;
Max3ce36862017-02-20 11:18:04 +0100547
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200548 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_NAM_PS_BY_IMSI
549 : DB_STMT_UPD_NAM_CS_BY_IMSI];
Max3ce36862017-02-20 11:18:04 +0100550
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200551 if (!db_bind_text(stmt, "$imsi", imsi))
552 return -EIO;
553 if (!db_bind_int(stmt, "$val", nam_val ? 1 : 0))
554 return -EIO;
555
556 /* execute the statement */
557 rc = sqlite3_step(stmt);
Max3ce36862017-02-20 11:18:04 +0100558 if (rc != SQLITE_DONE) {
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200559 LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL error: %s\n",
560 nam_val ? "enable" : "disable",
561 is_ps ? "PS" : "CS",
562 sqlite3_errmsg(dbc->db));
563 ret = -EIO;
564 goto out;
Max3ce36862017-02-20 11:18:04 +0100565 }
566
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200567 /* verify execution result */
568 rc = sqlite3_changes(dbc->db);
569 if (!rc) {
570 LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n",
571 nam_val ? "enable" : "disable",
572 is_ps ? "PS" : "CS",
573 imsi);
574 ret = -ENOENT;
575 goto out;
576 } else if (rc != 1) {
577 LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n",
578 nam_val ? "enable" : "disable",
579 is_ps ? "PS" : "CS",
Max3ce36862017-02-20 11:18:04 +0100580 rc);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200581 ret = -EIO;
Max3ce36862017-02-20 11:18:04 +0100582 }
583
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200584out:
Max3ce36862017-02-20 11:18:04 +0100585 db_remove_reset(stmt);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200586 return ret;
Max3ce36862017-02-20 11:18:04 +0100587}
588
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200589/*! Record a Location Updating in the database.
590 * \param[in,out] dbc database context.
591 * \param[in] subscr_id ID of the subscriber in the HLR db.
592 * \param[in] vlr_or_sgsn_number ASCII string of identifier digits.
593 * \param[in] is_ps when true, set sgsn_number, else set vlr_number.
594 * \returns 0 on success, -ENOENT when the given subscriber does not exist,
595 * -EIO on database errors.
596 */
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200597int db_subscr_lu(struct db_context *dbc, int64_t subscr_id,
598 const char *vlr_or_sgsn_number, bool is_ps)
Harald Weltee687be52016-05-03 18:49:27 +0200599{
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200600 sqlite3_stmt *stmt;
Harald Weltee687be52016-05-03 18:49:27 +0200601 int rc, ret = 0;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100602 struct timespec localtime;
Harald Weltee687be52016-05-03 18:49:27 +0200603
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200604 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_SGSN_BY_ID
605 : DB_STMT_UPD_VLR_BY_ID];
Harald Weltee687be52016-05-03 18:49:27 +0200606
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200607 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
608 return -EIO;
Harald Weltee687be52016-05-03 18:49:27 +0200609
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200610 if (!db_bind_text(stmt, "$number", vlr_or_sgsn_number))
611 return -EIO;
Harald Weltee687be52016-05-03 18:49:27 +0200612
613 /* execute the statement */
614 rc = sqlite3_step(stmt);
615 if (rc != SQLITE_DONE) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100616 LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64 ": SQL Error: %s\n",
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200617 is_ps? "SGSN" : "VLR", subscr_id, sqlite3_errmsg(dbc->db));
618 ret = -EIO;
619 goto out;
Harald Weltee687be52016-05-03 18:49:27 +0200620 }
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200621
622 /* verify execution result */
623 rc = sqlite3_changes(dbc->db);
624 if (!rc) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100625 LOGP(DAUC, LOGL_ERROR, "Cannot update %s number for subscriber ID=%" PRId64
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200626 ": no such subscriber\n",
627 is_ps? "SGSN" : "VLR", subscr_id);
628 ret = -ENOENT;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100629 goto out;
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200630 } else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100631 LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200632 ": SQL modified %d rows (expected 1)\n",
633 is_ps? "SGSN" : "VLR", subscr_id, rc);
634 ret = -EIO;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100635 goto out;
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200636 }
637
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100638 db_remove_reset(stmt);
639
640 if (osmo_clock_gettime(CLOCK_REALTIME, &localtime) != 0) {
641 LOGP(DAUC, LOGL_ERROR, "Cannot get the current time: (%d) %s\n", errno, strerror(errno));
642 ret = -errno;
643 goto out;
644 }
645
646 stmt = dbc->stmt[DB_STMT_SET_LAST_LU_SEEN];
647
648 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
649 return -EIO;
650 /* The timestamp will be converted to UTC by SQLite. */
651 if (!db_bind_int64(stmt, "$val", (int64_t)localtime.tv_sec)) {
652 ret = -EIO;
653 goto out;
654 }
655
656 rc = sqlite3_step(stmt);
657 if (rc != SQLITE_DONE) {
658 LOGP(DAUC, LOGL_ERROR,
Stefan Sperling705b61b2018-12-07 12:44:50 +0100659 "Cannot update LU timestamp for subscriber ID=%" PRId64 ": SQL error: (%d) %s\n",
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100660 subscr_id, rc, sqlite3_errmsg(dbc->db));
661 ret = -EIO;
662 goto out;
663 }
664
665 /* verify execution result */
666 rc = sqlite3_changes(dbc->db);
667 if (!rc) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100668 LOGP(DAUC, LOGL_ERROR, "Cannot update LU timestamp for subscriber ID=%" PRId64
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100669 ": no such subscriber\n", subscr_id);
670 ret = -ENOENT;
671 goto out;
672 } else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100673 LOGP(DAUC, LOGL_ERROR, "Update LU timestamp for subscriber ID=%" PRId64
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100674 ": SQL modified %d rows (expected 1)\n", subscr_id, rc);
675 ret = -EIO;
676 }
Harald Weltee687be52016-05-03 18:49:27 +0200677out:
Max00b37152017-02-20 11:09:27 +0100678 db_remove_reset(stmt);
Harald Weltee687be52016-05-03 18:49:27 +0200679 return ret;
680}
Harald Welteb18f0e02016-05-05 21:03:03 +0200681
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200682/*! Set the ms_purged_cs or ms_purged_ps values in the database.
683 * \param[in,out] dbc database context.
684 * \param[in] by_imsi ASCII string of IMSI digits.
685 * \param[in] purge_val true to purge, false to un-purge.
686 * \param[in] is_ps when true, set ms_purged_ps, else set ms_purged_cs.
687 * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on
688 * database errors.
689 */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200690int db_subscr_purge(struct db_context *dbc, const char *by_imsi,
691 bool purge_val, bool is_ps)
Harald Welteb18f0e02016-05-05 21:03:03 +0200692{
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200693 sqlite3_stmt *stmt;
694 int rc, ret = 0;
Harald Welteb18f0e02016-05-05 21:03:03 +0200695
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200696 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_PURGE_PS_BY_IMSI
697 : DB_STMT_UPD_PURGE_CS_BY_IMSI];
Harald Welteb18f0e02016-05-05 21:03:03 +0200698
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200699 if (!db_bind_text(stmt, "$imsi", by_imsi))
700 return -EIO;
701 if (!db_bind_int(stmt, "$val", purge_val ? 1 : 0))
702 return -EIO;
Harald Welteb18f0e02016-05-05 21:03:03 +0200703
704 /* execute the statement */
705 rc = sqlite3_step(stmt);
706 if (rc != SQLITE_DONE) {
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200707 LOGP(DAUC, LOGL_ERROR, "%s %s: SQL error: %s\n",
708 purge_val ? "purge" : "un-purge",
709 is_ps ? "PS" : "CS",
710 sqlite3_errmsg(dbc->db));
711 ret = -EIO;
712 goto out;
Harald Welteb18f0e02016-05-05 21:03:03 +0200713 }
Max00b37152017-02-20 11:09:27 +0100714
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200715 /* verify execution result */
716 rc = sqlite3_changes(dbc->db);
717 if (!rc) {
718 LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n",
719 purge_val ? "purge" : "un-purge",
720 is_ps ? "PS" : "CS",
721 by_imsi);
722 ret = -ENOENT;
723 goto out;
724 } else if (rc != 1) {
725 LOGHLR(by_imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n",
726 purge_val ? "purge" : "un-purge",
727 is_ps ? "PS" : "CS",
728 rc);
729 ret = -EIO;
730 }
731
732out:
Max00b37152017-02-20 11:09:27 +0100733 db_remove_reset(stmt);
Harald Welteb18f0e02016-05-05 21:03:03 +0200734
735 return ret;
736}
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200737
738/*! Update nam_cs/nam_ps in the db and trigger notifications to GSUP clients.
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200739 * \param[in,out] hlr Global hlr context.
740 * \param[in] subscr Subscriber from a fresh db_subscr_get_by_*() call.
741 * \param[in] nam_val True to enable CS/PS, false to disable.
742 * \param[in] is_ps True to enable/disable PS, false for CS.
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200743 * \returns 0 on success, ENOEXEC if there is no need to change, a negative
744 * value on error.
745 */
746int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps)
747{
748 int rc;
749 struct lu_operation *luop;
750 struct osmo_gsup_conn *co;
751 bool is_val = is_ps? subscr->nam_ps : subscr->nam_cs;
752
753 if (is_val == nam_val) {
754 LOGHLR(subscr->imsi, LOGL_DEBUG, "Already has the requested value when asked to %s %s\n",
755 nam_val ? "enable" : "disable", is_ps ? "PS" : "CS");
756 return ENOEXEC;
757 }
758
759 rc = db_subscr_nam(hlr->dbc, subscr->imsi, nam_val, is_ps);
760 if (rc)
761 return rc > 0? -rc : rc;
762
763 /* If we're disabling, send a notice out to the GSUP client that is
764 * responsible. Otherwise no need. */
765 if (nam_val)
766 return 0;
767
768 /* FIXME: only send to single SGSN where latest update for IMSI came from */
769 llist_for_each_entry(co, &hlr->gs->clients, list) {
770 luop = lu_op_alloc_conn(co);
771 if (!luop) {
772 LOGHLR(subscr->imsi, LOGL_ERROR,
773 "Cannot notify GSUP client, cannot allocate lu_operation,"
774 " for %s:%u\n",
775 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
776 co && co->conn && co->conn->server? co->conn->server->port : 0);
777 continue;
778 }
779 luop->subscr = *subscr;
780 lu_op_tx_del_subscr_data(luop);
781 lu_op_free(luop);
782 }
783 return 0;
784}