blob: 06af3b3205ce9152fc9fff6c1b4d421b6b16e045 [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
240struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
241{
242 char *str, *tmp, *saveptr = NULL;
243 char *var, *val;
244 struct ctrl_cmd *cmd;
245
246 cmd = talloc_zero(ctx, struct ctrl_cmd);
247 if (!cmd) {
248 LOGP(DINP, LOGL_ERROR, "Failed to allocate.\n");
249 return NULL;
250 }
251
252 /* Make sure input is NULL terminated */
253 msgb_put_u8(msg, 0);
254 str = (char *) msg->l2h;
255
256 tmp = strtok_r(str, " ", &saveptr);
257 if (!tmp) {
258 cmd->type = CTRL_TYPE_ERROR;
259 cmd->id = "err";
260 cmd->reply = "Request malformed";
261 goto err;
262 }
263
264 cmd->type = ctrl_cmd_str2type(tmp);
265 if (cmd->type == CTRL_TYPE_UNKNOWN) {
266 cmd->type = CTRL_TYPE_ERROR;
267 cmd->id = "err";
268 cmd->reply = "Request type unknown";
269 goto err;
270 }
271
272 tmp = strtok_r(NULL, " ", &saveptr);
273
274 if (!tmp) {
275 cmd->type = CTRL_TYPE_ERROR;
276 cmd->id = "err";
277 cmd->reply = "Missing ID";
278 goto err;
279 }
280 cmd->id = talloc_strdup(cmd, tmp);
281 if (!cmd->id)
282 goto oom;
283
284 switch (cmd->type) {
285 case CTRL_TYPE_GET:
286 var = strtok_r(NULL, " ", &saveptr);
287 if (!var) {
288 cmd->type = CTRL_TYPE_ERROR;
289 cmd->reply = "GET incomplete";
290 LOGP(DINP, LOGL_NOTICE, "GET Command incomplete\n");
291 goto err;
292 }
293 cmd->variable = talloc_strdup(cmd, var);
294 LOGP(DINP, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
295 break;
296 case CTRL_TYPE_SET:
297 var = strtok_r(NULL, " ", &saveptr);
298 val = strtok_r(NULL, " ", &saveptr);
299 if (!var || !val) {
300 cmd->type = CTRL_TYPE_ERROR;
301 cmd->reply = "SET incomplete";
302 LOGP(DINP, LOGL_NOTICE, "SET Command incomplete\n");
303 goto err;
304 }
305 cmd->variable = talloc_strdup(cmd, var);
306 cmd->value = talloc_strdup(cmd, val);
307 if (!cmd->variable || !cmd->value)
308 goto oom;
309 LOGP(DINP, LOGL_DEBUG, "Command: SET %s = %s\n", cmd->variable, cmd->value);
310 break;
311 case CTRL_TYPE_GET_REPLY:
312 case CTRL_TYPE_SET_REPLY:
313 case CTRL_TYPE_TRAP:
314 var = strtok_r(NULL, " ", &saveptr);
315 val = strtok_r(NULL, " ", &saveptr);
316 if (!var || !val) {
317 cmd->type = CTRL_TYPE_ERROR;
318 cmd->reply = "Trap/Reply incomplete";
319 LOGP(DINP, LOGL_NOTICE, "Trap/Reply incomplete\n");
320 goto err;
321 }
322 cmd->variable = talloc_strdup(cmd, var);
323 cmd->reply = talloc_strdup(cmd, val);
324 if (!cmd->variable || !cmd->reply)
325 goto oom;
326 LOGP(DINP, LOGL_DEBUG, "Command: TRAP/REPLY %s: %s\n", cmd->variable, cmd->reply);
327 break;
328 case CTRL_TYPE_ERROR:
329 var = strtok_r(NULL, "\0", &saveptr);
330 if (!var) {
331 cmd->reply = "";
332 goto err;
333 }
334 cmd->reply = talloc_strdup(cmd, var);
335 if (!cmd->reply)
336 goto oom;
337 LOGP(DINP, LOGL_DEBUG, "Command: ERROR %s\n", cmd->reply);
338 break;
339 case CTRL_TYPE_UNKNOWN:
340 default:
341 cmd->type = CTRL_TYPE_ERROR;
342 cmd->reply = "Unknown type";
343 goto err;
344 }
345
346 return cmd;
347oom:
348 cmd->type = CTRL_TYPE_ERROR;
349 cmd->id = "err";
350 cmd->reply = "OOM";
351err:
352 talloc_free(cmd);
353 return NULL;
354}
355
356struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
357{
358 struct msgb *msg;
359 char *type, *tmp;
360
361 if (!cmd->id)
362 return NULL;
363
364 msg = msgb_alloc_headroom(4096, 128, "ctrl command make");
365 if (!msg)
366 return NULL;
367
368 type = ctrl_cmd_type2str(cmd->type);
369
370 switch (cmd->type) {
371 case CTRL_TYPE_GET:
372 if (!cmd->variable)
373 goto err;
374
375 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
376 if (!tmp) {
377 LOGP(DINP, LOGL_ERROR, "Failed to allocate cmd.\n");
378 goto err;
379 }
380
381 msg->l2h = msgb_put(msg, strlen(tmp));
382 memcpy(msg->l2h, tmp, strlen(tmp));
383 talloc_free(tmp);
384 break;
385 case CTRL_TYPE_SET:
386 if (!cmd->variable || !cmd->value)
387 goto err;
388
389 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
390 cmd->value);
391 if (!tmp) {
392 LOGP(DINP, LOGL_ERROR, "Failed to allocate cmd.\n");
393 goto err;
394 }
395
396 msg->l2h = msgb_put(msg, strlen(tmp));
397 memcpy(msg->l2h, tmp, strlen(tmp));
398 talloc_free(tmp);
399 break;
400 case CTRL_TYPE_GET_REPLY:
401 case CTRL_TYPE_SET_REPLY:
402 case CTRL_TYPE_TRAP:
403 if (!cmd->variable || !cmd->reply)
404 goto err;
405
406 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
407 cmd->reply);
408 if (!tmp) {
409 LOGP(DINP, LOGL_ERROR, "Failed to allocate cmd.\n");
410 goto err;
411 }
412
413 msg->l2h = msgb_put(msg, strlen(tmp));
414 memcpy(msg->l2h, tmp, strlen(tmp));
415 talloc_free(tmp);
416 break;
417 case CTRL_TYPE_ERROR:
418 if (!cmd->reply)
419 goto err;
420
421 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id,
422 cmd->reply);
423 if (!tmp) {
424 LOGP(DINP, LOGL_ERROR, "Failed to allocate cmd.\n");
425 goto err;
426 }
427
428 msg->l2h = msgb_put(msg, strlen(tmp));
429 memcpy(msg->l2h, tmp, strlen(tmp));
430 talloc_free(tmp);
431 break;
432 default:
433 LOGP(DINP, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
434 goto err;
435 break;
436 }
437
438 return msg;
439
440err:
441 msgb_free(msg);
442 return NULL;
443}