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
23 * along with GNU Zebra; see the file COPYING. If not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
33 #include <lib/version.h>
39 #include "workqueue.h"
41 #include "command_match.h"
44 DEFINE_MTYPE( LIB
, HOST
, "Host config")
45 DEFINE_MTYPE( LIB
, STRVEC
, "String vector")
46 DEFINE_MTYPE_STATIC(LIB
, CMD_TOKENS
, "Command Tokens")
47 DEFINE_MTYPE_STATIC(LIB
, CMD_DESC
, "Command Token Text")
48 DEFINE_MTYPE_STATIC(LIB
, CMD_TEXT
, "Command Token Help")
49 DEFINE_MTYPE( LIB
, CMD_ARG
, "Command Argument")
51 /* Command vector which includes some level of command lists. Normally
52 each daemon maintains each own cmdvec. */
55 /* Host information structure. */
58 /* Standard command node structures. */
59 static struct cmd_node auth_node
=
65 static struct cmd_node view_node
=
71 static struct cmd_node auth_enable_node
=
77 static struct cmd_node enable_node
=
83 static struct cmd_node config_node
=
90 /* Default motd string. */
91 static const char *default_motd
= FRR_DEFAULT_MOTD
;
93 static const struct facility_map
{
97 } syslog_facilities
[] =
99 { LOG_KERN
, "kern", 1 },
100 { LOG_USER
, "user", 2 },
101 { LOG_MAIL
, "mail", 1 },
102 { LOG_DAEMON
, "daemon", 1 },
103 { LOG_AUTH
, "auth", 1 },
104 { LOG_SYSLOG
, "syslog", 1 },
105 { LOG_LPR
, "lpr", 2 },
106 { LOG_NEWS
, "news", 1 },
107 { LOG_UUCP
, "uucp", 2 },
108 { LOG_CRON
, "cron", 1 },
110 { LOG_FTP
, "ftp", 1 },
112 { LOG_LOCAL0
, "local0", 6 },
113 { LOG_LOCAL1
, "local1", 6 },
114 { LOG_LOCAL2
, "local2", 6 },
115 { LOG_LOCAL3
, "local3", 6 },
116 { LOG_LOCAL4
, "local4", 6 },
117 { LOG_LOCAL5
, "local5", 6 },
118 { LOG_LOCAL6
, "local6", 6 },
119 { LOG_LOCAL7
, "local7", 6 },
124 facility_name(int facility
)
126 const struct facility_map
*fm
;
128 for (fm
= syslog_facilities
; fm
->name
; fm
++)
129 if (fm
->facility
== facility
)
135 facility_match(const char *str
)
137 const struct facility_map
*fm
;
139 for (fm
= syslog_facilities
; fm
->name
; fm
++)
140 if (!strncmp(str
,fm
->name
,fm
->match
))
146 level_match(const char *s
)
150 for ( level
= 0 ; zlog_priority
[level
] != NULL
; level
++ )
151 if (!strncmp (s
, zlog_priority
[level
], 2))
153 return ZLOG_DISABLED
;
156 /* This is called from main when a daemon is invoked with -v or --version. */
158 print_version (const char *progname
)
160 printf ("%s version %s\n", progname
, FRR_VERSION
);
161 printf ("%s\n", FRR_COPYRIGHT
);
162 printf ("configured with:\n\t%s\n", FRR_CONFIG_ARGS
);
166 /* Utility function to concatenate argv argument into a single string
167 with inserting ' ' character between each argument. */
169 argv_concat (struct cmd_token
**argv
, int argc
, int shift
)
177 for (i
= shift
; i
< argc
; i
++)
178 len
+= strlen(argv
[i
]->arg
)+1;
181 p
= str
= XMALLOC(MTYPE_TMP
, len
);
182 for (i
= shift
; i
< argc
; i
++)
185 memcpy(p
, argv
[i
]->arg
, (arglen
= strlen(argv
[i
]->arg
)));
194 * Convenience function for accessing argv data.
198 * @param text definition snippet of the desired token
199 * @param index the starting index, and where to store the
200 * index of the found token if it exists
201 * @return 1 if found, 0 otherwise
204 argv_find (struct cmd_token
**argv
, int argc
, const char *text
, int *index
)
207 for (int i
= *index
; i
< argc
&& found
== 0; i
++)
208 if ((found
= strmatch (text
, argv
[i
]->text
)))
214 cmd_hash_key (void *p
)
216 return (uintptr_t) p
;
220 cmd_hash_cmp (const void *a
, const void *b
)
225 /* Install top node of command vector. */
227 install_node (struct cmd_node
*node
,
228 int (*func
) (struct vty
*))
230 vector_set_index (cmdvec
, node
->node
, node
);
232 node
->cmdgraph
= graph_new ();
233 node
->cmd_vector
= vector_init (VECTOR_MIN_SIZE
);
235 struct cmd_token
*token
= new_cmd_token (START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
236 graph_new_node (node
->cmdgraph
, token
, (void (*)(void *)) &del_cmd_token
);
237 node
->cmd_hash
= hash_create (cmd_hash_key
, cmd_hash_cmp
);
241 * Tokenizes a string, storing tokens in a vector.
242 * Whitespace is ignored.
244 * Delimiter string = " \n\r\t".
246 * @param string to tokenize
247 * @return tokenized string
250 cmd_make_strvec (const char *string
)
252 if (!string
) return NULL
;
254 char *copy
, *copystart
;
255 copystart
= copy
= XSTRDUP (MTYPE_TMP
, string
);
257 // skip leading whitespace
258 while (isspace ((int) *copy
) && *copy
!= '\0') copy
++;
260 // if the entire string was whitespace or a comment, return
261 if (*copy
== '\0' || *copy
== '!' || *copy
== '#')
263 XFREE (MTYPE_TMP
, copystart
);
267 vector strvec
= vector_init (VECTOR_MIN_SIZE
);
268 const char *delim
= " \n\r\t", *tok
= NULL
;
271 tok
= strsep (©
, delim
);
273 vector_set (strvec
, XSTRDUP (MTYPE_STRVEC
, tok
));
276 XFREE (MTYPE_TMP
, copystart
);
280 /* Free allocated string vector. */
282 cmd_free_strvec (vector v
)
290 for (i
= 0; i
< vector_active (v
); i
++)
291 if ((cp
= vector_slot (v
, i
)) != NULL
)
292 XFREE (MTYPE_STRVEC
, cp
);
297 /* Return prompt character of specified node. */
299 cmd_prompt (enum node_type node
)
301 struct cmd_node
*cnode
;
303 cnode
= vector_slot (cmdvec
, node
);
304 return cnode
->prompt
;
308 cmd_nodes_link (struct graph_node
*from
, struct graph_node
*to
)
310 for (size_t i
= 0; i
< vector_active (from
->to
); i
++)
311 if (vector_slot (from
->to
, i
) == to
)
316 static bool cmd_nodes_equal (struct graph_node
*ga
, struct graph_node
*gb
);
318 /* returns a single node to be excluded as "next" from iteration
319 * - for JOIN_TKN, never continue back to the FORK_TKN
320 * - in all other cases, don't try the node itself (in case of "...")
322 static inline struct graph_node
*
323 cmd_loopstop(struct graph_node
*gn
)
325 struct cmd_token
*tok
= gn
->data
;
326 if (tok
->type
== JOIN_TKN
)
327 return tok
->forkjoin
;
333 cmd_subgraph_equal (struct graph_node
*ga
, struct graph_node
*gb
,
334 struct graph_node
*a_join
)
337 struct graph_node
*a_fork
, *b_fork
;
338 a_fork
= cmd_loopstop (ga
);
339 b_fork
= cmd_loopstop (gb
);
341 if (vector_active (ga
->to
) != vector_active (gb
->to
))
343 for (i
= 0; i
< vector_active (ga
->to
); i
++)
345 struct graph_node
*cga
= vector_slot (ga
->to
, i
);
347 for (j
= 0; j
< vector_active (gb
->to
); j
++)
349 struct graph_node
*cgb
= vector_slot (gb
->to
, i
);
351 if (cga
== a_fork
&& cgb
!= b_fork
)
353 if (cga
== a_fork
&& cgb
== b_fork
)
356 if (cmd_nodes_equal (cga
, cgb
))
360 if (cmd_subgraph_equal (cga
, cgb
, a_join
))
364 if (j
== vector_active (gb
->to
))
370 /* deep compare -- for FORK_TKN, the entire subgraph is compared.
371 * this is what's needed since we're not currently trying to partially
374 cmd_nodes_equal (struct graph_node
*ga
, struct graph_node
*gb
)
376 struct cmd_token
*a
= ga
->data
, *b
= gb
->data
;
378 if (a
->type
!= b
->type
|| a
->allowrepeat
!= b
->allowrepeat
)
380 if (a
->type
< SPECIAL_TKN
&& strcmp (a
->text
, b
->text
))
382 /* one a ..., the other not. */
383 if (cmd_nodes_link (ga
, ga
) != cmd_nodes_link (gb
, gb
))
389 return a
->min
== b
->min
&& a
->max
== b
->max
;
392 /* one is keywords, the other just option or selector ... */
393 if (cmd_nodes_link (a
->forkjoin
, ga
) != cmd_nodes_link (b
->forkjoin
, gb
))
395 if (cmd_nodes_link (ga
, a
->forkjoin
) != cmd_nodes_link (gb
, b
->forkjoin
))
397 return cmd_subgraph_equal (ga
, gb
, a
->forkjoin
);
405 cmd_fork_bump_attr (struct graph_node
*gn
, struct graph_node
*join
,
409 struct cmd_token
*tok
= gn
->data
;
410 struct graph_node
*stop
= cmd_loopstop (gn
);
413 for (i
= 0; i
< vector_active (gn
->to
); i
++)
415 struct graph_node
*next
= vector_slot (gn
->to
, i
);
416 if (next
== stop
|| next
== join
)
418 cmd_fork_bump_attr (next
, join
, attr
);
422 /* move an entire subtree from the temporary graph resulting from
423 * parse() into the permanent graph for the command node.
425 * this touches rather deeply into the graph code unfortunately.
428 cmd_reparent_tree (struct graph
*fromgraph
, struct graph
*tograph
,
429 struct graph_node
*node
)
431 struct graph_node
*stop
= cmd_loopstop (node
);
434 for (i
= 0; i
< vector_active (fromgraph
->nodes
); i
++)
435 if (vector_slot (fromgraph
->nodes
, i
) == node
)
437 /* agressive iteration punching through subgraphs - may hit some
438 * nodes twice. reparent only if found on old graph */
439 vector_unset (fromgraph
->nodes
, i
);
440 vector_set (tograph
->nodes
, node
);
444 for (i
= 0; i
< vector_active (node
->to
); i
++)
446 struct graph_node
*next
= vector_slot (node
->to
, i
);
448 cmd_reparent_tree (fromgraph
, tograph
, next
);
453 cmd_free_recur (struct graph
*graph
, struct graph_node
*node
,
454 struct graph_node
*stop
)
456 struct graph_node
*next
, *nstop
;
458 for (size_t i
= vector_active (node
->to
); i
; i
--)
460 next
= vector_slot (node
->to
, i
- 1);
463 nstop
= cmd_loopstop (next
);
465 cmd_free_recur (graph
, next
, nstop
);
466 cmd_free_recur (graph
, nstop
, stop
);
468 graph_delete_node (graph
, node
);
472 cmd_free_node (struct graph
*graph
, struct graph_node
*node
)
474 struct cmd_token
*tok
= node
->data
;
475 if (tok
->type
== JOIN_TKN
)
476 cmd_free_recur (graph
, tok
->forkjoin
, node
);
477 graph_delete_node (graph
, node
);
480 /* recursive graph merge. call with
482 * (which holds true for old == START_TKN, new == START_TKN)
485 cmd_merge_nodes (struct graph
*oldgraph
, struct graph
*newgraph
,
486 struct graph_node
*old
, struct graph_node
*new,
489 struct cmd_token
*tok
;
490 struct graph_node
*old_skip
, *new_skip
;
491 old_skip
= cmd_loopstop (old
);
492 new_skip
= cmd_loopstop (new);
494 assert (direction
== 1 || direction
== -1);
497 tok
->refcnt
+= direction
;
500 for (j
= 0; j
< vector_active (new->to
); j
++)
502 struct graph_node
*cnew
= vector_slot (new->to
, j
);
503 if (cnew
== new_skip
)
506 for (i
= 0; i
< vector_active (old
->to
); i
++)
508 struct graph_node
*cold
= vector_slot (old
->to
, i
);
509 if (cold
== old_skip
)
512 if (cmd_nodes_equal (cold
, cnew
))
514 struct cmd_token
*told
= cold
->data
, *tnew
= cnew
->data
;
516 if (told
->type
== END_TKN
)
520 graph_delete_node (oldgraph
, vector_slot (cold
->to
, 0));
521 graph_delete_node (oldgraph
, cold
);
524 /* force no-match handling to install END_TKN */
525 i
= vector_active (old
->to
);
529 /* the entire fork compared as equal, we continue after it. */
530 if (told
->type
== FORK_TKN
)
532 if (tnew
->attr
< told
->attr
&& direction
> 0)
533 cmd_fork_bump_attr (cold
, told
->forkjoin
, tnew
->attr
);
534 /* XXX: no reverse bump on uninstall */
535 told
= (cold
= told
->forkjoin
)->data
;
536 tnew
= (cnew
= tnew
->forkjoin
)->data
;
538 if (tnew
->attr
< told
->attr
)
539 told
->attr
= tnew
->attr
;
541 cmd_merge_nodes (oldgraph
, newgraph
, cold
, cnew
, direction
);
545 /* nothing found => add new to old */
546 if (i
== vector_active (old
->to
) && direction
> 0)
548 assert (vector_count (cnew
->from
) ==
549 cmd_nodes_link (cnew
, cnew
) ? 2 : 1);
550 graph_remove_edge (new, cnew
);
552 cmd_reparent_tree (newgraph
, oldgraph
, cnew
);
554 graph_add_edge (old
, cnew
);
559 cmd_free_node (oldgraph
, old
);
563 cmd_merge_graphs (struct graph
*old
, struct graph
*new, int direction
)
565 assert (vector_active (old
->nodes
) >= 1);
566 assert (vector_active (new->nodes
) >= 1);
568 cmd_merge_nodes (old
, new,
569 vector_slot (old
->nodes
, 0), vector_slot (new->nodes
, 0),
573 /* Install a command into a node. */
575 install_element (enum node_type ntype
, struct cmd_element
*cmd
)
577 struct cmd_node
*cnode
;
579 /* cmd_init hasn't been called */
582 fprintf (stderr
, "%s called before cmd_init, breakage likely\n",
587 cnode
= vector_slot (cmdvec
, ntype
);
591 fprintf (stderr
, "Command node %d doesn't exist, please check it\n",
596 if (hash_lookup (cnode
->cmd_hash
, cmd
) != NULL
)
599 "Multiple command installs to node %d of command:\n%s\n",
604 assert (hash_get (cnode
->cmd_hash
, cmd
, hash_alloc_intern
));
606 struct graph
*graph
= graph_new();
607 struct cmd_token
*token
= new_cmd_token (START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
608 graph_new_node (graph
, token
, (void (*)(void *)) &del_cmd_token
);
610 command_parse_format (graph
, cmd
);
611 cmd_merge_graphs (cnode
->cmdgraph
, graph
, +1);
612 graph_delete_graph (graph
);
614 vector_set (cnode
->cmd_vector
, cmd
);
616 if (ntype
== VIEW_NODE
)
617 install_element (ENABLE_NODE
, cmd
);
621 uninstall_element (enum node_type ntype
, struct cmd_element
*cmd
)
623 struct cmd_node
*cnode
;
625 /* cmd_init hasn't been called */
628 fprintf (stderr
, "%s called before cmd_init, breakage likely\n",
633 cnode
= vector_slot (cmdvec
, ntype
);
637 fprintf (stderr
, "Command node %d doesn't exist, please check it\n",
642 if (hash_release (cnode
->cmd_hash
, cmd
) == NULL
)
645 "Trying to uninstall non-installed command (node %d):\n%s\n",
650 vector_unset_value (cnode
->cmd_vector
, cmd
);
652 struct graph
*graph
= graph_new();
653 struct cmd_token
*token
= new_cmd_token (START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
654 graph_new_node (graph
, token
, (void (*)(void *)) &del_cmd_token
);
656 command_parse_format (graph
, cmd
);
657 cmd_merge_graphs (cnode
->cmdgraph
, graph
, -1);
658 graph_delete_graph (graph
);
660 if (ntype
== VIEW_NODE
)
661 uninstall_element (ENABLE_NODE
, cmd
);
665 static const unsigned char itoa64
[] =
666 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
669 to64(char *s
, long v
, int n
)
673 *s
++ = itoa64
[v
&0x3f];
679 zencrypt (const char *passwd
)
683 char *crypt (const char *, const char *);
687 to64(&salt
[0], random(), 3);
688 to64(&salt
[3], tv
.tv_usec
, 3);
691 return crypt (passwd
, salt
);
694 /* This function write configuration of this host. */
696 config_write_host (struct vty
*vty
)
699 vty_out (vty
, "hostname %s%s", host
.name
, VTY_NEWLINE
);
703 if (host
.password_encrypt
)
704 vty_out (vty
, "password 8 %s%s", host
.password_encrypt
, VTY_NEWLINE
);
705 if (host
.enable_encrypt
)
706 vty_out (vty
, "enable password 8 %s%s", host
.enable_encrypt
, VTY_NEWLINE
);
711 vty_out (vty
, "password %s%s", host
.password
, VTY_NEWLINE
);
713 vty_out (vty
, "enable password %s%s", host
.enable
, VTY_NEWLINE
);
716 if (zlog_default
->default_lvl
!= LOG_DEBUG
)
718 vty_out (vty
, "! N.B. The 'log trap' command is deprecated.%s",
720 vty_out (vty
, "log trap %s%s",
721 zlog_priority
[zlog_default
->default_lvl
], VTY_NEWLINE
);
724 if (host
.logfile
&& (zlog_default
->maxlvl
[ZLOG_DEST_FILE
] != ZLOG_DISABLED
))
726 vty_out (vty
, "log file %s", host
.logfile
);
727 if (zlog_default
->maxlvl
[ZLOG_DEST_FILE
] != zlog_default
->default_lvl
)
729 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_FILE
]]);
730 vty_out (vty
, "%s", VTY_NEWLINE
);
733 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != ZLOG_DISABLED
)
735 vty_out (vty
, "log stdout");
736 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != zlog_default
->default_lvl
)
738 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
]]);
739 vty_out (vty
, "%s", VTY_NEWLINE
);
742 if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
743 vty_out(vty
,"no log monitor%s",VTY_NEWLINE
);
744 else if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] != zlog_default
->default_lvl
)
745 vty_out(vty
,"log monitor %s%s",
746 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]],VTY_NEWLINE
);
748 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
750 vty_out (vty
, "log syslog");
751 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != zlog_default
->default_lvl
)
753 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
]]);
754 vty_out (vty
, "%s", VTY_NEWLINE
);
757 if (zlog_default
->facility
!= LOG_DAEMON
)
758 vty_out (vty
, "log facility %s%s",
759 facility_name(zlog_default
->facility
), VTY_NEWLINE
);
761 if (zlog_default
->record_priority
== 1)
762 vty_out (vty
, "log record-priority%s", VTY_NEWLINE
);
764 if (zlog_default
->timestamp_precision
> 0)
765 vty_out (vty
, "log timestamp precision %d%s",
766 zlog_default
->timestamp_precision
, VTY_NEWLINE
);
769 vty_out (vty
, "service advanced-vty%s", VTY_NEWLINE
);
772 vty_out (vty
, "service password-encryption%s", VTY_NEWLINE
);
775 vty_out (vty
, "service terminal-length %d%s", host
.lines
,
779 vty_out (vty
, "banner motd file %s%s", host
.motdfile
, VTY_NEWLINE
);
780 else if (! host
.motd
)
781 vty_out (vty
, "no banner motd%s", VTY_NEWLINE
);
786 /* Utility function for getting command graph. */
787 static struct graph
*
788 cmd_node_graph (vector v
, enum node_type ntype
)
790 struct cmd_node
*cnode
= vector_slot (v
, ntype
);
791 return cnode
->cmdgraph
;
795 cmd_try_do_shortcut (enum node_type node
, char* first_word
) {
796 if ( first_word
!= NULL
&&
799 node
!= AUTH_ENABLE_NODE
&&
800 node
!= ENABLE_NODE
&&
801 0 == strcmp( "do", first_word
) )
807 * Compare function for cmd_token.
808 * Used with qsort to sort command completions.
811 compare_completions (const void *fst
, const void *snd
)
813 struct cmd_token
*first
= *(struct cmd_token
**) fst
,
814 *secnd
= *(struct cmd_token
**) snd
;
815 return strcmp (first
->text
, secnd
->text
);
819 * Takes a list of completions returned by command_complete,
820 * dedeuplicates them based on both text and description,
821 * sorts them, and returns them as a vector.
823 * @param completions linked list of cmd_token
824 * @return deduplicated and sorted vector with
827 completions_to_vec (struct list
*completions
)
829 vector comps
= vector_init (VECTOR_MIN_SIZE
);
832 struct cmd_token
*token
, *cr
= NULL
;
833 unsigned int i
, exists
;
834 for (ALL_LIST_ELEMENTS_RO(completions
,ln
,token
))
836 if (token
->type
== END_TKN
&& (cr
= token
))
839 // linear search for token in completions vector
841 for (i
= 0; i
< vector_active (comps
) && !exists
; i
++)
843 struct cmd_token
*curr
= vector_slot (comps
, i
);
845 exists
= !strcmp (curr
->text
, token
->text
) &&
846 !strcmp (curr
->desc
, token
->desc
);
848 exists
= !strcmp (curr
->text
, token
->text
);
849 #endif /* VTYSH_DEBUG */
853 vector_set (comps
, token
);
858 vector_active (comps
),
860 &compare_completions
);
862 // make <cr> the first element, if it is present
865 vector_set_index (comps
, vector_active (comps
), NULL
);
866 memmove (comps
->index
+ 1, comps
->index
, (comps
->alloced
- 1) * sizeof (void *));
867 vector_set_index (comps
, 0, cr
);
873 * Generates a vector of cmd_token representing possible completions
874 * on the current input.
876 * @param vline the vectorized input line
877 * @param vty the vty with the node to match on
878 * @param status pointer to matcher status code
879 * @return vector of struct cmd_token * with possible completions
882 cmd_complete_command_real (vector vline
, struct vty
*vty
, int *status
)
884 struct list
*completions
;
885 struct graph
*cmdgraph
= cmd_node_graph (cmdvec
, vty
->node
);
887 enum matcher_rv rv
= command_complete (cmdgraph
, vline
, &completions
);
889 if (MATCHER_ERROR(rv
))
891 *status
= CMD_ERR_NO_MATCH
;
895 vector comps
= completions_to_vec (completions
);
896 list_delete (completions
);
898 // set status code appropriately
899 switch (vector_active (comps
))
902 *status
= CMD_ERR_NO_MATCH
;
905 *status
= CMD_COMPLETE_FULL_MATCH
;
908 *status
= CMD_COMPLETE_LIST_MATCH
;
915 cmd_describe_command (vector vline
, struct vty
*vty
, int *status
)
919 if ( cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0) ) )
921 enum node_type onode
;
922 vector shifted_vline
;
926 vty
->node
= ENABLE_NODE
;
927 /* We can try it on enable node, cos' the vty is authenticated */
929 shifted_vline
= vector_init (vector_count(vline
));
931 for (index
= 1; index
< vector_active (vline
); index
++)
933 vector_set_index (shifted_vline
, index
-1, vector_lookup(vline
, index
));
936 ret
= cmd_complete_command_real (shifted_vline
, vty
, status
);
938 vector_free(shifted_vline
);
943 return cmd_complete_command_real (vline
, vty
, status
);
947 * Generate possible tab-completions for the given input. This function only
948 * returns results that would result in a valid command if used as Readline
949 * completions (as is the case in vtysh). For instance, if the passed vline ends
950 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
952 * @param vline vectorized input line
954 * @param status location to store matcher status code in
955 * @return set of valid strings for use with Readline as tab-completions.
959 cmd_complete_command (vector vline
, struct vty
*vty
, int *status
)
962 int original_node
= vty
->node
;
963 vector input_line
= vector_init (vector_count (vline
));
965 // if the first token is 'do' we'll want to execute the command in the enable node
966 int do_shortcut
= cmd_try_do_shortcut (vty
->node
, vector_slot (vline
, 0));
967 vty
->node
= do_shortcut
? ENABLE_NODE
: original_node
;
969 // construct the input line we'll be matching on
970 unsigned int offset
= (do_shortcut
) ? 1 : 0;
971 for (unsigned index
= 0; index
+ offset
< vector_active (vline
); index
++)
972 vector_set_index (input_line
, index
, vector_lookup (vline
, index
+ offset
));
974 // get token completions -- this is a copying operation
975 vector comps
= NULL
, initial_comps
;
976 initial_comps
= cmd_complete_command_real (input_line
, vty
, status
);
978 if (!MATCHER_ERROR (*status
))
980 assert (initial_comps
);
981 // filter out everything that is not suitable for a tab-completion
982 comps
= vector_init (VECTOR_MIN_SIZE
);
983 for (unsigned int i
= 0; i
< vector_active(initial_comps
); i
++)
985 struct cmd_token
*token
= vector_slot (initial_comps
, i
);
986 if (token
->type
== WORD_TKN
)
987 vector_set (comps
, token
);
989 vector_free (initial_comps
);
991 // since we filtered results, we need to re-set status code
992 switch (vector_active (comps
))
995 *status
= CMD_ERR_NO_MATCH
;
998 *status
= CMD_COMPLETE_FULL_MATCH
;
1001 *status
= CMD_COMPLETE_LIST_MATCH
;
1004 // copy completions text into an array of char*
1005 ret
= XMALLOC (MTYPE_TMP
, (vector_active (comps
)+1) * sizeof (char *));
1007 for (i
= 0; i
< vector_active (comps
); i
++)
1009 struct cmd_token
*token
= vector_slot (comps
, i
);
1010 ret
[i
] = XSTRDUP (MTYPE_TMP
, token
->text
);
1011 vector_unset (comps
, i
);
1013 // set the last element to NULL, because this array is used in
1014 // a Readline completion_generator function which expects NULL
1015 // as a sentinel value
1017 vector_free (comps
);
1020 else if (initial_comps
)
1021 vector_free (initial_comps
);
1023 // comps should always be null here
1026 // free the adjusted input line
1027 vector_free (input_line
);
1029 // reset vty->node to its original value
1030 vty
->node
= original_node
;
1035 /* return parent node */
1036 /* MUST eventually converge on CONFIG_NODE */
1038 node_parent ( enum node_type node
)
1042 assert (node
> CONFIG_NODE
);
1046 case BGP_VPNV4_NODE
:
1047 case BGP_VPNV6_NODE
:
1048 case BGP_ENCAP_NODE
:
1049 case BGP_ENCAPV6_NODE
:
1050 case BGP_VRF_POLICY_NODE
:
1051 case BGP_VNC_DEFAULTS_NODE
:
1052 case BGP_VNC_NVE_GROUP_NODE
:
1053 case BGP_VNC_L2_GROUP_NODE
:
1055 case BGP_IPV4M_NODE
:
1057 case BGP_IPV6M_NODE
:
1060 case KEYCHAIN_KEY_NODE
:
1061 ret
= KEYCHAIN_NODE
;
1063 case LINK_PARAMS_NODE
:
1064 ret
= INTERFACE_NODE
;
1070 case LDP_IPV4_IFACE_NODE
:
1071 ret
= LDP_IPV4_NODE
;
1073 case LDP_IPV6_IFACE_NODE
:
1074 ret
= LDP_IPV6_NODE
;
1076 case LDP_PSEUDOWIRE_NODE
:
1077 ret
= LDP_L2VPN_NODE
;
1087 /* Execute command by argument vline vector. */
1089 cmd_execute_command_real (vector vline
,
1090 enum filter_type filter
,
1092 const struct cmd_element
**cmd
)
1094 struct list
*argv_list
;
1095 enum matcher_rv status
;
1096 const struct cmd_element
*matched_element
= NULL
;
1098 struct graph
*cmdgraph
= cmd_node_graph (cmdvec
, vty
->node
);
1099 status
= command_match (cmdgraph
, vline
, &argv_list
, &matched_element
);
1102 *cmd
= matched_element
;
1104 // if matcher error, return corresponding CMD_ERR
1105 if (MATCHER_ERROR(status
))
1108 list_delete (argv_list
);
1111 case MATCHER_INCOMPLETE
:
1112 return CMD_ERR_INCOMPLETE
;
1113 case MATCHER_AMBIGUOUS
:
1114 return CMD_ERR_AMBIGUOUS
;
1116 return CMD_ERR_NO_MATCH
;
1120 // build argv array from argv list
1121 struct cmd_token
**argv
= XMALLOC (MTYPE_TMP
, argv_list
->count
* sizeof (struct cmd_token
*));
1122 struct listnode
*ln
;
1123 struct cmd_token
*token
;
1125 for (ALL_LIST_ELEMENTS_RO(argv_list
,ln
,token
))
1128 int argc
= argv_list
->count
;
1131 if (matched_element
->daemon
)
1132 ret
= CMD_SUCCESS_DAEMON
;
1134 ret
= matched_element
->func (matched_element
, vty
, argc
, argv
);
1136 // delete list and cmd_token's in it
1137 list_delete (argv_list
);
1138 XFREE (MTYPE_TMP
, argv
);
1144 * Execute a given command, handling things like "do ..." and checking
1145 * whether the given command might apply at a parent node if doesn't
1146 * apply for the current node.
1148 * @param vline Command line input, vector of char* where each element is
1150 * @param vty The vty context in which the command should be executed.
1151 * @param cmd Pointer where the struct cmd_element of the matched command
1152 * will be stored, if any. May be set to NULL if this info is
1154 * @param vtysh If set != 0, don't lookup the command at parent nodes.
1155 * @return The status of the command that has been executed or an error code
1156 * as to why no command could be executed.
1159 cmd_execute_command (vector vline
, struct vty
*vty
,
1160 const struct cmd_element
**cmd
,
1163 int ret
, saved_ret
= 0;
1164 enum node_type onode
, try_node
;
1166 onode
= try_node
= vty
->node
;
1168 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0)))
1170 vector shifted_vline
;
1173 vty
->node
= ENABLE_NODE
;
1174 /* We can try it on enable node, cos' the vty is authenticated */
1176 shifted_vline
= vector_init (vector_count(vline
));
1178 for (index
= 1; index
< vector_active (vline
); index
++)
1179 vector_set_index (shifted_vline
, index
-1, vector_lookup(vline
, index
));
1181 ret
= cmd_execute_command_real (shifted_vline
, FILTER_RELAXED
, vty
, cmd
);
1183 vector_free(shifted_vline
);
1188 saved_ret
= ret
= cmd_execute_command_real (vline
, FILTER_RELAXED
, vty
, cmd
);
1193 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1195 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
1196 while (vty
->node
> CONFIG_NODE
)
1198 try_node
= node_parent(try_node
);
1199 vty
->node
= try_node
;
1200 ret
= cmd_execute_command_real (vline
, FILTER_RELAXED
, vty
, cmd
);
1201 if (ret
== CMD_SUCCESS
|| ret
== CMD_WARNING
)
1204 /* no command succeeded, reset the vty to the original node */
1208 /* return command status for original node */
1213 * Execute a given command, matching it strictly against the current node.
1214 * This mode is used when reading config files.
1216 * @param vline Command line input, vector of char* where each element is
1218 * @param vty The vty context in which the command should be executed.
1219 * @param cmd Pointer where the struct cmd_element* of the matched command
1220 * will be stored, if any. May be set to NULL if this info is
1222 * @return The status of the command that has been executed or an error code
1223 * as to why no command could be executed.
1226 cmd_execute_command_strict (vector vline
, struct vty
*vty
,
1227 const struct cmd_element
**cmd
)
1229 return cmd_execute_command_real(vline
, FILTER_STRICT
, vty
, cmd
);
1233 * Parse one line of config, walking up the parse tree attempting to find a match
1235 * @param vty The vty context in which the command should be executed.
1236 * @param cmd Pointer where the struct cmd_element* of the match command
1237 * will be stored, if any. May be set to NULL if this info is
1239 * @param use_daemon Boolean to control whether or not we match on CMD_SUCCESS_DAEMON
1241 * @return The status of the command that has been executed or an error code
1242 * as to why no command could be executed.
1245 command_config_read_one_line (struct vty
*vty
, const struct cmd_element
**cmd
, int use_daemon
)
1251 vline
= cmd_make_strvec (vty
->buf
);
1253 /* In case of comment line */
1257 /* Execute configuration command : this is strict match */
1258 ret
= cmd_execute_command_strict (vline
, vty
, cmd
);
1260 // Climb the tree and try the command again at each node
1261 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1262 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1263 ret
!= CMD_SUCCESS
&&
1264 ret
!= CMD_WARNING
&&
1265 vty
->node
!= CONFIG_NODE
) {
1267 saved_node
= vty
->node
;
1269 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1270 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1271 ret
!= CMD_SUCCESS
&&
1272 ret
!= CMD_WARNING
&&
1273 vty
->node
> CONFIG_NODE
) {
1274 vty
->node
= node_parent(vty
->node
);
1275 ret
= cmd_execute_command_strict (vline
, vty
, cmd
);
1278 // If climbing the tree did not work then ignore the command and
1279 // stay at the same node
1280 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1281 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1282 ret
!= CMD_SUCCESS
&&
1285 vty
->node
= saved_node
;
1289 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1290 memcpy (vty
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
1292 cmd_free_strvec (vline
);
1297 /* Configuration make from file. */
1299 config_from_file (struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
1301 int ret
, error_ret
=0;
1304 while (fgets (vty
->buf
, VTY_BUFSIZ
, fp
))
1309 ret
= command_config_read_one_line (vty
, NULL
, 0);
1311 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
&&
1312 ret
!= CMD_ERR_NOTHING_TODO
)
1323 /* Configuration from terminal */
1324 DEFUN (config_terminal
,
1325 config_terminal_cmd
,
1326 "configure terminal",
1327 "Configuration from vty interface\n"
1328 "Configuration terminal\n")
1330 if (vty_config_lock (vty
))
1331 vty
->node
= CONFIG_NODE
;
1334 vty_out (vty
, "VTY configuration is locked by other VTY%s", VTY_NEWLINE
);
1340 /* Enable command */
1344 "Turn on privileged mode command\n")
1346 /* If enable password is NULL, change to ENABLE_NODE */
1347 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
) ||
1348 vty
->type
== VTY_SHELL_SERV
)
1349 vty
->node
= ENABLE_NODE
;
1351 vty
->node
= AUTH_ENABLE_NODE
;
1356 /* Disable command */
1360 "Turn off privileged mode command\n")
1362 if (vty
->node
== ENABLE_NODE
)
1363 vty
->node
= VIEW_NODE
;
1367 /* Down vty node level. */
1371 "Exit current mode and down to previous mode\n")
1378 cmd_exit (struct vty
*vty
)
1384 if (vty_shell (vty
))
1387 vty
->status
= VTY_CLOSE
;
1390 vty
->node
= ENABLE_NODE
;
1391 vty_config_unlock (vty
);
1393 case INTERFACE_NODE
:
1403 case LDP_L2VPN_NODE
:
1410 vty
->node
= CONFIG_NODE
;
1413 case BGP_IPV4M_NODE
:
1414 case BGP_VPNV4_NODE
:
1415 case BGP_VPNV6_NODE
:
1416 case BGP_ENCAP_NODE
:
1417 case BGP_ENCAPV6_NODE
:
1418 case BGP_VRF_POLICY_NODE
:
1419 case BGP_VNC_DEFAULTS_NODE
:
1420 case BGP_VNC_NVE_GROUP_NODE
:
1421 case BGP_VNC_L2_GROUP_NODE
:
1423 case BGP_IPV6M_NODE
:
1424 vty
->node
= BGP_NODE
;
1428 vty
->node
= LDP_NODE
;
1430 case LDP_IPV4_IFACE_NODE
:
1431 vty
->node
= LDP_IPV4_NODE
;
1433 case LDP_IPV6_IFACE_NODE
:
1434 vty
->node
= LDP_IPV6_NODE
;
1436 case LDP_PSEUDOWIRE_NODE
:
1437 vty
->node
= LDP_L2VPN_NODE
;
1439 case KEYCHAIN_KEY_NODE
:
1440 vty
->node
= KEYCHAIN_NODE
;
1442 case LINK_PARAMS_NODE
:
1443 vty
->node
= INTERFACE_NODE
;
1454 "Exit current mode and down to previous mode\n")
1456 return config_exit (self
, vty
, argc
, argv
);
1460 /* End of configuration. */
1464 "End current mode and change to enable mode.")
1470 /* Nothing to do. */
1473 case INTERFACE_NODE
:
1480 case BGP_ENCAP_NODE
:
1481 case BGP_ENCAPV6_NODE
:
1482 case BGP_VRF_POLICY_NODE
:
1483 case BGP_VNC_DEFAULTS_NODE
:
1484 case BGP_VNC_NVE_GROUP_NODE
:
1485 case BGP_VNC_L2_GROUP_NODE
:
1486 case BGP_VPNV4_NODE
:
1487 case BGP_VPNV6_NODE
:
1489 case BGP_IPV4M_NODE
:
1491 case BGP_IPV6M_NODE
:
1498 case LDP_IPV4_IFACE_NODE
:
1499 case LDP_IPV6_IFACE_NODE
:
1500 case LDP_L2VPN_NODE
:
1501 case LDP_PSEUDOWIRE_NODE
:
1504 case KEYCHAIN_KEY_NODE
:
1508 case LINK_PARAMS_NODE
:
1509 vty_config_unlock (vty
);
1510 vty
->node
= ENABLE_NODE
;
1519 DEFUN (show_version
,
1523 "Displays zebra version\n")
1525 vty_out (vty
, "%s %s (%s).%s", FRR_FULL_NAME
, FRR_VERSION
,
1526 host
.name
? host
.name
: "",
1528 vty_out (vty
, "%s%s%s", FRR_COPYRIGHT
, GIT_INFO
, VTY_NEWLINE
);
1529 vty_out (vty
, "configured with:%s %s%s", VTY_NEWLINE
,
1530 FRR_CONFIG_ARGS
, VTY_NEWLINE
);
1535 /* Help display function for all node. */
1539 "Description of the interactive help system\n")
1542 "Quagga VTY provides advanced help feature. When you need help,%s\
1543 anytime at the command line please press '?'.%s\
1545 If nothing matches, the help list will be empty and you must backup%s\
1546 until entering a '?' shows the available options.%s\
1547 Two styles of help are provided:%s\
1548 1. Full help is available when you are ready to enter a%s\
1549 command argument (e.g. 'show ?') and describes each possible%s\
1551 2. Partial help is provided when an abbreviated argument is entered%s\
1552 and you want to know what arguments match the input%s\
1553 (e.g. 'show me?'.)%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
,
1554 VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
,
1555 VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1560 permute (struct graph_node
*start
, struct vty
*vty
)
1562 static struct list
*position
= NULL
;
1563 if (!position
) position
= list_new ();
1566 listnode_add (position
, start
);
1567 for (unsigned int i
= 0; i
< vector_active (start
->to
); i
++)
1569 struct graph_node
*gn
= vector_slot (start
->to
, i
);
1570 struct cmd_token
*tok
= gn
->data
;
1571 if (tok
->attr
== CMD_ATTR_HIDDEN
||
1572 tok
->attr
== CMD_ATTR_DEPRECATED
)
1574 else if (tok
->type
== END_TKN
|| gn
== start
)
1576 struct graph_node
*gnn
;
1577 struct listnode
*ln
;
1579 for (ALL_LIST_ELEMENTS_RO (position
,ln
,gnn
))
1581 struct cmd_token
*tt
= gnn
->data
;
1582 if (tt
->type
< SPECIAL_TKN
)
1583 vty_out (vty
, " %s", tt
->text
);
1586 vty_out (vty
, "...");
1587 vty_out (vty
, VTY_NEWLINE
);
1592 list_delete_node (position
, listtail(position
));
1596 cmd_list_cmds (struct vty
*vty
, int do_permute
)
1598 struct cmd_node
*node
= vector_slot (cmdvec
, vty
->node
);
1601 permute (vector_slot (node
->cmdgraph
->nodes
, 0), vty
);
1604 /* loop over all commands at this node */
1605 struct cmd_element
*element
= NULL
;
1606 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
); i
++)
1607 if ((element
= vector_slot (node
->cmd_vector
, i
)) &&
1608 element
->attr
!= CMD_ATTR_DEPRECATED
&&
1609 element
->attr
!= CMD_ATTR_HIDDEN
)
1610 vty_out (vty
, " %s%s", element
->string
, VTY_NEWLINE
);
1615 /* Help display function for all node. */
1618 "list [permutations]",
1619 "Print command list\n"
1620 "Print all possible command permutations\n")
1622 return cmd_list_cmds (vty
, argc
== 2);
1625 DEFUN (show_commandtree
,
1626 show_commandtree_cmd
,
1627 "show commandtree [permutations]",
1629 "Show command tree\n")
1631 return cmd_list_cmds (vty
, argc
== 3);
1634 /* Write current configuration into file. */
1636 DEFUN (config_write
,
1638 "write [<file|memory|terminal>]",
1639 "Write running configuration to memory, network, or terminal\n"
1640 "Write to configuration file\n"
1641 "Write configuration currently in memory\n"
1642 "Write configuration to terminal\n")
1647 struct cmd_node
*node
;
1648 char *config_file
, *slash
;
1649 char *config_file_tmp
= NULL
;
1650 char *config_file_sav
= NULL
;
1651 int ret
= CMD_WARNING
;
1652 struct vty
*file_vty
;
1653 struct stat conf_stat
;
1655 // if command was 'write terminal' or 'show running-config'
1656 if (argc
== 2 && (!strcmp(argv
[idx_type
]->text
, "terminal") ||
1657 !strcmp(argv
[0]->text
, "show")))
1659 if (vty
->type
== VTY_SHELL_SERV
)
1661 for (i
= 0; i
< vector_active (cmdvec
); i
++)
1662 if ((node
= vector_slot (cmdvec
, i
)) && node
->func
&& node
->vtysh
)
1664 if ((*node
->func
) (vty
))
1665 vty_out (vty
, "!%s", VTY_NEWLINE
);
1670 vty_out (vty
, "%sCurrent configuration:%s", VTY_NEWLINE
,
1672 vty_out (vty
, "!%s", VTY_NEWLINE
);
1674 for (i
= 0; i
< vector_active (cmdvec
); i
++)
1675 if ((node
= vector_slot (cmdvec
, i
)) && node
->func
)
1677 if ((*node
->func
) (vty
))
1678 vty_out (vty
, "!%s", VTY_NEWLINE
);
1680 vty_out (vty
, "end%s",VTY_NEWLINE
);
1688 /* Check and see if we are operating under vtysh configuration */
1689 if (host
.config
== NULL
)
1691 vty_out (vty
, "Can't save to configuration file, using vtysh.%s",
1697 config_file
= host
.config
;
1700 #define O_DIRECTORY 0
1702 slash
= strrchr (config_file
, '/');
1705 char *config_dir
= XSTRDUP (MTYPE_TMP
, config_file
);
1706 config_dir
[slash
- config_file
] = '\0';
1707 dirfd
= open(config_dir
, O_DIRECTORY
| O_RDONLY
);
1708 XFREE (MTYPE_TMP
, config_dir
);
1711 dirfd
= open(".", O_DIRECTORY
| O_RDONLY
);
1712 /* if dirfd is invalid, directory sync fails, but we're still OK */
1715 XMALLOC (MTYPE_TMP
, strlen (config_file
) + strlen (CONF_BACKUP_EXT
) + 1);
1716 strcpy (config_file_sav
, config_file
);
1717 strcat (config_file_sav
, CONF_BACKUP_EXT
);
1720 config_file_tmp
= XMALLOC (MTYPE_TMP
, strlen (config_file
) + 8);
1721 sprintf (config_file_tmp
, "%s.XXXXXX", config_file
);
1723 /* Open file to configuration write. */
1724 fd
= mkstemp (config_file_tmp
);
1727 vty_out (vty
, "Can't open configuration file %s.%s", config_file_tmp
,
1731 if (fchmod (fd
, CONFIGFILE_MASK
) != 0)
1733 vty_out (vty
, "Can't chmod configuration file %s: %s (%d).%s",
1734 config_file_tmp
, safe_strerror(errno
), errno
, VTY_NEWLINE
);
1738 /* Make vty for configuration file. */
1739 file_vty
= vty_new ();
1741 file_vty
->type
= VTY_FILE
;
1743 /* Config file header print. */
1744 vty_out (file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1745 vty_time_print (file_vty
, 1);
1746 vty_out (file_vty
, "!\n");
1748 for (i
= 0; i
< vector_active (cmdvec
); i
++)
1749 if ((node
= vector_slot (cmdvec
, i
)) && node
->func
)
1751 if ((*node
->func
) (file_vty
))
1752 vty_out (file_vty
, "!\n");
1754 vty_close (file_vty
);
1756 if (stat(config_file
, &conf_stat
) >= 0)
1758 if (unlink (config_file_sav
) != 0)
1759 if (errno
!= ENOENT
)
1761 vty_out (vty
, "Can't unlink backup configuration file %s.%s", config_file_sav
,
1765 if (link (config_file
, config_file_sav
) != 0)
1767 vty_out (vty
, "Can't backup old configuration file %s.%s", config_file_sav
,
1773 if (rename (config_file_tmp
, config_file
) != 0)
1775 vty_out (vty
, "Can't save configuration file %s.%s", config_file
,
1781 vty_out (vty
, "Configuration saved to %s%s", config_file
,
1786 if (ret
!= CMD_SUCCESS
)
1787 unlink (config_file_tmp
);
1789 XFREE (MTYPE_TMP
, config_file_tmp
);
1790 XFREE (MTYPE_TMP
, config_file_sav
);
1794 /* ALIAS_FIXME for 'write <terminal|memory>' */
1795 DEFUN (show_running_config
,
1796 show_running_config_cmd
,
1797 "show running-config",
1799 "running configuration (same as write terminal/memory)\n")
1801 return config_write (self
, vty
, argc
, argv
);
1804 /* ALIAS_FIXME for 'write file' */
1805 DEFUN (copy_runningconf_startupconf
,
1806 copy_runningconf_startupconf_cmd
,
1807 "copy running-config startup-config",
1808 "Copy configuration\n"
1809 "Copy running config to... \n"
1810 "Copy running config to startup config (same as write file)\n")
1812 return config_write (self
, vty
, argc
, argv
);
1816 /* Write startup configuration into the terminal. */
1817 DEFUN (show_startup_config
,
1818 show_startup_config_cmd
,
1819 "show startup-config",
1821 "Contents of startup configuration\n")
1828 if (host
.config
== NULL
)
1831 confp
= fopen (host
.config
, "r");
1834 vty_out (vty
, "Can't open configuration file [%s] due to '%s'%s",
1835 host
.config
, safe_strerror(errno
), VTY_NEWLINE
);
1839 while (fgets (buf
, BUFSIZ
, confp
))
1843 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1847 vty_out (vty
, "%s%s", buf
, VTY_NEWLINE
);
1856 cmd_hostname_set (const char *hostname
)
1858 XFREE (MTYPE_HOST
, host
.name
);
1859 host
.name
= hostname
? XSTRDUP (MTYPE_HOST
, hostname
) : NULL
;
1863 /* Hostname configuration */
1864 DEFUN (config_hostname
,
1867 "Set system's network name\n"
1868 "This system's network name\n")
1870 struct cmd_token
*word
= argv
[1];
1872 if (!isalpha((int) word
->arg
[0]))
1874 vty_out (vty
, "Please specify string starting with alphabet%s", VTY_NEWLINE
);
1878 return cmd_hostname_set (word
->arg
);
1881 DEFUN (config_no_hostname
,
1883 "no hostname [HOSTNAME]",
1885 "Reset system's network name\n"
1886 "Host name of this router\n")
1888 return cmd_hostname_set (NULL
);
1891 /* VTY interface password set. */
1892 DEFUN (config_password
,
1894 "password [(8-8)] WORD",
1895 "Assign the terminal connection password\n"
1896 "Specifies a HIDDEN password will follow\n"
1897 "The password string\n")
1901 if (argc
== 3) // '8' was specified
1904 XFREE (MTYPE_HOST
, host
.password
);
1905 host
.password
= NULL
;
1906 if (host
.password_encrypt
)
1907 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1908 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, argv
[idx_word
]->arg
);
1912 if (!isalnum (argv
[idx_8
]->arg
[0]))
1915 "Please specify string starting with alphanumeric%s", VTY_NEWLINE
);
1920 XFREE (MTYPE_HOST
, host
.password
);
1921 host
.password
= NULL
;
1925 if (host
.password_encrypt
)
1926 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1927 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (argv
[idx_8
]->arg
));
1930 host
.password
= XSTRDUP (MTYPE_HOST
, argv
[idx_8
]->arg
);
1935 /* VTY enable password set. */
1936 DEFUN (config_enable_password
,
1937 enable_password_cmd
,
1938 "enable password [(8-8)] WORD",
1939 "Modify enable password parameters\n"
1940 "Assign the privileged level password\n"
1941 "Specifies a HIDDEN password will follow\n"
1942 "The HIDDEN 'enable' password string\n")
1947 /* Crypt type is specified. */
1950 if (argv
[idx_8
]->arg
[0] == '8')
1953 XFREE (MTYPE_HOST
, host
.enable
);
1956 if (host
.enable_encrypt
)
1957 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1958 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, argv
[idx_word
]->arg
);
1964 vty_out (vty
, "Unknown encryption type.%s", VTY_NEWLINE
);
1969 if (!isalnum (argv
[idx_8
]->arg
[0]))
1972 "Please specify string starting with alphanumeric%s", VTY_NEWLINE
);
1977 XFREE (MTYPE_HOST
, host
.enable
);
1980 /* Plain password input. */
1983 if (host
.enable_encrypt
)
1984 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1985 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (argv
[idx_8
]->arg
));
1988 host
.enable
= XSTRDUP (MTYPE_HOST
, argv
[idx_8
]->arg
);
1993 /* VTY enable password delete. */
1994 DEFUN (no_config_enable_password
,
1995 no_enable_password_cmd
,
1996 "no enable password",
1998 "Modify enable password parameters\n"
1999 "Assign the privileged level password\n")
2002 XFREE (MTYPE_HOST
, host
.enable
);
2005 if (host
.enable_encrypt
)
2006 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
2007 host
.enable_encrypt
= NULL
;
2012 DEFUN (service_password_encrypt
,
2013 service_password_encrypt_cmd
,
2014 "service password-encryption",
2015 "Set up miscellaneous service\n"
2016 "Enable encrypted passwords\n")
2025 if (host
.password_encrypt
)
2026 XFREE (MTYPE_HOST
, host
.password_encrypt
);
2027 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (host
.password
));
2031 if (host
.enable_encrypt
)
2032 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
2033 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (host
.enable
));
2039 DEFUN (no_service_password_encrypt
,
2040 no_service_password_encrypt_cmd
,
2041 "no service password-encryption",
2043 "Set up miscellaneous service\n"
2044 "Enable encrypted passwords\n")
2051 if (host
.password_encrypt
)
2052 XFREE (MTYPE_HOST
, host
.password_encrypt
);
2053 host
.password_encrypt
= NULL
;
2055 if (host
.enable_encrypt
)
2056 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
2057 host
.enable_encrypt
= NULL
;
2062 DEFUN (config_terminal_length
,
2063 config_terminal_length_cmd
,
2064 "terminal length (0-512)",
2065 "Set terminal line parameters\n"
2066 "Set number of lines on a screen\n"
2067 "Number of lines on screen (0 for no pausing)\n")
2071 char *endptr
= NULL
;
2073 lines
= strtol (argv
[idx_number
]->arg
, &endptr
, 10);
2074 if (lines
< 0 || lines
> 512 || *endptr
!= '\0')
2076 vty_out (vty
, "length is malformed%s", VTY_NEWLINE
);
2084 DEFUN (config_terminal_no_length
,
2085 config_terminal_no_length_cmd
,
2086 "terminal no length",
2087 "Set terminal line parameters\n"
2089 "Set number of lines on a screen\n")
2095 DEFUN (service_terminal_length
,
2096 service_terminal_length_cmd
,
2097 "service terminal-length (0-512)",
2098 "Set up miscellaneous service\n"
2099 "System wide terminal length configuration\n"
2100 "Number of lines of VTY (0 means no line control)\n")
2104 char *endptr
= NULL
;
2106 lines
= strtol (argv
[idx_number
]->arg
, &endptr
, 10);
2107 if (lines
< 0 || lines
> 512 || *endptr
!= '\0')
2109 vty_out (vty
, "length is malformed%s", VTY_NEWLINE
);
2117 DEFUN (no_service_terminal_length
,
2118 no_service_terminal_length_cmd
,
2119 "no service terminal-length [(0-512)]",
2121 "Set up miscellaneous service\n"
2122 "System wide terminal length configuration\n"
2123 "Number of lines of VTY (0 means no line control)\n")
2129 DEFUN_HIDDEN (do_echo
,
2132 "Echo a message back to the vty\n"
2133 "The message to echo\n")
2137 vty_out (vty
, "%s%s", ((message
= argv_concat (argv
, argc
, 1)) ? message
: ""),
2140 XFREE(MTYPE_TMP
, message
);
2144 DEFUN (config_logmsg
,
2146 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
2147 "Send a message to enabled logging destinations\n"
2149 "The message to send\n")
2151 int idx_log_level
= 1;
2152 int idx_message
= 2;
2156 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2157 return CMD_ERR_NO_MATCH
;
2159 zlog(NULL
, level
, "%s", ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
2161 XFREE(MTYPE_TMP
, message
);
2166 DEFUN (show_logging
,
2170 "Show current logging configuration\n")
2172 struct zlog
*zl
= zlog_default
;
2174 vty_out (vty
, "Syslog logging: ");
2175 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
2176 vty_out (vty
, "disabled");
2178 vty_out (vty
, "level %s, facility %s, ident %s",
2179 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
2180 facility_name(zl
->facility
), zl
->ident
);
2181 vty_out (vty
, "%s", VTY_NEWLINE
);
2183 vty_out (vty
, "Stdout logging: ");
2184 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
2185 vty_out (vty
, "disabled");
2187 vty_out (vty
, "level %s",
2188 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
2189 vty_out (vty
, "%s", VTY_NEWLINE
);
2191 vty_out (vty
, "Monitor logging: ");
2192 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
2193 vty_out (vty
, "disabled");
2195 vty_out (vty
, "level %s",
2196 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
2197 vty_out (vty
, "%s", VTY_NEWLINE
);
2199 vty_out (vty
, "File logging: ");
2200 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) ||
2202 vty_out (vty
, "disabled");
2204 vty_out (vty
, "level %s, filename %s",
2205 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
2207 vty_out (vty
, "%s", VTY_NEWLINE
);
2209 vty_out (vty
, "Protocol name: %s%s",
2210 zlog_proto_names
[zl
->protocol
], VTY_NEWLINE
);
2211 vty_out (vty
, "Record priority: %s%s",
2212 (zl
->record_priority
? "enabled" : "disabled"), VTY_NEWLINE
);
2213 vty_out (vty
, "Timestamp precision: %d%s",
2214 zl
->timestamp_precision
, VTY_NEWLINE
);
2219 DEFUN (config_log_stdout
,
2220 config_log_stdout_cmd
,
2221 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2223 "Set stdout logging level\n"
2226 int idx_log_level
= 2;
2228 if (argc
== idx_log_level
)
2230 zlog_set_level (NULL
, ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2235 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2236 return CMD_ERR_NO_MATCH
;
2237 zlog_set_level (NULL
, ZLOG_DEST_STDOUT
, level
);
2241 DEFUN (no_config_log_stdout
,
2242 no_config_log_stdout_cmd
,
2243 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2246 "Cancel logging to stdout\n"
2249 zlog_set_level (NULL
, ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
2253 DEFUN (config_log_monitor
,
2254 config_log_monitor_cmd
,
2255 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2257 "Set terminal line (monitor) logging level\n"
2260 int idx_log_level
= 2;
2262 if (argc
== idx_log_level
)
2264 zlog_set_level (NULL
, ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
2269 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2270 return CMD_ERR_NO_MATCH
;
2271 zlog_set_level (NULL
, ZLOG_DEST_MONITOR
, level
);
2275 DEFUN (no_config_log_monitor
,
2276 no_config_log_monitor_cmd
,
2277 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2280 "Disable terminal line (monitor) logging\n"
2283 zlog_set_level (NULL
, ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
2288 set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
2292 const char *fullpath
;
2294 /* Path detection. */
2295 if (! IS_DIRECTORY_SEP (*fname
))
2297 char cwd
[MAXPATHLEN
+1];
2298 cwd
[MAXPATHLEN
] = '\0';
2300 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
2302 zlog_err ("config_log_file: Unable to alloc mem!");
2306 if ( (p
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + strlen (fname
) + 2))
2309 zlog_err ("config_log_file: Unable to alloc mem!");
2312 sprintf (p
, "%s/%s", cwd
, fname
);
2318 ret
= zlog_set_file (NULL
, fullpath
, loglevel
);
2321 XFREE (MTYPE_TMP
, p
);
2325 vty_out (vty
, "can't open logfile %s\n", fname
);
2330 XFREE (MTYPE_HOST
, host
.logfile
);
2332 host
.logfile
= XSTRDUP (MTYPE_HOST
, fname
);
2334 #if defined(HAVE_CUMULUS)
2335 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2336 zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] = ZLOG_DISABLED
;
2341 DEFUN (config_log_file
,
2342 config_log_file_cmd
,
2343 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2346 "Logging filename\n"
2349 int idx_filename
= 2;
2350 int idx_log_levels
= 3;
2354 if ((level
= level_match(argv
[idx_log_levels
]->arg
)) == ZLOG_DISABLED
)
2355 return CMD_ERR_NO_MATCH
;
2356 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2359 return set_log_file(vty
, argv
[idx_filename
]->arg
, zlog_default
->default_lvl
);
2362 DEFUN (no_config_log_file
,
2363 no_config_log_file_cmd
,
2364 "no log file [FILENAME [LEVEL]]",
2367 "Cancel logging to file\n"
2368 "Logging file name\n"
2369 "Logging file name\n"
2372 zlog_reset_file (NULL
);
2375 XFREE (MTYPE_HOST
, host
.logfile
);
2377 host
.logfile
= NULL
;
2382 DEFUN (config_log_syslog
,
2383 config_log_syslog_cmd
,
2384 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2386 "Set syslog logging level\n"
2389 int idx_log_levels
= 2;
2393 if ((level
= level_match (argv
[idx_log_levels
]->arg
)) == ZLOG_DISABLED
)
2394 return CMD_ERR_NO_MATCH
;
2395 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, level
);
2400 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2405 DEFUN (no_config_log_syslog
,
2406 no_config_log_syslog_cmd
,
2407 "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>]",
2410 "Cancel logging to syslog\n"
2414 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2418 DEFUN (config_log_facility
,
2419 config_log_facility_cmd
,
2420 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2422 "Facility parameter for syslog messages\n"
2426 int facility
= facility_match(argv
[idx_target
]->arg
);
2428 zlog_default
->facility
= facility
;
2432 DEFUN (no_config_log_facility
,
2433 no_config_log_facility_cmd
,
2434 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2437 "Reset syslog facility to default (daemon)\n"
2440 zlog_default
->facility
= LOG_DAEMON
;
2444 DEFUN_DEPRECATED (config_log_trap
,
2445 config_log_trap_cmd
,
2446 "log trap <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>",
2448 "(Deprecated) Set logging level and default for all destinations\n"
2454 if ((new_level
= level_match(argv
[2]->arg
)) == ZLOG_DISABLED
)
2455 return CMD_ERR_NO_MATCH
;
2457 zlog_default
->default_lvl
= new_level
;
2458 for (i
= 0; i
< ZLOG_NUM_DESTS
; i
++)
2459 if (zlog_default
->maxlvl
[i
] != ZLOG_DISABLED
)
2460 zlog_default
->maxlvl
[i
] = new_level
;
2464 DEFUN_DEPRECATED (no_config_log_trap
,
2465 no_config_log_trap_cmd
,
2466 "no log trap [emergencies|alerts|critical|errors|warnings|notifications|informational|debugging]",
2469 "Permit all logging information\n"
2472 zlog_default
->default_lvl
= LOG_DEBUG
;
2476 DEFUN (config_log_record_priority
,
2477 config_log_record_priority_cmd
,
2478 "log record-priority",
2480 "Log the priority of the message within the message\n")
2482 zlog_default
->record_priority
= 1 ;
2486 DEFUN (no_config_log_record_priority
,
2487 no_config_log_record_priority_cmd
,
2488 "no log record-priority",
2491 "Do not log the priority of the message within the message\n")
2493 zlog_default
->record_priority
= 0 ;
2497 DEFUN (config_log_timestamp_precision
,
2498 config_log_timestamp_precision_cmd
,
2499 "log timestamp precision (0-6)",
2501 "Timestamp configuration\n"
2502 "Set the timestamp precision\n"
2503 "Number of subsecond digits\n")
2506 VTY_GET_INTEGER_RANGE("Timestamp Precision",
2507 zlog_default
->timestamp_precision
, argv
[idx_number
]->arg
, 0, 6);
2511 DEFUN (no_config_log_timestamp_precision
,
2512 no_config_log_timestamp_precision_cmd
,
2513 "no log timestamp precision",
2516 "Timestamp configuration\n"
2517 "Reset the timestamp precision to the default value of 0\n")
2519 zlog_default
->timestamp_precision
= 0 ;
2524 cmd_banner_motd_file (const char *file
)
2526 int success
= CMD_SUCCESS
;
2531 rpath
= realpath (file
, p
);
2533 return CMD_ERR_NO_FILE
;
2534 in
= strstr (rpath
, SYSCONFDIR
);
2538 XFREE (MTYPE_HOST
, host
.motdfile
);
2539 host
.motdfile
= XSTRDUP (MTYPE_HOST
, file
);
2542 success
= CMD_WARNING
;
2547 DEFUN (banner_motd_file
,
2548 banner_motd_file_cmd
,
2549 "banner motd file FILE",
2552 "Banner from a file\n"
2556 const char *filename
= argv
[idx_file
]->arg
;
2557 int cmd
= cmd_banner_motd_file (filename
);
2559 if (cmd
== CMD_ERR_NO_FILE
)
2560 vty_out (vty
, "%s does not exist", filename
);
2561 else if (cmd
== CMD_WARNING
)
2562 vty_out (vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2567 DEFUN (banner_motd_default
,
2568 banner_motd_default_cmd
,
2569 "banner motd default",
2570 "Set banner string\n"
2571 "Strings for motd\n"
2574 host
.motd
= default_motd
;
2578 DEFUN (no_banner_motd
,
2582 "Set banner string\n"
2583 "Strings for motd\n")
2587 XFREE (MTYPE_HOST
, host
.motdfile
);
2588 host
.motdfile
= NULL
;
2592 /* Set config filename. Called from vty.c */
2594 host_config_set (const char *filename
)
2597 XFREE (MTYPE_HOST
, host
.config
);
2598 host
.config
= XSTRDUP (MTYPE_HOST
, filename
);
2602 host_config_get (void)
2608 install_default (enum node_type node
)
2610 install_element (node
, &config_exit_cmd
);
2611 install_element (node
, &config_quit_cmd
);
2612 install_element (node
, &config_end_cmd
);
2613 install_element (node
, &config_help_cmd
);
2614 install_element (node
, &config_list_cmd
);
2616 install_element (node
, &config_write_cmd
);
2617 install_element (node
, &show_running_config_cmd
);
2620 /* Initialize command interface. Install basic nodes and commands.
2622 * terminal = 0 -- vtysh / no logging, no config control
2623 * terminal = 1 -- normal daemon
2624 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2626 cmd_init (int terminal
)
2630 /* Allocate initial top vector of commands. */
2631 cmdvec
= vector_init (VECTOR_MIN_SIZE
);
2633 /* Default host value settings. */
2635 host
.password
= NULL
;
2637 host
.logfile
= NULL
;
2639 host
.noconfig
= (terminal
< 0);
2641 host
.motd
= default_motd
;
2642 host
.motdfile
= NULL
;
2644 /* Install top nodes. */
2645 install_node (&view_node
, NULL
);
2646 install_node (&enable_node
, NULL
);
2647 install_node (&auth_node
, NULL
);
2648 install_node (&auth_enable_node
, NULL
);
2649 install_node (&config_node
, config_write_host
);
2651 /* Each node's basic commands. */
2652 install_element (VIEW_NODE
, &show_version_cmd
);
2655 install_element (VIEW_NODE
, &config_list_cmd
);
2656 install_element (VIEW_NODE
, &config_exit_cmd
);
2657 install_element (VIEW_NODE
, &config_quit_cmd
);
2658 install_element (VIEW_NODE
, &config_help_cmd
);
2659 install_element (VIEW_NODE
, &config_enable_cmd
);
2660 install_element (VIEW_NODE
, &config_terminal_length_cmd
);
2661 install_element (VIEW_NODE
, &config_terminal_no_length_cmd
);
2662 install_element (VIEW_NODE
, &show_logging_cmd
);
2663 install_element (VIEW_NODE
, &show_commandtree_cmd
);
2664 install_element (VIEW_NODE
, &echo_cmd
);
2669 install_element (ENABLE_NODE
, &config_end_cmd
);
2670 install_element (ENABLE_NODE
, &config_disable_cmd
);
2671 install_element (ENABLE_NODE
, &config_terminal_cmd
);
2672 install_element (ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2673 install_element (ENABLE_NODE
, &config_write_cmd
);
2674 install_element (ENABLE_NODE
, &show_running_config_cmd
);
2676 install_element (ENABLE_NODE
, &show_startup_config_cmd
);
2680 install_element (ENABLE_NODE
, &config_logmsg_cmd
);
2681 install_default (CONFIG_NODE
);
2684 workqueue_cmd_init ();
2687 install_element (CONFIG_NODE
, &hostname_cmd
);
2688 install_element (CONFIG_NODE
, &no_hostname_cmd
);
2692 install_element (CONFIG_NODE
, &password_cmd
);
2693 install_element (CONFIG_NODE
, &enable_password_cmd
);
2694 install_element (CONFIG_NODE
, &no_enable_password_cmd
);
2696 install_element (CONFIG_NODE
, &config_log_stdout_cmd
);
2697 install_element (CONFIG_NODE
, &no_config_log_stdout_cmd
);
2698 install_element (CONFIG_NODE
, &config_log_monitor_cmd
);
2699 install_element (CONFIG_NODE
, &no_config_log_monitor_cmd
);
2700 install_element (CONFIG_NODE
, &config_log_file_cmd
);
2701 install_element (CONFIG_NODE
, &no_config_log_file_cmd
);
2702 install_element (CONFIG_NODE
, &config_log_syslog_cmd
);
2703 install_element (CONFIG_NODE
, &no_config_log_syslog_cmd
);
2704 install_element (CONFIG_NODE
, &config_log_facility_cmd
);
2705 install_element (CONFIG_NODE
, &no_config_log_facility_cmd
);
2706 install_element (CONFIG_NODE
, &config_log_trap_cmd
);
2707 install_element (CONFIG_NODE
, &no_config_log_trap_cmd
);
2708 install_element (CONFIG_NODE
, &config_log_record_priority_cmd
);
2709 install_element (CONFIG_NODE
, &no_config_log_record_priority_cmd
);
2710 install_element (CONFIG_NODE
, &config_log_timestamp_precision_cmd
);
2711 install_element (CONFIG_NODE
, &no_config_log_timestamp_precision_cmd
);
2712 install_element (CONFIG_NODE
, &service_password_encrypt_cmd
);
2713 install_element (CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2714 install_element (CONFIG_NODE
, &banner_motd_default_cmd
);
2715 install_element (CONFIG_NODE
, &banner_motd_file_cmd
);
2716 install_element (CONFIG_NODE
, &no_banner_motd_cmd
);
2717 install_element (CONFIG_NODE
, &service_terminal_length_cmd
);
2718 install_element (CONFIG_NODE
, &no_service_terminal_length_cmd
);
2720 vrf_install_commands ();
2722 srandom(time(NULL
));
2725 grammar_sandbox_init();
2730 new_cmd_token (enum cmd_token_type type
, u_char attr
,
2731 const char *text
, const char *desc
)
2733 struct cmd_token
*token
= XCALLOC (MTYPE_CMD_TOKENS
, sizeof (struct cmd_token
));
2736 token
->text
= text
? XSTRDUP (MTYPE_CMD_TEXT
, text
) : NULL
;
2737 token
->desc
= desc
? XSTRDUP (MTYPE_CMD_DESC
, desc
) : NULL
;
2740 token
->allowrepeat
= false;
2746 del_cmd_token (struct cmd_token
*token
)
2751 XFREE (MTYPE_CMD_TEXT
, token
->text
);
2753 XFREE (MTYPE_CMD_DESC
, token
->desc
);
2755 XFREE (MTYPE_CMD_ARG
, token
->arg
);
2757 XFREE (MTYPE_CMD_TOKENS
, token
);
2761 copy_cmd_token (struct cmd_token
*token
)
2763 struct cmd_token
*copy
= new_cmd_token (token
->type
, token
->attr
, NULL
, NULL
);
2764 copy
->max
= token
->max
;
2765 copy
->min
= token
->min
;
2766 copy
->text
= token
->text
? XSTRDUP (MTYPE_CMD_TEXT
, token
->text
) : NULL
;
2767 copy
->desc
= token
->desc
? XSTRDUP (MTYPE_CMD_DESC
, token
->desc
) : NULL
;
2768 copy
->arg
= token
->arg
? XSTRDUP (MTYPE_CMD_ARG
, token
->arg
) : NULL
;
2776 struct cmd_node
*cmd_node
;
2780 for (unsigned int i
= 0; i
< vector_active (cmdvec
); i
++)
2781 if ((cmd_node
= vector_slot (cmdvec
, i
)) != NULL
)
2783 // deleting the graph delets the cmd_element as well
2784 graph_delete_graph (cmd_node
->cmdgraph
);
2785 vector_free (cmd_node
->cmd_vector
);
2786 hash_clean (cmd_node
->cmd_hash
, NULL
);
2787 hash_free (cmd_node
->cmd_hash
);
2788 cmd_node
->cmd_hash
= NULL
;
2791 vector_free (cmdvec
);
2796 XFREE (MTYPE_HOST
, host
.name
);
2798 XFREE (MTYPE_HOST
, host
.password
);
2799 if (host
.password_encrypt
)
2800 XFREE (MTYPE_HOST
, host
.password_encrypt
);
2802 XFREE (MTYPE_HOST
, host
.enable
);
2803 if (host
.enable_encrypt
)
2804 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
2806 XFREE (MTYPE_HOST
, host
.logfile
);
2808 XFREE (MTYPE_HOST
, host
.motdfile
);
2810 XFREE (MTYPE_HOST
, host
.config
);