xref: /netbsd-src/external/ibm-public/postfix/dist/html/MILTER_README.html (revision 53d1339bf7f9c7367b35a9e1ebe693f9b047a47b)
1<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"
2        "http://www.w3.org/TR/html4/loose.dtd">
3
4<html>
5
6<head>
7
8<title>Postfix before-queue Milter support </title>
9
10<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
11
12</head>
13
14<body>
15
16<h1><img src="postfix-logo.jpg" width="203" height="98" ALT="">Postfix before-queue Milter support </h1>
17
18<hr>
19
20<h2>Introduction</h2>
21
22<p> Postfix implements support for the Sendmail version 8 Milter
23(mail filter) protocol. This protocol is used by applications that
24run outside the MTA to inspect SMTP events (CONNECT, DISCONNECT),
25SMTP commands (HELO, MAIL FROM, etc.) as well as mail content
26(headers and body).  All this happens before mail is queued.  </p>
27
28<p> The reason for adding Milter support to Postfix is that there
29exists a large collection of applications, not only to block unwanted
30mail, but also to verify authenticity (examples: <a
31href="http://www.opendkim.org/">OpenDKIM</a> and <a
32href="http://www.trusteddomain.org/opendmarc/">DMARC </a>)
33or to digitally sign mail (example: <a
34href="http://www.opendkim.org/">OpenDKIM</a>).
35Having yet another Postfix-specific version of all that software
36is a poor use of human and system resources. </p>
37
38<p> The Milter protocol has evolved over time, and different Postfix
39versions implement different feature sets.  See the <a
40href="#workarounds">workarounds</a> and <a
41href="#limitations">limitations</a> sections at the end of this
42document for differences between Postfix and Sendmail implementations.
43</p>
44
45<p> This document provides information on the following topics: </p>
46
47<ul>
48
49<li><a href="#plumbing">How Milter applications plug into Postfix </a>
50
51<li><a href="#building">Building Milter applications</a>
52
53<li><a href="#running">Running Milter applications</a>
54
55<li><a href="#config">Configuring Postfix</a>
56
57<li><a href="#workarounds">Workarounds</a>
58
59<li><a href="#limitations">Limitations</a>
60
61</ul>
62
63<h2><a name="plumbing">How Milter applications plug into Postfix </a> </h2>
64
65<p> The Postfix Milter implementation uses two different lists of
66mail filters: one list of filters for SMTP mail only,
67and one list of filters for non-SMTP mail. The two
68lists have different capabilities, which is unfortunate.  Avoiding
69this would require major restructuring of Postfix. </p>
70
71<ul>
72
73<li> <p> The SMTP-only filters handle mail that arrives via the
74Postfix <a href="smtpd.8.html">smtpd(8)</a> server. They are typically used to filter unwanted
75mail and to sign mail from authorized SMTP clients. You specify
76SMTP-only Milter applications with the <a href="postconf.5.html#smtpd_milters">smtpd_milters</a> parameter as
77described in a later section.  Mail that arrives via the Postfix
78<a href="smtpd.8.html">smtpd(8)</a> server is not filtered by the non-SMTP filters that are
79described next. </p>
80
81<li> <p> The non-SMTP filters handle mail that arrives via the
82Postfix <a href="sendmail.1.html">sendmail(1)</a> command-line or via the Postfix <a href="qmqpd.8.html">qmqpd(8)</a> server.
83They are typically used to digitally sign mail only. Although
84non-SMTP filters can be used to filter unwanted mail, they have
85limitations compared to the SMTP-only filters. You specify non-SMTP
86Milter applications with the <a href="postconf.5.html#non_smtpd_milters">non_smtpd_milters</a> parameter as described
87in a later section.  </p>
88
89</ul>
90
91<p> For those who are familiar with the Postfix architecture, the
92figure below shows how Milter applications plug into Postfix.  Names
93followed by a number are Postfix commands or server programs, while
94unnumbered names inside shaded areas represent Postfix queues.  To
95avoid clutter, the path for local submission is simplified (the
96<a href="OVERVIEW.html">OVERVIEW</a> document has a more complete description of the Postfix
97architecture).  </p>
98
99<blockquote>
100
101<table>
102
103<tr>
104
105<td colspan="2"> </td>
106
107<td align="center"> SMTP-only <br> filters </td>
108
109<td> </td>
110
111<td align="center"> non-SMTP <br> filters </td>
112
113</tr>
114
115<tr>
116
117<td colspan="2"> </td>
118
119<td align="center"> <table> <tr> <td align="center">
120^<br> <tt> | </tt> </td> <td align="center"> <tt> |<br> v </tt>
121</td> </tr> </table> </td>
122
123<td rowspan="2"> </td>
124
125<td rowspan="3" align="center"> <table> <tr> <td align="center">
126^<br> <tt> |<br> |<br> | </tt> </td> <td align="center"> <tt> |<br>
127|<br> |<br> v </tt> </td> </tr> </table> </td>
128
129</tr>
130
131<tr>
132
133<td> Network </td> <td> <tt> -&gt; </tt> </td>
134
135<td bgcolor="#f0f0ff" align="center" valign="middle"> <a href="smtpd.8.html">smtpd(8)</a>
136</td>
137
138</tr>
139
140<tr>
141
142<td colspan="3"> </td> <td> <tt> \ </tt> </td>
143
144</tr>
145
146<tr>
147
148<td> Network </td> <td> <tt> -&gt; </tt> </td>
149
150<td bgcolor="#f0f0ff" align="center" valign="middle"> <a href="qmqpd.8.html">qmqpd(8)</a>
151</td>
152
153<td> <tt> -&gt; </tt> </td>
154
155<td bgcolor="#f0f0ff" align="center" valign="middle"> <a href="cleanup.8.html">cleanup(8)</a>
156</td>
157
158<td> <tt> -&gt; </tt> </td>
159
160<td bgcolor="#f0f0ff" align="center" valign="middle"> <a
161href="QSHAPE_README.html#incoming_queue"> incoming </a> </td>
162
163</tr>
164
165<tr>
166
167<td colspan="3"> </td> <td> <tt> / </tt> </td>
168
169</tr>
170
171<tr>
172
173<td colspan="2"> </td>
174
175<td bgcolor="#f0f0ff" align="center" valign="middle"> <a href="pickup.8.html">pickup(8)</a>
176</td>
177
178</tr>
179
180<tr> <td colspan="2"> </td> <td align="center"> : </td> </tr>
181
182<tr>
183
184<td> Local </td> <td> <tt> -&gt; </tt> </td>
185
186<td bgcolor="#f0f0ff" align="center" valign="middle"> <a href="sendmail.1.html">sendmail(1)</a>
187</td>
188
189</tr>
190
191</table>
192
193</blockquote>
194
195<h2><a name="building">Building Milter applications</a></h2>
196
197<p> Milter applications have been written in C, JAVA and Perl, but
198this document deals with C applications only.   For these, you need
199an object library that implements the Sendmail 8 Milter protocol.
200Postfix currently does not provide such a library, but Sendmail
201does. </p>
202
203<p> Some
204systems install the Sendmail libmilter library by default.  With
205other systems, libmilter may be provided by a package (called
206"sendmail-devel" on some Linux systems).  </p>
207
208<p> Once libmilter is installed, applications such as <a
209href="http://www.opendkim.org/">OpenDKIM</a> and
210<a href="http://www.trusteddomain.org/opendmarc/">OpenDMARC</a>
211build out of the box without requiring any tinkering:</p>
212
213<blockquote>
214<pre>
215$ <b>gzcat opendkim-<i>x.y.z</i>.tar.gz | tar xf -</b>
216$ <b>cd opendkim-<i>x.y.z</i></b>
217$ <b>./configure ...<i>options</i>...</b>
218$ <b>make</b>
219[...<i>lots of output omitted</i>...]
220$ <b>make install</b>
221</pre>
222</blockquote>
223
224<h2><a name="running">Running Milter applications</a></h2>
225
226<p> To run a Milter application, see the documentation of the filter
227for options.  A typical command looks like this:</p>
228
229<blockquote>
230<pre>
231# <b>/some/where/opendkim -l -u <i>userid</i> -p inet:<i>portnumber</i>@localhost ...<i>other options</i>...</b>
232</pre>
233</blockquote>
234
235<p> Please specify a <i>userid</i> value that isn't used for other
236applications (not "postfix", not "www", etc.). </p>
237
238<h2><a name="config">Configuring Postfix</a></h2>
239
240<p> Like Sendmail, Postfix has a lot of configuration options that
241control how it talks to Milter applications. Besides global options
242that apply to all Milter applications, Postfix 3.0 and later
243support per-Milter timeouts, per-Milter error handling, etc. </p>
244
245<p> Information in this section: </p>
246
247<ul>
248
249<li><a href="#smtp-only-milters">SMTP-Only Milter applications </a>
250
251<li><a href="#non-smtp-milters">Non-SMTP Milter applications </a>
252
253<li><a href="#errors">Milter error handling </a>
254
255<li><a href="#version">Milter protocol version</a>
256
257<li><a href="#timeouts">Milter protocol timeouts</a>
258
259<li><a href="#per-milter">Different settings for different Milter
260applications </a>
261
262<li><a href="#per-client">Different settings for different SMTP
263clients </a>
264
265<li><a href="#macros">Sendmail macro emulation</a>
266
267<li><a href="#send-macros">What macros will Postfix send to Milters?</a>
268
269</ul>
270
271<h3><a name="smtp-only-milters">SMTP-Only Milter applications</a></h3>
272
273<p>  The SMTP-only Milter applications handle mail that arrives via
274the Postfix <a href="smtpd.8.html">smtpd(8)</a> server. They are typically used to filter
275unwanted mail, and to sign mail from authorized SMTP clients.  Mail
276that arrives via the Postfix <a href="smtpd.8.html">smtpd(8)</a> server is not filtered by the
277non-SMTP filters that are described in the next section. </p>
278
279<blockquote> NOTE for Postfix versions that have a <a href="postconf.5.html#mail_release_date">mail_release_date</a>
280before 20141018: do not use the <a href="header_checks.5.html">header_checks(5)</a> IGNORE action to remove
281Postfix's own Received: message header. This causes problems with
282mail signing filters. Instead, keep Postfix's own Received: message
283header and use the <a href="header_checks.5.html">header_checks(5)</a> REPLACE action to sanitize
284information. </blockquote>
285
286<p> You specify SMTP-only Milter applications (there can be more
287than one) with the <a href="postconf.5.html#smtpd_milters">smtpd_milters</a> parameter.  Each Milter application
288is identified by the name of its listening socket; other Milter
289configuration options will be discussed in later sections.  Milter
290applications are applied in the order as specified, and the first
291Milter application that rejects a command will override the responses
292from other Milter applications.  </p>
293
294<blockquote>
295<pre>
296/etc/postfix/<a href="postconf.5.html">main.cf</a>:
297    # Milters for mail that arrives via the <a href="smtpd.8.html">smtpd(8)</a> server.
298    # See below for socket address syntax.
299    <a href="postconf.5.html#smtpd_milters">smtpd_milters</a> = inet:localhost:<i>portnumber</i> ...<i>other filters</i>...
300</pre>
301</blockquote>
302
303<p> The general syntax for listening sockets is as follows: </p>
304
305<blockquote>
306
307<dl>
308
309<dt> <b>unix:</b><i>pathname</i> </dt> <dd><p>Connect to the local
310UNIX-domain server that is bound to the specified pathname. If the
311<a href="smtpd.8.html">smtpd(8)</a> or <a href="cleanup.8.html">cleanup(8)</a> process runs chrooted, an absolute pathname
312is interpreted relative to the Postfix queue directory.</p> </dd>
313
314<dt> <b> inet:</b><i>host</i><b>:</b><i>port</i> </dt> <dd> <p>
315Connect to the specified TCP port on the specified local or remote
316host.  The host and port can be specified in numeric or symbolic
317form.</p>
318
319<p> NOTE: Postfix syntax differs from Milter syntax which has the
320form <b>inet:</b><i>port</i><b>@</b><i>host</i>. </p>  </dd>
321
322</dl>
323
324</blockquote>
325
326<p> For advanced configuration see "<a href="#per-client">Different
327settings for different SMTP clients</a>" and "<a
328href="#per-milter">Different settings for different Milter
329applications</a>". </p>
330
331<h3> <a name="non-smtp-milters">Non-SMTP Milter applications </a> </h3>
332
333<p> The non-SMTP Milter applications handle mail that arrives via
334the Postfix <a href="sendmail.1.html">sendmail(1)</a> command-line or via the Postfix <a href="qmqpd.8.html">qmqpd(8)</a>
335server. They are typically used to digitally sign mail. Although
336non-SMTP filters can be used to filter unwanted mail, there are
337limitations as discussed later in this section.  Mail that arrives
338via the Postfix <a href="smtpd.8.html">smtpd(8)</a> server is not filtered by the non-SMTP
339filters.  </p>
340
341<p> NOTE: Do not use the <a href="header_checks.5.html">header_checks(5)</a> IGNORE action to remove
342Postfix's own Received: message header. This causes problems with
343mail signing filters. Instead, keep Postfix's own Received: message
344header and use the <a href="header_checks.5.html">header_checks(5)</a> REPLACE action to sanitize
345information. </p>
346
347<p> You specify non-SMTP Milter applications with the <a href="postconf.5.html#non_smtpd_milters">non_smtpd_milters</a>
348parameter. This parameter uses the same syntax as the <a href="postconf.5.html#smtpd_milters">smtpd_milters</a>
349parameter in the previous section. As with the SMTP-only filters,
350you can specify more than one Milter application; they are applied
351in the order as specified, and the first Milter application that
352rejects a command will override the responses from the other
353applications.  </p>
354
355<blockquote>
356<pre>
357/etc/postfix/<a href="postconf.5.html">main.cf</a>:
358    # Milters for non-SMTP mail.
359    # See below for socket address syntax.
360    <a href="postconf.5.html#non_smtpd_milters">non_smtpd_milters</a> = inet:localhost:<i>portnumber</i> ...<i>other filters</i>...
361</pre>
362</blockquote>
363
364<p> There's one small complication when using Milter applications
365for non-SMTP mail: there is no SMTP session.  To keep Milter
366applications happy, the Postfix <a href="cleanup.8.html">cleanup(8)</a> server actually has to
367simulate the SMTP client CONNECT and DISCONNECT events, and the
368SMTP client EHLO, MAIL FROM, RCPT TO and DATA commands. </p>
369
370<ul>
371
372<li> <p> When new mail arrives via the <a href="sendmail.1.html">sendmail(1)</a> command line,
373the Postfix <a href="cleanup.8.html">cleanup(8)</a> server pretends that the mail arrives with
374ESMTP from "localhost" with IP address "127.0.0.1". The result is
375very similar to what happens with command line submissions in
376Sendmail version 8.12 and later, although Sendmail uses a different
377mechanism to achieve this result.  </p>
378
379<li> <p> When new mail arrives via the <a href="qmqpd.8.html">qmqpd(8)</a> server, the Postfix
380<a href="cleanup.8.html">cleanup(8)</a> server pretends that the mail arrives with ESMTP, and
381uses the QMQPD client hostname and IP address. </p>
382
383<li> <p> When old mail is re-injected into the queue with "postsuper
384-r", the Postfix <a href="cleanup.8.html">cleanup(8)</a> server uses the same client information
385that was used when the mail arrived as new mail. </p>
386
387</ul>
388
389<p> This generally works as expected, with only one exception:
390non-SMTP filters must not REJECT or TEMPFAIL simulated RCPT TO
391commands.  When a <a href="postconf.5.html#non_smtpd_milters">non_smtpd_milters</a> application REJECTs or TEMPFAILs
392a recipient, Postfix will report a configuration error, and mail
393will stay in the queue.  </p>
394
395<h4> Signing internally-generated bounce messages </h4>
396
397<p> Postfix normally does not apply content filters to mail
398that is generated internally such as bounces or Postmaster
399notifications. Filtering internally-generated bounces would result
400in loss of mail when a filter rejects a message, as the resulting
401double-bounce message would almost certainly also be blocked. </p>
402
403<p>  To sign Postfix's own bounce messages, enable filtering of
404internally-generated bounces (line 2 below), and don't reject any
405internally-generated bounces with <a href="postconf.5.html#non_smtpd_milters">non_smtpd_milters</a>, <a href="postconf.5.html#header_checks">header_checks</a>
406or <a href="postconf.5.html#body_checks">body_checks</a> (lines 3-5 below).  </p>
407
408<blockquote>
409<pre>
4101 /etc/postfix/<a href="postconf.5.html">main.cf</a>:
4112     <a href="postconf.5.html#internal_mail_filter_classes">internal_mail_filter_classes</a> = bounce
4123     <a href="postconf.5.html#non_smtpd_milters">non_smtpd_milters</a> = <i>don't reject internally-generated bounces</i>
4134     <a href="postconf.5.html#header_checks">header_checks</a> = <i>don't reject internally-generated bounces</i>
4145     <a href="postconf.5.html#body_checks">body_checks</a> = <i>don't reject internally-generated bounces</i>
415</pre>
416</blockquote>
417
418<h3><a name="errors">Milter error handling</a></h3>
419
420<p> The <a href="postconf.5.html#milter_default_action">milter_default_action</a> parameter specifies how Postfix handles
421Milter application errors. The default action is to respond with a
422temporary error status, so that the client will try again later.
423Specify "accept" if you want to receive mail as if the filter does
424not exist, and "reject" to reject mail with a permanent status.
425The "quarantine" action is like "accept" but freezes the message
426in the "<a href="QSHAPE_README.html#hold_queue">hold" queue</a>, and is available with Postfix 2.6 or later.
427</p>
428
429<blockquote>
430<pre>
431/etc/postfix/<a href="postconf.5.html">main.cf</a>:
432    # What to do in case of errors? Specify accept, reject, tempfail,
433    # or quarantine (Postfix 2.6 or later).
434    <a href="postconf.5.html#milter_default_action">milter_default_action</a> = tempfail
435</pre>
436</blockquote>
437
438<p> See "<a href="#per-milter">Different settings for different
439Milter applications</a>" for advanced configuration options. </p>
440
441<h3><a name="version">Milter protocol version</a></h3>
442
443<p> As Postfix is not built with the Sendmail libmilter library,
444you may need to configure the Milter protocol version that Postfix
445should use.  The default version is 6 (before Postfix 2.6 the default
446version is 2). </p>
447
448<blockquote>
449<pre>
450/etc/postfix/<a href="postconf.5.html">main.cf</a>:
451    # Postfix &ge; 2.6
452    <a href="postconf.5.html#milter_protocol">milter_protocol</a> = 6
453    # 2.3 &le; Postfix &le; 2.5
454    <a href="postconf.5.html#milter_protocol">milter_protocol</a> = 2
455</pre>
456</blockquote>
457
458<p> If the Postfix <a href="postconf.5.html#milter_protocol">milter_protocol</a> setting specifies a too low
459version, the libmilter library will log an error message like this:
460</p>
461
462<blockquote>
463<pre>
464<i>application name</i>: st_optionneg[<i>xxxxx</i>]: 0x<i>yy</i> does not fulfill action requirements 0x<i>zz</i>
465</pre>
466</blockquote>
467
468<p> The remedy is to increase the Postfix <a href="postconf.5.html#milter_protocol">milter_protocol</a> version
469number.  See, however, the <a href="#limitations">limitations</a>
470section below for features that aren't supported by Postfix. </p>
471
472<p> With Postfix 2.7 and earlier, if the Postfix <a href="postconf.5.html#milter_protocol">milter_protocol</a>
473setting specifies a too high
474version, the libmilter library simply hangs up without logging a
475warning, and you see a Postfix warning message like one of the
476following: </p>
477
478<blockquote>
479<pre>
480warning: milter inet:<i>host</i>:<i>port</i>: can't read packet header: Unknown error : 0
481warning: milter inet:<i>host</i>:<i>port</i>: can't read packet header: Success
482warning: milter inet:<i>host</i>:<i>port</i>: can't read SMFIC_DATA reply packet header: No such file or directory
483</pre>
484</blockquote>
485
486<p> The remedy is to lower the Postfix <a href="postconf.5.html#milter_protocol">milter_protocol</a> version
487number. Postfix 2.8 and later will automatically turn off protocol
488features that the application's libmilter library does not expect.
489</p>
490
491<p> See "<a href="#per-milter">Different settings for different
492Milter applications</a>" for advanced configuration options. </p>
493
494<h3><a name="timeouts">Milter protocol timeouts</a></h3>
495
496<p> Postfix uses different time limits at different Milter protocol
497stages. The table shows the timeout settings and the corresponding
498protocol stages
499(EOH = end of headers; EOM = end of message).  </p>
500
501<blockquote>
502
503<table border="1">
504
505<tr> <th> Postfix parameter </th> <th> Time limit </th> <th> Milter
506protocol stage</th> </tr>
507
508<tr> <td> <a href="postconf.5.html#milter_connect_timeout">milter_connect_timeout</a> </td> <td> 30s </td> <td> CONNECT
509</td> </tr>
510
511<tr> <td> <a href="postconf.5.html#milter_command_timeout">milter_command_timeout</a> </td> <td> 30s </td> <td> HELO,
512MAIL, RCPT, DATA, UNKNOWN </td> </tr>
513
514<tr> <td> <a href="postconf.5.html#milter_content_timeout">milter_content_timeout</a> </td> <td> 300s </td> <td> HEADER,
515EOH, BODY, EOM </td> </tr>
516
517</table>
518
519</blockquote>
520
521<p> Beware: 30s may be too short for Milter applications that do
522lots of DNS lookups.  However, if you increase the above timeouts
523too much, remote SMTP clients may hang up and mail may be delivered
524multiple times. This is an inherent problem with before-queue
525filtering. </p>
526
527<p> See "<a href="#per-milter">Different settings for different
528Milter applications</a>" for advanced configuration options. </p>
529
530<h3><a name="per-milter">Different settings for different Milter
531applications </a></h3>
532
533<p> The previous sections list a number of Postfix <a href="postconf.5.html">main.cf</a> parameters
534that control time limits and other settings for all Postfix Milter
535clients. This is sufficient for simple configurations. With more
536complex configurations it becomes desirable to have different
537settings for different Milter clients. This is supported with Postfix
5383.0 and later. </p>
539
540<p> The following example shows a "non-critical" Milter client with
541a short connect timeout, and with "accept" as default action when
542the service is unvailable. </p>
543
544<blockquote>
545<pre>
5461 /etc/postfix/<a href="postconf.5.html">main.cf</a>:
5472     <a href="postconf.5.html#smtpd_milters">smtpd_milters</a> = { inet:host:port,
5483         connect_timeout=10s, default_action=accept }
549</pre>
550</blockquote>
551
552<p> Instead of a server endpoint, we now have a list enclosed in {}. </p>
553
554<ul>
555
556<li> <p> Line 2: The first item in the list is the server endpoint.
557This supports the exact same "inet" and "unix" syntax as described
558earlier. </p>
559
560<li> <p> Line 3: The remainder of the list contains per-Milter
561settings. These settings override global <a href="postconf.5.html">main.cf</a> parameters, and
562have the same name as those parameters, without the "milter_" prefix.
563The per-Milter settings that are supported as of Postfix 3.0 are
564command_timeout, connect_timeout, content_timeout, default_action,
565and protocol.  </p>
566
567</ul>
568
569<p> Inside the list, syntax is similar to what we already know from
570<a href="postconf.5.html">main.cf</a>: items separated by space or comma. There is one difference:
571<b>you must enclose a setting in parentheses, as in "{ name = value
572}", if you want to have space or comma within a value or around
573"="</b>.  </p>
574
575<h3><a name="per-client">Different settings for different SMTP
576clients </a></h3>
577
578<p> The <a href="postconf.5.html#smtpd_milter_maps">smtpd_milter_maps</a> feature supports different Milter settings
579for different client IP addresses. Lookup results override the the
580global <a href="postconf.5.html#smtpd_milters">smtpd_milters</a> setting, and have the same syntax. For example,
581to disable Milter settings for local address ranges: </p>
582
583<pre>
584/etc/postfix/<a href="postconf.5.html">main.cf</a>:
585    <a href="postconf.5.html#smtpd_milter_maps">smtpd_milter_maps</a> = <a href="cidr_table.5.html">cidr</a>:/etc/postfix/smtpd_milter_map
586    <a href="postconf.5.html#smtpd_milters">smtpd_milters</a> = inet:host:port, { inet:host:port, ... }, ...
587
588/etc/postfix/smtpd_milter_map:
589    # Disable Milters for local clients.
590    127.0.0.0/8    DISABLE
591    192.168.0.0/16 DISABLE
592    ::/64          DISABLE
593    2001:db8::/32  DISABLE
594</pre>
595
596<p> This feature is available with Postfix 3.2 and later. </p>
597
598<h3><a name="macros">Sendmail macro emulation</a></h3>
599
600<p> Postfix emulates a limited number of Sendmail macros, as shown
601in the table. Some macro values depend on whether a recipient is
602rejected (rejected recipients are available on request by the Milter
603application). Different macros are available at different Milter
604protocol stages (EOH = end-of-header, EOM = end-of-message); their
605availability is not
606always the same as in Sendmail. See the <a
607href="#workarounds">workarounds</a> section below for solutions.
608</p>
609
610<blockquote>
611
612<table border="1">
613
614<tr> <th> Sendmail macro </th> <th> Milter protocol stage </th>
615<th> Description </th> </tr>
616
617<tr> <td> i </td> <td> DATA, EOH, EOM </td> <td> Queue ID, also
618Postfix queue file name </td> </tr>
619
620<tr> <td> j </td> <td> Always </td> <td> Value of <a href="postconf.5.html#myhostname">myhostname</a> </td>
621</tr>
622
623<tr> <td> _ </td> <td> Always </td> <td> The validated client name
624and address </td> </tr>
625
626<tr> <td> {auth_authen} </td> <td> MAIL, DATA, EOH, EOM </td> <td> SASL
627login name </td> </tr>
628
629<tr> <td> {auth_author} </td> <td> MAIL, DATA, EOH, EOM </td> <td> SASL
630sender </td> </tr>
631
632<tr> <td> {auth_type} </td> <td> MAIL, DATA, EOH, EOM </td> <td> SASL
633login method </td> </tr>
634
635<tr> <td> {client_addr} </td> <td> Always </td> <td> Remote client
636IP address </td> </tr>
637
638<tr> <td> {client_connections} </td> <td> CONNECT </td> <td>
639Connection concurrency for this client (zero if the client is
640excluded from all smtpd_client_* limits). </td> </tr>
641
642<tr> <td> {client_name} </td> <td> Always </td> <td> Remote client
643hostname <br> When address &rarr; name lookup or name &rarr; address
644verification fails: "unknown" </td> </tr>
645
646<tr> <td> {client_port} </td> <td> Always (Postfix &ge;2.5) </td>
647<td> Remote client TCP port </td> </tr>
648
649<tr> <td> {client_ptr} </td> <td> CONNECT, HELO, MAIL, DATA </td>
650<td> Client name from address &rarr; name lookup <br> When address
651&rarr; name lookup fails: "unknown" </td> </tr>
652
653<tr> <td> {cert_issuer} </td> <td> HELO, MAIL, DATA, EOH, EOM </td> <td>
654TLS client certificate issuer </td> </tr>
655
656<tr> <td> {cert_subject} </td> <td> HELO, MAIL, DATA, EOH, EOM </td>
657<td> TLS client certificate subject </td> </tr>
658
659<tr> <td> {cipher_bits} </td> <td> HELO, MAIL, DATA, EOH, EOM </td> <td>
660TLS session key size </td> </tr>
661
662<tr> <td> {cipher} </td> <td> HELO, MAIL, DATA, EOH, EOM </td> <td> TLS
663cipher </td> </tr>
664
665<tr> <td> {daemon_addr} </td> <td> Always (Postfix &ge;3.2) </td>
666<td> Local server IP address </td> </tr>
667
668<tr> <td> {daemon_name} </td> <td> Always </td> <td> value of
669<a href="postconf.5.html#milter_macro_daemon_name">milter_macro_daemon_name</a> </td> </tr>
670
671<tr> <td> {daemon_port} </td> <td> Always (Postfix &ge;3.2) </td>
672<td> Local server TCP port </td> </tr>
673
674<tr> <td> {mail_addr} </td> <td> MAIL </td> <td> Sender address
675</td> </tr>
676
677<tr> <td> {mail_host} </td> <td> MAIL (Postfix &ge; 2.6, only with
678<a href="postconf.5.html#smtpd_milters">smtpd_milters</a>) </td> <td> Sender next-hop destination </td> </tr>
679
680<tr> <td> {mail_mailer} </td> <td> MAIL (Postfix &ge; 2.6, only with
681<a href="postconf.5.html#smtpd_milters">smtpd_milters</a>) </td> <td> Sender mail delivery transport </td> </tr>
682
683<tr> <td> {rcpt_addr} </td> <td> RCPT </td> <td> Recipient address
684<br> With rejected recipient: descriptive text </td> </tr>
685
686<tr> <td> {rcpt_host} </td> <td> RCPT (Postfix &ge; 2.6, only with
687<a href="postconf.5.html#smtpd_milters">smtpd_milters</a>) </td> <td> Recipient next-hop destination <br> With
688rejected recipient: enhanced status code </td> </tr>
689
690<tr> <td> {rcpt_mailer} </td> <td> RCPT (Postfix &ge; 2.6, only with
691<a href="postconf.5.html#smtpd_milters">smtpd_milters</a>) </td> <td> Recipient mail delivery transport <br>
692With rejected recipient: "error" </td> </tr>
693
694<tr> <td> {tls_version} </td> <td> HELO, MAIL, DATA, EOH, EOM </td>
695<td> TLS protocol version </td> </tr>
696
697<tr> <td> v </td> <td> Always </td> <td> value of <a href="postconf.5.html#milter_macro_v">milter_macro_v</a>
698</td> </tr>
699
700</table>
701
702</blockquote>
703
704<h3><a name="send-macros">What macros will Postfix send to Milters?</a></h3>
705
706<p> Postfix sends specific sets of macros at different Milter protocol
707stages.  The sets are configured with the parameters as shown in the
708table below (EOH = end of headers; EOM = end of message). The
709protocol version is a number that Postfix sends at the beginning
710of the Milter protocol handshake. </p>
711
712<p> As of Sendmail 8.14.0, Milter applications can specify what
713macros they want to receive at different Milter protocol stages.
714An application-specified list takes precedence over a Postfix-specified
715list. </p>
716
717<blockquote>
718
719<table border="1">
720
721<tr> <th> Postfix parameter </th> <th> Milter protocol version </th>
722<th> Milter protocol stage </th> </tr>
723
724<tr> <td> <a href="postconf.5.html#milter_connect_macros">milter_connect_macros</a> </td> <td> 2 or higher </td> <td>
725CONNECT </td> </tr>
726
727<tr> <td> <a href="postconf.5.html#milter_helo_macros">milter_helo_macros</a> </td> <td> 2 or higher </td> <td>
728HELO/EHLO </td> </tr>
729
730<tr> <td> <a href="postconf.5.html#milter_mail_macros">milter_mail_macros</a> </td> <td> 2 or higher </td> <td> MAIL
731FROM </td> </tr>
732
733<tr> <td> <a href="postconf.5.html#milter_rcpt_macros">milter_rcpt_macros</a> </td> <td> 2 or higher </td> <td> RCPT
734TO </td> </tr>
735
736<tr> <td> <a href="postconf.5.html#milter_data_macros">milter_data_macros</a> </td> <td> 4 or higher </td> <td> DATA
737</td> </tr>
738
739<tr> <td> <a href="postconf.5.html#milter_end_of_header_macros">milter_end_of_header_macros</a> </td> <td> 6 or higher </td>
740<td> EOH </td> </tr>
741
742<tr> <td> <a href="postconf.5.html#milter_end_of_data_macros">milter_end_of_data_macros</a> </td> <td> 2 or higher </td>
743<td> EOM </td> </tr>
744
745<tr> <td> <a href="postconf.5.html#milter_unknown_command_macros">milter_unknown_command_macros</a> </td> <td> 3 or higher </td>
746<td> unknown command </td> </tr>
747
748</table>
749
750</blockquote>
751
752<p> By default, Postfix will send only macros whose values have been
753updated with information from <a href="postconf.5.html">main.cf</a> or <a href="master.5.html">master.cf</a>, from an SMTP session
754(for example; SASL login, or TLS certificates) or from a Mail delivery
755transaction (for example; queue ID, sender, or recipient). </p>
756
757<p> To force a macro to be sent even when its value has not been updated,
758you may specify macro default values with the <a href="postconf.5.html#milter_macro_defaults">milter_macro_defaults</a>
759parameter. Specify zero or more <i>name=value</i> pairs separated by
760comma or whitespace; you may even specify macro names that Postfix does
761know about! </p>
762
763<h2><a name="workarounds">Workarounds</a></h2>
764
765<ul>
766
767<li> <p> To avoid breaking DKIM etc. signatures with an SMTP-based
768content filter, update the before-filter SMTP client in <a href="master.5.html">master.cf</a>,
769and add a line with "-o <a href="postconf.5.html#disable_mime_output_conversion">disable_mime_output_conversion</a>=yes" (note:
770no spaces around the "="). For details, see the <a
771href="FILTER_README.html#advanced_filter">advanced content filter</a>
772example. </p>
773
774<pre>
775/etc/postfix/<a href="master.5.html">master.cf</a>:
776    # =============================================================
777    # service type  private unpriv  chroot  wakeup  maxproc command
778    #               (yes)   (yes)   (yes)   (never) (100)
779    # =============================================================
780    scan      unix  -       -       n       -       10      smtp
781        -o <a href="postconf.5.html#smtp_send_xforward_command">smtp_send_xforward_command</a>=yes
782        -o <a href="postconf.5.html#disable_mime_output_conversion">disable_mime_output_conversion</a>=yes
783        -o <a href="postconf.5.html#smtp_generic_maps">smtp_generic_maps</a>=
784</pre>
785
786<li> <p> Some Milter applications use the "<tt>{if_addr}</tt>" macro
787to recognize local mail; this macro does not exist in Postfix.
788Workaround: use the "<tt>{daemon_addr}</tt>" (Postfix &ge; 3.2) or
789"<tt>{client_addr}</tt>" macro instead. </p>
790
791<li> <p> Some Milter applications log a warning that looks like
792this: </p>
793
794<blockquote> <pre>
795sid-filter[36540]: WARNING: sendmail symbol 'i' not available
796</pre>
797</blockquote>
798
799<p> And they may insert an ugly message header with "unknown-msgid"
800like this: </p>
801
802<blockquote>
803<pre>
804X-SenderID: Sendmail Sender-ID Filter vx.y.z host.example.com &lt;unknown-msgid&gt;
805</pre>
806</blockquote>
807
808<p> The problem is that Milter applications expect that the queue
809ID is known <i>before</i> the MTA accepts the MAIL FROM (sender)
810command.  Postfix does not choose a queue ID, which is used as the
811queue file name, until <i>after</i> it accepts the first valid RCPT
812TO (recipient) command. </p>
813
814<p> If you experience the ugly header problem, see if a recent
815version of the Milter application fixes it. For example, current
816versions of dkim-filter and dk-filter already have code that looks
817up the Postfix queue ID at a later protocol stage, and sid-filter
818version 1.0.0 no longer includes the queue ID in the message header.
819</p>
820
821<p> To fix the ugly message header, you will need to add code that
822looks up the Postfix queue ID at some later point in time. The
823example below adds the lookup after the end-of-message.  </p>
824
825<ul>
826
827<li> <p> Edit the filter source file (typically named
828<tt>xxx-filter/xxx-filter.c</tt> or similar). </p>
829
830<li> <p> Look up the <tt>mlfi_eom()</tt> function and add code near
831the top shown as <b>bold</b> text below: </p>
832
833</ul>
834
835<blockquote>
836<pre>
837dfc = cc->cctx_msg;
838assert(dfc != NULL);
839<b>
840/* Determine the job ID for logging. */
841if (dfc->mctx_jobid == 0 || strcmp(dfc->mctx_jobid, JOBIDUNKNOWN) == 0) {
842        char *jobid = smfi_getsymval(ctx, "i");
843        if (jobid != 0)
844                dfc->mctx_jobid = jobid;
845}</b>
846</pre>
847</blockquote>
848
849<p> NOTES: </p>
850
851<ul>
852
853<li> <p> Different mail filters use slightly different names for
854variables. If the above code does not compile, look elsewhere in
855the mail filter source file for code that looks up the "i" macro
856value, and copy that code.  </p>
857
858<li> <p> This change fixes only the ugly message header, but not
859the WARNING message.  Fortunately, many Milters log that message
860only once. </p>
861
862</ul>
863
864</ul>
865
866<h2><a name="limitations">Limitations</a></h2>
867
868<p> This section lists limitations of the Postfix Milter implementation.
869Some limitations will be removed as the implementation is extended
870over time. Of course the usual limitations of before-queue filtering
871will always apply. See the <a href="CONTENT_INSPECTION_README.html">CONTENT_INSPECTION_README</a> document for
872a discussion. </p>
873
874<ul>
875
876<li> <p> The Milter protocol has evolved over time. Therefore,
877different Postfix versions implement different feature sets. </p>
878
879<table border="1">
880
881<tr> <th> Postfix </th> <th> Supported Milter requests </th>
882</tr>
883
884<tr> <td align="center"> 2.6 </td> <td> All Milter requests of
885Sendmail 8.14.0 (see notes below).  </td> </tr>
886
887<tr> <td align="center"> 2.5 </td> <td> All Milter requests of
888Sendmail 8.14.0, except: <br> SMFIP_RCPT_REJ (report rejected
889recipients to the mail filter), <br> SMFIR_CHGFROM (replace sender,
890with optional ESMTP parameters), <br> SMFIR_ADDRCPT_PAR (add
891recipient, with optional ESMTP parameters). </td> </tr>
892
893<tr> <td align="center"> 2.4 </td> <td> All Milter requests of
894Sendmail 8.13.0.  </td> </tr>
895
896<tr> <td align="center"> 2.3 </td> <td> All Milter requests of
897Sendmail 8.13.0, except: <br> SMFIR_REPLBODY (replace message body).
898
899</table>
900
901<li> <p> For Milter applications that are written in C, you need
902to use the Sendmail libmilter library. </p>
903
904<li> <p> Postfix has TWO sets of mail filters: filters that are used
905for SMTP mail only (specified with the <a href="postconf.5.html#smtpd_milters">smtpd_milters</a> parameter),
906and filters for non-SMTP mail (specified with the <a href="postconf.5.html#non_smtpd_milters">non_smtpd_milters</a>
907parameter).  The non-SMTP filters are primarily for local submissions.
908</p>
909
910<p> When mail is filtered by <a href="postconf.5.html#non_smtpd_milters">non_smtpd_milters</a>, the Postfix <a href="cleanup.8.html">cleanup(8)</a>
911server has to simulate SMTP client requests.  This works as expected,
912with only one exception: <a href="postconf.5.html#non_smtpd_milters">non_smtpd_milters</a> must not REJECT or
913TEMPFAIL simulated RCPT TO commands. When this rule is violated,
914Postfix will report a configuration error, and mail will stay in
915the queue.  </p>
916
917<li> <p> When you use the before-queue content filter for incoming
918SMTP mail (see <a href="SMTPD_PROXY_README.html">SMTPD_PROXY_README</a>), Milter applications have access
919only to the SMTP command information; they have no access to the
920message header or body, and cannot make modifications to the message
921or to the envelope. </p>
922
923<li> <p> Postfix 2.6 ignores the optional ESMTP parameters in
924requests to replace the sender (SMFIR_CHGFROM) or to append a
925recipient (SMFIR_ADDRCPT_PAR). Postfix logs a warning message when
926a Milter application supplies such ESMTP parameters: </p>
927
928<pre>
929warning: <i>queue-id</i>: cleanup_chg_from: ignoring ESMTP arguments "<i>whatever</i>"
930warning: <i>queue-id</i>: cleanup_add_rcpt: ignoring ESMTP arguments "<i>whatever</i>"
931</pre>
932
933<li> <p> Postfix 2.3 does not implement requests to replace the
934message body. Milter applications log a warning message when they
935need this unsupported operation: </p>
936
937<pre>
938st_optionneg[134563840]: 0x3d does not fulfill action requirements 0x1e
939</pre>
940
941<p> The solution is to use Postfix version 2.4 or later. </p>
942
943<li> <p> Most Milter configuration options are global. Future Postfix
944versions may support per-Milter timeouts, per-Milter error handling,
945etc. </p>
946
947</ul>
948
949</body>
950
951</html>
952