freedombone-utils-cron 3.8KB

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