Showing posts with label postfix. Show all posts
Showing posts with label postfix. Show all posts

Monday, May 2, 2011

Postfix with MySQL Backend

Here I am trying to describe how I configured Postfix with MySQL backend. Here I have given only the important parameter, however you can find tune this in various ways.

1.

Configure your Postfix with MySQL support.

I used following command - make -f Makefile.init makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql' 'AUXLIBS=-L/usr/lib/mysql -lmysqlclient -lz -lm'

Before do this, please make sure that you have installed all the MySQL components, including mysql-devel.

2.

Then do the make

3.

DO the make install. At the end of this phase you will be prompt with many questions, answer them appropriate.

4.

Then start configure your Postfix, normally you can find the configuration in main.cf

The important settings (though there are several settings) you need to add are :

data_directory = /var/lib/postfix
mail_owner = postfix
- make sure that you have created the user postfix
myhostname = mail.yy.xx.lk
mydomain = XX,cc.lk
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost
relayhost = 192.NN.N.N
home_mailbox = Maildir/
- This is important, and this is where you say that you want to use MailDir, not Mailbox
virtual_gid_maps = static:506
virtual_mailbox_base = /var/mail/vhosts
virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
virtual_minimum_uid = 100
virtual_transport = virtual
virtual_uid_maps = static:504
- You must create another user, may be vmail, who should have permission to write mails to maildir. After adding those user, you need to specify the uid and gid of that user here in uid_maps and gid_maps. Also make sure that you have given sufficient permission to the mailbox_base to the user that you have created.

After doing all these settings in the main.cf file, you need to create another two files that you have given in "virtual_mailbox_domains" and "virtual_mailbox_maps".

In my case etc/postfix/mysql_virtual_domains_maps.cf contains following parameters :

hosts = localhost
dbname = maildb
user = mailuser
password = xxxxxxx
table = mail_domains
select_field = mail_domain
where_field = mail_domain

This tells postfix that what are the domains should be handled by the postfix it self and what are should be routed to other MTAs.


/etc/postfix/mysql_virtual_mailbox_maps.cf contains :
hosts = localhost
dbname = maildb
user = mailuser
password = XXXX
table = mail_accounts
select_field = maildir
where_field = mail_user

This is used to select the MailDir name for the respective email account. For example if someone sends a mail to sarves@xyz.com, this will find and tells that where that message should go in the virtual_mailbox_base.

Hope this will work for you all as well.

Good luck

Reference :
http://hostingsoftware.net/index.php?module=pagemaster&PAGE_user_op=view_page&PAGE_id=56
http://www.postfix.org/INSTALL.html

Thursday, March 3, 2011

HowTo make two instance of postfix running on same machine

HowTo make two instance of postfix running on same machine
Posted by Nikesh Jauhari

http://linuxpoison.blogspot.com/2008/02/howto-make-two-instance-of-postfix.html


What's involved?

Creating a second instance of Postfix from an existing one involves the following steps:

1. Add an IP address to the server
2. Copy the /etc/postfix directory and all files
3. Create an additional spool directory
4. Edit the config files
5. Create startup and administration scripts

Step one: Add an IP address

The second instance of Postfix will be used for all outbound e-mail. Users will configure their e-mail clients to connect to that IP as their "SMTP server".

Follow the procedure appropriate to your server's version of Linux to add a second IP. The new IP can exist on the same network card as the first (eg. as device eth0:1 on Linux) or can be tied to a second NIC, whichever best suits your requirements.

The new IP address must resolve to a name. Either add a name for it in your DNS, or add an entry in the server's /etc/hosts file. Postfix will not work unless the IP address resolves to a name on the server Postfix is installed on.

As an alternative, the new instance can share the same IP but receive mail on a port other than port 25. We won't show that here, but it's an alternative to be aware of.

Step two: copy /etc/postfix

Copy your existing /etc/postfix directory to /etc/postfix-out:

cp -rp /etc/postfix /etc/postfix-out

The new directory should have all the files with the same ownership and permissions as the original.

To make the next step easier, edit file /etc/postfix-out/main.cf. Change the following setting or add it if it does not exist:

queue_directory = /var/spool/postfix-out

Save the changes to the file before proceeding to the next step.

Step three: create an additional spool directory

Each instance of Postfix must have it's own mail spool directory. To avoid file conflicts, the default directory /var/spool/postfix must not be shared among instances.

Create a directory named /var/spool/postfix-out and let Postfix create the appropriate subdirectories and permissions:

mkdir /var/spool/postfix-out
postfix -c /etc/postfix-out check

The result should be directory /var/spool/postfix-out containing something similar to the following:

drwxr-xr-x 14 root root 336 Jan 30 10:20 .
drwxr-xr-x 15 root root 384 Jan 30 10:20 ..
drwx------ 2 postfix root 48 Jan 30 10:20 active
drwx------ 2 postfix root 48 Jan 30 10:20 bounce
drwx------ 2 postfix root 48 Jan 30 10:20 corrupt
drwx------ 2 postfix root 48 Jan 30 10:20 defer
drwx------ 2 postfix root 48 Jan 30 10:20 deferred
drwx------ 2 postfix root 48 Jan 30 10:20 flush
drwx------ 2 postfix root 48 Jan 30 10:20 incoming
drwx-wx--- 2 postfix postdrop 48 Jan 30 10:20 maildrop
drwxr-xr-x 2 root root 48 Jan 30 10:20 pid
drwx------ 2 postfix root 48 Jan 30 10:20 private
drwx--x--- 2 postfix postdrop 48 Jan 30 10:20 public
drwx------ 2 postfix root 48 Jan 30 10:20 saved

