emailwiz-arch/emailwiz.sh

370 lines
13 KiB
Bash
Raw Normal View History

2019-05-27 20:15:26 +00:00
#!/bin/sh
2023-01-31 18:51:59 +00:00
# BEFORE INSTALLING
2019-05-27 20:15:26 +00:00
2023-01-31 18:51:59 +00:00
# Have a Debian or Ubuntu server with a static IP and DNS records (usually
# A/AAAA) that point your domain name to it.
2020-06-03 17:43:16 +00:00
# NOTE WHILE INSTALLING
# On installation of Postfix, select "Internet Site" and put in TLD (without
# `mail.` before it).
2019-05-27 20:15:26 +00:00
2023-01-31 18:51:59 +00:00
# AFTER INSTALLING
# More DNS records will be given to you to install. One of them will be
# different for every installation and is uniquely generated on your machine.
umask 0022
apt-get install -y postfix postfix-pcre dovecot-imapd dovecot-sieve opendkim opendkim-tools spamassassin spamc net-tools fail2ban
2019-05-27 23:24:15 +00:00
domain="$(cat /etc/mailname)"
2020-07-21 21:23:38 +00:00
subdom=${MAIL_SUBDOM:-mail}
2019-05-27 23:24:15 +00:00
maildomain="$subdom.$domain"
2020-06-20 19:43:27 +00:00
certdir="/etc/letsencrypt/live/$maildomain"
2019-05-27 20:15:26 +00:00
# Open required mail ports, and 80, for Certbot.
for port in 80 993 465 25 587; do
ufw allow "$port" 2>/dev/null
done
2022-07-01 23:07:46 +00:00
[ ! -d "$certdir" ] &&
possiblecert="$(certbot certificates 2>/dev/null | grep "Domains:\.* \(\*\.$domain\|$maildomain\)\(\s\|$\)" -A 2 | awk '/Certificate Path/ {print $3}' | head -n1)" &&
2022-07-01 23:07:46 +00:00
certdir="${possiblecert%/*}"
2023-01-27 00:51:36 +00:00
[ ! -d "$certdir" ] &&
certdir="/etc/letsencrypt/live/$maildomain" &&
case "$(netstat -tulpn | grep ":80\s")" in
*nginx*)
apt install -y python3-certbot-nginx
certbot -d "$maildomain" certonly --nginx --register-unsafely-without-email --agree-tos
;;
*apache*)
apt install -y python3-certbot-apache
certbot -d "$maildomain" certonly --apache --register-unsafely-without-email --agree-tos
;;
*)
apt install -y python3-certbot
certbot -d "$maildomain" certonly --standalone --register-unsafely-without-email --agree-tos
;;
2023-01-31 18:51:59 +00:00
esac
[ ! -d "$certdir" ] && echo "Error locating or installing SSL certificate." && exit 1
2019-05-27 20:15:26 +00:00
echo "Configuring Postfix's main.cf..."
# Change the cert/key files to the default locations of the Let's Encrypt cert/key
2020-06-03 17:43:16 +00:00
postconf -e "smtpd_tls_key_file=$certdir/privkey.pem"
postconf -e "smtpd_tls_cert_file=$certdir/fullchain.pem"
2021-09-14 23:34:54 +00:00
postconf -e "smtp_tls_CAfile=$certdir/cert.pem"
# Enable, but do not require TLS. Requiring it with other server would cause
# mail delivery problems and requiring it locally would cause many other
# issues.
postconf -e 'smtpd_tls_security_level = may'
postconf -e 'smtp_tls_security_level = may'
2021-09-14 23:34:54 +00:00
# TLS required for authentication.
postconf -e 'smtpd_tls_auth_only = yes'
2021-09-14 23:34:54 +00:00
# Exclude obsolete, insecure and obsolete encryption protocols.
postconf -e 'smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1'
postconf -e 'smtp_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1'
postconf -e 'smtpd_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1'
postconf -e 'smtp_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1'
2021-09-14 23:34:54 +00:00
# Exclude suboptimal ciphers.
postconf -e 'tls_preempt_cipherlist = yes'
postconf -e 'smtpd_tls_exclude_ciphers = aNULL, LOW, EXP, MEDIUM, ADH, AECDH, MD5, DSS, ECDSA, CAMELLIA128, 3DES, CAMELLIA256, RSA+AES, eNULL'
2019-05-27 20:15:26 +00:00
# Here we tell Postfix to look to Dovecot for authenticating users/passwords.
# Dovecot will be putting an authentication socket in /var/spool/postfix/private/auth
postconf -e 'smtpd_sasl_auth_enable = yes'
postconf -e 'smtpd_sasl_type = dovecot'
postconf -e 'smtpd_sasl_path = private/auth'
2019-05-27 20:15:26 +00:00
# helo, sender, relay and recipient restrictions
postconf -e "smtpd_sender_login_maps = pcre:/etc/postfix/login_maps.pcre"
postconf -e 'smtpd_sender_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_sender_login_mismatch, reject_unknown_reverse_client_hostname, reject_unknown_sender_domain'
2022-10-01 15:40:47 +00:00
postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination, reject_unknown_recipient_domain'
2023-01-26 21:56:57 +00:00
postconf -e 'smtpd_relay_restrictions = permit_sasl_authenticated, reject_unauth_destination'
postconf -e 'smtpd_helo_required = yes'
postconf -e 'smtpd_helo_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_invalid_helo_hostname, reject_non_fqdn_helo_hostname, reject_unknown_helo_hostname'
2019-05-27 20:15:26 +00:00
2020-06-03 17:43:16 +00:00
# NOTE: the trailing slash here, or for any directory name in the home_mailbox
# command, is necessary as it distinguishes a maildir (which is the actual
# directories that what we want) from a spoolfile (which is what old unix
# boomers want and no one else).
postconf -e 'home_mailbox = Mail/Inbox/'
2019-05-27 20:15:26 +00:00
# Prevent "Received From:" header in sent emails in order to prevent leakage of public ip addresses
postconf -e "header_checks = regexp:/etc/postfix/header_checks"
# strips "Received From:" in sent emails
echo "/^Received:.*/ IGNORE
/^X-Originating-IP:/ IGNORE" >> /etc/postfix/header_checks
# Create a login map file that ensures that if a sender wants to send a mail from a user at our local
# domain, they must be authenticated as that user
echo "/^(.*)@$(sh -c "echo $domain | sed 's/\./\\\./'")$/ \${1}" > /etc/postfix/login_maps.pcre
2019-05-27 20:15:26 +00:00
# master.cf
echo "Configuring Postfix's master.cf..."
sed -i '/^\s*-o/d;/^\s*submission/d;/^\s*smtp/d' /etc/postfix/master.cf
2019-05-27 20:15:26 +00:00
2019-05-27 22:43:27 +00:00
echo "smtp unix - - n - - smtp
smtp inet n - y - - smtpd
2019-08-11 15:05:11 +00:00
-o content_filter=spamassassin
2019-05-27 22:43:27 +00:00
submission inet n - y - - smtpd
2019-05-27 20:15:26 +00:00
-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
2020-06-03 17:43:16 +00:00
# By default, dovecot has a bunch of configs in /etc/dovecot/conf.d/ These
# files have nice documentation if you want to read it, but it's a huge pain to
# go through them to organize. Instead, we simply overwrite
# /etc/dovecot/dovecot.conf because it's easier to manage. You can get a backup
# of the original in /usr/share/dovecot if you want.
2021-09-14 23:34:54 +00:00
mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.backup.conf
2019-05-27 20:15:26 +00:00
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
ssl = required
2020-06-03 17:43:16 +00:00
ssl_cert = <$certdir/fullchain.pem
ssl_key = <$certdir/privkey.pem
ssl_min_protocol = TLSv1.2
ssl_cipher_list = "'EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA256:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA384:EDH+aRSA+AESGCM:EDH+aRSA+SHA256:EDH+aRSA:EECDH:!aNULL:!eNULL:!MEDIUM:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS:!RC4:!SEED'"
ssl_prefer_server_ciphers = yes
ssl_dh = </usr/share/dovecot/dh.pem
2020-06-09 18:52:36 +00:00
auth_mechanisms = plain login
auth_username_format = %n
2019-05-27 20:15:26 +00:00
protocols = \$protocols imap
# Search for valid users in /etc/passwd
userdb {
driver = passwd
}
2019-12-26 09:56:38 +00:00
#Fallback: Use plain old PAM to find user passwords
2019-05-27 20:15:26 +00:00
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
}
}
2019-07-26 12:43:57 +00:00
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/
}
2019-05-27 20:15:26 +00:00
" > /etc/dovecot/dovecot.conf
# If using an old version of Dovecot, remove the ssl_dl line.
case "$(dovecot --version)" in
1|2.1*|2.2*) sed -i '/^ssl_dh/d' /etc/dovecot/dovecot.conf ;;
esac
2019-07-26 12:43:57 +00:00
mkdir /var/lib/dovecot/sieve/
2022-03-10 00:50:55 +00:00
echo "require [\"fileinto\", \"mailbox\"];
2019-07-26 12:43:57 +00:00
if header :contains \"X-Spam-Flag\" \"YES\"
{
fileinto \"Junk\";
2022-03-10 00:50:55 +00:00
}" > /var/lib/dovecot/sieve/default.sieve
2019-07-26 12:43:57 +00:00
grep -q '^vmail:' /etc/passwd || useradd vmail
2019-07-26 12:43:57 +00:00
chown -R vmail:vmail /var/lib/dovecot
sievec /var/lib/dovecot/sieve/default.sieve
echo 'Preparing user authentication...'
2020-06-03 17:43:16 +00:00
grep -q nullok /etc/pam.d/dovecot ||
echo 'auth required pam_unix.so nullok
account required pam_unix.so' >> /etc/pam.d/dovecot
2019-05-27 20:15:26 +00:00
# OpenDKIM
2020-12-07 02:08:30 +00:00
# A lot of the big name email services, like Google, will automatically reject
# as spam unfamiliar and unauthenticated email addresses. As in, the server
# will flatly reject the email, not even delivering it to someone's Spam
# folder.
2019-05-27 20:15:26 +00:00
2020-06-03 17:43:16 +00:00
# OpenDKIM is a way to authenticate your email so you can send to such services
# without a problem.
2019-05-27 20:15:26 +00:00
2020-06-03 17:43:16 +00:00
# Create an OpenDKIM key in the proper place with proper permissions.
echo 'Generating OpenDKIM keys...'
mkdir -p "/etc/postfix/dkim/$domain"
opendkim-genkey -D "/etc/postfix/dkim/$domain" -d "$domain" -s "$subdom"
chgrp -R opendkim /etc/postfix/dkim/*
chmod -R g+r /etc/postfix/dkim/*
2019-05-27 20:15:26 +00:00
# Generate the OpenDKIM info:
echo 'Configuring OpenDKIM...'
2020-06-20 17:02:01 +00:00
grep -q "$domain" /etc/postfix/dkim/keytable 2>/dev/null ||
2022-10-09 19:28:43 +00:00
echo "$subdom._domainkey.$domain $domain:$subdom:/etc/postfix/dkim/$domain/$subdom.private" >> /etc/postfix/dkim/keytable
2019-05-27 20:15:26 +00:00
2020-06-20 17:02:01 +00:00
grep -q "$domain" /etc/postfix/dkim/signingtable 2>/dev/null ||
2019-05-27 20:15:26 +00:00
echo "*@$domain $subdom._domainkey.$domain" >> /etc/postfix/dkim/signingtable
grep -q '127.0.0.1' /etc/postfix/dkim/trustedhosts 2>/dev/null ||
echo '127.0.0.1
10.1.0.0/16' >> /etc/postfix/dkim/trustedhosts
2019-05-27 20:15:26 +00:00
# ...and source it from opendkim.conf
grep -q '^KeyTable' /etc/opendkim.conf 2>/dev/null || echo 'KeyTable file:/etc/postfix/dkim/keytable
2019-05-27 20:15:26 +00:00
SigningTable refile:/etc/postfix/dkim/signingtable
InternalHosts refile:/etc/postfix/dkim/trustedhosts' >> /etc/opendkim.conf
2019-05-27 20:15:26 +00:00
sed -i '/^#Canonicalization/s/simple/relaxed\/simple/' /etc/opendkim.conf
sed -i '/^#Canonicalization/s/^#//' /etc/opendkim.conf
2021-09-14 23:34:54 +00:00
sed -i '/Socket/s/^#*/#/' /etc/opendkim.conf
grep -q '^Socket\s*inet:12301@localhost' /etc/opendkim.conf || echo 'Socket inet:12301@localhost' >> /etc/opendkim.conf
2019-05-27 20:15:26 +00:00
# OpenDKIM daemon settings, removing previously activated socket.
2022-03-10 00:50:55 +00:00
sed -i '/^SOCKET/d' /etc/default/opendkim && echo "SOCKET=\"inet:12301@localhost\"" >> /etc/default/opendkim
2019-05-27 20:15:26 +00:00
# Here we add to postconf the needed settings for working with OpenDKIM
echo 'Configuring Postfix with OpenDKIM settings...'
postconf -e 'smtpd_sasl_security_options = noanonymous, noplaintext'
postconf -e 'smtpd_sasl_tls_security_options = noanonymous'
2023-03-01 11:01:20 +00:00
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'
2022-09-28 13:36:26 +00:00
# A fix for "Opendkim won't start: can't open PID file?", as specified here: https://serverfault.com/a/847442
/lib/opendkim/opendkim.service.generate
systemctl daemon-reload
# Enable fail2ban security for dovecot and postfix.
[ ! -f /etc/fail2ban/jail.d/emailwiz.local ] && echo "[postfix]
enabled = true
[postfix-sasl]
enabled = true
[sieve]
enabled = true
[dovecot]
enabled = true" > /etc/fail2ban/jail.d/emailwiz.local
2023-03-02 15:07:04 +00:00
# Enable SpamAssassin update cronjob.
sed -i "s|^CRON=0|CRON=1|" /etc/default/spamassassin
for x in spamassassin opendkim dovecot postfix fail2ban; do
2020-06-03 17:43:16 +00:00
printf "Restarting %s..." "$x"
service "$x" restart && printf " ...done\\n"
2022-07-20 17:33:10 +00:00
systemctl enable "$x"
2020-06-03 17:43:16 +00:00
done
2019-05-27 20:15:26 +00:00
pval="$(tr -d '\n' <"/etc/postfix/dkim/$domain/$subdom.txt" | sed "s/k=rsa.* \"p=/k=rsa; p=/;s/\"\s*\"//;s/\"\s*).*//" | grep -o 'p=.*')"
2020-06-22 15:12:50 +00:00
dkimentry="$subdom._domainkey.$domain TXT v=DKIM1; k=rsa; $pval"
2021-02-03 14:05:59 +00:00
dmarcentry="_dmarc.$domain TXT v=DMARC1; p=reject; rua=mailto:dmarc@$domain; fo=1"
2021-08-06 11:19:51 +00:00
spfentry="$domain TXT v=spf1 mx a:$maildomain -all"
mxentry="$domain MX 10 $maildomain 300"
2020-06-20 19:43:27 +00:00
useradd -m -G mail dmarc
# Create a cronjob that deletes month-old dmarc feedback:
cat <<EOF > /etc/cron.weekly/dmarc-clean
#!/bin/sh
find /home/dmarc/Mail -type f -mtime +30 -name '*.mail*' -delete
EOF
chmod 755 /etc/cron.weekly/dmarc-clean
2022-07-21 17:28:25 +00:00
grep -q '^deploy-hook = echo "$RENEWED_DOMAINS" | grep -q' /etc/letsencrypt/cli.ini ||
2022-07-01 23:15:26 +00:00
echo "
2022-07-21 17:42:26 +00:00
deploy-hook = echo \"\$RENEWED_DOMAINS\" | grep -q '$maildomain' && service postfix reload && service dovecot reload" >> /etc/letsencrypt/cli.ini
2022-07-01 23:15:26 +00:00
echo "NOTE: Elements in the entries might appear in a different order in your registrar's DNS settings.
$dkimentry
2020-06-20 20:15:28 +00:00
$dmarcentry
$spfentry
$mxentry" > "$HOME/dns_emailwizard"
2020-06-20 19:43:27 +00:00
2021-01-07 02:51:41 +00:00
printf "\033[31m
2020-06-20 20:15:28 +00:00
_ _
2020-06-20 19:43:27 +00:00
| \ | | _____ ___
| \| |/ _ \ \ /\ / (_)
| |\ | (_) \ V V / _
2021-01-07 02:51:41 +00:00
|_| \_|\___/ \_/\_/ (_)\033[0m
2020-06-20 20:15:28 +00:00
2020-06-20 19:43:27 +00:00
Add these three records to your DNS TXT records on either your registrar's site
or your DNS server:
2021-01-07 02:51:41 +00:00
\033[32m
2020-06-20 19:43:27 +00:00
$dkimentry
2020-06-20 19:46:17 +00:00
2020-06-20 19:43:27 +00:00
$dmarcentry
2020-06-20 19:46:17 +00:00
2020-06-20 19:43:27 +00:00
$spfentry
$mxentry
2021-01-07 02:51:41 +00:00
\033[0m
2020-06-20 20:15:28 +00:00
NOTE: You may need to omit the \`.$domain\` portion at the beginning if
inputting them in a registrar's web interface.
2021-01-07 02:51:41 +00:00
Also, these are now saved to \033[34m~/dns_emailwizard\033[0m in case you want them in a file.
2020-06-20 19:43:27 +00:00
2020-06-20 20:15:28 +00:00
Once you do that, you're done! Check the README for how to add users/accounts
and how to log in.\n"