Skip to content

Commit 1d1b3d4

Browse files
authored
Ignore Windows IDN search domains (#1034)
If a Windows search domain isn't already punycoded and therefore isn't ascii, ignore it for now. The real fix will come with #1031, but that code change wouldn't be suitable for backport. Fixes #1016 Signed-off-by: Brad House (@bradh352)
1 parent 89f91c0 commit 1d1b3d4

3 files changed

Lines changed: 54 additions & 32 deletions

File tree

‎src/lib/ares_hosts_file.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
#endif
4242
#include <time.h>
4343

44+
#ifdef USE_WINSOCK
45+
# define DATABASEPATH "DatabasePath"
46+
# define WIN_PATH_HOSTS "\\hosts"
47+
#endif
48+
4449
/* HOSTS FILE PROCESSING OVERVIEW
4550
* ==============================
4651
* The hosts file on the system contains static entries to be processed locally

‎src/lib/ares_private.h‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,7 @@
8383
#define CARES_INADDR_CAST(type, var) ((type)((const void *)var))
8484

8585
#if defined(USE_WINSOCK)
86-
87-
# define WIN_NS_9X "System\\CurrentControlSet\\Services\\VxD\\MSTCP"
8886
# define WIN_NS_NT_KEY "System\\CurrentControlSet\\Services\\Tcpip\\Parameters"
89-
# define WIN_DNSCLIENT "Software\\Policies\\Microsoft\\System\\DNSClient"
90-
# define WIN_NT_DNSCLIENT \
91-
"Software\\Policies\\Microsoft\\Windows NT\\DNSClient"
92-
# define NAMESERVER "NameServer"
93-
# define DHCPNAMESERVER "DhcpNameServer"
94-
# define DATABASEPATH "DatabasePath"
95-
# define WIN_PATH_HOSTS "\\hosts"
96-
# define SEARCHLIST_KEY "SearchList"
97-
# define PRIMARYDNSSUFFIX_KEY "PrimaryDNSSuffix"
98-
# define INTERFACES_KEY "Interfaces"
99-
# define DOMAIN_KEY "Domain"
100-
# define DHCPDOMAIN_KEY "DhcpDomain"
10187
# define PATH_RESOLV_CONF ""
10288
#elif defined(WATT32)
10389

‎src/lib/ares_sysconfig_win.c‎

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@
5555
#include "ares_inet_net_pton.h"
5656

5757
#if defined(USE_WINSOCK)
58+
59+
# define WIN_NS_9X "System\\CurrentControlSet\\Services\\VxD\\MSTCP"
60+
# define WIN_DNSCLIENT "Software\\Policies\\Microsoft\\System\\DNSClient"
61+
# define WIN_NT_DNSCLIENT \
62+
"Software\\Policies\\Microsoft\\Windows NT\\DNSClient"
63+
# define NAMESERVER "NameServer"
64+
# define DHCPNAMESERVER "DhcpNameServer"
65+
# define SEARCHLIST_KEY L"SearchList"
66+
# define PRIMARYDNSSUFFIX_KEY L"PrimaryDNSSuffix"
67+
# define INTERFACES_KEY "Interfaces"
68+
# define DOMAIN_KEY L"Domain"
69+
# define DHCPDOMAIN_KEY L"DhcpDomain"
70+
5871
/*
5972
* get_REG_SZ()
6073
*
@@ -69,38 +82,48 @@
6982
*
7083
* Supported on Windows NT 3.5 and newer.
7184
*/
72-
static ares_bool_t get_REG_SZ(HKEY hKey, const char *leafKeyName, char **outptr)
85+
static ares_bool_t get_REG_SZ(HKEY hKey, const WCHAR *leafKeyName, char **outptr)
7386
{
74-
DWORD size = 0;
75-
int res;
87+
DWORD size = 0;
88+
int res;
89+
int len;
90+
WCHAR *val = NULL;
7691

7792
*outptr = NULL;
7893

7994
/* Find out size of string stored in registry */
80-
res = RegQueryValueExA(hKey, leafKeyName, 0, NULL, NULL, &size);
95+
res = RegQueryValueExW(hKey, leafKeyName, 0, NULL, NULL, &size);
8196
if ((res != ERROR_SUCCESS && res != ERROR_MORE_DATA) || !size) {
8297
return ARES_FALSE;
8398
}
8499

85100
/* Allocate buffer of indicated size plus one given that string
86101
might have been stored without null termination */
87-
*outptr = ares_malloc(size + 1);
88-
if (!*outptr) {
102+
val = ares_malloc_zero(size + sizeof(WCHAR));
103+
if (val == NULL) {
89104
return ARES_FALSE;
90105
}
91106

92107
/* Get the value for real */
93-
res = RegQueryValueExA(hKey, leafKeyName, 0, NULL, (unsigned char *)*outptr,
94-
&size);
95-
if ((res != ERROR_SUCCESS) || (size == 1)) {
108+
res = RegQueryValueExW(hKey, leafKeyName, 0, NULL, (BYTE *)val, &size);
109+
if (res != ERROR_SUCCESS || size == 1) {
110+
ares_free(val);
111+
return ARES_FALSE;
112+
}
113+
114+
/* Convert to UTF8 */
115+
len = WideCharToMultiByte(CP_UTF8, 0, val, -1, NULL, 0, NULL, NULL);
116+
if (len == 0) {
117+
return ARES_FALSE;
118+
}
119+
*outptr = ares_malloc_zero((size_t)len + 1);
120+
if (WideCharToMultiByte(CP_UTF8, 0, val, -1, *outptr, len, NULL, NULL)
121+
== 0) {
96122
ares_free(*outptr);
97123
*outptr = NULL;
98124
return ARES_FALSE;
99125
}
100126

101-
/* Null terminate buffer always */
102-
*(*outptr + size) = '\0';
103-
104127
return ARES_TRUE;
105128
}
106129

@@ -135,6 +158,14 @@ static void commajoin(char **dst, const char *src)
135158
commanjoin(dst, src, ares_strlen(src));
136159
}
137160

161+
static void commajoin_asciionly(char **dst, const char *src)
162+
{
163+
if (!ares_str_isprint(src, ares_strlen(src))) {
164+
return;
165+
}
166+
commanjoin(dst, src, ares_strlen(src));
167+
}
168+
138169
/* A structure to hold the string form of IPv4 and IPv6 addresses so we can
139170
* sort them by a metric.
140171
*/
@@ -523,7 +554,7 @@ static ares_bool_t get_SuffixList_Windows(char **outptr)
523554
ERROR_SUCCESS) {
524555
get_REG_SZ(hKey, SEARCHLIST_KEY, outptr);
525556
if (get_REG_SZ(hKey, DOMAIN_KEY, &p)) {
526-
commajoin(outptr, p);
557+
commajoin_asciionly(outptr, p);
527558
ares_free(p);
528559
p = NULL;
529560
}
@@ -533,7 +564,7 @@ static ares_bool_t get_SuffixList_Windows(char **outptr)
533564
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, WIN_NT_DNSCLIENT, 0, KEY_READ, &hKey) ==
534565
ERROR_SUCCESS) {
535566
if (get_REG_SZ(hKey, SEARCHLIST_KEY, &p)) {
536-
commajoin(outptr, p);
567+
commajoin_asciionly(outptr, p);
537568
ares_free(p);
538569
p = NULL;
539570
}
@@ -545,7 +576,7 @@ static ares_bool_t get_SuffixList_Windows(char **outptr)
545576
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, WIN_DNSCLIENT, 0, KEY_READ, &hKey) ==
546577
ERROR_SUCCESS) {
547578
if (get_REG_SZ(hKey, PRIMARYDNSSUFFIX_KEY, &p)) {
548-
commajoin(outptr, p);
579+
commajoin_asciionly(outptr, p);
549580
ares_free(p);
550581
p = NULL;
551582
}
@@ -567,17 +598,17 @@ static ares_bool_t get_SuffixList_Windows(char **outptr)
567598
}
568599
/* p can be comma separated (SearchList) */
569600
if (get_REG_SZ(hKeyEnum, SEARCHLIST_KEY, &p)) {
570-
commajoin(outptr, p);
601+
commajoin_asciionly(outptr, p);
571602
ares_free(p);
572603
p = NULL;
573604
}
574605
if (get_REG_SZ(hKeyEnum, DOMAIN_KEY, &p)) {
575-
commajoin(outptr, p);
606+
commajoin_asciionly(outptr, p);
576607
ares_free(p);
577608
p = NULL;
578609
}
579610
if (get_REG_SZ(hKeyEnum, DHCPDOMAIN_KEY, &p)) {
580-
commajoin(outptr, p);
611+
commajoin_asciionly(outptr, p);
581612
ares_free(p);
582613
p = NULL;
583614
}

0 commit comments

Comments
 (0)