My solution is to block all ssh-access in hosts.deny with sshd: ALL
To allow the access from servers with a special (dynamic) domain I created a skript that turns a list of allowed domains in a list with IPs. This IP-list is included into the hosts.allow by adding following line
file: /etc/hosts.allow
sshd: /etc/ssh_dyn_allow/hosts_sshd.allow
The Source of the script looks like this:
file: /etc/ssh_dyn_allow/renew_allowed_ip_list.sh
#!/bin/bash
SCRIPT=$(readlink -f "$0")
WORKDIR=$(dirname "$SCRIPT")
ALLOWFILE=$WORKDIR/hosts_sshd.allow
DOMAINFILE=$WORKDIR/allowed_domain.list
DOMAINS=$(cat $DOMAINFILE |grep -v "^#")
echo "# automatic generated : $(date)" > $ALLOWFILE
for DOMAIN in $DOMAINS
do
echo ""
IP=$(dig $DOMAIN A | grep "^$DOMAIN" | sed -e 's/\s\s*/ /g' | cut -d " " -f5)
echo "# $DOMAIN"
echo $IP
echo
done >> $ALLOWFILE
The script is executed with a cronjob each 15 minutes.
The file with the allowed domains is text file with one domain per line looks like this
file: /etc/ssh_dyn_allow/allowed_domain.list
# domainlist
# one domain per line
user1.ddnss.org
user2.ddnss.org
# fired_user.ddnss.org
# our other servers
web1.example.com
web2.example.com
Advantages:
- more than one (dynamic) domain can be handled (maybe for a colleague or other servers )
- the original hosts.allow cannot be destroyed by an erroneous script manipulating the file.