-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_zone_files.sh
executable file
·199 lines (157 loc) · 6.54 KB
/
create_zone_files.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/zsh
# this provides the shell function strftime() and the variable ${EPOCHSECONDS}
zmodload zsh/datetime
base="${ROOT:-/}usr/local/admin/dns"
tmpNamedConf="${base}/tmpNamedConf"
tmpZoneFiles="${base}/zonefiles"
tmpcPanelZoneFiles="${base}/cPanelZoneFiles"
varNamed=${ROOT:-/}var/named
servers=( 10.123.128.11 10.123.128.12 10.123.128.211 10.123.128.212 )
verbose=${VERBOSE:-1}
debug=${DEBUG:-1}
# temporary file for a cpanel zone list
cPanelZoneList=$(mktemp)
trap "rm -f ${cPanelZoneList}; exit" 0 1 2
test -d ${tmpZoneFiles} || sudo mkdir ${tmpZoneFiles}
test -d ${tmpcPanelZoneFiles} || sudo mkdir ${tmpcPanelZoneFiles}
# see [email protected] for an explanation of this wget(1) call
# Note: the Basic Auth string needs to be updated when the WHM root password changes
(( verbose == 1 )) && echo "; -- Creating ${cPanelZoneList} --"
wget -q \
--output-document=- \
--header='Authorization: Basic cm9vdDpxbzpMeT02dDg1MjJeSE17aipVeFwjKSgtWWs8bmJ5WUw6QDdWIU5RcSsnRSZmbU87KTRKTyglNSp4amxFNlo=' \
'http://localhost:2086/json-api/listzones?api.version=1&searchtype=owner' |
tr '{' '\012' | tr -d '}' > ${cPanelZoneList}
(( verbose == 1 )) && echo "; -- Created ${cPanelZoneList} --"
todaysDate=$(strftime "%Y%m%d" ${EPOCHSECONDS})
mtime=${EPOCHSECONDS} # this is used in the first comment line in the cPanel zone file
lastSerial=$(cat ${base}/last_serial)
serialDate=$(echo ${lastSerial} | cut -c0-8)
if [ "${serialDate}" = "${todaysDate}" ]; then
newSerial=$(( lastSerial + 1 ))
else
newSerial="${todaysDate}01"
fi
add_forward=${ADD_FORWARD:-1}
kill_commented=${KILL_COMMENTED:-1}
reload_named=${RELOAD_NAMED:-0}
(( verbose == 1 )) && echo "; -- new serial number: ${newSerial}"
# get a fresh copy of the authoritative named.conf
sudo ssh [email protected] 'cat /var/named/named.conf' >! ${tmpNamedConf}
IFS="
"
if (( add_forward == 1 )); then
# the sed command drops the entry for the root zone (".")
for line in $(grep "^zone" ${tmpNamedConf} | sed '1d')
do
### Get zone & file names
# this
#zone=$(echo "${line}" | cut -d"\"" -f2 | cut -d"\"" -f1)
# becomes
zone=${${line#*\"}%\"*}
(( verbose == 1 )) && echo "\n; ------------------- ${zone} ---------------------"
# we only need to add the zone to cPanel if it is not already registered
if grep --quiet ${zone} ${cPanelZoneList}; then
echo "Zone ${zone} is already registered with cPanel. Not calling /scripts/add_dns."
else
if (( debug )); then
echo "DEBUG: /scripts/add_dns --domain ${zone} --reseller root --ip 64.33.128.80"
else
/scripts/add_dns --domain ${zone} --reseller root --ip 64.33.128.80
fi
fi
# find the name of the file containing the zone
# the grep(1) returns a line of the form <WHITESPACE>file "primary/rev.208.157.190";
# the second line below strips everything but the filename
file=$(grep -A2 "^zone \"${zone}\"" ${tmpNamedConf} | grep 'file "primary/')
file=${${file##*/}%\"*}
# get the zone file name from the cPanel configuration
# this depends on the zone having been in cPanel at the start of this run
cPanelZoneFile=$(grep "zonefile.*:\"${zone}\"" ${cPanelZoneList} | cut -d\" -f4)
(( verbose == 1 )) && echo "; -- Our zone file: ${file}, cPanel zone file: ${cPanelZoneFile}"
test -n "${cPanelZoneFile}" || { echo "ERROR: missing cPanelZoneFile"; exit; }
### Get contents of zone file on bucky and dump into local zone file
# Skip to the next zone if the zone file does not exist on bucky
sudo ssh [email protected] "/usr/local/admin/dnsscripts/catfileif.sh ${file}" > ${tmpZoneFiles}/${zone}
if grep "doesn't exist" ${tmpZoneFiles}/${zone}; then
echo "\n Cannot find zone file ${file} for zone ${zone} on bucky, skipping!"
rm -f ${tmpZoneFiles}/${zone}
continue
fi
### Update the zone serial number
# note that the following tr(1) command deletes all whitespace
# characters (including CR, NL, TAB, etc.)
serial=$(pcregrep -o '^\s+20[01]\d{7}' ${tmpZoneFiles}/${zone} | tr -d '[[:space:]]')
sed --regexp-extended --in-place --expression="s/^(\s+)${serial}/\1${newSerial}/" ${tmpZoneFiles}/${zone}
### And while we are at it, change brutus.bright.net to ns2.airstreamcomm.net
if pcregrep --quiet -i '\s+IN\s+NS\s+brutus\.bright\.net\.\s*$' ${tmpZoneFiles}/${zone}; then
(( verbose )) && echo "; -- Replacing brutus.bright.net NS record"
sed --regexp-extended --in-place --expression='s/(\s+IN\s+NS\s+)brutus\.bright\.net\.\s*$/\1ns2.airstreamcomm.net./i' ${tmpZoneFiles}/${zone}
fi
### Insert cPanel specific lines
# create the new zone file with a name and comments that cPanel will enjoy
cat > ${tmpcPanelZoneFiles}/${cPanelZoneFile} <<EOF
; cPanel first:11.28.83-STABLE_51164 (update_time):${mtime} Cpanel::ZoneFile::VERSION:1.3 hostname:cpanel.airstreamcomm.net latest:11.30.3.5
; Zone file for ${zone}
$(cat ${tmpZoneFiles}/${zone})
EOF
### Move the zone file into place
if (( debug )); then
echo "DEBUG: mv ${tmpcPanelZoneFiles}/${cPanelZoneFile} ${varNamed}"
else
sudo mv ${tmpcPanelZoneFiles}/${cPanelZoneFile} ${varNamed}
sudo /bin/chown named: ${varNamed}/${cPanelZoneFile}
fi
(( verbose == 1 )) && echo "\n; -- Completed ${zone}"
done
fi
if (( kill_commented )); then
echo
for line in $(grep "//zone" ${tmpNamedConf})
do
zone=${${line#*\"}%\"*}
if grep --quiet "^zone \"${zone}\"" ${tmpNamedConf}; then
echo " * ${zone} exists, not removing"
else
# check if the zone is in cPanel
if grep --quiet ${zone} ${cPanelZoneList}; then
# get the zone file name from the cPanel configuration
cPanelZoneFile=$(grep "zonefile.*:\"${zone}\"" ${cPanelZoneList} | cut -d\" -f4)
if (( debug )); then
echo "DEBUG: /scripts/killdns ${zone}"
if [ -f ${varNamed}/${cPanelZoneFile} ]
then
echo "DEBUG: rm -f ${varNamed}/${cPanelZoneFile}"
fi
else
if [ -f ${varNamed}/${cPanelZoneFile} ]
then
/scripts/killdns ${zone}
else
echo " - ${varNamed}/${cPanelZoneFile} does not exist!"
fi
fi
fi
fi
done
fi
if (( debug )); then
echo "echo ${newSerial} > ${base}/last_serial"
else
echo ${newSerial} > ${base}/last_serial
fi
### Rsync to the other cPanel DNS servers
for server in ${servers}; do
if (( debug )); then
sudo rsync -av --dry-run --exclude='cache/' --exclude='data/' ${varNamed}/ root@${server}:${varNamed}
else
sudo rsync -a --exclude='cache/' --exclude='data/' ${varNamed}/ root@${server}:${varNamed}
fi
done
if (( reload_named )); then
sudo service named reload
for server in ${servers}; do
sudo ssh root@${server} "service named reload"
done
fi
exit