#!/bin/bash # # vchkuser: qmail-spp plugin to check existance of vpopmail users # and ezmlm mailing lists. # Based on vpopmail_check_recipient.sh 1.6 by Werner Maier # # Copyright 2007 Benedikt Böhm # # 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 3 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, see . VPOPMAILDIR=/var/vpopmail VLS="${VPOPMAILDIR}"/bin/vls VGREP="${VPOPMAILDIR}"/bin/vgrep VALIAS="${VPOPMAILDIR}"/bin/valias VDOMINFO="${VPOPMAILDIR}"/bin/vdominfo VUSERINFO="${VPOPMAILDIR}"/bin/vuserinfo LOG_PREFIX="qmail-spp (`basename $0`) [$$]:" log() { [[ -z ${LOG_PREFIX} ]] && return echo "${LOG_PREFIX}" "$@" } error() { echo "E451 Temporary problem in vchkuser (#4.3.5)" exit 1 } nack() { echo "E511 Sorry, no mailbox here by that name (#5.1.1)" exit 1 } ack() { exit 0 } vls() { "${VLS}" "$@" 2>/dev/null } vgrep() { "${VGREP}" "$@" 2>/dev/null } export PATH="${VPOPMAILDIR}/bin:/bin:/usr/bin" # sanity checks for file in ${VLS} ${VGREP} ${VALIAS} ${VDOMINFO} ${VUSERINFO}; do [[ -x ${file} ]] || error "${file}: not executable" [[ -u ${file} ]] || error "${file}: set-user-id bit not set" [[ -g ${file} ]] || error "${file}: set-group-id bit not set" done # if the recipient is empty it does not exist if [[ -z "${SMTPRCPTTO}" ]]; then log "invalid empty recipient" nack fi # check if the box name contains invalid characters if echo ${SMTPRCPTTO} | grep -q "[^-0-9A-Za-z\.@_=]\+"; then log "invalid characters in recipient name: ${SMTPRCPTTO}" nack fi # get the domain name of the recipient HOST=${SMTPRCPTTO##*@} # get the user-name EXT=${SMTPRCPTTO%%@*} # check if the domain exists HDIR=$(vdominfo -d ${HOST}) if [[ $? -ne 0 ]]; then # if RELAYCLIENT is set, user is allowed to relay if [[ -n ${RELAYCLIENT} || -n ${SMTPAUTHUSER} ]]; then log "relaying email to: ${SMTPRCPTTO}" ack else log "no such domain: ${HOST}" nack fi else ALIAS=$(${VDOMINFO} -n ${HOST} | head -n1) [[ "${HOST}" != "${ALIAS}" ]] && HOST=${ALIAS} SMTPRCPTTO=${EXT}@${HOST} fi # remove alias lines if any HDIR=$(set -- $HDIR; echo $1) # check the existence of the user EDIR=$(${VUSERINFO} -d ${SMTPRCPTTO}) if [[ $? -eq 0 ]]; then log "${SMTPRCPTTO} exists (vuserinfo)." ack fi # check with valias if an alias or an e-mail address of that name exists if ${VALIAS} -s ${HOST} | grep -q -- "-> ${SMTPRCPTTO}$"; then log "${SMTPRCPTTO} exists (valias)." ack fi # if a .qmail-${BOX} file exists, then delivery is possible if [[ -n "$(vls ${HDIR}/.qmail-${EXT})" ]]; then log "${SMTPRCPTTO} exists (.qmail-${EXT})." ack fi # if a .qmail-default file exists, then delivery is possible # catchall is very bad, but possible if [[ -n "$(vls ${HDIR}/.qmail-default)" ]]; then if ! vgrep -E -q 'vdelivermail(.*)(bounce-no-mailbox|delete)' "${HDIR}/.qmail-default"; then log "${SMTPRCPTTO} exists (.qmail-default)." ack fi fi # ezmlm-list? remove things like -1176124408.5270.menbhdjbcdifngjljjgh LIST=${EXT%-*} LIST=${LIST%-accept} LIST=${LIST%-reject} if [[ "${LIST}" != "${BOX}" ]]; then # if a .qmail-${LIST}-default file exists, then delivery is possible (ezmlm: list-subscribe...) if [[ -n "$(vls ${HDIR}/.qmail-${LIST}-default)" ]]; then log "${SMTPRCPTTO} exists (.qmail-${LIST}-default)." ack fi # no ezmlm-list. now check for DIR/.qmail-ext LISTEXT=${EXT##*-} if [[ -n "$(vls ${HDIR}/${LIST}/.qmail-${LISTEXT})" ]]; then log "${SMTPRCPTTO} exists (${LIST}/.qmail-${LISTEXT})." ack fi fi # special: ezmlm-list with listname-subscribe-email-or-more=domain.tld@domain.tld # ${var%pattern} remove shortest pattern from right LIST=${EXT%-subscribe-*=*.*} LIST=${LIST%-unsubscribe-*=*.*} LIST=${LIST%-accept-*=*.*} LIST=${LIST%-allow-tc.*=*.*} LIST=${LIST%-reject-*=*.*} LIST=${LIST%-deny-*=*.*} LIST=${LIST%-sc.*=*.*} LIST=${LIST%-tc.*=*.*} LIST=${LIST%-uc.*=*.*} LIST=${LIST%-vc.*=*.*} if [[ "${LIST}" != "${EXT}" ]]; then # if a .qmail-${LIST}-default file exists, then delivery is possible (ezmlm: list-subscribe...) if [[ -n "$(vls ${HDIR}/.qmail-${LIST}-default)" ]]; then log "${SMTPRCPTTO} exists (.qmail-${LIST}-default)." ack fi fi # no other checks have prooven the existence of this email address log "no such recipient: ${SMTPRCPTTO}" nack