You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.8 KiB

From 7a5509053a78bf92511626ca06fdb61c2977693f Mon Sep 17 00:00:00 2001
From: Beniamino Galvani <bgalvani@redhat.com>
Date: Sun, 6 Oct 2019 08:27:18 +0200
Subject: [PATCH] dhcp: include conditionals from existing dhclient
configuration
Since commit 159ff23268b1 ('dhcp/dhclient-utils: skip over
dhclient.conf blocks') we skip blocks enclosed in lines containing '{'
and '}' because NM should ignore 'lease', 'alias' and other
declarations. However, conditional statements seem useful and should
not be skipped.
https://bugzilla.redhat.com/show_bug.cgi?id=1758550
(cherry picked from commit b58e4d311da630ea8d14f60ca3667710877e455d)
(cherry picked from commit 58ffded2d0a317c3a480821272b41780aff0e64e)
(cherry picked from commit 497101fd1e44128496d14a3b8b7496de7febe2f0)
---
src/dhcp/nm-dhcp-dhclient-utils.c | 43 +++++++++++++++++++++--------
src/dhcp/tests/test-dhcp-dhclient.c | 19 ++++++++++++-
2 files changed, 50 insertions(+), 12 deletions(-)
diff --git a/src/dhcp/nm-dhcp-dhclient-utils.c b/src/dhcp/nm-dhcp-dhclient-utils.c
index 3338fbff2..01d0fc020 100644
--- a/src/dhcp/nm-dhcp-dhclient-utils.c
+++ b/src/dhcp/nm-dhcp-dhclient-utils.c
@@ -275,11 +275,13 @@ nm_dhcp_dhclient_create_config (const char *interface,
if (orig_contents) {
gs_free const char **lines = NULL;
gsize line_i;
- int nest = 0;
+ nm_auto_free_gstring GString *blocks_stack = NULL;
+ guint blocks_skip = 0;
gboolean in_alsoreq = FALSE;
gboolean in_req = FALSE;
char intf[IFNAMSIZ];
+ blocks_stack = g_string_new (NULL);
g_string_append_printf (new_contents, _("# Merged from %s\n\n"), orig_path);
intf[0] = '\0';
@@ -297,19 +299,38 @@ nm_dhcp_dhclient_create_config (const char *interface,
if (in_req) {
/* pass */
} else if (strchr (p, '{')) {
- nest++;
- if ( !intf[0]
- && NM_STR_HAS_PREFIX (p, "interface"))
- if (read_interface (p, intf, sizeof (intf)))
- continue;
+ if ( NM_STR_HAS_PREFIX (p, "lease")
+ || NM_STR_HAS_PREFIX (p, "alias")
+ || NM_STR_HAS_PREFIX (p, "interface")
+ || NM_STR_HAS_PREFIX (p, "pseudo")) {
+ /* skip over these blocks, except 'interface' when it
+ * matches the current interface */
+ blocks_skip++;
+ g_string_append_c (blocks_stack, 'b');
+ if ( !intf[0]
+ && NM_STR_HAS_PREFIX (p, "interface")) {
+ if (read_interface (p, intf, sizeof (intf)))
+ continue;
+ }
+ } else {
+ /* allow other blocks (conditionals) */
+ if (!strchr (p, '}')) /* '} else {' */
+ g_string_append_c (blocks_stack, 'c');
+ }
} else if (strchr (p, '}')) {
- if (nest)
- nest--;
- intf[0] = '\0';
- continue;
+ if (blocks_stack->len > 0) {
+ if (blocks_stack->str[blocks_stack->len - 1] == 'b') {
+ g_string_truncate (blocks_stack, blocks_stack->len - 1);
+ nm_assert(blocks_skip > 0);
+ blocks_skip--;
+ intf[0] = '\0';
+ continue;
+ }
+ g_string_truncate (blocks_stack, blocks_stack->len - 1);
+ }
}
- if (nest && !intf[0])
+ if (blocks_skip > 0 && !intf[0])
continue;
if (intf[0] && !nm_streq (intf, interface))
diff --git a/src/dhcp/tests/test-dhcp-dhclient.c b/src/dhcp/tests/test-dhcp-dhclient.c
index f6267cd28..90480955f 100644
--- a/src/dhcp/tests/test-dhcp-dhclient.c
+++ b/src/dhcp/tests/test-dhcp-dhclient.c
@@ -900,6 +900,11 @@ test_structured (void)
" request subnet-mask, broadcast-address, time-offset, routers,\n"
" domain-search, domain-name, domain-name-servers, host-name;\n"
" require subnet-mask, domain-name-servers;\n"
+ " if not option domain-name = \"example.org\" {\n"
+ " prepend domain-name-servers 127.0.0.1;\n"
+ " } else {\n"
+ " prepend domain-name-servers 127.0.0.2;\n"
+ " } \n"
" } \n"
"\n"
"pseudo \"secondary\" \"eth0\" { \n"
@@ -926,7 +931,13 @@ test_structured (void)
" interface \"eth0\";\n"
" fixed-address 192.0.2.2;\n"
" option subnet-mask 255.255.255.0;\n"
- " } \n";
+ " } \n"
+ "if not option domain-name = \"example.org\" {\n"
+ " prepend domain-name-servers 127.0.0.1;\n"
+ " if not option domain-name = \"useless.example.com\" {\n"
+ " prepend domain-name-servers 127.0.0.2;\n"
+ " }\n"
+ "}\n";
static const char *const expected = \
"# Created by NetworkManager\n"
@@ -937,6 +948,12 @@ test_structured (void)
"send dhcp-client-identifier \"sad-and-useless\";\n"
"send dhcp-lease-time 8086;\n"
"require subnet-mask;\n"
+ "if not option domain-name = \"example.org\" {\n"
+ "prepend domain-name-servers 127.0.0.1;\n"
+ "if not option domain-name = \"useless.example.com\" {\n"
+ "prepend domain-name-servers 127.0.0.2;\n"
+ "}\n"
+ "}\n"
"\n"
"option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n"
"option ms-classless-static-routes code 249 = array of unsigned integer 8;\n"
--
2.21.0