summaryrefslogtreecommitdiffstats
path: root/test/testrun_1e.sh
blob: c2d874b3c3c7cd70239af8beffcd4a4228e0d728 (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#! /bin/sh

#
# Copyright Rainer Wichmann (2006)
#
# License Information:
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

BUILDOPTS="--quiet $TRUST --enable-debug=gdb --enable-xml-log --enable-port-check --prefix=$PW_DIR --localstatedir=$PW_DIR --with-config-file=$RCFILE --with-log-file=$LOGFILE --with-pid-file=$PW_DIR/.samhain_lock --with-data-file=$PW_DIR/.samhain_file"
export BUILDOPTS

MAXTEST=5; export MAXTEST

test_log_prelude="/var/log/prelude/prelude-text.log"; export test_log_prelude

PORTPOLICY_5="
[ReadOnly]
file=${BASE}
[PortCheck]
PortCheckActive = yes
PortCheckUDP = no
PortCheckInterface = 127.0.0.1
"

chk_portdata_5 () {
    one_sec_sleep

    if [ -z "$PM" ]; then
	log_skip 5 $MAXTEST 'prelude-manager not found in $PATH'
    elif [ -z "$doall" ]; then
	log_skip 5 $MAXTEST 'logging to prelude (or use --really-all)'
    else
	tmp=`egrep 'Service: port=5500 .unknown. protocol=tcp' ${test_log_prelude} 2>/dev/null | wc -l`
	if [ $tmp -lt 1 ]; then
	    [ -z "$verbose" ] || log_msg_fail "port 5500";
	    [ -z "$quiet" ] && log_fail 5 ${MAXTEST};
	    return 1
	fi
    #
	[ -z "$quiet" ] && log_ok 5 ${MAXTEST};
    fi
    return 0
}

refine_portpolicy_5 ()
{
    echo "PortCheckIgnore=2026/tcp" >>"${RCFILE}"
    echo "PortCheckIgnore=2027/udp" >>"${RCFILE}"
    echo "PortCheckIgnore=2028/tcp" >>"${RCFILE}"
    echo "PortCheckIgnore=2029/udp" >>"${RCFILE}"    
}

PORTPOLICY_4="
[ReadOnly]
file=${BASE}
[PortCheck]
PortCheckActive = yes
PortCheckUDP = no
"

chk_portdata_4 () {
    one_sec_sleep

    egrep 'CRIT.*POLICY \[ServiceNew\]' $LOGFILE >/dev/null 2>&1
    if [ $? -eq 0 ]; then
	[ -z "$verbose" ] || egrep 'CRIT.*POLICY \[ServiceNew\]' $LOGFILE
	[ -z "$verbose" ] || log_msg_fail "Open ports";
	return 1
    fi
}

refine_portpolicy_4 ()
{
    cat "$LOGFILE" | grep ServiceNew | sed 's/.*port: //' | awk '{ print $1 }' | \
    while read line; do
	echo "PortCheckSkip=$line" >>"${RCFILE}"
    done
    echo "PortCheckIgnore=2026/tcp" >>"${RCFILE}"
    echo "PortCheckIgnore=2027/udp" >>"${RCFILE}"
    echo "PortCheckIgnore=2028/tcp" >>"${RCFILE}"
    echo "PortCheckIgnore=2029/udp" >>"${RCFILE}"
}

PORTPOLICY_3="
[ReadOnly]
file=${BASE}
[PortCheck]
PortCheckActive = yes
PortCheckUDP = no
"

chk_portdata_3 () {
    one_sec_sleep

    egrep 'CRIT.*POLICY \[ServiceNew\]' $LOGFILE >/dev/null 2>&1
    if [ $? -eq 0 ]; then
	
	[ -z "$verbose" ] || log_msg_fail "Open ports";
	return 1
    fi
}

refine_portpolicy_3 ()
{
    cat "$LOGFILE" | grep ServiceNew | sed 's/.*port: //' | awk '{ print $1 }' | \
    while read line; do
	echo "PortCheckIgnore=$line" >>"${RCFILE}"
    done
    echo "PortCheckIgnore=2026/tcp" >>"${RCFILE}"
    echo "PortCheckIgnore=2027/udp" >>"${RCFILE}"
    echo "PortCheckIgnore=2028/tcp" >>"${RCFILE}"
    echo "PortCheckIgnore=2029/udp" >>"${RCFILE}"
}


PORTPOLICY_2="
[ReadOnly]
file=${BASE}
[PortCheck]
PortCheckActive = yes
PortCheckUDP = no
"

chk_portdata_2 () {
    one_sec_sleep

    egrep 'CRIT.*POLICY \[ServiceNew\]' $LOGFILE >/dev/null 2>&1
    if [ $? -eq 0 ]; then
	
	[ -z "$verbose" ] || log_msg_fail "Open ports";
	return 1
    fi
}

refine_portpolicy_2 ()
{
    cat "$LOGFILE" | grep ServiceNew | sed 's/.*port: //' | awk '{ print $1 }' | \
    while read line; do
	echo "PortCheckOptional=$line" >>"${RCFILE}"
    done
}

PORTPOLICY_1="
[ReadOnly]
file=${BASE}
[PortCheck]
PortCheckActive = yes
PortCheckUDP = no
"

chk_portdata_1 () {
    one_sec_sleep

    egrep 'CRIT.*POLICY \[ServiceNew\]' $LOGFILE >/dev/null 2>&1
    if [ $? -eq 0 ]; then
	
	[ -z "$verbose" ] || log_msg_fail "Open ports";
	return 1
    fi
}

refine_portpolicy_1 ()
{
    cat "$LOGFILE" | grep ServiceNew | sed 's/.*port: //' | awk '{ print $1 }' | \
    while read line; do
	echo "PortCheckRequired=$line" >>"${RCFILE}"
    done
}

prep_portpolicy ()
{
    test -f "${RCFILE}" || touch "${RCFILE}"
    eval echo '"$'"PORTPOLICY_$1"'"' >>"${RCFILE}"
}

run_check_prelude()
{
    echo -n >"${test_log_prelude}"

    ./samhain -t check -p none -l info --set-prelude-severity=info --prelude --server-addr 127.0.0.1:5500 >/dev/null
 
    if test x$? = x0; then

	./samhain -j -L $LOGFILE >"${LOGFILE}.tmp" && mv "${LOGFILE}.tmp" "${LOGFILE}"

	if [ $? -ne 0 ]; then
	    [ -z "$quiet" ]   && log_msg_fail  "mv logfile...";
	    return 1
	fi
	[ -z "$verbose" ] || log_msg_ok    "check...";
    else
	[ -z "$quiet" ]   && log_msg_fail  "check...";
	return 1
    fi
}


testrun_internal_1e ()
{
	[ -z "$verbose" ] || echo Working directory: $PW_DIR
	[ -z "$verbose" ] || { echo MAKE is $MAKE; echo; }

	#
	# test standalone compilation
	#
	[ -z "$verbose" ] || { echo; echo "${S}Building standalone agent${E}"; echo; }

	if test -r "Makefile"; then
		$MAKE distclean >/dev/null 
	fi

	tcount=1
	${TOP_SRCDIR}/configure ${BUILDOPTS} 

	#
	if test x$? = x0; then
		[ -z "$verbose" ] ||     log_msg_ok "configure..."; 
		$MAKE >/dev/null 2>&1
		if test x$? = x0; then
		    [ -z "$verbose" ] || log_msg_ok "make..."; 
		else
		    [ -z "$quiet" ] &&   log_msg_fail "make..."; 
		    return 1
		fi

	else
		[ -z "$quiet" ] &&       log_msg_fail "configure...";
		return 1
	fi

	[ -z "$verbose" ] || { echo; echo "${S}Running test suite${E}"; echo; }

	POLICY=`eval echo '"$'"PORTPOLICY_$tcount"'"'`

	until [ -z "$POLICY" ]
	do
	  prep_init
	  check_err $? ${tcount}; errval=$?
	  if [ $errval -eq 0 ]; then
	      prep_testdata
	      check_err $? ${tcount}; errval=$?
	  fi
	  if [ $errval -eq 0 ]; then
	      prep_portpolicy   ${tcount}
	      check_err $? ${tcount}; errval=$?
	  fi
	  if [ $errval -eq 0 ]; then
	      run_init
	      check_err $? ${tcount}; errval=$?
	  fi
	  #
	  if [ $errval -eq 0 ]; then
	      eval refine_portpolicy_${tcount}
	      check_err $? ${tcount}; errval=$?
	  fi
	  #
	  rm -f "$LOGFILE"
	  #
	  PRELUDEPID=0
	  #
	  if test ${tcount} -eq 5; 
	  then

	      PM=`find_path prelude-manager`

	      if [ -z "$PM" ]; then
		  if [ $errval -eq 0 ]; then
		      run_check
		      check_err $? ${tcount}; errval=$?
		  fi
	      elif [ -z "$doall" ]; then
		  if [ $errval -eq 0 ]; then
		      run_check
		      check_err $? ${tcount}; errval=$?
		  fi
	      else
		  #
		  #
		  ${TOP_SRCDIR}/configure ${BUILDOPTS} --with-prelude
		  #
	          #
		  if test x$? = x0; then
		      [ -z "$verbose" ] ||     log_msg_ok "configure..."; 
		      $MAKE >/dev/null 2>&1
		      if test x$? = x0; then
			  [ -z "$verbose" ] || log_msg_ok "make..."; 
		      else
			  [ -z "$quiet" ] &&   log_msg_fail "make..."; 
			  return 1
		      fi

		  else
		      [ -z "$quiet" ] &&       log_msg_fail "configure...";
		      return 1
		  fi
		  #
		  #
		  if ! test -d /var/run/prelude-manager
		  then
		      [ -z "$verbose" ] || log_msg_ok    "create /var/run/prelude-manager...";
		      sudo mkdir /var/run/prelude-manager
		      sudo chown prelude:rainer /var/run/prelude-manager
		      sudo chmod 770 /var/run/prelude-manager
		  fi
		  if ! test -d /var/spool/prelude/samhain/global
		  then
		      [ -z "$verbose" ] || log_msg_ok    "create /var/spool/prelude/samhain/global...";
		      sudo mkdir -p /var/spool/prelude/samhain/global
		      sudo chown prelude:rainer /var/spool/prelude/samhain/global
		      sudo chmod 770 /var/spool/prelude/samhain/global
		  fi

		  #
		  #
		  #[ -z "$verbose" ] || { echo " starting prelude-manager.."; echo " ($PM --textmod -l $PW_DIR/test_log_prelude --listen 127.0.0.1:5500 >/dev/null 2>&1 &)"; }
		  #"$PM" --textmod -l $PW_DIR/test_log_prelude --listen 127.0.0.1:5500 >/dev/null 2>&1 &
		  #PRELUDEPID=$!
		  #
		  #
		  five_sec_sleep
		  #
		  #
		  if [ $errval -eq 0 ]; then
		      run_check_prelude
		      check_err $? ${tcount}; errval=$?
		  fi
	      fi

	  else
	      if [ $errval -eq 0 ]; then
		  run_check
		  check_err $? ${tcount}; errval=$?
	      fi
	  fi
	  #
	  if [ $errval -eq 0 ]; then
	      eval chk_portdata_${tcount}
	      check_err $? ${tcount}; errval=$?
	  fi
	  #
	  if [ $errval -eq 0 ]; then
	      if test ${tcount} -ne 5; then
		  [ -z "$quiet" ] && log_ok ${tcount} ${MAXTEST};
	      fi
	  fi
	  let "tcount = tcount + 1" >/dev/null
	  POLICY=`eval echo '"$'"PORTPOLICY_$tcount"'"'`

	  if test $PRELUDEPID -ne 0;
	  then
	      kill $PRELUDEPID
	  fi

	done
	    
	return 0
}

testrun1e ()
{
    log_start "RUN STANDALONE W/PORTCHECK"
    testrun_internal_1e
    log_end "RUN STANDALONE W/PORTCHECK"
    return 0
}