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
34 #include <lib/version.h>
40 #include "workqueue.h"
42 #include "command_match.h"
45 DEFINE_MTYPE( LIB
, HOST
, "Host config")
46 DEFINE_MTYPE( LIB
, STRVEC
, "String vector")
47 DEFINE_MTYPE_STATIC(LIB
, CMD_TOKENS
, "Command Tokens")
48 DEFINE_MTYPE_STATIC(LIB
, CMD_DESC
, "Command Token Text")
49 DEFINE_MTYPE_STATIC(LIB
, CMD_TEXT
, "Command Token Help")
50 DEFINE_MTYPE( LIB
, CMD_ARG
, "Command Argument")
52 /* Command vector which includes some level of command lists. Normally
53 each daemon maintains each own cmdvec. */
56 /* Host information structure. */
59 /* Standard command node structures. */
60 static struct cmd_node auth_node
=
66 static struct cmd_node view_node
=
72 static struct cmd_node auth_enable_node
=
78 static struct cmd_node enable_node
=
84 static struct cmd_node config_node
=
91 /* Default motd string. */
92 static const char *default_motd
= FRR_DEFAULT_MOTD
;
94 static const struct facility_map
{
98 } syslog_facilities
[] =
100 { LOG_KERN
, "kern", 1 },
101 { LOG_USER
, "user", 2 },
102 { LOG_MAIL
, "mail", 1 },
103 { LOG_DAEMON
, "daemon", 1 },
104 { LOG_AUTH
, "auth", 1 },
105 { LOG_SYSLOG
, "syslog", 1 },
106 { LOG_LPR
, "lpr", 2 },
107 { LOG_NEWS
, "news", 1 },
108 { LOG_UUCP
, "uucp", 2 },
109 { LOG_CRON
, "cron", 1 },
111 { LOG_FTP
, "ftp", 1 },
113 { LOG_LOCAL0
, "local0", 6 },
114 { LOG_LOCAL1
, "local1", 6 },
115 { LOG_LOCAL2
, "local2", 6 },
116 { LOG_LOCAL3
, "local3", 6 },
117 { LOG_LOCAL4
, "local4", 6 },
118 { LOG_LOCAL5
, "local5", 6 },
119 { LOG_LOCAL6
, "local6", 6 },
120 { LOG_LOCAL7
, "local7", 6 },
125 facility_name(int facility
)
127 const struct facility_map
*fm
;
129 for (fm
= syslog_facilities
; fm
->name
; fm
++)
130 if (fm
->facility
== facility
)
136 facility_match(const char *str
)
138 const struct facility_map
*fm
;
140 for (fm
= syslog_facilities
; fm
->name
; fm
++)
141 if (!strncmp(str
,fm
->name
,fm
->match
))
147 level_match(const char *s
)
151 for ( level
= 0 ; zlog_priority
[level
] != NULL
; level
++ )
152 if (!strncmp (s
, zlog_priority
[level
], 2))
154 return ZLOG_DISABLED
;
157 /* This is called from main when a daemon is invoked with -v or --version. */
159 print_version (const char *progname
)
161 printf ("%s version %s\n", progname
, FRR_VERSION
);
162 printf ("%s\n", FRR_COPYRIGHT
);
163 printf ("configured with:\n\t%s\n", FRR_CONFIG_ARGS
);
167 /* Utility function to concatenate argv argument into a single string
168 with inserting ' ' character between each argument. */
170 argv_concat (struct cmd_token
**argv
, int argc
, int shift
)
178 for (i
= shift
; i
< argc
; i
++)
179 len
+= strlen(argv
[i
]->arg
)+1;
182 p
= str
= XMALLOC(MTYPE_TMP
, len
);
183 for (i
= shift
; i
< argc
; i
++)
186 memcpy(p
, argv
[i
]->arg
, (arglen
= strlen(argv
[i
]->arg
)));
195 * Convenience function for accessing argv data.
199 * @param text definition snippet of the desired token
200 * @param index the starting index, and where to store the
201 * index of the found token if it exists
202 * @return 1 if found, 0 otherwise
205 argv_find (struct cmd_token
**argv
, int argc
, const char *text
, int *index
)
208 for (int i
= *index
; i
< argc
&& found
== 0; i
++)
209 if ((found
= strmatch (text
, argv
[i
]->text
)))
215 cmd_hash_key (void *p
)
217 return (uintptr_t) p
;
221 cmd_hash_cmp (const void *a
, const void *b
)
226 /* Install top node of command vector. */
228 install_node (struct cmd_node
*node
,
229 int (*func
) (struct vty
*))
231 vector_set_index (cmdvec
, node
->node
, node
);
233 node
->cmdgraph
= graph_new ();
234 node
->cmd_vector
= vector_init (VECTOR_MIN_SIZE
);
236 struct cmd_token
*token
= new_cmd_token (START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
237 graph_new_node (node
->cmdgraph
, token
, (void (*)(void *)) &del_cmd_token
);
238 node
->cmd_hash
= hash_create (cmd_hash_key
, cmd_hash_cmp
);
242 * Tokenizes a string, storing tokens in a vector.
243 * Whitespace is ignored.
245 * Delimiter string = " \n\r\t".
247 * @param string to tokenize
248 * @return tokenized string
251 cmd_make_strvec (const char *string
)
253 if (!string
) return NULL
;
255 char *copy
, *copystart
;
256 copystart
= copy
= XSTRDUP (MTYPE_TMP
, string
);
258 // skip leading whitespace
259 while (isspace ((int) *copy
) && *copy
!= '\0') copy
++;
261 // if the entire string was whitespace or a comment, return
262 if (*copy
== '\0' || *copy
== '!' || *copy
== '#')
264 XFREE (MTYPE_TMP
, copystart
);
268 vector strvec
= vector_init (VECTOR_MIN_SIZE
);
269 const char *delim
= " \n\r\t", *tok
= NULL
;
272 tok
= strsep (©
, delim
);
274 vector_set (strvec
, XSTRDUP (MTYPE_STRVEC
, tok
));
277 XFREE (MTYPE_TMP
, copystart
);
281 /* Free allocated string vector. */
283 cmd_free_strvec (vector v
)
291 for (i
= 0; i
< vector_active (v
); i
++)
292 if ((cp
= vector_slot (v
, i
)) != NULL
)
293 XFREE (MTYPE_STRVEC
, cp
);
298 /* Return prompt character of specified node. */
300 cmd_prompt (enum node_type node
)
302 struct cmd_node
*cnode
;
304 cnode
= vector_slot (cmdvec
, node
);
305 return cnode
->prompt
;
309 cmd_nodes_link (struct graph_node
*from
, struct graph_node
*to
)
311 for (size_t i
= 0; i
< vector_active (from
->to
); i
++)
312 if (vector_slot (from
->to
, i
) == to
)
317 static bool cmd_nodes_equal (struct graph_node
*ga
, struct graph_node
*gb
);
319 /* returns a single node to be excluded as "next" from iteration
320 * - for JOIN_TKN, never continue back to the FORK_TKN
321 * - in all other cases, don't try the node itself (in case of "...")
323 static inline struct graph_node
*
324 cmd_loopstop(struct graph_node
*gn
)
326 struct cmd_token
*tok
= gn
->data
;
327 if (tok
->type
== JOIN_TKN
)
328 return tok
->forkjoin
;
334 cmd_subgraph_equal (struct graph_node
*ga
, struct graph_node
*gb
,
335 struct graph_node
*a_join
)
338 struct graph_node
*a_fork
, *b_fork
;
339 a_fork
= cmd_loopstop (ga
);
340 b_fork
= cmd_loopstop (gb
);
342 if (vector_active (ga
->to
) != vector_active (gb
->to
))
344 for (i
= 0; i
< vector_active (ga
->to
); i
++)
346 struct graph_node
*cga
= vector_slot (ga
->to
, i
);
348 for (j
= 0; j
< vector_active (gb
->to
); j
++)
350 struct graph_node
*cgb
= vector_slot (gb
->to
, i
);
352 if (cga
== a_fork
&& cgb
!= b_fork
)
354 if (cga
== a_fork
&& cgb
== b_fork
)
357 if (cmd_nodes_equal (cga
, cgb
))
361 if (cmd_subgraph_equal (cga
, cgb
, a_join
))
365 if (j
== vector_active (gb
->to
))
371 /* deep compare -- for FORK_TKN, the entire subgraph is compared.
372 * this is what's needed since we're not currently trying to partially
375 cmd_nodes_equal (struct graph_node
*ga
, struct graph_node
*gb
)
377 struct cmd_token
*a
= ga
->data
, *b
= gb
->data
;
379 if (a
->type
!= b
->type
|| a
->allowrepeat
!= b
->allowrepeat
)
381 if (a
->type
< SPECIAL_TKN
&& strcmp (a
->text
, b
->text
))
383 /* one a ..., the other not. */
384 if (cmd_nodes_link (ga
, ga
) != cmd_nodes_link (gb
, gb
))
390 return a
->min
== b
->min
&& a
->max
== b
->max
;
393 /* one is keywords, the other just option or selector ... */
394 if (cmd_nodes_link (a
->forkjoin
, ga
) != cmd_nodes_link (b
->forkjoin
, gb
))
396 if (cmd_nodes_link (ga
, a
->forkjoin
) != cmd_nodes_link (gb
, b
->forkjoin
))
398 return cmd_subgraph_equal (ga
, gb
, a
->forkjoin
);
406 cmd_fork_bump_attr (struct graph_node
*gn
, struct graph_node
*join
,
410 struct cmd_token
*tok
= gn
->data
;
411 struct graph_node
*stop
= cmd_loopstop (gn
);
414 for (i
= 0; i
< vector_active (gn
->to
); i
++)
416 struct graph_node
*next
= vector_slot (gn
->to
, i
);
417 if (next
== stop
|| next
== join
)
419 cmd_fork_bump_attr (next
, join
, attr
);
423 /* move an entire subtree from the temporary graph resulting from
424 * parse() into the permanent graph for the command node.
426 * this touches rather deeply into the graph code unfortunately.
429 cmd_reparent_tree (struct graph
*fromgraph
, struct graph
*tograph
,
430 struct graph_node
*node
)
432 struct graph_node
*stop
= cmd_loopstop (node
);
435 for (i
= 0; i
< vector_active (fromgraph
->nodes
); i
++)
436 if (vector_slot (fromgraph
->nodes
, i
) == node
)
438 /* agressive iteration punching through subgraphs - may hit some
439 * nodes twice. reparent only if found on old graph */
440 vector_unset (fromgraph
->nodes
, i
);
441 vector_set (tograph
->nodes
, node
);
445 for (i
= 0; i
< vector_active (node
->to
); i
++)
447 struct graph_node
*next
= vector_slot (node
->to
, i
);
449 cmd_reparent_tree (fromgraph
, tograph
, next
);
454 cmd_free_recur (struct graph
*graph
, struct graph_node
*node
,
455 struct graph_node
*stop
)
457 struct graph_node
*next
, *nstop
;
459 for (size_t i
= vector_active (node
->to
); i
; i
--)
461 next
= vector_slot (node
->to
, i
- 1);
464 nstop
= cmd_loopstop (next
);
466 cmd_free_recur (graph
, next
, nstop
);
467 cmd_free_recur (graph
, nstop
, stop
);
469 graph_delete_node (graph
, node
);
473 cmd_free_node (struct graph
*graph
, struct graph_node
*node
)
475 struct cmd_token
*tok
= node
->data
;
476 if (tok
->type
== JOIN_TKN
)
477 cmd_free_recur (graph
, tok
->forkjoin
, node
);
478 graph_delete_node (graph
, node
);
481 /* recursive graph merge. call with
483 * (which holds true for old == START_TKN, new == START_TKN)
486 cmd_merge_nodes (struct graph
*oldgraph
, struct graph
*newgraph
,
487 struct graph_node
*old
, struct graph_node
*new,
490 struct cmd_token
*tok
;
491 struct graph_node
*old_skip
, *new_skip
;
492 old_skip
= cmd_loopstop (old
);
493 new_skip
= cmd_loopstop (new);
495 assert (direction
== 1 || direction
== -1);
498 tok
->refcnt
+= direction
;
501 for (j
= 0; j
< vector_active (new->to
); j
++)
503 struct graph_node
*cnew
= vector_slot (new->to
, j
);
504 if (cnew
== new_skip
)
507 for (i
= 0; i
< vector_active (old
->to
); i
++)
509 struct graph_node
*cold
= vector_slot (old
->to
, i
);
510 if (cold
== old_skip
)
513 if (cmd_nodes_equal (cold
, cnew
))
515 struct cmd_token
*told
= cold
->data
, *tnew
= cnew
->data
;
517 if (told
->type
== END_TKN
)
521 graph_delete_node (oldgraph
, vector_slot (cold
->to
, 0));
522 graph_delete_node (oldgraph
, cold
);
525 /* force no-match handling to install END_TKN */
526 i
= vector_active (old
->to
);
530 /* the entire fork compared as equal, we continue after it. */
531 if (told
->type
== FORK_TKN
)
533 if (tnew
->attr
< told
->attr
&& direction
> 0)
534 cmd_fork_bump_attr (cold
, told
->forkjoin
, tnew
->attr
);
535 /* XXX: no reverse bump on uninstall */
536 told
= (cold
= told
->forkjoin
)->data
;
537 tnew
= (cnew
= tnew
->forkjoin
)->data
;
539 if (tnew
->attr
< told
->attr
)
540 told
->attr
= tnew
->attr
;
542 cmd_merge_nodes (oldgraph
, newgraph
, cold
, cnew
, direction
);
546 /* nothing found => add new to old */
547 if (i
== vector_active (old
->to
) && direction
> 0)
549 assert (vector_count (cnew
->from
) ==
550 cmd_nodes_link (cnew
, cnew
) ? 2 : 1);
551 graph_remove_edge (new, cnew
);
553 cmd_reparent_tree (newgraph
, oldgraph
, cnew
);
555 graph_add_edge (old
, cnew
);
560 cmd_free_node (oldgraph
, old
);
564 cmd_merge_graphs (struct graph
*old
, struct graph
*new, int direction
)
566 assert (vector_active (old
->nodes
) >= 1);
567 assert (vector_active (new->nodes
) >= 1);
569 cmd_merge_nodes (old
, new,
570 vector_slot (old
->nodes
, 0), vector_slot (new->nodes
, 0),
574 /* Install a command into a node. */
576 install_element (enum node_type ntype
, struct cmd_element
*cmd
)
578 struct cmd_node
*cnode
;
580 /* cmd_init hasn't been called */
583 fprintf (stderr
, "%s called before cmd_init, breakage likely\n",
588 cnode
= vector_slot (cmdvec
, ntype
);
592 fprintf (stderr
, "Command node %d doesn't exist, please check it\n",
597 if (hash_lookup (cnode
->cmd_hash
, cmd
) != NULL
)
600 "Multiple command installs to node %d of command:\n%s\n",
605 assert (hash_get (cnode
->cmd_hash
, cmd
, hash_alloc_intern
));
607 struct graph
*graph
= graph_new();
608 struct cmd_token
*token
= new_cmd_token (START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
609 graph_new_node (graph
, token
, (void (*)(void *)) &del_cmd_token
);
611 command_parse_format (graph
, cmd
);
612 cmd_merge_graphs (cnode
->cmdgraph
, graph
, +1);
613 graph_delete_graph (graph
);
615 vector_set (cnode
->cmd_vector
, cmd
);
617 if (ntype
== VIEW_NODE
)
618 install_element (ENABLE_NODE
, cmd
);
622 uninstall_element (enum node_type ntype
, struct cmd_element
*cmd
)
624 struct cmd_node
*cnode
;
626 /* cmd_init hasn't been called */
629 fprintf (stderr
, "%s called before cmd_init, breakage likely\n",
634 cnode
= vector_slot (cmdvec
, ntype
);
638 fprintf (stderr
, "Command node %d doesn't exist, please check it\n",
643 if (hash_release (cnode
->cmd_hash
, cmd
) == NULL
)
646 "Trying to uninstall non-installed command (node %d):\n%s\n",
651 vector_unset_value (cnode
->cmd_vector
, cmd
);
653 struct graph
*graph
= graph_new();
654 struct cmd_token
*token
= new_cmd_token (START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
655 graph_new_node (graph
, token
, (void (*)(void *)) &del_cmd_token
);
657 command_parse_format (graph
, cmd
);
658 cmd_merge_graphs (cnode
->cmdgraph
, graph
, -1);
659 graph_delete_graph (graph
);
661 if (ntype
== VIEW_NODE
)
662 uninstall_element (ENABLE_NODE
, cmd
);
666 static const unsigned char itoa64
[] =
667 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
670 to64(char *s
, long v
, int n
)
674 *s
++ = itoa64
[v
&0x3f];
680 zencrypt (const char *passwd
)
684 char *crypt (const char *, const char *);
688 to64(&salt
[0], random(), 3);
689 to64(&salt
[3], tv
.tv_usec
, 3);
692 return crypt (passwd
, salt
);
695 /* This function write configuration of this host. */
697 config_write_host (struct vty
*vty
)
700 vty_out (vty
, "hostname %s%s", host
.name
, VTY_NEWLINE
);
704 if (host
.password_encrypt
)
705 vty_out (vty
, "password 8 %s%s", host
.password_encrypt
, VTY_NEWLINE
);
706 if (host
.enable_encrypt
)
707 vty_out (vty
, "enable password 8 %s%s", host
.enable_encrypt
, VTY_NEWLINE
);
712 vty_out (vty
, "password %s%s", host
.password
, VTY_NEWLINE
);
714 vty_out (vty
, "enable password %s%s", host
.enable
, VTY_NEWLINE
);
717 if (zlog_default
->default_lvl
!= LOG_DEBUG
)
719 vty_out (vty
, "! N.B. The 'log trap' command is deprecated.%s",
721 vty_out (vty
, "log trap %s%s",
722 zlog_priority
[zlog_default
->default_lvl
], VTY_NEWLINE
);
725 if (host
.logfile
&& (zlog_default
->maxlvl
[ZLOG_DEST_FILE
] != ZLOG_DISABLED
))
727 vty_out (vty
, "log file %s", host
.logfile
);
728 if (zlog_default
->maxlvl
[ZLOG_DEST_FILE
] != zlog_default
->default_lvl
)
730 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_FILE
]]);
731 vty_out (vty
, "%s", VTY_NEWLINE
);
734 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != ZLOG_DISABLED
)
736 vty_out (vty
, "log stdout");
737 if (zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
] != zlog_default
->default_lvl
)
739 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
]]);
740 vty_out (vty
, "%s", VTY_NEWLINE
);
743 if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
744 vty_out(vty
,"no log monitor%s",VTY_NEWLINE
);
745 else if (zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
] != zlog_default
->default_lvl
)
746 vty_out(vty
,"log monitor %s%s",
747 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
]],VTY_NEWLINE
);
749 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
751 vty_out (vty
, "log syslog");
752 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != zlog_default
->default_lvl
)
754 zlog_priority
[zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
]]);
755 vty_out (vty
, "%s", VTY_NEWLINE
);
758 if (zlog_default
->facility
!= LOG_DAEMON
)
759 vty_out (vty
, "log facility %s%s",
760 facility_name(zlog_default
->facility
), VTY_NEWLINE
);
762 if (zlog_default
->record_priority
== 1)
763 vty_out (vty
, "log record-priority%s", VTY_NEWLINE
);
765 if (zlog_default
->timestamp_precision
> 0)
766 vty_out (vty
, "log timestamp precision %d%s",
767 zlog_default
->timestamp_precision
, VTY_NEWLINE
);
770 vty_out (vty
, "service advanced-vty%s", VTY_NEWLINE
);
773 vty_out (vty
, "service password-encryption%s", VTY_NEWLINE
);
776 vty_out (vty
, "service terminal-length %d%s", host
.lines
,
780 vty_out (vty
, "banner motd file %s%s", host
.motdfile
, VTY_NEWLINE
);
781 else if (! host
.motd
)
782 vty_out (vty
, "no banner motd%s", VTY_NEWLINE
);
787 /* Utility function for getting command graph. */
788 static struct graph
*
789 cmd_node_graph (vector v
, enum node_type ntype
)
791 struct cmd_node
*cnode
= vector_slot (v
, ntype
);
792 return cnode
->cmdgraph
;
796 cmd_try_do_shortcut (enum node_type node
, char* first_word
) {
797 if ( first_word
!= NULL
&&
800 node
!= AUTH_ENABLE_NODE
&&
801 node
!= ENABLE_NODE
&&
802 0 == strcmp( "do", first_word
) )
808 * Compare function for cmd_token.
809 * Used with qsort to sort command completions.
812 compare_completions (const void *fst
, const void *snd
)
814 struct cmd_token
*first
= *(struct cmd_token
**) fst
,
815 *secnd
= *(struct cmd_token
**) snd
;
816 return strcmp (first
->text
, secnd
->text
);
820 * Takes a list of completions returned by command_complete,
821 * dedeuplicates them based on both text and description,
822 * sorts them, and returns them as a vector.
824 * @param completions linked list of cmd_token
825 * @return deduplicated and sorted vector with
828 completions_to_vec (struct list
*completions
)
830 vector comps
= vector_init (VECTOR_MIN_SIZE
);
833 struct cmd_token
*token
, *cr
= NULL
;
834 unsigned int i
, exists
;
835 for (ALL_LIST_ELEMENTS_RO(completions
,ln
,token
))
837 if (token
->type
== END_TKN
&& (cr
= token
))
840 // linear search for token in completions vector
842 for (i
= 0; i
< vector_active (comps
) && !exists
; i
++)
844 struct cmd_token
*curr
= vector_slot (comps
, i
);
846 exists
= !strcmp (curr
->text
, token
->text
) &&
847 !strcmp (curr
->desc
, token
->desc
);
849 exists
= !strcmp (curr
->text
, token
->text
);
850 #endif /* VTYSH_DEBUG */
854 vector_set (comps
, token
);
859 vector_active (comps
),
861 &compare_completions
);
863 // make <cr> the first element, if it is present
866 vector_set_index (comps
, vector_active (comps
), NULL
);
867 memmove (comps
->index
+ 1, comps
->index
, (comps
->alloced
- 1) * sizeof (void *));
868 vector_set_index (comps
, 0, cr
);
874 * Generates a vector of cmd_token representing possible completions
875 * on the current input.
877 * @param vline the vectorized input line
878 * @param vty the vty with the node to match on
879 * @param status pointer to matcher status code
880 * @return vector of struct cmd_token * with possible completions
883 cmd_complete_command_real (vector vline
, struct vty
*vty
, int *status
)
885 struct list
*completions
;
886 struct graph
*cmdgraph
= cmd_node_graph (cmdvec
, vty
->node
);
888 enum matcher_rv rv
= command_complete (cmdgraph
, vline
, &completions
);
890 if (MATCHER_ERROR(rv
))
892 *status
= CMD_ERR_NO_MATCH
;
896 vector comps
= completions_to_vec (completions
);
897 list_delete (completions
);
899 // set status code appropriately
900 switch (vector_active (comps
))
903 *status
= CMD_ERR_NO_MATCH
;
906 *status
= CMD_COMPLETE_FULL_MATCH
;
909 *status
= CMD_COMPLETE_LIST_MATCH
;
916 cmd_describe_command (vector vline
, struct vty
*vty
, int *status
)
920 if ( cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0) ) )
922 enum node_type onode
;
923 vector shifted_vline
;
927 vty
->node
= ENABLE_NODE
;
928 /* We can try it on enable node, cos' the vty is authenticated */
930 shifted_vline
= vector_init (vector_count(vline
));
932 for (index
= 1; index
< vector_active (vline
); index
++)
934 vector_set_index (shifted_vline
, index
-1, vector_lookup(vline
, index
));
937 ret
= cmd_complete_command_real (shifted_vline
, vty
, status
);
939 vector_free(shifted_vline
);
944 return cmd_complete_command_real (vline
, vty
, status
);
948 * Generate possible tab-completions for the given input. This function only
949 * returns results that would result in a valid command if used as Readline
950 * completions (as is the case in vtysh). For instance, if the passed vline ends
951 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
953 * @param vline vectorized input line
955 * @param status location to store matcher status code in
956 * @return set of valid strings for use with Readline as tab-completions.
960 cmd_complete_command (vector vline
, struct vty
*vty
, int *status
)
963 int original_node
= vty
->node
;
964 vector input_line
= vector_init (vector_count (vline
));
966 // if the first token is 'do' we'll want to execute the command in the enable node
967 int do_shortcut
= cmd_try_do_shortcut (vty
->node
, vector_slot (vline
, 0));
968 vty
->node
= do_shortcut
? ENABLE_NODE
: original_node
;
970 // construct the input line we'll be matching on
971 unsigned int offset
= (do_shortcut
) ? 1 : 0;
972 for (unsigned index
= 0; index
+ offset
< vector_active (vline
); index
++)
973 vector_set_index (input_line
, index
, vector_lookup (vline
, index
+ offset
));
975 // get token completions -- this is a copying operation
976 vector comps
= NULL
, initial_comps
;
977 initial_comps
= cmd_complete_command_real (input_line
, vty
, status
);
979 if (!MATCHER_ERROR (*status
))
981 assert (initial_comps
);
982 // filter out everything that is not suitable for a tab-completion
983 comps
= vector_init (VECTOR_MIN_SIZE
);
984 for (unsigned int i
= 0; i
< vector_active(initial_comps
); i
++)
986 struct cmd_token
*token
= vector_slot (initial_comps
, i
);
987 if (token
->type
== WORD_TKN
)
988 vector_set (comps
, token
);
990 vector_free (initial_comps
);
992 // since we filtered results, we need to re-set status code
993 switch (vector_active (comps
))
996 *status
= CMD_ERR_NO_MATCH
;
999 *status
= CMD_COMPLETE_FULL_MATCH
;
1002 *status
= CMD_COMPLETE_LIST_MATCH
;
1005 // copy completions text into an array of char*
1006 ret
= XMALLOC (MTYPE_TMP
, (vector_active (comps
)+1) * sizeof (char *));
1008 for (i
= 0; i
< vector_active (comps
); i
++)
1010 struct cmd_token
*token
= vector_slot (comps
, i
);
1011 ret
[i
] = XSTRDUP (MTYPE_TMP
, token
->text
);
1012 vector_unset (comps
, i
);
1014 // set the last element to NULL, because this array is used in
1015 // a Readline completion_generator function which expects NULL
1016 // as a sentinel value
1018 vector_free (comps
);
1021 else if (initial_comps
)
1022 vector_free (initial_comps
);
1024 // comps should always be null here
1027 // free the adjusted input line
1028 vector_free (input_line
);
1030 // reset vty->node to its original value
1031 vty
->node
= original_node
;
1036 /* return parent node */
1037 /* MUST eventually converge on CONFIG_NODE */
1039 node_parent ( enum node_type node
)
1043 assert (node
> CONFIG_NODE
);
1047 case BGP_VPNV4_NODE
:
1048 case BGP_VPNV6_NODE
:
1049 case BGP_ENCAP_NODE
:
1050 case BGP_ENCAPV6_NODE
:
1051 case BGP_VRF_POLICY_NODE
:
1052 case BGP_VNC_DEFAULTS_NODE
:
1053 case BGP_VNC_NVE_GROUP_NODE
:
1054 case BGP_VNC_L2_GROUP_NODE
:
1056 case BGP_IPV4M_NODE
:
1058 case BGP_IPV6M_NODE
:
1062 case KEYCHAIN_KEY_NODE
:
1063 ret
= KEYCHAIN_NODE
;
1065 case LINK_PARAMS_NODE
:
1066 ret
= INTERFACE_NODE
;
1072 case LDP_IPV4_IFACE_NODE
:
1073 ret
= LDP_IPV4_NODE
;
1075 case LDP_IPV6_IFACE_NODE
:
1076 ret
= LDP_IPV6_NODE
;
1078 case LDP_PSEUDOWIRE_NODE
:
1079 ret
= LDP_L2VPN_NODE
;
1089 /* Execute command by argument vline vector. */
1091 cmd_execute_command_real (vector vline
,
1092 enum filter_type filter
,
1094 const struct cmd_element
**cmd
)
1096 struct list
*argv_list
;
1097 enum matcher_rv status
;
1098 const struct cmd_element
*matched_element
= NULL
;
1100 struct graph
*cmdgraph
= cmd_node_graph (cmdvec
, vty
->node
);
1101 status
= command_match (cmdgraph
, vline
, &argv_list
, &matched_element
);
1104 *cmd
= matched_element
;
1106 // if matcher error, return corresponding CMD_ERR
1107 if (MATCHER_ERROR(status
))
1110 list_delete (argv_list
);
1113 case MATCHER_INCOMPLETE
:
1114 return CMD_ERR_INCOMPLETE
;
1115 case MATCHER_AMBIGUOUS
:
1116 return CMD_ERR_AMBIGUOUS
;
1118 return CMD_ERR_NO_MATCH
;
1122 // build argv array from argv list
1123 struct cmd_token
**argv
= XMALLOC (MTYPE_TMP
, argv_list
->count
* sizeof (struct cmd_token
*));
1124 struct listnode
*ln
;
1125 struct cmd_token
*token
;
1127 for (ALL_LIST_ELEMENTS_RO(argv_list
,ln
,token
))
1130 int argc
= argv_list
->count
;
1133 if (matched_element
->daemon
)
1134 ret
= CMD_SUCCESS_DAEMON
;
1136 ret
= matched_element
->func (matched_element
, vty
, argc
, argv
);
1138 // delete list and cmd_token's in it
1139 list_delete (argv_list
);
1140 XFREE (MTYPE_TMP
, argv
);
1146 * Execute a given command, handling things like "do ..." and checking
1147 * whether the given command might apply at a parent node if doesn't
1148 * apply for the current node.
1150 * @param vline Command line input, vector of char* where each element is
1152 * @param vty The vty context in which the command should be executed.
1153 * @param cmd Pointer where the struct cmd_element of the matched command
1154 * will be stored, if any. May be set to NULL if this info is
1156 * @param vtysh If set != 0, don't lookup the command at parent nodes.
1157 * @return The status of the command that has been executed or an error code
1158 * as to why no command could be executed.
1161 cmd_execute_command (vector vline
, struct vty
*vty
,
1162 const struct cmd_element
**cmd
,
1165 int ret
, saved_ret
= 0;
1166 enum node_type onode
, try_node
;
1168 onode
= try_node
= vty
->node
;
1170 if (cmd_try_do_shortcut(vty
->node
, vector_slot(vline
, 0)))
1172 vector shifted_vline
;
1175 vty
->node
= ENABLE_NODE
;
1176 /* We can try it on enable node, cos' the vty is authenticated */
1178 shifted_vline
= vector_init (vector_count(vline
));
1180 for (index
= 1; index
< vector_active (vline
); index
++)
1181 vector_set_index (shifted_vline
, index
-1, vector_lookup(vline
, index
));
1183 ret
= cmd_execute_command_real (shifted_vline
, FILTER_RELAXED
, vty
, cmd
);
1185 vector_free(shifted_vline
);
1190 saved_ret
= ret
= cmd_execute_command_real (vline
, FILTER_RELAXED
, vty
, cmd
);
1195 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1197 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
1198 while (vty
->node
> CONFIG_NODE
)
1200 try_node
= node_parent(try_node
);
1201 vty
->node
= try_node
;
1202 ret
= cmd_execute_command_real (vline
, FILTER_RELAXED
, vty
, cmd
);
1203 if (ret
== CMD_SUCCESS
|| ret
== CMD_WARNING
)
1206 /* no command succeeded, reset the vty to the original node */
1210 /* return command status for original node */
1215 * Execute a given command, matching it strictly against the current node.
1216 * This mode is used when reading config files.
1218 * @param vline Command line input, vector of char* where each element is
1220 * @param vty The vty context in which the command should be executed.
1221 * @param cmd Pointer where the struct cmd_element* of the matched command
1222 * will be stored, if any. May be set to NULL if this info is
1224 * @return The status of the command that has been executed or an error code
1225 * as to why no command could be executed.
1228 cmd_execute_command_strict (vector vline
, struct vty
*vty
,
1229 const struct cmd_element
**cmd
)
1231 return cmd_execute_command_real(vline
, FILTER_STRICT
, vty
, cmd
);
1235 * Parse one line of config, walking up the parse tree attempting to find a match
1237 * @param vty The vty context in which the command should be executed.
1238 * @param cmd Pointer where the struct cmd_element* of the match command
1239 * will be stored, if any. May be set to NULL if this info is
1241 * @param use_daemon Boolean to control whether or not we match on CMD_SUCCESS_DAEMON
1243 * @return The status of the command that has been executed or an error code
1244 * as to why no command could be executed.
1247 command_config_read_one_line (struct vty
*vty
, const struct cmd_element
**cmd
, int use_daemon
)
1253 vline
= cmd_make_strvec (vty
->buf
);
1255 /* In case of comment line */
1259 /* Execute configuration command : this is strict match */
1260 ret
= cmd_execute_command_strict (vline
, vty
, cmd
);
1262 // Climb the tree and try the command again at each node
1263 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1264 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1265 ret
!= CMD_SUCCESS
&&
1266 ret
!= CMD_WARNING
&&
1267 vty
->node
!= CONFIG_NODE
) {
1269 saved_node
= vty
->node
;
1271 while (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1272 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1273 ret
!= CMD_SUCCESS
&&
1274 ret
!= CMD_WARNING
&&
1275 vty
->node
> CONFIG_NODE
) {
1276 vty
->node
= node_parent(vty
->node
);
1277 ret
= cmd_execute_command_strict (vline
, vty
, cmd
);
1280 // If climbing the tree did not work then ignore the command and
1281 // stay at the same node
1282 if (!(use_daemon
&& ret
== CMD_SUCCESS_DAEMON
) &&
1283 !(!use_daemon
&& ret
== CMD_ERR_NOTHING_TODO
) &&
1284 ret
!= CMD_SUCCESS
&&
1287 vty
->node
= saved_node
;
1291 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
)
1292 memcpy (vty
->error_buf
, vty
->buf
, VTY_BUFSIZ
);
1294 cmd_free_strvec (vline
);
1299 /* Configuration make from file. */
1301 config_from_file (struct vty
*vty
, FILE *fp
, unsigned int *line_num
)
1303 int ret
, error_ret
=0;
1306 while (fgets (vty
->buf
, VTY_BUFSIZ
, fp
))
1311 ret
= command_config_read_one_line (vty
, NULL
, 0);
1313 if (ret
!= CMD_SUCCESS
&& ret
!= CMD_WARNING
&&
1314 ret
!= CMD_ERR_NOTHING_TODO
)
1325 /* Configuration from terminal */
1326 DEFUN (config_terminal
,
1327 config_terminal_cmd
,
1328 "configure terminal",
1329 "Configuration from vty interface\n"
1330 "Configuration terminal\n")
1332 if (vty_config_lock (vty
))
1333 vty
->node
= CONFIG_NODE
;
1336 vty_out (vty
, "VTY configuration is locked by other VTY%s", VTY_NEWLINE
);
1342 /* Enable command */
1346 "Turn on privileged mode command\n")
1348 /* If enable password is NULL, change to ENABLE_NODE */
1349 if ((host
.enable
== NULL
&& host
.enable_encrypt
== NULL
) ||
1350 vty
->type
== VTY_SHELL_SERV
)
1351 vty
->node
= ENABLE_NODE
;
1353 vty
->node
= AUTH_ENABLE_NODE
;
1358 /* Disable command */
1362 "Turn off privileged mode command\n")
1364 if (vty
->node
== ENABLE_NODE
)
1365 vty
->node
= VIEW_NODE
;
1369 /* Down vty node level. */
1373 "Exit current mode and down to previous mode\n")
1380 cmd_exit (struct vty
*vty
)
1386 if (vty_shell (vty
))
1389 vty
->status
= VTY_CLOSE
;
1392 vty
->node
= ENABLE_NODE
;
1393 vty_config_unlock (vty
);
1395 case INTERFACE_NODE
:
1405 case LDP_L2VPN_NODE
:
1412 vty
->node
= CONFIG_NODE
;
1415 case BGP_IPV4M_NODE
:
1416 case BGP_VPNV4_NODE
:
1417 case BGP_VPNV6_NODE
:
1418 case BGP_ENCAP_NODE
:
1419 case BGP_ENCAPV6_NODE
:
1420 case BGP_VRF_POLICY_NODE
:
1421 case BGP_VNC_DEFAULTS_NODE
:
1422 case BGP_VNC_NVE_GROUP_NODE
:
1423 case BGP_VNC_L2_GROUP_NODE
:
1425 case BGP_IPV6M_NODE
:
1427 vty
->node
= BGP_NODE
;
1431 vty
->node
= LDP_NODE
;
1433 case LDP_IPV4_IFACE_NODE
:
1434 vty
->node
= LDP_IPV4_NODE
;
1436 case LDP_IPV6_IFACE_NODE
:
1437 vty
->node
= LDP_IPV6_NODE
;
1439 case LDP_PSEUDOWIRE_NODE
:
1440 vty
->node
= LDP_L2VPN_NODE
;
1442 case KEYCHAIN_KEY_NODE
:
1443 vty
->node
= KEYCHAIN_NODE
;
1445 case LINK_PARAMS_NODE
:
1446 vty
->node
= INTERFACE_NODE
;
1457 "Exit current mode and down to previous mode\n")
1459 return config_exit (self
, vty
, argc
, argv
);
1463 /* End of configuration. */
1467 "End current mode and change to enable mode.")
1473 /* Nothing to do. */
1476 case INTERFACE_NODE
:
1483 case BGP_ENCAP_NODE
:
1484 case BGP_ENCAPV6_NODE
:
1485 case BGP_VRF_POLICY_NODE
:
1486 case BGP_VNC_DEFAULTS_NODE
:
1487 case BGP_VNC_NVE_GROUP_NODE
:
1488 case BGP_VNC_L2_GROUP_NODE
:
1489 case BGP_VPNV4_NODE
:
1490 case BGP_VPNV6_NODE
:
1492 case BGP_IPV4M_NODE
:
1494 case BGP_IPV6M_NODE
:
1502 case LDP_IPV4_IFACE_NODE
:
1503 case LDP_IPV6_IFACE_NODE
:
1504 case LDP_L2VPN_NODE
:
1505 case LDP_PSEUDOWIRE_NODE
:
1508 case KEYCHAIN_KEY_NODE
:
1512 case LINK_PARAMS_NODE
:
1513 vty_config_unlock (vty
);
1514 vty
->node
= ENABLE_NODE
;
1523 DEFUN (show_version
,
1527 "Displays zebra version\n")
1529 vty_out (vty
, "%s %s (%s).%s", FRR_FULL_NAME
, FRR_VERSION
,
1530 host
.name
? host
.name
: "",
1532 vty_out (vty
, "%s%s%s", FRR_COPYRIGHT
, GIT_INFO
, VTY_NEWLINE
);
1533 vty_out (vty
, "configured with:%s %s%s", VTY_NEWLINE
,
1534 FRR_CONFIG_ARGS
, VTY_NEWLINE
);
1539 /* Help display function for all node. */
1543 "Description of the interactive help system\n")
1546 "Quagga VTY provides advanced help feature. When you need help,%s\
1547 anytime at the command line please press '?'.%s\
1549 If nothing matches, the help list will be empty and you must backup%s\
1550 until entering a '?' shows the available options.%s\
1551 Two styles of help are provided:%s\
1552 1. Full help is available when you are ready to enter a%s\
1553 command argument (e.g. 'show ?') and describes each possible%s\
1555 2. Partial help is provided when an abbreviated argument is entered%s\
1556 and you want to know what arguments match the input%s\
1557 (e.g. 'show me?'.)%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
,
1558 VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
,
1559 VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1564 permute (struct graph_node
*start
, struct vty
*vty
)
1566 static struct list
*position
= NULL
;
1567 if (!position
) position
= list_new ();
1570 listnode_add (position
, start
);
1571 for (unsigned int i
= 0; i
< vector_active (start
->to
); i
++)
1573 struct graph_node
*gn
= vector_slot (start
->to
, i
);
1574 struct cmd_token
*tok
= gn
->data
;
1575 if (tok
->attr
== CMD_ATTR_HIDDEN
||
1576 tok
->attr
== CMD_ATTR_DEPRECATED
)
1578 else if (tok
->type
== END_TKN
|| gn
== start
)
1580 struct graph_node
*gnn
;
1581 struct listnode
*ln
;
1583 for (ALL_LIST_ELEMENTS_RO (position
,ln
,gnn
))
1585 struct cmd_token
*tt
= gnn
->data
;
1586 if (tt
->type
< SPECIAL_TKN
)
1587 vty_out (vty
, " %s", tt
->text
);
1590 vty_out (vty
, "...");
1591 vty_out (vty
, VTY_NEWLINE
);
1596 list_delete_node (position
, listtail(position
));
1600 cmd_list_cmds (struct vty
*vty
, int do_permute
)
1602 struct cmd_node
*node
= vector_slot (cmdvec
, vty
->node
);
1605 permute (vector_slot (node
->cmdgraph
->nodes
, 0), vty
);
1608 /* loop over all commands at this node */
1609 struct cmd_element
*element
= NULL
;
1610 for (unsigned int i
= 0; i
< vector_active(node
->cmd_vector
); i
++)
1611 if ((element
= vector_slot (node
->cmd_vector
, i
)) &&
1612 element
->attr
!= CMD_ATTR_DEPRECATED
&&
1613 element
->attr
!= CMD_ATTR_HIDDEN
)
1614 vty_out (vty
, " %s%s", element
->string
, VTY_NEWLINE
);
1619 /* Help display function for all node. */
1622 "list [permutations]",
1623 "Print command list\n"
1624 "Print all possible command permutations\n")
1626 return cmd_list_cmds (vty
, argc
== 2);
1629 DEFUN (show_commandtree
,
1630 show_commandtree_cmd
,
1631 "show commandtree [permutations]",
1633 "Show command tree\n"
1634 "Permutations that we are interested in\n")
1636 return cmd_list_cmds (vty
, argc
== 3);
1639 /* Write current configuration into file. */
1641 DEFUN (config_write
,
1643 "write [<file|memory|terminal>]",
1644 "Write running configuration to memory, network, or terminal\n"
1645 "Write to configuration file\n"
1646 "Write configuration currently in memory\n"
1647 "Write configuration to terminal\n")
1652 struct cmd_node
*node
;
1653 char *config_file
, *slash
;
1654 char *config_file_tmp
= NULL
;
1655 char *config_file_sav
= NULL
;
1656 int ret
= CMD_WARNING
;
1657 struct vty
*file_vty
;
1658 struct stat conf_stat
;
1660 // if command was 'write terminal' or 'show running-config'
1661 if (argc
== 2 && (!strcmp(argv
[idx_type
]->text
, "terminal") ||
1662 !strcmp(argv
[0]->text
, "show")))
1664 if (vty
->type
== VTY_SHELL_SERV
)
1666 for (i
= 0; i
< vector_active (cmdvec
); i
++)
1667 if ((node
= vector_slot (cmdvec
, i
)) && node
->func
&& node
->vtysh
)
1669 if ((*node
->func
) (vty
))
1670 vty_out (vty
, "!%s", VTY_NEWLINE
);
1675 vty_out (vty
, "%sCurrent configuration:%s", VTY_NEWLINE
,
1677 vty_out (vty
, "!%s", VTY_NEWLINE
);
1679 for (i
= 0; i
< vector_active (cmdvec
); i
++)
1680 if ((node
= vector_slot (cmdvec
, i
)) && node
->func
)
1682 if ((*node
->func
) (vty
))
1683 vty_out (vty
, "!%s", VTY_NEWLINE
);
1685 vty_out (vty
, "end%s",VTY_NEWLINE
);
1693 /* Check and see if we are operating under vtysh configuration */
1694 if (host
.config
== NULL
)
1696 vty_out (vty
, "Can't save to configuration file, using vtysh.%s",
1702 config_file
= host
.config
;
1705 #define O_DIRECTORY 0
1707 slash
= strrchr (config_file
, '/');
1710 char *config_dir
= XSTRDUP (MTYPE_TMP
, config_file
);
1711 config_dir
[slash
- config_file
] = '\0';
1712 dirfd
= open(config_dir
, O_DIRECTORY
| O_RDONLY
);
1713 XFREE (MTYPE_TMP
, config_dir
);
1716 dirfd
= open(".", O_DIRECTORY
| O_RDONLY
);
1717 /* if dirfd is invalid, directory sync fails, but we're still OK */
1720 XMALLOC (MTYPE_TMP
, strlen (config_file
) + strlen (CONF_BACKUP_EXT
) + 1);
1721 strcpy (config_file_sav
, config_file
);
1722 strcat (config_file_sav
, CONF_BACKUP_EXT
);
1725 config_file_tmp
= XMALLOC (MTYPE_TMP
, strlen (config_file
) + 8);
1726 sprintf (config_file_tmp
, "%s.XXXXXX", config_file
);
1728 /* Open file to configuration write. */
1729 fd
= mkstemp (config_file_tmp
);
1732 vty_out (vty
, "Can't open configuration file %s.%s", config_file_tmp
,
1736 if (fchmod (fd
, CONFIGFILE_MASK
) != 0)
1738 vty_out (vty
, "Can't chmod configuration file %s: %s (%d).%s",
1739 config_file_tmp
, safe_strerror(errno
), errno
, VTY_NEWLINE
);
1743 /* Make vty for configuration file. */
1744 file_vty
= vty_new ();
1746 file_vty
->type
= VTY_FILE
;
1748 /* Config file header print. */
1749 vty_out (file_vty
, "!\n! Zebra configuration saved from vty\n! ");
1750 vty_time_print (file_vty
, 1);
1751 vty_out (file_vty
, "!\n");
1753 for (i
= 0; i
< vector_active (cmdvec
); i
++)
1754 if ((node
= vector_slot (cmdvec
, i
)) && node
->func
)
1756 if ((*node
->func
) (file_vty
))
1757 vty_out (file_vty
, "!\n");
1759 vty_close (file_vty
);
1761 if (stat(config_file
, &conf_stat
) >= 0)
1763 if (unlink (config_file_sav
) != 0)
1764 if (errno
!= ENOENT
)
1766 vty_out (vty
, "Can't unlink backup configuration file %s.%s", config_file_sav
,
1770 if (link (config_file
, config_file_sav
) != 0)
1772 vty_out (vty
, "Can't backup old configuration file %s.%s", config_file_sav
,
1779 if (rename (config_file_tmp
, config_file
) != 0)
1781 vty_out (vty
, "Can't save configuration file %s.%s", config_file
,
1788 vty_out (vty
, "Configuration saved to %s%s", config_file
,
1793 if (ret
!= CMD_SUCCESS
)
1794 unlink (config_file_tmp
);
1797 XFREE (MTYPE_TMP
, config_file_tmp
);
1798 XFREE (MTYPE_TMP
, config_file_sav
);
1802 /* ALIAS_FIXME for 'write <terminal|memory>' */
1803 DEFUN (show_running_config
,
1804 show_running_config_cmd
,
1805 "show running-config",
1807 "running configuration (same as write terminal/memory)\n")
1809 return config_write (self
, vty
, argc
, argv
);
1812 /* ALIAS_FIXME for 'write file' */
1813 DEFUN (copy_runningconf_startupconf
,
1814 copy_runningconf_startupconf_cmd
,
1815 "copy running-config startup-config",
1816 "Copy configuration\n"
1817 "Copy running config to... \n"
1818 "Copy running config to startup config (same as write file)\n")
1820 return config_write (self
, vty
, argc
, argv
);
1824 /* Write startup configuration into the terminal. */
1825 DEFUN (show_startup_config
,
1826 show_startup_config_cmd
,
1827 "show startup-config",
1829 "Contents of startup configuration\n")
1836 if (host
.config
== NULL
)
1839 confp
= fopen (host
.config
, "r");
1842 vty_out (vty
, "Can't open configuration file [%s] due to '%s'%s",
1843 host
.config
, safe_strerror(errno
), VTY_NEWLINE
);
1847 while (fgets (buf
, BUFSIZ
, confp
))
1851 while (*cp
!= '\r' && *cp
!= '\n' && *cp
!= '\0')
1855 vty_out (vty
, "%s%s", buf
, VTY_NEWLINE
);
1864 cmd_hostname_set (const char *hostname
)
1866 XFREE (MTYPE_HOST
, host
.name
);
1867 host
.name
= hostname
? XSTRDUP (MTYPE_HOST
, hostname
) : NULL
;
1871 /* Hostname configuration */
1872 DEFUN (config_hostname
,
1875 "Set system's network name\n"
1876 "This system's network name\n")
1878 struct cmd_token
*word
= argv
[1];
1880 if (!isalpha((int) word
->arg
[0]))
1882 vty_out (vty
, "Please specify string starting with alphabet%s", VTY_NEWLINE
);
1886 return cmd_hostname_set (word
->arg
);
1889 DEFUN (config_no_hostname
,
1891 "no hostname [HOSTNAME]",
1893 "Reset system's network name\n"
1894 "Host name of this router\n")
1896 return cmd_hostname_set (NULL
);
1899 /* VTY interface password set. */
1900 DEFUN (config_password
,
1902 "password [(8-8)] WORD",
1903 "Assign the terminal connection password\n"
1904 "Specifies a HIDDEN password will follow\n"
1905 "The password string\n")
1909 if (argc
== 3) // '8' was specified
1912 XFREE (MTYPE_HOST
, host
.password
);
1913 host
.password
= NULL
;
1914 if (host
.password_encrypt
)
1915 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1916 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, argv
[idx_word
]->arg
);
1920 if (!isalnum (argv
[idx_8
]->arg
[0]))
1923 "Please specify string starting with alphanumeric%s", VTY_NEWLINE
);
1928 XFREE (MTYPE_HOST
, host
.password
);
1929 host
.password
= NULL
;
1933 if (host
.password_encrypt
)
1934 XFREE (MTYPE_HOST
, host
.password_encrypt
);
1935 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (argv
[idx_8
]->arg
));
1938 host
.password
= XSTRDUP (MTYPE_HOST
, argv
[idx_8
]->arg
);
1943 /* VTY enable password set. */
1944 DEFUN (config_enable_password
,
1945 enable_password_cmd
,
1946 "enable password [(8-8)] WORD",
1947 "Modify enable password parameters\n"
1948 "Assign the privileged level password\n"
1949 "Specifies a HIDDEN password will follow\n"
1950 "The HIDDEN 'enable' password string\n")
1955 /* Crypt type is specified. */
1958 if (argv
[idx_8
]->arg
[0] == '8')
1961 XFREE (MTYPE_HOST
, host
.enable
);
1964 if (host
.enable_encrypt
)
1965 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1966 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, argv
[idx_word
]->arg
);
1972 vty_out (vty
, "Unknown encryption type.%s", VTY_NEWLINE
);
1977 if (!isalnum (argv
[idx_8
]->arg
[0]))
1980 "Please specify string starting with alphanumeric%s", VTY_NEWLINE
);
1985 XFREE (MTYPE_HOST
, host
.enable
);
1988 /* Plain password input. */
1991 if (host
.enable_encrypt
)
1992 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
1993 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (argv
[idx_8
]->arg
));
1996 host
.enable
= XSTRDUP (MTYPE_HOST
, argv
[idx_8
]->arg
);
2001 /* VTY enable password delete. */
2002 DEFUN (no_config_enable_password
,
2003 no_enable_password_cmd
,
2004 "no enable password",
2006 "Modify enable password parameters\n"
2007 "Assign the privileged level password\n")
2010 XFREE (MTYPE_HOST
, host
.enable
);
2013 if (host
.enable_encrypt
)
2014 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
2015 host
.enable_encrypt
= NULL
;
2020 DEFUN (service_password_encrypt
,
2021 service_password_encrypt_cmd
,
2022 "service password-encryption",
2023 "Set up miscellaneous service\n"
2024 "Enable encrypted passwords\n")
2033 if (host
.password_encrypt
)
2034 XFREE (MTYPE_HOST
, host
.password_encrypt
);
2035 host
.password_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (host
.password
));
2039 if (host
.enable_encrypt
)
2040 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
2041 host
.enable_encrypt
= XSTRDUP (MTYPE_HOST
, zencrypt (host
.enable
));
2047 DEFUN (no_service_password_encrypt
,
2048 no_service_password_encrypt_cmd
,
2049 "no service password-encryption",
2051 "Set up miscellaneous service\n"
2052 "Enable encrypted passwords\n")
2059 if (host
.password_encrypt
)
2060 XFREE (MTYPE_HOST
, host
.password_encrypt
);
2061 host
.password_encrypt
= NULL
;
2063 if (host
.enable_encrypt
)
2064 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
2065 host
.enable_encrypt
= NULL
;
2070 DEFUN (config_terminal_length
,
2071 config_terminal_length_cmd
,
2072 "terminal length (0-512)",
2073 "Set terminal line parameters\n"
2074 "Set number of lines on a screen\n"
2075 "Number of lines on screen (0 for no pausing)\n")
2079 char *endptr
= NULL
;
2081 lines
= strtol (argv
[idx_number
]->arg
, &endptr
, 10);
2082 if (lines
< 0 || lines
> 512 || *endptr
!= '\0')
2084 vty_out (vty
, "length is malformed%s", VTY_NEWLINE
);
2092 DEFUN (config_terminal_no_length
,
2093 config_terminal_no_length_cmd
,
2094 "terminal no length",
2095 "Set terminal line parameters\n"
2097 "Set number of lines on a screen\n")
2103 DEFUN (service_terminal_length
,
2104 service_terminal_length_cmd
,
2105 "service terminal-length (0-512)",
2106 "Set up miscellaneous service\n"
2107 "System wide terminal length configuration\n"
2108 "Number of lines of VTY (0 means no line control)\n")
2112 char *endptr
= NULL
;
2114 lines
= strtol (argv
[idx_number
]->arg
, &endptr
, 10);
2115 if (lines
< 0 || lines
> 512 || *endptr
!= '\0')
2117 vty_out (vty
, "length is malformed%s", VTY_NEWLINE
);
2125 DEFUN (no_service_terminal_length
,
2126 no_service_terminal_length_cmd
,
2127 "no service terminal-length [(0-512)]",
2129 "Set up miscellaneous service\n"
2130 "System wide terminal length configuration\n"
2131 "Number of lines of VTY (0 means no line control)\n")
2137 DEFUN_HIDDEN (do_echo
,
2140 "Echo a message back to the vty\n"
2141 "The message to echo\n")
2145 vty_out (vty
, "%s%s", ((message
= argv_concat (argv
, argc
, 1)) ? message
: ""),
2148 XFREE(MTYPE_TMP
, message
);
2152 DEFUN (config_logmsg
,
2154 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
2155 "Send a message to enabled logging destinations\n"
2157 "The message to send\n")
2159 int idx_log_level
= 1;
2160 int idx_message
= 2;
2164 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2165 return CMD_ERR_NO_MATCH
;
2167 zlog(level
, "%s", ((message
= argv_concat(argv
, argc
, idx_message
)) ? message
: ""));
2169 XFREE(MTYPE_TMP
, message
);
2174 DEFUN (show_logging
,
2178 "Show current logging configuration\n")
2180 struct zlog
*zl
= zlog_default
;
2182 vty_out (vty
, "Syslog logging: ");
2183 if (zl
->maxlvl
[ZLOG_DEST_SYSLOG
] == ZLOG_DISABLED
)
2184 vty_out (vty
, "disabled");
2186 vty_out (vty
, "level %s, facility %s, ident %s",
2187 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_SYSLOG
]],
2188 facility_name(zl
->facility
), zl
->ident
);
2189 vty_out (vty
, "%s", VTY_NEWLINE
);
2191 vty_out (vty
, "Stdout logging: ");
2192 if (zl
->maxlvl
[ZLOG_DEST_STDOUT
] == ZLOG_DISABLED
)
2193 vty_out (vty
, "disabled");
2195 vty_out (vty
, "level %s",
2196 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_STDOUT
]]);
2197 vty_out (vty
, "%s", VTY_NEWLINE
);
2199 vty_out (vty
, "Monitor logging: ");
2200 if (zl
->maxlvl
[ZLOG_DEST_MONITOR
] == ZLOG_DISABLED
)
2201 vty_out (vty
, "disabled");
2203 vty_out (vty
, "level %s",
2204 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_MONITOR
]]);
2205 vty_out (vty
, "%s", VTY_NEWLINE
);
2207 vty_out (vty
, "File logging: ");
2208 if ((zl
->maxlvl
[ZLOG_DEST_FILE
] == ZLOG_DISABLED
) ||
2210 vty_out (vty
, "disabled");
2212 vty_out (vty
, "level %s, filename %s",
2213 zlog_priority
[zl
->maxlvl
[ZLOG_DEST_FILE
]],
2215 vty_out (vty
, "%s", VTY_NEWLINE
);
2217 vty_out (vty
, "Protocol name: %s%s",
2218 zl
->protoname
, VTY_NEWLINE
);
2219 vty_out (vty
, "Record priority: %s%s",
2220 (zl
->record_priority
? "enabled" : "disabled"), VTY_NEWLINE
);
2221 vty_out (vty
, "Timestamp precision: %d%s",
2222 zl
->timestamp_precision
, VTY_NEWLINE
);
2227 DEFUN (config_log_stdout
,
2228 config_log_stdout_cmd
,
2229 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2231 "Set stdout logging level\n"
2234 int idx_log_level
= 2;
2236 if (argc
== idx_log_level
)
2238 zlog_set_level (ZLOG_DEST_STDOUT
, zlog_default
->default_lvl
);
2243 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2244 return CMD_ERR_NO_MATCH
;
2245 zlog_set_level (ZLOG_DEST_STDOUT
, level
);
2249 DEFUN (no_config_log_stdout
,
2250 no_config_log_stdout_cmd
,
2251 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2254 "Cancel logging to stdout\n"
2257 zlog_set_level (ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
2261 DEFUN (config_log_monitor
,
2262 config_log_monitor_cmd
,
2263 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2265 "Set terminal line (monitor) logging level\n"
2268 int idx_log_level
= 2;
2270 if (argc
== idx_log_level
)
2272 zlog_set_level (ZLOG_DEST_MONITOR
, zlog_default
->default_lvl
);
2277 if ((level
= level_match(argv
[idx_log_level
]->arg
)) == ZLOG_DISABLED
)
2278 return CMD_ERR_NO_MATCH
;
2279 zlog_set_level (ZLOG_DEST_MONITOR
, level
);
2283 DEFUN (no_config_log_monitor
,
2284 no_config_log_monitor_cmd
,
2285 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2288 "Disable terminal line (monitor) logging\n"
2291 zlog_set_level (ZLOG_DEST_MONITOR
, ZLOG_DISABLED
);
2296 set_log_file(struct vty
*vty
, const char *fname
, int loglevel
)
2300 const char *fullpath
;
2302 /* Path detection. */
2303 if (! IS_DIRECTORY_SEP (*fname
))
2305 char cwd
[MAXPATHLEN
+1];
2306 cwd
[MAXPATHLEN
] = '\0';
2308 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
2310 zlog_err ("config_log_file: Unable to alloc mem!");
2314 if ( (p
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + strlen (fname
) + 2))
2317 zlog_err ("config_log_file: Unable to alloc mem!");
2320 sprintf (p
, "%s/%s", cwd
, fname
);
2326 ret
= zlog_set_file (fullpath
, loglevel
);
2329 XFREE (MTYPE_TMP
, p
);
2333 vty_out (vty
, "can't open logfile %s\n", fname
);
2338 XFREE (MTYPE_HOST
, host
.logfile
);
2340 host
.logfile
= XSTRDUP (MTYPE_HOST
, fname
);
2342 #if defined(HAVE_CUMULUS)
2343 if (zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] != ZLOG_DISABLED
)
2344 zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
] = ZLOG_DISABLED
;
2349 DEFUN (config_log_file
,
2350 config_log_file_cmd
,
2351 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2354 "Logging filename\n"
2357 int idx_filename
= 2;
2358 int idx_log_levels
= 3;
2362 if ((level
= level_match(argv
[idx_log_levels
]->arg
)) == ZLOG_DISABLED
)
2363 return CMD_ERR_NO_MATCH
;
2364 return set_log_file(vty
, argv
[idx_filename
]->arg
, level
);
2367 return set_log_file(vty
, argv
[idx_filename
]->arg
, zlog_default
->default_lvl
);
2370 DEFUN (no_config_log_file
,
2371 no_config_log_file_cmd
,
2372 "no log file [FILENAME [LEVEL]]",
2375 "Cancel logging to file\n"
2376 "Logging file name\n"
2377 "Logging file name\n"
2383 XFREE (MTYPE_HOST
, host
.logfile
);
2385 host
.logfile
= NULL
;
2390 DEFUN (config_log_syslog
,
2391 config_log_syslog_cmd
,
2392 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2394 "Set syslog logging level\n"
2397 int idx_log_levels
= 2;
2401 if ((level
= level_match (argv
[idx_log_levels
]->arg
)) == ZLOG_DISABLED
)
2402 return CMD_ERR_NO_MATCH
;
2403 zlog_set_level (ZLOG_DEST_SYSLOG
, level
);
2408 zlog_set_level (ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
2413 DEFUN (no_config_log_syslog
,
2414 no_config_log_syslog_cmd
,
2415 "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>]",
2418 "Cancel logging to syslog\n"
2422 zlog_set_level (ZLOG_DEST_SYSLOG
, ZLOG_DISABLED
);
2426 DEFUN (config_log_facility
,
2427 config_log_facility_cmd
,
2428 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2430 "Facility parameter for syslog messages\n"
2434 int facility
= facility_match(argv
[idx_target
]->arg
);
2436 zlog_default
->facility
= facility
;
2440 DEFUN (no_config_log_facility
,
2441 no_config_log_facility_cmd
,
2442 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2445 "Reset syslog facility to default (daemon)\n"
2448 zlog_default
->facility
= LOG_DAEMON
;
2452 DEFUN_DEPRECATED (config_log_trap
,
2453 config_log_trap_cmd
,
2454 "log trap <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>",
2456 "(Deprecated) Set logging level and default for all destinations\n"
2462 if ((new_level
= level_match(argv
[2]->arg
)) == ZLOG_DISABLED
)
2463 return CMD_ERR_NO_MATCH
;
2465 zlog_default
->default_lvl
= new_level
;
2466 for (i
= 0; i
< ZLOG_NUM_DESTS
; i
++)
2467 if (zlog_default
->maxlvl
[i
] != ZLOG_DISABLED
)
2468 zlog_default
->maxlvl
[i
] = new_level
;
2472 DEFUN_DEPRECATED (no_config_log_trap
,
2473 no_config_log_trap_cmd
,
2474 "no log trap [emergencies|alerts|critical|errors|warnings|notifications|informational|debugging]",
2477 "Permit all logging information\n"
2480 zlog_default
->default_lvl
= LOG_DEBUG
;
2484 DEFUN (config_log_record_priority
,
2485 config_log_record_priority_cmd
,
2486 "log record-priority",
2488 "Log the priority of the message within the message\n")
2490 zlog_default
->record_priority
= 1 ;
2494 DEFUN (no_config_log_record_priority
,
2495 no_config_log_record_priority_cmd
,
2496 "no log record-priority",
2499 "Do not log the priority of the message within the message\n")
2501 zlog_default
->record_priority
= 0 ;
2505 DEFUN (config_log_timestamp_precision
,
2506 config_log_timestamp_precision_cmd
,
2507 "log timestamp precision (0-6)",
2509 "Timestamp configuration\n"
2510 "Set the timestamp precision\n"
2511 "Number of subsecond digits\n")
2514 VTY_GET_INTEGER_RANGE("Timestamp Precision",
2515 zlog_default
->timestamp_precision
, argv
[idx_number
]->arg
, 0, 6);
2519 DEFUN (no_config_log_timestamp_precision
,
2520 no_config_log_timestamp_precision_cmd
,
2521 "no log timestamp precision",
2524 "Timestamp configuration\n"
2525 "Reset the timestamp precision to the default value of 0\n")
2527 zlog_default
->timestamp_precision
= 0 ;
2532 cmd_banner_motd_file (const char *file
)
2534 int success
= CMD_SUCCESS
;
2539 rpath
= realpath (file
, p
);
2541 return CMD_ERR_NO_FILE
;
2542 in
= strstr (rpath
, SYSCONFDIR
);
2546 XFREE (MTYPE_HOST
, host
.motdfile
);
2547 host
.motdfile
= XSTRDUP (MTYPE_HOST
, file
);
2550 success
= CMD_WARNING
;
2555 DEFUN (banner_motd_file
,
2556 banner_motd_file_cmd
,
2557 "banner motd file FILE",
2560 "Banner from a file\n"
2564 const char *filename
= argv
[idx_file
]->arg
;
2565 int cmd
= cmd_banner_motd_file (filename
);
2567 if (cmd
== CMD_ERR_NO_FILE
)
2568 vty_out (vty
, "%s does not exist", filename
);
2569 else if (cmd
== CMD_WARNING
)
2570 vty_out (vty
, "%s must be in %s", filename
, SYSCONFDIR
);
2575 DEFUN (banner_motd_default
,
2576 banner_motd_default_cmd
,
2577 "banner motd default",
2578 "Set banner string\n"
2579 "Strings for motd\n"
2582 host
.motd
= default_motd
;
2586 DEFUN (no_banner_motd
,
2590 "Set banner string\n"
2591 "Strings for motd\n")
2595 XFREE (MTYPE_HOST
, host
.motdfile
);
2596 host
.motdfile
= NULL
;
2600 /* Set config filename. Called from vty.c */
2602 host_config_set (const char *filename
)
2605 XFREE (MTYPE_HOST
, host
.config
);
2606 host
.config
= XSTRDUP (MTYPE_HOST
, filename
);
2610 host_config_get (void)
2616 install_default (enum node_type node
)
2618 install_element (node
, &config_exit_cmd
);
2619 install_element (node
, &config_quit_cmd
);
2620 install_element (node
, &config_end_cmd
);
2621 install_element (node
, &config_help_cmd
);
2622 install_element (node
, &config_list_cmd
);
2624 install_element (node
, &config_write_cmd
);
2625 install_element (node
, &show_running_config_cmd
);
2628 /* Initialize command interface. Install basic nodes and commands.
2630 * terminal = 0 -- vtysh / no logging, no config control
2631 * terminal = 1 -- normal daemon
2632 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2634 cmd_init (int terminal
)
2638 /* Allocate initial top vector of commands. */
2639 cmdvec
= vector_init (VECTOR_MIN_SIZE
);
2641 /* Default host value settings. */
2643 host
.password
= NULL
;
2645 host
.logfile
= NULL
;
2647 host
.noconfig
= (terminal
< 0);
2649 host
.motd
= default_motd
;
2650 host
.motdfile
= NULL
;
2652 /* Install top nodes. */
2653 install_node (&view_node
, NULL
);
2654 install_node (&enable_node
, NULL
);
2655 install_node (&auth_node
, NULL
);
2656 install_node (&auth_enable_node
, NULL
);
2657 install_node (&config_node
, config_write_host
);
2659 /* Each node's basic commands. */
2660 install_element (VIEW_NODE
, &show_version_cmd
);
2663 install_element (VIEW_NODE
, &config_list_cmd
);
2664 install_element (VIEW_NODE
, &config_exit_cmd
);
2665 install_element (VIEW_NODE
, &config_quit_cmd
);
2666 install_element (VIEW_NODE
, &config_help_cmd
);
2667 install_element (VIEW_NODE
, &config_enable_cmd
);
2668 install_element (VIEW_NODE
, &config_terminal_length_cmd
);
2669 install_element (VIEW_NODE
, &config_terminal_no_length_cmd
);
2670 install_element (VIEW_NODE
, &show_logging_cmd
);
2671 install_element (VIEW_NODE
, &show_commandtree_cmd
);
2672 install_element (VIEW_NODE
, &echo_cmd
);
2677 install_element (ENABLE_NODE
, &config_end_cmd
);
2678 install_element (ENABLE_NODE
, &config_disable_cmd
);
2679 install_element (ENABLE_NODE
, &config_terminal_cmd
);
2680 install_element (ENABLE_NODE
, ©_runningconf_startupconf_cmd
);
2681 install_element (ENABLE_NODE
, &config_write_cmd
);
2682 install_element (ENABLE_NODE
, &show_running_config_cmd
);
2684 install_element (ENABLE_NODE
, &show_startup_config_cmd
);
2688 install_element (ENABLE_NODE
, &config_logmsg_cmd
);
2689 install_default (CONFIG_NODE
);
2692 workqueue_cmd_init ();
2695 install_element (CONFIG_NODE
, &hostname_cmd
);
2696 install_element (CONFIG_NODE
, &no_hostname_cmd
);
2700 install_element (CONFIG_NODE
, &password_cmd
);
2701 install_element (CONFIG_NODE
, &enable_password_cmd
);
2702 install_element (CONFIG_NODE
, &no_enable_password_cmd
);
2704 install_element (CONFIG_NODE
, &config_log_stdout_cmd
);
2705 install_element (CONFIG_NODE
, &no_config_log_stdout_cmd
);
2706 install_element (CONFIG_NODE
, &config_log_monitor_cmd
);
2707 install_element (CONFIG_NODE
, &no_config_log_monitor_cmd
);
2708 install_element (CONFIG_NODE
, &config_log_file_cmd
);
2709 install_element (CONFIG_NODE
, &no_config_log_file_cmd
);
2710 install_element (CONFIG_NODE
, &config_log_syslog_cmd
);
2711 install_element (CONFIG_NODE
, &no_config_log_syslog_cmd
);
2712 install_element (CONFIG_NODE
, &config_log_facility_cmd
);
2713 install_element (CONFIG_NODE
, &no_config_log_facility_cmd
);
2714 install_element (CONFIG_NODE
, &config_log_trap_cmd
);
2715 install_element (CONFIG_NODE
, &no_config_log_trap_cmd
);
2716 install_element (CONFIG_NODE
, &config_log_record_priority_cmd
);
2717 install_element (CONFIG_NODE
, &no_config_log_record_priority_cmd
);
2718 install_element (CONFIG_NODE
, &config_log_timestamp_precision_cmd
);
2719 install_element (CONFIG_NODE
, &no_config_log_timestamp_precision_cmd
);
2720 install_element (CONFIG_NODE
, &service_password_encrypt_cmd
);
2721 install_element (CONFIG_NODE
, &no_service_password_encrypt_cmd
);
2722 install_element (CONFIG_NODE
, &banner_motd_default_cmd
);
2723 install_element (CONFIG_NODE
, &banner_motd_file_cmd
);
2724 install_element (CONFIG_NODE
, &no_banner_motd_cmd
);
2725 install_element (CONFIG_NODE
, &service_terminal_length_cmd
);
2726 install_element (CONFIG_NODE
, &no_service_terminal_length_cmd
);
2728 vrf_install_commands ();
2732 grammar_sandbox_init();
2737 new_cmd_token (enum cmd_token_type type
, u_char attr
,
2738 const char *text
, const char *desc
)
2740 struct cmd_token
*token
= XCALLOC (MTYPE_CMD_TOKENS
, sizeof (struct cmd_token
));
2743 token
->text
= text
? XSTRDUP (MTYPE_CMD_TEXT
, text
) : NULL
;
2744 token
->desc
= desc
? XSTRDUP (MTYPE_CMD_DESC
, desc
) : NULL
;
2747 token
->allowrepeat
= false;
2753 del_cmd_token (struct cmd_token
*token
)
2758 XFREE (MTYPE_CMD_TEXT
, token
->text
);
2760 XFREE (MTYPE_CMD_DESC
, token
->desc
);
2762 XFREE (MTYPE_CMD_ARG
, token
->arg
);
2764 XFREE (MTYPE_CMD_TOKENS
, token
);
2768 copy_cmd_token (struct cmd_token
*token
)
2770 struct cmd_token
*copy
= new_cmd_token (token
->type
, token
->attr
, NULL
, NULL
);
2771 copy
->max
= token
->max
;
2772 copy
->min
= token
->min
;
2773 copy
->text
= token
->text
? XSTRDUP (MTYPE_CMD_TEXT
, token
->text
) : NULL
;
2774 copy
->desc
= token
->desc
? XSTRDUP (MTYPE_CMD_DESC
, token
->desc
) : NULL
;
2775 copy
->arg
= token
->arg
? XSTRDUP (MTYPE_CMD_ARG
, token
->arg
) : NULL
;
2783 struct cmd_node
*cmd_node
;
2787 for (unsigned int i
= 0; i
< vector_active (cmdvec
); i
++)
2788 if ((cmd_node
= vector_slot (cmdvec
, i
)) != NULL
)
2790 // deleting the graph delets the cmd_element as well
2791 graph_delete_graph (cmd_node
->cmdgraph
);
2792 vector_free (cmd_node
->cmd_vector
);
2793 hash_clean (cmd_node
->cmd_hash
, NULL
);
2794 hash_free (cmd_node
->cmd_hash
);
2795 cmd_node
->cmd_hash
= NULL
;
2798 vector_free (cmdvec
);
2803 XFREE (MTYPE_HOST
, host
.name
);
2805 XFREE (MTYPE_HOST
, host
.password
);
2806 if (host
.password_encrypt
)
2807 XFREE (MTYPE_HOST
, host
.password_encrypt
);
2809 XFREE (MTYPE_HOST
, host
.enable
);
2810 if (host
.enable_encrypt
)
2811 XFREE (MTYPE_HOST
, host
.enable_encrypt
);
2813 XFREE (MTYPE_HOST
, host
.logfile
);
2815 XFREE (MTYPE_HOST
, host
.motdfile
);
2817 XFREE (MTYPE_HOST
, host
.config
);