blob: 3ba457c1bd9948cd84d4ba6ac879c16b3e92923b [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.
Oliver Smith2dc7d962019-01-15 14:14:51 +0100144 * \param[in] imsi ASCII string of IMSI digits
145 * \param[in] msisdn ASCII string of MSISDN digits, or NULL to remove the MSISDN.
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200146 * \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
Oliver Smith81db3892019-01-09 12:03:51 +0100389/*! Set a subscriber's IMEI in the HLR database.
390 * \param[in,out] dbc database context.
391 * \param[in] imsi ASCII string of IMSI digits
392 * \param[in] imei ASCII string of identifier digits, or NULL to remove the IMEI.
393 * \returns 0 on success, -ENOENT when the given subscriber does not exist,
394 * -EIO on database errors.
395 */
396int db_subscr_update_imei_by_imsi(struct db_context *dbc, const char* imsi, const char *imei)
397{
398 int rc, ret = 0;
399 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_UPD_IMEI_BY_IMSI];
400
401 if (imei && !osmo_imei_str_valid(imei, false)) {
402 LOGP(DAUC, LOGL_ERROR, "Cannot update subscriber IMSI='%s': invalid IMEI: '%s'\n", imsi, imei);
403 return -EINVAL;
404 }
405
406 if (!db_bind_text(stmt, "$imsi", imsi))
407 return -EIO;
408 if (imei && !db_bind_text(stmt, "$imei", imei))
409 return -EIO;
410
411 /* execute the statement */
412 rc = sqlite3_step(stmt);
413 if (rc != SQLITE_DONE) {
414 LOGP(DAUC, LOGL_ERROR, "Update IMEI for subscriber IMSI='%s': SQL Error: %s\n", imsi,
415 sqlite3_errmsg(dbc->db));
416 ret = -EIO;
417 goto out;
418 }
419
420 /* verify execution result */
421 rc = sqlite3_changes(dbc->db);
422 if (!rc) {
423 LOGP(DAUC, LOGL_ERROR, "Cannot update IMEI for subscriber IMSI='%s': no such subscriber\n", imsi);
424 ret = -ENOENT;
425 } else if (rc != 1) {
426 LOGP(DAUC, LOGL_ERROR, "Update IMEI for subscriber IMSI='%s': SQL modified %d rows (expected 1)\n",
427 imsi, rc);
428 ret = -EIO;
429 }
430
431out:
432 db_remove_reset(stmt);
433 return ret;
434}
435
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200436/* Common code for db_subscr_get_by_*() functions. */
437static int db_sel(struct db_context *dbc, sqlite3_stmt *stmt, struct hlr_subscriber *subscr,
438 const char **err)
Harald Weltee687be52016-05-03 18:49:27 +0200439{
Maxadc66482017-02-20 11:23:20 +0100440 int rc;
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200441 int ret = 0;
Stefan Sperling5c14c9c2018-12-07 12:30:21 +0100442 const char *last_lu_seen_str;
443 struct tm tm;
Harald Weltee687be52016-05-03 18:49:27 +0200444
445 /* execute the statement */
446 rc = sqlite3_step(stmt);
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200447 if (rc == SQLITE_DONE) {
448 ret = -ENOENT;
449 goto out;
450 }
Harald Weltee687be52016-05-03 18:49:27 +0200451 if (rc != SQLITE_ROW) {
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200452 ret = -EIO;
453 goto out;
Maxadc66482017-02-20 11:23:20 +0100454 }
455
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200456 if (!subscr)
457 goto out;
Harald Weltee687be52016-05-03 18:49:27 +0200458
Neels Hofmeyrb6837e32017-10-10 23:20:26 +0200459 *subscr = (struct hlr_subscriber){};
460
Harald Weltee687be52016-05-03 18:49:27 +0200461 /* obtain the various columns */
462 subscr->id = sqlite3_column_int64(stmt, 0);
Neels Hofmeyrdbced932017-10-27 02:57:51 +0200463 copy_sqlite3_text_to_buf(subscr->imsi, stmt, 1);
464 copy_sqlite3_text_to_buf(subscr->msisdn, stmt, 2);
Oliver Smith81db3892019-01-09 12:03:51 +0100465 copy_sqlite3_text_to_buf(subscr->imei, stmt, 3);
Harald Welte99909272016-05-05 18:24:15 +0200466 /* FIXME: These should all be BLOBs as they might contain NUL */
Oliver Smith81db3892019-01-09 12:03:51 +0100467 copy_sqlite3_text_to_buf(subscr->vlr_number, stmt, 4);
468 copy_sqlite3_text_to_buf(subscr->sgsn_number, stmt, 5);
469 copy_sqlite3_text_to_buf(subscr->sgsn_address, stmt, 6);
470 subscr->periodic_lu_timer = sqlite3_column_int(stmt, 7);
471 subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 8);
472 subscr->nam_cs = sqlite3_column_int(stmt, 9);
473 subscr->nam_ps = sqlite3_column_int(stmt, 10);
474 subscr->lmsi = sqlite3_column_int(stmt, 11);
475 subscr->ms_purged_cs = sqlite3_column_int(stmt, 12);
476 subscr->ms_purged_ps = sqlite3_column_int(stmt, 13);
477 last_lu_seen_str = (const char *)sqlite3_column_text(stmt, 14);
Stefan Sperling5c14c9c2018-12-07 12:30:21 +0100478 if (last_lu_seen_str && last_lu_seen_str[0] != '\0') {
479 if (strptime(last_lu_seen_str, DB_LAST_LU_SEEN_FMT, &tm) == NULL) {
480 LOGP(DAUC, LOGL_ERROR, "Cannot parse last LU timestamp '%s' of subscriber with IMSI='%s': %s\n",
481 last_lu_seen_str, subscr->imsi, strerror(errno));
482 } else {
483 subscr->last_lu_seen = mktime(&tm);
484 if (subscr->last_lu_seen == -1) {
485 LOGP(DAUC, LOGL_ERROR, "Cannot convert LU timestamp '%s' to time_t: %s\n",
486 last_lu_seen_str, strerror(errno));
487 subscr->last_lu_seen = 0;
488 }
489 }
490 }
Harald Weltee687be52016-05-03 18:49:27 +0200491
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200492out:
Max00b37152017-02-20 11:09:27 +0100493 db_remove_reset(stmt);
Harald Weltee687be52016-05-03 18:49:27 +0200494
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200495 switch (ret) {
496 case 0:
497 *err = NULL;
498 break;
499 case -ENOENT:
500 *err = "No such subscriber";
501 break;
502 default:
503 *err = sqlite3_errmsg(dbc->db);
504 break;
505 }
506 return ret;
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] imsi ASCII string of IMSI digits.
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_imsi(struct db_context *dbc, const char *imsi,
517 struct hlr_subscriber *subscr)
518{
519 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI];
520 const char *err;
521 int rc;
522
523 if (!db_bind_text(stmt, NULL, imsi))
524 return -EIO;
525
526 rc = db_sel(dbc, stmt, subscr, &err);
527 if (rc)
528 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMSI='%s': %s\n",
529 imsi, err);
530 return rc;
531}
532
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200533/*! Retrieve subscriber data from the HLR database.
534 * \param[in,out] dbc database context.
535 * \param[in] msisdn ASCII string of MSISDN digits.
536 * \param[out] subscr place retrieved data in this struct.
537 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
538 * database error.
539 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200540int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
541 struct hlr_subscriber *subscr)
542{
543 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_MSISDN];
544 const char *err;
545 int rc;
546
547 if (!db_bind_text(stmt, NULL, msisdn))
548 return -EIO;
549
550 rc = db_sel(dbc, stmt, subscr, &err);
551 if (rc)
552 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: MSISDN='%s': %s\n",
553 msisdn, err);
554 return rc;
555}
556
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200557/*! Retrieve subscriber data from the HLR database.
558 * \param[in,out] dbc database context.
559 * \param[in] id ID of the subscriber in the HLR db.
560 * \param[out] subscr place retrieved data in this struct.
561 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
562 * database error.
563 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200564int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
565 struct hlr_subscriber *subscr)
566{
567 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_ID];
568 const char *err;
569 int rc;
570
571 if (!db_bind_int64(stmt, NULL, id))
572 return -EIO;
573
574 rc = db_sel(dbc, stmt, subscr, &err);
575 if (rc)
Stefan Sperling705b61b2018-12-07 12:44:50 +0100576 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: ID=%" PRId64 ": %s\n",
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200577 id, err);
578 return rc;
Harald Weltee687be52016-05-03 18:49:27 +0200579}
580
Oliver Smith81db3892019-01-09 12:03:51 +0100581/*! Retrieve subscriber data from the HLR database.
582 * \param[in,out] dbc database context.
583 * \param[in] imei ASCII string of identifier digits
584 * \param[out] subscr place retrieved data in this struct.
585 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
586 * database error.
587 */
588int db_subscr_get_by_imei(struct db_context *dbc, const char *imei, struct hlr_subscriber *subscr)
589{
590 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMEI];
591 const char *err;
592 int rc;
593
594 if (!db_bind_text(stmt, NULL, imei))
595 return -EIO;
596
597 rc = db_sel(dbc, stmt, subscr, &err);
598 if (rc)
599 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMEI=%s: %s\n", imei, err);
600 return rc;
601}
602
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200603/*! You should use hlr_subscr_nam() instead; enable or disable PS or CS for a
604 * subscriber without notifying GSUP clients.
605 * \param[in,out] dbc database context.
606 * \param[in] imsi ASCII string of IMSI digits.
607 * \param[in] nam_val True to enable CS/PS, false to disable.
608 * \param[in] is_ps when true, set nam_ps, else set nam_cs.
609 * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on
610 * database errors.
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200611 */
612int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps)
Max3ce36862017-02-20 11:18:04 +0100613{
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200614 sqlite3_stmt *stmt;
Max3ce36862017-02-20 11:18:04 +0100615 int rc;
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200616 int ret = 0;
Max3ce36862017-02-20 11:18:04 +0100617
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200618 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_NAM_PS_BY_IMSI
619 : DB_STMT_UPD_NAM_CS_BY_IMSI];
Max3ce36862017-02-20 11:18:04 +0100620
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200621 if (!db_bind_text(stmt, "$imsi", imsi))
622 return -EIO;
623 if (!db_bind_int(stmt, "$val", nam_val ? 1 : 0))
624 return -EIO;
625
626 /* execute the statement */
627 rc = sqlite3_step(stmt);
Max3ce36862017-02-20 11:18:04 +0100628 if (rc != SQLITE_DONE) {
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200629 LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL error: %s\n",
630 nam_val ? "enable" : "disable",
631 is_ps ? "PS" : "CS",
632 sqlite3_errmsg(dbc->db));
633 ret = -EIO;
634 goto out;
Max3ce36862017-02-20 11:18:04 +0100635 }
636
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200637 /* verify execution result */
638 rc = sqlite3_changes(dbc->db);
639 if (!rc) {
640 LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n",
641 nam_val ? "enable" : "disable",
642 is_ps ? "PS" : "CS",
643 imsi);
644 ret = -ENOENT;
645 goto out;
646 } else if (rc != 1) {
647 LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n",
648 nam_val ? "enable" : "disable",
649 is_ps ? "PS" : "CS",
Max3ce36862017-02-20 11:18:04 +0100650 rc);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200651 ret = -EIO;
Max3ce36862017-02-20 11:18:04 +0100652 }
653
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200654out:
Max3ce36862017-02-20 11:18:04 +0100655 db_remove_reset(stmt);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200656 return ret;
Max3ce36862017-02-20 11:18:04 +0100657}
658
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200659/*! Record a Location Updating in the database.
660 * \param[in,out] dbc database context.
661 * \param[in] subscr_id ID of the subscriber in the HLR db.
662 * \param[in] vlr_or_sgsn_number ASCII string of identifier digits.
663 * \param[in] is_ps when true, set sgsn_number, else set vlr_number.
664 * \returns 0 on success, -ENOENT when the given subscriber does not exist,
665 * -EIO on database errors.
666 */
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200667int db_subscr_lu(struct db_context *dbc, int64_t subscr_id,
668 const char *vlr_or_sgsn_number, bool is_ps)
Harald Weltee687be52016-05-03 18:49:27 +0200669{
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200670 sqlite3_stmt *stmt;
Harald Weltee687be52016-05-03 18:49:27 +0200671 int rc, ret = 0;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100672 struct timespec localtime;
Harald Weltee687be52016-05-03 18:49:27 +0200673
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200674 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_SGSN_BY_ID
675 : DB_STMT_UPD_VLR_BY_ID];
Harald Weltee687be52016-05-03 18:49:27 +0200676
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200677 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
678 return -EIO;
Harald Weltee687be52016-05-03 18:49:27 +0200679
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200680 if (!db_bind_text(stmt, "$number", vlr_or_sgsn_number))
681 return -EIO;
Harald Weltee687be52016-05-03 18:49:27 +0200682
683 /* execute the statement */
684 rc = sqlite3_step(stmt);
685 if (rc != SQLITE_DONE) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100686 LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64 ": SQL Error: %s\n",
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200687 is_ps? "SGSN" : "VLR", subscr_id, sqlite3_errmsg(dbc->db));
688 ret = -EIO;
689 goto out;
Harald Weltee687be52016-05-03 18:49:27 +0200690 }
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200691
692 /* verify execution result */
693 rc = sqlite3_changes(dbc->db);
694 if (!rc) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100695 LOGP(DAUC, LOGL_ERROR, "Cannot update %s number for subscriber ID=%" PRId64
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200696 ": no such subscriber\n",
697 is_ps? "SGSN" : "VLR", subscr_id);
698 ret = -ENOENT;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100699 goto out;
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200700 } else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100701 LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200702 ": SQL modified %d rows (expected 1)\n",
703 is_ps? "SGSN" : "VLR", subscr_id, rc);
704 ret = -EIO;
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100705 goto out;
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200706 }
707
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100708 db_remove_reset(stmt);
709
710 if (osmo_clock_gettime(CLOCK_REALTIME, &localtime) != 0) {
711 LOGP(DAUC, LOGL_ERROR, "Cannot get the current time: (%d) %s\n", errno, strerror(errno));
712 ret = -errno;
713 goto out;
714 }
715
716 stmt = dbc->stmt[DB_STMT_SET_LAST_LU_SEEN];
717
718 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
719 return -EIO;
720 /* The timestamp will be converted to UTC by SQLite. */
721 if (!db_bind_int64(stmt, "$val", (int64_t)localtime.tv_sec)) {
722 ret = -EIO;
723 goto out;
724 }
725
726 rc = sqlite3_step(stmt);
727 if (rc != SQLITE_DONE) {
728 LOGP(DAUC, LOGL_ERROR,
Stefan Sperling705b61b2018-12-07 12:44:50 +0100729 "Cannot update LU timestamp for subscriber ID=%" PRId64 ": SQL error: (%d) %s\n",
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100730 subscr_id, rc, sqlite3_errmsg(dbc->db));
731 ret = -EIO;
732 goto out;
733 }
734
735 /* verify execution result */
736 rc = sqlite3_changes(dbc->db);
737 if (!rc) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100738 LOGP(DAUC, LOGL_ERROR, "Cannot update LU timestamp for subscriber ID=%" PRId64
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100739 ": no such subscriber\n", subscr_id);
740 ret = -ENOENT;
741 goto out;
742 } else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +0100743 LOGP(DAUC, LOGL_ERROR, "Update LU timestamp for subscriber ID=%" PRId64
Stefan Sperling638ba8c2018-12-04 15:07:29 +0100744 ": SQL modified %d rows (expected 1)\n", subscr_id, rc);
745 ret = -EIO;
746 }
Harald Weltee687be52016-05-03 18:49:27 +0200747out:
Max00b37152017-02-20 11:09:27 +0100748 db_remove_reset(stmt);
Harald Weltee687be52016-05-03 18:49:27 +0200749 return ret;
750}
Harald Welteb18f0e02016-05-05 21:03:03 +0200751
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200752/*! Set the ms_purged_cs or ms_purged_ps values in the database.
753 * \param[in,out] dbc database context.
754 * \param[in] by_imsi ASCII string of IMSI digits.
755 * \param[in] purge_val true to purge, false to un-purge.
756 * \param[in] is_ps when true, set ms_purged_ps, else set ms_purged_cs.
757 * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on
758 * database errors.
759 */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200760int db_subscr_purge(struct db_context *dbc, const char *by_imsi,
761 bool purge_val, bool is_ps)
Harald Welteb18f0e02016-05-05 21:03:03 +0200762{
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200763 sqlite3_stmt *stmt;
764 int rc, ret = 0;
Harald Welteb18f0e02016-05-05 21:03:03 +0200765
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200766 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_PURGE_PS_BY_IMSI
767 : DB_STMT_UPD_PURGE_CS_BY_IMSI];
Harald Welteb18f0e02016-05-05 21:03:03 +0200768
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200769 if (!db_bind_text(stmt, "$imsi", by_imsi))
770 return -EIO;
771 if (!db_bind_int(stmt, "$val", purge_val ? 1 : 0))
772 return -EIO;
Harald Welteb18f0e02016-05-05 21:03:03 +0200773
774 /* execute the statement */
775 rc = sqlite3_step(stmt);
776 if (rc != SQLITE_DONE) {
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200777 LOGP(DAUC, LOGL_ERROR, "%s %s: SQL error: %s\n",
778 purge_val ? "purge" : "un-purge",
779 is_ps ? "PS" : "CS",
780 sqlite3_errmsg(dbc->db));
781 ret = -EIO;
782 goto out;
Harald Welteb18f0e02016-05-05 21:03:03 +0200783 }
Max00b37152017-02-20 11:09:27 +0100784
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200785 /* verify execution result */
786 rc = sqlite3_changes(dbc->db);
787 if (!rc) {
788 LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n",
789 purge_val ? "purge" : "un-purge",
790 is_ps ? "PS" : "CS",
791 by_imsi);
792 ret = -ENOENT;
793 goto out;
794 } else if (rc != 1) {
795 LOGHLR(by_imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n",
796 purge_val ? "purge" : "un-purge",
797 is_ps ? "PS" : "CS",
798 rc);
799 ret = -EIO;
800 }
801
802out:
Max00b37152017-02-20 11:09:27 +0100803 db_remove_reset(stmt);
Harald Welteb18f0e02016-05-05 21:03:03 +0200804
805 return ret;
806}
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200807
808/*! Update nam_cs/nam_ps in the db and trigger notifications to GSUP clients.
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200809 * \param[in,out] hlr Global hlr context.
810 * \param[in] subscr Subscriber from a fresh db_subscr_get_by_*() call.
811 * \param[in] nam_val True to enable CS/PS, false to disable.
812 * \param[in] is_ps True to enable/disable PS, false for CS.
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200813 * \returns 0 on success, ENOEXEC if there is no need to change, a negative
814 * value on error.
815 */
816int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps)
817{
818 int rc;
819 struct lu_operation *luop;
820 struct osmo_gsup_conn *co;
821 bool is_val = is_ps? subscr->nam_ps : subscr->nam_cs;
822
823 if (is_val == nam_val) {
824 LOGHLR(subscr->imsi, LOGL_DEBUG, "Already has the requested value when asked to %s %s\n",
825 nam_val ? "enable" : "disable", is_ps ? "PS" : "CS");
826 return ENOEXEC;
827 }
828
829 rc = db_subscr_nam(hlr->dbc, subscr->imsi, nam_val, is_ps);
830 if (rc)
831 return rc > 0? -rc : rc;
832
833 /* If we're disabling, send a notice out to the GSUP client that is
834 * responsible. Otherwise no need. */
835 if (nam_val)
836 return 0;
837
838 /* FIXME: only send to single SGSN where latest update for IMSI came from */
839 llist_for_each_entry(co, &hlr->gs->clients, list) {
840 luop = lu_op_alloc_conn(co);
841 if (!luop) {
842 LOGHLR(subscr->imsi, LOGL_ERROR,
843 "Cannot notify GSUP client, cannot allocate lu_operation,"
844 " for %s:%u\n",
845 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
846 co && co->conn && co->conn->server? co->conn->server->port : 0);
847 continue;
848 }
849 luop->subscr = *subscr;
850 lu_op_tx_del_subscr_data(luop);
851 lu_op_free(luop);
852 }
853 return 0;
854}