Overview
-------------------------------------------------
SimScan is a simplified scanner for qmail similar to qmail-scanner and qscand.
It uses clamav, trophie, and/or spamassassin.  It also supports attachment
blocking by extension.  Simscan is written entirely in C to ensure maximum
speed.  There are several options to allow simscan to scan per domain, and
reject spam mail.

Requirements:
-------------------------------------------------
1. ripmime
2. qmail with qmail-queue patch
3. clamav (optional)
4. spamassassin (optional)
5. trophie (or sophie) (both optional)

How it works
-------------------------------------------------
1) Simscan creates a temporary working directory. You can specify the base 
working directory with the --enable-workdir=/path. The default location
of this base is the  /var/qmail/simscan directory. The temporary working
directory under this bass directory is named as
"unix time in seconds" . "microseconds" . "process id".

The email is read into a temporary file named "msg.unixtime.micro.pid. 

2) ripmime is called to break this file into separate mime parts.

3) Optionally (--enable-attach) the mime message parts are checked against
the list of attachments to block.

Put the list of attachments in /var/qmail/control/ssattach or place the list
in the /var/qmail/control/simcontrol file for per-domain blocking.
This file gets read each time simscan is kicked off.
 
4) clamdscan is called to check all the files in the working directory.
The return code of clamdscan is checked to see if it found a virus.
If it did, simscan exits back to qmail-smtpd with a permenent error,
causing the mail to be bounced back to the sender.

5) Optionally (--enable-spam) spam assassin is called via spamc.
The email returned by spamc is checked X-Spam-Flag: header.  If this header
contains "YES", the spam is rejected.  You can reject instead on the calculated
hit count value by adding the --enable-spam-hits.  SimScan checks for both
"hits=" and "score=" so that both old and new versions of spamassassin are
supported.

6) Optionally (--enable-per-domain) enable per domain clamav and
spamassassin processing. You will need to edit /var/qmail/control/simcontrol
to define what domains get what scans.  Further details on editing this file
are below.

Then run /var/qmail/bin/simscanmk to build the cdb files that simscan
uses. The changes will take effect immediately.

7) Finally, if all of the above succeeds then the msg file is passed on to
qmail-queue. And the working directory and all the temporary files are deleted.

Sample qmail startup script:
-------------------------------------------------
rc.qmail is our sample qmail startup script. It shows you how you
can set the QMAILQUEUE environment variable to call simscan for
all incoming SMTP email.

Sample spamassassin startup script:
-------------------------------------------------
rc.spamd is our sample spamd startup script. It shows you working
options to start up the spamassassin deamon and log output via multilog. 
These options work with simscans default spamc_args.

Configuration Details
-------------------------------------------------
Following is a list of all of the configure options and defaults:

  --enable-user=<user>              Change the user for simscan.
                                    Default: simscan.
  --enable-clamav=y|n               Turn on clamav scanning. default yes.
  --enable-clamdscan=PATH           Full path to clamdscan program.
  --enable-custom-smtp-reject=y|n   Return smtp reject message with virus name.
  --enable-per-domain=y|n           Turn on per domain based checking.
  --enable-attach=y|n               Turn on attachment scanning. default no.
  --enable-spam=y|n                 Turn on spam scanning. default no.
  --enable-spam-passthru=y|n        Pass spam email thru or reject
                                    Default: disable (reject)
  --enable-spamc-user=y|n           Set user option to spamc.
  --enable-spam-hits=number         Reject spam above this hit level.
                                    Default 10.0
  --enable-spamc=PATH               Full path to spamc program.
  --enable-spamc-args=ARGS          Arguments to pass to spamc.
  --enable-qmaildir=DIR             Base qmail directory /var/qmail.
  --enable-workdir=DIR              Directory to unpack emails
                                    Default: /var/qmail/simscan
  --enable-qmail-queue=PATH         Full path to qmail-queue program.
  --enable-trophie-socket=PATH      Full path to the trophie socket.
  --enable-ripmime=PATH             Full path to ripmime program.
  --enable-received=y|n             Simscan should add a received line showing
                                    the version of all scanners that checked
                                    the message

  these are only needed when received=y and the corresponding scanners are 
  enabled as well:
    --enable-spamassassin-path=PATH Path to the spamassassin binary
    --enable-clamavdb-path=PATH     Directory where the clamav master.cvd and 
                                    daily.cvd files are saved
    --enable-sigtool-path=PATH      Path to the sigtool binary


