#!/bin/bash
set -euo pipefail
shopt -s inherit_errexit

# shellcheck source=scripts/libcc
. /usr/lib/common-criteria/scripts/libcc

CTRL_ALT_DEL_TARGET="/etc/systemd/system/ctrl-alt-del.target"

check_60_config_shutdown() {
	if [ ! -L "$CTRL_ALT_DEL_TARGET" ]; then
		echo "$CTRL_ALT_DEL_TARGET is not a symlink (not masked)"
		return 1
	fi
	if [ "$(readlink "$CTRL_ALT_DEL_TARGET")" != /dev/null ]; then
		echo "$CTRL_ALT_DEL_TARGET is a symlink but not to /dev/null"
		return 1
	fi
}

apply_60_config_shutdown() {
	if check_60_config_shutdown; then
		cc_echo "ctrl-alt-del already masked"
		return 0
	fi

	# Mask ctrl-alt-del.target so the ctrl-alt-del key combination can no
	# longer reboot the system: symlinking a unit to /dev/null is systemd's
	# own masking mechanism (see systemd.special(7)), preventing anyone with
	# keyboard access to an unlocked console from rebooting without
	# authorization.
	ln -sf /dev/null "$CTRL_ALT_DEL_TARGET"
	cc_echo "ctrl-alt-del masked"
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
	cc_start_logging
	trap 'cc_exit $?' ERR
	apply_60_config_shutdown
	cc_exit 0
fi
