#!/bin/bash

# NOTE: This script is meant to be called by lab-grade-securevnc. Running it
#       directly is not expected to work.

. /usr/local/lib/labtool.shlib || echo 'Error: labtool.shlib not found!'
trap on_exit EXIT

# uncomment to enable debug mode
#DEBUG=true

USER=$(whoami)
X=$(hostname -i | cut -d. -f4)
SERVER="server${X}.example.com"

debug "* B: Running as ${USER}."

if [ -z $SSH_AGENT_PID ]

then
    debug "* B: No ssh-agent found."
    debug "* B: Starting up ssh-agent."
    exec ssh-agent $0
    
else
    debug "* B: Found ssh-agent."

fi


echo
echo "NOTE: If prompted, enter the ssh key passphase for ${USER}. The passphrase will"
echo "      be visible to help accurate entry."
echo
if ssh-add

then
    echo
    echo "* Checking for ssh key for ${USER}... PASS" 

else
    echo
    echo "* Checking for ssh key for ${USER}... FAIL"
    echo "* Quiting."
    exit

fi


echo -n "* Checking ssh login via key on ${SERVER}... "
if ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no $SERVER 'ls' &>/dev/null

then
    echo "PASS"

else
    echo "FAIL"
    echo "* Quiting."
    exit

fi


echo -n "* Checking for localhost only vnc server on ${SERVER}... "
if ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no $SERVER \
       'netstat -ltnp 2>/dev/null | grep 127.0.0.1 | grep -q Xvnc' &>/dev/null
       
then
    echo "PASS"
    
else
    echo "FAIL"
    echo "The vnc server was not running on $SERVER or was not bound to localhost."
    
fi


exit
