Imported Upstream version 2.3
[ossec-hids.git] / active-response / host-deny.sh
1 #!/bin/sh
2 # Adds an IP to the /etc/hosts.deny file
3 # Requirements: sshd and other binaries with tcp wrappers support
4 # Expect: srcip
5 # Author: Daniel B. Cid
6 # Last modified: Nov 09, 2005
7
8 ACTION=$1
9 USER=$2
10 IP=$3
11
12 LOCAL=`dirname $0`;
13 cd $LOCAL
14 cd ../
15 PWD=`pwd`
16 LOCK="${PWD}/host-deny-lock"
17 LOCK_PID="${PWD}/host-deny-lock/pid"
18 UNAME=`uname`
19
20
21 # This number should be more than enough (even if a hundred
22 # instances of this script is ran together). If you have
23 # a really loaded env, you can increase it to 75 or 100.
24 MAX_ITERATION="50"
25
26
27 # Lock function
28 lock()
29 {
30     i=0;
31     # Providing a lock.
32     while [ 1 ]; do
33         mkdir ${LOCK} > /dev/null 2>&1
34         MSL=$?
35         if [ "${MSL}" = "0" ]; then
36             # Lock aquired (setting the pid)
37             echo "$$" > ${LOCK_PID}
38             return;
39         fi
40
41         # Getting currently/saved PID locking the file
42         C_PID=`cat ${LOCK_PID} 2>/dev/null`
43         if [ "x" = "x${S_PID}" ]; then
44             S_PID=${C_PID}
45         fi    
46
47         # Breaking out of the loop after X attempts
48         if [ "x${C_PID}" = "x${S_PID}" ]; then
49             i=`expr $i + 1`;
50         fi
51    
52         # Sleep 1 after 10/25 interactions
53         if [ "$i" = "10" -o "$i" = "25" ]; then
54             sleep 1;
55         fi
56              
57         i=`expr $i + 1`;
58         
59         # So i increments 2 by 2 if the pid does not change.
60         # If the pid keeps changing, we will increments one
61         # by one and fail after MAX_ITERACTION
62         if [ "$i" = "${MAX_ITERATION}" ]; then
63             echo "`date` Unable to execute. Locked: $0" \
64                         >> ${PWD}/ossec-hids-responses.log
65             
66             # Unlocking and exiting
67             unlock;
68             exit 1;                
69         fi
70     done
71 }
72
73 # Unlock function
74 unlock()
75 {
76    rm -rf ${LOCK} 
77 }
78
79
80 # Logging the call
81 echo "`date` $0 $1 $2 $3 $4 $5" >> ${PWD}/../logs/active-responses.log
82
83
84 # IP Address must be provided
85 if [ "x${IP}" = "x" ]; then
86    echo "$0: Missing argument <action> <user> (ip)" 
87    exit 1;
88 fi
89
90
91 # Checking for invalid entries (lacking ".", etc)
92 echo "${IP}" | grep "\." > /dev/null 2>&1
93 if [ ! $? = 0 ]; then
94     echo "`date` Invalid ip/hostname entry: ${IP}" >> ${PWD}/../logs/active-responses.log
95     exit 1;
96 fi
97
98
99 # Adding the ip to hosts.deny
100 if [ "x${ACTION}" = "xadd" ]; then
101    lock;     
102    if [ "X$UNAME" = "XFreeBSD" ]; then
103     echo "ALL : ${IP} : deny" >> /etc/hosts.allow
104    else    
105     echo "ALL:${IP}" >> /etc/hosts.deny
106    fi 
107    unlock;
108    exit 0;
109
110
111 # Deleting from hosts.deny   
112 elif [ "x${ACTION}" = "xdelete" ]; then   
113    lock;
114    if [ "X$UNAME" = "XFreeBSD" ]; then
115     cat /etc/hosts.allow | grep -v "ALL : ${IP} : deny$"> /tmp/hosts.deny.$$
116     mv /tmp/hosts.deny.$$ /etc/hosts.allow
117    else
118     cat /etc/hosts.deny | grep -v "ALL:${IP}$"> /tmp/hosts.deny.$$
119     cat /tmp/hosts.deny.$$ > /etc/hosts.deny
120     rm /tmp/hosts.deny.$$
121    fi 
122    unlock;
123    exit 0;
124
125
126 # Invalid action   
127 else
128    echo "$0: invalid action: ${ACTION}"
129 fi       
130
131 exit 1;