~ubuntu-branches/ubuntu/vivid/postfix/vivid-proposed

« back to all changes in this revision

Viewing changes to README_FILES/SMTPD_POLICY_README

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-02-27 09:33:07 UTC
  • Revision ID: james.westby@ubuntu.com-20050227093307-cn789t27ibnlh6tf
Tags: upstream-2.1.5
ImportĀ upstreamĀ versionĀ 2.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
PPoossttffiixx SSMMTTPP AAcccceessss PPoolliiccyy DDeelleeggaattiioonn
 
2
 
 
3
-------------------------------------------------------------------------------
 
4
 
 
5
PPuurrppoossee ooff PPoossttffiixx SSMMTTPP aacccceessss ppoolliiccyy ddeelleeggaattiioonn
 
6
 
 
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.
 
10
 
 
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. Another example of policy delegation is the SPF policy server by Meng
 
14
Wong at http://spf.pobox.com/. Examples of both policies can be found in the
 
15
Postfix source code, in the directory examples/smtpd-policy.
 
16
 
 
17
Policy delegation is now the preferred method for adding policies to Postfix.
 
18
It's much easier to develop a new feature in few lines of Perl, than trying to
 
19
do the same in C code. The difference in performance will be unnoticeable
 
20
except in the most demanding environments.
 
21
 
 
22
This document covers the following topics:
 
23
 
 
24
  * Policy protocol description
 
25
  * Policy client/server configuration
 
26
  * Example: greylist policy server
 
27
  * Greylisting mail from frequently forged domains
 
28
  * Greylisting all your mail
 
29
  * Routine greylist maintenance
 
30
  * Example Perl greylist server
 
31
 
 
32
PPrroottooccooll ddeessccrriippttiioonn
 
33
 
 
34
The Postfix policy delegation protocol is really simple. The client request is
 
35
a sequence of name=value attributes separated by newline, and is terminated by
 
36
an empty line. The server reply is one name=value attribute and it, too, is
 
37
terminated by an empty line.
 
38
 
 
39
Here is an example of all the attributes that the Postfix SMTP server sends in
 
40
a delegated SMTPD access policy request:
 
41
 
 
42
    request=smtpd_access_policy
 
43
    protocol_state=RCPT
 
44
    protocol_name=SMTP
 
45
    helo_name=some.domain.tld
 
46
    queue_id=8045F2AB23
 
47
    sender=foo@bar.tld
 
48
    recipient=bar@foo.tld
 
49
    client_address=1.2.3.4
 
50
    client_name=another.domain.tld
 
51
    instance=123.456.7
 
52
    sasl_method=plain
 
53
    sasl_username=you
 
54
    sasl_sender=
 
55
    size=12345
 
56
    [empty line]
 
57
 
 
58
Notes:
 
59
 
 
60
  * The "request" attribute is required. In this example the request type is
 
61
    "smtpd_access_policy".
 
62
 
 
63
  * The order of the attributes does not matter. The policy server should
 
64
    ignore any attributes that it does not care about.
 
65
 
 
66
  * When the same attribute name is sent more than once, the server may keep
 
67
    the first value or the last attribute value.
 
68
 
 
69
  * When an attribute value is unavailable, the client either does not send the
 
70
    attribute, or sends the attribute with an empty value ("name=").
 
71
 
 
72
  * An attribute name must not contain "=", null or newline, and an attribute
 
73
    value must not contain null or newline.
 
74
 
 
75
  * The "instance" attribute value can be used to correlate different requests
 
76
    regarding the same message delivery.
 
77
 
 
78
The following is specific to SMTPD delegated policy requests:
 
79
 
 
80
  * Protocol names are ESMTP or SMTP.
 
81
 
 
82
  * Protocol states are CONNECT, EHLO, HELO, MAIL, RCPT, DATA, VRFY or ETRN;
 
83
    these are the SMTP protocol states where the Postfix SMTP server makes an
 
84
    OK/REJECT/HOLD/etc. decision.
 
85
 
 
86
  * The SASL attributes are sent only when SASL support is built into Postfix.
 
87
 
 
88
The policy server replies with any action that is allowed in a Postfix SMTPD
 
89
access(5) table. Example:
 
90
 
 
91
    action=defer_if_permit Service temporarily unavailable
 
92
    [empty line]
 
93
 
 
94
This causes the Postfix SMTP server to reject the request with a 450 temporary
 
95
error code and with text "Service temporarily unavailable", if the Postfix SMTP
 
96
server finds no reason to reject the request permanently.
 
97
 
 
98
In case of trouble the policy server must not send a reply. Instead the server
 
