PDA

View Full Version : [mini howto] postfix configurazione semplice e mailing list


HexDEF6
15-06-2007, 17:54
Premessa


Se si ha un server, e si vogliono gestire alcune mail, si puo' optare, al posto di implementare server smtp/pop/imap ecc., solamente un server smtp che forwardi le mail ad altri account di posta preesistenti.
Con questo metodo la gestioni si semplifica parecchio, non necessitando di server pop/imap oltre a tutti i software acessori (antivirus, antispam, gestione utenti ecc).


Postfix

Questo si puo facilmente implementare con postfix.
Il file di configurazione /etc/postfix/main.cf sara' cosi configurato:

queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/lib/postfix
mail_owner = postfix

#qui dobbiamo mettere l'hostname del nostro server (in questo caso mail.miodominio.com)
myhostname = mail.miodominio.com

#inseriamo il dominio del server
mydomain = miodominio.com

myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost $mydomain
unknown_local_recipient_reject_code = 550

#inseriamo su quali reti postfix fara' da relay (IMPORTANTE!)
mynetworks = 127.0.0.0/8 192.168.10.0/24

relay_domains = $mydestination
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases


debug_peer_level = 2
debugger_command =
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
xxgdb $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.3.3/samples
readme_directory = /usr/share/doc/postfix-2.3.3/README_FILES

#se abbiamo un altro account che ha gli stessi indirizzi ([email protected] e [email protected]) possiamo usare un virtual alias domain
virtual_alias_domains = miodominio.net
#e andare ad indicare quale e' il file usato per i virtual
virtual_alias_maps = hash:/etc/postfix/virtual


Il 90% del file di configurazione e' gia preconfezionato di default, le uniche variazioni sono quelle con il commento.
In questa maniera postfix accetta le mail per gli indirizzi @miodominio.com e @miodominio.net e fa da relay solamente per gli ip presenti in mynetworks.

Il file di configurazione e' a posto, ma prima che il tutto funzioni bisogna "creare" gli utenti, utilizzando i file /etc/aliases e /etc/postfix/virtual

