Friday, September 27, 2013

Postfix Mysql Dovecot Mail Server Installation

Today I will explain how to configure postfix mysql dovecot mailing system with smtp authentication. For this I am using a debian based system.

First start with mysql installation,

apt-get install mysql-server libmysqlclient-dev

Install latest openssl package.
http://www.openssl.org/source/openssl-1.0.1e.tar.gz
tar -zxvf openssl 1.0.1e.tar.gz
cd openssl 1.0.1e
./config shared
make
make install

ld.so.conf
/usr/local/lib
/usr/local/ssl/lib

ldconfig

apt-get install bison flex

Install Linux PAM as a dependecy for cyrus
tar -zxvf Linux-PAM-1.1.7.tar.gz
cd Linux-PAM-1.1.7
./configure
make
make install

Now change the /etc/pam.d/ entries like this otherwise cant login after reboot

@include common-auth                   --> auth include common-auth
@include common-account                --> account include common-account
@include common-password               --> password include common-password
@include common-session                --> session include common-session
@include common-session-noninteractive --> session-noninteractive include common-session-noninteractive

For berkeley-db
wget http://download.oracle.com/berkeley-db/db-6.0.20.tar.gz
tar -zxvf db-6.0.20.tar.gz
cd db-6.0.20
cd build_unix/
../dist/configure --prefix=/usr --enable-compat185 --enable-dbm --disable-static  --enable-cxx
make
make install

Install Cyrus SASL for postfix smtp authentication

wget http://fossies.org/linux/misc/cyrus-sasl-2.1.26.tar.gz
tar -zxvf cyrus-sasl-2.1.26.tar.gz
cd cyrus-sasl-2.1.26
./configure --with-openssl=/usr/local/ssl --with-saslauthd --enable-login --with-pam=yes
make
make install

Now test sasl module support

saslauthd -v
authentication mechanisms should show = pam


Now install sasl auth init script
wget http://www.linuxfromscratch.org/blfs/downloads/svn/blfs-bootscripts-20130908.tar.bz2
tar -jxvf blfs-bootscripts-20130908.tar.bz2
cd blfs-bootscripts-20130908
make install-saslauthd

should install to /etc/rc.d/init.d/saslauthd

Now install mysql plugin

apt-get install g++

wget http://prdownloads.sourceforge.net/pam-mysql/pam_mysql-0.7RC1.tar.gz
tar -zxvf pam_mysql-0.7RC1.tar.gz

cd pam_mysql-0.7RC1
./configure --with-openssl=/usr/local/ssl/ --with-cyrus-sasl2=/usr/local
make
make install

ldconfig

Now configure saslauth

vim /etc/sysconfig/saslauthd
START="yes"    <-- for startup script
AUTHMECH="pam" <-- authentication method
OPTIONS="-r"   <-- for user@domain as username

vim /etc/pam.d/smtp

auth required pam_mysql.so user=postfixadmin passwd=postfixadmin db=postfix table=mailbox usercolumn=username passwdcolumn=password crypt=1 md5=1 debug=1
account sufficient pam_mysql.so user=postfixadmin passwd=postfixadmin db=postfix table=mailbox usercolumn=username passwdcolumn=password crypt=1 md5=1 debug=1

mkdir /var/state

/etc/rc.d/init.d/saslauthd start

check => ps -ef | grep saslauthd

For testing we need to install mysql backend

CREATE DATABASE postfix;
GRANT all ON postfix.* TO postfixadmin@localhost IDENTIFIED BY 'postfixadmin';
USE postfix;

CREATE TABLE admin (
  username varchar(255) NOT NULL default '',
  password varchar(255) NOT NULL default '',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (username),
  KEY username (username)
) COMMENT='Postfix Admin - Virtual Admins';

CREATE TABLE alias (
  address varchar(255) NOT NULL default '',
  goto text NOT NULL,
  domain varchar(255) NOT NULL default '',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (address),
  KEY address (address)
) COMMENT='Postfix Admin - Virtual Aliases';

CREATE TABLE domain (
  domain varchar(255) NOT NULL default '',
  description varchar(255) NOT NULL default '',
  aliases int(10) NOT NULL default '0',
  mailboxes int(10) NOT NULL default '0',
  maxquota int(10) NOT NULL default '0',
  transport varchar(255) default NULL,
  backupmx tinyint(1) NOT NULL default '0',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (domain),
  KEY domain (domain)
) COMMENT='Postfix Admin - Virtual Domains';

CREATE TABLE domain_admins (
  username varchar(255) NOT NULL default '',
  domain varchar(255) NOT NULL default '',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1',
  KEY username (username)
) COMMENT='Postfix Admin - Domain Admins';

CREATE TABLE log (
  timestamp datetime NOT NULL default '0000-00-00 00:00:00',
  username varchar(255) NOT NULL default '',
  domain varchar(255) NOT NULL default '',
  action varchar(255) NOT NULL default '',
  data varchar(255) NOT NULL default '',
  KEY timestamp (timestamp)
) COMMENT='Postfix Admin - Log';

