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 | 2fb33f1 | 2018-12-26 01:49:53 +0100 | [diff] [blame] | 6 | #include <osmocom/gsm/gsm_utils.h> |
| 7 | |
Neels Hofmeyr | 00b1d43 | 2017-10-17 01:43:48 +0200 | [diff] [blame] | 8 | struct hlr; |
| 9 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 10 | enum stmt_idx { |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 11 | DB_STMT_SEL_BY_IMSI, |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 12 | DB_STMT_SEL_BY_MSISDN, |
| 13 | DB_STMT_SEL_BY_ID, |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 14 | DB_STMT_UPD_VLR_BY_ID, |
| 15 | DB_STMT_UPD_SGSN_BY_ID, |
| 16 | DB_STMT_AUC_BY_IMSI, |
| 17 | DB_STMT_AUC_UPD_SQN, |
| 18 | DB_STMT_UPD_PURGE_CS_BY_IMSI, |
| 19 | DB_STMT_UPD_PURGE_PS_BY_IMSI, |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 20 | DB_STMT_UPD_NAM_PS_BY_IMSI, |
| 21 | DB_STMT_UPD_NAM_CS_BY_IMSI, |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 22 | DB_STMT_SUBSCR_CREATE, |
| 23 | DB_STMT_DEL_BY_ID, |
| 24 | DB_STMT_SET_MSISDN_BY_IMSI, |
Neels Hofmeyr | a820ea1 | 2018-12-02 19:46:46 +0100 | [diff] [blame] | 25 | DB_STMT_DELETE_MSISDN_BY_IMSI, |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 26 | DB_STMT_AUC_2G_INSERT, |
| 27 | DB_STMT_AUC_2G_DELETE, |
| 28 | DB_STMT_AUC_3G_INSERT, |
| 29 | DB_STMT_AUC_3G_DELETE, |
Stefan Sperling | 638ba8c | 2018-12-04 15:07:29 +0100 | [diff] [blame] | 30 | DB_STMT_SET_LAST_LU_SEEN, |
Neels Hofmeyr | 2fb33f1 | 2018-12-26 01:49:53 +0100 | [diff] [blame] | 31 | DB_STMT_UPD_RAT_FLAG, |
| 32 | DB_STMT_RAT_BY_ID, |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 33 | _NUM_DB_STMT |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | struct db_context { |
| 37 | char *fname; |
| 38 | sqlite3 *db; |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 39 | sqlite3_stmt *stmt[_NUM_DB_STMT]; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
Neels Hofmeyr | d7d9697 | 2017-10-06 03:50:30 +0200 | [diff] [blame] | 42 | void db_remove_reset(sqlite3_stmt *stmt); |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 43 | 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] | 44 | bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr); |
| 45 | bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 46 | void db_close(struct db_context *dbc); |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 47 | 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] | 48 | |
| 49 | #include <osmocom/crypt/auth.h> |
| 50 | |
| 51 | /* obtain the authentication data for a given imsi */ |
| 52 | int db_get_auth_data(struct db_context *dbc, const char *imsi, |
| 53 | struct osmo_sub_auth_data *aud2g, |
| 54 | struct osmo_sub_auth_data *aud3g, |
Neels Hofmeyr | 32633e2 | 2017-10-06 04:26:21 +0200 | [diff] [blame] | 55 | int64_t *subscr_id); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 56 | |
Neels Hofmeyr | 32633e2 | 2017-10-06 04:26:21 +0200 | [diff] [blame] | 57 | int db_update_sqn(struct db_context *dbc, int64_t id, |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 58 | uint64_t new_sqn); |
| 59 | |
| 60 | int db_get_auc(struct db_context *dbc, const char *imsi, |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 61 | unsigned int auc_3g_ind, struct osmo_auth_vector *vec, |
| 62 | unsigned int num_vec, const uint8_t *rand_auts, |
| 63 | const uint8_t *auts); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 64 | |
| 65 | #include <osmocom/core/linuxlist.h> |
| 66 | #include <osmocom/gsm/protocol/gsm_23_003.h> |
| 67 | |
| 68 | /* TODO: Get this from somewhere? */ |
| 69 | #define GT_MAX_DIGITS 15 |
| 70 | |
| 71 | struct hlr_subscriber { |
| 72 | struct llist_head list; |
| 73 | |
Neels Hofmeyr | 32633e2 | 2017-10-06 04:26:21 +0200 | [diff] [blame] | 74 | int64_t id; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 75 | char imsi[GSM23003_IMSI_MAX_DIGITS+1]; |
| 76 | char msisdn[GT_MAX_DIGITS+1]; |
| 77 | /* imeisv? */ |
Neels Hofmeyr | d646207 | 2018-09-28 01:22:15 +0200 | [diff] [blame] | 78 | char vlr_number[32]; |
| 79 | char sgsn_number[32]; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 80 | char sgsn_address[GT_MAX_DIGITS+1]; |
| 81 | /* ggsn number + address */ |
| 82 | /* gmlc number */ |
| 83 | /* smsc number */ |
| 84 | uint32_t periodic_lu_timer; |
| 85 | uint32_t periodic_rau_tau_timer; |
| 86 | bool nam_cs; |
| 87 | bool nam_ps; |
| 88 | uint32_t lmsi; |
| 89 | bool ms_purged_cs; |
| 90 | bool ms_purged_ps; |
Stefan Sperling | 5c14c9c | 2018-12-07 12:30:21 +0100 | [diff] [blame] | 91 | time_t last_lu_seen; |
Neels Hofmeyr | 0f9daa6 | 2018-12-29 03:28:38 +0100 | [diff] [blame] | 92 | char last_lu_rat[128]; |
Neels Hofmeyr | 2fb33f1 | 2018-12-26 01:49:53 +0100 | [diff] [blame] | 93 | bool rat_types[OSMO_RAT_COUNT]; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
Neels Hofmeyr | 2fb33f1 | 2018-12-26 01:49:53 +0100 | [diff] [blame] | 96 | static const struct hlr_subscriber hlr_subscriber_empty = { |
| 97 | .rat_types = { true, true, true }, |
| 98 | }; |
| 99 | |
Stefan Sperling | 5c14c9c | 2018-12-07 12:30:21 +0100 | [diff] [blame] | 100 | /* A format string for use with strptime(3). This format string is |
| 101 | * used to parse the last_lu_seen column stored in the HLR database. |
| 102 | * See https://sqlite.org/lang_datefunc.html, function datetime(). */ |
| 103 | #define DB_LAST_LU_SEEN_FMT "%Y-%m-%d %H:%M:%S" |
| 104 | |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 105 | /* Like struct osmo_sub_auth_data, but the keys are in hexdump representation. |
| 106 | * This is useful because SQLite requires them in hexdump format, and callers |
| 107 | * like the VTY and CTRL interface also have them available as hexdump to begin |
| 108 | * with. In the binary format, a VTY command would first need to hexparse, |
| 109 | * after which the db function would again hexdump, copying to separate |
| 110 | * buffers. The roundtrip can be saved by providing char* to begin with. */ |
| 111 | struct sub_auth_data_str { |
| 112 | enum osmo_sub_auth_type type; |
| 113 | enum osmo_auth_algo algo; |
| 114 | union { |
| 115 | struct { |
| 116 | const char *opc; |
| 117 | const char *k; |
| 118 | uint64_t sqn; |
| 119 | int opc_is_op; |
| 120 | unsigned int ind_bitlen; |
| 121 | } umts; |
| 122 | struct { |
| 123 | const char *ki; |
| 124 | } gsm; |
| 125 | } u; |
| 126 | }; |
| 127 | |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 128 | int db_subscr_create(struct db_context *dbc, const char *imsi); |
| 129 | int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id); |
| 130 | |
| 131 | int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi, |
| 132 | const char *msisdn); |
Neels Hofmeyr | 1332a17 | 2017-10-10 02:25:00 +0200 | [diff] [blame] | 133 | int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id, |
| 134 | const struct sub_auth_data_str *aud); |
Neels Hofmeyr | f7c3e6e | 2017-10-09 17:55:16 +0200 | [diff] [blame] | 135 | |
Neels Hofmeyr | 518335e | 2017-10-06 03:20:14 +0200 | [diff] [blame] | 136 | int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi, |
| 137 | struct hlr_subscriber *subscr); |
Neels Hofmeyr | 9c2bbc8 | 2017-10-09 17:30:32 +0200 | [diff] [blame] | 138 | int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn, |
| 139 | struct hlr_subscriber *subscr); |
| 140 | int db_subscr_get_by_id(struct db_context *dbc, int64_t id, |
| 141 | struct hlr_subscriber *subscr); |
Neels Hofmeyr | e8ccd50 | 2017-10-06 04:10:06 +0200 | [diff] [blame] | 142 | 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] | 143 | int db_subscr_lu(struct db_context *dbc, int64_t subscr_id, |
Neels Hofmeyr | 0f9daa6 | 2018-12-29 03:28:38 +0100 | [diff] [blame] | 144 | const char *vlr_or_sgsn_number, bool is_ps, |
| 145 | const enum osmo_rat_type rat_types[], size_t rat_types_len); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 146 | |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 147 | int db_subscr_purge(struct db_context *dbc, const char *by_imsi, |
| 148 | bool purge_val, bool is_ps); |
Neels Hofmeyr | 00b1d43 | 2017-10-17 01:43:48 +0200 | [diff] [blame] | 149 | |
| 150 | int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps); |
Neels Hofmeyr | 73d14af | 2017-10-24 23:26:53 +0200 | [diff] [blame] | 151 | |
Neels Hofmeyr | 2fb33f1 | 2018-12-26 01:49:53 +0100 | [diff] [blame] | 152 | int db_subscr_set_rat_type_flag(struct db_context *dbc, int64_t subscr_id, enum osmo_rat_type rat, bool allowed); |
| 153 | int db_subscr_get_rat_types(struct db_context *dbc, struct hlr_subscriber *subscr); |
| 154 | int hlr_subscr_rat_flag(struct hlr *hlr, struct hlr_subscriber *subscr, enum osmo_rat_type rat, bool allowed); |
| 155 | |
Neels Hofmeyr | 73d14af | 2017-10-24 23:26:53 +0200 | [diff] [blame] | 156 | /*! Call sqlite3_column_text() and copy result to a char[]. |
| 157 | * \param[out] buf A char[] used as sizeof() arg(!) and osmo_strlcpy() target. |
| 158 | * \param[in] stmt An sqlite3_stmt*. |
| 159 | * \param[in] idx Index in stmt's returned columns. |
| 160 | */ |
| 161 | #define copy_sqlite3_text_to_buf(buf, stmt, idx) \ |
| 162 | do { \ |
| 163 | const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \ |
| 164 | osmo_strlcpy(buf, _txt, sizeof(buf)); \ |
| 165 | } while (0) |