#!/bin/bash
source /OSM/script/os_load_base.sh
ALM_RESTORE_SERVER="$1"
ALM_RESTORE_USER="$2"
ALM_RESTORE_PASSWORD="$3"
CON_TIMEOUT=5
ALM_PROTOCOL_TYLE="$4"
ALM_PORT="$5"
SET_PORT=""
ALARM_RESTORE_RESULT="/tmp/restore_result_$$"

# Catch the exit signals, and clean up temp files
trap "RESTORE_FAIL_CLEAN_UP" HUP INT QUIT TERM

# **************************************************************************** #
# Function Name: ALM_RESTORE_CHECK_USER()
# Description: check user is OK with name & pw
# Parameter: 0
# Return: 0
# Author: xxx
# **************************************************************************** #
ALM_RESTORE_CHECK_USER()
{
    local L_RET=0
    lftp << EOF
set net:reconnect-interval-base 1 
set net:max-retries 1
set ftp:ssl-allow no
set sftp:auto-confirm yes
set net:timeout 4s
open -u ${ALM_RESTORE_USER},${ALM_RESTORE_PASSWORD} ${FTP_PROTOCOL}${ALM_RESTORE_SERVER} ${SET_PORT}
debug 4 -o ${ALARM_RESTORE_RESULT}
cd . >/dev/null
EOF
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "Connect error."
        return 1
    fi
    return 0
}

# **************************************************************************** #
# Function Name: ALM_RESTORE_IsLFtpSucc
# Description: check whether ftp is succ
# Parameter: 1-result file
# Return: 0-succ 1-fail
# **************************************************************************** #
ALM_RESTORE_IsLFtpSucc() {
    L_RESULT=$1
#1、运行到这说明连接失败
#2、根据协议类型是FTP还是SFTP分别判断
#3、在FTP协议下只有“用户名密码错误”和“连接错误”两种状态：
#       密码错误时，有“530 登录失败”或者“530 Login incorrect”可以鉴别
#       剩余失败的情况均认为是连接失败
#4、SFTP协议下有Host key校验失败错误，可根据“Host key verification failed”鉴别，先鉴别；
#5、在SFTP协议下没有明确回显体现用户名密码错误，现已知可能是用户名密码错误的返回值有：
#   “Permission denied” “Received disconnect from xx.xx.xx.xx: 4:” “Password authentication failed”
#6、当未获取到以上可能的用户名密码错误或HOSTKEY失败提示信息时，返回连接错误。
    if [ -f "$L_RESULT" ];then 
        if [ "$ALM_PROTOCOL_TYLE" -eq 0 ]; then
            L_LOGIN_ERROR=`cat $L_RESULT | grep "530"`
            L_LOGIN_INCORRECT=`cat $L_RESULT | grep "Login incorrect"`
            L_PASSWORD_INCORRECT=`cat $L_RESULT | grep "password incorrect"`
            if [ -n "$L_LOGIN_ERROR" -o -n "$L_LOGIN_INCORRECT" -o -n "$L_PASSWORD_INCORRECT" ];then
                return 2
            fi
        else
            L_VERIFY_ERROR=`cat $L_RESULT | grep "Host key verification failed"`
            if [ -n "$L_VERIFY_ERROR" ];then
                return 4
            fi
            #密码未授权
            L_PASSWORD_ERROR=`cat $L_RESULT | grep "Password authentication failed"`
            #权限被拒绝
            L_PERMISSION_DENIED=`cat $L_RESULT | grep "Permission denied"`
            #断开连接，错误码4
            L_DISCONNECT_ERROR=`cat $L_RESULT | grep "Received disconnect from" | grep "4:"`
            if [ -n "$L_PASSWORD_ERROR" -o -n "$L_DISCONNECT_ERROR" -o -n "$L_PERMISSION_DENIED" ];then
                return 2
            fi
        fi
    fi
    return 1
}

# **************************************************************************** #
# Function Name: ALM_RESTORE_ProducelftpCmd()
# Description: produce lftp cmd file
# Parameter: 0
# Return: 0
# Author: xxx
# **************************************************************************** #
ALM_RESTORE_ProducelftpCmd()
{ 
    if [ "$ALM_PROTOCOL_TYLE" -eq 0 ]; then
        FTP_PROTOCOL="ftp://"
        if [ -z "$ALM_PORT" ]; then
            SET_PORT=""
        else
            SET_PORT="-p $ALM_PORT"
        fi
    else
        FTP_PROTOCOL="sftp://"
        SET_PORT="-p $ALM_PORT"
    fi
    return 0
}

# **************************************************************************** #
# Function Name: RESTORE_FAIL_CLEAN_UP
# Description:   Clean up the tmp files when the script receives signals.
# Parameter:     void
# Return:        1-fail
# **************************************************************************** #
RESTORE_FAIL_CLEAN_UP()
{
    OS_echo "Receives signals while restoring files."
    rm -rf $ALARM_RESTORE_RESULT &>/dev/null

    exit 1
}
# --------------------------  main  ------------------------------------------ #
#1. set&&produce lftp command
ALM_RESTORE_ProducelftpCmd

ALM_RESTORE_CHECK_USER
iRet=$?

if [ 0 -eq ${iRet} ];then
    rm -rf ${ALARM_RESTORE_RESULT} &>/dev/null
    exit 0
fi

#3.check if ftp success
ALM_RESTORE_IsLFtpSucc ${ALARM_RESTORE_RESULT}
ret=$?

rm -rf ${ALARM_RESTORE_RESULT} &>/dev/null

exit ${ret}

