~ubuntu-branches/ubuntu/oneiric/isc-dhcp/oneiric-security

« back to all changes in this revision

Viewing changes to minires/res_findzonecut.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Pollock
  • Date: 2009-09-02 22:34:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090902223425-nypo7bkftxffq41m
Tags: upstream-4.1.0
ImportĀ upstreamĀ versionĀ 4.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if !defined(lint) && !defined(SABER)
 
2
static const char rcsid[] = "$Id: res_findzonecut.c,v 1.16 2005/03/17 20:15:18 dhankins Exp $";
 
3
#endif /* not lint */
 
4
 
 
5
/*
 
6
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 
7
 * Copyright (c) 1999-2003 by Internet Software Consortium
 
8
 *
 
9
 * Permission to use, copy, modify, and distribute this software for any
 
10
 * purpose with or without fee is hereby granted, provided that the above
 
11
 * copyright notice and this permission notice appear in all copies.
 
12
 *
 
13
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 
14
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
15
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 
16
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
17
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
18
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 
19
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
20
 *
 
21
 *   Internet Systems Consortium, Inc.
 
22
 *   950 Charter Street
 
23
 *   Redwood City, CA 94063
 
24
 *   <info@isc.org>
 
25
 *   http://www.isc.org/
 
26
 */
 
27
 
 
28
/* Import. */
 
29
 
 
30
#include <sys/param.h>
 
31
#include <sys/socket.h>
 
32
#include <sys/time.h>
 
33
 
 
34
#include <netinet/in.h>
 
35
#include <arpa/inet.h>
 
36
 
 
37
#include <errno.h>
 
38
#include <limits.h>
 
39
#include <netdb.h>
 
40
#include <stdarg.h>
 
41
#include <stdio.h>
 
42
#include <stdlib.h>
 
43
#include <string.h>
 
44
 
 
45
#include <isc-dhcp/list.h>
 
46
 
 
47
#include "minires/minires.h"
 
48
#include "arpa/nameser.h"
 
49
 
 
50
/* Data structures. */
 
51
 
 
52
typedef struct rr_a {
 
53
        ISC_LINK(struct rr_a)   link;
 
54
        struct in_addr          addr;
 
55
} rr_a;
 
56
typedef ISC_LIST(rr_a) rrset_a;
 
57
 
 
58
typedef struct rr_ns {
 
59
        ISC_LINK(struct rr_ns) link;
 
60
        char *name;
 
61
        rrset_a addrs;
 
62
} rr_ns;
 
63
typedef ISC_LIST(rr_ns) rrset_ns;
 
64
 
 
65
/* Forward. */
 
66
 
 
67
static int      satisfy(res_state,
 
68
                        const char *, rrset_ns *, struct in_addr *, int);
 
69
static int      add_addrs(res_state, rr_ns *, struct in_addr *, int);
 
70
static ns_rcode get_soa(res_state, const char *, ns_class,
 
71
                        char *, size_t, char *, size_t,
 
72
                        rrset_ns *);
 
73
static isc_result_t get_ns(res_state, const char *, ns_class, rrset_ns *);
 
74
static isc_result_t get_glue(res_state, ns_class, rrset_ns *);
 
75
static isc_result_t save_ns(res_state, ns_msg *, ns_sect,
 
76
                            const char *, ns_class, rrset_ns *);
 
77
static isc_result_t save_a(res_state, ns_msg *, ns_sect,
 
78
                           const char *, ns_class, rrset_a *);
 
79
static void     free_nsrrset(rrset_ns *);
 
80
static void     free_nsrr(rrset_ns *, rr_ns *);
 
81
static rr_ns *  find_ns(rrset_ns *, const char *);
 
82
static isc_result_t do_query(res_state, const char *, ns_class, ns_type,
 
83
                             double *, ns_msg *, int *);
 
84
 
 
85
/* Public. */
 
