| 1 | #!/usr/bin/envscip-shell filesystem . . `/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 diescip-shell shell shell-project 0.0.0 die().() |
| 20 | { |
| 21 | echoscip-shell system . . echo. "$*" >&2 |
| 22 | exit 1 |
| 23 | } |
| 24 | |
| 25 | function invalidscip-shell shell shell-project 0.0.0 invalid().() |
| 26 | { |
| 27 | echoscip-shell system . . echo. -e "$*\n" >&2 |
| 28 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 29 | exit 1 |
| 30 | } |
| 31 | |
| 32 | function usagescip-shell shell shell-project 0.0.0 usage().() |
| 33 | { |
| 34 | echoscip-shell system . . echo. "oscap-vm -- Tool for offline SCAP evaluation of virtual machines." |
| 35 | echoscip-shell system . . echo. |
| 36 | echoscip-shell system . . echo. "Usage:" |
| 37 | echoscip-shell system . . echo. |
| 38 | echoscip-shell system . . echo. "$ oscap-vm [--oscap=<oscap_binary>] image VM_STORAGE_IMAGE xccdf eval [options] INPUT_CONTENT" |
| 39 | echoscip-shell system . . echo. "$ oscap-vm [--oscap=<oscap_binary>] domain VM_DOMAIN xccdf eval [options] INPUT_CONTENT" |
| 40 | echoscip-shell system . . echo. |
| 41 | echoscip-shell system . . echo. "supported oscap xccdf eval options are:" |
| 42 | echoscip-shell system . . echo. " --profile" |
| 43 | echoscip-shell system . . echo. " --tailoring-file" |
| 44 | echoscip-shell system . . echo. " --tailoring-id" |
| 45 | echoscip-shell system . . echo. " --cpe (external OVAL dependencies are not supported yet!)" |
| 46 | echoscip-shell system . . echo. " --oval-results" |
| 47 | echoscip-shell system . . echo. " --check-engine-results" |
| 48 | echoscip-shell system . . echo. " --results" |
| 49 | echoscip-shell system . . echo. " --results-arf" |
| 50 | echoscip-shell system . . echo. " --report" |
| 51 | echoscip-shell system . . echo. " --skip-validation" |
| 52 | echoscip-shell system . . echo. " --fetch-remote-resources" |
| 53 | echoscip-shell system . . echo. " --local-files" |
| 54 | echoscip-shell system . . echo. " --progress" |
| 55 | echoscip-shell system . . echo. " --datastream-id" |
| 56 | echoscip-shell system . . echo. " --xccdf-id" |
| 57 | echoscip-shell system . . echo. " --benchmark-id" |
| 58 | echoscip-shell system . . echo. |
| 59 | echoscip-shell system . . echo. "$ oscap-vm image VM_STORAGE_IMAGE oval eval [options] INPUT_CONTENT" |
| 60 | echoscip-shell system . . echo. "$ oscap-vm domain VM_DOMAIN oval eval [options] INPUT_CONTENT" |
| 61 | echoscip-shell system . . echo. |
| 62 | echoscip-shell system . . echo. "supported oscap oval eval options are:" |
| 63 | echoscip-shell system . . echo. " --id" |
| 64 | echoscip-shell system . . echo. " --variables" |
| 65 | echoscip-shell system . . echo. " --directives" |
| 66 | echoscip-shell system . . echo. " --results" |
| 67 | echoscip-shell system . . echo. " --report" |
| 68 | echoscip-shell system . . echo. " --skip-validation" |
| 69 | echoscip-shell system . . echo. " --datastream-id" |
| 70 | echoscip-shell system . . echo. " --oval-id" |
| 71 | echoscip-shell system . . echo. |
| 72 | echoscip-shell system . . echo. "$ oscap-vm image VM_STORAGE_IMAGE oval collect [options] INPUT_CONTENT" |
| 73 | echoscip-shell system . . echo. "$ oscap-vm domain VM_DOMAIN oval collect [options] INPUT_CONTENT" |
| 74 | echoscip-shell system . . echo. |
| 75 | echoscip-shell system . . echo. "supported oscap oval collect options are:" |
| 76 | echoscip-shell system . . echo. " --id" |
| 77 | echoscip-shell system . . echo. " --syschar" |
| 78 | echoscip-shell system . . echo. " --variables" |
| 79 | echoscip-shell system . . echo. " --skip-validation" |
| 80 | echoscip-shell system . . echo. |
| 81 | echoscip-shell system . . echo. "See \`man oscap\` to learn more about semantics of these options." |
| 82 | } |
| 83 | |
| 84 | OSCAP_BINARY=oscap |
| 85 | |
| 86 | if [scip-shell system . . `[`. $# -lt 1 ]; then |
| 87 | invalidscip-shell shell shell-project 0.0.0 invalid(). "No arguments provided." |
| 88 | elif [scip-shell system . . `[`. "$1" == "-h" ] || [scip-shell system . . `[`. "$1" == "--help" ]; then |
| 89 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 90 | exit 0 |
| 91 | elif [[ "$1" == --oscap=* ]] && [scip-shell system . . `[`. $# -gt 3 ]; then |
| 92 | OSCAP_BINARY=${1#"--oscap="} |
| 93 | shift |
| 94 | elif [scip-shell system . . `[`. "$1" == "image" ] && [scip-shell system . . `[`. $# -gt 2 ]; then |
| 95 | truescip-shell system . . true. |
| 96 | elif [scip-shell system . . `[`. "$1" == "domain" ] && [scip-shell system . . `[`. $# -gt 2 ]; then |
| 97 | truescip-shell system . . true. |
| 98 | else |
| 99 | invalidscip-shell shell shell-project 0.0.0 invalid(). "Invalid arguments provided." |
| 100 | fi |
| 101 | |
| 102 | hash guestmount 2> /dev/nullscip-shell filesystem . . `/dev/null`. || diescip-shell shell shell-project 0.0.0 die(). "Cannot find guestmount, please install libguestfs utilities." |
| 103 | |
| 104 | if hash guestunmount 2> /dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 105 | UNMOUNT_COMMAND="guestunmount" |
| 106 | elif hash fusermount 2> /dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 107 | echoscip-shell system . . echo. "guestunmount command not present on the system, using simpler fusermount instead" |
| 108 | UNMOUNT_COMMAND="fusermount -u" |
| 109 | else |
| 110 | diescip-shell shell shell-project 0.0.0 die(). "Cannot find guestunmount or fusermount, please install libguestfs utilities, or fuse." |
| 111 | fi |
| 112 | |
| 113 | hash mktemp 2> /dev/nullscip-shell filesystem . . `/dev/null`. || diescip-shell shell shell-project 0.0.0 die(). "Cannot find mktemp, please install coreutils." |
| 114 | |
| 115 | MOUNTPOINT=$(mktempscip-shell system . . mktemp. -d) |
| 116 | |
| 117 | if [scip-shell system . . `[`. "$1" == "image" ]; then |
| 118 | echoscip-shell system . . echo. "Mounting guestfs image '$2' to '$MOUNTPOINT'..." |
| 119 | guestmount -a "$2" -i --ro "$MOUNTPOINT" |
| 120 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 121 | rmdirscip-shell system . . rmdir. "$MOUNTPOINT" |
| 122 | diescip-shell shell shell-project 0.0.0 die(). "Failed to mount image '$2' to '$MOUNTPOINT'!" |
| 123 | fi |
| 124 | elif [scip-shell system . . `[`. "$1" == "domain" ]; then |
| 125 | echoscip-shell system . . echo. "Mounting guestfs domain '$2' to '$MOUNTPOINT'..." |
| 126 | guestmount -d "$2" -i --ro "$MOUNTPOINT" |
| 127 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 128 | rmdirscip-shell system . . rmdir. "$MOUNTPOINT" |
| 129 | diescip-shell shell shell-project 0.0.0 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" && pwdscip-shell system . . pwd.)" || diescip-shell shell shell-project 0.0.0 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 | echoscip-shell system . . echo. "Unmounting '$MOUNTPOINT'..." |
| 142 | $UNMOUNT_COMMAND "$MOUNTPOINT" |
| 143 | rmdirscip-shell system . . rmdir. "$MOUNTPOINT" |
| 144 | exit $EXIT_CODE |