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
, " ");
1202 /* match result to known actions */
1203 if (strmatch(token
, "include")) {
1204 /* the remaining text should be a regexp */
1205 char *regexp
= working
;
1208 vty_out(vty
, "%% Need a regexp to filter with\n");
1212 bool succ
= vty_set_include(vty
, regexp
);
1215 vty_out(vty
, "%% Bad regexp '%s'\n", regexp
);
1218 *cmd_out
= XSTRDUP(MTYPE_TMP
, cmd_in
);
1222 vty_out(vty
, "%% Unknown action '%s'\n", token
);
1227 XFREE(MTYPE_TMP
, orig
);
1231 static int handle_pipe_action_done(struct vty
*vty
, const char *cmd_exec
)
1234 vty_set_include(vty
, NULL
);
1239 int cmd_execute(struct vty
*vty
, const char *cmd
,
1240 const struct cmd_element
**matched
, int vtysh
)
1243 char *cmd_out
= NULL
;
1244 const char *cmd_exec
;
1247 hook_call(cmd_execute
, vty
, cmd
, &cmd_out
);
1248 cmd_exec
= cmd_out
? (const char *)cmd_out
: cmd
;
1250 vline
= cmd_make_strvec(cmd_exec
);
1253 ret
= cmd_execute_command(vline
, vty
, matched
, vtysh
);
1254 cmd_free_strvec(vline
);
1259 hook_call(cmd_execute_done
, vty
, cmd_exec
);
1262 XFREE(MTYPE_TMP
, cmd_out
);
1269 * Parse one line of config, walking up the parse tree attempting to find a
1272 * @param vty The vty context in which the command should be executed.
1273 * @param cmd Pointer where the struct cmd_element* of the match command
1274 * will be stored, if any. May be set to NULL if this info is
1276 * @param use_daemon Boolean to control whether or not we match on
1277 * CMD_SUCCESS_DAEMON
1279 * @return The status of the command that has been executed or an error code
1280 * as to why no command could be executed.
1282 int command_config_read_one_line(struct vty
*vty
,
1283 const struct cmd_element
**cmd
, int use_daemon
)
1289 vline
= cmd_make_strvec(vty
->buf
);
1291 /* In case of comment line */
1295 /* Execute configuration command : this is strict match */
1296 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1298 // Climb the tree and try the command again at each node
1299 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1300 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1301 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1302 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
1303 && vty
->node
!= CONFIG_NODE
) {
1305 saved_node
= vty
->node
;
1307 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1308 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1309 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1310 && vty
->node
> CONFIG_NODE
) {
1311 vty
->node
= node_parent(vty
->node
);
1312 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1315 // If climbing the tree did not work then ignore the command and
1316 // stay at the same node
1317 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1318 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1319 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
) {
1320 vty
->node
= saved_node
;
1324 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1325 memcpy(vty
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
1327 cmd_free_strvec(vline
);
1332 /* Configuration make from file. */
1333 int config_from_file(struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
1335 int ret
, error_ret
= 0;
1338 while (fgets(vty
->buf
, VTY_BUFSIZ
, fp
)) {
1342 ret
= command_config_read_one_line(vty
, NULL
, 0);
1344 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1345 && ret
!= CMD_ERR_NOTHING_TODO
)
1356 /* Configuration from terminal */
1357 DEFUN (config_terminal
,
1358 config_terminal_cmd
,
1359 "configure terminal",
1360 "Configuration from vty interface\n"
1361 "Configuration terminal\n")
1363 if (vty_config_lock(vty
))
1364 vty
->node
= CONFIG_NODE
;
1366 vty_out(vty
, "VTY configuration is locked by other VTY\n");
1367 return CMD_WARNING_CONFIG_FAILED
;
1372 /* Enable command */
1376 "Turn on privileged mode command\n")
1378 /* If enable password is NULL, change to ENABLE_NODE */
1379 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
)
1380 || vty
->type
== VTY_SHELL_SERV
)
1381 vty
->node
= ENABLE_NODE
;
1383 vty
->node
= AUTH_ENABLE_NODE
;
1388 /* Disable command */
1392 "Turn off privileged mode command\n")
1394 if (vty
->node
== ENABLE_NODE
)
1395 vty
->node
= VIEW_NODE
;
1399 /* Down vty node level. */
1403 "Exit current mode and down to previous mode\n")
1409 void cmd_exit(struct vty
*vty
)
1411 switch (vty
->node
) {
1417 vty
->status
= VTY_CLOSE
;
1420 vty
->node
= ENABLE_NODE
;
1421 vty_config_unlock(vty
);
1423 case INTERFACE_NODE
:
1425 case LOGICALROUTER_NODE
:
1437 case LDP_L2VPN_NODE
:
1444 vty
->node
= CONFIG_NODE
;
1447 case BGP_IPV4M_NODE
:
1448 case BGP_IPV4L_NODE
:
1449 case BGP_VPNV4_NODE
:
1450 case BGP_VPNV6_NODE
:
1451 case BGP_FLOWSPECV4_NODE
:
1452 case BGP_FLOWSPECV6_NODE
:
1453 case BGP_VRF_POLICY_NODE
:
1454 case BGP_VNC_DEFAULTS_NODE
:
1455 case BGP_VNC_NVE_GROUP_NODE
:
1456 case BGP_VNC_L2_GROUP_NODE
:
1458 case BGP_IPV6M_NODE
:
1460 case BGP_IPV6L_NODE
:
1461 vty
->node
= BGP_NODE
;
1463 case BGP_EVPN_VNI_NODE
:
1464 vty
->node
= BGP_EVPN_NODE
;
1468 vty
->node
= LDP_NODE
;
1470 case LDP_IPV4_IFACE_NODE
:
1471 vty
->node
= LDP_IPV4_NODE
;
1473 case LDP_IPV6_IFACE_NODE
:
1474 vty
->node
= LDP_IPV6_NODE
;
1476 case LDP_PSEUDOWIRE_NODE
:
1477 vty
->node
= LDP_L2VPN_NODE
;
1479 case KEYCHAIN_KEY_NODE
:
1480 vty
->node
= KEYCHAIN_NODE
;
1482 case LINK_PARAMS_NODE
:
1483 vty
->node
= INTERFACE_NODE
;
1486 vty
->node
= BFD_NODE
;
1497 "Exit current mode and down to previous mode\n")
1499 return config_exit(self
, vty
, argc
, argv
);
1503 /* End of configuration. */
1507 "End current mode and change to enable mode.\n")
1509 switch (vty
->node
) {
1512 /* Nothing to do. */
1515 case INTERFACE_NODE
:
1517 case LOGICALROUTER_NODE
:
1526 case BGP_VRF_POLICY_NODE
:
1527 case BGP_VNC_DEFAULTS_NODE
:
1528 case BGP_VNC_NVE_GROUP_NODE
:
1529 case BGP_VNC_L2_GROUP_NODE
:
1530 case BGP_VPNV4_NODE
:
1531 case BGP_VPNV6_NODE
:
1532 case BGP_FLOWSPECV4_NODE
:
1533 case BGP_FLOWSPECV6_NODE
:
1535 case BGP_IPV4M_NODE
:
1536 case BGP_IPV4L_NODE
:
1538 case BGP_IPV6M_NODE
:
1540 case BGP_EVPN_VNI_NODE
:
1541 case BGP_IPV6L_NODE
:
1549 case LDP_IPV4_IFACE_NODE
:
1550 case LDP_IPV6_IFACE_NODE
:
1551 case LDP_L2VPN_NODE
:
1552 case LDP_PSEUDOWIRE_NODE
:
1555 case KEYCHAIN_KEY_NODE
:
1557 case LINK_PARAMS_NODE
:
1560 vty_config_unlock(vty
);
1561 vty
->node
= ENABLE_NODE
;
1570 DEFUN (show_version
,
1574 "Displays zebra version\n")
1576 vty_out(vty
, "%s %s (%s).\n", FRR_FULL_NAME
, FRR_VERSION
,
1577 cmd_hostname_get() ? cmd_hostname_get() : "");
1578 vty_out(vty
, "%s%s\n", FRR_COPYRIGHT
, GIT_INFO
);
1579 vty_out(vty
, "configured with:\n %s\n", FRR_CONFIG_ARGS
);
1584 /* "Set" version ... ignore version tags */
1585 DEFUN (frr_version_defaults
,
1586 frr_version_defaults_cmd
,
1587 "frr <version|defaults> LINE...",
1588 "FRRouting global parameters\n"
1589 "version configuration was written by\n"
1590 "set of configuration defaults used\n"
1596 /* Help display function for all node. */
1600 "Description of the interactive help system\n")
1603 "Quagga VTY provides advanced help feature. When you need help,\n\
1604 anytime at the command line please press '?'.\n\
1606 If nothing matches, the help list will be empty and you must backup\n\
1607 until entering a '?' shows the available options.\n\
1608 Two styles of help are provided:\n\
1609 1. Full help is available when you are ready to enter a\n\
1610 command argument (e.g. 'show ?') and describes each possible\n\
1612 2. Partial help is provided when an abbreviated argument is entered\n\
1613 and you want to know what arguments match the input\n\
1614 (e.g. 'show me?'.)\n\n");
1618 static void permute(struct graph_node
*start
, struct vty
*vty
)
1620 static struct list
*position
= NULL
;
1622 position
= list_new();
1624 struct cmd_token
*stok
= start
->data
;
1625 struct graph_node
*gnn
;
1626 struct listnode
*ln
;
1629 listnode_add(position
, start
);
1630 for (unsigned int i
= 0; i
< vector_active(start
->to
); i
++) {
1631 struct graph_node
*gn
= vector_slot(start
->to
, i
);
1632 struct cmd_token
*tok
= gn
->data
;
1633 if (tok
->attr
== CMD_ATTR_HIDDEN
1634 || tok
->attr
== CMD_ATTR_DEPRECATED
)
1636 else if (tok
->type
== END_TKN
|| gn
== start
) {
1638 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
)) {
1639 struct cmd_token
*tt
= gnn
->data
;
1640 if (tt
->type
< SPECIAL_TKN
)
1641 vty_out(vty
, " %s", tt
->text
);
1644 vty_out(vty
, "...");
1648 if (stok
->type
== FORK_TKN
&& tok
->type
!= FORK_TKN
)
1649 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
))
1658 list_delete_node(position
, listtail(position
));
1661 int cmd_list_cmds(struct vty
*vty
, int do_permute
)
1663 struct cmd_node
*node
= vector_slot(cmdvec
, vty
->node
);
1666 permute(vector_slot(node
->cmdgraph
->nodes
, 0), vty
);
1668 /* loop over all commands at this node */
1669 struct cmd_element
*element
= NULL
;
1670 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
);
1672 if ((element
= vector_slot(node
->cmd_vector
, i
))
1673 && element
->attr
!= CMD_ATTR_DEPRECATED
1674 && element
->attr
!= CMD_ATTR_HIDDEN
)
1675 vty_out(vty
, " %s\n", element
->string
);
1680 /* Help display function for all node. */
1683 "list [permutations]",
1684 "Print command list\n"
1685 "Print all possible command permutations\n")
1687 return cmd_list_cmds(vty
, argc
== 2);
1690 DEFUN (show_commandtree
,
1691 show_commandtree_cmd
,
1692 "show commandtree [permutations]",
1694 "Show command tree\n"
1695 "Permutations that we are interested in\n")
1697 return cmd_list_cmds(vty
, argc
== 3);
1700 DEFUN_HIDDEN(show_cli_graph
,
1705 "Dump current command space as DOT graph\n")
1707 struct cmd_node
*cn
= vector_slot(cmdvec
, vty
->node
);
1708 char *dot
= cmd_graph_dump_dot(cn
->cmdgraph
);
1710 vty_out(vty
, "%s\n", dot
);
1711 XFREE(MTYPE_TMP
, dot
);
1715 static int vty_write_config(struct vty
*vty
)
1718 struct cmd_node
*node
;
1723 if (vty
->type
== VTY_TERM
) {
1724 vty_out(vty
, "\nCurrent configuration:\n");
1725 vty_out(vty
, "!\n");
1728 vty_out(vty
, "frr version %s\n", FRR_VER_SHORT
);
1729 vty_out(vty
, "frr defaults %s\n", DFLT_NAME
);
1730 vty_out(vty
, "!\n");
1732 for (i
= 0; i
< vector_active(cmdvec
); i
++)
1733 if ((node
= vector_slot(cmdvec
, i
)) && node
->func
1734 && (node
->vtysh
|| vty
->type
!= VTY_SHELL
)) {
1735 if ((*node
->func
)(vty
))
1736 vty_out(vty
, "!\n");
1739 if (vty
->type
== VTY_TERM
) {
1740 vty_out(vty
, "end\n");
1746 static int file_write_config(struct vty
*vty
)
1749 char *config_file
, *slash
;
1750 char *config_file_tmp
= NULL
;
1751 char *config_file_sav
= NULL
;
1752 int ret
= CMD_WARNING
;
1753 struct vty
*file_vty
;
1754 struct stat conf_stat
;
1759 /* Check and see if we are operating under vtysh configuration */
1760 if (host
.config
== NULL
) {
1762 "Can't save to configuration file, using vtysh.\n");
1767 config_file
= host
.config
;
1770 #define O_DIRECTORY 0
1772 slash
= strrchr(config_file
, '/');
1774 char *config_dir
= XSTRDUP(MTYPE_TMP
, config_file
);
1775 config_dir
[slash
- config_file
] = '\0';
1776 dirfd
= open(config_dir
, O_DIRECTORY
| O_RDONLY
);
1777 XFREE(MTYPE_TMP
, config_dir
);
1779 dirfd
= open(".", O_DIRECTORY
| O_RDONLY
);
1780 /* if dirfd is invalid, directory sync fails, but we're still OK */
1782 config_file_sav
= XMALLOC(
1783 MTYPE_TMP
, strlen(config_file
) + strlen(CONF_BACKUP_EXT
) + 1);
1784 strcpy(config_file_sav
, config_file
);
1785 strcat(config_file_sav
, CONF_BACKUP_EXT
);
1788 config_file_tmp
= XMALLOC(MTYPE_TMP
, strlen(config_file
) + 8);
1789 sprintf(config_file_tmp
, "%s.XXXXXX", config_file
);
1791 /* Open file to configuration write. */
1792 fd
= mkstemp(config_file_tmp
);
1794 vty_out(vty
, "Can't open configuration file %s.\n",
1798 if (fchmod(fd
, CONFIGFILE_MASK
) != 0) {
1799 vty_out(vty
, "Can't chmod configuration file %s: %s (%d).\n",
1800 config_file_tmp
, safe_strerror(errno
), errno
);
1804 /* Make vty for configuration file. */
1805 file_vty
= vty_new();
1807 file_vty
->type
= VTY_FILE
;
1809 /* Config file header print. */
1810 vty_out(file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1811 vty_time_print(file_vty
, 1);
1812 vty_out(file_vty
, "!\n");
1813 vty_write_config(file_vty
);
1814 vty_close(file_vty
);
1816 if (stat(config_file
, &conf_stat
) >= 0) {
1817 if (unlink(config_file_sav
) != 0)
1818 if (errno
!= ENOENT
) {
1820 "Can't unlink backup configuration file %s.\n",
1824 if (link(config_file
, config_file_sav
) != 0) {
1826 "Can't backup old configuration file %s.\n",
1833 if (rename(config_file_tmp
, config_file
) != 0) {
1834 vty_out(vty
, "Can't save configuration file %s.\n",
1841 vty_out(vty
, "Configuration saved to %s\n", config_file
);
1845 if (ret
!= CMD_SUCCESS
)
1846 unlink(config_file_tmp
);
1849 XFREE(MTYPE_TMP
, config_file_tmp
);
1850 XFREE(MTYPE_TMP
, config_file_sav
);
1854 /* Write current configuration into file. */
1856 DEFUN (config_write
,
1858 "write [<file|memory|terminal>]",
1859 "Write running configuration to memory, network, or terminal\n"
1860 "Write to configuration file\n"
1861 "Write configuration currently in memory\n"
1862 "Write configuration to terminal\n")
1864 const int idx_type
= 1;
1866 // if command was 'write terminal' or 'write memory'
1867 if (argc
== 2 && (!strcmp(argv
[idx_type
]->text
, "terminal"))) {
1868 return vty_write_config(vty
);
1871 return file_write_config(vty
);
1874 /* ALIAS_FIXME for 'write <terminal|memory>' */
1875 DEFUN (show_running_config
,
1876 show_running_config_cmd
,
1877 "show running-config",
1879 "running configuration (same as write terminal)\n")
1881 return vty_write_config(vty
);
1884 /* ALIAS_FIXME for 'write file' */
1885 DEFUN (copy_runningconf_startupconf
,
1886 copy_runningconf_startupconf_cmd
,
1887 "copy running-config startup-config",
1888 "Copy configuration\n"
1889 "Copy running config to... \n"
1890 "Copy running config to startup config (same as write file/memory)\n")
1892 return file_write_config(vty
);
1896 /* Write startup configuration into the terminal. */
1897 DEFUN (show_startup_config
,
1898 show_startup_config_cmd
,
1899 "show startup-config",
1901 "Contents of startup configuration\n")
1908 if (host
.config
== NULL
)
1911 confp
= fopen(host
.config
, "r");
1912 if (confp
== NULL
) {
1913 vty_out(vty
, "Can't open configuration file [%s] due to '%s'\n",
1914 host
.config
, safe_strerror(errno
));
1918 while (fgets(buf
, BUFSIZ
, confp
)) {
1921 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1925 vty_out(vty
, "%s\n", buf
);
1933 int cmd_domainname_set(const char *domainname
)
1935 XFREE(MTYPE_HOST
, host
.domainname
);
1936 host
.domainname
= domainname
? XSTRDUP(MTYPE_HOST
, domainname
) : NULL
;
1940 /* Hostname configuration */
1941 DEFUN(config_domainname
,
1944 "Set system's domain name\n"
1945 "This system's domain name\n")
1947 struct cmd_token
*word
= argv
[1];
1949 if (!isalpha((int)word
->arg
[0])) {
1950 vty_out(vty
, "Please specify string starting with alphabet\n");
1951 return CMD_WARNING_CONFIG_FAILED
;
1954 return cmd_domainname_set(word
->arg
);
1957 DEFUN(config_no_domainname
,
1959 "no domainname [DOMAINNAME]",
1961 "Reset system's domain name\n"
1962 "domain name of this router\n")
1964 return cmd_domainname_set(NULL
);
1967 int cmd_hostname_set(const char *hostname
)
1969 XFREE(MTYPE_HOST
, host
.name
);
1970 host
.name
= hostname
? XSTRDUP(MTYPE_HOST
, hostname
) : NULL
;
1974 /* Hostname configuration */
1975 DEFUN (config_hostname
,
1978 "Set system's network name\n"
1979 "This system's network name\n")
1981 struct cmd_token
*word
= argv
[1];
1983 if (!isalnum((int)word
->arg
[0])) {
1984 vty_out(vty
, "Please specify string starting with alphabet\n");
1985 return CMD_WARNING_CONFIG_FAILED
;
1988 return cmd_hostname_set(word
->arg
);
1991 DEFUN (config_no_hostname
,
1993 "no hostname [HOSTNAME]",
1995 "Reset system's network name\n"
1996 "Host name of this router\n")
1998 return cmd_hostname_set(NULL
);
2001 /* VTY interface password set. */
2002 DEFUN (config_password
,
2004 "password [(8-8)] WORD",
2005 "Modify the terminal connection password\n"
2006 "Specifies a HIDDEN password will follow\n"
2007 "The password string\n")
2011 if (argc
== 3) // '8' was specified
2014 XFREE(MTYPE_HOST
, host
.password
);
2015 host
.password
= NULL
;
2016 if (host
.password_encrypt
)
2017 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2018 host
.password_encrypt
=
2019 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2023 if (!isalnum((int)argv
[idx_8
]->arg
[0])) {
2025 "Please specify string starting with alphanumeric\n");
2026 return CMD_WARNING_CONFIG_FAILED
;
2030 XFREE(MTYPE_HOST
, host
.password
);
2031 host
.password
= NULL
;
2034 if (host
.password_encrypt
)
2035 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2036 host
.password_encrypt
=
2037 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2039 host
.password
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2044 /* VTY interface password delete. */
2045 DEFUN (no_config_password
,
2049 "Modify the terminal connection password\n")
2051 bool warned
= false;
2053 if (host
.password
) {
2054 if (!vty_shell_serv(vty
)) {
2055 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2058 XFREE(MTYPE_HOST
, host
.password
);
2060 host
.password
= NULL
;
2062 if (host
.password_encrypt
) {
2063 if (!warned
&& !vty_shell_serv(vty
))
2064 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2065 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2067 host
.password_encrypt
= NULL
;
2072 /* VTY enable password set. */
2073 DEFUN (config_enable_password
,
2074 enable_password_cmd
,
2075 "enable password [(8-8)] WORD",
2076 "Modify enable password parameters\n"
2077 "Assign the privileged level password\n"
2078 "Specifies a HIDDEN password will follow\n"
2079 "The HIDDEN 'enable' password string\n")
2084 /* Crypt type is specified. */
2086 if (argv
[idx_8
]->arg
[0] == '8') {
2088 XFREE(MTYPE_HOST
, host
.enable
);
2091 if (host
.enable_encrypt
)
2092 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2093 host
.enable_encrypt
=
2094 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2098 vty_out(vty
, "Unknown encryption type.\n");
2099 return CMD_WARNING_CONFIG_FAILED
;
2103 if (!isalnum((int)argv
[idx_8
]->arg
[0])) {
2105 "Please specify string starting with alphanumeric\n");
2106 return CMD_WARNING_CONFIG_FAILED
;
2110 XFREE(MTYPE_HOST
, host
.enable
);
2113 /* Plain password input. */
2115 if (host
.enable_encrypt
)
2116 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2117 host
.enable_encrypt
=
2118 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2120 host
.enable
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2125 /* VTY enable password delete. */
2126 DEFUN (no_config_enable_password
,
2127 no_enable_password_cmd
,
2128 "no enable password",
2130 "Modify enable password parameters\n"
2131 "Assign the privileged level password\n")
2133 bool warned
= false;
2136 if (!vty_shell_serv(vty
)) {
2137 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2140 XFREE(MTYPE_HOST
, host
.enable
);
2144 if (host
.enable_encrypt
) {
2145 if (!warned
&& !vty_shell_serv(vty
))
2146 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2147 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2149 host
.enable_encrypt
= NULL
;
2154 DEFUN (service_password_encrypt
,
2155 service_password_encrypt_cmd
,
2156 "service password-encryption",
2157 "Set up miscellaneous service\n"
2158 "Enable encrypted passwords\n")
2165 if (host
.password
) {
2166 if (host
.password_encrypt
)
2167 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2168 host
.password_encrypt
=
2169 XSTRDUP(MTYPE_HOST
, zencrypt(host
.password
));
2172 if (host
.enable_encrypt
)
2173 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2174 host
.enable_encrypt
=
2175 XSTRDUP(MTYPE_HOST
, zencrypt(host
.enable
));
2181 DEFUN (no_service_password_encrypt
,
2182 no_service_password_encrypt_cmd
,
2183 "no service password-encryption",
2185 "Set up miscellaneous service\n"
2186 "Enable encrypted passwords\n")
2193 if (host
.password_encrypt
)
2194 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2195 host
.password_encrypt
= NULL
;
2197 if (host
.enable_encrypt
)
2198 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2199 host
.enable_encrypt
= NULL
;
2204 DEFUN (config_terminal_length
,
2205 config_terminal_length_cmd
,
2206 "terminal length (0-512)",
2207 "Set terminal line parameters\n"
2208 "Set number of lines on a screen\n"
2209 "Number of lines on screen (0 for no pausing)\n")
2213 vty
->lines
= atoi(argv
[idx_number
]->arg
);
2218 DEFUN (config_terminal_no_length
,
2219 config_terminal_no_length_cmd
,
2220 "terminal no length",
2221 "Set terminal line parameters\n"
2223 "Set number of lines on a screen\n")
2229 DEFUN (service_terminal_length
,
2230 service_terminal_length_cmd
,
2231 "service terminal-length (0-512)",
2232 "Set up miscellaneous service\n"
2233 "System wide terminal length configuration\n"
2234 "Number of lines of VTY (0 means no line control)\n")
2238 host
.lines
= atoi(argv
[idx_number
]->arg
);
2243 DEFUN (no_service_terminal_length
,
2244 no_service_terminal_length_cmd
,
2245 "no service terminal-length [(0-512)]",
2247 "Set up miscellaneous service\n"
2248 "System wide terminal length configuration\n"
2249 "Number of lines of VTY (0 means no line control)\n")
2255 DEFUN_HIDDEN (do_echo
,
2258 "Echo a message back to the vty\n"
2259 "The message to echo\n")
2263 vty_out(vty
, "%s\n",
2264 ((message
= argv_concat(argv
, argc
, 1)) ? message
: ""));
2266 XFREE(MTYPE_TMP
, message
);
2270 DEFUN (config_logmsg
,
2272 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
2273 "Send a message to enabled logging destinations\n"
2275 "The message to send\n")
2277 int idx_log_level
= 1;
2278 int idx_message
= 2;
2282 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2283 return CMD_ERR_NO_MATCH
;
2286 ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
2288 XFREE(MTYPE_TMP
, message
);
2293 DEFUN (show_logging
,
2297 "Show current logging configuration\n")
2299 struct zlog
*zl
= zlog_default
;
2301 vty_out(vty
, "Syslog logging: ");
2302 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
2303 vty_out(vty
, "disabled");
2305 vty_out(vty
, "level %s, facility %s, ident %s",
2306 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
2307 facility_name(zl
->facility
), zl
->ident
);
2310 vty_out(vty
, "Stdout logging: ");
2311 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
2312 vty_out(vty
, "disabled");
2314 vty_out(vty
, "level %s",
2315 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
2318 vty_out(vty
, "Monitor logging: ");
2319 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
2320 vty_out(vty
, "disabled");
2322 vty_out(vty
, "level %s",
2323 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
2326 vty_out(vty
, "File logging: ");
2327 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) || !zl
->fp
)
2328 vty_out(vty
, "disabled");
2330 vty_out(vty
, "level %s, filename %s",
2331 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
2335 vty_out(vty
, "Protocol name: %s\n", zl
->protoname
);
2336 vty_out(vty
, "Record priority: %s\n",
2337 (zl
->record_priority
? "enabled" : "disabled"));
2338 vty_out(vty
, "Timestamp precision: %d\n", zl
->timestamp_precision
);
2343 DEFUN (config_log_stdout
,
2344 config_log_stdout_cmd
,
2345 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2347 "Set stdout logging level\n"
2350 int idx_log_level
= 2;
2352 if (argc
== idx_log_level
) {
2353 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2358 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2359 return CMD_ERR_NO_MATCH
;
2360 zlog_set_level(ZLOG_DEST_STDOUT
, level
);
2364 DEFUN (no_config_log_stdout
,
2365 no_config_log_stdout_cmd
,
2366 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2369 "Cancel logging to stdout\n"
2372 zlog_set_level(ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
2376 DEFUN (config_log_monitor
,
2377 config_log_monitor_cmd
,
2378 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2380 "Set terminal line (monitor) logging level\n"
2383 int idx_log_level
= 2;
2385 if (argc
== idx_log_level
) {
2386 zlog_set_level(ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
2391 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2392 return CMD_ERR_NO_MATCH
;
2393 zlog_set_level(ZLOG_DEST_MONITOR
, level
);
2397 DEFUN (no_config_log_monitor
,
2398 no_config_log_monitor_cmd
,
2399 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2402 "Disable terminal line (monitor) logging\n"
2405 zlog_set_level(ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
2409 static int set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
2413 const char *fullpath
;
2415 /* Path detection. */
2416 if (!IS_DIRECTORY_SEP(*fname
)) {
2417 char cwd
[MAXPATHLEN
+ 1];
2418 cwd
[MAXPATHLEN
] = '\0';
2420 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2421 flog_err_sys(LIB_ERR_SYSTEM_CALL
,
2422 "config_log_file: Unable to alloc mem!");
2423 return CMD_WARNING_CONFIG_FAILED
;
2426 p
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + strlen(fname
) + 2);
2427 sprintf(p
, "%s/%s", cwd
, fname
);
2432 ret
= zlog_set_file(fullpath
, loglevel
);
2435 XFREE(MTYPE_TMP
, p
);
2439 vty_out(vty
, "can't open logfile %s\n", fname
);
2440 return CMD_WARNING_CONFIG_FAILED
;
2444 XFREE(MTYPE_HOST
, host
.logfile
);
2446 host
.logfile
= XSTRDUP(MTYPE_HOST
, fname
);
2448 #if defined(HAVE_CUMULUS)
2449 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2450 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2455 void command_setup_early_logging(const char *dest
, const char *level
)
2460 int nlevel
= level_match(level
);
2462 if (nlevel
!= ZLOG_DISABLED
)
2463 zlog_default
->default_lvl
= nlevel
;
2469 if (strcmp(dest
, "stdout") == 0) {
2470 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2474 if (strcmp(dest
, "syslog") == 0) {
2475 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2479 token
= strstr(dest
, ":");
2485 set_log_file(NULL
, token
, zlog_default
->default_lvl
);
2488 DEFUN (config_log_file
,
2489 config_log_file_cmd
,
2490 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2493 "Logging filename\n"
2496 int idx_filename
= 2;
2497 int idx_log_levels
= 3;
2500 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2502 return CMD_ERR_NO_MATCH
;
2503 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2505 return set_log_file(vty
, argv
[idx_filename
]->arg
,
2506 zlog_default
->default_lvl
);
2509 static void disable_log_file(void)
2514 XFREE(MTYPE_HOST
, host
.logfile
);
2516 host
.logfile
= NULL
;
2519 DEFUN (no_config_log_file
,
2520 no_config_log_file_cmd
,
2521 "no log file [FILENAME [LEVEL]]",
2524 "Cancel logging to file\n"
2525 "Logging file name\n"
2532 DEFUN (config_log_syslog
,
2533 config_log_syslog_cmd
,
2534 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2536 "Set syslog logging level\n"
2539 int idx_log_levels
= 2;
2545 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2547 return CMD_ERR_NO_MATCH
;
2548 zlog_set_level(ZLOG_DEST_SYSLOG
, level
);
2551 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2556 DEFUN (no_config_log_syslog
,
2557 no_config_log_syslog_cmd
,
2558 "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>]",
2561 "Cancel logging to syslog\n"
2565 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2569 DEFUN (config_log_facility
,
2570 config_log_facility_cmd
,
2571 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2573 "Facility parameter for syslog messages\n"
2577 int facility
= facility_match(argv
[idx_target
]->arg
);
2579 zlog_default
->facility
= facility
;
2583 DEFUN (no_config_log_facility
,
2584 no_config_log_facility_cmd
,
2585 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2588 "Reset syslog facility to default (daemon)\n"
2591 zlog_default
->facility
= LOG_DAEMON
;
2595 DEFUN (config_log_record_priority
,
2596 config_log_record_priority_cmd
,
2597 "log record-priority",
2599 "Log the priority of the message within the message\n")
2601 zlog_default
->record_priority
= 1;
2605 DEFUN (no_config_log_record_priority
,
2606 no_config_log_record_priority_cmd
,
2607 "no log record-priority",
2610 "Do not log the priority of the message within the message\n")
2612 zlog_default
->record_priority
= 0;
2616 DEFUN (config_log_timestamp_precision
,
2617 config_log_timestamp_precision_cmd
,
2618 "log timestamp precision (0-6)",
2620 "Timestamp configuration\n"
2621 "Set the timestamp precision\n"
2622 "Number of subsecond digits\n")
2625 zlog_default
->timestamp_precision
=
2626 strtoul(argv
[idx_number
]->arg
, NULL
, 10);
2630 DEFUN (no_config_log_timestamp_precision
,
2631 no_config_log_timestamp_precision_cmd
,
2632 "no log timestamp precision",
2635 "Timestamp configuration\n"
2636 "Reset the timestamp precision to the default value of 0\n")
2638 zlog_default
->timestamp_precision
= 0;
2642 DEFUN (debug_memstats
,
2644 "[no] debug memstats-at-exit",
2647 "Print memory type statistics at exit\n")
2649 debug_memstats_at_exit
= !!strcmp(argv
[0]->text
, "no");
2653 int cmd_banner_motd_file(const char *file
)
2655 int success
= CMD_SUCCESS
;
2660 rpath
= realpath(file
, p
);
2662 return CMD_ERR_NO_FILE
;
2663 in
= strstr(rpath
, SYSCONFDIR
);
2666 XFREE(MTYPE_HOST
, host
.motdfile
);
2667 host
.motdfile
= XSTRDUP(MTYPE_HOST
, file
);
2669 success
= CMD_WARNING_CONFIG_FAILED
;
2674 DEFUN (banner_motd_file
,
2675 banner_motd_file_cmd
,
2676 "banner motd file FILE",
2679 "Banner from a file\n"
2683 const char *filename
= argv
[idx_file
]->arg
;
2684 int cmd
= cmd_banner_motd_file(filename
);
2686 if (cmd
== CMD_ERR_NO_FILE
)
2687 vty_out(vty
, "%s does not exist", filename
);
2688 else if (cmd
== CMD_WARNING_CONFIG_FAILED
)
2689 vty_out(vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2694 DEFUN (banner_motd_default
,
2695 banner_motd_default_cmd
,
2696 "banner motd default",
2697 "Set banner string\n"
2698 "Strings for motd\n"
2701 host
.motd
= default_motd
;
2705 DEFUN (no_banner_motd
,
2709 "Set banner string\n"
2710 "Strings for motd\n")
2714 XFREE(MTYPE_HOST
, host
.motdfile
);
2715 host
.motdfile
= NULL
;
2722 "Find CLI command containing text\n"
2723 "Text to search for\n")
2725 char *text
= argv_concat(argv
, argc
, 1);
2726 const struct cmd_node
*node
;
2727 const struct cmd_element
*cli
;
2730 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++) {
2731 node
= vector_slot(cmdvec
, i
);
2734 clis
= node
->cmd_vector
;
2735 for (unsigned int j
= 0; j
< vector_active(clis
); j
++) {
2736 cli
= vector_slot(clis
, j
);
2737 if (strcasestr(cli
->string
, text
))
2738 vty_out(vty
, " (%s) %s\n",
2739 node_names
[node
->node
], cli
->string
);
2743 XFREE(MTYPE_TMP
, text
);
2748 /* Set config filename. Called from vty.c */
2749 void host_config_set(const char *filename
)
2752 XFREE(MTYPE_HOST
, host
.config
);
2753 host
.config
= XSTRDUP(MTYPE_HOST
, filename
);
2756 const char *host_config_get(void)
2761 void install_default(enum node_type node
)
2763 install_element(node
, &config_exit_cmd
);
2764 install_element(node
, &config_quit_cmd
);
2765 install_element(node
, &config_end_cmd
);
2766 install_element(node
, &config_help_cmd
);
2767 install_element(node
, &config_list_cmd
);
2768 install_element(node
, &show_cli_graph_cmd
);
2769 install_element(node
, &find_cmd
);
2771 install_element(node
, &config_write_cmd
);
2772 install_element(node
, &show_running_config_cmd
);
2774 install_element(node
, &autocomplete_cmd
);
2777 /* Initialize command interface. Install basic nodes and commands.
2779 * terminal = 0 -- vtysh / no logging, no config control
2780 * terminal = 1 -- normal daemon
2781 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2782 void cmd_init(int terminal
)
2784 struct utsname names
;
2786 if (array_size(node_names
) != NODE_TYPE_MAX
)
2787 assert(!"Update the CLI node description array!");
2792 /* register command preprocessors */
2793 hook_register(cmd_execute
, handle_pipe_action
);
2794 hook_register(cmd_execute_done
, handle_pipe_action_done
);
2796 varhandlers
= list_new();
2798 /* Allocate initial top vector of commands. */
2799 cmdvec
= vector_init(VECTOR_MIN_SIZE
);
2801 /* Default host value settings. */
2802 host
.name
= XSTRDUP(MTYPE_HOST
, names
.nodename
);
2803 #ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
2804 if ((strcmp(names
.domainname
, "(none)") == 0))
2805 host
.domainname
= NULL
;
2807 host
.domainname
= XSTRDUP(MTYPE_HOST
, names
.domainname
);
2809 host
.domainname
= NULL
;
2811 host
.password
= NULL
;
2813 host
.logfile
= NULL
;
2815 host
.noconfig
= (terminal
< 0);
2817 host
.motd
= default_motd
;
2818 host
.motdfile
= NULL
;
2820 /* Install top nodes. */
2821 install_node(&view_node
, NULL
);
2822 install_node(&enable_node
, NULL
);
2823 install_node(&auth_node
, NULL
);
2824 install_node(&auth_enable_node
, NULL
);
2825 install_node(&config_node
, config_write_host
);
2827 /* Each node's basic commands. */
2828 install_element(VIEW_NODE
, &show_version_cmd
);
2829 install_element(ENABLE_NODE
, &show_startup_config_cmd
);
2830 install_element(ENABLE_NODE
, &debug_memstats_cmd
);
2833 install_element(VIEW_NODE
, &config_list_cmd
);
2834 install_element(VIEW_NODE
, &config_exit_cmd
);
2835 install_element(VIEW_NODE
, &config_quit_cmd
);
2836 install_element(VIEW_NODE
, &config_help_cmd
);
2837 install_element(VIEW_NODE
, &config_enable_cmd
);
2838 install_element(VIEW_NODE
, &config_terminal_length_cmd
);
2839 install_element(VIEW_NODE
, &config_terminal_no_length_cmd
);
2840 install_element(VIEW_NODE
, &show_logging_cmd
);
2841 install_element(VIEW_NODE
, &show_commandtree_cmd
);
2842 install_element(VIEW_NODE
, &echo_cmd
);
2843 install_element(VIEW_NODE
, &autocomplete_cmd
);
2844 install_element(VIEW_NODE
, &find_cmd
);
2846 install_element(ENABLE_NODE
, &config_end_cmd
);
2847 install_element(ENABLE_NODE
, &config_disable_cmd
);
2848 install_element(ENABLE_NODE
, &config_terminal_cmd
);
2849 install_element(ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2850 install_element(ENABLE_NODE
, &config_write_cmd
);
2851 install_element(ENABLE_NODE
, &show_running_config_cmd
);
2852 install_element(ENABLE_NODE
, &config_logmsg_cmd
);
2854 install_default(CONFIG_NODE
);
2857 workqueue_cmd_init();
2861 install_element(CONFIG_NODE
, &hostname_cmd
);
2862 install_element(CONFIG_NODE
, &no_hostname_cmd
);
2863 install_element(CONFIG_NODE
, &domainname_cmd
);
2864 install_element(CONFIG_NODE
, &no_domainname_cmd
);
2865 install_element(CONFIG_NODE
, &frr_version_defaults_cmd
);
2866 install_element(CONFIG_NODE
, &debug_memstats_cmd
);
2869 install_element(CONFIG_NODE
, &password_cmd
);
2870 install_element(CONFIG_NODE
, &no_password_cmd
);
2871 install_element(CONFIG_NODE
, &enable_password_cmd
);
2872 install_element(CONFIG_NODE
, &no_enable_password_cmd
);
2874 install_element(CONFIG_NODE
, &config_log_stdout_cmd
);
2875 install_element(CONFIG_NODE
, &no_config_log_stdout_cmd
);
2876 install_element(CONFIG_NODE
, &config_log_monitor_cmd
);
2877 install_element(CONFIG_NODE
, &no_config_log_monitor_cmd
);
2878 install_element(CONFIG_NODE
, &config_log_file_cmd
);
2879 install_element(CONFIG_NODE
, &no_config_log_file_cmd
);
2880 install_element(CONFIG_NODE
, &config_log_syslog_cmd
);
2881 install_element(CONFIG_NODE
, &no_config_log_syslog_cmd
);
2882 install_element(CONFIG_NODE
, &config_log_facility_cmd
);
2883 install_element(CONFIG_NODE
, &no_config_log_facility_cmd
);
2884 install_element(CONFIG_NODE
, &config_log_record_priority_cmd
);
2885 install_element(CONFIG_NODE
,
2886 &no_config_log_record_priority_cmd
);
2887 install_element(CONFIG_NODE
,
2888 &config_log_timestamp_precision_cmd
);
2889 install_element(CONFIG_NODE
,
2890 &no_config_log_timestamp_precision_cmd
);
2891 install_element(CONFIG_NODE
, &service_password_encrypt_cmd
);
2892 install_element(CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2893 install_element(CONFIG_NODE
, &banner_motd_default_cmd
);
2894 install_element(CONFIG_NODE
, &banner_motd_file_cmd
);
2895 install_element(CONFIG_NODE
, &no_banner_motd_cmd
);
2896 install_element(CONFIG_NODE
, &service_terminal_length_cmd
);
2897 install_element(CONFIG_NODE
, &no_service_terminal_length_cmd
);
2899 vrf_install_commands();
2903 grammar_sandbox_init();
2907 void cmd_terminate()
2909 struct cmd_node
*cmd_node
;
2911 hook_unregister(cmd_execute
, handle_pipe_action
);
2912 hook_unregister(cmd_execute_done
, handle_pipe_action_done
);
2915 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++)
2916 if ((cmd_node
= vector_slot(cmdvec
, i
)) != NULL
) {
2917 // deleting the graph delets the cmd_element as
2919 graph_delete_graph(cmd_node
->cmdgraph
);
2920 vector_free(cmd_node
->cmd_vector
);
2921 hash_clean(cmd_node
->cmd_hash
, NULL
);
2922 hash_free(cmd_node
->cmd_hash
);
2923 cmd_node
->cmd_hash
= NULL
;
2926 vector_free(cmdvec
);
2931 XFREE(MTYPE_HOST
, host
.name
);
2932 if (host
.domainname
)
2933 XFREE(MTYPE_HOST
, host
.domainname
);
2935 XFREE(MTYPE_HOST
, host
.password
);
2936 if (host
.password_encrypt
)
2937 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2939 XFREE(MTYPE_HOST
, host
.enable
);
2940 if (host
.enable_encrypt
)
2941 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2943 XFREE(MTYPE_HOST
, host
.logfile
);
2945 XFREE(MTYPE_HOST
, host
.motdfile
);
2947 XFREE(MTYPE_HOST
, host
.config
);
2949 list_delete_and_null(&varhandlers
);