blob: 0d6a37c969c8c7510d2c08777f0634372df83c1b [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 */
Harald Welteba874b82014-08-20 23:47:15 +020021#include <osmocom/ctrl/control_cmd.h>
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010022#include <openbsc/gsm_data.h>
23#include <openbsc/gsm_subscriber.h>
24#include <openbsc/db.h>
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +010025#include <openbsc/debug.h>
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010026
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020027static bool alg_supported(const char *alg)
28{
29 /*
30 * TODO: share this with the vty_interface and extend to all
31 * algorithms supported by libosmocore now. Make it table based
32 * as well.
33 */
34 if (strcasecmp(alg, "none") == 0)
35 return true;
36 if (strcasecmp(alg, "xor") == 0)
37 return true;
38 if (strcasecmp(alg, "comp128v1") == 0)
39 return true;
40 return false;
41}
42
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010043static int verify_subscriber_modify(struct ctrl_cmd *cmd, const char *value, void *d)
44{
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020045 char *tmp, *imsi, *msisdn, *alg, *ki, *saveptr = NULL;
Jacob Erlbeck76606d32014-10-28 14:57:53 +010046 int rc = 0;
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010047
48 tmp = talloc_strdup(cmd, value);
49 if (!tmp)
50 return 1;
51
52 imsi = strtok_r(tmp, ",", &saveptr);
53 msisdn = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020054 alg = strtok_r(NULL, ",", &saveptr);
55 ki = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010056
57 if (!imsi || !msisdn)
Jacob Erlbeck76606d32014-10-28 14:57:53 +010058 rc = 1;
Harald Welted3fa84d2016-04-20 17:50:17 +020059 else if (strlen(imsi) > GSM23003_IMSI_MAX_DIGITS)
Jacob Erlbeck76606d32014-10-28 14:57:53 +010060 rc = 1;
61 else if (strlen(msisdn) >= GSM_EXTENSION_LENGTH)
62 rc = 1;
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020063 else if (alg) {
64 if (!alg_supported(alg))
65 rc = 1;
66 else if (strcasecmp(alg, "none") != 0 && !ki)
67 rc = 1;
68 }
Jacob Erlbeck76606d32014-10-28 14:57:53 +010069
70 talloc_free(tmp);
71 return rc;
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010072}
73
74static int get_subscriber_modify(struct ctrl_cmd *cmd, void *data)
75{
76 cmd->reply = "Set only attribute";
77 return CTRL_CMD_ERROR;
78}
79
80static int set_subscriber_modify(struct ctrl_cmd *cmd, void *data)
81{
82 struct gsm_network *net = cmd->node;
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020083 char *tmp, *imsi, *msisdn, *alg, *ki, *saveptr = NULL;
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010084 struct gsm_subscriber* subscr;
85 int rc;
86
87 tmp = talloc_strdup(cmd, cmd->value);
88 if (!tmp)
89 return 1;
90
91 imsi = strtok_r(tmp, ",", &saveptr);
92 msisdn = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020093 alg = strtok_r(NULL, ",", &saveptr);
94 ki = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010095
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010096 subscr = subscr_get_by_imsi(net->subscr_group, imsi);
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010097 if (!subscr)
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010098 subscr = subscr_create_subscriber(net->subscr_group, imsi);
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010099 if (!subscr)
100 goto fail;
101
102 subscr->authorized = 1;
103 strncpy(subscr->extension, msisdn, GSM_EXTENSION_LENGTH - 1);
104 subscr->extension[GSM_EXTENSION_LENGTH-1] = '\0';
105
106 /* put it back to the db */
107 rc = db_sync_subscriber(subscr);
108 db_subscriber_update(subscr);
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +0200109
110 /* handle optional ciphering */
111 if (alg) {
112 if (strcasecmp(alg, "none") == 0)
113 db_sync_authinfo_for_subscr(NULL, subscr);
114 else {
115 struct gsm_auth_info ainfo = { 0, };
116 /* the verify should make sure that this is okay */
117 OSMO_ASSERT(alg);
118 OSMO_ASSERT(ki);
119
120 if (strcasecmp(alg, "xor") == 0)
121 ainfo.auth_algo = AUTH_ALGO_XOR;
122 else if (strcasecmp(alg, "comp128v1") == 0)
123 ainfo.auth_algo = AUTH_ALGO_COMP128v1;
124
125 rc = osmo_hexparse(ki, ainfo.a3a8_ki, sizeof(ainfo.a3a8_ki));
126 if (rc < 0) {
127 subscr_put(subscr);
128 talloc_free(tmp);
129 cmd->reply = "Failed to parse KI";
130 return CTRL_CMD_ERROR;
131 }
132
133 ainfo.a3a8_ki_len = rc;
134 db_sync_authinfo_for_subscr(&ainfo, subscr);
135 rc = 0;
136 }
137 db_sync_lastauthtuple_for_subscr(NULL, subscr);
138 }
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100139 subscr_put(subscr);
140
141 talloc_free(tmp);
142
143 if (rc != 0) {
144 cmd->reply = "Failed to store the record in the DB";
145 return CTRL_CMD_ERROR;
146 }
147
148 cmd->reply = "OK";
149 return CTRL_CMD_REPLY;
150
151fail:
152 talloc_free(tmp);
153 cmd->reply = "Failed to create subscriber";
154 return CTRL_CMD_ERROR;
155}
156
157CTRL_CMD_DEFINE(subscriber_modify, "subscriber-modify-v1");
158
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +0100159static int verify_subscriber_delete(struct ctrl_cmd *cmd, const char *v, void *d)
160{
161 return 0;
162}
163
164static int get_subscriber_delete(struct ctrl_cmd *cmd, void *data)
165{
166 cmd->reply = "Set only attribute";
167 return CTRL_CMD_ERROR;
168}
169
170static int set_subscriber_delete(struct ctrl_cmd *cmd, void *data)
171{
172 int was_used = 0;
173 int rc;
174 struct gsm_subscriber *subscr;
175 struct gsm_network *net = cmd->node;
176
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100177 subscr = subscr_get_by_imsi(net->subscr_group, cmd->value);
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +0100178 if (!subscr) {
179 cmd->reply = "Failed to find subscriber";
180 return CTRL_CMD_ERROR;
181 }
182
183 if (subscr->use_count != 1) {
184 LOGP(DCTRL, LOGL_NOTICE, "Going to remove active subscriber.\n");
185 was_used = 1;
186 }
187
188 rc = db_subscriber_delete(subscr);
189 subscr_put(subscr);
190
191 if (rc != 0) {
192 cmd->reply = "Failed to remove subscriber";
193 return CTRL_CMD_ERROR;
194 }
195
196 cmd->reply = was_used ? "Removed active subscriber" : "Removed";
197 return CTRL_CMD_REPLY;
198}
199CTRL_CMD_DEFINE(subscriber_delete, "subscriber-delete-v1");
200
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +0100201static int verify_subscriber_list(struct ctrl_cmd *cmd, const char *value, void *d)
202{
203 return 1;
204}
205
206static int set_subscriber_list(struct ctrl_cmd *cmd, void *d)
207{
208 cmd->reply = "Get only attribute";
209 return CTRL_CMD_ERROR;
210}
211
212static void list_cb(struct gsm_subscriber *subscr, void *d)
213{
214 char **data = (char **) d;
215 *data = talloc_asprintf_append(*data, "%s,%s\n",
216 subscr->imsi, subscr->extension);
217}
218
219static int get_subscriber_list(struct ctrl_cmd *cmd, void *d)
220{
221 cmd->reply = talloc_strdup(cmd, "");
222
223 db_subscriber_list_active(list_cb, &cmd->reply);
224 printf("%s\n", cmd->reply);
225 return CTRL_CMD_REPLY;
226}
227CTRL_CMD_DEFINE(subscriber_list, "subscriber-list-active-v1");
228
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100229int msc_ctrl_cmds_install(void)
230{
231 int rc = 0;
232
233 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_modify);
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +0100234 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_delete);
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +0100235 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_list);
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100236 return rc;
237}