1 /*	$NetBSD: makejournal.c,v 1.3 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*! \file */
20 #include <config.h>
21 
22 #include <isc/entropy.h>
23 #include <isc/hash.h>
24 #include <isc/log.h>
25 #include <isc/mem.h>
26 #include <isc/util.h>
27 
28 #include <dns/db.h>
29 #include <dns/fixedname.h>
30 #include <dns/journal.h>
31 #include <dns/log.h>
32 #include <dns/name.h>
33 #include <dns/result.h>
34 #include <dns/types.h>
35 
36 #include <stdlib.h>
37 
38 #define CHECK(r) \
39 	do { \
40 		result = (r); \
41 		if (result != ISC_R_SUCCESS) \
42 		goto cleanup; \
43 	} while (/*CONSTCOND*/0)
44 
45 isc_mem_t *mctx = NULL;
46 isc_log_t *lctx = NULL;
47 isc_entropy_t *ectx = NULL;
48 
49 static isc_boolean_t hash_active = ISC_FALSE, dst_active = ISC_FALSE;
50 
51 /*
52  * Logging categories: this needs to match the list in bin/named/log.c.
53  */
54 static isc_logcategory_t categories[] = {
55 		{ "",                0 },
56 		{ "client",          0 },
57 		{ "network",         0 },
58 		{ "update",          0 },
59 		{ "queries",         0 },
60 		{ "unmatched",       0 },
61 		{ "update-security", 0 },
62 		{ "query-errors",    0 },
63 		{ NULL,              0 }
64 };
65 
66 static isc_result_t
67 loadzone(dns_db_t **db, const char *origin, const char *filename) {
68 	isc_result_t result;
69 	dns_fixedname_t fixed;
70 	dns_name_t *name;
71 
72 	dns_fixedname_init(&fixed);
73 	name = dns_fixedname_name(&fixed);
74 
75 	result = dns_name_fromstring(name, origin, 0, NULL);
76 	if (result != ISC_R_SUCCESS)
77 		return(result);
78 
79 	result = dns_db_create(mctx, "rbt", name, dns_dbtype_zone,
80 			       dns_rdataclass_in, 0, NULL, db);
81 	if (result != ISC_R_SUCCESS)
82 		return (result);
83 
84 	result = dns_db_load(*db, filename);
85 	return (result);
86 }
87 
88 int
89 main(int argc, char **argv) {
90 	isc_result_t result;
91 	char *origin, *file1, *file2, *journal;
92 	dns_db_t *old = NULL, *new = NULL;
93 	isc_logdestination_t destination;
94 	isc_logconfig_t *logconfig = NULL;
95 
96 	if (argc != 5) {
97 		printf("usage: %s origin file1 file2 journal\n", argv[0]);
98 		return (1);
99 	}
100 
101 	origin = argv[1];
102 	file1 = argv[2];
103 	file2 = argv[3];
104 	journal = argv[4];
105 
106 	isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
107 	CHECK(isc_mem_create(0, 0, &mctx));
108 	CHECK(isc_entropy_create(mctx, &ectx));
109 
110 	CHECK(isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE));
111 	hash_active = ISC_TRUE;
112 
113 	CHECK(dst_lib_init(mctx, ectx, ISC_ENTROPY_BLOCKING));
114 	dst_active = ISC_TRUE;
115 
116 	CHECK(isc_log_create(mctx, &lctx, &logconfig));
117 	isc_log_registercategories(lctx, categories);
118 	isc_log_setcontext(lctx);
119 	dns_log_init(lctx);
120 	dns_log_setcontext(lctx);
121 
122 	destination.file.stream = stderr;
123 	destination.file.name = NULL;
124 	destination.file.versions = ISC_LOG_ROLLNEVER;
125 	destination.file.maximum_size = 0;
126 	CHECK(isc_log_createchannel(logconfig, "stderr",
127 				    ISC_LOG_TOFILEDESC, ISC_LOG_DYNAMIC,
128 				    &destination, 0));
129 	CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL));
130 
131 	dns_result_register();
132 
133 	result = loadzone(&old, origin, file1);
134 	if (result != ISC_R_SUCCESS) {
135 		fprintf(stderr, "Couldn't load %s: ", file1);
136 		goto cleanup;
137 	}
138 
139 	result = loadzone(&new, origin, file2);
140 	if (result != ISC_R_SUCCESS) {
141 		fprintf(stderr, "Couldn't load %s: ", file2);
142 		goto cleanup;
143 	}
144 
145 	result = dns_db_diff(mctx, new, NULL, old, NULL, journal);
146 
147   cleanup:
148 	if (result != ISC_R_SUCCESS)
149 		fprintf(stderr, "%s\n", isc_result_totext(result));
150 
151 	if (new != NULL)
152 		dns_db_detach(&new);
153 	if (old != NULL)
154 		dns_db_detach(&old);
155 
156 	if (lctx != NULL)
157 		isc_log_destroy(&lctx);
158 	if (dst_active) {
159 		dst_lib_destroy();
160 		dst_active = ISC_FALSE;
161 	}
162 	if (hash_active) {
163 		isc_hash_destroy();
164 		hash_active = ISC_FALSE;
165 	}
166 	if (ectx != NULL)
167 		isc_entropy_detach(&ectx);
168 	if (mctx != NULL)
169 		isc_mem_destroy(&mctx);
170 
171 	return(result != ISC_R_SUCCESS ? 1 : 0);
172 }
173 
174