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 | |
Stefan Sperling | 5c14c9c | 2018-12-07 12:30:21 +0100 | [diff] [blame] | 20 | #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 Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 25 | #include <string.h> |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 26 | #include <errno.h> |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 27 | #include <inttypes.h> |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 28 | #include <time.h> |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 29 | |
| 30 | #include <osmocom/core/utils.h> |
| 31 | #include <osmocom/crypt/auth.h> |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 32 | #include <osmocom/gsm/gsm23003.h> |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 33 | |
| 34 | #include <sqlite3.h> |
| 35 | |
| 36 | #include "logging.h" |
Neels Hofmeyr | 00b1d43 | 2017-10-17 01:43:48 +0200 | [diff] [blame] | 37 | #include "hlr.h" |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 38 | #include "db.h" |
Neels Hofmeyr | 00b1d43 | 2017-10-17 01:43:48 +0200 | [diff] [blame] | 39 | #include "gsup_server.h" |
| 40 | #include "luop.h" |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 41 | |
Neels Hofmeyr | 40aa61c | 2017-10-09 17:56:04 +0200 | [diff] [blame] | 42 | #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] | 43 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 44 | /*! 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 Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 49 | int 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 Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 77 | /*! 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 Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 86 | int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id) |
| 87 | { |
| 88 | int rc; |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 89 | struct sub_auth_data_str aud; |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 90 | 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 Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 101 | "Cannot delete subscriber ID=%" PRId64 ": SQL error: (%d) %s\n", |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 102 | 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 Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 110 | LOGP(DAUC, LOGL_ERROR, "Cannot delete: no such subscriber: ID=%" PRId64 "\n", |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 111 | subscr_id); |
| 112 | ret = -ENOENT; |
| 113 | } else if (rc != 1) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 114 | LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%" PRId64 |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 115 | ": SQL modified %d rows (expected 1)\n", subscr_id, rc); |
| 116 | ret = -EIO; |
| 117 | } |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 118 | db_remove_reset(stmt); |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 119 | |
| 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 Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 139 | return ret; |
| 140 | } |
| 141 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 142 | /*! Set a subscriber's MSISDN in the HLR database. |
| 143 | * \param[in,out] dbc database context. |
Oliver Smith | 2dc7d96 | 2019-01-15 14:14:51 +0100 | [diff] [blame] | 144 | * \param[in] imsi ASCII string of IMSI digits |
| 145 | * \param[in] msisdn ASCII string of MSISDN digits, or NULL to remove the MSISDN. |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 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 Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 149 | int 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 Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 155 | if (msisdn && !osmo_msisdn_str_valid(msisdn)) { |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 156 | LOGHLR(imsi, LOGL_ERROR, |
| 157 | "Cannot update subscriber: invalid MSISDN: '%s'\n", |
| 158 | msisdn); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 162 | sqlite3_stmt *stmt = dbc->stmt[ |
| 163 | msisdn ? DB_STMT_SET_MSISDN_BY_IMSI : DB_STMT_DELETE_MSISDN_BY_IMSI]; |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 164 | |
| 165 | if (!db_bind_text(stmt, "$imsi", imsi)) |
| 166 | return -EIO; |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 167 | if (msisdn) { |
| 168 | if (!db_bind_text(stmt, "$msisdn", msisdn)) |
| 169 | return -EIO; |
| 170 | } |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 171 | |
| 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 | |
| 194 | out: |
| 195 | db_remove_reset(stmt); |
| 196 | return ret; |
| 197 | |
| 198 | } |
| 199 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 200 | /*! Insert or update 2G or 3G authentication tokens in the database. |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 201 | * 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 Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 206 | * \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 Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 211 | */ |
| 212 | int 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 Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 324 | LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%" PRId64 |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 325 | " 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 | |
| 384 | out: |
| 385 | db_remove_reset(stmt); |
| 386 | return ret; |
| 387 | } |
| 388 | |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 389 | /*! 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 | */ |
| 396 | int 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 | |
| 431 | out: |
| 432 | db_remove_reset(stmt); |
| 433 | return ret; |
| 434 | } |
| 435 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 436 | /* Common code for db_subscr_get_by_*() functions. */ |
| 437 | static int db_sel(struct db_context *dbc, sqlite3_stmt *stmt, struct hlr_subscriber *subscr, |
| 438 | const char **err) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 439 | { |
Max | adc6648 | 2017-02-20 11:23:20 +0100 | [diff] [blame] | 440 | int rc; |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 441 | int ret = 0; |
Stefan Sperling | 5c14c9c | 2018-12-07 12:30:21 +0100 | [diff] [blame] | 442 | const char *last_lu_seen_str; |
| 443 | struct tm tm; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 444 | |
| 445 | /* execute the statement */ |
| 446 | rc = sqlite3_step(stmt); |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 447 | if (rc == SQLITE_DONE) { |
| 448 | ret = -ENOENT; |
| 449 | goto out; |
| 450 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 451 | if (rc != SQLITE_ROW) { |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 452 | ret = -EIO; |
| 453 | goto out; |
Max | adc6648 | 2017-02-20 11:23:20 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 456 | if (!subscr) |
| 457 | goto out; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 458 | |
Neels Hofmeyr | b6837e3 | 2017-10-10 23:20:26 +0200 | [diff] [blame] | 459 | *subscr = (struct hlr_subscriber){}; |
| 460 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 461 | /* obtain the various columns */ |
| 462 | subscr->id = sqlite3_column_int64(stmt, 0); |
Neels Hofmeyr | dbced93 | 2017-10-27 02:57:51 +0200 | [diff] [blame] | 463 | copy_sqlite3_text_to_buf(subscr->imsi, stmt, 1); |
| 464 | copy_sqlite3_text_to_buf(subscr->msisdn, stmt, 2); |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 465 | copy_sqlite3_text_to_buf(subscr->imei, stmt, 3); |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 466 | /* FIXME: These should all be BLOBs as they might contain NUL */ |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 467 | 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 Sperling | 5c14c9c | 2018-12-07 12:30:21 +0100 | [diff] [blame] | 478 | 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 Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 491 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 492 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 493 | db_remove_reset(stmt); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 494 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 495 | 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 Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 509 | /*! 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 Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 516 | int 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 Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 533 | /*! 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 Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 540 | int 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 Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 557 | /*! 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 Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 564 | int 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 Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 576 | LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: ID=%" PRId64 ": %s\n", |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 577 | id, err); |
| 578 | return rc; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 581 | /*! 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 | */ |
| 588 | int 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 Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 603 | /*! 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 Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 611 | */ |
| 612 | 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] | 613 | { |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 614 | sqlite3_stmt *stmt; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 615 | int rc; |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 616 | int ret = 0; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 617 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 618 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_NAM_PS_BY_IMSI |
| 619 | : DB_STMT_UPD_NAM_CS_BY_IMSI]; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 620 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 621 | 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); |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 628 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 629 | 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; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 637 | /* 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", |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 650 | rc); |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 651 | ret = -EIO; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 654 | out: |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 655 | db_remove_reset(stmt); |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 656 | return ret; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 657 | } |
| 658 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 659 | /*! 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 Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 667 | int db_subscr_lu(struct db_context *dbc, int64_t subscr_id, |
| 668 | const char *vlr_or_sgsn_number, bool is_ps) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 669 | { |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 670 | sqlite3_stmt *stmt; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 671 | int rc, ret = 0; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 672 | struct timespec localtime; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 673 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 674 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_SGSN_BY_ID |
| 675 | : DB_STMT_UPD_VLR_BY_ID]; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 676 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 677 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 678 | return -EIO; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 679 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 680 | if (!db_bind_text(stmt, "$number", vlr_or_sgsn_number)) |
| 681 | return -EIO; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 682 | |
| 683 | /* execute the statement */ |
| 684 | rc = sqlite3_step(stmt); |
| 685 | if (rc != SQLITE_DONE) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 686 | LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64 ": SQL Error: %s\n", |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 687 | is_ps? "SGSN" : "VLR", subscr_id, sqlite3_errmsg(dbc->db)); |
| 688 | ret = -EIO; |
| 689 | goto out; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 690 | } |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 691 | |
| 692 | /* verify execution result */ |
| 693 | rc = sqlite3_changes(dbc->db); |
| 694 | if (!rc) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 695 | LOGP(DAUC, LOGL_ERROR, "Cannot update %s number for subscriber ID=%" PRId64 |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 696 | ": no such subscriber\n", |
| 697 | is_ps? "SGSN" : "VLR", subscr_id); |
| 698 | ret = -ENOENT; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 699 | goto out; |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 700 | } else if (rc != 1) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 701 | LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64 |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 702 | ": SQL modified %d rows (expected 1)\n", |
| 703 | is_ps? "SGSN" : "VLR", subscr_id, rc); |
| 704 | ret = -EIO; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 705 | goto out; |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 706 | } |
| 707 | |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 708 | 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 Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 729 | "Cannot update LU timestamp for subscriber ID=%" PRId64 ": SQL error: (%d) %s\n", |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 730 | 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 Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 738 | LOGP(DAUC, LOGL_ERROR, "Cannot update LU timestamp for subscriber ID=%" PRId64 |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 739 | ": no such subscriber\n", subscr_id); |
| 740 | ret = -ENOENT; |
| 741 | goto out; |
| 742 | } else if (rc != 1) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 743 | LOGP(DAUC, LOGL_ERROR, "Update LU timestamp for subscriber ID=%" PRId64 |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 744 | ": SQL modified %d rows (expected 1)\n", subscr_id, rc); |
| 745 | ret = -EIO; |
| 746 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 747 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 748 | db_remove_reset(stmt); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 749 | return ret; |
| 750 | } |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 751 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 752 | /*! 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 Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 760 | int db_subscr_purge(struct db_context *dbc, const char *by_imsi, |
| 761 | bool purge_val, bool is_ps) |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 762 | { |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 763 | sqlite3_stmt *stmt; |
| 764 | int rc, ret = 0; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 765 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 766 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_PURGE_PS_BY_IMSI |
| 767 | : DB_STMT_UPD_PURGE_CS_BY_IMSI]; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 768 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 769 | 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 Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 773 | |
| 774 | /* execute the statement */ |
| 775 | rc = sqlite3_step(stmt); |
| 776 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 777 | 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 Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 783 | } |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 784 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 785 | /* 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 | |
| 802 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 803 | db_remove_reset(stmt); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 804 | |
| 805 | return ret; |
| 806 | } |
Neels Hofmeyr | 00b1d43 | 2017-10-17 01:43:48 +0200 | [diff] [blame] | 807 | |
| 808 | /*! Update nam_cs/nam_ps in the db and trigger notifications to GSUP clients. |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 809 | * \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 Hofmeyr | 00b1d43 | 2017-10-17 01:43:48 +0200 | [diff] [blame] | 813 | * \returns 0 on success, ENOEXEC if there is no need to change, a negative |
| 814 | * value on error. |
| 815 | */ |
| 816 | int 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 | } |