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