Limitare il numero di email inviate da un dato auth-user/domain/IP

8 ottobre 2020 by Roberto Puzzanghera 0 commenti

Se si vuole evitare il rischio di compromettere il proprio server a causa di account che inviano indiscriminatamente messaggi a tutto il mondo, ad esempio per via di di una password che è stata violata in qualche modo, si può utilizzare lo script rcptcheck-overlimit.sh di Luca Franceschini, che deve essere usato insieme alla patch rcptcheck (una patch derivata da Luca da un lavoro originale di Jay Soffian).

Nel 2016 Luca ha deciso di unire la sua "combo patch" con la mia, e da allora continuare a dare il suo personale contributo alla "combined patch" da noi installata, aggiungendo nuove funzionalità, spesso scrivendo lui stesso il codice. Luca è un amministratore di grossi server e anche un programmatore esperto. Lo script da lui condiviso qui è solo l'ultimo della serie ed è piuttosto sorprendente (almeno per me) osservare quante cose vengono fatte mettendo insieme appena 20 righe di buon linguaggio shell.

Come funziona

Ciò che segue è un semplice copia & incolla delle note poste dall'autore in testa allo stesso script.


This script limits the number of emails sent by relayclients (authusers or ip with RELAYCLIENT in tcprules). You must define the variable RCPTCHECK=/var/qmail/bin/rcptcheck-overlimit.sh and RCPTCHECKRELAYCLIENT="1".

This script will be called for every accepted rcptto. If RELAYCLIENT is not defined the script terminates with the exit code 112 (ignore/accept). Messages sent to domains in rcpthosts will NOT be accounted for.

For every accepted rcptto with RELAYCLIENT defined, a char 'X' will be appended to a file in the directory $OVERLIMITDIR; this file name will be the authuser, if defined, or the client ip address.

The script will look for an entry corresponding to the client (authuser or ip) in $LIMITSCONTROLFILE and use the number found as the maximum number of allowed outgoing emails.

If the OVERLIMITDIR is not writable by the user running qmail-smtpd or the LIMITSCONTROLFILE cannot be read, the script terminates with 112 (ignore/accept). In case of overlimit, an exit code 113 (reject/overlimit) will be returned to qmail-smtpd and the connection will be dropped with a 421. $LIMITSCONTROLFILE can contain comments, '0' means unlimited, the entry starting with ':' will be considered the default limit.

If the default entry can't be found, the default will be set to unlimited. In case more lines match the client name, only the last will be used.

A cronjob must be created to periodically cleanup files in $OVERLIMITDIR: to use daily limits, schedule the job once a day:

0 3 * * * find /var/qmail/overlimit/ -type f -exec rm -f "{}" \; 1> /dev/null


$LIMITSCONTROLFILE example:

:1000
1.2.3.4:3000
test@example.com:0

Configurazione

Scaricare come segue

wget -O /var/qmail/bin/rcptcheck-overlimit.sh https://notes.sagredo.eu/files/qmail/patches/rcptcheck/rcptcheck-overlimit.sh
chmod +x rcptcheck-overlimit.sh

Creare la cartella $OVERLIMIT dove verrà tracciato il numero di messaggi inviati da un dato IP/auth-user, e assegnare pieni privilegiand all'utente che esegue qmail-smtpd (vpopmail nel nostro caso):

mkdir /var/qmail/overlimit
chown -R vpopmail.vchkpw /var/qmail/overlimit
chmod -R 755 /var/qmail/overlimit

creare il control file relaylimits e modificarlo secondo le proprie necessità secondo le istruzioni di cui sopra:

cat > /var/qmail/control/relaylimits << __EOF__
:1000
__EOF__

Modificare poi i propri script qmail-smtpd/run e qmail-submission/run

export RCPTCHECK=/var/qmail/bin/rcptcheck-overlimit.sh
export RCPTCHECKRELAYCLIENT="1"

Preparare infine un cronjob per ripulire periodicamente i file in $OVERLIMITDIR, per esempio:

0 3 * * * find /var/qmail/overlimit/ -type f -exec rm -f "{}" \; 1> /dev/null

E naturalmente ricordarsi di riavviare qmail.

Aggiungi un commento

Ultimi commenti
Vedi anche...
Articoli recenti

RSS feeds