From 6c092ddd89f370977ea96dd63165ba56693fb7f4 Mon Sep 17 00:00:00 2001 From: Chris Koeritz Date: Tue, 14 Nov 2017 22:12:44 -0500 Subject: [PATCH] closer to remove domain working --- scripts/system/add_domain.sh | 133 ++---------------------------- scripts/system/common_sysadmin.sh | 131 +++++++++++++++++++++++++++++ scripts/system/remove_domain.sh | 66 +++++++++++++++ 3 files changed, 205 insertions(+), 125 deletions(-) create mode 100644 scripts/system/common_sysadmin.sh create mode 100644 scripts/system/remove_domain.sh diff --git a/scripts/system/add_domain.sh b/scripts/system/add_domain.sh index 9a524f4e..e12faa7b 100644 --- a/scripts/system/add_domain.sh +++ b/scripts/system/add_domain.sh @@ -1,10 +1,12 @@ #!/bin/bash -# this set of functions serve the main purpose of adding new domains or subdomains to the bind9 DNS server on the current host. -# it is currently highly specific to running a bunch of domains on a linux VM, where the VM has one IP address. -# note that bind 'named' must already be configured. -# also, it is assumed that if a subdomain is being added, then the containing domain has already been configured and is -# configured in a file similar to "blah.com.conf" in /etc/bind. +# this set of functions serve the main purpose of adding new domains or +# subdomains to the bind9 DNS server on the current host. it is currently +# highly specific to running a bunch of domains on a linux VM, where the VM +# has one IP address. note that the bind 'named' must already be configured. +# also, it is assumed that, if a subdomain is being added, then the containing +# domain has already been configured and is configured in a file similar to +# "blah.com.conf" in /etc/bind. # # Author: Chris Koeritz @@ -12,6 +14,7 @@ export WORKDIR="$( \cd "$(\dirname "$0")" && \pwd )" # obtain the script's work export FEISTY_MEOW_APEX="$( \cd "$WORKDIR/../.." && \pwd )" source "$FEISTY_MEOW_APEX/scripts/core/launch_feisty_meow.sh" +source "$FEISTY_MEOW_SCRIPTS/system/common_sysadmin.sh" # some defaults that are convenient for current purposes. # existing values will be respected over our defaults. @@ -37,126 +40,6 @@ if [ -z "$DISTRO" ]; then DISTRO="ubuntu" fi -# creates a totally new domain config file for DNS. -function write_new_domain_file() -{ - local domain_name="$1"; shift - - local domain_file="/etc/bind/${domain_name}.conf" - - echo "adding a totally new domain called $domain_name" - echo "using the config file: $domain_file" - - if [ -f $domain_file ]; then - echo - echo "The domain configuration file already exists at:" - echo " $domain_file" - echo "Since we don't want to tear that down if it has specialized configuration" - echo "data in it, we will just leave it in place and consider our job done." - echo - exit 0 - fi - - echo " -\$TTL 1W -@ IN SOA @ ${SERVER_ADMIN}. ( - 2017100801 ; serial - 2H ; refresh - 8M ; retry - 14D ; expiry - 6H ) ; minimum - - IN NS ${MAIN_NAME_SERVER}. - IN MX 10 ${MAIL_SERVER}. - -${domain_name}. IN A ${IP_ADDRESS} - IN HINFO \"linux server\" \"${DISTRO}\" -" >"$domain_file" - - # our personalized configuration approach wants the real owner to own the file. - chown "$(logname):$(logname)" $domain_file - test_or_die "setting ownership on: $domain_file" -} - -# hooks up a new config file into bind's list of zones. -function add_zone_for_new_domain() -{ - local domain_name="$1"; shift - - local domain_file="/etc/bind/${domain_name}.conf" - - echo "adding a new domain configured by ${domain_file} into" - echo "the named.conf.local configuration file." - - # append the reference to the new conf file in the zone list. - echo " -zone \"${domain_name}\" in { - file \"${domain_file}\"; - type master; - allow-query { any; }; -}; - -//////////////////////////////////////////////////////////////////////////// - -" >> /etc/bind/named.conf.local - - # keep ownership for the real user. - chown "$(logname):$(logname)" /etc/bind/named.conf.local - test_or_die "setting ownership on: /etc/bind/named.conf.local" - -} - -# adds a new subdomain under a containing domain. -function add_new_subdomain() -{ - local new_domain="$1"; shift - - # split up the full domain name into subdomain portion and containing domain. - local subdomain="${new_domain%.*.*}" - local containing_domain="${new_domain#*.}" - - echo "adding a subdomain $subdomain to containing domain $containing_domain" - - local domain_file="/etc/bind/${containing_domain}.conf" - # see if config file already exists; if not, complain. - if [ ! -f "$domain_file" ]; then - echo "The domain configuration file for $new_domain is missing." - echo "It should already be present in: $domain_file" - echo "Please add the containing domain before trying to add a subdomain." - exit 1 - fi - - # see if subdomain already present in config. - if [ $(grep -q "$new_domain" "$domain_file") ]; then - echo "The subdomain $subdomain already seems to exist in the domain" - echo "configuration file: $domain_file" - echo "Please edit the config file to remove the subdomain before trying" - echo "to re-add the subdomain." - exit 1 - fi - - # append the new subdomain into the config file. - echo " -${subdomain}.${containing_domain}. IN A ${IP_ADDRESS} - IN HINFO \"linux server\" \"${DISTRO}\" -" >> /etc/bind/${containing_domain}.conf - - # keep ownership for real user. - chown "$(logname):$(logname)" "/etc/bind/${containing_domain}.conf" - test_or_die "setting ownership on: /etc/bind/${containing_domain}.conf" -} - -function restart_bind() -{ - echo restarting DNS server. - service bind9 restart - if [ $? -ne 0 ]; then - echo "The bind service did not restart properly. Please check the error logs." - exit 1 - fi - echo DNS server restarted. -} - # main body of script. if [[ $EUID != 0 ]]; then diff --git a/scripts/system/common_sysadmin.sh b/scripts/system/common_sysadmin.sh new file mode 100644 index 00000000..7ca026ad --- /dev/null +++ b/scripts/system/common_sysadmin.sh @@ -0,0 +1,131 @@ +#!/bin/bash + +# this is a library of functions shared by scripts in the system folder. +# +# Author: Chris Koeritz + +#export WORKDIR="$( \cd "$(\dirname "$0")" && \pwd )" # obtain the script's working directory. +#export FEISTY_MEOW_APEX="$( \cd "$WORKDIR/../.." && \pwd )" + +#source "$FEISTY_MEOW_APEX/scripts/core/launch_feisty_meow.sh" + +# creates a totally new domain config file for DNS. +function write_new_domain_file() +{ + local domain_name="$1"; shift + + local domain_file="/etc/bind/${domain_name}.conf" + + echo "adding a totally new domain called $domain_name" + echo "using the config file: $domain_file" + + if [ -f $domain_file ]; then + echo + echo "The domain configuration file already exists at:" + echo " $domain_file" + echo "Since we don't want to tear that down if it has specialized configuration" + echo "data in it, we will just leave it in place and consider our job done." + echo + exit 0 + fi + + echo " +\$TTL 1W +@ IN SOA @ ${SERVER_ADMIN}. ( + 2017100801 ; serial + 2H ; refresh + 8M ; retry + 14D ; expiry + 6H ) ; minimum + + IN NS ${MAIN_NAME_SERVER}. + IN MX 10 ${MAIL_SERVER}. + +${domain_name}. IN A ${IP_ADDRESS} + IN HINFO \"linux server\" \"${DISTRO}\" +" >"$domain_file" + + # our personalized configuration approach wants the real owner to own the file. + chown "$(logname):$(logname)" $domain_file + test_or_die "setting ownership on: $domain_file" +} + +# hooks up a new config file into bind's list of zones. +function add_zone_for_new_domain() +{ + local domain_name="$1"; shift + + local domain_file="/etc/bind/${domain_name}.conf" + + echo "adding a new domain configured by ${domain_file} into" + echo "the named.conf.local configuration file." + + # append the reference to the new conf file in the zone list. + echo " +zone \"${domain_name}\" in { + file \"${domain_file}\"; + type master; + allow-query { any; }; +}; + +//////////////////////////////////////////////////////////////////////////// + +" >> /etc/bind/named.conf.local + + # keep ownership for the real user. + chown "$(logname):$(logname)" /etc/bind/named.conf.local + test_or_die "setting ownership on: /etc/bind/named.conf.local" + +} + +# adds a new subdomain under a containing domain. +function add_new_subdomain() +{ + local new_domain="$1"; shift + + # split up the full domain name into subdomain portion and containing domain. + local subdomain="${new_domain%.*.*}" + local containing_domain="${new_domain#*.}" + + echo "adding a subdomain $subdomain to containing domain $containing_domain" + + local domain_file="/etc/bind/${containing_domain}.conf" + # see if config file already exists; if not, complain. + if [ ! -f "$domain_file" ]; then + echo "The domain configuration file for $new_domain is missing." + echo "It should already be present in: $domain_file" + echo "Please add the containing domain before trying to add a subdomain." + exit 1 + fi + + # see if subdomain already present in config. + if [ $(grep -q "$new_domain" "$domain_file") ]; then + echo "The subdomain $subdomain already seems to exist in the domain" + echo "configuration file: $domain_file" + echo "Please edit the config file to remove the subdomain before trying" + echo "to re-add the subdomain." + exit 1 + fi + + # append the new subdomain into the config file. + echo " +${subdomain}.${containing_domain}. IN A ${IP_ADDRESS} + IN HINFO \"linux server\" \"${DISTRO}\" +" >> /etc/bind/${containing_domain}.conf + + # keep ownership for real user. + chown "$(logname):$(logname)" "/etc/bind/${containing_domain}.conf" + test_or_die "setting ownership on: /etc/bind/${containing_domain}.conf" +} + +function restart_bind() +{ + echo restarting DNS server. + service bind9 restart + if [ $? -ne 0 ]; then + echo "The bind service did not restart properly. Please check the error logs." + exit 1 + fi + echo DNS server restarted. +} + diff --git a/scripts/system/remove_domain.sh b/scripts/system/remove_domain.sh new file mode 100644 index 00000000..f734878a --- /dev/null +++ b/scripts/system/remove_domain.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +# performs the inverse function of add_domain by deconfiguring a domain +# in bind. the domain needs to have been set up by add_domain, or this will +# not succeed. +# +# Author: Chris Koeritz + +export WORKDIR="$( \cd "$(\dirname "$0")" && \pwd )" # obtain the script's working directory. +export FEISTY_MEOW_APEX="$( \cd "$WORKDIR/../.." && \pwd )" + +source "$FEISTY_MEOW_APEX/scripts/core/launch_feisty_meow.sh" +source "$FEISTY_MEOW_SCRIPTS/system/common_sysadmin.sh" + +# some defaults that are convenient for current purposes. +# existing values will be respected over our defaults. + +#if [ -z "$IP_ADDRESS" ]; then +# # in our scheme, the single IP address that all our domains map to. +# IP_ADDRESS="10.28.42.20" +#fi +#if [ -z "$SERVER_ADMIN" ]; then +# # the email address (where first dot is replaced by @) for the administrator of the domain. +# SERVER_ADMIN="developer.cakelampvm.com" +#fi +#if [ -z "$MAIN_NAME_SERVER" ]; then +# # the name of the name server for the new domains (should already be configured). +# MAIN_NAME_SERVER="ns.cakelampvm.com" +#fi +#if [ -z "$MAIL_SERVER" ]; then +# # the name of the mail server for a new domain (should already be configured). +# MAIL_SERVER="mail.cakelampvm.com" +#fi +#if [ -z "$DISTRO" ]; then +# # the distribution name to be listed in info for the new domain or subdomain. +# DISTRO="ubuntu" +#fi + +# main body of script. + +if [[ $EUID != 0 ]]; then + echo "This script must be run as root or sudo." + exit 1 +fi + +old_domain="$1"; shift + +if [ -z "$old_domain" ]; then + echo "This script needs a domain name to remove from DNS." + exit 1 +fi + +# if domain name has three or more components, then remove a subdomain. +# otherwise, remove a full domain. +if [[ $old_domain == *"."*"."* ]]; then + # remove a subdomain from the containing domain. + remove_subdomain "$old_domain" + restart_bind +else + # remove the full domain in DNS. + remove_domain_file "$old_domain" + remove_zone_for_domain "$old_domain" + restart_bind +fi + + -- 2.34.1