abstracted chomper
[feisty_meow.git] / scripts / system / common_sysadmin.sh
1 #!/bin/bash
2
3 # this is a library of functions shared by scripts in the system folder.
4 #
5 # Author: Chris Koeritz
6
7 # removes a full domain from the DNS.
8 function remove_domain_file()
9 {
10   local domain_name="$1"; shift
11
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"
17   fi
18 }
19
20 # creates a totally new domain config file for DNS.
21 function write_new_domain_file()
22 {
23   local domain_name="$1"; shift
24
25   local domain_file="/etc/bind/${domain_name}.conf"
26
27   echo "adding a totally new domain called $domain_name"
28   echo "using the config file: $domain_file"
29
30   if [ -f $domain_file ]; then
31     echo
32     echo "The domain configuration file already exists at:"
33     echo "  $domain_file"
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."
36     echo
37     exit 0
38   fi
39
40   echo "
41 \$TTL 1W
42 @       IN SOA  @       ${SERVER_ADMIN}. (
43                 2017100801 ; serial
44                 2H ; refresh
45                 8M ; retry
46                 14D ; expiry
47                 6H ) ; minimum
48
49         IN NS           ${MAIN_NAME_SERVER}.
50         IN MX   10      ${MAIL_SERVER}.
51
52 ${domain_name}. IN A    ${IP_ADDRESS}
53         IN HINFO        \"linux server\" \"${DISTRO}\"
54 " >"$domain_file"
55
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"
59 }
60
61 # takes a zone back out of the local conf file for bind
62 function remove_zone_for_domain()
63 {
64   local domain_name="$1"; shift
65
66   local domain_file="/etc/bind/${domain_name}.conf"
67
68   # eat the zone file definition.  this will botch up badly if more text was added
69   # or the zone info shrank.
70   create_chomped_copy_of_file "/etc/bind/named.conf.local" "zone.*${domain_name}" 6
71
72 #  \cp -f "$domain_file" "$domain_file.bkup-${RANDOM}" 
73 #  test_or_die "backing up domain file: $domain_file"
74 #
75 #  # temp file to write to before we move file into place in bind.
76 #  local new_version="/tmp/$domain_file.bkup-${RANDOM}" 
77 #  \rm -f "$new_version"
78 #  test_or_die "cleaning out new version of domain file from: $new_version"
79 #
80 #  local line
81 #  local skip_count=0
82 #  while read line; do
83 #    # don't bother looking at the lines if we're already in skip mode.
84 #    if [[ $skip_count == 0 ]]; then
85 #      # find the zone for the domain.
86 #      if [[ ! "$line" =~ *"zone \"${domain_name}\""* ]]; then
87 #        echo "$line" >> "$new_version"
88 #      else
89 #        # start skipping.  we will delete this line and the next 6 lines.
90 #        ((skip_count++))
91 #echo first skip count is now $skip_count
92 #      fi
93 #    else
94 #      # we're already skipping.  let's keep going until we hit the limit.
95 #      ((skip_count++))
96 #      if [[ $skip_count >= 6 ]]; then
97 #        echo "Done skipping, and back to writing output file."
98 #        skip_count=0
99 #      fi
100 #    fi
101 #  done < "$domain_file"
102 #
103 ##put the file back into place.
104 #echo file we created looks like this:
105 #filedump "$new_version"
106 #
107 #echo bailing
108 #exit 1
109 #
110 #  \mv "$new_version" "$domain_file"
111 #  test_or_die "moving the new version into place in: $domain_file"
112
113 }
114
115 # hooks up a new config file into bind's list of zones.
116 function add_zone_for_new_domain()
117 {
118   local domain_name="$1"; shift
119
120   local domain_file="/etc/bind/${domain_name}.conf"
121
122   echo "adding a new domain configured by ${domain_file} into"
123   echo "the named.conf.local configuration file."
124
125   # append the reference to the new conf file in the zone list.
126   echo "
127 zone \"${domain_name}\" in {
128         file \"${domain_file}\";
129         type master;
130         allow-query { any; };
131 };
132
133 ////////////////////////////////////////////////////////////////////////////
134
135 " >> /etc/bind/named.conf.local
136
137   # keep ownership for the real user.
138   chown "$(logname):$(logname)" /etc/bind/named.conf.local
139   test_or_die "setting ownership on: /etc/bind/named.conf.local"
140 }
141
142 # zaps a subdomain out of the containing domain file.
143 function remove_subdomain()
144 {
145   local old_domain="$1"; shift
146
147   # split up the full domain name into subdomain portion and containing domain.
148   local subdomain="${old_domain%.*.*}"
149   local containing_domain="${old_domain#*.}"
150
151   echo "removing subdomain $subdomain from containing domain $containing_domain"
152 #hmmm: other functions could use that level of clarity in their logging.
153
154   local domain_file="/etc/bind/${containing_domain}.conf"
155   # see if config file already exists; if not, complain.
156   if [ ! -f "$domain_file" ]; then
157     echo "The domain configuration file for $old_domain is missing."
158     echo "It should already be present in: $domain_file"
159     echo "We cannot remove a subdomain if the containing domain isn't there."
160     exit 1
161   fi
162
163   # see if subdomain already present in config.
164   if [ ! $(grep -q "$old_domain" "$domain_file") ]; then
165     echo "The subdomain $subdomain is already missing from the domain"
166     echo "configuration file: $domain_file"
167     echo "Our work is apparently done for removing it."
168     return 0
169   fi
170
171   create_chomped_copy_of_file "$domain_file" \
172       "${subdomain}.*${containing_domain} *IN *A *${IP_ADDRESS}" 1
173 }
174
175 # adds a new subdomain under a containing domain.
176 function add_new_subdomain()
177 {
178   local new_domain="$1"; shift
179
180   # split up the full domain name into subdomain portion and containing domain.
181   local subdomain="${new_domain%.*.*}"
182   local containing_domain="${new_domain#*.}"
183
184   echo "adding a subdomain $subdomain to containing domain $containing_domain"
185
186   local domain_file="/etc/bind/${containing_domain}.conf"
187   # see if config file already exists; if not, complain.
188   if [ ! -f "$domain_file" ]; then
189     echo "The domain configuration file for $new_domain is missing."
190     echo "It should already be present in: $domain_file"
191     echo "Please add the containing domain before trying to add a subdomain."
192     exit 1
193   fi
194
195   # see if subdomain already present in config.
196   if [ $(grep -q "$new_domain" "$domain_file") ]; then
197     echo "The subdomain $subdomain already seems to exist in the domain"
198     echo "configuration file: $domain_file"
199     echo "Please edit the config file to remove the subdomain before trying"
200     echo "to re-add the subdomain."
201     exit 1
202   fi
203
204   # append the new subdomain into the config file.
205   echo "
206 ${subdomain}.${containing_domain}.    IN A    ${IP_ADDRESS}
207         IN HINFO \"linux server\" \"${DISTRO}\"
208 " >> /etc/bind/${containing_domain}.conf
209
210   # keep ownership for real user.
211   chown "$(logname):$(logname)" "/etc/bind/${containing_domain}.conf"
212   test_or_die "setting ownership on: /etc/bind/${containing_domain}.conf"
213 }
214
215 function restart_bind()
216 {
217   echo restarting DNS server.
218   service bind9 restart
219   if [ $? -ne 0 ]; then
220     echo "The bind service did not restart properly.  Please check the error logs."
221     exit 1
222   fi
223   echo DNS server restarted.
224 }
225