blob: f70df83b6b3960542cf5dee59afde91efdcefab8 [file] [log] [blame]
Harald Weltee72cf552016-04-28 07:18:49 +02001#pragma once
2
Harald Weltee687be52016-05-03 18:49:27 +02003#include <stdbool.h>
Harald Weltee72cf552016-04-28 07:18:49 +02004#include <sqlite3.h>
5
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +01006#include <osmocom/gsupclient/cni_peer_id.h>
Neels Hofmeyrad868e22019-11-20 02:36:45 +01007
Neels Hofmeyr00b1d432017-10-17 01:43:48 +02008struct hlr;
9
Harald Weltee72cf552016-04-28 07:18:49 +020010enum stmt_idx {
Keith89fda302021-01-19 07:01:33 +010011 DB_STMT_SEL_ALL,
12 DB_STMT_SEL_ALL_ORDER_LAST_SEEN,
13 DB_STMT_SEL_FILTER_MSISDN,
14 DB_STMT_SEL_FILTER_IMSI,
15 DB_STMT_SEL_FILTER_CS,
16 DB_STMT_SEL_FILTER_PS,
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020017 DB_STMT_SEL_BY_IMSI,
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020018 DB_STMT_SEL_BY_MSISDN,
19 DB_STMT_SEL_BY_ID,
Oliver Smith81db3892019-01-09 12:03:51 +010020 DB_STMT_SEL_BY_IMEI,
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020021 DB_STMT_UPD_VLR_BY_ID,
22 DB_STMT_UPD_SGSN_BY_ID,
Oliver Smith81db3892019-01-09 12:03:51 +010023 DB_STMT_UPD_IMEI_BY_IMSI,
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020024 DB_STMT_AUC_BY_IMSI,
25 DB_STMT_AUC_UPD_SQN,
26 DB_STMT_UPD_PURGE_CS_BY_IMSI,
27 DB_STMT_UPD_PURGE_PS_BY_IMSI,
Neels Hofmeyre8ccd502017-10-06 04:10:06 +020028 DB_STMT_UPD_NAM_PS_BY_IMSI,
29 DB_STMT_UPD_NAM_CS_BY_IMSI,
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020030 DB_STMT_SUBSCR_CREATE,
31 DB_STMT_DEL_BY_ID,
32 DB_STMT_SET_MSISDN_BY_IMSI,
Neels Hofmeyra820ea12018-12-02 19:46:46 +010033 DB_STMT_DELETE_MSISDN_BY_IMSI,
Neels Hofmeyr1332a172017-10-10 02:25:00 +020034 DB_STMT_AUC_2G_INSERT,
35 DB_STMT_AUC_2G_DELETE,
36 DB_STMT_AUC_3G_INSERT,
37 DB_STMT_AUC_3G_DELETE,
Stefan Sperling638ba8c2018-12-04 15:07:29 +010038 DB_STMT_SET_LAST_LU_SEEN,
Neels Hofmeyr07e16022019-11-20 02:36:35 +010039 DB_STMT_SET_LAST_LU_SEEN_PS,
Oliver Smith6b73fd92019-03-06 13:49:05 +010040 DB_STMT_EXISTS_BY_IMSI,
Vadim Yanitskiyc13599d2019-03-30 17:03:42 +070041 DB_STMT_EXISTS_BY_MSISDN,
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020042 _NUM_DB_STMT
Harald Weltee72cf552016-04-28 07:18:49 +020043};
44
45struct db_context {
46 char *fname;
47 sqlite3 *db;
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020048 sqlite3_stmt *stmt[_NUM_DB_STMT];
Harald Weltee72cf552016-04-28 07:18:49 +020049};
50
Vadim Yanitskiyfbd736e2018-07-31 22:40:30 +070051/* Optional feature to make SQLite3 using talloc */
52#ifdef SQLITE_USE_TALLOC
53int db_sqlite3_use_talloc(void *ctx);
54#endif
55
Neels Hofmeyrd7d96972017-10-06 03:50:30 +020056void db_remove_reset(sqlite3_stmt *stmt);
Neels Hofmeyrf3144592017-10-06 03:40:52 +020057bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text);
Neels Hofmeyr28da26e2017-10-06 03:44:57 +020058bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr);
59bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr);
Neels Hofmeyr04c23752019-11-25 03:59:50 +010060bool db_bind_null(sqlite3_stmt *stmt, const char *param_name);
Harald Weltee72cf552016-04-28 07:18:49 +020061void db_close(struct db_context *dbc);
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +010062struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite3_logging, bool allow_upgrades);
Harald Weltee72cf552016-04-28 07:18:49 +020063
64#include <osmocom/crypt/auth.h>
65
66/* obtain the authentication data for a given imsi */
67int db_get_auth_data(struct db_context *dbc, const char *imsi,
68 struct osmo_sub_auth_data *aud2g,
69 struct osmo_sub_auth_data *aud3g,
Neels Hofmeyr32633e22017-10-06 04:26:21 +020070 int64_t *subscr_id);
Harald Weltee72cf552016-04-28 07:18:49 +020071
Neels Hofmeyr32633e22017-10-06 04:26:21 +020072int db_update_sqn(struct db_context *dbc, int64_t id,
Harald Weltee72cf552016-04-28 07:18:49 +020073 uint64_t new_sqn);
74
75int db_get_auc(struct db_context *dbc, const char *imsi,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +010076 unsigned int auc_3g_ind, struct osmo_auth_vector *vec,
77 unsigned int num_vec, const uint8_t *rand_auts,
Harald Welte06f5af22019-08-21 20:01:31 +020078 const uint8_t *auts, bool separation_bit);
Harald Weltee687be52016-05-03 18:49:27 +020079
80#include <osmocom/core/linuxlist.h>
81#include <osmocom/gsm/protocol/gsm_23_003.h>
82
83/* TODO: Get this from somewhere? */
84#define GT_MAX_DIGITS 15
85
86struct hlr_subscriber {
87 struct llist_head list;
88
Neels Hofmeyr32633e22017-10-06 04:26:21 +020089 int64_t id;
Harald Weltee687be52016-05-03 18:49:27 +020090 char imsi[GSM23003_IMSI_MAX_DIGITS+1];
Vadim Yanitskiy2e403d62019-05-25 19:12:33 +070091 char msisdn[GSM23003_MSISDN_MAX_DIGITS+1];
Harald Weltee687be52016-05-03 18:49:27 +020092 /* imeisv? */
Oliver Smith81db3892019-01-09 12:03:51 +010093 char imei[GSM23003_IMEI_NUM_DIGITS+1];
Neels Hofmeyrd6462072018-09-28 01:22:15 +020094 char vlr_number[32];
95 char sgsn_number[32];
Harald Weltee687be52016-05-03 18:49:27 +020096 char sgsn_address[GT_MAX_DIGITS+1];
97 /* ggsn number + address */
98 /* gmlc number */
99 /* smsc number */
100 uint32_t periodic_lu_timer;
101 uint32_t periodic_rau_tau_timer;
102 bool nam_cs;
103 bool nam_ps;
104 uint32_t lmsi;
105 bool ms_purged_cs;
106 bool ms_purged_ps;
Stefan Sperling5c14c9c2018-12-07 12:30:21 +0100107 time_t last_lu_seen;
Neels Hofmeyr07e16022019-11-20 02:36:35 +0100108 time_t last_lu_seen_ps;
Neels Hofmeyr04c23752019-11-25 03:59:50 +0100109 /* talloc'd IPA unit name */
110 struct osmo_ipa_name vlr_via_proxy;
111 struct osmo_ipa_name sgsn_via_proxy;
Harald Weltee687be52016-05-03 18:49:27 +0200112};
113
Stefan Sperling5c14c9c2018-12-07 12:30:21 +0100114/* A format string for use with strptime(3). This format string is
115 * used to parse the last_lu_seen column stored in the HLR database.
116 * See https://sqlite.org/lang_datefunc.html, function datetime(). */
117#define DB_LAST_LU_SEEN_FMT "%Y-%m-%d %H:%M:%S"
118
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200119/* Like struct osmo_sub_auth_data, but the keys are in hexdump representation.
120 * This is useful because SQLite requires them in hexdump format, and callers
121 * like the VTY and CTRL interface also have them available as hexdump to begin
122 * with. In the binary format, a VTY command would first need to hexparse,
123 * after which the db function would again hexdump, copying to separate
124 * buffers. The roundtrip can be saved by providing char* to begin with. */
125struct sub_auth_data_str {
126 enum osmo_sub_auth_type type;
127 enum osmo_auth_algo algo;
128 union {
129 struct {
130 const char *opc;
131 const char *k;
132 uint64_t sqn;
133 int opc_is_op;
134 unsigned int ind_bitlen;
135 } umts;
136 struct {
137 const char *ki;
138 } gsm;
139 } u;
140};
141
Oliver Smithcd2af5e2019-03-06 13:17:39 +0100142#define DB_SUBSCR_FLAG_NAM_CS (1 << 1)
143#define DB_SUBSCR_FLAG_NAM_PS (1 << 2)
144
145int db_subscr_create(struct db_context *dbc, const char *imsi, uint8_t flags);
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200146int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id);
147
148int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi,
149 const char *msisdn);
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200150int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id,
151 const struct sub_auth_data_str *aud);
Oliver Smith81db3892019-01-09 12:03:51 +0100152int db_subscr_update_imei_by_imsi(struct db_context *dbc, const char* imsi, const char *imei);
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200153
Oliver Smith6b73fd92019-03-06 13:49:05 +0100154int db_subscr_exists_by_imsi(struct db_context *dbc, const char *imsi);
Vadim Yanitskiyc13599d2019-03-30 17:03:42 +0700155int db_subscr_exists_by_msisdn(struct db_context *dbc, const char *msisdn);
Oliver Smith6b73fd92019-03-06 13:49:05 +0100156
Keith89fda302021-01-19 07:01:33 +0100157int db_subscrs_get(struct db_context *dbc, const char *filter_type, const char *filter,
158 void (*get_cb)(struct hlr_subscriber *subscr, void *data), void *data,
159 int *count, const char **err);
Neels Hofmeyr518335e2017-10-06 03:20:14 +0200160int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
161 struct hlr_subscriber *subscr);
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200162int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
163 struct hlr_subscriber *subscr);
164int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
165 struct hlr_subscriber *subscr);
Oliver Smith81db3892019-01-09 12:03:51 +0100166int db_subscr_get_by_imei(struct db_context *dbc, const char *imei, struct hlr_subscriber *subscr);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200167int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps);
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200168int db_subscr_lu(struct db_context *dbc, int64_t subscr_id,
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100169 const struct osmo_ipa_name *vlr_name, bool is_ps,
170 const struct osmo_ipa_name *via_proxy);
Harald Welteb18f0e02016-05-05 21:03:03 +0200171
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200172int db_subscr_purge(struct db_context *dbc, const char *by_imsi,
173 bool purge_val, bool is_ps);
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200174
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200175/*! Call sqlite3_column_text() and copy result to a char[].
176 * \param[out] buf A char[] used as sizeof() arg(!) and osmo_strlcpy() target.
177 * \param[in] stmt An sqlite3_stmt*.
178 * \param[in] idx Index in stmt's returned columns.
179 */
180#define copy_sqlite3_text_to_buf(buf, stmt, idx) \
181 do { \
182 const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \
183 osmo_strlcpy(buf, _txt, sizeof(buf)); \
184 } while (0)
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100185
186/*! Call sqlite3_column_text() and copy result to a struct osmo_ipa_name.
187 * \param[out] ipa_name A struct osmo_ipa_name* to write to.
188 * \param[in] stmt An sqlite3_stmt*.
189 * \param[in] idx Index in stmt's returned columns.
190 */
191#define copy_sqlite3_text_to_ipa_name(ipa_name, stmt, idx) \
192 do { \
193 const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \
194 osmo_ipa_name_set_str(ipa_name, _txt); \
195 } while (0)