]>
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. */ |
47 | u_int32_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 | { |
d62a17ae | 76 | list_delete(config->line); |
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; | |
a3c1db5e DS |
178 | } else if (strncmp(line, |
179 | " ip multicast boundary", | |
180 | strlen(" ip multicast boundary")) == 0) { | |
181 | config_add_line_end(config->line, line); | |
d62a17ae | 182 | } else if (config->index == LINK_PARAMS_NODE |
183 | && strncmp(line, " exit-link-params", | |
184 | strlen(" exit")) | |
185 | == 0) { | |
186 | config_add_line(config->line, line); | |
187 | config->index = INTERFACE_NODE; | |
188 | } else if (config->index == RMAP_NODE | |
189 | || config->index == INTERFACE_NODE | |
190 | || config->index == NS_NODE | |
191 | || config->index == VTY_NODE | |
192 | || config->index == VRF_NODE) | |
193 | config_add_line_uniq(config->line, line); | |
194 | else | |
195 | config_add_line(config->line, line); | |
196 | } else | |
197 | config_add_line(config_top, line); | |
198 | break; | |
199 | default: | |
200 | if (strncmp(line, "interface", strlen("interface")) == 0) | |
201 | config = config_get(INTERFACE_NODE, line); | |
2dd0d726 RW |
202 | else if (strncmp(line, "pseudowire", strlen("pseudowire")) == 0) |
203 | config = config_get(PW_NODE, line); | |
d62a17ae | 204 | else if (strncmp(line, "logical-router", strlen("ns")) == 0) |
205 | config = config_get(NS_NODE, line); | |
206 | else if (strncmp(line, "vrf", strlen("vrf")) == 0) | |
207 | config = config_get(VRF_NODE, line); | |
208 | else if (strncmp(line, "router-id", strlen("router-id")) == 0) | |
209 | config = config_get(ZEBRA_NODE, line); | |
210 | else if (strncmp(line, "router rip", strlen("router rip")) == 0) | |
211 | config = config_get(RIP_NODE, line); | |
212 | else if (strncmp(line, "router ripng", strlen("router ripng")) | |
213 | == 0) | |
214 | config = config_get(RIPNG_NODE, line); | |
215 | else if (strncmp(line, "router eigrp", strlen("router eigrp")) | |
216 | == 0) | |
217 | config = config_get(EIGRP_NODE, line); | |
218 | else if (strncmp(line, "router babel", strlen("router babel")) | |
219 | == 0) | |
220 | config = config_get(BABEL_NODE, line); | |
221 | else if (strncmp(line, "router ospf", strlen("router ospf")) | |
222 | == 0) | |
223 | config = config_get(OSPF_NODE, line); | |
224 | else if (strncmp(line, "router ospf6", strlen("router ospf6")) | |
225 | == 0) | |
226 | config = config_get(OSPF6_NODE, line); | |
227 | else if (strncmp(line, "mpls ldp", strlen("mpls ldp")) == 0) | |
228 | config = config_get(LDP_NODE, line); | |
229 | else if (strncmp(line, "l2vpn", strlen("l2vpn")) == 0) | |
230 | config = config_get(LDP_L2VPN_NODE, line); | |
231 | else if (strncmp(line, "router bgp", strlen("router bgp")) == 0) | |
232 | config = config_get(BGP_NODE, line); | |
233 | else if (strncmp(line, "router isis", strlen("router isis")) | |
234 | == 0) | |
235 | config = config_get(ISIS_NODE, line); | |
236 | else if (strncmp(line, "route-map", strlen("route-map")) == 0) | |
237 | config = config_get(RMAP_NODE, line); | |
238 | else if (strncmp(line, "access-list", strlen("access-list")) | |
239 | == 0) | |
240 | config = config_get(ACCESS_NODE, line); | |
241 | else if (strncmp(line, "ipv6 access-list", | |
242 | strlen("ipv6 access-list")) | |
243 | == 0) | |
244 | config = config_get(ACCESS_IPV6_NODE, line); | |
d37ba549 MK |
245 | else if (strncmp(line, "mac access-list", |
246 | strlen("mac access-list")) | |
247 | == 0) | |
248 | config = config_get(ACCESS_MAC_NODE, line); | |
d62a17ae | 249 | else if (strncmp(line, "ip prefix-list", |
250 | strlen("ip prefix-list")) | |
251 | == 0) | |
252 | config = config_get(PREFIX_NODE, line); | |
253 | else if (strncmp(line, "ipv6 prefix-list", | |
254 | strlen("ipv6 prefix-list")) | |
255 | == 0) | |
256 | config = config_get(PREFIX_IPV6_NODE, line); | |
257 | else if (strncmp(line, "ip as-path access-list", | |
258 | strlen("ip as-path access-list")) | |
259 | == 0) | |
260 | config = config_get(AS_LIST_NODE, line); | |
261 | else if (strncmp(line, "ip community-list", | |
262 | strlen("ip community-list")) | |
263 | == 0 | |
264 | || strncmp(line, "ip extcommunity-list", | |
265 | strlen("ip extcommunity-list")) | |
344d9016 NK |
266 | == 0 |
267 | || strncmp(line, "ip large-community-list", | |
268 | strlen("ip large-community-list")) | |
269 | == 0) | |
d62a17ae | 270 | config = config_get(COMMUNITY_LIST_NODE, line); |
271 | else if (strncmp(line, "ip route", strlen("ip route")) == 0) | |
272 | config = config_get(IP_NODE, line); | |
273 | else if (strncmp(line, "ipv6 route", strlen("ipv6 route")) == 0) | |
274 | config = config_get(IP_NODE, line); | |
275 | else if (strncmp(line, "key", strlen("key")) == 0) | |
276 | config = config_get(KEYCHAIN_NODE, line); | |
277 | else if (strncmp(line, "line", strlen("line")) == 0) | |
278 | config = config_get(VTY_NODE, line); | |
279 | else if ((strncmp(line, "ipv6 forwarding", | |
280 | strlen("ipv6 forwarding")) | |
281 | == 0) | |
282 | || (strncmp(line, "ip forwarding", | |
283 | strlen("ip forwarding")) | |
284 | == 0)) | |
285 | config = config_get(FORWARDING_NODE, line); | |
286 | else if (strncmp(line, "service", strlen("service")) == 0) | |
287 | config = config_get(SERVICE_NODE, line); | |
288 | else if (strncmp(line, "debug vrf", strlen("debug vrf")) == 0) | |
289 | config = config_get(VRF_DEBUG_NODE, line); | |
290 | else if (strncmp(line, "debug", strlen("debug")) == 0) | |
291 | config = config_get(DEBUG_NODE, line); | |
292 | else if (strncmp(line, "password", strlen("password")) == 0 | |
293 | || strncmp(line, "enable password", | |
294 | strlen("enable password")) | |
295 | == 0) | |
296 | config = config_get(AAA_NODE, line); | |
297 | else if (strncmp(line, "ip protocol", strlen("ip protocol")) | |
298 | == 0) | |
299 | config = config_get(PROTOCOL_NODE, line); | |
300 | else if (strncmp(line, "ipv6 protocol", strlen("ipv6 protocol")) | |
301 | == 0) | |
302 | config = config_get(PROTOCOL_NODE, line); | |
303 | else if (strncmp(line, "ip nht", strlen("ip nht")) == 0) | |
304 | config = config_get(PROTOCOL_NODE, line); | |
305 | else if (strncmp(line, "ipv6 nht", strlen("ipv6 nht")) == 0) | |
306 | config = config_get(PROTOCOL_NODE, line); | |
307 | else if (strncmp(line, "mpls", strlen("mpls")) == 0) | |
308 | config = config_get(MPLS_NODE, line); | |
309 | else { | |
310 | if (strncmp(line, "log", strlen("log")) == 0 | |
311 | || strncmp(line, "hostname", strlen("hostname")) | |
312 | == 0 | |
313 | || strncmp(line, "frr", strlen("frr")) == 0 | |
314 | || strncmp(line, "agentx", strlen("agentx")) == 0 | |
315 | || strncmp(line, "no log", strlen("no log")) == 0) | |
316 | config_add_line_uniq(config_top, line); | |
317 | else | |
318 | config_add_line(config_top, line); | |
319 | config = NULL; | |
320 | } | |
321 | break; | |
718e3744 | 322 | } |
718e3744 | 323 | } |
324 | ||
718e3744 | 325 | /* Macro to check delimiter is needed between each configuration line |
95e735b5 | 326 | * or not. */ |
d62a17ae | 327 | #define NO_DELIMITER(I) \ |
328 | ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \ | |
329 | || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE \ | |
d37ba549 MK |
330 | || (I) == ACCESS_IPV6_NODE || (I) == ACCESS_MAC_NODE \ |
331 | || (I) == PREFIX_IPV6_NODE || (I) == SERVICE_NODE \ | |
332 | || (I) == FORWARDING_NODE || (I) == DEBUG_NODE || (I) == AAA_NODE \ | |
333 | || (I) == VRF_DEBUG_NODE || (I) == MPLS_NODE) | |
718e3744 | 334 | |
95e735b5 | 335 | /* Display configuration to file pointer. */ |
d62a17ae | 336 | void vtysh_config_dump(FILE *fp) |
718e3744 | 337 | { |
d62a17ae | 338 | struct listnode *node, *nnode; |
339 | struct listnode *mnode, *mnnode; | |
340 | struct config *config; | |
341 | struct list *master; | |
342 | char *line; | |
343 | unsigned int i; | |
344 | ||
345 | for (ALL_LIST_ELEMENTS(config_top, node, nnode, line)) { | |
346 | fprintf(fp, "%s\n", line); | |
347 | fflush(fp); | |
348 | } | |
349 | fprintf(fp, "!\n"); | |
350 | fflush(fp); | |
351 | ||
352 | for (i = 0; i < vector_active(configvec); i++) | |
353 | if ((master = vector_slot(configvec, i)) != NULL) { | |
354 | for (ALL_LIST_ELEMENTS(master, node, nnode, config)) { | |
355 | /* Don't print empty sections for interface/vrf. | |
356 | * Route maps on the | |
357 | * other hand could have a legitimate empty | |
358 | * section at the end. | |
359 | */ | |
360 | if ((config->index == INTERFACE_NODE | |
361 | || config->index == VRF_NODE) | |
362 | && list_isempty(config->line)) | |
363 | continue; | |
364 | ||
365 | fprintf(fp, "%s\n", config->name); | |
366 | fflush(fp); | |
367 | ||
368 | for (ALL_LIST_ELEMENTS(config->line, mnode, | |
369 | mnnode, line)) { | |
370 | fprintf(fp, "%s\n", line); | |
371 | fflush(fp); | |
372 | } | |
373 | if (!NO_DELIMITER(i)) { | |
374 | fprintf(fp, "!\n"); | |
375 | fflush(fp); | |
376 | } | |
377 | } | |
378 | if (NO_DELIMITER(i)) { | |
379 | fprintf(fp, "!\n"); | |
380 | fflush(fp); | |
381 | } | |
382 | } | |
383 | ||
384 | for (i = 0; i < vector_active(configvec); i++) | |
385 | if ((master = vector_slot(configvec, i)) != NULL) { | |
386 | list_delete(master); | |
387 | vector_slot(configvec, i) = NULL; | |
388 | } | |
389 | list_delete_all_node(config_top); | |
718e3744 | 390 | } |
391 | ||
392 | /* Read up configuration file from file_name. */ | |
d62a17ae | 393 | static int vtysh_read_file(FILE *confp) |
718e3744 | 394 | { |
d62a17ae | 395 | struct vty *vty; |
396 | int ret; | |
397 | ||
398 | vty = vty_new(); | |
399 | vty->fd = 0; /* stdout */ | |
400 | vty->type = VTY_TERM; | |
401 | vty->node = CONFIG_NODE; | |
718e3744 | 402 | |
d62a17ae | 403 | vtysh_execute_no_pager("enable"); |
404 | vtysh_execute_no_pager("configure terminal"); | |
718e3744 | 405 | |
d62a17ae | 406 | /* Execute configuration file. */ |
407 | ret = vtysh_config_from_file(vty, confp); | |
718e3744 | 408 | |
d62a17ae | 409 | vtysh_execute_no_pager("end"); |
410 | vtysh_execute_no_pager("disable"); | |
718e3744 | 411 | |
d62a17ae | 412 | vty_close(vty); |
3221dca8 | 413 | |
d62a17ae | 414 | return (ret); |
718e3744 | 415 | } |
416 | ||
e7168df4 | 417 | /* Read up configuration file from config_default_dir. */ |
d62a17ae | 418 | int vtysh_read_config(const char *config_default_dir) |
718e3744 | 419 | { |
d62a17ae | 420 | FILE *confp = NULL; |
421 | int ret; | |
422 | ||
423 | confp = fopen(config_default_dir, "r"); | |
424 | if (confp == NULL) { | |
425 | fprintf(stderr, | |
426 | "%% Can't open configuration file %s due to '%s'.\n", | |
427 | config_default_dir, safe_strerror(errno)); | |
428 | return (CMD_ERR_NO_FILE); | |
429 | } | |
67e29abc | 430 | |
d62a17ae | 431 | ret = vtysh_read_file(confp); |
432 | fclose(confp); | |
718e3744 | 433 | |
d62a17ae | 434 | return (ret); |
718e3744 | 435 | } |
436 | ||
e7168df4 | 437 | /* We don't write vtysh specific into file from vtysh. vtysh.conf should |
438 | * be edited by hand. So, we handle only "write terminal" case here and | |
439 | * integrate vtysh specific conf with conf from daemons. | |
440 | */ | |
d62a17ae | 441 | void vtysh_config_write() |
718e3744 | 442 | { |
d62a17ae | 443 | char line[81]; |
d62a17ae | 444 | |
6b3ee3a0 MK |
445 | if (cmd_hostname_get()) { |
446 | sprintf(line, "hostname %s", cmd_hostname_get()); | |
d62a17ae | 447 | vtysh_config_parse_line(NULL, line); |
448 | } | |
449 | if (vtysh_write_integrated == WRITE_INTEGRATED_NO) | |
450 | vtysh_config_parse_line(NULL, | |
451 | "no service integrated-vtysh-config"); | |
452 | if (vtysh_write_integrated == WRITE_INTEGRATED_YES) | |
453 | vtysh_config_parse_line(NULL, | |
454 | "service integrated-vtysh-config"); | |
455 | ||
456 | user_config_write(); | |
718e3744 | 457 | } |
458 | ||
d62a17ae | 459 | void vtysh_config_init() |
718e3744 | 460 | { |
d62a17ae | 461 | config_top = list_new(); |
462 | config_top->del = (void (*)(void *))line_del; | |
463 | configvec = vector_init(1); | |
718e3744 | 464 | } |