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">Policy client/server configuration</a> 51 52<li><a href="#greylist">Example: greylist policy server</a> 53 54<li><a href="#frequent">Greylisting mail from frequently forged domains</a> 55 56<li><a href="#all_mail">Greylisting all your mail</a> 57 58<li><a href="#maintenance">Routine greylist maintenance</a> 59 60<li><a href="#greylist_code">Example Perl greylist server</a> 61 62</ul> 63 64<h2><a name="protocol">Protocol description</a></h2> 65 66<p> The Postfix policy delegation protocol is really simple. The 67client request is a sequence of name=value attributes separated by 68newline, and is terminated by an empty line. The server reply is 69one name=value attribute and it, too, is terminated by an empty 70line. </p> 71 72<p> Here is an example of all the attributes that the Postfix SMTP 73server sends in a delegated SMTPD access policy request: </p> 74 75<blockquote> 76<pre> 77<b>Postfix version 2.1 and later:</b> 78request=smtpd_access_policy 79protocol_state=RCPT 80protocol_name=SMTP 81helo_name=some.domain.tld 82queue_id=8045F2AB23 83sender=foo@bar.tld 84recipient=bar@foo.tld 85recipient_count=0 86client_address=1.2.3.4 87client_name=another.domain.tld 88reverse_client_name=another.domain.tld 89instance=123.456.7 90<b>Postfix version 2.2 and later:</b> 91sasl_method=plain 92sasl_username=you 93sasl_sender= 94size=12345 95ccert_subject=solaris9.porcupine.org 96ccert_issuer=Wietse+20Venema 97ccert_fingerprint=C2:9D:F4:87:71:73:73:D9:18:E7:C2:F3:C1:DA:6E:04 98<b>Postfix version 2.3 and later:</b> 99encryption_protocol=TLSv1/SSLv3 100encryption_cipher=DHE-RSA-AES256-SHA 101encryption_keysize=256 102etrn_domain= 103<b>Postfix version 2.5 and later:</b> 104stress= 105<b>Postfix version 2.9 and later:</b> 106ccert_pubkey_fingerprint=68:B3:29:DA:98:93:E3:40:99:C7:D8:AD:5C:B9:C9:40 107[empty line] 108</pre> 109</blockquote> 110 111<p> Notes: </p> 112 113<ul> 114 115 <li> <p> The "request" attribute is required. In this example 116 the request type is "smtpd_access_policy". </p> 117 118 <li> <p> The order of the attributes does not matter. The policy 119 server should ignore any attributes that it does not care about. 120 </p> 121 122 <li> <p> When the same attribute name is sent more than once, 123 the server may keep the first value or the last attribute value. 124 </p> 125 126 <li> <p> When an attribute value is unavailable, the client 127 either does not send the attribute, sends the attribute with 128 an empty value ("name="), or sends a zero value ("name=0") in 129 the case of a numerical attribute. </p> 130 131 <li> <p> The "recipient" attribute is available in the "RCPT 132 TO" stage. It is also available in the "DATA" and "END-OF-MESSAGE" 133 stages if Postfix accepted only one recipient for the current 134 message. </p> 135 136 <li> <p> The "recipient_count" attribute (Postfix 2.3 and later) 137 is non-zero only in the "DATA" and "END-OF-MESSAGE" stages. It 138 specifies the number of recipients that Postfix accepted for 139 the current message. </p> 140 141 <li> <p> The client address is an IPv4 dotted quad in the form 142 1.2.3.4 or it is an IPv6 address in the form 1:2:3::4:5:6. 143 </p> 144 145 <li> <p> For a discussion of the differences between reverse 146 and verified client_name information, see the 147 <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> 148 document. </p> 149 150 <li> <p> An attribute name must not contain "=", null or newline, 151 and an attribute value must not contain null or newline. </p> 152 153 <li> <p> The "instance" attribute value can be used to correlate 154 different requests regarding the same message delivery. These 155 requests are sent over the same policy connection (unless the 156 policy daemon terminates the connection). Once Postfix sends 157 a query with a different instance attribute over that same 158 policy connection, the previous message delivery is either 159 completed or aborted. </p> 160 161 <li> <p> The "size" attribute value specifies the message size 162 that the client specified in the MAIL FROM command (zero if 163 none was specified). With Postfix 2.2 and later, it specifies 164 the actual message size when the client sends the END-OF-DATA 165 command. 166 </p> 167 168 <li> <p> The "sasl_*" attributes (Postfix 2.2 and later) specify 169 information about how the client was authenticated via SASL. 170 These attributes are empty in case of no SASL authentication. 171 </p> 172 173 <li> <p> The "ccert_*" attributes (Postfix 2.2 and later) specify 174 information about how the client was authenticated via TLS. 175 These attributes are empty in case of no certificate authentication. 176 As of Postfix 2.2.11 these attribute values are encoded as 177 xtext: some characters are represented by +XX, where XX is the 178 two-digit hexadecimal representation of the character value. With 179 Postfix 2.6 and later, the decoded string is an UTF-8 string 180 without non-printable ASCII characters. </p> 181 182 <li> <p> The "encryption_*" attributes (Postfix 2.3 and later) 183 specify information about how the connection is encrypted. With 184 plaintext connections the protocol and cipher attributes are 185 empty and the keysize is zero. </p> 186 187 <li> <p> The "etrn_domain" attribute is defined only in the 188 context of the ETRN command, and specifies the ETRN command 189 parameter. </p> 190 191 <li> <p> The "stress" attribute is either empty or "yes". See 192 the <a href="STRESS_README.html">STRESS_README</a> document for further information. </p> 193 194</ul> 195 196<p> The following is specific to SMTPD delegated policy requests: 197</p> 198 199<ul> 200 201 <li> <p> Protocol names are ESMTP or SMTP. </p> 202 203 <li> <p> Protocol states are CONNECT, EHLO, HELO, MAIL, RCPT, 204 DATA, END-OF-MESSAGE, VRFY or ETRN; these are the SMTP protocol 205 states where 206 the Postfix SMTP server makes an OK/REJECT/HOLD/etc. decision. 207 </p> 208 209</ul> 210 211<p> The policy server replies with any action that is allowed in a 212Postfix SMTPD <a href="access.5.html">access(5)</a> table. Example: </p> 213 214<blockquote> 215<pre> 216action=<a href="postconf.5.html#defer_if_permit">defer_if_permit</a> Service temporarily unavailable 217[empty line] 218</pre> 219</blockquote> 220 221<p> This causes the Postfix SMTP server to reject the request with 222a 450 temporary error code and with text "Service temporarily 223unavailable", if the Postfix SMTP server finds no reason to reject 224the request permanently. </p> 225 226<p> In case of trouble the policy server must not send a reply. 227Instead the server must log a warning and disconnect. Postfix will 228retry the request at some later time. </p> 229 230<h2><a name="client_config">Policy client/server configuration</a></h2> 231 232<p> The Postfix delegated policy client can connect to a TCP socket 233or to a UNIX-domain socket. Examples: </p> 234 235<blockquote> 236<pre> 237inet:127.0.0.1:9998 238unix:/some/where/policy 239unix:private/policy 240</pre> 241</blockquote> 242 243<p> The first example specifies that the policy server listens on 244a TCP socket at 127.0.0.1 port 9998. The second example specifies 245an absolute pathname of a UNIX-domain socket. The third example 246specifies a pathname relative to the Postfix queue directory; use 247this for policy servers that are spawned by the Postfix master 248daemon. </p> 249 250<p> To create a policy service that listens on a UNIX-domain socket 251called "policy", and that runs under control of the Postfix <a href="spawn.8.html">spawn(8)</a> 252daemon, you would use something like this: </p> 253 254<blockquote> 255<pre> 256 1 /etc/postfix/<a href="master.5.html">master.cf</a>: 257 2 policy unix - n n - 0 spawn 258 3 user=nobody argv=/some/where/policy-server 259 4 260 5 /etc/postfix/<a href="postconf.5.html">main.cf</a>: 261 6 <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> = 262 7 ... 263 8 <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> 264 9 <a href="postconf.5.html#check_policy_service">check_policy_service</a> unix:private/policy 26510 ... 26611 <a href="postconf.5.html#transport_time_limit">policy_time_limit</a> = 3600 267</pre> 268</blockquote> 269 270<p> NOTES: </p> 271 272<ul> 273 274<li> <p> Lines 2-3: this creates the service called "policy" that 275listens on a UNIX-domain socket. The service is implemented by the 276Postfix <a href="spawn.8.html">spawn(8)</a> daemon, which executes the policy server program 277that is specified with the <b>argv</b> attribute, using the privileges 278specified with the <b>user</b> attribute. </p> 279 280<li> <p> Line 2: specify a "0" process limit instead of the default 281"-", to avoid "connection refused" and other problems when you 282increase the smtpd process limit. </p> 283 284<li> <p> Line 8: <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> is not needed here if 285the mail relay policy is specified with <a href="postconf.5.html#smtpd_relay_restrictions">smtpd_relay_restrictions</a> 286(available with Postfix 2.10 and later). </p> 287 288<li> <p> Lines 8, 9: always specify "<a href="postconf.5.html#check_policy_service">check_policy_service</a>" AFTER 289"<a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>" or else your system could become an 290open relay. </p> 291 292<li> <p> Line 11: this increases the time that a policy server 293process may run to 3600 seconds. The default time limit of 1000 294seconds is too short; the policy daemon needs to run long as the 295SMTP server process that talks to it. 296See the <a href="spawn.8.html">spawn(8)</a> manpage for more information about the 297<a href="postconf.5.html#transport_time_limit"><i>transport</i>_time_limit</a> parameter. </p> 298 299<blockquote> <p> Note: the "<a href="postconf.5.html#transport_time_limit">policy_time_limit</a>" parameter will not 300show up in "postconf" command output before Postfix version 2.9. 301This limitation applies to many parameters whose name is a combination 302of a <a href="master.5.html">master.cf</a> service name (in the above example, "policy") and a 303built-in suffix (in the above example: "_time_limit"). </p> 304</blockquote> 305 306<li> <p> With Solaris < 9, or Postfix < 2.10 on any Solaris 307version, use TCP sockets instead of UNIX-domain sockets: </p> 308 309</ul> 310 311<blockquote> 312<pre> 313 1 /etc/postfix/<a href="master.5.html">master.cf</a>: 314 2 127.0.0.1:9998 inet n n n - 0 spawn 315 3 user=nobody argv=/some/where/policy-server 316 4 317 5 /etc/postfix/<a href="postconf.5.html">main.cf</a>: 318 6 <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> = 319 7 ... 320 8 <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> 321 9 <a href="postconf.5.html#check_policy_service">check_policy_service</a> inet:127.0.0.1:9998 32210 ... 32311 127.0.0.1:9998_time_limit = 3600 324</pre> 325</blockquote> 326 327<p> Other configuration parameters that control the client side of 328the policy delegation protocol: </p> 329 330<ul> 331 332<li> <p> <a href="postconf.5.html#smtpd_policy_service_max_idle">smtpd_policy_service_max_idle</a> (default: 300s): The amount 333of time before the Postfix SMTP server closes an unused policy 334client connection. </p> 335 336<li> <p> <a href="postconf.5.html#smtpd_policy_service_max_ttl">smtpd_policy_service_max_ttl</a> (default: 1000s): The amount 337of time before the Postfix SMTP server closes an active policy 338client connection. </p> 339 340<li> <p> <a href="postconf.5.html#smtpd_policy_service_timeout">smtpd_policy_service_timeout</a> (default: 100s): The time 341limit to connect to, send to or receive from a policy server. </p> 342 343</ul> 344 345<h2><a name="greylist">Example: greylist policy server</a></h2> 346 347<p> Greylisting is a defense against junk email that is described at 348<a href="http://www.greylisting.org/">http://www.greylisting.org/</a>. The idea was discussed on the 349postfix-users mailing list <a 350href="http://archives.neohapsis.com/archives/postfix/2002-03/0846.html"> 351one year before it was popularized</a>. </p> 352 353<p> The file examples/smtpd-policy/greylist.pl in the Postfix source 354tree implements a simplified greylist policy server. This server 355stores a time stamp for every (client, sender, recipient) triple. 356By default, mail is not accepted until a time stamp is more than 35760 seconds old. This stops junk mail with randomly selected sender 358addresses, and mail that is sent through randomly selected open 359proxies. It also stops junk mail from spammers that change their 360IP address frequently. </p> 361 362<p> Copy examples/smtpd-policy/greylist.pl to /usr/libexec/postfix 363or whatever location is appropriate for your system. </p> 364 365<p> In the greylist.pl Perl script you need to specify the 366location of the greylist database file, and how long mail will 367be delayed before it is accepted. The default settings are: 368</p> 369 370<blockquote> 371<pre> 372$database_name="/var/mta/greylist.db"; 373$greylist_delay=60; 374</pre> 375</blockquote> 376 377<p> The /var/mta directory (or whatever you choose) should be 378writable by "nobody", or by whatever username you configure below 379in <a href="master.5.html">master.cf</a> for the policy service. </p> 380 381<p> Example: </p> 382 383<blockquote> 384<pre> 385# mkdir /var/mta 386# chown nobody /var/mta 387</pre> 388</blockquote> 389 390<p> Note: DO NOT create the greylist database in a world-writable 391directory such as /tmp or /var/tmp, and DO NOT create the greylist 392database in a file system that may run out of space. Postfix can 393survive "out of space" conditions with the mail queue and with the 394mailbox store, but it cannot survive a corrupted greylist database. 395If the file becomes corrupted you may not be able to receive mail 396at all until you delete the file by hand. </p> 397 398<p> The greylist.pl Perl script can be run under control by 399the Postfix master daemon. For example, to run the script as user 400"nobody", using a UNIX-domain socket that is accessible by Postfix 401processes only: </p> 402 403<blockquote> 404<pre> 405 1 /etc/postfix/<a href="master.5.html">master.cf</a>: 406 2 greylist unix - n n - 0 spawn 407 3 user=nobody argv=/usr/bin/perl /usr/libexec/postfix/greylist.pl 408 4 409 5 /etc/postfix/<a href="postconf.5.html">main.cf</a>: 410 6 <a href="postconf.5.html#transport_time_limit">greylist_time_limit</a> = 3600 411 7 <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> = 412 8 ... 413 9 <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> 41410 <a href="postconf.5.html#check_policy_service">check_policy_service</a> unix:private/greylist 41511 ... 416</pre> 417</blockquote> 418 419<p> Notes: </p> 420 421<ul> 422 423<li> <p> Lines 2-3: this creates the service called "greylist" that 424listens on a UNIX-domain socket. The service is implemented by the 425Postfix <a href="spawn.8.html">spawn(8)</a> daemon, which executes the greylist.pl script that 426is specified with the <b>argv</b> attribute, using the privileges 427specified with the <b>user</b> attribute. </p> 428 429<li> <p> Line 2: specify a "0" process limit instead of the default 430"-", to avoid "connection refused" and other problems when you 431increase the smtpd process limit. </p> 432 433<li> <p> Line 3: Specify "greylist.pl -v" for verbose logging of 434each request and reply. </p> 435 436<li> <p> Line 6: this increases the time that a greylist server 437process may run to 3600 seconds. The default time limit of 1000 438seconds is too short; the greylist daemon needs to run long as the 439SMTP server process that talks to it. 440See the <a href="spawn.8.html">spawn(8)</a> manpage for more information about the 441<a href="postconf.5.html#transport_time_limit"><i>transport</i>_time_limit</a> parameter. </p> 442 443<li> <p> Line 9: <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> is not needed here if 444the mail relay policy is specified with <a href="postconf.5.html#smtpd_relay_restrictions">smtpd_relay_restrictions</a> 445(available with Postfix 2.10 and later). </p> 446 447<blockquote> <p> Note: the "<a href="postconf.5.html#transport_time_limit">greylist_time_limit</a>" parameter will not 448show up in "postconf" command output before Postfix version 2.9. 449This limitation applies to many parameters whose name is a combination 450of a <a href="master.5.html">master.cf</a> service name (in the above example, "greylist") and 451a built-in suffix (in the above example: "_time_limit"). </p> 452</blockquote> 453 454</ul> 455 456<p> With Solaris < 9, or Postfix < 2.10 on any Solaris 457version, use inet: style sockets instead of unix: 458style, as detailed in the "<a href="#client_config">Policy 459client/server configuration</a>" section above. </p> 460 461<blockquote> 462<pre> 463 1 /etc/postfix/<a href="master.5.html">master.cf</a>: 464 2 127.0.0.1:9998 inet n n n - 0 spawn 465 3 user=nobody argv=/usr/bin/perl /usr/libexec/postfix/greylist.pl 466 4 467 5 /etc/postfix/<a href="postconf.5.html">main.cf</a>: 468 6 127.0.0.1:9998_time_limit = 3600 469 7 <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> = 470 8 ... 471 9 <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> 47210 <a href="postconf.5.html#check_policy_service">check_policy_service</a> inet:127.0.0.1:9998 47311 ... 474</pre> 475</blockquote> 476 477<h2><a name="frequent">Greylisting mail from frequently forged domains</a></h2> 478 479<p> It is relatively safe to turn on greylisting for specific 480domains that often appear in forged email. At some point 481in cyberspace/time a list of frequently 482forged MAIL FROM domains could be found at 483<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>. 484 485<blockquote> 486<pre> 487 1 /etc/postfix/<a href="postconf.5.html">main.cf</a>: 488 2 <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> = 489 3 <a href="postconf.5.html#reject_unlisted_recipient">reject_unlisted_recipient</a> 490 4 ... 491 5 <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> 492 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 493 7 ... 494 8 <a href="postconf.5.html#smtpd_restriction_classes">smtpd_restriction_classes</a> = greylist 495 9 greylist = <a href="postconf.5.html#check_policy_service">check_policy_service</a> unix:private/greylist 49610 49711 /etc/postfix/sender_access: 49812 aol.com greylist 49913 hotmail.com greylist 50014 bigfoot.com greylist 50115 ... <i>etcetera</i> ... 502</pre> 503</blockquote> 504 505<p> NOTES: </p> 506 507<ul> 508 509<li> <p> Line 9: On Solaris < 9, or Postfix < 2.10 on any 510Solaris version, use inet: style sockets 511instead of unix: style, as detailed in the "<a href="#greylist">Example: 512greylist policy server</a>" section above. </p> 513 514<li> <p> Line 5: <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> is not needed here if 515the mail relay policy is specified with <a href="postconf.5.html#smtpd_relay_restrictions">smtpd_relay_restrictions</a> 516(available with Postfix 2.10 and later). </p> 517 518<li> <p> Line 6: Be sure to specify "<a href="postconf.5.html#check_sender_access">check_sender_access</a>" AFTER 519"<a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a>" or else your system could become an 520open mail relay. </p> 521 522<li> <p> Line 3: With Postfix 2.0 snapshot releases, 523"<a href="postconf.5.html#reject_unlisted_recipient">reject_unlisted_recipient</a>" is called "check_recipient_maps". 524Postfix 2.1 understands both forms. </p> 525 526<li> <p> Line 3: The greylist database gets polluted quickly with 527bogus addresses. It helps if you protect greylist lookups with 528other restrictions that reject unknown senders and/or recipients. 529</p> 530 531</ul> 532 533<h2><a name="all_mail">Greylisting all your mail</a></h2> 534 535<p> If you turn on greylisting for all mail you may want to make 536exceptions for mailing lists that use one-time sender addresses, 537because each message will be delayed due to greylisting, and the 538one-time sender addresses can pollute your greylist database 539relatively quickly. Instead of making exceptions, you can automatically 540whitelist clients that survive greylisting repeatedly; this avoids 541most of the delays and most of the database pollution problem. </p> 542 543<blockquote> 544<pre> 545 1 /etc/postfix/<a href="postconf.5.html">main.cf</a>: 546 2 <a href="postconf.5.html#smtpd_recipient_restrictions">smtpd_recipient_restrictions</a> = 547 3 <a href="postconf.5.html#reject_unlisted_recipient">reject_unlisted_recipient</a> 548 4 ... 549 5 <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> 550 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 551 7 <a href="postconf.5.html#check_policy_service">check_policy_service</a> unix:private/policy 552 8 ... 553 9 55410 /etc/postfix/sender_access: 55511 securityfocus.com OK 55612 ... 557</pre> 558</blockquote> 559 560<p> NOTES: </p> 561 562<ul> 563 564<li> <p> Line 7: On Solaris < 9, or Postfix < 2.10 on any 565Solaris version, use inet: style sockets 566instead of unix: style, as detailed in the "<a href="#greylist">Example: 567greylist policy server</a>" section above. </p> 568 569<li> <p> Line 5: <a href="postconf.5.html#reject_unauth_destination">reject_unauth_destination</a> is not needed here if 570the mail relay policy is specified with <a href="postconf.5.html#smtpd_relay_restrictions">smtpd_relay_restrictions</a> 571(available with Postfix 2.10 and later). </p> 572 573<li> <p> Lines 6-7: Be sure to specify <a href="postconf.5.html#check_sender_access">check_sender_access</a> and 574<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 575system could become an open mail relay. </p> 576 577<li> <p> Line 3: The greylist database gets polluted quickly with 578bogus addresses. It helps if you precede greylist lookups with 579restrictions that reject unknown senders and/or recipients. </p> 580 581</ul> 582 583<h2><a name="maintenance">Routine greylist maintenance</a></h2> 584 585<p> The greylist database grows over time, because the greylist server 586never removes database entries. If left unattended, the greylist 587database will eventually run your file system out of space. </p> 588 589<p> When the status file size exceeds some threshold you can simply 590rename or remove the file without adverse effects; Postfix 591automatically creates a new file. In the worst case, new mail will 592be delayed by an hour or so. To lessen the impact, rename or remove 593the file in the middle of the night at the beginning of a weekend. 594</p> 595 596<h2><a name="greylist_code">Example Perl greylist server</a></h2> 597 598<p> This is the Perl subroutine that implements the example greylist 599policy. It is part of a general purpose sample policy server that 600is distributed with the Postfix source as 601examples/smtpd-policy/greylist.pl. </p> 602 603<pre> 604# 605# greylist status database and greylist time interval. DO NOT create the 606# greylist status database in a world-writable directory such as /tmp 607# or /var/tmp. DO NOT create the greylist database in a file system 608# that can run out of space. 609# 610$database_name="/var/mta/greylist.db"; 611$greylist_delay=60; 612 613# 614# Auto-whitelist threshold. Specify 0 to disable, or the number of 615# successful "come backs" after which a client is no longer subject 616# to greylisting. 617# 618$auto_whitelist_threshold = 10; 619 620# 621# Demo SMTPD access policy routine. The result is an action just like 622# it would be specified on the right-hand side of a Postfix access 623# table. Request attributes are available via the %attr hash. 624# 625sub smtpd_access_policy { 626 my($key, $time_stamp, $now); 627 628 # Open the database on the fly. 629 open_database() unless $database_obj; 630 631 # Search the auto-whitelist. 632 if ($auto_whitelist_threshold > 0) { 633 $count = read_database($attr{"client_address"}); 634 if ($count > $auto_whitelist_threshold) { 635 return "dunno"; 636 } 637 } 638 639 # Lookup the time stamp for this client/sender/recipient. 640 $key = 641 lc $attr{"client_address"}."/".$attr{"sender"}."/".$attr{"recipient"}; 642 $time_stamp = read_database($key); 643 $now = time(); 644 645 # If new request, add this client/sender/recipient to the database. 646 if ($time_stamp == 0) { 647 $time_stamp = $now; 648 update_database($key, $time_stamp); 649 } 650 651 # The result can be any action that is allowed in a Postfix <a href="access.5.html">access(5)</a> map. 652 # 653 # To label the mail, return ``PREPEND headername: headertext'' 654 # 655 # In case of success, return ``DUNNO'' instead of ``OK'', so that the 656 # <a href="postconf.5.html#check_policy_service">check_policy_service</a> restriction can be followed by other restrictions. 657 # 658 # In case of failure, return ``DEFER_IF_PERMIT optional text...'', 659 # so that mail can still be blocked by other access restrictions. 660 # 661 syslog $syslog_priority, "request age %d", $now - $time_stamp if $verbose; 662 if ($now - $time_stamp > $greylist_delay) { 663 # Update the auto-whitelist. 664 if ($auto_whitelist_threshold > 0) { 665 update_database($attr{"client_address"}, $count + 1); 666 } 667 return "dunno"; 668 } else { 669 return "<a href="postconf.5.html#defer_if_permit">defer_if_permit</a> Service temporarily unavailable"; 670 } 671} 672</pre> 673 674</body> 675 676</html> 677