Skip to contentSkip to conversation

put --dns options into env as foreign_options as well

Openvpn-devel In review Heiko Hund 3 participants 11 messages May 27, 2022 – Jun 28, 2022

lore
Share & tools
lore

Heiko Hund

author
#0 lore raw

As discussed in this week's community meeting, here is the patchset to implement foreign_option emulation for --dns options. For more info please consult the individual commit messages.

author
#1 lore raw

PATCH 1/4 remove foreign_option() call for IPv6 DNS servers

The call survived since the initial commit 94bfc256d, where it was added as a fallback, since no IPv6 DNS server handling was implemented at the time. Now there's dhcp_option_dns6_parse() which adds the servers to the tuntap options, just like how it is done with the v4 servers.

---
 src/openvpn/options.c | 1 -
 1 file changed, 1 deletion(-)
src/openvpn/options.c +0 -1
index 20cc849d..65f4d889 100644
@@ -7825,7 +7825,6 @@ add_option(struct options *options,
             if (strstr(p[2], ":"))
             {
                 ipv6dns = true;
-                foreign_option(options, p, 3, es);
                 dhcp_option_dns6_parse(p[2], o->dns6, &o->dns6_len, msglevel);
             }
             else
Signature · 1 line
2.32.0
author
#3 lore raw

PATCH 2/4 remove dead foreign-option parsing code

In reply to Heiko Hund
---
 src/openvpn/options.c | 7 -------
 1 file changed, 7 deletions(-)
src/openvpn/options.c +0 -7
index 65f4d889..bc3fd6a2 100644
@@ -5531,13 +5531,6 @@ add_option(struct options *options,
         print_default_gateway(M_INFO, &rgi, &rgi6);
         openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
     }
-#endif
-#if 0
-    else if (streq(p[0], "foreign-option") && p[1])
-    {
-        VERIFY_PERMISSION(OPT_P_IPWIN32);
-        foreign_option(options, p, 3, es);
-    }
 #endif
     else if (streq(p[0], "echo") || streq(p[0], "parameter"))
     {
Signature · 1 line
2.32.0
author
#5 lore raw

PATCH 3/4 rename foreign_option() and move it up

In reply to Heiko Hund

Add setenv_ prefix to foreign_option funtion so it is more obvious what it does. Move it further up within options.c, so it is defined before all future callers. Also declare all argv strings const.

---
 src/openvpn/options.c | 78 +++++++++++++++++++++----------------------
 1 file changed, 39 insertions(+), 39 deletions(-)
src/openvpn/options.c +39 -39
index bc3fd6a2..9a0634a5 100644
@@ -1013,6 +1013,44 @@ setenv_settings(struct env_set *es, const struct options *o)
     }
 }
 
