blob: 5dc1dd4525b52f64c43d145c1a56dc468c9e6b8f [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
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010041#include <osmocom/core/talloc.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020042
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;
Harald Welte28222962011-02-18 20:37:04 +01002183 case CFG_LOG_NODE:
2184 vty->node = CONFIG_NODE;
2185 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002186 default:
2187 break;
2188 }
2189 return CMD_SUCCESS;
2190}
2191
2192/* End of configuration. */
2193 gDEFUN(config_end,
2194 config_end_cmd, "end", "End current mode and change to enable mode.")
2195{
2196 switch (vty->node) {
2197 case VIEW_NODE:
2198 case ENABLE_NODE:
2199 /* Nothing to do. */
2200 break;
Harald Welte28222962011-02-18 20:37:04 +01002201 case CFG_LOG_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002202 case CONFIG_NODE:
2203 case VTY_NODE:
2204 vty_config_unlock(vty);
2205 vty->node = ENABLE_NODE;
2206 break;
2207 default:
2208 break;
2209 }
2210 return CMD_SUCCESS;
2211}
2212
2213/* Show version. */
2214DEFUN(show_version,
2215 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2216{
Harald Welte237f6242010-05-25 23:00:45 +02002217 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2218 host.app_info->version,
2219 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2220 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002221
2222 return CMD_SUCCESS;
2223}
2224
2225/* Help display function for all node. */
2226gDEFUN(config_help,
2227 config_help_cmd, "help", "Description of the interactive help system\n")
2228{
2229 vty_out(vty,
2230 "This VTY provides advanced help features. When you need help,%s\
2231anytime at the command line please press '?'.%s\
2232%s\
2233If nothing matches, the help list will be empty and you must backup%s\
2234 until entering a '?' shows the available options.%s\
2235Two styles of help are provided:%s\
22361. Full help is available when you are ready to enter a%s\
2237command argument (e.g. 'show ?') and describes each possible%s\
2238argument.%s\
22392. Partial help is provided when an abbreviated argument is entered%s\
2240 and you want to know what arguments match the input%s\
2241 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2242 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2243 return CMD_SUCCESS;
2244}
2245
2246/* Help display function for all node. */
2247gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2248{
2249 unsigned int i;
2250 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2251 struct cmd_element *cmd;
2252
2253 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2254 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2255 && !(cmd->attr == CMD_ATTR_DEPRECATED
2256 || cmd->attr == CMD_ATTR_HIDDEN))
2257 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2258 return CMD_SUCCESS;
2259}
2260
2261/* Write current configuration into file. */
2262DEFUN(config_write_file,
2263 config_write_file_cmd,
2264 "write file",
2265 "Write running configuration to memory, network, or terminal\n"
2266 "Write to configuration file\n")
2267{
2268 unsigned int i;
2269 int fd;
2270 struct cmd_node *node;
2271 char *config_file;
2272 char *config_file_tmp = NULL;
2273 char *config_file_sav = NULL;
2274 struct vty *file_vty;
2275
2276 /* Check and see if we are operating under vtysh configuration */
2277 if (host.config == NULL) {
2278 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2279 VTY_NEWLINE);
2280 return CMD_WARNING;
2281 }
2282
2283 /* Get filename. */
2284 config_file = host.config;
2285
2286 config_file_sav =
2287 _talloc_zero(tall_vty_cmd_ctx,
2288 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2289 "config_file_sav");
2290 strcpy(config_file_sav, config_file);
2291 strcat(config_file_sav, CONF_BACKUP_EXT);
2292
2293 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2294 "config_file_tmp");
2295 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2296
2297 /* Open file to configuration write. */
2298 fd = mkstemp(config_file_tmp);
2299 if (fd < 0) {
2300 vty_out(vty, "Can't open configuration file %s.%s",
2301 config_file_tmp, VTY_NEWLINE);
2302 talloc_free(config_file_tmp);
2303 talloc_free(config_file_sav);
2304 return CMD_WARNING;
2305 }
2306
2307 /* Make vty for configuration file. */
2308 file_vty = vty_new();
2309 file_vty->fd = fd;
2310 file_vty->type = VTY_FILE;
2311
2312 /* Config file header print. */
2313 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02002314 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002315 //vty_time_print (file_vty, 1);
2316 vty_out(file_vty, "!\n");
2317
2318 for (i = 0; i < vector_active(cmdvec); i++)
2319 if ((node = vector_slot(cmdvec, i)) && node->func) {
2320 if ((*node->func) (file_vty))
2321 vty_out(file_vty, "!\n");
2322 }
2323 vty_close(file_vty);
2324
2325 if (unlink(config_file_sav) != 0)
2326 if (errno != ENOENT) {
2327 vty_out(vty,
2328 "Can't unlink backup configuration file %s.%s",
2329 config_file_sav, VTY_NEWLINE);
2330 talloc_free(config_file_sav);
2331 talloc_free(config_file_tmp);
2332 unlink(config_file_tmp);
2333 return CMD_WARNING;
2334 }
2335 if (link(config_file, config_file_sav) != 0) {
2336 vty_out(vty, "Can't backup old configuration file %s.%s",
2337 config_file_sav, VTY_NEWLINE);
2338 talloc_free(config_file_sav);
2339 talloc_free(config_file_tmp);
2340 unlink(config_file_tmp);
2341 return CMD_WARNING;
2342 }
2343 sync();
2344 if (unlink(config_file) != 0) {
2345 vty_out(vty, "Can't unlink configuration file %s.%s",
2346 config_file, VTY_NEWLINE);
2347 talloc_free(config_file_sav);
2348 talloc_free(config_file_tmp);
2349 unlink(config_file_tmp);
2350 return CMD_WARNING;
2351 }
2352 if (link(config_file_tmp, config_file) != 0) {
2353 vty_out(vty, "Can't save configuration file %s.%s", config_file,
2354 VTY_NEWLINE);
2355 talloc_free(config_file_sav);
2356 talloc_free(config_file_tmp);
2357 unlink(config_file_tmp);
2358 return CMD_WARNING;
2359 }
2360 unlink(config_file_tmp);
2361 sync();
2362
2363 talloc_free(config_file_sav);
2364 talloc_free(config_file_tmp);
2365
2366 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
2367 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2368 config_file, strerror(errno), errno, VTY_NEWLINE);
2369 return CMD_WARNING;
2370 }
2371
2372 vty_out(vty, "Configuration saved to %s%s", config_file, VTY_NEWLINE);
2373 return CMD_SUCCESS;
2374}
2375
2376ALIAS(config_write_file,
2377 config_write_cmd,
2378 "write", "Write running configuration to memory, network, or terminal\n")
2379
2380 ALIAS(config_write_file,
2381 config_write_memory_cmd,
2382 "write memory",
2383 "Write running configuration to memory, network, or terminal\n"
2384 "Write configuration to the file (same as write file)\n")
2385
2386 ALIAS(config_write_file,
2387 copy_runningconfig_startupconfig_cmd,
2388 "copy running-config startup-config",
2389 "Copy configuration\n"
2390 "Copy running config to... \n"
2391 "Copy running config to startup config (same as write file)\n")
2392
2393/* Write current configuration into the terminal. */
2394 DEFUN(config_write_terminal,
2395 config_write_terminal_cmd,
2396 "write terminal",
2397 "Write running configuration to memory, network, or terminal\n"
2398 "Write to terminal\n")
2399{
2400 unsigned int i;
2401 struct cmd_node *node;
2402
2403 if (vty->type == VTY_SHELL_SERV) {
2404 for (i = 0; i < vector_active(cmdvec); i++)
2405 if ((node = vector_slot(cmdvec, i)) && node->func
2406 && node->vtysh) {
2407 if ((*node->func) (vty))
2408 vty_out(vty, "!%s", VTY_NEWLINE);
2409 }
2410 } else {
2411 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2412 VTY_NEWLINE);
2413 vty_out(vty, "!%s", VTY_NEWLINE);
2414
2415 for (i = 0; i < vector_active(cmdvec); i++)
2416 if ((node = vector_slot(cmdvec, i)) && node->func) {
2417 if ((*node->func) (vty))
2418 vty_out(vty, "!%s", VTY_NEWLINE);
2419 }
2420 vty_out(vty, "end%s", VTY_NEWLINE);
2421 }
2422 return CMD_SUCCESS;
2423}
2424
2425/* Write current configuration into the terminal. */
2426ALIAS(config_write_terminal,
2427 show_running_config_cmd,
2428 "show running-config", SHOW_STR "running configuration\n")
2429
2430/* Write startup configuration into the terminal. */
2431 DEFUN(show_startup_config,
2432 show_startup_config_cmd,
2433 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2434{
2435 char buf[BUFSIZ];
2436 FILE *confp;
2437
2438 confp = fopen(host.config, "r");
2439 if (confp == NULL) {
2440 vty_out(vty, "Can't open configuration file [%s]%s",
2441 host.config, VTY_NEWLINE);
2442 return CMD_WARNING;
2443 }
2444
2445 while (fgets(buf, BUFSIZ, confp)) {
2446 char *cp = buf;
2447
2448 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2449 cp++;
2450 *cp = '\0';
2451
2452 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2453 }
2454
2455 fclose(confp);
2456
2457 return CMD_SUCCESS;
2458}
2459
2460/* Hostname configuration */
2461DEFUN(config_hostname,
2462 hostname_cmd,
2463 "hostname WORD",
2464 "Set system's network name\n" "This system's network name\n")
2465{
2466 if (!isalpha((int)*argv[0])) {
2467 vty_out(vty, "Please specify string starting with alphabet%s",
2468 VTY_NEWLINE);
2469 return CMD_WARNING;
2470 }
2471
2472 if (host.name)
2473 talloc_free(host.name);
2474
2475 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2476 return CMD_SUCCESS;
2477}
2478
2479DEFUN(config_no_hostname,
2480 no_hostname_cmd,
2481 "no hostname [HOSTNAME]",
2482 NO_STR "Reset system's network name\n" "Host name of this router\n")
2483{
2484 if (host.name)
2485 talloc_free(host.name);
2486 host.name = NULL;
2487 return CMD_SUCCESS;
2488}
2489
2490/* VTY interface password set. */
2491DEFUN(config_password, password_cmd,
2492 "password (8|) WORD",
2493 "Assign the terminal connection password\n"
2494 "Specifies a HIDDEN password will follow\n"
2495 "dummy string \n" "The HIDDEN line password string\n")
2496{
2497 /* Argument check. */
2498 if (argc == 0) {
2499 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2500 return CMD_WARNING;
2501 }
2502
2503 if (argc == 2) {
2504 if (*argv[0] == '8') {
2505 if (host.password)
2506 talloc_free(host.password);
2507 host.password = NULL;
2508 if (host.password_encrypt)
2509 talloc_free(host.password_encrypt);
2510 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2511 return CMD_SUCCESS;
2512 } else {
2513 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2514 return CMD_WARNING;
2515 }
2516 }
2517
2518 if (!isalnum((int)*argv[0])) {
2519 vty_out(vty,
2520 "Please specify string starting with alphanumeric%s",
2521 VTY_NEWLINE);
2522 return CMD_WARNING;
2523 }
2524
2525 if (host.password)
2526 talloc_free(host.password);
2527 host.password = NULL;
2528
2529#ifdef VTY_CRYPT_PW
2530 if (host.encrypt) {
2531 if (host.password_encrypt)
2532 talloc_free(host.password_encrypt);
2533 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2534 } else
2535#endif
2536 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2537
2538 return CMD_SUCCESS;
2539}
2540
2541ALIAS(config_password, password_text_cmd,
2542 "password LINE",
2543 "Assign the terminal connection password\n"
2544 "The UNENCRYPTED (cleartext) line password\n")
2545
2546/* VTY enable password set. */
2547 DEFUN(config_enable_password, enable_password_cmd,
2548 "enable password (8|) WORD",
2549 "Modify enable password parameters\n"
2550 "Assign the privileged level password\n"
2551 "Specifies a HIDDEN password will follow\n"
2552 "dummy string \n" "The HIDDEN 'enable' password string\n")
2553{
2554 /* Argument check. */
2555 if (argc == 0) {
2556 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2557 return CMD_WARNING;
2558 }
2559
2560 /* Crypt type is specified. */
2561 if (argc == 2) {
2562 if (*argv[0] == '8') {
2563 if (host.enable)
2564 talloc_free(host.enable);
2565 host.enable = NULL;
2566
2567 if (host.enable_encrypt)
2568 talloc_free(host.enable_encrypt);
2569 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2570
2571 return CMD_SUCCESS;
2572 } else {
2573 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2574 return CMD_WARNING;
2575 }
2576 }
2577
2578 if (!isalnum((int)*argv[0])) {
2579 vty_out(vty,
2580 "Please specify string starting with alphanumeric%s",
2581 VTY_NEWLINE);
2582 return CMD_WARNING;
2583 }
2584
2585 if (host.enable)
2586 talloc_free(host.enable);
2587 host.enable = NULL;
2588
2589 /* Plain password input. */
2590#ifdef VTY_CRYPT_PW
2591 if (host.encrypt) {
2592 if (host.enable_encrypt)
2593 talloc_free(host.enable_encrypt);
2594 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2595 } else
2596#endif
2597 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2598
2599 return CMD_SUCCESS;
2600}
2601
2602ALIAS(config_enable_password,
2603 enable_password_text_cmd,
2604 "enable password LINE",
2605 "Modify enable password parameters\n"
2606 "Assign the privileged level password\n"
2607 "The UNENCRYPTED (cleartext) 'enable' password\n")
2608
2609/* VTY enable password delete. */
2610 DEFUN(no_config_enable_password, no_enable_password_cmd,
2611 "no enable password",
2612 NO_STR
2613 "Modify enable password parameters\n"
2614 "Assign the privileged level password\n")
2615{
2616 if (host.enable)
2617 talloc_free(host.enable);
2618 host.enable = NULL;
2619
2620 if (host.enable_encrypt)
2621 talloc_free(host.enable_encrypt);
2622 host.enable_encrypt = NULL;
2623
2624 return CMD_SUCCESS;
2625}
2626
2627#ifdef VTY_CRYPT_PW
2628DEFUN(service_password_encrypt,
2629 service_password_encrypt_cmd,
2630 "service password-encryption",
2631 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2632{
2633 if (host.encrypt)
2634 return CMD_SUCCESS;
2635
2636 host.encrypt = 1;
2637
2638 if (host.password) {
2639 if (host.password_encrypt)
2640 talloc_free(host.password_encrypt);
2641 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
2642 }
2643 if (host.enable) {
2644 if (host.enable_encrypt)
2645 talloc_free(host.enable_encrypt);
2646 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
2647 }
2648
2649 return CMD_SUCCESS;
2650}
2651
2652DEFUN(no_service_password_encrypt,
2653 no_service_password_encrypt_cmd,
2654 "no service password-encryption",
2655 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2656{
2657 if (!host.encrypt)
2658 return CMD_SUCCESS;
2659
2660 host.encrypt = 0;
2661
2662 if (host.password_encrypt)
2663 talloc_free(host.password_encrypt);
2664 host.password_encrypt = NULL;
2665
2666 if (host.enable_encrypt)
2667 talloc_free(host.enable_encrypt);
2668 host.enable_encrypt = NULL;
2669
2670 return CMD_SUCCESS;
2671}
2672#endif
2673
2674DEFUN(config_terminal_length, config_terminal_length_cmd,
2675 "terminal length <0-512>",
2676 "Set terminal line parameters\n"
2677 "Set number of lines on a screen\n"
2678 "Number of lines on screen (0 for no pausing)\n")
2679{
2680 int lines;
2681 char *endptr = NULL;
2682
2683 lines = strtol(argv[0], &endptr, 10);
2684 if (lines < 0 || lines > 512 || *endptr != '\0') {
2685 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2686 return CMD_WARNING;
2687 }
2688 vty->lines = lines;
2689
2690 return CMD_SUCCESS;
2691}
2692
2693DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2694 "terminal no length",
2695 "Set terminal line parameters\n"
2696 NO_STR "Set number of lines on a screen\n")
2697{
2698 vty->lines = -1;
2699 return CMD_SUCCESS;
2700}
2701
2702DEFUN(service_terminal_length, service_terminal_length_cmd,
2703 "service terminal-length <0-512>",
2704 "Set up miscellaneous service\n"
2705 "System wide terminal length configuration\n"
2706 "Number of lines of VTY (0 means no line control)\n")
2707{
2708 int lines;
2709 char *endptr = NULL;
2710
2711 lines = strtol(argv[0], &endptr, 10);
2712 if (lines < 0 || lines > 512 || *endptr != '\0') {
2713 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2714 return CMD_WARNING;
2715 }
2716 host.lines = lines;
2717
2718 return CMD_SUCCESS;
2719}
2720
2721DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2722 "no service terminal-length [<0-512>]",
2723 NO_STR
2724 "Set up miscellaneous service\n"
2725 "System wide terminal length configuration\n"
2726 "Number of lines of VTY (0 means no line control)\n")
2727{
2728 host.lines = -1;
2729 return CMD_SUCCESS;
2730}
2731
2732DEFUN_HIDDEN(do_echo,
2733 echo_cmd,
2734 "echo .MESSAGE",
2735 "Echo a message back to the vty\n" "The message to echo\n")
2736{
2737 char *message;
2738
2739 vty_out(vty, "%s%s",
2740 ((message =
2741 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2742 if (message)
2743 talloc_free(message);
2744 return CMD_SUCCESS;
2745}
2746
2747#if 0
2748DEFUN(config_logmsg,
2749 config_logmsg_cmd,
2750 "logmsg " LOG_LEVELS " .MESSAGE",
2751 "Send a message to enabled logging destinations\n"
2752 LOG_LEVEL_DESC "The message to send\n")
2753{
2754 int level;
2755 char *message;
2756
2757 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2758 return CMD_ERR_NO_MATCH;
2759
2760 zlog(NULL, level,
2761 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2762 if (message)
2763 talloc_free(message);
2764 return CMD_SUCCESS;
2765}
2766
2767DEFUN(show_logging,
2768 show_logging_cmd,
2769 "show logging", SHOW_STR "Show current logging configuration\n")
2770{
2771 struct zlog *zl = zlog_default;
2772
2773 vty_out(vty, "Syslog logging: ");
2774 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2775 vty_out(vty, "disabled");
2776 else
2777 vty_out(vty, "level %s, facility %s, ident %s",
2778 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2779 facility_name(zl->facility), zl->ident);
2780 vty_out(vty, "%s", VTY_NEWLINE);
2781
2782 vty_out(vty, "Stdout logging: ");
2783 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2784 vty_out(vty, "disabled");
2785 else
2786 vty_out(vty, "level %s",
2787 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2788 vty_out(vty, "%s", VTY_NEWLINE);
2789
2790 vty_out(vty, "Monitor logging: ");
2791 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2792 vty_out(vty, "disabled");
2793 else
2794 vty_out(vty, "level %s",
2795 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2796 vty_out(vty, "%s", VTY_NEWLINE);
2797
2798 vty_out(vty, "File logging: ");
2799 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2800 vty_out(vty, "disabled");
2801 else
2802 vty_out(vty, "level %s, filename %s",
2803 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2804 zl->filename);
2805 vty_out(vty, "%s", VTY_NEWLINE);
2806
2807 vty_out(vty, "Protocol name: %s%s",
2808 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2809 vty_out(vty, "Record priority: %s%s",
2810 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2811
2812 return CMD_SUCCESS;
2813}
2814
2815DEFUN(config_log_stdout,
2816 config_log_stdout_cmd,
2817 "log stdout", "Logging control\n" "Set stdout logging level\n")
2818{
2819 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2820 return CMD_SUCCESS;
2821}
2822
2823DEFUN(config_log_stdout_level,
2824 config_log_stdout_level_cmd,
2825 "log stdout " LOG_LEVELS,
2826 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
2827{
2828 int level;
2829
2830 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2831 return CMD_ERR_NO_MATCH;
2832 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
2833 return CMD_SUCCESS;
2834}
2835
2836DEFUN(no_config_log_stdout,
2837 no_config_log_stdout_cmd,
2838 "no log stdout [LEVEL]",
2839 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
2840{
2841 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
2842 return CMD_SUCCESS;
2843}
2844
2845DEFUN(config_log_monitor,
2846 config_log_monitor_cmd,
2847 "log monitor",
2848 "Logging control\n" "Set terminal line (monitor) logging level\n")
2849{
2850 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
2851 return CMD_SUCCESS;
2852}
2853
2854DEFUN(config_log_monitor_level,
2855 config_log_monitor_level_cmd,
2856 "log monitor " LOG_LEVELS,
2857 "Logging control\n"
2858 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
2859{
2860 int level;
2861
2862 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2863 return CMD_ERR_NO_MATCH;
2864 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
2865 return CMD_SUCCESS;
2866}
2867
2868DEFUN(no_config_log_monitor,
2869 no_config_log_monitor_cmd,
2870 "no log monitor [LEVEL]",
2871 NO_STR
2872 "Logging control\n"
2873 "Disable terminal line (monitor) logging\n" "Logging level\n")
2874{
2875 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
2876 return CMD_SUCCESS;
2877}
2878
2879static int set_log_file(struct vty *vty, const char *fname, int loglevel)
2880{
2881 int ret;
2882 char *p = NULL;
2883 const char *fullpath;
2884
2885 /* Path detection. */
2886 if (!IS_DIRECTORY_SEP(*fname)) {
2887 char cwd[MAXPATHLEN + 1];
2888 cwd[MAXPATHLEN] = '\0';
2889
2890 if (getcwd(cwd, MAXPATHLEN) == NULL) {
2891 zlog_err("config_log_file: Unable to alloc mem!");
2892 return CMD_WARNING;
2893 }
2894
2895 if ((p = _talloc_zero(tall_vcmd_ctx,
2896 strlen(cwd) + strlen(fname) + 2),
2897 "set_log_file")
2898 == NULL) {
2899 zlog_err("config_log_file: Unable to alloc mem!");
2900 return CMD_WARNING;
2901 }
2902 sprintf(p, "%s/%s", cwd, fname);
2903 fullpath = p;
2904 } else
2905 fullpath = fname;
2906
2907 ret = zlog_set_file(NULL, fullpath, loglevel);
2908
2909 if (p)
2910 talloc_free(p);
2911
2912 if (!ret) {
2913 vty_out(vty, "can't open logfile %s\n", fname);
2914 return CMD_WARNING;
2915 }
2916
2917 if (host.logfile)
2918 talloc_free(host.logfile);
2919
2920 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
2921
2922 return CMD_SUCCESS;
2923}
2924
2925DEFUN(config_log_file,
2926 config_log_file_cmd,
2927 "log file FILENAME",
2928 "Logging control\n" "Logging to file\n" "Logging filename\n")
2929{
2930 return set_log_file(vty, argv[0], zlog_default->default_lvl);
2931}
2932
2933DEFUN(config_log_file_level,
2934 config_log_file_level_cmd,
2935 "log file FILENAME " LOG_LEVELS,
2936 "Logging control\n"
2937 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
2938{
2939 int level;
2940
2941 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
2942 return CMD_ERR_NO_MATCH;
2943 return set_log_file(vty, argv[0], level);
2944}
2945
2946DEFUN(no_config_log_file,
2947 no_config_log_file_cmd,
2948 "no log file [FILENAME]",
2949 NO_STR
2950 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
2951{
2952 zlog_reset_file(NULL);
2953
2954 if (host.logfile)
2955 talloc_free(host.logfile);
2956
2957 host.logfile = NULL;
2958
2959 return CMD_SUCCESS;
2960}
2961
2962ALIAS(no_config_log_file,
2963 no_config_log_file_level_cmd,
2964 "no log file FILENAME LEVEL",
2965 NO_STR
2966 "Logging control\n"
2967 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
2968
2969 DEFUN(config_log_syslog,
2970 config_log_syslog_cmd,
2971 "log syslog", "Logging control\n" "Set syslog logging level\n")
2972{
2973 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2974 return CMD_SUCCESS;
2975}
2976
2977DEFUN(config_log_syslog_level,
2978 config_log_syslog_level_cmd,
2979 "log syslog " LOG_LEVELS,
2980 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
2981{
2982 int level;
2983
2984 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2985 return CMD_ERR_NO_MATCH;
2986 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
2987 return CMD_SUCCESS;
2988}
2989
2990DEFUN_DEPRECATED(config_log_syslog_facility,
2991 config_log_syslog_facility_cmd,
2992 "log syslog facility " LOG_FACILITIES,
2993 "Logging control\n"
2994 "Logging goes to syslog\n"
2995 "(Deprecated) Facility parameter for syslog messages\n"
2996 LOG_FACILITY_DESC)
2997{
2998 int facility;
2999
3000 if ((facility = facility_match(argv[0])) < 0)
3001 return CMD_ERR_NO_MATCH;
3002
3003 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3004 zlog_default->facility = facility;
3005 return CMD_SUCCESS;
3006}
3007
3008DEFUN(no_config_log_syslog,
3009 no_config_log_syslog_cmd,
3010 "no log syslog [LEVEL]",
3011 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3012{
3013 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3014 return CMD_SUCCESS;
3015}
3016
3017ALIAS(no_config_log_syslog,
3018 no_config_log_syslog_facility_cmd,
3019 "no log syslog facility " LOG_FACILITIES,
3020 NO_STR
3021 "Logging control\n"
3022 "Logging goes to syslog\n"
3023 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3024
3025 DEFUN(config_log_facility,
3026 config_log_facility_cmd,
3027 "log facility " LOG_FACILITIES,
3028 "Logging control\n"
3029 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3030{
3031 int facility;
3032
3033 if ((facility = facility_match(argv[0])) < 0)
3034 return CMD_ERR_NO_MATCH;
3035 zlog_default->facility = facility;
3036 return CMD_SUCCESS;
3037}
3038
3039DEFUN(no_config_log_facility,
3040 no_config_log_facility_cmd,
3041 "no log facility [FACILITY]",
3042 NO_STR
3043 "Logging control\n"
3044 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3045{
3046 zlog_default->facility = LOG_DAEMON;
3047 return CMD_SUCCESS;
3048}
3049
3050DEFUN_DEPRECATED(config_log_trap,
3051 config_log_trap_cmd,
3052 "log trap " LOG_LEVELS,
3053 "Logging control\n"
3054 "(Deprecated) Set logging level and default for all destinations\n"
3055 LOG_LEVEL_DESC)
3056{
3057 int new_level;
3058 int i;
3059
3060 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3061 return CMD_ERR_NO_MATCH;
3062
3063 zlog_default->default_lvl = new_level;
3064 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3065 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3066 zlog_default->maxlvl[i] = new_level;
3067 return CMD_SUCCESS;
3068}
3069
3070DEFUN_DEPRECATED(no_config_log_trap,
3071 no_config_log_trap_cmd,
3072 "no log trap [LEVEL]",
3073 NO_STR
3074 "Logging control\n"
3075 "Permit all logging information\n" "Logging level\n")
3076{
3077 zlog_default->default_lvl = LOG_DEBUG;
3078 return CMD_SUCCESS;
3079}
3080
3081DEFUN(config_log_record_priority,
3082 config_log_record_priority_cmd,
3083 "log record-priority",
3084 "Logging control\n"
3085 "Log the priority of the message within the message\n")
3086{
3087 zlog_default->record_priority = 1;
3088 return CMD_SUCCESS;
3089}
3090
3091DEFUN(no_config_log_record_priority,
3092 no_config_log_record_priority_cmd,
3093 "no log record-priority",
3094 NO_STR
3095 "Logging control\n"
3096 "Do not log the priority of the message within the message\n")
3097{
3098 zlog_default->record_priority = 0;
3099 return CMD_SUCCESS;
3100}
3101#endif
3102
3103DEFUN(banner_motd_file,
3104 banner_motd_file_cmd,
3105 "banner motd file [FILE]",
3106 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3107{
3108 if (host.motdfile)
3109 talloc_free(host.motdfile);
3110 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3111
3112 return CMD_SUCCESS;
3113}
3114
3115DEFUN(banner_motd_default,
3116 banner_motd_default_cmd,
3117 "banner motd default",
3118 "Set banner string\n" "Strings for motd\n" "Default string\n")
3119{
3120 host.motd = default_motd;
3121 return CMD_SUCCESS;
3122}
3123
3124DEFUN(no_banner_motd,
3125 no_banner_motd_cmd,
3126 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3127{
3128 host.motd = NULL;
3129 if (host.motdfile)
3130 talloc_free(host.motdfile);
3131 host.motdfile = NULL;
3132 return CMD_SUCCESS;
3133}
3134
3135/* Set config filename. Called from vty.c */
3136void host_config_set(const char *filename)
3137{
3138 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3139}
3140
3141void install_default(enum node_type node)
3142{
3143 install_element(node, &config_help_cmd);
3144 install_element(node, &config_list_cmd);
3145
3146 install_element(node, &config_write_terminal_cmd);
3147 install_element(node, &config_write_file_cmd);
3148 install_element(node, &config_write_memory_cmd);
3149 install_element(node, &config_write_cmd);
3150 install_element(node, &show_running_config_cmd);
3151}
3152
3153/* Initialize command interface. Install basic nodes and commands. */
3154void cmd_init(int terminal)
3155{
3156 /* Allocate initial top vector of commands. */
3157 cmdvec = vector_init(VECTOR_MIN_SIZE);
3158
3159 /* Default host value settings. */
3160 host.name = NULL;
3161 host.password = NULL;
3162 host.enable = NULL;
3163 host.logfile = NULL;
3164 host.config = NULL;
3165 host.lines = -1;
3166 host.motd = default_motd;
3167 host.motdfile = NULL;
3168
3169 /* Install top nodes. */
3170 install_node(&view_node, NULL);
3171 install_node(&enable_node, NULL);
3172 install_node(&auth_node, NULL);
3173 install_node(&auth_enable_node, NULL);
3174 install_node(&config_node, config_write_host);
3175
3176 /* Each node's basic commands. */
3177 install_element(VIEW_NODE, &show_version_cmd);
3178 if (terminal) {
3179 install_element(VIEW_NODE, &config_list_cmd);
3180 install_element(VIEW_NODE, &config_exit_cmd);
3181 install_element(VIEW_NODE, &config_help_cmd);
3182 install_element(VIEW_NODE, &config_enable_cmd);
3183 install_element(VIEW_NODE, &config_terminal_length_cmd);
3184 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3185 install_element(VIEW_NODE, &echo_cmd);
3186 }
3187
3188 if (terminal) {
3189 install_element(ENABLE_NODE, &config_exit_cmd);
3190 install_default(ENABLE_NODE);
3191 install_element(ENABLE_NODE, &config_disable_cmd);
3192 install_element(ENABLE_NODE, &config_terminal_cmd);
3193 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3194 }
3195 install_element (ENABLE_NODE, &show_startup_config_cmd);
3196 install_element(ENABLE_NODE, &show_version_cmd);
3197
3198 if (terminal) {
3199 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3200 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3201 install_element(ENABLE_NODE, &echo_cmd);
3202
3203 install_default(CONFIG_NODE);
3204 install_element(CONFIG_NODE, &config_exit_cmd);
3205 }
3206
3207 install_element(CONFIG_NODE, &hostname_cmd);
3208 install_element(CONFIG_NODE, &no_hostname_cmd);
3209
3210 if (terminal) {
3211 install_element(CONFIG_NODE, &password_cmd);
3212 install_element(CONFIG_NODE, &password_text_cmd);
3213 install_element(CONFIG_NODE, &enable_password_cmd);
3214 install_element(CONFIG_NODE, &enable_password_text_cmd);
3215 install_element(CONFIG_NODE, &no_enable_password_cmd);
3216
3217#ifdef VTY_CRYPT_PW
3218 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3219 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3220#endif
3221 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3222 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3223 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3224 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3225 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3226
3227 }
3228 srand(time(NULL));
3229}