## Bu dosya ansible tarafindan yonetilmektedir!
## Burada yapilan degisikliklerin uzerine yazilir!!
{{ ansible_managed }}

#!/bin/bash
export PATH=/bin:/usr/bin:/usr/local/bin:/sbin

# LOG DIRECTORIES
signingdirectory={{ signer['signingdirectory'] }}
signedlogs={{ signer['signedlogs'] }}
invalidlogs={{ signer['invalidlogs'] }}

# LOG FILES
date=$(date --date='yesterday' "+%Y-%m-%d")
logfiledate=$(date "+%Y%m%d")
logfile=*_raw.log-$logfiledate*
logs={{ signer['logs'] }}

#################################################
# Create Daily Log Directories If Does not Exist
if [ ! -d $signedlogs/$date  ]
then
        mkdir -p $signedlogs/$date
fi

if [ ! -d $invalidlogs/$date  ]
then
        mkdir -p $invalidlogs/$date
fi
#################################################

serverfiles="{{ signer['serverfiles'] }}"

{% if signer['type'] == "remote" %}

username={{ signer['username'] }}
password={{ signer['password'] }}
proxyUser={{ signer['proxyuser'] }}
proxyPassword={{ signer['proxypassword'] }}
proxyIp={{ signer['proxyip'] }}
proxyPort={{ signer['proxyport'] }}

java=/usr/bin/java
command={{ signer['command'] }}

cd  $logs
serverlist=($(ls -d $serverfiles))


# Create Daily Log Directory If Does not Exist
    
if [ ! -d $signingdirectory/$date ]
then
    mkdir -p $signingdirectory/$date
fi

SignedName={{ signer['signedname'] }}
logpackage=$SignedName.$date.tgz
tsfile=$SignedName.$date.tgz.zd
signedlogpackage=$SignedName.$date.logs.tar

for server in "${serverlist[@]}"
do    
    logdirectory=$logs/$server
    # Copy Application Log to Signing Directory
    rsync -a $logdirectory/$logfile $signingdirectory/$date
done

## Compress The Log Directory
cd $signingdirectory; env GZIP=-9 tar cvfz $logpackage $date
sleep 3

# Sign The Compressed Logs
cd $signingdirectory; $java -jar $command -z $logpackage http://zd.kamusm.gov.tr 80 $username $password $proxyIp $proxyPort $proxyUser $proxyPassword sha-256
#cd $signingdirectory; $java -jar $command -z $logpackage  http://zd.kamusm.gov.tr 80 $username $password sha-256
sleep 3

# Check The Signature Validity
check=`$java -jar $command -c $logpackage $tsfile`

# Move Log Package to Archive
if [ "${check}" != "Zaman Damgasi gecerli, dosya degismemis." ]
then
    tar cvf $signedlogpackage $logpackage $tsfile
    rsync -a $signingdirectory/$signedlogpackage $invalidlogs/$date/
            ######          mail send       #########
else
    tar cvf $signedlogpackage $logpackage $tsfile
    rsync -a $signingdirectory/$signedlogpackage $signedlogs/$date/
fi
rm -rf $signingdirectory/*


{% elif signer['type'] == "local" %}

# Archive Directories
signedlogsarchive={{ signer['signedlogsarchive'] }}
invalidlogsarchive={{ signer['invalidlogsarchive'] }}

# OpenSSL and TSA files
openssl=/usr/bin/openssl
opensslconf=/etc/ssl/openssl.cnf
CAPublicKEY=/etc/ssl/certs/root-ca.pem
TSAPublicKEY=/etc/ssl/certs/root-ca.pem

#################################################
# Create Archive Log Directories If Does not Exist
if [ ! -d $signedlogsarchive/$date  ]
then
        mkdir -p $signedlogsarchive/$date
fi
if [ ! -d $invalidlogsarchive/$date  ]
then
        mkdir -p $invalidlogsarchive/$date
fi
#################################################

cd  $logs
serverlist=($(ls -d $serverfiles))
for server in "${serverlist[@]}"
do
    # Create Daily Log Directory If Does not Exist
    if [ ! -d $signingdirectory/$date ]
    then
        mkdir -p $signingdirectory/$date
    fi

    logdirectory=$logs/$server
    logpackage=$server.$date.tgz
    signedlogpackage=$server.$date.logs.tar

    # Copy Application Log to Signing Directory
    rsync -a $logdirectory/$logfile $signingdirectory/$date

    ## Compress The Log Directory
    cd $signingdirectory; env GZIP=-9 tar cvfz $logpackage $date
    sleep 3

    # Sign The Compressed Logs
    cd $signingdirectory
    $openssl ts -query -data $logpackage -no_nonce -out $logpackage.tsq
    $openssl ts -reply -config $opensslconf -queryfile $logpackage.tsq -out $logpackage.der -token_out -passin pass:'LinuxAkademi'
    sleep 3

    # Check The Signature Validity
    verify=`$openssl ts -verify -data $logpackage -in $logpackage.der -token_in -CAfile  $CAPublicKEY -untrusted $TSAPublicKEY`
    signedfilesize=$(stat -c%s "$logpackage")

    # Move Log Package to Archive
    if [[ "$verify" == "Verification: OK" && $signedfilesize -gt "1000" ]];
    then
        tar cvf $signedlogpackage $logpackage
        rsync -a $signingdirectory/$signedlogpackage $signedlogs/$date/
        rsync -a $signingdirectory/$signedlogpackage $signedlogsarchive/$date/
        ######          mail send       #########
    else
        tar cvf $signedlogpackage $logpackage
        rsync -a $signingdirectory/$signedlogpackage $invalidlogs/$date/
        rsync -a $signingdirectory/$signedlogpackage $invalidlogsarchive/$date/
    fi
    rm -rf $signingdirectory/*
done

{% endif %}
