]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Configuration generator. |
896014f4 DL |
2 | * Copyright (C) 2000 Kunihiro Ishiguro |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
718e3744 | 20 | |
21 | #include <zebra.h> | |
22 | ||
23 | #include "command.h" | |
24 | #include "linklist.h" | |
25 | #include "memory.h" | |
a7982412 | 26 | #include "typesafe.h" |
718e3744 | 27 | |
28 | #include "vtysh/vtysh.h" | |
88177fe3 | 29 | #include "vtysh/vtysh_user.h" |
718e3744 | 30 | |
4a1ab8e4 | 31 | DEFINE_MGROUP(MVTYSH, "vtysh") |
d62a17ae | 32 | DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CONFIG, "Vtysh configuration") |
4a1ab8e4 DL |
33 | DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CONFIG_LINE, "Vtysh configuration line") |
34 | ||
718e3744 | 35 | vector configvec; |
36 | ||
a7982412 JW |
37 | PREDECL_RBTREE_UNIQ(config_master); |
38 | ||
d62a17ae | 39 | struct config { |
40 | /* Configuration node name. */ | |
41 | char *name; | |
718e3744 | 42 | |
d62a17ae | 43 | /* Configuration string line. */ |
44 | struct list *line; | |
718e3744 | 45 | |
d62a17ae | 46 | /* Configuration can be nest. */ |
47 | struct config *config; | |
718e3744 | 48 | |
d62a17ae | 49 | /* Index of this config. */ |
d7c0a89a | 50 | uint32_t index; |
a7982412 JW |
51 | |
52 | /* Node entry for the typed Red-black tree */ | |
53 | struct config_master_item rbt_item; | |
718e3744 | 54 | }; |
55 | ||
56 | struct list *config_top; | |
95e735b5 | 57 | |
d62a17ae | 58 | static int line_cmp(char *c1, char *c2) |
718e3744 | 59 | { |
d62a17ae | 60 | return strcmp(c1, c2); |
718e3744 | 61 | } |
62 | ||
d62a17ae | 63 | static void line_del(char *line) |
718e3744 | 64 | { |
d62a17ae | 65 | XFREE(MTYPE_VTYSH_CONFIG_LINE, line); |
718e3744 | 66 | } |
67 | ||
d62a17ae | 68 | static struct config *config_new(void) |
718e3744 | 69 | { |
d62a17ae | 70 | struct config *config; |
71 | config = XCALLOC(MTYPE_VTYSH_CONFIG, sizeof(struct config)); | |
72 | return config; | |
718e3744 | 73 | } |
74 | ||
a7982412 | 75 | static int config_cmp(const struct config *c1, const struct config *c2) |
718e3744 | 76 | { |
d62a17ae | 77 | return strcmp(c1->name, c2->name); |
718e3744 | 78 | } |
79 | ||
d62a17ae | 80 | static void config_del(struct config *config) |
718e3744 | 81 | { |
6a154c88 | 82 | list_delete(&config->line); |
0a22ddfb | 83 | XFREE(MTYPE_VTYSH_CONFIG_LINE, config->name); |
d62a17ae | 84 | XFREE(MTYPE_VTYSH_CONFIG, config); |
718e3744 | 85 | } |
86 | ||
a7982412 JW |
87 | DECLARE_RBTREE_UNIQ(config_master, struct config, rbt_item, config_cmp) |
88 | ||
d62a17ae | 89 | static struct config *config_get(int index, const char *line) |
718e3744 | 90 | { |
d62a17ae | 91 | struct config *config; |
a7982412 | 92 | struct config_master_head *master; |
d62a17ae | 93 | |
94 | master = vector_lookup_ensure(configvec, index); | |
95 | ||
96 | if (!master) { | |
a7982412 JW |
97 | master = XMALLOC(MTYPE_VTYSH_CONFIG, sizeof(struct config_master_head)); |
98 | config_master_init(master); | |
d62a17ae | 99 | vector_set_index(configvec, index, master); |
100 | } | |
101 | ||
a7982412 JW |
102 | const struct config config_ref = { .name = (char *)line }; |
103 | config = config_master_find(master, &config_ref); | |
d62a17ae | 104 | |
105 | if (!config) { | |
106 | config = config_new(); | |
107 | config->line = list_new(); | |
108 | config->line->del = (void (*)(void *))line_del; | |
109 | config->line->cmp = (int (*)(void *, void *))line_cmp; | |
110 | config->name = XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line); | |
111 | config->index = index; | |
a7982412 | 112 | config_master_add(master, config); |
d62a17ae | 113 | } |
114 | return config; | |
718e3744 | 115 | } |
116 | ||
d62a17ae | 117 | void config_add_line(struct list *config, const char *line) |
718e3744 | 118 | { |
d62a17ae | 119 | listnode_add(config, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line)); |
718e3744 | 120 | } |
121 | ||
d62a17ae | 122 | static void config_add_line_uniq(struct list *config, const char *line) |
718e3744 | 123 | { |
d62a17ae | 124 | struct listnode *node, *nnode; |
125 | char *pnt; | |
126 | ||
127 | for (ALL_LIST_ELEMENTS(config, node, nnode, pnt)) { | |
128 | if (strcmp(pnt, line) == 0) | |
129 | return; | |
130 | } | |
131 | listnode_add_sort(config, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line)); | |
718e3744 | 132 | } |
133 | ||
a3c1db5e | 134 | /* |
e8be380a QY |
135 | * Add a line that should only be shown once, and always show at the end of the |
136 | * config block. | |
137 | * | |
138 | * If the line already exists, it will be moved to the end of the block. If it | |
139 | * does not exist, it will be added at the end of the block. | |
140 | * | |
141 | * Note that this only makes sense when there is just one such line that should | |
142 | * show up at the very end of a config block. Furthermore, if the same block | |
143 | * can show up from multiple daemons, all of them must make sure to print the | |
144 | * line at the end of their config, otherwise the line will show at the end of | |
145 | * the config for the last daemon that printed it. | |
146 | * | |
147 | * Here is a motivating example with the 'exit-vrf' command. Suppose we receive | |
148 | * a config from Zebra like so: | |
149 | * | |
150 | * vrf BLUE | |
151 | * ip route A | |
152 | * ip route B | |
153 | * exit-vrf | |
154 | * | |
155 | * Then suppose we later receive this config from PIM: | |
156 | * | |
157 | * vrf BLUE | |
158 | * ip msdp mesh-group MyGroup member 1.2.3.4 | |
159 | * exit-vrf | |
160 | * | |
161 | * Then we will combine them into one config block like so: | |
162 | * | |
163 | * vrf BLUE | |
164 | * ip route A | |
165 | * ip route B | |
166 | * ip msdp mesh-group MyGroup member 1.2.3.4 | |
167 | * exit-vrf | |
168 | * | |
169 | * Because PIM also sent us an 'exit-vrf', we noticed that we already had one | |
170 | * under the 'vrf BLUE' config block and so we moved it to the end of the | |
171 | * config block again. If PIM had neglected to send us 'exit-vrf', the result | |
172 | * would be this: | |
173 | * | |
174 | * vrf BLUE | |
175 | * ip route A | |
176 | * ip route B | |
177 | * exit-vrf | |
178 | * ip msdp mesh-group MyGroup member 1.2.3.4 | |
179 | * | |
180 | * Therefore, daemons that share config blocks must take care to consistently | |
181 | * print the same block terminators. | |
182 | * | |
183 | * Ideally this would be solved by adding a string to struct config that is | |
184 | * always printed at the end when dumping a config. However, this would only | |
185 | * work when the user is using integrated config. In the non-integrated config | |
186 | * case, daemons are responsible for writing their own config files, and so the | |
187 | * must be able to print these blocks correctly independently of vtysh, which | |
188 | * means they are the ones that need to handle printing the block terminators | |
189 | * and VTYSH needs to be smart enough to combine them properly. | |
190 | * | |
191 | * --- | |
192 | * | |
193 | * config | |
194 | * The config to add the line to | |
195 | * | |
196 | * line | |
197 | * The line to add to the end of the config | |
a3c1db5e | 198 | */ |
e8be380a | 199 | static void config_add_line_uniq_end(struct list *config, const char *line) |
a3c1db5e DS |
200 | { |
201 | struct listnode *node; | |
e8be380a | 202 | char *pnt; |
a3c1db5e | 203 | |
e8be380a QY |
204 | for (ALL_LIST_ELEMENTS_RO(config, node, pnt)) { |
205 | if (strcmp(pnt, line) == 0) | |
206 | break; | |
207 | } | |
208 | ||
209 | if (!node) | |
210 | config_add_line(config, line); | |
211 | else | |
a3c1db5e DS |
212 | listnode_move_to_tail(config, node); |
213 | } | |
214 | ||
d62a17ae | 215 | void vtysh_config_parse_line(void *arg, const char *line) |
718e3744 | 216 | { |
d62a17ae | 217 | char c; |
218 | static struct config *config = NULL; | |
219 | ||
220 | if (!line) | |
221 | return; | |
222 | ||
223 | c = line[0]; | |
224 | ||
225 | if (c == '\0') | |
226 | return; | |
227 | ||
d62a17ae | 228 | switch (c) { |
229 | /* Suppress exclamation points ! and commented lines. The !s are | |
230 | * generated | |
231 | * dynamically in vtysh_config_dump() */ | |
232 | case '!': | |
233 | case '#': | |
234 | break; | |
235 | case ' ': | |
236 | /* Store line to current configuration. */ | |
237 | if (config) { | |
238 | if (strncmp(line, " link-params", | |
239 | strlen(" link-params")) | |
240 | == 0) { | |
241 | config_add_line(config->line, line); | |
242 | config->index = LINK_PARAMS_NODE; | |
996c9314 LB |
243 | } else if (strncmp(line, " ip multicast boundary", |
244 | strlen(" ip multicast boundary")) | |
245 | == 0) { | |
e8be380a | 246 | config_add_line_uniq_end(config->line, line); |
61a7a236 DS |
247 | } else if (strncmp(line, " ip igmp query-interval", |
248 | strlen(" ip igmp query-interval")) == 0) { | |
e8be380a | 249 | config_add_line_uniq_end(config->line, line); |
d62a17ae | 250 | } else if (config->index == LINK_PARAMS_NODE |
251 | && strncmp(line, " exit-link-params", | |
252 | strlen(" exit")) | |
253 | == 0) { | |
254 | config_add_line(config->line, line); | |
255 | config->index = INTERFACE_NODE; | |
c319e19d QY |
256 | } else if (config->index == VRF_NODE |
257 | && strncmp(line, " exit-vrf", | |
258 | strlen(" exit-vrf")) | |
259 | == 0) { | |
e8be380a | 260 | config_add_line_uniq_end(config->line, line); |
18ca2e1f QY |
261 | } else if (!strncmp(line, " vrrp", strlen(" vrrp")) |
262 | || !strncmp(line, " no vrrp", | |
263 | strlen(" no vrrp"))) { | |
264 | config_add_line(config->line, line); | |
c552f61e DS |
265 | } else if (!strncmp(line, " ip mroute", strlen(" ip mroute"))) { |
266 | config_add_line_uniq_end(config->line, line); | |
d62a17ae | 267 | } else if (config->index == RMAP_NODE |
268 | || config->index == INTERFACE_NODE | |
d62a17ae | 269 | || config->index == VTY_NODE |
3741c4ba DS |
270 | || config->index == VRF_NODE |
271 | || config->index == NH_GROUP_NODE) | |
d62a17ae | 272 | config_add_line_uniq(config->line, line); |
273 | else | |
274 | config_add_line(config->line, line); | |
275 | } else | |
276 | config_add_line(config_top, line); | |
277 | break; | |
278 | default: | |
279 | if (strncmp(line, "interface", strlen("interface")) == 0) | |
280 | config = config_get(INTERFACE_NODE, line); | |
2dd0d726 RW |
281 | else if (strncmp(line, "pseudowire", strlen("pseudowire")) == 0) |
282 | config = config_get(PW_NODE, line); | |
d62a17ae | 283 | else if (strncmp(line, "vrf", strlen("vrf")) == 0) |
284 | config = config_get(VRF_NODE, line); | |
e5c83d9b DS |
285 | else if (strncmp(line, "nexthop-group", strlen("nexthop-group")) |
286 | == 0) | |
287 | config = config_get(NH_GROUP_NODE, line); | |
d62a17ae | 288 | else if (strncmp(line, "router-id", strlen("router-id")) == 0) |
289 | config = config_get(ZEBRA_NODE, line); | |
290 | else if (strncmp(line, "router rip", strlen("router rip")) == 0) | |
291 | config = config_get(RIP_NODE, line); | |
292 | else if (strncmp(line, "router ripng", strlen("router ripng")) | |
293 | == 0) | |
294 | config = config_get(RIPNG_NODE, line); | |
295 | else if (strncmp(line, "router eigrp", strlen("router eigrp")) | |
296 | == 0) | |
297 | config = config_get(EIGRP_NODE, line); | |
298 | else if (strncmp(line, "router babel", strlen("router babel")) | |
299 | == 0) | |
300 | config = config_get(BABEL_NODE, line); | |
301 | else if (strncmp(line, "router ospf", strlen("router ospf")) | |
302 | == 0) | |
303 | config = config_get(OSPF_NODE, line); | |
304 | else if (strncmp(line, "router ospf6", strlen("router ospf6")) | |
305 | == 0) | |
306 | config = config_get(OSPF6_NODE, line); | |
307 | else if (strncmp(line, "mpls ldp", strlen("mpls ldp")) == 0) | |
308 | config = config_get(LDP_NODE, line); | |
309 | else if (strncmp(line, "l2vpn", strlen("l2vpn")) == 0) | |
310 | config = config_get(LDP_L2VPN_NODE, line); | |
311 | else if (strncmp(line, "router bgp", strlen("router bgp")) == 0) | |
312 | config = config_get(BGP_NODE, line); | |
313 | else if (strncmp(line, "router isis", strlen("router isis")) | |
314 | == 0) | |
315 | config = config_get(ISIS_NODE, line); | |
770ccdf8 CF |
316 | else if (strncmp(line, "router openfabric", strlen("router openfabric")) |
317 | == 0) | |
318 | config = config_get(OPENFABRIC_NODE, line); | |
d62a17ae | 319 | else if (strncmp(line, "route-map", strlen("route-map")) == 0) |
320 | config = config_get(RMAP_NODE, line); | |
e5c83d9b DS |
321 | else if (strncmp(line, "pbr-map", strlen("pbr-map")) == 0) |
322 | config = config_get(PBRMAP_NODE, line); | |
d62a17ae | 323 | else if (strncmp(line, "access-list", strlen("access-list")) |
324 | == 0) | |
325 | config = config_get(ACCESS_NODE, line); | |
326 | else if (strncmp(line, "ipv6 access-list", | |
327 | strlen("ipv6 access-list")) | |
328 | == 0) | |
329 | config = config_get(ACCESS_IPV6_NODE, line); | |
d37ba549 MK |
330 | else if (strncmp(line, "mac access-list", |
331 | strlen("mac access-list")) | |
332 | == 0) | |
333 | config = config_get(ACCESS_MAC_NODE, line); | |
d62a17ae | 334 | else if (strncmp(line, "ip prefix-list", |
335 | strlen("ip prefix-list")) | |
336 | == 0) | |
337 | config = config_get(PREFIX_NODE, line); | |
338 | else if (strncmp(line, "ipv6 prefix-list", | |
339 | strlen("ipv6 prefix-list")) | |
340 | == 0) | |
341 | config = config_get(PREFIX_IPV6_NODE, line); | |
7336e101 SP |
342 | else if (strncmp(line, "bgp as-path access-list", |
343 | strlen("bgp as-path access-list")) | |
d62a17ae | 344 | == 0) |
345 | config = config_get(AS_LIST_NODE, line); | |
7336e101 SP |
346 | else if (strncmp(line, "bgp community-list", |
347 | strlen("bgp community-list")) | |
d62a17ae | 348 | == 0 |
7336e101 SP |
349 | || strncmp(line, "bgp extcommunity-list", |
350 | strlen("bgp extcommunity-list")) | |
996c9314 | 351 | == 0 |
7336e101 SP |
352 | || strncmp(line, "bgp large-community-list", |
353 | strlen("bgp large-community-list")) | |
996c9314 | 354 | == 0) |
d62a17ae | 355 | config = config_get(COMMUNITY_LIST_NODE, line); |
356 | else if (strncmp(line, "ip route", strlen("ip route")) == 0) | |
357 | config = config_get(IP_NODE, line); | |
358 | else if (strncmp(line, "ipv6 route", strlen("ipv6 route")) == 0) | |
359 | config = config_get(IP_NODE, line); | |
360 | else if (strncmp(line, "key", strlen("key")) == 0) | |
361 | config = config_get(KEYCHAIN_NODE, line); | |
362 | else if (strncmp(line, "line", strlen("line")) == 0) | |
363 | config = config_get(VTY_NODE, line); | |
364 | else if ((strncmp(line, "ipv6 forwarding", | |
365 | strlen("ipv6 forwarding")) | |
366 | == 0) | |
367 | || (strncmp(line, "ip forwarding", | |
368 | strlen("ip forwarding")) | |
369 | == 0)) | |
370 | config = config_get(FORWARDING_NODE, line); | |
d62a17ae | 371 | else if (strncmp(line, "debug vrf", strlen("debug vrf")) == 0) |
372 | config = config_get(VRF_DEBUG_NODE, line); | |
1c2facd1 RW |
373 | else if (strncmp(line, "debug northbound", |
374 | strlen("debug northbound")) | |
375 | == 0) | |
376 | config = config_get(NORTHBOUND_DEBUG_NODE, line); | |
e3ab8170 AD |
377 | else if (strncmp(line, "debug route-map", |
378 | strlen("debug route-map")) | |
379 | == 0) | |
380 | config = config_get(RMAP_DEBUG_NODE, line); | |
fe9e7b71 DL |
381 | else if (strncmp(line, "debug resolver", |
382 | strlen("debug resolver")) == 0) | |
383 | config = config_get(RESOLVER_DEBUG_NODE, line); | |
d62a17ae | 384 | else if (strncmp(line, "debug", strlen("debug")) == 0) |
385 | config = config_get(DEBUG_NODE, line); | |
386 | else if (strncmp(line, "password", strlen("password")) == 0 | |
387 | || strncmp(line, "enable password", | |
388 | strlen("enable password")) | |
389 | == 0) | |
390 | config = config_get(AAA_NODE, line); | |
391 | else if (strncmp(line, "ip protocol", strlen("ip protocol")) | |
392 | == 0) | |
393 | config = config_get(PROTOCOL_NODE, line); | |
394 | else if (strncmp(line, "ipv6 protocol", strlen("ipv6 protocol")) | |
395 | == 0) | |
396 | config = config_get(PROTOCOL_NODE, line); | |
397 | else if (strncmp(line, "ip nht", strlen("ip nht")) == 0) | |
398 | config = config_get(PROTOCOL_NODE, line); | |
399 | else if (strncmp(line, "ipv6 nht", strlen("ipv6 nht")) == 0) | |
400 | config = config_get(PROTOCOL_NODE, line); | |
401 | else if (strncmp(line, "mpls", strlen("mpls")) == 0) | |
402 | config = config_get(MPLS_NODE, line); | |
c2f29cf3 RZ |
403 | else if (strncmp(line, "bfd", strlen("bfd")) == 0) |
404 | config = config_get(BFD_NODE, line); | |
d62a17ae | 405 | else { |
406 | if (strncmp(line, "log", strlen("log")) == 0 | |
407 | || strncmp(line, "hostname", strlen("hostname")) | |
408 | == 0 | |
409 | || strncmp(line, "frr", strlen("frr")) == 0 | |
410 | || strncmp(line, "agentx", strlen("agentx")) == 0 | |
c0e83672 IR |
411 | || strncmp(line, "no log", strlen("no log")) == 0 |
412 | || strncmp(line, "no ip prefix-list", strlen("no ip prefix-list")) == 0 | |
413 | || strncmp(line, "no ipv6 prefix-list", strlen("no ipv6 prefix-list")) == 0) | |
d62a17ae | 414 | config_add_line_uniq(config_top, line); |
415 | else | |
416 | config_add_line(config_top, line); | |
417 | config = NULL; | |
418 | } | |
419 | break; | |
718e3744 | 420 | } |
718e3744 | 421 | } |
422 | ||
718e3744 | 423 | /* Macro to check delimiter is needed between each configuration line |
95e735b5 | 424 | * or not. */ |
d62a17ae | 425 | #define NO_DELIMITER(I) \ |
426 | ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \ | |
427 | || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE \ | |
d37ba549 | 428 | || (I) == ACCESS_IPV6_NODE || (I) == ACCESS_MAC_NODE \ |
f108d487 QY |
429 | || (I) == PREFIX_IPV6_NODE || (I) == FORWARDING_NODE \ |
430 | || (I) == DEBUG_NODE || (I) == AAA_NODE || (I) == VRF_DEBUG_NODE \ | |
e3ab8170 | 431 | || (I) == NORTHBOUND_DEBUG_NODE || (I) == RMAP_DEBUG_NODE \ |
fe9e7b71 | 432 | || (I) == RESOLVER_DEBUG_NODE || (I) == MPLS_NODE) |
718e3744 | 433 | |
95e735b5 | 434 | /* Display configuration to file pointer. */ |
0a334343 | 435 | void vtysh_config_dump(void) |
718e3744 | 436 | { |
d62a17ae | 437 | struct listnode *node, *nnode; |
438 | struct listnode *mnode, *mnnode; | |
439 | struct config *config; | |
a7982412 | 440 | struct config_master_head *master; |
d62a17ae | 441 | char *line; |
442 | unsigned int i; | |
443 | ||
2cddf2ff QY |
444 | for (ALL_LIST_ELEMENTS(config_top, node, nnode, line)) |
445 | vty_out(vty, "%s\n", line); | |
446 | ||
447 | vty_out(vty, "!\n"); | |
d62a17ae | 448 | |
449 | for (i = 0; i < vector_active(configvec); i++) | |
450 | if ((master = vector_slot(configvec, i)) != NULL) { | |
a7982412 | 451 | while ((config = config_master_pop(master))) { |
1d72e48a | 452 | /* Don't print empty sections for interface. |
d62a17ae | 453 | * Route maps on the |
454 | * other hand could have a legitimate empty | |
455 | * section at the end. | |
1d72e48a | 456 | * VRF is handled in the backend, we could have |
457 | * "configured" VRFs with static routes which | |
458 | * are not under the VRF node. | |
d62a17ae | 459 | */ |
1d72e48a | 460 | if (config->index == INTERFACE_NODE |
d62a17ae | 461 | && list_isempty(config->line)) |
462 | continue; | |
463 | ||
2cddf2ff | 464 | vty_out(vty, "%s\n", config->name); |
d62a17ae | 465 | |
466 | for (ALL_LIST_ELEMENTS(config->line, mnode, | |
2cddf2ff QY |
467 | mnnode, line)) |
468 | vty_out(vty, "%s\n", line); | |
469 | if (!NO_DELIMITER(i)) | |
470 | vty_out(vty, "!\n"); | |
a7982412 JW |
471 | |
472 | config_del(config); | |
d62a17ae | 473 | } |
2cddf2ff QY |
474 | if (NO_DELIMITER(i)) |
475 | vty_out(vty, "!\n"); | |
d62a17ae | 476 | } |
477 | ||
478 | for (i = 0; i < vector_active(configvec); i++) | |
479 | if ((master = vector_slot(configvec, i)) != NULL) { | |
a7982412 JW |
480 | config_master_fini(master); |
481 | XFREE(MTYPE_VTYSH_CONFIG, master); | |
d62a17ae | 482 | vector_slot(configvec, i) = NULL; |
483 | } | |
484 | list_delete_all_node(config_top); | |
718e3744 | 485 | } |
486 | ||
487 | /* Read up configuration file from file_name. */ | |
d62a17ae | 488 | static int vtysh_read_file(FILE *confp) |
718e3744 | 489 | { |
d62a17ae | 490 | struct vty *vty; |
491 | int ret; | |
492 | ||
493 | vty = vty_new(); | |
4a9746fd | 494 | vty->wfd = STDERR_FILENO; |
d62a17ae | 495 | vty->type = VTY_TERM; |
496 | vty->node = CONFIG_NODE; | |
718e3744 | 497 | |
d62a17ae | 498 | vtysh_execute_no_pager("enable"); |
499 | vtysh_execute_no_pager("configure terminal"); | |
718e3744 | 500 | |
d62a17ae | 501 | /* Execute configuration file. */ |
502 | ret = vtysh_config_from_file(vty, confp); | |
718e3744 | 503 | |
d62a17ae | 504 | vtysh_execute_no_pager("end"); |
505 | vtysh_execute_no_pager("disable"); | |
718e3744 | 506 | |
d62a17ae | 507 | vty_close(vty); |
3221dca8 | 508 | |
d62a17ae | 509 | return (ret); |
718e3744 | 510 | } |
511 | ||
e7168df4 | 512 | /* Read up configuration file from config_default_dir. */ |
d62a17ae | 513 | int vtysh_read_config(const char *config_default_dir) |
718e3744 | 514 | { |
d62a17ae | 515 | FILE *confp = NULL; |
516 | int ret; | |
517 | ||
518 | confp = fopen(config_default_dir, "r"); | |
519 | if (confp == NULL) { | |
520 | fprintf(stderr, | |
521 | "%% Can't open configuration file %s due to '%s'.\n", | |
522 | config_default_dir, safe_strerror(errno)); | |
523 | return (CMD_ERR_NO_FILE); | |
524 | } | |
67e29abc | 525 | |
d62a17ae | 526 | ret = vtysh_read_file(confp); |
527 | fclose(confp); | |
718e3744 | 528 | |
d62a17ae | 529 | return (ret); |
718e3744 | 530 | } |
531 | ||
e7168df4 | 532 | /* We don't write vtysh specific into file from vtysh. vtysh.conf should |
533 | * be edited by hand. So, we handle only "write terminal" case here and | |
534 | * integrate vtysh specific conf with conf from daemons. | |
535 | */ | |
4d762f26 | 536 | void vtysh_config_write(void) |
718e3744 | 537 | { |
63e653a2 | 538 | char line[512]; |
d62a17ae | 539 | |
6b3ee3a0 | 540 | if (cmd_hostname_get()) { |
63e653a2 | 541 | snprintf(line, sizeof(line), "hostname %s", cmd_hostname_get()); |
d62a17ae | 542 | vtysh_config_parse_line(NULL, line); |
543 | } | |
3b103fec MK |
544 | |
545 | if (cmd_domainname_get()) { | |
546 | sprintf(line, "domainname %s", cmd_domainname_get()); | |
547 | vtysh_config_parse_line(NULL, line); | |
548 | } | |
d62a17ae | 549 | if (vtysh_write_integrated == WRITE_INTEGRATED_NO) |
550 | vtysh_config_parse_line(NULL, | |
551 | "no service integrated-vtysh-config"); | |
552 | if (vtysh_write_integrated == WRITE_INTEGRATED_YES) | |
553 | vtysh_config_parse_line(NULL, | |
554 | "service integrated-vtysh-config"); | |
555 | ||
556 | user_config_write(); | |
718e3744 | 557 | } |
558 | ||
4d762f26 | 559 | void vtysh_config_init(void) |
718e3744 | 560 | { |
d62a17ae | 561 | config_top = list_new(); |
562 | config_top->del = (void (*)(void *))line_del; | |
563 | configvec = vector_init(1); | |
718e3744 | 564 | } |