+static void
+setenv_foreign_option(struct options *o, const char *argv[], int len, struct env_set *es)
+{
+    if (len > 0)
+    {
+        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);
+        int i;
+        bool first = true;
+        bool good = true;
+
+        good &= buf_printf(&name, "foreign_option_%d", o->foreign_option_index + 1);
+        ++o->foreign_option_index;
+        for (i = 0; i < len; ++i)
+        {
+            if (argv[i])
+            {
+                if (!first)
+                {
+                    good &= buf_printf(&value, " ");
+                }
+                good &= buf_printf(&value, "%s", argv[i]);
+                first = false;
+            }
+        }
+        if (good)
+        {
+            setenv_str(es, BSTR(&name), BSTR(&value));
+        }
+        else
+        {
+            msg(M_WARN, "foreign_option: name/value overflow");
+        }
+        gc_free(&gc);
+    }
+}
+
 static in_addr_t
 get_ip_addr(const char *ip_string, int msglevel, bool *error)
 {
@@ -4378,44 +4416,6 @@ options_string_extract_option(const char *options_string, const char *opt_name,
     return ret;
 }
 
-static void
-foreign_option(struct options *o, char *argv[], int len, struct env_set *es)
-{
-    if (len > 0)
-    {
-        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);
-        int i;
-        bool first = true;
-        bool good = true;
-
-        good &= buf_printf(&name, "foreign_option_%d", o->foreign_option_index + 1);
-        ++o->foreign_option_index;
-        for (i = 0; i < len; ++i)
-        {
-            if (argv[i])
-            {
-                if (!first)
-                {
-                    good &= buf_printf(&value, " ");
-                }
-                good &= buf_printf(&value, "%s", argv[i]);
-                first = false;
-            }
-        }
-        if (good)
-        {
-            setenv_str(es, BSTR(&name), BSTR(&value));
-        }
-        else
-        {
-            msg(M_WARN, "foreign_option: name/value overflow");
-        }
-        gc_free(&gc);
-    }
-}
-
 #ifdef _WIN32
 /**
  * Parses --windows-driver config option
@@ -8014,7 +8014,7 @@ add_option(struct options *options,
     else if (streq(p[0], "dhcp-option") && p[1] && !p[3])
     {
         VERIFY_PERMISSION(OPT_P_IPWIN32);
-        foreign_option(options, p, 3, es);
+        setenv_foreign_option(options, (const char **)p, 3, es);
     }
     else if (streq(p[0], "route-method") && p[1] && !p[2]) /* ignore when pushed to non-Windows OS */
     {
Signature · 1 line
2.32.0
author
#7 lore raw

PATCH 4/4 dns: also (re)place foreign dhcp options in env

In reply to Heiko Hund

Override DNS related foreign_options with values set by the --dns option. This is done, so that scripts looking for these options continue to work if only --dns option were pushed, or the values in the --dhcp-options differ fron what's pushed in --dns.

---
 src/openvpn/openvpn.c |  2 +-
 src/openvpn/options.c | 88 ++++++++++++++++++++++++++++++++++++++++---
 src/openvpn/options.h |  2 +-
 3 files changed, 85 insertions(+), 7 deletions(-)
src/openvpn/openvpn.c +1 -1
index a6389fed..15e21452 100644
@@ -248,7 +248,7 @@ openvpn_main(int argc, char *argv[])
             }
 
             /* sanity check on options */
-            options_postprocess(&c.options);
+            options_postprocess(&c.options, c.es);
 
             /* show all option settings */
             show_settings(&c.options);
src/openvpn/options.c +83 -5

123 lines not on this page · raw message

src/openvpn/options.h +1 -1
index c2937dc3..0e50c19e 100644
@@ -813,7 +813,7 @@ char *options_string_extract_option(const char *options_string,
                                     const char *opt_name, struct gc_arena *gc);
 
 
-void options_postprocess(struct options *options);
+void options_postprocess(struct options *options, struct env_set *es);
 
 bool options_postprocess_pull(struct options *o, struct env_set *es);
 
Signature · 1 line
2.32.0
Acked-by on v1 1/4
#2 lore raw

[Openvpn-devel] [PATCH applied] Re: remove foreign_option() call for IPv6 DNS servers

In reply to Heiko Hund

Indeed, this is inside an WIN32/ANDROID block, which does not need/use foreign options for DHCP. Out it goes. Trivial otherwise :-)

Your patch has been applied to the master branch.

commit 2c4cf1a6490b875a4c666d781c30f1033f739118

Author: Heiko Hund
Date:   Fri May 27 03:24:54 2022 +0200
     remove foreign_option() call for IPv6 DNS servers

     Signed-off-by: Heiko Hund <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg24433.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

Acked-by on v1 2/4
#4 lore raw

[Openvpn-devel] [PATCH applied] Re: remove dead foreign-option parsing code

In reply to Heiko Hund

Trivial. Inside #if 0, has been like that since the dawn of times, so really nobody can ever miss that one.

Your patch has been applied to the master branch.

commit ccdd7096ae9a5fe6f07451f525cbdd40885f3acc

Author: Heiko Hund
Date:   Fri May 27 03:24:55 2022 +0200
     remove dead foreign-option parsing code

     Signed-off-by: Heiko Hund <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg24434.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

