blob: 7a75e0e2259bad7d6b55a400e7dc083229cac421 [file] [log] [blame]
Daniel Willmann4462f8c2010-10-21 15:00:36 +02001/* SNMP-like status interface
2 *
3 * (C) 2010-2011 by Daniel Willmann <daniel@totalueberwachung.de>
4 * (C) 2010-2011 by On-Waves
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <ctype.h>
25#include <errno.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <time.h>
30#include <unistd.h>
31
32#include <openbsc/control_cmd.h>
33#include <openbsc/debug.h>
34#include <openbsc/vty.h>
35
36#include <osmocom/core/msgb.h>
37#include <osmocom/core/talloc.h>
38
39#include <osmocom/vty/command.h>
40#include <osmocom/vty/vector.h>
41
42extern vector ctrl_node_vec;
43
44static struct ctrl_cmd_map ccm[] = {
45 {"GET", CTRL_TYPE_GET},
46 {"SET", CTRL_TYPE_SET},
47 {"GET_REPLY", CTRL_TYPE_GET_REPLY},
48 {"SET_REPLY", CTRL_TYPE_SET_REPLY},
49 {"TRAP", CTRL_TYPE_TRAP},
50 {"ERROR", CTRL_TYPE_ERROR},
51 {NULL}
52};
53
54int ctrl_cmd_str2type(char *s)
55{
56 int i;
57 for (i=0; ccm[i].cmd != NULL; i++) {
58 if (strcasecmp(s, ccm[i].cmd) == 0)
59 return ccm[i].type;
60 }
61 return CTRL_TYPE_UNKNOWN;
62}
63
64char *ctrl_cmd_type2str(int type)
65{
66 int i;
67 for (i=0; ccm[i].cmd != NULL; i++) {
68 if (ccm[i].type == type)
69 return ccm[i].cmd;
70 }
71 return NULL;
72}
73
74/* Functions from libosmocom */
75extern vector cmd_make_descvec(const char *string, const char *descstr);
76
77/* Get the ctrl_cmd_element that matches this command */
78static struct ctrl_cmd_element *ctrl_cmd_get_element_match(vector vline, vector node)
79{
80 int index, j;
81 const char *desc;
82 struct ctrl_cmd_element *cmd_el;
83 struct ctrl_cmd_struct *cmd_desc;
84 char *str;
85
86 for (index = 0; index < vector_active(node); index++) {
87 if ((cmd_el = vector_slot(node, index))) {
88 cmd_desc = &cmd_el->strcmd;
89 if (cmd_desc->nr_commands > vector_active(vline))
90 continue;
91 for (j =0; j < vector_active(vline); j++) {
92 str = vector_slot(vline, j);
93 desc = cmd_desc->command[j];
94 if (desc[0] == '*')
95 return cmd_el; /* Partial match */
96 if (strcmp(desc, str) != 0)
97 break;
98 }
99 /* We went through all the elements and all matched */
100 if (j == cmd_desc->nr_commands)
101 return cmd_el;
102 }
103 }
104
105 return NULL;
106}
107
108int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data)
109{
110 int ret = CTRL_CMD_ERROR;
111 struct ctrl_cmd_element *cmd_el;
112
113 if ((command->type != CTRL_TYPE_GET) && (command->type != CTRL_TYPE_SET)) {
114 command->reply = "Trying to execute something not GET or SET";
115 goto out;
116 }
117 if ((command->type == CTRL_TYPE_SET) && (!command->value)) {
118 command->reply = "SET without a value";
119 goto out;
120 }
121
122 if (!vline)
123 goto out;
124
125 cmd_el = ctrl_cmd_get_element_match(vline, node);
126
127 if (!cmd_el) {
128 command->reply = "Command not found";
129 goto out;
130 }
131
132 if (command->type == CTRL_TYPE_SET) {
133 if (!cmd_el->set) {
134 command->reply = "SET not implemented";
135 goto out;
136 }
137 if (cmd_el->verify) {
138 if ((ret = cmd_el->verify(command, command->value, data))) {
139 ret = CTRL_CMD_ERROR;
Daniel Willmann9cc771b2011-07-28 21:38:51 +0200140 /* If verify() set an appropriate error message, don't change it. */
141 if (!command->reply)
142 command->reply = "Value failed verification.";
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200143 goto out;
144 }
145 } else if (cmd_el->param) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200146 LOGP(DCTRL, LOGL_NOTICE, "Parameter verification unimplemented, continuing without\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200147 }
148 ret = cmd_el->set(command, data);
149 goto out;
150 } else if (command->type == CTRL_TYPE_GET) {
151 if (!cmd_el->get) {
152 command->reply = "GET not implemented";
153 goto out;
154 }
155 ret = cmd_el->get(command, data);
156 goto out;
157 }
158out:
159 if (ret == CTRL_CMD_REPLY) {
160 if (command->type == CTRL_TYPE_SET) {
161 command->type = CTRL_TYPE_SET_REPLY;
162 } else if (command->type == CTRL_TYPE_GET) {
163 command->type = CTRL_TYPE_GET_REPLY;
164 }
165 } else if (ret == CTRL_CMD_ERROR) {
166 command->type = CTRL_TYPE_ERROR;
167 }
168 return ret;
169}
170
171static void add_word(struct ctrl_cmd_struct *cmd,
172 const char *start, const char *end)
173{
174 if (!cmd->command) {
175 cmd->command = talloc_zero_array(tall_vty_vec_ctx,
176 char*, 1);
177 cmd->nr_commands = 0;
178 } else {
179 cmd->command = talloc_realloc(tall_vty_vec_ctx,
180 cmd->command, char*,
181 cmd->nr_commands + 1);
182 }
183
184 cmd->command[cmd->nr_commands++] = talloc_strndup(cmd->command,
185 start, end - start);
186}
187
188static void create_cmd_struct(struct ctrl_cmd_struct *cmd, const char *name)
189{
190 const char *cur, *word;
191
192 for (cur = name, word = NULL; cur[0] != '\0'; ++cur) {
193 /* warn about optionals */
194 if (cur[0] == '(' || cur[0] == ')' || cur[0] == '|') {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200195 LOGP(DCTRL, LOGL_ERROR,
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200196 "Optionals are not supported in '%s'\n", name);
197 goto failure;
198 }
199
200 if (isspace(cur[0])) {
201 if (word) {
202 add_word(cmd, word, cur);
203 word = NULL;
204 }
205 continue;
206 }
207
208 if (!word)
209 word = cur;
210 }
211
212 if (word)
213 add_word(cmd, word, cur);
214
215 return;
216failure:
217 cmd->nr_commands = 0;
218 talloc_free(cmd->command);
219}
220
221int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd)
222{
223 vector cmds_vec;
224
225 cmds_vec = vector_lookup_ensure(ctrl_node_vec, node);
226
227 if (!cmds_vec) {
228 cmds_vec = vector_init(5);
229 if (!cmds_vec) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200230 LOGP(DCTRL, LOGL_ERROR, "vector_init failed.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200231 return -ENOMEM;
232 }
233 vector_set_index(ctrl_node_vec, node, cmds_vec);
234 }
235
236 vector_set(cmds_vec, cmd);
237
238 create_cmd_struct(&cmd->strcmd, cmd->name);
239 return 0;
240}
241
Daniel Willmannfc5391f2011-03-17 15:37:54 +0100242struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd)
243{
244 struct ctrl_cmd *cmd2;
245
246 cmd2 = talloc_zero(ctx, struct ctrl_cmd);
247 if (!cmd2)
248 return NULL;
249
250 cmd2->type = cmd->type;
251 if (cmd->id) {
252 cmd2->id = talloc_strdup(cmd2, cmd->id);
253 if (!cmd2->id)
254 goto err;
255 }
256 if (cmd->variable) {
257 cmd2->variable = talloc_strdup(cmd2, cmd->variable);
258 if (!cmd2->variable)
259 goto err;
260 }
261 if (cmd->value) {
262 cmd2->value = talloc_strdup(cmd2, cmd->value);
263 if (!cmd2->value)
264 goto err;
265 }
266 if (cmd->reply) {
267 cmd2->reply = talloc_strdup(cmd2, cmd->reply);
268 if (!cmd2->reply)
269 goto err;
270 }
271
272 return cmd2;
273err:
274 talloc_free(cmd2);
275 return NULL;
276}
277
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200278struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
279{
280 char *str, *tmp, *saveptr = NULL;
281 char *var, *val;
282 struct ctrl_cmd *cmd;
283
284 cmd = talloc_zero(ctx, struct ctrl_cmd);
285 if (!cmd) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200286 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200287 return NULL;
288 }
289
290 /* Make sure input is NULL terminated */
291 msgb_put_u8(msg, 0);
292 str = (char *) msg->l2h;
293
294 tmp = strtok_r(str, " ", &saveptr);
295 if (!tmp) {
296 cmd->type = CTRL_TYPE_ERROR;
297 cmd->id = "err";
298 cmd->reply = "Request malformed";
299 goto err;
300 }
301
302 cmd->type = ctrl_cmd_str2type(tmp);
303 if (cmd->type == CTRL_TYPE_UNKNOWN) {
304 cmd->type = CTRL_TYPE_ERROR;
305 cmd->id = "err";
306 cmd->reply = "Request type unknown";
307 goto err;
308 }
309
310 tmp = strtok_r(NULL, " ", &saveptr);
311
312 if (!tmp) {
313 cmd->type = CTRL_TYPE_ERROR;
314 cmd->id = "err";
315 cmd->reply = "Missing ID";
316 goto err;
317 }
318 cmd->id = talloc_strdup(cmd, tmp);
319 if (!cmd->id)
320 goto oom;
321
322 switch (cmd->type) {
323 case CTRL_TYPE_GET:
324 var = strtok_r(NULL, " ", &saveptr);
325 if (!var) {
326 cmd->type = CTRL_TYPE_ERROR;
327 cmd->reply = "GET incomplete";
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200328 LOGP(DCTRL, LOGL_NOTICE, "GET Command incomplete\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200329 goto err;
330 }
331 cmd->variable = talloc_strdup(cmd, var);
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200332 LOGP(DCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200333 break;
334 case CTRL_TYPE_SET:
335 var = strtok_r(NULL, " ", &saveptr);
336 val = strtok_r(NULL, " ", &saveptr);
337 if (!var || !val) {
338 cmd->type = CTRL_TYPE_ERROR;
339 cmd->reply = "SET incomplete";
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200340 LOGP(DCTRL, LOGL_NOTICE, "SET Command incomplete\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200341 goto err;
342 }
343 cmd->variable = talloc_strdup(cmd, var);
344 cmd->value = talloc_strdup(cmd, val);
345 if (!cmd->variable || !cmd->value)
346 goto oom;
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200347 LOGP(DCTRL, LOGL_DEBUG, "Command: SET %s = %s\n", cmd->variable, cmd->value);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200348 break;
349 case CTRL_TYPE_GET_REPLY:
350 case CTRL_TYPE_SET_REPLY:
351 case CTRL_TYPE_TRAP:
352 var = strtok_r(NULL, " ", &saveptr);
353 val = strtok_r(NULL, " ", &saveptr);
354 if (!var || !val) {
355 cmd->type = CTRL_TYPE_ERROR;
356 cmd->reply = "Trap/Reply incomplete";
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200357 LOGP(DCTRL, LOGL_NOTICE, "Trap/Reply incomplete\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200358 goto err;
359 }
360 cmd->variable = talloc_strdup(cmd, var);
361 cmd->reply = talloc_strdup(cmd, val);
362 if (!cmd->variable || !cmd->reply)
363 goto oom;
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200364 LOGP(DCTRL, LOGL_DEBUG, "Command: TRAP/REPLY %s: %s\n", cmd->variable, cmd->reply);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200365 break;
366 case CTRL_TYPE_ERROR:
367 var = strtok_r(NULL, "\0", &saveptr);
368 if (!var) {
369 cmd->reply = "";
370 goto err;
371 }
372 cmd->reply = talloc_strdup(cmd, var);
373 if (!cmd->reply)
374 goto oom;
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200375 LOGP(DCTRL, LOGL_DEBUG, "Command: ERROR %s\n", cmd->reply);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200376 break;
377 case CTRL_TYPE_UNKNOWN:
378 default:
379 cmd->type = CTRL_TYPE_ERROR;
380 cmd->reply = "Unknown type";
381 goto err;
382 }
383
384 return cmd;
385oom:
386 cmd->type = CTRL_TYPE_ERROR;
387 cmd->id = "err";
388 cmd->reply = "OOM";
389err:
390 talloc_free(cmd);
391 return NULL;
392}
393
394struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
395{
396 struct msgb *msg;
397 char *type, *tmp;
398
399 if (!cmd->id)
400 return NULL;
401
402 msg = msgb_alloc_headroom(4096, 128, "ctrl command make");
403 if (!msg)
404 return NULL;
405
406 type = ctrl_cmd_type2str(cmd->type);
407
408 switch (cmd->type) {
409 case CTRL_TYPE_GET:
410 if (!cmd->variable)
411 goto err;
412
413 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
414 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200415 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200416 goto err;
417 }
418
419 msg->l2h = msgb_put(msg, strlen(tmp));
420 memcpy(msg->l2h, tmp, strlen(tmp));
421 talloc_free(tmp);
422 break;
423 case CTRL_TYPE_SET:
424 if (!cmd->variable || !cmd->value)
425 goto err;
426
427 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
428 cmd->value);
429 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200430 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200431 goto err;
432 }
433
434 msg->l2h = msgb_put(msg, strlen(tmp));
435 memcpy(msg->l2h, tmp, strlen(tmp));
436 talloc_free(tmp);
437 break;
438 case CTRL_TYPE_GET_REPLY:
439 case CTRL_TYPE_SET_REPLY:
440 case CTRL_TYPE_TRAP:
441 if (!cmd->variable || !cmd->reply)
442 goto err;
443
444 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
445 cmd->reply);
446 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200447 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200448 goto err;
449 }
450
451 msg->l2h = msgb_put(msg, strlen(tmp));
452 memcpy(msg->l2h, tmp, strlen(tmp));
453 talloc_free(tmp);
454 break;
455 case CTRL_TYPE_ERROR:
456 if (!cmd->reply)
457 goto err;
458
459 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id,
460 cmd->reply);
461 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200462 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200463 goto err;
464 }
465
466 msg->l2h = msgb_put(msg, strlen(tmp));
467 memcpy(msg->l2h, tmp, strlen(tmp));
468 talloc_free(tmp);
469 break;
470 default:
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200471 LOGP(DCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200472 goto err;
473 break;
474 }
475
476 return msg;
477
478err:
479 msgb_free(msg);
480 return NULL;
481}