wiki'd

by JoKeru

Mail Server - Postfix with Virtual Domains & Users (MySQL)

$ apt-get install postfix postfix-mysql mailutils
# @ Postfix Configuration - No configuration

$ echo 'mail.jokeru.ro' > /etc/mailname

$ vi /etc/postfix/main.cf

$ vi /etc/aliases
$ newaliases

$ vi /etc/postfix/mysql-virtual_domains.cf
$ vi /etc/postfix/mysql-virtual_mailboxes.cf
$ vi /etc/postfix/mysql-virtual_forwardings.cf
$ vi /etc/postfix/mysql-virtual_email2email.cf
$ chmod o= /etc/postfix/mysql-virtual_*.cf
$ chgrp postfix /etc/postfix/mysql-virtual_*.cf

$ mysql -uroot -p
mysql> CREATE DATABASE mail;
mysql> GRANT SELECT ON mail.* TO 'mail'@'localhost' IDENTIFIED BY 'p1nky';
mysql> USE mail;
mysql> CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain)) TYPE=MyISAM;
mysql> CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source)) TYPE=MyISAM;
mysql> CREATE TABLE users (email varchar(80) NOT NULL, password varchar(20) NOT NULL, PRIMARY KEY (email)) TYPE=MyISAM;
mysql> INSERT INTO mail.domains (`domain`) VALUES ('jokeru.ro');
mysql> INSERT INTO mail.users (`email` ,`password`) VALUES ('wiki@jokeru.ro', ENCRYPT('w1k1'));

$ groupadd -g 5000 vmail
$ useradd -g vmail -u 5000 vmail -d /home/vmail -m

$ cd /etc/postfix/
$ openssl req -new -outform PEM -out smtpd.cert -newkey rsa:2048 -nodes -keyout smtpd.key -keyform PEM -days 3650 -x509
$ chmod o= smtpd.key

$ service postfix restart
$ netstat -tulnp | grep :25
$ echo "hello world" | mail -s "welcome" root
$ tail /var/log/mail.log

$ vi /etc/bind/zone.jokeru.ro
$ service bind9 reload
$ dig mx jokeru.ro +short
$ dig mail.jokeru.ro +short
$ dig txt jokeru.ro +short

/etc/postfix/main.cf

# See /usr/share/postfix/main.cf.dist for a commented, more complete version

smtpd_banner = $myhostname ESMTP $mail_name - by JoKeru
#always_bcc = wiki@jokeru.ro

# mail server name
myhostname = mail.jokeru.ro
# allow sending emails only from localhost without auth
mynetworks = 127.0.0.0/8
# the domain appended to email from this machine
myorigin = mail.jokeru.ro
# local domains
mydestination = mail.jokeru.ro, localhost, localhost.jokeru.ro
# bind process to interface
inet_interfaces = 10.20.30.40

# various settings
append_dot_mydomain = no
recipient_delimiter = +
message_size_limit = 26214400

# local aliasing
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases

# virtual domains list
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf
# location of the virtual mailbox folders
virtual_mailbox_base = /home/vmail
# the mailbox location for each virtual user
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf
# 50 x 1024 x 1024 Bytes = 50 MB virtual mailbox limit
virtual_mailbox_limit = 52428800
# user and group for virtual mailbox folders
virtual_minimum_uid = 100
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
# virtual aliasing
virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtualforwardings.cf,mysql:/etc/postfix/mysql-virtual_email2email.cf

# TLS (Transport Layer Security) will setup a SSL encrypted connection
# between the server and other servers or email clients so that the
# emails transmitted are secured from eavesdroppers.
smtpd_tls_security_level = may
smtpd_tls_auth_only = yes
smtpd_tls_key_file = /etc/postfix/smtpd.key
smtpd_tls_cert_file = /etc/postfix/smtpd.cert
smtpd_tls_loglevel = 1
#smtpd_tls_received_header = yes

proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $mynetworks

# restrictions / spam check
smtpd_helo_required = yes
disable_vrfy_command = yes
smtpd_delay_reject = yes
delay_warning_time = 1h
# Requirements for the HELO statement
smtpd_helo_restrictions =
 permit_mynetworks,
 permit_sasl_authenticated,
 permit
# Requirements for the Connecting Server
smtpd_client_restrictions =
 reject_rbl_client sbl.spamhaus.org,
 reject_rbl_client blackholes.easynet.nl,
 reject_rbl_client dnsbl.njabl.org
# Requirements for the Sender details  
smtpd_sender_restrictions =
 permit_mynetworks,
 permit_sasl_authenticated,
 warn_if_reject reject_non_fqdn_sender,
 reject_unknown_sender_domain,
 reject_unauth_pipelining,
 permit
# Requirement for the Recipient address
smtpd_recipient_restrictions =
 reject_unauth_pipelining,
 permit_mynetworks,
 permit_sasl_authenticated,
 reject_non_fqdn_recipient,
 reject_unknown_recipient_domain,
 reject_unauth_destination,
 permit
# Requirement for the Data content
smtpd_data_restrictions =
 reject_unauth_pipelining

/etc/aliases

webmaster: root
hostmaster: root
postmaster: root
root: wiki@jokeru.ro

/etc/postfix/mysql-virtual_domains.cf

user = mail
password = p1nky
dbname = mail
query = SELECT domain AS virtual FROM domains WHERE domain='%s'
hosts = 127.0.0.1

/etc/postfix/mysql-virtual_email2email.cf

user = mail
password = p1nky
dbname = mail
query = SELECT email FROM users WHERE email='%s'
hosts = 127.0.0.1

/etc/postfix/mysql-virtual_forwardings.cf

user = mail
password = p1nky
dbname = mail
query = SELECT destination FROM forwardings WHERE source='%s'
hosts = 127.0.0.1

/etc/postfix/mysql-virtual_mailboxes.cf

user = mail
password = p1nky
dbname = mail
query = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM users WHERE email='%s'  
hosts = 127.0.0.1

/etc/bind/zone.jokeru.ro

.....  
@ IN MX 10 mail

mail IN A 10.20.30.40

; Sender Policy Framework - SPF  
@ IN TXT "v=spf1 a mx ip4:10.20.30.40/32 -all"

source: http://www.howtoforge.com/virtual-users-domains-postfix-courier-mysql-squirrelmail-ubuntu-10.04

Comments