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