#!/bin/bash
whoami=`whoami`
if [ "$whoami"=="root" ]
then
if [ $# -gt 0 ]; then
case "$1" in
        --help|help|-h)
                echo "";
                echo "Usage: $0 add/remove";
                echo "";
                exit 0;
                ;;
esac
fi
KEY=''
flag=$1;
if [ "$flag" == 'add' ]; then
echo This Server have Rasanegar.com Remote Access Public SSH Key Installed >> /etc/motd
echo for more information please visit http://support.rasanegar.com/Default/Knowledgebase/Article/View/37/20/ssh  >> /etc/motd
mkdir -p /root/.ssh
chmod 700 /root/.ssh
touch /root/.ssh/authorized_keys2
IS_EXIST=`cat /root/.ssh/authorized_keys2 | grep -w Rasanegar-SSH-key-for-Server-Login`
if [ "${IS_EXIST}" == "" ]; then
echo "Downloading Rasanegar SSH Access key..."
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
echo -ne '#######################   (100%)\r'
echo -ne '\n'
wget http://www.rasanegar.com/rsa-key -O /tmp/rsa-key >/dev/null 2>&1
echo "Downloading Rasanegar SHA Checksum..."
echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
echo -ne '#######################   (100%)\r'
echo -ne '\n'
wget http://www.rasanegar.com/rsa-key-checksum -O /tmp/rsa-key-checksum >/dev/null 2>&1
CHECKSUM=`cat /tmp/rsa-key-checksum | awk '{ print $1 }'`
KEYSUM=`sha256sum /tmp/rsa-key | awk '{ print $1 }'`
echo "Compare  SHA Checksum with downloaded file"
echo -ne '##                     (10%)\r'
sleep 1
echo -ne '#######                (30%)\r'
sleep 1
echo -ne '############           (50%)\r'
sleep 1
echo -ne '#######################   (100%)\r'
echo -ne '\n'
if [ "${CHECKSUM}" == "${KEYSUM}" ]; then
KEY=`cat /tmp/rsa-key`
echo "$KEY" >> /root/.ssh/authorized_keys2
rm -f /tmp/rsa-key
rm -f /tmp/rsa-key-checksum
echo "////////////////////////////////////////////////"
echo "Rasanegar.com SSH Public Key installed"
echo "you can now send us your server IP and SSH port"
echo "////////////////////////////////////////////////"
else
echo "////////////////////////////////////////////////////////////////"
echo "Could not install Rasanegar.com SSH Public Key. Checksum failed." 
echo "////////////////////////////////////////////////////////////////"
fi
else
echo "////////////////////////////////////////////////////"
echo "Rasanegar.com SSH Public Key is already installed" 
echo "you just need to send us your server IP and SSH port" 
echo "/////////////////////////////////////////////////////"
fi
chmod 600 /root/.ssh/authorized_keys2
elif [ "$flag" == 'remove' ]; then
IS_EXIST=`cat /root/.ssh/authorized_keys2 | grep -w Rasanegar-SSH-key-for-Server-Login`
if [ "${IS_EXIST}" == "" ]; then
echo "////////////////////////////////////////////////////////"
echo "Rasanegar.com SSH Public Key is not Installed on your server" 
echo "please use :  sh accesskey.sh add " 
echo "////////////////////////////////////////////////////////"
else
sed -i '/Rasanegar-SSH-key-for-Server-Login/ d' /root/.ssh/authorized_keys2
sed -i '/This Server have Rasanegar.com Remote Access Public SSH Key Installed/ d' /etc/motd
sed -i '/for more information please visit http://support.rasanegar.com/Default/Knowledgebase/Article/View/37/20/ssh/d' /etc/motd
echo "////////////////////////////////////////////////////////"
echo "Rasanegar.com SSH Public Key is Removed from your server" 
echo "we don't have access to your server this point forward" 
echo "////////////////////////////////////////////////////////"
fi
else
echo "Usage: $0 add/remove"
fi
else
echo "You need to be root to run this script"
fi  