Debian Codegraph @

utils/oscap-ssh

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-ssh -- Tool for running oscap over SSH and collecting results."
35 echo
36 echo "Usage:"
37 echo
38 echo "$ oscap-ssh user@host 22 info INPUT_CONTENT"
39 echo "$ oscap-ssh user@host 22 xccdf eval [options] INPUT_CONTENT"
40 echo
41 echo "Only source data streams are supported as INPUT_CONTENT!"
42 echo
43 echo "supported oscap xccdf eval options are:"
44 echo " --profile"
45 echo " --tailoring-file"
46 echo " --tailoring-id"
47 echo " --cpe (external OVAL dependencies are not supported yet!)"
48 # --check-engine-results is not supported
49 # use --results-arf instead
50 echo " --oval-results"
51 echo " --results"
52 echo " --results-arf"
53 echo " --report"
54 echo " --skip-validation"
55 echo " --fetch-remote-resources"
56 echo " --local-files"
57 echo " --progress"
58 echo " --datastream-id"
59 echo " --xccdf-id"
60 echo " --benchmark-id"
61 echo " --remediate"
62 echo
63 echo "$ oscap-ssh user@host 22 oval eval [options] INPUT_CONTENT"
64 echo
65 echo "supported oscap oval eval options are:"
66 echo " --id"
67 echo " --variables"
68 echo " --directives"
69 echo " --results"
70 echo " --report"
71 echo " --skip-validation"
72 echo " --datastream-id"
73 echo " --oval-id"
74 echo
75 echo "$ oscap-ssh user@host 22 oval collect [options] INPUT_CONTENT"
76 echo
77 echo "supported oscap oval collect options are:"
78 echo " --id"
79 echo " --syschar"
80 echo " --variables"
81 echo " --skip-validation"
82 echo
83 echo "specific option for oscap-ssh (must be first argument):"
84 echo " --sudo"
85 echo
86 echo "To supply additional options to ssh/scp, define the SSH_ADDITIONAL_OPTIONS variable"
87 echo "For instance, to ignore known hosts records, define SSH_ADDITIONAL_OPTIONS='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'"
88 echo
89 echo "specific option for oscap-ssh (must be first argument):"
90 echo
91 echo "See \`man oscap\` to learn more about semantics of these options."
92 }
93
94 # $1, $2, ... SSH options (pass them as separate arguments)
95 function ssh_execute_with_options {
96 ssh -o ControlPath="$CONTROL_SOCKET" $SSH_ADDITIONAL_OPTIONS "$@" -p "$SSH_PORT" "$SSH_HOST"
97 }
98
99 # $1: The SSH command.
100 # $2: More of additional options (optional, pass one space-separated string)
101 function ssh_execute_with_command_and_options {
102 ssh -o ControlPath="$CONTROL_SOCKET" $SSH_ADDITIONAL_OPTIONS $2 -p "$SSH_PORT" "$SSH_HOST" "$1"
103 }
104
105 # $1: Local filename to copy
106 # $2: Remote destination
107 function scp_copy_to_temp_dir {
108 scp -o ControlPath="$CONTROL_SOCKET" -P "$SSH_PORT" $SSH_ADDITIONAL_OPTIONS "$1" "$SSH_HOST:$REMOTE_TEMP_DIR/$2"
109 }
110
111 # $1: Local directory name to copy
112 # $2: Remote destination
113 function scp_copy_dir_to_temp_dir {
114 scp -r -o ControlPath="$CONTROL_SOCKET" -P "$SSH_PORT" $SSH_ADDITIONAL_OPTIONS "$1" "$SSH_HOST:$REMOTE_TEMP_DIR/$2"
115 }
116
117 # $1: Remote filename to get
118 # $2: Local destination
119 function scp_retreive_from_temp_dir {
120 scp -o ControlPath="$CONTROL_SOCKET" -P "$SSH_PORT" $SSH_ADDITIONAL_OPTIONS "$SSH_HOST:$REMOTE_TEMP_DIR/$1" "$2"
121 }
122
123 # $1: The name of the array holding command elements
124 # Returns: String, where individual command components are double-quoted, so they are not interpreted by the shell.
125 # For example, an array ('-p' '(all)') will be transformed to "\"-p\" \"(all)\"", so after the shell expansion, it will end up as "-p" "(all)".
126 function command_array_to_string {
127 eval "printf '\"%s\" ' \"\${$1[@]}\""
128 }
129
130 function first_argument_is_sudo {
131 [ "$1" == "sudo" ] || [ "$1" == "--sudo" ]
132 return $?
133 }
134
135 function sanity_check_arguments {
136 if [ $# -lt 1 ]; then
137 invalid "No arguments provided."
138 elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
139 usage
140 exit 0
141 elif first_argument_is_sudo "$@"; then
142 OSCAP_SUDO="sudo"
143 # force pseudo-tty allocation so that users can type their password if necessary
144 SSH_TTY_ALLOCATION_OPTION="-t"
145 shift
146 fi
147 if [ $# -lt 2 ]; then
148 invalid "Missing ssh host and ssh port."
149 fi
150 }
151
152 function check_oscap_arguments {
153 if [ "$1" == "--v" ] || [ "$1" == "--version" ]; then
154 true
155 elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
156 true
157 elif [ "$1" == "info" ]; then
158 true
159 elif [ "$1 $2" == "xccdf eval" ]; then
160 true
161 elif [ "$1 $2" == "oval eval" ]; then
162 true
163 elif [ "$1 $2" == "oval collect" ]; then
164 true
165 else
166 die "This script only supports 'sudo' as first argument, '-h', '--help', '--v', '--version', 'info', 'xccdf eval', 'oval eval' and 'oval collect'."
167 fi
168 }
169
170
171 hash ssh 2> /dev/null || die "Cannot find ssh, please install the OpenSSH client."
172 hash scp 2> /dev/null || die "Cannot find scp, please install the OpenSSH client."
173 hash mktemp 2> /dev/null || die "Cannot find mktemp, please install coreutils."
174
175
176 OSCAP_SUDO=""
177 # SSH_ADDITIONAL_OPTIONS may be defined in the calling shell
178 SSH_TTY_ALLOCATION_OPTION=""
179
180 sanity_check_arguments "$@"
181 first_argument_is_sudo "$@" && shift
182
183 SSH_HOST="$1"
184 SSH_PORT="$2"
185
186 shift 2
187
188 check_oscap_arguments "$@"
189
190 CONTROL_SOCKET_DIR=$(mktemp -d)
191 CONTROL_SOCKET="$CONTROL_SOCKET_DIR/ssh_socket"
192
193 echo "Connecting to '$SSH_HOST' on port '$SSH_PORT'..."
194 ssh_execute_with_options -M -f -N -o ServerAliveInterval=60 || die "Failed to connect!"
195 echo "Connected!"
196
197 REMOTE_TEMP_DIR=$(ssh_execute_with_command_and_options "mktemp -d") || die "Failed to create remote temporary directory!"
198
199 oscap_args=("$@")
200
201 LOCAL_CONTENT_PATH=""
202 LOCAL_TAILORING_PATH=""
203 LOCAL_LOCAL_FILES_PATH=""
204 LOCAL_CPE_PATH=""
205 LOCAL_VARIABLES_PATH=""
206 LOCAL_DIRECTIVES_PATH=""
207 TARGET_RESULTS=""
208 TARGET_RESULTS_ARF=""
209 TARGET_REPORT=""
210 TARGET_SYSCHAR=""
211 OVAL_RESULTS=""
212 STIG_RESULTS=""
213
214 # We have to rewrite various paths to a remote temp dir
215 for i in $(seq 0 `expr $# - 1`); do
216 let j=i+1
217
218 case "${oscap_args[i]}" in
219 ("--tailoring-file")
220 LOCAL_TAILORING_PATH=${oscap_args[j]}
221 oscap_args[j]="$REMOTE_TEMP_DIR/tailoring.xml"
222 ;;
223 ("--stig-viewer")
224 STIG_RESULTS=${oscap_args[j]}
225 oscap_args[j]="$REMOTE_TEMP_DIR/stig_results.xml"
226 ;;
227 ("--local-files")
228 LOCAL_LOCAL_FILES_PATH=${oscap_args[j]}
229 oscap_args[j]="$REMOTE_TEMP_DIR/local_files"
230 ;;
231 ("--cpe")
232 LOCAL_CPE_PATH=${oscap_args[j]}
233 oscap_args[j]="$REMOTE_TEMP_DIR/cpe.xml"
234 ;;
235 ("--variables")
236 LOCAL_VARIABLES_PATH=${oscap_args[j]}
237 oscap_args[j]="$REMOTE_TEMP_DIR/variables.xml"
238 ;;
239 ("--directives")
240 LOCAL_DIRECTIVES_PATH=${oscap_args[j]}
241 oscap_args[j]="$REMOTE_TEMP_DIR/directives.xml"
242 ;;
243 ("--results")
244 TARGET_RESULTS=${oscap_args[j]}
245 oscap_args[j]="$REMOTE_TEMP_DIR/results.xml"
246 ;;
247 ("--results-arf")
248 TARGET_RESULTS_ARF=${oscap_args[j]}
249 oscap_args[j]="$REMOTE_TEMP_DIR/results-arf.xml"
250 ;;
251 ("--report")
252 TARGET_REPORT=${oscap_args[j]}
253 oscap_args[j]="$REMOTE_TEMP_DIR/report.html"
254 ;;
255 ("--syschar")
256 TARGET_SYSCHAR=${oscap_args[j]}
257 oscap_args[j]="$REMOTE_TEMP_DIR/syschar.xml"
258 ;;
259 ("--oval-results")
260 OVAL_RESULTS="yes"
261 ;;
262 *)
263 ;;
264 esac
265 done
266
267 if [ "$1" != "--v" ] && [ "$1" != "--version" ] && [ "$1" != "-h" ] && [ "$1" != "--help" ]; then
268 # Last argument should be the content path
269 LOCAL_CONTENT_PATH="${oscap_args[`expr $# - 1`]}"
270 oscap_args[`expr $# - 1`]="$REMOTE_TEMP_DIR/input.xml"
271 fi
272
273 [ "$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!"
274 [ "$LOCAL_TAILORING_PATH" == "" ] || [ -f "$LOCAL_TAILORING_PATH" ] || die "Tailoring file path '$LOCAL_TAILORING_PATH' isn't a valid file path or the file doesn't exist!"
275 [ "$LOCAL_LOCAL_FILES_PATH" == "" ] || [ -d "$LOCAL_LOCAL_FILES_PATH" ] || die "Directory '$LOCAL_LOCAL_FILES_PATH' isn't a valid directory path or the directory doesn't exist!"
276 [ "$LOCAL_CPE_PATH" == "" ] || [ -f "$LOCAL_CPE_PATH" ] || die "CPE file path '$LOCAL_CPE_PATH' isn't a valid file path or the file doesn't exist!"
277 [ "$LOCAL_VARIABLES_PATH" == "" ] || [ -f "$LOCAL_VARIABLES_PATH" ] || die "OVAL variables file path '$LOCAL_VARIABLES_PATH' isn't a valid file path or the file doesn't exist!"
278 [ "$LOCAL_DIRECTIVES_PATH" == "" ] || [ -f "$LOCAL_DIRECTIVES_PATH" ] || die "OVAL directives file path '$LOCAL_DIRECTIVES_PATH' isn't a valid file path or the file doesn't exist!"
279
280 if [ "$LOCAL_CONTENT_PATH" != "" ]; then
281 echo "Copying input file '$LOCAL_CONTENT_PATH' to remote working directory '$REMOTE_TEMP_DIR'..."
282 scp_copy_to_temp_dir "$LOCAL_CONTENT_PATH" input.xml || die "Failed to copy input file to remote temporary directory!"
283 fi
284 if [ "$LOCAL_TAILORING_PATH" != "" ]; then
285 echo "Copying tailoring file '$LOCAL_TAILORING_PATH' to remote working directory '$REMOTE_TEMP_DIR'..."
286 scp_copy_to_temp_dir "$LOCAL_TAILORING_PATH" tailoring.xml || die "Failed to copy tailoring file to remote temporary directory!"
287 fi
288 if [ "$LOCAL_LOCAL_FILES_PATH" != "" ]; then
289 echo "Copying directory '$LOCAL_LOCAL_FILES_PATH' to remote working directory '$REMOTE_TEMP_DIR'..."
290 scp_copy_dir_to_temp_dir "$LOCAL_LOCAL_FILES_PATH" local_files || die "Failed to copy directory $LOCAL_LOCAL_FILES_PATH to remote temporary directory!"
291 fi
292 if [ "$LOCAL_CPE_PATH" != "" ]; then
293 echo "Copying CPE file '$LOCAL_CPE_PATH' to remote working directory '$REMOTE_TEMP_DIR'..."
294 scp_copy_to_temp_dir "$LOCAL_CPE_PATH" cpe.xml || die "Failed to copy CPE file to remote temporary directory!"
295 fi
296 if [ "$LOCAL_VARIABLES_PATH" != "" ]; then
297 echo "Copying OVAL variables file '$LOCAL_VARIABLES_PATH' to remote working directory '$REMOTE_TEMP_DIR'..."
298 scp_copy_to_temp_dir "$LOCAL_VARIABLES_PATH" variables.xml || die "Failed to copy OVAL variables file to remote temporary directory!"
299 fi
300 if [ "$LOCAL_DIRECTIVES_PATH" != "" ]; then
301 echo "Copying OVAL directives file '$LOCAL_DIRECTIVES_PATH' to remote working directory '$REMOTE_TEMP_DIR'..."
302 scp_copy_to_temp_dir "$LOCAL_DIRECTIVES_PATH" directives.xml || die "Failed to copy OVAL directives file to remote temporary directory!"
303 fi
304
305 echo "Starting the evaluation..."
306 # changing directory because of --oval-results support. oval results files are
307 # dumped into PWD, and we can't be sure by the file names - we need controlled
308 # environment
309 if [ -z "$OSCAP_SUDO" ]; then
310 ssh_execute_with_command_and_options "cd $REMOTE_TEMP_DIR; oscap $(command_array_to_string oscap_args)" "$SSH_TTY_ALLOCATION_OPTION"
311 else
312 OSCAP_CMD="oscap $(command_array_to_string oscap_args); rc=\$?; chown \$SUDO_USER $REMOTE_TEMP_DIR/*; exit \$rc"
313 ssh_execute_with_command_and_options "cd $REMOTE_TEMP_DIR; $OSCAP_SUDO sh -c '$OSCAP_CMD'" "$SSH_TTY_ALLOCATION_OPTION"
314 fi
315 OSCAP_EXIT_CODE=$?
316 echo "oscap exit code: $OSCAP_EXIT_CODE"
317
318 echo "Copying back requested files..."
319 if [ "$TARGET_RESULTS" != "" ]; then
320 scp_retreive_from_temp_dir results.xml "$TARGET_RESULTS" || die "Failed to copy the results file back to local machine!"
321 fi
322 if [ "$STIG_RESULTS" != "" ]; then
323 scp_retreive_from_temp_dir stig_results.xml "$STIG_RESULTS" || die "Failed to copy the STIG results file back to local machine!"
324 fi
325 if [ "$TARGET_RESULTS_ARF" != "" ]; then
326 scp_retreive_from_temp_dir results-arf.xml "$TARGET_RESULTS_ARF" || die "Failed to copy the ARF file back to local machine!"
327 fi
328 if [ "$TARGET_REPORT" != "" ]; then
329 scp_retreive_from_temp_dir report.html "$TARGET_REPORT" || die "Failed to copy the HTML report back to local machine!"
330 fi
331 if [ "$TARGET_SYSCHAR" != "" ]; then
332 scp_retreive_from_temp_dir syschar.xml "$TARGET_SYSCHAR" || die "Failed to copy the OVAL syschar file back to local machine!"
333 fi
334 if [ "$OVAL_RESULTS" == "yes" ]; then
335 scp_retreive_from_temp_dir '*.result.xml' "./" || die "Failed to copy OVAL result files back to local machine!"
336 fi
337
338 echo "Removing remote temporary directory..."
339 ssh_execute_with_command_and_options "rm -r $REMOTE_TEMP_DIR" || die "Failed to remove remote temporary directory!"
340 echo "Disconnecting ssh and removing control ssh socket directory..."
341 ssh_execute_with_options -O exit || die "Failed to disconnect!"
342 rm -r "$CONTROL_SOCKET_DIR" || die "Failed to remove local control SSH socket directory!"
343
344 exit $OSCAP_EXIT_CODE