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