1

I've managed to configure Postfix to read virtual mailboxes list out of a database. But what if my list is distributed among multi databases? Is there any way to set Postfix's database access credentials based on the incoming email's destination domain?

I've got multiple customers sharing a web server and each one has his own database through sharing. And I was wondering if I can also share a Postfix among them!? The problem is that their list of users resides within their individual databases and the number of shards can grow over the time. And I don't want to reconfigure the system by each new customer.

Mehran
  • 519
  • 1
  • 5
  • 20
  • Let me clarifying your question. Suppose you have multi database, called **customer1**, **customer2**, **customer3** and so on. In every database you have table called `postfixuser`. So, in this case, you want postfix to run query across multiple database with same tables (same structure)? – masegaloeh May 10 '15 at 22:25
  • Yes, it's just that for every email address it is known which database to look in to. Consider using the domain part of email address to choose the proper database that Postfix should look in to. – Mehran May 11 '15 at 03:02

2 Answers2

2

From Postfix Database Readme, it states

All Postfix lookup tables store information as (key, value) pairs. This interface may seem simplistic at first, but it turns out to be very powerful. The (key, value) query interface completely hides the complexities of LDAP or SQL from Postfix. This is a good example of connecting complex systems with simple interfaces.

Postfix only provide you a lookup key, then "your table" must return the appropriate value. Regarding multiple databases, postfix also support it. But it only has one single operation mode: check the database one by one sequentially until the "key" was found. When you added some custom logic like set Postfix's database access credentials based on the incoming email's destination domain, then postfix doesn't have such option.


So, the answer of your question:

Is it possible to shard postfix virtual mailboxes database?

Yes, it is possible. But you can't do that with postfix alone. You need additional "tools" to help postfix lookup information from your DB. Postfix

Some idea:

  • Using MySQL stored procedure to populate all databases and then run query accross the databases, for example: https://stackoverflow.com/questions/2132654/querying-multiple-databases-at-once . You you need single master credential to connect to mysql.

  • Put multiple postfix-mysql-maps files in one folder and set the cron to periodically to re-populate all the files in postfix main.cf. The command like below should be sufficient.

postconf -e virtual_mailbox_maps=$(for maps in /etc/postfix/mymaps/*; do printf "mysql:$maps "; done)

  • Use tcp/socketmap table maps to add additional script and do your logic. You can use postfix spawn so your script doesn't have to listen in tcp/unix socket. Your postfix must will get the key via stdin and you must return the value via stdout. Here is one implementation of tcp table and spawn in transport_maps.
masegaloeh
  • 18,236
  • 10
  • 57
  • 106
  • It's great thanks. Could you please expand / give reference for the second option? How can I have multiple MySQL maps? Even though I think the last one is my final solution. – Mehran May 12 '15 at 05:16
  • see updated answer – masegaloeh May 12 '15 at 08:41
  • I haven't had that much time to study the last solution but at the first glance it seems to me that it's not made for virtual mailboxes!! Is it? – Mehran May 14 '15 at 09:16
  • Sorry for the confusion. I only gave you the example in `transport_maps`. Of course, you can use it in `virtual_mailbox_maps` too – masegaloeh May 14 '15 at 09:18
  • I've studied the process and the protocol, and that I'll be getting `get SPACE key NEWLINE` and I'm supposed to return `200 SPACE text NEWLINE`. But what will the `key` be and what should I return as `text`? Is there any document explaining that? – Mehran May 14 '15 at 20:12
  • Every parameter has its input and expected output. In virtual_mailbox_maps case, see http://www.postfix.org/postconf.5.html#virtual_mailbox_maps , just like normal mysql/hash table – masegaloeh May 14 '15 at 21:17
0

If you are using postfix and mysql to store the users domain and password and so on, you can have many postfix servers with one mysql server, because postfix uses mysql only for retrieve the informations using read sql query.

If you have a centralized database, every client can point to this database.

c4f4t0r
  • 5,301
  • 3
  • 31
  • 42
  • Thanks but you've got it all the way around! I want to use one Postfix with many MySQLs. My virtual mailboxes' list is scattered among multiple databases. – Mehran May 11 '15 at 02:53