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"
49 DEFINE_MTYPE(LIB
, HOST
, "Host config")
50 DEFINE_MTYPE(LIB
, COMPLETION
, "Completion item")
57 /* clang-format off */
58 const struct message tokennames
[] = {
63 item(IPV4_PREFIX_TKN
),
65 item(IPV6_PREFIX_TKN
),
75 const char *node_names
[] = {
78 "auth enable", // AUTH_ENABLE_NODE,
79 "enable", // ENABLE_NODE,
80 "config", // CONFIG_NODE,
81 "debug", // DEBUG_NODE,
82 "vrf debug", // VRF_DEBUG_NODE,
83 "vnc debug", // DEBUG_VNC_NODE,
85 "keychain", // KEYCHAIN_NODE,
86 "keychain key", // KEYCHAIN_KEY_NODE,
87 "logical-router", // LOGICALROUTER_NODE,
88 "static ip", // IP_NODE,
90 "interface", // INTERFACE_NODE,
91 "nexthop-group", // NH_GROUP_NODE,
92 "zebra", // ZEBRA_NODE,
93 "table", // TABLE_NODE,
95 "ripng", // RIPNG_NODE,
96 "babel", // BABEL_NODE,
97 "eigrp", // EIGRP_NODE,
99 "bgp vpnv4", // BGP_VPNV4_NODE,
100 "bgp vpnv6", // BGP_VPNV6_NODE,
101 "bgp ipv4 unicast", // BGP_IPV4_NODE,
102 "bgp ipv4 multicast", // BGP_IPV4M_NODE,
103 "bgp ipv4 labeled unicast", // BGP_IPV4L_NODE,
104 "bgp ipv6", // BGP_IPV6_NODE,
105 "bgp ipv6 multicast", // BGP_IPV6M_NODE,
106 "bgp ipv6 labeled unicast", // BGP_IPV6L_NODE,
107 "bgp vrf policy", // BGP_VRF_POLICY_NODE,
108 "bgp vnc defaults", // BGP_VNC_DEFAULTS_NODE,
109 "bgp vnc nve", // BGP_VNC_NVE_GROUP_NODE,
110 "bgp vnc l2", // BGP_VNC_L2_GROUP_NODE,
111 "rfp defaults", // RFP_DEFAULTS_NODE,
112 "bgp evpn", // BGP_EVPN_NODE,
113 "ospf", // OSPF_NODE,
114 "ospf6", // OSPF6_NODE,
116 "ldp ipv4", // LDP_IPV4_NODE,
117 "ldp ipv6", // LDP_IPV6_NODE,
118 "ldp ipv4 interface", // LDP_IPV4_IFACE_NODE,
119 "ldp ipv6 interface", // LDP_IPV6_IFACE_NODE,
120 "ldp l2vpn", // LDP_L2VPN_NODE,
121 "ldp", // LDP_PSEUDOWIRE_NODE,
122 "isis", // ISIS_NODE,
123 "ipv4 access list", // ACCESS_NODE,
124 "ipv4 prefix list", // PREFIX_NODE,
125 "ipv6 access list", // ACCESS_IPV6_NODE,
126 "MAC access list", // ACCESS_MAC_NODE,
127 "ipv6 prefix list", // PREFIX_IPV6_NODE,
128 "as list", // AS_LIST_NODE,
129 "community list", // COMMUNITY_LIST_NODE,
130 "routemap", // RMAP_NODE,
131 "pbr-map", // PBRMAP_NODE,
132 "smux", // SMUX_NODE,
133 "dump", // DUMP_NODE,
134 "forwarding", // FORWARDING_NODE,
135 "protocol", // PROTOCOL_NODE,
136 "mpls", // MPLS_NODE,
139 "link-params", // LINK_PARAMS_NODE,
140 "bgp evpn vni", // BGP_EVPN_VNI_NODE,
142 "bgp ipv4 flowspec", /* BGP_FLOWSPECV4_NODE
144 "bgp ipv6 flowspec", /* BGP_FLOWSPECV6_NODE
147 /* clang-format on */
149 /* Command vector which includes some level of command lists. Normally
150 each daemon maintains each own cmdvec. */
151 vector cmdvec
= NULL
;
153 /* Host information structure. */
157 * Returns host.name if any, otherwise
158 * it returns the system hostname.
160 const char *cmd_hostname_get(void)
166 * Returns unix domainname
168 const char *cmd_domainname_get(void)
170 return host
.domainname
;
173 /* Standard command node structures. */
174 static struct cmd_node auth_node
= {
175 AUTH_NODE
, "Password: ",
178 static struct cmd_node view_node
= {
182 static struct cmd_node auth_enable_node
= {
183 AUTH_ENABLE_NODE
, "Password: ",
186 static struct cmd_node enable_node
= {
190 static struct cmd_node config_node
= {CONFIG_NODE
, "%s(config)# ", 1};
192 /* Default motd string. */
193 static const char *default_motd
= FRR_DEFAULT_MOTD
;
195 static const struct facility_map
{
199 } syslog_facilities
[] = {
200 {LOG_KERN
, "kern", 1},
201 {LOG_USER
, "user", 2},
202 {LOG_MAIL
, "mail", 1},
203 {LOG_DAEMON
, "daemon", 1},
204 {LOG_AUTH
, "auth", 1},
205 {LOG_SYSLOG
, "syslog", 1},
207 {LOG_NEWS
, "news", 1},
208 {LOG_UUCP
, "uucp", 2},
209 {LOG_CRON
, "cron", 1},
213 {LOG_LOCAL0
, "local0", 6},
214 {LOG_LOCAL1
, "local1", 6},
215 {LOG_LOCAL2
, "local2", 6},
216 {LOG_LOCAL3
, "local3", 6},
217 {LOG_LOCAL4
, "local4", 6},
218 {LOG_LOCAL5
, "local5", 6},
219 {LOG_LOCAL6
, "local6", 6},
220 {LOG_LOCAL7
, "local7", 6},
224 static const char *facility_name(int facility
)
226 const struct facility_map
*fm
;
228 for (fm
= syslog_facilities
; fm
->name
; fm
++)
229 if (fm
->facility
== facility
)
234 static int facility_match(const char *str
)
236 const struct facility_map
*fm
;
238 for (fm
= syslog_facilities
; fm
->name
; fm
++)
239 if (!strncmp(str
, fm
->name
, fm
->match
))
244 static int level_match(const char *s
)
248 for (level
= 0; zlog_priority
[level
] != NULL
; level
++)
249 if (!strncmp(s
, zlog_priority
[level
], 2))
251 return ZLOG_DISABLED
;
254 /* This is called from main when a daemon is invoked with -v or --version. */
255 void print_version(const char *progname
)
257 printf("%s version %s\n", progname
, FRR_VERSION
);
258 printf("%s\n", FRR_COPYRIGHT
);
259 printf("configured with:\n\t%s\n", FRR_CONFIG_ARGS
);
262 char *argv_concat(struct cmd_token
**argv
, int argc
, int shift
)
264 int cnt
= MAX(argc
- shift
, 0);
265 const char *argstr
[cnt
+ 1];
270 for (int i
= 0; i
< cnt
; i
++)
271 argstr
[i
] = argv
[i
+ shift
]->arg
;
273 return frrstr_join(argstr
, cnt
, " ");
276 vector
cmd_make_strvec(const char *string
)
281 const char *copy
= string
;
283 /* skip leading whitespace */
284 while (isspace((int)*copy
) && *copy
!= '\0')
287 /* if the entire string was whitespace or a comment, return */
288 if (*copy
== '\0' || *copy
== '!' || *copy
== '#')
291 vector result
= frrstr_split_vec(copy
, "\n\r\t ");
293 for (unsigned int i
= 0; i
< vector_active(result
); i
++) {
294 if (strlen(vector_slot(result
, i
)) == 0) {
295 XFREE(MTYPE_TMP
, vector_slot(result
, i
));
296 vector_unset(result
, i
);
300 vector_compact(result
);
305 void cmd_free_strvec(vector v
)
307 frrstr_strvec_free(v
);
311 * Convenience function for accessing argv data.
315 * @param text definition snippet of the desired token
316 * @param index the starting index, and where to store the
317 * index of the found token if it exists
318 * @return 1 if found, 0 otherwise
320 int argv_find(struct cmd_token
**argv
, int argc
, const char *text
, int *index
)
323 for (int i
= *index
; i
< argc
&& found
== 0; i
++)
324 if ((found
= strmatch(text
, argv
[i
]->text
)))
329 static unsigned int cmd_hash_key(void *p
)
331 int size
= sizeof(p
);
333 return jhash(p
, size
, 0);
336 static int cmd_hash_cmp(const void *a
, const void *b
)
341 /* Install top node of command vector. */
342 void install_node(struct cmd_node
*node
, int (*func
)(struct vty
*))
344 vector_set_index(cmdvec
, node
->node
, node
);
346 node
->cmdgraph
= graph_new();
347 node
->cmd_vector
= vector_init(VECTOR_MIN_SIZE
);
349 struct cmd_token
*token
=
350 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
351 graph_new_node(node
->cmdgraph
, token
,
352 (void (*)(void *)) & cmd_token_del
);
353 node
->cmd_hash
= hash_create_size(16, cmd_hash_key
, cmd_hash_cmp
,
357 /* Return prompt character of specified node. */
358 const char *cmd_prompt(enum node_type node
)
360 struct cmd_node
*cnode
;
362 cnode
= vector_slot(cmdvec
, node
);
363 return cnode
->prompt
;
366 /* Install a command into a node. */
367 void install_element(enum node_type ntype
, struct cmd_element
*cmd
)
369 struct cmd_node
*cnode
;
371 /* cmd_init hasn't been called */
373 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
378 cnode
= vector_lookup(cmdvec
, ntype
);
383 "\tnode %d (%s) does not exist.\n"
384 "\tplease call install_node() before install_element()\n",
385 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
389 if (hash_lookup(cnode
->cmd_hash
, cmd
) != NULL
) {
392 "\tnode %d (%s) already has this command installed.\n"
393 "\tduplicate install_element call?\n",
394 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
398 assert(hash_get(cnode
->cmd_hash
, cmd
, hash_alloc_intern
));
400 struct graph
*graph
= graph_new();
401 struct cmd_token
*token
=
402 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
403 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
405 cmd_graph_parse(graph
, cmd
);
406 cmd_graph_names(graph
);
407 cmd_graph_merge(cnode
->cmdgraph
, graph
, +1);
408 graph_delete_graph(graph
);
410 vector_set(cnode
->cmd_vector
, cmd
);
412 if (ntype
== VIEW_NODE
)
413 install_element(ENABLE_NODE
, cmd
);
416 void uninstall_element(enum node_type ntype
, struct cmd_element
*cmd
)
418 struct cmd_node
*cnode
;
420 /* cmd_init hasn't been called */
422 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
427 cnode
= vector_lookup(cmdvec
, ntype
);
432 "\tnode %d (%s) does not exist.\n"
433 "\tplease call install_node() before uninstall_element()\n",
434 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
438 if (hash_release(cnode
->cmd_hash
, cmd
) == NULL
) {
441 "\tnode %d (%s) does not have this command installed.\n"
442 "\tduplicate uninstall_element call?\n",
443 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
447 vector_unset_value(cnode
->cmd_vector
, cmd
);
449 struct graph
*graph
= graph_new();
450 struct cmd_token
*token
=
451 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
452 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
454 cmd_graph_parse(graph
, cmd
);
455 cmd_graph_names(graph
);
456 cmd_graph_merge(cnode
->cmdgraph
, graph
, -1);
457 graph_delete_graph(graph
);
459 if (ntype
== VIEW_NODE
)
460 uninstall_element(ENABLE_NODE
, cmd
);
464 static const unsigned char itoa64
[] =
465 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
467 static void to64(char *s
, long v
, int n
)
470 *s
++ = itoa64
[v
& 0x3f];
475 static char *zencrypt(const char *passwd
)
479 char *crypt(const char *, const char *);
481 gettimeofday(&tv
, 0);
483 to64(&salt
[0], random(), 3);
484 to64(&salt
[3], tv
.tv_usec
, 3);
487 return crypt(passwd
, salt
);
490 /* This function write configuration of this host. */
491 static int config_write_host(struct vty
*vty
)
493 if (cmd_hostname_get())
494 vty_out(vty
, "hostname %s\n", cmd_hostname_get());
496 if (cmd_domainname_get())
497 vty_out(vty
, "domainname %s\n", cmd_domainname_get());
499 /* The following are all configuration commands that are not sent to
500 * watchfrr. For instance watchfrr is hardcoded to log to syslog so
501 * we would always display 'log syslog informational' in the config
502 * which would cause other daemons to then switch to syslog when they
505 if (strcmp(zlog_default
->protoname
, "WATCHFRR")) {
507 if (host
.password_encrypt
)
508 vty_out(vty
, "password 8 %s\n",
509 host
.password_encrypt
);
510 if (host
.enable_encrypt
)
511 vty_out(vty
, "enable password 8 %s\n",
512 host
.enable_encrypt
);
515 vty_out(vty
, "password %s\n", host
.password
);
517 vty_out(vty
, "enable password %s\n",
522 && (zlog_default
->maxlvl
[ZLOG_DEST_FILE
]
524 vty_out(vty
, "log file %s", host
.logfile
);
525 if (zlog_default
->maxlvl
[ZLOG_DEST_FILE
]
526 != zlog_default
->default_lvl
)
529 [zlog_default
->maxlvl
534 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != ZLOG_DISABLED
) {
535 vty_out(vty
, "log stdout");
536 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
]
537 != zlog_default
->default_lvl
)
540 [zlog_default
->maxlvl
541 [ZLOG_DEST_STDOUT
]]);
545 if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
546 vty_out(vty
, "no log monitor\n");
547 else if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]
548 != zlog_default
->default_lvl
)
549 vty_out(vty
, "log monitor %s\n",
550 zlog_priority
[zlog_default
->maxlvl
551 [ZLOG_DEST_MONITOR
]]);
553 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
) {
554 vty_out(vty
, "log syslog");
555 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
]
556 != zlog_default
->default_lvl
)
558 zlog_priority
[zlog_default
->maxlvl
559 [ZLOG_DEST_SYSLOG
]]);
563 if (zlog_default
->facility
!= LOG_DAEMON
)
564 vty_out(vty
, "log facility %s\n",
565 facility_name(zlog_default
->facility
));
567 if (zlog_default
->record_priority
== 1)
568 vty_out(vty
, "log record-priority\n");
570 if (zlog_default
->timestamp_precision
> 0)
571 vty_out(vty
, "log timestamp precision %d\n",
572 zlog_default
->timestamp_precision
);
575 vty_out(vty
, "service advanced-vty\n");
578 vty_out(vty
, "service password-encryption\n");
581 vty_out(vty
, "service terminal-length %d\n",
585 vty_out(vty
, "banner motd file %s\n", host
.motdfile
);
587 vty_out(vty
, "no banner motd\n");
590 if (debug_memstats_at_exit
)
591 vty_out(vty
, "!\ndebug memstats-at-exit\n");
596 /* Utility function for getting command graph. */
597 static struct graph
*cmd_node_graph(vector v
, enum node_type ntype
)
599 struct cmd_node
*cnode
= vector_slot(v
, ntype
);
600 return cnode
->cmdgraph
;
603 static int cmd_try_do_shortcut(enum node_type node
, char *first_word
)
605 if (first_word
!= NULL
&& node
!= AUTH_NODE
&& node
!= VIEW_NODE
606 && node
!= AUTH_ENABLE_NODE
&& 0 == strcmp("do", first_word
))
612 * Compare function for cmd_token.
613 * Used with qsort to sort command completions.
615 static int compare_completions(const void *fst
, const void *snd
)
617 struct cmd_token
*first
= *(struct cmd_token
**)fst
,
618 *secnd
= *(struct cmd_token
**)snd
;
619 return strcmp(first
->text
, secnd
->text
);
623 * Takes a list of completions returned by command_complete,
624 * dedeuplicates them based on both text and description,
625 * sorts them, and returns them as a vector.
627 * @param completions linked list of cmd_token
628 * @return deduplicated and sorted vector with
630 vector
completions_to_vec(struct list
*completions
)
632 vector comps
= vector_init(VECTOR_MIN_SIZE
);
635 struct cmd_token
*token
, *cr
= NULL
;
636 unsigned int i
, exists
;
637 for (ALL_LIST_ELEMENTS_RO(completions
, ln
, token
)) {
638 if (token
->type
== END_TKN
&& (cr
= token
))
641 // linear search for token in completions vector
643 for (i
= 0; i
< vector_active(comps
) && !exists
; i
++) {
644 struct cmd_token
*curr
= vector_slot(comps
, i
);
646 exists
= !strcmp(curr
->text
, token
->text
)
647 && !strcmp(curr
->desc
, token
->desc
);
649 exists
= !strcmp(curr
->text
, token
->text
);
650 #endif /* VTYSH_DEBUG */
654 vector_set(comps
, token
);
658 qsort(comps
->index
, vector_active(comps
), sizeof(void *),
659 &compare_completions
);
661 // make <cr> the first element, if it is present
663 vector_set_index(comps
, vector_active(comps
), NULL
);
664 memmove(comps
->index
+ 1, comps
->index
,
665 (comps
->alloced
- 1) * sizeof(void *));
666 vector_set_index(comps
, 0, cr
);
672 * Generates a vector of cmd_token representing possible completions
673 * on the current input.
675 * @param vline the vectorized input line
676 * @param vty the vty with the node to match on
677 * @param status pointer to matcher status code
678 * @return vector of struct cmd_token * with possible completions
680 static vector
cmd_complete_command_real(vector vline
, struct vty
*vty
,
683 struct list
*completions
;
684 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
686 enum matcher_rv rv
= command_complete(cmdgraph
, vline
, &completions
);
688 if (MATCHER_ERROR(rv
)) {
689 *status
= CMD_ERR_NO_MATCH
;
693 vector comps
= completions_to_vec(completions
);
694 list_delete_and_null(&completions
);
696 // set status code appropriately
697 switch (vector_active(comps
)) {
699 *status
= CMD_ERR_NO_MATCH
;
702 *status
= CMD_COMPLETE_FULL_MATCH
;
705 *status
= CMD_COMPLETE_LIST_MATCH
;
711 vector
cmd_describe_command(vector vline
, struct vty
*vty
, int *status
)
715 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
716 enum node_type onode
;
717 vector shifted_vline
;
721 vty
->node
= ENABLE_NODE
;
722 /* We can try it on enable node, cos' the vty is authenticated
725 shifted_vline
= vector_init(vector_count(vline
));
727 for (index
= 1; index
< vector_active(vline
); index
++) {
728 vector_set_index(shifted_vline
, index
- 1,
729 vector_lookup(vline
, index
));
732 ret
= cmd_complete_command_real(shifted_vline
, vty
, status
);
734 vector_free(shifted_vline
);
739 return cmd_complete_command_real(vline
, vty
, status
);
742 static struct list
*varhandlers
= NULL
;
744 void cmd_variable_complete(struct cmd_token
*token
, const char *arg
,
748 const struct cmd_variable_handler
*cvh
;
752 tmpcomps
= arg
? vector_init(VECTOR_MIN_SIZE
) : comps
;
754 for (ALL_LIST_ELEMENTS_RO(varhandlers
, ln
, cvh
)) {
755 if (cvh
->tokenname
&& strcmp(cvh
->tokenname
, token
->text
))
757 if (cvh
->varname
&& (!token
->varname
758 || strcmp(cvh
->varname
, token
->varname
)))
760 cvh
->completions(tmpcomps
, token
);
768 for (i
= vector_active(tmpcomps
); i
; i
--) {
769 char *item
= vector_slot(tmpcomps
, i
- 1);
770 if (strlen(item
) >= argsz
&& !strncmp(item
, arg
, argsz
))
771 vector_set(comps
, item
);
773 XFREE(MTYPE_COMPLETION
, item
);
775 vector_free(tmpcomps
);
778 #define AUTOCOMP_INDENT 5
780 char *cmd_variable_comp2str(vector comps
, unsigned short cols
)
783 char *buf
= XCALLOC(MTYPE_TMP
, bsz
);
784 int lc
= AUTOCOMP_INDENT
;
785 size_t cs
= AUTOCOMP_INDENT
;
787 snprintf(buf
, bsz
, "%*s", AUTOCOMP_INDENT
, "");
788 for (size_t j
= 0; j
< vector_active(comps
); j
++) {
789 char *item
= vector_slot(comps
, j
);
790 itemlen
= strlen(item
);
792 if (cs
+ itemlen
+ AUTOCOMP_INDENT
+ 3 >= bsz
)
793 buf
= XREALLOC(MTYPE_TMP
, buf
, (bsz
*= 2));
795 if (lc
+ itemlen
+ 1 >= cols
) {
796 cs
+= snprintf(&buf
[cs
], bsz
- cs
, "\n%*s",
797 AUTOCOMP_INDENT
, "");
798 lc
= AUTOCOMP_INDENT
;
801 size_t written
= snprintf(&buf
[cs
], bsz
- cs
, "%s ", item
);
804 XFREE(MTYPE_COMPLETION
, item
);
805 vector_set_index(comps
, j
, NULL
);
810 void cmd_variable_handler_register(const struct cmd_variable_handler
*cvh
)
815 for (; cvh
->completions
; cvh
++)
816 listnode_add(varhandlers
, (void *)cvh
);
819 DEFUN_HIDDEN (autocomplete
,
821 "autocomplete TYPE TEXT VARNAME",
822 "Autocompletion handler (internal, for vtysh)\n"
825 "cmd_token->varname\n")
827 struct cmd_token tok
;
828 vector comps
= vector_init(32);
831 memset(&tok
, 0, sizeof(tok
));
832 tok
.type
= atoi(argv
[1]->arg
);
833 tok
.text
= argv
[2]->arg
;
834 tok
.varname
= argv
[3]->arg
;
835 if (!strcmp(tok
.varname
, "-"))
838 cmd_variable_complete(&tok
, NULL
, comps
);
840 for (i
= 0; i
< vector_active(comps
); i
++) {
841 char *text
= vector_slot(comps
, i
);
842 vty_out(vty
, "%s\n", text
);
843 XFREE(MTYPE_COMPLETION
, text
);
851 * Generate possible tab-completions for the given input. This function only
852 * returns results that would result in a valid command if used as Readline
853 * completions (as is the case in vtysh). For instance, if the passed vline ends
854 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
856 * @param vline vectorized input line
858 * @param status location to store matcher status code in
859 * @return set of valid strings for use with Readline as tab-completions.
862 char **cmd_complete_command(vector vline
, struct vty
*vty
, int *status
)
865 int original_node
= vty
->node
;
866 vector input_line
= vector_init(vector_count(vline
));
868 // if the first token is 'do' we'll want to execute the command in the
870 int do_shortcut
= cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0));
871 vty
->node
= do_shortcut
? ENABLE_NODE
: original_node
;
873 // construct the input line we'll be matching on
874 unsigned int offset
= (do_shortcut
) ? 1 : 0;
875 for (unsigned index
= 0; index
+ offset
< vector_active(vline
); index
++)
876 vector_set_index(input_line
, index
,
877 vector_lookup(vline
, index
+ offset
));
879 // get token completions -- this is a copying operation
880 vector comps
= NULL
, initial_comps
;
881 initial_comps
= cmd_complete_command_real(input_line
, vty
, status
);
883 if (!MATCHER_ERROR(*status
)) {
884 assert(initial_comps
);
885 // filter out everything that is not suitable for a
887 comps
= vector_init(VECTOR_MIN_SIZE
);
888 for (unsigned int i
= 0; i
< vector_active(initial_comps
);
890 struct cmd_token
*token
= vector_slot(initial_comps
, i
);
891 if (token
->type
== WORD_TKN
)
892 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
,
894 else if (IS_VARYING_TOKEN(token
->type
)) {
895 const char *ref
= vector_lookup(
896 vline
, vector_active(vline
) - 1);
897 cmd_variable_complete(token
, ref
, comps
);
900 vector_free(initial_comps
);
902 // since we filtered results, we need to re-set status code
903 switch (vector_active(comps
)) {
905 *status
= CMD_ERR_NO_MATCH
;
908 *status
= CMD_COMPLETE_FULL_MATCH
;
911 *status
= CMD_COMPLETE_LIST_MATCH
;
914 // copy completions text into an array of char*
915 ret
= XMALLOC(MTYPE_TMP
,
916 (vector_active(comps
) + 1) * sizeof(char *));
918 for (i
= 0; i
< vector_active(comps
); i
++) {
919 ret
[i
] = vector_slot(comps
, i
);
921 // set the last element to NULL, because this array is used in
922 // a Readline completion_generator function which expects NULL
923 // as a sentinel value
927 } else if (initial_comps
)
928 vector_free(initial_comps
);
930 // comps should always be null here
933 // free the adjusted input line
934 vector_free(input_line
);
936 // reset vty->node to its original value
937 vty
->node
= original_node
;
942 /* return parent node */
943 /* MUST eventually converge on CONFIG_NODE */
944 enum node_type
node_parent(enum node_type node
)
948 assert(node
> CONFIG_NODE
);
953 case BGP_FLOWSPECV4_NODE
:
954 case BGP_FLOWSPECV6_NODE
:
955 case BGP_VRF_POLICY_NODE
:
956 case BGP_VNC_DEFAULTS_NODE
:
957 case BGP_VNC_NVE_GROUP_NODE
:
958 case BGP_VNC_L2_GROUP_NODE
:
968 case BGP_EVPN_VNI_NODE
:
971 case KEYCHAIN_KEY_NODE
:
974 case LINK_PARAMS_NODE
:
975 ret
= INTERFACE_NODE
;
981 case LDP_IPV4_IFACE_NODE
:
984 case LDP_IPV6_IFACE_NODE
:
987 case LDP_PSEUDOWIRE_NODE
:
988 ret
= LDP_L2VPN_NODE
;
998 /* Execute command by argument vline vector. */
999 static int cmd_execute_command_real(vector vline
, enum filter_type filter
,
1001 const struct cmd_element
**cmd
)
1003 struct list
*argv_list
;
1004 enum matcher_rv status
;
1005 const struct cmd_element
*matched_element
= NULL
;
1007 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
1008 status
= command_match(cmdgraph
, vline
, &argv_list
, &matched_element
);
1011 *cmd
= matched_element
;
1013 // if matcher error, return corresponding CMD_ERR
1014 if (MATCHER_ERROR(status
)) {
1016 list_delete_and_null(&argv_list
);
1018 case MATCHER_INCOMPLETE
:
1019 return CMD_ERR_INCOMPLETE
;
1020 case MATCHER_AMBIGUOUS
:
1021 return CMD_ERR_AMBIGUOUS
;
1023 return CMD_ERR_NO_MATCH
;
1027 // build argv array from argv list
1028 struct cmd_token
**argv
= XMALLOC(
1029 MTYPE_TMP
, argv_list
->count
* sizeof(struct cmd_token
*));
1030 struct listnode
*ln
;
1031 struct cmd_token
*token
;
1033 for (ALL_LIST_ELEMENTS_RO(argv_list
, ln
, token
))
1036 int argc
= argv_list
->count
;
1039 if (matched_element
->daemon
)
1040 ret
= CMD_SUCCESS_DAEMON
;
1042 ret
= matched_element
->func(matched_element
, vty
, argc
, argv
);
1044 // delete list and cmd_token's in it
1045 list_delete_and_null(&argv_list
);
1046 XFREE(MTYPE_TMP
, argv
);
1052 * Execute a given command, handling things like "do ..." and checking
1053 * whether the given command might apply at a parent node if doesn't
1054 * apply for the current node.
1056 * @param vline Command line input, vector of char* where each element is
1058 * @param vty The vty context in which the command should be executed.
1059 * @param cmd Pointer where the struct cmd_element of the matched command
1060 * will be stored, if any. May be set to NULL if this info is
1062 * @param vtysh If set != 0, don't lookup the command at parent nodes.
1063 * @return The status of the command that has been executed or an error code
1064 * as to why no command could be executed.
1066 int cmd_execute_command(vector vline
, struct vty
*vty
,
1067 const struct cmd_element
**cmd
, int vtysh
)
1069 int ret
, saved_ret
= 0;
1070 enum node_type onode
, try_node
;
1072 onode
= try_node
= vty
->node
;
1074 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
1075 vector shifted_vline
;
1078 vty
->node
= ENABLE_NODE
;
1079 /* We can try it on enable node, cos' the vty is authenticated
1082 shifted_vline
= vector_init(vector_count(vline
));
1084 for (index
= 1; index
< vector_active(vline
); index
++)
1085 vector_set_index(shifted_vline
, index
- 1,
1086 vector_lookup(vline
, index
));
1088 ret
= cmd_execute_command_real(shifted_vline
, FILTER_RELAXED
,
1091 vector_free(shifted_vline
);
1097 cmd_execute_command_real(vline
, FILTER_RELAXED
, vty
, cmd
);
1102 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1103 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
) {
1104 /* This assumes all nodes above CONFIG_NODE are childs of
1106 while (vty
->node
> CONFIG_NODE
) {
1107 try_node
= node_parent(try_node
);
1108 vty
->node
= try_node
;
1109 ret
= cmd_execute_command_real(vline
, FILTER_RELAXED
,
1111 if (ret
== CMD_SUCCESS
|| ret
== CMD_WARNING
1112 || ret
== CMD_NOT_MY_INSTANCE
1113 || ret
== CMD_WARNING_CONFIG_FAILED
)
1116 /* no command succeeded, reset the vty to the original node */
1120 /* return command status for original node */
1125 * Execute a given command, matching it strictly against the current node.
1126 * This mode is used when reading config files.
1128 * @param vline Command line input, vector of char* where each element is
1130 * @param vty The vty context in which the command should be executed.
1131 * @param cmd Pointer where the struct cmd_element* of the matched command
1132 * will be stored, if any. May be set to NULL if this info is
1134 * @return The status of the command that has been executed or an error code
1135 * as to why no command could be executed.
1137 int cmd_execute_command_strict(vector vline
, struct vty
*vty
,
1138 const struct cmd_element
**cmd
)
1140 return cmd_execute_command_real(vline
, FILTER_STRICT
, vty
, cmd
);
1144 * Hook for preprocessing command string before executing.
1146 * All subscribers are called with the raw command string that is to be
1147 * executed. If any changes are to be made, a new string should be allocated
1148 * with MTYPE_TMP and *cmd_out updated to point to this new string. The caller
1149 * is then responsible for freeing this string.
1151 * All processing functions must be mutually exclusive in their action, i.e. if
1152 * one subscriber decides to modify the command, all others must not modify it
1153 * when called. Feeding the output of one processing command into a subsequent
1154 * one is not supported.
1156 * This hook is intentionally internal to the command processing system.
1159 * The raw command string.
1162 * The result of any processing.
1164 DECLARE_HOOK(cmd_execute
,
1165 (struct vty
*vty
, const char *cmd_in
, char **cmd_out
),
1166 (vty
, cmd_in
, cmd_out
));
1167 DEFINE_HOOK(cmd_execute
, (struct vty
*vty
, const char *cmd_in
, char **cmd_out
),
1168 (vty
, cmd_in
, cmd_out
));
1170 /* Hook executed after a CLI command. */
1171 DECLARE_KOOH(cmd_execute_done
, (struct vty
*vty
, const char *cmd_exec
),
1173 DEFINE_KOOH(cmd_execute_done
, (struct vty
*vty
, const char *cmd_exec
),
1177 * cmd_execute hook subscriber to handle `|` actions.
1179 static int handle_pipe_action(struct vty
*vty
, const char *cmd_in
,
1183 char *orig
, *working
, *token
, *u
;
1184 char *pipe
= strstr(cmd_in
, "| ");
1189 /* duplicate string for processing purposes, not including pipe */
1190 orig
= working
= XSTRDUP(MTYPE_TMP
, pipe
+ 2);
1192 /* retrieve action */
1193 token
= strsep(&working
, " ");
1195 /* match result to known actions */
1196 if (strmatch(token
, "include")) {
1197 /* the remaining text should be a regexp */
1198 char *regexp
= working
;
1201 vty_out(vty
, "%% Need a regexp to filter with\n");
1205 bool succ
= vty_set_include(vty
, regexp
);
1208 vty_out(vty
, "%% Bad regexp '%s'\n", regexp
);
1211 *cmd_out
= XSTRDUP(MTYPE_TMP
, cmd_in
);
1215 vty_out(vty
, "%% Unknown action '%s'\n", token
);
1220 XFREE(MTYPE_TMP
, orig
);
1224 static int handle_pipe_action_done(struct vty
*vty
, const char *cmd_exec
)
1227 vty_set_include(vty
, NULL
);
1232 int cmd_execute(struct vty
*vty
, const char *cmd
,
1233 const struct cmd_element
**matched
, int vtysh
)
1236 char *cmd_out
= NULL
;
1237 const char *cmd_exec
;
1240 hook_call(cmd_execute
, vty
, cmd
, &cmd_out
);
1241 cmd_exec
= cmd_out
? (const char *)cmd_out
: cmd
;
1243 vline
= cmd_make_strvec(cmd_exec
);
1246 ret
= cmd_execute_command(vline
, vty
, matched
, vtysh
);
1247 cmd_free_strvec(vline
);
1252 hook_call(cmd_execute_done
, vty
, cmd_exec
);
1255 XFREE(MTYPE_TMP
, cmd_out
);
1262 * Parse one line of config, walking up the parse tree attempting to find a
1265 * @param vty The vty context in which the command should be executed.
1266 * @param cmd Pointer where the struct cmd_element* of the match command
1267 * will be stored, if any. May be set to NULL if this info is
1269 * @param use_daemon Boolean to control whether or not we match on
1270 * CMD_SUCCESS_DAEMON
1272 * @return The status of the command that has been executed or an error code
1273 * as to why no command could be executed.
1275 int command_config_read_one_line(struct vty
*vty
,
1276 const struct cmd_element
**cmd
, int use_daemon
)
1282 vline
= cmd_make_strvec(vty
->buf
);
1284 /* In case of comment line */
1288 /* Execute configuration command : this is strict match */
1289 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1291 // Climb the tree and try the command again at each node
1292 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1293 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1294 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1295 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
1296 && vty
->node
!= CONFIG_NODE
) {
1298 saved_node
= vty
->node
;
1300 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1301 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1302 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1303 && vty
->node
> CONFIG_NODE
) {
1304 vty
->node
= node_parent(vty
->node
);
1305 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1308 // If climbing the tree did not work then ignore the command and
1309 // stay at the same node
1310 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1311 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1312 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
) {
1313 vty
->node
= saved_node
;
1317 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1318 memcpy(vty
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
1320 cmd_free_strvec(vline
);
1325 /* Configuration make from file. */
1326 int config_from_file(struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
1328 int ret
, error_ret
= 0;
1331 while (fgets(vty
->buf
, VTY_BUFSIZ
, fp
)) {
1335 ret
= command_config_read_one_line(vty
, NULL
, 0);
1337 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1338 && ret
!= CMD_ERR_NOTHING_TODO
)
1349 /* Configuration from terminal */
1350 DEFUN (config_terminal
,
1351 config_terminal_cmd
,
1352 "configure terminal",
1353 "Configuration from vty interface\n"
1354 "Configuration terminal\n")
1356 if (vty_config_lock(vty
))
1357 vty
->node
= CONFIG_NODE
;
1359 vty_out(vty
, "VTY configuration is locked by other VTY\n");
1360 return CMD_WARNING_CONFIG_FAILED
;
1365 /* Enable command */
1369 "Turn on privileged mode command\n")
1371 /* If enable password is NULL, change to ENABLE_NODE */
1372 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
)
1373 || vty
->type
== VTY_SHELL_SERV
)
1374 vty
->node
= ENABLE_NODE
;
1376 vty
->node
= AUTH_ENABLE_NODE
;
1381 /* Disable command */
1385 "Turn off privileged mode command\n")
1387 if (vty
->node
== ENABLE_NODE
)
1388 vty
->node
= VIEW_NODE
;
1392 /* Down vty node level. */
1396 "Exit current mode and down to previous mode\n")
1402 void cmd_exit(struct vty
*vty
)
1404 switch (vty
->node
) {
1410 vty
->status
= VTY_CLOSE
;
1413 vty
->node
= ENABLE_NODE
;
1414 vty_config_unlock(vty
);
1416 case INTERFACE_NODE
:
1418 case LOGICALROUTER_NODE
:
1430 case LDP_L2VPN_NODE
:
1436 vty
->node
= CONFIG_NODE
;
1439 case BGP_IPV4M_NODE
:
1440 case BGP_IPV4L_NODE
:
1441 case BGP_VPNV4_NODE
:
1442 case BGP_VPNV6_NODE
:
1443 case BGP_FLOWSPECV4_NODE
:
1444 case BGP_FLOWSPECV6_NODE
:
1445 case BGP_VRF_POLICY_NODE
:
1446 case BGP_VNC_DEFAULTS_NODE
:
1447 case BGP_VNC_NVE_GROUP_NODE
:
1448 case BGP_VNC_L2_GROUP_NODE
:
1450 case BGP_IPV6M_NODE
:
1452 case BGP_IPV6L_NODE
:
1453 vty
->node
= BGP_NODE
;
1455 case BGP_EVPN_VNI_NODE
:
1456 vty
->node
= BGP_EVPN_NODE
;
1460 vty
->node
= LDP_NODE
;
1462 case LDP_IPV4_IFACE_NODE
:
1463 vty
->node
= LDP_IPV4_NODE
;
1465 case LDP_IPV6_IFACE_NODE
:
1466 vty
->node
= LDP_IPV6_NODE
;
1468 case LDP_PSEUDOWIRE_NODE
:
1469 vty
->node
= LDP_L2VPN_NODE
;
1471 case KEYCHAIN_KEY_NODE
:
1472 vty
->node
= KEYCHAIN_NODE
;
1474 case LINK_PARAMS_NODE
:
1475 vty
->node
= INTERFACE_NODE
;
1486 "Exit current mode and down to previous mode\n")
1488 return config_exit(self
, vty
, argc
, argv
);
1492 /* End of configuration. */
1496 "End current mode and change to enable mode.\n")
1498 switch (vty
->node
) {
1501 /* Nothing to do. */
1504 case INTERFACE_NODE
:
1506 case LOGICALROUTER_NODE
:
1515 case BGP_VRF_POLICY_NODE
:
1516 case BGP_VNC_DEFAULTS_NODE
:
1517 case BGP_VNC_NVE_GROUP_NODE
:
1518 case BGP_VNC_L2_GROUP_NODE
:
1519 case BGP_VPNV4_NODE
:
1520 case BGP_VPNV6_NODE
:
1521 case BGP_FLOWSPECV4_NODE
:
1522 case BGP_FLOWSPECV6_NODE
:
1524 case BGP_IPV4M_NODE
:
1525 case BGP_IPV4L_NODE
:
1527 case BGP_IPV6M_NODE
:
1529 case BGP_EVPN_VNI_NODE
:
1530 case BGP_IPV6L_NODE
:
1538 case LDP_IPV4_IFACE_NODE
:
1539 case LDP_IPV6_IFACE_NODE
:
1540 case LDP_L2VPN_NODE
:
1541 case LDP_PSEUDOWIRE_NODE
:
1544 case KEYCHAIN_KEY_NODE
:
1546 case LINK_PARAMS_NODE
:
1547 vty_config_unlock(vty
);
1548 vty
->node
= ENABLE_NODE
;
1557 DEFUN (show_version
,
1561 "Displays zebra version\n")
1563 vty_out(vty
, "%s %s (%s).\n", FRR_FULL_NAME
, FRR_VERSION
,
1564 cmd_hostname_get() ? cmd_hostname_get() : "");
1565 vty_out(vty
, "%s%s\n", FRR_COPYRIGHT
, GIT_INFO
);
1566 vty_out(vty
, "configured with:\n %s\n", FRR_CONFIG_ARGS
);
1571 /* "Set" version ... ignore version tags */
1572 DEFUN (frr_version_defaults
,
1573 frr_version_defaults_cmd
,
1574 "frr <version|defaults> LINE...",
1575 "FRRouting global parameters\n"
1576 "version configuration was written by\n"
1577 "set of configuration defaults used\n"
1583 /* Help display function for all node. */
1587 "Description of the interactive help system\n")
1590 "Quagga VTY provides advanced help feature. When you need help,\n\
1591 anytime at the command line please press '?'.\n\
1593 If nothing matches, the help list will be empty and you must backup\n\
1594 until entering a '?' shows the available options.\n\
1595 Two styles of help are provided:\n\
1596 1. Full help is available when you are ready to enter a\n\
1597 command argument (e.g. 'show ?') and describes each possible\n\
1599 2. Partial help is provided when an abbreviated argument is entered\n\
1600 and you want to know what arguments match the input\n\
1601 (e.g. 'show me?'.)\n\n");
1605 static void permute(struct graph_node
*start
, struct vty
*vty
)
1607 static struct list
*position
= NULL
;
1609 position
= list_new();
1611 struct cmd_token
*stok
= start
->data
;
1612 struct graph_node
*gnn
;
1613 struct listnode
*ln
;
1616 listnode_add(position
, start
);
1617 for (unsigned int i
= 0; i
< vector_active(start
->to
); i
++) {
1618 struct graph_node
*gn
= vector_slot(start
->to
, i
);
1619 struct cmd_token
*tok
= gn
->data
;
1620 if (tok
->attr
== CMD_ATTR_HIDDEN
1621 || tok
->attr
== CMD_ATTR_DEPRECATED
)
1623 else if (tok
->type
== END_TKN
|| gn
== start
) {
1625 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
)) {
1626 struct cmd_token
*tt
= gnn
->data
;
1627 if (tt
->type
< SPECIAL_TKN
)
1628 vty_out(vty
, " %s", tt
->text
);
1631 vty_out(vty
, "...");
1635 if (stok
->type
== FORK_TKN
&& tok
->type
!= FORK_TKN
)
1636 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
))
1645 list_delete_node(position
, listtail(position
));
1648 int cmd_list_cmds(struct vty
*vty
, int do_permute
)
1650 struct cmd_node
*node
= vector_slot(cmdvec
, vty
->node
);
1653 permute(vector_slot(node
->cmdgraph
->nodes
, 0), vty
);
1655 /* loop over all commands at this node */
1656 struct cmd_element
*element
= NULL
;
1657 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
);
1659 if ((element
= vector_slot(node
->cmd_vector
, i
))
1660 && element
->attr
!= CMD_ATTR_DEPRECATED
1661 && element
->attr
!= CMD_ATTR_HIDDEN
)
1662 vty_out(vty
, " %s\n", element
->string
);
1667 /* Help display function for all node. */
1670 "list [permutations]",
1671 "Print command list\n"
1672 "Print all possible command permutations\n")
1674 return cmd_list_cmds(vty
, argc
== 2);
1677 DEFUN (show_commandtree
,
1678 show_commandtree_cmd
,
1679 "show commandtree [permutations]",
1681 "Show command tree\n"
1682 "Permutations that we are interested in\n")
1684 return cmd_list_cmds(vty
, argc
== 3);
1687 DEFUN_HIDDEN(show_cli_graph
,
1692 "Dump current command space as DOT graph\n")
1694 struct cmd_node
*cn
= vector_slot(cmdvec
, vty
->node
);
1695 char *dot
= cmd_graph_dump_dot(cn
->cmdgraph
);
1697 vty_out(vty
, "%s\n", dot
);
1698 XFREE(MTYPE_TMP
, dot
);
1702 static int vty_write_config(struct vty
*vty
)
1705 struct cmd_node
*node
;
1710 if (vty
->type
== VTY_TERM
) {
1711 vty_out(vty
, "\nCurrent configuration:\n");
1712 vty_out(vty
, "!\n");
1715 vty_out(vty
, "frr version %s\n", FRR_VER_SHORT
);
1716 vty_out(vty
, "frr defaults %s\n", DFLT_NAME
);
1717 vty_out(vty
, "!\n");
1719 for (i
= 0; i
< vector_active(cmdvec
); i
++)
1720 if ((node
= vector_slot(cmdvec
, i
)) && node
->func
1721 && (node
->vtysh
|| vty
->type
!= VTY_SHELL
)) {
1722 if ((*node
->func
)(vty
))
1723 vty_out(vty
, "!\n");
1726 if (vty
->type
== VTY_TERM
) {
1727 vty_out(vty
, "end\n");
1733 static int file_write_config(struct vty
*vty
)
1736 char *config_file
, *slash
;
1737 char *config_file_tmp
= NULL
;
1738 char *config_file_sav
= NULL
;
1739 int ret
= CMD_WARNING
;
1740 struct vty
*file_vty
;
1741 struct stat conf_stat
;
1746 /* Check and see if we are operating under vtysh configuration */
1747 if (host
.config
== NULL
) {
1749 "Can't save to configuration file, using vtysh.\n");
1754 config_file
= host
.config
;
1757 #define O_DIRECTORY 0
1759 slash
= strrchr(config_file
, '/');
1761 char *config_dir
= XSTRDUP(MTYPE_TMP
, config_file
);
1762 config_dir
[slash
- config_file
] = '\0';
1763 dirfd
= open(config_dir
, O_DIRECTORY
| O_RDONLY
);
1764 XFREE(MTYPE_TMP
, config_dir
);
1766 dirfd
= open(".", O_DIRECTORY
| O_RDONLY
);
1767 /* if dirfd is invalid, directory sync fails, but we're still OK */
1769 config_file_sav
= XMALLOC(
1770 MTYPE_TMP
, strlen(config_file
) + strlen(CONF_BACKUP_EXT
) + 1);
1771 strcpy(config_file_sav
, config_file
);
1772 strcat(config_file_sav
, CONF_BACKUP_EXT
);
1775 config_file_tmp
= XMALLOC(MTYPE_TMP
, strlen(config_file
) + 8);
1776 sprintf(config_file_tmp
, "%s.XXXXXX", config_file
);
1778 /* Open file to configuration write. */
1779 fd
= mkstemp(config_file_tmp
);
1781 vty_out(vty
, "Can't open configuration file %s.\n",
1785 if (fchmod(fd
, CONFIGFILE_MASK
) != 0) {
1786 vty_out(vty
, "Can't chmod configuration file %s: %s (%d).\n",
1787 config_file_tmp
, safe_strerror(errno
), errno
);
1791 /* Make vty for configuration file. */
1792 file_vty
= vty_new();
1794 file_vty
->type
= VTY_FILE
;
1796 /* Config file header print. */
1797 vty_out(file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1798 vty_time_print(file_vty
, 1);
1799 vty_out(file_vty
, "!\n");
1800 vty_write_config(file_vty
);
1801 vty_close(file_vty
);
1803 if (stat(config_file
, &conf_stat
) >= 0) {
1804 if (unlink(config_file_sav
) != 0)
1805 if (errno
!= ENOENT
) {
1807 "Can't unlink backup configuration file %s.\n",
1811 if (link(config_file
, config_file_sav
) != 0) {
1813 "Can't backup old configuration file %s.\n",
1820 if (rename(config_file_tmp
, config_file
) != 0) {
1821 vty_out(vty
, "Can't save configuration file %s.\n",
1828 vty_out(vty
, "Configuration saved to %s\n", config_file
);
1832 if (ret
!= CMD_SUCCESS
)
1833 unlink(config_file_tmp
);
1836 XFREE(MTYPE_TMP
, config_file_tmp
);
1837 XFREE(MTYPE_TMP
, config_file_sav
);
1841 /* Write current configuration into file. */
1843 DEFUN (config_write
,
1845 "write [<file|memory|terminal>]",
1846 "Write running configuration to memory, network, or terminal\n"
1847 "Write to configuration file\n"
1848 "Write configuration currently in memory\n"
1849 "Write configuration to terminal\n")
1851 const int idx_type
= 1;
1853 // if command was 'write terminal' or 'write memory'
1854 if (argc
== 2 && (!strcmp(argv
[idx_type
]->text
, "terminal"))) {
1855 return vty_write_config(vty
);
1858 return file_write_config(vty
);
1861 /* ALIAS_FIXME for 'write <terminal|memory>' */
1862 DEFUN (show_running_config
,
1863 show_running_config_cmd
,
1864 "show running-config",
1866 "running configuration (same as write terminal)\n")
1868 return vty_write_config(vty
);
1871 /* ALIAS_FIXME for 'write file' */
1872 DEFUN (copy_runningconf_startupconf
,
1873 copy_runningconf_startupconf_cmd
,
1874 "copy running-config startup-config",
1875 "Copy configuration\n"
1876 "Copy running config to... \n"
1877 "Copy running config to startup config (same as write file/memory)\n")
1879 return file_write_config(vty
);
1883 /* Write startup configuration into the terminal. */
1884 DEFUN (show_startup_config
,
1885 show_startup_config_cmd
,
1886 "show startup-config",
1888 "Contents of startup configuration\n")
1895 if (host
.config
== NULL
)
1898 confp
= fopen(host
.config
, "r");
1899 if (confp
== NULL
) {
1900 vty_out(vty
, "Can't open configuration file [%s] due to '%s'\n",
1901 host
.config
, safe_strerror(errno
));
1905 while (fgets(buf
, BUFSIZ
, confp
)) {
1908 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1912 vty_out(vty
, "%s\n", buf
);
1920 int cmd_domainname_set(const char *domainname
)
1922 XFREE(MTYPE_HOST
, host
.domainname
);
1923 host
.domainname
= domainname
? XSTRDUP(MTYPE_HOST
, domainname
) : NULL
;
1927 /* Hostname configuration */
1928 DEFUN(config_domainname
,
1931 "Set system's domain name\n"
1932 "This system's domain name\n")
1934 struct cmd_token
*word
= argv
[1];
1936 if (!isalpha((int)word
->arg
[0])) {
1937 vty_out(vty
, "Please specify string starting with alphabet\n");
1938 return CMD_WARNING_CONFIG_FAILED
;
1941 return cmd_domainname_set(word
->arg
);
1944 DEFUN(config_no_domainname
,
1946 "no domainname [DOMAINNAME]",
1948 "Reset system's domain name\n"
1949 "domain name of this router\n")
1951 return cmd_domainname_set(NULL
);
1954 int cmd_hostname_set(const char *hostname
)
1956 XFREE(MTYPE_HOST
, host
.name
);
1957 host
.name
= hostname
? XSTRDUP(MTYPE_HOST
, hostname
) : NULL
;
1961 /* Hostname configuration */
1962 DEFUN (config_hostname
,
1965 "Set system's network name\n"
1966 "This system's network name\n")
1968 struct cmd_token
*word
= argv
[1];
1970 if (!isalnum((int)word
->arg
[0])) {
1971 vty_out(vty
, "Please specify string starting with alphabet\n");
1972 return CMD_WARNING_CONFIG_FAILED
;
1975 return cmd_hostname_set(word
->arg
);
1978 DEFUN (config_no_hostname
,
1980 "no hostname [HOSTNAME]",
1982 "Reset system's network name\n"
1983 "Host name of this router\n")
1985 return cmd_hostname_set(NULL
);
1988 /* VTY interface password set. */
1989 DEFUN (config_password
,
1991 "password [(8-8)] WORD",
1992 "Modify the terminal connection password\n"
1993 "Specifies a HIDDEN password will follow\n"
1994 "The password string\n")
1998 if (argc
== 3) // '8' was specified
2001 XFREE(MTYPE_HOST
, host
.password
);
2002 host
.password
= NULL
;
2003 if (host
.password_encrypt
)
2004 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2005 host
.password_encrypt
=
2006 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2010 if (!isalnum((int)argv
[idx_8
]->arg
[0])) {
2012 "Please specify string starting with alphanumeric\n");
2013 return CMD_WARNING_CONFIG_FAILED
;
2017 XFREE(MTYPE_HOST
, host
.password
);
2018 host
.password
= NULL
;
2021 if (host
.password_encrypt
)
2022 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2023 host
.password_encrypt
=
2024 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2026 host
.password
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2031 /* VTY interface password delete. */
2032 DEFUN (no_config_password
,
2036 "Modify the terminal connection password\n")
2038 bool warned
= false;
2040 if (host
.password
) {
2041 if (!vty_shell_serv(vty
)) {
2042 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2045 XFREE(MTYPE_HOST
, host
.password
);
2047 host
.password
= NULL
;
2049 if (host
.password_encrypt
) {
2050 if (!warned
&& !vty_shell_serv(vty
))
2051 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2052 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2054 host
.password_encrypt
= NULL
;
2059 /* VTY enable password set. */
2060 DEFUN (config_enable_password
,
2061 enable_password_cmd
,
2062 "enable password [(8-8)] WORD",
2063 "Modify enable password parameters\n"
2064 "Assign the privileged level password\n"
2065 "Specifies a HIDDEN password will follow\n"
2066 "The HIDDEN 'enable' password string\n")
2071 /* Crypt type is specified. */
2073 if (argv
[idx_8
]->arg
[0] == '8') {
2075 XFREE(MTYPE_HOST
, host
.enable
);
2078 if (host
.enable_encrypt
)
2079 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2080 host
.enable_encrypt
=
2081 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2085 vty_out(vty
, "Unknown encryption type.\n");
2086 return CMD_WARNING_CONFIG_FAILED
;
2090 if (!isalnum((int)argv
[idx_8
]->arg
[0])) {
2092 "Please specify string starting with alphanumeric\n");
2093 return CMD_WARNING_CONFIG_FAILED
;
2097 XFREE(MTYPE_HOST
, host
.enable
);
2100 /* Plain password input. */
2102 if (host
.enable_encrypt
)
2103 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2104 host
.enable_encrypt
=
2105 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2107 host
.enable
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2112 /* VTY enable password delete. */
2113 DEFUN (no_config_enable_password
,
2114 no_enable_password_cmd
,
2115 "no enable password",
2117 "Modify enable password parameters\n"
2118 "Assign the privileged level password\n")
2120 bool warned
= false;
2123 if (!vty_shell_serv(vty
)) {
2124 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2127 XFREE(MTYPE_HOST
, host
.enable
);
2131 if (host
.enable_encrypt
) {
2132 if (!warned
&& !vty_shell_serv(vty
))
2133 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2134 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2136 host
.enable_encrypt
= NULL
;
2141 DEFUN (service_password_encrypt
,
2142 service_password_encrypt_cmd
,
2143 "service password-encryption",
2144 "Set up miscellaneous service\n"
2145 "Enable encrypted passwords\n")
2152 if (host
.password
) {
2153 if (host
.password_encrypt
)
2154 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2155 host
.password_encrypt
=
2156 XSTRDUP(MTYPE_HOST
, zencrypt(host
.password
));
2159 if (host
.enable_encrypt
)
2160 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2161 host
.enable_encrypt
=
2162 XSTRDUP(MTYPE_HOST
, zencrypt(host
.enable
));
2168 DEFUN (no_service_password_encrypt
,
2169 no_service_password_encrypt_cmd
,
2170 "no service password-encryption",
2172 "Set up miscellaneous service\n"
2173 "Enable encrypted passwords\n")
2180 if (host
.password_encrypt
)
2181 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2182 host
.password_encrypt
= NULL
;
2184 if (host
.enable_encrypt
)
2185 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2186 host
.enable_encrypt
= NULL
;
2191 DEFUN (config_terminal_length
,
2192 config_terminal_length_cmd
,
2193 "terminal length (0-512)",
2194 "Set terminal line parameters\n"
2195 "Set number of lines on a screen\n"
2196 "Number of lines on screen (0 for no pausing)\n")
2200 vty
->lines
= atoi(argv
[idx_number
]->arg
);
2205 DEFUN (config_terminal_no_length
,
2206 config_terminal_no_length_cmd
,
2207 "terminal no length",
2208 "Set terminal line parameters\n"
2210 "Set number of lines on a screen\n")
2216 DEFUN (service_terminal_length
,
2217 service_terminal_length_cmd
,
2218 "service terminal-length (0-512)",
2219 "Set up miscellaneous service\n"
2220 "System wide terminal length configuration\n"
2221 "Number of lines of VTY (0 means no line control)\n")
2225 host
.lines
= atoi(argv
[idx_number
]->arg
);
2230 DEFUN (no_service_terminal_length
,
2231 no_service_terminal_length_cmd
,
2232 "no service terminal-length [(0-512)]",
2234 "Set up miscellaneous service\n"
2235 "System wide terminal length configuration\n"
2236 "Number of lines of VTY (0 means no line control)\n")
2242 DEFUN_HIDDEN (do_echo
,
2245 "Echo a message back to the vty\n"
2246 "The message to echo\n")
2250 vty_out(vty
, "%s\n",
2251 ((message
= argv_concat(argv
, argc
, 1)) ? message
: ""));
2253 XFREE(MTYPE_TMP
, message
);
2257 DEFUN (config_logmsg
,
2259 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
2260 "Send a message to enabled logging destinations\n"
2262 "The message to send\n")
2264 int idx_log_level
= 1;
2265 int idx_message
= 2;
2269 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2270 return CMD_ERR_NO_MATCH
;
2273 ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
2275 XFREE(MTYPE_TMP
, message
);
2280 DEFUN (show_logging
,
2284 "Show current logging configuration\n")
2286 struct zlog
*zl
= zlog_default
;
2288 vty_out(vty
, "Syslog logging: ");
2289 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
2290 vty_out(vty
, "disabled");
2292 vty_out(vty
, "level %s, facility %s, ident %s",
2293 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
2294 facility_name(zl
->facility
), zl
->ident
);
2297 vty_out(vty
, "Stdout logging: ");
2298 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
2299 vty_out(vty
, "disabled");
2301 vty_out(vty
, "level %s",
2302 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
2305 vty_out(vty
, "Monitor logging: ");
2306 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
2307 vty_out(vty
, "disabled");
2309 vty_out(vty
, "level %s",
2310 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
2313 vty_out(vty
, "File logging: ");
2314 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) || !zl
->fp
)
2315 vty_out(vty
, "disabled");
2317 vty_out(vty
, "level %s, filename %s",
2318 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
2322 vty_out(vty
, "Protocol name: %s\n", zl
->protoname
);
2323 vty_out(vty
, "Record priority: %s\n",
2324 (zl
->record_priority
? "enabled" : "disabled"));
2325 vty_out(vty
, "Timestamp precision: %d\n", zl
->timestamp_precision
);
2330 DEFUN (config_log_stdout
,
2331 config_log_stdout_cmd
,
2332 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2334 "Set stdout logging level\n"
2337 int idx_log_level
= 2;
2339 if (argc
== idx_log_level
) {
2340 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2345 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2346 return CMD_ERR_NO_MATCH
;
2347 zlog_set_level(ZLOG_DEST_STDOUT
, level
);
2351 DEFUN (no_config_log_stdout
,
2352 no_config_log_stdout_cmd
,
2353 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2356 "Cancel logging to stdout\n"
2359 zlog_set_level(ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
2363 DEFUN (config_log_monitor
,
2364 config_log_monitor_cmd
,
2365 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2367 "Set terminal line (monitor) logging level\n"
2370 int idx_log_level
= 2;
2372 if (argc
== idx_log_level
) {
2373 zlog_set_level(ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
2378 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2379 return CMD_ERR_NO_MATCH
;
2380 zlog_set_level(ZLOG_DEST_MONITOR
, level
);
2384 DEFUN (no_config_log_monitor
,
2385 no_config_log_monitor_cmd
,
2386 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2389 "Disable terminal line (monitor) logging\n"
2392 zlog_set_level(ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
2396 static int set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
2400 const char *fullpath
;
2402 /* Path detection. */
2403 if (!IS_DIRECTORY_SEP(*fname
)) {
2404 char cwd
[MAXPATHLEN
+ 1];
2405 cwd
[MAXPATHLEN
] = '\0';
2407 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2408 zlog_err("config_log_file: Unable to alloc mem!");
2409 return CMD_WARNING_CONFIG_FAILED
;
2412 if ((p
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + strlen(fname
) + 2))
2414 zlog_err("config_log_file: Unable to alloc mem!");
2415 return CMD_WARNING_CONFIG_FAILED
;
2417 sprintf(p
, "%s/%s", cwd
, fname
);
2422 ret
= zlog_set_file(fullpath
, loglevel
);
2425 XFREE(MTYPE_TMP
, p
);
2429 vty_out(vty
, "can't open logfile %s\n", fname
);
2430 return CMD_WARNING_CONFIG_FAILED
;
2434 XFREE(MTYPE_HOST
, host
.logfile
);
2436 host
.logfile
= XSTRDUP(MTYPE_HOST
, fname
);
2438 #if defined(HAVE_CUMULUS)
2439 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2440 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2445 void command_setup_early_logging(const char *dest
, const char *level
)
2450 int nlevel
= level_match(level
);
2452 if (nlevel
!= ZLOG_DISABLED
)
2453 zlog_default
->default_lvl
= nlevel
;
2459 if (strcmp(dest
, "stdout") == 0) {
2460 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2464 if (strcmp(dest
, "syslog") == 0) {
2465 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2469 token
= strstr(dest
, ":");
2475 set_log_file(NULL
, token
, zlog_default
->default_lvl
);
2478 DEFUN (config_log_file
,
2479 config_log_file_cmd
,
2480 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2483 "Logging filename\n"
2486 int idx_filename
= 2;
2487 int idx_log_levels
= 3;
2490 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2492 return CMD_ERR_NO_MATCH
;
2493 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2495 return set_log_file(vty
, argv
[idx_filename
]->arg
,
2496 zlog_default
->default_lvl
);
2499 static void disable_log_file(void)
2504 XFREE(MTYPE_HOST
, host
.logfile
);
2506 host
.logfile
= NULL
;
2509 DEFUN (no_config_log_file
,
2510 no_config_log_file_cmd
,
2511 "no log file [FILENAME [LEVEL]]",
2514 "Cancel logging to file\n"
2515 "Logging file name\n"
2522 DEFUN (config_log_syslog
,
2523 config_log_syslog_cmd
,
2524 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2526 "Set syslog logging level\n"
2529 int idx_log_levels
= 2;
2535 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2537 return CMD_ERR_NO_MATCH
;
2538 zlog_set_level(ZLOG_DEST_SYSLOG
, level
);
2541 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2546 DEFUN (no_config_log_syslog
,
2547 no_config_log_syslog_cmd
,
2548 "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>]",
2551 "Cancel logging to syslog\n"
2555 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2559 DEFUN (config_log_facility
,
2560 config_log_facility_cmd
,
2561 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2563 "Facility parameter for syslog messages\n"
2567 int facility
= facility_match(argv
[idx_target
]->arg
);
2569 zlog_default
->facility
= facility
;
2573 DEFUN (no_config_log_facility
,
2574 no_config_log_facility_cmd
,
2575 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2578 "Reset syslog facility to default (daemon)\n"
2581 zlog_default
->facility
= LOG_DAEMON
;
2585 DEFUN (config_log_record_priority
,
2586 config_log_record_priority_cmd
,
2587 "log record-priority",
2589 "Log the priority of the message within the message\n")
2591 zlog_default
->record_priority
= 1;
2595 DEFUN (no_config_log_record_priority
,
2596 no_config_log_record_priority_cmd
,
2597 "no log record-priority",
2600 "Do not log the priority of the message within the message\n")
2602 zlog_default
->record_priority
= 0;
2606 DEFUN (config_log_timestamp_precision
,
2607 config_log_timestamp_precision_cmd
,
2608 "log timestamp precision (0-6)",
2610 "Timestamp configuration\n"
2611 "Set the timestamp precision\n"
2612 "Number of subsecond digits\n")
2615 zlog_default
->timestamp_precision
=
2616 strtoul(argv
[idx_number
]->arg
, NULL
, 10);
2620 DEFUN (no_config_log_timestamp_precision
,
2621 no_config_log_timestamp_precision_cmd
,
2622 "no log timestamp precision",
2625 "Timestamp configuration\n"
2626 "Reset the timestamp precision to the default value of 0\n")
2628 zlog_default
->timestamp_precision
= 0;
2632 DEFUN (debug_memstats
,
2634 "[no] debug memstats-at-exit",
2637 "Print memory type statistics at exit\n")
2639 debug_memstats_at_exit
= !!strcmp(argv
[0]->text
, "no");
2643 int cmd_banner_motd_file(const char *file
)
2645 int success
= CMD_SUCCESS
;
2650 rpath
= realpath(file
, p
);
2652 return CMD_ERR_NO_FILE
;
2653 in
= strstr(rpath
, SYSCONFDIR
);
2656 XFREE(MTYPE_HOST
, host
.motdfile
);
2657 host
.motdfile
= XSTRDUP(MTYPE_HOST
, file
);
2659 success
= CMD_WARNING_CONFIG_FAILED
;
2664 DEFUN (banner_motd_file
,
2665 banner_motd_file_cmd
,
2666 "banner motd file FILE",
2669 "Banner from a file\n"
2673 const char *filename
= argv
[idx_file
]->arg
;
2674 int cmd
= cmd_banner_motd_file(filename
);
2676 if (cmd
== CMD_ERR_NO_FILE
)
2677 vty_out(vty
, "%s does not exist", filename
);
2678 else if (cmd
== CMD_WARNING_CONFIG_FAILED
)
2679 vty_out(vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2684 DEFUN (banner_motd_default
,
2685 banner_motd_default_cmd
,
2686 "banner motd default",
2687 "Set banner string\n"
2688 "Strings for motd\n"
2691 host
.motd
= default_motd
;
2695 DEFUN (no_banner_motd
,
2699 "Set banner string\n"
2700 "Strings for motd\n")
2704 XFREE(MTYPE_HOST
, host
.motdfile
);
2705 host
.motdfile
= NULL
;
2712 "Find CLI command containing text\n"
2713 "Text to search for\n")
2715 char *text
= argv_concat(argv
, argc
, 1);
2716 const struct cmd_node
*node
;
2717 const struct cmd_element
*cli
;
2720 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++) {
2721 node
= vector_slot(cmdvec
, i
);
2724 clis
= node
->cmd_vector
;
2725 for (unsigned int j
= 0; j
< vector_active(clis
); j
++) {
2726 cli
= vector_slot(clis
, j
);
2727 if (strcasestr(cli
->string
, text
))
2728 vty_out(vty
, " (%s) %s\n",
2729 node_names
[node
->node
], cli
->string
);
2733 XFREE(MTYPE_TMP
, text
);
2738 /* Set config filename. Called from vty.c */
2739 void host_config_set(const char *filename
)
2742 XFREE(MTYPE_HOST
, host
.config
);
2743 host
.config
= XSTRDUP(MTYPE_HOST
, filename
);
2746 const char *host_config_get(void)
2751 void install_default(enum node_type node
)
2753 install_element(node
, &config_exit_cmd
);
2754 install_element(node
, &config_quit_cmd
);
2755 install_element(node
, &config_end_cmd
);
2756 install_element(node
, &config_help_cmd
);
2757 install_element(node
, &config_list_cmd
);
2758 install_element(node
, &show_cli_graph_cmd
);
2759 install_element(node
, &find_cmd
);
2761 install_element(node
, &config_write_cmd
);
2762 install_element(node
, &show_running_config_cmd
);
2764 install_element(node
, &autocomplete_cmd
);
2767 /* Initialize command interface. Install basic nodes and commands.
2769 * terminal = 0 -- vtysh / no logging, no config control
2770 * terminal = 1 -- normal daemon
2771 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2772 void cmd_init(int terminal
)
2774 struct utsname names
;
2776 if (array_size(node_names
) != NODE_TYPE_MAX
)
2777 assert(!"Update the CLI node description array!");
2782 /* register command preprocessors */
2783 hook_register(cmd_execute
, handle_pipe_action
);
2784 hook_register(cmd_execute_done
, handle_pipe_action_done
);
2786 varhandlers
= list_new();
2788 /* Allocate initial top vector of commands. */
2789 cmdvec
= vector_init(VECTOR_MIN_SIZE
);
2791 /* Default host value settings. */
2792 host
.name
= XSTRDUP(MTYPE_HOST
, names
.nodename
);
2793 #ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
2794 if ((strcmp(names
.domainname
, "(none)") == 0))
2795 host
.domainname
= NULL
;
2797 host
.domainname
= XSTRDUP(MTYPE_HOST
, names
.domainname
);
2799 host
.domainname
= NULL
;
2801 host
.password
= NULL
;
2803 host
.logfile
= NULL
;
2805 host
.noconfig
= (terminal
< 0);
2807 host
.motd
= default_motd
;
2808 host
.motdfile
= NULL
;
2810 /* Install top nodes. */
2811 install_node(&view_node
, NULL
);
2812 install_node(&enable_node
, NULL
);
2813 install_node(&auth_node
, NULL
);
2814 install_node(&auth_enable_node
, NULL
);
2815 install_node(&config_node
, config_write_host
);
2817 /* Each node's basic commands. */
2818 install_element(VIEW_NODE
, &show_version_cmd
);
2819 install_element(ENABLE_NODE
, &show_startup_config_cmd
);
2820 install_element(ENABLE_NODE
, &debug_memstats_cmd
);
2823 install_element(VIEW_NODE
, &config_list_cmd
);
2824 install_element(VIEW_NODE
, &config_exit_cmd
);
2825 install_element(VIEW_NODE
, &config_quit_cmd
);
2826 install_element(VIEW_NODE
, &config_help_cmd
);
2827 install_element(VIEW_NODE
, &config_enable_cmd
);
2828 install_element(VIEW_NODE
, &config_terminal_length_cmd
);
2829 install_element(VIEW_NODE
, &config_terminal_no_length_cmd
);
2830 install_element(VIEW_NODE
, &show_logging_cmd
);
2831 install_element(VIEW_NODE
, &show_commandtree_cmd
);
2832 install_element(VIEW_NODE
, &echo_cmd
);
2833 install_element(VIEW_NODE
, &autocomplete_cmd
);
2834 install_element(VIEW_NODE
, &find_cmd
);
2836 install_element(ENABLE_NODE
, &config_end_cmd
);
2837 install_element(ENABLE_NODE
, &config_disable_cmd
);
2838 install_element(ENABLE_NODE
, &config_terminal_cmd
);
2839 install_element(ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2840 install_element(ENABLE_NODE
, &config_write_cmd
);
2841 install_element(ENABLE_NODE
, &show_running_config_cmd
);
2842 install_element(ENABLE_NODE
, &config_logmsg_cmd
);
2844 install_default(CONFIG_NODE
);
2847 workqueue_cmd_init();
2851 install_element(CONFIG_NODE
, &hostname_cmd
);
2852 install_element(CONFIG_NODE
, &no_hostname_cmd
);
2853 install_element(CONFIG_NODE
, &domainname_cmd
);
2854 install_element(CONFIG_NODE
, &no_domainname_cmd
);
2855 install_element(CONFIG_NODE
, &frr_version_defaults_cmd
);
2856 install_element(CONFIG_NODE
, &debug_memstats_cmd
);
2859 install_element(CONFIG_NODE
, &password_cmd
);
2860 install_element(CONFIG_NODE
, &no_password_cmd
);
2861 install_element(CONFIG_NODE
, &enable_password_cmd
);
2862 install_element(CONFIG_NODE
, &no_enable_password_cmd
);
2864 install_element(CONFIG_NODE
, &config_log_stdout_cmd
);
2865 install_element(CONFIG_NODE
, &no_config_log_stdout_cmd
);
2866 install_element(CONFIG_NODE
, &config_log_monitor_cmd
);
2867 install_element(CONFIG_NODE
, &no_config_log_monitor_cmd
);
2868 install_element(CONFIG_NODE
, &config_log_file_cmd
);
2869 install_element(CONFIG_NODE
, &no_config_log_file_cmd
);
2870 install_element(CONFIG_NODE
, &config_log_syslog_cmd
);
2871 install_element(CONFIG_NODE
, &no_config_log_syslog_cmd
);
2872 install_element(CONFIG_NODE
, &config_log_facility_cmd
);
2873 install_element(CONFIG_NODE
, &no_config_log_facility_cmd
);
2874 install_element(CONFIG_NODE
, &config_log_record_priority_cmd
);
2875 install_element(CONFIG_NODE
,
2876 &no_config_log_record_priority_cmd
);
2877 install_element(CONFIG_NODE
,
2878 &config_log_timestamp_precision_cmd
);
2879 install_element(CONFIG_NODE
,
2880 &no_config_log_timestamp_precision_cmd
);
2881 install_element(CONFIG_NODE
, &service_password_encrypt_cmd
);
2882 install_element(CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2883 install_element(CONFIG_NODE
, &banner_motd_default_cmd
);
2884 install_element(CONFIG_NODE
, &banner_motd_file_cmd
);
2885 install_element(CONFIG_NODE
, &no_banner_motd_cmd
);
2886 install_element(CONFIG_NODE
, &service_terminal_length_cmd
);
2887 install_element(CONFIG_NODE
, &no_service_terminal_length_cmd
);
2889 vrf_install_commands();
2893 grammar_sandbox_init();
2897 void cmd_terminate()
2899 struct cmd_node
*cmd_node
;
2901 hook_unregister(cmd_execute
, handle_pipe_action
);
2902 hook_unregister(cmd_execute_done
, handle_pipe_action_done
);
2905 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++)
2906 if ((cmd_node
= vector_slot(cmdvec
, i
)) != NULL
) {
2907 // deleting the graph delets the cmd_element as
2909 graph_delete_graph(cmd_node
->cmdgraph
);
2910 vector_free(cmd_node
->cmd_vector
);
2911 hash_clean(cmd_node
->cmd_hash
, NULL
);
2912 hash_free(cmd_node
->cmd_hash
);
2913 cmd_node
->cmd_hash
= NULL
;
2916 vector_free(cmdvec
);
2921 XFREE(MTYPE_HOST
, host
.name
);
2922 if (host
.domainname
)
2923 XFREE(MTYPE_HOST
, host
.domainname
);
2925 XFREE(MTYPE_HOST
, host
.password
);
2926 if (host
.password_encrypt
)
2927 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2929 XFREE(MTYPE_HOST
, host
.enable
);
2930 if (host
.enable_encrypt
)
2931 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2933 XFREE(MTYPE_HOST
, host
.logfile
);
2935 XFREE(MTYPE_HOST
, host
.motdfile
);
2937 XFREE(MTYPE_HOST
, host
.config
);
2939 list_delete_and_null(&varhandlers
);