blob: 5129b8dff4123056ab60cb4ec89bf22036977be6 [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 Hofmeyr00b1d432017-10-17 01:43:48 +02006struct hlr;
7
Harald Weltee72cf552016-04-28 07:18:49 +02008enum stmt_idx {
Neels Hofmeyr4bde9492017-10-06 03:09:34 +02009 DB_STMT_SEL_BY_IMSI,
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020010 DB_STMT_SEL_BY_MSISDN,
11 DB_STMT_SEL_BY_ID,
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020012 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 Hofmeyre8ccd502017-10-06 04:10:06 +020018 DB_STMT_UPD_NAM_PS_BY_IMSI,
19 DB_STMT_UPD_NAM_CS_BY_IMSI,
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020020 DB_STMT_SUBSCR_CREATE,
21 DB_STMT_DEL_BY_ID,
22 DB_STMT_SET_MSISDN_BY_IMSI,
Neels Hofmeyra820ea12018-12-02 19:46:46 +010023 DB_STMT_DELETE_MSISDN_BY_IMSI,
Neels Hofmeyr1332a172017-10-10 02:25:00 +020024 DB_STMT_AUC_2G_INSERT,
25 DB_STMT_AUC_2G_DELETE,
26 DB_STMT_AUC_3G_INSERT,
27 DB_STMT_AUC_3G_DELETE,
Stefan Sperling638ba8c2018-12-04 15:07:29 +010028 DB_STMT_SET_LAST_LU_SEEN,
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020029 _NUM_DB_STMT
Harald Weltee72cf552016-04-28 07:18:49 +020030};
31
32struct db_context {
33 char *fname;
34 sqlite3 *db;
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020035 sqlite3_stmt *stmt[_NUM_DB_STMT];
Harald Weltee72cf552016-04-28 07:18:49 +020036};
37
Neels Hofmeyrd7d96972017-10-06 03:50:30 +020038void db_remove_reset(sqlite3_stmt *stmt);
Neels Hofmeyrf3144592017-10-06 03:40:52 +020039bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text);
Neels Hofmeyr28da26e2017-10-06 03:44:57 +020040bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr);
41bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr);
Harald Weltee72cf552016-04-28 07:18:49 +020042void db_close(struct db_context *dbc);
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +010043struct db_context *db_open(void *ctx, const char *fname, bool enable_sqlite3_logging, bool allow_upgrades);
Harald Weltee72cf552016-04-28 07:18:49 +020044
45#include <osmocom/crypt/auth.h>
46
47/* obtain the authentication data for a given imsi */
48int db_get_auth_data(struct db_context *dbc, const char *imsi,
49 struct osmo_sub_auth_data *aud2g,
50 struct osmo_sub_auth_data *aud3g,
Neels Hofmeyr32633e22017-10-06 04:26:21 +020051 int64_t *subscr_id);
Harald Weltee72cf552016-04-28 07:18:49 +020052
Neels Hofmeyr32633e22017-10-06 04:26:21 +020053int db_update_sqn(struct db_context *dbc, int64_t id,
Harald Weltee72cf552016-04-28 07:18:49 +020054 uint64_t new_sqn);
55
56int db_get_auc(struct db_context *dbc, const char *imsi,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +010057 unsigned int auc_3g_ind, struct osmo_auth_vector *vec,
58 unsigned int num_vec, const uint8_t *rand_auts,
59 const uint8_t *auts);
Harald Weltee687be52016-05-03 18:49:27 +020060
61#include <osmocom/core/linuxlist.h>
62#include <osmocom/gsm/protocol/gsm_23_003.h>
63
64/* TODO: Get this from somewhere? */
65#define GT_MAX_DIGITS 15
66
67struct hlr_subscriber {
68 struct llist_head list;
69
Neels Hofmeyr32633e22017-10-06 04:26:21 +020070 int64_t id;
Harald Weltee687be52016-05-03 18:49:27 +020071 char imsi[GSM23003_IMSI_MAX_DIGITS+1];
72 char msisdn[GT_MAX_DIGITS+1];
73 /* imeisv? */
Neels Hofmeyrd6462072018-09-28 01:22:15 +020074 char vlr_number[32];
75 char sgsn_number[32];
Harald Weltee687be52016-05-03 18:49:27 +020076 char sgsn_address[GT_MAX_DIGITS+1];
77 /* ggsn number + address */
78 /* gmlc number */
79 /* smsc number */
80 uint32_t periodic_lu_timer;
81 uint32_t periodic_rau_tau_timer;
82 bool nam_cs;
83 bool nam_ps;
84 uint32_t lmsi;
85 bool ms_purged_cs;
86 bool ms_purged_ps;
87};
88
Neels Hofmeyr1332a172017-10-10 02:25:00 +020089/* Like struct osmo_sub_auth_data, but the keys are in hexdump representation.
90 * This is useful because SQLite requires them in hexdump format, and callers
91 * like the VTY and CTRL interface also have them available as hexdump to begin
92 * with. In the binary format, a VTY command would first need to hexparse,
93 * after which the db function would again hexdump, copying to separate
94 * buffers. The roundtrip can be saved by providing char* to begin with. */
95struct sub_auth_data_str {
96 enum osmo_sub_auth_type type;
97 enum osmo_auth_algo algo;
98 union {
99 struct {
100 const char *opc;
101 const char *k;
102 uint64_t sqn;
103 int opc_is_op;
104 unsigned int ind_bitlen;
105 } umts;
106 struct {
107 const char *ki;
108 } gsm;
109 } u;
110};
111
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200112int db_subscr_create(struct db_context *dbc, const char *imsi);
113int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id);
114
115int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi,
116 const char *msisdn);
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200117int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id,
118 const struct sub_auth_data_str *aud);
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200119
Neels Hofmeyr518335e2017-10-06 03:20:14 +0200120int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
121 struct hlr_subscriber *subscr);
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200122int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
123 struct hlr_subscriber *subscr);
124int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
125 struct hlr_subscriber *subscr);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200126int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps);
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200127int db_subscr_lu(struct db_context *dbc, int64_t subscr_id,
128 const char *vlr_or_sgsn_number, bool is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200129
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200130int db_subscr_purge(struct db_context *dbc, const char *by_imsi,
131 bool purge_val, bool is_ps);
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200132
133int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps);
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200134
135/*! Call sqlite3_column_text() and copy result to a char[].
136 * \param[out] buf A char[] used as sizeof() arg(!) and osmo_strlcpy() target.
137 * \param[in] stmt An sqlite3_stmt*.
138 * \param[in] idx Index in stmt's returned columns.
139 */
140#define copy_sqlite3_text_to_buf(buf, stmt, idx) \
141 do { \
142 const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \
143 osmo_strlcpy(buf, _txt, sizeof(buf)); \
144 } while (0)