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> |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 31 | #include <osmocom/core/timer.h> |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 32 | #include <osmocom/crypt/auth.h> |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 33 | #include <osmocom/gsm/gsm23003.h> |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 34 | |
| 35 | #include <sqlite3.h> |
| 36 | |
Neels Hofmeyr | 2f75803 | 2019-11-20 00:37:07 +0100 | [diff] [blame] | 37 | #include <osmocom/hlr/logging.h> |
| 38 | #include <osmocom/hlr/hlr.h> |
| 39 | #include <osmocom/hlr/db.h> |
Neels Hofmeyr | c79bcde | 2019-12-04 01:04:32 +0100 | [diff] [blame] | 40 | #include <osmocom/gsupclient/cni_peer_id.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_*. |
Pau Espin Pedrol | d456fce | 2022-06-17 17:56:56 +0200 | [diff] [blame] | 48 | * \returns 0 on success, -EINVAL on invalid IMSI, -EEXIST if subscriber with |
| 49 | * provided imsi already exists, -EIO on other database errors. |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 50 | */ |
Oliver Smith | cd2af5e | 2019-03-06 13:17:39 +0100 | [diff] [blame] | 51 | 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] | 52 | { |
| 53 | sqlite3_stmt *stmt; |
| 54 | int rc; |
| 55 | |
| 56 | if (!osmo_imsi_str_valid(imsi)) { |
| 57 | LOGP(DAUC, LOGL_ERROR, "Cannot create subscriber: invalid IMSI: '%s'\n", |
| 58 | imsi); |
| 59 | return -EINVAL; |
| 60 | } |
| 61 | |
| 62 | stmt = dbc->stmt[DB_STMT_SUBSCR_CREATE]; |
| 63 | |
| 64 | if (!db_bind_text(stmt, "$imsi", imsi)) |
| 65 | return -EIO; |
Oliver Smith | cd2af5e | 2019-03-06 13:17:39 +0100 | [diff] [blame] | 66 | if (!db_bind_int(stmt, "$nam_cs", (flags & DB_SUBSCR_FLAG_NAM_CS) != 0)) |
| 67 | return -EIO; |
| 68 | if (!db_bind_int(stmt, "$nam_ps", (flags & DB_SUBSCR_FLAG_NAM_PS) != 0)) |
| 69 | return -EIO; |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 70 | |
| 71 | /* execute the statement */ |
| 72 | rc = sqlite3_step(stmt); |
| 73 | db_remove_reset(stmt); |
| 74 | if (rc != SQLITE_DONE) { |
| 75 | LOGHLR(imsi, LOGL_ERROR, "Cannot create subscriber: SQL error: (%d) %s\n", |
| 76 | rc, sqlite3_errmsg(dbc->db)); |
Pau Espin Pedrol | d456fce | 2022-06-17 17:56:56 +0200 | [diff] [blame] | 77 | if (rc == SQLITE_CONSTRAINT_UNIQUE) |
| 78 | return -EEXIST; |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 79 | return -EIO; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 85 | /*! Completely delete a subscriber record from the HLR database. |
| 86 | * Also remove authentication data. |
| 87 | * Future todo: also drop from all other database tables, which aren't used yet |
| 88 | * at the time of writing this. |
| 89 | * \param[in,out] dbc database context. |
| 90 | * \param[in] subscr_id ID of the subscriber in the HLR db. |
| 91 | * \returns if the subscriber was found and removed, -EIO on database error, |
| 92 | * -ENOENT if no such subscriber data exists. |
| 93 | */ |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 94 | int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id) |
| 95 | { |
| 96 | int rc; |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 97 | struct sub_auth_data_str aud; |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 98 | int ret = 0; |
| 99 | |
| 100 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_DEL_BY_ID]; |
| 101 | |
| 102 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 103 | return -EIO; |
| 104 | |
| 105 | /* execute the statement */ |
| 106 | rc = sqlite3_step(stmt); |
| 107 | if (rc != SQLITE_DONE) { |
| 108 | LOGP(DAUC, LOGL_ERROR, |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 109 | "Cannot delete subscriber ID=%" PRId64 ": SQL error: (%d) %s\n", |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 110 | subscr_id, rc, sqlite3_errmsg(dbc->db)); |
| 111 | db_remove_reset(stmt); |
| 112 | return -EIO; |
| 113 | } |
| 114 | |
| 115 | /* verify execution result */ |
| 116 | rc = sqlite3_changes(dbc->db); |
| 117 | if (!rc) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 118 | LOGP(DAUC, LOGL_ERROR, "Cannot delete: no such subscriber: ID=%" PRId64 "\n", |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 119 | subscr_id); |
| 120 | ret = -ENOENT; |
| 121 | } else if (rc != 1) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 122 | LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%" PRId64 |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 123 | ": SQL modified %d rows (expected 1)\n", subscr_id, rc); |
| 124 | ret = -EIO; |
| 125 | } |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 126 | db_remove_reset(stmt); |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 127 | |
| 128 | /* make sure to remove authentication data for this subscriber id, for |
| 129 | * both 2G and 3G. */ |
| 130 | |
| 131 | aud = (struct sub_auth_data_str){ |
| 132 | .type = OSMO_AUTH_TYPE_GSM, |
| 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 | |
| 139 | aud = (struct sub_auth_data_str){ |
| 140 | .type = OSMO_AUTH_TYPE_UMTS, |
| 141 | .algo = OSMO_AUTH_ALG_NONE, |
| 142 | }; |
| 143 | rc = db_subscr_update_aud_by_id(dbc, subscr_id, &aud); |
| 144 | if (ret == -ENOENT && !rc) |
| 145 | ret = 0; |
| 146 | |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 147 | return ret; |
| 148 | } |
| 149 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 150 | /*! Set a subscriber's MSISDN in the HLR database. |
| 151 | * \param[in,out] dbc database context. |
Oliver Smith | 2dc7d96 | 2019-01-15 14:14:51 +0100 | [diff] [blame] | 152 | * \param[in] imsi ASCII string of IMSI digits |
| 153 | * \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] | 154 | * \returns 0 on success, -EINVAL in case of invalid MSISDN string, -EIO on |
| 155 | * database failure, -ENOENT if no such subscriber exists. |
| 156 | */ |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 157 | int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi, |
| 158 | const char *msisdn) |
| 159 | { |
| 160 | int rc; |
| 161 | int ret = 0; |
| 162 | |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 163 | if (msisdn && !osmo_msisdn_str_valid(msisdn)) { |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 164 | LOGHLR(imsi, LOGL_ERROR, |
| 165 | "Cannot update subscriber: invalid MSISDN: '%s'\n", |
| 166 | msisdn); |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 170 | sqlite3_stmt *stmt = dbc->stmt[ |
| 171 | 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] | 172 | |
| 173 | if (!db_bind_text(stmt, "$imsi", imsi)) |
| 174 | return -EIO; |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 175 | if (msisdn) { |
| 176 | if (!db_bind_text(stmt, "$msisdn", msisdn)) |
| 177 | return -EIO; |
| 178 | } |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 179 | |
| 180 | /* execute the statement */ |
| 181 | rc = sqlite3_step(stmt); |
| 182 | if (rc != SQLITE_DONE) { |
| 183 | LOGHLR(imsi, LOGL_ERROR, |
| 184 | "Cannot update subscriber's MSISDN: SQL error: (%d) %s\n", |
| 185 | rc, sqlite3_errmsg(dbc->db)); |
| 186 | ret = -EIO; |
| 187 | goto out; |
| 188 | } |
| 189 | |
| 190 | /* verify execution result */ |
| 191 | rc = sqlite3_changes(dbc->db); |
| 192 | if (!rc) { |
| 193 | LOGP(DAUC, LOGL_ERROR, "Cannot update MSISDN: no such subscriber: IMSI='%s'\n", |
| 194 | imsi); |
| 195 | ret = -ENOENT; |
| 196 | goto out; |
| 197 | } else if (rc != 1) { |
| 198 | LOGHLR(imsi, LOGL_ERROR, "Update MSISDN: SQL modified %d rows (expected 1)\n", rc); |
| 199 | ret = -EIO; |
| 200 | } |
| 201 | |
| 202 | out: |
| 203 | db_remove_reset(stmt); |
| 204 | return ret; |
| 205 | |
| 206 | } |
| 207 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 208 | /*! Insert or update 2G or 3G authentication tokens in the database. |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 209 | * If aud->type is OSMO_AUTH_TYPE_GSM, the auc_2g table entry for the |
| 210 | * subscriber will be added or modified; if aud->algo is OSMO_AUTH_ALG_NONE, |
| 211 | * however, the auc_2g entry for the subscriber is deleted. If aud->type is |
| 212 | * OSMO_AUTH_TYPE_UMTS, the auc_3g table is updated; again, if aud->algo is |
| 213 | * OSMO_AUTH_ALG_NONE, the auc_3g entry is deleted. |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 214 | * \param[in,out] dbc database context. |
| 215 | * \param[in] subscr_id DB ID of the subscriber. |
| 216 | * \param[in] aud Pointer to new auth data (in ASCII string form). |
| 217 | * \returns 0 on success, -EINVAL for invalid aud, -ENOENT for unknown |
| 218 | * subscr_id, -EIO for database errors. |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 219 | */ |
| 220 | int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id, |
| 221 | const struct sub_auth_data_str *aud) |
| 222 | { |
| 223 | sqlite3_stmt *stmt_del; |
| 224 | sqlite3_stmt *stmt_ins; |
| 225 | sqlite3_stmt *stmt; |
| 226 | const char *label; |
| 227 | int rc; |
| 228 | int ret = 0; |
| 229 | |
| 230 | switch (aud->type) { |
| 231 | case OSMO_AUTH_TYPE_GSM: |
| 232 | label = "auc_2g"; |
| 233 | stmt_del = dbc->stmt[DB_STMT_AUC_2G_DELETE]; |
| 234 | stmt_ins = dbc->stmt[DB_STMT_AUC_2G_INSERT]; |
| 235 | |
| 236 | switch (aud->algo) { |
| 237 | case OSMO_AUTH_ALG_NONE: |
| 238 | case OSMO_AUTH_ALG_COMP128v1: |
| 239 | case OSMO_AUTH_ALG_COMP128v2: |
| 240 | case OSMO_AUTH_ALG_COMP128v3: |
| 241 | case OSMO_AUTH_ALG_XOR: |
| 242 | break; |
| 243 | case OSMO_AUTH_ALG_MILENAGE: |
| 244 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 245 | " auth algo not suited for 2G: %s\n", |
| 246 | osmo_auth_alg_name(aud->algo)); |
| 247 | return -EINVAL; |
| 248 | default: |
| 249 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 250 | " Unknown auth algo: %d\n", aud->algo); |
| 251 | return -EINVAL; |
| 252 | } |
| 253 | |
| 254 | if (aud->algo == OSMO_AUTH_ALG_NONE) |
| 255 | break; |
| 256 | if (!osmo_is_hexstr(aud->u.gsm.ki, 32, 32, true)) { |
| 257 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 258 | " Invalid KI: '%s'\n", aud->u.gsm.ki); |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | break; |
| 262 | |
| 263 | case OSMO_AUTH_TYPE_UMTS: |
| 264 | label = "auc_3g"; |
| 265 | stmt_del = dbc->stmt[DB_STMT_AUC_3G_DELETE]; |
| 266 | stmt_ins = dbc->stmt[DB_STMT_AUC_3G_INSERT]; |
| 267 | switch (aud->algo) { |
| 268 | case OSMO_AUTH_ALG_NONE: |
| 269 | case OSMO_AUTH_ALG_MILENAGE: |
Harald Welte | 6e237d3 | 2020-12-28 01:01:31 +0100 | [diff] [blame] | 270 | case OSMO_AUTH_ALG_XOR: |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 271 | break; |
| 272 | case OSMO_AUTH_ALG_COMP128v1: |
| 273 | case OSMO_AUTH_ALG_COMP128v2: |
| 274 | case OSMO_AUTH_ALG_COMP128v3: |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 275 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 276 | " auth algo not suited for 3G: %s\n", |
| 277 | osmo_auth_alg_name(aud->algo)); |
| 278 | return -EINVAL; |
| 279 | default: |
| 280 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 281 | " Unknown auth algo: %d\n", aud->algo); |
| 282 | return -EINVAL; |
| 283 | } |
| 284 | |
| 285 | if (aud->algo == OSMO_AUTH_ALG_NONE) |
| 286 | break; |
| 287 | if (!osmo_is_hexstr(aud->u.umts.k, 32, 32, true)) { |
| 288 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 289 | " Invalid K: '%s'\n", aud->u.umts.k); |
| 290 | return -EINVAL; |
| 291 | } |
| 292 | if (!osmo_is_hexstr(aud->u.umts.opc, 32, 32, true)) { |
| 293 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 294 | " Invalid OP/OPC: '%s'\n", aud->u.umts.opc); |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | if (aud->u.umts.ind_bitlen > OSMO_MILENAGE_IND_BITLEN_MAX) { |
| 298 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 299 | " Invalid ind_bitlen: %d\n", aud->u.umts.ind_bitlen); |
| 300 | return -EINVAL; |
| 301 | } |
| 302 | break; |
| 303 | default: |
| 304 | LOGP(DAUC, LOGL_ERROR, "Cannot update auth tokens:" |
| 305 | " unknown auth type: %d\n", aud->type); |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
| 309 | stmt = stmt_del; |
| 310 | |
| 311 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 312 | return -EIO; |
| 313 | |
| 314 | /* execute the statement */ |
| 315 | rc = sqlite3_step(stmt); |
| 316 | if (rc != SQLITE_DONE) { |
| 317 | LOGP(DAUC, LOGL_ERROR, |
| 318 | "Cannot delete %s row: SQL error: (%d) %s\n", |
| 319 | label, rc, sqlite3_errmsg(dbc->db)); |
| 320 | ret = -EIO; |
| 321 | goto out; |
| 322 | } |
| 323 | |
| 324 | /* verify execution result */ |
| 325 | rc = sqlite3_changes(dbc->db); |
| 326 | if (!rc) |
| 327 | /* Leave "no such entry" logging to the caller -- during |
| 328 | * db_subscr_delete_by_id(), we call this to make sure it is |
| 329 | * empty, and no entry is not an error then.*/ |
| 330 | ret = -ENOENT; |
| 331 | else if (rc != 1) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 332 | LOGP(DAUC, LOGL_ERROR, "Delete subscriber ID=%" PRId64 |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 333 | " from %s: SQL modified %d rows (expected 1)\n", |
| 334 | subscr_id, label, rc); |
| 335 | ret = -EIO; |
| 336 | } |
| 337 | |
| 338 | db_remove_reset(stmt); |
| 339 | |
| 340 | /* Error situation? Return now. */ |
| 341 | if (ret && ret != -ENOENT) |
| 342 | return ret; |
| 343 | |
| 344 | /* Just delete requested? */ |
| 345 | if (aud->algo == OSMO_AUTH_ALG_NONE) |
| 346 | return ret; |
| 347 | |
| 348 | /* Don't return -ENOENT if inserting new data. */ |
| 349 | ret = 0; |
| 350 | |
| 351 | /* Insert new row */ |
| 352 | stmt = stmt_ins; |
| 353 | |
| 354 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 355 | return -EIO; |
| 356 | |
| 357 | switch (aud->type) { |
| 358 | case OSMO_AUTH_TYPE_GSM: |
| 359 | if (!db_bind_int(stmt, "$algo_id_2g", aud->algo)) |
| 360 | return -EIO; |
| 361 | if (!db_bind_text(stmt, "$ki", aud->u.gsm.ki)) |
| 362 | return -EIO; |
| 363 | break; |
| 364 | case OSMO_AUTH_TYPE_UMTS: |
| 365 | if (!db_bind_int(stmt, "$algo_id_3g", aud->algo)) |
| 366 | return -EIO; |
| 367 | if (!db_bind_text(stmt, "$k", aud->u.umts.k)) |
| 368 | return -EIO; |
| 369 | if (!db_bind_text(stmt, "$op", |
| 370 | aud->u.umts.opc_is_op ? aud->u.umts.opc : NULL)) |
| 371 | return -EIO; |
| 372 | if (!db_bind_text(stmt, "$opc", |
| 373 | aud->u.umts.opc_is_op ? NULL : aud->u.umts.opc)) |
| 374 | return -EIO; |
| 375 | if (!db_bind_int(stmt, "$ind_bitlen", aud->u.umts.ind_bitlen)) |
| 376 | return -EIO; |
| 377 | break; |
| 378 | default: |
| 379 | OSMO_ASSERT(false); |
| 380 | } |
| 381 | |
| 382 | /* execute the statement */ |
| 383 | rc = sqlite3_step(stmt); |
| 384 | if (rc != SQLITE_DONE) { |
| 385 | LOGP(DAUC, LOGL_ERROR, |
| 386 | "Cannot insert %s row: SQL error: (%d) %s\n", |
| 387 | label, rc, sqlite3_errmsg(dbc->db)); |
| 388 | ret = -EIO; |
| 389 | goto out; |
| 390 | } |
| 391 | |
| 392 | out: |
| 393 | db_remove_reset(stmt); |
| 394 | return ret; |
| 395 | } |
| 396 | |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 397 | /*! Set a subscriber's IMEI in the HLR database. |
| 398 | * \param[in,out] dbc database context. |
| 399 | * \param[in] imsi ASCII string of IMSI digits |
| 400 | * \param[in] imei ASCII string of identifier digits, or NULL to remove the IMEI. |
| 401 | * \returns 0 on success, -ENOENT when the given subscriber does not exist, |
| 402 | * -EIO on database errors. |
| 403 | */ |
| 404 | int db_subscr_update_imei_by_imsi(struct db_context *dbc, const char* imsi, const char *imei) |
| 405 | { |
| 406 | int rc, ret = 0; |
| 407 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_UPD_IMEI_BY_IMSI]; |
| 408 | |
| 409 | if (imei && !osmo_imei_str_valid(imei, false)) { |
| 410 | LOGP(DAUC, LOGL_ERROR, "Cannot update subscriber IMSI='%s': invalid IMEI: '%s'\n", imsi, imei); |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | |
| 414 | if (!db_bind_text(stmt, "$imsi", imsi)) |
| 415 | return -EIO; |
| 416 | if (imei && !db_bind_text(stmt, "$imei", imei)) |
| 417 | return -EIO; |
| 418 | |
| 419 | /* execute the statement */ |
| 420 | rc = sqlite3_step(stmt); |
| 421 | if (rc != SQLITE_DONE) { |
| 422 | LOGP(DAUC, LOGL_ERROR, "Update IMEI for subscriber IMSI='%s': SQL Error: %s\n", imsi, |
| 423 | sqlite3_errmsg(dbc->db)); |
| 424 | ret = -EIO; |
| 425 | goto out; |
| 426 | } |
| 427 | |
| 428 | /* verify execution result */ |
| 429 | rc = sqlite3_changes(dbc->db); |
| 430 | if (!rc) { |
| 431 | LOGP(DAUC, LOGL_ERROR, "Cannot update IMEI for subscriber IMSI='%s': no such subscriber\n", imsi); |
| 432 | ret = -ENOENT; |
| 433 | } else if (rc != 1) { |
| 434 | LOGP(DAUC, LOGL_ERROR, "Update IMEI for subscriber IMSI='%s': SQL modified %d rows (expected 1)\n", |
| 435 | imsi, rc); |
| 436 | ret = -EIO; |
| 437 | } |
| 438 | |
| 439 | out: |
| 440 | db_remove_reset(stmt); |
| 441 | return ret; |
| 442 | } |
| 443 | |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 444 | static void parse_last_lu_seen(time_t *dst, const char *last_lu_seen_str, const char *imsi, const char *label) |
| 445 | { |
| 446 | struct tm tm = {0}; |
| 447 | time_t val; |
| 448 | if (!last_lu_seen_str || last_lu_seen_str[0] == '\0') |
| 449 | return; |
| 450 | |
| 451 | if (strptime(last_lu_seen_str, DB_LAST_LU_SEEN_FMT, &tm) == NULL) { |
| 452 | LOGP(DAUC, LOGL_ERROR, "IMSI-%s: Last LU Seen %s: Cannot parse timestamp '%s'\n", |
| 453 | imsi, label, last_lu_seen_str); |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | errno = 0; |
| 458 | val = mktime(&tm); |
| 459 | if (val == -1) { |
| 460 | LOGP(DAUC, LOGL_ERROR, "IMSI-%s: Last LU Seen %s: Cannot convert timestamp '%s' to time_t: %s\n", |
| 461 | imsi, label, last_lu_seen_str, strerror(errno)); |
| 462 | val = 0; |
| 463 | } |
| 464 | |
| 465 | *dst = val; |
| 466 | } |
| 467 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 468 | /* Common code for db_subscr_get_by_*() functions. */ |
| 469 | static int db_sel(struct db_context *dbc, sqlite3_stmt *stmt, struct hlr_subscriber *subscr, |
| 470 | const char **err) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 471 | { |
Max | adc6648 | 2017-02-20 11:23:20 +0100 | [diff] [blame] | 472 | int rc; |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 473 | int ret = 0; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 474 | |
| 475 | /* execute the statement */ |
| 476 | rc = sqlite3_step(stmt); |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 477 | if (rc == SQLITE_DONE) { |
| 478 | ret = -ENOENT; |
| 479 | goto out; |
| 480 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 481 | if (rc != SQLITE_ROW) { |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 482 | ret = -EIO; |
| 483 | goto out; |
Max | adc6648 | 2017-02-20 11:23:20 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 486 | if (!subscr) |
| 487 | goto out; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 488 | |
Neels Hofmeyr | b6837e3 | 2017-10-10 23:20:26 +0200 | [diff] [blame] | 489 | *subscr = (struct hlr_subscriber){}; |
| 490 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 491 | /* obtain the various columns */ |
| 492 | subscr->id = sqlite3_column_int64(stmt, 0); |
Neels Hofmeyr | dbced93 | 2017-10-27 02:57:51 +0200 | [diff] [blame] | 493 | copy_sqlite3_text_to_buf(subscr->imsi, stmt, 1); |
| 494 | copy_sqlite3_text_to_buf(subscr->msisdn, stmt, 2); |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 495 | copy_sqlite3_text_to_buf(subscr->imei, stmt, 3); |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 496 | /* FIXME: These should all be BLOBs as they might contain NUL */ |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 497 | copy_sqlite3_text_to_buf(subscr->vlr_number, stmt, 4); |
| 498 | copy_sqlite3_text_to_buf(subscr->sgsn_number, stmt, 5); |
| 499 | copy_sqlite3_text_to_buf(subscr->sgsn_address, stmt, 6); |
| 500 | subscr->periodic_lu_timer = sqlite3_column_int(stmt, 7); |
| 501 | subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 8); |
| 502 | subscr->nam_cs = sqlite3_column_int(stmt, 9); |
| 503 | subscr->nam_ps = sqlite3_column_int(stmt, 10); |
| 504 | subscr->lmsi = sqlite3_column_int(stmt, 11); |
| 505 | subscr->ms_purged_cs = sqlite3_column_int(stmt, 12); |
| 506 | subscr->ms_purged_ps = sqlite3_column_int(stmt, 13); |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 507 | parse_last_lu_seen(&subscr->last_lu_seen, (const char *)sqlite3_column_text(stmt, 14), |
| 508 | subscr->imsi, "CS"); |
| 509 | parse_last_lu_seen(&subscr->last_lu_seen_ps, (const char *)sqlite3_column_text(stmt, 15), |
| 510 | subscr->imsi, "PS"); |
Neels Hofmeyr | 04c2375 | 2019-11-25 03:59:50 +0100 | [diff] [blame] | 511 | copy_sqlite3_text_to_ipa_name(&subscr->vlr_via_proxy, stmt, 16); |
| 512 | copy_sqlite3_text_to_ipa_name(&subscr->sgsn_via_proxy, stmt, 17); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 513 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 514 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 515 | db_remove_reset(stmt); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 516 | |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 517 | switch (ret) { |
| 518 | case 0: |
| 519 | *err = NULL; |
| 520 | break; |
| 521 | case -ENOENT: |
| 522 | *err = "No such subscriber"; |
| 523 | break; |
| 524 | default: |
| 525 | *err = sqlite3_errmsg(dbc->db); |
| 526 | break; |
| 527 | } |
| 528 | return ret; |
| 529 | } |
| 530 | |
Oliver Smith | 6b73fd9 | 2019-03-06 13:49:05 +0100 | [diff] [blame] | 531 | /*! Check if a subscriber exists in the HLR database. |
| 532 | * \param[in, out] dbc database context. |
| 533 | * \param[in] imsi ASCII string of IMSI digits. |
| 534 | * \returns 0 if it exists, -ENOENT if it does not exist, -EIO on database error. |
| 535 | */ |
| 536 | int db_subscr_exists_by_imsi(struct db_context *dbc, const char *imsi) { |
| 537 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_EXISTS_BY_IMSI]; |
| 538 | const char *err; |
| 539 | int rc; |
| 540 | |
| 541 | if (!db_bind_text(stmt, NULL, imsi)) |
| 542 | return -EIO; |
| 543 | |
| 544 | rc = sqlite3_step(stmt); |
| 545 | db_remove_reset(stmt); |
| 546 | if (rc == SQLITE_ROW) |
| 547 | return 0; /* exists */ |
| 548 | if (rc == SQLITE_DONE) |
| 549 | return -ENOENT; /* does not exist */ |
| 550 | |
| 551 | err = sqlite3_errmsg(dbc->db); |
| 552 | LOGP(DAUC, LOGL_ERROR, "Failed to check if subscriber exists by IMSI='%s': %s\n", imsi, err); |
| 553 | return rc; |
| 554 | } |
| 555 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 556 | /*! Retrieve subscriber data from the HLR database. |
| 557 | * \param[in,out] dbc database context. |
| 558 | * \param[in] imsi ASCII string of IMSI digits. |
| 559 | * \param[out] subscr place retrieved data in this struct. |
| 560 | * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on |
| 561 | * database error. |
| 562 | */ |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 563 | int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi, |
| 564 | struct hlr_subscriber *subscr) |
| 565 | { |
| 566 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI]; |
| 567 | const char *err; |
| 568 | int rc; |
| 569 | |
| 570 | if (!db_bind_text(stmt, NULL, imsi)) |
| 571 | return -EIO; |
| 572 | |
| 573 | rc = db_sel(dbc, stmt, subscr, &err); |
Neels Hofmeyr | 0d82a87 | 2019-11-25 05:37:53 +0100 | [diff] [blame] | 574 | if (rc && rc != -ENOENT) |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 575 | LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMSI='%s': %s\n", |
| 576 | imsi, err); |
| 577 | return rc; |
| 578 | } |
| 579 | |
Vadim Yanitskiy | c13599d | 2019-03-30 17:03:42 +0700 | [diff] [blame] | 580 | /*! Check if a subscriber exists in the HLR database. |
| 581 | * \param[in, out] dbc database context. |
| 582 | * \param[in] msisdn ASCII string of MSISDN digits. |
| 583 | * \returns 0 if it exists, -ENOENT if it does not exist, -EIO on database error. |
| 584 | */ |
| 585 | int db_subscr_exists_by_msisdn(struct db_context *dbc, const char *msisdn) |
| 586 | { |
| 587 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_EXISTS_BY_MSISDN]; |
| 588 | const char *err; |
| 589 | int rc; |
| 590 | |
| 591 | if (!db_bind_text(stmt, NULL, msisdn)) |
| 592 | return -EIO; |
| 593 | |
| 594 | rc = sqlite3_step(stmt); |
| 595 | db_remove_reset(stmt); |
| 596 | if (rc == SQLITE_ROW) |
| 597 | return 0; /* exists */ |
| 598 | if (rc == SQLITE_DONE) |
| 599 | return -ENOENT; /* does not exist */ |
| 600 | |
| 601 | err = sqlite3_errmsg(dbc->db); |
| 602 | LOGP(DAUC, LOGL_ERROR, "Failed to check if subscriber exists " |
| 603 | "by MSISDN='%s': %s\n", msisdn, err); |
| 604 | return rc; |
| 605 | } |
| 606 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 607 | /*! Retrieve subscriber data from the HLR database. |
| 608 | * \param[in,out] dbc database context. |
| 609 | * \param[in] msisdn ASCII string of MSISDN digits. |
| 610 | * \param[out] subscr place retrieved data in this struct. |
| 611 | * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on |
| 612 | * database error. |
| 613 | */ |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 614 | int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn, |
| 615 | struct hlr_subscriber *subscr) |
| 616 | { |
| 617 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_MSISDN]; |
| 618 | const char *err; |
| 619 | int rc; |
| 620 | |
| 621 | if (!db_bind_text(stmt, NULL, msisdn)) |
| 622 | return -EIO; |
| 623 | |
| 624 | rc = db_sel(dbc, stmt, subscr, &err); |
Neels Hofmeyr | 0d82a87 | 2019-11-25 05:37:53 +0100 | [diff] [blame] | 625 | if (rc && rc != -ENOENT) |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 626 | LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: MSISDN='%s': %s\n", |
| 627 | msisdn, err); |
| 628 | return rc; |
| 629 | } |
| 630 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 631 | /*! Retrieve subscriber data from the HLR database. |
| 632 | * \param[in,out] dbc database context. |
Keith | 89fda30 | 2021-01-19 07:01:33 +0100 | [diff] [blame] | 633 | * \param[in] filter_type ASCII string of identifier type to search. |
| 634 | * \param[in] filter ASCII string to search. |
| 635 | * \param[in] get_cb pointer to call back function for data. |
| 636 | * \param[in,out] data pointer to pass to callback function. |
| 637 | * \param[in,out] count counter for number of matched subscribers. |
| 638 | * \param[in,our] err |
| 639 | * \returns 0 on success, -ENOENT if no subscriber was found, -EIO on |
| 640 | * database error. |
| 641 | */ |
| 642 | int db_subscrs_get(struct db_context *dbc, const char *filter_type, const char *filter, |
| 643 | void (*get_cb)(struct hlr_subscriber *subscr, void *data), void *data, |
| 644 | int *count, const char **err) |
| 645 | { |
| 646 | sqlite3_stmt *stmt; |
| 647 | char search[256]; |
| 648 | int rc; |
| 649 | struct hlr_subscriber subscr; |
| 650 | bool show_ls = false; |
| 651 | |
| 652 | if (!filter_type) { |
| 653 | stmt = dbc->stmt[DB_STMT_SEL_ALL]; |
Keith | ca8e6ef | 2021-05-07 05:59:21 +0200 | [diff] [blame] | 654 | } else if (strcmp(filter_type, "imei") == 0) { |
| 655 | stmt = dbc->stmt[DB_STMT_SEL_FILTER_IMEI]; |
Keith | 89fda30 | 2021-01-19 07:01:33 +0100 | [diff] [blame] | 656 | } else if (strcmp(filter_type, "imsi") == 0) { |
| 657 | stmt = dbc->stmt[DB_STMT_SEL_FILTER_IMSI]; |
| 658 | } else if (strcmp(filter_type, "msisdn") == 0) { |
| 659 | stmt = dbc->stmt[DB_STMT_SEL_FILTER_MSISDN]; |
| 660 | } else if (strcmp(filter_type, "cs") == 0) { |
| 661 | stmt = dbc->stmt[DB_STMT_SEL_FILTER_CS]; |
| 662 | } else if (strcmp(filter_type, "ps") == 0) { |
| 663 | stmt = dbc->stmt[DB_STMT_SEL_FILTER_PS]; |
| 664 | } else if (strcmp(filter_type, "last_lu_seen") == 0) { |
| 665 | show_ls = true; |
| 666 | stmt = dbc->stmt[DB_STMT_SEL_ALL_ORDER_LAST_SEEN]; |
| 667 | } else { |
| 668 | return -EIO; |
| 669 | } |
| 670 | |
Keith | b5a5676 | 2021-01-30 13:49:15 -0600 | [diff] [blame] | 671 | if (filter_type && filter && strcmp(filter_type, "last_lu_seen") != 0) { |
Keith | 89fda30 | 2021-01-19 07:01:33 +0100 | [diff] [blame] | 672 | if (strcmp(filter, "on") == 0) { |
| 673 | sprintf(search, "%s", "1"); |
| 674 | } else if (strcmp(filter, "off") == 0) { |
| 675 | sprintf(search, "%s", "0"); |
| 676 | } else { |
| 677 | sprintf(search, "%%%s%%", filter); |
| 678 | } |
| 679 | if (!db_bind_text(stmt, "$search", search)) { |
| 680 | *err = sqlite3_errmsg(dbc->db); |
| 681 | return -EIO; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | rc = sqlite3_step(stmt); |
| 686 | |
| 687 | if (rc == SQLITE_DONE) { |
| 688 | db_remove_reset(stmt); |
| 689 | *err = "No matching subscriber(s)"; |
| 690 | return -ENOENT; |
| 691 | } |
| 692 | |
| 693 | while (rc == SQLITE_ROW) { |
| 694 | subscr = (struct hlr_subscriber){ |
| 695 | .id = sqlite3_column_int64(stmt, 0),}; |
| 696 | copy_sqlite3_text_to_buf(subscr.imsi, stmt, 1); |
| 697 | copy_sqlite3_text_to_buf(subscr.msisdn, stmt, 2); |
| 698 | copy_sqlite3_text_to_buf(subscr.imei, stmt, 3); |
| 699 | subscr.nam_cs = sqlite3_column_int(stmt, 9); |
| 700 | subscr.nam_ps = sqlite3_column_int(stmt, 10); |
| 701 | if (show_ls) |
| 702 | parse_last_lu_seen(&subscr.last_lu_seen, (const char *)sqlite3_column_text(stmt, 14), |
| 703 | subscr.imsi, "CS"); |
| 704 | get_cb(&subscr, data); |
| 705 | rc = sqlite3_step(stmt); |
| 706 | (*count)++; |
| 707 | } |
| 708 | |
| 709 | db_remove_reset(stmt); |
| 710 | if (rc != SQLITE_DONE) { |
| 711 | *err = sqlite3_errmsg(dbc->db); |
Keith | 89fda30 | 2021-01-19 07:01:33 +0100 | [diff] [blame] | 712 | LOGP(DAUC, LOGL_ERROR, "Cannot read subscribers from db:: %s\n", *err); |
| 713 | return rc; |
| 714 | } |
Keith | b5a5676 | 2021-01-30 13:49:15 -0600 | [diff] [blame] | 715 | *err = NULL; |
| 716 | return 0; |
Keith | 89fda30 | 2021-01-19 07:01:33 +0100 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /*! Retrieve subscriber data from the HLR database. |
| 720 | * \param[in,out] dbc database context. |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 721 | * \param[in] id ID of the subscriber in the HLR db. |
| 722 | * \param[out] subscr place retrieved data in this struct. |
| 723 | * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on |
| 724 | * database error. |
| 725 | */ |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 726 | int db_subscr_get_by_id(struct db_context *dbc, int64_t id, |
| 727 | struct hlr_subscriber *subscr) |
| 728 | { |
| 729 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_ID]; |
| 730 | const char *err; |
| 731 | int rc; |
| 732 | |
| 733 | if (!db_bind_int64(stmt, NULL, id)) |
| 734 | return -EIO; |
| 735 | |
| 736 | rc = db_sel(dbc, stmt, subscr, &err); |
Neels Hofmeyr | 0d82a87 | 2019-11-25 05:37:53 +0100 | [diff] [blame] | 737 | if (rc && rc != -ENOENT) |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 738 | 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] | 739 | id, err); |
| 740 | return rc; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 743 | /*! Retrieve subscriber data from the HLR database. |
| 744 | * \param[in,out] dbc database context. |
| 745 | * \param[in] imei ASCII string of identifier digits |
| 746 | * \param[out] subscr place retrieved data in this struct. |
| 747 | * \returns 0 on success, -ENOENT if no such subscriber was found, -EIO on |
| 748 | * database error. |
| 749 | */ |
| 750 | int db_subscr_get_by_imei(struct db_context *dbc, const char *imei, struct hlr_subscriber *subscr) |
| 751 | { |
| 752 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMEI]; |
| 753 | const char *err; |
| 754 | int rc; |
| 755 | |
| 756 | if (!db_bind_text(stmt, NULL, imei)) |
| 757 | return -EIO; |
| 758 | |
| 759 | rc = db_sel(dbc, stmt, subscr, &err); |
Neels Hofmeyr | 0d82a87 | 2019-11-25 05:37:53 +0100 | [diff] [blame] | 760 | if (rc && rc != -ENOENT) |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 761 | LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMEI=%s: %s\n", imei, err); |
| 762 | return rc; |
| 763 | } |
| 764 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 765 | /*! You should use hlr_subscr_nam() instead; enable or disable PS or CS for a |
| 766 | * subscriber without notifying GSUP clients. |
| 767 | * \param[in,out] dbc database context. |
| 768 | * \param[in] imsi ASCII string of IMSI digits. |
| 769 | * \param[in] nam_val True to enable CS/PS, false to disable. |
| 770 | * \param[in] is_ps when true, set nam_ps, else set nam_cs. |
| 771 | * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on |
| 772 | * database errors. |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 773 | */ |
| 774 | 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] | 775 | { |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 776 | sqlite3_stmt *stmt; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 777 | int rc; |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 778 | int ret = 0; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 779 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 780 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_NAM_PS_BY_IMSI |
| 781 | : DB_STMT_UPD_NAM_CS_BY_IMSI]; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 782 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 783 | if (!db_bind_text(stmt, "$imsi", imsi)) |
| 784 | return -EIO; |
| 785 | if (!db_bind_int(stmt, "$val", nam_val ? 1 : 0)) |
| 786 | return -EIO; |
| 787 | |
| 788 | /* execute the statement */ |
| 789 | rc = sqlite3_step(stmt); |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 790 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 791 | LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL error: %s\n", |
| 792 | nam_val ? "enable" : "disable", |
| 793 | is_ps ? "PS" : "CS", |
| 794 | sqlite3_errmsg(dbc->db)); |
| 795 | ret = -EIO; |
| 796 | goto out; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 799 | /* verify execution result */ |
| 800 | rc = sqlite3_changes(dbc->db); |
| 801 | if (!rc) { |
| 802 | LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n", |
| 803 | nam_val ? "enable" : "disable", |
| 804 | is_ps ? "PS" : "CS", |
| 805 | imsi); |
| 806 | ret = -ENOENT; |
| 807 | goto out; |
| 808 | } else if (rc != 1) { |
| 809 | LOGHLR(imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n", |
| 810 | nam_val ? "enable" : "disable", |
| 811 | is_ps ? "PS" : "CS", |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 812 | rc); |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 813 | ret = -EIO; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 814 | } |
| 815 | |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 816 | out: |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 817 | db_remove_reset(stmt); |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 818 | return ret; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 821 | /*! Record a Location Updating in the database. |
| 822 | * \param[in,out] dbc database context. |
| 823 | * \param[in] subscr_id ID of the subscriber in the HLR db. |
| 824 | * \param[in] vlr_or_sgsn_number ASCII string of identifier digits. |
| 825 | * \param[in] is_ps when true, set sgsn_number, else set vlr_number. |
| 826 | * \returns 0 on success, -ENOENT when the given subscriber does not exist, |
| 827 | * -EIO on database errors. |
| 828 | */ |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 829 | int db_subscr_lu(struct db_context *dbc, int64_t subscr_id, |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 830 | const struct osmo_ipa_name *vlr_name, bool is_ps, |
| 831 | const struct osmo_ipa_name *via_proxy) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 832 | { |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 833 | sqlite3_stmt *stmt; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 834 | int rc, ret = 0; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 835 | struct timespec localtime; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 836 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 837 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_SGSN_BY_ID |
| 838 | : DB_STMT_UPD_VLR_BY_ID]; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 839 | |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 840 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 841 | return -EIO; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 842 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 843 | if (!db_bind_text(stmt, "$number", (char*)vlr_name->val)) |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 844 | return -EIO; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 845 | |
Neels Hofmeyr | 04c2375 | 2019-11-25 03:59:50 +0100 | [diff] [blame] | 846 | if (via_proxy && via_proxy->len) { |
| 847 | if (!db_bind_text(stmt, "$proxy", (char*)via_proxy->val)) |
| 848 | return -EIO; |
| 849 | } else { |
| 850 | if (!db_bind_null(stmt, "$proxy")) |
| 851 | return -EIO; |
| 852 | } |
| 853 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 854 | /* execute the statement */ |
| 855 | rc = sqlite3_step(stmt); |
| 856 | if (rc != SQLITE_DONE) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 857 | 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] | 858 | is_ps? "SGSN" : "VLR", subscr_id, sqlite3_errmsg(dbc->db)); |
| 859 | ret = -EIO; |
| 860 | goto out; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 861 | } |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 862 | |
| 863 | /* verify execution result */ |
| 864 | rc = sqlite3_changes(dbc->db); |
| 865 | if (!rc) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 866 | LOGP(DAUC, LOGL_ERROR, "Cannot update %s number for subscriber ID=%" PRId64 |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 867 | ": no such subscriber\n", |
| 868 | is_ps? "SGSN" : "VLR", subscr_id); |
| 869 | ret = -ENOENT; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 870 | goto out; |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 871 | } else if (rc != 1) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 872 | LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%" PRId64 |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 873 | ": SQL modified %d rows (expected 1)\n", |
| 874 | is_ps? "SGSN" : "VLR", subscr_id, rc); |
| 875 | ret = -EIO; |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 876 | goto out; |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 877 | } |
| 878 | |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 879 | db_remove_reset(stmt); |
| 880 | |
| 881 | if (osmo_clock_gettime(CLOCK_REALTIME, &localtime) != 0) { |
| 882 | LOGP(DAUC, LOGL_ERROR, "Cannot get the current time: (%d) %s\n", errno, strerror(errno)); |
| 883 | ret = -errno; |
| 884 | goto out; |
| 885 | } |
| 886 | |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 887 | 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] | 888 | |
| 889 | if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) |
| 890 | return -EIO; |
| 891 | /* The timestamp will be converted to UTC by SQLite. */ |
| 892 | if (!db_bind_int64(stmt, "$val", (int64_t)localtime.tv_sec)) { |
| 893 | ret = -EIO; |
| 894 | goto out; |
| 895 | } |
| 896 | |
| 897 | rc = sqlite3_step(stmt); |
| 898 | if (rc != SQLITE_DONE) { |
| 899 | LOGP(DAUC, LOGL_ERROR, |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 900 | "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] | 901 | subscr_id, rc, sqlite3_errmsg(dbc->db)); |
| 902 | ret = -EIO; |
| 903 | goto out; |
| 904 | } |
| 905 | |
| 906 | /* verify execution result */ |
| 907 | rc = sqlite3_changes(dbc->db); |
| 908 | if (!rc) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 909 | LOGP(DAUC, LOGL_ERROR, "Cannot update LU timestamp for subscriber ID=%" PRId64 |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 910 | ": no such subscriber\n", subscr_id); |
| 911 | ret = -ENOENT; |
| 912 | goto out; |
| 913 | } else if (rc != 1) { |
Stefan Sperling | 705b61b | 2018-12-07 12:44:50 +0100 | [diff] [blame] | 914 | LOGP(DAUC, LOGL_ERROR, "Update LU timestamp for subscriber ID=%" PRId64 |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 915 | ": SQL modified %d rows (expected 1)\n", subscr_id, rc); |
| 916 | ret = -EIO; |
| 917 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 918 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 919 | db_remove_reset(stmt); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 920 | return ret; |
| 921 | } |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 922 | |
Neels Hofmeyr | 16140f7 | 2017-10-25 19:17:18 +0200 | [diff] [blame] | 923 | /*! Set the ms_purged_cs or ms_purged_ps values in the database. |
| 924 | * \param[in,out] dbc database context. |
| 925 | * \param[in] by_imsi ASCII string of IMSI digits. |
| 926 | * \param[in] purge_val true to purge, false to un-purge. |
| 927 | * \param[in] is_ps when true, set ms_purged_ps, else set ms_purged_cs. |
| 928 | * \returns 0 on success, -ENOENT when the given IMSI does not exist, -EIO on |
| 929 | * database errors. |
| 930 | */ |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 931 | int db_subscr_purge(struct db_context *dbc, const char *by_imsi, |
| 932 | bool purge_val, bool is_ps) |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 933 | { |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 934 | sqlite3_stmt *stmt; |
| 935 | int rc, ret = 0; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 936 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 937 | stmt = dbc->stmt[is_ps ? DB_STMT_UPD_PURGE_PS_BY_IMSI |
| 938 | : DB_STMT_UPD_PURGE_CS_BY_IMSI]; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 939 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 940 | if (!db_bind_text(stmt, "$imsi", by_imsi)) |
| 941 | return -EIO; |
| 942 | if (!db_bind_int(stmt, "$val", purge_val ? 1 : 0)) |
| 943 | return -EIO; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 944 | |
| 945 | /* execute the statement */ |
| 946 | rc = sqlite3_step(stmt); |
| 947 | if (rc != SQLITE_DONE) { |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 948 | LOGP(DAUC, LOGL_ERROR, "%s %s: SQL error: %s\n", |
| 949 | purge_val ? "purge" : "un-purge", |
| 950 | is_ps ? "PS" : "CS", |
| 951 | sqlite3_errmsg(dbc->db)); |
| 952 | ret = -EIO; |
| 953 | goto out; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 954 | } |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 955 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 956 | /* verify execution result */ |
| 957 | rc = sqlite3_changes(dbc->db); |
| 958 | if (!rc) { |
| 959 | LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n", |
| 960 | purge_val ? "purge" : "un-purge", |
| 961 | is_ps ? "PS" : "CS", |
| 962 | by_imsi); |
| 963 | ret = -ENOENT; |
| 964 | goto out; |
| 965 | } else if (rc != 1) { |
| 966 | LOGHLR(by_imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n", |
| 967 | purge_val ? "purge" : "un-purge", |
| 968 | is_ps ? "PS" : "CS", |
| 969 | rc); |
| 970 | ret = -EIO; |
| 971 | } |
| 972 | |
| 973 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 974 | db_remove_reset(stmt); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 975 | |
| 976 | return ret; |
| 977 | } |
Neels Hofmeyr | 3f9d197 | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 978 | |
| 979 | static int _db_ind_run(struct db_context *dbc, sqlite3_stmt *stmt, const char *vlr, bool reset) |
| 980 | { |
| 981 | int rc; |
| 982 | |
| 983 | if (!db_bind_text(stmt, "$vlr", vlr)) |
| 984 | return -EIO; |
| 985 | |
| 986 | /* execute the statement */ |
| 987 | rc = sqlite3_step(stmt); |
| 988 | if (reset) |
| 989 | db_remove_reset(stmt); |
| 990 | return rc; |
| 991 | } |
| 992 | |
| 993 | static int _db_ind_add(struct db_context *dbc, const char *vlr) |
| 994 | { |
| 995 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_IND_ADD]; |
| 996 | if (_db_ind_run(dbc, stmt, vlr, true) != SQLITE_DONE) { |
| 997 | LOGP(DDB, LOGL_ERROR, "Cannot create IND entry for %s\n", osmo_quote_str_c(OTC_SELECT, vlr, -1)); |
| 998 | return -EIO; |
| 999 | } |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | static int _db_ind_del(struct db_context *dbc, const char *vlr) |
| 1004 | { |
| 1005 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_IND_DEL]; |
| 1006 | _db_ind_run(dbc, stmt, vlr, true); |
| 1007 | /* We don't really care about the result. If it didn't exist, then that was the goal anyway. */ |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | static int _db_ind_get(struct db_context *dbc, const char *vlr, unsigned int *ind) |
| 1012 | { |
| 1013 | int ret = 0; |
| 1014 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_IND_SELECT]; |
| 1015 | int rc = _db_ind_run(dbc, stmt, vlr, false); |
| 1016 | if (rc == SQLITE_DONE) { |
| 1017 | /* Does not exist yet */ |
| 1018 | ret = -ENOENT; |
| 1019 | goto out; |
| 1020 | } else if (rc != SQLITE_ROW) { |
| 1021 | LOGP(DDB, LOGL_ERROR, "Error executing SQL: %d\n", rc); |
| 1022 | ret = -EIO; |
| 1023 | goto out; |
| 1024 | } |
| 1025 | |
| 1026 | OSMO_ASSERT(ind); |
| 1027 | *ind = sqlite3_column_int64(stmt, 0); |
| 1028 | out: |
| 1029 | db_remove_reset(stmt); |
| 1030 | return ret; |
| 1031 | } |
| 1032 | |
| 1033 | int _db_ind(struct db_context *dbc, const struct osmo_cni_peer_id *vlr, |
| 1034 | unsigned int *ind, bool del) |
| 1035 | { |
| 1036 | const char *vlr_name = NULL; |
| 1037 | int rc; |
| 1038 | |
| 1039 | switch (vlr->type) { |
| 1040 | case OSMO_CNI_PEER_ID_IPA_NAME: |
| 1041 | if (vlr->ipa_name.len < 2 || vlr->ipa_name.val[vlr->ipa_name.len - 1] != '\0') { |
| 1042 | LOGP(DDB, LOGL_ERROR, "Expecting VLR ipa_name to be zero terminated; found %s\n", |
| 1043 | osmo_ipa_name_to_str(&vlr->ipa_name)); |
| 1044 | return -ENOTSUP; |
| 1045 | } |
| 1046 | vlr_name = (const char*)vlr->ipa_name.val; |
| 1047 | break; |
| 1048 | default: |
| 1049 | LOGP(DDB, LOGL_ERROR, "Unsupported osmo_cni_peer_id type: %s\n", |
| 1050 | osmo_cni_peer_id_type_name(vlr->type)); |
| 1051 | return -ENOTSUP; |
| 1052 | } |
| 1053 | |
| 1054 | if (del) |
| 1055 | return _db_ind_del(dbc, vlr_name); |
| 1056 | |
| 1057 | rc = _db_ind_get(dbc, vlr_name, ind); |
| 1058 | if (!rc) |
| 1059 | return 0; |
| 1060 | |
| 1061 | /* Does not exist yet, create. */ |
| 1062 | rc = _db_ind_add(dbc, vlr_name); |
| 1063 | if (rc) { |
| 1064 | LOGP(DDB, LOGL_ERROR, "Error creating IND entry for %s\n", osmo_quote_str_c(OTC_SELECT, vlr_name, -1)); |
| 1065 | return rc; |
| 1066 | } |
| 1067 | |
| 1068 | /* To be sure, query again from scratch. */ |
| 1069 | return _db_ind_get(dbc, vlr_name, ind); |
| 1070 | } |
| 1071 | |
| 1072 | int db_ind(struct db_context *dbc, const struct osmo_cni_peer_id *vlr, unsigned int *ind) |
| 1073 | { |
| 1074 | return _db_ind(dbc, vlr, ind, false); |
| 1075 | } |
| 1076 | |
| 1077 | int db_ind_del(struct db_context *dbc, const struct osmo_cni_peer_id *vlr) |
| 1078 | { |
| 1079 | return _db_ind(dbc, vlr, NULL, true); |
| 1080 | } |