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"
50 DEFINE_MTYPE(LIB
, HOST
, "Host config")
51 DEFINE_MTYPE(LIB
, COMPLETION
, "Completion item")
58 /* clang-format off */
59 const struct message tokennames
[] = {
64 item(IPV4_PREFIX_TKN
),
66 item(IPV6_PREFIX_TKN
),
76 const char *node_names
[] = {
79 "auth enable", // AUTH_ENABLE_NODE,
80 "enable", // ENABLE_NODE,
81 "config", // CONFIG_NODE,
82 "debug", // DEBUG_NODE,
83 "vrf debug", // VRF_DEBUG_NODE,
84 "vnc debug", // DEBUG_VNC_NODE,
86 "keychain", // KEYCHAIN_NODE,
87 "keychain key", // KEYCHAIN_KEY_NODE,
88 "logical-router", // LOGICALROUTER_NODE,
89 "static ip", // IP_NODE,
91 "interface", // INTERFACE_NODE,
92 "nexthop-group", // NH_GROUP_NODE,
93 "zebra", // ZEBRA_NODE,
94 "table", // TABLE_NODE,
96 "ripng", // RIPNG_NODE,
97 "babel", // BABEL_NODE,
98 "eigrp", // EIGRP_NODE,
100 "bgp vpnv4", // BGP_VPNV4_NODE,
101 "bgp vpnv6", // BGP_VPNV6_NODE,
102 "bgp ipv4 unicast", // BGP_IPV4_NODE,
103 "bgp ipv4 multicast", // BGP_IPV4M_NODE,
104 "bgp ipv4 labeled unicast", // BGP_IPV4L_NODE,
105 "bgp ipv6", // BGP_IPV6_NODE,
106 "bgp ipv6 multicast", // BGP_IPV6M_NODE,
107 "bgp ipv6 labeled unicast", // BGP_IPV6L_NODE,
108 "bgp vrf policy", // BGP_VRF_POLICY_NODE,
109 "bgp vnc defaults", // BGP_VNC_DEFAULTS_NODE,
110 "bgp vnc nve", // BGP_VNC_NVE_GROUP_NODE,
111 "bgp vnc l2", // BGP_VNC_L2_GROUP_NODE,
112 "rfp defaults", // RFP_DEFAULTS_NODE,
113 "bgp evpn", // BGP_EVPN_NODE,
114 "ospf", // OSPF_NODE,
115 "ospf6", // OSPF6_NODE,
117 "ldp ipv4", // LDP_IPV4_NODE,
118 "ldp ipv6", // LDP_IPV6_NODE,
119 "ldp ipv4 interface", // LDP_IPV4_IFACE_NODE,
120 "ldp ipv6 interface", // LDP_IPV6_IFACE_NODE,
121 "ldp l2vpn", // LDP_L2VPN_NODE,
122 "ldp", // LDP_PSEUDOWIRE_NODE,
123 "isis", // ISIS_NODE,
124 "ipv4 access list", // ACCESS_NODE,
125 "ipv4 prefix list", // PREFIX_NODE,
126 "ipv6 access list", // ACCESS_IPV6_NODE,
127 "MAC access list", // ACCESS_MAC_NODE,
128 "ipv6 prefix list", // PREFIX_IPV6_NODE,
129 "as list", // AS_LIST_NODE,
130 "community list", // COMMUNITY_LIST_NODE,
131 "routemap", // RMAP_NODE,
132 "pbr-map", // PBRMAP_NODE,
133 "smux", // SMUX_NODE,
134 "dump", // DUMP_NODE,
135 "forwarding", // FORWARDING_NODE,
136 "protocol", // PROTOCOL_NODE,
137 "mpls", // MPLS_NODE,
140 "link-params", // LINK_PARAMS_NODE,
141 "bgp evpn vni", // BGP_EVPN_VNI_NODE,
143 "bgp ipv4 flowspec", /* BGP_FLOWSPECV4_NODE
145 "bgp ipv6 flowspec", /* BGP_FLOWSPECV6_NODE
147 "bfd", /* BFD_NODE */
148 "bfd peer", /* BFD_PEER_NODE */
149 "openfabric", // OPENFABRIC_NODE
151 /* clang-format on */
153 /* Command vector which includes some level of command lists. Normally
154 each daemon maintains each own cmdvec. */
155 vector cmdvec
= NULL
;
157 /* Host information structure. */
161 * Returns host.name if any, otherwise
162 * it returns the system hostname.
164 const char *cmd_hostname_get(void)
170 * Returns unix domainname
172 const char *cmd_domainname_get(void)
174 return host
.domainname
;
177 /* Standard command node structures. */
178 static struct cmd_node auth_node
= {
179 AUTH_NODE
, "Password: ",
182 static struct cmd_node view_node
= {
186 static struct cmd_node auth_enable_node
= {
187 AUTH_ENABLE_NODE
, "Password: ",
190 static struct cmd_node enable_node
= {
194 static struct cmd_node config_node
= {CONFIG_NODE
, "%s(config)# ", 1};
196 /* Default motd string. */
197 static const char *default_motd
= FRR_DEFAULT_MOTD
;
199 static const struct facility_map
{
203 } syslog_facilities
[] = {
204 {LOG_KERN
, "kern", 1},
205 {LOG_USER
, "user", 2},
206 {LOG_MAIL
, "mail", 1},
207 {LOG_DAEMON
, "daemon", 1},
208 {LOG_AUTH
, "auth", 1},
209 {LOG_SYSLOG
, "syslog", 1},
211 {LOG_NEWS
, "news", 1},
212 {LOG_UUCP
, "uucp", 2},
213 {LOG_CRON
, "cron", 1},
217 {LOG_LOCAL0
, "local0", 6},
218 {LOG_LOCAL1
, "local1", 6},
219 {LOG_LOCAL2
, "local2", 6},
220 {LOG_LOCAL3
, "local3", 6},
221 {LOG_LOCAL4
, "local4", 6},
222 {LOG_LOCAL5
, "local5", 6},
223 {LOG_LOCAL6
, "local6", 6},
224 {LOG_LOCAL7
, "local7", 6},
228 static const char *facility_name(int facility
)
230 const struct facility_map
*fm
;
232 for (fm
= syslog_facilities
; fm
->name
; fm
++)
233 if (fm
->facility
== facility
)
238 static int facility_match(const char *str
)
240 const struct facility_map
*fm
;
242 for (fm
= syslog_facilities
; fm
->name
; fm
++)
243 if (!strncmp(str
, fm
->name
, fm
->match
))
248 static int level_match(const char *s
)
252 for (level
= 0; zlog_priority
[level
] != NULL
; level
++)
253 if (!strncmp(s
, zlog_priority
[level
], 2))
255 return ZLOG_DISABLED
;
258 /* This is called from main when a daemon is invoked with -v or --version. */
259 void print_version(const char *progname
)
261 printf("%s version %s\n", progname
, FRR_VERSION
);
262 printf("%s\n", FRR_COPYRIGHT
);
263 printf("configured with:\n\t%s\n", FRR_CONFIG_ARGS
);
266 char *argv_concat(struct cmd_token
**argv
, int argc
, int shift
)
268 int cnt
= MAX(argc
- shift
, 0);
269 const char *argstr
[cnt
+ 1];
274 for (int i
= 0; i
< cnt
; i
++)
275 argstr
[i
] = argv
[i
+ shift
]->arg
;
277 return frrstr_join(argstr
, cnt
, " ");
280 vector
cmd_make_strvec(const char *string
)
285 const char *copy
= string
;
287 /* skip leading whitespace */
288 while (isspace((int)*copy
) && *copy
!= '\0')
291 /* if the entire string was whitespace or a comment, return */
292 if (*copy
== '\0' || *copy
== '!' || *copy
== '#')
295 vector result
= frrstr_split_vec(copy
, "\n\r\t ");
297 for (unsigned int i
= 0; i
< vector_active(result
); i
++) {
298 if (strlen(vector_slot(result
, i
)) == 0) {
299 XFREE(MTYPE_TMP
, vector_slot(result
, i
));
300 vector_unset(result
, i
);
304 vector_compact(result
);
309 void cmd_free_strvec(vector v
)
311 frrstr_strvec_free(v
);
315 * Convenience function for accessing argv data.
319 * @param text definition snippet of the desired token
320 * @param index the starting index, and where to store the
321 * index of the found token if it exists
322 * @return 1 if found, 0 otherwise
324 int argv_find(struct cmd_token
**argv
, int argc
, const char *text
, int *index
)
327 for (int i
= *index
; i
< argc
&& found
== 0; i
++)
328 if ((found
= strmatch(text
, argv
[i
]->text
)))
333 static unsigned int cmd_hash_key(void *p
)
335 int size
= sizeof(p
);
337 return jhash(p
, size
, 0);
340 static int cmd_hash_cmp(const void *a
, const void *b
)
345 /* Install top node of command vector. */
346 void install_node(struct cmd_node
*node
, int (*func
)(struct vty
*))
348 vector_set_index(cmdvec
, node
->node
, node
);
350 node
->cmdgraph
= graph_new();
351 node
->cmd_vector
= vector_init(VECTOR_MIN_SIZE
);
353 struct cmd_token
*token
=
354 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
355 graph_new_node(node
->cmdgraph
, token
,
356 (void (*)(void *)) & cmd_token_del
);
357 node
->cmd_hash
= hash_create_size(16, cmd_hash_key
, cmd_hash_cmp
,
361 /* Return prompt character of specified node. */
362 const char *cmd_prompt(enum node_type node
)
364 struct cmd_node
*cnode
;
366 cnode
= vector_slot(cmdvec
, node
);
367 return cnode
->prompt
;
370 /* Install a command into a node. */
371 void install_element(enum node_type ntype
, struct cmd_element
*cmd
)
373 struct cmd_node
*cnode
;
375 /* cmd_init hasn't been called */
377 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
382 cnode
= vector_lookup(cmdvec
, ntype
);
387 "\tnode %d (%s) does not exist.\n"
388 "\tplease call install_node() before install_element()\n",
389 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
393 if (hash_lookup(cnode
->cmd_hash
, cmd
) != NULL
) {
396 "\tnode %d (%s) already has this command installed.\n"
397 "\tduplicate install_element call?\n",
398 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
402 assert(hash_get(cnode
->cmd_hash
, cmd
, hash_alloc_intern
));
404 struct graph
*graph
= graph_new();
405 struct cmd_token
*token
=
406 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
407 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
409 cmd_graph_parse(graph
, cmd
);
410 cmd_graph_names(graph
);
411 cmd_graph_merge(cnode
->cmdgraph
, graph
, +1);
412 graph_delete_graph(graph
);
414 vector_set(cnode
->cmd_vector
, cmd
);
416 if (ntype
== VIEW_NODE
)
417 install_element(ENABLE_NODE
, cmd
);
420 void uninstall_element(enum node_type ntype
, struct cmd_element
*cmd
)
422 struct cmd_node
*cnode
;
424 /* cmd_init hasn't been called */
426 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
431 cnode
= vector_lookup(cmdvec
, ntype
);
436 "\tnode %d (%s) does not exist.\n"
437 "\tplease call install_node() before uninstall_element()\n",
438 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
442 if (hash_release(cnode
->cmd_hash
, cmd
) == NULL
) {
445 "\tnode %d (%s) does not have this command installed.\n"
446 "\tduplicate uninstall_element call?\n",
447 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
451 vector_unset_value(cnode
->cmd_vector
, cmd
);
453 struct graph
*graph
= graph_new();
454 struct cmd_token
*token
=
455 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
456 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
458 cmd_graph_parse(graph
, cmd
);
459 cmd_graph_names(graph
);
460 cmd_graph_merge(cnode
->cmdgraph
, graph
, -1);
461 graph_delete_graph(graph
);
463 if (ntype
== VIEW_NODE
)
464 uninstall_element(ENABLE_NODE
, cmd
);
468 static const unsigned char itoa64
[] =
469 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
471 static void to64(char *s
, long v
, int n
)
474 *s
++ = itoa64
[v
& 0x3f];
479 static char *zencrypt(const char *passwd
)
483 char *crypt(const char *, const char *);
485 gettimeofday(&tv
, 0);
487 to64(&salt
[0], random(), 3);
488 to64(&salt
[3], tv
.tv_usec
, 3);
491 return crypt(passwd
, salt
);
494 /* This function write configuration of this host. */
495 static int config_write_host(struct vty
*vty
)
497 if (cmd_hostname_get())
498 vty_out(vty
, "hostname %s\n", cmd_hostname_get());
500 if (cmd_domainname_get())
501 vty_out(vty
, "domainname %s\n", cmd_domainname_get());
503 /* The following are all configuration commands that are not sent to
504 * watchfrr. For instance watchfrr is hardcoded to log to syslog so
505 * we would always display 'log syslog informational' in the config
506 * which would cause other daemons to then switch to syslog when they
509 if (strcmp(zlog_default
->protoname
, "WATCHFRR")) {
511 if (host
.password_encrypt
)
512 vty_out(vty
, "password 8 %s\n",
513 host
.password_encrypt
);
514 if (host
.enable_encrypt
)
515 vty_out(vty
, "enable password 8 %s\n",
516 host
.enable_encrypt
);
519 vty_out(vty
, "password %s\n", host
.password
);
521 vty_out(vty
, "enable password %s\n",
526 && (zlog_default
->maxlvl
[ZLOG_DEST_FILE
]
528 vty_out(vty
, "log file %s", host
.logfile
);
529 if (zlog_default
->maxlvl
[ZLOG_DEST_FILE
]
530 != zlog_default
->default_lvl
)
533 [zlog_default
->maxlvl
538 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != ZLOG_DISABLED
) {
539 vty_out(vty
, "log stdout");
540 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
]
541 != zlog_default
->default_lvl
)
544 [zlog_default
->maxlvl
545 [ZLOG_DEST_STDOUT
]]);
549 if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
550 vty_out(vty
, "no log monitor\n");
551 else if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]
552 != zlog_default
->default_lvl
)
553 vty_out(vty
, "log monitor %s\n",
554 zlog_priority
[zlog_default
->maxlvl
555 [ZLOG_DEST_MONITOR
]]);
557 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
) {
558 vty_out(vty
, "log syslog");
559 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
]
560 != zlog_default
->default_lvl
)
562 zlog_priority
[zlog_default
->maxlvl
563 [ZLOG_DEST_SYSLOG
]]);
567 if (zlog_default
->facility
!= LOG_DAEMON
)
568 vty_out(vty
, "log facility %s\n",
569 facility_name(zlog_default
->facility
));
571 if (zlog_default
->record_priority
== 1)
572 vty_out(vty
, "log record-priority\n");
574 if (zlog_default
->timestamp_precision
> 0)
575 vty_out(vty
, "log timestamp precision %d\n",
576 zlog_default
->timestamp_precision
);
579 vty_out(vty
, "service advanced-vty\n");
582 vty_out(vty
, "service password-encryption\n");
585 vty_out(vty
, "service terminal-length %d\n",
589 vty_out(vty
, "banner motd file %s\n", host
.motdfile
);
591 vty_out(vty
, "no banner motd\n");
594 if (debug_memstats_at_exit
)
595 vty_out(vty
, "!\ndebug memstats-at-exit\n");
600 /* Utility function for getting command graph. */
601 static struct graph
*cmd_node_graph(vector v
, enum node_type ntype
)
603 struct cmd_node
*cnode
= vector_slot(v
, ntype
);
604 return cnode
->cmdgraph
;
607 static int cmd_try_do_shortcut(enum node_type node
, char *first_word
)
609 if (first_word
!= NULL
&& node
!= AUTH_NODE
&& node
!= VIEW_NODE
610 && node
!= AUTH_ENABLE_NODE
&& 0 == strcmp("do", first_word
))
616 * Compare function for cmd_token.
617 * Used with qsort to sort command completions.
619 static int compare_completions(const void *fst
, const void *snd
)
621 const struct cmd_token
*first
= *(const struct cmd_token
* const *)fst
,
622 *secnd
= *(const struct cmd_token
* const *)snd
;
623 return strcmp(first
->text
, secnd
->text
);
627 * Takes a list of completions returned by command_complete,
628 * dedeuplicates them based on both text and description,
629 * sorts them, and returns them as a vector.
631 * @param completions linked list of cmd_token
632 * @return deduplicated and sorted vector with
634 vector
completions_to_vec(struct list
*completions
)
636 vector comps
= vector_init(VECTOR_MIN_SIZE
);
639 struct cmd_token
*token
, *cr
= NULL
;
640 unsigned int i
, exists
;
641 for (ALL_LIST_ELEMENTS_RO(completions
, ln
, token
)) {
642 if (token
->type
== END_TKN
&& (cr
= token
))
645 // linear search for token in completions vector
647 for (i
= 0; i
< vector_active(comps
) && !exists
; i
++) {
648 struct cmd_token
*curr
= vector_slot(comps
, i
);
650 exists
= !strcmp(curr
->text
, token
->text
)
651 && !strcmp(curr
->desc
, token
->desc
);
653 exists
= !strcmp(curr
->text
, token
->text
);
654 #endif /* VTYSH_DEBUG */
658 vector_set(comps
, token
);
662 qsort(comps
->index
, vector_active(comps
), sizeof(void *),
663 &compare_completions
);
665 // make <cr> the first element, if it is present
667 vector_set_index(comps
, vector_active(comps
), NULL
);
668 memmove(comps
->index
+ 1, comps
->index
,
669 (comps
->alloced
- 1) * sizeof(void *));
670 vector_set_index(comps
, 0, cr
);
676 * Generates a vector of cmd_token representing possible completions
677 * on the current input.
679 * @param vline the vectorized input line
680 * @param vty the vty with the node to match on
681 * @param status pointer to matcher status code
682 * @return vector of struct cmd_token * with possible completions
684 static vector
cmd_complete_command_real(vector vline
, struct vty
*vty
,
687 struct list
*completions
;
688 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
690 enum matcher_rv rv
= command_complete(cmdgraph
, vline
, &completions
);
692 if (MATCHER_ERROR(rv
)) {
693 *status
= CMD_ERR_NO_MATCH
;
697 vector comps
= completions_to_vec(completions
);
698 list_delete(&completions
);
700 // set status code appropriately
701 switch (vector_active(comps
)) {
703 *status
= CMD_ERR_NO_MATCH
;
706 *status
= CMD_COMPLETE_FULL_MATCH
;
709 *status
= CMD_COMPLETE_LIST_MATCH
;
715 vector
cmd_describe_command(vector vline
, struct vty
*vty
, int *status
)
719 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
720 enum node_type onode
;
721 vector shifted_vline
;
725 vty
->node
= ENABLE_NODE
;
726 /* We can try it on enable node, cos' the vty is authenticated
729 shifted_vline
= vector_init(vector_count(vline
));
731 for (index
= 1; index
< vector_active(vline
); index
++) {
732 vector_set_index(shifted_vline
, index
- 1,
733 vector_lookup(vline
, index
));
736 ret
= cmd_complete_command_real(shifted_vline
, vty
, status
);
738 vector_free(shifted_vline
);
743 return cmd_complete_command_real(vline
, vty
, status
);
746 static struct list
*varhandlers
= NULL
;
748 void cmd_variable_complete(struct cmd_token
*token
, const char *arg
,
752 const struct cmd_variable_handler
*cvh
;
756 tmpcomps
= arg
? vector_init(VECTOR_MIN_SIZE
) : comps
;
758 for (ALL_LIST_ELEMENTS_RO(varhandlers
, ln
, cvh
)) {
759 if (cvh
->tokenname
&& strcmp(cvh
->tokenname
, token
->text
))
761 if (cvh
->varname
&& (!token
->varname
762 || strcmp(cvh
->varname
, token
->varname
)))
764 cvh
->completions(tmpcomps
, token
);
772 for (i
= vector_active(tmpcomps
); i
; i
--) {
773 char *item
= vector_slot(tmpcomps
, i
- 1);
774 if (strlen(item
) >= argsz
&& !strncmp(item
, arg
, argsz
))
775 vector_set(comps
, item
);
777 XFREE(MTYPE_COMPLETION
, item
);
779 vector_free(tmpcomps
);
782 #define AUTOCOMP_INDENT 5
784 char *cmd_variable_comp2str(vector comps
, unsigned short cols
)
787 char *buf
= XCALLOC(MTYPE_TMP
, bsz
);
788 int lc
= AUTOCOMP_INDENT
;
789 size_t cs
= AUTOCOMP_INDENT
;
791 snprintf(buf
, bsz
, "%*s", AUTOCOMP_INDENT
, "");
792 for (size_t j
= 0; j
< vector_active(comps
); j
++) {
793 char *item
= vector_slot(comps
, j
);
794 itemlen
= strlen(item
);
796 if (cs
+ itemlen
+ AUTOCOMP_INDENT
+ 3 >= bsz
)
797 buf
= XREALLOC(MTYPE_TMP
, buf
, (bsz
*= 2));
799 if (lc
+ itemlen
+ 1 >= cols
) {
800 cs
+= snprintf(&buf
[cs
], bsz
- cs
, "\n%*s",
801 AUTOCOMP_INDENT
, "");
802 lc
= AUTOCOMP_INDENT
;
805 size_t written
= snprintf(&buf
[cs
], bsz
- cs
, "%s ", item
);
808 XFREE(MTYPE_COMPLETION
, item
);
809 vector_set_index(comps
, j
, NULL
);
814 void cmd_variable_handler_register(const struct cmd_variable_handler
*cvh
)
819 for (; cvh
->completions
; cvh
++)
820 listnode_add(varhandlers
, (void *)cvh
);
823 DEFUN_HIDDEN (autocomplete
,
825 "autocomplete TYPE TEXT VARNAME",
826 "Autocompletion handler (internal, for vtysh)\n"
829 "cmd_token->varname\n")
831 struct cmd_token tok
;
832 vector comps
= vector_init(32);
835 memset(&tok
, 0, sizeof(tok
));
836 tok
.type
= atoi(argv
[1]->arg
);
837 tok
.text
= argv
[2]->arg
;
838 tok
.varname
= argv
[3]->arg
;
839 if (!strcmp(tok
.varname
, "-"))
842 cmd_variable_complete(&tok
, NULL
, comps
);
844 for (i
= 0; i
< vector_active(comps
); i
++) {
845 char *text
= vector_slot(comps
, i
);
846 vty_out(vty
, "%s\n", text
);
847 XFREE(MTYPE_COMPLETION
, text
);
855 * Generate possible tab-completions for the given input. This function only
856 * returns results that would result in a valid command if used as Readline
857 * completions (as is the case in vtysh). For instance, if the passed vline ends
858 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
860 * @param vline vectorized input line
862 * @param status location to store matcher status code in
863 * @return set of valid strings for use with Readline as tab-completions.
866 char **cmd_complete_command(vector vline
, struct vty
*vty
, int *status
)
869 int original_node
= vty
->node
;
870 vector input_line
= vector_init(vector_count(vline
));
872 // if the first token is 'do' we'll want to execute the command in the
874 int do_shortcut
= cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0));
875 vty
->node
= do_shortcut
? ENABLE_NODE
: original_node
;
877 // construct the input line we'll be matching on
878 unsigned int offset
= (do_shortcut
) ? 1 : 0;
879 for (unsigned index
= 0; index
+ offset
< vector_active(vline
); index
++)
880 vector_set_index(input_line
, index
,
881 vector_lookup(vline
, index
+ offset
));
883 // get token completions -- this is a copying operation
884 vector comps
= NULL
, initial_comps
;
885 initial_comps
= cmd_complete_command_real(input_line
, vty
, status
);
887 if (!MATCHER_ERROR(*status
)) {
888 assert(initial_comps
);
889 // filter out everything that is not suitable for a
891 comps
= vector_init(VECTOR_MIN_SIZE
);
892 for (unsigned int i
= 0; i
< vector_active(initial_comps
);
894 struct cmd_token
*token
= vector_slot(initial_comps
, i
);
895 if (token
->type
== WORD_TKN
)
896 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
,
898 else if (IS_VARYING_TOKEN(token
->type
)) {
899 const char *ref
= vector_lookup(
900 vline
, vector_active(vline
) - 1);
901 cmd_variable_complete(token
, ref
, comps
);
904 vector_free(initial_comps
);
906 // since we filtered results, we need to re-set status code
907 switch (vector_active(comps
)) {
909 *status
= CMD_ERR_NO_MATCH
;
912 *status
= CMD_COMPLETE_FULL_MATCH
;
915 *status
= CMD_COMPLETE_LIST_MATCH
;
918 // copy completions text into an array of char*
919 ret
= XMALLOC(MTYPE_TMP
,
920 (vector_active(comps
) + 1) * sizeof(char *));
922 for (i
= 0; i
< vector_active(comps
); i
++) {
923 ret
[i
] = vector_slot(comps
, i
);
925 // set the last element to NULL, because this array is used in
926 // a Readline completion_generator function which expects NULL
927 // as a sentinel value
931 } else if (initial_comps
)
932 vector_free(initial_comps
);
934 // comps should always be null here
937 // free the adjusted input line
938 vector_free(input_line
);
940 // reset vty->node to its original value
941 vty
->node
= original_node
;
946 /* return parent node */
947 /* MUST eventually converge on CONFIG_NODE */
948 enum node_type
node_parent(enum node_type node
)
952 assert(node
> CONFIG_NODE
);
957 case BGP_FLOWSPECV4_NODE
:
958 case BGP_FLOWSPECV6_NODE
:
959 case BGP_VRF_POLICY_NODE
:
960 case BGP_VNC_DEFAULTS_NODE
:
961 case BGP_VNC_NVE_GROUP_NODE
:
962 case BGP_VNC_L2_GROUP_NODE
:
972 case BGP_EVPN_VNI_NODE
:
975 case KEYCHAIN_KEY_NODE
:
978 case LINK_PARAMS_NODE
:
979 ret
= INTERFACE_NODE
;
985 case LDP_IPV4_IFACE_NODE
:
988 case LDP_IPV6_IFACE_NODE
:
991 case LDP_PSEUDOWIRE_NODE
:
992 ret
= LDP_L2VPN_NODE
;
1005 /* Execute command by argument vline vector. */
1006 static int cmd_execute_command_real(vector vline
, enum filter_type filter
,
1008 const struct cmd_element
**cmd
)
1010 struct list
*argv_list
;
1011 enum matcher_rv status
;
1012 const struct cmd_element
*matched_element
= NULL
;
1014 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
1015 status
= command_match(cmdgraph
, vline
, &argv_list
, &matched_element
);
1018 *cmd
= matched_element
;
1020 // if matcher error, return corresponding CMD_ERR
1021 if (MATCHER_ERROR(status
)) {
1023 list_delete(&argv_list
);
1025 case MATCHER_INCOMPLETE
:
1026 return CMD_ERR_INCOMPLETE
;
1027 case MATCHER_AMBIGUOUS
:
1028 return CMD_ERR_AMBIGUOUS
;
1030 return CMD_ERR_NO_MATCH
;
1034 // build argv array from argv list
1035 struct cmd_token
**argv
= XMALLOC(
1036 MTYPE_TMP
, argv_list
->count
* sizeof(struct cmd_token
*));
1037 struct listnode
*ln
;
1038 struct cmd_token
*token
;
1040 for (ALL_LIST_ELEMENTS_RO(argv_list
, ln
, token
))
1043 int argc
= argv_list
->count
;
1046 if (matched_element
->daemon
)
1047 ret
= CMD_SUCCESS_DAEMON
;
1049 ret
= matched_element
->func(matched_element
, vty
, argc
, argv
);
1051 // delete list and cmd_token's in it
1052 list_delete(&argv_list
);
1053 XFREE(MTYPE_TMP
, argv
);
1059 * Execute a given command, handling things like "do ..." and checking
1060 * whether the given command might apply at a parent node if doesn't
1061 * apply for the current node.
1063 * @param vline Command line input, vector of char* where each element is
1065 * @param vty The vty context in which the command should be executed.
1066 * @param cmd Pointer where the struct cmd_element of the matched command
1067 * will be stored, if any. May be set to NULL if this info is
1069 * @param vtysh If set != 0, don't lookup the command at parent nodes.
1070 * @return The status of the command that has been executed or an error code
1071 * as to why no command could be executed.
1073 int cmd_execute_command(vector vline
, struct vty
*vty
,
1074 const struct cmd_element
**cmd
, int vtysh
)
1076 int ret
, saved_ret
= 0;
1077 enum node_type onode
, try_node
;
1079 onode
= try_node
= vty
->node
;
1081 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
1082 vector shifted_vline
;
1085 vty
->node
= ENABLE_NODE
;
1086 /* We can try it on enable node, cos' the vty is authenticated
1089 shifted_vline
= vector_init(vector_count(vline
));
1091 for (index
= 1; index
< vector_active(vline
); index
++)
1092 vector_set_index(shifted_vline
, index
- 1,
1093 vector_lookup(vline
, index
));
1095 ret
= cmd_execute_command_real(shifted_vline
, FILTER_RELAXED
,
1098 vector_free(shifted_vline
);
1104 cmd_execute_command_real(vline
, FILTER_RELAXED
, vty
, cmd
);
1109 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1110 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
) {
1111 /* This assumes all nodes above CONFIG_NODE are childs of
1113 while (vty
->node
> CONFIG_NODE
) {
1114 try_node
= node_parent(try_node
);
1115 vty
->node
= try_node
;
1116 ret
= cmd_execute_command_real(vline
, FILTER_RELAXED
,
1118 if (ret
== CMD_SUCCESS
|| ret
== CMD_WARNING
1119 || ret
== CMD_NOT_MY_INSTANCE
1120 || ret
== CMD_WARNING_CONFIG_FAILED
)
1123 /* no command succeeded, reset the vty to the original node */
1127 /* return command status for original node */
1132 * Execute a given command, matching it strictly against the current node.
1133 * This mode is used when reading config files.
1135 * @param vline Command line input, vector of char* where each element is
1137 * @param vty The vty context in which the command should be executed.
1138 * @param cmd Pointer where the struct cmd_element* of the matched command
1139 * will be stored, if any. May be set to NULL if this info is
1141 * @return The status of the command that has been executed or an error code
1142 * as to why no command could be executed.
1144 int cmd_execute_command_strict(vector vline
, struct vty
*vty
,
1145 const struct cmd_element
**cmd
)
1147 return cmd_execute_command_real(vline
, FILTER_STRICT
, vty
, cmd
);
1151 * Hook for preprocessing command string before executing.
1153 * All subscribers are called with the raw command string that is to be
1154 * executed. If any changes are to be made, a new string should be allocated
1155 * with MTYPE_TMP and *cmd_out updated to point to this new string. The caller
1156 * is then responsible for freeing this string.
1158 * All processing functions must be mutually exclusive in their action, i.e. if
1159 * one subscriber decides to modify the command, all others must not modify it
1160 * when called. Feeding the output of one processing command into a subsequent
1161 * one is not supported.
1163 * This hook is intentionally internal to the command processing system.
1166 * The raw command string.
1169 * The result of any processing.
1171 DECLARE_HOOK(cmd_execute
,
1172 (struct vty
*vty
, const char *cmd_in
, char **cmd_out
),
1173 (vty
, cmd_in
, cmd_out
));
1174 DEFINE_HOOK(cmd_execute
, (struct vty
*vty
, const char *cmd_in
, char **cmd_out
),
1175 (vty
, cmd_in
, cmd_out
));
1177 /* Hook executed after a CLI command. */
1178 DECLARE_KOOH(cmd_execute_done
, (struct vty
*vty
, const char *cmd_exec
),
1180 DEFINE_KOOH(cmd_execute_done
, (struct vty
*vty
, const char *cmd_exec
),
1184 * cmd_execute hook subscriber to handle `|` actions.
1186 static int handle_pipe_action(struct vty
*vty
, const char *cmd_in
,
1190 char *orig
, *working
, *token
, *u
;
1191 char *pipe
= strstr(cmd_in
, "| ");
1196 /* duplicate string for processing purposes, not including pipe */
1197 orig
= working
= XSTRDUP(MTYPE_TMP
, pipe
+ 2);
1199 /* retrieve action */
1200 token
= strsep(&working
, " ");
1203 /* match result to known actions */
1204 if (strmatch(token
, "include")) {
1205 /* the remaining text should be a regexp */
1206 char *regexp
= working
;
1209 vty_out(vty
, "%% Need a regexp to filter with\n");
1213 bool succ
= vty_set_include(vty
, regexp
);
1216 vty_out(vty
, "%% Bad regexp '%s'\n", regexp
);
1219 *cmd_out
= XSTRDUP(MTYPE_TMP
, cmd_in
);
1223 vty_out(vty
, "%% Unknown action '%s'\n", token
);
1228 XFREE(MTYPE_TMP
, orig
);
1232 static int handle_pipe_action_done(struct vty
*vty
, const char *cmd_exec
)
1235 vty_set_include(vty
, NULL
);
1240 int cmd_execute(struct vty
*vty
, const char *cmd
,
1241 const struct cmd_element
**matched
, int vtysh
)
1244 char *cmd_out
= NULL
;
1245 const char *cmd_exec
;
1248 hook_call(cmd_execute
, vty
, cmd
, &cmd_out
);
1249 cmd_exec
= cmd_out
? (const char *)cmd_out
: cmd
;
1251 vline
= cmd_make_strvec(cmd_exec
);
1254 ret
= cmd_execute_command(vline
, vty
, matched
, vtysh
);
1255 cmd_free_strvec(vline
);
1260 hook_call(cmd_execute_done
, vty
, cmd_exec
);
1263 XFREE(MTYPE_TMP
, cmd_out
);
1270 * Parse one line of config, walking up the parse tree attempting to find a
1273 * @param vty The vty context in which the command should be executed.
1274 * @param cmd Pointer where the struct cmd_element* of the match command
1275 * will be stored, if any. May be set to NULL if this info is
1277 * @param use_daemon Boolean to control whether or not we match on
1278 * CMD_SUCCESS_DAEMON
1280 * @return The status of the command that has been executed or an error code
1281 * as to why no command could be executed.
1283 int command_config_read_one_line(struct vty
*vty
,
1284 const struct cmd_element
**cmd
,
1285 uint32_t line_num
, int use_daemon
)
1291 vline
= cmd_make_strvec(vty
->buf
);
1293 /* In case of comment line */
1297 /* Execute configuration command : this is strict match */
1298 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1300 // Climb the tree and try the command again at each node
1301 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1302 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1303 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1304 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
1305 && vty
->node
!= CONFIG_NODE
) {
1307 saved_node
= vty
->node
;
1309 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1310 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1311 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1312 && vty
->node
> CONFIG_NODE
) {
1313 vty
->node
= node_parent(vty
->node
);
1314 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1317 // If climbing the tree did not work then ignore the command and
1318 // stay at the same node
1319 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1320 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1321 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
) {
1322 vty
->node
= saved_node
;
1326 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
) {
1327 struct vty_error
*ve
= XCALLOC(MTYPE_TMP
, sizeof(*ve
));
1329 memcpy(ve
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
1330 ve
->line_num
= line_num
;
1332 vty
->error
= list_new();
1334 listnode_add(vty
->error
, ve
);
1337 cmd_free_strvec(vline
);
1342 /* Configuration make from file. */
1343 int config_from_file(struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
1345 int ret
, error_ret
= 0;
1348 while (fgets(vty
->buf
, VTY_BUFSIZ
, fp
)) {
1351 ret
= command_config_read_one_line(vty
, NULL
, *line_num
, 0);
1353 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1354 && ret
!= CMD_ERR_NOTHING_TODO
)
1365 /* Configuration from terminal */
1366 DEFUN (config_terminal
,
1367 config_terminal_cmd
,
1368 "configure terminal",
1369 "Configuration from vty interface\n"
1370 "Configuration terminal\n")
1372 if (vty_config_lock(vty
))
1373 vty
->node
= CONFIG_NODE
;
1375 vty_out(vty
, "VTY configuration is locked by other VTY\n");
1376 return CMD_WARNING_CONFIG_FAILED
;
1381 /* Enable command */
1385 "Turn on privileged mode command\n")
1387 /* If enable password is NULL, change to ENABLE_NODE */
1388 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
)
1389 || vty
->type
== VTY_SHELL_SERV
)
1390 vty
->node
= ENABLE_NODE
;
1392 vty
->node
= AUTH_ENABLE_NODE
;
1397 /* Disable command */
1401 "Turn off privileged mode command\n")
1403 if (vty
->node
== ENABLE_NODE
)
1404 vty
->node
= VIEW_NODE
;
1408 /* Down vty node level. */
1412 "Exit current mode and down to previous mode\n")
1418 void cmd_exit(struct vty
*vty
)
1420 switch (vty
->node
) {
1426 vty
->status
= VTY_CLOSE
;
1429 vty
->node
= ENABLE_NODE
;
1430 vty_config_unlock(vty
);
1432 case INTERFACE_NODE
:
1434 case LOGICALROUTER_NODE
:
1446 case LDP_L2VPN_NODE
:
1448 case OPENFABRIC_NODE
:
1454 vty
->node
= CONFIG_NODE
;
1457 case BGP_IPV4M_NODE
:
1458 case BGP_IPV4L_NODE
:
1459 case BGP_VPNV4_NODE
:
1460 case BGP_VPNV6_NODE
:
1461 case BGP_FLOWSPECV4_NODE
:
1462 case BGP_FLOWSPECV6_NODE
:
1463 case BGP_VRF_POLICY_NODE
:
1464 case BGP_VNC_DEFAULTS_NODE
:
1465 case BGP_VNC_NVE_GROUP_NODE
:
1466 case BGP_VNC_L2_GROUP_NODE
:
1468 case BGP_IPV6M_NODE
:
1470 case BGP_IPV6L_NODE
:
1471 vty
->node
= BGP_NODE
;
1473 case BGP_EVPN_VNI_NODE
:
1474 vty
->node
= BGP_EVPN_NODE
;
1478 vty
->node
= LDP_NODE
;
1480 case LDP_IPV4_IFACE_NODE
:
1481 vty
->node
= LDP_IPV4_NODE
;
1483 case LDP_IPV6_IFACE_NODE
:
1484 vty
->node
= LDP_IPV6_NODE
;
1486 case LDP_PSEUDOWIRE_NODE
:
1487 vty
->node
= LDP_L2VPN_NODE
;
1489 case KEYCHAIN_KEY_NODE
:
1490 vty
->node
= KEYCHAIN_NODE
;
1492 case LINK_PARAMS_NODE
:
1493 vty
->node
= INTERFACE_NODE
;
1496 vty
->node
= BFD_NODE
;
1507 "Exit current mode and down to previous mode\n")
1509 return config_exit(self
, vty
, argc
, argv
);
1513 /* End of configuration. */
1517 "End current mode and change to enable mode.\n")
1519 switch (vty
->node
) {
1522 /* Nothing to do. */
1525 case INTERFACE_NODE
:
1527 case LOGICALROUTER_NODE
:
1536 case BGP_VRF_POLICY_NODE
:
1537 case BGP_VNC_DEFAULTS_NODE
:
1538 case BGP_VNC_NVE_GROUP_NODE
:
1539 case BGP_VNC_L2_GROUP_NODE
:
1540 case BGP_VPNV4_NODE
:
1541 case BGP_VPNV6_NODE
:
1542 case BGP_FLOWSPECV4_NODE
:
1543 case BGP_FLOWSPECV6_NODE
:
1545 case BGP_IPV4M_NODE
:
1546 case BGP_IPV4L_NODE
:
1548 case BGP_IPV6M_NODE
:
1550 case BGP_EVPN_VNI_NODE
:
1551 case BGP_IPV6L_NODE
:
1559 case LDP_IPV4_IFACE_NODE
:
1560 case LDP_IPV6_IFACE_NODE
:
1561 case LDP_L2VPN_NODE
:
1562 case LDP_PSEUDOWIRE_NODE
:
1564 case OPENFABRIC_NODE
:
1566 case KEYCHAIN_KEY_NODE
:
1568 case LINK_PARAMS_NODE
:
1571 vty_config_unlock(vty
);
1572 vty
->node
= ENABLE_NODE
;
1581 DEFUN (show_version
,
1585 "Displays zebra version\n")
1587 vty_out(vty
, "%s %s (%s).\n", FRR_FULL_NAME
, FRR_VERSION
,
1588 cmd_hostname_get() ? cmd_hostname_get() : "");
1589 vty_out(vty
, "%s%s\n", FRR_COPYRIGHT
, GIT_INFO
);
1590 vty_out(vty
, "configured with:\n %s\n", FRR_CONFIG_ARGS
);
1595 /* "Set" version ... ignore version tags */
1596 DEFUN (frr_version_defaults
,
1597 frr_version_defaults_cmd
,
1598 "frr <version|defaults> LINE...",
1599 "FRRouting global parameters\n"
1600 "version configuration was written by\n"
1601 "set of configuration defaults used\n"
1607 /* Help display function for all node. */
1611 "Description of the interactive help system\n")
1614 "Quagga VTY provides advanced help feature. When you need help,\n\
1615 anytime at the command line please press '?'.\n\
1617 If nothing matches, the help list will be empty and you must backup\n\
1618 until entering a '?' shows the available options.\n\
1619 Two styles of help are provided:\n\
1620 1. Full help is available when you are ready to enter a\n\
1621 command argument (e.g. 'show ?') and describes each possible\n\
1623 2. Partial help is provided when an abbreviated argument is entered\n\
1624 and you want to know what arguments match the input\n\
1625 (e.g. 'show me?'.)\n\n");
1629 static void permute(struct graph_node
*start
, struct vty
*vty
)
1631 static struct list
*position
= NULL
;
1633 position
= list_new();
1635 struct cmd_token
*stok
= start
->data
;
1636 struct graph_node
*gnn
;
1637 struct listnode
*ln
;
1640 listnode_add(position
, start
);
1641 for (unsigned int i
= 0; i
< vector_active(start
->to
); i
++) {
1642 struct graph_node
*gn
= vector_slot(start
->to
, i
);
1643 struct cmd_token
*tok
= gn
->data
;
1644 if (tok
->attr
== CMD_ATTR_HIDDEN
1645 || tok
->attr
== CMD_ATTR_DEPRECATED
)
1647 else if (tok
->type
== END_TKN
|| gn
== start
) {
1649 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
)) {
1650 struct cmd_token
*tt
= gnn
->data
;
1651 if (tt
->type
< SPECIAL_TKN
)
1652 vty_out(vty
, " %s", tt
->text
);
1655 vty_out(vty
, "...");
1659 if (stok
->type
== FORK_TKN
&& tok
->type
!= FORK_TKN
)
1660 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
))
1669 list_delete_node(position
, listtail(position
));
1672 int cmd_list_cmds(struct vty
*vty
, int do_permute
)
1674 struct cmd_node
*node
= vector_slot(cmdvec
, vty
->node
);
1677 permute(vector_slot(node
->cmdgraph
->nodes
, 0), vty
);
1679 /* loop over all commands at this node */
1680 struct cmd_element
*element
= NULL
;
1681 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
);
1683 if ((element
= vector_slot(node
->cmd_vector
, i
))
1684 && element
->attr
!= CMD_ATTR_DEPRECATED
1685 && element
->attr
!= CMD_ATTR_HIDDEN
)
1686 vty_out(vty
, " %s\n", element
->string
);
1691 /* Help display function for all node. */
1694 "list [permutations]",
1695 "Print command list\n"
1696 "Print all possible command permutations\n")
1698 return cmd_list_cmds(vty
, argc
== 2);
1701 DEFUN (show_commandtree
,
1702 show_commandtree_cmd
,
1703 "show commandtree [permutations]",
1705 "Show command tree\n"
1706 "Permutations that we are interested in\n")
1708 return cmd_list_cmds(vty
, argc
== 3);
1711 DEFUN_HIDDEN(show_cli_graph
,
1716 "Dump current command space as DOT graph\n")
1718 struct cmd_node
*cn
= vector_slot(cmdvec
, vty
->node
);
1719 char *dot
= cmd_graph_dump_dot(cn
->cmdgraph
);
1721 vty_out(vty
, "%s\n", dot
);
1722 XFREE(MTYPE_TMP
, dot
);
1726 static int vty_write_config(struct vty
*vty
)
1729 struct cmd_node
*node
;
1734 if (vty
->type
== VTY_TERM
) {
1735 vty_out(vty
, "\nCurrent configuration:\n");
1736 vty_out(vty
, "!\n");
1739 vty_out(vty
, "frr version %s\n", FRR_VER_SHORT
);
1740 vty_out(vty
, "frr defaults %s\n", DFLT_NAME
);
1741 vty_out(vty
, "!\n");
1743 for (i
= 0; i
< vector_active(cmdvec
); i
++)
1744 if ((node
= vector_slot(cmdvec
, i
)) && node
->func
1745 && (node
->vtysh
|| vty
->type
!= VTY_SHELL
)) {
1746 if ((*node
->func
)(vty
))
1747 vty_out(vty
, "!\n");
1750 if (vty
->type
== VTY_TERM
) {
1751 vty_out(vty
, "end\n");
1757 static int file_write_config(struct vty
*vty
)
1760 char *config_file
, *slash
;
1761 char *config_file_tmp
= NULL
;
1762 char *config_file_sav
= NULL
;
1763 int ret
= CMD_WARNING
;
1764 struct vty
*file_vty
;
1765 struct stat conf_stat
;
1770 /* Check and see if we are operating under vtysh configuration */
1771 if (host
.config
== NULL
) {
1773 "Can't save to configuration file, using vtysh.\n");
1778 config_file
= host
.config
;
1781 #define O_DIRECTORY 0
1783 slash
= strrchr(config_file
, '/');
1785 char *config_dir
= XSTRDUP(MTYPE_TMP
, config_file
);
1786 config_dir
[slash
- config_file
] = '\0';
1787 dirfd
= open(config_dir
, O_DIRECTORY
| O_RDONLY
);
1788 XFREE(MTYPE_TMP
, config_dir
);
1790 dirfd
= open(".", O_DIRECTORY
| O_RDONLY
);
1791 /* if dirfd is invalid, directory sync fails, but we're still OK */
1793 config_file_sav
= XMALLOC(
1794 MTYPE_TMP
, strlen(config_file
) + strlen(CONF_BACKUP_EXT
) + 1);
1795 strcpy(config_file_sav
, config_file
);
1796 strcat(config_file_sav
, CONF_BACKUP_EXT
);
1799 config_file_tmp
= XMALLOC(MTYPE_TMP
, strlen(config_file
) + 8);
1800 sprintf(config_file_tmp
, "%s.XXXXXX", config_file
);
1802 /* Open file to configuration write. */
1803 fd
= mkstemp(config_file_tmp
);
1805 vty_out(vty
, "Can't open configuration file %s.\n",
1809 if (fchmod(fd
, CONFIGFILE_MASK
) != 0) {
1810 vty_out(vty
, "Can't chmod configuration file %s: %s (%d).\n",
1811 config_file_tmp
, safe_strerror(errno
), errno
);
1815 /* Make vty for configuration file. */
1816 file_vty
= vty_new();
1818 file_vty
->type
= VTY_FILE
;
1820 /* Config file header print. */
1821 vty_out(file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1822 vty_time_print(file_vty
, 1);
1823 vty_out(file_vty
, "!\n");
1824 vty_write_config(file_vty
);
1825 vty_close(file_vty
);
1827 if (stat(config_file
, &conf_stat
) >= 0) {
1828 if (unlink(config_file_sav
) != 0)
1829 if (errno
!= ENOENT
) {
1831 "Can't unlink backup configuration file %s.\n",
1835 if (link(config_file
, config_file_sav
) != 0) {
1837 "Can't backup old configuration file %s.\n",
1844 if (rename(config_file_tmp
, config_file
) != 0) {
1845 vty_out(vty
, "Can't save configuration file %s.\n",
1852 vty_out(vty
, "Configuration saved to %s\n", config_file
);
1856 if (ret
!= CMD_SUCCESS
)
1857 unlink(config_file_tmp
);
1860 XFREE(MTYPE_TMP
, config_file_tmp
);
1861 XFREE(MTYPE_TMP
, config_file_sav
);
1865 /* Write current configuration into file. */
1867 DEFUN (config_write
,
1869 "write [<file|memory|terminal>]",
1870 "Write running configuration to memory, network, or terminal\n"
1871 "Write to configuration file\n"
1872 "Write configuration currently in memory\n"
1873 "Write configuration to terminal\n")
1875 const int idx_type
= 1;
1877 // if command was 'write terminal' or 'write memory'
1878 if (argc
== 2 && (!strcmp(argv
[idx_type
]->text
, "terminal"))) {
1879 return vty_write_config(vty
);
1882 return file_write_config(vty
);
1885 /* ALIAS_FIXME for 'write <terminal|memory>' */
1886 DEFUN (show_running_config
,
1887 show_running_config_cmd
,
1888 "show running-config",
1890 "running configuration (same as write terminal)\n")
1892 return vty_write_config(vty
);
1895 /* ALIAS_FIXME for 'write file' */
1896 DEFUN (copy_runningconf_startupconf
,
1897 copy_runningconf_startupconf_cmd
,
1898 "copy running-config startup-config",
1899 "Copy configuration\n"
1900 "Copy running config to... \n"
1901 "Copy running config to startup config (same as write file/memory)\n")
1903 return file_write_config(vty
);
1907 /* Write startup configuration into the terminal. */
1908 DEFUN (show_startup_config
,
1909 show_startup_config_cmd
,
1910 "show startup-config",
1912 "Contents of startup configuration\n")
1919 if (host
.config
== NULL
)
1922 confp
= fopen(host
.config
, "r");
1923 if (confp
== NULL
) {
1924 vty_out(vty
, "Can't open configuration file [%s] due to '%s'\n",
1925 host
.config
, safe_strerror(errno
));
1929 while (fgets(buf
, BUFSIZ
, confp
)) {
1932 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1936 vty_out(vty
, "%s\n", buf
);
1944 int cmd_domainname_set(const char *domainname
)
1946 XFREE(MTYPE_HOST
, host
.domainname
);
1947 host
.domainname
= domainname
? XSTRDUP(MTYPE_HOST
, domainname
) : NULL
;
1951 /* Hostname configuration */
1952 DEFUN(config_domainname
,
1955 "Set system's domain name\n"
1956 "This system's domain name\n")
1958 struct cmd_token
*word
= argv
[1];
1960 if (!isalpha((int)word
->arg
[0])) {
1961 vty_out(vty
, "Please specify string starting with alphabet\n");
1962 return CMD_WARNING_CONFIG_FAILED
;
1965 return cmd_domainname_set(word
->arg
);
1968 DEFUN(config_no_domainname
,
1970 "no domainname [DOMAINNAME]",
1972 "Reset system's domain name\n"
1973 "domain name of this router\n")
1975 return cmd_domainname_set(NULL
);
1978 int cmd_hostname_set(const char *hostname
)
1980 XFREE(MTYPE_HOST
, host
.name
);
1981 host
.name
= hostname
? XSTRDUP(MTYPE_HOST
, hostname
) : NULL
;
1985 /* Hostname configuration */
1986 DEFUN (config_hostname
,
1989 "Set system's network name\n"
1990 "This system's network name\n")
1992 struct cmd_token
*word
= argv
[1];
1994 if (!isalnum((int)word
->arg
[0])) {
1995 vty_out(vty
, "Please specify string starting with alphabet\n");
1996 return CMD_WARNING_CONFIG_FAILED
;
1999 return cmd_hostname_set(word
->arg
);
2002 DEFUN (config_no_hostname
,
2004 "no hostname [HOSTNAME]",
2006 "Reset system's network name\n"
2007 "Host name of this router\n")
2009 return cmd_hostname_set(NULL
);
2012 /* VTY interface password set. */
2013 DEFUN (config_password
,
2015 "password [(8-8)] WORD",
2016 "Modify the terminal connection password\n"
2017 "Specifies a HIDDEN password will follow\n"
2018 "The password string\n")
2022 if (argc
== 3) // '8' was specified
2025 XFREE(MTYPE_HOST
, host
.password
);
2026 host
.password
= NULL
;
2027 if (host
.password_encrypt
)
2028 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2029 host
.password_encrypt
=
2030 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2034 if (!isalnum((int)argv
[idx_8
]->arg
[0])) {
2036 "Please specify string starting with alphanumeric\n");
2037 return CMD_WARNING_CONFIG_FAILED
;
2041 XFREE(MTYPE_HOST
, host
.password
);
2042 host
.password
= NULL
;
2045 if (host
.password_encrypt
)
2046 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2047 host
.password_encrypt
=
2048 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2050 host
.password
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2055 /* VTY interface password delete. */
2056 DEFUN (no_config_password
,
2060 "Modify the terminal connection password\n")
2062 bool warned
= false;
2064 if (host
.password
) {
2065 if (!vty_shell_serv(vty
)) {
2066 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2069 XFREE(MTYPE_HOST
, host
.password
);
2071 host
.password
= NULL
;
2073 if (host
.password_encrypt
) {
2074 if (!warned
&& !vty_shell_serv(vty
))
2075 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2076 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2078 host
.password_encrypt
= NULL
;
2083 /* VTY enable password set. */
2084 DEFUN (config_enable_password
,
2085 enable_password_cmd
,
2086 "enable password [(8-8)] WORD",
2087 "Modify enable password parameters\n"
2088 "Assign the privileged level password\n"
2089 "Specifies a HIDDEN password will follow\n"
2090 "The HIDDEN 'enable' password string\n")
2095 /* Crypt type is specified. */
2097 if (argv
[idx_8
]->arg
[0] == '8') {
2099 XFREE(MTYPE_HOST
, host
.enable
);
2102 if (host
.enable_encrypt
)
2103 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2104 host
.enable_encrypt
=
2105 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2109 vty_out(vty
, "Unknown encryption type.\n");
2110 return CMD_WARNING_CONFIG_FAILED
;
2114 if (!isalnum((int)argv
[idx_8
]->arg
[0])) {
2116 "Please specify string starting with alphanumeric\n");
2117 return CMD_WARNING_CONFIG_FAILED
;
2121 XFREE(MTYPE_HOST
, host
.enable
);
2124 /* Plain password input. */
2126 if (host
.enable_encrypt
)
2127 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2128 host
.enable_encrypt
=
2129 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2131 host
.enable
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2136 /* VTY enable password delete. */
2137 DEFUN (no_config_enable_password
,
2138 no_enable_password_cmd
,
2139 "no enable password",
2141 "Modify enable password parameters\n"
2142 "Assign the privileged level password\n")
2144 bool warned
= false;
2147 if (!vty_shell_serv(vty
)) {
2148 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2151 XFREE(MTYPE_HOST
, host
.enable
);
2155 if (host
.enable_encrypt
) {
2156 if (!warned
&& !vty_shell_serv(vty
))
2157 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2158 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2160 host
.enable_encrypt
= NULL
;
2165 DEFUN (service_password_encrypt
,
2166 service_password_encrypt_cmd
,
2167 "service password-encryption",
2168 "Set up miscellaneous service\n"
2169 "Enable encrypted passwords\n")
2176 if (host
.password
) {
2177 if (host
.password_encrypt
)
2178 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2179 host
.password_encrypt
=
2180 XSTRDUP(MTYPE_HOST
, zencrypt(host
.password
));
2183 if (host
.enable_encrypt
)
2184 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2185 host
.enable_encrypt
=
2186 XSTRDUP(MTYPE_HOST
, zencrypt(host
.enable
));
2192 DEFUN (no_service_password_encrypt
,
2193 no_service_password_encrypt_cmd
,
2194 "no service password-encryption",
2196 "Set up miscellaneous service\n"
2197 "Enable encrypted passwords\n")
2204 if (host
.password_encrypt
)
2205 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2206 host
.password_encrypt
= NULL
;
2208 if (host
.enable_encrypt
)
2209 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2210 host
.enable_encrypt
= NULL
;
2215 DEFUN (config_terminal_length
,
2216 config_terminal_length_cmd
,
2217 "terminal length (0-512)",
2218 "Set terminal line parameters\n"
2219 "Set number of lines on a screen\n"
2220 "Number of lines on screen (0 for no pausing)\n")
2224 vty
->lines
= atoi(argv
[idx_number
]->arg
);
2229 DEFUN (config_terminal_no_length
,
2230 config_terminal_no_length_cmd
,
2231 "terminal no length",
2232 "Set terminal line parameters\n"
2234 "Set number of lines on a screen\n")
2240 DEFUN (service_terminal_length
,
2241 service_terminal_length_cmd
,
2242 "service terminal-length (0-512)",
2243 "Set up miscellaneous service\n"
2244 "System wide terminal length configuration\n"
2245 "Number of lines of VTY (0 means no line control)\n")
2249 host
.lines
= atoi(argv
[idx_number
]->arg
);
2254 DEFUN (no_service_terminal_length
,
2255 no_service_terminal_length_cmd
,
2256 "no service terminal-length [(0-512)]",
2258 "Set up miscellaneous service\n"
2259 "System wide terminal length configuration\n"
2260 "Number of lines of VTY (0 means no line control)\n")
2266 DEFUN_HIDDEN (do_echo
,
2269 "Echo a message back to the vty\n"
2270 "The message to echo\n")
2274 vty_out(vty
, "%s\n",
2275 ((message
= argv_concat(argv
, argc
, 1)) ? message
: ""));
2277 XFREE(MTYPE_TMP
, message
);
2281 DEFUN (config_logmsg
,
2283 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
2284 "Send a message to enabled logging destinations\n"
2286 "The message to send\n")
2288 int idx_log_level
= 1;
2289 int idx_message
= 2;
2293 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2294 return CMD_ERR_NO_MATCH
;
2297 ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
2299 XFREE(MTYPE_TMP
, message
);
2304 DEFUN (show_logging
,
2308 "Show current logging configuration\n")
2310 struct zlog
*zl
= zlog_default
;
2312 vty_out(vty
, "Syslog logging: ");
2313 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
2314 vty_out(vty
, "disabled");
2316 vty_out(vty
, "level %s, facility %s, ident %s",
2317 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
2318 facility_name(zl
->facility
), zl
->ident
);
2321 vty_out(vty
, "Stdout logging: ");
2322 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
2323 vty_out(vty
, "disabled");
2325 vty_out(vty
, "level %s",
2326 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
2329 vty_out(vty
, "Monitor logging: ");
2330 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
2331 vty_out(vty
, "disabled");
2333 vty_out(vty
, "level %s",
2334 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
2337 vty_out(vty
, "File logging: ");
2338 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) || !zl
->fp
)
2339 vty_out(vty
, "disabled");
2341 vty_out(vty
, "level %s, filename %s",
2342 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
2346 vty_out(vty
, "Protocol name: %s\n", zl
->protoname
);
2347 vty_out(vty
, "Record priority: %s\n",
2348 (zl
->record_priority
? "enabled" : "disabled"));
2349 vty_out(vty
, "Timestamp precision: %d\n", zl
->timestamp_precision
);
2354 DEFUN (config_log_stdout
,
2355 config_log_stdout_cmd
,
2356 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2358 "Set stdout logging level\n"
2361 int idx_log_level
= 2;
2363 if (argc
== idx_log_level
) {
2364 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2369 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2370 return CMD_ERR_NO_MATCH
;
2371 zlog_set_level(ZLOG_DEST_STDOUT
, level
);
2375 DEFUN (no_config_log_stdout
,
2376 no_config_log_stdout_cmd
,
2377 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2380 "Cancel logging to stdout\n"
2383 zlog_set_level(ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
2387 DEFUN (config_log_monitor
,
2388 config_log_monitor_cmd
,
2389 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2391 "Set terminal line (monitor) logging level\n"
2394 int idx_log_level
= 2;
2396 if (argc
== idx_log_level
) {
2397 zlog_set_level(ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
2402 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2403 return CMD_ERR_NO_MATCH
;
2404 zlog_set_level(ZLOG_DEST_MONITOR
, level
);
2408 DEFUN (no_config_log_monitor
,
2409 no_config_log_monitor_cmd
,
2410 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2413 "Disable terminal line (monitor) logging\n"
2416 zlog_set_level(ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
2420 static int set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
2424 const char *fullpath
;
2426 /* Path detection. */
2427 if (!IS_DIRECTORY_SEP(*fname
)) {
2428 char cwd
[MAXPATHLEN
+ 1];
2429 cwd
[MAXPATHLEN
] = '\0';
2431 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2432 flog_err_sys(EC_LIB_SYSTEM_CALL
,
2433 "config_log_file: Unable to alloc mem!");
2434 return CMD_WARNING_CONFIG_FAILED
;
2437 p
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + strlen(fname
) + 2);
2438 sprintf(p
, "%s/%s", cwd
, fname
);
2443 ret
= zlog_set_file(fullpath
, loglevel
);
2446 XFREE(MTYPE_TMP
, p
);
2450 vty_out(vty
, "can't open logfile %s\n", fname
);
2451 return CMD_WARNING_CONFIG_FAILED
;
2455 XFREE(MTYPE_HOST
, host
.logfile
);
2457 host
.logfile
= XSTRDUP(MTYPE_HOST
, fname
);
2459 #if defined(HAVE_CUMULUS)
2460 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2461 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2466 void command_setup_early_logging(const char *dest
, const char *level
)
2471 int nlevel
= level_match(level
);
2473 if (nlevel
!= ZLOG_DISABLED
)
2474 zlog_default
->default_lvl
= nlevel
;
2480 if (strcmp(dest
, "stdout") == 0) {
2481 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2485 if (strcmp(dest
, "syslog") == 0) {
2486 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2490 token
= strstr(dest
, ":");
2496 set_log_file(NULL
, token
, zlog_default
->default_lvl
);
2499 DEFUN (config_log_file
,
2500 config_log_file_cmd
,
2501 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2504 "Logging filename\n"
2507 int idx_filename
= 2;
2508 int idx_log_levels
= 3;
2511 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2513 return CMD_ERR_NO_MATCH
;
2514 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2516 return set_log_file(vty
, argv
[idx_filename
]->arg
,
2517 zlog_default
->default_lvl
);
2520 static void disable_log_file(void)
2525 XFREE(MTYPE_HOST
, host
.logfile
);
2527 host
.logfile
= NULL
;
2530 DEFUN (no_config_log_file
,
2531 no_config_log_file_cmd
,
2532 "no log file [FILENAME [LEVEL]]",
2535 "Cancel logging to file\n"
2536 "Logging file name\n"
2543 DEFUN (config_log_syslog
,
2544 config_log_syslog_cmd
,
2545 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2547 "Set syslog logging level\n"
2550 int idx_log_levels
= 2;
2556 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2558 return CMD_ERR_NO_MATCH
;
2559 zlog_set_level(ZLOG_DEST_SYSLOG
, level
);
2562 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2567 DEFUN (no_config_log_syslog
,
2568 no_config_log_syslog_cmd
,
2569 "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>]",
2572 "Cancel logging to syslog\n"
2576 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2580 DEFUN (config_log_facility
,
2581 config_log_facility_cmd
,
2582 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2584 "Facility parameter for syslog messages\n"
2588 int facility
= facility_match(argv
[idx_target
]->arg
);
2590 zlog_default
->facility
= facility
;
2594 DEFUN (no_config_log_facility
,
2595 no_config_log_facility_cmd
,
2596 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2599 "Reset syslog facility to default (daemon)\n"
2602 zlog_default
->facility
= LOG_DAEMON
;
2606 DEFUN (config_log_record_priority
,
2607 config_log_record_priority_cmd
,
2608 "log record-priority",
2610 "Log the priority of the message within the message\n")
2612 zlog_default
->record_priority
= 1;
2616 DEFUN (no_config_log_record_priority
,
2617 no_config_log_record_priority_cmd
,
2618 "no log record-priority",
2621 "Do not log the priority of the message within the message\n")
2623 zlog_default
->record_priority
= 0;
2627 DEFUN (config_log_timestamp_precision
,
2628 config_log_timestamp_precision_cmd
,
2629 "log timestamp precision (0-6)",
2631 "Timestamp configuration\n"
2632 "Set the timestamp precision\n"
2633 "Number of subsecond digits\n")
2636 zlog_default
->timestamp_precision
=
2637 strtoul(argv
[idx_number
]->arg
, NULL
, 10);
2641 DEFUN (no_config_log_timestamp_precision
,
2642 no_config_log_timestamp_precision_cmd
,
2643 "no log timestamp precision",
2646 "Timestamp configuration\n"
2647 "Reset the timestamp precision to the default value of 0\n")
2649 zlog_default
->timestamp_precision
= 0;
2653 DEFUN (debug_memstats
,
2655 "[no] debug memstats-at-exit",
2658 "Print memory type statistics at exit\n")
2660 debug_memstats_at_exit
= !!strcmp(argv
[0]->text
, "no");
2664 int cmd_banner_motd_file(const char *file
)
2666 int success
= CMD_SUCCESS
;
2671 rpath
= realpath(file
, p
);
2673 return CMD_ERR_NO_FILE
;
2674 in
= strstr(rpath
, SYSCONFDIR
);
2677 XFREE(MTYPE_HOST
, host
.motdfile
);
2678 host
.motdfile
= XSTRDUP(MTYPE_HOST
, file
);
2680 success
= CMD_WARNING_CONFIG_FAILED
;
2685 DEFUN (banner_motd_file
,
2686 banner_motd_file_cmd
,
2687 "banner motd file FILE",
2690 "Banner from a file\n"
2694 const char *filename
= argv
[idx_file
]->arg
;
2695 int cmd
= cmd_banner_motd_file(filename
);
2697 if (cmd
== CMD_ERR_NO_FILE
)
2698 vty_out(vty
, "%s does not exist", filename
);
2699 else if (cmd
== CMD_WARNING_CONFIG_FAILED
)
2700 vty_out(vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2705 DEFUN (banner_motd_default
,
2706 banner_motd_default_cmd
,
2707 "banner motd default",
2708 "Set banner string\n"
2709 "Strings for motd\n"
2712 host
.motd
= default_motd
;
2716 DEFUN (no_banner_motd
,
2720 "Set banner string\n"
2721 "Strings for motd\n")
2725 XFREE(MTYPE_HOST
, host
.motdfile
);
2726 host
.motdfile
= NULL
;
2733 "Find CLI command containing text\n"
2734 "Text to search for\n")
2736 char *text
= argv_concat(argv
, argc
, 1);
2737 const struct cmd_node
*node
;
2738 const struct cmd_element
*cli
;
2741 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++) {
2742 node
= vector_slot(cmdvec
, i
);
2745 clis
= node
->cmd_vector
;
2746 for (unsigned int j
= 0; j
< vector_active(clis
); j
++) {
2747 cli
= vector_slot(clis
, j
);
2748 if (strcasestr(cli
->string
, text
))
2749 vty_out(vty
, " (%s) %s\n",
2750 node_names
[node
->node
], cli
->string
);
2754 XFREE(MTYPE_TMP
, text
);
2759 /* Set config filename. Called from vty.c */
2760 void host_config_set(const char *filename
)
2763 XFREE(MTYPE_HOST
, host
.config
);
2764 host
.config
= XSTRDUP(MTYPE_HOST
, filename
);
2767 const char *host_config_get(void)
2772 void install_default(enum node_type node
)
2774 install_element(node
, &config_exit_cmd
);
2775 install_element(node
, &config_quit_cmd
);
2776 install_element(node
, &config_end_cmd
);
2777 install_element(node
, &config_help_cmd
);
2778 install_element(node
, &config_list_cmd
);
2779 install_element(node
, &show_cli_graph_cmd
);
2780 install_element(node
, &find_cmd
);
2782 install_element(node
, &config_write_cmd
);
2783 install_element(node
, &show_running_config_cmd
);
2785 install_element(node
, &autocomplete_cmd
);
2788 /* Initialize command interface. Install basic nodes and commands.
2790 * terminal = 0 -- vtysh / no logging, no config control
2791 * terminal = 1 -- normal daemon
2792 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2793 void cmd_init(int terminal
)
2795 struct utsname names
;
2797 if (array_size(node_names
) != NODE_TYPE_MAX
)
2798 assert(!"Update the CLI node description array!");
2803 /* register command preprocessors */
2804 hook_register(cmd_execute
, handle_pipe_action
);
2805 hook_register(cmd_execute_done
, handle_pipe_action_done
);
2807 varhandlers
= list_new();
2809 /* Allocate initial top vector of commands. */
2810 cmdvec
= vector_init(VECTOR_MIN_SIZE
);
2812 /* Default host value settings. */
2813 host
.name
= XSTRDUP(MTYPE_HOST
, names
.nodename
);
2814 #ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
2815 if ((strcmp(names
.domainname
, "(none)") == 0))
2816 host
.domainname
= NULL
;
2818 host
.domainname
= XSTRDUP(MTYPE_HOST
, names
.domainname
);
2820 host
.domainname
= NULL
;
2822 host
.password
= NULL
;
2824 host
.logfile
= NULL
;
2826 host
.noconfig
= (terminal
< 0);
2828 host
.motd
= default_motd
;
2829 host
.motdfile
= NULL
;
2831 /* Install top nodes. */
2832 install_node(&view_node
, NULL
);
2833 install_node(&enable_node
, NULL
);
2834 install_node(&auth_node
, NULL
);
2835 install_node(&auth_enable_node
, NULL
);
2836 install_node(&config_node
, config_write_host
);
2838 /* Each node's basic commands. */
2839 install_element(VIEW_NODE
, &show_version_cmd
);
2840 install_element(ENABLE_NODE
, &show_startup_config_cmd
);
2841 install_element(ENABLE_NODE
, &debug_memstats_cmd
);
2844 install_element(VIEW_NODE
, &config_list_cmd
);
2845 install_element(VIEW_NODE
, &config_exit_cmd
);
2846 install_element(VIEW_NODE
, &config_quit_cmd
);
2847 install_element(VIEW_NODE
, &config_help_cmd
);
2848 install_element(VIEW_NODE
, &config_enable_cmd
);
2849 install_element(VIEW_NODE
, &config_terminal_length_cmd
);
2850 install_element(VIEW_NODE
, &config_terminal_no_length_cmd
);
2851 install_element(VIEW_NODE
, &show_logging_cmd
);
2852 install_element(VIEW_NODE
, &show_commandtree_cmd
);
2853 install_element(VIEW_NODE
, &echo_cmd
);
2854 install_element(VIEW_NODE
, &autocomplete_cmd
);
2855 install_element(VIEW_NODE
, &find_cmd
);
2857 install_element(ENABLE_NODE
, &config_end_cmd
);
2858 install_element(ENABLE_NODE
, &config_disable_cmd
);
2859 install_element(ENABLE_NODE
, &config_terminal_cmd
);
2860 install_element(ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2861 install_element(ENABLE_NODE
, &config_write_cmd
);
2862 install_element(ENABLE_NODE
, &show_running_config_cmd
);
2863 install_element(ENABLE_NODE
, &config_logmsg_cmd
);
2865 install_default(CONFIG_NODE
);
2868 workqueue_cmd_init();
2872 install_element(CONFIG_NODE
, &hostname_cmd
);
2873 install_element(CONFIG_NODE
, &no_hostname_cmd
);
2874 install_element(CONFIG_NODE
, &domainname_cmd
);
2875 install_element(CONFIG_NODE
, &no_domainname_cmd
);
2876 install_element(CONFIG_NODE
, &frr_version_defaults_cmd
);
2877 install_element(CONFIG_NODE
, &debug_memstats_cmd
);
2880 install_element(CONFIG_NODE
, &password_cmd
);
2881 install_element(CONFIG_NODE
, &no_password_cmd
);
2882 install_element(CONFIG_NODE
, &enable_password_cmd
);
2883 install_element(CONFIG_NODE
, &no_enable_password_cmd
);
2885 install_element(CONFIG_NODE
, &config_log_stdout_cmd
);
2886 install_element(CONFIG_NODE
, &no_config_log_stdout_cmd
);
2887 install_element(CONFIG_NODE
, &config_log_monitor_cmd
);
2888 install_element(CONFIG_NODE
, &no_config_log_monitor_cmd
);
2889 install_element(CONFIG_NODE
, &config_log_file_cmd
);
2890 install_element(CONFIG_NODE
, &no_config_log_file_cmd
);
2891 install_element(CONFIG_NODE
, &config_log_syslog_cmd
);
2892 install_element(CONFIG_NODE
, &no_config_log_syslog_cmd
);
2893 install_element(CONFIG_NODE
, &config_log_facility_cmd
);
2894 install_element(CONFIG_NODE
, &no_config_log_facility_cmd
);
2895 install_element(CONFIG_NODE
, &config_log_record_priority_cmd
);
2896 install_element(CONFIG_NODE
,
2897 &no_config_log_record_priority_cmd
);
2898 install_element(CONFIG_NODE
,
2899 &config_log_timestamp_precision_cmd
);
2900 install_element(CONFIG_NODE
,
2901 &no_config_log_timestamp_precision_cmd
);
2902 install_element(CONFIG_NODE
, &service_password_encrypt_cmd
);
2903 install_element(CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2904 install_element(CONFIG_NODE
, &banner_motd_default_cmd
);
2905 install_element(CONFIG_NODE
, &banner_motd_file_cmd
);
2906 install_element(CONFIG_NODE
, &no_banner_motd_cmd
);
2907 install_element(CONFIG_NODE
, &service_terminal_length_cmd
);
2908 install_element(CONFIG_NODE
, &no_service_terminal_length_cmd
);
2910 vrf_install_commands();
2914 grammar_sandbox_init();
2918 void cmd_terminate()
2920 struct cmd_node
*cmd_node
;
2922 hook_unregister(cmd_execute
, handle_pipe_action
);
2923 hook_unregister(cmd_execute_done
, handle_pipe_action_done
);
2926 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++)
2927 if ((cmd_node
= vector_slot(cmdvec
, i
)) != NULL
) {
2928 // deleting the graph delets the cmd_element as
2930 graph_delete_graph(cmd_node
->cmdgraph
);
2931 vector_free(cmd_node
->cmd_vector
);
2932 hash_clean(cmd_node
->cmd_hash
, NULL
);
2933 hash_free(cmd_node
->cmd_hash
);
2934 cmd_node
->cmd_hash
= NULL
;
2937 vector_free(cmdvec
);
2942 XFREE(MTYPE_HOST
, host
.name
);
2943 if (host
.domainname
)
2944 XFREE(MTYPE_HOST
, host
.domainname
);
2946 XFREE(MTYPE_HOST
, host
.password
);
2947 if (host
.password_encrypt
)
2948 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2950 XFREE(MTYPE_HOST
, host
.enable
);
2951 if (host
.enable_encrypt
)
2952 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2954 XFREE(MTYPE_HOST
, host
.logfile
);
2956 XFREE(MTYPE_HOST
, host
.motdfile
);
2958 XFREE(MTYPE_HOST
, host
.config
);
2960 list_delete(&varhandlers
);