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
33 #include <lib/version.h>
39 #include "workqueue.h"
41 #include "command_match.h"
42 #include "command_graph.h"
48 DEFINE_MTYPE(LIB
, HOST
, "Host config")
49 DEFINE_MTYPE(LIB
, STRVEC
, "String vector")
50 DEFINE_MTYPE(LIB
, COMPLETION
, "Completion item")
52 const char *node_names
[] = {
55 "auth enable", // AUTH_ENABLE_NODE,
56 "enable", // ENABLE_NODE,
57 "config", // CONFIG_NODE,
58 "service", // SERVICE_NODE,
59 "debug", // DEBUG_NODE,
60 "vrf debug", // VRF_DEBUG_NODE,
61 "vnc debug", // DEBUG_VNC_NODE,
63 "keychain", // KEYCHAIN_NODE,
64 "keychain key", // KEYCHAIN_KEY_NODE,
65 "logical-router", // NS_NODE,
67 "interface", // INTERFACE_NODE,
68 "zebra", // ZEBRA_NODE,
69 "table", // TABLE_NODE,
71 "ripng", // RIPNG_NODE,
72 "babel", // BABEL_NODE,
73 "eigrp", // EIGRP_NODE,
75 "bgp vpnv4", // BGP_VPNV4_NODE,
76 "bgp vpnv6", // BGP_VPNV6_NODE,
77 "bgp ipv4 unicast", // BGP_IPV4_NODE,
78 "bgp ipv4 multicast", // BGP_IPV4M_NODE,
79 "bgp ipv4 labeled unicast", // BGP_IPV4L_NODE,
80 "bgp ipv6", // BGP_IPV6_NODE,
81 "bgp ipv6 multicast", // BGP_IPV6M_NODE,
82 "bgp ipv6 labeled unicast", // BGP_IPV6L_NODE,
83 "bgp vrf policy", // BGP_VRF_POLICY_NODE,
84 "bgp vnc defaults", // BGP_VNC_DEFAULTS_NODE,
85 "bgp vnc nve", // BGP_VNC_NVE_GROUP_NODE,
86 "bgp vnc l2", // BGP_VNC_L2_GROUP_NODE,
87 "rfp defaults", // RFP_DEFAULTS_NODE,
88 "bgp evpn", // BGP_EVPN_NODE,
90 "ospf6", // OSPF6_NODE,
92 "ldp ipv4", // LDP_IPV4_NODE,
93 "ldp ipv6", // LDP_IPV6_NODE,
94 "ldp ipv4 interface", // LDP_IPV4_IFACE_NODE,
95 "ldp ipv6 interface", // LDP_IPV6_IFACE_NODE,
96 "ldp l2vpn", // LDP_L2VPN_NODE,
97 "ldp", // LDP_PSEUDOWIRE_NODE,
100 "masc", // MASC_NODE,
101 "irdp", // IRDP_NODE,
102 "static ip", // IP_NODE,
103 "ipv4 access list", // ACCESS_NODE,
104 "ipv4 prefix list", // PREFIX_NODE,
105 "ipv6 access list", // ACCESS_IPV6_NODE,
106 "MAC access list", // ACCESS_MAC_NODE,
107 "ipv6 prefix list", // PREFIX_IPV6_NODE,
108 "as list", // AS_LIST_NODE,
109 "community list", // COMMUNITY_LIST_NODE,
110 "routemap", // RMAP_NODE,
111 "smux", // SMUX_NODE,
112 "dump", // DUMP_NODE,
113 "forwarding", // FORWARDING_NODE,
114 "protocol", // PROTOCOL_NODE,
115 "mpls", // MPLS_NODE,
118 "link-params", // LINK_PARAMS_NODE,
119 "bgp evpn vni", // BGP_EVPN_VNI_NODE,
122 /* Command vector which includes some level of command lists. Normally
123 each daemon maintains each own cmdvec. */
124 vector cmdvec
= NULL
;
126 /* Host information structure. */
130 * Returns host.name if any, otherwise
131 * it returns the system hostname.
133 const char *cmd_hostname_get(void)
139 * Returns unix domainname
141 const char *cmd_domainname_get(void)
143 return host
.domainname
;
146 /* Standard command node structures. */
147 static struct cmd_node auth_node
= {
148 AUTH_NODE
, "Password: ",
151 static struct cmd_node view_node
= {
155 static struct cmd_node auth_enable_node
= {
156 AUTH_ENABLE_NODE
, "Password: ",
159 static struct cmd_node enable_node
= {
163 static struct cmd_node config_node
= {CONFIG_NODE
, "%s(config)# ", 1};
165 /* Default motd string. */
166 static const char *default_motd
= FRR_DEFAULT_MOTD
;
168 static const struct facility_map
{
172 } syslog_facilities
[] = {
173 {LOG_KERN
, "kern", 1},
174 {LOG_USER
, "user", 2},
175 {LOG_MAIL
, "mail", 1},
176 {LOG_DAEMON
, "daemon", 1},
177 {LOG_AUTH
, "auth", 1},
178 {LOG_SYSLOG
, "syslog", 1},
180 {LOG_NEWS
, "news", 1},
181 {LOG_UUCP
, "uucp", 2},
182 {LOG_CRON
, "cron", 1},
186 {LOG_LOCAL0
, "local0", 6},
187 {LOG_LOCAL1
, "local1", 6},
188 {LOG_LOCAL2
, "local2", 6},
189 {LOG_LOCAL3
, "local3", 6},
190 {LOG_LOCAL4
, "local4", 6},
191 {LOG_LOCAL5
, "local5", 6},
192 {LOG_LOCAL6
, "local6", 6},
193 {LOG_LOCAL7
, "local7", 6},
197 static const char *facility_name(int facility
)
199 const struct facility_map
*fm
;
201 for (fm
= syslog_facilities
; fm
->name
; fm
++)
202 if (fm
->facility
== facility
)
207 static int facility_match(const char *str
)
209 const struct facility_map
*fm
;
211 for (fm
= syslog_facilities
; fm
->name
; fm
++)
212 if (!strncmp(str
, fm
->name
, fm
->match
))
217 static int level_match(const char *s
)
221 for (level
= 0; zlog_priority
[level
] != NULL
; level
++)
222 if (!strncmp(s
, zlog_priority
[level
], 2))
224 return ZLOG_DISABLED
;
227 /* This is called from main when a daemon is invoked with -v or --version. */
228 void print_version(const char *progname
)
230 printf("%s version %s\n", progname
, FRR_VERSION
);
231 printf("%s\n", FRR_COPYRIGHT
);
232 printf("configured with:\n\t%s\n", FRR_CONFIG_ARGS
);
236 /* Utility function to concatenate argv argument into a single string
237 with inserting ' ' character between each argument. */
238 char *argv_concat(struct cmd_token
**argv
, int argc
, int shift
)
246 for (i
= shift
; i
< argc
; i
++)
247 len
+= strlen(argv
[i
]->arg
) + 1;
250 p
= str
= XMALLOC(MTYPE_TMP
, len
);
251 for (i
= shift
; i
< argc
; i
++) {
253 memcpy(p
, argv
[i
]->arg
, (arglen
= strlen(argv
[i
]->arg
)));
262 * Convenience function for accessing argv data.
266 * @param text definition snippet of the desired token
267 * @param index the starting index, and where to store the
268 * index of the found token if it exists
269 * @return 1 if found, 0 otherwise
271 int argv_find(struct cmd_token
**argv
, int argc
, const char *text
, int *index
)
274 for (int i
= *index
; i
< argc
&& found
== 0; i
++)
275 if ((found
= strmatch(text
, argv
[i
]->text
)))
280 static unsigned int cmd_hash_key(void *p
)
282 int size
= sizeof(p
);
284 return jhash(p
, size
, 0);
287 static int cmd_hash_cmp(const void *a
, const void *b
)
292 /* Install top node of command vector. */
293 void install_node(struct cmd_node
*node
, int (*func
)(struct vty
*))
295 vector_set_index(cmdvec
, node
->node
, node
);
297 node
->cmdgraph
= graph_new();
298 node
->cmd_vector
= vector_init(VECTOR_MIN_SIZE
);
300 struct cmd_token
*token
=
301 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
302 graph_new_node(node
->cmdgraph
, token
,
303 (void (*)(void *)) & cmd_token_del
);
304 node
->cmd_hash
= hash_create_size(16, cmd_hash_key
,
310 * Tokenizes a string, storing tokens in a vector.
311 * Whitespace is ignored.
313 * Delimiter string = " \n\r\t".
315 * @param string to tokenize
316 * @return tokenized string
318 vector
cmd_make_strvec(const char *string
)
323 char *copy
, *copystart
;
324 copystart
= copy
= XSTRDUP(MTYPE_TMP
, string
);
326 // skip leading whitespace
327 while (isspace((int)*copy
) && *copy
!= '\0')
330 // if the entire string was whitespace or a comment, return
331 if (*copy
== '\0' || *copy
== '!' || *copy
== '#') {
332 XFREE(MTYPE_TMP
, copystart
);
336 vector strvec
= vector_init(VECTOR_MIN_SIZE
);
337 const char *delim
= " \n\r\t", *tok
= NULL
;
339 tok
= strsep(©
, delim
);
341 vector_set(strvec
, XSTRDUP(MTYPE_STRVEC
, tok
));
344 XFREE(MTYPE_TMP
, copystart
);
348 /* Free allocated string vector. */
349 void cmd_free_strvec(vector v
)
357 for (i
= 0; i
< vector_active(v
); i
++)
358 if ((cp
= vector_slot(v
, i
)) != NULL
)
359 XFREE(MTYPE_STRVEC
, cp
);
364 /* Return prompt character of specified node. */
365 const char *cmd_prompt(enum node_type node
)
367 struct cmd_node
*cnode
;
369 cnode
= vector_slot(cmdvec
, node
);
370 return cnode
->prompt
;
373 /* Install a command into a node. */
374 void install_element(enum node_type ntype
, struct cmd_element
*cmd
)
376 struct cmd_node
*cnode
;
378 /* cmd_init hasn't been called */
380 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
385 cnode
= vector_lookup(cmdvec
, ntype
);
390 "\tnode %d (%s) does not exist.\n"
391 "\tplease call install_node() before install_element()\n",
392 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
396 if (hash_lookup(cnode
->cmd_hash
, cmd
) != NULL
) {
399 "\tnode %d (%s) already has this command installed.\n"
400 "\tduplicate install_element call?\n",
401 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
405 assert(hash_get(cnode
->cmd_hash
, cmd
, hash_alloc_intern
));
407 struct graph
*graph
= graph_new();
408 struct cmd_token
*token
=
409 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
410 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
412 cmd_graph_parse(graph
, cmd
);
413 cmd_graph_names(graph
);
414 cmd_graph_merge(cnode
->cmdgraph
, graph
, +1);
415 graph_delete_graph(graph
);
417 vector_set(cnode
->cmd_vector
, cmd
);
419 if (ntype
== VIEW_NODE
)
420 install_element(ENABLE_NODE
, cmd
);
423 void uninstall_element(enum node_type ntype
, struct cmd_element
*cmd
)
425 struct cmd_node
*cnode
;
427 /* cmd_init hasn't been called */
429 fprintf(stderr
, "%s called before cmd_init, breakage likely\n",
434 cnode
= vector_lookup(cmdvec
, ntype
);
439 "\tnode %d (%s) does not exist.\n"
440 "\tplease call install_node() before uninstall_element()\n",
441 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
445 if (hash_release(cnode
->cmd_hash
, cmd
) == NULL
) {
448 "\tnode %d (%s) does not have this command installed.\n"
449 "\tduplicate uninstall_element call?\n",
450 cmd
->name
, cmd
->string
, ntype
, node_names
[ntype
]);
454 vector_unset_value(cnode
->cmd_vector
, cmd
);
456 struct graph
*graph
= graph_new();
457 struct cmd_token
*token
=
458 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
459 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
461 cmd_graph_parse(graph
, cmd
);
462 cmd_graph_names(graph
);
463 cmd_graph_merge(cnode
->cmdgraph
, graph
, -1);
464 graph_delete_graph(graph
);
466 if (ntype
== VIEW_NODE
)
467 uninstall_element(ENABLE_NODE
, cmd
);
471 static const unsigned char itoa64
[] =
472 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
474 static void to64(char *s
, long v
, int n
)
477 *s
++ = itoa64
[v
& 0x3f];
482 static char *zencrypt(const char *passwd
)
486 char *crypt(const char *, const char *);
488 gettimeofday(&tv
, 0);
490 to64(&salt
[0], random(), 3);
491 to64(&salt
[3], tv
.tv_usec
, 3);
494 return crypt(passwd
, salt
);
497 /* This function write configuration of this host. */
498 static int config_write_host(struct vty
*vty
)
500 if (cmd_hostname_get())
501 vty_out(vty
, "hostname %s\n", cmd_hostname_get());
504 if (host
.password_encrypt
)
505 vty_out(vty
, "password 8 %s\n", host
.password_encrypt
);
506 if (host
.enable_encrypt
)
507 vty_out(vty
, "enable password 8 %s\n",
508 host
.enable_encrypt
);
511 vty_out(vty
, "password %s\n", host
.password
);
513 vty_out(vty
, "enable password %s\n", host
.enable
);
516 if (zlog_default
->default_lvl
!= LOG_DEBUG
) {
517 vty_out(vty
, "! N.B. The 'log trap' command is deprecated.\n");
518 vty_out(vty
, "log trap %s\n",
519 zlog_priority
[zlog_default
->default_lvl
]);
523 && (zlog_default
->maxlvl
[ZLOG_DEST_FILE
] != ZLOG_DISABLED
)) {
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
[ZLOG_DEST_FILE
]]);
533 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != ZLOG_DISABLED
) {
534 vty_out(vty
, "log stdout");
535 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
]
536 != zlog_default
->default_lvl
)
538 zlog_priority
[zlog_default
->maxlvl
539 [ZLOG_DEST_STDOUT
]]);
543 if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
544 vty_out(vty
, "no log monitor\n");
545 else if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]
546 != zlog_default
->default_lvl
)
547 vty_out(vty
, "log monitor %s\n",
548 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]]);
550 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
) {
551 vty_out(vty
, "log syslog");
552 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
]
553 != zlog_default
->default_lvl
)
555 zlog_priority
[zlog_default
->maxlvl
556 [ZLOG_DEST_SYSLOG
]]);
560 if (zlog_default
->facility
!= LOG_DAEMON
)
561 vty_out(vty
, "log facility %s\n",
562 facility_name(zlog_default
->facility
));
564 if (zlog_default
->record_priority
== 1)
565 vty_out(vty
, "log record-priority\n");
567 if (zlog_default
->timestamp_precision
> 0)
568 vty_out(vty
, "log timestamp precision %d\n",
569 zlog_default
->timestamp_precision
);
572 vty_out(vty
, "service advanced-vty\n");
575 vty_out(vty
, "service password-encryption\n");
578 vty_out(vty
, "service terminal-length %d\n", host
.lines
);
581 vty_out(vty
, "banner motd file %s\n", host
.motdfile
);
583 vty_out(vty
, "no banner motd\n");
585 if (debug_memstats_at_exit
)
586 vty_out(vty
, "!\ndebug memstats-at-exit\n");
591 /* Utility function for getting command graph. */
592 static struct graph
*cmd_node_graph(vector v
, enum node_type ntype
)
594 struct cmd_node
*cnode
= vector_slot(v
, ntype
);
595 return cnode
->cmdgraph
;
598 static int cmd_try_do_shortcut(enum node_type node
, char *first_word
)
600 if (first_word
!= NULL
&& node
!= AUTH_NODE
&& node
!= VIEW_NODE
601 && node
!= AUTH_ENABLE_NODE
&& 0 == strcmp("do", first_word
))
607 * Compare function for cmd_token.
608 * Used with qsort to sort command completions.
610 static int compare_completions(const void *fst
, const void *snd
)
612 struct cmd_token
*first
= *(struct cmd_token
**)fst
,
613 *secnd
= *(struct cmd_token
**)snd
;
614 return strcmp(first
->text
, secnd
->text
);
618 * Takes a list of completions returned by command_complete,
619 * dedeuplicates them based on both text and description,
620 * sorts them, and returns them as a vector.
622 * @param completions linked list of cmd_token
623 * @return deduplicated and sorted vector with
625 vector
completions_to_vec(struct list
*completions
)
627 vector comps
= vector_init(VECTOR_MIN_SIZE
);
630 struct cmd_token
*token
, *cr
= NULL
;
631 unsigned int i
, exists
;
632 for (ALL_LIST_ELEMENTS_RO(completions
, ln
, token
)) {
633 if (token
->type
== END_TKN
&& (cr
= token
))
636 // linear search for token in completions vector
638 for (i
= 0; i
< vector_active(comps
) && !exists
; i
++) {
639 struct cmd_token
*curr
= vector_slot(comps
, i
);
641 exists
= !strcmp(curr
->text
, token
->text
)
642 && !strcmp(curr
->desc
, token
->desc
);
644 exists
= !strcmp(curr
->text
, token
->text
);
645 #endif /* VTYSH_DEBUG */
649 vector_set(comps
, token
);
653 qsort(comps
->index
, vector_active(comps
), sizeof(void *),
654 &compare_completions
);
656 // make <cr> the first element, if it is present
658 vector_set_index(comps
, vector_active(comps
), NULL
);
659 memmove(comps
->index
+ 1, comps
->index
,
660 (comps
->alloced
- 1) * sizeof(void *));
661 vector_set_index(comps
, 0, cr
);
667 * Generates a vector of cmd_token representing possible completions
668 * on the current input.
670 * @param vline the vectorized input line
671 * @param vty the vty with the node to match on
672 * @param status pointer to matcher status code
673 * @return vector of struct cmd_token * with possible completions
675 static vector
cmd_complete_command_real(vector vline
, struct vty
*vty
,
678 struct list
*completions
;
679 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
681 enum matcher_rv rv
= command_complete(cmdgraph
, vline
, &completions
);
683 if (MATCHER_ERROR(rv
)) {
684 *status
= CMD_ERR_NO_MATCH
;
688 vector comps
= completions_to_vec(completions
);
689 list_delete_and_null(&completions
);
691 // set status code appropriately
692 switch (vector_active(comps
)) {
694 *status
= CMD_ERR_NO_MATCH
;
697 *status
= CMD_COMPLETE_FULL_MATCH
;
700 *status
= CMD_COMPLETE_LIST_MATCH
;
706 vector
cmd_describe_command(vector vline
, struct vty
*vty
, int *status
)
710 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
711 enum node_type onode
;
712 vector shifted_vline
;
716 vty
->node
= ENABLE_NODE
;
717 /* We can try it on enable node, cos' the vty is authenticated
720 shifted_vline
= vector_init(vector_count(vline
));
722 for (index
= 1; index
< vector_active(vline
); index
++) {
723 vector_set_index(shifted_vline
, index
- 1,
724 vector_lookup(vline
, index
));
727 ret
= cmd_complete_command_real(shifted_vline
, vty
, status
);
729 vector_free(shifted_vline
);
734 return cmd_complete_command_real(vline
, vty
, status
);
737 static struct list
*varhandlers
= NULL
;
739 void cmd_variable_complete(struct cmd_token
*token
, const char *arg
,
743 const struct cmd_variable_handler
*cvh
;
747 tmpcomps
= arg
? vector_init(VECTOR_MIN_SIZE
) : comps
;
749 for (ALL_LIST_ELEMENTS_RO(varhandlers
, ln
, cvh
)) {
750 if (cvh
->tokenname
&& strcmp(cvh
->tokenname
, token
->text
))
752 if (cvh
->varname
&& (!token
->varname
753 || strcmp(cvh
->varname
, token
->varname
)))
755 cvh
->completions(tmpcomps
, token
);
763 for (i
= vector_active(tmpcomps
); i
; i
--) {
764 char *item
= vector_slot(tmpcomps
, i
- 1);
765 if (strlen(item
) >= argsz
&& !strncmp(item
, arg
, argsz
))
766 vector_set(comps
, item
);
768 XFREE(MTYPE_COMPLETION
, item
);
770 vector_free(tmpcomps
);
773 #define AUTOCOMP_INDENT 5
775 char *cmd_variable_comp2str(vector comps
, unsigned short cols
)
778 char *buf
= XCALLOC(MTYPE_TMP
, bsz
);
779 int lc
= AUTOCOMP_INDENT
;
780 size_t cs
= AUTOCOMP_INDENT
;
782 snprintf(buf
, bsz
, "%*s", AUTOCOMP_INDENT
, "");
783 for (size_t j
= 0; j
< vector_active(comps
); j
++) {
784 char *item
= vector_slot(comps
, j
);
785 itemlen
= strlen(item
);
787 if (cs
+ itemlen
+ AUTOCOMP_INDENT
+ 3 >= bsz
)
788 buf
= XREALLOC(MTYPE_TMP
, buf
, (bsz
*= 2));
790 if (lc
+ itemlen
+ 1 >= cols
) {
791 cs
+= snprintf(&buf
[cs
], bsz
- cs
, "\n%*s",
792 AUTOCOMP_INDENT
, "");
793 lc
= AUTOCOMP_INDENT
;
796 size_t written
= snprintf(&buf
[cs
], bsz
- cs
, "%s ", item
);
799 XFREE(MTYPE_COMPLETION
, item
);
800 vector_set_index(comps
, j
, NULL
);
805 void cmd_variable_handler_register(const struct cmd_variable_handler
*cvh
)
810 for (; cvh
->completions
; cvh
++)
811 listnode_add(varhandlers
, (void *)cvh
);
814 DEFUN_HIDDEN (autocomplete
,
816 "autocomplete TYPE TEXT VARNAME",
817 "Autocompletion handler (internal, for vtysh)\n"
820 "cmd_token->varname\n")
822 struct cmd_token tok
;
823 vector comps
= vector_init(32);
826 memset(&tok
, 0, sizeof(tok
));
827 tok
.type
= atoi(argv
[1]->arg
);
828 tok
.text
= argv
[2]->arg
;
829 tok
.varname
= argv
[3]->arg
;
830 if (!strcmp(tok
.varname
, "-"))
833 cmd_variable_complete(&tok
, NULL
, comps
);
835 for (i
= 0; i
< vector_active(comps
); i
++) {
836 char *text
= vector_slot(comps
, i
);
837 vty_out(vty
, "%s\n", text
);
838 XFREE(MTYPE_COMPLETION
, text
);
846 * Generate possible tab-completions for the given input. This function only
847 * returns results that would result in a valid command if used as Readline
848 * completions (as is the case in vtysh). For instance, if the passed vline ends
849 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
851 * @param vline vectorized input line
853 * @param status location to store matcher status code in
854 * @return set of valid strings for use with Readline as tab-completions.
857 char **cmd_complete_command(vector vline
, struct vty
*vty
, int *status
)
860 int original_node
= vty
->node
;
861 vector input_line
= vector_init(vector_count(vline
));
863 // if the first token is 'do' we'll want to execute the command in the
865 int do_shortcut
= cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0));
866 vty
->node
= do_shortcut
? ENABLE_NODE
: original_node
;
868 // construct the input line we'll be matching on
869 unsigned int offset
= (do_shortcut
) ? 1 : 0;
870 for (unsigned index
= 0; index
+ offset
< vector_active(vline
); index
++)
871 vector_set_index(input_line
, index
,
872 vector_lookup(vline
, index
+ offset
));
874 // get token completions -- this is a copying operation
875 vector comps
= NULL
, initial_comps
;
876 initial_comps
= cmd_complete_command_real(input_line
, vty
, status
);
878 if (!MATCHER_ERROR(*status
)) {
879 assert(initial_comps
);
880 // filter out everything that is not suitable for a
882 comps
= vector_init(VECTOR_MIN_SIZE
);
883 for (unsigned int i
= 0; i
< vector_active(initial_comps
);
885 struct cmd_token
*token
= vector_slot(initial_comps
, i
);
886 if (token
->type
== WORD_TKN
)
887 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
,
889 else if (IS_VARYING_TOKEN(token
->type
)) {
890 const char *ref
= vector_lookup(
891 vline
, vector_active(vline
) - 1);
892 cmd_variable_complete(token
, ref
, comps
);
895 vector_free(initial_comps
);
897 // since we filtered results, we need to re-set status code
898 switch (vector_active(comps
)) {
900 *status
= CMD_ERR_NO_MATCH
;
903 *status
= CMD_COMPLETE_FULL_MATCH
;
906 *status
= CMD_COMPLETE_LIST_MATCH
;
909 // copy completions text into an array of char*
910 ret
= XMALLOC(MTYPE_TMP
,
911 (vector_active(comps
) + 1) * sizeof(char *));
913 for (i
= 0; i
< vector_active(comps
); i
++) {
914 ret
[i
] = vector_slot(comps
, i
);
916 // set the last element to NULL, because this array is used in
917 // a Readline completion_generator function which expects NULL
918 // as a sentinel value
922 } else if (initial_comps
)
923 vector_free(initial_comps
);
925 // comps should always be null here
928 // free the adjusted input line
929 vector_free(input_line
);
931 // reset vty->node to its original value
932 vty
->node
= original_node
;
937 /* return parent node */
938 /* MUST eventually converge on CONFIG_NODE */
939 enum node_type
node_parent(enum node_type node
)
943 assert(node
> CONFIG_NODE
);
948 case BGP_VRF_POLICY_NODE
:
949 case BGP_VNC_DEFAULTS_NODE
:
950 case BGP_VNC_NVE_GROUP_NODE
:
951 case BGP_VNC_L2_GROUP_NODE
:
961 case BGP_EVPN_VNI_NODE
:
964 case KEYCHAIN_KEY_NODE
:
967 case LINK_PARAMS_NODE
:
968 ret
= INTERFACE_NODE
;
974 case LDP_IPV4_IFACE_NODE
:
977 case LDP_IPV6_IFACE_NODE
:
980 case LDP_PSEUDOWIRE_NODE
:
981 ret
= LDP_L2VPN_NODE
;
991 /* Execute command by argument vline vector. */
992 static int cmd_execute_command_real(vector vline
, enum filter_type filter
,
994 const struct cmd_element
**cmd
)
996 struct list
*argv_list
;
997 enum matcher_rv status
;
998 const struct cmd_element
*matched_element
= NULL
;
1000 struct graph
*cmdgraph
= cmd_node_graph(cmdvec
, vty
->node
);
1001 status
= command_match(cmdgraph
, vline
, &argv_list
, &matched_element
);
1004 *cmd
= matched_element
;
1006 // if matcher error, return corresponding CMD_ERR
1007 if (MATCHER_ERROR(status
)) {
1009 list_delete_and_null(&argv_list
);
1011 case MATCHER_INCOMPLETE
:
1012 return CMD_ERR_INCOMPLETE
;
1013 case MATCHER_AMBIGUOUS
:
1014 return CMD_ERR_AMBIGUOUS
;
1016 return CMD_ERR_NO_MATCH
;
1020 // build argv array from argv list
1021 struct cmd_token
**argv
= XMALLOC(
1022 MTYPE_TMP
, argv_list
->count
* sizeof(struct cmd_token
*));
1023 struct listnode
*ln
;
1024 struct cmd_token
*token
;
1026 for (ALL_LIST_ELEMENTS_RO(argv_list
, ln
, token
))
1029 int argc
= argv_list
->count
;
1032 if (matched_element
->daemon
)
1033 ret
= CMD_SUCCESS_DAEMON
;
1035 ret
= matched_element
->func(matched_element
, vty
, argc
, argv
);
1037 // delete list and cmd_token's in it
1038 list_delete_and_null(&argv_list
);
1039 XFREE(MTYPE_TMP
, argv
);
1045 * Execute a given command, handling things like "do ..." and checking
1046 * whether the given command might apply at a parent node if doesn't
1047 * apply for the current node.
1049 * @param vline Command line input, vector of char* where each element is
1051 * @param vty The vty context in which the command should be executed.
1052 * @param cmd Pointer where the struct cmd_element of the matched command
1053 * will be stored, if any. May be set to NULL if this info is
1055 * @param vtysh If set != 0, don't lookup the command at parent nodes.
1056 * @return The status of the command that has been executed or an error code
1057 * as to why no command could be executed.
1059 int cmd_execute_command(vector vline
, struct vty
*vty
,
1060 const struct cmd_element
**cmd
, int vtysh
)
1062 int ret
, saved_ret
= 0;
1063 enum node_type onode
, try_node
;
1065 onode
= try_node
= vty
->node
;
1067 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0))) {
1068 vector shifted_vline
;
1071 vty
->node
= ENABLE_NODE
;
1072 /* We can try it on enable node, cos' the vty is authenticated
1075 shifted_vline
= vector_init(vector_count(vline
));
1077 for (index
= 1; index
< vector_active(vline
); index
++)
1078 vector_set_index(shifted_vline
, index
- 1,
1079 vector_lookup(vline
, index
));
1081 ret
= cmd_execute_command_real(shifted_vline
, FILTER_RELAXED
,
1084 vector_free(shifted_vline
);
1090 cmd_execute_command_real(vline
, FILTER_RELAXED
, vty
, cmd
);
1095 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1096 && ret
!= CMD_NOT_MY_INSTANCE
&& ret
!= CMD_WARNING_CONFIG_FAILED
) {
1097 /* This assumes all nodes above CONFIG_NODE are childs of
1099 while (vty
->node
> CONFIG_NODE
) {
1100 try_node
= node_parent(try_node
);
1101 vty
->node
= try_node
;
1102 ret
= cmd_execute_command_real(vline
, FILTER_RELAXED
,
1104 if (ret
== CMD_SUCCESS
|| ret
== CMD_WARNING
1105 || ret
== CMD_NOT_MY_INSTANCE
1106 || ret
== CMD_WARNING_CONFIG_FAILED
)
1109 /* no command succeeded, reset the vty to the original node */
1113 /* return command status for original node */
1118 * Execute a given command, matching it strictly against the current node.
1119 * This mode is used when reading config files.
1121 * @param vline Command line input, vector of char* where each element is
1123 * @param vty The vty context in which the command should be executed.
1124 * @param cmd Pointer where the struct cmd_element* of the matched command
1125 * will be stored, if any. May be set to NULL if this info is
1127 * @return The status of the command that has been executed or an error code
1128 * as to why no command could be executed.
1130 int cmd_execute_command_strict(vector vline
, struct vty
*vty
,
1131 const struct cmd_element
**cmd
)
1133 return cmd_execute_command_real(vline
, FILTER_STRICT
, vty
, cmd
);
1137 * Parse one line of config, walking up the parse tree attempting to find a
1140 * @param vty The vty context in which the command should be executed.
1141 * @param cmd Pointer where the struct cmd_element* of the match command
1142 * will be stored, if any. May be set to NULL if this info is
1144 * @param use_daemon Boolean to control whether or not we match on
1145 * CMD_SUCCESS_DAEMON
1147 * @return The status of the command that has been executed or an error code
1148 * as to why no command could be executed.
1150 int command_config_read_one_line(struct vty
*vty
,
1151 const struct cmd_element
**cmd
, int use_daemon
)
1157 vline
= cmd_make_strvec(vty
->buf
);
1159 /* In case of comment line */
1163 /* Execute configuration command : this is strict match */
1164 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1166 // Climb the tree and try the command again at each node
1167 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1168 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1169 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1170 && ret
!= CMD_NOT_MY_INSTANCE
1171 && ret
!= CMD_WARNING_CONFIG_FAILED
1172 && vty
->node
!= CONFIG_NODE
) {
1174 saved_node
= vty
->node
;
1176 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1177 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1178 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1179 && vty
->node
> CONFIG_NODE
) {
1180 vty
->node
= node_parent(vty
->node
);
1181 ret
= cmd_execute_command_strict(vline
, vty
, cmd
);
1184 // If climbing the tree did not work then ignore the command and
1185 // stay at the same node
1186 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
)
1187 && !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
)
1188 && ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
) {
1189 vty
->node
= saved_node
;
1193 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1194 memcpy(vty
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
1196 cmd_free_strvec(vline
);
1201 /* Configuration make from file. */
1202 int config_from_file(struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
1204 int ret
, error_ret
= 0;
1207 while (fgets(vty
->buf
, VTY_BUFSIZ
, fp
)) {
1211 ret
= command_config_read_one_line(vty
, NULL
, 0);
1213 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
1214 && ret
!= CMD_ERR_NOTHING_TODO
)
1225 /* Configuration from terminal */
1226 DEFUN (config_terminal
,
1227 config_terminal_cmd
,
1228 "configure terminal",
1229 "Configuration from vty interface\n"
1230 "Configuration terminal\n")
1232 if (vty_config_lock(vty
))
1233 vty
->node
= CONFIG_NODE
;
1235 vty_out(vty
, "VTY configuration is locked by other VTY\n");
1236 return CMD_WARNING_CONFIG_FAILED
;
1241 /* Enable command */
1245 "Turn on privileged mode command\n")
1247 /* If enable password is NULL, change to ENABLE_NODE */
1248 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
)
1249 || vty
->type
== VTY_SHELL_SERV
)
1250 vty
->node
= ENABLE_NODE
;
1252 vty
->node
= AUTH_ENABLE_NODE
;
1257 /* Disable command */
1261 "Turn off privileged mode command\n")
1263 if (vty
->node
== ENABLE_NODE
)
1264 vty
->node
= VIEW_NODE
;
1268 /* Down vty node level. */
1272 "Exit current mode and down to previous mode\n")
1278 void cmd_exit(struct vty
*vty
)
1280 switch (vty
->node
) {
1286 vty
->status
= VTY_CLOSE
;
1289 vty
->node
= ENABLE_NODE
;
1290 vty_config_unlock(vty
);
1292 case INTERFACE_NODE
:
1305 case LDP_L2VPN_NODE
:
1312 vty
->node
= CONFIG_NODE
;
1315 case BGP_IPV4M_NODE
:
1316 case BGP_IPV4L_NODE
:
1317 case BGP_VPNV4_NODE
:
1318 case BGP_VPNV6_NODE
:
1319 case BGP_VRF_POLICY_NODE
:
1320 case BGP_VNC_DEFAULTS_NODE
:
1321 case BGP_VNC_NVE_GROUP_NODE
:
1322 case BGP_VNC_L2_GROUP_NODE
:
1324 case BGP_IPV6M_NODE
:
1326 case BGP_IPV6L_NODE
:
1327 vty
->node
= BGP_NODE
;
1329 case BGP_EVPN_VNI_NODE
:
1330 vty
->node
= BGP_EVPN_NODE
;
1334 vty
->node
= LDP_NODE
;
1336 case LDP_IPV4_IFACE_NODE
:
1337 vty
->node
= LDP_IPV4_NODE
;
1339 case LDP_IPV6_IFACE_NODE
:
1340 vty
->node
= LDP_IPV6_NODE
;
1342 case LDP_PSEUDOWIRE_NODE
:
1343 vty
->node
= LDP_L2VPN_NODE
;
1345 case KEYCHAIN_KEY_NODE
:
1346 vty
->node
= KEYCHAIN_NODE
;
1348 case LINK_PARAMS_NODE
:
1349 vty
->node
= INTERFACE_NODE
;
1360 "Exit current mode and down to previous mode\n")
1362 return config_exit(self
, vty
, argc
, argv
);
1366 /* End of configuration. */
1370 "End current mode and change to enable mode.")
1372 switch (vty
->node
) {
1375 /* Nothing to do. */
1378 case INTERFACE_NODE
:
1388 case BGP_VRF_POLICY_NODE
:
1389 case BGP_VNC_DEFAULTS_NODE
:
1390 case BGP_VNC_NVE_GROUP_NODE
:
1391 case BGP_VNC_L2_GROUP_NODE
:
1392 case BGP_VPNV4_NODE
:
1393 case BGP_VPNV6_NODE
:
1395 case BGP_IPV4M_NODE
:
1396 case BGP_IPV4L_NODE
:
1398 case BGP_IPV6M_NODE
:
1400 case BGP_EVPN_VNI_NODE
:
1401 case BGP_IPV6L_NODE
:
1408 case LDP_IPV4_IFACE_NODE
:
1409 case LDP_IPV6_IFACE_NODE
:
1410 case LDP_L2VPN_NODE
:
1411 case LDP_PSEUDOWIRE_NODE
:
1414 case KEYCHAIN_KEY_NODE
:
1418 case LINK_PARAMS_NODE
:
1419 vty_config_unlock(vty
);
1420 vty
->node
= ENABLE_NODE
;
1429 DEFUN (show_version
,
1433 "Displays zebra version\n")
1435 vty_out(vty
, "%s %s (%s).\n", FRR_FULL_NAME
, FRR_VERSION
,
1436 cmd_hostname_get() ? cmd_hostname_get() : "");
1437 vty_out(vty
, "%s%s\n", FRR_COPYRIGHT
, GIT_INFO
);
1438 vty_out(vty
, "configured with:\n %s\n", FRR_CONFIG_ARGS
);
1443 /* "Set" version ... ignore version tags */
1444 DEFUN (frr_version_defaults
,
1445 frr_version_defaults_cmd
,
1446 "frr <version|defaults> LINE...",
1447 "FRRouting global parameters\n"
1448 "version configuration was written by\n"
1449 "set of configuration defaults used\n"
1455 /* Help display function for all node. */
1459 "Description of the interactive help system\n")
1462 "Quagga VTY provides advanced help feature. When you need help,\n\
1463 anytime at the command line please press '?'.\n\
1465 If nothing matches, the help list will be empty and you must backup\n\
1466 until entering a '?' shows the available options.\n\
1467 Two styles of help are provided:\n\
1468 1. Full help is available when you are ready to enter a\n\
1469 command argument (e.g. 'show ?') and describes each possible\n\
1471 2. Partial help is provided when an abbreviated argument is entered\n\
1472 and you want to know what arguments match the input\n\
1473 (e.g. 'show me?'.)\n\n");
1477 static void permute(struct graph_node
*start
, struct vty
*vty
)
1479 static struct list
*position
= NULL
;
1481 position
= list_new();
1483 struct cmd_token
*stok
= start
->data
;
1484 struct graph_node
*gnn
;
1485 struct listnode
*ln
;
1488 listnode_add(position
, start
);
1489 for (unsigned int i
= 0; i
< vector_active(start
->to
); i
++) {
1490 struct graph_node
*gn
= vector_slot(start
->to
, i
);
1491 struct cmd_token
*tok
= gn
->data
;
1492 if (tok
->attr
== CMD_ATTR_HIDDEN
1493 || tok
->attr
== CMD_ATTR_DEPRECATED
)
1495 else if (tok
->type
== END_TKN
|| gn
== start
) {
1497 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
)) {
1498 struct cmd_token
*tt
= gnn
->data
;
1499 if (tt
->type
< SPECIAL_TKN
)
1500 vty_out(vty
, " %s", tt
->text
);
1503 vty_out(vty
, "...");
1507 if (stok
->type
== FORK_TKN
&& tok
->type
!= FORK_TKN
)
1508 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
))
1517 list_delete_node(position
, listtail(position
));
1520 int cmd_list_cmds(struct vty
*vty
, int do_permute
)
1522 struct cmd_node
*node
= vector_slot(cmdvec
, vty
->node
);
1525 permute(vector_slot(node
->cmdgraph
->nodes
, 0), vty
);
1527 /* loop over all commands at this node */
1528 struct cmd_element
*element
= NULL
;
1529 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
);
1531 if ((element
= vector_slot(node
->cmd_vector
, i
))
1532 && element
->attr
!= CMD_ATTR_DEPRECATED
1533 && element
->attr
!= CMD_ATTR_HIDDEN
)
1534 vty_out(vty
, " %s\n", element
->string
);
1539 /* Help display function for all node. */
1542 "list [permutations]",
1543 "Print command list\n"
1544 "Print all possible command permutations\n")
1546 return cmd_list_cmds(vty
, argc
== 2);
1549 DEFUN (show_commandtree
,
1550 show_commandtree_cmd
,
1551 "show commandtree [permutations]",
1553 "Show command tree\n"
1554 "Permutations that we are interested in\n")
1556 return cmd_list_cmds(vty
, argc
== 3);
1559 static void vty_write_config(struct vty
*vty
)
1562 struct cmd_node
*node
;
1564 if (vty
->type
== VTY_TERM
) {
1565 vty_out(vty
, "\nCurrent configuration:\n");
1566 vty_out(vty
, "!\n");
1569 vty_out(vty
, "frr version %s\n", FRR_VER_SHORT
);
1570 vty_out(vty
, "frr defaults %s\n", DFLT_NAME
);
1571 vty_out(vty
, "!\n");
1573 for (i
= 0; i
< vector_active(cmdvec
); i
++)
1574 if ((node
= vector_slot(cmdvec
, i
)) && node
->func
1575 && (node
->vtysh
|| vty
->type
!= VTY_SHELL
)) {
1576 if ((*node
->func
)(vty
))
1577 vty_out(vty
, "!\n");
1580 if (vty
->type
== VTY_TERM
) {
1581 vty_out(vty
, "end\n");
1585 /* Write current configuration into file. */
1587 DEFUN (config_write
,
1589 "write [<file|memory|terminal>]",
1590 "Write running configuration to memory, network, or terminal\n"
1591 "Write to configuration file\n"
1592 "Write configuration currently in memory\n"
1593 "Write configuration to terminal\n")
1597 char *config_file
, *slash
;
1598 char *config_file_tmp
= NULL
;
1599 char *config_file_sav
= NULL
;
1600 int ret
= CMD_WARNING
;
1601 struct vty
*file_vty
;
1602 struct stat conf_stat
;
1604 // if command was 'write terminal' or 'show running-config'
1605 if (argc
== 2 && (strmatch(argv
[idx_type
]->text
, "terminal")
1606 || strmatch(argv
[0]->text
, "show"))) {
1607 vty_write_config(vty
);
1614 /* Check and see if we are operating under vtysh configuration */
1615 if (host
.config
== NULL
) {
1617 "Can't save to configuration file, using vtysh.\n");
1622 config_file
= host
.config
;
1625 #define O_DIRECTORY 0
1627 slash
= strrchr(config_file
, '/');
1629 char *config_dir
= XSTRDUP(MTYPE_TMP
, config_file
);
1630 config_dir
[slash
- config_file
] = '\0';
1631 dirfd
= open(config_dir
, O_DIRECTORY
| O_RDONLY
);
1632 XFREE(MTYPE_TMP
, config_dir
);
1634 dirfd
= open(".", O_DIRECTORY
| O_RDONLY
);
1635 /* if dirfd is invalid, directory sync fails, but we're still OK */
1637 config_file_sav
= XMALLOC(
1638 MTYPE_TMP
, strlen(config_file
) + strlen(CONF_BACKUP_EXT
) + 1);
1639 strcpy(config_file_sav
, config_file
);
1640 strcat(config_file_sav
, CONF_BACKUP_EXT
);
1643 config_file_tmp
= XMALLOC(MTYPE_TMP
, strlen(config_file
) + 8);
1644 sprintf(config_file_tmp
, "%s.XXXXXX", config_file
);
1646 /* Open file to configuration write. */
1647 fd
= mkstemp(config_file_tmp
);
1649 vty_out(vty
, "Can't open configuration file %s.\n",
1653 if (fchmod(fd
, CONFIGFILE_MASK
) != 0) {
1654 vty_out(vty
, "Can't chmod configuration file %s: %s (%d).\n",
1655 config_file_tmp
, safe_strerror(errno
), errno
);
1659 /* Make vty for configuration file. */
1660 file_vty
= vty_new();
1662 file_vty
->type
= VTY_FILE
;
1664 /* Config file header print. */
1665 vty_out(file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1666 vty_time_print(file_vty
, 1);
1667 vty_out(file_vty
, "!\n");
1668 vty_write_config(file_vty
);
1669 vty_close(file_vty
);
1671 if (stat(config_file
, &conf_stat
) >= 0) {
1672 if (unlink(config_file_sav
) != 0)
1673 if (errno
!= ENOENT
) {
1675 "Can't unlink backup configuration file %s.\n",
1679 if (link(config_file
, config_file_sav
) != 0) {
1681 "Can't backup old configuration file %s.\n",
1688 if (rename(config_file_tmp
, config_file
) != 0) {
1689 vty_out(vty
, "Can't save configuration file %s.\n",
1696 vty_out(vty
, "Configuration saved to %s\n", config_file
);
1700 if (ret
!= CMD_SUCCESS
)
1701 unlink(config_file_tmp
);
1704 XFREE(MTYPE_TMP
, config_file_tmp
);
1705 XFREE(MTYPE_TMP
, config_file_sav
);
1709 /* ALIAS_FIXME for 'write <terminal|memory>' */
1710 DEFUN (show_running_config
,
1711 show_running_config_cmd
,
1712 "show running-config",
1714 "running configuration (same as write terminal/memory)\n")
1716 return config_write(self
, vty
, argc
, argv
);
1719 /* ALIAS_FIXME for 'write file' */
1720 DEFUN (copy_runningconf_startupconf
,
1721 copy_runningconf_startupconf_cmd
,
1722 "copy running-config startup-config",
1723 "Copy configuration\n"
1724 "Copy running config to... \n"
1725 "Copy running config to startup config (same as write file)\n")
1728 vty_write_config(vty
);
1733 /* Write startup configuration into the terminal. */
1734 DEFUN (show_startup_config
,
1735 show_startup_config_cmd
,
1736 "show startup-config",
1738 "Contents of startup configuration\n")
1745 if (host
.config
== NULL
)
1748 confp
= fopen(host
.config
, "r");
1749 if (confp
== NULL
) {
1750 vty_out(vty
, "Can't open configuration file [%s] due to '%s'\n",
1751 host
.config
, safe_strerror(errno
));
1755 while (fgets(buf
, BUFSIZ
, confp
)) {
1758 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1762 vty_out(vty
, "%s\n", buf
);
1770 int cmd_domainname_set(const char *domainname
)
1772 XFREE(MTYPE_HOST
, host
.domainname
);
1773 host
.domainname
= domainname
? XSTRDUP(MTYPE_HOST
, domainname
) : NULL
;
1777 /* Hostname configuration */
1778 DEFUN(config_domainname
,
1781 "Set system's domain name\n"
1782 "This system's domain name\n")
1784 struct cmd_token
*word
= argv
[1];
1786 if (!isalpha((int)word
->arg
[0])) {
1787 vty_out(vty
, "Please specify string starting with alphabet\n");
1788 return CMD_WARNING_CONFIG_FAILED
;
1791 return cmd_domainname_set(word
->arg
);
1794 DEFUN(config_no_domainname
,
1796 "no domainname [DOMAINNAME]",
1798 "Reset system's domain name\n"
1799 "domain name of this router\n")
1801 return cmd_domainname_set(NULL
);
1804 int cmd_hostname_set(const char *hostname
)
1806 XFREE(MTYPE_HOST
, host
.name
);
1807 host
.name
= hostname
? XSTRDUP(MTYPE_HOST
, hostname
) : NULL
;
1811 /* Hostname configuration */
1812 DEFUN (config_hostname
,
1815 "Set system's network name\n"
1816 "This system's network name\n")
1818 struct cmd_token
*word
= argv
[1];
1820 if (!isalpha((int)word
->arg
[0])) {
1821 vty_out(vty
, "Please specify string starting with alphabet\n");
1822 return CMD_WARNING_CONFIG_FAILED
;
1825 return cmd_hostname_set(word
->arg
);
1828 DEFUN (config_no_hostname
,
1830 "no hostname [HOSTNAME]",
1832 "Reset system's network name\n"
1833 "Host name of this router\n")
1835 return cmd_hostname_set(NULL
);
1838 /* VTY interface password set. */
1839 DEFUN (config_password
,
1841 "password [(8-8)] WORD",
1842 "Assign the terminal connection password\n"
1843 "Specifies a HIDDEN password will follow\n"
1844 "The password string\n")
1848 if (argc
== 3) // '8' was specified
1851 XFREE(MTYPE_HOST
, host
.password
);
1852 host
.password
= NULL
;
1853 if (host
.password_encrypt
)
1854 XFREE(MTYPE_HOST
, host
.password_encrypt
);
1855 host
.password_encrypt
=
1856 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
1860 if (!isalnum(argv
[idx_8
]->arg
[0])) {
1862 "Please specify string starting with alphanumeric\n");
1863 return CMD_WARNING_CONFIG_FAILED
;
1867 XFREE(MTYPE_HOST
, host
.password
);
1868 host
.password
= NULL
;
1871 if (host
.password_encrypt
)
1872 XFREE(MTYPE_HOST
, host
.password_encrypt
);
1873 host
.password_encrypt
=
1874 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
1876 host
.password
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
1881 /* VTY enable password set. */
1882 DEFUN (config_enable_password
,
1883 enable_password_cmd
,
1884 "enable password [(8-8)] WORD",
1885 "Modify enable password parameters\n"
1886 "Assign the privileged level password\n"
1887 "Specifies a HIDDEN password will follow\n"
1888 "The HIDDEN 'enable' password string\n")
1893 /* Crypt type is specified. */
1895 if (argv
[idx_8
]->arg
[0] == '8') {
1897 XFREE(MTYPE_HOST
, host
.enable
);
1900 if (host
.enable_encrypt
)
1901 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
1902 host
.enable_encrypt
=
1903 XSTRDUP(MTYPE_HOST
, argv
[idx_word
]->arg
);
1907 vty_out(vty
, "Unknown encryption type.\n");
1908 return CMD_WARNING_CONFIG_FAILED
;
1912 if (!isalnum(argv
[idx_8
]->arg
[0])) {
1914 "Please specify string starting with alphanumeric\n");
1915 return CMD_WARNING_CONFIG_FAILED
;
1919 XFREE(MTYPE_HOST
, host
.enable
);
1922 /* Plain password input. */
1924 if (host
.enable_encrypt
)
1925 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
1926 host
.enable_encrypt
=
1927 XSTRDUP(MTYPE_HOST
, zencrypt(argv
[idx_8
]->arg
));
1929 host
.enable
= XSTRDUP(MTYPE_HOST
, argv
[idx_8
]->arg
);
1934 /* VTY enable password delete. */
1935 DEFUN (no_config_enable_password
,
1936 no_enable_password_cmd
,
1937 "no enable password",
1939 "Modify enable password parameters\n"
1940 "Assign the privileged level password\n")
1943 XFREE(MTYPE_HOST
, host
.enable
);
1946 if (host
.enable_encrypt
)
1947 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
1948 host
.enable_encrypt
= NULL
;
1953 DEFUN (service_password_encrypt
,
1954 service_password_encrypt_cmd
,
1955 "service password-encryption",
1956 "Set up miscellaneous service\n"
1957 "Enable encrypted passwords\n")
1964 if (host
.password
) {
1965 if (host
.password_encrypt
)
1966 XFREE(MTYPE_HOST
, host
.password_encrypt
);
1967 host
.password_encrypt
=
1968 XSTRDUP(MTYPE_HOST
, zencrypt(host
.password
));
1971 if (host
.enable_encrypt
)
1972 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
1973 host
.enable_encrypt
=
1974 XSTRDUP(MTYPE_HOST
, zencrypt(host
.enable
));
1980 DEFUN (no_service_password_encrypt
,
1981 no_service_password_encrypt_cmd
,
1982 "no service password-encryption",
1984 "Set up miscellaneous service\n"
1985 "Enable encrypted passwords\n")
1992 if (host
.password_encrypt
)
1993 XFREE(MTYPE_HOST
, host
.password_encrypt
);
1994 host
.password_encrypt
= NULL
;
1996 if (host
.enable_encrypt
)
1997 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
1998 host
.enable_encrypt
= NULL
;
2003 DEFUN (config_terminal_length
,
2004 config_terminal_length_cmd
,
2005 "terminal length (0-512)",
2006 "Set terminal line parameters\n"
2007 "Set number of lines on a screen\n"
2008 "Number of lines on screen (0 for no pausing)\n")
2012 vty
->lines
= atoi(argv
[idx_number
]->arg
);
2017 DEFUN (config_terminal_no_length
,
2018 config_terminal_no_length_cmd
,
2019 "terminal no length",
2020 "Set terminal line parameters\n"
2022 "Set number of lines on a screen\n")
2028 DEFUN (service_terminal_length
,
2029 service_terminal_length_cmd
,
2030 "service terminal-length (0-512)",
2031 "Set up miscellaneous service\n"
2032 "System wide terminal length configuration\n"
2033 "Number of lines of VTY (0 means no line control)\n")
2037 host
.lines
= atoi(argv
[idx_number
]->arg
);
2042 DEFUN (no_service_terminal_length
,
2043 no_service_terminal_length_cmd
,
2044 "no service terminal-length [(0-512)]",
2046 "Set up miscellaneous service\n"
2047 "System wide terminal length configuration\n"
2048 "Number of lines of VTY (0 means no line control)\n")
2054 DEFUN_HIDDEN (do_echo
,
2057 "Echo a message back to the vty\n"
2058 "The message to echo\n")
2062 vty_out(vty
, "%s\n",
2063 ((message
= argv_concat(argv
, argc
, 1)) ? message
: ""));
2065 XFREE(MTYPE_TMP
, message
);
2069 DEFUN (config_logmsg
,
2071 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
2072 "Send a message to enabled logging destinations\n"
2074 "The message to send\n")
2076 int idx_log_level
= 1;
2077 int idx_message
= 2;
2081 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2082 return CMD_ERR_NO_MATCH
;
2085 ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
2087 XFREE(MTYPE_TMP
, message
);
2092 DEFUN (show_logging
,
2096 "Show current logging configuration\n")
2098 struct zlog
*zl
= zlog_default
;
2100 vty_out(vty
, "Syslog logging: ");
2101 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
2102 vty_out(vty
, "disabled");
2104 vty_out(vty
, "level %s, facility %s, ident %s",
2105 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
2106 facility_name(zl
->facility
), zl
->ident
);
2109 vty_out(vty
, "Stdout logging: ");
2110 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
2111 vty_out(vty
, "disabled");
2113 vty_out(vty
, "level %s",
2114 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
2117 vty_out(vty
, "Monitor logging: ");
2118 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
2119 vty_out(vty
, "disabled");
2121 vty_out(vty
, "level %s",
2122 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
2125 vty_out(vty
, "File logging: ");
2126 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) || !zl
->fp
)
2127 vty_out(vty
, "disabled");
2129 vty_out(vty
, "level %s, filename %s",
2130 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
2134 vty_out(vty
, "Protocol name: %s\n", zl
->protoname
);
2135 vty_out(vty
, "Record priority: %s\n",
2136 (zl
->record_priority
? "enabled" : "disabled"));
2137 vty_out(vty
, "Timestamp precision: %d\n", zl
->timestamp_precision
);
2142 DEFUN (config_log_stdout
,
2143 config_log_stdout_cmd
,
2144 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2146 "Set stdout logging level\n"
2149 int idx_log_level
= 2;
2151 if (argc
== idx_log_level
) {
2152 zlog_set_level(ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2157 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2158 return CMD_ERR_NO_MATCH
;
2159 zlog_set_level(ZLOG_DEST_STDOUT
, level
);
2163 DEFUN (no_config_log_stdout
,
2164 no_config_log_stdout_cmd
,
2165 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2168 "Cancel logging to stdout\n"
2171 zlog_set_level(ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
2175 DEFUN (config_log_monitor
,
2176 config_log_monitor_cmd
,
2177 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2179 "Set terminal line (monitor) logging level\n"
2182 int idx_log_level
= 2;
2184 if (argc
== idx_log_level
) {
2185 zlog_set_level(ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
2190 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2191 return CMD_ERR_NO_MATCH
;
2192 zlog_set_level(ZLOG_DEST_MONITOR
, level
);
2196 DEFUN (no_config_log_monitor
,
2197 no_config_log_monitor_cmd
,
2198 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2201 "Disable terminal line (monitor) logging\n"
2204 zlog_set_level(ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
2208 static int set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
2212 const char *fullpath
;
2214 /* Path detection. */
2215 if (!IS_DIRECTORY_SEP(*fname
)) {
2216 char cwd
[MAXPATHLEN
+ 1];
2217 cwd
[MAXPATHLEN
] = '\0';
2219 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2220 zlog_err("config_log_file: Unable to alloc mem!");
2221 return CMD_WARNING_CONFIG_FAILED
;
2224 if ((p
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + strlen(fname
) + 2))
2226 zlog_err("config_log_file: Unable to alloc mem!");
2227 return CMD_WARNING_CONFIG_FAILED
;
2229 sprintf(p
, "%s/%s", cwd
, fname
);
2234 ret
= zlog_set_file(fullpath
, loglevel
);
2237 XFREE(MTYPE_TMP
, p
);
2240 vty_out(vty
, "can't open logfile %s\n", fname
);
2241 return CMD_WARNING_CONFIG_FAILED
;
2245 XFREE(MTYPE_HOST
, host
.logfile
);
2247 host
.logfile
= XSTRDUP(MTYPE_HOST
, fname
);
2249 #if defined(HAVE_CUMULUS)
2250 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2251 zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] = ZLOG_DISABLED
;
2256 DEFUN (config_log_file
,
2257 config_log_file_cmd
,
2258 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2261 "Logging filename\n"
2264 int idx_filename
= 2;
2265 int idx_log_levels
= 3;
2268 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2270 return CMD_ERR_NO_MATCH
;
2271 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2273 return set_log_file(vty
, argv
[idx_filename
]->arg
,
2274 zlog_default
->default_lvl
);
2277 DEFUN (no_config_log_file
,
2278 no_config_log_file_cmd
,
2279 "no log file [FILENAME [LEVEL]]",
2282 "Cancel logging to file\n"
2283 "Logging file name\n"
2289 XFREE(MTYPE_HOST
, host
.logfile
);
2291 host
.logfile
= NULL
;
2296 DEFUN (config_log_syslog
,
2297 config_log_syslog_cmd
,
2298 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2300 "Set syslog logging level\n"
2303 int idx_log_levels
= 2;
2306 if ((level
= level_match(argv
[idx_log_levels
]->arg
))
2308 return CMD_ERR_NO_MATCH
;
2309 zlog_set_level(ZLOG_DEST_SYSLOG
, level
);
2312 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2317 DEFUN (no_config_log_syslog
,
2318 no_config_log_syslog_cmd
,
2319 "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>]",
2322 "Cancel logging to syslog\n"
2326 zlog_set_level(ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2330 DEFUN (config_log_facility
,
2331 config_log_facility_cmd
,
2332 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2334 "Facility parameter for syslog messages\n"
2338 int facility
= facility_match(argv
[idx_target
]->arg
);
2340 zlog_default
->facility
= facility
;
2344 DEFUN (no_config_log_facility
,
2345 no_config_log_facility_cmd
,
2346 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2349 "Reset syslog facility to default (daemon)\n"
2352 zlog_default
->facility
= LOG_DAEMON
;
2357 config_log_trap
, config_log_trap_cmd
,
2358 "log trap <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>",
2360 "(Deprecated) Set logging level and default for all destinations\n" LOG_LEVEL_DESC
)
2365 if ((new_level
= level_match(argv
[2]->arg
)) == ZLOG_DISABLED
)
2366 return CMD_ERR_NO_MATCH
;
2368 zlog_default
->default_lvl
= new_level
;
2369 for (i
= 0; i
< ZLOG_NUM_DESTS
; i
++)
2370 if (zlog_default
->maxlvl
[i
] != ZLOG_DISABLED
)
2371 zlog_default
->maxlvl
[i
] = new_level
;
2376 no_config_log_trap
, no_config_log_trap_cmd
,
2377 "no log trap [emergencies|alerts|critical|errors|warnings|notifications|informational|debugging]",
2380 "Permit all logging information\n" LOG_LEVEL_DESC
)
2382 zlog_default
->default_lvl
= LOG_DEBUG
;
2386 DEFUN (config_log_record_priority
,
2387 config_log_record_priority_cmd
,
2388 "log record-priority",
2390 "Log the priority of the message within the message\n")
2392 zlog_default
->record_priority
= 1;
2396 DEFUN (no_config_log_record_priority
,
2397 no_config_log_record_priority_cmd
,
2398 "no log record-priority",
2401 "Do not log the priority of the message within the message\n")
2403 zlog_default
->record_priority
= 0;
2407 DEFUN (config_log_timestamp_precision
,
2408 config_log_timestamp_precision_cmd
,
2409 "log timestamp precision (0-6)",
2411 "Timestamp configuration\n"
2412 "Set the timestamp precision\n"
2413 "Number of subsecond digits\n")
2416 zlog_default
->timestamp_precision
=
2417 strtoul(argv
[idx_number
]->arg
, NULL
, 10);
2421 DEFUN (no_config_log_timestamp_precision
,
2422 no_config_log_timestamp_precision_cmd
,
2423 "no log timestamp precision",
2426 "Timestamp configuration\n"
2427 "Reset the timestamp precision to the default value of 0\n")
2429 zlog_default
->timestamp_precision
= 0;
2433 DEFUN (debug_memstats
,
2435 "[no] debug memstats-at-exit",
2438 "Print memory type statistics at exit\n")
2440 debug_memstats_at_exit
= !!strcmp(argv
[0]->text
, "no");
2444 int cmd_banner_motd_file(const char *file
)
2446 int success
= CMD_SUCCESS
;
2451 rpath
= realpath(file
, p
);
2453 return CMD_ERR_NO_FILE
;
2454 in
= strstr(rpath
, SYSCONFDIR
);
2457 XFREE(MTYPE_HOST
, host
.motdfile
);
2458 host
.motdfile
= XSTRDUP(MTYPE_HOST
, file
);
2460 success
= CMD_WARNING_CONFIG_FAILED
;
2465 DEFUN (banner_motd_file
,
2466 banner_motd_file_cmd
,
2467 "banner motd file FILE",
2470 "Banner from a file\n"
2474 const char *filename
= argv
[idx_file
]->arg
;
2475 int cmd
= cmd_banner_motd_file(filename
);
2477 if (cmd
== CMD_ERR_NO_FILE
)
2478 vty_out(vty
, "%s does not exist", filename
);
2479 else if (cmd
== CMD_WARNING_CONFIG_FAILED
)
2480 vty_out(vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2485 DEFUN (banner_motd_default
,
2486 banner_motd_default_cmd
,
2487 "banner motd default",
2488 "Set banner string\n"
2489 "Strings for motd\n"
2492 host
.motd
= default_motd
;
2496 DEFUN (no_banner_motd
,
2500 "Set banner string\n"
2501 "Strings for motd\n")
2505 XFREE(MTYPE_HOST
, host
.motdfile
);
2506 host
.motdfile
= NULL
;
2513 "Find CLI command containing text\n"
2514 "Text to search for\n")
2516 char *text
= argv_concat(argv
, argc
, 1);
2517 const struct cmd_node
*node
;
2518 const struct cmd_element
*cli
;
2521 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++) {
2522 node
= vector_slot(cmdvec
, i
);
2525 clis
= node
->cmd_vector
;
2526 for (unsigned int j
= 0; j
< vector_active(clis
); j
++) {
2527 cli
= vector_slot(clis
, j
);
2528 if (strcasestr(cli
->string
, text
))
2529 vty_out(vty
, " (%s) %s\n",
2530 node_names
[node
->node
], cli
->string
);
2534 XFREE(MTYPE_TMP
, text
);
2539 /* Set config filename. Called from vty.c */
2540 void host_config_set(const char *filename
)
2543 XFREE(MTYPE_HOST
, host
.config
);
2544 host
.config
= XSTRDUP(MTYPE_HOST
, filename
);
2547 const char *host_config_get(void)
2552 void install_default(enum node_type node
)
2554 install_element(node
, &config_exit_cmd
);
2555 install_element(node
, &config_quit_cmd
);
2556 install_element(node
, &config_end_cmd
);
2557 install_element(node
, &config_help_cmd
);
2558 install_element(node
, &config_list_cmd
);
2559 install_element(node
, &find_cmd
);
2561 install_element(node
, &config_write_cmd
);
2562 install_element(node
, &show_running_config_cmd
);
2564 install_element(node
, &autocomplete_cmd
);
2567 /* Initialize command interface. Install basic nodes and commands.
2569 * terminal = 0 -- vtysh / no logging, no config control
2570 * terminal = 1 -- normal daemon
2571 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2572 void cmd_init(int terminal
)
2574 struct utsname names
;
2576 if (array_size(node_names
) != NODE_TYPE_MAX
)
2577 assert(!"Update the CLI node description array!");
2582 varhandlers
= list_new();
2584 /* Allocate initial top vector of commands. */
2585 cmdvec
= vector_init(VECTOR_MIN_SIZE
);
2587 /* Default host value settings. */
2588 host
.name
= XSTRDUP(MTYPE_HOST
, names
.nodename
);
2589 #ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
2590 if ((strcmp(names
.domainname
, "(none)") == 0))
2591 host
.domainname
= NULL
;
2593 host
.domainname
= XSTRDUP(MTYPE_HOST
, names
.domainname
);
2595 host
.domainname
= NULL
;
2597 host
.password
= NULL
;
2599 host
.logfile
= NULL
;
2601 host
.noconfig
= (terminal
< 0);
2603 host
.motd
= default_motd
;
2604 host
.motdfile
= NULL
;
2606 /* Install top nodes. */
2607 install_node(&view_node
, NULL
);
2608 install_node(&enable_node
, NULL
);
2609 install_node(&auth_node
, NULL
);
2610 install_node(&auth_enable_node
, NULL
);
2611 install_node(&config_node
, config_write_host
);
2613 /* Each node's basic commands. */
2614 install_element(VIEW_NODE
, &show_version_cmd
);
2615 install_element(ENABLE_NODE
, &show_startup_config_cmd
);
2616 install_element(ENABLE_NODE
, &debug_memstats_cmd
);
2619 install_element(VIEW_NODE
, &config_list_cmd
);
2620 install_element(VIEW_NODE
, &config_exit_cmd
);
2621 install_element(VIEW_NODE
, &config_quit_cmd
);
2622 install_element(VIEW_NODE
, &config_help_cmd
);
2623 install_element(VIEW_NODE
, &config_enable_cmd
);
2624 install_element(VIEW_NODE
, &config_terminal_length_cmd
);
2625 install_element(VIEW_NODE
, &config_terminal_no_length_cmd
);
2626 install_element(VIEW_NODE
, &show_logging_cmd
);
2627 install_element(VIEW_NODE
, &show_commandtree_cmd
);
2628 install_element(VIEW_NODE
, &echo_cmd
);
2629 install_element(VIEW_NODE
, &autocomplete_cmd
);
2630 install_element(VIEW_NODE
, &find_cmd
);
2632 install_element(ENABLE_NODE
, &config_end_cmd
);
2633 install_element(ENABLE_NODE
, &config_disable_cmd
);
2634 install_element(ENABLE_NODE
, &config_terminal_cmd
);
2635 install_element(ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2636 install_element(ENABLE_NODE
, &config_write_cmd
);
2637 install_element(ENABLE_NODE
, &show_running_config_cmd
);
2638 install_element(ENABLE_NODE
, &config_logmsg_cmd
);
2640 install_default(CONFIG_NODE
);
2643 workqueue_cmd_init();
2647 install_element(CONFIG_NODE
, &hostname_cmd
);
2648 install_element(CONFIG_NODE
, &no_hostname_cmd
);
2649 install_element(CONFIG_NODE
, &domainname_cmd
);
2650 install_element(CONFIG_NODE
, &no_domainname_cmd
);
2651 install_element(CONFIG_NODE
, &frr_version_defaults_cmd
);
2652 install_element(CONFIG_NODE
, &debug_memstats_cmd
);
2655 install_element(CONFIG_NODE
, &password_cmd
);
2656 install_element(CONFIG_NODE
, &enable_password_cmd
);
2657 install_element(CONFIG_NODE
, &no_enable_password_cmd
);
2659 install_element(CONFIG_NODE
, &config_log_stdout_cmd
);
2660 install_element(CONFIG_NODE
, &no_config_log_stdout_cmd
);
2661 install_element(CONFIG_NODE
, &config_log_monitor_cmd
);
2662 install_element(CONFIG_NODE
, &no_config_log_monitor_cmd
);
2663 install_element(CONFIG_NODE
, &config_log_file_cmd
);
2664 install_element(CONFIG_NODE
, &no_config_log_file_cmd
);
2665 install_element(CONFIG_NODE
, &config_log_syslog_cmd
);
2666 install_element(CONFIG_NODE
, &no_config_log_syslog_cmd
);
2667 install_element(CONFIG_NODE
, &config_log_facility_cmd
);
2668 install_element(CONFIG_NODE
, &no_config_log_facility_cmd
);
2669 install_element(CONFIG_NODE
, &config_log_trap_cmd
);
2670 install_element(CONFIG_NODE
, &no_config_log_trap_cmd
);
2671 install_element(CONFIG_NODE
, &config_log_record_priority_cmd
);
2672 install_element(CONFIG_NODE
,
2673 &no_config_log_record_priority_cmd
);
2674 install_element(CONFIG_NODE
,
2675 &config_log_timestamp_precision_cmd
);
2676 install_element(CONFIG_NODE
,
2677 &no_config_log_timestamp_precision_cmd
);
2678 install_element(CONFIG_NODE
, &service_password_encrypt_cmd
);
2679 install_element(CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2680 install_element(CONFIG_NODE
, &banner_motd_default_cmd
);
2681 install_element(CONFIG_NODE
, &banner_motd_file_cmd
);
2682 install_element(CONFIG_NODE
, &no_banner_motd_cmd
);
2683 install_element(CONFIG_NODE
, &service_terminal_length_cmd
);
2684 install_element(CONFIG_NODE
, &no_service_terminal_length_cmd
);
2686 vrf_install_commands();
2690 grammar_sandbox_init();
2694 void cmd_terminate()
2696 struct cmd_node
*cmd_node
;
2699 for (unsigned int i
= 0; i
< vector_active(cmdvec
); i
++)
2700 if ((cmd_node
= vector_slot(cmdvec
, i
)) != NULL
) {
2701 // deleting the graph delets the cmd_element as
2703 graph_delete_graph(cmd_node
->cmdgraph
);
2704 vector_free(cmd_node
->cmd_vector
);
2705 hash_clean(cmd_node
->cmd_hash
, NULL
);
2706 hash_free(cmd_node
->cmd_hash
);
2707 cmd_node
->cmd_hash
= NULL
;
2710 vector_free(cmdvec
);
2715 XFREE(MTYPE_HOST
, host
.name
);
2716 if (host
.domainname
)
2717 XFREE(MTYPE_HOST
, host
.domainname
);
2719 XFREE(MTYPE_HOST
, host
.password
);
2720 if (host
.password_encrypt
)
2721 XFREE(MTYPE_HOST
, host
.password_encrypt
);
2723 XFREE(MTYPE_HOST
, host
.enable
);
2724 if (host
.enable_encrypt
)
2725 XFREE(MTYPE_HOST
, host
.enable_encrypt
);
2727 XFREE(MTYPE_HOST
, host
.logfile
);
2729 XFREE(MTYPE_HOST
, host
.motdfile
);
2731 XFREE(MTYPE_HOST
, host
.config
);
2733 list_delete_and_null(&varhandlers
);