3cx Three Charlie X-ray [?]

Transient Email Address (TEA)

January 3, 2017 17:40 UTC

This is from a README.md file that was last updated on 2016-07-02.

WHAT IS IT

This is a simple set of scripts that allow for the creation of temporary email addresses that will expire after a certain amount of time. These addresses can be used for things like signing up to website one isn't sure about or to test certain products using an email address that will go away (forcing rejects back to the sender).

Initially, this will simply generate email addresses in the following format.

<word>.<random hex string>@<domain>

For example.

chard.6d06@3cx.org

These email addresses will be added as aliases to the system's /etc/aliases file. Each transient address and its expiration date will be tracked via a mechanism outside of the /etc/aliases file.

Installation instructions and the scripts can be found after the break.


INSTALLATION INSTRUCTIONS

Simply put the add_tea and remove_tea somewhere accessible to the root user. /root/bin is a good location. Ensure the scripts are executable by the root user. You also need to decide where the TEA_DB will be kept. You could keep it in /etc where the aliases file is.

You can then run add_tea whenever you need a temporary/transient junk email address. To ensure they get cleaned up automatically, you need to have the remove_tea script run periodically from cron (once a day seems to make sense).

That is all.

add_tea

#!/usr/bin/env bash
#
# author: thalios@3cx.org
# version: 1.1
#
# description: Adds "junk" (transient) email aliases to /etc/aliases that
# forward mail to the specified "real" email address. This transient addresses
# are tracked in a separate sqlite DB with an expiration date.
# The user is prompted (with defaults) upon creation.
# There are no runtime arguments for this script. Options are interactive.

# Config vars
DEFAULT_DESTADDR="your_user_or_address"
DEFAULT_EXPIREDAYS=30
DOMAIN="yourdomain.com"
TEA_DB="/etc/tea.db"
WORKING_DIR="/etc" # Update this to the production working directory (e.g. /etc)

# Some helpful things
bold=$(tput bold)
normal=$(tput sgr0)

# Functions

function getuserinfo {

        echo "The new transient email address will be ${bold}$junkaddr${normal}."
        echo
        echo "Enter the destination username or email address. [default: ${bold}$DEFAULT_DESTADDR${normal}]"
        echo -n "> "; read destaddr
        echo
        echo "Enter the expiration time in days. [default: ${bold}$DEFAULT_EXPIREDAYS days${normal}]"
        echo -n "> "; read expiredays
        echo

        [ "$destaddr" = "" ] && destaddr=$DEFAULT_DESTADDR
        [ "$expiredays" = "" ] && expiredays=$DEFAULT_EXPIREDAYS
}

function dolog {
        logline="$1"
        logger -t "transient_email" "$logline" || >&2 echo "Error writing log line: $1"
}

function error_exit {
        errortext="$1"
        exitcode="$2"
        [ "${exitcode}xxx" = "xxx" ] || exitcode=1
        >&2 echo "Error: $errortext" && dolog "Error: $errortext"
        exit $exitcode
}

# MAIN PROGGY

lastdir="$PWD"
cd "$WORKING_DIR"

rprefix=$(egrep -i "^[^1234567890']{5}$" /usr/share/dict/words | egrep -v "\.|\-" | tr '[:upper:]' '[:lower:]' | egrep "[aeiouy]" | shuf | head -n 1)
rsuffix=$(date +%s%N | sha256sum | head -c 4)
newjunk="${rprefix}.${rsuffix}"
junkaddr="$newjunk@$DOMAIN"

# Declares var yn and forces to lower case.
declare -l yn
while [ "$yn" != "y" ]; do
        getuserinfo

        echo "The transient email address ${bold}$junkaddr${normal}"
        echo "will forward mail to the user/address ${bold}$destaddr${normal}."
        echo "This will expire in ${bold}$expiredays${normal} days."
        echo
        echo -n "Is this correct? (${bold}Y${normal}/n) "
        read yn
        [ "$yn" = "" ] && yn="y"
        echo
done

expirestamp=$(date -d "now + $expiredays days" +%s)

# Check out aliases. !!FIX - Need to implement sanity checking like that in
# the vir script.
co -l aliases > /dev/null 2>&1 || error_exit "Unable to check out aliases file."

echo -e "$newjunk:	$destaddr" >> aliases || error_exit "Unable to update aliases file."

ci -m"Added new transient alias $newjunk" -u aliases > /dev/null 2>&1 || error_exit "Unable to check in aliases file."

#newaliases || error_exit "newaliases command filed."   # need to uncomment for production

echo "$newjunk,$destaddr,$expirestamp" >> $TEA_DB
if [ $? -eq 0 ]; then
        dolog "Added $newjunk email pointing to $destaddr. Expires: $expirestamp"
else
        error_exit "Could not add new tea $newjunk to $TEA_DB"
fi

cd "$lastdir"

exit 0

remove_tea

#!/usr/bin/env bash
#
# author: thalios@3cx.org
# version: 1.1
#
# description:

TEA_DB="/etc/tea.db"
WORKING_DIR="/etc" # Update this to the production working directory (e.g. /etc)

function dolog {
        logline="$1"
        logger -t "transient_email" "$logline" || >&2 echo "Error writing log line: $1"
}

function error_exit {
        errortext="$1"
        exitcode="$2"
        [ "${exitcode}xxx" = "xxx" ] || exitcode=1
        >&2 echo "Error: $errortext" && dolog "Error: $errortext"
        exit $exitcode
}

# Main Proggy

lastdir="$PWD"
cd "$WORKING_DIR"

nowstamp=$(date +%s)

IFS=","
while read junkaddr destaddr expirestamp; do
        if [ $expirestamp -le $nowstamp ]; then
                co -l aliases > /dev/null 2>&1 || error_exit "Unable to check out aliases file."
                sed -i "/^$junkaddr:/d" aliases && sed -i "/^$junkaddr/d" "$TEA_DB" ||
                  erro_exit "Some error occurred trying to edit the files."
                dolog "$junkaddr has expired. Successfully removed from aliases."
                ci -m"Added new transient alias $newjunk" -u aliases > /dev/null 2>&1 || error_exit "Unable to check in aliases file."
                #newaliases || error_exit "newaliases command filed."   # need to uncomment for production
        fi

done < $TEA_DB
unset IFS

cd "$lastdir"

exit
Tags: code, tools

CC by-nc-nd 3cx.orgwebmaster@3cx.org
This webpage generated with blog.sh.