]>
Commit | Line | Data |
---|---|---|
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 | 51 | DEFINE_MTYPE(LIB, HOST, "Host config") |
d62a17ae | 52 | DEFINE_MTYPE(LIB, COMPLETION, "Completion item") |
4a1ab8e4 | 53 | |
26fbe472 QY |
54 | #define item(x) \ |
55 | { \ | |
56 | x, #x \ | |
57 | } | |
58 | ||
59 | /* clang-format off */ | |
60 | const 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 |
77 | const 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 | 157 | vector cmdvec = NULL; |
718e3744 | 158 | |
159 | /* Host information structure. */ | |
160 | struct host host; | |
161 | ||
419cd5a0 MK |
162 | /* |
163 | * Returns host.name if any, otherwise | |
164 | * it returns the system hostname. | |
165 | */ | |
6b3ee3a0 | 166 | const char *cmd_hostname_get(void) |
419cd5a0 MK |
167 | { |
168 | return host.name; | |
169 | } | |
170 | ||
171 | /* | |
172 | * Returns unix domainname | |
173 | */ | |
6b3ee3a0 | 174 | const char *cmd_domainname_get(void) |
419cd5a0 MK |
175 | { |
176 | return host.domainname; | |
177 | } | |
178 | ||
718e3744 | 179 | /* Standard command node structures. */ |
d62a17ae | 180 | static struct cmd_node auth_node = { |
9d303b37 | 181 | AUTH_NODE, "Password: ", |
718e3744 | 182 | }; |
183 | ||
d62a17ae | 184 | static struct cmd_node view_node = { |
9d303b37 | 185 | VIEW_NODE, "%s> ", |
718e3744 | 186 | }; |
187 | ||
d62a17ae | 188 | static struct cmd_node auth_enable_node = { |
9d303b37 | 189 | AUTH_ENABLE_NODE, "Password: ", |
718e3744 | 190 | }; |
191 | ||
d62a17ae | 192 | static struct cmd_node enable_node = { |
9d303b37 | 193 | ENABLE_NODE, "%s# ", |
718e3744 | 194 | }; |
195 | ||
d62a17ae | 196 | static struct cmd_node config_node = {CONFIG_NODE, "%s(config)# ", 1}; |
6590f2c3 | 197 | |
198 | /* Default motd string. */ | |
b2f36157 | 199 | static const char *default_motd = FRR_DEFAULT_MOTD; |
274a4a44 | 200 | |
2d362d10 | 201 | static 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 | 230 | static 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 | 240 | static 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 | 250 | static 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 | 261 | void 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 | 268 | char *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 | ||
282 | vector 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 | ||
311 | void 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 | 326 | int 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 | 335 | static 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 | 342 | static 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 | 348 | void 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 | 364 | const 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 | 373 | void 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 | 422 | void 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 | 470 | static const unsigned char itoa64[] = |
d62a17ae | 471 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
718e3744 | 472 | |
d62a17ae | 473 | static 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 | 481 | static 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 | 497 | static 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 | 603 | static 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 | 609 | static 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 | 621 | static 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 | 636 | vector 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 | 686 | static 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 | 717 | vector 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 |
752 | static struct list *varhandlers = NULL; |
753 | ||
d62a17ae | 754 | void 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 | 790 | char *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 | 820 | void 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 |
829 | DEFUN_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 | 872 | char **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 | 954 | enum 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 | 1012 | static 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 | 1054 | else { |
e7ba083c RW |
1055 | if (vty->config) { |
1056 | /* Clear array of enqueued configuration changes. */ | |
1057 | vty->num_cfg_changes = 0; | |
1058 | memset(&vty->cfg_changes, 0, sizeof(vty->cfg_changes)); | |
1059 | ||
1060 | /* Regenerate candidate configuration. */ | |
1061 | if (frr_get_cli_mode() == FRR_CLI_CLASSIC) | |
1062 | nb_config_replace(vty->candidate_config, | |
1063 | running_config, true); | |
1064 | } | |
a6233bfc | 1065 | |
d62a17ae | 1066 | ret = matched_element->func(matched_element, vty, argc, argv); |
a6233bfc | 1067 | } |
d62a17ae | 1068 | |
1069 | // delete list and cmd_token's in it | |
6a154c88 | 1070 | list_delete(&argv_list); |
d62a17ae | 1071 | XFREE(MTYPE_TMP, argv); |
1072 | ||
1073 | return ret; | |
718e3744 | 1074 | } |
1075 | ||
cd40b329 CF |
1076 | /** |
1077 | * Execute a given command, handling things like "do ..." and checking | |
1078 | * whether the given command might apply at a parent node if doesn't | |
1079 | * apply for the current node. | |
1080 | * | |
1081 | * @param vline Command line input, vector of char* where each element is | |
1082 | * one input token. | |
1083 | * @param vty The vty context in which the command should be executed. | |
1084 | * @param cmd Pointer where the struct cmd_element of the matched command | |
1085 | * will be stored, if any. May be set to NULL if this info is | |
1086 | * not needed. | |
1087 | * @param vtysh If set != 0, don't lookup the command at parent nodes. | |
1088 | * @return The status of the command that has been executed or an error code | |
1089 | * as to why no command could be executed. | |
1090 | */ | |
d62a17ae | 1091 | int cmd_execute_command(vector vline, struct vty *vty, |
1092 | const struct cmd_element **cmd, int vtysh) | |
17aca20b | 1093 | { |
d62a17ae | 1094 | int ret, saved_ret = 0; |
1095 | enum node_type onode, try_node; | |
1c2facd1 | 1096 | int orig_xpath_index; |
eda031f6 | 1097 | |
d62a17ae | 1098 | onode = try_node = vty->node; |
1c2facd1 | 1099 | orig_xpath_index = vty->xpath_index; |
b92938a7 | 1100 | |
d62a17ae | 1101 | if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) { |
1102 | vector shifted_vline; | |
1103 | unsigned int index; | |
b92938a7 | 1104 | |
d62a17ae | 1105 | vty->node = ENABLE_NODE; |
1c2facd1 | 1106 | vty->xpath_index = 0; |
d62a17ae | 1107 | /* We can try it on enable node, cos' the vty is authenticated |
1108 | */ | |
b92938a7 | 1109 | |
d62a17ae | 1110 | shifted_vline = vector_init(vector_count(vline)); |
1111 | /* use memcpy? */ | |
1112 | for (index = 1; index < vector_active(vline); index++) | |
1113 | vector_set_index(shifted_vline, index - 1, | |
1114 | vector_lookup(vline, index)); | |
b92938a7 | 1115 | |
d62a17ae | 1116 | ret = cmd_execute_command_real(shifted_vline, FILTER_RELAXED, |
1117 | vty, cmd); | |
b92938a7 | 1118 | |
d62a17ae | 1119 | vector_free(shifted_vline); |
1120 | vty->node = onode; | |
1c2facd1 | 1121 | vty->xpath_index = orig_xpath_index; |
d62a17ae | 1122 | return ret; |
1123 | } | |
b92938a7 | 1124 | |
d62a17ae | 1125 | saved_ret = ret = |
1126 | cmd_execute_command_real(vline, FILTER_RELAXED, vty, cmd); | |
b92938a7 | 1127 | |
d62a17ae | 1128 | if (vtysh) |
1129 | return saved_ret; | |
87d683b0 | 1130 | |
825d5fbf CS |
1131 | if (ret != CMD_SUCCESS && ret != CMD_WARNING |
1132 | && ret != CMD_NOT_MY_INSTANCE && ret != CMD_WARNING_CONFIG_FAILED) { | |
d62a17ae | 1133 | /* This assumes all nodes above CONFIG_NODE are childs of |
1134 | * CONFIG_NODE */ | |
1135 | while (vty->node > CONFIG_NODE) { | |
1136 | try_node = node_parent(try_node); | |
1137 | vty->node = try_node; | |
1c2facd1 RW |
1138 | if (vty->xpath_index > 0) |
1139 | vty->xpath_index--; | |
d62a17ae | 1140 | ret = cmd_execute_command_real(vline, FILTER_RELAXED, |
1141 | vty, cmd); | |
825d5fbf CS |
1142 | if (ret == CMD_SUCCESS || ret == CMD_WARNING |
1143 | || ret == CMD_NOT_MY_INSTANCE | |
1144 | || ret == CMD_WARNING_CONFIG_FAILED) | |
d62a17ae | 1145 | return ret; |
1146 | } | |
1147 | /* no command succeeded, reset the vty to the original node */ | |
1148 | vty->node = onode; | |
1c2facd1 | 1149 | vty->xpath_index = orig_xpath_index; |
d62a17ae | 1150 | } |
04e64062 | 1151 | |
d62a17ae | 1152 | /* return command status for original node */ |
1153 | return saved_ret; | |
b92938a7 | 1154 | } |
1155 | ||
cd40b329 CF |
1156 | /** |
1157 | * Execute a given command, matching it strictly against the current node. | |
1158 | * This mode is used when reading config files. | |
1159 | * | |
1160 | * @param vline Command line input, vector of char* where each element is | |
1161 | * one input token. | |
1162 | * @param vty The vty context in which the command should be executed. | |
1163 | * @param cmd Pointer where the struct cmd_element* of the matched command | |
1164 | * will be stored, if any. May be set to NULL if this info is | |
1165 | * not needed. | |
1166 | * @return The status of the command that has been executed or an error code | |
1167 | * as to why no command could be executed. | |
1168 | */ | |
d62a17ae | 1169 | int cmd_execute_command_strict(vector vline, struct vty *vty, |
1170 | const struct cmd_element **cmd) | |
718e3744 | 1171 | { |
d62a17ae | 1172 | return cmd_execute_command_real(vline, FILTER_STRICT, vty, cmd); |
718e3744 | 1173 | } |
1174 | ||
01e24c4a QY |
1175 | /* |
1176 | * Hook for preprocessing command string before executing. | |
1177 | * | |
1178 | * All subscribers are called with the raw command string that is to be | |
1179 | * executed. If any changes are to be made, a new string should be allocated | |
1180 | * with MTYPE_TMP and *cmd_out updated to point to this new string. The caller | |
1181 | * is then responsible for freeing this string. | |
1182 | * | |
1183 | * All processing functions must be mutually exclusive in their action, i.e. if | |
1184 | * one subscriber decides to modify the command, all others must not modify it | |
1185 | * when called. Feeding the output of one processing command into a subsequent | |
1186 | * one is not supported. | |
1187 | * | |
1188 | * This hook is intentionally internal to the command processing system. | |
1189 | * | |
1190 | * cmd_in | |
1191 | * The raw command string. | |
1192 | * | |
1193 | * cmd_out | |
1194 | * The result of any processing. | |
1195 | */ | |
1196 | DECLARE_HOOK(cmd_execute, | |
0a334343 | 1197 | (struct vty *vty, const char *cmd_in, char **cmd_out), |
01e24c4a | 1198 | (vty, cmd_in, cmd_out)); |
0a334343 | 1199 | DEFINE_HOOK(cmd_execute, (struct vty *vty, const char *cmd_in, char **cmd_out), |
01e24c4a QY |
1200 | (vty, cmd_in, cmd_out)); |
1201 | ||
1202 | /* Hook executed after a CLI command. */ | |
0a334343 | 1203 | DECLARE_KOOH(cmd_execute_done, (struct vty *vty, const char *cmd_exec), |
01e24c4a | 1204 | (vty, cmd_exec)); |
0a334343 | 1205 | DEFINE_KOOH(cmd_execute_done, (struct vty *vty, const char *cmd_exec), |
01e24c4a QY |
1206 | (vty, cmd_exec)); |
1207 | ||
fe6b47b9 QY |
1208 | /* |
1209 | * cmd_execute hook subscriber to handle `|` actions. | |
1210 | */ | |
1211 | static int handle_pipe_action(struct vty *vty, const char *cmd_in, | |
1212 | char **cmd_out) | |
1213 | { | |
1214 | /* look for `|` */ | |
9f227e4c | 1215 | char *orig, *working, *token, *u; |
fe6b47b9 QY |
1216 | char *pipe = strstr(cmd_in, "| "); |
1217 | ||
1218 | if (!pipe) | |
1219 | return 0; | |
1220 | ||
1221 | /* duplicate string for processing purposes, not including pipe */ | |
1222 | orig = working = XSTRDUP(MTYPE_TMP, pipe + 2); | |
1223 | ||
1224 | /* retrieve action */ | |
1225 | token = strsep(&working, " "); | |
4f4060f6 | 1226 | assert(token); |
fe6b47b9 QY |
1227 | |
1228 | /* match result to known actions */ | |
1229 | if (strmatch(token, "include")) { | |
1230 | /* the remaining text should be a regexp */ | |
1231 | char *regexp = working; | |
5d806ec6 QY |
1232 | |
1233 | if (!regexp) { | |
1234 | vty_out(vty, "%% Need a regexp to filter with\n"); | |
1235 | goto fail; | |
1236 | } | |
1237 | ||
fe6b47b9 | 1238 | bool succ = vty_set_include(vty, regexp); |
0a334343 | 1239 | |
fe6b47b9 | 1240 | if (!succ) { |
5d806ec6 | 1241 | vty_out(vty, "%% Bad regexp '%s'\n", regexp); |
fe6b47b9 QY |
1242 | goto fail; |
1243 | } | |
2cddf2ff | 1244 | *cmd_out = XSTRDUP(MTYPE_TMP, cmd_in); |
9f227e4c | 1245 | u = *cmd_out; |
1246 | strsep(&u, "|"); | |
fe6b47b9 | 1247 | } else { |
5d806ec6 | 1248 | vty_out(vty, "%% Unknown action '%s'\n", token); |
fe6b47b9 QY |
1249 | goto fail; |
1250 | } | |
1251 | ||
1252 | fail: | |
1253 | XFREE(MTYPE_TMP, orig); | |
1254 | return 0; | |
1255 | } | |
1256 | ||
1257 | static int handle_pipe_action_done(struct vty *vty, const char *cmd_exec) | |
1258 | { | |
0b42d81a | 1259 | if (vty->filter) |
fe6b47b9 | 1260 | vty_set_include(vty, NULL); |
0b42d81a | 1261 | |
fe6b47b9 QY |
1262 | return 0; |
1263 | } | |
1264 | ||
01e24c4a QY |
1265 | int cmd_execute(struct vty *vty, const char *cmd, |
1266 | const struct cmd_element **matched, int vtysh) | |
1267 | { | |
1268 | int ret; | |
1269 | char *cmd_out = NULL; | |
1270 | const char *cmd_exec; | |
1271 | vector vline; | |
1272 | ||
1273 | hook_call(cmd_execute, vty, cmd, &cmd_out); | |
1274 | cmd_exec = cmd_out ? (const char *)cmd_out : cmd; | |
1275 | ||
1276 | vline = cmd_make_strvec(cmd_exec); | |
1277 | ||
1278 | if (vline) { | |
1279 | ret = cmd_execute_command(vline, vty, matched, vtysh); | |
1280 | cmd_free_strvec(vline); | |
1281 | } else { | |
1282 | ret = CMD_SUCCESS; | |
1283 | } | |
1284 | ||
1285 | hook_call(cmd_execute_done, vty, cmd_exec); | |
1286 | ||
0a22ddfb | 1287 | XFREE(MTYPE_TMP, cmd_out); |
01e24c4a QY |
1288 | |
1289 | return ret; | |
1290 | } | |
1291 | ||
1292 | ||
bed578b8 | 1293 | /** |
d62a17ae | 1294 | * Parse one line of config, walking up the parse tree attempting to find a |
1295 | * match | |
bed578b8 DS |
1296 | * |
1297 | * @param vty The vty context in which the command should be executed. | |
1298 | * @param cmd Pointer where the struct cmd_element* of the match command | |
1299 | * will be stored, if any. May be set to NULL if this info is | |
1300 | * not needed. | |
d62a17ae | 1301 | * @param use_daemon Boolean to control whether or not we match on |
1302 | * CMD_SUCCESS_DAEMON | |
bed578b8 DS |
1303 | * or not. |
1304 | * @return The status of the command that has been executed or an error code | |
1305 | * as to why no command could be executed. | |
1306 | */ | |
d62a17ae | 1307 | int command_config_read_one_line(struct vty *vty, |
7ab57d19 DS |
1308 | const struct cmd_element **cmd, |
1309 | uint32_t line_num, int use_daemon) | |
bed578b8 | 1310 | { |
d62a17ae | 1311 | vector vline; |
d62a17ae | 1312 | int ret; |
bed578b8 | 1313 | |
d62a17ae | 1314 | vline = cmd_make_strvec(vty->buf); |
bed578b8 | 1315 | |
d62a17ae | 1316 | /* In case of comment line */ |
1317 | if (vline == NULL) | |
1318 | return CMD_SUCCESS; | |
bed578b8 | 1319 | |
d62a17ae | 1320 | /* Execute configuration command : this is strict match */ |
1321 | ret = cmd_execute_command_strict(vline, vty, cmd); | |
bed578b8 | 1322 | |
d62a17ae | 1323 | // Climb the tree and try the command again at each node |
1324 | if (!(use_daemon && ret == CMD_SUCCESS_DAEMON) | |
1325 | && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO) | |
1326 | && ret != CMD_SUCCESS && ret != CMD_WARNING | |
996c9314 | 1327 | && ret != CMD_NOT_MY_INSTANCE && ret != CMD_WARNING_CONFIG_FAILED |
d62a17ae | 1328 | && vty->node != CONFIG_NODE) { |
1c2facd1 RW |
1329 | int saved_node = vty->node; |
1330 | int saved_xpath_index = vty->xpath_index; | |
bed578b8 | 1331 | |
d62a17ae | 1332 | while (!(use_daemon && ret == CMD_SUCCESS_DAEMON) |
1333 | && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO) | |
1334 | && ret != CMD_SUCCESS && ret != CMD_WARNING | |
1335 | && vty->node > CONFIG_NODE) { | |
1336 | vty->node = node_parent(vty->node); | |
1c2facd1 RW |
1337 | if (vty->xpath_index > 0) |
1338 | vty->xpath_index--; | |
d62a17ae | 1339 | ret = cmd_execute_command_strict(vline, vty, cmd); |
1340 | } | |
bed578b8 | 1341 | |
d62a17ae | 1342 | // If climbing the tree did not work then ignore the command and |
1343 | // stay at the same node | |
1344 | if (!(use_daemon && ret == CMD_SUCCESS_DAEMON) | |
1345 | && !(!use_daemon && ret == CMD_ERR_NOTHING_TODO) | |
1346 | && ret != CMD_SUCCESS && ret != CMD_WARNING) { | |
1347 | vty->node = saved_node; | |
1c2facd1 | 1348 | vty->xpath_index = saved_xpath_index; |
d62a17ae | 1349 | } |
1350 | } | |
bed578b8 | 1351 | |
b45d8ccc DS |
1352 | if (ret != CMD_SUCCESS && |
1353 | ret != CMD_WARNING && | |
1354 | ret != CMD_SUCCESS_DAEMON) { | |
7ab57d19 DS |
1355 | struct vty_error *ve = XCALLOC(MTYPE_TMP, sizeof(*ve)); |
1356 | ||
1357 | memcpy(ve->error_buf, vty->buf, VTY_BUFSIZ); | |
1358 | ve->line_num = line_num; | |
1359 | if (!vty->error) | |
1360 | vty->error = list_new(); | |
1361 | ||
1362 | listnode_add(vty->error, ve); | |
1363 | } | |
cbd7259d | 1364 | |
d62a17ae | 1365 | cmd_free_strvec(vline); |
bed578b8 | 1366 | |
d62a17ae | 1367 | return ret; |
bed578b8 DS |
1368 | } |
1369 | ||
5689fe5f | 1370 | /* Configuration make from file. */ |
d62a17ae | 1371 | int config_from_file(struct vty *vty, FILE *fp, unsigned int *line_num) |
718e3744 | 1372 | { |
d62a17ae | 1373 | int ret, error_ret = 0; |
1374 | *line_num = 0; | |
718e3744 | 1375 | |
d62a17ae | 1376 | while (fgets(vty->buf, VTY_BUFSIZ, fp)) { |
7ab57d19 | 1377 | ++(*line_num); |
13fbc82d | 1378 | |
7ab57d19 | 1379 | ret = command_config_read_one_line(vty, NULL, *line_num, 0); |
718e3744 | 1380 | |
d62a17ae | 1381 | if (ret != CMD_SUCCESS && ret != CMD_WARNING |
1382 | && ret != CMD_ERR_NOTHING_TODO) | |
1383 | error_ret = ret; | |
1384 | } | |
5689fe5f | 1385 | |
d62a17ae | 1386 | if (error_ret) { |
1387 | return error_ret; | |
1388 | } | |
5689fe5f | 1389 | |
d62a17ae | 1390 | return CMD_SUCCESS; |
718e3744 | 1391 | } |
1392 | ||
5689fe5f | 1393 | /* Configuration from terminal */ |
718e3744 | 1394 | DEFUN (config_terminal, |
1395 | config_terminal_cmd, | |
1396 | "configure terminal", | |
1397 | "Configuration from vty interface\n" | |
1398 | "Configuration terminal\n") | |
1399 | { | |
f344c66e | 1400 | return vty_config_enter(vty, false, false); |
718e3744 | 1401 | } |
1402 | ||
1403 | /* Enable command */ | |
d0bfb22c | 1404 | DEFUN (enable, |
718e3744 | 1405 | config_enable_cmd, |
1406 | "enable", | |
1407 | "Turn on privileged mode command\n") | |
1408 | { | |
d62a17ae | 1409 | /* If enable password is NULL, change to ENABLE_NODE */ |
1410 | if ((host.enable == NULL && host.enable_encrypt == NULL) | |
1411 | || vty->type == VTY_SHELL_SERV) | |
1412 | vty->node = ENABLE_NODE; | |
1413 | else | |
1414 | vty->node = AUTH_ENABLE_NODE; | |
718e3744 | 1415 | |
d62a17ae | 1416 | return CMD_SUCCESS; |
718e3744 | 1417 | } |
1418 | ||
1419 | /* Disable command */ | |
d0bfb22c | 1420 | DEFUN (disable, |
718e3744 | 1421 | config_disable_cmd, |
1422 | "disable", | |
1423 | "Turn off privileged mode command\n") | |
1424 | { | |
d62a17ae | 1425 | if (vty->node == ENABLE_NODE) |
1426 | vty->node = VIEW_NODE; | |
1427 | return CMD_SUCCESS; | |
718e3744 | 1428 | } |
1429 | ||
1430 | /* Down vty node level. */ | |
1431 | DEFUN (config_exit, | |
1432 | config_exit_cmd, | |
1433 | "exit", | |
1434 | "Exit current mode and down to previous mode\n") | |
0b84f294 | 1435 | { |
d62a17ae | 1436 | cmd_exit(vty); |
1437 | return CMD_SUCCESS; | |
1438 | } | |
1439 | ||
1440 | void cmd_exit(struct vty *vty) | |
1441 | { | |
1442 | switch (vty->node) { | |
1443 | case VIEW_NODE: | |
1444 | case ENABLE_NODE: | |
1445 | if (vty_shell(vty)) | |
1446 | exit(0); | |
1447 | else | |
1448 | vty->status = VTY_CLOSE; | |
1449 | break; | |
1450 | case CONFIG_NODE: | |
1451 | vty->node = ENABLE_NODE; | |
f344c66e | 1452 | vty_config_exit(vty); |
d62a17ae | 1453 | break; |
1454 | case INTERFACE_NODE: | |
2dd0d726 | 1455 | case PW_NODE: |
e26aedbe | 1456 | case LOGICALROUTER_NODE: |
d62a17ae | 1457 | case VRF_NODE: |
dba32923 | 1458 | case NH_GROUP_NODE: |
d62a17ae | 1459 | case ZEBRA_NODE: |
1460 | case BGP_NODE: | |
1461 | case RIP_NODE: | |
1462 | case EIGRP_NODE: | |
1463 | case BABEL_NODE: | |
1464 | case RIPNG_NODE: | |
1465 | case OSPF_NODE: | |
1466 | case OSPF6_NODE: | |
1467 | case LDP_NODE: | |
1468 | case LDP_L2VPN_NODE: | |
1469 | case ISIS_NODE: | |
7c0cbd0e | 1470 | case OPENFABRIC_NODE: |
d62a17ae | 1471 | case KEYCHAIN_NODE: |
d62a17ae | 1472 | case RMAP_NODE: |
e5c83d9b | 1473 | case PBRMAP_NODE: |
d62a17ae | 1474 | case VTY_NODE: |
c2f29cf3 | 1475 | case BFD_NODE: |
d62a17ae | 1476 | vty->node = CONFIG_NODE; |
1477 | break; | |
1478 | case BGP_IPV4_NODE: | |
1479 | case BGP_IPV4M_NODE: | |
1480 | case BGP_IPV4L_NODE: | |
1481 | case BGP_VPNV4_NODE: | |
1482 | case BGP_VPNV6_NODE: | |
7c40bf39 | 1483 | case BGP_FLOWSPECV4_NODE: |
1484 | case BGP_FLOWSPECV6_NODE: | |
d62a17ae | 1485 | case BGP_VRF_POLICY_NODE: |
1486 | case BGP_VNC_DEFAULTS_NODE: | |
1487 | case BGP_VNC_NVE_GROUP_NODE: | |
1488 | case BGP_VNC_L2_GROUP_NODE: | |
1489 | case BGP_IPV6_NODE: | |
1490 | case BGP_IPV6M_NODE: | |
1491 | case BGP_EVPN_NODE: | |
1492 | case BGP_IPV6L_NODE: | |
1493 | vty->node = BGP_NODE; | |
1494 | break; | |
1495 | case BGP_EVPN_VNI_NODE: | |
1496 | vty->node = BGP_EVPN_NODE; | |
1497 | break; | |
1498 | case LDP_IPV4_NODE: | |
1499 | case LDP_IPV6_NODE: | |
1500 | vty->node = LDP_NODE; | |
1501 | break; | |
1502 | case LDP_IPV4_IFACE_NODE: | |
1503 | vty->node = LDP_IPV4_NODE; | |
1504 | break; | |
1505 | case LDP_IPV6_IFACE_NODE: | |
1506 | vty->node = LDP_IPV6_NODE; | |
1507 | break; | |
1508 | case LDP_PSEUDOWIRE_NODE: | |
1509 | vty->node = LDP_L2VPN_NODE; | |
1510 | break; | |
1511 | case KEYCHAIN_KEY_NODE: | |
1512 | vty->node = KEYCHAIN_NODE; | |
1513 | break; | |
1514 | case LINK_PARAMS_NODE: | |
1515 | vty->node = INTERFACE_NODE; | |
1516 | break; | |
c2f29cf3 RZ |
1517 | case BFD_PEER_NODE: |
1518 | vty->node = BFD_NODE; | |
1519 | break; | |
d62a17ae | 1520 | default: |
1521 | break; | |
1522 | } | |
1c2facd1 RW |
1523 | |
1524 | if (vty->xpath_index > 0) | |
1525 | vty->xpath_index--; | |
718e3744 | 1526 | } |
1527 | ||
f667a580 QY |
1528 | /* ALIAS_FIXME */ |
1529 | DEFUN (config_quit, | |
1530 | config_quit_cmd, | |
1531 | "quit", | |
1532 | "Exit current mode and down to previous mode\n") | |
1533 | { | |
d62a17ae | 1534 | return config_exit(self, vty, argc, argv); |
f667a580 QY |
1535 | } |
1536 | ||
d0bfb22c | 1537 | |
718e3744 | 1538 | /* End of configuration. */ |
1539 | DEFUN (config_end, | |
1540 | config_end_cmd, | |
1541 | "end", | |
efd7904e | 1542 | "End current mode and change to enable mode.\n") |
718e3744 | 1543 | { |
cf09d3ca | 1544 | if (vty->config) { |
f344c66e | 1545 | vty_config_exit(vty); |
d62a17ae | 1546 | vty->node = ENABLE_NODE; |
d62a17ae | 1547 | } |
1c2facd1 | 1548 | |
d62a17ae | 1549 | return CMD_SUCCESS; |
718e3744 | 1550 | } |
1551 | ||
1552 | /* Show version. */ | |
1553 | DEFUN (show_version, | |
1554 | show_version_cmd, | |
1555 | "show version", | |
1556 | SHOW_STR | |
1557 | "Displays zebra version\n") | |
1558 | { | |
d62a17ae | 1559 | vty_out(vty, "%s %s (%s).\n", FRR_FULL_NAME, FRR_VERSION, |
6b3ee3a0 | 1560 | cmd_hostname_get() ? cmd_hostname_get() : ""); |
d62a17ae | 1561 | vty_out(vty, "%s%s\n", FRR_COPYRIGHT, GIT_INFO); |
1562 | vty_out(vty, "configured with:\n %s\n", FRR_CONFIG_ARGS); | |
718e3744 | 1563 | |
d62a17ae | 1564 | return CMD_SUCCESS; |
718e3744 | 1565 | } |
1566 | ||
8efe88ea DL |
1567 | /* "Set" version ... ignore version tags */ |
1568 | DEFUN (frr_version_defaults, | |
1569 | frr_version_defaults_cmd, | |
3e7c8d04 | 1570 | "frr <version|defaults> LINE...", |
447a8fe9 | 1571 | "FRRouting global parameters\n" |
8efe88ea DL |
1572 | "version configuration was written by\n" |
1573 | "set of configuration defaults used\n" | |
1574 | "version string\n") | |
1575 | { | |
d62a17ae | 1576 | return CMD_SUCCESS; |
8efe88ea DL |
1577 | } |
1578 | ||
718e3744 | 1579 | /* Help display function for all node. */ |
1580 | DEFUN (config_help, | |
1581 | config_help_cmd, | |
1582 | "help", | |
1583 | "Description of the interactive help system\n") | |
1584 | { | |
d62a17ae | 1585 | vty_out(vty, |
1586 | "Quagga VTY provides advanced help feature. When you need help,\n\ | |
61b7d449 DL |
1587 | anytime at the command line please press '?'.\n\ |
1588 | \n\ | |
1589 | If nothing matches, the help list will be empty and you must backup\n\ | |
1590 | until entering a '?' shows the available options.\n\ | |
1591 | Two styles of help are provided:\n\ | |
1592 | 1. Full help is available when you are ready to enter a\n\ | |
1593 | command argument (e.g. 'show ?') and describes each possible\n\ | |
1594 | argument.\n\ | |
1595 | 2. Partial help is provided when an abbreviated argument is entered\n\ | |
1596 | and you want to know what arguments match the input\n\ | |
1597 | (e.g. 'show me?'.)\n\n"); | |
d62a17ae | 1598 | return CMD_SUCCESS; |
1599 | } | |
1600 | ||
1601 | static void permute(struct graph_node *start, struct vty *vty) | |
1602 | { | |
1603 | static struct list *position = NULL; | |
1604 | if (!position) | |
1605 | position = list_new(); | |
1606 | ||
1607 | struct cmd_token *stok = start->data; | |
1608 | struct graph_node *gnn; | |
1609 | struct listnode *ln; | |
1610 | ||
1611 | // recursive dfs | |
1612 | listnode_add(position, start); | |
1613 | for (unsigned int i = 0; i < vector_active(start->to); i++) { | |
1614 | struct graph_node *gn = vector_slot(start->to, i); | |
1615 | struct cmd_token *tok = gn->data; | |
1616 | if (tok->attr == CMD_ATTR_HIDDEN | |
1617 | || tok->attr == CMD_ATTR_DEPRECATED) | |
1618 | continue; | |
1619 | else if (tok->type == END_TKN || gn == start) { | |
1620 | vty_out(vty, " "); | |
1621 | for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) { | |
1622 | struct cmd_token *tt = gnn->data; | |
1623 | if (tt->type < SPECIAL_TKN) | |
1624 | vty_out(vty, " %s", tt->text); | |
1625 | } | |
1626 | if (gn == start) | |
1627 | vty_out(vty, "..."); | |
1628 | vty_out(vty, "\n"); | |
1629 | } else { | |
1630 | bool skip = false; | |
1631 | if (stok->type == FORK_TKN && tok->type != FORK_TKN) | |
1632 | for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) | |
1633 | if (gnn == gn) { | |
1634 | skip = true; | |
1635 | break; | |
1636 | } | |
1637 | if (!skip) | |
1638 | permute(gn, vty); | |
1639 | } | |
1640 | } | |
1641 | list_delete_node(position, listtail(position)); | |
1642 | } | |
1643 | ||
1644 | int cmd_list_cmds(struct vty *vty, int do_permute) | |
1645 | { | |
1646 | struct cmd_node *node = vector_slot(cmdvec, vty->node); | |
1647 | ||
1648 | if (do_permute) | |
1649 | permute(vector_slot(node->cmdgraph->nodes, 0), vty); | |
1650 | else { | |
1651 | /* loop over all commands at this node */ | |
1652 | struct cmd_element *element = NULL; | |
1653 | for (unsigned int i = 0; i < vector_active(node->cmd_vector); | |
1654 | i++) | |
1655 | if ((element = vector_slot(node->cmd_vector, i)) | |
1656 | && element->attr != CMD_ATTR_DEPRECATED | |
1657 | && element->attr != CMD_ATTR_HIDDEN) | |
1658 | vty_out(vty, " %s\n", element->string); | |
1659 | } | |
1660 | return CMD_SUCCESS; | |
718e3744 | 1661 | } |
1662 | ||
0b84f294 DL |
1663 | /* Help display function for all node. */ |
1664 | DEFUN (config_list, | |
1665 | config_list_cmd, | |
1666 | "list [permutations]", | |
1667 | "Print command list\n" | |
1668 | "Print all possible command permutations\n") | |
1669 | { | |
d62a17ae | 1670 | return cmd_list_cmds(vty, argc == 2); |
0b84f294 DL |
1671 | } |
1672 | ||
a2454870 QY |
1673 | DEFUN (show_commandtree, |
1674 | show_commandtree_cmd, | |
1675 | "show commandtree [permutations]", | |
1676 | SHOW_STR | |
5a1945e4 DS |
1677 | "Show command tree\n" |
1678 | "Permutations that we are interested in\n") | |
a2454870 | 1679 | { |
d62a17ae | 1680 | return cmd_list_cmds(vty, argc == 3); |
a2454870 QY |
1681 | } |
1682 | ||
26fbe472 QY |
1683 | DEFUN_HIDDEN(show_cli_graph, |
1684 | show_cli_graph_cmd, | |
1685 | "show cli graph", | |
1686 | SHOW_STR | |
1687 | "CLI reflection\n" | |
1688 | "Dump current command space as DOT graph\n") | |
1689 | { | |
1690 | struct cmd_node *cn = vector_slot(cmdvec, vty->node); | |
1691 | char *dot = cmd_graph_dump_dot(cn->cmdgraph); | |
1692 | ||
1693 | vty_out(vty, "%s\n", dot); | |
1694 | XFREE(MTYPE_TMP, dot); | |
1695 | return CMD_SUCCESS; | |
1696 | } | |
1697 | ||
f806f29c | 1698 | static int vty_write_config(struct vty *vty) |
8efe88ea | 1699 | { |
d62a17ae | 1700 | size_t i; |
1701 | struct cmd_node *node; | |
8efe88ea | 1702 | |
f806f29c | 1703 | if (host.noconfig) |
1704 | return CMD_SUCCESS; | |
1705 | ||
d62a17ae | 1706 | if (vty->type == VTY_TERM) { |
1707 | vty_out(vty, "\nCurrent configuration:\n"); | |
1708 | vty_out(vty, "!\n"); | |
1709 | } | |
8efe88ea | 1710 | |
d62a17ae | 1711 | vty_out(vty, "frr version %s\n", FRR_VER_SHORT); |
1712 | vty_out(vty, "frr defaults %s\n", DFLT_NAME); | |
1713 | vty_out(vty, "!\n"); | |
8efe88ea | 1714 | |
83981138 RW |
1715 | pthread_rwlock_rdlock(&running_config->lock); |
1716 | { | |
1717 | for (i = 0; i < vector_active(cmdvec); i++) | |
1718 | if ((node = vector_slot(cmdvec, i)) && node->func | |
1719 | && (node->vtysh || vty->type != VTY_SHELL)) { | |
1720 | if ((*node->func)(vty)) | |
1721 | vty_out(vty, "!\n"); | |
1722 | } | |
1723 | } | |
1724 | pthread_rwlock_unlock(&running_config->lock); | |
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 | 1733 | static 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 | |
1831 | finished: | |
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 | ||
1843 | DEFUN (config_write, | |
1844 | config_write_cmd, | |
1845 | "write [<file|memory|terminal>]", | |
1846 | "Write running configuration to memory, network, or terminal\n" | |
1847 | "Write to configuration file\n" | |
1848 | "Write configuration currently in memory\n" | |
1849 | "Write configuration to terminal\n") | |
1850 | { | |
1851 | const int idx_type = 1; | |
1852 | ||
1853 | // if command was 'write terminal' or 'write memory' | |
1854 | if (argc == 2 && (!strcmp(argv[idx_type]->text, "terminal"))) { | |
1855 | return vty_write_config(vty); | |
1856 | } | |
1857 | ||
1858 | return file_write_config(vty); | |
1859 | } | |
1860 | ||
d862bffb QY |
1861 | /* ALIAS_FIXME for 'write <terminal|memory>' */ |
1862 | DEFUN (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' */ |
1872 | DEFUN (copy_runningconf_startupconf, | |
1873 | copy_runningconf_startupconf_cmd, | |
1874 | "copy running-config startup-config", | |
1875 | "Copy configuration\n" | |
1876 | "Copy running config to... \n" | |
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. */ | |
1884 | DEFUN (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 |
1920 | int cmd_domainname_set(const char *domainname) |
1921 | { | |
1922 | XFREE(MTYPE_HOST, host.domainname); | |
1923 | host.domainname = domainname ? XSTRDUP(MTYPE_HOST, domainname) : NULL; | |
1924 | return CMD_SUCCESS; | |
1925 | } | |
1926 | ||
1927 | /* Hostname configuration */ | |
60466a63 QY |
1928 | DEFUN(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 |
1944 | DEFUN(config_no_domainname, |
1945 | no_domainname_cmd, | |
1946 | "no domainname [DOMAINNAME]", | |
1947 | NO_STR | |
1948 | "Reset system's domain name\n" | |
1949 | "domain name of this router\n") | |
6b3ee3a0 MK |
1950 | { |
1951 | return cmd_domainname_set(NULL); | |
1952 | } | |
1953 | ||
d62a17ae | 1954 | int 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 | 1962 | DEFUN (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 | 1978 | DEFUN (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 |
1989 | DEFUN (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. */ |
2032 | DEFUN (no_config_password, | |
2033 | no_password_cmd, | |
2034 | "no password", | |
2035 | NO_STR | |
2036 | "Modify the terminal connection password\n") | |
2037 | { | |
2038 | bool warned = false; | |
2039 | ||
2040 | if (host.password) { | |
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 |
2060 | DEFUN (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 |
2113 | DEFUN (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 | 2141 | DEFUN (service_password_encrypt, |
2142 | service_password_encrypt_cmd, | |
2143 | "service password-encryption", | |
2144 | "Set up miscellaneous service\n" | |
2145 | "Enable encrypted passwords\n") | |
2146 | { | |
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 | ||
2168 | DEFUN (no_service_password_encrypt, | |
2169 | no_service_password_encrypt_cmd, | |
2170 | "no service password-encryption", | |
2171 | NO_STR | |
2172 | "Set up miscellaneous service\n" | |
2173 | "Enable encrypted passwords\n") | |
2174 | { | |
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 |
2191 | DEFUN (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 |
2205 | DEFUN (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 |
2216 | DEFUN (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 |
2230 | DEFUN (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 | 2242 | DEFUN_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 | 2257 | DEFUN (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 | ||
2280 | DEFUN (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 | 2330 | DEFUN (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 | ||
2351 | DEFUN (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 | 2363 | DEFUN (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 | ||
2384 | DEFUN (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 | 2396 | static 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) { |
450971aa | 2408 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
09c866e3 | 2409 | "config_log_file: Unable to alloc mem!"); |
d62a17ae | 2410 | return CMD_WARNING_CONFIG_FAILED; |
2411 | } | |
d0bfb22c | 2412 | |
0ce1ca80 | 2413 | p = XMALLOC(MTYPE_TMP, strlen(cwd) + strlen(fname) + 2); |
d62a17ae | 2414 | sprintf(p, "%s/%s", cwd, fname); |
2415 | fullpath = p; | |
2416 | } else | |
2417 | fullpath = fname; | |
718e3744 | 2418 | |
d62a17ae | 2419 | ret = zlog_set_file(fullpath, loglevel); |
718e3744 | 2420 | |
0a22ddfb | 2421 | XFREE(MTYPE_TMP, p); |
9035efaa | 2422 | |
d62a17ae | 2423 | if (!ret) { |
f8507817 DS |
2424 | if (vty) |
2425 | vty_out(vty, "can't open logfile %s\n", fname); | |
d62a17ae | 2426 | return CMD_WARNING_CONFIG_FAILED; |
2427 | } | |
718e3744 | 2428 | |
0a22ddfb | 2429 | XFREE(MTYPE_HOST, host.logfile); |
718e3744 | 2430 | |
d62a17ae | 2431 | host.logfile = XSTRDUP(MTYPE_HOST, fname); |
718e3744 | 2432 | |
c05795b1 | 2433 | #if defined(HAVE_CUMULUS) |
d62a17ae | 2434 | if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED) |
0204baa8 | 2435 | zlog_set_level(ZLOG_DEST_SYSLOG, ZLOG_DISABLED); |
c05795b1 | 2436 | #endif |
d62a17ae | 2437 | return CMD_SUCCESS; |
718e3744 | 2438 | } |
2439 | ||
e9b4e74a | 2440 | void command_setup_early_logging(const char *dest, const char *level) |
f8507817 DS |
2441 | { |
2442 | char *token; | |
2443 | ||
e9b4e74a DS |
2444 | if (level) { |
2445 | int nlevel = level_match(level); | |
2446 | ||
2447 | if (nlevel != ZLOG_DISABLED) | |
2448 | zlog_default->default_lvl = nlevel; | |
2449 | } | |
2450 | ||
2451 | if (!dest) | |
2452 | return; | |
2453 | ||
2454 | if (strcmp(dest, "stdout") == 0) { | |
f8507817 DS |
2455 | zlog_set_level(ZLOG_DEST_STDOUT, zlog_default->default_lvl); |
2456 | return; | |
2457 | } | |
2458 | ||
e9b4e74a | 2459 | if (strcmp(dest, "syslog") == 0) { |
f8507817 DS |
2460 | zlog_set_level(ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
2461 | return; | |
2462 | } | |
2463 | ||
e9b4e74a | 2464 | token = strstr(dest, ":"); |
74e7158c | 2465 | if (token == NULL) |
2466 | return; | |
2467 | ||
f8507817 DS |
2468 | token++; |
2469 | ||
2470 | set_log_file(NULL, token, zlog_default->default_lvl); | |
2471 | } | |
2472 | ||
274a4a44 | 2473 | DEFUN (config_log_file, |
2474 | config_log_file_cmd, | |
6de69f83 | 2475 | "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]", |
274a4a44 | 2476 | "Logging control\n" |
2477 | "Logging to file\n" | |
2478 | "Logging filename\n" | |
2479 | LOG_LEVEL_DESC) | |
2480 | { | |
d62a17ae | 2481 | int idx_filename = 2; |
2482 | int idx_log_levels = 3; | |
2483 | if (argc == 4) { | |
2484 | int level; | |
2485 | if ((level = level_match(argv[idx_log_levels]->arg)) | |
2486 | == ZLOG_DISABLED) | |
2487 | return CMD_ERR_NO_MATCH; | |
2488 | return set_log_file(vty, argv[idx_filename]->arg, level); | |
2489 | } else | |
2490 | return set_log_file(vty, argv[idx_filename]->arg, | |
2491 | zlog_default->default_lvl); | |
274a4a44 | 2492 | } |
2493 | ||
914432b5 | 2494 | static void disable_log_file(void) |
0204baa8 DW |
2495 | { |
2496 | zlog_reset_file(); | |
2497 | ||
0a22ddfb | 2498 | XFREE(MTYPE_HOST, host.logfile); |
0204baa8 DW |
2499 | |
2500 | host.logfile = NULL; | |
2501 | } | |
2502 | ||
718e3744 | 2503 | DEFUN (no_config_log_file, |
2504 | no_config_log_file_cmd, | |
d0bfb22c | 2505 | "no log file [FILENAME [LEVEL]]", |
718e3744 | 2506 | NO_STR |
2507 | "Logging control\n" | |
2508 | "Cancel logging to file\n" | |
d0bfb22c | 2509 | "Logging file name\n" |
d0bfb22c | 2510 | "Logging level\n") |
718e3744 | 2511 | { |
0204baa8 | 2512 | disable_log_file(); |
d62a17ae | 2513 | return CMD_SUCCESS; |
718e3744 | 2514 | } |
2515 | ||
2516 | DEFUN (config_log_syslog, | |
2517 | config_log_syslog_cmd, | |
6de69f83 | 2518 | "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]", |
12ab19f1 | 2519 | "Logging control\n" |
274a4a44 | 2520 | "Set syslog logging level\n" |
2521 | LOG_LEVEL_DESC) | |
2522 | { | |
d62a17ae | 2523 | int idx_log_levels = 2; |
0204baa8 | 2524 | |
d62a17ae | 2525 | if (argc == 3) { |
2526 | int level; | |
2527 | if ((level = level_match(argv[idx_log_levels]->arg)) | |
2528 | == ZLOG_DISABLED) | |
2529 | return CMD_ERR_NO_MATCH; | |
2530 | zlog_set_level(ZLOG_DEST_SYSLOG, level); | |
2531 | return CMD_SUCCESS; | |
2532 | } else { | |
2533 | zlog_set_level(ZLOG_DEST_SYSLOG, zlog_default->default_lvl); | |
2534 | return CMD_SUCCESS; | |
2535 | } | |
274a4a44 | 2536 | } |
2537 | ||
718e3744 | 2538 | DEFUN (no_config_log_syslog, |
2539 | no_config_log_syslog_cmd, | |
6de69f83 | 2540 | "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 | 2541 | NO_STR |
2542 | "Logging control\n" | |
274a4a44 | 2543 | "Cancel logging to syslog\n" |
d0bfb22c QY |
2544 | LOG_FACILITY_DESC |
2545 | LOG_LEVEL_DESC) | |
718e3744 | 2546 | { |
d62a17ae | 2547 | zlog_set_level(ZLOG_DEST_SYSLOG, ZLOG_DISABLED); |
2548 | return CMD_SUCCESS; | |
718e3744 | 2549 | } |
2550 | ||
274a4a44 | 2551 | DEFUN (config_log_facility, |
2552 | config_log_facility_cmd, | |
9ccf14f7 | 2553 | "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>", |
718e3744 | 2554 | "Logging control\n" |
274a4a44 | 2555 | "Facility parameter for syslog messages\n" |
2556 | LOG_FACILITY_DESC) | |
718e3744 | 2557 | { |
d62a17ae | 2558 | int idx_target = 2; |
2559 | int facility = facility_match(argv[idx_target]->arg); | |
274a4a44 | 2560 | |
d62a17ae | 2561 | zlog_default->facility = facility; |
2562 | return CMD_SUCCESS; | |
718e3744 | 2563 | } |
2564 | ||
274a4a44 | 2565 | DEFUN (no_config_log_facility, |
2566 | no_config_log_facility_cmd, | |
9ccf14f7 | 2567 | "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]", |
718e3744 | 2568 | NO_STR |
2569 | "Logging control\n" | |
274a4a44 | 2570 | "Reset syslog facility to default (daemon)\n" |
d0bfb22c | 2571 | LOG_FACILITY_DESC) |
274a4a44 | 2572 | { |
d62a17ae | 2573 | zlog_default->facility = LOG_DAEMON; |
2574 | return CMD_SUCCESS; | |
274a4a44 | 2575 | } |
2576 | ||
718e3744 | 2577 | DEFUN (config_log_record_priority, |
2578 | config_log_record_priority_cmd, | |
2579 | "log record-priority", | |
2580 | "Logging control\n" | |
2581 | "Log the priority of the message within the message\n") | |
2582 | { | |
d62a17ae | 2583 | zlog_default->record_priority = 1; |
2584 | return CMD_SUCCESS; | |
718e3744 | 2585 | } |
2586 | ||
2587 | DEFUN (no_config_log_record_priority, | |
2588 | no_config_log_record_priority_cmd, | |
2589 | "no log record-priority", | |
2590 | NO_STR | |
2591 | "Logging control\n" | |
2592 | "Do not log the priority of the message within the message\n") | |
2593 | { | |
d62a17ae | 2594 | zlog_default->record_priority = 0; |
2595 | return CMD_SUCCESS; | |
718e3744 | 2596 | } |
2597 | ||
1ed72e0b AS |
2598 | DEFUN (config_log_timestamp_precision, |
2599 | config_log_timestamp_precision_cmd, | |
6147e2c6 | 2600 | "log timestamp precision (0-6)", |
1ed72e0b AS |
2601 | "Logging control\n" |
2602 | "Timestamp configuration\n" | |
2603 | "Set the timestamp precision\n" | |
2604 | "Number of subsecond digits\n") | |
2605 | { | |
d62a17ae | 2606 | int idx_number = 3; |
2607 | zlog_default->timestamp_precision = | |
2608 | strtoul(argv[idx_number]->arg, NULL, 10); | |
2609 | return CMD_SUCCESS; | |
1ed72e0b AS |
2610 | } |
2611 | ||
2612 | DEFUN (no_config_log_timestamp_precision, | |
2613 | no_config_log_timestamp_precision_cmd, | |
2614 | "no log timestamp precision", | |
2615 | NO_STR | |
2616 | "Logging control\n" | |
2617 | "Timestamp configuration\n" | |
2618 | "Reset the timestamp precision to the default value of 0\n") | |
2619 | { | |
d62a17ae | 2620 | zlog_default->timestamp_precision = 0; |
2621 | return CMD_SUCCESS; | |
1ed72e0b AS |
2622 | } |
2623 | ||
9eed278b DL |
2624 | DEFUN (debug_memstats, |
2625 | debug_memstats_cmd, | |
2626 | "[no] debug memstats-at-exit", | |
2627 | NO_STR | |
2628 | DEBUG_STR | |
2629 | "Print memory type statistics at exit\n") | |
2630 | { | |
2631 | debug_memstats_at_exit = !!strcmp(argv[0]->text, "no"); | |
2632 | return CMD_SUCCESS; | |
2633 | } | |
2634 | ||
d62a17ae | 2635 | int cmd_banner_motd_file(const char *file) |
7cfc61d3 | 2636 | { |
d62a17ae | 2637 | int success = CMD_SUCCESS; |
2638 | char p[PATH_MAX]; | |
2639 | char *rpath; | |
2640 | char *in; | |
7cfc61d3 | 2641 | |
d62a17ae | 2642 | rpath = realpath(file, p); |
2643 | if (!rpath) | |
2644 | return CMD_ERR_NO_FILE; | |
2645 | in = strstr(rpath, SYSCONFDIR); | |
2646 | if (in == rpath) { | |
0a22ddfb | 2647 | XFREE(MTYPE_HOST, host.motdfile); |
d62a17ae | 2648 | host.motdfile = XSTRDUP(MTYPE_HOST, file); |
2649 | } else | |
2650 | success = CMD_WARNING_CONFIG_FAILED; | |
1ee08155 | 2651 | |
d62a17ae | 2652 | return success; |
7cfc61d3 DS |
2653 | } |
2654 | ||
3b0c5d9a | 2655 | DEFUN (banner_motd_file, |
2656 | banner_motd_file_cmd, | |
4d833e55 | 2657 | "banner motd file FILE", |
3b0c5d9a | 2658 | "Set banner\n" |
2659 | "Banner for motd\n" | |
2660 | "Banner from a file\n" | |
2661 | "Filename\n") | |
2662 | { | |
d62a17ae | 2663 | int idx_file = 3; |
2664 | const char *filename = argv[idx_file]->arg; | |
2665 | int cmd = cmd_banner_motd_file(filename); | |
1ee08155 | 2666 | |
d62a17ae | 2667 | if (cmd == CMD_ERR_NO_FILE) |
2668 | vty_out(vty, "%s does not exist", filename); | |
2669 | else if (cmd == CMD_WARNING_CONFIG_FAILED) | |
2670 | vty_out(vty, "%s must be in %s", filename, SYSCONFDIR); | |
1ee08155 | 2671 | |
d62a17ae | 2672 | return cmd; |
3b0c5d9a | 2673 | } |
718e3744 | 2674 | |
2675 | DEFUN (banner_motd_default, | |
2676 | banner_motd_default_cmd, | |
2677 | "banner motd default", | |
2678 | "Set banner string\n" | |
2679 | "Strings for motd\n" | |
2680 | "Default string\n") | |
2681 | { | |
d62a17ae | 2682 | host.motd = default_motd; |
2683 | return CMD_SUCCESS; | |
718e3744 | 2684 | } |
2685 | ||
2686 | DEFUN (no_banner_motd, | |
2687 | no_banner_motd_cmd, | |
2688 | "no banner motd", | |
2689 | NO_STR | |
2690 | "Set banner string\n" | |
2691 | "Strings for motd\n") | |
2692 | { | |
d62a17ae | 2693 | host.motd = NULL; |
2694 | if (host.motdfile) | |
2695 | XFREE(MTYPE_HOST, host.motdfile); | |
2696 | host.motdfile = NULL; | |
2697 | return CMD_SUCCESS; | |
718e3744 | 2698 | } |
2699 | ||
a83a5331 QY |
2700 | DEFUN(find, |
2701 | find_cmd, | |
2702 | "find COMMAND...", | |
2703 | "Find CLI command containing text\n" | |
2704 | "Text to search for\n") | |
2705 | { | |
2706 | char *text = argv_concat(argv, argc, 1); | |
cf6c83e7 QY |
2707 | const struct cmd_node *node; |
2708 | const struct cmd_element *cli; | |
2709 | vector clis; | |
a83a5331 QY |
2710 | |
2711 | for (unsigned int i = 0; i < vector_active(cmdvec); i++) { | |
cf6c83e7 | 2712 | node = vector_slot(cmdvec, i); |
a83a5331 QY |
2713 | if (!node) |
2714 | continue; | |
cf6c83e7 | 2715 | clis = node->cmd_vector; |
a83a5331 | 2716 | for (unsigned int j = 0; j < vector_active(clis); j++) { |
cf6c83e7 | 2717 | cli = vector_slot(clis, j); |
a83a5331 | 2718 | if (strcasestr(cli->string, text)) |
cf6c83e7 | 2719 | vty_out(vty, " (%s) %s\n", |
a83a5331 QY |
2720 | node_names[node->node], cli->string); |
2721 | } | |
2722 | } | |
2723 | ||
2724 | XFREE(MTYPE_TMP, text); | |
2725 | ||
2726 | return CMD_SUCCESS; | |
2727 | } | |
2728 | ||
718e3744 | 2729 | /* Set config filename. Called from vty.c */ |
d62a17ae | 2730 | void host_config_set(const char *filename) |
718e3744 | 2731 | { |
0a22ddfb | 2732 | XFREE(MTYPE_HOST, host.config); |
d62a17ae | 2733 | host.config = XSTRDUP(MTYPE_HOST, filename); |
718e3744 | 2734 | } |
2735 | ||
d62a17ae | 2736 | const char *host_config_get(void) |
57387fb2 | 2737 | { |
d62a17ae | 2738 | return host.config; |
57387fb2 CF |
2739 | } |
2740 | ||
d62a17ae | 2741 | void install_default(enum node_type node) |
718e3744 | 2742 | { |
d62a17ae | 2743 | install_element(node, &config_exit_cmd); |
2744 | install_element(node, &config_quit_cmd); | |
2745 | install_element(node, &config_end_cmd); | |
2746 | install_element(node, &config_help_cmd); | |
2747 | install_element(node, &config_list_cmd); | |
26fbe472 | 2748 | install_element(node, &show_cli_graph_cmd); |
a83a5331 | 2749 | install_element(node, &find_cmd); |
718e3744 | 2750 | |
d62a17ae | 2751 | install_element(node, &config_write_cmd); |
2752 | install_element(node, &show_running_config_cmd); | |
7f059ea6 | 2753 | |
d62a17ae | 2754 | install_element(node, &autocomplete_cmd); |
1c2facd1 RW |
2755 | |
2756 | nb_cli_install_default(node); | |
718e3744 | 2757 | } |
2758 | ||
87f44e2f DL |
2759 | /* Initialize command interface. Install basic nodes and commands. |
2760 | * | |
2761 | * terminal = 0 -- vtysh / no logging, no config control | |
2762 | * terminal = 1 -- normal daemon | |
9473e340 | 2763 | * terminal = -1 -- watchfrr / no logging, but minimal config control */ |
d62a17ae | 2764 | void cmd_init(int terminal) |
2765 | { | |
419cd5a0 MK |
2766 | struct utsname names; |
2767 | ||
a83a5331 QY |
2768 | if (array_size(node_names) != NODE_TYPE_MAX) |
2769 | assert(!"Update the CLI node description array!"); | |
2770 | ||
419cd5a0 | 2771 | uname(&names); |
d62a17ae | 2772 | qobj_init(); |
2773 | ||
fe6b47b9 QY |
2774 | /* register command preprocessors */ |
2775 | hook_register(cmd_execute, handle_pipe_action); | |
2776 | hook_register(cmd_execute_done, handle_pipe_action_done); | |
2777 | ||
d62a17ae | 2778 | varhandlers = list_new(); |
2779 | ||
2780 | /* Allocate initial top vector of commands. */ | |
2781 | cmdvec = vector_init(VECTOR_MIN_SIZE); | |
2782 | ||
2783 | /* Default host value settings. */ | |
419cd5a0 MK |
2784 | host.name = XSTRDUP(MTYPE_HOST, names.nodename); |
2785 | #ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME | |
6b3ee3a0 MK |
2786 | if ((strcmp(names.domainname, "(none)") == 0)) |
2787 | host.domainname = NULL; | |
2788 | else | |
2789 | host.domainname = XSTRDUP(MTYPE_HOST, names.domainname); | |
419cd5a0 MK |
2790 | #else |
2791 | host.domainname = NULL; | |
2792 | #endif | |
d62a17ae | 2793 | host.password = NULL; |
2794 | host.enable = NULL; | |
2795 | host.logfile = NULL; | |
2796 | host.config = NULL; | |
2797 | host.noconfig = (terminal < 0); | |
2798 | host.lines = -1; | |
2799 | host.motd = default_motd; | |
2800 | host.motdfile = NULL; | |
2801 | ||
2802 | /* Install top nodes. */ | |
2803 | install_node(&view_node, NULL); | |
2804 | install_node(&enable_node, NULL); | |
2805 | install_node(&auth_node, NULL); | |
2806 | install_node(&auth_enable_node, NULL); | |
2807 | install_node(&config_node, config_write_host); | |
2808 | ||
2809 | /* Each node's basic commands. */ | |
2810 | install_element(VIEW_NODE, &show_version_cmd); | |
a83a5331 | 2811 | install_element(ENABLE_NODE, &show_startup_config_cmd); |
9eed278b | 2812 | install_element(ENABLE_NODE, &debug_memstats_cmd); |
a83a5331 | 2813 | |
d62a17ae | 2814 | if (terminal) { |
2815 | install_element(VIEW_NODE, &config_list_cmd); | |
2816 | install_element(VIEW_NODE, &config_exit_cmd); | |
2817 | install_element(VIEW_NODE, &config_quit_cmd); | |
2818 | install_element(VIEW_NODE, &config_help_cmd); | |
2819 | install_element(VIEW_NODE, &config_enable_cmd); | |
2820 | install_element(VIEW_NODE, &config_terminal_length_cmd); | |
2821 | install_element(VIEW_NODE, &config_terminal_no_length_cmd); | |
2822 | install_element(VIEW_NODE, &show_logging_cmd); | |
2823 | install_element(VIEW_NODE, &show_commandtree_cmd); | |
2824 | install_element(VIEW_NODE, &echo_cmd); | |
2825 | install_element(VIEW_NODE, &autocomplete_cmd); | |
a83a5331 | 2826 | install_element(VIEW_NODE, &find_cmd); |
d62a17ae | 2827 | |
d62a17ae | 2828 | install_element(ENABLE_NODE, &config_end_cmd); |
2829 | install_element(ENABLE_NODE, &config_disable_cmd); | |
2830 | install_element(ENABLE_NODE, &config_terminal_cmd); | |
2831 | install_element(ENABLE_NODE, ©_runningconf_startupconf_cmd); | |
2832 | install_element(ENABLE_NODE, &config_write_cmd); | |
2833 | install_element(ENABLE_NODE, &show_running_config_cmd); | |
d62a17ae | 2834 | install_element(ENABLE_NODE, &config_logmsg_cmd); |
a83a5331 | 2835 | |
d62a17ae | 2836 | install_default(CONFIG_NODE); |
2837 | ||
2838 | thread_cmd_init(); | |
2839 | workqueue_cmd_init(); | |
2840 | hash_cmd_init(); | |
2841 | } | |
2842 | ||
2843 | install_element(CONFIG_NODE, &hostname_cmd); | |
2844 | install_element(CONFIG_NODE, &no_hostname_cmd); | |
6b3ee3a0 MK |
2845 | install_element(CONFIG_NODE, &domainname_cmd); |
2846 | install_element(CONFIG_NODE, &no_domainname_cmd); | |
d62a17ae | 2847 | install_element(CONFIG_NODE, &frr_version_defaults_cmd); |
9eed278b | 2848 | install_element(CONFIG_NODE, &debug_memstats_cmd); |
d62a17ae | 2849 | |
2850 | if (terminal > 0) { | |
2851 | install_element(CONFIG_NODE, &password_cmd); | |
322e2d5c | 2852 | install_element(CONFIG_NODE, &no_password_cmd); |
d62a17ae | 2853 | install_element(CONFIG_NODE, &enable_password_cmd); |
2854 | install_element(CONFIG_NODE, &no_enable_password_cmd); | |
2855 | ||
2856 | install_element(CONFIG_NODE, &config_log_stdout_cmd); | |
2857 | install_element(CONFIG_NODE, &no_config_log_stdout_cmd); | |
2858 | install_element(CONFIG_NODE, &config_log_monitor_cmd); | |
2859 | install_element(CONFIG_NODE, &no_config_log_monitor_cmd); | |
2860 | install_element(CONFIG_NODE, &config_log_file_cmd); | |
2861 | install_element(CONFIG_NODE, &no_config_log_file_cmd); | |
2862 | install_element(CONFIG_NODE, &config_log_syslog_cmd); | |
2863 | install_element(CONFIG_NODE, &no_config_log_syslog_cmd); | |
2864 | install_element(CONFIG_NODE, &config_log_facility_cmd); | |
2865 | install_element(CONFIG_NODE, &no_config_log_facility_cmd); | |
d62a17ae | 2866 | install_element(CONFIG_NODE, &config_log_record_priority_cmd); |
2867 | install_element(CONFIG_NODE, | |
2868 | &no_config_log_record_priority_cmd); | |
2869 | install_element(CONFIG_NODE, | |
2870 | &config_log_timestamp_precision_cmd); | |
2871 | install_element(CONFIG_NODE, | |
2872 | &no_config_log_timestamp_precision_cmd); | |
2873 | install_element(CONFIG_NODE, &service_password_encrypt_cmd); | |
2874 | install_element(CONFIG_NODE, &no_service_password_encrypt_cmd); | |
2875 | install_element(CONFIG_NODE, &banner_motd_default_cmd); | |
2876 | install_element(CONFIG_NODE, &banner_motd_file_cmd); | |
2877 | install_element(CONFIG_NODE, &no_banner_motd_cmd); | |
2878 | install_element(CONFIG_NODE, &service_terminal_length_cmd); | |
2879 | install_element(CONFIG_NODE, &no_service_terminal_length_cmd); | |
2880 | ||
2881 | vrf_install_commands(); | |
2882 | } | |
af2567b6 DL |
2883 | |
2884 | #ifdef DEV_BUILD | |
d62a17ae | 2885 | grammar_sandbox_init(); |
af2567b6 | 2886 | #endif |
718e3744 | 2887 | } |
228da428 | 2888 | |
4d762f26 | 2889 | void cmd_terminate(void) |
d62a17ae | 2890 | { |
2891 | struct cmd_node *cmd_node; | |
2892 | ||
5d806ec6 QY |
2893 | hook_unregister(cmd_execute, handle_pipe_action); |
2894 | hook_unregister(cmd_execute_done, handle_pipe_action_done); | |
2895 | ||
d62a17ae | 2896 | if (cmdvec) { |
2897 | for (unsigned int i = 0; i < vector_active(cmdvec); i++) | |
2898 | if ((cmd_node = vector_slot(cmdvec, i)) != NULL) { | |
2899 | // deleting the graph delets the cmd_element as | |
2900 | // well | |
2901 | graph_delete_graph(cmd_node->cmdgraph); | |
2902 | vector_free(cmd_node->cmd_vector); | |
2903 | hash_clean(cmd_node->cmd_hash, NULL); | |
2904 | hash_free(cmd_node->cmd_hash); | |
2905 | cmd_node->cmd_hash = NULL; | |
2906 | } | |
2907 | ||
2908 | vector_free(cmdvec); | |
2909 | cmdvec = NULL; | |
2910 | } | |
2911 | ||
0a22ddfb QY |
2912 | XFREE(MTYPE_HOST, host.name); |
2913 | XFREE(MTYPE_HOST, host.domainname); | |
2914 | XFREE(MTYPE_HOST, host.password); | |
2915 | XFREE(MTYPE_HOST, host.password_encrypt); | |
2916 | XFREE(MTYPE_HOST, host.enable); | |
2917 | XFREE(MTYPE_HOST, host.enable_encrypt); | |
2918 | XFREE(MTYPE_HOST, host.logfile); | |
2919 | XFREE(MTYPE_HOST, host.motdfile); | |
2920 | XFREE(MTYPE_HOST, host.config); | |
d62a17ae | 2921 | |
6a154c88 | 2922 | list_delete(&varhandlers); |
d62a17ae | 2923 | qobj_finish(); |
228da428 | 2924 | } |