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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/*
** Copyright 1998 - 2011 Double Precision, Inc.
** See COPYING for distribution information.
*/
#include "rfc1035.h"
#include <string.h>
#include <stdlib.h>
static int found(const struct rfc1035_reply *r,
const struct rfc1035_rr *rrp, const char *h,
unsigned qclass, unsigned qtype)
{
char namebuf[RFC1035_MAXNAMESIZE+1];
if ((rrp->rrtype != qtype &&
rrp->rrtype != RFC1035_TYPE_CNAME) ||
rrp->rrclass != qclass) return (0);
if (rfc1035_replyhostname(r, rrp->rrname, namebuf) == 0)
return (0);
if (rfc1035_hostnamecmp(namebuf, h) == 0) return (1);
return (0);
}
int rfc1035_replysearch_an(const struct rfc1035_res *res,
const struct rfc1035_reply *r, const char *h,
unsigned qtype, unsigned qclass, int pos)
{
if (res->rfc1035_defaultdomain && strchr(h, '.') == NULL)
{
char *buf;
int rc;
/* Append default domain */
if ((buf=malloc(strlen(h)+
strlen(res->rfc1035_defaultdomain)+2)) == 0)
return (-1);
strcat(strcat(strcpy(buf, h), "."),
res->rfc1035_defaultdomain);
rc=rfc1035_replysearch_an(res, r, buf, qtype, qclass, pos);
free(buf);
return (rc);
}
for ( ; pos >= 0 && pos < r->ancount; pos++)
if (found(r, r->anptr+pos, h, qclass, qtype)) return (pos);
return (-1);
}
int rfc1035_replysearch_ns(const struct rfc1035_res *res,
const struct rfc1035_reply *r, const char *h,
unsigned qtype, unsigned qclass, int pos)
{
if (res->rfc1035_defaultdomain && strchr(h, '.') == NULL)
{
char *buf;
int rc;
/* Append default domain */
if ((buf=malloc(strlen(h)+
strlen(res->rfc1035_defaultdomain)+2)) == 0)
return (-1);
strcat(strcat(strcpy(buf, h), "."),
res->rfc1035_defaultdomain);
rc=rfc1035_replysearch_ns(res, r, buf, qtype, qclass, pos);
free(buf);
return (rc);
}
for ( ; pos >= 0 && pos < r->nscount; pos++)
if (found(r, r->nsptr+pos, h, qclass, qtype)) return (pos);
return (-1);
}
int rfc1035_replysearch_ar(const struct rfc1035_res *res,
const struct rfc1035_reply *r, const char *h,
unsigned qtype, unsigned qclass, int pos)
{
if (res->rfc1035_defaultdomain && strchr(h, '.') == NULL)
{
char *buf;
int rc;
/* Append default domain */
if ((buf=malloc(strlen(h)+
strlen(res->rfc1035_defaultdomain)+2)) == 0)
return (-1);
strcat(strcat(strcpy(buf, h), "."),
res->rfc1035_defaultdomain);
rc=rfc1035_replysearch_ar(res, r, buf, qtype, qclass, pos);
free(buf);
return (rc);
}
for ( ; pos >= 0 && pos < r->arcount; pos++)
if (found(r, r->arptr+pos, h, qclass, qtype)) return (pos);
return (-1);
}
int rfc1035_replysearch_all(const struct rfc1035_res *res,
const struct rfc1035_reply *r, const char *h,
unsigned qtype, unsigned qclass, int pos)
{
unsigned s;
if (res->rfc1035_defaultdomain && strchr(h, '.') == NULL)
{
char *buf;
int rc;
/* Append default domain */
if ((buf=malloc(strlen(h)+
strlen(res->rfc1035_defaultdomain)+2)) == 0)
return (-1);
strcat(strcat(strcpy(buf, h), "."),
res->rfc1035_defaultdomain);
rc=rfc1035_replysearch_all(res, r, buf, qtype, qclass, pos);
free(buf);
return (rc);
}
s=r->ancount+r->nscount+r->arcount;
for ( ; pos >= 0 && pos < s; pos++)
if (found(r, r->allrrs[pos], h, qclass, qtype)) return (pos);
return (-1);
}
int rfc1035_resolve_cname(struct rfc1035_res *res,
char *namebuf,
unsigned qtype,
unsigned qclass,
struct rfc1035_reply **ptr,
int x_flags)
{
int n;
int recursion_count=10;
if ( (*ptr=rfc1035_resolve(res, RFC1035_OPCODE_QUERY,
namebuf, qtype, qclass)) == 0)
return (-1);
if ((x_flags & RFC1035_X_RANDOMIZE) &&
(*ptr)->rcode == RFC1035_RCODE_NOERROR)
rfc1035_rr_rand(*ptr);
if ( (*ptr)->rcode != RFC1035_RCODE_NOERROR ||
(n=rfc1035_replysearch_an( res,
*ptr, namebuf, qtype, qclass, 0))<0)
return (-1);
if ( (*ptr)->anptr[n].rrtype == qtype) return (n);
/* Must've gotten back a CNAME when we were looking for something else
*/
for (;;)
{
if (rfc1035_replyhostname( *ptr, (*ptr)->anptr[n].rr.domainname,
namebuf) == 0)
{
rfc1035_replyfree( *ptr );
*ptr=0;
return (-1);
}
/* Let's see if we already have the answer for the canonical name */
if ( (n=rfc1035_replysearch_all( res, *ptr, namebuf, qtype,
qclass, 0)) >= 0)
{
if ( (*ptr)->anptr[n].rrtype == qtype) return (n);
if ( --recursion_count > 0)
continue;
rfc1035_replyfree( *ptr ); /* Recursive CNAME */
*ptr=0;
return (RFC1035_ERR_CNAME_RECURSIVE);
}
rfc1035_replyfree( *ptr );
if ( (*ptr=rfc1035_resolve(res, RFC1035_OPCODE_QUERY,
namebuf, qtype, qclass)) == 0)
return (-1);
if ( (*ptr)->rcode == RFC1035_RCODE_NOERROR &&
(n=rfc1035_replysearch_an( res, *ptr, namebuf,
qtype, qclass, 0)) >= 0)
{
if ( (*ptr)->anptr[n].rrtype == qtype) return (n);
if ( --recursion_count > 0)
continue;
rfc1035_replyfree( *ptr ); /* Recursive CNAME */
*ptr=0;
return (RFC1035_ERR_CNAME_RECURSIVE);
}
return (-1);
}
}
int rfc1035_resolve_cname_multiple(struct rfc1035_res *res,
char *namebuf,
unsigned char *qtypes,
unsigned qclass,
struct rfc1035_reply **ptr,
int x_flags)
{
int n, query_again;
int retry_count=10;
unsigned nqueries=strlen((char*)qtypes), u, good, cname;
struct rfc1035_query qu[nqueries];
struct rfc1035_reply *p;
char savename[RFC1035_MAXNAMESIZE+1],
cmpname1[RFC1035_MAXNAMESIZE+1],
cmpname2[RFC1035_MAXNAMESIZE+1];
static const char forbidden[] =
{
RFC1035_TYPE_CNAME,
RFC1035_TYPE_AXFR,
RFC1035_TYPE_ANY,
0
};
*ptr=NULL;
if (nqueries == 0)
return -1;
strcpy(savename, namebuf);
for (u=0; u<nqueries; ++u)
{
qu[u].name=namebuf;
qu[u].qclass=qclass;
qu[u].qtype=qtypes[u];
if (strchr(forbidden, qtypes[u]))
{
if (nqueries == 1)
{
return rfc1035_resolve_cname(res, namebuf,
qtypes[0],
qclass,
ptr,
x_flags);
}
return -1;
}
}
do // query loop
{
if (*ptr)
rfc1035_replyfree( *ptr );
if ( (*ptr=rfc1035_resolve_multiple(res, RFC1035_OPCODE_QUERY,
qu, nqueries)) == 0)
return (-1);
query_again=0;
for (;;) // change name loop
{
int fatal = 0;
good = cname = 0;
for (u=0, p=*ptr; p; ++u, p=p->next)
{
if (p->rcode != RFC1035_RCODE_NOERROR ||
(n=rfc1035_replysearch_all(res,
p, namebuf,
qu[u].qtype,
qclass, 0))
< 0)
continue;
else if (p->allrrs[n]->rrtype == qu[u].qtype)
{
++good;
}
else // CNAME (not the queried type).
{
if (rfc1035_replyhostname(p, p->anptr[n].rr.domainname,
cname? cmpname1: cmpname2) == 0)
fatal=-1;
else if (cname && strcmp(cmpname1, cmpname2))
query_again=1; // different aliases: try again
++cname;
}
}
if (good == 0 && cname == 0)
{
return (-1);
}
if (good > 0 && cname > 0)
query_again=1; // have both cnames and non-cnames
if (query_again)
{
if (--retry_count > 0)
{
/* Query back from the original alias. */
strcpy(namebuf, savename);
break;
}
fatal=RFC1035_ERR_CNAME_RECURSIVE;
}
if (fatal)
{
rfc1035_replyfree( *ptr );
*ptr=0;
return fatal;
}
if (good)
break;
/* CNAME found. Return alias to caller, and restart from there */
strcpy(namebuf, cmpname1);
}
} while (query_again);
if (x_flags & RFC1035_X_RANDOMIZE)
for (p=*ptr; p; p=p->next)
if (p->rcode == RFC1035_RCODE_NOERROR)
rfc1035_rr_rand(p);
return (0);
}
|