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