[1] | 1 | #! /bin/sh
|
---|
| 2 |
|
---|
[27] | 3 | #
|
---|
| 4 | # Copyright Rainer Wichmann (2006)
|
---|
| 5 | #
|
---|
| 6 | # License Information:
|
---|
| 7 | # This program is free software; you can redistribute it and/or modify
|
---|
| 8 | # it under the terms of the GNU General Public License as published by
|
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 10 | # (at your option) any later version.
|
---|
| 11 | #
|
---|
| 12 | # This program is distributed in the hope that it will be useful,
|
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | # GNU General Public License for more details.
|
---|
| 16 | #
|
---|
| 17 | # You should have received a copy of the GNU General Public License
|
---|
| 18 | # along with this program; if not, write to the Free Software
|
---|
| 19 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 20 | #
|
---|
| 21 |
|
---|
[19] | 22 | RCFILE="$PW_DIR/testrc_1.dyn"; export RCFILE
|
---|
| 23 | LOGFILE="$PW_DIR/.samhain_log"; export LOGFILE
|
---|
| 24 | PIDFILE="$PW_DIR/.samhain_lock"; export PIDFILE
|
---|
[1] | 25 |
|
---|
[19] | 26 | BASE="${PW_DIR}/testrun_testdata"; export BASE
|
---|
| 27 | TDIRS="a b c a/a a/b a/c a/a/a a/a/b a/a/c a/a/a/a a/a/a/b a/a/a/c"; export TDIRS
|
---|
| 28 | TFILES="x y z"; export TFILES
|
---|
[1] | 29 |
|
---|
[19] | 30 | prep_testdata ()
|
---|
| 31 | {
|
---|
| 32 | chmod -R 0700 "${BASE}" || {
|
---|
| 33 | [ -z "$quiet" ] && log_msg_fail "chmod -R 0700 ${BASE}";
|
---|
| 34 | return 1;
|
---|
| 35 | }
|
---|
[1] | 36 |
|
---|
[19] | 37 | rm -rf "${BASE}" || {
|
---|
| 38 | [ -z "$quiet" ] && log_msg_fail "rm -rf ${BASE}";
|
---|
| 39 | return 1;
|
---|
| 40 | }
|
---|
[1] | 41 |
|
---|
[19] | 42 | mkdir "${BASE}" || {
|
---|
| 43 | [ -z "$quiet" ] && log_msg_fail "mkdir ${BASE}";
|
---|
| 44 | return 1;
|
---|
| 45 | }
|
---|
[1] | 46 |
|
---|
[19] | 47 | for ff in $TDIRS; do
|
---|
| 48 | mkdir "${BASE}/${ff}" || {
|
---|
| 49 | [ -z "$quiet" ] && log_msg_fail "mkdir ${BASE}/${ff}";
|
---|
| 50 | return 1;
|
---|
| 51 | }
|
---|
| 52 | chmod 0755 "${BASE}/${ff}"
|
---|
| 53 | for gg in $TFILES; do
|
---|
| 54 | echo "This is a test file" > "${BASE}/${ff}/${gg}"
|
---|
| 55 | chmod 0644 "${BASE}/${ff}/${gg}"
|
---|
| 56 | done
|
---|
| 57 | done
|
---|
| 58 | }
|
---|
[1] | 59 |
|
---|
[19] | 60 | mkconfig_misc ()
|
---|
| 61 | {
|
---|
| 62 | test -f "${RCFILE}" || touch "${RCFILE}"
|
---|
| 63 | cat >> "${RCFILE}" <<End-of-data
|
---|
| 64 | [Misc]
|
---|
| 65 | Daemon=no
|
---|
| 66 | SetFilecheckTime=60
|
---|
| 67 | TrustedUser=uucp,fax,fnet
|
---|
| 68 | SetRecursionLevel=10
|
---|
| 69 | SetLoopTime=30
|
---|
| 70 | ReportFullDetail = no
|
---|
| 71 | ChecksumTest=check
|
---|
[1] | 72 |
|
---|
[19] | 73 | End-of-data
|
---|
| 74 | }
|
---|
[1] | 75 |
|
---|
[19] | 76 | mkconfig_log ()
|
---|
[1] | 77 | {
|
---|
[19] | 78 | test -f "${RCFILE}" || touch "${RCFILE}"
|
---|
| 79 | cat >> "${RCFILE}" <<End-of-data
|
---|
| 80 | [Log]
|
---|
| 81 | MailSeverity=none
|
---|
| 82 | LogSeverity=warn
|
---|
| 83 | SyslogSeverity=none
|
---|
| 84 | PrintSeverity=info
|
---|
| 85 | MailSeverity=none
|
---|
| 86 | #Restrict to certain classes of messages
|
---|
| 87 | #LogClass=RUN
|
---|
| 88 | #PreludeSeverity=err
|
---|
| 89 | #ExportSeverity=none
|
---|
| 90 |
|
---|
| 91 | End-of-data
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | mkconfig_sev ()
|
---|
| 95 | {
|
---|
| 96 | test -f "${RCFILE}" || touch "${RCFILE}"
|
---|
| 97 | cat >> "${RCFILE}" <<End-of-data
|
---|
| 98 | [EventSeverity]
|
---|
| 99 | SeverityUser0=crit
|
---|
| 100 | SeverityUser1=crit
|
---|
| 101 | SeverityReadOnly=crit
|
---|
| 102 | SeverityLogFiles=crit
|
---|
| 103 | SeverityGrowingLogs=crit
|
---|
| 104 | SeverityIgnoreNone=crit
|
---|
| 105 | SeverityAttributes=crit
|
---|
| 106 | SeverityIgnoreAll=crit
|
---|
| 107 | SeverityFiles=err
|
---|
| 108 | SeverityDirs=err
|
---|
| 109 | SeverityNames=warn
|
---|
| 110 |
|
---|
| 111 | End-of-data
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | prep_init ()
|
---|
| 115 | {
|
---|
| 116 | rm -f ./.samhain_file
|
---|
| 117 | rm -f "${LOGFILE}"
|
---|
| 118 | rm -f ./.samhain_lock
|
---|
| 119 |
|
---|
| 120 | rm -f "${RCFILE}"
|
---|
| 121 | mkconfig_sev
|
---|
| 122 | mkconfig_log
|
---|
| 123 | mkconfig_misc
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | TESTPOLICY="
|
---|
| 127 | [ReadOnly]
|
---|
| 128 | dir=${BASE}/c
|
---|
| 129 | [Attributes]
|
---|
| 130 | dir=${BASE}/a
|
---|
| 131 | #dir=${BASE}/b
|
---|
| 132 | "
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | testtime0_int ()
|
---|
| 136 | {
|
---|
| 137 | [ -z "$verbose" ] || echo Working directory: $PW_DIR
|
---|
| 138 | [ -z "$verbose" ] || { echo MAKE is $MAKE; echo; }
|
---|
[1] | 139 | #
|
---|
| 140 | # standalone compilation
|
---|
| 141 | #
|
---|
[19] | 142 | [ -z "$verbose" ] || { echo; echo "${S}Building standalone agent w/timeserver${E}"; echo; }
|
---|
[1] | 143 | #
|
---|
| 144 | if test -r "Makefile"; then
|
---|
[19] | 145 | $MAKE distclean >/dev/null
|
---|
[1] | 146 | fi
|
---|
| 147 | #
|
---|
[19] | 148 | ${TOP_SRCDIR}/configure --quiet --enable-debug --enable-xml-log --prefix=$PW_DIR --localstatedir=$PW_DIR --with-config-file=$RCFILE --with-log-file=$LOGFILE --with-pid-file=$PIDFILE --with-data-file=$PW_DIR/.samhain_file
|
---|
[1] | 149 | #
|
---|
| 150 | if test x$? = x0; then
|
---|
[19] | 151 | [ -z "$verbose" ] || log_msg_ok "configure...";
|
---|
[1] | 152 | $MAKE > /dev/null
|
---|
[19] | 153 | if test x$? = x0; then
|
---|
| 154 | [ -z "$verbose" ] || log_msg_ok "make...";
|
---|
| 155 | else
|
---|
| 156 | [ -z "$quiet" ] && log_msg_fail "make...";
|
---|
| 157 | return 1
|
---|
| 158 | fi
|
---|
[1] | 159 | else
|
---|
[19] | 160 | [ -z "$quiet" ] && log_msg_fail "configure...";
|
---|
| 161 | return 1
|
---|
[1] | 162 | fi
|
---|
| 163 |
|
---|
[19] | 164 | prep_init && prep_testdata && echo "$TESTPOLICY" >>$RCFILE
|
---|
| 165 | if [ $? -ne 0 ]; then
|
---|
| 166 | [ -z "$quiet" ] && log_msg_fail "prepare...";
|
---|
| 167 | return 1
|
---|
| 168 | fi
|
---|
[1] | 169 |
|
---|
[19] | 170 | ./samhain -t init -p none
|
---|
[1] | 171 |
|
---|
| 172 | if test x$? = x0; then
|
---|
[19] | 173 | [ -z "$verbose" ] || log_msg_ok "init...";
|
---|
[1] | 174 | else
|
---|
[19] | 175 | [ -z "$quiet" ] && log_msg_fail "init...";
|
---|
| 176 | return 1
|
---|
[1] | 177 | fi
|
---|
| 178 |
|
---|
[19] | 179 | chmod 0555 "${BASE}/a/x"
|
---|
| 180 | chmod 0555 "${BASE}/b/x"
|
---|
| 181 |
|
---|
| 182 | ./samhain -t check -p none -l info -D
|
---|
| 183 |
|
---|
| 184 | count=0
|
---|
| 185 | until [ -f $PIDFILE ]; do
|
---|
| 186 | sleep 1
|
---|
| 187 | let "count = count + 1" >/dev/null
|
---|
| 188 | if [ $count -gt 12 ]; then
|
---|
| 189 | break;
|
---|
| 190 | fi
|
---|
| 191 | done
|
---|
| 192 |
|
---|
| 193 | if test x$? = x0; then
|
---|
| 194 | [ -z "$verbose" ] || log_msg_ok "start daemon...";
|
---|
| 195 | else
|
---|
| 196 | [ -z "$quiet" ] && log_msg_fail "start daemon...";
|
---|
| 197 | return 1
|
---|
| 198 | fi
|
---|
| 199 |
|
---|
| 200 | return 0
|
---|
[1] | 201 | }
|
---|
| 202 |
|
---|
[19] | 203 | MAXTEST=14; export MAXTEST
|
---|
[1] | 204 |
|
---|
[19] | 205 | die () {
|
---|
| 206 | test -z "$stoponerr" && return 0;
|
---|
| 207 | PID=`cat $PIDFILE`
|
---|
| 208 | kill -9 $PID
|
---|
| 209 | }
|
---|
[1] | 210 |
|
---|
[19] | 211 | killdaemon () {
|
---|
| 212 | if [ -f $PIDFILE ]; then
|
---|
| 213 | PID=`cat $PIDFILE`
|
---|
| 214 | kill -9 $PID
|
---|
| 215 | fi
|
---|
| 216 | }
|
---|
[1] | 217 |
|
---|
[19] | 218 | check_err () {
|
---|
| 219 | if [ ${2} -ne 0 ]; then
|
---|
| 220 | die;
|
---|
| 221 | [ -z "$quiet" ] && log_fail ${1} ${MAXTEST} "${3}";
|
---|
| 222 | return 1
|
---|
| 223 | else
|
---|
| 224 | [ -z "$quiet" ] && log_ok ${1} ${MAXTEST} "${3}";
|
---|
| 225 | fi
|
---|
| 226 | }
|
---|
[1] | 227 |
|
---|
[19] | 228 | daemontest_started () {
|
---|
| 229 | PID=`cat $PIDFILE`
|
---|
[1] | 230 |
|
---|
[19] | 231 | kill -0 $PID
|
---|
| 232 | check_err ${1} $? "started"
|
---|
| 233 | }
|
---|
[1] | 234 |
|
---|
[19] | 235 | daemontest_sigterm () {
|
---|
| 236 | PID=`cat $PIDFILE`
|
---|
| 237 |
|
---|
| 238 | kill -15 $PID
|
---|
| 239 | count=0
|
---|
| 240 | while [ `kill -0 $PID` ]; do
|
---|
| 241 | sleep 1
|
---|
| 242 | let "count = count + 1" >/dev/null
|
---|
| 243 | if [ $count -gt 12 ]; then
|
---|
| 244 | check_err ${1} 1 "sigterm"
|
---|
| 245 | return 1
|
---|
| 246 | fi
|
---|
| 247 | done
|
---|
| 248 | check_err ${1} 0 "sigterm"
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | daemontest_sigusr2 () {
|
---|
| 252 | PID=`cat $PIDFILE`
|
---|
| 253 |
|
---|
| 254 | tmp=`grep 'File check completed' $LOGFILE | wc -l`
|
---|
| 255 | kill -USR2 $PID
|
---|
| 256 | kill -TTOU $PID
|
---|
| 257 |
|
---|
| 258 | count=0
|
---|
| 259 | tmp2=`grep 'SUSPEND' $LOGFILE | wc -l`
|
---|
| 260 | while [ $tmp2 -ne $2 ]; do
|
---|
| 261 | sleep 1
|
---|
| 262 | let "count = count + 1" >/dev/null
|
---|
| 263 | if [ $count -gt 12 ]; then
|
---|
| 264 | check_err ${1} 1 "sigusr2: suspend"
|
---|
| 265 | return 1
|
---|
| 266 | fi
|
---|
| 267 | tmp2=`grep 'SUSPEND' $LOGFILE | wc -l`
|
---|
| 268 | done
|
---|
| 269 |
|
---|
| 270 | kill -USR2 $PID
|
---|
| 271 |
|
---|
| 272 | count=0
|
---|
| 273 | tmp2=$tmp
|
---|
| 274 | while [ $tmp2 -eq $tmp ]; do
|
---|
| 275 | sleep 1
|
---|
| 276 | let "count = count + 1" >/dev/null
|
---|
| 277 | if [ $count -gt 12 ]; then
|
---|
| 278 | check_err ${1} 1 "sigusr2: wakeup"
|
---|
| 279 | return 1
|
---|
| 280 | fi
|
---|
| 281 | tmp2=`grep 'File check completed' $LOGFILE | wc -l`
|
---|
| 282 | done
|
---|
| 283 | check_err ${1} 0 "sigusr2"
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | daemontest_sigttou () {
|
---|
| 287 | PID=`cat $PIDFILE`
|
---|
| 288 |
|
---|
| 289 | tmp=`grep 'File check completed' $LOGFILE | wc -l`
|
---|
| 290 | kill -TTOU $PID
|
---|
| 291 | count=0
|
---|
| 292 | tmp2=$tmp
|
---|
| 293 | while [ $tmp2 -eq $tmp ]; do
|
---|
| 294 | sleep 1
|
---|
| 295 | let "count = count + 1" >/dev/null
|
---|
| 296 | if [ $count -gt 12 ]; then
|
---|
| 297 | check_err ${1} 1 "sigttou"
|
---|
| 298 | return 1
|
---|
| 299 | fi
|
---|
| 300 | tmp2=`grep 'File check completed' $LOGFILE | wc -l`
|
---|
| 301 | done
|
---|
| 302 | check_err ${1} 0 "sigttou"
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | daemontest_sighup () {
|
---|
| 306 |
|
---|
| 307 | if [ $2 -eq 1 ]; then
|
---|
| 308 | echo "dir=${BASE}/b" >>$RCFILE
|
---|
| 309 | tmp=`grep CRIT $LOGFILE | grep -v Runtime | wc -l`
|
---|
| 310 | if [ $tmp -ne 1 ]; then
|
---|
| 311 | [ -z "$verbose" ] || log_msg_fail "policy count (before)";
|
---|
| 312 | return 1
|
---|
| 313 | fi
|
---|
| 314 | fi
|
---|
| 315 |
|
---|
| 316 | PID=`cat $PIDFILE`
|
---|
| 317 | kill -HUP $PID
|
---|
| 318 |
|
---|
| 319 | if [ $2 -eq 1 ]; then
|
---|
| 320 | kill -TTOU $PID
|
---|
| 321 | count=0
|
---|
| 322 | tmp=`grep CRIT $LOGFILE | grep -v Runtime | wc -l`
|
---|
| 323 | while [ $tmp -lt 2 ]; do
|
---|
| 324 | sleep 1
|
---|
| 325 | if [ $count -gt 12 ]; then
|
---|
| 326 | [ -z "$verbose" ] || log_msg_fail "policy count (after)";
|
---|
| 327 | return 1
|
---|
| 328 | fi
|
---|
| 329 | tmp=`grep CRIT $LOGFILE | grep -v Runtime | wc -l`
|
---|
| 330 | done
|
---|
| 331 | fi
|
---|
| 332 |
|
---|
| 333 | count=0
|
---|
| 334 | tmp2=0
|
---|
| 335 | while [ $tmp2 -ne $2 ]; do
|
---|
| 336 | sleep 1
|
---|
| 337 | let "count = count + 1" >/dev/null
|
---|
| 338 | if [ $count -gt 12 ]; then
|
---|
| 339 | check_err ${1} 1 "sighup"
|
---|
| 340 | return 1
|
---|
| 341 | fi
|
---|
| 342 | tmp2=`grep 'Runtime configuration reloaded' $LOGFILE | wc -l`
|
---|
| 343 | done
|
---|
| 344 | check_err ${1} 0 "sighup"
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | daemontest_sigabrt () {
|
---|
| 348 | PID=`cat $PIDFILE`
|
---|
| 349 | kill -ABRT $PID
|
---|
| 350 |
|
---|
| 351 | count=0
|
---|
| 352 | while [ -f $LOGFILE.lock ]; do
|
---|
| 353 | sleep 1
|
---|
| 354 | let "count = count + 1" >/dev/null
|
---|
| 355 | if [ $count -gt 12 ]; then
|
---|
| 356 | check_err ${1} 1 "sigabrt"
|
---|
| 357 | return 1
|
---|
| 358 | fi
|
---|
| 359 | done
|
---|
| 360 |
|
---|
| 361 | kill -TTOU $PID
|
---|
| 362 |
|
---|
| 363 | sleep 4
|
---|
| 364 |
|
---|
| 365 | if [ -f $LOGFILE.lock ]; then
|
---|
| 366 | tmp=`grep '<trail>' $LOGFILE | wc -l`
|
---|
| 367 | tst=$2; let "tst = tst + 2" >/dev/null;
|
---|
| 368 | if [ $tmp -eq $tst ]; then
|
---|
| 369 | check_err ${1} 0 "sigabrt"
|
---|
| 370 | return 0
|
---|
| 371 | fi
|
---|
| 372 | fi
|
---|
| 373 | check_err ${1} 1 "sigabrt"
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | testtime0 () {
|
---|
| 377 | log_start "DAEMON CONTROL"
|
---|
| 378 |
|
---|
| 379 | testtime0_int;
|
---|
| 380 |
|
---|
| 381 | tcount=1
|
---|
| 382 |
|
---|
| 383 | trap 'killdaemon' 1 3 15
|
---|
| 384 |
|
---|
| 385 | daemontest_started $tcount;
|
---|
| 386 |
|
---|
| 387 | let "tcount = tcount + 1" >/dev/null
|
---|
| 388 | daemontest_sigttou $tcount;
|
---|
| 389 | let "tcount = tcount + 1" >/dev/null
|
---|
| 390 | daemontest_sigttou $tcount;
|
---|
| 391 | let "tcount = tcount + 1" >/dev/null
|
---|
| 392 | daemontest_sigttou $tcount;
|
---|
| 393 |
|
---|
| 394 | let "tcount = tcount + 1" >/dev/null
|
---|
| 395 | daemontest_sigusr2 $tcount 1;
|
---|
| 396 | let "tcount = tcount + 1" >/dev/null
|
---|
| 397 | daemontest_sigusr2 $tcount 2;
|
---|
| 398 | let "tcount = tcount + 1" >/dev/null
|
---|
| 399 | daemontest_sigusr2 $tcount 3;
|
---|
| 400 |
|
---|
| 401 | let "tcount = tcount + 1" >/dev/null
|
---|
| 402 | daemontest_sigabrt $tcount 1;
|
---|
| 403 | let "tcount = tcount + 1" >/dev/null
|
---|
| 404 | daemontest_sigabrt $tcount 2;
|
---|
| 405 | let "tcount = tcount + 1" >/dev/null
|
---|
| 406 | daemontest_sigabrt $tcount 3;
|
---|
| 407 |
|
---|
| 408 | let "tcount = tcount + 1" >/dev/null
|
---|
| 409 | daemontest_sighup $tcount 1;
|
---|
| 410 | let "tcount = tcount + 1" >/dev/null
|
---|
| 411 | daemontest_sighup $tcount 2;
|
---|
| 412 | let "tcount = tcount + 1" >/dev/null
|
---|
| 413 | daemontest_sighup $tcount 3;
|
---|
| 414 |
|
---|
| 415 | let "tcount = tcount + 1" >/dev/null
|
---|
| 416 | daemontest_sigterm $tcount;
|
---|
| 417 |
|
---|
| 418 | log_end "DAEMON CONTROL"
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 |
|
---|