86
 
 
87
/*
 
88
 * int
 
89
 * res_findzonecut(res, dname, class, zname, zsize, addrs, naddrs)
 
90
 *      find enclosing zone for a <dname,class>, and some server addresses
 
91
 * parameters:
 
92
 *      res - resolver context to work within (is modified)
 
93
 *      dname - domain name whose enclosing zone is desired
 
94
 *      class - class of dname (and its enclosing zone)
 
95
 *      zname - found zone name
 
96
 *      zsize - allocated size of zname
 
97
 *      addrs - found server addresses
 
98
 *      naddrs - max number of addrs
 
99
 * return values:
 
100
 *      < 0 - an error occurred (check errno)
 
101
 *      = 0 - zname is now valid, but addrs[] wasn't changed
 
102
 *      > 0 - zname is now valid, and return value is number of addrs[] found
 
103
 * notes:
 
104
 *      this function calls res_nsend() which means it depends on correctly
 
105
 *      functioning recursive nameservers (usually defined in /etc/resolv.conf
 
106
 *      or its local equivilent).
 
107
 *
 
108
 *      we start by asking for an SOA<dname,class>.  if we get one as an
 
109
 *      answer, that just means <dname,class> is a zone top, which is fine.
 
110
 *      more than likely we'll be told to go pound sand, in the form of a
 
111
 *      negative answer.
 
112
 *
 
113
 *      note that we are not prepared to deal with referrals since that would
 
114
 *      only come from authority servers and our correctly functioning local
 
115
 *      recursive server would have followed the referral and got us something
 
116
 *      more definite.
 
117
 *
 
118
 *      if the authority section contains an SOA, this SOA should also be the
 
119
 *      closest enclosing zone, since any intermediary zone cuts would've been
 
120
 *      returned as referrals and dealt with by our correctly functioning local
 
121
 *      recursive name server.  but an SOA in the authority section should NOT
 
122
 *      match our dname (since that would have been returned in the answer
 
123
 *      section).  an authority section SOA has to be "above" our dname.
 
124
 *
 
125
 *      we cannot fail to find an SOA in this way.  ultimately we'll return
 
126
 *      a zname indicating the root zone if that's the closest enclosing zone.
 
127
 *      however, since authority section SOA's were once optional, it's
 
128
 *      possible that we'll have to go hunting for the enclosing SOA by
 
129
 *      ripping labels off the front of our dname -- this is known as "doing
 
130
 *      it the hard way."
 
131
 *
 
132
 *      ultimately we want some server addresses, which are ideally the ones
 
133
 *      pertaining to the SOA.MNAME, but only if there is a matching NS RR.
 
134
 *      so the second phase (after we find an SOA) is to go looking for the
 
135
 *      NS RRset for that SOA's zone.
 
136
 *
 
137
 *      no answer section processed by this code is allowed to contain CNAME
 
138
 *      or DNAME RR's.  for the SOA query this means we strip a label and
 
139
 *      keep going.  for the NS and A queries this means we just give up.
 
140
 */
 
141
 
 
142
isc_result_t
 
143
res_findzonecut(res_state statp, const char *dname, ns_class class, int opts,
 
144
                char *zname, size_t zsize, struct in_addr *addrs, int naddrs,
 
145
                int *count, void *zcookie)
 
