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