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