- lock;
- if [ "X$UNAME" = "XFreeBSD" ]; then
- echo "ALL : ${IP} : deny" >> /etc/hosts.allow
- else
- echo "ALL:${IP}" >> /etc/hosts.deny
- fi
- unlock;
- exit 0;
-
-
-# Deleting from hosts.deny
-elif [ "x${ACTION}" = "xdelete" ]; then
- lock;
- TMP_FILE = `mktemp /var/ossec/ossec-hosts.XXXXXXXXXX`
- if [ "X${TMP_FILE}" = "X" ]; then
- # Cheap fake tmpfile, but should be harder then no random data
- TMP_FILE = "/var/ossec/ossec-hosts.`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -1 `"
- fi
- if [ "X$UNAME" = "XFreeBSD" ]; then
- cat /etc/hosts.allow | grep -v "ALL : ${IP} : deny$"> ${TMP_FILE}
- mv ${TMP_FILE} /etc/hosts.allow
- else
- cat /etc/hosts.deny | grep -v "ALL:${IP}$"> ${TMP_FILE}
- cat ${TMP_FILE} > /etc/hosts.deny
- rm ${TMP_FILE}
- fi
- unlock;
- exit 0;
-
-
-# Invalid action
+ # Looking for duplication
+ IPKEY=$(grep -w "${IP}" /etc/hosts.deny)
+ if [ ! -z "$IPKEY" ]; then
+ echo "IP ${IP} already exists on host.deny..." >> ${PWD}/../logs/active-responses.log
+ exit 1
+ fi
+ lock;
+ echo "${IP}" | grep "\:" > /dev/null 2>&1
+ if [ $? = 0 ]; then
+ IP="[${IP}]"
+ fi
+ if [ "X$UNAME" = "XFreeBSD" ]; then
+ echo "ALL : ${IP} : deny" >> /etc/hosts.allow
+ else
+ echo "ALL:${IP}" >> /etc/hosts.deny
+ fi
+ unlock;
+ exit 0;
+
+
+# Deleting from hosts.deny
+elif [ "x${ACTION}" = "xdelete" ]; then
+ lock;
+ TMP_FILE=`mktemp ${PWD}/ossec-hosts.XXXXXXXXXX`
+ if [ "X${TMP_FILE}" = "X" ]; then
+ # Cheap fake tmpfile, but should be harder then no random data
+ TMP_FILE="${PWD}/ossec-hosts.`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -1 `"
+ fi
+ echo "${IP}" | grep "\:" > /dev/null 2>&1
+ if [ $? = 0 ]; then
+ IP="\[${IP}\]"
+ fi
+ if [ "X$UNAME" = "XFreeBSD" ]; then
+ cat /etc/hosts.allow | grep -v "ALL : ${IP} : deny$"> ${TMP_FILE}
+ mv ${TMP_FILE} /etc/hosts.allow
+ else
+ cat /etc/hosts.deny | grep -v "ALL:${IP}$"> ${TMP_FILE}
+ cat ${TMP_FILE} > /etc/hosts.deny
+ rm ${TMP_FILE}
+ fi
+ unlock;
+ exit 0;
+
+
+# Invalid action