Debian Codegraph @

tests/mitre/test_mitre_common.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 # Author:
4 # Peter Vrabec, <[email protected]>
5 # Daniel Kopecek <[email protected]>
6
7
8 MITRE_FILES="/tmp/support"
9 EXTVARFILE="${MITRE_FILES}/var/linux-external-variables.xml"
10
11 DISTRO="$(cat /etc/*-release | head -n1)"
12 DISTRO_NAME="$(cat /etc/*-release | awk '{print $1}' | head -n1)"
13 DISTRO_RELEASE="$(cat /etc/*-release | sed -n 's|^[^0-9]*\([0-9]*\).*$|\1|p' | head -n1)"
14 SELINUX_ENABLED=$(grep -iqE "(SELINUX=enforcing|SELINUX=permissive)" \
15 /etc/selinux/config 2>/dev/null; echo $?)
16
17 function test_mitre {
18
19 require "egrep" || return 255
20 require "awk" || return 255
21
22 if [ ! -d "$MITRE_FILES" ]; then
23 tar zxf ${srcdir}/support.tgz -C /tmp
24 # workaround file access time issue
25 find "$MITRE_FILES"
26 # workaround old schema version in linux-external-variables.xml
27 sed -i 's/5.0/5.5/' "$EXTVARFILE"
28 fi
29
30 local ret_val=0;
31 local DEFFILE=${srcdir}/$1
32 local RESFILE=$1.results
33
34 [ -f $RESFILE ] && rm -f $RESFILE
35 $OSCAP oval eval --results "$RESFILE" --variables "$EXTVARFILE" "$DEFFILE"
36 # catch error from oscap tool
37 ret_val=$?
38 if [ $ret_val -eq 1 ]; then
39 return 1
40 fi
41
42 if [ -n "$3" ]; then
43 # only check the individual results specified in the arg list
44 shift 1
45 while [ -n "$1" -a -n "$2" ]; do
46 def_id="$1"
47 def_res="$2"
48 grep -q "definition_id=\"$def_id\".*result=\"$def_res\"" "$RESFILE" || return 1
49 shift 2
50 done
51 return 0
52 fi
53
54 # assume all definitions should have the same result
55
56 LINES=`grep definition_id "$RESFILE"`
57 # catch error from grep
58 ret_val=$?
59 if [ $ret_val -eq 2 ]; then
60 return 1
61 fi
62
63 # calculate return code
64 echo "$LINES" | grep -q -v "result=\"$2\""
65 ret_val=$?
66 if [ $ret_val -eq 1 ]; then
67 return 0;
68 elif [ $ret_val -eq 0 ]; then
69 return 1;
70 else
71 return "$ret_val"
72 fi
73 }
74
75 function cleanup_mitre {
76 rm -rf "$MITRE_FILES"
77 }