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