source: trunk/tools/zone_update/zone_update @ 245

Revision 245, 4.8 KB checked in by sque, 11 months ago (diff)

Merge branches/awmn 221:235
[awmn] Allow users to delete zones and nameservers without making a request to hostmaster
[awmn] Untaint sprintf
[awmn] Don't change to php script dir
[awmn] Add support to dump list of stealth nameservers
[awmn] Add active peers search fields to hostmaster's ranges page
[awmn] Redirect errors to stderr
[awmn] Don't explicitly set owner to 'N' (#146)
[awmn] Restrict ip ranges to valid b-classes (0-255)
[awmn] Escape HTML special characters
[awmn] Use appropriate entities for < and > HTML special characters
[awmn] Add missing 'alt' attribute for 'img' element
[awmn] Close 'td' and 'tr' elements properly
[awmn] Escape HTML special characters in services URLs
[awmn] Remove end of line carriage returns

  • Property svn:executable set to *
Line 
1#!/bin/bash
2# zone_update bash script Version 0.8
3# Copyleft 2005 - Ernest0x (ernest0x at yahoo dot gr)
4
5# Discription
6# -----------
7# If it is needed, this script  updates a DNS zone file from another
8# php-generated zone file.
9# It is meant to be used by the WiND project (http://wind.cube.gr).
10
11
12# Notes
13# -----
14# - Both your system's zone file and the php-generated zone file must include a
15#   line that contains a "; serial" comment string. This line gives its place to
16#   a new serial line, constructed by this script.
17
18
19PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11
20
21# Root path for zone files
22#_THIS SHOULD BE CHANGED TO THE PATH WHERE DNS ZONE FILES ARE STORED_.
23ZONES_ROOT="/etc/bind/zones/"
24
25# The php script to generate the new zone file.
26#_THIS SHOULD BE CHANGED TO YOUR PHP SCRIPT_.
27PHP_SCRIPT="/usr/local/lib/zones-poller/zones-poller.php"
28
29# The current zone file (just the filename, not the fullpath).
30# This is given as an argument when calling the script.
31CUR_ZONE=${1}
32
33# Check that an existing file was passed as an argument by the caller.
34if [ -z $CUR_ZONE ]; then
35   echo "Usage: $0 ZONE_FILENAME"
36   exit
37fi
38
39if [ ! -e $ZONES_ROOT$CUR_ZONE ]; then
40   echo "file '$ZONES_ROOT$CUR_ZONE' does not exist" 1>&2
41   exit
42fi
43
44# Check the syntax of the current zone file and make sure it includes "; serial" line
45if ! ( named-checkzone -q $CUR_ZONE $ZONES_ROOT$CUR_ZONE && grep -q "; serial" $ZONES_ROOT$CUR_ZONE ) ; then
46   echo "`date` - $ZONES_ROOT$CUR_ZONE has errors (wrong syntax or  missing '; serial' comment)." 1>&2
47   echo "#### START of named-checkzone output (if OK then '; serial' comment is missing) ####" 1>&2
48   echo "`named-checkzone $CUR_ZONE $ZONES_ROOT$CUR_ZONE`" 1>&2
49   echo "#### END of named-checkzone output ####" 1>&2
50   exit
51fi
52 
53# Where we will temporarily save the php-generated zone file.
54PHP_ZONE="/tmp/php-zone"
55
56# Where we will temporarily save the stripped (without the "serial" line) current zone file.
57CUR_ZONE_S="/tmp/cur-zone-s"
58
59# Where we will temporarily save the stripped (without the "serial" line) php-generated zone file.
60PHP_ZONE_S="/tmp/php-zone-s"
61
62# Make PHP_ZONE
63#cd `dirname $PHP_SCRIPT`
64php $PHP_SCRIPT $CUR_ZONE > $PHP_ZONE
65
66# Extract the serial number of the current zone file.
67# Remember to change '2' to '3' on the next millenium change. :P
68CUR_SERIAL=`grep "; serial" $ZONES_ROOT$CUR_ZONE | grep -o "2........."`
69
70# Check the validity of CUR_SERIAL
71CUR_SERIAL_CHARS=`echo -n $CUR_SERIAL | wc -m`
72if [ $CUR_SERIAL_CHARS -ne 10 ] || ! date -d `echo $CUR_SERIAL | cut -c 1-8` > /dev/null 2>&1 ; then
73    echo "`date` - Serial line in $ZONES_ROOT$CUR_ZONE is not valid." 1>&2
74    exit
75fi
76
77# Day Of CUR_SERIAL
78DCS=`echo $CUR_SERIAL | tail -c +7 | head -c 2`
79
80# Version of CUR_SERIAL
81VCS=`echo $CUR_SERIAL | tail -c +9`
82
83# If it is less than or equal to 9 delete leading zero (so that 08 or less is not interpreted as octal)
84if [ $VCS -le 9 ]; then
85  if [ $VCS -ne 0 ]; then
86     VCS=`echo $VCS|tr -d 0`
87  else
88     VCS=0
89  fi
90fi
91
92# Remove "serial" lines.
93grep -v "; serial" $ZONES_ROOT$CUR_ZONE > $CUR_ZONE_S
94grep -v "; serial" $PHP_ZONE > $PHP_ZONE_S
95
96# If stripped versions of current zone file and php-generated zone file are identical, remove temp files and exit.
97# Else, replace current zone file with the php-generated zone file and include the proper serial line.
98if diff $CUR_ZONE_S $PHP_ZONE_S > /dev/null ; then
99  rm -f $PHP_ZONE $CUR_ZONE_S $PHP_ZONE_S
100  exit
101else
102  # Verion of Serial to Append.
103  VSA="00"
104 
105  # Day of the Month, Now.
106  DMN=`date +"%d"`
107 
108  # If day has not changed append previous serial version incremented by 1.
109  if [ "$DMN" -eq "$DCS" ]; then
110     let VSA=VCS+1
111     
112     # if VSA is less than or equal to 9, prepend a '0' to make it a 2 digit number.
113     if [ "$VSA" -le 9 ]; then
114        VSA="0$VSA"
115     fi
116  fi
117 
118  # The new serial.
119  NEW_SERIAL=`date +"%Y%m%d"`$VSA
120 
121  # Serial Line Number.
122  SLN=`grep -n "; serial" $PHP_ZONE | head  -c 1`
123  # Line Number Before Serial.
124  let LNBS=SLN-1
125  # Line Number After Serial.
126  let LNAS=SLN+1
127 
128  # Build the final zone file
129  head -n $LNBS $PHP_ZONE > /tmp/$CUR_ZONE
130  echo "                                $NEW_SERIAL ; serial" >> /tmp/$CUR_ZONE
131  tail -n +$LNAS $PHP_ZONE >> /tmp/$CUR_ZONE
132
133  # Check that the final zone file describes correclty its zone
134  if named-checkzone -q $CUR_ZONE /tmp/$CUR_ZONE ; then
135     mv -f /tmp/$CUR_ZONE $ZONES_ROOT
136  else
137     echo "`date` - PHP-generated file has errors (possible database failure)" 1>&2
138     echo "#### START of named-checkzone output ####" 1>&2
139     echo "`named-checkzone $CUR_ZONE /tmp/$CUR_ZONE`" 1>&2
140     echo "#### END of named-checkzone output ####" 1>&2
141     rm -f $PHP_ZONE $CUR_ZONE_S $PHP_ZONE_S /tmp/$CUR_ZONE
142     exit
143  fi
144 
145  # Reload zone files in dns server
146  rndc reload
147
148fi
149
150# Remove temp files.
151rm -f $PHP_ZONE $CUR_ZONE_S $PHP_ZONE_S /tmp/$CUR_ZONE
Note: See TracBrowser for help on using the repository browser.