Holger Hans Peter Freyther | 9dbc3f8 | 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 | */ |
| 21 | #include <openbsc/control_cmd.h> |
| 22 | #include <openbsc/gsm_data.h> |
| 23 | #include <openbsc/gsm_subscriber.h> |
| 24 | #include <openbsc/db.h> |
| 25 | |
| 26 | static int verify_subscriber_modify(struct ctrl_cmd *cmd, const char *value, void *d) |
| 27 | { |
| 28 | char *tmp, *imsi, *msisdn, *saveptr = NULL; |
| 29 | |
| 30 | tmp = talloc_strdup(cmd, value); |
| 31 | if (!tmp) |
| 32 | return 1; |
| 33 | |
| 34 | imsi = strtok_r(tmp, ",", &saveptr); |
| 35 | msisdn = strtok_r(NULL, ",", &saveptr); |
| 36 | talloc_free(tmp); |
| 37 | |
| 38 | if (!imsi || !msisdn) |
| 39 | return 1; |
| 40 | if (strlen(imsi) >= GSM_IMSI_LENGTH) |
| 41 | return 1; |
| 42 | if (strlen(msisdn) >= GSM_EXTENSION_LENGTH) |
| 43 | return 1; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int get_subscriber_modify(struct ctrl_cmd *cmd, void *data) |
| 48 | { |
| 49 | cmd->reply = "Set only attribute"; |
| 50 | return CTRL_CMD_ERROR; |
| 51 | } |
| 52 | |
| 53 | static int set_subscriber_modify(struct ctrl_cmd *cmd, void *data) |
| 54 | { |
| 55 | struct gsm_network *net = cmd->node; |
| 56 | char *tmp, *imsi, *msisdn, *saveptr = NULL; |
| 57 | struct gsm_subscriber* subscr; |
| 58 | int rc; |
| 59 | |
| 60 | tmp = talloc_strdup(cmd, cmd->value); |
| 61 | if (!tmp) |
| 62 | return 1; |
| 63 | |
| 64 | imsi = strtok_r(tmp, ",", &saveptr); |
| 65 | msisdn = strtok_r(NULL, ",", &saveptr); |
| 66 | |
| 67 | subscr = subscr_get_by_imsi(net, imsi); |
| 68 | if (!subscr) |
| 69 | subscr = subscr_create_subscriber(net, imsi); |
| 70 | if (!subscr) |
| 71 | goto fail; |
| 72 | |
| 73 | subscr->authorized = 1; |
| 74 | strncpy(subscr->extension, msisdn, GSM_EXTENSION_LENGTH - 1); |
| 75 | subscr->extension[GSM_EXTENSION_LENGTH-1] = '\0'; |
| 76 | |
| 77 | /* put it back to the db */ |
| 78 | rc = db_sync_subscriber(subscr); |
| 79 | db_subscriber_update(subscr); |
| 80 | subscr_put(subscr); |
| 81 | |
| 82 | talloc_free(tmp); |
| 83 | |
| 84 | if (rc != 0) { |
| 85 | cmd->reply = "Failed to store the record in the DB"; |
| 86 | return CTRL_CMD_ERROR; |
| 87 | } |
| 88 | |
| 89 | cmd->reply = "OK"; |
| 90 | return CTRL_CMD_REPLY; |
| 91 | |
| 92 | fail: |
| 93 | talloc_free(tmp); |
| 94 | cmd->reply = "Failed to create subscriber"; |
| 95 | return CTRL_CMD_ERROR; |
| 96 | } |
| 97 | |
| 98 | CTRL_CMD_DEFINE(subscriber_modify, "subscriber-modify-v1"); |
| 99 | |
| 100 | int msc_ctrl_cmds_install(void) |
| 101 | { |
| 102 | int rc = 0; |
| 103 | |
| 104 | rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_modify); |
| 105 | return rc; |
| 106 | } |