new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / debian / ossec-hids / var / ossec / bin / ossec-client.sh
1 #!/bin/sh
2 # ossec-control        This shell script takes care of starting
3 #                      or stopping ossec-hids
4 # Author: Daniel B. Cid <daniel.cid@gmail.com>
5
6 LOCAL=`dirname $0`;
7 cd ${LOCAL}
8 PWD=`pwd`
9 DIR=`dirname $PWD`;
10
11
12 ###  Do not modify below here ###
13 NAME="OSSEC HIDS"
14 VERSION="v3.3.0"
15 DAEMONS="ossec-logcollector ossec-syscheckd ossec-agentd ossec-execd"
16
17 [ -f /etc/ossec-init.conf ] && . /etc/ossec-init.conf
18
19 ## Locking for the start/stop
20 LOCK="${DIR}/var/start-script-lock"
21 LOCK_PID="${LOCK}/pid"
22
23 # This number should be more than enough (even if it is
24 # started multiple times together). It will try for up
25 # to 10 attempts (or 10 seconds) to execute.
26 MAX_ITERATION="10"
27
28 checkpid()
29 {
30     for i in ${DAEMONS}; do
31         for j in `cat ${DIR}/var/run/${i}*.pid 2>/dev/null`; do
32             ps -p $j |grep ossec >/dev/null 2>&1
33             if [ ! $? = 0 ]; then
34                 echo "Deleting PID file '${DIR}/var/run/${i}-${j}.pid' not used..."
35                 rm ${DIR}/var/run/${i}-${j}.pid
36             fi
37         done
38     done
39 }
40
41 lock()
42 {
43     i=0;
44
45     # Providing a lock.
46     while [ 1 ]; do
47         mkdir ${LOCK} > /dev/null 2>&1
48         MSL=$?
49         if [ "${MSL}" = "0" ]; then
50             # Lock acquired (setting the pid)
51             echo "$$" > ${LOCK_PID}
52             return;
53         fi
54
55         # Waiting 1 second before trying again
56         sleep 1;
57         i=`expr $i + 1`;
58
59         # If PID is not present, speed things a bit.
60         kill -0 `cat ${LOCK_PID}` >/dev/null 2>&1
61         if [ ! $? = 0 ]; then
62             # Pid is not present.
63             i=`expr $i + 1`;
64         fi
65
66         # We tried 10 times to acquire the lock.
67         if [ "$i" = "${MAX_ITERATION}" ]; then
68             # Unlocking and executing
69             unlock;
70             mkdir ${LOCK} > /dev/null 2>&1
71             echo "$$" > ${LOCK_PID}
72             return;
73         fi
74     done
75 }
76
77 unlock()
78 {
79     rm -rf ${LOCK}
80 }
81
82 help()
83 {
84     # Help message
85     echo "Usage: $0 {start|stop|reload|restart|status}";
86     exit 1;
87 }
88
89 status()
90 {
91     RETVAL=0
92     for i in ${DAEMONS}; do
93         pstatus ${i};
94         if [ $? = 0 ]; then
95             RETVAL=1
96             echo "${i} not running..."
97         else
98             echo "${i} is running..."
99         fi
100     done
101     exit $RETVAL
102 }
103
104 testconfig()
105 {
106     # We first loop to check the config.
107     for i in ${SDAEMONS}; do
108         ${DIR}/bin/${i} -t;
109         if [ $? != 0 ]; then
110             echo "${i}: Configuration error. Exiting"
111             unlock;
112             exit 1;
113         fi
114     done
115 }
116
117 # Start function
118 start()
119 {
120     SDAEMONS="ossec-execd ossec-agentd ossec-logcollector ossec-syscheckd"
121
122     echo "Starting $NAME $VERSION..."
123     lock;
124     checkpid;
125
126     # We actually start them now.
127     for i in ${SDAEMONS}; do
128         pstatus ${i};
129         if [ $? = 0 ]; then
130             ${DIR}/bin/${i};
131             if [ $? != 0 ]; then
132                 echo "${i} did not start";
133                 unlock;
134                 exit 1;
135             fi
136
137             echo "Started ${i}..."
138         else
139             echo "${i} already running..."
140         fi
141     done
142
143     # After we start we give 2 seconds for the daemons
144     # to internally create their PID files.
145     sleep 2;
146     unlock;
147     echo "Completed."
148 }
149
150 pstatus()
151 {
152     pfile=$1;
153
154     # pfile must be set
155     if [ "X${pfile}" = "X" ]; then
156         return 0;
157     fi
158
159     ls ${DIR}/var/run/${pfile}*.pid > /dev/null 2>&1
160     if [ $? = 0 ]; then
161         for j in `cat ${DIR}/var/run/${pfile}*.pid 2>/dev/null`; do
162             ps -p $j |grep ossec >/dev/null 2>&1
163             if [ ! $? = 0 ]; then
164                 echo "${pfile}: Process $j not used by ossec, removing .."
165                 rm -f ${DIR}/var/run/${pfile}-$j.pid
166                 continue;
167             fi
168
169             kill -0 $j > /dev/null 2>&1
170             if [ $? = 0 ]; then
171                 return 1;
172             fi
173         done
174     fi
175
176     return 0;
177 }
178
179 stopa()
180 {
181     lock;
182     checkpid;
183     for i in ${DAEMONS}; do
184         pstatus ${i};
185         if [ $? = 1 ]; then
186             echo "Killing ${i} .. ";
187
188             kill `cat ${DIR}/var/run/${i}*.pid`;
189         else
190             echo "${i} not running ..";
191         fi
192
193         rm -f ${DIR}/var/run/${i}*.pid
194      done
195
196     unlock;
197     echo "$NAME $VERSION Stopped"
198 }
199
200 ### MAIN HERE ###
201
202 case "$1" in
203 start)
204     testconfig
205     start
206     ;;
207 stop)
208     stopa
209     ;;
210 restart)
211     testconfig
212     stopa
213     sleep 1;
214     start
215     ;;
216 reload)
217     DAEMONS="ossec-logcollector ossec-syscheckd ossec-agentd"
218     stopa
219     start
220     ;;
221 status)
222     status
223     ;;
224 help)
225     help
226     ;;
227 *)
228     help
229 esac
230