Debian Codegraph @

utils/oscap-vm

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 # Copyright 2015 Martin Preisler <[email protected]>
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 function die()
20 {
21 echo "$*" >&2
22 exit 1
23 }
24
25 function invalid()
26 {
27 echo -e "$*\n" >&2
28 usage
29 exit 1
30 }
31
32 function usage()
33 {
34 echo "oscap-vm -- Tool for offline SCAP evaluation of virtual machines."
35 echo
36 echo "Usage:"
37 echo
38 echo "$ oscap-vm [--oscap=<oscap_binary>] image VM_STORAGE_IMAGE xccdf eval [options] INPUT_CONTENT"
39 echo "$ oscap-vm [--oscap=<oscap_binary>] domain VM_DOMAIN xccdf eval [options] INPUT_CONTENT"
40 echo
41 echo "supported oscap xccdf eval options are:"
42 echo " --profile"
43 echo " --tailoring-file"
44 echo " --tailoring-id"
45 echo " --cpe (external OVAL dependencies are not supported yet!)"
46 echo " --oval-results"
47 echo " --check-engine-results"
48 echo " --results"
49 echo " --results-arf"
50 echo " --report"
51 echo " --skip-validation"
52 echo " --fetch-remote-resources"
53 echo " --local-files"
54 echo " --progress"
55 echo " --datastream-id"
56 echo " --xccdf-id"
57 echo " --benchmark-id"
58 echo
59 echo "$ oscap-vm image VM_STORAGE_IMAGE oval eval [options] INPUT_CONTENT"
60 echo "$ oscap-vm domain VM_DOMAIN oval eval [options] INPUT_CONTENT"
61 echo
62 echo "supported oscap oval eval options are:"
63 echo " --id"
64 echo " --variables"
65 echo " --directives"
66 echo " --results"
67 echo " --report"
68 echo " --skip-validation"
69 echo " --datastream-id"
70 echo " --oval-id"
71 echo
72 echo "$ oscap-vm image VM_STORAGE_IMAGE oval collect [options] INPUT_CONTENT"
73 echo "$ oscap-vm domain VM_DOMAIN oval collect [options] INPUT_CONTENT"
74 echo
75 echo "supported oscap oval collect options are:"
76 echo " --id"
77 echo " --syschar"
78 echo " --variables"
79 echo " --skip-validation"
80 echo
81 echo "See \`man oscap\` to learn more about semantics of these options."
82 }
83
84 OSCAP_BINARY=oscap
85
86 if [ $# -lt 1 ]; then
87 invalid "No arguments provided."
88 elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
89 usage
90 exit 0
91 elif [[ "$1" == --oscap=* ]] && [ $# -gt 3 ]; then
92 OSCAP_BINARY=${1#"--oscap="}
93 shift
94 elif [ "$1" == "image" ] && [ $# -gt 2 ]; then
95 true
96 elif [ "$1" == "domain" ] && [ $# -gt 2 ]; then
97 true
98 else
99 invalid "Invalid arguments provided."
100 fi
101
102 hash guestmount 2> /dev/null || die "Cannot find guestmount, please install libguestfs utilities."
103
104 if hash guestunmount 2> /dev/null; then
105 UNMOUNT_COMMAND="guestunmount"
106 elif hash fusermount 2> /dev/null; then
107 echo "guestunmount command not present on the system, using simpler fusermount instead"
108 UNMOUNT_COMMAND="fusermount -u"
109 else
110 die "Cannot find guestunmount or fusermount, please install libguestfs utilities, or fuse."
111 fi
112
113 hash mktemp 2> /dev/null || die "Cannot find mktemp, please install coreutils."
114
115 MOUNTPOINT=$(mktemp -d)
116
117 if [ "$1" == "image" ]; then
118 echo "Mounting guestfs image '$2' to '$MOUNTPOINT'..."
119 guestmount -a "$2" -i --ro "$MOUNTPOINT"
120 if [ $? -ne 0 ]; then
121 rmdir "$MOUNTPOINT"
122 die "Failed to mount image '$2' to '$MOUNTPOINT'!"
123 fi
124 elif [ "$1" == "domain" ]; then
125 echo "Mounting guestfs domain '$2' to '$MOUNTPOINT'..."
126 guestmount -d "$2" -i --ro "$MOUNTPOINT"
127 if [ $? -ne 0 ]; then
128 rmdir "$MOUNTPOINT"
129 die "Failed to mount guestfs domain '$2' to '$MOUNTPOINT'!"
130 fi
131 fi
132
133 # Learn more at https://www.redhat.com/archives/open-scap-list/2013-July/msg00000.html
134 export OSCAP_PROBE_ROOT
135 OSCAP_PROBE_ROOT="$(cd "$MOUNTPOINT" && pwd)" || die "Unable to change current directory to OSCAP_PROBE_ROOT (MOUNTPOINT)."
136 export OSCAP_EVALUATION_TARGET="oscap-vm $1 $2"
137 shift 2
138
139 $OSCAP_BINARY "$@"
140 EXIT_CODE=$?
141 echo "Unmounting '$MOUNTPOINT'..."
142 $UNMOUNT_COMMAND "$MOUNTPOINT"
143 rmdir "$MOUNTPOINT"
144 exit $EXIT_CODE