1.\"	$MirOS: src/libexec/spamd/spamd.8,v 1.2 2009/07/01 18:16:01 tg Exp $
2.\"	$OpenBSD: spamd.8,v 1.100 2007/03/26 19:08:09 jmc Exp $
3.\"
4.\" Copyright (c) 2002 Theo de Raadt.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25.\"
26.Dd $Mdocdate: July 1 2009 $
27.Dt SPAMD 8
28.Os
29.Sh NAME
30.Nm spamd
31.Nd spam deferral daemon
32.Sh SYNOPSIS
33.Nm spamd
34.Bk -words
35.Op Fl 45bdv
36.Op Fl B Ar maxblack
37.Op Fl c Ar maxcon
38.Oo
39.Fl G
40.Ar passtime : Ns Ar greyexp : Ns Ar whiteexp
41.Oc
42.Op Fl h Ar hostname
43.Op Fl l Ar address
44.Op Fl M Ar address
45.Op Fl n Ar name
46.Op Fl p Ar port
47.Op Fl S Ar secs
48.Op Fl s Ar secs
49.Op Fl w Ar window
50.Op Fl Y Ar synctarget
51.Op Fl y Ar synclisten
52.Ek
53.Sh DESCRIPTION
54.Nm
55is a fake
56.Xr sendmail 8 Ns -like
57daemon which rejects false mail.
58It is designed to be very efficient so that it does not slow down the
59receiving machine.
60.Pp
61.Nm
62considers sending hosts to be of three types:
63.Pp
64.Em blacklisted
65hosts are redirected to
66.Nm
67and
68.Em tarpitted
69i.e. they are communicated with very slowly
70to consume the sender's resources.
71Mail is rejected with either a 450 or 550 error message.
72A blacklisted host will not be allowed to talk to a real mail server.
73.Pp
74.Em whitelisted
75hosts do not talk to
76.Nm .
77Their connections are instead sent to a real mail server,
78such as
79.Xr sendmail 8 .
80.Pp
81.Em greylisted
82hosts are redirected to
83.Nm ,
84but
85.Nm
86has not yet decided if they are likely spammers.
87They are given a temporary failure message by
88.Nm
89when they try to deliver mail.
90.Pp
91When
92.Nm
93is run in default mode,
94it will greylist connections from new hosts.
95Depending on its configuration,
96it may choose to blacklist the host or,
97if the checks described below are met,
98eventually whitelist it.
99When
100.Nm
101is run in blacklist-only mode,
102using the
103.Fl b
104flag,
105it will consult a pre-defined set of blacklist addresses
106to decide whether to tarpit the host or not.
107.Pp
108When a sending hosts talks to
109.Nm ,
110the reply will be
111.Em stuttered .
112That is,
113the response will be sent back a character at a time, slowly.
114For blacklisted hosts,
115the entire dialogue is stuttered.
116For greylisted hosts,
117the default is to stutter for the first 10 seconds
118of dialogue only.
119.Pp
120The options are as follows:
121.Bl -tag -width Ds
122.It Fl 4
123For blacklisted entries, return error code 450 to the spammer (default).
124.It Fl 5
125For blacklisted entries, return error code 550 to the spammer.
126.It Fl B Ar maxblack
127The maximum number of concurrent blacklisted connections to stutter at.
128This value may not be greater than maxcon (see below).
129The default is
130.Ar maxcon
131\- 100.
132When this value is exceeded new blacklisted connections will not be stuttered
133at.
134.It Fl b
135Run in blacklist-only mode.
136.It Fl c Ar maxcon
137The maximum number of concurrent connections to allow.
138.Ar maxcon
139may not exceed
140.Va kern.maxfiles
141\- 200, and defaults to 800.
142.It Fl d
143Debug mode.
144.Nm
145does not
146.Xr fork 2
147into the background.
148.It Xo
149.Fl G
150.Ar passtime : Ns Ar greyexp : Ns Ar whiteexp
151.Xc
152Adjust the three time parameters for greylisting.
153.Ar passtime
154defaults to 25 (minutes),
155.Ar greyexp
156to 4 (hours),
157and
158.Ar whiteexp
159to 864 (hours, approximately 36 days).
160.It Fl h Ar hostname
161The hostname that is reported in the SMTP banner.
162.It Fl l Ar address
163Specify the local address to which
164.Nm
165is to
166.Xr bind 2 .
167By default
168.Nm
169listens on all local addresses.
170.It Fl M Ar address
171Specify a local IP address which is listed as a low priority MX record,
172used to identify and trap hosts that connect to MX hosts out of order.
173See
174.Sx GREYTRAPPING
175below for details.
176.It Fl n Ar name
177The SMTP version banner that is reported upon initial connection.
178.It Fl p Ar port
179Specify a different port number from the default port that
180.Nm
181should listen for redirected SMTP connections on.
182The default port is found by looking for the named service
183.Dq spamd
184using
185.Xr getservbyname 3 .
186.It Fl S Ar secs
187Stutter at greylisted connections for the specified amount
188of seconds, after which the connection is not stuttered at.
189Defaults to 10.
190.It Fl s Ar secs
191Delay each character sent to the client by the specified
192amount of seconds.
193Defaults to 1.
194.It Fl v
195Enable verbose logging.
196By default
197.Nm
198logs connections, disconnections and blacklist matches to
199.Xr syslogd 8
200at
201.Dv LOG_INFO
202level.
203With verbose logging enabled, message detail
204including subject and recipient information is logged at
205.Dv LOG_INFO ,
206along with the message body and SMTP dialogue being logged at
207.Dv LOG_DEBUG
208level.
209.It Fl w Ar window
210Set the socket receive buffer to this many bytes, adjusting the window size.
211.It Fl Y Ar synctarget
212Add target
213.Ar synctarget
214to receive synchronisation messages.
215.Ar synctarget
216can be either an IPv4 address for unicast messages
217or a network interface and optional TTL value for multicast messages
218to the group 224.0.1.240.
219If the multicast TTL is not specified, a default value of 1 is used.
220This option can be specified multiple times.
221See also
222.Sx SYNCHRONISATION
223below.
224.It Fl y Ar synclisten
225Listen on
226.Ar synclisten
227for incoming synchronisation messages.
228The format for
229.Ar synclisten
230is the same as for
231.Ar synctarget ,
232above.
233This option can be specified only once.
234See also
235.Sx SYNCHRONISATION
236below.
237.El
238.Pp
239When run in default mode,
240connections receive the pleasantly innocuous temporary failure of:
241.Bd -literal -offset 4n
242451 Temporary failure, please try again later.
243.Ed
244.Pp
245This happens in the SMTP dialogue
246immediately after the DATA command is received from the client.
247.Nm
248will use the db file in
249.Pa /var/db/spamd
250to track these connections to
251.Nm
252by connecting IP address, HELO/EHLO, envelope-from, and envelope-to, or
253.Em tuple
254for short.
255.Pp
256A previously unseen tuple is added to the
257.Pa /var/db/spamd
258database, recording the time an initial connection attempt was seen.
259After
260.Em passtime
261minutes if
262.Nm
263sees a retried attempt to deliver mail for the same tuple,
264.Nm
265will whitelist the connecting address by adding it as a
266whitelist entry to
267.Pa /var/db/spamd .
268.Pp
269.Nm
270regularly scans the
271.Pa /var/db/spamd
272database and configures all whitelist addresses as the
273.Xr pf 4
274.Aq spamd-white
275table,
276allowing connections to pass to the real MTA.
277Any addresses not found in
278.Aq spamd-white
279are redirected to
280.Nm .
281The following
282.Xr pf.conf 5
283example is suggested:
284.Bd -literal -offset 4n
285table \*(Ltspamd-white\*(Gt persist
286no rdr inet proto tcp from \*(Ltspamd-white\*(Gt to any \e
287    port smtp
288rdr pass inet proto tcp from any to any \e
289    port smtp -\*(Gt 127.0.0.1 port spamd
290.Ed
291.Pp
292.Nm
293removes tuple entries from the
294.Pa /var/db/spamd
295database if delivery has not been retried within
296.Em greyexp
297hours from the initial time a connection is seen.
298The default is 4 hours as this is the most common setting after which
299MTAs will give up attempting to retry delivery of a message.
300.Pp
301.Nm
302removes whitelist entries from the
303.Pa /var/db/spamd
304database if no mail delivery activity has been seen from the
305whitelisted address by
306.Xr spamlogd 8
307within
308.Em whiteexp
309hours from the initial time an address
310is whitelisted.
311The default is 36 days to allow for the delivery of
312monthly mailing list digests without greylist delays every time.
313.Pp
314.Xr spamd-setup 8
315should be run periodically by
316.Xr cron 8 .
317Use
318.Xr crontab 1
319to uncomment the entry in root's crontab.
320.Pp
321.Xr spamlogd 8
322should be used to update the whitelist entries in
323.Pa /var/db/spamd
324when connections are seen to pass to the real MTA on the
325.Em smtp
326port.
327.Pp
328.Xr spamdb 8
329can be used to examine and alter the contents of
330.Pa /var/db/spamd .
331See
332.Xr spamdb 8
333for further information.
334.Pp
335.Nm
336sends log messages to
337.Xr syslogd 8
338using
339.Em facility
340daemon and, with increasing verbosity,
341.Em level
342err, warn, info, and debug.
343The following
344.Xr syslog.conf 5
345section can be used to log connection details to a dedicated file:
346.Bd -literal -offset indent
347!spamd
348daemon.err;daemon.warn;daemon.info	/var/log/spamd
349.Ed
350.Sh GREYTRAPPING
351When running
352.Nm
353in default mode,
354it may be useful to define
355.Em spamtrap
356destination addresses to catch spammers as they send mail from greylisted
357hosts.
358Such spamtrap addresses affect only greylisted connections to
359.Nm
360and are used to temporarily blacklist a host that is obviously sending spam.
361Unused email addresses or email addresses on spammers' lists are very
362useful for this.
363When a host that is currently greylisted attempts to send mail to a
364spamtrap address,
365it is blacklisted for 24 hours by adding the host to the
366.Nm
367blacklist
368.Aq spamd-greytrap .
369Spamtrap addresses are added to the
370.Pa /var/db/spamd
371database with the following
372.Xr spamdb 8
373command:
374.Pp
375.Dl # spamdb -T -a 'spamtrap@mydomain.org'
376.Pp
377See
378.Xr spamdb 8
379for further details.
380.Pp
381The file
382.Pa /etc/mail/spamd.alloweddomains
383can be used to specify a list of domainname suffixes, one per line, one of
384which must match each destination email address in the greylist.
385Any destination address which does not match one of the suffixes listed in
386.Pa spamd.alloweddomains
387will be trapped, exactly as if it were sent to a spamtrap address.
388Entries beginning with an equals sign
389.Pq Sq =
390will be taken as full match instead of suffix match.
391This can be used for whitelisting on recipient addresses, if the full
392set of possible localparts is known.
393.Pp
394For example, if
395.Pa spamd.alloweddomains
396contains:
397.Bd -literal -offset indent
398@humpingforjesus.com
399obtuse.com
400=paul@apostles.humpingforjesus.com
401.Ed
402.Pp
403The following destination addresses
404.Em would not
405cause the sending host to be trapped:
406.Bd -literal -offset indent
407beardedclams@humpingforjesus.com
408beck@obtuse.com
409beck@snouts.obtuse.com
410paul@apostles.humpingforjesus.com
411.Ed
412.Pp
413However the following addresses
414.Em would
415cause the sending host to be trapped:
416.Bd -literal -offset indent
417peter@apostles.humpingforjesus.com
418bigbutts@bofh.ucs.ualberta.ca
419.Ed
420.Pp
421A low priority MX IP address may be specified with the
422.Fl M
423option.
424When
425.Nm
426has such an address specified, no host may enter new greylist
427tuples when connecting to this address; only existing entries
428may be updated.
429Any host attempting to make new deliveries to
430the low priority MX for which a tuple has not previously
431been seen will be trapped.
432.Pp
433Note that is is important to ensure that a host running
434.Nm
435with the low priority MX address active must see all the greylist
436changes for a higher priority MX host for the same domains, either by
437being synchronised with it, or by receiving the connections itself to
438the higher priority MX on another IP address (which may be an IP alias).
439This will ensure that hosts are not trapped erroneously if the higher
440priority MX is unavailable.
441For example, on a host which is an existing MX record for a domain of
442value 10, a second IP address with MX of value 99 (a higher number, and
443therefore lower priority) would ensure that any RFC conformant client
444would attempt delivery to the IP address with the MX value of 10
445first, and should not attempt to deliver to the address with MX value 99.
446.Sh BLACKLISTING
447The normal way that spam has been dealt with in the past is to either
448accept and drop, or outright block.
449When configured to use 450 responses,
450.Nm
451takes neither of these actions: it rejects the mail back to the senders'
452queue.
453.Pp
454When running in default mode, the
455.Xr pf.conf 5
456rules described above are sufficient.
457However when running in blacklist-only mode,
458a slightly modified
459.Xr pf.conf 5
460ruleset is required,
461redirecting any addresses found in the
462.Aq spamd
463table to
464.Nm .
465Any other addresses
466are passed to the real MTA.
467.Bd -literal -offset 4n
468table \*(Ltspamd\*(Gt persist
469rdr pass inet proto tcp from \*(Ltspamd\*(Gt to any \e
470    port smtp -\*(Gt 127.0.0.1 port spamd
471.Ed
472.Pp
473Addresses can be loaded into the
474.Em table ,
475like:
476.Bd -literal -offset 4n
477# pfctl -q -t spamd -T replace -f /usr/local/share/spammers
478.Ed
479.Pp
480.Xr spamd-setup 8
481can also be used to load addresses into the
482.Aq spamd
483table.
484It has the added benefit of being able to remove addresses from
485blacklists, and will connect to
486.Nm
487over a localhost socket, giving
488.Nm
489information about each source of blacklist addresses, as well as custom
490rejection messages for each blacklist source
491that can be used to let any real person whose mail
492is deferred by
493.Nm
494know why their address has been listed
495from sending mail.
496This is important as it allows legitimate mail
497senders to pressure spam sources into behaving properly so that they
498may be removed from the relevant blacklists.
499.Sh CONFIGURATION CONNECTIONS
500.Nm
501listens for configuration connections on the port identified by the
502named service
503.Dq spamd-cfg
504(see
505.Xr services 5 ) .
506The configuration socket listens only on the INADDR_LOOPBACK
507address.
508Configuration of spamd is done by connecting to the configuration
509socket, and sending blacklist information, one blacklist per line.
510Each blacklist consists of a name, a message to reject mail
511with, and addresses in CIDR format, all separated by semicolons (;):
512.Bd -literal -offset indent
513tag;"rejection message";aaa.bbb.ccc.ddd/mm;aaa.bbb.ccc.ddd/mm
514.Ed
515.Pp
516The rejection message must be inside double quotes.
517A \e" will produce a double quote in the output.
518\en will produce a newline.
519%A will expand to the connecting IP address in dotted quad format.
520%% may be used to produce a single % in the output.
521\e\e will produce a single \e.
522.Nm
523will reject mail by displaying all the messages from all blacklists in which
524a connecting address is matched.
525.Xr spamd-setup 8
526is normally used to configure this information.
527.Sh SYNCHRONISATION
528.Nm
529supports realtime synchronisation of greylisting states between
530a number of
531.Nm
532daemons running on multiple machines,
533using the
534.Fl Y
535and
536.Fl y
537options.
538.Pp
539The following example will accept incoming multicast and unicast
540synchronisation messages, and send outgoing multicast messages through
541the network interface
542.Ar em0 :
543.Bd -literal -offset indent
544# /usr/libexec/spamd -y em0 -Y em0
545.Ed
546.Pp
547The second example will increase the multicast TTL to a value of 2,
548add the unicast targets
549.Ar foo.somewhere.org
550and
551.Ar bar.somewhere.org ,
552and accept incoming unicast messages sent to
553.Ar example.somewhere.org
554only.
555.Bd -literal -offset indent
556# /usr/libexec/spamd -y example.somewhere.org -Y em0:2 \e
557	-Y foo.somewhere.org -Y bar.somewhere.org
558.Ed
559.Pp
560If the file
561.Pa /etc/mail/spamd.key
562exists,
563.Nm
564will calculate the message-digest fingerprint (checksum) for the file
565and use it as a shared key to authenticate the synchronisation messages.
566The file itself can contain any data.
567For example, to create a secure random key:
568.Bd -literal -offset indent
569# dd if=/dev/arandom of=/etc/mail/spamd.key bs=2048 count=1
570.Ed
571.Pp
572The file needs to be copied to all hosts
573sending or receiving synchronisation messages.
574.Sh FILES
575.Bl -tag -width "/etc/mail/spamd.alloweddomainsXX" -compact
576.It /etc/mail/spamd.alloweddomains
577Required suffixes for greytrapping.
578.It /etc/mail/spamd.conf
579Default configuration file.
580.It /etc/mail/spamd.key
581Authentication key for synchronisation messages.
582.It /var/db/spamd
583Greylisting database.
584.El
585.Sh SEE ALSO
586.Xr pf.conf 5 ,
587.Xr services 5 ,
588.Xr spamd.conf 5 ,
589.Xr syslog.conf 5 ,
590.Xr pfctl 8 ,
591.Xr spamd-setup 8 ,
592.Xr spamdb 8 ,
593.Xr spamlogd 8 ,
594.Xr syslogd 8
595.Sh HISTORY
596The
597.Nm
598command first appeared in
599.Ox 3.3 .
600.Pp
601Previous versions of
602.Nm
603required traps to be entered into the database including the enclosing
604\*(Lt\*(Gt characters;
605current versions expect only the email address without the enclosing
606\*(Lt\*(Gt characters.
607.Sh BUGS
608.Nm
609currently uses the user
610.Dq _spamd
611outside a chroot jail when running in default mode, and requires
612the greylisting database in
613.Pa /var/db/spamd
614to be owned by the
615.Dq _spamd
616user.
617This is wrong and should change to a distinct user from the
618one used by the chrooted
619.Nm
620process.
621