#!/bin/bash
#
# Name          : upgrade-2-rh5-pci2_0.sh
# Summary       : Datapipe PCI-DSS Hardening Standards Script
# Author        : Kenny Lo
# Date          : Jul 16 2012
##############################################

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

# Sanity check : OS version check
version=`cat /etc/redhat-release |grep -c "release 5"`

if [ $version -lt 1 ]; then
        echo "This script will only work on Cent OS 5 or Red Hat ES 5.  Please"
        echo "check your OS and version number."
        sleep 2
        exit
fi


# Install required packages for hardening script
yum -y install ntp sysstat rcs

# Confirm packages installed
if [ ! -f /usr/bin/rcs ]; then
echo "RCS did not install successfully.  Please"
echo "manually install the following packages"
echo "and re-run hardening script.  Type:"
echo ""
echo "yum -y install ntp sysstat rcs"
echo "--"
echo "No modifications have been made to the system."
sleep 5
exit
fi

if [ ! -f /usr/bin/sar ]; then
echo "Sysstat did not install successfully.  Please"
echo "manually install the following packages"
echo "and re-run hardening script.  Type:"
echo ""
echo "yum -y install ntp sysstat rcs"
echo "--"
echo "No modifications have been made to the system."
sleep 5
exit
fi

if [ ! -f /usr/sbin/ntpd  ]; then
echo "NTP did not install successfully.  Please"
echo "manually install the following packages"
echo "and re-run hardening script.  Type:"
echo "" 
echo "yum -y install ntp sysstat rcs"
echo "--"
echo "No modifications have been made to the system."
sleep 5
exit
fi

mkdir -p /etc/cron.daily/RCS
mkdir -p /etc/init.d/RCS
mkdir -p /etc/logrotate.d/RCS
mkdir -p /etc/RCS

### fix 10.2.0.10a Verify authpriv Entry
# backup file first
ci -l -wsecurity /etc/syslog.conf <<EOF
State before upgrade to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF

sed -i '/^authpriv\.\*[[:blank:]]*\/var\/log\/secure/a \authpriv\.\*\t\t\t\t\t\t\/var/\log/\messages' /etc/syslog.conf
/etc/init.d/syslog restart
chmod 600 /etc/syslog.conf

# backup file after change
ci -l -wsecurity /etc/syslog.conf <<EOF
upgraded to PCI-DSS 2.0 Hardening Standards.
.
EOF

##############################################

### fix 10.2.0.22 Turns on the Auditing Subsystem
#
if [ -f /etc/audit/audit.rules ]; then
# backup file first
ci -l -wsecurity /etc/audit/audit.rules <<EOF
State before upgrade to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF
# replace "-e 1" with "-e 2"
sed -i 's/^\-e\ 1$/\-e\ 2/' /etc/audit/audit.rules
fi

sed -i 's%\-w\ /var/log/audit/%\#\-w\ /var/log/audit/%' /etc/audit/audit.rules

##############################################

### fix 10.7.6 Verify Audit Log Size Is Set to 8192
if [ `grep ^"\-b\ [[:digit:]]" /etc/audit/audit.rules | awk '{print $2}'` -ne 8192 ]
then
# increase the size of the audit log to 8192
sed -i 's/^\-b\ [0-9]*/\-b\ 8192/' /etc/audit/audit.rules
/etc/init.d/auditd reload
fi

# backup file after change
ci -l -wsecurity /etc/audit/audit.rules <<EOF
upgraded to PCI-DSS 2.0 Hardening Standards.
.
EOF
##############################################

