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
|
/*
* Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* 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 ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC 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.
*/
/* $Id: zonetodb.c,v 1.12.4.2.8.6 2005/09/06 02:12:40 marka Exp $ */
#include <stdlib.h>
#include <string.h>
#include <isc/buffer.h>
#include <isc/entropy.h>
#include <isc/hash.h>
#include <isc/mem.h>
#include <isc/print.h>
#include <isc/result.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/result.h>
#include <pgsql/libpq-fe.h>
/*
* Generate a PostgreSQL table from a zone.
*
* This is compiled this with something like the following (assuming bind9 has
* been installed):
*
* gcc -g `isc-config.sh --cflags isc dns` -c zonetodb.c
* gcc -g -o zonetodb zonetodb.o `isc-config.sh --libs isc dns` -lpq
*/
PGconn *conn = NULL;
char *dbname, *dbtable;
char str[10240];
void
closeandexit(int status) {
if (conn != NULL)
PQfinish(conn);
exit(status);
}
void
check_result(isc_result_t result, const char *message) {
if (result != ISC_R_SUCCESS) {
fprintf(stderr, "%s: %s\n", message,
isc_result_totext(result));
closeandexit(1);
}
}
/*
* Canonicalize a string before writing it to the database.
* "dest" must be an array of at least size 2*strlen(source) + 1.
*/
static void
quotestring(const char *source, char *dest) {
while (*source != 0) {
if (*source == '\'')
*dest++ = '\'';
else if (*source == '\\')
*dest++ = '\\';
*dest++ = *source++;
}
*dest++ = 0;
}
void
addrdata(dns_name_t *name, dns_ttl_t ttl, dns_rdata_t *rdata) {
unsigned char namearray[DNS_NAME_MAXTEXT + 1];
unsigned char canonnamearray[2 * DNS_NAME_MAXTEXT + 1];
unsigned char typearray[20];
unsigned char canontypearray[40];
unsigned char dataarray[2048];
unsigned char canondataarray[4096];
isc_buffer_t b;
isc_result_t result;
PGresult *res;
isc_buffer_init(&b, namearray, sizeof(namearray) - 1);
result = dns_name_totext(name, ISC_TRUE, &b);
check_result(result, "dns_name_totext");
namearray[isc_buffer_usedlength(&b)] = 0;
quotestring(namearray, canonnamearray);
isc_buffer_init(&b, typearray, sizeof(typearray) - 1);
result = dns_rdatatype_totext(rdata->type, &b);
check_result(result, "dns_rdatatype_totext");
typearray[isc_buffer_usedlength(&b)] = 0;
quotestring(typearray, canontypearray);
isc_buffer_init(&b, dataarray, sizeof(dataarray) - 1);
result = dns_rdata_totext(rdata, NULL, &b);
check_result(result, "dns_rdata_totext");
dataarray[isc_buffer_usedlength(&b)] = 0;
quotestring(dataarray, canondataarray);
snprintf(str, sizeof(str),
"INSERT INTO %s (NAME, TTL, RDTYPE, RDATA)"
" VALUES ('%s', %d, '%s', '%s')",
dbtable, canonnamearray, ttl, canontypearray, canondataarray);
printf("%s\n", str);
res = PQexec(conn, str);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "INSERT INTO command failed: %s\n",
PQresultErrorMessage(res));
PQclear(res);
closeandexit(1);
}
PQclear(res);
}
int
main(int argc, char **argv) {
char *porigin, *zonefile;
dns_fixedname_t forigin, fname;
dns_name_t *origin, *name;
dns_db_t *db = NULL;
dns_dbiterator_t *dbiter;
dns_dbnode_t *node;
dns_rdatasetiter_t *rdsiter;
dns_rdataset_t rdataset;
dns_rdata_t rdata = DNS_RDATA_INIT;
isc_mem_t *mctx = NULL;
isc_entropy_t *ectx = NULL;
isc_buffer_t b;
isc_result_t result;
PGresult *res;
if (argc != 5) {
printf("usage: %s origin file dbname dbtable\n", argv[0]);
printf("Note that dbname must be an existing database.\n");
exit(1);
}
porigin = argv[1];
zonefile = argv[2];
dbname = argv[3];
dbtable = argv[4];
dns_result_register();
mctx = NULL;
result = isc_mem_create(0, 0, &mctx);
check_result(result, "isc_mem_create");
result = isc_entropy_create(mctx, &ectx);
result_check (result, "isc_entropy_create");
result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
check_result (result, "isc_hash_create");
isc_buffer_init(&b, porigin, strlen(porigin));
isc_buffer_add(&b, strlen(porigin));
dns_fixedname_init(&forigin);
origin = dns_fixedname_name(&forigin);
result = dns_name_fromtext(origin, &b, dns_rootname, ISC_FALSE, NULL);
check_result(result, "dns_name_fromtext");
db = NULL;
result = dns_db_create(mctx, "rbt", origin, dns_dbtype_zone,
dns_rdataclass_in, 0, NULL, &db);
check_result(result, "dns_db_create");
result = dns_db_load(db, zonefile);
if (result == DNS_R_SEENINCLUDE)
result = ISC_R_SUCCESS;
check_result(result, "dns_db_load");
printf("Connecting to '%s'\n", dbname);
conn = PQsetdb(NULL, NULL, NULL, NULL, dbname);
if (PQstatus(conn) == CONNECTION_BAD) {
fprintf(stderr, "Connection to database '%s' failed: %s\n",
dbname, PQerrorMessage(conn));
closeandexit(1);
}
snprintf(str, sizeof(str),
"DROP TABLE %s", dbtable);
printf("%s\n", str);
res = PQexec(conn, str);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
fprintf(stderr, "DROP TABLE command failed: %s\n",
PQresultErrorMessage(res));
PQclear(res);
snprintf(str, sizeof(str), "BEGIN");
printf("%s\n", str);
res = PQexec(conn, str);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "BEGIN command failed: %s\n",
PQresultErrorMessage(res));
PQclear(res);
closeandexit(1);
}
PQclear(res);
snprintf(str, sizeof(str),
"CREATE TABLE %s "
"(NAME TEXT, TTL INTEGER, RDTYPE TEXT, RDATA TEXT)",
dbtable);
printf("%s\n", str);
res = PQexec(conn, str);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "CREATE TABLE command failed: %s\n",
PQresultErrorMessage(res));
PQclear(res);
closeandexit(1);
}
PQclear(res);
dbiter = NULL;
result = dns_db_createiterator(db, ISC_FALSE, &dbiter);
check_result(result, "dns_db_createiterator()");
result = dns_dbiterator_first(dbiter);
check_result(result, "dns_dbiterator_first");
dns_fixedname_init(&fname);
name = dns_fixedname_name(&fname);
dns_rdataset_init(&rdataset);
dns_rdata_init(&rdata);
while (result == ISC_R_SUCCESS) {
node = NULL;
result = dns_dbiterator_current(dbiter, &node, name);
if (result == ISC_R_NOMORE)
break;
check_result(result, "dns_dbiterator_current");
rdsiter = NULL;
result = dns_db_allrdatasets(db, node, NULL, 0, &rdsiter);
check_result(result, "dns_db_allrdatasets");
result = dns_rdatasetiter_first(rdsiter);
while (result == ISC_R_SUCCESS) {
dns_rdatasetiter_current(rdsiter, &rdataset);
result = dns_rdataset_first(&rdataset);
check_result(result, "dns_rdataset_first");
while (result == ISC_R_SUCCESS) {
dns_rdataset_current(&rdataset, &rdata);
addrdata(name, rdataset.ttl, &rdata);
dns_rdata_reset(&rdata);
result = dns_rdataset_next(&rdataset);
}
dns_rdataset_disassociate(&rdataset);
result = dns_rdatasetiter_next(rdsiter);
}
dns_rdatasetiter_destroy(&rdsiter);
dns_db_detachnode(db, &node);
result = dns_dbiterator_next(dbiter);
}
snprintf(str, sizeof(str), "COMMIT TRANSACTION");
printf("%s\n", str);
res = PQexec(conn, str);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "COMMIT command failed: %s\n",
PQresultErrorMessage(res));
PQclear(res);
closeandexit(1);
}
PQclear(res);
dns_dbiterator_destroy(&dbiter);
dns_db_detach(&db);
isc_hash_destroy();
isc_entropy_detach(&ectx);
isc_mem_destroy(&mctx);
closeandexit(0);
exit(0);
}
|