99
must log a warning and disconnect. Postfix will retry the request at some later
 
100
time.
 
101
 
 
102
PPoolliiccyy cclliieenntt//sseerrvveerr ccoonnffiigguurraattiioonn
 
103
 
 
104
The Postfix delegated policy client can connect to a TCP socket or to a UNIX-
 
105
domain socket. Examples:
 
106
 
 
107
    inet:127.0.0.1:9998
 
108
    unix:/some/where/policy
 
109
    unix:private/policy
 
110
 
 
111
The first example specifies that the policy server listens on a TCP socket at
 
112
127.0.0.1 port 9998. The second example specifies an absolute pathname of a
 
113
UNIX-domain socket. The third example specifies a pathname relative to the
 
114
Postfix queue directory; use this for policy servers that are spawned by the
 
115
Postfix master daemon.
 
116
 
 
117
To create a policy service that listens on a UNIX-domain socket called
 
118
"policy", and that runs under control of the Postfix spawn(8) daemon, you would
 
119
use something like this:
 
120
 
 
121
     1 /etc/postfix/master.cf:
 
122
     2     policy  unix  -       n       n       -       -       spawn
 
123
     3       user=nobody argv=/some/where/policy-server
 
124
     4
 
125
     5 /etc/postfix/main.cf:
 
126
     6     smtpd_recipient_restrictions =
 
127
     7         ...
 
128
     8         reject_unauth_destination
 
129
     9         check_policy_service unix:private/policy
 
130
    10         ...
 
131
    11     policy_time_limit = 3600
 
132
 
 
133
NOTES:
 
134
 
 
135
  * Lines 2, 11: the Postfix spawn(8) daemon by default kills its child process
 
136
    after 1000 seconds. This is too short for a policy daemon that may run for
 
137
    as long as an SMTP client is connected to an SMTP server process. The
 
138
    default time limit is overruled in main.cf with an explicit
 
139
    "policy_time_limit" setting. The name of the parameter is the name of the
 
140
    master.cf entry ("policy") concatenated with the "_time_limit" suffix.
 
141
 
 
142
  * Lines 8, 9: always specify "check_policy_service" AFTER
 
143
    "reject_unauth_destination" or else your system could become an open relay.
 
144
 
 
145
  * Solaris UNIX-domain sockets do not work reliably. Use TCP sockets instead:
 
146
 
 
147
     1 /etc/postfix/master.cf:
 
148
     2     127.0.0.1:9998  inet  n       n       n       -       -       spawn
 
149
     3       user=nobody argv=/some/where/policy-server
 
150
     4
 
151
     5 /etc/postfix/main.cf:
 
152
     6     smtpd_recipient_restrictions =
 
153
     7         ...
 
154
     8         reject_unauth_destination
 
155
     9         check_policy_service inet:127.0.0.1:9998
 
156
    10         ...
 
157
    11     127.0.0.1:9998_time_limit = 3600
 
158
 
 
159
Other configuration parameters that control the client side of the policy
 
160
delegation protocol:
 
161
 
 
162
  * smtpd_policy_service_max_idle (default: 300s): The amount of time before
 
163
    the Postfix SMTP server closes an unused policy client connection.
 
164
 
 
165
  * smtpd_policy_service_max_ttl (default: 1000s): The amount of time before
 
166
    the Postfix SMTP server closes an active policy client connection.
 
167
 
 
168
  * smtpd_policy_service_timeout (default: 100s): The time limit to connect to,
 
169
    send to or receive from a policy server.
 
170
 
 
171
EExxaammppllee:: ggrreeyylliisstt ppoolliiccyy sseerrvveerr
 
172
 
 
173
Greylisting is a defense against junk email that is described at http://
 
174
www.greylisting.org/. The idea was discussed on the postfix-users mailing list
 
175
one year before it was popularized.
 
176
 
 
177
The file examples/smtpd-policy/greylist.pl in the Postfix source tree
 
178
implements a simplified greylist policy server. This server stores a time stamp
 
179
for every (client, sender, recipient) triple. By default, mail is not accepted
 
180
until a time stamp is more than 60 seconds old. This stops junk mail with
 
181
randomly selected sender addresses, and mail that is sent through randomly
 
182
selected open proxies. It also stops junk mail from spammers that change their
 
183
IP address frequently.
 
184
 
 
185
Copy examples/smtpd-policy/greylist.pl to /usr/libexec/postfix or whatever
 
186
location is appropriate for your system.
 
187
 
 
188
In the greylist.pl Perl script you need to specify the location of the greylist
 
189
database file, and how long mail will be delayed before it is accepted. The
 
190
default settings are:
 
