2 * CLI backend interface.
5 * Copyright (C) 2016 Cumulus Networks, Inc.
6 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
7 * Copyright (C) 2013 by Open Source Routing.
8 * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
10 * This file is part of GNU Zebra.
12 * GNU Zebra is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
17 * GNU Zebra is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; see the file COPYING; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <lib/version.h>
39 #include "workqueue.h"
41 #include "command_match.h"
42 #include "command_graph.h"
48 #include "lib_errors.h"
49 #include "northbound_cli.h"
51 DEFINE_MTYPE(LIB
, HOST
, "Host config")
52 DEFINE_MTYPE(LIB
, COMPLETION
, "Completion item")
59 /* clang-format off */
60 const struct message tokennames
[] = {
65 item(IPV4_PREFIX_TKN
),
67 item(IPV6_PREFIX_TKN
),
77 const char *node_names
[] = {
80 "auth enable", // AUTH_ENABLE_NODE,
81 "enable", // ENABLE_NODE,
82 "config", // CONFIG_NODE,
83 "debug", // DEBUG_NODE,
84 "vrf debug", // VRF_DEBUG_NODE,
85 "northbound debug", // NORTHBOUND_DEBUG_NODE,
86 "vnc debug", // DEBUG_VNC_NODE,
88 "keychain", // KEYCHAIN_NODE,
89 "keychain key", // KEYCHAIN_KEY_NODE,
90 "logical-router", // LOGICALROUTER_NODE,
91 "static ip", // IP_NODE,
93 "interface", // INTERFACE_NODE,
94 "nexthop-group", // NH_GROUP_NODE,
95 "zebra", // ZEBRA_NODE,
96 "table", // TABLE_NODE,
98 "ripng", // RIPNG_NODE,
99 "babel", // BABEL_NODE,
100 "eigrp", // EIGRP_NODE,
102 "bgp vpnv4", // BGP_VPNV4_NODE,
103 "bgp vpnv6", // BGP_VPNV6_NODE,
104 "bgp ipv4 unicast", // BGP_IPV4_NODE,
105 "bgp ipv4 multicast", // BGP_IPV4M_NODE,
106 "bgp ipv4 labeled unicast", // BGP_IPV4L_NODE,
107 "bgp ipv6", // BGP_IPV6_NODE,
108 "bgp ipv6 multicast", // BGP_IPV6M_NODE,
109 "bgp ipv6 labeled unicast", // BGP_IPV6L_NODE,
110 "bgp vrf policy", // BGP_VRF_POLICY_NODE,
111 "bgp vnc defaults", // BGP_VNC_DEFAULTS_NODE,
112 "bgp vnc nve", // BGP_VNC_NVE_GROUP_NODE,
113 "bgp vnc l2", // BGP_VNC_L2_GROUP_NODE,
114 "rfp defaults", // RFP_DEFAULTS_NODE,
115 "bgp evpn", // BGP_EVPN_NODE,
116 "ospf", // OSPF_NODE,
117 "ospf6", // OSPF6_NODE,
119 "ldp ipv4", // LDP_IPV4_NODE,
120 "ldp ipv6", // LDP_IPV6_NODE,
121 "ldp ipv4 interface", // LDP_IPV4_IFACE_NODE,
122 "ldp ipv6 interface", // LDP_IPV6_IFACE_NODE,
123 "ldp l2vpn", // LDP_L2VPN_NODE,
124 "ldp", // LDP_PSEUDOWIRE_NODE,
125 "isis", // ISIS_NODE,
126 "ipv4 access list", // ACCESS_NODE,
127 "ipv4 prefix list", // PREFIX_NODE,
128 "ipv6 access list", // ACCESS_IPV6_NODE,
129 "MAC access list", // ACCESS_MAC_NODE,
130 "ipv6 prefix list", // PREFIX_IPV6_NODE,
131 "as list", // AS_LIST_NODE,
132 "community list", // COMMUNITY_LIST_NODE,
133 "routemap", // RMAP_NODE,
134 "pbr-map", // PBRMAP_NODE,
135 "smux", // SMUX_NODE,
136 "dump", // DUMP_NODE,
137 "forwarding", // FORWARDING_NODE,
138 "protocol", // PROTOCOL_NODE,
139 "mpls", // MPLS_NODE,
142 "link-params", // LINK_PARAMS_NODE,
143 "bgp evpn vni", // BGP_EVPN_VNI_NODE,
145 "bgp ipv4 flowspec", /* BGP_FLOWSPECV4_NODE
147 "bgp ipv6 flowspec", /* BGP_FLOWSPECV6_NODE
149 "bfd", /* BFD_NODE */
150 "bfd peer", /* BFD_PEER_NODE */
151 "openfabric", // OPENFABRIC_NODE
153 /* clang-format on */
155 /* Command vector which includes some level of command lists. Normally
156 each daemon maintains each own cmdvec. */
157 vector cmdvec
= NULL
;
159 /* Host information structure. */
163 * Returns host.name if any, otherwise
164 * it returns the system hostname.
166 const char *cmd_hostname_get(void)
172 * Returns unix domainname
174 const char *cmd_domainname_get(void)
176 return host
.domainname
;
179 /* Standard command node structures. */
180 static struct cmd_node auth_node
= {
181 AUTH_NODE
, "Password: ",
184 static struct cmd_node view_node
= {
188 static struct cmd_node auth_enable_node
= {
189 AUTH_ENABLE_NODE
, "Password: ",
192 static struct cmd_node enable_node
= {
196 static struct cmd_node config_node
= {CONFIG_NODE
, "%s(config)# ", 1};
198 /* Default motd string. */
199 static const char *default_motd
= FRR_DEFAULT_MOTD
;
201 static const struct facility_map
{
205 } syslog_facilities
[] = {
206 {LOG_KERN
, "kern", 1},
207 {LOG_USER
, "user", 2},
208 {LOG_MAIL
, "mail", 1},
209 {LOG_DAEMON
, "daemon", 1},
210 {LOG_AUTH
, "auth", 1},
211 {LOG_SYSLOG
, "syslog", 1},
213 {LOG_NEWS
, "news", 1},
214 {LOG_UUCP
, "uucp", 2},
215 {LOG_CRON
, "cron", 1},
219 {LOG_LOCAL0
, "local0", 6},
220 {LOG_LOCAL1
, "local1", 6},
221 {LOG_LOCAL2
, "local2", 6},
222 {LOG_LOCAL3
, "local3", 6},
223 {LOG_LOCAL4
, "local4", 6},
224 {LOG_LOCAL5
, "local5", 6},
225 {LOG_LOCAL6
, "local6", 6},
226 {LOG_LOCAL7
, "local7", 6},
230 static const char *facility_name(int facility
)
232 const struct facility_map
*fm
;
234 for (fm
= syslog_facilities
; fm
->name
; fm
++)
235 if (fm
->facility
== facility
)
240 static int facility_match(const char *str
)
242 const struct facility_map
*fm
;
244 for (fm
= syslog_facilities
; fm
->name
; fm
++)
245 if (!strncmp(str
, fm
->name
, fm
->match
))
250 static int level_match(const char *s
)
254 for (level
= 0; zlog_priority
[level
] != NULL
; level
++)
255 if (!strncmp(s
, zlog_priority
[level
], 2))
257 return ZLOG_DISABLED
;
260 /* This is called from main when a daemon is invoked with -v or --version. */
261 void print_version(const char *progname
)
263 printf("%s version %s\n", progname
, FRR_VERSION
);
264 printf("%s\n", FRR_COPYRIGHT
);
265 printf("configured with:\n\t%s\n", FRR_CONFIG_ARGS
);
268 char *argv_concat(struct cmd_token
**argv
, int argc
, int shift
)
270 int cnt
= MAX(argc
- shift
, 0);
271 const char *argstr
[cnt
+ 1];
276 for (int i
= 0; i
< cnt
; i
++)
277 argstr
[i
] = argv
[i
+ shift
]->arg
;
279 return frrstr_join(argstr
, cnt
, " ");
282 vector
cmd_make_strvec(const char *string
)
287 const char *copy
= string
;
289 /* skip leading whitespace */
290 while (isspace((int)*copy
) && *copy
!= '\0')
293 /* if the entire string was whitespace or a comment, return */
294 if (*copy
== '\0' || *copy
== '!' || *copy
== '#')
297 vector result
= frrstr_split_vec(copy
, "\n\r\t ");
299 for (unsigned int i
= 0; i
< vector_active(result
); i
++) {
300 if (strlen(vector_slot(result
, i
)) == 0) {
301 XFREE(MTYPE_TMP
, vector_slot(result
, i
));
302 vector_unset(result
, i
);
306 vector_compact(result
);
311 void cmd_free_strvec(vector v
)
313 frrstr_strvec_free(v
);
317 * Convenience function for accessing argv data.
321 * @param text definition snippet of the desired token
322 * @param index the starting index, and where to store the
323 * index of the found token if it exists
324 * @return 1 if found, 0 otherwise
326 int argv_find(struct cmd_token
**argv
, int argc
, const char *text
, int *index
)
329 for (int i
= *index
; i
< argc
&& found
== 0; i
++)
330 if ((found
= strmatch(text
, argv
[i
]->text
)))
335 static unsigned int cmd_hash_key(void *p
)
337 int size
= sizeof(p
);
339 return jhash(p
, size
, 0);
342 static bool cmd_hash_cmp(const void *a
, const void *b
)
347 /* Install top node of command vector. */
348 void install_node(struct cmd_node
*node
, int (*func
)(struct vty
*))
350 vector_set_index(cmdvec
, node
->node
, node
);
352 node
->cmdgraph
= graph_new();
353 node
->cmd_vector
= vector_init(VECTOR_MIN_SIZE
);
355 struct cmd_token
*token
=
356 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
357 graph_new_node(node
->cmdgraph
, token
,
358 (void (*)(void *)) & cmd_token_del
);
359 node
->cmd_hash
= hash_create_size(16, cmd_hash_key
, cmd_hash_cmp
,
363 /* Return prompt character of specified node. */
364 const char *cmd_prompt(enum node_type node
)
366 struct cmd_node
*cnode
;
368 cnode
= vector_slot(cmdvec
, node
);
369 return cnode
->prompt
;
372 /* Install a command into a node. */
373 void install_element(enum node_type ntype
, struct cmd_element
*cmd
)
375 struct cmd_node
*cnode
;
377 /* cmd_init hasn't been called */
379 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
384 cnode
= vector_lookup(cmdvec
, ntype
);
389 "\tnode %d (%s) does not exist.\n"
390 "\tplease call install_node() before install_element()\n",
391 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
395 if (hash_lookup(cnode
->cmd_hash
, cmd
) != NULL
) {
398 "\tnode %d (%s) already has this command installed.\n"
399 "\tduplicate install_element call?\n",
400 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
404 assert(hash_get(cnode
->cmd_hash
, cmd
, hash_alloc_intern
));
406 struct graph
*graph
= graph_new();
407 struct cmd_token
*token
=
408 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
409 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
411 cmd_graph_parse(graph
, cmd
);
412 cmd_graph_names(graph
);
413 cmd_graph_merge(cnode
->cmdgraph
, graph
, +1);
414 graph_delete_graph(graph
);
416 vector_set(cnode
->cmd_vector
, cmd
);
418 if (ntype
== VIEW_NODE
)
419 install_element(ENABLE_NODE
, cmd
);
422 void uninstall_element(enum node_type ntype
, struct cmd_element
*cmd
)
424 struct cmd_node
*cnode
;
426 /* cmd_init hasn't been called */
428 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
433 cnode
= vector_lookup(cmdvec
, ntype
);
438 "\tnode %d (%s) does not exist.\n"
439 "\tplease call install_node() before uninstall_element()\n",
440 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
444 if (hash_release(cnode
->cmd_hash
, cmd
) == NULL
) {
447 "\tnode %d (%s) does not have this command installed.\n"
448 "\tduplicate uninstall_element call?\n",
449 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
453 vector_unset_value(cnode
->cmd_vector
, cmd
);
455 struct graph
*graph
= graph_new();
456 struct cmd_token
*token
=
457 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
458 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
460 cmd_graph_parse(graph
, cmd
);
461 cmd_graph_names(graph
);
462 cmd_graph_merge(cnode
->cmdgraph
, graph
, -1);
463 graph_delete_graph(graph
);
465 if (ntype
== VIEW_NODE
)
466 uninstall_element(ENABLE_NODE
, cmd
);
470 static const unsigned char itoa64
[] =
471 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
473 static void to64(char *s
, long v
, int n
)
476 *s
++ = itoa64
[v
& 0x3f];
481 static char *zencrypt(const char *passwd
)
485 char *crypt(const char *, const char *);
487 gettimeofday(&tv
, 0);
489 to64(&salt
[0], random(), 3);
490 to64(&salt
[3], tv
.tv_usec
, 3);
493 return crypt(passwd
, salt
);
496 /* This function write configuration of this host. */
497 static int config_write_host(struct vty
*vty
)
499 if (cmd_hostname_get())
500 vty_out(vty
, "hostname %s\n", cmd_hostname_get());
502 if (cmd_domainname_get())
503 vty_out(vty
, "domainname %s\n", cmd_domainname_get());
505 /* The following are all configuration commands that are not sent to
506 * watchfrr. For instance watchfrr is hardcoded to log to syslog so
507 * we would always display 'log syslog informational' in the config
508 * which would cause other daemons to then switch to syslog when they
511 if (strcmp(zlog_default
->protoname
, "WATCHFRR")) {
513 if (host
.password_encrypt
)
514 vty_out(vty
, "password 8 %s\n",
515 host
.password_encrypt
);
516 if (host
.enable_encrypt
)
517 vty_out(vty
, "enable password 8 %s\n",
518 host
.enable_encrypt
);
521 vty_out(vty
, "password %s\n", host
.password
);
523 vty_out(vty
, "enable password %s\n",
528 && (zlog_default
->maxlvl
[ZLOG_DEST_FILE
]
530 vty_out(vty
, "log file %s", host
.logfile
);
531 if (zlog_default
->maxlvl
[ZLOG_DEST_FILE
]
532 != zlog_default
->default_lvl
)
535 [zlog_default
->maxlvl
540 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != ZLOG_DISABLED
) {
541 vty_out(vty
, "log stdout");
542 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
]
543 != zlog_default
->default_lvl
)
546 [zlog_default
->maxlvl
547 [ZLOG_DEST_STDOUT
]]);
551 if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
552 vty_out(vty
, "no log monitor\n");
553 else if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]
554 != zlog_default
->default_lvl
)
555 vty_out(vty
, "log monitor %s\n",
556 zlog_priority
[zlog_default
->maxlvl
557 [ZLOG_DEST_MONITOR
]]);
559 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
) {
560 vty_out(vty
, "log syslog");
561 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
]
562 != zlog_default
->default_lvl
)
564 zlog_priority
[zlog_default
->maxlvl
565 [ZLOG_DEST_SYSLOG
]]);
569 if (zlog_default
->facility
!= LOG_DAEMON
)
570 vty_out(vty
, "log facility %s\n",
571 facility_name(zlog_default
->facility
));
573 if (zlog_default
->record_priority
== 1)
574 vty_out(vty
, "log record-priority\n");
576 if (zlog_default
->timestamp_precision
> 0)
577 vty_out(vty
, "log timestamp precision %d\n",
578 zlog_default
->timestamp_precision
);
581 vty_out(vty
, "service advanced-vty\n");
584 vty_out(vty
, "service password-encryption\n");
587 vty_out(vty
, "service terminal-length %d\n",
591 vty_out(vty
, "banner motd file %s\n", host
.motdfile
);
593 vty_out(vty
, "no banner motd\n");
596 if (debug_memstats_at_exit
)
597 vty_out(vty
, "!\ndebug memstats-at-exit\n");
602 /* Utility function for getting command graph. */
603 static struct graph
*cmd_node_graph(vector v
, enum node_type ntype
)
605 struct cmd_node
*cnode
= vector_slot(v
, ntype
);
606 return cnode
->cmdgraph
;
609 static int cmd_try_do_shortcut(enum node_type node
, char *first_word
)
611 if (first_word
!= NULL
&& node
!= AUTH_NODE
&& node
!= VIEW_NODE
612 && node
!= AUTH_ENABLE_NODE
&& 0 == strcmp("do", first_word
))
618 * Compare function for cmd_token.
619 * Used with qsort to sort command completions.
621 static int compare_completions(const void *fst
, const void *snd
)
623 const struct cmd_token
*first
= *(const struct cmd_token
* const *)fst
,
624 *secnd
= *(const struct cmd_token
* const *)snd
;
625 return strcmp(first
->text
, secnd
->text
);
629 * Takes a list of completions returned by command_complete,
630 * dedeuplicates them based on both text and description,
631 * sorts them, and returns them as a vector.
633 * @param completions linked list of cmd_token
634 * @return deduplicated and sorted vector with
636 vector
completions_to_vec(struct list
*completions
)
638 vector comps
= vector_init(VECTOR_MIN_SIZE
);
641 struct cmd_token
*token
, *cr
= NULL
;
642 unsigned int i
, exists
;
643 for (ALL_LIST_ELEMENTS_RO(completions
, ln
, token
)) {
644 if (token
->type
== END_TKN
&& (cr
= token
))
647 // linear search for token in completions vector
649 for (i
= 0; i
< vector_active(comps
) && !exists
; i
++) {
650 struct cmd_token
*curr
= vector_slot(comps
, i
);
652 exists
= !strcmp(curr
->text
, token
->text
)
653 && !strcmp(curr
->desc
, token
->desc
);
655 exists
= !strcmp(curr
->text
, token
->text
);
656 #endif /* VTYSH_DEBUG */
660 vector_set(comps
, token
);
664 qsort(comps
->index
, vector_active(comps
), sizeof(void *),
665 &compare_completions
);
667 // make <cr> the first element, if it is present
669 vector_set_index(comps
, vector_active(comps
), NULL
);
670 memmove(comps
->index
+ 1, comps
->index
,
671 (comps
->alloced
- 1) * sizeof(void *));
672 vector_set_index(comps
, 0, cr
);
678 * Generates a vector of cmd_token representing possible completions
679 * on the current input.
681 * @param vline the vectorized input line
682 * @param vty the vty with the node to match on
683 * @param status pointer to matcher status code
684 * @return vector of struct cmd_token * with possible completions
686 static vector
cmd_complete_command_real(vector vline
, struct vty
*vty
,
689 struct list
*completions
;
690 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
692 enum matcher_rv rv
= command_complete(cmdgraph
, vline
, &completions
);
694 if (MATCHER_ERROR(rv
)) {
695 *status
= CMD_ERR_NO_MATCH
;
699 vector comps
= completions_to_vec(completions
);
700 list_delete(&completions
);
702 // set status code appropriately
703 switch (vector_active(comps
)) {
705 *status
= CMD_ERR_NO_MATCH
;
708 *status
= CMD_COMPLETE_FULL_MATCH
;
711 *status
= CMD_COMPLETE_LIST_MATCH
;
717 vector
cmd_describe_command(vector vline
, struct vty
*vty
, int *status
)
721 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
722 enum node_type onode
;
723 int orig_xpath_index
;
724 vector shifted_vline
;
728 orig_xpath_index
= vty
->xpath_index
;
729 vty
->node
= ENABLE_NODE
;
730 vty
->xpath_index
= 0;
731 /* We can try it on enable node, cos' the vty is authenticated
734 shifted_vline
= vector_init(vector_count(vline
));
736 for (index
= 1; index
< vector_active(vline
); index
++) {
737 vector_set_index(shifted_vline
, index
- 1,
738 vector_lookup(vline
, index
));
741 ret
= cmd_complete_command_real(shifted_vline
, vty
, status
);
743 vector_free(shifted_vline
);
745 vty
->xpath_index
= orig_xpath_index
;
749 return cmd_complete_command_real(vline
, vty
, status
);
752 static struct list
*varhandlers
= NULL
;
754 void cmd_variable_complete(struct cmd_token
*token
, const char *arg
,
758 const struct cmd_variable_handler
*cvh
;
762 tmpcomps
= arg
? vector_init(VECTOR_MIN_SIZE
) : comps
;
764 for (ALL_LIST_ELEMENTS_RO(varhandlers
, ln
, cvh
)) {
765 if (cvh
->tokenname
&& strcmp(cvh
->tokenname
, token
->text
))
767 if (cvh
->varname
&& (!token
->varname
768 || strcmp(cvh
->varname
, token
->varname
)))
770 cvh
->completions(tmpcomps
, token
);
778 for (i
= vector_active(tmpcomps
); i
; i
--) {
779 char *item
= vector_slot(tmpcomps
, i
- 1);
780 if (strlen(item
) >= argsz
&& !strncmp(item
, arg
, argsz
))
781 vector_set(comps
, item
);
783 XFREE(MTYPE_COMPLETION
, item
);
785 vector_free(tmpcomps
);
788 #define AUTOCOMP_INDENT 5
790 char *cmd_variable_comp2str(vector comps
, unsigned short cols
)
793 char *buf
= XCALLOC(MTYPE_TMP
, bsz
);
794 int lc
= AUTOCOMP_INDENT
;
795 size_t cs
= AUTOCOMP_INDENT
;
797 snprintf(buf
, bsz
, "%*s", AUTOCOMP_INDENT
, "");
798 for (size_t j
= 0; j
< vector_active(comps
); j
++) {
799 char *item
= vector_slot(comps
, j
);
800 itemlen
= strlen(item
);
802 if (cs
+ itemlen
+ AUTOCOMP_INDENT
+ 3 >= bsz
)
803 buf
= XREALLOC(MTYPE_TMP
, buf
, (bsz
*= 2));
805 if (lc
+ itemlen
+ 1 >= cols
) {
806 cs
+= snprintf(&buf
[cs
], bsz
- cs
, "\n%*s",
807 AUTOCOMP_INDENT
, "");
808 lc
= AUTOCOMP_INDENT
;
811 size_t written
= snprintf(&buf
[cs
], bsz
- cs
, "%s ", item
);
814 XFREE(MTYPE_COMPLETION
, item
);
815 vector_set_index(comps
, j
, NULL
);
820 void cmd_variable_handler_register(const struct cmd_variable_handler
*cvh
)
825 for (; cvh
->completions
; cvh
++)
826 listnode_add(varhandlers
, (void *)cvh
);
829 DEFUN_HIDDEN (autocomplete
,
831 "autocomplete TYPE TEXT VARNAME",
832 "Autocompletion handler (internal, for vtysh)\n"
835 "cmd_token->varname\n")
837 struct cmd_token tok
;
838 vector comps
= vector_init(32);
841 memset(&tok
, 0, sizeof(tok
));
842 tok
.type
= atoi(argv
[1]->arg
);
843 tok
.text
= argv
[2]->arg
;
844 tok
.varname
= argv
[3]->arg
;
845 if (!strcmp(tok
.varname
, "-"))
848 cmd_variable_complete(&tok
, NULL
, comps
);
850 for (i
= 0; i
< vector_active(comps
); i
++) {
851 char *text
= vector_slot(comps
, i
);
852 vty_out(vty
, "%s\n", text
);
853 XFREE(MTYPE_COMPLETION
, text
);
861 * Generate possible tab-completions for the given input. This function only
862 * returns results that would result in a valid command if used as Readline
863 * completions (as is the case in vtysh). For instance, if the passed vline ends
864 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
866 * @param vline vectorized input line
868 * @param status location to store matcher status code in
869 * @return set of valid strings for use with Readline as tab-completions.
872 char **cmd_complete_command(vector vline
, struct vty
*vty
, int *status
)
875 int original_node
= vty
->node
;
876 vector input_line
= vector_init(vector_count(vline
));
878 // if the first token is 'do' we'll want to execute the command in the
880 int do_shortcut
= cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0));
881 vty
->node
= do_shortcut
? ENABLE_NODE
: original_node
;
883 // construct the input line we'll be matching on
884 unsigned int offset
= (do_shortcut
) ? 1 : 0;
885 for (unsigned index
= 0; index
+ offset
< vector_active(vline
); index
++)
886 vector_set_index(input_line
, index
,
887 vector_lookup(vline
, index
+ offset
));
889 // get token completions -- this is a copying operation
890 vector comps
= NULL
, initial_comps
;
891 initial_comps
= cmd_complete_command_real(input_line
, vty
, status
);
893 if (!MATCHER_ERROR(*status
)) {
894 assert(initial_comps
);
895 // filter out everything that is not suitable for a
897 comps
= vector_init(VECTOR_MIN_SIZE
);
898 for (unsigned int i
= 0; i
< vector_active(initial_comps
);
900 struct cmd_token
*token
= vector_slot(initial_comps
, i
);
901 if (token
->type
== WORD_TKN
)
902 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
,
904 else if (IS_VARYING_TOKEN(token
->type
)) {
905 const char *ref
= vector_lookup(
906 vline
, vector_active(vline
) - 1);
907 cmd_variable_complete(token
, ref
, comps
);
910 vector_free(initial_comps
);
912 // since we filtered results, we need to re-set status code
913 switch (vector_active(comps
)) {
915 *status
= CMD_ERR_NO_MATCH
;
918 *status
= CMD_COMPLETE_FULL_MATCH
;
921 *status
= CMD_COMPLETE_LIST_MATCH
;
924 // copy completions text into an array of char*
925 ret
= XMALLOC(MTYPE_TMP
,
926 (vector_active(comps
) + 1) * sizeof(char *));
928 for (i
= 0; i
< vector_active(comps
); i
++) {
929 ret
[i
] = vector_slot(comps
, i
);
931 // set the last element to NULL, because this array is used in
932 // a Readline completion_generator function which expects NULL
933 // as a sentinel value
937 } else if (initial_comps
)
938 vector_free(initial_comps
);
940 // comps should always be null here
943 // free the adjusted input line
944 vector_free(input_line
);
946 // reset vty->node to its original value
947 vty
->node
= original_node
;
952 /* return parent node */
953 /* MUST eventually converge on CONFIG_NODE */
954 enum node_type
node_parent(enum node_type node
)
958 assert(node
> CONFIG_NODE
);
963 case BGP_FLOWSPECV4_NODE
:
964 case BGP_FLOWSPECV6_NODE
:
965 case BGP_VRF_POLICY_NODE
:
966 case BGP_VNC_DEFAULTS_NODE
:
967 case BGP_VNC_NVE_GROUP_NODE
:
968 case BGP_VNC_L2_GROUP_NODE
:
978 case BGP_EVPN_VNI_NODE
:
981 case KEYCHAIN_KEY_NODE
:
984 case LINK_PARAMS_NODE
:
985 ret
= INTERFACE_NODE
;
991 case LDP_IPV4_IFACE_NODE
:
994 case LDP_IPV6_IFACE_NODE
:
997 case LDP_PSEUDOWIRE_NODE
:
998 ret
= LDP_L2VPN_NODE
;
1011 /* Execute command by argument vline vector. */
1012 static int cmd_execute_command_real(vector vline
, enum cmd_filter_type filter
,
1014 const struct cmd_element
**cmd
)
1016 struct list
*argv_list
;
1017 enum matcher_rv status
;
1018 const struct cmd_element
*matched_element
= NULL
;
1020 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
1021 status
= command_match(cmdgraph
, vline
, &argv_list
, &matched_element
);
1024 *cmd
= matched_element
;
1026 // if matcher error, return corresponding CMD_ERR
1027 if (MATCHER_ERROR(status
)) {
1029 list_delete(&argv_list
);
1031 case MATCHER_INCOMPLETE
:
1032 return CMD_ERR_INCOMPLETE
;
1033 case MATCHER_AMBIGUOUS
:
1034 return CMD_ERR_AMBIGUOUS
;
1036 return CMD_ERR_NO_MATCH
;
1040 // build argv array from argv list
1041 struct cmd_token
**argv
= XMALLOC(
1042 MTYPE_TMP
, argv_list
->count
* sizeof(struct cmd_token
*));
1043 struct listnode
*ln
;
1044 struct cmd_token
*token
;
1046 for (ALL_LIST_ELEMENTS_RO(argv_list
, ln
, token
))
1049 int argc
= argv_list
->count
;
1052 if (matched_element
->daemon
)
1053 ret
= CMD_SUCCESS_DAEMON
;
1055 /* Clear enqueued configuration changes. */
1056 vty
->num_cfg_changes
= 0;
1057 memset(&vty
->cfg_changes
, 0, sizeof(vty
->cfg_changes
));
1059 ret
= matched_element
->func(matched_element
, vty
, argc
, argv
);
1062 // delete list and cmd_token's in it
1063 list_delete(&argv_list
);
1064 XFREE(MTYPE_TMP
, argv
);
1070 * Execute a given command, handling things like "do ..." and checking
1071 * whether the given command might apply at a parent node if doesn't
1072 * apply for the current node.
1074 * @param vline Command line input, vector of char* where each element is
1076 * @param vty The vty context in which the command should be executed.
1077 * @param cmd Pointer where the struct cmd_element of the matched command
1078 * will be stored, if any. May be set to NULL if this info is
1080 * @param vtysh If set != 0, don't lookup the command at parent nodes.
1081 * @return The status of the command that has been executed or an error code
1082 * as to why no command could be executed.
1084 int cmd_execute_command(vector vline
, struct vty
*vty
,
1085 const struct cmd_element
**cmd
, int vtysh
)
1087 int ret
, saved_ret
= 0;
1088 enum node_type onode
, try_node
;
1089 int orig_xpath_index
;
1091 onode
= try_node
= vty
->node
;
1092 orig_xpath_index
= vty
->xpath_index
;
1094 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
1095 vector shifted_vline
;
1098 vty
->node
= ENABLE_NODE
;
1099 vty
->xpath_index
= 0;
1100 /* We can try it on enable node, cos' the vty is authenticated
1103 shifted_vline
= vector_init(vector_count(vline
));
1105 for (index
= 1; index
< vector_active(vline
); index
++)
1106 vector_set_index(shifted_vline
, index
- 1,
1107 vector_lookup(vline
, index
));
1109 ret
= cmd_execute_command_real(shifted_vline
, FILTER_RELAXED
,
1112 vector_free(shifted_vline
);
1114 vty
->xpath_index
= orig_xpath_index
;
1119 cmd_execute_command_real(vline
, FILTER_RELAXED
, vty
, cmd
);
1124 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1125 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
) {
1126 /* This assumes all nodes above CONFIG_NODE are childs of
1128 while (vty
->node
> CONFIG_NODE
) {
1129 try_node
= node_parent(try_node
);
1130 vty
->node
= try_node
;
1131 if (vty
->xpath_index
> 0)
1133 ret
= cmd_execute_command_real(vline
, FILTER_RELAXED
,
1135 if (ret
== CMD_SUCCESS
|| ret
== CMD_WARNING
1136 || ret
== CMD_NOT_MY_INSTANCE
1137 || ret
== CMD_WARNING_CONFIG_FAILED
)
1140 /* no command succeeded, reset the vty to the original node */
1142 vty
->xpath_index
= orig_xpath_index
;
1145 /* return command status for original node */
1150 * Execute a given command, matching it strictly against the current node.
1151 * This mode is used when reading config files.
1153 * @param vline Command line input, vector of char* where each element is
1155 * @param vty The vty context in which the command should be executed.
1156 * @param cmd Pointer where the struct cmd_element* of the matched command
1157 * will be stored, if any. May be set to NULL if this info is
1159 * @return The status of the command that has been executed or an error code
1160 * as to why no command could be executed.
1162 int cmd_execute_command_strict(vector vline
, struct vty
*vty
,
1163 const struct cmd_element
**cmd
)
1165 return cmd_execute_command_real(vline
, FILTER_STRICT
, vty
, cmd
);
1169 * Hook for preprocessing command string before executing.
1171 * All subscribers are called with the raw command string that is to be
1172 * executed. If any changes are to be made, a new string should be allocated
1173 * with MTYPE_TMP and *cmd_out updated to point to this new string. The caller
1174 * is then responsible for freeing this string.
1176 * All processing functions must be mutually exclusive in their action, i.e. if
1177 * one subscriber decides to modify the command, all others must not modify it
1178 * when called. Feeding the output of one processing command into a subsequent
1179 * one is not supported.
1181 * This hook is intentionally internal to the command processing system.
1184 * The raw command string.
1187 * The result of any processing.
1189 DECLARE_HOOK(cmd_execute
,
1190 (struct vty
*vty
, const char *cmd_in
, char **cmd_out
),
1191 (vty
, cmd_in
, cmd_out
));
1192 DEFINE_HOOK(cmd_execute
, (struct vty
*vty
, const char *cmd_in
, char **cmd_out
),
1193 (vty
, cmd_in
, cmd_out
));
1195 /* Hook executed after a CLI command. */
1196 DECLARE_KOOH(cmd_execute_done
, (struct vty
*vty
, const char *cmd_exec
),
1198 DEFINE_KOOH(cmd_execute_done
, (struct vty
*vty
, const char *cmd_exec
),
1202 * cmd_execute hook subscriber to handle `|` actions.
1204 static int handle_pipe_action(struct vty
*vty
, const char *cmd_in
,
1208 char *orig
, *working
, *token
, *u
;
1209 char *pipe
= strstr(cmd_in
, "| ");
1214 /* duplicate string for processing purposes, not including pipe */
1215 orig
= working
= XSTRDUP(MTYPE_TMP
, pipe
+ 2);
1217 /* retrieve action */
1218 token
= strsep(&working
, " ");
1221 /* match result to known actions */
1222 if (strmatch(token
, "include")) {
1223 /* the remaining text should be a regexp */
1224 char *regexp
= working
;
1227 vty_out(vty
, "%% Need a regexp to filter with\n");
1231 bool succ
= vty_set_include(vty
, regexp
);
1234 vty_out(vty
, "%% Bad regexp '%s'\n", regexp
);
1237 *cmd_out
= XSTRDUP(MTYPE_TMP
, cmd_in
);
1241 vty_out(vty
, "%% Unknown action '%s'\n", token
);
1246 XFREE(MTYPE_TMP
, orig
);
1250 static int handle_pipe_action_done(struct vty
*vty
, const char *cmd_exec
)
1253 vty_set_include(vty
, NULL
);
1258 int cmd_execute(struct vty
*vty
, const char *cmd
,
1259 const struct cmd_element
**matched
, int vtysh
)
1262 char *cmd_out
= NULL
;
1263 const char *cmd_exec
;
1266 hook_call(cmd_execute
, vty
, cmd
, &cmd_out
);
1267 cmd_exec
= cmd_out
? (const char *)cmd_out
: cmd
;
1269 vline
= cmd_make_strvec(cmd_exec
);
1272 ret
= cmd_execute_command(vline
, vty
, matched
, vtysh
);
1273 cmd_free_strvec(vline
);
1278 hook_call(cmd_execute_done
, vty
, cmd_exec
);
1280 XFREE(MTYPE_TMP
, cmd_out
);
1287 * Parse one line of config, walking up the parse tree attempting to find a
1290 * @param vty The vty context in which the command should be executed.
1291 * @param cmd Pointer where the struct cmd_element* of the match command
1292 * will be stored, if any. May be set to NULL if this info is
1294 * @param use_daemon Boolean to control whether or not we match on
1295 * CMD_SUCCESS_DAEMON
1297 * @return The status of the command that has been executed or an error code
1298 * as to why no command could be executed.
1300 int command_config_read_one_line(struct vty
*vty
,
1301 const struct cmd_element
**cmd
,
1302 uint32_t line_num
, int use_daemon
)
1307 vline
= cmd_make_strvec(vty
->buf
);
1309 /* In case of comment line */
1313 /* Execute configuration command : this is strict match */
1314 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1316 // Climb the tree and try the command again at each 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 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
1321 && vty
->node
!= CONFIG_NODE
) {
1322 int saved_node
= vty
->node
;
1323 int saved_xpath_index
= vty
->xpath_index
;
1325 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1326 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1327 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1328 && vty
->node
> CONFIG_NODE
) {
1329 vty
->node
= node_parent(vty
->node
);
1330 if (vty
->xpath_index
> 0)
1332 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1335 // If climbing the tree did not work then ignore the command and
1336 // stay at the same node
1337 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1338 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1339 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
) {
1340 vty
->node
= saved_node
;
1341 vty
->xpath_index
= saved_xpath_index
;
1345 if (ret
!= CMD_SUCCESS
&&
1346 ret
!= CMD_WARNING
&&
1347 ret
!= CMD_SUCCESS_DAEMON
) {
1348 struct vty_error
*ve
= XCALLOC(MTYPE_TMP
, sizeof(*ve
));
1350 memcpy(ve
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
1351 ve
->line_num
= line_num
;
1353 vty
->error
= list_new();
1355 listnode_add(vty
->error
, ve
);
1358 cmd_free_strvec(vline
);
1363 /* Configuration make from file. */
1364 int config_from_file(struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
1366 int ret
, error_ret
= 0;
1369 while (fgets(vty
->buf
, VTY_BUFSIZ
, fp
)) {
1372 ret
= command_config_read_one_line(vty
, NULL
, *line_num
, 0);
1374 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1375 && ret
!= CMD_ERR_NOTHING_TODO
)
1386 /* Configuration from terminal */
1387 DEFUN (config_terminal
,
1388 config_terminal_cmd
,
1389 "configure [terminal]",
1390 "Configuration from vty interface\n"
1391 "Configuration terminal\n")
1393 return vty_config_enter(vty
, false, false);
1396 /* Enable command */
1400 "Turn on privileged mode command\n")
1402 /* If enable password is NULL, change to ENABLE_NODE */
1403 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
)
1404 || vty
->type
== VTY_SHELL_SERV
)
1405 vty
->node
= ENABLE_NODE
;
1407 vty
->node
= AUTH_ENABLE_NODE
;
1412 /* Disable command */
1416 "Turn off privileged mode command\n")
1418 if (vty
->node
== ENABLE_NODE
)
1419 vty
->node
= VIEW_NODE
;
1423 /* Down vty node level. */
1427 "Exit current mode and down to previous mode\n")
1433 void cmd_exit(struct vty
*vty
)
1435 switch (vty
->node
) {
1441 vty
->status
= VTY_CLOSE
;
1444 vty
->node
= ENABLE_NODE
;
1445 vty_config_exit(vty
);
1447 case INTERFACE_NODE
:
1449 case LOGICALROUTER_NODE
:
1461 case LDP_L2VPN_NODE
:
1463 case OPENFABRIC_NODE
:
1469 vty
->node
= CONFIG_NODE
;
1472 case BGP_IPV4M_NODE
:
1473 case BGP_IPV4L_NODE
:
1474 case BGP_VPNV4_NODE
:
1475 case BGP_VPNV6_NODE
:
1476 case BGP_FLOWSPECV4_NODE
:
1477 case BGP_FLOWSPECV6_NODE
:
1478 case BGP_VRF_POLICY_NODE
:
1479 case BGP_VNC_DEFAULTS_NODE
:
1480 case BGP_VNC_NVE_GROUP_NODE
:
1481 case BGP_VNC_L2_GROUP_NODE
:
1483 case BGP_IPV6M_NODE
:
1485 case BGP_IPV6L_NODE
:
1486 vty
->node
= BGP_NODE
;
1488 case BGP_EVPN_VNI_NODE
:
1489 vty
->node
= BGP_EVPN_NODE
;
1493 vty
->node
= LDP_NODE
;
1495 case LDP_IPV4_IFACE_NODE
:
1496 vty
->node
= LDP_IPV4_NODE
;
1498 case LDP_IPV6_IFACE_NODE
:
1499 vty
->node
= LDP_IPV6_NODE
;
1501 case LDP_PSEUDOWIRE_NODE
:
1502 vty
->node
= LDP_L2VPN_NODE
;
1504 case KEYCHAIN_KEY_NODE
:
1505 vty
->node
= KEYCHAIN_NODE
;
1507 case LINK_PARAMS_NODE
:
1508 vty
->node
= INTERFACE_NODE
;
1511 vty
->node
= BFD_NODE
;
1517 if (vty
->xpath_index
> 0)
1525 "Exit current mode and down to previous mode\n")
1527 return config_exit(self
, vty
, argc
, argv
);
1531 /* End of configuration. */
1535 "End current mode and change to enable mode.\n")
1538 vty_config_exit(vty
);
1539 vty
->node
= ENABLE_NODE
;
1546 DEFUN (show_version
,
1550 "Displays zebra version\n")
1552 vty_out(vty
, "%s %s (%s).\n", FRR_FULL_NAME
, FRR_VERSION
,
1553 cmd_hostname_get() ? cmd_hostname_get() : "");
1554 vty_out(vty
, "%s%s\n", FRR_COPYRIGHT
, GIT_INFO
);
1555 vty_out(vty
, "configured with:\n %s\n", FRR_CONFIG_ARGS
);
1560 /* "Set" version ... ignore version tags */
1561 DEFUN (frr_version_defaults
,
1562 frr_version_defaults_cmd
,
1563 "frr <version|defaults> LINE...",
1564 "FRRouting global parameters\n"
1565 "version configuration was written by\n"
1566 "set of configuration defaults used\n"
1572 /* Help display function for all node. */
1576 "Description of the interactive help system\n")
1579 "Quagga VTY provides advanced help feature. When you need help,\n\
1580 anytime at the command line please press '?'.\n\
1582 If nothing matches, the help list will be empty and you must backup\n\
1583 until entering a '?' shows the available options.\n\
1584 Two styles of help are provided:\n\
1585 1. Full help is available when you are ready to enter a\n\
1586 command argument (e.g. 'show ?') and describes each possible\n\
1588 2. Partial help is provided when an abbreviated argument is entered\n\
1589 and you want to know what arguments match the input\n\
1590 (e.g. 'show me?'.)\n\n");
1594 static void permute(struct graph_node
*start
, struct vty
*vty
)
1596 static struct list
*position
= NULL
;
1598 position
= list_new();
1600 struct cmd_token
*stok
= start
->data
;
1601 struct graph_node
*gnn
;
1602 struct listnode
*ln
;
1605 listnode_add(position
, start
);
1606 for (unsigned int i
= 0; i
< vector_active(start
->to
); i
++) {
1607 struct graph_node
*gn
= vector_slot(start
->to
, i
);
1608 struct cmd_token
*tok
= gn
->data
;
1609 if (tok
->attr
== CMD_ATTR_HIDDEN
1610 || tok
->attr
== CMD_ATTR_DEPRECATED
)
1612 else if (tok
->type
== END_TKN
|| gn
== start
) {
1614 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
)) {
1615 struct cmd_token
*tt
= gnn
->data
;
1616 if (tt
->type
< SPECIAL_TKN
)
1617 vty_out(vty
, " %s", tt
->text
);
1620 vty_out(vty
, "...");
1624 if (stok
->type
== FORK_TKN
&& tok
->type
!= FORK_TKN
)
1625 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
))
1634 list_delete_node(position
, listtail(position
));
1637 int cmd_list_cmds(struct vty
*vty
, int do_permute
)
1639 struct cmd_node
*node
= vector_slot(cmdvec
, vty
->node
);
1642 permute(vector_slot(node
->cmdgraph
->nodes
, 0), vty
);
1644 /* loop over all commands at this node */
1645 struct cmd_element
*element
= NULL
;
1646 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
);
1648 if ((element
= vector_slot(node
->cmd_vector
, i
))
1649 && element
->attr
!= CMD_ATTR_DEPRECATED
1650 && element
->attr
!= CMD_ATTR_HIDDEN
)
1651 vty_out(vty
, " %s\n", element
->string
);
1656 /* Help display function for all node. */
1659 "list [permutations]",
1660 "Print command list\n"
1661 "Print all possible command permutations\n")
1663 return cmd_list_cmds(vty
, argc
== 2);
1666 DEFUN (show_commandtree
,
1667 show_commandtree_cmd
,
1668 "show commandtree [permutations]",
1670 "Show command tree\n"
1671 "Permutations that we are interested in\n")
1673 return cmd_list_cmds(vty
, argc
== 3);
1676 DEFUN_HIDDEN(show_cli_graph
,
1681 "Dump current command space as DOT graph\n")
1683 struct cmd_node
*cn
= vector_slot(cmdvec
, vty
->node
);
1684 char *dot
= cmd_graph_dump_dot(cn
->cmdgraph
);
1686 vty_out(vty
, "%s\n", dot
);
1687 XFREE(MTYPE_TMP
, dot
);
1691 static int vty_write_config(struct vty
*vty
)
1694 struct cmd_node
*node
;
1699 if (vty
->type
== VTY_TERM
) {
1700 vty_out(vty
, "\nCurrent configuration:\n");
1701 vty_out(vty
, "!\n");
1704 vty_out(vty
, "frr version %s\n", FRR_VER_SHORT
);
1705 vty_out(vty
, "frr defaults %s\n", DFLT_NAME
);
1706 vty_out(vty
, "!\n");
1708 pthread_rwlock_rdlock(&running_config
->lock
);
1710 for (i
= 0; i
< vector_active(cmdvec
); i
++)
1711 if ((node
= vector_slot(cmdvec
, i
)) && node
->func
1712 && (node
->vtysh
|| vty
->type
!= VTY_SHELL
)) {
1713 if ((*node
->func
)(vty
))
1714 vty_out(vty
, "!\n");
1717 pthread_rwlock_unlock(&running_config
->lock
);
1719 if (vty
->type
== VTY_TERM
) {
1720 vty_out(vty
, "end\n");
1726 static int file_write_config(struct vty
*vty
)
1729 char *config_file
, *slash
;
1730 char *config_file_tmp
= NULL
;
1731 char *config_file_sav
= NULL
;
1732 int ret
= CMD_WARNING
;
1733 struct vty
*file_vty
;
1734 struct stat conf_stat
;
1739 /* Check and see if we are operating under vtysh configuration */
1740 if (host
.config
== NULL
) {
1742 "Can't save to configuration file, using vtysh.\n");
1747 config_file
= host
.config
;
1750 #define O_DIRECTORY 0
1752 slash
= strrchr(config_file
, '/');
1754 char *config_dir
= XSTRDUP(MTYPE_TMP
, config_file
);
1755 config_dir
[slash
- config_file
] = '\0';
1756 dirfd
= open(config_dir
, O_DIRECTORY
| O_RDONLY
);
1757 XFREE(MTYPE_TMP
, config_dir
);
1759 dirfd
= open(".", O_DIRECTORY
| O_RDONLY
);
1760 /* if dirfd is invalid, directory sync fails, but we're still OK */
1762 config_file_sav
= XMALLOC(
1763 MTYPE_TMP
, strlen(config_file
) + strlen(CONF_BACKUP_EXT
) + 1);
1764 strcpy(config_file_sav
, config_file
);
1765 strcat(config_file_sav
, CONF_BACKUP_EXT
);
1768 config_file_tmp
= XMALLOC(MTYPE_TMP
, strlen(config_file
) + 8);
1769 sprintf(config_file_tmp
, "%s.XXXXXX", config_file
);
1771 /* Open file to configuration write. */
1772 fd
= mkstemp(config_file_tmp
);
1774 vty_out(vty
, "Can't open configuration file %s.\n",
1778 if (fchmod(fd
, CONFIGFILE_MASK
) != 0) {
1779 vty_out(vty
, "Can't chmod configuration file %s: %s (%d).\n",
1780 config_file_tmp
, safe_strerror(errno
), errno
);
1784 /* Make vty for configuration file. */
1785 file_vty
= vty_new();
1787 file_vty
->type
= VTY_FILE
;
1789 /* Config file header print. */
1790 vty_out(file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1791 vty_time_print(file_vty
, 1);
1792 vty_out(file_vty
, "!\n");
1793 vty_write_config(file_vty
);
1794 vty_close(file_vty
);
1796 if (stat(config_file
, &conf_stat
) >= 0) {
1797 if (unlink(config_file_sav
) != 0)
1798 if (errno
!= ENOENT
) {
1800 "Can't unlink backup configuration file %s.\n",
1804 if (link(config_file
, config_file_sav
) != 0) {
1806 "Can't backup old configuration file %s.\n",
1813 if (rename(config_file_tmp
, config_file
) != 0) {
1814 vty_out(vty
, "Can't save configuration file %s.\n",
1821 vty_out(vty
, "Configuration saved to %s\n", config_file
);
1825 if (ret
!= CMD_SUCCESS
)
1826 unlink(config_file_tmp
);
1829 XFREE(MTYPE_TMP
, config_file_tmp
);
1830 XFREE(MTYPE_TMP
, config_file_sav
);
1834 /* Write current configuration into file. */
1836 DEFUN (config_write
,
1838 "write [<file|memory|terminal>]",
1839 "Write running configuration to memory, network, or terminal\n"
1840 "Write to configuration file\n"
1841 "Write configuration currently in memory\n"
1842 "Write configuration to terminal\n")
1844 const int idx_type
= 1;
1846 // if command was 'write terminal' or 'write memory'
1847 if (argc
== 2 && (!strcmp(argv
[idx_type
]->text
, "terminal"))) {
1848 return vty_write_config(vty
);
1851 return file_write_config(vty
);
1854 /* ALIAS_FIXME for 'write <terminal|memory>' */
1855 DEFUN (show_running_config
,
1856 show_running_config_cmd
,
1857 "show running-config",
1859 "running configuration (same as write terminal)\n")
1861 return vty_write_config(vty
);
1864 /* ALIAS_FIXME for 'write file' */
1865 DEFUN (copy_runningconf_startupconf
,
1866 copy_runningconf_startupconf_cmd
,
1867 "copy running-config startup-config",
1868 "Copy configuration\n"
1869 "Copy running config to... \n"
1870 "Copy running config to startup config (same as write file/memory)\n")
1872 return file_write_config(vty
);
1876 /* Write startup configuration into the terminal. */
1877 DEFUN (show_startup_config
,
1878 show_startup_config_cmd
,
1879 "show startup-config",
1881 "Contents of startup configuration\n")
1888 if (host
.config
== NULL
)
1891 confp
= fopen(host
.config
, "r");
1892 if (confp
== NULL
) {
1893 vty_out(vty
, "Can't open configuration file [%s] due to '%s'\n",
1894 host
.config
, safe_strerror(errno
));
1898 while (fgets(buf
, BUFSIZ
, confp
)) {
1901 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1905 vty_out(vty
, "%s\n", buf
);
1913 int cmd_domainname_set(const char *domainname
)
1915 XFREE(MTYPE_HOST
, host
.domainname
);
1916 host
.domainname
= domainname
? XSTRDUP(MTYPE_HOST
, domainname
) : NULL
;
1920 /* Hostname configuration */
1921 DEFUN(config_domainname
,
1924 "Set system's domain name\n"
1925 "This system's domain name\n")
1927 struct cmd_token
*word
= argv
[1];
1929 if (!isalpha((int)word
->arg
[0])) {
1930 vty_out(vty
, "Please specify string starting with alphabet\n");
1931 return CMD_WARNING_CONFIG_FAILED
;
1934 return cmd_domainname_set(word
->arg
);
1937 DEFUN(config_no_domainname
,
1939 "no domainname [DOMAINNAME]",
1941 "Reset system's domain name\n"
1942 "domain name of this router\n")
1944 return cmd_domainname_set(NULL
);
1947 int cmd_hostname_set(const char *hostname
)
1949 XFREE(MTYPE_HOST
, host
.name
);
1950 host
.name
= hostname
? XSTRDUP(MTYPE_HOST
, hostname
) : NULL
;
1954 /* Hostname configuration */
1955 DEFUN (config_hostname
,
1958 "Set system's network name\n"
1959 "This system's network name\n")
1961 struct cmd_token
*word
= argv
[1];
1963 if (!isalnum((int)word
->arg
[0])) {
1964 vty_out(vty
, "Please specify string starting with alphabet\n");
1965 return CMD_WARNING_CONFIG_FAILED
;
1968 return cmd_hostname_set(word
->arg
);
1971 DEFUN (config_no_hostname
,
1973 "no hostname [HOSTNAME]",
1975 "Reset system's network name\n"
1976 "Host name of this router\n")
1978 return cmd_hostname_set(NULL
);
1981 /* VTY interface password set. */
1982 DEFUN (config_password
,
1984 "password [(8-8)] WORD",
1985 "Modify the terminal connection password\n"
1986 "Specifies a HIDDEN password will follow\n"
1987 "The password string\n")
1991 if (argc
== 3) // '8' was specified
1994 XFREE(MTYPE_HOST
, host
.password
);
1995 host
.password
= NULL
;
1996 if (host
.password_encrypt
)
1997 XFREE(MTYPE_HOST
, host
.password_encrypt
);
1998 host
.password_encrypt
=
1999 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2003 if (!isalnum((int)argv
[idx_8
]->arg
[0])) {
2005 "Please specify string starting with alphanumeric\n");
2006 return CMD_WARNING_CONFIG_FAILED
;
2010 XFREE(MTYPE_HOST
, host
.password
);
2011 host
.password
= NULL
;
2014 if (host
.password_encrypt
)
2015 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2016 host
.password_encrypt
=
2017 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2019 host
.password
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2024 /* VTY interface password delete. */
2025 DEFUN (no_config_password
,
2029 "Modify the terminal connection password\n")
2031 bool warned
= false;
2033 if (host
.password
) {
2034 if (!vty_shell_serv(vty
)) {
2035 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2038 XFREE(MTYPE_HOST
, host
.password
);
2040 host
.password
= NULL
;
2042 if (host
.password_encrypt
) {
2043 if (!warned
&& !vty_shell_serv(vty
))
2044 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2045 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2047 host
.password_encrypt
= NULL
;
2052 /* VTY enable password set. */
2053 DEFUN (config_enable_password
,
2054 enable_password_cmd
,
2055 "enable password [(8-8)] WORD",
2056 "Modify enable password parameters\n"
2057 "Assign the privileged level password\n"
2058 "Specifies a HIDDEN password will follow\n"
2059 "The HIDDEN 'enable' password string\n")
2064 /* Crypt type is specified. */
2066 if (argv
[idx_8
]->arg
[0] == '8') {
2068 XFREE(MTYPE_HOST
, host
.enable
);
2071 if (host
.enable_encrypt
)
2072 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2073 host
.enable_encrypt
=
2074 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
2078 vty_out(vty
, "Unknown encryption type.\n");
2079 return CMD_WARNING_CONFIG_FAILED
;
2083 if (!isalnum((int)argv
[idx_8
]->arg
[0])) {
2085 "Please specify string starting with alphanumeric\n");
2086 return CMD_WARNING_CONFIG_FAILED
;
2090 XFREE(MTYPE_HOST
, host
.enable
);
2093 /* Plain password input. */
2095 if (host
.enable_encrypt
)
2096 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2097 host
.enable_encrypt
=
2098 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
2100 host
.enable
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
2105 /* VTY enable password delete. */
2106 DEFUN (no_config_enable_password
,
2107 no_enable_password_cmd
,
2108 "no enable password",
2110 "Modify enable password parameters\n"
2111 "Assign the privileged level password\n")
2113 bool warned
= false;
2116 if (!vty_shell_serv(vty
)) {
2117 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2120 XFREE(MTYPE_HOST
, host
.enable
);
2124 if (host
.enable_encrypt
) {
2125 if (!warned
&& !vty_shell_serv(vty
))
2126 vty_out(vty
, NO_PASSWD_CMD_WARNING
);
2127 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2129 host
.enable_encrypt
= NULL
;
2134 DEFUN (service_password_encrypt
,
2135 service_password_encrypt_cmd
,
2136 "service password-encryption",
2137 "Set up miscellaneous service\n"
2138 "Enable encrypted passwords\n")
2145 if (host
.password
) {
2146 if (host
.password_encrypt
)
2147 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2148 host
.password_encrypt
=
2149 XSTRDUP(MTYPE_HOST
, zencrypt(host
.password
));
2152 if (host
.enable_encrypt
)
2153 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2154 host
.enable_encrypt
=
2155 XSTRDUP(MTYPE_HOST
, zencrypt(host
.enable
));
2161 DEFUN (no_service_password_encrypt
,
2162 no_service_password_encrypt_cmd
,
2163 "no service password-encryption",
2165 "Set up miscellaneous service\n"
2166 "Enable encrypted passwords\n")
2173 if (host
.password_encrypt
)
2174 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2175 host
.password_encrypt
= NULL
;
2177 if (host
.enable_encrypt
)
2178 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2179 host
.enable_encrypt
= NULL
;
2184 DEFUN (config_terminal_length
,
2185 config_terminal_length_cmd
,
2186 "terminal length (0-512)",
2187 "Set terminal line parameters\n"
2188 "Set number of lines on a screen\n"
2189 "Number of lines on screen (0 for no pausing)\n")
2193 vty
->lines
= atoi(argv
[idx_number
]->arg
);
2198 DEFUN (config_terminal_no_length
,
2199 config_terminal_no_length_cmd
,
2200 "terminal no length",
2201 "Set terminal line parameters\n"
2203 "Set number of lines on a screen\n")
2209 DEFUN (service_terminal_length
,
2210 service_terminal_length_cmd
,
2211 "service terminal-length (0-512)",
2212 "Set up miscellaneous service\n"
2213 "System wide terminal length configuration\n"
2214 "Number of lines of VTY (0 means no line control)\n")
2218 host
.lines
= atoi(argv
[idx_number
]->arg
);
2223 DEFUN (no_service_terminal_length
,
2224 no_service_terminal_length_cmd
,
2225 "no service terminal-length [(0-512)]",
2227 "Set up miscellaneous service\n"
2228 "System wide terminal length configuration\n"
2229 "Number of lines of VTY (0 means no line control)\n")
2235 DEFUN_HIDDEN (do_echo
,
2238 "Echo a message back to the vty\n"
2239 "The message to echo\n")
2243 vty_out(vty
, "%s\n",
2244 ((message
= argv_concat(argv
, argc
, 1)) ? message
: ""));
2246 XFREE(MTYPE_TMP
, message
);
2250 DEFUN (config_logmsg
,
2252 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
2253 "Send a message to enabled logging destinations\n"
2255 "The message to send\n")
2257 int idx_log_level
= 1;
2258 int idx_message
= 2;
2262 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2263 return CMD_ERR_NO_MATCH
;
2266 ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
2268 XFREE(MTYPE_TMP
, message
);
2273 DEFUN (show_logging
,
2277 "Show current logging configuration\n")
2279 struct zlog
*zl
= zlog_default
;
2281 vty_out(vty
, "Syslog logging: ");
2282 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
2283 vty_out(vty
, "disabled");
2285 vty_out(vty
, "level %s, facility %s, ident %s",
2286 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
2287 facility_name(zl
->facility
), zl
->ident
);
2290 vty_out(vty
, "Stdout logging: ");
2291 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
2292 vty_out(vty
, "disabled");
2294 vty_out(vty
, "level %s",
2295 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
2298 vty_out(vty
, "Monitor logging: ");
2299 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
2300 vty_out(vty
, "disabled");
2302 vty_out(vty
, "level %s",
2303 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
2306 vty_out(vty
, "File logging: ");
2307 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) || !zl
->fp
)
2308 vty_out(vty
, "disabled");
2310 vty_out(vty
, "level %s, filename %s",
2311 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
2315 vty_out(vty
, "Protocol name: %s\n", zl
->protoname
);
2316 vty_out(vty
, "Record priority: %s\n",
2317 (zl
->record_priority
? "enabled" : "disabled"));
2318 vty_out(vty
, "Timestamp precision: %d\n", zl
->timestamp_precision
);
2323 DEFUN (config_log_stdout
,
2324 config_log_stdout_cmd
,
2325 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2327 "Set stdout logging level\n"
2330 int idx_log_level
= 2;
2332 if (argc
== idx_log_level
) {
2333 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2338 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2339 return CMD_ERR_NO_MATCH
;
2340 zlog_set_level(ZLOG_DEST_STDOUT
, level
);
2344 DEFUN (no_config_log_stdout
,
2345 no_config_log_stdout_cmd
,
2346 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2349 "Cancel logging to stdout\n"
2352 zlog_set_level(ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
2356 DEFUN (config_log_monitor
,
2357 config_log_monitor_cmd
,
2358 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2360 "Set terminal line (monitor) logging level\n"
2363 int idx_log_level
= 2;
2365 if (argc
== idx_log_level
) {
2366 zlog_set_level(ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
2371 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2372 return CMD_ERR_NO_MATCH
;
2373 zlog_set_level(ZLOG_DEST_MONITOR
, level
);
2377 DEFUN (no_config_log_monitor
,
2378 no_config_log_monitor_cmd
,
2379 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2382 "Disable terminal line (monitor) logging\n"
2385 zlog_set_level(ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
2389 static int set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
2393 const char *fullpath
;
2395 /* Path detection. */
2396 if (!IS_DIRECTORY_SEP(*fname
)) {
2397 char cwd
[MAXPATHLEN
+ 1];
2398 cwd
[MAXPATHLEN
] = '\0';
2400 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2401 flog_err_sys(EC_LIB_SYSTEM_CALL
,
2402 "config_log_file: Unable to alloc mem!");
2403 return CMD_WARNING_CONFIG_FAILED
;
2406 p
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + strlen(fname
) + 2);
2407 sprintf(p
, "%s/%s", cwd
, fname
);
2412 ret
= zlog_set_file(fullpath
, loglevel
);
2414 XFREE(MTYPE_TMP
, p
);
2418 vty_out(vty
, "can't open logfile %s\n", fname
);
2419 return CMD_WARNING_CONFIG_FAILED
;
2422 XFREE(MTYPE_HOST
, host
.logfile
);
2424 host
.logfile
= XSTRDUP(MTYPE_HOST
, fname
);
2426 #if defined(HAVE_CUMULUS)
2427 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2428 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2433 void command_setup_early_logging(const char *dest
, const char *level
)
2438 int nlevel
= level_match(level
);
2440 if (nlevel
!= ZLOG_DISABLED
)
2441 zlog_default
->default_lvl
= nlevel
;
2447 if (strcmp(dest
, "stdout") == 0) {
2448 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2452 if (strcmp(dest
, "syslog") == 0) {
2453 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2457 token
= strstr(dest
, ":");
2463 set_log_file(NULL
, token
, zlog_default
->default_lvl
);
2466 DEFUN (config_log_file
,
2467 config_log_file_cmd
,
2468 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2471 "Logging filename\n"
2474 int idx_filename
= 2;
2475 int idx_log_levels
= 3;
2478 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2480 return CMD_ERR_NO_MATCH
;
2481 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2483 return set_log_file(vty
, argv
[idx_filename
]->arg
,
2484 zlog_default
->default_lvl
);
2487 static void disable_log_file(void)
2491 XFREE(MTYPE_HOST
, host
.logfile
);
2493 host
.logfile
= NULL
;
2496 DEFUN (no_config_log_file
,
2497 no_config_log_file_cmd
,
2498 "no log file [FILENAME [LEVEL]]",
2501 "Cancel logging to file\n"
2502 "Logging file name\n"
2509 DEFUN (config_log_syslog
,
2510 config_log_syslog_cmd
,
2511 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2513 "Set syslog logging level\n"
2516 int idx_log_levels
= 2;
2520 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2522 return CMD_ERR_NO_MATCH
;
2523 zlog_set_level(ZLOG_DEST_SYSLOG
, level
);
2526 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2531 DEFUN (no_config_log_syslog
,
2532 no_config_log_syslog_cmd
,
2533 "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>]",
2536 "Cancel logging to syslog\n"
2540 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2544 DEFUN (config_log_facility
,
2545 config_log_facility_cmd
,
2546 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2548 "Facility parameter for syslog messages\n"
2552 int facility
= facility_match(argv
[idx_target
]->arg
);
2554 zlog_default
->facility
= facility
;
2558 DEFUN (no_config_log_facility
,
2559 no_config_log_facility_cmd
,
2560 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2563 "Reset syslog facility to default (daemon)\n"
2566 zlog_default
->facility
= LOG_DAEMON
;
2570 DEFUN (config_log_record_priority
,
2571 config_log_record_priority_cmd
,
2572 "log record-priority",
2574 "Log the priority of the message within the message\n")
2576 zlog_default
->record_priority
= 1;
2580 DEFUN (no_config_log_record_priority
,
2581 no_config_log_record_priority_cmd
,
2582 "no log record-priority",
2585 "Do not log the priority of the message within the message\n")
2587 zlog_default
->record_priority
= 0;
2591 DEFUN (config_log_timestamp_precision
,
2592 config_log_timestamp_precision_cmd
,
2593 "log timestamp precision (0-6)",
2595 "Timestamp configuration\n"
2596 "Set the timestamp precision\n"
2597 "Number of subsecond digits\n")
2600 zlog_default
->timestamp_precision
=
2601 strtoul(argv
[idx_number
]->arg
, NULL
, 10);
2605 DEFUN (no_config_log_timestamp_precision
,
2606 no_config_log_timestamp_precision_cmd
,
2607 "no log timestamp precision",
2610 "Timestamp configuration\n"
2611 "Reset the timestamp precision to the default value of 0\n")
2613 zlog_default
->timestamp_precision
= 0;
2617 DEFUN (debug_memstats
,
2619 "[no] debug memstats-at-exit",
2622 "Print memory type statistics at exit\n")
2624 debug_memstats_at_exit
= !!strcmp(argv
[0]->text
, "no");
2628 int cmd_banner_motd_file(const char *file
)
2630 int success
= CMD_SUCCESS
;
2635 rpath
= realpath(file
, p
);
2637 return CMD_ERR_NO_FILE
;
2638 in
= strstr(rpath
, SYSCONFDIR
);
2640 XFREE(MTYPE_HOST
, host
.motdfile
);
2641 host
.motdfile
= XSTRDUP(MTYPE_HOST
, file
);
2643 success
= CMD_WARNING_CONFIG_FAILED
;
2648 DEFUN (banner_motd_file
,
2649 banner_motd_file_cmd
,
2650 "banner motd file FILE",
2653 "Banner from a file\n"
2657 const char *filename
= argv
[idx_file
]->arg
;
2658 int cmd
= cmd_banner_motd_file(filename
);
2660 if (cmd
== CMD_ERR_NO_FILE
)
2661 vty_out(vty
, "%s does not exist", filename
);
2662 else if (cmd
== CMD_WARNING_CONFIG_FAILED
)
2663 vty_out(vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2668 DEFUN (banner_motd_default
,
2669 banner_motd_default_cmd
,
2670 "banner motd default",
2671 "Set banner string\n"
2672 "Strings for motd\n"
2675 host
.motd
= default_motd
;
2679 DEFUN (no_banner_motd
,
2683 "Set banner string\n"
2684 "Strings for motd\n")
2688 XFREE(MTYPE_HOST
, host
.motdfile
);
2689 host
.motdfile
= NULL
;
2696 "Find CLI command containing text\n"
2697 "Text to search for\n")
2699 char *text
= argv_concat(argv
, argc
, 1);
2700 const struct cmd_node
*node
;
2701 const struct cmd_element
*cli
;
2704 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++) {
2705 node
= vector_slot(cmdvec
, i
);
2708 clis
= node
->cmd_vector
;
2709 for (unsigned int j
= 0; j
< vector_active(clis
); j
++) {
2710 cli
= vector_slot(clis
, j
);
2711 if (strcasestr(cli
->string
, text
))
2712 vty_out(vty
, " (%s) %s\n",
2713 node_names
[node
->node
], cli
->string
);
2717 XFREE(MTYPE_TMP
, text
);
2722 /* Set config filename. Called from vty.c */
2723 void host_config_set(const char *filename
)
2725 XFREE(MTYPE_HOST
, host
.config
);
2726 host
.config
= XSTRDUP(MTYPE_HOST
, filename
);
2729 const char *host_config_get(void)
2734 void install_default(enum node_type node
)
2736 install_element(node
, &config_exit_cmd
);
2737 install_element(node
, &config_quit_cmd
);
2738 install_element(node
, &config_end_cmd
);
2739 install_element(node
, &config_help_cmd
);
2740 install_element(node
, &config_list_cmd
);
2741 install_element(node
, &show_cli_graph_cmd
);
2742 install_element(node
, &find_cmd
);
2744 install_element(node
, &config_write_cmd
);
2745 install_element(node
, &show_running_config_cmd
);
2747 install_element(node
, &autocomplete_cmd
);
2749 nb_cli_install_default(node
);
2752 /* Initialize command interface. Install basic nodes and commands.
2754 * terminal = 0 -- vtysh / no logging, no config control
2755 * terminal = 1 -- normal daemon
2756 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2757 void cmd_init(int terminal
)
2759 struct utsname names
;
2761 if (array_size(node_names
) != NODE_TYPE_MAX
)
2762 assert(!"Update the CLI node description array!");
2767 /* register command preprocessors */
2768 hook_register(cmd_execute
, handle_pipe_action
);
2769 hook_register(cmd_execute_done
, handle_pipe_action_done
);
2771 varhandlers
= list_new();
2773 /* Allocate initial top vector of commands. */
2774 cmdvec
= vector_init(VECTOR_MIN_SIZE
);
2776 /* Default host value settings. */
2777 host
.name
= XSTRDUP(MTYPE_HOST
, names
.nodename
);
2778 #ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
2779 if ((strcmp(names
.domainname
, "(none)") == 0))
2780 host
.domainname
= NULL
;
2782 host
.domainname
= XSTRDUP(MTYPE_HOST
, names
.domainname
);
2784 host
.domainname
= NULL
;
2786 host
.password
= NULL
;
2788 host
.logfile
= NULL
;
2790 host
.noconfig
= (terminal
< 0);
2792 host
.motd
= default_motd
;
2793 host
.motdfile
= NULL
;
2795 /* Install top nodes. */
2796 install_node(&view_node
, NULL
);
2797 install_node(&enable_node
, NULL
);
2798 install_node(&auth_node
, NULL
);
2799 install_node(&auth_enable_node
, NULL
);
2800 install_node(&config_node
, config_write_host
);
2802 /* Each node's basic commands. */
2803 install_element(VIEW_NODE
, &show_version_cmd
);
2804 install_element(ENABLE_NODE
, &show_startup_config_cmd
);
2805 install_element(ENABLE_NODE
, &debug_memstats_cmd
);
2808 install_element(VIEW_NODE
, &config_list_cmd
);
2809 install_element(VIEW_NODE
, &config_exit_cmd
);
2810 install_element(VIEW_NODE
, &config_quit_cmd
);
2811 install_element(VIEW_NODE
, &config_help_cmd
);
2812 install_element(VIEW_NODE
, &config_enable_cmd
);
2813 install_element(VIEW_NODE
, &config_terminal_length_cmd
);
2814 install_element(VIEW_NODE
, &config_terminal_no_length_cmd
);
2815 install_element(VIEW_NODE
, &show_logging_cmd
);
2816 install_element(VIEW_NODE
, &show_commandtree_cmd
);
2817 install_element(VIEW_NODE
, &echo_cmd
);
2818 install_element(VIEW_NODE
, &autocomplete_cmd
);
2819 install_element(VIEW_NODE
, &find_cmd
);
2821 install_element(ENABLE_NODE
, &config_end_cmd
);
2822 install_element(ENABLE_NODE
, &config_disable_cmd
);
2823 install_element(ENABLE_NODE
, &config_terminal_cmd
);
2824 install_element(ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2825 install_element(ENABLE_NODE
, &config_write_cmd
);
2826 install_element(ENABLE_NODE
, &show_running_config_cmd
);
2827 install_element(ENABLE_NODE
, &config_logmsg_cmd
);
2829 install_default(CONFIG_NODE
);
2832 workqueue_cmd_init();
2836 install_element(CONFIG_NODE
, &hostname_cmd
);
2837 install_element(CONFIG_NODE
, &no_hostname_cmd
);
2838 install_element(CONFIG_NODE
, &domainname_cmd
);
2839 install_element(CONFIG_NODE
, &no_domainname_cmd
);
2840 install_element(CONFIG_NODE
, &frr_version_defaults_cmd
);
2841 install_element(CONFIG_NODE
, &debug_memstats_cmd
);
2844 install_element(CONFIG_NODE
, &password_cmd
);
2845 install_element(CONFIG_NODE
, &no_password_cmd
);
2846 install_element(CONFIG_NODE
, &enable_password_cmd
);
2847 install_element(CONFIG_NODE
, &no_enable_password_cmd
);
2849 install_element(CONFIG_NODE
, &config_log_stdout_cmd
);
2850 install_element(CONFIG_NODE
, &no_config_log_stdout_cmd
);
2851 install_element(CONFIG_NODE
, &config_log_monitor_cmd
);
2852 install_element(CONFIG_NODE
, &no_config_log_monitor_cmd
);
2853 install_element(CONFIG_NODE
, &config_log_file_cmd
);
2854 install_element(CONFIG_NODE
, &no_config_log_file_cmd
);
2855 install_element(CONFIG_NODE
, &config_log_syslog_cmd
);
2856 install_element(CONFIG_NODE
, &no_config_log_syslog_cmd
);
2857 install_element(CONFIG_NODE
, &config_log_facility_cmd
);
2858 install_element(CONFIG_NODE
, &no_config_log_facility_cmd
);
2859 install_element(CONFIG_NODE
, &config_log_record_priority_cmd
);
2860 install_element(CONFIG_NODE
,
2861 &no_config_log_record_priority_cmd
);
2862 install_element(CONFIG_NODE
,
2863 &config_log_timestamp_precision_cmd
);
2864 install_element(CONFIG_NODE
,
2865 &no_config_log_timestamp_precision_cmd
);
2866 install_element(CONFIG_NODE
, &service_password_encrypt_cmd
);
2867 install_element(CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2868 install_element(CONFIG_NODE
, &banner_motd_default_cmd
);
2869 install_element(CONFIG_NODE
, &banner_motd_file_cmd
);
2870 install_element(CONFIG_NODE
, &no_banner_motd_cmd
);
2871 install_element(CONFIG_NODE
, &service_terminal_length_cmd
);
2872 install_element(CONFIG_NODE
, &no_service_terminal_length_cmd
);
2874 vrf_install_commands();
2878 grammar_sandbox_init();
2882 void cmd_terminate(void)
2884 struct cmd_node
*cmd_node
;
2886 hook_unregister(cmd_execute
, handle_pipe_action
);
2887 hook_unregister(cmd_execute_done
, handle_pipe_action_done
);
2890 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++)
2891 if ((cmd_node
= vector_slot(cmdvec
, i
)) != NULL
) {
2892 // deleting the graph delets the cmd_element as
2894 graph_delete_graph(cmd_node
->cmdgraph
);
2895 vector_free(cmd_node
->cmd_vector
);
2896 hash_clean(cmd_node
->cmd_hash
, NULL
);
2897 hash_free(cmd_node
->cmd_hash
);
2898 cmd_node
->cmd_hash
= NULL
;
2901 vector_free(cmdvec
);
2905 XFREE(MTYPE_HOST
, host
.name
);
2906 XFREE(MTYPE_HOST
, host
.domainname
);
2907 XFREE(MTYPE_HOST
, host
.password
);
2908 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2909 XFREE(MTYPE_HOST
, host
.enable
);
2910 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2911 XFREE(MTYPE_HOST
, host
.logfile
);
2912 XFREE(MTYPE_HOST
, host
.motdfile
);
2913 XFREE(MTYPE_HOST
, host
.config
);
2915 list_delete(&varhandlers
);