#!/bin/sh
#
# Record whether the osmc account still has the default password, for the SSH
# login warning in /etc/profile.d/105-check-ssh-password.sh. Run as root by
# osmc-password-check.service, at boot and, through osmc-password-check.path,
# whenever /etc/shadow changes. Logins then check a flag instead of reading
# the password hash, which needed sudo.

set -eu
export LC_ALL=C

FLAG=/run/osmc-password-check/default

hash=$(awk -F: '$1 == "osmc" { print $2; exit }' /etc/shadow)

# crypt(3) through perl, which is essential on bullseye and trixie alike:
# Python's crypt module is gone from Python 3.13, trixie's version. The hash
# goes through the environment, not the command line, where any user could
# read it. A locked or empty field never matches.
if HASH="$hash" perl -e 'exit(length($ENV{HASH}) && crypt("osmc", $ENV{HASH}) eq $ENV{HASH} ? 0 : 1)'; then
	touch "$FLAG"
	echo "osmc-password-check: osmc has the default password"
else
	rm -f "$FLAG"
	echo "osmc-password-check: osmc does not have the default password"
fi
