#!/bin/bash
#########################################
#Copyright (c) 2023 ABLECLOUD Co. Ltd.
#
#무결성 검증 해시 값 초기화하는 스크립트
#
#최초작성자 : 류홍욱 선임
#최초작성일 : 2023-10-05
#########################################
LOGFILE="/var/log/mold_install.log"
DATABASE_PASSWD="Ablecloud1!"
DATABASE_PASSWD_ENC_KEY=$(openssl rand -base64 16)

correct_password="Ablecloud1!"

read -s -p "Please enter the password: " entered_password

if [ "$entered_password" == "$correct_password" ]; then
    # Integrity Verification 실행
    echo "Integrity verification in progress"

    # 주요 파일 초기 해시 값 추출
    paths=(
    "/etc/cloudstack/management/config.json"
    "/etc/cloudstack/management/db.properties"
    "/etc/cloudstack/management/environment.properties"
    "/etc/cloudstack/management/log4j-cloud.xml"
    "/etc/cloudstack/management/server.properties"
    "/etc/cloudstack/usage/log4j-cloud.xml"
    )
    for path in "${paths[@]}"; do
        for file in $(find "$path" -type f); do
            hash_value=$(sha512sum "$file" | awk '{print $1}')
            mysql --user=root --password=$DATABASE_PASSWD -e "use cloud; UPDATE integrity_verification_initial_hash SET initial_hash_value='$hash_value' WHERE mshost_id='1' AND file_path='$file';" > /dev/null 2>&1
        done
    done
    # jar 파일 초기 해시 값 추출
    directory="/usr/share/cloudstack-usage/lib"
    declare -a jar_file_paths
    for jar_file in $(find "$directory" -type f -name "*.jar"); do
        jar_file_paths+=("$jar_file")
    done
    for jar_path in "${jar_file_paths[@]}"; do
        for jar_file in $(find "$jar_path" -type f); do
            jar_hash_value=$(sha512sum "$jar_file" | awk '{print $1}')
            mysql --user=root --password=$DATABASE_PASSWD -e "use cloud; UPDATE integrity_verification_initial_hash SET initial_hash_value='$jar_hash_value' WHERE mshost_id='1' AND file_path='$jar_file';" > /dev/null 2>&1
        done
    done

    # 주요 파일 비교 해시 값 추출 후, 실패 파일 리스트 업데이트
    for path in "${paths[@]}"; do
        for file in $(find "$path" -type f); do
            hash_value=$(sha512sum "$file" | awk '{print $1}')
            mysql --user=root --password=$DATABASE_PASSWD -e "use cloud; UPDATE integrity_verification_initial_hash SET comparison_hash_value='$hash_value' WHERE mshost_id='1' AND file_path='$file';" > /dev/null 2>&1
            mysql --user=root --password=$DATABASE_PASSWD -e "use cloud; UPDATE integrity_verification_initial_hash SET verification_result = (initial_hash_value = comparison_hash_value);" | tail -n +2 
            value="$(mysql --user=root --password=$DATABASE_PASSWD -se "use cloud; SELECT file_path FROM integrity_verification_initial_hash WHERE mshost_id='1' AND file_path='$file' AND verification_result=0;")"
            if [ -n "$value" ]; then
                failed_files+="$value, "
            fi
        done
    done
    # jar 파일 비교 해시 값 추출 후, 실패 파일 리스트 업데이트
    for jar_path in "${jar_file_paths[@]}"; do
        for jar_file in $(find "$jar_path" -type f); do
            jar_hash_value=$(sha512sum "$jar_file" | awk '{print $1}')
            mysql --user=root --password=$DATABASE_PASSWD -e "use cloud; UPDATE integrity_verification_initial_hash SET comparison_hash_value='$jar_hash_value' WHERE mshost_id='1' AND file_path='$jar_file';" > /dev/null 2>&1
            mysql --user=root --password=$DATABASE_PASSWD -e "use cloud; UPDATE integrity_verification_initial_hash SET verification_result = (initial_hash_value = comparison_hash_value);" | tail -n +2
            value="$(mysql --user=root --password=$DATABASE_PASSWD -se "use cloud; SELECT file_path FROM integrity_verification_initial_hash WHERE mshost_id='1' AND file_path='$jar_file' AND verification_result=0;")" > /dev/null 2>&1
            if [ -n "$value" ]; then
                failed_files+="$value, "
            fi
        done
    done
    echo "Integrity verification is finished successfully"
else
    echo "Operation halted due to incorrect password."
fi

