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 relay and access control </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 17SMTP relay and access control </h1> 18 19<hr> 20 21<h2> Introduction </h2> 22 23<p> The Postfix SMTP server receives mail from the network and is 24exposed to the big bad world of junk email and viruses. This document 25introduces the built-in and external methods that control what SMTP 26mail Postfix will accept, what mistakes to avoid, and how to test 27your configuration. </p> 28 29<p> Topics covered in this document: </p> 30 31<ul> 32 33<li> <a href="#relay"> Relay control, junk mail control, and per-user 34policies </a> 35 36<li> <a href="#global"> Restrictions that apply to all SMTP mail 37</a> 38 39<li> <a href="#lists"> Getting selective with SMTP access restriction 40lists </a> 41 42<li> <a href="#timing"> Delayed evaluation of SMTP access restriction lists </a> 43 44<li> <a href="#danger"> Dangerous use of smtpd_recipient_restrictions 45</a> 46 47<li> <a href="#testing"> SMTP access rule testing </a> 48 49</ul> 50 51<h2> <a name="relay"> Relay control, junk mail control, and per-user 52policies </a> </h2> 53 54<p> In a distant past, the Internet was a friendly environment. 55Mail servers happily forwarded mail on behalf of anyone towards 56any destination. On today's Internet, spammers abuse servers that 57forward mail from arbitrary systems, and abused systems end up on 58anti-spammer blacklists. See, for example, the information on 59http://www.mail-abuse.org/ and other websites. </p> 60 61<p> By default, Postfix has a moderately restrictive approach to 62mail relaying. Postfix forwards mail only from clients in trusted 63networks, from clients that have authenticated with SASL, or to 64domains that are configured as authorized relay 65destinations. For a description of the default mail relay policy, 66see the smtpd_relay_restrictions parameter in the postconf(5) manual 67page, and the information that is referenced from there. </p> 68 69<blockquote> <p> NOTE: Postfix versions before 2.10 did not have 70smtpd_relay_restrictions. They combined the mail relay and spam 71blocking policies, under smtpd_recipient_restrictions. This could 72lead to unexpected results. For example, a permissive spam blocking 73policy could unexpectedly result in a permissive mail relay policy. 74An example of this is documented under "<a href="#danger">Dangerous 75use of smtpd_recipient_restrictions</a>". </p> </blockquote> 76 77<p> Most of the Postfix SMTP server access controls are targeted 78at stopping junk email. </p> 79 80<ul> 81 82<li> <p> Protocol oriented: some SMTP server access controls block 83mail by being very strict with respect to the SMTP protocol; these 84catch poorly implemented and/or poorly configured junk email 85software, as well as email worms that come with their own non-standard 86SMTP client implementations. Protocol-oriented access controls 87become less useful over time as spammers and worm writers learn to 88read RFC documents. </p> 89 90<li> <p> Blacklist oriented: some SMTP server access controls 91query blacklists with known to be bad sites such as open mail 92relays, open web proxies, and home computers that have been 93compromised and that are under remote control by criminals. The 94effectiveness of these blacklists depends on how complete and how 95up to date they are. </p> 96 97<li> <p> Threshold oriented: some SMTP server access controls attempt 98to raise the bar by either making the client do more work (greylisting) 99or by asking for a second opinion (SPF and sender/recipient address 100verification). The greylisting and SPF policies are implemented 101externally, and are the subject of the SMTPD_POLICY_README document. 102Sender/recipient address verification is the subject of the 103ADDRESS_VERIFICATION_README document. </p> 104 105</ul> 106 107<p> Unfortunately, all junk mail controls have the possibility of 108falsely rejecting legitimate mail. This can be a problem for sites 109with many different types of users. For some users it is unacceptable 110when any junk email slips through, while for other users the world 111comes to an end when a single legitimate email message is blocked. 112Because there is no single policy that is "right" for all users, 113Postfix supports different SMTP access restrictions for different 114users. This is described in the RESTRICTION_CLASS_README document. 115</p> 116 117<h2> <a name="global"> Restrictions that apply to all SMTP mail </a> </h2> 118 119<p> Besides the restrictions that can be made configurable per 120client or per user as described in the next section, Postfix 121implements a few restrictions that apply to all SMTP mail. </p> 122 123<ul> 124 125<li> <p> The built-in header_checks and body_checks content 126restrictions, as described in the BUILTIN_FILTER_README document. 127This happens while Postfix receives mail, before it is stored in 128the incoming queue. </p> 129 130<li> <p> The external before-queue content restrictions, as described 131in the SMTPD_PROXY_README document. This happens while Postfix 132receives mail, before it is stored in the incoming queue. </p> 133 134<li> <p> Requiring that the client sends the HELO or EHLO command 135before sending the MAIL FROM or ETRN command. This may cause problems 136with home-grown applications that send mail. For this reason, the 137requirement is disabled by default ("smtpd_helo_required = no"). 138</p> 139 140<li> <p> Disallowing illegal syntax in MAIL FROM or RCPT TO commands. 141This may cause problems with home-grown applications that send 142mail, and with ancient PC mail clients. For this reason, the 143requirement is disabled by default ("strict_rfc821_envelopes = 144no"). </p> 145 146<ul> 147 148<li> <p> Disallowing RFC 822 address syntax (example: "MAIL FROM: the 149dude <dude@example.com>"). </p> 150 151<li> <p> Disallowing addresses that are not enclosed with <> 152(example: "MAIL FROM: dude@example.com"). </p> 153 154</ul> 155 156<li> <p> Rejecting mail from a non-existent sender address. This form 157of egress filtering helps to slow down worms and other malware, but 158may cause problems with home-grown software that sends out mail 159software with an unreplyable address. For this reason the requirement 160is disabled by default ("smtpd_reject_unlisted_sender = no"). </p> 161 162<li> <p> Rejecting mail for a non-existent recipient address. This 163form of ingress filtering helps to keep the mail queue free of 164undeliverable MAILER-DAEMON messages. This requirement is enabled 165by default ("smtpd_reject_unlisted_recipient = yes"). </p> 166 167</ul> 168 169<h2> <a name="lists"> Getting selective with SMTP access restriction 170lists </a> </h2> 171 172<p> Postfix allows you to specify lists of access restrictions for 173each stage of the SMTP conversation. Individual restrictions are 174described in the postconf(5) manual page. </p> 175 176<p> Examples of simple restriction lists are: </p> 177 178<pre> 179/etc/postfix/main.cf: 180 # Allow connections from trusted networks only. 181 smtpd_client_restrictions = permit_mynetworks, reject 182 183 # Don't talk to mail systems that don't know their own hostname. 184 # With Postfix < 2.3, specify reject_unknown_hostname. 185 smtpd_helo_restrictions = reject_unknown_helo_hostname 186 187 # Don't accept mail from domains that don't exist. 188 smtpd_sender_restrictions = reject_unknown_sender_domain 189 190 # Spam control: exclude local clients and authenticated clients 191 # from DNSBL lookups. 192 smtpd_recipient_restrictions = permit_mynetworks, 193 permit_sasl_authenticated, 194 # reject_unauth_destination is not needed here if the mail 195 # relay policy is specified under smtpd_relay_restrictions 196 # (available with Postfix 2.10 and later). 197 reject_unauth_destination 198 reject_rbl_client zen.spamhaus.org, 199 reject_rhsbl_reverse_client dbl.spamhaus.org, 200 reject_rhsbl_helo dbl.spamhaus.org, 201 reject_rhsbl_sender dbl.spamhaus.org 202 203 # Relay control (Postfix 2.10 and later): local clients and 204 # authenticated clients may specify any destination domain. 205 smtpd_relay_restrictions = permit_mynetworks, 206 permit_sasl_authenticated, 207 reject_unauth_destination 208 209 # Block clients that speak too early. 210 smtpd_data_restrictions = reject_unauth_pipelining 211 212 # Enforce mail volume quota via policy service callouts. 213 smtpd_end_of_data_restrictions = check_policy_service unix:private/policy 214</pre> 215 216<p> Each restriction list is evaluated from left to right until 217some restriction produces a result of PERMIT, REJECT or DEFER (try 218again later). The end of each list is equivalent to a PERMIT result. 219By placing a PERMIT restriction before a REJECT restriction you 220can make exceptions for specific clients or users. This is called 221whitelisting; the fourth example above allows mail from local 222networks but otherwise rejects mail to arbitrary destinations. </p> 223 224<p> The table below summarizes the purpose of each SMTP access 225restriction list. All lists use the exact same syntax; they differ 226only in the time of evaluation and in the effect of a REJECT or 227DEFER result. </p> 228 229<blockquote> 230 231<table border="1"> 232 233<tr> <th> Restriction list name </th> <th> Version </th> <th> Status 234</th> <th> Effect 235of REJECT or DEFER result </th> </tr> 236 237<tr> <td> smtpd_client_restrictions </td> <td> All </td> <td> 238Optional </td> <td> 239Reject all client commands </td> </tr> 240 241<tr> <td> smtpd_helo_restrictions </td> <td> All </td> <td> Optional 242</td> <td> 243Reject HELO/EHLO information </td> </tr> 244 245<tr> <td> smtpd_sender_restrictions </td> <td> All </td> <td> 246Optional </td> <td> 247Reject MAIL FROM information </td> </tr> 248 249<tr> <td rowspan="2"> smtpd_recipient_restrictions </td> <td> ≥ 2502.10 </td> <td> Required if smtpd_relay_restrictions does not enforce 251relay policy</td> 252<td rowspan="2"> Reject RCPT TO information </td> </tr> 253 254<tr> <td> < 2.10</td> <td> Required </td> </tr> 255 256<tr> <td rowspan="2"> smtpd_relay_restrictions </td> <td> ≥ 2.10 257</td> <td> Required if smtpd_recipient_restrictions does not enforce 258relay policy</td> 259<td rowspan="2"> Reject RCPT TO information </td> </tr> 260 261<tr> <td> < 2.10</td> <td> Not available </td> 262</tr> 263 264<tr> <td> smtpd_data_restrictions </td> <td> ≥ 2.0 </td> <td> 265Optional </td> <td> 266Reject DATA command </td> </tr> 267 268<tr> <td> smtpd_end_of_data_restrictions </td> <td> ≥ 2.2 </td> 269<td> Optional </td> <td> 270Reject END-OF-DATA command </td> </tr> 271 272<tr> <td> smtpd_etrn_restrictions </td> <td> All </td> <td> Optional 273</td> <td> 274Reject ETRN command </td> </tr> 275 276</table> 277 278</blockquote> 279 280<h2> <a name="timing"> Delayed evaluation of SMTP access restriction lists 281</a> </h2> 282 283<p> Early Postfix versions evaluated SMTP access restrictions lists 284as early as possible. The client restriction list was evaluated 285before Postfix sent the "220 $myhostname..." greeting banner to 286the SMTP client, the helo restriction list was evaluated before 287Postfix replied to the HELO (EHLO) command, the sender restriction 288list was evaluated before Postfix replied to the MAIL FROM command, 289and so on. This approach turned out to be difficult to use. </p> 290 291<p> Current Postfix versions postpone the evaluation of client, 292helo and sender restriction lists until the RCPT TO or ETRN command. 293This behavior is controlled by the smtpd_delay_reject parameter. 294Restriction lists are still evaluated in the proper order of (client, 295helo, etrn) or (client, helo, sender, relay, recipient, data, or 296end-of-data) restrictions. 297When a restriction list (example: client) evaluates to REJECT or 298DEFER the restriction lists that follow (example: helo, sender, etc.) 299are skipped. </p> 300 301<p> Around the time that smtpd_delay_reject was introduced, Postfix 302was also changed to support mixed restriction lists that combine 303information about the client, helo, sender and recipient or etrn 304command. </p> 305 306<p> Benefits of delayed restriction evaluation, and of restriction 307mixing: </p> 308 309<ul> 310 311<li> <p> Some SMTP clients do not expect a negative reply early in 312the SMTP session. When the bad news is postponed until the RCPT TO 313reply, the client goes away as it is supposed to, instead of hanging 314around until a timeout happens, or worse, going into an endless 315connect-reject-connect loop. </p> 316 317<li> <p> Postfix can log more useful information. For example, when 318Postfix rejects a client name or address and delays the action 319until the RCPT TO command, it can log the sender and the recipient 320address. This is more useful than logging only the client hostname 321and IP address and not knowing whose mail was being blocked. </p> 322 323<li> <p> Mixing is needed for complex whitelisting policies. For 324example, in order to reject local sender addresses in mail from 325non-local clients, you need to be able to mix restrictions on client 326information with restrictions on sender information in the same 327restriction list. Without this ability, many per-user access 328restrictions would be impossible to express. </p> 329 330</ul> 331 332<h2> <a name="danger"> Dangerous use of smtpd_recipient_restrictions </a> </h2> 333 334<p> By now the reader may wonder why we need smtpd client, helo 335or sender restrictions, when their evaluation is postponed until 336the RCPT TO or ETRN command. Some people recommend placing ALL the 337access restrictions in the smtpd_recipient_restrictions list. 338Unfortunately, this can result in too permissive access. How is 339this possible? </p> 340 341<p> The purpose of the smtpd_recipient_restrictions feature is to 342control how Postfix replies to the RCPT TO command. If the restriction 343list evaluates to REJECT or DEFER, the recipient address is rejected; 344no surprises here. If the result is PERMIT, then the recipient 345address is accepted. And this is where surprises can happen. </p> 346 347<p> The problem is that Postfix versions before 2.10 did not have 348smtpd_relay_restrictions. They combined the mail relay and spam 349blocking policies, under smtpd_recipient_restrictions. The result 350is that a permissive spam blocking policy could unexpectedly result 351in a permissive mail relay policy. </p> 352 353<p> Here is an example that shows when a PERMIT result can result 354in too much access permission: </p> 355 356<pre> 3571 /etc/postfix/main.cf: 3582 smtpd_recipient_restrictions = 3593 permit_mynetworks 3604 check_helo_access hash:/etc/postfix/helo_access 3615 reject_unknown_helo_hostname 3626 <b>reject_unauth_destination</b> 3637 3648 /etc/postfix/helo_access: 3659 localhost.localdomain PERMIT 366</pre> 367 368<p> Line 5 rejects mail from hosts that don't specify a proper 369hostname in the HELO command (with Postfix < 2.3, specify 370reject_unknown_hostname). Lines 4 and 9 make an exception to 371allow mail from some machine that announces itself with "HELO 372localhost.localdomain". </p> 373 374<p> The problem with this configuration is that 375smtpd_recipient_restrictions evaluates to PERMIT for EVERY host 376that announces itself as "localhost.localdomain", making Postfix 377an open relay for all such hosts. </p> 378 379<p> With Postfix before version 2.10 you should place non-recipient 380restrictions AFTER the reject_unauth_destination restriction, not 381before. In the above example, the HELO based restrictions should 382be placed AFTER reject_unauth_destination, or better, the HELO 383based restrictions should be placed under smtpd_helo_restrictions 384where they can do no harm. </p> 385 386<pre> 3871 /etc/postfix/main.cf: 3882 smtpd_recipient_restrictions = 3893 permit_mynetworks 3904 <b>reject_unauth_destination</b> 3915 check_helo_access hash:/etc/postfix/helo_access 3926 reject_unknown_helo_hostname 3937 3948 /etc/postfix/helo_access: 3959 localhost.localdomain PERMIT 396</pre> 397 398<p> The above mistake will not happen with Postfix 2.10 and later, 399when the relay policy is specified under smtpd_relay_restrictions, 400and the spam blocking policy under smtpd_recipient_restrictions. 401Then, a permissive spam blocking policy will not result in a 402permissive mail relay policy. </p> 403 404<h2> <a name="testing"> SMTP access rule testing </a> </h2> 405 406<p> Postfix has several features that aid in SMTP access rule 407testing: </p> 408 409<dl> 410 411<dt> soft_bounce </dt> <dd> <p> This is a safety net that changes 412SMTP server REJECT actions into DEFER (try again later) actions. 413This keeps mail queued that would otherwise be returned to the 414sender. Specify "soft_bounce = yes" in the main.cf file to prevent 415the Postfix SMTP server from rejecting mail permanently, by changing 416all 5xx SMTP reply codes into 4xx. </p> </dd> 417 418<dt> warn_if_reject </dt> <dd> <p> When placed before a reject-type 419restriction, access table query, or check_policy_service query, 420this logs a "reject_warning" message instead of rejecting a request 421(when a reject-type restriction fails due to a temporary error, 422this logs a "reject_warning" message for any implicit "defer_if_permit" 423actions that would normally prevent mail from being accepted by 424some later access restriction). This feature has no effect on 425defer_if_reject restrictions. </p> </dd> 426 427<dt> XCLIENT </dt> <dd> <p> With this feature, an authorized SMTP 428client can impersonate other systems and perform realistic SMTP 429access rule tests. Examples of how to impersonate other systems 430for access rule testing are given at the end of the XCLIENT_README 431document. <br> This feature is available in Postfix 2.1. </p> 432</dd> 433 434</dl> 435 436</body> 437 438</html> 439