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 */
150 /* clang-format on */
152 /* Command vector which includes some level of command lists. Normally
153 each daemon maintains each own cmdvec. */
154 vector cmdvec
= NULL
;
156 /* Host information structure. */
160 * Returns host.name if any, otherwise
161 * it returns the system hostname.
163 const char *cmd_hostname_get(void)
169 * Returns unix domainname
171 const char *cmd_domainname_get(void)
173 return host
.domainname
;
176 /* Standard command node structures. */
177 static struct cmd_node auth_node
= {
178 AUTH_NODE
, "Password: ",
181 static struct cmd_node view_node
= {
185 static struct cmd_node auth_enable_node
= {
186 AUTH_ENABLE_NODE
, "Password: ",
189 static struct cmd_node enable_node
= {
193 static struct cmd_node config_node
= {CONFIG_NODE
, "%s(config)# ", 1};
195 /* Default motd string. */
196 static const char *default_motd
= FRR_DEFAULT_MOTD
;
198 static const struct facility_map
{
202 } syslog_facilities
[] = {
203 {LOG_KERN
, "kern", 1},
204 {LOG_USER
, "user", 2},
205 {LOG_MAIL
, "mail", 1},
206 {LOG_DAEMON
, "daemon", 1},
207 {LOG_AUTH
, "auth", 1},
208 {LOG_SYSLOG
, "syslog", 1},
210 {LOG_NEWS
, "news", 1},
211 {LOG_UUCP
, "uucp", 2},
212 {LOG_CRON
, "cron", 1},
216 {LOG_LOCAL0
, "local0", 6},
217 {LOG_LOCAL1
, "local1", 6},
218 {LOG_LOCAL2
, "local2", 6},
219 {LOG_LOCAL3
, "local3", 6},
220 {LOG_LOCAL4
, "local4", 6},
221 {LOG_LOCAL5
, "local5", 6},
222 {LOG_LOCAL6
, "local6", 6},
223 {LOG_LOCAL7
, "local7", 6},
227 static const char *facility_name(int facility
)
229 const struct facility_map
*fm
;
231 for (fm
= syslog_facilities
; fm
->name
; fm
++)
232 if (fm
->facility
== facility
)
237 static int facility_match(const char *str
)
239 const struct facility_map
*fm
;
241 for (fm
= syslog_facilities
; fm
->name
; fm
++)
242 if (!strncmp(str
, fm
->name
, fm
->match
))
247 static int level_match(const char *s
)
251 for (level
= 0; zlog_priority
[level
] != NULL
; level
++)
252 if (!strncmp(s
, zlog_priority
[level
], 2))
254 return ZLOG_DISABLED
;
257 /* This is called from main when a daemon is invoked with -v or --version. */
258 void print_version(const char *progname
)
260 printf("%s version %s\n", progname
, FRR_VERSION
);
261 printf("%s\n", FRR_COPYRIGHT
);
262 printf("configured with:\n\t%s\n", FRR_CONFIG_ARGS
);
265 char *argv_concat(struct cmd_token
**argv
, int argc
, int shift
)
267 int cnt
= MAX(argc
- shift
, 0);
268 const char *argstr
[cnt
+ 1];
273 for (int i
= 0; i
< cnt
; i
++)
274 argstr
[i
] = argv
[i
+ shift
]->arg
;
276 return frrstr_join(argstr
, cnt
, " ");
279 vector
cmd_make_strvec(const char *string
)
284 const char *copy
= string
;
286 /* skip leading whitespace */
287 while (isspace((int)*copy
) && *copy
!= '\0')
290 /* if the entire string was whitespace or a comment, return */
291 if (*copy
== '\0' || *copy
== '!' || *copy
== '#')
294 vector result
= frrstr_split_vec(copy
, "\n\r\t ");
296 for (unsigned int i
= 0; i
< vector_active(result
); i
++) {
297 if (strlen(vector_slot(result
, i
)) == 0) {
298 XFREE(MTYPE_TMP
, vector_slot(result
, i
));
299 vector_unset(result
, i
);
303 vector_compact(result
);
308 void cmd_free_strvec(vector v
)
310 frrstr_strvec_free(v
);
314 * Convenience function for accessing argv data.
318 * @param text definition snippet of the desired token
319 * @param index the starting index, and where to store the
320 * index of the found token if it exists
321 * @return 1 if found, 0 otherwise
323 int argv_find(struct cmd_token
**argv
, int argc
, const char *text
, int *index
)
326 for (int i
= *index
; i
< argc
&& found
== 0; i
++)
327 if ((found
= strmatch(text
, argv
[i
]->text
)))
332 static unsigned int cmd_hash_key(void *p
)
334 int size
= sizeof(p
);
336 return jhash(p
, size
, 0);
339 static int cmd_hash_cmp(const void *a
, const void *b
)
344 /* Install top node of command vector. */
345 void install_node(struct cmd_node
*node
, int (*func
)(struct vty
*))
347 vector_set_index(cmdvec
, node
->node
, node
);
349 node
->cmdgraph
= graph_new();
350 node
->cmd_vector
= vector_init(VECTOR_MIN_SIZE
);
352 struct cmd_token
*token
=
353 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
354 graph_new_node(node
->cmdgraph
, token
,
355 (void (*)(void *)) & cmd_token_del
);
356 node
->cmd_hash
= hash_create_size(16, cmd_hash_key
, cmd_hash_cmp
,
360 /* Return prompt character of specified node. */
361 const char *cmd_prompt(enum node_type node
)
363 struct cmd_node
*cnode
;
365 cnode
= vector_slot(cmdvec
, node
);
366 return cnode
->prompt
;
369 /* Install a command into a node. */
370 void install_element(enum node_type ntype
, struct cmd_element
*cmd
)
372 struct cmd_node
*cnode
;
374 /* cmd_init hasn't been called */
376 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
381 cnode
= vector_lookup(cmdvec
, ntype
);
386 "\tnode %d (%s) does not exist.\n"
387 "\tplease call install_node() before install_element()\n",
388 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
392 if (hash_lookup(cnode
->cmd_hash
, cmd
) != NULL
) {
395 "\tnode %d (%s) already has this command installed.\n"
396 "\tduplicate install_element call?\n",
397 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
401 assert(hash_get(cnode
->cmd_hash
, cmd
, hash_alloc_intern
));
403 struct graph
*graph
= graph_new();
404 struct cmd_token
*token
=
405 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
406 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
408 cmd_graph_parse(graph
, cmd
);
409 cmd_graph_names(graph
);
410 cmd_graph_merge(cnode
->cmdgraph
, graph
, +1);
411 graph_delete_graph(graph
);
413 vector_set(cnode
->cmd_vector
, cmd
);
415 if (ntype
== VIEW_NODE
)
416 install_element(ENABLE_NODE
, cmd
);
419 void uninstall_element(enum node_type ntype
, struct cmd_element
*cmd
)
421 struct cmd_node
*cnode
;
423 /* cmd_init hasn't been called */
425 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
430 cnode
= vector_lookup(cmdvec
, ntype
);
435 "\tnode %d (%s) does not exist.\n"
436 "\tplease call install_node() before uninstall_element()\n",
437 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
441 if (hash_release(cnode
->cmd_hash
, cmd
) == NULL
) {
444 "\tnode %d (%s) does not have this command installed.\n"
445 "\tduplicate uninstall_element call?\n",
446 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
450 vector_unset_value(cnode
->cmd_vector
, cmd
);
452 struct graph
*graph
= graph_new();
453 struct cmd_token
*token
=
454 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
455 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
457 cmd_graph_parse(graph
, cmd
);
458 cmd_graph_names(graph
);
459 cmd_graph_merge(cnode
->cmdgraph
, graph
, -1);
460 graph_delete_graph(graph
);
462 if (ntype
== VIEW_NODE
)
463 uninstall_element(ENABLE_NODE
, cmd
);
467 static const unsigned char itoa64
[] =
468 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
470 static void to64(char *s
, long v
, int n
)
473 *s
++ = itoa64
[v
& 0x3f];
478 static char *zencrypt(const char *passwd
)
482 char *crypt(const char *, const char *);
484 gettimeofday(&tv
, 0);
486 to64(&salt
[0], random(), 3);
487 to64(&salt
[3], tv
.tv_usec
, 3);
490 return crypt(passwd
, salt
);
493 /* This function write configuration of this host. */
494 static int config_write_host(struct vty
*vty
)
496 if (cmd_hostname_get())
497 vty_out(vty
, "hostname %s\n", cmd_hostname_get());
499 if (cmd_domainname_get())
500 vty_out(vty
, "domainname %s\n", cmd_domainname_get());
502 /* The following are all configuration commands that are not sent to
503 * watchfrr. For instance watchfrr is hardcoded to log to syslog so
504 * we would always display 'log syslog informational' in the config
505 * which would cause other daemons to then switch to syslog when they
508 if (strcmp(zlog_default
->protoname
, "WATCHFRR")) {
510 if (host
.password_encrypt
)
511 vty_out(vty
, "password 8 %s\n",
512 host
.password_encrypt
);
513 if (host
.enable_encrypt
)
514 vty_out(vty
, "enable password 8 %s\n",
515 host
.enable_encrypt
);
518 vty_out(vty
, "password %s\n", host
.password
);
520 vty_out(vty
, "enable password %s\n",
525 && (zlog_default
->maxlvl
[ZLOG_DEST_FILE
]
527 vty_out(vty
, "log file %s", host
.logfile
);
528 if (zlog_default
->maxlvl
[ZLOG_DEST_FILE
]
529 != zlog_default
->default_lvl
)
532 [zlog_default
->maxlvl
537 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != ZLOG_DISABLED
) {
538 vty_out(vty
, "log stdout");
539 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
]
540 != zlog_default
->default_lvl
)
543 [zlog_default
->maxlvl
544 [ZLOG_DEST_STDOUT
]]);
548 if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
549 vty_out(vty
, "no log monitor\n");
550 else if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]
551 != zlog_default
->default_lvl
)
552 vty_out(vty
, "log monitor %s\n",
553 zlog_priority
[zlog_default
->maxlvl
554 [ZLOG_DEST_MONITOR
]]);
556 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
) {
557 vty_out(vty
, "log syslog");
558 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
]
559 != zlog_default
->default_lvl
)
561 zlog_priority
[zlog_default
->maxlvl
562 [ZLOG_DEST_SYSLOG
]]);
566 if (zlog_default
->facility
!= LOG_DAEMON
)
567 vty_out(vty
, "log facility %s\n",
568 facility_name(zlog_default
->facility
));
570 if (zlog_default
->record_priority
== 1)
571 vty_out(vty
, "log record-priority\n");
573 if (zlog_default
->timestamp_precision
> 0)
574 vty_out(vty
, "log timestamp precision %d\n",
575 zlog_default
->timestamp_precision
);
578 vty_out(vty
, "service advanced-vty\n");
581 vty_out(vty
, "service password-encryption\n");
584 vty_out(vty
, "service terminal-length %d\n",
588 vty_out(vty
, "banner motd file %s\n", host
.motdfile
);
590 vty_out(vty
, "no banner motd\n");
593 if (debug_memstats_at_exit
)
594 vty_out(vty
, "!\ndebug memstats-at-exit\n");
599 /* Utility function for getting command graph. */
600 static struct graph
*cmd_node_graph(vector v
, enum node_type ntype
)
602 struct cmd_node
*cnode
= vector_slot(v
, ntype
);
603 return cnode
->cmdgraph
;
606 static int cmd_try_do_shortcut(enum node_type node
, char *first_word
)
608 if (first_word
!= NULL
&& node
!= AUTH_NODE
&& node
!= VIEW_NODE
609 && node
!= AUTH_ENABLE_NODE
&& 0 == strcmp("do", first_word
))
615 * Compare function for cmd_token.
616 * Used with qsort to sort command completions.
618 static int compare_completions(const void *fst
, const void *snd
)
620 struct cmd_token
*first
= *(struct cmd_token
**)fst
,
621 *secnd
= *(struct cmd_token
**)snd
;
622 return strcmp(first
->text
, secnd
->text
);
626 * Takes a list of completions returned by command_complete,
627 * dedeuplicates them based on both text and description,
628 * sorts them, and returns them as a vector.
630 * @param completions linked list of cmd_token
631 * @return deduplicated and sorted vector with
633 vector
completions_to_vec(struct list
*completions
)
635 vector comps
= vector_init(VECTOR_MIN_SIZE
);
638 struct cmd_token
*token
, *cr
= NULL
;
639 unsigned int i
, exists
;
640 for (ALL_LIST_ELEMENTS_RO(completions
, ln
, token
)) {
641 if (token
->type
== END_TKN
&& (cr
= token
))
644 // linear search for token in completions vector
646 for (i
= 0; i
< vector_active(comps
) && !exists
; i
++) {
647 struct cmd_token
*curr
= vector_slot(comps
, i
);
649 exists
= !strcmp(curr
->text
, token
->text
)
650 && !strcmp(curr
->desc
, token
->desc
);
652 exists
= !strcmp(curr
->text
, token
->text
);
653 #endif /* VTYSH_DEBUG */
657 vector_set(comps
, token
);
661 qsort(comps
->index
, vector_active(comps
), sizeof(void *),
662 &compare_completions
);
664 // make <cr> the first element, if it is present
666 vector_set_index(comps
, vector_active(comps
), NULL
);
667 memmove(comps
->index
+ 1, comps
->index
,
668 (comps
->alloced
- 1) * sizeof(void *));
669 vector_set_index(comps
, 0, cr
);
675 * Generates a vector of cmd_token representing possible completions
676 * on the current input.
678 * @param vline the vectorized input line
679 * @param vty the vty with the node to match on
680 * @param status pointer to matcher status code
681 * @return vector of struct cmd_token * with possible completions
683 static vector
cmd_complete_command_real(vector vline
, struct vty
*vty
,
686 struct list
*completions
;
687 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
689 enum matcher_rv rv
= command_complete(cmdgraph
, vline
, &completions
);
691 if (MATCHER_ERROR(rv
)) {
692 *status
= CMD_ERR_NO_MATCH
;
696 vector comps
= completions_to_vec(completions
);
697 list_delete_and_null(&completions
);
699 // set status code appropriately
700 switch (vector_active(comps
)) {
702 *status
= CMD_ERR_NO_MATCH
;
705 *status
= CMD_COMPLETE_FULL_MATCH
;
708 *status
= CMD_COMPLETE_LIST_MATCH
;
714 vector
cmd_describe_command(vector vline
, struct vty
*vty
, int *status
)
718 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
719 enum node_type onode
;
720 vector shifted_vline
;
724 vty
->node
= ENABLE_NODE
;
725 /* We can try it on enable node, cos' the vty is authenticated
728 shifted_vline
= vector_init(vector_count(vline
));
730 for (index
= 1; index
< vector_active(vline
); index
++) {
731 vector_set_index(shifted_vline
, index
- 1,
732 vector_lookup(vline
, index
));
735 ret
= cmd_complete_command_real(shifted_vline
, vty
, status
);
737 vector_free(shifted_vline
);
742 return cmd_complete_command_real(vline
, vty
, status
);
745 static struct list
*varhandlers
= NULL
;
747 void cmd_variable_complete(struct cmd_token
*token
, const char *arg
,
751 const struct cmd_variable_handler
*cvh
;
755 tmpcomps
= arg
? vector_init(VECTOR_MIN_SIZE
) : comps
;
757 for (ALL_LIST_ELEMENTS_RO(varhandlers
, ln
, cvh
)) {
758 if (cvh
->tokenname
&& strcmp(cvh
->tokenname
, token
->text
))
760 if (cvh
->varname
&& (!token
->varname
761 || strcmp(cvh
->varname
, token
->varname
)))
763 cvh
->completions(tmpcomps
, token
);
771 for (i
= vector_active(tmpcomps
); i
; i
--) {
772 char *item
= vector_slot(tmpcomps
, i
- 1);
773 if (strlen(item
) >= argsz
&& !strncmp(item
, arg
, argsz
))
774 vector_set(comps
, item
);
776 XFREE(MTYPE_COMPLETION
, item
);
778 vector_free(tmpcomps
);
781 #define AUTOCOMP_INDENT 5
783 char *cmd_variable_comp2str(vector comps
, unsigned short cols
)
786 char *buf
= XCALLOC(MTYPE_TMP
, bsz
);
787 int lc
= AUTOCOMP_INDENT
;
788 size_t cs
= AUTOCOMP_INDENT
;
790 snprintf(buf
, bsz
, "%*s", AUTOCOMP_INDENT
, "");
791 for (size_t j
= 0; j
< vector_active(comps
); j
++) {
792 char *item
= vector_slot(comps
, j
);
793 itemlen
= strlen(item
);
795 if (cs
+ itemlen
+ AUTOCOMP_INDENT
+ 3 >= bsz
)
796 buf
= XREALLOC(MTYPE_TMP
, buf
, (bsz
*= 2));
798 if (lc
+ itemlen
+ 1 >= cols
) {
799 cs
+= snprintf(&buf
[cs
], bsz
- cs
, "\n%*s",
800 AUTOCOMP_INDENT
, "");
801 lc
= AUTOCOMP_INDENT
;
804 size_t written
= snprintf(&buf
[cs
], bsz
- cs
, "%s ", item
);
807 XFREE(MTYPE_COMPLETION
, item
);
808 vector_set_index(comps
, j
, NULL
);
813 void cmd_variable_handler_register(const struct cmd_variable_handler
*cvh
)
818 for (; cvh
->completions
; cvh
++)
819 listnode_add(varhandlers
, (void *)cvh
);
822 DEFUN_HIDDEN (autocomplete
,
824 "autocomplete TYPE TEXT VARNAME",
825 "Autocompletion handler (internal, for vtysh)\n"
828 "cmd_token->varname\n")
830 struct cmd_token tok
;
831 vector comps
= vector_init(32);
834 memset(&tok
, 0, sizeof(tok
));
835 tok
.type
= atoi(argv
[1]->arg
);
836 tok
.text
= argv
[2]->arg
;
837 tok
.varname
= argv
[3]->arg
;
838 if (!strcmp(tok
.varname
, "-"))
841 cmd_variable_complete(&tok
, NULL
, comps
);
843 for (i
= 0; i
< vector_active(comps
); i
++) {
844 char *text
= vector_slot(comps
, i
);
845 vty_out(vty
, "%s\n", text
);
846 XFREE(MTYPE_COMPLETION
, text
);
854 * Generate possible tab-completions for the given input. This function only
855 * returns results that would result in a valid command if used as Readline
856 * completions (as is the case in vtysh). For instance, if the passed vline ends
857 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
859 * @param vline vectorized input line
861 * @param status location to store matcher status code in
862 * @return set of valid strings for use with Readline as tab-completions.
865 char **cmd_complete_command(vector vline
, struct vty
*vty
, int *status
)
868 int original_node
= vty
->node
;
869 vector input_line
= vector_init(vector_count(vline
));
871 // if the first token is 'do' we'll want to execute the command in the
873 int do_shortcut
= cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0));
874 vty
->node
= do_shortcut
? ENABLE_NODE
: original_node
;
876 // construct the input line we'll be matching on
877 unsigned int offset
= (do_shortcut
) ? 1 : 0;
878 for (unsigned index
= 0; index
+ offset
< vector_active(vline
); index
++)
879 vector_set_index(input_line
, index
,
880 vector_lookup(vline
, index
+ offset
));
882 // get token completions -- this is a copying operation
883 vector comps
= NULL
, initial_comps
;
884 initial_comps
= cmd_complete_command_real(input_line
, vty
, status
);
886 if (!MATCHER_ERROR(*status
)) {
887 assert(initial_comps
);
888 // filter out everything that is not suitable for a
890 comps
= vector_init(VECTOR_MIN_SIZE
);
891 for (unsigned int i
= 0; i
< vector_active(initial_comps
);
893 struct cmd_token
*token
= vector_slot(initial_comps
, i
);
894 if (token
->type
== WORD_TKN
)
895 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
,
897 else if (IS_VARYING_TOKEN(token
->type
)) {
898 const char *ref
= vector_lookup(
899 vline
, vector_active(vline
) - 1);
900 cmd_variable_complete(token
, ref
, comps
);
903 vector_free(initial_comps
);
905 // since we filtered results, we need to re-set status code
906 switch (vector_active(comps
)) {
908 *status
= CMD_ERR_NO_MATCH
;
911 *status
= CMD_COMPLETE_FULL_MATCH
;
914 *status
= CMD_COMPLETE_LIST_MATCH
;
917 // copy completions text into an array of char*
918 ret
= XMALLOC(MTYPE_TMP
,
919 (vector_active(comps
) + 1) * sizeof(char *));
921 for (i
= 0; i
< vector_active(comps
); i
++) {
922 ret
[i
] = vector_slot(comps
, i
);
924 // set the last element to NULL, because this array is used in
925 // a Readline completion_generator function which expects NULL
926 // as a sentinel value
930 } else if (initial_comps
)
931 vector_free(initial_comps
);
933 // comps should always be null here
936 // free the adjusted input line
937 vector_free(input_line
);
939 // reset vty->node to its original value
940 vty
->node
= original_node
;
945 /* return parent node */
946 /* MUST eventually converge on CONFIG_NODE */
947 enum node_type
node_parent(enum node_type node
)
951 assert(node
> CONFIG_NODE
);
956 case BGP_FLOWSPECV4_NODE
:
957 case BGP_FLOWSPECV6_NODE
:
958 case BGP_VRF_POLICY_NODE
:
959 case BGP_VNC_DEFAULTS_NODE
:
960 case BGP_VNC_NVE_GROUP_NODE
:
961 case BGP_VNC_L2_GROUP_NODE
:
971 case BGP_EVPN_VNI_NODE
:
974 case KEYCHAIN_KEY_NODE
:
977 case LINK_PARAMS_NODE
:
978 ret
= INTERFACE_NODE
;
984 case LDP_IPV4_IFACE_NODE
:
987 case LDP_IPV6_IFACE_NODE
:
990 case LDP_PSEUDOWIRE_NODE
:
991 ret
= LDP_L2VPN_NODE
;
1004 /* Execute command by argument vline vector. */
1005 static int cmd_execute_command_real(vector vline
, enum filter_type filter
,
1007 const struct cmd_element
**cmd
)
1009 struct list
*argv_list
;
1010 enum matcher_rv status
;
1011 const struct cmd_element
*matched_element
= NULL
;
1013 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
1014 status
= command_match(cmdgraph
, vline
, &argv_list
, &matched_element
);
1017 *cmd
= matched_element
;
1019 // if matcher error, return corresponding CMD_ERR
1020 if (MATCHER_ERROR(status
)) {
1022 list_delete_and_null(&argv_list
);
1024 case MATCHER_INCOMPLETE
:
1025 return CMD_ERR_INCOMPLETE
;
1026 case MATCHER_AMBIGUOUS
:
1027 return CMD_ERR_AMBIGUOUS
;
1029 return CMD_ERR_NO_MATCH
;
1033 // build argv array from argv list
1034 struct cmd_token
**argv
= XMALLOC(
1035 MTYPE_TMP
, argv_list
->count
* sizeof(struct cmd_token
*));
1036 struct listnode
*ln
;
1037 struct cmd_token
*token
;
1039 for (ALL_LIST_ELEMENTS_RO(argv_list
, ln
, token
))
1042 int argc
= argv_list
->count
;
1045 if (matched_element
->daemon
)
1046 ret
= CMD_SUCCESS_DAEMON
;
1048 ret
= matched_element
->func(matched_element
, vty
, argc
, argv
);
1050 // delete list and cmd_token's in it
1051 list_delete_and_null(&argv_list
);
1052 XFREE(MTYPE_TMP
, argv
);
1058 * Execute a given command, handling things like "do ..." and checking
1059 * whether the given command might apply at a parent node if doesn't
1060 * apply for the current node.
1062 * @param vline Command line input, vector of char* where each element is
1064 * @param vty The vty context in which the command should be executed.
1065 * @param cmd Pointer where the struct cmd_element of the matched command
1066 * will be stored, if any. May be set to NULL if this info is
1068 * @param vtysh If set != 0, don't lookup the command at parent nodes.
1069 * @return The status of the command that has been executed or an error code
1070 * as to why no command could be executed.
1072 int cmd_execute_command(vector vline
, struct vty
*vty
,
1073 const struct cmd_element
**cmd
, int vtysh
)
1075 int ret
, saved_ret
= 0;
1076 enum node_type onode
, try_node
;
1078 onode
= try_node
= vty
->node
;
1080 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
1081 vector shifted_vline
;
1084 vty
->node
= ENABLE_NODE
;
1085 /* We can try it on enable node, cos' the vty is authenticated
1088 shifted_vline
= vector_init(vector_count(vline
));
1090 for (index
= 1; index
< vector_active(vline
); index
++)
1091 vector_set_index(shifted_vline
, index
- 1,
1092 vector_lookup(vline
, index
));
1094 ret
= cmd_execute_command_real(shifted_vline
, FILTER_RELAXED
,
1097 vector_free(shifted_vline
);
1103 cmd_execute_command_real(vline
, FILTER_RELAXED
, vty
, cmd
);
1108 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1109 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
) {
1110 /* This assumes all nodes above CONFIG_NODE are childs of
1112 while (vty
->node
> CONFIG_NODE
) {
1113 try_node
= node_parent(try_node
);
1114 vty
->node
= try_node
;
1115 ret
= cmd_execute_command_real(vline
, FILTER_RELAXED
,
1117 if (ret
== CMD_SUCCESS
|| ret
== CMD_WARNING
1118 || ret
== CMD_NOT_MY_INSTANCE
1119 || ret
== CMD_WARNING_CONFIG_FAILED
)
1122 /* no command succeeded, reset the vty to the original node */
1126 /* return command status for original node */
1131 * Execute a given command, matching it strictly against the current node.
1132 * This mode is used when reading config files.
1134 * @param vline Command line input, vector of char* where each element is
1136 * @param vty The vty context in which the command should be executed.
1137 * @param cmd Pointer where the struct cmd_element* of the matched command
1138 * will be stored, if any. May be set to NULL if this info is
1140 * @return The status of the command that has been executed or an error code
1141 * as to why no command could be executed.
1143 int cmd_execute_command_strict(vector vline
, struct vty
*vty
,
1144 const struct cmd_element
**cmd
)
1146 return cmd_execute_command_real(vline
, FILTER_STRICT
, vty
, cmd
);
1150 * Hook for preprocessing command string before executing.
1152 * All subscribers are called with the raw command string that is to be
1153 * executed. If any changes are to be made, a new string should be allocated
1154 * with MTYPE_TMP and *cmd_out updated to point to this new string. The caller
1155 * is then responsible for freeing this string.
1157 * All processing functions must be mutually exclusive in their action, i.e. if
1158 * one subscriber decides to modify the command, all others must not modify it
1159 * when called. Feeding the output of one processing command into a subsequent
1160 * one is not supported.
1162 * This hook is intentionally internal to the command processing system.
1165 * The raw command string.
1168 * The result of any processing.
1170 DECLARE_HOOK(cmd_execute
,
1171 (struct vty
*vty
, const char *cmd_in
, char **cmd_out
),
1172 (vty
, cmd_in
, cmd_out
));
1173 DEFINE_HOOK(cmd_execute
, (struct vty
*vty
, const char *cmd_in
, char **cmd_out
),
1174 (vty
, cmd_in
, cmd_out
));
1176 /* Hook executed after a CLI command. */
1177 DECLARE_KOOH(cmd_execute_done
, (struct vty
*vty
, const char *cmd_exec
),
1179 DEFINE_KOOH(cmd_execute_done
, (struct vty
*vty
, const char *cmd_exec
),
1183 * cmd_execute hook subscriber to handle `|` actions.
1185 static int handle_pipe_action(struct vty
*vty
, const char *cmd_in
,
1189 char *orig
, *working
, *token
, *u
;
1190 char *pipe
= strstr(cmd_in
, "| ");
1195 /* duplicate string for processing purposes, not including pipe */
1196 orig
= working
= XSTRDUP(MTYPE_TMP
, pipe
+ 2);
1198 /* retrieve action */
1199 token
= strsep(&working
, " ");
1201 /* match result to known actions */
1202 if (strmatch(token
, "include")) {
1203 /* the remaining text should be a regexp */
1204 char *regexp
= working
;
1207 vty_out(vty
, "%% Need a regexp to filter with\n");
1211 bool succ
= vty_set_include(vty
, regexp
);
1214 vty_out(vty
, "%% Bad regexp '%s'\n", regexp
);
1217 *cmd_out
= XSTRDUP(MTYPE_TMP
, cmd_in
);
1221 vty_out(vty
, "%% Unknown action '%s'\n", token
);
1226 XFREE(MTYPE_TMP
, orig
);
1230 static int handle_pipe_action_done(struct vty
*vty
, const char *cmd_exec
)
1233 vty_set_include(vty
, NULL
);
1238 int cmd_execute(struct vty
*vty
, const char *cmd
,
1239 const struct cmd_element
**matched
, int vtysh
)
1242 char *cmd_out
= NULL
;
1243 const char *cmd_exec
;
1246 hook_call(cmd_execute
, vty
, cmd
, &cmd_out
);
1247 cmd_exec
= cmd_out
? (const char *)cmd_out
: cmd
;
1249 vline
= cmd_make_strvec(cmd_exec
);
1252 ret
= cmd_execute_command(vline
, vty
, matched
, vtysh
);
1253 cmd_free_strvec(vline
);
1258 hook_call(cmd_execute_done
, vty
, cmd_exec
);
1261 XFREE(MTYPE_TMP
, cmd_out
);
1268 * Parse one line of config, walking up the parse tree attempting to find a
1271 * @param vty The vty context in which the command should be executed.
1272 * @param cmd Pointer where the struct cmd_element* of the match command
1273 * will be stored, if any. May be set to NULL if this info is
1275 * @param use_daemon Boolean to control whether or not we match on
1276 * CMD_SUCCESS_DAEMON
1278 * @return The status of the command that has been executed or an error code
1279 * as to why no command could be executed.
1281 int command_config_read_one_line(struct vty
*vty
,
1282 const struct cmd_element
**cmd
, int use_daemon
)
1288 vline
= cmd_make_strvec(vty
->buf
);
1290 /* In case of comment line */
1294 /* Execute configuration command : this is strict match */
1295 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1297 // Climb the tree and try the command again at each node
1298 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1299 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1300 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1301 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
1302 && vty
->node
!= CONFIG_NODE
) {
1304 saved_node
= vty
->node
;
1306 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1307 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1308 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1309 && vty
->node
> CONFIG_NODE
) {
1310 vty
->node
= node_parent(vty
->node
);
1311 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1314 // If climbing the tree did not work then ignore the command and
1315 // stay at the same node
1316 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1317 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1318 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
) {
1319 vty
->node
= saved_node
;
1323 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1324 memcpy(vty
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
1326 cmd_free_strvec(vline
);
1331 /* Configuration make from file. */
1332 int config_from_file(struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
1334 int ret
, error_ret
= 0;
1337 while (fgets(vty
->buf
, VTY_BUFSIZ
, fp
)) {
1341 ret
= command_config_read_one_line(vty
, NULL
, 0);
1343 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1344 && ret
!= CMD_ERR_NOTHING_TODO
)
1355 /* Configuration from terminal */
1356 DEFUN (config_terminal
,
1357 config_terminal_cmd
,
1358 "configure terminal",
1359 "Configuration from vty interface\n"
1360 "Configuration terminal\n")
1362 if (vty_config_lock(vty
))
1363 vty
->node
= CONFIG_NODE
;
1365 vty_out(vty
, "VTY configuration is locked by other VTY\n");
1366 return CMD_WARNING_CONFIG_FAILED
;
1371 /* Enable command */
1375 "Turn on privileged mode command\n")
1377 /* If enable password is NULL, change to ENABLE_NODE */
1378 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
)
1379 || vty
->type
== VTY_SHELL_SERV
)
1380 vty
->node
= ENABLE_NODE
;
1382 vty
->node
= AUTH_ENABLE_NODE
;
1387 /* Disable command */
1391 "Turn off privileged mode command\n")
1393 if (vty
->node
== ENABLE_NODE
)
1394 vty
->node
= VIEW_NODE
;
1398 /* Down vty node level. */
1402 "Exit current mode and down to previous mode\n")
1408 void cmd_exit(struct vty
*vty
)
1410 switch (vty
->node
) {
1416 vty
->status
= VTY_CLOSE
;
1419 vty
->node
= ENABLE_NODE
;
1420 vty_config_unlock(vty
);
1422 case INTERFACE_NODE
:
1424 case LOGICALROUTER_NODE
:
1436 case LDP_L2VPN_NODE
:
1443 vty
->node
= CONFIG_NODE
;
1446 case BGP_IPV4M_NODE
:
1447 case BGP_IPV4L_NODE
:
1448 case BGP_VPNV4_NODE
:
1449 case BGP_VPNV6_NODE
:
1450 case BGP_FLOWSPECV4_NODE
:
1451 case BGP_FLOWSPECV6_NODE
:
1452 case BGP_VRF_POLICY_NODE
:
1453 case BGP_VNC_DEFAULTS_NODE
:
1454 case BGP_VNC_NVE_GROUP_NODE
:
1455 case BGP_VNC_L2_GROUP_NODE
:
1457 case BGP_IPV6M_NODE
:
1459 case BGP_IPV6L_NODE
:
1460 vty
->node
= BGP_NODE
;
1462 case BGP_EVPN_VNI_NODE
:
1463 vty
->node
= BGP_EVPN_NODE
;
1467 vty
->node
= LDP_NODE
;
1469 case LDP_IPV4_IFACE_NODE
:
1470 vty
->node
= LDP_IPV4_NODE
;
1472 case LDP_IPV6_IFACE_NODE
:
1473 vty
->node
= LDP_IPV6_NODE
;
1475 case LDP_PSEUDOWIRE_NODE
:
1476 vty
->node
= LDP_L2VPN_NODE
;
1478 case KEYCHAIN_KEY_NODE
:
1479 vty
->node
= KEYCHAIN_NODE
;
1481 case LINK_PARAMS_NODE
:
1482 vty
->node
= INTERFACE_NODE
;
1485 vty
->node
= BFD_NODE
;
1496 "Exit current mode and down to previous mode\n")
1498 return config_exit(self
, vty
, argc
, argv
);
1502 /* End of configuration. */
1506 "End current mode and change to enable mode.\n")
1508 switch (vty
->node
) {
1511 /* Nothing to do. */
1514 case INTERFACE_NODE
:
1516 case LOGICALROUTER_NODE
:
1525 case BGP_VRF_POLICY_NODE
:
1526 case BGP_VNC_DEFAULTS_NODE
:
1527 case BGP_VNC_NVE_GROUP_NODE
:
1528 case BGP_VNC_L2_GROUP_NODE
:
1529 case BGP_VPNV4_NODE
:
1530 case BGP_VPNV6_NODE
:
1531 case BGP_FLOWSPECV4_NODE
:
1532 case BGP_FLOWSPECV6_NODE
:
1534 case BGP_IPV4M_NODE
:
1535 case BGP_IPV4L_NODE
:
1537 case BGP_IPV6M_NODE
:
1539 case BGP_EVPN_VNI_NODE
:
1540 case BGP_IPV6L_NODE
:
1548 case LDP_IPV4_IFACE_NODE
:
1549 case LDP_IPV6_IFACE_NODE
:
1550 case LDP_L2VPN_NODE
:
1551 case LDP_PSEUDOWIRE_NODE
:
1554 case KEYCHAIN_KEY_NODE
:
1556 case LINK_PARAMS_NODE
:
1559 vty_config_unlock(vty
);
1560 vty
->node
= ENABLE_NODE
;
1569 DEFUN (show_version
,
1573 "Displays zebra version\n")
1575 vty_out(vty
, "%s %s (%s).\n", FRR_FULL_NAME
, FRR_VERSION
,
1576 cmd_hostname_get() ? cmd_hostname_get() : "");
1577 vty_out(vty
, "%s%s\n", FRR_COPYRIGHT
, GIT_INFO
);
1578 vty_out(vty
, "configured with:\n %s\n", FRR_CONFIG_ARGS
);
1583 /* "Set" version ... ignore version tags */
1584 DEFUN (frr_version_defaults
,
1585 frr_version_defaults_cmd
,
1586 "frr <version|defaults> LINE...",
1587 "FRRouting global parameters\n"
1588 "version configuration was written by\n"
1589 "set of configuration defaults used\n"
1595 /* Help display function for all node. */
1599 "Description of the interactive help system\n")
1602 "Quagga VTY provides advanced help feature. When you need help,\n\
1603 anytime at the command line please press '?'.\n\
1605 If nothing matches, the help list will be empty and you must backup\n\
1606 until entering a '?' shows the available options.\n\
1607 Two styles of help are provided:\n\
1608 1. Full help is available when you are ready to enter a\n\
1609 command argument (e.g. 'show ?') and describes each possible\n\
1611 2. Partial help is provided when an abbreviated argument is entered\n\
1612 and you want to know what arguments match the input\n\
1613 (e.g. 'show me?'.)\n\n");
1617 static void permute(struct graph_node
*start
, struct vty
*vty
)
1619 static struct list
*position
= NULL
;
1621 position
= list_new();
1623 struct cmd_token
*stok
= start
->data
;
1624 struct graph_node
*gnn
;
1625 struct listnode
*ln
;
1628 listnode_add(position
, start
);
1629 for (unsigned int i
= 0; i
< vector_active(start
->to
); i
++) {
1630 struct graph_node
*gn
= vector_slot(start
->to
, i
);
1631 struct cmd_token
*tok
= gn
->data
;
1632 if (tok
->attr
== CMD_ATTR_HIDDEN
1633 || tok
->attr
== CMD_ATTR_DEPRECATED
)
1635 else if (tok
->type
== END_TKN
|| gn
== start
) {
1637 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
)) {
1638 struct cmd_token
*tt
= gnn
->data
;
1639 if (tt
->type
< SPECIAL_TKN
)
1640 vty_out(vty
, " %s", tt
->text
);
1643 vty_out(vty
, "...");
1647 if (stok
->type
== FORK_TKN
&& tok
->type
!= FORK_TKN
)
1648 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
))
1657 list_delete_node(position
, listtail(position
));
1660 int cmd_list_cmds(struct vty
*vty
, int do_permute
)
1662 struct cmd_node
*node
= vector_slot(cmdvec
, vty
->node
);
1665 permute(vector_slot(node
->cmdgraph
->nodes
, 0), vty
);
1667 /* loop over all commands at this node */
1668 struct cmd_element
*element
= NULL
;
1669 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
);
1671 if ((element
= vector_slot(node
->cmd_vector
, i
))
1672 && element
->attr
!= CMD_ATTR_DEPRECATED
1673 && element
->attr
!= CMD_ATTR_HIDDEN
)
1674 vty_out(vty
, " %s\n", element
->string
);
1679 /* Help display function for all node. */
1682 "list [permutations]",
1683 "Print command list\n"
1684 "Print all possible command permutations\n")
1686 return cmd_list_cmds(vty
, argc
== 2);
1689 DEFUN (show_commandtree
,
1690 show_commandtree_cmd
,
1691 "show commandtree [permutations]",
1693 "Show command tree\n"
1694 "Permutations that we are interested in\n")
1696 return cmd_list_cmds(vty
, argc
== 3);
1699 DEFUN_HIDDEN(show_cli_graph
,
1704 "Dump current command space as DOT graph\n")
1706 struct cmd_node
*cn
= vector_slot(cmdvec
, vty
->node
);
1707 char *dot
= cmd_graph_dump_dot(cn
->cmdgraph
);
1709 vty_out(vty
, "%s\n", dot
);
1710 XFREE(MTYPE_TMP
, dot
);
1714 static int vty_write_config(struct vty
*vty
)
1717 struct cmd_node
*node
;
1722 if (vty
->type
== VTY_TERM
) {
1723 vty_out(vty
, "\nCurrent configuration:\n");
1724 vty_out(vty
, "!\n");
1727 vty_out(vty
, "frr version %s\n", FRR_VER_SHORT
);
1728 vty_out(vty
, "frr defaults %s\n", DFLT_NAME
);
1729 vty_out(vty
, "!\n");
1731 for (i
= 0; i
< vector_active(cmdvec
); i
++)
1732 if ((node
= vector_slot(cmdvec
, i
)) && node
->func
1733 && (node
->vtysh
|| vty
->type
!= VTY_SHELL
)) {
1734 if ((*node
->func
)(vty
))
1735 vty_out(vty
, "!\n");
1738 if (vty
->type
== VTY_TERM
) {
1739 vty_out(vty
, "end\n");
1745 static int file_write_config(struct vty
*vty
)
1748 char *config_file
, *slash
;
1749 char *config_file_tmp
= NULL
;
1750 char *config_file_sav
= NULL
;
1751 int ret
= CMD_WARNING
;
1752 struct vty
*file_vty
;
1753 struct stat conf_stat
;
1758 /* Check and see if we are operating under vtysh configuration */
1759 if (host
.config
== NULL
) {
1761 "Can't save to configuration file, using vtysh.\n");
1766 config_file
= host
.config
;
1769 #define O_DIRECTORY 0
1771 slash
= strrchr(config_file
, '/');
1773 char *config_dir
= XSTRDUP(MTYPE_TMP
, config_file
);
1774 config_dir
[slash
- config_file
] = '\0';
1775 dirfd
= open(config_dir
, O_DIRECTORY
| O_RDONLY
);
1776 XFREE(MTYPE_TMP
, config_dir
);
1778 dirfd
= open(".", O_DIRECTORY
| O_RDONLY
);
1779 /* if dirfd is invalid, directory sync fails, but we're still OK */
1781 config_file_sav
= XMALLOC(
1782 MTYPE_TMP
, strlen(config_file
) + strlen(CONF_BACKUP_EXT
) + 1);
1783 strcpy(config_file_sav
, config_file
);
1784 strcat(config_file_sav
, CONF_BACKUP_EXT
);
1787 config_file_tmp
= XMALLOC(MTYPE_TMP
, strlen(config_file
) + 8);
1788 sprintf(config_file_tmp
, "%s.XXXXXX", config_file
);
1790 /* Open file to configuration write. */
1791 fd
= mkstemp(config_file_tmp
);
1793 vty_out(vty
, "Can't open configuration file %s.\n",
1797 if (fchmod(fd
, CONFIGFILE_MASK
) != 0) {
1798 vty_out(vty
, "Can't chmod configuration file %s: %s (%d).\n",
1799 config_file_tmp
, safe_strerror(errno
), errno
);
1803 /* Make vty for configuration file. */
1804 file_vty
= vty_new();
1806 file_vty
->type
= VTY_FILE
;
1808 /* Config file header print. */
1809 vty_out(file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1810 vty_time_print(file_vty
, 1);
1811 vty_out(file_vty
, "!\n");
1812 vty_write_config(file_vty
);
1813 vty_close(file_vty
);
1815 if (stat(config_file
, &conf_stat
) >= 0) {
1816 if (unlink(config_file_sav
) != 0)
1817 if (errno
!= ENOENT
) {
1819 "Can't unlink backup configuration file %s.\n",
1823 if (link(config_file
, config_file_sav
) != 0) {
1825 "Can't backup old configuration file %s.\n",
1832 if (rename(config_file_tmp
, config_file
) != 0) {
1833 vty_out(vty
, "Can't save configuration file %s.\n",
1840 vty_out(vty
, "Configuration saved to %s\n", config_file
);
1844 if (ret
!= CMD_SUCCESS
)
1845 unlink(config_file_tmp
);
1848 XFREE(MTYPE_TMP
, config_file_tmp
);
1849 XFREE(MTYPE_TMP
, config_file_sav
);
1853 /* Write current configuration into file. */
1855 DEFUN (config_write
,
1857 "write [<file|memory|terminal>]",
1858 "Write running configuration to memory, network, or terminal\n"
1859 "Write to configuration file\n"
1860 "Write configuration currently in memory\n"
1861 "Write configuration to terminal\n")
1863 const int idx_type
= 1;
1865 // if command was 'write terminal' or 'write memory'
1866 if (argc
== 2 && (!strcmp(argv
[idx_type
]->text
, "terminal"))) {
1867 return vty_write_config(vty
);
1870 return file_write_config(vty
);
1873 /* ALIAS_FIXME for 'write <terminal|memory>' */
1874 DEFUN (show_running_config
,
1875 show_running_config_cmd
,
1876 "show running-config",
1878 "running configuration (same as write terminal)\n")
1880 return vty_write_config(vty
);
1883 /* ALIAS_FIXME for 'write file' */
1884 DEFUN (copy_runningconf_startupconf
,
1885 copy_runningconf_startupconf_cmd
,
1886 "copy running-config startup-config",
1887 "Copy configuration\n"
1888 "Copy running config to... \n"
1889 "Copy running config to startup config (same as write file/memory)\n")
1891 return file_write_config(vty
);
1895 /* Write startup configuration into the terminal. */
1896 DEFUN (show_startup_config
,
1897 show_startup_config_cmd
,
1898 "show startup-config",
1900 "Contents of startup configuration\n")
1907 if (host
.config
== NULL
)
1910 confp
= fopen(host
.config
, "r");
1911 if (confp
== NULL
) {
1912 vty_out(vty
, "Can't open configuration file [%s] due to '%s'\n",
1913 host
.config
, safe_strerror(errno
));
1917 while (fgets(buf
, BUFSIZ
, confp
)) {
1920 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1924 vty_out(vty
, "%s\n", buf
);
1932 int cmd_domainname_set(const char *domainname
)
1934 XFREE(MTYPE_HOST
, host
.domainname
);
1935 host
.domainname
= domainname
? XSTRDUP(MTYPE_HOST
, domainname
) : NULL
;
1939 /* Hostname configuration */
1940 DEFUN(config_domainname
,
1943 "Set system's domain name\n"
1944 "This system's domain name\n")
1946 struct cmd_token
*word
= argv
[1];
1948 if (!isalpha((int)word
->arg
[0])) {
1949 vty_out(vty
, "Please specify string starting with alphabet\n");
1950 return CMD_WARNING_CONFIG_FAILED
;
1953 return cmd_domainname_set(word
->arg
);
1956 DEFUN(config_no_domainname
,
1958 "no domainname [DOMAINNAME]",
1960 "Reset system's domain name\n"
1961 "domain name of this router\n")
1963 return cmd_domainname_set(NULL
);
1966 int cmd_hostname_set(const char *hostname
)
1968 XFREE(MTYPE_HOST
, host
.name
);
1969 host
.name
= hostname
? XSTRDUP(MTYPE_HOST
, hostname
) : NULL
;
1973 /* Hostname configuration */
1974 DEFUN (config_hostname
,
1977 "Set system's network name\n"
1978 "This system's network name\n")
1980 struct cmd_token
*word
= argv
[1];
1982 if (!isalnum((int)word
->arg
[0])) {
1983 vty_out(vty
, "Please specify string starting with alphabet\n");
1984 return CMD_WARNING_CONFIG_FAILED
;
1987 return cmd_hostname_set(word
->arg
);
1990 DEFUN (config_no_hostname
,
1992 "no hostname [HOSTNAME]",
1994 "Reset system's network name\n"
1995 "Host name of this router\n")
1997 return cmd_hostname_set(NULL
);
2000 /* VTY interface password set. */
2001 DEFUN (config_password
,
2003 "password [(8-8)] WORD",
2004 "Modify the terminal connection password\n"
2005 "Specifies a HIDDEN password will follow\n"
2006 "The password string\n")
2010 if (argc
== 3) // '8' was specified
2013 XFREE(MTYPE_HOST
, host
.password
);
2014 host
.password
= NULL
;
2015 if (host
.password_encrypt
)
2016 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2017 host
.password_encrypt
=
2018 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2022 if (!isalnum((int)argv
[idx_8
]->arg
[0])) {
2024 "Please specify string starting with alphanumeric\n");
2025 return CMD_WARNING_CONFIG_FAILED
;
2029 XFREE(MTYPE_HOST
, host
.password
);
2030 host
.password
= NULL
;
2033 if (host
.password_encrypt
)
2034 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2035 host
.password_encrypt
=
2036 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2038 host
.password
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2043 /* VTY interface password delete. */
2044 DEFUN (no_config_password
,
2048 "Modify the terminal connection password\n")
2050 bool warned
= false;
2052 if (host
.password
) {
2053 if (!vty_shell_serv(vty
)) {
2054 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2057 XFREE(MTYPE_HOST
, host
.password
);
2059 host
.password
= NULL
;
2061 if (host
.password_encrypt
) {
2062 if (!warned
&& !vty_shell_serv(vty
))
2063 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2064 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2066 host
.password_encrypt
= NULL
;
2071 /* VTY enable password set. */
2072 DEFUN (config_enable_password
,
2073 enable_password_cmd
,
2074 "enable password [(8-8)] WORD",
2075 "Modify enable password parameters\n"
2076 "Assign the privileged level password\n"
2077 "Specifies a HIDDEN password will follow\n"
2078 "The HIDDEN 'enable' password string\n")
2083 /* Crypt type is specified. */
2085 if (argv
[idx_8
]->arg
[0] == '8') {
2087 XFREE(MTYPE_HOST
, host
.enable
);
2090 if (host
.enable_encrypt
)
2091 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2092 host
.enable_encrypt
=
2093 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2097 vty_out(vty
, "Unknown encryption type.\n");
2098 return CMD_WARNING_CONFIG_FAILED
;
2102 if (!isalnum((int)argv
[idx_8
]->arg
[0])) {
2104 "Please specify string starting with alphanumeric\n");
2105 return CMD_WARNING_CONFIG_FAILED
;
2109 XFREE(MTYPE_HOST
, host
.enable
);
2112 /* Plain password input. */
2114 if (host
.enable_encrypt
)
2115 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2116 host
.enable_encrypt
=
2117 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2119 host
.enable
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2124 /* VTY enable password delete. */
2125 DEFUN (no_config_enable_password
,
2126 no_enable_password_cmd
,
2127 "no enable password",
2129 "Modify enable password parameters\n"
2130 "Assign the privileged level password\n")
2132 bool warned
= false;
2135 if (!vty_shell_serv(vty
)) {
2136 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2139 XFREE(MTYPE_HOST
, host
.enable
);
2143 if (host
.enable_encrypt
) {
2144 if (!warned
&& !vty_shell_serv(vty
))
2145 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2146 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2148 host
.enable_encrypt
= NULL
;
2153 DEFUN (service_password_encrypt
,
2154 service_password_encrypt_cmd
,
2155 "service password-encryption",
2156 "Set up miscellaneous service\n"
2157 "Enable encrypted passwords\n")
2164 if (host
.password
) {
2165 if (host
.password_encrypt
)
2166 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2167 host
.password_encrypt
=
2168 XSTRDUP(MTYPE_HOST
, zencrypt(host
.password
));
2171 if (host
.enable_encrypt
)
2172 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2173 host
.enable_encrypt
=
2174 XSTRDUP(MTYPE_HOST
, zencrypt(host
.enable
));
2180 DEFUN (no_service_password_encrypt
,
2181 no_service_password_encrypt_cmd
,
2182 "no service password-encryption",
2184 "Set up miscellaneous service\n"
2185 "Enable encrypted passwords\n")
2192 if (host
.password_encrypt
)
2193 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2194 host
.password_encrypt
= NULL
;
2196 if (host
.enable_encrypt
)
2197 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2198 host
.enable_encrypt
= NULL
;
2203 DEFUN (config_terminal_length
,
2204 config_terminal_length_cmd
,
2205 "terminal length (0-512)",
2206 "Set terminal line parameters\n"
2207 "Set number of lines on a screen\n"
2208 "Number of lines on screen (0 for no pausing)\n")
2212 vty
->lines
= atoi(argv
[idx_number
]->arg
);
2217 DEFUN (config_terminal_no_length
,
2218 config_terminal_no_length_cmd
,
2219 "terminal no length",
2220 "Set terminal line parameters\n"
2222 "Set number of lines on a screen\n")
2228 DEFUN (service_terminal_length
,
2229 service_terminal_length_cmd
,
2230 "service terminal-length (0-512)",
2231 "Set up miscellaneous service\n"
2232 "System wide terminal length configuration\n"
2233 "Number of lines of VTY (0 means no line control)\n")
2237 host
.lines
= atoi(argv
[idx_number
]->arg
);
2242 DEFUN (no_service_terminal_length
,
2243 no_service_terminal_length_cmd
,
2244 "no service terminal-length [(0-512)]",
2246 "Set up miscellaneous service\n"
2247 "System wide terminal length configuration\n"
2248 "Number of lines of VTY (0 means no line control)\n")
2254 DEFUN_HIDDEN (do_echo
,
2257 "Echo a message back to the vty\n"
2258 "The message to echo\n")
2262 vty_out(vty
, "%s\n",
2263 ((message
= argv_concat(argv
, argc
, 1)) ? message
: ""));
2265 XFREE(MTYPE_TMP
, message
);
2269 DEFUN (config_logmsg
,
2271 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
2272 "Send a message to enabled logging destinations\n"
2274 "The message to send\n")
2276 int idx_log_level
= 1;
2277 int idx_message
= 2;
2281 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2282 return CMD_ERR_NO_MATCH
;
2285 ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
2287 XFREE(MTYPE_TMP
, message
);
2292 DEFUN (show_logging
,
2296 "Show current logging configuration\n")
2298 struct zlog
*zl
= zlog_default
;
2300 vty_out(vty
, "Syslog logging: ");
2301 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
2302 vty_out(vty
, "disabled");
2304 vty_out(vty
, "level %s, facility %s, ident %s",
2305 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
2306 facility_name(zl
->facility
), zl
->ident
);
2309 vty_out(vty
, "Stdout logging: ");
2310 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
2311 vty_out(vty
, "disabled");
2313 vty_out(vty
, "level %s",
2314 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
2317 vty_out(vty
, "Monitor logging: ");
2318 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
2319 vty_out(vty
, "disabled");
2321 vty_out(vty
, "level %s",
2322 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
2325 vty_out(vty
, "File logging: ");
2326 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) || !zl
->fp
)
2327 vty_out(vty
, "disabled");
2329 vty_out(vty
, "level %s, filename %s",
2330 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
2334 vty_out(vty
, "Protocol name: %s\n", zl
->protoname
);
2335 vty_out(vty
, "Record priority: %s\n",
2336 (zl
->record_priority
? "enabled" : "disabled"));
2337 vty_out(vty
, "Timestamp precision: %d\n", zl
->timestamp_precision
);
2342 DEFUN (config_log_stdout
,
2343 config_log_stdout_cmd
,
2344 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2346 "Set stdout logging level\n"
2349 int idx_log_level
= 2;
2351 if (argc
== idx_log_level
) {
2352 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2357 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2358 return CMD_ERR_NO_MATCH
;
2359 zlog_set_level(ZLOG_DEST_STDOUT
, level
);
2363 DEFUN (no_config_log_stdout
,
2364 no_config_log_stdout_cmd
,
2365 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2368 "Cancel logging to stdout\n"
2371 zlog_set_level(ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
2375 DEFUN (config_log_monitor
,
2376 config_log_monitor_cmd
,
2377 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2379 "Set terminal line (monitor) logging level\n"
2382 int idx_log_level
= 2;
2384 if (argc
== idx_log_level
) {
2385 zlog_set_level(ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
2390 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2391 return CMD_ERR_NO_MATCH
;
2392 zlog_set_level(ZLOG_DEST_MONITOR
, level
);
2396 DEFUN (no_config_log_monitor
,
2397 no_config_log_monitor_cmd
,
2398 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2401 "Disable terminal line (monitor) logging\n"
2404 zlog_set_level(ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
2408 static int set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
2412 const char *fullpath
;
2414 /* Path detection. */
2415 if (!IS_DIRECTORY_SEP(*fname
)) {
2416 char cwd
[MAXPATHLEN
+ 1];
2417 cwd
[MAXPATHLEN
] = '\0';
2419 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2420 flog_err_sys(LIB_ERR_SYSTEM_CALL
,
2421 "config_log_file: Unable to alloc mem!");
2422 return CMD_WARNING_CONFIG_FAILED
;
2425 p
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + strlen(fname
) + 2);
2426 sprintf(p
, "%s/%s", cwd
, fname
);
2431 ret
= zlog_set_file(fullpath
, loglevel
);
2434 XFREE(MTYPE_TMP
, p
);
2438 vty_out(vty
, "can't open logfile %s\n", fname
);
2439 return CMD_WARNING_CONFIG_FAILED
;
2443 XFREE(MTYPE_HOST
, host
.logfile
);
2445 host
.logfile
= XSTRDUP(MTYPE_HOST
, fname
);
2447 #if defined(HAVE_CUMULUS)
2448 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2449 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2454 void command_setup_early_logging(const char *dest
, const char *level
)
2459 int nlevel
= level_match(level
);
2461 if (nlevel
!= ZLOG_DISABLED
)
2462 zlog_default
->default_lvl
= nlevel
;
2468 if (strcmp(dest
, "stdout") == 0) {
2469 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2473 if (strcmp(dest
, "syslog") == 0) {
2474 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2478 token
= strstr(dest
, ":");
2484 set_log_file(NULL
, token
, zlog_default
->default_lvl
);
2487 DEFUN (config_log_file
,
2488 config_log_file_cmd
,
2489 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2492 "Logging filename\n"
2495 int idx_filename
= 2;
2496 int idx_log_levels
= 3;
2499 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2501 return CMD_ERR_NO_MATCH
;
2502 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2504 return set_log_file(vty
, argv
[idx_filename
]->arg
,
2505 zlog_default
->default_lvl
);
2508 static void disable_log_file(void)
2513 XFREE(MTYPE_HOST
, host
.logfile
);
2515 host
.logfile
= NULL
;
2518 DEFUN (no_config_log_file
,
2519 no_config_log_file_cmd
,
2520 "no log file [FILENAME [LEVEL]]",
2523 "Cancel logging to file\n"
2524 "Logging file name\n"
2531 DEFUN (config_log_syslog
,
2532 config_log_syslog_cmd
,
2533 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2535 "Set syslog logging level\n"
2538 int idx_log_levels
= 2;
2544 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2546 return CMD_ERR_NO_MATCH
;
2547 zlog_set_level(ZLOG_DEST_SYSLOG
, level
);
2550 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2555 DEFUN (no_config_log_syslog
,
2556 no_config_log_syslog_cmd
,
2557 "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>]",
2560 "Cancel logging to syslog\n"
2564 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2568 DEFUN (config_log_facility
,
2569 config_log_facility_cmd
,
2570 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2572 "Facility parameter for syslog messages\n"
2576 int facility
= facility_match(argv
[idx_target
]->arg
);
2578 zlog_default
->facility
= facility
;
2582 DEFUN (no_config_log_facility
,
2583 no_config_log_facility_cmd
,
2584 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2587 "Reset syslog facility to default (daemon)\n"
2590 zlog_default
->facility
= LOG_DAEMON
;
2594 DEFUN (config_log_record_priority
,
2595 config_log_record_priority_cmd
,
2596 "log record-priority",
2598 "Log the priority of the message within the message\n")
2600 zlog_default
->record_priority
= 1;
2604 DEFUN (no_config_log_record_priority
,
2605 no_config_log_record_priority_cmd
,
2606 "no log record-priority",
2609 "Do not log the priority of the message within the message\n")
2611 zlog_default
->record_priority
= 0;
2615 DEFUN (config_log_timestamp_precision
,
2616 config_log_timestamp_precision_cmd
,
2617 "log timestamp precision (0-6)",
2619 "Timestamp configuration\n"
2620 "Set the timestamp precision\n"
2621 "Number of subsecond digits\n")
2624 zlog_default
->timestamp_precision
=
2625 strtoul(argv
[idx_number
]->arg
, NULL
, 10);
2629 DEFUN (no_config_log_timestamp_precision
,
2630 no_config_log_timestamp_precision_cmd
,
2631 "no log timestamp precision",
2634 "Timestamp configuration\n"
2635 "Reset the timestamp precision to the default value of 0\n")
2637 zlog_default
->timestamp_precision
= 0;
2641 DEFUN (debug_memstats
,
2643 "[no] debug memstats-at-exit",
2646 "Print memory type statistics at exit\n")
2648 debug_memstats_at_exit
= !!strcmp(argv
[0]->text
, "no");
2652 int cmd_banner_motd_file(const char *file
)
2654 int success
= CMD_SUCCESS
;
2659 rpath
= realpath(file
, p
);
2661 return CMD_ERR_NO_FILE
;
2662 in
= strstr(rpath
, SYSCONFDIR
);
2665 XFREE(MTYPE_HOST
, host
.motdfile
);
2666 host
.motdfile
= XSTRDUP(MTYPE_HOST
, file
);
2668 success
= CMD_WARNING_CONFIG_FAILED
;
2673 DEFUN (banner_motd_file
,
2674 banner_motd_file_cmd
,
2675 "banner motd file FILE",
2678 "Banner from a file\n"
2682 const char *filename
= argv
[idx_file
]->arg
;
2683 int cmd
= cmd_banner_motd_file(filename
);
2685 if (cmd
== CMD_ERR_NO_FILE
)
2686 vty_out(vty
, "%s does not exist", filename
);
2687 else if (cmd
== CMD_WARNING_CONFIG_FAILED
)
2688 vty_out(vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2693 DEFUN (banner_motd_default
,
2694 banner_motd_default_cmd
,
2695 "banner motd default",
2696 "Set banner string\n"
2697 "Strings for motd\n"
2700 host
.motd
= default_motd
;
2704 DEFUN (no_banner_motd
,
2708 "Set banner string\n"
2709 "Strings for motd\n")
2713 XFREE(MTYPE_HOST
, host
.motdfile
);
2714 host
.motdfile
= NULL
;
2721 "Find CLI command containing text\n"
2722 "Text to search for\n")
2724 char *text
= argv_concat(argv
, argc
, 1);
2725 const struct cmd_node
*node
;
2726 const struct cmd_element
*cli
;
2729 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++) {
2730 node
= vector_slot(cmdvec
, i
);
2733 clis
= node
->cmd_vector
;
2734 for (unsigned int j
= 0; j
< vector_active(clis
); j
++) {
2735 cli
= vector_slot(clis
, j
);
2736 if (strcasestr(cli
->string
, text
))
2737 vty_out(vty
, " (%s) %s\n",
2738 node_names
[node
->node
], cli
->string
);
2742 XFREE(MTYPE_TMP
, text
);
2747 /* Set config filename. Called from vty.c */
2748 void host_config_set(const char *filename
)
2751 XFREE(MTYPE_HOST
, host
.config
);
2752 host
.config
= XSTRDUP(MTYPE_HOST
, filename
);
2755 const char *host_config_get(void)
2760 void install_default(enum node_type node
)
2762 install_element(node
, &config_exit_cmd
);
2763 install_element(node
, &config_quit_cmd
);
2764 install_element(node
, &config_end_cmd
);
2765 install_element(node
, &config_help_cmd
);
2766 install_element(node
, &config_list_cmd
);
2767 install_element(node
, &show_cli_graph_cmd
);
2768 install_element(node
, &find_cmd
);
2770 install_element(node
, &config_write_cmd
);
2771 install_element(node
, &show_running_config_cmd
);
2773 install_element(node
, &autocomplete_cmd
);
2776 /* Initialize command interface. Install basic nodes and commands.
2778 * terminal = 0 -- vtysh / no logging, no config control
2779 * terminal = 1 -- normal daemon
2780 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2781 void cmd_init(int terminal
)
2783 struct utsname names
;
2785 if (array_size(node_names
) != NODE_TYPE_MAX
)
2786 assert(!"Update the CLI node description array!");
2791 /* register command preprocessors */
2792 hook_register(cmd_execute
, handle_pipe_action
);
2793 hook_register(cmd_execute_done
, handle_pipe_action_done
);
2795 varhandlers
= list_new();
2797 /* Allocate initial top vector of commands. */
2798 cmdvec
= vector_init(VECTOR_MIN_SIZE
);
2800 /* Default host value settings. */
2801 host
.name
= XSTRDUP(MTYPE_HOST
, names
.nodename
);
2802 #ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
2803 if ((strcmp(names
.domainname
, "(none)") == 0))
2804 host
.domainname
= NULL
;
2806 host
.domainname
= XSTRDUP(MTYPE_HOST
, names
.domainname
);
2808 host
.domainname
= NULL
;
2810 host
.password
= NULL
;
2812 host
.logfile
= NULL
;
2814 host
.noconfig
= (terminal
< 0);
2816 host
.motd
= default_motd
;
2817 host
.motdfile
= NULL
;
2819 /* Install top nodes. */
2820 install_node(&view_node
, NULL
);
2821 install_node(&enable_node
, NULL
);
2822 install_node(&auth_node
, NULL
);
2823 install_node(&auth_enable_node
, NULL
);
2824 install_node(&config_node
, config_write_host
);
2826 /* Each node's basic commands. */
2827 install_element(VIEW_NODE
, &show_version_cmd
);
2828 install_element(ENABLE_NODE
, &show_startup_config_cmd
);
2829 install_element(ENABLE_NODE
, &debug_memstats_cmd
);
2832 install_element(VIEW_NODE
, &config_list_cmd
);
2833 install_element(VIEW_NODE
, &config_exit_cmd
);
2834 install_element(VIEW_NODE
, &config_quit_cmd
);
2835 install_element(VIEW_NODE
, &config_help_cmd
);
2836 install_element(VIEW_NODE
, &config_enable_cmd
);
2837 install_element(VIEW_NODE
, &config_terminal_length_cmd
);
2838 install_element(VIEW_NODE
, &config_terminal_no_length_cmd
);
2839 install_element(VIEW_NODE
, &show_logging_cmd
);
2840 install_element(VIEW_NODE
, &show_commandtree_cmd
);
2841 install_element(VIEW_NODE
, &echo_cmd
);
2842 install_element(VIEW_NODE
, &autocomplete_cmd
);
2843 install_element(VIEW_NODE
, &find_cmd
);
2845 install_element(ENABLE_NODE
, &config_end_cmd
);
2846 install_element(ENABLE_NODE
, &config_disable_cmd
);
2847 install_element(ENABLE_NODE
, &config_terminal_cmd
);
2848 install_element(ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2849 install_element(ENABLE_NODE
, &config_write_cmd
);
2850 install_element(ENABLE_NODE
, &show_running_config_cmd
);
2851 install_element(ENABLE_NODE
, &config_logmsg_cmd
);
2853 install_default(CONFIG_NODE
);
2856 workqueue_cmd_init();
2860 install_element(CONFIG_NODE
, &hostname_cmd
);
2861 install_element(CONFIG_NODE
, &no_hostname_cmd
);
2862 install_element(CONFIG_NODE
, &domainname_cmd
);
2863 install_element(CONFIG_NODE
, &no_domainname_cmd
);
2864 install_element(CONFIG_NODE
, &frr_version_defaults_cmd
);
2865 install_element(CONFIG_NODE
, &debug_memstats_cmd
);
2868 install_element(CONFIG_NODE
, &password_cmd
);
2869 install_element(CONFIG_NODE
, &no_password_cmd
);
2870 install_element(CONFIG_NODE
, &enable_password_cmd
);
2871 install_element(CONFIG_NODE
, &no_enable_password_cmd
);
2873 install_element(CONFIG_NODE
, &config_log_stdout_cmd
);
2874 install_element(CONFIG_NODE
, &no_config_log_stdout_cmd
);
2875 install_element(CONFIG_NODE
, &config_log_monitor_cmd
);
2876 install_element(CONFIG_NODE
, &no_config_log_monitor_cmd
);
2877 install_element(CONFIG_NODE
, &config_log_file_cmd
);
2878 install_element(CONFIG_NODE
, &no_config_log_file_cmd
);
2879 install_element(CONFIG_NODE
, &config_log_syslog_cmd
);
2880 install_element(CONFIG_NODE
, &no_config_log_syslog_cmd
);
2881 install_element(CONFIG_NODE
, &config_log_facility_cmd
);
2882 install_element(CONFIG_NODE
, &no_config_log_facility_cmd
);
2883 install_element(CONFIG_NODE
, &config_log_record_priority_cmd
);
2884 install_element(CONFIG_NODE
,
2885 &no_config_log_record_priority_cmd
);
2886 install_element(CONFIG_NODE
,
2887 &config_log_timestamp_precision_cmd
);
2888 install_element(CONFIG_NODE
,
2889 &no_config_log_timestamp_precision_cmd
);
2890 install_element(CONFIG_NODE
, &service_password_encrypt_cmd
);
2891 install_element(CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2892 install_element(CONFIG_NODE
, &banner_motd_default_cmd
);
2893 install_element(CONFIG_NODE
, &banner_motd_file_cmd
);
2894 install_element(CONFIG_NODE
, &no_banner_motd_cmd
);
2895 install_element(CONFIG_NODE
, &service_terminal_length_cmd
);
2896 install_element(CONFIG_NODE
, &no_service_terminal_length_cmd
);
2898 vrf_install_commands();
2902 grammar_sandbox_init();
2906 void cmd_terminate()
2908 struct cmd_node
*cmd_node
;
2910 hook_unregister(cmd_execute
, handle_pipe_action
);
2911 hook_unregister(cmd_execute_done
, handle_pipe_action_done
);
2914 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++)
2915 if ((cmd_node
= vector_slot(cmdvec
, i
)) != NULL
) {
2916 // deleting the graph delets the cmd_element as
2918 graph_delete_graph(cmd_node
->cmdgraph
);
2919 vector_free(cmd_node
->cmd_vector
);
2920 hash_clean(cmd_node
->cmd_hash
, NULL
);
2921 hash_free(cmd_node
->cmd_hash
);
2922 cmd_node
->cmd_hash
= NULL
;
2925 vector_free(cmdvec
);
2930 XFREE(MTYPE_HOST
, host
.name
);
2931 if (host
.domainname
)
2932 XFREE(MTYPE_HOST
, host
.domainname
);
2934 XFREE(MTYPE_HOST
, host
.password
);
2935 if (host
.password_encrypt
)
2936 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2938 XFREE(MTYPE_HOST
, host
.enable
);
2939 if (host
.enable_encrypt
)
2940 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2942 XFREE(MTYPE_HOST
, host
.logfile
);
2944 XFREE(MTYPE_HOST
, host
.motdfile
);
2946 XFREE(MTYPE_HOST
, host
.config
);
2948 list_delete_and_null(&varhandlers
);