adamwill / rpms / openscap

Forked from rpms/openscap 3 years ago
Clone

Blame SOURCES/openscap-1.2.18-oscap_ssh.patch

d7b4b6
From 3f813a216322041210ebf952fc1d8efc553d488d Mon Sep 17 00:00:00 2001
d7b4b6
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
d7b4b6
Date: Tue, 21 Aug 2018 12:22:41 +0200
d7b4b6
Subject: [PATCH 1/3] Refactored code to enable supply of ssh options via env
d7b4b6
 var.
d7b4b6
d7b4b6
---
d7b4b6
 utils/oscap-ssh   | 125 +++++++++++++++++++++++++++++-----------------
d7b4b6
 utils/oscap-ssh.8 |   8 ++-
d7b4b6
 2 files changed, 86 insertions(+), 47 deletions(-)
d7b4b6
d7b4b6
diff --git a/utils/oscap-ssh b/utils/oscap-ssh
d7b4b6
index 63c95456e..d6404600c 100755
d7b4b6
--- a/utils/oscap-ssh
d7b4b6
+++ b/utils/oscap-ssh
d7b4b6
@@ -80,11 +80,37 @@ function usage()
d7b4b6
     echo "specific option for oscap-ssh (must be first argument):"
d7b4b6
     echo "  --sudo"
d7b4b6
     echo
d7b4b6
+    echo "To supply additional options to ssh/scp, define the SSH_ADDITIONAL_OPTIONS variable"
d7b4b6
+    echo "For instance, to ignore known hosts records, define SSH_ADDITIONAL_OPTIONS='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'"
d7b4b6
+    echo
d7b4b6
+    echo "specific option for oscap-ssh (must be first argument):"
d7b4b6
+    echo
d7b4b6
     echo "See \`man oscap\` to learn more about semantics of these options."
d7b4b6
 }
d7b4b6
 
d7b4b6
 OSCAP_SUDO=""
