Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
Max | 3911533 | 2016-06-07 15:32:16 +0200 | [diff] [blame] | 21 | |
Harald Welte | 5919b6a | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 22 | #include <osmocom/ctrl/control_cmd.h> |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame^] | 23 | #include <osmocom/core/utils.h> |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 24 | #include <openbsc/gsm_data.h> |
| 25 | #include <openbsc/gsm_subscriber.h> |
| 26 | #include <openbsc/db.h> |
Holger Hans Peter Freyther | c652a5d | 2014-03-23 14:01:08 +0100 | [diff] [blame] | 27 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 28 | |
Max | 5872641 | 2016-06-30 10:25:49 +0200 | [diff] [blame] | 29 | #include <stdbool.h> |
| 30 | |
Holger Hans Peter Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 31 | static bool alg_supported(const char *alg) |
| 32 | { |
| 33 | /* |
| 34 | * TODO: share this with the vty_interface and extend to all |
| 35 | * algorithms supported by libosmocore now. Make it table based |
| 36 | * as well. |
| 37 | */ |
| 38 | if (strcasecmp(alg, "none") == 0) |
| 39 | return true; |
| 40 | if (strcasecmp(alg, "xor") == 0) |
| 41 | return true; |
| 42 | if (strcasecmp(alg, "comp128v1") == 0) |
| 43 | return true; |
| 44 | return false; |
| 45 | } |
| 46 | |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 47 | static int verify_subscriber_modify(struct ctrl_cmd *cmd, const char *value, void *d) |
| 48 | { |
Holger Hans Peter Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 49 | char *tmp, *imsi, *msisdn, *alg, *ki, *saveptr = NULL; |
Jacob Erlbeck | c73a5fa | 2014-10-28 14:57:53 +0100 | [diff] [blame] | 50 | int rc = 0; |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 51 | |
| 52 | tmp = talloc_strdup(cmd, value); |
| 53 | if (!tmp) |
| 54 | return 1; |
| 55 | |
| 56 | imsi = strtok_r(tmp, ",", &saveptr); |
| 57 | msisdn = strtok_r(NULL, ",", &saveptr); |
Holger Hans Peter Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 58 | alg = strtok_r(NULL, ",", &saveptr); |
| 59 | ki = strtok_r(NULL, ",", &saveptr); |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 60 | |
| 61 | if (!imsi || !msisdn) |
Jacob Erlbeck | c73a5fa | 2014-10-28 14:57:53 +0100 | [diff] [blame] | 62 | rc = 1; |
Harald Welte | 02884f2 | 2016-04-20 17:50:17 +0200 | [diff] [blame] | 63 | else if (strlen(imsi) > GSM23003_IMSI_MAX_DIGITS) |
Jacob Erlbeck | c73a5fa | 2014-10-28 14:57:53 +0100 | [diff] [blame] | 64 | rc = 1; |
| 65 | else if (strlen(msisdn) >= GSM_EXTENSION_LENGTH) |
| 66 | rc = 1; |
Holger Hans Peter Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 67 | else if (alg) { |
| 68 | if (!alg_supported(alg)) |
| 69 | rc = 1; |
| 70 | else if (strcasecmp(alg, "none") != 0 && !ki) |
| 71 | rc = 1; |
| 72 | } |
Jacob Erlbeck | c73a5fa | 2014-10-28 14:57:53 +0100 | [diff] [blame] | 73 | |
| 74 | talloc_free(tmp); |
| 75 | return rc; |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static int get_subscriber_modify(struct ctrl_cmd *cmd, void *data) |
| 79 | { |
| 80 | cmd->reply = "Set only attribute"; |
| 81 | return CTRL_CMD_ERROR; |
| 82 | } |
| 83 | |
| 84 | static int set_subscriber_modify(struct ctrl_cmd *cmd, void *data) |
| 85 | { |
| 86 | struct gsm_network *net = cmd->node; |
Holger Hans Peter Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 87 | char *tmp, *imsi, *msisdn, *alg, *ki, *saveptr = NULL; |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 88 | struct gsm_subscriber* subscr; |
| 89 | int rc; |
| 90 | |
| 91 | tmp = talloc_strdup(cmd, cmd->value); |
| 92 | if (!tmp) |
| 93 | return 1; |
| 94 | |
| 95 | imsi = strtok_r(tmp, ",", &saveptr); |
| 96 | msisdn = strtok_r(NULL, ",", &saveptr); |
Holger Hans Peter Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 97 | alg = strtok_r(NULL, ",", &saveptr); |
| 98 | ki = strtok_r(NULL, ",", &saveptr); |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 99 | |
Jacob Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 100 | subscr = subscr_get_by_imsi(net->subscr_group, imsi); |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 101 | if (!subscr) |
Max | 5872641 | 2016-06-30 10:25:49 +0200 | [diff] [blame] | 102 | subscr = subscr_create_subscriber(net->subscr_group, imsi); |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 103 | if (!subscr) |
| 104 | goto fail; |
| 105 | |
| 106 | subscr->authorized = 1; |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame^] | 107 | osmo_strlcpy(subscr->extension, msisdn, sizeof(subscr->extension)); |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 108 | |
| 109 | /* put it back to the db */ |
| 110 | rc = db_sync_subscriber(subscr); |
| 111 | db_subscriber_update(subscr); |
Holger Hans Peter Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 112 | |
| 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 Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 142 | 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 | |
| 154 | fail: |
| 155 | talloc_free(tmp); |
| 156 | cmd->reply = "Failed to create subscriber"; |
| 157 | return CTRL_CMD_ERROR; |
| 158 | } |
| 159 | |
| 160 | CTRL_CMD_DEFINE(subscriber_modify, "subscriber-modify-v1"); |
| 161 | |
Holger Hans Peter Freyther | c652a5d | 2014-03-23 14:01:08 +0100 | [diff] [blame] | 162 | static int verify_subscriber_delete(struct ctrl_cmd *cmd, const char *v, void *d) |
| 163 | { |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int get_subscriber_delete(struct ctrl_cmd *cmd, void *data) |
| 168 | { |
| 169 | cmd->reply = "Set only attribute"; |
| 170 | return CTRL_CMD_ERROR; |
| 171 | } |
| 172 | |
| 173 | static 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 Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 180 | subscr = subscr_get_by_imsi(net->subscr_group, cmd->value); |
Holger Hans Peter Freyther | c652a5d | 2014-03-23 14:01:08 +0100 | [diff] [blame] | 181 | 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 | } |
| 202 | CTRL_CMD_DEFINE(subscriber_delete, "subscriber-delete-v1"); |
| 203 | |
Holger Hans Peter Freyther | d41b7b7 | 2014-03-23 16:22:55 +0100 | [diff] [blame] | 204 | static int verify_subscriber_list(struct ctrl_cmd *cmd, const char *value, void *d) |
| 205 | { |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | static int set_subscriber_list(struct ctrl_cmd *cmd, void *d) |
| 210 | { |
| 211 | cmd->reply = "Get only attribute"; |
| 212 | return CTRL_CMD_ERROR; |
| 213 | } |
| 214 | |
| 215 | static 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 | |
| 222 | static 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 | } |
| 230 | CTRL_CMD_DEFINE(subscriber_list, "subscriber-list-active-v1"); |
| 231 | |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 232 | int 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 Freyther | c652a5d | 2014-03-23 14:01:08 +0100 | [diff] [blame] | 237 | rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_delete); |
Holger Hans Peter Freyther | d41b7b7 | 2014-03-23 16:22:55 +0100 | [diff] [blame] | 238 | rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_list); |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 239 | return rc; |
| 240 | } |