1 /* Configuration generator.
2 * Copyright (C) 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
28 #include "vtysh/vtysh.h"
29 #include "vtysh/vtysh_user.h"
31 DEFINE_MGROUP(MVTYSH
, "vtysh")
32 DEFINE_MTYPE_STATIC(MVTYSH
, VTYSH_CONFIG
, "Vtysh configuration")
33 DEFINE_MTYPE_STATIC(MVTYSH
, VTYSH_CONFIG_LINE
, "Vtysh configuration line")
37 PREDECL_RBTREE_UNIQ(config_master
);
40 /* Configuration node name. */
43 /* Configuration string line. */
46 /* Configuration can be nest. */
47 struct config
*config
;
49 /* Index of this config. */
52 /* Node entry for the typed Red-black tree */
53 struct config_master_item rbt_item
;
56 struct list
*config_top
;
58 static int line_cmp(char *c1
, char *c2
)
60 return strcmp(c1
, c2
);
63 static void line_del(char *line
)
65 XFREE(MTYPE_VTYSH_CONFIG_LINE
, line
);
68 static struct config
*config_new(void)
70 struct config
*config
;
71 config
= XCALLOC(MTYPE_VTYSH_CONFIG
, sizeof(struct config
));
75 static int config_cmp(const struct config
*c1
, const struct config
*c2
)
77 return strcmp(c1
->name
, c2
->name
);
80 static void config_del(struct config
*config
)
82 list_delete(&config
->line
);
83 XFREE(MTYPE_VTYSH_CONFIG_LINE
, config
->name
);
84 XFREE(MTYPE_VTYSH_CONFIG
, config
);
87 DECLARE_RBTREE_UNIQ(config_master
, struct config
, rbt_item
, config_cmp
)
89 static struct config
*config_get(int index
, const char *line
)
91 struct config
*config
;
92 struct config_master_head
*master
;
94 master
= vector_lookup_ensure(configvec
, index
);
97 master
= XMALLOC(MTYPE_VTYSH_CONFIG
, sizeof(struct config_master_head
));
98 config_master_init(master
);
99 vector_set_index(configvec
, index
, master
);
102 const struct config config_ref
= { .name
= (char *)line
};
103 config
= config_master_find(master
, &config_ref
);
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 config_master_add(master
, config
);
117 void config_add_line(struct list
*config
, const char *line
)
119 listnode_add(config
, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE
, line
));
122 static void config_add_line_uniq(struct list
*config
, const char *line
)
124 struct listnode
*node
, *nnode
;
127 for (ALL_LIST_ELEMENTS(config
, node
, nnode
, pnt
)) {
128 if (strcmp(pnt
, line
) == 0)
131 listnode_add_sort(config
, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE
, line
));
135 * Add a line that should only be shown once, and always show at the end of the
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.
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.
147 * Here is a motivating example with the 'exit-vrf' command. Suppose we receive
148 * a config from Zebra like so:
155 * Then suppose we later receive this config from PIM:
158 * ip msdp mesh-group MyGroup member 1.2.3.4
161 * Then we will combine them into one config block like so:
166 * ip msdp mesh-group MyGroup member 1.2.3.4
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
178 * ip msdp mesh-group MyGroup member 1.2.3.4
180 * Therefore, daemons that share config blocks must take care to consistently
181 * print the same block terminators.
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.
194 * The config to add the line to
197 * The line to add to the end of the config
199 static void config_add_line_uniq_end(struct list
*config
, const char *line
)
201 struct listnode
*node
;
204 for (ALL_LIST_ELEMENTS_RO(config
, node
, pnt
)) {
205 if (strcmp(pnt
, line
) == 0)
210 config_add_line(config
, line
);
212 listnode_move_to_tail(config
, node
);
215 void vtysh_config_parse_line(void *arg
, const char *line
)
218 static struct config
*config
= NULL
;
229 /* Suppress exclamation points ! and commented lines. The !s are
231 * dynamically in vtysh_config_dump() */
236 /* Store line to current configuration. */
238 if (strncmp(line
, " link-params",
239 strlen(" link-params"))
241 config_add_line(config
->line
, line
);
242 config
->index
= LINK_PARAMS_NODE
;
243 } else if (strncmp(line
, " ip multicast boundary",
244 strlen(" ip multicast boundary"))
246 config_add_line_uniq_end(config
->line
, line
);
247 } else if (strncmp(line
, " ip igmp query-interval",
248 strlen(" ip igmp query-interval")) == 0) {
249 config_add_line_uniq_end(config
->line
, line
);
250 } else if (config
->index
== LINK_PARAMS_NODE
251 && strncmp(line
, " exit-link-params",
254 config_add_line(config
->line
, line
);
255 config
->index
= INTERFACE_NODE
;
256 } else if (config
->index
== VRF_NODE
257 && strncmp(line
, " exit-vrf",
260 config_add_line_uniq_end(config
->line
, line
);
261 } else if (!strncmp(line
, " vrrp", strlen(" vrrp"))
262 || !strncmp(line
, " no vrrp",
263 strlen(" no vrrp"))) {
264 config_add_line(config
->line
, line
);
265 } else if (config
->index
== RMAP_NODE
266 || config
->index
== INTERFACE_NODE
267 || config
->index
== LOGICALROUTER_NODE
268 || config
->index
== VTY_NODE
269 || config
->index
== VRF_NODE
270 || config
->index
== NH_GROUP_NODE
)
271 config_add_line_uniq(config
->line
, line
);
273 config_add_line(config
->line
, line
);
275 config_add_line(config_top
, line
);
278 if (strncmp(line
, "interface", strlen("interface")) == 0)
279 config
= config_get(INTERFACE_NODE
, line
);
280 else if (strncmp(line
, "pseudowire", strlen("pseudowire")) == 0)
281 config
= config_get(PW_NODE
, line
);
282 else if (strncmp(line
, "logical-router", strlen("logical-router")) == 0)
283 config
= config_get(LOGICALROUTER_NODE
, line
);
284 else if (strncmp(line
, "vrf", strlen("vrf")) == 0)
285 config
= config_get(VRF_NODE
, line
);
286 else if (strncmp(line
, "nexthop-group", strlen("nexthop-group"))
288 config
= config_get(NH_GROUP_NODE
, line
);
289 else if (strncmp(line
, "router-id", strlen("router-id")) == 0)
290 config
= config_get(ZEBRA_NODE
, line
);
291 else if (strncmp(line
, "router rip", strlen("router rip")) == 0)
292 config
= config_get(RIP_NODE
, line
);
293 else if (strncmp(line
, "router ripng", strlen("router ripng"))
295 config
= config_get(RIPNG_NODE
, line
);
296 else if (strncmp(line
, "router eigrp", strlen("router eigrp"))
298 config
= config_get(EIGRP_NODE
, line
);
299 else if (strncmp(line
, "router babel", strlen("router babel"))
301 config
= config_get(BABEL_NODE
, line
);
302 else if (strncmp(line
, "router ospf", strlen("router ospf"))
304 config
= config_get(OSPF_NODE
, line
);
305 else if (strncmp(line
, "router ospf6", strlen("router ospf6"))
307 config
= config_get(OSPF6_NODE
, line
);
308 else if (strncmp(line
, "mpls ldp", strlen("mpls ldp")) == 0)
309 config
= config_get(LDP_NODE
, line
);
310 else if (strncmp(line
, "l2vpn", strlen("l2vpn")) == 0)
311 config
= config_get(LDP_L2VPN_NODE
, line
);
312 else if (strncmp(line
, "router bgp", strlen("router bgp")) == 0)
313 config
= config_get(BGP_NODE
, line
);
314 else if (strncmp(line
, "router isis", strlen("router isis"))
316 config
= config_get(ISIS_NODE
, line
);
317 else if (strncmp(line
, "router openfabric", strlen("router openfabric"))
319 config
= config_get(OPENFABRIC_NODE
, line
);
320 else if (strncmp(line
, "route-map", strlen("route-map")) == 0)
321 config
= config_get(RMAP_NODE
, line
);
322 else if (strncmp(line
, "pbr-map", strlen("pbr-map")) == 0)
323 config
= config_get(PBRMAP_NODE
, line
);
324 else if (strncmp(line
, "access-list", strlen("access-list"))
326 config
= config_get(ACCESS_NODE
, line
);
327 else if (strncmp(line
, "ipv6 access-list",
328 strlen("ipv6 access-list"))
330 config
= config_get(ACCESS_IPV6_NODE
, line
);
331 else if (strncmp(line
, "mac access-list",
332 strlen("mac access-list"))
334 config
= config_get(ACCESS_MAC_NODE
, line
);
335 else if (strncmp(line
, "ip prefix-list",
336 strlen("ip prefix-list"))
338 config
= config_get(PREFIX_NODE
, line
);
339 else if (strncmp(line
, "ipv6 prefix-list",
340 strlen("ipv6 prefix-list"))
342 config
= config_get(PREFIX_IPV6_NODE
, line
);
343 else if (strncmp(line
, "bgp as-path access-list",
344 strlen("bgp as-path access-list"))
346 config
= config_get(AS_LIST_NODE
, line
);
347 else if (strncmp(line
, "bgp community-list",
348 strlen("bgp community-list"))
350 || strncmp(line
, "bgp extcommunity-list",
351 strlen("bgp extcommunity-list"))
353 || strncmp(line
, "bgp large-community-list",
354 strlen("bgp large-community-list"))
356 config
= config_get(COMMUNITY_LIST_NODE
, line
);
357 else if (strncmp(line
, "ip route", strlen("ip route")) == 0)
358 config
= config_get(IP_NODE
, line
);
359 else if (strncmp(line
, "ipv6 route", strlen("ipv6 route")) == 0)
360 config
= config_get(IP_NODE
, line
);
361 else if (strncmp(line
, "key", strlen("key")) == 0)
362 config
= config_get(KEYCHAIN_NODE
, line
);
363 else if (strncmp(line
, "line", strlen("line")) == 0)
364 config
= config_get(VTY_NODE
, line
);
365 else if ((strncmp(line
, "ipv6 forwarding",
366 strlen("ipv6 forwarding"))
368 || (strncmp(line
, "ip forwarding",
369 strlen("ip forwarding"))
371 config
= config_get(FORWARDING_NODE
, line
);
372 else if (strncmp(line
, "debug vrf", strlen("debug vrf")) == 0)
373 config
= config_get(VRF_DEBUG_NODE
, line
);
374 else if (strncmp(line
, "debug northbound",
375 strlen("debug northbound"))
377 config
= config_get(NORTHBOUND_DEBUG_NODE
, line
);
378 else if (strncmp(line
, "debug route-map",
379 strlen("debug route-map"))
381 config
= config_get(RMAP_DEBUG_NODE
, line
);
382 else if (strncmp(line
, "debug resolver",
383 strlen("debug resolver")) == 0)
384 config
= config_get(RESOLVER_DEBUG_NODE
, line
);
385 else if (strncmp(line
, "debug", strlen("debug")) == 0)
386 config
= config_get(DEBUG_NODE
, line
);
387 else if (strncmp(line
, "password", strlen("password")) == 0
388 || strncmp(line
, "enable password",
389 strlen("enable password"))
391 config
= config_get(AAA_NODE
, line
);
392 else if (strncmp(line
, "ip protocol", strlen("ip protocol"))
394 config
= config_get(PROTOCOL_NODE
, line
);
395 else if (strncmp(line
, "ipv6 protocol", strlen("ipv6 protocol"))
397 config
= config_get(PROTOCOL_NODE
, line
);
398 else if (strncmp(line
, "ip nht", strlen("ip nht")) == 0)
399 config
= config_get(PROTOCOL_NODE
, line
);
400 else if (strncmp(line
, "ipv6 nht", strlen("ipv6 nht")) == 0)
401 config
= config_get(PROTOCOL_NODE
, line
);
402 else if (strncmp(line
, "mpls", strlen("mpls")) == 0)
403 config
= config_get(MPLS_NODE
, line
);
404 else if (strncmp(line
, "bfd", strlen("bfd")) == 0)
405 config
= config_get(BFD_NODE
, line
);
407 if (strncmp(line
, "log", strlen("log")) == 0
408 || strncmp(line
, "hostname", strlen("hostname"))
410 || strncmp(line
, "frr", strlen("frr")) == 0
411 || strncmp(line
, "agentx", strlen("agentx")) == 0
412 || strncmp(line
, "no log", strlen("no log")) == 0)
413 config_add_line_uniq(config_top
, line
);
415 config_add_line(config_top
, line
);
422 /* Macro to check delimiter is needed between each configuration line
424 #define NO_DELIMITER(I) \
425 ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \
426 || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE \
427 || (I) == ACCESS_IPV6_NODE || (I) == ACCESS_MAC_NODE \
428 || (I) == PREFIX_IPV6_NODE || (I) == FORWARDING_NODE \
429 || (I) == DEBUG_NODE || (I) == AAA_NODE || (I) == VRF_DEBUG_NODE \
430 || (I) == NORTHBOUND_DEBUG_NODE || (I) == RMAP_DEBUG_NODE \
431 || (I) == RESOLVER_DEBUG_NODE || (I) == MPLS_NODE)
433 /* Display configuration to file pointer. */
434 void vtysh_config_dump(void)
436 struct listnode
*node
, *nnode
;
437 struct listnode
*mnode
, *mnnode
;
438 struct config
*config
;
439 struct config_master_head
*master
;
443 for (ALL_LIST_ELEMENTS(config_top
, node
, nnode
, line
))
444 vty_out(vty
, "%s\n", line
);
448 for (i
= 0; i
< vector_active(configvec
); i
++)
449 if ((master
= vector_slot(configvec
, i
)) != NULL
) {
450 while ((config
= config_master_pop(master
))) {
451 /* Don't print empty sections for interface.
453 * other hand could have a legitimate empty
454 * section at the end.
455 * VRF is handled in the backend, we could have
456 * "configured" VRFs with static routes which
457 * are not under the VRF node.
459 if (config
->index
== INTERFACE_NODE
460 && list_isempty(config
->line
))
463 vty_out(vty
, "%s\n", config
->name
);
465 for (ALL_LIST_ELEMENTS(config
->line
, mnode
,
467 vty_out(vty
, "%s\n", line
);
468 if (!NO_DELIMITER(i
))
477 for (i
= 0; i
< vector_active(configvec
); i
++)
478 if ((master
= vector_slot(configvec
, i
)) != NULL
) {
479 config_master_fini(master
);
480 XFREE(MTYPE_VTYSH_CONFIG
, master
);
481 vector_slot(configvec
, i
) = NULL
;
483 list_delete_all_node(config_top
);
486 /* Read up configuration file from file_name. */
487 static int vtysh_read_file(FILE *confp
)
493 vty
->wfd
= STDERR_FILENO
;
494 vty
->type
= VTY_TERM
;
495 vty
->node
= CONFIG_NODE
;
497 vtysh_execute_no_pager("enable");
498 vtysh_execute_no_pager("configure terminal");
500 /* Execute configuration file. */
501 ret
= vtysh_config_from_file(vty
, confp
);
503 vtysh_execute_no_pager("end");
504 vtysh_execute_no_pager("disable");
511 /* Read up configuration file from config_default_dir. */
512 int vtysh_read_config(const char *config_default_dir
)
517 confp
= fopen(config_default_dir
, "r");
520 "%% Can't open configuration file %s due to '%s'.\n",
521 config_default_dir
, safe_strerror(errno
));
522 return (CMD_ERR_NO_FILE
);
525 ret
= vtysh_read_file(confp
);
531 /* We don't write vtysh specific into file from vtysh. vtysh.conf should
532 * be edited by hand. So, we handle only "write terminal" case here and
533 * integrate vtysh specific conf with conf from daemons.
535 void vtysh_config_write(void)
539 if (cmd_hostname_get()) {
540 snprintf(line
, sizeof(line
), "hostname %s", cmd_hostname_get());
541 vtysh_config_parse_line(NULL
, line
);
544 if (cmd_domainname_get()) {
545 sprintf(line
, "domainname %s", cmd_domainname_get());
546 vtysh_config_parse_line(NULL
, line
);
548 if (vtysh_write_integrated
== WRITE_INTEGRATED_NO
)
549 vtysh_config_parse_line(NULL
,
550 "no service integrated-vtysh-config");
551 if (vtysh_write_integrated
== WRITE_INTEGRATED_YES
)
552 vtysh_config_parse_line(NULL
,
553 "service integrated-vtysh-config");
558 void vtysh_config_init(void)
560 config_top
= list_new();
561 config_top
->del
= (void (*)(void *))line_del
;
562 configvec
= vector_init(1);