/*****************************************************************
**
** @(#) soaserial.c -- helper function for the dnssec zone key tools
**
** Copyright (c) Jan 2005, Holger Zuleger HZnet. All rights reserved.
**
** This software is open source.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer.
**
** Redistributions in binary form must reproduce the above copyright notice,
** this list of conditions and the following disclaimer in the documentation
**
** Neither the name of Holger Zuleger HZnet 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 COPYRIGHT HOLDERS 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 SUCH DAMAGE.
**
*****************************************************************/
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <ctype.h>
# include <time.h>
# include <utime.h>
# include <assert.h>
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
# include "config_zkt.h"
# include "zconf.h"
# include "log.h"
# include "debug.h"
#define extern
# include "soaserial.h"
#undef extern
/****************************************************************
**
** int inc_serial (filename, use_unixtime)
**
** This function depends on a special syntax formating the
** SOA record in the zone file!!
**
** To match the SOA record, the SOA RR must be formatted
** like this:
** @ [ttl] IN SOA <master.fq.dn.> <hostmaster.fq.dn.> (
** <SPACEes or TABs> 1234567890; serial number
** <SPACEes or TABs> 86400 ; other values
** ...
** The space from the first digit of the serial number to
** the first none white space char or to the end of the line
** must be at least 10 characters!
** So you have to left justify the serial number in a field
** of at least 10 characters like this:
** <SPACEes or TABs> 1 ; Serial
**
****************************************************************/
{
int error;
/**
since BIND 9.4, there is a dnssec-signzone option available for
serial number increment.
If the user requests "unixtime"; then use this mechanism.
**/
if ( use_unixtime )
return 0;
#endif
return -1;
/* read until the line matches the beginning of a soa record ... */
;
{
return -2;
}
return -5;
return error;
}
/*****************************************************************
** check if line is the beginning of a SOA RR record, thus
** containing the string "IN .* SOA" and ends with a '('
** returns 1 if true
*****************************************************************/
{
const char *p;
{
p--;
if ( *p == '(' ) /* last character have to be a '(' to start a multi line record */
return 1;
}
return 0;
}
/*****************************************************************
** Find string 'search' in 'str' and ignore case in comparison.
** returns the position of 'search' in 'str' or NULL if not found.
*****************************************************************/
{
const char *p;
int c;
p = str;
do {
while ( *p && tolower (*p) != c )
p++;
return p;
p++;
} while ( *p );
return NULL;
}
/*****************************************************************
** return the serial number of the given time in the form
** of YYYYmmdd00 as ulong value
*****************************************************************/
{
struct tm *t;
serialtime += t->tm_mday;
serialtime *= 100;
return serialtime;
}
/*****************************************************************
** inc_soa_serial (fp, use_unixtime)
** increment the soa serial number of the file 'fp'
** 'fp' must be opened "r+"
*****************************************************************/
{
int c;
int digits;
/* move forward until any non ws reached */
;
serial = 0L; /* read in the current serial number */
/* be aware of the trailing space in the format string !! */
return -3;
return -4;
if ( !use_unixtime )
{
serial++; /* increment anyway */
}
return 1; /* yep! */
}
/*****************************************************************
** return the error text of the inc_serial return coode
*****************************************************************/
{
switch ( err )
{
case -1: return "couldn't open zone file for modifying";
case -2: return "unexpected end of file";
case -3: return "no serial number found in zone file";
case -4: return "not enough space left for serialno";
case -5: return "error on closing zone file";
}
return "";
}
#ifdef SOA_TEST
const char *progname;
{
int err;
{
exit (1);
}
}
#endif