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 | |
| 213 | cmds_vec = vector_lookup_ensure(ctrl_node_vec, node); |
| 214 | |
| 215 | if (!cmds_vec) { |
| 216 | cmds_vec = vector_init(5); |
| 217 | if (!cmds_vec) { |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 218 | LOGP(DLCTRL, LOGL_ERROR, "vector_init failed.\n"); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 219 | return -ENOMEM; |
| 220 | } |
| 221 | vector_set_index(ctrl_node_vec, node, cmds_vec); |
| 222 | } |
| 223 | |
| 224 | vector_set(cmds_vec, cmd); |
| 225 | |
| 226 | create_cmd_struct(&cmd->strcmd, cmd->name); |
| 227 | return 0; |
| 228 | } |
| 229 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 230 | /*! Allocate a control command of given \a type. |
| 231 | * \param[in] ctx talloc context from which to allocate |
| 232 | * \param[in] type command type to set after allocation |
| 233 | * \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] | 234 | struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type type) |
| 235 | { |
| 236 | struct ctrl_cmd *cmd; |
| 237 | |
| 238 | cmd = talloc_zero(ctx, struct ctrl_cmd); |
| 239 | if (!cmd) |
| 240 | return NULL; |
| 241 | |
| 242 | cmd->type = type; |
| 243 | return cmd; |
| 244 | } |
| 245 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 246 | /*! Perform a deepl copy of the given \a cmd, allocating memory from \a ctx. |
| 247 | * \param[in] ctx talloc context from which to allocate |
| 248 | * \param[in cmd CTRL command to be copied |
| 249 | * \returns deep copy of \a cmd on success; NULL on error */ |
Daniel Willmann | 8b7a962 | 2011-03-17 15:37:54 +0100 | [diff] [blame] | 250 | struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd) |
| 251 | { |
| 252 | struct ctrl_cmd *cmd2; |
| 253 | |
| 254 | cmd2 = talloc_zero(ctx, struct ctrl_cmd); |
| 255 | if (!cmd2) |
| 256 | return NULL; |
| 257 | |
| 258 | cmd2->type = cmd->type; |
| 259 | if (cmd->id) { |
| 260 | cmd2->id = talloc_strdup(cmd2, cmd->id); |
| 261 | if (!cmd2->id) |
| 262 | goto err; |
| 263 | } |
| 264 | if (cmd->variable) { |
| 265 | cmd2->variable = talloc_strdup(cmd2, cmd->variable); |
| 266 | if (!cmd2->variable) |
| 267 | goto err; |
| 268 | } |
| 269 | if (cmd->value) { |
| 270 | cmd2->value = talloc_strdup(cmd2, cmd->value); |
| 271 | if (!cmd2->value) |
| 272 | goto err; |
| 273 | } |
| 274 | if (cmd->reply) { |
| 275 | cmd2->reply = talloc_strdup(cmd2, cmd->reply); |
| 276 | if (!cmd2->reply) |
| 277 | goto err; |
| 278 | } |
| 279 | |
| 280 | return cmd2; |
| 281 | err: |
| 282 | talloc_free(cmd2); |
| 283 | return NULL; |
| 284 | } |
| 285 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 286 | /*! Parse/Decode CTRL from \ref msgb into command struct. |
| 287 | * \param[in] ctx talloc context from which to allocate |
| 288 | * \param[in] msg message buffer containing command to be decoded |
| 289 | * \returns callee-allocated decoded CTRL command; NULL on allocation or other failure |
Neels Hofmeyr | f2e83ad | 2017-12-16 01:05:25 +0100 | [diff] [blame] | 290 | * The caller is responsible to talloc_free() the returned struct pointer. */ |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 291 | struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg) |
| 292 | { |
Neels Hofmeyr | f2e83ad | 2017-12-16 01:05:25 +0100 | [diff] [blame] | 293 | struct ctrl_cmd *res = ctrl_cmd_parse2(ctx, msg); |
| 294 | if (res->type == CTRL_TYPE_ERROR) { |
| 295 | talloc_free(res); |
| 296 | return NULL; |
| 297 | } |
| 298 | return res; |
| 299 | } |
| 300 | |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 301 | static bool id_str_valid(const char *str) |
| 302 | { |
| 303 | for (;*str;str++) { |
| 304 | if (!isdigit(*str)) |
| 305 | return false; |
| 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 310 | /*! Parse/Decode CTRL from \ref msgb into command struct. |
| 311 | * \param[in] ctx talloc context from which to allocate |
| 312 | * \param[in] msg message buffer containing command to be decoded |
| 313 | * \returns callee-allocated decoded CTRL command; NULL on allocation failure, |
| 314 | * 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] | 315 | * The caller is responsible to talloc_free() the returned struct pointer. |
| 316 | * If information of the origin of the ERROR cmd returned is required (received |
| 317 | * or local parsing failure), use \ref ctrl_cmd_parse3 instead. */ |
Neels Hofmeyr | f2e83ad | 2017-12-16 01:05:25 +0100 | [diff] [blame] | 318 | struct ctrl_cmd *ctrl_cmd_parse2(void *ctx, struct msgb *msg) |
| 319 | { |
Pau Espin Pedrol | 239ed3b | 2018-07-12 17:51:16 +0200 | [diff] [blame] | 320 | bool unused; |
| 321 | return ctrl_cmd_parse3(ctx, msg, &unused); |
| 322 | } |
| 323 | |
| 324 | /*! Parse/Decode CTRL from \ref msgb into command struct. |
| 325 | * \param[in] ctx talloc context from which to allocate |
| 326 | * \param[in] msg message buffer containing command to be decoded |
| 327 | * \param[out] parse_failed Whether returned ERROR cmd was generatd locally |
| 328 | * (due to parse failure) or was received. |
| 329 | * \returns callee-allocated decoded CTRL command; NULL on allocation failure, |
| 330 | * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error. |
| 331 | * The caller is responsible to talloc_free() the returned struct pointer. */ |
| 332 | struct ctrl_cmd *ctrl_cmd_parse3(void *ctx, struct msgb *msg, bool *parse_failed) |
| 333 | { |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 334 | char *str, *tmp, *saveptr = NULL; |
| 335 | char *var, *val; |
| 336 | struct ctrl_cmd *cmd; |
| 337 | |
| 338 | cmd = talloc_zero(ctx, struct ctrl_cmd); |
| 339 | if (!cmd) { |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 340 | LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n"); |
Pau Espin Pedrol | 239ed3b | 2018-07-12 17:51:16 +0200 | [diff] [blame] | 341 | *parse_failed = true; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | /* Make sure input is NULL terminated */ |
| 346 | msgb_put_u8(msg, 0); |
| 347 | str = (char *) msg->l2h; |
| 348 | |
Harald Welte | edf6fe7 | 2016-11-26 10:06:07 +0100 | [diff] [blame] | 349 | OSMO_ASSERT(str); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 350 | tmp = strtok_r(str, " ", &saveptr); |
| 351 | if (!tmp) { |
| 352 | cmd->type = CTRL_TYPE_ERROR; |
| 353 | cmd->id = "err"; |
| 354 | cmd->reply = "Request malformed"; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 355 | 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] | 356 | goto err; |
| 357 | } |
| 358 | |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 359 | cmd->type = get_string_value(ctrl_type_vals, tmp); |
Pau Espin Pedrol | 8aa85bd | 2017-06-23 12:43:31 +0200 | [diff] [blame] | 360 | if ((int)cmd->type < 0 || cmd->type == CTRL_TYPE_UNKNOWN) { |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 361 | cmd->type = CTRL_TYPE_ERROR; |
| 362 | cmd->id = "err"; |
| 363 | cmd->reply = "Request type unknown"; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 364 | 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] | 365 | goto err; |
| 366 | } |
| 367 | |
| 368 | tmp = strtok_r(NULL, " ", &saveptr); |
| 369 | |
| 370 | if (!tmp) { |
| 371 | cmd->type = CTRL_TYPE_ERROR; |
| 372 | cmd->id = "err"; |
| 373 | cmd->reply = "Missing ID"; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 374 | LOGP(DLCTRL, LOGL_NOTICE, "Missing ID: \"%s\"\n", osmo_escape_str(str, -1)); |
| 375 | goto err; |
| 376 | } |
| 377 | |
Pau Espin Pedrol | 40ad913 | 2018-07-12 17:41:55 +0200 | [diff] [blame] | 378 | if (!id_str_valid(tmp) && |
| 379 | !(cmd->type == CTRL_TYPE_ERROR && strcmp(tmp, "err") == 0)) { |
Pau Espin Pedrol | 55088b7 | 2018-07-12 13:03:52 +0200 | [diff] [blame] | 380 | LOGP(DLCTRL, LOGL_NOTICE, "Invalid %s message ID number: \"%s\"\n", |
| 381 | 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] | 382 | cmd->type = CTRL_TYPE_ERROR; |
| 383 | cmd->id = "err"; |
| 384 | cmd->reply = "Invalid message ID number"; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 385 | goto err; |
| 386 | } |
| 387 | cmd->id = talloc_strdup(cmd, tmp); |
| 388 | if (!cmd->id) |
| 389 | goto oom; |
| 390 | |
| 391 | switch (cmd->type) { |
| 392 | case CTRL_TYPE_GET: |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 393 | var = strtok_r(NULL, " \n", &saveptr); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 394 | if (!var) { |
| 395 | cmd->type = CTRL_TYPE_ERROR; |
| 396 | cmd->reply = "GET incomplete"; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 397 | LOGP(DLCTRL, LOGL_NOTICE, "GET Command incomplete: \"%s\"\n", |
| 398 | osmo_escape_str(str, -1)); |
| 399 | goto err; |
| 400 | } |
| 401 | if (!osmo_separated_identifiers_valid(var, ".")) { |
| 402 | cmd->type = CTRL_TYPE_ERROR; |
| 403 | cmd->reply = "GET variable contains invalid characters"; |
| 404 | LOGP(DLCTRL, LOGL_NOTICE, "GET variable contains invalid characters: \"%s\"\n", |
| 405 | osmo_escape_str(var, -1)); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 406 | goto err; |
| 407 | } |
| 408 | cmd->variable = talloc_strdup(cmd, var); |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 409 | var = strtok_r(NULL, "", &saveptr); |
| 410 | if (var) { |
| 411 | cmd->type = CTRL_TYPE_ERROR; |
| 412 | cmd->reply = "GET with trailing characters"; |
| 413 | LOGP(DLCTRL, LOGL_NOTICE, "GET with trailing characters: \"%s\"\n", |
| 414 | osmo_escape_str(var, -1)); |
| 415 | goto err; |
| 416 | } |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 417 | LOGP(DLCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 418 | break; |
| 419 | case CTRL_TYPE_SET: |
| 420 | var = strtok_r(NULL, " ", &saveptr); |
Holger Hans Peter Freyther | 5ad742d | 2014-08-08 19:41:28 +0200 | [diff] [blame] | 421 | val = strtok_r(NULL, "\n", &saveptr); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 422 | if (!var || !val) { |
| 423 | cmd->type = CTRL_TYPE_ERROR; |
| 424 | cmd->reply = "SET incomplete"; |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 425 | LOGP(DLCTRL, LOGL_NOTICE, "SET Command incomplete\n"); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 426 | goto err; |
| 427 | } |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 428 | if (!osmo_separated_identifiers_valid(var, ".")) { |
| 429 | cmd->type = CTRL_TYPE_ERROR; |
| 430 | cmd->reply = "SET variable contains invalid characters"; |
| 431 | LOGP(DLCTRL, LOGL_NOTICE, "SET variable contains invalid characters: \"%s\"\n", |
| 432 | osmo_escape_str(var, -1)); |
| 433 | goto err; |
| 434 | } |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 435 | cmd->variable = talloc_strdup(cmd, var); |
| 436 | cmd->value = talloc_strdup(cmd, val); |
| 437 | if (!cmd->variable || !cmd->value) |
| 438 | goto oom; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 439 | |
| 440 | var = strtok_r(NULL, "", &saveptr); |
| 441 | if (var) { |
| 442 | cmd->type = CTRL_TYPE_ERROR; |
| 443 | cmd->reply = "SET with trailing characters"; |
| 444 | LOGP(DLCTRL, LOGL_NOTICE, "SET with trailing characters: \"%s\"\n", |
| 445 | osmo_escape_str(var, -1)); |
| 446 | goto err; |
| 447 | } |
| 448 | |
| 449 | LOGP(DLCTRL, LOGL_DEBUG, "Command: SET %s = \"%s\"\n", cmd->variable, |
| 450 | osmo_escape_str(cmd->value, -1)); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 451 | break; |
Neels Hofmeyr | a5e2162 | 2017-12-16 01:18:53 +0100 | [diff] [blame] | 452 | #define REPLY_CASE(TYPE, NAME) \ |
| 453 | case TYPE: \ |
| 454 | var = strtok_r(NULL, " ", &saveptr); \ |
| 455 | val = strtok_r(NULL, "", &saveptr); \ |
| 456 | if (!var) { \ |
| 457 | cmd->type = CTRL_TYPE_ERROR; \ |
| 458 | cmd->reply = NAME " incomplete"; \ |
| 459 | LOGP(DLCTRL, LOGL_NOTICE, NAME " incomplete\n"); \ |
| 460 | goto err; \ |
| 461 | } \ |
| 462 | if (!osmo_separated_identifiers_valid(var, ".")) { \ |
| 463 | cmd->type = CTRL_TYPE_ERROR; \ |
| 464 | cmd->reply = NAME " variable contains invalid characters"; \ |
| 465 | LOGP(DLCTRL, LOGL_NOTICE, NAME " variable contains invalid characters: \"%s\"\n", \ |
| 466 | osmo_escape_str(var, -1)); \ |
| 467 | goto err; \ |
| 468 | } \ |
| 469 | cmd->variable = talloc_strdup(cmd, var); \ |
| 470 | cmd->reply = talloc_strdup(cmd, val); \ |
| 471 | if (!cmd->variable || !cmd->reply) \ |
| 472 | goto oom; \ |
| 473 | LOGP(DLCTRL, LOGL_DEBUG, "Command: " NAME " %s: %s\n", cmd->variable, \ |
| 474 | osmo_escape_str(cmd->reply, -1)); \ |
| 475 | break |
| 476 | REPLY_CASE(CTRL_TYPE_GET_REPLY, "GET REPLY"); |
| 477 | REPLY_CASE(CTRL_TYPE_SET_REPLY, "SET REPLY"); |
| 478 | REPLY_CASE(CTRL_TYPE_TRAP, "TRAP"); |
| 479 | #undef REPLY_CASE |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 480 | case CTRL_TYPE_ERROR: |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 481 | var = strtok_r(NULL, "", &saveptr); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 482 | if (!var) { |
| 483 | cmd->reply = ""; |
| 484 | goto err; |
| 485 | } |
| 486 | cmd->reply = talloc_strdup(cmd, var); |
| 487 | if (!cmd->reply) |
| 488 | goto oom; |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 489 | LOGP(DLCTRL, LOGL_DEBUG, "Command: ERROR \"%s\"\n", |
| 490 | osmo_escape_str(cmd->reply, -1)); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 491 | break; |
| 492 | case CTRL_TYPE_UNKNOWN: |
| 493 | default: |
| 494 | cmd->type = CTRL_TYPE_ERROR; |
| 495 | cmd->reply = "Unknown type"; |
| 496 | goto err; |
| 497 | } |
| 498 | |
Pau Espin Pedrol | 239ed3b | 2018-07-12 17:51:16 +0200 | [diff] [blame] | 499 | *parse_failed = false; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 500 | return cmd; |
| 501 | oom: |
| 502 | cmd->type = CTRL_TYPE_ERROR; |
| 503 | cmd->id = "err"; |
| 504 | cmd->reply = "OOM"; |
| 505 | err: |
Pau Espin Pedrol | 239ed3b | 2018-07-12 17:51:16 +0200 | [diff] [blame] | 506 | *parse_failed = true; |
Neels Hofmeyr | f2e83ad | 2017-12-16 01:05:25 +0100 | [diff] [blame] | 507 | return cmd; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 510 | /*! Encode a given CTRL command from its parsed form into a message buffer. |
| 511 | * \param[in] cmd decoded/parsed form of to-be-encoded command |
| 512 | * \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] | 513 | struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd) |
| 514 | { |
Pau Espin Pedrol | f5b8ed1 | 2021-06-10 13:15:20 +0200 | [diff] [blame] | 515 | struct msgb *msg = NULL; |
| 516 | char *strbuf; |
| 517 | size_t len; |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 518 | const char *type; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 519 | |
| 520 | if (!cmd->id) |
| 521 | return NULL; |
| 522 | |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 523 | type = get_value_string(ctrl_type_vals, cmd->type); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 524 | |
| 525 | switch (cmd->type) { |
| 526 | case CTRL_TYPE_GET: |
| 527 | if (!cmd->variable) |
Pau Espin Pedrol | f5b8ed1 | 2021-06-10 13:15:20 +0200 | [diff] [blame] | 528 | return NULL; |
| 529 | strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 530 | break; |
| 531 | case CTRL_TYPE_SET: |
| 532 | if (!cmd->variable || !cmd->value) |
Pau Espin Pedrol | f5b8ed1 | 2021-06-10 13:15:20 +0200 | [diff] [blame] | 533 | return NULL; |
| 534 | strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, |
| 535 | cmd->variable, cmd->value); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 536 | break; |
| 537 | case CTRL_TYPE_GET_REPLY: |
| 538 | case CTRL_TYPE_SET_REPLY: |
| 539 | case CTRL_TYPE_TRAP: |
| 540 | if (!cmd->variable || !cmd->reply) |
Pau Espin Pedrol | f5b8ed1 | 2021-06-10 13:15:20 +0200 | [diff] [blame] | 541 | return NULL; |
| 542 | strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, |
| 543 | cmd->variable, cmd->reply); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 544 | break; |
| 545 | case CTRL_TYPE_ERROR: |
| 546 | if (!cmd->reply) |
Pau Espin Pedrol | f5b8ed1 | 2021-06-10 13:15:20 +0200 | [diff] [blame] | 547 | return NULL; |
| 548 | strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->reply); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 549 | break; |
| 550 | default: |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 551 | LOGP(DLCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type); |
Pau Espin Pedrol | f5b8ed1 | 2021-06-10 13:15:20 +0200 | [diff] [blame] | 552 | return NULL; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Pau Espin Pedrol | f5b8ed1 | 2021-06-10 13:15:20 +0200 | [diff] [blame] | 555 | if (!strbuf) { |
| 556 | LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n"); |
| 557 | goto ret; |
| 558 | } |
| 559 | len = strlen(strbuf); |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 560 | |
Pau Espin Pedrol | f5b8ed1 | 2021-06-10 13:15:20 +0200 | [diff] [blame] | 561 | msg = msgb_alloc_headroom(len + 128, 128, "ctrl ERROR command make"); |
| 562 | if (!msg) |
| 563 | goto ret; |
| 564 | msg->l2h = msgb_put(msg, len); |
| 565 | memcpy(msg->l2h, strbuf, len); |
| 566 | |
| 567 | ret: |
| 568 | talloc_free(strbuf); |
| 569 | return msg; |
Daniel Willmann | 1264cb4 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 570 | } |
Harald Welte | 39c9e7b | 2014-08-22 00:28:51 +0200 | [diff] [blame] | 571 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 572 | /*! Build a deferred control command state and keep it the per-connection list of deferred commands. |
| 573 | * This function is typically called by a ctrl command handler that wishes to defer returning a |
| 574 | * response. The reutnred state can later be used to check if the deferred command is still alive, |
| 575 | * and to respond to the specific command. This only works to defer the response to GET and SET. |
| 576 | * \param[in] ctx talloc context from whihc to allocate the ctrl_cmd_def |
| 577 | * \param[in] cmd the control command whose response is deferred |
| 578 | * \param[in] data opaque, user-defined pointer |
| 579 | * \param[in] secs number of seconds until the command times out |
| 580 | * \returns callee-allocated ctrl_cmd_def */ |
Harald Welte | 39c9e7b | 2014-08-22 00:28:51 +0200 | [diff] [blame] | 581 | struct ctrl_cmd_def * |
| 582 | ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs) |
| 583 | { |
| 584 | struct ctrl_cmd_def *cd; |
| 585 | |
| 586 | if (!cmd->ccon) |
| 587 | return NULL; |
| 588 | |
| 589 | cd = talloc_zero(ctx, struct ctrl_cmd_def); |
| 590 | |
Neels Hofmeyr | cdbc9af | 2018-04-03 16:51:49 +0200 | [diff] [blame] | 591 | cmd->defer = cd; |
Harald Welte | 39c9e7b | 2014-08-22 00:28:51 +0200 | [diff] [blame] | 592 | cd->cmd = cmd; |
| 593 | cd->data = data; |
| 594 | |
| 595 | /* add to per-connection list of deferred commands */ |
| 596 | llist_add(&cd->list, &cmd->ccon->def_cmds); |
| 597 | |
| 598 | return cd; |
| 599 | } |
| 600 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 601 | /*! Determine if the given deferred control command is still alive or a zombie. |
| 602 | * \param[in] cd deferred ctrl command state |
| 603 | * \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] | 604 | int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd) |
| 605 | { |
| 606 | /* luckily we're still alive */ |
| 607 | if (cd->cmd) |
| 608 | return 0; |
| 609 | |
| 610 | /* if we are a zombie, make sure we really die */ |
| 611 | llist_del(&cd->list); |
| 612 | talloc_free(cd); |
| 613 | |
| 614 | return 1; |
| 615 | } |
| 616 | |
Harald Welte | b418682 | 2018-05-26 17:25:11 +0200 | [diff] [blame] | 617 | /*! Send the response to a deferred ctrl command. |
| 618 | * The command can only be a resply to a SET or a GET operation. |
| 619 | * \param[in] cd deferred ctrl command state |
| 620 | * \returns 0 if command sent successfully; negative on error */ |
Harald Welte | 39c9e7b | 2014-08-22 00:28:51 +0200 | [diff] [blame] | 621 | int ctrl_cmd_def_send(struct ctrl_cmd_def *cd) |
| 622 | { |
| 623 | struct ctrl_cmd *cmd = cd->cmd; |
| 624 | |
| 625 | int rc; |
| 626 | |
| 627 | /* Deferred commands can only be responses to GET/SET or ERROR, but |
| 628 | * never TRAP or anything else */ |
| 629 | switch (cmd->type) { |
| 630 | case CTRL_TYPE_GET: |
| 631 | cmd->type = CTRL_TYPE_GET_REPLY; |
| 632 | break; |
| 633 | case CTRL_TYPE_SET: |
| 634 | cmd->type = CTRL_TYPE_SET_REPLY; |
| 635 | break; |
| 636 | default: |
| 637 | cmd->type = CTRL_TYPE_ERROR; |
| 638 | } |
| 639 | |
| 640 | rc = ctrl_cmd_send(&cmd->ccon->write_queue, cmd); |
| 641 | |
| 642 | talloc_free(cmd); |
| 643 | llist_del(&cd->list); |
| 644 | talloc_free(cd); |
| 645 | |
| 646 | return rc; |
| 647 | } |