If directory /var/spool/postfix contains directories named etc, usr and lib, your first Postfix instance was probably installed chrooted: if those directories exist, manually copy them to /var/spool/postfix-out:

cp -rp /var/spool/postfix/etc /var/spool/postfix-out
cp -rp /var/spool/postfix/usr /var/spool/postfix-out
cp -rp /var/spool/postfix/lib /var/spool/postfix-out

Step four: edit the config files

Edit the file /etc/postfix/main.cf and add the following near the bottom of the file:

alternate_config_directories = /etc/postfix-out

The above setting is required to inform the Postfix daemons about the second instance.

Next, edit the file /etc/postfix-out/main.cf and change the following setting:

inet_interfaces = second-IP-address-NAME

Note: in the above you must specify the DNS name of the second IP address, not the IP address. If the IP address does not have a DNS name, add an entry for it to /etc/hosts so it can be resolved locally on the server.

You should also remove settings such as reject_maps_rbl and content filtering that only need to be applied to inbound e-mail, and change syslog_facility so logging of outbound mail sent to a different file than inbound.

You might also want to change the setting myhostname so the second instance uses a name difference than the first (eg. "mx1-out"). This is required if the two instances will exchange mail with each other, otherwise Postfix will complain mail "loops back to myself".

Lastly, you can force mail being sent from the outbound instance to send using the same IP address as the inbound instance. This is useful when the mail server is behind a firewall and you want only one IP address to communicate with the Internet. To send mail on a different IP, add the setting smtp_bind_address to main.cf similar to the following:

smtp_bind_address = 192.168.1.1

(Of course, replace the IP address above with your own server's inbound SMTP IP address)

Step five: Create startup and administration scripts
Startup script
The second instance can be started using the normal postfix start command, except you must point to the other configuration directory. For example:

postfix -c /etc/postfix-out start

To create a startup script for the second instance, either edit your existing Postfix startup script and add the above command after the existing postfix start command, or copy the existing startup script to a new name and change the copy. If you copy the start script, be sure to also follow your operating system's instructions for installing a new init script (for example, chkconfig on Redhat Linux, update-rc.d on Debian Linux).

Tuesday, January 4, 2011

email server migration - Sendmail - Postfix - LDAP

I am given with a task to migrate emails from a sendmail server to postfix server. The was a complex task, because of following reason.
- There were many virtual domains
- The sendmail was using mailbox and postfix was using maildir
- The sendmail authentications was on passd files and the postfix's user profile was on LDAP
- The users who are migrated to postfix will get a emails with new domain which was there in postfix and users should be able to use both emails using single account.

I learnt a lot by doing this. I will share that here.

Migrating virtual domain accounts
Sendmail had several virtual domains
eg. xxx.ttt.lk, nnn.mmm.lk etc. All these were created under /etc/vmail/alias.xxx.yyy.lk, alias.nnn.mmm.lk etc. In /etc/vmail the respecting passwd and shadow files also were there.

There I extracted all the users information and made a ldif file so that I can create users on LDAP server. To do that I developed a PHP scipt which reads the information from an excel file and output a ldif file.

When I do this, I add all the mail accounts are mail alias in LDAP.

Another issue I found when I do this was, some password on the old sendmail system was in Unix crypt format. However in the new postfix server it was decided to use the md5crypt. Since the Unix crypt use fixed salting, I used a default md5crypted password for existing Unix Crypted password.

Using the generated ldif file, I created the accounts on LDAP server


Then another issue was how to have all the sendmail virtual domains in postfix. I found that in postfix configuration there is a pasrameter called virtual_domains where I listed all the virtual domains of sendmail so that the postfix will start allowing those domains.

In addition to that I had to do the change in the DNS server as well so that here after all the xxx.yyy.lk and nnn.mmm.lk will be sent to new postfix server.


Moving mailbox to maildir

Other issues was moving existing mailbox of users in sendmail to maildirs in postfix.
To do this, I used a tool called mb2md (http://batleth.sapienti-sat.org/projects/mb2md/)
This tool has several options.
However to use this tool, your system also should have date::parse perl

I installed this using following way :

perl -MCPAN -e 'SHELL'
Install Date::parse

After installing this I added executable access to downloaded mb2md perl script.

Thereafter I ran the script as follows :

./mb2md -s /var/spool/mail/user1 -d /opt/maildirs/user1

Then I also prepared the existing mailbox folders (which were located in /vhome/user1) as maildirs using following command :

./mb2md -s /vhome/user1 -R -d /opt/maildirs/user1

when I ran these command it was showing how many messages got extacted and how many folders have been created etc.

After creating these maildirs I moved them to the respecting locations in the postfix server and provided the owenership to vmail (in my case it was vmail. This user should be defined in main.cf file.


Hope this will help

Enjoy!