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_outln (vty
, "hostname %s", host
.name
);
440 if (host
.password_encrypt
)
441 vty_outln (vty
, "password 8 %s", host
.password_encrypt
);
442 if (host
.enable_encrypt
)
443 vty_outln (vty
, "enable password 8 %s", host
.enable_encrypt
);
448 vty_outln (vty
, "password %s", host
.password
);
450 vty_outln (vty
, "enable password %s", host
.enable
);
453 if (zlog_default
->default_lvl
!= LOG_DEBUG
)
455 vty_outln (vty
,"! N.B. The 'log trap' command is deprecated.");
456 vty_outln (vty
, "log trap %s",
457 zlog_priority
[zlog_default
->default_lvl
]);
460 if (host
.logfile
&& (zlog_default
->maxlvl
[ZLOG_DEST_FILE
] != ZLOG_DISABLED
))
462 vty_out (vty
, "log file %s", host
.logfile
);
463 if (zlog_default
->maxlvl
[ZLOG_DEST_FILE
] != zlog_default
->default_lvl
)
465 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_FILE
]]);
466 vty_out (vty
, VTYNL
);
469 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != ZLOG_DISABLED
)
471 vty_out (vty
, "log stdout");
472 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != zlog_default
->default_lvl
)
474 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
]]);
475 vty_out (vty
, VTYNL
);
478 if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
479 vty_outln (vty
,"no log monitor");
480 else if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] != zlog_default
->default_lvl
)
481 vty_outln (vty
,"log monitor %s",
482 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]]);
484 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
486 vty_out (vty
, "log syslog");
487 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != zlog_default
->default_lvl
)
489 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
]]);
490 vty_out (vty
, VTYNL
);
493 if (zlog_default
->facility
!= LOG_DAEMON
)
494 vty_outln (vty
, "log facility %s",
495 facility_name(zlog_default
->facility
));
497 if (zlog_default
->record_priority
== 1)
498 vty_outln (vty
, "log record-priority");
500 if (zlog_default
->timestamp_precision
> 0)
501 vty_outln (vty
, "log timestamp precision %d",
502 zlog_default
->timestamp_precision
);
505 vty_outln (vty
, "service advanced-vty");
508 vty_outln (vty
, "service password-encryption");
511 vty_outln (vty
, "service terminal-length %d",host
.lines
);
514 vty_outln (vty
, "banner motd file %s", host
.motdfile
);
515 else if (! host
.motd
)
516 vty_outln (vty
, "no banner motd");
521 /* Utility function for getting command graph. */
522 static struct graph
*
523 cmd_node_graph (vector v
, enum node_type ntype
)
525 struct cmd_node
*cnode
= vector_slot (v
, ntype
);
526 return cnode
->cmdgraph
;
530 cmd_try_do_shortcut (enum node_type node
, char* first_word
) {
531 if ( first_word
!= NULL
&&
534 node
!= AUTH_ENABLE_NODE
&&
535 0 == strcmp( "do", first_word
) )
541 * Compare function for cmd_token.
542 * Used with qsort to sort command completions.
545 compare_completions (const void *fst
, const void *snd
)
547 struct cmd_token
*first
= *(struct cmd_token
**) fst
,
548 *secnd
= *(struct cmd_token
**) snd
;
549 return strcmp (first
->text
, secnd
->text
);
553 * Takes a list of completions returned by command_complete,
554 * dedeuplicates them based on both text and description,
555 * sorts them, and returns them as a vector.
557 * @param completions linked list of cmd_token
558 * @return deduplicated and sorted vector with
561 completions_to_vec (struct list
*completions
)
563 vector comps
= vector_init (VECTOR_MIN_SIZE
);
566 struct cmd_token
*token
, *cr
= NULL
;
567 unsigned int i
, exists
;
568 for (ALL_LIST_ELEMENTS_RO(completions
,ln
,token
))
570 if (token
->type
== END_TKN
&& (cr
= token
))
573 // linear search for token in completions vector
575 for (i
= 0; i
< vector_active (comps
) && !exists
; i
++)
577 struct cmd_token
*curr
= vector_slot (comps
, i
);
579 exists
= !strcmp (curr
->text
, token
->text
) &&
580 !strcmp (curr
->desc
, token
->desc
);
582 exists
= !strcmp (curr
->text
, token
->text
);
583 #endif /* VTYSH_DEBUG */
587 vector_set (comps
, token
);
592 vector_active (comps
),
594 &compare_completions
);
596 // make <cr> the first element, if it is present
599 vector_set_index (comps
, vector_active (comps
), NULL
);
600 memmove (comps
->index
+ 1, comps
->index
, (comps
->alloced
- 1) * sizeof (void *));
601 vector_set_index (comps
, 0, cr
);
607 * Generates a vector of cmd_token representing possible completions
608 * on the current input.
610 * @param vline the vectorized input line
611 * @param vty the vty with the node to match on
612 * @param status pointer to matcher status code
613 * @return vector of struct cmd_token * with possible completions
616 cmd_complete_command_real (vector vline
, struct vty
*vty
, int *status
)
618 struct list
*completions
;
619 struct graph
*cmdgraph
= cmd_node_graph (cmdvec
, vty
->node
);
621 enum matcher_rv rv
= command_complete (cmdgraph
, vline
, &completions
);
623 if (MATCHER_ERROR(rv
))
625 *status
= CMD_ERR_NO_MATCH
;
629 vector comps
= completions_to_vec (completions
);
630 list_delete (completions
);
632 // set status code appropriately
633 switch (vector_active (comps
))
636 *status
= CMD_ERR_NO_MATCH
;
639 *status
= CMD_COMPLETE_FULL_MATCH
;
642 *status
= CMD_COMPLETE_LIST_MATCH
;
649 cmd_describe_command (vector vline
, struct vty
*vty
, int *status
)
653 if ( cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0) ) )
655 enum node_type onode
;
656 vector shifted_vline
;
660 vty
->node
= ENABLE_NODE
;
661 /* We can try it on enable node, cos' the vty is authenticated */
663 shifted_vline
= vector_init (vector_count(vline
));
665 for (index
= 1; index
< vector_active (vline
); index
++)
667 vector_set_index (shifted_vline
, index
-1, vector_lookup(vline
, index
));
670 ret
= cmd_complete_command_real (shifted_vline
, vty
, status
);
672 vector_free(shifted_vline
);
677 return cmd_complete_command_real (vline
, vty
, status
);
680 static struct list
*varhandlers
= NULL
;
683 cmd_variable_complete (struct cmd_token
*token
, const char *arg
, vector comps
)
686 const struct cmd_variable_handler
*cvh
;
690 tmpcomps
= arg
? vector_init (VECTOR_MIN_SIZE
) : comps
;
692 for (ALL_LIST_ELEMENTS_RO(varhandlers
, ln
, cvh
))
694 if (cvh
->tokenname
&& strcmp(cvh
->tokenname
, token
->text
))
696 if (cvh
->varname
&& (!token
->varname
|| strcmp(cvh
->varname
, token
->varname
)))
698 cvh
->completions(tmpcomps
, token
);
706 for (i
= vector_active(tmpcomps
); i
; i
--)
708 char *item
= vector_slot(tmpcomps
, i
- 1);
709 if (strlen(item
) >= argsz
710 && !strncmp(item
, arg
, argsz
))
711 vector_set(comps
, item
);
713 XFREE(MTYPE_COMPLETION
, item
);
715 vector_free(tmpcomps
);
719 cmd_variable_handler_register (const struct cmd_variable_handler
*cvh
)
724 for (; cvh
->completions
; cvh
++)
725 listnode_add(varhandlers
, (void *)cvh
);
728 DEFUN_HIDDEN (autocomplete
,
730 "autocomplete TYPE TEXT VARNAME",
731 "Autocompletion handler (internal, for vtysh)\n"
734 "cmd_token->varname\n")
736 struct cmd_token tok
;
737 vector comps
= vector_init(32);
740 memset(&tok
, 0, sizeof(tok
));
741 tok
.type
= atoi(argv
[1]->arg
);
742 tok
.text
= argv
[2]->arg
;
743 tok
.varname
= argv
[3]->arg
;
744 if (!strcmp(tok
.varname
, "-"))
747 cmd_variable_complete(&tok
, NULL
, comps
);
749 for (i
= 0; i
< vector_active(comps
); i
++)
751 char *text
= vector_slot(comps
, i
);
752 vty_out(vty
, "%s\n", text
);
753 XFREE(MTYPE_COMPLETION
, text
);
761 * Generate possible tab-completions for the given input. This function only
762 * returns results that would result in a valid command if used as Readline
763 * completions (as is the case in vtysh). For instance, if the passed vline ends
764 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
766 * @param vline vectorized input line
768 * @param status location to store matcher status code in
769 * @return set of valid strings for use with Readline as tab-completions.
773 cmd_complete_command (vector vline
, struct vty
*vty
, int *status
)
776 int original_node
= vty
->node
;
777 vector input_line
= vector_init (vector_count (vline
));
779 // if the first token is 'do' we'll want to execute the command in the enable node
780 int do_shortcut
= cmd_try_do_shortcut (vty
->node
, vector_slot (vline
, 0));
781 vty
->node
= do_shortcut
? ENABLE_NODE
: original_node
;
783 // construct the input line we'll be matching on
784 unsigned int offset
= (do_shortcut
) ? 1 : 0;
785 for (unsigned index
= 0; index
+ offset
< vector_active (vline
); index
++)
786 vector_set_index (input_line
, index
, vector_lookup (vline
, index
+ offset
));
788 // get token completions -- this is a copying operation
789 vector comps
= NULL
, initial_comps
;
790 initial_comps
= cmd_complete_command_real (input_line
, vty
, status
);
792 if (!MATCHER_ERROR (*status
))
794 assert (initial_comps
);
795 // filter out everything that is not suitable for a tab-completion
796 comps
= vector_init (VECTOR_MIN_SIZE
);
797 for (unsigned int i
= 0; i
< vector_active(initial_comps
); i
++)
799 struct cmd_token
*token
= vector_slot (initial_comps
, i
);
800 if (token
->type
== WORD_TKN
)
801 vector_set (comps
, XSTRDUP (MTYPE_COMPLETION
, token
->text
));
802 else if (IS_VARYING_TOKEN(token
->type
))
804 const char *ref
= vector_lookup(vline
, vector_active (vline
) - 1);
805 cmd_variable_complete (token
, ref
, comps
);
808 vector_free (initial_comps
);
810 // since we filtered results, we need to re-set status code
811 switch (vector_active (comps
))
814 *status
= CMD_ERR_NO_MATCH
;
817 *status
= CMD_COMPLETE_FULL_MATCH
;
820 *status
= CMD_COMPLETE_LIST_MATCH
;
823 // copy completions text into an array of char*
824 ret
= XMALLOC (MTYPE_TMP
, (vector_active (comps
)+1) * sizeof (char *));
826 for (i
= 0; i
< vector_active (comps
); i
++)
828 ret
[i
] = vector_slot (comps
, i
);
830 // set the last element to NULL, because this array is used in
831 // a Readline completion_generator function which expects NULL
832 // as a sentinel value
837 else if (initial_comps
)
838 vector_free (initial_comps
);
840 // comps should always be null here
843 // free the adjusted input line
844 vector_free (input_line
);
846 // reset vty->node to its original value
847 vty
->node
= original_node
;
852 /* return parent node */
853 /* MUST eventually converge on CONFIG_NODE */
855 node_parent ( enum node_type node
)
859 assert (node
> CONFIG_NODE
);
865 case BGP_VRF_POLICY_NODE
:
866 case BGP_VNC_DEFAULTS_NODE
:
867 case BGP_VNC_NVE_GROUP_NODE
:
868 case BGP_VNC_L2_GROUP_NODE
:
878 case KEYCHAIN_KEY_NODE
:
881 case LINK_PARAMS_NODE
:
882 ret
= INTERFACE_NODE
;
888 case LDP_IPV4_IFACE_NODE
:
891 case LDP_IPV6_IFACE_NODE
:
894 case LDP_PSEUDOWIRE_NODE
:
895 ret
= LDP_L2VPN_NODE
;
905 /* Execute command by argument vline vector. */
907 cmd_execute_command_real (vector vline
,
908 enum filter_type filter
,
910 const struct cmd_element
**cmd
)
912 struct list
*argv_list
;
913 enum matcher_rv status
;
914 const struct cmd_element
*matched_element
= NULL
;
916 struct graph
*cmdgraph
= cmd_node_graph (cmdvec
, vty
->node
);
917 status
= command_match (cmdgraph
, vline
, &argv_list
, &matched_element
);
920 *cmd
= matched_element
;
922 // if matcher error, return corresponding CMD_ERR
923 if (MATCHER_ERROR(status
))
926 list_delete (argv_list
);
929 case MATCHER_INCOMPLETE
:
930 return CMD_ERR_INCOMPLETE
;
931 case MATCHER_AMBIGUOUS
:
932 return CMD_ERR_AMBIGUOUS
;
934 return CMD_ERR_NO_MATCH
;
938 // build argv array from argv list
939 struct cmd_token
**argv
= XMALLOC (MTYPE_TMP
, argv_list
->count
* sizeof (struct cmd_token
*));
941 struct cmd_token
*token
;
943 for (ALL_LIST_ELEMENTS_RO(argv_list
,ln
,token
))
946 int argc
= argv_list
->count
;
949 if (matched_element
->daemon
)
950 ret
= CMD_SUCCESS_DAEMON
;
952 ret
= matched_element
->func (matched_element
, vty
, argc
, argv
);
954 // delete list and cmd_token's in it
955 list_delete (argv_list
);
956 XFREE (MTYPE_TMP
, argv
);
962 * Execute a given command, handling things like "do ..." and checking
963 * whether the given command might apply at a parent node if doesn't
964 * apply for the current node.
966 * @param vline Command line input, vector of char* where each element is
968 * @param vty The vty context in which the command should be executed.
969 * @param cmd Pointer where the struct cmd_element of the matched command
970 * will be stored, if any. May be set to NULL if this info is
972 * @param vtysh If set != 0, don't lookup the command at parent nodes.
973 * @return The status of the command that has been executed or an error code
974 * as to why no command could be executed.
977 cmd_execute_command (vector vline
, struct vty
*vty
,
978 const struct cmd_element
**cmd
,
981 int ret
, saved_ret
= 0;
982 enum node_type onode
, try_node
;
984 onode
= try_node
= vty
->node
;
986 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0)))
988 vector shifted_vline
;
991 vty
->node
= ENABLE_NODE
;
992 /* We can try it on enable node, cos' the vty is authenticated */
994 shifted_vline
= vector_init (vector_count(vline
));
996 for (index
= 1; index
< vector_active (vline
); index
++)
997 vector_set_index (shifted_vline
, index
-1, vector_lookup(vline
, index
));
999 ret
= cmd_execute_command_real (shifted_vline
, FILTER_RELAXED
, vty
, cmd
);
1001 vector_free(shifted_vline
);
1006 saved_ret
= ret
= cmd_execute_command_real (vline
, FILTER_RELAXED
, vty
, cmd
);
1011 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1013 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
1014 while (vty
->node
> CONFIG_NODE
)
1016 try_node
= node_parent(try_node
);
1017 vty
->node
= try_node
;
1018 ret
= cmd_execute_command_real (vline
, FILTER_RELAXED
, vty
, cmd
);
1019 if (ret
== CMD_SUCCESS
|| ret
== CMD_WARNING
)
1022 /* no command succeeded, reset the vty to the original node */
1026 /* return command status for original node */
1031 * Execute a given command, matching it strictly against the current node.
1032 * This mode is used when reading config files.
1034 * @param vline Command line input, vector of char* where each element is
1036 * @param vty The vty context in which the command should be executed.
1037 * @param cmd Pointer where the struct cmd_element* of the matched command
1038 * will be stored, if any. May be set to NULL if this info is
1040 * @return The status of the command that has been executed or an error code
1041 * as to why no command could be executed.
1044 cmd_execute_command_strict (vector vline
, struct vty
*vty
,
1045 const struct cmd_element
**cmd
)
1047 return cmd_execute_command_real(vline
, FILTER_STRICT
, vty
, cmd
);
1051 * Parse one line of config, walking up the parse tree attempting to find a match
1053 * @param vty The vty context in which the command should be executed.
1054 * @param cmd Pointer where the struct cmd_element* of the match command
1055 * will be stored, if any. May be set to NULL if this info is
1057 * @param use_daemon Boolean to control whether or not we match on CMD_SUCCESS_DAEMON
1059 * @return The status of the command that has been executed or an error code
1060 * as to why no command could be executed.
1063 command_config_read_one_line (struct vty
*vty
, const struct cmd_element
**cmd
, int use_daemon
)
1069 vline
= cmd_make_strvec (vty
->buf
);
1071 /* In case of comment line */
1075 /* Execute configuration command : this is strict match */
1076 ret
= cmd_execute_command_strict (vline
, vty
, cmd
);
1078 // Climb the tree and try the command again at each node
1079 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1080 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1081 ret
!= CMD_SUCCESS
&&
1082 ret
!= CMD_WARNING
&&
1083 vty
->node
!= CONFIG_NODE
) {
1085 saved_node
= vty
->node
;
1087 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1088 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1089 ret
!= CMD_SUCCESS
&&
1090 ret
!= CMD_WARNING
&&
1091 vty
->node
> CONFIG_NODE
) {
1092 vty
->node
= node_parent(vty
->node
);
1093 ret
= cmd_execute_command_strict (vline
, vty
, cmd
);
1096 // If climbing the tree did not work then ignore the command and
1097 // stay at the same node
1098 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1099 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1100 ret
!= CMD_SUCCESS
&&
1103 vty
->node
= saved_node
;
1107 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1108 memcpy (vty
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
1110 cmd_free_strvec (vline
);
1115 /* Configuration make from file. */
1117 config_from_file (struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
1119 int ret
, error_ret
=0;
1122 while (fgets (vty
->buf
, VTY_BUFSIZ
, fp
))
1127 ret
= command_config_read_one_line (vty
, NULL
, 0);
1129 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
&&
1130 ret
!= CMD_ERR_NOTHING_TODO
)
1141 /* Configuration from terminal */
1142 DEFUN (config_terminal
,
1143 config_terminal_cmd
,
1144 "configure terminal",
1145 "Configuration from vty interface\n"
1146 "Configuration terminal\n")
1148 if (vty_config_lock (vty
))
1149 vty
->node
= CONFIG_NODE
;
1152 vty_outln (vty
, "VTY configuration is locked by other VTY");
1158 /* Enable command */
1162 "Turn on privileged mode command\n")
1164 /* If enable password is NULL, change to ENABLE_NODE */
1165 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
) ||
1166 vty
->type
== VTY_SHELL_SERV
)
1167 vty
->node
= ENABLE_NODE
;
1169 vty
->node
= AUTH_ENABLE_NODE
;
1174 /* Disable command */
1178 "Turn off privileged mode command\n")
1180 if (vty
->node
== ENABLE_NODE
)
1181 vty
->node
= VIEW_NODE
;
1185 /* Down vty node level. */
1189 "Exit current mode and down to previous mode\n")
1196 cmd_exit (struct vty
*vty
)
1202 if (vty_shell (vty
))
1205 vty
->status
= VTY_CLOSE
;
1208 vty
->node
= ENABLE_NODE
;
1209 vty_config_unlock (vty
);
1211 case INTERFACE_NODE
:
1223 case LDP_L2VPN_NODE
:
1230 vty
->node
= CONFIG_NODE
;
1233 case BGP_IPV4M_NODE
:
1234 case BGP_IPV4L_NODE
:
1235 case BGP_VPNV4_NODE
:
1236 case BGP_VPNV6_NODE
:
1237 case BGP_VRF_POLICY_NODE
:
1238 case BGP_VNC_DEFAULTS_NODE
:
1239 case BGP_VNC_NVE_GROUP_NODE
:
1240 case BGP_VNC_L2_GROUP_NODE
:
1242 case BGP_IPV6M_NODE
:
1244 case BGP_IPV6L_NODE
:
1245 vty
->node
= BGP_NODE
;
1249 vty
->node
= LDP_NODE
;
1251 case LDP_IPV4_IFACE_NODE
:
1252 vty
->node
= LDP_IPV4_NODE
;
1254 case LDP_IPV6_IFACE_NODE
:
1255 vty
->node
= LDP_IPV6_NODE
;
1257 case LDP_PSEUDOWIRE_NODE
:
1258 vty
->node
= LDP_L2VPN_NODE
;
1260 case KEYCHAIN_KEY_NODE
:
1261 vty
->node
= KEYCHAIN_NODE
;
1263 case LINK_PARAMS_NODE
:
1264 vty
->node
= INTERFACE_NODE
;
1275 "Exit current mode and down to previous mode\n")
1277 return config_exit (self
, vty
, argc
, argv
);
1281 /* End of configuration. */
1285 "End current mode and change to enable mode.")
1291 /* Nothing to do. */
1294 case INTERFACE_NODE
:
1303 case BGP_VRF_POLICY_NODE
:
1304 case BGP_VNC_DEFAULTS_NODE
:
1305 case BGP_VNC_NVE_GROUP_NODE
:
1306 case BGP_VNC_L2_GROUP_NODE
:
1307 case BGP_VPNV4_NODE
:
1308 case BGP_VPNV6_NODE
:
1310 case BGP_IPV4M_NODE
:
1311 case BGP_IPV4L_NODE
:
1313 case BGP_IPV6M_NODE
:
1315 case BGP_IPV6L_NODE
:
1322 case LDP_IPV4_IFACE_NODE
:
1323 case LDP_IPV6_IFACE_NODE
:
1324 case LDP_L2VPN_NODE
:
1325 case LDP_PSEUDOWIRE_NODE
:
1328 case KEYCHAIN_KEY_NODE
:
1332 case LINK_PARAMS_NODE
:
1333 vty_config_unlock (vty
);
1334 vty
->node
= ENABLE_NODE
;
1343 DEFUN (show_version
,
1347 "Displays zebra version\n")
1349 vty_outln (vty
, "%s %s (%s).", FRR_FULL_NAME
, FRR_VERSION
,
1350 host
.name
? host
.name
: "");
1351 vty_outln (vty
, "%s%s", FRR_COPYRIGHT
, GIT_INFO
);
1352 vty_outln (vty
, "configured with:%s %s", VTYNL
,
1358 /* "Set" version ... ignore version tags */
1359 DEFUN (frr_version_defaults
,
1360 frr_version_defaults_cmd
,
1361 "frr <version|defaults> LINE...",
1362 "FRRouting global parameters\n"
1363 "version configuration was written by\n"
1364 "set of configuration defaults used\n"
1370 /* Help display function for all node. */
1374 "Description of the interactive help system\n")
1377 "Quagga VTY provides advanced help feature. When you need help,%s\
1378 anytime at the command line please press '?'.%s\
1380 If nothing matches, the help list will be empty and you must backup%s\
1381 until entering a '?' shows the available options.%s\
1382 Two styles of help are provided:%s\
1383 1. Full help is available when you are ready to enter a%s\
1384 command argument (e.g. 'show ?') and describes each possible%s\
1386 2. Partial help is provided when an abbreviated argument is entered%s\
1387 and you want to know what arguments match the input%s\
1388 (e.g. 'show me?'.)%s", VTYNL
, VTYNL
, VTYNL
,
1389 VTYNL
, VTYNL
, VTYNL
, VTYNL
, VTYNL
,
1390 VTYNL
, VTYNL
, VTYNL
, VTYNL
);
1395 permute (struct graph_node
*start
, struct vty
*vty
)
1397 static struct list
*position
= NULL
;
1398 if (!position
) position
= list_new ();
1400 struct cmd_token
*stok
= start
->data
;
1401 struct graph_node
*gnn
;
1402 struct listnode
*ln
;
1405 listnode_add (position
, start
);
1406 for (unsigned int i
= 0; i
< vector_active (start
->to
); i
++)
1408 struct graph_node
*gn
= vector_slot (start
->to
, i
);
1409 struct cmd_token
*tok
= gn
->data
;
1410 if (tok
->attr
== CMD_ATTR_HIDDEN
||
1411 tok
->attr
== CMD_ATTR_DEPRECATED
)
1413 else if (tok
->type
== END_TKN
|| gn
== start
)
1416 for (ALL_LIST_ELEMENTS_RO (position
,ln
,gnn
))
1418 struct cmd_token
*tt
= gnn
->data
;
1419 if (tt
->type
< SPECIAL_TKN
)
1420 vty_out (vty
, " %s", tt
->text
);
1423 vty_out (vty
, "...");
1424 vty_out (vty
, VTYNL
);
1429 if (stok
->type
== FORK_TKN
&& tok
->type
!= FORK_TKN
)
1430 for (ALL_LIST_ELEMENTS_RO (position
, ln
, gnn
))
1440 list_delete_node (position
, listtail(position
));
1444 cmd_list_cmds (struct vty
*vty
, int do_permute
)
1446 struct cmd_node
*node
= vector_slot (cmdvec
, vty
->node
);
1449 permute (vector_slot (node
->cmdgraph
->nodes
, 0), vty
);
1452 /* loop over all commands at this node */
1453 struct cmd_element
*element
= NULL
;
1454 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
); i
++)
1455 if ((element
= vector_slot (node
->cmd_vector
, i
)) &&
1456 element
->attr
!= CMD_ATTR_DEPRECATED
&&
1457 element
->attr
!= CMD_ATTR_HIDDEN
)
1458 vty_outln (vty
, " %s", element
->string
);
1463 /* Help display function for all node. */
1466 "list [permutations]",
1467 "Print command list\n"
1468 "Print all possible command permutations\n")
1470 return cmd_list_cmds (vty
, argc
== 2);
1473 DEFUN (show_commandtree
,
1474 show_commandtree_cmd
,
1475 "show commandtree [permutations]",
1477 "Show command tree\n"
1478 "Permutations that we are interested in\n")
1480 return cmd_list_cmds (vty
, argc
== 3);
1484 vty_write_config (struct vty
*vty
)
1487 struct cmd_node
*node
;
1489 if (vty
->type
== VTY_TERM
)
1491 vty_outln (vty
, "%sCurrent configuration:",VTYNL
);
1492 vty_outln (vty
, "!");
1495 vty_outln (vty
, "frr version %s", FRR_VER_SHORT
);
1496 vty_outln (vty
, "frr defaults %s", DFLT_NAME
);
1497 vty_outln (vty
, "!");
1499 for (i
= 0; i
< vector_active (cmdvec
); i
++)
1500 if ((node
= vector_slot (cmdvec
, i
)) && node
->func
1501 && (node
->vtysh
|| vty
->type
!= VTY_SHELL
))
1503 if ((*node
->func
) (vty
))
1504 vty_outln (vty
, "!");
1507 if (vty
->type
== VTY_TERM
)
1509 vty_outln (vty
, "end");
1513 /* Write current configuration into file. */
1515 DEFUN (config_write
,
1517 "write [<file|memory|terminal>]",
1518 "Write running configuration to memory, network, or terminal\n"
1519 "Write to configuration file\n"
1520 "Write configuration currently in memory\n"
1521 "Write configuration to terminal\n")
1525 char *config_file
, *slash
;
1526 char *config_file_tmp
= NULL
;
1527 char *config_file_sav
= NULL
;
1528 int ret
= CMD_WARNING
;
1529 struct vty
*file_vty
;
1530 struct stat conf_stat
;
1532 // if command was 'write terminal' or 'show running-config'
1533 if (argc
== 2 && (strmatch(argv
[idx_type
]->text
, "terminal") ||
1534 strmatch(argv
[0]->text
, "show")))
1536 vty_write_config (vty
);
1543 /* Check and see if we are operating under vtysh configuration */
1544 if (host
.config
== NULL
)
1546 vty_outln (vty
,"Can't save to configuration file, using vtysh.");
1551 config_file
= host
.config
;
1554 #define O_DIRECTORY 0
1556 slash
= strrchr (config_file
, '/');
1559 char *config_dir
= XSTRDUP (MTYPE_TMP
, config_file
);
1560 config_dir
[slash
- config_file
] = '\0';
1561 dirfd
= open(config_dir
, O_DIRECTORY
| O_RDONLY
);
1562 XFREE (MTYPE_TMP
, config_dir
);
1565 dirfd
= open(".", O_DIRECTORY
| O_RDONLY
);
1566 /* if dirfd is invalid, directory sync fails, but we're still OK */
1569 XMALLOC (MTYPE_TMP
, strlen (config_file
) + strlen (CONF_BACKUP_EXT
) + 1);
1570 strcpy (config_file_sav
, config_file
);
1571 strcat (config_file_sav
, CONF_BACKUP_EXT
);
1574 config_file_tmp
= XMALLOC (MTYPE_TMP
, strlen (config_file
) + 8);
1575 sprintf (config_file_tmp
, "%s.XXXXXX", config_file
);
1577 /* Open file to configuration write. */
1578 fd
= mkstemp (config_file_tmp
);
1581 vty_outln (vty
, "Can't open configuration file %s.",config_file_tmp
);
1584 if (fchmod (fd
, CONFIGFILE_MASK
) != 0)
1586 vty_outln (vty
, "Can't chmod configuration file %s: %s (%d).",
1587 config_file_tmp
, safe_strerror(errno
), errno
);
1591 /* Make vty for configuration file. */
1592 file_vty
= vty_new ();
1594 file_vty
->type
= VTY_FILE
;
1596 /* Config file header print. */
1597 vty_out (file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1598 vty_time_print (file_vty
, 1);
1599 vty_out (file_vty
, "!\n");
1600 vty_write_config (file_vty
);
1601 vty_close (file_vty
);
1603 if (stat(config_file
, &conf_stat
) >= 0)
1605 if (unlink (config_file_sav
) != 0)
1606 if (errno
!= ENOENT
)
1608 vty_outln (vty
, "Can't unlink backup configuration file %s.",
1612 if (link (config_file
, config_file_sav
) != 0)
1614 vty_outln (vty
, "Can't backup old configuration file %s.",
1621 if (rename (config_file_tmp
, config_file
) != 0)
1623 vty_outln (vty
, "Can't save configuration file %s.",config_file
);
1629 vty_outln (vty
, "Configuration saved to %s",config_file
);
1633 if (ret
!= CMD_SUCCESS
)
1634 unlink (config_file_tmp
);
1637 XFREE (MTYPE_TMP
, config_file_tmp
);
1638 XFREE (MTYPE_TMP
, config_file_sav
);
1642 /* ALIAS_FIXME for 'write <terminal|memory>' */
1643 DEFUN (show_running_config
,
1644 show_running_config_cmd
,
1645 "show running-config",
1647 "running configuration (same as write terminal/memory)\n")
1649 return config_write (self
, vty
, argc
, argv
);
1652 /* ALIAS_FIXME for 'write file' */
1653 DEFUN (copy_runningconf_startupconf
,
1654 copy_runningconf_startupconf_cmd
,
1655 "copy running-config startup-config",
1656 "Copy configuration\n"
1657 "Copy running config to... \n"
1658 "Copy running config to startup config (same as write file)\n")
1661 vty_write_config (vty
);
1666 /* Write startup configuration into the terminal. */
1667 DEFUN (show_startup_config
,
1668 show_startup_config_cmd
,
1669 "show startup-config",
1671 "Contents of startup configuration\n")
1678 if (host
.config
== NULL
)
1681 confp
= fopen (host
.config
, "r");
1684 vty_outln (vty
, "Can't open configuration file [%s] due to '%s'",
1685 host
.config
, safe_strerror(errno
));
1689 while (fgets (buf
, BUFSIZ
, confp
))
1693 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1697 vty_outln (vty
, "%s", buf
);
1706 cmd_hostname_set (const char *hostname
)
1708 XFREE (MTYPE_HOST
, host
.name
);
1709 host
.name
= hostname
? XSTRDUP (MTYPE_HOST
, hostname
) : NULL
;
1713 /* Hostname configuration */
1714 DEFUN (config_hostname
,
1717 "Set system's network name\n"
1718 "This system's network name\n")
1720 struct cmd_token
*word
= argv
[1];
1722 if (!isalpha((int) word
->arg
[0]))
1724 vty_outln (vty
, "Please specify string starting with alphabet");
1728 return cmd_hostname_set (word
->arg
);
1731 DEFUN (config_no_hostname
,
1733 "no hostname [HOSTNAME]",
1735 "Reset system's network name\n"
1736 "Host name of this router\n")
1738 return cmd_hostname_set (NULL
);
1741 /* VTY interface password set. */
1742 DEFUN (config_password
,
1744 "password [(8-8)] WORD",
1745 "Assign the terminal connection password\n"
1746 "Specifies a HIDDEN password will follow\n"
1747 "The password string\n")
1751 if (argc
== 3) // '8' was specified
1754 XFREE (MTYPE_HOST
, host
.password
);
1755 host
.password
= NULL
;
1756 if (host
.password_encrypt
)
1757 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1758 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, argv
[idx_word
]->arg
);
1762 if (!isalnum (argv
[idx_8
]->arg
[0]))
1765 "Please specify string starting with alphanumeric");
1770 XFREE (MTYPE_HOST
, host
.password
);
1771 host
.password
= NULL
;
1775 if (host
.password_encrypt
)
1776 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1777 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (argv
[idx_8
]->arg
));
1780 host
.password
= XSTRDUP (MTYPE_HOST
, argv
[idx_8
]->arg
);
1785 /* VTY enable password set. */
1786 DEFUN (config_enable_password
,
1787 enable_password_cmd
,
1788 "enable password [(8-8)] WORD",
1789 "Modify enable password parameters\n"
1790 "Assign the privileged level password\n"
1791 "Specifies a HIDDEN password will follow\n"
1792 "The HIDDEN 'enable' password string\n")
1797 /* Crypt type is specified. */
1800 if (argv
[idx_8
]->arg
[0] == '8')
1803 XFREE (MTYPE_HOST
, host
.enable
);
1806 if (host
.enable_encrypt
)
1807 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1808 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, argv
[idx_word
]->arg
);
1814 vty_outln (vty
, "Unknown encryption type.");
1819 if (!isalnum (argv
[idx_8
]->arg
[0]))
1822 "Please specify string starting with alphanumeric");
1827 XFREE (MTYPE_HOST
, host
.enable
);
1830 /* Plain password input. */
1833 if (host
.enable_encrypt
)
1834 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1835 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (argv
[idx_8
]->arg
));
1838 host
.enable
= XSTRDUP (MTYPE_HOST
, argv
[idx_8
]->arg
);
1843 /* VTY enable password delete. */
1844 DEFUN (no_config_enable_password
,
1845 no_enable_password_cmd
,
1846 "no enable password",
1848 "Modify enable password parameters\n"
1849 "Assign the privileged level password\n")
1852 XFREE (MTYPE_HOST
, host
.enable
);
1855 if (host
.enable_encrypt
)
1856 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1857 host
.enable_encrypt
= NULL
;
1862 DEFUN (service_password_encrypt
,
1863 service_password_encrypt_cmd
,
1864 "service password-encryption",
1865 "Set up miscellaneous service\n"
1866 "Enable encrypted passwords\n")
1875 if (host
.password_encrypt
)
1876 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1877 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (host
.password
));
1881 if (host
.enable_encrypt
)
1882 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1883 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (host
.enable
));
1889 DEFUN (no_service_password_encrypt
,
1890 no_service_password_encrypt_cmd
,
1891 "no service password-encryption",
1893 "Set up miscellaneous service\n"
1894 "Enable encrypted passwords\n")
1901 if (host
.password_encrypt
)
1902 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1903 host
.password_encrypt
= NULL
;
1905 if (host
.enable_encrypt
)
1906 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1907 host
.enable_encrypt
= NULL
;
1912 DEFUN (config_terminal_length
,
1913 config_terminal_length_cmd
,
1914 "terminal length (0-512)",
1915 "Set terminal line parameters\n"
1916 "Set number of lines on a screen\n"
1917 "Number of lines on screen (0 for no pausing)\n")
1921 vty
->lines
= atoi (argv
[idx_number
]->arg
);
1926 DEFUN (config_terminal_no_length
,
1927 config_terminal_no_length_cmd
,
1928 "terminal no length",
1929 "Set terminal line parameters\n"
1931 "Set number of lines on a screen\n")
1937 DEFUN (service_terminal_length
,
1938 service_terminal_length_cmd
,
1939 "service terminal-length (0-512)",
1940 "Set up miscellaneous service\n"
1941 "System wide terminal length configuration\n"
1942 "Number of lines of VTY (0 means no line control)\n")
1946 host
.lines
= atoi (argv
[idx_number
]->arg
);
1951 DEFUN (no_service_terminal_length
,
1952 no_service_terminal_length_cmd
,
1953 "no service terminal-length [(0-512)]",
1955 "Set up miscellaneous service\n"
1956 "System wide terminal length configuration\n"
1957 "Number of lines of VTY (0 means no line control)\n")
1963 DEFUN_HIDDEN (do_echo
,
1966 "Echo a message back to the vty\n"
1967 "The message to echo\n")
1971 vty_outln (vty
, "%s",
1972 ((message
= argv_concat(argv
, argc
, 1)) ? message
: ""));
1974 XFREE(MTYPE_TMP
, message
);
1978 DEFUN (config_logmsg
,
1980 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
1981 "Send a message to enabled logging destinations\n"
1983 "The message to send\n")
1985 int idx_log_level
= 1;
1986 int idx_message
= 2;
1990 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
1991 return CMD_ERR_NO_MATCH
;
1993 zlog(level
, "%s", ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
1995 XFREE(MTYPE_TMP
, message
);
2000 DEFUN (show_logging
,
2004 "Show current logging configuration\n")
2006 struct zlog
*zl
= zlog_default
;
2008 vty_out (vty
, "Syslog logging: ");
2009 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
2010 vty_out (vty
, "disabled");
2012 vty_out (vty
, "level %s, facility %s, ident %s",
2013 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
2014 facility_name(zl
->facility
), zl
->ident
);
2015 vty_out (vty
, VTYNL
);
2017 vty_out (vty
, "Stdout logging: ");
2018 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
2019 vty_out (vty
, "disabled");
2021 vty_out (vty
, "level %s",
2022 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
2023 vty_out (vty
, VTYNL
);
2025 vty_out (vty
, "Monitor logging: ");
2026 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
2027 vty_out (vty
, "disabled");
2029 vty_out (vty
, "level %s",
2030 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
2031 vty_out (vty
, VTYNL
);
2033 vty_out (vty
, "File logging: ");
2034 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) ||
2036 vty_out (vty
, "disabled");
2038 vty_out (vty
, "level %s, filename %s",
2039 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
2041 vty_out (vty
, VTYNL
);
2043 vty_outln (vty
, "Protocol name: %s",
2045 vty_outln (vty
, "Record priority: %s",
2046 (zl
->record_priority
? "enabled" : "disabled"));
2047 vty_outln (vty
, "Timestamp precision: %d",
2048 zl
->timestamp_precision
);
2053 DEFUN (config_log_stdout
,
2054 config_log_stdout_cmd
,
2055 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2057 "Set stdout logging level\n"
2060 int idx_log_level
= 2;
2062 if (argc
== idx_log_level
)
2064 zlog_set_level (ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2069 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2070 return CMD_ERR_NO_MATCH
;
2071 zlog_set_level (ZLOG_DEST_STDOUT
, level
);
2075 DEFUN (no_config_log_stdout
,
2076 no_config_log_stdout_cmd
,
2077 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2080 "Cancel logging to stdout\n"
2083 zlog_set_level (ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
2087 DEFUN (config_log_monitor
,
2088 config_log_monitor_cmd
,
2089 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2091 "Set terminal line (monitor) logging level\n"
2094 int idx_log_level
= 2;
2096 if (argc
== idx_log_level
)
2098 zlog_set_level (ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
2103 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2104 return CMD_ERR_NO_MATCH
;
2105 zlog_set_level (ZLOG_DEST_MONITOR
, level
);
2109 DEFUN (no_config_log_monitor
,
2110 no_config_log_monitor_cmd
,
2111 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2114 "Disable terminal line (monitor) logging\n"
2117 zlog_set_level (ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
2122 set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
2126 const char *fullpath
;
2128 /* Path detection. */
2129 if (! IS_DIRECTORY_SEP (*fname
))
2131 char cwd
[MAXPATHLEN
+1];
2132 cwd
[MAXPATHLEN
] = '\0';
2134 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
2136 zlog_err ("config_log_file: Unable to alloc mem!");
2140 if ( (p
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + strlen (fname
) + 2))
2143 zlog_err ("config_log_file: Unable to alloc mem!");
2146 sprintf (p
, "%s/%s", cwd
, fname
);
2152 ret
= zlog_set_file (fullpath
, loglevel
);
2155 XFREE (MTYPE_TMP
, p
);
2159 vty_out (vty
, "can't open logfile %s\n", fname
);
2164 XFREE (MTYPE_HOST
, host
.logfile
);
2166 host
.logfile
= XSTRDUP (MTYPE_HOST
, fname
);
2168 #if defined(HAVE_CUMULUS)
2169 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2170 zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] = ZLOG_DISABLED
;
2175 DEFUN (config_log_file
,
2176 config_log_file_cmd
,
2177 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2180 "Logging filename\n"
2183 int idx_filename
= 2;
2184 int idx_log_levels
= 3;
2188 if ((level
= level_match(argv
[idx_log_levels
]->arg
)) == ZLOG_DISABLED
)
2189 return CMD_ERR_NO_MATCH
;
2190 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2193 return set_log_file(vty
, argv
[idx_filename
]->arg
, zlog_default
->default_lvl
);
2196 DEFUN (no_config_log_file
,
2197 no_config_log_file_cmd
,
2198 "no log file [FILENAME [LEVEL]]",
2201 "Cancel logging to file\n"
2202 "Logging file name\n"
2203 "Logging file name\n"
2209 XFREE (MTYPE_HOST
, host
.logfile
);
2211 host
.logfile
= NULL
;
2216 DEFUN (config_log_syslog
,
2217 config_log_syslog_cmd
,
2218 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2220 "Set syslog logging level\n"
2223 int idx_log_levels
= 2;
2227 if ((level
= level_match (argv
[idx_log_levels
]->arg
)) == ZLOG_DISABLED
)
2228 return CMD_ERR_NO_MATCH
;
2229 zlog_set_level (ZLOG_DEST_SYSLOG
, level
);
2234 zlog_set_level (ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2239 DEFUN (no_config_log_syslog
,
2240 no_config_log_syslog_cmd
,
2241 "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>]",
2244 "Cancel logging to syslog\n"
2248 zlog_set_level (ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2252 DEFUN (config_log_facility
,
2253 config_log_facility_cmd
,
2254 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2256 "Facility parameter for syslog messages\n"
2260 int facility
= facility_match(argv
[idx_target
]->arg
);
2262 zlog_default
->facility
= facility
;
2266 DEFUN (no_config_log_facility
,
2267 no_config_log_facility_cmd
,
2268 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2271 "Reset syslog facility to default (daemon)\n"
2274 zlog_default
->facility
= LOG_DAEMON
;
2278 DEFUN_DEPRECATED (config_log_trap
,
2279 config_log_trap_cmd
,
2280 "log trap <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>",
2282 "(Deprecated) Set logging level and default for all destinations\n"
2288 if ((new_level
= level_match(argv
[2]->arg
)) == ZLOG_DISABLED
)
2289 return CMD_ERR_NO_MATCH
;
2291 zlog_default
->default_lvl
= new_level
;
2292 for (i
= 0; i
< ZLOG_NUM_DESTS
; i
++)
2293 if (zlog_default
->maxlvl
[i
] != ZLOG_DISABLED
)
2294 zlog_default
->maxlvl
[i
] = new_level
;
2298 DEFUN_DEPRECATED (no_config_log_trap
,
2299 no_config_log_trap_cmd
,
2300 "no log trap [emergencies|alerts|critical|errors|warnings|notifications|informational|debugging]",
2303 "Permit all logging information\n"
2306 zlog_default
->default_lvl
= LOG_DEBUG
;
2310 DEFUN (config_log_record_priority
,
2311 config_log_record_priority_cmd
,
2312 "log record-priority",
2314 "Log the priority of the message within the message\n")
2316 zlog_default
->record_priority
= 1 ;
2320 DEFUN (no_config_log_record_priority
,
2321 no_config_log_record_priority_cmd
,
2322 "no log record-priority",
2325 "Do not log the priority of the message within the message\n")
2327 zlog_default
->record_priority
= 0 ;
2331 DEFUN (config_log_timestamp_precision
,
2332 config_log_timestamp_precision_cmd
,
2333 "log timestamp precision (0-6)",
2335 "Timestamp configuration\n"
2336 "Set the timestamp precision\n"
2337 "Number of subsecond digits\n")
2340 zlog_default
->timestamp_precision
= strtoul(argv
[idx_number
]->arg
, NULL
, 10);
2344 DEFUN (no_config_log_timestamp_precision
,
2345 no_config_log_timestamp_precision_cmd
,
2346 "no log timestamp precision",
2349 "Timestamp configuration\n"
2350 "Reset the timestamp precision to the default value of 0\n")
2352 zlog_default
->timestamp_precision
= 0 ;
2357 cmd_banner_motd_file (const char *file
)
2359 int success
= CMD_SUCCESS
;
2364 rpath
= realpath (file
, p
);
2366 return CMD_ERR_NO_FILE
;
2367 in
= strstr (rpath
, SYSCONFDIR
);
2371 XFREE (MTYPE_HOST
, host
.motdfile
);
2372 host
.motdfile
= XSTRDUP (MTYPE_HOST
, file
);
2375 success
= CMD_WARNING
;
2380 DEFUN (banner_motd_file
,
2381 banner_motd_file_cmd
,
2382 "banner motd file FILE",
2385 "Banner from a file\n"
2389 const char *filename
= argv
[idx_file
]->arg
;
2390 int cmd
= cmd_banner_motd_file (filename
);
2392 if (cmd
== CMD_ERR_NO_FILE
)
2393 vty_out (vty
, "%s does not exist", filename
);
2394 else if (cmd
== CMD_WARNING
)
2395 vty_out (vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2400 DEFUN (banner_motd_default
,
2401 banner_motd_default_cmd
,
2402 "banner motd default",
2403 "Set banner string\n"
2404 "Strings for motd\n"
2407 host
.motd
= default_motd
;
2411 DEFUN (no_banner_motd
,
2415 "Set banner string\n"
2416 "Strings for motd\n")
2420 XFREE (MTYPE_HOST
, host
.motdfile
);
2421 host
.motdfile
= NULL
;
2425 /* Set config filename. Called from vty.c */
2427 host_config_set (const char *filename
)
2430 XFREE (MTYPE_HOST
, host
.config
);
2431 host
.config
= XSTRDUP (MTYPE_HOST
, filename
);
2435 host_config_get (void)
2441 install_default (enum node_type node
)
2443 install_element (node
, &config_exit_cmd
);
2444 install_element (node
, &config_quit_cmd
);
2445 install_element (node
, &config_end_cmd
);
2446 install_element (node
, &config_help_cmd
);
2447 install_element (node
, &config_list_cmd
);
2449 install_element (node
, &config_write_cmd
);
2450 install_element (node
, &show_running_config_cmd
);
2452 install_element (node
, &autocomplete_cmd
);
2455 /* Initialize command interface. Install basic nodes and commands.
2457 * terminal = 0 -- vtysh / no logging, no config control
2458 * terminal = 1 -- normal daemon
2459 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2461 cmd_init (int terminal
)
2465 varhandlers
= list_new ();
2467 /* Allocate initial top vector of commands. */
2468 cmdvec
= vector_init (VECTOR_MIN_SIZE
);
2470 /* Default host value settings. */
2472 host
.password
= NULL
;
2474 host
.logfile
= NULL
;
2476 host
.noconfig
= (terminal
< 0);
2478 host
.motd
= default_motd
;
2479 host
.motdfile
= NULL
;
2481 /* Install top nodes. */
2482 install_node (&view_node
, NULL
);
2483 install_node (&enable_node
, NULL
);
2484 install_node (&auth_node
, NULL
);
2485 install_node (&auth_enable_node
, NULL
);
2486 install_node (&config_node
, config_write_host
);
2488 /* Each node's basic commands. */
2489 install_element (VIEW_NODE
, &show_version_cmd
);
2492 install_element (VIEW_NODE
, &config_list_cmd
);
2493 install_element (VIEW_NODE
, &config_exit_cmd
);
2494 install_element (VIEW_NODE
, &config_quit_cmd
);
2495 install_element (VIEW_NODE
, &config_help_cmd
);
2496 install_element (VIEW_NODE
, &config_enable_cmd
);
2497 install_element (VIEW_NODE
, &config_terminal_length_cmd
);
2498 install_element (VIEW_NODE
, &config_terminal_no_length_cmd
);
2499 install_element (VIEW_NODE
, &show_logging_cmd
);
2500 install_element (VIEW_NODE
, &show_commandtree_cmd
);
2501 install_element (VIEW_NODE
, &echo_cmd
);
2502 install_element (VIEW_NODE
, &autocomplete_cmd
);
2507 install_element (ENABLE_NODE
, &config_end_cmd
);
2508 install_element (ENABLE_NODE
, &config_disable_cmd
);
2509 install_element (ENABLE_NODE
, &config_terminal_cmd
);
2510 install_element (ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2511 install_element (ENABLE_NODE
, &config_write_cmd
);
2512 install_element (ENABLE_NODE
, &show_running_config_cmd
);
2514 install_element (ENABLE_NODE
, &show_startup_config_cmd
);
2518 install_element (ENABLE_NODE
, &config_logmsg_cmd
);
2519 install_default (CONFIG_NODE
);
2522 workqueue_cmd_init ();
2525 install_element (CONFIG_NODE
, &hostname_cmd
);
2526 install_element (CONFIG_NODE
, &no_hostname_cmd
);
2527 install_element (CONFIG_NODE
, &frr_version_defaults_cmd
);
2531 install_element (CONFIG_NODE
, &password_cmd
);
2532 install_element (CONFIG_NODE
, &enable_password_cmd
);
2533 install_element (CONFIG_NODE
, &no_enable_password_cmd
);
2535 install_element (CONFIG_NODE
, &config_log_stdout_cmd
);
2536 install_element (CONFIG_NODE
, &no_config_log_stdout_cmd
);
2537 install_element (CONFIG_NODE
, &config_log_monitor_cmd
);
2538 install_element (CONFIG_NODE
, &no_config_log_monitor_cmd
);
2539 install_element (CONFIG_NODE
, &config_log_file_cmd
);
2540 install_element (CONFIG_NODE
, &no_config_log_file_cmd
);
2541 install_element (CONFIG_NODE
, &config_log_syslog_cmd
);
2542 install_element (CONFIG_NODE
, &no_config_log_syslog_cmd
);
2543 install_element (CONFIG_NODE
, &config_log_facility_cmd
);
2544 install_element (CONFIG_NODE
, &no_config_log_facility_cmd
);
2545 install_element (CONFIG_NODE
, &config_log_trap_cmd
);
2546 install_element (CONFIG_NODE
, &no_config_log_trap_cmd
);
2547 install_element (CONFIG_NODE
, &config_log_record_priority_cmd
);
2548 install_element (CONFIG_NODE
, &no_config_log_record_priority_cmd
);
2549 install_element (CONFIG_NODE
, &config_log_timestamp_precision_cmd
);
2550 install_element (CONFIG_NODE
, &no_config_log_timestamp_precision_cmd
);
2551 install_element (CONFIG_NODE
, &service_password_encrypt_cmd
);
2552 install_element (CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2553 install_element (CONFIG_NODE
, &banner_motd_default_cmd
);
2554 install_element (CONFIG_NODE
, &banner_motd_file_cmd
);
2555 install_element (CONFIG_NODE
, &no_banner_motd_cmd
);
2556 install_element (CONFIG_NODE
, &service_terminal_length_cmd
);
2557 install_element (CONFIG_NODE
, &no_service_terminal_length_cmd
);
2559 vrf_install_commands ();
2563 grammar_sandbox_init();
2570 struct cmd_node
*cmd_node
;
2574 for (unsigned int i
= 0; i
< vector_active (cmdvec
); i
++)
2575 if ((cmd_node
= vector_slot (cmdvec
, i
)) != NULL
)
2577 // deleting the graph delets the cmd_element as well
2578 graph_delete_graph (cmd_node
->cmdgraph
);
2579 vector_free (cmd_node
->cmd_vector
);
2580 hash_clean (cmd_node
->cmd_hash
, NULL
);
2581 hash_free (cmd_node
->cmd_hash
);
2582 cmd_node
->cmd_hash
= NULL
;
2585 vector_free (cmdvec
);
2590 XFREE (MTYPE_HOST
, host
.name
);
2592 XFREE (MTYPE_HOST
, host
.password
);
2593 if (host
.password_encrypt
)
2594 XFREE (MTYPE_HOST
, host
.password_encrypt
);
2596 XFREE (MTYPE_HOST
, host
.enable
);
2597 if (host
.enable_encrypt
)
2598 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
2600 XFREE (MTYPE_HOST
, host
.logfile
);
2602 XFREE (MTYPE_HOST
, host
.motdfile
);
2604 XFREE (MTYPE_HOST
, host
.config
);
2606 list_delete (varhandlers
);