Postfix tips

Translation of the original in Catalan. Read it in: CatalàCastellano

I have spent many, many hours at the beginning of this week reading Postfix documentation in order to solve a problem for a client who had a huge mail queue and there was no way to serve it because the server could not cope. Well then, because of this I have had to read many documents and I wanted to reference them so as not to lose sight of them, since many of them are extremely interesting. Really, every day that goes by I am more convinced that Postfix is the best option as an MTA; I would say I have spent many hours and many years trying sendmail, exim and qmail. But experience has always told me that Postfix was the one that solved the most unlikely problems for me and that has been the case this time too.

  • Postfix Bottleneck Analysis: this document is an introduction to the qshape tool, which lets us see the state of the different queues that postfix has, not only as totals but also broken down by domain and with a time distribution of how long the mail has been in the queue. Really very, very useful for knowing where the mail is really piling up and what measures we have to take.

  • postsuper - Postfix superintendent: this command lets us do maintenance tasks on the postfix queues. Although the postqueue command also allows this kind of task, these tasks are much more limited and oriented to users rather than system administrators. So that you understand the power of postsuper, some of its functions are: deleting messages from a specific queue through their ID, putting messages in hold status so that these messages are not attempted to be sent and we keep them retained until we want them, messages that have already been queued can be re-queued so we can reapply filters that had been applied wrongly, etc.

  • Postfix built-in content inspection: basically it is a set of directives in the main.cf file that allow filtering mail based on regular expressions applied to the mail header (header_checks), mail body (body_checks), mime attachments (mime_header_checks) or headers of mails attached to mails (nested_header_checks).

  • Postfix After-Queue Content Filter: this document explains how to filter mail in much more advanced ways, for example, it explains the basis of how applications like amavisd-new work when integrated with postfix. What I mean is having a postfix listening on port 25, when a mail comes in it is sent to a localhost port, there the mail is processed and other mails with spam reports are generated if needed and then it is re-injected into another localhost port, in this case published by the postfix, and finally this mail is served. So this, which seems simple to set up, has its peculiarities that must be taken into account and in this document they are explained very, very well; it also references tools that I mention below to create our own SMTP proxies where we can apply the filters that interest us. An example of an application, apart from the typical anti-spam filters, would be to set up command services via SMTP, that is, sending a mail with a specific format would perform a series of actions and the result would return to its origin via email. In my packet radio days we used this to do smtp2ftp, for example.

  • smtpprox - simple efficient SMTP proxy in perl: with this tool it is trivial to set up our custom applications to do mail filtering as I mentioned in the previous point; moreover the page itself references many patches to do specific functions with this tool. Some examples: we can do the same as amavisd-new but with much simpler code, we can act as an authentication proxy for an SMTP that does not have authentication set up, have a blacklist in MySQL, etc. Basically the SMTP proxy only acts as an SMTP server on one side and an SMTP client on the other, in the middle we can process the mail according to its information: header, attachments, mail content, etc.

I wanted to explicitly comment on one of the interesting solutions presented in the first document, the one that talks about qshape. The idea is that through this utility we can find a domain on which many mails are queued and therefore the outgoing mail queue is harmed because this destination monopolises a large part of the queue. Well then, it is about building an outgoing mail queue only for this destination and making the mails going there be processed separately.

To set this up you have to:

  • Modify the master.cf file and add an "smtp" transport for the specific destination. This would be done like this:

/etc/postfix/<a href="http://www.postfix.org/master.5.html">master.cf</a>:
    # service type  private unpriv  chroot  wakeup  maxproc command
    fragile   unix     -       -       n       -      20    smtp
  • Notice that we set the number of processes to a maximum of 20, this means that at most there can be 20 simultaneous instances of the "smtp" process to serve mails from this outgoing queue.

  • In main.cf we configure the limits of the queue that will be used by the "fragile" transport that we have configured.

/etc/postfix/<a href="http://www.postfix.org/postconf.5.html">main.cf</a>:
<a href="http://www.postfix.org/postconf.5.html#transport_maps">transport_maps</a> = hash:/etc/postfix/transport
    fragile_destination_concurrency_failed_cohort_limit = 100
    fragile_destination_concurrency_limit = 20
  • Now it is time to create the transport file where we will indicate that the mails for this domain are served by a particular queue; next to the queue name, if desired, a host and a port can be indicated towards which the relay will be done:

/etc/postfix/transport:
    example.com  fragile:
  • Remember that after creating the transport file you have to do a postmap transport.

  • To see the state of the new queue, you just have to do qshape fragile.

I hope that if you did not know postfix this small post has served to show you how simple and dynamic it is to configure, I did not intend to make any introduction, just a pure documentation reference that I think from now on I will try to keep close.

1 comment

Comments from the old blog, as they were written. Comments are closed.

  1. Friki

    Molt interessant el post, útil pels que administrem postfix!!