add support for --dns option
PATCH 1/1 add support for --dns option
As a first step towards DNS configuration in openvpn and a unified way to push DNS related settings to clients in v2 and v3, this commit adds support for parsing the new --dns option. Later commits will add support for setting up DNS on different platforms.
For now, --dns and DNS related --dhcp-option can be used together for smoother transition. Settings from --dns will override ones --dhcp-option where applicable.
For detailed information about the option consult the documentation in this commit.
--- doc/man-sections/client-options.rst | 55 +++ doc/man-sections/script-options.rst | 19 + doc/man-sections/server-options.rst | 2 +- src/openvpn/Makefile.am | 1 + src/openvpn/dns.c | 561 ++++++++++++++++++++++++++++ src/openvpn/dns.h | 89 +++++ src/openvpn/openvpn.vcxproj | 4 +- src/openvpn/openvpn.vcxproj.filters | 8 +- src/openvpn/options.c | 221 +++++++++++ src/openvpn/options.h | 7 + src/openvpn/push.c | 4 + src/openvpn/socket.c | 11 + src/openvpn/socket.h | 2 + 13 files changed, 981 insertions(+), 3 deletions(-) create mode 100644 src/openvpn/dns.c create mode 100644 src/openvpn/dns.h
doc/man-sections/client-options.rst +55 -0
@@ -154,6 +154,61 @@ configuration. --connect-timeout n See ``--server-poll-timeout``. +--dns args + Client DNS configuration to be used with the connection. + + Valid syntaxes: + :: + + dns search-domains domain [domain ...] + dns server n address addr[:port] [addr[:port]] + dns server n resolve-domains|exclude-domains domain [domain ...] + dns server n dnssec yes|optional|no + dns server n transport DoH|DoT|plain + dns server n sni server-name + + The ``--dns search-domains`` directive takes one or more domain names + to be added as DNS domain suffixes. If it is repeated multiple times within + a configuration the domains are appended, thus e.g. domain names pushed by + a server will amend locally defined ones. + + The ``--dns server`` directive is used to configure DNS server ``n``. + The server id ``n`` must be a value between -128 and 127. For pushed + DNS server options it must be between 0 and 127. The server id is used + to group options and also for ordering the list of configured DNS servers; + lower numbers come first. DNS servers being pushed to a client replace + already configured DNS servers with the same server id. + + The ``address`` option configures the IPv4 and / or IPv6 address of + the DNS server. Optionally a port can be appended after a colon. IPv6 + addresses need to be enclosed in brackets if a port is appended. + + The ``resolve-domains`` and ``exclude-domains`` options take one or + more DNS domains which are explicitly resolved or explicitly not resolved + by a server. Only one of the options can be configured for a server. + ``resolve-domains`` is used to define a split-dns setup, where only + given domains are resolved by a server. ``exclude-domains`` is used to + define domains which will never be resolved by a server (e.g. domains + which can only be resolved locally). Systems which do not support fine + grained DNS domain configuration, will ignore these settings. + + The ``dnssec`` option is used to configure validation of DNSSEC records. + While the exact semantics may differ for resolvers on different systems, + ``yes`` likely makes validation mandatory, ``no`` disables it, and ``optional`` + uses it opportunistically. + + The ``transport`` option enables DNS-over-HTTPS (``DoH``) or DNS-over-TLS (``DoT``) + for a DNS server. The ``sni`` option can be used with them to specify the + ``server-name`` for TLS server name indication. + + Each server has to have at least one address configured for a configuration + to be valid. All the other options can be omitted. + + The ``--dns`` option will eventually obsolete the ``--dhcp-option`` directive. + Until then it will add configuration at the places ``--dhcp-option`` puts it + and will override it if both are given. So, ``--dns`` can be used today to + migrate from ``--dhcp-option``. + --explicit-exit-notify n In UDP client mode or point-to-point mode, send server/peer an exit notification if tunnel is restarted or OpenVPN process is exited. In
doc/man-sections/script-options.rst +19 -0
@@ -586,6 +586,25 @@ instances. netsh.exe calls which sometimes just do not work right with interface names). Set prior to ``--up`` or ``--down`` script execution. +:code:`dns_*` + The ``--dns`` configuration options will be made available to script + execution through this set of environment variables. Variables appear + only if the corresponding option has a value assigned. For the semantics + of each individual variable, please refer to the documentation for ``--dns``. + + :: + + dns_search_domain_{n} + dns_server_{n}_address4 + dns_server_{n}_port4 + dns_server_{n}_address6 + dns_server_{n}_port6 + dns_server_{n}_resolve_domain_{m} + dns_server_{n}_exclude_domain_{m} + dns_server_{n}_dnssec + dns_server_{n}_transport + dns_server_{n}_sni + :code:`foreign_option_{n}` An option pushed via ``--push`` to a client which does not natively support it, such as ``--dhcp-option`` on a non-Windows system, will be
doc/man-sections/server-options.rst +1 -1
@@ -412,7 +412,7 @@ fast hardware. SSL/TLS authentication must be used in this mode. This is a partial list of options which can currently be pushed: ``--route``, ``--route-gateway``, ``--route-delay``, - ``--redirect-gateway``, ``--ip-win32``, ``--dhcp-option``, + ``--redirect-gateway``, ``--ip-win32``, ``--dhcp-option``, ``--dns``, ``--inactive``, ``--ping``, ``--ping-exit``, ``--ping-restart``, ``--setenv``, ``--auth-token``, ``--persist-key``, ``--persist-tun``, ``--echo``, ``--comp-lzo``, ``--socket-flags``, ``--sndbuf``,
src/openvpn/Makefile.am +1 -0
@@ -54,6 +54,7 @@ openvpn_SOURCES = \ crypto_openssl.c crypto_openssl.h \ crypto_mbedtls.c crypto_mbedtls.h \ dhcp.c dhcp.h \ + dns.c dns.h \ env_set.c env_set.h \ errlevel.h \ error.c error.h \
src/openvpn/dns.c new file +561 -0
561 lines not on this page · raw message
src/openvpn/dns.h new file +89 -0
89 lines not on this page · raw message
src/openvpn/openvpn.vcxproj +3 -1
@@ -255,6 +255,7 @@ <ClCompile Include="cryptoapi.c" /> <ClCompile Include="env_set.c" /> <ClCompile Include="dhcp.c" /> + <ClCompile Include="dns.c" /> <ClCompile Include="error.c" /> <ClCompile Include="event.c" /> <ClCompile Include="fdmisc.c" />
@@ -336,6 +337,7 @@ <ClInclude Include="crypto_openssl.h" /> <ClInclude Include="cryptoapi.h" /> <ClInclude Include="dhcp.h" /> + <ClInclude Include="dns.h" /> <ClInclude Include="env_set.h" /> <ClInclude Include="errlevel.h" /> <ClInclude Include="error.h" />
@@ -427,4 +429,4 @@ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> </ImportGroup> -</Project> +</Project>
src/openvpn/openvpn.vcxproj.filters +7 -1
@@ -39,6 +39,9 @@ <ClCompile Include="dhcp.c"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="dns.c"> + <Filter>Source Files</Filter> + </ClCompile> <ClCompile Include="error.c"> <Filter>Source Files</Filter> </ClCompile>
@@ -290,6 +293,9 @@ <ClInclude Include="dhcp.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="dns.h"> + <Filter>Header Files</Filter> + </ClInclude> <ClInclude Include="errlevel.h"> <Filter>Header Files</Filter> </ClInclude>
@@ -526,4 +532,4 @@ <Filter>Resource Files</Filter> </Manifest> </ItemGroup> -</Project> +</Project>
src/openvpn/options.c +221 -0
293 lines not on this page · raw message
src/openvpn/options.h +7 -0
@@ -42,6 +42,7 @@#include "pushlist.h" #include "clinat.h" #include "crypto_backend.h" +#include "dns.h" /*
@@ -76,6 +77,8 @@ struct options_pre_connectbool client_nat_defined;struct client_nat_option_list *client_nat; +struct dns_options dns_options; +const char * ciphername;const char * authname;
@@ -272,6 +275,8 @@ struct optionsstruct remote_host_store *rh_store; +struct dns_options dns_options; +bool remote_random;const char *ipchange;const char *dev;
@@ -802,6 +807,8 @@ char *options_string_extract_option(const char *options_string,void options_postprocess (struct options *options); +bool options_postprocess_pull (struct options *o,struct env_set *es); +void pre_connect_save (struct options *o);void pre_connect_restore (struct options *o,struct gc_arena *gc);
src/openvpn/push.c +4 -0
@@ -459,6 +459,10 @@ incoming_push_message(struct context *c, const struct buffer *buffer)/* delay bringing tun/tap up until --push parms received from remote */ if (status ==PUSH_MSG_REPLY ) { +if (!options_postprocess_pull (&c->options, c->c2.es)) + { + goto error; + }if (!do_up (c, true, c->options.push_option_types_found)) {msg (D_PUSH_ERRORS ,"Failed to open tun/tap interface" );
src/openvpn/socket.c +11 -0
@@ -2906,6 +2906,17 @@ print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)return BSTR (&out); } +/* +* Convert an in_port_t in host byte order to a string +*/ +const char * +print_in_port_t (in_port_t port,struct gc_arena *gc) +{ +struct buffer buffer =alloc_buf_gc (8 , gc); +buf_printf (&buffer,"%hu" , port); +return BSTR (&buffer); +} +#ifndef UINT8_MAX #define UINT8_MAX 0xff#endif
src/openvpn/socket.h +2 -0
@@ -360,6 +360,8 @@ const char *print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arenaconst char *print_in6_addr (struct in6_addr addr6,unsigned int flags,struct gc_arena *gc); +const char *print_in_port_t (in_port_t port,struct gc_arena *gc); +struct in6_addr add_in6_addr (struct in6_addr base,uint32_t add );#define SA_IP_PORT (1<<0)
Signature · 1 line
2.32.0
I have reviewed this on Github already and had no further issues.
Quoted 8 lines
Heiko Hund <[email protected]...> hat am 09.03.2022 00:06 geschrieben:
As a first step towards DNS configuration in openvpn and a unified way to push DNS related settings to clients in v2 and v3, this commit adds support for parsing the new --dns option. Later commits will add support for setting up DNS on different platforms.
Quoted 8 lines
For now, --dns and DNS related --dhcp-option can be used together for smoother transition. Settings from --dns will override ones --dhcp-option where applicable.
For detailed information about the option consult the documentation in this commit.
Signed-off-by Heiko Hund <[email protected]...>
Grüße,
--
Frank Lichtenheld
Am 09.03.22 um 00:06 schrieb Heiko Hund:
src/openvpn/dns.h line 87
Quoted 11 lines
src/openvpn/dns.h new file +11
-0+ +bool dns_server_priority_parse (long *priority,const char *str,bool pulled); +struct dns_server *dns_server_get (struct dns_server **entry,long priority,struct gc_arena *gc); +void dns_domain_list_append (struct dns_domain **entry,char **domains,struct gc_arena *gc); +bool dns_server_addr_parse (struct dns_server *server,const char *addr); +bool dns_options_verify (int msglevel,const struct dns_options *o); +struct dns_options clone_dns_options (const struct dns_options o,struct gc_arena *gc); +void dns_options_preprocess_pull (struct dns_options *o); +void dns_options_postprocess_pull (struct dns_options *o); +void setenv_dns_options (const struct dns_options *o,struct env_set *es); +void show_dns_options (const struct dns_options *o);
These new functions are missing doxygen comments. We generally have the doxygen comments in the header files rather than in the c files.
Quoted 2 lines
+--dns args + Client DNS configuration to be used with the connection.
This man page section sounds like all options are supported on all platforms while currently that is not the case. An explicit warning etc that says that some options like port, dnssec, doh etc are not supported on certain platforms should be included and also what happens if they are not.
Review
Quoted 3 lines
The ``--dns`` option will eventually obsolete the ``--dhcp-option`` directive.
(quoted diff) +2
-0+ Until then it will add configuration at the places ``--dhcp-option`` puts it + and will override it if both are given.
Is dhcp-option ignored when dns is present by newer client or are they mixed? This is a bit vague.
Quoted 1 line
+ long priority;
why a type that is 32 bit on some platforms and 64 bit on others? Either go with just int if 32 bit is enough or use long long or int64_t if we need 64 but long is a weird type.
Quoted 1 line
+
name_ok&=openvpn_snprintf(env_name, sizeof(env_name), "dns_server_%d_address4", i);
bitwise and with a bool feels wrong. We should use && instead of & for boolean logic.
src/openvpn/dns.h line 69
Quoted 4 lines
src/openvpn/dns.h new file +4
-0+enum dns_security dnssec; +enum dns_server_transport transport; +char *sni; +};
That should be probably be a const char* instead of a modifable char*
Quoted 2 lines
gc_free(&o->gc);+
gc_free(&o->dns_options.gc);
having an extra gc in this way feels a bit strange. Since this is the only time it is initialised you can just as well just use o->gc instead.
Quoted 1 line
+ struct
gc_arenadns_gc=gc_new();;
extra ;
src/openvpn/options.c pre_connect_restore(struct options *o, struct gc_arena *gc) · line 3850
Quoted 2 lines
src/openvpn/options.c +2
-0+/* Free DNS options and reset them to pre-pull state */ +gc_free (&o->dns_options.gc);
There is an additional free for the gc here that has no matching gc_init. If your goal is to have a gc for the life time of prepull etc, then it is better to put it there as that is easier to see its lifetime than to have it only for DNS where the lifetime/ownership is not so clear.
I see also that this code makes no attempt to remove/refactor the old DHCP code. The only thing I see is the tuntap_options_copy_dns code. What the supposed way forward here? That also raises the question about platform specific code. Does that code now need two implementations? One for installing options from the dhcp structs and one from the DNS struct?
Some idea how this is going to go forward would be good.
Arne
Quoted 7 lines · On Mittwoch, 9. März 2022 13:40:32 CET Arne Schwabe wrote:
Am 09.03.22 um 00:06 schrieb Heiko Hund:
Review
Quoted 3 lines
(quoted diff) +3
-0+bool dns_server_priority_parse(long *priority, const char *str, bool +[...] +void show_dns_options(const struct dns_options *o);These new functions are missing doxygen comments. We generally have the doxygen comments in the header files rather than in the c files.
Okay, I'll move the comments into the header file.
Quoted 8 lines
Quoted 2 lines
+--dns args + Client DNS configuration to be used with the connection.This man page section sounds like all options are supported on all platforms while currently that is not the case. An explicit warning etc that says that some options like port, dnssec, doh etc are not supported on certain platforms should be included and also what happens if they are not.
Noted. What happens will likely be determined when we have the implementation(s) that actually take this option and try to apply it. Think it is too early to add this info to the manual, as it may change. Some ideas exist, but they have to prove themselves and survive public discussion. So far I have the idea that options will be ignored if they are not supported, and split DNS is turned into all DNS redirected to the pushed server(s) if they are unsupported. Again, this is very theoretical at this point and may change once facing reality.
Quoted 7 lines
Quoted 4 lines
The ``--dns`` option will eventually obsolete the ``--dhcp-option`` directive.>
+ Until then it will add configuration at the places ``--dhcp-option`` puts it + and will override it if both are given.Is dhcp-option ignored when dns is present by newer client or are they mixed? This is a bit vague.
Will clarify.
Quoted 5 lines
Quoted 1 line
+ long priority;
why a type that is 32 bit on some platforms and 64 bit on others? Either go with just int if 32 bit is enough or use long long or
int64_tif we need 64 but long is a weird type.
int8_t would be enough, just need an extra few bits for the conversation from text. It is a long because strtol(3) returns a long, and I went with that. Not sure if casting after checks will gain much. If we decide to change this I'd rather use int8_t however, or a future somebody will ponder why the extra bits there, when the priority is really a 8 bit value. IMHO it is not worth the extra code, that's why I went with long. Compilers might align an 8 bit value anyways.
Quoted 4 lines
Quoted 2 lines
+
name_ok&= openvpn_snprintf(env_name,sizeof(env_name),
"dns_server_%d_address4", i);bitwise and with a bool feels wrong. We should use && instead of & for boolean logic.
Since there is no &&= operator, I thought it's more readable with &=. The result is the same because of the way bool is defined. I'll convert it to logical operators for the v2 and then we'll see if it looks and feels better.
Quoted 6 lines
src/openvpn/dns.h line 69
Quoted 4 lines
src/openvpn/dns.h new file +4
-0+enum dns_security dnssec; +enum dns_server_transport transport; +char *sni; +};That should be probably be a const char* instead of a modifable char*
Right.
Quoted 6 lines
Quoted 3 lines
gc_free(&o->gc);+
gc_free(&o->dns_options.gc);having an extra gc in this way feels a bit strange. Since this is the only time it is initialised you can just as well just use
o->gcinstead.
Actually no. The --dns option is a bit special as it can partially replace local setting with pushed ones (i.e. override single dns servers and others not). The contained gc is used to allocate (and free) pulled options. If we'd use o->gc as well, the pulled options wouldn't be freed for a potentially long time, increasing the memory footprint over time.
Quoted 3 lines
Quoted 1 line
+ struct
gc_arenadns_gc=gc_new();;extra ;
Eagle eyes! =)
Quoted 7 lines
src/openvpn/options.c pre_connect_restore(struct options *o, struct gc_arena *gc) · line 3850
Quoted 2 lines
src/openvpn/options.c +2
-0+/* Free DNS options and reset them to pre-pull state */ +gc_free (&o->dns_options.gc);There is an additional free for the gc here that has no matching
gc_init. If your goal is to have a gc for the life time of prepull etc, then it is better to put it there as that is easier to see its lifetime than to have it only for DNS where the lifetime/ownership is not so clear.
The accompanying gc_init() is with the one for o->gc. Don't see how moving the gc into struct options_pre_connect would change anything substantially. The gc_init would still be done in init_options() then.
Quoted 7 lines
I see also that this code makes no attempt to remove/refactor the old DHCP code. The only thing I see is the
tuntap_options_copy_dnscode. What the supposed way forward here? That also raises the question about platform specific code. Does that code now need two implementations? One for installing options from the dhcp structs and one from the DNS struct?Some idea how this is going to go forward would be good.
There are several ideas. Instead of having a #ifdef hell, like for other platform specific code, I would prefer some sort of "dns plugin / script" way, where platform specific code could reside in individual units and linked / called on a on-demand basis. However I want the community to discuss and bless an approach before implementing it. Later. For now this is the first step to get openvpn 2 and 3 on a common basis for (pushing) dns options. dhcp-options will fade then, once we have proper --dns code in. If it turns out some of the dhcp-options are worthwhile keeping, I think it is best to introduce new specialized (Windows) options to handle them.
Cheers,
Heiko
PATCH 1/1 add support for --dns option
In reply to Heiko HundAs a first step towards DNS configuration in openvpn and a unified way to push DNS related settings to clients in v2 and v3, this commit adds support for parsing the new --dns option. Later commits will add support for setting up DNS on different platforms.
For now, --dns and DNS related --dhcp-option can be used together for smoother transition. Settings from --dns will override ones --dhcp-option where applicable.
For detailed information about the option consult the documentation in this commit.
--- doc/man-sections/client-options.rst | 59 ++++ doc/man-sections/script-options.rst | 19 ++ doc/man-sections/server-options.rst | 2 +- src/openvpn/Makefile.am | 1 + src/openvpn/dns.c | 495 ++++++++++++++++++++++++++++ src/openvpn/dns.h | 164 +++++++++ src/openvpn/openvpn.vcxproj | 4 +- src/openvpn/openvpn.vcxproj.filters | 8 +- src/openvpn/options.c | 221 +++++++++++++ src/openvpn/options.h | 7 + src/openvpn/push.c | 4 + src/openvpn/socket.c | 11 + src/openvpn/socket.h | 2 + 13 files changed, 994 insertions(+), 3 deletions(-) create mode 100644 src/openvpn/dns.c create mode 100644 src/openvpn/dns.h
doc/man-sections/client-options.rst +59 -0
@@ -154,6 +154,65 @@ configuration. --connect-timeout n See ``--server-poll-timeout``. +--dns args + Client DNS configuration to be used with the connection. + + Valid syntaxes: + :: + + dns search-domains domain [domain ...] + dns server n address addr[:port] [addr[:port]] + dns server n resolve-domains|exclude-domains domain [domain ...] + dns server n dnssec yes|optional|no + dns server n transport DoH|DoT|plain + dns server n sni server-name + + The ``--dns search-domains`` directive takes one or more domain names + to be added as DNS domain suffixes. If it is repeated multiple times within + a configuration the domains are appended, thus e.g. domain names pushed by + a server will amend locally defined ones. + + The ``--dns server`` directive is used to configure DNS server ``n``. + The server id ``n`` must be a value between -128 and 127. For pushed + DNS server options it must be between 0 and 127. The server id is used + to group options and also for ordering the list of configured DNS servers; + lower numbers come first. DNS servers being pushed to a client replace + already configured DNS servers with the same server id. + + The ``address`` option configures the IPv4 and / or IPv6 address of + the DNS server. Optionally a port can be appended after a colon. IPv6 + addresses need to be enclosed in brackets if a port is appended. + + The ``resolve-domains`` and ``exclude-domains`` options take one or + more DNS domains which are explicitly resolved or explicitly not resolved + by a server. Only one of the options can be configured for a server. + ``resolve-domains`` is used to define a split-dns setup, where only + given domains are resolved by a server. ``exclude-domains`` is used to + define domains which will never be resolved by a server (e.g. domains + which can only be resolved locally). Systems which do not support fine + grained DNS domain configuration, will ignore these settings. + + The ``dnssec`` option is used to configure validation of DNSSEC records. + While the exact semantics may differ for resolvers on different systems, + ``yes`` likely makes validation mandatory, ``no`` disables it, and ``optional`` + uses it opportunistically. + + The ``transport`` option enables DNS-over-HTTPS (``DoH``) or DNS-over-TLS (``DoT``) + for a DNS server. The ``sni`` option can be used with them to specify the + ``server-name`` for TLS server name indication. + + Each server has to have at least one address configured for a configuration + to be valid. All the other options can be omitted. + + Note that not all options may be supported on all platforms. As soon support + for different systems is implemented, information will be added here how + unsupported options are treated. + + The ``--dns`` option will eventually obsolete the ``--dhcp-option`` directive. + Until then it will replace configuration at the places ``--dhcp-option`` puts it, + so that ``--dns`` overrides ``--dhcp-option``. Thus, ``--dns`` can be used today + to migrate from ``--dhcp-option``. + --explicit-exit-notify n In UDP client mode or point-to-point mode, send server/peer an exit notification if tunnel is restarted or OpenVPN process is exited. In
doc/man-sections/script-options.rst +19 -0
@@ -586,6 +586,25 @@ instances. netsh.exe calls which sometimes just do not work right with interface names). Set prior to ``--up`` or ``--down`` script execution. +:code:`dns_*` + The ``--dns`` configuration options will be made available to script + execution through this set of environment variables. Variables appear + only if the corresponding option has a value assigned. For the semantics + of each individual variable, please refer to the documentation for ``--dns``. + + :: + + dns_search_domain_{n} + dns_server_{n}_address4 + dns_server_{n}_port4 + dns_server_{n}_address6 + dns_server_{n}_port6 + dns_server_{n}_resolve_domain_{m} + dns_server_{n}_exclude_domain_{m} + dns_server_{n}_dnssec + dns_server_{n}_transport + dns_server_{n}_sni + :code:`foreign_option_{n}` An option pushed via ``--push`` to a client which does not natively support it, such as ``--dhcp-option`` on a non-Windows system, will be
doc/man-sections/server-options.rst +1 -1
@@ -412,7 +412,7 @@ fast hardware. SSL/TLS authentication must be used in this mode. This is a partial list of options which can currently be pushed: ``--route``, ``--route-gateway``, ``--route-delay``, - ``--redirect-gateway``, ``--ip-win32``, ``--dhcp-option``, + ``--redirect-gateway``, ``--ip-win32``, ``--dhcp-option``, ``--dns``, ``--inactive``, ``--ping``, ``--ping-exit``, ``--ping-restart``, ``--setenv``, ``--auth-token``, ``--persist-key``, ``--persist-tun``, ``--echo``, ``--comp-lzo``, ``--socket-flags``, ``--sndbuf``,
src/openvpn/Makefile.am +1 -0
@@ -54,6 +54,7 @@ openvpn_SOURCES = \ crypto_openssl.c crypto_openssl.h \ crypto_mbedtls.c crypto_mbedtls.h \ dhcp.c dhcp.h \ + dns.c dns.h \ env_set.c env_set.h \ errlevel.h \ error.c error.h \
src/openvpn/dns.c new file +495 -0
495 lines not on this page · raw message
src/openvpn/dns.h new file +164 -0
164 lines not on this page · raw message
src/openvpn/openvpn.vcxproj +3 -1
@@ -255,6 +255,7 @@ <ClCompile Include="cryptoapi.c" /> <ClCompile Include="env_set.c" /> <ClCompile Include="dhcp.c" /> + <ClCompile Include="dns.c" /> <ClCompile Include="error.c" /> <ClCompile Include="event.c" /> <ClCompile Include="fdmisc.c" />
@@ -336,6 +337,7 @@ <ClInclude Include="crypto_openssl.h" /> <ClInclude Include="cryptoapi.h" /> <ClInclude Include="dhcp.h" /> + <ClInclude Include="dns.h" /> <ClInclude Include="env_set.h" /> <ClInclude Include="errlevel.h" /> <ClInclude Include="error.h" />
@@ -427,4 +429,4 @@ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> </ImportGroup> -</Project> +</Project>
src/openvpn/openvpn.vcxproj.filters +7 -1
@@ -39,6 +39,9 @@ <ClCompile Include="dhcp.c"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="dns.c"> + <Filter>Source Files</Filter> + </ClCompile> <ClCompile Include="error.c"> <Filter>Source Files</Filter> </ClCompile>
@@ -290,6 +293,9 @@ <ClInclude Include="dhcp.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="dns.h"> + <Filter>Header Files</Filter> + </ClInclude> <ClInclude Include="errlevel.h"> <Filter>Header Files</Filter> </ClInclude>
@@ -526,4 +532,4 @@ <Filter>Resource Files</Filter> </Manifest> </ItemGroup> -</Project> +</Project>
src/openvpn/options.c +221 -0
293 lines not on this page · raw message
src/openvpn/options.h +7 -0
@@ -42,6 +42,7 @@#include "pushlist.h" #include "clinat.h" #include "crypto_backend.h" +#include "dns.h" /*
@@ -76,6 +77,8 @@ struct options_pre_connectbool client_nat_defined;struct client_nat_option_list *client_nat; +struct dns_options dns_options; +const char * ciphername;const char * authname;
@@ -272,6 +275,8 @@ struct optionsstruct remote_host_store *rh_store; +struct dns_options dns_options; +bool remote_random;const char *ipchange;const char *dev;
@@ -802,6 +807,8 @@ char *options_string_extract_option(const char *options_string,void options_postprocess (struct options *options); +bool options_postprocess_pull (struct options *o,struct env_set *es); +void pre_connect_save (struct options *o);void pre_connect_restore (struct options *o,struct gc_arena *gc);
src/openvpn/push.c +4 -0
@@ -459,6 +459,10 @@ incoming_push_message(struct context *c, const struct buffer *buffer)/* delay bringing tun/tap up until --push parms received from remote */ if (status ==PUSH_MSG_REPLY ) { +if (!options_postprocess_pull (&c->options, c->c2.es)) + { + goto error; + }if (!do_up (c, true, c->options.push_option_types_found)) {msg (D_PUSH_ERRORS ,"Failed to open tun/tap interface" );
src/openvpn/socket.c +11 -0
@@ -2906,6 +2906,17 @@ print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)return BSTR (&out); } +/* +* Convert an in_port_t in host byte order to a string +*/ +const char * +print_in_port_t (in_port_t port,struct gc_arena *gc) +{ +struct buffer buffer =alloc_buf_gc (8 , gc); +buf_printf (&buffer,"%hu" , port); +return BSTR (&buffer); +} +#ifndef UINT8_MAX #define UINT8_MAX 0xff#endif
src/openvpn/socket.h +2 -0
@@ -360,6 +360,8 @@ const char *print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arenaconst char *print_in6_addr (struct in6_addr addr6,unsigned int flags,struct gc_arena *gc); +const char *print_in_port_t (in_port_t port,struct gc_arena *gc); +struct in6_addr add_in6_addr (struct in6_addr base,uint32_t add );#define SA_IP_PORT (1<<0)
Signature · 1 line
2.32.0
Sorry, this is [PATCH v2].
Somehow I managed for send-email to override my --subject.
Quoted 1 line · On Samstag, 12. März 2022 14:58:10 CET Heiko Hund wrote:
+
name_ok=openvpn_snprintf(env_name, sizeof(env_name), "dns_search_domain_%d", i)&&name_ok;
With some distance, I still like the &= version better from a readability standpoint. Even though it's a bit unclean.
Heiko
Hi,
(this is about v2)
Quoted 11 lines · Heiko Hund
As a first step towards DNS configuration in openvpn and a unified way to push DNS related settings to clients in v2 and v3, this commit adds support for parsing the new --dns option. Later commits will add support for setting up DNS on different platforms.
For now, --dns and DNS related --dhcp-option can be used together for smoother transition. Settings from --dns will override ones --dhcp-option where applicable.
For detailed information about the option consult the documentation in this commit.
[..]
src/openvpn/dns.c line 347
Quoted 5 lines
src/openvpn/dns.c new file +5
-0+for (i =1 , d = o->search_domains; d !=NULL ; i++, d = d->next) + { + name_ok =openvpn_snprintf (env_name,sizeof (env_name),"dns_search_domain_%d" , i) && name_ok; +setenv_str (es, env_name, d->name); + }
With the "&& name_ok" this code does not really improve, even if it was requested in the v1 review...
I wonder why we bother to actually *do* this? As in "we already know that this can never overflow here" (because all strings involved are known, and the max width of %d is known, too), but *if* it ever did, calling the subsequent setenv_str() wouldn't be a good idea to do - no?
Can we make transform these into a miniwrapper that does
- openvpn_snprintf()
- check result, warn if overflow
- call setenv_str() otherwise
like
for (i = 1, d = o->search_domains; d != NULL; i++, d = d->next)
{
_do_dns_env_thing( "dns_search_domain_%d", i, d->name );
}so, shorter lines, clearer code, actual error checking, and no && ugliness :-)
(As far as I could see, it's all "setenv_str()" anyway)
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]...Hi Gert
Quoted 4 lines · On Donnerstag, 17. März 2022 11:41:22 CET Gert Doering wrote:
I wonder why we bother to actually *do* this? As in "we already know that this can never overflow here" (because all strings involved are known, and the max width of %d is known, too), but *if* it ever did, calling the subsequent
setenv_str()wouldn't be a good idea to do - no?
It's in the spirit of defensive programming. It cannot overflow now, but will likely overflow some time. However I thought that getting the value into env might still be worthwhile in some scenarios, even if the name is not complete, e.g. if the env is logged somewhere. The error message in the openvpn log, will lead to cause then.
Quoted 7 lines
Can we make transform these into a miniwrapper that does like
for (i = 1, d = o->search_domains; d != NULL; i++, d = d->next) { _do_dns_env_thing( "dns_search_domain_%d", i, d->name ); }
I actually had such a wrapper in place, but dumped it because I didn't like the fact that the value would have to come before the name in the argument list, because of the var-args. Since we have a format string with two %d (and there might be more / other formatting specifiers in the future), the wrapper would have to deal with ..., or we'd end up with two wrappers (or more if this is extended in the future). No matter what:
Quoted 2 lines
const char* format = s->domain_type == DNS_RESOLVE_DOMAINS ?
"dns_server_%d_resolve_domain_%d" : "dns_server_%d_exclude_domain_%d";requires it to be:
_do_dns_env_thing(d->name, "dns_server_%d_resolve__domain_%d", i, j);
right now already. And that lead me to not making things wrapped.
However I could be talked into removing the checks for openvpn_snprintf()'s return value if there's a consensus, as it wouldn't introduce an actual issue. Just maybe a little harder to debug issues in future code possibly. Also not setting the env if sprintf fails sounds good, would do this rather:
if (openvpn_snprinf())
setenv_str();
else
name_ok = false;Iff the value is really not useful in the env, when the name is incomplete. Bonus: no &=.
Heiko
PATCH v3 1/1 add support for --dns option
In reply to Heiko HundAs a first step towards DNS configuration in openvpn and a unified way to push DNS related settings to clients in v2 and v3, this commit adds support for parsing the new --dns option. Later commits will add support for setting up DNS on different platforms.
For now, --dns and DNS related --dhcp-option can be used together for smoother transition. Settings from --dns will override ones --dhcp-option where applicable.
For detailed information about the option consult the documentation in this commit.
--- doc/man-sections/client-options.rst | 59 ++++ doc/man-sections/script-options.rst | 19 ++ doc/man-sections/server-options.rst | 2 +- src/openvpn/Makefile.am | 1 + src/openvpn/dns.c | 510 ++++++++++++++++++++++++++++ src/openvpn/dns.h | 164 +++++++++ src/openvpn/openvpn.vcxproj | 4 +- src/openvpn/openvpn.vcxproj.filters | 8 +- src/openvpn/options.c | 221 ++++++++++++ src/openvpn/options.h | 7 + src/openvpn/push.c | 4 + src/openvpn/socket.c | 11 + src/openvpn/socket.h | 2 + 13 files changed, 1009 insertions(+), 3 deletions(-) create mode 100644 src/openvpn/dns.c create mode 100644 src/openvpn/dns.h
doc/man-sections/client-options.rst +59 -0
@@ -154,6 +154,65 @@ configuration. --connect-timeout n See ``--server-poll-timeout``. +--dns args + Client DNS configuration to be used with the connection. + + Valid syntaxes: + :: + + dns search-domains domain [domain ...] + dns server n address addr[:port] [addr[:port]] + dns server n resolve-domains|exclude-domains domain [domain ...] + dns server n dnssec yes|optional|no + dns server n transport DoH|DoT|plain + dns server n sni server-name + + The ``--dns search-domains`` directive takes one or more domain names + to be added as DNS domain suffixes. If it is repeated multiple times within + a configuration the domains are appended, thus e.g. domain names pushed by + a server will amend locally defined ones. + + The ``--dns server`` directive is used to configure DNS server ``n``. + The server id ``n`` must be a value between -128 and 127. For pushed + DNS server options it must be between 0 and 127. The server id is used + to group options and also for ordering the list of configured DNS servers; + lower numbers come first. DNS servers being pushed to a client replace + already configured DNS servers with the same server id. + + The ``address`` option configures the IPv4 and / or IPv6 address of + the DNS server. Optionally a port can be appended after a colon. IPv6 + addresses need to be enclosed in brackets if a port is appended. + + The ``resolve-domains`` and ``exclude-domains`` options take one or + more DNS domains which are explicitly resolved or explicitly not resolved + by a server. Only one of the options can be configured for a server. + ``resolve-domains`` is used to define a split-dns setup, where only + given domains are resolved by a server. ``exclude-domains`` is used to + define domains which will never be resolved by a server (e.g. domains + which can only be resolved locally). Systems which do not support fine + grained DNS domain configuration, will ignore these settings. + + The ``dnssec`` option is used to configure validation of DNSSEC records. + While the exact semantics may differ for resolvers on different systems, + ``yes`` likely makes validation mandatory, ``no`` disables it, and ``optional`` + uses it opportunistically. + + The ``transport`` option enables DNS-over-HTTPS (``DoH``) or DNS-over-TLS (``DoT``) + for a DNS server. The ``sni`` option can be used with them to specify the + ``server-name`` for TLS server name indication. + + Each server has to have at least one address configured for a configuration + to be valid. All the other options can be omitted. + + Note that not all options may be supported on all platforms. As soon support + for different systems is implemented, information will be added here how + unsupported options are treated. + + The ``--dns`` option will eventually obsolete the ``--dhcp-option`` directive. + Until then it will replace configuration at the places ``--dhcp-option`` puts it, + so that ``--dns`` overrides ``--dhcp-option``. Thus, ``--dns`` can be used today + to migrate from ``--dhcp-option``. + --explicit-exit-notify n In UDP client mode or point-to-point mode, send server/peer an exit notification if tunnel is restarted or OpenVPN process is exited. In
doc/man-sections/script-options.rst +19 -0
@@ -588,6 +588,25 @@ instances. netsh.exe calls which sometimes just do not work right with interface names). Set prior to ``--up`` or ``--down`` script execution. +:code:`dns_*` + The ``--dns`` configuration options will be made available to script + execution through this set of environment variables. Variables appear + only if the corresponding option has a value assigned. For the semantics + of each individual variable, please refer to the documentation for ``--dns``. + + :: + + dns_search_domain_{n} + dns_server_{n}_address4 + dns_server_{n}_port4 + dns_server_{n}_address6 + dns_server_{n}_port6 + dns_server_{n}_resolve_domain_{m} + dns_server_{n}_exclude_domain_{m} + dns_server_{n}_dnssec + dns_server_{n}_transport + dns_server_{n}_sni + :code:`foreign_option_{n}` An option pushed via ``--push`` to a client which does not natively support it, such as ``--dhcp-option`` on a non-Windows system, will be
doc/man-sections/server-options.rst +1 -1
@@ -412,7 +412,7 @@ fast hardware. SSL/TLS authentication must be used in this mode. This is a partial list of options which can currently be pushed: ``--route``, ``--route-gateway``, ``--route-delay``, - ``--redirect-gateway``, ``--ip-win32``, ``--dhcp-option``, + ``--redirect-gateway``, ``--ip-win32``, ``--dhcp-option``, ``--dns``, ``--inactive``, ``--ping``, ``--ping-exit``, ``--ping-restart``, ``--setenv``, ``--auth-token``, ``--persist-key``, ``--persist-tun``, ``--echo``, ``--comp-lzo``, ``--socket-flags``, ``--sndbuf``,
src/openvpn/Makefile.am +1 -0
@@ -54,6 +54,7 @@ openvpn_SOURCES = \ crypto_openssl.c crypto_openssl.h \ crypto_mbedtls.c crypto_mbedtls.h \ dhcp.c dhcp.h \ + dns.c dns.h \ env_set.c env_set.h \ errlevel.h \ error.c error.h \
src/openvpn/dns.c new file +510 -0
510 lines not on this page · raw message
src/openvpn/dns.h new file +164 -0
164 lines not on this page · raw message
src/openvpn/openvpn.vcxproj +3 -1
@@ -269,6 +269,7 @@ <ClCompile Include="cryptoapi.c" /> <ClCompile Include="env_set.c" /> <ClCompile Include="dhcp.c" /> + <ClCompile Include="dns.c" /> <ClCompile Include="error.c" /> <ClCompile Include="event.c" /> <ClCompile Include="fdmisc.c" />
@@ -352,6 +353,7 @@ <ClInclude Include="crypto_openssl.h" /> <ClInclude Include="cryptoapi.h" /> <ClInclude Include="dhcp.h" /> + <ClInclude Include="dns.h" /> <ClInclude Include="env_set.h" /> <ClInclude Include="errlevel.h" /> <ClInclude Include="error.h" />
@@ -444,4 +446,4 @@ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> </ImportGroup> -</Project> +</Project>
src/openvpn/openvpn.vcxproj.filters +7 -1
@@ -39,6 +39,9 @@ <ClCompile Include="dhcp.c"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="dns.c"> + <Filter>Source Files</Filter> + </ClCompile> <ClCompile Include="error.c"> <Filter>Source Files</Filter> </ClCompile>
@@ -296,6 +299,9 @@ <ClInclude Include="dhcp.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="dns.h"> + <Filter>Header Files</Filter> + </ClInclude> <ClInclude Include="errlevel.h"> <Filter>Header Files</Filter> </ClInclude>
@@ -535,4 +541,4 @@ <Filter>Resource Files</Filter> </Manifest> </ItemGroup> -</Project> +</Project>
src/openvpn/options.c +221 -0
293 lines not on this page · raw message
src/openvpn/options.h +7 -0
@@ -42,6 +42,7 @@#include "pushlist.h" #include "clinat.h" #include "crypto_backend.h" +#include "dns.h" /*
@@ -76,6 +77,8 @@ struct options_pre_connectbool client_nat_defined;struct client_nat_option_list *client_nat; +struct dns_options dns_options; +const char * ciphername;const char * authname;
@@ -276,6 +279,8 @@ struct optionsstruct remote_host_store *rh_store; +struct dns_options dns_options; +bool remote_random;const char *ipchange;const char *dev;
@@ -806,6 +811,8 @@ char *options_string_extract_option(const char *options_string,void options_postprocess (struct options *options); +bool options_postprocess_pull (struct options *o,struct env_set *es); +void pre_connect_save (struct options *o);void pre_connect_restore (struct options *o,struct gc_arena *gc);
src/openvpn/push.c +4 -0
@@ -459,6 +459,10 @@ incoming_push_message(struct context *c, const struct buffer *buffer)/* delay bringing tun/tap up until --push parms received from remote */ if (status ==PUSH_MSG_REPLY ) { +if (!options_postprocess_pull (&c->options, c->c2.es)) + { + goto error; + }if (!do_up (c, true, c->options.push_option_types_found)) {msg (D_PUSH_ERRORS ,"Failed to open tun/tap interface" );
src/openvpn/socket.c +11 -0
@@ -2875,6 +2875,17 @@ print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)return BSTR (&out); } +/* +* Convert an in_port_t in host byte order to a string +*/ +const char * +print_in_port_t (in_port_t port,struct gc_arena *gc) +{ +struct buffer buffer =alloc_buf_gc (8 , gc); +buf_printf (&buffer,"%hu" , port); +return BSTR (&buffer); +} +#ifndef UINT8_MAX #define UINT8_MAX 0xff#endif
src/openvpn/socket.h +2 -0
@@ -389,6 +389,8 @@ const char *print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arenaconst char *print_in6_addr (struct in6_addr addr6,unsigned int flags,struct gc_arena *gc); +const char *print_in_port_t (in_port_t port,struct gc_arena *gc); +struct in6_addr add_in6_addr (struct in6_addr base,uint32_t add );#define SA_IP_PORT (1<<0)
Signature · 1 line
2.32.0
Quoted 14 lines · On Mittwoch, 23. März 2022 15:34:52 CET Heiko Hund wrote:
src/openvpn/dns.c new file +14
-0+static void +setenv_dns_option (struct env_set *es, +const char *format,int i,int j, +const char *value) +{ +char name[64 ]; +bool name_ok = false; + +if (j <0 ) + { + name_ok =openvpn_snprintf (name,sizeof (name), format, i); + } +else + {
src/openvpn/dns.c line 356
Quoted 10 lines
src/openvpn/dns.c new file +10
-0+ name_ok =openvpn_snprintf (name,sizeof (name), format, i, j); + } + +if (!name_ok) + { +msg (M_WARN ,"WARNING: dns option setenv name buffer overflow" ); + } + +setenv_str (es, name, value); +}
Here's the helper function Gert was asking for. It's somewhat special in how the 'j' parameter is handled, but since it's local and very specialized, I can live with that.
Regards, Heiko
Thanks for being patient with an old man and rewriting the &name_ok thing :-) - I like the current code much better.
For the original patch, I am relying on the ACK from Frank. I have tested the setenv bit (which is new) by feeding openvpn a few --dns options on the command line and then looking at the env passed to --up - and things matched what the documentation says it should be.
To see "--dns server 5 address 1.2.3.4" show up as "dns_server_2_address4=1.2.3.4" was a bit as a surprise first, but it makes sense when re-reading the documentation - servers will be sorted by preference, and then the output is just "1, 2, 3, ..." in contiguous numbering. Also, things are nicely output in the "print options" part of openvpn (show_dns_options()).
** NOTE: as discussed on IRC, uncrustify did have a few whitespace things ** to complain about, so I fixed those on-the-fly. No code change.
Your patch has been applied to the master branch.
commit b3e0d95dcfd0de2a5fe6545fed8f46e0dd35784d
Author: Heiko Hund Date: Wed Mar 23 15:34:52 2022 +0100
add support for --dns option
Signed-off-by: Heiko Hund <[email protected]...>
Acked-by: Frank Lichtenheld <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg23997.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering