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