1 P
\bPo
\bos
\bst
\btf
\bfi
\bix
\bx S
\bSM
\bMT
\bTP
\bP A
\bAc
\bcc
\bce
\bes
\bss
\bs P
\bPo
\bol
\bli
\bic
\bcy
\by D
\bDe
\bel
\ble
\beg
\bga
\bat
\bti
\bio
\bon
\bn
3 -------------------------------------------------------------------------------
5 P
\bPu
\bur
\brp
\bpo
\bos
\bse
\be o
\bof
\bf P
\bPo
\bos
\bst
\btf
\bfi
\bix
\bx S
\bSM
\bMT
\bTP
\bP a
\bac
\bcc
\bce
\bes
\bss
\bs p
\bpo
\bol
\bli
\bic
\bcy
\by d
\bde
\bel
\ble
\beg
\bga
\bat
\bti
\bio
\bon
\bn
7 The Postfix SMTP server has a number of built-in mechanisms to block or accept
8 mail at specific SMTP protocol stages. As of version 2.1, Postfix can delegate
9 policy decisions to an external server that runs outside Postfix.
11 With this policy delegation mechanism, a simple greylist policy can be
12 implemented with only a dozen lines of Perl, as is shown at the end of this
13 document. A complete example can be found in the Postfix source code, in the
14 directory examples/smtpd-policy.
16 Another example of policy delegation is the SPF policy server at http://
17 www.openspf.org/Software.
19 Policy delegation is now the preferred method for adding policies to Postfix.
20 It's much easier to develop a new feature in few lines of Perl, Python, Ruby,
21 or TCL, than trying to do the same in C code. The difference in performance
22 will be unnoticeable except in the most demanding environments. On active
23 systems a policy daemon process is used multiple times, for up to $max_use
24 incoming SMTP connections.
26 This document covers the following topics:
28 * Policy protocol description
29 * Policy client/server configuration
30 * Example: greylist policy server
31 * Greylisting mail from frequently forged domains
32 * Greylisting all your mail
33 * Routine greylist maintenance
34 * Example Perl greylist server
36 P
\bPr
\bro
\bot
\bto
\boc
\bco
\bol
\bl d
\bde
\bes
\bsc
\bcr
\bri
\bip
\bpt
\bti
\bio
\bon
\bn
38 The Postfix policy delegation protocol is really simple. The client request is
39 a sequence of name=value attributes separated by newline, and is terminated by
40 an empty line. The server reply is one name=value attribute and it, too, is
41 terminated by an empty line.
43 Here is an example of all the attributes that the Postfix SMTP server sends in
44 a delegated SMTPD access policy request:
46 P
\bPo
\bos
\bst
\btf
\bfi
\bix
\bx v
\bve
\ber
\brs
\bsi
\bio
\bon
\bn 2
\b2.
\b.1
\b1 a
\ban
\bnd
\bd l
\bla
\bat
\bte
\ber
\br:
\b:
47 request=smtpd_access_policy
50 helo_name=some.domain.tld
55 client_address=1.2.3.4
56 client_name=another.domain.tld
57 reverse_client_name=another.domain.tld
59 P
\bPo
\bos
\bst
\btf
\bfi
\bix
\bx v
\bve
\ber
\brs
\bsi
\bio
\bon
\bn 2
\b2.
\b.2
\b2 a
\ban
\bnd
\bd l
\bla
\bat
\bte
\ber
\br:
\b:
64 ccert_subject=solaris9.porcupine.org
65 ccert_issuer=Wietse+20Venema
66 ccert_fingerprint=C2:9D:F4:87:71:73:73:D9:18:E7:C2:F3:C1:DA:6E:04
67 P
\bPo
\bos
\bst
\btf
\bfi
\bix
\bx v
\bve
\ber
\brs
\bsi
\bio
\bon
\bn 2
\b2.
\b.3
\b3 a
\ban
\bnd
\bd l
\bla
\bat
\bte
\ber
\br:
\b:
68 encryption_protocol=TLSv1/SSLv3
69 encryption_cipher=DHE-RSA-AES256-SHA
70 encryption_keysize=256
72 P
\bPo
\bos
\bst
\btf
\bfi
\bix
\bx v
\bve
\ber
\brs
\bsi
\bio
\bon
\bn 2
\b2.
\b.5
\b5 a
\ban
\bnd
\bd l
\bla
\bat
\bte
\ber
\br:
\b:
78 * The "request" attribute is required. In this example the request type is
79 "smtpd_access_policy".
81 * The order of the attributes does not matter. The policy server should
82 ignore any attributes that it does not care about.
84 * When the same attribute name is sent more than once, the server may keep
85 the first value or the last attribute value.
87 * When an attribute value is unavailable, the client either does not send the
88 attribute, sends the attribute with an empty value ("name="), or sends a
89 zero value ("name=0") in the case of a numerical attribute.
91 * The "recipient" attribute is available in the "RCPT TO" stage. It is also
92 available in the "DATA" and "END-OF-MESSAGE" stages if Postfix accepted
93 only one recipient for the current message.
95 * The "recipient_count" attribute (Postfix 2.3 and later) is non-zero only in
96 the "DATA" and "END-OF-MESSAGE" stages. It specifies the number of
97 recipients that Postfix accepted for the current message.
99 * The client address is an IPv4 dotted quad in the form 1.2.3.4 or it is an
100 IPv6 address in the form 1:2:3::4:5:6.
102 * For a discussion of the differences between reverse and verified
103 client_name information, see the reject_unknown_client_hostname discussion
104 in the postconf(5) document.
106 * An attribute name must not contain "=", null or newline, and an attribute
107 value must not contain null or newline.
109 * The "instance" attribute value can be used to correlate different requests
110 regarding the same message delivery. These requests are sent over the same
111 policy connection (unless the policy daemon terminates the connection).
112 Once Postfix sends a query with a different instance attribute over that
113 same policy connection, the previous message delivery is either completed
116 * The "size" attribute value specifies the message size that the client
117 specified in the MAIL FROM command (zero if none was specified). With
118 Postfix 2.2 and later, it specifies the actual message size when the client
119 sends the END-OF-DATA command.
121 * The "sasl_*" attributes (Postfix 2.2 and later) specify information about
122 how the client was authenticated via SASL. These attributes are empty in
123 case of no SASL authentication.
125 * The "ccert_*" attributes (Postfix 2.2 and later) specify information about
126 how the client was authenticated via TLS. These attributes are empty in
127 case of no certificate authentication. As of Postfix 2.2.11 these attribute
128 values are encoded as xtext: some characters are represented by +XX, where
129 XX is the two-digit hexadecimal representation of the character value. With
130 Postfix 2.6 and later, the decoded string is an UTF-8 string without non-
131 printable ASCII characters.
133 * The "encryption_*" attributes (Postfix 2.3 and later) specify information
134 about how the connection is encrypted. With plaintext connections the
135 protocol and cipher attributes are empty and the keysize is zero.
137 * The "etrn_domain" attribute is defined only in the context of the ETRN
138 command, and specifies the ETRN command parameter.
140 * The "stress" attribute is either empty or "yes". See the STRESS_README
141 document for further information.
143 The following is specific to SMTPD delegated policy requests:
145 * Protocol names are ESMTP or SMTP.
147 * Protocol states are CONNECT, EHLO, HELO, MAIL, RCPT, DATA, END-OF-MESSAGE,
148 VRFY or ETRN; these are the SMTP protocol states where the Postfix SMTP
149 server makes an OK/REJECT/HOLD/etc. decision.
151 The policy server replies with any action that is allowed in a Postfix SMTPD
152 access(5) table. Example:
154 action=defer_if_permit Service temporarily unavailable
157 This causes the Postfix SMTP server to reject the request with a 450 temporary
158 error code and with text "Service temporarily unavailable", if the Postfix SMTP
159 server finds no reason to reject the request permanently.
161 In case of trouble the policy server must not send a reply. Instead the server
162 must log a warning and disconnect. Postfix will retry the request at some later
165 P
\bPo
\bol
\bli
\bic
\bcy
\by c
\bcl
\bli
\bie
\ben
\bnt
\bt/
\b/s
\bse
\ber
\brv
\bve
\ber
\br c
\bco
\bon
\bnf
\bfi
\big
\bgu
\bur
\bra
\bat
\bti
\bio
\bon
\bn
167 The Postfix delegated policy client can connect to a TCP socket or to a UNIX-
168 domain socket. Examples:
171 unix:/some/where/policy
174 The first example specifies that the policy server listens on a TCP socket at
175 127.0.0.1 port 9998. The second example specifies an absolute pathname of a
176 UNIX-domain socket. The third example specifies a pathname relative to the
177 Postfix queue directory; use this for policy servers that are spawned by the
178 Postfix master daemon.
180 To create a policy service that listens on a UNIX-domain socket called
181 "policy", and that runs under control of the Postfix spawn(8) daemon, you would
182 use something like this:
184 1 /etc/postfix/master.cf:
185 2 policy unix - n n - 0 spawn
186 3 user=nobody argv=/some/where/policy-server
188 5 /etc/postfix/main.cf:
189 6 smtpd_recipient_restrictions =
191 8 reject_unauth_destination
192 9 check_policy_service unix:private/policy
194 11 policy_time_limit = 3600
198 * Lines 2, 11: the Postfix spawn(8) daemon by default kills its child process
199 after 1000 seconds. This is too short for a policy daemon that may need to
200 run for as long as the SMTP server process that talks to it. The default
201 time limit is overruled in main.cf with an explicit "policy_time_limit"
202 setting. The name of the parameter is the name of the master.cf entry
203 ("policy") concatenated with the "_time_limit" suffix. See spawn(8) for
204 more information about the time limit parameter.
206 * Line 2: specify a "0" process limit instead of the default "-", to avoid
207 "connection refused" and other problems when the smtpd process limit
208 exceeds the default_process_limit setting.
210 * Lines 8, 9: always specify "check_policy_service" AFTER
211 "reject_unauth_destination" or else your system could become an open relay.
213 * Solaris UNIX-domain sockets do not work reliably. Use TCP sockets instead:
215 1 /etc/postfix/master.cf:
216 2 127.0.0.1:9998 inet n n n - 0 spawn
217 3 user=nobody argv=/some/where/policy-server
219 5 /etc/postfix/main.cf:
220 6 smtpd_recipient_restrictions =
222 8 reject_unauth_destination
223 9 check_policy_service inet:127.0.0.1:9998
225 11 127.0.0.1:9998_time_limit = 3600
227 Other configuration parameters that control the client side of the policy
230 * smtpd_policy_service_max_idle (default: 300s): The amount of time before
231 the Postfix SMTP server closes an unused policy client connection.
233 * smtpd_policy_service_max_ttl (default: 1000s): The amount of time before
234 the Postfix SMTP server closes an active policy client connection.
236 * smtpd_policy_service_timeout (default: 100s): The time limit to connect to,
237 send to or receive from a policy server.
239 E
\bEx
\bxa
\bam
\bmp
\bpl
\ble
\be:
\b: g
\bgr
\bre
\bey
\byl
\bli
\bis
\bst
\bt p
\bpo
\bol
\bli
\bic
\bcy
\by s
\bse
\ber
\brv
\bve
\ber
\br
241 Greylisting is a defense against junk email that is described at http://
242 www.greylisting.org/. The idea was discussed on the postfix-users mailing list
243 one year before it was popularized.
245 The file examples/smtpd-policy/greylist.pl in the Postfix source tree
246 implements a simplified greylist policy server. This server stores a time stamp
247 for every (client, sender, recipient) triple. By default, mail is not accepted
248 until a time stamp is more than 60 seconds old. This stops junk mail with
249 randomly selected sender addresses, and mail that is sent through randomly
250 selected open proxies. It also stops junk mail from spammers that change their
251 IP address frequently.
253 Copy examples/smtpd-policy/greylist.pl to /usr/libexec/postfix or whatever
254 location is appropriate for your system.
256 In the greylist.pl Perl script you need to specify the location of the greylist
257 database file, and how long mail will be delayed before it is accepted. The
258 default settings are:
260 $database_name="/var/mta/greylist.db";
263 The /var/mta directory (or whatever you choose) should be writable by "nobody",
264 or by whatever username you configure below in master.cf for the policy
270 # chown nobody /var/mta
272 Note: DO NOT create the greylist database in a world-writable directory such as
273 /tmp or /var/tmp, and DO NOT create the greylist database in a file system that
274 may run out of space. Postfix can survive "out of space" conditions with the
275 mail queue and with the mailbox store, but it cannot survive a corrupted
276 greylist database. If the file becomes corrupted you may not be able to receive
277 mail at all until you delete the file by hand.
279 The greylist.pl Perl script can be run under control by the Postfix master
280 daemon. For example, to run the script as user "nobody", using a UNIX-domain
281 socket that is accessible by Postfix processes only:
283 1 /etc/postfix/master.cf:
284 2 policy unix - n n - 0 spawn
285 3 user=nobody argv=/usr/bin/perl /usr/libexec/postfix/greylist.pl
287 5 /etc/postfix/main.cf:
288 6 policy_time_limit = 3600
292 * Line 3: Specify "greylist.pl -v" for verbose logging of each request and
295 * Lines 2, 6: the Postfix spawn(8) daemon by default kills its child process
296 after 1000 seconds. This is too short for a policy daemon that may run for
297 as long as an SMTP client is connected to an SMTP server process. The
298 default time limit is overruled in main.cf with an explicit
299 "policy_time_limit" setting. The name of the parameter is the name of the
300 master.cf entry ("policy") concatenated with the "_time_limit" suffix.
302 * Line 2: specify a "0" process limit instead of the default "-", to avoid
303 "connection refused" and other problems when the smtpd process limit
304 exceeds the default_process_limit setting.
306 On Solaris you must use inet: style sockets instead of unix: style, as detailed
307 in the "Policy client/server configuration" section above.
309 1 /etc/postfix/master.cf:
310 2 127.0.0.1:9998 inet n n n - 0 spawn
311 3 user=nobody argv=/usr/bin/perl /usr/libexec/postfix/greylist.pl
313 5 /etc/postfix/main.cf:
314 6 127.0.0.1:9998_time_limit = 3600
316 To invoke this service you would specify "check_policy_service inet:127.0.0.1:
319 G
\bGr
\bre
\bey
\byl
\bli
\bis
\bst
\bti
\bin
\bng
\bg m
\bma
\bai
\bil
\bl f
\bfr
\bro
\bom
\bm f
\bfr
\bre
\beq
\bqu
\bue
\ben
\bnt
\btl
\bly
\by f
\bfo
\bor
\brg
\bge
\bed
\bd d
\bdo
\bom
\bma
\bai
\bin
\bns
\bs
321 It is relatively safe to turn on greylisting for specific domains that often
322 appear in forged email. At some point in cyberspace/time a list of frequently
323 forged MAIL FROM domains could be found at http://www.monkeys.com/anti-spam/
324 filtering/sender-domain-validate.in.
326 1 /etc/postfix/main.cf:
327 2 smtpd_recipient_restrictions =
328 3 reject_unlisted_recipient
330 5 reject_unauth_destination
331 6 check_sender_access hash:/etc/postfix/sender_access
333 8 smtpd_restriction_classes = greylist
334 9 greylist = check_policy_service unix:private/policy
336 11 /etc/postfix/sender_access:
338 13 hotmail.com greylist
339 14 bigfoot.com greylist
344 * Line 9: On Solaris you must use inet: style sockets instead of unix: style,
345 as detailed in the "Example: greylist policy server" section above.
347 * Line 6: Be sure to specify "check_sender_access" AFTER
348 "reject_unauth_destination" or else your system could become an open mail
351 * Line 3: With Postfix 2.0 snapshot releases, "reject_unlisted_recipient" is
352 called "check_recipient_maps". Postfix 2.1 understands both forms.
354 * Line 3: The greylist database gets polluted quickly with bogus addresses.
355 It helps if you protect greylist lookups with other restrictions that
356 reject unknown senders and/or recipients.
358 G
\bGr
\bre
\bey
\byl
\bli
\bis
\bst
\bti
\bin
\bng
\bg a
\bal
\bll
\bl y
\byo
\bou
\bur
\br m
\bma
\bai
\bil
\bl
360 If you turn on greylisting for all mail you will almost certainly want to make
361 exceptions for mailing lists that use one-time sender addresses, because such
362 mailing lists can pollute your greylist database relatively quickly.
364 1 /etc/postfix/main.cf:
365 2 smtpd_recipient_restrictions =
366 3 reject_unlisted_recipient
368 5 reject_unauth_destination
369 6 check_sender_access hash:/etc/postfix/sender_access
370 7 check_policy_service unix:private/policy
373 10 /etc/postfix/sender_access:
374 11 securityfocus.com OK
379 * Line 7: On Solaris you must use inet: style sockets instead of unix: style,
380 as detailed in the "Example: greylist policy server" section above.
382 * Lines 6-7: Be sure to specify check_sender_access and check_policy_service
383 AFTER reject_unauth_destination or else your system could become an open
386 * Line 3: The greylist database gets polluted quickly with bogus addresses.
387 It helps if you precede greylist lookups with restrictions that reject
388 unknown senders and/or recipients.
390 R
\bRo
\bou
\but
\bti
\bin
\bne
\be g
\bgr
\bre
\bey
\byl
\bli
\bis
\bst
\bt m
\bma
\bai
\bin
\bnt
\bte
\ben
\bna
\ban
\bnc
\bce
\be
392 The greylist database grows over time, because the greylist server never
393 removes database entries. If left unattended, the greylist database will
394 eventually run your file system out of space.
396 When the status file size exceeds some threshold you can simply rename or
397 remove the file without adverse effects; Postfix automatically creates a new
398 file. In the worst case, new mail will be delayed by an hour or so. To lessen
399 the impact, rename or remove the file in the middle of the night at the
400 beginning of a weekend.
402 E
\bEx
\bxa
\bam
\bmp
\bpl
\ble
\be P
\bPe
\ber
\brl
\bl g
\bgr
\bre
\bey
\byl
\bli
\bis
\bst
\bt s
\bse
\ber
\brv
\bve
\ber
\br
404 This is the Perl subroutine that implements the example greylist policy. It is
405 part of a general purpose sample policy server that is distributed with the
406 Postfix source as examples/smtpd-policy/greylist.pl.
409 # greylist status database and greylist time interval. DO NOT create the
410 # greylist status database in a world-writable directory such as /tmp
411 # or /var/tmp. DO NOT create the greylist database in a file system
412 # that can run out of space.
414 $database_name="/var/mta/greylist.db";
418 # Auto-whitelist threshold. Specify 0 to disable, or the number of
419 # successful "come backs" after which a client is no longer subject
422 $auto_whitelist_threshold = 10;
425 # Demo SMTPD access policy routine. The result is an action just like
426 # it would be specified on the right-hand side of a Postfix access
427 # table. Request attributes are available via the %attr hash.
429 sub smtpd_access_policy {
430 my($key, $time_stamp, $now);
432 # Open the database on the fly.
433 open_database() unless $database_obj;
435 # Search the auto-whitelist.
436 if ($auto_whitelist_threshold > 0) {
437 $count = read_database($attr{"client_address"});
438 if ($count > $auto_whitelist_threshold) {
443 # Lookup the time stamp for this client/sender/recipient.
445 lc $attr{"client_address"}."/".$attr{"sender"}."/".$attr{"recipient"};
446 $time_stamp = read_database($key);
449 # If new request, add this client/sender/recipient to the database.
450 if ($time_stamp == 0) {
452 update_database($key, $time_stamp);
455 # The result can be any action that is allowed in a Postfix access(5) map.
457 # To label the mail, return ``PREPEND headername: headertext''
459 # In case of success, return ``DUNNO'' instead of ``OK'', so that the
460 # check_policy_service restriction can be followed by other restrictions.
462 # In case of failure, return ``DEFER_IF_PERMIT optional text...'',
463 # so that mail can still be blocked by other access restrictions.
465 syslog $syslog_priority, "request age %d", $now - $time_stamp if $verbose;
466 if ($now - $time_stamp > $greylist_delay) {
467 # Update the auto-whitelist.
468 if ($auto_whitelist_threshold > 0) {
469 update_database($attr{"client_address"}, $count + 1);
473 return "defer_if_permit Service temporarily unavailable";