Acked-by on v1 3/4
#6 lore raw

[Openvpn-devel] [PATCH applied] Re: rename foreign_option() and move it up

In reply to Heiko Hund

Slightly less trivial, but "git show --color-moved=zebra" helps visualize that it's indeed moved 1:1 and just gets a new name.

Your patch has been applied to the master branch.

commit 2c2b22b370b239ae8311e8152ab0bb81cc66fe29

Author: Heiko Hund
Date:   Fri May 27 03:24:56 2022 +0200
     rename foreign_option() and move it up

     Signed-off-by: Heiko Hund <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg24436.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

Acked-by on v1 4/4
#8 lore raw

Re: [Openvpn-devel] [PATCH 4/4] dns: also (re)place foreign dhcp options in env

In reply to Heiko Hund

Code definitely looks like it will be doing what it is intended to do.

Would be a good opportunity for a UT, though.

Quoted 2 lines · Heiko Hund

Override DNS related foreign_options with values set by the --dns option. This is done, so that scripts looking for these options continue

Superflouos comma.

Quoted 2 lines

to work if only --dns option were pushed, or the values in the --dhcp-options differ fron what's pushed in --dns.

"from"

Quoted 174 lines
---
 src/openvpn/openvpn.c |  2 +-
 src/openvpn/options.c | 88 ++++++++++++++++++++++++++++++++++++++++---
 src/openvpn/options.h |  2 +-
 3 files changed, 85 insertions(+), 7 deletions(-)
src/openvpn/openvpn.c +1 -1
index a6389fed..15e21452 100644
@@ -248,7 +248,7 @@ openvpn_main(int argc, char *argv[])
             }
 
             /* sanity check on options */
-            options_postprocess(&c.options);
+            options_postprocess(&c.options, c.es);
 
             /* show all option settings */
             show_settings(&c.options);
src/openvpn/options.c +83 -5
index 9a0634a5..750444fe 100644
@@ -1381,6 +1381,80 @@ tuntap_options_copy_dns(struct options *o)
         }
     }
 }
+#else /* if defined(_WIN32) || defined(TARGET_ANDROID) */
+static void
+foreign_options_copy_dns(struct options *o, struct env_set *es)
+{
+    const struct dns_domain *domain = o->dns_options.search_domains;
+    const struct dns_server *server = o->dns_options.servers;
+    if (!domain && !server)
+    {
+        return;
+    }
+
+    /* reset the index since we're starting all over again */
+    int opt_max = o->foreign_option_index;
+    o->foreign_option_index = 0;
+
+    for (int i = 1; i <= opt_max; ++i)
+    {
+        char name[32];
+        openvpn_snprintf(name, sizeof(name), "foreign_option_%d", i);
+
+        const char *env_str = env_set_get(es, name);
+        const char *value = strchr(env_str, '=') + 1;
+        if ((domain && strstr(value, "dhcp-option DOMAIN-SEARCH") == value)
+            || (server && strstr(value, "dhcp-option DNS") == value))
+        {
+            setenv_del(es, name);
+        }
+        else
+        {
+            setenv_foreign_option(o, &value, 1, es);
+        }
+    }
+
+    struct gc_arena gc = gc_new();
+
+    while (server)
+    {
+        if (server->addr4_defined)
+        {
+            const char *argv[] = {
+                "dhcp-option",
+                "DNS",
+                print_in_addr_t(server->addr4.s_addr, 0, &gc)
+            };
+            setenv_foreign_option(o, argv, 3, es);
+        }
+        if (server->addr6_defined)
+        {
+            const char *argv[] = {
+                "dhcp-option",
+                "DNS6",
+                print_in6_addr(server->addr6, 0, &gc)
+            };
+            setenv_foreign_option(o, argv, 3, es);
+        }
+        server = server->next;
+    }
+    while (domain)
+    {
+        const char *argv[] = { "dhcp-option", "DOMAIN-SEARCH", domain->name };
+        setenv_foreign_option(o, argv, 3, es);
+        domain = domain->next;
+    }
+
+    gc_free(&gc);
+
+    /* remove old leftover entries */
+    while (o->foreign_option_index < opt_max)
+    {
+        char name[32];
+        openvpn_snprintf(name, sizeof(name), "foreign_option_%d", opt_max--);
+        setenv_del(es, name);
+    }
+}
 #endif /* if defined(_WIN32) || defined(TARGET_ANDROID) */
 
 #ifndef ENABLE_SMALL
