Blame SOURCES/1000-dhclient-include-conditionals-rh1758550.patch

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