blob: 44cfa4851363586c6c0526222e7900dfe16a696f [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 }
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200145 }
146 ret = cmd_el->set(command, data);
147 goto out;
148 } else if (command->type == CTRL_TYPE_GET) {
149 if (!cmd_el->get) {
150 command->reply = "GET not implemented";
151 goto out;
152 }
153 ret = cmd_el->get(command, data);
154 goto out;
155 }
156out:
157 if (ret == CTRL_CMD_REPLY) {
158 if (command->type == CTRL_TYPE_SET) {
159 command->type = CTRL_TYPE_SET_REPLY;
160 } else if (command->type == CTRL_TYPE_GET) {
161 command->type = CTRL_TYPE_GET_REPLY;
162 }
163 } else if (ret == CTRL_CMD_ERROR) {
164 command->type = CTRL_TYPE_ERROR;
165 }
166 return ret;
167}
168
169static void add_word(struct ctrl_cmd_struct *cmd,
170 const char *start, const char *end)
171{
172 if (!cmd->command) {
173 cmd->command = talloc_zero_array(tall_vty_vec_ctx,
174 char*, 1);
175 cmd->nr_commands = 0;
176 } else {
177 cmd->command = talloc_realloc(tall_vty_vec_ctx,
178 cmd->command, char*,
179 cmd->nr_commands + 1);
180 }
181
182 cmd->command[cmd->nr_commands++] = talloc_strndup(cmd->command,
183 start, end - start);
184}
185
186static void create_cmd_struct(struct ctrl_cmd_struct *cmd, const char *name)
187{
188 const char *cur, *word;
189
190 for (cur = name, word = NULL; cur[0] != '\0'; ++cur) {
191 /* warn about optionals */
192 if (cur[0] == '(' || cur[0] == ')' || cur[0] == '|') {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200193 LOGP(DCTRL, LOGL_ERROR,
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200194 "Optionals are not supported in '%s'\n", name);
195 goto failure;
196 }
197
198 if (isspace(cur[0])) {
199 if (word) {
200 add_word(cmd, word, cur);
201 word = NULL;
202 }
203 continue;
204 }
205
206 if (!word)
207 word = cur;
208 }
209
210 if (word)
211 add_word(cmd, word, cur);
212
213 return;
214failure:
215 cmd->nr_commands = 0;
216 talloc_free(cmd->command);
217}
218
219int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd)
220{
221 vector cmds_vec;
222
223 cmds_vec = vector_lookup_ensure(ctrl_node_vec, node);
224
225 if (!cmds_vec) {
226 cmds_vec = vector_init(5);
227 if (!cmds_vec) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200228 LOGP(DCTRL, LOGL_ERROR, "vector_init failed.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200229 return -ENOMEM;
230 }
231 vector_set_index(ctrl_node_vec, node, cmds_vec);
232 }
233
234 vector_set(cmds_vec, cmd);
235
236 create_cmd_struct(&cmd->strcmd, cmd->name);
237 return 0;
238}
239
Holger Hans Peter Freyther05ac9e32011-08-22 23:44:32 +0200240struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type type)
241{
242 struct ctrl_cmd *cmd;
243
244 cmd = talloc_zero(ctx, struct ctrl_cmd);
245 if (!cmd)
246 return NULL;
247
248 cmd->type = type;
249 return cmd;
250}
251
Daniel Willmannfc5391f2011-03-17 15:37:54 +0100252struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd)
253{
254 struct ctrl_cmd *cmd2;
255
256 cmd2 = talloc_zero(ctx, struct ctrl_cmd);
257 if (!cmd2)
258 return NULL;
259
260 cmd2->type = cmd->type;
261 if (cmd->id) {
262 cmd2->id = talloc_strdup(cmd2, cmd->id);
263 if (!cmd2->id)
264 goto err;
265 }
266 if (cmd->variable) {
267 cmd2->variable = talloc_strdup(cmd2, cmd->variable);
268 if (!cmd2->variable)
269 goto err;
270 }
271 if (cmd->value) {
272 cmd2->value = talloc_strdup(cmd2, cmd->value);
273 if (!cmd2->value)
274 goto err;
275 }
276 if (cmd->reply) {
277 cmd2->reply = talloc_strdup(cmd2, cmd->reply);
278 if (!cmd2->reply)
279 goto err;
280 }
281
282 return cmd2;
283err:
284 talloc_free(cmd2);
285 return NULL;
286}
287
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200288struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
289{
290 char *str, *tmp, *saveptr = NULL;
291 char *var, *val;
292 struct ctrl_cmd *cmd;
293
294 cmd = talloc_zero(ctx, struct ctrl_cmd);
295 if (!cmd) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200296 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200297 return NULL;
298 }
299
300 /* Make sure input is NULL terminated */
301 msgb_put_u8(msg, 0);
302 str = (char *) msg->l2h;
303
304 tmp = strtok_r(str, " ", &saveptr);
305 if (!tmp) {
306 cmd->type = CTRL_TYPE_ERROR;
307 cmd->id = "err";
308 cmd->reply = "Request malformed";
309 goto err;
310 }
311
312 cmd->type = ctrl_cmd_str2type(tmp);
313 if (cmd->type == CTRL_TYPE_UNKNOWN) {
314 cmd->type = CTRL_TYPE_ERROR;
315 cmd->id = "err";
316 cmd->reply = "Request type unknown";
317 goto err;
318 }
319
320 tmp = strtok_r(NULL, " ", &saveptr);
321
322 if (!tmp) {
323 cmd->type = CTRL_TYPE_ERROR;
324 cmd->id = "err";
325 cmd->reply = "Missing ID";
326 goto err;
327 }
328 cmd->id = talloc_strdup(cmd, tmp);
329 if (!cmd->id)
330 goto oom;
331
332 switch (cmd->type) {
333 case CTRL_TYPE_GET:
334 var = strtok_r(NULL, " ", &saveptr);
335 if (!var) {
336 cmd->type = CTRL_TYPE_ERROR;
337 cmd->reply = "GET incomplete";
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200338 LOGP(DCTRL, LOGL_NOTICE, "GET Command incomplete\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200339 goto err;
340 }
341 cmd->variable = talloc_strdup(cmd, var);
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200342 LOGP(DCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200343 break;
344 case CTRL_TYPE_SET:
345 var = strtok_r(NULL, " ", &saveptr);
Holger Hans Peter Freyther350de9f2014-08-08 19:41:28 +0200346 val = strtok_r(NULL, "\n", &saveptr);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200347 if (!var || !val) {
348 cmd->type = CTRL_TYPE_ERROR;
349 cmd->reply = "SET incomplete";
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200350 LOGP(DCTRL, LOGL_NOTICE, "SET Command incomplete\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200351 goto err;
352 }
353 cmd->variable = talloc_strdup(cmd, var);
354 cmd->value = talloc_strdup(cmd, val);
355 if (!cmd->variable || !cmd->value)
356 goto oom;
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200357 LOGP(DCTRL, LOGL_DEBUG, "Command: SET %s = %s\n", cmd->variable, cmd->value);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200358 break;
359 case CTRL_TYPE_GET_REPLY:
360 case CTRL_TYPE_SET_REPLY:
361 case CTRL_TYPE_TRAP:
362 var = strtok_r(NULL, " ", &saveptr);
363 val = strtok_r(NULL, " ", &saveptr);
364 if (!var || !val) {
365 cmd->type = CTRL_TYPE_ERROR;
366 cmd->reply = "Trap/Reply incomplete";
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200367 LOGP(DCTRL, LOGL_NOTICE, "Trap/Reply incomplete\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200368 goto err;
369 }
370 cmd->variable = talloc_strdup(cmd, var);
371 cmd->reply = talloc_strdup(cmd, val);
372 if (!cmd->variable || !cmd->reply)
373 goto oom;
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200374 LOGP(DCTRL, LOGL_DEBUG, "Command: TRAP/REPLY %s: %s\n", cmd->variable, cmd->reply);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200375 break;
376 case CTRL_TYPE_ERROR:
377 var = strtok_r(NULL, "\0", &saveptr);
378 if (!var) {
379 cmd->reply = "";
380 goto err;
381 }
382 cmd->reply = talloc_strdup(cmd, var);
383 if (!cmd->reply)
384 goto oom;
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200385 LOGP(DCTRL, LOGL_DEBUG, "Command: ERROR %s\n", cmd->reply);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200386 break;
387 case CTRL_TYPE_UNKNOWN:
388 default:
389 cmd->type = CTRL_TYPE_ERROR;
390 cmd->reply = "Unknown type";
391 goto err;
392 }
393
394 return cmd;
395oom:
396 cmd->type = CTRL_TYPE_ERROR;
397 cmd->id = "err";
398 cmd->reply = "OOM";
399err:
400 talloc_free(cmd);
401 return NULL;
402}
403
404struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
405{
406 struct msgb *msg;
407 char *type, *tmp;
408
409 if (!cmd->id)
410 return NULL;
411
412 msg = msgb_alloc_headroom(4096, 128, "ctrl command make");
413 if (!msg)
414 return NULL;
415
416 type = ctrl_cmd_type2str(cmd->type);
417
418 switch (cmd->type) {
419 case CTRL_TYPE_GET:
420 if (!cmd->variable)
421 goto err;
422
423 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
424 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200425 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200426 goto err;
427 }
428
429 msg->l2h = msgb_put(msg, strlen(tmp));
430 memcpy(msg->l2h, tmp, strlen(tmp));
431 talloc_free(tmp);
432 break;
433 case CTRL_TYPE_SET:
434 if (!cmd->variable || !cmd->value)
435 goto err;
436
437 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
438 cmd->value);
439 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200440 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200441 goto err;
442 }
443
444 msg->l2h = msgb_put(msg, strlen(tmp));
445 memcpy(msg->l2h, tmp, strlen(tmp));
446 talloc_free(tmp);
447 break;
448 case CTRL_TYPE_GET_REPLY:
449 case CTRL_TYPE_SET_REPLY:
450 case CTRL_TYPE_TRAP:
451 if (!cmd->variable || !cmd->reply)
452 goto err;
453
454 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
455 cmd->reply);
456 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200457 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200458 goto err;
459 }
460
461 msg->l2h = msgb_put(msg, strlen(tmp));
462 memcpy(msg->l2h, tmp, strlen(tmp));
463 talloc_free(tmp);
464 break;
465 case CTRL_TYPE_ERROR:
466 if (!cmd->reply)
467 goto err;
468
469 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id,
470 cmd->reply);
471 if (!tmp) {
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200472 LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200473 goto err;
474 }
475
476 msg->l2h = msgb_put(msg, strlen(tmp));
477 memcpy(msg->l2h, tmp, strlen(tmp));
478 talloc_free(tmp);
479 break;
480 default:
Holger Hans Peter Freyther4ad44242011-08-22 23:37:02 +0200481 LOGP(DCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
Daniel Willmann4462f8c2010-10-21 15:00:36 +0200482 goto err;
483 break;
484 }
485
486 return msg;
487
488err:
489 msgb_free(msg);
490 return NULL;
491}