3 # this is a library of functions shared by scripts in the system folder.
5 # Author: Chris Koeritz
7 # removes a full domain from the DNS.
8 function remove_domain_file()
10 local domain_name="$1"; shift
12 local domain_file="/etc/bind/${domain_name}.conf"
13 if [ -f "$domain_file" ]; then
14 # don't destroy, just shuffle.
15 \mv -f "$domain_file" "/tmp/$(basename ${domain_file})-old-${RANDOM}"
16 test_or_die "removing domain file: $domain_file"
20 # creates a totally new domain config file for DNS.
21 function write_new_domain_file()
23 local domain_name="$1"; shift
25 local domain_file="/etc/bind/${domain_name}.conf"
27 echo "adding a totally new domain called $domain_name"
28 echo "using the config file: $domain_file"
30 if [ -f $domain_file ]; then
32 echo "The domain configuration file already exists at:"
34 echo "Since we don't want to tear that down if it has specialized configuration"
35 echo "data in it, we will just leave it in place and consider our job done."
42 @ IN SOA @ ${SERVER_ADMIN}. (
49 IN NS ${MAIN_NAME_SERVER}.
50 IN MX 10 ${MAIL_SERVER}.
52 ${domain_name}. IN A ${IP_ADDRESS}
53 IN HINFO \"linux server\" \"${DISTRO}\"
56 # our personalized configuration approach wants the real owner to own the file.
57 chown "$(logname):$(logname)" $domain_file
58 test_or_die "setting ownership on: $domain_file"
61 #hmmm: move this chomper to core!
63 # given a filename and a string to seek and a number of lines, then this
64 # function will remove the first occurrence of a line in the file that
65 # matches the string, and it will also axe the next N lines as specified.
66 function create_chomped_copy_of_file()
68 local filename="$1"; shift
69 local seeker="$1"; shift
70 local numlines=$1; shift
72 echo into create_chomped_copy...
73 var filename seeker numlines
75 # make a backup first, oy.
76 \cp -f "$filename" "$filename.bkup-${RANDOM}"
77 test_or_die "backing up file: $filename"
79 # make a temp file to write to before we move file into place in bind.
80 local new_version="/tmp/$(basename ${filename}).bkup-${RANDOM}"
82 test_or_die "cleaning out new version of file from: $new_version"
87 # don't bother looking at the lines if we're already in skip mode.
88 if [[ $skip_count == 0 ]]; then
89 # find the string they're seeking.
90 if [[ ! "$line" =~ .*${seeker}.* ]]; then
92 echo "$line" >> "$new_version"
94 # a match! start skipping. we will delete this line and the next N lines.
96 echo first skip count is now $skip_count
99 # we're already skipping. let's keep going until we hit the limit.
101 echo ongoing skip count is now $skip_count
102 if (( $skip_count > $numlines )); then
103 echo "Done skipping, and back to writing output file."
109 #put the file back into place.
110 echo file we created looks like this:
111 filedump "$new_version"
116 \mv "$new_version" "$filename"
117 test_or_die "moving the new version into place in: $filename"
121 # takes a zone back out of the local conf file for bind
122 function remove_zone_for_domain()
124 local domain_name="$1"; shift
126 local domain_file="/etc/bind/${domain_name}.conf"
128 # eat the zone file definition. this will botch up badly if more text was added
129 # or the zone info shrank.
130 create_chomped_copy_of_file "/etc/bind/named.conf.local" "zone.*${domain_name}" 6
132 # \cp -f "$domain_file" "$domain_file.bkup-${RANDOM}"
133 # test_or_die "backing up domain file: $domain_file"
135 # # temp file to write to before we move file into place in bind.
136 # local new_version="/tmp/$domain_file.bkup-${RANDOM}"
137 # \rm -f "$new_version"
138 # test_or_die "cleaning out new version of domain file from: $new_version"
142 # while read line; do
143 # # don't bother looking at the lines if we're already in skip mode.
144 # if [[ $skip_count == 0 ]]; then
145 # # find the zone for the domain.
146 # if [[ ! "$line" =~ *"zone \"${domain_name}\""* ]]; then
147 # echo "$line" >> "$new_version"
149 # # start skipping. we will delete this line and the next 6 lines.
151 #echo first skip count is now $skip_count
154 # # we're already skipping. let's keep going until we hit the limit.
156 # if [[ $skip_count >= 6 ]]; then
157 # echo "Done skipping, and back to writing output file."
161 # done < "$domain_file"
163 ##put the file back into place.
164 #echo file we created looks like this:
165 #filedump "$new_version"
170 # \mv "$new_version" "$domain_file"
171 # test_or_die "moving the new version into place in: $domain_file"
175 # hooks up a new config file into bind's list of zones.
176 function add_zone_for_new_domain()
178 local domain_name="$1"; shift
180 local domain_file="/etc/bind/${domain_name}.conf"
182 echo "adding a new domain configured by ${domain_file} into"
183 echo "the named.conf.local configuration file."
185 # append the reference to the new conf file in the zone list.
187 zone \"${domain_name}\" in {
188 file \"${domain_file}\";
190 allow-query { any; };
193 ////////////////////////////////////////////////////////////////////////////
195 " >> /etc/bind/named.conf.local
197 # keep ownership for the real user.
198 chown "$(logname):$(logname)" /etc/bind/named.conf.local
199 test_or_die "setting ownership on: /etc/bind/named.conf.local"
202 # zaps a subdomain out of the containing domain file.
203 function remove_subdomain()
205 local old_domain="$1"; shift
207 # split up the full domain name into subdomain portion and containing domain.
208 local subdomain="${old_domain%.*.*}"
209 local containing_domain="${old_domain#*.}"
211 echo "removing subdomain $subdomain from containing domain $containing_domain"
212 #hmmm: other functions could use that level of clarity in their logging.
214 local domain_file="/etc/bind/${containing_domain}.conf"
215 # see if config file already exists; if not, complain.
216 if [ ! -f "$domain_file" ]; then
217 echo "The domain configuration file for $old_domain is missing."
218 echo "It should already be present in: $domain_file"
219 echo "We cannot remove a subdomain if the containing domain isn't there."
223 # see if subdomain already present in config.
224 if [ ! $(grep -q "$old_domain" "$domain_file") ]; then
225 echo "The subdomain $subdomain is already missing from the domain"
226 echo "configuration file: $domain_file"
227 echo "Our work is apparently done for removing it."
231 create_chomped_copy_of_file "$domain_file" \
232 "${subdomain}.*${containing_domain} *IN *A *${IP_ADDRESS}" 1
235 # adds a new subdomain under a containing domain.
236 function add_new_subdomain()
238 local new_domain="$1"; shift
240 # split up the full domain name into subdomain portion and containing domain.
241 local subdomain="${new_domain%.*.*}"
242 local containing_domain="${new_domain#*.}"
244 echo "adding a subdomain $subdomain to containing domain $containing_domain"
246 local domain_file="/etc/bind/${containing_domain}.conf"
247 # see if config file already exists; if not, complain.
248 if [ ! -f "$domain_file" ]; then
249 echo "The domain configuration file for $new_domain is missing."
250 echo "It should already be present in: $domain_file"
251 echo "Please add the containing domain before trying to add a subdomain."
255 # see if subdomain already present in config.
256 if [ $(grep -q "$new_domain" "$domain_file") ]; then
257 echo "The subdomain $subdomain already seems to exist in the domain"
258 echo "configuration file: $domain_file"
259 echo "Please edit the config file to remove the subdomain before trying"
260 echo "to re-add the subdomain."
264 # append the new subdomain into the config file.
266 ${subdomain}.${containing_domain}. IN A ${IP_ADDRESS}
267 IN HINFO \"linux server\" \"${DISTRO}\"
268 " >> /etc/bind/${containing_domain}.conf
270 # keep ownership for real user.
271 chown "$(logname):$(logname)" "/etc/bind/${containing_domain}.conf"
272 test_or_die "setting ownership on: /etc/bind/${containing_domain}.conf"
275 function restart_bind()
277 echo restarting DNS server.
278 service bind9 restart
279 if [ $? -ne 0 ]; then
280 echo "The bind service did not restart properly. Please check the error logs."
283 echo DNS server restarted.