/etc/aliases (ho omesso parecchi alias che ci sono di default nel file, che dipendono dalla distribuzione..

info: [email protected]
contact: info
help: info

pippo: [email protected]
paperino: [email protected]


in questa maniera tutte le mail mandate a info, contact, help @miodominio.com vengono girate a [email protected], inoltre le mail indirizzate a [email protected] vengono girate a [email protected] e infine le mail per [email protected] vengono girate a [email protected] (sgamato! :D )

per le mail che arrivano a miodominio.net ci pensa il file /etc/postfix/virual:

#importante altrimenti non funziona!
miodominio.net anything

[email protected] pippo
[email protected] paperino
[email protected] info

in questo caso le mail indirizzate a [email protected] vengono girate all'alias locale pippo (che poi punta a [email protected])
la prima riga e' importante per far si che il tutto venga rigirato agli alias locali.

Infine bisogna ricordare che ogni volta che si modifica il file alias si deve lanciare il comando newaliases e ogni volta che si cambia il file virtual si deve lanciare un postamp /etc/postfix/virtual

Mailing list

per mettere in piedi una mailing list con pochi utenti e gestita da una sola persona, ci sono un paio di metodi:
con gli alias
e con uno script

con alias
Per mettere in piedi una semplice mailing list, si puo' fare un alias che giri le mail a X altri indirizzi:
mettete in /etc/aliases

miamailinglist: [email protected], [email protected], [email protected]

in questa maniera mandando una mail a [email protected], viene rigirata a tutti e 3 gli indirizzi di posta
ovviamente se volete che la mailing list venga raggiunta anche dall'indirizzo [email protected], basta aggiungere in /etc/postfix/virtual:

miamailinglist miamailinglist


in questa maniera pero' chiunque puo' mandare mail alla mailing list, e non solo i partecipanti.
(ovviamente al posto di miamailinglist mettete quello che volete!)

con script

con questo secondo metodo, usando lo script che si trova su http://www.heinzi.at/projects/simple_forwarder/
si risolve il problema di chi puo' mandare mail in lista, il file /etc/aliases sara cosi:

miamailinglist: "| /usr/local/bin/simple_forwarder miamailinglist"

dovrete editare lo script /usr/local/bin/simple_forwarder (ovviamente dovrete renderlo eseguibile) nelle sue prime parti e dovrete puntarlo alla directory che contiene i file di configurazione della mailing list, che sono semplicemente 2 file:

un file senders che contiene tutti gli indirizzi mail che possono spedire alla mailing list e un file miamailinglist.recipients che dovra' contenere tutti i "partecipanti" alla mailing list.
Come sempre se volete che si possa scrivere anche a [email protected] dovrete modificare il file /etc/postfix/virtual di conseguenza (come sopra)

Questo script pero' ha un paio di inconvenienti:

se c'e' piu' di una mailing list, non si possono avere senders diversi
non inserisce [miamailinglist] all'inizio del Subject delle mail (comodo lato client se si vogliono filtrare le mail)

quindi ho fatto qualche piccola modifica allo script per ovviare a questo, e lo script e' diventato:

# Simple mailing list distributor and filter
#
# Usage: simple_forwarder [--open] [--bounces-to bounce-address] listname
#
# The mail is expected on stdin. The script
#
# - if "--open" is NOT present on the command line:
# checks if the From:-address is in ALLOWED_SENDERS. If it isn't, an
# error message (REJECT_SUBJECT, REJECT_FROM, REJECT_TEXT) is sent
# back to the envelope sender (unless the message has been tagged by
# SpamAssassin as spam with score >= 5).
#
# - forwards the message to all recipients in LISTDIR/listname.recipients.
# If --bounces-to is set, the envelope sender is bounce-address rather
# than the original envelope-from.
#
# All files must be newline-separated lists of mail addresses.
#
# For use from within /etc/aliases (Postfix):
# - group nogroup must have read access on ALLOWED_SENDERS, LISTDIR and
# all files within LISTDIR
# - group nogroup must have write access on LOGFILE
#
# Change the settings below to suit your needs before using.
#
# Author: Heinrich Moser, [email protected]
# Version: 1.2, 2006-03-20

# Settings
LOGFILE=/var/log/mlist.log
LISTDIR=/etc/mlist
SENDMAIL=/usr/lib/sendmail
DOMAIN="@miodominio.com" # Mailing list suffix used for loop detection
DETECT_SPAM="X-Spam-Level: *****" # If this is found (grep -Fi), message is spam
CONFIG_FILE="/etc/simple_forwarder.conf"

# Rejection mail
REJECT_SUBJECT="Your message to _LIST_$DOMAIN has been rejected"
REJECT_FROM="do_not_reply$DOMAIN"
REJECT_TEXT="Please register your e-mail address _EMAIL_ to be allowed
to send mail to the restricted mailing list _LIST_$DOMAIN.

The following message was rejected:
From: _FROM_
To: _TO_
Subject: _SUBJECT_
Date: _DATE_
Message-ID: _MSGID_

If there are any problems, ask your administrator for help.

Hugs,
the Administrators"

# Load configuration values from config file, if exists
[ -e "$CONFIG_FILE" ] && source "$CONFIG_FILE"

# Functions
log() {
echo "`date "+%Y-%m-%d %H:%M:%S"` $1" >> $LOGFILE
}

leave() { # clean up and exit
if [ \( -n "$MAILFILE" \) -a \( -f "$MAILFILE" \) ]; then
rm -f -- $MAILFILE
fi
exit 0
}

leave_usage() { # show usage message and exit with error
echo "Usage: simple_forwarder [--open] [--bounces-to bounce-address] listname"
exit 1
}

# Save mail from stdin to temporary file
MAILFILE=`tempfile`
cat - > $MAILFILE

# Parse command line arguments
REDIRECT_BOUNCES=0
OPEN=0
LIST=
while [ -n "$1" ]; do
if [ "$1" = "--bounces-to" ]; then
REDIRECT_BOUNCES=1
BOUNCES_TO=$2
shift 2
elif [ "$1" = "--open" ]; then
OPEN=1
shift 1
elif [ -z "$LIST" ]; then
LIST=$1
shift
else
leave_usage
fi
done

if [ -z "$LIST" ]; then
leave_usage
fi

ALLOWED_SENDERS=$LISTDIR/$LIST.senders

# Parse mail
MSGID=`cat "$MAILFILE" | formail -czx Message-ID:`
SUBJECT=`cat "$MAILFILE" | formail -czx Subject:`
FROM=`cat "$MAILFILE" | formail -czx From:`
ENV_SENDER=`cat "$MAILFILE" | formail -rczx To:`

log "received: mail $MSGID for $LIST from $FROM ($ENV_SENDER): $SUBJECT"

# Detect mail loops
cat "$MAILFILE" | formail -czx X-Loop: | grep -Fix "$LIST$DOMAIN"
if [ "$?" -eq "0" ]; then
log "dropped: loop detected in mail $MSGID"
leave
fi

# Check if sender is allowed
if [ "$OPEN" -eq "0" ]; then
# Get sender address (only address): remove Reply-To, generate reply header, get "To:", grep
FROM_EMAIL=`cat "$MAILFILE" | formail -fI Reply-To: | formail -rctzx To:`
grep -Fixq "$FROM_EMAIL" $ALLOWED_SENDERS
RET=$?
if [ "$RET" -eq "2" ]; then
log "dropped: error during sender check of mail $MSGID ($FROM_EMAIL, $ALLOWED_SENDERS)"
leave
elif [ "$RET" -eq "1" ]; then
if cat "$MAILFILE" | grep -Fiq "$DETECT_SPAM"; then
# Message was spam - drop it silently
log "rejected: $FROM_EMAIL (mail $MSGID) does not have permission - dropped"
else
# Get some more data about the mail
TO=`cat "$MAILFILE" | formail -czx To:`
DATE=`cat "$MAILFILE" | formail -czx Date:`

# Send error message to envelope sender
S="${REJECT_SUBJECT//_LIST_/$LIST}"
T="${REJECT_TEXT//_LIST_/$LIST}"
T="${T//_EMAIL_/$FROM_EMAIL}"
T="${T//_FROM_/$FROM}"
T="${T//_TO_/$TO}"
T="${T//_SUBJECT_/$SUBJECT}"
T="${T//_DATE_/$DATE}"
T="${T//_MSGID_/$MSGID}"
cat "$MAILFILE" | ( formail -r ; echo "$T" ) | \
formail -bA "X-Loop: $LIST$DOMAIN" -I "Subject: $S" -I "From: $REJECT_FROM" | \
$SENDMAIL -bm -f "" -it
log "rejected: $FROM_EMAIL (mail $MSGID) does not have permission - sender notified"
fi
leave
fi
fi

# Forward mail to intended recipients
RECIPIENTS_FILE=${LISTDIR}/${LIST}.recipients
if [ ! -f $RECIPIENTS_FILE ]; then
log "dropped: recipients file $RECIPIENTS_FILE for mail $MSGID not found"
leave
fi

for RECIPIENT in `cat "$RECIPIENTS_FILE"`; do
if [ "$REDIRECT_BOUNCES" -eq "1" ]; then
cat "$MAILFILE" | \
formail -bf -A "Errors-To: $BOUNCES_TO" -A "X-Loop: $LIST$DOMAIN" | \
$SENDMAIL -bm -f "$BOUNCES_TO" -i "$RECIPIENT"
else
SUB=$(cat "$MAILFILE" | formail -X Subject: | sed -e 's/Subject://' -e "s/\[$LIST\] //")
cat "$MAILFILE" | \
formail -bf -A "X-Loop: $LIST$DOMAIN" -I "Subject: [$LIST] $SUB" | \
$SENDMAIL -bm -f "$ENV_SENDER" -i "$RECIPIENT"
fi
done
log "sent: message $MSGID sent to list $LIST"
leave

l'unica cosa che cambia a livello di configurazione e' il file di chi puo' spedire mail, che passa da '''senders''' a '''miamailinglist.senders''' in modo da poterne avere uno diverso per ogni mailing list.

Conclusioni

Postfix e' molto semplice da configurare per mettere in piedi un server di posta in poco tempo, ma ricordo sempre che oltre ad usare questo howto, consiglio di dare un'occhiata al sito ufficiale http://www.postfix.org/ anche perche' potrei aver commesso castronerie varie!
Ovviamente se vedete errori o avete suggerimenti, basta che postate!

Ciao!