blob: 6fd33ee0e55b9a8759aef9792ae83a6586f42831 [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 Hofmeyr2fb33f12018-12-26 01:49:53 +01006#include <osmocom/gsm/gsm_utils.h>
7
Neels Hofmeyr00b1d432017-10-17 01:43:48 +02008struct hlr;
9
Harald Weltee72cf552016-04-28 07:18:49 +020010enum stmt_idx {
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020011 DB_STMT_SEL_BY_IMSI,
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020012 DB_STMT_SEL_BY_MSISDN,
13 DB_STMT_SEL_BY_ID,
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020014 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 Hofmeyre8ccd502017-10-06 04:10:06 +020020 DB_STMT_UPD_NAM_PS_BY_IMSI,
21 DB_STMT_UPD_NAM_CS_BY_IMSI,
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020022 DB_STMT_SUBSCR_CREATE,
23 DB_STMT_DEL_BY_ID,
24 DB_STMT_SET_MSISDN_BY_IMSI,
Neels Hofmeyra820ea12018-12-02 19:46:46 +010025 DB_STMT_DELETE_MSISDN_BY_IMSI,
Neels Hofmeyr1332a172017-10-10 02:25:00 +020026 DB_STMT_AUC_2G_INSERT,
27 DB_STMT_AUC_2G_DELETE,
28 DB_STMT_AUC_3G_INSERT,
29 DB_STMT_AUC_3G_DELETE,
Stefan Sperling638ba8c2018-12-04 15:07:29 +010030 DB_STMT_SET_LAST_LU_SEEN,
Neels Hofmeyr2fb33f12018-12-26 01:49:53 +010031 DB_STMT_UPD_RAT_FLAG,
32 DB_STMT_RAT_BY_ID,
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020033 _NUM_DB_STMT
Harald Weltee72cf552016-04-28 07:18:49 +020034};
35
36struct db_context {
37 char *fname;
38 sqlite3 *db;
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020039 sqlite3_stmt *stmt[_NUM_DB_STMT];
Harald Weltee72cf552016-04-28 07:18:49 +020040};
41
Neels Hofmeyrd7d96972017-10-06 03:50:30 +020042void db_remove_reset(sqlite3_stmt *stmt);
Neels Hofmeyrf3144592017-10-06 03:40:52 +020043bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text);
Neels Hofmeyr28da26e2017-10-06 03:44:57 +020044bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr);
45bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr);
Harald Weltee72cf552016-04-28 07:18:49 +020046void db_close(struct db_context *dbc);
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +010047struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite3_logging, bool allow_upgrades);
Harald Weltee72cf552016-04-28 07:18:49 +020048
49#include <osmocom/crypt/auth.h>
50
51/* obtain the authentication data for a given imsi */
52int 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 Hofmeyr32633e22017-10-06 04:26:21 +020055 int64_t *subscr_id);
Harald Weltee72cf552016-04-28 07:18:49 +020056
Neels Hofmeyr32633e22017-10-06 04:26:21 +020057int db_update_sqn(struct db_context *dbc, int64_t id,
Harald Weltee72cf552016-04-28 07:18:49 +020058 uint64_t new_sqn);
59
60int db_get_auc(struct db_context *dbc, const char *imsi,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +010061 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 Weltee687be52016-05-03 18:49:27 +020064
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
71struct hlr_subscriber {
72 struct llist_head list;
73
Neels Hofmeyr32633e22017-10-06 04:26:21 +020074 int64_t id;
Harald Weltee687be52016-05-03 18:49:27 +020075 char imsi[GSM23003_IMSI_MAX_DIGITS+1];
76 char msisdn[GT_MAX_DIGITS+1];
77 /* imeisv? */
Neels Hofmeyrd6462072018-09-28 01:22:15 +020078 char vlr_number[32];
79 char sgsn_number[32];
Harald Weltee687be52016-05-03 18:49:27 +020080 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 Sperling5c14c9c2018-12-07 12:30:21 +010091 time_t last_lu_seen;
Neels Hofmeyr2fb33f12018-12-26 01:49:53 +010092 bool rat_types[OSMO_RAT_COUNT];
Harald Weltee687be52016-05-03 18:49:27 +020093};
94
Neels Hofmeyr2fb33f12018-12-26 01:49:53 +010095static const struct hlr_subscriber hlr_subscriber_empty = {
96 .rat_types = { true, true, true },
97 };
98
Stefan Sperling5c14c9c2018-12-07 12:30:21 +010099/* A format string for use with strptime(3). This format string is
100 * used to parse the last_lu_seen column stored in the HLR database.
101 * See https://sqlite.org/lang_datefunc.html, function datetime(). */
102#define DB_LAST_LU_SEEN_FMT "%Y-%m-%d %H:%M:%S"
103
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200104/* Like struct osmo_sub_auth_data, but the keys are in hexdump representation.
105 * This is useful because SQLite requires them in hexdump format, and callers
106 * like the VTY and CTRL interface also have them available as hexdump to begin
107 * with. In the binary format, a VTY command would first need to hexparse,
108 * after which the db function would again hexdump, copying to separate
109 * buffers. The roundtrip can be saved by providing char* to begin with. */
110struct sub_auth_data_str {
111 enum osmo_sub_auth_type type;
112 enum osmo_auth_algo algo;
113 union {
114 struct {
115 const char *opc;
116 const char *k;
117 uint64_t sqn;
118 int opc_is_op;
119 unsigned int ind_bitlen;
120 } umts;
121 struct {
122 const char *ki;
123 } gsm;
124 } u;
125};
126
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200127int db_subscr_create(struct db_context *dbc, const char *imsi);
128int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id);
129
130int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi,
131 const char *msisdn);
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200132int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id,
133 const struct sub_auth_data_str *aud);
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200134
Neels Hofmeyr518335e2017-10-06 03:20:14 +0200135int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
136 struct hlr_subscriber *subscr);
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200137int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
138 struct hlr_subscriber *subscr);
139int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
140 struct hlr_subscriber *subscr);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200141int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps);
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200142int db_subscr_lu(struct db_context *dbc, int64_t subscr_id,
143 const char *vlr_or_sgsn_number, bool is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200144
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200145int db_subscr_purge(struct db_context *dbc, const char *by_imsi,
146 bool purge_val, bool is_ps);
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200147
148int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps);
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200149
Neels Hofmeyr2fb33f12018-12-26 01:49:53 +0100150int db_subscr_set_rat_type_flag(struct db_context *dbc, int64_t subscr_id, enum osmo_rat_type rat, bool allowed);
151int db_subscr_get_rat_types(struct db_context *dbc, struct hlr_subscriber *subscr);
152int hlr_subscr_rat_flag(struct hlr *hlr, struct hlr_subscriber *subscr, enum osmo_rat_type rat, bool allowed);
153
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200154/*! Call sqlite3_column_text() and copy result to a char[].
155 * \param[out] buf A char[] used as sizeof() arg(!) and osmo_strlcpy() target.
156 * \param[in] stmt An sqlite3_stmt*.
157 * \param[in] idx Index in stmt's returned columns.
158 */
159#define copy_sqlite3_text_to_buf(buf, stmt, idx) \
160 do { \
161 const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \
162 osmo_strlcpy(buf, _txt, sizeof(buf)); \
163 } while (0)