blob: 149eca380b4af0d1956c9fe98a251c5123524801 [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
Jaroslav Škarvada2b82c1c2015-11-11 16:02:54 +010021Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22Boston, MA 02110-1301, USA. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020023
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 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000551void install_element(int ntype, struct cmd_element *cmd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200552{
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 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00001900int vty_go_parent(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001901{
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
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +01002500 if (host.app_info->config_is_consistent) {
2501 rc = host.app_info->config_is_consistent(vty);
2502 if (!rc) {
2503 vty_out(vty, "Configuration is not consistent%s",
2504 VTY_NEWLINE);
2505 return CMD_WARNING;
2506 }
2507 }
2508
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002509 if (host.config == NULL) {
2510 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2511 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002512 return CMD_WARNING;
2513 }
2514
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002515 rc = write_config_file(host.config, &failed_file);
2516 switch (rc) {
2517 case -1:
2518 vty_out(vty, "Can't open configuration file %s.%s",
2519 failed_file, VTY_NEWLINE);
2520 rc = CMD_WARNING;
2521 break;
2522 case -2:
2523 vty_out(vty, "Can't unlink backup configuration file %s.%s",
2524 failed_file, VTY_NEWLINE);
2525 rc = CMD_WARNING;
2526 break;
2527 case -3:
2528 vty_out(vty, "Can't backup old configuration file %s.%s",
2529 failed_file, VTY_NEWLINE);
2530 rc = CMD_WARNING;
2531 break;
2532 case -4:
2533 vty_out(vty, "Can't unlink configuration file %s.%s",
2534 failed_file, VTY_NEWLINE);
2535 rc = CMD_WARNING;
2536 break;
2537 case -5:
2538 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
2539 VTY_NEWLINE);
2540 rc = CMD_WARNING;
2541 break;
2542 case -6:
2543 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2544 failed_file, strerror(errno), errno, VTY_NEWLINE);
2545 rc = CMD_WARNING;
2546 break;
2547 default:
2548 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
2549 rc = CMD_SUCCESS;
2550 break;
2551 }
2552
2553 talloc_free(failed_file);
2554 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002555}
2556
2557ALIAS(config_write_file,
2558 config_write_cmd,
2559 "write", "Write running configuration to memory, network, or terminal\n")
2560
2561 ALIAS(config_write_file,
2562 config_write_memory_cmd,
2563 "write memory",
2564 "Write running configuration to memory, network, or terminal\n"
2565 "Write configuration to the file (same as write file)\n")
2566
2567 ALIAS(config_write_file,
2568 copy_runningconfig_startupconfig_cmd,
2569 "copy running-config startup-config",
2570 "Copy configuration\n"
2571 "Copy running config to... \n"
2572 "Copy running config to startup config (same as write file)\n")
2573
2574/* Write current configuration into the terminal. */
2575 DEFUN(config_write_terminal,
2576 config_write_terminal_cmd,
2577 "write terminal",
2578 "Write running configuration to memory, network, or terminal\n"
2579 "Write to terminal\n")
2580{
2581 unsigned int i;
2582 struct cmd_node *node;
2583
2584 if (vty->type == VTY_SHELL_SERV) {
2585 for (i = 0; i < vector_active(cmdvec); i++)
2586 if ((node = vector_slot(cmdvec, i)) && node->func
2587 && node->vtysh) {
2588 if ((*node->func) (vty))
2589 vty_out(vty, "!%s", VTY_NEWLINE);
2590 }
2591 } else {
2592 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2593 VTY_NEWLINE);
2594 vty_out(vty, "!%s", VTY_NEWLINE);
2595
2596 for (i = 0; i < vector_active(cmdvec); i++)
2597 if ((node = vector_slot(cmdvec, i)) && node->func) {
2598 if ((*node->func) (vty))
2599 vty_out(vty, "!%s", VTY_NEWLINE);
2600 }
2601 vty_out(vty, "end%s", VTY_NEWLINE);
2602 }
2603 return CMD_SUCCESS;
2604}
2605
2606/* Write current configuration into the terminal. */
2607ALIAS(config_write_terminal,
2608 show_running_config_cmd,
2609 "show running-config", SHOW_STR "running configuration\n")
2610
2611/* Write startup configuration into the terminal. */
2612 DEFUN(show_startup_config,
2613 show_startup_config_cmd,
2614 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2615{
2616 char buf[BUFSIZ];
2617 FILE *confp;
2618
2619 confp = fopen(host.config, "r");
2620 if (confp == NULL) {
2621 vty_out(vty, "Can't open configuration file [%s]%s",
2622 host.config, VTY_NEWLINE);
2623 return CMD_WARNING;
2624 }
2625
2626 while (fgets(buf, BUFSIZ, confp)) {
2627 char *cp = buf;
2628
2629 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2630 cp++;
2631 *cp = '\0';
2632
2633 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2634 }
2635
2636 fclose(confp);
2637
2638 return CMD_SUCCESS;
2639}
2640
2641/* Hostname configuration */
2642DEFUN(config_hostname,
2643 hostname_cmd,
2644 "hostname WORD",
2645 "Set system's network name\n" "This system's network name\n")
2646{
2647 if (!isalpha((int)*argv[0])) {
2648 vty_out(vty, "Please specify string starting with alphabet%s",
2649 VTY_NEWLINE);
2650 return CMD_WARNING;
2651 }
2652
2653 if (host.name)
2654 talloc_free(host.name);
2655
2656 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2657 return CMD_SUCCESS;
2658}
2659
2660DEFUN(config_no_hostname,
2661 no_hostname_cmd,
2662 "no hostname [HOSTNAME]",
2663 NO_STR "Reset system's network name\n" "Host name of this router\n")
2664{
2665 if (host.name)
2666 talloc_free(host.name);
2667 host.name = NULL;
2668 return CMD_SUCCESS;
2669}
2670
2671/* VTY interface password set. */
2672DEFUN(config_password, password_cmd,
2673 "password (8|) WORD",
2674 "Assign the terminal connection password\n"
2675 "Specifies a HIDDEN password will follow\n"
2676 "dummy string \n" "The HIDDEN line password string\n")
2677{
2678 /* Argument check. */
2679 if (argc == 0) {
2680 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2681 return CMD_WARNING;
2682 }
2683
2684 if (argc == 2) {
2685 if (*argv[0] == '8') {
2686 if (host.password)
2687 talloc_free(host.password);
2688 host.password = NULL;
2689 if (host.password_encrypt)
2690 talloc_free(host.password_encrypt);
2691 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2692 return CMD_SUCCESS;
2693 } else {
2694 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2695 return CMD_WARNING;
2696 }
2697 }
2698
2699 if (!isalnum((int)*argv[0])) {
2700 vty_out(vty,
2701 "Please specify string starting with alphanumeric%s",
2702 VTY_NEWLINE);
2703 return CMD_WARNING;
2704 }
2705
2706 if (host.password)
2707 talloc_free(host.password);
2708 host.password = NULL;
2709
2710#ifdef VTY_CRYPT_PW
2711 if (host.encrypt) {
2712 if (host.password_encrypt)
2713 talloc_free(host.password_encrypt);
2714 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2715 } else
2716#endif
2717 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2718
2719 return CMD_SUCCESS;
2720}
2721
2722ALIAS(config_password, password_text_cmd,
2723 "password LINE",
2724 "Assign the terminal connection password\n"
2725 "The UNENCRYPTED (cleartext) line password\n")
2726
2727/* VTY enable password set. */
2728 DEFUN(config_enable_password, enable_password_cmd,
2729 "enable password (8|) WORD",
2730 "Modify enable password parameters\n"
2731 "Assign the privileged level password\n"
2732 "Specifies a HIDDEN password will follow\n"
2733 "dummy string \n" "The HIDDEN 'enable' password string\n")
2734{
2735 /* Argument check. */
2736 if (argc == 0) {
2737 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2738 return CMD_WARNING;
2739 }
2740
2741 /* Crypt type is specified. */
2742 if (argc == 2) {
2743 if (*argv[0] == '8') {
2744 if (host.enable)
2745 talloc_free(host.enable);
2746 host.enable = NULL;
2747
2748 if (host.enable_encrypt)
2749 talloc_free(host.enable_encrypt);
2750 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2751
2752 return CMD_SUCCESS;
2753 } else {
2754 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2755 return CMD_WARNING;
2756 }
2757 }
2758
2759 if (!isalnum((int)*argv[0])) {
2760 vty_out(vty,
2761 "Please specify string starting with alphanumeric%s",
2762 VTY_NEWLINE);
2763 return CMD_WARNING;
2764 }
2765
2766 if (host.enable)
2767 talloc_free(host.enable);
2768 host.enable = NULL;
2769
2770 /* Plain password input. */
2771#ifdef VTY_CRYPT_PW
2772 if (host.encrypt) {
2773 if (host.enable_encrypt)
2774 talloc_free(host.enable_encrypt);
2775 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2776 } else
2777#endif
2778 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2779
2780 return CMD_SUCCESS;
2781}
2782
2783ALIAS(config_enable_password,
2784 enable_password_text_cmd,
2785 "enable password LINE",
2786 "Modify enable password parameters\n"
2787 "Assign the privileged level password\n"
2788 "The UNENCRYPTED (cleartext) 'enable' password\n")
2789
2790/* VTY enable password delete. */
2791 DEFUN(no_config_enable_password, no_enable_password_cmd,
2792 "no enable password",
2793 NO_STR
2794 "Modify enable password parameters\n"
2795 "Assign the privileged level password\n")
2796{
2797 if (host.enable)
2798 talloc_free(host.enable);
2799 host.enable = NULL;
2800
2801 if (host.enable_encrypt)
2802 talloc_free(host.enable_encrypt);
2803 host.enable_encrypt = NULL;
2804
2805 return CMD_SUCCESS;
2806}
2807
2808#ifdef VTY_CRYPT_PW
2809DEFUN(service_password_encrypt,
2810 service_password_encrypt_cmd,
2811 "service password-encryption",
2812 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2813{
2814 if (host.encrypt)
2815 return CMD_SUCCESS;
2816
2817 host.encrypt = 1;
2818
2819 if (host.password) {
2820 if (host.password_encrypt)
2821 talloc_free(host.password_encrypt);
2822 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
2823 }
2824 if (host.enable) {
2825 if (host.enable_encrypt)
2826 talloc_free(host.enable_encrypt);
2827 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
2828 }
2829
2830 return CMD_SUCCESS;
2831}
2832
2833DEFUN(no_service_password_encrypt,
2834 no_service_password_encrypt_cmd,
2835 "no service password-encryption",
2836 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2837{
2838 if (!host.encrypt)
2839 return CMD_SUCCESS;
2840
2841 host.encrypt = 0;
2842
2843 if (host.password_encrypt)
2844 talloc_free(host.password_encrypt);
2845 host.password_encrypt = NULL;
2846
2847 if (host.enable_encrypt)
2848 talloc_free(host.enable_encrypt);
2849 host.enable_encrypt = NULL;
2850
2851 return CMD_SUCCESS;
2852}
2853#endif
2854
2855DEFUN(config_terminal_length, config_terminal_length_cmd,
2856 "terminal length <0-512>",
2857 "Set terminal line parameters\n"
2858 "Set number of lines on a screen\n"
2859 "Number of lines on screen (0 for no pausing)\n")
2860{
2861 int lines;
2862 char *endptr = NULL;
2863
2864 lines = strtol(argv[0], &endptr, 10);
2865 if (lines < 0 || lines > 512 || *endptr != '\0') {
2866 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2867 return CMD_WARNING;
2868 }
2869 vty->lines = lines;
2870
2871 return CMD_SUCCESS;
2872}
2873
2874DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2875 "terminal no length",
2876 "Set terminal line parameters\n"
2877 NO_STR "Set number of lines on a screen\n")
2878{
2879 vty->lines = -1;
2880 return CMD_SUCCESS;
2881}
2882
2883DEFUN(service_terminal_length, service_terminal_length_cmd,
2884 "service terminal-length <0-512>",
2885 "Set up miscellaneous service\n"
2886 "System wide terminal length configuration\n"
2887 "Number of lines of VTY (0 means no line control)\n")
2888{
2889 int lines;
2890 char *endptr = NULL;
2891
2892 lines = strtol(argv[0], &endptr, 10);
2893 if (lines < 0 || lines > 512 || *endptr != '\0') {
2894 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2895 return CMD_WARNING;
2896 }
2897 host.lines = lines;
2898
2899 return CMD_SUCCESS;
2900}
2901
2902DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2903 "no service terminal-length [<0-512>]",
2904 NO_STR
2905 "Set up miscellaneous service\n"
2906 "System wide terminal length configuration\n"
2907 "Number of lines of VTY (0 means no line control)\n")
2908{
2909 host.lines = -1;
2910 return CMD_SUCCESS;
2911}
2912
2913DEFUN_HIDDEN(do_echo,
2914 echo_cmd,
2915 "echo .MESSAGE",
2916 "Echo a message back to the vty\n" "The message to echo\n")
2917{
2918 char *message;
2919
2920 vty_out(vty, "%s%s",
2921 ((message =
2922 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2923 if (message)
2924 talloc_free(message);
2925 return CMD_SUCCESS;
2926}
2927
2928#if 0
2929DEFUN(config_logmsg,
2930 config_logmsg_cmd,
2931 "logmsg " LOG_LEVELS " .MESSAGE",
2932 "Send a message to enabled logging destinations\n"
2933 LOG_LEVEL_DESC "The message to send\n")
2934{
2935 int level;
2936 char *message;
2937
2938 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2939 return CMD_ERR_NO_MATCH;
2940
2941 zlog(NULL, level,
2942 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2943 if (message)
2944 talloc_free(message);
2945 return CMD_SUCCESS;
2946}
2947
2948DEFUN(show_logging,
2949 show_logging_cmd,
2950 "show logging", SHOW_STR "Show current logging configuration\n")
2951{
2952 struct zlog *zl = zlog_default;
2953
2954 vty_out(vty, "Syslog logging: ");
2955 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2956 vty_out(vty, "disabled");
2957 else
2958 vty_out(vty, "level %s, facility %s, ident %s",
2959 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2960 facility_name(zl->facility), zl->ident);
2961 vty_out(vty, "%s", VTY_NEWLINE);
2962
2963 vty_out(vty, "Stdout logging: ");
2964 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2965 vty_out(vty, "disabled");
2966 else
2967 vty_out(vty, "level %s",
2968 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2969 vty_out(vty, "%s", VTY_NEWLINE);
2970
2971 vty_out(vty, "Monitor logging: ");
2972 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2973 vty_out(vty, "disabled");
2974 else
2975 vty_out(vty, "level %s",
2976 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2977 vty_out(vty, "%s", VTY_NEWLINE);
2978
2979 vty_out(vty, "File logging: ");
2980 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2981 vty_out(vty, "disabled");
2982 else
2983 vty_out(vty, "level %s, filename %s",
2984 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2985 zl->filename);
2986 vty_out(vty, "%s", VTY_NEWLINE);
2987
2988 vty_out(vty, "Protocol name: %s%s",
2989 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2990 vty_out(vty, "Record priority: %s%s",
2991 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2992
2993 return CMD_SUCCESS;
2994}
2995
2996DEFUN(config_log_stdout,
2997 config_log_stdout_cmd,
2998 "log stdout", "Logging control\n" "Set stdout logging level\n")
2999{
3000 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3001 return CMD_SUCCESS;
3002}
3003
3004DEFUN(config_log_stdout_level,
3005 config_log_stdout_level_cmd,
3006 "log stdout " LOG_LEVELS,
3007 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
3008{
3009 int level;
3010
3011 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3012 return CMD_ERR_NO_MATCH;
3013 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3014 return CMD_SUCCESS;
3015}
3016
3017DEFUN(no_config_log_stdout,
3018 no_config_log_stdout_cmd,
3019 "no log stdout [LEVEL]",
3020 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3021{
3022 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3023 return CMD_SUCCESS;
3024}
3025
3026DEFUN(config_log_monitor,
3027 config_log_monitor_cmd,
3028 "log monitor",
3029 "Logging control\n" "Set terminal line (monitor) logging level\n")
3030{
3031 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3032 return CMD_SUCCESS;
3033}
3034
3035DEFUN(config_log_monitor_level,
3036 config_log_monitor_level_cmd,
3037 "log monitor " LOG_LEVELS,
3038 "Logging control\n"
3039 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3040{
3041 int level;
3042
3043 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3044 return CMD_ERR_NO_MATCH;
3045 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3046 return CMD_SUCCESS;
3047}
3048
3049DEFUN(no_config_log_monitor,
3050 no_config_log_monitor_cmd,
3051 "no log monitor [LEVEL]",
3052 NO_STR
3053 "Logging control\n"
3054 "Disable terminal line (monitor) logging\n" "Logging level\n")
3055{
3056 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3057 return CMD_SUCCESS;
3058}
3059
3060static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3061{
3062 int ret;
3063 char *p = NULL;
3064 const char *fullpath;
3065
3066 /* Path detection. */
3067 if (!IS_DIRECTORY_SEP(*fname)) {
3068 char cwd[MAXPATHLEN + 1];
3069 cwd[MAXPATHLEN] = '\0';
3070
3071 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3072 zlog_err("config_log_file: Unable to alloc mem!");
3073 return CMD_WARNING;
3074 }
3075
3076 if ((p = _talloc_zero(tall_vcmd_ctx,
3077 strlen(cwd) + strlen(fname) + 2),
3078 "set_log_file")
3079 == NULL) {
3080 zlog_err("config_log_file: Unable to alloc mem!");
3081 return CMD_WARNING;
3082 }
3083 sprintf(p, "%s/%s", cwd, fname);
3084 fullpath = p;
3085 } else
3086 fullpath = fname;
3087
3088 ret = zlog_set_file(NULL, fullpath, loglevel);
3089
3090 if (p)
3091 talloc_free(p);
3092
3093 if (!ret) {
3094 vty_out(vty, "can't open logfile %s\n", fname);
3095 return CMD_WARNING;
3096 }
3097
3098 if (host.logfile)
3099 talloc_free(host.logfile);
3100
3101 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
3102
3103 return CMD_SUCCESS;
3104}
3105
3106DEFUN(config_log_file,
3107 config_log_file_cmd,
3108 "log file FILENAME",
3109 "Logging control\n" "Logging to file\n" "Logging filename\n")
3110{
3111 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3112}
3113
3114DEFUN(config_log_file_level,
3115 config_log_file_level_cmd,
3116 "log file FILENAME " LOG_LEVELS,
3117 "Logging control\n"
3118 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3119{
3120 int level;
3121
3122 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3123 return CMD_ERR_NO_MATCH;
3124 return set_log_file(vty, argv[0], level);
3125}
3126
3127DEFUN(no_config_log_file,
3128 no_config_log_file_cmd,
3129 "no log file [FILENAME]",
3130 NO_STR
3131 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3132{
3133 zlog_reset_file(NULL);
3134
3135 if (host.logfile)
3136 talloc_free(host.logfile);
3137
3138 host.logfile = NULL;
3139
3140 return CMD_SUCCESS;
3141}
3142
3143ALIAS(no_config_log_file,
3144 no_config_log_file_level_cmd,
3145 "no log file FILENAME LEVEL",
3146 NO_STR
3147 "Logging control\n"
3148 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3149
3150 DEFUN(config_log_syslog,
3151 config_log_syslog_cmd,
3152 "log syslog", "Logging control\n" "Set syslog logging level\n")
3153{
3154 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3155 return CMD_SUCCESS;
3156}
3157
3158DEFUN(config_log_syslog_level,
3159 config_log_syslog_level_cmd,
3160 "log syslog " LOG_LEVELS,
3161 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3162{
3163 int level;
3164
3165 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3166 return CMD_ERR_NO_MATCH;
3167 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3168 return CMD_SUCCESS;
3169}
3170
3171DEFUN_DEPRECATED(config_log_syslog_facility,
3172 config_log_syslog_facility_cmd,
3173 "log syslog facility " LOG_FACILITIES,
3174 "Logging control\n"
3175 "Logging goes to syslog\n"
3176 "(Deprecated) Facility parameter for syslog messages\n"
3177 LOG_FACILITY_DESC)
3178{
3179 int facility;
3180
3181 if ((facility = facility_match(argv[0])) < 0)
3182 return CMD_ERR_NO_MATCH;
3183
3184 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3185 zlog_default->facility = facility;
3186 return CMD_SUCCESS;
3187}
3188
3189DEFUN(no_config_log_syslog,
3190 no_config_log_syslog_cmd,
3191 "no log syslog [LEVEL]",
3192 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3193{
3194 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3195 return CMD_SUCCESS;
3196}
3197
3198ALIAS(no_config_log_syslog,
3199 no_config_log_syslog_facility_cmd,
3200 "no log syslog facility " LOG_FACILITIES,
3201 NO_STR
3202 "Logging control\n"
3203 "Logging goes to syslog\n"
3204 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3205
3206 DEFUN(config_log_facility,
3207 config_log_facility_cmd,
3208 "log facility " LOG_FACILITIES,
3209 "Logging control\n"
3210 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3211{
3212 int facility;
3213
3214 if ((facility = facility_match(argv[0])) < 0)
3215 return CMD_ERR_NO_MATCH;
3216 zlog_default->facility = facility;
3217 return CMD_SUCCESS;
3218}
3219
3220DEFUN(no_config_log_facility,
3221 no_config_log_facility_cmd,
3222 "no log facility [FACILITY]",
3223 NO_STR
3224 "Logging control\n"
3225 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3226{
3227 zlog_default->facility = LOG_DAEMON;
3228 return CMD_SUCCESS;
3229}
3230
3231DEFUN_DEPRECATED(config_log_trap,
3232 config_log_trap_cmd,
3233 "log trap " LOG_LEVELS,
3234 "Logging control\n"
3235 "(Deprecated) Set logging level and default for all destinations\n"
3236 LOG_LEVEL_DESC)
3237{
3238 int new_level;
3239 int i;
3240
3241 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3242 return CMD_ERR_NO_MATCH;
3243
3244 zlog_default->default_lvl = new_level;
3245 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3246 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3247 zlog_default->maxlvl[i] = new_level;
3248 return CMD_SUCCESS;
3249}
3250
3251DEFUN_DEPRECATED(no_config_log_trap,
3252 no_config_log_trap_cmd,
3253 "no log trap [LEVEL]",
3254 NO_STR
3255 "Logging control\n"
3256 "Permit all logging information\n" "Logging level\n")
3257{
3258 zlog_default->default_lvl = LOG_DEBUG;
3259 return CMD_SUCCESS;
3260}
3261
3262DEFUN(config_log_record_priority,
3263 config_log_record_priority_cmd,
3264 "log record-priority",
3265 "Logging control\n"
3266 "Log the priority of the message within the message\n")
3267{
3268 zlog_default->record_priority = 1;
3269 return CMD_SUCCESS;
3270}
3271
3272DEFUN(no_config_log_record_priority,
3273 no_config_log_record_priority_cmd,
3274 "no log record-priority",
3275 NO_STR
3276 "Logging control\n"
3277 "Do not log the priority of the message within the message\n")
3278{
3279 zlog_default->record_priority = 0;
3280 return CMD_SUCCESS;
3281}
3282#endif
3283
3284DEFUN(banner_motd_file,
3285 banner_motd_file_cmd,
3286 "banner motd file [FILE]",
3287 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3288{
3289 if (host.motdfile)
3290 talloc_free(host.motdfile);
3291 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3292
3293 return CMD_SUCCESS;
3294}
3295
3296DEFUN(banner_motd_default,
3297 banner_motd_default_cmd,
3298 "banner motd default",
3299 "Set banner string\n" "Strings for motd\n" "Default string\n")
3300{
3301 host.motd = default_motd;
3302 return CMD_SUCCESS;
3303}
3304
3305DEFUN(no_banner_motd,
3306 no_banner_motd_cmd,
3307 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3308{
3309 host.motd = NULL;
3310 if (host.motdfile)
3311 talloc_free(host.motdfile);
3312 host.motdfile = NULL;
3313 return CMD_SUCCESS;
3314}
3315
3316/* Set config filename. Called from vty.c */
3317void host_config_set(const char *filename)
3318{
3319 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3320}
3321
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003322void install_default(int node)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003323{
3324 install_element(node, &config_help_cmd);
3325 install_element(node, &config_list_cmd);
3326
3327 install_element(node, &config_write_terminal_cmd);
3328 install_element(node, &config_write_file_cmd);
3329 install_element(node, &config_write_memory_cmd);
3330 install_element(node, &config_write_cmd);
3331 install_element(node, &show_running_config_cmd);
3332}
3333
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003334void vty_install_default(int node)
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003335{
3336 install_default(node);
3337
3338 install_element(node, &config_exit_cmd);
3339
3340 if (node >= CONFIG_NODE) {
3341 /* It's not a top node. */
3342 install_element(node, &config_end_cmd);
3343 }
3344}
3345
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003346/**
3347 * \brief Write the current running config to a given file
3348 * \param[in] vty the vty of the code
3349 * \param[in] filename where to store the file
3350 * \return 0 in case of success.
3351 *
3352 * If the filename already exists create a filename.sav
3353 * version with the current code.
3354 *
3355 */
3356int osmo_vty_write_config_file(const char *filename)
3357{
3358 char *failed_file;
3359 int rc;
3360
3361 rc = write_config_file(filename, &failed_file);
3362 talloc_free(failed_file);
3363 return rc;
3364}
3365
3366/**
3367 * \brief Save the current state to the config file
3368 * \return 0 in case of success.
3369 *
3370 * If the filename already exists create a filename.sav
3371 * version with the current code.
3372 *
3373 */
3374int osmo_vty_save_config_file(void)
3375{
3376 char *failed_file;
3377 int rc;
3378
3379 if (host.config == NULL)
3380 return -7;
3381
3382 rc = write_config_file(host.config, &failed_file);
3383 talloc_free(failed_file);
3384 return rc;
3385}
3386
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003387/* Initialize command interface. Install basic nodes and commands. */
3388void cmd_init(int terminal)
3389{
3390 /* Allocate initial top vector of commands. */
3391 cmdvec = vector_init(VECTOR_MIN_SIZE);
3392
3393 /* Default host value settings. */
3394 host.name = NULL;
3395 host.password = NULL;
3396 host.enable = NULL;
3397 host.logfile = NULL;
3398 host.config = NULL;
3399 host.lines = -1;
3400 host.motd = default_motd;
3401 host.motdfile = NULL;
3402
3403 /* Install top nodes. */
3404 install_node(&view_node, NULL);
3405 install_node(&enable_node, NULL);
3406 install_node(&auth_node, NULL);
3407 install_node(&auth_enable_node, NULL);
3408 install_node(&config_node, config_write_host);
3409
3410 /* Each node's basic commands. */
3411 install_element(VIEW_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003412 install_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003413 if (terminal) {
3414 install_element(VIEW_NODE, &config_list_cmd);
3415 install_element(VIEW_NODE, &config_exit_cmd);
3416 install_element(VIEW_NODE, &config_help_cmd);
3417 install_element(VIEW_NODE, &config_enable_cmd);
3418 install_element(VIEW_NODE, &config_terminal_length_cmd);
3419 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3420 install_element(VIEW_NODE, &echo_cmd);
3421 }
3422
3423 if (terminal) {
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003424 vty_install_default(ENABLE_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003425 install_element(ENABLE_NODE, &config_disable_cmd);
3426 install_element(ENABLE_NODE, &config_terminal_cmd);
3427 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3428 }
3429 install_element (ENABLE_NODE, &show_startup_config_cmd);
3430 install_element(ENABLE_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003431 install_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003432
3433 if (terminal) {
3434 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3435 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3436 install_element(ENABLE_NODE, &echo_cmd);
3437
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003438 vty_install_default(CONFIG_NODE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003439 }
3440
3441 install_element(CONFIG_NODE, &hostname_cmd);
3442 install_element(CONFIG_NODE, &no_hostname_cmd);
3443
3444 if (terminal) {
3445 install_element(CONFIG_NODE, &password_cmd);
3446 install_element(CONFIG_NODE, &password_text_cmd);
3447 install_element(CONFIG_NODE, &enable_password_cmd);
3448 install_element(CONFIG_NODE, &enable_password_text_cmd);
3449 install_element(CONFIG_NODE, &no_enable_password_cmd);
3450
3451#ifdef VTY_CRYPT_PW
3452 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3453 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3454#endif
3455 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3456 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3457 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3458 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3459 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3460
3461 }
3462 srand(time(NULL));
3463}
Harald Welte7acb30c2011-08-17 17:13:48 +02003464
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02003465/*! @} */