#!/bin/sh

#*
#* Copyright (C) 2004 Perolo Silantico <per.sil@gmx.it> and
#*                    Pawel Foremski <pjf@gna.org>, et al.
#*
#* This program is free software; you can redistribute it and/or
#* modify it under the terms of the GNU General Public License
#* as published by the Free Software Foundation; either
#* version 2 of the License, or (at your option) any later
#* version.
#*
#* This program is distributed in the hope that it will be useful,
#* but WITHOUT ANY WARRANTY; without even the implied warranty of
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#* GNU General Public License for more details.
#*
#* You should have received a copy of the GNU General Public License
#* along with this program; if not, write to the Free Software Foundation,
#* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#*
#***
#*
#* (C)opyRight 2007 Werner Maier <werner@maiers.de>
#*
#* $Id: local_check_recipient.sh,v 1.3 2007/03/22 07:29:55 werner Exp $
#*
#* extensions:
#* - check for .qmail-default in $HOMEPATH (catchall). 
#*   I know: catchall is very stupid, but possible.
#* - check for .qmail-ezmlmlist-command for use with ezmlm
#*   e.g. newsletter-subscribe@domain.net
#* - check for .qmail-extensions, e.g. $HOMEPATH/$USER/.qmail-ext
#*   should not be possible with qmail-admin, but who knows...
#* - checks if the needed tools are executable and suid.
#* - changes to lower letters and . to : to allow Surname.NAME@domain
#*   with .qmail-surname:name@domain
#*
#* this version is based on the vpopmail_check_recipient.sh, 
#* but just checks local users, for qmail-setups without vpopmail.
#*   
#* Known shortcomings: only the shortest "-ext" will be used.
#* USER-list-bla will not be checked. 
#* 

#* must be configured to check your domain(s) in the 
#* case domain in ... structure below.

# checks an email address provided as parameter as local user or 
# if called as a qmail-spp plugin using ${SMTPRCPTTO}
# returns
#   0   ... in case the email exists
#   1   ... in case this email does not exists

VERSION=2.0

# the recipient to check is the only parameter on the command line - if any present
RECIPIENT="$1"

## --- LOG settings
# trigger log messages
LOG_USE=1
LOG_PREFIX="qmail-spp (`basename $0`) [$$]: "


## --- qmail-spp settings
MSG_ERROR="E511 Sorry, no mailbox here by that name (#5.1.1)"
MSG_OK=""


# if this is a qmail-spp plugin. recipient is passed via environement
if [ ! -z "${SMTPRCPTTO}" ]; then
  RECIPIENT="${SMTPRCPTTO}"
  SPP=1
else
  SPP=0
fi


# if the recipient is empty it does not exist :)
if [ -z "${RECIPIENT}" ]; then
  [ "${LOG_USE}" == "1" ] && echo "${LOG_PREFIX}invalid empty recipient" 1>&2
  [ "${SPP}" == "1" ] && echo "${MSG_ERROR}"
  exit 1 
fi

# change everything to lower letters:
RECIPIENT=`echo $RECIPIENT | /usr/bin/tr [:upper:] [:lower:]`

# get the domain name of the recipient
# ${var##pattern} remove longest pattern from left
DOMAIN=${RECIPIENT##*@}

# get the user-name
# ${var%%pattern} remove longest pattern from right
BOX=${RECIPIENT%%@*}

# we go to .qmail file level now.
# so change "." to ":" as, .qmail-surname:name is allowed
BOX=`echo $BOX | tr . :`

# check if the box name contains invalid characters
if [ ! -z "`/bin/echo ${RECIPIENT} | /bin/grep '[^-=\+0-9A-Za-z\.@_]'`" ]; then
  [ "${LOG_USE}" == "1" ] && echo "${LOG_PREFIX}invalid characters in recipient name: ${RECIPIENT}" 1>&2
  [ "${SPP}" == "1" ] && echo "${MSG_ERROR}"
  exit 1
fi

# if RELAYCLIENT is set, then the user has authenticated before
# relaying is allowed
if [ "`set | /bin/grep RELAYCLIENT > /dev/null; echo $?`" == "0" -o ! -z "${SMTPAUTHUSER}" ]; then
[ "${LOG_USE}" == "1" ] && echo "${LOG_PREFIX}relaying email to: ${RECIPIENT}"  1>&2
[ "${SPP}" == "1" ] && echo "${MSG_OK}"
exit 0
fi

# this check only for some domains.
# put your domain(s) in here.

EXIT=1;
case $DOMAIN in 
    fidion.de)
        EXIT=0
        ;;
    fidion.net)
        EXIT=0
        ;;
    fidion.com)
        EXIT=0
        ;;
esac

if [ "$EXIT" == "1" ]
then 
    [ "${LOG_USE}" == "1" ] && echo "${LOG_PREFIX} not checkable domain ${RECIPIENT}" 1>&2
    exit
fi

# if a .qmail-${BOX} file exists, then delivery is possible
if [ -d /home/${BOX} ]; then
  [ "${SPP}" == "1" ] && echo "${MSG_OK}"
  [ "${LOG_USE}" == "1" ] && echo "${LOG_PREFIX} ${RECIPIENT} exists (/home/${BOX})." 1>&2
  exit 0
fi

# if a /var/qmail/alias/.qmail-box exists, then delivery is possible
if [ -f /var/qmail/alias/.qmail-${BOX} ]
then
    [ "${SPP}" == "1" ] && echo "${MSG_OK}"
    [ "${LOG_USE}" == "1" ] && echo "${LOG_PREFIX} ${RECIPIENT} exists (/var/qmail/alias/.qmail-${BOX})." 1>&2
exit 0
fi

# ${var%pattern} remove shortest pattern from right
LIST=${BOX%-*}
if [ "$LIST" != "$BOX" ]
then
    # if a .qmail-${BOX} file exists, then delivery may possible
    if [ -d /home/${LIST} ]; then
      [ "${SPP}" == "1" ] && echo "${MSG_OK}"
      [ "${LOG_USE}" == "1" ] && echo "${LOG_PREFIX} ${RECIPIENT} could exist (/home/${LIST})." 1>&2
      exit 0
    fi

    # if a /var/qmail/alias/.qmail-box exists, then delivery may possible
    if [ -f /var/qmail/alias/.qmail-${LIST} ]
    then
        [ "${SPP}" == "1" ] && echo "${MSG_OK}"
        [ "${LOG_USE}" == "1" ] && echo "${LOG_PREFIX} ${RECIPIENT} could exist (/var/qmail/alias/.qmail-${LIST})." 1>&2
    exit 0
  fi
fi

# no other checks have prooven the existence of this email address
[ "${LOG_USE}" == "1" ] && echo "${LOG_PREFIX}no such recipient: ${RECIPIENT}" 1>&2
[ "${SPP}" == "1" ] && echo "${MSG_ERROR}"
exit 1
