blob: f6aaa589a11217b6a70597237a64121be9f59ac2 [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
6enum stmt_idx {
Neels Hofmeyr4bde9492017-10-06 03:09:34 +02007 DB_STMT_SEL_BY_IMSI,
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +02008 DB_STMT_SEL_BY_MSISDN,
9 DB_STMT_SEL_BY_ID,
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020010 DB_STMT_UPD_VLR_BY_ID,
11 DB_STMT_UPD_SGSN_BY_ID,
12 DB_STMT_AUC_BY_IMSI,
13 DB_STMT_AUC_UPD_SQN,
14 DB_STMT_UPD_PURGE_CS_BY_IMSI,
15 DB_STMT_UPD_PURGE_PS_BY_IMSI,
Neels Hofmeyre8ccd502017-10-06 04:10:06 +020016 DB_STMT_UPD_NAM_PS_BY_IMSI,
17 DB_STMT_UPD_NAM_CS_BY_IMSI,
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020018 DB_STMT_SUBSCR_CREATE,
19 DB_STMT_DEL_BY_ID,
20 DB_STMT_SET_MSISDN_BY_IMSI,
Neels Hofmeyr1332a172017-10-10 02:25:00 +020021 DB_STMT_AUC_2G_INSERT,
22 DB_STMT_AUC_2G_DELETE,
23 DB_STMT_AUC_3G_INSERT,
24 DB_STMT_AUC_3G_DELETE,
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020025 _NUM_DB_STMT
Harald Weltee72cf552016-04-28 07:18:49 +020026};
27
28struct db_context {
29 char *fname;
30 sqlite3 *db;
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020031 sqlite3_stmt *stmt[_NUM_DB_STMT];
Harald Weltee72cf552016-04-28 07:18:49 +020032};
33
Neels Hofmeyrd7d96972017-10-06 03:50:30 +020034void db_remove_reset(sqlite3_stmt *stmt);
Neels Hofmeyrf3144592017-10-06 03:40:52 +020035bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text);
Neels Hofmeyr28da26e2017-10-06 03:44:57 +020036bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr);
37bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr);
Harald Weltee72cf552016-04-28 07:18:49 +020038void db_close(struct db_context *dbc);
39struct db_context *db_open(void *ctx, const char *fname);
40
41#include <osmocom/crypt/auth.h>
42
43/* obtain the authentication data for a given imsi */
44int db_get_auth_data(struct db_context *dbc, const char *imsi,
45 struct osmo_sub_auth_data *aud2g,
46 struct osmo_sub_auth_data *aud3g,
Neels Hofmeyr32633e22017-10-06 04:26:21 +020047 int64_t *subscr_id);
Harald Weltee72cf552016-04-28 07:18:49 +020048
Neels Hofmeyr32633e22017-10-06 04:26:21 +020049int db_update_sqn(struct db_context *dbc, int64_t id,
Harald Weltee72cf552016-04-28 07:18:49 +020050 uint64_t new_sqn);
51
52int db_get_auc(struct db_context *dbc, const char *imsi,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +010053 unsigned int auc_3g_ind, struct osmo_auth_vector *vec,
54 unsigned int num_vec, const uint8_t *rand_auts,
55 const uint8_t *auts);
Harald Weltee687be52016-05-03 18:49:27 +020056
57#include <osmocom/core/linuxlist.h>
58#include <osmocom/gsm/protocol/gsm_23_003.h>
59
60/* TODO: Get this from somewhere? */
61#define GT_MAX_DIGITS 15
62
63struct hlr_subscriber {
64 struct llist_head list;
65
Neels Hofmeyr32633e22017-10-06 04:26:21 +020066 int64_t id;
Harald Weltee687be52016-05-03 18:49:27 +020067 char imsi[GSM23003_IMSI_MAX_DIGITS+1];
68 char msisdn[GT_MAX_DIGITS+1];
69 /* imeisv? */
70 char vlr_number[GT_MAX_DIGITS+1];
71 char sgsn_number[GT_MAX_DIGITS+1];
72 char sgsn_address[GT_MAX_DIGITS+1];
73 /* ggsn number + address */
74 /* gmlc number */
75 /* smsc number */
76 uint32_t periodic_lu_timer;
77 uint32_t periodic_rau_tau_timer;
78 bool nam_cs;
79 bool nam_ps;
80 uint32_t lmsi;
81 bool ms_purged_cs;
82 bool ms_purged_ps;
83};
84
Neels Hofmeyr1332a172017-10-10 02:25:00 +020085/* Like struct osmo_sub_auth_data, but the keys are in hexdump representation.
86 * This is useful because SQLite requires them in hexdump format, and callers
87 * like the VTY and CTRL interface also have them available as hexdump to begin
88 * with. In the binary format, a VTY command would first need to hexparse,
89 * after which the db function would again hexdump, copying to separate
90 * buffers. The roundtrip can be saved by providing char* to begin with. */
91struct sub_auth_data_str {
92 enum osmo_sub_auth_type type;
93 enum osmo_auth_algo algo;
94 union {
95 struct {
96 const char *opc;
97 const char *k;
98 uint64_t sqn;
99 int opc_is_op;
100 unsigned int ind_bitlen;
101 } umts;
102 struct {
103 const char *ki;
104 } gsm;
105 } u;
106};
107
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200108int db_subscr_create(struct db_context *dbc, const char *imsi);
109int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id);
110
111int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi,
112 const char *msisdn);
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200113int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id,
114 const struct sub_auth_data_str *aud);
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200115
Neels Hofmeyr518335e2017-10-06 03:20:14 +0200116int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
117 struct hlr_subscriber *subscr);
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200118int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
119 struct hlr_subscriber *subscr);
120int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
121 struct hlr_subscriber *subscr);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200122int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps);
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200123int db_subscr_lu(struct db_context *dbc, int64_t subscr_id,
124 const char *vlr_or_sgsn_number, bool is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200125
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200126int db_subscr_purge(struct db_context *dbc, const char *by_imsi,
127 bool purge_val, bool is_ps);