From: "d12fk (Code Review)" Date: Thu, 20 Mar 2025 12:09:46 +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="o64tBAZM/No="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: dns: deal with --dhcp-options when --dns is active List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --o64tBAZM/No= 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 (#9). The following approvals got outdated and were removed: Code-Review-1 by flichtenheld Change subject: dns: deal with --dhcp-options when --dns is active ...................................................................... dns: deal with --dhcp-options when --dns is active Since --dns settings overrule DNS related --dhcp-options, remove the latter when values were defined via --dns. To stay as backward compatible as possible, we add foreign_options to the script hook environment from the --dns values when a --up script is defined. In that case the default --dns-updown is not run, even when --dns values are present, to prevent double DNS configuration. This way an existing --up script that deals with DNS can run, without the immediate need to change after an openvpn upgrade and a server pushing --dns options. If you specify a custom --dns-updown, or force running the default dns-updown that comes with openvpn, those compat env vars are not set for --up scripts and the dns-updown command is run, even when there's an --up script present. Since Android uses the DNS values from tuntap_options, we always override those with --dns stuff unconditionally. Change-Id: I635c4018fb43b5976a39b6a90cb2e9cb2570cd6a Signed-off-by: Heiko Hund --- M doc/man-sections/script-options.rst M src/openvpn/dns.c M src/openvpn/options.c 3 files changed, 231 insertions(+), 158 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/904/9 diff --git a/doc/man-sections/script-options.rst b/doc/man-sections/script-= options.rst index 8285122..765518a 100644 --- a/doc/man-sections/script-options.rst +++ b/doc/man-sections/script-options.rst @@ -258,9 +258,11 @@ Note that DNS-related ``--dhcp-option``\ s will not be handled by this h= ook. If any ``--dns server`` option is present, DNS-related ``--dhcp-option``= \ s will be ignored. If an ``--up`` script is defined, foreign_option env vars wi= ll be - generated from ``--dns`` options and passed to the script. The ``--dns-u= pdown`` - command is not run if a ``--up`` script is defined. Both is done for bac= kward - compatibility. + generated from ``--dns`` options and passed to the script for backward c= ompatibility. + + Also for backward compatibility, the ``--dns-updown`` command is not run= if a + ``--up`` script is defined, unless a user-defined ``dns-updown`` was spe= cified, + or running the default updown is forced via the ``--dns-updown force`` o= ption. --down cmd Run command ``cmd`` after TUN/TAP device close (post ``--user`` UID diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c index 0ad8e44..c654d78 100644 --- a/src/openvpn/dns.c +++ b/src/openvpn/dns.c @@ -703,7 +703,8 @@ static void run_up_down_command(bool up, struct options *o, const struct tuntap *tt, s= truct dns_updown_runner_info *updown_runner) { - if (!o->dns_options.updown) + struct dns_options *dns =3D &o->dns_options; + if (!dns->updown || (o->up_script && !dns->user_defined_updown)) { return; } @@ -713,7 +714,7 @@ if (!updown_runner->required) { /* Run dns updown directly */ - status =3D do_run_up_down_command(up, NULL, &o->dns_options, tt); + status =3D do_run_up_down_command(up, NULL, dns, tt); } else { diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 1a9c337..25f97e0 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -530,7 +530,9 @@ " sni : DNS server name indication\n" "--dns search-domains [domain ...]:\n" " Add domains to DNS domain search list\n" - "--dns-updown cmd|disable : Run cmd as user defined dns config command= or disable it\n" + "--dns-updown cmd|force|disable :\n" + " Run cmd as user defined dns config command, force r= unning the\n" + " default one even when --up exists, or disable runni= ng it.\n" "--auth-retry t : How to handle auth failures. Set t to\n" " none (default), interact, or nointeract.\n" "--static-challenge t e []: Enable static challenge/resp= onse protocol using\n" @@ -1374,149 +1376,6 @@ } } } - -/* - * If DNS options are set use these for TUN/TAP options as well. - * Applies to DNS, DNS6 and DOMAIN-SEARCH. - * Existing options will be discarded. - */ -static void -tuntap_options_copy_dns(struct options *o) -{ - struct tuntap_options *tt =3D &o->tuntap_options; - struct dns_options *dns =3D &o->dns_options; - - if (dns->search_domains) - { - tt->domain_search_list_len =3D 0; - const struct dns_domain *domain =3D dns->search_domains; - while (domain && tt->domain_search_list_len < N_SEARCH_LIST_LEN) - { - tt->domain_search_list[tt->domain_search_list_len++] =3D domai= n->name; - domain =3D domain->next; - } - if (domain) - { - msg(M_WARN, "WARNING: couldn't copy all --dns search-domains t= o --dhcp-option"); - } - tt->dhcp_options |=3D DHCP_OPTIONS_DHCP_REQUIRED; - } - - if (dns->servers) - { - tt->dns_len =3D 0; - tt->dns6_len =3D 0; - bool overflow =3D false; - const struct dns_server *server =3D dns->servers; - while (server) - { - for (int i =3D 0; i < server->addr_count; ++i) - { - if (server->addr[i].family =3D=3D AF_INET) - { - if (tt->dns_len >=3D N_DHCP_ADDR) - { - overflow =3D true; - continue; - } - tt->dns[tt->dns_len++] =3D ntohl(server->addr[i].in.a4= .s_addr); - } - else - { - if (tt->dns6_len >=3D N_DHCP_ADDR) - { - overflow =3D true; - continue; - } - tt->dns6[tt->dns6_len++] =3D server->addr[i].in.a6; - } - } - server =3D server->next; - } - if (overflow) - { - msg(M_WARN, "WARNING: couldn't copy all --dns server addresses= to --dhcp-option"); - } - tt->dhcp_options |=3D DHCP_OPTIONS_DHCP_OPTIONAL; - } -} -#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 =3D o->dns_options.search_domains; - const struct dns_server *server =3D o->dns_options.servers; - if (!domain && !server) - { - return; - } - - /* reset the index since we're starting all over again */ - int opt_max =3D o->foreign_option_index; - o->foreign_option_index =3D 0; - - for (int i =3D 1; i <=3D opt_max; ++i) - { - char name[32]; - snprintf(name, sizeof(name), "foreign_option_%d", i); - - const char *env_str =3D env_set_get(es, name); - const char *value =3D strchr(env_str, '=3D') + 1; - if ((domain && strstr(value, "dhcp-option DOMAIN-SEARCH") =3D=3D v= alue) - || (server && strstr(value, "dhcp-option DNS") =3D=3D value)) - { - setenv_del(es, name); - } - else - { - setenv_foreign_option(o, &value, 1, es); - } - } - - struct gc_arena gc =3D gc_new(); - - while (server) - { - for (size_t i =3D 0; i < server->addr_count; ++i) - { - if (server->addr[i].family =3D=3D AF_INET) - { - const char *argv[] =3D { - "dhcp-option", - "DNS", - print_in_addr_t(server->addr[i].in.a4.s_addr, 0, &gc) - }; - setenv_foreign_option(o, argv, 3, es); - } - else - { - const char *argv[] =3D { - "dhcp-option", - "DNS6", - print_in6_addr(server->addr[i].in.a6, 0, &gc) - }; - setenv_foreign_option(o, argv, 3, es); - } - } - server =3D server->next; - } - while (domain) - { - const char *argv[] =3D { "dhcp-option", "DOMAIN-SEARCH", domain->n= ame }; - setenv_foreign_option(o, argv, 3, es); - domain =3D domain->next; - } - - gc_free(&gc); - - /* remove old leftover entries */ - while (o->foreign_option_index < opt_max) - { - char name[32]; - snprintf(name, sizeof(name), "foreign_option_%d", opt_max--); - setenv_del(es, name); - } -} #endif /* if defined(_WIN32) || defined(TARGET_ANDROID) */ #ifndef ENABLE_SMALL @@ -3616,6 +3475,207 @@ } } +#if defined(_WIN32) || defined(TARGET_ANDROID) +/** + * @brief Postprocess DNS related settings + * + * Discard existing TUN/TAP DNS options and override them with --dns + * values for Android. If there are no --dns servers leave things alone. + * + * @param o pointer to the options struct + */ +static void +tuntap_options_postprocess_dns(struct options *o) +{ + struct dns_options *dns =3D &o->dns_options; + if (!dns->servers) + { + return; + } + + /* 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; + +#if defined(TARGET_ANDROID) + /* Set options from --dns config */ + const struct dns_domain *d =3D dns->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; + } + if (d) + { + msg(M_WARN, "WARNING: couldn't copy all --dns search-domains to TU= N/TAP"); + } + + const struct dns_server *s =3D dns->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 + { + bool overflow =3D false; + 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; + } + else + { + overflow =3D true; + } + } + if (overflow) + { + msg(M_WARN, "WARNING: couldn't copy all --dns server addre= sses to TUN/TAP"); + } + return; + } + } +#endif /* if defined(TARGET_ANDROID) */ +} + +#else /* if defined(_WIN32) || defined(TARGET_ANDROID) */ + +/** + * @brief Postprocess DNS related settings in the env_set + * + * Discard existing --dhcp-options and set values from --dns + * instead. If there are no --dns servers leave things alone. + * + * @param o pointer to the options struct + * @param es env set to modify potentially + */ +static void +foreign_options_postprocess_dns(struct options *o, struct env_set *es) +{ + struct dns_options *dns =3D &o->dns_options; + if (!dns->servers) + { + return; + } + + /* 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_ind= ex); + setenv_str(es, BSTR(&name), BSTR(&value)); + } + } + + /* Set foreign option env vars from --dns config */ + if (!o->up_script || dns->user_defined_updown) + { + /* No need to, when there is no --up script or a custom updown */ + return; + } + + 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 dns->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 dns->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_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); + } + break; + } + } + gc_free(&gc); +} +#endif /* if defined(_WIN32) || defined(TARGET_ANDROID) */ + static void options_postprocess_mutate(struct options *o, struct env_set *es) { @@ -3801,9 +3861,9 @@ else { #if defined(_WIN32) || defined(TARGET_ANDROID) - tuntap_options_copy_dns(o); + tuntap_options_postprocess_dns(o); #else - foreign_options_copy_dns(o, es); + foreign_options_postprocess_dns(o, es); #endif } if (o->auth_token_generate && !o->auth_token_renewal) @@ -4186,9 +4246,9 @@ { dns_options_postprocess_pull(&o->dns_options); #if defined(_WIN32) || defined(TARGET_ANDROID) - tuntap_options_copy_dns(o); + tuntap_options_postprocess_dns(o); #else - foreign_options_copy_dns(o, es); + foreign_options_postprocess_dns(o, es); #endif } return success; @@ -8089,19 +8149,29 @@ goto err; } #ifdef ENABLE_DNS_UPDOWN + struct dns_options *dns =3D &options->dns_options; if (streq(p[1], "disable")) { - options->dns_options.updown =3D NULL; + dns->updown =3D NULL; + dns->user_defined_updown =3D false; + } + else if (streq(p[1], "force")) + { + /* force dns-updown run, even if a --up script is defined */ + if (dns->updown) + { + dns->user_defined_updown =3D true; + } } else { - if (options->dns_options.user_defined_updown =3D=3D false) + if (dns->user_defined_updown =3D=3D false) { /* Unset the default command to prevent warnings */ - options->dns_options.updown =3D NULL; + dns->updown =3D NULL; } - set_user_script(options, &options->dns_options.updown, p[1], p= [0], false); - options->dns_options.user_defined_updown =3D true; + set_user_script(options, &dns->updown, p[1], p[0], false); + dns->user_defined_updown =3D true; } #endif /* ENABLE_DNS_UPDOWN */ } -- 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: 9 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 --o64tBAZM/No= 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 #9 to this change.

View Change

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

dns: deal with --dhcp-options when --dns is a=
ctive

Since --dns settings overrule DNS related --dhcp-options,
r= emove the latter when values were defined via --dns.

To stay as back= ward compatible as possible, we add foreign_options to
the script hook e= nvironment from the --dns values when a --up script
is defined. In that = case the default --dns-updown is not run, even
when --dns values are pre= sent, to prevent double DNS configuration.
This way an existing --up scr= ipt that deals with DNS can run, without
the immediate need to change af= ter an openvpn upgrade and a server
pushing --dns options.

If you= specify a custom --dns-updown, or force running the default
dns-updown = that comes with openvpn, those compat env vars are not set
for --up scri= pts and the dns-updown command is run, even when there's
an --up scr= ipt present.

Since Android uses the DNS values from tuntap_options, = we always
override those with --dns stuff unconditionally.

Change= -Id: I635c4018fb43b5976a39b6a90cb2e9cb2570cd6a
Signed-off-by: Heiko Hund= <heiko@...2662...>
---
M doc/man-sections/script-options.rstM src/openvpn/dns.c
M src/openvpn/options.c
3 files changed, 231 in= sertions(+), 158 deletions(-)

git pull ssh://gerrit.openvpn.net:2=
9418/openvpn refs/changes/04/904/9
diff --git a/doc/man-sections/sc=
ript-options.rst b/doc/man-sections/script-options.rst
inde= x 8285122..765518a 100644
--- a/doc/man-sections/script-opt= ions.rst
+++ b/doc/man-sections/script-options.rst@@ -258,9 +258,11 @@
Note that DNS-related ``--d= hcp-option``\ s will not be handled by this hook.
If any= ``--dns server`` option is present, DNS-related ``--dhcp-option``\ s will<= /span>
be ignored. If an ``--up`` script is defined, foreign_op= tion env vars will be
-= generated from ``--dns`` options and passed to the script. The ``--dns-up= down``
- command is no= t run if a ``--up`` script is defined. Both is done for backward
= - compatibility.
+ generated from ``--dns`` options = and passed to the script for backward compatibility.

+
+ Also for backward compatibility, the ``--dns-updown`` comm= and is not run if a
+= ``--up`` script is defined, unless a user-defined ``dns-updown`` was spec= ified,
+ or running = the default updown is forced via the ``--dns-updown force`` option.<= br>
--down cmd
Run command ``cmd= `` after TUN/TAP device close (post ``--user`` UID
diff --g= it a/src/openvpn/dns.c b/src/openvpn/dns.c
index 0ad8e44..c= 654d78 100644
--- a/src/openvpn/dns.c
+++ b= /src/openvpn/dns.c
@@ -703,7 +703,8 @@
sta= tic void
run_up_down_command(bool up, struct options *o, c= onst struct tuntap *tt, struct dns_updown_runner_info *updown_runner)
{
- if (= !o->dns_options.updown)
+ struct dns_options *dns =3D &o->dns_options;
+ if (!dns->updown || (o-&g= t;up_script && !dns->user_defined_updown))

= {
return;
}
@= @ -713,7 +714,7 @@
if (!updown_runner->required)
{
/* Run dns updown directly */=
- status =3D do= _run_up_down_command(up, NULL, &o->dns_options, tt);
+ status =3D do_run_up_down_c= ommand(up, NULL, dns, tt);

}
else=
{
diff --git a/src/openvpn/options.c = b/src/openvpn/options.c
index 1a9c337..25f97e0 100644
--- a/src/openvpn/options.c
+++ b/src/openvpn/op= tions.c
@@ -530,7 +530,9 @@
" = sni <domain> : DNS server name indication\n"
"--dns search-domains <domain> [domain ...]:\n&q= uot;
" Add domains to DNS domain= search list\n"
- = "--dns-updown cmd|disable : Run cmd as user defined dns config comm= and or disable it\n"
+ "--dns-updown cmd|force|disable :\n"
+ " Run cmd as= user defined dns config command, force running the\n"
+ " default= one even when --up exists, or disable running it.\n"

= "--auth-retry t : How to handle auth failures. Set t to\n"=
" none (default), interact, or = nointeract.\n"
"--static-challenge t e [<= scrv1|concat>]: Enable static challenge/response protocol using\n"<= /span>
@@ -1374,149 +1376,6 @@
} }
}
-
-/*=
- * If DNS options are set us= e these for TUN/TAP options as well.
- * Applies to DNS, DNS6 and DOMAIN-SEARCH.
- * Existing options will be discarded.<= /span>
- */
-static void
-tuntap_options_copy_dns(struct options *o)<= br>-{
- struct tuntap_options *tt =3D &o->tunt= ap_options;
- struct= dns_options *dns =3D &o->dns_options;
-
- {
- = tt->domain_search_list_len =3D 0;
- const struct dns_domain *domain =3D dns->sea= rch_domains;
- w= hile (domain && tt->domain_search_list_len < N_SEARCH_LIST_LE= N)
- {- tt->domain_searc= h_list[tt->domain_search_list_len++] =3D domain->name;
- domain =3D domain->ne= xt;

- }- if (domain)
= - {
- msg(M_WARN, "WARNING: coul= dn't copy all --dns search-domains to --dhcp-option");
<= span style=3D"color: hsl(0, 100%, 40%);">- }

- tt->dhcp_options |=3D DHCP_OPTIO= NS_DHCP_REQUIRED;
- = }
-
- if (dns->servers)
- {
- tt->dns_len =3D 0;
- tt->dns6_len =3D 0;
- bool overflow =3D false;
- const struct dns_s= erver *server =3D dns->servers;
- while (server)
- {
- for (int i =3D 0; i < server->addr_count; ++i)- {
- if (server->addr[i]= .family =3D=3D AF_INET)
- {
- = if (tt->dns_len >=3D N_DHCP_ADDR)
- {
- overflow =3D tr= ue;
- = continue;
- = }
- = tt->dns[tt->dns_len++] =3D ntohl(server->addr[i].in.= a4.s_addr);
- = }
- = else
- = {
- = if (tt->dns6_len >=3D N_DHCP_ADDR)
- {
- overflow =3D true;
- continue;<= /span>
- }<= /span>
- tt= ->dns6[tt->dns6_len++] =3D server->addr[i].in.a6;
- }
- }
- server =3D server->next;
= - }
- if (overflow)
- {
- msg(M_WARN, "WARNING: couldn't cop= y all --dns server addresses to --dhcp-option");
- }
- tt->dhcp_options |=3D DHCP_OPTIONS_DHCP_OPTI= ONAL;
- }
= -}
-#else /* if defined(_WIN32) || defined(TARGET_ANDROI= D) */
-static void
-foreign_options_copy_dns(s= truct options *o, struct env_set *es)
-{
- = const struct dns_domain *domain =3D o->dns_options.search_domains;

- const struct dns_server= *server =3D o->dns_options.servers;
- if (!domain && !server)
- {
- return;
- }
-
- /* reset the index sinc= e we're starting all over again */
- int opt_max =3D o->foreign_option_index;
= - o->foreign_option_index = =3D 0;
-
- for (int i =3D 1; i <=3D opt_= max; ++i)

- {=
- char name[32];
- snprintf(name, size= of(name), "foreign_option_%d", i);
-
- const char *env_str =3D env_set_get(es, name);
- const char *value =3D strchr(env= _str, '=3D') + 1;
- if ((domain && strstr(value, "dhcp-option DOMAIN-S= EARCH") =3D=3D value)
- || (server && strstr(value, "dhcp-option DNS&= quot;) =3D=3D value))
-= {
- = setenv_del(es, name);
-= }
- else=
- {
<= span style=3D"color: hsl(0, 100%, 40%);">- setenv_foreign_option= (o, &value, 1, es);

- }
- }
-
- struct gc_arena gc =3D gc_new();
= -
- while (server)
- {
- {
- if (server->addr[i].family = =3D=3D AF_INET)
- = {
- = const char *argv[] =3D {
- "dhcp-option",
- "DNS",
= - print_in_add= r_t(server->addr[i].in.a4.s_addr, 0, &gc)
- };
- setenv_foreign_option(o, argv, 3, e= s);
- }
- else- {
- const char *argv[] =3D = {
- = "dhcp-option",
- print_in6_addr(server->addr[i].in= .a6, 0, &gc)
- = };
- = setenv_foreign_option(o, argv, 3, es);
- }
- }
- server =3D server->next;
- }
-= {
- const ch= ar *argv[] =3D { "dhcp-option", "DOMAIN-SEARCH", domain= ->name };
- s= etenv_foreign_option(o, argv, 3, es);
- domain =3D domain->next;
- }
-
- g= c_free(&gc);
-
- /* remove old leftover= entries */
- while = (o->foreign_option_index < opt_max)
- {
- char name[32];
- snprintf(name, sizeof(name), "foreign_option_%d", op= t_max--);
- sete= nv_del(es, name);
- = }
-}
#= endif /* if defined(_WIN32) || defined(TARGET_ANDROID) */
=
#ifndef ENABLE_SMALL
@@ -3616,6 +3475,207= @@
}
}

<= span style=3D"color: hsl(120, 100%, 40%);">+#if defined(_WIN32) || defined(= TARGET_ANDROID)

+/**<= /span>
+ * @brief Postproces= s DNS related settings
+ * Discard exi= sting TUN/TAP DNS options and override them with --dns
+ * values for Android. If there are no -= -dns servers leave things alone.
+ *
+ * @= param o pointer to the options struct
+ */
+static void
+tu= ntap_options_postprocess_dns(struct options *o)
+{
+ struct dns_options *dns =3D &o->dns_options;+ if (!dns->servers)
+ {
+ return;
+ }
+
= + /* Remove DNS related --dhcp-options */
+ struct tuntap_options *tt =3D &o->tunta= p_options;
+ tt-&g= t;dns_len =3D 0;
+ = tt->dns6_len =3D 0;
+ tt->domain =3D NULL;
+ tt->domain_search_list_len =3D 0;
+
+#if defined(TARGET_ANDROID)
+ /* Set options from --dns config */
+ const struct dns_domain *d = =3D dns->search_domains;

+ while (d && tt->domain_search_list_len + 1 < N_SE= ARCH_LIST_LEN)
+ {=
+ tt->doma= in_search_list[tt->domain_search_list_len++] =3D d->name;
<= span style=3D"color: hsl(120, 100%, 40%);">+ d =3D d->next;
+ }
+ if (d)
+ {
+ msg(M_WARN, "WARNING: couldn't copy all --dns= search-domains to TUN/TAP");
+ }
= +
+ const struct d= ns_server *s =3D dns->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 &&= amp; s->transport !=3D DNS_TRANSPORT_PLAIN)

+ || (s->dnssec && s->dn= ssec !=3D DNS_SECURITY_NO)
+ || non_standard_server_port)
+ {
+ /* Skip servers requiring unsupported config = to be set */
+ = s =3D s->next;
+ = else
+ {
+ bool overflo= w =3D false;
+ = 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->dn= s_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;
+ }
+ else
+ {
+ overflow =3D true;<= /span>
+ }
+ }+ if (overflow)
+ {+ msg(M_WARN, = "WARNING: couldn't copy all --dns server addresses to TUN/TAP"= ;);
+ }
+ return;
+ }
+ }

