#!/bin/sh

# privilege-checker-integrity
#
# Integrity check and recovery for the read-write privilege-checker databases
# (.policy.db, .privacy.db). Each database is checked by the matching updater
# script (--check) and, when it is not usable, restored from its backup copy
# by the same script (--restore).
#
# Run at boot by privilege-checker-integrity.service, or manually with either
#     /usr/share/privilege-manager/privilege-checker-integrity
#     systemctl start privilege-checker-integrity.service

PATH=/bin:/usr/bin:/sbin:/usr/sbin

. /etc/tizen-platform.conf

PRIVILEGE_DB_DIR=${TZ_SYS_RO_SHARE}/privilege-manager

POLICY_DB=${TZ_SYS_DB}/.policy.db
POLICY_UPDATER=${PRIVILEGE_DB_DIR}/policy_db_updater.sh

PRIVACY_DB=${TZ_SYS_DB}/.privacy.db
PRIVACY_UPDATER=${PRIVILEGE_DB_DIR}/privacy_db_updater.sh

LOG_TAG=PRIVILEGE_CHECKER_INTEGRITY

logsend() {
	# Send a dlog message. dlog is not up yet this early in the boot, so a
	# failed log must neither abort the check nor pollute the output with
	# dlogsend's error message. The printed copy still reaches the journal.
	dlogsend -p "$1" -t "$LOG_TAG" "$2" 2>/dev/null || true

	if [ "$1" = Error ]; then
		echo "[ERROR] $2"
	else
		echo "$2"
	fi
}

# verifyDb <db> <updater>
# Returns 0 when the database is intact, 2 when it was restored, 1 on failure.
verifyDb() {
	if [ ! -x "$2" ]; then
		logsend Error "Cannot check $1: $2 is not executable"
		return 1
	fi

	reason="`"$2" --check`"
	if [ $? -eq 0 ]; then
		logsend Info "Integrity check of $1 passed"
		return 0
	fi

	logsend Error "Integrity check of $1 failed: $reason"

	# Keep the damaged database for investigation.
	if [ -e "$1" ]; then
		if cp "$1" "$1-failed"; then
			logsend Info "Damaged $1 kept as $1-failed"
		else
			logsend Error "Could not keep a copy of damaged $1"
		fi
	fi

	if ! "$2" --restore; then
		logsend Error "Restoring $1 failed"
		return 1
	fi

	logsend Info "Restored $1 from its backup"
	return 2
}

logsend Info "Starting integrity check of the read-write databases"

rst=0
verifyDb "$POLICY_DB" "$POLICY_UPDATER"
if [ $? -eq 1 ]; then
	rst=1
fi

verifyDb "$PRIVACY_DB" "$PRIVACY_UPDATER"
if [ $? -eq 1 ]; then
	rst=1
fi

if [ "$rst" -eq 0 ]; then
	logsend Info "Integrity check finished"
else
	logsend Error "Integrity check finished with errors"
fi

exit $rst
