freedombone-utils-cron 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Cron functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. function cron_add_mins {
  29. if ! grep -q "${2}" /etc/crontab; then
  30. job_user='root'
  31. if [ "$3" ]; then
  32. job_user=$3
  33. fi
  34. echo "*/${1} * * * * ${job_user} ${2}" >> /etc/crontab
  35. systemctl restart cron
  36. fi
  37. }
  38. function randomize_cron {
  39. # The predictable default timing of Debian cron jobs might
  40. # be exploitable knowledge. Avoid too much predictability
  41. # by randomizing the times when cron jobs run
  42. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  43. return
  44. fi
  45. # randomize the day on which the weekly cron job runs
  46. randdow=$((RANDOM%6+1))
  47. sed -i "s|\\* \\* 7|* * $randdow|g" /etc/crontab
  48. # randomize the time when the weekly cron job runs
  49. randmin=$((RANDOM%60))
  50. randhr=$((RANDOM%3+1))
  51. sed -i "s|47 6|$randmin $randhr|g" /etc/crontab
  52. # randomize the time when the daily cron job runs
  53. randmin=$((RANDOM%60))
  54. randhr=$((RANDOM%3+4))
  55. sed -i "s|25 6\\t\\* \\* \\*|$randmin $randhr\\t* * *|g" /etc/crontab
  56. # randomize the time when the hourly cron job runs
  57. randmin=$((RANDOM%60))
  58. sed -i "s|17 \\*\\t|$randmin *\\t|g" /etc/crontab
  59. # randomize monthly cron job time and day
  60. randmin=$((RANDOM%60))
  61. randhr=$((RANDOM%22+1))
  62. randdom=$((RANDOM%27+1))
  63. sed -i "s|52 6\\t|$randmin $randhr\\t|g" /etc/crontab
  64. sed -i "s|\\t1 \\* \\*|\\t$randdom * *|g" /etc/crontab
  65. systemctl restart cron
  66. mark_completed "${FUNCNAME[0]}"
  67. }
  68. function schedule_stig_tests {
  69. stig_tests_script=/tmp/stig_tests_script
  70. { echo '#!/bin/bash';
  71. echo "ADMIN_EMAIL_ADDRESS=${MY_USERNAME}@\${HOSTNAME}";
  72. echo "pkill ${PROJECT_NAME}-tests";
  73. echo 'rm -rf /tmp/*';
  74. echo "${PROJECT_NAME}-tests --stig yes > /tmp/daily-stig-tests";
  75. echo 'if [ ! "$?" = "0" ]; then';
  76. echo " echo \"\$(cat /tmp/daily-stig-tests)\" | mail -s \"${PROJECT_NAME} STIG test failures\" \$ADMIN_EMAIL_ADDRESS";
  77. echo 'fi';
  78. echo 'if [ -f /tmp/daily-stig-tests ]; then';
  79. echo ' rm /tmp/daily-stig-tests';
  80. echo 'fi'; } > $stig_tests_script
  81. chmod +x $stig_tests_script
  82. if [ ! -f /etc/cron.daily/stig_tests ]; then
  83. cp $stig_tests_script /etc/cron.daily/stig_tests
  84. else
  85. HASH1=$(sha256sum $stig_tests_script | awk -F ' ' '{print $1}')
  86. HASH2=$(sha256sum /etc/cron.daily/stig_tests | awk -F ' ' '{print $1}')
  87. if [[ "$HASH1" != "$HASH2" ]]; then
  88. cp $stig_tests_script /etc/cron.daily/stig_tests
  89. fi
  90. fi
  91. rm $stig_tests_script
  92. }
  93. # NOTE: deliberately there is no "exit 0"