191
 
 
192
    $database_name="/var/mta/greylist.db";
 
193
    $greylist_delay=60;
 
194
 
 
195
The /var/mta directory (or whatever you choose) should be writable by "nobody",
 
196
or by whatever username you configure below in master.cf for the policy
 
197
service.
 
198
 
 
199
Example:
 
200
 
 
201
    # mkdir /var/mta
 
202
    # chown nobody /var/mta
 
203
 
 
204
Note: DO NOT create the greylist database in a world-writable directory such as
 
205
/tmp or /var/tmp, and DO NOT create the greylist database in a file system that
 
206
may run out of space. Postfix can survive "out of space" conditions with the
 
207
mail queue and with the mailbox store, but it cannot survive a corrupted
 
208
greylist database. If the file becomes corrupted you may not be able to receive
 
209
mail at all until you delete the file by hand.
 
210
 
 
211
The greylist.pl Perl script can be run under control by the Postfix master
 
212
daemon. For example, to run the script as user "nobody", using a UNIX-domain
 
213
socket that is accessible by Postfix processes only:
 
214
 
 
215
    1 /etc/postfix/master.cf:
 
216
    2     policy  unix  -       n       n       -       -       spawn
 
217
    3       user=nobody argv=/usr/bin/perl /usr/libexec/postfix/greylist.pl
 
218
    4
 
219
    5 /etc/postfix/main.cf:
 
220
    6      policy_time_limit = 3600
 
221
 
 
222
Notes:
 
223
 
 
224
  * Line 3: Specify "greylist.pl -v" for verbose logging of each request and
 
225
    reply.
 
226
 
 
227
  * Lines 2, 6: the Postfix spawn(8) daemon by default kills its child process
 
228
    after 1000 seconds. This is too short for a policy daemon that may run for
 
229
    as long as an SMTP client is connected to an SMTP server process. The
 
230
    default time limit is overruled in main.cf with an explicit
 
231
    "policy_time_limit" setting. The name of the parameter is the name of the
 
232
    master.cf entry ("policy") concatenated with the "_time_limit" suffix.
 
233
 
 
234
On Solaris you must use inet: style sockets instead of unix: style, as detailed
 
235
in the "Policy client/server configuration" section above.
 
236
 
 
237
    1 /etc/postfix/master.cf:
 
238
    2     127.0.0.1:9998  inet  n       n       n       -       -       spawn
 
239
    3       user=nobody argv=/usr/bin/perl /usr/libexec/postfix/greylist.pl
 
240
    4
 
241
    5 /etc/postfix/main.cf:
 
242
    6      127.0.0.1:9998_time_limit = 3600
 
243
 
 
244
To invoke this service you would specify "check_policy_service inet:127.0.0.1:
 
245
9998".
 
246
 
 
247
GGrreeyylliissttiinngg mmaaiill ffrroomm ffrreeqquueennttllyy ffoorrggeedd ddoommaaiinnss
 
248
 
 
249
It is relatively safe to turn on greylisting for specific domains that often
 
250
appear in forged email. A list of frequently forged MAIL FROM domains can be
 
251
found at http://www.monkeys.com/anti-spam/filtering/sender-domain-validate.in.
 
252
 
 
253
     1 /etc/postfix/main.cf:
 
254
     2     smtpd_recipient_restrictions =
 
255
     3         reject_unlisted_recipient
 
256
     4         ...
 
257
     5         reject_unauth_destination
 
258
     6         check_sender_access hash:/etc/postfix/sender_access
 
259
     7         ...
 
260
     8     restriction_classes = greylist
 
261
     9     greylist = check_policy_service unix:private/policy
 
262
    10
 
263
    11 /etc/postfix/sender_access:
 
264
    12     aol.com     greylist
 
265
    13     hotmail.com greylist
 
266
    14     bigfoot.com greylist
 
267
    15     ... etcetera ...
 
268
 
 
269
NOTES:
 
270
 
 
271
  * Line 9: On Solaris you must use inet: style sockets instead of unix: style,
 
272
    as detailed in the "Example: greylist policy server" section above.
 
273
 
 
274
  * Line 6: Be sure to specify "check_sender_access" AFTER
 
275
    "reject_unauth_destination" or else your system could become an open mail
 
276
    relay.
 
277
 
 
278
  * Line 3: With Postfix 2.0 snapshot releases, "reject_unlisted_recipient" is
 
279
    called "check_recipient_maps". Postfix 2.1 understands both forms.
 
280
 
 
281
  * Line 3: The greylist database gets polluted quickly with bogus addresses.
 
282
    It helps if you protect greylist lookups with other restrictions that
 
