2 * CLI backend interface.
5 * Copyright (C) 2016 Cumulus Networks, Inc.
6 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
7 * Copyright (C) 2013 by Open Source Routing.
8 * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
10 * This file is part of GNU Zebra.
12 * GNU Zebra is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
17 * GNU Zebra is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with GNU Zebra; see the file COPYING. If not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
33 #include <lib/version.h>
39 #include "workqueue.h"
41 #include "command_match.h"
44 DEFINE_MTYPE( LIB
, HOST
, "Host config")
45 DEFINE_MTYPE( LIB
, STRVEC
, "String vector")
46 DEFINE_MTYPE_STATIC(LIB
, CMD_TOKENS
, "Command desc")
48 /* Command vector which includes some level of command lists. Normally
49 each daemon maintains each own cmdvec. */
52 /* Host information structure. */
55 /* Standard command node structures. */
56 static struct cmd_node auth_node
=
62 static struct cmd_node view_node
=
68 static struct cmd_node auth_enable_node
=
74 static struct cmd_node enable_node
=
80 static struct cmd_node config_node
=
87 /* Default motd string. */
88 static const char *default_motd
= FRR_DEFAULT_MOTD
;
90 static const struct facility_map
{
94 } syslog_facilities
[] =
96 { LOG_KERN
, "kern", 1 },
97 { LOG_USER
, "user", 2 },
98 { LOG_MAIL
, "mail", 1 },
99 { LOG_DAEMON
, "daemon", 1 },
100 { LOG_AUTH
, "auth", 1 },
101 { LOG_SYSLOG
, "syslog", 1 },
102 { LOG_LPR
, "lpr", 2 },
103 { LOG_NEWS
, "news", 1 },
104 { LOG_UUCP
, "uucp", 2 },
105 { LOG_CRON
, "cron", 1 },
107 { LOG_FTP
, "ftp", 1 },
109 { LOG_LOCAL0
, "local0", 6 },
110 { LOG_LOCAL1
, "local1", 6 },
111 { LOG_LOCAL2
, "local2", 6 },
112 { LOG_LOCAL3
, "local3", 6 },
113 { LOG_LOCAL4
, "local4", 6 },
114 { LOG_LOCAL5
, "local5", 6 },
115 { LOG_LOCAL6
, "local6", 6 },
116 { LOG_LOCAL7
, "local7", 6 },
121 facility_name(int facility
)
123 const struct facility_map
*fm
;
125 for (fm
= syslog_facilities
; fm
->name
; fm
++)
126 if (fm
->facility
== facility
)
132 facility_match(const char *str
)
134 const struct facility_map
*fm
;
136 for (fm
= syslog_facilities
; fm
->name
; fm
++)
137 if (!strncmp(str
,fm
->name
,fm
->match
))
143 level_match(const char *s
)
147 for ( level
= 0 ; zlog_priority
[level
] != NULL
; level
++ )
148 if (!strncmp (s
, zlog_priority
[level
], 2))
150 return ZLOG_DISABLED
;
153 /* This is called from main when a daemon is invoked with -v or --version. */
155 print_version (const char *progname
)
157 printf ("%s version %s\n", progname
, FRR_VERSION
);
158 printf ("%s\n", FRR_COPYRIGHT
);
159 printf ("configured with:\n\t%s\n", FRR_CONFIG_ARGS
);
163 /* Utility function to concatenate argv argument into a single string
164 with inserting ' ' character between each argument. */
166 argv_concat (struct cmd_token
**argv
, int argc
, int shift
)
174 for (i
= shift
; i
< argc
; i
++)
175 len
+= strlen(argv
[i
]->arg
)+1;
178 p
= str
= XMALLOC(MTYPE_TMP
, len
);
179 for (i
= shift
; i
< argc
; i
++)
182 memcpy(p
, argv
[i
]->arg
, (arglen
= strlen(argv
[i
]->arg
)));
191 * Convenience function for accessing argv data.
195 * @param text definition snippet of the desired token
196 * @param index the starting index, and where to store the
197 * index of the found token if it exists
198 * @return 1 if found, 0 otherwise
201 argv_find (struct cmd_token
**argv
, int argc
, const char *text
, int *index
)
204 for (int i
= *index
; i
< argc
&& found
== 0; i
++)
205 if ((found
= strmatch (text
, argv
[i
]->text
)))
211 cmd_hash_key (void *p
)
213 return (uintptr_t) p
;
217 cmd_hash_cmp (const void *a
, const void *b
)
222 /* Install top node of command vector. */
224 install_node (struct cmd_node
*node
,
225 int (*func
) (struct vty
*))
227 vector_set_index (cmdvec
, node
->node
, node
);
229 node
->cmdgraph
= graph_new ();
230 node
->cmd_vector
= vector_init (VECTOR_MIN_SIZE
);
232 struct cmd_token
*token
= new_cmd_token (START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
233 graph_new_node (node
->cmdgraph
, token
, (void (*)(void *)) &del_cmd_token
);
234 node
->cmd_hash
= hash_create (cmd_hash_key
, cmd_hash_cmp
);
238 * Tokenizes a string, storing tokens in a vector.
239 * Whitespace is ignored.
241 * Delimiter string = " \n\r\t".
243 * @param string to tokenize
244 * @return tokenized string
247 cmd_make_strvec (const char *string
)
249 if (!string
) return NULL
;
251 char *copy
, *copystart
;
252 copystart
= copy
= XSTRDUP (MTYPE_TMP
, string
);
254 // skip leading whitespace
255 while (isspace ((int) *copy
) && *copy
!= '\0') copy
++;
257 // if the entire string was whitespace or a comment, return
258 if (*copy
== '\0' || *copy
== '!' || *copy
== '#')
260 XFREE (MTYPE_TMP
, copystart
);
264 vector strvec
= vector_init (VECTOR_MIN_SIZE
);
265 const char *delim
= " \n\r\t", *tok
= NULL
;
268 tok
= strsep (©
, delim
);
270 vector_set (strvec
, XSTRDUP (MTYPE_STRVEC
, tok
));
273 XFREE (MTYPE_TMP
, copystart
);
277 /* Free allocated string vector. */
279 cmd_free_strvec (vector v
)
287 for (i
= 0; i
< vector_active (v
); i
++)
288 if ((cp
= vector_slot (v
, i
)) != NULL
)
289 XFREE (MTYPE_STRVEC
, cp
);
295 cmd_concat_strvec (vector v
)
298 for (unsigned int i
= 0; i
< vector_active (v
); i
++)
299 if (vector_slot (v
, i
))
300 strsize
+= strlen ((char *) vector_slot (v
, i
)) + 1;
302 char *concatenated
= calloc (sizeof (char), strsize
);
303 for (unsigned int i
= 0; i
< vector_active (v
); i
++)
305 strlcat (concatenated
, (char *) vector_slot (v
, i
), strsize
);
306 strlcat (concatenated
, " ", strsize
);
312 /* Return prompt character of specified node. */
314 cmd_prompt (enum node_type node
)
316 struct cmd_node
*cnode
;
318 cnode
= vector_slot (cmdvec
, node
);
319 return cnode
->prompt
;
322 /* Install a command into a node. */
324 install_element (enum node_type ntype
, struct cmd_element
*cmd
)
326 struct cmd_node
*cnode
;
328 /* cmd_init hasn't been called */
331 fprintf (stderr
, "%s called before cmd_init, breakage likely\n",
336 cnode
= vector_slot (cmdvec
, ntype
);
340 fprintf (stderr
, "Command node %d doesn't exist, please check it\n",
345 if (hash_lookup (cnode
->cmd_hash
, cmd
) != NULL
)
348 "Multiple command installs to node %d of command:\n%s\n",
353 assert (hash_get (cnode
->cmd_hash
, cmd
, hash_alloc_intern
));
355 command_parse_format (cnode
->cmdgraph
, cmd
);
356 vector_set (cnode
->cmd_vector
, cmd
);
358 if (ntype
== VIEW_NODE
)
359 install_element (ENABLE_NODE
, cmd
);
362 static const unsigned char itoa64
[] =
363 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
366 to64(char *s
, long v
, int n
)
370 *s
++ = itoa64
[v
&0x3f];
376 zencrypt (const char *passwd
)
380 char *crypt (const char *, const char *);
384 to64(&salt
[0], random(), 3);
385 to64(&salt
[3], tv
.tv_usec
, 3);
388 return crypt (passwd
, salt
);
391 /* This function write configuration of this host. */
393 config_write_host (struct vty
*vty
)
396 vty_out (vty
, "hostname %s%s", host
.name
, VTY_NEWLINE
);
400 if (host
.password_encrypt
)
401 vty_out (vty
, "password 8 %s%s", host
.password_encrypt
, VTY_NEWLINE
);
402 if (host
.enable_encrypt
)
403 vty_out (vty
, "enable password 8 %s%s", host
.enable_encrypt
, VTY_NEWLINE
);
408 vty_out (vty
, "password %s%s", host
.password
, VTY_NEWLINE
);
410 vty_out (vty
, "enable password %s%s", host
.enable
, VTY_NEWLINE
);
413 if (zlog_default
->default_lvl
!= LOG_DEBUG
)
415 vty_out (vty
, "! N.B. The 'log trap' command is deprecated.%s",
417 vty_out (vty
, "log trap %s%s",
418 zlog_priority
[zlog_default
->default_lvl
], VTY_NEWLINE
);
421 if (host
.logfile
&& (zlog_default
->maxlvl
[ZLOG_DEST_FILE
] != ZLOG_DISABLED
))
423 vty_out (vty
, "log file %s", host
.logfile
);
424 if (zlog_default
->maxlvl
[ZLOG_DEST_FILE
] != zlog_default
->default_lvl
)
426 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_FILE
]]);
427 vty_out (vty
, "%s", VTY_NEWLINE
);
430 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != ZLOG_DISABLED
)
432 vty_out (vty
, "log stdout");
433 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != zlog_default
->default_lvl
)
435 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
]]);
436 vty_out (vty
, "%s", VTY_NEWLINE
);
439 if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
440 vty_out(vty
,"no log monitor%s",VTY_NEWLINE
);
441 else if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] != zlog_default
->default_lvl
)
442 vty_out(vty
,"log monitor %s%s",
443 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]],VTY_NEWLINE
);
445 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
447 vty_out (vty
, "log syslog");
448 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != zlog_default
->default_lvl
)
450 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
]]);
451 vty_out (vty
, "%s", VTY_NEWLINE
);
454 if (zlog_default
->facility
!= LOG_DAEMON
)
455 vty_out (vty
, "log facility %s%s",
456 facility_name(zlog_default
->facility
), VTY_NEWLINE
);
458 if (zlog_default
->record_priority
== 1)
459 vty_out (vty
, "log record-priority%s", VTY_NEWLINE
);
461 if (zlog_default
->timestamp_precision
> 0)
462 vty_out (vty
, "log timestamp precision %d%s",
463 zlog_default
->timestamp_precision
, VTY_NEWLINE
);
466 vty_out (vty
, "service advanced-vty%s", VTY_NEWLINE
);
469 vty_out (vty
, "service password-encryption%s", VTY_NEWLINE
);
472 vty_out (vty
, "service terminal-length %d%s", host
.lines
,
476 vty_out (vty
, "banner motd file %s%s", host
.motdfile
, VTY_NEWLINE
);
477 else if (! host
.motd
)
478 vty_out (vty
, "no banner motd%s", VTY_NEWLINE
);
483 /* Utility function for getting command graph. */
484 static struct graph
*
485 cmd_node_graph (vector v
, enum node_type ntype
)
487 struct cmd_node
*cnode
= vector_slot (v
, ntype
);
488 return cnode
->cmdgraph
;
492 cmd_try_do_shortcut (enum node_type node
, char* first_word
) {
493 if ( first_word
!= NULL
&&
496 node
!= AUTH_ENABLE_NODE
&&
497 node
!= ENABLE_NODE
&&
498 0 == strcmp( "do", first_word
) )
504 * Compare function for cmd_token.
505 * Used with qsort to sort command completions.
508 compare_completions (const void *fst
, const void *snd
)
510 struct cmd_token
*first
= *(struct cmd_token
**) fst
,
511 *secnd
= *(struct cmd_token
**) snd
;
512 return strcmp (first
->text
, secnd
->text
);
516 * Takes a list of completions returned by command_complete,
517 * dedeuplicates them based on both text and description,
518 * sorts them, and returns them as a vector.
520 * @param completions linked list of cmd_token
521 * @return deduplicated and sorted vector with
524 completions_to_vec (struct list
*completions
)
526 vector comps
= vector_init (VECTOR_MIN_SIZE
);
529 struct cmd_token
*token
, *cr
= NULL
;
530 unsigned int i
, exists
;
531 for (ALL_LIST_ELEMENTS_RO(completions
,ln
,token
))
533 if (token
->type
== END_TKN
&& (cr
= token
))
536 // linear search for token in completions vector
538 for (i
= 0; i
< vector_active (comps
) && !exists
; i
++)
540 struct cmd_token
*curr
= vector_slot (comps
, i
);
541 exists
= !strcmp (curr
->text
, token
->text
) &&
542 !strcmp (curr
->desc
, token
->desc
);
546 vector_set (comps
, token
);
551 vector_active (comps
),
553 &compare_completions
);
555 // make <cr> the first element, if it is present
558 vector_set_index (comps
, vector_active (comps
), NULL
);
559 memmove (comps
->index
+ 1, comps
->index
, (comps
->alloced
- 1) * sizeof (void *));
560 vector_set_index (comps
, 0, cr
);
566 * Generates a vector of cmd_token representing possible completions
567 * on the current input.
569 * @param vline the vectorized input line
570 * @param vty the vty with the node to match on
571 * @param status pointer to matcher status code
572 * @return vector of struct cmd_token * with possible completions
575 cmd_complete_command_real (vector vline
, struct vty
*vty
, int *status
)
577 struct list
*completions
;
578 struct graph
*cmdgraph
= cmd_node_graph (cmdvec
, vty
->node
);
580 enum matcher_rv rv
= command_complete (cmdgraph
, vline
, &completions
);
582 if (MATCHER_ERROR(rv
))
584 *status
= CMD_ERR_NO_MATCH
;
588 vector comps
= completions_to_vec (completions
);
589 list_delete (completions
);
591 // set status code appropriately
592 switch (vector_active (comps
))
595 *status
= CMD_ERR_NO_MATCH
;
598 *status
= CMD_COMPLETE_FULL_MATCH
;
601 *status
= CMD_COMPLETE_LIST_MATCH
;
608 cmd_describe_command (vector vline
, struct vty
*vty
, int *status
)
612 if ( cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0) ) )
614 enum node_type onode
;
615 vector shifted_vline
;
619 vty
->node
= ENABLE_NODE
;
620 /* We can try it on enable node, cos' the vty is authenticated */
622 shifted_vline
= vector_init (vector_count(vline
));
624 for (index
= 1; index
< vector_active (vline
); index
++)
626 vector_set_index (shifted_vline
, index
-1, vector_lookup(vline
, index
));
629 ret
= cmd_complete_command_real (shifted_vline
, vty
, status
);
631 vector_free(shifted_vline
);
636 return cmd_complete_command_real (vline
, vty
, status
);
640 * Generate possible tab-completions for the given input. This function only
641 * returns results that would result in a valid command if used as Readline
642 * completions (as is the case in vtysh). For instance, if the passed vline ends
643 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
645 * @param vline vectorized input line
647 * @param status location to store matcher status code in
648 * @return set of valid strings for use with Readline as tab-completions.
652 cmd_complete_command (vector vline
, struct vty
*vty
, int *status
)
655 int original_node
= vty
->node
;
656 vector input_line
= vector_init (vector_count (vline
));
658 // if the first token is 'do' we'll want to execute the command in the enable node
659 int do_shortcut
= cmd_try_do_shortcut (vty
->node
, vector_slot (vline
, 0));
660 vty
->node
= do_shortcut
? ENABLE_NODE
: original_node
;
662 // construct the input line we'll be matching on
663 unsigned int offset
= (do_shortcut
) ? 1 : 0;
664 for (unsigned index
= 0; index
+ offset
< vector_active (vline
); index
++)
665 vector_set_index (input_line
, index
, vector_lookup (vline
, index
+ offset
));
667 // get token completions -- this is a copying operation
668 vector comps
= NULL
, initial_comps
;
669 initial_comps
= cmd_complete_command_real (input_line
, vty
, status
);
671 if (!MATCHER_ERROR (*status
))
673 assert (initial_comps
);
674 // filter out everything that is not suitable for a tab-completion
675 comps
= vector_init (VECTOR_MIN_SIZE
);
676 for (unsigned int i
= 0; i
< vector_active(initial_comps
); i
++)
678 struct cmd_token
*token
= vector_slot (initial_comps
, i
);
679 if (token
->type
== WORD_TKN
)
680 vector_set (comps
, token
);
682 vector_free (initial_comps
);
684 // since we filtered results, we need to re-set status code
685 switch (vector_active (comps
))
688 *status
= CMD_ERR_NO_MATCH
;
691 *status
= CMD_COMPLETE_FULL_MATCH
;
694 *status
= CMD_COMPLETE_LIST_MATCH
;
697 // copy completions text into an array of char*
698 ret
= XMALLOC (MTYPE_TMP
, (vector_active (comps
)+1) * sizeof (char *));
700 for (i
= 0; i
< vector_active (comps
); i
++)
702 struct cmd_token
*token
= vector_slot (comps
, i
);
703 ret
[i
] = XSTRDUP (MTYPE_TMP
, token
->text
);
704 vector_unset (comps
, i
);
706 // set the last element to NULL, because this array is used in
707 // a Readline completion_generator function which expects NULL
708 // as a sentinel value
714 // comps should always be null here
717 // free the adjusted input line
718 vector_free (input_line
);
720 // reset vty->node to its original value
721 vty
->node
= original_node
;
726 /* return parent node */
727 /* MUST eventually converge on CONFIG_NODE */
729 node_parent ( enum node_type node
)
733 assert (node
> CONFIG_NODE
);
740 case BGP_ENCAPV6_NODE
:
741 case BGP_VNC_DEFAULTS_NODE
:
742 case BGP_VNC_NVE_GROUP_NODE
:
743 case BGP_VNC_L2_GROUP_NODE
:
750 case KEYCHAIN_KEY_NODE
:
753 case LINK_PARAMS_NODE
:
754 ret
= INTERFACE_NODE
;
760 case LDP_IPV4_IFACE_NODE
:
763 case LDP_IPV6_IFACE_NODE
:
766 case LDP_PSEUDOWIRE_NODE
:
767 ret
= LDP_L2VPN_NODE
;
777 /* Execute command by argument vline vector. */
779 cmd_execute_command_real (vector vline
,
780 enum filter_type filter
,
782 const struct cmd_element
**cmd
)
784 struct list
*argv_list
;
785 enum matcher_rv status
;
786 const struct cmd_element
*matched_element
= NULL
;
788 struct graph
*cmdgraph
= cmd_node_graph (cmdvec
, vty
->node
);
789 status
= command_match (cmdgraph
, vline
, &argv_list
, &matched_element
);
792 *cmd
= matched_element
;
794 // if matcher error, return corresponding CMD_ERR
795 if (MATCHER_ERROR(status
))
799 case MATCHER_INCOMPLETE
:
800 return CMD_ERR_INCOMPLETE
;
801 case MATCHER_AMBIGUOUS
:
802 return CMD_ERR_AMBIGUOUS
;
804 return CMD_ERR_NO_MATCH
;
808 // build argv array from argv list
809 struct cmd_token
**argv
= XMALLOC (MTYPE_TMP
, argv_list
->count
* sizeof (struct cmd_token
*));
811 struct cmd_token
*token
;
813 for (ALL_LIST_ELEMENTS_RO(argv_list
,ln
,token
))
816 int argc
= argv_list
->count
;
819 if (matched_element
->daemon
)
820 ret
= CMD_SUCCESS_DAEMON
;
822 ret
= matched_element
->func (matched_element
, vty
, argc
, argv
);
824 // delete list and cmd_token's in it
825 list_delete (argv_list
);
826 XFREE (MTYPE_TMP
, argv
);
832 * Execute a given command, handling things like "do ..." and checking
833 * whether the given command might apply at a parent node if doesn't
834 * apply for the current node.
836 * @param vline Command line input, vector of char* where each element is
838 * @param vty The vty context in which the command should be executed.
839 * @param cmd Pointer where the struct cmd_element of the matched command
840 * will be stored, if any. May be set to NULL if this info is
842 * @param vtysh If set != 0, don't lookup the command at parent nodes.
843 * @return The status of the command that has been executed or an error code
844 * as to why no command could be executed.
847 cmd_execute_command (vector vline
, struct vty
*vty
,
848 const struct cmd_element
**cmd
,
851 int ret
, saved_ret
= 0;
852 enum node_type onode
, try_node
;
854 onode
= try_node
= vty
->node
;
856 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0)))
858 vector shifted_vline
;
861 vty
->node
= ENABLE_NODE
;
862 /* We can try it on enable node, cos' the vty is authenticated */
864 shifted_vline
= vector_init (vector_count(vline
));
866 for (index
= 1; index
< vector_active (vline
); index
++)
867 vector_set_index (shifted_vline
, index
-1, vector_lookup(vline
, index
));
869 ret
= cmd_execute_command_real (shifted_vline
, FILTER_RELAXED
, vty
, cmd
);
871 vector_free(shifted_vline
);
876 saved_ret
= ret
= cmd_execute_command_real (vline
, FILTER_RELAXED
, vty
, cmd
);
881 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
883 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
884 while (vty
->node
> CONFIG_NODE
)
886 try_node
= node_parent(try_node
);
887 vty
->node
= try_node
;
888 ret
= cmd_execute_command_real (vline
, FILTER_RELAXED
, vty
, cmd
);
889 if (ret
== CMD_SUCCESS
|| ret
== CMD_WARNING
)
892 /* no command succeeded, reset the vty to the original node */
896 /* return command status for original node */
901 * Execute a given command, matching it strictly against the current node.
902 * This mode is used when reading config files.
904 * @param vline Command line input, vector of char* where each element is
906 * @param vty The vty context in which the command should be executed.
907 * @param cmd Pointer where the struct cmd_element* of the matched command
908 * will be stored, if any. May be set to NULL if this info is
910 * @return The status of the command that has been executed or an error code
911 * as to why no command could be executed.
914 cmd_execute_command_strict (vector vline
, struct vty
*vty
,
915 const struct cmd_element
**cmd
)
917 return cmd_execute_command_real(vline
, FILTER_STRICT
, vty
, cmd
);
921 * Parse one line of config, walking up the parse tree attempting to find a match
923 * @param vty The vty context in which the command should be executed.
924 * @param cmd Pointer where the struct cmd_element* of the match command
925 * will be stored, if any. May be set to NULL if this info is
927 * @param use_daemon Boolean to control whether or not we match on CMD_SUCCESS_DAEMON
929 * @return The status of the command that has been executed or an error code
930 * as to why no command could be executed.
933 command_config_read_one_line (struct vty
*vty
, const struct cmd_element
**cmd
, int use_daemon
)
939 vline
= cmd_make_strvec (vty
->buf
);
941 /* In case of comment line */
945 /* Execute configuration command : this is strict match */
946 ret
= cmd_execute_command_strict (vline
, vty
, cmd
);
948 // Climb the tree and try the command again at each node
949 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
950 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
951 ret
!= CMD_SUCCESS
&&
952 ret
!= CMD_WARNING
&&
953 vty
->node
!= CONFIG_NODE
) {
955 saved_node
= vty
->node
;
957 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
958 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
959 ret
!= CMD_SUCCESS
&&
960 ret
!= CMD_WARNING
&&
961 vty
->node
> CONFIG_NODE
) {
962 vty
->node
= node_parent(vty
->node
);
963 ret
= cmd_execute_command_strict (vline
, vty
, cmd
);
966 // If climbing the tree did not work then ignore the command and
967 // stay at the same node
968 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
969 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
970 ret
!= CMD_SUCCESS
&&
973 vty
->node
= saved_node
;
977 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
978 memcpy (vty
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
980 cmd_free_strvec (vline
);
985 /* Configuration make from file. */
987 config_from_file (struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
989 int ret
, error_ret
=0;
992 while (fgets (vty
->buf
, VTY_BUFSIZ
, fp
))
997 ret
= command_config_read_one_line (vty
, NULL
, 0);
999 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
&&
1000 ret
!= CMD_ERR_NOTHING_TODO
)
1011 /* Configuration from terminal */
1012 DEFUN (config_terminal
,
1013 config_terminal_cmd
,
1014 "configure terminal",
1015 "Configuration from vty interface\n"
1016 "Configuration terminal\n")
1018 if (vty_config_lock (vty
))
1019 vty
->node
= CONFIG_NODE
;
1022 vty_out (vty
, "VTY configuration is locked by other VTY%s", VTY_NEWLINE
);
1028 /* Enable command */
1032 "Turn on privileged mode command\n")
1034 /* If enable password is NULL, change to ENABLE_NODE */
1035 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
) ||
1036 vty
->type
== VTY_SHELL_SERV
)
1037 vty
->node
= ENABLE_NODE
;
1039 vty
->node
= AUTH_ENABLE_NODE
;
1044 /* Disable command */
1048 "Turn off privileged mode command\n")
1050 if (vty
->node
== ENABLE_NODE
)
1051 vty
->node
= VIEW_NODE
;
1055 /* Down vty node level. */
1059 "Exit current mode and down to previous mode\n")
1066 cmd_exit (struct vty
*vty
)
1072 if (vty_shell (vty
))
1075 vty
->status
= VTY_CLOSE
;
1078 vty
->node
= ENABLE_NODE
;
1079 vty_config_unlock (vty
);
1081 case INTERFACE_NODE
:
1091 case LDP_L2VPN_NODE
:
1098 vty
->node
= CONFIG_NODE
;
1101 case BGP_IPV4M_NODE
:
1102 case BGP_VPNV4_NODE
:
1103 case BGP_VPNV6_NODE
:
1104 case BGP_ENCAP_NODE
:
1105 case BGP_ENCAPV6_NODE
:
1106 case BGP_VNC_DEFAULTS_NODE
:
1107 case BGP_VNC_NVE_GROUP_NODE
:
1108 case BGP_VNC_L2_GROUP_NODE
:
1110 case BGP_IPV6M_NODE
:
1111 vty
->node
= BGP_NODE
;
1115 vty
->node
= LDP_NODE
;
1117 case LDP_IPV4_IFACE_NODE
:
1118 vty
->node
= LDP_IPV4_NODE
;
1120 case LDP_IPV6_IFACE_NODE
:
1121 vty
->node
= LDP_IPV6_NODE
;
1123 case LDP_PSEUDOWIRE_NODE
:
1124 vty
->node
= LDP_L2VPN_NODE
;
1126 case KEYCHAIN_KEY_NODE
:
1127 vty
->node
= KEYCHAIN_NODE
;
1129 case LINK_PARAMS_NODE
:
1130 vty
->node
= INTERFACE_NODE
;
1141 "Exit current mode and down to previous mode\n")
1143 return config_exit (self
, vty
, argc
, argv
);
1147 /* End of configuration. */
1151 "End current mode and change to enable mode.")
1157 /* Nothing to do. */
1160 case INTERFACE_NODE
:
1167 case BGP_ENCAP_NODE
:
1168 case BGP_ENCAPV6_NODE
:
1169 case BGP_VNC_DEFAULTS_NODE
:
1170 case BGP_VNC_NVE_GROUP_NODE
:
1171 case BGP_VNC_L2_GROUP_NODE
:
1172 case BGP_VPNV4_NODE
:
1173 case BGP_VPNV6_NODE
:
1175 case BGP_IPV4M_NODE
:
1177 case BGP_IPV6M_NODE
:
1184 case LDP_IPV4_IFACE_NODE
:
1185 case LDP_IPV6_IFACE_NODE
:
1186 case LDP_L2VPN_NODE
:
1187 case LDP_PSEUDOWIRE_NODE
:
1190 case KEYCHAIN_KEY_NODE
:
1194 case LINK_PARAMS_NODE
:
1195 vty_config_unlock (vty
);
1196 vty
->node
= ENABLE_NODE
;
1205 DEFUN (show_version
,
1209 "Displays zebra version\n")
1211 vty_out (vty
, "%s %s (%s).%s", FRR_FULL_NAME
, FRR_VERSION
,
1212 host
.name
? host
.name
: "",
1214 vty_out (vty
, "%s%s%s", FRR_COPYRIGHT
, GIT_INFO
, VTY_NEWLINE
);
1215 vty_out (vty
, "configured with:%s %s%s", VTY_NEWLINE
,
1216 FRR_CONFIG_ARGS
, VTY_NEWLINE
);
1221 /* Help display function for all node. */
1225 "Description of the interactive help system\n")
1228 "Quagga VTY provides advanced help feature. When you need help,%s\
1229 anytime at the command line please press '?'.%s\
1231 If nothing matches, the help list will be empty and you must backup%s\
1232 until entering a '?' shows the available options.%s\
1233 Two styles of help are provided:%s\
1234 1. Full help is available when you are ready to enter a%s\
1235 command argument (e.g. 'show ?') and describes each possible%s\
1237 2. Partial help is provided when an abbreviated argument is entered%s\
1238 and you want to know what arguments match the input%s\
1239 (e.g. 'show me?'.)%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
,
1240 VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
,
1241 VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1246 permute (struct graph_node
*start
, struct vty
*vty
)
1248 static struct list
*position
= NULL
;
1249 if (!position
) position
= list_new ();
1252 listnode_add (position
, start
);
1253 for (unsigned int i
= 0; i
< vector_active (start
->to
); i
++)
1255 struct graph_node
*gn
= vector_slot (start
->to
, i
);
1256 struct cmd_token
*tok
= gn
->data
;
1257 if (tok
->attr
== CMD_ATTR_HIDDEN
||
1258 tok
->attr
== CMD_ATTR_DEPRECATED
)
1260 else if (tok
->type
== END_TKN
|| gn
== start
)
1262 struct graph_node
*gnn
;
1263 struct listnode
*ln
;
1265 for (ALL_LIST_ELEMENTS_RO (position
,ln
,gnn
))
1267 struct cmd_token
*tt
= gnn
->data
;
1268 if (tt
->type
< SELECTOR_TKN
)
1269 vty_out (vty
, " %s", tt
->text
);
1272 vty_out (vty
, "...");
1273 vty_out (vty
, VTY_NEWLINE
);
1278 list_delete_node (position
, listtail(position
));
1282 cmd_list_cmds (struct vty
*vty
, int do_permute
)
1284 struct cmd_node
*node
= vector_slot (cmdvec
, vty
->node
);
1287 permute (vector_slot (node
->cmdgraph
->nodes
, 0), vty
);
1290 /* loop over all commands at this node */
1291 struct cmd_element
*element
= NULL
;
1292 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
); i
++)
1293 if ((element
= vector_slot (node
->cmd_vector
, i
)) &&
1294 element
->attr
!= CMD_ATTR_DEPRECATED
&&
1295 element
->attr
!= CMD_ATTR_HIDDEN
)
1296 vty_out (vty
, " %s%s", element
->string
, VTY_NEWLINE
);
1301 /* Help display function for all node. */
1304 "list [permutations]",
1305 "Print command list\n"
1306 "Print all possible command permutations\n")
1308 return cmd_list_cmds (vty
, argc
== 2);
1311 DEFUN (show_commandtree
,
1312 show_commandtree_cmd
,
1313 "show commandtree [permutations]",
1315 "Show command tree\n")
1317 return cmd_list_cmds (vty
, argc
== 3);
1320 /* Write current configuration into file. */
1322 DEFUN (config_write
,
1324 "write [<file|memory|terminal>]",
1325 "Write running configuration to memory, network, or terminal\n"
1326 "Write to configuration file\n"
1327 "Write configuration currently in memory\n"
1328 "Write configuration to terminal\n")
1333 struct cmd_node
*node
;
1335 char *config_file_tmp
= NULL
;
1336 char *config_file_sav
= NULL
;
1337 int ret
= CMD_WARNING
;
1338 struct vty
*file_vty
;
1339 struct stat conf_stat
;
1341 // if command was 'write terminal' or 'show running-config'
1342 if (argc
== 2 && (!strcmp(argv
[idx_type
]->text
, "terminal") ||
1343 !strcmp(argv
[0]->text
, "show")))
1345 if (vty
->type
== VTY_SHELL_SERV
)
1347 for (i
= 0; i
< vector_active (cmdvec
); i
++)
1348 if ((node
= vector_slot (cmdvec
, i
)) && node
->func
&& node
->vtysh
)
1350 if ((*node
->func
) (vty
))
1351 vty_out (vty
, "!%s", VTY_NEWLINE
);
1356 vty_out (vty
, "%sCurrent configuration:%s", VTY_NEWLINE
,
1358 vty_out (vty
, "!%s", VTY_NEWLINE
);
1360 for (i
= 0; i
< vector_active (cmdvec
); i
++)
1361 if ((node
= vector_slot (cmdvec
, i
)) && node
->func
)
1363 if ((*node
->func
) (vty
))
1364 vty_out (vty
, "!%s", VTY_NEWLINE
);
1366 vty_out (vty
, "end%s",VTY_NEWLINE
);
1374 /* Check and see if we are operating under vtysh configuration */
1375 if (host
.config
== NULL
)
1377 vty_out (vty
, "Can't save to configuration file, using vtysh.%s",
1383 config_file
= host
.config
;
1386 XMALLOC (MTYPE_TMP
, strlen (config_file
) + strlen (CONF_BACKUP_EXT
) + 1);
1387 strcpy (config_file_sav
, config_file
);
1388 strcat (config_file_sav
, CONF_BACKUP_EXT
);
1391 config_file_tmp
= XMALLOC (MTYPE_TMP
, strlen (config_file
) + 8);
1392 sprintf (config_file_tmp
, "%s.XXXXXX", config_file
);
1394 /* Open file to configuration write. */
1395 fd
= mkstemp (config_file_tmp
);
1398 vty_out (vty
, "Can't open configuration file %s.%s", config_file_tmp
,
1403 /* Make vty for configuration file. */
1404 file_vty
= vty_new ();
1406 file_vty
->type
= VTY_FILE
;
1408 /* Config file header print. */
1409 vty_out (file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1410 vty_time_print (file_vty
, 1);
1411 vty_out (file_vty
, "!\n");
1413 for (i
= 0; i
< vector_active (cmdvec
); i
++)
1414 if ((node
= vector_slot (cmdvec
, i
)) && node
->func
)
1416 if ((*node
->func
) (file_vty
))
1417 vty_out (file_vty
, "!\n");
1419 vty_close (file_vty
);
1421 if (stat(config_file
, &conf_stat
) >= 0)
1423 if (unlink (config_file_sav
) != 0)
1424 if (errno
!= ENOENT
)
1426 vty_out (vty
, "Can't unlink backup configuration file %s.%s", config_file_sav
,
1430 if (link (config_file
, config_file_sav
) != 0)
1432 vty_out (vty
, "Can't backup old configuration file %s.%s", config_file_sav
,
1437 if (unlink (config_file
) != 0)
1439 vty_out (vty
, "Can't unlink configuration file %s.%s", config_file
,
1444 if (link (config_file_tmp
, config_file
) != 0)
1446 vty_out (vty
, "Can't save configuration file %s.%s", config_file
,
1452 if (chmod (config_file
, CONFIGFILE_MASK
) != 0)
1454 vty_out (vty
, "Can't chmod configuration file %s: %s (%d).%s",
1455 config_file
, safe_strerror(errno
), errno
, VTY_NEWLINE
);
1459 vty_out (vty
, "Configuration saved to %s%s", config_file
,
1464 unlink (config_file_tmp
);
1465 XFREE (MTYPE_TMP
, config_file_tmp
);
1466 XFREE (MTYPE_TMP
, config_file_sav
);
1470 /* ALIAS_FIXME for 'write <terminal|memory>' */
1471 DEFUN (show_running_config
,
1472 show_running_config_cmd
,
1473 "show running-config",
1475 "running configuration (same as write terminal/memory)\n")
1477 return config_write (self
, vty
, argc
, argv
);
1480 /* ALIAS_FIXME for 'write file' */
1481 DEFUN (copy_runningconf_startupconf
,
1482 copy_runningconf_startupconf_cmd
,
1483 "copy running-config startup-config",
1484 "Copy configuration\n"
1485 "Copy running config to... \n"
1486 "Copy running config to startup config (same as write file)\n")
1488 return config_write (self
, vty
, argc
, argv
);
1492 /* Write startup configuration into the terminal. */
1493 DEFUN (show_startup_config
,
1494 show_startup_config_cmd
,
1495 "show startup-config",
1497 "Contents of startup configuration\n")
1504 if (host
.config
== NULL
)
1507 confp
= fopen (host
.config
, "r");
1510 vty_out (vty
, "Can't open configuration file [%s] due to '%s'%s",
1511 host
.config
, safe_strerror(errno
), VTY_NEWLINE
);
1515 while (fgets (buf
, BUFSIZ
, confp
))
1519 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1523 vty_out (vty
, "%s%s", buf
, VTY_NEWLINE
);
1531 /* Hostname configuration */
1532 DEFUN (config_hostname
,
1535 "Set system's network name\n"
1536 "This system's network name\n")
1538 struct cmd_token
*word
= argv
[1];
1540 if (!isalpha((int) word
->arg
[0]))
1542 vty_out (vty
, "Please specify string starting with alphabet%s", VTY_NEWLINE
);
1547 XFREE (MTYPE_HOST
, host
.name
);
1549 host
.name
= XSTRDUP (MTYPE_HOST
, word
->arg
);
1553 DEFUN (config_no_hostname
,
1555 "no hostname [HOSTNAME]",
1557 "Reset system's network name\n"
1558 "Host name of this router\n")
1561 XFREE (MTYPE_HOST
, host
.name
);
1566 /* VTY interface password set. */
1567 DEFUN (config_password
,
1569 "password [(8-8)] WORD",
1570 "Assign the terminal connection password\n"
1571 "Specifies a HIDDEN password will follow\n"
1572 "The password string\n")
1576 if (argc
== 3) // '8' was specified
1579 XFREE (MTYPE_HOST
, host
.password
);
1580 host
.password
= NULL
;
1581 if (host
.password_encrypt
)
1582 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1583 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, argv
[idx_word
]->arg
);
1587 if (!isalnum (argv
[idx_8
]->arg
[0]))
1590 "Please specify string starting with alphanumeric%s", VTY_NEWLINE
);
1595 XFREE (MTYPE_HOST
, host
.password
);
1596 host
.password
= NULL
;
1600 if (host
.password_encrypt
)
1601 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1602 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (argv
[idx_8
]->arg
));
1605 host
.password
= XSTRDUP (MTYPE_HOST
, argv
[idx_8
]->arg
);
1610 /* VTY enable password set. */
1611 DEFUN (config_enable_password
,
1612 enable_password_cmd
,
1613 "enable password [(8-8)] WORD",
1614 "Modify enable password parameters\n"
1615 "Assign the privileged level password\n"
1616 "Specifies a HIDDEN password will follow\n"
1617 "The HIDDEN 'enable' password string\n")
1622 /* Crypt type is specified. */
1625 if (argv
[idx_8
]->arg
[0] == '8')
1628 XFREE (MTYPE_HOST
, host
.enable
);
1631 if (host
.enable_encrypt
)
1632 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1633 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, argv
[idx_word
]->arg
);
1639 vty_out (vty
, "Unknown encryption type.%s", VTY_NEWLINE
);
1644 if (!isalnum (argv
[idx_8
]->arg
[0]))
1647 "Please specify string starting with alphanumeric%s", VTY_NEWLINE
);
1652 XFREE (MTYPE_HOST
, host
.enable
);
1655 /* Plain password input. */
1658 if (host
.enable_encrypt
)
1659 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1660 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (argv
[idx_8
]->arg
));
1663 host
.enable
= XSTRDUP (MTYPE_HOST
, argv
[idx_8
]->arg
);
1668 /* VTY enable password delete. */
1669 DEFUN (no_config_enable_password
,
1670 no_enable_password_cmd
,
1671 "no enable password",
1673 "Modify enable password parameters\n"
1674 "Assign the privileged level password\n")
1677 XFREE (MTYPE_HOST
, host
.enable
);
1680 if (host
.enable_encrypt
)
1681 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1682 host
.enable_encrypt
= NULL
;
1687 DEFUN (service_password_encrypt
,
1688 service_password_encrypt_cmd
,
1689 "service password-encryption",
1690 "Set up miscellaneous service\n"
1691 "Enable encrypted passwords\n")
1700 if (host
.password_encrypt
)
1701 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1702 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (host
.password
));
1706 if (host
.enable_encrypt
)
1707 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1708 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (host
.enable
));
1714 DEFUN (no_service_password_encrypt
,
1715 no_service_password_encrypt_cmd
,
1716 "no service password-encryption",
1718 "Set up miscellaneous service\n"
1719 "Enable encrypted passwords\n")
1726 if (host
.password_encrypt
)
1727 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1728 host
.password_encrypt
= NULL
;
1730 if (host
.enable_encrypt
)
1731 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1732 host
.enable_encrypt
= NULL
;
1737 DEFUN (config_terminal_length
,
1738 config_terminal_length_cmd
,
1739 "terminal length (0-512)",
1740 "Set terminal line parameters\n"
1741 "Set number of lines on a screen\n"
1742 "Number of lines on screen (0 for no pausing)\n")
1746 char *endptr
= NULL
;
1748 lines
= strtol (argv
[idx_number
]->arg
, &endptr
, 10);
1749 if (lines
< 0 || lines
> 512 || *endptr
!= '\0')
1751 vty_out (vty
, "length is malformed%s", VTY_NEWLINE
);
1759 DEFUN (config_terminal_no_length
,
1760 config_terminal_no_length_cmd
,
1761 "terminal no length",
1762 "Set terminal line parameters\n"
1764 "Set number of lines on a screen\n")
1770 DEFUN (service_terminal_length
,
1771 service_terminal_length_cmd
,
1772 "service terminal-length (0-512)",
1773 "Set up miscellaneous service\n"
1774 "System wide terminal length configuration\n"
1775 "Number of lines of VTY (0 means no line control)\n")
1779 char *endptr
= NULL
;
1781 lines
= strtol (argv
[idx_number
]->arg
, &endptr
, 10);
1782 if (lines
< 0 || lines
> 512 || *endptr
!= '\0')
1784 vty_out (vty
, "length is malformed%s", VTY_NEWLINE
);
1792 DEFUN (no_service_terminal_length
,
1793 no_service_terminal_length_cmd
,
1794 "no service terminal-length [(0-512)]",
1796 "Set up miscellaneous service\n"
1797 "System wide terminal length configuration\n"
1798 "Number of lines of VTY (0 means no line control)\n")
1804 DEFUN_HIDDEN (do_echo
,
1807 "Echo a message back to the vty\n"
1808 "The message to echo\n")
1812 vty_out (vty
, "%s%s", ((message
= argv_concat (argv
, argc
, 1)) ? message
: ""),
1815 XFREE(MTYPE_TMP
, message
);
1819 DEFUN (config_logmsg
,
1821 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
1822 "Send a message to enabled logging destinations\n"
1824 "The message to send\n")
1826 int idx_log_level
= 1;
1827 int idx_message
= 2;
1831 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
1832 return CMD_ERR_NO_MATCH
;
1834 zlog(NULL
, level
, "%s", ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
1836 XFREE(MTYPE_TMP
, message
);
1841 DEFUN (show_logging
,
1845 "Show current logging configuration\n")
1847 struct zlog
*zl
= zlog_default
;
1849 vty_out (vty
, "Syslog logging: ");
1850 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
1851 vty_out (vty
, "disabled");
1853 vty_out (vty
, "level %s, facility %s, ident %s",
1854 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
1855 facility_name(zl
->facility
), zl
->ident
);
1856 vty_out (vty
, "%s", VTY_NEWLINE
);
1858 vty_out (vty
, "Stdout logging: ");
1859 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
1860 vty_out (vty
, "disabled");
1862 vty_out (vty
, "level %s",
1863 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
1864 vty_out (vty
, "%s", VTY_NEWLINE
);
1866 vty_out (vty
, "Monitor logging: ");
1867 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
1868 vty_out (vty
, "disabled");
1870 vty_out (vty
, "level %s",
1871 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
1872 vty_out (vty
, "%s", VTY_NEWLINE
);
1874 vty_out (vty
, "File logging: ");
1875 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) ||
1877 vty_out (vty
, "disabled");
1879 vty_out (vty
, "level %s, filename %s",
1880 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
1882 vty_out (vty
, "%s", VTY_NEWLINE
);
1884 vty_out (vty
, "Protocol name: %s%s",
1885 zlog_proto_names
[zl
->protocol
], VTY_NEWLINE
);
1886 vty_out (vty
, "Record priority: %s%s",
1887 (zl
->record_priority
? "enabled" : "disabled"), VTY_NEWLINE
);
1888 vty_out (vty
, "Timestamp precision: %d%s",
1889 zl
->timestamp_precision
, VTY_NEWLINE
);
1894 DEFUN (config_log_stdout
,
1895 config_log_stdout_cmd
,
1896 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
1898 "Set stdout logging level\n"
1901 int idx_log_level
= 2;
1903 if (argc
== idx_log_level
)
1905 zlog_set_level (NULL
, ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
1910 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
1911 return CMD_ERR_NO_MATCH
;
1912 zlog_set_level (NULL
, ZLOG_DEST_STDOUT
, level
);
1916 DEFUN (no_config_log_stdout
,
1917 no_config_log_stdout_cmd
,
1918 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
1921 "Cancel logging to stdout\n"
1924 zlog_set_level (NULL
, ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
1928 DEFUN (config_log_monitor
,
1929 config_log_monitor_cmd
,
1930 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
1932 "Set terminal line (monitor) logging level\n"
1935 int idx_log_level
= 2;
1937 if (argc
== idx_log_level
)
1939 zlog_set_level (NULL
, ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
1944 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
1945 return CMD_ERR_NO_MATCH
;
1946 zlog_set_level (NULL
, ZLOG_DEST_MONITOR
, level
);
1950 DEFUN (no_config_log_monitor
,
1951 no_config_log_monitor_cmd
,
1952 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
1955 "Disable terminal line (monitor) logging\n"
1958 zlog_set_level (NULL
, ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
1963 set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
1967 const char *fullpath
;
1969 /* Path detection. */
1970 if (! IS_DIRECTORY_SEP (*fname
))
1972 char cwd
[MAXPATHLEN
+1];
1973 cwd
[MAXPATHLEN
] = '\0';
1975 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
1977 zlog_err ("config_log_file: Unable to alloc mem!");
1981 if ( (p
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + strlen (fname
) + 2))
1984 zlog_err ("config_log_file: Unable to alloc mem!");
1987 sprintf (p
, "%s/%s", cwd
, fname
);
1993 ret
= zlog_set_file (NULL
, fullpath
, loglevel
);
1996 XFREE (MTYPE_TMP
, p
);
2000 vty_out (vty
, "can't open logfile %s\n", fname
);
2005 XFREE (MTYPE_HOST
, host
.logfile
);
2007 host
.logfile
= XSTRDUP (MTYPE_HOST
, fname
);
2009 #if defined(HAVE_CUMULUS)
2010 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2011 zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] = ZLOG_DISABLED
;
2016 DEFUN (config_log_file
,
2017 config_log_file_cmd
,
2018 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2021 "Logging filename\n"
2024 int idx_filename
= 2;
2025 int idx_log_levels
= 3;
2029 if ((level
= level_match(argv
[idx_log_levels
]->arg
)) == ZLOG_DISABLED
)
2030 return CMD_ERR_NO_MATCH
;
2031 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2034 return set_log_file(vty
, argv
[idx_filename
]->arg
, zlog_default
->default_lvl
);
2037 DEFUN (no_config_log_file
,
2038 no_config_log_file_cmd
,
2039 "no log file [FILENAME [LEVEL]]",
2042 "Cancel logging to file\n"
2043 "Logging file name\n"
2044 "Logging file name\n"
2047 zlog_reset_file (NULL
);
2050 XFREE (MTYPE_HOST
, host
.logfile
);
2052 host
.logfile
= NULL
;
2057 DEFUN (config_log_syslog
,
2058 config_log_syslog_cmd
,
2059 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2061 "Set syslog logging level\n"
2064 int idx_log_levels
= 2;
2068 if ((level
= level_match (argv
[idx_log_levels
]->arg
)) == ZLOG_DISABLED
)
2069 return CMD_ERR_NO_MATCH
;
2070 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, level
);
2075 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2080 DEFUN (no_config_log_syslog
,
2081 no_config_log_syslog_cmd
,
2082 "no log syslog [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>] [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2085 "Cancel logging to syslog\n"
2089 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2093 DEFUN (config_log_facility
,
2094 config_log_facility_cmd
,
2095 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2097 "Facility parameter for syslog messages\n"
2101 int facility
= facility_match(argv
[idx_target
]->arg
);
2103 zlog_default
->facility
= facility
;
2107 DEFUN (no_config_log_facility
,
2108 no_config_log_facility_cmd
,
2109 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2112 "Reset syslog facility to default (daemon)\n"
2115 zlog_default
->facility
= LOG_DAEMON
;
2119 DEFUN_DEPRECATED (config_log_trap
,
2120 config_log_trap_cmd
,
2121 "log trap <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>",
2123 "(Deprecated) Set logging level and default for all destinations\n"
2129 if ((new_level
= level_match(argv
[2]->arg
)) == ZLOG_DISABLED
)
2130 return CMD_ERR_NO_MATCH
;
2132 zlog_default
->default_lvl
= new_level
;
2133 for (i
= 0; i
< ZLOG_NUM_DESTS
; i
++)
2134 if (zlog_default
->maxlvl
[i
] != ZLOG_DISABLED
)
2135 zlog_default
->maxlvl
[i
] = new_level
;
2139 DEFUN_DEPRECATED (no_config_log_trap
,
2140 no_config_log_trap_cmd
,
2141 "no log trap [emergencies|alerts|critical|errors|warnings|notifications|informational|debugging]",
2144 "Permit all logging information\n"
2147 zlog_default
->default_lvl
= LOG_DEBUG
;
2151 DEFUN (config_log_record_priority
,
2152 config_log_record_priority_cmd
,
2153 "log record-priority",
2155 "Log the priority of the message within the message\n")
2157 zlog_default
->record_priority
= 1 ;
2161 DEFUN (no_config_log_record_priority
,
2162 no_config_log_record_priority_cmd
,
2163 "no log record-priority",
2166 "Do not log the priority of the message within the message\n")
2168 zlog_default
->record_priority
= 0 ;
2172 DEFUN (config_log_timestamp_precision
,
2173 config_log_timestamp_precision_cmd
,
2174 "log timestamp precision (0-6)",
2176 "Timestamp configuration\n"
2177 "Set the timestamp precision\n"
2178 "Number of subsecond digits\n")
2181 VTY_GET_INTEGER_RANGE("Timestamp Precision",
2182 zlog_default
->timestamp_precision
, argv
[idx_number
]->arg
, 0, 6);
2186 DEFUN (no_config_log_timestamp_precision
,
2187 no_config_log_timestamp_precision_cmd
,
2188 "no log timestamp precision",
2191 "Timestamp configuration\n"
2192 "Reset the timestamp precision to the default value of 0\n")
2194 zlog_default
->timestamp_precision
= 0 ;
2199 cmd_banner_motd_file (const char *file
)
2201 int success
= CMD_SUCCESS
;
2206 rpath
= realpath (file
, p
);
2208 return CMD_ERR_NO_FILE
;
2209 in
= strstr (rpath
, SYSCONFDIR
);
2213 XFREE (MTYPE_HOST
, host
.motdfile
);
2214 host
.motdfile
= XSTRDUP (MTYPE_HOST
, file
);
2217 success
= CMD_WARNING
;
2222 DEFUN (banner_motd_file
,
2223 banner_motd_file_cmd
,
2224 "banner motd file FILE",
2227 "Banner from a file\n"
2231 const char *filename
= argv
[idx_file
]->arg
;
2232 int cmd
= cmd_banner_motd_file (filename
);
2234 if (cmd
== CMD_ERR_NO_FILE
)
2235 vty_out (vty
, "%s does not exist", filename
);
2236 else if (cmd
== CMD_WARNING
)
2237 vty_out (vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2242 DEFUN (banner_motd_default
,
2243 banner_motd_default_cmd
,
2244 "banner motd default",
2245 "Set banner string\n"
2246 "Strings for motd\n"
2249 host
.motd
= default_motd
;
2253 DEFUN (no_banner_motd
,
2257 "Set banner string\n"
2258 "Strings for motd\n")
2262 XFREE (MTYPE_HOST
, host
.motdfile
);
2263 host
.motdfile
= NULL
;
2267 /* Set config filename. Called from vty.c */
2269 host_config_set (const char *filename
)
2272 XFREE (MTYPE_HOST
, host
.config
);
2273 host
.config
= XSTRDUP (MTYPE_HOST
, filename
);
2277 host_config_get (void)
2283 install_default (enum node_type node
)
2285 install_element (node
, &config_exit_cmd
);
2286 install_element (node
, &config_quit_cmd
);
2287 install_element (node
, &config_end_cmd
);
2288 install_element (node
, &config_help_cmd
);
2289 install_element (node
, &config_list_cmd
);
2291 install_element (node
, &config_write_cmd
);
2292 install_element (node
, &show_running_config_cmd
);
2295 /* Initialize command interface. Install basic nodes and commands.
2297 * terminal = 0 -- vtysh / no logging, no config control
2298 * terminal = 1 -- normal daemon
2299 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2301 cmd_init (int terminal
)
2305 /* Allocate initial top vector of commands. */
2306 cmdvec
= vector_init (VECTOR_MIN_SIZE
);
2308 /* Default host value settings. */
2310 host
.password
= NULL
;
2312 host
.logfile
= NULL
;
2314 host
.noconfig
= (terminal
< 0);
2316 host
.motd
= default_motd
;
2317 host
.motdfile
= NULL
;
2319 /* Install top nodes. */
2320 install_node (&view_node
, NULL
);
2321 install_node (&enable_node
, NULL
);
2322 install_node (&auth_node
, NULL
);
2323 install_node (&auth_enable_node
, NULL
);
2324 install_node (&config_node
, config_write_host
);
2326 /* Each node's basic commands. */
2327 install_element (VIEW_NODE
, &show_version_cmd
);
2330 install_element (VIEW_NODE
, &config_list_cmd
);
2331 install_element (VIEW_NODE
, &config_exit_cmd
);
2332 install_element (VIEW_NODE
, &config_quit_cmd
);
2333 install_element (VIEW_NODE
, &config_help_cmd
);
2334 install_element (VIEW_NODE
, &config_enable_cmd
);
2335 install_element (VIEW_NODE
, &config_terminal_length_cmd
);
2336 install_element (VIEW_NODE
, &config_terminal_no_length_cmd
);
2337 install_element (VIEW_NODE
, &show_logging_cmd
);
2338 install_element (VIEW_NODE
, &show_commandtree_cmd
);
2339 install_element (VIEW_NODE
, &echo_cmd
);
2344 install_element (ENABLE_NODE
, &config_end_cmd
);
2345 install_element (ENABLE_NODE
, &config_disable_cmd
);
2346 install_element (ENABLE_NODE
, &config_terminal_cmd
);
2347 install_element (ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2348 install_element (ENABLE_NODE
, &config_write_cmd
);
2349 install_element (ENABLE_NODE
, &show_running_config_cmd
);
2351 install_element (ENABLE_NODE
, &show_startup_config_cmd
);
2355 install_element (ENABLE_NODE
, &config_logmsg_cmd
);
2356 install_default (CONFIG_NODE
);
2359 workqueue_cmd_init ();
2362 install_element (CONFIG_NODE
, &hostname_cmd
);
2363 install_element (CONFIG_NODE
, &no_hostname_cmd
);
2367 install_element (CONFIG_NODE
, &password_cmd
);
2368 install_element (CONFIG_NODE
, &enable_password_cmd
);
2369 install_element (CONFIG_NODE
, &no_enable_password_cmd
);
2371 install_element (CONFIG_NODE
, &config_log_stdout_cmd
);
2372 install_element (CONFIG_NODE
, &no_config_log_stdout_cmd
);
2373 install_element (CONFIG_NODE
, &config_log_monitor_cmd
);
2374 install_element (CONFIG_NODE
, &no_config_log_monitor_cmd
);
2375 install_element (CONFIG_NODE
, &config_log_file_cmd
);
2376 install_element (CONFIG_NODE
, &no_config_log_file_cmd
);
2377 install_element (CONFIG_NODE
, &config_log_syslog_cmd
);
2378 install_element (CONFIG_NODE
, &no_config_log_syslog_cmd
);
2379 install_element (CONFIG_NODE
, &config_log_facility_cmd
);
2380 install_element (CONFIG_NODE
, &no_config_log_facility_cmd
);
2381 install_element (CONFIG_NODE
, &config_log_trap_cmd
);
2382 install_element (CONFIG_NODE
, &no_config_log_trap_cmd
);
2383 install_element (CONFIG_NODE
, &config_log_record_priority_cmd
);
2384 install_element (CONFIG_NODE
, &no_config_log_record_priority_cmd
);
2385 install_element (CONFIG_NODE
, &config_log_timestamp_precision_cmd
);
2386 install_element (CONFIG_NODE
, &no_config_log_timestamp_precision_cmd
);
2387 install_element (CONFIG_NODE
, &service_password_encrypt_cmd
);
2388 install_element (CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2389 install_element (CONFIG_NODE
, &banner_motd_default_cmd
);
2390 install_element (CONFIG_NODE
, &banner_motd_file_cmd
);
2391 install_element (CONFIG_NODE
, &no_banner_motd_cmd
);
2392 install_element (CONFIG_NODE
, &service_terminal_length_cmd
);
2393 install_element (CONFIG_NODE
, &no_service_terminal_length_cmd
);
2395 vrf_install_commands ();
2397 srandom(time(NULL
));
2401 new_cmd_token (enum cmd_token_type type
, u_char attr
, char *text
, char *desc
)
2403 struct cmd_token
*token
= XMALLOC (MTYPE_CMD_TOKENS
, sizeof (struct cmd_token
));
2414 del_cmd_token (struct cmd_token
*token
)
2419 XFREE (MTYPE_CMD_TOKENS
, token
->text
);
2421 XFREE (MTYPE_CMD_TOKENS
, token
->desc
);
2423 XFREE (MTYPE_CMD_TOKENS
, token
->arg
);
2425 XFREE (MTYPE_CMD_TOKENS
, token
);
2429 copy_cmd_token (struct cmd_token
*token
)
2431 struct cmd_token
*copy
= new_cmd_token (token
->type
, token
->attr
, NULL
, NULL
);
2432 copy
->max
= token
->max
;
2433 copy
->min
= token
->min
;
2434 copy
->text
= token
->text
? XSTRDUP (MTYPE_CMD_TOKENS
, token
->text
) : NULL
;
2435 copy
->desc
= token
->desc
? XSTRDUP (MTYPE_CMD_TOKENS
, token
->desc
) : NULL
;
2436 copy
->arg
= token
->arg
? XSTRDUP (MTYPE_CMD_TOKENS
, token
->arg
) : NULL
;
2444 struct cmd_node
*cmd_node
;
2448 for (unsigned int i
= 0; i
< vector_active (cmdvec
); i
++)
2449 if ((cmd_node
= vector_slot (cmdvec
, i
)) != NULL
)
2451 // deleting the graph delets the cmd_element as well
2452 graph_delete_graph (cmd_node
->cmdgraph
);
2453 vector_free (cmd_node
->cmd_vector
);
2454 hash_clean (cmd_node
->cmd_hash
, NULL
);
2455 hash_free (cmd_node
->cmd_hash
);
2456 cmd_node
->cmd_hash
= NULL
;
2459 vector_free (cmdvec
);
2464 XFREE (MTYPE_HOST
, host
.name
);
2466 XFREE (MTYPE_HOST
, host
.password
);
2467 if (host
.password_encrypt
)
2468 XFREE (MTYPE_HOST
, host
.password_encrypt
);
2470 XFREE (MTYPE_HOST
, host
.enable
);
2471 if (host
.enable_encrypt
)
2472 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
2474 XFREE (MTYPE_HOST
, host
.logfile
);
2476 XFREE (MTYPE_HOST
, host
.motdfile
);
2478 XFREE (MTYPE_HOST
, host
.config
);