Workaround to prevent spoofing From address by Directadmin Message System

kam821

Verified User
Joined
Aug 1, 2021
Messages
24
Hello everyone.
I decided to post simple script I wrote to workaround one of the weirdest mechanic I've ever seen - sending e-mails with spoofed From header.

Currently Directadmin send notification e-mails to user with From header set to the e-mail address of creator of a given user.
Not only this doesn't work with DMARC at all, but it can also be dangerous for mailserver IP (from the RBL point of view this is spoofing).

After change every mail is send with envelope-from=<[email protected]> and From: (Directadmin's msg_sys variable content or Directadmin Message System) <[email protected]>
You can adjust it for your needs.

Additional bonus - added Auto-Submitted, Precedence and X-Auto-Response-Suppress headers to suppress auto-replying (e.g vacation messages) to Directadmin's auto-generated messages.

1) Put the code in /usr/local/directadmin/scripts/custom/sendmail_pre.sh

Bash:
#!/bin/sh

if [ "${to}" = "" ] || [ "${subject}" = "" ] || [ "${message}" = "" ]; then
        exit 0
fi

EMAIL_USER="diradmin"

if ! id "${EMAIL_USER}" >/dev/null 2>&1; then
        echo "Cannot find user ${EMAIL_USER}. Exiting..."
        exit 0
fi

DIRECTADMIN_BIN="/usr/local/directadmin/directadmin"

EMAIL_FROM_NAME="$(${DIRECTADMIN_BIN} c | grep -m1 'msg_sys=' | cut -d'=' -f2)"
if [ "${EMAIL_FROM_NAME}" = "" ]; then
        EMAIL_FROM_NAME="Directadmin Message System"
fi

EMAIL_FROM_DOMAIN="$(${DIRECTADMIN_BIN} c | grep -m1 'servername=' | cut -d'=' -f2)"
if [ "${EMAIL_FROM_DOMAIN}" = "" ]; then
        EMAIL_FROM_DOMAIN="$(hostname -f)"
fi

EMAIL_FROM_ADDR="${EMAIL_USER}@${EMAIL_FROM_DOMAIN}"

EMAIL_CONTENT_TYPE="$(echo "${full_message}" | grep -m1 'Content-Type:' | cut -d ':' -f2- | xargs)"
if [ "${EMAIL_CONTENT_TYPE}" = "" ]; then
        EMAIL_CONTENT_TYPE="text/plain; charset=utf-8"
fi

EMAIL_MIME_VERSION="$(echo "${full_message}" | grep -m1 'MIME-Version:' | cut -d ':' -f2- | xargs)"
if [ "${EMAIL_MIME_VERSION}" = "" ]; then
        EMAIL_MIME_VERSION="1.0"
fi

su -s /bin/sh ${EMAIL_USER} -c "/usr/sbin/sendmail -t << EOF
From: ${EMAIL_FROM_NAME} <${EMAIL_FROM_ADDR}>
To: ${to}
Subject: ${subject}
Content-Type: ${EMAIL_CONTENT_TYPE}
MIME-Version: ${EMAIL_MIME_VERSION}
Auto-Submitted: auto-generated
Precedence: bulk
X-Auto-Response-Suppress: All
${message}
EOF"

exit 0

2)
chown diradmin:diradmin /usr/local/directadmin/scripts/custom/sendmail_pre.sh
chmod 700 /usr/local/directadmin/scripts/custom/sendmail_pre.sh

PS:
@DirectAdmin Support
Over a month ago hooks system did not detect scripts located in /usr/local/directadmin/scripts/custom/sendmail_pre, it was necessary to put the script directly as custom/sendmail_pre.sh.
Has this problem been reported and has it been fixed?
 
Last edited:
Nice script. The new way of Directadmin is to put this script in hook folder as a standalone script like this:

Code:
/usr/local/directadmin/scripts/custom/sendmail_pre/your_script.sh
 
Nice script. The new way of Directadmin is to put this script in hook folder as a standalone script like this:

Code:
/usr/local/directadmin/scripts/custom/sendmail_pre/your_script.sh
I know, I wrote that in the post-scriptum that script should be located there, but over a month ago hooks system was broken (didn't detect scripts located there) and it was necessary to put script directly as sendmail_pre.sh
I don't know if it is already fixed or not.
 
Last edited:
Back
Top