#!/bin/sh

# Description: Script to remotely add, remove or list qmail alias addresses:

# http://www.netdevice.com/qmail/patch/qtool-06-script

# Run from an OS X machine, assumes that ssh passphraseless

# public key authentication has been set up.

# Needs qaset-06.script running from cron on the mailservers.

# Usage: ~/qtool-06.script

# Author: Eben Pratt, goodrcptto at netdevice dot com

# 2003-09-02 01: Original version.

# 2003-10-04 02: Added support for protected accept client addresses.

# User to domain mapping is contained in the staged alias file.

# 2003-11-07 03: Disable X11 forwarding to suppress notice regarding xauth, when

# X11 forwarding is set to be requested by default on OS X 10.3.

# 2004-01-12 04: The list option correctly displays dotted recipient addresses.

# 2004-05-16 05: This script updates the primary, rsynced to the secondary.

# 2004-09-28 06: Updated to allow off network functionality.

# User specific edit required;

USR='maryjane'

DOM='example.com'

#

MS0='example.com'

MS1='sun01'

MS2='sun02'

LIS='/bin/ls'

RMV='/bin/rm'

SED='/bin/sed'

NUL='/dev/null'

CUT='/bin/cut -d'

TRA='/usr/bin/tr'

EGR='/usr/bin/egrep'

IFC='/usr/sbin/ifconfig hme0'

SSH='/usr/bin/ssh -x -l '${USR}''

QMA='/var/qmail/alias/.qmail-'

QMS='/var/qmail/stage/*/.qmail-'

QMN='/var/qmail/stage/normal'

QMP='/var/qmail/stage/protected'

SHO='/var/qmail/bin/qmail-showctl'

echo ""

if [ `${SSH} ${MS0} "/bin/cat /etc/nodename" 2>${NUL}` = ${MS1} ] ; then

echo "The mail server is running on ${MS1}."

elif [ `${SSH} ${MS0} "/bin/cat /etc/nodename" 2>${NUL}` = ${MS2} ] ; then

echo "The mail server is running on ${MS2}."

else

echo "The mail server is unreachable, please try again."

exit 0

fi

