mods from cakelampvm
[feisty_meow.git] / scripts / system / add_domain.sh
1 #!/bin/bash
2
3 # this set of functions serve the main purpose of adding new domains or subdomains to the bind9 DNS server on the current host.
4 # it is currently highly specific to running a bunch of domains on a linux VM, where the VM has one IP address.
5 # note that bind 'named' must already be configured.
6 # also, it is assumed that if a subdomain is being added, then the containing domain has already been configured and is 
7 # configured in a file similar to "blah.com.conf" in /etc/bind.
8 #
9 # Author: Chris Koeritz
10
11 # some defaults that are convenient for current purposes.
12
13 # hmmm: !!! these would need to be parameterized somehow for this script to become really general.
14
15 # in our scheme, the single IP address that all our domains map to.
16 IP_ADDRESS="10.28.42.20"
17 # the email address (where first dot is replaced by @) for the administrator of the domain.
18 SERVER_ADMIN="developer.cakelampvm.com"
19 # the name of the name server for the new domains (should already be configured).
20 MAIN_NAME_SERVER="ns.cakelampvm.com"
21 # the name of the mail server for a new domain (should already be configured).
22 MAIL_SERVER="mail.cakelampvm.com"
23 # the distribution name to be listed in info for the new domain or subdomain.
24 DISTRO="ubuntu"
25
26 # creates a totally new domain config file for DNS.
27 function write_new_domain_file()
28 {
29   local domain_name="$1"; shift
30
31   local domain_file="/etc/bind/${domain_name}.conf"
32
33   echo "adding a totally new domain called $domain_name"
34   echo "using the config file: $domain_file"
35
36   if [ -f $domain_file ]; then
37     echo
38     echo "The domain configuration file already exists at:"
39     echo "  $domain_file"
40     echo "Since we don't want to tear that down if it has specialized configuration"
41     echo "data in it, we will just leave it in place and consider our job done."
42     echo
43     exit 0
44   fi
45
46   echo "
47 \$TTL 1W
48 @       IN SOA  @       ${SERVER_ADMIN}. (
49                 2017100801 ; serial
50                 2H ; refresh
51                 8M ; retry
52                 14D ; expiry
53                 6H ) ; minimum
54
55         IN NS           ${MAIN_NAME_SERVER}.
56         IN MX   10      ${MAIL_SERVER}.
57
58 ${domain_name}. IN A    ${IP_ADDRESS}
59         IN HINFO        \"linux server\" \"${DISTRO}\"
60 " >"$domain_file"
61 }
62
63 # hooks up a new config file into bind's list of zones.
64 function add_zone_for_new_domain()
65 {
66   local domain_name="$1"; shift
67
68   local domain_file="/etc/bind/${domain_name}.conf"
69
70   echo "adding a new domain configured by ${domain_file} into"
71   echo "the named.conf.local configuration file."
72
73   # append the reference to the new conf file in the zone list.
74   echo "
75 zone \"${domain_name}\" in {
76         file \"${domain_file}\";
77         type master;
78         allow-query { any; };
79 };
80
81 ////////////////////////////////////////////////////////////////////////////
82
83 " >> /etc/bind/named.conf.local
84 }
85
86 # adds a new subdomain under a containing domain.
87 function add_new_subdomain()
88 {
89   local new_domain="$1"; shift
90
91   # split up the full domain name into subdomain portion and containing domain.
92   local subdomain="${new_domain%.*.*}"
93   local containing_domain="${new_domain#*.}"
94
95   echo "adding a subdomain $subdomain to containing domain $containing_domain"
96
97   local domain_file="/etc/bind/${containing_domain}.conf"
98   # see if config file already exists; if not, complain.
99   if [ ! -f "$domain_file" ]; then
100     echo "The domain configuration file for $new_domain is missing."
101     echo "It should already be present in: $domain_file"
102     echo "Please add the containing domain before trying to add a subdomain."
103     exit 1
104   fi
105
106   # see if subdomain already present in config.
107   if [ $(grep -q "$new_domain" "$domain_file") ]; then
108     echo "The subdomain $subdomain already seems to exist in the domain"
109     echo "configuration file: $domain_file"
110     echo "Please edit the config file to remove the subdomain before trying"
111     echo "to re-add the subdomain."
112     exit 1
113   fi
114
115   # append the new subdomain into the config file.
116   echo "
117 ${subdomain}.${containing_domain}.    IN A    ${IP_ADDRESS}
118         IN HINFO \"linux server\" \"${DISTRO}\"
119 " >> /etc/bind/${containing_domain}.conf
120
121 }
122
123 function restart_bind()
124 {
125   echo restarting DNS server.
126   service bind9 restart
127   if [ $? -ne 0 ]; then
128     echo "The bind service did not restart properly.  Please check the error logs."
129     exit 1
130   fi
131   echo DNS server restarted.
132 }
133
134 # main body of script.
135
136 if [[ $EUID != 0 ]]; then
137   echo "This script must be run as root or sudo."
138   exit 1
139 fi
140
141 new_domain="$1"; shift
142
143 if [ -z "$new_domain" ]; then
144   echo "This script needs a domain name to add to DNS." 
145   exit 1
146 fi
147
148 # if domain name has three or more components, then add a subdomain.
149 # otherwise, add a full new domain.
150 if [[ $new_domain == *"."*"."* ]]; then
151   # add a subdomain to the containing domain.
152   add_new_subdomain "$new_domain"
153   restart_bind
154 else
155   # create a totally new domain in DNS.
156   write_new_domain_file "$new_domain"
157   add_zone_for_new_domain "$new_domain"
158   restart_bind
159 fi
160
161