CREATE TABLE mailbox (
  username varchar(255) NOT NULL default '',
  password varchar(255) NOT NULL default '',
  name varchar(255) NOT NULL default '',
  maildir varchar(255) NOT NULL default '',
  quota int(10) NOT NULL default '0',
  domain varchar(255) NOT NULL default '',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (username),
  KEY username (username)
) COMMENT='Postfix Admin - Virtual Mailboxes';

CREATE TABLE vacation (
  email varchar(255) NOT NULL default '',
  subject varchar(255) NOT NULL default '',
  body text NOT NULL,
  cache text NOT NULL,
  domain varchar(255) NOT NULL default '',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (email),
  KEY email (email)
) COMMENT='Postfix Admin - Virtual Vacation';

Now install postfix admin for user creation

apt-get install apache2 php5 libapache2-mod-php5 php5-mysql

visit http://postfixadmin.sourceforge.net/
download and extract to postfixadmin in the root directory

Edit config.inc.php

"configured"=true and give DB credentials

Now point to http://localhost/postfixadmin
Go through the web install..

Now creat a domain example.com and user=test and password=test.pass

Now test sasl
testsaslauthd -u test@example.com -p test.pass -s smtp
0: OK "Success."

openssl symlink
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl

Now configure Postfix

wget http://cdn.postfix.johnriley.me/mirrors/postfix-release/official/postfix-2.10.2.tar.gz

groupadd -g 32 postfix
groupadd -g 35 postdrop
useradd -c "Postfix Daemon User" -d /var/spool/postfix -g postfix -s /bin/false -u 32 postfix

tar -zxvf postfix-2.10.2.tar.gz
cd postfix-2.10.2

For support for berkeley db version 6,
 vim src/util/dict_db.c
change  DB_VERSION_MAJOR == 5       to         DB_VERSION_MAJOR ==6

make CCARGS='-DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/local/include/sasl -DHAS_MYSQL -I/usr/include/mysql -DUSE_TLS -I/usr/local/ssl/include' AUXLIBS='-L/usr/lib -lmysqlclient -lz -lm -L/usr/local/ssl/lib -lssl -lcrypto -L/usr/local/lib -lsasl2' makefiles
make
sh postfix-install -non-interactive (install with default parameters)

Install postfix boot script
wget http://www.linuxfromscratch.org/blfs/downloads/svn/blfs-bootscripts-20130908.tar.bz2
tar -jxvf blfs-bootscripts-20130908.tar.bz2
cd blfs-bootscripts-20130908
make install-saslauthd

should install to /etc/rc.d/init.d/postfix

adduser postfix sasl (For sasl permission)


Configure postfix for sasl and tls support

vim /etc/postfix/main.cf

smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes

smtpd_recipient_restrictions =
   permit_sasl_authenticated,
   permit_mynetworks,
   check_relay_domains
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_tls_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
broken_sasl_auth_clients = yes
smtpd_sasl_path = smtpd
smtpd_sasl_type = cyrus
smtpd_sasl_authenticated_header = yes

For postfix - sasl connect

mkdir /etc/sasl2/
vim /etc/sasl2/smtpd.conf

pwcheck_method: saslauthd
mech_list: PLAIN LOGIN
saslauthd_path: /var/state/saslauthd/mux
log_level: 3

/etc/rc.d/init.d/postfix start

Now Test SMTP authentication ..

printf "\0test@example.com\0test.pass" | base64 -i
AHJpbkBleGFwbGUuY29tAHJpbg==


telnet localhost 25
EHLO localhost
AUTH PLAIN AHJpbkBleGFwbGUuY29tAHJpbg==


Configure postfix mysql maps

vim /etc/postfix/mysql_virtual_alias_maps.cf
user = postfixadmin
password = postfixadmin
hosts = 127.0.0.1
dbname = postfix
table = alias
select_field = goto
where_field = address

vim /etc/postfix/mysql_virtual_domains_maps.cf
user = postfixadmin
password = postfixadmin
hosts = 127.0.0.1
dbname = postfix
table = domain
select_field = domain
where_field = domain

vim /etc/postfix/mysql_virtual_mailbox_maps.cf
user = postfixadmin
password = postfixadmin
hosts = 127.0.0.1
dbname = postfix
table = mailbox
select_field = maildir
where_field = username


vim /etc/postfix/mysql_virtual_mailbox_limit_maps.cf
user = postfixadmin
password = postfixadmin
hosts = 127.0.0.1
dbname = postfix
table = mailbox
select_field = quota
where_field = username

vim /etc/postfix/mysql_relay_domains_maps.cf
user = postfixadmin
password = postfixadmin
hosts = 127.0.0.1
dbname = postfix
table = domain
select_field = domain
where_field = domain
additional_conditions = and backupmx = '1'

chgrp postfix /etc/postfix/mysql_*.cf
chmod 640 /etc/postfix/mysql_*.cf


Now create a virtual user
groupadd -g 5000 dovecot
useradd -m -g dovecot -u 5000 -d /home/vmail -s /bin/bash dovecot

