d3dfbad1d5a793b9ff2f050aaa95d26ef337a0c5
[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
73 # hooks up a new config file into bind's list of zones.
74 function add_zone_for_new_domain()
75 {
76   local domain_name="$1"; shift
77
78   local domain_file="/etc/bind/${domain_name}.conf"
79
80   echo "adding a new domain configured by ${domain_file} into"
81   echo "the named.conf.local configuration file."
82
83   # append the reference to the new conf file in the zone list.
84   echo "
85 zone \"${domain_name}\" in {
86         file \"${domain_file}\";
87         type master;
88         allow-query { any; };
89 };
90
91 ////////////////////////////////////////////////////////////////////////////
92
93 " >> /etc/bind/named.conf.local
94
95   # keep ownership for the real user.
96   chown "$(logname):$(logname)" /etc/bind/named.conf.local
97   test_or_die "setting ownership on: /etc/bind/named.conf.local"
98 }
99
100 # zaps a subdomain out of the containing domain file.
101 function remove_subdomain()
102 {
103   local old_domain="$1"; shift
104
105   # split up the full domain name into subdomain portion and containing domain.
106   local subdomain="${old_domain%.*.*}"
107   local containing_domain="${old_domain#*.}"
108
109   echo "removing subdomain $subdomain from containing domain $containing_domain"
110
111   local domain_file="/etc/bind/${containing_domain}.conf"
112   # see if config file already exists; if not, complain.
113   if [ ! -f "$domain_file" ]; then
114     echo "The domain configuration file for $old_domain is missing."
115     echo "It should already be present in: $domain_file"
116     echo "We cannot remove a subdomain if the containing domain isn't there."
117     exit 1
118   fi
119
120   # see if subdomain already present in config.
121   if ! grep -q "$old_domain" "$domain_file"; then
122     echo "The subdomain $subdomain is already missing from the domain"
123     echo "configuration file: $domain_file"
124     echo "Our work is apparently done for removing it."
125     return 0
126   fi
127
128   create_chomped_copy_of_file "$domain_file" \
129       "${subdomain}.*${containing_domain} *IN *A *${IP_ADDRESS}" 1
130 }
131
132 # adds a new subdomain under a containing domain.
133 function add_new_subdomain()
134 {
135   local new_domain="$1"; shift
136
137   # split up the full domain name into subdomain portion and containing domain.
138   local subdomain="${new_domain%.*.*}"
139   local containing_domain="${new_domain#*.}"
140
141   echo "adding a subdomain $subdomain to containing domain $containing_domain"
142
143   local domain_file="/etc/bind/${containing_domain}.conf"
144   # see if config file already exists; if not, complain.
145   if [ ! -f "$domain_file" ]; then
146     echo "The domain configuration file for $new_domain is missing."
147     echo "It should already be present in: $domain_file"
148     echo "Please add the containing domain before trying to add a subdomain."
149     exit 1
150   fi
151
152   # see if subdomain already present in config.
153   if grep -q "$new_domain" "$domain_file"; then
154     echo "The subdomain $subdomain already seems to exist in the domain"
155     echo "configuration file: $domain_file"
156     echo "Please edit the config file to remove the subdomain before trying"
157     echo "to re-add the subdomain."
158     exit 1
159   fi
160
161   # append the new subdomain into the config file.
162   echo "
163 ${subdomain}.${containing_domain}.    IN A    ${IP_ADDRESS}
164         IN HINFO \"linux server\" \"${DISTRO}\"
165 " >> /etc/bind/${containing_domain}.conf
166
167   # keep ownership for real user.
168   chown "$(logname):$(logname)" "/etc/bind/${containing_domain}.conf"
169   test_or_die "setting ownership on: /etc/bind/${containing_domain}.conf"
170 }
171
172 function restart_bind()
173 {
174   echo restarting DNS server.
175   service bind9 restart
176   if [ $? -ne 0 ]; then
177     echo "The bind service did not restart properly.  Please check the error logs."
178     exit 1
179   fi
180   echo DNS server restarted.
181 }
182