#!/bin/bash

staff_email='l3@ihnetworks.com'

cloud=`uname  -r | grep -o lve`
if [ "$cloud" = "lve" ]; then
        echo -e "`hostname` cloud kernel. Exiting"  | mail -s "`hostname` cloud kernel. Exiting"  $staff_email
	exit
fi

vpstrue=`ifconfig  | grep -o venet | uniq`
if [ "$vpstrue" = "venet" ]; then
        echo -e "VE single node detected on  `hostname`  Exiting"  | mail -s "SECURETMP VE single node detected on  `hostname` " $staff_email
        exit
fi


dfhome=`df -h | grep home | head -1 | awk '{print $4}' |grep G | cut -dG -f1`
if [ $dfhome -lt 2 ]; then
	echo "WARNING `hostname` /home folder size less than 2 GB" | mail -s "WARNING `hostname` /home folder size less than 2 GB" $staff_email
	exit
fi



noexecval=`mount  | grep tmp |grep -v shm |grep -o noexec | head -1`
if [ "$noexecval" = "noexec" ]; then
	exit
else
	sed -i 's/\/usr\/tmpDSK/#\/usr\/tmpDSK/' /etc/fstab
	sed -i 's/\/tmp/#\/tmp/' /etc/fstab
fi


if [ -f /usr/tmpDSK ]; then
	mv /usr/tmpDSK /backup/tmpDSK.old.`date -I`
fi
modprobe loop
dd if=/dev/zero of=/home/tmpDSK bs=1M count=800
echo y | mkfs.ext3 /home/tmpDSK
mount -o loop,rw,noexec,nosuid /home/tmpDSK /tmp
mount -o bind,rw,noexec,nosuid /tmp /var/tmp
chmod 1777 /tmp
ln -s /var/lib/mysql/mysql.sock  /tmp/
echo "/home/tmpDSK             /tmp                    ext3    loop,noexec,nosuid,rw        0 0"  >> /etc/fstab
echo "/tmp                    /var/tmp                ext3    bind,noexec,nosuid        0 0" >> /etc/fstab
echo "modprobe loop" >> /etc/rc.local

noexecvala=`mount  | grep tmp |grep tmpDSK | grep tmp | grep -o noexec`
if [ "$noexecvala" = "noexec" ]; then
	echo "modprobe loop" >> /etc/rc.local
	echo "mount -a" >> /etc/rc.local
	echo "SUCCESS securetmp enabled on `hostname`" | mail -s "SUCCESS securetmp enabled on `hostname`" $staff_email
else
	echo "FAILURE securetmp NOT enabled on `hostname`" | mail -s "FAILURE securetmp NOT enabled on `hostname`" $staff_email
fi
