1.\" $OpenBSD: spamd.8,v 1.135 2019/07/24 18:41:05 mestre 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: July 24 2019 $ 26.Dt SPAMD 8 27.Os 28.Sh NAME 29.Nm spamd 30.Nd spam deferral daemon 31.Sh SYNOPSIS 32.Nm spamd 33.Op Fl 45bdv 34.Op Fl B Ar maxblack 35.Op Fl C Ar file 36.Op Fl c Ar maxcon 37.Op Fl G Ar passtime : Ns Ar greyexp : Ns Ar whiteexp 38.Op Fl h Ar hostname 39.Op Fl K Ar file 40.Op Fl l Ar address 41.Op Fl M Ar address 42.Op Fl n Ar name 43.Op Fl p Ar port 44.Op Fl S Ar secs 45.Op Fl s Ar secs 46.Op Fl w Ar window 47.Op Fl Y Ar synctarget 48.Op Fl y Ar synclisten 49.Sh DESCRIPTION 50.Nm 51is a fake mail daemon which rejects false mail. 52It is designed to be very efficient so that it does not slow down the 53receiving machine. 54.Pp 55.Nm 56considers sending hosts to be of three types: 57.Pp 58.Em blacklisted 59hosts are diverted to 60.Nm 61and 62.Em tarpitted 63i.e. they are communicated with very slowly 64to consume the sender's resources. 65Mail is rejected with either a 450 or 550 error message. 66A blacklisted host will not be allowed to talk to a real mail server. 67.Pp 68.Em whitelisted 69hosts do not talk to 70.Nm . 71Their connections are instead sent to a real mail server, 72such as 73.Xr smtpd 8 . 74.Pp 75.Em greylisted 76hosts are diverted to 77.Nm , 78but 79.Nm 80has not yet decided if they are likely spammers. 81They are given a temporary failure message by 82.Nm 83when they try to deliver mail. 84.Pp 85When 86.Nm 87is run in default mode, 88it will greylist connections from new hosts. 89Depending on its configuration, 90it may choose to blacklist the host or, 91if the checks described below are met, 92eventually whitelist it. 93When 94.Nm 95is run in blacklist-only mode, 96using the 97.Fl b 98flag, 99it will consult a pre-defined set of blacklist addresses 100to decide whether to tarpit the host or not. 101.Pp 102When a sending host talks to 103.Nm , 104the reply will be 105.Em stuttered . 106That is, 107the response will be sent back a character at a time, slowly. 108For blacklisted hosts, 109the entire dialogue is stuttered. 110For greylisted hosts, 111the default is to stutter for the first 10 seconds 112of dialogue only. 113.Pp 114The options are as follows: 115.Bl -tag -width Ds 116.It Fl 4 117For blacklisted entries, return error code 450 to the spammer (default). 118.It Fl 5 119For blacklisted entries, return error code 550 to the spammer. 120.It Fl B Ar maxblack 121The maximum number of concurrent blacklisted connections to stutter at. 122This value may not be greater than maxcon (see below). 123The default is 124.Ar maxcon 125\- 100. 126When this value is exceeded, new blacklisted connections will not be 127stuttered at. 128.It Fl b 129Run in blacklist-only mode. 130.It Fl C Ar file 131Load the certificate for TLS from the given 132.Ar file . 133.It Fl c Ar maxcon 134The maximum number of concurrent connections to allow. 135.Ar maxcon 136may not exceed 137.Va kern.maxfiles 138\- 200, and defaults to 800. 139.It Fl d 140Debug mode. 141.Nm 142does not 143.Xr fork 2 144into the background. 145.It Xo 146.Fl G 147.Ar passtime : Ns Ar greyexp : Ns Ar whiteexp 148.Xc 149Adjust the three time parameters for greylisting. 150.Ar passtime 151defaults to 25 (minutes), 152.Ar greyexp 153to 4 (hours), 154and 155.Ar whiteexp 156to 864 (hours, approximately 36 days). 157.It Fl h Ar hostname 158The hostname that is reported in the SMTP banner. 159.It Fl K Ar file 160Load the private key for TLS from the given 161.Ar file . 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 the localhost address 127.0.0.1. 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 diverted 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. 189The default is 10; maximum is 90. 190.It Fl s Ar secs 191Delay each character sent to the client by the specified 192amount of seconds. 193The default is 1; maximum is 10. 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 227network interface for incoming synchronisation messages. 228This option can be specified only once. 229See also 230.Sx SYNCHRONISATION 231below. 232.El 233.Pp 234When run in default mode, 235connections receive the pleasantly innocuous temporary failure of: 236.Bd -literal -offset 4n 237451 Temporary failure, please try again later. 238.Ed 239.Pp 240This happens in the SMTP dialogue 241immediately after the DATA command is received from the client. 242.Nm 243will use the db file in 244.Pa /var/db/spamd 245to track these connections to 246.Nm 247by connecting IP address, HELO/EHLO, envelope-from, and envelope-to, or 248.Em tuple 249for short. 250Hosts which connect but do not attempt to deliver mail 251will not generate a tuple and always be ignored. 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 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 322to update the blacklists configured in 323.Xr spamd.conf 5 . 324Use 325.Xr crontab 1 326to uncomment the entry in root's crontab. 327When run in blacklist-only mode, 328the 329.Fl b 330flag should be specified. 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.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<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, 472diverting any addresses found in the <spamd> table to 473.Nm . 474Any other addresses 475are passed to the real MTA. 476.Bd -literal -offset 4n 477table <spamd> persist 478pass in on egress inet proto tcp from <spamd> to any port smtp \e 479 divert-to 127.0.0.1 port spamd 480.Ed 481.Pp 482Addresses can be loaded into the 483.Em table , 484like: 485.Bd -literal -offset 4n 486# pfctl -q -t spamd -T replace -f /usr/local/share/spammers 487.Ed 488.Pp 489.Xr spamd-setup 8 490can also be used to load addresses into the <spamd> table. 491It has the added benefit of being able to remove addresses from 492blacklists, and will connect to 493.Nm 494over a localhost socket, giving 495.Nm 496information about each source of blacklist addresses, as well as custom 497rejection messages for each blacklist source 498that can be used to let any real person whose mail 499is deferred by 500.Nm 501know why their address has been listed 502from sending mail. 503This is important as it allows legitimate mail 504senders to pressure spam sources into behaving properly so that they 505may be removed from the relevant blacklists. 506.Sh CONFIGURATION CONNECTIONS 507.Nm 508listens for configuration connections on the port identified by the 509named service 510.Dq spamd-cfg 511(see 512.Xr services 5 ) . 513The configuration socket listens only on the INADDR_LOOPBACK 514address. 515Configuration of spamd is done by connecting to the configuration 516socket, and sending blacklist information, one blacklist per line. 517Each blacklist consists of a name, a message to reject mail 518with, and addresses in CIDR format, all separated by semicolons (;): 519.Bd -literal -offset indent 520tag;"rejection message";aaa.bbb.ccc.ddd/mm;aaa.bbb.ccc.ddd/mm 521.Ed 522.Pp 523The rejection message must be inside double quotes. 524A \e" will produce a double quote in the output. 525\en will produce a newline. 526%A will expand to the connecting IP address in dotted quad format. 527%% may be used to produce a single % in the output. 528\e\e will produce a single \e. 529.Nm 530will reject mail by displaying all the messages from all blacklists in which 531a connecting address is matched. 532.Xr spamd-setup 8 533is normally used to configure this information. 534.Sh SYNCHRONISATION 535.Nm 536supports realtime synchronisation of spamd databases between 537a number of spamd 538daemons running on multiple machines, 539using the 540.Fl Y 541and 542.Fl y 543options. 544The databases are synchronised for greylisted and trapped entries; 545whitelisted entries and entries made manually using 546.Xr spamdb 8 547are not updated. 548.Pp 549The following example will accept incoming multicast and unicast 550synchronisation messages, and send outgoing multicast messages through 551the network interface 552.Ar em0 : 553.Bd -literal -offset indent 554# /usr/libexec/spamd -y em0 -Y em0 555.Ed 556.Pp 557The second example will increase the multicast TTL to a value of 2, 558add the unicast targets 559.Ar foo.somewhere.org 560and 561.Ar bar.somewhere.org , 562and accept incoming unicast messages received on 563.Ar bge0 564only. 565.Bd -literal -offset indent 566# /usr/libexec/spamd -y bge0 -Y em0:2 \e 567 -Y foo.somewhere.org -Y bar.somewhere.org 568.Ed 569.Pp 570If the file 571.Pa /etc/mail/spamd.key 572exists, 573.Nm 574will calculate the message-digest fingerprint (checksum) for the file 575and use it as a shared key to authenticate the synchronisation messages. 576The file itself can contain any data. 577For example, to create a secure random key: 578.Bd -literal -offset indent 579# dd if=/dev/random of=/etc/mail/spamd.key bs=2048 count=1 580.Ed 581.Pp 582The file needs to be copied to all hosts 583sending or receiving synchronisation messages. 584.Sh FILES 585.Bl -tag -width "/etc/mail/spamd.alloweddomainsXX" -compact 586.It Pa /etc/mail/spamd.alloweddomains 587Required suffixes for greytrapping. 588.It Pa /etc/mail/spamd.conf 589Default configuration file. 590.It Pa /etc/mail/spamd.key 591Authentication key for synchronisation messages. 592.It Pa /var/db/spamd 593Greylisting database. 594.El 595.Sh SEE ALSO 596.Xr pf.conf 5 , 597.Xr services 5 , 598.Xr spamd.conf 5 , 599.Xr syslog.conf 5 , 600.Xr pfctl 8 , 601.Xr spamd-setup 8 , 602.Xr spamdb 8 , 603.Xr spamlogd 8 , 604.Xr syslogd 8 605.Sh HISTORY 606The 607.Nm 608command first appeared in 609.Ox 3.3 . 610