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