From 22fa505a3e0e09993fc27865ee2524f65cac13b4 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 20 Apr 2017 13:43:55 +0100 Subject: [PATCH] Scripts to remove blocked domains from gnusocial/postactiv databases --- src/freedombone-app-gnusocial | 3 ++ src/freedombone-app-postactiv | 4 ++ src/freedombone-utils-database | 11 ++++ src/freedombone-utils-gnusocialtools | 75 ++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) diff --git a/src/freedombone-app-gnusocial b/src/freedombone-app-gnusocial index 5dcf8c89..385e7f3b 100755 --- a/src/freedombone-app-gnusocial +++ b/src/freedombone-app-gnusocial @@ -503,6 +503,8 @@ function remove_gnusocial { sed -i '/gnusocial/d' $COMPLETION_FILE remove_backup_database_local gnusocial + sed -i '/gnusocial-firewall/d' /etc/crontab + function_check remove_ddns_domain remove_ddns_domain $GNUSOCIAL_DOMAIN_NAME } @@ -761,6 +763,7 @@ function install_gnusocial_main { ${PROJECT_NAME}-addemail -u $MY_USERNAME -e "noreply@$GNUSOCIAL_DOMAIN_NAME" -g gnusocial --public no ${PROJECT_NAME}-pass -u $MY_USERNAME -a gnusocial -p "$GNUSOCIAL_ADMIN_PASSWORD" + gnusocial_block_domain_script gnusocial set_completion_param "gnusocial domain" "$GNUSOCIAL_DOMAIN_NAME" diff --git a/src/freedombone-app-postactiv b/src/freedombone-app-postactiv index d2fd1c21..5f3dfc4c 100755 --- a/src/freedombone-app-postactiv +++ b/src/freedombone-app-postactiv @@ -496,6 +496,8 @@ function remove_postactiv { sed -i '/postactiv/d' $COMPLETION_FILE remove_backup_database_local postactiv + sed -i '/postactiv-firewall/d' /etc/crontab + function_check remove_ddns_domain remove_ddns_domain $POSTACTIV_DOMAIN_NAME } @@ -758,6 +760,8 @@ function install_postactiv_main { ${PROJECT_NAME}-pass -u $MY_USERNAME -a postactiv -p "$POSTACTIV_ADMIN_PASSWORD" + gnusocial_block_domain_script postactiv + set_completion_param "postactiv domain" "$POSTACTIV_DOMAIN_NAME" install_completed postactiv_main diff --git a/src/freedombone-utils-database b/src/freedombone-utils-database index c5285dc5..7783090d 100755 --- a/src/freedombone-utils-database +++ b/src/freedombone-utils-database @@ -275,6 +275,17 @@ function run_query { mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name } +function run_query_with_output { + database_name=$1 + database_query=$2 + output=$(mysql -u root --password="$MARIADB_PASSWORD" << EOF +use $database_name; +$database_query +EOF +) + echo "$output" +} + function drop_database { database_name=$1 get_mariadb_password diff --git a/src/freedombone-utils-gnusocialtools b/src/freedombone-utils-gnusocialtools index c4de39ae..fde1b509 100755 --- a/src/freedombone-utils-gnusocialtools +++ b/src/freedombone-utils-gnusocialtools @@ -327,6 +327,81 @@ function gnusocial_hourly_script { chmod +x /etc/cron.hourly/${gnusocial_type}-daemons } +function gnusocial_block_domain_script { + database_name=$1 + + echo '#!/bin/bash' > /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo 'database_name=$1' >> /usr/bin/gnusocial-delete-domain + echo 'remove_domain=$2' >> /usr/bin/gnusocial-delete-domain + echo "MARIADB_PASSWORD=\$(${PROJECT_NAME}-pass -u root -a mariadb)" >> /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo 'if [ ${#remove_domain} -lt 2 ]; then' >> /usr/bin/gnusocial-delete-domain + echo ' echo $"No domain was specified"' >> /usr/bin/gnusocial-delete-domain + echo ' exit 1' >> /usr/bin/gnusocial-delete-domain + echo 'fi' >> /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo 'if [[ "$remove_domain" != *"."* ]]; then' >> /usr/bin/gnusocial-delete-domain + echo ' echo $"This doesnt look like a domain"' >> /usr/bin/gnusocial-delete-domain + echo ' exit 2' >> /usr/bin/gnusocial-delete-domain + echo 'fi' >> /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo 'if [ ${#database_name} -lt 2 ]; then' >> /usr/bin/gnusocial-delete-domain + echo ' echo $"No database was specified"' >> /usr/bin/gnusocial-delete-domain + echo ' exit 3' >> /usr/bin/gnusocial-delete-domain + echo 'fi' >> /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo "database_query=\"select profile.id from profile where profileurl like '%\${remove_domain}%';\"" >> /usr/bin/gnusocial-delete-domain + echo 'declare -a ids=$(mysql -u root --password="$MARIADB_PASSWORD" << EOF' >> /usr/bin/gnusocial-delete-domain + echo 'use $database_name;' >> /usr/bin/gnusocial-delete-domain + echo '$database_query' >> /usr/bin/gnusocial-delete-domain + echo 'EOF' >> /usr/bin/gnusocial-delete-domain + echo ')' >> /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo 'ctr=0' >> /usr/bin/gnusocial-delete-domain + echo 'for id in $ids' >> /usr/bin/gnusocial-delete-domain + echo 'do' >> /usr/bin/gnusocial-delete-domain + echo ' if [ $ctr -gt 0 ]; then' >> /usr/bin/gnusocial-delete-domain + echo ' echo "Removing user ID $id on $remove_domain"' >> /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo " database_query=\"delete from conversation where uri like \"%\${remove_domain}%';\"" >> /usr/bin/gnusocial-delete-domain + echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo ' database_query="delete from reply where profile_id=${id};"' >> /usr/bin/gnusocial-delete-domain + echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo ' database_query="delete from reply where replied_id=${id};"' >> /usr/bin/gnusocial-delete-domain + echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo ' database_query="delete from notice where profile_id=${id};"' >> /usr/bin/gnusocial-delete-domain + echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo ' database_query="delete from profile where id=${id};"' >> /usr/bin/gnusocial-delete-domain + echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain + echo '' >> /usr/bin/gnusocial-delete-domain + echo ' database_query="delete from user where id=${id};"' >> /usr/bin/gnusocial-delete-domain + echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain + echo ' fi' >> /usr/bin/gnusocial-delete-domain + echo ' ctr=$((ctr + 1))' >> /usr/bin/gnusocial-delete-domain + echo 'done' >> /usr/bin/gnusocial-delete-domain + chmod +x /usr/bin/gnusocial-delete-domain + + echo '#!/bin/bash' > /usr/bin/${database_name}-firewall + echo '' >> /usr/bin/${database_name}-firewall + echo "PROJECT_NAME=$PROJECT_NAME" >> /usr/bin/${database_name}-firewall + echo "database_name=$database_name" >> /usr/bin/${database_name}-firewall + echo 'FIREWALL_DOMAINS=$HOME/${PROJECT_NAME}-firewall-domains.cfg' >> /usr/bin/${database_name}-firewall + echo '' >> /usr/bin/${database_name}-firewall + echo 'while read block; do' >> /usr/bin/${database_name}-firewall + echo ' /usr/bin/gnusocial-delete-domain "$database_name" "$block"' >> /usr/bin/${database_name}-firewall + echo 'done <$FIREWALL_DOMAINS' >> /usr/bin/${database_name}-firewall + chmod +x /usr/bin/${database_name}-firewall + + if ! grep -q "${database_name}-firewall" /etc/crontab; then + echo "*/1 * * * * root /usr/bin/${database_name}-firewall" >> /etc/crontab + fi +} + function expire_gnusocial_posts { domain_name=$1 gnusocial_type=$2