Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file control_cmd.c |
| 2 | * SNMP-like status interface. */ |
| 3 | /* |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 4 | * (C) 2010-2011 by Daniel Willmann <daniel@totalueberwachung.de> |
| 5 | * (C) 2010-2011 by On-Waves |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 6 | * (C) 2014 by Harald Welte <laforge@gnumonks.org> |
| 7 | * (C) 2017 by sysmocom - s.f.m.c. GmbH |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 8 | * |
| 9 | * All Rights Reserved |
| 10 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 11 | * SPDX-License-Identifier: GPL-2.0+ |
| 12 | * |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #include <ctype.h> |
| 30 | #include <errno.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
| 36 | |
Harald Welte | 1238cc6 | 2014-08-20 19:50:04 +0200 | [diff] [blame] | 37 | #include <osmocom/ctrl/control_cmd.h> |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 38 | |
| 39 | #include <osmocom/core/msgb.h> |
| 40 | #include <osmocom/core/talloc.h> |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 41 | #include <osmocom/core/utils.h> |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 42 | #include <osmocom/vty/command.h> |
| 43 | #include <osmocom/vty/vector.h> |
| 44 | |
| 45 | extern vector ctrl_node_vec; |
| 46 | |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 47 | const struct value_string ctrl_type_vals[] = { |
Neels Hofmeyr | 7c1ec8c | 2017-03-02 14:32:52 +0100 | [diff] [blame] | 48 | { CTRL_TYPE_UNKNOWN, "(unknown)" }, |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 49 | { CTRL_TYPE_GET, "GET" }, |
| 50 | { CTRL_TYPE_SET, "SET" }, |
| 51 | { CTRL_TYPE_GET_REPLY, "GET_REPLY" }, |
| 52 | { CTRL_TYPE_SET_REPLY, "SET_REPLY" }, |
| 53 | { CTRL_TYPE_TRAP, "TRAP" }, |
| 54 | { CTRL_TYPE_ERROR, "ERROR" }, |
Neels Hofmeyr | 7c1ec8c | 2017-03-02 14:32:52 +0100 | [diff] [blame] | 55 | { 0, NULL } |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 58 | /* Functions from libosmocom */ |
| 59 | extern vector cmd_make_descvec(const char *string, const char *descstr); |
| 60 | |
| 61 | /* Get the ctrl_cmd_element that matches this command */ |
| 62 | static struct ctrl_cmd_element *ctrl_cmd_get_element_match(vector vline, vector node) |
| 63 | { |
| 64 | int index, j; |
| 65 | const char *desc; |
| 66 | struct ctrl_cmd_element *cmd_el; |
| 67 | struct ctrl_cmd_struct *cmd_desc; |
| 68 | char *str; |
| 69 | |
| 70 | for (index = 0; index < vector_active(node); index++) { |
| 71 | if ((cmd_el = vector_slot(node, index))) { |
| 72 | cmd_desc = &cmd_el->strcmd; |
| 73 | if (cmd_desc->nr_commands > vector_active(vline)) |
| 74 | continue; |
Holger Hans Peter Freyther | 5fb265e | 2015-04-05 14:36:31 +0200 | [diff] [blame] | 75 | for (j =0; j < vector_active(vline) && j < cmd_desc->nr_commands; j++) { |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 76 | str = vector_slot(vline, j); |
| 77 | desc = cmd_desc->command[j]; |
| 78 | if (desc[0] == '*') |
| 79 | return cmd_el; /* Partial match */ |
| 80 | if (strcmp(desc, str) != 0) |
| 81 | break; |
| 82 | } |
| 83 | /* We went through all the elements and all matched */ |
| 84 | if (j == cmd_desc->nr_commands) |
| 85 | return cmd_el; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return NULL; |
| 90 | } |
| 91 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 92 | /*! Execute a given received command |
| 93 | * \param[in] vline vector representing the available/registered commands |
| 94 | * \param[inout] command parsed received command to be executed |
| 95 | * \param[in] node CTRL interface node |
| 96 | * \param[in] data opaque data passed to verify(), get() and set() call-backs |
| 97 | * \returns CTRL_CMD_HANDLED or CTRL_CMD_REPLY; CTRL_CMD_ERROR on error */ |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 98 | int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data) |
| 99 | { |
| 100 | int ret = CTRL_CMD_ERROR; |
| 101 | struct ctrl_cmd_element *cmd_el; |
| 102 | |
| 103 | if ((command->type != CTRL_TYPE_GET) && (command->type != CTRL_TYPE_SET)) { |
| 104 | command->reply = "Trying to execute something not GET or SET"; |
| 105 | goto out; |
| 106 | } |
| 107 | if ((command->type == CTRL_TYPE_SET) && (!command->value)) { |
| 108 | command->reply = "SET without a value"; |
| 109 | goto out; |
| 110 | } |
| 111 | |
| 112 | if (!vline) |
| 113 | goto out; |
| 114 | |
| 115 | cmd_el = ctrl_cmd_get_element_match(vline, node); |
| 116 | |
| 117 | if (!cmd_el) { |
| 118 | command->reply = "Command not found"; |
| 119 | goto out; |
| 120 | } |
| 121 | |
| 122 | if (command->type == CTRL_TYPE_SET) { |
| 123 | if (!cmd_el->set) { |
| 124 | command->reply = "SET not implemented"; |
| 125 | goto out; |
| 126 | } |
| 127 | if (cmd_el->verify) { |
| 128 | if ((ret = cmd_el->verify(command, command->value, data))) { |
| 129 | ret = CTRL_CMD_ERROR; |
Daniel Willmann | 9225ab1 | 2011-07-28 21:38:51 +0200 | [diff] [blame] | 130 | /* If verify() set an appropriate error message, don't change it. */ |
| 131 | if (!command->reply) |
| 132 | command->reply = "Value failed verification."; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 133 | goto out; |
| 134 | } |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 135 | } |
| 136 | ret = cmd_el->set(command, data); |
| 137 | goto out; |
| 138 | } else if (command->type == CTRL_TYPE_GET) { |
| 139 | if (!cmd_el->get) { |
| 140 | command->reply = "GET not implemented"; |
| 141 | goto out; |
| 142 | } |
| 143 | ret = cmd_el->get(command, data); |
| 144 | goto out; |
| 145 | } |
| 146 | out: |
| 147 | if (ret == CTRL_CMD_REPLY) { |
| 148 | if (command->type == CTRL_TYPE_SET) { |
| 149 | command->type = CTRL_TYPE_SET_REPLY; |
| 150 | } else if (command->type == CTRL_TYPE_GET) { |
| 151 | command->type = CTRL_TYPE_GET_REPLY; |
| 152 | } |
| 153 | } else if (ret == CTRL_CMD_ERROR) { |
| 154 | command->type = CTRL_TYPE_ERROR; |
| 155 | } |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static void add_word(struct ctrl_cmd_struct *cmd, |
| 160 | const char *start, const char *end) |
| 161 | { |
| 162 | if (!cmd->command) { |
| 163 | cmd->command = talloc_zero_array(tall_vty_vec_ctx, |
| 164 | char*, 1); |
| 165 | cmd->nr_commands = 0; |
| 166 | } else { |
| 167 | cmd->command = talloc_realloc(tall_vty_vec_ctx, |
| 168 | cmd->command, char*, |
| 169 | cmd->nr_commands + 1); |
| 170 | } |
| 171 | |
| 172 | cmd->command[cmd->nr_commands++] = talloc_strndup(cmd->command, |
| 173 | start, end - start); |
| 174 | } |
| 175 | |
| 176 | static void create_cmd_struct(struct ctrl_cmd_struct *cmd, const char *name) |
| 177 | { |
| 178 | const char *cur, *word; |
| 179 | |
| 180 | for (cur = name, word = NULL; cur[0] != '\0'; ++cur) { |
| 181 | /* warn about optionals */ |
| 182 | if (cur[0] == '(' || cur[0] == ')' || cur[0] == '|') { |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 183 | LOGP(DLCTRL, LOGL_ERROR, |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 184 | "Optionals are not supported in '%s'\n", name); |
| 185 | goto failure; |
| 186 | } |
| 187 | |
| 188 | if (isspace(cur[0])) { |
| 189 | if (word) { |
| 190 | add_word(cmd, word, cur); |
| 191 | word = NULL; |
| 192 | } |
| 193 | continue; |
| 194 | } |
| 195 | |
| 196 | if (!word) |
| 197 | word = cur; |
| 198 | } |
| 199 | |
| 200 | if (word) |
| 201 | add_word(cmd, word, cur); |
| 202 | |
| 203 | return; |
| 204 | failure: |
| 205 | cmd->nr_commands = 0; |
| 206 | talloc_free(cmd->command); |
| 207 | } |
| 208 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 209 | /*! Install a given command definition at a given CTRL node. |
| 210 | * \param[in] node CTRL node at whihc \a cmd is to be installed |
| 211 | * \param[in] cmd command definition to be installed |
| 212 | * \returns 0 on success; negative on error */ |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 213 | int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd) |
| 214 | { |
| 215 | vector cmds_vec; |
| 216 | |
| 217 | cmds_vec = vector_lookup_ensure(ctrl_node_vec, node); |
| 218 | |
| 219 | if (!cmds_vec) { |
| 220 | cmds_vec = vector_init(5); |
| 221 | if (!cmds_vec) { |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 222 | LOGP(DLCTRL, LOGL_ERROR, "vector_init failed.\n"); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 223 | return -ENOMEM; |
| 224 | } |
| 225 | vector_set_index(ctrl_node_vec, node, cmds_vec); |
| 226 | } |
| 227 | |
| 228 | vector_set(cmds_vec, cmd); |
| 229 | |
| 230 | create_cmd_struct(&cmd->strcmd, cmd->name); |
| 231 | return 0; |
| 232 | } |
| 233 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 234 | /*! Allocate a control command of given \a type. |
| 235 | * \param[in] ctx talloc context from which to allocate |
| 236 | * \param[in] type command type to set after allocation |
| 237 | * \returns callee-allocated \ref ctrl_cmd. Caller must talloc_free() it. */ |
Holger Hans Peter Freyther | a6b3401 | 2011-08-22 23:44:32 +0200 | [diff] [blame] | 238 | struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type type) |
| 239 | { |
| 240 | struct ctrl_cmd *cmd; |
| 241 | |
| 242 | cmd = talloc_zero(ctx, struct ctrl_cmd); |
| 243 | if (!cmd) |
| 244 | return NULL; |
| 245 | |
| 246 | cmd->type = type; |
| 247 | return cmd; |
| 248 | } |
| 249 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 250 | /*! Perform a deepl copy of the given \a cmd, allocating memory from \a ctx. |
| 251 | * \param[in] ctx talloc context from which to allocate |
| 252 | * \param[in cmd CTRL command to be copied |
| 253 | * \returns deep copy of \a cmd on success; NULL on error */ |
Daniel Willmann | 8b7a962 | 2011-03-17 15:37:54 +0100 | [diff] [blame] | 254 | struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd) |
| 255 | { |
| 256 | struct ctrl_cmd *cmd2; |
| 257 | |
| 258 | cmd2 = talloc_zero(ctx, struct ctrl_cmd); |
| 259 | if (!cmd2) |
| 260 | return NULL; |
| 261 | |
| 262 | cmd2->type = cmd->type; |
| 263 | if (cmd->id) { |
| 264 | cmd2->id = talloc_strdup(cmd2, cmd->id); |
| 265 | if (!cmd2->id) |
| 266 | goto err; |
| 267 | } |
| 268 | if (cmd->variable) { |
| 269 | cmd2->variable = talloc_strdup(cmd2, cmd->variable); |
| 270 | if (!cmd2->variable) |
| 271 | goto err; |
| 272 | } |
| 273 | if (cmd->value) { |
| 274 | cmd2->value = talloc_strdup(cmd2, cmd->value); |
| 275 | if (!cmd2->value) |
| 276 | goto err; |
| 277 | } |
| 278 | if (cmd->reply) { |
| 279 | cmd2->reply = talloc_strdup(cmd2, cmd->reply); |
| 280 | if (!cmd2->reply) |
| 281 | goto err; |
| 282 | } |
| 283 | |
| 284 | return cmd2; |
| 285 | err: |
| 286 | talloc_free(cmd2); |
| 287 | return NULL; |
| 288 | } |
| 289 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 290 | /*! Parse/Decode CTRL from \ref msgb into command struct. |
| 291 | * \param[in] ctx talloc context from which to allocate |
| 292 | * \param[in] msg message buffer containing command to be decoded |
| 293 | * \returns callee-allocated decoded CTRL command; NULL on allocation or other failure |
Neels Hofmeyr | f2e83ad | 2017-12-16 01:05:25 +0100 | [diff] [blame] | 294 | * The caller is responsible to talloc_free() the returned struct pointer. */ |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 295 | struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg) |
| 296 | { |
Neels Hofmeyr | f2e83ad | 2017-12-16 01:05:25 +0100 | [diff] [blame] | 297 | struct ctrl_cmd *res = ctrl_cmd_parse2(ctx, msg); |
| 298 | if (res->type == CTRL_TYPE_ERROR) { |
| 299 | talloc_free(res); |
| 300 | return NULL; |
| 301 | } |
| 302 | return res; |
| 303 | } |
| 304 | |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 305 | static bool id_str_valid(const char *str) |
| 306 | { |
| 307 | for (;*str;str++) { |
| 308 | if (!isdigit(*str)) |
| 309 | return false; |
| 310 | } |
| 311 | return true; |
| 312 | } |
| 313 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 314 | /*! Parse/Decode CTRL from \ref msgb into command struct. |
| 315 | * \param[in] ctx talloc context from which to allocate |
| 316 | * \param[in] msg message buffer containing command to be decoded |
| 317 | * \returns callee-allocated decoded CTRL command; NULL on allocation failure, |
| 318 | * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error. |
Pau Espin Pedrol | 239ed3b | 2018-07-12 17:51:16 +0200 | [diff] [blame] | 319 | * The caller is responsible to talloc_free() the returned struct pointer. |
| 320 | * If information of the origin of the ERROR cmd returned is required (received |
| 321 | * or local parsing failure), use \ref ctrl_cmd_parse3 instead. */ |
Neels Hofmeyr | f2e83ad | 2017-12-16 01:05:25 +0100 | [diff] [blame] | 322 | struct ctrl_cmd *ctrl_cmd_parse2(void *ctx, struct msgb *msg) |
| 323 | { |
Pau Espin Pedrol | 239ed3b | 2018-07-12 17:51:16 +0200 | [diff] [blame] | 324 | bool unused; |
| 325 | return ctrl_cmd_parse3(ctx, msg, &unused); |
| 326 | } |
| 327 | |
| 328 | /*! Parse/Decode CTRL from \ref msgb into command struct. |
| 329 | * \param[in] ctx talloc context from which to allocate |
| 330 | * \param[in] msg message buffer containing command to be decoded |
| 331 | * \param[out] parse_failed Whether returned ERROR cmd was generatd locally |
| 332 | * (due to parse failure) or was received. |
| 333 | * \returns callee-allocated decoded CTRL command; NULL on allocation failure, |
| 334 | * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error. |
| 335 | * The caller is responsible to talloc_free() the returned struct pointer. */ |
| 336 | struct ctrl_cmd *ctrl_cmd_parse3(void *ctx, struct msgb *msg, bool *parse_failed) |
| 337 | { |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 338 | char *str, *tmp, *saveptr = NULL; |
| 339 | char *var, *val; |
| 340 | struct ctrl_cmd *cmd; |
| 341 | |
| 342 | cmd = talloc_zero(ctx, struct ctrl_cmd); |
| 343 | if (!cmd) { |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 344 | LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n"); |
Pau Espin Pedrol | 239ed3b | 2018-07-12 17:51:16 +0200 | [diff] [blame] | 345 | *parse_failed = true; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | /* Make sure input is NULL terminated */ |
| 350 | msgb_put_u8(msg, 0); |
| 351 | str = (char *) msg->l2h; |
| 352 | |
Harald Welte | edf6fe7 | 2016-11-26 10:06:07 +0100 | [diff] [blame] | 353 | OSMO_ASSERT(str); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 354 | tmp = strtok_r(str, " ", &saveptr); |
| 355 | if (!tmp) { |
| 356 | cmd->type = CTRL_TYPE_ERROR; |
| 357 | cmd->id = "err"; |
| 358 | cmd->reply = "Request malformed"; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 359 | LOGP(DLCTRL, LOGL_NOTICE, "Malformed request: \"%s\"\n", osmo_escape_str(str, -1)); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 360 | goto err; |
| 361 | } |
| 362 | |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 363 | cmd->type = get_string_value(ctrl_type_vals, tmp); |
Pau Espin Pedrol | 8aa85bd | 2017-06-23 12:43:31 +0200 | [diff] [blame] | 364 | if ((int)cmd->type < 0 || cmd->type == CTRL_TYPE_UNKNOWN) { |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 365 | cmd->type = CTRL_TYPE_ERROR; |
| 366 | cmd->id = "err"; |
| 367 | cmd->reply = "Request type unknown"; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 368 | LOGP(DLCTRL, LOGL_NOTICE, "Request type unknown: \"%s\"\n", osmo_escape_str(str, -1)); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 369 | goto err; |
| 370 | } |
| 371 | |
| 372 | tmp = strtok_r(NULL, " ", &saveptr); |
| 373 | |
| 374 | if (!tmp) { |
| 375 | cmd->type = CTRL_TYPE_ERROR; |
| 376 | cmd->id = "err"; |
| 377 | cmd->reply = "Missing ID"; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 378 | LOGP(DLCTRL, LOGL_NOTICE, "Missing ID: \"%s\"\n", osmo_escape_str(str, -1)); |
| 379 | goto err; |
| 380 | } |
| 381 | |
Pau Espin Pedrol | 40ad913 | 2018-07-12 17:41:55 +0200 | [diff] [blame] | 382 | if (!id_str_valid(tmp) && |
| 383 | !(cmd->type == CTRL_TYPE_ERROR && strcmp(tmp, "err") == 0)) { |
Pau Espin Pedrol | 55088b7 | 2018-07-12 13:03:52 +0200 | [diff] [blame] | 384 | LOGP(DLCTRL, LOGL_NOTICE, "Invalid %s message ID number: \"%s\"\n", |
| 385 | get_value_string(ctrl_type_vals, cmd->type), osmo_escape_str(tmp, -1)); |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 386 | cmd->type = CTRL_TYPE_ERROR; |
| 387 | cmd->id = "err"; |
| 388 | cmd->reply = "Invalid message ID number"; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 389 | goto err; |
| 390 | } |
| 391 | cmd->id = talloc_strdup(cmd, tmp); |
| 392 | if (!cmd->id) |
| 393 | goto oom; |
| 394 | |
| 395 | switch (cmd->type) { |
| 396 | case CTRL_TYPE_GET: |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 397 | var = strtok_r(NULL, " \n", &saveptr); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 398 | if (!var) { |
| 399 | cmd->type = CTRL_TYPE_ERROR; |
| 400 | cmd->reply = "GET incomplete"; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 401 | LOGP(DLCTRL, LOGL_NOTICE, "GET Command incomplete: \"%s\"\n", |
| 402 | osmo_escape_str(str, -1)); |
| 403 | goto err; |
| 404 | } |
| 405 | if (!osmo_separated_identifiers_valid(var, ".")) { |
| 406 | cmd->type = CTRL_TYPE_ERROR; |
| 407 | cmd->reply = "GET variable contains invalid characters"; |
| 408 | LOGP(DLCTRL, LOGL_NOTICE, "GET variable contains invalid characters: \"%s\"\n", |
| 409 | osmo_escape_str(var, -1)); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 410 | goto err; |
| 411 | } |
| 412 | cmd->variable = talloc_strdup(cmd, var); |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 413 | var = strtok_r(NULL, "", &saveptr); |
| 414 | if (var) { |
| 415 | cmd->type = CTRL_TYPE_ERROR; |
| 416 | cmd->reply = "GET with trailing characters"; |
| 417 | LOGP(DLCTRL, LOGL_NOTICE, "GET with trailing characters: \"%s\"\n", |
| 418 | osmo_escape_str(var, -1)); |
| 419 | goto err; |
| 420 | } |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 421 | LOGP(DLCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 422 | break; |
| 423 | case CTRL_TYPE_SET: |
| 424 | var = strtok_r(NULL, " ", &saveptr); |
Holger Hans Peter Freyther | 5ad742d | 2014-08-08 19:41:28 +0200 | [diff] [blame] | 425 | val = strtok_r(NULL, "\n", &saveptr); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 426 | if (!var || !val) { |
| 427 | cmd->type = CTRL_TYPE_ERROR; |
| 428 | cmd->reply = "SET incomplete"; |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 429 | LOGP(DLCTRL, LOGL_NOTICE, "SET Command incomplete\n"); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 430 | goto err; |
| 431 | } |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 432 | if (!osmo_separated_identifiers_valid(var, ".")) { |
| 433 | cmd->type = CTRL_TYPE_ERROR; |
| 434 | cmd->reply = "SET variable contains invalid characters"; |
| 435 | LOGP(DLCTRL, LOGL_NOTICE, "SET variable contains invalid characters: \"%s\"\n", |
| 436 | osmo_escape_str(var, -1)); |
| 437 | goto err; |
| 438 | } |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 439 | cmd->variable = talloc_strdup(cmd, var); |
| 440 | cmd->value = talloc_strdup(cmd, val); |
| 441 | if (!cmd->variable || !cmd->value) |
| 442 | goto oom; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 443 | |
| 444 | var = strtok_r(NULL, "", &saveptr); |
| 445 | if (var) { |
| 446 | cmd->type = CTRL_TYPE_ERROR; |
| 447 | cmd->reply = "SET with trailing characters"; |
| 448 | LOGP(DLCTRL, LOGL_NOTICE, "SET with trailing characters: \"%s\"\n", |
| 449 | osmo_escape_str(var, -1)); |
| 450 | goto err; |
| 451 | } |
| 452 | |
| 453 | LOGP(DLCTRL, LOGL_DEBUG, "Command: SET %s = \"%s\"\n", cmd->variable, |
| 454 | osmo_escape_str(cmd->value, -1)); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 455 | break; |
Neels Hofmeyr | a5e2162 | 2017-12-16 01:18:53 +0100 | [diff] [blame] | 456 | #define REPLY_CASE(TYPE, NAME) \ |
| 457 | case TYPE: \ |
| 458 | var = strtok_r(NULL, " ", &saveptr); \ |
| 459 | val = strtok_r(NULL, "", &saveptr); \ |
| 460 | if (!var) { \ |
| 461 | cmd->type = CTRL_TYPE_ERROR; \ |
| 462 | cmd->reply = NAME " incomplete"; \ |
| 463 | LOGP(DLCTRL, LOGL_NOTICE, NAME " incomplete\n"); \ |
| 464 | goto err; \ |
| 465 | } \ |
| 466 | if (!osmo_separated_identifiers_valid(var, ".")) { \ |
| 467 | cmd->type = CTRL_TYPE_ERROR; \ |
| 468 | cmd->reply = NAME " variable contains invalid characters"; \ |
| 469 | LOGP(DLCTRL, LOGL_NOTICE, NAME " variable contains invalid characters: \"%s\"\n", \ |
| 470 | osmo_escape_str(var, -1)); \ |
| 471 | goto err; \ |
| 472 | } \ |
| 473 | cmd->variable = talloc_strdup(cmd, var); \ |
| 474 | cmd->reply = talloc_strdup(cmd, val); \ |
| 475 | if (!cmd->variable || !cmd->reply) \ |
| 476 | goto oom; \ |
| 477 | LOGP(DLCTRL, LOGL_DEBUG, "Command: " NAME " %s: %s\n", cmd->variable, \ |
| 478 | osmo_escape_str(cmd->reply, -1)); \ |
| 479 | break |
| 480 | REPLY_CASE(CTRL_TYPE_GET_REPLY, "GET REPLY"); |
| 481 | REPLY_CASE(CTRL_TYPE_SET_REPLY, "SET REPLY"); |
| 482 | REPLY_CASE(CTRL_TYPE_TRAP, "TRAP"); |
| 483 | #undef REPLY_CASE |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 484 | case CTRL_TYPE_ERROR: |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 485 | var = strtok_r(NULL, "", &saveptr); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 486 | if (!var) { |
| 487 | cmd->reply = ""; |
| 488 | goto err; |
| 489 | } |
| 490 | cmd->reply = talloc_strdup(cmd, var); |
| 491 | if (!cmd->reply) |
| 492 | goto oom; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 493 | LOGP(DLCTRL, LOGL_DEBUG, "Command: ERROR \"%s\"\n", |
| 494 | osmo_escape_str(cmd->reply, -1)); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 495 | break; |
| 496 | case CTRL_TYPE_UNKNOWN: |
| 497 | default: |
| 498 | cmd->type = CTRL_TYPE_ERROR; |
| 499 | cmd->reply = "Unknown type"; |
| 500 | goto err; |
| 501 | } |
| 502 | |
Pau Espin Pedrol | 239ed3b | 2018-07-12 17:51:16 +0200 | [diff] [blame] | 503 | *parse_failed = false; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 504 | return cmd; |
| 505 | oom: |
| 506 | cmd->type = CTRL_TYPE_ERROR; |
| 507 | cmd->id = "err"; |
| 508 | cmd->reply = "OOM"; |
| 509 | err: |
Pau Espin Pedrol | 239ed3b | 2018-07-12 17:51:16 +0200 | [diff] [blame] | 510 | *parse_failed = true; |
Neels Hofmeyr | f2e83ad | 2017-12-16 01:05:25 +0100 | [diff] [blame] | 511 | return cmd; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 514 | /*! Encode a given CTRL command from its parsed form into a message buffer. |
| 515 | * \param[in] cmd decoded/parsed form of to-be-encoded command |
| 516 | * \returns callee-allocated message buffer containing the encoded \a cmd; NULL on error */ |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 517 | struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd) |
| 518 | { |
| 519 | struct msgb *msg; |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 520 | const char *type; |
| 521 | char *tmp; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 522 | |
| 523 | if (!cmd->id) |
| 524 | return NULL; |
| 525 | |
| 526 | msg = msgb_alloc_headroom(4096, 128, "ctrl command make"); |
| 527 | if (!msg) |
| 528 | return NULL; |
| 529 | |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 530 | type = get_value_string(ctrl_type_vals, cmd->type); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 531 | |
| 532 | switch (cmd->type) { |
| 533 | case CTRL_TYPE_GET: |
| 534 | if (!cmd->variable) |
| 535 | goto err; |
| 536 | |
| 537 | tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable); |
| 538 | if (!tmp) { |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 539 | LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n"); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 540 | goto err; |
| 541 | } |
| 542 | |
| 543 | msg->l2h = msgb_put(msg, strlen(tmp)); |
| 544 | memcpy(msg->l2h, tmp, strlen(tmp)); |
| 545 | talloc_free(tmp); |
| 546 | break; |
| 547 | case CTRL_TYPE_SET: |
| 548 | if (!cmd->variable || !cmd->value) |
| 549 | goto err; |
| 550 | |
| 551 | tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable, |
| 552 | cmd->value); |
| 553 | if (!tmp) { |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 554 | LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n"); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 555 | goto err; |
| 556 | } |
| 557 | |
| 558 | msg->l2h = msgb_put(msg, strlen(tmp)); |
| 559 | memcpy(msg->l2h, tmp, strlen(tmp)); |
| 560 | talloc_free(tmp); |
| 561 | break; |
| 562 | case CTRL_TYPE_GET_REPLY: |
| 563 | case CTRL_TYPE_SET_REPLY: |
| 564 | case CTRL_TYPE_TRAP: |
| 565 | if (!cmd->variable || !cmd->reply) |
| 566 | goto err; |
| 567 | |
| 568 | tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable, |
| 569 | cmd->reply); |
| 570 | if (!tmp) { |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 571 | LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n"); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 572 | goto err; |
| 573 | } |
| 574 | |
| 575 | msg->l2h = msgb_put(msg, strlen(tmp)); |
| 576 | memcpy(msg->l2h, tmp, strlen(tmp)); |
| 577 | talloc_free(tmp); |
| 578 | break; |
| 579 | case CTRL_TYPE_ERROR: |
| 580 | if (!cmd->reply) |
| 581 | goto err; |
| 582 | |
| 583 | tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, |
| 584 | cmd->reply); |
| 585 | if (!tmp) { |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 586 | LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n"); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 587 | goto err; |
| 588 | } |
| 589 | |
| 590 | msg->l2h = msgb_put(msg, strlen(tmp)); |
| 591 | memcpy(msg->l2h, tmp, strlen(tmp)); |
| 592 | talloc_free(tmp); |
| 593 | break; |
| 594 | default: |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 595 | LOGP(DLCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 596 | goto err; |
| 597 | break; |
| 598 | } |
| 599 | |
| 600 | return msg; |
| 601 | |
| 602 | err: |
| 603 | msgb_free(msg); |
| 604 | return NULL; |
| 605 | } |
Harald Welte | 39c9e7b | 2014-08-22 00:28:51 +0200 | [diff] [blame] | 606 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 607 | /*! Build a deferred control command state and keep it the per-connection list of deferred commands. |
| 608 | * This function is typically called by a ctrl command handler that wishes to defer returning a |
| 609 | * response. The reutnred state can later be used to check if the deferred command is still alive, |
| 610 | * and to respond to the specific command. This only works to defer the response to GET and SET. |
| 611 | * \param[in] ctx talloc context from whihc to allocate the ctrl_cmd_def |
| 612 | * \param[in] cmd the control command whose response is deferred |
| 613 | * \param[in] data opaque, user-defined pointer |
| 614 | * \param[in] secs number of seconds until the command times out |
| 615 | * \returns callee-allocated ctrl_cmd_def */ |
Harald Welte | 39c9e7b | 2014-08-22 00:28:51 +0200 | [diff] [blame] | 616 | struct ctrl_cmd_def * |
| 617 | ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs) |
| 618 | { |
| 619 | struct ctrl_cmd_def *cd; |
| 620 | |
| 621 | if (!cmd->ccon) |
| 622 | return NULL; |
| 623 | |
| 624 | cd = talloc_zero(ctx, struct ctrl_cmd_def); |
| 625 | |
Neels Hofmeyr | cdbc9af | 2018-04-03 16:51:49 +0200 | [diff] [blame] | 626 | cmd->defer = cd; |
Harald Welte | 39c9e7b | 2014-08-22 00:28:51 +0200 | [diff] [blame] | 627 | cd->cmd = cmd; |
| 628 | cd->data = data; |
| 629 | |
| 630 | /* add to per-connection list of deferred commands */ |
| 631 | llist_add(&cd->list, &cmd->ccon->def_cmds); |
| 632 | |
| 633 | return cd; |
| 634 | } |
| 635 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 636 | /*! Determine if the given deferred control command is still alive or a zombie. |
| 637 | * \param[in] cd deferred ctrl command state |
| 638 | * \returns 0 is \a cd is still alive; 1 if it's a zombie */ |
Harald Welte | 39c9e7b | 2014-08-22 00:28:51 +0200 | [diff] [blame] | 639 | int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd) |
| 640 | { |
| 641 | /* luckily we're still alive */ |
| 642 | if (cd->cmd) |
| 643 | return 0; |
| 644 | |
| 645 | /* if we are a zombie, make sure we really die */ |
| 646 | llist_del(&cd->list); |
| 647 | talloc_free(cd); |
| 648 | |
| 649 | return 1; |
| 650 | } |
| 651 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 652 | /*! Send the response to a deferred ctrl command. |
| 653 | * The command can only be a resply to a SET or a GET operation. |
| 654 | * \param[in] cd deferred ctrl command state |
| 655 | * \returns 0 if command sent successfully; negative on error */ |
Harald Welte | 39c9e7b | 2014-08-22 00:28:51 +0200 | [diff] [blame] | 656 | int ctrl_cmd_def_send(struct ctrl_cmd_def *cd) |
| 657 | { |
| 658 | struct ctrl_cmd *cmd = cd->cmd; |
| 659 | |
| 660 | int rc; |
| 661 | |
| 662 | /* Deferred commands can only be responses to GET/SET or ERROR, but |
| 663 | * never TRAP or anything else */ |
| 664 | switch (cmd->type) { |
| 665 | case CTRL_TYPE_GET: |
| 666 | cmd->type = CTRL_TYPE_GET_REPLY; |
| 667 | break; |
| 668 | case CTRL_TYPE_SET: |
| 669 | cmd->type = CTRL_TYPE_SET_REPLY; |
| 670 | break; |
| 671 | default: |
| 672 | cmd->type = CTRL_TYPE_ERROR; |
| 673 | } |
| 674 | |
| 675 | rc = ctrl_cmd_send(&cmd->ccon->write_queue, cmd); |
| 676 | |
| 677 | talloc_free(cmd); |
| 678 | llist_del(&cd->list); |
| 679 | talloc_free(cd); |
| 680 | |
| 681 | return rc; |
| 682 | } |