blob: 3c4efc06dce83a2a28a1220a8110565022d9396a [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
Holger Hans Peter Freyther05ac9e32011-08-22 23:44:32 +0200242struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type type)
243{
244 struct ctrl_cmd *cmd;
245
246 cmd = talloc_zero(ctx, struct ctrl_cmd);
247 if (!cmd)
248 return NULL;
249
250 cmd->type = type;
251 return cmd;
252}
253
Daniel Willmannfc5391f2011-03-17 15:37:54 +0100254struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd)
255{
256 struct ctrl_cmd *cmd2;
257
258 cmd2 = talloc_zero(ctx, struct ctrl_cmd);
259 if (!cmd2)
260 return NULL;
261
262 cmd2->type = cmd->type;
263 if (cmd->id) {
264 cmd2->id = talloc_strdup(cmd2, cmd->id);
265 if (!cmd2->id)
266 goto err;
267 }
268 if (cmd->variable) {
269 cmd2->variable = talloc_strdup(cmd2, cmd->variable);
270 if (!cmd2->variable)
271 goto err;
272 }
273 if (cmd->value) {
274 cmd2->value = talloc_strdup(cmd2, cmd->value);
275 if (!cmd2->value)
276 goto err;
277 }
278 if (cmd->reply) {
279 cmd2->reply = talloc_strdup(cmd2, cmd->reply);
280 if (!cmd2->reply)
281 goto err;
282 }
283
284 return cmd2;
285err:
286 talloc_free(cmd2);
287 return NULL;
288}
289
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200290struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
291{
292 char *str, *tmp, *saveptr = NULL;
293 char *var, *val;
294 struct ctrl_cmd *cmd;
295
296 cmd = talloc_zero(ctx, struct ctrl_cmd);
297 if (!cmd) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200298 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200299 return NULL;
300 }
301
302 /* Make sure input is NULL terminated */
303 msgb_put_u8(msg, 0);
304 str = (char *) msg->l2h;
305
306 tmp = strtok_r(str, " ", &saveptr);
307 if (!tmp) {
308 cmd->type = CTRL_TYPE_ERROR;
309 cmd->id = "err";
310 cmd->reply = "Request malformed";
311 goto err;
312 }
313
314 cmd->type = ctrl_cmd_str2type(tmp);
315 if (cmd->type == CTRL_TYPE_UNKNOWN) {
316 cmd->type = CTRL_TYPE_ERROR;
317 cmd->id = "err";
318 cmd->reply = "Request type unknown";
319 goto err;
320 }
321
322 tmp = strtok_r(NULL, " ", &saveptr);
323
324 if (!tmp) {
325 cmd->type = CTRL_TYPE_ERROR;
326 cmd->id = "err";
327 cmd->reply = "Missing ID";
328 goto err;
329 }
330 cmd->id = talloc_strdup(cmd, tmp);
331 if (!cmd->id)
332 goto oom;
333
334 switch (cmd->type) {
335 case CTRL_TYPE_GET:
336 var = strtok_r(NULL, " ", &saveptr);
337 if (!var) {
338 cmd->type = CTRL_TYPE_ERROR;
339 cmd->reply = "GET incomplete";
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200340 LOGP(DCTRL, LOGL_NOTICE, "GET Command incomplete\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200341 goto err;
342 }
343 cmd->variable = talloc_strdup(cmd, var);
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200344 LOGP(DCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200345 break;
346 case CTRL_TYPE_SET:
347 var = strtok_r(NULL, " ", &saveptr);
348 val = strtok_r(NULL, " ", &saveptr);
349 if (!var || !val) {
350 cmd->type = CTRL_TYPE_ERROR;
351 cmd->reply = "SET incomplete";
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200352 LOGP(DCTRL, LOGL_NOTICE, "SET Command incomplete\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200353 goto err;
354 }
355 cmd->variable = talloc_strdup(cmd, var);
356 cmd->value = talloc_strdup(cmd, val);
357 if (!cmd->variable || !cmd->value)
358 goto oom;
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200359 LOGP(DCTRL, LOGL_DEBUG, "Command: SET %s = %s\n", cmd->variable, cmd->value);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200360 break;
361 case CTRL_TYPE_GET_REPLY:
362 case CTRL_TYPE_SET_REPLY:
363 case CTRL_TYPE_TRAP:
364 var = strtok_r(NULL, " ", &saveptr);
365 val = strtok_r(NULL, " ", &saveptr);
366 if (!var || !val) {
367 cmd->type = CTRL_TYPE_ERROR;
368 cmd->reply = "Trap/Reply incomplete";
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200369 LOGP(DCTRL, LOGL_NOTICE, "Trap/Reply incomplete\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200370 goto err;
371 }
372 cmd->variable = talloc_strdup(cmd, var);
373 cmd->reply = talloc_strdup(cmd, val);
374 if (!cmd->variable || !cmd->reply)
375 goto oom;
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200376 LOGP(DCTRL, LOGL_DEBUG, "Command: TRAP/REPLY %s: %s\n", cmd->variable, cmd->reply);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200377 break;
378 case CTRL_TYPE_ERROR:
379 var = strtok_r(NULL, "\0", &saveptr);
380 if (!var) {
381 cmd->reply = "";
382 goto err;
383 }
384 cmd->reply = talloc_strdup(cmd, var);
385 if (!cmd->reply)
386 goto oom;
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200387 LOGP(DCTRL, LOGL_DEBUG, "Command: ERROR %s\n", cmd->reply);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200388 break;
389 case CTRL_TYPE_UNKNOWN:
390 default:
391 cmd->type = CTRL_TYPE_ERROR;
392 cmd->reply = "Unknown type";
393 goto err;
394 }
395
396 return cmd;
397oom:
398 cmd->type = CTRL_TYPE_ERROR;
399 cmd->id = "err";
400 cmd->reply = "OOM";
401err:
402 talloc_free(cmd);
403 return NULL;
404}
405
406struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
407{
408 struct msgb *msg;
409 char *type, *tmp;
410
411 if (!cmd->id)
412 return NULL;
413
414 msg = msgb_alloc_headroom(4096, 128, "ctrl command make");
415 if (!msg)
416 return NULL;
417
418 type = ctrl_cmd_type2str(cmd->type);
419
420 switch (cmd->type) {
421 case CTRL_TYPE_GET:
422 if (!cmd->variable)
423 goto err;
424
425 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
426 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200427 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200428 goto err;
429 }
430
431 msg->l2h = msgb_put(msg, strlen(tmp));
432 memcpy(msg->l2h, tmp, strlen(tmp));
433 talloc_free(tmp);
434 break;
435 case CTRL_TYPE_SET:
436 if (!cmd->variable || !cmd->value)
437 goto err;
438
439 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
440 cmd->value);
441 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200442 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200443 goto err;
444 }
445
446 msg->l2h = msgb_put(msg, strlen(tmp));
447 memcpy(msg->l2h, tmp, strlen(tmp));
448 talloc_free(tmp);
449 break;
450 case CTRL_TYPE_GET_REPLY:
451 case CTRL_TYPE_SET_REPLY:
452 case CTRL_TYPE_TRAP:
453 if (!cmd->variable || !cmd->reply)
454 goto err;
455
456 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
457 cmd->reply);
458 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200459 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200460 goto err;
461 }
462
463 msg->l2h = msgb_put(msg, strlen(tmp));
464 memcpy(msg->l2h, tmp, strlen(tmp));
465 talloc_free(tmp);
466 break;
467 case CTRL_TYPE_ERROR:
468 if (!cmd->reply)
469 goto err;
470
471 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id,
472 cmd->reply);
473 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200474 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200475 goto err;
476 }
477
478 msg->l2h = msgb_put(msg, strlen(tmp));
479 memcpy(msg->l2h, tmp, strlen(tmp));
480 talloc_free(tmp);
481 break;
482 default:
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200483 LOGP(DCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200484 goto err;
485 break;
486 }
487
488 return msg;
489
490err:
491 msgb_free(msg);
492 return NULL;
493}