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