2023-01-28 21:26:34 +01:00

244 lines
5.1 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
# cree les fichiers de configuration bind9
# - ficher de zone directe
# - ficher de zone inverse
#
version="1.1"
zone="domaine.lan"
slave=0
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# script cleanup here
}
initialize () {
readonly zonenet="192.168.56"
readonly zonerev="56.168.192.in-addr.arpa"
# NS1
readonly nsname="srv1"
readonly nsip="${zonenet}.10"
readonly nsiprev=$(echo ${nsip}|cut -d. -f4) # dernier octet pour classe C
# NS2
readonly nsname2="srv2"
readonly nsip2="${zonenet}.11"
readonly nsiprev2=$(echo ${nsip2}|cut -d. -f4)
readonly ttl="86400"
# Global
readonly nsfqdn="${nsname}.${zone}"
readonly nsfqdnp="${nsfqdn}."
readonly zonep="${zone}."
readonly date=$(date +%Y%m%d00)
}
mkconflocal () {
if [[ "${slave}" != 1 ]] ; then
echo "Generation fichier named.conf.local ..."
cat <<EOT > "named.conf.local"
# fichier zone ${zone}
# le $(date)
zone "${zone}" {
type master;
file "/etc/bind/db.${zone}"; # zone directe
};
zone "${zonerev}" {
type master;
};
EOT
else
echo "Generation fichier named.conf.local ..."
cat <<EOT > "named.conf.local"
# fichier zone ${zone}
# le $(date)
zone "${zone}" {
type slave;
file "/var/cache/bind/db.${zone}"; # zone directe
masters { ${nsip} ; };
};
zone "${zonerev}" {
type slave;
file "/var/cache/bind/db.${zone}.rev"; # zone directe
masters { ${nsip} ; };
};
EOT
fi
}
mkzdirrect () {
# fichier de zone directe
echo "Generation fichier de zone directe db.${zone} ..."
cat <<EOT > "db.${zone}"
; fichier zone ${zone}
; le $(date)
\$TTL ${ttl} ; (1 day)
\$ORIGIN ${zonep}
@ IN SOA ${nsfqdnp} root.${nsfqdnp} (
${date} ; serial YYYYMMDDnn
14400 ; refresh (4 hours)
1800 ; retry (30 minutes)
1209600 ; expire (2 weeks)
3600 ; minimum (1 hour)
)
@ IN NS ${nsname}
@ IN NS ${nsname2}
IN A 203.0.113.10 ; zone
IN AAAA 2001:DB8:BEEF:113::10 ;
www IN CNAME ${nsfqdnp}
ftp IN CNAME ${nsfqdnp}
${nsname} IN A ${nsip}
IN AAAA 2001:DB8:BEEF:2::22
${nsname2} IN A ${nsip2}
IN AAAA 2001:DB8:BEEF:100::22
EOT
}
mkzreverse () {
echo "Generation fichier de zone inverse db.${zone}.rev ..."
cat <<EOT > "db.${zone}.rev"
; fichier zone inverse ${zone}
; le $(date)
\$TTL ${ttl} ; (1 day)
@ IN SOA ${nsfqdnp} root.${nsfqdnp} (
${date} ; serial YYYYMMDDnn
14400 ; refresh (4 hours)
1800 ; retry (30 minutes)
1209600 ; expire (2 weeks)
3600 ; minimum (1 hour)
)
@ IN NS ${nsname}.${zone}.
@ IN NS ${nsname2}.${zone}.
${nsiprev} IN PTR ${nsname}.${zone}.
${nsiprev2} IN PTR ${nsname2}.${zone}.
EOT
}
mkresolv () {
echo "Generation fichier /etc/resolv.conf ..."
cat <<EOT > "/etc/resolv.conf"
# fichier resolv.conf ${zone}
# le $(date)
domain ${zone}
search ${zone}
nameserver 127.0.0.1
EOT
}
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat <<EOF # remove the space between << and EOF, this is due to web plugin issue
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-f] -p param_value zone [arg2...]
Script description here.
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-s, --slave Some flag description
-p, --param Some param description
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# script cleanup here
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
parse_params() {
# default values of variables set from params
flag=0
param=''
while :; do
case "${1-}" in
-h | --help) usage ;;
--no-color) NO_COLOR=1 ;;
-s | --slave) flag=1 ;; # example flag
# -p | --param) # example named parameter
# param="${2-}"
# shift
# ;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
args=("$@")
# check required params and arguments
# [[ -z "${param-}" ]] && die "Missing required parameter: param"
[[ ${#args[@]} -eq 0 ]] && die "Missing script arguments"
return 0
}
parse_params "$@"
setup_colors
# script logic here
#msg "${RED}Read parameters:${NOFORMAT}"
#msg "- flag: ${flag}"
#msg "- param: ${param}"
#msg "- arguments: ${args[*]-}"
zone="${args[0]-}"
slave="${flag}"
initialize
if [[ ${flag} != 1 ]] ; then
mkconflocal
mkzdirect
mkzreverse
mkresolv
else
mkconflocal
mkresolv
fi
exit 0