blob: 79e136d528e9c93d389758ec11d20348d47d6e56 [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>
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010023#include <openbsc/gsm_data.h>
24#include <openbsc/gsm_subscriber.h>
25#include <openbsc/db.h>
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +010026#include <openbsc/debug.h>
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010027
Maxe6052c42016-06-30 10:25:49 +020028#include <stdbool.h>
29
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020030static bool alg_supported(const char *alg)
31{
32 /*
33 * TODO: share this with the vty_interface and extend to all
34 * algorithms supported by libosmocore now. Make it table based
35 * as well.
36 */
37 if (strcasecmp(alg, "none") == 0)
38 return true;
39 if (strcasecmp(alg, "xor") == 0)
40 return true;
41 if (strcasecmp(alg, "comp128v1") == 0)
42 return true;
43 return false;
44}
45
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010046static int verify_subscriber_modify(struct ctrl_cmd *cmd, const char *value, void *d)
47{
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020048 char *tmp, *imsi, *msisdn, *alg, *ki, *saveptr = NULL;
Jacob Erlbeck76606d32014-10-28 14:57:53 +010049 int rc = 0;
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010050
51 tmp = talloc_strdup(cmd, value);
52 if (!tmp)
53 return 1;
54
55 imsi = strtok_r(tmp, ",", &saveptr);
56 msisdn = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020057 alg = strtok_r(NULL, ",", &saveptr);
58 ki = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010059
60 if (!imsi || !msisdn)
Jacob Erlbeck76606d32014-10-28 14:57:53 +010061 rc = 1;
Harald Welted3fa84d2016-04-20 17:50:17 +020062 else if (strlen(imsi) > GSM23003_IMSI_MAX_DIGITS)
Jacob Erlbeck76606d32014-10-28 14:57:53 +010063 rc = 1;
64 else if (strlen(msisdn) >= GSM_EXTENSION_LENGTH)
65 rc = 1;
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020066 else if (alg) {
67 if (!alg_supported(alg))
68 rc = 1;
69 else if (strcasecmp(alg, "none") != 0 && !ki)
70 rc = 1;
71 }
Jacob Erlbeck76606d32014-10-28 14:57:53 +010072
73 talloc_free(tmp);
74 return rc;
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010075}
76
77static int get_subscriber_modify(struct ctrl_cmd *cmd, void *data)
78{
79 cmd->reply = "Set only attribute";
80 return CTRL_CMD_ERROR;
81}
82
83static int set_subscriber_modify(struct ctrl_cmd *cmd, void *data)
84{
85 struct gsm_network *net = cmd->node;
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020086 char *tmp, *imsi, *msisdn, *alg, *ki, *saveptr = NULL;
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010087 struct gsm_subscriber* subscr;
88 int rc;
89
90 tmp = talloc_strdup(cmd, cmd->value);
91 if (!tmp)
92 return 1;
93
94 imsi = strtok_r(tmp, ",", &saveptr);
95 msisdn = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +020096 alg = strtok_r(NULL, ",", &saveptr);
97 ki = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +010098
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010099 subscr = subscr_get_by_imsi(net->subscr_group, imsi);
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100100 if (!subscr)
Maxe6052c42016-06-30 10:25:49 +0200101 subscr = subscr_create_subscriber(net->subscr_group, imsi);
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100102 if (!subscr)
103 goto fail;
104
105 subscr->authorized = 1;
106 strncpy(subscr->extension, msisdn, GSM_EXTENSION_LENGTH - 1);
107 subscr->extension[GSM_EXTENSION_LENGTH-1] = '\0';
108
109 /* put it back to the db */
110 rc = db_sync_subscriber(subscr);
111 db_subscriber_update(subscr);
Holger Hans Peter Freyther9bcb1a52016-04-06 22:41:12 +0200112
113 /* handle optional ciphering */
114 if (alg) {
115 if (strcasecmp(alg, "none") == 0)
116 db_sync_authinfo_for_subscr(NULL, subscr);
117 else {
118 struct gsm_auth_info ainfo = { 0, };
119 /* the verify should make sure that this is okay */
120 OSMO_ASSERT(alg);
121 OSMO_ASSERT(ki);
122
123 if (strcasecmp(alg, "xor") == 0)
124 ainfo.auth_algo = AUTH_ALGO_XOR;
125 else if (strcasecmp(alg, "comp128v1") == 0)
126 ainfo.auth_algo = AUTH_ALGO_COMP128v1;
127
128 rc = osmo_hexparse(ki, ainfo.a3a8_ki, sizeof(ainfo.a3a8_ki));
129 if (rc < 0) {
130 subscr_put(subscr);
131 talloc_free(tmp);
132 cmd->reply = "Failed to parse KI";
133 return CTRL_CMD_ERROR;
134 }
135
136 ainfo.a3a8_ki_len = rc;
137 db_sync_authinfo_for_subscr(&ainfo, subscr);
138 rc = 0;
139 }
140 db_sync_lastauthtuple_for_subscr(NULL, subscr);
141 }
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100142 subscr_put(subscr);
143
144 talloc_free(tmp);
145
146 if (rc != 0) {
147 cmd->reply = "Failed to store the record in the DB";
148 return CTRL_CMD_ERROR;
149 }
150
151 cmd->reply = "OK";
152 return CTRL_CMD_REPLY;
153
154fail:
155 talloc_free(tmp);
156 cmd->reply = "Failed to create subscriber";
157 return CTRL_CMD_ERROR;
158}
159
160CTRL_CMD_DEFINE(subscriber_modify, "subscriber-modify-v1");
161
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +0100162static int verify_subscriber_delete(struct ctrl_cmd *cmd, const char *v, void *d)
163{
164 return 0;
165}
166
167static int get_subscriber_delete(struct ctrl_cmd *cmd, void *data)
168{
169 cmd->reply = "Set only attribute";
170 return CTRL_CMD_ERROR;
171}
172
173static int set_subscriber_delete(struct ctrl_cmd *cmd, void *data)
174{
175 int was_used = 0;
176 int rc;
177 struct gsm_subscriber *subscr;
178 struct gsm_network *net = cmd->node;
179
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100180 subscr = subscr_get_by_imsi(net->subscr_group, cmd->value);
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +0100181 if (!subscr) {
182 cmd->reply = "Failed to find subscriber";
183 return CTRL_CMD_ERROR;
184 }
185
186 if (subscr->use_count != 1) {
187 LOGP(DCTRL, LOGL_NOTICE, "Going to remove active subscriber.\n");
188 was_used = 1;
189 }
190
191 rc = db_subscriber_delete(subscr);
192 subscr_put(subscr);
193
194 if (rc != 0) {
195 cmd->reply = "Failed to remove subscriber";
196 return CTRL_CMD_ERROR;
197 }
198
199 cmd->reply = was_used ? "Removed active subscriber" : "Removed";
200 return CTRL_CMD_REPLY;
201}
202CTRL_CMD_DEFINE(subscriber_delete, "subscriber-delete-v1");
203
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +0100204static int verify_subscriber_list(struct ctrl_cmd *cmd, const char *value, void *d)
205{
206 return 1;
207}
208
209static int set_subscriber_list(struct ctrl_cmd *cmd, void *d)
210{
211 cmd->reply = "Get only attribute";
212 return CTRL_CMD_ERROR;
213}
214
215static void list_cb(struct gsm_subscriber *subscr, void *d)
216{
217 char **data = (char **) d;
218 *data = talloc_asprintf_append(*data, "%s,%s\n",
219 subscr->imsi, subscr->extension);
220}
221
222static int get_subscriber_list(struct ctrl_cmd *cmd, void *d)
223{
224 cmd->reply = talloc_strdup(cmd, "");
225
226 db_subscriber_list_active(list_cb, &cmd->reply);
227 printf("%s\n", cmd->reply);
228 return CTRL_CMD_REPLY;
229}
230CTRL_CMD_DEFINE(subscriber_list, "subscriber-list-active-v1");
231
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100232int msc_ctrl_cmds_install(void)
233{
234 int rc = 0;
235
236 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_modify);
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +0100237 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_delete);
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +0100238 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_list);
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100239 return rc;
240}