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
|
/*
* Copyright (C) 2002 Nuno M. Rodrigues.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND NUNO M. RODRIGUES
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <isc/mem.h>
#include <isc/result.h>
#include <isc/types.h>
#include <isc/util.h>
#include <dns/db.h>
#include <dns/dbiterator.h>
#include <dns/fixedname.h>
#include <dns/name.h>
#include <dns/rdata.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include <dns/rdatatype.h>
#include <dns/ttl.h>
#include <dns/types.h>
#include <db.h>
#define MAX_RDATATEXT 63 + 4 + 65535 + 2 /* ttl + type + rdata + sep */
/*
* Returns a valid 'DB' handle.
*
* Requires:
* 'file' is a valid non-existant path.
*/
DB *
bdb_init(const char *file)
{
DB *db;
REQUIRE(db_create(&db, NULL, 0) == 0);
REQUIRE(db->set_flags(db, DB_DUP) == 0);
REQUIRE(db->open(db, file, NULL, DB_HASH, DB_CREATE | DB_EXCL, 0) == 0);
return db;
}
/*
* Puts resource record data on 'db'.
*/
isc_result_t
bdb_putrdata(DB *db, dns_name_t *name, dns_ttl_t ttl, dns_rdata_t *rdata)
{
static DBT key, data;
isc_buffer_t keybuf, databuf;
char nametext[DNS_NAME_MAXTEXT];
char rdatatext[MAX_RDATATEXT];
isc_buffer_init(&keybuf, nametext, DNS_NAME_MAXTEXT);
dns_name_totext(name, true, &keybuf);
key.data = isc_buffer_base(&keybuf);
key.size = isc_buffer_usedlength(&keybuf);
isc_buffer_init(&databuf, rdatatext, MAX_RDATATEXT);
dns_ttl_totext(ttl, false, &databuf);
*(char *)isc_buffer_used(&databuf) = ' ';
isc_buffer_add(&databuf, 1);
dns_rdatatype_totext(rdata->type, &databuf); /* XXX private data */
*(char *)isc_buffer_used(&databuf) = ' ';
isc_buffer_add(&databuf, 1);
dns_rdata_totext(rdata, NULL, &databuf);
data.data = isc_buffer_base(&databuf);
data.size = isc_buffer_usedlength(&databuf);
REQUIRE(db->put(db, NULL, &key, &data, 0) == 0);
return ISC_R_SUCCESS;
}
isc_result_t
bdb_destroy(DB *db)
{
return (db->close(db, 0) == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE;
}
void
usage(const char *prog)
{
fprintf(stderr, "Usage: %s <origin> <zonefile> <db>\n", prog);
exit(1);
}
int
main(int argc, char *argv[])
{
isc_mem_t *mctx = NULL;
isc_buffer_t b;
int n;
dns_fixedname_t origin, name;
dns_db_t *db = NULL;
dns_dbiterator_t *dbiter = NULL;
isc_result_t res;
dns_dbnode_t *node = NULL;
dns_rdataset_t rdataset;
dns_rdatasetiter_t *rdatasetiter = NULL;
dns_rdata_t rdata;
DB *bdb;
if (argc != 4) usage(*argv);
REQUIRE(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
n = strlen(argv[1]);
isc_buffer_init(&b, argv[1], n);
isc_buffer_add(&b, n);
dns_fixedname_init(&origin);
REQUIRE(dns_name_fromtext(dns_fixedname_name(&origin), &b, dns_rootname,
0, NULL) == ISC_R_SUCCESS);
REQUIRE(dns_db_create(mctx, "rbt", dns_fixedname_name(&origin),
dns_dbtype_zone, dns_rdataclass_in, 0, NULL,
&db) == ISC_R_SUCCESS);
REQUIRE(dns_db_load(db, argv[2]) == ISC_R_SUCCESS);
REQUIRE(dns_db_createiterator(db, 0, &dbiter) == ISC_R_SUCCESS);
dns_rdataset_init(&rdataset);
dns_rdata_init(&rdata);
dns_fixedname_init(&name);
bdb = bdb_init(argv[3]);
for (res = dns_dbiterator_first(dbiter); res == ISC_R_SUCCESS;
res = dns_dbiterator_next(dbiter)) {
dns_dbiterator_current(dbiter, &node, dns_fixedname_name(&name));
REQUIRE(dns_db_allrdatasets(db, node, NULL, 0, &rdatasetiter)
== ISC_R_SUCCESS);
for (res = dns_rdatasetiter_first(rdatasetiter);
res == ISC_R_SUCCESS;
res = dns_rdatasetiter_next(rdatasetiter)) {
dns_rdatasetiter_current(rdatasetiter, &rdataset);
res = dns_rdataset_first(&rdataset);
while (res == ISC_R_SUCCESS) {
dns_rdataset_current(&rdataset, &rdata);
REQUIRE(bdb_putrdata(bdb,
dns_fixedname_name(&name),
rdataset.ttl, &rdata)
== ISC_R_SUCCESS);
dns_rdata_reset(&rdata);
res = dns_rdataset_next(&rdataset);
}
dns_rdataset_disassociate(&rdataset);
}
dns_rdatasetiter_destroy(&rdatasetiter);
dns_db_detachnode(db, &node);
}
dns_dbiterator_destroy(&dbiter);
REQUIRE(bdb_destroy(bdb) == ISC_R_SUCCESS);
return 0;
}
|