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