|
- #!/bin/sh
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- echo "Installing programs..."
- apt install postfix dovecot-imapd dovecot-sieve opendkim spamassassin spamc
-
- which opendkim-genkey >/dev/null 2>&1 || apt install opendkim-tools
- domain="$(cat /etc/mailname)"
- subdom="mail"
- maildomain="$subdom.$domain"
-
-
- for x in /etc/letsencrypt/live/$maildomain /etc/letsencrypt/live/mail /etc/letsencrypt/live/$domain; do
- [ -d "$x" ] && certdir="$x" && break
- done
-
-
-
-
-
-
-
-
- echo "Configuring Postfix's main.cf..."
-
-
- postconf -e "smtpd_tls_key_file=$certdir/privkey.pem"
- postconf -e "smtpd_tls_cert_file=$certdir/fullchain.pem"
- postconf -e "smtpd_use_tls = yes"
- postconf -e "smtpd_tls_auth_only = yes"
- postconf -e "smtp_tls_security_level = may"
- postconf -e "smtp_tls_loglevel = 1"
- postconf -e "smtp_tls_CAfile=$certdir/cert.pem"
-
-
-
- postconf -e "smtpd_sasl_auth_enable = yes"
- postconf -e "smtpd_sasl_type = dovecot"
- postconf -e "smtpd_sasl_path = private/auth"
-
-
-
-
-
-
-
-
- postconf -e "home_mailbox = Mail/Inbox/"
-
-
-
-
-
-
-
- echo "Configuring Postfix's master.cf..."
-
- sed -i "/^\s*-o/d;/^\s*submission/d;/^\s*smtp/d" /etc/postfix/master.cf
-
- echo "smtp unix - - n - - smtp
- smtp inet n - y - - smtpd
- -o content_filter=spamassassin
- 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
- smtps inet n - y - - smtpd
- -o syslog_name=postfix/smtps
- -o smtpd_tls_wrappermode=yes
- -o smtpd_sasl_auth_enable=yes
- spamassassin unix - n n - - pipe
- user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f \${sender} \${recipient}" >> /etc/postfix/master.cf
-
-
-
-
-
-
-
-
- echo "Creating Dovecot config..."
-
- echo "# Dovecot config
- # Note that in the dovecot conf, you can use:
- # %u for username
- # %n for the name in name@domain.tld
- # %d for the domain
- # %h the user's home directory
-
- # If you're not a brainlet, SSL must be set to required.
- ssl = required
- ssl_cert = <$certdir/fullchain.pem
- ssl_key = <$certdir/privkey.pem
- # Plaintext login. This is safe and easy thanks to SSL.
- auth_mechanisms = plain login
-
- protocols = \$protocols imap
-
- # Search for valid users in /etc/passwd
- userdb {
- driver = passwd
- }
- #Fallback: Use plain old PAM to find user passwords
- passdb {
- driver = pam
- }
-
- # Our mail for each user will be in ~/Mail, and the inbox will be ~/Mail/Inbox
- # The LAYOUT option is also important because otherwise, the boxes will be \`.Sent\` instead of \`Sent\`.
- mail_location = maildir:~/Mail:INBOX=~/Mail/Inbox:LAYOUT=fs
- namespace inbox {
- inbox = yes
- mailbox Drafts {
- special_use = \\Drafts
- auto = subscribe
- }
- mailbox Junk {
- special_use = \\Junk
- auto = subscribe
- autoexpunge = 30d
- }
- mailbox Sent {
- special_use = \\Sent
- auto = subscribe
- }
- mailbox Trash {
- special_use = \\Trash
- }
- mailbox Archive {
- special_use = \\Archive
- }
- }
-
- # Here we let Postfix use Dovecot's authetication system.
-
- service auth {
- unix_listener /var/spool/postfix/private/auth {
- mode = 0660
- user = postfix
- group = postfix
- }
- }
-
- protocol lda {
- mail_plugins = \$mail_plugins sieve
- }
-
- protocol lmtp {
- mail_plugins = \$mail_plugins sieve
- }
-
- plugin {
- sieve = ~/.dovecot.sieve
- sieve_default = /var/lib/dovecot/sieve/default.sieve
- #sieve_global_path = /var/lib/dovecot/sieve/default.sieve
- sieve_dir = ~/.sieve
- sieve_global_dir = /var/lib/dovecot/sieve/
- }
- " > /etc/dovecot/dovecot.conf
-
- mkdir /var/lib/dovecot/sieve/
-
- echo "require [\"fileinto\", \"mailbox\"];
- if header :contains \"X-Spam-Flag\" \"YES\"
- {
- fileinto \"Junk\";
- }" > /var/lib/dovecot/sieve/default.sieve
-
- cut -d: -f1 /etc/passwd | grep -q "^vmail" || useradd vmail
- chown -R vmail:vmail /var/lib/dovecot
- sievec /var/lib/dovecot/sieve/default.sieve
-
- echo "Preparing user authetication..."
- grep -q nullok /etc/pam.d/dovecot ||
- echo "auth required pam_unix.so nullok
- account required pam_unix.so" >> /etc/pam.d/dovecot
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- echo "Generating OpenDKIM keys..."
- mkdir -p /etc/postfix/dkim
- opendkim-genkey -D /etc/postfix/dkim/ -d $ "$domain" -s "$subdom"
- chgrp opendkim /etc/postfix/dkim/*
- chmod g+r /etc/postfix/dkim/*
-
-
- echo "Configuring OpenDKIM..."
- grep -q "$domain" /etc/postfix/dkim/keytable ||
- echo "$subdom._domainkey.$domain $domain:mail:/etc/postfix/dkim/mail.private" >> /etc/postfix/dkim/keytable
-
- grep -q "$domain" /etc/postfix/dkim/signingtable ||
- echo "*@$domain $subdom._domainkey.$domain" >> /etc/postfix/dkim/signingtable
-
- grep -q "127.0.0.1" /etc/postfix/dkim/trustedhosts ||
- echo "127.0.0.1
- 10.1.0.0/16
- 1.2.3.4/24" >> /etc/postfix/dkim/trustedhosts
-
-
- grep -q "^KeyTable" /etc/opendkim.conf || echo "KeyTable file:/etc/postfix/dkim/keytable
- SigningTable refile:/etc/postfix/dkim/signingtable
- InternalHosts refile:/etc/postfix/dkim/trustedhosts" >> /etc/opendkim.conf
-
- sed -i '/^#Canonicalization/s/simple/relaxed\/simple/' /etc/opendkim.conf
- sed -i '/^#Canonicalization/s/^#//' /etc/opendkim.conf
-
- sed -e '/Socket/s/^#*/#/' -i /etc/opendkim.conf
- sed -i '/\local:\/var\/run\/opendkim\/opendkim.sock/a \Socket\t\t\tinet:12301@localhost' /etc/opendkim.conf
-
-
- sed -i "/^SOCKET/d" /etc/default/opendkim && echo "SOCKET=\"inet:12301@localhost\"" >> /etc/default/opendkim
-
-
- echo "Configuring Postfix with OpenDKIM settings..."
- postconf -e "smtpd_sasl_security_options = noanonymous, noplaintext"
- postconf -e "smtpd_sasl_tls_security_options = noanonymous"
- postconf -e "myhostname = $maildomain"
- postconf -e "milter_default_action = accept"
- postconf -e "milter_protocol = 6"
- postconf -e "smtpd_milters = inet:localhost:12301"
- postconf -e "non_smtpd_milters = inet:localhost:12301"
- postconf -e "mailbox_command = /usr/lib/dovecot/deliver"
-
- for x in dovecot postfix opendkim spamassassin; do
- printf "Restarting %s..." "$x"
- service "$x" restart && printf " ...done\\n"
- done
-
- pval="$(tr -d "\n" </etc/postfix/dkim/mail.txt | sed "s/k=rsa.* \"p=/k=rsa; p=/;s/\"\s*\"//;s/\"\s*).*//" | grep -o "p=.*")"
- echo "Here is your TXT entry:"
- echo
- echo
- echo
- printf "Record Name\\tRecord Type\\tText of entry\\n"
-
- printf "%s._domainkey\\tTXT\\t\\tv=DKIM1; k=rsa; %s\\n" "$subdom" "$pval"
-
- printf "%s\\tTXT\\t\\tv=spf1 mx a:%s -all\\n" "@" "$maildomain"
- echo
- echo
- echo "$pval"
|