#!/bin/bash

# check-assn2.bash

# Author: Murray Saul
# Created: October 7, 2017 Updated: October 20, 2018

# Purpose: To generate data to be mailed to OPS335 instructor in order
#          to mark OPS335 assignment #2

# Error-checking prior to running shell script
# Check that user is logged on as root

if [ `id -u` -ne 0 ]
then
  echo "You must run this shell script as root" >&2
  exit 1
fi

# Check that mailx application has been installed

if ! which mailx > /dev/null 2> /dev/null 
then
  echo "You need to run \"yum install mailx\" prior to running this shell script" >&2
  exit 1
fi


if ! virsh list | egrep -iqs "(cloyne|toronto|ottawa|kingston|coburg|milton)"
then
  echo "You need to run your cloning-source \"cloyne\", \"toronto\", \"ottawa\", \"kingston\", \"coburg\" and \"milton\" VMs" >&2
  exit 2
fi


# Prompt for username and Full name

read -p "Please enter YOUR FULL NAME: " fullName

read -p "Please enter YOUR SENECA LOGIN USERNAME: " userID

# Generate Evaluation Report

clear
echo "Your Assignment #2 is being evaluated..."
echo "This make take a few minutes to complete..."


echo "Assignment 2 Evaluation Report - Date: $(date +'%A %B %d, %Y @ %H:%M:%S %P')" > a2-eval.txt
echo >> a2-eval.txt
# Email server evaluation
echo "----- EMAIL SERVER INFORMATION: -----" >> a2-eval.txt
echo >> a2-eval.txt
echo "kingston POSTFIX MTA settings: " >> a2-eval.txt
ssh root@172.17.15.5 egrep "inet_interfaces" /etc/postfix/main.cf | grep -v "#" >> a2-eval.txt
ssh root@172.17.15.5 egrep "mydomain" /etc/postfix/main.cf | grep -v "#" >> a2-eval.txt
ssh root@172.17.15.5 egrep "myorigin" /etc/postfix/main.cf  | grep -v "#" >> a2-eval.txt
ssh root@172.17.15.5 egrep "relayhost" /etc/postfix/main.cf  | grep -v "#" >> a2-eval.txt
ssh root@172.17.15.5 egrep "mynetwork" /etc/postfix/main.cf  | grep -v "#" >> a2-eval.txt
echo >> a2-eval.txt
echo "POSTFIX MTA settings (coburg): " >> a2-eval.txt
ssh root@172.17.15.6 egrep "mydestination" /etc/postfix/main.cf  | grep -v "#" >> a2-eval.txt
ssh root@172.17.15.6 egrep "mailbox_command" /etc/postfix/main.cf  | grep -v "#" >> a2-eval.txt
echo >> a2-eval.txt
echo "MDA/LDA settings (coburg):" >> a2-eval.txt
echo "   /etc/dovecot/conf.d/10-mail.conf:" >> a2-eval.txt
ssh root@172.17.15.6 egrep "mail_location" /etc/dovecot/conf.d/10-mail.conf | grep -v "#" >> a2-eval.txt
echo >> a2-eval.txt
echo "LDA (AA - IMAP) settings (coburg):" >> a2-eval.txt
echo "   /etc/dovecot/conf.d/10-auth.conf:" >> a2-eval.txt
ssh root@172.17.15.6 egrep "disable_plaintext_auth" /etc/dovecot/conf.d/10-auth.conf  | grep -v "#" >> a2-eval.txt
echo >> a2-eval.txt
echo "   /etc/dovecot/conf.d/10-ssl.conf:" >> a2-eval.txt
ssh root@172.17.15.6 egrep "ssl" /etc/dovecot/conf.d/10-ssl.conf | grep -v "#" >> a2-eval.txt
echo >> a2-eval.txt
echo "DNS (MX) information:" >> a2-eval.txt
ssh root@172.17.15.2 cat /var/named/mydb-for-towns.ontario.ops >> a2-eval.txt
echo >> a2-eval.txt
echo >> a2-eval.txt

# Samba server evaluation

echo "----- SAMBA SERVER INFORMATION:----- " >> a2-eval.txt
echo >> a2-eval.txt
echo "SAMBA CONFIG FILE (milton):" >> a2-eval.txt
ssh root@172.17.15.8 cat /etc/samba/smb.conf >> a2-eval.txt

echo >> a2-eval.txt
echo "SAMBA USER ACCOUNTS (milton):" >> a2-eval.txt
ssh root@172.17.15.8 pdbedit -L -v >> a2-eval.txt

echo >> a2-eval.txt
echo >> a2-eval.txt

echo "FIREWALL POLICIES" >> a2-eval.txt
echo >> a2-eval.txt
echo "-------------------- IPTABLES STATUS SMTP (kingston): --------------------" >> a2-eval.txt
ssh root@172.17.15.5 iptables -L >> a2-eval.txt
echo >> a2-eval.txt
echo "-------------------- IPTABLES STATUS IMAP (coburg): --------------------" >> a2-eval.txt
ssh root@172.17.15.6 iptables -L >> a2-eval.txt
echo >> a2-eval.txt
echo "-------------------- IPTABLES STATUS SAMBA (milton): --------------------" >> a2-eval.txt
ssh root@172.17.15.8 iptables -L >> a2-eval.txt
echo >> a2-eval.txt

