summary refs log tree commit diff
path: root/src/dhcp
diff options
context:
space:
mode:
Diffstat (limited to 'src/dhcp')
-rw-r--r--src/dhcp/nm-dhcp-dhclient-utils.c43
-rw-r--r--src/dhcp/tests/test-dhcp-dhclient.c19
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 bf3df399..98f8c13a 100644
--- a/src/dhcp/nm-dhcp-dhclient-utils.c
+++ b/src/dhcp/nm-dhcp-dhclient-utils.c
@@ -304,11 +304,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';
 
@@ -326,19 +328,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 2cc7e5ca..9e51fceb 100644
--- a/src/dhcp/tests/test-dhcp-dhclient.c
+++ b/src/dhcp/tests/test-dhcp-dhclient.c
@@ -996,6 +996,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"
@@ -1022,7 +1027,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"
@@ -1033,6 +1044,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"