summaryrefslogtreecommitdiffstats
path: root/lib/irs/win32/resconf.c
blob: 3b46b8ed90b4da8fb7471d212dfe77a1c4634f59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

/*
 * Note that on Win32 there is normally no resolv.conf since all information
 * is stored in the registry. Therefore there is no ordering like the
 * contents of resolv.conf. Since the "search" or "domain" keyword, on
 * Win32 if a search list is found it is used, otherwise the domain name
 * is used since they are mutually exclusive. The search list can be entered
 * in the DNS tab of the "Advanced TCP/IP settings" window under the same place
 * that you add your nameserver list.
 */

#define HAVE_GET_WIN32_NAMESERVERS 1

#include "../resconf.c"
#include <iphlpapi.h>

#define TCPIP_SUBKEY "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"

isc_result_t
get_win32_searchlist(irs_resconf_t *conf) {
	isc_result_t result = ISC_R_SUCCESS;
	HKEY hKey;
	char searchlist[MAX_PATH];
	DWORD searchlen = MAX_PATH;
	LSTATUS status;
	char *cp;

	REQUIRE(conf != NULL);

	memset(searchlist, 0, MAX_PATH);
	status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TCPIP_SUBKEY, 0, KEY_READ,
			      &hKey);
	if (status != ERROR_SUCCESS) {
		return (ISC_R_SUCCESS);
	}

	status = RegQueryValueEx(hKey, "SearchList", NULL, NULL,
				 (LPBYTE)searchlist, &searchlen);
	RegCloseKey(hKey);
	if (status != ERROR_SUCCESS) {
		return (ISC_R_SUCCESS);
	}

	cp = strtok((char *)searchlist, ", \0");
	while (cp != NULL) {
		result = add_search(conf, cp);
		if (result != ISC_R_SUCCESS) {
			break;
		}
		cp = strtok(NULL, ", \0");
	}
	return (result);
}

isc_result_t
get_win32_nameservers(irs_resconf_t *conf) {
	isc_result_t result;
	FIXED_INFO *FixedInfo;
	ULONG BufLen = sizeof(FIXED_INFO);
	DWORD dwRetVal;
	IP_ADDR_STRING *pIPAddr;

	REQUIRE(conf != NULL);

	FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, BufLen);
	if (FixedInfo == NULL) {
		return (ISC_R_NOMEMORY);
	}
	dwRetVal = GetNetworkParams(FixedInfo, &BufLen);
	if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
		GlobalFree(FixedInfo);
		FixedInfo = GlobalAlloc(GPTR, BufLen);
		if (FixedInfo == NULL) {
			return (ISC_R_NOMEMORY);
		}
		dwRetVal = GetNetworkParams(FixedInfo, &BufLen);
	}
	if (dwRetVal != ERROR_SUCCESS) {
		GlobalFree(FixedInfo);
		return (ISC_R_FAILURE);
	}

	result = get_win32_searchlist(conf);
	if (result != ISC_R_SUCCESS) {
		goto cleanup;
	}

	if (ISC_LIST_EMPTY(conf->searchlist) &&
	    strlen(FixedInfo->DomainName) > 0)
	{
		result = add_search(conf, FixedInfo->DomainName);
		if (result != ISC_R_SUCCESS) {
			goto cleanup;
		}
	}

	/* Get the list of nameservers */
	pIPAddr = &FixedInfo->DnsServerList;
	while (pIPAddr) {
		if (conf->numns >= RESCONFMAXNAMESERVERS) {
			break;
		}

		result = add_server(conf->mctx, pIPAddr->IpAddress.String,
				    &conf->nameservers);
		if (result != ISC_R_SUCCESS) {
			break;
		}
		conf->numns++;
		pIPAddr = pIPAddr->Next;
	}

cleanup:
	if (FixedInfo != NULL) {
		GlobalFree(FixedInfo);
	}
	return (result);
}