Created
October 3, 2015 20:38
-
-
Save bretmartin/5a5b77998d90c06219be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
umask 007 | |
DOMAINS=/etc/mail/domains | |
MAPNAME=virtual | |
MAILFROM=root@example.com | |
MAILTO=admin@example.com | |
LOCKFILE=/var/lock/$( basename $0 ) | |
MAPFILE=/etc/postfix/${MAPNAME} | |
TMPFILE=$( mktemp /etc/postfix/${MAPNAME}.XXXXXXXX ) | |
ERRFILE=$( mktemp ) | |
trap 'rm -f ${TMPFILE} ${ERRFILE} ${LOCKFILE}' 0 1 2 3 15 | |
exec 9> ${LOCKFILE} | |
flock --exclusive 9 | |
exec 1>&- | |
exec 1> ${TMPFILE} | |
exec 2>&- | |
exec 2>> ${ERRFILE} | |
cd ${DOMAINS} | |
for DOMAIN in * | |
do | |
case ${DOMAIN} in | |
\#*|*~) continue ;; | |
esac | |
printf "%-39s %s\n" $DOMAIN - \ | |
| sed --regexp-extended \ | |
--expression='s/ / /g' \ | |
--expression='s/ +/ /g' | |
FILTEREDDOMAIN=$( mktemp /tmp/${DOMAIN}.XXXXXXXX ) | |
grep \ | |
--binary-files=text \ | |
--extended-regexp \ | |
--regexp='^$' \ | |
--regexp='^#' \ | |
--regexp='^[[:space:]]+$' \ | |
--regexp='^[^[:space:]]+@'${DOMAIN}'[[:space:]]+[^[:space:]]+' \ | |
--regexp='^[[:space:]]+[^[:space:]]+' \ | |
${DOMAIN} \ | |
> ${FILTEREDDOMAIN} | |
FILTERDIFF="$( diff --text --unified ${DOMAIN} ${FILTEREDDOMAIN} )" | |
if [ $? = 0 ] | |
then | |
cat ${FILTEREDDOMAIN} | |
echo | |
else | |
REFUSE=yes | |
DIFF="${DIFF} | |
${FILTERDIFF}" | |
fi | |
rm -f ${FILTEREDDOMAIN} | |
done | |
if [ "$REFUSE" = "yes" ] | |
then | |
mail -t <<EOF | |
From: ${MAILFROM} | |
To: ${MAILTO} | |
Subject: ERROR from $( basename $0 ) on $( hostname -f ) | |
Hi. This is the $( basename $0 ) program on $( hostname -f ). | |
Sorry, I refuse to assemble ${MAPFILE} from ${DOMAINS}/ because of the following problems I found:${DIFF} | |
==> standard error (if any) <== | |
$( cat ${ERRFILE} ) | |
EOF | |
exit 1 | |
fi | |
if [ ! -f ${MAPFILE} ] | |
then | |
touch ${MAPFILE} | |
fi | |
DIFF="$( diff -u ${MAPFILE} ${TMPFILE} )" | |
if [ $? = 1 ] && [ -s ${TMPFILE} ] | |
then | |
mv -f ${TMPFILE} ${MAPFILE} | |
chmod 660 ${MAPFILE} | |
chgrp staff ${MAPFILE} | |
postmap ${MAPFILE} | |
chmod 660 ${MAPFILE}.db | |
chgrp postfix ${MAPFILE}.db | |
umask 002 | |
cd /etc/postfix | |
git commit \ | |
--author="$( basename $0 ) <${MAILFROM}>" \ | |
--message="$( basename $0 ) automated change | |
(contents of ${DOMAINS}/ changed)" \ | |
--quiet \ | |
${MAPFILE} | |
mail -t <<EOF | |
From: ${MAILFROM} | |
To: ${MAILTO} | |
Subject: $( basename $0 ) on $( hostname -f ) | |
Hi. This is the $( basename $0 ) program on $( hostname -f ). | |
I just made the following changes based on the contents of ${DOMAINS}/ : | |
${DIFF} | |
==> standard error (if any) <== | |
$( cat ${ERRFILE} ) | |
EOF | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code appears in my blog post User-managed Postfix mail forwarding using inotify.