ignore some files
[tridge/bind9.git] / lib / lwres / win32 / lwconfig.c
1 /*
2  * Copyright (C) 2004, 2006, 2007  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2002  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: lwconfig.c,v 1.7 2007/12/14 01:40:42 marka Exp $ */
19
20 /*
21  * We do this so that we may incorporate everything in the main routines
22  * so that we can take advantage of the fixes and changes made there
23  * without having to add them twice. We can then call the parse routine
24  * if there is a resolv.conf file and fetch our own data from the
25  * Windows environment otherwise.
26  */
27
28 /*
29  * Note that on Win32 there is normally no resolv.conf since all information
30  * is stored in the registry. Therefore there is no ordering like the
31  * contents of resolv.conf. Since the "search" or "domain" keyword, on
32  * Win32 if a search list is found it is used, otherwise the domain name
33  * is used since they are mutually exclusive. The search list can be entered
34  * in the DNS tab of the "Advanced TCP/IP settings" window under the same place
35  * that you add your nameserver list.
36  */
37
38 #define lwres_conf_parse generic_lwres_conf_parse
39 #include "../lwconfig.c"
40 #undef lwres_conf_parse
41
42 #include <iphlpapi.h>
43
44 #define TCPIP_SUBKEY    \
45         "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"
46
47 void
48 get_win32_searchlist(lwres_context_t *ctx) {
49         HKEY hKey;
50         BOOL keyFound = TRUE;
51         char searchlist[MAX_PATH];
52         DWORD searchlen = MAX_PATH;
53         char *cp;
54         lwres_conf_t *confdata;
55
56         REQUIRE(ctx != NULL);
57         confdata = &ctx->confdata;
58
59         memset(searchlist, 0, MAX_PATH);
60         if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TCPIP_SUBKEY, 0, KEY_READ, &hKey)
61                 != ERROR_SUCCESS)
62                 keyFound = FALSE;
63         
64         if (keyFound == TRUE) {
65                 /* Get the named directory */
66                 if (RegQueryValueEx(hKey, "SearchList", NULL, NULL,
67                         (LPBYTE)searchlist, &searchlen) != ERROR_SUCCESS)
68                         keyFound = FALSE;
69                 RegCloseKey(hKey);
70         }
71
72         confdata->searchnxt = 0;
73         cp = strtok((char *)searchlist, ", \0");
74         while (cp != NULL) {
75                 if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
76                         break;
77                 if (strlen(cp) <= MAX_PATH && strlen(cp) > 0) {
78                         confdata->search[confdata->searchnxt] = lwres_strdup(ctx, cp);
79                         if (confdata->search[confdata->searchnxt] != NULL)
80                                 confdata->searchnxt++;
81                 }
82                 cp = strtok(NULL, ", \0");
83         }
84 }
85
86 lwres_result_t
87 lwres_conf_parse(lwres_context_t *ctx, const char *filename) {
88         lwres_result_t ret = LWRES_R_SUCCESS;
89         lwres_result_t res;
90         lwres_conf_t *confdata;
91         FIXED_INFO * FixedInfo;
92         ULONG    BufLen = sizeof(FIXED_INFO);
93         DWORD    dwRetVal;
94         IP_ADDR_STRING *pIPAddr;
95
96         REQUIRE(ctx != NULL);
97         confdata = &ctx->confdata;
98         REQUIRE(confdata != NULL);
99
100         /* Use the resolver if there is one */
101         ret = generic_lwres_conf_parse(ctx, filename);
102         if ((ret != LWRES_R_NOTFOUND && ret != LWRES_R_SUCCESS) ||
103                 (ret == LWRES_R_SUCCESS && confdata->nsnext > 0))
104                 return (ret);
105
106         /*
107          * We didn't get any nameservers so we need to do this ourselves
108          */
109         FixedInfo = (FIXED_INFO *) GlobalAlloc(GPTR, BufLen);
110         dwRetVal = GetNetworkParams(FixedInfo, &BufLen);
111         if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
112                 GlobalFree(FixedInfo);
113                 FixedInfo = GlobalAlloc(GPTR, BufLen);
114                 dwRetVal = GetNetworkParams(FixedInfo, &BufLen);
115         }
116         if (dwRetVal != ERROR_SUCCESS) {
117                 GlobalFree(FixedInfo);
118                 return (LWRES_R_FAILURE);
119         }
120
121         /* Get the search list from the registry */
122         get_win32_searchlist(ctx);
123
124         /* Use only if there is no search list */
125         if (confdata->searchnxt == 0 && strlen(FixedInfo->DomainName) > 0) {
126                 confdata->domainname = lwres_strdup(ctx, FixedInfo->DomainName);
127                 if (confdata->domainname == NULL) {
128                         GlobalFree(FixedInfo);
129                         return (LWRES_R_FAILURE);
130                 }
131         } else
132                 confdata->domainname = NULL;
133
134         /* Get the list of nameservers */
135         pIPAddr = &FixedInfo->DnsServerList;
136         while (pIPAddr) {
137                 if (confdata->nsnext >= LWRES_CONFMAXNAMESERVERS)
138                         break;
139
140                 res = lwres_create_addr(pIPAddr->IpAddress.String,
141                                 &confdata->nameservers[confdata->nsnext++], 1);
142                 if (res != LWRES_R_SUCCESS) {
143                         GlobalFree(FixedInfo);
144                         return (res);
145                 }
146                 pIPAddr = pIPAddr ->Next;
147         }
148
149         GlobalFree(FixedInfo);
150         return (LWRES_R_SUCCESS);
151 }