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