From d63ec88dba8bf6e262f7abf7d689b6a8d1fff1e4 Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Fri, 17 Jun 2022 17:54:51 +0200 Subject: [PATCH] ctrl: Introduce cmd SET subscriber.create Create a new subscriber from CTRL, similar to VTY command "imsi IDENT create". On success SET_REPLY contains the ID of the newly created subscriber. Related: SYS#5993 Change-Id: Id1b760cd07712245a0eeabaac7891bce93c1fe8e --- include/osmocom/hlr/logging.h | 1 + src/ctrl.c | 39 +++++++++++++++++++++++++++++++ src/logging.c | 6 +++++ tests/test_subscriber.ctrl | 14 +++++++++++ tests/test_subscriber_errors.ctrl | 6 +++++ 5 files changed, 66 insertions(+) diff --git a/include/osmocom/hlr/logging.h b/include/osmocom/hlr/logging.h index a8081af6..0a44a083 100644 --- a/include/osmocom/hlr/logging.h +++ b/include/osmocom/hlr/logging.h @@ -11,6 +11,7 @@ enum { DMSLOOKUP, DLU, DDGSM, + DCTRL, }; extern const struct log_info hlr_log_info; diff --git a/src/ctrl.c b/src/ctrl.c index 37177c58..5b091f82 100644 --- a/src/ctrl.c +++ b/src/ctrl.c @@ -197,6 +197,43 @@ static void print_subscr_info_aud3g(struct ctrl_cmd *cmd, struct osmo_sub_auth_d aud->u.umts.sqn); } +CTRL_CMD_DEFINE_WO_NOVRF(subscr_create, "create"); +static int set_subscr_create(struct ctrl_cmd *cmd, void *data) +{ + struct hlr_subscriber subscr; + struct hlr *hlr = data; + const char *imsi = cmd->value; + int rc; + + if (!osmo_imsi_str_valid(imsi)) { + cmd->reply = "Invalid IMSI value."; + return CTRL_CMD_ERROR; + } + + /* Create the subscriber in the DB */ + rc = db_subscr_create(g_hlr->dbc, imsi, DB_SUBSCR_FLAG_NAM_CS | DB_SUBSCR_FLAG_NAM_PS); + if (rc) { + if (rc == -EEXIST) + cmd->reply = "Subscriber already exists."; + else + cmd->reply = "Cannot create subscriber."; + return CTRL_CMD_ERROR; + } + + LOGP(DCTRL, LOGL_INFO, "Created subscriber IMSI='%s'\n", + imsi); + + /* Retrieve data of newly created subscriber: */ + rc = db_subscr_get_by_imsi(hlr->dbc, imsi, &subscr); + if (rc < 0) { + cmd->reply = "Failed retrieving ID of newly created subscriber."; + return CTRL_CMD_ERROR; + } + + cmd->reply = talloc_asprintf(cmd, "%" PRIu64, subscr.id); + return CTRL_CMD_REPLY; +} + CTRL_CMD_DEFINE_RO(subscr_info, "info"); static int get_subscr_info(struct ctrl_cmd *cmd, void *data) { @@ -380,6 +417,8 @@ static int hlr_ctrl_cmds_install() { int rc = 0; + rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR, &cmd_subscr_create); + rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info); rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info_aud); rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info_all); diff --git a/src/logging.c b/src/logging.c index eab0510c..6f0f3d2a 100644 --- a/src/logging.c +++ b/src/logging.c @@ -43,6 +43,12 @@ const struct log_info_cat hlr_log_info_cat[] = { .color = "\033[1;35m", .enabled = 1, .loglevel = LOGL_NOTICE, }, + [DCTRL] = { + .name = "DCTRL", + .description = "Osmocom CTRL interface", + .color = "\033[1;30m", + .enabled = 1, .loglevel = LOGL_NOTICE, + }, }; const struct log_info hlr_log_info = { diff --git a/tests/test_subscriber.ctrl b/tests/test_subscriber.ctrl index 756e8881..8d3c9dc6 100644 --- a/tests/test_subscriber.ctrl +++ b/tests/test_subscriber.ctrl @@ -610,3 +610,17 @@ periodic_lu_timer 0 periodic_rau_tau_timer 0 lmsi 00000000 +SET 101 subscriber.create 901991234567891 +SET_REPLY 101 subscriber.create 124 + +GET 102 subscriber.by-id-124.info +GET_REPLY 102 subscriber.by-id-124.info +id 124 +imsi 901991234567891 +nam_cs 1 +nam_ps 1 +ms_purged_cs 0 +ms_purged_ps 0 +periodic_lu_timer 0 +periodic_rau_tau_timer 0 +lmsi 00000000 diff --git a/tests/test_subscriber_errors.ctrl b/tests/test_subscriber_errors.ctrl index 0c5b5872..425b0df3 100644 --- a/tests/test_subscriber_errors.ctrl +++ b/tests/test_subscriber_errors.ctrl @@ -108,3 +108,9 @@ ERROR 47 Invalid value part of 'by-xxx-value' selector. GET 48 subscriber.by-id-0x0123.info ERROR 48 Invalid value part of 'by-xxx-value' selector. + +SET 49 subscriber.create zzz +ERROR 49 Invalid IMSI value. + +SET 50 subscriber.create 901990000000001 +ERROR 50 Subscriber already exists.