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