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 | |
Neels Hofmeyr | 2f75803 | 2019-11-20 00:37:07 +0100 | [diff] [blame] | 36 | #include <osmocom/hlr/logging.h> |
| 37 | #include <osmocom/hlr/hlr.h> |
| 38 | #include <osmocom/hlr/db.h> |
| 39 | #include <osmocom/hlr/gsup_server.h> |
| 40 | #include <osmocom/hlr/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. |
Oliver Smith | cd2af5e | 2019-03-06 13:17:39 +0100 | [diff] [blame] | 47 | * \param[in] flags Bitmask of DB_SUBSCR_FLAG_*. |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 48 | * \returns 0 on success, -EINVAL on invalid IMSI, -EIO on database error. |
| 49 | */ |
Oliver Smith | cd2af5e | 2019-03-06 13:17:39 +0100 | [diff] [blame] | 50 | int db_subscr_create(struct db_context *dbc, const char *imsi, uint8_t flags) |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 51 | { |
| 52 | sqlite3_stmt *stmt; |
| 53 | int rc; |
| 54 | |
| 55 | if (!osmo_imsi_str_valid(imsi)) { |
| 56 | LOGP(DAUC, LOGL_ERROR, "Cannot create subscriber: invalid IMSI: '%s'\n", |
| 57 | imsi); |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | |
| 61 | stmt = dbc->stmt[DB_STMT_SUBSCR_CREATE]; |
| 62 | |
| 63 | if (!db_bind_text(stmt, "$imsi", imsi)) |
| 64 | return -EIO; |
Oliver Smith | cd2af5e | 2019-03-06 13:17:39 +0100 | [diff] [blame] | 65 | if (!db_bind_int(stmt, "$nam_cs", (flags & DB_SUBSCR_FLAG_NAM_CS) != 0)) |
| 66 | return -EIO; |
| 67 | if (!db_bind_int(stmt, "$nam_ps", (flags & DB_SUBSCR_FLAG_NAM_PS) != 0)) |
| 68 | return -EIO; |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 69 | |
| 70 | /* execute the statement */ |
| 71 | rc = sqlite3_step(stmt); |
| 72 | db_remove_reset(stmt); |
| 73 | if (rc != SQLITE_DONE) { |
| 74 | LOGHLR(imsi, LOGL_ERROR, "Cannot create subscriber: SQL error: (%d) %s\n", |
| 75 | rc, sqlite3_errmsg(dbc->db)); |
| 76 | return -EIO; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 82 | /*! Completely delete a subscriber record from the HLR database. |
| 83 | * Also remove authentication data. |
| 84 | * Future todo: also drop from all other database tables, which aren't used yet |
| 85 | * at the time of writing this. |
| 86 | * \param[in,out] dbc database context. |
| 87 | * \param[in] subscr_id ID of the subscriber in the HLR db. |
| 88 | * \returns if the subscriber was found and removed, -EIO on database error, |
| 89 | * -ENOENT if no such subscriber data exists. |
| 90 | */ |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 91 | int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id) |
| 92 | { |
| 93 | int rc; |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 94 | struct sub_auth_data_str aud; |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 95 | int ret = 0; |
| 96 | |
| 97 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_DEL_BY_ID]; |
| 98 | |
| 99 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 100 | return -EIO; |
| 101 | |
| 102 | /* execute the statement */ |
| 103 | rc = sqlite3_step(stmt); |
| 104 | if (rc != SQLITE_DONE) { |
| 105 | LOGP(DAUC, LOGL_ERROR, |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 106 | "Cannot delete subscriber ID=%" PRId64 ": SQL error: (%d) %s\n", |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 107 | subscr_id, rc, sqlite3_errmsg(dbc->db)); |
| 108 | db_remove_reset(stmt); |
| 109 | return -EIO; |
| 110 | } |
| 111 | |
| 112 | /* verify execution result */ |
| 113 | rc = sqlite3_changes(dbc->db); |
| 114 | if (!rc) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 115 | LOGP(DAUC, LOGL_ERROR, "Cannot delete: no such subscriber: ID=%" PRId64 "\n", |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 116 | subscr_id); |
| 117 | ret = -ENOENT; |
| 118 | } else if (rc != 1) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 119 | LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%" PRId64 |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 120 | ": SQL modified %d rows (expected 1)\n", subscr_id, rc); |
| 121 | ret = -EIO; |
| 122 | } |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 123 | db_remove_reset(stmt); |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 124 | |
| 125 | /* make sure to remove authentication data for this subscriber id, for |
| 126 | * both 2G and 3G. */ |
| 127 | |
| 128 | aud = (struct sub_auth_data_str){ |
| 129 | .type = OSMO_AUTH_TYPE_GSM, |
| 130 | .algo = OSMO_AUTH_ALG_NONE, |
| 131 | }; |
| 132 | rc = db_subscr_update_aud_by_id(dbc, subscr_id, &aud); |
| 133 | if (ret == -ENOENT && !rc) |
| 134 | ret = 0; |
| 135 | |
| 136 | aud = (struct sub_auth_data_str){ |
| 137 | .type = OSMO_AUTH_TYPE_UMTS, |
| 138 | .algo = OSMO_AUTH_ALG_NONE, |
| 139 | }; |
| 140 | rc = db_subscr_update_aud_by_id(dbc, subscr_id, &aud); |
| 141 | if (ret == -ENOENT && !rc) |
| 142 | ret = 0; |
| 143 | |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 144 | return ret; |
| 145 | } |
| 146 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 147 | /*! Set a subscriber's MSISDN in the HLR database. |
| 148 | * \param[in,out] dbc database context. |
Oliver Smith | 2dc7d96 | 2019-01-15 14:14:51 +0100 | [diff] [blame] | 149 | * \param[in] imsi ASCII string of IMSI digits |
| 150 | * \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] | 151 | * \returns 0 on success, -EINVAL in case of invalid MSISDN string, -EIO on |
| 152 | * database failure, -ENOENT if no such subscriber exists. |
| 153 | */ |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 154 | int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi, |
| 155 | const char *msisdn) |
| 156 | { |
| 157 | int rc; |
| 158 | int ret = 0; |
| 159 | |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 160 | if (msisdn && !osmo_msisdn_str_valid(msisdn)) { |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 161 | LOGHLR(imsi, LOGL_ERROR, |
| 162 | "Cannot update subscriber: invalid MSISDN: '%s'\n", |
| 163 | msisdn); |
| 164 | return -EINVAL; |
| 165 | } |
| 166 | |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 167 | sqlite3_stmt *stmt = dbc->stmt[ |
| 168 | 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] | 169 | |
| 170 | if (!db_bind_text(stmt, "$imsi", imsi)) |
| 171 | return -EIO; |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 172 | if (msisdn) { |
| 173 | if (!db_bind_text(stmt, "$msisdn", msisdn)) |
| 174 | return -EIO; |
| 175 | } |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 176 | |
| 177 | /* execute the statement */ |
| 178 | rc = sqlite3_step(stmt); |
| 179 | if (rc != SQLITE_DONE) { |
| 180 | LOGHLR(imsi, LOGL_ERROR, |
| 181 | "Cannot update subscriber's MSISDN: SQL error: (%d) %s\n", |
| 182 | rc, sqlite3_errmsg(dbc->db)); |
| 183 | ret = -EIO; |
| 184 | goto out; |
| 185 | } |
| 186 | |
| 187 | /* verify execution result */ |
| 188 | rc = sqlite3_changes(dbc->db); |
| 189 | if (!rc) { |
| 190 | LOGP(DAUC, LOGL_ERROR, "Cannot update MSISDN: no such subscriber: IMSI='%s'\n", |
| 191 | imsi); |
| 192 | ret = -ENOENT; |
| 193 | goto out; |
| 194 | } else if (rc != 1) { |
| 195 | LOGHLR(imsi, LOGL_ERROR, "Update MSISDN: SQL modified %d rows (expected 1)\n", rc); |
| 196 | ret = -EIO; |
| 197 | } |
| 198 | |
| 199 | out: |
| 200 | db_remove_reset(stmt); |
| 201 | return ret; |
| 202 | |
| 203 | } |
| 204 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 205 | /*! Insert or update 2G or 3G authentication tokens in the database. |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 206 | * If aud->type is OSMO_AUTH_TYPE_GSM, the auc_2g table entry for the |
| 207 | * subscriber will be added or modified; if aud->algo is OSMO_AUTH_ALG_NONE, |
| 208 | * however, the auc_2g entry for the subscriber is deleted. If aud->type is |
| 209 | * OSMO_AUTH_TYPE_UMTS, the auc_3g table is updated; again, if aud->algo is |
| 210 | * OSMO_AUTH_ALG_NONE, the auc_3g entry is deleted. |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 211 | * \param[in,out] dbc database context. |
| 212 | * \param[in] subscr_id DB ID of the subscriber. |
| 213 | * \param[in] aud Pointer to new auth data (in ASCII string form). |
| 214 | * \returns 0 on success, -EINVAL for invalid aud, -ENOENT for unknown |
| 215 | * subscr_id, -EIO for database errors. |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 216 | */ |
| 217 | int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id, |
| 218 | const struct sub_auth_data_str *aud) |
| 219 | { |
| 220 | sqlite3_stmt *stmt_del; |
| 221 | sqlite3_stmt *stmt_ins; |
| 222 | sqlite3_stmt *stmt; |
| 223 | const char *label; |
| 224 | int rc; |
| 225 | int ret = 0; |
| 226 | |
| 227 | switch (aud->type) { |
| 228 | case OSMO_AUTH_TYPE_GSM: |
| 229 | label = "auc_2g"; |
| 230 | stmt_del = dbc->stmt[DB_STMT_AUC_2G_DELETE]; |
| 231 | stmt_ins = dbc->stmt[DB_STMT_AUC_2G_INSERT]; |
| 232 | |
| 233 | switch (aud->algo) { |
| 234 | case OSMO_AUTH_ALG_NONE: |
| 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 | break; |
| 240 | case OSMO_AUTH_ALG_MILENAGE: |
| 241 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 242 | " auth algo not suited for 2G: %s\n", |
| 243 | osmo_auth_alg_name(aud->algo)); |
| 244 | return -EINVAL; |
| 245 | default: |
| 246 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 247 | " Unknown auth algo: %d\n", aud->algo); |
| 248 | return -EINVAL; |
| 249 | } |
| 250 | |
| 251 | if (aud->algo == OSMO_AUTH_ALG_NONE) |
| 252 | break; |
| 253 | if (!osmo_is_hexstr(aud->u.gsm.ki, 32, 32, true)) { |
| 254 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 255 | " Invalid KI: '%s'\n", aud->u.gsm.ki); |
| 256 | return -EINVAL; |
| 257 | } |
| 258 | break; |
| 259 | |
| 260 | case OSMO_AUTH_TYPE_UMTS: |
| 261 | label = "auc_3g"; |
| 262 | stmt_del = dbc->stmt[DB_STMT_AUC_3G_DELETE]; |
| 263 | stmt_ins = dbc->stmt[DB_STMT_AUC_3G_INSERT]; |
| 264 | switch (aud->algo) { |
| 265 | case OSMO_AUTH_ALG_NONE: |
| 266 | case OSMO_AUTH_ALG_MILENAGE: |
| 267 | break; |
| 268 | case OSMO_AUTH_ALG_COMP128v1: |
| 269 | case OSMO_AUTH_ALG_COMP128v2: |
| 270 | case OSMO_AUTH_ALG_COMP128v3: |
| 271 | case OSMO_AUTH_ALG_XOR: |
| 272 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 273 | " auth algo not suited for 3G: %s\n", |
| 274 | osmo_auth_alg_name(aud->algo)); |
| 275 | return -EINVAL; |
| 276 | default: |
| 277 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 278 | " Unknown auth algo: %d\n", aud->algo); |
| 279 | return -EINVAL; |
| 280 | } |
| 281 | |
| 282 | if (aud->algo == OSMO_AUTH_ALG_NONE) |
| 283 | break; |
| 284 | if (!osmo_is_hexstr(aud->u.umts.k, 32, 32, true)) { |
| 285 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 286 | " Invalid K: '%s'\n", aud->u.umts.k); |
| 287 | return -EINVAL; |
| 288 | } |
| 289 | if (!osmo_is_hexstr(aud->u.umts.opc, 32, 32, true)) { |
| 290 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 291 | " Invalid OP/OPC: '%s'\n", aud->u.umts.opc); |
| 292 | return -EINVAL; |
| 293 | } |
| 294 | if (aud->u.umts.ind_bitlen > OSMO_MILENAGE_IND_BITLEN_MAX) { |
| 295 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 296 | " Invalid ind_bitlen: %d\n", aud->u.umts.ind_bitlen); |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | break; |
| 300 | default: |
| 301 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 302 | " unknown auth type: %d\n", aud->type); |
| 303 | return -EINVAL; |
| 304 | } |
| 305 | |
| 306 | stmt = stmt_del; |
| 307 | |
| 308 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 309 | return -EIO; |
| 310 | |
| 311 | /* execute the statement */ |
| 312 | rc = sqlite3_step(stmt); |
| 313 | if (rc != SQLITE_DONE) { |
| 314 | LOGP(DAUC, LOGL_ERROR, |
| 315 | "Cannot delete %s row: SQL error: (%d) %s\n", |
| 316 | label, rc, sqlite3_errmsg(dbc->db)); |
| 317 | ret = -EIO; |
| 318 | goto out; |
| 319 | } |
| 320 | |
| 321 | /* verify execution result */ |
| 322 | rc = sqlite3_changes(dbc->db); |
| 323 | if (!rc) |
| 324 | /* Leave "no such entry" logging to the caller -- during |
| 325 | * db_subscr_delete_by_id(), we call this to make sure it is |
| 326 | * empty, and no entry is not an error then.*/ |
| 327 | ret = -ENOENT; |
| 328 | else if (rc != 1) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 329 | LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%" PRId64 |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 330 | " from %s: SQL modified %d rows (expected 1)\n", |
| 331 | subscr_id, label, rc); |
| 332 | ret = -EIO; |
| 333 | } |
| 334 | |
| 335 | db_remove_reset(stmt); |
| 336 | |
| 337 | /* Error situation? Return now. */ |
| 338 | if (ret && ret != -ENOENT) |
| 339 | return ret; |
| 340 | |
| 341 | /* Just delete requested? */ |
| 342 | if (aud->algo == OSMO_AUTH_ALG_NONE) |
| 343 | return ret; |
| 344 | |
| 345 | /* Don't return -ENOENT if inserting new data. */ |
| 346 | ret = 0; |
| 347 | |
| 348 | /* Insert new row */ |
| 349 | stmt = stmt_ins; |
| 350 | |
| 351 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 352 | return -EIO; |
| 353 | |
| 354 | switch (aud->type) { |
| 355 | case OSMO_AUTH_TYPE_GSM: |
| 356 | if (!db_bind_int(stmt, "$algo_id_2g", aud->algo)) |
| 357 | return -EIO; |
| 358 | if (!db_bind_text(stmt, "$ki", aud->u.gsm.ki)) |
| 359 | return -EIO; |
| 360 | break; |
| 361 | case OSMO_AUTH_TYPE_UMTS: |
| 362 | if (!db_bind_int(stmt, "$algo_id_3g", aud->algo)) |
| 363 | return -EIO; |
| 364 | if (!db_bind_text(stmt, "$k", aud->u.umts.k)) |
| 365 | return -EIO; |
| 366 | if (!db_bind_text(stmt, "$op", |
| 367 | aud->u.umts.opc_is_op ? aud->u.umts.opc : NULL)) |
| 368 | return -EIO; |
| 369 | if (!db_bind_text(stmt, "$opc", |
| 370 | aud->u.umts.opc_is_op ? NULL : aud->u.umts.opc)) |
| 371 | return -EIO; |
| 372 | if (!db_bind_int(stmt, "$ind_bitlen", aud->u.umts.ind_bitlen)) |
| 373 | return -EIO; |
| 374 | break; |
| 375 | default: |
| 376 | OSMO_ASSERT(false); |
| 377 | } |
| 378 | |
| 379 | /* execute the statement */ |
| 380 | rc = sqlite3_step(stmt); |
| 381 | if (rc != SQLITE_DONE) { |
| 382 | LOGP(DAUC, LOGL_ERROR, |
| 383 | "Cannot insert %s row: SQL error: (%d) %s\n", |
| 384 | label, rc, sqlite3_errmsg(dbc->db)); |
| 385 | ret = -EIO; |
| 386 | goto out; |
| 387 | } |
| 388 | |
| 389 | out: |
| 390 | db_remove_reset(stmt); |
| 391 | return ret; |
| 392 | } |
| 393 | |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 394 | /*! Set a subscriber's IMEI in the HLR database. |
| 395 | * \param[in,out] dbc database context. |
| 396 | * \param[in] imsi ASCII string of IMSI digits |
| 397 | * \param[in] imei ASCII string of identifier digits, or NULL to remove the IMEI. |
| 398 | * \returns 0 on success, -ENOENT when the given subscriber does not exist, |
| 399 | * -EIO on database errors. |
| 400 | */ |
| 401 | int db_subscr_update_imei_by_imsi(struct db_context *dbc, const char* imsi, const char *imei) |
| 402 | { |
| 403 | int rc, ret = 0; |
| 404 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_UPD_IMEI_BY_IMSI]; |
| 405 | |
| 406 | if (imei && !osmo_imei_str_valid(imei, false)) { |
| 407 | LOGP(DAUC, LOGL_ERROR, "Cannot update subscriber IMSI='%s': invalid IMEI: '%s'\n", imsi, imei); |
| 408 | return -EINVAL; |
| 409 | } |
| 410 | |
| 411 | if (!db_bind_text(stmt, "$imsi", imsi)) |
| 412 | return -EIO; |
| 413 | if (imei && !db_bind_text(stmt, "$imei", imei)) |
| 414 | return -EIO; |
| 415 | |
| 416 | /* execute the statement */ |
| 417 | rc = sqlite3_step(stmt); |
| 418 | if (rc != SQLITE_DONE) { |
| 419 | LOGP(DAUC, LOGL_ERROR, "Update IMEI for subscriber IMSI='%s': SQL Error: %s\n", imsi, |
| 420 | sqlite3_errmsg(dbc->db)); |
| 421 | ret = -EIO; |
| 422 | goto out; |
| 423 | } |
| 424 | |
| 425 | /* verify execution result */ |
| 426 | rc = sqlite3_changes(dbc->db); |
| 427 | if (!rc) { |
| 428 | LOGP(DAUC, LOGL_ERROR, "Cannot update IMEI for subscriber IMSI='%s': no such subscriber\n", imsi); |
| 429 | ret = -ENOENT; |
| 430 | } else if (rc != 1) { |
| 431 | LOGP(DAUC, LOGL_ERROR, "Update IMEI for subscriber IMSI='%s': SQL modified %d rows (expected 1)\n", |
| 432 | imsi, rc); |
| 433 | ret = -EIO; |
| 434 | } |
| 435 | |
| 436 | out: |
| 437 | db_remove_reset(stmt); |
| 438 | return ret; |
| 439 | } |
| 440 | |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 441 | static void parse_last_lu_seen(time_t *dst, const char *last_lu_seen_str, const char *imsi, const char *label) |
| 442 | { |
| 443 | struct tm tm = {0}; |
| 444 | time_t val; |
| 445 | if (!last_lu_seen_str || last_lu_seen_str[0] == '\0') |
| 446 | return; |
| 447 | |
| 448 | if (strptime(last_lu_seen_str, DB_LAST_LU_SEEN_FMT, &tm) == NULL) { |
| 449 | LOGP(DAUC, LOGL_ERROR, "IMSI-%s: Last LU Seen %s: Cannot parse timestamp '%s'\n", |
| 450 | imsi, label, last_lu_seen_str); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | errno = 0; |
| 455 | val = mktime(&tm); |
| 456 | if (val == -1) { |
| 457 | LOGP(DAUC, LOGL_ERROR, "IMSI-%s: Last LU Seen %s: Cannot convert timestamp '%s' to time_t: %s\n", |
| 458 | imsi, label, last_lu_seen_str, strerror(errno)); |
| 459 | val = 0; |
| 460 | } |
| 461 | |
| 462 | *dst = val; |
| 463 | } |
| 464 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 465 | /* Common code for db_subscr_get_by_*() functions. */ |
| 466 | static int db_sel(struct db_context *dbc, sqlite3_stmt *stmt, struct hlr_subscriber *subscr, |
| 467 | const char **err) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 468 | { |
Max | adc6648 | 2017-02-20 11:23:20 +0100 | [diff] [blame] | 469 | int rc; |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 470 | int ret = 0; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 471 | |
| 472 | /* execute the statement */ |
| 473 | rc = sqlite3_step(stmt); |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 474 | if (rc == SQLITE_DONE) { |
| 475 | ret = -ENOENT; |
| 476 | goto out; |
| 477 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 478 | if (rc != SQLITE_ROW) { |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 479 | ret = -EIO; |
| 480 | goto out; |
Max | adc6648 | 2017-02-20 11:23:20 +0100 | [diff] [blame] | 481 | } |
| 482 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 483 | if (!subscr) |
| 484 | goto out; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 485 | |
Neels Hofmeyr | b6837e3 | 2017-10-10 23:20:26 +0200 | [diff] [blame] | 486 | *subscr = (struct hlr_subscriber){}; |
| 487 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 488 | /* obtain the various columns */ |
| 489 | subscr->id = sqlite3_column_int64(stmt, 0); |
Neels Hofmeyr | dbced93 | 2017-10-27 02:57:51 +0200 | [diff] [blame] | 490 | copy_sqlite3_text_to_buf(subscr->imsi, stmt, 1); |
| 491 | copy_sqlite3_text_to_buf(subscr->msisdn, stmt, 2); |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 492 | copy_sqlite3_text_to_buf(subscr->imei, stmt, 3); |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 493 | /* FIXME: These should all be BLOBs as they might contain NUL */ |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 494 | copy_sqlite3_text_to_buf(subscr->vlr_number, stmt, 4); |
| 495 | copy_sqlite3_text_to_buf(subscr->sgsn_number, stmt, 5); |
| 496 | copy_sqlite3_text_to_buf(subscr->sgsn_address, stmt, 6); |
| 497 | subscr->periodic_lu_timer = sqlite3_column_int(stmt, 7); |
| 498 | subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 8); |
| 499 | subscr->nam_cs = sqlite3_column_int(stmt, 9); |
| 500 | subscr->nam_ps = sqlite3_column_int(stmt, 10); |
| 501 | subscr->lmsi = sqlite3_column_int(stmt, 11); |
| 502 | subscr->ms_purged_cs = sqlite3_column_int(stmt, 12); |
| 503 | subscr->ms_purged_ps = sqlite3_column_int(stmt, 13); |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 504 | parse_last_lu_seen(&subscr->last_lu_seen, (const char *)sqlite3_column_text(stmt, 14), |
| 505 | subscr->imsi, "CS"); |
| 506 | parse_last_lu_seen(&subscr->last_lu_seen_ps, (const char *)sqlite3_column_text(stmt, 15), |
| 507 | subscr->imsi, "PS"); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 508 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 509 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 510 | db_remove_reset(stmt); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 511 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 512 | switch (ret) { |
| 513 | case 0: |
| 514 | *err = NULL; |
| 515 | break; |
| 516 | case -ENOENT: |
| 517 | *err = "No such subscriber"; |
| 518 | break; |
| 519 | default: |
| 520 | *err = sqlite3_errmsg(dbc->db); |
| 521 | break; |
| 522 | } |
| 523 | return ret; |
| 524 | } |
| 525 | |
Oliver Smith | 6b73fd9 | 2019-03-06 13:49:05 +0100 | [diff] [blame] | 526 | /*! Check if a subscriber exists in the HLR database. |
| 527 | * \param[in, out] dbc database context. |
| 528 | * \param[in] imsi ASCII string of IMSI digits. |
| 529 | * \returns 0 if it exists, -ENOENT if it does not exist, -EIO on database error. |
| 530 | */ |
| 531 | int db_subscr_exists_by_imsi(struct db_context *dbc, const char *imsi) { |
| 532 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_EXISTS_BY_IMSI]; |
| 533 | const char *err; |
| 534 | int rc; |
| 535 | |
| 536 | if (!db_bind_text(stmt, NULL, imsi)) |
| 537 | return -EIO; |
| 538 | |
| 539 | rc = sqlite3_step(stmt); |
| 540 | db_remove_reset(stmt); |
| 541 | if (rc == SQLITE_ROW) |
| 542 | return 0; /* exists */ |
| 543 | if (rc == SQLITE_DONE) |
| 544 | return -ENOENT; /* does not exist */ |
| 545 | |
| 546 | err = sqlite3_errmsg(dbc->db); |
| 547 | LOGP(DAUC, LOGL_ERROR, "Failed to check if subscriber exists by IMSI='%s': %s\n", imsi, err); |
| 548 | return rc; |
| 549 | } |
| 550 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 551 | /*! Retrieve subscriber data from the HLR database. |
| 552 | * \param[in,out] dbc database context. |
| 553 | * \param[in] imsi ASCII string of IMSI digits. |
| 554 | * \param[out] subscr place retrieved data in this struct. |
| 555 | * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on |
| 556 | * database error. |
| 557 | */ |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 558 | int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi, |
| 559 | struct hlr_subscriber *subscr) |
| 560 | { |
| 561 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI]; |
| 562 | const char *err; |
| 563 | int rc; |
| 564 | |
| 565 | if (!db_bind_text(stmt, NULL, imsi)) |
| 566 | return -EIO; |
| 567 | |
| 568 | rc = db_sel(dbc, stmt, subscr, &err); |
| 569 | if (rc) |
| 570 | LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMSI='%s': %s\n", |
| 571 | imsi, err); |
| 572 | return rc; |
| 573 | } |
| 574 | |
Vadim Yanitskiy | c13599d | 2019-03-30 17:03:42 +0700 | [diff] [blame] | 575 | /*! Check if a subscriber exists in the HLR database. |
| 576 | * \param[in, out] dbc database context. |
| 577 | * \param[in] msisdn ASCII string of MSISDN digits. |
| 578 | * \returns 0 if it exists, -ENOENT if it does not exist, -EIO on database error. |
| 579 | */ |
| 580 | int db_subscr_exists_by_msisdn(struct db_context *dbc, const char *msisdn) |
| 581 | { |
| 582 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_EXISTS_BY_MSISDN]; |
| 583 | const char *err; |
| 584 | int rc; |
| 585 | |
| 586 | if (!db_bind_text(stmt, NULL, msisdn)) |
| 587 | return -EIO; |
| 588 | |
| 589 | rc = sqlite3_step(stmt); |
| 590 | db_remove_reset(stmt); |
| 591 | if (rc == SQLITE_ROW) |
| 592 | return 0; /* exists */ |
| 593 | if (rc == SQLITE_DONE) |
| 594 | return -ENOENT; /* does not exist */ |
| 595 | |
| 596 | err = sqlite3_errmsg(dbc->db); |
| 597 | LOGP(DAUC, LOGL_ERROR, "Failed to check if subscriber exists " |
| 598 | "by MSISDN='%s': %s\n", msisdn, err); |
| 599 | return rc; |
| 600 | } |
| 601 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 602 | /*! Retrieve subscriber data from the HLR database. |
| 603 | * \param[in,out] dbc database context. |
| 604 | * \param[in] msisdn ASCII string of MSISDN digits. |
| 605 | * \param[out] subscr place retrieved data in this struct. |
| 606 | * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on |
| 607 | * database error. |
| 608 | */ |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 609 | int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn, |
| 610 | struct hlr_subscriber *subscr) |
| 611 | { |
| 612 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_MSISDN]; |
| 613 | const char *err; |
| 614 | int rc; |
| 615 | |
| 616 | if (!db_bind_text(stmt, NULL, msisdn)) |
| 617 | return -EIO; |
| 618 | |
| 619 | rc = db_sel(dbc, stmt, subscr, &err); |
| 620 | if (rc) |
| 621 | LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: MSISDN='%s': %s\n", |
| 622 | msisdn, err); |
| 623 | return rc; |
| 624 | } |
| 625 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 626 | /*! Retrieve subscriber data from the HLR database. |
| 627 | * \param[in,out] dbc database context. |
| 628 | * \param[in] id ID of the subscriber in the HLR db. |
| 629 | * \param[out] subscr place retrieved data in this struct. |
| 630 | * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on |
| 631 | * database error. |
| 632 | */ |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 633 | int db_subscr_get_by_id(struct db_context *dbc, int64_t id, |
| 634 | struct hlr_subscriber *subscr) |
| 635 | { |
| 636 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_ID]; |
| 637 | const char *err; |
| 638 | int rc; |
| 639 | |
| 640 | if (!db_bind_int64(stmt, NULL, id)) |
| 641 | return -EIO; |
| 642 | |
| 643 | rc = db_sel(dbc, stmt, subscr, &err); |
| 644 | if (rc) |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 645 | 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] | 646 | id, err); |
| 647 | return rc; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 650 | /*! Retrieve subscriber data from the HLR database. |
| 651 | * \param[in,out] dbc database context. |
| 652 | * \param[in] imei ASCII string of identifier digits |
| 653 | * \param[out] subscr place retrieved data in this struct. |
| 654 | * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on |
| 655 | * database error. |
| 656 | */ |
| 657 | int db_subscr_get_by_imei(struct db_context *dbc, const char *imei, struct hlr_subscriber *subscr) |
| 658 | { |
| 659 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMEI]; |
| 660 | const char *err; |
| 661 | int rc; |
| 662 | |
| 663 | if (!db_bind_text(stmt, NULL, imei)) |
| 664 | return -EIO; |
| 665 | |
| 666 | rc = db_sel(dbc, stmt, subscr, &err); |
| 667 | if (rc) |
| 668 | LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMEI=%s: %s\n", imei, err); |
| 669 | return rc; |
| 670 | } |
| 671 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 672 | /*! You should use hlr_subscr_nam() instead; enable or disable PS or CS for a |
| 673 | * subscriber without notifying GSUP clients. |
| 674 | * \param[in,out] dbc database context. |
| 675 | * \param[in] imsi ASCII string of IMSI digits. |
| 676 | * \param[in] nam_val True to enable CS/PS, false to disable. |
| 677 | * \param[in] is_ps when true, set nam_ps, else set nam_cs. |
| 678 | * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on |
| 679 | * database errors. |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 680 | */ |
| 681 | 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] | 682 | { |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 683 | sqlite3_stmt *stmt; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 684 | int rc; |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 685 | int ret = 0; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 686 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 687 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_NAM_PS_BY_IMSI |
| 688 | : DB_STMT_UPD_NAM_CS_BY_IMSI]; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 689 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 690 | if (!db_bind_text(stmt, "$imsi", imsi)) |
| 691 | return -EIO; |
| 692 | if (!db_bind_int(stmt, "$val", nam_val ? 1 : 0)) |
| 693 | return -EIO; |
| 694 | |
| 695 | /* execute the statement */ |
| 696 | rc = sqlite3_step(stmt); |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 697 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 698 | LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL error: %s\n", |
| 699 | nam_val ? "enable" : "disable", |
| 700 | is_ps ? "PS" : "CS", |
| 701 | sqlite3_errmsg(dbc->db)); |
| 702 | ret = -EIO; |
| 703 | goto out; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 704 | } |
| 705 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 706 | /* verify execution result */ |
| 707 | rc = sqlite3_changes(dbc->db); |
| 708 | if (!rc) { |
| 709 | LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n", |
| 710 | nam_val ? "enable" : "disable", |
| 711 | is_ps ? "PS" : "CS", |
| 712 | imsi); |
| 713 | ret = -ENOENT; |
| 714 | goto out; |
| 715 | } else if (rc != 1) { |
| 716 | LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n", |
| 717 | nam_val ? "enable" : "disable", |
| 718 | is_ps ? "PS" : "CS", |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 719 | rc); |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 720 | ret = -EIO; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 721 | } |
| 722 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 723 | out: |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 724 | db_remove_reset(stmt); |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 725 | return ret; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 726 | } |
| 727 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 728 | /*! Record a Location Updating in the database. |
| 729 | * \param[in,out] dbc database context. |
| 730 | * \param[in] subscr_id ID of the subscriber in the HLR db. |
| 731 | * \param[in] vlr_or_sgsn_number ASCII string of identifier digits. |
| 732 | * \param[in] is_ps when true, set sgsn_number, else set vlr_number. |
| 733 | * \returns 0 on success, -ENOENT when the given subscriber does not exist, |
| 734 | * -EIO on database errors. |
| 735 | */ |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 736 | int db_subscr_lu(struct db_context *dbc, int64_t subscr_id, |
| 737 | const char *vlr_or_sgsn_number, bool is_ps) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 738 | { |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 739 | sqlite3_stmt *stmt; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 740 | int rc, ret = 0; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 741 | struct timespec localtime; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 742 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 743 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_SGSN_BY_ID |
| 744 | : DB_STMT_UPD_VLR_BY_ID]; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 745 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 746 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 747 | return -EIO; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 748 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 749 | if (!db_bind_text(stmt, "$number", vlr_or_sgsn_number)) |
| 750 | return -EIO; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 751 | |
| 752 | /* execute the statement */ |
| 753 | rc = sqlite3_step(stmt); |
| 754 | if (rc != SQLITE_DONE) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 755 | 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] | 756 | is_ps? "SGSN" : "VLR", subscr_id, sqlite3_errmsg(dbc->db)); |
| 757 | ret = -EIO; |
| 758 | goto out; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 759 | } |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 760 | |
| 761 | /* verify execution result */ |
| 762 | rc = sqlite3_changes(dbc->db); |
| 763 | if (!rc) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 764 | LOGP(DAUC, LOGL_ERROR, "Cannot update %s number for subscriber ID=%" PRId64 |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 765 | ": no such subscriber\n", |
| 766 | is_ps? "SGSN" : "VLR", subscr_id); |
| 767 | ret = -ENOENT; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 768 | goto out; |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 769 | } else if (rc != 1) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 770 | LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64 |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 771 | ": SQL modified %d rows (expected 1)\n", |
| 772 | is_ps? "SGSN" : "VLR", subscr_id, rc); |
| 773 | ret = -EIO; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 774 | goto out; |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 775 | } |
| 776 | |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 777 | db_remove_reset(stmt); |
| 778 | |
| 779 | if (osmo_clock_gettime(CLOCK_REALTIME, &localtime) != 0) { |
| 780 | LOGP(DAUC, LOGL_ERROR, "Cannot get the current time: (%d) %s\n", errno, strerror(errno)); |
| 781 | ret = -errno; |
| 782 | goto out; |
| 783 | } |
| 784 | |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 785 | stmt = dbc->stmt[is_ps? DB_STMT_SET_LAST_LU_SEEN_PS : DB_STMT_SET_LAST_LU_SEEN]; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 786 | |
| 787 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 788 | return -EIO; |
| 789 | /* The timestamp will be converted to UTC by SQLite. */ |
| 790 | if (!db_bind_int64(stmt, "$val", (int64_t)localtime.tv_sec)) { |
| 791 | ret = -EIO; |
| 792 | goto out; |
| 793 | } |
| 794 | |
| 795 | rc = sqlite3_step(stmt); |
| 796 | if (rc != SQLITE_DONE) { |
| 797 | LOGP(DAUC, LOGL_ERROR, |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 798 | "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] | 799 | subscr_id, rc, sqlite3_errmsg(dbc->db)); |
| 800 | ret = -EIO; |
| 801 | goto out; |
| 802 | } |
| 803 | |
| 804 | /* verify execution result */ |
| 805 | rc = sqlite3_changes(dbc->db); |
| 806 | if (!rc) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 807 | LOGP(DAUC, LOGL_ERROR, "Cannot update LU timestamp for subscriber ID=%" PRId64 |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 808 | ": no such subscriber\n", subscr_id); |
| 809 | ret = -ENOENT; |
| 810 | goto out; |
| 811 | } else if (rc != 1) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 812 | LOGP(DAUC, LOGL_ERROR, "Update LU timestamp for subscriber ID=%" PRId64 |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 813 | ": SQL modified %d rows (expected 1)\n", subscr_id, rc); |
| 814 | ret = -EIO; |
| 815 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 816 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 817 | db_remove_reset(stmt); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 818 | return ret; |
| 819 | } |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 820 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 821 | /*! Set the ms_purged_cs or ms_purged_ps values in the database. |
| 822 | * \param[in,out] dbc database context. |
| 823 | * \param[in] by_imsi ASCII string of IMSI digits. |
| 824 | * \param[in] purge_val true to purge, false to un-purge. |
| 825 | * \param[in] is_ps when true, set ms_purged_ps, else set ms_purged_cs. |
| 826 | * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on |
| 827 | * database errors. |
| 828 | */ |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 829 | int db_subscr_purge(struct db_context *dbc, const char *by_imsi, |
| 830 | bool purge_val, bool is_ps) |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 831 | { |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 832 | sqlite3_stmt *stmt; |
| 833 | int rc, ret = 0; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 834 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 835 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_PURGE_PS_BY_IMSI |
| 836 | : DB_STMT_UPD_PURGE_CS_BY_IMSI]; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 837 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 838 | if (!db_bind_text(stmt, "$imsi", by_imsi)) |
| 839 | return -EIO; |
| 840 | if (!db_bind_int(stmt, "$val", purge_val ? 1 : 0)) |
| 841 | return -EIO; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 842 | |
| 843 | /* execute the statement */ |
| 844 | rc = sqlite3_step(stmt); |
| 845 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 846 | LOGP(DAUC, LOGL_ERROR, "%s %s: SQL error: %s\n", |
| 847 | purge_val ? "purge" : "un-purge", |
| 848 | is_ps ? "PS" : "CS", |
| 849 | sqlite3_errmsg(dbc->db)); |
| 850 | ret = -EIO; |
| 851 | goto out; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 852 | } |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 853 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 854 | /* verify execution result */ |
| 855 | rc = sqlite3_changes(dbc->db); |
| 856 | if (!rc) { |
| 857 | LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n", |
| 858 | purge_val ? "purge" : "un-purge", |
| 859 | is_ps ? "PS" : "CS", |
| 860 | by_imsi); |
| 861 | ret = -ENOENT; |
| 862 | goto out; |
| 863 | } else if (rc != 1) { |
| 864 | LOGHLR(by_imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n", |
| 865 | purge_val ? "purge" : "un-purge", |
| 866 | is_ps ? "PS" : "CS", |
| 867 | rc); |
| 868 | ret = -EIO; |
| 869 | } |
| 870 | |
| 871 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 872 | db_remove_reset(stmt); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 873 | |
| 874 | return ret; |
| 875 | } |
Neels Hofmeyr | 00b1d43 | 2017-10-17 01:43:48 +0200 | [diff] [blame] | 876 | |
| 877 | /*! 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] | 878 | * \param[in,out] hlr Global hlr context. |
| 879 | * \param[in] subscr Subscriber from a fresh db_subscr_get_by_*() call. |
| 880 | * \param[in] nam_val True to enable CS/PS, false to disable. |
| 881 | * \param[in] is_ps True to enable/disable PS, false for CS. |
Neels Hofmeyr | 00b1d43 | 2017-10-17 01:43:48 +0200 | [diff] [blame] | 882 | * \returns 0 on success, ENOEXEC if there is no need to change, a negative |
| 883 | * value on error. |
| 884 | */ |
| 885 | int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps) |
| 886 | { |
| 887 | int rc; |
| 888 | struct lu_operation *luop; |
| 889 | struct osmo_gsup_conn *co; |
| 890 | bool is_val = is_ps? subscr->nam_ps : subscr->nam_cs; |
| 891 | |
| 892 | if (is_val == nam_val) { |
| 893 | LOGHLR(subscr->imsi, LOGL_DEBUG, "Already has the requested value when asked to %s %s\n", |
| 894 | nam_val ? "enable" : "disable", is_ps ? "PS" : "CS"); |
| 895 | return ENOEXEC; |
| 896 | } |
| 897 | |
| 898 | rc = db_subscr_nam(hlr->dbc, subscr->imsi, nam_val, is_ps); |
| 899 | if (rc) |
| 900 | return rc > 0? -rc : rc; |
| 901 | |
| 902 | /* If we're disabling, send a notice out to the GSUP client that is |
| 903 | * responsible. Otherwise no need. */ |
| 904 | if (nam_val) |
| 905 | return 0; |
| 906 | |
| 907 | /* FIXME: only send to single SGSN where latest update for IMSI came from */ |
| 908 | llist_for_each_entry(co, &hlr->gs->clients, list) { |
| 909 | luop = lu_op_alloc_conn(co); |
| 910 | if (!luop) { |
| 911 | LOGHLR(subscr->imsi, LOGL_ERROR, |
| 912 | "Cannot notify GSUP client, cannot allocate lu_operation," |
| 913 | " for %s:%u\n", |
| 914 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 915 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 916 | continue; |
| 917 | } |
| 918 | luop->subscr = *subscr; |
| 919 | lu_op_tx_del_subscr_data(luop); |
| 920 | lu_op_free(luop); |
| 921 | } |
| 922 | return 0; |
| 923 | } |