Databases, Systems & Networks La caverne d'Ali Data

Accueil Logiciel

Serveur mail sur Ubuntu 26.04 LTS : Postfix, MariaDB, Dovecot, Amavis, SpamAssassin, ClamAV

9. Dovecot 2.4 : IMAP, LMTP, SASL, Sieve, quotas

La syntaxe 2.4 est incompatible avec les versions 2.2/2.3 : ne reprenez pas un ancien dovecot.conf.

. /root/mail.env
[ -f /etc/dovecot/dovecot.conf.orig ] || cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.orig
cat > /etc/dovecot/dovecot.conf <<'EOF'
dovecot_config_version = 2.4.0
dovecot_storage_version = 2.4.0

protocols = imap lmtp sieve
postmaster_address = postmaster@__DOMAINE__
recipient_delimiter = +

# --- TLS et authentification
ssl = required
ssl_min_protocol = TLSv1.2
ssl_server_cert_file = /etc/ssl/mail/fullchain.pem
ssl_server_key_file = /etc/ssl/mail/privkey.pem
auth_allow_cleartext = no
auth_mechanisms = plain login
auth_username_format = %{user | lower}

# --- Stockage
mail_driver = maildir
mail_path = ~/Maildir
mail_uid = vmail
mail_gid = vmail
first_valid_uid = 5000
last_valid_uid = 5000

# --- Base PostfixAdmin
sql_driver = mysql
mysql localhost {
  user = mailread
  password = __MDP_LECTURE__
  dbname = postfix
}
passdb sql {
  default_password_scheme = BLF-CRYPT
  query = SELECT username AS user, password FROM mailbox \
    WHERE username = '%{user}' AND active = 1
}
userdb sql {
  query = SELECT CONCAT('/var/vmail/', maildir) AS home, \
    IF(quota > 0, CONCAT(quota, 'B'), 'unlimited') AS quota_storage_size \
    FROM mailbox WHERE username = '%{user}' AND active = 1
  iterate_query = SELECT username AS user FROM mailbox WHERE active = 1
}

# --- Quotas
mail_plugins {
  quota = yes
  quota_clone = yes
}
quota "User quota" {
  driver = count
  warning warn-95 {
    quota_storage_percentage = 95
    execute quota-warning {
      args = 95 %{user}
    }
  }
  warning warn-80 {
    quota_storage_percentage = 80
    execute quota-warning {
      args = 80 %{user}
    }
  }
}
# copie de l'occupation dans la table quota2, lue par PostfixAdmin
quota_clone {
  dict proxy {
    name = quota
  }
}
dict_server {
  dict quota {
    driver = sql
    dict_map priv/quota/storage {
      sql_table = quota2
      username_field = username
      value_field bytes {
        type = uint
      }
    }
    dict_map priv/quota/messages {
      sql_table = quota2
      username_field = username
      value_field messages {
        type = uint
      }
    }
  }
}

protocol imap {
  mail_plugins {
    imap_quota = yes
    imap_sieve = yes
  }
}
protocol lmtp {
  mail_plugins {
    sieve = yes
  }
}

# --- Dossiers et apprentissage antispam
namespace inbox {
  inbox = yes
  separator = /
  mailbox Drafts {
    auto = subscribe
    special_use = \Drafts
  }
  mailbox Junk {
    auto = subscribe
    special_use = \Junk
    # message déplacé VERS Junk : appris comme spam
    sieve_script learn-spam {
      type = before
      cause = copy append
      path = /etc/dovecot/sieve/learn-spam.sieve
    }
  }
  mailbox Sent {
    auto = subscribe
    special_use = \Sent
  }
  mailbox Trash {
    auto = subscribe
    special_use = \Trash
  }
}
# message sorti DE Junk : appris comme légitime
imapsieve_from Junk {
  sieve_script learn-ham {
    type = before
    cause = copy
    path = /etc/dovecot/sieve/learn-ham.sieve
  }
}

