blob: 35e432742097dcb34001245e812818a815ec61df [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 Hofmeyr1332a172017-10-10 02:25:00 +020023 DB_STMT_AUC_2G_INSERT,
24 DB_STMT_AUC_2G_DELETE,
25 DB_STMT_AUC_3G_INSERT,
26 DB_STMT_AUC_3G_DELETE,
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020027 _NUM_DB_STMT
Harald Weltee72cf552016-04-28 07:18:49 +020028};
29
30struct db_context {
31 char *fname;
32 sqlite3 *db;
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020033 sqlite3_stmt *stmt[_NUM_DB_STMT];
Harald Weltee72cf552016-04-28 07:18:49 +020034};
35
Neels Hofmeyrd7d96972017-10-06 03:50:30 +020036void db_remove_reset(sqlite3_stmt *stmt);
Neels Hofmeyrf3144592017-10-06 03:40:52 +020037bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text);
Neels Hofmeyr28da26e2017-10-06 03:44:57 +020038bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr);
39bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr);
Harald Weltee72cf552016-04-28 07:18:49 +020040void db_close(struct db_context *dbc);
41struct db_context *db_open(void *ctx, const char *fname);
42
43#include <osmocom/crypt/auth.h>
44
45/* obtain the authentication data for a given imsi */
46int 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 Hofmeyr32633e22017-10-06 04:26:21 +020049 int64_t *subscr_id);
Harald Weltee72cf552016-04-28 07:18:49 +020050
Neels Hofmeyr32633e22017-10-06 04:26:21 +020051int db_update_sqn(struct db_context *dbc, int64_t id,
Harald Weltee72cf552016-04-28 07:18:49 +020052 uint64_t new_sqn);
53
54int db_get_auc(struct db_context *dbc, const char *imsi,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +010055 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 Weltee687be52016-05-03 18:49:27 +020058
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
65struct hlr_subscriber {
66 struct llist_head list;
67
Neels Hofmeyr32633e22017-10-06 04:26:21 +020068 int64_t id;
Harald Weltee687be52016-05-03 18:49:27 +020069 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 Hofmeyr1332a172017-10-10 02:25:00 +020087/* 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. */
93struct 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 Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200110int db_subscr_create(struct db_context *dbc, const char *imsi);
111int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id);
112
113int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi,
114 const char *msisdn);
Neels Hofmeyr1332a172017-10-10 02:25:00 +0200115int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id,
116 const struct sub_auth_data_str *aud);
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +0200117
Neels Hofmeyr518335e2017-10-06 03:20:14 +0200118int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
119 struct hlr_subscriber *subscr);
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +0200120int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
121 struct hlr_subscriber *subscr);
122int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
123 struct hlr_subscriber *subscr);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +0200124int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps);
Neels Hofmeyrdd783052017-10-09 17:36:08 +0200125int db_subscr_lu(struct db_context *dbc, int64_t subscr_id,
126 const char *vlr_or_sgsn_number, bool is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200127
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200128int db_subscr_purge(struct db_context *dbc, const char *by_imsi,
129 bool purge_val, bool is_ps);
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200130
131int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps);