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