MailScan for SMTP Servers: Complete Setup GuideMailScan is a high-performance mail-scanning solution designed to filter spam, viruses, and other malicious content at the SMTP server level. This guide walks you through selecting, installing, configuring, integrating, and tuning MailScan for production SMTP servers, with practical examples, troubleshooting tips, and best practices for reliability and compliance.
Overview: What MailScan Does and Why Use It
MailScan operates as a gateway or inline filter that inspects SMTP traffic in real time. It typically integrates with mail transfer agents (MTAs) such as Postfix, Exim, Sendmail, or Microsoft Exchange (via transport agents or SMTP relay). Key capabilities include:
- Virus and malware detection using antivirus engines (ClamAV, commercial engines).
- Spam filtering using Bayesian filters, DNS blacklists (RBLs), and heuristic/rule-based engines.
- Content and attachment policy enforcement (blocking, stripping, quarantine).
- Header and body rewriting for tagging, disclaimers, or tracking.
- Quarantine management and reporting for administrators and end-users.
- High-throughput scanning and multi-threaded performance suitable for large SMTP deployments.
Pre-installation Planning
-
Server roles and topology
- Decide whether MailScan will run on the same host as the MTA (inline) or on dedicated gateway servers. Dedicated gateways improve isolation and scalability.
- For high availability, prepare at least two MailScan gateway servers behind a load balancer or DNS MX priority configuration.
-
System requirements (example baseline)
- CPU: Multi-core (4+ cores recommended for moderate load).
- RAM: 8–32 GB depending on mail volume.
- Disk: SSD for OS and application; additional storage for quarantines/logs (rotate/archive).
- Network: 1 Gbps NIC or higher for heavy throughput.
- OS: Supported Linux distributions (CentOS/RHEL/Ubuntu/Debian) or Windows if using a Windows build.
-
Software dependencies
- MTA: Postfix/Exim/Sendmail/Microsoft Exchange.
- Antivirus: ClamAV or commercial AV engines.
- Spam engines: SpamAssassin or integrated spam modules.
- Database: Optional—MySQL/MariaDB/PostgreSQL for quarantine, logging, and reporting.
- Web server: Optional for admin UI (Apache/Nginx).
-
Security and compliance considerations
- TLS for SMTP (STARTTLS) to protect in-transit messages.
- Authentication & access controls for admin interfaces.
- Retention policies to meet legal or organizational requirements.
- Logging and SIEM integration for audit trails.
Installation
The exact installation steps depend on the MailScan product variant and OS. Below are generalized steps for a Linux-based deployment; adapt paths and package names to your product.
-
Obtain software
- Download the MailScan package from your vendor or repository. For open-source projects, clone the repository or install via package manager where available.
-
Install dependencies
- Example (Debian/Ubuntu):
sudo apt update sudo apt install postfix clamav spamassassin mysql-client nginx
- Example (RHEL/CentOS):
sudo yum install postfix clamav spamassassin mariadb-server httpd
- Example (Debian/Ubuntu):
-
Install MailScan
- Using a package:
sudo dpkg -i mailscan_<version>_amd64.deb # Debian/Ubuntu sudo rpm -ivh mailscan-<version>.rpm # RHEL/CentOS
- Or follow vendor installer:
sudo ./install.sh
- Using a package:
-
Initialize databases and services
- Create a database schema if MailScan uses a DB:
mysql -u root -p CREATE DATABASE mailscan; GRANT ALL ON mailscan.* TO 'mailscanuser'@'localhost' IDENTIFIED BY 'strongpassword'; FLUSH PRIVILEGES;
- Initialize MailScan schema:
sudo mailscan-db-init --dbuser mailscanuser --dbpass strongpassword
- Create a database schema if MailScan uses a DB:
-
Start and enable services
sudo systemctl enable --now mailscan sudo systemctl enable --now clamd sudo systemctl enable --now spamassassin
Integration with MTAs
Integration mode depends on whether MailScan operates as an SMTP proxy, milter, or transport filter.
-
Postfix (recommended approaches)
- Milter (if MailScan exposes a milter socket)
- In main.cf add:
smtpd_milters = unix:/var/run/mailscan/milter.sock non_smtpd_milters = $smtpd_milters milter_default_action = accept milter_protocol = 6
- Ensure Postfix can read/write the socket (permissions).
- In main.cf add:
- SMTP relay (MailScan as a separate SMTP service)
- Configure Postfix to relay outbound mail through MailScan:
relayhost = [127.0.0.1]:10025
- Configure MailScan to listen on port 10025 and forward to the upstream MTA on 127.0.0.1:25.
- Configure Postfix to relay outbound mail through MailScan:
- Milter (if MailScan exposes a milter socket)
-
Exim
- Use Exim’s ACLs or Exim filter to pipe messages to MailScan or use a listen/relay configuration similar to Postfix.
-
Sendmail
- Use milter interface or configure sendmail to relay via MailScan.
-
Microsoft Exchange
- Use Exchange Transport Agents (edge transport or hub transport) or place MailScan as an SMTP relay in front of Exchange.
Configuration Essentials
-
Scanning policies
- Define scanning tiers: header-only pre-accept checks, body & attachment scanning post-accept.
- Set size thresholds for scanning (e.g., skip scanning for > 50 MB and quarantine separately).
- Configure scanning rules to handle common attachment types (.exe, .zip, .js, Office macros).
-
Quarantine and delivery actions
- Actions typically include: deliver (with tag), reject/temporary reject, quarantine, strip attachment, or bounce.
- Configure quarantine retention and notification templates for admins and optionally for users.
-
Spam scoring and thresholds
- Use SpamAssassin or internal spam engine. Example thresholds:
- score < 3.0: accept
- 3.0 <= score < 6.0: tag subject “Suspected spam”
- score >= 6.0: quarantine or reject
- Enable bayesian training and integrate auto-whitelists or per-domain rules.
- Use SpamAssassin or internal spam engine. Example thresholds:
-
Virus engine setup
- Configure ClamAV (clamd) with up-to-date virus databases:
sudo freshclam sudo systemctl enable --now clamav-freshclam
- For commercial engines, follow vendor installation and licensing steps.
- Configure ClamAV (clamd) with up-to-date virus databases:
-
TLS and certificate management
- Enable STARTTLS on MailScan’s SMTP listeners.
- Use Let’s Encrypt or enterprise CA certs; automate renewal and reload of MailScan TLS configuration.
-
Logging and monitoring
- Syslog or structured logging: ensure MailScan logs to /var/log/mailscan or central syslog.
- Export metrics via Prometheus or integrate with Nagios/Zabbix for health checks (queue length, CPU, memory, scan latency).
- Track false positives/negatives and maintain a feedback loop.
Performance Tuning
-
Parallelism and threading
- Tune MailScan’s worker threads according to CPU cores and expected concurrent SMTP sessions.
- Example: worker_threads = number_of_cores * 1.5 (round down/up based on testing).
-
Caching
- Enable caching for RBL lookups and AV scan results (short TTL) to reduce external lookups.
- Cache common header checks and DNS queries.
-
Offload heavy tasks
- Consider scanning large attachments on a separate worker pool or during off-peak hours to avoid blocking SMTP responses.
- Use asynchronous quarantine handling for heavy I/O operations.
-
Resource limits
- Set per-message CPU/memory/time limits to avoid denial-of-service via huge/complex messages.
-
Benchmarking
- Use test harnesses to simulate SMTP load (tsung, smtp-source, or in-house tools).
- Measure throughput (msg/sec), latency (ms), and CPU/memory utilization while varying worker counts and queue depths.
High Availability and Scalability
-
Redundancy
- Deploy multiple MailScan instances across different physical hosts or availability zones.
- Use DNS MX records with multiple MX hosts to distribute inbound traffic.
-
Load balancing
- Use TCP load balancers (HAProxy, LVS) that understand SMTP states (stickiness not required for stateless SMTP) to distribute to MailScan backends.
-
Shared quarantine and DB
- Use a centralized database or replicated DB cluster for quarantines and reporting so all MailScan nodes share state.
-
Automatic failover
- Configure MTA retry policies and health checks to detect a failed MailScan node and route to a healthy node.
Admin UI, Reporting, and User Quarantine Access
-
Admin dashboard
- Install and secure the MailScan web UI with HTTPS, strong authentication (LDAP/AD, SSO), and role-based access.
- Limit access by IP or VPN.
-
Reporting
- Typical reports: blocked messages, quarantined items, virus detections, top senders/recipients, spam trends.
- Schedule daily/weekly summary reports via email or integrate with SIEM/log analytics.
-
End-user quarantine access
- Offer an opt-in user quarantine digest so users can release false positives.
- Log all user actions (release, delete) for auditability.
Security Best Practices
- Run MailScan under a dedicated, unprivileged service account.
- Keep AV and spam signature databases updated (automate freshclam and rule updates).
- Apply OS and application security patches regularly.
- Limit administrative access with MFA and IP restrictions.
- Sanitize and validate all user-supplied input in web UIs.
- Use transport-level TLS both between MTA and MailScan and between MailScan and upstream MTA.
Common Issues and Troubleshooting
-
Messages delayed or queued
- Check MailScan worker pool saturation and incoming connection queue.
- Review logs for slow AV engine responses (clamd) or external DNS/RBL timeouts.
- Increase worker threads or offload large attachment scanning.
-
High false positives
- Review spam scoring thresholds; lower sensitivity, enable safelist for trusted senders/domains.
- Update rules and whitelist critical senders, but avoid wide whitelists.
-
Socket/permission errors with Milter
- Ensure socket file permissions and ownership allow the MTA user to connect.
- Verify SELinux/AppArmor policies aren’t blocking socket access.
-
ClamAV reporting many errors or slow scans
- Ensure freshclam updates are working and AV engine has sufficient memory.
- Consider tuning clamd.conf (MaxThreads, StreamMaxLength) or switching to a commercial engine if performance is inadequate.
-
TLS handshake failures
- Verify certificate chain and that MailScan supports the cipher suites required by clients.
- Check time synchronization (NTP) as expired/invalid certs often come from clock skew.
Example: Minimal Postfix + MailScan Relay Setup
- Postfix forwards incoming mail to MailScan running on localhost:10025:
- Postfix main.cf:
relayhost = [127.0.0.1]:10025
- Postfix main.cf:
- MailScan listens on 10025, scans, then forwards to local MTA on 127.0.0.1:25.
- This relay pattern keeps Postfix configuration simple and isolates scanning.
Testing and Validation
- Send test messages with EICAR test string to verify virus detection.
- Use GTUBE test for SpamAssassin detection.
- Send messages with blocked attachment types and verify expected actions (reject/quarantine/strip).
- Monitor logs and message headers for MailScan stamps/tags showing processing path and rule triggers.
Maintenance and Lifecycle
- Update MailScan and dependent engines on a regular schedule; test patches in staging before production.
- Rotate and archive logs and quarantine stores; maintain retention policies that meet compliance.
- Regularly review rules, whitelists, and blacklists; keep training data current for Bayesian filters.
- Periodically conduct penetration testing and audit configurations.
Appendix: Quick Command Reference
- Start/stop services:
sudo systemctl start mailscan sudo systemctl stop mailscan sudo systemctl restart mailscan
- Check logs:
sudo tail -f /var/log/mailscan/mailscan.log sudo journalctl -u mailscan -f
- Update ClamAV signatures:
sudo freshclam
- Test antivirus:
- Send an email containing the EICAR string to verify detection.
Conclusion
A properly deployed MailScan system protects SMTP mailflows by filtering malware and spam while enforcing organizational policies. Focus on correct integration with your MTA, keeping antivirus and rule sets up to date, tuning performance for your message volume, and implementing redundancy for high availability. Regular testing and metrics-driven tuning will keep false positives low and throughput high.
If you want, I can provide a tailored configuration example for Postfix+MailScan on Ubuntu 22.04 with exact config file snippets and systemd units.
Leave a Reply