]> git.proxmox.com Git - mirror_frr.git/blob - lib/command.c
Merge pull request #2829 from donaldsharp/more_upstream
[mirror_frr.git] / lib / command.c
1 /*
2 * CLI backend interface.
3 *
4 * --
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")
9 *
10 * This file is part of GNU Zebra.
11 *
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
15 * later version.
16 *
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.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; see the file COPYING; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 #include <zebra.h>
28 #include <lib/version.h>
29
30 #include "command.h"
31 #include "frrstr.h"
32 #include "memory.h"
33 #include "log.h"
34 #include "log_int.h"
35 #include "thread.h"
36 #include "vector.h"
37 #include "linklist.h"
38 #include "vty.h"
39 #include "workqueue.h"
40 #include "vrf.h"
41 #include "command_match.h"
42 #include "command_graph.h"
43 #include "qobj.h"
44 #include "defaults.h"
45 #include "libfrr.h"
46 #include "jhash.h"
47 #include "hook.h"
48 #include "lib_errors.h"
49
50 DEFINE_MTYPE(LIB, HOST, "Host config")
51 DEFINE_MTYPE(LIB, COMPLETION, "Completion item")
52
53 #define item(x) \
54 { \
55 x, #x \
56 }
57
58 /* clang-format off */
59 const struct message tokennames[] = {
60 item(WORD_TKN),
61 item(VARIABLE_TKN),
62 item(RANGE_TKN),
63 item(IPV4_TKN),
64 item(IPV4_PREFIX_TKN),
65 item(IPV6_TKN),
66 item(IPV6_PREFIX_TKN),
67 item(MAC_TKN),
68 item(MAC_PREFIX_TKN),
69 item(FORK_TKN),
70 item(JOIN_TKN),
71 item(START_TKN),
72 item(END_TKN),
73 {0},
74 };
75
76 const char *node_names[] = {
77 "auth", // AUTH_NODE,
78 "view", // VIEW_NODE,
79 "auth enable", // AUTH_ENABLE_NODE,
80 "enable", // ENABLE_NODE,
81 "config", // CONFIG_NODE,
82 "debug", // DEBUG_NODE,
83 "vrf debug", // VRF_DEBUG_NODE,
84 "vnc debug", // DEBUG_VNC_NODE,
85 "aaa", // AAA_NODE,
86 "keychain", // KEYCHAIN_NODE,
87 "keychain key", // KEYCHAIN_KEY_NODE,
88 "logical-router", // LOGICALROUTER_NODE,
89 "static ip", // IP_NODE,
90 "vrf", // VRF_NODE,
91 "interface", // INTERFACE_NODE,
92 "nexthop-group", // NH_GROUP_NODE,
93 "zebra", // ZEBRA_NODE,
94 "table", // TABLE_NODE,
95 "rip", // RIP_NODE,
96 "ripng", // RIPNG_NODE,
97 "babel", // BABEL_NODE,
98 "eigrp", // EIGRP_NODE,
99 "bgp", // BGP_NODE,
100 "bgp vpnv4", // BGP_VPNV4_NODE,
101 "bgp vpnv6", // BGP_VPNV6_NODE,
102 "bgp ipv4 unicast", // BGP_IPV4_NODE,
103 "bgp ipv4 multicast", // BGP_IPV4M_NODE,
104 "bgp ipv4 labeled unicast", // BGP_IPV4L_NODE,
105 "bgp ipv6", // BGP_IPV6_NODE,
106 "bgp ipv6 multicast", // BGP_IPV6M_NODE,
107 "bgp ipv6 labeled unicast", // BGP_IPV6L_NODE,
108 "bgp vrf policy", // BGP_VRF_POLICY_NODE,
109 "bgp vnc defaults", // BGP_VNC_DEFAULTS_NODE,
110 "bgp vnc nve", // BGP_VNC_NVE_GROUP_NODE,
111 "bgp vnc l2", // BGP_VNC_L2_GROUP_NODE,
112 "rfp defaults", // RFP_DEFAULTS_NODE,
113 "bgp evpn", // BGP_EVPN_NODE,
114 "ospf", // OSPF_NODE,
115 "ospf6", // OSPF6_NODE,
116 "ldp", // LDP_NODE,
117 "ldp ipv4", // LDP_IPV4_NODE,
118 "ldp ipv6", // LDP_IPV6_NODE,
119 "ldp ipv4 interface", // LDP_IPV4_IFACE_NODE,
120 "ldp ipv6 interface", // LDP_IPV6_IFACE_NODE,
121 "ldp l2vpn", // LDP_L2VPN_NODE,
122 "ldp", // LDP_PSEUDOWIRE_NODE,
123 "isis", // ISIS_NODE,
124 "ipv4 access list", // ACCESS_NODE,
125 "ipv4 prefix list", // PREFIX_NODE,
126 "ipv6 access list", // ACCESS_IPV6_NODE,
127 "MAC access list", // ACCESS_MAC_NODE,
128 "ipv6 prefix list", // PREFIX_IPV6_NODE,
129 "as list", // AS_LIST_NODE,
130 "community list", // COMMUNITY_LIST_NODE,
131 "routemap", // RMAP_NODE,
132 "pbr-map", // PBRMAP_NODE,
133 "smux", // SMUX_NODE,
134 "dump", // DUMP_NODE,
135 "forwarding", // FORWARDING_NODE,
136 "protocol", // PROTOCOL_NODE,
137 "mpls", // MPLS_NODE,
138 "pw", // PW_NODE,
139 "vty", // VTY_NODE,
140 "link-params", // LINK_PARAMS_NODE,
141 "bgp evpn vni", // BGP_EVPN_VNI_NODE,
142 "rpki", // RPKI_NODE
143 "bgp ipv4 flowspec", /* BGP_FLOWSPECV4_NODE
144 */
145 "bgp ipv6 flowspec", /* BGP_FLOWSPECV6_NODE
146 */
147 "bfd", /* BFD_NODE */
148 "bfd peer", /* BFD_PEER_NODE */
149 };
150 /* clang-format on */
151
152 /* Command vector which includes some level of command lists. Normally
153 each daemon maintains each own cmdvec. */
154 vector cmdvec = NULL;
155
156 /* Host information structure. */
157 struct host host;
158
159 /*
160 * Returns host.name if any, otherwise
161 * it returns the system hostname.
162 */
163 const char *cmd_hostname_get(void)
164 {
165 return host.name;
166 }
167
168 /*
169 * Returns unix domainname
170 */
171 const char *cmd_domainname_get(void)
172 {
173 return host.domainname;
174 }
175
176 /* Standard command node structures. */
177 static struct cmd_node auth_node = {
178 AUTH_NODE, "Password: ",
179 };
180
181 static struct cmd_node view_node = {
182 VIEW_NODE, "%s> ",
183 };
184
185 static struct cmd_node auth_enable_node = {
186 AUTH_ENABLE_NODE, "Password: ",
187 };
188
189 static struct cmd_node enable_node = {
190 ENABLE_NODE, "%s# ",
191 };
192
193 static struct cmd_node config_node = {CONFIG_NODE, "%s(config)# ", 1};
194
195 /* Default motd string. */
196 static const char *default_motd = FRR_DEFAULT_MOTD;
197
198 static const struct facility_map {
199 int facility;
200 const char *name;
201 size_t match;
202 } syslog_facilities[] = {
203 {LOG_KERN, "kern", 1},
204 {LOG_USER, "user", 2},
205 {LOG_MAIL, "mail", 1},
206 {LOG_DAEMON, "daemon", 1},
207 {LOG_AUTH, "auth", 1},
208 {LOG_SYSLOG, "syslog", 1},
209 {LOG_LPR, "lpr", 2},
210 {LOG_NEWS, "news", 1},
211 {LOG_UUCP, "uucp", 2},
212 {LOG_CRON, "cron", 1},
213 #ifdef LOG_FTP
214 {LOG_FTP, "ftp", 1},
215 #endif
216 {LOG_LOCAL0, "local0", 6},
217 {LOG_LOCAL1, "local1", 6},
218 {LOG_LOCAL2, "local2", 6},
219 {LOG_LOCAL3, "local3", 6},
220 {LOG_LOCAL4, "local4", 6},
221 {LOG_LOCAL5, "local5", 6},
222 {LOG_LOCAL6, "local6", 6},
223 {LOG_LOCAL7, "local7", 6},
224 {0, NULL, 0},
225 };
226
227 static const char *facility_name(int facility)
228 {
229 const struct facility_map *fm;
230
231 for (fm = syslog_facilities; fm->name; fm++)
232 if (fm->facility == facility)
233 return fm->name;
234 return "";
235 }
236
237 static int facility_match(const char *str)
238 {
239 const struct facility_map *fm;
240
241 for (fm = syslog_facilities; fm->name; fm++)
242 if (!strncmp(str, fm->name, fm->match))
243 return fm->facility;
244 return -1;
245 }
246
247 static int level_match(const char *s)
248 {
249 int level;
250
251 for (level = 0; zlog_priority[level] != NULL; level++)
252 if (!strncmp(s, zlog_priority[level], 2))
253 return level;
254 return ZLOG_DISABLED;
255 }
256
257 /* This is called from main when a daemon is invoked with -v or --version. */
258 void print_version(const char *progname)
259 {
260 printf("%s version %s\n", progname, FRR_VERSION);
261 printf("%s\n", FRR_COPYRIGHT);
262 printf("configured with:\n\t%s\n", FRR_CONFIG_ARGS);
263 }
264
265 char *argv_concat(struct cmd_token **argv, int argc, int shift)
266 {
267 int cnt = MAX(argc - shift, 0);
268 const char *argstr[cnt + 1];
269
270 if (!cnt)
271 return NULL;
272
273 for (int i = 0; i < cnt; i++)
274 argstr[i] = argv[i + shift]->arg;
275
276 return frrstr_join(argstr, cnt, " ");
277 }
278
279 vector cmd_make_strvec(const char *string)
280 {
281 if (!string)
282 return NULL;
283
284 const char *copy = string;
285
286 /* skip leading whitespace */
287 while (isspace((int)*copy) && *copy != '\0')
288 copy++;
289
290 /* if the entire string was whitespace or a comment, return */
291 if (*copy == '\0' || *copy == '!' || *copy == '#')
292 return NULL;
293
294 vector result = frrstr_split_vec(copy, "\n\r\t ");
295
296 for (unsigned int i = 0; i < vector_active(result); i++) {
297 if (strlen(vector_slot(result, i)) == 0) {
298 XFREE(MTYPE_TMP, vector_slot(result, i));
299 vector_unset(result, i);
300 }
301 }
302
303 vector_compact(result);
304
305 return result;
306 }
307
308 void cmd_free_strvec(vector v)
309 {
310 frrstr_strvec_free(v);
311 }
312
313 /**
314 * Convenience function for accessing argv data.
315 *
316 * @param argc
317 * @param argv
318 * @param text definition snippet of the desired token
319 * @param index the starting index, and where to store the
320 * index of the found token if it exists
321 * @return 1 if found, 0 otherwise
322 */
323 int argv_find(struct cmd_token **argv, int argc, const char *text, int *index)
324 {
325 int found = 0;
326 for (int i = *index; i < argc && found == 0; i++)
327 if ((found = strmatch(text, argv[i]->text)))
328 *index = i;
329 return found;
330 }
331
332 static unsigned int cmd_hash_key(void *p)
333 {
334 int size = sizeof(p);
335
336 return jhash(p, size, 0);
337 }
338
339 static int cmd_hash_cmp(const void *a, const void *b)
340 {
341 return a == b;
342 }
343
344 /* Install top node of command vector. */
345 void install_node(struct cmd_node *node, int (*func)(struct vty *))
346 {
347 vector_set_index(cmdvec, node->node, node);
348 node->func = func;
349 node->cmdgraph = graph_new();
350 node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
351 // add start node
352 struct cmd_token *token =
353 cmd_token_new(START_TKN, CMD_ATTR_NORMAL, NULL, NULL);
354 graph_new_node(node->cmdgraph, token,
355 (void (*)(void *)) & cmd_token_del);
356 node->cmd_hash = hash_create_size(16, cmd_hash_key, cmd_hash_cmp,
357 "Command Hash");
358 }
359
360 /* Return prompt character of specified node. */
361 const char *cmd_prompt(enum node_type node)
362 {
363 struct cmd_node *cnode;
364
365 cnode = vector_slot(cmdvec, node);
366 return cnode->prompt;
367 }
368
369 /* Install a command into a node. */
370 void install_element(enum node_type ntype, struct cmd_element *cmd)
371 {
372 struct cmd_node *cnode;
373
374 /* cmd_init hasn't been called */
375 if (!cmdvec) {
376 fprintf(stderr, "%s called before cmd_init, breakage likely\n",
377 __func__);
378 return;
379 }
380
381 cnode = vector_lookup(cmdvec, ntype);
382
383 if (cnode == NULL) {
384 fprintf(stderr,
385 "%s[%s]:\n"
386 "\tnode %d (%s) does not exist.\n"
387 "\tplease call install_node() before install_element()\n",
388 cmd->name, cmd->string, ntype, node_names[ntype]);
389 exit(EXIT_FAILURE);
390 }
391
392 if (hash_lookup(cnode->cmd_hash, cmd) != NULL) {
393 fprintf(stderr,
394 "%s[%s]:\n"
395 "\tnode %d (%s) already has this command installed.\n"
396 "\tduplicate install_element call?\n",
397 cmd->name, cmd->string, ntype, node_names[ntype]);
398 return;
399 }
400
401 assert(hash_get(cnode->cmd_hash, cmd, hash_alloc_intern));
402
403 struct graph *graph = graph_new();
404 struct cmd_token *token =
405 cmd_token_new(START_TKN, CMD_ATTR_NORMAL, NULL, NULL);
406 graph_new_node(graph, token, (void (*)(void *)) & cmd_token_del);
407
408 cmd_graph_parse(graph, cmd);
409 cmd_graph_names(graph);
410 cmd_graph_merge(cnode->cmdgraph, graph, +1);
411 graph_delete_graph(graph);
412
413 vector_set(cnode->cmd_vector, cmd);
414
415 if (ntype == VIEW_NODE)
416 install_element(ENABLE_NODE, cmd);
417 }
418
419 void uninstall_element(enum node_type ntype, struct cmd_element *cmd)
420 {
421 struct cmd_node *cnode;
422
423 /* cmd_init hasn't been called */
424 if (!cmdvec) {
425 fprintf(stderr, "%s called before cmd_init, breakage likely\n",
426 __func__);
427 return;
428 }
429
430 cnode = vector_lookup(cmdvec, ntype);
431
432 if (cnode == NULL) {
433 fprintf(stderr,
434 "%s[%s]:\n"
435 "\tnode %d (%s) does not exist.\n"
436 "\tplease call install_node() before uninstall_element()\n",
437 cmd->name, cmd->string, ntype, node_names[ntype]);
438 exit(EXIT_FAILURE);
439 }
440
441 if (hash_release(cnode->cmd_hash, cmd) == NULL) {
442 fprintf(stderr,
443 "%s[%s]:\n"
444 "\tnode %d (%s) does not have this command installed.\n"
445 "\tduplicate uninstall_element call?\n",
446 cmd->name, cmd->string, ntype, node_names[ntype]);
447 return;
448 }
449
450 vector_unset_value(cnode->cmd_vector, cmd);
451
452 struct graph *graph = graph_new();
453 struct cmd_token *token =
454 cmd_token_new(START_TKN, CMD_ATTR_NORMAL, NULL, NULL);
455 graph_new_node(graph, token, (void (*)(void *)) & cmd_token_del);
456
457 cmd_graph_parse(graph, cmd);
458 cmd_graph_names(graph);
459 cmd_graph_merge(cnode->cmdgraph, graph, -1);
460 graph_delete_graph(graph);
461
462 if (ntype == VIEW_NODE)
463 uninstall_element(ENABLE_NODE, cmd);
464 }
465
466
467 static const unsigned char itoa64[] =
468 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
469
470 static void to64(char *s, long v, int n)
471 {
472 while (--n >= 0) {
473 *s++ = itoa64[v & 0x3f];
474 v >>= 6;
475 }
476 }
477
478 static char *zencrypt(const char *passwd)
479 {
480 char salt[6];
481 struct timeval tv;
482 char *crypt(const char *, const char *);
483
484 gettimeofday(&tv, 0);
485
486 to64(&salt[0], random(), 3);
487 to64(&salt[3], tv.tv_usec, 3);
488 salt[5] = '\0';
489
490 return crypt(passwd, salt);
491 }
492
493 /* This function write configuration of this host. */
494 static int config_write_host(struct vty *vty)
495 {
496 if (cmd_hostname_get())
497 vty_out(vty, "hostname %s\n", cmd_hostname_get());
498
499 if (cmd_domainname_get())
500 vty_out(vty, "domainname %s\n", cmd_domainname_get());
501
502 /* The following are all configuration commands that are not sent to
503 * watchfrr. For instance watchfrr is hardcoded to log to syslog so
504 * we would always display 'log syslog informational' in the config
505 * which would cause other daemons to then switch to syslog when they
506 * parse frr.conf.
507 */
508 if (strcmp(zlog_default->protoname, "WATCHFRR")) {
509 if (host.encrypt) {
510 if (host.password_encrypt)
511 vty_out(vty, "password 8 %s\n",
512 host.password_encrypt);
513 if (host.enable_encrypt)
514 vty_out(vty, "enable password 8 %s\n",
515 host.enable_encrypt);
516 } else {
517 if (host.password)
518 vty_out(vty, "password %s\n", host.password);
519 if (host.enable)
520 vty_out(vty, "enable password %s\n",
521 host.enable);
522 }
523
524 if (host.logfile
525 && (zlog_default->maxlvl[ZLOG_DEST_FILE]
526 != ZLOG_DISABLED)) {
527 vty_out(vty, "log file %s", host.logfile);
528 if (zlog_default->maxlvl[ZLOG_DEST_FILE]
529 != zlog_default->default_lvl)
530 vty_out(vty, " %s",
531 zlog_priority
532 [zlog_default->maxlvl
533 [ZLOG_DEST_FILE]]);
534 vty_out(vty, "\n");
535 }
536
537 if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != ZLOG_DISABLED) {
538 vty_out(vty, "log stdout");
539 if (zlog_default->maxlvl[ZLOG_DEST_STDOUT]
540 != zlog_default->default_lvl)
541 vty_out(vty, " %s",
542 zlog_priority
543 [zlog_default->maxlvl
544 [ZLOG_DEST_STDOUT]]);
545 vty_out(vty, "\n");
546 }
547
548 if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
549 vty_out(vty, "no log monitor\n");
550 else if (zlog_default->maxlvl[ZLOG_DEST_MONITOR]
551 != zlog_default->default_lvl)
552 vty_out(vty, "log monitor %s\n",
553 zlog_priority[zlog_default->maxlvl
554 [ZLOG_DEST_MONITOR]]);
555
556 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED) {
557 vty_out(vty, "log syslog");
558 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG]
559 != zlog_default->default_lvl)
560 vty_out(vty, " %s",
561 zlog_priority[zlog_default->maxlvl
562 [ZLOG_DEST_SYSLOG]]);
563 vty_out(vty, "\n");
564 }
565
566 if (zlog_default->facility != LOG_DAEMON)
567 vty_out(vty, "log facility %s\n",
568 facility_name(zlog_default->facility));
569
570 if (zlog_default->record_priority == 1)
571 vty_out(vty, "log record-priority\n");
572
573 if (zlog_default->timestamp_precision > 0)
574 vty_out(vty, "log timestamp precision %d\n",
575 zlog_default->timestamp_precision);
576
577 if (host.advanced)
578 vty_out(vty, "service advanced-vty\n");
579
580 if (host.encrypt)
581 vty_out(vty, "service password-encryption\n");
582
583 if (host.lines >= 0)
584 vty_out(vty, "service terminal-length %d\n",
585 host.lines);
586
587 if (host.motdfile)
588 vty_out(vty, "banner motd file %s\n", host.motdfile);
589 else if (!host.motd)
590 vty_out(vty, "no banner motd\n");
591 }
592
593 if (debug_memstats_at_exit)
594 vty_out(vty, "!\ndebug memstats-at-exit\n");
595
596 return 1;
597 }
598
599 /* Utility function for getting command graph. */
600 static struct graph *cmd_node_graph(vector v, enum node_type ntype)
601 {
602 struct cmd_node *cnode = vector_slot(v, ntype);
603 return cnode->cmdgraph;
604 }
605
606 static int cmd_try_do_shortcut(enum node_type node, char *first_word)
607 {
608 if (first_word != NULL && node != AUTH_NODE && node != VIEW_NODE
609 && node != AUTH_ENABLE_NODE && 0 == strcmp("do", first_word))
610 return 1;
611 return 0;
612 }
613
614 /**
615 * Compare function for cmd_token.
616 * Used with qsort to sort command completions.
617 */
618 static int compare_completions(const void *fst, const void *snd)
619 {
620 struct cmd_token *first = *(struct cmd_token **)fst,
621 *secnd = *(struct cmd_token **)snd;
622 return strcmp(first->text, secnd->text);
623 }
624
625 /**
626 * Takes a list of completions returned by command_complete,
627 * dedeuplicates them based on both text and description,
628 * sorts them, and returns them as a vector.
629 *
630 * @param completions linked list of cmd_token
631 * @return deduplicated and sorted vector with
632 */
633 vector completions_to_vec(struct list *completions)
634 {
635 vector comps = vector_init(VECTOR_MIN_SIZE);
636
637 struct listnode *ln;
638 struct cmd_token *token, *cr = NULL;
639 unsigned int i, exists;
640 for (ALL_LIST_ELEMENTS_RO(completions, ln, token)) {
641 if (token->type == END_TKN && (cr = token))
642 continue;
643
644 // linear search for token in completions vector
645 exists = 0;
646 for (i = 0; i < vector_active(comps) && !exists; i++) {
647 struct cmd_token *curr = vector_slot(comps, i);
648 #ifdef VTYSH_DEBUG
649 exists = !strcmp(curr->text, token->text)
650 && !strcmp(curr->desc, token->desc);
651 #else
652 exists = !strcmp(curr->text, token->text);
653 #endif /* VTYSH_DEBUG */
654 }
655
656 if (!exists)
657 vector_set(comps, token);
658 }
659
660 // sort completions
661 qsort(comps->index, vector_active(comps), sizeof(void *),
662 &compare_completions);
663
664 // make <cr> the first element, if it is present
665 if (cr) {
666 vector_set_index(comps, vector_active(comps), NULL);
667 memmove(comps->index + 1, comps->index,
668 (comps->alloced - 1) * sizeof(void *));
669 vector_set_index(comps, 0, cr);
670 }
671
672 return comps;
673 }
674 /**
675 * Generates a vector of cmd_token representing possible completions
676 * on the current input.
677 *
678 * @param vline the vectorized input line
679 * @param vty the vty with the node to match on
680 * @param status pointer to matcher status code
681 * @return vector of struct cmd_token * with possible completions
682 */
683 static vector cmd_complete_command_real(vector vline, struct vty *vty,
684 int *status)
685 {
686 struct list *completions;
687 struct graph *cmdgraph = cmd_node_graph(cmdvec, vty->node);
688
689 enum matcher_rv rv = command_complete(cmdgraph, vline, &completions);
690
691 if (MATCHER_ERROR(rv)) {
692 *status = CMD_ERR_NO_MATCH;
693 return NULL;
694 }
695
696 vector comps = completions_to_vec(completions);
697 list_delete_and_null(&completions);
698
699 // set status code appropriately
700 switch (vector_active(comps)) {
701 case 0:
702 *status = CMD_ERR_NO_MATCH;
703 break;
704 case 1:
705 *status = CMD_COMPLETE_FULL_MATCH;
706 break;
707 default:
708 *status = CMD_COMPLETE_LIST_MATCH;
709 }
710
711 return comps;
712 }
713
714 vector cmd_describe_command(vector vline, struct vty *vty, int *status)
715 {
716 vector ret;
717
718 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
719 enum node_type onode;
720 vector shifted_vline;
721 unsigned int index;
722
723 onode = vty->node;
724 vty->node = ENABLE_NODE;
725 /* We can try it on enable node, cos' the vty is authenticated
726 */
727
728 shifted_vline = vector_init(vector_count(vline));
729 /* use memcpy? */
730 for (index = 1; index < vector_active(vline); index++) {
731 vector_set_index(shifted_vline, index - 1,
732 vector_lookup(vline, index));
733 }
734
735 ret = cmd_complete_command_real(shifted_vline, vty, status);
736
737 vector_free(shifted_vline);
738 vty->node = onode;
739 return ret;
740 }
741
742 return cmd_complete_command_real(vline, vty, status);
743 }
744
745 static struct list *varhandlers = NULL;
746
747 void cmd_variable_complete(struct cmd_token *token, const char *arg,
748 vector comps)
749 {
750 struct listnode *ln;
751 const struct cmd_variable_handler *cvh;
752 size_t i, argsz;
753 vector tmpcomps;
754
755 tmpcomps = arg ? vector_init(VECTOR_MIN_SIZE) : comps;
756
757 for (ALL_LIST_ELEMENTS_RO(varhandlers, ln, cvh)) {
758 if (cvh->tokenname && strcmp(cvh->tokenname, token->text))
759 continue;
760 if (cvh->varname && (!token->varname
761 || strcmp(cvh->varname, token->varname)))
762 continue;
763 cvh->completions(tmpcomps, token);
764 break;
765 }
766
767 if (!arg)
768 return;
769
770 argsz = strlen(arg);
771 for (i = vector_active(tmpcomps); i; i--) {
772 char *item = vector_slot(tmpcomps, i - 1);
773 if (strlen(item) >= argsz && !strncmp(item, arg, argsz))
774 vector_set(comps, item);
775 else
776 XFREE(MTYPE_COMPLETION, item);
777 }
778 vector_free(tmpcomps);
779 }
780
781 #define AUTOCOMP_INDENT 5
782
783 char *cmd_variable_comp2str(vector comps, unsigned short cols)
784 {
785 size_t bsz = 16;
786 char *buf = XCALLOC(MTYPE_TMP, bsz);
787 int lc = AUTOCOMP_INDENT;
788 size_t cs = AUTOCOMP_INDENT;
789 size_t itemlen;
790 snprintf(buf, bsz, "%*s", AUTOCOMP_INDENT, "");
791 for (size_t j = 0; j < vector_active(comps); j++) {
792 char *item = vector_slot(comps, j);
793 itemlen = strlen(item);
794
795 if (cs + itemlen + AUTOCOMP_INDENT + 3 >= bsz)
796 buf = XREALLOC(MTYPE_TMP, buf, (bsz *= 2));
797
798 if (lc + itemlen + 1 >= cols) {
799 cs += snprintf(&buf[cs], bsz - cs, "\n%*s",
800 AUTOCOMP_INDENT, "");
801 lc = AUTOCOMP_INDENT;
802 }
803
804 size_t written = snprintf(&buf[cs], bsz - cs, "%s ", item);
805 lc += written;
806 cs += written;
807 XFREE(MTYPE_COMPLETION, item);
808 vector_set_index(comps, j, NULL);
809 }
810 return buf;
811 }
812
813 void cmd_variable_handler_register(const struct cmd_variable_handler *cvh)
814 {
815 if (!varhandlers)
816 return;
817
818 for (; cvh->completions; cvh++)
819 listnode_add(varhandlers, (void *)cvh);
820 }
821
822 DEFUN_HIDDEN (autocomplete,
823 autocomplete_cmd,
824 "autocomplete TYPE TEXT VARNAME",
825 "Autocompletion handler (internal, for vtysh)\n"
826 "cmd_token->type\n"
827 "cmd_token->text\n"
828 "cmd_token->varname\n")
829 {
830 struct cmd_token tok;
831 vector comps = vector_init(32);
832 size_t i;
833
834 memset(&tok, 0, sizeof(tok));
835 tok.type = atoi(argv[1]->arg);
836 tok.text = argv[2]->arg;
837 tok.varname = argv[3]->arg;
838 if (!strcmp(tok.varname, "-"))
839 tok.varname = NULL;
840
841 cmd_variable_complete(&tok, NULL, comps);
842
843 for (i = 0; i < vector_active(comps); i++) {
844 char *text = vector_slot(comps, i);
845 vty_out(vty, "%s\n", text);
846 XFREE(MTYPE_COMPLETION, text);
847 }
848
849 vector_free(comps);
850 return CMD_SUCCESS;
851 }
852
853 /**
854 * Generate possible tab-completions for the given input. This function only
855 * returns results that would result in a valid command if used as Readline
856 * completions (as is the case in vtysh). For instance, if the passed vline ends
857 * with '4.3.2', the strings 'A.B.C.D' and 'A.B.C.D/M' will _not_ be returned.
858 *
859 * @param vline vectorized input line
860 * @param vty the vty
861 * @param status location to store matcher status code in
862 * @return set of valid strings for use with Readline as tab-completions.
863 */
864
865 char **cmd_complete_command(vector vline, struct vty *vty, int *status)
866 {
867 char **ret = NULL;
868 int original_node = vty->node;
869 vector input_line = vector_init(vector_count(vline));
870
871 // if the first token is 'do' we'll want to execute the command in the
872 // enable node
873 int do_shortcut = cmd_try_do_shortcut(vty->node, vector_slot(vline, 0));
874 vty->node = do_shortcut ? ENABLE_NODE : original_node;
875
876 // construct the input line we'll be matching on
877 unsigned int offset = (do_shortcut) ? 1 : 0;
878 for (unsigned index = 0; index + offset < vector_active(vline); index++)
879 vector_set_index(input_line, index,
880 vector_lookup(vline, index + offset));
881
882 // get token completions -- this is a copying operation
883 vector comps = NULL, initial_comps;
884 initial_comps = cmd_complete_command_real(input_line, vty, status);
885
886 if (!MATCHER_ERROR(*status)) {
887 assert(initial_comps);
888 // filter out everything that is not suitable for a
889 // tab-completion
890 comps = vector_init(VECTOR_MIN_SIZE);
891 for (unsigned int i = 0; i < vector_active(initial_comps);
892 i++) {
893 struct cmd_token *token = vector_slot(initial_comps, i);
894 if (token->type == WORD_TKN)
895 vector_set(comps, XSTRDUP(MTYPE_COMPLETION,
896 token->text));
897 else if (IS_VARYING_TOKEN(token->type)) {
898 const char *ref = vector_lookup(
899 vline, vector_active(vline) - 1);
900 cmd_variable_complete(token, ref, comps);
901 }
902 }
903 vector_free(initial_comps);
904
905 // since we filtered results, we need to re-set status code
906 switch (vector_active(comps)) {
907 case 0:
908 *status = CMD_ERR_NO_MATCH;
909 break;
910 case 1:
911 *status = CMD_COMPLETE_FULL_MATCH;
912 break;
913 default:
914 *status = CMD_COMPLETE_LIST_MATCH;
915 }
916
917 // copy completions text into an array of char*
918 ret = XMALLOC(MTYPE_TMP,
919 (vector_active(comps) + 1) * sizeof(char *));
920 unsigned int i;
921 for (i = 0; i < vector_active(comps); i++) {
922 ret[i] = vector_slot(comps, i);
923 }
924 // set the last element to NULL, because this array is used in
925 // a Readline completion_generator function which expects NULL
926 // as a sentinel value
927 ret[i] = NULL;
928 vector_free(comps);
929 comps = NULL;
930 } else if (initial_comps)
931 vector_free(initial_comps);
932
933 // comps should always be null here
934 assert(!comps);
935
936 // free the adjusted input line
937 vector_free(input_line);
938
939 // reset vty->node to its original value
940 vty->node = original_node;
941
942 return ret;
943 }
944
945 /* return parent node */
946 /* MUST eventually converge on CONFIG_NODE */
947 enum node_type node_parent(enum node_type node)
948 {
949 enum node_type ret;
950
951 assert(node > CONFIG_NODE);
952
953 switch (node) {
954 case BGP_VPNV4_NODE:
955 case BGP_VPNV6_NODE:
956 case BGP_FLOWSPECV4_NODE:
957 case BGP_FLOWSPECV6_NODE:
958 case BGP_VRF_POLICY_NODE:
959 case BGP_VNC_DEFAULTS_NODE:
960 case BGP_VNC_NVE_GROUP_NODE:
961 case BGP_VNC_L2_GROUP_NODE:
962 case BGP_IPV4_NODE:
963 case BGP_IPV4M_NODE:
964 case BGP_IPV4L_NODE:
965 case BGP_IPV6_NODE:
966 case BGP_IPV6M_NODE:
967 case BGP_EVPN_NODE:
968 case BGP_IPV6L_NODE:
969 ret = BGP_NODE;
970 break;
971 case BGP_EVPN_VNI_NODE:
972 ret = BGP_EVPN_NODE;
973 break;
974 case KEYCHAIN_KEY_NODE:
975 ret = KEYCHAIN_NODE;
976 break;
977 case LINK_PARAMS_NODE:
978 ret = INTERFACE_NODE;
979 break;
980 case LDP_IPV4_NODE:
981 case LDP_IPV6_NODE:
982 ret = LDP_NODE;
983 break;
984 case LDP_IPV4_IFACE_NODE:
985 ret = LDP_IPV4_NODE;
986 break;
987 case LDP_IPV6_IFACE_NODE:
988 ret = LDP_IPV6_NODE;
989 break;
990 case LDP_PSEUDOWIRE_NODE:
991 ret = LDP_L2VPN_NODE;
992 break;
993 case BFD_PEER_NODE:
994 ret = BFD_NODE;
995 break;
996 default:
997 ret = CONFIG_NODE;
998 break;
999 }
1000
1001 return ret;
1002 }
1003
1004 /* Execute command by argument vline vector. */
1005 static int cmd_execute_command_real(vector vline, enum filter_type filter,
1006 struct vty *vty,
1007 const struct cmd_element **cmd)
1008 {
1009 struct list *argv_list;
1010 enum matcher_rv status;
1011 const struct cmd_element *matched_element = NULL;
1012
1013 struct graph *cmdgraph = cmd_node_graph(cmdvec, vty->node);
1014 status = command_match(cmdgraph, vline, &argv_list, &matched_element);
1015
1016 if (cmd)
1017 *cmd = matched_element;
1018
1019 // if matcher error, return corresponding CMD_ERR
1020 if (MATCHER_ERROR(status)) {
1021 if (argv_list)
1022 list_delete_and_null(&argv_list);
1023 switch (status) {
1024 case MATCHER_INCOMPLETE:
1025 return CMD_ERR_INCOMPLETE;
1026 case MATCHER_AMBIGUOUS:
1027 return CMD_ERR_AMBIGUOUS;
1028 default:
1029 return CMD_ERR_NO_MATCH;
1030 }
1031 }
1032
1033 // build argv array from argv list
1034 struct cmd_token **argv = XMALLOC(
1035 MTYPE_TMP, argv_list->count * sizeof(struct cmd_token *));
1036 struct listnode *ln;
1037 struct cmd_token *token;
1038 unsigned int i = 0;
1039 for (ALL_LIST_ELEMENTS_RO(argv_list, ln, token))
1040 argv[i++] = token;
1041
1042 int argc = argv_list->count;
1043
1044 int ret;
1045 if (matched_element->daemon)
1046 ret = CMD_SUCCESS_DAEMON;
1047 else
1048 ret = matched_element->func(matched_element, vty, argc, argv);
1049
1050 // delete list and cmd_token's in it
1051 list_delete_and_null(&argv_list);
1052 XFREE(MTYPE_TMP, argv);
1053
1054 return ret;
1055 }
1056
1057 /**
1058 * Execute a given command, handling things like "do ..." and checking
1059 * whether the given command might apply at a parent node if doesn't
1060 * apply for the current node.
1061 *
1062 * @param vline Command line input, vector of char* where each element is
1063 * one input token.
1064 * @param vty The vty context in which the command should be executed.
1065 * @param cmd Pointer where the struct cmd_element of the matched command
1066 * will be stored, if any. May be set to NULL if this info is
1067 * not needed.
1068 * @param vtysh If set != 0, don't lookup the command at parent nodes.
1069 * @return The status of the command that has been executed or an error code
1070 * as to why no command could be executed.
1071 */
1072 int cmd_execute_command(vector vline, struct vty *vty,
1073 const struct cmd_element **cmd, int vtysh)
1074 {
1075 int ret, saved_ret = 0;
1076 enum node_type onode, try_node;
1077
1078 onode = try_node = vty->node;
1079
1080 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1081 vector shifted_vline;
1082 unsigned int index;
1083
1084 vty->node = ENABLE_NODE;
1085 /* We can try it on enable node, cos' the vty is authenticated
1086 */
1087
1088 shifted_vline = vector_init(vector_count(vline));
1089 /* use memcpy? */
1090 for (index = 1; index < vector_active(vline); index++)
1091 vector_set_index(shifted_vline, index - 1,
1092 vector_lookup(vline, index));
1093
1094 ret = cmd_execute_command_real(shifted_vline, FILTER_RELAXED,
1095 vty, cmd);
1096
1097 vector_free(shifted_vline);
1098 vty->node = onode;
1099 return ret;
1100 }
1101
1102 saved_ret = ret =
1103 cmd_execute_command_real(vline, FILTER_RELAXED, vty, cmd);
1104
1105 if (vtysh)
1106 return saved_ret;
1107
1108 if (ret != CMD_SUCCESS && ret != CMD_WARNING
1109 && ret != CMD_NOT_MY_INSTANCE && ret != CMD_WARNING_CONFIG_FAILED) {
1110 /* This assumes all nodes above CONFIG_NODE are childs of
1111 * CONFIG_NODE */
1112 while (vty->node > CONFIG_NODE) {
1113 try_node = node_parent(try_node);
1114 vty->node = try_node;
1115 ret = cmd_execute_command_real(vline, FILTER_RELAXED,
1116 vty, cmd);
1117 if (ret == CMD_SUCCESS || ret == CMD_WARNING
1118 || ret == CMD_NOT_MY_INSTANCE
1119 || ret == CMD_WARNING_CONFIG_FAILED)
1120 return ret;
1121 }
1122 /* no command succeeded, reset the vty to the original node */
1123 vty->node = onode;
1124 }
1125
1126 /* return command status for original node */
1127 return saved_ret;
1128 }
1129
1130 /**
1131 * Execute a given command, matching it strictly against the current node.
1132 * This mode is used when reading config files.
1133 *
1134 * @param vline Command line input, vector of char* where each element is
1135 * one input token.
1136 * @param vty The vty context in which the command should be executed.
1137 * @param cmd Pointer where the struct cmd_element* of the matched command
1138 * will be stored, if any. May be set to NULL if this info is
1139 * not needed.
1140 * @return The status of the command that has been executed or an error code
1141 * as to why no command could be executed.
1142 */
1143 int cmd_execute_command_strict(vector vline, struct vty *vty,
1144 const struct cmd_element **cmd)
1145 {
1146 return cmd_execute_command_real(vline, FILTER_STRICT, vty, cmd);
1147 }
1148
1149 /*
1150 * Hook for preprocessing command string before executing.
1151 *
1152 * All subscribers are called with the raw command string that is to be
1153 * executed. If any changes are to be made, a new string should be allocated
1154 * with MTYPE_TMP and *cmd_out updated to point to this new string. The caller
1155 * is then responsible for freeing this string.
1156 *
1157 * All processing functions must be mutually exclusive in their action, i.e. if
1158 * one subscriber decides to modify the command, all others must not modify it
1159 * when called. Feeding the output of one processing command into a subsequent
1160 * one is not supported.
1161 *
1162 * This hook is intentionally internal to the command processing system.
1163 *
1164 * cmd_in
1165 * The raw command string.
1166 *
1167 * cmd_out
1168 * The result of any processing.
1169 */
1170 DECLARE_HOOK(cmd_execute,
1171 (struct vty *vty, const char *cmd_in, char **cmd_out),
1172 (vty, cmd_in, cmd_out));
1173 DEFINE_HOOK(cmd_execute, (struct vty *vty, const char *cmd_in, char **cmd_out),
1174 (vty, cmd_in, cmd_out));
1175
1176 /* Hook executed after a CLI command. */
1177 DECLARE_KOOH(cmd_execute_done, (struct vty *vty, const char *cmd_exec),
1178 (vty, cmd_exec));
1179 DEFINE_KOOH(cmd_execute_done, (struct vty *vty, const char *cmd_exec),
1180 (vty, cmd_exec));
1181
1182 /*
1183 * cmd_execute hook subscriber to handle `|` actions.
1184 */
1185 static int handle_pipe_action(struct vty *vty, const char *cmd_in,
1186 char **cmd_out)
1187 {
1188 /* look for `|` */
1189 char *orig, *working, *token, *u;
1190 char *pipe = strstr(cmd_in, "| ");
1191
1192 if (!pipe)
1193 return 0;
1194
1195 /* duplicate string for processing purposes, not including pipe */
1196 orig = working = XSTRDUP(MTYPE_TMP, pipe + 2);
1197
1198 /* retrieve action */
1199 token = strsep(&working, " ");
1200
1201 /* match result to known actions */
1202 if (strmatch(token, "include")) {
1203 /* the remaining text should be a regexp */
1204 char *regexp = working;
1205
1206 if (!regexp) {
1207 vty_out(vty, "%% Need a regexp to filter with\n");
1208 goto fail;
1209 }
1210
1211 bool succ = vty_set_include(vty, regexp);
1212
1213 if (!succ) {
1214 vty_out(vty, "%% Bad regexp '%s'\n", regexp);
1215 goto fail;
1216 }
1217 *cmd_out = XSTRDUP(MTYPE_TMP, cmd_in);
1218 u = *cmd_out;
1219 strsep(&u, "|");
1220 } else {
1221 vty_out(vty, "%% Unknown action '%s'\n", token);
1222 goto fail;
1223 }
1224
1225 fail:
1226 XFREE(MTYPE_TMP, orig);
1227 return 0;
1228 }
1229
1230 static int handle_pipe_action_done(struct vty *vty, const char *cmd_exec)
1231 {
1232 if (vty->filter)
1233 vty_set_include(vty, NULL);
1234
1235 return 0;
1236 }
1237
1238 int cmd_execute(struct vty *vty, const char *cmd,
1239 const struct cmd_element **matched, int vtysh)
1240 {
1241 int ret;
1242 char *cmd_out = NULL;
1243 const char *cmd_exec;
1244 vector vline;
1245
1246 hook_call(cmd_execute, vty, cmd, &cmd_out);
1247 cmd_exec = cmd_out ? (const char *)cmd_out : cmd;
1248
1249 vline = cmd_make_strvec(cmd_exec);
1250
1251 if (vline) {
1252 ret = cmd_execute_command(vline, vty, matched, vtysh);
1253 cmd_free_strvec(vline);
1254 } else {
1255 ret = CMD_SUCCESS;
1256 }
1257
1258 hook_call(cmd_execute_done, vty, cmd_exec);
1259
1260 if (cmd_out)
1261 XFREE(MTYPE_TMP, cmd_out);
1262
1263 return ret;
1264 }
1265
1266
1267 /**
1268 * Parse one line of config, walking up the parse tree attempting to find a
1269 * match
1270 *
1271 * @param vty The vty context in which the command should be executed.
1272 * @param cmd Pointer where the struct cmd_element* of the match command
1273 * will be stored, if any. May be set to NULL if this info is
1274 * not needed.
1275 * @param use_daemon Boolean to control whether or not we match on
1276 * CMD_SUCCESS_DAEMON
1277 * or not.
1278 * @return The status of the command that has been executed or an error code
1279 * as to why no command could be executed.
1280 */
1281 int command_config_read_one_line(struct vty *vty,
1282 const struct cmd_element **cmd, int use_daemon)
1283 {
1284 vector vline;
1285 int saved_node;
1286 int ret;
1287
1288 vline = cmd_make_strvec(vty->buf);
1289
1290 /* In case of comment line */
1291 if (vline == NULL)
1292 return CMD_SUCCESS;
1293
1294 /* Execute configuration command : this is strict match */
1295 ret = cmd_execute_command_strict(vline, vty, cmd);
1296
1297 // Climb the tree and try the command again at each node
1298 if (!(use_daemon && ret == CMD_SUCCESS_DAEMON)
1299 && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO)
1300 && ret != CMD_SUCCESS && ret != CMD_WARNING
1301 && ret != CMD_NOT_MY_INSTANCE && ret != CMD_WARNING_CONFIG_FAILED
1302 && vty->node != CONFIG_NODE) {
1303
1304 saved_node = vty->node;
1305
1306 while (!(use_daemon && ret == CMD_SUCCESS_DAEMON)
1307 && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO)
1308 && ret != CMD_SUCCESS && ret != CMD_WARNING
1309 && vty->node > CONFIG_NODE) {
1310 vty->node = node_parent(vty->node);
1311 ret = cmd_execute_command_strict(vline, vty, cmd);
1312 }
1313
1314 // If climbing the tree did not work then ignore the command and
1315 // stay at the same node
1316 if (!(use_daemon && ret == CMD_SUCCESS_DAEMON)
1317 && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO)
1318 && ret != CMD_SUCCESS && ret != CMD_WARNING) {
1319 vty->node = saved_node;
1320 }
1321 }
1322
1323 if (ret != CMD_SUCCESS && ret != CMD_WARNING)
1324 memcpy(vty->error_buf, vty->buf, VTY_BUFSIZ);
1325
1326 cmd_free_strvec(vline);
1327
1328 return ret;
1329 }
1330
1331 /* Configuration make from file. */
1332 int config_from_file(struct vty *vty, FILE *fp, unsigned int *line_num)
1333 {
1334 int ret, error_ret = 0;
1335 *line_num = 0;
1336
1337 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
1338 if (!error_ret)
1339 ++(*line_num);
1340
1341 ret = command_config_read_one_line(vty, NULL, 0);
1342
1343 if (ret != CMD_SUCCESS && ret != CMD_WARNING
1344 && ret != CMD_ERR_NOTHING_TODO)
1345 error_ret = ret;
1346 }
1347
1348 if (error_ret) {
1349 return error_ret;
1350 }
1351
1352 return CMD_SUCCESS;
1353 }
1354
1355 /* Configuration from terminal */
1356 DEFUN (config_terminal,
1357 config_terminal_cmd,
1358 "configure terminal",
1359 "Configuration from vty interface\n"
1360 "Configuration terminal\n")
1361 {
1362 if (vty_config_lock(vty))
1363 vty->node = CONFIG_NODE;
1364 else {
1365 vty_out(vty, "VTY configuration is locked by other VTY\n");
1366 return CMD_WARNING_CONFIG_FAILED;
1367 }
1368 return CMD_SUCCESS;
1369 }
1370
1371 /* Enable command */
1372 DEFUN (enable,
1373 config_enable_cmd,
1374 "enable",
1375 "Turn on privileged mode command\n")
1376 {
1377 /* If enable password is NULL, change to ENABLE_NODE */
1378 if ((host.enable == NULL && host.enable_encrypt == NULL)
1379 || vty->type == VTY_SHELL_SERV)
1380 vty->node = ENABLE_NODE;
1381 else
1382 vty->node = AUTH_ENABLE_NODE;
1383
1384 return CMD_SUCCESS;
1385 }
1386
1387 /* Disable command */
1388 DEFUN (disable,
1389 config_disable_cmd,
1390 "disable",
1391 "Turn off privileged mode command\n")
1392 {
1393 if (vty->node == ENABLE_NODE)
1394 vty->node = VIEW_NODE;
1395 return CMD_SUCCESS;
1396 }
1397
1398 /* Down vty node level. */
1399 DEFUN (config_exit,
1400 config_exit_cmd,
1401 "exit",
1402 "Exit current mode and down to previous mode\n")
1403 {
1404 cmd_exit(vty);
1405 return CMD_SUCCESS;
1406 }
1407
1408 void cmd_exit(struct vty *vty)
1409 {
1410 switch (vty->node) {
1411 case VIEW_NODE:
1412 case ENABLE_NODE:
1413 if (vty_shell(vty))
1414 exit(0);
1415 else
1416 vty->status = VTY_CLOSE;
1417 break;
1418 case CONFIG_NODE:
1419 vty->node = ENABLE_NODE;
1420 vty_config_unlock(vty);
1421 break;
1422 case INTERFACE_NODE:
1423 case PW_NODE:
1424 case LOGICALROUTER_NODE:
1425 case VRF_NODE:
1426 case NH_GROUP_NODE:
1427 case ZEBRA_NODE:
1428 case BGP_NODE:
1429 case RIP_NODE:
1430 case EIGRP_NODE:
1431 case BABEL_NODE:
1432 case RIPNG_NODE:
1433 case OSPF_NODE:
1434 case OSPF6_NODE:
1435 case LDP_NODE:
1436 case LDP_L2VPN_NODE:
1437 case ISIS_NODE:
1438 case KEYCHAIN_NODE:
1439 case RMAP_NODE:
1440 case PBRMAP_NODE:
1441 case VTY_NODE:
1442 case BFD_NODE:
1443 vty->node = CONFIG_NODE;
1444 break;
1445 case BGP_IPV4_NODE:
1446 case BGP_IPV4M_NODE:
1447 case BGP_IPV4L_NODE:
1448 case BGP_VPNV4_NODE:
1449 case BGP_VPNV6_NODE:
1450 case BGP_FLOWSPECV4_NODE:
1451 case BGP_FLOWSPECV6_NODE:
1452 case BGP_VRF_POLICY_NODE:
1453 case BGP_VNC_DEFAULTS_NODE:
1454 case BGP_VNC_NVE_GROUP_NODE:
1455 case BGP_VNC_L2_GROUP_NODE:
1456 case BGP_IPV6_NODE:
1457 case BGP_IPV6M_NODE:
1458 case BGP_EVPN_NODE:
1459 case BGP_IPV6L_NODE:
1460 vty->node = BGP_NODE;
1461 break;
1462 case BGP_EVPN_VNI_NODE:
1463 vty->node = BGP_EVPN_NODE;
1464 break;
1465 case LDP_IPV4_NODE:
1466 case LDP_IPV6_NODE:
1467 vty->node = LDP_NODE;
1468 break;
1469 case LDP_IPV4_IFACE_NODE:
1470 vty->node = LDP_IPV4_NODE;
1471 break;
1472 case LDP_IPV6_IFACE_NODE:
1473 vty->node = LDP_IPV6_NODE;
1474 break;
1475 case LDP_PSEUDOWIRE_NODE:
1476 vty->node = LDP_L2VPN_NODE;
1477 break;
1478 case KEYCHAIN_KEY_NODE:
1479 vty->node = KEYCHAIN_NODE;
1480 break;
1481 case LINK_PARAMS_NODE:
1482 vty->node = INTERFACE_NODE;
1483 break;
1484 case BFD_PEER_NODE:
1485 vty->node = BFD_NODE;
1486 break;
1487 default:
1488 break;
1489 }
1490 }
1491
1492 /* ALIAS_FIXME */
1493 DEFUN (config_quit,
1494 config_quit_cmd,
1495 "quit",
1496 "Exit current mode and down to previous mode\n")
1497 {
1498 return config_exit(self, vty, argc, argv);
1499 }
1500
1501
1502 /* End of configuration. */
1503 DEFUN (config_end,
1504 config_end_cmd,
1505 "end",
1506 "End current mode and change to enable mode.\n")
1507 {
1508 switch (vty->node) {
1509 case VIEW_NODE:
1510 case ENABLE_NODE:
1511 /* Nothing to do. */
1512 break;
1513 case CONFIG_NODE:
1514 case INTERFACE_NODE:
1515 case PW_NODE:
1516 case LOGICALROUTER_NODE:
1517 case VRF_NODE:
1518 case NH_GROUP_NODE:
1519 case ZEBRA_NODE:
1520 case RIP_NODE:
1521 case RIPNG_NODE:
1522 case EIGRP_NODE:
1523 case BABEL_NODE:
1524 case BGP_NODE:
1525 case BGP_VRF_POLICY_NODE:
1526 case BGP_VNC_DEFAULTS_NODE:
1527 case BGP_VNC_NVE_GROUP_NODE:
1528 case BGP_VNC_L2_GROUP_NODE:
1529 case BGP_VPNV4_NODE:
1530 case BGP_VPNV6_NODE:
1531 case BGP_FLOWSPECV4_NODE:
1532 case BGP_FLOWSPECV6_NODE:
1533 case BGP_IPV4_NODE:
1534 case BGP_IPV4M_NODE:
1535 case BGP_IPV4L_NODE:
1536 case BGP_IPV6_NODE:
1537 case BGP_IPV6M_NODE:
1538 case BGP_EVPN_NODE:
1539 case BGP_EVPN_VNI_NODE:
1540 case BGP_IPV6L_NODE:
1541 case RMAP_NODE:
1542 case PBRMAP_NODE:
1543 case OSPF_NODE:
1544 case OSPF6_NODE:
1545 case LDP_NODE:
1546 case LDP_IPV4_NODE:
1547 case LDP_IPV6_NODE:
1548 case LDP_IPV4_IFACE_NODE:
1549 case LDP_IPV6_IFACE_NODE:
1550 case LDP_L2VPN_NODE:
1551 case LDP_PSEUDOWIRE_NODE:
1552 case ISIS_NODE:
1553 case KEYCHAIN_NODE:
1554 case KEYCHAIN_KEY_NODE:
1555 case VTY_NODE:
1556 case LINK_PARAMS_NODE:
1557 case BFD_NODE:
1558 case BFD_PEER_NODE:
1559 vty_config_unlock(vty);
1560 vty->node = ENABLE_NODE;
1561 break;
1562 default:
1563 break;
1564 }
1565 return CMD_SUCCESS;
1566 }
1567
1568 /* Show version. */
1569 DEFUN (show_version,
1570 show_version_cmd,
1571 "show version",
1572 SHOW_STR
1573 "Displays zebra version\n")
1574 {
1575 vty_out(vty, "%s %s (%s).\n", FRR_FULL_NAME, FRR_VERSION,
1576 cmd_hostname_get() ? cmd_hostname_get() : "");
1577 vty_out(vty, "%s%s\n", FRR_COPYRIGHT, GIT_INFO);
1578 vty_out(vty, "configured with:\n %s\n", FRR_CONFIG_ARGS);
1579
1580 return CMD_SUCCESS;
1581 }
1582
1583 /* "Set" version ... ignore version tags */
1584 DEFUN (frr_version_defaults,
1585 frr_version_defaults_cmd,
1586 "frr <version|defaults> LINE...",
1587 "FRRouting global parameters\n"
1588 "version configuration was written by\n"
1589 "set of configuration defaults used\n"
1590 "version string\n")
1591 {
1592 return CMD_SUCCESS;
1593 }
1594
1595 /* Help display function for all node. */
1596 DEFUN (config_help,
1597 config_help_cmd,
1598 "help",
1599 "Description of the interactive help system\n")
1600 {
1601 vty_out(vty,
1602 "Quagga VTY provides advanced help feature. When you need help,\n\
1603 anytime at the command line please press '?'.\n\
1604 \n\
1605 If nothing matches, the help list will be empty and you must backup\n\
1606 until entering a '?' shows the available options.\n\
1607 Two styles of help are provided:\n\
1608 1. Full help is available when you are ready to enter a\n\
1609 command argument (e.g. 'show ?') and describes each possible\n\
1610 argument.\n\
1611 2. Partial help is provided when an abbreviated argument is entered\n\
1612 and you want to know what arguments match the input\n\
1613 (e.g. 'show me?'.)\n\n");
1614 return CMD_SUCCESS;
1615 }
1616
1617 static void permute(struct graph_node *start, struct vty *vty)
1618 {
1619 static struct list *position = NULL;
1620 if (!position)
1621 position = list_new();
1622
1623 struct cmd_token *stok = start->data;
1624 struct graph_node *gnn;
1625 struct listnode *ln;
1626
1627 // recursive dfs
1628 listnode_add(position, start);
1629 for (unsigned int i = 0; i < vector_active(start->to); i++) {
1630 struct graph_node *gn = vector_slot(start->to, i);
1631 struct cmd_token *tok = gn->data;
1632 if (tok->attr == CMD_ATTR_HIDDEN
1633 || tok->attr == CMD_ATTR_DEPRECATED)
1634 continue;
1635 else if (tok->type == END_TKN || gn == start) {
1636 vty_out(vty, " ");
1637 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) {
1638 struct cmd_token *tt = gnn->data;
1639 if (tt->type < SPECIAL_TKN)
1640 vty_out(vty, " %s", tt->text);
1641 }
1642 if (gn == start)
1643 vty_out(vty, "...");
1644 vty_out(vty, "\n");
1645 } else {
1646 bool skip = false;
1647 if (stok->type == FORK_TKN && tok->type != FORK_TKN)
1648 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn))
1649 if (gnn == gn) {
1650 skip = true;
1651 break;
1652 }
1653 if (!skip)
1654 permute(gn, vty);
1655 }
1656 }
1657 list_delete_node(position, listtail(position));
1658 }
1659
1660 int cmd_list_cmds(struct vty *vty, int do_permute)
1661 {
1662 struct cmd_node *node = vector_slot(cmdvec, vty->node);
1663
1664 if (do_permute)
1665 permute(vector_slot(node->cmdgraph->nodes, 0), vty);
1666 else {
1667 /* loop over all commands at this node */
1668 struct cmd_element *element = NULL;
1669 for (unsigned int i = 0; i < vector_active(node->cmd_vector);
1670 i++)
1671 if ((element = vector_slot(node->cmd_vector, i))
1672 && element->attr != CMD_ATTR_DEPRECATED
1673 && element->attr != CMD_ATTR_HIDDEN)
1674 vty_out(vty, " %s\n", element->string);
1675 }
1676 return CMD_SUCCESS;
1677 }
1678
1679 /* Help display function for all node. */
1680 DEFUN (config_list,
1681 config_list_cmd,
1682 "list [permutations]",
1683 "Print command list\n"
1684 "Print all possible command permutations\n")
1685 {
1686 return cmd_list_cmds(vty, argc == 2);
1687 }
1688
1689 DEFUN (show_commandtree,
1690 show_commandtree_cmd,
1691 "show commandtree [permutations]",
1692 SHOW_STR
1693 "Show command tree\n"
1694 "Permutations that we are interested in\n")
1695 {
1696 return cmd_list_cmds(vty, argc == 3);
1697 }
1698
1699 DEFUN_HIDDEN(show_cli_graph,
1700 show_cli_graph_cmd,
1701 "show cli graph",
1702 SHOW_STR
1703 "CLI reflection\n"
1704 "Dump current command space as DOT graph\n")
1705 {
1706 struct cmd_node *cn = vector_slot(cmdvec, vty->node);
1707 char *dot = cmd_graph_dump_dot(cn->cmdgraph);
1708
1709 vty_out(vty, "%s\n", dot);
1710 XFREE(MTYPE_TMP, dot);
1711 return CMD_SUCCESS;
1712 }
1713
1714 static int vty_write_config(struct vty *vty)
1715 {
1716 size_t i;
1717 struct cmd_node *node;
1718
1719 if (host.noconfig)
1720 return CMD_SUCCESS;
1721
1722 if (vty->type == VTY_TERM) {
1723 vty_out(vty, "\nCurrent configuration:\n");
1724 vty_out(vty, "!\n");
1725 }
1726
1727 vty_out(vty, "frr version %s\n", FRR_VER_SHORT);
1728 vty_out(vty, "frr defaults %s\n", DFLT_NAME);
1729 vty_out(vty, "!\n");
1730
1731 for (i = 0; i < vector_active(cmdvec); i++)
1732 if ((node = vector_slot(cmdvec, i)) && node->func
1733 && (node->vtysh || vty->type != VTY_SHELL)) {
1734 if ((*node->func)(vty))
1735 vty_out(vty, "!\n");
1736 }
1737
1738 if (vty->type == VTY_TERM) {
1739 vty_out(vty, "end\n");
1740 }
1741
1742 return CMD_SUCCESS;
1743 }
1744
1745 static int file_write_config(struct vty *vty)
1746 {
1747 int fd, dirfd;
1748 char *config_file, *slash;
1749 char *config_file_tmp = NULL;
1750 char *config_file_sav = NULL;
1751 int ret = CMD_WARNING;
1752 struct vty *file_vty;
1753 struct stat conf_stat;
1754
1755 if (host.noconfig)
1756 return CMD_SUCCESS;
1757
1758 /* Check and see if we are operating under vtysh configuration */
1759 if (host.config == NULL) {
1760 vty_out(vty,
1761 "Can't save to configuration file, using vtysh.\n");
1762 return CMD_WARNING;
1763 }
1764
1765 /* Get filename. */
1766 config_file = host.config;
1767
1768 #ifndef O_DIRECTORY
1769 #define O_DIRECTORY 0
1770 #endif
1771 slash = strrchr(config_file, '/');
1772 if (slash) {
1773 char *config_dir = XSTRDUP(MTYPE_TMP, config_file);
1774 config_dir[slash - config_file] = '\0';
1775 dirfd = open(config_dir, O_DIRECTORY | O_RDONLY);
1776 XFREE(MTYPE_TMP, config_dir);
1777 } else
1778 dirfd = open(".", O_DIRECTORY | O_RDONLY);
1779 /* if dirfd is invalid, directory sync fails, but we're still OK */
1780
1781 config_file_sav = XMALLOC(
1782 MTYPE_TMP, strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1);
1783 strcpy(config_file_sav, config_file);
1784 strcat(config_file_sav, CONF_BACKUP_EXT);
1785
1786
1787 config_file_tmp = XMALLOC(MTYPE_TMP, strlen(config_file) + 8);
1788 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
1789
1790 /* Open file to configuration write. */
1791 fd = mkstemp(config_file_tmp);
1792 if (fd < 0) {
1793 vty_out(vty, "Can't open configuration file %s.\n",
1794 config_file_tmp);
1795 goto finished;
1796 }
1797 if (fchmod(fd, CONFIGFILE_MASK) != 0) {
1798 vty_out(vty, "Can't chmod configuration file %s: %s (%d).\n",
1799 config_file_tmp, safe_strerror(errno), errno);
1800 goto finished;
1801 }
1802
1803 /* Make vty for configuration file. */
1804 file_vty = vty_new();
1805 file_vty->wfd = fd;
1806 file_vty->type = VTY_FILE;
1807
1808 /* Config file header print. */
1809 vty_out(file_vty, "!\n! Zebra configuration saved from vty\n! ");
1810 vty_time_print(file_vty, 1);
1811 vty_out(file_vty, "!\n");
1812 vty_write_config(file_vty);
1813 vty_close(file_vty);
1814
1815 if (stat(config_file, &conf_stat) >= 0) {
1816 if (unlink(config_file_sav) != 0)
1817 if (errno != ENOENT) {
1818 vty_out(vty,
1819 "Can't unlink backup configuration file %s.\n",
1820 config_file_sav);
1821 goto finished;
1822 }
1823 if (link(config_file, config_file_sav) != 0) {
1824 vty_out(vty,
1825 "Can't backup old configuration file %s.\n",
1826 config_file_sav);
1827 goto finished;
1828 }
1829 if (dirfd >= 0)
1830 fsync(dirfd);
1831 }
1832 if (rename(config_file_tmp, config_file) != 0) {
1833 vty_out(vty, "Can't save configuration file %s.\n",
1834 config_file);
1835 goto finished;
1836 }
1837 if (dirfd >= 0)
1838 fsync(dirfd);
1839
1840 vty_out(vty, "Configuration saved to %s\n", config_file);
1841 ret = CMD_SUCCESS;
1842
1843 finished:
1844 if (ret != CMD_SUCCESS)
1845 unlink(config_file_tmp);
1846 if (dirfd >= 0)
1847 close(dirfd);
1848 XFREE(MTYPE_TMP, config_file_tmp);
1849 XFREE(MTYPE_TMP, config_file_sav);
1850 return ret;
1851 }
1852
1853 /* Write current configuration into file. */
1854
1855 DEFUN (config_write,
1856 config_write_cmd,
1857 "write [<file|memory|terminal>]",
1858 "Write running configuration to memory, network, or terminal\n"
1859 "Write to configuration file\n"
1860 "Write configuration currently in memory\n"
1861 "Write configuration to terminal\n")
1862 {
1863 const int idx_type = 1;
1864
1865 // if command was 'write terminal' or 'write memory'
1866 if (argc == 2 && (!strcmp(argv[idx_type]->text, "terminal"))) {
1867 return vty_write_config(vty);
1868 }
1869
1870 return file_write_config(vty);
1871 }
1872
1873 /* ALIAS_FIXME for 'write <terminal|memory>' */
1874 DEFUN (show_running_config,
1875 show_running_config_cmd,
1876 "show running-config",
1877 SHOW_STR
1878 "running configuration (same as write terminal)\n")
1879 {
1880 return vty_write_config(vty);
1881 }
1882
1883 /* ALIAS_FIXME for 'write file' */
1884 DEFUN (copy_runningconf_startupconf,
1885 copy_runningconf_startupconf_cmd,
1886 "copy running-config startup-config",
1887 "Copy configuration\n"
1888 "Copy running config to... \n"
1889 "Copy running config to startup config (same as write file/memory)\n")
1890 {
1891 return file_write_config(vty);
1892 }
1893 /** -- **/
1894
1895 /* Write startup configuration into the terminal. */
1896 DEFUN (show_startup_config,
1897 show_startup_config_cmd,
1898 "show startup-config",
1899 SHOW_STR
1900 "Contents of startup configuration\n")
1901 {
1902 char buf[BUFSIZ];
1903 FILE *confp;
1904
1905 if (host.noconfig)
1906 return CMD_SUCCESS;
1907 if (host.config == NULL)
1908 return CMD_WARNING;
1909
1910 confp = fopen(host.config, "r");
1911 if (confp == NULL) {
1912 vty_out(vty, "Can't open configuration file [%s] due to '%s'\n",
1913 host.config, safe_strerror(errno));
1914 return CMD_WARNING;
1915 }
1916
1917 while (fgets(buf, BUFSIZ, confp)) {
1918 char *cp = buf;
1919
1920 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
1921 cp++;
1922 *cp = '\0';
1923
1924 vty_out(vty, "%s\n", buf);
1925 }
1926
1927 fclose(confp);
1928
1929 return CMD_SUCCESS;
1930 }
1931
1932 int cmd_domainname_set(const char *domainname)
1933 {
1934 XFREE(MTYPE_HOST, host.domainname);
1935 host.domainname = domainname ? XSTRDUP(MTYPE_HOST, domainname) : NULL;
1936 return CMD_SUCCESS;
1937 }
1938
1939 /* Hostname configuration */
1940 DEFUN(config_domainname,
1941 domainname_cmd,
1942 "domainname WORD",
1943 "Set system's domain name\n"
1944 "This system's domain name\n")
1945 {
1946 struct cmd_token *word = argv[1];
1947
1948 if (!isalpha((int)word->arg[0])) {
1949 vty_out(vty, "Please specify string starting with alphabet\n");
1950 return CMD_WARNING_CONFIG_FAILED;
1951 }
1952
1953 return cmd_domainname_set(word->arg);
1954 }
1955
1956 DEFUN(config_no_domainname,
1957 no_domainname_cmd,
1958 "no domainname [DOMAINNAME]",
1959 NO_STR
1960 "Reset system's domain name\n"
1961 "domain name of this router\n")
1962 {
1963 return cmd_domainname_set(NULL);
1964 }
1965
1966 int cmd_hostname_set(const char *hostname)
1967 {
1968 XFREE(MTYPE_HOST, host.name);
1969 host.name = hostname ? XSTRDUP(MTYPE_HOST, hostname) : NULL;
1970 return CMD_SUCCESS;
1971 }
1972
1973 /* Hostname configuration */
1974 DEFUN (config_hostname,
1975 hostname_cmd,
1976 "hostname WORD",
1977 "Set system's network name\n"
1978 "This system's network name\n")
1979 {
1980 struct cmd_token *word = argv[1];
1981
1982 if (!isalnum((int)word->arg[0])) {
1983 vty_out(vty, "Please specify string starting with alphabet\n");
1984 return CMD_WARNING_CONFIG_FAILED;
1985 }
1986
1987 return cmd_hostname_set(word->arg);
1988 }
1989
1990 DEFUN (config_no_hostname,
1991 no_hostname_cmd,
1992 "no hostname [HOSTNAME]",
1993 NO_STR
1994 "Reset system's network name\n"
1995 "Host name of this router\n")
1996 {
1997 return cmd_hostname_set(NULL);
1998 }
1999
2000 /* VTY interface password set. */
2001 DEFUN (config_password,
2002 password_cmd,
2003 "password [(8-8)] WORD",
2004 "Modify the terminal connection password\n"
2005 "Specifies a HIDDEN password will follow\n"
2006 "The password string\n")
2007 {
2008 int idx_8 = 1;
2009 int idx_word = 2;
2010 if (argc == 3) // '8' was specified
2011 {
2012 if (host.password)
2013 XFREE(MTYPE_HOST, host.password);
2014 host.password = NULL;
2015 if (host.password_encrypt)
2016 XFREE(MTYPE_HOST, host.password_encrypt);
2017 host.password_encrypt =
2018 XSTRDUP(MTYPE_HOST, argv[idx_word]->arg);
2019 return CMD_SUCCESS;
2020 }
2021
2022 if (!isalnum((int)argv[idx_8]->arg[0])) {
2023 vty_out(vty,
2024 "Please specify string starting with alphanumeric\n");
2025 return CMD_WARNING_CONFIG_FAILED;
2026 }
2027
2028 if (host.password)
2029 XFREE(MTYPE_HOST, host.password);
2030 host.password = NULL;
2031
2032 if (host.encrypt) {
2033 if (host.password_encrypt)
2034 XFREE(MTYPE_HOST, host.password_encrypt);
2035 host.password_encrypt =
2036 XSTRDUP(MTYPE_HOST, zencrypt(argv[idx_8]->arg));
2037 } else
2038 host.password = XSTRDUP(MTYPE_HOST, argv[idx_8]->arg);
2039
2040 return CMD_SUCCESS;
2041 }
2042
2043 /* VTY interface password delete. */
2044 DEFUN (no_config_password,
2045 no_password_cmd,
2046 "no password",
2047 NO_STR
2048 "Modify the terminal connection password\n")
2049 {
2050 bool warned = false;
2051
2052 if (host.password) {
2053 if (!vty_shell_serv(vty)) {
2054 vty_out(vty, NO_PASSWD_CMD_WARNING);
2055 warned = true;
2056 }
2057 XFREE(MTYPE_HOST, host.password);
2058 }
2059 host.password = NULL;
2060
2061 if (host.password_encrypt) {
2062 if (!warned && !vty_shell_serv(vty))
2063 vty_out(vty, NO_PASSWD_CMD_WARNING);
2064 XFREE(MTYPE_HOST, host.password_encrypt);
2065 }
2066 host.password_encrypt = NULL;
2067
2068 return CMD_SUCCESS;
2069 }
2070
2071 /* VTY enable password set. */
2072 DEFUN (config_enable_password,
2073 enable_password_cmd,
2074 "enable password [(8-8)] WORD",
2075 "Modify enable password parameters\n"
2076 "Assign the privileged level password\n"
2077 "Specifies a HIDDEN password will follow\n"
2078 "The HIDDEN 'enable' password string\n")
2079 {
2080 int idx_8 = 2;
2081 int idx_word = 3;
2082
2083 /* Crypt type is specified. */
2084 if (argc == 4) {
2085 if (argv[idx_8]->arg[0] == '8') {
2086 if (host.enable)
2087 XFREE(MTYPE_HOST, host.enable);
2088 host.enable = NULL;
2089
2090 if (host.enable_encrypt)
2091 XFREE(MTYPE_HOST, host.enable_encrypt);
2092 host.enable_encrypt =
2093 XSTRDUP(MTYPE_HOST, argv[idx_word]->arg);
2094
2095 return CMD_SUCCESS;
2096 } else {
2097 vty_out(vty, "Unknown encryption type.\n");
2098 return CMD_WARNING_CONFIG_FAILED;
2099 }
2100 }
2101
2102 if (!isalnum((int)argv[idx_8]->arg[0])) {
2103 vty_out(vty,
2104 "Please specify string starting with alphanumeric\n");
2105 return CMD_WARNING_CONFIG_FAILED;
2106 }
2107
2108 if (host.enable)
2109 XFREE(MTYPE_HOST, host.enable);
2110 host.enable = NULL;
2111
2112 /* Plain password input. */
2113 if (host.encrypt) {
2114 if (host.enable_encrypt)
2115 XFREE(MTYPE_HOST, host.enable_encrypt);
2116 host.enable_encrypt =
2117 XSTRDUP(MTYPE_HOST, zencrypt(argv[idx_8]->arg));
2118 } else
2119 host.enable = XSTRDUP(MTYPE_HOST, argv[idx_8]->arg);
2120
2121 return CMD_SUCCESS;
2122 }
2123
2124 /* VTY enable password delete. */
2125 DEFUN (no_config_enable_password,
2126 no_enable_password_cmd,
2127 "no enable password",
2128 NO_STR
2129 "Modify enable password parameters\n"
2130 "Assign the privileged level password\n")
2131 {
2132 bool warned = false;
2133
2134 if (host.enable) {
2135 if (!vty_shell_serv(vty)) {
2136 vty_out(vty, NO_PASSWD_CMD_WARNING);
2137 warned = true;
2138 }
2139 XFREE(MTYPE_HOST, host.enable);
2140 }
2141 host.enable = NULL;
2142
2143 if (host.enable_encrypt) {
2144 if (!warned && !vty_shell_serv(vty))
2145 vty_out(vty, NO_PASSWD_CMD_WARNING);
2146 XFREE(MTYPE_HOST, host.enable_encrypt);
2147 }
2148 host.enable_encrypt = NULL;
2149
2150 return CMD_SUCCESS;
2151 }
2152
2153 DEFUN (service_password_encrypt,
2154 service_password_encrypt_cmd,
2155 "service password-encryption",
2156 "Set up miscellaneous service\n"
2157 "Enable encrypted passwords\n")
2158 {
2159 if (host.encrypt)
2160 return CMD_SUCCESS;
2161
2162 host.encrypt = 1;
2163
2164 if (host.password) {
2165 if (host.password_encrypt)
2166 XFREE(MTYPE_HOST, host.password_encrypt);
2167 host.password_encrypt =
2168 XSTRDUP(MTYPE_HOST, zencrypt(host.password));
2169 }
2170 if (host.enable) {
2171 if (host.enable_encrypt)
2172 XFREE(MTYPE_HOST, host.enable_encrypt);
2173 host.enable_encrypt =
2174 XSTRDUP(MTYPE_HOST, zencrypt(host.enable));
2175 }
2176
2177 return CMD_SUCCESS;
2178 }
2179
2180 DEFUN (no_service_password_encrypt,
2181 no_service_password_encrypt_cmd,
2182 "no service password-encryption",
2183 NO_STR
2184 "Set up miscellaneous service\n"
2185 "Enable encrypted passwords\n")
2186 {
2187 if (!host.encrypt)
2188 return CMD_SUCCESS;
2189
2190 host.encrypt = 0;
2191
2192 if (host.password_encrypt)
2193 XFREE(MTYPE_HOST, host.password_encrypt);
2194 host.password_encrypt = NULL;
2195
2196 if (host.enable_encrypt)
2197 XFREE(MTYPE_HOST, host.enable_encrypt);
2198 host.enable_encrypt = NULL;
2199
2200 return CMD_SUCCESS;
2201 }
2202
2203 DEFUN (config_terminal_length,
2204 config_terminal_length_cmd,
2205 "terminal length (0-512)",
2206 "Set terminal line parameters\n"
2207 "Set number of lines on a screen\n"
2208 "Number of lines on screen (0 for no pausing)\n")
2209 {
2210 int idx_number = 2;
2211
2212 vty->lines = atoi(argv[idx_number]->arg);
2213
2214 return CMD_SUCCESS;
2215 }
2216
2217 DEFUN (config_terminal_no_length,
2218 config_terminal_no_length_cmd,
2219 "terminal no length",
2220 "Set terminal line parameters\n"
2221 NO_STR
2222 "Set number of lines on a screen\n")
2223 {
2224 vty->lines = -1;
2225 return CMD_SUCCESS;
2226 }
2227
2228 DEFUN (service_terminal_length,
2229 service_terminal_length_cmd,
2230 "service terminal-length (0-512)",
2231 "Set up miscellaneous service\n"
2232 "System wide terminal length configuration\n"
2233 "Number of lines of VTY (0 means no line control)\n")
2234 {
2235 int idx_number = 2;
2236
2237 host.lines = atoi(argv[idx_number]->arg);
2238
2239 return CMD_SUCCESS;
2240 }
2241
2242 DEFUN (no_service_terminal_length,
2243 no_service_terminal_length_cmd,
2244 "no service terminal-length [(0-512)]",
2245 NO_STR
2246 "Set up miscellaneous service\n"
2247 "System wide terminal length configuration\n"
2248 "Number of lines of VTY (0 means no line control)\n")
2249 {
2250 host.lines = -1;
2251 return CMD_SUCCESS;
2252 }
2253
2254 DEFUN_HIDDEN (do_echo,
2255 echo_cmd,
2256 "echo MESSAGE...",
2257 "Echo a message back to the vty\n"
2258 "The message to echo\n")
2259 {
2260 char *message;
2261
2262 vty_out(vty, "%s\n",
2263 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2264 if (message)
2265 XFREE(MTYPE_TMP, message);
2266 return CMD_SUCCESS;
2267 }
2268
2269 DEFUN (config_logmsg,
2270 config_logmsg_cmd,
2271 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
2272 "Send a message to enabled logging destinations\n"
2273 LOG_LEVEL_DESC
2274 "The message to send\n")
2275 {
2276 int idx_log_level = 1;
2277 int idx_message = 2;
2278 int level;
2279 char *message;
2280
2281 if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)
2282 return CMD_ERR_NO_MATCH;
2283
2284 zlog(level, "%s",
2285 ((message = argv_concat(argv, argc, idx_message)) ? message : ""));
2286 if (message)
2287 XFREE(MTYPE_TMP, message);
2288
2289 return CMD_SUCCESS;
2290 }
2291
2292 DEFUN (show_logging,
2293 show_logging_cmd,
2294 "show logging",
2295 SHOW_STR
2296 "Show current logging configuration\n")
2297 {
2298 struct zlog *zl = zlog_default;
2299
2300 vty_out(vty, "Syslog logging: ");
2301 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2302 vty_out(vty, "disabled");
2303 else
2304 vty_out(vty, "level %s, facility %s, ident %s",
2305 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2306 facility_name(zl->facility), zl->ident);
2307 vty_out(vty, "\n");
2308
2309 vty_out(vty, "Stdout logging: ");
2310 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2311 vty_out(vty, "disabled");
2312 else
2313 vty_out(vty, "level %s",
2314 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2315 vty_out(vty, "\n");
2316
2317 vty_out(vty, "Monitor logging: ");
2318 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2319 vty_out(vty, "disabled");
2320 else
2321 vty_out(vty, "level %s",
2322 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2323 vty_out(vty, "\n");
2324
2325 vty_out(vty, "File logging: ");
2326 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2327 vty_out(vty, "disabled");
2328 else
2329 vty_out(vty, "level %s, filename %s",
2330 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2331 zl->filename);
2332 vty_out(vty, "\n");
2333
2334 vty_out(vty, "Protocol name: %s\n", zl->protoname);
2335 vty_out(vty, "Record priority: %s\n",
2336 (zl->record_priority ? "enabled" : "disabled"));
2337 vty_out(vty, "Timestamp precision: %d\n", zl->timestamp_precision);
2338
2339 return CMD_SUCCESS;
2340 }
2341
2342 DEFUN (config_log_stdout,
2343 config_log_stdout_cmd,
2344 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2345 "Logging control\n"
2346 "Set stdout logging level\n"
2347 LOG_LEVEL_DESC)
2348 {
2349 int idx_log_level = 2;
2350
2351 if (argc == idx_log_level) {
2352 zlog_set_level(ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2353 return CMD_SUCCESS;
2354 }
2355 int level;
2356
2357 if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)
2358 return CMD_ERR_NO_MATCH;
2359 zlog_set_level(ZLOG_DEST_STDOUT, level);
2360 return CMD_SUCCESS;
2361 }
2362
2363 DEFUN (no_config_log_stdout,
2364 no_config_log_stdout_cmd,
2365 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2366 NO_STR
2367 "Logging control\n"
2368 "Cancel logging to stdout\n"
2369 LOG_LEVEL_DESC)
2370 {
2371 zlog_set_level(ZLOG_DEST_STDOUT, ZLOG_DISABLED);
2372 return CMD_SUCCESS;
2373 }
2374
2375 DEFUN (config_log_monitor,
2376 config_log_monitor_cmd,
2377 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2378 "Logging control\n"
2379 "Set terminal line (monitor) logging level\n"
2380 LOG_LEVEL_DESC)
2381 {
2382 int idx_log_level = 2;
2383
2384 if (argc == idx_log_level) {
2385 zlog_set_level(ZLOG_DEST_MONITOR, zlog_default->default_lvl);
2386 return CMD_SUCCESS;
2387 }
2388 int level;
2389
2390 if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)
2391 return CMD_ERR_NO_MATCH;
2392 zlog_set_level(ZLOG_DEST_MONITOR, level);
2393 return CMD_SUCCESS;
2394 }
2395
2396 DEFUN (no_config_log_monitor,
2397 no_config_log_monitor_cmd,
2398 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2399 NO_STR
2400 "Logging control\n"
2401 "Disable terminal line (monitor) logging\n"
2402 LOG_LEVEL_DESC)
2403 {
2404 zlog_set_level(ZLOG_DEST_MONITOR, ZLOG_DISABLED);
2405 return CMD_SUCCESS;
2406 }
2407
2408 static int set_log_file(struct vty *vty, const char *fname, int loglevel)
2409 {
2410 int ret;
2411 char *p = NULL;
2412 const char *fullpath;
2413
2414 /* Path detection. */
2415 if (!IS_DIRECTORY_SEP(*fname)) {
2416 char cwd[MAXPATHLEN + 1];
2417 cwd[MAXPATHLEN] = '\0';
2418
2419 if (getcwd(cwd, MAXPATHLEN) == NULL) {
2420 flog_err_sys(LIB_ERR_SYSTEM_CALL,
2421 "config_log_file: Unable to alloc mem!");
2422 return CMD_WARNING_CONFIG_FAILED;
2423 }
2424
2425 p = XMALLOC(MTYPE_TMP, strlen(cwd) + strlen(fname) + 2);
2426 sprintf(p, "%s/%s", cwd, fname);
2427 fullpath = p;
2428 } else
2429 fullpath = fname;
2430
2431 ret = zlog_set_file(fullpath, loglevel);
2432
2433 if (p)
2434 XFREE(MTYPE_TMP, p);
2435
2436 if (!ret) {
2437 if (vty)
2438 vty_out(vty, "can't open logfile %s\n", fname);
2439 return CMD_WARNING_CONFIG_FAILED;
2440 }
2441
2442 if (host.logfile)
2443 XFREE(MTYPE_HOST, host.logfile);
2444
2445 host.logfile = XSTRDUP(MTYPE_HOST, fname);
2446
2447 #if defined(HAVE_CUMULUS)
2448 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED)
2449 zlog_set_level(ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
2450 #endif
2451 return CMD_SUCCESS;
2452 }
2453
2454 void command_setup_early_logging(const char *dest, const char *level)
2455 {
2456 char *token;
2457
2458 if (level) {
2459 int nlevel = level_match(level);
2460
2461 if (nlevel != ZLOG_DISABLED)
2462 zlog_default->default_lvl = nlevel;
2463 }
2464
2465 if (!dest)
2466 return;
2467
2468 if (strcmp(dest, "stdout") == 0) {
2469 zlog_set_level(ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2470 return;
2471 }
2472
2473 if (strcmp(dest, "syslog") == 0) {
2474 zlog_set_level(ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2475 return;
2476 }
2477
2478 token = strstr(dest, ":");
2479 if (token == NULL)
2480 return;
2481
2482 token++;
2483
2484 set_log_file(NULL, token, zlog_default->default_lvl);
2485 }
2486
2487 DEFUN (config_log_file,
2488 config_log_file_cmd,
2489 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2490 "Logging control\n"
2491 "Logging to file\n"
2492 "Logging filename\n"
2493 LOG_LEVEL_DESC)
2494 {
2495 int idx_filename = 2;
2496 int idx_log_levels = 3;
2497 if (argc == 4) {
2498 int level;
2499 if ((level = level_match(argv[idx_log_levels]->arg))
2500 == ZLOG_DISABLED)
2501 return CMD_ERR_NO_MATCH;
2502 return set_log_file(vty, argv[idx_filename]->arg, level);
2503 } else
2504 return set_log_file(vty, argv[idx_filename]->arg,
2505 zlog_default->default_lvl);
2506 }
2507
2508 static void disable_log_file(void)
2509 {
2510 zlog_reset_file();
2511
2512 if (host.logfile)
2513 XFREE(MTYPE_HOST, host.logfile);
2514
2515 host.logfile = NULL;
2516 }
2517
2518 DEFUN (no_config_log_file,
2519 no_config_log_file_cmd,
2520 "no log file [FILENAME [LEVEL]]",
2521 NO_STR
2522 "Logging control\n"
2523 "Cancel logging to file\n"
2524 "Logging file name\n"
2525 "Logging level\n")
2526 {
2527 disable_log_file();
2528 return CMD_SUCCESS;
2529 }
2530
2531 DEFUN (config_log_syslog,
2532 config_log_syslog_cmd,
2533 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2534 "Logging control\n"
2535 "Set syslog logging level\n"
2536 LOG_LEVEL_DESC)
2537 {
2538 int idx_log_levels = 2;
2539
2540 disable_log_file();
2541
2542 if (argc == 3) {
2543 int level;
2544 if ((level = level_match(argv[idx_log_levels]->arg))
2545 == ZLOG_DISABLED)
2546 return CMD_ERR_NO_MATCH;
2547 zlog_set_level(ZLOG_DEST_SYSLOG, level);
2548 return CMD_SUCCESS;
2549 } else {
2550 zlog_set_level(ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2551 return CMD_SUCCESS;
2552 }
2553 }
2554
2555 DEFUN (no_config_log_syslog,
2556 no_config_log_syslog_cmd,
2557 "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>]",
2558 NO_STR
2559 "Logging control\n"
2560 "Cancel logging to syslog\n"
2561 LOG_FACILITY_DESC
2562 LOG_LEVEL_DESC)
2563 {
2564 zlog_set_level(ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
2565 return CMD_SUCCESS;
2566 }
2567
2568 DEFUN (config_log_facility,
2569 config_log_facility_cmd,
2570 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2571 "Logging control\n"
2572 "Facility parameter for syslog messages\n"
2573 LOG_FACILITY_DESC)
2574 {
2575 int idx_target = 2;
2576 int facility = facility_match(argv[idx_target]->arg);
2577
2578 zlog_default->facility = facility;
2579 return CMD_SUCCESS;
2580 }
2581
2582 DEFUN (no_config_log_facility,
2583 no_config_log_facility_cmd,
2584 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2585 NO_STR
2586 "Logging control\n"
2587 "Reset syslog facility to default (daemon)\n"
2588 LOG_FACILITY_DESC)
2589 {
2590 zlog_default->facility = LOG_DAEMON;
2591 return CMD_SUCCESS;
2592 }
2593
2594 DEFUN (config_log_record_priority,
2595 config_log_record_priority_cmd,
2596 "log record-priority",
2597 "Logging control\n"
2598 "Log the priority of the message within the message\n")
2599 {
2600 zlog_default->record_priority = 1;
2601 return CMD_SUCCESS;
2602 }
2603
2604 DEFUN (no_config_log_record_priority,
2605 no_config_log_record_priority_cmd,
2606 "no log record-priority",
2607 NO_STR
2608 "Logging control\n"
2609 "Do not log the priority of the message within the message\n")
2610 {
2611 zlog_default->record_priority = 0;
2612 return CMD_SUCCESS;
2613 }
2614
2615 DEFUN (config_log_timestamp_precision,
2616 config_log_timestamp_precision_cmd,
2617 "log timestamp precision (0-6)",
2618 "Logging control\n"
2619 "Timestamp configuration\n"
2620 "Set the timestamp precision\n"
2621 "Number of subsecond digits\n")
2622 {
2623 int idx_number = 3;
2624 zlog_default->timestamp_precision =
2625 strtoul(argv[idx_number]->arg, NULL, 10);
2626 return CMD_SUCCESS;
2627 }
2628
2629 DEFUN (no_config_log_timestamp_precision,
2630 no_config_log_timestamp_precision_cmd,
2631 "no log timestamp precision",
2632 NO_STR
2633 "Logging control\n"
2634 "Timestamp configuration\n"
2635 "Reset the timestamp precision to the default value of 0\n")
2636 {
2637 zlog_default->timestamp_precision = 0;
2638 return CMD_SUCCESS;
2639 }
2640
2641 DEFUN (debug_memstats,
2642 debug_memstats_cmd,
2643 "[no] debug memstats-at-exit",
2644 NO_STR
2645 DEBUG_STR
2646 "Print memory type statistics at exit\n")
2647 {
2648 debug_memstats_at_exit = !!strcmp(argv[0]->text, "no");
2649 return CMD_SUCCESS;
2650 }
2651
2652 int cmd_banner_motd_file(const char *file)
2653 {
2654 int success = CMD_SUCCESS;
2655 char p[PATH_MAX];
2656 char *rpath;
2657 char *in;
2658
2659 rpath = realpath(file, p);
2660 if (!rpath)
2661 return CMD_ERR_NO_FILE;
2662 in = strstr(rpath, SYSCONFDIR);
2663 if (in == rpath) {
2664 if (host.motdfile)
2665 XFREE(MTYPE_HOST, host.motdfile);
2666 host.motdfile = XSTRDUP(MTYPE_HOST, file);
2667 } else
2668 success = CMD_WARNING_CONFIG_FAILED;
2669
2670 return success;
2671 }
2672
2673 DEFUN (banner_motd_file,
2674 banner_motd_file_cmd,
2675 "banner motd file FILE",
2676 "Set banner\n"
2677 "Banner for motd\n"
2678 "Banner from a file\n"
2679 "Filename\n")
2680 {
2681 int idx_file = 3;
2682 const char *filename = argv[idx_file]->arg;
2683 int cmd = cmd_banner_motd_file(filename);
2684
2685 if (cmd == CMD_ERR_NO_FILE)
2686 vty_out(vty, "%s does not exist", filename);
2687 else if (cmd == CMD_WARNING_CONFIG_FAILED)
2688 vty_out(vty, "%s must be in %s", filename, SYSCONFDIR);
2689
2690 return cmd;
2691 }
2692
2693 DEFUN (banner_motd_default,
2694 banner_motd_default_cmd,
2695 "banner motd default",
2696 "Set banner string\n"
2697 "Strings for motd\n"
2698 "Default string\n")
2699 {
2700 host.motd = default_motd;
2701 return CMD_SUCCESS;
2702 }
2703
2704 DEFUN (no_banner_motd,
2705 no_banner_motd_cmd,
2706 "no banner motd",
2707 NO_STR
2708 "Set banner string\n"
2709 "Strings for motd\n")
2710 {
2711 host.motd = NULL;
2712 if (host.motdfile)
2713 XFREE(MTYPE_HOST, host.motdfile);
2714 host.motdfile = NULL;
2715 return CMD_SUCCESS;
2716 }
2717
2718 DEFUN(find,
2719 find_cmd,
2720 "find COMMAND...",
2721 "Find CLI command containing text\n"
2722 "Text to search for\n")
2723 {
2724 char *text = argv_concat(argv, argc, 1);
2725 const struct cmd_node *node;
2726 const struct cmd_element *cli;
2727 vector clis;
2728
2729 for (unsigned int i = 0; i < vector_active(cmdvec); i++) {
2730 node = vector_slot(cmdvec, i);
2731 if (!node)
2732 continue;
2733 clis = node->cmd_vector;
2734 for (unsigned int j = 0; j < vector_active(clis); j++) {
2735 cli = vector_slot(clis, j);
2736 if (strcasestr(cli->string, text))
2737 vty_out(vty, " (%s) %s\n",
2738 node_names[node->node], cli->string);
2739 }
2740 }
2741
2742 XFREE(MTYPE_TMP, text);
2743
2744 return CMD_SUCCESS;
2745 }
2746
2747 /* Set config filename. Called from vty.c */
2748 void host_config_set(const char *filename)
2749 {
2750 if (host.config)
2751 XFREE(MTYPE_HOST, host.config);
2752 host.config = XSTRDUP(MTYPE_HOST, filename);
2753 }
2754
2755 const char *host_config_get(void)
2756 {
2757 return host.config;
2758 }
2759
2760 void install_default(enum node_type node)
2761 {
2762 install_element(node, &config_exit_cmd);
2763 install_element(node, &config_quit_cmd);
2764 install_element(node, &config_end_cmd);
2765 install_element(node, &config_help_cmd);
2766 install_element(node, &config_list_cmd);
2767 install_element(node, &show_cli_graph_cmd);
2768 install_element(node, &find_cmd);
2769
2770 install_element(node, &config_write_cmd);
2771 install_element(node, &show_running_config_cmd);
2772
2773 install_element(node, &autocomplete_cmd);
2774 }
2775
2776 /* Initialize command interface. Install basic nodes and commands.
2777 *
2778 * terminal = 0 -- vtysh / no logging, no config control
2779 * terminal = 1 -- normal daemon
2780 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2781 void cmd_init(int terminal)
2782 {
2783 struct utsname names;
2784
2785 if (array_size(node_names) != NODE_TYPE_MAX)
2786 assert(!"Update the CLI node description array!");
2787
2788 uname(&names);
2789 qobj_init();
2790
2791 /* register command preprocessors */
2792 hook_register(cmd_execute, handle_pipe_action);
2793 hook_register(cmd_execute_done, handle_pipe_action_done);
2794
2795 varhandlers = list_new();
2796
2797 /* Allocate initial top vector of commands. */
2798 cmdvec = vector_init(VECTOR_MIN_SIZE);
2799
2800 /* Default host value settings. */
2801 host.name = XSTRDUP(MTYPE_HOST, names.nodename);
2802 #ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
2803 if ((strcmp(names.domainname, "(none)") == 0))
2804 host.domainname = NULL;
2805 else
2806 host.domainname = XSTRDUP(MTYPE_HOST, names.domainname);
2807 #else
2808 host.domainname = NULL;
2809 #endif
2810 host.password = NULL;
2811 host.enable = NULL;
2812 host.logfile = NULL;
2813 host.config = NULL;
2814 host.noconfig = (terminal < 0);
2815 host.lines = -1;
2816 host.motd = default_motd;
2817 host.motdfile = NULL;
2818
2819 /* Install top nodes. */
2820 install_node(&view_node, NULL);
2821 install_node(&enable_node, NULL);
2822 install_node(&auth_node, NULL);
2823 install_node(&auth_enable_node, NULL);
2824 install_node(&config_node, config_write_host);
2825
2826 /* Each node's basic commands. */
2827 install_element(VIEW_NODE, &show_version_cmd);
2828 install_element(ENABLE_NODE, &show_startup_config_cmd);
2829 install_element(ENABLE_NODE, &debug_memstats_cmd);
2830
2831 if (terminal) {
2832 install_element(VIEW_NODE, &config_list_cmd);
2833 install_element(VIEW_NODE, &config_exit_cmd);
2834 install_element(VIEW_NODE, &config_quit_cmd);
2835 install_element(VIEW_NODE, &config_help_cmd);
2836 install_element(VIEW_NODE, &config_enable_cmd);
2837 install_element(VIEW_NODE, &config_terminal_length_cmd);
2838 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
2839 install_element(VIEW_NODE, &show_logging_cmd);
2840 install_element(VIEW_NODE, &show_commandtree_cmd);
2841 install_element(VIEW_NODE, &echo_cmd);
2842 install_element(VIEW_NODE, &autocomplete_cmd);
2843 install_element(VIEW_NODE, &find_cmd);
2844
2845 install_element(ENABLE_NODE, &config_end_cmd);
2846 install_element(ENABLE_NODE, &config_disable_cmd);
2847 install_element(ENABLE_NODE, &config_terminal_cmd);
2848 install_element(ENABLE_NODE, &copy_runningconf_startupconf_cmd);
2849 install_element(ENABLE_NODE, &config_write_cmd);
2850 install_element(ENABLE_NODE, &show_running_config_cmd);
2851 install_element(ENABLE_NODE, &config_logmsg_cmd);
2852
2853 install_default(CONFIG_NODE);
2854
2855 thread_cmd_init();
2856 workqueue_cmd_init();
2857 hash_cmd_init();
2858 }
2859
2860 install_element(CONFIG_NODE, &hostname_cmd);
2861 install_element(CONFIG_NODE, &no_hostname_cmd);
2862 install_element(CONFIG_NODE, &domainname_cmd);
2863 install_element(CONFIG_NODE, &no_domainname_cmd);
2864 install_element(CONFIG_NODE, &frr_version_defaults_cmd);
2865 install_element(CONFIG_NODE, &debug_memstats_cmd);
2866
2867 if (terminal > 0) {
2868 install_element(CONFIG_NODE, &password_cmd);
2869 install_element(CONFIG_NODE, &no_password_cmd);
2870 install_element(CONFIG_NODE, &enable_password_cmd);
2871 install_element(CONFIG_NODE, &no_enable_password_cmd);
2872
2873 install_element(CONFIG_NODE, &config_log_stdout_cmd);
2874 install_element(CONFIG_NODE, &no_config_log_stdout_cmd);
2875 install_element(CONFIG_NODE, &config_log_monitor_cmd);
2876 install_element(CONFIG_NODE, &no_config_log_monitor_cmd);
2877 install_element(CONFIG_NODE, &config_log_file_cmd);
2878 install_element(CONFIG_NODE, &no_config_log_file_cmd);
2879 install_element(CONFIG_NODE, &config_log_syslog_cmd);
2880 install_element(CONFIG_NODE, &no_config_log_syslog_cmd);
2881 install_element(CONFIG_NODE, &config_log_facility_cmd);
2882 install_element(CONFIG_NODE, &no_config_log_facility_cmd);
2883 install_element(CONFIG_NODE, &config_log_record_priority_cmd);
2884 install_element(CONFIG_NODE,
2885 &no_config_log_record_priority_cmd);
2886 install_element(CONFIG_NODE,
2887 &config_log_timestamp_precision_cmd);
2888 install_element(CONFIG_NODE,
2889 &no_config_log_timestamp_precision_cmd);
2890 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
2891 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
2892 install_element(CONFIG_NODE, &banner_motd_default_cmd);
2893 install_element(CONFIG_NODE, &banner_motd_file_cmd);
2894 install_element(CONFIG_NODE, &no_banner_motd_cmd);
2895 install_element(CONFIG_NODE, &service_terminal_length_cmd);
2896 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
2897
2898 vrf_install_commands();
2899 }
2900
2901 #ifdef DEV_BUILD
2902 grammar_sandbox_init();
2903 #endif
2904 }
2905
2906 void cmd_terminate()
2907 {
2908 struct cmd_node *cmd_node;
2909
2910 hook_unregister(cmd_execute, handle_pipe_action);
2911 hook_unregister(cmd_execute_done, handle_pipe_action_done);
2912
2913 if (cmdvec) {
2914 for (unsigned int i = 0; i < vector_active(cmdvec); i++)
2915 if ((cmd_node = vector_slot(cmdvec, i)) != NULL) {
2916 // deleting the graph delets the cmd_element as
2917 // well
2918 graph_delete_graph(cmd_node->cmdgraph);
2919 vector_free(cmd_node->cmd_vector);
2920 hash_clean(cmd_node->cmd_hash, NULL);
2921 hash_free(cmd_node->cmd_hash);
2922 cmd_node->cmd_hash = NULL;
2923 }
2924
2925 vector_free(cmdvec);
2926 cmdvec = NULL;
2927 }
2928
2929 if (host.name)
2930 XFREE(MTYPE_HOST, host.name);
2931 if (host.domainname)
2932 XFREE(MTYPE_HOST, host.domainname);
2933 if (host.password)
2934 XFREE(MTYPE_HOST, host.password);
2935 if (host.password_encrypt)
2936 XFREE(MTYPE_HOST, host.password_encrypt);
2937 if (host.enable)
2938 XFREE(MTYPE_HOST, host.enable);
2939 if (host.enable_encrypt)
2940 XFREE(MTYPE_HOST, host.enable_encrypt);
2941 if (host.logfile)
2942 XFREE(MTYPE_HOST, host.logfile);
2943 if (host.motdfile)
2944 XFREE(MTYPE_HOST, host.motdfile);
2945 if (host.config)
2946 XFREE(MTYPE_HOST, host.config);
2947
2948 list_delete_and_null(&varhandlers);
2949 qobj_finish();
2950 }