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
28 #include <lib/version.h>
39 #include "workqueue.h"
41 #include "command_match.h"
42 #include "command_graph.h"
48 #include "lib_errors.h"
49 #include "northbound_cli.h"
51 DEFINE_MTYPE_STATIC(LIB
, HOST
, "Host config")
52 DEFINE_MTYPE(LIB
, COMPLETION
, "Completion item")
59 /* clang-format off */
60 const struct message tokennames
[] = {
65 item(IPV4_PREFIX_TKN
),
67 item(IPV6_PREFIX_TKN
),
77 const char *node_names
[] = {
80 "auth enable", // AUTH_ENABLE_NODE,
81 "enable", // ENABLE_NODE,
82 "config", // CONFIG_NODE,
83 "debug", // DEBUG_NODE,
84 "vrf debug", // VRF_DEBUG_NODE,
85 "northbound debug", // NORTHBOUND_DEBUG_NODE,
86 "vnc debug", // DEBUG_VNC_NODE,
87 "route-map debug", /* RMAP_DEBUG_NODE */
88 "resolver debug", /* RESOLVER_DEBUG_NODE */
90 "keychain", // KEYCHAIN_NODE,
91 "keychain key", // KEYCHAIN_KEY_NODE,
92 "static ip", // IP_NODE,
94 "interface", // INTERFACE_NODE,
95 "nexthop-group", // NH_GROUP_NODE,
96 "zebra", // ZEBRA_NODE,
97 "table", // TABLE_NODE,
99 "ripng", // RIPNG_NODE,
100 "babel", // BABEL_NODE,
101 "eigrp", // EIGRP_NODE,
103 "bgp vpnv4", // BGP_VPNV4_NODE,
104 "bgp vpnv6", // BGP_VPNV6_NODE,
105 "bgp ipv4 unicast", // BGP_IPV4_NODE,
106 "bgp ipv4 multicast", // BGP_IPV4M_NODE,
107 "bgp ipv4 labeled unicast", // BGP_IPV4L_NODE,
108 "bgp ipv6", // BGP_IPV6_NODE,
109 "bgp ipv6 multicast", // BGP_IPV6M_NODE,
110 "bgp ipv6 labeled unicast", // BGP_IPV6L_NODE,
111 "bgp vrf policy", // BGP_VRF_POLICY_NODE,
112 "bgp vnc defaults", // BGP_VNC_DEFAULTS_NODE,
113 "bgp vnc nve", // BGP_VNC_NVE_GROUP_NODE,
114 "bgp vnc l2", // BGP_VNC_L2_GROUP_NODE,
115 "rfp defaults", // RFP_DEFAULTS_NODE,
116 "bgp evpn", // BGP_EVPN_NODE,
117 "ospf", // OSPF_NODE,
118 "ospf6", // OSPF6_NODE,
120 "ldp ipv4", // LDP_IPV4_NODE,
121 "ldp ipv6", // LDP_IPV6_NODE,
122 "ldp ipv4 interface", // LDP_IPV4_IFACE_NODE,
123 "ldp ipv6 interface", // LDP_IPV6_IFACE_NODE,
124 "ldp l2vpn", // LDP_L2VPN_NODE,
125 "ldp", // LDP_PSEUDOWIRE_NODE,
126 "isis", // ISIS_NODE,
127 "ipv4 access list", // ACCESS_NODE,
128 "ipv4 prefix list", // PREFIX_NODE,
129 "ipv6 access list", // ACCESS_IPV6_NODE,
130 "MAC access list", // ACCESS_MAC_NODE,
131 "ipv6 prefix list", // PREFIX_IPV6_NODE,
132 "as list", // AS_LIST_NODE,
133 "community list", // COMMUNITY_LIST_NODE,
134 "routemap", // RMAP_NODE,
135 "pbr-map", // PBRMAP_NODE,
136 "smux", // SMUX_NODE,
137 "dump", // DUMP_NODE,
138 "forwarding", // FORWARDING_NODE,
139 "protocol", // PROTOCOL_NODE,
140 "mpls", // MPLS_NODE,
143 "link-params", // LINK_PARAMS_NODE,
144 "bgp evpn vni", // BGP_EVPN_VNI_NODE,
146 "bgp ipv4 flowspec", /* BGP_FLOWSPECV4_NODE
148 "bgp ipv6 flowspec", /* BGP_FLOWSPECV6_NODE
150 "bfd", /* BFD_NODE */
151 "bfd peer", /* BFD_PEER_NODE */
152 "openfabric", // OPENFABRIC_NODE
153 "vrrp", /* VRRP_NODE */
154 "bmp", /* BMP_NODE */
156 /* clang-format on */
158 /* Command vector which includes some level of command lists. Normally
159 each daemon maintains each own cmdvec. */
160 vector cmdvec
= NULL
;
162 /* Host information structure. */
166 * Returns host.name if any, otherwise
167 * it returns the system hostname.
169 const char *cmd_hostname_get(void)
175 * Returns unix domainname
177 const char *cmd_domainname_get(void)
179 return host
.domainname
;
182 /* Standard command node structures. */
183 static struct cmd_node auth_node
= {
184 AUTH_NODE
, "Password: ",
187 static struct cmd_node view_node
= {
191 static struct cmd_node auth_enable_node
= {
192 AUTH_ENABLE_NODE
, "Password: ",
195 static struct cmd_node enable_node
= {
199 static struct cmd_node config_node
= {CONFIG_NODE
, "%s(config)# ", 1};
201 /* Default motd string. */
202 static const char *default_motd
= FRR_DEFAULT_MOTD
;
204 static const struct facility_map
{
208 } syslog_facilities
[] = {
209 {LOG_KERN
, "kern", 1},
210 {LOG_USER
, "user", 2},
211 {LOG_MAIL
, "mail", 1},
212 {LOG_DAEMON
, "daemon", 1},
213 {LOG_AUTH
, "auth", 1},
214 {LOG_SYSLOG
, "syslog", 1},
216 {LOG_NEWS
, "news", 1},
217 {LOG_UUCP
, "uucp", 2},
218 {LOG_CRON
, "cron", 1},
222 {LOG_LOCAL0
, "local0", 6},
223 {LOG_LOCAL1
, "local1", 6},
224 {LOG_LOCAL2
, "local2", 6},
225 {LOG_LOCAL3
, "local3", 6},
226 {LOG_LOCAL4
, "local4", 6},
227 {LOG_LOCAL5
, "local5", 6},
228 {LOG_LOCAL6
, "local6", 6},
229 {LOG_LOCAL7
, "local7", 6},
233 static const char *facility_name(int facility
)
235 const struct facility_map
*fm
;
237 for (fm
= syslog_facilities
; fm
->name
; fm
++)
238 if (fm
->facility
== facility
)
243 static int facility_match(const char *str
)
245 const struct facility_map
*fm
;
247 for (fm
= syslog_facilities
; fm
->name
; fm
++)
248 if (!strncmp(str
, fm
->name
, fm
->match
))
253 static int level_match(const char *s
)
257 for (level
= 0; zlog_priority
[level
] != NULL
; level
++)
258 if (!strncmp(s
, zlog_priority
[level
], 2))
260 return ZLOG_DISABLED
;
263 /* This is called from main when a daemon is invoked with -v or --version. */
264 void print_version(const char *progname
)
266 printf("%s version %s\n", progname
, FRR_VERSION
);
267 printf("%s\n", FRR_COPYRIGHT
);
268 printf("configured with:\n\t%s\n", FRR_CONFIG_ARGS
);
271 char *argv_concat(struct cmd_token
**argv
, int argc
, int shift
)
273 int cnt
= MAX(argc
- shift
, 0);
274 const char *argstr
[cnt
+ 1];
279 for (int i
= 0; i
< cnt
; i
++)
280 argstr
[i
] = argv
[i
+ shift
]->arg
;
282 return frrstr_join(argstr
, cnt
, " ");
285 vector
cmd_make_strvec(const char *string
)
290 const char *copy
= string
;
292 /* skip leading whitespace */
293 while (isspace((unsigned char)*copy
) && *copy
!= '\0')
296 /* if the entire string was whitespace or a comment, return */
297 if (*copy
== '\0' || *copy
== '!' || *copy
== '#')
300 vector result
= frrstr_split_vec(copy
, "\n\r\t ");
302 for (unsigned int i
= 0; i
< vector_active(result
); i
++) {
303 if (strlen(vector_slot(result
, i
)) == 0) {
304 XFREE(MTYPE_TMP
, vector_slot(result
, i
));
305 vector_unset(result
, i
);
309 vector_compact(result
);
314 void cmd_free_strvec(vector v
)
316 frrstr_strvec_free(v
);
320 * Convenience function for accessing argv data.
324 * @param text definition snippet of the desired token
325 * @param index the starting index, and where to store the
326 * index of the found token if it exists
327 * @return 1 if found, 0 otherwise
329 int argv_find(struct cmd_token
**argv
, int argc
, const char *text
, int *index
)
332 for (int i
= *index
; i
< argc
&& found
== 0; i
++)
333 if ((found
= strmatch(text
, argv
[i
]->text
)))
338 static unsigned int cmd_hash_key(const void *p
)
340 int size
= sizeof(p
);
342 return jhash(p
, size
, 0);
345 static bool cmd_hash_cmp(const void *a
, const void *b
)
350 /* Install top node of command vector. */
351 void install_node(struct cmd_node
*node
, int (*func
)(struct vty
*))
353 vector_set_index(cmdvec
, node
->node
, node
);
355 node
->cmdgraph
= graph_new();
356 node
->cmd_vector
= vector_init(VECTOR_MIN_SIZE
);
358 struct cmd_token
*token
=
359 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
360 graph_new_node(node
->cmdgraph
, token
,
361 (void (*)(void *)) & cmd_token_del
);
362 node
->cmd_hash
= hash_create_size(16, cmd_hash_key
, cmd_hash_cmp
,
366 /* Return prompt character of specified node. */
367 const char *cmd_prompt(enum node_type node
)
369 struct cmd_node
*cnode
;
371 cnode
= vector_slot(cmdvec
, node
);
372 return cnode
->prompt
;
375 /* Install a command into a node. */
376 void install_element(enum node_type ntype
, struct cmd_element
*cmd
)
378 struct cmd_node
*cnode
;
380 /* cmd_init hasn't been called */
382 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
387 cnode
= vector_lookup(cmdvec
, ntype
);
392 "\tnode %d (%s) does not exist.\n"
393 "\tplease call install_node() before install_element()\n",
394 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
398 if (hash_lookup(cnode
->cmd_hash
, cmd
) != NULL
) {
401 "\tnode %d (%s) already has this command installed.\n"
402 "\tduplicate install_element call?\n",
403 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
407 assert(hash_get(cnode
->cmd_hash
, cmd
, hash_alloc_intern
));
409 struct graph
*graph
= graph_new();
410 struct cmd_token
*token
=
411 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
412 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
414 cmd_graph_parse(graph
, cmd
);
415 cmd_graph_names(graph
);
416 cmd_graph_merge(cnode
->cmdgraph
, graph
, +1);
417 graph_delete_graph(graph
);
419 vector_set(cnode
->cmd_vector
, cmd
);
421 if (ntype
== VIEW_NODE
)
422 install_element(ENABLE_NODE
, cmd
);
425 void uninstall_element(enum node_type ntype
, struct cmd_element
*cmd
)
427 struct cmd_node
*cnode
;
429 /* cmd_init hasn't been called */
431 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
436 cnode
= vector_lookup(cmdvec
, ntype
);
441 "\tnode %d (%s) does not exist.\n"
442 "\tplease call install_node() before uninstall_element()\n",
443 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
447 if (hash_release(cnode
->cmd_hash
, cmd
) == NULL
) {
450 "\tnode %d (%s) does not have this command installed.\n"
451 "\tduplicate uninstall_element call?\n",
452 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
456 vector_unset_value(cnode
->cmd_vector
, cmd
);
458 struct graph
*graph
= graph_new();
459 struct cmd_token
*token
=
460 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
461 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
463 cmd_graph_parse(graph
, cmd
);
464 cmd_graph_names(graph
);
465 cmd_graph_merge(cnode
->cmdgraph
, graph
, -1);
466 graph_delete_graph(graph
);
468 if (ntype
== VIEW_NODE
)
469 uninstall_element(ENABLE_NODE
, cmd
);
473 static const unsigned char itoa64
[] =
474 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
476 static void to64(char *s
, long v
, int n
)
479 *s
++ = itoa64
[v
& 0x3f];
484 static char *zencrypt(const char *passwd
)
488 char *crypt(const char *, const char *);
490 gettimeofday(&tv
, 0);
492 to64(&salt
[0], random(), 3);
493 to64(&salt
[3], tv
.tv_usec
, 3);
496 return crypt(passwd
, salt
);
499 /* This function write configuration of this host. */
500 static int config_write_host(struct vty
*vty
)
502 if (cmd_hostname_get())
503 vty_out(vty
, "hostname %s\n", cmd_hostname_get());
505 if (cmd_domainname_get())
506 vty_out(vty
, "domainname %s\n", cmd_domainname_get());
508 /* The following are all configuration commands that are not sent to
509 * watchfrr. For instance watchfrr is hardcoded to log to syslog so
510 * we would always display 'log syslog informational' in the config
511 * which would cause other daemons to then switch to syslog when they
514 if (strcmp(zlog_default
->protoname
, "WATCHFRR")) {
516 if (host
.password_encrypt
)
517 vty_out(vty
, "password 8 %s\n",
518 host
.password_encrypt
);
519 if (host
.enable_encrypt
)
520 vty_out(vty
, "enable password 8 %s\n",
521 host
.enable_encrypt
);
524 vty_out(vty
, "password %s\n", host
.password
);
526 vty_out(vty
, "enable password %s\n",
531 && (zlog_default
->maxlvl
[ZLOG_DEST_FILE
]
533 vty_out(vty
, "log file %s", host
.logfile
);
534 if (zlog_default
->maxlvl
[ZLOG_DEST_FILE
]
535 != zlog_default
->default_lvl
)
538 [zlog_default
->maxlvl
543 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != ZLOG_DISABLED
) {
544 vty_out(vty
, "log stdout");
545 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
]
546 != zlog_default
->default_lvl
)
549 [zlog_default
->maxlvl
550 [ZLOG_DEST_STDOUT
]]);
554 if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
555 vty_out(vty
, "no log monitor\n");
556 else if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]
557 != zlog_default
->default_lvl
)
558 vty_out(vty
, "log monitor %s\n",
559 zlog_priority
[zlog_default
->maxlvl
560 [ZLOG_DEST_MONITOR
]]);
562 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
) {
563 vty_out(vty
, "log syslog");
564 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
]
565 != zlog_default
->default_lvl
)
567 zlog_priority
[zlog_default
->maxlvl
568 [ZLOG_DEST_SYSLOG
]]);
572 if (zlog_default
->facility
!= LOG_DAEMON
)
573 vty_out(vty
, "log facility %s\n",
574 facility_name(zlog_default
->facility
));
576 if (zlog_default
->record_priority
== 1)
577 vty_out(vty
, "log record-priority\n");
579 if (zlog_default
->timestamp_precision
> 0)
580 vty_out(vty
, "log timestamp precision %d\n",
581 zlog_default
->timestamp_precision
);
584 vty_out(vty
, "service advanced-vty\n");
587 vty_out(vty
, "service password-encryption\n");
590 vty_out(vty
, "service terminal-length %d\n",
594 vty_out(vty
, "banner motd file %s\n", host
.motdfile
);
596 vty_out(vty
, "no banner motd\n");
599 if (debug_memstats_at_exit
)
600 vty_out(vty
, "!\ndebug memstats-at-exit\n");
605 /* Utility function for getting command graph. */
606 static struct graph
*cmd_node_graph(vector v
, enum node_type ntype
)
608 struct cmd_node
*cnode
= vector_slot(v
, ntype
);
609 return cnode
->cmdgraph
;
612 static int cmd_try_do_shortcut(enum node_type node
, char *first_word
)
614 if (first_word
!= NULL
&& node
!= AUTH_NODE
&& node
!= VIEW_NODE
615 && node
!= AUTH_ENABLE_NODE
&& 0 == strcmp("do", first_word
))
621 * Compare function for cmd_token.
622 * Used with qsort to sort command completions.
624 static int compare_completions(const void *fst
, const void *snd
)
626 const struct cmd_token
*first
= *(const struct cmd_token
* const *)fst
,
627 *secnd
= *(const struct cmd_token
* const *)snd
;
628 return strcmp(first
->text
, secnd
->text
);
632 * Takes a list of completions returned by command_complete,
633 * dedeuplicates them based on both text and description,
634 * sorts them, and returns them as a vector.
636 * @param completions linked list of cmd_token
637 * @return deduplicated and sorted vector with
639 vector
completions_to_vec(struct list
*completions
)
641 vector comps
= vector_init(VECTOR_MIN_SIZE
);
644 struct cmd_token
*token
, *cr
= NULL
;
645 unsigned int i
, exists
;
646 for (ALL_LIST_ELEMENTS_RO(completions
, ln
, token
)) {
647 if (token
->type
== END_TKN
&& (cr
= token
))
650 // linear search for token in completions vector
652 for (i
= 0; i
< vector_active(comps
) && !exists
; i
++) {
653 struct cmd_token
*curr
= vector_slot(comps
, i
);
655 exists
= !strcmp(curr
->text
, token
->text
)
656 && !strcmp(curr
->desc
, token
->desc
);
658 exists
= !strcmp(curr
->text
, token
->text
);
659 #endif /* VTYSH_DEBUG */
663 vector_set(comps
, token
);
667 qsort(comps
->index
, vector_active(comps
), sizeof(void *),
668 &compare_completions
);
670 // make <cr> the first element, if it is present
672 vector_set_index(comps
, vector_active(comps
), NULL
);
673 memmove(comps
->index
+ 1, comps
->index
,
674 (comps
->alloced
- 1) * sizeof(void *));
675 vector_set_index(comps
, 0, cr
);
681 * Generates a vector of cmd_token representing possible completions
682 * on the current input.
684 * @param vline the vectorized input line
685 * @param vty the vty with the node to match on
686 * @param status pointer to matcher status code
687 * @return vector of struct cmd_token * with possible completions
689 static vector
cmd_complete_command_real(vector vline
, struct vty
*vty
,
692 struct list
*completions
;
693 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
695 enum matcher_rv rv
= command_complete(cmdgraph
, vline
, &completions
);
697 if (MATCHER_ERROR(rv
)) {
698 *status
= CMD_ERR_NO_MATCH
;
702 vector comps
= completions_to_vec(completions
);
703 list_delete(&completions
);
705 // set status code appropriately
706 switch (vector_active(comps
)) {
708 *status
= CMD_ERR_NO_MATCH
;
711 *status
= CMD_COMPLETE_FULL_MATCH
;
714 *status
= CMD_COMPLETE_LIST_MATCH
;
720 vector
cmd_describe_command(vector vline
, struct vty
*vty
, int *status
)
724 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
725 enum node_type onode
;
726 int orig_xpath_index
;
727 vector shifted_vline
;
731 orig_xpath_index
= vty
->xpath_index
;
732 vty
->node
= ENABLE_NODE
;
733 vty
->xpath_index
= 0;
734 /* We can try it on enable node, cos' the vty is authenticated
737 shifted_vline
= vector_init(vector_count(vline
));
739 for (index
= 1; index
< vector_active(vline
); index
++) {
740 vector_set_index(shifted_vline
, index
- 1,
741 vector_lookup(vline
, index
));
744 ret
= cmd_complete_command_real(shifted_vline
, vty
, status
);
746 vector_free(shifted_vline
);
748 vty
->xpath_index
= orig_xpath_index
;
752 return cmd_complete_command_real(vline
, vty
, status
);
755 static struct list
*varhandlers
= NULL
;
757 void cmd_variable_complete(struct cmd_token
*token
, const char *arg
,
761 const struct cmd_variable_handler
*cvh
;
765 tmpcomps
= arg
? vector_init(VECTOR_MIN_SIZE
) : comps
;
767 for (ALL_LIST_ELEMENTS_RO(varhandlers
, ln
, cvh
)) {
768 if (cvh
->tokenname
&& strcmp(cvh
->tokenname
, token
->text
))
770 if (cvh
->varname
&& (!token
->varname
771 || strcmp(cvh
->varname
, token
->varname
)))
773 cvh
->completions(tmpcomps
, token
);
781 for (i
= vector_active(tmpcomps
); i
; i
--) {
782 char *item
= vector_slot(tmpcomps
, i
- 1);
783 if (strlen(item
) >= argsz
&& !strncmp(item
, arg
, argsz
))
784 vector_set(comps
, item
);
786 XFREE(MTYPE_COMPLETION
, item
);
788 vector_free(tmpcomps
);
791 #define AUTOCOMP_INDENT 5
793 char *cmd_variable_comp2str(vector comps
, unsigned short cols
)
796 char *buf
= XCALLOC(MTYPE_TMP
, bsz
);
797 int lc
= AUTOCOMP_INDENT
;
798 size_t cs
= AUTOCOMP_INDENT
;
800 snprintf(buf
, bsz
, "%*s", AUTOCOMP_INDENT
, "");
801 for (size_t j
= 0; j
< vector_active(comps
); j
++) {
802 char *item
= vector_slot(comps
, j
);
803 itemlen
= strlen(item
);
805 if (cs
+ itemlen
+ AUTOCOMP_INDENT
+ 3 >= bsz
)
806 buf
= XREALLOC(MTYPE_TMP
, buf
, (bsz
*= 2));
808 if (lc
+ itemlen
+ 1 >= cols
) {
809 cs
+= snprintf(&buf
[cs
], bsz
- cs
, "\n%*s",
810 AUTOCOMP_INDENT
, "");
811 lc
= AUTOCOMP_INDENT
;
814 size_t written
= snprintf(&buf
[cs
], bsz
- cs
, "%s ", item
);
817 XFREE(MTYPE_COMPLETION
, item
);
818 vector_set_index(comps
, j
, NULL
);
823 void cmd_variable_handler_register(const struct cmd_variable_handler
*cvh
)
828 for (; cvh
->completions
; cvh
++)
829 listnode_add(varhandlers
, (void *)cvh
);
832 DEFUN_HIDDEN (autocomplete
,
834 "autocomplete TYPE TEXT VARNAME",
835 "Autocompletion handler (internal, for vtysh)\n"
838 "cmd_token->varname\n")
840 struct cmd_token tok
;
841 vector comps
= vector_init(32);
844 memset(&tok
, 0, sizeof(tok
));
845 tok
.type
= atoi(argv
[1]->arg
);
846 tok
.text
= argv
[2]->arg
;
847 tok
.varname
= argv
[3]->arg
;
848 if (!strcmp(tok
.varname
, "-"))
851 cmd_variable_complete(&tok
, NULL
, comps
);
853 for (i
= 0; i
< vector_active(comps
); i
++) {
854 char *text
= vector_slot(comps
, i
);
855 vty_out(vty
, "%s\n", text
);
856 XFREE(MTYPE_COMPLETION
, text
);
864 * Generate possible tab-completions for the given input. This function only
865 * returns results that would result in a valid command if used as Readline
866 * completions (as is the case in vtysh). For instance, if the passed vline ends
867 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
869 * @param vline vectorized input line
871 * @param status location to store matcher status code in
872 * @return set of valid strings for use with Readline as tab-completions.
875 char **cmd_complete_command(vector vline
, struct vty
*vty
, int *status
)
878 int original_node
= vty
->node
;
879 vector input_line
= vector_init(vector_count(vline
));
881 // if the first token is 'do' we'll want to execute the command in the
883 int do_shortcut
= cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0));
884 vty
->node
= do_shortcut
? ENABLE_NODE
: original_node
;
886 // construct the input line we'll be matching on
887 unsigned int offset
= (do_shortcut
) ? 1 : 0;
888 for (unsigned index
= 0; index
+ offset
< vector_active(vline
); index
++)
889 vector_set_index(input_line
, index
,
890 vector_lookup(vline
, index
+ offset
));
892 // get token completions -- this is a copying operation
893 vector comps
= NULL
, initial_comps
;
894 initial_comps
= cmd_complete_command_real(input_line
, vty
, status
);
896 if (!MATCHER_ERROR(*status
)) {
897 assert(initial_comps
);
898 // filter out everything that is not suitable for a
900 comps
= vector_init(VECTOR_MIN_SIZE
);
901 for (unsigned int i
= 0; i
< vector_active(initial_comps
);
903 struct cmd_token
*token
= vector_slot(initial_comps
, i
);
904 if (token
->type
== WORD_TKN
)
905 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
,
907 else if (IS_VARYING_TOKEN(token
->type
)) {
908 const char *ref
= vector_lookup(
909 vline
, vector_active(vline
) - 1);
910 cmd_variable_complete(token
, ref
, comps
);
913 vector_free(initial_comps
);
915 // since we filtered results, we need to re-set status code
916 switch (vector_active(comps
)) {
918 *status
= CMD_ERR_NO_MATCH
;
921 *status
= CMD_COMPLETE_FULL_MATCH
;
924 *status
= CMD_COMPLETE_LIST_MATCH
;
927 // copy completions text into an array of char*
928 ret
= XMALLOC(MTYPE_TMP
,
929 (vector_active(comps
) + 1) * sizeof(char *));
931 for (i
= 0; i
< vector_active(comps
); i
++) {
932 ret
[i
] = vector_slot(comps
, i
);
934 // set the last element to NULL, because this array is used in
935 // a Readline completion_generator function which expects NULL
936 // as a sentinel value
940 } else if (initial_comps
)
941 vector_free(initial_comps
);
943 // comps should always be null here
946 // free the adjusted input line
947 vector_free(input_line
);
949 // reset vty->node to its original value
950 vty
->node
= original_node
;
955 /* return parent node */
956 /* MUST eventually converge on CONFIG_NODE */
957 enum node_type
node_parent(enum node_type node
)
961 assert(node
> CONFIG_NODE
);
966 case BGP_FLOWSPECV4_NODE
:
967 case BGP_FLOWSPECV6_NODE
:
968 case BGP_VRF_POLICY_NODE
:
969 case BGP_VNC_DEFAULTS_NODE
:
970 case BGP_VNC_NVE_GROUP_NODE
:
971 case BGP_VNC_L2_GROUP_NODE
:
982 case BGP_EVPN_VNI_NODE
:
985 case KEYCHAIN_KEY_NODE
:
988 case LINK_PARAMS_NODE
:
989 ret
= INTERFACE_NODE
;
995 case LDP_IPV4_IFACE_NODE
:
998 case LDP_IPV6_IFACE_NODE
:
1001 case LDP_PSEUDOWIRE_NODE
:
1002 ret
= LDP_L2VPN_NODE
;
1015 /* Execute command by argument vline vector. */
1016 static int cmd_execute_command_real(vector vline
, enum cmd_filter_type filter
,
1018 const struct cmd_element
**cmd
)
1020 struct list
*argv_list
;
1021 enum matcher_rv status
;
1022 const struct cmd_element
*matched_element
= NULL
;
1024 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
1025 status
= command_match(cmdgraph
, vline
, &argv_list
, &matched_element
);
1028 *cmd
= matched_element
;
1030 // if matcher error, return corresponding CMD_ERR
1031 if (MATCHER_ERROR(status
)) {
1033 list_delete(&argv_list
);
1035 case MATCHER_INCOMPLETE
:
1036 return CMD_ERR_INCOMPLETE
;
1037 case MATCHER_AMBIGUOUS
:
1038 return CMD_ERR_AMBIGUOUS
;
1040 return CMD_ERR_NO_MATCH
;
1044 // build argv array from argv list
1045 struct cmd_token
**argv
= XMALLOC(
1046 MTYPE_TMP
, argv_list
->count
* sizeof(struct cmd_token
*));
1047 struct listnode
*ln
;
1048 struct cmd_token
*token
;
1050 for (ALL_LIST_ELEMENTS_RO(argv_list
, ln
, token
))
1053 int argc
= argv_list
->count
;
1056 if (matched_element
->daemon
)
1057 ret
= CMD_SUCCESS_DAEMON
;
1060 /* Clear array of enqueued configuration changes. */
1061 vty
->num_cfg_changes
= 0;
1062 memset(&vty
->cfg_changes
, 0, sizeof(vty
->cfg_changes
));
1064 /* Regenerate candidate configuration if necessary. */
1065 if (frr_get_cli_mode() == FRR_CLI_CLASSIC
1066 && running_config
->version
1067 > vty
->candidate_config
->version
)
1068 nb_config_replace(vty
->candidate_config
,
1069 running_config
, true);
1072 ret
= matched_element
->func(matched_element
, vty
, argc
, argv
);
1075 // delete list and cmd_token's in it
1076 list_delete(&argv_list
);
1077 XFREE(MTYPE_TMP
, argv
);
1083 * Execute a given command, handling things like "do ..." and checking
1084 * whether the given command might apply at a parent node if doesn't
1085 * apply for the current node.
1087 * @param vline Command line input, vector of char* where each element is
1089 * @param vty The vty context in which the command should be executed.
1090 * @param cmd Pointer where the struct cmd_element of the matched command
1091 * will be stored, if any. May be set to NULL if this info is
1093 * @param vtysh If set != 0, don't lookup the command at parent nodes.
1094 * @return The status of the command that has been executed or an error code
1095 * as to why no command could be executed.
1097 int cmd_execute_command(vector vline
, struct vty
*vty
,
1098 const struct cmd_element
**cmd
, int vtysh
)
1100 int ret
, saved_ret
= 0;
1101 enum node_type onode
, try_node
;
1102 int orig_xpath_index
;
1104 onode
= try_node
= vty
->node
;
1105 orig_xpath_index
= vty
->xpath_index
;
1107 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
1108 vector shifted_vline
;
1111 vty
->node
= ENABLE_NODE
;
1112 vty
->xpath_index
= 0;
1113 /* We can try it on enable node, cos' the vty is authenticated
1116 shifted_vline
= vector_init(vector_count(vline
));
1118 for (index
= 1; index
< vector_active(vline
); index
++)
1119 vector_set_index(shifted_vline
, index
- 1,
1120 vector_lookup(vline
, index
));
1122 ret
= cmd_execute_command_real(shifted_vline
, FILTER_RELAXED
,
1125 vector_free(shifted_vline
);
1127 vty
->xpath_index
= orig_xpath_index
;
1132 cmd_execute_command_real(vline
, FILTER_RELAXED
, vty
, cmd
);
1137 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1138 && ret
!= CMD_ERR_AMBIGUOUS
&& ret
!= CMD_ERR_INCOMPLETE
1139 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
) {
1140 /* This assumes all nodes above CONFIG_NODE are childs of
1142 while (vty
->node
> CONFIG_NODE
) {
1143 try_node
= node_parent(try_node
);
1144 vty
->node
= try_node
;
1145 if (vty
->xpath_index
> 0)
1147 ret
= cmd_execute_command_real(vline
, FILTER_RELAXED
,
1149 if (ret
== CMD_SUCCESS
|| ret
== CMD_WARNING
1150 || ret
== CMD_ERR_AMBIGUOUS
|| ret
== CMD_ERR_INCOMPLETE
1151 || ret
== CMD_NOT_MY_INSTANCE
1152 || ret
== CMD_WARNING_CONFIG_FAILED
)
1155 /* no command succeeded, reset the vty to the original node */
1157 vty
->xpath_index
= orig_xpath_index
;
1160 /* return command status for original node */
1165 * Execute a given command, matching it strictly against the current node.
1166 * This mode is used when reading config files.
1168 * @param vline Command line input, vector of char* where each element is
1170 * @param vty The vty context in which the command should be executed.
1171 * @param cmd Pointer where the struct cmd_element* of the matched command
1172 * will be stored, if any. May be set to NULL if this info is
1174 * @return The status of the command that has been executed or an error code
1175 * as to why no command could be executed.
1177 int cmd_execute_command_strict(vector vline
, struct vty
*vty
,
1178 const struct cmd_element
**cmd
)
1180 return cmd_execute_command_real(vline
, FILTER_STRICT
, vty
, cmd
);
1184 * Hook for preprocessing command string before executing.
1186 * All subscribers are called with the raw command string that is to be
1187 * executed. If any changes are to be made, a new string should be allocated
1188 * with MTYPE_TMP and *cmd_out updated to point to this new string. The caller
1189 * is then responsible for freeing this string.
1191 * All processing functions must be mutually exclusive in their action, i.e. if
1192 * one subscriber decides to modify the command, all others must not modify it
1193 * when called. Feeding the output of one processing command into a subsequent
1194 * one is not supported.
1196 * This hook is intentionally internal to the command processing system.
1199 * The raw command string.
1202 * The result of any processing.
1204 DECLARE_HOOK(cmd_execute
,
1205 (struct vty
*vty
, const char *cmd_in
, char **cmd_out
),
1206 (vty
, cmd_in
, cmd_out
));
1207 DEFINE_HOOK(cmd_execute
, (struct vty
*vty
, const char *cmd_in
, char **cmd_out
),
1208 (vty
, cmd_in
, cmd_out
));
1210 /* Hook executed after a CLI command. */
1211 DECLARE_KOOH(cmd_execute_done
, (struct vty
*vty
, const char *cmd_exec
),
1213 DEFINE_KOOH(cmd_execute_done
, (struct vty
*vty
, const char *cmd_exec
),
1217 * cmd_execute hook subscriber to handle `|` actions.
1219 static int handle_pipe_action(struct vty
*vty
, const char *cmd_in
,
1223 char *orig
, *working
, *token
, *u
;
1224 char *pipe
= strstr(cmd_in
, "| ");
1229 /* duplicate string for processing purposes, not including pipe */
1230 orig
= working
= XSTRDUP(MTYPE_TMP
, pipe
+ 2);
1232 /* retrieve action */
1233 token
= strsep(&working
, " ");
1236 /* match result to known actions */
1237 if (strmatch(token
, "include")) {
1238 /* the remaining text should be a regexp */
1239 char *regexp
= working
;
1242 vty_out(vty
, "%% Need a regexp to filter with\n");
1246 bool succ
= vty_set_include(vty
, regexp
);
1249 vty_out(vty
, "%% Bad regexp '%s'\n", regexp
);
1252 *cmd_out
= XSTRDUP(MTYPE_TMP
, cmd_in
);
1256 vty_out(vty
, "%% Unknown action '%s'\n", token
);
1261 XFREE(MTYPE_TMP
, orig
);
1265 static int handle_pipe_action_done(struct vty
*vty
, const char *cmd_exec
)
1268 vty_set_include(vty
, NULL
);
1273 int cmd_execute(struct vty
*vty
, const char *cmd
,
1274 const struct cmd_element
**matched
, int vtysh
)
1277 char *cmd_out
= NULL
;
1278 const char *cmd_exec
;
1281 hook_call(cmd_execute
, vty
, cmd
, &cmd_out
);
1282 cmd_exec
= cmd_out
? (const char *)cmd_out
: cmd
;
1284 vline
= cmd_make_strvec(cmd_exec
);
1287 ret
= cmd_execute_command(vline
, vty
, matched
, vtysh
);
1288 cmd_free_strvec(vline
);
1293 hook_call(cmd_execute_done
, vty
, cmd_exec
);
1295 XFREE(MTYPE_TMP
, cmd_out
);
1302 * Parse one line of config, walking up the parse tree attempting to find a
1305 * @param vty The vty context in which the command should be executed.
1306 * @param cmd Pointer where the struct cmd_element* of the match command
1307 * will be stored, if any. May be set to NULL if this info is
1309 * @param use_daemon Boolean to control whether or not we match on
1310 * CMD_SUCCESS_DAEMON
1312 * @return The status of the command that has been executed or an error code
1313 * as to why no command could be executed.
1315 int command_config_read_one_line(struct vty
*vty
,
1316 const struct cmd_element
**cmd
,
1317 uint32_t line_num
, int use_daemon
)
1322 vline
= cmd_make_strvec(vty
->buf
);
1324 /* In case of comment line */
1328 /* Execute configuration command : this is strict match */
1329 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1331 // Climb the tree and try the command again at each node
1332 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1333 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1334 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1335 && ret
!= CMD_ERR_AMBIGUOUS
&& ret
!= CMD_ERR_INCOMPLETE
1336 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
1337 && vty
->node
!= CONFIG_NODE
) {
1338 int saved_node
= vty
->node
;
1339 int saved_xpath_index
= vty
->xpath_index
;
1341 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1342 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1343 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1344 && ret
!= CMD_ERR_AMBIGUOUS
&& ret
!= CMD_ERR_INCOMPLETE
1345 && vty
->node
> CONFIG_NODE
) {
1346 vty
->node
= node_parent(vty
->node
);
1347 if (vty
->xpath_index
> 0)
1349 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1352 // If climbing the tree did not work then ignore the command and
1353 // stay at the same node
1354 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1355 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1356 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
) {
1357 vty
->node
= saved_node
;
1358 vty
->xpath_index
= saved_xpath_index
;
1362 if (ret
!= CMD_SUCCESS
&&
1363 ret
!= CMD_WARNING
&&
1364 ret
!= CMD_SUCCESS_DAEMON
) {
1365 struct vty_error
*ve
= XCALLOC(MTYPE_TMP
, sizeof(*ve
));
1367 memcpy(ve
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
1368 ve
->line_num
= line_num
;
1370 vty
->error
= list_new();
1372 listnode_add(vty
->error
, ve
);
1375 cmd_free_strvec(vline
);
1380 /* Configuration make from file. */
1381 int config_from_file(struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
1383 int ret
, error_ret
= 0;
1386 while (fgets(vty
->buf
, VTY_BUFSIZ
, fp
)) {
1389 ret
= command_config_read_one_line(vty
, NULL
, *line_num
, 0);
1391 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1392 && ret
!= CMD_ERR_NOTHING_TODO
)
1403 /* Configuration from terminal */
1404 DEFUN (config_terminal
,
1405 config_terminal_cmd
,
1406 "configure [terminal]",
1407 "Configuration from vty interface\n"
1408 "Configuration terminal\n")
1410 return vty_config_enter(vty
, false, false);
1413 /* Enable command */
1417 "Turn on privileged mode command\n")
1419 /* If enable password is NULL, change to ENABLE_NODE */
1420 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
)
1421 || vty
->type
== VTY_SHELL_SERV
)
1422 vty
->node
= ENABLE_NODE
;
1424 vty
->node
= AUTH_ENABLE_NODE
;
1429 /* Disable command */
1433 "Turn off privileged mode command\n")
1435 if (vty
->node
== ENABLE_NODE
)
1436 vty
->node
= VIEW_NODE
;
1440 /* Down vty node level. */
1444 "Exit current mode and down to previous mode\n")
1450 void cmd_exit(struct vty
*vty
)
1452 switch (vty
->node
) {
1458 vty
->status
= VTY_CLOSE
;
1461 vty
->node
= ENABLE_NODE
;
1462 vty_config_exit(vty
);
1464 case INTERFACE_NODE
:
1477 case LDP_L2VPN_NODE
:
1479 case OPENFABRIC_NODE
:
1485 vty
->node
= CONFIG_NODE
;
1488 case BGP_IPV4M_NODE
:
1489 case BGP_IPV4L_NODE
:
1490 case BGP_VPNV4_NODE
:
1491 case BGP_VPNV6_NODE
:
1492 case BGP_FLOWSPECV4_NODE
:
1493 case BGP_FLOWSPECV6_NODE
:
1494 case BGP_VRF_POLICY_NODE
:
1495 case BGP_VNC_DEFAULTS_NODE
:
1496 case BGP_VNC_NVE_GROUP_NODE
:
1497 case BGP_VNC_L2_GROUP_NODE
:
1499 case BGP_IPV6M_NODE
:
1501 case BGP_IPV6L_NODE
:
1503 vty
->node
= BGP_NODE
;
1505 case BGP_EVPN_VNI_NODE
:
1506 vty
->node
= BGP_EVPN_NODE
;
1510 vty
->node
= LDP_NODE
;
1512 case LDP_IPV4_IFACE_NODE
:
1513 vty
->node
= LDP_IPV4_NODE
;
1515 case LDP_IPV6_IFACE_NODE
:
1516 vty
->node
= LDP_IPV6_NODE
;
1518 case LDP_PSEUDOWIRE_NODE
:
1519 vty
->node
= LDP_L2VPN_NODE
;
1521 case KEYCHAIN_KEY_NODE
:
1522 vty
->node
= KEYCHAIN_NODE
;
1524 case LINK_PARAMS_NODE
:
1525 vty
->node
= INTERFACE_NODE
;
1528 vty
->node
= BFD_NODE
;
1534 if (vty
->xpath_index
> 0)
1542 "Exit current mode and down to previous mode\n")
1544 return config_exit(self
, vty
, argc
, argv
);
1548 /* End of configuration. */
1552 "End current mode and change to enable mode.\n")
1555 vty_config_exit(vty
);
1556 vty
->node
= ENABLE_NODE
;
1563 DEFUN (show_version
,
1567 "Displays zebra version\n")
1569 vty_out(vty
, "%s %s (%s).\n", FRR_FULL_NAME
, FRR_VERSION
,
1570 cmd_hostname_get() ? cmd_hostname_get() : "");
1571 vty_out(vty
, "%s%s\n", FRR_COPYRIGHT
, GIT_INFO
);
1572 vty_out(vty
, "configured with:\n %s\n", FRR_CONFIG_ARGS
);
1577 /* "Set" version ... ignore version tags */
1578 DEFUN (frr_version_defaults
,
1579 frr_version_defaults_cmd
,
1580 "frr <version|defaults> LINE...",
1581 "FRRouting global parameters\n"
1582 "version configuration was written by\n"
1583 "set of configuration defaults used\n"
1589 /* Help display function for all node. */
1593 "Description of the interactive help system\n")
1596 "Quagga VTY provides advanced help feature. When you need help,\n\
1597 anytime at the command line please press '?'.\n\
1599 If nothing matches, the help list will be empty and you must backup\n\
1600 until entering a '?' shows the available options.\n\
1601 Two styles of help are provided:\n\
1602 1. Full help is available when you are ready to enter a\n\
1603 command argument (e.g. 'show ?') and describes each possible\n\
1605 2. Partial help is provided when an abbreviated argument is entered\n\
1606 and you want to know what arguments match the input\n\
1607 (e.g. 'show me?'.)\n\n");
1611 static void permute(struct graph_node
*start
, struct vty
*vty
)
1613 static struct list
*position
= NULL
;
1615 position
= list_new();
1617 struct cmd_token
*stok
= start
->data
;
1618 struct graph_node
*gnn
;
1619 struct listnode
*ln
;
1622 listnode_add(position
, start
);
1623 for (unsigned int i
= 0; i
< vector_active(start
->to
); i
++) {
1624 struct graph_node
*gn
= vector_slot(start
->to
, i
);
1625 struct cmd_token
*tok
= gn
->data
;
1626 if (tok
->attr
== CMD_ATTR_HIDDEN
1627 || tok
->attr
== CMD_ATTR_DEPRECATED
)
1629 else if (tok
->type
== END_TKN
|| gn
== start
) {
1631 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
)) {
1632 struct cmd_token
*tt
= gnn
->data
;
1633 if (tt
->type
< SPECIAL_TKN
)
1634 vty_out(vty
, " %s", tt
->text
);
1637 vty_out(vty
, "...");
1641 if (stok
->type
== FORK_TKN
&& tok
->type
!= FORK_TKN
)
1642 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
))
1651 list_delete_node(position
, listtail(position
));
1654 int cmd_list_cmds(struct vty
*vty
, int do_permute
)
1656 struct cmd_node
*node
= vector_slot(cmdvec
, vty
->node
);
1659 permute(vector_slot(node
->cmdgraph
->nodes
, 0), vty
);
1661 /* loop over all commands at this node */
1662 struct cmd_element
*element
= NULL
;
1663 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
);
1665 if ((element
= vector_slot(node
->cmd_vector
, i
))
1666 && element
->attr
!= CMD_ATTR_DEPRECATED
1667 && element
->attr
!= CMD_ATTR_HIDDEN
)
1668 vty_out(vty
, " %s\n", element
->string
);
1673 /* Help display function for all node. */
1676 "list [permutations]",
1677 "Print command list\n"
1678 "Print all possible command permutations\n")
1680 return cmd_list_cmds(vty
, argc
== 2);
1683 DEFUN (show_commandtree
,
1684 show_commandtree_cmd
,
1685 "show commandtree [permutations]",
1687 "Show command tree\n"
1688 "Permutations that we are interested in\n")
1690 return cmd_list_cmds(vty
, argc
== 3);
1693 DEFUN_HIDDEN(show_cli_graph
,
1698 "Dump current command space as DOT graph\n")
1700 struct cmd_node
*cn
= vector_slot(cmdvec
, vty
->node
);
1701 char *dot
= cmd_graph_dump_dot(cn
->cmdgraph
);
1703 vty_out(vty
, "%s\n", dot
);
1704 XFREE(MTYPE_TMP
, dot
);
1708 static int vty_write_config(struct vty
*vty
)
1711 struct cmd_node
*node
;
1716 nb_cli_show_config_prepare(running_config
, false);
1718 if (vty
->type
== VTY_TERM
) {
1719 vty_out(vty
, "\nCurrent configuration:\n");
1720 vty_out(vty
, "!\n");
1723 vty_out(vty
, "frr version %s\n", FRR_VER_SHORT
);
1724 vty_out(vty
, "frr defaults %s\n", DFLT_NAME
);
1725 vty_out(vty
, "!\n");
1727 for (i
= 0; i
< vector_active(cmdvec
); i
++)
1728 if ((node
= vector_slot(cmdvec
, i
)) && node
->func
1729 && (node
->vtysh
|| vty
->type
!= VTY_SHELL
)) {
1730 if ((*node
->func
)(vty
))
1731 vty_out(vty
, "!\n");
1734 if (vty
->type
== VTY_TERM
) {
1735 vty_out(vty
, "end\n");
1741 static int file_write_config(struct vty
*vty
)
1744 char *config_file
, *slash
;
1745 char *config_file_tmp
= NULL
;
1746 char *config_file_sav
= NULL
;
1747 int ret
= CMD_WARNING
;
1748 struct vty
*file_vty
;
1749 struct stat conf_stat
;
1754 /* Check and see if we are operating under vtysh configuration */
1755 if (host
.config
== NULL
) {
1757 "Can't save to configuration file, using vtysh.\n");
1762 config_file
= host
.config
;
1765 #define O_DIRECTORY 0
1767 slash
= strrchr(config_file
, '/');
1769 char *config_dir
= XSTRDUP(MTYPE_TMP
, config_file
);
1770 config_dir
[slash
- config_file
] = '\0';
1771 dirfd
= open(config_dir
, O_DIRECTORY
| O_RDONLY
);
1772 XFREE(MTYPE_TMP
, config_dir
);
1774 dirfd
= open(".", O_DIRECTORY
| O_RDONLY
);
1775 /* if dirfd is invalid, directory sync fails, but we're still OK */
1777 size_t config_file_sav_sz
= strlen(config_file
) + strlen(CONF_BACKUP_EXT
) + 1;
1778 config_file_sav
= XMALLOC(MTYPE_TMP
, config_file_sav_sz
);
1779 strlcpy(config_file_sav
, config_file
, config_file_sav_sz
);
1780 strlcat(config_file_sav
, CONF_BACKUP_EXT
, config_file_sav_sz
);
1783 config_file_tmp
= XMALLOC(MTYPE_TMP
, strlen(config_file
) + 8);
1784 sprintf(config_file_tmp
, "%s.XXXXXX", config_file
);
1786 /* Open file to configuration write. */
1787 fd
= mkstemp(config_file_tmp
);
1789 vty_out(vty
, "Can't open configuration file %s.\n",
1793 if (fchmod(fd
, CONFIGFILE_MASK
) != 0) {
1794 vty_out(vty
, "Can't chmod configuration file %s: %s (%d).\n",
1795 config_file_tmp
, safe_strerror(errno
), errno
);
1799 /* Make vty for configuration file. */
1800 file_vty
= vty_new();
1802 file_vty
->type
= VTY_FILE
;
1804 /* Config file header print. */
1805 vty_out(file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1806 vty_time_print(file_vty
, 1);
1807 vty_out(file_vty
, "!\n");
1808 vty_write_config(file_vty
);
1809 vty_close(file_vty
);
1811 if (stat(config_file
, &conf_stat
) >= 0) {
1812 if (unlink(config_file_sav
) != 0)
1813 if (errno
!= ENOENT
) {
1815 "Can't unlink backup configuration file %s.\n",
1819 if (link(config_file
, config_file_sav
) != 0) {
1821 "Can't backup old configuration file %s.\n",
1828 if (rename(config_file_tmp
, config_file
) != 0) {
1829 vty_out(vty
, "Can't save configuration file %s.\n",
1836 vty_out(vty
, "Configuration saved to %s\n", config_file
);
1840 if (ret
!= CMD_SUCCESS
)
1841 unlink(config_file_tmp
);
1844 XFREE(MTYPE_TMP
, config_file_tmp
);
1845 XFREE(MTYPE_TMP
, config_file_sav
);
1849 /* Write current configuration into file. */
1851 DEFUN (config_write
,
1853 "write [<file|memory|terminal>]",
1854 "Write running configuration to memory, network, or terminal\n"
1855 "Write to configuration file\n"
1856 "Write configuration currently in memory\n"
1857 "Write configuration to terminal\n")
1859 const int idx_type
= 1;
1861 // if command was 'write terminal' or 'write memory'
1862 if (argc
== 2 && (!strcmp(argv
[idx_type
]->text
, "terminal"))) {
1863 return vty_write_config(vty
);
1866 return file_write_config(vty
);
1869 /* ALIAS_FIXME for 'write <terminal|memory>' */
1870 DEFUN (show_running_config
,
1871 show_running_config_cmd
,
1872 "show running-config",
1874 "running configuration (same as write terminal)\n")
1876 return vty_write_config(vty
);
1879 /* ALIAS_FIXME for 'write file' */
1880 DEFUN (copy_runningconf_startupconf
,
1881 copy_runningconf_startupconf_cmd
,
1882 "copy running-config startup-config",
1883 "Copy configuration\n"
1884 "Copy running config to... \n"
1885 "Copy running config to startup config (same as write file/memory)\n")
1887 return file_write_config(vty
);
1891 /* Write startup configuration into the terminal. */
1892 DEFUN (show_startup_config
,
1893 show_startup_config_cmd
,
1894 "show startup-config",
1896 "Contents of startup configuration\n")
1903 if (host
.config
== NULL
)
1906 confp
= fopen(host
.config
, "r");
1907 if (confp
== NULL
) {
1908 vty_out(vty
, "Can't open configuration file [%s] due to '%s'\n",
1909 host
.config
, safe_strerror(errno
));
1913 while (fgets(buf
, BUFSIZ
, confp
)) {
1916 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1920 vty_out(vty
, "%s\n", buf
);
1928 int cmd_domainname_set(const char *domainname
)
1930 XFREE(MTYPE_HOST
, host
.domainname
);
1931 host
.domainname
= domainname
? XSTRDUP(MTYPE_HOST
, domainname
) : NULL
;
1935 /* Hostname configuration */
1936 DEFUN(config_domainname
,
1939 "Set system's domain name\n"
1940 "This system's domain name\n")
1942 struct cmd_token
*word
= argv
[1];
1944 if (!isalpha((unsigned char)word
->arg
[0])) {
1945 vty_out(vty
, "Please specify string starting with alphabet\n");
1946 return CMD_WARNING_CONFIG_FAILED
;
1949 return cmd_domainname_set(word
->arg
);
1952 DEFUN(config_no_domainname
,
1954 "no domainname [DOMAINNAME]",
1956 "Reset system's domain name\n"
1957 "domain name of this router\n")
1959 return cmd_domainname_set(NULL
);
1962 int cmd_hostname_set(const char *hostname
)
1964 XFREE(MTYPE_HOST
, host
.name
);
1965 host
.name
= hostname
? XSTRDUP(MTYPE_HOST
, hostname
) : NULL
;
1969 /* Hostname configuration */
1970 DEFUN (config_hostname
,
1973 "Set system's network name\n"
1974 "This system's network name\n")
1976 struct cmd_token
*word
= argv
[1];
1978 if (!isalnum((unsigned char)word
->arg
[0])) {
1980 "Please specify string starting with alphabet or number\n");
1981 return CMD_WARNING_CONFIG_FAILED
;
1984 /* With reference to RFC 1123 Section 2.1 */
1985 if (strlen(word
->arg
) > HOSTNAME_LEN
) {
1986 vty_out(vty
, "Hostname length should be less than %d chars\n",
1988 return CMD_WARNING_CONFIG_FAILED
;
1991 return cmd_hostname_set(word
->arg
);
1994 DEFUN (config_no_hostname
,
1996 "no hostname [HOSTNAME]",
1998 "Reset system's network name\n"
1999 "Host name of this router\n")
2001 return cmd_hostname_set(NULL
);
2004 /* VTY interface password set. */
2005 DEFUN (config_password
,
2007 "password [(8-8)] WORD",
2008 "Modify the terminal connection password\n"
2009 "Specifies a HIDDEN password will follow\n"
2010 "The password string\n")
2014 if (argc
== 3) // '8' was specified
2017 XFREE(MTYPE_HOST
, host
.password
);
2018 host
.password
= NULL
;
2019 if (host
.password_encrypt
)
2020 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2021 host
.password_encrypt
=
2022 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2026 if (!isalnum((unsigned char)argv
[idx_8
]->arg
[0])) {
2028 "Please specify string starting with alphanumeric\n");
2029 return CMD_WARNING_CONFIG_FAILED
;
2033 XFREE(MTYPE_HOST
, host
.password
);
2034 host
.password
= NULL
;
2037 if (host
.password_encrypt
)
2038 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2039 host
.password_encrypt
=
2040 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2042 host
.password
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2047 /* VTY interface password delete. */
2048 DEFUN (no_config_password
,
2052 "Modify the terminal connection password\n")
2054 bool warned
= false;
2056 if (host
.password
) {
2057 if (!vty_shell_serv(vty
)) {
2058 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2061 XFREE(MTYPE_HOST
, host
.password
);
2063 host
.password
= NULL
;
2065 if (host
.password_encrypt
) {
2066 if (!warned
&& !vty_shell_serv(vty
))
2067 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2068 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2070 host
.password_encrypt
= NULL
;
2075 /* VTY enable password set. */
2076 DEFUN (config_enable_password
,
2077 enable_password_cmd
,
2078 "enable password [(8-8)] WORD",
2079 "Modify enable password parameters\n"
2080 "Assign the privileged level password\n"
2081 "Specifies a HIDDEN password will follow\n"
2082 "The HIDDEN 'enable' password string\n")
2087 /* Crypt type is specified. */
2089 if (argv
[idx_8
]->arg
[0] == '8') {
2091 XFREE(MTYPE_HOST
, host
.enable
);
2094 if (host
.enable_encrypt
)
2095 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2096 host
.enable_encrypt
=
2097 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2101 vty_out(vty
, "Unknown encryption type.\n");
2102 return CMD_WARNING_CONFIG_FAILED
;
2106 if (!isalnum((unsigned char)argv
[idx_8
]->arg
[0])) {
2108 "Please specify string starting with alphanumeric\n");
2109 return CMD_WARNING_CONFIG_FAILED
;
2113 XFREE(MTYPE_HOST
, host
.enable
);
2116 /* Plain password input. */
2118 if (host
.enable_encrypt
)
2119 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2120 host
.enable_encrypt
=
2121 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2123 host
.enable
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2128 /* VTY enable password delete. */
2129 DEFUN (no_config_enable_password
,
2130 no_enable_password_cmd
,
2131 "no enable password",
2133 "Modify enable password parameters\n"
2134 "Assign the privileged level password\n")
2136 bool warned
= false;
2139 if (!vty_shell_serv(vty
)) {
2140 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2143 XFREE(MTYPE_HOST
, host
.enable
);
2147 if (host
.enable_encrypt
) {
2148 if (!warned
&& !vty_shell_serv(vty
))
2149 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2150 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2152 host
.enable_encrypt
= NULL
;
2157 DEFUN (service_password_encrypt
,
2158 service_password_encrypt_cmd
,
2159 "service password-encryption",
2160 "Set up miscellaneous service\n"
2161 "Enable encrypted passwords\n")
2168 if (host
.password
) {
2169 if (host
.password_encrypt
)
2170 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2171 host
.password_encrypt
=
2172 XSTRDUP(MTYPE_HOST
, zencrypt(host
.password
));
2175 if (host
.enable_encrypt
)
2176 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2177 host
.enable_encrypt
=
2178 XSTRDUP(MTYPE_HOST
, zencrypt(host
.enable
));
2184 DEFUN (no_service_password_encrypt
,
2185 no_service_password_encrypt_cmd
,
2186 "no service password-encryption",
2188 "Set up miscellaneous service\n"
2189 "Enable encrypted passwords\n")
2196 if (host
.password_encrypt
)
2197 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2198 host
.password_encrypt
= NULL
;
2200 if (host
.enable_encrypt
)
2201 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2202 host
.enable_encrypt
= NULL
;
2207 DEFUN (config_terminal_length
,
2208 config_terminal_length_cmd
,
2209 "terminal length (0-512)",
2210 "Set terminal line parameters\n"
2211 "Set number of lines on a screen\n"
2212 "Number of lines on screen (0 for no pausing)\n")
2216 vty
->lines
= atoi(argv
[idx_number
]->arg
);
2221 DEFUN (config_terminal_no_length
,
2222 config_terminal_no_length_cmd
,
2223 "terminal no length",
2224 "Set terminal line parameters\n"
2226 "Set number of lines on a screen\n")
2232 DEFUN (service_terminal_length
,
2233 service_terminal_length_cmd
,
2234 "service terminal-length (0-512)",
2235 "Set up miscellaneous service\n"
2236 "System wide terminal length configuration\n"
2237 "Number of lines of VTY (0 means no line control)\n")
2241 host
.lines
= atoi(argv
[idx_number
]->arg
);
2246 DEFUN (no_service_terminal_length
,
2247 no_service_terminal_length_cmd
,
2248 "no service terminal-length [(0-512)]",
2250 "Set up miscellaneous service\n"
2251 "System wide terminal length configuration\n"
2252 "Number of lines of VTY (0 means no line control)\n")
2258 DEFUN_HIDDEN (do_echo
,
2261 "Echo a message back to the vty\n"
2262 "The message to echo\n")
2266 vty_out(vty
, "%s\n",
2267 ((message
= argv_concat(argv
, argc
, 1)) ? message
: ""));
2269 XFREE(MTYPE_TMP
, message
);
2273 DEFUN (config_logmsg
,
2275 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
2276 "Send a message to enabled logging destinations\n"
2278 "The message to send\n")
2280 int idx_log_level
= 1;
2281 int idx_message
= 2;
2285 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2286 return CMD_ERR_NO_MATCH
;
2289 ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
2291 XFREE(MTYPE_TMP
, message
);
2296 DEFUN (show_logging
,
2300 "Show current logging configuration\n")
2302 struct zlog
*zl
= zlog_default
;
2304 vty_out(vty
, "Syslog logging: ");
2305 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
2306 vty_out(vty
, "disabled");
2308 vty_out(vty
, "level %s, facility %s, ident %s",
2309 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
2310 facility_name(zl
->facility
), zl
->ident
);
2313 vty_out(vty
, "Stdout logging: ");
2314 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
2315 vty_out(vty
, "disabled");
2317 vty_out(vty
, "level %s",
2318 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
2321 vty_out(vty
, "Monitor logging: ");
2322 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
2323 vty_out(vty
, "disabled");
2325 vty_out(vty
, "level %s",
2326 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
2329 vty_out(vty
, "File logging: ");
2330 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) || !zl
->fp
)
2331 vty_out(vty
, "disabled");
2333 vty_out(vty
, "level %s, filename %s",
2334 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
2338 vty_out(vty
, "Protocol name: %s\n", zl
->protoname
);
2339 vty_out(vty
, "Record priority: %s\n",
2340 (zl
->record_priority
? "enabled" : "disabled"));
2341 vty_out(vty
, "Timestamp precision: %d\n", zl
->timestamp_precision
);
2346 DEFUN (config_log_stdout
,
2347 config_log_stdout_cmd
,
2348 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2350 "Set stdout logging level\n"
2353 int idx_log_level
= 2;
2355 if (argc
== idx_log_level
) {
2356 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2361 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2362 return CMD_ERR_NO_MATCH
;
2363 zlog_set_level(ZLOG_DEST_STDOUT
, level
);
2367 DEFUN (no_config_log_stdout
,
2368 no_config_log_stdout_cmd
,
2369 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2372 "Cancel logging to stdout\n"
2375 zlog_set_level(ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
2379 DEFUN (config_log_monitor
,
2380 config_log_monitor_cmd
,
2381 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2383 "Set terminal line (monitor) logging level\n"
2386 int idx_log_level
= 2;
2388 if (argc
== idx_log_level
) {
2389 zlog_set_level(ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
2394 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2395 return CMD_ERR_NO_MATCH
;
2396 zlog_set_level(ZLOG_DEST_MONITOR
, level
);
2400 DEFUN (no_config_log_monitor
,
2401 no_config_log_monitor_cmd
,
2402 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2405 "Disable terminal line (monitor) logging\n"
2408 zlog_set_level(ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
2412 static int set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
2416 const char *fullpath
;
2418 /* Path detection. */
2419 if (!IS_DIRECTORY_SEP(*fname
)) {
2420 char cwd
[MAXPATHLEN
+ 1];
2421 cwd
[MAXPATHLEN
] = '\0';
2423 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2424 flog_err_sys(EC_LIB_SYSTEM_CALL
,
2425 "config_log_file: Unable to alloc mem!");
2426 return CMD_WARNING_CONFIG_FAILED
;
2429 p
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + strlen(fname
) + 2);
2430 sprintf(p
, "%s/%s", cwd
, fname
);
2435 ret
= zlog_set_file(fullpath
, loglevel
);
2437 XFREE(MTYPE_TMP
, p
);
2441 vty_out(vty
, "can't open logfile %s\n", fname
);
2442 return CMD_WARNING_CONFIG_FAILED
;
2445 XFREE(MTYPE_HOST
, host
.logfile
);
2447 host
.logfile
= XSTRDUP(MTYPE_HOST
, fname
);
2449 #if defined(HAVE_CUMULUS)
2450 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2451 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2456 void command_setup_early_logging(const char *dest
, const char *level
)
2461 int nlevel
= level_match(level
);
2463 if (nlevel
!= ZLOG_DISABLED
)
2464 zlog_default
->default_lvl
= nlevel
;
2470 if (strcmp(dest
, "stdout") == 0) {
2471 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2475 if (strcmp(dest
, "syslog") == 0) {
2476 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2480 token
= strstr(dest
, ":");
2486 set_log_file(NULL
, token
, zlog_default
->default_lvl
);
2489 DEFUN (config_log_file
,
2490 config_log_file_cmd
,
2491 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2494 "Logging filename\n"
2497 int idx_filename
= 2;
2498 int idx_log_levels
= 3;
2501 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2503 return CMD_ERR_NO_MATCH
;
2504 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2506 return set_log_file(vty
, argv
[idx_filename
]->arg
,
2507 zlog_default
->default_lvl
);
2510 static void disable_log_file(void)
2514 XFREE(MTYPE_HOST
, host
.logfile
);
2516 host
.logfile
= NULL
;
2519 DEFUN (no_config_log_file
,
2520 no_config_log_file_cmd
,
2521 "no log file [FILENAME [LEVEL]]",
2524 "Cancel logging to file\n"
2525 "Logging file name\n"
2532 DEFUN (config_log_syslog
,
2533 config_log_syslog_cmd
,
2534 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2536 "Set syslog logging level\n"
2539 int idx_log_levels
= 2;
2543 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2545 return CMD_ERR_NO_MATCH
;
2546 zlog_set_level(ZLOG_DEST_SYSLOG
, level
);
2549 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2554 DEFUN (no_config_log_syslog
,
2555 no_config_log_syslog_cmd
,
2556 "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>]",
2559 "Cancel logging to syslog\n"
2563 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2567 DEFUN (config_log_facility
,
2568 config_log_facility_cmd
,
2569 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2571 "Facility parameter for syslog messages\n"
2575 int facility
= facility_match(argv
[idx_target
]->arg
);
2577 zlog_default
->facility
= facility
;
2581 DEFUN (no_config_log_facility
,
2582 no_config_log_facility_cmd
,
2583 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2586 "Reset syslog facility to default (daemon)\n"
2589 zlog_default
->facility
= LOG_DAEMON
;
2593 DEFUN (config_log_record_priority
,
2594 config_log_record_priority_cmd
,
2595 "log record-priority",
2597 "Log the priority of the message within the message\n")
2599 zlog_default
->record_priority
= 1;
2603 DEFUN (no_config_log_record_priority
,
2604 no_config_log_record_priority_cmd
,
2605 "no log record-priority",
2608 "Do not log the priority of the message within the message\n")
2610 zlog_default
->record_priority
= 0;
2614 DEFUN (config_log_timestamp_precision
,
2615 config_log_timestamp_precision_cmd
,
2616 "log timestamp precision (0-6)",
2618 "Timestamp configuration\n"
2619 "Set the timestamp precision\n"
2620 "Number of subsecond digits\n")
2623 zlog_default
->timestamp_precision
=
2624 strtoul(argv
[idx_number
]->arg
, NULL
, 10);
2628 DEFUN (no_config_log_timestamp_precision
,
2629 no_config_log_timestamp_precision_cmd
,
2630 "no log timestamp precision",
2633 "Timestamp configuration\n"
2634 "Reset the timestamp precision to the default value of 0\n")
2636 zlog_default
->timestamp_precision
= 0;
2640 DEFUN (debug_memstats
,
2642 "[no] debug memstats-at-exit",
2645 "Print memory type statistics at exit\n")
2647 debug_memstats_at_exit
= !!strcmp(argv
[0]->text
, "no");
2651 int cmd_banner_motd_file(const char *file
)
2653 int success
= CMD_SUCCESS
;
2658 rpath
= realpath(file
, p
);
2660 return CMD_ERR_NO_FILE
;
2661 in
= strstr(rpath
, SYSCONFDIR
);
2663 XFREE(MTYPE_HOST
, host
.motdfile
);
2664 host
.motdfile
= XSTRDUP(MTYPE_HOST
, file
);
2666 success
= CMD_WARNING_CONFIG_FAILED
;
2671 DEFUN (banner_motd_file
,
2672 banner_motd_file_cmd
,
2673 "banner motd file FILE",
2676 "Banner from a file\n"
2680 const char *filename
= argv
[idx_file
]->arg
;
2681 int cmd
= cmd_banner_motd_file(filename
);
2683 if (cmd
== CMD_ERR_NO_FILE
)
2684 vty_out(vty
, "%s does not exist", filename
);
2685 else if (cmd
== CMD_WARNING_CONFIG_FAILED
)
2686 vty_out(vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2691 DEFUN (banner_motd_default
,
2692 banner_motd_default_cmd
,
2693 "banner motd default",
2694 "Set banner string\n"
2695 "Strings for motd\n"
2698 host
.motd
= default_motd
;
2702 DEFUN (no_banner_motd
,
2706 "Set banner string\n"
2707 "Strings for motd\n")
2711 XFREE(MTYPE_HOST
, host
.motdfile
);
2712 host
.motdfile
= NULL
;
2719 "Find CLI command matching a regular expression\n"
2720 "Search pattern (POSIX regex)\n")
2722 char *pattern
= argv
[1]->arg
;
2723 const struct cmd_node
*node
;
2724 const struct cmd_element
*cli
;
2729 int cr
= regcomp(&exp
, pattern
, REG_NOSUB
| REG_EXTENDED
);
2734 vty_out(vty
, "%% Invalid {...} expression\n");
2737 vty_out(vty
, "%% Bad repetition operator\n");
2740 vty_out(vty
, "%% Regex syntax error\n");
2743 vty_out(vty
, "%% Invalid collating element\n");
2746 vty_out(vty
, "%% Invalid character class name\n");
2750 "%% Regex ended with escape character (\\)\n");
2754 "%% Invalid number in \\digit construction\n");
2757 vty_out(vty
, "%% Unbalanced square brackets\n");
2760 vty_out(vty
, "%% Unbalanced parentheses\n");
2763 vty_out(vty
, "%% Unbalanced braces\n");
2767 "%% Invalid endpoint in range expression\n");
2770 vty_out(vty
, "%% Failed to compile (out of memory)\n");
2778 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++) {
2779 node
= vector_slot(cmdvec
, i
);
2782 clis
= node
->cmd_vector
;
2783 for (unsigned int j
= 0; j
< vector_active(clis
); j
++) {
2784 cli
= vector_slot(clis
, j
);
2786 if (regexec(&exp
, cli
->string
, 0, NULL
, 0) == 0)
2787 vty_out(vty
, " (%s) %s\n",
2788 node_names
[node
->node
], cli
->string
);
2797 /* Set config filename. Called from vty.c */
2798 void host_config_set(const char *filename
)
2800 XFREE(MTYPE_HOST
, host
.config
);
2801 host
.config
= XSTRDUP(MTYPE_HOST
, filename
);
2804 const char *host_config_get(void)
2809 void install_default(enum node_type node
)
2811 install_element(node
, &config_exit_cmd
);
2812 install_element(node
, &config_quit_cmd
);
2813 install_element(node
, &config_end_cmd
);
2814 install_element(node
, &config_help_cmd
);
2815 install_element(node
, &config_list_cmd
);
2816 install_element(node
, &show_cli_graph_cmd
);
2817 install_element(node
, &find_cmd
);
2819 install_element(node
, &config_write_cmd
);
2820 install_element(node
, &show_running_config_cmd
);
2822 install_element(node
, &autocomplete_cmd
);
2824 nb_cli_install_default(node
);
2827 /* Initialize command interface. Install basic nodes and commands.
2829 * terminal = 0 -- vtysh / no logging, no config control
2830 * terminal = 1 -- normal daemon
2831 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2832 void cmd_init(int terminal
)
2834 struct utsname names
;
2836 if (array_size(node_names
) != NODE_TYPE_MAX
)
2837 assert(!"Update the CLI node description array!");
2842 /* register command preprocessors */
2843 hook_register(cmd_execute
, handle_pipe_action
);
2844 hook_register(cmd_execute_done
, handle_pipe_action_done
);
2846 varhandlers
= list_new();
2848 /* Allocate initial top vector of commands. */
2849 cmdvec
= vector_init(VECTOR_MIN_SIZE
);
2851 /* Default host value settings. */
2852 host
.name
= XSTRDUP(MTYPE_HOST
, names
.nodename
);
2853 #ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
2854 if ((strcmp(names
.domainname
, "(none)") == 0))
2855 host
.domainname
= NULL
;
2857 host
.domainname
= XSTRDUP(MTYPE_HOST
, names
.domainname
);
2859 host
.domainname
= NULL
;
2861 host
.password
= NULL
;
2863 host
.logfile
= NULL
;
2865 host
.noconfig
= (terminal
< 0);
2867 host
.motd
= default_motd
;
2868 host
.motdfile
= NULL
;
2870 /* Install top nodes. */
2871 install_node(&view_node
, NULL
);
2872 install_node(&enable_node
, NULL
);
2873 install_node(&auth_node
, NULL
);
2874 install_node(&auth_enable_node
, NULL
);
2875 install_node(&config_node
, config_write_host
);
2877 /* Each node's basic commands. */
2878 install_element(VIEW_NODE
, &show_version_cmd
);
2879 install_element(ENABLE_NODE
, &show_startup_config_cmd
);
2882 install_element(ENABLE_NODE
, &debug_memstats_cmd
);
2884 install_element(VIEW_NODE
, &config_list_cmd
);
2885 install_element(VIEW_NODE
, &config_exit_cmd
);
2886 install_element(VIEW_NODE
, &config_quit_cmd
);
2887 install_element(VIEW_NODE
, &config_help_cmd
);
2888 install_element(VIEW_NODE
, &config_enable_cmd
);
2889 install_element(VIEW_NODE
, &config_terminal_length_cmd
);
2890 install_element(VIEW_NODE
, &config_terminal_no_length_cmd
);
2891 install_element(VIEW_NODE
, &show_logging_cmd
);
2892 install_element(VIEW_NODE
, &show_commandtree_cmd
);
2893 install_element(VIEW_NODE
, &echo_cmd
);
2894 install_element(VIEW_NODE
, &autocomplete_cmd
);
2895 install_element(VIEW_NODE
, &find_cmd
);
2897 install_element(ENABLE_NODE
, &config_end_cmd
);
2898 install_element(ENABLE_NODE
, &config_disable_cmd
);
2899 install_element(ENABLE_NODE
, &config_terminal_cmd
);
2900 install_element(ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2901 install_element(ENABLE_NODE
, &config_write_cmd
);
2902 install_element(ENABLE_NODE
, &show_running_config_cmd
);
2903 install_element(ENABLE_NODE
, &config_logmsg_cmd
);
2905 install_default(CONFIG_NODE
);
2908 workqueue_cmd_init();
2912 install_element(CONFIG_NODE
, &hostname_cmd
);
2913 install_element(CONFIG_NODE
, &no_hostname_cmd
);
2914 install_element(CONFIG_NODE
, &domainname_cmd
);
2915 install_element(CONFIG_NODE
, &no_domainname_cmd
);
2916 install_element(CONFIG_NODE
, &frr_version_defaults_cmd
);
2919 install_element(CONFIG_NODE
, &debug_memstats_cmd
);
2921 install_element(CONFIG_NODE
, &password_cmd
);
2922 install_element(CONFIG_NODE
, &no_password_cmd
);
2923 install_element(CONFIG_NODE
, &enable_password_cmd
);
2924 install_element(CONFIG_NODE
, &no_enable_password_cmd
);
2926 install_element(CONFIG_NODE
, &config_log_stdout_cmd
);
2927 install_element(CONFIG_NODE
, &no_config_log_stdout_cmd
);
2928 install_element(CONFIG_NODE
, &config_log_monitor_cmd
);
2929 install_element(CONFIG_NODE
, &no_config_log_monitor_cmd
);
2930 install_element(CONFIG_NODE
, &config_log_file_cmd
);
2931 install_element(CONFIG_NODE
, &no_config_log_file_cmd
);
2932 install_element(CONFIG_NODE
, &config_log_syslog_cmd
);
2933 install_element(CONFIG_NODE
, &no_config_log_syslog_cmd
);
2934 install_element(CONFIG_NODE
, &config_log_facility_cmd
);
2935 install_element(CONFIG_NODE
, &no_config_log_facility_cmd
);
2936 install_element(CONFIG_NODE
, &config_log_record_priority_cmd
);
2937 install_element(CONFIG_NODE
,
2938 &no_config_log_record_priority_cmd
);
2939 install_element(CONFIG_NODE
,
2940 &config_log_timestamp_precision_cmd
);
2941 install_element(CONFIG_NODE
,
2942 &no_config_log_timestamp_precision_cmd
);
2943 install_element(CONFIG_NODE
, &service_password_encrypt_cmd
);
2944 install_element(CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2945 install_element(CONFIG_NODE
, &banner_motd_default_cmd
);
2946 install_element(CONFIG_NODE
, &banner_motd_file_cmd
);
2947 install_element(CONFIG_NODE
, &no_banner_motd_cmd
);
2948 install_element(CONFIG_NODE
, &service_terminal_length_cmd
);
2949 install_element(CONFIG_NODE
, &no_service_terminal_length_cmd
);
2951 vrf_install_commands();
2955 grammar_sandbox_init();
2959 void cmd_terminate(void)
2961 struct cmd_node
*cmd_node
;
2963 hook_unregister(cmd_execute
, handle_pipe_action
);
2964 hook_unregister(cmd_execute_done
, handle_pipe_action_done
);
2967 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++)
2968 if ((cmd_node
= vector_slot(cmdvec
, i
)) != NULL
) {
2969 // deleting the graph delets the cmd_element as
2971 graph_delete_graph(cmd_node
->cmdgraph
);
2972 vector_free(cmd_node
->cmd_vector
);
2973 hash_clean(cmd_node
->cmd_hash
, NULL
);
2974 hash_free(cmd_node
->cmd_hash
);
2975 cmd_node
->cmd_hash
= NULL
;
2978 vector_free(cmdvec
);
2982 XFREE(MTYPE_HOST
, host
.name
);
2983 XFREE(MTYPE_HOST
, host
.domainname
);
2984 XFREE(MTYPE_HOST
, host
.password
);
2985 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2986 XFREE(MTYPE_HOST
, host
.enable
);
2987 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2988 XFREE(MTYPE_HOST
, host
.logfile
);
2989 XFREE(MTYPE_HOST
, host
.motdfile
);
2990 XFREE(MTYPE_HOST
, host
.config
);
2992 list_delete(&varhandlers
);