Skip to content

Instantly share code, notes, and snippets.

@kanazux
Last active August 29, 2015 14:22
Show Gist options
  • Save kanazux/2542869bb1326cda55a7 to your computer and use it in GitHub Desktop.
Save kanazux/2542869bb1326cda55a7 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Bruno Antunes
# Silvio Giunge - <[email protected]>
#
if [ "$1" = "" -o "$1" = "-h" -o "$1" = "--help" ] || [ "$1" = "edit" -a "$2" = "" ]; then
echo ""
echo "Usage: sh block-domains.sh <list|edit> <username>"
echo "sh block-domains.sh <list> | List all users."
echo "sh block-domains.sh <edit> <all> | Edit all users."
echo "sh block-domains.sh <edit> <username> | Edit list for only one user."
echo "sh block-domains.sh <remove> <username> | Remove user from the blocked list."
echo ""
exit
fi
USERS='/tmp/users-list.txt'
NUM_USERS=`wc -l $USERS`
USERS_RESTRICTED='/tmp/users-restricted.txt'
OLD_USERS_RESTRICTED='/tmp/old-users-restricted.txt'
MAIN_DOMAINS='/tmp/main-domains.txt'
SENDER_RESTRICTED='/tmp/sender-restricted.txt'
INTERNAL_DOMAINS='/tmp/internal-domains.txt'
POSTFIX='/tmp/main.cf'
# Limpa arquivo antigos
[ -f $MAIN_DOMAINS ] && rm $MAIN_DOMAINS
[ -f $USERS_RESTRICTED ] && rm $USERS_RESTRICTED
[ -f $OLD_USERS_RESTRICTED ] && rm $OLD_USERS_RESTRICTED
[ -f $SENDER_RESTRICTED ] && rm $SENDER_RESTRICTED
[ -f $INTERNAL_DOMAINS ] && rm $INTERNAL_DOMAINS
[ -f $POSTFIX ] && rm $POSTFIX
[ -f /tmp/bpant.sdb ] && rm /tmp/bpant.sdb
if [ "$1" = "list" ]; then
/usr/bin/zarafa-admin -l | awk '{print $1}' | sed '/^User$\|^username$\|---------------------------------------------\|SYSTEM\|^$/d' | sort
exit
fi
APPLY () {
/bin/cp /var/db/bpant.sdb /tmp/bpant.sdb
/bin/sh get-main-domain.sh > $MAIN_DOMAINS
# Gera arqivo dos domínios internos
for DOMAIN in `cat $MAIN_DOMAINS`; do
echo "$DOMAIN OK" >> $INTERNAL_DOMAINS
done
# Gera arquivos dos usuários restritos dos domínios internos
if [ `cat $USERS_RESTRICTED | wc -l` -gt 0 ]; then
for USER in `cat $USERS_RESTRICTED`; do
for DOMAIN in `cat $MAIN_DOMAINS`; do
echo $USER@$DOMAIN" restricted-group" >> $SENDER_RESTRICTED
done
done
fi
if [ `cat /etc/postfix/main.cf | grep -c 'Custom Block Domains'` -eq 0 ]; then
# Criando main.cf se necessario
sed '/^\#\{24\}$/,/reject$/d' /etc/postfix/main.cf > $POSTFIX
echo "Criando main.cf se necessario."
echo "########################" >> $POSTFIX
echo "# Custom Block Domains #" >> $POSTFIX
echo "########################" >> $POSTFIX
echo "smtpd_recipient_restrictions = check_sender_access hash:/etc/postfix/sender-restricted, permit_mynetworks, check_relay_domains" >> $POSTFIX
echo "smtpd_restriction_classes = restricted-group" >> $POSTFIX
echo "restricted-group = check_recipient_access hash:/etc/postfix/restricted-group, reject" >> $POSTFIX
cat $POSTFIX >> /etc/postfix/main.cf
fi
# Move todos os arquivos mantendo as permissões
echo "Movendo arquivos postfix ..."
cat $SENDER_RESTRICTED > /etc/postfix/sender-restricted
cat $INTERNAL_DOMAINS > /etc/postfix/restricted-group
# Carrega arquivos das restricoes
echo "Aplicando modificacoes ..."
postmap /etc/postfix/sender-restricted
postmap /etc/postfix/restricted-group
# Recarrega o Postfix
/etc/init.d/postfix reload
echo "Configuracoes aplicadas com sucesso."
}
if [ "$1" = "remove" ]; then
[ -f /etc/postfix/sender-restricted ] && `cat /etc/postfix/sender-restricted | tr '@' ' ' | awk '{print $1}' >> $OLD_USERS_RESTRICTED`
for i in `cat $OLD_USERS_RESTRICTED`; do
if [ "$i" != "$2" ]; then
echo -e "$i" >> $USERS_RESTRICTED
else
echo 'Usuario '$i' Removido'
fi
done
APPLY
fi
if [ "$1" = "edit" ]; then
/usr/bin/zarafa-admin -l | awk '{print $1}' | sed '/^User$\|^username$\|---------------------------------------------\|SYSTEM\|^$/d' | sort > /tmp/users-list.txt
# Lista os usuarios e printa na tela.
if [ "$2" != "all" ]; then
[ -f /etc/postfix/sender-restricted ] && `cat /etc/postfix/sender-restricted | tr '@' ' ' | awk '{print $1}' >> $USERS_RESTRICTED`
for i in `cat $USERS`; do
if [ "$2" = "$i" ]; then
echo -ne 'Bloquear envio externo para: '$i '(S/N): '
read BLOQUEIA
if [ "$BLOQUEIA" = "s" -o "$BLOQUEIA" = "S" ]; then
echo $i 'Bloqueado'
[ `cat $USERS_RESTRICTED | grep -c $i` -eq 0 ] && echo -e "$i" >> $USERS_RESTRICTED
else
echo $i 'Liberado'
fi
echo ""
fi
done
else
[ -f /etc/postfix/sender-restricted ] && /bin/rm /etc/postfix/sender-restricted
[ -f /etc/postfix/sender-restricted.db ] && /bin/rm /etc/postfix/sender-restricted.db
for i in `cat $USERS`; do
echo -ne 'Bloquear envio externo para: '$i '(S/N): '
read BLOQUEIA
if [ "$BLOQUEIA" = "s" -o "$BLOQUEIA" = "S" ]; then
echo $i 'Bloqueado'
echo -e "$i" >> $USERS_RESTRICTED
else
echo $i 'Liberado'
fi
echo ""
done
fi
APPLY
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment