]> git.proxmox.com Git - mirror_frr.git/blob - lib/command.c
*: fix clang-6 SA warnings
[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 assert(token);
1201
1202 /* match result to known actions */
1203 if (strmatch(token, "include")) {
1204 /* the remaining text should be a regexp */
1205 char *regexp = working;
1206
1207 if (!regexp) {
1208 vty_out(vty, "%% Need a regexp to filter with\n");
1209 goto fail;
1210 }
1211
1212 bool succ = vty_set_include(vty, regexp);
1213
1214 if (!succ) {
1215 vty_out(vty, "%% Bad regexp '%s'\n", regexp);
1216 goto fail;
1217 }
1218 *cmd_out = XSTRDUP(MTYPE_TMP, cmd_in);
1219 u = *cmd_out;
1220 strsep(&u, "|");
1221 } else {
1222 vty_out(vty, "%% Unknown action '%s'\n", token);
1223 goto fail;
1224 }
1225
1226 fail:
1227 XFREE(MTYPE_TMP, orig);
1228 return 0;
1229 }
1230
1231 static int handle_pipe_action_done(struct vty *vty, const char *cmd_exec)
1232 {
1233 if (vty->filter)
1234 vty_set_include(vty, NULL);
1235
1236 return 0;
1237 }
1238
1239 int cmd_execute(struct vty *vty, const char *cmd,
1240 const struct cmd_element **matched, int vtysh)
1241 {
1242 int ret;
1243 char *cmd_out = NULL;
1244 const char *cmd_exec;
1245 vector vline;
1246
1247 hook_call(cmd_execute, vty, cmd, &cmd_out);
1248 cmd_exec = cmd_out ? (const char *)cmd_out : cmd;
1249
1250 vline = cmd_make_strvec(cmd_exec);
1251
1252 if (vline) {
1253 ret = cmd_execute_command(vline, vty, matched, vtysh);
1254 cmd_free_strvec(vline);
1255 } else {
1256 ret = CMD_SUCCESS;
1257 }
1258
1259 hook_call(cmd_execute_done, vty, cmd_exec);
1260
1261 if (cmd_out)
1262 XFREE(MTYPE_TMP, cmd_out);
1263
1264 return ret;
1265 }
1266
1267
1268 /**
1269 * Parse one line of config, walking up the parse tree attempting to find a
1270 * match
1271 *
1272 * @param vty The vty context in which the command should be executed.
1273 * @param cmd Pointer where the struct cmd_element* of the match command
1274 * will be stored, if any. May be set to NULL if this info is
1275 * not needed.
1276 * @param use_daemon Boolean to control whether or not we match on
1277 * CMD_SUCCESS_DAEMON
1278 * or not.
1279 * @return The status of the command that has been executed or an error code
1280 * as to why no command could be executed.
1281 */
1282 int command_config_read_one_line(struct vty *vty,
1283 const struct cmd_element **cmd, int use_daemon)
1284 {
1285 vector vline;
1286 int saved_node;
1287 int ret;
1288
1289 vline = cmd_make_strvec(vty->buf);
1290
1291 /* In case of comment line */
1292 if (vline == NULL)
1293 return CMD_SUCCESS;
1294
1295 /* Execute configuration command : this is strict match */
1296 ret = cmd_execute_command_strict(vline, vty, cmd);
1297
1298 // Climb the tree and try the command again at each node
1299 if (!(use_daemon && ret == CMD_SUCCESS_DAEMON)
1300 && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO)
1301 && ret != CMD_SUCCESS && ret != CMD_WARNING
1302 && ret != CMD_NOT_MY_INSTANCE && ret != CMD_WARNING_CONFIG_FAILED
1303 && vty->node != CONFIG_NODE) {
1304
1305 saved_node = vty->node;
1306
1307 while (!(use_daemon && ret == CMD_SUCCESS_DAEMON)
1308 && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO)
1309 && ret != CMD_SUCCESS && ret != CMD_WARNING
1310 && vty->node > CONFIG_NODE) {
1311 vty->node = node_parent(vty->node);
1312 ret = cmd_execute_command_strict(vline, vty, cmd);
1313 }
1314
1315 // If climbing the tree did not work then ignore the command and
1316 // stay at the same node
1317 if (!(use_daemon && ret == CMD_SUCCESS_DAEMON)
1318 && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO)
1319 && ret != CMD_SUCCESS && ret != CMD_WARNING) {
1320 vty->node = saved_node;
1321 }
1322 }
1323
1324 if (ret != CMD_SUCCESS && ret != CMD_WARNING)
1325 memcpy(vty->error_buf, vty->buf, VTY_BUFSIZ);
1326
1327 cmd_free_strvec(vline);
1328
1329 return ret;
1330 }
1331
1332 /* Configuration make from file. */
1333 int config_from_file(struct vty *vty, FILE *fp, unsigned int *line_num)
1334 {
1335 int ret, error_ret = 0;
1336 *line_num = 0;
1337
1338 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
1339 if (!error_ret)
1340 ++(*line_num);
1341
1342 ret = command_config_read_one_line(vty, NULL, 0);
1343
1344 if (ret != CMD_SUCCESS && ret != CMD_WARNING
1345 && ret != CMD_ERR_NOTHING_TODO)
1346 error_ret = ret;
1347 }
1348
1349 if (error_ret) {
1350 return error_ret;
1351 }
1352
1353 return CMD_SUCCESS;
1354 }
1355
1356 /* Configuration from terminal */
1357 DEFUN (config_terminal,
1358 config_terminal_cmd,
1359 "configure terminal",
1360 "Configuration from vty interface\n"
1361 "Configuration terminal\n")
1362 {
1363 if (vty_config_lock(vty))
1364 vty->node = CONFIG_NODE;
1365 else {
1366 vty_out(vty, "VTY configuration is locked by other VTY\n");
1367 return CMD_WARNING_CONFIG_FAILED;
1368 }
1369 return CMD_SUCCESS;
1370 }
1371
1372 /* Enable command */
1373 DEFUN (enable,
1374 config_enable_cmd,
1375 "enable",
1376 "Turn on privileged mode command\n")
1377 {
1378 /* If enable password is NULL, change to ENABLE_NODE */
1379 if ((host.enable == NULL && host.enable_encrypt == NULL)
1380 || vty->type == VTY_SHELL_SERV)
1381 vty->node = ENABLE_NODE;
1382 else
1383 vty->node = AUTH_ENABLE_NODE;
1384
1385 return CMD_SUCCESS;
1386 }
1387
1388 /* Disable command */
1389 DEFUN (disable,
1390 config_disable_cmd,
1391 "disable",
1392 "Turn off privileged mode command\n")
1393 {
1394 if (vty->node == ENABLE_NODE)
1395 vty->node = VIEW_NODE;
1396 return CMD_SUCCESS;
1397 }
1398
1399 /* Down vty node level. */
1400 DEFUN (config_exit,
1401 config_exit_cmd,
1402 "exit",
1403 "Exit current mode and down to previous mode\n")
1404 {
1405 cmd_exit(vty);
1406 return CMD_SUCCESS;
1407 }
1408
1409 void cmd_exit(struct vty *vty)
1410 {
1411 switch (vty->node) {
1412 case VIEW_NODE:
1413 case ENABLE_NODE:
1414 if (vty_shell(vty))
1415 exit(0);
1416 else
1417 vty->status = VTY_CLOSE;
1418 break;
1419 case CONFIG_NODE:
1420 vty->node = ENABLE_NODE;
1421 vty_config_unlock(vty);
1422 break;
1423 case INTERFACE_NODE:
1424 case PW_NODE:
1425 case LOGICALROUTER_NODE:
1426 case VRF_NODE:
1427 case NH_GROUP_NODE:
1428 case ZEBRA_NODE:
1429 case BGP_NODE:
1430 case RIP_NODE:
1431 case EIGRP_NODE:
1432 case BABEL_NODE:
1433 case RIPNG_NODE:
1434 case OSPF_NODE:
1435 case OSPF6_NODE:
1436 case LDP_NODE:
1437 case LDP_L2VPN_NODE:
1438 case ISIS_NODE:
1439 case KEYCHAIN_NODE:
1440 case RMAP_NODE:
1441 case PBRMAP_NODE:
1442 case VTY_NODE:
1443 case BFD_NODE:
1444 vty->node = CONFIG_NODE;
1445 break;
1446 case BGP_IPV4_NODE:
1447 case BGP_IPV4M_NODE:
1448 case BGP_IPV4L_NODE:
1449 case BGP_VPNV4_NODE:
1450 case BGP_VPNV6_NODE:
1451 case BGP_FLOWSPECV4_NODE:
1452 case BGP_FLOWSPECV6_NODE:
1453 case BGP_VRF_POLICY_NODE:
1454 case BGP_VNC_DEFAULTS_NODE:
1455 case BGP_VNC_NVE_GROUP_NODE:
1456 case BGP_VNC_L2_GROUP_NODE:
1457 case BGP_IPV6_NODE:
1458 case BGP_IPV6M_NODE:
1459 case BGP_EVPN_NODE:
1460 case BGP_IPV6L_NODE:
1461 vty->node = BGP_NODE;
1462 break;
1463 case BGP_EVPN_VNI_NODE:
1464 vty->node = BGP_EVPN_NODE;
1465 break;
1466 case LDP_IPV4_NODE:
1467 case LDP_IPV6_NODE:
1468 vty->node = LDP_NODE;
1469 break;
1470 case LDP_IPV4_IFACE_NODE:
1471 vty->node = LDP_IPV4_NODE;
1472 break;
1473 case LDP_IPV6_IFACE_NODE:
1474 vty->node = LDP_IPV6_NODE;
1475 break;
1476 case LDP_PSEUDOWIRE_NODE:
1477 vty->node = LDP_L2VPN_NODE;
1478 break;
1479 case KEYCHAIN_KEY_NODE:
1480 vty->node = KEYCHAIN_NODE;
1481 break;
1482 case LINK_PARAMS_NODE:
1483 vty->node = INTERFACE_NODE;
1484 break;
1485 case BFD_PEER_NODE:
1486 vty->node = BFD_NODE;
1487 break;
1488 default:
1489 break;
1490 }
1491 }
1492
1493 /* ALIAS_FIXME */
1494 DEFUN (config_quit,
1495 config_quit_cmd,
1496 "quit",
1497 "Exit current mode and down to previous mode\n")
1498 {
1499 return config_exit(self, vty, argc, argv);
1500 }
1501
1502
1503 /* End of configuration. */
1504 DEFUN (config_end,
1505 config_end_cmd,
1506 "end",
1507 "End current mode and change to enable mode.\n")
1508 {
1509 switch (vty->node) {
1510 case VIEW_NODE:
1511 case ENABLE_NODE:
1512 /* Nothing to do. */
1513 break;
1514 case CONFIG_NODE:
1515 case INTERFACE_NODE:
1516 case PW_NODE:
1517 case LOGICALROUTER_NODE:
1518 case VRF_NODE:
1519 case NH_GROUP_NODE:
1520 case ZEBRA_NODE:
1521 case RIP_NODE:
1522 case RIPNG_NODE:
1523 case EIGRP_NODE:
1524 case BABEL_NODE:
1525 case BGP_NODE:
1526 case BGP_VRF_POLICY_NODE:
1527 case BGP_VNC_DEFAULTS_NODE:
1528 case BGP_VNC_NVE_GROUP_NODE:
1529 case BGP_VNC_L2_GROUP_NODE:
1530 case BGP_VPNV4_NODE:
1531 case BGP_VPNV6_NODE:
1532 case BGP_FLOWSPECV4_NODE:
1533 case BGP_FLOWSPECV6_NODE:
1534 case BGP_IPV4_NODE:
1535 case BGP_IPV4M_NODE:
1536 case BGP_IPV4L_NODE:
1537 case BGP_IPV6_NODE:
1538 case BGP_IPV6M_NODE:
1539 case BGP_EVPN_NODE:
1540 case BGP_EVPN_VNI_NODE:
1541 case BGP_IPV6L_NODE:
1542 case RMAP_NODE:
1543 case PBRMAP_NODE:
1544 case OSPF_NODE:
1545 case OSPF6_NODE:
1546 case LDP_NODE:
1547 case LDP_IPV4_NODE:
1548 case LDP_IPV6_NODE:
1549 case LDP_IPV4_IFACE_NODE:
1550 case LDP_IPV6_IFACE_NODE:
1551 case LDP_L2VPN_NODE:
1552 case LDP_PSEUDOWIRE_NODE:
1553 case ISIS_NODE:
1554 case KEYCHAIN_NODE:
1555 case KEYCHAIN_KEY_NODE:
1556 case VTY_NODE:
1557 case LINK_PARAMS_NODE:
1558 case BFD_NODE:
1559 case BFD_PEER_NODE:
1560 vty_config_unlock(vty);
1561 vty->node = ENABLE_NODE;
1562 break;
1563 default:
1564 break;
1565 }
1566 return CMD_SUCCESS;
1567 }
1568
1569 /* Show version. */
1570 DEFUN (show_version,
1571 show_version_cmd,
1572 "show version",
1573 SHOW_STR
1574 "Displays zebra version\n")
1575 {
1576 vty_out(vty, "%s %s (%s).\n", FRR_FULL_NAME, FRR_VERSION,
1577 cmd_hostname_get() ? cmd_hostname_get() : "");
1578 vty_out(vty, "%s%s\n", FRR_COPYRIGHT, GIT_INFO);
1579 vty_out(vty, "configured with:\n %s\n", FRR_CONFIG_ARGS);
1580
1581 return CMD_SUCCESS;
1582 }
1583
1584 /* "Set" version ... ignore version tags */
1585 DEFUN (frr_version_defaults,
1586 frr_version_defaults_cmd,
1587 "frr <version|defaults> LINE...",
1588 "FRRouting global parameters\n"
1589 "version configuration was written by\n"
1590 "set of configuration defaults used\n"
1591 "version string\n")
1592 {
1593 return CMD_SUCCESS;
1594 }
1595
1596 /* Help display function for all node. */
1597 DEFUN (config_help,
1598 config_help_cmd,
1599 "help",
1600 "Description of the interactive help system\n")
1601 {
1602 vty_out(vty,
1603 "Quagga VTY provides advanced help feature. When you need help,\n\
1604 anytime at the command line please press '?'.\n\
1605 \n\
1606 If nothing matches, the help list will be empty and you must backup\n\
1607 until entering a '?' shows the available options.\n\
1608 Two styles of help are provided:\n\
1609 1. Full help is available when you are ready to enter a\n\
1610 command argument (e.g. 'show ?') and describes each possible\n\
1611 argument.\n\
1612 2. Partial help is provided when an abbreviated argument is entered\n\
1613 and you want to know what arguments match the input\n\
1614 (e.g. 'show me?'.)\n\n");
1615 return CMD_SUCCESS;
1616 }
1617
1618 static void permute(struct graph_node *start, struct vty *vty)
1619 {
1620 static struct list *position = NULL;
1621 if (!position)
1622 position = list_new();
1623
1624 struct cmd_token *stok = start->data;
1625 struct graph_node *gnn;
1626 struct listnode *ln;
1627
1628 // recursive dfs
1629 listnode_add(position, start);
1630 for (unsigned int i = 0; i < vector_active(start->to); i++) {
1631 struct graph_node *gn = vector_slot(start->to, i);
1632 struct cmd_token *tok = gn->data;
1633 if (tok->attr == CMD_ATTR_HIDDEN
1634 || tok->attr == CMD_ATTR_DEPRECATED)
1635 continue;
1636 else if (tok->type == END_TKN || gn == start) {
1637 vty_out(vty, " ");
1638 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) {
1639 struct cmd_token *tt = gnn->data;
1640 if (tt->type < SPECIAL_TKN)
1641 vty_out(vty, " %s", tt->text);
1642 }
1643 if (gn == start)
1644 vty_out(vty, "...");
1645 vty_out(vty, "\n");
1646 } else {
1647 bool skip = false;
1648 if (stok->type == FORK_TKN && tok->type != FORK_TKN)
1649 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn))
1650 if (gnn == gn) {
1651 skip = true;
1652 break;
1653 }
1654 if (!skip)
1655 permute(gn, vty);
1656 }
1657 }
1658 list_delete_node(position, listtail(position));
1659 }
1660
1661 int cmd_list_cmds(struct vty *vty, int do_permute)
1662 {
1663 struct cmd_node *node = vector_slot(cmdvec, vty->node);
1664
1665 if (do_permute)
1666 permute(vector_slot(node->cmdgraph->nodes, 0), vty);
1667 else {
1668 /* loop over all commands at this node */
1669 struct cmd_element *element = NULL;
1670 for (unsigned int i = 0; i < vector_active(node->cmd_vector);
1671 i++)
1672 if ((element = vector_slot(node->cmd_vector, i))
1673 && element->attr != CMD_ATTR_DEPRECATED
1674 && element->attr != CMD_ATTR_HIDDEN)
1675 vty_out(vty, " %s\n", element->string);
1676 }
1677 return CMD_SUCCESS;
1678 }
1679
1680 /* Help display function for all node. */
1681 DEFUN (config_list,
1682 config_list_cmd,
1683 "list [permutations]",
1684 "Print command list\n"
1685 "Print all possible command permutations\n")
1686 {
1687 return cmd_list_cmds(vty, argc == 2);
1688 }
1689
1690 DEFUN (show_commandtree,
1691 show_commandtree_cmd,
1692 "show commandtree [permutations]",
1693 SHOW_STR
1694 "Show command tree\n"
1695 "Permutations that we are interested in\n")
1696 {
1697 return cmd_list_cmds(vty, argc == 3);
1698 }
1699
1700 DEFUN_HIDDEN(show_cli_graph,
1701 show_cli_graph_cmd,
1702 "show cli graph",
1703 SHOW_STR
1704 "CLI reflection\n"
1705 "Dump current command space as DOT graph\n")
1706 {
1707 struct cmd_node *cn = vector_slot(cmdvec, vty->node);
1708 char *dot = cmd_graph_dump_dot(cn->cmdgraph);
1709
1710 vty_out(vty, "%s\n", dot);
1711 XFREE(MTYPE_TMP, dot);
1712 return CMD_SUCCESS;
1713 }
1714
1715 static int vty_write_config(struct vty *vty)
1716 {
1717 size_t i;
1718 struct cmd_node *node;
1719
1720 if (host.noconfig)
1721 return CMD_SUCCESS;
1722
1723 if (vty->type == VTY_TERM) {
1724 vty_out(vty, "\nCurrent configuration:\n");
1725 vty_out(vty, "!\n");
1726 }
1727
1728 vty_out(vty, "frr version %s\n", FRR_VER_SHORT);
1729 vty_out(vty, "frr defaults %s\n", DFLT_NAME);
1730 vty_out(vty, "!\n");
1731
1732 for (i = 0; i < vector_active(cmdvec); i++)
1733 if ((node = vector_slot(cmdvec, i)) && node->func
1734 && (node->vtysh || vty->type != VTY_SHELL)) {
1735 if ((*node->func)(vty))
1736 vty_out(vty, "!\n");
1737 }
1738
1739 if (vty->type == VTY_TERM) {
1740 vty_out(vty, "end\n");
1741 }
1742
1743 return CMD_SUCCESS;
1744 }
1745
1746 static int file_write_config(struct vty *vty)
1747 {
1748 int fd, dirfd;
1749 char *config_file, *slash;
1750 char *config_file_tmp = NULL;
1751 char *config_file_sav = NULL;
1752 int ret = CMD_WARNING;
1753 struct vty *file_vty;
1754 struct stat conf_stat;
1755
1756 if (host.noconfig)
1757 return CMD_SUCCESS;
1758
1759 /* Check and see if we are operating under vtysh configuration */
1760 if (host.config == NULL) {
1761 vty_out(vty,
1762 "Can't save to configuration file, using vtysh.\n");
1763 return CMD_WARNING;
1764 }
1765
1766 /* Get filename. */
1767 config_file = host.config;
1768
1769 #ifndef O_DIRECTORY
1770 #define O_DIRECTORY 0
1771 #endif
1772 slash = strrchr(config_file, '/');
1773 if (slash) {
1774 char *config_dir = XSTRDUP(MTYPE_TMP, config_file);
1775 config_dir[slash - config_file] = '\0';
1776 dirfd = open(config_dir, O_DIRECTORY | O_RDONLY);
1777 XFREE(MTYPE_TMP, config_dir);
1778 } else
1779 dirfd = open(".", O_DIRECTORY | O_RDONLY);
1780 /* if dirfd is invalid, directory sync fails, but we're still OK */
1781
1782 config_file_sav = XMALLOC(
1783 MTYPE_TMP, strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1);
1784 strcpy(config_file_sav, config_file);
1785 strcat(config_file_sav, CONF_BACKUP_EXT);
1786
1787
1788 config_file_tmp = XMALLOC(MTYPE_TMP, strlen(config_file) + 8);
1789 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
1790
1791 /* Open file to configuration write. */
1792 fd = mkstemp(config_file_tmp);
1793 if (fd < 0) {
1794 vty_out(vty, "Can't open configuration file %s.\n",
1795 config_file_tmp);
1796 goto finished;
1797 }
1798 if (fchmod(fd, CONFIGFILE_MASK) != 0) {
1799 vty_out(vty, "Can't chmod configuration file %s: %s (%d).\n",
1800 config_file_tmp, safe_strerror(errno), errno);
1801 goto finished;
1802 }
1803
1804 /* Make vty for configuration file. */
1805 file_vty = vty_new();
1806 file_vty->wfd = fd;
1807 file_vty->type = VTY_FILE;
1808
1809 /* Config file header print. */
1810 vty_out(file_vty, "!\n! Zebra configuration saved from vty\n! ");
1811 vty_time_print(file_vty, 1);
1812 vty_out(file_vty, "!\n");
1813 vty_write_config(file_vty);
1814 vty_close(file_vty);
1815
1816 if (stat(config_file, &conf_stat) >= 0) {
1817 if (unlink(config_file_sav) != 0)
1818 if (errno != ENOENT) {
1819 vty_out(vty,
1820 "Can't unlink backup configuration file %s.\n",
1821 config_file_sav);
1822 goto finished;
1823 }
1824 if (link(config_file, config_file_sav) != 0) {
1825 vty_out(vty,
1826 "Can't backup old configuration file %s.\n",
1827 config_file_sav);
1828 goto finished;
1829 }
1830 if (dirfd >= 0)
1831 fsync(dirfd);
1832 }
1833 if (rename(config_file_tmp, config_file) != 0) {
1834 vty_out(vty, "Can't save configuration file %s.\n",
1835 config_file);
1836 goto finished;
1837 }
1838 if (dirfd >= 0)
1839 fsync(dirfd);
1840
1841 vty_out(vty, "Configuration saved to %s\n", config_file);
1842 ret = CMD_SUCCESS;
1843
1844 finished:
1845 if (ret != CMD_SUCCESS)
1846 unlink(config_file_tmp);
1847 if (dirfd >= 0)
1848 close(dirfd);
1849 XFREE(MTYPE_TMP, config_file_tmp);
1850 XFREE(MTYPE_TMP, config_file_sav);
1851 return ret;
1852 }
1853
1854 /* Write current configuration into file. */
1855
1856 DEFUN (config_write,
1857 config_write_cmd,
1858 "write [<file|memory|terminal>]",
1859 "Write running configuration to memory, network, or terminal\n"
1860 "Write to configuration file\n"
1861 "Write configuration currently in memory\n"
1862 "Write configuration to terminal\n")
1863 {
1864 const int idx_type = 1;
1865
1866 // if command was 'write terminal' or 'write memory'
1867 if (argc == 2 && (!strcmp(argv[idx_type]->text, "terminal"))) {
1868 return vty_write_config(vty);
1869 }
1870
1871 return file_write_config(vty);
1872 }
1873
1874 /* ALIAS_FIXME for 'write <terminal|memory>' */
1875 DEFUN (show_running_config,
1876 show_running_config_cmd,
1877 "show running-config",
1878 SHOW_STR
1879 "running configuration (same as write terminal)\n")
1880 {
1881 return vty_write_config(vty);
1882 }
1883
1884 /* ALIAS_FIXME for 'write file' */
1885 DEFUN (copy_runningconf_startupconf,
1886 copy_runningconf_startupconf_cmd,
1887 "copy running-config startup-config",
1888 "Copy configuration\n"
1889 "Copy running config to... \n"
1890 "Copy running config to startup config (same as write file/memory)\n")
1891 {
1892 return file_write_config(vty);
1893 }
1894 /** -- **/
1895
1896 /* Write startup configuration into the terminal. */
1897 DEFUN (show_startup_config,
1898 show_startup_config_cmd,
1899 "show startup-config",
1900 SHOW_STR
1901 "Contents of startup configuration\n")
1902 {
1903 char buf[BUFSIZ];
1904 FILE *confp;
1905
1906 if (host.noconfig)
1907 return CMD_SUCCESS;
1908 if (host.config == NULL)
1909 return CMD_WARNING;
1910
1911 confp = fopen(host.config, "r");
1912 if (confp == NULL) {
1913 vty_out(vty, "Can't open configuration file [%s] due to '%s'\n",
1914 host.config, safe_strerror(errno));
1915 return CMD_WARNING;
1916 }
1917
1918 while (fgets(buf, BUFSIZ, confp)) {
1919 char *cp = buf;
1920
1921 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
1922 cp++;
1923 *cp = '\0';
1924
1925 vty_out(vty, "%s\n", buf);
1926 }
1927
1928 fclose(confp);
1929
1930 return CMD_SUCCESS;
1931 }
1932
1933 int cmd_domainname_set(const char *domainname)
1934 {
1935 XFREE(MTYPE_HOST, host.domainname);
1936 host.domainname = domainname ? XSTRDUP(MTYPE_HOST, domainname) : NULL;
1937 return CMD_SUCCESS;
1938 }
1939
1940 /* Hostname configuration */
1941 DEFUN(config_domainname,
1942 domainname_cmd,
1943 "domainname WORD",
1944 "Set system's domain name\n"
1945 "This system's domain name\n")
1946 {
1947 struct cmd_token *word = argv[1];
1948
1949 if (!isalpha((int)word->arg[0])) {
1950 vty_out(vty, "Please specify string starting with alphabet\n");
1951 return CMD_WARNING_CONFIG_FAILED;
1952 }
1953
1954 return cmd_domainname_set(word->arg);
1955 }
1956
1957 DEFUN(config_no_domainname,
1958 no_domainname_cmd,
1959 "no domainname [DOMAINNAME]",
1960 NO_STR
1961 "Reset system's domain name\n"
1962 "domain name of this router\n")
1963 {
1964 return cmd_domainname_set(NULL);
1965 }
1966
1967 int cmd_hostname_set(const char *hostname)
1968 {
1969 XFREE(MTYPE_HOST, host.name);
1970 host.name = hostname ? XSTRDUP(MTYPE_HOST, hostname) : NULL;
1971 return CMD_SUCCESS;
1972 }
1973
1974 /* Hostname configuration */
1975 DEFUN (config_hostname,
1976 hostname_cmd,
1977 "hostname WORD",
1978 "Set system's network name\n"
1979 "This system's network name\n")
1980 {
1981 struct cmd_token *word = argv[1];
1982
1983 if (!isalnum((int)word->arg[0])) {
1984 vty_out(vty, "Please specify string starting with alphabet\n");
1985 return CMD_WARNING_CONFIG_FAILED;
1986 }
1987
1988 return cmd_hostname_set(word->arg);
1989 }
1990
1991 DEFUN (config_no_hostname,
1992 no_hostname_cmd,
1993 "no hostname [HOSTNAME]",
1994 NO_STR
1995 "Reset system's network name\n"
1996 "Host name of this router\n")
1997 {
1998 return cmd_hostname_set(NULL);
1999 }
2000
2001 /* VTY interface password set. */
2002 DEFUN (config_password,
2003 password_cmd,
2004 "password [(8-8)] WORD",
2005 "Modify the terminal connection password\n"
2006 "Specifies a HIDDEN password will follow\n"
2007 "The password string\n")
2008 {
2009 int idx_8 = 1;
2010 int idx_word = 2;
2011 if (argc == 3) // '8' was specified
2012 {
2013 if (host.password)
2014 XFREE(MTYPE_HOST, host.password);
2015 host.password = NULL;
2016 if (host.password_encrypt)
2017 XFREE(MTYPE_HOST, host.password_encrypt);
2018 host.password_encrypt =
2019 XSTRDUP(MTYPE_HOST, argv[idx_word]->arg);
2020 return CMD_SUCCESS;
2021 }
2022
2023 if (!isalnum((int)argv[idx_8]->arg[0])) {
2024 vty_out(vty,
2025 "Please specify string starting with alphanumeric\n");
2026 return CMD_WARNING_CONFIG_FAILED;
2027 }
2028
2029 if (host.password)
2030 XFREE(MTYPE_HOST, host.password);
2031 host.password = NULL;
2032
2033 if (host.encrypt) {
2034 if (host.password_encrypt)
2035 XFREE(MTYPE_HOST, host.password_encrypt);
2036 host.password_encrypt =
2037 XSTRDUP(MTYPE_HOST, zencrypt(argv[idx_8]->arg));
2038 } else
2039 host.password = XSTRDUP(MTYPE_HOST, argv[idx_8]->arg);
2040
2041 return CMD_SUCCESS;
2042 }
2043
2044 /* VTY interface password delete. */
2045 DEFUN (no_config_password,
2046 no_password_cmd,
2047 "no password",
2048 NO_STR
2049 "Modify the terminal connection password\n")
2050 {
2051 bool warned = false;
2052
2053 if (host.password) {
2054 if (!vty_shell_serv(vty)) {
2055 vty_out(vty, NO_PASSWD_CMD_WARNING);
2056 warned = true;
2057 }
2058 XFREE(MTYPE_HOST, host.password);
2059 }
2060 host.password = NULL;
2061
2062 if (host.password_encrypt) {
2063 if (!warned && !vty_shell_serv(vty))
2064 vty_out(vty, NO_PASSWD_CMD_WARNING);
2065 XFREE(MTYPE_HOST, host.password_encrypt);
2066 }
2067 host.password_encrypt = NULL;
2068
2069 return CMD_SUCCESS;
2070 }
2071
2072 /* VTY enable password set. */
2073 DEFUN (config_enable_password,
2074 enable_password_cmd,
2075 "enable password [(8-8)] WORD",
2076 "Modify enable password parameters\n"
2077 "Assign the privileged level password\n"
2078 "Specifies a HIDDEN password will follow\n"
2079 "The HIDDEN 'enable' password string\n")
2080 {
2081 int idx_8 = 2;
2082 int idx_word = 3;
2083
2084 /* Crypt type is specified. */
2085 if (argc == 4) {
2086 if (argv[idx_8]->arg[0] == '8') {
2087 if (host.enable)
2088 XFREE(MTYPE_HOST, host.enable);
2089 host.enable = NULL;
2090
2091 if (host.enable_encrypt)
2092 XFREE(MTYPE_HOST, host.enable_encrypt);
2093 host.enable_encrypt =
2094 XSTRDUP(MTYPE_HOST, argv[idx_word]->arg);
2095
2096 return CMD_SUCCESS;
2097 } else {
2098 vty_out(vty, "Unknown encryption type.\n");
2099 return CMD_WARNING_CONFIG_FAILED;
2100 }
2101 }
2102
2103 if (!isalnum((int)argv[idx_8]->arg[0])) {
2104 vty_out(vty,
2105 "Please specify string starting with alphanumeric\n");
2106 return CMD_WARNING_CONFIG_FAILED;
2107 }
2108
2109 if (host.enable)
2110 XFREE(MTYPE_HOST, host.enable);
2111 host.enable = NULL;
2112
2113 /* Plain password input. */
2114 if (host.encrypt) {
2115 if (host.enable_encrypt)
2116 XFREE(MTYPE_HOST, host.enable_encrypt);
2117 host.enable_encrypt =
2118 XSTRDUP(MTYPE_HOST, zencrypt(argv[idx_8]->arg));
2119 } else
2120 host.enable = XSTRDUP(MTYPE_HOST, argv[idx_8]->arg);
2121
2122 return CMD_SUCCESS;
2123 }
2124
2125 /* VTY enable password delete. */
2126 DEFUN (no_config_enable_password,
2127 no_enable_password_cmd,
2128 "no enable password",
2129 NO_STR
2130 "Modify enable password parameters\n"
2131 "Assign the privileged level password\n")
2132 {
2133 bool warned = false;
2134
2135 if (host.enable) {
2136 if (!vty_shell_serv(vty)) {
2137 vty_out(vty, NO_PASSWD_CMD_WARNING);
2138 warned = true;
2139 }
2140 XFREE(MTYPE_HOST, host.enable);
2141 }
2142 host.enable = NULL;
2143
2144 if (host.enable_encrypt) {
2145 if (!warned && !vty_shell_serv(vty))
2146 vty_out(vty, NO_PASSWD_CMD_WARNING);
2147 XFREE(MTYPE_HOST, host.enable_encrypt);
2148 }
2149 host.enable_encrypt = NULL;
2150
2151 return CMD_SUCCESS;
2152 }
2153
2154 DEFUN (service_password_encrypt,
2155 service_password_encrypt_cmd,
2156 "service password-encryption",
2157 "Set up miscellaneous service\n"
2158 "Enable encrypted passwords\n")
2159 {
2160 if (host.encrypt)
2161 return CMD_SUCCESS;
2162
2163 host.encrypt = 1;
2164
2165 if (host.password) {
2166 if (host.password_encrypt)
2167 XFREE(MTYPE_HOST, host.password_encrypt);
2168 host.password_encrypt =
2169 XSTRDUP(MTYPE_HOST, zencrypt(host.password));
2170 }
2171 if (host.enable) {
2172 if (host.enable_encrypt)
2173 XFREE(MTYPE_HOST, host.enable_encrypt);
2174 host.enable_encrypt =
2175 XSTRDUP(MTYPE_HOST, zencrypt(host.enable));
2176 }
2177
2178 return CMD_SUCCESS;
2179 }
2180
2181 DEFUN (no_service_password_encrypt,
2182 no_service_password_encrypt_cmd,
2183 "no service password-encryption",
2184 NO_STR
2185 "Set up miscellaneous service\n"
2186 "Enable encrypted passwords\n")
2187 {
2188 if (!host.encrypt)
2189 return CMD_SUCCESS;
2190
2191 host.encrypt = 0;
2192
2193 if (host.password_encrypt)
2194 XFREE(MTYPE_HOST, host.password_encrypt);
2195 host.password_encrypt = NULL;
2196
2197 if (host.enable_encrypt)
2198 XFREE(MTYPE_HOST, host.enable_encrypt);
2199 host.enable_encrypt = NULL;
2200
2201 return CMD_SUCCESS;
2202 }
2203
2204 DEFUN (config_terminal_length,
2205 config_terminal_length_cmd,
2206 "terminal length (0-512)",
2207 "Set terminal line parameters\n"
2208 "Set number of lines on a screen\n"
2209 "Number of lines on screen (0 for no pausing)\n")
2210 {
2211 int idx_number = 2;
2212
2213 vty->lines = atoi(argv[idx_number]->arg);
2214
2215 return CMD_SUCCESS;
2216 }
2217
2218 DEFUN (config_terminal_no_length,
2219 config_terminal_no_length_cmd,
2220 "terminal no length",
2221 "Set terminal line parameters\n"
2222 NO_STR
2223 "Set number of lines on a screen\n")
2224 {
2225 vty->lines = -1;
2226 return CMD_SUCCESS;
2227 }
2228
2229 DEFUN (service_terminal_length,
2230 service_terminal_length_cmd,
2231 "service terminal-length (0-512)",
2232 "Set up miscellaneous service\n"
2233 "System wide terminal length configuration\n"
2234 "Number of lines of VTY (0 means no line control)\n")
2235 {
2236 int idx_number = 2;
2237
2238 host.lines = atoi(argv[idx_number]->arg);
2239
2240 return CMD_SUCCESS;
2241 }
2242
2243 DEFUN (no_service_terminal_length,
2244 no_service_terminal_length_cmd,
2245 "no service terminal-length [(0-512)]",
2246 NO_STR
2247 "Set up miscellaneous service\n"
2248 "System wide terminal length configuration\n"
2249 "Number of lines of VTY (0 means no line control)\n")
2250 {
2251 host.lines = -1;
2252 return CMD_SUCCESS;
2253 }
2254
2255 DEFUN_HIDDEN (do_echo,
2256 echo_cmd,
2257 "echo MESSAGE...",
2258 "Echo a message back to the vty\n"
2259 "The message to echo\n")
2260 {
2261 char *message;
2262
2263 vty_out(vty, "%s\n",
2264 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2265 if (message)
2266 XFREE(MTYPE_TMP, message);
2267 return CMD_SUCCESS;
2268 }
2269
2270 DEFUN (config_logmsg,
2271 config_logmsg_cmd,
2272 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
2273 "Send a message to enabled logging destinations\n"
2274 LOG_LEVEL_DESC
2275 "The message to send\n")
2276 {
2277 int idx_log_level = 1;
2278 int idx_message = 2;
2279 int level;
2280 char *message;
2281
2282 if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)
2283 return CMD_ERR_NO_MATCH;
2284
2285 zlog(level, "%s",
2286 ((message = argv_concat(argv, argc, idx_message)) ? message : ""));
2287 if (message)
2288 XFREE(MTYPE_TMP, message);
2289
2290 return CMD_SUCCESS;
2291 }
2292
2293 DEFUN (show_logging,
2294 show_logging_cmd,
2295 "show logging",
2296 SHOW_STR
2297 "Show current logging configuration\n")
2298 {
2299 struct zlog *zl = zlog_default;
2300
2301 vty_out(vty, "Syslog logging: ");
2302 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2303 vty_out(vty, "disabled");
2304 else
2305 vty_out(vty, "level %s, facility %s, ident %s",
2306 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2307 facility_name(zl->facility), zl->ident);
2308 vty_out(vty, "\n");
2309
2310 vty_out(vty, "Stdout logging: ");
2311 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2312 vty_out(vty, "disabled");
2313 else
2314 vty_out(vty, "level %s",
2315 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2316 vty_out(vty, "\n");
2317
2318 vty_out(vty, "Monitor logging: ");
2319 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2320 vty_out(vty, "disabled");
2321 else
2322 vty_out(vty, "level %s",
2323 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2324 vty_out(vty, "\n");
2325
2326 vty_out(vty, "File logging: ");
2327 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2328 vty_out(vty, "disabled");
2329 else
2330 vty_out(vty, "level %s, filename %s",
2331 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2332 zl->filename);
2333 vty_out(vty, "\n");
2334
2335 vty_out(vty, "Protocol name: %s\n", zl->protoname);
2336 vty_out(vty, "Record priority: %s\n",
2337 (zl->record_priority ? "enabled" : "disabled"));
2338 vty_out(vty, "Timestamp precision: %d\n", zl->timestamp_precision);
2339
2340 return CMD_SUCCESS;
2341 }
2342
2343 DEFUN (config_log_stdout,
2344 config_log_stdout_cmd,
2345 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2346 "Logging control\n"
2347 "Set stdout logging level\n"
2348 LOG_LEVEL_DESC)
2349 {
2350 int idx_log_level = 2;
2351
2352 if (argc == idx_log_level) {
2353 zlog_set_level(ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2354 return CMD_SUCCESS;
2355 }
2356 int level;
2357
2358 if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)
2359 return CMD_ERR_NO_MATCH;
2360 zlog_set_level(ZLOG_DEST_STDOUT, level);
2361 return CMD_SUCCESS;
2362 }
2363
2364 DEFUN (no_config_log_stdout,
2365 no_config_log_stdout_cmd,
2366 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2367 NO_STR
2368 "Logging control\n"
2369 "Cancel logging to stdout\n"
2370 LOG_LEVEL_DESC)
2371 {
2372 zlog_set_level(ZLOG_DEST_STDOUT, ZLOG_DISABLED);
2373 return CMD_SUCCESS;
2374 }
2375
2376 DEFUN (config_log_monitor,
2377 config_log_monitor_cmd,
2378 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2379 "Logging control\n"
2380 "Set terminal line (monitor) logging level\n"
2381 LOG_LEVEL_DESC)
2382 {
2383 int idx_log_level = 2;
2384
2385 if (argc == idx_log_level) {
2386 zlog_set_level(ZLOG_DEST_MONITOR, zlog_default->default_lvl);
2387 return CMD_SUCCESS;
2388 }
2389 int level;
2390
2391 if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)
2392 return CMD_ERR_NO_MATCH;
2393 zlog_set_level(ZLOG_DEST_MONITOR, level);
2394 return CMD_SUCCESS;
2395 }
2396
2397 DEFUN (no_config_log_monitor,
2398 no_config_log_monitor_cmd,
2399 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2400 NO_STR
2401 "Logging control\n"
2402 "Disable terminal line (monitor) logging\n"
2403 LOG_LEVEL_DESC)
2404 {
2405 zlog_set_level(ZLOG_DEST_MONITOR, ZLOG_DISABLED);
2406 return CMD_SUCCESS;
2407 }
2408
2409 static int set_log_file(struct vty *vty, const char *fname, int loglevel)
2410 {
2411 int ret;
2412 char *p = NULL;
2413 const char *fullpath;
2414
2415 /* Path detection. */
2416 if (!IS_DIRECTORY_SEP(*fname)) {
2417 char cwd[MAXPATHLEN + 1];
2418 cwd[MAXPATHLEN] = '\0';
2419
2420 if (getcwd(cwd, MAXPATHLEN) == NULL) {
2421 flog_err_sys(LIB_ERR_SYSTEM_CALL,
2422 "config_log_file: Unable to alloc mem!");
2423 return CMD_WARNING_CONFIG_FAILED;
2424 }
2425
2426 p = XMALLOC(MTYPE_TMP, strlen(cwd) + strlen(fname) + 2);
2427 sprintf(p, "%s/%s", cwd, fname);
2428 fullpath = p;
2429 } else
2430 fullpath = fname;
2431
2432 ret = zlog_set_file(fullpath, loglevel);
2433
2434 if (p)
2435 XFREE(MTYPE_TMP, p);
2436
2437 if (!ret) {
2438 if (vty)
2439 vty_out(vty, "can't open logfile %s\n", fname);
2440 return CMD_WARNING_CONFIG_FAILED;
2441 }
2442
2443 if (host.logfile)
2444 XFREE(MTYPE_HOST, host.logfile);
2445
2446 host.logfile = XSTRDUP(MTYPE_HOST, fname);
2447
2448 #if defined(HAVE_CUMULUS)
2449 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED)
2450 zlog_set_level(ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
2451 #endif
2452 return CMD_SUCCESS;
2453 }
2454
2455 void command_setup_early_logging(const char *dest, const char *level)
2456 {
2457 char *token;
2458
2459 if (level) {
2460 int nlevel = level_match(level);
2461
2462 if (nlevel != ZLOG_DISABLED)
2463 zlog_default->default_lvl = nlevel;
2464 }
2465
2466 if (!dest)
2467 return;
2468
2469 if (strcmp(dest, "stdout") == 0) {
2470 zlog_set_level(ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2471 return;
2472 }
2473
2474 if (strcmp(dest, "syslog") == 0) {
2475 zlog_set_level(ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2476 return;
2477 }
2478
2479 token = strstr(dest, ":");
2480 if (token == NULL)
2481 return;
2482
2483 token++;
2484
2485 set_log_file(NULL, token, zlog_default->default_lvl);
2486 }
2487
2488 DEFUN (config_log_file,
2489 config_log_file_cmd,
2490 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2491 "Logging control\n"
2492 "Logging to file\n"
2493 "Logging filename\n"
2494 LOG_LEVEL_DESC)
2495 {
2496 int idx_filename = 2;
2497 int idx_log_levels = 3;
2498 if (argc == 4) {
2499 int level;
2500 if ((level = level_match(argv[idx_log_levels]->arg))
2501 == ZLOG_DISABLED)
2502 return CMD_ERR_NO_MATCH;
2503 return set_log_file(vty, argv[idx_filename]->arg, level);
2504 } else
2505 return set_log_file(vty, argv[idx_filename]->arg,
2506 zlog_default->default_lvl);
2507 }
2508
2509 static void disable_log_file(void)
2510 {
2511 zlog_reset_file();
2512
2513 if (host.logfile)
2514 XFREE(MTYPE_HOST, host.logfile);
2515
2516 host.logfile = NULL;
2517 }
2518
2519 DEFUN (no_config_log_file,
2520 no_config_log_file_cmd,
2521 "no log file [FILENAME [LEVEL]]",
2522 NO_STR
2523 "Logging control\n"
2524 "Cancel logging to file\n"
2525 "Logging file name\n"
2526 "Logging level\n")
2527 {
2528 disable_log_file();
2529 return CMD_SUCCESS;
2530 }
2531
2532 DEFUN (config_log_syslog,
2533 config_log_syslog_cmd,
2534 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2535 "Logging control\n"
2536 "Set syslog logging level\n"
2537 LOG_LEVEL_DESC)
2538 {
2539 int idx_log_levels = 2;
2540
2541 disable_log_file();
2542
2543 if (argc == 3) {
2544 int level;
2545 if ((level = level_match(argv[idx_log_levels]->arg))
2546 == ZLOG_DISABLED)
2547 return CMD_ERR_NO_MATCH;
2548 zlog_set_level(ZLOG_DEST_SYSLOG, level);
2549 return CMD_SUCCESS;
2550 } else {
2551 zlog_set_level(ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2552 return CMD_SUCCESS;
2553 }
2554 }
2555
2556 DEFUN (no_config_log_syslog,
2557 no_config_log_syslog_cmd,
2558 "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>]",
2559 NO_STR
2560 "Logging control\n"
2561 "Cancel logging to syslog\n"
2562 LOG_FACILITY_DESC
2563 LOG_LEVEL_DESC)
2564 {
2565 zlog_set_level(ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
2566 return CMD_SUCCESS;
2567 }
2568
2569 DEFUN (config_log_facility,
2570 config_log_facility_cmd,
2571 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2572 "Logging control\n"
2573 "Facility parameter for syslog messages\n"
2574 LOG_FACILITY_DESC)
2575 {
2576 int idx_target = 2;
2577 int facility = facility_match(argv[idx_target]->arg);
2578
2579 zlog_default->facility = facility;
2580 return CMD_SUCCESS;
2581 }
2582
2583 DEFUN (no_config_log_facility,
2584 no_config_log_facility_cmd,
2585 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2586 NO_STR
2587 "Logging control\n"
2588 "Reset syslog facility to default (daemon)\n"
2589 LOG_FACILITY_DESC)
2590 {
2591 zlog_default->facility = LOG_DAEMON;
2592 return CMD_SUCCESS;
2593 }
2594
2595 DEFUN (config_log_record_priority,
2596 config_log_record_priority_cmd,
2597 "log record-priority",
2598 "Logging control\n"
2599 "Log the priority of the message within the message\n")
2600 {
2601 zlog_default->record_priority = 1;
2602 return CMD_SUCCESS;
2603 }
2604
2605 DEFUN (no_config_log_record_priority,
2606 no_config_log_record_priority_cmd,
2607 "no log record-priority",
2608 NO_STR
2609 "Logging control\n"
2610 "Do not log the priority of the message within the message\n")
2611 {
2612 zlog_default->record_priority = 0;
2613 return CMD_SUCCESS;
2614 }
2615
2616 DEFUN (config_log_timestamp_precision,
2617 config_log_timestamp_precision_cmd,
2618 "log timestamp precision (0-6)",
2619 "Logging control\n"
2620 "Timestamp configuration\n"
2621 "Set the timestamp precision\n"
2622 "Number of subsecond digits\n")
2623 {
2624 int idx_number = 3;
2625 zlog_default->timestamp_precision =
2626 strtoul(argv[idx_number]->arg, NULL, 10);
2627 return CMD_SUCCESS;
2628 }
2629
2630 DEFUN (no_config_log_timestamp_precision,
2631 no_config_log_timestamp_precision_cmd,
2632 "no log timestamp precision",
2633 NO_STR
2634 "Logging control\n"
2635 "Timestamp configuration\n"
2636 "Reset the timestamp precision to the default value of 0\n")
2637 {
2638 zlog_default->timestamp_precision = 0;
2639 return CMD_SUCCESS;
2640 }
2641
2642 DEFUN (debug_memstats,
2643 debug_memstats_cmd,
2644 "[no] debug memstats-at-exit",
2645 NO_STR
2646 DEBUG_STR
2647 "Print memory type statistics at exit\n")
2648 {
2649 debug_memstats_at_exit = !!strcmp(argv[0]->text, "no");
2650 return CMD_SUCCESS;
2651 }
2652
2653 int cmd_banner_motd_file(const char *file)
2654 {
2655 int success = CMD_SUCCESS;
2656 char p[PATH_MAX];
2657 char *rpath;
2658 char *in;
2659
2660 rpath = realpath(file, p);
2661 if (!rpath)
2662 return CMD_ERR_NO_FILE;
2663 in = strstr(rpath, SYSCONFDIR);
2664 if (in == rpath) {
2665 if (host.motdfile)
2666 XFREE(MTYPE_HOST, host.motdfile);
2667 host.motdfile = XSTRDUP(MTYPE_HOST, file);
2668 } else
2669 success = CMD_WARNING_CONFIG_FAILED;
2670
2671 return success;
2672 }
2673
2674 DEFUN (banner_motd_file,
2675 banner_motd_file_cmd,
2676 "banner motd file FILE",
2677 "Set banner\n"
2678 "Banner for motd\n"
2679 "Banner from a file\n"
2680 "Filename\n")
2681 {
2682 int idx_file = 3;
2683 const char *filename = argv[idx_file]->arg;
2684 int cmd = cmd_banner_motd_file(filename);
2685
2686 if (cmd == CMD_ERR_NO_FILE)
2687 vty_out(vty, "%s does not exist", filename);
2688 else if (cmd == CMD_WARNING_CONFIG_FAILED)
2689 vty_out(vty, "%s must be in %s", filename, SYSCONFDIR);
2690
2691 return cmd;
2692 }
2693
2694 DEFUN (banner_motd_default,
2695 banner_motd_default_cmd,
2696 "banner motd default",
2697 "Set banner string\n"
2698 "Strings for motd\n"
2699 "Default string\n")
2700 {
2701 host.motd = default_motd;
2702 return CMD_SUCCESS;
2703 }
2704
2705 DEFUN (no_banner_motd,
2706 no_banner_motd_cmd,
2707 "no banner motd",
2708 NO_STR
2709 "Set banner string\n"
2710 "Strings for motd\n")
2711 {
2712 host.motd = NULL;
2713 if (host.motdfile)
2714 XFREE(MTYPE_HOST, host.motdfile);
2715 host.motdfile = NULL;
2716 return CMD_SUCCESS;
2717 }
2718
2719 DEFUN(find,
2720 find_cmd,
2721 "find COMMAND...",
2722 "Find CLI command containing text\n"
2723 "Text to search for\n")
2724 {
2725 char *text = argv_concat(argv, argc, 1);
2726 const struct cmd_node *node;
2727 const struct cmd_element *cli;
2728 vector clis;
2729
2730 for (unsigned int i = 0; i < vector_active(cmdvec); i++) {
2731 node = vector_slot(cmdvec, i);
2732 if (!node)
2733 continue;
2734 clis = node->cmd_vector;
2735 for (unsigned int j = 0; j < vector_active(clis); j++) {
2736 cli = vector_slot(clis, j);
2737 if (strcasestr(cli->string, text))
2738 vty_out(vty, " (%s) %s\n",
2739 node_names[node->node], cli->string);
2740 }
2741 }
2742
2743 XFREE(MTYPE_TMP, text);
2744
2745 return CMD_SUCCESS;
2746 }
2747
2748 /* Set config filename. Called from vty.c */
2749 void host_config_set(const char *filename)
2750 {
2751 if (host.config)
2752 XFREE(MTYPE_HOST, host.config);
2753 host.config = XSTRDUP(MTYPE_HOST, filename);
2754 }
2755
2756 const char *host_config_get(void)
2757 {
2758 return host.config;
2759 }
2760
2761 void install_default(enum node_type node)
2762 {
2763 install_element(node, &config_exit_cmd);
2764 install_element(node, &config_quit_cmd);
2765 install_element(node, &config_end_cmd);
2766 install_element(node, &config_help_cmd);
2767 install_element(node, &config_list_cmd);
2768 install_element(node, &show_cli_graph_cmd);
2769 install_element(node, &find_cmd);
2770
2771 install_element(node, &config_write_cmd);
2772 install_element(node, &show_running_config_cmd);
2773
2774 install_element(node, &autocomplete_cmd);
2775 }
2776
2777 /* Initialize command interface. Install basic nodes and commands.
2778 *
2779 * terminal = 0 -- vtysh / no logging, no config control
2780 * terminal = 1 -- normal daemon
2781 * terminal = -1 -- watchfrr / no logging, but minimal config control */
2782 void cmd_init(int terminal)
2783 {
2784 struct utsname names;
2785
2786 if (array_size(node_names) != NODE_TYPE_MAX)
2787 assert(!"Update the CLI node description array!");
2788
2789 uname(&names);
2790 qobj_init();
2791
2792 /* register command preprocessors */
2793 hook_register(cmd_execute, handle_pipe_action);
2794 hook_register(cmd_execute_done, handle_pipe_action_done);
2795
2796 varhandlers = list_new();
2797
2798 /* Allocate initial top vector of commands. */
2799 cmdvec = vector_init(VECTOR_MIN_SIZE);
2800
2801 /* Default host value settings. */
2802 host.name = XSTRDUP(MTYPE_HOST, names.nodename);
2803 #ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
2804 if ((strcmp(names.domainname, "(none)") == 0))
2805 host.domainname = NULL;
2806 else
2807 host.domainname = XSTRDUP(MTYPE_HOST, names.domainname);
2808 #else
2809 host.domainname = NULL;
2810 #endif
2811 host.password = NULL;
2812 host.enable = NULL;
2813 host.logfile = NULL;
2814 host.config = NULL;
2815 host.noconfig = (terminal < 0);
2816 host.lines = -1;
2817 host.motd = default_motd;
2818 host.motdfile = NULL;
2819
2820 /* Install top nodes. */
2821 install_node(&view_node, NULL);
2822 install_node(&enable_node, NULL);
2823 install_node(&auth_node, NULL);
2824 install_node(&auth_enable_node, NULL);
2825 install_node(&config_node, config_write_host);
2826
2827 /* Each node's basic commands. */
2828 install_element(VIEW_NODE, &show_version_cmd);
2829 install_element(ENABLE_NODE, &show_startup_config_cmd);
2830 install_element(ENABLE_NODE, &debug_memstats_cmd);
2831
2832 if (terminal) {
2833 install_element(VIEW_NODE, &config_list_cmd);
2834 install_element(VIEW_NODE, &config_exit_cmd);
2835 install_element(VIEW_NODE, &config_quit_cmd);
2836 install_element(VIEW_NODE, &config_help_cmd);
2837 install_element(VIEW_NODE, &config_enable_cmd);
2838 install_element(VIEW_NODE, &config_terminal_length_cmd);
2839 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
2840 install_element(VIEW_NODE, &show_logging_cmd);
2841 install_element(VIEW_NODE, &show_commandtree_cmd);
2842 install_element(VIEW_NODE, &echo_cmd);
2843 install_element(VIEW_NODE, &autocomplete_cmd);
2844 install_element(VIEW_NODE, &find_cmd);
2845
2846 install_element(ENABLE_NODE, &config_end_cmd);
2847 install_element(ENABLE_NODE, &config_disable_cmd);
2848 install_element(ENABLE_NODE, &config_terminal_cmd);
2849 install_element(ENABLE_NODE, &copy_runningconf_startupconf_cmd);
2850 install_element(ENABLE_NODE, &config_write_cmd);
2851 install_element(ENABLE_NODE, &show_running_config_cmd);
2852 install_element(ENABLE_NODE, &config_logmsg_cmd);
2853
2854 install_default(CONFIG_NODE);
2855
2856 thread_cmd_init();
2857 workqueue_cmd_init();
2858 hash_cmd_init();
2859 }
2860
2861 install_element(CONFIG_NODE, &hostname_cmd);
2862 install_element(CONFIG_NODE, &no_hostname_cmd);
2863 install_element(CONFIG_NODE, &domainname_cmd);
2864 install_element(CONFIG_NODE, &no_domainname_cmd);
2865 install_element(CONFIG_NODE, &frr_version_defaults_cmd);
2866 install_element(CONFIG_NODE, &debug_memstats_cmd);
2867
2868 if (terminal > 0) {
2869 install_element(CONFIG_NODE, &password_cmd);
2870 install_element(CONFIG_NODE, &no_password_cmd);
2871 install_element(CONFIG_NODE, &enable_password_cmd);
2872 install_element(CONFIG_NODE, &no_enable_password_cmd);
2873
2874 install_element(CONFIG_NODE, &config_log_stdout_cmd);
2875 install_element(CONFIG_NODE, &no_config_log_stdout_cmd);
2876 install_element(CONFIG_NODE, &config_log_monitor_cmd);
2877 install_element(CONFIG_NODE, &no_config_log_monitor_cmd);
2878 install_element(CONFIG_NODE, &config_log_file_cmd);
2879 install_element(CONFIG_NODE, &no_config_log_file_cmd);
2880 install_element(CONFIG_NODE, &config_log_syslog_cmd);
2881 install_element(CONFIG_NODE, &no_config_log_syslog_cmd);
2882 install_element(CONFIG_NODE, &config_log_facility_cmd);
2883 install_element(CONFIG_NODE, &no_config_log_facility_cmd);
2884 install_element(CONFIG_NODE, &config_log_record_priority_cmd);
2885 install_element(CONFIG_NODE,
2886 &no_config_log_record_priority_cmd);
2887 install_element(CONFIG_NODE,
2888 &config_log_timestamp_precision_cmd);
2889 install_element(CONFIG_NODE,
2890 &no_config_log_timestamp_precision_cmd);
2891 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
2892 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
2893 install_element(CONFIG_NODE, &banner_motd_default_cmd);
2894 install_element(CONFIG_NODE, &banner_motd_file_cmd);
2895 install_element(CONFIG_NODE, &no_banner_motd_cmd);
2896 install_element(CONFIG_NODE, &service_terminal_length_cmd);
2897 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
2898
2899 vrf_install_commands();
2900 }
2901
2902 #ifdef DEV_BUILD
2903 grammar_sandbox_init();
2904 #endif
2905 }
2906
2907 void cmd_terminate()
2908 {
2909 struct cmd_node *cmd_node;
2910
2911 hook_unregister(cmd_execute, handle_pipe_action);
2912 hook_unregister(cmd_execute_done, handle_pipe_action_done);
2913
2914 if (cmdvec) {
2915 for (unsigned int i = 0; i < vector_active(cmdvec); i++)
2916 if ((cmd_node = vector_slot(cmdvec, i)) != NULL) {
2917 // deleting the graph delets the cmd_element as
2918 // well
2919 graph_delete_graph(cmd_node->cmdgraph);
2920 vector_free(cmd_node->cmd_vector);
2921 hash_clean(cmd_node->cmd_hash, NULL);
2922 hash_free(cmd_node->cmd_hash);
2923 cmd_node->cmd_hash = NULL;
2924 }
2925
2926 vector_free(cmdvec);
2927 cmdvec = NULL;
2928 }
2929
2930 if (host.name)
2931 XFREE(MTYPE_HOST, host.name);
2932 if (host.domainname)
2933 XFREE(MTYPE_HOST, host.domainname);
2934 if (host.password)
2935 XFREE(MTYPE_HOST, host.password);
2936 if (host.password_encrypt)
2937 XFREE(MTYPE_HOST, host.password_encrypt);
2938 if (host.enable)
2939 XFREE(MTYPE_HOST, host.enable);
2940 if (host.enable_encrypt)
2941 XFREE(MTYPE_HOST, host.enable_encrypt);
2942 if (host.logfile)
2943 XFREE(MTYPE_HOST, host.logfile);
2944 if (host.motdfile)
2945 XFREE(MTYPE_HOST, host.motdfile);
2946 if (host.config)
2947 XFREE(MTYPE_HOST, host.config);
2948
2949 list_delete_and_null(&varhandlers);
2950 qobj_finish();
2951 }