diff options
author | Holger Hans Peter Freyther <holger@moiji-mobile.com> | 2014-03-23 14:01:08 +0100 |
---|---|---|
committer | Holger Hans Peter Freyther <holger@moiji-mobile.com> | 2014-03-23 14:05:49 +0100 |
commit | 2d99eeb7f2c7978e9d96f5df61e462e6feb05973 (patch) | |
tree | ffe29fa00874ddb3aade11d773c49b8ea35f32e3 /openbsc/src/libmsc | |
parent | 9dbc3f8db7ca2c95ad6b986cdc4cd6c7a602a8b4 (diff) |
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
Diffstat (limited to 'openbsc/src/libmsc')
-rw-r--r-- | openbsc/src/libmsc/ctrl_commands.c | 44 | ||||
-rw-r--r-- | openbsc/src/libmsc/db.c | 87 |
2 files changed, 131 insertions, 0 deletions
diff --git a/openbsc/src/libmsc/ctrl_commands.c b/openbsc/src/libmsc/ctrl_commands.c index 75b094af1..b66cece8e 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 @@ fail: 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 4e6ffc371..a21258d14 100644 --- a/openbsc/src/libmsc/db.c +++ b/openbsc/src/libmsc/db.c @@ -815,6 +815,93 @@ int db_sync_subscriber(struct gsm_subscriber *subscriber) 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; |