From: "d12fk (Code Review)" Date: Fri, 14 Mar 2025 04:21:29 +0000 In-Reply-To: References: Reply-To: heiko@...515..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="g4U+mMcJcUY="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: dns: clean up --dhcp-options when --dns is active List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --g4U+mMcJcUY= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: d12fk, flichtenheld, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/904?usp=3Demail to look at the new patch set (#4). The following approvals got outdated and were removed: Code-Review+2 by flichtenheld The change is no longer submittable: Code-Review and checks~ChecksSubmitRul= e are unsatisfied now. Change subject: dns: clean up --dhcp-options when --dns is active ...................................................................... dns: clean up --dhcp-options when --dns is active Since --dns settings overrule DNS related --dhcp-options, remove the latter when we got some via --dns. To stay as backward compatible as possible, the --dns server addresses and search domains are added as foreign_options env vars, so that an existing --up script can use them to set up DNS as before, without the immediate need to change after an openvpn upgrade. In case an --up script is defined, the --dns-script is not run to prevent potential double DNS configuration. Change-Id: I635c4018fb43b5976a39b6a90cb2e9cb2570cd6a Signed-off-by: Heiko Hund --- M src/openvpn/dns.c M src/openvpn/options.c 2 files changed, 106 insertions(+), 1 deletion(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/904/4 diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c index 6ddf5c2..147509a 100644 --- a/src/openvpn/dns.c +++ b/src/openvpn/dns.c @@ -703,7 +703,7 @@ static void run_up_down_script(bool up, struct options *o, const struct tuntap *tt, st= ruct dns_script_runner_info *script_runner) { - if (!o->dns_options.script) + if (!o->dns_options.script || o->up_script) { return; } diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 4c331dc..b26c864 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -4163,6 +4163,111 @@ if (success) { dns_options_postprocess_pull(&o->dns_options); + if (!o->dns_options.servers) + { + return true; + } + +#if defined(_WIN32) || defined(TARGET_ANDROID) + /* If there's --dns servers, remove dns related --dhcp-options */ + o->tuntap_options.dns_len =3D 0; + o->tuntap_options.dns6_len =3D 0; + o->tuntap_options.domain =3D NULL; + o->tuntap_options.domain_search_list_len =3D 0; +#else /* if defined(_WIN32) || defined(TARGET_ANDROID) */ + + /* Clean up env from --dhcp-option DNS config */ + struct gc_arena gc =3D gc_new(); + struct buffer name =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc); + struct buffer value =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc); + + const int fo_count =3D o->foreign_option_index; + o->foreign_option_index =3D 0; + + for (int i =3D 1; i <=3D fo_count; ++i) + { + buf_clear(&name); + buf_printf(&name, "foreign_option_%d", i); + const char *env_str =3D env_set_get(es, BSTR(&name)); + const char *item_val =3D strchr(env_str, '=3D') + 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 =3D BSTR(&value); + if (strncmp(item_val, "dhcp-option ", 12) !=3D 0 + || (strncmp(item_val + 12, "ADAPTER-DOMAIN-SUFFIX ", 22) != =3D 0 + && strncmp(item_val + 12, "DOMAIN-SEARCH ", 14) !=3D 0 + && strncmp(item_val + 12, "DOMAIN ", 7) !=3D 0 + && strncmp(item_val + 12, "DNS6 ", 5) !=3D 0 + && strncmp(item_val + 12, "DNS ", 4) !=3D 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)); + } + } + + /* Set foreign option env vars from --dns config */ + if (!o->up_script) + { + /* No need to, when there is no --up script */ + return true; + } + const char *p[] =3D { "dhcp-option", NULL, NULL }; + size_t p_len =3D sizeof(p) / sizeof(p[0]); + + p[1] =3D "DOMAIN"; + const struct dns_domain *d =3D o->dns_options.search_domains; + while (d) + { + p[2] =3D d->name; + setenv_foreign_option(o, (const char **)p, p_len, es); + d =3D d->next; + } + + const struct dns_server *s =3D o->dns_options.servers; + while (s) + { + bool non_standard_server_port =3D false; + for (int i =3D 0; i < s->addr_count; ++i) + { + if (s->addr[i].port && s->addr[i].port !=3D 53) + { + non_standard_server_port =3D true; + break; + } + } + if ((s->transport && s->transport !=3D DNS_TRANSPORT_PLAIN) + || (s->dnssec && s->dnssec !=3D DNS_SECURITY_NO) + || non_standard_server_port) + { + /* Ignore servers requiring too much config to be set */ + s =3D s->next; + continue; + } + + for (int i =3D 0; i < s->addr_count; ++i) + { + if (s->addr[i].family =3D=3D AF_INET) + { + p[1] =3D "DNS"; + p[2] =3D print_in_addr_t(s->addr[i].in.a4.s_addr, IA_N= ET_ORDER, &gc); + } + else + { + p[1] =3D "DNS6"; + p[2] =3D print_in6_addr(s->addr[i].in.a6, 0, &gc); + } + setenv_foreign_option(o, (const char **)p, p_len, es); + } + s =3D s->next; + } + gc_free(&gc); +#endif /* defined(_WIN32) || defined(TARGET_ANDROID) */ } return success; } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/904?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I635c4018fb43b5976a39b6a90cb2e9cb2570cd6a Gerrit-Change-Number: 904 Gerrit-PatchSet: 4 Gerrit-Owner: d12fk Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-Attention: d12fk Gerrit-MessageType: newpatchset --g4U+mMcJcUY= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: d12fk, flichtenheld, plaisthos. <= /p>

d12fk uploaded patch set #4 to this change.

View Change

The following approvals got outdated and were removed: Code-Review+2 by flichtenheld

The change is no longer submittable: Co= de-Review and checks~ChecksSubmitRule are unsatisfied now.

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

Since --dns settings overrule DNS = related --dhcp-options,
remove the latter when we got some via --dns.
To stay as backward compatible as possible, the --dns server addresses=
and search domains are added as foreign_options env vars, so that anexisting --up script can use them to set up DNS as before, without the
= immediate need to change after an openvpn upgrade.

In case an --up s= cript is defined, the --dns-script is not run to
prevent potential doubl= e DNS configuration.

Change-Id: I635c4018fb43b5976a39b6a90cb2e9cb257= 0cd6a
Signed-off-by: Heiko Hund <heiko@...2662...>
---
M src= /openvpn/dns.c
M src/openvpn/options.c
2 files changed, 106 insertion= s(+), 1 deletion(-)

git pull ssh://gerrit.openvpn.net:29418/openv=
pn refs/changes/04/904/4
diff --git a/src/openvpn/dns.c b/src/openv=
pn/dns.c
index 6ddf5c2..147509a 100644
--- = a/src/openvpn/dns.c
+++ b/src/openvpn/dns.c
@@ -703,7 +703,7 @@
static void
run_up_d= own_script(bool up, struct options *o, const struct tuntap *tt, struct dns_= script_runner_info *script_runner)
{
- if (!o->dns_options.script)+ if (!o->dns_options.s= cript || o->up_script)
{
r= eturn;
}
diff --git a/src/openvpn/opti= ons.c b/src/openvpn/options.c
index 4c331dc..b26c864 100644=
--- a/src/openvpn/options.c
+++ b/src/open= vpn/options.c
@@ -4163,6 +4163,111 @@
= if (success)
{
dns_options_po= stprocess_pull(&o->dns_options);
+ if (!o->dns_options.servers)
+ {
+ return true;
+ }
+
+#if defined(_WIN32) || defined(TARGET_ANDROID)
+ /* If there's --dns servers, = remove dns related --dhcp-options */
+ o->tuntap_options.dns_len =3D 0;
+ o->tuntap_options.dns6_= len =3D 0;
+ o= ->tuntap_options.domain =3D NULL;
+ o->tuntap_options.domain_search_list_len =3D 0;=
+#else /* if define= d(_WIN32) || defined(TARGET_ANDROID) */
+
= + /* Clean up env from --dhcp-option DNS config */
+ struct gc_arena gc =3D gc_new= ();
+ struct b= uffer name =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc);
+ struct buffer value =3D alloc_b= uf_gc(OPTION_PARM_SIZE, &gc);
+
+ = const int fo_count =3D o->foreign_option_index;
+ o->foreign_option_index =3D 0;=
+
+ for (int i =3D 1; i <=3D fo= _count; ++i)
+ = {
+ buf_c= lear(&name);
+ = buf_printf(&name, "foreign_option_%d", i);+ const char *env_s= tr =3D env_set_get(es, BSTR(&name));
+ const char *item_val =3D strchr(env_str, &= #39;=3D') + 1;
+ = buf_clear(&value);
+ buf_printf(&value, "%s", item_val);=
+
+ /* Remove foreign option i= tem from env set */
+= env_set_del(es, BSTR(&name));
+
+ item_val =3D BSTR(&value);
+ if (strncmp(item_val, "dhcp-= option ", 12) !=3D 0
+ || (strncmp(item_val + 12, "ADAPTER-DOMAIN-SUFFI= X ", 22) !=3D 0
= + && strncmp(item_val + 12, "DOMAIN-SEARCH = ", 14) !=3D 0
+ = && strncmp(item_val + 12, "DOMAIN ", 7= ) !=3D 0
+ = && strncmp(item_val + 12, "DNS6 ", 5) !=3D 0
+ &am= p;& strncmp(item_val + 12, "DNS ", 4) !=3D 0))
+ {

+ /* Re-set the item with = potentially updated name */
+ buf_clear(&name);
+ buf_printf(&name, "forei= gn_option_%d", ++o->foreign_option_index);
+ setenv_str(es, BSTR(&nam= e), BSTR(&value));
+ = }
+
+ /* Set foreign option en= v vars from --dns config */
+ if (!o->up_script)
+ {
+ /* No need to, when there is no --up script */+ return true;
+ }
+ const char *p[] =3D { &qu= ot;dhcp-option", NULL, NULL };

+ size_t p_len =3D sizeof(p) / sizeof(p[0]);+
+ p[1] =3D "DOMAIN";
= + const struct dns_domai= n *d =3D o->dns_options.search_domains;
+ while (d)
+ {
+ p[2] =3D d->name;
+ setenv_foreign_option(o, (const char **= )p, p_len, es);
+ = d =3D d->next;
+ }
+
+ const struct dn= s_server *s =3D o->dns_options.servers;
+ while (s)
+ {
+ bool non_standard_server_port =3D false;
<= span style=3D"color: hsl(120, 100%, 40%);">+ for (int i =3D 0; i= < s->addr_count; ++i)

+ {
+ = {
+ = non_standard_server_port =3D true;
+ break;
+ }
+ }
+ if ((s->transport && s->trans= port !=3D DNS_TRANSPORT_PLAIN)
+ || (s->dnssec && s->dnssec !=3D DN= S_SECURITY_NO)
+ = || non_standard_server_port)
+ {
+ /* Ignore servers requiring too much config t= o be set */
+ = s =3D s->next;
+ continue;
+ }
+
+ f= or (int i =3D 0; i < s->addr_count; ++i)
+ {
+ if (s->addr[i].family =3D=3D AF_INE= T)
+ {=
+ = p[1] =3D "DNS";
+ p[2] =3D print_in_addr_t(s->addr[i].in.a4.s_a= ddr, IA_NET_ORDER, &gc);
+ }
+ else
+ {
+ p[1] =3D "DNS6";
+ p[2] =3D print_in6_ad= dr(s->addr[i].in.a6, 0, &gc);
+ }
+ setenv_foreign_option(o, (const char **)p, p= _len, es);
+ = }
+ s = =3D s->next;
+ = }
+ gc_fre= e(&gc);
+#endif /= * defined(_WIN32) || defined(TARGET_ANDROID) */
}

return success;
}
=

To view, visit change 904. To unsubscribe, or for help writing mail= filters, visit settings= .

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I635c4018fb43b5976a39b6a90cb= 2e9cb2570cd6a
Gerrit-Change-Number: 904
Gerrit-PatchSet: 4
Gerrit-Owner: d12fk <heiko@...515...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: d12fk <heiko@...515...>= ;
Gerrit-MessageType: newpatchset
--g4U+mMcJcUY=--