d7b4b6
-SSH_ADDITIONAL_ARGS=""
d7b4b6
+# SSH_ADDITIONAL_OPTIONS may be defined in the calling shell
d7b4b6
+SSH_TTY_ALLOCATION_OPTION=""
d7b4b6
+
d7b4b6
+# $1: The SSH command.
d7b4b6
+# $2: More of additional options (optional, space-separated string)
d7b4b6
+function ssh_execute_with_options {
d7b4b6
+    ssh -o ControlPath="$MASTER_SOCKET" $SSH_ADDITIONAL_OPTIONS $2 -p "$SSH_PORT" "$SSH_HOST" "$1"
d7b4b6
+}
d7b4b6
+
d7b4b6
+# $1: Local filename to copy
d7b4b6
+# $2: Remote destination
d7b4b6
+function scp_copy_to_temp_dir {
d7b4b6
+    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" $SSH_ADDITIONAL_OPTIONS "$1" "$SSH_HOST:$REMOTE_TEMP_DIR/$2"
d7b4b6
+}
d7b4b6
+
d7b4b6
+# $1: Remote filename to get
d7b4b6
+# $2: Local destination
d7b4b6
+function scp_retreive_from_temp_dir {
d7b4b6
+    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" $SSH_ADDITIONAL_OPTIONS "$SSH_HOST:$REMOTE_TEMP_DIR/$1" "$2"
d7b4b6
+}
d7b4b6
+
d7b4b6
+function sanity_check_arguments {
d7b4b6
 if [ $# -lt 1 ]; then
d7b4b6
     echo "No arguments provided."
d7b4b6
     usage
d7b4b6
@@ -95,7 +121,7 @@ elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
d7b4b6
 elif [ "$1" == "sudo" ] || [ "$1" == "--sudo" ]; then
d7b4b6
     OSCAP_SUDO="sudo"
d7b4b6
     # force pseudo-tty allocation so that users can type their password if necessary
d7b4b6
-    SSH_ADDITIONAL_ARGS="-t"
d7b4b6
+    SSH_TTY_ALLOCATION_OPTION="-t"
d7b4b6
     shift
d7b4b6
 fi
d7b4b6
 if [ $# -lt 2 ]; then
d7b4b6
@@ -103,38 +129,45 @@ if [ $# -lt 2 ]; then
d7b4b6
     usage
d7b4b6
     die
d7b4b6
 fi
d7b4b6
+}
d7b4b6
 
d7b4b6
-SSH_HOST="$1"
d7b4b6
-SSH_PORT="$2"
d7b4b6
-
d7b4b6
-if [ "$3" == "--v" ] || [ "$3" == "--version" ]; then
d7b4b6
+function check_oscap_arguments {
d7b4b6
+if [ "$1" == "--v" ] || [ "$1" == "--version" ]; then
d7b4b6
     true
d7b4b6
-elif [ "$3" == "-h" ] || [ "$3" == "--help" ]; then
d7b4b6
+elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
d7b4b6
     true
d7b4b6
-elif [ "$3" == "info" ]; then
d7b4b6
+elif [ "$1" == "info" ]; then
d7b4b6
     true
d7b4b6
-elif [ "$3 $4" == "xccdf eval" ]; then
d7b4b6
+elif [ "$1 $2" == "xccdf eval" ]; then
d7b4b6
     true
d7b4b6
-elif [ "$3 $4" == "oval eval" ]; then
d7b4b6
+elif [ "$1 $2" == "oval eval" ]; then
d7b4b6
     true
d7b4b6
-elif [ "$3 $4" == "oval collect" ]; then
d7b4b6
+elif [ "$1 $2" == "oval collect" ]; then
d7b4b6
     true
d7b4b6
 else
d7b4b6
     die "This script only supports '-h', '--help', '--v', '--version', 'info', 'xccdf eval', 'oval eval' and 'oval collect'."
d7b4b6
 fi
d7b4b6
+}
d7b4b6
+
d7b4b6
+sanity_check_arguments "$@"
d7b4b6
+
d7b4b6
+SSH_HOST="$1"
d7b4b6
+SSH_PORT="$2"
d7b4b6
 
d7b4b6
 shift 2
d7b4b6
 
d7b4b6
+check_oscap_arguments "$@"
d7b4b6
+
d7b4b6
 MASTER_SOCKET_DIR=$(mktemp -d)
d7b4b6
 MASTER_SOCKET="$MASTER_SOCKET_DIR/ssh_socket"
d7b4b6
 
d7b4b6
 echo "Connecting to '$SSH_HOST' on port '$SSH_PORT'..."
d7b4b6
-ssh -M -f -N -o ServerAliveInterval=60 -o ControlPath="$MASTER_SOCKET" -p "$SSH_PORT" "$SSH_HOST" || die "Failed to connect!"
d7b4b6
+ssh -M -f -N -o ServerAliveInterval=60 -o ControlPath="$MASTER_SOCKET" -p "$SSH_PORT" $SSH_ADDITIONAL_OPTIONS "$SSH_HOST" || die "Failed to connect!"
d7b4b6
 echo "Connected!"
d7b4b6
 
d7b4b6
-REMOTE_TEMP_DIR=$(ssh -o ControlPath="$MASTER_SOCKET" -p "$SSH_PORT" "$SSH_HOST" mktemp -d) || die "Failed to create remote temporary directory!"
d7b4b6
+REMOTE_TEMP_DIR=$(ssh_execute_with_options "mktemp -d") || die "Failed to create remote temporary directory!"
d7b4b6
 
d7b4b6
-args=("$@")
d7b4b6
+oscap_args=("$@")
d7b4b6
 
d7b4b6
 LOCAL_CONTENT_PATH=""
d7b4b6
 LOCAL_TAILORING_PATH=""
d7b4b6
@@ -151,38 +184,38 @@ OVAL_RESULTS=""
d7b4b6
 for i in $(seq 0 `expr $# - 1`); do
d7b4b6
     let j=i+1
d7b4b6
 
d7b4b6
-    case "${args[i]}" in
d7b4b6
+    case "${oscap_args[i]}" in
d7b4b6
     ("--tailoring-file")
d7b4b6
-        LOCAL_TAILORING_PATH=${args[j]}
d7b4b6
-        args[j]="$REMOTE_TEMP_DIR/tailoring.xml"
d7b4b6
+        LOCAL_TAILORING_PATH=${oscap_args[j]}
d7b4b6
+        oscap_args[j]="$REMOTE_TEMP_DIR/tailoring.xml"
d7b4b6
       ;;
d7b4b6
     ("--cpe")
d7b4b6
-        LOCAL_CPE_PATH=${args[j]}
d7b4b6
-        args[j]="$REMOTE_TEMP_DIR/cpe.xml"
d7b4b6
+        LOCAL_CPE_PATH=${oscap_args[j]}
d7b4b6
+        oscap_args[j]="$REMOTE_TEMP_DIR/cpe.xml"
d7b4b6
       ;;
d7b4b6
     ("--variables")
d7b4b6
-        LOCAL_VARIABLES_PATH=${args[j]}
d7b4b6
-        args[j]="$REMOTE_TEMP_DIR/variables.xml"
d7b4b6
+        LOCAL_VARIABLES_PATH=${oscap_args[j]}
d7b4b6
+        oscap_args[j]="$REMOTE_TEMP_DIR/variables.xml"
d7b4b6
       ;;
d7b4b6
     ("--directives")
d7b4b6
-        LOCAL_DIRECTIVES_PATH=${args[j]}
d7b4b6
-        args[j]="$REMOTE_TEMP_DIR/directives.xml"
d7b4b6
+        LOCAL_DIRECTIVES_PATH=${oscap_args[j]}
d7b4b6
+        oscap_args[j]="$REMOTE_TEMP_DIR/directives.xml"
d7b4b6
       ;;
d7b4b6
     ("--results")
d7b4b6
-        TARGET_RESULTS=${args[j]}
d7b4b6
-        args[j]="$REMOTE_TEMP_DIR/results.xml"
d7b4b6
+        TARGET_RESULTS=${oscap_args[j]}
d7b4b6
+        oscap_args[j]="$REMOTE_TEMP_DIR/results.xml"
d7b4b6
       ;;
d7b4b6
     ("--results-arf")
d7b4b6
-        TARGET_RESULTS_ARF=${args[j]}
d7b4b6
-        args[j]="$REMOTE_TEMP_DIR/results-arf.xml"
d7b4b6
+        TARGET_RESULTS_ARF=${oscap_args[j]}
d7b4b6
+        oscap_args[j]="$REMOTE_TEMP_DIR/results-arf.xml"
d7b4b6
       ;;
d7b4b6
     ("--report")
d7b4b6
-        TARGET_REPORT=${args[j]}
d7b4b6
-        args[j]="$REMOTE_TEMP_DIR/report.html"
d7b4b6
+        TARGET_REPORT=${oscap_args[j]}
d7b4b6
+        oscap_args[j]="$REMOTE_TEMP_DIR/report.html"
d7b4b6
       ;;
d7b4b6
     ("--syschar")
d7b4b6
-        TARGET_SYSCHAR=${args[j]}
d7b4b6
-        args[j]="$REMOTE_TEMP_DIR/syschar.xml"
d7b4b6
+        TARGET_SYSCHAR=${oscap_args[j]}
d7b4b6
+        oscap_args[j]="$REMOTE_TEMP_DIR/syschar.xml"
d7b4b6
       ;;
d7b4b6
     ("--oval-results")
d7b4b6
         OVAL_RESULTS="yes"
d7b4b6
@@ -194,8 +227,8 @@ done
d7b4b6
 
d7b4b6
 if [ "$1" != "--v" ] && [ "$1" != "--version" ] && [ "$1" != "-h" ] && [ "$1" != "--help" ]; then
d7b4b6
     # Last argument should be the content path
d7b4b6
-    LOCAL_CONTENT_PATH="${args[`expr $# - 1`]}"
d7b4b6
-    args[`expr $# - 1`]="$REMOTE_TEMP_DIR/input.xml"
d7b4b6
+    LOCAL_CONTENT_PATH="${oscap_args[`expr $# - 1`]}"
d7b4b6
+    oscap_args[`expr $# - 1`]="$REMOTE_TEMP_DIR/input.xml"
d7b4b6
 fi
d7b4b6
 
d7b4b6
 [ "$LOCAL_CONTENT_PATH" == "" ] || [ -f "$LOCAL_CONTENT_PATH" ] || die "Expected the last argument to be an input file, '$LOCAL_CONTENT_PATH' isn't a valid file path or the file doesn't exist!"
d7b4b6
@@ -206,54 +239,54 @@ fi
d7b4b6
 
d7b4b6
 if [ "$LOCAL_CONTENT_PATH" != "" ]; then
d7b4b6
     echo "Copying input file '$LOCAL_CONTENT_PATH' to remote working directory '$REMOTE_TEMP_DIR'..."
d7b4b6
-    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" "$LOCAL_CONTENT_PATH" "$SSH_HOST:$REMOTE_TEMP_DIR/input.xml" || die "Failed to copy input file to remote temporary directory!"
d7b4b6
+    scp_copy_to_temp_dir "$LOCAL_CONTENT_PATH" input.xml || die "Failed to copy input file to remote temporary directory!"
d7b4b6
 fi
d7b4b6
 if [ "$LOCAL_TAILORING_PATH" != "" ]; then
d7b4b6
     echo "Copying tailoring file '$LOCAL_TAILORING_PATH' to remote working directory '$REMOTE_TEMP_DIR'..."
d7b4b6
-    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" "$LOCAL_TAILORING_PATH" "$SSH_HOST:$REMOTE_TEMP_DIR/tailoring.xml" || die "Failed to copy tailoring file to remote temporary directory!"
d7b4b6
+    scp_copy_to_temp_dir "$LOCAL_TAILORING_PATH" tailoring.xml || die "Failed to copy tailoring file to remote temporary directory!"
d7b4b6
 fi
d7b4b6
 if [ "$LOCAL_CPE_PATH" != "" ]; then
d7b4b6
     echo "Copying CPE file '$LOCAL_CPE_PATH' to remote working directory '$REMOTE_TEMP_DIR'..."
d7b4b6
-    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" "$LOCAL_CPE_PATH" "$SSH_HOST:$REMOTE_TEMP_DIR/cpe.xml" || die "Failed to copy CPE file to remote temporary directory!"
d7b4b6
+    scp_copy_to_temp_dir "$LOCAL_CPE_PATH" cpe.xml || die "Failed to copy CPE file to remote temporary directory!"
d7b4b6
 fi
d7b4b6
 if [ "$LOCAL_VARIABLES_PATH" != "" ]; then
d7b4b6
     echo "Copying OVAL variables file '$LOCAL_VARIABLES_PATH' to remote working directory '$REMOTE_TEMP_DIR'..."
d7b4b6
-    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" "$LOCAL_VARIABLES_PATH" "$SSH_HOST:$REMOTE_TEMP_DIR/variables.xml" || die "Failed to copy OVAL variables file to remote temporary directory!"
d7b4b6
+    scp_copy_to_temp_dir "$LOCAL_VARIABLES_PATH" variables.xml || die "Failed to copy OVAL variables file to remote temporary directory!"
d7b4b6
 fi
d7b4b6
 if [ "$LOCAL_DIRECTIVES_PATH" != "" ]; then
d7b4b6
     echo "Copying OVAL directives file '$LOCAL_DIRECTIVES_PATH' to remote working directory '$REMOTE_TEMP_DIR'..."
d7b4b6
-    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" "$LOCAL_DIRECTIVES_PATH" "$SSH_HOST:$REMOTE_TEMP_DIR/directives.xml" || die "Failed to copy OVAL directives file to remote temporary directory!"
d7b4b6
+    scp_copy_to_temp_dir "$LOCAL_DIRECTIVES_PATH" directives.xml || die "Failed to copy OVAL directives file to remote temporary directory!"
d7b4b6
 fi
d7b4b6
 
d7b4b6
 echo "Starting the evaluation..."
d7b4b6
 # changing directory because of --oval-results support. oval results files are
d7b4b6
 # dumped into PWD, and we can't be sure by the file names - we need controlled
d7b4b6
 # environment
d7b4b6
-ssh -o ControlPath="$MASTER_SOCKET" $SSH_ADDITIONAL_ARGS -p "$SSH_PORT" "$SSH_HOST" "cd $REMOTE_TEMP_DIR; $OSCAP_SUDO oscap ${args[*]}"
d7b4b6
+ssh_execute_with_options "cd $REMOTE_TEMP_DIR; $OSCAP_SUDO oscap ${oscap_args[*]}" "$SSH_TTY_ALLOCATION_OPTION"
d7b4b6
 OSCAP_EXIT_CODE=$?
d7b4b6
 echo "oscap exit code: $OSCAP_EXIT_CODE"
d7b4b6
 
d7b4b6
 echo "Copying back requested files..."
d7b4b6
 if [ "$TARGET_RESULTS" != "" ]; then
d7b4b6
-    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" "$SSH_HOST:$REMOTE_TEMP_DIR/results.xml" "$TARGET_RESULTS" || die "Failed to copy the results file back to local machine!"
d7b4b6
+    scp_retreive_from_temp_dir results.xml "$TARGET_RESULTS" || die "Failed to copy the results file back to local machine!"
d7b4b6
 fi
d7b4b6
 if [ "$TARGET_RESULTS_ARF" != "" ]; then
d7b4b6
-    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" "$SSH_HOST:$REMOTE_TEMP_DIR/results-arf.xml" "$TARGET_RESULTS_ARF" || die "Failed to copy the ARF file back to local machine!"
d7b4b6
+    scp_retreive_from_temp_dir results-arf.xml "$TARGET_RESULTS_ARF" || die "Failed to copy the ARF file back to local machine!"
d7b4b6
 fi
d7b4b6
 if [ "$TARGET_REPORT" != "" ]; then
d7b4b6
-    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" "$SSH_HOST:$REMOTE_TEMP_DIR/report.html" "$TARGET_REPORT" || die "Failed to copy the HTML report back to local machine!"
d7b4b6
+    scp_retreive_from_temp_dir report.html "$TARGET_REPORT" || die "Failed to copy the HTML report back to local machine!"
d7b4b6
 fi
d7b4b6
 if [ "$TARGET_SYSCHAR" != "" ]; then
d7b4b6
-    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" "$SSH_HOST:$REMOTE_TEMP_DIR/syschar.xml" "$TARGET_SYSCHAR" || die "Failed to copy the OVAL syschar file back to local machine!"
d7b4b6
+    scp_retreive_from_temp_dir syschar.xml "$TARGET_SYSCHAR" || die "Failed to copy the OVAL syschar file back to local machine!"
d7b4b6
 fi
d7b4b6
 if [ "$OVAL_RESULTS" == "yes" ]; then
d7b4b6
-    scp -o ControlPath="$MASTER_SOCKET" -P "$SSH_PORT" "$SSH_HOST:$REMOTE_TEMP_DIR/*.result.xml" "./" || die "Failed to copy OVAL result files back to local machine!"
d7b4b6
+    scp_retreive_from_temp_dir '*.result.xml' "./" || die "Failed to copy OVAL result files back to local machine!"
d7b4b6
 fi
d7b4b6
 
d7b4b6
 echo "Removing remote temporary directory..."
d7b4b6
-ssh -o ControlPath="$MASTER_SOCKET" -p "$SSH_PORT" "$SSH_HOST" "rm -r $REMOTE_TEMP_DIR" || die "Failed to remove remote temporary directory!"
d7b4b6
+ssh_execute_with_options "rm -r $REMOTE_TEMP_DIR" || die "Failed to remove remote temporary directory!"
d7b4b6
 echo "Disconnecting ssh and removing master ssh socket directory..."
d7b4b6
-ssh -o ControlPath="$MASTER_SOCKET" -p "$SSH_PORT" "$SSH_HOST" -O exit || die "Failed to disconnect!"
d7b4b6
+ssh -o ControlPath="$MASTER_SOCKET" $SSH_ADDITIONAL_OPTIONS -p "$SSH_PORT" "$SSH_HOST" -O exit || die "Failed to disconnect!"
d7b4b6
 rm -r "$MASTER_SOCKET_DIR" || die "Failed to remove local master SSH socket directory!"
d7b4b6
 
d7b4b6
 exit $OSCAP_EXIT_CODE
d7b4b6
diff --git a/utils/oscap-ssh.8 b/utils/oscap-ssh.8
d7b4b6
index 874bf31bf..38d96e76f 100644
d7b4b6
--- a/utils/oscap-ssh.8
d7b4b6
+++ b/utils/oscap-ssh.8
d7b4b6
@@ -60,10 +60,16 @@ Supported options are:
d7b4b6
 Specific option for oscap-ssh (must be first argument):
d7b4b6
   --sudo
d7b4b6
 
d7b4b6
-.SH EXEMPLARY USAGE
d7b4b6
+.SS Environment variables
d7b4b6
+oscap-ssh checks out the SSH_ADDITIONAL_OPTIONS environment variable, and pastes its contents into the command-line of ssh to the location where options are expected.
d7b4b6
+Supply the variable in form of a string that corresponds to a section of the ssh command-line and that consists of options you want to pass.
d7b4b6
+
d7b4b6
+.SH EXAMPLE USAGE
d7b4b6
 .SS Simple XCCDF evaluation
d7b4b6
 The following command evaluates a remote Fedora machine as root. HTML report is written out as report.html on the local machine. Can be executed from any machine that has ssh, scp and bash. The local machine does not need to have openscap installed.
d7b4b6
+It also uses the SSH_ADDITIONAL_OPTIONS variable to configure ssh in such way that contents of the known_hosts file are ignored.
d7b4b6
 
d7b4b6
+$ export SSH_ADDITIONAL_OPTIONS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
d7b4b6
 $ oscap-ssh root@192.168.1.13 22 xccdf eval --profile xccdf_org.ssgproject.content_profile_common --report report.html /usr/share/xml/scap/ssg/content/ssg-fedora-ds.xml
d7b4b6
 
d7b4b6
 .SS XCCDF Evaluation with tailoring file
d7b4b6
d7b4b6
From be470f5c51279efafa384ec8f28ca1e0a5c447ed Mon Sep 17 00:00:00 2001
d7b4b6
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
d7b4b6
Date: Tue, 21 Aug 2018 12:24:24 +0200
d7b4b6
Subject: [PATCH 2/3] Fixed indentation inside functions.
d7b4b6
d7b4b6
---
d7b4b6
 utils/oscap-ssh | 66 ++++++++++++++++++++++++-------------------------
d7b4b6
 1 file changed, 33 insertions(+), 33 deletions(-)
d7b4b6
d7b4b6
diff --git a/utils/oscap-ssh b/utils/oscap-ssh
d7b4b6
index d6404600c..08bc698d2 100755
d7b4b6
--- a/utils/oscap-ssh
d7b4b6
+++ b/utils/oscap-ssh
d7b4b6
@@ -111,42 +111,42 @@ function scp_retreive_from_temp_dir {
d7b4b6
 }
d7b4b6
 
d7b4b6
 function sanity_check_arguments {
d7b4b6
-if [ $# -lt 1 ]; then
d7b4b6
-    echo "No arguments provided."
d7b4b6
-    usage
d7b4b6
-    die
d7b4b6
-elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
d7b4b6
-    usage
d7b4b6
-    die
d7b4b6
-elif [ "$1" == "sudo" ] || [ "$1" == "--sudo" ]; then
d7b4b6
-    OSCAP_SUDO="sudo"
d7b4b6
-    # force pseudo-tty allocation so that users can type their password if necessary
d7b4b6
-    SSH_TTY_ALLOCATION_OPTION="-t"
d7b4b6
-    shift
d7b4b6
-fi
d7b4b6
-if [ $# -lt 2 ]; then
d7b4b6
-    echo "Missing ssh host and ssh port."
d7b4b6
-    usage
d7b4b6
-    die
d7b4b6
-fi
d7b4b6
+    if [ $# -lt 1 ]; then
d7b4b6
+        echo "No arguments provided."
d7b4b6
+        usage
d7b4b6
+        die
d7b4b6
+    elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
d7b4b6
+        usage
d7b4b6
+        die
d7b4b6
+    elif [ "$1" == "sudo" ] || [ "$1" == "--sudo" ]; then
d7b4b6
+        OSCAP_SUDO="sudo"
d7b4b6
+        # force pseudo-tty allocation so that users can type their password if necessary
d7b4b6
+        SSH_TTY_ALLOCATION_OPTION="-t"
d7b4b6
+        shift
d7b4b6
+    fi
d7b4b6
+    if [ $# -lt 2 ]; then
d7b4b6
+        echo "Missing ssh host and ssh port."
d7b4b6
+        usage
d7b4b6
+        die
d7b4b6
+    fi
d7b4b6
 }
d7b4b6
 
d7b4b6
 function check_oscap_arguments {
d7b4b6
-if [ "$1" == "--v" ] || [ "$1" == "--version" ]; then
d7b4b6
-    true
d7b4b6
-elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
d7b4b6
-    true
d7b4b6
-elif [ "$1" == "info" ]; then
d7b4b6
-    true
d7b4b6
-elif [ "$1 $2" == "xccdf eval" ]; then
d7b4b6
-    true
d7b4b6
-elif [ "$1 $2" == "oval eval" ]; then
d7b4b6
-    true
d7b4b6
-elif [ "$1 $2" == "oval collect" ]; then
d7b4b6
-    true
d7b4b6
-else
d7b4b6
-    die "This script only supports '-h', '--help', '--v', '--version', 'info', 'xccdf eval', 'oval eval' and 'oval collect'."
d7b4b6
-fi
d7b4b6
+    if [ "$1" == "--v" ] || [ "$1" == "--version" ]; then
d7b4b6
+        true
d7b4b6
+    elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
d7b4b6
+        true
d7b4b6
+    elif [ "$1" == "info" ]; then
d7b4b6
+        true
d7b4b6
+    elif [ "$1 $2" == "xccdf eval" ]; then
d7b4b6
+        true
d7b4b6
+    elif [ "$1 $2" == "oval eval" ]; then
d7b4b6
+        true
d7b4b6
+    elif [ "$1 $2" == "oval collect" ]; then
d7b4b6
+        true
d7b4b6
+    else
d7b4b6
+        die "This script only supports '-h', '--help', '--v', '--version', 'info', 'xccdf eval', 'oval eval' and 'oval collect'."
d7b4b6
+    fi
d7b4b6
 }
d7b4b6
 
d7b4b6
 sanity_check_arguments "$@"
d7b4b6
d7b4b6
From 78215f62d30fe3c9851d792a4f6e239f045342c1 Mon Sep 17 00:00:00 2001
d7b4b6
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
d7b4b6
Date: Mon, 27 Aug 2018 15:34:18 +0200
d7b4b6
Subject: [PATCH 3/3] Improved the code style.
d7b4b6
d7b4b6
* Rewritten more of ssh calls into functions.
d7b4b6
* Improved the option check message.
d7b4b6
---
d7b4b6
 utils/oscap-ssh | 21 +++++++++++++--------
d7b4b6
 1 file changed, 13 insertions(+), 8 deletions(-)
d7b4b6
d7b4b6
diff --git a/utils/oscap-ssh b/utils/oscap-ssh
d7b4b6
index 08bc698d2..ee6eb9c81 100755
d7b4b6
--- a/utils/oscap-ssh
d7b4b6
+++ b/utils/oscap-ssh
d7b4b6
@@ -92,9 +92,14 @@ OSCAP_SUDO=""
d7b4b6
 # SSH_ADDITIONAL_OPTIONS may be defined in the calling shell
d7b4b6
 SSH_TTY_ALLOCATION_OPTION=""
d7b4b6
 
d7b4b6
-# $1: The SSH command.
d7b4b6
-# $2: More of additional options (optional, space-separated string)
d7b4b6
+# $1, $2, ... SSH options (pass them as separate arguments)
d7b4b6
 function ssh_execute_with_options {
d7b4b6
+    ssh -o ControlPath="$MASTER_SOCKET" $SSH_ADDITIONAL_OPTIONS "$@" -p "$SSH_PORT" "$SSH_HOST"
d7b4b6
+}
d7b4b6
+
d7b4b6
+# $1: The SSH command.
d7b4b6
+# $2: More of additional options (optional, pass one space-separated string)
d7b4b6
+function ssh_execute_with_command_and_options {
d7b4b6
     ssh -o ControlPath="$MASTER_SOCKET" $SSH_ADDITIONAL_OPTIONS $2 -p "$SSH_PORT" "$SSH_HOST" "$1"
d7b4b6
 }
d7b4b6
 
d7b4b6
@@ -145,7 +150,7 @@ function check_oscap_arguments {
d7b4b6
     elif [ "$1 $2" == "oval collect" ]; then
d7b4b6
         true
d7b4b6
     else
d7b4b6
-        die "This script only supports '-h', '--help', '--v', '--version', 'info', 'xccdf eval', 'oval eval' and 'oval collect'."
d7b4b6
+        die "This script only supports 'sudo' as first argument, '-h', '--help', '--v', '--version', 'info', 'xccdf eval', 'oval eval' and 'oval collect'."
d7b4b6
     fi
d7b4b6
 }
d7b4b6
 
d7b4b6
@@ -162,10 +167,10 @@ MASTER_SOCKET_DIR=$(mktemp -d)
d7b4b6
 MASTER_SOCKET="$MASTER_SOCKET_DIR/ssh_socket"
d7b4b6
 
d7b4b6
 echo "Connecting to '$SSH_HOST' on port '$SSH_PORT'..."
d7b4b6
-ssh -M -f -N -o ServerAliveInterval=60 -o ControlPath="$MASTER_SOCKET" -p "$SSH_PORT" $SSH_ADDITIONAL_OPTIONS "$SSH_HOST" || die "Failed to connect!"
d7b4b6
+ssh_execute_with_options -M -f -N -o ServerAliveInterval=60 || die "Failed to connect!"
d7b4b6
 echo "Connected!"
d7b4b6
 
d7b4b6
-REMOTE_TEMP_DIR=$(ssh_execute_with_options "mktemp -d") || die "Failed to create remote temporary directory!"
d7b4b6
+REMOTE_TEMP_DIR=$(ssh_execute_with_command_and_options "mktemp -d") || die "Failed to create remote temporary directory!"
d7b4b6
 
d7b4b6
 oscap_args=("$@")
d7b4b6
 
d7b4b6
@@ -262,7 +267,7 @@ echo "Starting the evaluation..."
d7b4b6
 # changing directory because of --oval-results support. oval results files are
d7b4b6
 # dumped into PWD, and we can't be sure by the file names - we need controlled
d7b4b6
 # environment
d7b4b6
-ssh_execute_with_options "cd $REMOTE_TEMP_DIR; $OSCAP_SUDO oscap ${oscap_args[*]}" "$SSH_TTY_ALLOCATION_OPTION"
d7b4b6
+ssh_execute_with_command_and_options "cd $REMOTE_TEMP_DIR; $OSCAP_SUDO oscap ${oscap_args[*]}" "$SSH_TTY_ALLOCATION_OPTION"
d7b4b6
 OSCAP_EXIT_CODE=$?
d7b4b6
 echo "oscap exit code: $OSCAP_EXIT_CODE"
d7b4b6
 
d7b4b6
@@ -284,9 +289,9 @@ if [ "$OVAL_RESULTS" == "yes" ]; then
d7b4b6
 fi
d7b4b6
 
d7b4b6
 echo "Removing remote temporary directory..."
d7b4b6
-ssh_execute_with_options "rm -r $REMOTE_TEMP_DIR" || die "Failed to remove remote temporary directory!"
d7b4b6
+ssh_execute_with_command_and_options "rm -r $REMOTE_TEMP_DIR" || die "Failed to remove remote temporary directory!"
d7b4b6
 echo "Disconnecting ssh and removing master ssh socket directory..."
d7b4b6
-ssh -o ControlPath="$MASTER_SOCKET" $SSH_ADDITIONAL_OPTIONS -p "$SSH_PORT" "$SSH_HOST" -O exit || die "Failed to disconnect!"
d7b4b6
+ssh_execute_with_options -O exit || die "Failed to disconnect!"
d7b4b6
 rm -r "$MASTER_SOCKET_DIR" || die "Failed to remove local master SSH socket directory!"
d7b4b6
 
d7b4b6
 exit $OSCAP_EXIT_CODE