#! /bin/sh
set -e

#PREREQ="framebuffer console_setup"
PREREQ=""

prereqs () {
	echo "$PREREQ"
}

case $1 in
	prereqs)
		prereqs
		exit 0
		;;
esac

. /scripts/functions
. /scripts/security-functions

# Mount the selinux directory in both the ramdisk's root as well as in
# the real root directory.
mkdir -p /selinux

# Temporarily pivot to the real root directory, loading the policy
# from that disk.  Normally this process will occur by init, but kinit
# is not compiled against libselinux.  Therefore use load_policy to
# perform the same initialization.
set +e

clear


mount_backup_partition() {
        if [ -f $rootmnt/etc/.bootinfo ]; then
                . $rootmnt/etc/.bootinfo
                if [ $SNAPSHOT_ENABLED -eq 1 ]; then
                    if [ -e $rootmnt/dev/disk/by-uuid/$RECOVERY_DEV_UUID ]; then
                        mount -t ext4 $rootmnt/dev/disk/by-uuid/$RECOVERY_DEV_UUID $rootmnt/backup
                    fi
                fi
        fi     
}

umount_backup_partition() {
        if [ -f $rootmnt/etc/.bootinfo ]; then
                . $rootmnt/etc/.bootinfo
                if [ $SNAPSHOT_ENABLED -eq 1 ]; then
                        umount $rootmnt/backup 
                fi
        fi     
}

#relabel#
statusfile=${rootmnt}/.autorelabel

# Get the selinux configuration variables
SELINUXTYPE=""
if [ -e ${rootmnt}/etc/selinux/config ]; then
	. ${rootmnt}/etc/selinux/config
fi

if [ "x${SELINUXTYPE}" = "x" ]; then
	log_failure_msg "No SELinux policy found"
	exit 0
elif [ ! -d ${rootmnt}/etc/selinux/${SELINUXTYPE} ]; then
	log_failure_msg "No SELinux policy found"
	exit 0
elif [ ! -e ${rootmnt}/etc/selinux/${SELINUXTYPE}/policy/policy.32 ]; then
	log_failure_msg "No SELinux policy found"
	exit 0
fi


#change system exectl mode to softmode
securityfs_mnt=/sys/kernel/security
kysec_status_path="$securityfs_mnt/kysec/status"
kysec_admin_path="$securityfs_mnt/kysec/3adm"

kysec_is_enabled(){
        if [ -e "$kysec_status_path" ]; then
                cat "$kysec_status_path"
        else
                echo 0
        fi
}

kysec_enabled=`kysec_is_enabled`
kysec_admin=`cat "$kysec_admin_path"`

set_kysec_status(){
        echo $1 > $kysec_status_path
}

#check whether cmdline selinux autorelabel
export autorelabel=

for x in $(cat /proc/cmdline); do
       	case $x in
       	autorelabel)
               	autorelabel=y
       	        ;;
        esac
done

#if [ ! -e $statusfile -a  "x${autorelabel}" = "x" ]; then
#	exit 0
#fi

mount -t sysfs -o nodev,noexec,nosuid sysfs ${rootmnt}/sys
mount -t proc -o nodev,noexec,nosuid proc ${rootmnt}/proc
mount -t devtmpfs udev "$rootmnt/dev"

mount -t securityfs | grep -q "securityfs"
if [ $? -ne 0 ]; then
	mount -t securityfs -o nodev,noexec,nosuid none ${rootmnt}/sys/kernel/security/
fi

mount -o remount,rw ${ROOT} ${rootmnt}

chroot ${rootmnt} /sbin/load_policy -i
RET=$?

mount -t selinuxfs none /selinux || \
    log_warning_msg "Unable to mount /selinux"

if [ $RET -eq 3 ]; then
    log_warning_msg "SELinux policy load failed"
elif [ $RET -ne 0 ]; then
    log_warning_msg "SELinux policy load failed, continuing"
else
    log_success_msg "SELinux policy was loaded"
fi