+#endif /* if defined(TARGET_ANDROID) */=
+}
+
+#else /* if defined(_WIN32) || defined(TARGET_ANDROID) */
+
+/**
+ * @brief Postprocess DNS related settings in the env_set
+ *
+ * Discard existing --dhcp-options and = set values from --dns
+ * instead. If there are no --dns servers leave things alone.
<= span style=3D"color: hsl(120, 100%, 40%);">+ *

+ * @param o pointer to the options struct
+ * @param es env se= t to modify potentially
+ */
+static void<= /span>
+foreign_options_post= process_dns(struct options *o, struct env_set *es)
+{
+ struct dns_options *dns =3D &o->dns_options;=
+ if (!dns->servers)<= /span>
+ {
+ return;

+ }
+
+ /* Clean up env from --dhcp-option DNS config */
+ struct gc_arena gc =3D gc_new();
+ struct buffer nam= e =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc);
+ struct buffer value =3D alloc_buf_gc(OPTION_= PARM_SIZE, &gc);
= +
+ const int fo_c= ount =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));<= /span>
+ const char *= item_val =3D strchr(env_str, '=3D') + 1;
+ buf_clear(&value);
+ buf_printf(&value, &quo= t;%s", item_val);

+ /* Remov= e 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-SUF= FIX ", 22) !=3D 0
+ = && strncmp(item_val + 12, "DOMAIN ", 7) !=3D= 0
+ &= amp;& strncmp(item_val + 12, "DNS6 ", 5) !=3D 0
+ && strncm= p(item_val + 12, "DNS ", 4) !=3D 0))

+ {
+ /* Re-set the item with potentially updated na= me */
+ bu= f_clear(&name);
+= buf_printf(&name, "foreign_option_%d", ++o->fo= reign_option_index);
= + setenv_str(es, BSTR(&name), BSTR(&value));
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ }
+
= + /* Set foreign option env vars from --dns config */
+ if (!o->up_script || dns->us= er_defined_updown)
+ = {
+ /* No n= eed to, when there is no --up script or a custom updown */
+ return;
+ }
+
+ const char *p[] =3D { "dhcp-option", NULL, NULL };+ size_t p_len =3D sizeof(= p) / sizeof(p[0]);
+<= /span>
+ p[1] =3D "D= OMAIN";
+ con= st struct dns_domain *d =3D dns->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 d= ns->servers;
+ = while (s)
+ {
+ bool non_standar= d_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->trans= port !=3D DNS_TRANSPORT_PLAIN)
+ || (s->dnssec && s->dnssec !=3D DNS_SE= CURITY_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-&= gt;addr_count; ++i)
+= {
+ = if (s->addr[i].family =3D=3D AF_INET)
+ {
+ p[1] =3D "DNS";<= /span>
+ = p[2] =3D print_in_addr_t(s->addr[i].in.a4.s_addr, IA_NET_ORDER, &gc)= ;
+ }<= /span>
+ else=
+ {
+ p= [1] =3D "DNS6";
+ p[2] =3D print_in6_addr(s->addr[i].in.a6, 0, &= amp;gc);
+ = }
+ = setenv_foreign_option(o, (const char **)p, p_len, es);
+ }
+ break;
+ }
+ }
+ gc_free(&gc);
+}
+#endif= /* if defined(_WIN32) || defined(TARGET_ANDROID) */
+
static void
<= span> options_postprocess_mutate(struct options *o, struct env_set *es)
{
@@ -3801,9 +3861,9 @@
= else
{
#if defined(_WIN32) || defined= (TARGET_ANDROID)
- = tuntap_options_copy_dns(o);
+ tuntap_options_postprocess_dns(o);
#els= e
- foreign_opti= ons_copy_dns(o, es);
= + foreign_options_postprocess_dns(o, es);
#endif
}
if (o->auth_token_generate &am= p;& !o->auth_token_renewal)
@@ -4186,9 +4246,9 @@
{
dns_options_postprocess_pull(= &o->dns_options);
#if defined(_WIN32) || defined(TA= RGET_ANDROID)
- = tuntap_options_copy_dns(o);
+ tuntap_options_postprocess_dns(o);
#else
- foreign_options= _copy_dns(o, es);
+ = foreign_options_postprocess_dns(o, es);
#endif
}
return success;
@@ -= 8089,19 +8149,29 @@
goto err;
= }
#ifdef ENABLE_DNS_UPDOWN
+ struct dns_options *dns =3D &= options->dns_options;
if (streq(p[1], "dis= able"))
{
- options->dns_options.updown =3D NULL;
+ dns->updo= wn =3D NULL;
+ = dns->user_defined_updown =3D false;
+ }
+ else if (streq(p[1], "force"))
+ {

+ /* force dns-updown run, even= if a --up script is defined */
+ if (dns->updown)
+ {
+ dns->user_defined_updown =3D true;
+ } }
else
{<= /span>
- if (option= s->dns_options.user_defined_updown =3D=3D false)
+ if (dns->user_defined_updo= wn =3D=3D false)
{
= /* Unset the default command to prevent warnings */
- options->dns_options= .updown =3D NULL;
+ = dns->updown =3D NULL;
}
- set_user_script= (options, &options->dns_options.updown, p[1], p[0], false);- options->dns_op= tions.user_defined_updown =3D true;
+ set_user_script(options, &dns->updown, p= [1], p[0], false);
+ = dns->user_defined_updown =3D true;
}=
#endif /* ENABLE_DNS_UPDOWN */
}

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

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I635c4018fb43b5976a39b6a90cb= 2e9cb2570cd6a
Gerrit-Change-Number: 904
Gerrit-PatchSet: 9
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
--o64tBAZM/No=--