blob: b5cff6859add6653dde6f595ebc9614acbb62cec [file] [log] [blame]
Daniel Willmann1264cb42010-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;
140 command->reply = "Value failed verification.";
141 goto out;
142 }
143 } else if (cmd_el->param) {
144 LOGP(DINP, LOGL_NOTICE, "Parameter verification unimplemented, continuing without\n");
145 }
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] == '|') {
193 LOGP(DINP, LOGL_ERROR,
194 "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) {
228 LOGP(DINP, LOGL_ERROR, "vector_init failed.\n");
229 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
Daniel Willmann8b7a9622011-03-17 15:37:54 +0100240struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd)
241{
242 struct ctrl_cmd *cmd2;
243
244 cmd2 = talloc_zero(ctx, struct ctrl_cmd);
245 if (!cmd2)
246 return NULL;
247
248 cmd2->type = cmd->type;
249 if (cmd->id) {
250 cmd2->id = talloc_strdup(cmd2, cmd->id);
251 if (!cmd2->id)
252 goto err;
253 }
254 if (cmd->variable) {
255 cmd2->variable = talloc_strdup(cmd2, cmd->variable);
256 if (!cmd2->variable)
257 goto err;
258 }
259 if (cmd->value) {
260 cmd2->value = talloc_strdup(cmd2, cmd->value);
261 if (!cmd2->value)
262 goto err;
263 }
264 if (cmd->reply) {
265 cmd2->reply = talloc_strdup(cmd2, cmd->reply);
266 if (!cmd2->reply)
267 goto err;
268 }
269
270 return cmd2;
271err:
272 talloc_free(cmd2);
273 return NULL;
274}
275
Daniel Willmann1264cb42010-10-21 15:00:36 +0200276struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
277{
278 char *str, *tmp, *saveptr = NULL;
279 char *var, *val;
280 struct ctrl_cmd *cmd;
281
282 cmd = talloc_zero(ctx, struct ctrl_cmd);
283 if (!cmd) {
284 LOGP(DINP, LOGL_ERROR, "Failed to allocate.\n");
285 return NULL;
286 }
287
288 /* Make sure input is NULL terminated */
289 msgb_put_u8(msg, 0);
290 str = (char *) msg->l2h;
291
292 tmp = strtok_r(str, " ", &saveptr);
293 if (!tmp) {
294 cmd->type = CTRL_TYPE_ERROR;
295 cmd->id = "err";
296 cmd->reply = "Request malformed";
297 goto err;
298 }
299
300 cmd->type = ctrl_cmd_str2type(tmp);
301 if (cmd->type == CTRL_TYPE_UNKNOWN) {
302 cmd->type = CTRL_TYPE_ERROR;
303 cmd->id = "err";
304 cmd->reply = "Request type unknown";
305 goto err;
306 }
307
308 tmp = strtok_r(NULL, " ", &saveptr);
309
310 if (!tmp) {
311 cmd->type = CTRL_TYPE_ERROR;
312 cmd->id = "err";
313 cmd->reply = "Missing ID";
314 goto err;
315 }
316 cmd->id = talloc_strdup(cmd, tmp);
317 if (!cmd->id)
318 goto oom;
319
320 switch (cmd->type) {
321 case CTRL_TYPE_GET:
322 var = strtok_r(NULL, " ", &saveptr);
323 if (!var) {
324 cmd->type = CTRL_TYPE_ERROR;
325 cmd->reply = "GET incomplete";
326 LOGP(DINP, LOGL_NOTICE, "GET Command incomplete\n");
327 goto err;
328 }
329 cmd->variable = talloc_strdup(cmd, var);
330 LOGP(DINP, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
331 break;
332 case CTRL_TYPE_SET:
333 var = strtok_r(NULL, " ", &saveptr);
334 val = strtok_r(NULL, " ", &saveptr);
335 if (!var || !val) {
336 cmd->type = CTRL_TYPE_ERROR;
337 cmd->reply = "SET incomplete";
338 LOGP(DINP, LOGL_NOTICE, "SET Command incomplete\n");
339 goto err;
340 }
341 cmd->variable = talloc_strdup(cmd, var);
342 cmd->value = talloc_strdup(cmd, val);
343 if (!cmd->variable || !cmd->value)
344 goto oom;
345 LOGP(DINP, LOGL_DEBUG, "Command: SET %s = %s\n", cmd->variable, cmd->value);
346 break;
347 case CTRL_TYPE_GET_REPLY:
348 case CTRL_TYPE_SET_REPLY:
349 case CTRL_TYPE_TRAP:
350 var = strtok_r(NULL, " ", &saveptr);
351 val = strtok_r(NULL, " ", &saveptr);
352 if (!var || !val) {
353 cmd->type = CTRL_TYPE_ERROR;
354 cmd->reply = "Trap/Reply incomplete";
355 LOGP(DINP, LOGL_NOTICE, "Trap/Reply incomplete\n");
356 goto err;
357 }
358 cmd->variable = talloc_strdup(cmd, var);
359 cmd->reply = talloc_strdup(cmd, val);
360 if (!cmd->variable || !cmd->reply)
361 goto oom;
362 LOGP(DINP, LOGL_DEBUG, "Command: TRAP/REPLY %s: %s\n", cmd->variable, cmd->reply);
363 break;
364 case CTRL_TYPE_ERROR:
365 var = strtok_r(NULL, "\0", &saveptr);
366 if (!var) {
367 cmd->reply = "";
368 goto err;
369 }
370 cmd->reply = talloc_strdup(cmd, var);
371 if (!cmd->reply)
372 goto oom;
373 LOGP(DINP, LOGL_DEBUG, "Command: ERROR %s\n", cmd->reply);
374 break;
375 case CTRL_TYPE_UNKNOWN:
376 default:
377 cmd->type = CTRL_TYPE_ERROR;
378 cmd->reply = "Unknown type";
379 goto err;
380 }
381
382 return cmd;
383oom:
384 cmd->type = CTRL_TYPE_ERROR;
385 cmd->id = "err";
386 cmd->reply = "OOM";
387err:
388 talloc_free(cmd);
389 return NULL;
390}
391
392struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
393{
394 struct msgb *msg;
395 char *type, *tmp;
396
397 if (!cmd->id)
398 return NULL;
399
400 msg = msgb_alloc_headroom(4096, 128, "ctrl command make");
401 if (!msg)
402 return NULL;
403
404 type = ctrl_cmd_type2str(cmd->type);
405
406 switch (cmd->type) {
407 case CTRL_TYPE_GET:
408 if (!cmd->variable)
409 goto err;
410
411 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
412 if (!tmp) {
413 LOGP(DINP, LOGL_ERROR, "Failed to allocate cmd.\n");
414 goto err;
415 }
416
417 msg->l2h = msgb_put(msg, strlen(tmp));
418 memcpy(msg->l2h, tmp, strlen(tmp));
419 talloc_free(tmp);
420 break;
421 case CTRL_TYPE_SET:
422 if (!cmd->variable || !cmd->value)
423 goto err;
424
425 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
426 cmd->value);
427 if (!tmp) {
428 LOGP(DINP, LOGL_ERROR, "Failed to allocate cmd.\n");
429 goto err;
430 }
431
432 msg->l2h = msgb_put(msg, strlen(tmp));
433 memcpy(msg->l2h, tmp, strlen(tmp));
434 talloc_free(tmp);
435 break;
436 case CTRL_TYPE_GET_REPLY:
437 case CTRL_TYPE_SET_REPLY:
438 case CTRL_TYPE_TRAP:
439 if (!cmd->variable || !cmd->reply)
440 goto err;
441
442 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
443 cmd->reply);
444 if (!tmp) {
445 LOGP(DINP, LOGL_ERROR, "Failed to allocate cmd.\n");
446 goto err;
447 }
448
449 msg->l2h = msgb_put(msg, strlen(tmp));
450 memcpy(msg->l2h, tmp, strlen(tmp));
451 talloc_free(tmp);
452 break;
453 case CTRL_TYPE_ERROR:
454 if (!cmd->reply)
455 goto err;
456
457 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id,
458 cmd->reply);
459 if (!tmp) {
460 LOGP(DINP, LOGL_ERROR, "Failed to allocate cmd.\n");
461 goto err;
462 }
463
464 msg->l2h = msgb_put(msg, strlen(tmp));
465 memcpy(msg->l2h, tmp, strlen(tmp));
466 talloc_free(tmp);
467 break;
468 default:
469 LOGP(DINP, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
470 goto err;
471 break;
472 }
473
474 return msg;
475
476err:
477 msgb_free(msg);
478 return NULL;
479}