-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.cpp
308 lines (250 loc) · 7.32 KB
/
dns.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*****************************************************************************
* Copyright 2005 Alt-N Technologies, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* This code incorporates intellectual property owned by Yahoo! and licensed
* pursuant to the Yahoo! DomainKeys Patent License Agreement.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*****************************************************************************/
#ifdef WIN32
#include <windows.h>
#include "windns.h"
#else
#include <sys/types.h>
#include <ctype.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#endif
#include <stdio.h>
#include <string.h>
#include "dkim.h"
#include "dns.h"
#ifdef WIN32
#include "dnsresolv.h"
#endif
#ifdef WIN32
#ifndef DNS_ERROR_RCODE_SERVER_FAILURE
#define DNS_ERROR_RCODE_SERVER_FAILURE 9002L
#endif
typedef DNS_STATUS (WINAPI *DNSQUERYFUNC)( LPCSTR lpstrName, WORD wType, DWORD fOptions,
PIP4_ARRAY aipServers, PDNS_RECORD *ppQueryResultsSet,
PVOID *pReserved );
typedef void (WINAPI *DNSRECORDLISTFREE)(PDNS_RECORD, DNS_FREE_TYPE);
static HMODULE s_hDNSAPI = NULL; // handle to dnsapi.dll
static bool s_bDNSAPIAvailable = true; // set to false if DLL can't be loaded
static DNSQUERYFUNC s_DnsQuery = NULL; // DnsQuery function address
static DNSRECORDLISTFREE s_DnsRecordListFree = NULL;
////////////////////////////////////////////////////////////////////////////////
//
// CheckForDNSAPI - Attempt to load dnsapi.dll once
//
////////////////////////////////////////////////////////////////////////////////
bool CheckForDNSAPI(void)
{
if( s_bDNSAPIAvailable )
{
if( s_hDNSAPI == NULL )
{
s_hDNSAPI = LoadLibrary( "dnsapi.dll" );
if( s_hDNSAPI == NULL )
{
s_bDNSAPIAvailable = false;
}
else
{
s_DnsQuery = (DNSQUERYFUNC)GetProcAddress( s_hDNSAPI, "DnsQuery_A" );
s_DnsRecordListFree = (DNSRECORDLISTFREE)GetProcAddress( s_hDNSAPI, "DnsRecordListFree" );
if( s_DnsQuery == NULL || s_DnsRecordListFree == NULL )
{
FreeLibrary( s_hDNSAPI );
s_hDNSAPI = NULL;
s_bDNSAPIAvailable = false;
}
}
}
}
return s_bDNSAPIAvailable;
}
////////////////////////////////////////////////////////////////////////////////
//
// _DNSGetTXT2k - for Win2k+
//
////////////////////////////////////////////////////////////////////////////////
int _DNSGetTXT2k( const char* szSubDomain, char* Buffer, int nBufLen )
{
PDNS_RECORD answer;
DNS_STATUS res = s_DnsQuery( szSubDomain, DNS_TYPE_TEXT, DNS_QUERY_TREAT_AS_FQDN, NULL,
&answer, NULL );
if (res)
{
if( res == DNS_ERROR_RCODE_SERVER_FAILURE)
return DNSRESP_TEMP_FAIL;
else if (res == DNS_ERROR_RCODE_NAME_ERROR)
return DNSRESP_NXDOMAIN;
else
return DNSRESP_PERM_FAIL;
}
bool bFoundRecord = false;
int nRet = DNSRESP_EMPTY;
PDNS_RECORD prr = answer;
while( prr )
{
if( prr->wType == DNS_TYPE_TEXT && prr->Data.Txt.dwStringCount > 0 )
{
if( bFoundRecord )
{
// multiple records are allowed but we only use the first one
}
else
{
char* BufPtr = Buffer;
int BufLeft = nBufLen-1; // -1 to save room for null terminator
bool Truncated = false;
for( unsigned int i = 0; i < prr->Data.Txt.dwStringCount && !Truncated; i++ )
{
int Len = strlen( prr->Data.Txt.pStringArray[i] );
if( Len > BufLeft )
{
Len = BufLeft;
Truncated = true;
}
memcpy( BufPtr, prr->Data.Txt.pStringArray[i], Len );
BufPtr += Len;
BufLeft -= Len;
}
*BufPtr = '\0';
// TODO: verify that the record follows the DKIM tag-value syntax
bFoundRecord = true;
// TODO: use a different return code if the result was truncated?
nRet = DNSRESP_SUCCESS;
}
}
prr = prr->pNext;
}
s_DnsRecordListFree( answer, DnsFreeRecordList );
return nRet;
}
#define PACKETSZ 1024
#define C_IN 1
#define T_TXT 16
#define HFIXEDSZ 12 /* #/bytes of fixed data in header */
#define MAXDNAME 1025 /* maximum domain name */
#define QFIXEDSZ 4
#define RRFIXEDSZ 10 /* #/bytes of fixed data in r record */
#define NS_CMPRSFLGS 0xc0 /* Flag bits indicating name compression. */
#define NS_MAXCDNAME 255 /* maximum compressed domain name */
#define MAXCDNAME NS_MAXCDNAME
#endif // defined WIN32
static inline unsigned short getshort(unsigned char *cp) {
return (cp[0] << 8) | cp[1];
}
////////////////////////////////////////////////////////////////////////////////
//
// _DNSGetTXT for UNIX and win2k-
//
////////////////////////////////////////////////////////////////////////////////
int _DNSGetTXT( const char* szSubDomain, char* Buffer, int nBufLen )
{
u_char answer[2*PACKETSZ+1];
int answerlen;
int ancount, qdcount; /* answer count and query count */
u_char *eom, *cp;
u_short type, rdlength; /* fields of records returned */
int rc;
char* bufptr;
answerlen = res_query( szSubDomain, C_IN, T_TXT, answer, sizeof(answer));
if( answerlen < 0 )
{
if( h_errno == TRY_AGAIN )
return DNSRESP_TEMP_FAIL;
else
return DNSRESP_PERM_FAIL;
}
unsigned char rcode = answer[3] & 15;
if (rcode != 0)
{
if (rcode == 3)
return DNSRESP_NXDOMAIN;
else
return DNSRESP_PERM_FAIL;
}
qdcount = getshort( answer + 4); /* http://crynwr.com/rfc1035/rfc1035.html#4.1.1. */
ancount = getshort( answer + 6);
eom = answer + answerlen;
cp = answer + HFIXEDSZ;
while( qdcount-- > 0 && cp < eom )
{
rc = dn_expand( answer, eom, cp, Buffer, nBufLen );
if( rc < 0 ) {
return DNSRESP_PERM_FAIL;
}
cp += rc + QFIXEDSZ;
}
while( ancount-- > 0 && cp < eom )
{
rc = dn_expand( answer, eom, cp, Buffer, nBufLen );
if( rc < 0 ) {
return DNSRESP_PERM_FAIL;
}
cp += rc;
if (cp + RRFIXEDSZ >= eom) return DNSRESP_PERM_FAIL;
type = getshort(cp + 0); /* http://crynwr.com/rfc1035/rfc1035.html#4.1.3. */
rdlength = getshort(cp + 8);
cp += RRFIXEDSZ;
if( type != T_TXT ) {
cp += rdlength;
continue;
}
bufptr = Buffer;
while (rdlength && cp < eom)
{
int cnt;
cnt = *cp++; /* http://crynwr.com/rfc1035/rfc1035.html#3.3.14. */
if( bufptr-Buffer + cnt + 1 >= nBufLen )
return DNSRESP_PERM_FAIL;
if (cp + cnt > eom)
return DNSRESP_PERM_FAIL;
memcpy( bufptr, cp, cnt);
rdlength -= cnt + 1;
bufptr += cnt;
cp += cnt;
*bufptr = '\0';
}
return DNSRESP_SUCCESS;
}
return DNSRESP_EMPTY;
}
////////////////////////////////////////////////////////////////////////////////
//
// DNSGetTXT
//
// Pass in the FQDN to get the TXT record
//
////////////////////////////////////////////////////////////////////////////////
int DNSGetTXT( const char* szFQDN, char* Buffer, int nBufLen )
{
if (strlen(szFQDN) > MAX_DOMAIN)
return DNSRESP_DOMAIN_NAME_TOO_LONG;
// Initialize out parameter
Buffer[0] = '\0';
#ifdef WIN32
if( CheckForDNSAPI() == true )
{
return _DNSGetTXT2k( szFQDN, Buffer, nBufLen );
}
#endif
return _DNSGetTXT( szFQDN, Buffer, nBufLen );
}