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