Imposing stricter checks on option parameters

This commit is contained in:
Thomas Schmitt 2019-12-07 15:35:41 +01:00
parent 51994a7571
commit ba505e86c9
1 changed files with 34 additions and 13 deletions

View File

@ -131,6 +131,31 @@ round_down_div_million() {
-e 's/[Tt]$/000000/'
}
## Check for harmless name or number in program argument
check_parameter() {
if test "$2" = "device_name"
then
if echo "$1" | grep '[^A-za-z0-9_/-]' >/dev/null
then
echo "SORRY: Given device name contains unexpected character. Ok: [A-za-z0-9_/-]" >&2
exit 12
fi
elif test "$2" = "image_file"
then
if echo "$1" | grep '[$`[*?<>|&!{\]' >/dev/null
then
echo "SORRY: Given image file name contains unexpected character. Not ok: "'[$`[*?<>|&!{\]' >&2
exit 15
fi
else
if echo "$1" | grep -v '^[0-9][0-9]*[0-9MGTmgt]$' >/dev/null
then
echo "SORRY: Number for $2 too short or bad character. Ok: [0-9][0-9MGTmgt]" >&2
exit 14
fi
fi
}
### Assessing arguments and setting up the job
# Settings
@ -163,15 +188,18 @@ arg_interpreter() {
# The next_is option parameter readers get programmed by the -options
if test "$next_is" = "max_size"
then
check_parameter "$i" -max_size
max_size="$(echo "$i" | round_down_div_million)"
next_is=
elif test "$next_is" = "min_size"
then
check_parameter "$i" -min_size
min_size="$(echo "$i" | round_down_div_million)"
min_size="$(expr $min_size + 1)"
next_is=
elif test "$next_is" = "image_file"
then
check_parameter "$i" image_file
image_file="$i"
min_size="$(stat -c '%s' "$i" | round_down_div_million)"
if test -z "$min_size"
@ -231,20 +259,14 @@ arg_interpreter() {
exit 0
elif echo "$i" | grep -v '^-' >/dev/null
then
num=$(echo "$i" | wc -w)
if test "$num" = 1
then
check_parameter "$i" device_name
devs_named=y
devs="$devs $i"
show_reasons=y
else
echo "$0 : Given device name is not a single word: '$i'" >&2
exit 12
fi
else
echo "$0 : Unknown option: $i" >&2
echo "$0 : Unknown option: '$i'" >&2
echo >&2
print_usage >&2
echo "For a help text run: $0 -help" >&2
exit 1
fi
done
@ -426,7 +448,6 @@ list_devices() {
transports="mmcblk"
elif echo "$name" | grep -F "/" >/dev/null
then
echo "NOTE: The device name must not contain '/' characters" >&2
transports=not_an_expected_name
reasons="${reasons}name_with_slash- "
else