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