283
    reject unknown senders and/or recipients.
 
284
 
 
285
GGrreeyylliissttiinngg aallll yyoouurr mmaaiill
 
286
 
 
287
If you turn on greylisting for all mail you will almost certainly want to make
 
288
exceptions for mailing lists that use one-time sender addresses, because such
 
289
mailing lists can pollute your greylist database relatively quickly.
 
290
 
 
291
     1 /etc/postfix/main.cf:
 
292
     2     smtpd_recipient_restrictions =
 
293
     3         reject_unlisted_recipient
 
294
     4         ...
 
295
     5         reject_unauth_destination
 
296
     6         check_sender_access hash:/etc/postfix/sender_access
 
297
     7         check_policy_service unix:private/policy
 
298
     8         ...
 
299
     9
 
300
    10 /etc/postfix/sender_access:
 
301
    11     securityfocus.com OK
 
302
    12     ...
 
303
 
 
304
NOTES:
 
305
 
 
306
  * Line 7: On Solaris you must use inet: style sockets instead of unix: style,
 
307
    as detailed in the "Example: greylist policy server" section above.
 
308
 
 
309
  * Lines 6-7: Be sure to specify check_sender_access and check_policy_service
 
310
    AFTER reject_unauth_destination or else your system could become an open
 
311
    mail relay.
 
312
 
 
313
  * Line 3: The greylist database gets polluted quickly with bogus addresses.
 
314
    It helps if you precede greylist lookups with restrictions that reject
 
315
    unknown senders and/or recipients.
 
316
 
 
317
RRoouuttiinnee ggrreeyylliisstt mmaaiinntteennaannccee
 
318
 
 
319
The greylist database grows over time, because the greylist server never
 
320
removes database entries. If left unattended, the greylist database will
 
321
eventually run your file system out of space.
 
322
 
 
323
When the status file size exceeds some threshold you can simply rename or
 
324
remove the file without adverse effects; Postfix automatically creates a new
 
325
file. In the worst case, new mail will be delayed by an hour or so. To lessen
 
326
the impact, rename or remove the file in the middle of the night at the
 
327
beginning of a weekend.
 
328
 
 
329
EExxaammppllee PPeerrll ggrreeyylliisstt sseerrvveerr
 
330
 
 
331
This is the Perl subroutine that implements the example greylist policy. It is
 
332
part of a general purpose sample policy server that is distributed with the
 
333
Postfix source as examples/smtpd-policy/greylist.pl.
 
334
 
 
335
#
 
336
# greylist status database and greylist time interval. DO NOT create the
 
337
# greylist status database in a world-writable directory such as /tmp
 
338
# or /var/tmp. DO NOT create the greylist database in a file system
 
339
# that can run out of space.
 
340
#
 
341
$database_name="/var/mta/greylist.db";
 
342
$greylist_delay=60;
 
343
 
 
344
#
 
345
# Demo SMTPD access policy routine. The result is an action just like
 
346
# it would be specified on the right-hand side of a Postfix access
 
347
# table.  Request attributes are available via the %attr hash.
 
348
#
 
349
sub smtpd_access_policy {
 
350
    my($key, $time_stamp, $now);
 
351
 
 
352
    # Open the database on the fly.
 
353
    open_database() unless $database_obj;
 
354
 
 
355
    # Lookup the time stamp for this client/sender/recipient.
 
356
    $key =
 
357
        lc $attr{"client_address"}."/".$attr{"sender"}."/".$attr{"recipient"};
 
358
    $time_stamp = read_database($key);
 
359
    $now = time();
 
360
 
 
361
    # If new request, add this client/sender/recipient to the database.
 
362
    if ($time_stamp == 0) {
 
363
        $time_stamp = $now;
 
364
        update_database($key, $time_stamp);
 
365
    }
 
366
 
 
367
    # The result can be any action that is allowed in a Postfix access(5) map.
 
368
    #
 
369
    # To label the mail, return ``PREPEND headername: headertext''
 
370
    #
 
371
    # In case of success, return ``DUNNO'' instead of ``OK'', so that the
 
372
    # check_policy_service restriction can be followed by other restrictions.
 
373
    #
 
374
    # In case of failure, return ``DEFER_IF_PERMIT optional text...'',
 
375
    # so that mail can still be blocked by other access restrictions.
 
376
    #
 
377
    syslog $syslog_priority, "request age %d", $now - $time_stamp if $verbose;
 
378
    if ($now - $time_stamp > $greylist_delay) {
 
379
        return "dunno";
 
380
    } else {
 
381
        return "defer_if_permit Service temporarily unavailable";
 
382
    }
 
383
}
 
384