Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 3 | #include <stdbool.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 4 | #include <sqlite3.h> |
| 5 | |
Neels Hofmeyr | 008ce4b | 2019-12-04 01:04:32 +0100 | [diff] [blame] | 6 | #include <osmocom/gsupclient/gsup_peer_id.h> |
Neels Hofmeyr | ba0b5bd | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 7 | #include <osmocom/gsm/gsup.h> |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 8 | |
Neels Hofmeyr | 00b1d43 | 2017-10-17 01:43:48 +0200 | [diff] [blame] | 9 | struct hlr; |
| 10 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 11 | enum stmt_idx { |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 12 | DB_STMT_SEL_BY_IMSI, |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 13 | DB_STMT_SEL_BY_MSISDN, |
| 14 | DB_STMT_SEL_BY_ID, |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 15 | DB_STMT_SEL_BY_IMEI, |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 16 | DB_STMT_UPD_VLR_BY_ID, |
| 17 | DB_STMT_UPD_SGSN_BY_ID, |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 18 | DB_STMT_UPD_IMEI_BY_IMSI, |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 19 | DB_STMT_AUC_BY_IMSI, |
| 20 | DB_STMT_AUC_UPD_SQN, |
| 21 | DB_STMT_UPD_PURGE_CS_BY_IMSI, |
| 22 | DB_STMT_UPD_PURGE_PS_BY_IMSI, |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 23 | DB_STMT_UPD_NAM_PS_BY_IMSI, |
| 24 | DB_STMT_UPD_NAM_CS_BY_IMSI, |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 25 | DB_STMT_SUBSCR_CREATE, |
| 26 | DB_STMT_DEL_BY_ID, |
| 27 | DB_STMT_SET_MSISDN_BY_IMSI, |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 28 | DB_STMT_DELETE_MSISDN_BY_IMSI, |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 29 | DB_STMT_AUC_2G_INSERT, |
| 30 | DB_STMT_AUC_2G_DELETE, |
| 31 | DB_STMT_AUC_3G_INSERT, |
| 32 | DB_STMT_AUC_3G_DELETE, |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 33 | DB_STMT_SET_LAST_LU_SEEN, |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 34 | DB_STMT_SET_LAST_LU_SEEN_PS, |
Oliver Smith | 6b73fd9 | 2019-03-06 13:49:05 +0100 | [diff] [blame] | 35 | DB_STMT_EXISTS_BY_IMSI, |
Vadim Yanitskiy | c13599d | 2019-03-30 17:03:42 +0700 | [diff] [blame] | 36 | DB_STMT_EXISTS_BY_MSISDN, |
Neels Hofmeyr | ba0b5bd | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 37 | DB_STMT_IND_ADD, |
| 38 | DB_STMT_IND_SELECT, |
| 39 | DB_STMT_IND_DEL, |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 40 | _NUM_DB_STMT |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | struct db_context { |
| 44 | char *fname; |
| 45 | sqlite3 *db; |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 46 | sqlite3_stmt *stmt[_NUM_DB_STMT]; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
Vadim Yanitskiy | fbd736e | 2018-07-31 22:40:30 +0700 | [diff] [blame] | 49 | /* Optional feature to make SQLite3 using talloc */ |
| 50 | #ifdef SQLITE_USE_TALLOC |
| 51 | int db_sqlite3_use_talloc(void *ctx); |
| 52 | #endif |
| 53 | |
Neels Hofmeyr | d7d9697 | 2017-10-06 03:50:30 +0200 | [diff] [blame] | 54 | void db_remove_reset(sqlite3_stmt *stmt); |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 55 | bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text); |
Neels Hofmeyr | 28da26e | 2017-10-06 03:44:57 +0200 | [diff] [blame] | 56 | bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr); |
| 57 | bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr); |
Neels Hofmeyr | f0c02ad | 2019-11-25 03:59:50 +0100 | [diff] [blame] | 58 | bool db_bind_null(sqlite3_stmt *stmt, const char *param_name); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 59 | void db_close(struct db_context *dbc); |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 60 | struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite3_logging, bool allow_upgrades); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 61 | |
| 62 | #include <osmocom/crypt/auth.h> |
| 63 | |
| 64 | /* obtain the authentication data for a given imsi */ |
| 65 | int db_get_auth_data(struct db_context *dbc, const char *imsi, |
| 66 | struct osmo_sub_auth_data *aud2g, |
| 67 | struct osmo_sub_auth_data *aud3g, |
Neels Hofmeyr | 32633e2 | 2017-10-06 04:26:21 +0200 | [diff] [blame] | 68 | int64_t *subscr_id); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 69 | |
Neels Hofmeyr | 32633e2 | 2017-10-06 04:26:21 +0200 | [diff] [blame] | 70 | int db_update_sqn(struct db_context *dbc, int64_t id, |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 71 | uint64_t new_sqn); |
| 72 | |
| 73 | int db_get_auc(struct db_context *dbc, const char *imsi, |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 74 | unsigned int auc_3g_ind, struct osmo_auth_vector *vec, |
| 75 | unsigned int num_vec, const uint8_t *rand_auts, |
Harald Welte | 06f5af2 | 2019-08-21 20:01:31 +0200 | [diff] [blame] | 76 | const uint8_t *auts, bool separation_bit); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 77 | |
| 78 | #include <osmocom/core/linuxlist.h> |
| 79 | #include <osmocom/gsm/protocol/gsm_23_003.h> |
| 80 | |
| 81 | /* TODO: Get this from somewhere? */ |
| 82 | #define GT_MAX_DIGITS 15 |
| 83 | |
| 84 | struct hlr_subscriber { |
| 85 | struct llist_head list; |
| 86 | |
Neels Hofmeyr | 32633e2 | 2017-10-06 04:26:21 +0200 | [diff] [blame] | 87 | int64_t id; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 88 | char imsi[GSM23003_IMSI_MAX_DIGITS+1]; |
Vadim Yanitskiy | 2e403d6 | 2019-05-25 19:12:33 +0700 | [diff] [blame] | 89 | char msisdn[GSM23003_MSISDN_MAX_DIGITS+1]; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 90 | /* imeisv? */ |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 91 | char imei[GSM23003_IMEI_NUM_DIGITS+1]; |
Neels Hofmeyr | d646207 | 2018-09-28 01:22:15 +0200 | [diff] [blame] | 92 | char vlr_number[32]; |
| 93 | char sgsn_number[32]; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 94 | char sgsn_address[GT_MAX_DIGITS+1]; |
| 95 | /* ggsn number + address */ |
| 96 | /* gmlc number */ |
| 97 | /* smsc number */ |
| 98 | uint32_t periodic_lu_timer; |
| 99 | uint32_t periodic_rau_tau_timer; |
| 100 | bool nam_cs; |
| 101 | bool nam_ps; |
| 102 | uint32_t lmsi; |
| 103 | bool ms_purged_cs; |
| 104 | bool ms_purged_ps; |
Stefan Sperling | 5c14c9c | 2018-12-07 12:30:21 +0100 | [diff] [blame] | 105 | time_t last_lu_seen; |
Neels Hofmeyr | 07e1602 | 2019-11-20 02:36:35 +0100 | [diff] [blame] | 106 | time_t last_lu_seen_ps; |
Neels Hofmeyr | f0c02ad | 2019-11-25 03:59:50 +0100 | [diff] [blame] | 107 | /* talloc'd IPA unit name */ |
| 108 | struct osmo_ipa_name vlr_via_proxy; |
| 109 | struct osmo_ipa_name sgsn_via_proxy; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
Stefan Sperling | 5c14c9c | 2018-12-07 12:30:21 +0100 | [diff] [blame] | 112 | /* A format string for use with strptime(3). This format string is |
| 113 | * used to parse the last_lu_seen column stored in the HLR database. |
| 114 | * See https://sqlite.org/lang_datefunc.html, function datetime(). */ |
| 115 | #define DB_LAST_LU_SEEN_FMT "%Y-%m-%d %H:%M:%S" |
| 116 | |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 117 | /* Like struct osmo_sub_auth_data, but the keys are in hexdump representation. |
| 118 | * This is useful because SQLite requires them in hexdump format, and callers |
| 119 | * like the VTY and CTRL interface also have them available as hexdump to begin |
| 120 | * with. In the binary format, a VTY command would first need to hexparse, |
| 121 | * after which the db function would again hexdump, copying to separate |
| 122 | * buffers. The roundtrip can be saved by providing char* to begin with. */ |
| 123 | struct sub_auth_data_str { |
| 124 | enum osmo_sub_auth_type type; |
| 125 | enum osmo_auth_algo algo; |
| 126 | union { |
| 127 | struct { |
| 128 | const char *opc; |
| 129 | const char *k; |
| 130 | uint64_t sqn; |
| 131 | int opc_is_op; |
| 132 | unsigned int ind_bitlen; |
| 133 | } umts; |
| 134 | struct { |
| 135 | const char *ki; |
| 136 | } gsm; |
| 137 | } u; |
| 138 | }; |
| 139 | |
Oliver Smith | cd2af5e | 2019-03-06 13:17:39 +0100 | [diff] [blame] | 140 | #define DB_SUBSCR_FLAG_NAM_CS (1 << 1) |
| 141 | #define DB_SUBSCR_FLAG_NAM_PS (1 << 2) |
| 142 | |
| 143 | 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] | 144 | int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id); |
| 145 | |
| 146 | int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi, |
| 147 | const char *msisdn); |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 148 | int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id, |
| 149 | const struct sub_auth_data_str *aud); |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 150 | int db_subscr_update_imei_by_imsi(struct db_context *dbc, const char* imsi, const char *imei); |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 151 | |
Oliver Smith | 6b73fd9 | 2019-03-06 13:49:05 +0100 | [diff] [blame] | 152 | int db_subscr_exists_by_imsi(struct db_context *dbc, const char *imsi); |
Vadim Yanitskiy | c13599d | 2019-03-30 17:03:42 +0700 | [diff] [blame] | 153 | int db_subscr_exists_by_msisdn(struct db_context *dbc, const char *msisdn); |
Oliver Smith | 6b73fd9 | 2019-03-06 13:49:05 +0100 | [diff] [blame] | 154 | |
Neels Hofmeyr | 518335e | 2017-10-06 03:20:14 +0200 | [diff] [blame] | 155 | int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi, |
| 156 | struct hlr_subscriber *subscr); |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 157 | int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn, |
| 158 | struct hlr_subscriber *subscr); |
| 159 | int db_subscr_get_by_id(struct db_context *dbc, int64_t id, |
| 160 | struct hlr_subscriber *subscr); |
Oliver Smith | 81db389 | 2019-01-09 12:03:51 +0100 | [diff] [blame] | 161 | int db_subscr_get_by_imei(struct db_context *dbc, const char *imei, struct hlr_subscriber *subscr); |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 162 | int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps); |
Neels Hofmeyr | dd78305 | 2017-10-09 17:36:08 +0200 | [diff] [blame] | 163 | int db_subscr_lu(struct db_context *dbc, int64_t subscr_id, |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 164 | const struct osmo_ipa_name *vlr_name, bool is_ps, |
| 165 | const struct osmo_ipa_name *via_proxy); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 166 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 167 | int db_subscr_purge(struct db_context *dbc, const char *by_imsi, |
| 168 | bool purge_val, bool is_ps); |
Neels Hofmeyr | 00b1d43 | 2017-10-17 01:43:48 +0200 | [diff] [blame] | 169 | |
Neels Hofmeyr | ba0b5bd | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 170 | int db_ind(struct db_context *dbc, const struct osmo_gsup_peer_id *vlr, unsigned int *ind); |
| 171 | int db_ind_del(struct db_context *dbc, const struct osmo_gsup_peer_id *vlr); |
| 172 | |
Neels Hofmeyr | 73d14af | 2017-10-24 23:26:53 +0200 | [diff] [blame] | 173 | /*! Call sqlite3_column_text() and copy result to a char[]. |
| 174 | * \param[out] buf A char[] used as sizeof() arg(!) and osmo_strlcpy() target. |
| 175 | * \param[in] stmt An sqlite3_stmt*. |
| 176 | * \param[in] idx Index in stmt's returned columns. |
| 177 | */ |
| 178 | #define copy_sqlite3_text_to_buf(buf, stmt, idx) \ |
| 179 | do { \ |
| 180 | const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \ |
| 181 | osmo_strlcpy(buf, _txt, sizeof(buf)); \ |
| 182 | } while (0) |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 183 | |
| 184 | /*! Call sqlite3_column_text() and copy result to a struct osmo_ipa_name. |
| 185 | * \param[out] ipa_name A struct osmo_ipa_name* to write to. |
| 186 | * \param[in] stmt An sqlite3_stmt*. |
| 187 | * \param[in] idx Index in stmt's returned columns. |
| 188 | */ |
| 189 | #define copy_sqlite3_text_to_ipa_name(ipa_name, stmt, idx) \ |
| 190 | do { \ |
| 191 | const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \ |
| 192 | osmo_ipa_name_set_str(ipa_name, _txt); \ |
| 193 | } while (0) |