Configure postfix main.cf
virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
virtual_mailbox_limit = 51200000
virtual_minimum_uid = 5000
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
virtual_mailbox_base = /home/vmail
virtual_transport = virtual

mynetworks = all

/etc/rc.d/init.d/postfix restart

Now install Dovecot

wget http://dovecot.org/releases/2.2/dovecot-2.2.6.tar.gz

tar -zxvf dovecot-2.2.6.tar.gz

cd dovecot-2.2.6
CPPFLAGS=-I/usr/local/ssl/include LDFLAGS=-L/usr/local/ssl/lib ./configure --with-mysql --with-ssl=openssl
make
make install

dovecot init script
vim /etc/init.d/dovecot

DAEMON=/usr/local/sbin/dovecot
test -x $DAEMON || exit 1
set -e

base_dir=`$DAEMON config -h base_dir`
pidfile=$base_dir/master.pid

if test -f $pidfile; then
  running=yes
else
  running=no
fi

case "$1" in
  start)
    echo -n "Starting Dovecot"
    $DAEMON
    echo "."
    ;;
  stop)
    if test $running = yes; then
      echo "Stopping Dovecot"
      kill `cat $pidfile`
      echo "."
    else
      echo "Dovecot is already stopped."
    fi
    ;;
  reload)
    if test $running = yes; then
      echo -n "Reloading Dovecot configuration"
      kill -HUP `cat $pidfile`
      echo "."
    else
      echo "Dovecot isn't running."
    fi
    ;;
  restart|force-reload)
    echo -n "Restarting Dovecot"
    if test $running = yes; then
      kill `cat $pidfile`
      sleep 1
    fi
    $DAEMON
    echo "."
    ;;
  *)
    echo "Usage: /etc/init.d/dovecot {start|stop|reload|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0


chmod +x /etc/init.d/dovecot

Now configure dovecot
cd /usr/local/share/doc/dovecot/example-config/
cp dovecot-sql.conf.ext dovecot.conf /usr/local/etc/dovecot/
mkdir /usr/local/etc/dovecot/conf.d

cd /usr/local/share/doc/dovecot/example-config/conf.d
cp 10-mail.conf auth-sql.conf.ext 10-ssl.conf 10-master.conf 10-auth.conf /usr/local/etc/dovecot/conf.d/

cd /usr/local/etc/dovecot/

vim dovecot.conf
protocols = imap pop3 lmtp
!include conf.d/*.conf


vim dovecot-sql.conf.ext
driver = mysql
connect = host=127.0.0.1 dbname=postfix user=postfixadmin password=postfixadmin
default_pass_scheme = MD5
password_query = SELECT username AS user, password FROM mailbox WHERE username = '%u'

cd conf.d/

vim 10-mail.conf
mail_location = maildir:/home/vmail/%u
mail_privileged_group = dovecot


vim auth-sql.conf.ext

passdb {
  driver = sql
  args = /usr/local/etc/dovecot/dovecot-sql.conf.ext
}

userdb {
  driver = static
  args = uid=dovecot gid=dovecot home=/home/vmail/%u
}


vim 10-ssl.conf

ssl = required
ssl_cert = </etc/ssl/certs/ssl-cert-snakeoil.pem
ssl_key = </etc/ssl/private/ssl-cert-snakeoil.key


vim 10-master.conf

default_internal_user = dovecot

service auth {
unix_listener auth-userdb {
    mode = 0666
    user = dovecot
  }
user = dovecot
}

user = dovecot
service auth-worker {
 user = $default_internal_user
}


vim 10-auth.conf

auth_mechanisms = plain
!include auth-sql.conf.ext
disable_plaintext_auth = yes

useradd -g dovecot dovenull (dovecot internal user)

Now start dovecot
/etc/init.d/dovecot start

netstat -ntlp
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      9912/mysqld
tcp        0      0 0.0.0.0:110             0.0.0.0:*               LISTEN      10601/dovecot
tcp        0      0 0.0.0.0:143             0.0.0.0:*               LISTEN      10601/dovecot
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      10693/master
tcp        0      0 0.0.0.0:993             0.0.0.0:*               LISTEN      10601/dovecot
tcp        0      0 0.0.0.0:995             0.0.0.0:*               LISTEN      10601/dovecot

Use postfixadmin to create domains and users and aiases.

For web based client we can configure roundcube

wget http://kaz.dl.sourceforge.net/project/roundcubemail/roundcubemail/0.9.4/roundcubemail-0.9.4.tar.gz

apt-get install php5-mcrypt php5-intl

tar -zxvf roundcubemail-0.9.4.tar.gz
mv roundcubemail-0.9.4 /var/www/roundcube

mysql -u root
create database roundcubemail
GRANT ALL PRIVILEGES ON roundcubemail.* TO rcube@localhost IDENTIFIED BY 'rcube';
quit

http://localhost/roundcubemail/installer

follow the steps and you are ready ..

Thanks for reading
Cheers /-

No comments:

Post a Comment