--enable-user=<user>
   This option defines the user that simscan will run as.  By default, the
   user is 'simscan'.

--enable-clamav=y|n
   This option turns clamav scanning on or off.  When enabled, an incoming
   email will be rejected if a virus is detected in the email.

--enable-clamdscan=PATH
   This option defines the path to the clamdscan binary.  NOTE : This is the
   directory the binary is located in.  This option does nothing when clamav
   is not enabled.

--enable-custom-smtp-reject=y|n
   This option turns custom smtp reject messages on and off.  When enabled
   simscan will place the virus name in the reject message if a virus is
   detected.

--enable-per-domain=y|n
   This option turns per-domain scanning on and off.  Per domain scanning
   allows the administrator to explicitly state what scanning occurs for
   what domain.  In addition, attachment scanning can be enabled or disabled
   for each domain.  Details about how to set up per-domain scanning are
   below.

--enable-spam=y|n
   Ths option turns spam scanning on and off.  When enabled, simscan allows
   mail over a certain spam threshold to be rejected back to the sender.

--enable-spam-passthru=y|n
   This option turns spam passthru on and off.  When enabled, email
   identified as spam via the X-Spam-Status: header will be passed on to the
   user instead of rejected.  Note : Enabling spam-hits effectively disables
   this option.

--enable-spamc-user=y|n
   This option turns per-user spamassassin on or off.  When enabled, the email
   address of the first rcpt to user is sent to spamassassin.  This allows
   spamassassin to use customized rules and settings for that email.

--enable-spam-hits=number
   This option specifies the number of hits a spam must receive to be rejected
   by simscan.  This option defaults to 10 hits.
   
--enable-spamc=PATH
   This option specifies the path to the spamc binary.  Note : This is the
   directory where the spamc binary is located.
  
--enable-spamc-args=ARGS
   This option defines the arguments to pass to spamc.  Be sure to place quotes
   around the options you define.
   
--enable-qmaildir=DIR
   This option defines the location of qmail.
   
--enable-workdir=DIR
   This option defines the location of the working directory.  Note : The
   default directory is /var/qmail/simscan

--enable-qmail-queue=PATH
   This option defines the full path and name of the qmail-queue program.
   Incoming mail is passed to this program after being scanned by SimScan.
   
--enable-trophie-socket=PATH
   This option defines the path to the trophie socket.  Defining this option
   enables trophie scanning.

--enable-trophie-path=PATH
   Path to the trophie binary. Only needed to extract the Version for the 
   received line

--enable-ripmime=PATH
   This option defines the path to the ripmime program.  This program is used
   to rip apart emails into files.

--enable-received=y|n
   Simscan adds a Received line, showing the runtime, and a version string of
   the scanners that checked the message:
   Received: by simscan 1.0.6 ppid: 25399, pid: 25400, t: 4.7007s
         scanners: attach: 1.0.6 clamav: 0.80/m:27/d:556 trophie: 7.000-1011/218/74141 spam: 2.63


Attachment blocking option:
-------------------------------------------------
Attachments can be blocked. It is disabled by default.
If you use the per domain scanning as well, look in that section of the 
documentation on how to enable the checking.
With only the attachment blocker and NO per domain option it works like
this:

Put the list of attachments in /var/qmail/control/ssattach.
List each attachment on it's own line. For example:
.jpg
.mp3
.scr
.bat

Then configure with:
./configure --enable-attatch (with any other options you want)
make
make install-strip

SpamAssassin option:
-------------------------------------------------
Spamassassin can be called on each incoming email. If the email is
marked as spam the email is rejected with a permanent failure during
the smtp conversation.

./configure --enable-spam (with any other options you want)
make
make install-strip

Look at the rc.spamd sample startup script for spamd startup options
that are known to work on our test machines.

Trophie/Sophie option
-------------------------------------------------
./configure --enable-trophie-socket=/path/to/the/socket \
  --enable-trophie-path=/path/to/trophie/binary
Enables the trophie virus scanner. As sophie uses the same interface 
it may work as well, but is not tested!


Enable Per Domain processing:
-------------------------------------------------
./configure --enable-per-domain (with any other options you want)
make 
make install-strip

This will enable the per domain processing for both clamav and
spamassassin (if spamassassin is enabled too).

