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