#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Updates the date

# License
# =======
#
# Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

PROJECT_NAME='freedombone'

export TEXTDOMAIN=${PROJECT_NAME}-update-date
export TEXTDOMAINDIR="/usr/share/locale"

COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt
TIMESOURCE='google.com'
TIMESOURCE2='www.ptb.de'
LOGFILE=/var/log/tlsdate.log
TIMEOUT=5
EMAIL=
if grep -q "Admin user" $COMPLETION_FILE; then
    ADMIN_USER=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
    EMAIL=$ADMIN_USER@$HOSTNAME
fi

# File which contains the previous date as a number
BEFORE_DATE_FILE=/var/log/tlsdateprevious.txt

# File which contains the previous date as a string
BEFORE_FULLDATE_FILE=/var/log/tlsdate.txt
DATE_BEFORE=$(date)
BEFORE=$(date "+%s")
BACKWARDS_BETWEEN=0
re="^[0-9]+$"

# If the date was previously set
if [ -s "$BEFORE_DATE_FILE" ]; then
    filesize=$(wc -c "$BEFORE_DATE_FILE" | cut -f 1 -d " ")
    if [ $filesize -ge 5 ]; then
        BEFORE_FILE=$(cat $BEFORE_DATE_FILE)
        if [[ $BEFORE_FILE =~ $re ]]; then
            BEFORE_FULLDATE=$(cat $BEFORE_FULLDATE_FILE)
            # is the date going backwards?
            if (( $BEFORE_FILE > $BEFORE )); then
                echo -n $"Date went backwards between tlsdate updates. " >> $LOGFILE
                echo -n "$BEFORE_FILE > $BEFORE, " >> $LOGFILE
                echo "$BEFORE_FULLDATE > $DATE_BEFORE" >> $LOGFILE
                # Send a warning email
                if [ $EMAIL ]; then
                    echo $(tail $LOGFILE -n 2) | mail -s $"tlsdate anomaly" $EMAIL
                fi
                # Try another time source
                TIMESOURCE=$TIMESOURCE2
                # try running without any parameters
                tlsdate >> $LOGFILE
                BACKWARDS_BETWEEN=1
            fi
        fi
    fi
fi

# Set the date
/usr/bin/timeout $TIMEOUT tlsdate -l -t -H $TIMESOURCE -p 443 >> $LOGFILE
DATE_AFTER=$(date)
AFTER=$(date "+%s")

# After setting the date did it go backwards?
if (( $AFTER < $BEFORE )); then
    echo $"Incorrect date: $DATE_BEFORE -> $DATE_AFTER" >> $LOGFILE
    # Send a warning email
    if [ $EMAIL ]; then
        echo $(tail $LOGFILE -n 2) | mail -s $"tlsdate anomaly" $EMAIL
    fi
    # Try resetting the date from another time source
    /usr/bin/timeout $TIMEOUT tlsdate -l -t -H $TIMESOURCE2 -p 443 >> $LOGFILE
    DATE_AFTER=$(date)
    AFTER=$(date "+%s")
else
    echo -n $TIMESOURCE >> $LOGFILE
    if [ -s "$BEFORE_DATE_FILE" ]; then
        echo -n " " >> $LOGFILE
        echo -n $BEFORE_FILE >> $LOGFILE
    fi
    echo -n " " >> $LOGFILE
    echo -n $BEFORE >> $LOGFILE
    echo -n " " >> $LOGFILE
    echo -n $AFTER >> $LOGFILE
    echo -n " " >> $LOGFILE
    echo $DATE_AFTER >> $LOGFILE
fi

# Log the last date
if [[ $BACKWARDS_BETWEEN == 0 ]]; then
    echo "$AFTER" > $BEFORE_DATE_FILE
    echo "$DATE_AFTER" > $BEFORE_FULLDATE_FILE
    exit 0
fi
exit 1