# Less Deductions

echo >> a2-eval.txt
echo "LESS DEDUCTIONS" >> a2-eval.txt
echo >> a2-eval.txt
echo >> a2-eval.txt
echo "Correct hostnames:" >> a2-eval.txt
echo "   cloyne hostname: $(ssh root@172.17.15.100 hostname)" >> a2-eval.txt
echo "   toronto hostname: $(ssh root@172.17.15.2 hostname)" >> a2-eval.txt
echo "   ottawa hostname: $(ssh root@172.17.15.3 hostname)" >> a2-eval.txt
echo "   kingston hostname: $(ssh root@172.17.15.5 hostname)" >> a2-eval.txt
echo "   coburg hostname: $(ssh root@172.17.15.6 hostname)" >> a2-eval.txt
echo "   milton hostname: $(ssh root@172.17.15.8 hostname)" >> a2-eval.txt
echo >> a2-eval.txt
echo "firewall status:" >> a2-eval.txt
echo "   cloyne: $(ssh root@172.17.15.100 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   toronto: $(ssh root@172.17.15.2 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   ottawa: $(ssh root@172.17.15.3 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   kingston: $(ssh root@172.17.15.5 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   coburg: $(ssh root@172.17.15.6 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   milton: $(ssh root@172.17.15.8 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo >> a2-eval.txt
echo "SELINUX Enabled:" >> a2-eval.txt
echo "   cloyne: $(ssh root@172.17.15.100  /usr/sbin/getenforce)" >> a2-eval.txt
echo "   toronto: $(ssh root@172.17.15.2  /usr/sbin/getenforce)" >> a2-eval.txt
echo "   ottawa: $(ssh root@172.17.15.3  /usr/sbin/getenforce)" >> a2-eval.txt
echo "   kingston: $(ssh root@172.17.15.5  /usr/sbin/getenforce)" >> a2-eval.txt
echo "   coburg: $(ssh root@172.17.15.6   /usr/sbin/getenforce)" >> a2-eval.txt
echo "   milton: $(ssh root@172.17.15.8  /usr/sbin/getenforce)" >> a2-eval.txt
echo >> a2-eval.txt
echo "yum update performed:" >> a2-eval.txt
echo "   cloyne: $(ssh root@172.17.15.100 yum check-update && echo updated)" >> a2-eval.txt
echo "   toronto: $(ssh root@172.17.15.2 yum check-update && echo updated)" >> a2-eval.txt
echo "   ottawa: $(ssh root@172.17.15.3 yum check-update && echo updated)" >> a2-eval.txt
echo "   kingston: $(ssh root@172.17.15.5 yum check-update && echo updated)" >> a2-eval.txt
echo "   closet: $(ssh root@172.17.15.6 yum check-update && echo updated)" >> a2-eval.txt
echo "   milton: $(ssh root@172.17.15.8 yum check-update && echo updated)" >> a2-eval.txt
echo >> a2-eval.txt
echo "Services running:"  >> a2-eval.txt

echo "smtp status:" >> a2-eval.txt
echo "   kingston (SMTP): $(ssh root@172.17.15.5 systemctl status postfix | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   coburg (IMAP): $(ssh root@172.17.15.6 systemctl status dovecot | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   milton (SAMBA): $(ssh root@172.17.15.8 systemctl status smb | egrep -i "(active|stop)")" >> a2-eval.txt

echo >> a2-eval.txt
echo "/etc/hosts NOT removed:" >> a2-eval.txt
ls  >> a2-eval.txt 2> /dev/null
echo "   cloyne:" >> a2-eval.txt
ssh root@172.17.15.100 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.15.100 ls /home  >> a2-eval.txt 2> /dev/null
echo "   toronto:" >> a2-eval.txt
ssh root@172.17.15.2 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.15.2 ls /home  >> a2-eval.txt 2> /dev/null
echo "   ottawa:" >> a2-eval.txt
ssh root@172.17.15.3 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.15.3 ls /home  >> a2-eval.txt 2> /dev/null
echo "   kingston:" >> a2-eval.txt
ssh root@172.17.15.5 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.15.5 ls /home  >> a2-eval.txt 2> /dev/null
echo "   coburg:" >> a2-eval.txt
ssh root@172.17.15.6 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.15.6 ls /home  >> a2-eval.txt 2> /dev/null
echo "   milton:" >> a2-eval.txt
ssh root@172.17.15.8 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.15.8 ls /home  >> a2-eval.txt 2> /dev/null
echo >> a2-eval.txt

# Send report information to instructor

cat > message.txt <<+
If you have received this e-mail message, then
you have successfully submitted the remaining
information for your OPS335 assignment 2

+



mail -s "OPS335-a2-$fullName" Ryan.Lockhart@senecacollege.ca < a2-eval.txt
mail -s "OPS335-a2-confirmation" "$userID@myseneca.ca"  < message.txt

rm  a2-eval.txt message.txt 2> /dev/null

cat <<+

Submission of OPS335 assignment 2 completed.
A confirmation message should have been sent to your
Seneca email account. If you did NOT receive a confirmation
message, please check that you install the mailx package
or otherwise, contact your OPS335 instructor.

+







