Merge from vendor branch BSDTAR:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / rootns.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001, 2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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: rootns.c,v 1.20.2.5 2004/03/09 06:11:07 marka Exp $ */
19
20 #include <config.h>
21
22 #include <isc/buffer.h>
23 #include <isc/string.h>         /* Required for HP/UX (and others?) */
24 #include <isc/util.h>
25
26 #include <dns/callbacks.h>
27 #include <dns/db.h>
28 #include <dns/dbiterator.h>
29 #include <dns/log.h>
30 #include <dns/fixedname.h>
31 #include <dns/master.h>
32 #include <dns/rdata.h>
33 #include <dns/rdatasetiter.h>
34 #include <dns/rdataset.h>
35 #include <dns/rdatastruct.h>
36 #include <dns/result.h>
37 #include <dns/rootns.h>
38
39 static char root_ns[] =
40 ";\n"
41 "; Internet Root Nameservers\n"
42 ";\n"
43 "; Thu Sep 23 17:57:37 PDT 1999\n"
44 ";\n"
45 "$TTL 518400\n"
46 ".                       518400  IN      NS      A.ROOT-SERVERS.NET.\n"
47 ".                       518400  IN      NS      B.ROOT-SERVERS.NET.\n"
48 ".                       518400  IN      NS      C.ROOT-SERVERS.NET.\n"
49 ".                       518400  IN      NS      D.ROOT-SERVERS.NET.\n"
50 ".                       518400  IN      NS      E.ROOT-SERVERS.NET.\n"
51 ".                       518400  IN      NS      F.ROOT-SERVERS.NET.\n"
52 ".                       518400  IN      NS      G.ROOT-SERVERS.NET.\n"
53 ".                       518400  IN      NS      H.ROOT-SERVERS.NET.\n"
54 ".                       518400  IN      NS      I.ROOT-SERVERS.NET.\n"
55 ".                       518400  IN      NS      J.ROOT-SERVERS.NET.\n"
56 ".                       518400  IN      NS      K.ROOT-SERVERS.NET.\n"
57 ".                       518400  IN      NS      L.ROOT-SERVERS.NET.\n"
58 ".                       518400  IN      NS      M.ROOT-SERVERS.NET.\n"
59 "A.ROOT-SERVERS.NET.     3600000 IN      A       198.41.0.4\n"
60 "B.ROOT-SERVERS.NET.     3600000 IN      A       192.228.79.201\n"
61 "C.ROOT-SERVERS.NET.     3600000 IN      A       192.33.4.12\n"
62 "D.ROOT-SERVERS.NET.     3600000 IN      A       128.8.10.90\n"
63 "E.ROOT-SERVERS.NET.     3600000 IN      A       192.203.230.10\n"
64 "F.ROOT-SERVERS.NET.     3600000 IN      A       192.5.5.241\n"
65 "G.ROOT-SERVERS.NET.     3600000 IN      A       192.112.36.4\n"
66 "H.ROOT-SERVERS.NET.     3600000 IN      A       128.63.2.53\n"
67 "I.ROOT-SERVERS.NET.     3600000 IN      A       192.36.148.17\n"
68 "J.ROOT-SERVERS.NET.     3600000 IN      A       192.58.128.30\n"
69 "K.ROOT-SERVERS.NET.     3600000 IN      A       193.0.14.129\n"
70 "L.ROOT-SERVERS.NET.     3600000 IN      A       198.32.64.12\n"
71 "M.ROOT-SERVERS.NET.     3600000 IN      A       202.12.27.33\n";
72
73 static isc_result_t
74 in_rootns(dns_rdataset_t *rootns, dns_name_t *name) {
75         isc_result_t result;
76         dns_rdata_t rdata = DNS_RDATA_INIT;
77         dns_rdata_ns_t ns;
78         
79         if (!dns_rdataset_isassociated(rootns))
80                 return (ISC_R_NOTFOUND);
81
82         result = dns_rdataset_first(rootns);
83         while (result == ISC_R_SUCCESS) {
84                 dns_rdataset_current(rootns, &rdata);
85                 result = dns_rdata_tostruct(&rdata, &ns, NULL);
86                 if (result != ISC_R_SUCCESS)
87                         return (result);
88                 if (dns_name_compare(name, &ns.name) == 0)
89                         return (ISC_R_SUCCESS);
90                 result = dns_rdataset_next(rootns);
91         }
92         if (result == ISC_R_NOMORE)
93                 result = ISC_R_NOTFOUND;
94         return (result);
95 }
96
97 static isc_result_t 
98 check_node(dns_rdataset_t *rootns, dns_name_t *name,
99            dns_rdatasetiter_t *rdsiter) {
100         isc_result_t result;
101         dns_rdataset_t rdataset;
102
103         dns_rdataset_init(&rdataset);
104         result = dns_rdatasetiter_first(rdsiter);
105         while (result == ISC_R_SUCCESS) {
106                 dns_rdatasetiter_current(rdsiter, &rdataset);
107                 switch (rdataset.type) {
108                 case dns_rdatatype_a:
109                 case dns_rdatatype_aaaa:
110                 case dns_rdatatype_a6:
111                         result = in_rootns(rootns, name);
112                         if (result != ISC_R_SUCCESS)
113                                 goto cleanup;
114                         break;
115                 case dns_rdatatype_ns:
116                         if (dns_name_compare(name, dns_rootname) == 0)
117                                 break;
118                         /*FALLTHROUGH*/
119                 default:
120                         result = ISC_R_FAILURE;
121                         goto cleanup;
122                 }
123                 dns_rdataset_disassociate(&rdataset);
124                 result = dns_rdatasetiter_next(rdsiter);
125         }
126         if (result == ISC_R_NOMORE)
127                 result = ISC_R_SUCCESS;
128  cleanup:
129         if (dns_rdataset_isassociated(&rdataset))
130                 dns_rdataset_disassociate(&rdataset);
131         return (result);
132 }
133
134 static isc_result_t
135 check_hints(dns_db_t *db) {
136         isc_result_t result;
137         dns_rdataset_t rootns;
138         dns_dbiterator_t *dbiter = NULL;
139         dns_dbnode_t *node = NULL;
140         isc_stdtime_t now;
141         dns_fixedname_t fixname;
142         dns_name_t *name;
143         dns_rdatasetiter_t *rdsiter = NULL;
144
145         isc_stdtime_get(&now);
146
147         dns_fixedname_init(&fixname);
148         name = dns_fixedname_name(&fixname);
149
150         dns_rdataset_init(&rootns);
151         (void)dns_db_find(db, dns_rootname, NULL, dns_rdatatype_ns, 0,
152                           now, NULL, name, &rootns, NULL);
153         result = dns_db_createiterator(db, ISC_FALSE, &dbiter);
154         if (result != ISC_R_SUCCESS)
155                 goto cleanup;
156         result = dns_dbiterator_first(dbiter);
157         while (result == ISC_R_SUCCESS) {
158                 result = dns_dbiterator_current(dbiter, &node, name);
159                 if (result != ISC_R_SUCCESS)
160                         goto cleanup;
161                 result = dns_db_allrdatasets(db, node, NULL, now, &rdsiter);
162                 if (result != ISC_R_SUCCESS)
163                         goto cleanup;
164                 result = check_node(&rootns, name, rdsiter);
165                 if (result != ISC_R_SUCCESS)
166                         goto cleanup;
167                 dns_rdatasetiter_destroy(&rdsiter);
168                 dns_db_detachnode(db, &node);
169                 result = dns_dbiterator_next(dbiter);
170         }
171         if (result == ISC_R_NOMORE)
172                 result = ISC_R_SUCCESS;
173
174  cleanup:
175         if (dns_rdataset_isassociated(&rootns))
176                 dns_rdataset_disassociate(&rootns);
177         if (rdsiter != NULL)
178                 dns_rdatasetiter_destroy(&rdsiter);
179         if (node != NULL)
180                 dns_db_detachnode(db, &node);
181         if (dbiter != NULL)
182                 dns_dbiterator_destroy(&dbiter);
183         return (result);
184 }
185
186 isc_result_t
187 dns_rootns_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
188                   const char *filename, dns_db_t **target)
189 {
190         isc_result_t result, eresult;
191         isc_buffer_t source;
192         size_t len;
193         dns_rdatacallbacks_t callbacks;
194         dns_db_t *db = NULL;
195
196         REQUIRE(target != NULL && *target == NULL);
197
198         result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_zone,
199                                rdclass, 0, NULL, &db);
200         if (result != ISC_R_SUCCESS)
201                 return (result);
202
203         dns_rdatacallbacks_init(&callbacks);
204
205         len = strlen(root_ns);
206         isc_buffer_init(&source, root_ns, len);
207         isc_buffer_add(&source, len);
208
209         result = dns_db_beginload(db, &callbacks.add,
210                                   &callbacks.add_private);
211         if (result != ISC_R_SUCCESS)
212                 return (result);
213         if (filename != NULL) {
214                 /*
215                  * Load the hints from the specified filename.
216                  */
217                 result = dns_master_loadfile(filename, &db->origin,
218                                              &db->origin, db->rdclass, 0,
219                                              &callbacks, db->mctx);
220         } else if (rdclass == dns_rdataclass_in) {
221                 /*
222                  * Default to using the Internet root servers.
223                  */
224                 result = dns_master_loadbuffer(&source, &db->origin,
225                                                &db->origin, db->rdclass, 0,
226                                                &callbacks, db->mctx);
227         } else
228                 result = ISC_R_NOTFOUND;
229         eresult = dns_db_endload(db, &callbacks.add_private);
230         if (result == ISC_R_SUCCESS || result == DNS_R_SEENINCLUDE)
231                 result = eresult;
232         if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE)
233                 goto db_detach;
234         if (check_hints(db) != ISC_R_SUCCESS)
235                 isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
236                               DNS_LOGMODULE_HINTS, ISC_LOG_WARNING,
237                               "extra data in root hints '%s'",
238                               (filename != NULL) ? filename : "<BUILT-IN>");
239         *target = db;
240         return (ISC_R_SUCCESS);
241
242  db_detach:
243         dns_db_detach(&db);
244
245         return (result);
246 }