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