#!/bin/bash

. /usr/local/lib/labtool.shlib
trap on_exit EXIT

#DEBUG=true
#QUEUE=remote-test
STATION_NUM="$(hostname -s | grep -o "[0-9]*")"
SERVERX=server${STATION_NUM}.example.com
#QUEUE_URL="http://instructor.example.com/printers/printer${STATION_NUM}"
#TEMP_FILE="$(mktemp)"



check_root

check_host "server"

if [ ! -f /root/review-rebooted ];then
	cat << EOL

	*** NOTICE ***
	This script will reboot $SERVERX to grade the excercise.
EOL
	confirm
	echo
	touch /root/review-rebooted
	reboot
	exit
fi




# This was reused from the SA1 review lab.  Remove the checks and create
# new ones for RHCE-RT



# FTP server
echo -n "* Checking for FTP server... "
if service vsftpd status &> /dev/null
then
    echo "PASS"
else
    echo "FAIL"
    echo "FTP server not found."
fi


# FTP file ftp2.txt
echo -n "* Checking for FTP file... "
if wget -q ftp://$SERVERX/ftp2.txt  &> /dev/null
then
    echo "PASS"
else
    echo "FAIL"
    echo "FTP file not found."
fi



# Temp files, /tmp/ftp1.txt and /tmp/http2.html
echo -n "* Checking for temporary files... "
if [ -f /tmp/ftp1.txt -a -f /tmp/http2.html ]
then
    echo "PASS"
else
    echo "FAIL"
    echo "Temporary files not found"
fi

# VNC server
echo -n "* Checking for VNC server... "
if service vncserver status &> /dev/null
then
    echo "PASS"
else
    echo "FAIL"
    echo "VNC server not configured correctly."
fi

# VNC server
echo -n "* Checking for VNC user... "
if grep -q '2:student' /etc/sysconfig/vncservers &> /dev/null
then
    echo "PASS"
else
    echo "FAIL"
    echo "VNC user not configured correctly."
fi

# Web server
echo -n "* Checking for web server... "
if service httpd status &> /dev/null
then
    echo "PASS"
else
    echo "FAIL"
    echo "Web server not found."
fi

# Web file http1.html
echo -n "* Checking for web file... "
if wget -q http://$SERVERX/http1.html  &> /dev/null
then
    echo "PASS"
else
    echo "FAIL"
    echo "Web file not found."
fi

# Restorecon
echo -n "* Checking for file context... "
# Just check one file (http1.html)
if [ -f /var/www/html/http1.html ]
then
	if ls -Z /var/www/html/http1.html | grep -q httpd_sys_content_t &> /dev/null
	then
    	echo "PASS"
	else
    	echo "FAIL"
    	echo "File context not correct."
	fi
else
	echo "FAIL"
	echo "File context not correct."
fi

# Firewall HTTP, FTP, SSH
echo -n "* Checking for firewall... "
if iptables -nL | grep -q 'dpt:80' && iptables -nL | grep -q 'dpt:21' && iptables -nL | grep -q 'dpt:22'
then
    echo "PASS"
else
    echo "FAIL"
    echo "Firewall not configured properly."
fi

echo
echo You began:
cat /root/review-start
echo The current time is:
date
echo

# Remove the reboot file, so they have to reboot again to check.
rm -f /root/review-rebooted

exit
