blob: 9d4274d436b2c5f4444e54d06d99849fb7e77b93 [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 Hofmeyr4bde9492017-10-06 03:09:34 +020021 _NUM_DB_STMT
Harald Weltee72cf552016-04-28 07:18:49 +020022};
23
24struct db_context {
25 char *fname;
26 sqlite3 *db;
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020027 sqlite3_stmt *stmt[_NUM_DB_STMT];
Harald Weltee72cf552016-04-28 07:18:49 +020028};
29
Neels Hofmeyrd7d96972017-10-06 03:50:30 +020030void db_remove_reset(sqlite3_stmt *stmt);
Neels Hofmeyrf3144592017-10-06 03:40:52 +020031bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text);
Neels Hofmeyr28da26e2017-10-06 03:44:57 +020032bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr);
33bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr);
Harald Weltee72cf552016-04-28 07:18:49 +020034void db_close(struct db_context *dbc);
35struct db_context *db_open(void *ctx, const char *fname);
36
37#include <osmocom/crypt/auth.h>
38
39/* obtain the authentication data for a given imsi */
40int db_get_auth_data(struct db_context *dbc, const char *imsi,
41 struct osmo_sub_auth_data *aud2g,
42 struct osmo_sub_auth_data *aud3g,
Neels Hofmeyr32633e22017-10-06 04:26:21 +020043 int64_t *subscr_id);
Harald Weltee72cf552016-04-28 07:18:49 +020044
Neels Hofmeyr32633e22017-10-06 04:26:21 +020045int db_update_sqn(struct db_context *dbc, int64_t id,
Harald Weltee72cf552016-04-28 07:18:49 +020046 uint64_t new_sqn);
47
48int db_get_auc(struct db_context *dbc, const char *imsi,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +010049 unsigned int auc_3g_ind, struct osmo_auth_vector *vec,
50 unsigned int num_vec, const uint8_t *rand_auts,
51 const uint8_t *auts);
Harald Weltee687be52016-05-03 18:49:27 +020052
53#include <osmocom/core/linuxlist.h>
54#include <osmocom/gsm/protocol/gsm_23_003.h>
55
56/* TODO: Get this from somewhere? */
57#define GT_MAX_DIGITS 15
58
59struct hlr_subscriber {
60 struct llist_head list;
61
Neels Hofmeyr32633e22017-10-06 04:26:21 +020062 int64_t id;
Harald Weltee687be52016-05-03 18:49:27 +020063 char imsi[GSM23003_IMSI_MAX_DIGITS+1];
64 char msisdn[GT_MAX_DIGITS+1];
65 /* imeisv? */
66 char vlr_number[GT_MAX_DIGITS+1];
67 char sgsn_number[GT_MAX_DIGITS+1];
68 char sgsn_address[GT_MAX_DIGITS+1];
69 /* ggsn number + address */
70 /* gmlc number */
71 /* smsc number */
72 uint32_t periodic_lu_timer;
73 uint32_t periodic_rau_tau_timer;
74 bool nam_cs;
75 bool nam_ps;
76 uint32_t lmsi;
77 bool ms_purged_cs;
78 bool ms_purged_ps;
79};
80
Neels Hofmeyrf7c3e6e2017-10-09 17:55:16 +020081int db_subscr_create(struct db_context *dbc, const char *imsi);
82int db_subscr_delete_by_id(struct db_context *dbc, int64_t subscr_id);
83
84int db_subscr_update_msisdn_by_imsi(struct db_context *dbc, const char *imsi,
85 const char *msisdn);
86
Neels Hofmeyr518335e2017-10-06 03:20:14 +020087int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
88 struct hlr_subscriber *subscr);
Neels Hofmeyr9c2bbc82017-10-09 17:30:32 +020089int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
90 struct hlr_subscriber *subscr);
91int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
92 struct hlr_subscriber *subscr);
Neels Hofmeyre8ccd502017-10-06 04:10:06 +020093int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps);
Neels Hofmeyrdd783052017-10-09 17:36:08 +020094int db_subscr_lu(struct db_context *dbc, int64_t subscr_id,
95 const char *vlr_or_sgsn_number, bool is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +020096
97int db_subscr_purge(struct db_context *dbc,
98 const char *imsi, bool is_ps);