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