2000-11-17 22:01:45 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-11-17 22:01:45 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2000-11-17 22:01:45 +00:00
|
|
|
*/
|
|
|
|
|
2009-09-02 23:48:03 +00:00
|
|
|
/* $Id: zonetodb.c,v 1.23 2009/09/02 23:48:01 tbox Exp $ */
|
2002-03-19 15:57:23 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-11-17 22:01:45 +00:00
|
|
|
|
2000-11-17 21:54:02 +00:00
|
|
|
#include <isc/buffer.h>
|
2005-08-16 04:22:37 +00:00
|
|
|
#include <isc/entropy.h>
|
|
|
|
#include <isc/hash.h>
|
2000-11-17 21:54:02 +00:00
|
|
|
#include <isc/mem.h>
|
2000-11-20 13:02:18 +00:00
|
|
|
#include <isc/print.h>
|
2000-11-17 21:54:02 +00:00
|
|
|
#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>
|
2002-03-19 15:57:23 +00:00
|
|
|
#include <dns/rdatatype.h>
|
2000-11-17 21:54:02 +00:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-11-17 23:12:11 +00:00
|
|
|
/*
|
|
|
|
* Canonicalize a string before writing it to the database.
|
|
|
|
* "dest" must be an array of at least size 2*strlen(source) + 1.
|
|
|
|
*/
|
|
|
|
static void
|
2014-08-20 23:14:03 +10:00
|
|
|
quotestring(const unsigned char *source, unsigned char *dest) {
|
2000-11-17 23:12:11 +00:00
|
|
|
while (*source != 0) {
|
2000-11-20 19:56:12 +00:00
|
|
|
if (*source == '\'')
|
|
|
|
*dest++ = '\'';
|
|
|
|
else if (*source == '\\')
|
2000-11-17 23:12:11 +00:00
|
|
|
*dest++ = '\\';
|
|
|
|
*dest++ = *source++;
|
|
|
|
}
|
|
|
|
*dest++ = 0;
|
|
|
|
}
|
|
|
|
|
2000-11-17 21:54:02 +00:00
|
|
|
void
|
2000-11-17 23:57:33 +00:00
|
|
|
addrdata(dns_name_t *name, dns_ttl_t ttl, dns_rdata_t *rdata) {
|
2000-11-17 21:54:02 +00:00
|
|
|
unsigned char namearray[DNS_NAME_MAXTEXT + 1];
|
2000-11-17 23:12:11 +00:00
|
|
|
unsigned char canonnamearray[2 * DNS_NAME_MAXTEXT + 1];
|
2000-11-17 21:54:02 +00:00
|
|
|
unsigned char typearray[20];
|
2000-11-17 23:12:11 +00:00
|
|
|
unsigned char canontypearray[40];
|
2000-11-17 21:54:02 +00:00
|
|
|
unsigned char dataarray[2048];
|
2000-11-17 23:12:11 +00:00
|
|
|
unsigned char canondataarray[4096];
|
2000-11-17 21:54:02 +00:00
|
|
|
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;
|
2014-08-20 23:14:03 +10:00
|
|
|
quotestring((const unsigned char *)namearray, canonnamearray);
|
2008-09-25 04:02:39 +00:00
|
|
|
|
2000-11-17 21:54:02 +00:00
|
|
|
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;
|
2014-08-20 23:14:03 +10:00
|
|
|
quotestring((const unsigned char *)typearray, canontypearray);
|
2000-11-17 21:54:02 +00:00
|
|
|
|
|
|
|
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;
|
2014-08-20 23:14:03 +10:00
|
|
|
quotestring((const unsigned char *)dataarray, canondataarray);
|
2008-09-25 04:02:39 +00:00
|
|
|
|
2000-11-17 21:54:02 +00:00
|
|
|
snprintf(str, sizeof(str),
|
2000-11-17 23:57:33 +00:00
|
|
|
"INSERT INTO %s (NAME, TTL, RDTYPE, RDATA)"
|
|
|
|
" VALUES ('%s', %d, '%s', '%s')",
|
|
|
|
dbtable, canonnamearray, ttl, canontypearray, canondataarray);
|
2000-11-17 21:54:02 +00:00
|
|
|
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;
|
2000-12-11 23:09:47 +00:00
|
|
|
dns_rdata_t rdata = DNS_RDATA_INIT;
|
2000-11-17 21:54:02 +00:00
|
|
|
isc_mem_t *mctx = NULL;
|
2005-08-16 04:22:37 +00:00
|
|
|
isc_entropy_t *ectx = NULL;
|
2000-11-17 21:54:02 +00:00
|
|
|
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];
|
|
|
|
|
2002-03-19 03:32:08 +00:00
|
|
|
dns_result_register();
|
|
|
|
|
2000-11-17 21:54:02 +00:00
|
|
|
mctx = NULL;
|
|
|
|
result = isc_mem_create(0, 0, &mctx);
|
|
|
|
check_result(result, "isc_mem_create");
|
|
|
|
|
2005-08-16 04:22:37 +00:00
|
|
|
result = isc_entropy_create(mctx, &ectx);
|
2008-11-27 06:14:22 +00:00
|
|
|
check_result(result, "isc_entropy_create");
|
2005-08-16 04:22:37 +00:00
|
|
|
|
2000-11-17 21:54:02 +00:00
|
|
|
isc_buffer_init(&b, porigin, strlen(porigin));
|
|
|
|
isc_buffer_add(&b, strlen(porigin));
|
2018-03-28 14:38:09 +02:00
|
|
|
origin = dns_fixedname_initname(&forigin);
|
2009-09-01 00:22:28 +00:00
|
|
|
result = dns_name_fromtext(origin, &b, dns_rootname, 0, NULL);
|
2000-11-17 21:54:02 +00:00
|
|
|
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");
|
|
|
|
|
2018-04-03 13:22:09 +02:00
|
|
|
result = dns_db_load(db, zonefile, dns_masterformat_text, 0);
|
2002-03-19 03:32:08 +00:00
|
|
|
if (result == DNS_R_SEENINCLUDE)
|
|
|
|
result = ISC_R_SUCCESS;
|
2000-11-17 21:54:02 +00:00
|
|
|
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),
|
2000-11-17 23:57:33 +00:00
|
|
|
"CREATE TABLE %s "
|
|
|
|
"(NAME TEXT, TTL INTEGER, RDTYPE TEXT, RDATA TEXT)",
|
2000-11-17 21:54:02 +00:00
|
|
|
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;
|
2008-09-24 02:46:23 +00:00
|
|
|
result = dns_db_createiterator(db, 0, &dbiter);
|
2000-11-17 21:54:02 +00:00
|
|
|
check_result(result, "dns_db_createiterator()");
|
|
|
|
|
|
|
|
result = dns_dbiterator_first(dbiter);
|
|
|
|
check_result(result, "dns_dbiterator_first");
|
|
|
|
|
2018-03-28 14:38:09 +02:00
|
|
|
name = dns_fixedname_initname(&fname);
|
2000-11-17 21:54:02 +00:00
|
|
|
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);
|
2000-11-17 23:57:33 +00:00
|
|
|
addrdata(name, rdataset.ttl, &rdata);
|
2000-11-17 21:54:02 +00:00
|
|
|
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);
|
2005-08-16 04:22:37 +00:00
|
|
|
isc_entropy_detach(&ectx);
|
2000-11-17 21:54:02 +00:00
|
|
|
isc_mem_destroy(&mctx);
|
|
|
|
closeandexit(0);
|
2002-03-19 15:57:23 +00:00
|
|
|
exit(0);
|
2000-11-17 21:54:02 +00:00
|
|
|
}
|