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/${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"
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 # make a backup first, oy.
73 \cp -f "$filename" "$filename.bkup-${RANDOM}"
74 test_or_die "backing up file: $filename"
76 # make a temp file to write to before we move file into place in bind.
77 local new_version="/tmp/${filename}.bkup-${RANDOM}"
79 test_or_die "cleaning out new version of file from: $new_version"
84 # don't bother looking at the lines if we're already in skip mode.
85 if [[ $skip_count == 0 ]]; then
86 # find the string they're seeking.
87 if [[ ! "$line" =~ *"$seeker"* ]]; then
89 echo "$line" >> "$new_version"
91 # a match! start skipping. we will delete this line and the next N lines.
93 echo first skip count is now $skip_count
96 # we're already skipping. let's keep going until we hit the limit.
98 echo ongoing skip count is now $skip_count
99 if [[ $skip_count >= $numlines ]]; then
100 echo "Done skipping, and back to writing output file."
106 #put the file back into place.
107 echo file we created looks like this:
108 filedump "$new_version"
113 \mv "$new_version" "$filename"
114 test_or_die "moving the new version into place in: $filename"
119 # takes a zone back out of the local conf file for bind
120 function remove_zone_for_domain()
122 local domain_name="$1"; shift
124 local domain_file="/etc/bind/${domain_name}.conf"
126 # eat the zone file definition. this will botch up badly if more text was added
127 # or the zone info shrank.
128 create_chomped_copy_of_file "$domain_file" "zone \"${domain_name}\"" 6
131 \cp -f "$domain_file" "$domain_file.bkup-${RANDOM}"
132 test_or_die "backing up domain file: $domain_file"
134 # temp file to write to before we move file into place in bind.
135 local new_version="/tmp/$domain_file.bkup-${RANDOM}"
136 \rm -f "$new_version"
137 test_or_die "cleaning out new version of domain file from: $new_version"
142 # don't bother looking at the lines if we're already in skip mode.
143 if [[ $skip_count == 0 ]]; then
144 # find the zone for the domain.
145 if [[ ! "$line" =~ *"zone \"${domain_name}\""* ]]; then
146 echo "$line" >> "$new_version"
148 # start skipping. we will delete this line and the next 6 lines.
150 echo first skip count is now $skip_count
153 # we're already skipping. let's keep going until we hit the limit.
155 if [[ $skip_count >= 6 ]]; then
156 echo "Done skipping, and back to writing output file."
160 done < "$domain_file"
162 #put the file back into place.
163 echo file we created looks like this:
164 filedump "$new_version"
169 \mv "$new_version" "$domain_file"
170 test_or_die "moving the new version into place in: $domain_file"
174 # hooks up a new config file into bind's list of zones.
175 function add_zone_for_new_domain()
177 local domain_name="$1"; shift
179 local domain_file="/etc/bind/${domain_name}.conf"
181 echo "adding a new domain configured by ${domain_file} into"
182 echo "the named.conf.local configuration file."
184 # append the reference to the new conf file in the zone list.
186 zone \"${domain_name}\" in {
187 file \"${domain_file}\";
189 allow-query { any; };
192 ////////////////////////////////////////////////////////////////////////////
194 " >> /etc/bind/named.conf.local
196 # keep ownership for the real user.
197 chown "$(logname):$(logname)" /etc/bind/named.conf.local
198 test_or_die "setting ownership on: /etc/bind/named.conf.local"
202 # adds a new subdomain under a containing domain.
203 function add_new_subdomain()
205 local new_domain="$1"; shift
207 # split up the full domain name into subdomain portion and containing domain.
208 local subdomain="${new_domain%.*.*}"
209 local containing_domain="${new_domain#*.}"
211 echo "adding a subdomain $subdomain to containing domain $containing_domain"
213 local domain_file="/etc/bind/${containing_domain}.conf"
214 # see if config file already exists; if not, complain.
215 if [ ! -f "$domain_file" ]; then
216 echo "The domain configuration file for $new_domain is missing."
217 echo "It should already be present in: $domain_file"
218 echo "Please add the containing domain before trying to add a subdomain."
222 # see if subdomain already present in config.
223 if [ $(grep -q "$new_domain" "$domain_file") ]; then
224 echo "The subdomain $subdomain already seems to exist in the domain"
225 echo "configuration file: $domain_file"
226 echo "Please edit the config file to remove the subdomain before trying"
227 echo "to re-add the subdomain."
231 # append the new subdomain into the config file.
233 ${subdomain}.${containing_domain}. IN A ${IP_ADDRESS}
234 IN HINFO \"linux server\" \"${DISTRO}\"
235 " >> /etc/bind/${containing_domain}.conf
237 # keep ownership for real user.
238 chown "$(logname):$(logname)" "/etc/bind/${containing_domain}.conf"
239 test_or_die "setting ownership on: /etc/bind/${containing_domain}.conf"
242 function restart_bind()
244 echo restarting DNS server.
245 service bind9 restart
246 if [ $? -ne 0 ]; then
247 echo "The bind service did not restart properly. Please check the error logs."
250 echo DNS server restarted.