blob: a02db36b9d2f0775da06326e124a41df8bf8d11f [file] [log] [blame]
Holger Hans Peter Freytherfba03162014-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 */
Max39115332016-06-07 15:32:16 +020021
Harald Welte5919b6a2014-08-20 23:47:15 +020022#include <osmocom/ctrl/control_cmd.h>
Holger Hans Peter Freytherfba03162014-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 Freytherc652a5d2014-03-23 14:01:08 +010026#include <openbsc/debug.h>
Holger Hans Peter Freytherfba03162014-03-23 12:06:36 +010027
Holger Hans Peter Freyther7bd1b702016-04-06 22:41:12 +020028static bool alg_supported(const char *alg)
29{
30 /*
31 * TODO: share this with the vty_interface and extend to all
32 * algorithms supported by libosmocore now. Make it table based
33 * as well.
34 */
35 if (strcasecmp(alg, "none") == 0)
36 return true;
37 if (strcasecmp(alg, "xor") == 0)
38 return true;
39 if (strcasecmp(alg, "comp128v1") == 0)
40 return true;
41 return false;
42}
43
Holger Hans Peter Freytherfba03162014-03-23 12:06:36 +010044static int verify_subscriber_modify(struct ctrl_cmd *cmd, const char *value, void *d)
45{
Holger Hans Peter Freyther7bd1b702016-04-06 22:41:12 +020046 char *tmp, *imsi, *msisdn, *alg, *ki, *saveptr = NULL;
Jacob Erlbeckc73a5fa2014-10-28 14:57:53 +010047 int rc = 0;
Holger Hans Peter Freytherfba03162014-03-23 12:06:36 +010048
49 tmp = talloc_strdup(cmd, value);
50 if (!tmp)
51 return 1;
52
53 imsi = strtok_r(tmp, ",", &saveptr);
54 msisdn = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freyther7bd1b702016-04-06 22:41:12 +020055 alg = strtok_r(NULL, ",", &saveptr);
56 ki = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freytherfba03162014-03-23 12:06:36 +010057
58 if (!imsi || !msisdn)
Jacob Erlbeckc73a5fa2014-10-28 14:57:53 +010059 rc = 1;
Harald Welte02884f22016-04-20 17:50:17 +020060 else if (strlen(imsi) > GSM23003_IMSI_MAX_DIGITS)
Jacob Erlbeckc73a5fa2014-10-28 14:57:53 +010061 rc = 1;
62 else if (strlen(msisdn) >= GSM_EXTENSION_LENGTH)
63 rc = 1;
Holger Hans Peter Freyther7bd1b702016-04-06 22:41:12 +020064 else if (alg) {
65 if (!alg_supported(alg))
66 rc = 1;
67 else if (strcasecmp(alg, "none") != 0 && !ki)
68 rc = 1;
69 }
Jacob Erlbeckc73a5fa2014-10-28 14:57:53 +010070
71 talloc_free(tmp);
72 return rc;
Holger Hans Peter Freytherfba03162014-03-23 12:06:36 +010073}
74
75static int get_subscriber_modify(struct ctrl_cmd *cmd, void *data)
76{
77 cmd->reply = "Set only attribute";
78 return CTRL_CMD_ERROR;
79}
80
81static int set_subscriber_modify(struct ctrl_cmd *cmd, void *data)
82{
83 struct gsm_network *net = cmd->node;
Holger Hans Peter Freyther7bd1b702016-04-06 22:41:12 +020084 char *tmp, *imsi, *msisdn, *alg, *ki, *saveptr = NULL;
Holger Hans Peter Freytherfba03162014-03-23 12:06:36 +010085 struct gsm_subscriber* subscr;
86 int rc;
87
88 tmp = talloc_strdup(cmd, cmd->value);
89 if (!tmp)
90 return 1;
91
92 imsi = strtok_r(tmp, ",", &saveptr);
93 msisdn = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freyther7bd1b702016-04-06 22:41:12 +020094 alg = strtok_r(NULL, ",", &saveptr);
95 ki = strtok_r(NULL, ",", &saveptr);
Holger Hans Peter Freytherfba03162014-03-23 12:06:36 +010096
Jacob Erlbeckda8770b2014-12-03 09:28:24 +010097 subscr = subscr_get_by_imsi(net->subscr_group, imsi);
Holger Hans Peter Freytherfba03162014-03-23 12:06:36 +010098 if (!subscr)
Max39115332016-06-07 15:32:16 +020099 subscr = subscr_create_subscriber(net->subscr_group, imsi,
100 net->ext_min,
101 net->ext_max);
Holger Hans Peter Freytherfba03162014-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 Freyther7bd1b702016-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 Freytherfba03162014-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 Freytherc652a5d2014-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 Erlbeckda8770b2014-12-03 09:28:24 +0100180 subscr = subscr_get_by_imsi(net->subscr_group, cmd->value);
Holger Hans Peter Freytherc652a5d2014-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 Freytherd41b7b72014-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 Freytherfba03162014-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 Freytherc652a5d2014-03-23 14:01:08 +0100237 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_delete);
Holger Hans Peter Freytherd41b7b72014-03-23 16:22:55 +0100238 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_list);
Holger Hans Peter Freytherfba03162014-03-23 12:06:36 +0100239 return rc;
240}