654 lines
24 KiB
Bash
654 lines
24 KiB
Bash
#!/bin/sh
|
|
|
|
## FORK OF https://github.com/LukeSmithxyz/emailwiz
|
|
|
|
# BEFORE INSTALLING
|
|
|
|
# Have a Debian or Ubuntu server with a static IP and DNS records (usually
|
|
# A/AAAA) that point your domain name to it.
|
|
|
|
# NOTE WHILE INSTALLING
|
|
|
|
# On installation of Postfix, select "Internet Site" and put in TLD (without
|
|
# `mail.` before it).
|
|
|
|
# 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-ldap dovecot-lmtpd postfix-ldap dovecot-sieve opendkim opendkim-tools spamassassin spamc net-tools fail2ban whiptail bind9-host
|
|
|
|
LDAPHOST="$(whiptail --title "LDAP Host" --inputbox "Please input the fully qualified domain name of your LDAP host" 10 60 example.org 3>&1 1>&2 2>&3)"
|
|
LDAPPORT="$(whiptail --title "LDAP Port" --inputbox "Please input the port your LDAP server is running on" 10 60 636 3>&1 1>&2 2>&3)"
|
|
LDAPBASE="$(whiptail --title "LDAP Base" --inputbox "Please input the LDAP base for your LDAP server (where users are located)" 10 60 ou=people,dc=example,dc=org 3>&1 1>&2 2>&3)"
|
|
LDAPDN="$(whiptail --title "LDAP DN" --inputbox "Please input the Distinguished Name (DN) of the account that you would like to be used to log into the LDAP server" 10 60 cn=overwatch,ou=services,dc=example,dc=org 3>&1 1>&2 2>&3)"
|
|
LDAPPASS="$(whiptail --title "LDAP DN" --passwordbox "Please input the password for the Distinguised Name you provided" 10 60 3>&1 1>&2 2>&3)"
|
|
LDAPPASSCHECK="$(whiptail --title "LDAP DN" --passwordbox "Please repeat the password" 10 60 3>&1 1>&2 2>&3)"
|
|
if [ "$LDAPPASS" != "$LDAPPASSCHECK" ]
|
|
then
|
|
echo "Passwords did not match! Exiting!"
|
|
exit 1
|
|
fi
|
|
LDAPTLS="no"
|
|
whiptail --title "LDAP TLS" --yesno "Will you be using TLS on this LDAP server?" 7 60
|
|
result=$?
|
|
if [ $result -eq 0 ]
|
|
then
|
|
LDAPTLS="yes"
|
|
fi
|
|
|
|
domain="$(cat /etc/mailname)"
|
|
subdom=${MAIL_SUBDOM:-mail}
|
|
maildomain="$domain"
|
|
certdir="/etc/letsencrypt/live/$maildomain"
|
|
|
|
# Preliminary record checks
|
|
ipv4=$(host "$domain" | grep -m1 -Eo '([0-9]+\.){3}[0-9]+')
|
|
[ -z "$ipv4" ] && echo "\033[0;31mPlease point your domain ("$domain") to your server's ipv4 address." && exit 1
|
|
ipv6=$(host "$domain" | grep "IPv6" | awk '{print $NF}')
|
|
[ -z "$ipv6" ] && echo "\033[0;31mPlease point your domain ("$domain") to your server's ipv6 address." && exit 1
|
|
|
|
# Open required mail ports, and 80, for Certbot.
|
|
for port in 80 993 465 25 587; do
|
|
ufw allow "$port" 2>/dev/null
|
|
done
|
|
|
|
[ ! -d "$certdir" ] &&
|
|
possiblecert="$(certbot certificates 2>/dev/null | grep "Domains:\.* \(\*\.$domain\|$maildomain\)\(\s\|$\)" -A 2 | awk '/Certificate Path/ {print $3}' | head -n1)" &&
|
|
certdir="${possiblecert%/*}"
|
|
|
|
[ ! -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
|
|
;;
|
|
esac
|
|
|
|
[ ! -d "$certdir" ] && echo "Error locating or installing SSL certificate." && exit 1
|
|
|
|
echo "Configuring Postfix's main.cf..."
|
|
|
|
# Adding additional vars to fix an issue with receiving emails (relay access denied) and adding it to mydestination.
|
|
postconf -e "myhostname = $maildomain"
|
|
postconf -e "mail_name = $domain" #This is for the smtpd_banner
|
|
postconf -e "mydomain = $domain"
|
|
postconf -e 'mydestination = $myhostname, $mydomain, mail, localhost.localdomain, localhost, localhost.$mydomain'
|
|
|
|
# Change the cert/key files to the default locations of the Let's Encrypt cert/key
|
|
postconf -e "smtpd_tls_key_file=$certdir/privkey.pem"
|
|
postconf -e "smtpd_tls_cert_file=$certdir/fullchain.pem"
|
|
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'
|
|
|
|
# TLS required for authentication.
|
|
postconf -e 'smtpd_tls_auth_only = yes'
|
|
|
|
# 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'
|
|
|
|
# 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'
|
|
|
|
# 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'
|
|
|
|
# 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'
|
|
postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination, reject_unknown_recipient_domain'
|
|
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'
|
|
|
|
# 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 = Maildir/"
|
|
# UNIX zoomer-larping-as-boomer here, we're going full spool
|
|
|
|
# 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"
|
|
|
|
# Prevent postfix from rejecting non-UNIX accounts, we use LDAP so this is necessary
|
|
postconf -e "local_recipient_maps = "
|
|
|
|
#Set up virtual mail with postfix
|
|
# Copied mostly from : https://gist.github.com/sahil87/fe2569f472cccaa1c277aefc2c2ff245
|
|
postconf -e "local_transport = virtual "
|
|
postconf -e "local_recipient_maps = $virtual_mailbox_maps"
|
|
postconf -e "virtual_mailbox_base = /var/vmail/"
|
|
|
|
### SET UP POSTFIX VIRTUAL ALIAS MAPS (The actual list of users)
|
|
postconf -e "virtual_alias_maps = ldap:/etc/postfix/ldap-aliases.cf"
|
|
echo "
|
|
version = 3
|
|
server_host = ldaps://$LDAPHOST
|
|
search_base = $LDAPBASE
|
|
query_filter = (&(objectClass=*)(mail=%s))
|
|
result_format = %s
|
|
result_attribute = mail
|
|
bind = yes
|
|
bind_dn = $LDAPDN
|
|
bind_pw = $LDAPPASS
|
|
" > /etc/postfix/ldap-aliases.cf
|
|
|
|
### SET UP POSTFIX VIRTUAL ACCOUNT MAP (The actual list of locations for the user's mail
|
|
postconf -e "virtual_mailbox_maps = ldap:/etc/postfix/ldap-accountsmap.cf"
|
|
echo "
|
|
version = 3
|
|
server_host = ldaps://$LDAPHOST
|
|
search_base = $LDAPBASE
|
|
query_filter = (&(objectClass=*)(mail=%s))
|
|
result_format = %s/Inbox/
|
|
result_attribute = uid
|
|
bind = yes
|
|
bind_dn = $LDAPDN
|
|
bind_pw = $LDAPPASS
|
|
" > /etc/postfix/ldap-accountsmap.cf
|
|
|
|
postconf -e "virtual_minimum_uid = 100"
|
|
|
|
postconf -e "virtual_mailbox_domains = $maildomain"
|
|
|
|
# Tell postfix to use LMTP for delivery https://wiki.dovecot.org/HowTo/PostfixDovecotLMTP
|
|
postconf -e "virtual_transport = lmtp:unix:private/dovecot-lmtp"
|
|
postconf -e "mailbox_transport = lmtp:unix:private/dovecot-lmtp"
|
|
|
|
# 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
|
|
|
|
# master.cf
|
|
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_tls_auth_only=yes
|
|
-o smtpd_enforce_tls=yes
|
|
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
|
|
-o smtpd_sender_restrictions=reject_sender_login_mismatch
|
|
-o smtpd_sender_login_maps=pcre:/etc/postfix/login_maps.pcre
|
|
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject_unauth_destination
|
|
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
|
|
|
|
# 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.
|
|
mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.backup.conf
|
|
|
|
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
|
|
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
|
|
auth_mechanisms = plain login
|
|
auth_username_format = %n
|
|
|
|
protocols = \$protocols imap lmtp
|
|
|
|
# static settings generated from template <doc/wiki/UserDatabase.Static.txt>
|
|
userdb static {
|
|
driver = static
|
|
# Template for the fields. Can return anything a userdb could normally
|
|
# return. For example:
|
|
args = uid=vmail gid=vmail home=/var/vmail/%u allow_all_users=yes
|
|
# args = uid=500 gid=500 home=/var/mail/%u
|
|
#
|
|
# If you use deliver, it needs to look up users only from the userdb. This
|
|
# of course doesn't work with static because there is no list of users.
|
|
# Normally static userdb handles this by doing a passdb lookup. This works
|
|
# with most passdbs, with PAM being the most notable exception. If you do
|
|
# the user verification another way, you can add allow_all_users=yes to
|
|
# the args in which case the passdb lookup is skipped.
|
|
#
|
|
#args =
|
|
}
|
|
|
|
# Use LDAP config as configured at /etc/dovecot/dovecot-ldap.conf.ext
|
|
#LDAP Usernames
|
|
userdb ldap {
|
|
# Path for LDAP configuration file, see doc/dovecot-ldap-example.conf
|
|
driver = ldap
|
|
args = /etc/dovecot/dovecot-ldap.conf.ext
|
|
}
|
|
#LDAP Passwords
|
|
passdb ldap {
|
|
driver = ldap
|
|
args = /etc/dovecot/dovecot-ldap.conf.ext
|
|
}
|
|
|
|
# vmail will store our user's mail, as their LDAP UID as well.
|
|
# The LAYOUT option is also important because otherwise, the boxes will be \`.Sent\` instead of \`Sent\`.
|
|
mail_location = maildir:/var/vmail/%u:INBOX=/var/vmail/%u/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
|
|
}
|
|
|
|
#here, we let postfix use dovecot for mail sorting/storage
|
|
service lmtp {
|
|
unix_listener /var/spool/postfix/private/dovecot-lmtp {
|
|
mode = 0660
|
|
user = postfix
|
|
group = postfix
|
|
}
|
|
}
|
|
|
|
|
|
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
|
|
|
|
# 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
|
|
|
|
mkdir /var/lib/dovecot/sieve/
|
|
|
|
echo "require [\"fileinto\", \"mailbox\"];
|
|
if header :contains \"X-Spam-Flag\" \"YES\"
|
|
{
|
|
fileinto \"Junk\";
|
|
}" > /var/lib/dovecot/sieve/default.sieve
|
|
|
|
# Create virtual mailbox user
|
|
grep -q '^vmail:' /etc/passwd || useradd vmail
|
|
usermod -aG vmail postfix
|
|
usermod -aG vmail dovecot
|
|
vmailUID=$(id -u vmail)
|
|
vmailGID=$(id -g vmail)
|
|
postconf -e "virtual_uid_maps = static:$vmailUID"
|
|
postconf -e "virtual_gid_maps = static:$vmailGID"
|
|
mkdir /var/vmail
|
|
chmod -R 2770 /var/vmail
|
|
chmod -R +x /var/vmail
|
|
chown vmail:vmail /var/vmail
|
|
chown -R vmail:vmail /var/lib/dovecot
|
|
sievec /var/lib/dovecot/sieve/default.sieve
|
|
|
|
echo "Creating Dovecot-LDAP config..."
|
|
echo "# This file is commonly accessed via passdb {} or userdb {} section in
|
|
# conf.d/auth-ldap.conf.ext
|
|
|
|
# This file is opened as root, so it should be owned by root and mode 0600.
|
|
#
|
|
# http://wiki2.dovecot.org/AuthDatabase/LDAP
|
|
#
|
|
# NOTE: If you're not using authentication binds, you'll need to give
|
|
# dovecot-auth read access to userPassword field in the LDAP server.
|
|
# With OpenLDAP this is done by modifying /etc/ldap/slapd.conf. There should
|
|
# already be something like this:
|
|
|
|
# access to attribute=userPassword
|
|
# by dn=\"<dovecot's dn>\" read # add this
|
|
# by anonymous auth
|
|
# by self write
|
|
# by * none
|
|
|
|
# Space separated list of LDAP hosts to use. host:port is allowed too.
|
|
#hosts = $LDAPHOST:$LDAPPORT
|
|
|
|
# LDAP URIs to use. You can use this instead of hosts list. Note that this
|
|
# setting isnt supported by all LDAP libraries.
|
|
uris = ldaps://$LDAPHOST
|
|
|
|
# Distinguished Name - the username used to login to the LDAP server.
|
|
# Leave it commented out to bind anonymously (useful with auth_bind=yes).
|
|
dn = $LDAPDN
|
|
|
|
# Password for LDAP server, if dn is specified.
|
|
dnpass = $LDAPPASS
|
|
|
|
# Use SASL binding instead of the simple binding. Note that this changes
|
|
# ldap_version automatically to be 3 if it's lower.
|
|
#sasl_bind = no
|
|
# SASL mechanism name to use.
|
|
#sasl_mech =
|
|
# SASL realm to use.
|
|
#sasl_realm =
|
|
# SASL authorization ID, ie. the dnpass is for this "master user", but the
|
|
# dn is still the logged in user. Normally you want to keep this empty.
|
|
#sasl_authz_id =
|
|
|
|
# Use TLS to connect to the LDAP server.
|
|
#tls = $LDAPTLS
|
|
# TLS options, currently supported only with OpenLDAP:
|
|
#tls_ca_cert_file =
|
|
#tls_ca_cert_dir =
|
|
#tls_cipher_suite =
|
|
# TLS cert/key is used only if LDAP server requires a client certificate.
|
|
#tls_cert_file =
|
|
#tls_key_file =
|
|
# Valid values: never, hard, demand, allow, try
|
|
tls_require_cert = demand
|
|
|
|
# Use the given ldaprc path.
|
|
#ldaprc_path =
|
|
|
|
# LDAP library debug level as specified by LDAP_DEBUG_* in ldap_log.h.
|
|
# -1 = everything. You may need to recompile OpenLDAP with debugging enabled
|
|
# to get enough output.
|
|
#debug_level = 0
|
|
|
|
# Use authentication binding for verifying password's validity. This works by
|
|
# logging into LDAP server using the username and password given by client.
|
|
# The pass_filter is used to find the DN for the user. Note that the pass_attrs
|
|
# is still used, only the password field is ignored in it. Before doing any
|
|
# search, the binding is switched back to the default DN.
|
|
auth_bind = yes
|
|
|
|
# If authentication binding is used, you can save one LDAP request per login
|
|
# if users' DN can be specified with a common template. The template can use
|
|
# the standard %variables (see user_filter). Note that you can't
|
|
# use any pass_attrs if you use this setting.
|
|
#
|
|
# If you use this setting, it's a good idea to use a different
|
|
# dovecot-ldap.conf.ext for userdb (it can even be a symlink, just as long as
|
|
# the filename is different in userdb's args). That way one connection is used
|
|
# only for LDAP binds and another connection is used for user lookups.
|
|
# Otherwise the binding is changed to the default DN before each user lookup.
|
|
#
|
|
# For example:
|
|
# auth_bind_userdn = cn=%u,ou=people,o=org
|
|
#
|
|
#auth_bind_userdn =
|
|
|
|
# LDAP protocol version to use. Likely 2 or 3.
|
|
#ldap_version = 3
|
|
|
|
# LDAP base. %variables can be used here.
|
|
# For example: dc=mail, dc=example, dc=org
|
|
base = $LDAPBASE
|
|
|
|
# Dereference: never, searching, finding, always
|
|
#deref = never
|
|
|
|
# Search scope: base, onelevel, subtree
|
|
scope = subtree
|
|
|
|
# User attributes are given in LDAP-name=dovecot-internal-name list. The
|
|
# internal names are:
|
|
# uid - System UID
|
|
# gid - System GID
|
|
# home - Home directory
|
|
# mail - Mail location
|
|
#
|
|
# There are also other special fields which can be returned, see
|
|
# http://wiki2.dovecot.org/UserDatabase/ExtraFields
|
|
user_attrs = homeDirectory=home,uidNumber=uid,gidNumber=gid
|
|
|
|
# Filter for user lookup. Some variables can be used (see
|
|
# http://wiki2.dovecot.org/Variables for full list):
|
|
# %u - username
|
|
# %n - user part in user@domain, same as %u if there's no domain
|
|
# %d - domain part in user@domain, empty if user there's no domain
|
|
#user_filter = (&(objectClass=posixAccount)(uid=%u))
|
|
|
|
# Password checking attributes:
|
|
# user: Virtual user name (user@domain), if you wish to change the
|
|
# user-given username to something else
|
|
# password: Password, may optionally start with {type}, eg. {crypt}
|
|
# There are also other special fields which can be returned, see
|
|
# http://wiki2.dovecot.org/PasswordDatabase/ExtraFields
|
|
#pass_attrs = uid=user,userPassword=password
|
|
pass_attrs = uid=user
|
|
|
|
# If you wish to avoid two LDAP lookups (passdb + userdb), you can use
|
|
# userdb prefetch instead of userdb ldap in dovecot.conf. In that case you'll
|
|
# also have to include user_attrs in pass_attrs field prefixed with "userdb_"
|
|
# string. For example:
|
|
#pass_attrs = uid=user,userPassword=password,\
|
|
# homeDirectory=userdb_home,uidNumber=userdb_uid,gidNumber=userdb_gid
|
|
|
|
# Filter for password lookups
|
|
pass_filter = (&(objectClass=posixAccount)(uid=%u))
|
|
|
|
# Attributes and filter to get a list of all users
|
|
#iterate_attrs = uid=user
|
|
#iterate_filter = (objectClass=posixAccount)
|
|
|
|
# Default password scheme. "{scheme}" before password overrides this.
|
|
# List of supported schemes is in: http://wiki2.dovecot.org/Authentication
|
|
default_pass_scheme = SHA
|
|
|
|
# By default all LDAP lookups are performed by the auth master process.
|
|
# If blocking=yes, auth worker processes are used to perform the lookups.
|
|
# Each auth worker process creates its own LDAP connection so this can
|
|
# increase parallelism. With blocking=no the auth master process can
|
|
# keep 8 requests pipelined for the LDAP connection, while with blocking=yes
|
|
# each connection has a maximum of 1 request running. For small systems the
|
|
# blocking=no is sufficient and uses less resources.
|
|
#blocking = no
|
|
" > /etc/dovecot/dovecot-ldap.conf.ext
|
|
|
|
|
|
#echo 'Preparing user authentication...'
|
|
#grep -q nullok /etc/pam.d/dovecot ||
|
|
#echo 'auth required pam_unix.so nullok
|
|
#account required pam_unix.so' >> /etc/pam.d/dovecot
|
|
|
|
# OpenDKIM
|
|
|
|
# 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.
|
|
|
|
# OpenDKIM is a way to authenticate your email so you can send to such services
|
|
# without a problem.
|
|
|
|
# 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"
|
|
chgrp -R opendkim /etc/postfix/dkim/*
|
|
chmod -R g+r /etc/postfix/dkim/*
|
|
|
|
# Generate the OpenDKIM info:
|
|
echo 'Configuring OpenDKIM...'
|
|
grep -q "$domain" /etc/postfix/dkim/keytable 2>/dev/null ||
|
|
echo "default._domainkey.$domain $domain:default:/etc/postfix/dkim/$domain/default.private" >> /etc/postfix/dkim/keytable
|
|
|
|
grep -q "$domain" /etc/postfix/dkim/signingtable 2>/dev/null ||
|
|
echo "*@$domain default._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
|
|
|
|
# ...and source it from opendkim.conf
|
|
grep -q '^KeyTable' /etc/opendkim.conf 2>/dev/null || 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 -i '/Socket/s/^#*/#/' /etc/opendkim.conf
|
|
grep -q '^Socket\s*inet:12301@localhost' /etc/opendkim.conf || echo 'Socket inet:12301@localhost' >> /etc/opendkim.conf
|
|
|
|
# OpenDKIM daemon settings, removing previously activated socket.
|
|
sed -i '/^SOCKET/d' /etc/default/opendkim && echo "SOCKET=\"inet:12301@localhost\"" >> /etc/default/opendkim
|
|
|
|
# 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'
|
|
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'
|
|
|
|
# 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
|
|
|
|
# Enable SpamAssassin update cronjob.
|
|
sed -i "s|^CRON=0|CRON=1|" /etc/default/spamassassin
|
|
|
|
for x in spamassassin opendkim dovecot postfix fail2ban; do
|
|
printf "Restarting %s..." "$x"
|
|
service "$x" restart && printf " ...done\\n"
|
|
systemctl enable "$x"
|
|
done
|
|
|
|
pval="$(tr -d '\n' <"/etc/postfix/dkim/$domain/default.txt" | sed "s/k=rsa.* \"p=/k=rsa; p=/;s/\"\s*\"//;s/\"\s*).*//" | grep -o 'p=.*')"
|
|
dkimentry="default._domainkey.$domain TXT v=DKIM1; k=rsa; $pval"
|
|
dmarcentry="_dmarc.$domain TXT v=DMARC1; p=reject; rua=mailto:dmarc@$domain; fo=1"
|
|
spfentry="$domain TXT v=spf1 mx a:$maildomain -all"
|
|
mxentry="$domain MX 10 $maildomain 300"
|
|
|
|
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 >/dev/null 2>&1
|
|
exit 0
|
|
EOF
|
|
chmod 755 /etc/cron.weekly/dmarc-clean
|
|
|
|
grep -q '^deploy-hook = echo "$RENEWED_DOMAINS" | grep -q' /etc/letsencrypt/cli.ini ||
|
|
echo "
|
|
deploy-hook = echo \"\$RENEWED_DOMAINS\" | grep -q '$maildomain' && service postfix reload && service dovecot reload" >> /etc/letsencrypt/cli.ini
|
|
|
|
echo "NOTE: Elements in the entries might appear in a different order in your registrar's DNS settings.
|
|
$dkimentry
|
|
$dmarcentry
|
|
$spfentry
|
|
$mxentry" > "$HOME/dns_emailwizard"
|
|
|
|
printf "\033[31m
|
|
_ _
|
|
| \ | | _____ ___
|
|
| \| |/ _ \ \ /\ / (_)
|
|
| |\ | (_) \ V V / _
|
|
|_| \_|\___/ \_/\_/ (_)\033[0m
|
|
|
|
Add these three records to your DNS TXT records on either your registrar's site
|
|
or your DNS server:
|
|
\033[32m
|
|
$dkimentry
|
|
Note: some name registrars will merely want default._domainkey. Namecheap is one of them.
|
|
|
|
$dmarcentry
|
|
|
|
$spfentry
|
|
|
|
$mxentry
|
|
\033[0m
|
|
NOTE: You may need to omit the \`.$domain\` portion at the beginning if
|
|
inputting them in a registrar's web interface.
|
|
|
|
Also, these are now saved to \033[34m~/dns_emailwizard\033[0m in case you want them in a file.
|
|
|
|
Once you do that, you're done! Check the README for how to add users/accounts
|
|
and how to log in.\n"
|