#!/bin/bash
#
# Version: 0.1
# Auther : zhuanglei.chen@lingycloud.com


red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

os=
now=`date +"%Y%m%d%H%M%S"`


ensure_bash() {
    set -e

    if [ "$BASH_VERSION" = "" ]; then
        echo "Should be run by /bin/bash"
        exit 1
    fi

    set +e
}
ensure_bash


get_os() {
    set -e

    if [[ -e /etc/centos-release || -e /etc/redhat-release ]]; then
        os=centos
    else
        echo -e "${red}Looks like you aren't running CentOS${plain}"
        exit 1
    fi

    set +e
}


create_user_cloud() {
    local A

    grep -E "^cloud:" /etc/passwd >& /dev/null
    if [ $? -ne 0 ]
    then
        set -e

        echo -e -n "${yellow}Create cloud user? (y/n):${plain} "
        read A
        if [ "$A" = "y" ]; then
            useradd -m -s /bin/bash -U cloud
        else
            echo -e "${red}Abort!${plain}"
            exit 1
        fi
    fi

    set -e

    mkdir -p /home/cloud/.ssh
    echo -n "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJBGElzu0pi2OBQzv5+u55apv"   > /home/cloud/.ssh/authorized_keys
    echo -n "TdHvM9f1VdanWmVl2x/yop2FHLdrd/yYSK30szWnH+VB+Ia1ebVmKbvvy3k5FCU"   >> /home/cloud/.ssh/authorized_keys
    echo -n "h2esXcUs7jGUVgtW5S9/58GHcr1pbWirEPBnUXG8ffyEz0mL3ujFjVG0lyvOAtF"   >> /home/cloud/.ssh/authorized_keys
    echo -n "MeyWvBzzrP6JjuTpUxgP6+jj/U7ZtCn7u6NsFGKz9exsLIk8vOnb4SlbpzUSOkO"   >> /home/cloud/.ssh/authorized_keys
    echo -n "qJp5QMZfNq9cAv9u5+TRRRJhXhahRklr0oONfWJTMvyG5/Whe6to1yU9bgDb5+K"   >> /home/cloud/.ssh/authorized_keys
    echo    "3dIXEzXSiOGBLGy8nwoJ4AA8jGtqIbHoPdNeJawQbg690p0ndh85dtEGKedVgVoVN" >> /home/cloud/.ssh/authorized_keys
    mkdir -p /root/.ssh
    echo -e -n "\nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJBGElzu0pi2OBQzv5+u55apv"   >> /root/.ssh/authorized_keys
    echo -n "TdHvM9f1VdanWmVl2x/yop2FHLdrd/yYSK30szWnH+VB+Ia1ebVmKbvvy3k5FCU"   >> /root/.ssh/authorized_keys
    echo -n "h2esXcUs7jGUVgtW5S9/58GHcr1pbWirEPBnUXG8ffyEz0mL3ujFjVG0lyvOAtF"   >> /root/.ssh/authorized_keys
    echo -n "MeyWvBzzrP6JjuTpUxgP6+jj/U7ZtCn7u6NsFGKz9exsLIk8vOnb4SlbpzUSOkO"   >> /root/.ssh/authorized_keys
    echo -n "qJp5QMZfNq9cAv9u5+TRRRJhXhahRklr0oONfWJTMvyG5/Whe6to1yU9bgDb5+K"   >> /root/.ssh/authorized_keys
    echo    "3dIXEzXSiOGBLGy8nwoJ4AA8jGtqIbHoPdNeJawQbg690p0ndh85dtEGKedVgVoVN" >> /root/.ssh/authorized_keys

    set +e
}


change_root_pass() {
    set -e

    echo -e -n "${yellow}Change root password? (y/n):${plain} "
    read A
    if [ "$A" = "y" ]; then
        passwd root
    fi

    set +e
}


change_sshd_config() {
    set -e

    local A

    echo -e "${yellow}sshd will be configured with the following items:${plain}"
    echo -e "${green}Port 51999\nPort 22\nAuthorizedKeysFile .ssh/authorized_keys\nUseDNS no\nPermitRootLogin yes\nPasswordAuthentication yes\nAllowUsers root cloud${plain}"
    echo -e -n "${yellow}Is that OK? (y/n):${plain} "

    read A
    if [ "$A" != "y" ]; then
        echo -e "${red}Abort!${plain}"
        exit 1
    fi

    local bak
    bak=/tmp/sshd_config-${now}
    echo -e "${yellow}cp -f /etc/ssh/sshd_config ${bak}${plain}"
    cp -f /etc/ssh/sshd_config $bak
    grep -E -v 'Port|AuthorizedKeysFile|UseDNS|PermitRootLogin|PasswordAuthentication|AllowUsers' $bak > /etc/ssh/sshd_config
    echo >> /etc/ssh/sshd_config

    echo -e "Port 51999\nPort 22\nAuthorizedKeysFile .ssh/authorized_keys\nUseDNS no\nPermitRootLogin yes\nPasswordAuthentication yes\nAllowUsers root cloud" >> /etc/ssh/sshd_config

    echo -e -n "${yellow}Edit /etc/ssh/sshd_config? (y/n):${plain} "
    read A
    if [ "$A" = "y" ]; then
        vi /etc/ssh/sshd_config
    fi

    echo -e -n "${yellow}Restart sshd? (y/n):${plain} "
    read A
    if [ "$A" = "y" ]; then
        echo -e "${red}sshd will be restarted, don't close this shell!\nOpen another shell and try log in, if anything is wrong,\ncopy $bak back to /etc/ssh/sshd_config, and restart sshd${plain}"
        systemctl restart sshd
    fi

    set +e
}


get_os
create_user_cloud
change_root_pass
change_sshd_config