qtool() {

printf "\nOptions are to [a]dd, [r]emove, or [l]ist your alias addresses: "

read RES

if [ "${RES}" = a ] ; then

printf "\nOptions are to add a [n]ormal or [p]rotected alias address: "

read ADD

if [ "${ADD}" = n ] ; then

printf "\nAdd [ ]@"${DOM}": "

read NOR

ALA=`echo ${NOR} | ${TRA} -d '[:space:]'`

if [ `echo ${ALA} | ${EGR} '@'` ] ; then

echo ""

echo "Please only enter the part before the @ character."

echo ""

exit 0

elif [ `echo ${ALA} | ${EGR} '[^a-zA-Z0-9._-]'` ] ; then

echo ""

echo "Please only use letters, numbers, dots, underscores or dashes."

echo ""

exit 0

elif [ `echo ${ALA} | ${EGR} '[a-zA-Z0-9._-]'` ] ; then

ALS=`echo ${ALA} | ${TRA} '[:upper:]' '[:lower:]' | ${TRA} '.' ':'`

if [ `${SSH} ${MS0} "${LIS} ${QMS}* | ${EGR} .qmail-"${ALS}"$" 2>${NUL}` \

] ; then

OWN=`${SSH} ${MS0} "${LIS} -o ${QMS}"${ALS}$" | ${CUT} ' ' -f 5" \

2>${NUL}`

echo ""

echo "That alias already exists, "${OWN}" is using it."

echo ""

exit 0

else

${SSH} ${MS0} "echo \&"${USR}"@"${DOM}" > ${QMN}/.qmail-"${ALS}"" \

2>${NUL}

YES=0

while [ ${YES} -ne 1 ]

do

sleep 10

if [ `${SSH} ${MS0} "${LIS} ${QMA}* | ${EGR} .qmail-"${ALS}"$" \

2>${NUL}` ] ; then

sleep 10

echo ""

${SSH} ${MS0} "${SHO} | ${EGR} ' ${ALA}'@'${DOM}'." 2>${NUL}

echo ""

YES=1

fi

done

fi

else

echo ""

exit 0

fi

elif [ "${ADD}" = p ] ; then

printf "\nPlease remove the normal alias, then add it as a protected one.

Messages will be rejected to this address unless the qmail administrator has

responded to your request to allow the sender's IPs.

Add [ ]@"${DOM}": "

read PRO

ALA=`echo ${PRO} | ${TRA} -d '[:space:]'`

if [ `echo ${ALA} | ${EGR} '@'` ] ; then

echo ""

echo "Please only enter the part before the @ character."

echo ""

exit 0

elif [ `echo ${ALA} | ${EGR} '[^a-zA-Z0-9._-]'` ] ; then

echo ""

echo "Please only use letters, numbers, dots, underscores or dashes."

echo ""

exit 0

elif [ `echo ${ALA} | ${EGR} '[a-zA-Z0-9._-]'` ] ; then

ALS=`echo ${ALA} | ${TRA} '[:upper:]' '[:lower:]' | ${TRA} '.' ':'`

if [ `${SSH} ${MS0} "${LIS} ${QMS}* | ${EGR} .qmail-"${ALS}"$" 2>${NUL} \

` ] ; then

OWN=`${SSH} ${MS0} "${LIS} -o ${QMS}"${ALS}$" | ${CUT} ' ' -f 5" \

2>${NUL}`

echo ""

echo "That alias already exists, "${OWN}" is using it."

echo ""

exit 0

else

${SSH} ${MS0} "echo \&"${USR}"@"${DOM}" > ${QMP}/.qmail-"${ALS}"" \

2>${NUL}

YES=0

while [ ${YES} -ne 1 ]

do

sleep 10

if [ `${SSH} ${MS0} "${LIS} ${QMA}* | ${EGR} .qmail-"${ALS}"$"` \

2>${NUL} ] ; then

sleep 10

echo ""

${SSH} ${MS0} "${SHO} | ${EGR} ' ${ALA}'@'${DOM}'." 2>${NUL}

echo ""

YES=1

fi

done

fi

else

echo ""

exit 0

fi

else

echo ""

exit 0

fi

elif [ "${RES}" = r ] ; then

printf "\nRemove [ ]@"${DOM}": "

read REM

ALR=`echo ${REM} | ${TRA} -d '[:space:]'`

if [ `echo ${ALR} | ${EGR} '@'` ] ; then

echo ""

echo "Please only enter the part before the @ character."

echo ""

exit 0

elif [ `echo ${ALR} | ${EGR} '[^a-zA-Z0-9._-]'` ] ; then

echo ""

echo "Please only use letters, numbers, dots, underscores or dashes."

echo ""

exit 0

elif [ `echo ${ALR} | ${EGR} '[a-zA-Z0-9._-]'` ] ; then

ALS=`echo ${REM} | ${TRA} '[:upper:]' '[:lower:]' | ${TRA} '.' ':'`

if [ `${SSH} ${MS0} "${LIS} ${QMS}* | ${EGR} .qmail-"${ALS}"$" 2>${NUL}` \

] ; then

OWN=`${SSH} ${MS0} "${LIS} -o ${QMS}"${ALS}$" | ${CUT} ' ' -f 5" \

2>${NUL}`

if [ "${OWN}" = "${USR}" ] ; then

${SSH} ${MS0} "${RMV} ${QMS}${ALS}" 2>${NUL}

YES=0

while [ ${YES} -ne 1 ]

do

sleep 10

if [ `${SSH} ${MS0} "${LIS} ${QMA}* | ${EGR} .qmail-"${ALS}"$" \

2>${NUL}` ] ; then

sleep 10

else

echo ""

echo "Alias removed."

echo ""

YES=1

fi

done

else

echo ""

echo "That alias is owned by "${OWN}"."

echo ""

exit 0

fi

else

echo ""

echo "That alias doesn't exist."

echo ""

exit 0

fi

else

echo ""

exit 0

fi

elif [ "${RES}" = l ] ; then

echo ""

echo "Normal:"

for LIN in `${SSH} ${MS0} "${EGR} \&"${USR}"@"${DOM}" ${QMN}/.qmail-* \

| ${CUT}- -f2- | ${SED} 's/:&/@/g' | ${CUT}@ -f1 | ${TRA} ':' '.'" 2>${NUL}`

do

echo "SMTP clients may send to ${LIN}@${DOM}."

done

echo ""

echo "Protected:"

for LIN in `${SSH} ${MS0} "${EGR} \&"${USR}"@"${DOM}" ${QMP}/.qmail-* \

| ${CUT}- -f2- | ${SED} 's/:&/@/g' | ${CUT}@ -f1 | ${TRA} ':' '.'" 2>${NUL}`

do

echo "SMTP accept clients may send to ${LIN}@${DOM}."

done

echo ""

else

echo ""

exit 0

fi

}

qtool