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 along
23 * with this program; see the file COPYING; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <lib/version.h>
39 #include "workqueue.h"
41 #include "command_match.h"
42 #include "command_graph.h"
46 DEFINE_MTYPE( LIB
, HOST
, "Host config")
47 DEFINE_MTYPE( LIB
, STRVEC
, "String vector")
48 DEFINE_MTYPE( LIB
, COMPLETION
, "Completion item")
50 /* Command vector which includes some level of command lists. Normally
51 each daemon maintains each own cmdvec. */
54 /* Host information structure. */
57 /* Standard command node structures. */
58 static struct cmd_node auth_node
=
64 static struct cmd_node view_node
=
70 static struct cmd_node auth_enable_node
=
76 static struct cmd_node enable_node
=
82 static struct cmd_node config_node
=
89 /* Default motd string. */
90 static const char *default_motd
= FRR_DEFAULT_MOTD
;
92 static const struct facility_map
{
96 } syslog_facilities
[] =
98 { LOG_KERN
, "kern", 1 },
99 { LOG_USER
, "user", 2 },
100 { LOG_MAIL
, "mail", 1 },
101 { LOG_DAEMON
, "daemon", 1 },
102 { LOG_AUTH
, "auth", 1 },
103 { LOG_SYSLOG
, "syslog", 1 },
104 { LOG_LPR
, "lpr", 2 },
105 { LOG_NEWS
, "news", 1 },
106 { LOG_UUCP
, "uucp", 2 },
107 { LOG_CRON
, "cron", 1 },
109 { LOG_FTP
, "ftp", 1 },
111 { LOG_LOCAL0
, "local0", 6 },
112 { LOG_LOCAL1
, "local1", 6 },
113 { LOG_LOCAL2
, "local2", 6 },
114 { LOG_LOCAL3
, "local3", 6 },
115 { LOG_LOCAL4
, "local4", 6 },
116 { LOG_LOCAL5
, "local5", 6 },
117 { LOG_LOCAL6
, "local6", 6 },
118 { LOG_LOCAL7
, "local7", 6 },
123 facility_name(int facility
)
125 const struct facility_map
*fm
;
127 for (fm
= syslog_facilities
; fm
->name
; fm
++)
128 if (fm
->facility
== facility
)
134 facility_match(const char *str
)
136 const struct facility_map
*fm
;
138 for (fm
= syslog_facilities
; fm
->name
; fm
++)
139 if (!strncmp(str
,fm
->name
,fm
->match
))
145 level_match(const char *s
)
149 for ( level
= 0 ; zlog_priority
[level
] != NULL
; level
++ )
150 if (!strncmp (s
, zlog_priority
[level
], 2))
152 return ZLOG_DISABLED
;
155 /* This is called from main when a daemon is invoked with -v or --version. */
157 print_version (const char *progname
)
159 printf ("%s version %s\n", progname
, FRR_VERSION
);
160 printf ("%s\n", FRR_COPYRIGHT
);
161 printf ("configured with:\n\t%s\n", FRR_CONFIG_ARGS
);
165 /* Utility function to concatenate argv argument into a single string
166 with inserting ' ' character between each argument. */
168 argv_concat (struct cmd_token
**argv
, int argc
, int shift
)
176 for (i
= shift
; i
< argc
; i
++)
177 len
+= strlen(argv
[i
]->arg
)+1;
180 p
= str
= XMALLOC(MTYPE_TMP
, len
);
181 for (i
= shift
; i
< argc
; i
++)
184 memcpy(p
, argv
[i
]->arg
, (arglen
= strlen(argv
[i
]->arg
)));
193 * Convenience function for accessing argv data.
197 * @param text definition snippet of the desired token
198 * @param index the starting index, and where to store the
199 * index of the found token if it exists
200 * @return 1 if found, 0 otherwise
203 argv_find (struct cmd_token
**argv
, int argc
, const char *text
, int *index
)
206 for (int i
= *index
; i
< argc
&& found
== 0; i
++)
207 if ((found
= strmatch (text
, argv
[i
]->text
)))
213 cmd_hash_key (void *p
)
215 return (uintptr_t) p
;
219 cmd_hash_cmp (const void *a
, const void *b
)
224 /* Install top node of command vector. */
226 install_node (struct cmd_node
*node
,
227 int (*func
) (struct vty
*))
229 vector_set_index (cmdvec
, node
->node
, node
);
231 node
->cmdgraph
= graph_new ();
232 node
->cmd_vector
= vector_init (VECTOR_MIN_SIZE
);
234 struct cmd_token
*token
= cmd_token_new (START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
235 graph_new_node (node
->cmdgraph
, token
, (void (*)(void *)) &cmd_token_del
);
236 node
->cmd_hash
= hash_create (cmd_hash_key
, cmd_hash_cmp
);
240 * Tokenizes a string, storing tokens in a vector.
241 * Whitespace is ignored.
243 * Delimiter string = " \n\r\t".
245 * @param string to tokenize
246 * @return tokenized string
249 cmd_make_strvec (const char *string
)
251 if (!string
) return NULL
;
253 char *copy
, *copystart
;
254 copystart
= copy
= XSTRDUP (MTYPE_TMP
, string
);
256 // skip leading whitespace
257 while (isspace ((int) *copy
) && *copy
!= '\0') copy
++;
259 // if the entire string was whitespace or a comment, return
260 if (*copy
== '\0' || *copy
== '!' || *copy
== '#')
262 XFREE (MTYPE_TMP
, copystart
);
266 vector strvec
= vector_init (VECTOR_MIN_SIZE
);
267 const char *delim
= " \n\r\t", *tok
= NULL
;
270 tok
= strsep (©
, delim
);
272 vector_set (strvec
, XSTRDUP (MTYPE_STRVEC
, tok
));
275 XFREE (MTYPE_TMP
, copystart
);
279 /* Free allocated string vector. */
281 cmd_free_strvec (vector v
)
289 for (i
= 0; i
< vector_active (v
); i
++)
290 if ((cp
= vector_slot (v
, i
)) != NULL
)
291 XFREE (MTYPE_STRVEC
, cp
);
296 /* Return prompt character of specified node. */
298 cmd_prompt (enum node_type node
)
300 struct cmd_node
*cnode
;
302 cnode
= vector_slot (cmdvec
, node
);
303 return cnode
->prompt
;
306 /* Install a command into a node. */
308 install_element (enum node_type ntype
, struct cmd_element
*cmd
)
310 struct cmd_node
*cnode
;
312 /* cmd_init hasn't been called */
315 fprintf (stderr
, "%s called before cmd_init, breakage likely\n",
320 cnode
= vector_slot (cmdvec
, ntype
);
324 fprintf (stderr
, "Command node %d doesn't exist, please check it\n",
326 fprintf (stderr
, "Have you called install_node before this install_element?\n");
330 if (hash_lookup (cnode
->cmd_hash
, cmd
) != NULL
)
333 "Multiple command installs to node %d of command:\n%s\n",
338 assert (hash_get (cnode
->cmd_hash
, cmd
, hash_alloc_intern
));
340 struct graph
*graph
= graph_new();
341 struct cmd_token
*token
= cmd_token_new (START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
342 graph_new_node (graph
, token
, (void (*)(void *)) &cmd_token_del
);
344 cmd_graph_parse (graph
, cmd
);
345 cmd_graph_names (graph
);
346 cmd_graph_merge (cnode
->cmdgraph
, graph
, +1);
347 graph_delete_graph (graph
);
349 vector_set (cnode
->cmd_vector
, cmd
);
351 if (ntype
== VIEW_NODE
)
352 install_element (ENABLE_NODE
, cmd
);
356 uninstall_element (enum node_type ntype
, struct cmd_element
*cmd
)
358 struct cmd_node
*cnode
;
360 /* cmd_init hasn't been called */
363 fprintf (stderr
, "%s called before cmd_init, breakage likely\n",
368 cnode
= vector_slot (cmdvec
, ntype
);
372 fprintf (stderr
, "Command node %d doesn't exist, please check it\n",
374 fprintf (stderr
, "Have you called install_node before this install_element?\n");
378 if (hash_release (cnode
->cmd_hash
, cmd
) == NULL
)
381 "Trying to uninstall non-installed command (node %d):\n%s\n",
386 vector_unset_value (cnode
->cmd_vector
, cmd
);
388 struct graph
*graph
= graph_new();
389 struct cmd_token
*token
= cmd_token_new (START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
390 graph_new_node (graph
, token
, (void (*)(void *)) &cmd_token_del
);
392 cmd_graph_parse (graph
, cmd
);
393 cmd_graph_names (graph
);
394 cmd_graph_merge (cnode
->cmdgraph
, graph
, -1);
395 graph_delete_graph (graph
);
397 if (ntype
== VIEW_NODE
)
398 uninstall_element (ENABLE_NODE
, cmd
);
402 static const unsigned char itoa64
[] =
403 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
406 to64(char *s
, long v
, int n
)
410 *s
++ = itoa64
[v
&0x3f];
416 zencrypt (const char *passwd
)
420 char *crypt (const char *, const char *);
424 to64(&salt
[0], random(), 3);
425 to64(&salt
[3], tv
.tv_usec
, 3);
428 return crypt (passwd
, salt
);
431 /* This function write configuration of this host. */
433 config_write_host (struct vty
*vty
)
436 vty_out (vty
, "hostname %s%s", host
.name
, VTY_NEWLINE
);
440 if (host
.password_encrypt
)
441 vty_out (vty
, "password 8 %s%s", host
.password_encrypt
, VTY_NEWLINE
);
442 if (host
.enable_encrypt
)
443 vty_out (vty
, "enable password 8 %s%s", host
.enable_encrypt
, VTY_NEWLINE
);
448 vty_out (vty
, "password %s%s", host
.password
, VTY_NEWLINE
);
450 vty_out (vty
, "enable password %s%s", host
.enable
, VTY_NEWLINE
);
453 if (zlog_default
->default_lvl
!= LOG_DEBUG
)
455 vty_out (vty
, "! N.B. The 'log trap' command is deprecated.%s",
457 vty_out (vty
, "log trap %s%s",
458 zlog_priority
[zlog_default
->default_lvl
], VTY_NEWLINE
);
461 if (host
.logfile
&& (zlog_default
->maxlvl
[ZLOG_DEST_FILE
] != ZLOG_DISABLED
))
463 vty_out (vty
, "log file %s", host
.logfile
);
464 if (zlog_default
->maxlvl
[ZLOG_DEST_FILE
] != zlog_default
->default_lvl
)
466 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_FILE
]]);
467 vty_out (vty
, "%s", VTY_NEWLINE
);
470 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != ZLOG_DISABLED
)
472 vty_out (vty
, "log stdout");
473 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != zlog_default
->default_lvl
)
475 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
]]);
476 vty_out (vty
, "%s", VTY_NEWLINE
);
479 if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
480 vty_out(vty
,"no log monitor%s",VTY_NEWLINE
);
481 else if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] != zlog_default
->default_lvl
)
482 vty_out(vty
,"log monitor %s%s",
483 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]],VTY_NEWLINE
);
485 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
487 vty_out (vty
, "log syslog");
488 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != zlog_default
->default_lvl
)
490 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
]]);
491 vty_out (vty
, "%s", VTY_NEWLINE
);
494 if (zlog_default
->facility
!= LOG_DAEMON
)
495 vty_out (vty
, "log facility %s%s",
496 facility_name(zlog_default
->facility
), VTY_NEWLINE
);
498 if (zlog_default
->record_priority
== 1)
499 vty_out (vty
, "log record-priority%s", VTY_NEWLINE
);
501 if (zlog_default
->timestamp_precision
> 0)
502 vty_out (vty
, "log timestamp precision %d%s",
503 zlog_default
->timestamp_precision
, VTY_NEWLINE
);
506 vty_out (vty
, "service advanced-vty%s", VTY_NEWLINE
);
509 vty_out (vty
, "service password-encryption%s", VTY_NEWLINE
);
512 vty_out (vty
, "service terminal-length %d%s", host
.lines
,
516 vty_out (vty
, "banner motd file %s%s", host
.motdfile
, VTY_NEWLINE
);
517 else if (! host
.motd
)
518 vty_out (vty
, "no banner motd%s", VTY_NEWLINE
);
523 /* Utility function for getting command graph. */
524 static struct graph
*
525 cmd_node_graph (vector v
, enum node_type ntype
)
527 struct cmd_node
*cnode
= vector_slot (v
, ntype
);
528 return cnode
->cmdgraph
;
532 cmd_try_do_shortcut (enum node_type node
, char* first_word
) {
533 if ( first_word
!= NULL
&&
536 node
!= AUTH_ENABLE_NODE
&&
537 0 == strcmp( "do", first_word
) )
543 * Compare function for cmd_token.
544 * Used with qsort to sort command completions.
547 compare_completions (const void *fst
, const void *snd
)
549 struct cmd_token
*first
= *(struct cmd_token
**) fst
,
550 *secnd
= *(struct cmd_token
**) snd
;
551 return strcmp (first
->text
, secnd
->text
);
555 * Takes a list of completions returned by command_complete,
556 * dedeuplicates them based on both text and description,
557 * sorts them, and returns them as a vector.
559 * @param completions linked list of cmd_token
560 * @return deduplicated and sorted vector with
563 completions_to_vec (struct list
*completions
)
565 vector comps
= vector_init (VECTOR_MIN_SIZE
);
568 struct cmd_token
*token
, *cr
= NULL
;
569 unsigned int i
, exists
;
570 for (ALL_LIST_ELEMENTS_RO(completions
,ln
,token
))
572 if (token
->type
== END_TKN
&& (cr
= token
))
575 // linear search for token in completions vector
577 for (i
= 0; i
< vector_active (comps
) && !exists
; i
++)
579 struct cmd_token
*curr
= vector_slot (comps
, i
);
581 exists
= !strcmp (curr
->text
, token
->text
) &&
582 !strcmp (curr
->desc
, token
->desc
);
584 exists
= !strcmp (curr
->text
, token
->text
);
585 #endif /* VTYSH_DEBUG */
589 vector_set (comps
, token
);
594 vector_active (comps
),
596 &compare_completions
);
598 // make <cr> the first element, if it is present
601 vector_set_index (comps
, vector_active (comps
), NULL
);
602 memmove (comps
->index
+ 1, comps
->index
, (comps
->alloced
- 1) * sizeof (void *));
603 vector_set_index (comps
, 0, cr
);
609 * Generates a vector of cmd_token representing possible completions
610 * on the current input.
612 * @param vline the vectorized input line
613 * @param vty the vty with the node to match on
614 * @param status pointer to matcher status code
615 * @return vector of struct cmd_token * with possible completions
618 cmd_complete_command_real (vector vline
, struct vty
*vty
, int *status
)
620 struct list
*completions
;
621 struct graph
*cmdgraph
= cmd_node_graph (cmdvec
, vty
->node
);
623 enum matcher_rv rv
= command_complete (cmdgraph
, vline
, &completions
);
625 if (MATCHER_ERROR(rv
))
627 *status
= CMD_ERR_NO_MATCH
;
631 vector comps
= completions_to_vec (completions
);
632 list_delete (completions
);
634 // set status code appropriately
635 switch (vector_active (comps
))
638 *status
= CMD_ERR_NO_MATCH
;
641 *status
= CMD_COMPLETE_FULL_MATCH
;
644 *status
= CMD_COMPLETE_LIST_MATCH
;
651 cmd_describe_command (vector vline
, struct vty
*vty
, int *status
)
655 if ( cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0) ) )
657 enum node_type onode
;
658 vector shifted_vline
;
662 vty
->node
= ENABLE_NODE
;
663 /* We can try it on enable node, cos' the vty is authenticated */
665 shifted_vline
= vector_init (vector_count(vline
));
667 for (index
= 1; index
< vector_active (vline
); index
++)
669 vector_set_index (shifted_vline
, index
-1, vector_lookup(vline
, index
));
672 ret
= cmd_complete_command_real (shifted_vline
, vty
, status
);
674 vector_free(shifted_vline
);
679 return cmd_complete_command_real (vline
, vty
, status
);
682 static struct list
*varhandlers
= NULL
;
685 cmd_variable_complete (struct cmd_token
*token
, const char *arg
, vector comps
)
688 const struct cmd_variable_handler
*cvh
;
692 tmpcomps
= arg
? vector_init (VECTOR_MIN_SIZE
) : comps
;
694 for (ALL_LIST_ELEMENTS_RO(varhandlers
, ln
, cvh
))
696 if (cvh
->tokenname
&& strcmp(cvh
->tokenname
, token
->text
))
698 if (cvh
->varname
&& (!token
->varname
|| strcmp(cvh
->varname
, token
->varname
)))
700 cvh
->completions(tmpcomps
, token
);
708 for (i
= vector_active(tmpcomps
); i
; i
--)
710 char *item
= vector_slot(tmpcomps
, i
- 1);
711 if (strlen(item
) >= argsz
712 && !strncmp(item
, arg
, argsz
))
713 vector_set(comps
, item
);
715 XFREE(MTYPE_COMPLETION
, item
);
717 vector_free(tmpcomps
);
721 cmd_variable_handler_register (const struct cmd_variable_handler
*cvh
)
726 for (; cvh
->completions
; cvh
++)
727 listnode_add(varhandlers
, (void *)cvh
);
730 DEFUN_HIDDEN (autocomplete
,
732 "autocomplete TYPE TEXT VARNAME",
733 "Autocompletion handler (internal, for vtysh)\n"
736 "cmd_token->varname\n")
738 struct cmd_token tok
;
739 vector comps
= vector_init(32);
742 memset(&tok
, 0, sizeof(tok
));
743 tok
.type
= atoi(argv
[1]->arg
);
744 tok
.text
= argv
[2]->arg
;
745 tok
.varname
= argv
[3]->arg
;
746 if (!strcmp(tok
.varname
, "-"))
749 cmd_variable_complete(&tok
, NULL
, comps
);
751 for (i
= 0; i
< vector_active(comps
); i
++)
753 char *text
= vector_slot(comps
, i
);
754 vty_out(vty
, "%s\n", text
);
755 XFREE(MTYPE_COMPLETION
, text
);
763 * Generate possible tab-completions for the given input. This function only
764 * returns results that would result in a valid command if used as Readline
765 * completions (as is the case in vtysh). For instance, if the passed vline ends
766 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
768 * @param vline vectorized input line
770 * @param status location to store matcher status code in
771 * @return set of valid strings for use with Readline as tab-completions.
775 cmd_complete_command (vector vline
, struct vty
*vty
, int *status
)
778 int original_node
= vty
->node
;
779 vector input_line
= vector_init (vector_count (vline
));
781 // if the first token is 'do' we'll want to execute the command in the enable node
782 int do_shortcut
= cmd_try_do_shortcut (vty
->node
, vector_slot (vline
, 0));
783 vty
->node
= do_shortcut
? ENABLE_NODE
: original_node
;
785 // construct the input line we'll be matching on
786 unsigned int offset
= (do_shortcut
) ? 1 : 0;
787 for (unsigned index
= 0; index
+ offset
< vector_active (vline
); index
++)
788 vector_set_index (input_line
, index
, vector_lookup (vline
, index
+ offset
));
790 // get token completions -- this is a copying operation
791 vector comps
= NULL
, initial_comps
;
792 initial_comps
= cmd_complete_command_real (input_line
, vty
, status
);
794 if (!MATCHER_ERROR (*status
))
796 assert (initial_comps
);
797 // filter out everything that is not suitable for a tab-completion
798 comps
= vector_init (VECTOR_MIN_SIZE
);
799 for (unsigned int i
= 0; i
< vector_active(initial_comps
); i
++)
801 struct cmd_token
*token
= vector_slot (initial_comps
, i
);
802 if (token
->type
== WORD_TKN
)
803 vector_set (comps
, XSTRDUP (MTYPE_COMPLETION
, token
->text
));
804 else if (IS_VARYING_TOKEN(token
->type
))
806 const char *ref
= vector_lookup(vline
, vector_active (vline
) - 1);
807 cmd_variable_complete (token
, ref
, comps
);
810 vector_free (initial_comps
);
812 // since we filtered results, we need to re-set status code
813 switch (vector_active (comps
))
816 *status
= CMD_ERR_NO_MATCH
;
819 *status
= CMD_COMPLETE_FULL_MATCH
;
822 *status
= CMD_COMPLETE_LIST_MATCH
;
825 // copy completions text into an array of char*
826 ret
= XMALLOC (MTYPE_TMP
, (vector_active (comps
)+1) * sizeof (char *));
828 for (i
= 0; i
< vector_active (comps
); i
++)
830 ret
[i
] = vector_slot (comps
, i
);
832 // set the last element to NULL, because this array is used in
833 // a Readline completion_generator function which expects NULL
834 // as a sentinel value
839 else if (initial_comps
)
840 vector_free (initial_comps
);
842 // comps should always be null here
845 // free the adjusted input line
846 vector_free (input_line
);
848 // reset vty->node to its original value
849 vty
->node
= original_node
;
854 /* return parent node */
855 /* MUST eventually converge on CONFIG_NODE */
857 node_parent ( enum node_type node
)
861 assert (node
> CONFIG_NODE
);
867 case BGP_VRF_POLICY_NODE
:
868 case BGP_VNC_DEFAULTS_NODE
:
869 case BGP_VNC_NVE_GROUP_NODE
:
870 case BGP_VNC_L2_GROUP_NODE
:
880 case KEYCHAIN_KEY_NODE
:
883 case LINK_PARAMS_NODE
:
884 ret
= INTERFACE_NODE
;
890 case LDP_IPV4_IFACE_NODE
:
893 case LDP_IPV6_IFACE_NODE
:
896 case LDP_PSEUDOWIRE_NODE
:
897 ret
= LDP_L2VPN_NODE
;
907 /* Execute command by argument vline vector. */
909 cmd_execute_command_real (vector vline
,
910 enum filter_type filter
,
912 const struct cmd_element
**cmd
)
914 struct list
*argv_list
;
915 enum matcher_rv status
;
916 const struct cmd_element
*matched_element
= NULL
;
918 struct graph
*cmdgraph
= cmd_node_graph (cmdvec
, vty
->node
);
919 status
= command_match (cmdgraph
, vline
, &argv_list
, &matched_element
);
922 *cmd
= matched_element
;
924 // if matcher error, return corresponding CMD_ERR
925 if (MATCHER_ERROR(status
))
928 list_delete (argv_list
);
931 case MATCHER_INCOMPLETE
:
932 return CMD_ERR_INCOMPLETE
;
933 case MATCHER_AMBIGUOUS
:
934 return CMD_ERR_AMBIGUOUS
;
936 return CMD_ERR_NO_MATCH
;
940 // build argv array from argv list
941 struct cmd_token
**argv
= XMALLOC (MTYPE_TMP
, argv_list
->count
* sizeof (struct cmd_token
*));
943 struct cmd_token
*token
;
945 for (ALL_LIST_ELEMENTS_RO(argv_list
,ln
,token
))
948 int argc
= argv_list
->count
;
951 if (matched_element
->daemon
)
952 ret
= CMD_SUCCESS_DAEMON
;
954 ret
= matched_element
->func (matched_element
, vty
, argc
, argv
);
956 // delete list and cmd_token's in it
957 list_delete (argv_list
);
958 XFREE (MTYPE_TMP
, argv
);
964 * Execute a given command, handling things like "do ..." and checking
965 * whether the given command might apply at a parent node if doesn't
966 * apply for the current node.
968 * @param vline Command line input, vector of char* where each element is
970 * @param vty The vty context in which the command should be executed.
971 * @param cmd Pointer where the struct cmd_element of the matched command
972 * will be stored, if any. May be set to NULL if this info is
974 * @param vtysh If set != 0, don't lookup the command at parent nodes.
975 * @return The status of the command that has been executed or an error code
976 * as to why no command could be executed.
979 cmd_execute_command (vector vline
, struct vty
*vty
,
980 const struct cmd_element
**cmd
,
983 int ret
, saved_ret
= 0;
984 enum node_type onode
, try_node
;
986 onode
= try_node
= vty
->node
;
988 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0)))
990 vector shifted_vline
;
993 vty
->node
= ENABLE_NODE
;
994 /* We can try it on enable node, cos' the vty is authenticated */
996 shifted_vline
= vector_init (vector_count(vline
));
998 for (index
= 1; index
< vector_active (vline
); index
++)
999 vector_set_index (shifted_vline
, index
-1, vector_lookup(vline
, index
));
1001 ret
= cmd_execute_command_real (shifted_vline
, FILTER_RELAXED
, vty
, cmd
);
1003 vector_free(shifted_vline
);
1008 saved_ret
= ret
= cmd_execute_command_real (vline
, FILTER_RELAXED
, vty
, cmd
);
1013 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1015 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
1016 while (vty
->node
> CONFIG_NODE
)
1018 try_node
= node_parent(try_node
);
1019 vty
->node
= try_node
;
1020 ret
= cmd_execute_command_real (vline
, FILTER_RELAXED
, vty
, cmd
);
1021 if (ret
== CMD_SUCCESS
|| ret
== CMD_WARNING
)
1024 /* no command succeeded, reset the vty to the original node */
1028 /* return command status for original node */
1033 * Execute a given command, matching it strictly against the current node.
1034 * This mode is used when reading config files.
1036 * @param vline Command line input, vector of char* where each element is
1038 * @param vty The vty context in which the command should be executed.
1039 * @param cmd Pointer where the struct cmd_element* of the matched command
1040 * will be stored, if any. May be set to NULL if this info is
1042 * @return The status of the command that has been executed or an error code
1043 * as to why no command could be executed.
1046 cmd_execute_command_strict (vector vline
, struct vty
*vty
,
1047 const struct cmd_element
**cmd
)
1049 return cmd_execute_command_real(vline
, FILTER_STRICT
, vty
, cmd
);
1053 * Parse one line of config, walking up the parse tree attempting to find a match
1055 * @param vty The vty context in which the command should be executed.
1056 * @param cmd Pointer where the struct cmd_element* of the match command
1057 * will be stored, if any. May be set to NULL if this info is
1059 * @param use_daemon Boolean to control whether or not we match on CMD_SUCCESS_DAEMON
1061 * @return The status of the command that has been executed or an error code
1062 * as to why no command could be executed.
1065 command_config_read_one_line (struct vty
*vty
, const struct cmd_element
**cmd
, int use_daemon
)
1071 vline
= cmd_make_strvec (vty
->buf
);
1073 /* In case of comment line */
1077 /* Execute configuration command : this is strict match */
1078 ret
= cmd_execute_command_strict (vline
, vty
, cmd
);
1080 // Climb the tree and try the command again at each node
1081 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1082 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1083 ret
!= CMD_SUCCESS
&&
1084 ret
!= CMD_WARNING
&&
1085 vty
->node
!= CONFIG_NODE
) {
1087 saved_node
= vty
->node
;
1089 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1090 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1091 ret
!= CMD_SUCCESS
&&
1092 ret
!= CMD_WARNING
&&
1093 vty
->node
> CONFIG_NODE
) {
1094 vty
->node
= node_parent(vty
->node
);
1095 ret
= cmd_execute_command_strict (vline
, vty
, cmd
);
1098 // If climbing the tree did not work then ignore the command and
1099 // stay at the same node
1100 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1101 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1102 ret
!= CMD_SUCCESS
&&
1105 vty
->node
= saved_node
;
1109 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1110 memcpy (vty
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
1112 cmd_free_strvec (vline
);
1117 /* Configuration make from file. */
1119 config_from_file (struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
1121 int ret
, error_ret
=0;
1124 while (fgets (vty
->buf
, VTY_BUFSIZ
, fp
))
1129 ret
= command_config_read_one_line (vty
, NULL
, 0);
1131 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
&&
1132 ret
!= CMD_ERR_NOTHING_TODO
)
1143 /* Configuration from terminal */
1144 DEFUN (config_terminal
,
1145 config_terminal_cmd
,
1146 "configure terminal",
1147 "Configuration from vty interface\n"
1148 "Configuration terminal\n")
1150 if (vty_config_lock (vty
))
1151 vty
->node
= CONFIG_NODE
;
1154 vty_out (vty
, "VTY configuration is locked by other VTY%s", VTY_NEWLINE
);
1160 /* Enable command */
1164 "Turn on privileged mode command\n")
1166 /* If enable password is NULL, change to ENABLE_NODE */
1167 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
) ||
1168 vty
->type
== VTY_SHELL_SERV
)
1169 vty
->node
= ENABLE_NODE
;
1171 vty
->node
= AUTH_ENABLE_NODE
;
1176 /* Disable command */
1180 "Turn off privileged mode command\n")
1182 if (vty
->node
== ENABLE_NODE
)
1183 vty
->node
= VIEW_NODE
;
1187 /* Down vty node level. */
1191 "Exit current mode and down to previous mode\n")
1198 cmd_exit (struct vty
*vty
)
1204 if (vty_shell (vty
))
1207 vty
->status
= VTY_CLOSE
;
1210 vty
->node
= ENABLE_NODE
;
1211 vty_config_unlock (vty
);
1213 case INTERFACE_NODE
:
1225 case LDP_L2VPN_NODE
:
1232 vty
->node
= CONFIG_NODE
;
1235 case BGP_IPV4M_NODE
:
1236 case BGP_IPV4L_NODE
:
1237 case BGP_VPNV4_NODE
:
1238 case BGP_VPNV6_NODE
:
1239 case BGP_VRF_POLICY_NODE
:
1240 case BGP_VNC_DEFAULTS_NODE
:
1241 case BGP_VNC_NVE_GROUP_NODE
:
1242 case BGP_VNC_L2_GROUP_NODE
:
1244 case BGP_IPV6M_NODE
:
1246 case BGP_IPV6L_NODE
:
1247 vty
->node
= BGP_NODE
;
1251 vty
->node
= LDP_NODE
;
1253 case LDP_IPV4_IFACE_NODE
:
1254 vty
->node
= LDP_IPV4_NODE
;
1256 case LDP_IPV6_IFACE_NODE
:
1257 vty
->node
= LDP_IPV6_NODE
;
1259 case LDP_PSEUDOWIRE_NODE
:
1260 vty
->node
= LDP_L2VPN_NODE
;
1262 case KEYCHAIN_KEY_NODE
:
1263 vty
->node
= KEYCHAIN_NODE
;
1265 case LINK_PARAMS_NODE
:
1266 vty
->node
= INTERFACE_NODE
;
1277 "Exit current mode and down to previous mode\n")
1279 return config_exit (self
, vty
, argc
, argv
);
1283 /* End of configuration. */
1287 "End current mode and change to enable mode.")
1293 /* Nothing to do. */
1296 case INTERFACE_NODE
:
1305 case BGP_VRF_POLICY_NODE
:
1306 case BGP_VNC_DEFAULTS_NODE
:
1307 case BGP_VNC_NVE_GROUP_NODE
:
1308 case BGP_VNC_L2_GROUP_NODE
:
1309 case BGP_VPNV4_NODE
:
1310 case BGP_VPNV6_NODE
:
1312 case BGP_IPV4M_NODE
:
1313 case BGP_IPV4L_NODE
:
1315 case BGP_IPV6M_NODE
:
1317 case BGP_IPV6L_NODE
:
1324 case LDP_IPV4_IFACE_NODE
:
1325 case LDP_IPV6_IFACE_NODE
:
1326 case LDP_L2VPN_NODE
:
1327 case LDP_PSEUDOWIRE_NODE
:
1330 case KEYCHAIN_KEY_NODE
:
1334 case LINK_PARAMS_NODE
:
1335 vty_config_unlock (vty
);
1336 vty
->node
= ENABLE_NODE
;
1345 DEFUN (show_version
,
1349 "Displays zebra version\n")
1351 vty_out (vty
, "%s %s (%s).%s", FRR_FULL_NAME
, FRR_VERSION
,
1352 host
.name
? host
.name
: "",
1354 vty_out (vty
, "%s%s%s", FRR_COPYRIGHT
, GIT_INFO
, VTY_NEWLINE
);
1355 vty_out (vty
, "configured with:%s %s%s", VTY_NEWLINE
,
1356 FRR_CONFIG_ARGS
, VTY_NEWLINE
);
1361 /* "Set" version ... ignore version tags */
1362 DEFUN (frr_version_defaults
,
1363 frr_version_defaults_cmd
,
1364 "frr <version|defaults> LINE...",
1365 "FRRouting global parameters\n"
1366 "version configuration was written by\n"
1367 "set of configuration defaults used\n"
1373 /* Help display function for all node. */
1377 "Description of the interactive help system\n")
1380 "Quagga VTY provides advanced help feature. When you need help,%s\
1381 anytime at the command line please press '?'.%s\
1383 If nothing matches, the help list will be empty and you must backup%s\
1384 until entering a '?' shows the available options.%s\
1385 Two styles of help are provided:%s\
1386 1. Full help is available when you are ready to enter a%s\
1387 command argument (e.g. 'show ?') and describes each possible%s\
1389 2. Partial help is provided when an abbreviated argument is entered%s\
1390 and you want to know what arguments match the input%s\
1391 (e.g. 'show me?'.)%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
,
1392 VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
,
1393 VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1398 permute (struct graph_node
*start
, struct vty
*vty
)
1400 static struct list
*position
= NULL
;
1401 if (!position
) position
= list_new ();
1403 struct cmd_token
*stok
= start
->data
;
1404 struct graph_node
*gnn
;
1405 struct listnode
*ln
;
1408 listnode_add (position
, start
);
1409 for (unsigned int i
= 0; i
< vector_active (start
->to
); i
++)
1411 struct graph_node
*gn
= vector_slot (start
->to
, i
);
1412 struct cmd_token
*tok
= gn
->data
;
1413 if (tok
->attr
== CMD_ATTR_HIDDEN
||
1414 tok
->attr
== CMD_ATTR_DEPRECATED
)
1416 else if (tok
->type
== END_TKN
|| gn
== start
)
1419 for (ALL_LIST_ELEMENTS_RO (position
,ln
,gnn
))
1421 struct cmd_token
*tt
= gnn
->data
;
1422 if (tt
->type
< SPECIAL_TKN
)
1423 vty_out (vty
, " %s", tt
->text
);
1426 vty_out (vty
, "...");
1427 vty_out (vty
, VTY_NEWLINE
);
1432 if (stok
->type
== FORK_TKN
&& tok
->type
!= FORK_TKN
)
1433 for (ALL_LIST_ELEMENTS_RO (position
, ln
, gnn
))
1443 list_delete_node (position
, listtail(position
));
1447 cmd_list_cmds (struct vty
*vty
, int do_permute
)
1449 struct cmd_node
*node
= vector_slot (cmdvec
, vty
->node
);
1452 permute (vector_slot (node
->cmdgraph
->nodes
, 0), vty
);
1455 /* loop over all commands at this node */
1456 struct cmd_element
*element
= NULL
;
1457 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
); i
++)
1458 if ((element
= vector_slot (node
->cmd_vector
, i
)) &&
1459 element
->attr
!= CMD_ATTR_DEPRECATED
&&
1460 element
->attr
!= CMD_ATTR_HIDDEN
)
1461 vty_out (vty
, " %s%s", element
->string
, VTY_NEWLINE
);
1466 /* Help display function for all node. */
1469 "list [permutations]",
1470 "Print command list\n"
1471 "Print all possible command permutations\n")
1473 return cmd_list_cmds (vty
, argc
== 2);
1476 DEFUN (show_commandtree
,
1477 show_commandtree_cmd
,
1478 "show commandtree [permutations]",
1480 "Show command tree\n"
1481 "Permutations that we are interested in\n")
1483 return cmd_list_cmds (vty
, argc
== 3);
1487 vty_write_config (struct vty
*vty
)
1490 struct cmd_node
*node
;
1492 if (vty
->type
== VTY_TERM
)
1494 vty_out (vty
, "%sCurrent configuration:%s", VTY_NEWLINE
,
1496 vty_out (vty
, "!%s", VTY_NEWLINE
);
1499 vty_out (vty
, "frr version %s%s", FRR_VER_SHORT
, VTY_NEWLINE
);
1500 vty_out (vty
, "frr defaults %s%s", DFLT_NAME
, VTY_NEWLINE
);
1501 vty_out (vty
, "!%s", VTY_NEWLINE
);
1503 for (i
= 0; i
< vector_active (cmdvec
); i
++)
1504 if ((node
= vector_slot (cmdvec
, i
)) && node
->func
1505 && (node
->vtysh
|| vty
->type
!= VTY_SHELL
))
1507 if ((*node
->func
) (vty
))
1508 vty_out (vty
, "!%s", VTY_NEWLINE
);
1511 if (vty
->type
== VTY_TERM
)
1513 vty_out (vty
, "end%s",VTY_NEWLINE
);
1517 /* Write current configuration into file. */
1519 DEFUN (config_write
,
1521 "write [<file|memory|terminal>]",
1522 "Write running configuration to memory, network, or terminal\n"
1523 "Write to configuration file\n"
1524 "Write configuration currently in memory\n"
1525 "Write configuration to terminal\n")
1529 char *config_file
, *slash
;
1530 char *config_file_tmp
= NULL
;
1531 char *config_file_sav
= NULL
;
1532 int ret
= CMD_WARNING
;
1533 struct vty
*file_vty
;
1534 struct stat conf_stat
;
1536 // if command was 'write terminal' or 'show running-config'
1537 if (argc
== 2 && (strmatch(argv
[idx_type
]->text
, "terminal") ||
1538 strmatch(argv
[0]->text
, "show")))
1540 vty_write_config (vty
);
1547 /* Check and see if we are operating under vtysh configuration */
1548 if (host
.config
== NULL
)
1550 vty_out (vty
, "Can't save to configuration file, using vtysh.%s",
1556 config_file
= host
.config
;
1559 #define O_DIRECTORY 0
1561 slash
= strrchr (config_file
, '/');
1564 char *config_dir
= XSTRDUP (MTYPE_TMP
, config_file
);
1565 config_dir
[slash
- config_file
] = '\0';
1566 dirfd
= open(config_dir
, O_DIRECTORY
| O_RDONLY
);
1567 XFREE (MTYPE_TMP
, config_dir
);
1570 dirfd
= open(".", O_DIRECTORY
| O_RDONLY
);
1571 /* if dirfd is invalid, directory sync fails, but we're still OK */
1574 XMALLOC (MTYPE_TMP
, strlen (config_file
) + strlen (CONF_BACKUP_EXT
) + 1);
1575 strcpy (config_file_sav
, config_file
);
1576 strcat (config_file_sav
, CONF_BACKUP_EXT
);
1579 config_file_tmp
= XMALLOC (MTYPE_TMP
, strlen (config_file
) + 8);
1580 sprintf (config_file_tmp
, "%s.XXXXXX", config_file
);
1582 /* Open file to configuration write. */
1583 fd
= mkstemp (config_file_tmp
);
1586 vty_out (vty
, "Can't open configuration file %s.%s", config_file_tmp
,
1590 if (fchmod (fd
, CONFIGFILE_MASK
) != 0)
1592 vty_out (vty
, "Can't chmod configuration file %s: %s (%d).%s",
1593 config_file_tmp
, safe_strerror(errno
), errno
, VTY_NEWLINE
);
1597 /* Make vty for configuration file. */
1598 file_vty
= vty_new ();
1600 file_vty
->type
= VTY_FILE
;
1602 /* Config file header print. */
1603 vty_out (file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1604 vty_time_print (file_vty
, 1);
1605 vty_out (file_vty
, "!\n");
1606 vty_write_config (file_vty
);
1607 vty_close (file_vty
);
1609 if (stat(config_file
, &conf_stat
) >= 0)
1611 if (unlink (config_file_sav
) != 0)
1612 if (errno
!= ENOENT
)
1614 vty_out (vty
, "Can't unlink backup configuration file %s.%s", config_file_sav
,
1618 if (link (config_file
, config_file_sav
) != 0)
1620 vty_out (vty
, "Can't backup old configuration file %s.%s", config_file_sav
,
1627 if (rename (config_file_tmp
, config_file
) != 0)
1629 vty_out (vty
, "Can't save configuration file %s.%s", config_file
,
1636 vty_out (vty
, "Configuration saved to %s%s", config_file
,
1641 if (ret
!= CMD_SUCCESS
)
1642 unlink (config_file_tmp
);
1645 XFREE (MTYPE_TMP
, config_file_tmp
);
1646 XFREE (MTYPE_TMP
, config_file_sav
);
1650 /* ALIAS_FIXME for 'write <terminal|memory>' */
1651 DEFUN (show_running_config
,
1652 show_running_config_cmd
,
1653 "show running-config",
1655 "running configuration (same as write terminal/memory)\n")
1657 return config_write (self
, vty
, argc
, argv
);
1660 /* ALIAS_FIXME for 'write file' */
1661 DEFUN (copy_runningconf_startupconf
,
1662 copy_runningconf_startupconf_cmd
,
1663 "copy running-config startup-config",
1664 "Copy configuration\n"
1665 "Copy running config to... \n"
1666 "Copy running config to startup config (same as write file)\n")
1669 vty_write_config (vty
);
1674 /* Write startup configuration into the terminal. */
1675 DEFUN (show_startup_config
,
1676 show_startup_config_cmd
,
1677 "show startup-config",
1679 "Contents of startup configuration\n")
1686 if (host
.config
== NULL
)
1689 confp
= fopen (host
.config
, "r");
1692 vty_out (vty
, "Can't open configuration file [%s] due to '%s'%s",
1693 host
.config
, safe_strerror(errno
), VTY_NEWLINE
);
1697 while (fgets (buf
, BUFSIZ
, confp
))
1701 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1705 vty_out (vty
, "%s%s", buf
, VTY_NEWLINE
);
1714 cmd_hostname_set (const char *hostname
)
1716 XFREE (MTYPE_HOST
, host
.name
);
1717 host
.name
= hostname
? XSTRDUP (MTYPE_HOST
, hostname
) : NULL
;
1721 /* Hostname configuration */
1722 DEFUN (config_hostname
,
1725 "Set system's network name\n"
1726 "This system's network name\n")
1728 struct cmd_token
*word
= argv
[1];
1730 if (!isalpha((int) word
->arg
[0]))
1732 vty_out (vty
, "Please specify string starting with alphabet%s", VTY_NEWLINE
);
1736 return cmd_hostname_set (word
->arg
);
1739 DEFUN (config_no_hostname
,
1741 "no hostname [HOSTNAME]",
1743 "Reset system's network name\n"
1744 "Host name of this router\n")
1746 return cmd_hostname_set (NULL
);
1749 /* VTY interface password set. */
1750 DEFUN (config_password
,
1752 "password [(8-8)] WORD",
1753 "Assign the terminal connection password\n"
1754 "Specifies a HIDDEN password will follow\n"
1755 "The password string\n")
1759 if (argc
== 3) // '8' was specified
1762 XFREE (MTYPE_HOST
, host
.password
);
1763 host
.password
= NULL
;
1764 if (host
.password_encrypt
)
1765 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1766 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, argv
[idx_word
]->arg
);
1770 if (!isalnum (argv
[idx_8
]->arg
[0]))
1773 "Please specify string starting with alphanumeric%s", VTY_NEWLINE
);
1778 XFREE (MTYPE_HOST
, host
.password
);
1779 host
.password
= NULL
;
1783 if (host
.password_encrypt
)
1784 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1785 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (argv
[idx_8
]->arg
));
1788 host
.password
= XSTRDUP (MTYPE_HOST
, argv
[idx_8
]->arg
);
1793 /* VTY enable password set. */
1794 DEFUN (config_enable_password
,
1795 enable_password_cmd
,
1796 "enable password [(8-8)] WORD",
1797 "Modify enable password parameters\n"
1798 "Assign the privileged level password\n"
1799 "Specifies a HIDDEN password will follow\n"
1800 "The HIDDEN 'enable' password string\n")
1805 /* Crypt type is specified. */
1808 if (argv
[idx_8
]->arg
[0] == '8')
1811 XFREE (MTYPE_HOST
, host
.enable
);
1814 if (host
.enable_encrypt
)
1815 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1816 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, argv
[idx_word
]->arg
);
1822 vty_out (vty
, "Unknown encryption type.%s", VTY_NEWLINE
);
1827 if (!isalnum (argv
[idx_8
]->arg
[0]))
1830 "Please specify string starting with alphanumeric%s", VTY_NEWLINE
);
1835 XFREE (MTYPE_HOST
, host
.enable
);
1838 /* Plain password input. */
1841 if (host
.enable_encrypt
)
1842 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1843 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (argv
[idx_8
]->arg
));
1846 host
.enable
= XSTRDUP (MTYPE_HOST
, argv
[idx_8
]->arg
);
1851 /* VTY enable password delete. */
1852 DEFUN (no_config_enable_password
,
1853 no_enable_password_cmd
,
1854 "no enable password",
1856 "Modify enable password parameters\n"
1857 "Assign the privileged level password\n")
1860 XFREE (MTYPE_HOST
, host
.enable
);
1863 if (host
.enable_encrypt
)
1864 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1865 host
.enable_encrypt
= NULL
;
1870 DEFUN (service_password_encrypt
,
1871 service_password_encrypt_cmd
,
1872 "service password-encryption",
1873 "Set up miscellaneous service\n"
1874 "Enable encrypted passwords\n")
1883 if (host
.password_encrypt
)
1884 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1885 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (host
.password
));
1889 if (host
.enable_encrypt
)
1890 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1891 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (host
.enable
));
1897 DEFUN (no_service_password_encrypt
,
1898 no_service_password_encrypt_cmd
,
1899 "no service password-encryption",
1901 "Set up miscellaneous service\n"
1902 "Enable encrypted passwords\n")
1909 if (host
.password_encrypt
)
1910 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1911 host
.password_encrypt
= NULL
;
1913 if (host
.enable_encrypt
)
1914 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1915 host
.enable_encrypt
= NULL
;
1920 DEFUN (config_terminal_length
,
1921 config_terminal_length_cmd
,
1922 "terminal length (0-512)",
1923 "Set terminal line parameters\n"
1924 "Set number of lines on a screen\n"
1925 "Number of lines on screen (0 for no pausing)\n")
1929 vty
->lines
= atoi (argv
[idx_number
]->arg
);
1934 DEFUN (config_terminal_no_length
,
1935 config_terminal_no_length_cmd
,
1936 "terminal no length",
1937 "Set terminal line parameters\n"
1939 "Set number of lines on a screen\n")
1945 DEFUN (service_terminal_length
,
1946 service_terminal_length_cmd
,
1947 "service terminal-length (0-512)",
1948 "Set up miscellaneous service\n"
1949 "System wide terminal length configuration\n"
1950 "Number of lines of VTY (0 means no line control)\n")
1954 host
.lines
= atoi (argv
[idx_number
]->arg
);
1959 DEFUN (no_service_terminal_length
,
1960 no_service_terminal_length_cmd
,
1961 "no service terminal-length [(0-512)]",
1963 "Set up miscellaneous service\n"
1964 "System wide terminal length configuration\n"
1965 "Number of lines of VTY (0 means no line control)\n")
1971 DEFUN_HIDDEN (do_echo
,
1974 "Echo a message back to the vty\n"
1975 "The message to echo\n")
1979 vty_out (vty
, "%s%s", ((message
= argv_concat (argv
, argc
, 1)) ? message
: ""),
1982 XFREE(MTYPE_TMP
, message
);
1986 DEFUN (config_logmsg
,
1988 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
1989 "Send a message to enabled logging destinations\n"
1991 "The message to send\n")
1993 int idx_log_level
= 1;
1994 int idx_message
= 2;
1998 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
1999 return CMD_ERR_NO_MATCH
;
2001 zlog(level
, "%s", ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
2003 XFREE(MTYPE_TMP
, message
);
2008 DEFUN (show_logging
,
2012 "Show current logging configuration\n")
2014 struct zlog
*zl
= zlog_default
;
2016 vty_out (vty
, "Syslog logging: ");
2017 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
2018 vty_out (vty
, "disabled");
2020 vty_out (vty
, "level %s, facility %s, ident %s",
2021 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
2022 facility_name(zl
->facility
), zl
->ident
);
2023 vty_out (vty
, "%s", VTY_NEWLINE
);
2025 vty_out (vty
, "Stdout logging: ");
2026 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
2027 vty_out (vty
, "disabled");
2029 vty_out (vty
, "level %s",
2030 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
2031 vty_out (vty
, "%s", VTY_NEWLINE
);
2033 vty_out (vty
, "Monitor logging: ");
2034 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
2035 vty_out (vty
, "disabled");
2037 vty_out (vty
, "level %s",
2038 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
2039 vty_out (vty
, "%s", VTY_NEWLINE
);
2041 vty_out (vty
, "File logging: ");
2042 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) ||
2044 vty_out (vty
, "disabled");
2046 vty_out (vty
, "level %s, filename %s",
2047 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
2049 vty_out (vty
, "%s", VTY_NEWLINE
);
2051 vty_out (vty
, "Protocol name: %s%s",
2052 zl
->protoname
, VTY_NEWLINE
);
2053 vty_out (vty
, "Record priority: %s%s",
2054 (zl
->record_priority
? "enabled" : "disabled"), VTY_NEWLINE
);
2055 vty_out (vty
, "Timestamp precision: %d%s",
2056 zl
->timestamp_precision
, VTY_NEWLINE
);
2061 DEFUN (config_log_stdout
,
2062 config_log_stdout_cmd
,
2063 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2065 "Set stdout logging level\n"
2068 int idx_log_level
= 2;
2070 if (argc
== idx_log_level
)
2072 zlog_set_level (ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2077 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2078 return CMD_ERR_NO_MATCH
;
2079 zlog_set_level (ZLOG_DEST_STDOUT
, level
);
2083 DEFUN (no_config_log_stdout
,
2084 no_config_log_stdout_cmd
,
2085 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2088 "Cancel logging to stdout\n"
2091 zlog_set_level (ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
2095 DEFUN (config_log_monitor
,
2096 config_log_monitor_cmd
,
2097 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2099 "Set terminal line (monitor) logging level\n"
2102 int idx_log_level
= 2;
2104 if (argc
== idx_log_level
)
2106 zlog_set_level (ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
2111 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2112 return CMD_ERR_NO_MATCH
;
2113 zlog_set_level (ZLOG_DEST_MONITOR
, level
);
2117 DEFUN (no_config_log_monitor
,
2118 no_config_log_monitor_cmd
,
2119 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2122 "Disable terminal line (monitor) logging\n"
2125 zlog_set_level (ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
2130 set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
2134 const char *fullpath
;
2136 /* Path detection. */
2137 if (! IS_DIRECTORY_SEP (*fname
))
2139 char cwd
[MAXPATHLEN
+1];
2140 cwd
[MAXPATHLEN
] = '\0';
2142 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
2144 zlog_err ("config_log_file: Unable to alloc mem!");
2148 if ( (p
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + strlen (fname
) + 2))
2151 zlog_err ("config_log_file: Unable to alloc mem!");
2154 sprintf (p
, "%s/%s", cwd
, fname
);
2160 ret
= zlog_set_file (fullpath
, loglevel
);
2163 XFREE (MTYPE_TMP
, p
);
2167 vty_out (vty
, "can't open logfile %s\n", fname
);
2172 XFREE (MTYPE_HOST
, host
.logfile
);
2174 host
.logfile
= XSTRDUP (MTYPE_HOST
, fname
);
2176 #if defined(HAVE_CUMULUS)
2177 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2178 zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] = ZLOG_DISABLED
;
2183 DEFUN (config_log_file
,
2184 config_log_file_cmd
,
2185 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2188 "Logging filename\n"
2191 int idx_filename
= 2;
2192 int idx_log_levels
= 3;
2196 if ((level
= level_match(argv
[idx_log_levels
]->arg
)) == ZLOG_DISABLED
)
2197 return CMD_ERR_NO_MATCH
;
2198 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2201 return set_log_file(vty
, argv
[idx_filename
]->arg
, zlog_default
->default_lvl
);
2204 DEFUN (no_config_log_file
,
2205 no_config_log_file_cmd
,
2206 "no log file [FILENAME [LEVEL]]",
2209 "Cancel logging to file\n"
2210 "Logging file name\n"
2211 "Logging file name\n"
2217 XFREE (MTYPE_HOST
, host
.logfile
);
2219 host
.logfile
= NULL
;
2224 DEFUN (config_log_syslog
,
2225 config_log_syslog_cmd
,
2226 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2228 "Set syslog logging level\n"
2231 int idx_log_levels
= 2;
2235 if ((level
= level_match (argv
[idx_log_levels
]->arg
)) == ZLOG_DISABLED
)
2236 return CMD_ERR_NO_MATCH
;
2237 zlog_set_level (ZLOG_DEST_SYSLOG
, level
);
2242 zlog_set_level (ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2247 DEFUN (no_config_log_syslog
,
2248 no_config_log_syslog_cmd
,
2249 "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>]",
2252 "Cancel logging to syslog\n"
2256 zlog_set_level (ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2260 DEFUN (config_log_facility
,
2261 config_log_facility_cmd
,
2262 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2264 "Facility parameter for syslog messages\n"
2268 int facility
= facility_match(argv
[idx_target
]->arg
);
2270 zlog_default
->facility
= facility
;
2274 DEFUN (no_config_log_facility
,
2275 no_config_log_facility_cmd
,
2276 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2279 "Reset syslog facility to default (daemon)\n"
2282 zlog_default
->facility
= LOG_DAEMON
;
2286 DEFUN_DEPRECATED (config_log_trap
,
2287 config_log_trap_cmd
,
2288 "log trap <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>",
2290 "(Deprecated) Set logging level and default for all destinations\n"
2296 if ((new_level
= level_match(argv
[2]->arg
)) == ZLOG_DISABLED
)
2297 return CMD_ERR_NO_MATCH
;
2299 zlog_default
->default_lvl
= new_level
;
2300 for (i
= 0; i
< ZLOG_NUM_DESTS
; i
++)
2301 if (zlog_default
->maxlvl
[i
] != ZLOG_DISABLED
)
2302 zlog_default
->maxlvl
[i
] = new_level
;
2306 DEFUN_DEPRECATED (no_config_log_trap
,
2307 no_config_log_trap_cmd
,
2308 "no log trap [emergencies|alerts|critical|errors|warnings|notifications|informational|debugging]",
2311 "Permit all logging information\n"
2314 zlog_default
->default_lvl
= LOG_DEBUG
;
2318 DEFUN (config_log_record_priority
,
2319 config_log_record_priority_cmd
,
2320 "log record-priority",
2322 "Log the priority of the message within the message\n")
2324 zlog_default
->record_priority
= 1 ;
2328 DEFUN (no_config_log_record_priority
,
2329 no_config_log_record_priority_cmd
,
2330 "no log record-priority",
2333 "Do not log the priority of the message within the message\n")
2335 zlog_default
->record_priority
= 0 ;
2339 DEFUN (config_log_timestamp_precision
,
2340 config_log_timestamp_precision_cmd
,
2341 "log timestamp precision (0-6)",
2343 "Timestamp configuration\n"
2344 "Set the timestamp precision\n"
2345 "Number of subsecond digits\n")
2348 VTY_GET_INTEGER_RANGE("Timestamp Precision",
2349 zlog_default
->timestamp_precision
, argv
[idx_number
]->arg
, 0, 6);
2353 DEFUN (no_config_log_timestamp_precision
,
2354 no_config_log_timestamp_precision_cmd
,
2355 "no log timestamp precision",
2358 "Timestamp configuration\n"
2359 "Reset the timestamp precision to the default value of 0\n")
2361 zlog_default
->timestamp_precision
= 0 ;
2366 cmd_banner_motd_file (const char *file
)
2368 int success
= CMD_SUCCESS
;
2373 rpath
= realpath (file
, p
);
2375 return CMD_ERR_NO_FILE
;
2376 in
= strstr (rpath
, SYSCONFDIR
);
2380 XFREE (MTYPE_HOST
, host
.motdfile
);
2381 host
.motdfile
= XSTRDUP (MTYPE_HOST
, file
);
2384 success
= CMD_WARNING
;
2389 DEFUN (banner_motd_file
,
2390 banner_motd_file_cmd
,
2391 "banner motd file FILE",
2394 "Banner from a file\n"
2398 const char *filename
= argv
[idx_file
]->arg
;
2399 int cmd
= cmd_banner_motd_file (filename
);
2401 if (cmd
== CMD_ERR_NO_FILE
)
2402 vty_out (vty
, "%s does not exist", filename
);
2403 else if (cmd
== CMD_WARNING
)
2404 vty_out (vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2409 DEFUN (banner_motd_default
,
2410 banner_motd_default_cmd
,
2411 "banner motd default",
2412 "Set banner string\n"
2413 "Strings for motd\n"
2416 host
.motd
= default_motd
;
2420 DEFUN (no_banner_motd
,
2424 "Set banner string\n"
2425 "Strings for motd\n")
2429 XFREE (MTYPE_HOST
, host
.motdfile
);
2430 host
.motdfile
= NULL
;
2434 /* Set config filename. Called from vty.c */
2436 host_config_set (const char *filename
)
2439 XFREE (MTYPE_HOST
, host
.config
);
2440 host
.config
= XSTRDUP (MTYPE_HOST
, filename
);
2444 host_config_get (void)
2450 install_default (enum node_type node
)
2452 install_element (node
, &config_exit_cmd
);
2453 install_element (node
, &config_quit_cmd
);
2454 install_element (node
, &config_end_cmd
);
2455 install_element (node
, &config_help_cmd
);
2456 install_element (node
, &config_list_cmd
);
2458 install_element (node
, &config_write_cmd
);
2459 install_element (node
, &show_running_config_cmd
);
2461 install_element (node
, &autocomplete_cmd
);
2464 /* Initialize command interface. Install basic nodes and commands.
2466 * terminal = 0 -- vtysh / no logging, no config control
2467 * terminal = 1 -- normal daemon
2468 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2470 cmd_init (int terminal
)
2474 varhandlers
= list_new ();
2476 /* Allocate initial top vector of commands. */
2477 cmdvec
= vector_init (VECTOR_MIN_SIZE
);
2479 /* Default host value settings. */
2481 host
.password
= NULL
;
2483 host
.logfile
= NULL
;
2485 host
.noconfig
= (terminal
< 0);
2487 host
.motd
= default_motd
;
2488 host
.motdfile
= NULL
;
2490 /* Install top nodes. */
2491 install_node (&view_node
, NULL
);
2492 install_node (&enable_node
, NULL
);
2493 install_node (&auth_node
, NULL
);
2494 install_node (&auth_enable_node
, NULL
);
2495 install_node (&config_node
, config_write_host
);
2497 /* Each node's basic commands. */
2498 install_element (VIEW_NODE
, &show_version_cmd
);
2501 install_element (VIEW_NODE
, &config_list_cmd
);
2502 install_element (VIEW_NODE
, &config_exit_cmd
);
2503 install_element (VIEW_NODE
, &config_quit_cmd
);
2504 install_element (VIEW_NODE
, &config_help_cmd
);
2505 install_element (VIEW_NODE
, &config_enable_cmd
);
2506 install_element (VIEW_NODE
, &config_terminal_length_cmd
);
2507 install_element (VIEW_NODE
, &config_terminal_no_length_cmd
);
2508 install_element (VIEW_NODE
, &show_logging_cmd
);
2509 install_element (VIEW_NODE
, &show_commandtree_cmd
);
2510 install_element (VIEW_NODE
, &echo_cmd
);
2511 install_element (VIEW_NODE
, &autocomplete_cmd
);
2516 install_element (ENABLE_NODE
, &config_end_cmd
);
2517 install_element (ENABLE_NODE
, &config_disable_cmd
);
2518 install_element (ENABLE_NODE
, &config_terminal_cmd
);
2519 install_element (ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2520 install_element (ENABLE_NODE
, &config_write_cmd
);
2521 install_element (ENABLE_NODE
, &show_running_config_cmd
);
2523 install_element (ENABLE_NODE
, &show_startup_config_cmd
);
2527 install_element (ENABLE_NODE
, &config_logmsg_cmd
);
2528 install_default (CONFIG_NODE
);
2531 workqueue_cmd_init ();
2534 install_element (CONFIG_NODE
, &hostname_cmd
);
2535 install_element (CONFIG_NODE
, &no_hostname_cmd
);
2536 install_element (CONFIG_NODE
, &frr_version_defaults_cmd
);
2540 install_element (CONFIG_NODE
, &password_cmd
);
2541 install_element (CONFIG_NODE
, &enable_password_cmd
);
2542 install_element (CONFIG_NODE
, &no_enable_password_cmd
);
2544 install_element (CONFIG_NODE
, &config_log_stdout_cmd
);
2545 install_element (CONFIG_NODE
, &no_config_log_stdout_cmd
);
2546 install_element (CONFIG_NODE
, &config_log_monitor_cmd
);
2547 install_element (CONFIG_NODE
, &no_config_log_monitor_cmd
);
2548 install_element (CONFIG_NODE
, &config_log_file_cmd
);
2549 install_element (CONFIG_NODE
, &no_config_log_file_cmd
);
2550 install_element (CONFIG_NODE
, &config_log_syslog_cmd
);
2551 install_element (CONFIG_NODE
, &no_config_log_syslog_cmd
);
2552 install_element (CONFIG_NODE
, &config_log_facility_cmd
);
2553 install_element (CONFIG_NODE
, &no_config_log_facility_cmd
);
2554 install_element (CONFIG_NODE
, &config_log_trap_cmd
);
2555 install_element (CONFIG_NODE
, &no_config_log_trap_cmd
);
2556 install_element (CONFIG_NODE
, &config_log_record_priority_cmd
);
2557 install_element (CONFIG_NODE
, &no_config_log_record_priority_cmd
);
2558 install_element (CONFIG_NODE
, &config_log_timestamp_precision_cmd
);
2559 install_element (CONFIG_NODE
, &no_config_log_timestamp_precision_cmd
);
2560 install_element (CONFIG_NODE
, &service_password_encrypt_cmd
);
2561 install_element (CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2562 install_element (CONFIG_NODE
, &banner_motd_default_cmd
);
2563 install_element (CONFIG_NODE
, &banner_motd_file_cmd
);
2564 install_element (CONFIG_NODE
, &no_banner_motd_cmd
);
2565 install_element (CONFIG_NODE
, &service_terminal_length_cmd
);
2566 install_element (CONFIG_NODE
, &no_service_terminal_length_cmd
);
2568 vrf_install_commands ();
2572 grammar_sandbox_init();
2579 struct cmd_node
*cmd_node
;
2583 for (unsigned int i
= 0; i
< vector_active (cmdvec
); i
++)
2584 if ((cmd_node
= vector_slot (cmdvec
, i
)) != NULL
)
2586 // deleting the graph delets the cmd_element as well
2587 graph_delete_graph (cmd_node
->cmdgraph
);
2588 vector_free (cmd_node
->cmd_vector
);
2589 hash_clean (cmd_node
->cmd_hash
, NULL
);
2590 hash_free (cmd_node
->cmd_hash
);
2591 cmd_node
->cmd_hash
= NULL
;
2594 vector_free (cmdvec
);
2599 XFREE (MTYPE_HOST
, host
.name
);
2601 XFREE (MTYPE_HOST
, host
.password
);
2602 if (host
.password_encrypt
)
2603 XFREE (MTYPE_HOST
, host
.password_encrypt
);
2605 XFREE (MTYPE_HOST
, host
.enable
);
2606 if (host
.enable_encrypt
)
2607 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
2609 XFREE (MTYPE_HOST
, host
.logfile
);
2611 XFREE (MTYPE_HOST
, host
.motdfile
);
2613 XFREE (MTYPE_HOST
, host
.config
);
2615 list_delete (varhandlers
);