[Openvpn-devel,S] Change in openvpn[master]: dns: clean up --dhcp-options when --dns is active

Message ID a9ec6c9aea920039877bb6f4c6589dff110ed677-HTML@gerrit.openvpn.net
State New
Headers show
Series [Openvpn-devel,S] Change in openvpn[master]: dns: clean up --dhcp-options when --dns is active | expand

Commit Message

d12fk (Code Review) March 6, 2025, 10:15 p.m. UTC
Attention is currently required from: flichtenheld, plaisthos.

Hello plaisthos, flichtenheld,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/904?usp=email

to review the following change.


Change subject: dns: clean up --dhcp-options when --dns is active
......................................................................

dns: clean up --dhcp-options when --dns is active

Since --dns setting overrule DNS related --dhcp-options,
remove the latter when we got some via --dns.

Change-Id: I635c4018fb43b5976a39b6a90cb2e9cb2570cd6a
Signed-off-by: Heiko Hund <heiko@ist.eigentlich.net>
---
M src/openvpn/options.c
1 file changed, 47 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/904/1

Patch

diff --git a/src/openvpn/options.c b/src/openvpn/options.c
index d7f6b16..c42d933 100644
--- a/src/openvpn/options.c
+++ b/src/openvpn/options.c
@@ -4160,6 +4160,53 @@ 
     if (success)
     {
         dns_options_postprocess_pull(&o->dns_options);
+
+#if defined(_WIN32) || defined(TARGET_ANDROID)
+        /* If there's --dns servers, remove dns related --dhcp-options */
+        if (o->dns_options.servers)
+        {
+            o->tuntap_options.dns_len = 0;
+            o->tuntap_options.dns6_len = 0;
+            o->tuntap_options.domain = NULL;
+            o->tuntap_options.domain_search_list_len = 0;
+        }
+#else  /* if defined(_WIN32) || defined(TARGET_ANDROID) */
+        /* Clean up env from overridden DNS config */
+        struct gc_arena gc = gc_new();
+        struct buffer name = alloc_buf_gc(OPTION_PARM_SIZE, &gc);
+        struct buffer value = alloc_buf_gc(OPTION_PARM_SIZE, &gc);
+
+        const int fo_count = o->foreign_option_index;
+        o->foreign_option_index = 0;
+
+        for (int i = 1; i <= fo_count; ++i)
+        {
+            buf_clear(&name);
+            buf_printf(&name, "foreign_option_%d", i);
+            const char *env_str = env_set_get(es, BSTR(&name));
+            const char *item_val = strchr(env_str, '=') + 1;
+            buf_clear(&value);
+            buf_printf(&value, "%s", item_val);
+
+            /* Remove foreign option item from env set */
+            env_set_del(es, BSTR(&name));
+
+            item_val = BSTR(&value);
+            if (strncmp(item_val, "dhcp-option ", 12) != 0
+                || (strncmp(item_val + 12, "ADAPTER-DOMAIN-SUFFIX ", 22) != 0
+                    && strncmp(item_val + 12, "DOMAIN-SEARCH ", 14) != 0
+                    && strncmp(item_val + 12, "DOMAIN ", 7) != 0
+                    && strncmp(item_val + 12, "DNS6 ", 5) != 0
+                    && strncmp(item_val + 12, "DNS ", 4) != 0))
+            {
+                /* Re-set the item with potentially updated name */
+                buf_clear(&name);
+                buf_printf(&name, "foreign_option_%d", ++o->foreign_option_index);
+                setenv_str(es, BSTR(&name), BSTR(&value));
+            }
+        }
+        gc_free(&gc);
+#endif /* defined(_WIN32) || defined(TARGET_ANDROID) */
     }
     return success;
 }