Edit /var/qmail/control/simcontrol text file
You can enable/disable clam/spam/trophie/attachments per domain 
and per user and set a default for the whole machine. 

Here is an example: 
postmaster@example.com:clam=yes,spam=no,attach=.txt:.com
example.com:clam=no,spam=yes,attach=.mp3
:clam=yes,spam=yes,trophie=yes,spam_hits=20.1

The third line sets the default for the whole machine to
enable clam,trophie, spam scanning, and sets the reject level for
spam hits to 20.1.

Without any matches, no scans will be done.

The second line sets clam off and spam on for the example.com domain and
disallows .mp3 files for the attachment scanner

The first line sets clam on and spam off for postmaster@example.com and
.txt and .com files for the attachment names.

The order of precedence is:
email address (over rides all)
domain (over rides default)
default (only used if not over ridden by domain or email address.

First the sender address will be looked up and then the recipients.

Then run /var/qmail/bin/simscanmk to build the simcontrol.cdb file.
You can rebuild this files at any time. The simscanmk program can safely
update the cdb files while the system is running.

Qmail extensions are handled like this: the address is breaken into their
parts and are looked up: test-list-owner@test.ch looks up:

test@test.ch
test-list@test.ch
test-list-owner@test.ch


Security
-------------------------------------------------
The simscan program is restricted to running setuid simscan
to protect the rest of the system. It does all of it's work
in the /var/qmail/simscan directory (default location).

The simscan program runs setuid simscan. It does all of it's work
in the /var/qmail/simscan directory which is owned by simscan.

Permissions and ClamAntiVirus
-------------------------------------------------
To get ClamAV to play nicely with simscan's permissions you have two options:
1) run clamd as root
2) Add clamav to simscan's group. Then clamav will have access
to the working directory and it's files. On Linux like systems:
usermod -G simscan clamav

How to chain additional scanning programs with simscan.
-------------------------------------------------
When simscan finishes it expects to call a program that reads
the file descriptors like qmail-queue does. You can configure
simscan to call a different program. By default the configure
script picks up the path to your qmail-queue program, which
is normally /var/qmail/bin/qmail-queue. Use this configure option: 
--enable-qmail-queue=PATH   where PATH is full path to qmail-queue program.
 
I do not know why you would want to have another scanning process happen,
but you can sure configure it before compiling.
 

Temporary File Management
-------------------------------------------------
Simscan uses unique file names for the message, to/from headers and the
optional spamassassin output. The files are created in the unique simscan
work directory for this process. The files are unlinked along with the
temporary work directory just before we hand/execl all the information to
qmail-queue or on error exits.

         data          file name
       ____________   _______________
       message fd 0 = msg.X.Y.Z          where X = unix seconds
       to/from fd 1 = addr.X.Y.Z               Y = microseconds
       spamassassin = spamc.msg.X.Y.Z          Z = simscan process id

       fd 0 and fd 1 normally come from the qmail smtp daemon.

How to Disable/Enable simscan for smtp connections by IP ranges.
-------------------------------------------------
Use the standard tcp.smtp text file to set or not set the QMAILQUEUE
environment variable per IP ranges. qmails smtp server is normally
run via tcpserver with the -x option to the constant database file
tcp.smtp.cdb. 

Example:
Description: 
Turn simscan off for our machines loopback address (127.*.*.*). 
Disable it for hypothetical local linux users on the 192.168.1.* lan. 
Enable it for hypothetical local windows users on the 192.168.1.* lan. 
Finally, we enable simscan(clamav/spamassassin) for all those untrusted 
internet email senders.

Example tcp.smtp file contents:
127.:allow,RELAYCLIENT=""
192.168.1.:allow,RELAYCLIENT=""
192.168.2.:allow,RELAYCLIENT="",QMAILQUEUE="/var/qmail/bin/simscan"
:allow,QMAILQUEUE="/var/qmail/bin/simscan"

This tcp.smtp file then needs to be compiled into the tcp.smtp.cdb file
using your systems method of generating it. If you only need the rules
in the tcp.smtp file you can compile it with this command: 

1. cd /to/directory/where/your/tcp.smtp.cdb file lives
2. /usr/local/bin/tcprules tcp.smtp.cdb tempfile < tcp.smtp

Once compiled, the rules take effect immediately. Actually it
takes effect on every new smtp connection.

