#!/bin/bash # apply_config.sh # Régénère les configs Postfix/Dovecot/Samba depuis les valeurs en BDD et recharge les services set -e PHP=/usr/bin/php # Récupère les valeurs depuis la BDD via PHP DOMAIN=$($PHP -r "require '/opt/copymail/web/includes/db.php'; echo cfg('domain');") HOSTNAME=$($PHP -r "require '/opt/copymail/web/includes/db.php'; echo cfg('hostname');") SMTP_PORT=$($PHP -r "require '/opt/copymail/web/includes/db.php'; echo cfg('smtp_port');") SUBM_PORT=$($PHP -r "require '/opt/copymail/web/includes/db.php'; echo cfg('submission_port');") IMAP_PORT=$($PHP -r "require '/opt/copymail/web/includes/db.php'; echo cfg('imap_port');") IMAPS_PORT=$($PHP -r "require '/opt/copymail/web/includes/db.php'; echo cfg('imaps_port');") WORKGROUP=$($PHP -r "require '/opt/copymail/web/includes/db.php'; echo cfg('smb_workgroup');") # --- Postfix main.cf --- cat > /etc/postfix/main.cf << POSTFIX # CopyMail - généré automatiquement myhostname = ${HOSTNAME}.${DOMAIN} mydomain = ${DOMAIN} myorigin = \$mydomain inet_interfaces = all inet_protocols = ipv4 mydestination = localhost mynetworks = 127.0.0.0/8 # Virtual mailboxes virtual_mailbox_domains = ${DOMAIN} virtual_mailbox_base = /var/mail/vhosts virtual_mailbox_maps = hash:/etc/postfix/vmailbox virtual_minimum_uid = 100 virtual_uid_maps = static:5000 virtual_gid_maps = static:5000 smtpd_banner = \$myhostname ESMTP biff = no append_dot_mydomain = no readme_directory = no # Taille max message: 50 Mo message_size_limit = 52428800 POSTFIX # --- Postfix master.cf (ports) --- cat > /etc/postfix/master.cf << MASTER smtp inet n - y - - smtpd ${SUBM_PORT} inet n - y - - smtpd -o syslog_name=postfix/submission -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes pickup unix n - y 60 1 pickup cleanup unix n - y - 0 cleanup qmgr unix n - n 300 1 qmgr tlsmgr unix - - y 1000? 1 tlsmgr rewrite unix - - y - - trivial-rewrite bounce unix - - y - 0 bounce defer unix - - y - 0 bounce trace unix - - y - 0 bounce verify unix - - y - 1 verify flush unix n - y 1000? 0 flush proxymap unix - - n - - proxymap proxywrite unix - - n - 1 proxymap smtp unix - - y - - smtp relay unix - - y - - smtp showq unix n - y - - showq error unix - - y - - error retry unix - - y - - error discard unix - - y - - discard local unix - n n - - local virtual unix - n n - - virtual lmtp unix - - y - - lmtp anvil unix - - y - 1 anvil scache unix - - y - 1 scache MASTER # --- Dovecot 2.4 (format unifié) --- cat > /etc/dovecot/dovecot.conf << DOVECOT dovecot_config_version = 2.4.0 dovecot_storage_version = 2.4.0 protocols = imap listen = * mail_driver = maildir mail_path = ~/Maildir mail_privileged_group = vmail auth_mechanisms = plain login auth_allow_cleartext = yes passdb passwd-file { default_password_scheme = SHA512-CRYPT passwd_file_path = /etc/dovecot/users } userdb static { fields { uid = vmail gid = vmail home = /var/mail/vhosts/%{user|domain}/%{user|username} } } service imap-login { inet_listener imap { port = ${IMAP_PORT} } inet_listener imaps { port = ${IMAPS_PORT} } } log_path = /var/log/dovecot.log info_log_path = /var/log/dovecot-info.log DOVECOT # --- Samba workgroup --- sed -i "s/^\s*workgroup\s*=.*/ workgroup = ${WORKGROUP}/" /etc/samba/smb.conf # --- Rechargement --- systemctl reload postfix || systemctl restart postfix systemctl reload dovecot || systemctl restart dovecot systemctl reload smbd || systemctl restart smbd echo "OK: configuration appliquée pour ${DOMAIN}"