Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 1 | /* (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> |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 21 | #include <errno.h> |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 22 | #include <inttypes.h> |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 23 | |
| 24 | #include <osmocom/core/utils.h> |
| 25 | #include <osmocom/crypt/auth.h> |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 26 | #include <osmocom/gsm/gsm23003.h> |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 27 | |
| 28 | #include <sqlite3.h> |
| 29 | |
| 30 | #include "logging.h" |
| 31 | #include "db.h" |
| 32 | |
Neels Hofmeyr | 40aa61c | 2017-10-09 17:56:04 +0200 | [diff] [blame] | 33 | #define LOGHLR(imsi, level, fmt, args ...) LOGP(DAUC, level, "IMSI='%s': " fmt, imsi, ## args) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 34 | |
Neels Hofmeyr | 1e31d18 | 2017-10-10 23:20:09 +0200 | [diff] [blame] | 35 | #define SL3_TXT(x, stmt, idx) \ |
| 36 | do { \ |
| 37 | const char *_txt = (const char *) sqlite3_column_text(stmt, idx);\ |
| 38 | if (_txt) \ |
| 39 | strncpy(x, _txt, sizeof(x)); \ |
| 40 | x[sizeof(x)-1] = '\0'; \ |
| 41 | } while (0) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 42 | |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 43 | int 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 | |
| 71 | int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id) |
| 72 | { |
| 73 | int rc; |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 74 | struct sub_auth_data_str aud; |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 75 | int ret = 0; |
| 76 | |
| 77 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_DEL_BY_ID]; |
| 78 | |
| 79 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 80 | return -EIO; |
| 81 | |
| 82 | /* execute the statement */ |
| 83 | rc = sqlite3_step(stmt); |
| 84 | if (rc != SQLITE_DONE) { |
| 85 | LOGP(DAUC, LOGL_ERROR, |
| 86 | "Cannot delete subscriber ID=%"PRId64": SQL error: (%d) %s\n", |
| 87 | subscr_id, rc, sqlite3_errmsg(dbc->db)); |
| 88 | db_remove_reset(stmt); |
| 89 | return -EIO; |
| 90 | } |
| 91 | |
| 92 | /* verify execution result */ |
| 93 | rc = sqlite3_changes(dbc->db); |
| 94 | if (!rc) { |
| 95 | LOGP(DAUC, LOGL_ERROR, "Cannot delete: no such subscriber: ID=%"PRId64"\n", |
| 96 | subscr_id); |
| 97 | ret = -ENOENT; |
| 98 | } else if (rc != 1) { |
| 99 | LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%"PRId64 |
| 100 | ": SQL modified %d rows (expected 1)\n", subscr_id, rc); |
| 101 | ret = -EIO; |
| 102 | } |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 103 | db_remove_reset(stmt); |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 104 | |
| 105 | /* make sure to remove authentication data for this subscriber id, for |
| 106 | * both 2G and 3G. */ |
| 107 | |
| 108 | aud = (struct sub_auth_data_str){ |
| 109 | .type = OSMO_AUTH_TYPE_GSM, |
| 110 | .algo = OSMO_AUTH_ALG_NONE, |
| 111 | }; |
| 112 | rc = db_subscr_update_aud_by_id(dbc, subscr_id, &aud); |
| 113 | if (ret == -ENOENT && !rc) |
| 114 | ret = 0; |
| 115 | |
| 116 | aud = (struct sub_auth_data_str){ |
| 117 | .type = OSMO_AUTH_TYPE_UMTS, |
| 118 | .algo = OSMO_AUTH_ALG_NONE, |
| 119 | }; |
| 120 | rc = db_subscr_update_aud_by_id(dbc, subscr_id, &aud); |
| 121 | if (ret == -ENOENT && !rc) |
| 122 | ret = 0; |
| 123 | |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi, |
| 128 | const char *msisdn) |
| 129 | { |
| 130 | int rc; |
| 131 | int ret = 0; |
| 132 | |
| 133 | if (!osmo_msisdn_str_valid(msisdn)) { |
| 134 | LOGHLR(imsi, LOGL_ERROR, |
| 135 | "Cannot update subscriber: invalid MSISDN: '%s'\n", |
| 136 | msisdn); |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | |
| 140 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SET_MSISDN_BY_IMSI]; |
| 141 | |
| 142 | if (!db_bind_text(stmt, "$imsi", imsi)) |
| 143 | return -EIO; |
| 144 | if (!db_bind_text(stmt, "$msisdn", msisdn)) |
| 145 | return -EIO; |
| 146 | |
| 147 | /* execute the statement */ |
| 148 | rc = sqlite3_step(stmt); |
| 149 | if (rc != SQLITE_DONE) { |
| 150 | LOGHLR(imsi, LOGL_ERROR, |
| 151 | "Cannot update subscriber's MSISDN: SQL error: (%d) %s\n", |
| 152 | rc, sqlite3_errmsg(dbc->db)); |
| 153 | ret = -EIO; |
| 154 | goto out; |
| 155 | } |
| 156 | |
| 157 | /* verify execution result */ |
| 158 | rc = sqlite3_changes(dbc->db); |
| 159 | if (!rc) { |
| 160 | LOGP(DAUC, LOGL_ERROR, "Cannot update MSISDN: no such subscriber: IMSI='%s'\n", |
| 161 | imsi); |
| 162 | ret = -ENOENT; |
| 163 | goto out; |
| 164 | } else if (rc != 1) { |
| 165 | LOGHLR(imsi, LOGL_ERROR, "Update MSISDN: SQL modified %d rows (expected 1)\n", rc); |
| 166 | ret = -EIO; |
| 167 | } |
| 168 | |
| 169 | out: |
| 170 | db_remove_reset(stmt); |
| 171 | return ret; |
| 172 | |
| 173 | } |
| 174 | |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 175 | /* Insert or update 2G or 3G authentication tokens in the database. |
| 176 | * If aud->type is OSMO_AUTH_TYPE_GSM, the auc_2g table entry for the |
| 177 | * subscriber will be added or modified; if aud->algo is OSMO_AUTH_ALG_NONE, |
| 178 | * however, the auc_2g entry for the subscriber is deleted. If aud->type is |
| 179 | * OSMO_AUTH_TYPE_UMTS, the auc_3g table is updated; again, if aud->algo is |
| 180 | * OSMO_AUTH_ALG_NONE, the auc_3g entry is deleted. |
| 181 | * Returns 0 if successful, -EINVAL for unknown aud->type, -ENOENT for unknown |
| 182 | * subscr_id, -EIO for SQL errors. |
| 183 | */ |
| 184 | int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id, |
| 185 | const struct sub_auth_data_str *aud) |
| 186 | { |
| 187 | sqlite3_stmt *stmt_del; |
| 188 | sqlite3_stmt *stmt_ins; |
| 189 | sqlite3_stmt *stmt; |
| 190 | const char *label; |
| 191 | int rc; |
| 192 | int ret = 0; |
| 193 | |
| 194 | switch (aud->type) { |
| 195 | case OSMO_AUTH_TYPE_GSM: |
| 196 | label = "auc_2g"; |
| 197 | stmt_del = dbc->stmt[DB_STMT_AUC_2G_DELETE]; |
| 198 | stmt_ins = dbc->stmt[DB_STMT_AUC_2G_INSERT]; |
| 199 | |
| 200 | switch (aud->algo) { |
| 201 | case OSMO_AUTH_ALG_NONE: |
| 202 | case OSMO_AUTH_ALG_COMP128v1: |
| 203 | case OSMO_AUTH_ALG_COMP128v2: |
| 204 | case OSMO_AUTH_ALG_COMP128v3: |
| 205 | case OSMO_AUTH_ALG_XOR: |
| 206 | break; |
| 207 | case OSMO_AUTH_ALG_MILENAGE: |
| 208 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 209 | " auth algo not suited for 2G: %s\n", |
| 210 | osmo_auth_alg_name(aud->algo)); |
| 211 | return -EINVAL; |
| 212 | default: |
| 213 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 214 | " Unknown auth algo: %d\n", aud->algo); |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | if (aud->algo == OSMO_AUTH_ALG_NONE) |
| 219 | break; |
| 220 | if (!osmo_is_hexstr(aud->u.gsm.ki, 32, 32, true)) { |
| 221 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 222 | " Invalid KI: '%s'\n", aud->u.gsm.ki); |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | break; |
| 226 | |
| 227 | case OSMO_AUTH_TYPE_UMTS: |
| 228 | label = "auc_3g"; |
| 229 | stmt_del = dbc->stmt[DB_STMT_AUC_3G_DELETE]; |
| 230 | stmt_ins = dbc->stmt[DB_STMT_AUC_3G_INSERT]; |
| 231 | switch (aud->algo) { |
| 232 | case OSMO_AUTH_ALG_NONE: |
| 233 | case OSMO_AUTH_ALG_MILENAGE: |
| 234 | break; |
| 235 | case OSMO_AUTH_ALG_COMP128v1: |
| 236 | case OSMO_AUTH_ALG_COMP128v2: |
| 237 | case OSMO_AUTH_ALG_COMP128v3: |
| 238 | case OSMO_AUTH_ALG_XOR: |
| 239 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 240 | " auth algo not suited for 3G: %s\n", |
| 241 | osmo_auth_alg_name(aud->algo)); |
| 242 | return -EINVAL; |
| 243 | default: |
| 244 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 245 | " Unknown auth algo: %d\n", aud->algo); |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | if (aud->algo == OSMO_AUTH_ALG_NONE) |
| 250 | break; |
| 251 | if (!osmo_is_hexstr(aud->u.umts.k, 32, 32, true)) { |
| 252 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 253 | " Invalid K: '%s'\n", aud->u.umts.k); |
| 254 | return -EINVAL; |
| 255 | } |
| 256 | if (!osmo_is_hexstr(aud->u.umts.opc, 32, 32, true)) { |
| 257 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 258 | " Invalid OP/OPC: '%s'\n", aud->u.umts.opc); |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | if (aud->u.umts.ind_bitlen > OSMO_MILENAGE_IND_BITLEN_MAX) { |
| 262 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 263 | " Invalid ind_bitlen: %d\n", aud->u.umts.ind_bitlen); |
| 264 | return -EINVAL; |
| 265 | } |
| 266 | break; |
| 267 | default: |
| 268 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 269 | " unknown auth type: %d\n", aud->type); |
| 270 | return -EINVAL; |
| 271 | } |
| 272 | |
| 273 | stmt = stmt_del; |
| 274 | |
| 275 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 276 | return -EIO; |
| 277 | |
| 278 | /* execute the statement */ |
| 279 | rc = sqlite3_step(stmt); |
| 280 | if (rc != SQLITE_DONE) { |
| 281 | LOGP(DAUC, LOGL_ERROR, |
| 282 | "Cannot delete %s row: SQL error: (%d) %s\n", |
| 283 | label, rc, sqlite3_errmsg(dbc->db)); |
| 284 | ret = -EIO; |
| 285 | goto out; |
| 286 | } |
| 287 | |
| 288 | /* verify execution result */ |
| 289 | rc = sqlite3_changes(dbc->db); |
| 290 | if (!rc) |
| 291 | /* Leave "no such entry" logging to the caller -- during |
| 292 | * db_subscr_delete_by_id(), we call this to make sure it is |
| 293 | * empty, and no entry is not an error then.*/ |
| 294 | ret = -ENOENT; |
| 295 | else if (rc != 1) { |
| 296 | LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%"PRId64 |
| 297 | " from %s: SQL modified %d rows (expected 1)\n", |
| 298 | subscr_id, label, rc); |
| 299 | ret = -EIO; |
| 300 | } |
| 301 | |
| 302 | db_remove_reset(stmt); |
| 303 | |
| 304 | /* Error situation? Return now. */ |
| 305 | if (ret && ret != -ENOENT) |
| 306 | return ret; |
| 307 | |
| 308 | /* Just delete requested? */ |
| 309 | if (aud->algo == OSMO_AUTH_ALG_NONE) |
| 310 | return ret; |
| 311 | |
| 312 | /* Don't return -ENOENT if inserting new data. */ |
| 313 | ret = 0; |
| 314 | |
| 315 | /* Insert new row */ |
| 316 | stmt = stmt_ins; |
| 317 | |
| 318 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 319 | return -EIO; |
| 320 | |
| 321 | switch (aud->type) { |
| 322 | case OSMO_AUTH_TYPE_GSM: |
| 323 | if (!db_bind_int(stmt, "$algo_id_2g", aud->algo)) |
| 324 | return -EIO; |
| 325 | if (!db_bind_text(stmt, "$ki", aud->u.gsm.ki)) |
| 326 | return -EIO; |
| 327 | break; |
| 328 | case OSMO_AUTH_TYPE_UMTS: |
| 329 | if (!db_bind_int(stmt, "$algo_id_3g", aud->algo)) |
| 330 | return -EIO; |
| 331 | if (!db_bind_text(stmt, "$k", aud->u.umts.k)) |
| 332 | return -EIO; |
| 333 | if (!db_bind_text(stmt, "$op", |
| 334 | aud->u.umts.opc_is_op ? aud->u.umts.opc : NULL)) |
| 335 | return -EIO; |
| 336 | if (!db_bind_text(stmt, "$opc", |
| 337 | aud->u.umts.opc_is_op ? NULL : aud->u.umts.opc)) |
| 338 | return -EIO; |
| 339 | if (!db_bind_int(stmt, "$ind_bitlen", aud->u.umts.ind_bitlen)) |
| 340 | return -EIO; |
| 341 | break; |
| 342 | default: |
| 343 | OSMO_ASSERT(false); |
| 344 | } |
| 345 | |
| 346 | /* execute the statement */ |
| 347 | rc = sqlite3_step(stmt); |
| 348 | if (rc != SQLITE_DONE) { |
| 349 | LOGP(DAUC, LOGL_ERROR, |
| 350 | "Cannot insert %s row: SQL error: (%d) %s\n", |
| 351 | label, rc, sqlite3_errmsg(dbc->db)); |
| 352 | ret = -EIO; |
| 353 | goto out; |
| 354 | } |
| 355 | |
| 356 | out: |
| 357 | db_remove_reset(stmt); |
| 358 | return ret; |
| 359 | } |
| 360 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 361 | /* Common code for db_subscr_get_by_*() functions. */ |
| 362 | static int db_sel(struct db_context *dbc, sqlite3_stmt *stmt, struct hlr_subscriber *subscr, |
| 363 | const char **err) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 364 | { |
Max | adc6648 | 2017-02-20 11:23:20 +0100 | [diff] [blame] | 365 | int rc; |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 366 | int ret = 0; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 367 | |
| 368 | /* execute the statement */ |
| 369 | rc = sqlite3_step(stmt); |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 370 | if (rc == SQLITE_DONE) { |
| 371 | ret = -ENOENT; |
| 372 | goto out; |
| 373 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 374 | if (rc != SQLITE_ROW) { |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 375 | ret = -EIO; |
| 376 | goto out; |
Max | adc6648 | 2017-02-20 11:23:20 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 379 | if (!subscr) |
| 380 | goto out; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 381 | |
| 382 | /* obtain the various columns */ |
| 383 | subscr->id = sqlite3_column_int64(stmt, 0); |
| 384 | SL3_TXT(subscr->imsi, stmt, 1); |
| 385 | SL3_TXT(subscr->msisdn, stmt, 2); |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 386 | /* FIXME: These should all be BLOBs as they might contain NUL */ |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 387 | SL3_TXT(subscr->vlr_number, stmt, 3); |
| 388 | SL3_TXT(subscr->sgsn_number, stmt, 4); |
| 389 | SL3_TXT(subscr->sgsn_address, stmt, 5); |
| 390 | subscr->periodic_lu_timer = sqlite3_column_int(stmt, 6); |
| 391 | subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 7); |
| 392 | subscr->nam_cs = sqlite3_column_int(stmt, 8); |
| 393 | subscr->nam_ps = sqlite3_column_int(stmt, 9); |
| 394 | subscr->lmsi = sqlite3_column_int(stmt, 10); |
| 395 | subscr->ms_purged_cs = sqlite3_column_int(stmt, 11); |
| 396 | subscr->ms_purged_ps = sqlite3_column_int(stmt, 12); |
| 397 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 398 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 399 | db_remove_reset(stmt); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 400 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 401 | switch (ret) { |
| 402 | case 0: |
| 403 | *err = NULL; |
| 404 | break; |
| 405 | case -ENOENT: |
| 406 | *err = "No such subscriber"; |
| 407 | break; |
| 408 | default: |
| 409 | *err = sqlite3_errmsg(dbc->db); |
| 410 | break; |
| 411 | } |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi, |
| 416 | struct hlr_subscriber *subscr) |
| 417 | { |
| 418 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI]; |
| 419 | const char *err; |
| 420 | int rc; |
| 421 | |
| 422 | if (!db_bind_text(stmt, NULL, imsi)) |
| 423 | return -EIO; |
| 424 | |
| 425 | rc = db_sel(dbc, stmt, subscr, &err); |
| 426 | if (rc) |
| 427 | LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMSI='%s': %s\n", |
| 428 | imsi, err); |
| 429 | return rc; |
| 430 | } |
| 431 | |
| 432 | int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn, |
| 433 | struct hlr_subscriber *subscr) |
| 434 | { |
| 435 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_MSISDN]; |
| 436 | const char *err; |
| 437 | int rc; |
| 438 | |
| 439 | if (!db_bind_text(stmt, NULL, msisdn)) |
| 440 | return -EIO; |
| 441 | |
| 442 | rc = db_sel(dbc, stmt, subscr, &err); |
| 443 | if (rc) |
| 444 | LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: MSISDN='%s': %s\n", |
| 445 | msisdn, err); |
| 446 | return rc; |
| 447 | } |
| 448 | |
| 449 | int db_subscr_get_by_id(struct db_context *dbc, int64_t id, |
| 450 | struct hlr_subscriber *subscr) |
| 451 | { |
| 452 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_ID]; |
| 453 | const char *err; |
| 454 | int rc; |
| 455 | |
| 456 | if (!db_bind_int64(stmt, NULL, id)) |
| 457 | return -EIO; |
| 458 | |
| 459 | rc = db_sel(dbc, stmt, subscr, &err); |
| 460 | if (rc) |
| 461 | LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: ID=%"PRId64": %s\n", |
| 462 | id, err); |
| 463 | return rc; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 466 | /* Enable or disable PS or CS for a subscriber. |
| 467 | * For the subscriber with the given imsi, set nam_ps (when is_ps == true) or |
| 468 | * nam_cs (when is_ps == false) to nam_val in the database. |
| 469 | * Returns 0 on success, -ENOENT when the given IMSI does not exist, -EINVAL if |
| 470 | * the SQL statement could not be composed, -ENOEXEC if running the SQL |
| 471 | * statement failed, -EIO if the amount of rows modified is unexpected. |
| 472 | */ |
| 473 | int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps) |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 474 | { |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 475 | sqlite3_stmt *stmt; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 476 | int rc; |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 477 | int ret = 0; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 478 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 479 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_NAM_PS_BY_IMSI |
| 480 | : DB_STMT_UPD_NAM_CS_BY_IMSI]; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 481 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 482 | if (!db_bind_text(stmt, "$imsi", imsi)) |
| 483 | return -EIO; |
| 484 | if (!db_bind_int(stmt, "$val", nam_val ? 1 : 0)) |
| 485 | return -EIO; |
| 486 | |
| 487 | /* execute the statement */ |
| 488 | rc = sqlite3_step(stmt); |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 489 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 490 | LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL error: %s\n", |
| 491 | nam_val ? "enable" : "disable", |
| 492 | is_ps ? "PS" : "CS", |
| 493 | sqlite3_errmsg(dbc->db)); |
| 494 | ret = -EIO; |
| 495 | goto out; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 498 | /* verify execution result */ |
| 499 | rc = sqlite3_changes(dbc->db); |
| 500 | if (!rc) { |
| 501 | LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n", |
| 502 | nam_val ? "enable" : "disable", |
| 503 | is_ps ? "PS" : "CS", |
| 504 | imsi); |
| 505 | ret = -ENOENT; |
| 506 | goto out; |
| 507 | } else if (rc != 1) { |
| 508 | LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n", |
| 509 | nam_val ? "enable" : "disable", |
| 510 | is_ps ? "PS" : "CS", |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 511 | rc); |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 512 | ret = -EIO; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 515 | out: |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 516 | db_remove_reset(stmt); |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 517 | return ret; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 520 | int db_subscr_lu(struct db_context *dbc, int64_t subscr_id, |
| 521 | const char *vlr_or_sgsn_number, bool is_ps) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 522 | { |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 523 | sqlite3_stmt *stmt; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 524 | int rc, ret = 0; |
| 525 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 526 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_SGSN_BY_ID |
| 527 | : DB_STMT_UPD_VLR_BY_ID]; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 528 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 529 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 530 | return -EIO; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 531 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 532 | if (!db_bind_text(stmt, "$number", vlr_or_sgsn_number)) |
| 533 | return -EIO; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 534 | |
| 535 | /* execute the statement */ |
| 536 | rc = sqlite3_step(stmt); |
| 537 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 538 | LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%"PRId64": SQL Error: %s\n", |
| 539 | is_ps? "SGSN" : "VLR", subscr_id, sqlite3_errmsg(dbc->db)); |
| 540 | ret = -EIO; |
| 541 | goto out; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 542 | } |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 543 | |
| 544 | /* verify execution result */ |
| 545 | rc = sqlite3_changes(dbc->db); |
| 546 | if (!rc) { |
| 547 | LOGP(DAUC, LOGL_ERROR, "Cannot update %s number for subscriber ID=%"PRId64 |
| 548 | ": no such subscriber\n", |
| 549 | is_ps? "SGSN" : "VLR", subscr_id); |
| 550 | ret = -ENOENT; |
| 551 | } else if (rc != 1) { |
| 552 | LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%"PRId64 |
| 553 | ": SQL modified %d rows (expected 1)\n", |
| 554 | is_ps? "SGSN" : "VLR", subscr_id, rc); |
| 555 | ret = -EIO; |
| 556 | } |
| 557 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 558 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 559 | db_remove_reset(stmt); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 560 | return ret; |
| 561 | } |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 562 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 563 | int db_subscr_purge(struct db_context *dbc, const char *by_imsi, |
| 564 | bool purge_val, bool is_ps) |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 565 | { |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 566 | sqlite3_stmt *stmt; |
| 567 | int rc, ret = 0; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 568 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 569 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_PURGE_PS_BY_IMSI |
| 570 | : DB_STMT_UPD_PURGE_CS_BY_IMSI]; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 571 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 572 | if (!db_bind_text(stmt, "$imsi", by_imsi)) |
| 573 | return -EIO; |
| 574 | if (!db_bind_int(stmt, "$val", purge_val ? 1 : 0)) |
| 575 | return -EIO; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 576 | |
| 577 | /* execute the statement */ |
| 578 | rc = sqlite3_step(stmt); |
| 579 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 580 | LOGP(DAUC, LOGL_ERROR, "%s %s: SQL error: %s\n", |
| 581 | purge_val ? "purge" : "un-purge", |
| 582 | is_ps ? "PS" : "CS", |
| 583 | sqlite3_errmsg(dbc->db)); |
| 584 | ret = -EIO; |
| 585 | goto out; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 586 | } |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 587 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 588 | /* verify execution result */ |
| 589 | rc = sqlite3_changes(dbc->db); |
| 590 | if (!rc) { |
| 591 | LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n", |
| 592 | purge_val ? "purge" : "un-purge", |
| 593 | is_ps ? "PS" : "CS", |
| 594 | by_imsi); |
| 595 | ret = -ENOENT; |
| 596 | goto out; |
| 597 | } else if (rc != 1) { |
| 598 | LOGHLR(by_imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n", |
| 599 | purge_val ? "purge" : "un-purge", |
| 600 | is_ps ? "PS" : "CS", |
| 601 | rc); |
| 602 | ret = -EIO; |
| 603 | } |
| 604 | |
| 605 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 606 | db_remove_reset(stmt); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 607 | |
| 608 | return ret; |
| 609 | } |