Skip to content

Instantly share code, notes, and snippets.

@rahogata
Last active August 19, 2017 06:11
Show Gist options
  • Save rahogata/e6c3fccebdc8067d8111e82ed7156cba to your computer and use it in GitHub Desktop.
Save rahogata/e6c3fccebdc8067d8111e82ed7156cba to your computer and use it in GitHub Desktop.
Install Postfix and minimal setup (not completely automated).
#!/bin/sh
# Name: postfix_setup
# Description: Install and minimal configure postfix.
# Args: $1 -> FQDN
# Pre-requisites:
# Add FQDN as host name in /etc/hosts with format Eg: mail.domain.com
# where mail is your host name and domain.com is your domain name.
# If required change /etc/hostname and reboot.
# Eg. /etc/hosts
# 127.0.1.1 mail.domain.com mail
# Eg. /etc/hostname
# mail
MASTERCONF='/etc/postfix/master.cf'
USAGE() {
echo "`basename $0` {Domain Name}"
exit 1
}
# Name: showProgress
# Description: print . until background process completes.
# Args: $1 -> Opening message
# $2 -> Closing message
showProgress() {
printf "$1"
while : ; do
if [ -z "$(ps -p $! -o pid=)" ] ; then
printf "$2\n"
break
fi
sleep 1
printf "."
done
}
if [ $# -lt 1 ] ; then
USAGE
fi
if [ $(id -u) -ne 0 ] ; then
sudo=sudo
fi
DOMAINNAME="$(echo $1 | cut -d. -f2-)"
$sudo echo
$sudo apt-get update >/dev/null 2>&1 &
showProgress "Updating repositories" "Done."
# Install postfix during which in setup window add proper values.
# Eg.
# System mail name: domain.com
# Root and postmaster mail recipient: user with root access or administration previleges.
# Other destinations: add domain.com to the end.
# Local networks: add local network you are in Eg. 192.168.0.0/24
# Mailbox size: 0
# Extension char: +
# Ineternet protocols: all
$sudo apt-get install -y postfix
# Configure /etc/postfix/main.cf
$sudo postconf -e 'home_mailbox = Maildir/'
$sudo postconf -e 'smtpd_sasl_type = dovecot'
$sudo postconf -e 'smtpd_sasl_path = private/auth'
$sudo postconf -e "smtpd_sasl_local_domain = $DOMAINNAME"
$sudo postconf -e 'smtpd_sasl_security_options = noanonymous'
$sudo postconf -e 'broken_sasl_auth_clients = yes'
$sudo postconf -e 'smtpd_sasl_auth_enable = yes'
$sudo postconf -e 'smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated reject_unauth_destination'
$sudo postconf -e 'smtpd_client_restrictions = permit_mynetworks permit_sasl_authenticated reject_unknown_client_hostname'
$sudo postconf -e 'smtp_tls_security_level = may'
$sudo postconf -e 'smtpd_tls_security_level = may'
$sudo postconf -e 'smtp_tls_note_starttls_offer = yes'
$sudo postconf -e 'smtpd_tls_loglevel = 1'
$sudo postconf -e 'smtpd_tls_received_header = yes'
$sudo postconf -e 'smtpd_helo_required = yes'
$sudo postconf -e 'smtpd_helo_restrictions = permit_mynetworks permit_sasl_authenticated reject_non_fqdn_hostname reject_invalid_hostname'
$sudo postconf -e 'virtual_alias_maps = hash:/etc/postfix/virtual'
# TLS configuration
$sudo openssl genrsa -des3 -out mserver$$.key 4096
$sudo openssl rsa -in mserver$$.key -out mserver$$.key.insecure
$sudo mv mserver$$.key mserver$$.key.secure
$sudo mv mserver$$.key.insecure mserver$$.key
# CN must be domain.com
$sudo openssl req -new -key mserver$$.key -out mserver$$.csr
$sudo openssl x509 -req -days 365 -in mserver$$.csr -signkey mserver$$.key -out mserver$$.crt
$sudo cp mserver$$.crt /etc/ssl/certs
$sudo cp mserver$$.key /etc/ssl/private
$sudo postconf -e "smtpd_tls_cert_file = /etc/ssl/certs/mserver$$.crt"
$sudo postconf -e "smtpd_tls_key_file = /etc/ssl/private/mserver$$.key"
$sudo postconf -e 'smtpd_helo_required = yes'
$sudo postconf -e 'smtpd_helo_restrictions = permit_mynetworks permit_sasl_authenticated reject_non_fqdn_hostname reject_invalid_hostname'
$sudo sed -i '/^# *submission *inet.*$/s/^#//' $MASTERCONF
$sudo sed -i '/^# *smtps *inet.*$/s/^#//' $MASTERCONF
$sudo sed -i '/^#.*postfix\/submission.*$/s/^#//' $MASTERCONF
$sudo sed -i '/^#.*smtpd_tls_security_level *= *encrypt.*$/s/^#//' $MASTERCONF
$sudo sed -i '/^#.*smtpd_sasl_auth_enable *= *yes.*$/s/^#//' $MASTERCONF
$sudo sed -i '/^#.*smtpd_relay_restrictions *=.*$/s/^#//' $MASTERCONF
$sudo sed -i '/^#.*milter_macro_daemon_name *=.*$/s/^#//' $MASTERCONF
$sudo sed -i '/^#.*postfix\/smtps.*$/s/^#//' $MASTERCONF
$sudo sed -i '/^#.*smtpd_tls_wrappermode *=.*$/s/^#//' $MASTERCONF
#Dovecot
$sudo apt-get install -y dovecot-common >/dev/null 2>&1 &
showProgress "Installing dovecot" "Done."
cat << EOF
Configure Dovecot Manually Sorry for trouble...
Uncomment unix_listener /var/spool/postfix/private auth next 2 line below it as well
change it lik this below
#
# Postfix smpt-auth
# unix_listener /var/spool/postfix/private/auth {
# mode = 0660
# user = postfix
# group = postfix
# }
EOF
$sudo sed -i 's/^.*auth_mechanisms *=.*$/auth_mechanisms = plain login/' /etc/dovecot/conf.d/10-auth.conf
$sudo service postfix restart
$sudo service dovecot restart
$sudo apt-get install -y dovecot-imapd dovecot-pop3d >/dev/null 2>&1 &
showProgress "Installing dovecot-imapd, dovecot-pop3d" "Done."
#Configure
$sudo sed -i 's/^mail_location *=.*$/mail_location = maildir:~\/Maildir/' /etc/dovecot/conf.d/10-mail.conf
$sudo sed -i '/^#.*pop3_uidl_format *=.*$/s/^#//' /etc/dovecot/conf.d/20-pop3.conf
## SSL configure dovecot
$sudo sed -i 's/^#.*ssl *=.*$/ssl = yes/' /etc/dovecot/conf.d/10-ssl.conf
$sudo sed -i '/^#.*ssl_cert *=.*$/s/^#//' /etc/dovecot/conf.d/10-ssl.conf
$sudo sed -i '/^#.*ssl_key *=.*$/s/^#//' /etc/dovecot/conf.d/10-ssl.conf
$sudo sed -i "s/@commonName@/$1/" /usr/share/dovecot/dovecot-openssl.cnf
cat << EOF
Generate dovecot ssl certificate manually.
cd into /usr/share/dovecot/ as root user
run mkcert.sh command
Thank You..
EOF
$sudo service dovecot restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment