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