# --- Sieve
sieve_plugins {
  sieve_imapsieve = yes
  sieve_extprograms = yes
}
sieve_global_extensions {
  vnd.dovecot.pipe = yes
}
sieve_pipe_bin_dir = /etc/dovecot/sieve/bin
sieve_script spam-to-junk {
  type = before
  driver = file
  path = /etc/dovecot/sieve/spam-to-junk.sieve
}
sieve_script personal {
  driver = file
  path = ~/sieve
  active_path = ~/.dovecot.sieve
}

# --- Services
service imap-login {
  inet_listener imap {
    port = 143
  }
  inet_listener imaps {
    port = 993
    ssl = yes
  }
}
service managesieve-login {
  inet_listener sieve {
    port = 4190
  }
}
service lmtp {
  unix_listener /var/spool/postfix/private/dovecot-lmtp {
    user = postfix
    group = postfix
    mode = 0600
  }
}
service auth {
  unix_listener /var/spool/postfix/private/auth {
    user = postfix
    group = postfix
    mode = 0660
  }
  # consultation des comptes par dovecot-lda (alertes de quota)
  unix_listener auth-userdb {
    user = vmail
    mode = 0600
  }
}
service dict {
  unix_listener dict {
    user = vmail
    mode = 0660
  }
}
# refus des boîtes pleines dès la session SMTP ; les alias, inconnus
# de Dovecot, sont laissés à Postfix (DUNNO)
quota_status_nouser = DUNNO
service quota-status {
  executable = quota-status -p postfix
  client_limit = 1
  unix_listener /var/spool/postfix/private/quota-status {
    user = postfix
    group = postfix
    mode = 0660
  }
}
service quota-warning {
  executable = script /usr/local/bin/quota-warning.sh
  user = vmail
  unix_listener quota-warning {
    user = vmail
    mode = 0600
  }
}

!include_try local.conf
EOF
appliquer /etc/dovecot/dovecot.conf
# lisible par vmail : nécessaire aux alertes de quota
chown root:vmail /etc/dovecot/dovecot.conf
chmod 640 /etc/dovecot/dovecot.conf

Message d’alerte de quota et scripts Sieve :

. /root/mail.env
cat > /usr/local/bin/quota-warning.sh <<'EOF'
#!/bin/sh
POURCENT="$1"
COMPTE="$2"
/usr/lib/dovecot/dovecot-lda -d "$COMPTE" -o quota_enforce=no <<MSG
From: postmaster@__DOMAINE__
To: $COMPTE
Subject: Boite mail remplie a $POURCENT%
Content-Type: text/plain; charset=utf-8

Votre boite mail depasse $POURCENT% de son quota. Pensez a la vider.
MSG
EOF
appliquer /usr/local/bin/quota-warning.sh
chmod 755 /usr/local/bin/quota-warning.sh

mkdir -p /etc/dovecot/sieve/bin
cat > /etc/dovecot/sieve/spam-to-junk.sieve <<'EOF'
require ["fileinto", "mailbox"];
if header :contains "X-Spam-Flag" "YES" {
  fileinto :create "Junk";
  stop;
}
EOF
cat > /etc/dovecot/sieve/learn-spam.sieve <<'EOF'
require ["vnd.dovecot.pipe", "copy", "imapsieve"];
pipe :copy "learn-spam.sh";
EOF
cat > /etc/dovecot/sieve/learn-ham.sieve <<'EOF'
require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];
if environment :matches "imap.mailbox" "*" {
  set "dossier" "${1}";
}
# vider Junk vers la corbeille n'est pas un signalement "légitime"
if string "${dossier}" "Trash" {
  stop;
}
pipe :copy "learn-ham.sh";
EOF
for type in spam ham; do
cat > /etc/dovecot/sieve/bin/learn-$type.sh <<EOF
#!/bin/sh
HOME=/var/vmail exec /usr/bin/sa-learn --$type
EOF
done
chmod 755 /etc/dovecot/sieve/bin/*.sh

Page suivante : SpamAssassin, ClamAV et Amavis

Pages : ⬅ Page précédente Page suivante ➡

Lire aussi:  Postfix + fail2ban = win