146
{
 
147
        char mname[NS_MAXDNAME];
 
148
        u_long save_pfcode;
 
149
        rrset_ns nsrrs;
 
150
        int n = 0;
 
151
        isc_result_t rcode;
 
152
 
 
153
        DPRINTF(("START dname='%s' class=%s, zsize=%ld, naddrs=%d",
 
154
                 dname, p_class(class), (long)zsize, naddrs));
 
155
        save_pfcode = statp->pfcode;
 
156
        statp->pfcode |= RES_PRF_HEAD2 | RES_PRF_HEAD1 | RES_PRF_HEADX |
 
157
                         RES_PRF_QUES | RES_PRF_ANS |
 
158
                         RES_PRF_AUTH | RES_PRF_ADD;
 
159
        ISC_LIST_INIT(nsrrs);
 
160
 
 
161
        DPRINTF (("look for a predefined zone statement"));
 
162
        rcode = find_cached_zone (dname, class, zname, zsize,
 
163
                                  addrs, naddrs, &n, zcookie);
 
164
        if (rcode == ISC_R_SUCCESS)
 
165
                goto done;
 
166
 
 
167
        DPRINTF(("get the soa, and see if it has enough glue"));
 
168
        if ((rcode = get_soa(statp, dname, class, zname, zsize,
 
169
                             mname, sizeof mname, &nsrrs)) != ISC_R_SUCCESS ||
 
170
            ((opts & RES_EXHAUSTIVE) == 0 &&
 
171
             (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
 
172
                goto done;
 
173
 
 
174
        DPRINTF(("get the ns rrset and see if it has enough glue"));
 
175
        if ((rcode = get_ns(statp, zname, class, &nsrrs)) != ISC_R_SUCCESS ||
 
176
            ((opts & RES_EXHAUSTIVE) == 0 &&
 
177
             (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
 
178
                goto done;
 
179
 
 
180
        DPRINTF(("get the missing glue and see if it's finally enough"));
 
181
        if ((rcode = get_glue(statp, class, &nsrrs)) == ISC_R_SUCCESS)
 
182
                n = satisfy(statp, mname, &nsrrs, addrs, naddrs);
 
183
 
 
184
        /* If we found the zone, cache it. */
 
185
        if (n > 0)
 
186
                cache_found_zone (class, zname, addrs, n);
 
187
 done:
 
188
        DPRINTF(("FINISH n=%d (%s)", n, (n < 0) ? strerror(errno) : "OK"));
 
189
        free_nsrrset(&nsrrs);
 
190
        statp->pfcode = save_pfcode;
 
191
        if (count)
 
192
                *count = n;
 
193
        return rcode;
 
194
}
 
195
 
 
196
/* Private. */
 
197
 
 
198
static int
 
199
satisfy(res_state statp,
 
200
        const char *mname, rrset_ns *nsrrsp, struct in_addr *addrs, int naddrs)
 
201
{
 
202
        rr_ns *nsrr;
 
203
        int n, x;
 
204
 
 
205
        n = 0;
 
206
        nsrr = find_ns(nsrrsp, mname);
 
207
        if (nsrr != NULL) {
 
208
                x = add_addrs(statp, nsrr, addrs, naddrs);
 
209
                addrs += x;
 
210
                naddrs -= x;
 
211
                n += x;
 
212
        }
 
213
        for (nsrr = ISC_LIST_HEAD(*nsrrsp);
 
214
             nsrr != NULL && naddrs > 0;
 
215
             nsrr = ISC_LIST_NEXT(nsrr, link))
 
216
                if (ns_samename(nsrr->name, mname) != 1) {
 
217
                        x = add_addrs(statp, nsrr, addrs, naddrs);
 
218
                        addrs += x;
 
219
                        naddrs -= x;
 
220
                        n += x;
 
221
                }
 
222
        DPRINTF(("satisfy(%s): %d", mname, n));
 
223
        return (n);
 
224
}
 
225
 
 
226
static int
 
227
add_addrs(res_state statp, rr_ns *nsrr, struct in_addr *addrs, int naddrs) {
 
228
        rr_a *arr;
 
229
        int n = 0;
 
230
 
 
231
        for (arr = ISC_LIST_HEAD(nsrr->addrs);
 
232
             arr != NULL; arr = ISC_LIST_NEXT(arr, link)) {
 
233
                if (naddrs <= 0)
 
234
                        return (0);
 
235
                *addrs++ = arr->addr;
 
236
                naddrs--;
 
237
                n++;
 
238
        }
 
239
        DPRINTF(("add_addrs: %d", n));
 
240
        return (n);
 
241
}
 
242
 
 
243
static ns_rcode
 
244
get_soa(res_state statp, const char *dname, ns_class class,
 
245
        char *zname, size_t zsize, char *mname, size_t msize,
 
246
        rrset_ns *nsrrsp)
 
247
{
 
248
        char tname[NS_MAXDNAME];
 
249
        double resp[NS_PACKETSZ / sizeof (double)];
 
250
        int n, i, ancount, nscount;
 
251
        ns_sect sect;
 
252
        ns_msg msg;
 
253
        u_int rcode;
 
254
        isc_result_t status;
 
255
 
 
256
        /*
 
257
         * Find closest enclosing SOA, even if it's for the root zone.
 
258
         */
 
259
 
 
260
        /* First canonicalize dname (exactly one unescaped trailing "."). */
 
261
        status = ns_makecanon(dname, tname, sizeof tname);
 
262
        if (status != ISC_R_SUCCESS)
 
263
                return status;
 
264
        dname = tname;
 
265
 
 
266
        /* Now grovel the subdomains, hunting for an SOA answer or auth. */
 
267
        for (;;) {
 
268
                /* Leading or inter-label '.' are skipped here. */
 
269
                while (*dname == '.')
 
270
                        dname++;
 
271
 
 
272
                /* Is there an SOA? */
 
273
                rcode = do_query(statp, dname, class, ns_t_soa,
 
274
                                 resp, &msg, &n);
 
275
                if (rcode != ISC_R_SUCCESS) {
 
276
                        DPRINTF(("get_soa: do_query('%s', %s) failed (%d)",
 
277
                                 dname, p_class(class), n));
 
278
                        return rcode;
 
279
                }
 
280
                if (n > 0) {
 
281
                        DPRINTF(("get_soa: CNAME or DNAME found"));
 
282
                        sect = ns_s_max, n = 0;
 
283
                } else {
 
284
                        ancount = ns_msg_count(msg, ns_s_an);
 
285
                        nscount = ns_msg_count(msg, ns_s_ns);
 
286
                        if (ancount > 0 && rcode == ISC_R_SUCCESS)
 
287
                                sect = ns_s_an, n = ancount;
 
288
                        else if (nscount > 0)
 
289
                                sect = ns_s_ns, n = nscount;
 
290
                        else
 
291
                                sect = ns_s_max, n = 0;
 
292
                }
 
293
                for (i = 0; i < n; i++) {
 
294
                        const char *t;
 
295
                        const u_char *rdata;
 
296
                        int rdlen;
 
297
                        ns_rr rr;
 
298
 
 
299
                        rcode = ns_parserr(&msg, sect, i, &rr) < 0;
 
300
                        if (rcode != ISC_R_SUCCESS) {
 
301
                                DPRINTF(("get_soa: ns_parserr(%s, %d) failed",
 
302
                                         p_section(sect, ns_o_query), i));
 
303
                                return rcode;
 
304
                        }
 
305
                        if (ns_rr_type(rr) == ns_t_cname ||
 
306
                            ns_rr_type(rr) == ns_t_dname)
 
307
                                break;
 
308
                        if (ns_rr_type(rr) != ns_t_soa ||
 
309
                            ns_rr_class(rr) != class)
 
310
                                continue;
 
311
                        t = ns_rr_name(rr);
 
312
                        switch (sect) {
 
313
                        case ns_s_an:
 
314
                                if (ns_samedomain(dname, t) == 0) {
 
315
                                        DPRINTF(("get_soa: %s'%s', '%s') == 0",
 
316
                                                 "ns_samedomain(", dname, t));
 
317
                                        return ISC_R_NOTZONE;
 
318
                                }
 
319
                                break;
 
320
                        case ns_s_ns:
 
321
                                if (ns_samename(dname, t) == 1 ||
 
322
                                    ns_samedomain(dname, t) == 0) {
 
323
                                        DPRINTF(("get_soa: %smain('%s', '%s')",
 
324
                                                 "ns_samename() || !ns_samedo",
 
325
                                                 dname, t));
 
326
                                        return ISC_R_NOTZONE;
 
327
                                }
 
328
                                break;
 
329
                        default:
 
330
                                abort();
 
331
                        }
 
332
                        if (strlen(t) + 1 > zsize) {
 
333
                                DPRINTF(("get_soa: zname(%d) too small (%d)",
 
334
                                         zsize, strlen(t) + 1));
 
335
                                return ISC_R_NOSPACE;
 
336
                        }
 
337
                        strcpy(zname, t);
 
338
                        rdata = ns_rr_rdata(rr);
 
339
                        rdlen = ns_rr_rdlen(rr);
 
340
                        if (ns_name_uncompress((u_char *)resp,
 
341
                                               ns_msg_end(msg), rdata,
 
342
                                               mname, msize) < 0) {
 
343
                                DPRINTF(("get_soa: %s failed",
 
344
                                         "ns_name_uncompress"));
 
345
                                return ISC_R_NOMEMORY;
 
346
                        }
 
347
                        rcode = save_ns(statp, &msg,
 
348
                                        ns_s_ns, zname, class, nsrrsp);
 
349
                        if (rcode != ISC_R_SUCCESS) {
 
350
                                DPRINTF(("get_soa: save_ns failed"));
 
351
                                return rcode;
 
352
                        }
 
353
                        return ISC_R_SUCCESS;
 
354
                }
 
355
 
 
356
                /* If we're out of labels, then not even "." has an SOA! */
 
357
                if (*dname == '\0')
 
358
                        break;
 
359
 
 
360
                /* Find label-terminating "."; top of loop will skip it. */
 
361
                while (*dname != '.') {
 
362
                        if (*dname == '\\')
 
363
                                if (*++dname == '\0') {
 
364
                                        return ISC_R_NOSPACE;
 
365
                                }
 
366
                        dname++;
 
367
                }
 
368
        }
 
369
        DPRINTF(("get_soa: out of labels"));
 
370
        return ISC_R_DESTADDRREQ;
 
371
}
 
372
 
 
373
static isc_result_t
 
374
get_ns(res_state statp, const char *zname, ns_class class, rrset_ns *nsrrsp) {
 
375
        double resp[NS_PACKETSZ / sizeof (double)];
 
376
        ns_msg msg;
 
377
        int n;
 
378
        isc_result_t rcode;
 
379
 
 
380
        /* Go and get the NS RRs for this zone. */
 
381
        rcode = do_query(statp, zname, class, ns_t_ns, resp, &msg, &n);
 
382
        if (rcode != ISC_R_SUCCESS) {
 
383
                DPRINTF(("get_ns: do_query('zname', %s) failed (%d)",
 
384
                         zname, p_class(class), rcode));
 
385
                return rcode;
 
386
        }
 
387
 
 
388
        /* Remember the NS RRs and associated A RRs that came back. */
 
389
        rcode = save_ns(statp, &msg, ns_s_an, zname, class, nsrrsp);
 
390
        if (rcode != ISC_R_SUCCESS) {
 
391
                DPRINTF(("get_ns save_ns('%s', %s) failed",
 
392
                         zname, p_class(class)));
 
393
                return rcode;
 
394
        }
 
395
 
 
396
        return ISC_R_SUCCESS;
 
397
}
 
398
 
 
399
static isc_result_t
 
400
get_glue(res_state statp, ns_class class, rrset_ns *nsrrsp) {
 
401
        rr_ns *nsrr, *nsrr_n;
 
402
 
 
403
        /* Go and get the A RRs for each empty NS RR on our list. */
 
404
        for (nsrr = ISC_LIST_HEAD(*nsrrsp); nsrr != NULL; nsrr = nsrr_n) {
 
405
                double resp[NS_PACKETSZ / sizeof (double)];
 
406
                ns_msg msg;
 
407
                int n;
 
408
                isc_result_t rcode;
 
409
 
 
410
                nsrr_n = ISC_LIST_NEXT(nsrr, link);
 
411
 
 
412
                if (ISC_LIST_EMPTY(nsrr->addrs)) {
 
413
                        rcode = do_query(statp, nsrr->name, class, ns_t_a,
 
414
                                         resp, &msg, &n);
 
415
                        if (rcode != ISC_R_SUCCESS) {
 
416
                            DPRINTF(("get_glue: do_query('%s', %s') failed",
 
417
                                     nsrr->name, p_class(class)));
 
418
                            return rcode;
 
419
                        }
 
420
                        if (n > 0) {
 
421
                                DPRINTF((
 
422
                        "get_glue: do_query('%s', %s') CNAME or DNAME found",
 
423
                                         nsrr->name, p_class(class)));
 
424
                        }
 
425
                        rcode = save_a(statp, &msg, ns_s_an, nsrr->name, class,
 
426
                                       &nsrr->addrs);
 
427
                        if (rcode != ISC_R_SUCCESS) {
 
428
                                DPRINTF(("get_glue: save_r('%s', %s) failed",
 
429
                                         nsrr->name, p_class(class)));
 
430
                                return rcode;
 
431
                        }
 
432
                        /* If it's still empty, it's just chaff. */
 
433
                        if (ISC_LIST_EMPTY(nsrr->addrs)) {
 
434
                                DPRINTF(("get_glue: removing empty '%s' NS",
 
435
                                         nsrr->name));
 
436
                                free_nsrr(nsrrsp, nsrr);
 
437
                        }
 
438
                }
 
439
        }
 
440
        return ISC_R_SUCCESS;
 
441
}
 
442
 
 
443
static isc_result_t
 
444
save_ns(res_state statp, ns_msg *msg, ns_sect sect,
 
445
        const char *owner, ns_class class,
 
446
        rrset_ns *nsrrsp)
 
447
{
 
448
        int i;
 
449
        isc_result_t rcode;
 
450
 
 
451
        for (i = 0; i < ns_msg_count(*msg, sect); i++) {
 
452
                char tname[MAXDNAME];
 
453
                const u_char *rdata;
 
454
                rr_ns *nsrr;
 
455
                ns_rr rr;
 
456
                int rdlen;
 
457
 
 
458
                rcode = ns_parserr(msg, sect, i, &rr);
 
459
                if (rcode != ISC_R_SUCCESS) {
 
460
                        DPRINTF(("save_ns: ns_parserr(%s, %d) failed",
 
461
                                 p_section(sect, ns_o_query), i));
 
462
                        return rcode;
 
463
                }
 
464
                if (ns_rr_type(rr) != ns_t_ns ||
 
465
                    ns_rr_class(rr) != class ||
 
466
                    ns_samename(ns_rr_name(rr), owner) != 1)
 
467
                        continue;
 
468
                nsrr = find_ns(nsrrsp, ns_rr_name(rr));
 
469
                if (nsrr == NULL) {
 
470
                        nsrr = malloc(sizeof *nsrr);
 
471
                        if (nsrr == NULL) {
 
472
                                DPRINTF(("save_ns: malloc failed"));
 
473
                                return ISC_R_NOMEMORY;
 
474
                        }
 
475
                        rdata = ns_rr_rdata(rr);
 
476
                        rdlen = ns_rr_rdlen(rr);
 
477
                        if (ns_name_uncompress(ns_msg_base(*msg),
 
478
                                               ns_msg_end(*msg), rdata,
 
479
                                               tname, sizeof tname) < 0) {
 
480
                                DPRINTF(("save_ns: ns_name_uncompress failed"));
 
481
                                free(nsrr);
 
482
                                return ISC_R_NOMEMORY;
 
483
                        }
 
484
                        nsrr->name = strdup(tname);
 
485
                        if (nsrr->name == NULL) {
 
486
                                DPRINTF(("save_ns: strdup failed"));
 
487
                                free(nsrr);
 
488
                                return ISC_R_NOMEMORY;
 
489
                        }
 
490
                        ISC_LIST_INIT(nsrr->addrs);
 
491
                        ISC_LIST_APPEND(*nsrrsp, nsrr, link);
 
492
                }
 
493
                rcode = save_a(statp, msg, ns_s_ar,
 
494
                               nsrr->name, class, &nsrr->addrs);
 
495
                if (rcode != ISC_R_SUCCESS) {
 
496
                        DPRINTF(("save_ns: save_r('%s', %s) failed",
 
497
                                 nsrr->name, p_class(class)));
 
498
                        return rcode;
 
499
                }
 
500
        }
 
501
        return ISC_R_SUCCESS;
 
502
}
 
503
 
 
504
static isc_result_t
 
505
save_a(res_state statp, ns_msg *msg, ns_sect sect,
 
506
       const char *owner, ns_class class,
 
507
       rrset_a *arrsp)
 
508
{
 
509
        int i;
 
510
        isc_result_t rcode;
 
511
 
 
512
        for (i = 0; i < ns_msg_count(*msg, sect); i++) {
 
513
                ns_rr rr;
 
514
                rr_a *arr;
 
515
 
 
516
                rcode = ns_parserr(msg, sect, i, &rr);
 
517
                if (rcode != ISC_R_SUCCESS) {
 
518
                        DPRINTF(("save_a: ns_parserr(%s, %d) failed",
 
519
                                 p_section(sect, ns_o_query), i));
 
520
                        return rcode;
 
521
                }
 
522
                if (ns_rr_type(rr) != ns_t_a ||
 
523
                    ns_rr_class(rr) != class ||
 
524
                    ns_samename(ns_rr_name(rr), owner) != 1 ||
 
525
                    ns_rr_rdlen(rr) != NS_INADDRSZ)
 
526
                        continue;
 
527
                arr = malloc(sizeof *arr);
 
528
                if (arr == NULL) {
 
529
                        DPRINTF(("save_a: malloc failed"));
 
530
                        return ISC_R_NOMEMORY;
 
531
                }
 
532
                memcpy(&arr->addr, ns_rr_rdata(rr), NS_INADDRSZ);
 
533
                ISC_LIST_APPEND(*arrsp, arr, link);
 
534
        }
 
535
        return ISC_R_SUCCESS;
 
536
}
 
537
 
 
538
static void
 
539
free_nsrrset(rrset_ns *nsrrsp) {
 
540
        rr_ns *nsrr;
 
541
 
 
542
        while ((nsrr = ISC_LIST_HEAD(*nsrrsp)) != NULL)
 
543
                free_nsrr(nsrrsp, nsrr);
 
544
}
 
545
 
 
546
static void
 
547
free_nsrr(rrset_ns *nsrrsp, rr_ns *nsrr) {
 
548
        rr_a *arr;
 
549
 
 
550
        while ((arr = ISC_LIST_HEAD(nsrr->addrs)) != NULL) {
 
551
                ISC_LIST_UNLINK(nsrr->addrs, arr, link);
 
552
                free(arr);
 
553
        }
 
554
        free((char *)nsrr->name);
 
555
        ISC_LIST_UNLINK(*nsrrsp, nsrr, link);
 
556
        free(nsrr);
 
557
}
 
558
 
 
559
static rr_ns *
 
560
find_ns(rrset_ns *nsrrsp, const char *dname) {
 
561
        rr_ns *nsrr;
 
562
 
 
563
        for (nsrr = ISC_LIST_HEAD(*nsrrsp);
 
564
             nsrr != NULL; nsrr = ISC_LIST_NEXT(nsrr, link))
 
565
                if (ns_samename(nsrr->name, dname) == 1)
 
566
                        return (nsrr);
 
567
        return (NULL);
 
568
}
 
569
 
 
570
static isc_result_t
 
571
do_query(res_state statp, const char *dname, ns_class class, ns_type qtype,
 
572
         double *resp, ns_msg *msg, int *alias_count)
 
573
{
 
574
        double req[NS_PACKETSZ / sizeof (double)];
 
575
        int i;
 
576
        unsigned n;
 
577
        isc_result_t status;
 
578
 
 
579
        status = res_nmkquery(statp, ns_o_query, dname, class, qtype,
 
580
                              NULL, 0, NULL, req, NS_PACKETSZ, &n);
 
581
        if (status != ISC_R_SUCCESS) {
 
582
                DPRINTF(("do_query: res_nmkquery failed"));
 
583
                return status;
 
584
        }
 
585
        status = res_nsend(statp, req, n, resp, NS_PACKETSZ, &n);
 
586
        if (status != ISC_R_SUCCESS) {
 
587
                DPRINTF(("do_query: res_nsend failed"));
 
588
                return status;
 
589
        }
 
590
        if (n == 0) {
 
591
                DPRINTF(("do_query: res_nsend returned 0"));
 
592
                return ISC_R_NOTFOUND;
 
593
        }
 
594
        if (ns_initparse((u_char *)resp, n, msg) < 0) {
 
595
                DPRINTF(("do_query: ns_initparse failed"));
 
596
                return ISC_R_NOSPACE;
 
597
        }
 
598
        n = 0;
 
599
        for (i = 0; i < ns_msg_count(*msg, ns_s_an); i++) {
 
600
                ns_rr rr;
 
601
 
 
602
                status = ns_parserr(msg, ns_s_an, i, &rr);
 
603
                if (status != ISC_R_SUCCESS) {
 
604
                        DPRINTF(("do_query: ns_parserr failed"));
 
605
                        return status;
 
606
                }
 
607
                n += (ns_rr_class(rr) == class &&
 
608
                      (ns_rr_type(rr) == ns_t_cname ||
 
609
                       ns_rr_type(rr) == ns_t_dname));
 
610
        }
 
611
        if (alias_count)
 
612
                *alias_count = n;
 
613
        return ISC_R_SUCCESS;
 
614
}