#!/bin/bash
# MAAS
# Multiplexer Adaptive Adminstrator Solution

# Copyright (C) 2016 Aurélien DESBRIÈRES <aurelien@hackers.camp> 

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


maas() {


tput clear

the_user="$(whoami)"
the_admin="$(whoami)"
admin_ip="$(ip addr show $network | grep 'inet\b' | grep 192 | awk '{print $2}' | cut -d/ -f1)"

printf "\033[1;32mWelcome to MAAS - Multiplexer Adaptive Adminstrator Solution\033[0m%s\n"
printf "\033[1;32mMAAS offer you the way to works with different OS type like they were all the same\033[0m%s\n"

printf "\033[1;32mPlease report your needs to:\033[0m aurelien@hackers.camp%s\n"
printf "\033[1;32mYou need root privileges to run it\033[0m%s\n"

# Copy files in the working directory
mkdir -p /home/"$the_user"/MAAS-REPORT/
mkdir -p /home/"$the_user"/MAAS-REPORT/LOG/
cp run maas scan.awk faron.sh update.sh install.sh remove.sh virusscan.sh sysinfo.sh /home/"$the_user"/MAAS/

cp -r mussh /home/"$the_user"/MAAS/
cd /home/"$the_user"/MAAS/


# Register the user identification during process
printf "\033[1;32m%s\nRegistering you identification during the MAAS process\033[0m%s\n"
if [ ! -S ~/.ssh/ssh_auth_sock ]; then
    eval "$(ssh-agent)"
    ln -sf "$SSH_AUTH_SOCK" ~/.ssh/ssh_auth_sock
fi
export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock
ssh-add -l | grep "The agent has no identities" && ssh-add


# copy files to remote host
printf "\033[1;32m%s\nCoying files on remote machines\033[0m%s\n"
for s in $(cat maas-hostfile) ; do
    scp -r /home/"$the_user"/MAAS/ "$s":~/MAAS/
done

# Request the user what he want to do
# Execute the file in git during the configuration on the machine
while true; do
    echo "What do you want to do?"
    read -p "Update & Upgrade | Install | Remove | Command | Forensic analysis | Virus Scan | Hardware Information? " uircfvh
    case $uircfvh in
	
	# Full Update
	[Uu]* ) mussh/mussh -a -i /home/"$the_user"/.ssh/"$private" -d -H maas-hostfile -C 'update.sh' -m2
		break;;
	
	# Search for a package
	[Ss]* ) echo "which software are you looking for? "
	        read soft_search
		sed -i "s/soft_search/$soft_search/g" search.sh
		chmod +x search.sh
		echo "Loading MAAS search please wait "
		mussh/mussh -a -i /home/"$the_user"/.ssh/"$private" -d -H maas-hostfile -C "search.sh" -m2
		break;;
	
	# Install a package
	[Ii]* ) echo "which software do you wish to install? "
		read soft_install
		sed -i "s/soft_install/$soft_install/g" install.sh
		chmod +x install.sh
		echo "Loading MAAS install please wait "
		mussh/mussh -a -i /home/"$the_user"/.ssh/"$private" -d -H maas-hostfile -C 'install.sh' -m2
		break;;

		# Remove a package
	[Rr]* ) echo "which software do you wish to remove? "
		read soft_remove
		sed -i "s/soft_remove/$soft_remove/g" remove.sh
		chmod +x remove.sh
		echo "Loading MAAS remove please wait "
		mussh/mussh -a -i /home/"$the_user"/.ssh/"$private" -d -H maas-hostfile -C 'remove.sh' -m2
		break;;
	
	# Load a command
	[Cc]* ) echo "which command do you want to load? "
		read command_request
		echo "Loading MAAS command please wait "
		mussh/mussh -a -i /home/"$the_user"/.ssh/"$private" -d -H maas-hostfile -c "$command_request" -m2
		break;;

	
	# Forensic Scan & Log
	[Ff]* ) echo "MAAS will now load FARON - Forensic Analyser Remote Over Network"
		mussh/mussh -a -i /home/"$the_user"/.ssh/"$private" -d -H maas-hostfile -c 'sh /root/MAAS/faron.sh' -m2

		# Get the log back
		printf "\033[1;32m\nGetting all logs back ...\033[0m%s\n"

		for s in $(cat maas-hostfile) ; do
		    scp "$s":~/maas-log*tar.gz /home/"$the_user"/MAAS-REPORT/LOG/
		done
		break;;


	# Virus Scan & Log
	[Vv]* ) echo "MAAS will now load Virus Scan - Scan, log and report"
		mkdir ~/MAAS-REPORT/LOG/
		mussh/mussh -a -i /home/"$the_user"/.ssh/"$private" -d -H maas-hostfile -C 'virusscan.sh' -m2

		# Get the log back
		printf "\033[1;32m\nGetting all logs back ...\033[0m%s\n"

		for s in $(cat maas-hostfile) ; do
		    scp "$s":~/maas-virus-log*tar.gz /home/"$the_user"/MAAS-REPORT/LOG/
		done
		break;;
	

	# Hardware and System Information & Log
	[Hh]* ) echo "MAAS will now load The Hardware and System Information Scan - Scan, log and report"
		mkdir ~/MAAS-REPORT/LOG/
		mussh/mussh -a -i /home/"$the_user"/.ssh/"$private" -d -H maas-hostfile -C 'sysinfo.sh' -m2

		# Get the log back
		printf "\033[1;32m\nGetting all logs back ...\033[0m%s\n"

		for s in $(cat maas-hostfile) ; do
		    scp "$s":~/maas-sysinfo-log*tar.gz /home/"$the_user"/MAAS-REPORT/LOG/
		done
		break;;
	
	# request of the correct word / letter
	* ) echo "Please answer 'U'pdate/'U'pgrade or 'I'nstall or 'C'ommand or 'F'orensic or 'V'irus scan.";;
    esac
done

}
