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

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

# The paranoid profile alone strips the setuid bit from sudo/su and several
# other programs (mount/umount, fusermount/fusermount3, expiry, pkexec,
# gpasswd). This package's permctl variant file overrides sudo/su
# specifically back to setuid, restricted to the trusted group - without it
# present, applying paranoid genuinely does remove every remote path to root.
# permctl reads the variant only while paranoid is active (and only next to
# its base file), so none of its entries apply before this component runs.
PERMISSIONS_CC="/usr/share/permissions/packages.d/certification-sles-eal4"

# Is paranoid among the levels in PERMISSION_SECURITY? The file is shell
# syntax, read as such so that a comment doesn't count.
cc71_level_paranoid() {
	(
		set +eu
		PERMISSION_SECURITY=""
		# shellcheck source=/dev/null
		. /etc/sysconfig/security >/dev/null 2>&1
		[[ " $PERMISSION_SECURITY " == *" paranoid "* ]]
	)
}

# Prints each entry of this package's permissions file whose file has other
# owner, group or mode, as "PATH is OWNER:GROUP MODE, expected ...". Files
# that aren't installed are skipped, as chkstat does.
cc71_own_entries_wrong() {
	local path owner mode actual
	while read -r path owner mode; do
		case "$path" in
		"" | \#*) continue ;;
		esac
		[ -e "$path" ] || continue
		actual=$(stat -c '%U:%G %a' "$path")
		if [ "$actual" != "$owner $(printf '%o' "$((8#$mode))")" ]; then
			echo "$path is $actual, expected $owner $mode"
		fi
	done <"$PERMISSIONS_CC.paranoid"
}

# Prints chkstat's findings (--warn changes nothing) that concern privileges:
# a setuid or setgid bit, in what the profile expects or in what the file
# has, or file capabilities. Other differences, e.g. a file a service
# recreates with its own mode at boot, aren't part of what the profile's
# paranoid level is used for here.
cc71_privilege_drift() {
	local out rc line path expected actual
	rc=0
	out=$(chkstat --system --warn -n 2>&1) || rc=$?
	if [ "$rc" -ne 0 ] && [ "$rc" -ne 2 ]; then
		echo "chkstat --system --warn failed (exit status $rc): $out"
		return
	fi
	while IFS= read -r line; do
		[[ "$line" == *": should be "* ]] || continue
		path=${line%%: should be *}
		if [[ "$line" == *capabilities* ]]; then
			echo "$line"
			continue
		fi
		expected=${line#*: should be }
		expected=${expected#* }
		expected=${expected%% *}
		if [[ "$line" == *"wrong permissions "* ]]; then
			actual=${line##*wrong permissions }
			actual=${actual%%[^0-7]*}
		else
			actual=$(stat -c '%a' "$path" 2>/dev/null) || actual=0
		fi
		if [ $(((8#$expected | 8#$actual) & 8#6000)) -ne 0 ]; then
			echo "$line"
		fi
	done <<<"$out"
}

# What the paranoid profile is used for here: no program outside this
# package's list keeps a setuid or setgid bit the profile removes (mount,
# gpasswd, pkexec, ...), and sudo and su are restricted to the trusted group
# (guide: "Gaining administrative access", "Mounting filesystems",
# "Removing users"). Checked on the files themselves, so a chmod after chkstat,
# or an /etc/permissions.local entry that overrides this package's, shows up.
check_71_config_permissions() {
	local wrong
	if ! cc71_level_paranoid; then
		echo "/etc/sysconfig/security PERMISSION_SECURITY is not set to paranoid"
		return 1
	fi
	wrong=$(cc71_own_entries_wrong)
	if [ -n "$wrong" ]; then
		echo "${wrong//$'\n'/; }"
		return 1
	fi
	wrong=$(cc71_privilege_drift)
	if [ -n "$wrong" ]; then
		echo "setuid/setgid bits or capabilities differ from the paranoid profile (chkstat --system --warn): ${wrong//$'\n'/; }"
		return 1
	fi
}

apply_71_config_permissions() {
	if [ ! -f "$PERMISSIONS_CC" ] || [ ! -f "$PERMISSIONS_CC.paranoid" ]; then
		echo "FAILED: $PERMISSIONS_CC or its .paranoid variant missing - is this package installed correctly?"
		return 1
	fi

	if check_71_config_permissions; then
		echo "permissions already set to paranoid and applied"
		return 0
	fi

	sed -ie '/^PERMISSION_SECURITY=/s/\(secure\|easy\)/paranoid/' /etc/sysconfig/security
	chkstat --set --system

	check_71_config_permissions || {
		echo "FAILED: permissions do not verify as paranoid after applying"
		return 1
	}
	echo "permissions set to paranoid and applied"
}
