[bgpd] AS4 bugfix by Chris Caputo <ccaputo@alt.net>
[jleu-quagga.git] / lib / command.c
blob4887f94fbddfb09b775cafc49b052efa310265c8
1 /*
2 $Id$
4 Command interpreter routine for virtual terminal [aka TeletYpe]
5 Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
7 This file is part of GNU Zebra.
9 GNU Zebra is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published
11 by the Free Software Foundation; either version 2, or (at your
12 option) any later version.
14 GNU Zebra is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Zebra; see the file COPYING. If not, write to the
21 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 #include <zebra.h>
27 #include "memory.h"
28 #include "log.h"
29 #include <lib/version.h>
30 #include "thread.h"
31 #include "vector.h"
32 #include "vty.h"
33 #include "command.h"
34 #include "workqueue.h"
36 /* Command vector which includes some level of command lists. Normally
37 each daemon maintains each own cmdvec. */
38 vector cmdvec = NULL;
40 /* Host information structure. */
41 struct host host;
43 /* Standard command node structures. */
44 struct cmd_node auth_node =
46 AUTH_NODE,
47 "Password: ",
50 struct cmd_node view_node =
52 VIEW_NODE,
53 "%s> ",
56 struct cmd_node restricted_node =
58 RESTRICTED_NODE,
59 "%s$ ",
62 struct cmd_node auth_enable_node =
64 AUTH_ENABLE_NODE,
65 "Password: ",
68 struct cmd_node enable_node =
70 ENABLE_NODE,
71 "%s# ",
74 struct cmd_node config_node =
76 CONFIG_NODE,
77 "%s(config)# ",
81 /* Default motd string. */
82 const char *default_motd =
83 "\r\n\
84 Hello, this is " QUAGGA_PROGNAME " (version " QUAGGA_VERSION ").\r\n\
85 " QUAGGA_COPYRIGHT "\r\n\
86 \r\n";
89 static struct facility_map {
90 int facility;
91 const char *name;
92 size_t match;
93 } syslog_facilities[] =
95 { LOG_KERN, "kern", 1 },
96 { LOG_USER, "user", 2 },
97 { LOG_MAIL, "mail", 1 },
98 { LOG_DAEMON, "daemon", 1 },
99 { LOG_AUTH, "auth", 1 },
100 { LOG_SYSLOG, "syslog", 1 },
101 { LOG_LPR, "lpr", 2 },
102 { LOG_NEWS, "news", 1 },
103 { LOG_UUCP, "uucp", 2 },
104 { LOG_CRON, "cron", 1 },
105 #ifdef LOG_FTP
106 { LOG_FTP, "ftp", 1 },
107 #endif
108 { LOG_LOCAL0, "local0", 6 },
109 { LOG_LOCAL1, "local1", 6 },
110 { LOG_LOCAL2, "local2", 6 },
111 { LOG_LOCAL3, "local3", 6 },
112 { LOG_LOCAL4, "local4", 6 },
113 { LOG_LOCAL5, "local5", 6 },
114 { LOG_LOCAL6, "local6", 6 },
115 { LOG_LOCAL7, "local7", 6 },
116 { 0, NULL, 0 },
119 static const char *
120 facility_name(int facility)
122 struct facility_map *fm;
124 for (fm = syslog_facilities; fm->name; fm++)
125 if (fm->facility == facility)
126 return fm->name;
127 return "";
130 static int
131 facility_match(const char *str)
133 struct facility_map *fm;
135 for (fm = syslog_facilities; fm->name; fm++)
136 if (!strncmp(str,fm->name,fm->match))
137 return fm->facility;
138 return -1;
141 static int
142 level_match(const char *s)
144 int level ;
146 for ( level = 0 ; zlog_priority [level] != NULL ; level ++ )
147 if (!strncmp (s, zlog_priority[level], 2))
148 return level;
149 return ZLOG_DISABLED;
152 /* This is called from main when a daemon is invoked with -v or --version. */
153 void
154 print_version (const char *progname)
156 printf ("%s version %s\n", progname, QUAGGA_VERSION);
157 printf ("%s\n", QUAGGA_COPYRIGHT);
161 /* Utility function to concatenate argv argument into a single string
162 with inserting ' ' character between each argument. */
163 char *
164 argv_concat (const char **argv, int argc, int shift)
166 int i;
167 size_t len;
168 char *str;
169 char *p;
171 len = 0;
172 for (i = shift; i < argc; i++)
173 len += strlen(argv[i])+1;
174 if (!len)
175 return NULL;
176 p = str = XMALLOC(MTYPE_TMP, len);
177 for (i = shift; i < argc; i++)
179 size_t arglen;
180 memcpy(p, argv[i], (arglen = strlen(argv[i])));
181 p += arglen;
182 *p++ = ' ';
184 *(p-1) = '\0';
185 return str;
188 /* Install top node of command vector. */
189 void
190 install_node (struct cmd_node *node,
191 int (*func) (struct vty *))
193 vector_set_index (cmdvec, node->node, node);
194 node->func = func;
195 node->cmd_vector = vector_init (VECTOR_MIN_SIZE);
198 /* Compare two command's string. Used in sort_node (). */
199 static int
200 cmp_node (const void *p, const void *q)
202 const struct cmd_element *a = *(struct cmd_element **)p;
203 const struct cmd_element *b = *(struct cmd_element **)q;
205 return strcmp (a->string, b->string);
208 static int
209 cmp_desc (const void *p, const void *q)
211 const struct desc *a = *(struct desc **)p;
212 const struct desc *b = *(struct desc **)q;
214 return strcmp (a->cmd, b->cmd);
217 /* Sort each node's command element according to command string. */
218 void
219 sort_node ()
221 unsigned int i, j;
222 struct cmd_node *cnode;
223 vector descvec;
224 struct cmd_element *cmd_element;
226 for (i = 0; i < vector_active (cmdvec); i++)
227 if ((cnode = vector_slot (cmdvec, i)) != NULL)
229 vector cmd_vector = cnode->cmd_vector;
230 qsort (cmd_vector->index, vector_active (cmd_vector),
231 sizeof (void *), cmp_node);
233 for (j = 0; j < vector_active (cmd_vector); j++)
234 if ((cmd_element = vector_slot (cmd_vector, j)) != NULL
235 && vector_active (cmd_element->strvec))
237 descvec = vector_slot (cmd_element->strvec,
238 vector_active (cmd_element->strvec) - 1);
239 qsort (descvec->index, vector_active (descvec),
240 sizeof (void *), cmp_desc);
245 /* Breaking up string into each command piece. I assume given
246 character is separated by a space character. Return value is a
247 vector which includes char ** data element. */
248 vector
249 cmd_make_strvec (const char *string)
251 const char *cp, *start;
252 char *token;
253 int strlen;
254 vector strvec;
256 if (string == NULL)
257 return NULL;
259 cp = string;
261 /* Skip white spaces. */
262 while (isspace ((int) *cp) && *cp != '\0')
263 cp++;
265 /* Return if there is only white spaces */
266 if (*cp == '\0')
267 return NULL;
269 if (*cp == '!' || *cp == '#')
270 return NULL;
272 /* Prepare return vector. */
273 strvec = vector_init (VECTOR_MIN_SIZE);
275 /* Copy each command piece and set into vector. */
276 while (1)
278 start = cp;
279 while (!(isspace ((int) *cp) || *cp == '\r' || *cp == '\n') &&
280 *cp != '\0')
281 cp++;
282 strlen = cp - start;
283 token = XMALLOC (MTYPE_STRVEC, strlen + 1);
284 memcpy (token, start, strlen);
285 *(token + strlen) = '\0';
286 vector_set (strvec, token);
288 while ((isspace ((int) *cp) || *cp == '\n' || *cp == '\r') &&
289 *cp != '\0')
290 cp++;
292 if (*cp == '\0')
293 return strvec;
297 /* Free allocated string vector. */
298 void
299 cmd_free_strvec (vector v)
301 unsigned int i;
302 char *cp;
304 if (!v)
305 return;
307 for (i = 0; i < vector_active (v); i++)
308 if ((cp = vector_slot (v, i)) != NULL)
309 XFREE (MTYPE_STRVEC, cp);
311 vector_free (v);
314 /* Fetch next description. Used in cmd_make_descvec(). */
315 static char *
316 cmd_desc_str (const char **string)
318 const char *cp, *start;
319 char *token;
320 int strlen;
322 cp = *string;
324 if (cp == NULL)
325 return NULL;
327 /* Skip white spaces. */
328 while (isspace ((int) *cp) && *cp != '\0')
329 cp++;
331 /* Return if there is only white spaces */
332 if (*cp == '\0')
333 return NULL;
335 start = cp;
337 while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
338 cp++;
340 strlen = cp - start;
341 token = XMALLOC (MTYPE_STRVEC, strlen + 1);
342 memcpy (token, start, strlen);
343 *(token + strlen) = '\0';
345 *string = cp;
347 return token;
350 /* New string vector. */
351 static vector
352 cmd_make_descvec (const char *string, const char *descstr)
354 int multiple = 0;
355 const char *sp;
356 char *token;
357 int len;
358 const char *cp;
359 const char *dp;
360 vector allvec;
361 vector strvec = NULL;
362 struct desc *desc;
364 cp = string;
365 dp = descstr;
367 if (cp == NULL)
368 return NULL;
370 allvec = vector_init (VECTOR_MIN_SIZE);
372 while (1)
374 while (isspace ((int) *cp) && *cp != '\0')
375 cp++;
377 if (*cp == '(')
379 multiple = 1;
380 cp++;
382 if (*cp == ')')
384 multiple = 0;
385 cp++;
387 if (*cp == '|')
389 if (! multiple)
391 fprintf (stderr, "Command parse error!: %s\n", string);
392 exit (1);
394 cp++;
397 while (isspace ((int) *cp) && *cp != '\0')
398 cp++;
400 if (*cp == '(')
402 multiple = 1;
403 cp++;
406 if (*cp == '\0')
407 return allvec;
409 sp = cp;
411 while (! (isspace ((int) *cp) || *cp == '\r' || *cp == '\n' || *cp == ')' || *cp == '|') && *cp != '\0')
412 cp++;
414 len = cp - sp;
416 token = XMALLOC (MTYPE_STRVEC, len + 1);
417 memcpy (token, sp, len);
418 *(token + len) = '\0';
420 desc = XCALLOC (MTYPE_DESC, sizeof (struct desc));
421 desc->cmd = token;
422 desc->str = cmd_desc_str (&dp);
424 if (multiple)
426 if (multiple == 1)
428 strvec = vector_init (VECTOR_MIN_SIZE);
429 vector_set (allvec, strvec);
431 multiple++;
433 else
435 strvec = vector_init (VECTOR_MIN_SIZE);
436 vector_set (allvec, strvec);
438 vector_set (strvec, desc);
442 /* Count mandantory string vector size. This is to determine inputed
443 command has enough command length. */
444 static int
445 cmd_cmdsize (vector strvec)
447 unsigned int i;
448 int size = 0;
449 vector descvec;
450 struct desc *desc;
452 for (i = 0; i < vector_active (strvec); i++)
453 if ((descvec = vector_slot (strvec, i)) != NULL)
455 if ((vector_active (descvec)) == 1
456 && (desc = vector_slot (descvec, 0)) != NULL)
458 if (desc->cmd == NULL || CMD_OPTION (desc->cmd))
459 return size;
460 else
461 size++;
463 else
464 size++;
466 return size;
469 /* Return prompt character of specified node. */
470 const char *
471 cmd_prompt (enum node_type node)
473 struct cmd_node *cnode;
475 cnode = vector_slot (cmdvec, node);
476 return cnode->prompt;
479 /* Install a command into a node. */
480 void
481 install_element (enum node_type ntype, struct cmd_element *cmd)
483 struct cmd_node *cnode;
485 /* cmd_init hasn't been called */
486 if (!cmdvec)
487 return;
489 cnode = vector_slot (cmdvec, ntype);
491 if (cnode == NULL)
493 fprintf (stderr, "Command node %d doesn't exist, please check it\n",
494 ntype);
495 exit (1);
498 vector_set (cnode->cmd_vector, cmd);
500 cmd->strvec = cmd_make_descvec (cmd->string, cmd->doc);
501 cmd->cmdsize = cmd_cmdsize (cmd->strvec);
504 static unsigned char itoa64[] =
505 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
507 static void
508 to64(char *s, long v, int n)
510 while (--n >= 0)
512 *s++ = itoa64[v&0x3f];
513 v >>= 6;
517 static char *
518 zencrypt (const char *passwd)
520 char salt[6];
521 struct timeval tv;
522 char *crypt (const char *, const char *);
524 gettimeofday(&tv,0);
526 to64(&salt[0], random(), 3);
527 to64(&salt[3], tv.tv_usec, 3);
528 salt[5] = '\0';
530 return crypt (passwd, salt);
533 /* This function write configuration of this host. */
534 static int
535 config_write_host (struct vty *vty)
537 if (host.name)
538 vty_out (vty, "hostname %s%s", host.name, VTY_NEWLINE);
540 if (host.encrypt)
542 if (host.password_encrypt)
543 vty_out (vty, "password 8 %s%s", host.password_encrypt, VTY_NEWLINE);
544 if (host.enable_encrypt)
545 vty_out (vty, "enable password 8 %s%s", host.enable_encrypt, VTY_NEWLINE);
547 else
549 if (host.password)
550 vty_out (vty, "password %s%s", host.password, VTY_NEWLINE);
551 if (host.enable)
552 vty_out (vty, "enable password %s%s", host.enable, VTY_NEWLINE);
555 if (zlog_default->default_lvl != LOG_DEBUG)
557 vty_out (vty, "! N.B. The 'log trap' command is deprecated.%s",
558 VTY_NEWLINE);
559 vty_out (vty, "log trap %s%s",
560 zlog_priority[zlog_default->default_lvl], VTY_NEWLINE);
563 if (host.logfile && (zlog_default->maxlvl[ZLOG_DEST_FILE] != ZLOG_DISABLED))
565 vty_out (vty, "log file %s", host.logfile);
566 if (zlog_default->maxlvl[ZLOG_DEST_FILE] != zlog_default->default_lvl)
567 vty_out (vty, " %s",
568 zlog_priority[zlog_default->maxlvl[ZLOG_DEST_FILE]]);
569 vty_out (vty, "%s", VTY_NEWLINE);
572 if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != ZLOG_DISABLED)
574 vty_out (vty, "log stdout");
575 if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != zlog_default->default_lvl)
576 vty_out (vty, " %s",
577 zlog_priority[zlog_default->maxlvl[ZLOG_DEST_STDOUT]]);
578 vty_out (vty, "%s", VTY_NEWLINE);
581 if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
582 vty_out(vty,"no log monitor%s",VTY_NEWLINE);
583 else if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] != zlog_default->default_lvl)
584 vty_out(vty,"log monitor %s%s",
585 zlog_priority[zlog_default->maxlvl[ZLOG_DEST_MONITOR]],VTY_NEWLINE);
587 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED)
589 vty_out (vty, "log syslog");
590 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != zlog_default->default_lvl)
591 vty_out (vty, " %s",
592 zlog_priority[zlog_default->maxlvl[ZLOG_DEST_SYSLOG]]);
593 vty_out (vty, "%s", VTY_NEWLINE);
596 if (zlog_default->facility != LOG_DAEMON)
597 vty_out (vty, "log facility %s%s",
598 facility_name(zlog_default->facility), VTY_NEWLINE);
600 if (zlog_default->record_priority == 1)
601 vty_out (vty, "log record-priority%s", VTY_NEWLINE);
603 if (zlog_default->timestamp_precision > 0)
604 vty_out (vty, "log timestamp precision %d%s",
605 zlog_default->timestamp_precision, VTY_NEWLINE);
607 if (host.advanced)
608 vty_out (vty, "service advanced-vty%s", VTY_NEWLINE);
610 if (host.encrypt)
611 vty_out (vty, "service password-encryption%s", VTY_NEWLINE);
613 if (host.lines >= 0)
614 vty_out (vty, "service terminal-length %d%s", host.lines,
615 VTY_NEWLINE);
617 if (host.motdfile)
618 vty_out (vty, "banner motd file %s%s", host.motdfile, VTY_NEWLINE);
619 else if (! host.motd)
620 vty_out (vty, "no banner motd%s", VTY_NEWLINE);
622 return 1;
625 /* Utility function for getting command vector. */
626 static vector
627 cmd_node_vector (vector v, enum node_type ntype)
629 struct cmd_node *cnode = vector_slot (v, ntype);
630 return cnode->cmd_vector;
633 #if 0
634 /* Filter command vector by symbol. This function is not actually used;
635 * should it be deleted? */
636 static int
637 cmd_filter_by_symbol (char *command, char *symbol)
639 int i, lim;
641 if (strcmp (symbol, "IPV4_ADDRESS") == 0)
643 i = 0;
644 lim = strlen (command);
645 while (i < lim)
647 if (! (isdigit ((int) command[i]) || command[i] == '.' || command[i] == '/'))
648 return 1;
649 i++;
651 return 0;
653 if (strcmp (symbol, "STRING") == 0)
655 i = 0;
656 lim = strlen (command);
657 while (i < lim)
659 if (! (isalpha ((int) command[i]) || command[i] == '_' || command[i] == '-'))
660 return 1;
661 i++;
663 return 0;
665 if (strcmp (symbol, "IFNAME") == 0)
667 i = 0;
668 lim = strlen (command);
669 while (i < lim)
671 if (! isalnum ((int) command[i]))
672 return 1;
673 i++;
675 return 0;
677 return 0;
679 #endif
681 /* Completion match types. */
682 enum match_type
684 no_match,
685 extend_match,
686 ipv4_prefix_match,
687 ipv4_match,
688 ipv6_prefix_match,
689 ipv6_match,
690 range_match,
691 vararg_match,
692 partly_match,
693 exact_match
696 static enum match_type
697 cmd_ipv4_match (const char *str)
699 const char *sp;
700 int dots = 0, nums = 0;
701 char buf[4];
703 if (str == NULL)
704 return partly_match;
706 for (;;)
708 memset (buf, 0, sizeof (buf));
709 sp = str;
710 while (*str != '\0')
712 if (*str == '.')
714 if (dots >= 3)
715 return no_match;
717 if (*(str + 1) == '.')
718 return no_match;
720 if (*(str + 1) == '\0')
721 return partly_match;
723 dots++;
724 break;
726 if (!isdigit ((int) *str))
727 return no_match;
729 str++;
732 if (str - sp > 3)
733 return no_match;
735 strncpy (buf, sp, str - sp);
736 if (atoi (buf) > 255)
737 return no_match;
739 nums++;
741 if (*str == '\0')
742 break;
744 str++;
747 if (nums < 4)
748 return partly_match;
750 return exact_match;
753 static enum match_type
754 cmd_ipv4_prefix_match (const char *str)
756 const char *sp;
757 int dots = 0;
758 char buf[4];
760 if (str == NULL)
761 return partly_match;
763 for (;;)
765 memset (buf, 0, sizeof (buf));
766 sp = str;
767 while (*str != '\0' && *str != '/')
769 if (*str == '.')
771 if (dots == 3)
772 return no_match;
774 if (*(str + 1) == '.' || *(str + 1) == '/')
775 return no_match;
777 if (*(str + 1) == '\0')
778 return partly_match;
780 dots++;
781 break;
784 if (!isdigit ((int) *str))
785 return no_match;
787 str++;
790 if (str - sp > 3)
791 return no_match;
793 strncpy (buf, sp, str - sp);
794 if (atoi (buf) > 255)
795 return no_match;
797 if (dots == 3)
799 if (*str == '/')
801 if (*(str + 1) == '\0')
802 return partly_match;
804 str++;
805 break;
807 else if (*str == '\0')
808 return partly_match;
811 if (*str == '\0')
812 return partly_match;
814 str++;
817 sp = str;
818 while (*str != '\0')
820 if (!isdigit ((int) *str))
821 return no_match;
823 str++;
826 if (atoi (sp) > 32)
827 return no_match;
829 return exact_match;
832 #define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
833 #define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
834 #define STATE_START 1
835 #define STATE_COLON 2
836 #define STATE_DOUBLE 3
837 #define STATE_ADDR 4
838 #define STATE_DOT 5
839 #define STATE_SLASH 6
840 #define STATE_MASK 7
842 #ifdef HAVE_IPV6
844 static enum match_type
845 cmd_ipv6_match (const char *str)
847 int state = STATE_START;
848 int colons = 0, nums = 0, double_colon = 0;
849 const char *sp = NULL;
850 struct sockaddr_in6 sin6_dummy;
851 int ret;
853 if (str == NULL)
854 return partly_match;
856 if (strspn (str, IPV6_ADDR_STR) != strlen (str))
857 return no_match;
859 /* use inet_pton that has a better support,
860 * for example inet_pton can support the automatic addresses:
861 * ::1.2.3.4
863 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
865 if (ret == 1)
866 return exact_match;
868 while (*str != '\0')
870 switch (state)
872 case STATE_START:
873 if (*str == ':')
875 if (*(str + 1) != ':' && *(str + 1) != '\0')
876 return no_match;
877 colons--;
878 state = STATE_COLON;
880 else
882 sp = str;
883 state = STATE_ADDR;
886 continue;
887 case STATE_COLON:
888 colons++;
889 if (*(str + 1) == ':')
890 state = STATE_DOUBLE;
891 else
893 sp = str + 1;
894 state = STATE_ADDR;
896 break;
897 case STATE_DOUBLE:
898 if (double_colon)
899 return no_match;
901 if (*(str + 1) == ':')
902 return no_match;
903 else
905 if (*(str + 1) != '\0')
906 colons++;
907 sp = str + 1;
908 state = STATE_ADDR;
911 double_colon++;
912 nums++;
913 break;
914 case STATE_ADDR:
915 if (*(str + 1) == ':' || *(str + 1) == '\0')
917 if (str - sp > 3)
918 return no_match;
920 nums++;
921 state = STATE_COLON;
923 if (*(str + 1) == '.')
924 state = STATE_DOT;
925 break;
926 case STATE_DOT:
927 state = STATE_ADDR;
928 break;
929 default:
930 break;
933 if (nums > 8)
934 return no_match;
936 if (colons > 7)
937 return no_match;
939 str++;
942 #if 0
943 if (nums < 11)
944 return partly_match;
945 #endif /* 0 */
947 return exact_match;
950 static enum match_type
951 cmd_ipv6_prefix_match (const char *str)
953 int state = STATE_START;
954 int colons = 0, nums = 0, double_colon = 0;
955 int mask;
956 const char *sp = NULL;
957 char *endptr = NULL;
959 if (str == NULL)
960 return partly_match;
962 if (strspn (str, IPV6_PREFIX_STR) != strlen (str))
963 return no_match;
965 while (*str != '\0' && state != STATE_MASK)
967 switch (state)
969 case STATE_START:
970 if (*str == ':')
972 if (*(str + 1) != ':' && *(str + 1) != '\0')
973 return no_match;
974 colons--;
975 state = STATE_COLON;
977 else
979 sp = str;
980 state = STATE_ADDR;
983 continue;
984 case STATE_COLON:
985 colons++;
986 if (*(str + 1) == '/')
987 return no_match;
988 else if (*(str + 1) == ':')
989 state = STATE_DOUBLE;
990 else
992 sp = str + 1;
993 state = STATE_ADDR;
995 break;
996 case STATE_DOUBLE:
997 if (double_colon)
998 return no_match;
1000 if (*(str + 1) == ':')
1001 return no_match;
1002 else
1004 if (*(str + 1) != '\0' && *(str + 1) != '/')
1005 colons++;
1006 sp = str + 1;
1008 if (*(str + 1) == '/')
1009 state = STATE_SLASH;
1010 else
1011 state = STATE_ADDR;
1014 double_colon++;
1015 nums += 1;
1016 break;
1017 case STATE_ADDR:
1018 if (*(str + 1) == ':' || *(str + 1) == '.'
1019 || *(str + 1) == '\0' || *(str + 1) == '/')
1021 if (str - sp > 3)
1022 return no_match;
1024 for (; sp <= str; sp++)
1025 if (*sp == '/')
1026 return no_match;
1028 nums++;
1030 if (*(str + 1) == ':')
1031 state = STATE_COLON;
1032 else if (*(str + 1) == '.')
1033 state = STATE_DOT;
1034 else if (*(str + 1) == '/')
1035 state = STATE_SLASH;
1037 break;
1038 case STATE_DOT:
1039 state = STATE_ADDR;
1040 break;
1041 case STATE_SLASH:
1042 if (*(str + 1) == '\0')
1043 return partly_match;
1045 state = STATE_MASK;
1046 break;
1047 default:
1048 break;
1051 if (nums > 11)
1052 return no_match;
1054 if (colons > 7)
1055 return no_match;
1057 str++;
1060 if (state < STATE_MASK)
1061 return partly_match;
1063 mask = strtol (str, &endptr, 10);
1064 if (*endptr != '\0')
1065 return no_match;
1067 if (mask < 0 || mask > 128)
1068 return no_match;
1070 /* I don't know why mask < 13 makes command match partly.
1071 Forgive me to make this comments. I Want to set static default route
1072 because of lack of function to originate default in ospf6d; sorry
1073 yasu
1074 if (mask < 13)
1075 return partly_match;
1078 return exact_match;
1081 #endif /* HAVE_IPV6 */
1083 #define DECIMAL_STRLEN_MAX 10
1085 static int
1086 cmd_range_match (const char *range, const char *str)
1088 char *p;
1089 char buf[DECIMAL_STRLEN_MAX + 1];
1090 char *endptr = NULL;
1091 unsigned long min, max, val;
1093 if (str == NULL)
1094 return 1;
1096 val = strtoul (str, &endptr, 10);
1097 if (*endptr != '\0')
1098 return 0;
1100 range++;
1101 p = strchr (range, '-');
1102 if (p == NULL)
1103 return 0;
1104 if (p - range > DECIMAL_STRLEN_MAX)
1105 return 0;
1106 strncpy (buf, range, p - range);
1107 buf[p - range] = '\0';
1108 min = strtoul (buf, &endptr, 10);
1109 if (*endptr != '\0')
1110 return 0;
1112 range = p + 1;
1113 p = strchr (range, '>');
1114 if (p == NULL)
1115 return 0;
1116 if (p - range > DECIMAL_STRLEN_MAX)
1117 return 0;
1118 strncpy (buf, range, p - range);
1119 buf[p - range] = '\0';
1120 max = strtoul (buf, &endptr, 10);
1121 if (*endptr != '\0')
1122 return 0;
1124 if (val < min || val > max)
1125 return 0;
1127 return 1;
1130 /* Make completion match and return match type flag. */
1131 static enum match_type
1132 cmd_filter_by_completion (char *command, vector v, unsigned int index)
1134 unsigned int i;
1135 const char *str;
1136 struct cmd_element *cmd_element;
1137 enum match_type match_type;
1138 vector descvec;
1139 struct desc *desc;
1141 match_type = no_match;
1143 /* If command and cmd_element string does not match set NULL to vector */
1144 for (i = 0; i < vector_active (v); i++)
1145 if ((cmd_element = vector_slot (v, i)) != NULL)
1147 if (index >= vector_active (cmd_element->strvec))
1148 vector_slot (v, i) = NULL;
1149 else
1151 unsigned int j;
1152 int matched = 0;
1154 descvec = vector_slot (cmd_element->strvec, index);
1156 for (j = 0; j < vector_active (descvec); j++)
1157 if ((desc = vector_slot (descvec, j)))
1159 str = desc->cmd;
1161 if (CMD_VARARG (str))
1163 if (match_type < vararg_match)
1164 match_type = vararg_match;
1165 matched++;
1167 else if (CMD_RANGE (str))
1169 if (cmd_range_match (str, command))
1171 if (match_type < range_match)
1172 match_type = range_match;
1174 matched++;
1177 #ifdef HAVE_IPV6
1178 else if (CMD_IPV6 (str))
1180 if (cmd_ipv6_match (command))
1182 if (match_type < ipv6_match)
1183 match_type = ipv6_match;
1185 matched++;
1188 else if (CMD_IPV6_PREFIX (str))
1190 if (cmd_ipv6_prefix_match (command))
1192 if (match_type < ipv6_prefix_match)
1193 match_type = ipv6_prefix_match;
1195 matched++;
1198 #endif /* HAVE_IPV6 */
1199 else if (CMD_IPV4 (str))
1201 if (cmd_ipv4_match (command))
1203 if (match_type < ipv4_match)
1204 match_type = ipv4_match;
1206 matched++;
1209 else if (CMD_IPV4_PREFIX (str))
1211 if (cmd_ipv4_prefix_match (command))
1213 if (match_type < ipv4_prefix_match)
1214 match_type = ipv4_prefix_match;
1215 matched++;
1218 else
1219 /* Check is this point's argument optional ? */
1220 if (CMD_OPTION (str) || CMD_VARIABLE (str))
1222 if (match_type < extend_match)
1223 match_type = extend_match;
1224 matched++;
1226 else if (strncmp (command, str, strlen (command)) == 0)
1228 if (strcmp (command, str) == 0)
1229 match_type = exact_match;
1230 else
1232 if (match_type < partly_match)
1233 match_type = partly_match;
1235 matched++;
1238 if (!matched)
1239 vector_slot (v, i) = NULL;
1242 return match_type;
1245 /* Filter vector by command character with index. */
1246 static enum match_type
1247 cmd_filter_by_string (char *command, vector v, unsigned int index)
1249 unsigned int i;
1250 const char *str;
1251 struct cmd_element *cmd_element;
1252 enum match_type match_type;
1253 vector descvec;
1254 struct desc *desc;
1256 match_type = no_match;
1258 /* If command and cmd_element string does not match set NULL to vector */
1259 for (i = 0; i < vector_active (v); i++)
1260 if ((cmd_element = vector_slot (v, i)) != NULL)
1262 /* If given index is bigger than max string vector of command,
1263 set NULL */
1264 if (index >= vector_active (cmd_element->strvec))
1265 vector_slot (v, i) = NULL;
1266 else
1268 unsigned int j;
1269 int matched = 0;
1271 descvec = vector_slot (cmd_element->strvec, index);
1273 for (j = 0; j < vector_active (descvec); j++)
1274 if ((desc = vector_slot (descvec, j)))
1276 str = desc->cmd;
1278 if (CMD_VARARG (str))
1280 if (match_type < vararg_match)
1281 match_type = vararg_match;
1282 matched++;
1284 else if (CMD_RANGE (str))
1286 if (cmd_range_match (str, command))
1288 if (match_type < range_match)
1289 match_type = range_match;
1290 matched++;
1293 #ifdef HAVE_IPV6
1294 else if (CMD_IPV6 (str))
1296 if (cmd_ipv6_match (command) == exact_match)
1298 if (match_type < ipv6_match)
1299 match_type = ipv6_match;
1300 matched++;
1303 else if (CMD_IPV6_PREFIX (str))
1305 if (cmd_ipv6_prefix_match (command) == exact_match)
1307 if (match_type < ipv6_prefix_match)
1308 match_type = ipv6_prefix_match;
1309 matched++;
1312 #endif /* HAVE_IPV6 */
1313 else if (CMD_IPV4 (str))
1315 if (cmd_ipv4_match (command) == exact_match)
1317 if (match_type < ipv4_match)
1318 match_type = ipv4_match;
1319 matched++;
1322 else if (CMD_IPV4_PREFIX (str))
1324 if (cmd_ipv4_prefix_match (command) == exact_match)
1326 if (match_type < ipv4_prefix_match)
1327 match_type = ipv4_prefix_match;
1328 matched++;
1331 else if (CMD_OPTION (str) || CMD_VARIABLE (str))
1333 if (match_type < extend_match)
1334 match_type = extend_match;
1335 matched++;
1337 else
1339 if (strcmp (command, str) == 0)
1341 match_type = exact_match;
1342 matched++;
1346 if (!matched)
1347 vector_slot (v, i) = NULL;
1350 return match_type;
1353 /* Check ambiguous match */
1354 static int
1355 is_cmd_ambiguous (char *command, vector v, int index, enum match_type type)
1357 unsigned int i;
1358 unsigned int j;
1359 const char *str = NULL;
1360 struct cmd_element *cmd_element;
1361 const char *matched = NULL;
1362 vector descvec;
1363 struct desc *desc;
1365 for (i = 0; i < vector_active (v); i++)
1366 if ((cmd_element = vector_slot (v, i)) != NULL)
1368 int match = 0;
1370 descvec = vector_slot (cmd_element->strvec, index);
1372 for (j = 0; j < vector_active (descvec); j++)
1373 if ((desc = vector_slot (descvec, j)))
1375 enum match_type ret;
1377 str = desc->cmd;
1379 switch (type)
1381 case exact_match:
1382 if (!(CMD_OPTION (str) || CMD_VARIABLE (str))
1383 && strcmp (command, str) == 0)
1384 match++;
1385 break;
1386 case partly_match:
1387 if (!(CMD_OPTION (str) || CMD_VARIABLE (str))
1388 && strncmp (command, str, strlen (command)) == 0)
1390 if (matched && strcmp (matched, str) != 0)
1391 return 1; /* There is ambiguous match. */
1392 else
1393 matched = str;
1394 match++;
1396 break;
1397 case range_match:
1398 if (cmd_range_match (str, command))
1400 if (matched && strcmp (matched, str) != 0)
1401 return 1;
1402 else
1403 matched = str;
1404 match++;
1406 break;
1407 #ifdef HAVE_IPV6
1408 case ipv6_match:
1409 if (CMD_IPV6 (str))
1410 match++;
1411 break;
1412 case ipv6_prefix_match:
1413 if ((ret = cmd_ipv6_prefix_match (command)) != no_match)
1415 if (ret == partly_match)
1416 return 2; /* There is incomplete match. */
1418 match++;
1420 break;
1421 #endif /* HAVE_IPV6 */
1422 case ipv4_match:
1423 if (CMD_IPV4 (str))
1424 match++;
1425 break;
1426 case ipv4_prefix_match:
1427 if ((ret = cmd_ipv4_prefix_match (command)) != no_match)
1429 if (ret == partly_match)
1430 return 2; /* There is incomplete match. */
1432 match++;
1434 break;
1435 case extend_match:
1436 if (CMD_OPTION (str) || CMD_VARIABLE (str))
1437 match++;
1438 break;
1439 case no_match:
1440 default:
1441 break;
1444 if (!match)
1445 vector_slot (v, i) = NULL;
1447 return 0;
1450 /* If src matches dst return dst string, otherwise return NULL */
1451 static const char *
1452 cmd_entry_function (const char *src, const char *dst)
1454 /* Skip variable arguments. */
1455 if (CMD_OPTION (dst) || CMD_VARIABLE (dst) || CMD_VARARG (dst) ||
1456 CMD_IPV4 (dst) || CMD_IPV4_PREFIX (dst) || CMD_RANGE (dst))
1457 return NULL;
1459 /* In case of 'command \t', given src is NULL string. */
1460 if (src == NULL)
1461 return dst;
1463 /* Matched with input string. */
1464 if (strncmp (src, dst, strlen (src)) == 0)
1465 return dst;
1467 return NULL;
1470 /* If src matches dst return dst string, otherwise return NULL */
1471 /* This version will return the dst string always if it is
1472 CMD_VARIABLE for '?' key processing */
1473 static const char *
1474 cmd_entry_function_desc (const char *src, const char *dst)
1476 if (CMD_VARARG (dst))
1477 return dst;
1479 if (CMD_RANGE (dst))
1481 if (cmd_range_match (dst, src))
1482 return dst;
1483 else
1484 return NULL;
1487 #ifdef HAVE_IPV6
1488 if (CMD_IPV6 (dst))
1490 if (cmd_ipv6_match (src))
1491 return dst;
1492 else
1493 return NULL;
1496 if (CMD_IPV6_PREFIX (dst))
1498 if (cmd_ipv6_prefix_match (src))
1499 return dst;
1500 else
1501 return NULL;
1503 #endif /* HAVE_IPV6 */
1505 if (CMD_IPV4 (dst))
1507 if (cmd_ipv4_match (src))
1508 return dst;
1509 else
1510 return NULL;
1513 if (CMD_IPV4_PREFIX (dst))
1515 if (cmd_ipv4_prefix_match (src))
1516 return dst;
1517 else
1518 return NULL;
1521 /* Optional or variable commands always match on '?' */
1522 if (CMD_OPTION (dst) || CMD_VARIABLE (dst))
1523 return dst;
1525 /* In case of 'command \t', given src is NULL string. */
1526 if (src == NULL)
1527 return dst;
1529 if (strncmp (src, dst, strlen (src)) == 0)
1530 return dst;
1531 else
1532 return NULL;
1535 /* Check same string element existence. If it isn't there return
1536 1. */
1537 static int
1538 cmd_unique_string (vector v, const char *str)
1540 unsigned int i;
1541 char *match;
1543 for (i = 0; i < vector_active (v); i++)
1544 if ((match = vector_slot (v, i)) != NULL)
1545 if (strcmp (match, str) == 0)
1546 return 0;
1547 return 1;
1550 /* Compare string to description vector. If there is same string
1551 return 1 else return 0. */
1552 static int
1553 desc_unique_string (vector v, const char *str)
1555 unsigned int i;
1556 struct desc *desc;
1558 for (i = 0; i < vector_active (v); i++)
1559 if ((desc = vector_slot (v, i)) != NULL)
1560 if (strcmp (desc->cmd, str) == 0)
1561 return 1;
1562 return 0;
1565 static int
1566 cmd_try_do_shortcut (enum node_type node, char* first_word) {
1567 if ( first_word != NULL &&
1568 node != AUTH_NODE &&
1569 node != VIEW_NODE &&
1570 node != AUTH_ENABLE_NODE &&
1571 node != ENABLE_NODE &&
1572 node != RESTRICTED_NODE &&
1573 0 == strcmp( "do", first_word ) )
1574 return 1;
1575 return 0;
1578 /* '?' describe command support. */
1579 static vector
1580 cmd_describe_command_real (vector vline, struct vty *vty, int *status)
1582 unsigned int i;
1583 vector cmd_vector;
1584 #define INIT_MATCHVEC_SIZE 10
1585 vector matchvec;
1586 struct cmd_element *cmd_element;
1587 unsigned int index;
1588 int ret;
1589 enum match_type match;
1590 char *command;
1591 static struct desc desc_cr = { "<cr>", "" };
1593 /* Set index. */
1594 if (vector_active (vline) == 0)
1596 *status = CMD_ERR_NO_MATCH;
1597 return NULL;
1599 else
1600 index = vector_active (vline) - 1;
1602 /* Make copy vector of current node's command vector. */
1603 cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
1605 /* Prepare match vector */
1606 matchvec = vector_init (INIT_MATCHVEC_SIZE);
1608 /* Filter commands. */
1609 /* Only words precedes current word will be checked in this loop. */
1610 for (i = 0; i < index; i++)
1611 if ((command = vector_slot (vline, i)))
1613 match = cmd_filter_by_completion (command, cmd_vector, i);
1615 if (match == vararg_match)
1617 struct cmd_element *cmd_element;
1618 vector descvec;
1619 unsigned int j, k;
1621 for (j = 0; j < vector_active (cmd_vector); j++)
1622 if ((cmd_element = vector_slot (cmd_vector, j)) != NULL
1623 && (vector_active (cmd_element->strvec)))
1625 descvec = vector_slot (cmd_element->strvec,
1626 vector_active (cmd_element->strvec) - 1);
1627 for (k = 0; k < vector_active (descvec); k++)
1629 struct desc *desc = vector_slot (descvec, k);
1630 vector_set (matchvec, desc);
1634 vector_set (matchvec, &desc_cr);
1635 vector_free (cmd_vector);
1637 return matchvec;
1640 if ((ret = is_cmd_ambiguous (command, cmd_vector, i, match)) == 1)
1642 vector_free (cmd_vector);
1643 vector_free (matchvec);
1644 *status = CMD_ERR_AMBIGUOUS;
1645 return NULL;
1647 else if (ret == 2)
1649 vector_free (cmd_vector);
1650 vector_free (matchvec);
1651 *status = CMD_ERR_NO_MATCH;
1652 return NULL;
1656 /* Prepare match vector */
1657 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1659 /* Make sure that cmd_vector is filtered based on current word */
1660 command = vector_slot (vline, index);
1661 if (command)
1662 match = cmd_filter_by_completion (command, cmd_vector, index);
1664 /* Make description vector. */
1665 for (i = 0; i < vector_active (cmd_vector); i++)
1666 if ((cmd_element = vector_slot (cmd_vector, i)) != NULL)
1668 const char *string = NULL;
1669 vector strvec = cmd_element->strvec;
1671 /* if command is NULL, index may be equal to vector_active */
1672 if (command && index >= vector_active (strvec))
1673 vector_slot (cmd_vector, i) = NULL;
1674 else
1676 /* Check if command is completed. */
1677 if (command == NULL && index == vector_active (strvec))
1679 string = "<cr>";
1680 if (!desc_unique_string (matchvec, string))
1681 vector_set (matchvec, &desc_cr);
1683 else
1685 unsigned int j;
1686 vector descvec = vector_slot (strvec, index);
1687 struct desc *desc;
1689 for (j = 0; j < vector_active (descvec); j++)
1690 if ((desc = vector_slot (descvec, j)))
1692 string = cmd_entry_function_desc (command, desc->cmd);
1693 if (string)
1695 /* Uniqueness check */
1696 if (!desc_unique_string (matchvec, string))
1697 vector_set (matchvec, desc);
1703 vector_free (cmd_vector);
1705 if (vector_slot (matchvec, 0) == NULL)
1707 vector_free (matchvec);
1708 *status = CMD_ERR_NO_MATCH;
1709 return NULL;
1712 *status = CMD_SUCCESS;
1713 return matchvec;
1716 vector
1717 cmd_describe_command (vector vline, struct vty *vty, int *status)
1719 vector ret;
1721 if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
1723 enum node_type onode;
1724 vector shifted_vline;
1725 unsigned int index;
1727 onode = vty->node;
1728 vty->node = ENABLE_NODE;
1729 /* We can try it on enable node, cos' the vty is authenticated */
1731 shifted_vline = vector_init (vector_count(vline));
1732 /* use memcpy? */
1733 for (index = 1; index < vector_active (vline); index++)
1735 vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
1738 ret = cmd_describe_command_real (shifted_vline, vty, status);
1740 vector_free(shifted_vline);
1741 vty->node = onode;
1742 return ret;
1746 return cmd_describe_command_real (vline, vty, status);
1750 /* Check LCD of matched command. */
1751 static int
1752 cmd_lcd (char **matched)
1754 int i;
1755 int j;
1756 int lcd = -1;
1757 char *s1, *s2;
1758 char c1, c2;
1760 if (matched[0] == NULL || matched[1] == NULL)
1761 return 0;
1763 for (i = 1; matched[i] != NULL; i++)
1765 s1 = matched[i - 1];
1766 s2 = matched[i];
1768 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1769 if (c1 != c2)
1770 break;
1772 if (lcd < 0)
1773 lcd = j;
1774 else
1776 if (lcd > j)
1777 lcd = j;
1780 return lcd;
1783 /* Command line completion support. */
1784 static char **
1785 cmd_complete_command_real (vector vline, struct vty *vty, int *status)
1787 unsigned int i;
1788 vector cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
1789 #define INIT_MATCHVEC_SIZE 10
1790 vector matchvec;
1791 struct cmd_element *cmd_element;
1792 unsigned int index;
1793 char **match_str;
1794 struct desc *desc;
1795 vector descvec;
1796 char *command;
1797 int lcd;
1799 if (vector_active (vline) == 0)
1801 vector_free (cmd_vector);
1802 *status = CMD_ERR_NO_MATCH;
1803 return NULL;
1805 else
1806 index = vector_active (vline) - 1;
1808 /* First, filter by preceeding command string */
1809 for (i = 0; i < index; i++)
1810 if ((command = vector_slot (vline, i)))
1812 enum match_type match;
1813 int ret;
1815 /* First try completion match, if there is exactly match return 1 */
1816 match = cmd_filter_by_completion (command, cmd_vector, i);
1818 /* If there is exact match then filter ambiguous match else check
1819 ambiguousness. */
1820 if ((ret = is_cmd_ambiguous (command, cmd_vector, i, match)) == 1)
1822 vector_free (cmd_vector);
1823 *status = CMD_ERR_AMBIGUOUS;
1824 return NULL;
1827 else if (ret == 2)
1829 vector_free (cmd_vector);
1830 *status = CMD_ERR_NO_MATCH;
1831 return NULL;
1836 /* Prepare match vector. */
1837 matchvec = vector_init (INIT_MATCHVEC_SIZE);
1839 /* Now we got into completion */
1840 for (i = 0; i < vector_active (cmd_vector); i++)
1841 if ((cmd_element = vector_slot (cmd_vector, i)))
1843 const char *string;
1844 vector strvec = cmd_element->strvec;
1846 /* Check field length */
1847 if (index >= vector_active (strvec))
1848 vector_slot (cmd_vector, i) = NULL;
1849 else
1851 unsigned int j;
1853 descvec = vector_slot (strvec, index);
1854 for (j = 0; j < vector_active (descvec); j++)
1855 if ((desc = vector_slot (descvec, j)))
1857 if ((string =
1858 cmd_entry_function (vector_slot (vline, index),
1859 desc->cmd)))
1860 if (cmd_unique_string (matchvec, string))
1861 vector_set (matchvec, XSTRDUP (MTYPE_TMP, string));
1866 /* We don't need cmd_vector any more. */
1867 vector_free (cmd_vector);
1869 /* No matched command */
1870 if (vector_slot (matchvec, 0) == NULL)
1872 vector_free (matchvec);
1874 /* In case of 'command \t' pattern. Do you need '?' command at
1875 the end of the line. */
1876 if (vector_slot (vline, index) == '\0')
1877 *status = CMD_ERR_NOTHING_TODO;
1878 else
1879 *status = CMD_ERR_NO_MATCH;
1880 return NULL;
1883 /* Only one matched */
1884 if (vector_slot (matchvec, 1) == NULL)
1886 match_str = (char **) matchvec->index;
1887 vector_only_wrapper_free (matchvec);
1888 *status = CMD_COMPLETE_FULL_MATCH;
1889 return match_str;
1891 /* Make it sure last element is NULL. */
1892 vector_set (matchvec, NULL);
1894 /* Check LCD of matched strings. */
1895 if (vector_slot (vline, index) != NULL)
1897 lcd = cmd_lcd ((char **) matchvec->index);
1899 if (lcd)
1901 int len = strlen (vector_slot (vline, index));
1903 if (len < lcd)
1905 char *lcdstr;
1907 lcdstr = XMALLOC (MTYPE_STRVEC, lcd + 1);
1908 memcpy (lcdstr, matchvec->index[0], lcd);
1909 lcdstr[lcd] = '\0';
1911 /* match_str = (char **) &lcdstr; */
1913 /* Free matchvec. */
1914 for (i = 0; i < vector_active (matchvec); i++)
1916 if (vector_slot (matchvec, i))
1917 XFREE (MTYPE_STRVEC, vector_slot (matchvec, i));
1919 vector_free (matchvec);
1921 /* Make new matchvec. */
1922 matchvec = vector_init (INIT_MATCHVEC_SIZE);
1923 vector_set (matchvec, lcdstr);
1924 match_str = (char **) matchvec->index;
1925 vector_only_wrapper_free (matchvec);
1927 *status = CMD_COMPLETE_MATCH;
1928 return match_str;
1933 match_str = (char **) matchvec->index;
1934 vector_only_wrapper_free (matchvec);
1935 *status = CMD_COMPLETE_LIST_MATCH;
1936 return match_str;
1939 char **
1940 cmd_complete_command (vector vline, struct vty *vty, int *status)
1942 char **ret;
1944 if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
1946 enum node_type onode;
1947 vector shifted_vline;
1948 unsigned int index;
1950 onode = vty->node;
1951 vty->node = ENABLE_NODE;
1952 /* We can try it on enable node, cos' the vty is authenticated */
1954 shifted_vline = vector_init (vector_count(vline));
1955 /* use memcpy? */
1956 for (index = 1; index < vector_active (vline); index++)
1958 vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
1961 ret = cmd_complete_command_real (shifted_vline, vty, status);
1963 vector_free(shifted_vline);
1964 vty->node = onode;
1965 return ret;
1969 return cmd_complete_command_real (vline, vty, status);
1972 /* return parent node */
1973 /* MUST eventually converge on CONFIG_NODE */
1974 enum node_type
1975 node_parent ( enum node_type node )
1977 enum node_type ret;
1979 assert (node > CONFIG_NODE);
1981 switch (node)
1983 case BGP_VPNV4_NODE:
1984 case BGP_IPV4_NODE:
1985 case BGP_IPV4M_NODE:
1986 case BGP_IPV6_NODE:
1987 case BGP_IPV6M_NODE:
1988 ret = BGP_NODE;
1989 break;
1990 case KEYCHAIN_KEY_NODE:
1991 ret = KEYCHAIN_NODE;
1992 break;
1993 default:
1994 ret = CONFIG_NODE;
1997 return ret;
2000 /* Execute command by argument vline vector. */
2001 static int
2002 cmd_execute_command_real (vector vline, struct vty *vty,
2003 struct cmd_element **cmd)
2005 unsigned int i;
2006 unsigned int index;
2007 vector cmd_vector;
2008 struct cmd_element *cmd_element;
2009 struct cmd_element *matched_element;
2010 unsigned int matched_count, incomplete_count;
2011 int argc;
2012 const char *argv[CMD_ARGC_MAX];
2013 enum match_type match = 0;
2014 int varflag;
2015 char *command;
2017 /* Make copy of command elements. */
2018 cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
2020 for (index = 0; index < vector_active (vline); index++)
2021 if ((command = vector_slot (vline, index)))
2023 int ret;
2025 match = cmd_filter_by_completion (command, cmd_vector, index);
2027 if (match == vararg_match)
2028 break;
2030 ret = is_cmd_ambiguous (command, cmd_vector, index, match);
2032 if (ret == 1)
2034 vector_free (cmd_vector);
2035 return CMD_ERR_AMBIGUOUS;
2037 else if (ret == 2)
2039 vector_free (cmd_vector);
2040 return CMD_ERR_NO_MATCH;
2044 /* Check matched count. */
2045 matched_element = NULL;
2046 matched_count = 0;
2047 incomplete_count = 0;
2049 for (i = 0; i < vector_active (cmd_vector); i++)
2050 if ((cmd_element = vector_slot (cmd_vector, i)))
2052 if (match == vararg_match || index >= cmd_element->cmdsize)
2054 matched_element = cmd_element;
2055 #if 0
2056 printf ("DEBUG: %s\n", cmd_element->string);
2057 #endif
2058 matched_count++;
2060 else
2062 incomplete_count++;
2066 /* Finish of using cmd_vector. */
2067 vector_free (cmd_vector);
2069 /* To execute command, matched_count must be 1. */
2070 if (matched_count == 0)
2072 if (incomplete_count)
2073 return CMD_ERR_INCOMPLETE;
2074 else
2075 return CMD_ERR_NO_MATCH;
2078 if (matched_count > 1)
2079 return CMD_ERR_AMBIGUOUS;
2081 /* Argument treatment */
2082 varflag = 0;
2083 argc = 0;
2085 for (i = 0; i < vector_active (vline); i++)
2087 if (varflag)
2088 argv[argc++] = vector_slot (vline, i);
2089 else
2091 vector descvec = vector_slot (matched_element->strvec, i);
2093 if (vector_active (descvec) == 1)
2095 struct desc *desc = vector_slot (descvec, 0);
2097 if (CMD_VARARG (desc->cmd))
2098 varflag = 1;
2100 if (varflag || CMD_VARIABLE (desc->cmd) || CMD_OPTION (desc->cmd))
2101 argv[argc++] = vector_slot (vline, i);
2103 else
2104 argv[argc++] = vector_slot (vline, i);
2107 if (argc >= CMD_ARGC_MAX)
2108 return CMD_ERR_EXEED_ARGC_MAX;
2111 /* For vtysh execution. */
2112 if (cmd)
2113 *cmd = matched_element;
2115 if (matched_element->daemon)
2116 return CMD_SUCCESS_DAEMON;
2118 /* Execute matched command. */
2119 return (*matched_element->func) (matched_element, vty, argc, argv);
2123 cmd_execute_command (vector vline, struct vty *vty, struct cmd_element **cmd,
2124 int vtysh) {
2125 int ret, saved_ret, tried = 0;
2126 enum node_type onode, try_node;
2128 onode = try_node = vty->node;
2130 if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
2132 vector shifted_vline;
2133 unsigned int index;
2135 vty->node = ENABLE_NODE;
2136 /* We can try it on enable node, cos' the vty is authenticated */
2138 shifted_vline = vector_init (vector_count(vline));
2139 /* use memcpy? */
2140 for (index = 1; index < vector_active (vline); index++)
2142 vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
2145 ret = cmd_execute_command_real (shifted_vline, vty, cmd);
2147 vector_free(shifted_vline);
2148 vty->node = onode;
2149 return ret;
2153 saved_ret = ret = cmd_execute_command_real (vline, vty, cmd);
2155 if (vtysh)
2156 return saved_ret;
2158 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
2159 while ( ret != CMD_SUCCESS && ret != CMD_WARNING
2160 && vty->node > CONFIG_NODE )
2162 try_node = node_parent(try_node);
2163 vty->node = try_node;
2164 ret = cmd_execute_command_real (vline, vty, cmd);
2165 tried = 1;
2166 if (ret == CMD_SUCCESS || ret == CMD_WARNING)
2168 /* succesfull command, leave the node as is */
2169 return ret;
2172 /* no command succeeded, reset the vty to the original node and
2173 return the error for this node */
2174 if ( tried )
2175 vty->node = onode;
2176 return saved_ret;
2179 /* Execute command by argument readline. */
2181 cmd_execute_command_strict (vector vline, struct vty *vty,
2182 struct cmd_element **cmd)
2184 unsigned int i;
2185 unsigned int index;
2186 vector cmd_vector;
2187 struct cmd_element *cmd_element;
2188 struct cmd_element *matched_element;
2189 unsigned int matched_count, incomplete_count;
2190 int argc;
2191 const char *argv[CMD_ARGC_MAX];
2192 int varflag;
2193 enum match_type match = 0;
2194 char *command;
2196 /* Make copy of command element */
2197 cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
2199 for (index = 0; index < vector_active (vline); index++)
2200 if ((command = vector_slot (vline, index)))
2202 int ret;
2204 match = cmd_filter_by_string (vector_slot (vline, index),
2205 cmd_vector, index);
2207 /* If command meets '.VARARG' then finish matching. */
2208 if (match == vararg_match)
2209 break;
2211 ret = is_cmd_ambiguous (command, cmd_vector, index, match);
2212 if (ret == 1)
2214 vector_free (cmd_vector);
2215 return CMD_ERR_AMBIGUOUS;
2217 if (ret == 2)
2219 vector_free (cmd_vector);
2220 return CMD_ERR_NO_MATCH;
2224 /* Check matched count. */
2225 matched_element = NULL;
2226 matched_count = 0;
2227 incomplete_count = 0;
2228 for (i = 0; i < vector_active (cmd_vector); i++)
2229 if (vector_slot (cmd_vector, i) != NULL)
2231 cmd_element = vector_slot (cmd_vector, i);
2233 if (match == vararg_match || index >= cmd_element->cmdsize)
2235 matched_element = cmd_element;
2236 matched_count++;
2238 else
2239 incomplete_count++;
2242 /* Finish of using cmd_vector. */
2243 vector_free (cmd_vector);
2245 /* To execute command, matched_count must be 1. */
2246 if (matched_count == 0)
2248 if (incomplete_count)
2249 return CMD_ERR_INCOMPLETE;
2250 else
2251 return CMD_ERR_NO_MATCH;
2254 if (matched_count > 1)
2255 return CMD_ERR_AMBIGUOUS;
2257 /* Argument treatment */
2258 varflag = 0;
2259 argc = 0;
2261 for (i = 0; i < vector_active (vline); i++)
2263 if (varflag)
2264 argv[argc++] = vector_slot (vline, i);
2265 else
2267 vector descvec = vector_slot (matched_element->strvec, i);
2269 if (vector_active (descvec) == 1)
2271 struct desc *desc = vector_slot (descvec, 0);
2273 if (CMD_VARARG (desc->cmd))
2274 varflag = 1;
2276 if (varflag || CMD_VARIABLE (desc->cmd) || CMD_OPTION (desc->cmd))
2277 argv[argc++] = vector_slot (vline, i);
2279 else
2280 argv[argc++] = vector_slot (vline, i);
2283 if (argc >= CMD_ARGC_MAX)
2284 return CMD_ERR_EXEED_ARGC_MAX;
2287 /* For vtysh execution. */
2288 if (cmd)
2289 *cmd = matched_element;
2291 if (matched_element->daemon)
2292 return CMD_SUCCESS_DAEMON;
2294 /* Now execute matched command */
2295 return (*matched_element->func) (matched_element, vty, argc, argv);
2298 /* Configration make from file. */
2300 config_from_file (struct vty *vty, FILE *fp)
2302 int ret;
2303 vector vline;
2305 while (fgets (vty->buf, VTY_BUFSIZ, fp))
2307 vline = cmd_make_strvec (vty->buf);
2309 /* In case of comment line */
2310 if (vline == NULL)
2311 continue;
2312 /* Execute configuration command : this is strict match */
2313 ret = cmd_execute_command_strict (vline, vty, NULL);
2315 /* Try again with setting node to CONFIG_NODE */
2316 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2317 && ret != CMD_ERR_NOTHING_TODO && vty->node != CONFIG_NODE)
2319 vty->node = node_parent(vty->node);
2320 ret = cmd_execute_command_strict (vline, vty, NULL);
2323 cmd_free_strvec (vline);
2325 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2326 && ret != CMD_ERR_NOTHING_TODO)
2327 return ret;
2329 return CMD_SUCCESS;
2332 /* Configration from terminal */
2333 DEFUN (config_terminal,
2334 config_terminal_cmd,
2335 "configure terminal",
2336 "Configuration from vty interface\n"
2337 "Configuration terminal\n")
2339 if (vty_config_lock (vty))
2340 vty->node = CONFIG_NODE;
2341 else
2343 vty_out (vty, "VTY configuration is locked by other VTY%s", VTY_NEWLINE);
2344 return CMD_WARNING;
2346 return CMD_SUCCESS;
2349 /* Enable command */
2350 DEFUN (enable,
2351 config_enable_cmd,
2352 "enable",
2353 "Turn on privileged mode command\n")
2355 /* If enable password is NULL, change to ENABLE_NODE */
2356 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2357 vty->type == VTY_SHELL_SERV)
2358 vty->node = ENABLE_NODE;
2359 else
2360 vty->node = AUTH_ENABLE_NODE;
2362 return CMD_SUCCESS;
2365 /* Disable command */
2366 DEFUN (disable,
2367 config_disable_cmd,
2368 "disable",
2369 "Turn off privileged mode command\n")
2371 if (vty->node == ENABLE_NODE)
2372 vty->node = VIEW_NODE;
2373 return CMD_SUCCESS;
2376 /* Down vty node level. */
2377 DEFUN (config_exit,
2378 config_exit_cmd,
2379 "exit",
2380 "Exit current mode and down to previous mode\n")
2382 switch (vty->node)
2384 case VIEW_NODE:
2385 case ENABLE_NODE:
2386 case RESTRICTED_NODE:
2387 if (vty_shell (vty))
2388 exit (0);
2389 else
2390 vty->status = VTY_CLOSE;
2391 break;
2392 case CONFIG_NODE:
2393 vty->node = ENABLE_NODE;
2394 vty_config_unlock (vty);
2395 break;
2396 case INTERFACE_NODE:
2397 case ZEBRA_NODE:
2398 case BGP_NODE:
2399 case RIP_NODE:
2400 case RIPNG_NODE:
2401 case OSPF_NODE:
2402 case OSPF6_NODE:
2403 case ISIS_NODE:
2404 case KEYCHAIN_NODE:
2405 case MASC_NODE:
2406 case RMAP_NODE:
2407 case VTY_NODE:
2408 vty->node = CONFIG_NODE;
2409 break;
2410 case BGP_VPNV4_NODE:
2411 case BGP_IPV4_NODE:
2412 case BGP_IPV4M_NODE:
2413 case BGP_IPV6_NODE:
2414 case BGP_IPV6M_NODE:
2415 vty->node = BGP_NODE;
2416 break;
2417 case KEYCHAIN_KEY_NODE:
2418 vty->node = KEYCHAIN_NODE;
2419 break;
2420 default:
2421 break;
2423 return CMD_SUCCESS;
2426 /* quit is alias of exit. */
2427 ALIAS (config_exit,
2428 config_quit_cmd,
2429 "quit",
2430 "Exit current mode and down to previous mode\n")
2432 /* End of configuration. */
2433 DEFUN (config_end,
2434 config_end_cmd,
2435 "end",
2436 "End current mode and change to enable mode.")
2438 switch (vty->node)
2440 case VIEW_NODE:
2441 case ENABLE_NODE:
2442 case RESTRICTED_NODE:
2443 /* Nothing to do. */
2444 break;
2445 case CONFIG_NODE:
2446 case INTERFACE_NODE:
2447 case ZEBRA_NODE:
2448 case RIP_NODE:
2449 case RIPNG_NODE:
2450 case BGP_NODE:
2451 case BGP_VPNV4_NODE:
2452 case BGP_IPV4_NODE:
2453 case BGP_IPV4M_NODE:
2454 case BGP_IPV6_NODE:
2455 case BGP_IPV6M_NODE:
2456 case RMAP_NODE:
2457 case OSPF_NODE:
2458 case OSPF6_NODE:
2459 case ISIS_NODE:
2460 case KEYCHAIN_NODE:
2461 case KEYCHAIN_KEY_NODE:
2462 case MASC_NODE:
2463 case VTY_NODE:
2464 vty_config_unlock (vty);
2465 vty->node = ENABLE_NODE;
2466 break;
2467 default:
2468 break;
2470 return CMD_SUCCESS;
2473 /* Show version. */
2474 DEFUN (show_version,
2475 show_version_cmd,
2476 "show version",
2477 SHOW_STR
2478 "Displays zebra version\n")
2480 vty_out (vty, "Quagga %s (%s).%s", QUAGGA_VERSION, host.name?host.name:"",
2481 VTY_NEWLINE);
2482 vty_out (vty, "%s%s", QUAGGA_COPYRIGHT, VTY_NEWLINE);
2484 return CMD_SUCCESS;
2487 /* Help display function for all node. */
2488 DEFUN (config_help,
2489 config_help_cmd,
2490 "help",
2491 "Description of the interactive help system\n")
2493 vty_out (vty,
2494 "Quagga VTY provides advanced help feature. When you need help,%s\
2495 anytime at the command line please press '?'.%s\
2497 If nothing matches, the help list will be empty and you must backup%s\
2498 until entering a '?' shows the available options.%s\
2499 Two styles of help are provided:%s\
2500 1. Full help is available when you are ready to enter a%s\
2501 command argument (e.g. 'show ?') and describes each possible%s\
2502 argument.%s\
2503 2. Partial help is provided when an abbreviated argument is entered%s\
2504 and you want to know what arguments match the input%s\
2505 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2506 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2507 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2508 return CMD_SUCCESS;
2511 /* Help display function for all node. */
2512 DEFUN (config_list,
2513 config_list_cmd,
2514 "list",
2515 "Print command list\n")
2517 unsigned int i;
2518 struct cmd_node *cnode = vector_slot (cmdvec, vty->node);
2519 struct cmd_element *cmd;
2521 for (i = 0; i < vector_active (cnode->cmd_vector); i++)
2522 if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL
2523 && !(cmd->attr == CMD_ATTR_DEPRECATED
2524 || cmd->attr == CMD_ATTR_HIDDEN))
2525 vty_out (vty, " %s%s", cmd->string,
2526 VTY_NEWLINE);
2527 return CMD_SUCCESS;
2530 /* Write current configuration into file. */
2531 DEFUN (config_write_file,
2532 config_write_file_cmd,
2533 "write file",
2534 "Write running configuration to memory, network, or terminal\n"
2535 "Write to configuration file\n")
2537 unsigned int i;
2538 int fd;
2539 struct cmd_node *node;
2540 char *config_file;
2541 char *config_file_tmp = NULL;
2542 char *config_file_sav = NULL;
2543 int ret = CMD_WARNING;
2544 struct vty *file_vty;
2546 /* Check and see if we are operating under vtysh configuration */
2547 if (host.config == NULL)
2549 vty_out (vty, "Can't save to configuration file, using vtysh.%s",
2550 VTY_NEWLINE);
2551 return CMD_WARNING;
2554 /* Get filename. */
2555 config_file = host.config;
2557 config_file_sav =
2558 XMALLOC (MTYPE_TMP, strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1);
2559 strcpy (config_file_sav, config_file);
2560 strcat (config_file_sav, CONF_BACKUP_EXT);
2563 config_file_tmp = XMALLOC (MTYPE_TMP, strlen (config_file) + 8);
2564 sprintf (config_file_tmp, "%s.XXXXXX", config_file);
2566 /* Open file to configuration write. */
2567 fd = mkstemp (config_file_tmp);
2568 if (fd < 0)
2570 vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp,
2571 VTY_NEWLINE);
2572 goto finished;
2575 /* Make vty for configuration file. */
2576 file_vty = vty_new ();
2577 file_vty->fd = fd;
2578 file_vty->type = VTY_FILE;
2580 /* Config file header print. */
2581 vty_out (file_vty, "!\n! Zebra configuration saved from vty\n! ");
2582 vty_time_print (file_vty, 1);
2583 vty_out (file_vty, "!\n");
2585 for (i = 0; i < vector_active (cmdvec); i++)
2586 if ((node = vector_slot (cmdvec, i)) && node->func)
2588 if ((*node->func) (file_vty))
2589 vty_out (file_vty, "!\n");
2591 vty_close (file_vty);
2593 if (unlink (config_file_sav) != 0)
2594 if (errno != ENOENT)
2596 vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav,
2597 VTY_NEWLINE);
2598 goto finished;
2600 if (link (config_file, config_file_sav) != 0)
2602 vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav,
2603 VTY_NEWLINE);
2604 goto finished;
2606 sync ();
2607 if (unlink (config_file) != 0)
2609 vty_out (vty, "Can't unlink configuration file %s.%s", config_file,
2610 VTY_NEWLINE);
2611 goto finished;
2613 if (link (config_file_tmp, config_file) != 0)
2615 vty_out (vty, "Can't save configuration file %s.%s", config_file,
2616 VTY_NEWLINE);
2617 goto finished;
2619 sync ();
2621 if (chmod (config_file, CONFIGFILE_MASK) != 0)
2623 vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s",
2624 config_file, safe_strerror(errno), errno, VTY_NEWLINE);
2625 goto finished;
2628 vty_out (vty, "Configuration saved to %s%s", config_file,
2629 VTY_NEWLINE);
2630 ret = CMD_SUCCESS;
2632 finished:
2633 unlink (config_file_tmp);
2634 XFREE (MTYPE_TMP, config_file_tmp);
2635 XFREE (MTYPE_TMP, config_file_sav);
2636 return ret;
2639 ALIAS (config_write_file,
2640 config_write_cmd,
2641 "write",
2642 "Write running configuration to memory, network, or terminal\n")
2644 ALIAS (config_write_file,
2645 config_write_memory_cmd,
2646 "write memory",
2647 "Write running configuration to memory, network, or terminal\n"
2648 "Write configuration to the file (same as write file)\n")
2650 ALIAS (config_write_file,
2651 copy_runningconfig_startupconfig_cmd,
2652 "copy running-config startup-config",
2653 "Copy configuration\n"
2654 "Copy running config to... \n"
2655 "Copy running config to startup config (same as write file)\n")
2657 /* Write current configuration into the terminal. */
2658 DEFUN (config_write_terminal,
2659 config_write_terminal_cmd,
2660 "write terminal",
2661 "Write running configuration to memory, network, or terminal\n"
2662 "Write to terminal\n")
2664 unsigned int i;
2665 struct cmd_node *node;
2667 if (vty->type == VTY_SHELL_SERV)
2669 for (i = 0; i < vector_active (cmdvec); i++)
2670 if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh)
2672 if ((*node->func) (vty))
2673 vty_out (vty, "!%s", VTY_NEWLINE);
2676 else
2678 vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2679 VTY_NEWLINE);
2680 vty_out (vty, "!%s", VTY_NEWLINE);
2682 for (i = 0; i < vector_active (cmdvec); i++)
2683 if ((node = vector_slot (cmdvec, i)) && node->func)
2685 if ((*node->func) (vty))
2686 vty_out (vty, "!%s", VTY_NEWLINE);
2688 vty_out (vty, "end%s",VTY_NEWLINE);
2690 return CMD_SUCCESS;
2693 /* Write current configuration into the terminal. */
2694 ALIAS (config_write_terminal,
2695 show_running_config_cmd,
2696 "show running-config",
2697 SHOW_STR
2698 "running configuration\n")
2700 /* Write startup configuration into the terminal. */
2701 DEFUN (show_startup_config,
2702 show_startup_config_cmd,
2703 "show startup-config",
2704 SHOW_STR
2705 "Contentes of startup configuration\n")
2707 char buf[BUFSIZ];
2708 FILE *confp;
2710 confp = fopen (host.config, "r");
2711 if (confp == NULL)
2713 vty_out (vty, "Can't open configuration file [%s]%s",
2714 host.config, VTY_NEWLINE);
2715 return CMD_WARNING;
2718 while (fgets (buf, BUFSIZ, confp))
2720 char *cp = buf;
2722 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2723 cp++;
2724 *cp = '\0';
2726 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
2729 fclose (confp);
2731 return CMD_SUCCESS;
2734 /* Hostname configuration */
2735 DEFUN (config_hostname,
2736 hostname_cmd,
2737 "hostname WORD",
2738 "Set system's network name\n"
2739 "This system's network name\n")
2741 if (!isalpha((int) *argv[0]))
2743 vty_out (vty, "Please specify string starting with alphabet%s", VTY_NEWLINE);
2744 return CMD_WARNING;
2747 if (host.name)
2748 XFREE (MTYPE_HOST, host.name);
2750 host.name = XSTRDUP (MTYPE_HOST, argv[0]);
2751 return CMD_SUCCESS;
2754 DEFUN (config_no_hostname,
2755 no_hostname_cmd,
2756 "no hostname [HOSTNAME]",
2757 NO_STR
2758 "Reset system's network name\n"
2759 "Host name of this router\n")
2761 if (host.name)
2762 XFREE (MTYPE_HOST, host.name);
2763 host.name = NULL;
2764 return CMD_SUCCESS;
2767 /* VTY interface password set. */
2768 DEFUN (config_password, password_cmd,
2769 "password (8|) WORD",
2770 "Assign the terminal connection password\n"
2771 "Specifies a HIDDEN password will follow\n"
2772 "dummy string \n"
2773 "The HIDDEN line password string\n")
2775 /* Argument check. */
2776 if (argc == 0)
2778 vty_out (vty, "Please specify password.%s", VTY_NEWLINE);
2779 return CMD_WARNING;
2782 if (argc == 2)
2784 if (*argv[0] == '8')
2786 if (host.password)
2787 XFREE (MTYPE_HOST, host.password);
2788 host.password = NULL;
2789 if (host.password_encrypt)
2790 XFREE (MTYPE_HOST, host.password_encrypt);
2791 host.password_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
2792 return CMD_SUCCESS;
2794 else
2796 vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE);
2797 return CMD_WARNING;
2801 if (!isalnum ((int) *argv[0]))
2803 vty_out (vty,
2804 "Please specify string starting with alphanumeric%s", VTY_NEWLINE);
2805 return CMD_WARNING;
2808 if (host.password)
2809 XFREE (MTYPE_HOST, host.password);
2810 host.password = NULL;
2812 if (host.encrypt)
2814 if (host.password_encrypt)
2815 XFREE (MTYPE_HOST, host.password_encrypt);
2816 host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
2818 else
2819 host.password = XSTRDUP (MTYPE_HOST, argv[0]);
2821 return CMD_SUCCESS;
2824 ALIAS (config_password, password_text_cmd,
2825 "password LINE",
2826 "Assign the terminal connection password\n"
2827 "The UNENCRYPTED (cleartext) line password\n")
2829 /* VTY enable password set. */
2830 DEFUN (config_enable_password, enable_password_cmd,
2831 "enable password (8|) WORD",
2832 "Modify enable password parameters\n"
2833 "Assign the privileged level password\n"
2834 "Specifies a HIDDEN password will follow\n"
2835 "dummy string \n"
2836 "The HIDDEN 'enable' password string\n")
2838 /* Argument check. */
2839 if (argc == 0)
2841 vty_out (vty, "Please specify password.%s", VTY_NEWLINE);
2842 return CMD_WARNING;
2845 /* Crypt type is specified. */
2846 if (argc == 2)
2848 if (*argv[0] == '8')
2850 if (host.enable)
2851 XFREE (MTYPE_HOST, host.enable);
2852 host.enable = NULL;
2854 if (host.enable_encrypt)
2855 XFREE (MTYPE_HOST, host.enable_encrypt);
2856 host.enable_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
2858 return CMD_SUCCESS;
2860 else
2862 vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE);
2863 return CMD_WARNING;
2867 if (!isalnum ((int) *argv[0]))
2869 vty_out (vty,
2870 "Please specify string starting with alphanumeric%s", VTY_NEWLINE);
2871 return CMD_WARNING;
2874 if (host.enable)
2875 XFREE (MTYPE_HOST, host.enable);
2876 host.enable = NULL;
2878 /* Plain password input. */
2879 if (host.encrypt)
2881 if (host.enable_encrypt)
2882 XFREE (MTYPE_HOST, host.enable_encrypt);
2883 host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
2885 else
2886 host.enable = XSTRDUP (MTYPE_HOST, argv[0]);
2888 return CMD_SUCCESS;
2891 ALIAS (config_enable_password,
2892 enable_password_text_cmd,
2893 "enable password LINE",
2894 "Modify enable password parameters\n"
2895 "Assign the privileged level password\n"
2896 "The UNENCRYPTED (cleartext) 'enable' password\n")
2898 /* VTY enable password delete. */
2899 DEFUN (no_config_enable_password, no_enable_password_cmd,
2900 "no enable password",
2901 NO_STR
2902 "Modify enable password parameters\n"
2903 "Assign the privileged level password\n")
2905 if (host.enable)
2906 XFREE (MTYPE_HOST, host.enable);
2907 host.enable = NULL;
2909 if (host.enable_encrypt)
2910 XFREE (MTYPE_HOST, host.enable_encrypt);
2911 host.enable_encrypt = NULL;
2913 return CMD_SUCCESS;
2916 DEFUN (service_password_encrypt,
2917 service_password_encrypt_cmd,
2918 "service password-encryption",
2919 "Set up miscellaneous service\n"
2920 "Enable encrypted passwords\n")
2922 if (host.encrypt)
2923 return CMD_SUCCESS;
2925 host.encrypt = 1;
2927 if (host.password)
2929 if (host.password_encrypt)
2930 XFREE (MTYPE_HOST, host.password_encrypt);
2931 host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.password));
2933 if (host.enable)
2935 if (host.enable_encrypt)
2936 XFREE (MTYPE_HOST, host.enable_encrypt);
2937 host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.enable));
2940 return CMD_SUCCESS;
2943 DEFUN (no_service_password_encrypt,
2944 no_service_password_encrypt_cmd,
2945 "no service password-encryption",
2946 NO_STR
2947 "Set up miscellaneous service\n"
2948 "Enable encrypted passwords\n")
2950 if (! host.encrypt)
2951 return CMD_SUCCESS;
2953 host.encrypt = 0;
2955 if (host.password_encrypt)
2956 XFREE (MTYPE_HOST, host.password_encrypt);
2957 host.password_encrypt = NULL;
2959 if (host.enable_encrypt)
2960 XFREE (MTYPE_HOST, host.enable_encrypt);
2961 host.enable_encrypt = NULL;
2963 return CMD_SUCCESS;
2966 DEFUN (config_terminal_length, config_terminal_length_cmd,
2967 "terminal length <0-512>",
2968 "Set terminal line parameters\n"
2969 "Set number of lines on a screen\n"
2970 "Number of lines on screen (0 for no pausing)\n")
2972 int lines;
2973 char *endptr = NULL;
2975 lines = strtol (argv[0], &endptr, 10);
2976 if (lines < 0 || lines > 512 || *endptr != '\0')
2978 vty_out (vty, "length is malformed%s", VTY_NEWLINE);
2979 return CMD_WARNING;
2981 vty->lines = lines;
2983 return CMD_SUCCESS;
2986 DEFUN (config_terminal_no_length, config_terminal_no_length_cmd,
2987 "terminal no length",
2988 "Set terminal line parameters\n"
2989 NO_STR
2990 "Set number of lines on a screen\n")
2992 vty->lines = -1;
2993 return CMD_SUCCESS;
2996 DEFUN (service_terminal_length, service_terminal_length_cmd,
2997 "service terminal-length <0-512>",
2998 "Set up miscellaneous service\n"
2999 "System wide terminal length configuration\n"
3000 "Number of lines of VTY (0 means no line control)\n")
3002 int lines;
3003 char *endptr = NULL;
3005 lines = strtol (argv[0], &endptr, 10);
3006 if (lines < 0 || lines > 512 || *endptr != '\0')
3008 vty_out (vty, "length is malformed%s", VTY_NEWLINE);
3009 return CMD_WARNING;
3011 host.lines = lines;
3013 return CMD_SUCCESS;
3016 DEFUN (no_service_terminal_length, no_service_terminal_length_cmd,
3017 "no service terminal-length [<0-512>]",
3018 NO_STR
3019 "Set up miscellaneous service\n"
3020 "System wide terminal length configuration\n"
3021 "Number of lines of VTY (0 means no line control)\n")
3023 host.lines = -1;
3024 return CMD_SUCCESS;
3027 DEFUN_HIDDEN (do_echo,
3028 echo_cmd,
3029 "echo .MESSAGE",
3030 "Echo a message back to the vty\n"
3031 "The message to echo\n")
3033 char *message;
3035 vty_out (vty, "%s%s", ((message = argv_concat(argv, argc, 0)) ? message : ""),
3036 VTY_NEWLINE);
3037 if (message)
3038 XFREE(MTYPE_TMP, message);
3039 return CMD_SUCCESS;
3042 DEFUN (config_logmsg,
3043 config_logmsg_cmd,
3044 "logmsg "LOG_LEVELS" .MESSAGE",
3045 "Send a message to enabled logging destinations\n"
3046 LOG_LEVEL_DESC
3047 "The message to send\n")
3049 int level;
3050 char *message;
3052 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3053 return CMD_ERR_NO_MATCH;
3055 zlog(NULL, level, ((message = argv_concat(argv, argc, 1)) ? message : ""));
3056 if (message)
3057 XFREE(MTYPE_TMP, message);
3058 return CMD_SUCCESS;
3061 DEFUN (show_logging,
3062 show_logging_cmd,
3063 "show logging",
3064 SHOW_STR
3065 "Show current logging configuration\n")
3067 struct zlog *zl = zlog_default;
3069 vty_out (vty, "Syslog logging: ");
3070 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
3071 vty_out (vty, "disabled");
3072 else
3073 vty_out (vty, "level %s, facility %s, ident %s",
3074 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
3075 facility_name(zl->facility), zl->ident);
3076 vty_out (vty, "%s", VTY_NEWLINE);
3078 vty_out (vty, "Stdout logging: ");
3079 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
3080 vty_out (vty, "disabled");
3081 else
3082 vty_out (vty, "level %s",
3083 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
3084 vty_out (vty, "%s", VTY_NEWLINE);
3086 vty_out (vty, "Monitor logging: ");
3087 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
3088 vty_out (vty, "disabled");
3089 else
3090 vty_out (vty, "level %s",
3091 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
3092 vty_out (vty, "%s", VTY_NEWLINE);
3094 vty_out (vty, "File logging: ");
3095 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) ||
3096 !zl->fp)
3097 vty_out (vty, "disabled");
3098 else
3099 vty_out (vty, "level %s, filename %s",
3100 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
3101 zl->filename);
3102 vty_out (vty, "%s", VTY_NEWLINE);
3104 vty_out (vty, "Protocol name: %s%s",
3105 zlog_proto_names[zl->protocol], VTY_NEWLINE);
3106 vty_out (vty, "Record priority: %s%s",
3107 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
3108 vty_out (vty, "Timestamp precision: %d%s",
3109 zl->timestamp_precision, VTY_NEWLINE);
3111 return CMD_SUCCESS;
3114 DEFUN (config_log_stdout,
3115 config_log_stdout_cmd,
3116 "log stdout",
3117 "Logging control\n"
3118 "Set stdout logging level\n")
3120 zlog_set_level (NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3121 return CMD_SUCCESS;
3124 DEFUN (config_log_stdout_level,
3125 config_log_stdout_level_cmd,
3126 "log stdout "LOG_LEVELS,
3127 "Logging control\n"
3128 "Set stdout logging level\n"
3129 LOG_LEVEL_DESC)
3131 int level;
3133 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3134 return CMD_ERR_NO_MATCH;
3135 zlog_set_level (NULL, ZLOG_DEST_STDOUT, level);
3136 return CMD_SUCCESS;
3139 DEFUN (no_config_log_stdout,
3140 no_config_log_stdout_cmd,
3141 "no log stdout [LEVEL]",
3142 NO_STR
3143 "Logging control\n"
3144 "Cancel logging to stdout\n"
3145 "Logging level\n")
3147 zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3148 return CMD_SUCCESS;
3151 DEFUN (config_log_monitor,
3152 config_log_monitor_cmd,
3153 "log monitor",
3154 "Logging control\n"
3155 "Set terminal line (monitor) logging level\n")
3157 zlog_set_level (NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3158 return CMD_SUCCESS;
3161 DEFUN (config_log_monitor_level,
3162 config_log_monitor_level_cmd,
3163 "log monitor "LOG_LEVELS,
3164 "Logging control\n"
3165 "Set terminal line (monitor) logging level\n"
3166 LOG_LEVEL_DESC)
3168 int level;
3170 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3171 return CMD_ERR_NO_MATCH;
3172 zlog_set_level (NULL, ZLOG_DEST_MONITOR, level);
3173 return CMD_SUCCESS;
3176 DEFUN (no_config_log_monitor,
3177 no_config_log_monitor_cmd,
3178 "no log monitor [LEVEL]",
3179 NO_STR
3180 "Logging control\n"
3181 "Disable terminal line (monitor) logging\n"
3182 "Logging level\n")
3184 zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3185 return CMD_SUCCESS;
3188 static int
3189 set_log_file(struct vty *vty, const char *fname, int loglevel)
3191 int ret;
3192 char *p = NULL;
3193 const char *fullpath;
3195 /* Path detection. */
3196 if (! IS_DIRECTORY_SEP (*fname))
3198 char cwd[MAXPATHLEN+1];
3199 cwd[MAXPATHLEN] = '\0';
3201 if (getcwd (cwd, MAXPATHLEN) == NULL)
3203 zlog_err ("config_log_file: Unable to alloc mem!");
3204 return CMD_WARNING;
3207 if ( (p = XMALLOC (MTYPE_TMP, strlen (cwd) + strlen (fname) + 2))
3208 == NULL)
3210 zlog_err ("config_log_file: Unable to alloc mem!");
3211 return CMD_WARNING;
3213 sprintf (p, "%s/%s", cwd, fname);
3214 fullpath = p;
3216 else
3217 fullpath = fname;
3219 ret = zlog_set_file (NULL, fullpath, loglevel);
3221 if (p)
3222 XFREE (MTYPE_TMP, p);
3224 if (!ret)
3226 vty_out (vty, "can't open logfile %s\n", fname);
3227 return CMD_WARNING;
3230 if (host.logfile)
3231 XFREE (MTYPE_HOST, host.logfile);
3233 host.logfile = XSTRDUP (MTYPE_HOST, fname);
3235 return CMD_SUCCESS;
3238 DEFUN (config_log_file,
3239 config_log_file_cmd,
3240 "log file FILENAME",
3241 "Logging control\n"
3242 "Logging to file\n"
3243 "Logging filename\n")
3245 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3248 DEFUN (config_log_file_level,
3249 config_log_file_level_cmd,
3250 "log file FILENAME "LOG_LEVELS,
3251 "Logging control\n"
3252 "Logging to file\n"
3253 "Logging filename\n"
3254 LOG_LEVEL_DESC)
3256 int level;
3258 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3259 return CMD_ERR_NO_MATCH;
3260 return set_log_file(vty, argv[0], level);
3263 DEFUN (no_config_log_file,
3264 no_config_log_file_cmd,
3265 "no log file [FILENAME]",
3266 NO_STR
3267 "Logging control\n"
3268 "Cancel logging to file\n"
3269 "Logging file name\n")
3271 zlog_reset_file (NULL);
3273 if (host.logfile)
3274 XFREE (MTYPE_HOST, host.logfile);
3276 host.logfile = NULL;
3278 return CMD_SUCCESS;
3281 ALIAS (no_config_log_file,
3282 no_config_log_file_level_cmd,
3283 "no log file FILENAME LEVEL",
3284 NO_STR
3285 "Logging control\n"
3286 "Cancel logging to file\n"
3287 "Logging file name\n"
3288 "Logging level\n")
3290 DEFUN (config_log_syslog,
3291 config_log_syslog_cmd,
3292 "log syslog",
3293 "Logging control\n"
3294 "Set syslog logging level\n")
3296 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3297 return CMD_SUCCESS;
3300 DEFUN (config_log_syslog_level,
3301 config_log_syslog_level_cmd,
3302 "log syslog "LOG_LEVELS,
3303 "Logging control\n"
3304 "Set syslog logging level\n"
3305 LOG_LEVEL_DESC)
3307 int level;
3309 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3310 return CMD_ERR_NO_MATCH;
3311 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, level);
3312 return CMD_SUCCESS;
3315 DEFUN_DEPRECATED (config_log_syslog_facility,
3316 config_log_syslog_facility_cmd,
3317 "log syslog facility "LOG_FACILITIES,
3318 "Logging control\n"
3319 "Logging goes to syslog\n"
3320 "(Deprecated) Facility parameter for syslog messages\n"
3321 LOG_FACILITY_DESC)
3323 int facility;
3325 if ((facility = facility_match(argv[0])) < 0)
3326 return CMD_ERR_NO_MATCH;
3328 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3329 zlog_default->facility = facility;
3330 return CMD_SUCCESS;
3333 DEFUN (no_config_log_syslog,
3334 no_config_log_syslog_cmd,
3335 "no log syslog [LEVEL]",
3336 NO_STR
3337 "Logging control\n"
3338 "Cancel logging to syslog\n"
3339 "Logging level\n")
3341 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3342 return CMD_SUCCESS;
3345 ALIAS (no_config_log_syslog,
3346 no_config_log_syslog_facility_cmd,
3347 "no log syslog facility "LOG_FACILITIES,
3348 NO_STR
3349 "Logging control\n"
3350 "Logging goes to syslog\n"
3351 "Facility parameter for syslog messages\n"
3352 LOG_FACILITY_DESC)
3354 DEFUN (config_log_facility,
3355 config_log_facility_cmd,
3356 "log facility "LOG_FACILITIES,
3357 "Logging control\n"
3358 "Facility parameter for syslog messages\n"
3359 LOG_FACILITY_DESC)
3361 int facility;
3363 if ((facility = facility_match(argv[0])) < 0)
3364 return CMD_ERR_NO_MATCH;
3365 zlog_default->facility = facility;
3366 return CMD_SUCCESS;
3369 DEFUN (no_config_log_facility,
3370 no_config_log_facility_cmd,
3371 "no log facility [FACILITY]",
3372 NO_STR
3373 "Logging control\n"
3374 "Reset syslog facility to default (daemon)\n"
3375 "Syslog facility\n")
3377 zlog_default->facility = LOG_DAEMON;
3378 return CMD_SUCCESS;
3381 DEFUN_DEPRECATED (config_log_trap,
3382 config_log_trap_cmd,
3383 "log trap "LOG_LEVELS,
3384 "Logging control\n"
3385 "(Deprecated) Set logging level and default for all destinations\n"
3386 LOG_LEVEL_DESC)
3388 int new_level ;
3389 int i;
3391 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3392 return CMD_ERR_NO_MATCH;
3394 zlog_default->default_lvl = new_level;
3395 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3396 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3397 zlog_default->maxlvl[i] = new_level;
3398 return CMD_SUCCESS;
3401 DEFUN_DEPRECATED (no_config_log_trap,
3402 no_config_log_trap_cmd,
3403 "no log trap [LEVEL]",
3404 NO_STR
3405 "Logging control\n"
3406 "Permit all logging information\n"
3407 "Logging level\n")
3409 zlog_default->default_lvl = LOG_DEBUG;
3410 return CMD_SUCCESS;
3413 DEFUN (config_log_record_priority,
3414 config_log_record_priority_cmd,
3415 "log record-priority",
3416 "Logging control\n"
3417 "Log the priority of the message within the message\n")
3419 zlog_default->record_priority = 1 ;
3420 return CMD_SUCCESS;
3423 DEFUN (no_config_log_record_priority,
3424 no_config_log_record_priority_cmd,
3425 "no log record-priority",
3426 NO_STR
3427 "Logging control\n"
3428 "Do not log the priority of the message within the message\n")
3430 zlog_default->record_priority = 0 ;
3431 return CMD_SUCCESS;
3434 DEFUN (config_log_timestamp_precision,
3435 config_log_timestamp_precision_cmd,
3436 "log timestamp precision <0-6>",
3437 "Logging control\n"
3438 "Timestamp configuration\n"
3439 "Set the timestamp precision\n"
3440 "Number of subsecond digits\n")
3442 if (argc != 1)
3444 vty_out (vty, "Insufficient arguments%s", VTY_NEWLINE);
3445 return CMD_WARNING;
3448 VTY_GET_INTEGER_RANGE("Timestamp Precision",
3449 zlog_default->timestamp_precision, argv[0], 0, 6);
3450 return CMD_SUCCESS;
3453 DEFUN (no_config_log_timestamp_precision,
3454 no_config_log_timestamp_precision_cmd,
3455 "no log timestamp precision",
3456 NO_STR
3457 "Logging control\n"
3458 "Timestamp configuration\n"
3459 "Reset the timestamp precision to the default value of 0\n")
3461 zlog_default->timestamp_precision = 0 ;
3462 return CMD_SUCCESS;
3465 DEFUN (banner_motd_file,
3466 banner_motd_file_cmd,
3467 "banner motd file [FILE]",
3468 "Set banner\n"
3469 "Banner for motd\n"
3470 "Banner from a file\n"
3471 "Filename\n")
3473 if (host.motdfile)
3474 XFREE (MTYPE_HOST, host.motdfile);
3475 host.motdfile = XSTRDUP (MTYPE_HOST, argv[0]);
3477 return CMD_SUCCESS;
3480 DEFUN (banner_motd_default,
3481 banner_motd_default_cmd,
3482 "banner motd default",
3483 "Set banner string\n"
3484 "Strings for motd\n"
3485 "Default string\n")
3487 host.motd = default_motd;
3488 return CMD_SUCCESS;
3491 DEFUN (no_banner_motd,
3492 no_banner_motd_cmd,
3493 "no banner motd",
3494 NO_STR
3495 "Set banner string\n"
3496 "Strings for motd\n")
3498 host.motd = NULL;
3499 if (host.motdfile)
3500 XFREE (MTYPE_HOST, host.motdfile);
3501 host.motdfile = NULL;
3502 return CMD_SUCCESS;
3505 /* Set config filename. Called from vty.c */
3506 void
3507 host_config_set (char *filename)
3509 host.config = XSTRDUP (MTYPE_HOST, filename);
3512 void
3513 install_default (enum node_type node)
3515 install_element (node, &config_exit_cmd);
3516 install_element (node, &config_quit_cmd);
3517 install_element (node, &config_end_cmd);
3518 install_element (node, &config_help_cmd);
3519 install_element (node, &config_list_cmd);
3521 install_element (node, &config_write_terminal_cmd);
3522 install_element (node, &config_write_file_cmd);
3523 install_element (node, &config_write_memory_cmd);
3524 install_element (node, &config_write_cmd);
3525 install_element (node, &show_running_config_cmd);
3528 /* Initialize command interface. Install basic nodes and commands. */
3529 void
3530 cmd_init (int terminal)
3532 /* Allocate initial top vector of commands. */
3533 cmdvec = vector_init (VECTOR_MIN_SIZE);
3535 /* Default host value settings. */
3536 host.name = NULL;
3537 host.password = NULL;
3538 host.enable = NULL;
3539 host.logfile = NULL;
3540 host.config = NULL;
3541 host.lines = -1;
3542 host.motd = default_motd;
3543 host.motdfile = NULL;
3545 /* Install top nodes. */
3546 install_node (&view_node, NULL);
3547 install_node (&enable_node, NULL);
3548 install_node (&auth_node, NULL);
3549 install_node (&auth_enable_node, NULL);
3550 install_node (&restricted_node, NULL);
3551 install_node (&config_node, config_write_host);
3553 /* Each node's basic commands. */
3554 install_element (VIEW_NODE, &show_version_cmd);
3555 if (terminal)
3557 install_element (VIEW_NODE, &config_list_cmd);
3558 install_element (VIEW_NODE, &config_exit_cmd);
3559 install_element (VIEW_NODE, &config_quit_cmd);
3560 install_element (VIEW_NODE, &config_help_cmd);
3561 install_element (VIEW_NODE, &config_enable_cmd);
3562 install_element (VIEW_NODE, &config_terminal_length_cmd);
3563 install_element (VIEW_NODE, &config_terminal_no_length_cmd);
3564 install_element (VIEW_NODE, &show_logging_cmd);
3565 install_element (VIEW_NODE, &echo_cmd);
3567 install_element (RESTRICTED_NODE, &config_list_cmd);
3568 install_element (RESTRICTED_NODE, &config_exit_cmd);
3569 install_element (RESTRICTED_NODE, &config_quit_cmd);
3570 install_element (RESTRICTED_NODE, &config_help_cmd);
3571 install_element (RESTRICTED_NODE, &config_enable_cmd);
3572 install_element (RESTRICTED_NODE, &config_terminal_length_cmd);
3573 install_element (RESTRICTED_NODE, &config_terminal_no_length_cmd);
3574 install_element (RESTRICTED_NODE, &echo_cmd);
3577 if (terminal)
3579 install_default (ENABLE_NODE);
3580 install_element (ENABLE_NODE, &config_disable_cmd);
3581 install_element (ENABLE_NODE, &config_terminal_cmd);
3582 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3584 install_element (ENABLE_NODE, &show_startup_config_cmd);
3585 install_element (ENABLE_NODE, &show_version_cmd);
3587 if (terminal)
3589 install_element (ENABLE_NODE, &config_terminal_length_cmd);
3590 install_element (ENABLE_NODE, &config_terminal_no_length_cmd);
3591 install_element (ENABLE_NODE, &show_logging_cmd);
3592 install_element (ENABLE_NODE, &echo_cmd);
3593 install_element (ENABLE_NODE, &config_logmsg_cmd);
3595 install_default (CONFIG_NODE);
3598 install_element (CONFIG_NODE, &hostname_cmd);
3599 install_element (CONFIG_NODE, &no_hostname_cmd);
3601 if (terminal)
3603 install_element (CONFIG_NODE, &password_cmd);
3604 install_element (CONFIG_NODE, &password_text_cmd);
3605 install_element (CONFIG_NODE, &enable_password_cmd);
3606 install_element (CONFIG_NODE, &enable_password_text_cmd);
3607 install_element (CONFIG_NODE, &no_enable_password_cmd);
3609 install_element (CONFIG_NODE, &config_log_stdout_cmd);
3610 install_element (CONFIG_NODE, &config_log_stdout_level_cmd);
3611 install_element (CONFIG_NODE, &no_config_log_stdout_cmd);
3612 install_element (CONFIG_NODE, &config_log_monitor_cmd);
3613 install_element (CONFIG_NODE, &config_log_monitor_level_cmd);
3614 install_element (CONFIG_NODE, &no_config_log_monitor_cmd);
3615 install_element (CONFIG_NODE, &config_log_file_cmd);
3616 install_element (CONFIG_NODE, &config_log_file_level_cmd);
3617 install_element (CONFIG_NODE, &no_config_log_file_cmd);
3618 install_element (CONFIG_NODE, &no_config_log_file_level_cmd);
3619 install_element (CONFIG_NODE, &config_log_syslog_cmd);
3620 install_element (CONFIG_NODE, &config_log_syslog_level_cmd);
3621 install_element (CONFIG_NODE, &config_log_syslog_facility_cmd);
3622 install_element (CONFIG_NODE, &no_config_log_syslog_cmd);
3623 install_element (CONFIG_NODE, &no_config_log_syslog_facility_cmd);
3624 install_element (CONFIG_NODE, &config_log_facility_cmd);
3625 install_element (CONFIG_NODE, &no_config_log_facility_cmd);
3626 install_element (CONFIG_NODE, &config_log_trap_cmd);
3627 install_element (CONFIG_NODE, &no_config_log_trap_cmd);
3628 install_element (CONFIG_NODE, &config_log_record_priority_cmd);
3629 install_element (CONFIG_NODE, &no_config_log_record_priority_cmd);
3630 install_element (CONFIG_NODE, &config_log_timestamp_precision_cmd);
3631 install_element (CONFIG_NODE, &no_config_log_timestamp_precision_cmd);
3632 install_element (CONFIG_NODE, &service_password_encrypt_cmd);
3633 install_element (CONFIG_NODE, &no_service_password_encrypt_cmd);
3634 install_element (CONFIG_NODE, &banner_motd_default_cmd);
3635 install_element (CONFIG_NODE, &banner_motd_file_cmd);
3636 install_element (CONFIG_NODE, &no_banner_motd_cmd);
3637 install_element (CONFIG_NODE, &service_terminal_length_cmd);
3638 install_element (CONFIG_NODE, &no_service_terminal_length_cmd);
3640 install_element (VIEW_NODE, &show_thread_cpu_cmd);
3641 install_element (ENABLE_NODE, &show_thread_cpu_cmd);
3642 install_element (RESTRICTED_NODE, &show_thread_cpu_cmd);
3643 install_element (VIEW_NODE, &show_work_queues_cmd);
3644 install_element (ENABLE_NODE, &show_work_queues_cmd);
3646 srand(time(NULL));