- bind_set_new_serial.sh, bind_set_renew_tlsa.sh: redesign code, add configuration file support

- Add 'script bind_rndc_sync_clean.sh'.
- bind_get_domain_by_hostname.sh: minor changes at code order.
- Adjust sample configuration file 'bind.conf.sample'.
This commit is contained in:
2017-08-03 23:39:28 +02:00
parent 3b0b759850
commit 5057d983bf
5 changed files with 817 additions and 142 deletions

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# -
# - Sets new serial and reloads zone
# -
# - Return (Exit) Codes:
@@ -18,78 +18,155 @@
# -
# - example: ./nd_set_new_serial.sh a.mx.open.de
# -
## -- Variable definitions
## --
#---------------------------------------
#-----------------------------
# Setting Defaults
#-----------------------------
#---------------------------------------
# - Bind configuration file containing zone definitions
# -
ZONE_CONF_FILE=/etc/bind/named.conf.local
DEFAULT_CONF_FILE_DIR="/etc/bind"
DEFAULT_BIND_USER="bind"
DEFAULT_BIND_GROUP="bind"
BIND_USER=bind
BIND_GROUP=bind
## --
## -- End: Variable definitions
#***************************************
#-----------------------------
# Don't make changes after this
#-----------------------------
#***************************************
## ***
## *** Don't make changes after this line ***
## ***
## --- some functions
## ---
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/bind.conf"
log_file="$(mktemp)"
#---------------------------------------
#-----------------------------
# Base Function(s)
#-----------------------------
#---------------------------------------
usage() {
echo
[ -n "$1" ] && echo -e "Error: $1\n"
cat<<EOF
Usage: $(basename $0) <hostname|domain> | <check>
Script increases the serial for a given domain or a given hostname concerning domain.
Parameter "check" can be used, to test whether this script is accessable (e.g. from a
further script on a remote host). Nothing will be done, scripts returns '0'.
Return (Exit) Codes:
success:
0: Serial is replaced and Zone is reloaded
error:
10: Invalid Hostname/Domain given
15: Hostname/Domain not supported
11: No zonefile found
12: Determin new Serial failed
13: Increasing Serial failed
14: Reloading Zone failed
99: Fatal error
Options:
-h
Prints this help.
-q
Rund in silent mode.
Example: $(basename $0) oopen.de
EOF
clean_up 1
}
clean_up() {
# Perform program exit housekeeping
rm $log_file
exit $1
}
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
if $verbose ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
fi
rm /tmp/shprompt$$
}
fatal(){
echo ""
echo -e "[ \033[31m\033[1mError\033[m ]: $*"
echo ""
echo -e "\t\033[31m\033[1mScript is canceled\033[m\033[m"
echo ""
exit 1
if $verbose ; then
echo ""
echo -e "[ \033[31m\033[1mError\033[m ]: $*"
echo ""
echo -e "\t\033[31m\033[1mScript is canceled\033[m\033[m"
echo ""
clean_up 99
fi
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
if $verbose ; then
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
fi
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
if $verbose ; then
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
fi
}
ok (){
echo ""
echo -e "\t[ \033[36m\033[1mOk\033[m ]: $*"
echo ""
if $verbose ; then
echo ""
echo -e "\t[ \033[36m\033[1mOk\033[m ]: $*"
echo ""
fi
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
if $verbose ; then
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
fi
}
echo_ok() {
echo -e "\033[75G[ \033[32mok\033[m ]"
if $verbose ; then
echo -e "\033[75G[ \033[32mok\033[m ]"
fi
}
echo_failed(){
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
if $verbose ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
if $verbose ; then
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
fi
}
containsElement () {
@@ -98,21 +175,99 @@ containsElement () {
return 1
}
## ---
## --- END: functions
trap clean_up SIGHUP SIGINT SIGTERM
# - Test whether stdout (file descriptor 1) is a terminal or not (e.g. cron
# - or if you pipe the output to some other program)
#
if [[ -t 1 ]] ; then
verbose=true
else
verbose=false
fi
while getopts hq opt ; do
case $opt in
q) verbose=false
;;
h) usage
;;
*)
;;
esac
done
shift $(expr $OPTIND - 1)
if [[ $# -ne 1 ]] ; then
if $verbose ; then
usage "wrong number of arguments"
else
clean_up 99
fi
fi
# - Parameter "check" can be used, to test whether this script
# - is accessable (e.g. from a script on a remote host)
# -
if [[ "$1" = "check" ]]; then
echo "\$1: $1"
exit 0
info "Script \033[1m$(basename $0)\033[m was successfully invoked, but its only a test."
clean_up 0
fi
host_name=$1
echo ""
$verbose && echo ""
if [[ -z "$host_name" ]] ; then
fatal "No hostname/domain given!"
fi
#---------------------------------------
#-----------------------------
# Load default values from bind.conf
#
# Overwrites the settings above
#
#-----------------------------
#---------------------------------------
if $verbose ; then
clear
echo ""
echo -e "\033[32mRunning script \033[1m"$(basename $0)"\033[m .."
echo ""
fi
info "Given hostname: \033[1m${host_name}\033[m"
echononl "\t Loading default Configuration values from $(basename ${conf_file}).."
if [[ ! -f "$conf_file" ]]; then
echo_skipped
else
source "${conf_file}" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
fi
[[ -n "$CONF_FILE_DIR" ]] || CONF_FILE_DIR="$DEFAULT_CONF_FILE_DIR"
[[ -n "$ZONES_DECLARATION_FILE" ]] || ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local"
[[ -n "$BIND_USER" ]] || BIND_USER="$DEFAULT_BIND_USER"
[[ -n "$BIND_GROUP" ]] || BIND_GROUP="$DEFAULT_BIND_GROUP"
$verbose && echo ""
# - Validate Syntax of given domain
@@ -122,15 +277,15 @@ echononl "\tValidate syntax of given domain.."
if [[ $host_name =~ $valid_domain_regex ]]; then
if [[ ! $host_name =~ \. ]]; then
echo_failed
error "Invalid hostname/domain \"$1\" given!"
exit 10
error "Invalid hostname/domain \"$host_name\" given!"
clean_up 10
else
echo_ok
fi
else
echo_failed
error "Invalid hostname/domain ($1) given!"
exit 10
error "Invalid hostname/domain \"$host_name\" given!"
clean_up 10
fi
@@ -139,7 +294,7 @@ fi
_failed=false
_host_name=$host_name
_tmp_host_name=$(echo ${_host_name//\./\\.})
while ! grep -e "$_tmp_host_name" $ZONE_CONF_FILE > /dev/null 2>&1 ; do
while ! grep -e "$_tmp_host_name" $ZONES_DECLARATION_FILE > /dev/null 2>&1 ; do
_host_name=${_host_name#*.}
_tmp_host_name=$(echo ${_host_name//\./\\.})
if [[ ! $_tmp_host_name =~ \. ]]; then
@@ -149,7 +304,7 @@ while ! grep -e "$_tmp_host_name" $ZONE_CONF_FILE > /dev/null 2>&1 ; do
done
if $_failed ; then
error "Given hostname/domain \"$1\" not supported by this nameserver!"
error "Given hostname/domain \"${_host_name}\" not supported by this nameserver!"
else
domain=$_host_name
fi
@@ -181,15 +336,15 @@ while IFS='' read -r line || [[ -n "$line" ]] ; do
break
fi
fi
done < $ZONE_CONF_FILE
zone_file_dir=`dirname $zone_file`
done < $ZONES_DECLARATION_FILE
if [[ $number -eq 0 ]] ; then
error "No Zonefile (master) found for domain \"$domain\" ."
exit 11
clean_up 11
fi
zone_file_dir=`dirname $zone_file`
echononl "\tBackup existing directory containg zonefiles.."
if [[ -d "$zone_file_dir" ]] ; then
@@ -198,13 +353,13 @@ if [[ -d "$zone_file_dir" ]] ; then
echo_ok
else
echo_failed
echo ""
exit 99
error "Backup directory 'zone_file_dir' containg zonefiles failed!"
clean_up 99
fi
else
echo_failed
error "Zonefile directory not found for domain \"$domain\" ."
exit 99
clean_up 99
fi
@@ -226,7 +381,7 @@ fi
if $_failed ; then
echo_failed
error "Determin Serial failed!"
exit 12
clean_up 12
else
echo_ok
fi
@@ -241,17 +396,17 @@ if [[ $? -eq 0 ]]; then
else
echo_failed
error "Increasing Serial failed!"
exit 13
clean_up 13
fi
echo ""
$verbose && echo ""
echononl "\tCorrect Owner for $zone_file .."
chown $BIND_USER:$BIND_GROUP $zone_file
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
exit 99
clean_up 99
fi
echononl "\tCorrect permissions on $zone_file .."
chmod 644 $zone_file
@@ -259,7 +414,7 @@ if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
exit 99
clean_up 99
fi
@@ -270,12 +425,12 @@ rndc reload $domain > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
info "Serial increased and zone reloaded ($domain)"
exit 0
clean_up 0
else
echo_failed
error "Increasing Serial failed!"
exit 13
clean_up 13
fi
echo
exit 99
$verbose && echo ""
clean_up 99