Imported Upstream version 2.3
[ossec-hids.git] / src / init / ossec-local.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
7 # Getting where we are installed
8 LOCAL=`dirname $0`;
9 cd ${LOCAL}
10 PWD=`pwd`
11 DIR=`dirname $PWD`;
12 PLIST=${DIR}/bin/.process_list;
13
14
15 ###  Do not modify bellow here ###
16
17 # Getting additional processes
18 ls -la ${PLIST} > /dev/null 2>&1
19 if [ $? = 0 ]; then
20 . ${PLIST};
21 fi
22
23
24 NAME="OSSEC HIDS"
25 VERSION="v2.3"
26 AUTHOR="Trend Micro Inc."
27 DAEMONS="ossec-monitord ossec-logcollector ossec-syscheckd ossec-analysisd ossec-maild ossec-execd ${DB_DAEMON} ${CSYSLOG_DAEMON} ${AGENTLESS_DAEMON}"
28
29
30 ## Locking for the start/stop
31 LOCK="${DIR}/var/start-script-lock"
32 LOCK_PID="${LOCK}/pid"
33
34
35 # This number should be more than enough (even if it is
36 # started multiple times together). It will try for up
37 # to 10 attempts (or 10 seconds) to execute.
38 MAX_ITERATION="10"
39
40
41
42 # Check pid
43 checkpid()
44 {
45     for i in ${DAEMONS}; do
46         for j in `cat ${DIR}/var/run/${i}*.pid 2>/dev/null`; do
47             ps -p $j |grep ossec >/dev/null 2>&1
48             if [ ! $? = 0 ]; then
49                 echo "Deleting PID file '${DIR}/var/run/${i}-${j}.pid' not used..."
50                 rm ${DIR}/var/run/${i}-${j}.pid
51             fi    
52         done    
53     done    
54 }
55
56
57
58 # Lock function
59 lock()
60 {
61     i=0;
62     
63     # Providing a lock.
64     while [ 1 ]; do
65         mkdir ${LOCK} > /dev/null 2>&1
66         MSL=$?
67         if [ "${MSL}" = "0" ]; then
68             # Lock aquired (setting the pid)
69             echo "$$" > ${LOCK_PID}
70             return;
71         fi
72
73         # Waiting 1 second before trying again
74         sleep 1;
75         i=`expr $i + 1`;
76
77         # If PID is not present, speed things a bit.
78         kill -0 `cat ${LOCK_PID}` >/dev/null 2>&1
79         if [ ! $? = 0 ]; then
80             # Pid is not present.
81             i=`expr $i + 1`;
82         fi    
83
84         # We tried 10 times to acquire the lock.
85         if [ "$i" = "${MAX_ITERATION}" ]; then
86             # Unlocking and executing
87             unlock;
88             mkdir ${LOCK} > /dev/null 2>&1
89             echo "$$" > ${LOCK_PID}
90             return;
91         fi
92     done
93 }
94
95
96 # Unlock function
97 unlock()
98 {
99     rm -rf ${LOCK}
100 }
101
102     
103 # Help message
104 help()
105 {
106     # Help message
107     echo ""
108     echo "Usage: $0 {start|stop|restart|status|enable|disable}";
109     exit 1;
110 }
111
112
113 # Enables/disables additional daemons
114 enable()
115 {
116     if [ "X$2" = "X" ]; then
117         echo ""
118         echo "Enable options: database, client-syslog, agentless"
119         echo "Usage: $0 enable [database|client-syslog|agentless]"
120         exit 1;
121     fi
122     
123     if [ "X$2" = "Xdatabase" ]; then
124         echo "DB_DAEMON=ossec-dbd" >> ${PLIST};
125     elif [ "X$2" = "Xclient-syslog" ]; then
126         echo "CSYSLOG_DAEMON=ossec-csyslogd" >> ${PLIST};
127     elif [ "X$2" = "Xagentless" ]; then
128         echo "AGENTLESS_DAEMON=ossec-agentlessd" >> ${PLIST};    
129     else
130         echo ""
131         echo "Invalid enable option."
132         echo ""
133         echo "Enable options: database, client-syslog, agentless"
134         echo "Usage: $0 enable [database|client-syslog|agentless]"
135         exit 1;
136     fi         
137
138     
139 }
140
141
142
143 # Enables/disables additional daemons
144 disable()
145 {
146     if [ "X$2" = "X" ]; then
147         echo ""
148         echo "Disable options: database, client-syslog, agentless"
149         echo "Usage: $0 disable [database|client-syslog|agentless]"
150         exit 1;
151     fi
152     
153     if [ "X$2" = "Xdatabase" ]; then
154         echo "DB_DAEMON=\"\"" >> ${PLIST};
155     elif [ "X$2" = "Xclient-syslog" ]; then
156         echo "CSYSLOG_DAEMON=\"\"" >> ${PLIST};
157     elif [ "X$2" = "Xagentless" ]; then
158         echo "AGENTLESS_DAEMON=\"\"" >> ${PLIST};    
159     else
160         echo ""
161         echo "Invalid disable option."
162         echo ""
163         echo "Disable options: database, client-syslog, agentless"
164         echo "Usage: $0 disable [database|client-syslog|agentless]"
165         exit 1;
166     fi         
167
168     
169 }
170
171
172
173 # Status function
174 status()
175 {
176     for i in ${DAEMONS}; do
177         pstatus ${i};
178         if [ $? = 0 ]; then
179             echo "${i} not running..."
180         else
181             echo "${i} is running..."
182         fi
183     done             
184 }
185
186
187 # Start function
188 start()
189 {
190     SDAEMONS="${DB_DAEMON} ${CSYSLOG_DAEMON} ${AGENTLESS_DAEMON} ossec-maild ossec-execd ossec-analysisd ossec-logcollector ossec-syscheckd ossec-monitord"
191     
192     echo "Starting $NAME $VERSION (by $AUTHOR)..."
193     lock;
194     checkpid;
195
196     # We first loop to check the config. 
197     for i in ${SDAEMONS}; do
198         ${DIR}/bin/${i} -t;
199         if [ $? != 0 ]; then
200             echo "${i}: Configuration error. Exiting"
201             unlock;
202             exit 1;
203         fi    
204     done
205     
206     # We actually start them now.
207     for i in ${SDAEMONS}; do
208         pstatus ${i};
209         if [ $? = 0 ]; then
210             ${DIR}/bin/${i};
211             if [ $? != 0 ]; then
212                 unlock;
213                 exit 1;
214             fi 
215
216             echo "Started ${i}..."            
217         else
218             echo "${i} already running..."                
219         fi    
220     
221     done    
222
223     # After we start we give 2 seconds for the daemons
224     # to internally create their PID files.
225     sleep 2;
226     unlock;
227     echo "Completed."
228 }
229
230 # Process status
231 pstatus()
232 {
233     pfile=$1;
234     
235     # pfile must be set
236     if [ "X${pfile}" = "X" ]; then
237         return 0;
238     fi
239         
240     ls ${DIR}/var/run/${pfile}*.pid > /dev/null 2>&1
241     if [ $? = 0 ]; then
242         for j in `cat ${DIR}/var/run/${pfile}*.pid 2>/dev/null`; do
243             ps -p $j |grep ossec >/dev/null 2>&1
244             if [ ! $? = 0 ]; then
245                 echo "${pfile}: Process $j not used by ossec, removing .."
246                 rm -f ${DIR}/var/run/${pfile}-$j.pid
247                 continue;
248             fi
249                 
250             kill -0 $j > /dev/null 2>&1
251             if [ $? = 0 ]; then
252                 return 1;
253             fi    
254         done    
255     fi
256     
257     return 0;    
258 }
259
260
261 # Stop all
262 stopa()
263 {
264     lock;
265     checkpid;
266     for i in ${DAEMONS}; do
267         pstatus ${i};
268         if [ $? = 1 ]; then
269             echo "Killing ${i} .. ";
270             
271             kill `cat ${DIR}/var/run/${i}*.pid`;
272         else
273             echo "${i} not running .."; 
274         fi
275         
276         rm -f ${DIR}/var/run/${i}*.pid
277         
278      done    
279     
280     unlock;
281     echo "$NAME $VERSION Stopped"
282 }
283
284
285 ### MAIN HERE ###
286
287 case "$1" in
288   start)
289         start
290         ;;
291   stop) 
292         stopa
293         ;;
294   restart)
295         stopa
296         start
297         ;;
298   status)
299     status
300         ;;
301   help)  
302     help
303     ;;
304   enable)
305     enable $1 $2;
306     ;;  
307   disable)
308     disable $1 $2;
309     ;;  
310   *)
311     help
312 esac