From: "d12fk (Code Review)" Date: Thu, 20 Mar 2025 02:51:34 +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="zvOU3wwFghI="; 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 --zvOU3wwFghI= 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 (#8). 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/8 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..22c882b 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 (domain) + { + 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: 8 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 --zvOU3wwFghI= 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 #8 to this change.

View Change

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04=
/904/8
diff --git a/doc/man-sections/script-options.rst b/doc/man-s=
ections/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 hook.
If any ``--dns server`` option is = present, DNS-related ``--dhcp-option``\ s will
be ignore= d. If an ``--up`` script is defined, foreign_option env vars will be=
- generated from ``--dns`` o= ptions and passed to the script. The ``--dns-updown``
- command is not run if a ``--up`` script i= s defined. Both is done for backward
- compatibility.
+ generated from ``--dns`` options and passed to the script for= backward compatibility.
+
+ Also for bac= kward compatibility, the ``--dns-updown`` command is not run if a+ ``--up`` script is defined,= unless a user-defined ``dns-updown`` was specified,
+ or running the default updown is forced= via the ``--dns-updown force`` option.

= --down cmd
Run command ``cmd`` after TUN/TAP device clos= e (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
ru= n_up_down_command(bool up, struct options *o, const struct tuntap *tt, stru= ct dns_updown_runner_info *updown_runner)
{
- if (!o->dns_options.updown)
+ struct dns_option= s *dns =3D &o->dns_options;
+ if (!dns->updown || (o->up_script && !dns-= >user_defined_updown))
{
r= eturn;
}
@@ -713,7 +714,7 @@ if (!updown_runner->required)
{<= br> /* Run dns updown directly */
- status =3D do_run_up_down_command(up, NUL= L, &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..22c882b 100644
--- a/src/openvpn= /options.c
+++ b/src/openvpn/options.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"<= br>- "--dns-updown cmd|di= sable : 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 com= mand, 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 st= atic challenge/response 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
-tun= tap_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_do= mains)
- {- tt->domain_search_li= st_len =3D 0;
- = const struct dns_domain *domain =3D dns->search_domains;
- while (domain && tt-&= gt;domain_search_list_len < N_SEARCH_LIST_LEN)

- {
- tt->domain_search_list[tt->domain_search_= list_len++] =3D domain->name;
- domain =3D domain->next;
- }
- if (domain)
- {
- }
= - tt->dhcp_options |=3D DHCP_OPTIONS_DHCP_REQUIRED;
- }
-
- {
- tt= ->dns_len =3D 0;
- = tt->dns6_len =3D 0;
- bool overflow =3D false;
- const struct dns_server *server =3D dns->serve= rs;
- while (ser= ver)
- {<= br>- for (int i =3D 0;= i < server->addr_count; ++i)
- {
- if (server->addr[i].family =3D=3D AF_INET)<= br>- {
<= span style=3D"color: hsl(0, 100%, 40%);">- if (tt->dn= s_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;
- }
- = msg(M_WARN, "WARNING: couldn't copy all --dns server addresses = to --dhcp-option");
- t= t->dhcp_options |=3D DHCP_OPTIONS_DHCP_OPTIONAL;
- }
-}
-#els= e /* if defined(_WIN32) || defined(TARGET_ANDROID) */
-static void
-foreign_options_copy_dns(struct options *o, struct en= v_set *es)
-{- const struct dns_domain *do= main =3D o->dns_options.search_domains;
- const struct dns_server *server =3D o->dns_opti= ons.servers;
- if (!= domain && !server)
- {
- ret= urn;
- }
<= span style=3D"color: hsl(0, 100%, 40%);">-
- /* reset the index since we're starting all ov= er again */
- int op= t_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_opti= on_%d", i);
-
- const char *env_st= r =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 value)
- || (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();
-
- wh= ile (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 {
- &qu= ot;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)
<= span style=3D"color: hsl(0, 100%, 40%);">- };

- setenv_foreign_optio= n(o, argv, 3, es);

- = }
- }
- server =3D serv= er->next;
- }
- while (domain)=
- {
- const char *argv[] =3D { "dhcp= -option", "DOMAIN-SEARCH", domain->name };
- setenv_foreign_option(o, arg= v, 3, es);

- dom= ain =3D domain->next;
-
- gc_free(&gc);

-
- /* remove old leftover entries */
- while (o->foreign_option_index &= lt; opt_max)
- {
- char name[32];

- snprintf(name, s= izeof(name), "foreign_option_%d", opt_max--);
- setenv_del(es, name);
<= span style=3D"color: hsl(0, 100%, 40%);">- }
-}
#endif /* if defined(_WIN32) |= | defined(TARGET_ANDROID) */

#ifndef ENA= BLE_SMALL
@@ -3616,6 +3475,207 @@
}

}

+#if defined(_WIN32) || defined(TARGET_ANDROID)
+/**
+ * @brief Postprocess DNS related settings=
+ *
+ * Discard existing TUN/TAP DNS options a= nd override them with --dns
+ * values for Android. If there are no --dns servers leave things a= lone.
+ *
<= span style=3D"color: hsl(120, 100%, 40%);">+ * @param o pointer to the = options struct
+ */
+static void+tuntap_options_postprocess_dn= s(struct options *o)
= +{
+ struct dns_op= tions *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 N= ULL;
+ tt->doma= in_search_list_len =3D 0;
+
+#if defined(T= ARGET_ANDROID)
+ /= * Set options from --dns config */
+ const struct dns_domain *d =3D dns->search_domains;
+ while (d &&am= p; 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 (domain)
+ msg= (M_WARN, "WARNING: couldn't copy all --dns search-domains to TUN/T= AP");
+ }
+
+ const struct dns_server *s =3D dns-&g= t;servers;
+ while= (s)
+ {+ bool non_standard_ser= ver_port =3D false;
+= for (int i =3D 0; i < s->addr_count; ++i)
+ {
+ if (s->addr[i].port && s-&= gt;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: coul= dn'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 --d= ns
+ * instead. If th= ere are no --dns servers leave things alone.
+ *
+ * @param o pointer to the options struct
+ * @param es env set to modify potenti= ally
+ */
<= span style=3D"color: hsl(120, 100%, 40%);">+static void
+foreign_options_postprocess_dns(struct = options *o, struct env_set *es)
+{
+ st= ruct 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->fore= ign_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&q= uot;, i);
+ co= nst 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_va= l);
+
+ /* Remove foreign option it= em 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<= /span>
+ &= ;& 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 */
<= span style=3D"color: hsl(120, 100%, 40%);">+ buf_clear(&name= );
+ buf_p= rintf(&name, "foreign_option_%d", ++o->foreign_option_inde= x);
+ sete= nv_str(es, BSTR(&name), BSTR(&value));
+ }
+ }
+ /* Set forei= gn option env vars from --dns config */
+ if (!o->up_script || dns->user_defined_updown= )
+ {
+ /* No need to, when ther= e 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_dom= ain *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].p= ort !=3D 53)
+ = {
+ = non_standard_server_port =3D true;
+ break;
+ }
+ }
+ if ((s->transport && s->transport !=3D DNS_TRANS= PORT_PLAIN)
+ = || (s->dnssec && s->dnssec !=3D DNS_SECURITY_NO)+ || non_standard_= server_port)
+ = {
+ /* Sk= ip servers requiring unsupported config to be set */
+ s =3D s->next;
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ else
+ {
+ for (int i =3D 0; i < s->addr_count; ++i= )
+ {
+ if (s-&g= t;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);+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ setenv_foreign_= option(o, (const char **)p, p_len, es);

+ }
+ break;
+ }
+ gc_free= (&gc);
+}<= br>+#endif /* if defined(_WIN32= ) || defined(TARGET_ANDROID) */
+
static void
options_postproc= ess_mutate(struct options *o, struct env_set *es)
{=
@@ -3801,9 +3861,9 @@
else
= {
#if defined(_WIN32) || defined(TARGET_ANDROID)

- tuntap_options_copy= _dns(o);
+ tun= tap_options_postprocess_dns(o);
#else
- foreign_options_copy_dns(o, es);
+ foreign_optio= ns_postprocess_dns(o, es);
#endif
}
if (o->auth_token_generate && !o->auth_to= ken_renewal)
@@ -4186,9 +4246,9 @@
{
dns_options_postprocess_pull(&o->dns_options= );
#if defined(_WIN32) || defined(TARGET_ANDROID)- tuntap_options_copy_dn= s(o);
+ tuntap= _options_postprocess_dns(o);
#else
- foreign_options_copy_dns(o, es);
+ foreign_option= s_postprocess_dns(o, es);
#endif
}
return success;
@@ -8089,19 +8149,29 @@<= /span>
goto err;
} #ifdef ENABLE_DNS_UPDOWN
+ struct dns_options *dns =3D &options->dns_optio= ns;
if (streq(p[1], "disable")) {
- = options->dns_options.updown =3D NULL;
+ dns->updown =3D NULL;+ dns->user_def= ined_updown =3D false;
+ = else if (streq(p[1], "force"))
+ {
+ /* force dns-updown run, even if a --up script is d= efined */
+ = if (dns->updown)
+ {
+ = dns->user_defined_updown =3D true;
+ }
}
else
{
- if (options->dns_options.use= r_defined_updown =3D=3D false)
+ if (dns->user_defined_updown =3D=3D false)=
{
/* Unset the defa= ult command to prevent warnings */
- options->dns_options.updown =3D NULL;
+ dns->u= pdown =3D NULL;
}
- set_user_script(options, &options-= >dns_options.updown, p[1], p[0], false);
- options->dns_options.user_defined_updo= wn =3D true;
+ = set_user_script(options, &dns->updown, p[1], p[0], false);
+ dns->user= _defined_updown =3D true;
}
#endi= f /* ENABLE_DNS_UPDOWN */
}

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

=
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I635c4018fb43b5976a39b6a90cb= 2e9cb2570cd6a
Gerrit-Change-Number: 904
Gerrit-PatchSet: 8
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
--zvOU3wwFghI=--