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
15 test_or_die "removing domain file: $domain_file"
19 # creates a totally new domain config file for DNS.
20 function write_new_domain_file()
22 local domain_name="$1"; shift
24 local domain_file="/etc/bind/${domain_name}.conf"
26 echo "adding a totally new domain called $domain_name"
27 echo "using the config file: $domain_file"
29 if [ -f $domain_file ]; then
31 echo "The domain configuration file already exists at:"
33 echo "Since we don't want to tear that down if it has specialized configuration"
34 echo "data in it, we will just leave it in place and consider our job done."
41 @ IN SOA @ ${SERVER_ADMIN}. (
48 IN NS ${MAIN_NAME_SERVER}.
49 IN MX 10 ${MAIL_SERVER}.
51 ${domain_name}. IN A ${IP_ADDRESS}
52 IN HINFO \"linux server\" \"${DISTRO}\"
55 # our personalized configuration approach wants the real owner to own the file.
56 chown "$(logname):$(logname)" $domain_file
57 test_or_die "setting ownership on: $domain_file"
60 # takes a zone back out of the local conf file for bind
61 function remove_zone_for_domain()
63 local domain_name="$1"; shift
65 local domain_file="/etc/bind/${domain_name}.conf"
67 \cp -f "$domain_file" "$domain_file.bkup-${RANDOM}"
68 test_or_die "backing up domain file: $domain_file"
70 # temp file to write to before we move file into place in bind.
71 local new_version="/tmp/$domain_file.bkup-${RANDOM}"
73 test_or_die "cleaning out new version of domain file from : $new_version"
78 # don't bother looking at the lines if we're already in skip mode.
79 if [[ $skip_count == 0 ]]; then
80 # find the zone for the domain.
81 if [[ ! "$line" =~ *"zone \"${domain_name}\""* ]]; then
82 echo "$line" >> "$new_version"
84 # start skipping. we will delete this line and the next 6 lines.
86 echo first skip count is now $skip_count
89 # we're already skipping. let's keep going until we hit the limit.
91 if [[ $skip_count >= 6 ]]; then
92 echo "Done skipping, and back to writing output file."
98 #put the file back into place.
99 echo file we created looks like this:
100 filedump "$new_version"
107 # hooks up a new config file into bind's list of zones.
108 function add_zone_for_new_domain()
110 local domain_name="$1"; shift
112 local domain_file="/etc/bind/${domain_name}.conf"
114 echo "adding a new domain configured by ${domain_file} into"
115 echo "the named.conf.local configuration file."
117 # append the reference to the new conf file in the zone list.
119 zone \"${domain_name}\" in {
120 file \"${domain_file}\";
122 allow-query { any; };
125 ////////////////////////////////////////////////////////////////////////////
127 " >> /etc/bind/named.conf.local
129 # keep ownership for the real user.
130 chown "$(logname):$(logname)" /etc/bind/named.conf.local
131 test_or_die "setting ownership on: /etc/bind/named.conf.local"
135 # adds a new subdomain under a containing domain.
136 function add_new_subdomain()
138 local new_domain="$1"; shift
140 # split up the full domain name into subdomain portion and containing domain.
141 local subdomain="${new_domain%.*.*}"
142 local containing_domain="${new_domain#*.}"
144 echo "adding a subdomain $subdomain to containing domain $containing_domain"
146 local domain_file="/etc/bind/${containing_domain}.conf"
147 # see if config file already exists; if not, complain.
148 if [ ! -f "$domain_file" ]; then
149 echo "The domain configuration file for $new_domain is missing."
150 echo "It should already be present in: $domain_file"
151 echo "Please add the containing domain before trying to add a subdomain."
155 # see if subdomain already present in config.
156 if [ $(grep -q "$new_domain" "$domain_file") ]; then
157 echo "The subdomain $subdomain already seems to exist in the domain"
158 echo "configuration file: $domain_file"
159 echo "Please edit the config file to remove the subdomain before trying"
160 echo "to re-add the subdomain."
164 # append the new subdomain into the config file.
166 ${subdomain}.${containing_domain}. IN A ${IP_ADDRESS}
167 IN HINFO \"linux server\" \"${DISTRO}\"
168 " >> /etc/bind/${containing_domain}.conf
170 # keep ownership for real user.
171 chown "$(logname):$(logname)" "/etc/bind/${containing_domain}.conf"
172 test_or_die "setting ownership on: /etc/bind/${containing_domain}.conf"
175 function restart_bind()
177 echo restarting DNS server.
178 service bind9 restart
179 if [ $? -ne 0 ]; then
180 echo "The bind service did not restart properly. Please check the error logs."
183 echo DNS server restarted.