### fix 10.5.2.6 Verify /var/log/httpd/* permissions
if [ -f /etc/logrotate.d/httpd ]; then
# backup file first
ci -l -wsecurity /etc/logrotate.d/httpd <<EOF
State before upgrade to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF
# insert the line "create 0600 root root" before the line postrotate
sed -i '/postrotate/ i\    create 0600 root root' /etc/logrotate.d/httpd
#
chmod 0600 /var/log/httpd/*
chown -R root:root /var/log/httpd/*
#
# backup file after change
ci -l -wsecurity /etc/logrotate.d/httpd <<EOF
Upgraded to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF
fi
##############################################


### fix 10.5.2.11 /var/log/rpmpkgs* Permissions
#
chmod 640 /var/log/rpmpkgs*
chown root:root /var/log/rpmpkgs*
#
if [ -f /etc/logrotate.d/rpm ]; then
# backup file first
ci -l -wsecurity /etc/logrotate.d/rpm <<EOF
State before upgrade to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF
# append "create 0640 root root" to the rpm file
sed -i '/missingok/a \    create 0640 root root' /etc/logrotate.d/rpm
fi
# backup file after change
ci -l -wsecurity /etc/logrotate.d/rpm <<EOF
Upgraded to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF

if [ -f /etc/cron.daily/rpm ]; then
# backup file first
ci -l -wsecurity /etc/cron.daily/rpm <<EOF
State before upgrade to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF
sed -i 's/\/bin\/chmod 0644 \/var\/log\/rpmpkgs/\/bin\/chmod 0640 \/var\/log\/rpmpkgs/' /etc/cron.daily/rpm
# backup file after change
ci -l -wsecurity /etc/cron.daily/rpm <<EOF
Upgraded to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF
chmod 700 /etc/cron.daily/rpm
fi

##############################################


### fix 10.5.2.13 Verify /var/log/sa/* Permissions

cat << EOF > /etc/cron.d/set-sa-permission
1 * * * * root /usr/bin/find /var/log/sa -perm /g+r,o+r -exec /bin/chmod -R 700 {} \; > /dev/null 2>&1
EOF
chmod 600 /etc/cron.d/set-sa-permission
chown root:root /etc/cron.d/set-sa-permission
for i in {1..9};do touch /var/log/sa/sa0$i; touch /var/log/sa/sar0$i; done
for i in {10..31};do touch /var/log/sa/sa$i; touch /var/log/sa/sar$i; done
chmod -R 700 /var/log/sa

##############################################

### fix 2.2.2.125 Verify That the rhnsd Service Is Disabled
/etc/init.d/rhnsd stop
chkconfig --level 123345 rhnsd off
##############################################

### fix 7.1.1.  7 Verify /etc/ftpusers Permissions
chmod 0600 /etc/ftpusers
##############################################

##############################################

# backup file first
ci -l -wsecurity /etc/logrotate.conf <<EOF
State before upgrade to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF
sed -i 's/create 0664 root utmp/create 0600 root utmp/' /etc/logrotate.conf

# backup file after change
ci -l -wsecurity /etc/logrotate.conf <<EOF
Upgraded to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF
##############################################

# backup file first
cd /etc/pam.d
ci -l -wsecurity /etc/pam.d/system-auth-ac <<EOF
State before upgrade to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF

cd /etc/pam.d
sed -i 's/min=disabled,disabled,7,7,7/min=disabled,disabled,8,8,8/g' system-auth-ac

# backup file after change
ci -l -wsecurity /etc/pam.d/system-auth-ac <<EOF
Upgraded to Datapipe PCI-DSS 2.0 Hardening Standards.
.
EOF

##############################################

### END.

if [ -d /var/lib/nfs ]; then chmod 750 /var/lib/nfs; fi

/etc/init.d/lm_sensors stop
chkconfig --level 12345 lm_sensors off

chmod 0640 /var/log/dmesg*
chmod 0444 /etc/bashrc /etc/csh.cshrc /etc/csh.login /etc/profile
chmod 0400 /root/.bash_profile /root/.bashrc /root/.cshrc /root/.tcshrc

chmod u-x,go-rwx /etc/syslog.conf
chmod 0640 /var/log/dmesg*

find /home -type d -name RCS -exec chmod -R g-rwx,o-rwx {} \;
find /boot -type d -name RCS -exec chmod -R g-rwx,o-rwx {} \;
find /etc -type d -name RCS -exec chmod -R g-rwx,o-rwx {} \;
find /usr -type d -name RCS -exec chmod -R g-rwx,o-rwx {} \;
find /root -type d -name RCS -exec chmod -R g-rwx,o-rwx {} \;
if [ -d /usr/openv/netbackup/logs ]; then chmod -R o-w /usr/openv/netbackup/logs; fi
if [ -d /usr/local/APPCure-full/tmp ]; then chmod +t /usr/local/APPCure-full/tmp; fi

echo "net.ipv4.tcp_timestamps = 0" >> /etc/sysctl.conf
sysctl -w net.ipv4.tcp_timestamps=0

###############################################

if [ -d /etc/X11/xinit ]; then
    cd /etc/X11/xinit
    if [ -e xserverrc ]; then
sed -i 's/exec X :0 -nolisten tcp \$@/exec X :0 -nolisten tcp \\\$@/' xserverrc
    fi
fi

#rm -f /upgrade-2-rh5-pci2_0.sh
