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