@@ -3368,7 +3442,7 @@ options_set_backwards_compatible_options(struct options *o)
 }
 
 static void
-options_postprocess_mutate(struct options *o)
+options_postprocess_mutate(struct options *o, struct env_set *es)
 {
     int i;
     /*
@@ -3462,12 +3536,14 @@ options_postprocess_mutate(struct options *o)
     {
         dns_options_preprocess_pull(&o->dns_options);
     }
-#if defined(_WIN32) || defined(TARGET_ANDROID)
     else
     {
+#if defined(_WIN32) || defined(TARGET_ANDROID)
         tuntap_options_copy_dns(o);
-    }
+#else
+        foreign_options_copy_dns(o, es);
 #endif
+    }
     pre_connect_save(o);
 }
 
@@ -3803,9 +3879,9 @@ options_postprocess_filechecks(struct options *options)
  * options.
  */
 void
-options_postprocess(struct options *options)
+options_postprocess(struct options *options, struct env_set *es)
 {
-    options_postprocess_mutate(options);
+    options_postprocess_mutate(options, es);
     options_postprocess_verify(options);
 #ifndef ENABLE_SMALL
     options_postprocess_filechecks(options);
@@ -3826,6 +3902,8 @@ options_postprocess_pull(struct options *o, struct env_set *es)
         setenv_dns_options(&o->dns_options, es);
 #if defined(_WIN32) || defined(TARGET_ANDROID)
         tuntap_options_copy_dns(o);
+#else
+        foreign_options_copy_dns(o, es);
 #endif
     }
     return success;
src/openvpn/options.h +1 -1
index c2937dc3..0e50c19e 100644
@@ -813,7 +813,7 @@ char *options_string_extract_option(const char *options_string,
                                     const char *opt_name, struct gc_arena *gc);
 
 
-void options_postprocess(struct options *options);
+void options_postprocess(struct options *options, struct env_set *es);
 
 bool options_postprocess_pull(struct options *o, struct env_set *es);
 
Signature · 8 lines
2.32.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Signature · 1 line
  Frank Lichtenheld
#9 lore raw

[Openvpn-devel] [PATCH applied] Re: dns: also (re)place foreign dhcp options in env

In reply to Heiko Hund

I have stared at the code (... weeks ago already) but never came around to actually test this - so, happy to see the ACK from Frank :-)

Your patch has been applied to the master branch.

commit 8d345ff16db8d797a1b8485c65361e2949fd15d3

Author: Heiko Hund
Date:   Fri May 27 03:24:57 2022 +0200
     dns: also (re)place foreign dhcp options in env

     Signed-off-by: Heiko Hund <[email protected]...>
     Acked-by: Frank Lichtenheld <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg24432.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#10 lore raw

Re: [Openvpn-devel] [PATCH applied] Re: dns: also (re)place foreign dhcp options in env

Hi,

Quoted 8 lines · Gert Doering

I have stared at the code (... weeks ago already) but never came around to actually test this - so, happy to see the ACK from Frank :-)

Your patch has been applied to the master branch.

commit 8d345ff16db8d797a1b8485c65361e2949fd15d3

Author: Heiko Hund
Date:   Fri May 27 03:24:57 2022 +0200

Uh. Did some last-last minute fixes of the commit message ("Fron"), so the actual commit ID changed again.

commit dac85fff2e4588fd922927737a2aafcbd9c9766e (HEAD -> master)

Author: Heiko Hund <[email protected]...>
Date:   Fri May 27 03:24:57 2022 +0200

dns: also (re)place foreign dhcp options in env

sorry for the confusion.

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...