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_LIST(config_master
);
38 PREDECL_HASH(config_master_hash
);
41 /* Configuration node name. */
44 /* Configuration string line. */
47 /* Configuration can be nested. */
48 struct config
*parent
;
54 /* Index of this config. */
57 /* Node entry for the typed Red-black tree */
58 struct config_master_item rbt_item
;
59 struct config_master_hash_item hash_item
;
62 struct list
*config_top
;
64 static int line_cmp(char *c1
, char *c2
)
66 return strcmp(c1
, c2
);
69 static void line_del(char *line
)
71 XFREE(MTYPE_VTYSH_CONFIG_LINE
, line
);
74 static struct config
*config_new(void)
76 struct config
*config
;
77 config
= XCALLOC(MTYPE_VTYSH_CONFIG
, sizeof(struct config
));
81 static void config_del(struct config
*config
)
83 vector_free(config
->nested
);
84 list_delete(&config
->line
);
86 XFREE(MTYPE_VTYSH_CONFIG_LINE
, config
->exit
);
87 XFREE(MTYPE_VTYSH_CONFIG_LINE
, config
->name
);
88 XFREE(MTYPE_VTYSH_CONFIG
, config
);
91 static int config_cmp(const struct config
*c1
, const struct config
*c2
)
93 return strcmp(c1
->name
, c2
->name
);
96 static uint32_t config_hash(const struct config
*c
)
98 return string_hash_make(c
->name
);
101 DECLARE_LIST(config_master
, struct config
, rbt_item
);
102 DECLARE_HASH(config_master_hash
, struct config
, hash_item
, config_cmp
,
106 * The config_master_head is a list for order of receipt
107 * The hash is for quick lookup under this NODE
109 struct configuration
{
110 struct config_master_head master
;
111 struct config_master_hash_head hash_master
;
114 static struct config
*config_get_vec(vector vec
, int index
, const char *line
)
116 struct config
*config
, *config_loop
;
117 struct configuration
*configuration
;
118 struct config lookup
;
120 config
= config_loop
= NULL
;
122 configuration
= vector_lookup_ensure(vec
, index
);
124 if (!configuration
) {
125 configuration
= XMALLOC(MTYPE_VTYSH_CONFIG
,
126 sizeof(struct configuration
));
127 config_master_init(&configuration
->master
);
128 config_master_hash_init(&configuration
->hash_master
);
129 vector_set_index(vec
, index
, configuration
);
132 lookup
.name
= (char *)line
;
133 config
= config_master_hash_find(&configuration
->hash_master
, &lookup
);
136 config
= config_new();
137 config
->line
= list_new();
138 config
->line
->del
= (void (*)(void *))line_del
;
139 config
->line
->cmp
= (int (*)(void *, void *))line_cmp
;
140 config
->name
= XSTRDUP(MTYPE_VTYSH_CONFIG_LINE
, line
);
142 config
->index
= index
;
143 config
->nested
= vector_init(1);
144 config_master_add_tail(&configuration
->master
, config
);
145 config_master_hash_add(&configuration
->hash_master
, config
);
150 static struct config
*config_get(int index
, const char *line
)
152 return config_get_vec(configvec
, index
, line
);
155 static struct config
*config_get_nested(struct config
*parent
, int index
,
158 struct config
*config
;
160 config
= config_get_vec(parent
->nested
, index
, line
);
161 config
->parent
= parent
;
166 void config_add_line(struct list
*config
, const char *line
)
168 listnode_add(config
, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE
, line
));
171 static void config_add_line_uniq(struct list
*config
, const char *line
)
173 struct listnode
*node
, *nnode
;
176 for (ALL_LIST_ELEMENTS(config
, node
, nnode
, pnt
)) {
177 if (strcmp(pnt
, line
) == 0)
180 listnode_add_sort(config
, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE
, line
));
184 * Add a line that should only be shown once, and always show at the end of the
187 * If the line already exists, it will be moved to the end of the block. If it
188 * does not exist, it will be added at the end of the block.
190 * Note that this only makes sense when there is just one such line that should
191 * show up at the very end of a config block. Furthermore, if the same block
192 * can show up from multiple daemons, all of them must make sure to print the
193 * line at the end of their config, otherwise the line will show at the end of
194 * the config for the last daemon that printed it.
196 * Here is a motivating example with the 'exit-vrf' command. Suppose we receive
197 * a config from Zebra like so:
204 * Then suppose we later receive this config from PIM:
207 * ip msdp mesh-group MyGroup member 1.2.3.4
210 * Then we will combine them into one config block like so:
215 * ip msdp mesh-group MyGroup member 1.2.3.4
218 * Because PIM also sent us an 'exit-vrf', we noticed that we already had one
219 * under the 'vrf BLUE' config block and so we moved it to the end of the
220 * config block again. If PIM had neglected to send us 'exit-vrf', the result
227 * ip msdp mesh-group MyGroup member 1.2.3.4
229 * Therefore, daemons that share config blocks must take care to consistently
230 * print the same block terminators.
232 * Ideally this would be solved by adding a string to struct config that is
233 * always printed at the end when dumping a config. However, this would only
234 * work when the user is using integrated config. In the non-integrated config
235 * case, daemons are responsible for writing their own config files, and so the
236 * must be able to print these blocks correctly independently of vtysh, which
237 * means they are the ones that need to handle printing the block terminators
238 * and VTYSH needs to be smart enough to combine them properly.
243 * The config to add the line to
246 * The line to add to the end of the config
248 static void config_add_line_uniq_end(struct list
*config
, const char *line
)
250 struct listnode
*node
;
253 for (ALL_LIST_ELEMENTS_RO(config
, node
, pnt
)) {
254 if (strcmp(pnt
, line
) == 0)
259 config_add_line(config
, line
);
261 listnode_move_to_tail(config
, node
);
264 void vtysh_config_parse_line(void *arg
, const char *line
)
267 static struct config
*config
= NULL
;
278 /* Suppress exclamation points ! and commented lines. The !s are
280 * dynamically in vtysh_config_dump() */
285 /* Store line to current configuration. */
287 if (config
->index
== KEYCHAIN_NODE
288 && strncmp(line
, " key", strlen(" key")) == 0) {
289 config
= config_get_nested(
290 config
, KEYCHAIN_KEY_NODE
, line
);
291 } else if (config
->index
== KEYCHAIN_KEY_NODE
) {
292 if (strncmp(line
, " exit", strlen(" exit"))
294 config_add_line_uniq_end(config
->line
,
296 config
= config
->parent
;
298 config_add_line_uniq(config
->line
,
301 } else if (strncmp(line
, " link-params",
302 strlen(" link-params"))
304 config_add_line(config
->line
, line
);
305 config
->index
= LINK_PARAMS_NODE
;
306 } else if (strncmp(line
, " ip multicast boundary",
307 strlen(" ip multicast boundary"))
309 config_add_line_uniq_end(config
->line
, line
);
310 } else if (strncmp(line
, " ip igmp query-interval",
311 strlen(" ip igmp query-interval"))
313 config_add_line_uniq_end(config
->line
, line
);
314 } else if (config
->index
== LINK_PARAMS_NODE
315 && strncmp(line
, " exit-link-params",
318 config_add_line(config
->line
, line
);
319 config
->index
= INTERFACE_NODE
;
320 } else if (!strncmp(line
, " vrrp", strlen(" vrrp"))
321 || !strncmp(line
, " no vrrp",
322 strlen(" no vrrp"))) {
323 config_add_line(config
->line
, line
);
324 } else if (!strncmp(line
, " ip mroute",
325 strlen(" ip mroute"))) {
326 config_add_line_uniq_end(config
->line
, line
);
327 } else if (config
->index
== RMAP_NODE
328 || config
->index
== INTERFACE_NODE
329 || config
->index
== VTY_NODE
330 || config
->index
== NH_GROUP_NODE
)
331 config_add_line_uniq(config
->line
, line
);
333 config_add_line(config
->line
, line
);
335 config_add_line(config_top
, line
);
338 if (strncmp(line
, "exit", strlen("exit")) == 0) {
341 XSTRDUP(MTYPE_VTYSH_CONFIG_LINE
, line
);
342 } else if (strncmp(line
, "interface", strlen("interface")) == 0)
343 config
= config_get(INTERFACE_NODE
, line
);
344 else if (strncmp(line
, "pseudowire", strlen("pseudowire")) == 0)
345 config
= config_get(PW_NODE
, line
);
346 else if (strncmp(line
, "vrf", strlen("vrf")) == 0)
347 config
= config_get(VRF_NODE
, line
);
348 else if (strncmp(line
, "nexthop-group", strlen("nexthop-group"))
350 config
= config_get(NH_GROUP_NODE
, line
);
351 else if (strncmp(line
, "router-id", strlen("router-id")) == 0)
352 config
= config_get(ZEBRA_NODE
, line
);
353 else if (strncmp(line
, "router rip", strlen("router rip")) == 0)
354 config
= config_get(RIP_NODE
, line
);
355 else if (strncmp(line
, "router ripng", strlen("router ripng"))
357 config
= config_get(RIPNG_NODE
, line
);
358 else if (strncmp(line
, "router eigrp", strlen("router eigrp"))
360 config
= config_get(EIGRP_NODE
, line
);
361 else if (strncmp(line
, "router babel", strlen("router babel"))
363 config
= config_get(BABEL_NODE
, line
);
364 else if (strncmp(line
, "router ospf", strlen("router ospf"))
366 config
= config_get(OSPF_NODE
, line
);
367 else if (strncmp(line
, "router ospf6", strlen("router ospf6"))
369 config
= config_get(OSPF6_NODE
, line
);
370 else if (strncmp(line
, "mpls ldp", strlen("mpls ldp")) == 0)
371 config
= config_get(LDP_NODE
, line
);
372 else if (strncmp(line
, "l2vpn", strlen("l2vpn")) == 0)
373 config
= config_get(LDP_L2VPN_NODE
, line
);
374 else if (strncmp(line
, "router bgp", strlen("router bgp")) == 0)
375 config
= config_get(BGP_NODE
, line
);
376 else if (strncmp(line
, "router isis", strlen("router isis"))
378 config
= config_get(ISIS_NODE
, line
);
379 else if (strncmp(line
, "router openfabric", strlen("router openfabric"))
381 config
= config_get(OPENFABRIC_NODE
, line
);
382 else if (strncmp(line
, "route-map", strlen("route-map")) == 0)
383 config
= config_get(RMAP_NODE
, line
);
384 else if (strncmp(line
, "no route-map", strlen("no route-map"))
386 config
= config_get(RMAP_NODE
, line
);
387 else if (strncmp(line
, "pbr-map", strlen("pbr-map")) == 0)
388 config
= config_get(PBRMAP_NODE
, line
);
389 else if (strncmp(line
, "access-list", strlen("access-list"))
391 config
= config_get(ACCESS_NODE
, line
);
392 else if (strncmp(line
, "ipv6 access-list",
393 strlen("ipv6 access-list"))
395 config
= config_get(ACCESS_IPV6_NODE
, line
);
396 else if (strncmp(line
, "mac access-list",
397 strlen("mac access-list"))
399 config
= config_get(ACCESS_MAC_NODE
, line
);
400 else if (strncmp(line
, "ip prefix-list",
401 strlen("ip prefix-list"))
403 config
= config_get(PREFIX_NODE
, line
);
404 else if (strncmp(line
, "ipv6 prefix-list",
405 strlen("ipv6 prefix-list"))
407 config
= config_get(PREFIX_IPV6_NODE
, line
);
408 else if (strncmp(line
, "bgp as-path access-list",
409 strlen("bgp as-path access-list"))
411 config
= config_get(AS_LIST_NODE
, line
);
412 else if (strncmp(line
, "bgp community-list",
413 strlen("bgp community-list"))
415 || strncmp(line
, "bgp extcommunity-list",
416 strlen("bgp extcommunity-list"))
418 || strncmp(line
, "bgp large-community-list",
419 strlen("bgp large-community-list"))
421 config
= config_get(COMMUNITY_LIST_NODE
, line
);
422 else if (strncmp(line
, "bgp community alias",
423 strlen("bgp community alias")) == 0)
424 config
= config_get(COMMUNITY_ALIAS_NODE
, line
);
425 else if (strncmp(line
, "ip route", strlen("ip route")) == 0)
426 config
= config_get(IP_NODE
, line
);
427 else if (strncmp(line
, "ipv6 route", strlen("ipv6 route")) == 0)
428 config
= config_get(IP_NODE
, line
);
429 else if (strncmp(line
, "key", strlen("key")) == 0)
430 config
= config_get(KEYCHAIN_NODE
, line
);
431 else if (strncmp(line
, "line", strlen("line")) == 0)
432 config
= config_get(VTY_NODE
, line
);
433 else if ((strncmp(line
, "ipv6 forwarding",
434 strlen("ipv6 forwarding"))
436 || (strncmp(line
, "ip forwarding",
437 strlen("ip forwarding"))
439 config
= config_get(FORWARDING_NODE
, line
);
440 else if (strncmp(line
, "debug vrf", strlen("debug vrf")) == 0)
441 config
= config_get(VRF_DEBUG_NODE
, line
);
442 else if (strncmp(line
, "debug northbound",
443 strlen("debug northbound"))
445 config
= config_get(NORTHBOUND_DEBUG_NODE
, line
);
446 else if (strncmp(line
, "debug route-map",
447 strlen("debug route-map"))
449 config
= config_get(RMAP_DEBUG_NODE
, line
);
450 else if (strncmp(line
, "debug resolver",
451 strlen("debug resolver")) == 0)
452 config
= config_get(RESOLVER_DEBUG_NODE
, line
);
453 else if (strncmp(line
, "debug", strlen("debug")) == 0)
454 config
= config_get(DEBUG_NODE
, line
);
455 else if (strncmp(line
, "password", strlen("password")) == 0
456 || strncmp(line
, "enable password",
457 strlen("enable password"))
459 config
= config_get(AAA_NODE
, line
);
460 else if (strncmp(line
, "ip protocol", strlen("ip protocol"))
462 config
= config_get(PROTOCOL_NODE
, line
);
463 else if (strncmp(line
, "ipv6 protocol", strlen("ipv6 protocol"))
465 config
= config_get(PROTOCOL_NODE
, line
);
466 else if (strncmp(line
, "ip nht", strlen("ip nht")) == 0)
467 config
= config_get(PROTOCOL_NODE
, line
);
468 else if (strncmp(line
, "ipv6 nht", strlen("ipv6 nht")) == 0)
469 config
= config_get(PROTOCOL_NODE
, line
);
470 else if (strncmp(line
, "mpls", strlen("mpls")) == 0)
471 config
= config_get(MPLS_NODE
, line
);
472 else if (strncmp(line
, "segment-routing",
473 strlen("segment-routing"))
475 config
= config_get(SEGMENT_ROUTING_NODE
, line
);
476 else if (strncmp(line
, "bfd", strlen("bfd")) == 0)
477 config
= config_get(BFD_NODE
, line
);
478 else if (strncmp(line
, "rpki", strlen("rpki")) == 0)
479 config
= config_get(RPKI_NODE
, line
);
481 if (strncmp(line
, "log", strlen("log")) == 0
482 || strncmp(line
, "hostname", strlen("hostname")) == 0
483 || strncmp(line
, "domainname", strlen("domainname")) == 0
484 || strncmp(line
, "frr", strlen("frr")) == 0
485 || strncmp(line
, "agentx", strlen("agentx")) == 0
486 || strncmp(line
, "no log", strlen("no log")) == 0
487 || strncmp(line
, "no ip prefix-list", strlen("no ip prefix-list")) == 0
488 || strncmp(line
, "no ipv6 prefix-list", strlen("no ipv6 prefix-list")) == 0)
489 config_add_line_uniq(config_top
, line
);
491 config_add_line(config_top
, line
);
498 /* Macro to check delimiter is needed between each configuration line
500 #define NO_DELIMITER(I) \
501 ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \
502 || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE \
503 || (I) == COMMUNITY_ALIAS_NODE || (I) == ACCESS_IPV6_NODE \
504 || (I) == ACCESS_MAC_NODE || (I) == PREFIX_IPV6_NODE \
505 || (I) == FORWARDING_NODE || (I) == DEBUG_NODE || (I) == AAA_NODE \
506 || (I) == VRF_DEBUG_NODE || (I) == NORTHBOUND_DEBUG_NODE \
507 || (I) == RMAP_DEBUG_NODE || (I) == RESOLVER_DEBUG_NODE \
508 || (I) == MPLS_NODE || (I) == KEYCHAIN_KEY_NODE)
510 static void configvec_dump(vector vec
, bool nested
)
512 struct listnode
*mnode
, *mnnode
;
513 struct config
*config
;
514 struct configuration
*configuration
;
518 for (i
= 0; i
< vector_active(vec
); i
++)
519 if ((configuration
= vector_slot(vec
, i
)) != NULL
) {
520 while ((config
= config_master_pop(
521 &configuration
->master
))) {
522 config_master_hash_del(
523 &configuration
->hash_master
, config
);
524 /* Don't print empty sections for interface.
526 * other hand could have a legitimate empty
527 * section at the end.
528 * VRF is handled in the backend, we could have
529 * "configured" VRFs with static routes which
530 * are not under the VRF node.
532 if (config
->index
== INTERFACE_NODE
533 && (listcount(config
->line
) == 1)
534 && (line
= listnode_head(config
->line
))
535 && strmatch(line
, "exit")) {
540 vty_out(vty
, "%s\n", config
->name
);
542 for (ALL_LIST_ELEMENTS(config
->line
, mnode
,
544 vty_out(vty
, "%s\n", line
);
546 configvec_dump(config
->nested
, true);
549 vty_out(vty
, "%s\n", config
->exit
);
551 if (!NO_DELIMITER(i
))
556 config_master_fini(&configuration
->master
);
557 config_master_hash_fini(&configuration
->hash_master
);
558 XFREE(MTYPE_VTYSH_CONFIG
, configuration
);
559 vector_slot(vec
, i
) = NULL
;
560 if (!nested
&& NO_DELIMITER(i
))
565 void vtysh_config_dump(void)
567 struct listnode
*node
, *nnode
;
570 for (ALL_LIST_ELEMENTS(config_top
, node
, nnode
, line
))
571 vty_out(vty
, "%s\n", line
);
573 list_delete_all_node(config_top
);
577 configvec_dump(configvec
, false);
580 /* Read up configuration file from file_name. */
581 static int vtysh_read_file(FILE *confp
, bool dry_run
)
587 vty
->wfd
= STDERR_FILENO
;
588 vty
->type
= VTY_TERM
;
589 vty
->node
= CONFIG_NODE
;
591 vtysh_execute_no_pager("enable");
592 vtysh_execute_no_pager("configure terminal");
595 vtysh_execute_no_pager("XFRR_start_configuration");
597 /* Execute configuration file. */
598 ret
= vtysh_config_from_file(vty
, confp
);
601 vtysh_execute_no_pager("XFRR_end_configuration");
603 vtysh_execute_no_pager("end");
604 vtysh_execute_no_pager("disable");
611 /* Read up configuration file from config_default_dir. */
612 int vtysh_read_config(const char *config_default_dir
, bool dry_run
)
618 confp
= fopen(config_default_dir
, "r");
621 "%% Can't open configuration file %s due to '%s'.\n",
622 config_default_dir
, safe_strerror(errno
));
623 return CMD_ERR_NO_FILE
;
626 save
= vtysh_add_timestamp
;
627 vtysh_add_timestamp
= false;
629 ret
= vtysh_read_file(confp
, dry_run
);
632 vtysh_add_timestamp
= save
;
637 /* We don't write vtysh specific into file from vtysh. vtysh.conf should
638 * be edited by hand. So, we handle only "write terminal" case here and
639 * integrate vtysh specific conf with conf from daemons.
641 void vtysh_config_write(void)
645 if (cmd_hostname_get()) {
646 snprintf(line
, sizeof(line
), "hostname %s", cmd_hostname_get());
647 vtysh_config_parse_line(NULL
, line
);
650 if (cmd_domainname_get()) {
651 snprintf(line
, sizeof(line
), "domainname %s",
652 cmd_domainname_get());
653 vtysh_config_parse_line(NULL
, line
);
655 if (vtysh_write_integrated
== WRITE_INTEGRATED_NO
)
656 vtysh_config_parse_line(NULL
,
657 "no service integrated-vtysh-config");
658 if (vtysh_write_integrated
== WRITE_INTEGRATED_YES
)
659 vtysh_config_parse_line(NULL
,
660 "service integrated-vtysh-config");
665 void vtysh_config_init(void)
667 config_top
= list_new();
668 config_top
->del
= (void (*)(void *))line_del
;
669 configvec
= vector_init(1);