done with remove domain. whew.
[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   else
18     echo "Did not see a domain file to remove: $domain_file"
19   fi
20 }
21
22 # creates a totally new domain config file for DNS.
23 function write_new_domain_file()
24 {
25   local domain_name="$1"; shift
26
27   local domain_file="/etc/bind/${domain_name}.conf"
28
29   echo "adding a totally new domain called $domain_name"
30   echo "using the config file: $domain_file"
31
32   if [ -f $domain_file ]; then
33     echo
34     echo "The domain configuration file already exists at:"
35     echo "  $domain_file"
36     echo "Since we don't want to tear that down if it has specialized configuration"
37     echo "data in it, we will just leave it in place and consider our job done."
38     echo
39     exit 0
40   fi
41
42   echo "
43 \$TTL 1W
44 @       IN SOA  @       ${SERVER_ADMIN}. (
45                 2017100801 ; serial
46                 2H ; refresh
47                 8M ; retry
48                 14D ; expiry
49                 6H ) ; minimum
50
51         IN NS           ${MAIN_NAME_SERVER}.
52         IN MX   10      ${MAIL_SERVER}.
53
54 ${domain_name}. IN A    ${IP_ADDRESS}
55         IN HINFO        \"linux server\" \"${DISTRO}\"
56 " >"$domain_file"
57
58   # our personalized configuration approach wants the real owner to own the file.
59   chown "$(logname):$(logname)" $domain_file
60   test_or_die "setting ownership on: $domain_file"
61 }
62
63 # takes a zone back out of the local conf file for bind
64 function remove_zone_for_domain()
65 {
66   local domain_name="$1"; shift
67
68   local domain_file="/etc/bind/${domain_name}.conf"
69
70   # eat the zone file definition.  this will botch up badly if more text was added
71   # or the zone info shrank.
72   create_chomped_copy_of_file "/etc/bind/named.conf.local" "zone.*${domain_name}" 6
73 }
74
75 # hooks up a new config file into bind's list of zones.
76 function add_zone_for_new_domain()
77 {
78   local domain_name="$1"; shift
79
80   local domain_file="/etc/bind/${domain_name}.conf"
81
82   echo "adding a new domain configured by ${domain_file} into"
83   echo "the named.conf.local configuration file."
84
85   # append the reference to the new conf file in the zone list.
86   echo "
87 zone \"${domain_name}\" in {
88         file \"${domain_file}\";
89         type master;
90         allow-query { any; };
91 };
92
93 ////////////////////////////////////////////////////////////////////////////
94
95 " >> /etc/bind/named.conf.local
96
97   # keep ownership for the real user.
98   chown "$(logname):$(logname)" /etc/bind/named.conf.local
99   test_or_die "setting ownership on: /etc/bind/named.conf.local"
100 }
101
102 # zaps a subdomain out of the containing domain file.
103 function remove_subdomain()
104 {
105   local old_domain="$1"; shift
106
107   # split up the full domain name into subdomain portion and containing domain.
108   local subdomain="${old_domain%.*.*}"
109   local containing_domain="${old_domain#*.}"
110
111   echo "removing subdomain $subdomain from containing domain $containing_domain"
112
113   local domain_file="/etc/bind/${containing_domain}.conf"
114   # see if config file already exists; if not, complain.
115   if [ ! -f "$domain_file" ]; then
116     echo "The domain configuration file for $old_domain is missing."
117     echo "It should already be present in: $domain_file"
118     echo "We cannot remove a subdomain if the containing domain isn't there."
119     exit 1
120   fi
121
122   # see if subdomain already present in config.
123   if ! grep -q "$old_domain" "$domain_file"; then
124     echo "The subdomain $subdomain is already missing from the domain"
125     echo "configuration file: $domain_file"
126     echo "Our work is apparently done for removing it."
127     return 0
128   fi
129
130   create_chomped_copy_of_file "$domain_file" "${old_domain}" 2
131 }
132
133 # adds a new subdomain under a containing domain.
134 function add_new_subdomain()
135 {
136   local new_domain="$1"; shift
137
138   # split up the full domain name into subdomain portion and containing domain.
139   local subdomain="${new_domain%.*.*}"
140   local containing_domain="${new_domain#*.}"
141
142   echo "adding a subdomain $subdomain to containing domain $containing_domain"
143
144   local domain_file="/etc/bind/${containing_domain}.conf"
145   # see if config file already exists; if not, complain.
146   if [ ! -f "$domain_file" ]; then
147     echo "The domain configuration file for $new_domain is missing."
148     echo "It should already be present in: $domain_file"
149     echo "Please add the containing domain before trying to add a subdomain."
150     exit 1
151   fi
152
153   # see if subdomain already present in config.
154   if grep -q "$new_domain" "$domain_file"; then
155     echo "The subdomain $subdomain already seems to exist in the domain"
156     echo "configuration file: $domain_file"
157     echo "We are considering our work done; if you want to modify the subdomain,"
158     echo "then please call remove_domain on it first."
159     return 0
160   fi
161
162   # append the new subdomain into the config file.
163   echo "${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