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