]> git.proxmox.com Git - mirror_frr.git/blame - lib/command.c
lib: STAILQ_FOREACH_SAFE never gives a null elem
[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. */
c139972c 1012static int cmd_execute_command_real(vector vline, enum cmd_filter_type filter,
d62a17ae 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;
a6233bfc
RW
1054 else {
1055 /* Clear enqueued configuration changes. */
1056 vty->num_cfg_changes = 0;
1057 memset(&vty->cfg_changes, 0, sizeof(vty->cfg_changes));
1058
d62a17ae 1059 ret = matched_element->func(matched_element, vty, argc, argv);
a6233bfc 1060 }
d62a17ae 1061
1062 // delete list and cmd_token's in it
6a154c88 1063 list_delete(&argv_list);
d62a17ae 1064 XFREE(MTYPE_TMP, argv);
1065
1066 return ret;
718e3744 1067}
1068
cd40b329
CF
1069/**
1070 * Execute a given command, handling things like "do ..." and checking
1071 * whether the given command might apply at a parent node if doesn't
1072 * apply for the current node.
1073 *
1074 * @param vline Command line input, vector of char* where each element is
1075 * one input token.
1076 * @param vty The vty context in which the command should be executed.
1077 * @param cmd Pointer where the struct cmd_element of the matched command
1078 * will be stored, if any. May be set to NULL if this info is
1079 * not needed.
1080 * @param vtysh If set != 0, don't lookup the command at parent nodes.
1081 * @return The status of the command that has been executed or an error code
1082 * as to why no command could be executed.
1083 */
d62a17ae 1084int cmd_execute_command(vector vline, struct vty *vty,
1085 const struct cmd_element **cmd, int vtysh)
17aca20b 1086{
d62a17ae 1087 int ret, saved_ret = 0;
1088 enum node_type onode, try_node;
1c2facd1 1089 int orig_xpath_index;
eda031f6 1090
d62a17ae 1091 onode = try_node = vty->node;
1c2facd1 1092 orig_xpath_index = vty->xpath_index;
b92938a7 1093
d62a17ae 1094 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1095 vector shifted_vline;
1096 unsigned int index;
b92938a7 1097
d62a17ae 1098 vty->node = ENABLE_NODE;
1c2facd1 1099 vty->xpath_index = 0;
d62a17ae 1100 /* We can try it on enable node, cos' the vty is authenticated
1101 */
b92938a7 1102
d62a17ae 1103 shifted_vline = vector_init(vector_count(vline));
1104 /* use memcpy? */
1105 for (index = 1; index < vector_active(vline); index++)
1106 vector_set_index(shifted_vline, index - 1,
1107 vector_lookup(vline, index));
b92938a7 1108
d62a17ae 1109 ret = cmd_execute_command_real(shifted_vline, FILTER_RELAXED,
1110 vty, cmd);
b92938a7 1111
d62a17ae 1112 vector_free(shifted_vline);
1113 vty->node = onode;
1c2facd1 1114 vty->xpath_index = orig_xpath_index;
d62a17ae 1115 return ret;
1116 }
b92938a7 1117
d62a17ae 1118 saved_ret = ret =
1119 cmd_execute_command_real(vline, FILTER_RELAXED, vty, cmd);
b92938a7 1120
d62a17ae 1121 if (vtysh)
1122 return saved_ret;
87d683b0 1123
825d5fbf
CS
1124 if (ret != CMD_SUCCESS && ret != CMD_WARNING
1125 && ret != CMD_NOT_MY_INSTANCE && ret != CMD_WARNING_CONFIG_FAILED) {
d62a17ae 1126 /* This assumes all nodes above CONFIG_NODE are childs of
1127 * CONFIG_NODE */
1128 while (vty->node > CONFIG_NODE) {
1129 try_node = node_parent(try_node);
1130 vty->node = try_node;
1c2facd1
RW
1131 if (vty->xpath_index > 0)
1132 vty->xpath_index--;
d62a17ae 1133 ret = cmd_execute_command_real(vline, FILTER_RELAXED,
1134 vty, cmd);
825d5fbf
CS
1135 if (ret == CMD_SUCCESS || ret == CMD_WARNING
1136 || ret == CMD_NOT_MY_INSTANCE
1137 || ret == CMD_WARNING_CONFIG_FAILED)
d62a17ae 1138 return ret;
1139 }
1140 /* no command succeeded, reset the vty to the original node */
1141 vty->node = onode;
1c2facd1 1142 vty->xpath_index = orig_xpath_index;
d62a17ae 1143 }
04e64062 1144
d62a17ae 1145 /* return command status for original node */
1146 return saved_ret;
b92938a7 1147}
1148
cd40b329
CF
1149/**
1150 * Execute a given command, matching it strictly against the current node.
1151 * This mode is used when reading config files.
1152 *
1153 * @param vline Command line input, vector of char* where each element is
1154 * one input token.
1155 * @param vty The vty context in which the command should be executed.
1156 * @param cmd Pointer where the struct cmd_element* of the matched command
1157 * will be stored, if any. May be set to NULL if this info is
1158 * not needed.
1159 * @return The status of the command that has been executed or an error code
1160 * as to why no command could be executed.
1161 */
d62a17ae 1162int cmd_execute_command_strict(vector vline, struct vty *vty,
1163 const struct cmd_element **cmd)
718e3744 1164{
d62a17ae 1165 return cmd_execute_command_real(vline, FILTER_STRICT, vty, cmd);
718e3744 1166}
1167
01e24c4a
QY
1168/*
1169 * Hook for preprocessing command string before executing.
1170 *
1171 * All subscribers are called with the raw command string that is to be
1172 * executed. If any changes are to be made, a new string should be allocated
1173 * with MTYPE_TMP and *cmd_out updated to point to this new string. The caller
1174 * is then responsible for freeing this string.
1175 *
1176 * All processing functions must be mutually exclusive in their action, i.e. if
1177 * one subscriber decides to modify the command, all others must not modify it
1178 * when called. Feeding the output of one processing command into a subsequent
1179 * one is not supported.
1180 *
1181 * This hook is intentionally internal to the command processing system.
1182 *
1183 * cmd_in
1184 * The raw command string.
1185 *
1186 * cmd_out
1187 * The result of any processing.
1188 */
1189DECLARE_HOOK(cmd_execute,
0a334343 1190 (struct vty *vty, const char *cmd_in, char **cmd_out),
01e24c4a 1191 (vty, cmd_in, cmd_out));
0a334343 1192DEFINE_HOOK(cmd_execute, (struct vty *vty, const char *cmd_in, char **cmd_out),
01e24c4a
QY
1193 (vty, cmd_in, cmd_out));
1194
1195/* Hook executed after a CLI command. */
0a334343 1196DECLARE_KOOH(cmd_execute_done, (struct vty *vty, const char *cmd_exec),
01e24c4a 1197 (vty, cmd_exec));
0a334343 1198DEFINE_KOOH(cmd_execute_done, (struct vty *vty, const char *cmd_exec),
01e24c4a
QY
1199 (vty, cmd_exec));
1200
fe6b47b9
QY
1201/*
1202 * cmd_execute hook subscriber to handle `|` actions.
1203 */
1204static int handle_pipe_action(struct vty *vty, const char *cmd_in,
1205 char **cmd_out)
1206{
1207 /* look for `|` */
9f227e4c 1208 char *orig, *working, *token, *u;
fe6b47b9
QY
1209 char *pipe = strstr(cmd_in, "| ");
1210
1211 if (!pipe)
1212 return 0;
1213
1214 /* duplicate string for processing purposes, not including pipe */
1215 orig = working = XSTRDUP(MTYPE_TMP, pipe + 2);
1216
1217 /* retrieve action */
1218 token = strsep(&working, " ");
4f4060f6 1219 assert(token);
fe6b47b9
QY
1220
1221 /* match result to known actions */
1222 if (strmatch(token, "include")) {
1223 /* the remaining text should be a regexp */
1224 char *regexp = working;
5d806ec6
QY
1225
1226 if (!regexp) {
1227 vty_out(vty, "%% Need a regexp to filter with\n");
1228 goto fail;
1229 }
1230
fe6b47b9 1231 bool succ = vty_set_include(vty, regexp);
0a334343 1232
fe6b47b9 1233 if (!succ) {
5d806ec6 1234 vty_out(vty, "%% Bad regexp '%s'\n", regexp);
fe6b47b9
QY
1235 goto fail;
1236 }
2cddf2ff 1237 *cmd_out = XSTRDUP(MTYPE_TMP, cmd_in);
9f227e4c 1238 u = *cmd_out;
1239 strsep(&u, "|");
fe6b47b9 1240 } else {
5d806ec6 1241 vty_out(vty, "%% Unknown action '%s'\n", token);
fe6b47b9
QY
1242 goto fail;
1243 }
1244
1245fail:
1246 XFREE(MTYPE_TMP, orig);
1247 return 0;
1248}
1249
1250static int handle_pipe_action_done(struct vty *vty, const char *cmd_exec)
1251{
0b42d81a 1252 if (vty->filter)
fe6b47b9 1253 vty_set_include(vty, NULL);
0b42d81a 1254
fe6b47b9
QY
1255 return 0;
1256}
1257
01e24c4a
QY
1258int cmd_execute(struct vty *vty, const char *cmd,
1259 const struct cmd_element **matched, int vtysh)
1260{
1261 int ret;
1262 char *cmd_out = NULL;
1263 const char *cmd_exec;
1264 vector vline;
1265
1266 hook_call(cmd_execute, vty, cmd, &cmd_out);
1267 cmd_exec = cmd_out ? (const char *)cmd_out : cmd;
1268
1269 vline = cmd_make_strvec(cmd_exec);
1270
1271 if (vline) {
1272 ret = cmd_execute_command(vline, vty, matched, vtysh);
1273 cmd_free_strvec(vline);
1274 } else {
1275 ret = CMD_SUCCESS;
1276 }
1277
1278 hook_call(cmd_execute_done, vty, cmd_exec);
1279
1280 if (cmd_out)
1281 XFREE(MTYPE_TMP, cmd_out);
1282
1283 return ret;
1284}
1285
1286
bed578b8 1287/**
d62a17ae 1288 * Parse one line of config, walking up the parse tree attempting to find a
1289 * match
bed578b8
DS
1290 *
1291 * @param vty The vty context in which the command should be executed.
1292 * @param cmd Pointer where the struct cmd_element* of the match command
1293 * will be stored, if any. May be set to NULL if this info is
1294 * not needed.
d62a17ae 1295 * @param use_daemon Boolean to control whether or not we match on
1296 * CMD_SUCCESS_DAEMON
bed578b8
DS
1297 * or not.
1298 * @return The status of the command that has been executed or an error code
1299 * as to why no command could be executed.
1300 */
d62a17ae 1301int command_config_read_one_line(struct vty *vty,
7ab57d19
DS
1302 const struct cmd_element **cmd,
1303 uint32_t line_num, int use_daemon)
bed578b8 1304{
d62a17ae 1305 vector vline;
d62a17ae 1306 int ret;
bed578b8 1307
d62a17ae 1308 vline = cmd_make_strvec(vty->buf);
bed578b8 1309
d62a17ae 1310 /* In case of comment line */
1311 if (vline == NULL)
1312 return CMD_SUCCESS;
bed578b8 1313
d62a17ae 1314 /* Execute configuration command : this is strict match */
1315 ret = cmd_execute_command_strict(vline, vty, cmd);
bed578b8 1316
d62a17ae 1317 // Climb the tree and try the command again at each node
1318 if (!(use_daemon && ret == CMD_SUCCESS_DAEMON)
1319 && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO)
1320 && ret != CMD_SUCCESS && ret != CMD_WARNING
996c9314 1321 && ret != CMD_NOT_MY_INSTANCE && ret != CMD_WARNING_CONFIG_FAILED
d62a17ae 1322 && vty->node != CONFIG_NODE) {
1c2facd1
RW
1323 int saved_node = vty->node;
1324 int saved_xpath_index = vty->xpath_index;
bed578b8 1325
d62a17ae 1326 while (!(use_daemon && ret == CMD_SUCCESS_DAEMON)
1327 && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO)
1328 && ret != CMD_SUCCESS && ret != CMD_WARNING
1329 && vty->node > CONFIG_NODE) {
1330 vty->node = node_parent(vty->node);
1c2facd1
RW
1331 if (vty->xpath_index > 0)
1332 vty->xpath_index--;
d62a17ae 1333 ret = cmd_execute_command_strict(vline, vty, cmd);
1334 }
bed578b8 1335
d62a17ae 1336 // If climbing the tree did not work then ignore the command and
1337 // stay at the same node
1338 if (!(use_daemon && ret == CMD_SUCCESS_DAEMON)
1339 && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO)
1340 && ret != CMD_SUCCESS && ret != CMD_WARNING) {
1341 vty->node = saved_node;
1c2facd1 1342 vty->xpath_index = saved_xpath_index;
d62a17ae 1343 }
1344 }
bed578b8 1345
b45d8ccc
DS
1346 if (ret != CMD_SUCCESS &&
1347 ret != CMD_WARNING &&
1348 ret != CMD_SUCCESS_DAEMON) {
7ab57d19
DS
1349 struct vty_error *ve = XCALLOC(MTYPE_TMP, sizeof(*ve));
1350
1351 memcpy(ve->error_buf, vty->buf, VTY_BUFSIZ);
1352 ve->line_num = line_num;
1353 if (!vty->error)
1354 vty->error = list_new();
1355
1356 listnode_add(vty->error, ve);
1357 }
cbd7259d 1358
d62a17ae 1359 cmd_free_strvec(vline);
bed578b8 1360
d62a17ae 1361 return ret;
bed578b8
DS
1362}
1363
5689fe5f 1364/* Configuration make from file. */
d62a17ae 1365int config_from_file(struct vty *vty, FILE *fp, unsigned int *line_num)
718e3744 1366{
d62a17ae 1367 int ret, error_ret = 0;
1368 *line_num = 0;
718e3744 1369
d62a17ae 1370 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
7ab57d19 1371 ++(*line_num);
13fbc82d 1372
7ab57d19 1373 ret = command_config_read_one_line(vty, NULL, *line_num, 0);
718e3744 1374
d62a17ae 1375 if (ret != CMD_SUCCESS && ret != CMD_WARNING
1376 && ret != CMD_ERR_NOTHING_TODO)
1377 error_ret = ret;
1378 }
5689fe5f 1379
d62a17ae 1380 if (error_ret) {
1381 return error_ret;
1382 }
5689fe5f 1383
d62a17ae 1384 return CMD_SUCCESS;
718e3744 1385}
1386
5689fe5f 1387/* Configuration from terminal */
718e3744 1388DEFUN (config_terminal,
1389 config_terminal_cmd,
1390 "configure terminal",
1391 "Configuration from vty interface\n"
1392 "Configuration terminal\n")
1393{
f344c66e 1394 return vty_config_enter(vty, false, false);
718e3744 1395}
1396
1397/* Enable command */
d0bfb22c 1398DEFUN (enable,
718e3744 1399 config_enable_cmd,
1400 "enable",
1401 "Turn on privileged mode command\n")
1402{
d62a17ae 1403 /* If enable password is NULL, change to ENABLE_NODE */
1404 if ((host.enable == NULL && host.enable_encrypt == NULL)
1405 || vty->type == VTY_SHELL_SERV)
1406 vty->node = ENABLE_NODE;
1407 else
1408 vty->node = AUTH_ENABLE_NODE;
718e3744 1409
d62a17ae 1410 return CMD_SUCCESS;
718e3744 1411}
1412
1413/* Disable command */
d0bfb22c 1414DEFUN (disable,
718e3744 1415 config_disable_cmd,
1416 "disable",
1417 "Turn off privileged mode command\n")
1418{
d62a17ae 1419 if (vty->node == ENABLE_NODE)
1420 vty->node = VIEW_NODE;
1421 return CMD_SUCCESS;
718e3744 1422}
1423
1424/* Down vty node level. */
1425DEFUN (config_exit,
1426 config_exit_cmd,
1427 "exit",
1428 "Exit current mode and down to previous mode\n")
0b84f294 1429{
d62a17ae 1430 cmd_exit(vty);
1431 return CMD_SUCCESS;
1432}
1433
1434void cmd_exit(struct vty *vty)
1435{
1436 switch (vty->node) {
1437 case VIEW_NODE:
1438 case ENABLE_NODE:
1439 if (vty_shell(vty))
1440 exit(0);
1441 else
1442 vty->status = VTY_CLOSE;
1443 break;
1444 case CONFIG_NODE:
1445 vty->node = ENABLE_NODE;
f344c66e 1446 vty_config_exit(vty);
d62a17ae 1447 break;
1448 case INTERFACE_NODE:
2dd0d726 1449 case PW_NODE:
e26aedbe 1450 case LOGICALROUTER_NODE:
d62a17ae 1451 case VRF_NODE:
dba32923 1452 case NH_GROUP_NODE:
d62a17ae 1453 case ZEBRA_NODE:
1454 case BGP_NODE:
1455 case RIP_NODE:
1456 case EIGRP_NODE:
1457 case BABEL_NODE:
1458 case RIPNG_NODE:
1459 case OSPF_NODE:
1460 case OSPF6_NODE:
1461 case LDP_NODE:
1462 case LDP_L2VPN_NODE:
1463 case ISIS_NODE:
7c0cbd0e 1464 case OPENFABRIC_NODE:
d62a17ae 1465 case KEYCHAIN_NODE:
d62a17ae 1466 case RMAP_NODE:
e5c83d9b 1467 case PBRMAP_NODE:
d62a17ae 1468 case VTY_NODE:
c2f29cf3 1469 case BFD_NODE:
d62a17ae 1470 vty->node = CONFIG_NODE;
1471 break;
1472 case BGP_IPV4_NODE:
1473 case BGP_IPV4M_NODE:
1474 case BGP_IPV4L_NODE:
1475 case BGP_VPNV4_NODE:
1476 case BGP_VPNV6_NODE:
7c40bf39 1477 case BGP_FLOWSPECV4_NODE:
1478 case BGP_FLOWSPECV6_NODE:
d62a17ae 1479 case BGP_VRF_POLICY_NODE:
1480 case BGP_VNC_DEFAULTS_NODE:
1481 case BGP_VNC_NVE_GROUP_NODE:
1482 case BGP_VNC_L2_GROUP_NODE:
1483 case BGP_IPV6_NODE:
1484 case BGP_IPV6M_NODE:
1485 case BGP_EVPN_NODE:
1486 case BGP_IPV6L_NODE:
1487 vty->node = BGP_NODE;
1488 break;
1489 case BGP_EVPN_VNI_NODE:
1490 vty->node = BGP_EVPN_NODE;
1491 break;
1492 case LDP_IPV4_NODE:
1493 case LDP_IPV6_NODE:
1494 vty->node = LDP_NODE;
1495 break;
1496 case LDP_IPV4_IFACE_NODE:
1497 vty->node = LDP_IPV4_NODE;
1498 break;
1499 case LDP_IPV6_IFACE_NODE:
1500 vty->node = LDP_IPV6_NODE;
1501 break;
1502 case LDP_PSEUDOWIRE_NODE:
1503 vty->node = LDP_L2VPN_NODE;
1504 break;
1505 case KEYCHAIN_KEY_NODE:
1506 vty->node = KEYCHAIN_NODE;
1507 break;
1508 case LINK_PARAMS_NODE:
1509 vty->node = INTERFACE_NODE;
1510 break;
c2f29cf3
RZ
1511 case BFD_PEER_NODE:
1512 vty->node = BFD_NODE;
1513 break;
d62a17ae 1514 default:
1515 break;
1516 }
1c2facd1
RW
1517
1518 if (vty->xpath_index > 0)
1519 vty->xpath_index--;
718e3744 1520}
1521
f667a580
QY
1522/* ALIAS_FIXME */
1523DEFUN (config_quit,
1524 config_quit_cmd,
1525 "quit",
1526 "Exit current mode and down to previous mode\n")
1527{
d62a17ae 1528 return config_exit(self, vty, argc, argv);
f667a580
QY
1529}
1530
d0bfb22c 1531
718e3744 1532/* End of configuration. */
1533DEFUN (config_end,
1534 config_end_cmd,
1535 "end",
efd7904e 1536 "End current mode and change to enable mode.\n")
718e3744 1537{
cf09d3ca 1538 if (vty->config) {
f344c66e 1539 vty_config_exit(vty);
d62a17ae 1540 vty->node = ENABLE_NODE;
d62a17ae 1541 }
1c2facd1 1542
d62a17ae 1543 return CMD_SUCCESS;
718e3744 1544}
1545
1546/* Show version. */
1547DEFUN (show_version,
1548 show_version_cmd,
1549 "show version",
1550 SHOW_STR
1551 "Displays zebra version\n")
1552{
d62a17ae 1553 vty_out(vty, "%s %s (%s).\n", FRR_FULL_NAME, FRR_VERSION,
6b3ee3a0 1554 cmd_hostname_get() ? cmd_hostname_get() : "");
d62a17ae 1555 vty_out(vty, "%s%s\n", FRR_COPYRIGHT, GIT_INFO);
1556 vty_out(vty, "configured with:\n %s\n", FRR_CONFIG_ARGS);
718e3744 1557
d62a17ae 1558 return CMD_SUCCESS;
718e3744 1559}
1560
8efe88ea
DL
1561/* "Set" version ... ignore version tags */
1562DEFUN (frr_version_defaults,
1563 frr_version_defaults_cmd,
3e7c8d04 1564 "frr <version|defaults> LINE...",
447a8fe9 1565 "FRRouting global parameters\n"
8efe88ea
DL
1566 "version configuration was written by\n"
1567 "set of configuration defaults used\n"
1568 "version string\n")
1569{
d62a17ae 1570 return CMD_SUCCESS;
8efe88ea
DL
1571}
1572
718e3744 1573/* Help display function for all node. */
1574DEFUN (config_help,
1575 config_help_cmd,
1576 "help",
1577 "Description of the interactive help system\n")
1578{
d62a17ae 1579 vty_out(vty,
1580 "Quagga VTY provides advanced help feature. When you need help,\n\
61b7d449
DL
1581anytime at the command line please press '?'.\n\
1582\n\
1583If nothing matches, the help list will be empty and you must backup\n\
1584 until entering a '?' shows the available options.\n\
1585Two styles of help are provided:\n\
15861. Full help is available when you are ready to enter a\n\
1587command argument (e.g. 'show ?') and describes each possible\n\
1588argument.\n\
15892. Partial help is provided when an abbreviated argument is entered\n\
1590 and you want to know what arguments match the input\n\
1591 (e.g. 'show me?'.)\n\n");
d62a17ae 1592 return CMD_SUCCESS;
1593}
1594
1595static void permute(struct graph_node *start, struct vty *vty)
1596{
1597 static struct list *position = NULL;
1598 if (!position)
1599 position = list_new();
1600
1601 struct cmd_token *stok = start->data;
1602 struct graph_node *gnn;
1603 struct listnode *ln;
1604
1605 // recursive dfs
1606 listnode_add(position, start);
1607 for (unsigned int i = 0; i < vector_active(start->to); i++) {
1608 struct graph_node *gn = vector_slot(start->to, i);
1609 struct cmd_token *tok = gn->data;
1610 if (tok->attr == CMD_ATTR_HIDDEN
1611 || tok->attr == CMD_ATTR_DEPRECATED)
1612 continue;
1613 else if (tok->type == END_TKN || gn == start) {
1614 vty_out(vty, " ");
1615 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) {
1616 struct cmd_token *tt = gnn->data;
1617 if (tt->type < SPECIAL_TKN)
1618 vty_out(vty, " %s", tt->text);
1619 }
1620 if (gn == start)
1621 vty_out(vty, "...");
1622 vty_out(vty, "\n");
1623 } else {
1624 bool skip = false;
1625 if (stok->type == FORK_TKN && tok->type != FORK_TKN)
1626 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn))
1627 if (gnn == gn) {
1628 skip = true;
1629 break;
1630 }
1631 if (!skip)
1632 permute(gn, vty);
1633 }
1634 }
1635 list_delete_node(position, listtail(position));
1636}
1637
1638int cmd_list_cmds(struct vty *vty, int do_permute)
1639{
1640 struct cmd_node *node = vector_slot(cmdvec, vty->node);
1641
1642 if (do_permute)
1643 permute(vector_slot(node->cmdgraph->nodes, 0), vty);
1644 else {
1645 /* loop over all commands at this node */
1646 struct cmd_element *element = NULL;
1647 for (unsigned int i = 0; i < vector_active(node->cmd_vector);
1648 i++)
1649 if ((element = vector_slot(node->cmd_vector, i))
1650 && element->attr != CMD_ATTR_DEPRECATED
1651 && element->attr != CMD_ATTR_HIDDEN)
1652 vty_out(vty, " %s\n", element->string);
1653 }
1654 return CMD_SUCCESS;
718e3744 1655}
1656
0b84f294
DL
1657/* Help display function for all node. */
1658DEFUN (config_list,
1659 config_list_cmd,
1660 "list [permutations]",
1661 "Print command list\n"
1662 "Print all possible command permutations\n")
1663{
d62a17ae 1664 return cmd_list_cmds(vty, argc == 2);
0b84f294
DL
1665}
1666
a2454870
QY
1667DEFUN (show_commandtree,
1668 show_commandtree_cmd,
1669 "show commandtree [permutations]",
1670 SHOW_STR
5a1945e4
DS
1671 "Show command tree\n"
1672 "Permutations that we are interested in\n")
a2454870 1673{
d62a17ae 1674 return cmd_list_cmds(vty, argc == 3);
a2454870
QY
1675}
1676
26fbe472
QY
1677DEFUN_HIDDEN(show_cli_graph,
1678 show_cli_graph_cmd,
1679 "show cli graph",
1680 SHOW_STR
1681 "CLI reflection\n"
1682 "Dump current command space as DOT graph\n")
1683{
1684 struct cmd_node *cn = vector_slot(cmdvec, vty->node);
1685 char *dot = cmd_graph_dump_dot(cn->cmdgraph);
1686
1687 vty_out(vty, "%s\n", dot);
1688 XFREE(MTYPE_TMP, dot);
1689 return CMD_SUCCESS;
1690}
1691
f806f29c 1692static int vty_write_config(struct vty *vty)
8efe88ea 1693{
d62a17ae 1694 size_t i;
1695 struct cmd_node *node;
8efe88ea 1696
f806f29c 1697 if (host.noconfig)
1698 return CMD_SUCCESS;
1699
d62a17ae 1700 if (vty->type == VTY_TERM) {
1701 vty_out(vty, "\nCurrent configuration:\n");
1702 vty_out(vty, "!\n");
1703 }
8efe88ea 1704
d62a17ae 1705 vty_out(vty, "frr version %s\n", FRR_VER_SHORT);
1706 vty_out(vty, "frr defaults %s\n", DFLT_NAME);
1707 vty_out(vty, "!\n");
8efe88ea 1708
d62a17ae 1709 for (i = 0; i < vector_active(cmdvec); i++)
1710 if ((node = vector_slot(cmdvec, i)) && node->func
1711 && (node->vtysh || vty->type != VTY_SHELL)) {
1712 if ((*node->func)(vty))
1713 vty_out(vty, "!\n");
1714 }
8efe88ea 1715
d62a17ae 1716 if (vty->type == VTY_TERM) {
1717 vty_out(vty, "end\n");
1718 }
8efe88ea 1719
f806f29c 1720 return CMD_SUCCESS;
1721}
d862bffb 1722
f806f29c 1723static int file_write_config(struct vty *vty)
718e3744 1724{
d62a17ae 1725 int fd, dirfd;
1726 char *config_file, *slash;
1727 char *config_file_tmp = NULL;
1728 char *config_file_sav = NULL;
1729 int ret = CMD_WARNING;
1730 struct vty *file_vty;
1731 struct stat conf_stat;
1732
d62a17ae 1733 if (host.noconfig)
1734 return CMD_SUCCESS;
1735
1736 /* Check and see if we are operating under vtysh configuration */
1737 if (host.config == NULL) {
1738 vty_out(vty,
1739 "Can't save to configuration file, using vtysh.\n");
1740 return CMD_WARNING;
1741 }
1742
1743 /* Get filename. */
1744 config_file = host.config;
d0bfb22c 1745
056cfe49
DL
1746#ifndef O_DIRECTORY
1747#define O_DIRECTORY 0
1748#endif
d62a17ae 1749 slash = strrchr(config_file, '/');
1750 if (slash) {
1751 char *config_dir = XSTRDUP(MTYPE_TMP, config_file);
1752 config_dir[slash - config_file] = '\0';
1753 dirfd = open(config_dir, O_DIRECTORY | O_RDONLY);
1754 XFREE(MTYPE_TMP, config_dir);
1755 } else
1756 dirfd = open(".", O_DIRECTORY | O_RDONLY);
1757 /* if dirfd is invalid, directory sync fails, but we're still OK */
1758
1759 config_file_sav = XMALLOC(
1760 MTYPE_TMP, strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1);
1761 strcpy(config_file_sav, config_file);
1762 strcat(config_file_sav, CONF_BACKUP_EXT);
1763
1764
1765 config_file_tmp = XMALLOC(MTYPE_TMP, strlen(config_file) + 8);
1766 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
1767
1768 /* Open file to configuration write. */
1769 fd = mkstemp(config_file_tmp);
1770 if (fd < 0) {
1771 vty_out(vty, "Can't open configuration file %s.\n",
1772 config_file_tmp);
1773 goto finished;
1774 }
1775 if (fchmod(fd, CONFIGFILE_MASK) != 0) {
1776 vty_out(vty, "Can't chmod configuration file %s: %s (%d).\n",
1777 config_file_tmp, safe_strerror(errno), errno);
1778 goto finished;
1779 }
1780
1781 /* Make vty for configuration file. */
1782 file_vty = vty_new();
1783 file_vty->wfd = fd;
1784 file_vty->type = VTY_FILE;
1785
1786 /* Config file header print. */
1787 vty_out(file_vty, "!\n! Zebra configuration saved from vty\n! ");
1788 vty_time_print(file_vty, 1);
1789 vty_out(file_vty, "!\n");
1790 vty_write_config(file_vty);
1791 vty_close(file_vty);
1792
1793 if (stat(config_file, &conf_stat) >= 0) {
1794 if (unlink(config_file_sav) != 0)
1795 if (errno != ENOENT) {
1796 vty_out(vty,
1797 "Can't unlink backup configuration file %s.\n",
1798 config_file_sav);
1799 goto finished;
1800 }
1801 if (link(config_file, config_file_sav) != 0) {
1802 vty_out(vty,
1803 "Can't backup old configuration file %s.\n",
1804 config_file_sav);
1805 goto finished;
1806 }
1807 if (dirfd >= 0)
1808 fsync(dirfd);
1809 }
1810 if (rename(config_file_tmp, config_file) != 0) {
1811 vty_out(vty, "Can't save configuration file %s.\n",
1812 config_file);
1813 goto finished;
1814 }
1815 if (dirfd >= 0)
1816 fsync(dirfd);
1817
1818 vty_out(vty, "Configuration saved to %s\n", config_file);
1819 ret = CMD_SUCCESS;
05865c90 1820
1821finished:
d62a17ae 1822 if (ret != CMD_SUCCESS)
1823 unlink(config_file_tmp);
1824 if (dirfd >= 0)
1825 close(dirfd);
1826 XFREE(MTYPE_TMP, config_file_tmp);
1827 XFREE(MTYPE_TMP, config_file_sav);
1828 return ret;
718e3744 1829}
1830
f806f29c 1831/* Write current configuration into file. */
1832
1833DEFUN (config_write,
1834 config_write_cmd,
1835 "write [<file|memory|terminal>]",
1836 "Write running configuration to memory, network, or terminal\n"
1837 "Write to configuration file\n"
1838 "Write configuration currently in memory\n"
1839 "Write configuration to terminal\n")
1840{
1841 const int idx_type = 1;
1842
1843 // if command was 'write terminal' or 'write memory'
1844 if (argc == 2 && (!strcmp(argv[idx_type]->text, "terminal"))) {
1845 return vty_write_config(vty);
1846 }
1847
1848 return file_write_config(vty);
1849}
1850
d862bffb
QY
1851/* ALIAS_FIXME for 'write <terminal|memory>' */
1852DEFUN (show_running_config,
1853 show_running_config_cmd,
1854 "show running-config",
1855 SHOW_STR
f806f29c 1856 "running configuration (same as write terminal)\n")
d862bffb 1857{
f806f29c 1858 return vty_write_config(vty);
d862bffb 1859}
718e3744 1860
d862bffb
QY
1861/* ALIAS_FIXME for 'write file' */
1862DEFUN (copy_runningconf_startupconf,
1863 copy_runningconf_startupconf_cmd,
1864 "copy running-config startup-config",
1865 "Copy configuration\n"
1866 "Copy running config to... \n"
f806f29c 1867 "Copy running config to startup config (same as write file/memory)\n")
d862bffb 1868{
f806f29c 1869 return file_write_config(vty);
d862bffb
QY
1870}
1871/** -- **/
718e3744 1872
1873/* Write startup configuration into the terminal. */
1874DEFUN (show_startup_config,
1875 show_startup_config_cmd,
1876 "show startup-config",
1877 SHOW_STR
d0bfb22c 1878 "Contents of startup configuration\n")
718e3744 1879{
d62a17ae 1880 char buf[BUFSIZ];
1881 FILE *confp;
718e3744 1882
d62a17ae 1883 if (host.noconfig)
1884 return CMD_SUCCESS;
1885 if (host.config == NULL)
1886 return CMD_WARNING;
87f44e2f 1887
d62a17ae 1888 confp = fopen(host.config, "r");
1889 if (confp == NULL) {
1890 vty_out(vty, "Can't open configuration file [%s] due to '%s'\n",
1891 host.config, safe_strerror(errno));
1892 return CMD_WARNING;
1893 }
718e3744 1894
d62a17ae 1895 while (fgets(buf, BUFSIZ, confp)) {
1896 char *cp = buf;
718e3744 1897
d62a17ae 1898 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
1899 cp++;
1900 *cp = '\0';
718e3744 1901
d62a17ae 1902 vty_out(vty, "%s\n", buf);
1903 }
718e3744 1904
d62a17ae 1905 fclose(confp);
718e3744 1906
d62a17ae 1907 return CMD_SUCCESS;
718e3744 1908}
1909
6b3ee3a0
MK
1910int cmd_domainname_set(const char *domainname)
1911{
1912 XFREE(MTYPE_HOST, host.domainname);
1913 host.domainname = domainname ? XSTRDUP(MTYPE_HOST, domainname) : NULL;
1914 return CMD_SUCCESS;
1915}
1916
1917/* Hostname configuration */
60466a63
QY
1918DEFUN(config_domainname,
1919 domainname_cmd,
1920 "domainname WORD",
1921 "Set system's domain name\n"
1922 "This system's domain name\n")
6b3ee3a0
MK
1923{
1924 struct cmd_token *word = argv[1];
1925
1926 if (!isalpha((int)word->arg[0])) {
1927 vty_out(vty, "Please specify string starting with alphabet\n");
1928 return CMD_WARNING_CONFIG_FAILED;
1929 }
1930
1931 return cmd_domainname_set(word->arg);
1932}
1933
60466a63
QY
1934DEFUN(config_no_domainname,
1935 no_domainname_cmd,
1936 "no domainname [DOMAINNAME]",
1937 NO_STR
1938 "Reset system's domain name\n"
1939 "domain name of this router\n")
6b3ee3a0
MK
1940{
1941 return cmd_domainname_set(NULL);
1942}
1943
d62a17ae 1944int cmd_hostname_set(const char *hostname)
bff9c3e9 1945{
d62a17ae 1946 XFREE(MTYPE_HOST, host.name);
1947 host.name = hostname ? XSTRDUP(MTYPE_HOST, hostname) : NULL;
1948 return CMD_SUCCESS;
bff9c3e9
DL
1949}
1950
718e3744 1951/* Hostname configuration */
d0bfb22c 1952DEFUN (config_hostname,
718e3744 1953 hostname_cmd,
1954 "hostname WORD",
1955 "Set system's network name\n"
1956 "This system's network name\n")
1957{
d62a17ae 1958 struct cmd_token *word = argv[1];
d0bfb22c 1959
d4f368e1 1960 if (!isalnum((int)word->arg[0])) {
d62a17ae 1961 vty_out(vty, "Please specify string starting with alphabet\n");
1962 return CMD_WARNING_CONFIG_FAILED;
1963 }
718e3744 1964
d62a17ae 1965 return cmd_hostname_set(word->arg);
718e3744 1966}
1967
d0bfb22c 1968DEFUN (config_no_hostname,
718e3744 1969 no_hostname_cmd,
1970 "no hostname [HOSTNAME]",
1971 NO_STR
1972 "Reset system's network name\n"
1973 "Host name of this router\n")
1974{
d62a17ae 1975 return cmd_hostname_set(NULL);
718e3744 1976}
1977
1978/* VTY interface password set. */
f412b39a
DW
1979DEFUN (config_password,
1980 password_cmd,
98463e0a 1981 "password [(8-8)] WORD",
322e2d5c 1982 "Modify the terminal connection password\n"
718e3744 1983 "Specifies a HIDDEN password will follow\n"
d0bfb22c 1984 "The password string\n")
718e3744 1985{
d62a17ae 1986 int idx_8 = 1;
1987 int idx_word = 2;
1988 if (argc == 3) // '8' was specified
1989 {
1990 if (host.password)
1991 XFREE(MTYPE_HOST, host.password);
1992 host.password = NULL;
1993 if (host.password_encrypt)
1994 XFREE(MTYPE_HOST, host.password_encrypt);
1995 host.password_encrypt =
1996 XSTRDUP(MTYPE_HOST, argv[idx_word]->arg);
1997 return CMD_SUCCESS;
1998 }
1999
a7ce0ad1 2000 if (!isalnum((int)argv[idx_8]->arg[0])) {
d62a17ae 2001 vty_out(vty,
2002 "Please specify string starting with alphanumeric\n");
2003 return CMD_WARNING_CONFIG_FAILED;
2004 }
2005
2006 if (host.password)
2007 XFREE(MTYPE_HOST, host.password);
2008 host.password = NULL;
2009
2010 if (host.encrypt) {
2011 if (host.password_encrypt)
2012 XFREE(MTYPE_HOST, host.password_encrypt);
2013 host.password_encrypt =
2014 XSTRDUP(MTYPE_HOST, zencrypt(argv[idx_8]->arg));
2015 } else
2016 host.password = XSTRDUP(MTYPE_HOST, argv[idx_8]->arg);
2017
2018 return CMD_SUCCESS;
718e3744 2019}
2020
322e2d5c
PM
2021/* VTY interface password delete. */
2022DEFUN (no_config_password,
2023 no_password_cmd,
2024 "no password",
2025 NO_STR
2026 "Modify the terminal connection password\n")
2027{
2028 bool warned = false;
2029
2030 if (host.password) {
eb83f7ce 2031 if (!vty_shell_serv(vty)) {
4911ca9c 2032 vty_out(vty, NO_PASSWD_CMD_WARNING);
eb83f7ce
PM
2033 warned = true;
2034 }
322e2d5c
PM
2035 XFREE(MTYPE_HOST, host.password);
2036 }
2037 host.password = NULL;
2038
2039 if (host.password_encrypt) {
eb83f7ce 2040 if (!warned && !vty_shell_serv(vty))
4911ca9c 2041 vty_out(vty, NO_PASSWD_CMD_WARNING);
322e2d5c
PM
2042 XFREE(MTYPE_HOST, host.password_encrypt);
2043 }
2044 host.password_encrypt = NULL;
2045
2046 return CMD_SUCCESS;
2047}
2048
718e3744 2049/* VTY enable password set. */
f412b39a
DW
2050DEFUN (config_enable_password,
2051 enable_password_cmd,
98463e0a 2052 "enable password [(8-8)] WORD",
718e3744 2053 "Modify enable password parameters\n"
2054 "Assign the privileged level password\n"
2055 "Specifies a HIDDEN password will follow\n"
718e3744 2056 "The HIDDEN 'enable' password string\n")
2057{
d62a17ae 2058 int idx_8 = 2;
2059 int idx_word = 3;
2060
2061 /* Crypt type is specified. */
2062 if (argc == 4) {
2063 if (argv[idx_8]->arg[0] == '8') {
2064 if (host.enable)
2065 XFREE(MTYPE_HOST, host.enable);
2066 host.enable = NULL;
2067
2068 if (host.enable_encrypt)
2069 XFREE(MTYPE_HOST, host.enable_encrypt);
2070 host.enable_encrypt =
2071 XSTRDUP(MTYPE_HOST, argv[idx_word]->arg);
2072
2073 return CMD_SUCCESS;
2074 } else {
2075 vty_out(vty, "Unknown encryption type.\n");
2076 return CMD_WARNING_CONFIG_FAILED;
2077 }
2078 }
2079
a7ce0ad1 2080 if (!isalnum((int)argv[idx_8]->arg[0])) {
d62a17ae 2081 vty_out(vty,
2082 "Please specify string starting with alphanumeric\n");
2083 return CMD_WARNING_CONFIG_FAILED;
2084 }
2085
2086 if (host.enable)
2087 XFREE(MTYPE_HOST, host.enable);
2088 host.enable = NULL;
2089
2090 /* Plain password input. */
2091 if (host.encrypt) {
2092 if (host.enable_encrypt)
2093 XFREE(MTYPE_HOST, host.enable_encrypt);
2094 host.enable_encrypt =
2095 XSTRDUP(MTYPE_HOST, zencrypt(argv[idx_8]->arg));
2096 } else
2097 host.enable = XSTRDUP(MTYPE_HOST, argv[idx_8]->arg);
2098
2099 return CMD_SUCCESS;
718e3744 2100}
2101
718e3744 2102/* VTY enable password delete. */
f412b39a
DW
2103DEFUN (no_config_enable_password,
2104 no_enable_password_cmd,
718e3744 2105 "no enable password",
2106 NO_STR
2107 "Modify enable password parameters\n"
2108 "Assign the privileged level password\n")
2109{
322e2d5c
PM
2110 bool warned = false;
2111
2112 if (host.enable) {
eb83f7ce 2113 if (!vty_shell_serv(vty)) {
4911ca9c 2114 vty_out(vty, NO_PASSWD_CMD_WARNING);
eb83f7ce
PM
2115 warned = true;
2116 }
d62a17ae 2117 XFREE(MTYPE_HOST, host.enable);
322e2d5c 2118 }
d62a17ae 2119 host.enable = NULL;
718e3744 2120
322e2d5c 2121 if (host.enable_encrypt) {
eb83f7ce 2122 if (!warned && !vty_shell_serv(vty))
4911ca9c 2123 vty_out(vty, NO_PASSWD_CMD_WARNING);
d62a17ae 2124 XFREE(MTYPE_HOST, host.enable_encrypt);
322e2d5c 2125 }
d62a17ae 2126 host.enable_encrypt = NULL;
718e3744 2127
d62a17ae 2128 return CMD_SUCCESS;
718e3744 2129}
d0bfb22c 2130
718e3744 2131DEFUN (service_password_encrypt,
2132 service_password_encrypt_cmd,
2133 "service password-encryption",
2134 "Set up miscellaneous service\n"
2135 "Enable encrypted passwords\n")
2136{
d62a17ae 2137 if (host.encrypt)
2138 return CMD_SUCCESS;
718e3744 2139
d62a17ae 2140 host.encrypt = 1;
718e3744 2141
d62a17ae 2142 if (host.password) {
2143 if (host.password_encrypt)
2144 XFREE(MTYPE_HOST, host.password_encrypt);
2145 host.password_encrypt =
2146 XSTRDUP(MTYPE_HOST, zencrypt(host.password));
2147 }
2148 if (host.enable) {
2149 if (host.enable_encrypt)
2150 XFREE(MTYPE_HOST, host.enable_encrypt);
2151 host.enable_encrypt =
2152 XSTRDUP(MTYPE_HOST, zencrypt(host.enable));
2153 }
718e3744 2154
d62a17ae 2155 return CMD_SUCCESS;
718e3744 2156}
2157
2158DEFUN (no_service_password_encrypt,
2159 no_service_password_encrypt_cmd,
2160 "no service password-encryption",
2161 NO_STR
2162 "Set up miscellaneous service\n"
2163 "Enable encrypted passwords\n")
2164{
d62a17ae 2165 if (!host.encrypt)
2166 return CMD_SUCCESS;
718e3744 2167
d62a17ae 2168 host.encrypt = 0;
718e3744 2169
d62a17ae 2170 if (host.password_encrypt)
2171 XFREE(MTYPE_HOST, host.password_encrypt);
2172 host.password_encrypt = NULL;
718e3744 2173
d62a17ae 2174 if (host.enable_encrypt)
2175 XFREE(MTYPE_HOST, host.enable_encrypt);
2176 host.enable_encrypt = NULL;
718e3744 2177
d62a17ae 2178 return CMD_SUCCESS;
718e3744 2179}
2180
f412b39a
DW
2181DEFUN (config_terminal_length,
2182 config_terminal_length_cmd,
d0bfb22c 2183 "terminal length (0-512)",
718e3744 2184 "Set terminal line parameters\n"
2185 "Set number of lines on a screen\n"
2186 "Number of lines on screen (0 for no pausing)\n")
2187{
d62a17ae 2188 int idx_number = 2;
718e3744 2189
d62a17ae 2190 vty->lines = atoi(argv[idx_number]->arg);
718e3744 2191
d62a17ae 2192 return CMD_SUCCESS;
718e3744 2193}
2194
f412b39a
DW
2195DEFUN (config_terminal_no_length,
2196 config_terminal_no_length_cmd,
718e3744 2197 "terminal no length",
2198 "Set terminal line parameters\n"
2199 NO_STR
2200 "Set number of lines on a screen\n")
2201{
d62a17ae 2202 vty->lines = -1;
2203 return CMD_SUCCESS;
718e3744 2204}
2205
f412b39a
DW
2206DEFUN (service_terminal_length,
2207 service_terminal_length_cmd,
d0bfb22c 2208 "service terminal-length (0-512)",
718e3744 2209 "Set up miscellaneous service\n"
2210 "System wide terminal length configuration\n"
2211 "Number of lines of VTY (0 means no line control)\n")
2212{
d62a17ae 2213 int idx_number = 2;
718e3744 2214
d62a17ae 2215 host.lines = atoi(argv[idx_number]->arg);
718e3744 2216
d62a17ae 2217 return CMD_SUCCESS;
718e3744 2218}
2219
f412b39a
DW
2220DEFUN (no_service_terminal_length,
2221 no_service_terminal_length_cmd,
d0bfb22c 2222 "no service terminal-length [(0-512)]",
718e3744 2223 NO_STR
2224 "Set up miscellaneous service\n"
2225 "System wide terminal length configuration\n"
2226 "Number of lines of VTY (0 means no line control)\n")
2227{
d62a17ae 2228 host.lines = -1;
2229 return CMD_SUCCESS;
718e3744 2230}
2231
2885f72d 2232DEFUN_HIDDEN (do_echo,
d0bfb22c
QY
2233 echo_cmd,
2234 "echo MESSAGE...",
2235 "Echo a message back to the vty\n"
2236 "The message to echo\n")
2885f72d 2237{
d62a17ae 2238 char *message;
2885f72d 2239
d62a17ae 2240 vty_out(vty, "%s\n",
2241 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2242 if (message)
2243 XFREE(MTYPE_TMP, message);
2244 return CMD_SUCCESS;
2885f72d 2245}
2246
274a4a44 2247DEFUN (config_logmsg,
2248 config_logmsg_cmd,
199d90a1 2249 "logmsg <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging> MESSAGE...",
274a4a44 2250 "Send a message to enabled logging destinations\n"
2251 LOG_LEVEL_DESC
2252 "The message to send\n")
2253{
d62a17ae 2254 int idx_log_level = 1;
2255 int idx_message = 2;
2256 int level;
2257 char *message;
274a4a44 2258
d62a17ae 2259 if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)
2260 return CMD_ERR_NO_MATCH;
274a4a44 2261
d62a17ae 2262 zlog(level, "%s",
2263 ((message = argv_concat(argv, argc, idx_message)) ? message : ""));
2264 if (message)
2265 XFREE(MTYPE_TMP, message);
65efcfce 2266
d62a17ae 2267 return CMD_SUCCESS;
274a4a44 2268}
2269
2270DEFUN (show_logging,
2271 show_logging_cmd,
2272 "show logging",
2273 SHOW_STR
2274 "Show current logging configuration\n")
2275{
d62a17ae 2276 struct zlog *zl = zlog_default;
2277
2278 vty_out(vty, "Syslog logging: ");
2279 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2280 vty_out(vty, "disabled");
2281 else
2282 vty_out(vty, "level %s, facility %s, ident %s",
2283 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2284 facility_name(zl->facility), zl->ident);
2285 vty_out(vty, "\n");
2286
2287 vty_out(vty, "Stdout logging: ");
2288 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2289 vty_out(vty, "disabled");
2290 else
2291 vty_out(vty, "level %s",
2292 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2293 vty_out(vty, "\n");
2294
2295 vty_out(vty, "Monitor logging: ");
2296 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2297 vty_out(vty, "disabled");
2298 else
2299 vty_out(vty, "level %s",
2300 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2301 vty_out(vty, "\n");
2302
2303 vty_out(vty, "File logging: ");
2304 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2305 vty_out(vty, "disabled");
2306 else
2307 vty_out(vty, "level %s, filename %s",
2308 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2309 zl->filename);
2310 vty_out(vty, "\n");
2311
2312 vty_out(vty, "Protocol name: %s\n", zl->protoname);
2313 vty_out(vty, "Record priority: %s\n",
2314 (zl->record_priority ? "enabled" : "disabled"));
2315 vty_out(vty, "Timestamp precision: %d\n", zl->timestamp_precision);
2316
2317 return CMD_SUCCESS;
274a4a44 2318}
2319
718e3744 2320DEFUN (config_log_stdout,
2321 config_log_stdout_cmd,
6de69f83 2322 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
274a4a44 2323 "Logging control\n"
2324 "Set stdout logging level\n"
2325 LOG_LEVEL_DESC)
2326{
d62a17ae 2327 int idx_log_level = 2;
58749582 2328
d62a17ae 2329 if (argc == idx_log_level) {
2330 zlog_set_level(ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2331 return CMD_SUCCESS;
2332 }
2333 int level;
274a4a44 2334
d62a17ae 2335 if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)
2336 return CMD_ERR_NO_MATCH;
2337 zlog_set_level(ZLOG_DEST_STDOUT, level);
2338 return CMD_SUCCESS;
718e3744 2339}
2340
2341DEFUN (no_config_log_stdout,
2342 no_config_log_stdout_cmd,
6de69f83 2343 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
718e3744 2344 NO_STR
2345 "Logging control\n"
274a4a44 2346 "Cancel logging to stdout\n"
d0bfb22c 2347 LOG_LEVEL_DESC)
718e3744 2348{
d62a17ae 2349 zlog_set_level(ZLOG_DEST_STDOUT, ZLOG_DISABLED);
2350 return CMD_SUCCESS;
718e3744 2351}
2352
274a4a44 2353DEFUN (config_log_monitor,
2354 config_log_monitor_cmd,
6de69f83 2355 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
274a4a44 2356 "Logging control\n"
2357 "Set terminal line (monitor) logging level\n"
2358 LOG_LEVEL_DESC)
2359{
d62a17ae 2360 int idx_log_level = 2;
58749582 2361
d62a17ae 2362 if (argc == idx_log_level) {
2363 zlog_set_level(ZLOG_DEST_MONITOR, zlog_default->default_lvl);
2364 return CMD_SUCCESS;
2365 }
2366 int level;
274a4a44 2367
d62a17ae 2368 if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)
2369 return CMD_ERR_NO_MATCH;
2370 zlog_set_level(ZLOG_DEST_MONITOR, level);
2371 return CMD_SUCCESS;
274a4a44 2372}
2373
2374DEFUN (no_config_log_monitor,
2375 no_config_log_monitor_cmd,
6de69f83 2376 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
274a4a44 2377 NO_STR
2378 "Logging control\n"
2379 "Disable terminal line (monitor) logging\n"
d0bfb22c 2380 LOG_LEVEL_DESC)
274a4a44 2381{
d62a17ae 2382 zlog_set_level(ZLOG_DEST_MONITOR, ZLOG_DISABLED);
2383 return CMD_SUCCESS;
274a4a44 2384}
2385
d62a17ae 2386static int set_log_file(struct vty *vty, const char *fname, int loglevel)
718e3744 2387{
d62a17ae 2388 int ret;
2389 char *p = NULL;
2390 const char *fullpath;
d0bfb22c 2391
d62a17ae 2392 /* Path detection. */
2393 if (!IS_DIRECTORY_SEP(*fname)) {
2394 char cwd[MAXPATHLEN + 1];
2395 cwd[MAXPATHLEN] = '\0';
d0bfb22c 2396
d62a17ae 2397 if (getcwd(cwd, MAXPATHLEN) == NULL) {
450971aa 2398 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3 2399 "config_log_file: Unable to alloc mem!");
d62a17ae 2400 return CMD_WARNING_CONFIG_FAILED;
2401 }
d0bfb22c 2402
0ce1ca80 2403 p = XMALLOC(MTYPE_TMP, strlen(cwd) + strlen(fname) + 2);
d62a17ae 2404 sprintf(p, "%s/%s", cwd, fname);
2405 fullpath = p;
2406 } else
2407 fullpath = fname;
718e3744 2408
d62a17ae 2409 ret = zlog_set_file(fullpath, loglevel);
718e3744 2410
d62a17ae 2411 if (p)
2412 XFREE(MTYPE_TMP, p);
9035efaa 2413
d62a17ae 2414 if (!ret) {
f8507817
DS
2415 if (vty)
2416 vty_out(vty, "can't open logfile %s\n", fname);
d62a17ae 2417 return CMD_WARNING_CONFIG_FAILED;
2418 }
718e3744 2419
d62a17ae 2420 if (host.logfile)
2421 XFREE(MTYPE_HOST, host.logfile);
718e3744 2422
d62a17ae 2423 host.logfile = XSTRDUP(MTYPE_HOST, fname);
718e3744 2424
c05795b1 2425#if defined(HAVE_CUMULUS)
d62a17ae 2426 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED)
0204baa8 2427 zlog_set_level(ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
c05795b1 2428#endif
d62a17ae 2429 return CMD_SUCCESS;
718e3744 2430}
2431
e9b4e74a 2432void command_setup_early_logging(const char *dest, const char *level)
f8507817
DS
2433{
2434 char *token;
2435
e9b4e74a
DS
2436 if (level) {
2437 int nlevel = level_match(level);
2438
2439 if (nlevel != ZLOG_DISABLED)
2440 zlog_default->default_lvl = nlevel;
2441 }
2442
2443 if (!dest)
2444 return;
2445
2446 if (strcmp(dest, "stdout") == 0) {
f8507817
DS
2447 zlog_set_level(ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2448 return;
2449 }
2450
e9b4e74a 2451 if (strcmp(dest, "syslog") == 0) {
f8507817
DS
2452 zlog_set_level(ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2453 return;
2454 }
2455
e9b4e74a 2456 token = strstr(dest, ":");
74e7158c 2457 if (token == NULL)
2458 return;
2459
f8507817
DS
2460 token++;
2461
2462 set_log_file(NULL, token, zlog_default->default_lvl);
2463}
2464
274a4a44 2465DEFUN (config_log_file,
2466 config_log_file_cmd,
6de69f83 2467 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
274a4a44 2468 "Logging control\n"
2469 "Logging to file\n"
2470 "Logging filename\n"
2471 LOG_LEVEL_DESC)
2472{
d62a17ae 2473 int idx_filename = 2;
2474 int idx_log_levels = 3;
2475 if (argc == 4) {
2476 int level;
2477 if ((level = level_match(argv[idx_log_levels]->arg))
2478 == ZLOG_DISABLED)
2479 return CMD_ERR_NO_MATCH;
2480 return set_log_file(vty, argv[idx_filename]->arg, level);
2481 } else
2482 return set_log_file(vty, argv[idx_filename]->arg,
2483 zlog_default->default_lvl);
274a4a44 2484}
2485
914432b5 2486static void disable_log_file(void)
0204baa8
DW
2487{
2488 zlog_reset_file();
2489
2490 if (host.logfile)
2491 XFREE(MTYPE_HOST, host.logfile);
2492
2493 host.logfile = NULL;
2494}
2495
718e3744 2496DEFUN (no_config_log_file,
2497 no_config_log_file_cmd,
d0bfb22c 2498 "no log file [FILENAME [LEVEL]]",
718e3744 2499 NO_STR
2500 "Logging control\n"
2501 "Cancel logging to file\n"
d0bfb22c 2502 "Logging file name\n"
d0bfb22c 2503 "Logging level\n")
718e3744 2504{
0204baa8 2505 disable_log_file();
d62a17ae 2506 return CMD_SUCCESS;
718e3744 2507}
2508
2509DEFUN (config_log_syslog,
2510 config_log_syslog_cmd,
6de69f83 2511 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
12ab19f1 2512 "Logging control\n"
274a4a44 2513 "Set syslog logging level\n"
2514 LOG_LEVEL_DESC)
2515{
d62a17ae 2516 int idx_log_levels = 2;
0204baa8 2517
d62a17ae 2518 if (argc == 3) {
2519 int level;
2520 if ((level = level_match(argv[idx_log_levels]->arg))
2521 == ZLOG_DISABLED)
2522 return CMD_ERR_NO_MATCH;
2523 zlog_set_level(ZLOG_DEST_SYSLOG, level);
2524 return CMD_SUCCESS;
2525 } else {
2526 zlog_set_level(ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2527 return CMD_SUCCESS;
2528 }
274a4a44 2529}
2530
718e3744 2531DEFUN (no_config_log_syslog,
2532 no_config_log_syslog_cmd,
6de69f83 2533 "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 2534 NO_STR
2535 "Logging control\n"
274a4a44 2536 "Cancel logging to syslog\n"
d0bfb22c
QY
2537 LOG_FACILITY_DESC
2538 LOG_LEVEL_DESC)
718e3744 2539{
d62a17ae 2540 zlog_set_level(ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
2541 return CMD_SUCCESS;
718e3744 2542}
2543
274a4a44 2544DEFUN (config_log_facility,
2545 config_log_facility_cmd,
9ccf14f7 2546 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
718e3744 2547 "Logging control\n"
274a4a44 2548 "Facility parameter for syslog messages\n"
2549 LOG_FACILITY_DESC)
718e3744 2550{
d62a17ae 2551 int idx_target = 2;
2552 int facility = facility_match(argv[idx_target]->arg);
274a4a44 2553
d62a17ae 2554 zlog_default->facility = facility;
2555 return CMD_SUCCESS;
718e3744 2556}
2557
274a4a44 2558DEFUN (no_config_log_facility,
2559 no_config_log_facility_cmd,
9ccf14f7 2560 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
718e3744 2561 NO_STR
2562 "Logging control\n"
274a4a44 2563 "Reset syslog facility to default (daemon)\n"
d0bfb22c 2564 LOG_FACILITY_DESC)
274a4a44 2565{
d62a17ae 2566 zlog_default->facility = LOG_DAEMON;
2567 return CMD_SUCCESS;
274a4a44 2568}
2569
718e3744 2570DEFUN (config_log_record_priority,
2571 config_log_record_priority_cmd,
2572 "log record-priority",
2573 "Logging control\n"
2574 "Log the priority of the message within the message\n")
2575{
d62a17ae 2576 zlog_default->record_priority = 1;
2577 return CMD_SUCCESS;
718e3744 2578}
2579
2580DEFUN (no_config_log_record_priority,
2581 no_config_log_record_priority_cmd,
2582 "no log record-priority",
2583 NO_STR
2584 "Logging control\n"
2585 "Do not log the priority of the message within the message\n")
2586{
d62a17ae 2587 zlog_default->record_priority = 0;
2588 return CMD_SUCCESS;
718e3744 2589}
2590
1ed72e0b
AS
2591DEFUN (config_log_timestamp_precision,
2592 config_log_timestamp_precision_cmd,
6147e2c6 2593 "log timestamp precision (0-6)",
1ed72e0b
AS
2594 "Logging control\n"
2595 "Timestamp configuration\n"
2596 "Set the timestamp precision\n"
2597 "Number of subsecond digits\n")
2598{
d62a17ae 2599 int idx_number = 3;
2600 zlog_default->timestamp_precision =
2601 strtoul(argv[idx_number]->arg, NULL, 10);
2602 return CMD_SUCCESS;
1ed72e0b
AS
2603}
2604
2605DEFUN (no_config_log_timestamp_precision,
2606 no_config_log_timestamp_precision_cmd,
2607 "no log timestamp precision",
2608 NO_STR
2609 "Logging control\n"
2610 "Timestamp configuration\n"
2611 "Reset the timestamp precision to the default value of 0\n")
2612{
d62a17ae 2613 zlog_default->timestamp_precision = 0;
2614 return CMD_SUCCESS;
1ed72e0b
AS
2615}
2616
9eed278b
DL
2617DEFUN (debug_memstats,
2618 debug_memstats_cmd,
2619 "[no] debug memstats-at-exit",
2620 NO_STR
2621 DEBUG_STR
2622 "Print memory type statistics at exit\n")
2623{
2624 debug_memstats_at_exit = !!strcmp(argv[0]->text, "no");
2625 return CMD_SUCCESS;
2626}
2627
d62a17ae 2628int cmd_banner_motd_file(const char *file)
7cfc61d3 2629{
d62a17ae 2630 int success = CMD_SUCCESS;
2631 char p[PATH_MAX];
2632 char *rpath;
2633 char *in;
7cfc61d3 2634
d62a17ae 2635 rpath = realpath(file, p);
2636 if (!rpath)
2637 return CMD_ERR_NO_FILE;
2638 in = strstr(rpath, SYSCONFDIR);
2639 if (in == rpath) {
2640 if (host.motdfile)
2641 XFREE(MTYPE_HOST, host.motdfile);
2642 host.motdfile = XSTRDUP(MTYPE_HOST, file);
2643 } else
2644 success = CMD_WARNING_CONFIG_FAILED;
1ee08155 2645
d62a17ae 2646 return success;
7cfc61d3
DS
2647}
2648
3b0c5d9a 2649DEFUN (banner_motd_file,
2650 banner_motd_file_cmd,
4d833e55 2651 "banner motd file FILE",
3b0c5d9a 2652 "Set banner\n"
2653 "Banner for motd\n"
2654 "Banner from a file\n"
2655 "Filename\n")
2656{
d62a17ae 2657 int idx_file = 3;
2658 const char *filename = argv[idx_file]->arg;
2659 int cmd = cmd_banner_motd_file(filename);
1ee08155 2660
d62a17ae 2661 if (cmd == CMD_ERR_NO_FILE)
2662 vty_out(vty, "%s does not exist", filename);
2663 else if (cmd == CMD_WARNING_CONFIG_FAILED)
2664 vty_out(vty, "%s must be in %s", filename, SYSCONFDIR);
1ee08155 2665
d62a17ae 2666 return cmd;
3b0c5d9a 2667}
718e3744 2668
2669DEFUN (banner_motd_default,
2670 banner_motd_default_cmd,
2671 "banner motd default",
2672 "Set banner string\n"
2673 "Strings for motd\n"
2674 "Default string\n")
2675{
d62a17ae 2676 host.motd = default_motd;
2677 return CMD_SUCCESS;
718e3744 2678}
2679
2680DEFUN (no_banner_motd,
2681 no_banner_motd_cmd,
2682 "no banner motd",
2683 NO_STR
2684 "Set banner string\n"
2685 "Strings for motd\n")
2686{
d62a17ae 2687 host.motd = NULL;
2688 if (host.motdfile)
2689 XFREE(MTYPE_HOST, host.motdfile);
2690 host.motdfile = NULL;
2691 return CMD_SUCCESS;
718e3744 2692}
2693
a83a5331
QY
2694DEFUN(find,
2695 find_cmd,
2696 "find COMMAND...",
2697 "Find CLI command containing text\n"
2698 "Text to search for\n")
2699{
2700 char *text = argv_concat(argv, argc, 1);
cf6c83e7
QY
2701 const struct cmd_node *node;
2702 const struct cmd_element *cli;
2703 vector clis;
a83a5331
QY
2704
2705 for (unsigned int i = 0; i < vector_active(cmdvec); i++) {
cf6c83e7 2706 node = vector_slot(cmdvec, i);
a83a5331
QY
2707 if (!node)
2708 continue;
cf6c83e7 2709 clis = node->cmd_vector;
a83a5331 2710 for (unsigned int j = 0; j < vector_active(clis); j++) {
cf6c83e7 2711 cli = vector_slot(clis, j);
a83a5331 2712 if (strcasestr(cli->string, text))
cf6c83e7 2713 vty_out(vty, " (%s) %s\n",
a83a5331
QY
2714 node_names[node->node], cli->string);
2715 }
2716 }
2717
2718 XFREE(MTYPE_TMP, text);
2719
2720 return CMD_SUCCESS;
2721}
2722
718e3744 2723/* Set config filename. Called from vty.c */
d62a17ae 2724void host_config_set(const char *filename)
718e3744 2725{
d62a17ae 2726 if (host.config)
2727 XFREE(MTYPE_HOST, host.config);
2728 host.config = XSTRDUP(MTYPE_HOST, filename);
718e3744 2729}
2730
d62a17ae 2731const char *host_config_get(void)
57387fb2 2732{
d62a17ae 2733 return host.config;
57387fb2
CF
2734}
2735
d62a17ae 2736void install_default(enum node_type node)
718e3744 2737{
d62a17ae 2738 install_element(node, &config_exit_cmd);
2739 install_element(node, &config_quit_cmd);
2740 install_element(node, &config_end_cmd);
2741 install_element(node, &config_help_cmd);
2742 install_element(node, &config_list_cmd);
26fbe472 2743 install_element(node, &show_cli_graph_cmd);
a83a5331 2744 install_element(node, &find_cmd);
718e3744 2745
d62a17ae 2746 install_element(node, &config_write_cmd);
2747 install_element(node, &show_running_config_cmd);
7f059ea6 2748
d62a17ae 2749 install_element(node, &autocomplete_cmd);
1c2facd1
RW
2750
2751 nb_cli_install_default(node);
718e3744 2752}
2753
87f44e2f
DL
2754/* Initialize command interface. Install basic nodes and commands.
2755 *
2756 * terminal = 0 -- vtysh / no logging, no config control
2757 * terminal = 1 -- normal daemon
9473e340 2758 * terminal = -1 -- watchfrr / no logging, but minimal config control */
d62a17ae 2759void cmd_init(int terminal)
2760{
419cd5a0
MK
2761 struct utsname names;
2762
a83a5331
QY
2763 if (array_size(node_names) != NODE_TYPE_MAX)
2764 assert(!"Update the CLI node description array!");
2765
419cd5a0 2766 uname(&names);
d62a17ae 2767 qobj_init();
2768
fe6b47b9
QY
2769 /* register command preprocessors */
2770 hook_register(cmd_execute, handle_pipe_action);
2771 hook_register(cmd_execute_done, handle_pipe_action_done);
2772
d62a17ae 2773 varhandlers = list_new();
2774
2775 /* Allocate initial top vector of commands. */
2776 cmdvec = vector_init(VECTOR_MIN_SIZE);
2777
2778 /* Default host value settings. */
419cd5a0
MK
2779 host.name = XSTRDUP(MTYPE_HOST, names.nodename);
2780#ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
6b3ee3a0
MK
2781 if ((strcmp(names.domainname, "(none)") == 0))
2782 host.domainname = NULL;
2783 else
2784 host.domainname = XSTRDUP(MTYPE_HOST, names.domainname);
419cd5a0
MK
2785#else
2786 host.domainname = NULL;
2787#endif
d62a17ae 2788 host.password = NULL;
2789 host.enable = NULL;
2790 host.logfile = NULL;
2791 host.config = NULL;
2792 host.noconfig = (terminal < 0);
2793 host.lines = -1;
2794 host.motd = default_motd;
2795 host.motdfile = NULL;
2796
2797 /* Install top nodes. */
2798 install_node(&view_node, NULL);
2799 install_node(&enable_node, NULL);
2800 install_node(&auth_node, NULL);
2801 install_node(&auth_enable_node, NULL);
2802 install_node(&config_node, config_write_host);
2803
2804 /* Each node's basic commands. */
2805 install_element(VIEW_NODE, &show_version_cmd);
a83a5331 2806 install_element(ENABLE_NODE, &show_startup_config_cmd);
9eed278b 2807 install_element(ENABLE_NODE, &debug_memstats_cmd);
a83a5331 2808
d62a17ae 2809 if (terminal) {
2810 install_element(VIEW_NODE, &config_list_cmd);
2811 install_element(VIEW_NODE, &config_exit_cmd);
2812 install_element(VIEW_NODE, &config_quit_cmd);
2813 install_element(VIEW_NODE, &config_help_cmd);
2814 install_element(VIEW_NODE, &config_enable_cmd);
2815 install_element(VIEW_NODE, &config_terminal_length_cmd);
2816 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
2817 install_element(VIEW_NODE, &show_logging_cmd);
2818 install_element(VIEW_NODE, &show_commandtree_cmd);
2819 install_element(VIEW_NODE, &echo_cmd);
2820 install_element(VIEW_NODE, &autocomplete_cmd);
a83a5331 2821 install_element(VIEW_NODE, &find_cmd);
d62a17ae 2822
d62a17ae 2823 install_element(ENABLE_NODE, &config_end_cmd);
2824 install_element(ENABLE_NODE, &config_disable_cmd);
2825 install_element(ENABLE_NODE, &config_terminal_cmd);
2826 install_element(ENABLE_NODE, &copy_runningconf_startupconf_cmd);
2827 install_element(ENABLE_NODE, &config_write_cmd);
2828 install_element(ENABLE_NODE, &show_running_config_cmd);
d62a17ae 2829 install_element(ENABLE_NODE, &config_logmsg_cmd);
a83a5331 2830
d62a17ae 2831 install_default(CONFIG_NODE);
2832
2833 thread_cmd_init();
2834 workqueue_cmd_init();
2835 hash_cmd_init();
2836 }
2837
2838 install_element(CONFIG_NODE, &hostname_cmd);
2839 install_element(CONFIG_NODE, &no_hostname_cmd);
6b3ee3a0
MK
2840 install_element(CONFIG_NODE, &domainname_cmd);
2841 install_element(CONFIG_NODE, &no_domainname_cmd);
d62a17ae 2842 install_element(CONFIG_NODE, &frr_version_defaults_cmd);
9eed278b 2843 install_element(CONFIG_NODE, &debug_memstats_cmd);
d62a17ae 2844
2845 if (terminal > 0) {
2846 install_element(CONFIG_NODE, &password_cmd);
322e2d5c 2847 install_element(CONFIG_NODE, &no_password_cmd);
d62a17ae 2848 install_element(CONFIG_NODE, &enable_password_cmd);
2849 install_element(CONFIG_NODE, &no_enable_password_cmd);
2850
2851 install_element(CONFIG_NODE, &config_log_stdout_cmd);
2852 install_element(CONFIG_NODE, &no_config_log_stdout_cmd);
2853 install_element(CONFIG_NODE, &config_log_monitor_cmd);
2854 install_element(CONFIG_NODE, &no_config_log_monitor_cmd);
2855 install_element(CONFIG_NODE, &config_log_file_cmd);
2856 install_element(CONFIG_NODE, &no_config_log_file_cmd);
2857 install_element(CONFIG_NODE, &config_log_syslog_cmd);
2858 install_element(CONFIG_NODE, &no_config_log_syslog_cmd);
2859 install_element(CONFIG_NODE, &config_log_facility_cmd);
2860 install_element(CONFIG_NODE, &no_config_log_facility_cmd);
d62a17ae 2861 install_element(CONFIG_NODE, &config_log_record_priority_cmd);
2862 install_element(CONFIG_NODE,
2863 &no_config_log_record_priority_cmd);
2864 install_element(CONFIG_NODE,
2865 &config_log_timestamp_precision_cmd);
2866 install_element(CONFIG_NODE,
2867 &no_config_log_timestamp_precision_cmd);
2868 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
2869 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
2870 install_element(CONFIG_NODE, &banner_motd_default_cmd);
2871 install_element(CONFIG_NODE, &banner_motd_file_cmd);
2872 install_element(CONFIG_NODE, &no_banner_motd_cmd);
2873 install_element(CONFIG_NODE, &service_terminal_length_cmd);
2874 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
2875
2876 vrf_install_commands();
2877 }
af2567b6
DL
2878
2879#ifdef DEV_BUILD
d62a17ae 2880 grammar_sandbox_init();
af2567b6 2881#endif
718e3744 2882}
228da428 2883
4d762f26 2884void cmd_terminate(void)
d62a17ae 2885{
2886 struct cmd_node *cmd_node;
2887
5d806ec6
QY
2888 hook_unregister(cmd_execute, handle_pipe_action);
2889 hook_unregister(cmd_execute_done, handle_pipe_action_done);
2890
d62a17ae 2891 if (cmdvec) {
2892 for (unsigned int i = 0; i < vector_active(cmdvec); i++)
2893 if ((cmd_node = vector_slot(cmdvec, i)) != NULL) {
2894 // deleting the graph delets the cmd_element as
2895 // well
2896 graph_delete_graph(cmd_node->cmdgraph);
2897 vector_free(cmd_node->cmd_vector);
2898 hash_clean(cmd_node->cmd_hash, NULL);
2899 hash_free(cmd_node->cmd_hash);
2900 cmd_node->cmd_hash = NULL;
2901 }
2902
2903 vector_free(cmdvec);
2904 cmdvec = NULL;
2905 }
2906
2907 if (host.name)
2908 XFREE(MTYPE_HOST, host.name);
419cd5a0
MK
2909 if (host.domainname)
2910 XFREE(MTYPE_HOST, host.domainname);
d62a17ae 2911 if (host.password)
2912 XFREE(MTYPE_HOST, host.password);
2913 if (host.password_encrypt)
2914 XFREE(MTYPE_HOST, host.password_encrypt);
2915 if (host.enable)
2916 XFREE(MTYPE_HOST, host.enable);
2917 if (host.enable_encrypt)
2918 XFREE(MTYPE_HOST, host.enable_encrypt);
2919 if (host.logfile)
2920 XFREE(MTYPE_HOST, host.logfile);
2921 if (host.motdfile)
2922 XFREE(MTYPE_HOST, host.motdfile);
2923 if (host.config)
2924 XFREE(MTYPE_HOST, host.config);
2925
6a154c88 2926 list_delete(&varhandlers);
d62a17ae 2927 qobj_finish();
228da428 2928}