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