fix: freebsd: add md5sum_compat()

to account for systems where md5sum's binary name is gmd5sum
This commit is contained in:
Stéphane Lesimple 2020-11-17 11:14:34 +01:00
parent 4105c10193
commit 09bd6dffd9
3 changed files with 12 additions and 2 deletions

View file

@ -436,7 +436,8 @@ if [ "$nothing" = 0 ]; then
action_detail "... we would divide by zero! fallback to a non-random random, such as $n"
random=$n
else
random=$(( ( 0x$(echo "$(hostname -f) $placeholder $file" | md5sum | cut -c1-4) % (m-n) ) + n ))
# shellcheck disable=SC2119
random=$(( ( 0x$(echo "$(hostname -f) $placeholder $file" | md5sum_compat | cut -c1-4) % (m-n) ) + n ))
fi
action_detail "... in above file, replacing $placeholder by $random"
sed_compat "s/$placeholder/$random/g" "$destfile"

View file

@ -32,7 +32,7 @@ generate_account_sudoers()
normalized_account=$(sed -re 's/[^A-Z0-9_]/_/gi' <<< "$account")
# as we're reducing the amount of possible chars in normalized_account
# we could have collisions: use MD5 to generate a uniq suffix
account_suffix=$(md5sum - <<< "$account" | cut -c1-6)
account_suffix=$(md5sum_compat - <<< "$account" | cut -c1-6)
normalized_account="${normalized_account}_${account_suffix}"
# lowercase is prohibited
normalized_account=$(tr '[:lower:]' '[:upper:]' <<< "$normalized_account")

View file

@ -108,6 +108,15 @@ sed_compat()
fi
}
md5sum_compat()
{
if command -v gmd5sum >/dev/null; then
gmd5sum "$@"; return $?
else
md5sum "$@"; return $?
fi
}
useradd_compat()
{
local _user="$1" _uid="" _home="" _shell="" _gid="" _extra=""