freedomboneeee/src/freedombone-utils-cron

106 lines
3.5 KiB
Plaintext
Raw Permalink Normal View History

2016-07-03 17:13:34 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-07-03 17:13:34 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-07-03 17:13:34 +02:00
#
# Cron functions
#
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
2016-07-03 17:13:34 +02:00
#
# 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/>.
function cron_add_mins {
2016-10-16 20:50:56 +02:00
if ! grep -q "${2}" /etc/crontab; then
2016-10-25 21:59:03 +02:00
job_user='root'
2018-02-25 16:16:23 +01:00
if [ "$3" ]; then
2016-10-25 21:59:03 +02:00
job_user=$3
fi
echo "*/${1} * * * * ${job_user} ${2}" >> /etc/crontab
2016-10-16 20:50:56 +02:00
systemctl restart cron
fi
2016-07-03 17:13:34 +02:00
}
function randomize_cron {
2016-10-16 20:50:56 +02:00
# The predictable default timing of Debian cron jobs might
# be exploitable knowledge. Avoid too much predictability
# by randomizing the times when cron jobs run
2018-02-25 13:50:46 +01:00
if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
2016-10-16 20:50:56 +02:00
return
fi
2016-07-03 17:13:34 +02:00
2016-10-16 20:50:56 +02:00
# randomize the day on which the weekly cron job runs
2018-02-25 16:16:23 +01:00
randdow=$((RANDOM%6+1))
sed -i "s|\\* \\* 7|* * $randdow|g" /etc/crontab
2016-07-03 17:13:34 +02:00
2016-10-16 20:50:56 +02:00
# randomize the time when the weekly cron job runs
2018-02-25 16:16:23 +01:00
randmin=$((RANDOM%60))
randhr=$((RANDOM%3+1))
2016-10-16 20:50:56 +02:00
sed -i "s|47 6|$randmin $randhr|g" /etc/crontab
2016-07-03 17:13:34 +02:00
2016-10-16 20:50:56 +02:00
# randomize the time when the daily cron job runs
2018-02-25 16:16:23 +01:00
randmin=$((RANDOM%60))
randhr=$((RANDOM%3+4))
sed -i "s|25 6\\t\\* \\* \\*|$randmin $randhr\\t* * *|g" /etc/crontab
2016-07-03 17:13:34 +02:00
2016-10-16 20:50:56 +02:00
# randomize the time when the hourly cron job runs
2018-02-25 16:16:23 +01:00
randmin=$((RANDOM%60))
sed -i "s|17 \\*\\t|$randmin *\\t|g" /etc/crontab
2016-07-03 17:13:34 +02:00
2016-10-16 20:50:56 +02:00
# randomize monthly cron job time and day
2018-02-25 16:16:23 +01:00
randmin=$((RANDOM%60))
randhr=$((RANDOM%22+1))
randdom=$((RANDOM%27+1))
sed -i "s|52 6\\t|$randmin $randhr\\t|g" /etc/crontab
sed -i "s|\\t1 \\* \\*|\\t$randdom * *|g" /etc/crontab
2016-07-03 17:13:34 +02:00
2016-10-16 20:50:56 +02:00
systemctl restart cron
2016-07-03 17:13:34 +02:00
2018-02-25 13:50:46 +01:00
mark_completed "${FUNCNAME[0]}"
2016-07-03 17:13:34 +02:00
}
2016-11-30 22:00:17 +01:00
function schedule_stig_tests {
stig_tests_script=/tmp/stig_tests_script
2018-02-25 16:16:23 +01:00
{ echo '#!/bin/bash';
echo "ADMIN_EMAIL_ADDRESS=${MY_USERNAME}@\${HOSTNAME}";
echo "pkill ${PROJECT_NAME}-tests";
echo 'rm -rf /tmp/*';
echo "${PROJECT_NAME}-tests --stig yes > /tmp/daily-stig-tests";
echo 'if [ ! "$?" = "0" ]; then';
echo " echo \"\$(cat /tmp/daily-stig-tests)\" | mail -s \"${PROJECT_NAME} STIG test failures\" \$ADMIN_EMAIL_ADDRESS";
echo 'fi';
echo 'if [ -f /tmp/daily-stig-tests ]; then';
echo ' rm /tmp/daily-stig-tests';
echo 'fi'; } > $stig_tests_script
chmod +x $stig_tests_script
if [ ! -f /etc/cron.daily/stig_tests ]; then
cp $stig_tests_script /etc/cron.daily/stig_tests
else
HASH1=$(sha256sum $stig_tests_script | awk -F ' ' '{print $1}')
HASH2=$(sha256sum /etc/cron.daily/stig_tests | awk -F ' ' '{print $1}')
if [[ "$HASH1" != "$HASH2" ]]; then
cp $stig_tests_script /etc/cron.daily/stig_tests
fi
fi
rm $stig_tests_script
2016-11-30 22:00:17 +01:00
}
2016-07-03 17:13:34 +02:00
# NOTE: deliberately there is no "exit 0"