Merge from vendor branch WPA_SUPPLICANT:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / journal.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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.77.2.2 2004/03/09 06:11:02 marka Exp $ */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include <isc/file.h>
25 #include <isc/mem.h>
26 #include <isc/stdio.h>
27 #include <isc/string.h>
28 #include <isc/util.h>
29
30 #include <dns/compress.h>
31 #include <dns/db.h>
32 #include <dns/dbiterator.h>
33 #include <dns/diff.h>
34 #include <dns/fixedname.h>
35 #include <dns/journal.h>
36 #include <dns/log.h>
37 #include <dns/rdataset.h>
38 #include <dns/rdatasetiter.h>
39 #include <dns/result.h>
40 #include <dns/soa.h>
41
42 /*
43  * When true, accept IXFR difference sequences where the
44  * SOA serial number does not change (BIND 8 sends such
45  * sequences).
46  */
47 static isc_boolean_t bind8_compat = ISC_TRUE; /* XXX config */
48
49 /**************************************************************************/
50 /*
51  * Miscellaneous utilities.
52  */
53
54 #define JOURNAL_COMMON_LOGARGS \
55         dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_JOURNAL
56
57 #define JOURNAL_DEBUG_LOGARGS(n) \
58         JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(n)
59
60 /*
61  * It would be non-sensical (or at least obtuse) to use FAIL() with an
62  * ISC_R_SUCCESS code, but the test is there to keep the Solaris compiler
63  * from complaining about "end-of-loop code not reached".
64  */
65 #define FAIL(code) \
66         do { result = (code);                                   \
67                 if (result != ISC_R_SUCCESS) goto failure;      \
68         } while (0)
69
70 #define CHECK(op) \
71         do { result = (op);                                     \
72                 if (result != ISC_R_SUCCESS) goto failure;      \
73         } while (0)
74
75 static inline isc_uint32_t
76 decode_uint32(unsigned char *p) {
77         return ((p[0] << 24) +
78                 (p[1] << 16) +
79                 (p[2] <<  8) +
80                 (p[3] <<  0));
81 }
82
83 static inline void
84 encode_uint32(isc_uint32_t val, unsigned char *p) {
85         p[0] = (isc_uint8_t)(val >> 24);
86         p[1] = (isc_uint8_t)(val >> 16);
87         p[2] = (isc_uint8_t)(val >>  8);
88         p[3] = (isc_uint8_t)(val >>  0);
89 }
90
91 isc_result_t
92 dns_db_createsoatuple(dns_db_t *db, dns_dbversion_t *ver, isc_mem_t *mctx,
93                       dns_diffop_t op, dns_difftuple_t **tp)
94 {
95         isc_result_t result;
96         dns_dbnode_t *node;
97         dns_rdataset_t rdataset;
98         dns_rdata_t rdata = DNS_RDATA_INIT;
99         dns_name_t *zonename;
100
101         zonename = dns_db_origin(db);
102
103         node = NULL;
104         result = dns_db_findnode(db, zonename, ISC_FALSE, &node);
105         if (result != ISC_R_SUCCESS)
106                 goto nonode;
107
108         dns_rdataset_init(&rdataset);
109         result = dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, 0,
110                                      (isc_stdtime_t)0, &rdataset, NULL);
111         if (result != ISC_R_SUCCESS)
112                 goto freenode;
113
114         result = dns_rdataset_first(&rdataset);
115         if (result != ISC_R_SUCCESS)
116                 goto freenode;
117
118         dns_rdataset_current(&rdataset, &rdata);
119
120         result = dns_difftuple_create(mctx, op, zonename, rdataset.ttl,
121                                       &rdata, tp);
122
123         dns_rdataset_disassociate(&rdataset);
124         dns_db_detachnode(db, &node);
125         return (ISC_R_SUCCESS);
126
127  freenode:
128         dns_db_detachnode(db, &node);
129  nonode:
130         UNEXPECTED_ERROR(__FILE__, __LINE__, "missing SOA");
131         return (result);
132 }
133
134 /**************************************************************************/
135 /*
136  * Journalling.
137  */
138
139 /*
140  * A journal file consists of
141  *
142  *   - A fixed-size header of type journal_rawheader_t.
143  *
144  *   - The index.  This is an unordered array of index entries
145  *     of type journal_rawpos_t giving the locations
146  *     of some arbitrary subset of the journal's addressable
147  *     transactions.  The index entries are used as hints to
148  *     speed up the process of locating a transaction with a given
149  *     serial number.  Unused index entries have an "offset"
150  *     field of zero.  The size of the index can vary between
151  *     journal files, but does not change during the lifetime
152  *     of a file.  The size can be zero.
153  *
154  *   - The journal data.  This  consists of one or more transactions.
155  *     Each transaction begins with a transaction header of type
156  *     journal_rawxhdr_t.  The transaction header is followed by a
157  *     sequence of RRs, similar in structure to an IXFR difference
158  *     sequence (RFC1995).  That is, the pre-transaction SOA,
159  *     zero or more other deleted RRs, the post-transaction SOA,
160  *     and zero or more other added RRs.  Unlike in IXFR, each RR
161  *     is prefixed with a 32-bit length.
162  *
163  *     The journal data part grows as new transactions are
164  *     appended to the file.  Only those transactions
165  *     whose serial number is current-(2^31-1) to current
166  *     are considered "addressable" and may be pointed
167  *     to from the header or index.  They may be preceded
168  *     by old transactions that are no longer addressable,
169  *     and they may be followed by transactions that were
170  *     appended to the journal but never committed by updating
171  *     the "end" position in the header.  The latter will
172  *     be overwritten when new transactions are added.
173  */
174
175 /*
176  * On-disk representation of a "pointer" to a journal entry.
177  * These are used in the journal header to locate the beginning
178  * and end of the journal, and in the journal index to locate
179  * other transactions.
180  */
181 typedef struct {
182         unsigned char   serial[4];  /* SOA serial before update. */
183         /*
184          * XXXRTH  Should offset be 8 bytes?
185          * XXXDCL ... probably, since isc_offset_t is 8 bytes on many OSs.
186          * XXXAG  ... but we will not be able to seek >2G anyway on many
187          *            platforms as long as we are using fseek() rather
188          *            than lseek().
189          */
190         unsigned char   offset[4];  /* Offset from beginning of file. */
191 } journal_rawpos_t;
192
193 /*
194  * The on-disk representation of the journal header.
195  * All numbers are stored in big-endian order.
196  */
197
198 /*
199  * The header is of a fixed size, with some spare room for future
200  * extensions.
201  */
202 #define JOURNAL_HEADER_SIZE 64 /* Bytes. */
203
204 typedef union {
205         struct {
206                 /* File format version ID. */
207                 unsigned char           format[16];
208                 /* Position of the first addressable transaction */
209                 journal_rawpos_t        begin;
210                 /* Position of the next (yet nonexistent) transaction. */
211                 journal_rawpos_t        end;
212                 /* Number of index entries following the header. */
213                 unsigned char           index_size[4];
214         } h;
215         /* Pad the header to a fixed size. */
216         unsigned char pad[JOURNAL_HEADER_SIZE];
217 } journal_rawheader_t;
218
219 /*
220  * The on-disk representation of the transaction header.
221  * There is one of these at the beginning of each transaction.
222  */
223 typedef struct {
224         unsigned char   size[4];        /* In bytes, excluding header. */
225         unsigned char   serial0[4];     /* SOA serial before update. */
226         unsigned char   serial1[4];     /* SOA serial after update. */
227 } journal_rawxhdr_t;
228
229 /*
230  * The on-disk representation of the RR header.
231  * There is one of these at the beginning of each RR.
232  */
233 typedef struct {
234         unsigned char   size[4];        /* In bytes, excluding header. */
235 } journal_rawrrhdr_t;
236
237 /*
238  * The in-core representation of the journal header.
239  */
240 typedef struct {
241         isc_uint32_t    serial;
242         isc_offset_t    offset;
243 } journal_pos_t;
244
245 #define POS_VALID(pos)          ((pos).offset != 0)
246 #define POS_INVALIDATE(pos)     ((pos).offset = 0, (pos).serial = 0)
247
248 typedef struct {
249         unsigned char   format[16];
250         journal_pos_t   begin;
251         journal_pos_t   end;
252         isc_uint32_t    index_size;
253 } journal_header_t;
254
255 /*
256  * The in-core representation of the transaction header.
257  */
258
259 typedef struct {
260         isc_uint32_t    size;
261         isc_uint32_t    serial0;
262         isc_uint32_t    serial1;
263 } journal_xhdr_t;
264
265 /*
266  * The in-core representation of the RR header.
267  */
268 typedef struct {
269         isc_uint32_t    size;
270 } journal_rrhdr_t;
271
272
273 /*
274  * Initial contents to store in the header of a newly created
275  * journal file.
276  *
277  * The header starts with the magic string ";BIND LOG V9\n"
278  * to identify the file as a BIND 9 journal file.  An ASCII
279  * identification string is used rather than a binary magic
280  * number to be consistent with BIND 8 (BIND 8 journal files
281  * are ASCII text files).
282  */
283
284 static journal_header_t
285 initial_journal_header = { ";BIND LOG V9\n", { 0, 0 }, { 0, 0 }, 0 };
286
287 #define JOURNAL_EMPTY(h) ((h)->begin.offset == (h)->end.offset)
288
289 typedef enum {
290         JOURNAL_STATE_INVALID,
291         JOURNAL_STATE_READ,
292         JOURNAL_STATE_WRITE,
293         JOURNAL_STATE_TRANSACTION
294 } journal_state_t;
295
296 struct dns_journal {
297         unsigned int            magic;          /* JOUR */
298         isc_mem_t               *mctx;          /* Memory context */
299         journal_state_t         state;
300         const char              *filename;      /* Journal file name */
301         FILE *                  fp;             /* File handle */
302         isc_offset_t            offset;         /* Current file offset */
303         journal_header_t        header;         /* In-core journal header */
304         unsigned char           *rawindex;      /* In-core buffer for journal
305                                                    index in on-disk format */
306         journal_pos_t           *index;         /* In-core journal index */
307
308         /* Current transaction state (when writing). */
309         struct {
310                 unsigned int    n_soa;          /* Number of SOAs seen */
311                 journal_pos_t   pos[2];         /* Begin/end position */
312         } x;
313
314         /* Iteration state (when reading). */
315         struct {
316                 /* These define the part of the journal we iterate over. */
317                 journal_pos_t bpos;             /* Position before first, */
318                 journal_pos_t epos;             /* and after last
319                                                    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         memcpy(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         memcpy(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 += 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 += 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         memcpy(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
542 isc_result_t
543 dns_journal_open(isc_mem_t *mctx, const char *filename, isc_boolean_t write,
544                  dns_journal_t **journalp) {
545         FILE *fp = NULL;
546         isc_result_t result;
547         journal_rawheader_t rawheader;
548         dns_journal_t *j;
549
550         INSIST(journalp != NULL && *journalp == NULL);
551         j = isc_mem_get(mctx, sizeof(*j));
552         if (j == NULL)
553                 return (ISC_R_NOMEMORY);
554
555         j->mctx = mctx;
556         j->state = JOURNAL_STATE_INVALID;
557         j->fp = NULL;
558         j->filename = filename;
559         j->index = NULL;
560         j->rawindex = NULL;
561
562         result = isc_stdio_open(j->filename, write ? "rb+" : "rb", &fp);
563
564         if (result == ISC_R_FILENOTFOUND) {
565                 if (write) {
566                         isc_log_write(JOURNAL_COMMON_LOGARGS,
567                                       ISC_LOG_INFO,
568                                       "journal file %s does not exist, "
569                                       "creating it",
570                                       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 uncheched 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->fp != NULL)
667                 (void)isc_stdio_close(j->fp);
668         isc_mem_put(j->mctx, j, sizeof(*j));
669         return (result);
670 }
671
672 /*
673  * A comparison function defining the sorting order for
674  * entries in the IXFR-style journal file.
675  *
676  * The IXFR format requires that deletions are sorted before
677  * additions, and within either one, SOA records are sorted
678  * before others.
679  *
680  * Also sort the non-SOA records by type as a courtesy to the
681  * server receiving the IXFR - it may help reduce the amount of
682  * rdataset merging it has to do.
683  */
684 static int
685 ixfr_order(const void *av, const void *bv) {
686         dns_difftuple_t const * const *ap = av;
687         dns_difftuple_t const * const *bp = bv;
688         dns_difftuple_t const *a = *ap;
689         dns_difftuple_t const *b = *bp;
690         int r;
691
692         r = (b->op == DNS_DIFFOP_DEL) - (a->op == DNS_DIFFOP_DEL);
693         if (r != 0)
694                 return (r);
695
696         r = (b->rdata.type == dns_rdatatype_soa) -
697                 (a->rdata.type == dns_rdatatype_soa);
698         if (r != 0)
699                 return (r);
700
701         r = (a->rdata.type - b->rdata.type);
702         return (r);
703 }
704
705 /*
706  * Advance '*pos' to the next journal transaction.
707  *
708  * Requires:
709  *      *pos refers to a valid journal transaction.
710  *
711  * Ensures:
712  *      When ISC_R_SUCCESS is returned,
713  *      *pos refers to the next journal transaction.
714  *
715  * Returns one of:
716  *
717  *    ISC_R_SUCCESS
718  *    ISC_R_NOMORE      *pos pointed at the last transaction
719  *    Other results due to file errors are possible.
720  */
721 static isc_result_t
722 journal_next(dns_journal_t *j, journal_pos_t *pos) {
723         isc_result_t result;
724         journal_xhdr_t xhdr;
725         REQUIRE(DNS_JOURNAL_VALID(j));
726
727         result = journal_seek(j, pos->offset);
728         if (result != ISC_R_SUCCESS)
729                 return (result);
730
731         /*
732          * Read the header of the current transaction.
733          * This will return ISC_R_NOMORE if we are at EOF.
734          */
735         result = journal_read_xhdr(j, &xhdr);
736         if (result != ISC_R_SUCCESS)
737                 return (result);
738
739         /*
740          * Check serial number consistency.
741          */
742         if (xhdr.serial0 != pos->serial) {
743                 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
744                               "%s: journal file corrupt: "
745                               "expected serial %u, got %u",
746                               j->filename, pos->serial, xhdr.serial0);
747                 return (ISC_R_UNEXPECTED);
748         }
749
750         /*
751          * Check for offset wraparound.
752          */
753         if ((isc_offset_t)(pos->offset + sizeof(journal_rawxhdr_t) + xhdr.size)
754             < pos->offset) {
755                 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
756                               "%s: offset too large", j->filename);
757                 return (ISC_R_UNEXPECTED);
758         }
759
760         pos->offset += sizeof(journal_rawxhdr_t) + xhdr.size;
761         pos->serial = xhdr.serial1;
762         return (ISC_R_SUCCESS);
763 }
764
765 /*
766  * If the index of the journal 'j' contains an entry "better"
767  * than '*best_guess', replace '*best_guess' with it.
768  *
769  * "Better" means having a serial number closer to 'serial'
770  * but not greater than 'serial'.
771  */
772 static void
773 index_find(dns_journal_t *j, isc_uint32_t serial, journal_pos_t *best_guess) {
774         unsigned int i;
775         if (j->index == NULL)
776                 return;
777         for (i = 0; i < j->header.index_size; i++) {
778                 if (POS_VALID(j->index[i]) &&
779                     DNS_SERIAL_GE(serial, j->index[i].serial) &&
780                     DNS_SERIAL_GT(j->index[i].serial, best_guess->serial))
781                         *best_guess = j->index[i];
782         }
783 }
784
785 /*
786  * Add a new index entry.  If there is no room, make room by removing
787  * the odd-numbered entries and compacting the others into the first
788  * half of the index.  This decimates old index entries exponentially
789  * over time, so that the index always contains a much larger fraction
790  * of recent serial numbers than of old ones.  This is deliberate -
791  * most index searches are for outgoing IXFR, and IXFR tends to request
792  * recent versions more often than old ones.
793  */
794 static void
795 index_add(dns_journal_t *j, journal_pos_t *pos) {
796         unsigned int i;
797         if (j->index == NULL)
798                 return;
799         /*
800          * Search for a vacant position.
801          */
802         for (i = 0; i < j->header.index_size; i++) {
803                 if (! POS_VALID(j->index[i]))
804                         break;
805         }
806         if (i == j->header.index_size) {
807                 unsigned int k = 0;
808                 /*
809                  * Found no vacant position.  Make some room.
810                  */
811                 for (i = 0; i < j->header.index_size; i += 2) {
812                         j->index[k++] = j->index[i];
813                 }
814                 i = k; /* 'i' identifies the first vacant position. */
815                 while (k < j->header.index_size) {
816                         POS_INVALIDATE(j->index[k]);
817                         k++;
818                 }
819         }
820         INSIST(i < j->header.index_size);
821         INSIST(! POS_VALID(j->index[i]));
822
823         /*
824          * Store the new index entry.
825          */
826         j->index[i] = *pos;
827 }
828
829 /*
830  * Invalidate any existing index entries that could become
831  * ambiguous when a new transaction with number 'serial' is added.
832  */
833 static void
834 index_invalidate(dns_journal_t *j, isc_uint32_t serial) {
835         unsigned int i;
836         if (j->index == NULL)
837                 return;
838         for (i = 0; i < j->header.index_size; i++) {
839                 if (! DNS_SERIAL_GT(serial, j->index[i].serial))
840                         POS_INVALIDATE(j->index[i]);
841         }
842 }
843
844 /*
845  * Try to find a transaction with initial serial number 'serial'
846  * in the journal 'j'.
847  *
848  * If found, store its position at '*pos' and return ISC_R_SUCCESS.
849  *
850  * If 'serial' is current (= the ending serial number of the
851  * last transaction in the journal), set '*pos' to
852  * the position immediately following the last transaction and
853  * return ISC_R_SUCCESS.
854  *
855  * If 'serial' is within the range of addressable serial numbers
856  * covered by the journal but that particular serial number is missing
857  * (from the journal, not just from the index), return ISC_R_NOTFOUND.
858  *
859  * If 'serial' is outside the range of addressable serial numbers
860  * covered by the journal, return ISC_R_RANGE.
861  *
862  */
863 static isc_result_t
864 journal_find(dns_journal_t *j, isc_uint32_t serial, journal_pos_t *pos) {
865         isc_result_t result;
866         journal_pos_t current_pos;
867         REQUIRE(DNS_JOURNAL_VALID(j));
868
869         if (DNS_SERIAL_GT(j->header.begin.serial, serial))
870                 return (ISC_R_RANGE);
871         if (DNS_SERIAL_GT(serial, j->header.end.serial))
872                 return (ISC_R_RANGE);
873         if (serial == j->header.end.serial) {
874                 *pos = j->header.end;
875                 return (ISC_R_SUCCESS);
876         }
877
878         current_pos = j->header.begin;
879         index_find(j, serial, &current_pos);
880
881         while (current_pos.serial != serial) {
882                 if (DNS_SERIAL_GT(current_pos.serial, serial))
883                         return (ISC_R_NOTFOUND);
884                 result = journal_next(j, &current_pos);
885                 if (result != ISC_R_SUCCESS)
886                         return (result);
887         }
888         *pos = current_pos;
889         return (ISC_R_SUCCESS);
890 }
891
892 isc_result_t
893 dns_journal_begin_transaction(dns_journal_t *j) {
894         isc_uint32_t offset;
895         isc_result_t result;
896         journal_rawxhdr_t hdr;
897
898         REQUIRE(DNS_JOURNAL_VALID(j));
899         REQUIRE(j->state == JOURNAL_STATE_WRITE);
900
901         /*
902          * Find the file offset where the new transaction should
903          * be written, and seek there.
904          */
905         if (JOURNAL_EMPTY(&j->header)) {
906                 offset = sizeof(journal_rawheader_t) +
907                         j->header.index_size * sizeof(journal_rawpos_t);
908         } else {
909                 offset = j->header.end.offset;
910         }
911         j->x.pos[0].offset = offset;
912         j->x.pos[1].offset = offset; /* Initial value, will be incremented. */
913         j->x.n_soa = 0;
914
915         CHECK(journal_seek(j, offset));
916
917         /*
918          * Write a dummy transaction header of all zeroes to reserve
919          * space.  It will be filled in when the transaction is
920          * finished.
921          */
922         memset(&hdr, 0, sizeof(hdr));
923         CHECK(journal_write(j, &hdr, sizeof(hdr)));
924         j->x.pos[1].offset = j->offset;
925
926         j->state = JOURNAL_STATE_TRANSACTION;
927         result = ISC_R_SUCCESS;
928  failure:
929         return (result);
930 }
931
932 isc_result_t
933 dns_journal_writediff(dns_journal_t *j, dns_diff_t *diff) {
934         dns_difftuple_t *t;
935         isc_buffer_t buffer;
936         void *mem = NULL;
937         unsigned int size;
938         isc_result_t result;
939         isc_region_t used;
940
941         REQUIRE(DNS_DIFF_VALID(diff));
942         REQUIRE(j->state == JOURNAL_STATE_TRANSACTION);
943
944         isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "writing to journal");
945         dns_diff_print(diff, NULL);
946
947         /*
948          * Pass 1: determine the buffer size needed, and
949          * keep track of SOA serial numbers.
950          */
951         size = 0;
952         for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
953              t = ISC_LIST_NEXT(t, link))
954         {
955                 if (t->rdata.type == dns_rdatatype_soa) {
956                         if (j->x.n_soa < 2)
957                                 j->x.pos[j->x.n_soa].serial =
958                                         dns_soa_getserial(&t->rdata);
959                         j->x.n_soa++;
960                 }
961                 size += sizeof(journal_rawrrhdr_t);
962                 size += t->name.length; /* XXX should have access macro? */
963                 size += 10;
964                 size += t->rdata.length;
965         }
966
967         mem = isc_mem_get(j->mctx, size);
968         if (mem == NULL)
969                 return (ISC_R_NOMEMORY);
970
971         isc_buffer_init(&buffer, mem, size);
972
973         /*
974          * Pass 2.  Write RRs to buffer.
975          */
976         for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
977              t = ISC_LIST_NEXT(t, link))
978         {
979                 /*
980                  * Write the RR header.
981                  */
982                 isc_buffer_putuint32(&buffer, t->name.length + 10 +
983                                      t->rdata.length);
984                 /*
985                  * Write the owner name, RR header, and RR data.
986                  */
987                 isc_buffer_putmem(&buffer, t->name.ndata, t->name.length);
988                 isc_buffer_putuint16(&buffer, t->rdata.type);
989                 isc_buffer_putuint16(&buffer, t->rdata.rdclass);
990                 isc_buffer_putuint32(&buffer, t->ttl);
991                 INSIST(t->rdata.length < 65536);
992                 isc_buffer_putuint16(&buffer, (isc_uint16_t)t->rdata.length);
993                 INSIST(isc_buffer_availablelength(&buffer) >= t->rdata.length);
994                 isc_buffer_putmem(&buffer, t->rdata.data, t->rdata.length);
995         }
996
997         isc_buffer_usedregion(&buffer, &used);
998         INSIST(used.length == size);
999
1000         j->x.pos[1].offset += used.length;
1001
1002         /*
1003          * Write the buffer contents to the journal file.
1004          */
1005         CHECK(journal_write(j, used.base, used.length));
1006
1007         result = ISC_R_SUCCESS;
1008
1009  failure:
1010         if (mem != NULL)
1011                 isc_mem_put(j->mctx, mem, size);
1012         return (result);
1013
1014 }
1015
1016 isc_result_t
1017 dns_journal_commit(dns_journal_t *j) {
1018         isc_result_t result;
1019         journal_rawheader_t rawheader;
1020
1021         REQUIRE(DNS_JOURNAL_VALID(j));
1022         REQUIRE(j->state == JOURNAL_STATE_TRANSACTION);
1023
1024         /*
1025          * Perform some basic consistency checks.
1026          */
1027         if (j->x.n_soa != 2) {
1028                 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1029                               "malformed transaction: %d SOAs",
1030                               j->x.n_soa);
1031                 return (ISC_R_UNEXPECTED);
1032         }
1033         if (! (DNS_SERIAL_GT(j->x.pos[1].serial, j->x.pos[0].serial) ||
1034                (bind8_compat &&
1035                 j->x.pos[1].serial == j->x.pos[0].serial)))
1036         {
1037                 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1038                               "malformed transaction: serial number "
1039                               "would decrease");
1040                 return (ISC_R_UNEXPECTED);
1041         }
1042         if (! JOURNAL_EMPTY(&j->header)) {
1043                 if (j->x.pos[0].serial != j->header.end.serial) {
1044                         isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1045                                          "malformed transaction: "
1046                                          "%s last serial %u != "
1047                                          "transaction first serial %u",
1048                                          j->filename,
1049                                          j->header.end.serial,
1050                                          j->x.pos[0].serial);
1051                         return (ISC_R_UNEXPECTED);
1052                 }
1053         }
1054
1055         /*
1056          * Some old journal entries may become non-addressable
1057          * when we increment the current serial number.  Purge them
1058          * by stepping header.begin forward to the first addressable
1059          * transaction.  Also purge them from the index.
1060          */
1061         if (! JOURNAL_EMPTY(&j->header)) {
1062                 while (! DNS_SERIAL_GT(j->x.pos[1].serial,
1063                                        j->header.begin.serial)) {
1064                         CHECK(journal_next(j, &j->header.begin));
1065                 }
1066                 index_invalidate(j, j->x.pos[1].serial);
1067         }
1068 #ifdef notyet
1069         if (DNS_SERIAL_GT(last_dumped_serial, j->x.pos[1].serial)) {
1070                 force_dump(...);
1071         }
1072 #endif
1073
1074         /*
1075          * Commit the transaction data to stable storage.
1076          */
1077         CHECK(journal_fsync(j));
1078
1079         /*
1080          * Update the transaction header.
1081          */
1082         CHECK(journal_seek(j, j->x.pos[0].offset));
1083         CHECK(journal_write_xhdr(j, (j->x.pos[1].offset - j->x.pos[0].offset) -
1084                                  sizeof(journal_rawxhdr_t),
1085                                  j->x.pos[0].serial, j->x.pos[1].serial));
1086
1087         /*
1088          * Update the journal header.
1089          */
1090         if (JOURNAL_EMPTY(&j->header)) {
1091                 j->header.begin = j->x.pos[0];
1092         }
1093         j->header.end = j->x.pos[1];
1094         journal_header_encode(&j->header, &rawheader);
1095         CHECK(journal_seek(j, 0));
1096         CHECK(journal_write(j, &rawheader, sizeof(rawheader)));
1097
1098         /*
1099          * Update the index.
1100          */
1101         index_add(j, &j->x.pos[0]);
1102
1103         /*
1104          * Convert the index into on-disk format and write
1105          * it to disk.
1106          */
1107         if (j->header.index_size != 0) {
1108                 unsigned int i;
1109                 unsigned char *p;
1110                 unsigned int rawbytes;
1111
1112                 rawbytes = j->header.index_size * sizeof(journal_rawpos_t);
1113
1114                 p = j->rawindex;
1115                 for (i = 0; i < j->header.index_size; i++) {
1116                         encode_uint32(j->index[i].serial, p);
1117                         p += 4;
1118                         encode_uint32(j->index[i].offset, p);
1119                         p += 4;
1120                 }
1121                 INSIST(p == j->rawindex + rawbytes);
1122
1123                 CHECK(journal_write(j, j->rawindex, rawbytes));
1124         }
1125
1126         /*
1127          * Commit the header to stable storage.
1128          */
1129         CHECK(journal_fsync(j));
1130
1131         /*
1132          * We no longer have a transaction open.
1133          */
1134         j->state = JOURNAL_STATE_WRITE;
1135
1136         result = ISC_R_SUCCESS;
1137
1138  failure:
1139         return (result);
1140 }
1141
1142 isc_result_t
1143 dns_journal_write_transaction(dns_journal_t *j, dns_diff_t *diff) {
1144         isc_result_t result;
1145         CHECK(dns_diff_sort(diff, ixfr_order));
1146         CHECK(dns_journal_begin_transaction(j));
1147         CHECK(dns_journal_writediff(j, diff));
1148         CHECK(dns_journal_commit(j));
1149         result = ISC_R_SUCCESS;
1150  failure:
1151         return (result);
1152 }
1153
1154 void
1155 dns_journal_destroy(dns_journal_t **journalp) {
1156         dns_journal_t *j = *journalp;
1157         REQUIRE(DNS_JOURNAL_VALID(j));
1158
1159         j->it.result = ISC_R_FAILURE;
1160         dns_name_invalidate(&j->it.name);
1161         dns_decompress_invalidate(&j->it.dctx);
1162         if (j->rawindex != NULL)
1163                 isc_mem_put(j->mctx, j->rawindex, j->header.index_size *
1164                             sizeof(journal_rawpos_t));
1165         if (j->index != NULL)
1166                 isc_mem_put(j->mctx, j->index, j->header.index_size *
1167                             sizeof(journal_pos_t));
1168         if (j->it.target.base != NULL)
1169                 isc_mem_put(j->mctx, j->it.target.base, j->it.target.length);
1170         if (j->it.source.base != NULL)
1171                 isc_mem_put(j->mctx, j->it.source.base, j->it.source.length);
1172
1173         if (j->fp != NULL)
1174                 (void)isc_stdio_close(j->fp);
1175         j->magic = 0;
1176         isc_mem_put(j->mctx, j, sizeof(*j));
1177         *journalp = NULL;
1178 }
1179
1180 /*
1181  * Roll the open journal 'j' into the database 'db'.
1182  * A new database version will be created.
1183  */
1184
1185 /* XXX Share code with incoming IXFR? */
1186
1187 static isc_result_t
1188 roll_forward(dns_journal_t *j, dns_db_t *db) {
1189         isc_buffer_t source;            /* Transaction data from disk */
1190         isc_buffer_t target;            /* Ditto after _fromwire check */
1191         isc_uint32_t db_serial;         /* Database SOA serial */
1192         isc_uint32_t end_serial;        /* Last journal SOA serial */
1193         isc_result_t result;
1194         dns_dbversion_t *ver = NULL;
1195         journal_pos_t pos;
1196         dns_diff_t diff;
1197         unsigned int n_soa = 0;
1198         unsigned int n_put = 0;
1199
1200         REQUIRE(DNS_JOURNAL_VALID(j));
1201         REQUIRE(DNS_DB_VALID(db));
1202
1203         dns_diff_init(j->mctx, &diff);
1204
1205         /*
1206          * Set up empty initial buffers for uncheched and checked
1207          * wire format transaction data.  They will be reallocated
1208          * later.
1209          */
1210         isc_buffer_init(&source, NULL, 0);
1211         isc_buffer_init(&target, NULL, 0);
1212
1213         /*
1214          * Create the new database version.
1215          */
1216         CHECK(dns_db_newversion(db, &ver));
1217
1218         /*
1219          * Get the current database SOA serial number.
1220          */
1221         CHECK(dns_db_getsoaserial(db, ver, &db_serial));
1222
1223         /*
1224          * Locate a journal entry for the current database serial.
1225          */
1226         CHECK(journal_find(j, db_serial, &pos));
1227         /*
1228          * XXX do more drastic things, like marking zone stale,
1229          * if this fails?
1230          */
1231         /*
1232          * XXXRTH  The zone code should probably mark the zone as bad and
1233          *         scream loudly into the log if this is a dynamic update
1234          *         log reply that failed.
1235          */
1236
1237         end_serial = dns_journal_last_serial(j);
1238         if (db_serial == end_serial)
1239                 CHECK(DNS_R_UPTODATE);
1240
1241         CHECK(dns_journal_iter_init(j, db_serial, end_serial));
1242
1243         for (result = dns_journal_first_rr(j);
1244              result == ISC_R_SUCCESS;
1245              result = dns_journal_next_rr(j))
1246         {
1247                 dns_name_t *name;
1248                 isc_uint32_t ttl;
1249                 dns_rdata_t *rdata;
1250                 dns_difftuple_t *tuple = NULL;
1251
1252                 name = NULL;
1253                 rdata = NULL;
1254                 dns_journal_current_rr(j, &name, &ttl, &rdata);
1255
1256                 if (rdata->type == dns_rdatatype_soa)
1257                         n_soa++;
1258
1259                 if (n_soa == 3)
1260                         n_soa = 1;
1261                 if (n_soa == 0) {
1262                         isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1263                                          "%s: journal file corrupt: missing "
1264                                          "initial SOA", j->filename);
1265                         FAIL(ISC_R_UNEXPECTED);
1266                 }
1267                 CHECK(dns_difftuple_create(diff.mctx, n_soa == 1 ?
1268                                            DNS_DIFFOP_DEL : DNS_DIFFOP_ADD,
1269                                            name, ttl, rdata, &tuple));
1270                 dns_diff_append(&diff, &tuple);
1271
1272                 if (++n_put > 100)  {
1273                         isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
1274                                       "applying diff to database");
1275                         dns_diff_print(&diff, NULL);
1276                         CHECK(dns_diff_apply(&diff, db, ver));
1277                         dns_diff_clear(&diff);
1278                         n_put = 0;
1279                 }
1280         }
1281         if (result == ISC_R_NOMORE)
1282                 result = ISC_R_SUCCESS;
1283         CHECK(result);
1284
1285         if (n_put != 0) {
1286                 isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
1287                               "applying final diff to database");
1288                 dns_diff_print(&diff, NULL);
1289                 CHECK(dns_diff_apply(&diff, db, ver));
1290                 dns_diff_clear(&diff);
1291         }
1292
1293  failure:
1294         if (ver != NULL)
1295                 dns_db_closeversion(db, &ver, result == ISC_R_SUCCESS ?
1296                                     ISC_TRUE : ISC_FALSE);
1297
1298         if (source.base != NULL)
1299                 isc_mem_put(j->mctx, source.base, source.length);
1300         if (target.base != NULL)
1301                 isc_mem_put(j->mctx, target.base, target.length);
1302
1303         dns_diff_clear(&diff);
1304
1305         return (result);
1306 }
1307
1308 isc_result_t
1309 dns_journal_rollforward(isc_mem_t *mctx, dns_db_t *db, const char *filename) {
1310         dns_journal_t *j;
1311         isc_result_t result;
1312
1313         REQUIRE(DNS_DB_VALID(db));
1314         REQUIRE(filename != NULL);
1315
1316         j = NULL;
1317         result = dns_journal_open(mctx, filename, ISC_FALSE, &j);
1318         if (result == ISC_R_NOTFOUND) {
1319                 isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
1320                               "no journal file, but that's OK");
1321                 return (DNS_R_NOJOURNAL);
1322         }
1323         if (result != ISC_R_SUCCESS)
1324                 return (result);
1325         if (JOURNAL_EMPTY(&j->header))
1326                 result = DNS_R_UPTODATE;
1327         else
1328                 result = roll_forward(j, db);
1329
1330         dns_journal_destroy(&j);
1331
1332         return (result);
1333 }
1334
1335 isc_result_t
1336 dns_journal_print(isc_mem_t *mctx, const char *filename, FILE *file) {
1337         dns_journal_t *j;
1338         isc_buffer_t source;            /* Transaction data from disk */
1339         isc_buffer_t target;            /* Ditto after _fromwire check */
1340         isc_uint32_t start_serial;              /* Database SOA serial */
1341         isc_uint32_t end_serial;        /* Last journal SOA serial */
1342         isc_result_t result;
1343         dns_diff_t diff;
1344         unsigned int n_soa = 0;
1345         unsigned int n_put = 0;
1346
1347         REQUIRE(filename != NULL);
1348
1349         j = NULL;
1350         result = dns_journal_open(mctx, filename, ISC_FALSE, &j);
1351         if (result == ISC_R_NOTFOUND) {
1352                 isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no journal file");
1353                 return (DNS_R_NOJOURNAL);
1354         }
1355
1356         if (result != ISC_R_SUCCESS) {
1357                 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1358                               "journal open failure");
1359                 return (result);
1360         }
1361
1362         dns_diff_init(j->mctx, &diff);
1363
1364         /*
1365          * Set up empty initial buffers for uncheched and checked
1366          * wire format transaction data.  They will be reallocated
1367          * later.
1368          */
1369         isc_buffer_init(&source, NULL, 0);
1370         isc_buffer_init(&target, NULL, 0);
1371
1372         start_serial = dns_journal_first_serial(j);
1373         end_serial = dns_journal_last_serial(j);
1374
1375         CHECK(dns_journal_iter_init(j, start_serial, end_serial));
1376
1377         for (result = dns_journal_first_rr(j);
1378              result == ISC_R_SUCCESS;
1379              result = dns_journal_next_rr(j))
1380         {
1381                 dns_name_t *name;
1382                 isc_uint32_t ttl;
1383                 dns_rdata_t *rdata;
1384                 dns_difftuple_t *tuple = NULL;
1385
1386                 name = NULL;
1387                 rdata = NULL;
1388                 dns_journal_current_rr(j, &name, &ttl, &rdata);
1389
1390                 if (rdata->type == dns_rdatatype_soa)
1391                         n_soa++;
1392
1393                 if (n_soa == 3)
1394                         n_soa = 1;
1395                 if (n_soa == 0) {
1396                 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1397                                          "%s: journal file corrupt: missing "
1398                                          "initial SOA", j->filename);
1399                         FAIL(ISC_R_UNEXPECTED);
1400                 }
1401                 CHECK(dns_difftuple_create(diff.mctx, n_soa == 1 ?
1402                                            DNS_DIFFOP_DEL : DNS_DIFFOP_ADD,
1403                                            name, ttl, rdata, &tuple));
1404                 dns_diff_append(&diff, &tuple);
1405
1406                 if (++n_put > 100)  {
1407                         result = dns_diff_print(&diff, file);
1408                         dns_diff_clear(&diff);
1409                         n_put = 0;
1410                         if (result != ISC_R_SUCCESS)
1411                                 break;
1412                 }
1413         }
1414         if (result == ISC_R_NOMORE)
1415                 result = ISC_R_SUCCESS;
1416         CHECK(result);
1417
1418         if (n_put != 0) {
1419                 result = dns_diff_print(&diff, file);
1420                 dns_diff_clear(&diff);
1421         }
1422         goto cleanup;
1423
1424  failure:
1425         isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1426                       "%s: cannot print: journal file corrupt", j->filename);
1427
1428  cleanup:
1429         if (source.base != NULL)
1430                 isc_mem_put(j->mctx, source.base, source.length);
1431         if (target.base != NULL)
1432                 isc_mem_put(j->mctx, target.base, target.length);
1433
1434         dns_diff_clear(&diff);
1435         dns_journal_destroy(&j);
1436
1437         return (result);
1438 }
1439
1440 /**************************************************************************/
1441 /*
1442  * Miscellaneous accessors.
1443  */
1444 isc_uint32_t dns_journal_first_serial(dns_journal_t *j) {
1445         return (j->header.begin.serial);
1446 }
1447
1448 isc_uint32_t dns_journal_last_serial(dns_journal_t *j) {
1449         return (j->header.end.serial);
1450 }
1451
1452 /**************************************************************************/
1453 /*
1454  * Iteration support.
1455  *
1456  * When serving an outgoing IXFR, we transmit a part the journal starting
1457  * at the serial number in the IXFR request and ending at the serial
1458  * number that is current when the IXFR request arrives.  The ending
1459  * serial number is not necessarily at the end of the journal:
1460  * the journal may grow while the IXFR is in progress, but we stop
1461  * when we reach the serial number that was current when the IXFR started.
1462  */
1463
1464 static isc_result_t read_one_rr(dns_journal_t *j);
1465
1466 /*
1467  * Make sure the buffer 'b' is has at least 'size' bytes
1468  * allocated, and clear it.
1469  *
1470  * Requires:
1471  *      Either b->base is NULL, or it points to b->length bytes of memory
1472  *      previously allocated by isc_mem_get().
1473  */
1474
1475 static isc_result_t
1476 size_buffer(isc_mem_t *mctx, isc_buffer_t *b, unsigned size) {
1477         if (b->length < size) {
1478                 void *mem = isc_mem_get(mctx, size);
1479                 if (mem == NULL)
1480                         return (ISC_R_NOMEMORY);
1481                 if (b->base != NULL)
1482                         isc_mem_put(mctx, b->base, b->length);
1483                 b->base = mem;
1484                 b->length = size;
1485         }
1486         isc_buffer_clear(b);
1487         return (ISC_R_SUCCESS);
1488 }
1489
1490 isc_result_t
1491 dns_journal_iter_init(dns_journal_t *j,
1492                       isc_uint32_t begin_serial, isc_uint32_t end_serial)
1493 {
1494         isc_result_t result;
1495
1496         CHECK(journal_find(j, begin_serial, &j->it.bpos));
1497         INSIST(j->it.bpos.serial == begin_serial);
1498
1499         CHECK(journal_find(j, end_serial, &j->it.epos));
1500         INSIST(j->it.epos.serial == end_serial);
1501
1502         result = ISC_R_SUCCESS;
1503  failure:
1504         j->it.result = result;
1505         return (j->it.result);
1506 }
1507
1508
1509 isc_result_t
1510 dns_journal_first_rr(dns_journal_t *j) {
1511         isc_result_t result;
1512
1513         /*
1514          * Seek to the beginning of the first transaction we are
1515          * interested in.
1516          */
1517         CHECK(journal_seek(j, j->it.bpos.offset));
1518         j->it.current_serial = j->it.bpos.serial;
1519
1520         j->it.xsize = 0;  /* We have no transaction data yet... */
1521         j->it.xpos = 0;   /* ...and haven't used any of it. */
1522
1523         return (read_one_rr(j));
1524
1525  failure:
1526         return (result);
1527 }
1528
1529 static isc_result_t
1530 read_one_rr(dns_journal_t *j) {
1531         isc_result_t result;
1532
1533         dns_rdatatype_t rdtype;
1534         dns_rdataclass_t rdclass;
1535         unsigned int rdlen;
1536         isc_uint32_t ttl;
1537         journal_xhdr_t xhdr;
1538         journal_rrhdr_t rrhdr;
1539
1540         INSIST(j->offset <= j->it.epos.offset);
1541         if (j->offset == j->it.epos.offset)
1542                 return (ISC_R_NOMORE);
1543         if (j->it.xpos == j->it.xsize) {
1544                 /*
1545                  * We are at a transaction boundary.
1546                  * Read another transaction header.
1547                  */
1548                 CHECK(journal_read_xhdr(j, &xhdr));
1549                 if (xhdr.size == 0) {
1550                         isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1551                                       "journal corrupt: empty transaction");
1552                         FAIL(ISC_R_UNEXPECTED);
1553                 }
1554                 if (xhdr.serial0 != j->it.current_serial) {
1555                         isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1556                                          "%s: journal file corrupt: "
1557                                          "expected serial %u, got %u",
1558                                          j->filename,
1559                                          j->it.current_serial, xhdr.serial0);
1560                         FAIL(ISC_R_UNEXPECTED);
1561                 }
1562                 j->it.xsize = xhdr.size;
1563                 j->it.xpos = 0;
1564         }
1565         /*
1566          * Read an RR.
1567          */
1568         result = journal_read_rrhdr(j, &rrhdr);
1569         /*
1570          * Perform a sanity check on the journal RR size.
1571          * The smallest possible RR has a 1-byte owner name
1572          * and a 10-byte header.  The largest possible
1573          * RR has 65535 bytes of data, a header, and a maximum-
1574          * size owner name, well below 70 k total.
1575          */
1576         if (rrhdr.size < 1+10 || rrhdr.size > 70000) {
1577                 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1578                                  "%s: journal corrupt: impossible RR size "
1579                                  "(%d bytes)", j->filename, rrhdr.size);
1580                 FAIL(ISC_R_UNEXPECTED);
1581         }
1582
1583         CHECK(size_buffer(j->mctx, &j->it.source, rrhdr.size));
1584         CHECK(journal_read(j, j->it.source.base, rrhdr.size));
1585         isc_buffer_add(&j->it.source, rrhdr.size);
1586
1587         /*
1588          * The target buffer is made the same size
1589          * as the source buffer, with the assumption that when
1590          * no compression in present, the output of dns_*_fromwire()
1591          * is no larger than the input.
1592          */
1593         CHECK(size_buffer(j->mctx, &j->it.target, rrhdr.size));
1594
1595         /*
1596          * Parse the owner name.  We don't know where it
1597          * ends yet, so we make the entire "remaining"
1598          * part of the buffer "active".
1599          */
1600         isc_buffer_setactive(&j->it.source,
1601                              j->it.source.used - j->it.source.current);
1602         CHECK(dns_name_fromwire(&j->it.name, &j->it.source,
1603                                 &j->it.dctx, ISC_FALSE, &j->it.target));
1604
1605         /*
1606          * Check that the RR header is there, and parse it.
1607          */
1608         if (isc_buffer_remaininglength(&j->it.source) < 10)
1609                 FAIL(DNS_R_FORMERR);
1610
1611         rdtype = isc_buffer_getuint16(&j->it.source);
1612         rdclass = isc_buffer_getuint16(&j->it.source);
1613         ttl = isc_buffer_getuint32(&j->it.source);
1614         rdlen = isc_buffer_getuint16(&j->it.source);
1615
1616         /*
1617          * Parse the rdata.
1618          */
1619         isc_buffer_setactive(&j->it.source, rdlen);
1620         dns_rdata_reset(&j->it.rdata);
1621         CHECK(dns_rdata_fromwire(&j->it.rdata, rdclass,
1622                                  rdtype, &j->it.source, &j->it.dctx,
1623                                  ISC_FALSE, &j->it.target));
1624         j->it.ttl = ttl;
1625
1626         j->it.xpos += sizeof(journal_rawrrhdr_t) + rrhdr.size;
1627         if (rdtype == dns_rdatatype_soa) {
1628                 /* XXX could do additional consistency checks here */
1629                 j->it.current_serial = dns_soa_getserial(&j->it.rdata);
1630         }
1631
1632         result = ISC_R_SUCCESS;
1633
1634  failure:
1635         j->it.result = result;
1636         return (result);
1637 }
1638
1639 isc_result_t
1640 dns_journal_next_rr(dns_journal_t *j) {
1641         j->it.result = read_one_rr(j);
1642         return (j->it.result);
1643 }
1644
1645 void
1646 dns_journal_current_rr(dns_journal_t *j, dns_name_t **name, isc_uint32_t *ttl,
1647                    dns_rdata_t **rdata)
1648 {
1649         REQUIRE(j->it.result == ISC_R_SUCCESS);
1650         *name = &j->it.name;
1651         *ttl = j->it.ttl;
1652         *rdata = &j->it.rdata;
1653 }
1654
1655 /**************************************************************************/
1656 /*
1657  * Generating diffs from databases
1658  */
1659
1660 /*
1661  * Construct a diff containing all the RRs at the current name of the
1662  * database iterator 'dbit' in database 'db', version 'ver'.
1663  * Set '*name' to the current name, and append the diff to 'diff'.
1664  * All new tuples will have the operation 'op'.
1665  *
1666  * Requires: 'name' must have buffer large enough to hold the name.
1667  * Typically, a dns_fixedname_t would be used.
1668  */
1669 static isc_result_t
1670 get_name_diff(dns_db_t *db, dns_dbversion_t *ver, isc_stdtime_t now,
1671               dns_dbiterator_t *dbit, dns_name_t *name, dns_diffop_t op,
1672               dns_diff_t *diff)
1673 {
1674         isc_result_t result;
1675         dns_dbnode_t *node = NULL;
1676         dns_rdatasetiter_t *rdsiter = NULL;
1677         dns_difftuple_t *tuple = NULL;
1678
1679         result = dns_dbiterator_current(dbit, &node, name);
1680         if (result != ISC_R_SUCCESS)
1681                 return (result);
1682
1683         result = dns_db_allrdatasets(db, node, ver, now, &rdsiter);
1684         if (result != ISC_R_SUCCESS)
1685                 goto cleanup_node;
1686
1687         for (result = dns_rdatasetiter_first(rdsiter);
1688              result == ISC_R_SUCCESS;
1689              result = dns_rdatasetiter_next(rdsiter))
1690         {
1691                 dns_rdataset_t rdataset;
1692
1693                 dns_rdataset_init(&rdataset);
1694                 dns_rdatasetiter_current(rdsiter, &rdataset);
1695
1696                 for (result = dns_rdataset_first(&rdataset);
1697                      result == ISC_R_SUCCESS;
1698                      result = dns_rdataset_next(&rdataset))
1699                 {
1700                         dns_rdata_t rdata = DNS_RDATA_INIT;
1701                         dns_rdataset_current(&rdataset, &rdata);
1702                         result = dns_difftuple_create(diff->mctx, op, name,
1703                                                       rdataset.ttl, &rdata,
1704                                                       &tuple);
1705                         if (result != ISC_R_SUCCESS) {
1706                                 dns_rdataset_disassociate(&rdataset);
1707                                 goto cleanup_iterator;
1708                         }
1709                         dns_diff_append(diff, &tuple);
1710                 }
1711                 dns_rdataset_disassociate(&rdataset);
1712                 if (result != ISC_R_NOMORE)
1713                         goto cleanup_iterator;
1714         }
1715         if (result != ISC_R_NOMORE)
1716                 goto cleanup_iterator;
1717
1718         result = ISC_R_SUCCESS;
1719
1720  cleanup_iterator:
1721         dns_rdatasetiter_destroy(&rdsiter);
1722
1723  cleanup_node:
1724         dns_db_detachnode(db, &node);
1725
1726         return (result);
1727 }
1728
1729 /*
1730  * Comparison function for use by dns_diff_subtract when sorting
1731  * the diffs to be subtracted.  The sort keys are the rdata type
1732  * and the rdata itself.  The owner name is ignored, because
1733  * it is known to be the same for all tuples.
1734  */
1735 static int
1736 rdata_order(const void *av, const void *bv) {
1737         dns_difftuple_t const * const *ap = av;
1738         dns_difftuple_t const * const *bp = bv;
1739         dns_difftuple_t const *a = *ap;
1740         dns_difftuple_t const *b = *bp;
1741         int r;
1742         r = (b->rdata.type - a->rdata.type);
1743         if (r != 0)
1744                 return (r);
1745         r = dns_rdata_compare(&a->rdata, &b->rdata);
1746         return (r);
1747 }
1748
1749 static isc_result_t
1750 dns_diff_subtract(dns_diff_t diff[2], dns_diff_t *r) {
1751         isc_result_t result;
1752         dns_difftuple_t *p[2];
1753         int i, t;
1754         CHECK(dns_diff_sort(&diff[0], rdata_order));
1755         CHECK(dns_diff_sort(&diff[1], rdata_order));
1756
1757         for (;;) {
1758                 p[0] = ISC_LIST_HEAD(diff[0].tuples);
1759                 p[1] = ISC_LIST_HEAD(diff[1].tuples);
1760                 if (p[0] == NULL && p[1] == NULL)
1761                         break;
1762
1763                 for (i = 0; i < 2; i++)
1764                         if (p[!i] == NULL) {
1765                                 ISC_LIST_UNLINK(diff[i].tuples, p[i], link);
1766                                 ISC_LIST_APPEND(r->tuples, p[i], link);
1767                                 goto next;
1768                         }
1769                 t = rdata_order(&p[0], &p[1]);
1770                 if (t < 0) {
1771                         ISC_LIST_UNLINK(diff[0].tuples, p[0], link);
1772                         ISC_LIST_APPEND(r->tuples, p[0], link);
1773                         goto next;
1774                 }
1775                 if (t > 0) {
1776                         ISC_LIST_UNLINK(diff[1].tuples, p[1], link);
1777                         ISC_LIST_APPEND(r->tuples, p[1], link);
1778                         goto next;
1779                 }
1780                 INSIST(t == 0);
1781                 /*
1782                  * Identical RRs in both databases; skip them both.
1783                  */
1784                 for (i = 0; i < 2; i++) {
1785                         ISC_LIST_UNLINK(diff[i].tuples, p[i], link);
1786                         dns_difftuple_free(&p[i]);
1787                 }
1788         next: ;
1789         }
1790         result = ISC_R_SUCCESS;
1791  failure:
1792         return (result);
1793 }
1794
1795 /*
1796  * Compare the databases 'dba' and 'dbb' and generate a journal
1797  * entry containing the changes to make 'dba' from 'dbb' (note
1798  * the order).  This journal entry will consist of a single,
1799  * possibly very large transaction.
1800  */
1801
1802 isc_result_t
1803 dns_db_diff(isc_mem_t *mctx,
1804             dns_db_t *dba, dns_dbversion_t *dbvera,
1805             dns_db_t *dbb, dns_dbversion_t *dbverb,
1806             const char *journal_filename)
1807 {
1808         dns_db_t *db[2];
1809         dns_dbversion_t *ver[2];
1810         dns_dbiterator_t *dbit[2] = { NULL, NULL };
1811         isc_boolean_t have[2] = { ISC_FALSE, ISC_FALSE };
1812         dns_fixedname_t fixname[2];
1813         isc_result_t result, itresult[2];
1814         dns_diff_t diff[2], resultdiff;
1815         int i, t;
1816         dns_journal_t *journal = NULL;
1817
1818         db[0] = dba, db[1] = dbb;
1819         ver[0] = dbvera, ver[1] = dbverb;
1820
1821         dns_diff_init(mctx, &diff[0]);
1822         dns_diff_init(mctx, &diff[1]);
1823         dns_diff_init(mctx, &resultdiff);
1824
1825         dns_fixedname_init(&fixname[0]);
1826         dns_fixedname_init(&fixname[1]);
1827
1828         CHECK(dns_journal_open(mctx, journal_filename, ISC_TRUE, &journal));
1829
1830         CHECK(dns_db_createiterator(db[0], ISC_FALSE, &dbit[0]));
1831         CHECK(dns_db_createiterator(db[1], ISC_FALSE, &dbit[1]));
1832
1833         itresult[0] = dns_dbiterator_first(dbit[0]);
1834         itresult[1] = dns_dbiterator_first(dbit[1]);
1835
1836         for (;;) {
1837                 for (i = 0; i < 2; i++) {
1838                         if (! have[i] && itresult[i] == ISC_R_SUCCESS) {
1839                                 CHECK(get_name_diff(db[i], ver[i], 0, dbit[i],
1840                                             dns_fixedname_name(&fixname[i]),
1841                                             i == 0 ?
1842                                             DNS_DIFFOP_ADD :
1843                                             DNS_DIFFOP_DEL,
1844                                             &diff[i]));
1845                                 itresult[i] = dns_dbiterator_next(dbit[i]);
1846                                 have[i] = ISC_TRUE;
1847                         }
1848                 }
1849
1850                 if (! have[0] && ! have[1]) {
1851                         INSIST(ISC_LIST_EMPTY(diff[0].tuples));
1852                         INSIST(ISC_LIST_EMPTY(diff[1].tuples));
1853                         break;
1854                 }
1855
1856                 for (i = 0; i < 2; i++) {
1857                         if (! have[!i]) {
1858                                 ISC_LIST_APPENDLIST(resultdiff.tuples,
1859                                                     diff[i].tuples, link);
1860                                 INSIST(ISC_LIST_EMPTY(diff[i].tuples));
1861                                 have[i] = ISC_FALSE;
1862                                 goto next;
1863                         }
1864                 }
1865
1866                 t = dns_name_compare(dns_fixedname_name(&fixname[0]),
1867                                      dns_fixedname_name(&fixname[1]));
1868                 if (t < 0) {
1869                         ISC_LIST_APPENDLIST(resultdiff.tuples,
1870                                             diff[0].tuples, link);
1871                         INSIST(ISC_LIST_EMPTY(diff[0].tuples));
1872                         have[0] = ISC_FALSE;
1873                         continue;
1874                 }
1875                 if (t > 0) {
1876                         ISC_LIST_APPENDLIST(resultdiff.tuples,
1877                                             diff[1].tuples, link);
1878                         INSIST(ISC_LIST_EMPTY(diff[1].tuples));
1879                         have[1] = ISC_FALSE;
1880                         continue;
1881                 }
1882                 INSIST(t == 0);
1883                 CHECK(dns_diff_subtract(diff, &resultdiff));
1884                 INSIST(ISC_LIST_EMPTY(diff[0].tuples));
1885                 INSIST(ISC_LIST_EMPTY(diff[1].tuples));
1886                 have[0] = have[1] = ISC_FALSE;
1887         next: ;
1888         }
1889         if (itresult[0] != ISC_R_NOMORE)
1890                 FAIL(itresult[0]);
1891         if (itresult[1] != ISC_R_NOMORE)
1892                 FAIL(itresult[1]);
1893
1894         if (ISC_LIST_EMPTY(resultdiff.tuples)) {
1895                 isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no changes");
1896         } else {
1897                 CHECK(dns_journal_write_transaction(journal, &resultdiff));
1898         }
1899         INSIST(ISC_LIST_EMPTY(diff[0].tuples));
1900         INSIST(ISC_LIST_EMPTY(diff[1].tuples));
1901         dns_diff_clear(&resultdiff);
1902
1903  failure:
1904         dns_dbiterator_destroy(&dbit[0]);
1905         dns_dbiterator_destroy(&dbit[1]);
1906         dns_journal_destroy(&journal);
1907         return (result);
1908 }