## Table des matières 1. [Introduction et Architecture](#introduction-et-architecture) 2. [Prérequis](#prérequis) 3. [Installation des paquets](#installation-des-paquets) 4. [Configuration DNS](#configuration-dns) 5. [Configuration de Postfix](#configuration-de-postfix) 6. [Configuration de Dovecot](#configuration-de-dovecot) 7. [Sécurisation avec SSL/TLS](#sécurisation-avec-ssltls) 8. [Authentification SASL](#authentification-sasl) 9. [Tests et Validation](#tests-et-validation) 10. [Dépannage](#dépannage) --- ## Introduction et Architecture ### Qu'est-ce qu'un serveur mail ? Un serveur mail gère l'envoi et la réception des emails. Notre configuration utilise deux composants principaux : - **Postfix** : Agent de transfert de mail (MTA) - gère l'envoi et la réception SMTP - **Dovecot** : Serveur IMAP/POP3 - permet aux clients de consulter leurs emails ### Schéma d'Architecture Globale ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ ARCHITECTURE SERVEUR MAIL │ └─────────────────────────────────────────────────────────────────────────────┘ INTERNET SERVEUR CLIENTS ──────── ─────── ─────── ┌──────────────┐ ┌─────────────────────────────┐ │ Serveur │ Port 25 │ │ │ Externe │ ──────────────▶│ POSTFIX │ │ (SMTP) │ │ (MTA - SMTP) │ └──────────────┘ │ │ │ ┌─────────────────────┐ │ │ │ File d'attente │ │ │ │ (Mail Queue) │ │ │ └─────────────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────────────┐ │ │ │ Maildir │ │ │ │ /var/mail/vhosts/ │ │ │ └─────────────────────┘ │ │ │ │ └──────────────┼──────────────┘ │ ┌──────────────┼──────────────┐ │ ▼ │ │ DOVECOT │ ┌──────────┐ │ (IMAP/POP3 Server) │────▶│ Outlook │ │ │ └──────────┘ │ Ports: 993 (IMAPS) │ ┌──────────┐ │ 995 (POP3S) │────▶│Thunderbird│ │ 587 (Submission) │ └──────────┘ └─────────────────────────────┘ ┌──────────┐ │ Webmail │ └──────────┘ ``` ### Flux de Communication ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ FLUX EMAIL ENTRANT │ └─────────────────────────────────────────────────────────────────────────────┘ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ Envoyeur│ │ DNS │ │ Postfix │ │ Dovecot │ │ Client │ │ Externe │ │ (MX) │ │ (MTA) │ │ (IMAP) │ │ Mail │ └────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘ │ │ │ │ │ │ Requête MX │ │ │ │ │──────────────▶│ │ │ │ │ │ │ │ │ │ Réponse MX │ │ │ │ │◀──────────────│ │ │ │ │ │ │ │ │ │ Connexion SMTP (port 25) │ │ │──────────────────────────────▶│ │ │ │ │ │ │ │ │ │ │ Stockage │ │ │ │ │ Maildir │ │ │ │ │──────────────▶│ │ │ │ │ │ │ │ │ │ │ Connexion │ │ │ │ │ IMAP (993) │ │ │ │ │◀──────────────│ │ │ │ │ │ │ │ │ │ Emails │ │ │ │ │──────────────▶│ │ │ │ │ │ ``` ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ FLUX EMAIL SORTANT │ └─────────────────────────────────────────────────────────────────────────────┘ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ Client │ │ Dovecot │ │ Postfix │ │ DNS │ │Serveur │ │ Mail │ │ (Auth) │ │ (MTA) │ │ │ │Distant │ └────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘ │ │ │ │ │ │ SMTP Auth │ │ │ │ │ (port 587) │ │ │ │ │──────────────▶│ │ │ │ │ │ │ │ │ │ │ Validation │ │ │ │ │──────────────▶│ │ │ │ │ │ │ │ │ │ │ Requête MX │ │ │ │ │──────────────▶│ │ │ │ │ │ │ │ │ │ Réponse MX │ │ │ │ │◀──────────────│ │ │ │ │ │ │ │ │ │ Envoi SMTP │ │ │ │ │──────────────────────────────▶│ │ │ │ │ │ ``` --- ## Prérequis ### Configuration Système Requise | Élément | Minimum | Recommandé | |---------|---------|------------| | RAM | 512 Mo | 2 Go | | Stockage | 10 Go | 50 Go+ | | OS | Ubuntu 20.04+ / Debian 11+ | Ubuntu 22.04 LTS | | Réseau | IP publique fixe | IP dédiée | ### Ports à Ouvrir ``` ┌─────────────────────────────────────────────────────────────────┐ │ PORTS RÉSEAU REQUIS │ ├────────┬──────────────┬─────────────────────────────────────────┤ │ Port │ Protocole │ Description │ ├────────┼──────────────┼─────────────────────────────────────────┤ │ 25 │ SMTP │ Réception emails (serveur à serveur) │ │ 587 │ Submission │ Envoi emails (client authentifié) │ │ 465 │ SMTPS │ SMTP over SSL (legacy) │ │ 993 │ IMAPS │ IMAP over SSL (consultation emails) │ │ 995 │ POP3S │ POP3 over SSL (téléchargement emails) │ └────────┴──────────────┴─────────────────────────────────────────┘ ``` ### Vérifications Préalables ```bash # Vérifier le hostname hostname -f # Vérifier que le port 25 n'est pas bloqué telnet smtp.google.com 25 # Vérifier l'IP publique curl ifconfig.me ``` --- ## Installation des Paquets ### Ubuntu/Debian ```bash # Mise à jour du système sudo apt update && sudo apt upgrade -y # Installation de Postfix sudo apt install postfix postfix-policyd-spf-python -y # Lors de l'installation, choisir : # - Type de configuration : Site Internet # - Nom de courrier : votre-domaine.com # Installation de Dovecot sudo apt install dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd -y # Outils supplémentaires sudo apt install certbot mailutils -y ``` ### CentOS/RHEL ```bash # Installation des dépôts EPEL sudo dnf install epel-release -y # Installation de Postfix sudo dnf install postfix -y # Installation de Dovecot sudo dnf install dovecot dovecot-pigeonhole -y # Activer les services sudo systemctl enable postfix dovecot ``` --- ## Configuration DNS ### Enregistrements DNS Requis ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ CONFIGURATION DNS REQUISE │ └─────────────────────────────────────────────────────────────────────────────┘ ┌─────────────────────────────────────────────────────────────────────────────┐ │ TYPE │ NOM │ VALEUR │ TTL │ ├────────┼────────────────────┼───────────────────────────────┼──────────────┤ │ A │ mail │ 203.0.113.10 │ 3600 │ │ MX │ @ │ 10 mail.votre-domaine.com │ 3600 │ │ TXT │ @ │ v=spf1 mx a -all │ 3600 │ │ TXT │ _dmarc │ v=DMARC1; p=quarantine │ 3600 │ │ PTR │ 10.113.0.203 │ mail.votre-domaine.com │ 3600 │ └────────┴────────────────────┴───────────────────────────────┴──────────────┘ Légende : • A : Pointe le sous-domaine "mail" vers l'IP du serveur • MX : Indique le serveur mail pour le domaine • TXT : SPF pour l'authentification des emails sortants • TXT : DMARC pour la politique de traitement des emails • PTR : Reverse DNS (à configurer chez l'hébergeur) ``` ### Vérification DNS ```bash # Vérifier l'enregistrement MX dig MX votre-domaine.com +short # Vérifier l'enregistrement A dig A mail.votre-domaine.com +short # Vérifier le SPF dig TXT votre-domaine.com +short # Vérifier le reverse DNS dig -x VOTRE_IP_PUBLIQUE +short ``` --- ## Configuration de Postfix ### Architecture Interne de Postfix ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ ARCHITECTURE INTERNE POSTFIX │ └─────────────────────────────────────────────────────────────────────────────┘ ┌──────────────────┐ │ smtpd │◀─── Port 25 (entrant) │ (réception) │◀─── Port 587 (submission) └────────┬─────────┘ │ ▼ ┌──────────────────┐ │ cleanup │ │ (vérification) │ └────────┬─────────┘ │ ▼ ┌──────────────────┐ │ qmgr │ │ (gestionnaire │ │ de queue) │ └────────┬─────────┘ │ ┌────────────────────────┼────────────────────────┐ │ │ │ ▼ ▼ ▼ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ smtp │ │ lmtp │ │ local │ │ (sortant) │ │ (Dovecot) │ │ (système) │ └──────────────┘ └──────────────┘ └──────────────┘ │ │ │ ▼ ▼ ▼ Serveurs externes Dovecot LMTP /var/mail/ ``` ### Fichier /etc/postfix/main.cf ```bash # Créer une sauvegarde sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.backup # Éditer le fichier principal sudo nano /etc/postfix/main.cf ``` ```ini # ============================================================================= # POSTFIX - CONFIGURATION PRINCIPALE # ============================================================================= # ----------------------------------------------------------------------------- # IDENTITÉ DU SERVEUR # ----------------------------------------------------------------------------- myhostname = mail.votre-domaine.com mydomain = votre-domaine.com myorigin = $mydomain # ----------------------------------------------------------------------------- # RÉSEAUX ET INTERFACES # ----------------------------------------------------------------------------- inet_interfaces = all inet_protocols = ipv4 mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 # ----------------------------------------------------------------------------- # BOÎTES AUX LETTRES # ----------------------------------------------------------------------------- home_mailbox = Maildir/ mailbox_size_limit = 0 recipient_delimiter = + # ----------------------------------------------------------------------------- # TLS/SSL # ----------------------------------------------------------------------------- smtpd_tls_cert_file = /etc/letsencrypt/live/mail.votre-domaine.com/fullchain.pem smtpd_tls_key_file = /etc/letsencrypt/live/mail.votre-domaine.com/privkey.pem smtpd_tls_security_level = may smtpd_tls_auth_only = yes smtpd_tls_loglevel = 1 smtpd_tls_received_header = yes smtpd_tls_session_cache_timeout = 3600s smtp_tls_security_level = may smtp_tls_loglevel = 1 # Protocoles et ciphers sécurisés smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1 smtpd_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1 smtp_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1 smtp_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1 # ----------------------------------------------------------------------------- # AUTHENTIFICATION SASL (via Dovecot) # ----------------------------------------------------------------------------- smtpd_sasl_type = dovecot smtpd_sasl_path = private/auth smtpd_sasl_auth_enable = yes smtpd_sasl_security_options = noanonymous smtpd_sasl_local_domain = $myhostname broken_sasl_auth_clients = yes # ----------------------------------------------------------------------------- # RESTRICTIONS # ----------------------------------------------------------------------------- smtpd_helo_required = yes smtpd_helo_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_invalid_helo_hostname, reject_non_fqdn_helo_hostname smtpd_sender_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_non_fqdn_sender, reject_unknown_sender_domain smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination, reject_non_fqdn_recipient, reject_unknown_recipient_domain # ----------------------------------------------------------------------------- # LIMITES ET PERFORMANCES # ----------------------------------------------------------------------------- message_size_limit = 52428800 mailbox_size_limit = 0 smtpd_client_connection_count_limit = 10 smtpd_client_connection_rate_limit = 30 ``` ### Fichier /etc/postfix/master.cf ```bash sudo nano /etc/postfix/master.cf ``` ```ini # ============================================================================= # POSTFIX - SERVICES # ============================================================================= # Service SMTP standard (port 25) smtp inet n - y - - smtpd # Submission (port 587) - pour les clients authentifiés submission inet n - y - - smtpd -o syslog_name=postfix/submission -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_tls_auth_only=yes -o smtpd_reject_unlisted_recipient=no -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject -o milter_macro_daemon_name=ORIGINATING # SMTPS (port 465) - SMTP over SSL smtps inet n - y - - smtpd -o syslog_name=postfix/smtps -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes -o smtpd_reject_unlisted_recipient=no -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject -o milter_macro_daemon_name=ORIGINATING ``` --- ## Configuration de Dovecot ### Architecture Interne de Dovecot ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ ARCHITECTURE INTERNE DOVECOT │ └─────────────────────────────────────────────────────────────────────────────┘ Clients Mail Dovecot Stockage ─────────── ───────── ──────── ┌────────────┐ │ Outlook │──┐ └────────────┘ │ ┌─────────────────────────────┐ │ │ │ ┌────────────┐ │ IMAPS │ ┌─────────────────────┐ │ │Thunderbird │──┼────────▶│ │ imap-login │ │ └────────────┘ │ (993) │ │ (authentif.) │ │ │ │ └──────────┬──────────┘ │ ┌────────────┐ │ │ │ │ │ Mobile │──┘ │ ▼ │ └────────────┘ │ ┌─────────────────────┐ │ ┌───────────────┐ │ │ imap │ │ │ │ │ │ (protocole) │ │────▶│ Maildir │ │ └─────────────────────┘ │ │ │ │ │ │ /var/mail/ │ │ ┌─────────────────────┐ │ │ vhosts/ │ │ │ auth │───┼────▶│ domain/ │ │ │ (SASL) │ │ │ user/ │ │ └──────────┬──────────┘ │ │ │ │ │ │ └───────────────┘ │ ▼ │ │ ┌─────────────────────┐ │ Postfix ◀──────────│ │ auth-userdb │ │ (SASL) │ │ (socket) │ │ │ └─────────────────────┘ │ │ │ └─────────────────────────────┘ ``` ### Fichier /etc/dovecot/dovecot.conf ```bash sudo nano /etc/dovecot/dovecot.conf ``` ```ini # ============================================================================= # DOVECOT - CONFIGURATION PRINCIPALE # ============================================================================= # Protocoles activés protocols = imap pop3 lmtp # Écouter sur toutes les interfaces listen = *, :: # Inclure les configurations modulaires !include conf.d/*.conf ``` ### Fichier /etc/dovecot/conf.d/10-mail.conf ```bash sudo nano /etc/dovecot/conf.d/10-mail.conf ``` ```ini # ============================================================================= # DOVECOT - CONFIGURATION STOCKAGE MAIL # ============================================================================= # Emplacement des boîtes mail (format Maildir) mail_location = maildir:~/Maildir # Namespace pour la boîte de réception namespace inbox { inbox = yes } # Utilisateur/groupe pour accéder aux mails mail_uid = vmail mail_gid = vmail # Privilèges mail_privileged_group = mail # Première UID/GID valide first_valid_uid = 1000 first_valid_gid = 1000 ``` ### Fichier /etc/dovecot/conf.d/10-auth.conf ```bash sudo nano /etc/dovecot/conf.d/10-auth.conf ``` ```ini # ============================================================================= # DOVECOT - AUTHENTIFICATION # ============================================================================= # Désactiver l'authentification en clair sans SSL disable_plaintext_auth = yes # Mécanismes d'authentification auth_mechanisms = plain login # Inclure la configuration système !include auth-system.conf.ext ``` ### Fichier /etc/dovecot/conf.d/10-ssl.conf ```bash sudo nano /etc/dovecot/conf.d/10-ssl.conf ``` ```ini # ============================================================================= # DOVECOT - CONFIGURATION SSL/TLS # ============================================================================= # Activer SSL ssl = required # Certificats Let's Encrypt ssl_cert = &1 | grep -q "succeeded" && echo "OK" || echo "ERREUR" echo -n "Port 587 (Submission): " nc -zv localhost 587 2>&1 | grep -q "succeeded" && echo "OK" || echo "ERREUR" # Test 2: Vérifier que Dovecot écoute echo -n "Port 993 (IMAPS): " nc -zv localhost 993 2>&1 | grep -q "succeeded" && echo "OK" || echo "ERREUR" echo -n "Port 995 (POP3S): " nc -zv localhost 995 2>&1 | grep -q "succeeded" && echo "OK" || echo "ERREUR" # Test 3: Vérifier le certificat SSL echo -n "Certificat SSL: " openssl s_client -connect localhost:993 -servername mail.votre-domaine.com /dev/null | grep -q "Verify return code: 0" && echo "OK" || echo "VÉRIFIER" # Test 4: Vérifier la configuration Postfix echo -n "Configuration Postfix: " postfix check 2>&1 | grep -q "error" && echo "ERREUR" || echo "OK" # Test 5: Vérifier la configuration Dovecot echo -n "Configuration Dovecot: " dovecot -n 2>&1 | grep -q "Error" && echo "ERREUR" || echo "OK" echo "=== Fin des Tests ===" ``` ### Envoyer un Email de Test ```bash # Via la ligne de commande echo "Ceci est un test" | mail -s "Test Email" [email protected] # Vérifier les logs sudo tail -f /var/log/mail.log ``` ### Test avec Telnet/OpenSSL ```bash # Test SMTP openssl s_client -connect mail.votre-domaine.com:587 -starttls smtp # Test IMAP openssl s_client -connect mail.votre-domaine.com:993 ``` --- ## Dépannage ### Commandes de Diagnostic ```bash # Vérifier l'état des services sudo systemctl status postfix sudo systemctl status dovecot # Consulter les logs en temps réel sudo tail -f /var/log/mail.log sudo tail -f /var/log/mail.err # Vérifier la queue Postfix sudo postqueue -p # Forcer l'envoi de la queue sudo postqueue -f # Supprimer tous les messages en queue sudo postsuper -d ALL # Tester la configuration Postfix sudo postfix check # Tester la configuration Dovecot sudo doveconf -n ``` ### Problèmes Fréquents | Problème | Cause Probable | Solution | |----------|----------------|----------| | Connection refused port 25 | Postfix non démarré ou port bloqué | `systemctl start postfix` / Vérifier firewall | | SSL handshake failed | Certificat invalide ou expiré | Renouveler avec `certbot renew` | | Authentication failed | Mauvais mot de passe ou config SASL | Vérifier `/var/log/auth.log` | | Emails rejetés | SPF/DKIM/DMARC mal configurés | Vérifier les enregistrements DNS | | Maildir non créé | Permissions incorrectes | `chown -R vmail:vmail /var/mail` | ### Schéma de Résolution ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ ARBRE DE DÉCISION - DÉPANNAGE │ └─────────────────────────────────────────────────────────────────────────────┘ ┌─────────────────┐ │ Email ne part │ │ pas ? │ └────────┬────────┘ │ ┌───────────────┴───────────────┐ ▼ ▼ ┌───────────────┐ ┌───────────────┐ │ Service actif │ │ Service arrêté│ │ ? │ │ │ └───────┬───────┘ └───────┬───────┘ │ │ ▼ ▼ ┌──────────────────┐ ┌──────────────────┐ │ Vérifier les │ │ systemctl start │ │ logs mail.log │ │ postfix │ └────────┬─────────┘ └──────────────────┘ │ ┌────────────┼────────────┐ ▼ ▼ ▼ ┌────────┐ ┌────────┐ ┌────────┐ │ Auth │ │ DNS │ │ Quota │ │ Error │ │ Error │ │ Error │ └───┬────┘ └───┬────┘ └───┬────┘ │ │ │ ▼ ▼ ▼ ┌────────┐ ┌────────┐ ┌────────┐ │Vérifier│ │Vérifier│ │Vérifier│ │password│ │MX, SPF │ │espace │ │& SASL │ │DMARC │ │disque │ └────────┘ └────────┘ └────────┘ ``` --- ## Récapitulatif des Fichiers ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ STRUCTURE DES FICHIERS DE CONFIGURATION │ └─────────────────────────────────────────────────────────────────────────────┘ /etc/ ├── postfix/ │ ├── main.cf ← Configuration principale Postfix │ ├── master.cf ← Services et ports │ └── main.cf.backup ← Sauvegarde │ ├── dovecot/ │ ├── dovecot.conf ← Configuration principale Dovecot │ └── conf.d/ │ ├── 10-mail.conf ← Stockage des mails │ ├── 10-auth.conf ← Authentification │ ├── 10-ssl.conf ← Certificats SSL │ └── 10-master.conf ← Services et sockets │ └── letsencrypt/ └── live/ └── mail.votre-domaine.com/ ├── fullchain.pem ← Certificat + chaîne └── privkey.pem ← Clé privée /var/ ├── mail/ │ └── vhosts/ │ └── votre-domaine.com/ ← Boîtes mail (Maildir) │ └── log/ ├── mail.log ← Logs principaux └── mail.err ← Erreurs ``` --- ## Annexes ### A. Commandes Utiles ```bash # Gestion des services sudo systemctl {start|stop|restart|status} postfix sudo systemctl {start|stop|restart|status} dovecot # Logs sudo journalctl -u postfix -f sudo journalctl -u dovecot -f # Queue sudo postqueue -p # Afficher la queue sudo postsuper -d ALL # Vider la queue # Utilisateurs sudo doveadm user '*' # Lister les utilisateurs ``` ### B. Checklist de Déploiement - [ ] DNS : Enregistrements A, MX, SPF, DMARC configurés - [ ] Reverse DNS (PTR) configuré - [ ] Certificat SSL obtenu et valide - [ ] Postfix configuré et testé - [ ] Dovecot configuré et testé - [ ] Authentification SASL fonctionnelle - [ ] Firewall : Ports 25, 587, 993, 995 ouverts - [ ] Test d'envoi et réception réussi - [ ] Renouvellement automatique du certificat configuré ---