in process, hardest code is done
[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     \rm -f "$domain_file"
15     test_or_die "removing domain file: $domain_file"
16   fi
17 }
18
19 # creates a totally new domain config file for DNS.
20 function write_new_domain_file()
21 {
22   local domain_name="$1"; shift
23
24   local domain_file="/etc/bind/${domain_name}.conf"
25
26   echo "adding a totally new domain called $domain_name"
27   echo "using the config file: $domain_file"
28
29   if [ -f $domain_file ]; then
30     echo
31     echo "The domain configuration file already exists at:"
32     echo "  $domain_file"
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."
35     echo
36     exit 0
37   fi
38
39   echo "
40 \$TTL 1W
41 @       IN SOA  @       ${SERVER_ADMIN}. (
42                 2017100801 ; serial
43                 2H ; refresh
44                 8M ; retry
45                 14D ; expiry
46                 6H ) ; minimum
47
48         IN NS           ${MAIN_NAME_SERVER}.
49         IN MX   10      ${MAIL_SERVER}.
50
51 ${domain_name}. IN A    ${IP_ADDRESS}
52         IN HINFO        \"linux server\" \"${DISTRO}\"
53 " >"$domain_file"
54
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"
58 }
59
60 # takes a zone back out of the local conf file for bind
61 function remove_zone_for_domain()
62 {
63   local domain_name="$1"; shift
64
65   local domain_file="/etc/bind/${domain_name}.conf"
66
67   \cp -f "$domain_file" "$domain_file.bkup-${RANDOM}" 
68   test_or_die "backing up domain file: $domain_file"
69
70   # temp file to write to before we move file into place in bind.
71   local new_version="/tmp/$domain_file.bkup-${RANDOM}" 
72   \rm -f "$new_version"
73   test_or_die "cleaning out new version of domain file from : $new_version"
74
75   local line
76   local skip_count=0
77   while read line; do
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"
83       else
84         # start skipping.  we will delete this line and the next 6 lines.
85         ((skip_count++))
86 echo first skip count is now $skip_count
87       fi
88     else
89       # we're already skipping.  let's keep going until we hit the limit.
90       ((skip_count++))
91       if [[ $skip_count >= 6 ]]; then
92         echo "Done skipping, and back to writing output file."
93         skip_count=0
94       fi
95     fi
96   done < "$domain_file"
97
98 #put the file back into place.
99 echo file we created looks like this:
100 filedump "$new_version"
101
102 echo bailing
103 exit 1
104
105 }
106
107 # hooks up a new config file into bind's list of zones.
108 function add_zone_for_new_domain()
109 {
110   local domain_name="$1"; shift
111
112   local domain_file="/etc/bind/${domain_name}.conf"
113
114   echo "adding a new domain configured by ${domain_file} into"
115   echo "the named.conf.local configuration file."
116
117   # append the reference to the new conf file in the zone list.
118   echo "
119 zone \"${domain_name}\" in {
120         file \"${domain_file}\";
121         type master;
122         allow-query { any; };
123 };
124
125 ////////////////////////////////////////////////////////////////////////////
126
127 " >> /etc/bind/named.conf.local
128
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"
132
133 }
134
135 # adds a new subdomain under a containing domain.
136 function add_new_subdomain()
137 {
138   local new_domain="$1"; shift
139
140   # split up the full domain name into subdomain portion and containing domain.
141   local subdomain="${new_domain%.*.*}"
142   local containing_domain="${new_domain#*.}"
143
144   echo "adding a subdomain $subdomain to containing domain $containing_domain"
145
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."
152     exit 1
153   fi
154
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."
161     exit 1
162   fi
163
164   # append the new subdomain into the config file.
165   echo "
166 ${subdomain}.${containing_domain}.    IN A    ${IP_ADDRESS}
167         IN HINFO \"linux server\" \"${DISTRO}\"
168 " >> /etc/bind/${containing_domain}.conf
169
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"
173 }
174
175 function restart_bind()
176 {
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."
181     exit 1
182   fi
183   echo DNS server restarted.
184 }
185