# Selinux Setup

secEnableSELinux() {
log "SLOSSecurity:EnableSELinux:Remediating rule 'SLOS-EnableSelinux'"
# Install Selinux Packages
dnf --setopt=main.default_timeout=30 -y install  policycoreutils selinux-policy selinux-policy-targeted libselinux-utils setools setools-console mcstrans selinux-policy-minimum libsemanage container-selinux policycoreutils-restorecond selinux-policy-minimum || :


# Correct the form of default kernel command line in GRUB
if /usr/bin/grep -q '^GRUB_CMDLINE_LINUX=.*selinux=.*"'  /etc/default/grub ; then
    # modify the GRUB command-line if an audit= arg already exists
    /usr/bin/sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)selinux=[^[:space:]]*\(.*"\)/\1 selinux=1 \2/'  /etc/default/grub
else
    # no selinux=arg is present, append it
    /usr/bin/sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)"/\1 selinux=1"/' /etc/default/grub
fi

if [ -f /etc/selinux/config ] ; then
    /usr/bin/sed -i -e "s/^SELINUX=.*/SELINUX=enforcing/g" -e "s/^SELINUXTYPE=.*/SELINUXTYPE=targeted/g" /etc/selinux/config
else
    # no selinux=arg is present, append it
    /usr/bin/echo -e "
SELINUX=enforcing
SELINUXTYPE=targeted
" > /etc/selinux/config
fi
# Correct the form of kernel command line for each installed kernel in the bootloader
/usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg

#Create the AutoRelable file
echo Enable the AutoRelabel file
systemctl enable selinux-autorelabel-mark.service
sleep 1
touch /.autorelabel
}

SECFILE=/etc/slossec.conf
[ -f "$SECFILE" ] && source ${SECFILE} 
if [[ $selinux -eq 1 ]] ; then
    log "SLOSSecurity: SELinux Setup"
    $DIALOG --backtitle "$BT" --title "SELinux" --infobox "Security Enhanced Linux - SELinux" $H1 $W1
    sleep $autoTO
    secEnableSELinux
    log "SLOSSecurity: Selinux Enabled"
    [ -f $PROG/Started ] && rm -f $PROG/securityStarted
    #sed -i "s/selinux=1/selinux=0/g" $SECFILE 2>/dev/null
    log "SELINUX: REBOOT"
    export REBOOT=Y
    systemctl enable getty\@tty1.service
    sleep .1
    show "SLOSSecurity: SELinux Setup Completed"
else
    log "SLOSSecurity: SELinux Setup not performed"
fi

