blob: 44a1b6cb935bb6b2fcb12d070bae9741fbfb12ed [file] [log] [blame]
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001/*
2 $Id: command.c,v 1.47 2005/04/25 16:26:42 paul Exp $
3
4 Command interpreter routine for virtual terminal [aka TeletYpe]
5 Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
6
7This file is part of GNU Zebra.
8
9GNU Zebra is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published
11by the Free Software Foundation; either version 2, or (at your
12option) any later version.
13
14GNU Zebra is distributed in the hope that it will be useful, but
15WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with GNU Zebra; see the file COPYING. If not, write to the
21Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22Boston, MA 02111-1307, USA. */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Sylvain Munaut4d8eea42012-12-28 11:58:23 +010027#include <stdbool.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020028#include <syslog.h>
29#include <errno.h>
30#define _XOPEN_SOURCE
31#include <unistd.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020032#include <ctype.h>
33#include <time.h>
34#include <sys/time.h>
35#include <sys/stat.h>
36
37#include <osmocom/vty/vector.h>
38#include <osmocom/vty/vty.h>
39#include <osmocom/vty/command.h>
40
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010041#include <osmocom/core/talloc.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020042
Harald Weltee881b1b2011-08-17 18:52:30 +020043/*! \addtogroup command
Harald Welte7acb30c2011-08-17 17:13:48 +020044 * @{
45 */
46/*! \file command.c */
47
Harald Welte3fb0b6f2010-05-19 19:02:52 +020048#define CONFIGFILE_MASK 022
49
50void *tall_vty_cmd_ctx;
51
52/* Command vector which includes some level of command lists. Normally
53 each daemon maintains each own cmdvec. */
54vector cmdvec;
55
56/* Host information structure. */
57struct host host;
58
59/* Standard command node structures. */
60struct cmd_node auth_node = {
61 AUTH_NODE,
62 "Password: ",
63};
64
65struct cmd_node view_node = {
66 VIEW_NODE,
67 "%s> ",
68};
69
70struct cmd_node auth_enable_node = {
71 AUTH_ENABLE_NODE,
72 "Password: ",
73};
74
75struct cmd_node enable_node = {
76 ENABLE_NODE,
77 "%s# ",
78};
79
80struct cmd_node config_node = {
81 CONFIG_NODE,
82 "%s(config)# ",
83 1
84};
85
86/* Default motd string. */
87const char *default_motd = "";
88
Harald Welte7acb30c2011-08-17 17:13:48 +020089/*! \brief print the version (and optionally copyright) information
90 *
91 * This is called from main when a daemon is invoked with -v or --version. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020092void print_version(int print_copyright)
93{
Harald Welte237f6242010-05-25 23:00:45 +020094 printf("%s version %s\n", host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020095 if (print_copyright)
Harald Welte237f6242010-05-25 23:00:45 +020096 printf("\n%s\n", host.app_info->copyright);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020097}
98
99/* Utility function to concatenate argv argument into a single string
100 with inserting ' ' character between each argument. */
101char *argv_concat(const char **argv, int argc, int shift)
102{
103 int i;
104 size_t len;
105 char *str;
106 char *p;
107
108 len = 0;
109 for (i = shift; i < argc; i++)
110 len += strlen(argv[i]) + 1;
111 if (!len)
112 return NULL;
113 p = str = _talloc_zero(tall_vty_cmd_ctx, len, "arvg_concat");
114 for (i = shift; i < argc; i++) {
115 size_t arglen;
116 memcpy(p, argv[i], (arglen = strlen(argv[i])));
117 p += arglen;
118 *p++ = ' ';
119 }
120 *(p - 1) = '\0';
121 return str;
122}
123
Harald Welte7acb30c2011-08-17 17:13:48 +0200124/*! \brief Install top node of command vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200125void install_node(struct cmd_node *node, int (*func) (struct vty *))
126{
127 vector_set_index(cmdvec, node->node, node);
128 node->func = func;
129 node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
130}
131
132/* Compare two command's string. Used in sort_node (). */
133static int cmp_node(const void *p, const void *q)
134{
135 struct cmd_element *a = *(struct cmd_element **)p;
136 struct cmd_element *b = *(struct cmd_element **)q;
137
138 return strcmp(a->string, b->string);
139}
140
141static int cmp_desc(const void *p, const void *q)
142{
143 struct desc *a = *(struct desc **)p;
144 struct desc *b = *(struct desc **)q;
145
146 return strcmp(a->cmd, b->cmd);
147}
148
Jacob Erlbeck2442e092013-09-06 16:51:58 +0200149static int is_config_child(struct vty *vty)
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +0800150{
Holger Hans Peter Freyther8304b1e2010-09-04 11:19:39 +0800151 if (vty->node <= CONFIG_NODE)
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800152 return 0;
Holger Hans Peter Freyther8304b1e2010-09-04 11:19:39 +0800153 else if (vty->node > CONFIG_NODE && vty->node < _LAST_OSMOVTY_NODE)
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800154 return 1;
155 else if (host.app_info->is_config_node)
Holger Hans Peter Freyther8f09f012010-08-25 17:34:56 +0800156 return host.app_info->is_config_node(vty, vty->node);
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800157 else
158 return vty->node > CONFIG_NODE;
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +0800159}
160
Harald Welte7acb30c2011-08-17 17:13:48 +0200161/*! \brief Sort each node's command element according to command string. */
Harald Welte95b2b472011-07-16 11:58:09 +0200162void sort_node(void)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200163{
164 unsigned int i, j;
165 struct cmd_node *cnode;
166 vector descvec;
167 struct cmd_element *cmd_element;
168
169 for (i = 0; i < vector_active(cmdvec); i++)
170 if ((cnode = vector_slot(cmdvec, i)) != NULL) {
171 vector cmd_vector = cnode->cmd_vector;
172 qsort(cmd_vector->index, vector_active(cmd_vector),
173 sizeof(void *), cmp_node);
174
175 for (j = 0; j < vector_active(cmd_vector); j++)
176 if ((cmd_element =
177 vector_slot(cmd_vector, j)) != NULL
178 && vector_active(cmd_element->strvec)) {
179 descvec =
180 vector_slot(cmd_element->strvec,
181 vector_active
182 (cmd_element->strvec) -
183 1);
184 qsort(descvec->index,
185 vector_active(descvec),
186 sizeof(void *), cmp_desc);
187 }
188 }
189}
190
Harald Welte7acb30c2011-08-17 17:13:48 +0200191/*! Breaking up string into each command piece. I assume given
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200192 character is separated by a space character. Return value is a
193 vector which includes char ** data element. */
194vector cmd_make_strvec(const char *string)
195{
196 const char *cp, *start;
197 char *token;
198 int strlen;
199 vector strvec;
200
201 if (string == NULL)
202 return NULL;
203
204 cp = string;
205
206 /* Skip white spaces. */
207 while (isspace((int)*cp) && *cp != '\0')
208 cp++;
209
210 /* Return if there is only white spaces */
211 if (*cp == '\0')
212 return NULL;
213
214 if (*cp == '!' || *cp == '#')
215 return NULL;
216
217 /* Prepare return vector. */
218 strvec = vector_init(VECTOR_MIN_SIZE);
219
220 /* Copy each command piece and set into vector. */
221 while (1) {
222 start = cp;
223 while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') &&
224 *cp != '\0')
225 cp++;
226 strlen = cp - start;
227 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "make_strvec");
228 memcpy(token, start, strlen);
229 *(token + strlen) = '\0';
230 vector_set(strvec, token);
231
232 while ((isspace((int)*cp) || *cp == '\n' || *cp == '\r') &&
233 *cp != '\0')
234 cp++;
235
236 if (*cp == '\0')
237 return strvec;
238 }
239}
240
Harald Welte7acb30c2011-08-17 17:13:48 +0200241/*! \brief Free allocated string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200242void cmd_free_strvec(vector v)
243{
244 unsigned int i;
245 char *cp;
246
247 if (!v)
248 return;
249
250 for (i = 0; i < vector_active(v); i++)
251 if ((cp = vector_slot(v, i)) != NULL)
252 talloc_free(cp);
253
254 vector_free(v);
255}
256
Harald Welte7acb30c2011-08-17 17:13:48 +0200257/*! \brief Fetch next description. Used in \ref cmd_make_descvec(). */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200258static char *cmd_desc_str(const char **string)
259{
260 const char *cp, *start;
261 char *token;
262 int strlen;
263
264 cp = *string;
265
266 if (cp == NULL)
267 return NULL;
268
269 /* Skip white spaces. */
270 while (isspace((int)*cp) && *cp != '\0')
271 cp++;
272
273 /* Return if there is only white spaces */
274 if (*cp == '\0')
275 return NULL;
276
277 start = cp;
278
279 while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
280 cp++;
281
282 strlen = cp - start;
283 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "cmd_desc_str");
284 memcpy(token, start, strlen);
285 *(token + strlen) = '\0';
286
287 *string = cp;
288
289 return token;
290}
291
Harald Welte7acb30c2011-08-17 17:13:48 +0200292/*! \brief New string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200293static vector cmd_make_descvec(const char *string, const char *descstr)
294{
295 int multiple = 0;
296 const char *sp;
297 char *token;
298 int len;
299 const char *cp;
300 const char *dp;
301 vector allvec;
302 vector strvec = NULL;
303 struct desc *desc;
304
305 cp = string;
306 dp = descstr;
307
308 if (cp == NULL)
309 return NULL;
310
311 allvec = vector_init(VECTOR_MIN_SIZE);
312
313 while (1) {
314 while (isspace((int)*cp) && *cp != '\0')
315 cp++;
316
317 if (*cp == '(') {
318 multiple = 1;
319 cp++;
320 }
321 if (*cp == ')') {
322 multiple = 0;
323 cp++;
324 }
325 if (*cp == '|') {
326 if (!multiple) {
327 fprintf(stderr, "Command parse error!: %s\n",
328 string);
329 exit(1);
330 }
331 cp++;
332 }
333
334 while (isspace((int)*cp) && *cp != '\0')
335 cp++;
336
337 if (*cp == '(') {
338 multiple = 1;
339 cp++;
340 }
341
342 if (*cp == '\0')
343 return allvec;
344
345 sp = cp;
346
347 while (!
348 (isspace((int)*cp) || *cp == '\r' || *cp == '\n'
349 || *cp == ')' || *cp == '|') && *cp != '\0')
350 cp++;
351
352 len = cp - sp;
353
354 token = _talloc_zero(tall_vty_cmd_ctx, len + 1, "cmd_make_descvec");
355 memcpy(token, sp, len);
356 *(token + len) = '\0';
357
358 desc = talloc_zero(tall_vty_cmd_ctx, struct desc);
359 desc->cmd = token;
360 desc->str = cmd_desc_str(&dp);
361
362 if (multiple) {
363 if (multiple == 1) {
364 strvec = vector_init(VECTOR_MIN_SIZE);
365 vector_set(allvec, strvec);
366 }
367 multiple++;
368 } else {
369 strvec = vector_init(VECTOR_MIN_SIZE);
370 vector_set(allvec, strvec);
371 }
372 vector_set(strvec, desc);
373 }
374}
375
376/* Count mandantory string vector size. This is to determine inputed
377 command has enough command length. */
378static int cmd_cmdsize(vector strvec)
379{
380 unsigned int i;
381 int size = 0;
382 vector descvec;
383 struct desc *desc;
384
385 for (i = 0; i < vector_active(strvec); i++)
386 if ((descvec = vector_slot(strvec, i)) != NULL) {
387 if ((vector_active(descvec)) == 1
388 && (desc = vector_slot(descvec, 0)) != NULL) {
389 if (desc->cmd == NULL || CMD_OPTION(desc->cmd))
390 return size;
391 else
392 size++;
393 } else
394 size++;
395 }
396 return size;
397}
398
Harald Welte7acb30c2011-08-17 17:13:48 +0200399/*! \brief Return prompt character of specified node. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200400const char *cmd_prompt(enum node_type node)
401{
402 struct cmd_node *cnode;
403
404 cnode = vector_slot(cmdvec, node);
405 return cnode->prompt;
406}
407
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100408static char *xml_escape(const char *inp)
409{
410 int _strlen;
411 char *out, *out_ptr;
412 int len = 0, i, j;
413
414 if (!inp)
415 return NULL;
416 _strlen = strlen(inp);
417
418 for (i = 0; i < _strlen; ++i) {
419 switch (inp[i]) {
420 case '"':
421 len += 6;
422 break;
423 case '\'':
424 len += 6;
425 break;
426 case '<':
427 len += 4;
428 break;
429 case '>':
430 len += 4;
431 break;
432 case '&':
433 len += 5;
434 break;
435 default:
436 len += 1;
437 break;
438 }
439 }
440
441 out = talloc_size(NULL, len + 1);
442 if (!out)
443 return NULL;
444
445 out_ptr = out;
446
447#define ADD(out, str) \
448 for (j = 0; j < strlen(str); ++j) \
449 *(out++) = str[j];
450
451 for (i = 0; i < _strlen; ++i) {
452 switch (inp[i]) {
453 case '"':
454 ADD(out_ptr, "&quot;");
455 break;
456 case '\'':
457 ADD(out_ptr, "&apos;");
458 break;
459 case '<':
460 ADD(out_ptr, "&lt;");
461 break;
462 case '>':
463 ADD(out_ptr, "&gt;");
464 break;
465 case '&':
466 ADD(out_ptr, "&amp;");
467 break;
468 default:
469 *(out_ptr++) = inp[i];
470 break;
471 }
472 }
473
474#undef ADD
475
476 out_ptr[0] = '\0';
477 return out;
478}
479
480/*
481 * Write one cmd_element as XML to the given VTY.
482 */
483static int vty_dump_element(struct cmd_element *cmd, struct vty *vty)
484{
485 char *xml_string = xml_escape(cmd->string);
486
487 vty_out(vty, " <command id='%s'>%s", xml_string, VTY_NEWLINE);
488 vty_out(vty, " <params>%s", VTY_NEWLINE);
489
490 int j;
491 for (j = 0; j < vector_count(cmd->strvec); ++j) {
492 vector descvec = vector_slot(cmd->strvec, j);
493 int i;
494 for (i = 0; i < vector_active(descvec); ++i) {
495 char *xml_param, *xml_doc;
496 struct desc *desc = vector_slot(descvec, i);
497 if (desc == NULL)
498 continue;
499
500 xml_param = xml_escape(desc->cmd);
501 xml_doc = xml_escape(desc->str);
502 vty_out(vty, " <param name='%s' doc='%s' />%s",
503 xml_param, xml_doc, VTY_NEWLINE);
504 talloc_free(xml_param);
505 talloc_free(xml_doc);
506 }
507 }
508
509 vty_out(vty, " </params>%s", VTY_NEWLINE);
510 vty_out(vty, " </command>%s", VTY_NEWLINE);
511
512 talloc_free(xml_string);
513 return 0;
514}
515
516/*
517 * Dump all nodes and commands associated with a given node as XML to the VTY.
518 */
519static int vty_dump_nodes(struct vty *vty)
520{
521 int i, j;
522
523 vty_out(vty, "<vtydoc xmlns='urn:osmocom:xml:libosmocore:vty:doc:1.0'>%s", VTY_NEWLINE);
524
525 for (i = 0; i < vector_active(cmdvec); ++i) {
526 struct cmd_node *cnode;
527 cnode = vector_slot(cmdvec, i);
528 if (!cnode)
529 continue;
530
531 vty_out(vty, " <node id='%d'>%s", i, VTY_NEWLINE);
532
533 for (j = 0; j < vector_active(cnode->cmd_vector); ++j) {
534 struct cmd_element *elem;
535 elem = vector_slot(cnode->cmd_vector, j);
536 vty_dump_element(elem, vty);
537 }
538
539 vty_out(vty, " </node>%s", VTY_NEWLINE);
540 }
541
542 vty_out(vty, "</vtydoc>%s", VTY_NEWLINE);
543
544 return 0;
545}
546
Harald Welte7acb30c2011-08-17 17:13:48 +0200547/*! \brief Install a command into a node
548 * \param[in] ntype Node Type
549 * \param[cmd] element to be installed
550 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200551void install_element(enum node_type ntype, struct cmd_element *cmd)
552{
553 struct cmd_node *cnode;
554
555 cnode = vector_slot(cmdvec, ntype);
556
557 if (cnode == NULL) {
558 fprintf(stderr,
559 "Command node %d doesn't exist, please check it\n",
560 ntype);
561 exit(1);
562 }
563
564 vector_set(cnode->cmd_vector, cmd);
565
566 cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
567 cmd->cmdsize = cmd_cmdsize(cmd->strvec);
568}
569
570/* Install a command into VIEW and ENABLE node */
571void install_element_ve(struct cmd_element *cmd)
572{
573 install_element(VIEW_NODE, cmd);
574 install_element(ENABLE_NODE, cmd);
575}
576
577#ifdef VTY_CRYPT_PW
578static unsigned char itoa64[] =
579 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
580
581static void to64(char *s, long v, int n)
582{
583 while (--n >= 0) {
584 *s++ = itoa64[v & 0x3f];
585 v >>= 6;
586 }
587}
588
589static char *zencrypt(const char *passwd)
590{
591 char salt[6];
592 struct timeval tv;
593 char *crypt(const char *, const char *);
594
595 gettimeofday(&tv, 0);
596
597 to64(&salt[0], random(), 3);
598 to64(&salt[3], tv.tv_usec, 3);
599 salt[5] = '\0';
600
601 return crypt(passwd, salt);
602}
603#endif
604
605/* This function write configuration of this host. */
606static int config_write_host(struct vty *vty)
607{
608 if (host.name)
609 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
610
611 if (host.encrypt) {
612 if (host.password_encrypt)
613 vty_out(vty, "password 8 %s%s", host.password_encrypt,
614 VTY_NEWLINE);
615 if (host.enable_encrypt)
616 vty_out(vty, "enable password 8 %s%s",
617 host.enable_encrypt, VTY_NEWLINE);
618 } else {
619 if (host.password)
620 vty_out(vty, "password %s%s", host.password,
621 VTY_NEWLINE);
622 if (host.enable)
623 vty_out(vty, "enable password %s%s", host.enable,
624 VTY_NEWLINE);
625 }
626
627 if (host.advanced)
628 vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
629
630 if (host.encrypt)
631 vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
632
633 if (host.lines >= 0)
634 vty_out(vty, "service terminal-length %d%s", host.lines,
635 VTY_NEWLINE);
636
637 if (host.motdfile)
638 vty_out(vty, "banner motd file %s%s", host.motdfile,
639 VTY_NEWLINE);
640 else if (!host.motd)
641 vty_out(vty, "no banner motd%s", VTY_NEWLINE);
642
643 return 1;
644}
645
646/* Utility function for getting command vector. */
647static vector cmd_node_vector(vector v, enum node_type ntype)
648{
649 struct cmd_node *cnode = vector_slot(v, ntype);
650 return cnode->cmd_vector;
651}
652
653/* Completion match types. */
654enum match_type {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100655 no_match = 0,
656 any_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200657 extend_match,
658 ipv4_prefix_match,
659 ipv4_match,
660 ipv6_prefix_match,
661 ipv6_match,
662 range_match,
663 vararg_match,
664 partly_match,
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100665 exact_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200666};
667
668static enum match_type cmd_ipv4_match(const char *str)
669{
670 const char *sp;
671 int dots = 0, nums = 0;
672 char buf[4];
673
674 if (str == NULL)
675 return partly_match;
676
677 for (;;) {
678 memset(buf, 0, sizeof(buf));
679 sp = str;
680 while (*str != '\0') {
681 if (*str == '.') {
682 if (dots >= 3)
683 return no_match;
684
685 if (*(str + 1) == '.')
686 return no_match;
687
688 if (*(str + 1) == '\0')
689 return partly_match;
690
691 dots++;
692 break;
693 }
694 if (!isdigit((int)*str))
695 return no_match;
696
697 str++;
698 }
699
700 if (str - sp > 3)
701 return no_match;
702
703 strncpy(buf, sp, str - sp);
704 if (atoi(buf) > 255)
705 return no_match;
706
707 nums++;
708
709 if (*str == '\0')
710 break;
711
712 str++;
713 }
714
715 if (nums < 4)
716 return partly_match;
717
718 return exact_match;
719}
720
721static enum match_type cmd_ipv4_prefix_match(const char *str)
722{
723 const char *sp;
724 int dots = 0;
725 char buf[4];
726
727 if (str == NULL)
728 return partly_match;
729
730 for (;;) {
731 memset(buf, 0, sizeof(buf));
732 sp = str;
733 while (*str != '\0' && *str != '/') {
734 if (*str == '.') {
735 if (dots == 3)
736 return no_match;
737
738 if (*(str + 1) == '.' || *(str + 1) == '/')
739 return no_match;
740
741 if (*(str + 1) == '\0')
742 return partly_match;
743
744 dots++;
745 break;
746 }
747
748 if (!isdigit((int)*str))
749 return no_match;
750
751 str++;
752 }
753
754 if (str - sp > 3)
755 return no_match;
756
757 strncpy(buf, sp, str - sp);
758 if (atoi(buf) > 255)
759 return no_match;
760
761 if (dots == 3) {
762 if (*str == '/') {
763 if (*(str + 1) == '\0')
764 return partly_match;
765
766 str++;
767 break;
768 } else if (*str == '\0')
769 return partly_match;
770 }
771
772 if (*str == '\0')
773 return partly_match;
774
775 str++;
776 }
777
778 sp = str;
779 while (*str != '\0') {
780 if (!isdigit((int)*str))
781 return no_match;
782
783 str++;
784 }
785
786 if (atoi(sp) > 32)
787 return no_match;
788
789 return exact_match;
790}
791
792#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
793#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
794#define STATE_START 1
795#define STATE_COLON 2
796#define STATE_DOUBLE 3
797#define STATE_ADDR 4
798#define STATE_DOT 5
799#define STATE_SLASH 6
800#define STATE_MASK 7
801
802#ifdef HAVE_IPV6
803
804static enum match_type cmd_ipv6_match(const char *str)
805{
806 int state = STATE_START;
807 int colons = 0, nums = 0, double_colon = 0;
808 const char *sp = NULL;
809 struct sockaddr_in6 sin6_dummy;
810 int ret;
811
812 if (str == NULL)
813 return partly_match;
814
815 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
816 return no_match;
817
818 /* use inet_pton that has a better support,
819 * for example inet_pton can support the automatic addresses:
820 * ::1.2.3.4
821 */
822 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
823
824 if (ret == 1)
825 return exact_match;
826
827 while (*str != '\0') {
828 switch (state) {
829 case STATE_START:
830 if (*str == ':') {
831 if (*(str + 1) != ':' && *(str + 1) != '\0')
832 return no_match;
833 colons--;
834 state = STATE_COLON;
835 } else {
836 sp = str;
837 state = STATE_ADDR;
838 }
839
840 continue;
841 case STATE_COLON:
842 colons++;
843 if (*(str + 1) == ':')
844 state = STATE_DOUBLE;
845 else {
846 sp = str + 1;
847 state = STATE_ADDR;
848 }
849 break;
850 case STATE_DOUBLE:
851 if (double_colon)
852 return no_match;
853
854 if (*(str + 1) == ':')
855 return no_match;
856 else {
857 if (*(str + 1) != '\0')
858 colons++;
859 sp = str + 1;
860 state = STATE_ADDR;
861 }
862
863 double_colon++;
864 nums++;
865 break;
866 case STATE_ADDR:
867 if (*(str + 1) == ':' || *(str + 1) == '\0') {
868 if (str - sp > 3)
869 return no_match;
870
871 nums++;
872 state = STATE_COLON;
873 }
874 if (*(str + 1) == '.')
875 state = STATE_DOT;
876 break;
877 case STATE_DOT:
878 state = STATE_ADDR;
879 break;
880 default:
881 break;
882 }
883
884 if (nums > 8)
885 return no_match;
886
887 if (colons > 7)
888 return no_match;
889
890 str++;
891 }
892
893#if 0
894 if (nums < 11)
895 return partly_match;
896#endif /* 0 */
897
898 return exact_match;
899}
900
901static enum match_type cmd_ipv6_prefix_match(const char *str)
902{
903 int state = STATE_START;
904 int colons = 0, nums = 0, double_colon = 0;
905 int mask;
906 const char *sp = NULL;
907 char *endptr = NULL;
908
909 if (str == NULL)
910 return partly_match;
911
912 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
913 return no_match;
914
915 while (*str != '\0' && state != STATE_MASK) {
916 switch (state) {
917 case STATE_START:
918 if (*str == ':') {
919 if (*(str + 1) != ':' && *(str + 1) != '\0')
920 return no_match;
921 colons--;
922 state = STATE_COLON;
923 } else {
924 sp = str;
925 state = STATE_ADDR;
926 }
927
928 continue;
929 case STATE_COLON:
930 colons++;
931 if (*(str + 1) == '/')
932 return no_match;
933 else if (*(str + 1) == ':')
934 state = STATE_DOUBLE;
935 else {
936 sp = str + 1;
937 state = STATE_ADDR;
938 }
939 break;
940 case STATE_DOUBLE:
941 if (double_colon)
942 return no_match;
943
944 if (*(str + 1) == ':')
945 return no_match;
946 else {
947 if (*(str + 1) != '\0' && *(str + 1) != '/')
948 colons++;
949 sp = str + 1;
950
951 if (*(str + 1) == '/')
952 state = STATE_SLASH;
953 else
954 state = STATE_ADDR;
955 }
956
957 double_colon++;
958 nums += 1;
959 break;
960 case STATE_ADDR:
961 if (*(str + 1) == ':' || *(str + 1) == '.'
962 || *(str + 1) == '\0' || *(str + 1) == '/') {
963 if (str - sp > 3)
964 return no_match;
965
966 for (; sp <= str; sp++)
967 if (*sp == '/')
968 return no_match;
969
970 nums++;
971
972 if (*(str + 1) == ':')
973 state = STATE_COLON;
974 else if (*(str + 1) == '.')
975 state = STATE_DOT;
976 else if (*(str + 1) == '/')
977 state = STATE_SLASH;
978 }
979 break;
980 case STATE_DOT:
981 state = STATE_ADDR;
982 break;
983 case STATE_SLASH:
984 if (*(str + 1) == '\0')
985 return partly_match;
986
987 state = STATE_MASK;
988 break;
989 default:
990 break;
991 }
992
993 if (nums > 11)
994 return no_match;
995
996 if (colons > 7)
997 return no_match;
998
999 str++;
1000 }
1001
1002 if (state < STATE_MASK)
1003 return partly_match;
1004
1005 mask = strtol(str, &endptr, 10);
1006 if (*endptr != '\0')
1007 return no_match;
1008
1009 if (mask < 0 || mask > 128)
1010 return no_match;
1011
1012/* I don't know why mask < 13 makes command match partly.
1013 Forgive me to make this comments. I Want to set static default route
1014 because of lack of function to originate default in ospf6d; sorry
1015 yasu
1016 if (mask < 13)
1017 return partly_match;
1018*/
1019
1020 return exact_match;
1021}
1022
1023#endif /* HAVE_IPV6 */
1024
1025#define DECIMAL_STRLEN_MAX 10
1026
1027static int cmd_range_match(const char *range, const char *str)
1028{
1029 char *p;
1030 char buf[DECIMAL_STRLEN_MAX + 1];
1031 char *endptr = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001032
1033 if (str == NULL)
1034 return 1;
1035
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001036 if (range[1] == '-') {
1037 signed long min = 0, max = 0, val;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001038
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001039 val = strtol(str, &endptr, 10);
1040 if (*endptr != '\0')
1041 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001042
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001043 range += 2;
1044 p = strchr(range, '-');
1045 if (p == NULL)
1046 return 0;
1047 if (p - range > DECIMAL_STRLEN_MAX)
1048 return 0;
1049 strncpy(buf, range, p - range);
1050 buf[p - range] = '\0';
1051 min = -strtol(buf, &endptr, 10);
1052 if (*endptr != '\0')
1053 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001054
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001055 range = p + 1;
1056 p = strchr(range, '>');
1057 if (p == NULL)
1058 return 0;
1059 if (p - range > DECIMAL_STRLEN_MAX)
1060 return 0;
1061 strncpy(buf, range, p - range);
1062 buf[p - range] = '\0';
1063 max = strtol(buf, &endptr, 10);
1064 if (*endptr != '\0')
1065 return 0;
1066
1067 if (val < min || val > max)
1068 return 0;
1069 } else {
1070 unsigned long min, max, val;
1071
1072 val = strtoul(str, &endptr, 10);
1073 if (*endptr != '\0')
1074 return 0;
1075
1076 range++;
1077 p = strchr(range, '-');
1078 if (p == NULL)
1079 return 0;
1080 if (p - range > DECIMAL_STRLEN_MAX)
1081 return 0;
1082 strncpy(buf, range, p - range);
1083 buf[p - range] = '\0';
1084 min = strtoul(buf, &endptr, 10);
1085 if (*endptr != '\0')
1086 return 0;
1087
1088 range = p + 1;
1089 p = strchr(range, '>');
1090 if (p == NULL)
1091 return 0;
1092 if (p - range > DECIMAL_STRLEN_MAX)
1093 return 0;
1094 strncpy(buf, range, p - range);
1095 buf[p - range] = '\0';
1096 max = strtoul(buf, &endptr, 10);
1097 if (*endptr != '\0')
1098 return 0;
1099
1100 if (val < min || val > max)
1101 return 0;
1102 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001103
1104 return 1;
1105}
1106
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001107/* helper to retrieve the 'real' argument string from an optional argument */
1108static char *
1109cmd_deopt(const char *str)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001110{
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001111 /* we've got "[blah]". We want to strip off the []s and redo the
1112 * match check for "blah"
1113 */
1114 size_t len = strlen(str);
1115 char *tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001116
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001117 if (len < 3)
1118 return NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001119
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001120 /* tmp will hold a string of len-2 chars, so 'len' size is fine */
1121 tmp = talloc_size(NULL, len);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001122
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001123 memcpy(tmp, (str + 1), len - 2);
1124 tmp[len - 2] = '\0';
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001125
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001126 return tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001127}
1128
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001129static enum match_type
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001130cmd_match(const char *str, const char *command,
1131 enum match_type min, bool recur)
1132{
1133
1134 if (recur && CMD_OPTION(str))
1135 {
1136 enum match_type ret;
1137 char *tmp = cmd_deopt(str);
1138
1139 /* this would be a bug in a command, however handle it gracefully
1140 * as it we only discover it if a user tries to run it
1141 */
1142 if (tmp == NULL)
1143 return no_match;
1144
1145 ret = cmd_match(tmp, command, min, false);
1146
1147 talloc_free(tmp);
1148
1149 return ret;
1150 }
1151 else if (CMD_VARARG(str))
1152 return vararg_match;
1153 else if (CMD_RANGE(str))
1154 {
1155 if (cmd_range_match(str, command))
1156 return range_match;
1157 }
1158#ifdef HAVE_IPV6
1159 else if (CMD_IPV6(str))
1160 {
1161 if (cmd_ipv6_match(command) >= min)
1162 return ipv6_match;
1163 }
1164 else if (CMD_IPV6_PREFIX(str))
1165 {
1166 if (cmd_ipv6_prefix_match(command) >= min)
1167 return ipv6_prefix_match;
1168 }
1169#endif /* HAVE_IPV6 */
1170 else if (CMD_IPV4(str))
1171 {
1172 if (cmd_ipv4_match(command) >= min)
1173 return ipv4_match;
1174 }
1175 else if (CMD_IPV4_PREFIX(str))
1176 {
1177 if (cmd_ipv4_prefix_match(command) >= min)
1178 return ipv4_prefix_match;
1179 }
1180 else if (CMD_VARIABLE(str))
1181 return extend_match;
1182 else if (strncmp(command, str, strlen(command)) == 0)
1183 {
1184 if (strcmp(command, str) == 0)
1185 return exact_match;
1186 else if (partly_match >= min)
1187 return partly_match;
1188 }
1189
1190 return no_match;
1191}
1192
1193/* Filter vector at the specified index and by the given command string, to
1194 * the desired matching level (thus allowing part matches), and return match
1195 * type flag.
1196 */
1197static enum match_type
1198cmd_filter(char *command, vector v, unsigned int index, enum match_type level)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001199{
1200 unsigned int i;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001201 struct cmd_element *cmd_element;
1202 enum match_type match_type;
1203 vector descvec;
1204 struct desc *desc;
1205
1206 match_type = no_match;
1207
1208 /* If command and cmd_element string does not match set NULL to vector */
1209 for (i = 0; i < vector_active(v); i++)
1210 if ((cmd_element = vector_slot(v, i)) != NULL) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001211 if (index >= vector_active(cmd_element->strvec))
1212 vector_slot(v, i) = NULL;
1213 else {
1214 unsigned int j;
1215 int matched = 0;
1216
1217 descvec =
1218 vector_slot(cmd_element->strvec, index);
1219
1220 for (j = 0; j < vector_active(descvec); j++)
1221 if ((desc = vector_slot(descvec, j))) {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001222 enum match_type ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001223
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001224 ret = cmd_match (desc->cmd, command, level, true);
1225
1226 if (ret != no_match)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001227 matched++;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001228
1229 if (match_type < ret)
1230 match_type = ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001231 }
1232 if (!matched)
1233 vector_slot(v, i) = NULL;
1234 }
1235 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001236
1237 if (match_type == no_match)
1238 return no_match;
1239
1240 /* 2nd pass: We now know the 'strongest' match type for the index, so we
1241 * go again and filter out commands whose argument (at this index) is
1242 * 'weaker'. E.g., if we have 2 commands:
1243 *
1244 * foo bar <1-255>
1245 * foo bar BLAH
1246 *
1247 * and the command string is 'foo bar 10', then we will get here with with
1248 * 'range_match' being the strongest match. However, if 'BLAH' came
1249 * earlier, it won't have been filtered out (as a CMD_VARIABLE allows "10").
1250 *
1251 * If we don't do a 2nd pass and filter it out, the higher-layers will
1252 * consider this to be ambiguous.
1253 */
1254 for (i = 0; i < vector_active(v); i++)
1255 if ((cmd_element = vector_slot(v, i)) != NULL) {
1256 if (index >= vector_active(cmd_element->strvec))
1257 vector_slot(v, i) = NULL;
1258 else {
1259 unsigned int j;
1260 int matched = 0;
1261
1262 descvec =
1263 vector_slot(cmd_element->strvec, index);
1264
1265 for (j = 0; j < vector_active(descvec); j++)
1266 if ((desc = vector_slot(descvec, j))) {
1267 enum match_type ret;
1268
1269 ret = cmd_match(desc->cmd, command, any_match, true);
1270
1271 if (ret >= match_type)
1272 matched++;
1273 }
1274 if (!matched)
1275 vector_slot(v, i) = NULL;
1276 }
1277 }
1278
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001279 return match_type;
1280}
1281
1282/* Check ambiguous match */
1283static int
1284is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1285{
1286 unsigned int i;
1287 unsigned int j;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001288 struct cmd_element *cmd_element;
1289 const char *matched = NULL;
1290 vector descvec;
1291 struct desc *desc;
1292
1293 for (i = 0; i < vector_active(v); i++)
1294 if ((cmd_element = vector_slot(v, i)) != NULL) {
1295 int match = 0;
1296
1297 descvec = vector_slot(cmd_element->strvec, index);
1298
1299 for (j = 0; j < vector_active(descvec); j++)
1300 if ((desc = vector_slot(descvec, j))) {
1301 enum match_type ret;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001302 const char *str = desc->cmd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001303
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001304 if (CMD_OPTION(str))
1305 if ((str = cmd_deopt(str)) == NULL)
1306 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001307
1308 switch (type) {
1309 case exact_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001310 if (!(CMD_VARIABLE (str))
1311 && strcmp(command, str) == 0)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001312 match++;
1313 break;
1314 case partly_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001315 if (!(CMD_VARIABLE (str))
1316 && strncmp(command, str, strlen (command)) == 0)
1317 {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001318 if (matched
1319 && strcmp(matched,
1320 str) != 0)
1321 return 1; /* There is ambiguous match. */
1322 else
1323 matched = str;
1324 match++;
1325 }
1326 break;
1327 case range_match:
1328 if (cmd_range_match
1329 (str, command)) {
1330 if (matched
1331 && strcmp(matched,
1332 str) != 0)
1333 return 1;
1334 else
1335 matched = str;
1336 match++;
1337 }
1338 break;
1339#ifdef HAVE_IPV6
1340 case ipv6_match:
1341 if (CMD_IPV6(str))
1342 match++;
1343 break;
1344 case ipv6_prefix_match:
1345 if ((ret =
1346 cmd_ipv6_prefix_match
1347 (command)) != no_match) {
1348 if (ret == partly_match)
1349 return 2; /* There is incomplete match. */
1350
1351 match++;
1352 }
1353 break;
1354#endif /* HAVE_IPV6 */
1355 case ipv4_match:
1356 if (CMD_IPV4(str))
1357 match++;
1358 break;
1359 case ipv4_prefix_match:
1360 if ((ret =
1361 cmd_ipv4_prefix_match
1362 (command)) != no_match) {
1363 if (ret == partly_match)
1364 return 2; /* There is incomplete match. */
1365
1366 match++;
1367 }
1368 break;
1369 case extend_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001370 if (CMD_VARIABLE (str))
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001371 match++;
1372 break;
1373 case no_match:
1374 default:
1375 break;
1376 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001377
1378 if (CMD_OPTION(desc->cmd))
1379 talloc_free((void*)str);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001380 }
1381 if (!match)
1382 vector_slot(v, i) = NULL;
1383 }
1384 return 0;
1385}
1386
1387/* If src matches dst return dst string, otherwise return NULL */
1388static const char *cmd_entry_function(const char *src, const char *dst)
1389{
1390 /* Skip variable arguments. */
1391 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1392 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1393 return NULL;
1394
1395 /* In case of 'command \t', given src is NULL string. */
1396 if (src == NULL)
1397 return dst;
1398
1399 /* Matched with input string. */
1400 if (strncmp(src, dst, strlen(src)) == 0)
1401 return dst;
1402
1403 return NULL;
1404}
1405
1406/* If src matches dst return dst string, otherwise return NULL */
1407/* This version will return the dst string always if it is
1408 CMD_VARIABLE for '?' key processing */
1409static const char *cmd_entry_function_desc(const char *src, const char *dst)
1410{
1411 if (CMD_VARARG(dst))
1412 return dst;
1413
1414 if (CMD_RANGE(dst)) {
1415 if (cmd_range_match(dst, src))
1416 return dst;
1417 else
1418 return NULL;
1419 }
1420#ifdef HAVE_IPV6
1421 if (CMD_IPV6(dst)) {
1422 if (cmd_ipv6_match(src))
1423 return dst;
1424 else
1425 return NULL;
1426 }
1427
1428 if (CMD_IPV6_PREFIX(dst)) {
1429 if (cmd_ipv6_prefix_match(src))
1430 return dst;
1431 else
1432 return NULL;
1433 }
1434#endif /* HAVE_IPV6 */
1435
1436 if (CMD_IPV4(dst)) {
1437 if (cmd_ipv4_match(src))
1438 return dst;
1439 else
1440 return NULL;
1441 }
1442
1443 if (CMD_IPV4_PREFIX(dst)) {
1444 if (cmd_ipv4_prefix_match(src))
1445 return dst;
1446 else
1447 return NULL;
1448 }
1449
1450 /* Optional or variable commands always match on '?' */
1451 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1452 return dst;
1453
1454 /* In case of 'command \t', given src is NULL string. */
1455 if (src == NULL)
1456 return dst;
1457
1458 if (strncmp(src, dst, strlen(src)) == 0)
1459 return dst;
1460 else
1461 return NULL;
1462}
1463
1464/* Check same string element existence. If it isn't there return
1465 1. */
1466static int cmd_unique_string(vector v, const char *str)
1467{
1468 unsigned int i;
1469 char *match;
1470
1471 for (i = 0; i < vector_active(v); i++)
1472 if ((match = vector_slot(v, i)) != NULL)
1473 if (strcmp(match, str) == 0)
1474 return 0;
1475 return 1;
1476}
1477
1478/* Compare string to description vector. If there is same string
1479 return 1 else return 0. */
1480static int desc_unique_string(vector v, const char *str)
1481{
1482 unsigned int i;
1483 struct desc *desc;
1484
1485 for (i = 0; i < vector_active(v); i++)
1486 if ((desc = vector_slot(v, i)) != NULL)
1487 if (strcmp(desc->cmd, str) == 0)
1488 return 1;
1489 return 0;
1490}
1491
1492static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1493{
1494 if (first_word != NULL &&
1495 node != AUTH_NODE &&
1496 node != VIEW_NODE &&
1497 node != AUTH_ENABLE_NODE &&
1498 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1499 return 1;
1500 return 0;
1501}
1502
1503/* '?' describe command support. */
1504static vector
1505cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1506{
1507 unsigned int i;
1508 vector cmd_vector;
1509#define INIT_MATCHVEC_SIZE 10
1510 vector matchvec;
1511 struct cmd_element *cmd_element;
1512 unsigned int index;
1513 int ret;
1514 enum match_type match;
1515 char *command;
1516 static struct desc desc_cr = { "<cr>", "" };
1517
1518 /* Set index. */
1519 if (vector_active(vline) == 0) {
1520 *status = CMD_ERR_NO_MATCH;
1521 return NULL;
1522 } else
1523 index = vector_active(vline) - 1;
1524
1525 /* Make copy vector of current node's command vector. */
1526 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1527
1528 /* Prepare match vector */
1529 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1530
1531 /* Filter commands. */
1532 /* Only words precedes current word will be checked in this loop. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001533 for (i = 0; i < index; i++) {
1534 command = vector_slot(vline, i);
1535 if (!command)
1536 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001537
Harald Welte80d30fe2013-02-12 11:08:57 +01001538 match = cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001539
Harald Welte80d30fe2013-02-12 11:08:57 +01001540 if (match == vararg_match) {
1541 struct cmd_element *cmd_element;
1542 vector descvec;
1543 unsigned int j, k;
1544
1545 for (j = 0; j < vector_active(cmd_vector); j++)
1546 if ((cmd_element =
1547 vector_slot(cmd_vector, j)) != NULL
1548 &&
1549 (vector_active(cmd_element->strvec))) {
1550 descvec =
1551 vector_slot(cmd_element->
1552 strvec,
1553 vector_active
1554 (cmd_element->
1555 strvec) - 1);
1556 for (k = 0;
1557 k < vector_active(descvec);
1558 k++) {
1559 struct desc *desc =
1560 vector_slot(descvec,
1561 k);
1562 vector_set(matchvec,
1563 desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001564 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001565 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001566
Harald Welte80d30fe2013-02-12 11:08:57 +01001567 vector_set(matchvec, &desc_cr);
1568 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001569
Harald Welte80d30fe2013-02-12 11:08:57 +01001570 return matchvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001571 }
1572
Harald Welte80d30fe2013-02-12 11:08:57 +01001573 if ((ret = is_cmd_ambiguous(command, cmd_vector, i,
1574 match)) == 1) {
1575 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001576 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001577 *status = CMD_ERR_AMBIGUOUS;
1578 return NULL;
1579 } else if (ret == 2) {
1580 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001581 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001582 *status = CMD_ERR_NO_MATCH;
1583 return NULL;
1584 }
1585 }
1586
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001587 /* Prepare match vector */
1588 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1589
1590 /* Make sure that cmd_vector is filtered based on current word */
1591 command = vector_slot(vline, index);
1592 if (command)
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001593 match = cmd_filter(command, cmd_vector, index, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001594
1595 /* Make description vector. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001596 for (i = 0; i < vector_active(cmd_vector); i++) {
1597 const char *string = NULL;
1598 vector strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001599
Harald Welte80d30fe2013-02-12 11:08:57 +01001600 cmd_element = vector_slot(cmd_vector, i);
1601 if (!cmd_element)
1602 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001603
Harald Welted17aa592013-02-12 11:11:34 +01001604 if (cmd_element->attr & (CMD_ATTR_DEPRECATED|CMD_ATTR_HIDDEN))
1605 continue;
1606
Harald Welte80d30fe2013-02-12 11:08:57 +01001607 strvec = cmd_element->strvec;
1608
1609 /* if command is NULL, index may be equal to vector_active */
1610 if (command && index >= vector_active(strvec))
1611 vector_slot(cmd_vector, i) = NULL;
1612 else {
1613 /* Check if command is completed. */
1614 if (command == NULL
1615 && index == vector_active(strvec)) {
1616 string = "<cr>";
1617 if (!desc_unique_string(matchvec, string))
1618 vector_set(matchvec, &desc_cr);
1619 } else {
1620 unsigned int j;
1621 vector descvec = vector_slot(strvec, index);
1622 struct desc *desc;
1623
1624 for (j = 0; j < vector_active(descvec); j++) {
1625 desc = vector_slot(descvec, j);
1626 if (!desc)
1627 continue;
1628 string = cmd_entry_function_desc
1629 (command, desc->cmd);
1630 if (!string)
1631 continue;
1632 /* Uniqueness check */
1633 if (!desc_unique_string(matchvec, string))
1634 vector_set(matchvec, desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001635 }
1636 }
1637 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001638 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001639 vector_free(cmd_vector);
1640
1641 if (vector_slot(matchvec, 0) == NULL) {
1642 vector_free(matchvec);
1643 *status = CMD_ERR_NO_MATCH;
1644 } else
1645 *status = CMD_SUCCESS;
1646
1647 return matchvec;
1648}
1649
1650vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1651{
1652 vector ret;
1653
1654 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1655 enum node_type onode;
1656 vector shifted_vline;
1657 unsigned int index;
1658
1659 onode = vty->node;
1660 vty->node = ENABLE_NODE;
1661 /* We can try it on enable node, cos' the vty is authenticated */
1662
1663 shifted_vline = vector_init(vector_count(vline));
1664 /* use memcpy? */
1665 for (index = 1; index < vector_active(vline); index++) {
1666 vector_set_index(shifted_vline, index - 1,
1667 vector_lookup(vline, index));
1668 }
1669
1670 ret = cmd_describe_command_real(shifted_vline, vty, status);
1671
1672 vector_free(shifted_vline);
1673 vty->node = onode;
1674 return ret;
1675 }
1676
1677 return cmd_describe_command_real(vline, vty, status);
1678}
1679
1680/* Check LCD of matched command. */
1681static int cmd_lcd(char **matched)
1682{
1683 int i;
1684 int j;
1685 int lcd = -1;
1686 char *s1, *s2;
1687 char c1, c2;
1688
1689 if (matched[0] == NULL || matched[1] == NULL)
1690 return 0;
1691
1692 for (i = 1; matched[i] != NULL; i++) {
1693 s1 = matched[i - 1];
1694 s2 = matched[i];
1695
1696 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1697 if (c1 != c2)
1698 break;
1699
1700 if (lcd < 0)
1701 lcd = j;
1702 else {
1703 if (lcd > j)
1704 lcd = j;
1705 }
1706 }
1707 return lcd;
1708}
1709
1710/* Command line completion support. */
1711static char **cmd_complete_command_real(vector vline, struct vty *vty,
1712 int *status)
1713{
1714 unsigned int i;
1715 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1716#define INIT_MATCHVEC_SIZE 10
1717 vector matchvec;
1718 struct cmd_element *cmd_element;
1719 unsigned int index;
1720 char **match_str;
1721 struct desc *desc;
1722 vector descvec;
1723 char *command;
1724 int lcd;
1725
1726 if (vector_active(vline) == 0) {
1727 *status = CMD_ERR_NO_MATCH;
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001728 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001729 return NULL;
1730 } else
1731 index = vector_active(vline) - 1;
1732
1733 /* First, filter by preceeding command string */
1734 for (i = 0; i < index; i++)
1735 if ((command = vector_slot(vline, i))) {
1736 enum match_type match;
1737 int ret;
1738
1739 /* First try completion match, if there is exactly match return 1 */
1740 match =
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001741 cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001742
1743 /* If there is exact match then filter ambiguous match else check
1744 ambiguousness. */
1745 if ((ret =
1746 is_cmd_ambiguous(command, cmd_vector, i,
1747 match)) == 1) {
1748 vector_free(cmd_vector);
1749 *status = CMD_ERR_AMBIGUOUS;
1750 return NULL;
1751 }
1752 /*
1753 else if (ret == 2)
1754 {
1755 vector_free (cmd_vector);
1756 *status = CMD_ERR_NO_MATCH;
1757 return NULL;
1758 }
1759 */
1760 }
1761
1762 /* Prepare match vector. */
1763 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1764
1765 /* Now we got into completion */
1766 for (i = 0; i < vector_active(cmd_vector); i++)
1767 if ((cmd_element = vector_slot(cmd_vector, i))) {
1768 const char *string;
1769 vector strvec = cmd_element->strvec;
1770
1771 /* Check field length */
1772 if (index >= vector_active(strvec))
1773 vector_slot(cmd_vector, i) = NULL;
1774 else {
1775 unsigned int j;
1776
1777 descvec = vector_slot(strvec, index);
1778 for (j = 0; j < vector_active(descvec); j++)
1779 if ((desc = vector_slot(descvec, j))) {
1780 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1781 if (cmd_unique_string (matchvec, string))
1782 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
1783 }
1784 }
1785 }
1786
1787 /* We don't need cmd_vector any more. */
1788 vector_free(cmd_vector);
1789
1790 /* No matched command */
1791 if (vector_slot(matchvec, 0) == NULL) {
1792 vector_free(matchvec);
1793
1794 /* In case of 'command \t' pattern. Do you need '?' command at
1795 the end of the line. */
1796 if (vector_slot(vline, index) == '\0')
1797 *status = CMD_ERR_NOTHING_TODO;
1798 else
1799 *status = CMD_ERR_NO_MATCH;
1800 return NULL;
1801 }
1802
1803 /* Only one matched */
1804 if (vector_slot(matchvec, 1) == NULL) {
1805 match_str = (char **)matchvec->index;
1806 vector_only_wrapper_free(matchvec);
1807 *status = CMD_COMPLETE_FULL_MATCH;
1808 return match_str;
1809 }
1810 /* Make it sure last element is NULL. */
1811 vector_set(matchvec, NULL);
1812
1813 /* Check LCD of matched strings. */
1814 if (vector_slot(vline, index) != NULL) {
1815 lcd = cmd_lcd((char **)matchvec->index);
1816
1817 if (lcd) {
1818 int len = strlen(vector_slot(vline, index));
1819
1820 if (len < lcd) {
1821 char *lcdstr;
1822
1823 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
1824 "complete-lcdstr");
1825 memcpy(lcdstr, matchvec->index[0], lcd);
1826 lcdstr[lcd] = '\0';
1827
1828 /* match_str = (char **) &lcdstr; */
1829
1830 /* Free matchvec. */
1831 for (i = 0; i < vector_active(matchvec); i++) {
1832 if (vector_slot(matchvec, i))
1833 talloc_free(vector_slot(matchvec, i));
1834 }
1835 vector_free(matchvec);
1836
1837 /* Make new matchvec. */
1838 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1839 vector_set(matchvec, lcdstr);
1840 match_str = (char **)matchvec->index;
1841 vector_only_wrapper_free(matchvec);
1842
1843 *status = CMD_COMPLETE_MATCH;
1844 return match_str;
1845 }
1846 }
1847 }
1848
1849 match_str = (char **)matchvec->index;
1850 vector_only_wrapper_free(matchvec);
1851 *status = CMD_COMPLETE_LIST_MATCH;
1852 return match_str;
1853}
1854
1855char **cmd_complete_command(vector vline, struct vty *vty, int *status)
1856{
1857 char **ret;
1858
1859 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1860 enum node_type onode;
1861 vector shifted_vline;
1862 unsigned int index;
1863
1864 onode = vty->node;
1865 vty->node = ENABLE_NODE;
1866 /* We can try it on enable node, cos' the vty is authenticated */
1867
1868 shifted_vline = vector_init(vector_count(vline));
1869 /* use memcpy? */
1870 for (index = 1; index < vector_active(vline); index++) {
1871 vector_set_index(shifted_vline, index - 1,
1872 vector_lookup(vline, index));
1873 }
1874
1875 ret = cmd_complete_command_real(shifted_vline, vty, status);
1876
1877 vector_free(shifted_vline);
1878 vty->node = onode;
1879 return ret;
1880 }
1881
1882 return cmd_complete_command_real(vline, vty, status);
1883}
1884
1885/* return parent node */
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001886/*
1887 * This function MUST eventually converge on a node when called repeatedly,
1888 * there must not be any cycles.
1889 * All 'config' nodes shall converge on CONFIG_NODE.
1890 * All other 'enable' nodes shall converge on ENABLE_NODE.
1891 * All 'view' only nodes shall converge on VIEW_NODE.
1892 * All other nodes shall converge on themselves or it must be ensured,
1893 * that the user's rights are not extended anyhow by calling this function.
1894 *
1895 * Note that these requirements also apply to all functions that are used
1896 * as go_parent_cb.
1897 * Note also that this function relies on the is_config_child callback to
1898 * recognize non-config nodes if go_parent_cb is not set.
1899 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001900enum node_type vty_go_parent(struct vty *vty)
1901{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001902 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001903 case AUTH_NODE:
1904 case VIEW_NODE:
1905 case ENABLE_NODE:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001906 case CONFIG_NODE:
1907 break;
1908
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001909 case AUTH_ENABLE_NODE:
1910 vty->node = VIEW_NODE;
1911 break;
1912
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001913 case CFG_LOG_NODE:
1914 case VTY_NODE:
1915 vty->node = CONFIG_NODE;
1916 break;
1917
1918 default:
1919 if (host.app_info->go_parent_cb)
1920 host.app_info->go_parent_cb(vty);
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001921 else if (is_config_child(vty))
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001922 vty->node = CONFIG_NODE;
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02001923 else
1924 vty->node = VIEW_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02001925 break;
1926 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001927
1928 return vty->node;
1929}
1930
1931/* Execute command by argument vline vector. */
1932static int
1933cmd_execute_command_real(vector vline, struct vty *vty,
1934 struct cmd_element **cmd)
1935{
1936 unsigned int i;
1937 unsigned int index;
1938 vector cmd_vector;
1939 struct cmd_element *cmd_element;
1940 struct cmd_element *matched_element;
1941 unsigned int matched_count, incomplete_count;
1942 int argc;
1943 const char *argv[CMD_ARGC_MAX];
1944 enum match_type match = 0;
1945 int varflag;
1946 char *command;
1947
1948 /* Make copy of command elements. */
1949 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1950
1951 for (index = 0; index < vector_active(vline); index++)
1952 if ((command = vector_slot(vline, index))) {
1953 int ret;
1954
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001955 match = cmd_filter(command, cmd_vector, index,
1956 any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001957
1958 if (match == vararg_match)
1959 break;
1960
1961 ret =
1962 is_cmd_ambiguous(command, cmd_vector, index, match);
1963
1964 if (ret == 1) {
1965 vector_free(cmd_vector);
1966 return CMD_ERR_AMBIGUOUS;
1967 } else if (ret == 2) {
1968 vector_free(cmd_vector);
1969 return CMD_ERR_NO_MATCH;
1970 }
1971 }
1972
1973 /* Check matched count. */
1974 matched_element = NULL;
1975 matched_count = 0;
1976 incomplete_count = 0;
1977
1978 for (i = 0; i < vector_active(cmd_vector); i++)
1979 if ((cmd_element = vector_slot(cmd_vector, i))) {
1980 if (match == vararg_match
1981 || index >= cmd_element->cmdsize) {
1982 matched_element = cmd_element;
1983#if 0
1984 printf("DEBUG: %s\n", cmd_element->string);
1985#endif
1986 matched_count++;
1987 } else {
1988 incomplete_count++;
1989 }
1990 }
1991
1992 /* Finish of using cmd_vector. */
1993 vector_free(cmd_vector);
1994
1995 /* To execute command, matched_count must be 1. */
1996 if (matched_count == 0) {
1997 if (incomplete_count)
1998 return CMD_ERR_INCOMPLETE;
1999 else
2000 return CMD_ERR_NO_MATCH;
2001 }
2002
2003 if (matched_count > 1)
2004 return CMD_ERR_AMBIGUOUS;
2005
2006 /* Argument treatment */
2007 varflag = 0;
2008 argc = 0;
2009
2010 for (i = 0; i < vector_active(vline); i++) {
2011 if (varflag)
2012 argv[argc++] = vector_slot(vline, i);
2013 else {
2014 vector descvec =
2015 vector_slot(matched_element->strvec, i);
2016
2017 if (vector_active(descvec) == 1) {
2018 struct desc *desc = vector_slot(descvec, 0);
2019
2020 if (CMD_VARARG(desc->cmd))
2021 varflag = 1;
2022
2023 if (varflag || CMD_VARIABLE(desc->cmd)
2024 || CMD_OPTION(desc->cmd))
2025 argv[argc++] = vector_slot(vline, i);
2026 } else
2027 argv[argc++] = vector_slot(vline, i);
2028 }
2029
2030 if (argc >= CMD_ARGC_MAX)
2031 return CMD_ERR_EXEED_ARGC_MAX;
2032 }
2033
2034 /* For vtysh execution. */
2035 if (cmd)
2036 *cmd = matched_element;
2037
2038 if (matched_element->daemon)
2039 return CMD_SUCCESS_DAEMON;
2040
2041 /* Execute matched command. */
2042 return (*matched_element->func) (matched_element, vty, argc, argv);
2043}
2044
2045int
2046cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2047 int vtysh)
2048{
2049 int ret, saved_ret, tried = 0;
2050 enum node_type onode;
2051 void *oindex;
2052
2053 onode = vty->node;
2054 oindex = vty->index;
2055
2056 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2057 vector shifted_vline;
2058 unsigned int index;
2059
2060 vty->node = ENABLE_NODE;
2061 /* We can try it on enable node, cos' the vty is authenticated */
2062
2063 shifted_vline = vector_init(vector_count(vline));
2064 /* use memcpy? */
2065 for (index = 1; index < vector_active(vline); index++) {
2066 vector_set_index(shifted_vline, index - 1,
2067 vector_lookup(vline, index));
2068 }
2069
2070 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2071
2072 vector_free(shifted_vline);
2073 vty->node = onode;
2074 return ret;
2075 }
2076
2077 saved_ret = ret = cmd_execute_command_real(vline, vty, cmd);
2078
2079 if (vtysh)
2080 return saved_ret;
2081
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +08002082 /* Go to parent for config nodes to attempt to find the right command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002083 while (ret != CMD_SUCCESS && ret != CMD_WARNING
Jacob Erlbeck2442e092013-09-06 16:51:58 +02002084 && is_config_child(vty)) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002085 vty_go_parent(vty);
2086 ret = cmd_execute_command_real(vline, vty, cmd);
2087 tried = 1;
2088 if (ret == CMD_SUCCESS || ret == CMD_WARNING) {
2089 /* succesfull command, leave the node as is */
2090 return ret;
2091 }
2092 }
2093 /* no command succeeded, reset the vty to the original node and
2094 return the error for this node */
2095 if (tried) {
2096 vty->node = onode;
2097 vty->index = oindex;
2098 }
2099 return saved_ret;
2100}
2101
2102/* Execute command by argument readline. */
2103int
2104cmd_execute_command_strict(vector vline, struct vty *vty,
2105 struct cmd_element **cmd)
2106{
2107 unsigned int i;
2108 unsigned int index;
2109 vector cmd_vector;
2110 struct cmd_element *cmd_element;
2111 struct cmd_element *matched_element;
2112 unsigned int matched_count, incomplete_count;
2113 int argc;
2114 const char *argv[CMD_ARGC_MAX];
2115 int varflag;
2116 enum match_type match = 0;
2117 char *command;
2118
2119 /* Make copy of command element */
2120 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2121
2122 for (index = 0; index < vector_active(vline); index++)
2123 if ((command = vector_slot(vline, index))) {
2124 int ret;
2125
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002126 match = cmd_filter(vector_slot(vline, index),
2127 cmd_vector, index, exact_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002128
2129 /* If command meets '.VARARG' then finish matching. */
2130 if (match == vararg_match)
2131 break;
2132
2133 ret =
2134 is_cmd_ambiguous(command, cmd_vector, index, match);
2135 if (ret == 1) {
2136 vector_free(cmd_vector);
2137 return CMD_ERR_AMBIGUOUS;
2138 }
2139 if (ret == 2) {
2140 vector_free(cmd_vector);
2141 return CMD_ERR_NO_MATCH;
2142 }
2143 }
2144
2145 /* Check matched count. */
2146 matched_element = NULL;
2147 matched_count = 0;
2148 incomplete_count = 0;
2149 for (i = 0; i < vector_active(cmd_vector); i++)
2150 if (vector_slot(cmd_vector, i) != NULL) {
2151 cmd_element = vector_slot(cmd_vector, i);
2152
2153 if (match == vararg_match
2154 || index >= cmd_element->cmdsize) {
2155 matched_element = cmd_element;
2156 matched_count++;
2157 } else
2158 incomplete_count++;
2159 }
2160
2161 /* Finish of using cmd_vector. */
2162 vector_free(cmd_vector);
2163
2164 /* To execute command, matched_count must be 1. */
2165 if (matched_count == 0) {
2166 if (incomplete_count)
2167 return CMD_ERR_INCOMPLETE;
2168 else
2169 return CMD_ERR_NO_MATCH;
2170 }
2171
2172 if (matched_count > 1)
2173 return CMD_ERR_AMBIGUOUS;
2174
2175 /* Argument treatment */
2176 varflag = 0;
2177 argc = 0;
2178
2179 for (i = 0; i < vector_active(vline); i++) {
2180 if (varflag)
2181 argv[argc++] = vector_slot(vline, i);
2182 else {
2183 vector descvec =
2184 vector_slot(matched_element->strvec, i);
2185
2186 if (vector_active(descvec) == 1) {
2187 struct desc *desc = vector_slot(descvec, 0);
2188
2189 if (CMD_VARARG(desc->cmd))
2190 varflag = 1;
2191
2192 if (varflag || CMD_VARIABLE(desc->cmd)
2193 || CMD_OPTION(desc->cmd))
2194 argv[argc++] = vector_slot(vline, i);
2195 } else
2196 argv[argc++] = vector_slot(vline, i);
2197 }
2198
2199 if (argc >= CMD_ARGC_MAX)
2200 return CMD_ERR_EXEED_ARGC_MAX;
2201 }
2202
2203 /* For vtysh execution. */
2204 if (cmd)
2205 *cmd = matched_element;
2206
2207 if (matched_element->daemon)
2208 return CMD_SUCCESS_DAEMON;
2209
2210 /* Now execute matched command */
2211 return (*matched_element->func) (matched_element, vty, argc, argv);
2212}
2213
2214/* Configration make from file. */
2215int config_from_file(struct vty *vty, FILE * fp)
2216{
2217 int ret;
2218 vector vline;
2219
2220 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
2221 vline = cmd_make_strvec(vty->buf);
2222
2223 /* In case of comment line */
2224 if (vline == NULL)
2225 continue;
2226 /* Execute configuration command : this is strict match */
2227 ret = cmd_execute_command_strict(vline, vty, NULL);
2228
2229 /* Try again with setting node to CONFIG_NODE */
2230 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2231 && ret != CMD_ERR_NOTHING_TODO
Jacob Erlbeck2442e092013-09-06 16:51:58 +02002232 && is_config_child(vty)) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002233 vty_go_parent(vty);
2234 ret = cmd_execute_command_strict(vline, vty, NULL);
2235 }
2236
2237 cmd_free_strvec(vline);
2238
2239 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2240 && ret != CMD_ERR_NOTHING_TODO)
2241 return ret;
2242 }
2243 return CMD_SUCCESS;
2244}
2245
2246/* Configration from terminal */
2247DEFUN(config_terminal,
2248 config_terminal_cmd,
2249 "configure terminal",
2250 "Configuration from vty interface\n" "Configuration terminal\n")
2251{
2252 if (vty_config_lock(vty))
2253 vty->node = CONFIG_NODE;
2254 else {
2255 vty_out(vty, "VTY configuration is locked by other VTY%s",
2256 VTY_NEWLINE);
2257 return CMD_WARNING;
2258 }
2259 return CMD_SUCCESS;
2260}
2261
2262/* Enable command */
2263DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2264{
2265 /* If enable password is NULL, change to ENABLE_NODE */
2266 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2267 vty->type == VTY_SHELL_SERV)
2268 vty->node = ENABLE_NODE;
2269 else
2270 vty->node = AUTH_ENABLE_NODE;
2271
2272 return CMD_SUCCESS;
2273}
2274
2275/* Disable command */
2276DEFUN(disable,
2277 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2278{
2279 if (vty->node == ENABLE_NODE)
2280 vty->node = VIEW_NODE;
2281 return CMD_SUCCESS;
2282}
2283
2284/* Down vty node level. */
2285gDEFUN(config_exit,
2286 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2287{
2288 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002289 case AUTH_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002290 case VIEW_NODE:
2291 case ENABLE_NODE:
2292 if (0) //vty_shell (vty))
2293 exit(0);
2294 else
2295 vty->status = VTY_CLOSE;
2296 break;
2297 case CONFIG_NODE:
2298 vty->node = ENABLE_NODE;
2299 vty_config_unlock(vty);
2300 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002301 default:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002302 if (vty->node > CONFIG_NODE)
2303 vty_go_parent (vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002304 break;
2305 }
2306 return CMD_SUCCESS;
2307}
2308
2309/* End of configuration. */
2310 gDEFUN(config_end,
2311 config_end_cmd, "end", "End current mode and change to enable mode.")
2312{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002313 if (vty->node > ENABLE_NODE) {
Jacob Erlbeck23497212013-09-10 09:07:31 +02002314 int last_node = CONFIG_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002315
2316 /* Repeatedly call go_parent until a top node is reached. */
2317 while (vty->node > CONFIG_NODE) {
2318 if (vty->node == last_node) {
2319 /* Ensure termination, this shouldn't happen. */
2320 break;
2321 }
2322 last_node = vty->node;
2323 vty_go_parent(vty);
2324 }
2325
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002326 vty_config_unlock(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002327 if (vty->node > ENABLE_NODE)
2328 vty->node = ENABLE_NODE;
2329 vty->index = NULL;
2330 vty->index_sub = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002331 }
2332 return CMD_SUCCESS;
2333}
2334
2335/* Show version. */
2336DEFUN(show_version,
2337 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2338{
Harald Welte237f6242010-05-25 23:00:45 +02002339 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2340 host.app_info->version,
2341 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2342 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002343
2344 return CMD_SUCCESS;
2345}
2346
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01002347DEFUN(show_online_help,
2348 show_online_help_cmd, "show online-help", SHOW_STR "Online help\n")
2349{
2350 vty_dump_nodes(vty);
2351 return CMD_SUCCESS;
2352}
2353
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002354/* Help display function for all node. */
2355gDEFUN(config_help,
2356 config_help_cmd, "help", "Description of the interactive help system\n")
2357{
2358 vty_out(vty,
2359 "This VTY provides advanced help features. When you need help,%s\
2360anytime at the command line please press '?'.%s\
2361%s\
2362If nothing matches, the help list will be empty and you must backup%s\
2363 until entering a '?' shows the available options.%s\
2364Two styles of help are provided:%s\
23651. Full help is available when you are ready to enter a%s\
2366command argument (e.g. 'show ?') and describes each possible%s\
2367argument.%s\
23682. Partial help is provided when an abbreviated argument is entered%s\
2369 and you want to know what arguments match the input%s\
2370 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2371 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2372 return CMD_SUCCESS;
2373}
2374
2375/* Help display function for all node. */
2376gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2377{
2378 unsigned int i;
2379 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2380 struct cmd_element *cmd;
2381
2382 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2383 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2384 && !(cmd->attr == CMD_ATTR_DEPRECATED
2385 || cmd->attr == CMD_ATTR_HIDDEN))
2386 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2387 return CMD_SUCCESS;
2388}
2389
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002390static int write_config_file(const char *config_file, char **outpath)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002391{
2392 unsigned int i;
2393 int fd;
2394 struct cmd_node *node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002395 char *config_file_tmp = NULL;
2396 char *config_file_sav = NULL;
2397 struct vty *file_vty;
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002398 struct stat st;
2399
2400 *outpath = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002401
2402 /* Check and see if we are operating under vtysh configuration */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002403 config_file_sav =
2404 _talloc_zero(tall_vty_cmd_ctx,
2405 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2406 "config_file_sav");
2407 strcpy(config_file_sav, config_file);
2408 strcat(config_file_sav, CONF_BACKUP_EXT);
2409
2410 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2411 "config_file_tmp");
2412 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2413
2414 /* Open file to configuration write. */
2415 fd = mkstemp(config_file_tmp);
2416 if (fd < 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002417 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002418 talloc_free(config_file_tmp);
2419 talloc_free(config_file_sav);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002420 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002421 }
2422
2423 /* Make vty for configuration file. */
2424 file_vty = vty_new();
2425 file_vty->fd = fd;
2426 file_vty->type = VTY_FILE;
2427
2428 /* Config file header print. */
2429 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02002430 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002431 //vty_time_print (file_vty, 1);
2432 vty_out(file_vty, "!\n");
2433
2434 for (i = 0; i < vector_active(cmdvec); i++)
2435 if ((node = vector_slot(cmdvec, i)) && node->func) {
2436 if ((*node->func) (file_vty))
2437 vty_out(file_vty, "!\n");
2438 }
2439 vty_close(file_vty);
2440
2441 if (unlink(config_file_sav) != 0)
2442 if (errno != ENOENT) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002443 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002444 talloc_free(config_file_sav);
2445 talloc_free(config_file_tmp);
2446 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002447 return -2;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002448 }
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002449
2450 /* Only link the .sav file if the original file exists */
2451 if (stat(config_file, &st) == 0) {
2452 if (link(config_file, config_file_sav) != 0) {
2453 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
2454 talloc_free(config_file_sav);
2455 talloc_free(config_file_tmp);
2456 unlink(config_file_tmp);
2457 return -3;
2458 }
2459 sync();
2460 if (unlink(config_file) != 0) {
2461 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2462 talloc_free(config_file_sav);
2463 talloc_free(config_file_tmp);
2464 unlink(config_file_tmp);
2465 return -4;
2466 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002467 }
2468 if (link(config_file_tmp, config_file) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002469 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002470 talloc_free(config_file_sav);
2471 talloc_free(config_file_tmp);
2472 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002473 return -5;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002474 }
2475 unlink(config_file_tmp);
2476 sync();
2477
2478 talloc_free(config_file_sav);
2479 talloc_free(config_file_tmp);
2480
2481 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002482 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2483 return -6;
2484 }
2485
2486 return 0;
2487}
2488
2489
2490/* Write current configuration into file. */
2491DEFUN(config_write_file,
2492 config_write_file_cmd,
2493 "write file",
2494 "Write running configuration to memory, network, or terminal\n"
2495 "Write to configuration file\n")
2496{
2497 char *failed_file;
2498 int rc;
2499
2500 if (host.config == NULL) {
2501 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2502 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002503 return CMD_WARNING;
2504 }
2505
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002506 rc = write_config_file(host.config, &failed_file);
2507 switch (rc) {
2508 case -1:
2509 vty_out(vty, "Can't open configuration file %s.%s",
2510 failed_file, VTY_NEWLINE);
2511 rc = CMD_WARNING;
2512 break;
2513 case -2:
2514 vty_out(vty, "Can't unlink backup configuration file %s.%s",
2515 failed_file, VTY_NEWLINE);
2516 rc = CMD_WARNING;
2517 break;
2518 case -3:
2519 vty_out(vty, "Can't backup old configuration file %s.%s",
2520 failed_file, VTY_NEWLINE);
2521 rc = CMD_WARNING;
2522 break;
2523 case -4:
2524 vty_out(vty, "Can't unlink configuration file %s.%s",
2525 failed_file, VTY_NEWLINE);
2526 rc = CMD_WARNING;
2527 break;
2528 case -5:
2529 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
2530 VTY_NEWLINE);
2531 rc = CMD_WARNING;
2532 break;
2533 case -6:
2534 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2535 failed_file, strerror(errno), errno, VTY_NEWLINE);
2536 rc = CMD_WARNING;
2537 break;
2538 default:
2539 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
2540 rc = CMD_SUCCESS;
2541 break;
2542 }
2543
2544 talloc_free(failed_file);
2545 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002546}
2547
2548ALIAS(config_write_file,
2549 config_write_cmd,
2550 "write", "Write running configuration to memory, network, or terminal\n")
2551
2552 ALIAS(config_write_file,
2553 config_write_memory_cmd,
2554 "write memory",
2555 "Write running configuration to memory, network, or terminal\n"
2556 "Write configuration to the file (same as write file)\n")
2557
2558 ALIAS(config_write_file,
2559 copy_runningconfig_startupconfig_cmd,
2560 "copy running-config startup-config",
2561 "Copy configuration\n"
2562 "Copy running config to... \n"
2563 "Copy running config to startup config (same as write file)\n")
2564
2565/* Write current configuration into the terminal. */
2566 DEFUN(config_write_terminal,
2567 config_write_terminal_cmd,
2568 "write terminal",
2569 "Write running configuration to memory, network, or terminal\n"
2570 "Write to terminal\n")
2571{
2572 unsigned int i;
2573 struct cmd_node *node;
2574
2575 if (vty->type == VTY_SHELL_SERV) {
2576 for (i = 0; i < vector_active(cmdvec); i++)
2577 if ((node = vector_slot(cmdvec, i)) && node->func
2578 && node->vtysh) {
2579 if ((*node->func) (vty))
2580 vty_out(vty, "!%s", VTY_NEWLINE);
2581 }
2582 } else {
2583 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2584 VTY_NEWLINE);
2585 vty_out(vty, "!%s", VTY_NEWLINE);
2586
2587 for (i = 0; i < vector_active(cmdvec); i++)
2588 if ((node = vector_slot(cmdvec, i)) && node->func) {
2589 if ((*node->func) (vty))
2590 vty_out(vty, "!%s", VTY_NEWLINE);
2591 }
2592 vty_out(vty, "end%s", VTY_NEWLINE);
2593 }
2594 return CMD_SUCCESS;
2595}
2596
2597/* Write current configuration into the terminal. */
2598ALIAS(config_write_terminal,
2599 show_running_config_cmd,
2600 "show running-config", SHOW_STR "running configuration\n")
2601
2602/* Write startup configuration into the terminal. */
2603 DEFUN(show_startup_config,
2604 show_startup_config_cmd,
2605 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2606{
2607 char buf[BUFSIZ];
2608 FILE *confp;
2609
2610 confp = fopen(host.config, "r");
2611 if (confp == NULL) {
2612 vty_out(vty, "Can't open configuration file [%s]%s",
2613 host.config, VTY_NEWLINE);
2614 return CMD_WARNING;
2615 }
2616
2617 while (fgets(buf, BUFSIZ, confp)) {
2618 char *cp = buf;
2619
2620 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2621 cp++;
2622 *cp = '\0';
2623
2624 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2625 }
2626
2627 fclose(confp);
2628
2629 return CMD_SUCCESS;
2630}
2631
2632/* Hostname configuration */
2633DEFUN(config_hostname,
2634 hostname_cmd,
2635 "hostname WORD",
2636 "Set system's network name\n" "This system's network name\n")
2637{
2638 if (!isalpha((int)*argv[0])) {
2639 vty_out(vty, "Please specify string starting with alphabet%s",
2640 VTY_NEWLINE);
2641 return CMD_WARNING;
2642 }
2643
2644 if (host.name)
2645 talloc_free(host.name);
2646
2647 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2648 return CMD_SUCCESS;
2649}
2650
2651DEFUN(config_no_hostname,
2652 no_hostname_cmd,
2653 "no hostname [HOSTNAME]",
2654 NO_STR "Reset system's network name\n" "Host name of this router\n")
2655{
2656 if (host.name)
2657 talloc_free(host.name);
2658 host.name = NULL;
2659 return CMD_SUCCESS;
2660}
2661
2662/* VTY interface password set. */
2663DEFUN(config_password, password_cmd,
2664 "password (8|) WORD",
2665 "Assign the terminal connection password\n"
2666 "Specifies a HIDDEN password will follow\n"
2667 "dummy string \n" "The HIDDEN line password string\n")
2668{
2669 /* Argument check. */
2670 if (argc == 0) {
2671 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2672 return CMD_WARNING;
2673 }
2674
2675 if (argc == 2) {
2676 if (*argv[0] == '8') {
2677 if (host.password)
2678 talloc_free(host.password);
2679 host.password = NULL;
2680 if (host.password_encrypt)
2681 talloc_free(host.password_encrypt);
2682 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2683 return CMD_SUCCESS;
2684 } else {
2685 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2686 return CMD_WARNING;
2687 }
2688 }
2689
2690 if (!isalnum((int)*argv[0])) {
2691 vty_out(vty,
2692 "Please specify string starting with alphanumeric%s",
2693 VTY_NEWLINE);
2694 return CMD_WARNING;
2695 }
2696
2697 if (host.password)
2698 talloc_free(host.password);
2699 host.password = NULL;
2700
2701#ifdef VTY_CRYPT_PW
2702 if (host.encrypt) {
2703 if (host.password_encrypt)
2704 talloc_free(host.password_encrypt);
2705 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2706 } else
2707#endif
2708 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2709
2710 return CMD_SUCCESS;
2711}
2712
2713ALIAS(config_password, password_text_cmd,
2714 "password LINE",
2715 "Assign the terminal connection password\n"
2716 "The UNENCRYPTED (cleartext) line password\n")
2717
2718/* VTY enable password set. */
2719 DEFUN(config_enable_password, enable_password_cmd,
2720 "enable password (8|) WORD",
2721 "Modify enable password parameters\n"
2722 "Assign the privileged level password\n"
2723 "Specifies a HIDDEN password will follow\n"
2724 "dummy string \n" "The HIDDEN 'enable' password string\n")
2725{
2726 /* Argument check. */
2727 if (argc == 0) {
2728 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2729 return CMD_WARNING;
2730 }
2731
2732 /* Crypt type is specified. */
2733 if (argc == 2) {
2734 if (*argv[0] == '8') {
2735 if (host.enable)
2736 talloc_free(host.enable);
2737 host.enable = NULL;
2738
2739 if (host.enable_encrypt)
2740 talloc_free(host.enable_encrypt);
2741 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2742
2743 return CMD_SUCCESS;
2744 } else {
2745 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2746 return CMD_WARNING;
2747 }
2748 }
2749
2750 if (!isalnum((int)*argv[0])) {
2751 vty_out(vty,
2752 "Please specify string starting with alphanumeric%s",
2753 VTY_NEWLINE);
2754 return CMD_WARNING;
2755 }
2756
2757 if (host.enable)
2758 talloc_free(host.enable);
2759 host.enable = NULL;
2760
2761 /* Plain password input. */
2762#ifdef VTY_CRYPT_PW
2763 if (host.encrypt) {
2764 if (host.enable_encrypt)
2765 talloc_free(host.enable_encrypt);
2766 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2767 } else
2768#endif
2769 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2770
2771 return CMD_SUCCESS;
2772}
2773
2774ALIAS(config_enable_password,
2775 enable_password_text_cmd,
2776 "enable password LINE",
2777 "Modify enable password parameters\n"
2778 "Assign the privileged level password\n"
2779 "The UNENCRYPTED (cleartext) 'enable' password\n")
2780
2781/* VTY enable password delete. */
2782 DEFUN(no_config_enable_password, no_enable_password_cmd,
2783 "no enable password",
2784 NO_STR
2785 "Modify enable password parameters\n"
2786 "Assign the privileged level password\n")
2787{
2788 if (host.enable)
2789 talloc_free(host.enable);
2790 host.enable = NULL;
2791
2792 if (host.enable_encrypt)
2793 talloc_free(host.enable_encrypt);
2794 host.enable_encrypt = NULL;
2795
2796 return CMD_SUCCESS;
2797}
2798
2799#ifdef VTY_CRYPT_PW
2800DEFUN(service_password_encrypt,
2801 service_password_encrypt_cmd,
2802 "service password-encryption",
2803 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2804{
2805 if (host.encrypt)
2806 return CMD_SUCCESS;
2807
2808 host.encrypt = 1;
2809
2810 if (host.password) {
2811 if (host.password_encrypt)
2812 talloc_free(host.password_encrypt);
2813 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
2814 }
2815 if (host.enable) {
2816 if (host.enable_encrypt)
2817 talloc_free(host.enable_encrypt);
2818 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
2819 }
2820
2821 return CMD_SUCCESS;
2822}
2823
2824DEFUN(no_service_password_encrypt,
2825 no_service_password_encrypt_cmd,
2826 "no service password-encryption",
2827 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2828{
2829 if (!host.encrypt)
2830 return CMD_SUCCESS;
2831
2832 host.encrypt = 0;
2833
2834 if (host.password_encrypt)
2835 talloc_free(host.password_encrypt);
2836 host.password_encrypt = NULL;
2837
2838 if (host.enable_encrypt)
2839 talloc_free(host.enable_encrypt);
2840 host.enable_encrypt = NULL;
2841
2842 return CMD_SUCCESS;
2843}
2844#endif
2845
2846DEFUN(config_terminal_length, config_terminal_length_cmd,
2847 "terminal length <0-512>",
2848 "Set terminal line parameters\n"
2849 "Set number of lines on a screen\n"
2850 "Number of lines on screen (0 for no pausing)\n")
2851{
2852 int lines;
2853 char *endptr = NULL;
2854
2855 lines = strtol(argv[0], &endptr, 10);
2856 if (lines < 0 || lines > 512 || *endptr != '\0') {
2857 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2858 return CMD_WARNING;
2859 }
2860 vty->lines = lines;
2861
2862 return CMD_SUCCESS;
2863}
2864
2865DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2866 "terminal no length",
2867 "Set terminal line parameters\n"
2868 NO_STR "Set number of lines on a screen\n")
2869{
2870 vty->lines = -1;
2871 return CMD_SUCCESS;
2872}
2873
2874DEFUN(service_terminal_length, service_terminal_length_cmd,
2875 "service terminal-length <0-512>",
2876 "Set up miscellaneous service\n"
2877 "System wide terminal length configuration\n"
2878 "Number of lines of VTY (0 means no line control)\n")
2879{
2880 int lines;
2881 char *endptr = NULL;
2882
2883 lines = strtol(argv[0], &endptr, 10);
2884 if (lines < 0 || lines > 512 || *endptr != '\0') {
2885 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2886 return CMD_WARNING;
2887 }
2888 host.lines = lines;
2889
2890 return CMD_SUCCESS;
2891}
2892
2893DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2894 "no service terminal-length [<0-512>]",
2895 NO_STR
2896 "Set up miscellaneous service\n"
2897 "System wide terminal length configuration\n"
2898 "Number of lines of VTY (0 means no line control)\n")
2899{
2900 host.lines = -1;
2901 return CMD_SUCCESS;
2902}
2903
2904DEFUN_HIDDEN(do_echo,
2905 echo_cmd,
2906 "echo .MESSAGE",
2907 "Echo a message back to the vty\n" "The message to echo\n")
2908{
2909 char *message;
2910
2911 vty_out(vty, "%s%s",
2912 ((message =
2913 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2914 if (message)
2915 talloc_free(message);
2916 return CMD_SUCCESS;
2917}
2918
2919#if 0
2920DEFUN(config_logmsg,
2921 config_logmsg_cmd,
2922 "logmsg " LOG_LEVELS " .MESSAGE",
2923 "Send a message to enabled logging destinations\n"
2924 LOG_LEVEL_DESC "The message to send\n")
2925{
2926 int level;
2927 char *message;
2928
2929 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2930 return CMD_ERR_NO_MATCH;
2931
2932 zlog(NULL, level,
2933 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2934 if (message)
2935 talloc_free(message);
2936 return CMD_SUCCESS;
2937}
2938
2939DEFUN(show_logging,
2940 show_logging_cmd,
2941 "show logging", SHOW_STR "Show current logging configuration\n")
2942{
2943 struct zlog *zl = zlog_default;
2944
2945 vty_out(vty, "Syslog logging: ");
2946 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2947 vty_out(vty, "disabled");
2948 else
2949 vty_out(vty, "level %s, facility %s, ident %s",
2950 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2951 facility_name(zl->facility), zl->ident);
2952 vty_out(vty, "%s", VTY_NEWLINE);
2953
2954 vty_out(vty, "Stdout logging: ");
2955 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2956 vty_out(vty, "disabled");
2957 else
2958 vty_out(vty, "level %s",
2959 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2960 vty_out(vty, "%s", VTY_NEWLINE);
2961
2962 vty_out(vty, "Monitor logging: ");
2963 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2964 vty_out(vty, "disabled");
2965 else
2966 vty_out(vty, "level %s",
2967 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2968 vty_out(vty, "%s", VTY_NEWLINE);
2969
2970 vty_out(vty, "File logging: ");
2971 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2972 vty_out(vty, "disabled");
2973 else
2974 vty_out(vty, "level %s, filename %s",
2975 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2976 zl->filename);
2977 vty_out(vty, "%s", VTY_NEWLINE);
2978
2979 vty_out(vty, "Protocol name: %s%s",
2980 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2981 vty_out(vty, "Record priority: %s%s",
2982 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2983
2984 return CMD_SUCCESS;
2985}
2986
2987DEFUN(config_log_stdout,
2988 config_log_stdout_cmd,
2989 "log stdout", "Logging control\n" "Set stdout logging level\n")
2990{
2991 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2992 return CMD_SUCCESS;
2993}
2994
2995DEFUN(config_log_stdout_level,
2996 config_log_stdout_level_cmd,
2997 "log stdout " LOG_LEVELS,
2998 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
2999{
3000 int level;
3001
3002 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3003 return CMD_ERR_NO_MATCH;
3004 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3005 return CMD_SUCCESS;
3006}
3007
3008DEFUN(no_config_log_stdout,
3009 no_config_log_stdout_cmd,
3010 "no log stdout [LEVEL]",
3011 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3012{
3013 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3014 return CMD_SUCCESS;
3015}
3016
3017DEFUN(config_log_monitor,
3018 config_log_monitor_cmd,
3019 "log monitor",
3020 "Logging control\n" "Set terminal line (monitor) logging level\n")
3021{
3022 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3023 return CMD_SUCCESS;
3024}
3025
3026DEFUN(config_log_monitor_level,
3027 config_log_monitor_level_cmd,
3028 "log monitor " LOG_LEVELS,
3029 "Logging control\n"
3030 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3031{
3032 int level;
3033
3034 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3035 return CMD_ERR_NO_MATCH;
3036 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3037 return CMD_SUCCESS;
3038}
3039
3040DEFUN(no_config_log_monitor,
3041 no_config_log_monitor_cmd,
3042 "no log monitor [LEVEL]",
3043 NO_STR
3044 "Logging control\n"
3045 "Disable terminal line (monitor) logging\n" "Logging level\n")
3046{
3047 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3048 return CMD_SUCCESS;
3049}
3050
3051static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3052{
3053 int ret;
3054 char *p = NULL;
3055 const char *fullpath;
3056
3057 /* Path detection. */
3058 if (!IS_DIRECTORY_SEP(*fname)) {
3059 char cwd[MAXPATHLEN + 1];
3060 cwd[MAXPATHLEN] = '\0';
3061
3062 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3063 zlog_err("config_log_file: Unable to alloc mem!");
3064 return CMD_WARNING;
3065 }
3066
3067 if ((p = _talloc_zero(tall_vcmd_ctx,
3068 strlen(cwd) + strlen(fname) + 2),
3069 "set_log_file")
3070 == NULL) {
3071 zlog_err("config_log_file: Unable to alloc mem!");
3072 return CMD_WARNING;
3073 }
3074 sprintf(p, "%s/%s", cwd, fname);
3075 fullpath = p;
3076 } else
3077 fullpath = fname;
3078
3079 ret = zlog_set_file(NULL, fullpath, loglevel);
3080
3081 if (p)
3082 talloc_free(p);
3083
3084 if (!ret) {
3085 vty_out(vty, "can't open logfile %s\n", fname);
3086 return CMD_WARNING;
3087 }
3088
3089 if (host.logfile)
3090 talloc_free(host.logfile);
3091
3092 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
3093
3094 return CMD_SUCCESS;
3095}
3096
3097DEFUN(config_log_file,
3098 config_log_file_cmd,
3099 "log file FILENAME",
3100 "Logging control\n" "Logging to file\n" "Logging filename\n")
3101{
3102 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3103}
3104
3105DEFUN(config_log_file_level,
3106 config_log_file_level_cmd,
3107 "log file FILENAME " LOG_LEVELS,
3108 "Logging control\n"
3109 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3110{
3111 int level;
3112
3113 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3114 return CMD_ERR_NO_MATCH;
3115 return set_log_file(vty, argv[0], level);
3116}
3117
3118DEFUN(no_config_log_file,
3119 no_config_log_file_cmd,
3120 "no log file [FILENAME]",
3121 NO_STR
3122 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3123{
3124 zlog_reset_file(NULL);
3125
3126 if (host.logfile)
3127 talloc_free(host.logfile);
3128
3129 host.logfile = NULL;
3130
3131 return CMD_SUCCESS;
3132}
3133
3134ALIAS(no_config_log_file,
3135 no_config_log_file_level_cmd,
3136 "no log file FILENAME LEVEL",
3137 NO_STR
3138 "Logging control\n"
3139 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3140
3141 DEFUN(config_log_syslog,
3142 config_log_syslog_cmd,
3143 "log syslog", "Logging control\n" "Set syslog logging level\n")
3144{
3145 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3146 return CMD_SUCCESS;
3147}
3148
3149DEFUN(config_log_syslog_level,
3150 config_log_syslog_level_cmd,
3151 "log syslog " LOG_LEVELS,
3152 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3153{
3154 int level;
3155
3156 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3157 return CMD_ERR_NO_MATCH;
3158 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3159 return CMD_SUCCESS;
3160}
3161
3162DEFUN_DEPRECATED(config_log_syslog_facility,
3163 config_log_syslog_facility_cmd,
3164 "log syslog facility " LOG_FACILITIES,
3165 "Logging control\n"
3166 "Logging goes to syslog\n"
3167 "(Deprecated) Facility parameter for syslog messages\n"
3168 LOG_FACILITY_DESC)
3169{
3170 int facility;
3171
3172 if ((facility = facility_match(argv[0])) < 0)
3173 return CMD_ERR_NO_MATCH;
3174
3175 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3176 zlog_default->facility = facility;
3177 return CMD_SUCCESS;
3178}
3179
3180DEFUN(no_config_log_syslog,
3181 no_config_log_syslog_cmd,
3182 "no log syslog [LEVEL]",
3183 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3184{
3185 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3186 return CMD_SUCCESS;
3187}
3188
3189ALIAS(no_config_log_syslog,
3190 no_config_log_syslog_facility_cmd,
3191 "no log syslog facility " LOG_FACILITIES,
3192 NO_STR
3193 "Logging control\n"
3194 "Logging goes to syslog\n"
3195 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3196
3197 DEFUN(config_log_facility,
3198 config_log_facility_cmd,
3199 "log facility " LOG_FACILITIES,
3200 "Logging control\n"
3201 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3202{
3203 int facility;
3204
3205 if ((facility = facility_match(argv[0])) < 0)
3206 return CMD_ERR_NO_MATCH;
3207 zlog_default->facility = facility;
3208 return CMD_SUCCESS;
3209}
3210
3211DEFUN(no_config_log_facility,
3212 no_config_log_facility_cmd,
3213 "no log facility [FACILITY]",
3214 NO_STR
3215 "Logging control\n"
3216 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3217{
3218 zlog_default->facility = LOG_DAEMON;
3219 return CMD_SUCCESS;
3220}
3221
3222DEFUN_DEPRECATED(config_log_trap,
3223 config_log_trap_cmd,
3224 "log trap " LOG_LEVELS,
3225 "Logging control\n"
3226 "(Deprecated) Set logging level and default for all destinations\n"
3227 LOG_LEVEL_DESC)
3228{
3229 int new_level;
3230 int i;
3231
3232 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3233 return CMD_ERR_NO_MATCH;
3234
3235 zlog_default->default_lvl = new_level;
3236 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3237 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3238 zlog_default->maxlvl[i] = new_level;
3239 return CMD_SUCCESS;
3240}
3241
3242DEFUN_DEPRECATED(no_config_log_trap,
3243 no_config_log_trap_cmd,
3244 "no log trap [LEVEL]",
3245 NO_STR
3246 "Logging control\n"
3247 "Permit all logging information\n" "Logging level\n")
3248{
3249 zlog_default->default_lvl = LOG_DEBUG;
3250 return CMD_SUCCESS;
3251}
3252
3253DEFUN(config_log_record_priority,
3254 config_log_record_priority_cmd,
3255 "log record-priority",
3256 "Logging control\n"
3257 "Log the priority of the message within the message\n")
3258{
3259 zlog_default->record_priority = 1;
3260 return CMD_SUCCESS;
3261}
3262
3263DEFUN(no_config_log_record_priority,
3264 no_config_log_record_priority_cmd,
3265 "no log record-priority",
3266 NO_STR
3267 "Logging control\n"
3268 "Do not log the priority of the message within the message\n")
3269{
3270 zlog_default->record_priority = 0;
3271 return CMD_SUCCESS;
3272}
3273#endif
3274
3275DEFUN(banner_motd_file,
3276 banner_motd_file_cmd,
3277 "banner motd file [FILE]",
3278 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3279{
3280 if (host.motdfile)
3281 talloc_free(host.motdfile);
3282 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3283
3284 return CMD_SUCCESS;
3285}
3286
3287DEFUN(banner_motd_default,
3288 banner_motd_default_cmd,
3289 "banner motd default",
3290 "Set banner string\n" "Strings for motd\n" "Default string\n")
3291{
3292 host.motd = default_motd;
3293 return CMD_SUCCESS;
3294}
3295
3296DEFUN(no_banner_motd,
3297 no_banner_motd_cmd,
3298 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3299{
3300 host.motd = NULL;
3301 if (host.motdfile)
3302 talloc_free(host.motdfile);
3303 host.motdfile = NULL;
3304 return CMD_SUCCESS;
3305}
3306
3307/* Set config filename. Called from vty.c */
3308void host_config_set(const char *filename)
3309{
3310 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3311}
3312
3313void install_default(enum node_type node)
3314{
3315 install_element(node, &config_help_cmd);
3316 install_element(node, &config_list_cmd);
3317
3318 install_element(node, &config_write_terminal_cmd);
3319 install_element(node, &config_write_file_cmd);
3320 install_element(node, &config_write_memory_cmd);
3321 install_element(node, &config_write_cmd);
3322 install_element(node, &show_running_config_cmd);
3323}
3324
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003325void vty_install_default(enum node_type node)
3326{
3327 install_default(node);
3328
3329 install_element(node, &config_exit_cmd);
3330
3331 if (node >= CONFIG_NODE) {
3332 /* It's not a top node. */
3333 install_element(node, &config_end_cmd);
3334 }
3335}
3336
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003337/**
3338 * \brief Write the current running config to a given file
3339 * \param[in] vty the vty of the code
3340 * \param[in] filename where to store the file
3341 * \return 0 in case of success.
3342 *
3343 * If the filename already exists create a filename.sav
3344 * version with the current code.
3345 *
3346 */
3347int osmo_vty_write_config_file(const char *filename)
3348{
3349 char *failed_file;
3350 int rc;
3351
3352 rc = write_config_file(filename, &failed_file);
3353 talloc_free(failed_file);
3354 return rc;
3355}
3356
3357/**
3358 * \brief Save the current state to the config file
3359 * \return 0 in case of success.
3360 *
3361 * If the filename already exists create a filename.sav
3362 * version with the current code.
3363 *
3364 */
3365int osmo_vty_save_config_file(void)
3366{
3367 char *failed_file;
3368 int rc;
3369
3370 if (host.config == NULL)
3371 return -7;
3372
3373 rc = write_config_file(host.config, &failed_file);
3374 talloc_free(failed_file);
3375 return rc;
3376}
3377
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003378/* Initialize command interface. Install basic nodes and commands. */
3379void cmd_init(int terminal)
3380{
3381 /* Allocate initial top vector of commands. */
3382 cmdvec = vector_init(VECTOR_MIN_SIZE);
3383
3384 /* Default host value settings. */
3385 host.name = NULL;
3386 host.password = NULL;
3387 host.enable = NULL;
3388 host.logfile = NULL;
3389 host.config = NULL;
3390 host.lines = -1;
3391 host.motd = default_motd;
3392 host.motdfile = NULL;
3393
3394 /* Install top nodes. */
3395 install_node(&view_node, NULL);
3396 install_node(&enable_node, NULL);
3397 install_node(&auth_node, NULL);
3398 install_node(&auth_enable_node, NULL);
3399 install_node(&config_node, config_write_host);
3400
3401 /* Each node's basic commands. */
3402 install_element(VIEW_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003403 install_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003404 if (terminal) {
3405 install_element(VIEW_NODE, &config_list_cmd);
3406 install_element(VIEW_NODE, &config_exit_cmd);
3407 install_element(VIEW_NODE, &config_help_cmd);
3408 install_element(VIEW_NODE, &config_enable_cmd);
3409 install_element(VIEW_NODE, &config_terminal_length_cmd);
3410 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3411 install_element(VIEW_NODE, &echo_cmd);
3412 }
3413
3414 if (terminal) {
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003415 vty_install_default(ENABLE_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003416 install_element(ENABLE_NODE, &config_disable_cmd);
3417 install_element(ENABLE_NODE, &config_terminal_cmd);
3418 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3419 }
3420 install_element (ENABLE_NODE, &show_startup_config_cmd);
3421 install_element(ENABLE_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003422 install_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003423
3424 if (terminal) {
3425 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3426 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3427 install_element(ENABLE_NODE, &echo_cmd);
3428
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003429 vty_install_default(CONFIG_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003430 }
3431
3432 install_element(CONFIG_NODE, &hostname_cmd);
3433 install_element(CONFIG_NODE, &no_hostname_cmd);
3434
3435 if (terminal) {
3436 install_element(CONFIG_NODE, &password_cmd);
3437 install_element(CONFIG_NODE, &password_text_cmd);
3438 install_element(CONFIG_NODE, &enable_password_cmd);
3439 install_element(CONFIG_NODE, &enable_password_text_cmd);
3440 install_element(CONFIG_NODE, &no_enable_password_cmd);
3441
3442#ifdef VTY_CRYPT_PW
3443 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3444 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3445#endif
3446 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3447 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3448 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3449 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3450 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3451
3452 }
3453 srand(time(NULL));
3454}
Harald Welte7acb30c2011-08-17 17:13:48 +02003455
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02003456/*! @} */