]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-or-later | |
2 | /* Configuration generator. | |
3 | * Copyright (C) 2000 Kunihiro Ishiguro | |
4 | */ | |
5 | ||
6 | #include <zebra.h> | |
7 | ||
8 | #include "command.h" | |
9 | #include "linklist.h" | |
10 | #include "memory.h" | |
11 | #include "typesafe.h" | |
12 | ||
13 | #include "vtysh/vtysh.h" | |
14 | #include "vtysh/vtysh_user.h" | |
15 | ||
16 | DEFINE_MGROUP(MVTYSH, "vtysh"); | |
17 | DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CONFIG, "Vtysh configuration"); | |
18 | DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CONFIG_LINE, "Vtysh configuration line"); | |
19 | ||
20 | vector configvec; | |
21 | ||
22 | PREDECL_LIST(config_master); | |
23 | PREDECL_HASH(config_master_hash); | |
24 | ||
25 | struct config { | |
26 | /* Configuration node name. */ | |
27 | char *name; | |
28 | ||
29 | /* Configuration string line. */ | |
30 | struct list *line; | |
31 | ||
32 | /* Configuration can be nested. */ | |
33 | struct config *parent; | |
34 | vector nested; | |
35 | ||
36 | /* Exit command. */ | |
37 | char *exit; | |
38 | ||
39 | /* Index of this config. */ | |
40 | uint32_t index; | |
41 | ||
42 | /* Node entry for the typed Red-black tree */ | |
43 | struct config_master_item rbt_item; | |
44 | struct config_master_hash_item hash_item; | |
45 | }; | |
46 | ||
47 | struct list *config_top; | |
48 | ||
49 | static int line_cmp(char *c1, char *c2) | |
50 | { | |
51 | return strcmp(c1, c2); | |
52 | } | |
53 | ||
54 | static void line_del(char *line) | |
55 | { | |
56 | XFREE(MTYPE_VTYSH_CONFIG_LINE, line); | |
57 | } | |
58 | ||
59 | static struct config *config_new(void) | |
60 | { | |
61 | struct config *config; | |
62 | config = XCALLOC(MTYPE_VTYSH_CONFIG, sizeof(struct config)); | |
63 | return config; | |
64 | } | |
65 | ||
66 | static void config_del(struct config *config) | |
67 | { | |
68 | vector_free(config->nested); | |
69 | list_delete(&config->line); | |
70 | if (config->exit) | |
71 | XFREE(MTYPE_VTYSH_CONFIG_LINE, config->exit); | |
72 | XFREE(MTYPE_VTYSH_CONFIG_LINE, config->name); | |
73 | XFREE(MTYPE_VTYSH_CONFIG, config); | |
74 | } | |
75 | ||
76 | static int config_cmp(const struct config *c1, const struct config *c2) | |
77 | { | |
78 | return strcmp(c1->name, c2->name); | |
79 | } | |
80 | ||
81 | static uint32_t config_hash(const struct config *c) | |
82 | { | |
83 | return string_hash_make(c->name); | |
84 | } | |
85 | ||
86 | DECLARE_LIST(config_master, struct config, rbt_item); | |
87 | DECLARE_HASH(config_master_hash, struct config, hash_item, config_cmp, | |
88 | config_hash); | |
89 | ||
90 | /* | |
91 | * The config_master_head is a list for order of receipt | |
92 | * The hash is for quick lookup under this NODE | |
93 | */ | |
94 | struct configuration { | |
95 | struct config_master_head master; | |
96 | struct config_master_hash_head hash_master; | |
97 | }; | |
98 | ||
99 | static struct config *config_get_vec(vector vec, int index, const char *line) | |
100 | { | |
101 | struct config *config, *config_loop; | |
102 | struct configuration *configuration; | |
103 | struct config lookup; | |
104 | ||
105 | config = config_loop = NULL; | |
106 | ||
107 | configuration = vector_lookup_ensure(vec, index); | |
108 | ||
109 | if (!configuration) { | |
110 | configuration = XMALLOC(MTYPE_VTYSH_CONFIG, | |
111 | sizeof(struct configuration)); | |
112 | config_master_init(&configuration->master); | |
113 | config_master_hash_init(&configuration->hash_master); | |
114 | vector_set_index(vec, index, configuration); | |
115 | } | |
116 | ||
117 | lookup.name = (char *)line; | |
118 | config = config_master_hash_find(&configuration->hash_master, &lookup); | |
119 | ||
120 | if (!config) { | |
121 | config = config_new(); | |
122 | config->line = list_new(); | |
123 | config->line->del = (void (*)(void *))line_del; | |
124 | config->line->cmp = (int (*)(void *, void *))line_cmp; | |
125 | config->name = XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line); | |
126 | config->exit = NULL; | |
127 | config->index = index; | |
128 | config->nested = vector_init(1); | |
129 | config_master_add_tail(&configuration->master, config); | |
130 | config_master_hash_add(&configuration->hash_master, config); | |
131 | } | |
132 | return config; | |
133 | } | |
134 | ||
135 | static struct config *config_get(int index, const char *line) | |
136 | { | |
137 | return config_get_vec(configvec, index, line); | |
138 | } | |
139 | ||
140 | static struct config *config_get_nested(struct config *parent, int index, | |
141 | const char *line) | |
142 | { | |
143 | struct config *config; | |
144 | ||
145 | config = config_get_vec(parent->nested, index, line); | |
146 | config->parent = parent; | |
147 | ||
148 | return config; | |
149 | } | |
150 | ||
151 | void config_add_line(struct list *config, const char *line) | |
152 | { | |
153 | listnode_add(config, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line)); | |
154 | } | |
155 | ||
156 | static void config_add_line_uniq(struct list *config, const char *line) | |
157 | { | |
158 | struct listnode *node, *nnode; | |
159 | char *pnt; | |
160 | ||
161 | for (ALL_LIST_ELEMENTS(config, node, nnode, pnt)) { | |
162 | if (strcmp(pnt, line) == 0) | |
163 | return; | |
164 | } | |
165 | listnode_add_sort(config, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line)); | |
166 | } | |
167 | ||
168 | /* | |
169 | * Add a line that should only be shown once, and always show at the end of the | |
170 | * config block. | |
171 | * | |
172 | * If the line already exists, it will be moved to the end of the block. If it | |
173 | * does not exist, it will be added at the end of the block. | |
174 | * | |
175 | * Note that this only makes sense when there is just one such line that should | |
176 | * show up at the very end of a config block. Furthermore, if the same block | |
177 | * can show up from multiple daemons, all of them must make sure to print the | |
178 | * line at the end of their config, otherwise the line will show at the end of | |
179 | * the config for the last daemon that printed it. | |
180 | * | |
181 | * Here is a motivating example with the 'exit-vrf' command. Suppose we receive | |
182 | * a config from Zebra like so: | |
183 | * | |
184 | * vrf BLUE | |
185 | * ip route A | |
186 | * ip route B | |
187 | * exit-vrf | |
188 | * | |
189 | * Then suppose we later receive this config from PIM: | |
190 | * | |
191 | * vrf BLUE | |
192 | * ip msdp mesh-group MyGroup member 1.2.3.4 | |
193 | * exit-vrf | |
194 | * | |
195 | * Then we will combine them into one config block like so: | |
196 | * | |
197 | * vrf BLUE | |
198 | * ip route A | |
199 | * ip route B | |
200 | * ip msdp mesh-group MyGroup member 1.2.3.4 | |
201 | * exit-vrf | |
202 | * | |
203 | * Because PIM also sent us an 'exit-vrf', we noticed that we already had one | |
204 | * under the 'vrf BLUE' config block and so we moved it to the end of the | |
205 | * config block again. If PIM had neglected to send us 'exit-vrf', the result | |
206 | * would be this: | |
207 | * | |
208 | * vrf BLUE | |
209 | * ip route A | |
210 | * ip route B | |
211 | * exit-vrf | |
212 | * ip msdp mesh-group MyGroup member 1.2.3.4 | |
213 | * | |
214 | * Therefore, daemons that share config blocks must take care to consistently | |
215 | * print the same block terminators. | |
216 | * | |
217 | * Ideally this would be solved by adding a string to struct config that is | |
218 | * always printed at the end when dumping a config. However, this would only | |
219 | * work when the user is using integrated config. In the non-integrated config | |
220 | * case, daemons are responsible for writing their own config files, and so the | |
221 | * must be able to print these blocks correctly independently of vtysh, which | |
222 | * means they are the ones that need to handle printing the block terminators | |
223 | * and VTYSH needs to be smart enough to combine them properly. | |
224 | * | |
225 | * --- | |
226 | * | |
227 | * config | |
228 | * The config to add the line to | |
229 | * | |
230 | * line | |
231 | * The line to add to the end of the config | |
232 | */ | |
233 | static void config_add_line_uniq_end(struct list *config, const char *line) | |
234 | { | |
235 | struct listnode *node; | |
236 | char *pnt; | |
237 | ||
238 | for (ALL_LIST_ELEMENTS_RO(config, node, pnt)) { | |
239 | if (strcmp(pnt, line) == 0) | |
240 | break; | |
241 | } | |
242 | ||
243 | if (!node) | |
244 | config_add_line(config, line); | |
245 | else | |
246 | listnode_move_to_tail(config, node); | |
247 | } | |
248 | ||
249 | static void config_add_line_head(struct list *config, const char *line) | |
250 | { | |
251 | listnode_add_head(config, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line)); | |
252 | } | |
253 | ||
254 | void vtysh_config_parse_line(void *arg, const char *line) | |
255 | { | |
256 | char c; | |
257 | static struct config *config = NULL; | |
258 | ||
259 | if (!line) | |
260 | return; | |
261 | ||
262 | c = line[0]; | |
263 | ||
264 | if (c == '\0') | |
265 | return; | |
266 | ||
267 | switch (c) { | |
268 | /* Suppress exclamation points ! and commented lines. The !s are | |
269 | * generated | |
270 | * dynamically in vtysh_config_dump() */ | |
271 | case '!': | |
272 | case '#': | |
273 | break; | |
274 | case ' ': | |
275 | /* Store line to current configuration. */ | |
276 | if (config) { | |
277 | if (config->index == KEYCHAIN_NODE | |
278 | && strncmp(line, " key", strlen(" key")) == 0) { | |
279 | config = config_get_nested( | |
280 | config, KEYCHAIN_KEY_NODE, line); | |
281 | } else if (config->index == KEYCHAIN_KEY_NODE) { | |
282 | if (strncmp(line, " exit", strlen(" exit")) | |
283 | == 0) { | |
284 | config_add_line_uniq_end(config->line, | |
285 | line); | |
286 | config = config->parent; | |
287 | } else { | |
288 | config_add_line_uniq(config->line, | |
289 | line); | |
290 | } | |
291 | } else if (strncmp(line, " link-params", | |
292 | strlen(" link-params")) | |
293 | == 0) { | |
294 | config_add_line(config->line, line); | |
295 | config->index = LINK_PARAMS_NODE; | |
296 | } else if (strncmp(line, " ip multicast boundary", | |
297 | strlen(" ip multicast boundary")) | |
298 | == 0) { | |
299 | config_add_line_uniq_end(config->line, line); | |
300 | } else if (strncmp(line, " ip igmp query-interval", | |
301 | strlen(" ip igmp query-interval")) | |
302 | == 0) { | |
303 | config_add_line_uniq_end(config->line, line); | |
304 | } else if (config->index == LINK_PARAMS_NODE | |
305 | && strncmp(line, " exit-link-params", | |
306 | strlen(" exit")) | |
307 | == 0) { | |
308 | config_add_line(config->line, line); | |
309 | config->index = INTERFACE_NODE; | |
310 | } else if (!strncmp(line, " vrrp", strlen(" vrrp")) | |
311 | || !strncmp(line, " no vrrp", | |
312 | strlen(" no vrrp"))) { | |
313 | config_add_line(config->line, line); | |
314 | } else if (!strncmp(line, " ip mroute", | |
315 | strlen(" ip mroute"))) { | |
316 | config_add_line_uniq_end(config->line, line); | |
317 | } else if (config->index == RMAP_NODE || | |
318 | config->index == INTERFACE_NODE || | |
319 | config->index == VTY_NODE) | |
320 | config_add_line_uniq(config->line, line); | |
321 | else if (config->index == NH_GROUP_NODE) { | |
322 | if (strncmp(line, " resilient", | |
323 | strlen(" resilient")) == 0) | |
324 | config_add_line_head(config->line, | |
325 | line); | |
326 | else | |
327 | config_add_line_uniq_end(config->line, | |
328 | line); | |
329 | } else | |
330 | config_add_line(config->line, line); | |
331 | } else | |
332 | config_add_line(config_top, line); | |
333 | break; | |
334 | default: | |
335 | if (strncmp(line, "exit", strlen("exit")) == 0) { | |
336 | if (config) { | |
337 | if (config->exit) | |
338 | XFREE(MTYPE_VTYSH_CONFIG_LINE, | |
339 | config->exit); | |
340 | config->exit = | |
341 | XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line); | |
342 | } | |
343 | } else if (strncmp(line, "interface", strlen("interface")) == 0) | |
344 | config = config_get(INTERFACE_NODE, line); | |
345 | else if (strncmp(line, "pseudowire", strlen("pseudowire")) == 0) | |
346 | config = config_get(PW_NODE, line); | |
347 | else if (strncmp(line, "vrf", strlen("vrf")) == 0) | |
348 | config = config_get(VRF_NODE, line); | |
349 | else if (strncmp(line, "nexthop-group", strlen("nexthop-group")) | |
350 | == 0) | |
351 | config = config_get(NH_GROUP_NODE, line); | |
352 | else if (strncmp(line, "router-id", strlen("router-id")) == 0) | |
353 | config = config_get(ZEBRA_NODE, line); | |
354 | else if (strncmp(line, "router rip", strlen("router rip")) == 0) | |
355 | config = config_get(RIP_NODE, line); | |
356 | else if (strncmp(line, "router ripng", strlen("router ripng")) | |
357 | == 0) | |
358 | config = config_get(RIPNG_NODE, line); | |
359 | else if (strncmp(line, "router eigrp", strlen("router eigrp")) | |
360 | == 0) | |
361 | config = config_get(EIGRP_NODE, line); | |
362 | else if (strncmp(line, "router babel", strlen("router babel")) | |
363 | == 0) | |
364 | config = config_get(BABEL_NODE, line); | |
365 | else if (strncmp(line, "router ospf", strlen("router ospf")) | |
366 | == 0) | |
367 | config = config_get(OSPF_NODE, line); | |
368 | else if (strncmp(line, "router ospf6", strlen("router ospf6")) | |
369 | == 0) | |
370 | config = config_get(OSPF6_NODE, line); | |
371 | else if (strncmp(line, "mpls ldp", strlen("mpls ldp")) == 0) | |
372 | config = config_get(LDP_NODE, line); | |
373 | else if (strncmp(line, "l2vpn", strlen("l2vpn")) == 0) | |
374 | config = config_get(LDP_L2VPN_NODE, line); | |
375 | else if (strncmp(line, "router bgp", strlen("router bgp")) == 0) | |
376 | config = config_get(BGP_NODE, line); | |
377 | else if (strncmp(line, "router isis", strlen("router isis")) | |
378 | == 0) | |
379 | config = config_get(ISIS_NODE, line); | |
380 | else if (strncmp(line, "router openfabric", strlen("router openfabric")) | |
381 | == 0) | |
382 | config = config_get(OPENFABRIC_NODE, line); | |
383 | else if (strncmp(line, "affinity-map", | |
384 | strlen("affinity-map")) == 0) | |
385 | config = config_get(AFFMAP_NODE, line); | |
386 | else if (strncmp(line, "route-map", strlen("route-map")) == 0) | |
387 | config = config_get(RMAP_NODE, line); | |
388 | else if (strncmp(line, "no route-map", strlen("no route-map")) | |
389 | == 0) | |
390 | config = config_get(RMAP_NODE, line); | |
391 | else if (strncmp(line, "pbr-map", strlen("pbr-map")) == 0) | |
392 | config = config_get(PBRMAP_NODE, line); | |
393 | else if (strncmp(line, "access-list", strlen("access-list")) | |
394 | == 0) | |
395 | config = config_get(ACCESS_NODE, line); | |
396 | else if (strncmp(line, "ipv6 access-list", | |
397 | strlen("ipv6 access-list")) | |
398 | == 0) | |
399 | config = config_get(ACCESS_IPV6_NODE, line); | |
400 | else if (strncmp(line, "mac access-list", | |
401 | strlen("mac access-list")) | |
402 | == 0) | |
403 | config = config_get(ACCESS_MAC_NODE, line); | |
404 | else if (strncmp(line, "ip prefix-list", | |
405 | strlen("ip prefix-list")) | |
406 | == 0) | |
407 | config = config_get(PREFIX_NODE, line); | |
408 | else if (strncmp(line, "ipv6 prefix-list", | |
409 | strlen("ipv6 prefix-list")) | |
410 | == 0) | |
411 | config = config_get(PREFIX_IPV6_NODE, line); | |
412 | else if (strncmp(line, "bgp as-path access-list", | |
413 | strlen("bgp as-path access-list")) | |
414 | == 0) | |
415 | config = config_get(AS_LIST_NODE, line); | |
416 | else if (strncmp(line, "bgp community-list", | |
417 | strlen("bgp community-list")) | |
418 | == 0 | |
419 | || strncmp(line, "bgp extcommunity-list", | |
420 | strlen("bgp extcommunity-list")) | |
421 | == 0 | |
422 | || strncmp(line, "bgp large-community-list", | |
423 | strlen("bgp large-community-list")) | |
424 | == 0) | |
425 | config = config_get(COMMUNITY_LIST_NODE, line); | |
426 | else if (strncmp(line, "bgp community alias", | |
427 | strlen("bgp community alias")) == 0) | |
428 | config = config_get(COMMUNITY_ALIAS_NODE, line); | |
429 | else if (strncmp(line, "ip route", strlen("ip route")) == 0) | |
430 | config = config_get(IP_NODE, line); | |
431 | else if (strncmp(line, "ipv6 route", strlen("ipv6 route")) == 0) | |
432 | config = config_get(IP_NODE, line); | |
433 | else if (strncmp(line, "key", strlen("key")) == 0) | |
434 | config = config_get(KEYCHAIN_NODE, line); | |
435 | else if (strncmp(line, "line", strlen("line")) == 0) | |
436 | config = config_get(VTY_NODE, line); | |
437 | else if ((strncmp(line, "ipv6 forwarding", | |
438 | strlen("ipv6 forwarding")) | |
439 | == 0) | |
440 | || (strncmp(line, "ip forwarding", | |
441 | strlen("ip forwarding")) | |
442 | == 0)) | |
443 | config = config_get(FORWARDING_NODE, line); | |
444 | else if (strncmp(line, "debug vrf", strlen("debug vrf")) == 0) | |
445 | config = config_get(VRF_DEBUG_NODE, line); | |
446 | else if (strncmp(line, "debug northbound", | |
447 | strlen("debug northbound")) | |
448 | == 0) | |
449 | config = config_get(NORTHBOUND_DEBUG_NODE, line); | |
450 | else if (strncmp(line, "debug route-map", | |
451 | strlen("debug route-map")) | |
452 | == 0) | |
453 | config = config_get(RMAP_DEBUG_NODE, line); | |
454 | else if (strncmp(line, "debug resolver", | |
455 | strlen("debug resolver")) == 0) | |
456 | config = config_get(RESOLVER_DEBUG_NODE, line); | |
457 | else if (strncmp(line, "debug", strlen("debug")) == 0) | |
458 | config = config_get(DEBUG_NODE, line); | |
459 | else if (strncmp(line, "password", strlen("password")) == 0 | |
460 | || strncmp(line, "enable password", | |
461 | strlen("enable password")) | |
462 | == 0) | |
463 | config = config_get(AAA_NODE, line); | |
464 | else if (strncmp(line, "ip protocol", strlen("ip protocol")) | |
465 | == 0) | |
466 | config = config_get(PROTOCOL_NODE, line); | |
467 | else if (strncmp(line, "ipv6 protocol", strlen("ipv6 protocol")) | |
468 | == 0) | |
469 | config = config_get(PROTOCOL_NODE, line); | |
470 | else if (strncmp(line, "ip nht", strlen("ip nht")) == 0) | |
471 | config = config_get(PROTOCOL_NODE, line); | |
472 | else if (strncmp(line, "ipv6 nht", strlen("ipv6 nht")) == 0) | |
473 | config = config_get(PROTOCOL_NODE, line); | |
474 | else if (strncmp(line, "mpls", strlen("mpls")) == 0) | |
475 | config = config_get(MPLS_NODE, line); | |
476 | else if (strncmp(line, "segment-routing", | |
477 | strlen("segment-routing")) | |
478 | == 0) | |
479 | config = config_get(SEGMENT_ROUTING_NODE, line); | |
480 | else if (strncmp(line, "bfd", strlen("bfd")) == 0) | |
481 | config = config_get(BFD_NODE, line); | |
482 | else if (strncmp(line, "rpki", strlen("rpki")) == 0) | |
483 | config = config_get(RPKI_NODE, line); | |
484 | else { | |
485 | if (strncmp(line, "log", strlen("log")) == 0 || | |
486 | strncmp(line, "hostname", strlen("hostname")) == | |
487 | 0 || | |
488 | strncmp(line, "domainname", strlen("domainname")) == | |
489 | 0 || | |
490 | strncmp(line, "allow-reserved-ranges", | |
491 | strlen("allow-reserved-ranges")) == 0 || | |
492 | strncmp(line, "frr", strlen("frr")) == 0 || | |
493 | strncmp(line, "agentx", strlen("agentx")) == 0 || | |
494 | strncmp(line, "no log", strlen("no log")) == 0 || | |
495 | strncmp(line, "no ip prefix-list", | |
496 | strlen("no ip prefix-list")) == 0 || | |
497 | strncmp(line, "no ipv6 prefix-list", | |
498 | strlen("no ipv6 prefix-list")) == 0 || | |
499 | strncmp(line, "service ", strlen("service ")) == | |
500 | 0 || | |
501 | strncmp(line, "no service cputime-stats", | |
502 | strlen("no service cputime-stats")) == 0 || | |
503 | strncmp(line, "service cputime-warning", | |
504 | strlen("service cputime-warning")) == 0) | |
505 | config_add_line_uniq(config_top, line); | |
506 | else | |
507 | config_add_line(config_top, line); | |
508 | config = NULL; | |
509 | } | |
510 | break; | |
511 | } | |
512 | } | |
513 | ||
514 | /* Macro to check delimiter is needed between each configuration line | |
515 | * or not. */ | |
516 | #define NO_DELIMITER(I) \ | |
517 | ((I) == AFFMAP_NODE || (I) == ACCESS_NODE || (I) == PREFIX_NODE || \ | |
518 | (I) == IP_NODE || (I) == AS_LIST_NODE || \ | |
519 | (I) == COMMUNITY_LIST_NODE || (I) == COMMUNITY_ALIAS_NODE || \ | |
520 | (I) == ACCESS_IPV6_NODE || (I) == ACCESS_MAC_NODE || \ | |
521 | (I) == PREFIX_IPV6_NODE || (I) == FORWARDING_NODE || \ | |
522 | (I) == DEBUG_NODE || (I) == AAA_NODE || (I) == VRF_DEBUG_NODE || \ | |
523 | (I) == NORTHBOUND_DEBUG_NODE || (I) == RMAP_DEBUG_NODE || \ | |
524 | (I) == RESOLVER_DEBUG_NODE || (I) == MPLS_NODE || \ | |
525 | (I) == KEYCHAIN_KEY_NODE) | |
526 | ||
527 | static void configvec_dump(vector vec, bool nested) | |
528 | { | |
529 | struct listnode *mnode, *mnnode; | |
530 | struct config *config; | |
531 | struct configuration *configuration; | |
532 | char *line; | |
533 | unsigned int i; | |
534 | ||
535 | for (i = 0; i < vector_active(vec); i++) | |
536 | if ((configuration = vector_slot(vec, i)) != NULL) { | |
537 | while ((config = config_master_pop( | |
538 | &configuration->master))) { | |
539 | config_master_hash_del( | |
540 | &configuration->hash_master, config); | |
541 | /* Don't print empty sections for interface. | |
542 | * Route maps on the | |
543 | * other hand could have a legitimate empty | |
544 | * section at the end. | |
545 | * VRF is handled in the backend, we could have | |
546 | * "configured" VRFs with static routes which | |
547 | * are not under the VRF node. | |
548 | */ | |
549 | if (config->index == INTERFACE_NODE | |
550 | && (listcount(config->line) == 1) | |
551 | && (line = listnode_head(config->line)) | |
552 | && strmatch(line, "exit")) { | |
553 | config_del(config); | |
554 | continue; | |
555 | } | |
556 | ||
557 | vty_out(vty, "%s\n", config->name); | |
558 | ||
559 | for (ALL_LIST_ELEMENTS(config->line, mnode, | |
560 | mnnode, line)) | |
561 | vty_out(vty, "%s\n", line); | |
562 | ||
563 | configvec_dump(config->nested, true); | |
564 | ||
565 | if (config->exit) | |
566 | vty_out(vty, "%s\n", config->exit); | |
567 | ||
568 | if (!NO_DELIMITER(i)) | |
569 | vty_out(vty, "!\n"); | |
570 | ||
571 | config_del(config); | |
572 | } | |
573 | config_master_fini(&configuration->master); | |
574 | config_master_hash_fini(&configuration->hash_master); | |
575 | XFREE(MTYPE_VTYSH_CONFIG, configuration); | |
576 | vector_slot(vec, i) = NULL; | |
577 | if (!nested && NO_DELIMITER(i)) | |
578 | vty_out(vty, "!\n"); | |
579 | } | |
580 | } | |
581 | ||
582 | void vtysh_config_dump(void) | |
583 | { | |
584 | struct listnode *node, *nnode; | |
585 | char *line; | |
586 | ||
587 | for (ALL_LIST_ELEMENTS(config_top, node, nnode, line)) | |
588 | vty_out(vty, "%s\n", line); | |
589 | ||
590 | list_delete_all_node(config_top); | |
591 | ||
592 | vty_out(vty, "!\n"); | |
593 | ||
594 | configvec_dump(configvec, false); | |
595 | } | |
596 | ||
597 | /* Read up configuration file from file_name. */ | |
598 | static int vtysh_read_file(FILE *confp, bool dry_run) | |
599 | { | |
600 | struct vty *vty; | |
601 | int ret; | |
602 | ||
603 | vty = vty_new(); | |
604 | vty->wfd = STDERR_FILENO; | |
605 | vty->type = VTY_TERM; | |
606 | vty->node = CONFIG_NODE; | |
607 | ||
608 | vtysh_execute_no_pager("enable"); | |
609 | vtysh_execute_no_pager("configure terminal"); | |
610 | ||
611 | if (!dry_run) | |
612 | vtysh_execute_no_pager("XFRR_start_configuration"); | |
613 | ||
614 | /* Execute configuration file. */ | |
615 | ret = vtysh_config_from_file(vty, confp); | |
616 | ||
617 | if (!dry_run) | |
618 | vtysh_execute_no_pager("XFRR_end_configuration"); | |
619 | ||
620 | vtysh_execute_no_pager("end"); | |
621 | vtysh_execute_no_pager("disable"); | |
622 | ||
623 | vty_close(vty); | |
624 | ||
625 | return (ret); | |
626 | } | |
627 | ||
628 | /* Read up configuration file from config_default_dir. */ | |
629 | int vtysh_read_config(const char *config_default_dir, bool dry_run) | |
630 | { | |
631 | FILE *confp = NULL; | |
632 | bool save; | |
633 | int ret; | |
634 | ||
635 | confp = fopen(config_default_dir, "r"); | |
636 | if (confp == NULL) { | |
637 | fprintf(stderr, | |
638 | "%% Can't open configuration file %s due to '%s'.\n", | |
639 | config_default_dir, safe_strerror(errno)); | |
640 | return CMD_ERR_NO_FILE; | |
641 | } | |
642 | ||
643 | save = vtysh_add_timestamp; | |
644 | vtysh_add_timestamp = false; | |
645 | ||
646 | ret = vtysh_read_file(confp, dry_run); | |
647 | fclose(confp); | |
648 | ||
649 | vtysh_add_timestamp = save; | |
650 | ||
651 | return (ret); | |
652 | } | |
653 | ||
654 | /* We don't write vtysh specific into file from vtysh. vtysh.conf should | |
655 | * be edited by hand. So, we handle only "write terminal" case here and | |
656 | * integrate vtysh specific conf with conf from daemons. | |
657 | */ | |
658 | void vtysh_config_write(void) | |
659 | { | |
660 | const char *name; | |
661 | char line[512]; | |
662 | ||
663 | name = cmd_hostname_get(); | |
664 | if (name && name[0] != '\0') { | |
665 | snprintf(line, sizeof(line), "hostname %s", name); | |
666 | vtysh_config_parse_line(NULL, line); | |
667 | } | |
668 | ||
669 | name = cmd_domainname_get(); | |
670 | if (name && name[0] != '\0') { | |
671 | snprintf(line, sizeof(line), "domainname %s", name); | |
672 | vtysh_config_parse_line(NULL, line); | |
673 | } | |
674 | ||
675 | if (vtysh_write_integrated == WRITE_INTEGRATED_NO) | |
676 | vtysh_config_parse_line(NULL, | |
677 | "no service integrated-vtysh-config"); | |
678 | if (vtysh_write_integrated == WRITE_INTEGRATED_YES) | |
679 | vtysh_config_parse_line(NULL, | |
680 | "service integrated-vtysh-config"); | |
681 | ||
682 | user_config_write(); | |
683 | } | |
684 | ||
685 | void vtysh_config_init(void) | |
686 | { | |
687 | config_top = list_new(); | |
688 | config_top->del = (void (*)(void *))line_del; | |
689 | configvec = vector_init(1); | |
690 | } |