]>
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" | |
26 | ||
27 | #include "vtysh/vtysh.h" | |
88177fe3 | 28 | #include "vtysh/vtysh_user.h" |
718e3744 | 29 | |
4a1ab8e4 | 30 | DEFINE_MGROUP(MVTYSH, "vtysh") |
d62a17ae | 31 | DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CONFIG, "Vtysh configuration") |
4a1ab8e4 DL |
32 | DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CONFIG_LINE, "Vtysh configuration line") |
33 | ||
718e3744 | 34 | vector configvec; |
35 | ||
d62a17ae | 36 | struct config { |
37 | /* Configuration node name. */ | |
38 | char *name; | |
718e3744 | 39 | |
d62a17ae | 40 | /* Configuration string line. */ |
41 | struct list *line; | |
718e3744 | 42 | |
d62a17ae | 43 | /* Configuration can be nest. */ |
44 | struct config *config; | |
718e3744 | 45 | |
d62a17ae | 46 | /* Index of this config. */ |
d7c0a89a | 47 | uint32_t index; |
718e3744 | 48 | }; |
49 | ||
50 | struct list *config_top; | |
95e735b5 | 51 | |
d62a17ae | 52 | static int line_cmp(char *c1, char *c2) |
718e3744 | 53 | { |
d62a17ae | 54 | return strcmp(c1, c2); |
718e3744 | 55 | } |
56 | ||
d62a17ae | 57 | static void line_del(char *line) |
718e3744 | 58 | { |
d62a17ae | 59 | XFREE(MTYPE_VTYSH_CONFIG_LINE, line); |
718e3744 | 60 | } |
61 | ||
d62a17ae | 62 | static struct config *config_new(void) |
718e3744 | 63 | { |
d62a17ae | 64 | struct config *config; |
65 | config = XCALLOC(MTYPE_VTYSH_CONFIG, sizeof(struct config)); | |
66 | return config; | |
718e3744 | 67 | } |
68 | ||
d62a17ae | 69 | static int config_cmp(struct config *c1, struct config *c2) |
718e3744 | 70 | { |
d62a17ae | 71 | return strcmp(c1->name, c2->name); |
718e3744 | 72 | } |
73 | ||
d62a17ae | 74 | static void config_del(struct config *config) |
718e3744 | 75 | { |
6a154c88 | 76 | list_delete(&config->line); |
d62a17ae | 77 | if (config->name) |
78 | XFREE(MTYPE_VTYSH_CONFIG_LINE, config->name); | |
79 | XFREE(MTYPE_VTYSH_CONFIG, config); | |
718e3744 | 80 | } |
81 | ||
d62a17ae | 82 | static struct config *config_get(int index, const char *line) |
718e3744 | 83 | { |
d62a17ae | 84 | struct config *config; |
85 | struct config *config_loop; | |
86 | struct list *master; | |
87 | struct listnode *node, *nnode; | |
88 | ||
89 | config = config_loop = NULL; | |
90 | ||
91 | master = vector_lookup_ensure(configvec, index); | |
92 | ||
93 | if (!master) { | |
94 | master = list_new(); | |
95 | master->del = (void (*)(void *))config_del; | |
96 | master->cmp = (int (*)(void *, void *))config_cmp; | |
97 | vector_set_index(configvec, index, master); | |
98 | } | |
99 | ||
100 | for (ALL_LIST_ELEMENTS(master, node, nnode, config_loop)) { | |
101 | if (strcmp(config_loop->name, line) == 0) | |
102 | config = config_loop; | |
103 | } | |
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; | |
112 | listnode_add(master, config); | |
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 DS |
134 | /* |
135 | * I want to explicitly move this command to the end of the line | |
136 | */ | |
137 | static void config_add_line_end(struct list *config, const char *line) | |
138 | { | |
139 | struct listnode *node; | |
140 | void *item = XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line); | |
141 | ||
142 | listnode_add(config, item); | |
143 | node = listnode_lookup(config, item); | |
144 | if (node) | |
145 | listnode_move_to_tail(config, node); | |
146 | } | |
147 | ||
d62a17ae | 148 | void vtysh_config_parse_line(void *arg, const char *line) |
718e3744 | 149 | { |
d62a17ae | 150 | char c; |
151 | static struct config *config = NULL; | |
152 | ||
153 | if (!line) | |
154 | return; | |
155 | ||
156 | c = line[0]; | |
157 | ||
158 | if (c == '\0') | |
159 | return; | |
160 | ||
161 | /* printf ("[%s]\n", line); */ | |
162 | ||
163 | switch (c) { | |
164 | /* Suppress exclamation points ! and commented lines. The !s are | |
165 | * generated | |
166 | * dynamically in vtysh_config_dump() */ | |
167 | case '!': | |
168 | case '#': | |
169 | break; | |
170 | case ' ': | |
171 | /* Store line to current configuration. */ | |
172 | if (config) { | |
173 | if (strncmp(line, " link-params", | |
174 | strlen(" link-params")) | |
175 | == 0) { | |
176 | config_add_line(config->line, line); | |
177 | config->index = LINK_PARAMS_NODE; | |
996c9314 LB |
178 | } else if (strncmp(line, " ip multicast boundary", |
179 | strlen(" ip multicast boundary")) | |
180 | == 0) { | |
a3c1db5e | 181 | config_add_line_end(config->line, line); |
61a7a236 DS |
182 | } else if (strncmp(line, " ip igmp query-interval", |
183 | strlen(" ip igmp query-interval")) == 0) { | |
184 | config_add_line_end(config->line, line); | |
d62a17ae | 185 | } else if (config->index == LINK_PARAMS_NODE |
186 | && strncmp(line, " exit-link-params", | |
187 | strlen(" exit")) | |
188 | == 0) { | |
189 | config_add_line(config->line, line); | |
190 | config->index = INTERFACE_NODE; | |
c319e19d QY |
191 | } else if (config->index == VRF_NODE |
192 | && strncmp(line, " exit-vrf", | |
193 | strlen(" exit-vrf")) | |
194 | == 0) { | |
195 | config_add_line(config->line, line); | |
196 | config->index = CONFIG_NODE; | |
d62a17ae | 197 | } else if (config->index == RMAP_NODE |
198 | || config->index == INTERFACE_NODE | |
f5d20fdb | 199 | || config->index == LOGICALROUTER_NODE |
d62a17ae | 200 | || config->index == VTY_NODE |
201 | || config->index == VRF_NODE) | |
202 | config_add_line_uniq(config->line, line); | |
203 | else | |
204 | config_add_line(config->line, line); | |
205 | } else | |
206 | config_add_line(config_top, line); | |
207 | break; | |
208 | default: | |
209 | if (strncmp(line, "interface", strlen("interface")) == 0) | |
210 | config = config_get(INTERFACE_NODE, line); | |
2dd0d726 RW |
211 | else if (strncmp(line, "pseudowire", strlen("pseudowire")) == 0) |
212 | config = config_get(PW_NODE, line); | |
34f6bdbe | 213 | else if (strncmp(line, "logical-router", strlen("logical-router")) == 0) |
f5d20fdb | 214 | config = config_get(LOGICALROUTER_NODE, line); |
d62a17ae | 215 | else if (strncmp(line, "vrf", strlen("vrf")) == 0) |
216 | config = config_get(VRF_NODE, line); | |
e5c83d9b DS |
217 | else if (strncmp(line, "nexthop-group", strlen("nexthop-group")) |
218 | == 0) | |
219 | config = config_get(NH_GROUP_NODE, line); | |
d62a17ae | 220 | else if (strncmp(line, "router-id", strlen("router-id")) == 0) |
221 | config = config_get(ZEBRA_NODE, line); | |
222 | else if (strncmp(line, "router rip", strlen("router rip")) == 0) | |
223 | config = config_get(RIP_NODE, line); | |
224 | else if (strncmp(line, "router ripng", strlen("router ripng")) | |
225 | == 0) | |
226 | config = config_get(RIPNG_NODE, line); | |
227 | else if (strncmp(line, "router eigrp", strlen("router eigrp")) | |
228 | == 0) | |
229 | config = config_get(EIGRP_NODE, line); | |
230 | else if (strncmp(line, "router babel", strlen("router babel")) | |
231 | == 0) | |
232 | config = config_get(BABEL_NODE, line); | |
233 | else if (strncmp(line, "router ospf", strlen("router ospf")) | |
234 | == 0) | |
235 | config = config_get(OSPF_NODE, line); | |
236 | else if (strncmp(line, "router ospf6", strlen("router ospf6")) | |
237 | == 0) | |
238 | config = config_get(OSPF6_NODE, line); | |
239 | else if (strncmp(line, "mpls ldp", strlen("mpls ldp")) == 0) | |
240 | config = config_get(LDP_NODE, line); | |
241 | else if (strncmp(line, "l2vpn", strlen("l2vpn")) == 0) | |
242 | config = config_get(LDP_L2VPN_NODE, line); | |
243 | else if (strncmp(line, "router bgp", strlen("router bgp")) == 0) | |
244 | config = config_get(BGP_NODE, line); | |
245 | else if (strncmp(line, "router isis", strlen("router isis")) | |
246 | == 0) | |
247 | config = config_get(ISIS_NODE, line); | |
770ccdf8 CF |
248 | else if (strncmp(line, "router openfabric", strlen("router openfabric")) |
249 | == 0) | |
250 | config = config_get(OPENFABRIC_NODE, line); | |
d62a17ae | 251 | else if (strncmp(line, "route-map", strlen("route-map")) == 0) |
252 | config = config_get(RMAP_NODE, line); | |
e5c83d9b DS |
253 | else if (strncmp(line, "pbr-map", strlen("pbr-map")) == 0) |
254 | config = config_get(PBRMAP_NODE, line); | |
d62a17ae | 255 | else if (strncmp(line, "access-list", strlen("access-list")) |
256 | == 0) | |
257 | config = config_get(ACCESS_NODE, line); | |
258 | else if (strncmp(line, "ipv6 access-list", | |
259 | strlen("ipv6 access-list")) | |
260 | == 0) | |
261 | config = config_get(ACCESS_IPV6_NODE, line); | |
d37ba549 MK |
262 | else if (strncmp(line, "mac access-list", |
263 | strlen("mac access-list")) | |
264 | == 0) | |
265 | config = config_get(ACCESS_MAC_NODE, line); | |
d62a17ae | 266 | else if (strncmp(line, "ip prefix-list", |
267 | strlen("ip prefix-list")) | |
268 | == 0) | |
269 | config = config_get(PREFIX_NODE, line); | |
270 | else if (strncmp(line, "ipv6 prefix-list", | |
271 | strlen("ipv6 prefix-list")) | |
272 | == 0) | |
273 | config = config_get(PREFIX_IPV6_NODE, line); | |
7336e101 SP |
274 | else if (strncmp(line, "bgp as-path access-list", |
275 | strlen("bgp as-path access-list")) | |
d62a17ae | 276 | == 0) |
277 | config = config_get(AS_LIST_NODE, line); | |
7336e101 SP |
278 | else if (strncmp(line, "bgp community-list", |
279 | strlen("bgp community-list")) | |
d62a17ae | 280 | == 0 |
7336e101 SP |
281 | || strncmp(line, "bgp extcommunity-list", |
282 | strlen("bgp extcommunity-list")) | |
996c9314 | 283 | == 0 |
7336e101 SP |
284 | || strncmp(line, "bgp large-community-list", |
285 | strlen("bgp large-community-list")) | |
996c9314 | 286 | == 0) |
d62a17ae | 287 | config = config_get(COMMUNITY_LIST_NODE, line); |
288 | else if (strncmp(line, "ip route", strlen("ip route")) == 0) | |
289 | config = config_get(IP_NODE, line); | |
290 | else if (strncmp(line, "ipv6 route", strlen("ipv6 route")) == 0) | |
291 | config = config_get(IP_NODE, line); | |
292 | else if (strncmp(line, "key", strlen("key")) == 0) | |
293 | config = config_get(KEYCHAIN_NODE, line); | |
294 | else if (strncmp(line, "line", strlen("line")) == 0) | |
295 | config = config_get(VTY_NODE, line); | |
296 | else if ((strncmp(line, "ipv6 forwarding", | |
297 | strlen("ipv6 forwarding")) | |
298 | == 0) | |
299 | || (strncmp(line, "ip forwarding", | |
300 | strlen("ip forwarding")) | |
301 | == 0)) | |
302 | config = config_get(FORWARDING_NODE, line); | |
d62a17ae | 303 | else if (strncmp(line, "debug vrf", strlen("debug vrf")) == 0) |
304 | config = config_get(VRF_DEBUG_NODE, line); | |
305 | else if (strncmp(line, "debug", strlen("debug")) == 0) | |
306 | config = config_get(DEBUG_NODE, line); | |
307 | else if (strncmp(line, "password", strlen("password")) == 0 | |
308 | || strncmp(line, "enable password", | |
309 | strlen("enable password")) | |
310 | == 0) | |
311 | config = config_get(AAA_NODE, line); | |
312 | else if (strncmp(line, "ip protocol", strlen("ip protocol")) | |
313 | == 0) | |
314 | config = config_get(PROTOCOL_NODE, line); | |
315 | else if (strncmp(line, "ipv6 protocol", strlen("ipv6 protocol")) | |
316 | == 0) | |
317 | config = config_get(PROTOCOL_NODE, line); | |
318 | else if (strncmp(line, "ip nht", strlen("ip nht")) == 0) | |
319 | config = config_get(PROTOCOL_NODE, line); | |
320 | else if (strncmp(line, "ipv6 nht", strlen("ipv6 nht")) == 0) | |
321 | config = config_get(PROTOCOL_NODE, line); | |
322 | else if (strncmp(line, "mpls", strlen("mpls")) == 0) | |
323 | config = config_get(MPLS_NODE, line); | |
c2f29cf3 RZ |
324 | else if (strncmp(line, "bfd", strlen("bfd")) == 0) |
325 | config = config_get(BFD_NODE, line); | |
d62a17ae | 326 | else { |
327 | if (strncmp(line, "log", strlen("log")) == 0 | |
328 | || strncmp(line, "hostname", strlen("hostname")) | |
329 | == 0 | |
330 | || strncmp(line, "frr", strlen("frr")) == 0 | |
331 | || strncmp(line, "agentx", strlen("agentx")) == 0 | |
332 | || strncmp(line, "no log", strlen("no log")) == 0) | |
333 | config_add_line_uniq(config_top, line); | |
334 | else | |
335 | config_add_line(config_top, line); | |
336 | config = NULL; | |
337 | } | |
338 | break; | |
718e3744 | 339 | } |
718e3744 | 340 | } |
341 | ||
718e3744 | 342 | /* Macro to check delimiter is needed between each configuration line |
95e735b5 | 343 | * or not. */ |
d62a17ae | 344 | #define NO_DELIMITER(I) \ |
345 | ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \ | |
346 | || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE \ | |
d37ba549 | 347 | || (I) == ACCESS_IPV6_NODE || (I) == ACCESS_MAC_NODE \ |
f108d487 QY |
348 | || (I) == PREFIX_IPV6_NODE || (I) == FORWARDING_NODE \ |
349 | || (I) == DEBUG_NODE || (I) == AAA_NODE || (I) == VRF_DEBUG_NODE \ | |
350 | || (I) == MPLS_NODE) | |
718e3744 | 351 | |
95e735b5 | 352 | /* Display configuration to file pointer. */ |
0a334343 | 353 | void vtysh_config_dump(void) |
718e3744 | 354 | { |
d62a17ae | 355 | struct listnode *node, *nnode; |
356 | struct listnode *mnode, *mnnode; | |
357 | struct config *config; | |
358 | struct list *master; | |
359 | char *line; | |
360 | unsigned int i; | |
361 | ||
2cddf2ff QY |
362 | for (ALL_LIST_ELEMENTS(config_top, node, nnode, line)) |
363 | vty_out(vty, "%s\n", line); | |
364 | ||
365 | vty_out(vty, "!\n"); | |
d62a17ae | 366 | |
367 | for (i = 0; i < vector_active(configvec); i++) | |
368 | if ((master = vector_slot(configvec, i)) != NULL) { | |
369 | for (ALL_LIST_ELEMENTS(master, node, nnode, config)) { | |
1d72e48a | 370 | /* Don't print empty sections for interface. |
d62a17ae | 371 | * Route maps on the |
372 | * other hand could have a legitimate empty | |
373 | * section at the end. | |
1d72e48a | 374 | * VRF is handled in the backend, we could have |
375 | * "configured" VRFs with static routes which | |
376 | * are not under the VRF node. | |
d62a17ae | 377 | */ |
1d72e48a | 378 | if (config->index == INTERFACE_NODE |
d62a17ae | 379 | && list_isempty(config->line)) |
380 | continue; | |
381 | ||
2cddf2ff | 382 | vty_out(vty, "%s\n", config->name); |
d62a17ae | 383 | |
384 | for (ALL_LIST_ELEMENTS(config->line, mnode, | |
2cddf2ff QY |
385 | mnnode, line)) |
386 | vty_out(vty, "%s\n", line); | |
387 | if (!NO_DELIMITER(i)) | |
388 | vty_out(vty, "!\n"); | |
d62a17ae | 389 | } |
2cddf2ff QY |
390 | if (NO_DELIMITER(i)) |
391 | vty_out(vty, "!\n"); | |
d62a17ae | 392 | } |
393 | ||
394 | for (i = 0; i < vector_active(configvec); i++) | |
395 | if ((master = vector_slot(configvec, i)) != NULL) { | |
6a154c88 | 396 | list_delete(&master); |
d62a17ae | 397 | vector_slot(configvec, i) = NULL; |
398 | } | |
399 | list_delete_all_node(config_top); | |
718e3744 | 400 | } |
401 | ||
402 | /* Read up configuration file from file_name. */ | |
d62a17ae | 403 | static int vtysh_read_file(FILE *confp) |
718e3744 | 404 | { |
d62a17ae | 405 | struct vty *vty; |
406 | int ret; | |
407 | ||
408 | vty = vty_new(); | |
4a9746fd | 409 | vty->wfd = STDERR_FILENO; |
d62a17ae | 410 | vty->type = VTY_TERM; |
411 | vty->node = CONFIG_NODE; | |
718e3744 | 412 | |
d62a17ae | 413 | vtysh_execute_no_pager("enable"); |
414 | vtysh_execute_no_pager("configure terminal"); | |
718e3744 | 415 | |
d62a17ae | 416 | /* Execute configuration file. */ |
417 | ret = vtysh_config_from_file(vty, confp); | |
718e3744 | 418 | |
d62a17ae | 419 | vtysh_execute_no_pager("end"); |
420 | vtysh_execute_no_pager("disable"); | |
718e3744 | 421 | |
d62a17ae | 422 | vty_close(vty); |
3221dca8 | 423 | |
d62a17ae | 424 | return (ret); |
718e3744 | 425 | } |
426 | ||
e7168df4 | 427 | /* Read up configuration file from config_default_dir. */ |
d62a17ae | 428 | int vtysh_read_config(const char *config_default_dir) |
718e3744 | 429 | { |
d62a17ae | 430 | FILE *confp = NULL; |
431 | int ret; | |
432 | ||
433 | confp = fopen(config_default_dir, "r"); | |
434 | if (confp == NULL) { | |
435 | fprintf(stderr, | |
436 | "%% Can't open configuration file %s due to '%s'.\n", | |
437 | config_default_dir, safe_strerror(errno)); | |
438 | return (CMD_ERR_NO_FILE); | |
439 | } | |
67e29abc | 440 | |
d62a17ae | 441 | ret = vtysh_read_file(confp); |
442 | fclose(confp); | |
718e3744 | 443 | |
d62a17ae | 444 | return (ret); |
718e3744 | 445 | } |
446 | ||
e7168df4 | 447 | /* We don't write vtysh specific into file from vtysh. vtysh.conf should |
448 | * be edited by hand. So, we handle only "write terminal" case here and | |
449 | * integrate vtysh specific conf with conf from daemons. | |
450 | */ | |
d62a17ae | 451 | void vtysh_config_write() |
718e3744 | 452 | { |
d62a17ae | 453 | char line[81]; |
d62a17ae | 454 | |
6b3ee3a0 MK |
455 | if (cmd_hostname_get()) { |
456 | sprintf(line, "hostname %s", cmd_hostname_get()); | |
d62a17ae | 457 | vtysh_config_parse_line(NULL, line); |
458 | } | |
3b103fec MK |
459 | |
460 | if (cmd_domainname_get()) { | |
461 | sprintf(line, "domainname %s", cmd_domainname_get()); | |
462 | vtysh_config_parse_line(NULL, line); | |
463 | } | |
d62a17ae | 464 | if (vtysh_write_integrated == WRITE_INTEGRATED_NO) |
465 | vtysh_config_parse_line(NULL, | |
466 | "no service integrated-vtysh-config"); | |
467 | if (vtysh_write_integrated == WRITE_INTEGRATED_YES) | |
468 | vtysh_config_parse_line(NULL, | |
469 | "service integrated-vtysh-config"); | |
470 | ||
471 | user_config_write(); | |
718e3744 | 472 | } |
473 | ||
d62a17ae | 474 | void vtysh_config_init() |
718e3744 | 475 | { |
d62a17ae | 476 | config_top = list_new(); |
477 | config_top->del = (void (*)(void *))line_del; | |
478 | configvec = vector_init(1); | |
718e3744 | 479 | } |