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