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