#!/bin/bash
# Polish Script: Clears up unwanted files and packages
# Uninstall packages - don't error if they are absent but don't hide error
# output either
rpms=$(rpm -qa --queryformat '%{NAME}\n' | grep -e yum -e rpm -e slos-security)
rpm -e ${rpms} 2>&1 || {
    echo -e "Failed to uninstall RPMs!\n${rpms}" >&2
    #exit 1
}

# Cleanup Dir and files
[ -x /usr/bin/find ] && /usr/bin/find /var/log -type f -delete
rm -rf /var/lib/rpm
rm -rf /var/lib/yum /var/log/yum.log
rm -rf /var/tmp/* /tmp/* /var/cache*
rm -rf /root/install

# Cleanup User's, Groups
sed -i -e '/^systemd-network/d' -e '/^dbus/d' /etc/passwd  /etc/passwd- /etc/shadow  /etc/shadow- /etc/group   /etc/group- || true

# CIS hardening found that some passwd files do not have the correct permissions
# do this after any new users are added
chmod 644 /etc/{passwd,group}
chmod 000 /etc/{shadow,gshadow}{,-}
chmod 600 /etc/{passwd,group}-

# Remove the nullok directives from pam files
for f in /etc/pam.d/password-auth /etc/pam.d/system-auth; do [[ -e $f ]] && sed -i -e 's/\snullok//' "$f"; done

#remove all setuid and setgid from files.
#CCI-002235 Security hardening requirement
[ -x /usr/bin/find ] && /usr/bin/find / -xdev -perm /6000 -exec chmod a-s,g-s {} \+ -print || true
