closer to remove domain working
[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 #export WORKDIR="$( \cd "$(\dirname "$0")" && \pwd )"  # obtain the script's working directory.
8 #export FEISTY_MEOW_APEX="$( \cd "$WORKDIR/../.." && \pwd )"
9
10 #source "$FEISTY_MEOW_APEX/scripts/core/launch_feisty_meow.sh"
11
12 # creates a totally new domain config file for DNS.
13 function write_new_domain_file()
14 {
15   local domain_name="$1"; shift
16
17   local domain_file="/etc/bind/${domain_name}.conf"
18
19   echo "adding a totally new domain called $domain_name"
20   echo "using the config file: $domain_file"
21
22   if [ -f $domain_file ]; then
23     echo
24     echo "The domain configuration file already exists at:"
25     echo "  $domain_file"
26     echo "Since we don't want to tear that down if it has specialized configuration"
27     echo "data in it, we will just leave it in place and consider our job done."
28     echo
29     exit 0
30   fi
31
32   echo "
33 \$TTL 1W
34 @       IN SOA  @       ${SERVER_ADMIN}. (
35                 2017100801 ; serial
36                 2H ; refresh
37                 8M ; retry
38                 14D ; expiry
39                 6H ) ; minimum
40
41         IN NS           ${MAIN_NAME_SERVER}.
42         IN MX   10      ${MAIL_SERVER}.
43
44 ${domain_name}. IN A    ${IP_ADDRESS}
45         IN HINFO        \"linux server\" \"${DISTRO}\"
46 " >"$domain_file"
47
48   # our personalized configuration approach wants the real owner to own the file.
49   chown "$(logname):$(logname)" $domain_file
50   test_or_die "setting ownership on: $domain_file"
51 }
52
53 # hooks up a new config file into bind's list of zones.
54 function add_zone_for_new_domain()
55 {
56   local domain_name="$1"; shift
57
58   local domain_file="/etc/bind/${domain_name}.conf"
59
60   echo "adding a new domain configured by ${domain_file} into"
61   echo "the named.conf.local configuration file."
62
63   # append the reference to the new conf file in the zone list.
64   echo "
65 zone \"${domain_name}\" in {
66         file \"${domain_file}\";
67         type master;
68         allow-query { any; };
69 };
70
71 ////////////////////////////////////////////////////////////////////////////
72
73 " >> /etc/bind/named.conf.local
74
75   # keep ownership for the real user.
76   chown "$(logname):$(logname)" /etc/bind/named.conf.local
77   test_or_die "setting ownership on: /etc/bind/named.conf.local"
78
79 }
80
81 # adds a new subdomain under a containing domain.
82 function add_new_subdomain()
83 {
84   local new_domain="$1"; shift
85
86   # split up the full domain name into subdomain portion and containing domain.
87   local subdomain="${new_domain%.*.*}"
88   local containing_domain="${new_domain#*.}"
89
90   echo "adding a subdomain $subdomain to containing domain $containing_domain"
91
92   local domain_file="/etc/bind/${containing_domain}.conf"
93   # see if config file already exists; if not, complain.
94   if [ ! -f "$domain_file" ]; then
95     echo "The domain configuration file for $new_domain is missing."
96     echo "It should already be present in: $domain_file"
97     echo "Please add the containing domain before trying to add a subdomain."
98     exit 1
99   fi
100
101   # see if subdomain already present in config.
102   if [ $(grep -q "$new_domain" "$domain_file") ]; then
103     echo "The subdomain $subdomain already seems to exist in the domain"
104     echo "configuration file: $domain_file"
105     echo "Please edit the config file to remove the subdomain before trying"
106     echo "to re-add the subdomain."
107     exit 1
108   fi
109
110   # append the new subdomain into the config file.
111   echo "
112 ${subdomain}.${containing_domain}.    IN A    ${IP_ADDRESS}
113         IN HINFO \"linux server\" \"${DISTRO}\"
114 " >> /etc/bind/${containing_domain}.conf
115
116   # keep ownership for real user.
117   chown "$(logname):$(logname)" "/etc/bind/${containing_domain}.conf"
118   test_or_die "setting ownership on: /etc/bind/${containing_domain}.conf"
119 }
120
121 function restart_bind()
122 {
123   echo restarting DNS server.
124   service bind9 restart
125   if [ $? -ne 0 ]; then
126     echo "The bind service did not restart properly.  Please check the error logs."
127     exit 1
128   fi
129   echo DNS server restarted.
130 }
131