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