xref: /netbsd-src/external/ibm-public/postfix/dist/html/SMTPD_POLICY_README.html (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
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 SMTP Access Policy Delegation </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 SMTP Access Policy Delegation </h1>
17
18<hr>
19
20<h2>Purpose of Postfix SMTP access policy delegation</h2>
21
22<p> The Postfix SMTP server has a number of built-in mechanisms to
23block or accept mail at specific SMTP protocol stages. In addition,
24the Postfix SMTP server can delegate decisions to an external policy
25server (Postfix 2.1 and later). </p>
26
27<p> With this policy delegation mechanism, a simple <a href="#greylist">
28greylist </a> policy can be implemented with only a dozen lines of
29Perl, as is shown at the end of this document. A complete example
30can be found in the Postfix source code, in the directory
31examples/smtpd-policy. </p>
32
33<p> Another example of policy delegation is the SPF policy server
34at <a href="http://www.openspf.org/Software">http://www.openspf.org/Software</a>.  </p>
35
36<p> Policy delegation is now the preferred method for adding policies
37to Postfix. It's much easier to develop a new feature in few lines
38of Perl, Python, Ruby, or TCL, than trying to do the same in C code.
39The difference in
40performance will be unnoticeable except in the most demanding
41environments. On active systems a policy daemon process is used
42multiple times, for up to $<a href="postconf.5.html#max_use">max_use</a> incoming SMTP connections. </p>
43
44<p> This document covers the following topics: </p>
45
46<ul>
47
48<li><a href="#protocol">Policy protocol description</a>
49
50<li><a href="#client_config">Simple policy client/server configuration</a>
51
52<li><a href="#advanced">Advanced policy client configuration</a>
53
54<li><a href="#greylist">Example: greylist policy server</a>
55
56<li><a href="#frequent">Greylisting mail from frequently forged domains</a>
57
58<li><a href="#all_mail">Greylisting all your mail</a>
59
60<li><a href="#maintenance">Routine greylist maintenance</a>
61
62<li><a href="#greylist_code">Example Perl greylist server</a>
63
64</ul>
65
66<h2><a name="protocol">Protocol description</a></h2>
67
68<p> The Postfix policy delegation protocol is really simple. The
69client request is a sequence of name=value attributes separated by
70newline, and is terminated by an empty line. The server reply is
71one name=value attribute and it, too, is terminated by an empty
72line. </p>
73
74<p> Here is an example of all the attributes that the Postfix SMTP
75server sends in a delegated SMTPD access policy request: </p>
76
77<blockquote>
78<pre>
79<b>Postfix version 2.1 and later:</b>
80request=smtpd_access_policy
81protocol_state=RCPT
82protocol_name=SMTP
83helo_name=some.domain.tld
84queue_id=8045F2AB23
85sender=foo@bar.tld
86recipient=bar@foo.tld
87recipient_count=0
88client_address=1.2.3.4
89client_name=another.domain.tld
90reverse_client_name=another.domain.tld
91instance=123.456.7
92<b>Postfix version 2.2 and later:</b>
93sasl_method=plain
94sasl_username=you
95sasl_sender=
96size=12345
97ccert_subject=solaris9.porcupine.org
98ccert_issuer=Wietse+20Venema
99ccert_fingerprint=C2:9D:F4:87:71:73:73:D9:18:E7:C2:F3:C1:DA:6E:04
100<b>Postfix version 2.3 and later:</b>
101encryption_protocol=TLSv1/SSLv3
102encryption_cipher=DHE-RSA-AES256-SHA
103encryption_keysize=256
104etrn_domain=
105<b>Postfix version 2.5 and later:</b>
106stress=
107<b>Postfix version 2.9 and later:</b>
108ccert_pubkey_fingerprint=68:B3:29:DA:98:93:E3:40:99:C7:D8:AD:5C:B9:C9:40
109<b>Postfix version 3.0 and later:</b>
110client_port=1234
111<b>Postfix version 3.1 and later:</b>
112policy_context=submission
113[empty line]
114</pre>
115</blockquote>
116
117<p> Notes: </p>
118
119<ul>
120
121    <li> <p> The "request" attribute is required. In this example
122    the request type is "smtpd_access_policy". </p>
123
124    <li> <p> The order of the attributes does not matter. The policy
125    server should ignore any attributes that it does not care about.
126    </p>
127
128    <li> <p> When the same attribute name is sent more than once,
129    the server may keep the first value or the last attribute value.
130    </p>
131
132    <li> <p> When an attribute value is unavailable, the client
133    either does not send the attribute, sends the attribute with
134    an empty value ("name="), or sends a zero value ("name=0") in
135    the case of a numerical attribute. </p>
136
137    <li> <p> The "recipient" attribute is available in the "RCPT
138    TO" stage. It is also available in the "DATA" and "END-OF-MESSAGE"
139    stages if Postfix accepted only one recipient for the current
140    message.  </p>
141
142    <li> <p> The "recipient_count" attribute (Postfix 2.3 and later)
143    is non-zero only in the "DATA" and "END-OF-MESSAGE" stages. It
144    specifies the number of recipients that Postfix accepted for
145    the current message.  </p>
146
147    <li> <p> The client address is an IPv4 dotted quad in the form
148    1.2.3.4 or it is an IPv6 address in the form 1:2:3::4:5:6.
149    </p>
150
151    <li> <p> For a discussion of the differences between reverse
152    and verified client_name information, see the
153    <a href="postconf.5.html#reject_unknown_client_hostname">reject_unknown_client_hostname</a> discussion in the <a href="postconf.5.html">postconf(5)</a>
154    document.  </p>
155
156    <li> <p> An attribute name must not contain "=", null or newline,
157    and an attribute value must not contain null or newline. </p>
158
159    <li> <p> The "instance" attribute value can be used to correlate
160    different requests regarding the same message delivery. These
161    requests are sent over the same policy connection (unless the
162    policy daemon terminates the connection).  Once Postfix sends
163    a query with a different instance attribute over that same
164    policy connection, the previous message delivery is either
165    completed or aborted. </p>
166
167    <li> <p> The "size" attribute value specifies the message size
168    that the client specified in the MAIL FROM command (zero if
169    none was specified). With Postfix 2.2 and later, it specifies
170    the actual message size when the client sends the END-OF-DATA
171    command.
172    </p>
173
174    <li> <p> The "sasl_*" attributes (Postfix 2.2 and later) specify
175    information about how the client was authenticated via SASL.
176    These attributes are empty in case of no SASL authentication.
177    </p>
178
179    <li> <p> The "ccert_*" attributes (Postfix 2.2 and later) specify
180    information about how the client was authenticated via TLS.
181    These attributes are empty in case of no certificate authentication.
182    As of Postfix 2.2.11 these attribute values are encoded as
183    xtext: some characters are represented by +XX, where XX is the
184    two-digit hexadecimal representation of the character value. With
185    Postfix 2.6 and later, the decoded string is an UTF-8 string
186    without non-printable ASCII characters.  </p>
187
188    <li> <p> The "encryption_*" attributes (Postfix 2.3 and later)
189    specify information about how the connection is encrypted. With
190    plaintext connections the protocol and cipher attributes are
191    empty and the keysize is zero.  </p>
192
193    <li> <p> The "etrn_domain" attribute is defined only in the
194    context of the ETRN command, and specifies the ETRN command
195    parameter. </p>
196
197    <li> <p> The "stress" attribute is either empty or "yes".  See
198    the <a href="STRESS_README.html">STRESS_README</a> document for further information. </p>
199
200    <li> <p> The "policy_context" attribute provides a way to pass
201    information that is not available via other attributes (Postfix
202    version 3.1 and later). </p>
203
204</ul>
205
206<p> The following is specific to SMTPD delegated policy requests:
207</p>
208
209<ul>
210
211    <li> <p> Protocol names are ESMTP or SMTP. </p>
212
213    <li> <p> Protocol states are CONNECT, EHLO, HELO, MAIL, RCPT,
214    DATA, END-OF-MESSAGE, VRFY or ETRN; these are the SMTP protocol
215    states where
216    the Postfix SMTP server makes an OK/REJECT/HOLD/etc. decision.
217    </p>
218
219</ul>
220
221<p> The policy server replies with any action that is allowed in a
222Postfix SMTPD <a href="access.5.html">access(5)</a> table. Example: </p>
223
224<blockquote>
225<pre>
226action=<a href="postconf.5.html#defer_if_permit">defer_if_permit</a> Service temporarily unavailable
227[empty line]
228</pre>
229</blockquote>
230
231<p> This causes the Postfix SMTP server to reject the request with
232a 450 temporary error code and with text "Service temporarily
233unavailable", if the Postfix SMTP server finds no reason to reject
234the request permanently. </p>
235
236<p> In case of trouble the policy server must not send a reply.
237Instead the server must log a warning and disconnect.  Postfix will
238retry the request at some later time.  </p>
239
240<h2><a name="client_config">Simple policy client/server configuration</a></h2>
241
242<p> The Postfix delegated policy client can connect to a TCP socket
243or to a UNIX-domain socket. Examples: </p>
244
245<blockquote>
246<pre>
247inet:127.0.0.1:9998
248unix:/some/where/policy
249unix:private/policy
250</pre>
251</blockquote>
252
253<p> The first example specifies that the policy server listens on
254a TCP socket at 127.0.0.1 port 9998. The second example specifies
255an absolute pathname of a UNIX-domain socket. The third example
256specifies a pathname relative to the Postfix queue directory; use
257this for policy servers that are spawned by the Postfix master
258daemon. </p>
259
260<p> To create a policy service that listens on a UNIX-domain socket
261called "policy", and that runs under control of the Postfix <a href="spawn.8.html">spawn(8)</a>
262daemon, you would use something like this: </p>
263
264<blockquote>
265<pre>
266 1 /etc/postfix/<a href="master.5.html">master.cf</a>:
267 2     policy  unix  -       n       n       -       0       spawn
268 3       user=nobody argv=/some/where/policy-server
269 4
270 5 /etc/postfix/<a href="postconf.5.html">main.cf</a>:
271 6     <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> =
272 7         ...
273 8         <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>
274 9         <a href="postconf.5.html#check_policy_service">check_policy_service</a> unix:private/policy
27510         ...
27611     <a href="postconf.5.html#transport_time_limit">policy_time_limit</a> = 3600
27712     # <a href="postconf.5.html#smtpd_policy_service_request_limit">smtpd_policy_service_request_limit</a> = 1
278</pre>
279</blockquote>
280
281<p> NOTES: </p>
282
283<ul>
284
285<li> <p> Lines 2-3: this creates the service called "policy" that
286listens on a UNIX-domain socket. The service is implemented by the
287Postfix <a href="spawn.8.html">spawn(8)</a> daemon, which executes the policy server program
288that is specified with the <b>argv</b> attribute, using the privileges
289specified with the <b>user</b> attribute. </p>
290
291<li> <p> Line 2: specify a "0" process limit instead of the default
292"-", to avoid "connection refused" and other problems when you
293increase the smtpd process limit. </p>
294
295<li> <p> Line 8: <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> is not needed here if
296the mail relay policy is specified with <a href="postconf.5.html#smtpd_relay_restrictions">smtpd_relay_restrictions</a>
297(available with Postfix 2.10 and later). </p>
298
299<li> <p> Lines 8, 9: always specify "<a href="postconf.5.html#check_policy_service">check_policy_service</a>" AFTER
300"<a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>" or else your system could become an
301open relay. </p>
302
303<li> <p> Line 11: this increases the time that a policy server
304process may run to 3600 seconds.  The default time limit of 1000
305seconds is too short; the policy daemon needs to run long as the
306SMTP server process that talks to it.
307See the <a href="spawn.8.html">spawn(8)</a> manpage for more information about the
308<a href="postconf.5.html#transport_time_limit"><i>transport</i>_time_limit</a> parameter.  </p>
309
310<blockquote> <p> Note: the "<a href="postconf.5.html#transport_time_limit">policy_time_limit</a>" parameter will not
311show up in "postconf" command output before Postfix version 2.9.
312This limitation applies to many parameters whose name is a combination
313of a <a href="master.5.html">master.cf</a> service name (in the above example, "policy") and a
314built-in suffix (in the above example: "_time_limit").  </p>
315</blockquote>
316
317<li> <p> Line 12: specify <a href="postconf.5.html#smtpd_policy_service_request_limit">smtpd_policy_service_request_limit</a> to
318avoid error-recovery delays with policy servers that cannot
319maintain a persistent connection. </p>
320
321<li> <p> With Solaris &lt; 9, or Postfix &lt; 2.10 on any Solaris
322version, use TCP sockets instead of UNIX-domain sockets: </p>
323
324</ul>
325
326<blockquote>
327<pre>
328 1 /etc/postfix/<a href="master.5.html">master.cf</a>:
329 2     127.0.0.1:9998  inet  n       n       n       -       0       spawn
330 3       user=nobody argv=/some/where/policy-server
331 4
332 5 /etc/postfix/<a href="postconf.5.html">main.cf</a>:
333 6     <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> =
334 7         ...
335 8         <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>
336 9         <a href="postconf.5.html#check_policy_service">check_policy_service</a> inet:127.0.0.1:9998
33710         ...
33811     127.0.0.1:9998_time_limit = 3600
33912     # <a href="postconf.5.html#smtpd_policy_service_request_limit">smtpd_policy_service_request_limit</a> = 1
340</pre>
341</blockquote>
342
343<p> Configuration parameters that control the client side of the
344policy delegation protocol: </p>
345
346<ul>
347
348<li> <p> <a href="postconf.5.html#smtpd_policy_service_default_action">smtpd_policy_service_default_action</a> (default: 451 4.3.5
349Server configuration problem): The default action when an SMTPD
350policy service request fails.  Available with Postfix 3.0 and
351later. </p>
352
353<li> <p> <a href="postconf.5.html#smtpd_policy_service_max_idle">smtpd_policy_service_max_idle</a> (default: 300s): The amount
354of time before the Postfix SMTP server closes an unused policy
355client connection. </p>
356
357<li> <p> <a href="postconf.5.html#smtpd_policy_service_max_ttl">smtpd_policy_service_max_ttl</a> (default: 1000s): The amount
358of time before the Postfix SMTP server closes an active policy
359client connection. </p>
360
361<li> <p> <a href="postconf.5.html#smtpd_policy_service_request_limit">smtpd_policy_service_request_limit</a> (default: 0): The maximal
362number of requests per policy connection, or zero (no limit).
363Available with Postfix 3.0 and later. </p>
364
365<li> <p> <a href="postconf.5.html#smtpd_policy_service_timeout">smtpd_policy_service_timeout</a> (default: 100s): The time
366limit to connect to, send to or receive from a policy server. </p>
367
368<li> <p> <a href="postconf.5.html#smtpd_policy_service_try_limit">smtpd_policy_service_try_limit</a> (default: 2): The maximal
369number of attempts to send an SMTPD policy service request before
370giving up.  Available with Postfix 3.0 and later.  </p>
371
372<li> <p> <a href="postconf.5.html#smtpd_policy_service_retry_delay">smtpd_policy_service_retry_delay</a> (default: 1s): The delay
373between attempts to resend a failed SMTPD policy service request.
374Available with Postfix 3.0 and later.  </p>
375
376<li> <p> <a href="postconf.5.html#smtpd_policy_service_policy_context">smtpd_policy_service_policy_context</a> (default: empty):
377Optional information that is passed in the "policy_context" attribute
378of an SMTPD policy service request (originally, to share the same
379SMTPD service endpoint among multiple <a href="postconf.5.html#check_policy_service">check_policy_service</a> clients).
380Available with Postfix 3.1 and later.  </p>
381
382</ul>
383
384<p> Configuration parameters that control the server side of the
385policy delegation protocol: </p>
386
387<ul>
388
389<li> <p> <a href="postconf.5.html#transport_time_limit"><i>transport</i>_time_limit</a> ($<a href="postconf.5.html#command_time_limit">command_time_limit</a>): The
390maximal amount of time the policy daemon is allowed to run before
391it is terminated. The <i>transport</i> is the service name of the
392<a href="master.5.html">master.cf</a> entry for the policy daemon service. In the above
393examples, the service name is "policy" or "127.0.0.1:9998". </p>
394
395</ul>
396
397<h2><a name="advanced">Advanced policy client configuration</a></h2>
398
399<p> The previous section lists a number of Postfix <a href="postconf.5.html">main.cf</a> parameters
400that control time limits and other settings for all policy clients.
401This is sufficient for simple configurations. With more complex
402configurations it becomes desirable to have different settings per
403policy client. This is supported with Postfix 3.0 and later. </p>
404
405<p> The following example shows a "non-critical" policy service
406with a short timeout, and with "DUNNO" as default action when the
407service is unvailable. The "DUNNO" action causes Postfix to ignore
408the result. </p>
409
410<blockquote>
411<pre>
4121 /etc/postfix/<a href="postconf.5.html">main.cf</a>:
4132     <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> =
4143         ...
4154         <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>
4165         <a href="postconf.5.html#check_policy_service">check_policy_service</a> { inet:host:port,
4176             timeout=10s, default_action=DUNNO }
4188         ...
419</pre>
420</blockquote>
421
422<p> Instead of a server endpoint, we now have a list enclosed in {}. </p>
423
424<ul>
425
426<li> <p> Line 5: The first item in the list is the server endpoint.
427This supports the exact same "inet" and "unix" syntax as described
428earlier. </p>
429
430<li> <p> Line 6: The remainder of the list contains per-client
431settings. These settings override global <a href="postconf.5.html">main.cf</a> parameters,
432and have the same name as those parameters, without the
433"smtpd_policy_service_" prefix.  </p>
434
435</ul>
436
437<p> Inside the list, syntax is similar to what we already know from
438<a href="postconf.5.html">main.cf</a>: items separated by space or comma. There is one difference:
439<b>you must enclose a setting in parentheses, as in "{ name = value
440}", if you want to have space or comma within a value or around
441"="</b>.  This comes in handy when different policy servers require
442different default actions with different SMTP status codes or text:
443</p>
444
445<blockquote>
446<pre>
4471 /etc/postfix/<a href="postconf.5.html">main.cf</a>:
4482     <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> =
4493         ...
4504         <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>
4515         <a href="postconf.5.html#check_policy_service">check_policy_service</a> {
4526           inet:host:port1,
4537           { default_action = 451 4.3.5 See <a href="http://www.example.com/support1">http://www.example.com/support1</a> }
4548         }
4559         ...
456</pre>
457</blockquote>
458
459<h2><a name="greylist">Example: greylist policy server</a></h2>
460
461<p> Greylisting is a defense against junk email that is described at
462<a href="http://www.greylisting.org/">http://www.greylisting.org/</a>. The idea was discussed on the
463postfix-users mailing list <a
464href="http://archives.neohapsis.com/archives/postfix/2002-03/0846.html">
465one year before it was popularized</a>. </p>
466
467<p> The file examples/smtpd-policy/greylist.pl in the Postfix source
468tree implements a simplified greylist policy server. This server
469stores a time stamp for every (client, sender, recipient) triple.
470By default, mail is not accepted until a time stamp is more than
47160 seconds old. This stops junk mail with randomly selected sender
472addresses, and mail that is sent through randomly selected open
473proxies. It also stops junk mail from spammers that change their
474IP address frequently. </p>
475
476<p> Copy examples/smtpd-policy/greylist.pl to /usr/libexec/postfix
477or whatever location is appropriate for your system. </p>
478
479<p> In the greylist.pl Perl script you need to specify the
480location of the greylist database file, and how long mail will
481be delayed before it is accepted. The default settings are:
482</p>
483
484<blockquote>
485<pre>
486$database_name="/var/mta/greylist.db";
487$greylist_delay=60;
488</pre>
489</blockquote>
490
491<p> The /var/mta directory (or whatever you choose) should be
492writable by "nobody", or by whatever username you configure below
493in <a href="master.5.html">master.cf</a> for the policy service. </p>
494
495<p> Example: </p>
496
497<blockquote>
498<pre>
499# mkdir /var/mta
500# chown nobody /var/mta
501</pre>
502</blockquote>
503
504<p> Note: DO NOT create the greylist database in a world-writable
505directory such as /tmp or /var/tmp, and DO NOT create the greylist
506database in a file system that may run out of space. Postfix can
507survive "out of space" conditions with the mail queue and with the
508mailbox store, but it cannot survive a corrupted greylist database.
509If the file becomes corrupted you may not be able to receive mail
510at all until you delete the file by hand. </p>
511
512<p> The greylist.pl Perl script can be run under control by
513the Postfix master daemon.  For example, to run the script as user
514"nobody", using a UNIX-domain socket that is accessible by Postfix
515processes only: </p>
516
517<blockquote>
518<pre>
519 1 /etc/postfix/<a href="master.5.html">master.cf</a>:
520 2     greylist  unix  -       n       n       -       0       spawn
521 3       user=nobody argv=/usr/bin/perl /usr/libexec/postfix/greylist.pl
522 4
523 5 /etc/postfix/<a href="postconf.5.html">main.cf</a>:
524 6     <a href="postconf.5.html#transport_time_limit">greylist_time_limit</a> = 3600
525 7     <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> =
526 8         ...
527 9         <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>
52810         <a href="postconf.5.html#check_policy_service">check_policy_service</a> unix:private/greylist
52911         ...
53012     # <a href="postconf.5.html#smtpd_policy_service_request_limit">smtpd_policy_service_request_limit</a> = 1
531</pre>
532</blockquote>
533
534<p> Notes: </p>
535
536<ul>
537
538<li> <p> Lines 2-3: this creates the service called "greylist" that
539listens on a UNIX-domain socket. The service is implemented by the
540Postfix <a href="spawn.8.html">spawn(8)</a> daemon, which executes the greylist.pl script that
541is specified with the <b>argv</b> attribute, using the privileges
542specified with the <b>user</b> attribute. </p>
543
544<li> <p> Line 2: specify a "0" process limit instead of the default
545"-", to avoid "connection refused" and other problems when you
546increase the smtpd process limit. </p>
547
548<li> <p> Line 3: Specify "greylist.pl -v" for verbose logging of
549each request and reply. </p>
550
551<li> <p> Line 6: this increases the time that a greylist server
552process may run to 3600 seconds.  The default time limit of 1000
553seconds is too short; the greylist daemon needs to run long as the
554SMTP server process that talks to it.
555See the <a href="spawn.8.html">spawn(8)</a> manpage for more information about the
556<a href="postconf.5.html#transport_time_limit"><i>transport</i>_time_limit</a> parameter.  </p>
557
558<li> <p> Line 9: <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> is not needed here if
559the mail relay policy is specified with <a href="postconf.5.html#smtpd_relay_restrictions">smtpd_relay_restrictions</a>
560(available with Postfix 2.10 and later). </p>
561
562<blockquote> <p> Note: the "<a href="postconf.5.html#transport_time_limit">greylist_time_limit</a>" parameter will not
563show up in "postconf" command output before Postfix version 2.9.
564This limitation applies to many parameters whose name is a combination
565of a <a href="master.5.html">master.cf</a> service name (in the above example, "greylist") and
566a built-in suffix (in the above example: "_time_limit").  </p>
567</blockquote>
568
569<li> <p> Line 12: specify <a href="postconf.5.html#smtpd_policy_service_request_limit">smtpd_policy_service_request_limit</a> to
570avoid error-recovery delays with policy servers that cannot
571maintain a persistent connection. </p>
572
573</ul>
574
575<p> With Solaris &lt; 9, or Postfix &lt; 2.10 on any Solaris
576version, use inet: style sockets instead of unix:
577style, as detailed in the "<a href="#client_config">Policy
578client/server configuration</a>" section above.  </p>
579
580<blockquote>
581<pre>
582 1 /etc/postfix/<a href="master.5.html">master.cf</a>:
583 2     127.0.0.1:9998  inet  n       n       n       -       0       spawn
584 3       user=nobody argv=/usr/bin/perl /usr/libexec/postfix/greylist.pl
585 4
586 5 /etc/postfix/<a href="postconf.5.html">main.cf</a>:
587 6     127.0.0.1:9998_time_limit = 3600
588 7     <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> =
589 8         ...
590 9         <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>
59110         <a href="postconf.5.html#check_policy_service">check_policy_service</a> inet:127.0.0.1:9998
59211         ...
59312     # <a href="postconf.5.html#smtpd_policy_service_request_limit">smtpd_policy_service_request_limit</a> = 1
594</pre>
595</blockquote>
596
597<h2><a name="frequent">Greylisting mail from frequently forged domains</a></h2>
598
599<p> It is relatively safe to turn on greylisting for specific
600domains that often appear in forged email.  At some point
601in cyberspace/time a list of frequently
602forged MAIL FROM domains could be found at
603<a href="http://www.monkeys.com/anti-spam/filtering/sender-domain-validate.in">http://www.monkeys.com/anti-spam/filtering/sender-domain-validate.in</a>.
604
605<blockquote>
606<pre>
607 1 /etc/postfix/<a href="postconf.5.html">main.cf</a>:
608 2     <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> =
609 3         <a href="postconf.5.html#reject_unlisted_recipient">reject_unlisted_recipient</a>
610 4         ...
611 5         <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>
612 6         <a href="postconf.5.html#check_sender_access">check_sender_access</a> <a href="DATABASE_README.html#types">hash</a>:/etc/postfix/sender_access
613 7         ...
614 8     <a href="postconf.5.html#smtpd_restriction_classes">smtpd_restriction_classes</a> = greylist
615 9     greylist = <a href="postconf.5.html#check_policy_service">check_policy_service</a> unix:private/greylist
61610
61711 /etc/postfix/sender_access:
61812     aol.com     greylist
61913     hotmail.com greylist
62014     bigfoot.com greylist
62115     ... <i>etcetera</i> ...
622</pre>
623</blockquote>
624
625<p> NOTES: </p>
626
627<ul>
628
629<li> <p> Line 9: On Solaris &lt; 9, or Postfix &lt; 2.10 on any
630Solaris version, use inet: style sockets
631instead of unix: style, as detailed in the "<a href="#greylist">Example:
632greylist policy server</a>" section above.  </p>
633
634<li> <p> Line 5: <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> is not needed here if
635the mail relay policy is specified with <a href="postconf.5.html#smtpd_relay_restrictions">smtpd_relay_restrictions</a>
636(available with Postfix 2.10 and later). </p>
637
638<li> <p> Line 6: Be sure to specify "<a href="postconf.5.html#check_sender_access">check_sender_access</a>" AFTER
639"<a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>" or else your system could become an
640open mail relay. </p>
641
642<li> <p> Line 3: With Postfix 2.0 snapshot releases,
643"<a href="postconf.5.html#reject_unlisted_recipient">reject_unlisted_recipient</a>" is called "check_recipient_maps".
644Postfix 2.1 understands both forms. </p>
645
646<li> <p> Line 3: The greylist database gets polluted quickly with
647bogus addresses.  It helps if you protect greylist lookups with
648other restrictions that reject unknown senders and/or recipients.
649</p>
650
651</ul>
652
653<h2><a name="all_mail">Greylisting all your mail</a></h2>
654
655<p> If you turn on greylisting for all mail you may want to make
656exceptions for mailing lists that use one-time sender addresses,
657because each message will be delayed due to greylisting, and the
658one-time sender addresses can pollute your greylist database
659relatively quickly. Instead of making exceptions, you can automatically
660whitelist clients that survive greylisting repeatedly; this avoids
661most of the delays and most of the database pollution problem. </p>
662
663<blockquote>
664<pre>
665 1 /etc/postfix/<a href="postconf.5.html">main.cf</a>:
666 2     <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> =
667 3         <a href="postconf.5.html#reject_unlisted_recipient">reject_unlisted_recipient</a>
668 4         ...
669 5         <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>
670 6         <a href="postconf.5.html#check_sender_access">check_sender_access</a> <a href="DATABASE_README.html#types">hash</a>:/etc/postfix/sender_access
671 7         <a href="postconf.5.html#check_policy_service">check_policy_service</a> unix:private/policy
672 8         ...
673 9
67410 /etc/postfix/sender_access:
67511     securityfocus.com OK
67612     ...
677</pre>
678</blockquote>
679
680<p> NOTES: </p>
681
682<ul>
683
684<li> <p> Line 7: On Solaris &lt; 9, or Postfix &lt; 2.10 on any
685Solaris version, use inet: style sockets
686instead of unix: style, as detailed in the "<a href="#greylist">Example:
687greylist policy server</a>" section above.  </p>
688
689<li> <p> Line 5: <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> is not needed here if
690the mail relay policy is specified with <a href="postconf.5.html#smtpd_relay_restrictions">smtpd_relay_restrictions</a>
691(available with Postfix 2.10 and later). </p>
692
693<li> <p> Lines 6-7: Be sure to specify <a href="postconf.5.html#check_sender_access">check_sender_access</a> and
694<a href="postconf.5.html#check_policy_service">check_policy_service</a> AFTER <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> or else your
695system could become an open mail relay. </p>
696
697<li> <p> Line 3: The greylist database gets polluted quickly with
698bogus addresses.  It helps if you precede greylist lookups with
699restrictions that reject unknown senders and/or recipients. </p>
700
701</ul>
702
703<h2><a name="maintenance">Routine greylist maintenance</a></h2>
704
705<p> The greylist database grows over time, because the greylist server
706never removes database entries. If left unattended, the greylist
707database will eventually run your file system out of space. </p>
708
709<p> When the status file size exceeds some threshold you can simply
710rename or remove the file without adverse effects; Postfix
711automatically creates a new file. In the worst case, new mail will
712be delayed by an hour or so. To lessen the impact, rename or remove
713the file in the middle of the night at the beginning of a weekend.
714</p>
715
716<h2><a name="greylist_code">Example Perl greylist server</a></h2>
717
718<p> This is the Perl subroutine that implements the example greylist
719policy.  It is part of a general purpose sample policy server that
720is distributed with the Postfix source as
721examples/smtpd-policy/greylist.pl. </p>
722
723<pre>
724#
725# greylist status database and greylist time interval. DO NOT create the
726# greylist status database in a world-writable directory such as /tmp
727# or /var/tmp. DO NOT create the greylist database in a file system
728# that can run out of space.
729#
730$database_name="/var/mta/greylist.db";
731$greylist_delay=60;
732
733#
734# Auto-whitelist threshold. Specify 0 to disable, or the number of
735# successful "come backs" after which a client is no longer subject
736# to greylisting.
737#
738$auto_whitelist_threshold = 10;
739
740#
741# Demo SMTPD access policy routine. The result is an action just like
742# it would be specified on the right-hand side of a Postfix access
743# table.  Request attributes are available via the %attr hash.
744#
745sub smtpd_access_policy {
746    my($key, $time_stamp, $now);
747
748    # Open the database on the fly.
749    open_database() unless $database_obj;
750
751    # Search the auto-whitelist.
752    if ($auto_whitelist_threshold &gt; 0) {
753        $count = read_database($attr{"client_address"});
754        if ($count &gt; $auto_whitelist_threshold) {
755            return "dunno";
756        }
757    }
758
759    # Lookup the time stamp for this client/sender/recipient.
760    $key =
761        lc $attr{"client_address"}."/".$attr{"sender"}."/".$attr{"recipient"};
762    $time_stamp = read_database($key);
763    $now = time();
764
765    # If new request, add this client/sender/recipient to the database.
766    if ($time_stamp == 0) {
767        $time_stamp = $now;
768        update_database($key, $time_stamp);
769    }
770
771    # The result can be any action that is allowed in a Postfix <a href="access.5.html">access(5)</a> map.
772    #
773    # To label the mail, return ``PREPEND headername: headertext''
774    #
775    # In case of success, return ``DUNNO'' instead of ``OK'', so that the
776    # <a href="postconf.5.html#check_policy_service">check_policy_service</a> restriction can be followed by other restrictions.
777    #
778    # In case of failure, return ``DEFER_IF_PERMIT optional text...'',
779    # so that mail can still be blocked by other access restrictions.
780    #
781    syslog $syslog_priority, "request age %d", $now - $time_stamp if $verbose;
782    if ($now - $time_stamp &gt; $greylist_delay) {
783        # Update the auto-whitelist.
784        if ($auto_whitelist_threshold &gt; 0) {
785            update_database($attr{"client_address"}, $count + 1);
786        }
787        return "dunno";
788    } else {
789        return "<a href="postconf.5.html#defer_if_permit">defer_if_permit</a> Service temporarily unavailable";
790    }
791}
792</pre>
793
794</body>
795
796</html>
797