nitb/ctrl: Implement creating and deleting subscribers
Sadly there is no proper foreign key relationship on the tables
that related to the Subscriber. This means we can't use a DELETE
with Cascade and need to delete everything by hand. To make things
worse maybe the SMS/Paging code is still using the subscriber
making the operation more dangerous. I had added NULL checks for
sender_id/receiver_id at 30C3 so we should not crash in this
situation.
Fixes: SYS#274
diff --git a/openbsc/src/libmsc/ctrl_commands.c b/openbsc/src/libmsc/ctrl_commands.c
index 75b094a..b66cece 100644
--- a/openbsc/src/libmsc/ctrl_commands.c
+++ b/openbsc/src/libmsc/ctrl_commands.c
@@ -22,6 +22,7 @@
#include <openbsc/gsm_data.h>
#include <openbsc/gsm_subscriber.h>
#include <openbsc/db.h>
+#include <openbsc/debug.h>
static int verify_subscriber_modify(struct ctrl_cmd *cmd, const char *value, void *d)
{
@@ -97,10 +98,53 @@
CTRL_CMD_DEFINE(subscriber_modify, "subscriber-modify-v1");
+static int verify_subscriber_delete(struct ctrl_cmd *cmd, const char *v, void *d)
+{
+ return 0;
+}
+
+static int get_subscriber_delete(struct ctrl_cmd *cmd, void *data)
+{
+ cmd->reply = "Set only attribute";
+ return CTRL_CMD_ERROR;
+}
+
+static int set_subscriber_delete(struct ctrl_cmd *cmd, void *data)
+{
+ int was_used = 0;
+ int rc;
+ struct gsm_subscriber *subscr;
+ struct gsm_network *net = cmd->node;
+
+ subscr = subscr_get_by_imsi(net, cmd->value);
+ if (!subscr) {
+ cmd->reply = "Failed to find subscriber";
+ return CTRL_CMD_ERROR;
+ }
+
+ if (subscr->use_count != 1) {
+ LOGP(DCTRL, LOGL_NOTICE, "Going to remove active subscriber.\n");
+ was_used = 1;
+ }
+
+ rc = db_subscriber_delete(subscr);
+ subscr_put(subscr);
+
+ if (rc != 0) {
+ cmd->reply = "Failed to remove subscriber";
+ return CTRL_CMD_ERROR;
+ }
+
+ cmd->reply = was_used ? "Removed active subscriber" : "Removed";
+ return CTRL_CMD_REPLY;
+}
+CTRL_CMD_DEFINE(subscriber_delete, "subscriber-delete-v1");
+
int msc_ctrl_cmds_install(void)
{
int rc = 0;
rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_modify);
+ rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_delete);
return rc;
}
diff --git a/openbsc/src/libmsc/db.c b/openbsc/src/libmsc/db.c
index 4e6ffc3..a21258d 100644
--- a/openbsc/src/libmsc/db.c
+++ b/openbsc/src/libmsc/db.c
@@ -815,6 +815,93 @@
return 0;
}
+int db_subscriber_delete(struct gsm_subscriber *subscr)
+{
+ dbi_result result;
+
+ result = dbi_conn_queryf(conn,
+ "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
+ subscr->id);
+ if (!result) {
+ LOGP(DDB, LOGL_ERROR,
+ "Failed to delete Authkeys for %llu\n", subscr->id);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(conn,
+ "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
+ subscr->id);
+ if (!result) {
+ LOGP(DDB, LOGL_ERROR,
+ "Failed to delete AuthLastTuples for %llu\n", subscr->id);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(conn,
+ "DELETE FROM AuthToken WHERE subscriber_id=%llu",
+ subscr->id);
+ if (!result) {
+ LOGP(DDB, LOGL_ERROR,
+ "Failed to delete AuthToken for %llu\n", subscr->id);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(conn,
+ "DELETE FROM EquipmentWatch WHERE subscriber_id=%llu",
+ subscr->id);
+ if (!result) {
+ LOGP(DDB, LOGL_ERROR,
+ "Failed to delete EquipmentWatch for %llu\n", subscr->id);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(conn,
+ "DELETE FROM SMS WHERE sender_id=%llu OR receiver_id=%llu",
+ subscr->id, subscr->id);
+ if (!result) {
+ LOGP(DDB, LOGL_ERROR,
+ "Failed to delete SMS for %llu\n", subscr->id);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(conn,
+ "DELETE FROM VLR WHERE subscriber_id=%llu",
+ subscr->id);
+ if (!result) {
+ LOGP(DDB, LOGL_ERROR,
+ "Failed to delete VLR for %llu\n", subscr->id);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(conn,
+ "DELETE FROM ApduBlobs WHERE subscriber_id=%llu",
+ subscr->id);
+ if (!result) {
+ LOGP(DDB, LOGL_ERROR,
+ "Failed to delete ApduBlobs for %llu\n", subscr->id);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(conn,
+ "DELETE FROM Subscriber WHERE id=%llu",
+ subscr->id);
+ if (!result) {
+ LOGP(DDB, LOGL_ERROR,
+ "Failed to delete Subscriber for %llu\n", subscr->id);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ return 0;
+}
+
int db_sync_equipment(struct gsm_equipment *equip)
{
dbi_result result;