diff --git a/src/freedombone-app-keyserver b/src/freedombone-app-keyserver index 59792f04414e3136f0981386e3f943049db46bcb..0a1aca39d394badc8079bb5e2742ae37f953ab23 100755 --- a/src/freedombone-app-keyserver +++ b/src/freedombone-app-keyserver @@ -68,14 +68,12 @@ function keyserver_watchdog { { echo '#!/bin/bash'; echo "dirsize=\$(du /var/lib/sks/DB | awk -F ' ' '{print \$1}')"; echo "if [ \$dirsize -gt 450000 ]; then"; - - echo " echo \"$keyserver_size_warning\" | mail -s \"$keyserver_mail_subject_line\" $ADMIN_EMAIL_ADDRESS"; - + echo " ${PROJECT_NAME}-notification -m \"$keyserver_size_warning\" -s \"$keyserver_mail_subject_line\""; echo " if [ \$dirsize -gt 500000 ]; then"; echo " nginx_dissite $KEYSERVER_DOMAIN_NAME"; echo ' systemctl stop sks'; echo ' systemctl disable sks'; - echo " echo \"$keyserver_disabled_warning\" | mail -s \"$keyserver_mail_subject_line_disabled\" $ADMIN_EMAIL_ADDRESS"; + echo " ${PROJECT_NAME}-notification -m \"$keyserver_disabled_warning\" -s \"$keyserver_mail_subject_line_disabled\""; echo ' fi'; echo 'fi'; } > $keyserver_watchdog_script chmod +x $keyserver_watchdog_script diff --git a/src/freedombone-backup-remote b/src/freedombone-backup-remote index 7997990cb519dfc27180b19bee767c8014abf030..ea94b455ea768a6fc20be42cf83f3184eebf8989 100755 --- a/src/freedombone-backup-remote +++ b/src/freedombone-backup-remote @@ -333,7 +333,7 @@ function backup_mariadb { echo $"Unable to backup MariaDB settings" rm -rf $temp_backup_dir # Send a warning email - echo $"Unable to export database settings" | mail -s "${PROJECT_NAME} backup to friends" "$ADMIN_EMAIL_ADDRESS" + ${PROJECT_NAME}-notification -m $"Unable to export database settings" -s $"${PROJECT_NAME} backup to friends" exit 653 fi echo "$DATABASE_PASSWORD" > $temp_backup_dir/db @@ -410,12 +410,10 @@ function disperse_key_shares { # shellcheck disable=SC2181 if [ ! "$?" = "0" ]; then # Send a warning email - echo "Key share to $REMOTE_SERVER failed" | \ - mail -s "${PROJECT_NAME} social key management" "$MY_EMAIL_ADDRESS" + ${PROJECT_NAME}-notification -m $"Key share to $REMOTE_SERVER failed" -s $"${PROJECT_NAME} social key management" else # Send a confirmation email - echo "Key ${share_filename} shared to $REMOTE_SERVER" | \ - mail -s "${PROJECT_NAME} social key management" "$MY_EMAIL_ADDRESS" + ${PROJECT_NAME}-notification -m $"Key ${share_filename} shared to $REMOTE_SERVER" -s $"${PROJECT_NAME} social key management" fi # remove the temp file/directory @@ -480,7 +478,7 @@ do if ! rsync -ratlzv --rsh="/usr/bin/sshpass -p \"$REMOTE_PASSWORD\" ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" "$SERVER_DIRECTORY/backup" "$REMOTE_SERVER"; then echo "$NOW Backup to $REMOTE_SERVER failed" >> /var/log/remotebackups.log # Send a warning email - echo "Backup to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} backup to friends" "$ADMIN_EMAIL_ADDRESS" + ${PROJECT_NAME}-notification -m $"Backup to $REMOTE_SERVER failed" -s $"${PROJECT_NAME} backup to friends" else echo "$NOW Backed up to $REMOTE_SERVER" >> /var/log/remotebackups.log fi diff --git a/src/freedombone-notification b/src/freedombone-notification new file mode 100755 index 0000000000000000000000000000000000000000..e30b279482a5a43b51a4b32508dbf29213c8d9ae --- /dev/null +++ b/src/freedombone-notification @@ -0,0 +1,81 @@ +#!/bin/bash +# _____ _ _ +# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___ +# | __| _| -_| -_| . | . | | . | . | | -_| +# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___| +# +# Freedom in the Cloud +# +# Sends a notification to the administrator user +# +# License +# ======= +# +# Copyright (C) 2018 Bob Mottram <bob@freedombone.net> +# +# 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' + +ADMIN_USERNAME= +SUBJECT= +MESSAGE= + +while [ $# -gt 1 ] +do + key="$1" + + case $key in + -e|--email) + shift + ADMIN_EMAIL_ADDRESS="$1" + ;; + -u|--user|--username) + shift + ADMIN_USERNAME="$1" + ;; + -s|--subject|--header) + shift + SUBJECT="$1" + ;; + -m|--message|--msg) + shift + MESSAGE="$1" + ;; + *) + # unknown option + ;; + esac + shift +done + +if [ ! "$MESSAGE" ]; then + exit 0 +fi + +if [ ! "$COMPLETION_FILE" ]; then + COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt +fi + +if [ ! "$ADMIN_USERNAME" ]; then + ADMIN_USERNAME=$(grep "Admin user" "$COMPLETION_FILE" | awk -F ':' '{print $2}') +fi +ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME} + +# send email to administrator +if [ "$SUBJECT" ]; then + echo "$MESSAGE" | mail -s "$SUBJECT" "$ADMIN_EMAIL_ADDRESS" +fi + +exit 0 diff --git a/src/freedombone-repair-database b/src/freedombone-repair-database index 615d3341db93175d1c8d2fdddf7f8d6e3f815157..e4a918800e7ba036172e652b286c648fa5daabf1 100755 --- a/src/freedombone-repair-database +++ b/src/freedombone-repair-database @@ -85,7 +85,7 @@ if grep -q "Error" "$TEMPFILE"; then mysql -u root --password="$MYSQL_ROOT_PASSWORD" "$DATABASE" -o < "/var/backups/${DATABASE}_${BACKUP_TYPE}.sql" # Send a warning email - echo $"$DATABASE database corruption could not be repaired. Restored from backup." | mail -s $"${PROJECT_NAME} database maintenance" "$ADMIN_EMAIL_ADDRESS" + ${PROJECT_NAME}-notification -m $"$DATABASE database corruption could not be repaired. Restored from backup." -s $"${PROJECT_NAME} database maintenance" rm -f "$TEMPFILE" exit 1 diff --git a/src/freedombone-restore-remote b/src/freedombone-restore-remote index 492b8e3b455438f83c5a1a5081ec8db2cd5852a2..6bf994bc6d6f5d29733086ded471ecbae25c081e 100755 --- a/src/freedombone-restore-remote +++ b/src/freedombone-restore-remote @@ -85,7 +85,7 @@ echo "$NOW Starting restore from $REMOTE_SERVER" >> /var/log/remotebackups.log if ! rsync -ratlzv --rsh="/usr/bin/sshpass -p $REMOTE_PASSWORD ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" "$REMOTE_SERVER/backup" "$SERVER_DIRECTORY"; then echo "$NOW Restore from $REMOTE_SERVER failed" >> /var/log/remotebackups.log # Send a warning email - echo "Restore from $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} restore from friend" "$ADMIN_EMAIL_ADDRESS" + ${PROJECT_NAME}-notification -m $"Restore from $REMOTE_SERVER failed" -s $"${PROJECT_NAME} restore from friend" exit 790 else echo "$NOW Restored encrypted data from $REMOTE_SERVER" >> /var/log/remotebackups.log diff --git a/src/freedombone-usb-canary b/src/freedombone-usb-canary index 88cdd9dbd86ef29887b50a8478c09c999b7f0e84..ba82e2495bfc7170b4197fdb6db96fc613cb996e 100755 --- a/src/freedombone-usb-canary +++ b/src/freedombone-usb-canary @@ -42,7 +42,7 @@ if [ ! -f /tmp/.upgrading ]; then rm /tmp/.curr_usb_devices fi - echo -e "USB device connected on ${DEVPATH}\\n\\n${devices_changed}" | mail -s "${PROJECT_NAME} USB canary" "${MY_EMAIL_ADDRESS}" + ${PROJECT_NAME}-notification -m $"USB device connected on ${DEVPATH}\\n\\n${devices_changed}" -s $"${PROJECT_NAME} USB canary" echo "${ACTION}" > /tmp/usb-canary echo "${MY_EMAIL_ADDRESS}" >> /tmp/usb-canary date >> /tmp/usb-canary diff --git a/src/freedombone-utils-backup b/src/freedombone-utils-backup index ed5c7ddb10f2ba8f6ba0ba776aee2742b74e6692..9754f41b84dc6ff0f84e82766251a9e31297d0fd 100755 --- a/src/freedombone-utils-backup +++ b/src/freedombone-utils-backup @@ -503,7 +503,7 @@ function backup_database_remote { echo $"${1} database could not be saved" rm -rf "${local_database_dir}" # Send a warning email - echo $"Unable to export ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" "$ADMIN_EMAIL_ADDRESS" + ${PROJECT_NAME}-notification -m $"Unable to export ${1} database" -s $"${PROJECT_NAME} backup to friends" function_check restart_site restart_site exit 57386728 @@ -512,7 +512,7 @@ function backup_database_remote { echo $"${1} database could not be dumped" rm -rf "${local_database_dir}" # Send a warning email - echo $"Unable to dump ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" "$ADMIN_EMAIL_ADDRESS" + "${PROJECT_NAME}-notification" -m $"Unable to dump ${1} database" -s $"${PROJECT_NAME} backup to friends" function_check restart_site restart_site exit 36874289 diff --git a/src/freedombone-utils-cron b/src/freedombone-utils-cron index b1247c9384c631e5b927c13ca470370652d8b1cd..a588354b204b523920015bb51cc17ff4588da47a 100755 --- a/src/freedombone-utils-cron +++ b/src/freedombone-utils-cron @@ -83,7 +83,7 @@ function schedule_stig_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 " ${PROJECT_NAME}-notification -m \"\$(cat /tmp/daily-stig-tests)\" -s \"${PROJECT_NAME} STIG test failures\""; echo 'fi'; echo 'if [ -f /tmp/daily-stig-tests ]; then'; echo ' rm /tmp/daily-stig-tests'; diff --git a/src/freedombone-utils-database b/src/freedombone-utils-database index 1f0df97f32f0e43f322bbd748d04afa7cb138a1f..7610d82be62c4d014e56aeaf94366985d2f69cf8 100755 --- a/src/freedombone-utils-database +++ b/src/freedombone-utils-database @@ -85,10 +85,10 @@ function backup_database_local { echo " mysql -u root --password=\"\$MYSQL_PASSWORD\" ${database_name} -o < \$DAILYFILE"; echo ''; echo ' # Send a warning email'; - echo " echo \"Unable to create a backup of the ${database_name} database. Attempted to restore from yesterdays backup\" | mail -s \"${database_name} backup\" \$EMAIL"; + echo " ${PROJECT_NAME}-notification -m \"Unable to create a backup of the ${database_name} database. Attempted to restore from yesterdays backup\" -s \"${database_name} backup\""; echo ' else'; echo ' # Send a warning email'; - echo " echo \"Unable to create a backup of the ${database_name} database.\" | mail -s \"${database_name} backup\" \$EMAIL"; + echo " ${PROJECT_NAME}-notification -m \"Unable to create a backup of the ${database_name} database.\" -s \"${database_name} backup\""; echo ' fi'; echo 'else'; echo " chmod 600 \$TEMPFILE"; diff --git a/src/freedombone-utils-gnusocialtools b/src/freedombone-utils-gnusocialtools index 4ff1af2005e11133a270fb0afa155824cf9c3d2f..288b0cfa3687e16645e2ae72539273d17a1435da 100755 --- a/src/freedombone-utils-gnusocialtools +++ b/src/freedombone-utils-gnusocialtools @@ -468,8 +468,8 @@ function gnusocial_hourly_script { echo " if [ \"\$UPTIME\" -gt 500 ]; then"; echo " ADMIN_USER=\$(cat $COMPLETION_FILE | grep 'Admin user' | awk -F ':' '{print \$2}')"; echo " MY_EMAIL_ADDRESS=\$ADMIN_USER@$HOSTNAME"; - echo -n " echo \"Restarting ${gnusocial_type} daemons\" | mail -s \"${gnusocial_type} "; - echo "daemons not found\" \$MY_EMAIL_ADDRESS"; + echo -n " ${PROJECT_NAME}-notification -m \"Restarting ${gnusocial_type} daemons\" -s \"${gnusocial_type} "; + echo "daemons not found\""; echo ' fi'; echo ' su -c "sh scripts/startdaemons.sh" -s /bin/sh www-data'; echo 'fi'; diff --git a/src/freedombone-utils-onion b/src/freedombone-utils-onion index c56d0238ed93bd8099dd9519217c80e608aa1dfd..95d228b80ffd7db78cd5310d2de6347f49e87642 100755 --- a/src/freedombone-utils-onion +++ b/src/freedombone-utils-onion @@ -316,17 +316,17 @@ function check_tor_health { echo "if [[ \"\$status\" == 'G'* ]]; then"; echo ' if [ -f /tmp/.torfailed ]; then'; echo ' rm /tmp/.torfailed'; - echo " tail -n 3 /var/log/tor/notices.log | mail -s \"[${PROJECT_NAME}] Tor status is now \$status\" \$ADMIN_USER@\$HOSTNAME"; + echo " ${PROJECT_NAME}-notification -m \"\$(tail -n 3 /var/log/tor/notices.log)\" -s \"[${PROJECT_NAME}] Tor status is now \$status\""; echo ' fi'; echo ' exit 0'; echo 'fi'; echo 'if [ ! -f /tmp/.torfailed ]; then'; - echo " tail -n 3 /var/log/tor/notices.log | mail -s \"[${PROJECT_NAME}] Tor status is \$status\" \$ADMIN_USER@\$HOSTNAME"; + echo " ${PROJECT_NAME}-notification -m \"\$(tail -n 3 /var/log/tor/notices.log)\" -s \"[${PROJECT_NAME}] Tor status is \$status\""; echo " echo \"\$status\" > /tmp/.torfailed"; echo 'else'; echo " prev_status=\$(cat /tmp/.torfailed)"; echo " if [[ \"\$prev_status\" != \"\$status\" ]]; then"; - echo " tail -n 3 /var/log/tor/notices.log | mail -s \"[${PROJECT_NAME}] Tor status is \$status\" \$ADMIN_USER@\$HOSTNAME"; + echo " ${PROJECT_NAME}-notification -m \"\$(tail -n 3 /var/log/tor/notices.log)\" -s \"[${PROJECT_NAME}] Tor status is \$status\""; echo " echo \"\$status\" > /tmp/.torfailed"; echo ' fi'; echo 'fi'; } > /usr/bin/check_tor_health diff --git a/src/freedombone-utils-web b/src/freedombone-utils-web index b188296557daa8a0f573fb4c4941f760fbf3c923..cc7188a2d2dc20694fd0d17f05f126f5e2b58a85 100755 --- a/src/freedombone-utils-web +++ b/src/freedombone-utils-web @@ -360,8 +360,7 @@ function letsencrypt_renewals { echo " echo \"${renewal_failure_msg}\" > ~/temp_renewletsencrypt.txt"; echo ' echo "" >> ~/temp_renewletsencrypt.txt'; echo " \${PROJECT_NAME}-renew-cert -h \$LETSENCRYPT_DOMAIN -p letsencrypt 2>> ~/temp_renewletsencrypt.txt"; - echo -n " cat ~/temp_renewletsencrypt.txt | mail -s \"${renewal_email_title}\" "; - echo "\$ADMIN_EMAIL_ADDRESS"; + echo " \${PROJECT_NAME}-notification -m \"\$(cat ~/temp_renewletsencrypt.txt)\" -s \"${renewal_email_title}\""; echo ' rm ~/temp_renewletsencrypt.txt'; echo ' if [ ! -f ~/letsencrypt_failed ]; then'; echo ' touch ~/letsencrypt_failed'; @@ -427,8 +426,7 @@ function letsencrypt_renewals { echo " echo \"${renewal_failure_msg}\" > ~/temp_renewletsencrypt.txt"; echo ' echo "" >> ~/temp_renewletsencrypt.txt'; echo " \${PROJECT_NAME}-renew-cert -h \$LETSENCRYPT_DOMAIN -p letsencrypt 2>> ~/temp_renewletsencrypt.txt"; - echo -n " cat ~/temp_renewletsencrypt.txt | mail -s \"${renewal_email_title}\" "; - echo "\$ADMIN_EMAIL_ADDRESS"; + echo " \${PROJECT_NAME}-notification -m \"\$(cat ~/temp_renewletsencrypt.txt)\" -s \"${renewal_email_title}\""; echo ' rm ~/temp_renewletsencrypt.txt'; echo ' if [ ! -f ~/letsencrypt_failed ]; then'; echo ' touch ~/letsencrypt_failed';