Ticket #111: zone_update_patched

File zone_update_patched, 4.1 KB (added by Ernest0x, 6 years ago)
Line 
1#!/bin/bash
2# zone_update bash script Version 0.8-testing
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"
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 "$ZONES_ROOT$CUR_ZONE has errors (wrong syntax or  missing '; serial' comment)."
47   exit
48fi
49 
50# Where we will temporarily save the php-generated zone file.
51PHP_ZONE="/tmp/php-zone"
52
53# Where we will temporarily save the stripped (without the "serial" line) current zone file.
54CUR_ZONE_S="/tmp/cur-zone-s"
55
56# Where we will temporarily save the stripped (without the "serial" line) php-generated zone file.
57PHP_ZONE_S="/tmp/php-zone-s"
58
59# Make PHP_ZONE
60php $PHP_SCRIPT $CUR_ZONE > $PHP_ZONE
61
62# Extract the serial number of the current zone file.
63# Remember to change '2' to '3' on the next millenium change. :P
64CUR_SERIAL=`grep "; serial" $ZONES_ROOT$CUR_ZONE | grep -o "2........."`
65
66# Day Of CUR_SERIAL
67DCS=`echo $CUR_SERIAL | tail -c +7 | head -c 2`
68
69# Version of CUR_SERIAL
70VCS=`echo $CUR_SERIAL | tail -c +9`
71
72# If it is less than or equal to 9 delete leading zero (so that 08 or less is not interpreted as octal)
73if [ $VCS -le 9 ]; then
74  if [ $VCS -ne 0 ]; then
75     VCS=`echo $VCS|tr -d 0`
76  else
77     VCS=0
78  fi
79fi
80
81# Remove "serial" lines.
82grep -v "; serial" $ZONES_ROOT$CUR_ZONE > $CUR_ZONE_S
83grep -v "; serial" $PHP_ZONE > $PHP_ZONE_S
84
85# If stripped versions of current zone file and php-generated zone file are identical, remove temp files and exit.
86# Else, replace current zone file with the php-generated zone file and include the proper serial line.
87if diff $CUR_ZONE_S $PHP_ZONE_S > /dev/null ; then
88  rm -f $PHP_ZONE $CUR_ZONE_S $PHP_ZONE_S
89  exit
90else
91  # Verion of Serial to Append.
92  VSA="00"
93 
94  # Day of the Month, Now.
95  DMN=`date +"%d"`
96 
97  # If day has not changed append previous serial version incremented by 1.
98  if [ "$DMN" -eq "$DCS" ]; then
99     let VSA=VCS+1
100     
101     # if VSA is less than or equal to 9, prepend a '0' to make it a 2 digit number.
102     if [ "$VSA" -le 9 ]; then
103        VSA="0$VSA"
104     fi
105  fi
106 
107  # The new serial.
108  NEW_SERIAL=`date +"%Y%m%d"`$VSA
109 
110  # Serial Line Number.
111  SLN=`grep -n "; serial" $PHP_ZONE | head  -c 1`
112  # Line Number Before Serial.
113  let LNBS=SLN-1
114  # Line Number After Serial.
115  let LNAS=SLN+1
116 
117  # Build the final zone file
118  head -n $LNBS $PHP_ZONE > /tmp/$CUR_ZONE
119  echo "                                $NEW_SERIAL ; serial" >> /tmp/$CUR_ZONE
120  tail -n +$LNAS $PHP_ZONE >> /tmp/$CUR_ZONE
121
122  # Check that the final zone file describes correclty its zone
123  if named-checkzone -q $CUR_ZONE /tmp/$CUR_ZONE ; then
124     mv -f /tmp/$CUR_ZONE $ZONES_ROOT
125  else
126     echo "PHP-generated file has errors (possible database failure)"
127     rm -f $PHP_ZONE $CUR_ZONE_S $PHP_ZONE_S /tmp/$CUR_ZONE
128     exit
129  fi
130 
131  # Reload zone files in dns server
132  rndc reload
133
134fi
135
136# Remove temp files.
137rm -f $PHP_ZONE $CUR_ZONE_S $PHP_ZONE_S /tmp/$CUR_ZONE