blob: b0625bb0fb9dbb64ecabda3e9a72b7c133231aa8 [file] [log] [blame]
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +01001/*
2 * (C) 2014 by Holger Hans Peter Freyther
3 * (C) 2014 by sysmocom s.f.m.c. GmbH
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
Max0fcd2e22016-06-07 15:32:16 +020021
Harald Welteba874b82014-08-20 23:47:15 +020022#include <osmocom/ctrl/control_cmd.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010023#include <osmocom/core/utils.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020024#include <osmocom/msc/gsm_data.h>
25#include <osmocom/msc/gsm_subscriber.h>
26#include <osmocom/msc/db.h>
27#include <osmocom/msc/debug.h>
28#include <osmocom/msc/vlr.h>
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010029
Maxe6052c42016-06-30 10:25:49 +020030#include <stdbool.h>
31
Harald Welte2483f1b2016-06-19 18:06:02 +020032static struct gsm_network *msc_ctrl_net = NULL;
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020033
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +010034static int get_subscriber_list(struct ctrl_cmd *cmd, void *d)
35{
Harald Welte2483f1b2016-06-19 18:06:02 +020036 struct vlr_subscr *vsub;
37
38 if (!msc_ctrl_net) {
39 cmd->reply = "MSC CTRL commands not initialized";
40 return CTRL_CMD_ERROR;
41 }
42
43 if (!msc_ctrl_net->vlr) {
44 cmd->reply = "VLR not initialized";
45 return CTRL_CMD_ERROR;
46 }
47
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +010048 cmd->reply = talloc_strdup(cmd, "");
49
Harald Welte2483f1b2016-06-19 18:06:02 +020050 llist_for_each_entry(vsub, &msc_ctrl_net->vlr->subscribers, list) {
Neels Hofmeyrb305a002017-09-09 17:00:21 +020051 /* Do not list subscribers that aren't successfully attached. */
52 if (!vsub->lu_complete)
53 continue;
Harald Welte2483f1b2016-06-19 18:06:02 +020054 cmd->reply = talloc_asprintf_append(cmd->reply, "%s,%s\n",
55 vsub->imsi, vsub->msisdn);
56 }
57 printf("%s\n", cmd->reply); /* <-- what? */
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +010058 return CTRL_CMD_REPLY;
59}
Max50eb6692017-05-02 16:44:43 +020060CTRL_CMD_DEFINE_RO(subscriber_list, "subscriber-list-active-v1");
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +010061
Maxdcc193d2017-12-27 19:34:15 +010062CTRL_CMD_DEFINE_WO_NOVRF(sub_expire, "subscriber-expire");
63static int set_sub_expire(struct ctrl_cmd *cmd, void *data)
64{
65 struct vlr_subscr *vsub;
66
67 if (!msc_ctrl_net) {
68 cmd->reply = "MSC CTRL commands not initialized";
69 return CTRL_CMD_ERROR;
70 }
71
72 if (!msc_ctrl_net->vlr) {
73 cmd->reply = "VLR not initialized";
74 return CTRL_CMD_ERROR;
75 }
76
77 vsub = vlr_subscr_find_by_imsi(msc_ctrl_net->vlr, cmd->value);
78 if (!vsub) {
79 LOGP(DCTRL, LOGL_ERROR, "Attempt to expire unknown subscriber IMSI=%s\n", cmd->value);
80 cmd->reply = "IMSI unknown";
81 return CTRL_CMD_ERROR;
82 }
83
84 LOGP(DCTRL, LOGL_NOTICE, "Expiring subscriber IMSI=%s\n", cmd->value);
85
86 if (vlr_subscr_expire(vsub))
87 LOGP(DCTRL, LOGL_NOTICE, "VLR released subscriber %s\n", vlr_subscr_name(vsub));
88
89 if (vsub->use_count > 1)
90 LOGP(DCTRL, LOGL_NOTICE, "Subscriber %s is still in use, should be released soon\n",
91 vlr_subscr_name(vsub));
92
93 vlr_subscr_put(vsub);
94
95 return CTRL_CMD_REPLY;
96}
97
Harald Welte2483f1b2016-06-19 18:06:02 +020098int msc_ctrl_cmds_install(struct gsm_network *net)
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010099{
100 int rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +0200101 msc_ctrl_net = net;
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100102
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +0100103 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_list);
Maxdcc193d2017-12-27 19:34:15 +0100104 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_sub_expire);
105
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100106 return rc;
107}