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