1 /*	$NetBSD: makejournal.c,v 1.2 2018/08/12 13:02:28 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 /*! \file */
15 #include <config.h>
16 
17 #include <isc/entropy.h>
18 #include <isc/hash.h>
19 #include <isc/log.h>
20 #include <isc/mem.h>
21 #include <isc/util.h>
22 
23 #include <dns/db.h>
24 #include <dns/fixedname.h>
25 #include <dns/journal.h>
26 #include <dns/log.h>
27 #include <dns/name.h>
28 #include <isc/print.h>
29 #include <dns/result.h>
30 #include <dns/types.h>
31 
32 #include <stdlib.h>
33 
34 #define CHECK(r) \
35 	do { \
36 		result = (r); \
37 		if (result != ISC_R_SUCCESS) \
38 		goto cleanup; \
39 	} while (/*CONSTCOND*/0)
40 
41 isc_mem_t *mctx = NULL;
42 isc_log_t *lctx = NULL;
43 isc_entropy_t *ectx = NULL;
44 
45 static isc_boolean_t hash_active = ISC_FALSE, dst_active = ISC_FALSE;
46 
47 /*
48  * Logging categories: this needs to match the list in bin/named/log.c.
49  */
50 static isc_logcategory_t categories[] = {
51 		{ "",                0 },
52 		{ "client",          0 },
53 		{ "network",         0 },
54 		{ "update",          0 },
55 		{ "queries",         0 },
56 		{ "unmatched",       0 },
57 		{ "update-security", 0 },
58 		{ "query-errors",    0 },
59 		{ NULL,              0 }
60 };
61 
62 static isc_result_t
63 loadzone(dns_db_t **db, const char *origin, const char *filename) {
64 	isc_result_t result;
65 	dns_fixedname_t fixed;
66 	dns_name_t *name;
67 
68 	name = dns_fixedname_initname(&fixed);
69 
70 	result = dns_name_fromstring(name, origin, 0, NULL);
71 	if (result != ISC_R_SUCCESS)
72 		return(result);
73 
74 	result = dns_db_create(mctx, "rbt", name, dns_dbtype_zone,
75 			       dns_rdataclass_in, 0, NULL, db);
76 	if (result != ISC_R_SUCCESS)
77 		return (result);
78 
79 	result = dns_db_load(*db, filename);
80 	return (result);
81 }
82 
83 int
84 main(int argc, char **argv) {
85 	isc_result_t result;
86 	char *origin, *file1, *file2, *journal;
87 	dns_db_t *olddb = NULL, *newdb = NULL;
88 	isc_logdestination_t destination;
89 	isc_logconfig_t *logconfig = NULL;
90 
91 	if (argc != 5) {
92 		printf("usage: %s origin file1 file2 journal\n", argv[0]);
93 		return (1);
94 	}
95 
96 	origin = argv[1];
97 	file1 = argv[2];
98 	file2 = argv[3];
99 	journal = argv[4];
100 
101 	isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
102 	CHECK(isc_mem_create(0, 0, &mctx));
103 	CHECK(isc_entropy_create(mctx, &ectx));
104 
105 	CHECK(dst_lib_init(mctx, ectx, ISC_ENTROPY_BLOCKING));
106 	dst_active = ISC_TRUE;
107 
108 	CHECK(isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE));
109 	hash_active = ISC_TRUE;
110 
111 	CHECK(isc_log_create(mctx, &lctx, &logconfig));
112 	isc_log_registercategories(lctx, categories);
113 	isc_log_setcontext(lctx);
114 	dns_log_init(lctx);
115 	dns_log_setcontext(lctx);
116 
117 	destination.file.stream = stderr;
118 	destination.file.name = NULL;
119 	destination.file.versions = ISC_LOG_ROLLNEVER;
120 	destination.file.maximum_size = 0;
121 	CHECK(isc_log_createchannel(logconfig, "stderr",
122 				    ISC_LOG_TOFILEDESC, ISC_LOG_DYNAMIC,
123 				    &destination, 0));
124 	CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL));
125 
126 	dns_result_register();
127 
128 	result = loadzone(&olddb, origin, file1);
129 	if (result != ISC_R_SUCCESS) {
130 		fprintf(stderr, "Couldn't load %s: ", file1);
131 		goto cleanup;
132 	}
133 
134 	result = loadzone(&newdb, origin, file2);
135 	if (result != ISC_R_SUCCESS) {
136 		fprintf(stderr, "Couldn't load %s: ", file2);
137 		goto cleanup;
138 	}
139 
140 	result = dns_db_diff(mctx, newdb, NULL, olddb, NULL, journal);
141 
142   cleanup:
143 	if (result != ISC_R_SUCCESS)
144 		fprintf(stderr, "%s\n", isc_result_totext(result));
145 
146 	if (newdb != NULL)
147 		dns_db_detach(&newdb);
148 	if (olddb != NULL)
149 		dns_db_detach(&olddb);
150 
151 	if (lctx != NULL)
152 		isc_log_destroy(&lctx);
153 	if (dst_active) {
154 		dst_lib_destroy();
155 		dst_active = ISC_FALSE;
156 	}
157 	if (hash_active) {
158 		isc_hash_destroy();
159 		hash_active = ISC_FALSE;
160 	}
161 	if (ectx != NULL)
162 		isc_entropy_detach(&ectx);
163 	if (mctx != NULL)
164 		isc_mem_destroy(&mctx);
165 
166 	return(result != ISC_R_SUCCESS ? 1 : 0);
167 }
168 
169