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