1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 #include <errno.h>
13 #include <inttypes.h>
14 #include <stdbool.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 
18 #include <isc/file.h>
19 #include <isc/mem.h>
20 #include <isc/print.h>
21 #include <isc/stdio.h>
22 #include <isc/string.h>
23 #include <isc/util.h>
24 
25 #include <dns/compress.h>
26 #include <dns/db.h>
27 #include <dns/dbiterator.h>
28 #include <dns/diff.h>
29 #include <dns/fixedname.h>
30 #include <dns/journal.h>
31 #include <dns/log.h>
32 #include <dns/rdataset.h>
33 #include <dns/rdatasetiter.h>
34 #include <dns/result.h>
35 #include <dns/soa.h>
36 
37 /*! \file
38  * \brief Journaling.
39  *
40  * A journal file consists of
41  *
42  *   \li A fixed-size header of type journal_rawheader_t.
43  *
44  *   \li The index.  This is an unordered array of index entries
45  *     of type journal_rawpos_t giving the locations
46  *     of some arbitrary subset of the journal's addressable
47  *     transactions.  The index entries are used as hints to
48  *     speed up the process of locating a transaction with a given
49  *     serial number.  Unused index entries have an "offset"
50  *     field of zero.  The size of the index can vary between
51  *     journal files, but does not change during the lifetime
52  *     of a file.  The size can be zero.
53  *
54  *   \li The journal data.  This  consists of one or more transactions.
55  *     Each transaction begins with a transaction header of type
56  *     journal_rawxhdr_t.  The transaction header is followed by a
57  *     sequence of RRs, similar in structure to an IXFR difference
58  *     sequence (RFC1995).  That is, the pre-transaction SOA,
59  *     zero or more other deleted RRs, the post-transaction SOA,
60  *     and zero or more other added RRs.  Unlike in IXFR, each RR
61  *     is prefixed with a 32-bit length.
62  *
63  *     The journal data part grows as new transactions are
64  *     appended to the file.  Only those transactions
65  *     whose serial number is current-(2^31-1) to current
66  *     are considered "addressable" and may be pointed
67  *     to from the header or index.  They may be preceded
68  *     by old transactions that are no longer addressable,
69  *     and they may be followed by transactions that were
70  *     appended to the journal but never committed by updating
71  *     the "end" position in the header.  The latter will
72  *     be overwritten when new transactions are added.
73  */
74 
75 /**************************************************************************/
76 /*
77  * Miscellaneous utilities.
78  */
79 
80 #define JOURNAL_COMMON_LOGARGS \
81 	dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_JOURNAL
82 
83 #define JOURNAL_DEBUG_LOGARGS(n) JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(n)
84 
85 /*%
86  * It would be non-sensical (or at least obtuse) to use FAIL() with an
87  * ISC_R_SUCCESS code, but the test is there to keep the Solaris compiler
88  * from complaining about "end-of-loop code not reached".
89  */
90 #define FAIL(code)                           \
91 	do {                                 \
92 		result = (code);             \
93 		if (result != ISC_R_SUCCESS) \
94 			goto failure;        \
95 	} while (0)
96 
97 #define CHECK(op)                            \
98 	do {                                 \
99 		result = (op);               \
100 		if (result != ISC_R_SUCCESS) \
101 			goto failure;        \
102 	} while (0)
103 
104 #define JOURNAL_SERIALSET 0x01U
105 
106 static isc_result_t
107 index_to_disk(dns_journal_t *);
108 
109 static inline uint32_t
decode_uint32(unsigned char * p)110 decode_uint32(unsigned char *p) {
111 	return ((p[0] << 24) + (p[1] << 16) + (p[2] << 8) + (p[3] << 0));
112 }
113 
114 static inline void
encode_uint32(uint32_t val,unsigned char * p)115 encode_uint32(uint32_t val, unsigned char *p) {
116 	p[0] = (uint8_t)(val >> 24);
117 	p[1] = (uint8_t)(val >> 16);
118 	p[2] = (uint8_t)(val >> 8);
119 	p[3] = (uint8_t)(val >> 0);
120 }
121 
122 isc_result_t
dns_db_createsoatuple(dns_db_t * db,dns_dbversion_t * ver,isc_mem_t * mctx,dns_diffop_t op,dns_difftuple_t ** tp)123 dns_db_createsoatuple(dns_db_t *db, dns_dbversion_t *ver, isc_mem_t *mctx,
124 		      dns_diffop_t op, dns_difftuple_t **tp) {
125 	isc_result_t result;
126 	dns_dbnode_t *node;
127 	dns_rdataset_t rdataset;
128 	dns_rdata_t rdata = DNS_RDATA_INIT;
129 	dns_fixedname_t fixed;
130 	dns_name_t *zonename;
131 
132 	zonename = dns_fixedname_initname(&fixed);
133 	dns_name_copynf(dns_db_origin(db), zonename);
134 
135 	node = NULL;
136 	result = dns_db_findnode(db, zonename, false, &node);
137 	if (result != ISC_R_SUCCESS) {
138 		goto nonode;
139 	}
140 
141 	dns_rdataset_init(&rdataset);
142 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, 0,
143 				     (isc_stdtime_t)0, &rdataset, NULL);
144 	if (result != ISC_R_SUCCESS) {
145 		goto freenode;
146 	}
147 
148 	result = dns_rdataset_first(&rdataset);
149 	if (result != ISC_R_SUCCESS) {
150 		goto freenode;
151 	}
152 
153 	dns_rdataset_current(&rdataset, &rdata);
154 	dns_rdataset_getownercase(&rdataset, zonename);
155 
156 	result = dns_difftuple_create(mctx, op, zonename, rdataset.ttl, &rdata,
157 				      tp);
158 
159 	dns_rdataset_disassociate(&rdataset);
160 	dns_db_detachnode(db, &node);
161 	return (result);
162 
163 freenode:
164 	dns_db_detachnode(db, &node);
165 nonode:
166 	UNEXPECTED_ERROR(__FILE__, __LINE__, "missing SOA");
167 	return (result);
168 }
169 
170 /* Journaling */
171 
172 /*%
173  * On-disk representation of a "pointer" to a journal entry.
174  * These are used in the journal header to locate the beginning
175  * and end of the journal, and in the journal index to locate
176  * other transactions.
177  */
178 typedef struct {
179 	unsigned char serial[4]; /*%< SOA serial before update. */
180 	/*
181 	 * XXXRTH  Should offset be 8 bytes?
182 	 * XXXDCL ... probably, since isc_offset_t is 8 bytes on many OSs.
183 	 * XXXAG  ... but we will not be able to seek >2G anyway on many
184 	 *            platforms as long as we are using fseek() rather
185 	 *            than lseek().
186 	 */
187 	unsigned char offset[4]; /*%< Offset from beginning of file. */
188 } journal_rawpos_t;
189 
190 /*%
191  * The header is of a fixed size, with some spare room for future
192  * extensions.
193  */
194 #define JOURNAL_HEADER_SIZE 64 /* Bytes. */
195 
196 /*%
197  * The on-disk representation of the journal header.
198  * All numbers are stored in big-endian order.
199  */
200 typedef union {
201 	struct {
202 		/*% File format version ID. */
203 		unsigned char format[16];
204 		/*% Position of the first addressable transaction */
205 		journal_rawpos_t begin;
206 		/*% Position of the next (yet nonexistent) transaction. */
207 		journal_rawpos_t end;
208 		/*% Number of index entries following the header. */
209 		unsigned char index_size[4];
210 		/*% Source serial number. */
211 		unsigned char sourceserial[4];
212 		unsigned char flags;
213 	} h;
214 	/* Pad the header to a fixed size. */
215 	unsigned char pad[JOURNAL_HEADER_SIZE];
216 } journal_rawheader_t;
217 
218 /*%
219  * The on-disk representation of the transaction header.
220  * There is one of these at the beginning of each transaction.
221  */
222 typedef struct {
223 	unsigned char size[4];	  /*%< In bytes, excluding header. */
224 	unsigned char serial0[4]; /*%< SOA serial before update. */
225 	unsigned char serial1[4]; /*%< SOA serial after update. */
226 } journal_rawxhdr_t;
227 
228 /*%
229  * The on-disk representation of the RR header.
230  * There is one of these at the beginning of each RR.
231  */
232 typedef struct {
233 	unsigned char size[4]; /*%< In bytes, excluding header. */
234 } journal_rawrrhdr_t;
235 
236 /*%
237  * The in-core representation of the journal header.
238  */
239 typedef struct {
240 	uint32_t serial;
241 	isc_offset_t offset;
242 } journal_pos_t;
243 
244 #define POS_VALID(pos)	    ((pos).offset != 0)
245 #define POS_INVALIDATE(pos) ((pos).offset = 0, (pos).serial = 0)
246 
247 typedef struct {
248 	unsigned char format[16];
249 	journal_pos_t begin;
250 	journal_pos_t end;
251 	uint32_t index_size;
252 	uint32_t sourceserial;
253 	bool serialset;
254 } journal_header_t;
255 
256 /*%
257  * The in-core representation of the transaction header.
258  */
259 
260 typedef struct {
261 	uint32_t size;
262 	uint32_t serial0;
263 	uint32_t serial1;
264 } journal_xhdr_t;
265 
266 /*%
267  * The in-core representation of the RR header.
268  */
269 typedef struct {
270 	uint32_t size;
271 } journal_rrhdr_t;
272 
273 /*%
274  * Initial contents to store in the header of a newly created
275  * journal file.
276  *
277  * The header starts with the magic string ";BIND LOG V9\n"
278  * to identify the file as a BIND 9 journal file.  An ASCII
279  * identification string is used rather than a binary magic
280  * number to be consistent with BIND 8 (BIND 8 journal files
281  * are ASCII text files).
282  */
283 
284 static journal_header_t initial_journal_header = {
285 	";BIND LOG V9\n", { 0, 0 }, { 0, 0 }, 0, 0, 0
286 };
287 
288 #define JOURNAL_EMPTY(h) ((h)->begin.offset == (h)->end.offset)
289 
290 typedef enum {
291 	JOURNAL_STATE_INVALID,
292 	JOURNAL_STATE_READ,
293 	JOURNAL_STATE_WRITE,
294 	JOURNAL_STATE_TRANSACTION,
295 	JOURNAL_STATE_INLINE
296 } journal_state_t;
297 
298 struct dns_journal {
299 	unsigned int magic; /*%< JOUR */
300 	isc_mem_t *mctx;    /*%< Memory context */
301 	journal_state_t state;
302 	char *filename;		 /*%< Journal file name */
303 	FILE *fp;		 /*%< File handle */
304 	isc_offset_t offset;	 /*%< Current file offset */
305 	journal_header_t header; /*%< In-core journal header */
306 	unsigned char *rawindex; /*%< In-core buffer for journal index
307 				  * in
308 				  * on-disk format */
309 	journal_pos_t *index;	 /*%< In-core journal index */
310 
311 	/*% Current transaction state (when writing). */
312 	struct {
313 		unsigned int n_soa;   /*%< Number of SOAs seen */
314 		journal_pos_t pos[2]; /*%< Begin/end position */
315 	} x;
316 
317 	/*% Iteration state (when reading). */
318 	struct {
319 		/* These define the part of the journal we iterate over. */
320 		journal_pos_t bpos; /*%< Position before first, */
321 		journal_pos_t epos; /*%< and after last transaction */
322 		/* The rest is iterator state. */
323 		uint32_t current_serial; /*%< Current SOA serial
324 					  * */
325 		isc_buffer_t source;	 /*%< Data from disk */
326 		isc_buffer_t target;	 /*%< Data from _fromwire check
327 					  * */
328 		dns_decompress_t dctx;	 /*%< Dummy decompression ctx */
329 		dns_name_t name;	 /*%< Current domain name */
330 		dns_rdata_t rdata;	 /*%< Current rdata */
331 		uint32_t ttl;		 /*%< Current TTL */
332 		unsigned int xsize;	 /*%< Size of transaction data */
333 		unsigned int xpos;	 /*%< Current position in it */
334 		isc_result_t result;	 /*%< Result of last call */
335 	} it;
336 };
337 
338 #define DNS_JOURNAL_MAGIC    ISC_MAGIC('J', 'O', 'U', 'R')
339 #define DNS_JOURNAL_VALID(t) ISC_MAGIC_VALID(t, DNS_JOURNAL_MAGIC)
340 
341 static void
journal_pos_decode(journal_rawpos_t * raw,journal_pos_t * cooked)342 journal_pos_decode(journal_rawpos_t *raw, journal_pos_t *cooked) {
343 	cooked->serial = decode_uint32(raw->serial);
344 	cooked->offset = decode_uint32(raw->offset);
345 }
346 
347 static void
journal_pos_encode(journal_rawpos_t * raw,journal_pos_t * cooked)348 journal_pos_encode(journal_rawpos_t *raw, journal_pos_t *cooked) {
349 	encode_uint32(cooked->serial, raw->serial);
350 	encode_uint32(cooked->offset, raw->offset);
351 }
352 
353 static void
journal_header_decode(journal_rawheader_t * raw,journal_header_t * cooked)354 journal_header_decode(journal_rawheader_t *raw, journal_header_t *cooked) {
355 	INSIST(sizeof(cooked->format) == sizeof(raw->h.format));
356 	memmove(cooked->format, raw->h.format, sizeof(cooked->format));
357 	journal_pos_decode(&raw->h.begin, &cooked->begin);
358 	journal_pos_decode(&raw->h.end, &cooked->end);
359 	cooked->index_size = decode_uint32(raw->h.index_size);
360 	cooked->sourceserial = decode_uint32(raw->h.sourceserial);
361 	cooked->serialset = ((raw->h.flags & JOURNAL_SERIALSET) != 0);
362 }
363 
364 static void
journal_header_encode(journal_header_t * cooked,journal_rawheader_t * raw)365 journal_header_encode(journal_header_t *cooked, journal_rawheader_t *raw) {
366 	unsigned char flags = 0;
367 
368 	INSIST(sizeof(cooked->format) == sizeof(raw->h.format));
369 	memset(raw->pad, 0, sizeof(raw->pad));
370 	memmove(raw->h.format, cooked->format, sizeof(raw->h.format));
371 	journal_pos_encode(&raw->h.begin, &cooked->begin);
372 	journal_pos_encode(&raw->h.end, &cooked->end);
373 	encode_uint32(cooked->index_size, raw->h.index_size);
374 	encode_uint32(cooked->sourceserial, raw->h.sourceserial);
375 	if (cooked->serialset) {
376 		flags |= JOURNAL_SERIALSET;
377 	}
378 	raw->h.flags = flags;
379 }
380 
381 /*
382  * Journal file I/O subroutines, with error checking and reporting.
383  */
384 static isc_result_t
journal_seek(dns_journal_t * j,uint32_t offset)385 journal_seek(dns_journal_t *j, uint32_t offset) {
386 	isc_result_t result;
387 
388 	result = isc_stdio_seek(j->fp, (off_t)offset, SEEK_SET);
389 	if (result != ISC_R_SUCCESS) {
390 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
391 			      "%s: seek: %s", j->filename,
392 			      isc_result_totext(result));
393 		return (ISC_R_UNEXPECTED);
394 	}
395 	j->offset = offset;
396 	return (ISC_R_SUCCESS);
397 }
398 
399 static isc_result_t
journal_read(dns_journal_t * j,void * mem,size_t nbytes)400 journal_read(dns_journal_t *j, void *mem, size_t nbytes) {
401 	isc_result_t result;
402 
403 	result = isc_stdio_read(mem, 1, nbytes, j->fp, NULL);
404 	if (result != ISC_R_SUCCESS) {
405 		if (result == ISC_R_EOF) {
406 			return (ISC_R_NOMORE);
407 		}
408 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
409 			      "%s: read: %s", j->filename,
410 			      isc_result_totext(result));
411 		return (ISC_R_UNEXPECTED);
412 	}
413 	j->offset += (isc_offset_t)nbytes;
414 	return (ISC_R_SUCCESS);
415 }
416 
417 static isc_result_t
journal_write(dns_journal_t * j,void * mem,size_t nbytes)418 journal_write(dns_journal_t *j, void *mem, size_t nbytes) {
419 	isc_result_t result;
420 
421 	result = isc_stdio_write(mem, 1, nbytes, j->fp, NULL);
422 	if (result != ISC_R_SUCCESS) {
423 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
424 			      "%s: write: %s", j->filename,
425 			      isc_result_totext(result));
426 		return (ISC_R_UNEXPECTED);
427 	}
428 	j->offset += (isc_offset_t)nbytes;
429 	return (ISC_R_SUCCESS);
430 }
431 
432 static isc_result_t
journal_fsync(dns_journal_t * j)433 journal_fsync(dns_journal_t *j) {
434 	isc_result_t result;
435 	result = isc_stdio_flush(j->fp);
436 	if (result != ISC_R_SUCCESS) {
437 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
438 			      "%s: flush: %s", j->filename,
439 			      isc_result_totext(result));
440 		return (ISC_R_UNEXPECTED);
441 	}
442 	result = isc_stdio_sync(j->fp);
443 	if (result != ISC_R_SUCCESS) {
444 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
445 			      "%s: fsync: %s", j->filename,
446 			      isc_result_totext(result));
447 		return (ISC_R_UNEXPECTED);
448 	}
449 	return (ISC_R_SUCCESS);
450 }
451 
452 /*
453  * Read/write a transaction header at the current file position.
454  */
455 
456 static isc_result_t
journal_read_xhdr(dns_journal_t * j,journal_xhdr_t * xhdr)457 journal_read_xhdr(dns_journal_t *j, journal_xhdr_t *xhdr) {
458 	journal_rawxhdr_t raw;
459 	isc_result_t result;
460 	result = journal_read(j, &raw, sizeof(raw));
461 	if (result != ISC_R_SUCCESS) {
462 		return (result);
463 	}
464 	xhdr->size = decode_uint32(raw.size);
465 	xhdr->serial0 = decode_uint32(raw.serial0);
466 	xhdr->serial1 = decode_uint32(raw.serial1);
467 	return (ISC_R_SUCCESS);
468 }
469 
470 static isc_result_t
journal_write_xhdr(dns_journal_t * j,uint32_t size,uint32_t serial0,uint32_t serial1)471 journal_write_xhdr(dns_journal_t *j, uint32_t size, uint32_t serial0,
472 		   uint32_t serial1) {
473 	journal_rawxhdr_t raw;
474 	encode_uint32(size, raw.size);
475 	encode_uint32(serial0, raw.serial0);
476 	encode_uint32(serial1, raw.serial1);
477 	return (journal_write(j, &raw, sizeof(raw)));
478 }
479 
480 /*
481  * Read an RR header at the current file position.
482  */
483 
484 static isc_result_t
journal_read_rrhdr(dns_journal_t * j,journal_rrhdr_t * rrhdr)485 journal_read_rrhdr(dns_journal_t *j, journal_rrhdr_t *rrhdr) {
486 	journal_rawrrhdr_t raw;
487 	isc_result_t result;
488 	result = journal_read(j, &raw, sizeof(raw));
489 	if (result != ISC_R_SUCCESS) {
490 		return (result);
491 	}
492 	rrhdr->size = decode_uint32(raw.size);
493 	return (ISC_R_SUCCESS);
494 }
495 
496 static isc_result_t
journal_file_create(isc_mem_t * mctx,const char * filename)497 journal_file_create(isc_mem_t *mctx, const char *filename) {
498 	FILE *fp = NULL;
499 	isc_result_t result;
500 	journal_header_t header;
501 	journal_rawheader_t rawheader;
502 	int index_size = 56; /* XXX configurable */
503 	int size;
504 	void *mem; /* Memory for temporary index image. */
505 
506 	INSIST(sizeof(journal_rawheader_t) == JOURNAL_HEADER_SIZE);
507 
508 	result = isc_stdio_open(filename, "wb", &fp);
509 	if (result != ISC_R_SUCCESS) {
510 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
511 			      "%s: create: %s", filename,
512 			      isc_result_totext(result));
513 		return (ISC_R_UNEXPECTED);
514 	}
515 
516 	header = initial_journal_header;
517 	header.index_size = index_size;
518 	journal_header_encode(&header, &rawheader);
519 
520 	size = sizeof(journal_rawheader_t) +
521 	       index_size * sizeof(journal_rawpos_t);
522 
523 	mem = isc_mem_get(mctx, size);
524 	memset(mem, 0, size);
525 	memmove(mem, &rawheader, sizeof(rawheader));
526 
527 	result = isc_stdio_write(mem, 1, (size_t)size, fp, NULL);
528 	if (result != ISC_R_SUCCESS) {
529 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
530 			      "%s: write: %s", filename,
531 			      isc_result_totext(result));
532 		(void)isc_stdio_close(fp);
533 		(void)isc_file_remove(filename);
534 		isc_mem_put(mctx, mem, size);
535 		return (ISC_R_UNEXPECTED);
536 	}
537 	isc_mem_put(mctx, mem, size);
538 
539 	result = isc_stdio_close(fp);
540 	if (result != ISC_R_SUCCESS) {
541 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
542 			      "%s: close: %s", filename,
543 			      isc_result_totext(result));
544 		(void)isc_file_remove(filename);
545 		return (ISC_R_UNEXPECTED);
546 	}
547 
548 	return (ISC_R_SUCCESS);
549 }
550 
551 static isc_result_t
journal_open(isc_mem_t * mctx,const char * filename,bool writable,bool create,dns_journal_t ** journalp)552 journal_open(isc_mem_t *mctx, const char *filename, bool writable, bool create,
553 	     dns_journal_t **journalp) {
554 	FILE *fp = NULL;
555 	isc_result_t result;
556 	journal_rawheader_t rawheader;
557 	dns_journal_t *j;
558 
559 	INSIST(journalp != NULL && *journalp == NULL);
560 	j = isc_mem_get(mctx, sizeof(*j));
561 
562 	j->mctx = NULL;
563 	isc_mem_attach(mctx, &j->mctx);
564 	j->state = JOURNAL_STATE_INVALID;
565 	j->fp = NULL;
566 	j->filename = isc_mem_strdup(mctx, filename);
567 	j->index = NULL;
568 	j->rawindex = NULL;
569 
570 	if (j->filename == NULL) {
571 		FAIL(ISC_R_NOMEMORY);
572 	}
573 
574 	result = isc_stdio_open(j->filename, writable ? "rb+" : "rb", &fp);
575 
576 	if (result == ISC_R_FILENOTFOUND) {
577 		if (create) {
578 			isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(1),
579 				      "journal file %s does not exist, "
580 				      "creating it",
581 				      j->filename);
582 			CHECK(journal_file_create(mctx, filename));
583 			/*
584 			 * Retry.
585 			 */
586 			result = isc_stdio_open(j->filename, "rb+", &fp);
587 		} else {
588 			FAIL(ISC_R_NOTFOUND);
589 		}
590 	}
591 	if (result != ISC_R_SUCCESS) {
592 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
593 			      "%s: open: %s", j->filename,
594 			      isc_result_totext(result));
595 		FAIL(ISC_R_UNEXPECTED);
596 	}
597 
598 	j->fp = fp;
599 
600 	/*
601 	 * Set magic early so that seek/read can succeed.
602 	 */
603 	j->magic = DNS_JOURNAL_MAGIC;
604 
605 	CHECK(journal_seek(j, 0));
606 	CHECK(journal_read(j, &rawheader, sizeof(rawheader)));
607 
608 	if (memcmp(rawheader.h.format, initial_journal_header.format,
609 		   sizeof(initial_journal_header.format)) != 0)
610 	{
611 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
612 			      "%s: journal format not recognized", j->filename);
613 		FAIL(ISC_R_UNEXPECTED);
614 	}
615 	journal_header_decode(&rawheader, &j->header);
616 
617 	/*
618 	 * If there is an index, read the raw index into a dynamically
619 	 * allocated buffer and then convert it into a cooked index.
620 	 */
621 	if (j->header.index_size != 0) {
622 		unsigned int i;
623 		unsigned int rawbytes;
624 		unsigned char *p;
625 
626 		rawbytes = j->header.index_size * sizeof(journal_rawpos_t);
627 		j->rawindex = isc_mem_get(mctx, rawbytes);
628 
629 		CHECK(journal_read(j, j->rawindex, rawbytes));
630 
631 		j->index = isc_mem_get(mctx, j->header.index_size *
632 						     sizeof(journal_pos_t));
633 
634 		p = j->rawindex;
635 		for (i = 0; i < j->header.index_size; i++) {
636 			j->index[i].serial = decode_uint32(p);
637 			p += 4;
638 			j->index[i].offset = decode_uint32(p);
639 			p += 4;
640 		}
641 		INSIST(p == j->rawindex + rawbytes);
642 	}
643 	j->offset = -1; /* Invalid, must seek explicitly. */
644 
645 	/*
646 	 * Initialize the iterator.
647 	 */
648 	dns_name_init(&j->it.name, NULL);
649 	dns_rdata_init(&j->it.rdata);
650 
651 	/*
652 	 * Set up empty initial buffers for unchecked and checked
653 	 * wire format RR data.  They will be reallocated
654 	 * later.
655 	 */
656 	isc_buffer_init(&j->it.source, NULL, 0);
657 	isc_buffer_init(&j->it.target, NULL, 0);
658 	dns_decompress_init(&j->it.dctx, -1, DNS_DECOMPRESS_NONE);
659 
660 	j->state = writable ? JOURNAL_STATE_WRITE : JOURNAL_STATE_READ;
661 
662 	*journalp = j;
663 	return (ISC_R_SUCCESS);
664 
665 failure:
666 	j->magic = 0;
667 	if (j->rawindex != NULL) {
668 		isc_mem_put(j->mctx, j->rawindex,
669 			    j->header.index_size * sizeof(journal_rawpos_t));
670 	}
671 	if (j->index != NULL) {
672 		isc_mem_put(j->mctx, j->index,
673 			    j->header.index_size * sizeof(journal_pos_t));
674 	}
675 	if (j->filename != NULL) {
676 		isc_mem_free(j->mctx, j->filename);
677 	}
678 	if (j->fp != NULL) {
679 		(void)isc_stdio_close(j->fp);
680 	}
681 	isc_mem_putanddetach(&j->mctx, j, sizeof(*j));
682 	return (result);
683 }
684 
685 isc_result_t
dns_journal_open(isc_mem_t * mctx,const char * filename,unsigned int mode,dns_journal_t ** journalp)686 dns_journal_open(isc_mem_t *mctx, const char *filename, unsigned int mode,
687 		 dns_journal_t **journalp) {
688 	isc_result_t result;
689 	size_t namelen;
690 	char backup[1024];
691 	bool writable, create;
692 
693 	create = ((mode & DNS_JOURNAL_CREATE) != 0);
694 	writable = ((mode & (DNS_JOURNAL_WRITE | DNS_JOURNAL_CREATE)) != 0);
695 
696 	result = journal_open(mctx, filename, writable, create, journalp);
697 	if (result == ISC_R_NOTFOUND) {
698 		namelen = strlen(filename);
699 		if (namelen > 4U && strcmp(filename + namelen - 4, ".jnl") == 0)
700 		{
701 			namelen -= 4;
702 		}
703 
704 		result = snprintf(backup, sizeof(backup), "%.*s.jbk",
705 				  (int)namelen, filename);
706 		if (result >= sizeof(backup)) {
707 			return (ISC_R_NOSPACE);
708 		}
709 		result = journal_open(mctx, backup, writable, writable,
710 				      journalp);
711 	}
712 	return (result);
713 }
714 
715 /*
716  * A comparison function defining the sorting order for
717  * entries in the IXFR-style journal file.
718  *
719  * The IXFR format requires that deletions are sorted before
720  * additions, and within either one, SOA records are sorted
721  * before others.
722  *
723  * Also sort the non-SOA records by type as a courtesy to the
724  * server receiving the IXFR - it may help reduce the amount of
725  * rdataset merging it has to do.
726  */
727 static int
ixfr_order(const void * av,const void * bv)728 ixfr_order(const void *av, const void *bv) {
729 	dns_difftuple_t const *const *ap = av;
730 	dns_difftuple_t const *const *bp = bv;
731 	dns_difftuple_t const *a = *ap;
732 	dns_difftuple_t const *b = *bp;
733 	int r;
734 	int bop = 0, aop = 0;
735 
736 	switch (a->op) {
737 	case DNS_DIFFOP_DEL:
738 	case DNS_DIFFOP_DELRESIGN:
739 		aop = 1;
740 		break;
741 	case DNS_DIFFOP_ADD:
742 	case DNS_DIFFOP_ADDRESIGN:
743 		aop = 0;
744 		break;
745 	default:
746 		INSIST(0);
747 		ISC_UNREACHABLE();
748 	}
749 
750 	switch (b->op) {
751 	case DNS_DIFFOP_DEL:
752 	case DNS_DIFFOP_DELRESIGN:
753 		bop = 1;
754 		break;
755 	case DNS_DIFFOP_ADD:
756 	case DNS_DIFFOP_ADDRESIGN:
757 		bop = 0;
758 		break;
759 	default:
760 		INSIST(0);
761 		ISC_UNREACHABLE();
762 	}
763 
764 	r = bop - aop;
765 	if (r != 0) {
766 		return (r);
767 	}
768 
769 	r = (b->rdata.type == dns_rdatatype_soa) -
770 	    (a->rdata.type == dns_rdatatype_soa);
771 	if (r != 0) {
772 		return (r);
773 	}
774 
775 	r = (a->rdata.type - b->rdata.type);
776 	return (r);
777 }
778 
779 /*
780  * Advance '*pos' to the next journal transaction.
781  *
782  * Requires:
783  *	*pos refers to a valid journal transaction.
784  *
785  * Ensures:
786  *	When ISC_R_SUCCESS is returned,
787  *	*pos refers to the next journal transaction.
788  *
789  * Returns one of:
790  *
791  *    ISC_R_SUCCESS
792  *    ISC_R_NOMORE 	*pos pointed at the last transaction
793  *    Other results due to file errors are possible.
794  */
795 static isc_result_t
journal_next(dns_journal_t * j,journal_pos_t * pos)796 journal_next(dns_journal_t *j, journal_pos_t *pos) {
797 	isc_result_t result;
798 	journal_xhdr_t xhdr;
799 	REQUIRE(DNS_JOURNAL_VALID(j));
800 
801 	result = journal_seek(j, pos->offset);
802 	if (result != ISC_R_SUCCESS) {
803 		return (result);
804 	}
805 
806 	if (pos->serial == j->header.end.serial) {
807 		return (ISC_R_NOMORE);
808 	}
809 	/*
810 	 * Read the header of the current transaction.
811 	 * This will return ISC_R_NOMORE if we are at EOF.
812 	 */
813 	result = journal_read_xhdr(j, &xhdr);
814 	if (result != ISC_R_SUCCESS) {
815 		return (result);
816 	}
817 
818 	/*
819 	 * Check serial number consistency.
820 	 */
821 	if (xhdr.serial0 != pos->serial) {
822 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
823 			      "%s: journal file corrupt: "
824 			      "expected serial %u, got %u",
825 			      j->filename, pos->serial, xhdr.serial0);
826 		return (ISC_R_UNEXPECTED);
827 	}
828 
829 	/*
830 	 * Check for offset wraparound.
831 	 */
832 	if ((isc_offset_t)(pos->offset + sizeof(journal_rawxhdr_t) +
833 			   xhdr.size) < pos->offset)
834 	{
835 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
836 			      "%s: offset too large", j->filename);
837 		return (ISC_R_UNEXPECTED);
838 	}
839 
840 	pos->offset += sizeof(journal_rawxhdr_t) + xhdr.size;
841 	pos->serial = xhdr.serial1;
842 	return (ISC_R_SUCCESS);
843 }
844 
845 /*
846  * If the index of the journal 'j' contains an entry "better"
847  * than '*best_guess', replace '*best_guess' with it.
848  *
849  * "Better" means having a serial number closer to 'serial'
850  * but not greater than 'serial'.
851  */
852 static void
index_find(dns_journal_t * j,uint32_t serial,journal_pos_t * best_guess)853 index_find(dns_journal_t *j, uint32_t serial, journal_pos_t *best_guess) {
854 	unsigned int i;
855 	if (j->index == NULL) {
856 		return;
857 	}
858 	for (i = 0; i < j->header.index_size; i++) {
859 		if (POS_VALID(j->index[i]) &&
860 		    DNS_SERIAL_GE(serial, j->index[i].serial) &&
861 		    DNS_SERIAL_GT(j->index[i].serial, best_guess->serial))
862 		{
863 			*best_guess = j->index[i];
864 		}
865 	}
866 }
867 
868 /*
869  * Add a new index entry.  If there is no room, make room by removing
870  * the odd-numbered entries and compacting the others into the first
871  * half of the index.  This decimates old index entries exponentially
872  * over time, so that the index always contains a much larger fraction
873  * of recent serial numbers than of old ones.  This is deliberate -
874  * most index searches are for outgoing IXFR, and IXFR tends to request
875  * recent versions more often than old ones.
876  */
877 static void
index_add(dns_journal_t * j,journal_pos_t * pos)878 index_add(dns_journal_t *j, journal_pos_t *pos) {
879 	unsigned int i;
880 	if (j->index == NULL) {
881 		return;
882 	}
883 	/*
884 	 * Search for a vacant position.
885 	 */
886 	for (i = 0; i < j->header.index_size; i++) {
887 		if (!POS_VALID(j->index[i])) {
888 			break;
889 		}
890 	}
891 	if (i == j->header.index_size) {
892 		unsigned int k = 0;
893 		/*
894 		 * Found no vacant position.  Make some room.
895 		 */
896 		for (i = 0; i < j->header.index_size; i += 2) {
897 			j->index[k++] = j->index[i];
898 		}
899 		i = k; /* 'i' identifies the first vacant position. */
900 		while (k < j->header.index_size) {
901 			POS_INVALIDATE(j->index[k]);
902 			k++;
903 		}
904 	}
905 	INSIST(i < j->header.index_size);
906 	INSIST(!POS_VALID(j->index[i]));
907 
908 	/*
909 	 * Store the new index entry.
910 	 */
911 	j->index[i] = *pos;
912 }
913 
914 /*
915  * Invalidate any existing index entries that could become
916  * ambiguous when a new transaction with number 'serial' is added.
917  */
918 static void
index_invalidate(dns_journal_t * j,uint32_t serial)919 index_invalidate(dns_journal_t *j, uint32_t serial) {
920 	unsigned int i;
921 	if (j->index == NULL) {
922 		return;
923 	}
924 	for (i = 0; i < j->header.index_size; i++) {
925 		if (!DNS_SERIAL_GT(serial, j->index[i].serial)) {
926 			POS_INVALIDATE(j->index[i]);
927 		}
928 	}
929 }
930 
931 /*
932  * Try to find a transaction with initial serial number 'serial'
933  * in the journal 'j'.
934  *
935  * If found, store its position at '*pos' and return ISC_R_SUCCESS.
936  *
937  * If 'serial' is current (= the ending serial number of the
938  * last transaction in the journal), set '*pos' to
939  * the position immediately following the last transaction and
940  * return ISC_R_SUCCESS.
941  *
942  * If 'serial' is within the range of addressable serial numbers
943  * covered by the journal but that particular serial number is missing
944  * (from the journal, not just from the index), return ISC_R_NOTFOUND.
945  *
946  * If 'serial' is outside the range of addressable serial numbers
947  * covered by the journal, return ISC_R_RANGE.
948  *
949  */
950 static isc_result_t
journal_find(dns_journal_t * j,uint32_t serial,journal_pos_t * pos)951 journal_find(dns_journal_t *j, uint32_t serial, journal_pos_t *pos) {
952 	isc_result_t result;
953 	journal_pos_t current_pos;
954 	REQUIRE(DNS_JOURNAL_VALID(j));
955 
956 	if (DNS_SERIAL_GT(j->header.begin.serial, serial)) {
957 		return (ISC_R_RANGE);
958 	}
959 	if (DNS_SERIAL_GT(serial, j->header.end.serial)) {
960 		return (ISC_R_RANGE);
961 	}
962 	if (serial == j->header.end.serial) {
963 		*pos = j->header.end;
964 		return (ISC_R_SUCCESS);
965 	}
966 
967 	current_pos = j->header.begin;
968 	index_find(j, serial, &current_pos);
969 
970 	while (current_pos.serial != serial) {
971 		if (DNS_SERIAL_GT(current_pos.serial, serial)) {
972 			return (ISC_R_NOTFOUND);
973 		}
974 		result = journal_next(j, &current_pos);
975 		if (result != ISC_R_SUCCESS) {
976 			return (result);
977 		}
978 	}
979 	*pos = current_pos;
980 	return (ISC_R_SUCCESS);
981 }
982 
983 isc_result_t
dns_journal_begin_transaction(dns_journal_t * j)984 dns_journal_begin_transaction(dns_journal_t *j) {
985 	uint32_t offset;
986 	isc_result_t result;
987 	journal_rawxhdr_t hdr;
988 
989 	REQUIRE(DNS_JOURNAL_VALID(j));
990 	REQUIRE(j->state == JOURNAL_STATE_WRITE ||
991 		j->state == JOURNAL_STATE_INLINE);
992 
993 	/*
994 	 * Find the file offset where the new transaction should
995 	 * be written, and seek there.
996 	 */
997 	if (JOURNAL_EMPTY(&j->header)) {
998 		offset = sizeof(journal_rawheader_t) +
999 			 j->header.index_size * sizeof(journal_rawpos_t);
1000 	} else {
1001 		offset = j->header.end.offset;
1002 	}
1003 	j->x.pos[0].offset = offset;
1004 	j->x.pos[1].offset = offset; /* Initial value, will be incremented. */
1005 	j->x.n_soa = 0;
1006 
1007 	CHECK(journal_seek(j, offset));
1008 
1009 	/*
1010 	 * Write a dummy transaction header of all zeroes to reserve
1011 	 * space.  It will be filled in when the transaction is
1012 	 * finished.
1013 	 */
1014 	memset(&hdr, 0, sizeof(hdr));
1015 	CHECK(journal_write(j, &hdr, sizeof(hdr)));
1016 	j->x.pos[1].offset = j->offset;
1017 
1018 	j->state = JOURNAL_STATE_TRANSACTION;
1019 	result = ISC_R_SUCCESS;
1020 failure:
1021 	return (result);
1022 }
1023 
1024 isc_result_t
dns_journal_writediff(dns_journal_t * j,dns_diff_t * diff)1025 dns_journal_writediff(dns_journal_t *j, dns_diff_t *diff) {
1026 	dns_difftuple_t *t;
1027 	isc_buffer_t buffer;
1028 	void *mem = NULL;
1029 	uint64_t size;
1030 	isc_result_t result;
1031 	isc_region_t used;
1032 
1033 	REQUIRE(DNS_DIFF_VALID(diff));
1034 	REQUIRE(j->state == JOURNAL_STATE_TRANSACTION);
1035 
1036 	isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "writing to journal");
1037 	(void)dns_diff_print(diff, NULL);
1038 
1039 	/*
1040 	 * Pass 1: determine the buffer size needed, and
1041 	 * keep track of SOA serial numbers.
1042 	 */
1043 	size = 0;
1044 	for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
1045 	     t = ISC_LIST_NEXT(t, link)) {
1046 		if (t->rdata.type == dns_rdatatype_soa) {
1047 			if (j->x.n_soa < 2) {
1048 				j->x.pos[j->x.n_soa].serial =
1049 					dns_soa_getserial(&t->rdata);
1050 			}
1051 			j->x.n_soa++;
1052 		}
1053 		size += sizeof(journal_rawrrhdr_t);
1054 		size += t->name.length; /* XXX should have access macro? */
1055 		size += 10;
1056 		size += t->rdata.length;
1057 	}
1058 
1059 	if (size >= DNS_JOURNAL_SIZE_MAX) {
1060 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1061 			      "dns_journal_writediff: %s: journal entry "
1062 			      "too big to be stored: %" PRIu64 " bytes",
1063 			      j->filename, size);
1064 		return (ISC_R_NOSPACE);
1065 	}
1066 
1067 	mem = isc_mem_get(j->mctx, size);
1068 
1069 	isc_buffer_init(&buffer, mem, size);
1070 
1071 	/*
1072 	 * Pass 2.  Write RRs to buffer.
1073 	 */
1074 	for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
1075 	     t = ISC_LIST_NEXT(t, link)) {
1076 		/*
1077 		 * Write the RR header.
1078 		 */
1079 		isc_buffer_putuint32(&buffer,
1080 				     t->name.length + 10 + t->rdata.length);
1081 		/*
1082 		 * Write the owner name, RR header, and RR data.
1083 		 */
1084 		isc_buffer_putmem(&buffer, t->name.ndata, t->name.length);
1085 		isc_buffer_putuint16(&buffer, t->rdata.type);
1086 		isc_buffer_putuint16(&buffer, t->rdata.rdclass);
1087 		isc_buffer_putuint32(&buffer, t->ttl);
1088 		INSIST(t->rdata.length < 65536);
1089 		isc_buffer_putuint16(&buffer, (uint16_t)t->rdata.length);
1090 		INSIST(isc_buffer_availablelength(&buffer) >= t->rdata.length);
1091 		isc_buffer_putmem(&buffer, t->rdata.data, t->rdata.length);
1092 	}
1093 
1094 	isc_buffer_usedregion(&buffer, &used);
1095 	INSIST(used.length == size);
1096 
1097 	j->x.pos[1].offset += used.length;
1098 
1099 	/*
1100 	 * Write the buffer contents to the journal file.
1101 	 */
1102 	CHECK(journal_write(j, used.base, used.length));
1103 
1104 	result = ISC_R_SUCCESS;
1105 
1106 failure:
1107 	if (mem != NULL) {
1108 		isc_mem_put(j->mctx, mem, size);
1109 	}
1110 	return (result);
1111 }
1112 
1113 isc_result_t
dns_journal_commit(dns_journal_t * j)1114 dns_journal_commit(dns_journal_t *j) {
1115 	isc_result_t result;
1116 	journal_rawheader_t rawheader;
1117 	uint64_t total;
1118 
1119 	REQUIRE(DNS_JOURNAL_VALID(j));
1120 	REQUIRE(j->state == JOURNAL_STATE_TRANSACTION ||
1121 		j->state == JOURNAL_STATE_INLINE);
1122 
1123 	/*
1124 	 * Just write out a updated header.
1125 	 */
1126 	if (j->state == JOURNAL_STATE_INLINE) {
1127 		CHECK(journal_fsync(j));
1128 		journal_header_encode(&j->header, &rawheader);
1129 		CHECK(journal_seek(j, 0));
1130 		CHECK(journal_write(j, &rawheader, sizeof(rawheader)));
1131 		CHECK(journal_fsync(j));
1132 		j->state = JOURNAL_STATE_WRITE;
1133 		return (ISC_R_SUCCESS);
1134 	}
1135 
1136 	/*
1137 	 * Perform some basic consistency checks.
1138 	 */
1139 	if (j->x.n_soa != 2) {
1140 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1141 			      "%s: malformed transaction: %d SOAs", j->filename,
1142 			      j->x.n_soa);
1143 		return (ISC_R_UNEXPECTED);
1144 	}
1145 	if (!DNS_SERIAL_GT(j->x.pos[1].serial, j->x.pos[0].serial)) {
1146 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1147 			      "%s: malformed transaction: serial number "
1148 			      "did not increase",
1149 			      j->filename);
1150 		return (ISC_R_UNEXPECTED);
1151 	}
1152 	if (!JOURNAL_EMPTY(&j->header)) {
1153 		if (j->x.pos[0].serial != j->header.end.serial) {
1154 			isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1155 				      "malformed transaction: "
1156 				      "%s last serial %u != "
1157 				      "transaction first serial %u",
1158 				      j->filename, j->header.end.serial,
1159 				      j->x.pos[0].serial);
1160 			return (ISC_R_UNEXPECTED);
1161 		}
1162 	}
1163 
1164 	/*
1165 	 * We currently don't support huge journal entries.
1166 	 */
1167 	total = j->x.pos[1].offset - j->x.pos[0].offset;
1168 	if (total >= DNS_JOURNAL_SIZE_MAX) {
1169 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1170 			      "transaction too big to be stored in journal: "
1171 			      "%" PRIu64 "b (max is %" PRIu64 "b)",
1172 			      total, (uint64_t)DNS_JOURNAL_SIZE_MAX);
1173 		return (ISC_R_UNEXPECTED);
1174 	}
1175 
1176 	/*
1177 	 * Some old journal entries may become non-addressable
1178 	 * when we increment the current serial number.  Purge them
1179 	 * by stepping header.begin forward to the first addressable
1180 	 * transaction.  Also purge them from the index.
1181 	 */
1182 	if (!JOURNAL_EMPTY(&j->header)) {
1183 		while (!DNS_SERIAL_GT(j->x.pos[1].serial,
1184 				      j->header.begin.serial)) {
1185 			CHECK(journal_next(j, &j->header.begin));
1186 		}
1187 		index_invalidate(j, j->x.pos[1].serial);
1188 	}
1189 #ifdef notyet
1190 	if (DNS_SERIAL_GT(last_dumped_serial, j->x.pos[1].serial)) {
1191 		force_dump(...);
1192 	}
1193 #endif /* ifdef notyet */
1194 
1195 	/*
1196 	 * Commit the transaction data to stable storage.
1197 	 */
1198 	CHECK(journal_fsync(j));
1199 
1200 	if (j->state == JOURNAL_STATE_TRANSACTION) {
1201 		isc_offset_t offset;
1202 		offset = (j->x.pos[1].offset - j->x.pos[0].offset) -
1203 			 sizeof(journal_rawxhdr_t);
1204 		/*
1205 		 * Update the transaction header.
1206 		 */
1207 		CHECK(journal_seek(j, j->x.pos[0].offset));
1208 		CHECK(journal_write_xhdr(j, offset, j->x.pos[0].serial,
1209 					 j->x.pos[1].serial));
1210 	}
1211 
1212 	/*
1213 	 * Update the journal header.
1214 	 */
1215 	if (JOURNAL_EMPTY(&j->header)) {
1216 		j->header.begin = j->x.pos[0];
1217 	}
1218 	j->header.end = j->x.pos[1];
1219 	journal_header_encode(&j->header, &rawheader);
1220 	CHECK(journal_seek(j, 0));
1221 	CHECK(journal_write(j, &rawheader, sizeof(rawheader)));
1222 
1223 	/*
1224 	 * Update the index.
1225 	 */
1226 	index_add(j, &j->x.pos[0]);
1227 
1228 	/*
1229 	 * Convert the index into on-disk format and write
1230 	 * it to disk.
1231 	 */
1232 	CHECK(index_to_disk(j));
1233 
1234 	/*
1235 	 * Commit the header to stable storage.
1236 	 */
1237 	CHECK(journal_fsync(j));
1238 
1239 	/*
1240 	 * We no longer have a transaction open.
1241 	 */
1242 	j->state = JOURNAL_STATE_WRITE;
1243 
1244 	result = ISC_R_SUCCESS;
1245 
1246 failure:
1247 	return (result);
1248 }
1249 
1250 isc_result_t
dns_journal_write_transaction(dns_journal_t * j,dns_diff_t * diff)1251 dns_journal_write_transaction(dns_journal_t *j, dns_diff_t *diff) {
1252 	isc_result_t result;
1253 	CHECK(dns_diff_sort(diff, ixfr_order));
1254 	CHECK(dns_journal_begin_transaction(j));
1255 	CHECK(dns_journal_writediff(j, diff));
1256 	CHECK(dns_journal_commit(j));
1257 	result = ISC_R_SUCCESS;
1258 failure:
1259 	return (result);
1260 }
1261 
1262 void
dns_journal_destroy(dns_journal_t ** journalp)1263 dns_journal_destroy(dns_journal_t **journalp) {
1264 	dns_journal_t *j = *journalp;
1265 	*journalp = NULL;
1266 	REQUIRE(DNS_JOURNAL_VALID(j));
1267 
1268 	j->it.result = ISC_R_FAILURE;
1269 	dns_name_invalidate(&j->it.name);
1270 	dns_decompress_invalidate(&j->it.dctx);
1271 	if (j->rawindex != NULL) {
1272 		isc_mem_put(j->mctx, j->rawindex,
1273 			    j->header.index_size * sizeof(journal_rawpos_t));
1274 	}
1275 	if (j->index != NULL) {
1276 		isc_mem_put(j->mctx, j->index,
1277 			    j->header.index_size * sizeof(journal_pos_t));
1278 	}
1279 	if (j->it.target.base != NULL) {
1280 		isc_mem_put(j->mctx, j->it.target.base, j->it.target.length);
1281 	}
1282 	if (j->it.source.base != NULL) {
1283 		isc_mem_put(j->mctx, j->it.source.base, j->it.source.length);
1284 	}
1285 	if (j->filename != NULL) {
1286 		isc_mem_free(j->mctx, j->filename);
1287 	}
1288 	if (j->fp != NULL) {
1289 		(void)isc_stdio_close(j->fp);
1290 	}
1291 	j->magic = 0;
1292 	isc_mem_putanddetach(&j->mctx, j, sizeof(*j));
1293 }
1294 
1295 /*
1296  * Roll the open journal 'j' into the database 'db'.
1297  * A new database version will be created.
1298  */
1299 
1300 /* XXX Share code with incoming IXFR? */
1301 
1302 static isc_result_t
roll_forward(dns_journal_t * j,dns_db_t * db,unsigned int options)1303 roll_forward(dns_journal_t *j, dns_db_t *db, unsigned int options) {
1304 	isc_buffer_t source; /* Transaction data from disk */
1305 	isc_buffer_t target; /* Ditto after _fromwire check */
1306 	uint32_t db_serial;  /* Database SOA serial */
1307 	uint32_t end_serial; /* Last journal SOA serial */
1308 	isc_result_t result;
1309 	dns_dbversion_t *ver = NULL;
1310 	journal_pos_t pos;
1311 	dns_diff_t diff;
1312 	unsigned int n_soa = 0;
1313 	unsigned int n_put = 0;
1314 	dns_diffop_t op;
1315 
1316 	REQUIRE(DNS_JOURNAL_VALID(j));
1317 	REQUIRE(DNS_DB_VALID(db));
1318 
1319 	dns_diff_init(j->mctx, &diff);
1320 
1321 	/*
1322 	 * Set up empty initial buffers for unchecked and checked
1323 	 * wire format transaction data.  They will be reallocated
1324 	 * later.
1325 	 */
1326 	isc_buffer_init(&source, NULL, 0);
1327 	isc_buffer_init(&target, NULL, 0);
1328 
1329 	/*
1330 	 * Create the new database version.
1331 	 */
1332 	CHECK(dns_db_newversion(db, &ver));
1333 
1334 	/*
1335 	 * Get the current database SOA serial number.
1336 	 */
1337 	CHECK(dns_db_getsoaserial(db, ver, &db_serial));
1338 
1339 	/*
1340 	 * Locate a journal entry for the current database serial.
1341 	 */
1342 	CHECK(journal_find(j, db_serial, &pos));
1343 	/*
1344 	 * XXX do more drastic things, like marking zone stale,
1345 	 * if this fails?
1346 	 */
1347 	/*
1348 	 * XXXRTH  The zone code should probably mark the zone as bad and
1349 	 *         scream loudly into the log if this is a dynamic update
1350 	 *	   log reply that failed.
1351 	 */
1352 
1353 	end_serial = dns_journal_last_serial(j);
1354 	if (db_serial == end_serial) {
1355 		CHECK(DNS_R_UPTODATE);
1356 	}
1357 
1358 	CHECK(dns_journal_iter_init(j, db_serial, end_serial));
1359 
1360 	for (result = dns_journal_first_rr(j); result == ISC_R_SUCCESS;
1361 	     result = dns_journal_next_rr(j))
1362 	{
1363 		dns_name_t *name;
1364 		uint32_t ttl;
1365 		dns_rdata_t *rdata;
1366 		dns_difftuple_t *tuple = NULL;
1367 
1368 		name = NULL;
1369 		rdata = NULL;
1370 		dns_journal_current_rr(j, &name, &ttl, &rdata);
1371 
1372 		if (rdata->type == dns_rdatatype_soa) {
1373 			n_soa++;
1374 			if (n_soa == 2) {
1375 				db_serial = j->it.current_serial;
1376 			}
1377 		}
1378 
1379 		if (n_soa == 3) {
1380 			n_soa = 1;
1381 		}
1382 		if (n_soa == 0) {
1383 			isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1384 				      "%s: journal file corrupt: missing "
1385 				      "initial SOA",
1386 				      j->filename);
1387 			FAIL(ISC_R_UNEXPECTED);
1388 		}
1389 		if ((options & DNS_JOURNALOPT_RESIGN) != 0) {
1390 			op = (n_soa == 1) ? DNS_DIFFOP_DELRESIGN
1391 					  : DNS_DIFFOP_ADDRESIGN;
1392 		} else {
1393 			op = (n_soa == 1) ? DNS_DIFFOP_DEL : DNS_DIFFOP_ADD;
1394 		}
1395 
1396 		CHECK(dns_difftuple_create(diff.mctx, op, name, ttl, rdata,
1397 					   &tuple));
1398 		dns_diff_append(&diff, &tuple);
1399 
1400 		if (++n_put > 100) {
1401 			isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
1402 				      "%s: applying diff to database (%u)",
1403 				      j->filename, db_serial);
1404 			(void)dns_diff_print(&diff, NULL);
1405 			CHECK(dns_diff_apply(&diff, db, ver));
1406 			dns_diff_clear(&diff);
1407 			n_put = 0;
1408 		}
1409 	}
1410 	if (result == ISC_R_NOMORE) {
1411 		result = ISC_R_SUCCESS;
1412 	}
1413 	CHECK(result);
1414 
1415 	if (n_put != 0) {
1416 		isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
1417 			      "%s: applying final diff to database (%u)",
1418 			      j->filename, db_serial);
1419 		(void)dns_diff_print(&diff, NULL);
1420 		CHECK(dns_diff_apply(&diff, db, ver));
1421 		dns_diff_clear(&diff);
1422 	}
1423 
1424 failure:
1425 	if (ver != NULL) {
1426 		dns_db_closeversion(db, &ver,
1427 				    result == ISC_R_SUCCESS ? true : false);
1428 	}
1429 
1430 	if (source.base != NULL) {
1431 		isc_mem_put(j->mctx, source.base, source.length);
1432 	}
1433 	if (target.base != NULL) {
1434 		isc_mem_put(j->mctx, target.base, target.length);
1435 	}
1436 
1437 	dns_diff_clear(&diff);
1438 
1439 	INSIST(ver == NULL);
1440 
1441 	return (result);
1442 }
1443 
1444 isc_result_t
dns_journal_rollforward(isc_mem_t * mctx,dns_db_t * db,unsigned int options,const char * filename)1445 dns_journal_rollforward(isc_mem_t *mctx, dns_db_t *db, unsigned int options,
1446 			const char *filename) {
1447 	dns_journal_t *j;
1448 	isc_result_t result;
1449 
1450 	REQUIRE(DNS_DB_VALID(db));
1451 	REQUIRE(filename != NULL);
1452 
1453 	j = NULL;
1454 	result = dns_journal_open(mctx, filename, DNS_JOURNAL_READ, &j);
1455 	if (result == ISC_R_NOTFOUND) {
1456 		isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no journal file, but "
1457 							"that's OK");
1458 		return (DNS_R_NOJOURNAL);
1459 	}
1460 	if (result != ISC_R_SUCCESS) {
1461 		return (result);
1462 	}
1463 	if (JOURNAL_EMPTY(&j->header)) {
1464 		result = DNS_R_UPTODATE;
1465 	} else {
1466 		result = roll_forward(j, db, options);
1467 	}
1468 
1469 	dns_journal_destroy(&j);
1470 
1471 	return (result);
1472 }
1473 
1474 isc_result_t
dns_journal_print(isc_mem_t * mctx,const char * filename,FILE * file)1475 dns_journal_print(isc_mem_t *mctx, const char *filename, FILE *file) {
1476 	dns_journal_t *j;
1477 	isc_buffer_t source;   /* Transaction data from disk */
1478 	isc_buffer_t target;   /* Ditto after _fromwire check */
1479 	uint32_t start_serial; /* Database SOA serial */
1480 	uint32_t end_serial;   /* Last journal SOA serial */
1481 	isc_result_t result;
1482 	dns_diff_t diff;
1483 	unsigned int n_soa = 0;
1484 	unsigned int n_put = 0;
1485 
1486 	REQUIRE(filename != NULL);
1487 
1488 	j = NULL;
1489 	result = dns_journal_open(mctx, filename, DNS_JOURNAL_READ, &j);
1490 	if (result == ISC_R_NOTFOUND) {
1491 		isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no journal file");
1492 		return (DNS_R_NOJOURNAL);
1493 	}
1494 
1495 	if (result != ISC_R_SUCCESS) {
1496 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1497 			      "journal open failure: %s: %s",
1498 			      isc_result_totext(result), filename);
1499 		return (result);
1500 	}
1501 
1502 	if (j->header.serialset) {
1503 		fprintf(file, "Source serial = %u\n", j->header.sourceserial);
1504 	}
1505 	dns_diff_init(j->mctx, &diff);
1506 
1507 	/*
1508 	 * Set up empty initial buffers for unchecked and checked
1509 	 * wire format transaction data.  They will be reallocated
1510 	 * later.
1511 	 */
1512 	isc_buffer_init(&source, NULL, 0);
1513 	isc_buffer_init(&target, NULL, 0);
1514 
1515 	start_serial = dns_journal_first_serial(j);
1516 	end_serial = dns_journal_last_serial(j);
1517 
1518 	CHECK(dns_journal_iter_init(j, start_serial, end_serial));
1519 
1520 	for (result = dns_journal_first_rr(j); result == ISC_R_SUCCESS;
1521 	     result = dns_journal_next_rr(j))
1522 	{
1523 		dns_name_t *name;
1524 		uint32_t ttl;
1525 		dns_rdata_t *rdata;
1526 		dns_difftuple_t *tuple = NULL;
1527 
1528 		name = NULL;
1529 		rdata = NULL;
1530 		dns_journal_current_rr(j, &name, &ttl, &rdata);
1531 
1532 		if (rdata->type == dns_rdatatype_soa) {
1533 			n_soa++;
1534 		}
1535 
1536 		if (n_soa == 3) {
1537 			n_soa = 1;
1538 		}
1539 		if (n_soa == 0) {
1540 			isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1541 				      "%s: journal file corrupt: missing "
1542 				      "initial SOA",
1543 				      j->filename);
1544 			FAIL(ISC_R_UNEXPECTED);
1545 		}
1546 		CHECK(dns_difftuple_create(
1547 			diff.mctx, n_soa == 1 ? DNS_DIFFOP_DEL : DNS_DIFFOP_ADD,
1548 			name, ttl, rdata, &tuple));
1549 		dns_diff_append(&diff, &tuple);
1550 
1551 		if (++n_put > 100) {
1552 			result = dns_diff_print(&diff, file);
1553 			dns_diff_clear(&diff);
1554 			n_put = 0;
1555 			if (result != ISC_R_SUCCESS) {
1556 				break;
1557 			}
1558 		}
1559 	}
1560 	if (result == ISC_R_NOMORE) {
1561 		result = ISC_R_SUCCESS;
1562 	}
1563 	CHECK(result);
1564 
1565 	if (n_put != 0) {
1566 		result = dns_diff_print(&diff, file);
1567 		dns_diff_clear(&diff);
1568 	}
1569 	goto cleanup;
1570 
1571 failure:
1572 	isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1573 		      "%s: cannot print: journal file corrupt", j->filename);
1574 
1575 cleanup:
1576 	if (source.base != NULL) {
1577 		isc_mem_put(j->mctx, source.base, source.length);
1578 	}
1579 	if (target.base != NULL) {
1580 		isc_mem_put(j->mctx, target.base, target.length);
1581 	}
1582 
1583 	dns_diff_clear(&diff);
1584 	dns_journal_destroy(&j);
1585 
1586 	return (result);
1587 }
1588 
1589 /**************************************************************************/
1590 /*
1591  * Miscellaneous accessors.
1592  */
1593 bool
dns_journal_empty(dns_journal_t * j)1594 dns_journal_empty(dns_journal_t *j) {
1595 	return (JOURNAL_EMPTY(&j->header));
1596 }
1597 
1598 uint32_t
dns_journal_first_serial(dns_journal_t * j)1599 dns_journal_first_serial(dns_journal_t *j) {
1600 	return (j->header.begin.serial);
1601 }
1602 
1603 uint32_t
dns_journal_last_serial(dns_journal_t * j)1604 dns_journal_last_serial(dns_journal_t *j) {
1605 	return (j->header.end.serial);
1606 }
1607 
1608 void
dns_journal_set_sourceserial(dns_journal_t * j,uint32_t sourceserial)1609 dns_journal_set_sourceserial(dns_journal_t *j, uint32_t sourceserial) {
1610 	REQUIRE(j->state == JOURNAL_STATE_WRITE ||
1611 		j->state == JOURNAL_STATE_INLINE ||
1612 		j->state == JOURNAL_STATE_TRANSACTION);
1613 
1614 	j->header.sourceserial = sourceserial;
1615 	j->header.serialset = true;
1616 	if (j->state == JOURNAL_STATE_WRITE) {
1617 		j->state = JOURNAL_STATE_INLINE;
1618 	}
1619 }
1620 
1621 bool
dns_journal_get_sourceserial(dns_journal_t * j,uint32_t * sourceserial)1622 dns_journal_get_sourceserial(dns_journal_t *j, uint32_t *sourceserial) {
1623 	REQUIRE(sourceserial != NULL);
1624 
1625 	if (!j->header.serialset) {
1626 		return (false);
1627 	}
1628 	*sourceserial = j->header.sourceserial;
1629 	return (true);
1630 }
1631 
1632 /**************************************************************************/
1633 /*
1634  * Iteration support.
1635  *
1636  * When serving an outgoing IXFR, we transmit a part the journal starting
1637  * at the serial number in the IXFR request and ending at the serial
1638  * number that is current when the IXFR request arrives.  The ending
1639  * serial number is not necessarily at the end of the journal:
1640  * the journal may grow while the IXFR is in progress, but we stop
1641  * when we reach the serial number that was current when the IXFR started.
1642  */
1643 
1644 static isc_result_t
1645 read_one_rr(dns_journal_t *j);
1646 
1647 /*
1648  * Make sure the buffer 'b' is has at least 'size' bytes
1649  * allocated, and clear it.
1650  *
1651  * Requires:
1652  *	Either b->base is NULL, or it points to b->length bytes of memory
1653  *	previously allocated by isc_mem_get().
1654  */
1655 
1656 static isc_result_t
size_buffer(isc_mem_t * mctx,isc_buffer_t * b,unsigned size)1657 size_buffer(isc_mem_t *mctx, isc_buffer_t *b, unsigned size) {
1658 	if (b->length < size) {
1659 		void *mem = isc_mem_get(mctx, size);
1660 		if (mem == NULL) {
1661 			return (ISC_R_NOMEMORY);
1662 		}
1663 		if (b->base != NULL) {
1664 			isc_mem_put(mctx, b->base, b->length);
1665 		}
1666 		b->base = mem;
1667 		b->length = size;
1668 	}
1669 	isc_buffer_clear(b);
1670 	return (ISC_R_SUCCESS);
1671 }
1672 
1673 isc_result_t
dns_journal_iter_init(dns_journal_t * j,uint32_t begin_serial,uint32_t end_serial)1674 dns_journal_iter_init(dns_journal_t *j, uint32_t begin_serial,
1675 		      uint32_t end_serial) {
1676 	isc_result_t result;
1677 
1678 	CHECK(journal_find(j, begin_serial, &j->it.bpos));
1679 	INSIST(j->it.bpos.serial == begin_serial);
1680 
1681 	CHECK(journal_find(j, end_serial, &j->it.epos));
1682 	INSIST(j->it.epos.serial == end_serial);
1683 
1684 	result = ISC_R_SUCCESS;
1685 failure:
1686 	j->it.result = result;
1687 	return (j->it.result);
1688 }
1689 
1690 isc_result_t
dns_journal_first_rr(dns_journal_t * j)1691 dns_journal_first_rr(dns_journal_t *j) {
1692 	isc_result_t result;
1693 
1694 	/*
1695 	 * Seek to the beginning of the first transaction we are
1696 	 * interested in.
1697 	 */
1698 	CHECK(journal_seek(j, j->it.bpos.offset));
1699 	j->it.current_serial = j->it.bpos.serial;
1700 
1701 	j->it.xsize = 0; /* We have no transaction data yet... */
1702 	j->it.xpos = 0;	 /* ...and haven't used any of it. */
1703 
1704 	return (read_one_rr(j));
1705 
1706 failure:
1707 	return (result);
1708 }
1709 
1710 static isc_result_t
read_one_rr(dns_journal_t * j)1711 read_one_rr(dns_journal_t *j) {
1712 	isc_result_t result;
1713 
1714 	dns_rdatatype_t rdtype;
1715 	dns_rdataclass_t rdclass;
1716 	unsigned int rdlen;
1717 	uint32_t ttl;
1718 	journal_xhdr_t xhdr;
1719 	journal_rrhdr_t rrhdr;
1720 
1721 	if (j->offset > j->it.epos.offset) {
1722 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1723 			      "%s: journal corrupt: possible integer overflow",
1724 			      j->filename);
1725 		return (ISC_R_UNEXPECTED);
1726 	}
1727 	if (j->offset == j->it.epos.offset) {
1728 		return (ISC_R_NOMORE);
1729 	}
1730 	if (j->it.xpos == j->it.xsize) {
1731 		/*
1732 		 * We are at a transaction boundary.
1733 		 * Read another transaction header.
1734 		 */
1735 		CHECK(journal_read_xhdr(j, &xhdr));
1736 		if (xhdr.size == 0) {
1737 			isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1738 				      "%s: journal corrupt: empty transaction",
1739 				      j->filename);
1740 			FAIL(ISC_R_UNEXPECTED);
1741 		}
1742 		if (xhdr.serial0 != j->it.current_serial) {
1743 			isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1744 				      "%s: journal file corrupt: "
1745 				      "expected serial %u, got %u",
1746 				      j->filename, j->it.current_serial,
1747 				      xhdr.serial0);
1748 			FAIL(ISC_R_UNEXPECTED);
1749 		}
1750 		j->it.xsize = xhdr.size;
1751 		j->it.xpos = 0;
1752 	}
1753 	/*
1754 	 * Read an RR.
1755 	 */
1756 	CHECK(journal_read_rrhdr(j, &rrhdr));
1757 	/*
1758 	 * Perform a sanity check on the journal RR size.
1759 	 * The smallest possible RR has a 1-byte owner name
1760 	 * and a 10-byte header.  The largest possible
1761 	 * RR has 65535 bytes of data, a header, and a maximum-
1762 	 * size owner name, well below 70 k total.
1763 	 */
1764 	if (rrhdr.size < 1 + 10 || rrhdr.size > 70000) {
1765 		isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1766 			      "%s: journal corrupt: impossible RR size "
1767 			      "(%d bytes)",
1768 			      j->filename, rrhdr.size);
1769 		FAIL(ISC_R_UNEXPECTED);
1770 	}
1771 
1772 	CHECK(size_buffer(j->mctx, &j->it.source, rrhdr.size));
1773 	CHECK(journal_read(j, j->it.source.base, rrhdr.size));
1774 	isc_buffer_add(&j->it.source, rrhdr.size);
1775 
1776 	/*
1777 	 * The target buffer is made the same size
1778 	 * as the source buffer, with the assumption that when
1779 	 * no compression in present, the output of dns_*_fromwire()
1780 	 * is no larger than the input.
1781 	 */
1782 	CHECK(size_buffer(j->mctx, &j->it.target, rrhdr.size));
1783 
1784 	/*
1785 	 * Parse the owner name.  We don't know where it
1786 	 * ends yet, so we make the entire "remaining"
1787 	 * part of the buffer "active".
1788 	 */
1789 	isc_buffer_setactive(&j->it.source,
1790 			     j->it.source.used - j->it.source.current);
1791 	CHECK(dns_name_fromwire(&j->it.name, &j->it.source, &j->it.dctx, 0,
1792 				&j->it.target));
1793 
1794 	/*
1795 	 * Check that the RR header is there, and parse it.
1796 	 */
1797 	if (isc_buffer_remaininglength(&j->it.source) < 10) {
1798 		FAIL(DNS_R_FORMERR);
1799 	}
1800 
1801 	rdtype = isc_buffer_getuint16(&j->it.source);
1802 	rdclass = isc_buffer_getuint16(&j->it.source);
1803 	ttl = isc_buffer_getuint32(&j->it.source);
1804 	rdlen = isc_buffer_getuint16(&j->it.source);
1805 
1806 	/*
1807 	 * Parse the rdata.
1808 	 */
1809 	if (isc_buffer_remaininglength(&j->it.source) != rdlen) {
1810 		FAIL(DNS_R_FORMERR);
1811 	}
1812 	isc_buffer_setactive(&j->it.source, rdlen);
1813 	dns_rdata_reset(&j->it.rdata);
1814 	CHECK(dns_rdata_fromwire(&j->it.rdata, rdclass, rdtype, &j->it.source,
1815 				 &j->it.dctx, 0, &j->it.target));
1816 	j->it.ttl = ttl;
1817 
1818 	j->it.xpos += sizeof(journal_rawrrhdr_t) + rrhdr.size;
1819 	if (rdtype == dns_rdatatype_soa) {
1820 		/* XXX could do additional consistency checks here */
1821 		j->it.current_serial = dns_soa_getserial(&j->it.rdata);
1822 	}
1823 
1824 	result = ISC_R_SUCCESS;
1825 
1826 failure:
1827 	j->it.result = result;
1828 	return (result);
1829 }
1830 
1831 isc_result_t
dns_journal_next_rr(dns_journal_t * j)1832 dns_journal_next_rr(dns_journal_t *j) {
1833 	j->it.result = read_one_rr(j);
1834 	return (j->it.result);
1835 }
1836 
1837 void
dns_journal_current_rr(dns_journal_t * j,dns_name_t ** name,uint32_t * ttl,dns_rdata_t ** rdata)1838 dns_journal_current_rr(dns_journal_t *j, dns_name_t **name, uint32_t *ttl,
1839 		       dns_rdata_t **rdata) {
1840 	REQUIRE(j->it.result == ISC_R_SUCCESS);
1841 	*name = &j->it.name;
1842 	*ttl = j->it.ttl;
1843 	*rdata = &j->it.rdata;
1844 }
1845 
1846 /**************************************************************************/
1847 /*
1848  * Generating diffs from databases
1849  */
1850 
1851 /*
1852  * Construct a diff containing all the RRs at the current name of the
1853  * database iterator 'dbit' in database 'db', version 'ver'.
1854  * Set '*name' to the current name, and append the diff to 'diff'.
1855  * All new tuples will have the operation 'op'.
1856  *
1857  * Requires: 'name' must have buffer large enough to hold the name.
1858  * Typically, a dns_fixedname_t would be used.
1859  */
1860 static isc_result_t
get_name_diff(dns_db_t * db,dns_dbversion_t * ver,isc_stdtime_t now,dns_dbiterator_t * dbit,dns_name_t * name,dns_diffop_t op,dns_diff_t * diff)1861 get_name_diff(dns_db_t *db, dns_dbversion_t *ver, isc_stdtime_t now,
1862 	      dns_dbiterator_t *dbit, dns_name_t *name, dns_diffop_t op,
1863 	      dns_diff_t *diff) {
1864 	isc_result_t result;
1865 	dns_dbnode_t *node = NULL;
1866 	dns_rdatasetiter_t *rdsiter = NULL;
1867 	dns_difftuple_t *tuple = NULL;
1868 
1869 	result = dns_dbiterator_current(dbit, &node, name);
1870 	if (result != ISC_R_SUCCESS) {
1871 		return (result);
1872 	}
1873 
1874 	result = dns_db_allrdatasets(db, node, ver, now, &rdsiter);
1875 	if (result != ISC_R_SUCCESS) {
1876 		goto cleanup_node;
1877 	}
1878 
1879 	for (result = dns_rdatasetiter_first(rdsiter); result == ISC_R_SUCCESS;
1880 	     result = dns_rdatasetiter_next(rdsiter))
1881 	{
1882 		dns_rdataset_t rdataset;
1883 
1884 		dns_rdataset_init(&rdataset);
1885 		dns_rdatasetiter_current(rdsiter, &rdataset);
1886 
1887 		for (result = dns_rdataset_first(&rdataset);
1888 		     result == ISC_R_SUCCESS;
1889 		     result = dns_rdataset_next(&rdataset))
1890 		{
1891 			dns_rdata_t rdata = DNS_RDATA_INIT;
1892 			dns_rdataset_current(&rdataset, &rdata);
1893 			result = dns_difftuple_create(diff->mctx, op, name,
1894 						      rdataset.ttl, &rdata,
1895 						      &tuple);
1896 			if (result != ISC_R_SUCCESS) {
1897 				dns_rdataset_disassociate(&rdataset);
1898 				goto cleanup_iterator;
1899 			}
1900 			dns_diff_append(diff, &tuple);
1901 		}
1902 		dns_rdataset_disassociate(&rdataset);
1903 		if (result != ISC_R_NOMORE) {
1904 			goto cleanup_iterator;
1905 		}
1906 	}
1907 	if (result != ISC_R_NOMORE) {
1908 		goto cleanup_iterator;
1909 	}
1910 
1911 	result = ISC_R_SUCCESS;
1912 
1913 cleanup_iterator:
1914 	dns_rdatasetiter_destroy(&rdsiter);
1915 
1916 cleanup_node:
1917 	dns_db_detachnode(db, &node);
1918 
1919 	return (result);
1920 }
1921 
1922 /*
1923  * Comparison function for use by dns_diff_subtract when sorting
1924  * the diffs to be subtracted.  The sort keys are the rdata type
1925  * and the rdata itself.  The owner name is ignored, because
1926  * it is known to be the same for all tuples.
1927  */
1928 static int
rdata_order(const void * av,const void * bv)1929 rdata_order(const void *av, const void *bv) {
1930 	dns_difftuple_t const *const *ap = av;
1931 	dns_difftuple_t const *const *bp = bv;
1932 	dns_difftuple_t const *a = *ap;
1933 	dns_difftuple_t const *b = *bp;
1934 	int r;
1935 	r = (b->rdata.type - a->rdata.type);
1936 	if (r != 0) {
1937 		return (r);
1938 	}
1939 	r = dns_rdata_compare(&a->rdata, &b->rdata);
1940 	return (r);
1941 }
1942 
1943 static isc_result_t
dns_diff_subtract(dns_diff_t diff[2],dns_diff_t * r)1944 dns_diff_subtract(dns_diff_t diff[2], dns_diff_t *r) {
1945 	isc_result_t result;
1946 	dns_difftuple_t *p[2];
1947 	int i, t;
1948 	bool append;
1949 
1950 	CHECK(dns_diff_sort(&diff[0], rdata_order));
1951 	CHECK(dns_diff_sort(&diff[1], rdata_order));
1952 
1953 	for (;;) {
1954 		p[0] = ISC_LIST_HEAD(diff[0].tuples);
1955 		p[1] = ISC_LIST_HEAD(diff[1].tuples);
1956 		if (p[0] == NULL && p[1] == NULL) {
1957 			break;
1958 		}
1959 
1960 		for (i = 0; i < 2; i++) {
1961 			if (p[!i] == NULL) {
1962 				{
1963 					ISC_LIST_UNLINK(diff[i].tuples, p[i],
1964 							link);
1965 					ISC_LIST_APPEND(r->tuples, p[i], link);
1966 					goto next;
1967 				}
1968 			}
1969 		}
1970 		t = rdata_order(&p[0], &p[1]);
1971 		if (t < 0) {
1972 			ISC_LIST_UNLINK(diff[0].tuples, p[0], link);
1973 			ISC_LIST_APPEND(r->tuples, p[0], link);
1974 			goto next;
1975 		}
1976 		if (t > 0) {
1977 			ISC_LIST_UNLINK(diff[1].tuples, p[1], link);
1978 			ISC_LIST_APPEND(r->tuples, p[1], link);
1979 			goto next;
1980 		}
1981 		INSIST(t == 0);
1982 		/*
1983 		 * Identical RRs in both databases; skip them both
1984 		 * if the ttl differs.
1985 		 */
1986 		append = (p[0]->ttl != p[1]->ttl);
1987 		for (i = 0; i < 2; i++) {
1988 			ISC_LIST_UNLINK(diff[i].tuples, p[i], link);
1989 			if (append) {
1990 				ISC_LIST_APPEND(r->tuples, p[i], link);
1991 			} else {
1992 				dns_difftuple_free(&p[i]);
1993 			}
1994 		}
1995 	next:;
1996 	}
1997 	result = ISC_R_SUCCESS;
1998 failure:
1999 	return (result);
2000 }
2001 
2002 static isc_result_t
diff_namespace(dns_db_t * dba,dns_dbversion_t * dbvera,dns_db_t * dbb,dns_dbversion_t * dbverb,unsigned int options,dns_diff_t * resultdiff)2003 diff_namespace(dns_db_t *dba, dns_dbversion_t *dbvera, dns_db_t *dbb,
2004 	       dns_dbversion_t *dbverb, unsigned int options,
2005 	       dns_diff_t *resultdiff) {
2006 	dns_db_t *db[2];
2007 	dns_dbversion_t *ver[2];
2008 	dns_dbiterator_t *dbit[2] = { NULL, NULL };
2009 	bool have[2] = { false, false };
2010 	dns_fixedname_t fixname[2];
2011 	isc_result_t result, itresult[2];
2012 	dns_diff_t diff[2];
2013 	int i, t;
2014 
2015 	db[0] = dba, db[1] = dbb;
2016 	ver[0] = dbvera, ver[1] = dbverb;
2017 
2018 	dns_diff_init(resultdiff->mctx, &diff[0]);
2019 	dns_diff_init(resultdiff->mctx, &diff[1]);
2020 
2021 	dns_fixedname_init(&fixname[0]);
2022 	dns_fixedname_init(&fixname[1]);
2023 
2024 	result = dns_db_createiterator(db[0], options, &dbit[0]);
2025 	if (result != ISC_R_SUCCESS) {
2026 		return (result);
2027 	}
2028 	result = dns_db_createiterator(db[1], options, &dbit[1]);
2029 	if (result != ISC_R_SUCCESS) {
2030 		goto cleanup_iterator;
2031 	}
2032 
2033 	itresult[0] = dns_dbiterator_first(dbit[0]);
2034 	itresult[1] = dns_dbiterator_first(dbit[1]);
2035 
2036 	for (;;) {
2037 		for (i = 0; i < 2; i++) {
2038 			if (!have[i] && itresult[i] == ISC_R_SUCCESS) {
2039 				CHECK(get_name_diff(
2040 					db[i], ver[i], 0, dbit[i],
2041 					dns_fixedname_name(&fixname[i]),
2042 					i == 0 ? DNS_DIFFOP_ADD
2043 					       : DNS_DIFFOP_DEL,
2044 					&diff[i]));
2045 				itresult[i] = dns_dbiterator_next(dbit[i]);
2046 				have[i] = true;
2047 			}
2048 		}
2049 
2050 		if (!have[0] && !have[1]) {
2051 			INSIST(ISC_LIST_EMPTY(diff[0].tuples));
2052 			INSIST(ISC_LIST_EMPTY(diff[1].tuples));
2053 			break;
2054 		}
2055 
2056 		for (i = 0; i < 2; i++) {
2057 			if (!have[!i]) {
2058 				ISC_LIST_APPENDLIST(resultdiff->tuples,
2059 						    diff[i].tuples, link);
2060 				INSIST(ISC_LIST_EMPTY(diff[i].tuples));
2061 				have[i] = false;
2062 				goto next;
2063 			}
2064 		}
2065 
2066 		t = dns_name_compare(dns_fixedname_name(&fixname[0]),
2067 				     dns_fixedname_name(&fixname[1]));
2068 		if (t < 0) {
2069 			ISC_LIST_APPENDLIST(resultdiff->tuples, diff[0].tuples,
2070 					    link);
2071 			INSIST(ISC_LIST_EMPTY(diff[0].tuples));
2072 			have[0] = false;
2073 			continue;
2074 		}
2075 		if (t > 0) {
2076 			ISC_LIST_APPENDLIST(resultdiff->tuples, diff[1].tuples,
2077 					    link);
2078 			INSIST(ISC_LIST_EMPTY(diff[1].tuples));
2079 			have[1] = false;
2080 			continue;
2081 		}
2082 		INSIST(t == 0);
2083 		CHECK(dns_diff_subtract(diff, resultdiff));
2084 		INSIST(ISC_LIST_EMPTY(diff[0].tuples));
2085 		INSIST(ISC_LIST_EMPTY(diff[1].tuples));
2086 		have[0] = have[1] = false;
2087 	next:;
2088 	}
2089 	if (itresult[0] != ISC_R_NOMORE) {
2090 		FAIL(itresult[0]);
2091 	}
2092 	if (itresult[1] != ISC_R_NOMORE) {
2093 		FAIL(itresult[1]);
2094 	}
2095 
2096 	INSIST(ISC_LIST_EMPTY(diff[0].tuples));
2097 	INSIST(ISC_LIST_EMPTY(diff[1].tuples));
2098 
2099 failure:
2100 	dns_dbiterator_destroy(&dbit[1]);
2101 
2102 cleanup_iterator:
2103 	dns_dbiterator_destroy(&dbit[0]);
2104 	dns_diff_clear(&diff[0]);
2105 	dns_diff_clear(&diff[1]);
2106 	return (result);
2107 }
2108 
2109 /*
2110  * Compare the databases 'dba' and 'dbb' and generate a journal
2111  * entry containing the changes to make 'dba' from 'dbb' (note
2112  * the order).  This journal entry will consist of a single,
2113  * possibly very large transaction.
2114  */
2115 isc_result_t
dns_db_diff(isc_mem_t * mctx,dns_db_t * dba,dns_dbversion_t * dbvera,dns_db_t * dbb,dns_dbversion_t * dbverb,const char * filename)2116 dns_db_diff(isc_mem_t *mctx, dns_db_t *dba, dns_dbversion_t *dbvera,
2117 	    dns_db_t *dbb, dns_dbversion_t *dbverb, const char *filename) {
2118 	isc_result_t result;
2119 	dns_diff_t diff;
2120 
2121 	dns_diff_init(mctx, &diff);
2122 
2123 	result = dns_db_diffx(&diff, dba, dbvera, dbb, dbverb, filename);
2124 
2125 	dns_diff_clear(&diff);
2126 
2127 	return (result);
2128 }
2129 
2130 isc_result_t
dns_db_diffx(dns_diff_t * diff,dns_db_t * dba,dns_dbversion_t * dbvera,dns_db_t * dbb,dns_dbversion_t * dbverb,const char * filename)2131 dns_db_diffx(dns_diff_t *diff, dns_db_t *dba, dns_dbversion_t *dbvera,
2132 	     dns_db_t *dbb, dns_dbversion_t *dbverb, const char *filename) {
2133 	isc_result_t result;
2134 	dns_journal_t *journal = NULL;
2135 
2136 	if (filename != NULL) {
2137 		result = dns_journal_open(diff->mctx, filename,
2138 					  DNS_JOURNAL_CREATE, &journal);
2139 		if (result != ISC_R_SUCCESS) {
2140 			return (result);
2141 		}
2142 	}
2143 
2144 	CHECK(diff_namespace(dba, dbvera, dbb, dbverb, DNS_DB_NONSEC3, diff));
2145 	CHECK(diff_namespace(dba, dbvera, dbb, dbverb, DNS_DB_NSEC3ONLY, diff));
2146 
2147 	if (journal != NULL) {
2148 		if (ISC_LIST_EMPTY(diff->tuples)) {
2149 			isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no changes");
2150 		} else {
2151 			CHECK(dns_journal_write_transaction(journal, diff));
2152 		}
2153 	}
2154 
2155 failure:
2156 	if (journal != NULL) {
2157 		dns_journal_destroy(&journal);
2158 	}
2159 	return (result);
2160 }
2161 
2162 isc_result_t
dns_journal_compact(isc_mem_t * mctx,char * filename,uint32_t serial,uint32_t target_size)2163 dns_journal_compact(isc_mem_t *mctx, char *filename, uint32_t serial,
2164 		    uint32_t target_size) {
2165 	unsigned int i;
2166 	journal_pos_t best_guess;
2167 	journal_pos_t current_pos;
2168 	dns_journal_t *j1 = NULL;
2169 	dns_journal_t *j2 = NULL;
2170 	journal_rawheader_t rawheader;
2171 	unsigned int copy_length;
2172 	size_t namelen;
2173 	char *buf = NULL;
2174 	unsigned int size = 0;
2175 	isc_result_t result;
2176 	unsigned int indexend;
2177 	char newname[PATH_MAX];
2178 	char backup[PATH_MAX];
2179 	bool is_backup = false;
2180 
2181 	REQUIRE(filename != NULL);
2182 
2183 	namelen = strlen(filename);
2184 	if (namelen > 4U && strcmp(filename + namelen - 4, ".jnl") == 0) {
2185 		namelen -= 4;
2186 	}
2187 
2188 	result = snprintf(newname, sizeof(newname), "%.*s.jnw", (int)namelen,
2189 			  filename);
2190 	RUNTIME_CHECK(result < sizeof(newname));
2191 
2192 	result = snprintf(backup, sizeof(backup), "%.*s.jbk", (int)namelen,
2193 			  filename);
2194 	RUNTIME_CHECK(result < sizeof(backup));
2195 
2196 	result = journal_open(mctx, filename, false, false, &j1);
2197 	if (result == ISC_R_NOTFOUND) {
2198 		is_backup = true;
2199 		result = journal_open(mctx, backup, false, false, &j1);
2200 	}
2201 	if (result != ISC_R_SUCCESS) {
2202 		return (result);
2203 	}
2204 
2205 	if (JOURNAL_EMPTY(&j1->header)) {
2206 		dns_journal_destroy(&j1);
2207 		return (ISC_R_SUCCESS);
2208 	}
2209 
2210 	if (DNS_SERIAL_GT(j1->header.begin.serial, serial) ||
2211 	    DNS_SERIAL_GT(serial, j1->header.end.serial))
2212 	{
2213 		dns_journal_destroy(&j1);
2214 		return (ISC_R_RANGE);
2215 	}
2216 
2217 	/*
2218 	 * Cope with very small target sizes.
2219 	 */
2220 	indexend = sizeof(journal_rawheader_t) +
2221 		   j1->header.index_size * sizeof(journal_rawpos_t);
2222 	if (target_size < DNS_JOURNAL_SIZE_MIN) {
2223 		target_size = DNS_JOURNAL_SIZE_MIN;
2224 	}
2225 	if (target_size < indexend * 2) {
2226 		target_size = target_size / 2 + indexend;
2227 	}
2228 
2229 	/*
2230 	 * See if there is any work to do.
2231 	 */
2232 	if ((uint32_t)j1->header.end.offset < target_size) {
2233 		dns_journal_destroy(&j1);
2234 		return (ISC_R_SUCCESS);
2235 	}
2236 
2237 	CHECK(journal_open(mctx, newname, true, true, &j2));
2238 
2239 	/*
2240 	 * Remove overhead so space test below can succeed.
2241 	 */
2242 	if (target_size >= indexend) {
2243 		target_size -= indexend;
2244 	}
2245 
2246 	/*
2247 	 * Find if we can create enough free space.
2248 	 */
2249 	best_guess = j1->header.begin;
2250 	for (i = 0; i < j1->header.index_size; i++) {
2251 		if (POS_VALID(j1->index[i]) &&
2252 		    DNS_SERIAL_GE(serial, j1->index[i].serial) &&
2253 		    ((uint32_t)(j1->header.end.offset - j1->index[i].offset) >=
2254 		     target_size / 2) &&
2255 		    j1->index[i].offset > best_guess.offset)
2256 		{
2257 			best_guess = j1->index[i];
2258 		}
2259 	}
2260 
2261 	current_pos = best_guess;
2262 	while (current_pos.serial != serial) {
2263 		CHECK(journal_next(j1, &current_pos));
2264 		if (current_pos.serial == j1->header.end.serial) {
2265 			break;
2266 		}
2267 
2268 		if (DNS_SERIAL_GE(serial, current_pos.serial) &&
2269 		    ((uint32_t)(j1->header.end.offset - current_pos.offset) >=
2270 		     (target_size / 2)) &&
2271 		    current_pos.offset > best_guess.offset)
2272 		{
2273 			best_guess = current_pos;
2274 		} else {
2275 			break;
2276 		}
2277 	}
2278 
2279 	INSIST(best_guess.serial != j1->header.end.serial);
2280 	if (best_guess.serial != serial) {
2281 		CHECK(journal_next(j1, &best_guess));
2282 	}
2283 
2284 	/*
2285 	 * We should now be roughly half target_size provided
2286 	 * we did not reach 'serial'.  If not we will just copy
2287 	 * all uncommitted deltas regardless of the size.
2288 	 */
2289 	copy_length = j1->header.end.offset - best_guess.offset;
2290 
2291 	if (copy_length != 0) {
2292 		/*
2293 		 * Copy best_guess to end into space just freed.
2294 		 */
2295 		size = 64 * 1024;
2296 		if (copy_length < size) {
2297 			size = copy_length;
2298 		}
2299 		buf = isc_mem_get(mctx, size);
2300 
2301 		CHECK(journal_seek(j1, best_guess.offset));
2302 		CHECK(journal_seek(j2, indexend));
2303 		for (i = 0; i < copy_length; i += size) {
2304 			unsigned int len = (copy_length - i) > size
2305 						   ? size
2306 						   : (copy_length - i);
2307 			CHECK(journal_read(j1, buf, len));
2308 			CHECK(journal_write(j2, buf, len));
2309 		}
2310 
2311 		CHECK(journal_fsync(j2));
2312 
2313 		/*
2314 		 * Compute new header.
2315 		 */
2316 		j2->header.begin.serial = best_guess.serial;
2317 		j2->header.begin.offset = indexend;
2318 		j2->header.end.serial = j1->header.end.serial;
2319 		j2->header.end.offset = indexend + copy_length;
2320 		j2->header.sourceserial = j1->header.sourceserial;
2321 		j2->header.serialset = j1->header.serialset;
2322 
2323 		/*
2324 		 * Update the journal header.
2325 		 */
2326 		journal_header_encode(&j2->header, &rawheader);
2327 		CHECK(journal_seek(j2, 0));
2328 		CHECK(journal_write(j2, &rawheader, sizeof(rawheader)));
2329 		CHECK(journal_fsync(j2));
2330 
2331 		/*
2332 		 * Build new index.
2333 		 */
2334 		current_pos = j2->header.begin;
2335 		while (current_pos.serial != j2->header.end.serial) {
2336 			index_add(j2, &current_pos);
2337 			CHECK(journal_next(j2, &current_pos));
2338 		}
2339 
2340 		/*
2341 		 * Write index.
2342 		 */
2343 		CHECK(index_to_disk(j2));
2344 		CHECK(journal_fsync(j2));
2345 
2346 		indexend = j2->header.end.offset;
2347 		POST(indexend);
2348 	}
2349 
2350 	/*
2351 	 * Close both journals before trying to rename files (this is
2352 	 * necessary on WIN32).
2353 	 */
2354 	dns_journal_destroy(&j1);
2355 	dns_journal_destroy(&j2);
2356 
2357 	/*
2358 	 * With a UFS file system this should just succeed and be atomic.
2359 	 * Any IXFR outs will just continue and the old journal will be
2360 	 * removed on final close.
2361 	 *
2362 	 * With MSDOS / NTFS we need to do a two stage rename, triggered
2363 	 * by EEXIST.  (If any IXFR's are running in other threads, however,
2364 	 * this will fail, and the journal will not be compacted.  But
2365 	 * if so, hopefully they'll be finished by the next time we
2366 	 * compact.)
2367 	 */
2368 	if (rename(newname, filename) == -1) {
2369 		if (errno == EEXIST && !is_backup) {
2370 			result = isc_file_remove(backup);
2371 			if (result != ISC_R_SUCCESS &&
2372 			    result != ISC_R_FILENOTFOUND) {
2373 				goto failure;
2374 			}
2375 			if (rename(filename, backup) == -1) {
2376 				goto maperrno;
2377 			}
2378 			if (rename(newname, filename) == -1) {
2379 				goto maperrno;
2380 			}
2381 			(void)isc_file_remove(backup);
2382 		} else {
2383 		maperrno:
2384 			result = ISC_R_FAILURE;
2385 			goto failure;
2386 		}
2387 	}
2388 
2389 	result = ISC_R_SUCCESS;
2390 
2391 failure:
2392 	(void)isc_file_remove(newname);
2393 	if (buf != NULL) {
2394 		isc_mem_put(mctx, buf, size);
2395 	}
2396 	if (j1 != NULL) {
2397 		dns_journal_destroy(&j1);
2398 	}
2399 	if (j2 != NULL) {
2400 		dns_journal_destroy(&j2);
2401 	}
2402 	return (result);
2403 }
2404 
2405 static isc_result_t
index_to_disk(dns_journal_t * j)2406 index_to_disk(dns_journal_t *j) {
2407 	isc_result_t result = ISC_R_SUCCESS;
2408 
2409 	if (j->header.index_size != 0) {
2410 		unsigned int i;
2411 		unsigned char *p;
2412 		unsigned int rawbytes;
2413 
2414 		rawbytes = j->header.index_size * sizeof(journal_rawpos_t);
2415 
2416 		p = j->rawindex;
2417 		for (i = 0; i < j->header.index_size; i++) {
2418 			encode_uint32(j->index[i].serial, p);
2419 			p += 4;
2420 			encode_uint32(j->index[i].offset, p);
2421 			p += 4;
2422 		}
2423 		INSIST(p == j->rawindex + rawbytes);
2424 
2425 		CHECK(journal_seek(j, sizeof(journal_rawheader_t)));
2426 		CHECK(journal_write(j, j->rawindex, rawbytes));
2427 	}
2428 failure:
2429 	return (result);
2430 }
2431