# IP ban

#!/bin/bash
LOGFILE=/var/log/iptables_baned.log
tday=$(date +"%y-%m-%d %H:%M:%S")

function usage () {
    echo "Usage: $0 [-vnh] [-a ban|unban|check] IP[, IPn…]

-a      Action: ban, unban, check
-v      verbose
-n      no logging
-h      Show this help message
"
}

ACTION="ban"

while getopts "vnha:" o; do
    case "${o}" in
        a)  ACTION="$OPTARG"
            ;;
        v)  V=1
            ;;
        n)  NOLOG=1
            ;;
        h)
            usage
            exit 0
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done
shift $((OPTIND-1))

ips=$*

for ip in $ips; do
    if [ "$ACTION" = "ban" ]; then
        [ "$V" = "1" ] && echo "Ban ip : $ip"
        iptables -t mangle -A PREROUTING -i vmbr0 -s $ip -j DROP
        iptables -t mangle -A PREROUTING -i etho -s $ip -j DROP
        [ "$NOLOG" != "1" ] && echo "$ACTION [ $ip ] [ $tday ]" >> $LOGFILE
    elif [ "$ACTION" = "unban" ]; then
        [ "$V" = "1" ] && echo "Unban ip : $ip"
        iptables -t mangle -D PREROUTING -i vmbr0 -s $ip -j DROP
        iptables -t mangle -D PREROUTING -i etho -s $ip -j DROP
        [ "$NOLOG" != "1" ] && echo "$ACTION [ $ip ] [ $tday ]" >> $LOGFILE
    elif [ "$ACTION" = "check" ]; then
        if iptables -t mangle -L | grep $ip >/dev/null; then
            [ "$V" = "1" ] && echo "IP $ip is banned"
	    exit 1
        else
            [ "$V" = "1" ] && echo "IP $ip is not banned"
            exit 0
        fi
    fi
done

# Create MySQL user and database

#!/bin/bash
mysql -u root -e "CREATE DATABASE $1 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;";
mysql -u root -e "CREATE USER $2@localhost IDENTIFIED BY '$3';";
mysql -u root -e "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES ON $1.* TO '$2'@'localhost' IDENTIFIED BY '$3';";

# Service Systemd pour Etherpad

[Unit]
Description=Etherpad-lite, the collaborative editor.
After=syslog.target network.target

[Service]
Type=simple
User=etherpad
Group=etherpad
WorkingDirectory=/opt/etherpad/etherpad-lite
Environment=NODE_ENV=production
ExecStart=/usr/bin/nodejs --experimental-worker /opt/etherpad/etherpad-lite/node_modules/ep_etherpad-lite/node/server.js
# use mysql plus a complete settings.json to avoid Service hold-off time over, scheduling restart.
Restart=always

[Install]
WantedBy=multi-user.target

# Nextcloud reset all users passwords

cd /path/to/Nextcloud
nc_users=$(php occ user:list | awk '{ print $2}' | sed 's/://g')
for i in $nc_users; do 
  OC_PASS=$(apg -M SLNC -n 1 -m 15) 
  php occ user:resetpassword --password-from-env $i;
done