From: "d12fk (Code Review)" Date: Tue, 18 Mar 2025 02:54:13 +0000 In-Reply-To: References: Reply-To: heiko@...515..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <7596311e6d58826548bc9f6339f0b771ad74e665-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="0zrfQulgEds="; 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 --0zrfQulgEds= 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 (#5). 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, 152 insertions(+), 1 deletion(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/904/5 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..e94af84 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -4163,6 +4163,157 @@ if (success) { dns_options_postprocess_pull(&o->dns_options); + if (!o->dns_options.servers) + { + return true; + } + +#if defined(_WIN32) || defined(TARGET_ANDROID) + /* Remove DNS related --dhcp-options */ + struct tuntap_options *tt =3D &o->tuntap_options; + tt->dns_len =3D 0; + tt->dns6_len =3D 0; + tt->domain =3D NULL; + tt->domain_search_list_len =3D 0; + + /* Set options from --dns config */ + const struct dns_domain *d =3D o->dns_options.search_domains; + while (d && tt->domain_search_list_len + 1 < N_SEARCH_LIST_LEN) + { + tt->domain_search_list[tt->domain_search_list_len++] =3D d->na= me; + 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) + { + /* Skip servers requiring unsupported config to be set */ + s =3D s->next; + } + else + { + for (int i =3D 0; i < s->addr_count; ++i) + { + if (s->addr[i].family =3D=3D AF_INET && tt->dns_len + = 1 < N_DHCP_ADDR) + { + tt->dns[tt->dns_len++] =3D s->addr[i].in.a4.s_addr; + } + else if (tt->dns6_len + 1 < N_DHCP_ADDR) + { + tt->dns6[tt->dns6_len] =3D s->addr[i].in.a6; + } + } + break; + } + } +#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) + { + /* Skip servers requiring unsupported config to be set */ + s =3D s->next; + } + else + { + 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_NET_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); + } + break; + } + } + 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: 5 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 --0zrfQulgEds= 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 #5 to this change.

View Change

git pull ssh://gerrit.openv=
pn.net:29418/openvpn refs/changes/04/904/5
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<= br> run_up_down_script(bool up, struct options *o, const struct tunta= p *tt, struct dns_script_runner_info *script_runner)
{
- if (!o->dns_option= s.script)
+ if (!o= ->dns_options.script || o->up_script)
{ return;
}
diff --git = a/src/openvpn/options.c b/src/openvpn/options.c
index 4c331= dc..e94af84 100644
--- a/src/openvpn/options.c
+++ b/src/openvpn/options.c

@@ -4163,6 +4163,157 @@
if (success)
{
= dns_options_postprocess_pull(&o->dns_options);
+ if (!o->dns_options.server= s)
+ {<= br>+ return true;
+ }
+

+#if defined(_WIN32) || defined(TARGET_ANDROID)
+ /* Remove DNS re= lated --dhcp-options */
+ struct tuntap_options *tt =3D &o->tuntap_options;=
+ tt->dns_len =3D= 0;
+ tt->d= ns6_len =3D 0;
+ = tt->domain =3D NULL;
+ tt->domain_search_list_len =3D 0;
+
+ /* Set options from --dns config */
+ const struct dns_domain *d = =3D o->dns_options.search_domains;
+ while (d && tt->domain_search_list_len= + 1 < N_SEARCH_LIST_LEN)
+ {
+ = tt->domain_search_list[tt->domain_search_list_len++] =3D d= ->name;
+ = d =3D d->next;
= + }
++ const struct dns_ser= ver *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)=
+ {
+ n= on_standard_server_port =3D true;
+ break;
+ }
+ }
+ if ((s->transport && s->transport != =3D DNS_TRANSPORT_PLAIN)
+ || (s->dnssec && s->dnssec !=3D DNS_SECU= RITY_NO)
+ = || non_standard_server_port)
+ {
+ /* Skip servers requiring unsupported config to be = set */
+ = s =3D s->next;
= + }
+ = else
+ = {
+ = for (int i =3D 0; i < s->addr_count; ++i)
+ {
+ if (s->addr[i].family = =3D=3D AF_INET && tt->dns_len + 1 < N_DHCP_ADDR)
+ {

= + tt->= ;dns[tt->dns_len++] =3D s->addr[i].in.a4.s_addr;
+ }
+ else if (tt->dn= s6_len + 1 < N_DHCP_ADDR)
+ {
+ tt->dns6[tt->dns6_len] =3D s->a= ddr[i].in.a6;
+ = }
+ = }
+ = break;
+ = }
+ }=
+#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)
+ {
+ /* Skip servers requiring unsupported config = to be set */
+ = s =3D s->next;
+ }
= + else
+ = {
+ = for (int i =3D 0; i < s->addr_count; ++i)
+ {
+ if (s->addr[i].fam= ily =3D=3D AF_INET)
+= {
+ p[1] =3D "DNS";
+ p[2] =3D print_in= _addr_t(s->addr[i].in.a4.s_addr, IA_NET_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);=
+ }+ break;
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ gc_free(&gc);
+#endif /* defined(_WIN32) || define= d(TARGET_ANDROID) */
}
return suc= cess;
}

To view, visit= change 9= 04. 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: 5
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
--0zrfQulgEds=--