Set DNS Domain using iservice
PATCH 1/1 Set DNS Domain using iservice
From: Selva Nair <[email protected]...>
Use wmic instead of directly editing the registry as the former does not take full effect unless the dns client service is restarted.
Editing the registry appears to work erratically depending on whether its followed with a dchp renew or ipconfig /registerdns etc.
DOMAIN-SEARCH is not handled here as wmic only supports setting the global search list which will over-ride all interface specific values. Editing the registry directly combined with a wmic command to reset the global SearchList is an option that could be considered in a separate patch.
Trac # 1209, 1331
---
Note: this will set the domain twice if both v4 and v6 DNS servers are defined. It cant hurt, but could be avoided by making the domain setting a separate call from the DNS server setting.
src/openvpn/tun.c | 21 +++++-- src/openvpnserv/interactive.c | 133 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 147 insertions(+), 7 deletions(-)
src/openvpn/tun.c +17 -4
@@ -159,7 +159,7 @@ do_dns_service(bool add, const short family, const struct tuntap *tt)int addr_len = add ? len :0 ;const char *ip_proto_name = family ==AF_INET6 ?"IPv6" :"IPv4" ; -if (addr_len ==0 && add)/* no addresses to add */ +if (addr_len ==0 && !tt->options.domain && add)/* no addresses or domain to add */ {return true; }
@@ -199,9 +199,21 @@ do_dns_service(bool add, const short family, const struct tuntap *tt) dns.addr[i].ipv4.s_addr =htonl (tt->options.dns[i]); } } +if (tt->options.domain) + { +strncpy (dns.domains, tt->options.domain,_countof (dns.domains)); +/* truncation of domain name is not checked as it cant happen +* with 512 bytes room in dns.domains. +*/ +msg (D_LOW ,"%s dns domain on '%s' (if_index = %d) using service" , + (add ?"Setting" :"Deleting" ), dns.iface.name, dns.iface.index); + } -msg (D_LOW ,"%s %s dns servers on '%s' (if_index = %d) using service" , - (add ?"Setting" :"Deleting" ), ip_proto_name, dns.iface.name, dns.iface.index); +if (addr_len >0 || !add) + { +msg (D_LOW ,"%s %s dns servers on '%s' (if_index = %d) using service" , + (add ?"Setting" :"Deleting" ), ip_proto_name, dns.iface.name, dns.iface.index); + }if (!send_msg_iservice (pipe, &dns,sizeof (dns), &ack,"TUN" )) {
@@ -216,7 +228,8 @@ do_dns_service(bool add, const short family, const struct tuntap *tt) goto out; } -msg (M_INFO ,"%s dns servers %s using service" , ip_proto_name, (add ?"set" :"deleted" )); +msg (M_INFO ,"%s dns servers %s%s using service" , ip_proto_name, + (tt->options.domain?"and domain " :"" ), (add ?"set" :"deleted" )); ret = true; out:
src/openvpnserv/interactive.c +130 -3
182 lines not on this page · raw message
Signature · 1 line
2.1.4
Hi,
Quoted 4 lines
Note: this will set the domain twice if both v4 and v6 DNS servers are defined. It cant hurt, but could be avoided by making the domain setting a separate call from the DNS server setting.
I think we should do that - there is no reason to make expensive call twice:
$ time wmic nicconfig where \(InterfaceIndex=38\) call SetDNSDomain vpntest.lev
Executing (\\LAPTOP-4L3N7KFS\ROOT\CIMV2:Win32_NetworkAdapterConfiguration.Index=17)->SetDNSDomain()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 0;
};real 0m0,236s user 0m0,000s sys 0m0,015s
Besides, DOMAIN could also be set by DHCP - there is no point to do it via service in this case.
src/openvpn/tun.c do_dns_service(bool add, const short family, const struct tuntap *tt) · line 207
Quoted 4 lines
src/openvpn/tun.c +4
-0+strncpy (dns.domains, tt->options.domain,_countof (dns.domains)); +/* truncation of domain name is not checked as it cant happen +* with 512 bytes room in dns.domains. +*/
Mix of tabs and whitespaces.
Quoted 1 line
+ /* comma separated list must be enclosed in parenthesis */
We don't pass lists yet, is this for future SearchDomains support?
src/openvpnserv/interactive.c HandleDNSConfigMessage(const dns_cfg_message_t *msg, undo_lists_t *lists) · line 1280
Quoted 3 lines
src/openvpnserv/interactive.c +3
-0+if (msg->domains[0 ]) { + err =SetDNSDomain (wide_name, msg->domains, lists); + }
msg->domains comes from (unprivileged) client and might not be NULL terminated. Shall we do something like
msg->domains[sizeof(msg->domains) - 1] = '\0';
Same for interface_t::name. Or am I missing something?
Hi
Thanks for the review.
Quoted 10 lines · Lev Stipakov
Hi,
Quoted 4 lines
Note: this will set the domain twice if both v4 and v6 DNS servers are defined. It cant hurt, but could be avoided by making the domain setting a separate call from the DNS server setting.
I think we should do that - there is no reason to make expensive call twice:
Separating the two is easy, but not sure how hard it would be to avoid calling it twice. With the ipv6-only feature, ipv4 setting is no longer guaranteed and elsewhere in the code we do some tasks twice: once for v4 and once for v6 instead of keeping a tab.
I'll take a closer look.
Quoted 12 lines
$ time wmic nicconfig where \(InterfaceIndex=38\) call SetDNSDomain
vpntest.lev
Executing
(\\LAPTOP-4L3N7KFS\ROOT\CIMV2:Win32_NetworkAdapterConfiguration.Index=17)->SetDNSDomain()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 0;
};Quoted 3 lines
real 0m0,236s user 0m0,000s sys 0m0,015s
Besides, DOMAIN could also be set by DHCP - there is no point
Quoted 2 lines
to do it via service in this case.
If DHCP is in use, do_dns_service() will not get called and the DOMAIN setting will not be delegated to the service even with this patch.
Quoted 7 lines
src/openvpn/tun.c do_dns_service(bool add, const short family, const struct tuntap *tt) · line 207
Quoted 4 lines
src/openvpn/tun.c +4
-0+strncpy (dns.domains, tt->options.domain,_countof (dns.domains)); +/* truncation of domain name is not checked as it cant happen +* with 512 bytes room in dns.domains. +*/ Mix of tabs and whitespaces.
Will fix.
Quoted 5 lines
Quoted 2 lines
+ /* comma separated list must be enclosed in parenthesis */
We don't pass lists yet, is this for future SearchDomains support?
Yes, kind of...
I have been playing with this for a while and was motivated because wmic does support SetDNSSuffixSearchOrder. But works only for the global setting, not per interface -- it errors if a where clause is added.
I left the code in there as it may get supported in future. Also it took awhile for me to figure out what format is needed for lists vs single objects from the command line.
Quoted 5 lines
src/openvpnserv/interactive.c HandleDNSConfigMessage(const dns_cfg_message_t *msg, undo_lists_t *lists) · line 1280
Quoted 4 lines
src/openvpnserv/interactive.c +3
-0+if (msg->domains[0 ]) { + err =SetDNSDomain (wide_name, msg->domains, lists); + }
Quoted 7 lines
msg->domainscomes from (unprivileged) client and might not be NULL terminated. Shall we do something likemsg->domains[sizeof(msg->domains) - 1] = '\0';Same for
interface_t::name. Or am I missing something?
My mistake. Will fix. Also our own safer strncpynt instead of strncpy in do_dns_service().
Thanks,
Selva
PATCH v2 1/1 Set DNS Domain using iservice
In reply to [email protected]...From: Selva Nair <[email protected]...>
Use wmic instead of directly editing the registry as the former does not take full effect unless the dns client service is restarted.
Editing the registry appears to work erratically depending on whether its followed with a dchp renew or ipconfig /registerdns etc.
DOMAIN-SEARCH is not handled here as wmic only supports setting the global search list which will over-ride all interface specific values. Editing the registry directly combined with a wmic command to reset the global SearchList is an option that could be considered in a separate patch.
Trac # 1209, 1331
v2 changes
- Separate DNS domain setting from DNS server setting and call only once either during IPv4 processing or IPv6 processing if the former is not active. (file changed: tun.c)
- Null terminate domain and interface_name received from the client. (file changed: interactive.c)
Its done using a const cast-away of msg in a limited scope. Not pretty, but alternatives are no better.
---
Note that the design of interactive service is such that the msg-channel client is always a designated (i.e. trusted) executable. So strigs received from it can be trusted. The null-termination above is out of an abundance of caution. This is unlike those received on the service pipe which cannot be trusted.
src/openvpn/tun.c | 68 ++++++++++++++++++++ src/openvpnserv/interactive.c | 140 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 205 insertions(+), 3 deletions(-)
src/openvpn/tun.c +68 -0
@@ -149,6 +149,61 @@ out: }static bool +do_dns_domain_service (bool add,const struct tuntap *tt) +{ +bool ret = false; +ack_message_t ack; +struct gc_arena gc =gc_new (); +HANDLE pipe = tt->options.msg_channel; + +if (!tt->options.domain)/* no domain to add or delete */ + { +return true; + } + +/* Use dns_cfg_msg with addr_len = 0 for setting only the DOMAIN */ +dns_cfg_message_t dns = { + .header = { + (add ? msg_add_dns_cfg : msg_del_dns_cfg), +sizeof (dns_cfg_message_t), +0 + }, + .iface = { .index = tt->adapter_index, .name ="" }, + .domains ="" ,/* set below */ + .family =AF_INET ,/* unused */ + .addr_len =0 /* add/delete only the domain, not DNS servers */ + }; + +strncpynt (dns.iface.name, tt->actual_name,sizeof (dns.iface.name)); +strncpynt (dns.domains, tt->options.domain,sizeof (dns.domains)); +/* truncation of domain name is not checked as it can't happen +* with 512 bytes room in dns.domains. +*/ + +msg (D_LOW ,"%s dns domain on '%s' (if_index = %d) using service" , + (add ?"Setting" :"Deleting" ), dns.iface.name, dns.iface.index); +if (!send_msg_iservice (pipe, &dns,sizeof (dns), &ack,"TUN" )) + { + goto out; + } + +if (ack.error_number !=NO_ERROR ) + { +msg (M_WARN ,"TUN: %s dns domain failed using service: %s [status=%u if_name=%s]" , + (add ?"adding" :"deleting" ),strerror_win32 (ack.error_number, &gc), + ack.error_number, dns.iface.name); + goto out; + } + +msg (M_INFO ,"DNS domain %s using service" , (add ?"set" :"deleted" )); + ret = true; + +out: +gc_free (&gc); +return ret; +} + +static bool do_dns_service (bool add,const short family,const struct tuntap *tt) {bool ret = false;
@@ -164,6 +219,7 @@ do_dns_service(bool add, const short family, const struct tuntap *tt)return true; } +/* Use dns_cfg_msg with domain = "" for setting only the DNS servers */ dns_cfg_message_t dns = { .header = { (add ? msg_add_dns_cfg : msg_del_dns_cfg),
@@ -1100,6 +1156,11 @@ do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, int tun_mtu, }do_dns_service (true,AF_INET6 , tt);do_set_mtu_service (tt,AF_INET6 , tun_mtu); +/* If IPv4 is not enabled, set DNS domain here */ +if (!tt->did_ifconfig_setup) + { +do_dns_domain_service (true, tt); + } }else {
@@ -1485,6 +1546,7 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu, {do_address_service (true,AF_INET , tt);do_dns_service (true,AF_INET , tt); +do_dns_domain_service (true, tt); } else if (tt->options.ip_win32_type == IPW32_SET_NETSH) {
@@ -6761,6 +6823,11 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) }else if (tt->options.msg_channel) { +/* If IPv4 is not enabled, delete DNS domain here */ +if (!tt->did_ifconfig_setup) + { +do_dns_domain_service (false, tt); + }if (tt->options.dns6_len >0 ) {do_dns_service (false,AF_INET6 , tt);
@@ -6786,6 +6853,7 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) }else if (tt->options.msg_channel) { +do_dns_domain_service (false, tt);do_dns_service (false,AF_INET , tt);do_address_service (false,AF_INET , tt); }
src/openvpnserv/interactive.c +137 -3
195 lines not on this page · raw message
Signature · 1 line
2.1.4
Hi,
Quoted 1 line
+ if (msg->domains[0]) {
Curly bracket could go to the next line, but @cron2 could fix that.
do_dns_domain_service() duplicates code from do_dns_service(), but putting everything into one function will make it harder to read.
Compiled and tested on MSVC/Win10 - with that and ADAPTER_DOMAIN_SUFFIX patch I connected to corporate VPN with Wintun and was able to browse intra websites such as https://jenkins-dev. Also checked that "Connection-specific DNS Suffix" is visible in "ipconfig /all".
I didn't run client on Windows 7, but checked that "wmic nicconf" works there.
Your patch has been applied to the master and release/2.5 branch.
I have not tested it, but at least test compiled on MinGW/Ubuntu 18.04, and slightly stared at the code. Whitespace fixed as instructed :-)
commit 70882f3e40df1c70c553b8c22c747b468d5a0dc7 (master) commit 7d7bb0c9ba0594809a889798485c299e8973a24f (release/2.5)
Author: Selva Nair Date: Fri Sep 25 22:04:46 2020 -0400
Set DNS Domain using iservice
Signed-off-by: Selva Nair <[email protected]...>
Acked-by: Lev Stipakov <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg21097.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering