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