* Copyright (c) 1986, 1988, 1990 Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)db_dump.c 4.33 (Berkeley) 3/3/91";
#include <arpa/nameser.h>
char *dumpfile
= DUMPFILE
;
char *dumpfile
= _PATH_DUMPFILE
;
* Dump current cache in a format similar to RFC 883.
* We try to be careful and determine whether the operation succeeded
* so that the new cache file can be installed.
#define DB_ROOT_TIMBUF 3600
/* nowhere to checkpoint cache... */
if (cache_file
== NULL
) {
fprintf(ddt
,"doachkpt(to where?)\n");
fprintf(ddt
,"doachkpt()\n");
(void) sprintf(tmpcheckfile
, "%s.chk", cache_file
);
if ((fp
= fopen(tmpcheckfile
, "w")) == NULL
) {
fprintf(ddt
,"doachkpt(can't open %s for write)\n", tmpcheckfile
);
fprintf(fp
, "; Dumped at %s", ctime(&tt
.tv_sec
));
fprintf(ddt
,"doachkpt(write to checkpoint file failed)\n");
if ((n
= scan_root(hashtab
)) < MINROOTS
) {
syslog(LOG_ERR
, "%d root hints... (too low)", n
);
fprintf(fp
, "; ---- Root hint cache dump ----\n");
(void) db_dump(fcachetab
, fp
, DB_Z_CACHE
, "");
fprintf(fp
, "; ---- Cache dump ----\n");
if (db_dump(hashtab
, fp
, DB_Z_CACHE
, "") == NODBFILE
) {
fprintf(ddt
,"doachkpt(checkpoint failed)\n");
(void) fsync(fileno(fp
));
fprintf(ddt
,"doachkpt(close failed)\n");
if (rename(tmpcheckfile
, cache_file
)) {
fprintf(ddt
,"doachkpt(install %s to %s failed, %d)\n",
tmpcheckfile
,cache_file
, errno
);
* What we do is scan the root hint cache to make sure there are at least
* MINROOTS root pointers with non-0 TTL's so that the checkpoint will not
* lose the root. Failing this, all pointers are written out w/ TTL ~0
* (root pointers timed out and prime_cache() not done or failed).
register struct databuf
*dp
;
register struct namebuf
*np
;
fprintf(ddt
,"scan_root(0x%x)\n", htp
);
/* metric by which we determine whether a root NS pointer is still */
/* valid (will be written out if we do a dump). we also add some */
/* time buffer for safety... */
for (np
= htp
->h_tab
[0]; np
!= NULL
; np
= np
->n_next
) {
if (np
->n_dname
[0] == '\0') {
if (dp
->d_type
== T_NS
&&
dp
->d_ttl
> soon
.tv_sec
) {
register struct databuf
*dp
;
register struct namebuf
*np
;
struct namebuf
**npp
, **nppend
;
fprintf(ddt
,"mark_cache()\n");
nppend
= npp
+ htp
->h_size
;
for (np
= *npp
++; np
!= NULL
; np
= np
->n_next
) {
for (dp
= np
->n_data
; dp
!= NULL
; dp
= dp
->d_next
) {
if (dp
->d_ttl
< soon
.tv_sec
)
nppend
= npp
+ htp
->h_size
;
for (np
= *npp
++; np
!= NULL
; np
= np
->n_next
) {
mark_cache(np
->n_hash
, ttl
);
* Dump current data base in a format similar to RFC 883.
fprintf(ddt
,"doadump()\n");
if ((fp
= fopen(dumpfile
, "w")) == NULL
)
fprintf(fp
, "; Dumped at %s", ctime(&tt
.tv_sec
));
fprintf(fp
, "; --- Cache & Data ---\n");
(void) db_dump(hashtab
, fp
, DB_Z_ALL
, "");
fprintf(fp
, "; --- Hints ---\n");
(void) db_dump(fcachetab
, fp
, DB_Z_ALL
, "");
/* Create a disk database to back up zones
register struct zoneinfo
*zp
;
/* Only dump zone if there is a cache specified */
if (zp
->z_source
&& *(zp
->z_source
)) {
fprintf(ddt
, "zonedump(%s)\n", zp
->z_source
);
if ((fp
= fopen(zp
->z_source
, "w")) == NULL
)
if (op
= index(zp
->z_origin
, '.'))
if (nlookup(zp
->z_origin
, &htp
, &fname
, 0) != NULL
) {
db_dump(htp
, fp
, zp
-zones
, (op
== NULL
? "" : op
));
zp
->hasChanged
= 0; /* Checkpointed */
if (stat(zp
->z_source
, &st
) == 0)
zp
->z_ftime
= st
.st_mtime
;
fprintf(ddt
, "zonedump: no zone to dump\n");
db_dump(htp
, fp
, zone
, origin
)
register struct databuf
*dp
;
register struct namebuf
*np
;
struct namebuf
**npp
, **nppend
;
extern char *inet_ntoa(), *protocolname(), *servicename();
int found_data
, tab
, printed_origin
= 0;
nppend
= npp
+ htp
->h_size
;
for (np
= *npp
++; np
!= NULL
; np
= np
->n_next
) {
/* Blecch - can't tell if there is data here for the
* right zone, so can't print name yet
/* we want a snapshot in time... */
for (dp
= np
->n_data
; dp
!= NULL
; dp
= dp
->d_next
) {
/* Is the data for this zone? */
if (zone
!= DB_Z_ALL
&& dp
->d_zone
!= zone
)
if (dp
->d_zone
== DB_Z_CACHE
&&
dp
->d_ttl
<= tt
.tv_sec
&&
(dp
->d_flags
& DB_F_HINT
) == 0)
fprintf(fp
, "$ORIGIN %s.\n", origin
);
if (np
->n_dname
[0] == 0) {
fprintf(fp
, ".%s.\t", origin
); /* ??? */
fprintf(fp
, "%s\t", np
->n_dname
);
if (strlen(np
->n_dname
) < 8)
if (dp
->d_zone
== DB_Z_CACHE
) {
if (dp
->d_flags
& DB_F_HINT
&&
(long)(dp
->d_ttl
- tt
.tv_sec
) < DB_ROOT_TIMBUF
)
fprintf(fp
, "%d\t", DB_ROOT_TIMBUF
);
(int)(dp
->d_ttl
- tt
.tv_sec
));
} else if (dp
->d_ttl
!= 0 &&
dp
->d_ttl
!= zones
[dp
->d_zone
].z_minimum
)
fprintf(fp
, "%d\t", (int)dp
->d_ttl
);
fprintf(fp
, "%s\t%s\t", p_class(dp
->d_class
),
cp
= (u_char
*)dp
->d_data
;
* Print type specific data
inet_ntoa(*(struct in_addr
*)&n
));
fprintf(fp
, "\t; %d", dp
->d_nstime
);
fprintf(fp
, "%s.\n", cp
);
cp
= (u_char
*)dp
->d_data
;
fprintf(fp
, "\t; %d???", dp
->d_nstime
);
fprintf(fp
, "\"%.*s\"", (int)n
, cp
);
fprintf(fp
, " \"%.*s\"", (int)n
, cp
);
cp
+= strlen((char *)cp
) + 1;
fprintf(fp
, " %s. (\n", cp
);
cp
+= strlen((char *)cp
) + 1;
fprintf(fp
, "\t\t%lu", n
);
fprintf(fp
, " %lu )\n", n
);
fprintf(fp
," %s.\n", cp
);
end
= (u_char
*)dp
->d_data
+ dp
->d_size
;
for (j
= n
; j
> 0 && cp
< end
; j
--)
(void) fputs("\"\n", fp
);
fprintf(fp
, "\"%s\"\n", cp
);
if (dp
->d_size
== sizeof(u_long
)) {
inet_ntoa(*(struct in_addr
*)&addr
));
proto
= protocolname(*cp
);
fprintf(fp
, "%s ", proto
);
while(cp
< (u_char
*)dp
->d_data
+ dp
->d_size
) {
cp
+= strlen((char *)cp
) + 1;
fprintf(fp
, " %s.\n", cp
);
/* Dump binary data out in an ASCII-encoded
/* Allocate more than enough space:
* actually need 5/4 size + 20 or so
int TmpSize
= 2 * dp
->d_size
+ 30;
char *TmpBuf
= (char *) malloc(TmpSize
);
fprintf(ddt
, "Dump T_UNSPEC: malloc returned NULL\n");
syslog(LOG_ERR
, "Dump T_UNSPEC: malloc: %m");
if (btoa(cp
, dp
->d_size
, TmpBuf
,
TmpSize
) == CONV_OVERFLOW
) {
fprintf(ddt
, "Dump T_UNSPEC: Output buffer overflow\n");
syslog(LOG_ERR
, "Dump T_UNSPEC: Output buffer overflow\n");
fprintf(fp
, "%s\n", TmpBuf
);
nppend
= npp
+ htp
->h_size
;
for (np
= *npp
++; np
!= NULL
; np
= np
->n_next
) {
getname(np
, dname
, sizeof(dname
));
if (db_dump(np
->n_hash
, fp
, zone
, dname
) == NODBFILE
)
* Subroutines to convert between 8 bit binary bytes and printable ASCII.
* Computes the number of bytes, and three kinds of simple checksums.
* Incoming bytes are collected into 32-bit words, then printed in base 85:
* The ASCII characters used are between '!' and 'u';
* 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
* Originally by Paul Rutter (philabs!per) and Joe Orost (petsd!joe) for
* the atob/btoa programs, released with the compress program, in mod.sources.
* Modified by Mike Schwartz 8/19/86 for use in BIND.
/* Make sure global variable names are unique */
#define Ceor T_UNSPEC_Ceor
#define Csum T_UNSPEC_Csum
#define Crot T_UNSPEC_Crot
#define word T_UNSPEC_word
#define bcount T_UNSPEC_bcount
static long int Ceor
, Csum
, Crot
, word
, bcount
;
#define EN(c) ((int) ((c) + '!'))
#define DE(c) ((c) - '!')
#define AddToBuf(bufp, c) **bufp = c; (*bufp)++;
#define streq(s0, s1) strcmp(s0, s1) == 0
#define times85(x) ((((((x<<2)+x)<<2)+x)<<2)+x)
/* Decode ASCII-encoded byte c into binary representation and
* place into *bufp, advancing bufp
} else if ((c
>= '!') && (c
< ('!' + 85))) {
word
= times85(word
) + DE(c
);
putbyte((int)((word
>> 24) & 255), bufp
);
putbyte((int)((word
>> 16) & 255), bufp
);
putbyte((int)((word
>> 8) & 255), bufp
);
putbyte((int)(word
& 255), bufp
);
/* Compute checksum info and place c into *bufp, advancing bufp */
if ((Crot
& 0x80000000)) {
/* Read the ASCII-encoded data from inbuf, of length inbuflen, and convert
it into T_UNSPEC (binary data) in outbuf, not to exceed outbuflen bytes;
outbuflen must be divisible by 4. (Note: this is because outbuf is filled
in 4 bytes at a time. If the actual data doesn't end on an even 4-byte
boundary, there will be no problem...it will be padded with 0 bytes, and
numbytes will indicate the correct number of bytes. The main point is
that since the buffer is filled in 4 bytes at a time, even if there is
not a full 4 bytes of data at the end, there has to be room to 0-pad the
data, so the buffer must be of size divisible by 4). Place the number of
output bytes in numbytes, and return a failure/success status */
atob(inbuf
, inbuflen
, outbuf
, outbuflen
, numbytes
)
long int oeor
, osum
, orot
;
char *inp
, *outp
= outbuf
, *endoutp
= &outbuf
[outbuflen
];
if ( (outbuflen
% 4) != 0)
Ceor
= Csum
= Crot
= word
= bcount
= 0;
for (inp
= inbuf
, inc
= 0; inc
< inbuflen
; inp
++, inc
++) {
if (byte_atob(*inp
, &outp
) == CONV_BADFMT
)
/* Get byte count and checksum information from end of buffer */
if(sscanf(inp
, "%ld %lx %lx %lx", numbytes
, &oeor
, &osum
, &orot
) != 4)
if ((oeor
!= Ceor
) || (osum
!= Csum
) || (orot
!= Crot
))
/* Encode binary byte c into ASCII representation and place into *bufp,
if ((Crot
& 0x80000000)) {
register long int tmpword
= word
;
/* Because some don't support unsigned long */
tmpword
-= (long)(85 * 85 * 85 * 85 * 32);
tmpword
-= (long)(85 * 85 * 85 * 85 * 32);
EN((tmpword
/ (long)(85 * 85 * 85 * 85)) + tmp
));
tmpword
%= (long)(85 * 85 * 85 * 85);
AddToBuf(bufp
, EN(tmpword
/ (85 * 85 * 85)));
tmpword
%= (85 * 85 * 85);
AddToBuf(bufp
, EN(tmpword
/ (85 * 85)));
AddToBuf(bufp
, EN(tmpword
/ 85));
AddToBuf(bufp
, EN(tmpword
));
* Encode the binary data from inbuf, of length inbuflen, into a
* null-terminated ASCII representation in outbuf, not to exceed outbuflen
* bytes. Return success/failure status
btoa(inbuf
, inbuflen
, outbuf
, outbuflen
)
long int oeor
, osum
, orot
;
char *inp
, *outp
= outbuf
, *endoutp
= &outbuf
[outbuflen
-1];
Ceor
= Csum
= Crot
= word
= bcount
= 0;
for (inp
= inbuf
, inc
= 0; inc
< inbuflen
; inp
++, inc
++) {
byte_btoa((unsigned char) (*inp
), &outp
);
/* Put byte count and checksum information at end of buffer, delimited
(void) sprintf(outp
, "x %ld %lx %lx %lx", inbuflen
, Ceor
, Csum
, Crot
);
if (&outp
[strlen(outp
) - 1] >= endoutp
)