prompt="
Relabeling The SELinux Policy context :
********************************************************
* If you are not already running SELinux, then you can *
* safely ignore the following error message.           *
* SELinux ${SELINUXTYPE} policy relabel is required.   *
* Relabeling could take a very long time, depending    *
* on file system size and speed of hard drives.	       *
********************************************************
* Relabeling files >>>>>>>>"

. ${rootmnt}/etc/default/locale

translate_message()
{
       case "$LANG" in
               zh_CN.*)
                       echo "$1"
               ;;
               en_US.*)
                       echo "$2"
               ;;
               bo_CN.*)
                       echo "$3"
               ;;
	       zh_HK.*)
                       echo "$4"
	       ;;
	       mn_MN.*)
                       echo "$5"
               ;;
               *)
                       echo "$2"
               ;;
       esac
}

if [ $RET -eq 0 ]; then
	if [ -e $statusfile -o  "x${autorelabel}" = "xy" ]; then
		echo "$prompt"
#		show_message "正在初始化强制访问控制标记，请稍后 ..." 
		zh_CN_msg="正在初始化强制访问控制标记，请稍后 ..."
		en_CN_msg="Initializing Mandatory access control token, please wait ......"
		bo_CN_msg="ཐོག་མར་འགྱུར་བཞིན་ཡོད་།བཙན་ཤེད་ཀྱིས་འཚམས་འདྲི་ཚོད་འཛིན་བཀོད་རྟགས་དང་།ཅུང་ཙམ་འགོར་རྗེས་རོགས་། "
		zh_HK_msg="正在初始化強制存取控制標記，請稍後…"
		mn_MN_msg="ᠶᠠᠭ ᠠᠯᠪᠠᠳᠠᠯᠭ᠎ᠠ ᠪᠠᠷ ᠰᠤᠷᠪᠤᠯᠵᠢᠯᠠᠬᠤ ᠡᠵᠡᠮᠰᠢᠯ ᠤᠨ ᠲᠡᠮᠳᠡᠭ ᠢ ᠠᠩᠬᠠᠵᠢᠭᠤᠯᠵᠤ ᠪᠠᠶᠢᠨ᠎ᠠ ᠤᠳᠠᠰᠬᠢᠭᠡᠳ"
		msg="$(translate_message "$zh_CN_msg" "$en_CN_msg" "$bo_CN_msg" "$zh_HK_msg" "$mn_MN_msg")"
		show_message "$msg"

		oldvalue=`cat $kysec_status_path`
		if [ "$kysec_enabled" -ne 0 ]; then
			set_kysec_status 4
		fi

		se_status=`cat /selinux/enforce`
		if [ $se_status -ne 0 ]; then
			echo "0" > /selinux/enforce
		fi

		if [ ! -e ${rootmnt}/usr/share/selinux/${SELINUXTYPE}/.home ]; then
			chroot ${rootmnt} /usr/sbin/genhomedircon
			touch ${rootmnt}/usr/share/selinux/${SELINUXTYPE}/.home
		fi

		chroot ${rootmnt} /bin/mount -a
		bootpart=$(mount | grep "/boot")
		booteif=$(mount | grep "boot/efi")
		if [ -n "$bootpart" ]; then
			chroot ${rootmnt} mount -o remount,rw /boot
		fi
		if [ -n "booteif" ]; then
			chroot ${rootmnt} mount -o remount,rw /boot/efi
		fi
		mount_backup_partition
		chroot ${rootmnt} /sbin/restorecon -RF / >/dev/null  2>&1
		umount_backup_partition
		chroot ${rootmnt} /sbin/restorecon -RF /backup >/dev/null  2>&1
		chroot ${rootmnt} /sbin/restorecon -RF /home >/dev/null  2>&1
		chroot ${rootmnt} /sbin/restorecon -RF /root >/dev/null  2>&1
        	log_end_msg $?
        	/bin/rm -f $statusfile
		set_kysec_status ${oldvalue}
		echo $se_status > /selinux/enforce

		clear_message
	fi
fi
#relabel end#
umount ${rootmnt}/proc
umount ${rootmnt}/sys/fs/selinux
umount ${rootmnt}/sys
umount $rootmnt/dev
clear
