blob: c4d4974661bbc72beaf96f78c9b69aec1200e955 [file] [log] [blame]
Harald Weltee687be52016-05-03 18:49:27 +02001/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20#include <string.h>
Max00b37152017-02-20 11:09:27 +010021#include <errno.h>
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020022#include <inttypes.h>
Harald Weltee687be52016-05-03 18:49:27 +020023
24#include <osmocom/core/utils.h>
25#include <osmocom/crypt/auth.h>
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020026#include <osmocom/gsm/gsm23003.h>
Harald Weltee687be52016-05-03 18:49:27 +020027
28#include <sqlite3.h>
29
30#include "logging.h"
Neels Hofmeyr00b1d432017-10-17 01:43:48 +020031#include "hlr.h"
Harald Weltee687be52016-05-03 18:49:27 +020032#include "db.h"
Neels Hofmeyr00b1d432017-10-17 01:43:48 +020033#include "gsup_server.h"
34#include "luop.h"
Harald Weltee687be52016-05-03 18:49:27 +020035
Neels Hofmeyr40aa61c2017-10-09 17:56:04 +020036#define LOGHLR(imsi, level, fmt, args ...) LOGP(DAUC, level, "IMSI='%s': " fmt, imsi, ## args)
Harald Weltee687be52016-05-03 18:49:27 +020037
Neels Hofmeyr16140f72017-10-25 19:17:18 +020038/*! Add new subscriber record to the HLR database.
39 * \param[in,out] dbc database context.
40 * \param[in] imsi ASCII string of IMSI digits, is validated.
41 * \returns 0 on success, -EINVAL on invalid IMSI, -EIO on database error.
42 */
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020043int db_subscr_create(struct db_context *dbc, const char *imsi)
44{
45 sqlite3_stmt *stmt;
46 int rc;
47
48 if (!osmo_imsi_str_valid(imsi)) {
49 LOGP(DAUC, LOGL_ERROR, "Cannot create subscriber: invalid IMSI: '%s'\n",
50 imsi);
51 return -EINVAL;
52 }
53
54 stmt = dbc->stmt[DB_STMT_SUBSCR_CREATE];
55
56 if (!db_bind_text(stmt, "$imsi", imsi))
57 return -EIO;
58
59 /* execute the statement */
60 rc = sqlite3_step(stmt);
61 db_remove_reset(stmt);
62 if (rc != SQLITE_DONE) {
63 LOGHLR(imsi, LOGL_ERROR, "Cannot create subscriber: SQL error: (%d) %s\n",
64 rc, sqlite3_errmsg(dbc->db));
65 return -EIO;
66 }
67
68 return 0;
69}
70
Neels Hofmeyr16140f72017-10-25 19:17:18 +020071/*! Completely delete a subscriber record from the HLR database.
72 * Also remove authentication data.
73 * Future todo: also drop from all other database tables, which aren't used yet
74 * at the time of writing this.
75 * \param[in,out] dbc database context.
76 * \param[in] subscr_id ID of the subscriber in the HLR db.
77 * \returns if the subscriber was found and removed, -EIO on database error,
78 * -ENOENT if no such subscriber data exists.
79 */
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020080int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id)
81{
82 int rc;
Neels Hofmeyr1332a172017-10-10 02:25:00 +020083 struct sub_auth_data_str aud;
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020084 int ret = 0;
85
86 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_DEL_BY_ID];
87
88 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
89 return -EIO;
90
91 /* execute the statement */
92 rc = sqlite3_step(stmt);
93 if (rc != SQLITE_DONE) {
94 LOGP(DAUC, LOGL_ERROR,
95 "Cannot delete subscriber ID=%"PRId64": SQL error: (%d) %s\n",
96 subscr_id, rc, sqlite3_errmsg(dbc->db));
97 db_remove_reset(stmt);
98 return -EIO;
99 }
100
101 /* verify execution result */
102 rc = sqlite3_changes(dbc->db);
103 if (!rc) {
104 LOGP(DAUC, LOGL_ERROR, "Cannot delete: no such subscriber: ID=%"PRId64"\n",
105 subscr_id);
106 ret = -ENOENT;
107 } else if (rc != 1) {
108 LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%"PRId64
109 ": SQL modified %d rows (expected 1)\n", subscr_id, rc);
110 ret = -EIO;
111 }
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200112 db_remove_reset(stmt);
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200113
114 /* make sure to remove authentication data for this subscriber id, for
115 * both 2G and 3G. */
116
117 aud = (struct sub_auth_data_str){
118 .type = OSMO_AUTH_TYPE_GSM,
119 .algo = OSMO_AUTH_ALG_NONE,
120 };
121 rc = db_subscr_update_aud_by_id(dbc, subscr_id, &aud);
122 if (ret == -ENOENT && !rc)
123 ret = 0;
124
125 aud = (struct sub_auth_data_str){
126 .type = OSMO_AUTH_TYPE_UMTS,
127 .algo = OSMO_AUTH_ALG_NONE,
128 };
129 rc = db_subscr_update_aud_by_id(dbc, subscr_id, &aud);
130 if (ret == -ENOENT && !rc)
131 ret = 0;
132
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200133 return ret;
134}
135
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200136/*! Set a subscriber's MSISDN in the HLR database.
137 * \param[in,out] dbc database context.
138 * \param[in] imsi ASCII string of IMSI digits.
139 * \param[in] msisdn ASCII string of MSISDN digits.
140 * \returns 0 on success, -EINVAL in case of invalid MSISDN string, -EIO on
141 * database failure, -ENOENT if no such subscriber exists.
142 */
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200143int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi,
144 const char *msisdn)
145{
146 int rc;
147 int ret = 0;
148
149 if (!osmo_msisdn_str_valid(msisdn)) {
150 LOGHLR(imsi, LOGL_ERROR,
151 "Cannot update subscriber: invalid MSISDN: '%s'\n",
152 msisdn);
153 return -EINVAL;
154 }
155
156 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SET_MSISDN_BY_IMSI];
157
158 if (!db_bind_text(stmt, "$imsi", imsi))
159 return -EIO;
160 if (!db_bind_text(stmt, "$msisdn", msisdn))
161 return -EIO;
162
163 /* execute the statement */
164 rc = sqlite3_step(stmt);
165 if (rc != SQLITE_DONE) {
166 LOGHLR(imsi, LOGL_ERROR,
167 "Cannot update subscriber's MSISDN: SQL error: (%d) %s\n",
168 rc, sqlite3_errmsg(dbc->db));
169 ret = -EIO;
170 goto out;
171 }
172
173 /* verify execution result */
174 rc = sqlite3_changes(dbc->db);
175 if (!rc) {
176 LOGP(DAUC, LOGL_ERROR, "Cannot update MSISDN: no such subscriber: IMSI='%s'\n",
177 imsi);
178 ret = -ENOENT;
179 goto out;
180 } else if (rc != 1) {
181 LOGHLR(imsi, LOGL_ERROR, "Update MSISDN: SQL modified %d rows (expected 1)\n", rc);
182 ret = -EIO;
183 }
184
185out:
186 db_remove_reset(stmt);
187 return ret;
188
189}
190
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200191/*! Insert or update 2G or 3G authentication tokens in the database.
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200192 * If aud->type is OSMO_AUTH_TYPE_GSM, the auc_2g table entry for the
193 * subscriber will be added or modified; if aud->algo is OSMO_AUTH_ALG_NONE,
194 * however, the auc_2g entry for the subscriber is deleted. If aud->type is
195 * OSMO_AUTH_TYPE_UMTS, the auc_3g table is updated; again, if aud->algo is
196 * OSMO_AUTH_ALG_NONE, the auc_3g entry is deleted.
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200197 * \param[in,out] dbc database context.
198 * \param[in] subscr_id DB ID of the subscriber.
199 * \param[in] aud Pointer to new auth data (in ASCII string form).
200 * \returns 0 on success, -EINVAL for invalid aud, -ENOENT for unknown
201 * subscr_id, -EIO for database errors.
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200202 */
203int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id,
204 const struct sub_auth_data_str *aud)
205{
206 sqlite3_stmt *stmt_del;
207 sqlite3_stmt *stmt_ins;
208 sqlite3_stmt *stmt;
209 const char *label;
210 int rc;
211 int ret = 0;
212
213 switch (aud->type) {
214 case OSMO_AUTH_TYPE_GSM:
215 label = "auc_2g";
216 stmt_del = dbc->stmt[DB_STMT_AUC_2G_DELETE];
217 stmt_ins = dbc->stmt[DB_STMT_AUC_2G_INSERT];
218
219 switch (aud->algo) {
220 case OSMO_AUTH_ALG_NONE:
221 case OSMO_AUTH_ALG_COMP128v1:
222 case OSMO_AUTH_ALG_COMP128v2:
223 case OSMO_AUTH_ALG_COMP128v3:
224 case OSMO_AUTH_ALG_XOR:
225 break;
226 case OSMO_AUTH_ALG_MILENAGE:
227 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
228 " auth algo not suited for 2G: %s\n",
229 osmo_auth_alg_name(aud->algo));
230 return -EINVAL;
231 default:
232 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
233 " Unknown auth algo: %d\n", aud->algo);
234 return -EINVAL;
235 }
236
237 if (aud->algo == OSMO_AUTH_ALG_NONE)
238 break;
239 if (!osmo_is_hexstr(aud->u.gsm.ki, 32, 32, true)) {
240 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
241 " Invalid KI: '%s'\n", aud->u.gsm.ki);
242 return -EINVAL;
243 }
244 break;
245
246 case OSMO_AUTH_TYPE_UMTS:
247 label = "auc_3g";
248 stmt_del = dbc->stmt[DB_STMT_AUC_3G_DELETE];
249 stmt_ins = dbc->stmt[DB_STMT_AUC_3G_INSERT];
250 switch (aud->algo) {
251 case OSMO_AUTH_ALG_NONE:
252 case OSMO_AUTH_ALG_MILENAGE:
253 break;
254 case OSMO_AUTH_ALG_COMP128v1:
255 case OSMO_AUTH_ALG_COMP128v2:
256 case OSMO_AUTH_ALG_COMP128v3:
257 case OSMO_AUTH_ALG_XOR:
258 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
259 " auth algo not suited for 3G: %s\n",
260 osmo_auth_alg_name(aud->algo));
261 return -EINVAL;
262 default:
263 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
264 " Unknown auth algo: %d\n", aud->algo);
265 return -EINVAL;
266 }
267
268 if (aud->algo == OSMO_AUTH_ALG_NONE)
269 break;
270 if (!osmo_is_hexstr(aud->u.umts.k, 32, 32, true)) {
271 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
272 " Invalid K: '%s'\n", aud->u.umts.k);
273 return -EINVAL;
274 }
275 if (!osmo_is_hexstr(aud->u.umts.opc, 32, 32, true)) {
276 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
277 " Invalid OP/OPC: '%s'\n", aud->u.umts.opc);
278 return -EINVAL;
279 }
280 if (aud->u.umts.ind_bitlen > OSMO_MILENAGE_IND_BITLEN_MAX) {
281 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
282 " Invalid ind_bitlen: %d\n", aud->u.umts.ind_bitlen);
283 return -EINVAL;
284 }
285 break;
286 default:
287 LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:"
288 " unknown auth type: %d\n", aud->type);
289 return -EINVAL;
290 }
291
292 stmt = stmt_del;
293
294 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
295 return -EIO;
296
297 /* execute the statement */
298 rc = sqlite3_step(stmt);
299 if (rc != SQLITE_DONE) {
300 LOGP(DAUC, LOGL_ERROR,
301 "Cannot delete %s row: SQL error: (%d) %s\n",
302 label, rc, sqlite3_errmsg(dbc->db));
303 ret = -EIO;
304 goto out;
305 }
306
307 /* verify execution result */
308 rc = sqlite3_changes(dbc->db);
309 if (!rc)
310 /* Leave "no such entry" logging to the caller -- during
311 * db_subscr_delete_by_id(), we call this to make sure it is
312 * empty, and no entry is not an error then.*/
313 ret = -ENOENT;
314 else if (rc != 1) {
315 LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%"PRId64
316 " from %s: SQL modified %d rows (expected 1)\n",
317 subscr_id, label, rc);
318 ret = -EIO;
319 }
320
321 db_remove_reset(stmt);
322
323 /* Error situation? Return now. */
324 if (ret && ret != -ENOENT)
325 return ret;
326
327 /* Just delete requested? */
328 if (aud->algo == OSMO_AUTH_ALG_NONE)
329 return ret;
330
331 /* Don't return -ENOENT if inserting new data. */
332 ret = 0;
333
334 /* Insert new row */
335 stmt = stmt_ins;
336
337 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
338 return -EIO;
339
340 switch (aud->type) {
341 case OSMO_AUTH_TYPE_GSM:
342 if (!db_bind_int(stmt, "$algo_id_2g", aud->algo))
343 return -EIO;
344 if (!db_bind_text(stmt, "$ki", aud->u.gsm.ki))
345 return -EIO;
346 break;
347 case OSMO_AUTH_TYPE_UMTS:
348 if (!db_bind_int(stmt, "$algo_id_3g", aud->algo))
349 return -EIO;
350 if (!db_bind_text(stmt, "$k", aud->u.umts.k))
351 return -EIO;
352 if (!db_bind_text(stmt, "$op",
353 aud->u.umts.opc_is_op ? aud->u.umts.opc : NULL))
354 return -EIO;
355 if (!db_bind_text(stmt, "$opc",
356 aud->u.umts.opc_is_op ? NULL : aud->u.umts.opc))
357 return -EIO;
358 if (!db_bind_int(stmt, "$ind_bitlen", aud->u.umts.ind_bitlen))
359 return -EIO;
360 break;
361 default:
362 OSMO_ASSERT(false);
363 }
364
365 /* execute the statement */
366 rc = sqlite3_step(stmt);
367 if (rc != SQLITE_DONE) {
368 LOGP(DAUC, LOGL_ERROR,
369 "Cannot insert %s row: SQL error: (%d) %s\n",
370 label, rc, sqlite3_errmsg(dbc->db));
371 ret = -EIO;
372 goto out;
373 }
374
375out:
376 db_remove_reset(stmt);
377 return ret;
378}
379
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200380/* Common code for db_subscr_get_by_*() functions. */
381static int db_sel(struct db_context *dbc, sqlite3_stmt *stmt, struct hlr_subscriber *subscr,
382 const char **err)
Harald Weltee687be52016-05-03 18:49:27 +0200383{
Maxadc66482017-02-20 11:23:20 +0100384 int rc;
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200385 int ret = 0;
Harald Weltee687be52016-05-03 18:49:27 +0200386
387 /* execute the statement */
388 rc = sqlite3_step(stmt);
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200389 if (rc == SQLITE_DONE) {
390 ret = -ENOENT;
391 goto out;
392 }
Harald Weltee687be52016-05-03 18:49:27 +0200393 if (rc != SQLITE_ROW) {
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200394 ret = -EIO;
395 goto out;
Maxadc66482017-02-20 11:23:20 +0100396 }
397
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200398 if (!subscr)
399 goto out;
Harald Weltee687be52016-05-03 18:49:27 +0200400
Neels Hofmeyrb6837e32017-10-10 23:20:26 +0200401 *subscr = (struct hlr_subscriber){};
402
Harald Weltee687be52016-05-03 18:49:27 +0200403 /* obtain the various columns */
404 subscr->id = sqlite3_column_int64(stmt, 0);
Neels Hofmeyrdbced932017-10-27 02:57:51 +0200405 copy_sqlite3_text_to_buf(subscr->imsi, stmt, 1);
406 copy_sqlite3_text_to_buf(subscr->msisdn, stmt, 2);
Harald Welte99909272016-05-05 18:24:15 +0200407 /* FIXME: These should all be BLOBs as they might contain NUL */
Neels Hofmeyrdbced932017-10-27 02:57:51 +0200408 copy_sqlite3_text_to_buf(subscr->vlr_number, stmt, 3);
409 copy_sqlite3_text_to_buf(subscr->sgsn_number, stmt, 4);
410 copy_sqlite3_text_to_buf(subscr->sgsn_address, stmt, 5);
Harald Weltee687be52016-05-03 18:49:27 +0200411 subscr->periodic_lu_timer = sqlite3_column_int(stmt, 6);
412 subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 7);
413 subscr->nam_cs = sqlite3_column_int(stmt, 8);
414 subscr->nam_ps = sqlite3_column_int(stmt, 9);
415 subscr->lmsi = sqlite3_column_int(stmt, 10);
416 subscr->ms_purged_cs = sqlite3_column_int(stmt, 11);
417 subscr->ms_purged_ps = sqlite3_column_int(stmt, 12);
418
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200419out:
Max00b37152017-02-20 11:09:27 +0100420 db_remove_reset(stmt);
Harald Weltee687be52016-05-03 18:49:27 +0200421
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200422 switch (ret) {
423 case 0:
424 *err = NULL;
425 break;
426 case -ENOENT:
427 *err = "No such subscriber";
428 break;
429 default:
430 *err = sqlite3_errmsg(dbc->db);
431 break;
432 }
433 return ret;
434}
435
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200436/*! Retrieve subscriber data from the HLR database.
437 * \param[in,out] dbc database context.
438 * \param[in] imsi ASCII string of IMSI digits.
439 * \param[out] subscr place retrieved data in this struct.
440 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
441 * database error.
442 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200443int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
444 struct hlr_subscriber *subscr)
445{
446 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI];
447 const char *err;
448 int rc;
449
450 if (!db_bind_text(stmt, NULL, imsi))
451 return -EIO;
452
453 rc = db_sel(dbc, stmt, subscr, &err);
454 if (rc)
455 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMSI='%s': %s\n",
456 imsi, err);
457 return rc;
458}
459
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200460/*! Retrieve subscriber data from the HLR database.
461 * \param[in,out] dbc database context.
462 * \param[in] msisdn ASCII string of MSISDN digits.
463 * \param[out] subscr place retrieved data in this struct.
464 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
465 * database error.
466 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200467int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
468 struct hlr_subscriber *subscr)
469{
470 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_MSISDN];
471 const char *err;
472 int rc;
473
474 if (!db_bind_text(stmt, NULL, msisdn))
475 return -EIO;
476
477 rc = db_sel(dbc, stmt, subscr, &err);
478 if (rc)
479 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: MSISDN='%s': %s\n",
480 msisdn, err);
481 return rc;
482}
483
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200484/*! Retrieve subscriber data from the HLR database.
485 * \param[in,out] dbc database context.
486 * \param[in] id ID of the subscriber in the HLR db.
487 * \param[out] subscr place retrieved data in this struct.
488 * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on
489 * database error.
490 */
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200491int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
492 struct hlr_subscriber *subscr)
493{
494 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_ID];
495 const char *err;
496 int rc;
497
498 if (!db_bind_int64(stmt, NULL, id))
499 return -EIO;
500
501 rc = db_sel(dbc, stmt, subscr, &err);
502 if (rc)
503 LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: ID=%"PRId64": %s\n",
504 id, err);
505 return rc;
Harald Weltee687be52016-05-03 18:49:27 +0200506}
507
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200508/*! You should use hlr_subscr_nam() instead; enable or disable PS or CS for a
509 * subscriber without notifying GSUP clients.
510 * \param[in,out] dbc database context.
511 * \param[in] imsi ASCII string of IMSI digits.
512 * \param[in] nam_val True to enable CS/PS, false to disable.
513 * \param[in] is_ps when true, set nam_ps, else set nam_cs.
514 * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on
515 * database errors.
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200516 */
517int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps)
Max3ce36862017-02-20 11:18:04 +0100518{
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200519 sqlite3_stmt *stmt;
Max3ce36862017-02-20 11:18:04 +0100520 int rc;
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200521 int ret = 0;
Max3ce36862017-02-20 11:18:04 +0100522
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200523 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_NAM_PS_BY_IMSI
524 : DB_STMT_UPD_NAM_CS_BY_IMSI];
Max3ce36862017-02-20 11:18:04 +0100525
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200526 if (!db_bind_text(stmt, "$imsi", imsi))
527 return -EIO;
528 if (!db_bind_int(stmt, "$val", nam_val ? 1 : 0))
529 return -EIO;
530
531 /* execute the statement */
532 rc = sqlite3_step(stmt);
Max3ce36862017-02-20 11:18:04 +0100533 if (rc != SQLITE_DONE) {
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200534 LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL error: %s\n",
535 nam_val ? "enable" : "disable",
536 is_ps ? "PS" : "CS",
537 sqlite3_errmsg(dbc->db));
538 ret = -EIO;
539 goto out;
Max3ce36862017-02-20 11:18:04 +0100540 }
541
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200542 /* verify execution result */
543 rc = sqlite3_changes(dbc->db);
544 if (!rc) {
545 LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n",
546 nam_val ? "enable" : "disable",
547 is_ps ? "PS" : "CS",
548 imsi);
549 ret = -ENOENT;
550 goto out;
551 } else if (rc != 1) {
552 LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n",
553 nam_val ? "enable" : "disable",
554 is_ps ? "PS" : "CS",
Max3ce36862017-02-20 11:18:04 +0100555 rc);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200556 ret = -EIO;
Max3ce36862017-02-20 11:18:04 +0100557 }
558
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200559out:
Max3ce36862017-02-20 11:18:04 +0100560 db_remove_reset(stmt);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200561 return ret;
Max3ce36862017-02-20 11:18:04 +0100562}
563
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200564/*! Record a Location Updating in the database.
565 * \param[in,out] dbc database context.
566 * \param[in] subscr_id ID of the subscriber in the HLR db.
567 * \param[in] vlr_or_sgsn_number ASCII string of identifier digits.
568 * \param[in] is_ps when true, set sgsn_number, else set vlr_number.
569 * \returns 0 on success, -ENOENT when the given subscriber does not exist,
570 * -EIO on database errors.
571 */
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200572int db_subscr_lu(struct db_context *dbc, int64_t subscr_id,
573 const char *vlr_or_sgsn_number, bool is_ps)
Harald Weltee687be52016-05-03 18:49:27 +0200574{
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200575 sqlite3_stmt *stmt;
Harald Weltee687be52016-05-03 18:49:27 +0200576 int rc, ret = 0;
577
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200578 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_SGSN_BY_ID
579 : DB_STMT_UPD_VLR_BY_ID];
Harald Weltee687be52016-05-03 18:49:27 +0200580
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200581 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
582 return -EIO;
Harald Weltee687be52016-05-03 18:49:27 +0200583
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200584 if (!db_bind_text(stmt, "$number", vlr_or_sgsn_number))
585 return -EIO;
Harald Weltee687be52016-05-03 18:49:27 +0200586
587 /* execute the statement */
588 rc = sqlite3_step(stmt);
589 if (rc != SQLITE_DONE) {
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200590 LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%"PRId64": SQL Error: %s\n",
591 is_ps? "SGSN" : "VLR", subscr_id, sqlite3_errmsg(dbc->db));
592 ret = -EIO;
593 goto out;
Harald Weltee687be52016-05-03 18:49:27 +0200594 }
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200595
596 /* verify execution result */
597 rc = sqlite3_changes(dbc->db);
598 if (!rc) {
599 LOGP(DAUC, LOGL_ERROR, "Cannot update %s number for subscriber ID=%"PRId64
600 ": no such subscriber\n",
601 is_ps? "SGSN" : "VLR", subscr_id);
602 ret = -ENOENT;
603 } else if (rc != 1) {
604 LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%"PRId64
605 ": SQL modified %d rows (expected 1)\n",
606 is_ps? "SGSN" : "VLR", subscr_id, rc);
607 ret = -EIO;
608 }
609
Harald Weltee687be52016-05-03 18:49:27 +0200610out:
Max00b37152017-02-20 11:09:27 +0100611 db_remove_reset(stmt);
Harald Weltee687be52016-05-03 18:49:27 +0200612 return ret;
613}
Harald Welteb18f0e02016-05-05 21:03:03 +0200614
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200615/*! Set the ms_purged_cs or ms_purged_ps values in the database.
616 * \param[in,out] dbc database context.
617 * \param[in] by_imsi ASCII string of IMSI digits.
618 * \param[in] purge_val true to purge, false to un-purge.
619 * \param[in] is_ps when true, set ms_purged_ps, else set ms_purged_cs.
620 * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on
621 * database errors.
622 */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200623int db_subscr_purge(struct db_context *dbc, const char *by_imsi,
624 bool purge_val, bool is_ps)
Harald Welteb18f0e02016-05-05 21:03:03 +0200625{
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200626 sqlite3_stmt *stmt;
627 int rc, ret = 0;
Harald Welteb18f0e02016-05-05 21:03:03 +0200628
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200629 stmt = dbc->stmt[is_ps ? DB_STMT_UPD_PURGE_PS_BY_IMSI
630 : DB_STMT_UPD_PURGE_CS_BY_IMSI];
Harald Welteb18f0e02016-05-05 21:03:03 +0200631
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200632 if (!db_bind_text(stmt, "$imsi", by_imsi))
633 return -EIO;
634 if (!db_bind_int(stmt, "$val", purge_val ? 1 : 0))
635 return -EIO;
Harald Welteb18f0e02016-05-05 21:03:03 +0200636
637 /* execute the statement */
638 rc = sqlite3_step(stmt);
639 if (rc != SQLITE_DONE) {
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200640 LOGP(DAUC, LOGL_ERROR, "%s %s: SQL error: %s\n",
641 purge_val ? "purge" : "un-purge",
642 is_ps ? "PS" : "CS",
643 sqlite3_errmsg(dbc->db));
644 ret = -EIO;
645 goto out;
Harald Welteb18f0e02016-05-05 21:03:03 +0200646 }
Max00b37152017-02-20 11:09:27 +0100647
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200648 /* verify execution result */
649 rc = sqlite3_changes(dbc->db);
650 if (!rc) {
651 LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n",
652 purge_val ? "purge" : "un-purge",
653 is_ps ? "PS" : "CS",
654 by_imsi);
655 ret = -ENOENT;
656 goto out;
657 } else if (rc != 1) {
658 LOGHLR(by_imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n",
659 purge_val ? "purge" : "un-purge",
660 is_ps ? "PS" : "CS",
661 rc);
662 ret = -EIO;
663 }
664
665out:
Max00b37152017-02-20 11:09:27 +0100666 db_remove_reset(stmt);
Harald Welteb18f0e02016-05-05 21:03:03 +0200667
668 return ret;
669}
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200670
671/*! Update nam_cs/nam_ps in the db and trigger notifications to GSUP clients.
Neels Hofmeyr16140f72017-10-25 19:17:18 +0200672 * \param[in,out] hlr Global hlr context.
673 * \param[in] subscr Subscriber from a fresh db_subscr_get_by_*() call.
674 * \param[in] nam_val True to enable CS/PS, false to disable.
675 * \param[in] is_ps True to enable/disable PS, false for CS.
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200676 * \returns 0 on success, ENOEXEC if there is no need to change, a negative
677 * value on error.
678 */
679int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps)
680{
681 int rc;
682 struct lu_operation *luop;
683 struct osmo_gsup_conn *co;
684 bool is_val = is_ps? subscr->nam_ps : subscr->nam_cs;
685
686 if (is_val == nam_val) {
687 LOGHLR(subscr->imsi, LOGL_DEBUG, "Already has the requested value when asked to %s %s\n",
688 nam_val ? "enable" : "disable", is_ps ? "PS" : "CS");
689 return ENOEXEC;
690 }
691
692 rc = db_subscr_nam(hlr->dbc, subscr->imsi, nam_val, is_ps);
693 if (rc)
694 return rc > 0? -rc : rc;
695
696 /* If we're disabling, send a notice out to the GSUP client that is
697 * responsible. Otherwise no need. */
698 if (nam_val)
699 return 0;
700
701 /* FIXME: only send to single SGSN where latest update for IMSI came from */
702 llist_for_each_entry(co, &hlr->gs->clients, list) {
703 luop = lu_op_alloc_conn(co);
704 if (!luop) {
705 LOGHLR(subscr->imsi, LOGL_ERROR,
706 "Cannot notify GSUP client, cannot allocate lu_operation,"
707 " for %s:%u\n",
708 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
709 co && co->conn && co->conn->server? co->conn->server->port : 0);
710 continue;
711 }
712 luop->subscr = *subscr;
713 lu_op_tx_del_subscr_data(luop);
714 lu_op_free(luop);
715 }
716 return 0;
717}