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> |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 23 | #include <openbsc/gsm_data.h> |
| 24 | #include <openbsc/gsm_subscriber.h> |
| 25 | #include <openbsc/db.h> |
Holger Hans Peter Freyther | c652a5d | 2014-03-23 14:01:08 +0100 | [diff] [blame] | 26 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 27 | |
Max | 5872641 | 2016-06-30 10:25:49 +0200 | [diff] [blame] | 28 | #include <stdbool.h> |
| 29 | |
Holger Hans Peter Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 30 | static 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 Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 46 | static int verify_subscriber_modify(struct ctrl_cmd *cmd, const char *value, void *d) |
| 47 | { |
Holger Hans Peter Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 48 | char *tmp, *imsi, *msisdn, *alg, *ki, *saveptr = NULL; |
Jacob Erlbeck | c73a5fa | 2014-10-28 14:57:53 +0100 | [diff] [blame] | 49 | int rc = 0; |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 50 | |
| 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 Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 57 | alg = strtok_r(NULL, ",", &saveptr); |
| 58 | ki = strtok_r(NULL, ",", &saveptr); |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 59 | |
| 60 | if (!imsi || !msisdn) |
Jacob Erlbeck | c73a5fa | 2014-10-28 14:57:53 +0100 | [diff] [blame] | 61 | rc = 1; |
Harald Welte | 02884f2 | 2016-04-20 17:50:17 +0200 | [diff] [blame] | 62 | else if (strlen(imsi) > GSM23003_IMSI_MAX_DIGITS) |
Jacob Erlbeck | c73a5fa | 2014-10-28 14:57:53 +0100 | [diff] [blame] | 63 | rc = 1; |
| 64 | else if (strlen(msisdn) >= GSM_EXTENSION_LENGTH) |
| 65 | rc = 1; |
Holger Hans Peter Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 66 | else if (alg) { |
| 67 | if (!alg_supported(alg)) |
| 68 | rc = 1; |
| 69 | else if (strcasecmp(alg, "none") != 0 && !ki) |
| 70 | rc = 1; |
| 71 | } |
Jacob Erlbeck | c73a5fa | 2014-10-28 14:57:53 +0100 | [diff] [blame] | 72 | |
| 73 | talloc_free(tmp); |
| 74 | return rc; |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static int get_subscriber_modify(struct ctrl_cmd *cmd, void *data) |
| 78 | { |
| 79 | cmd->reply = "Set only attribute"; |
| 80 | return CTRL_CMD_ERROR; |
| 81 | } |
| 82 | |
| 83 | static int set_subscriber_modify(struct ctrl_cmd *cmd, void *data) |
| 84 | { |
| 85 | struct gsm_network *net = cmd->node; |
Holger Hans Peter Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 86 | char *tmp, *imsi, *msisdn, *alg, *ki, *saveptr = NULL; |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 87 | 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 Freyther | 7bd1b70 | 2016-04-06 22:41:12 +0200 | [diff] [blame] | 96 | alg = strtok_r(NULL, ",", &saveptr); |
| 97 | ki = strtok_r(NULL, ",", &saveptr); |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 98 | |
Jacob Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 99 | subscr = subscr_get_by_imsi(net->subscr_group, imsi); |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 100 | if (!subscr) |
Max | 5872641 | 2016-06-30 10:25:49 +0200 | [diff] [blame] | 101 | subscr = subscr_create_subscriber(net->subscr_group, imsi); |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 102 | 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 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 | } |