diff -urN exim-4.30-orig/OS/Makefile-Base exim-4.30/OS/Makefile-Base --- exim-4.30-orig/OS/Makefile-Base Mon Dec 1 11:15:41 2003 +++ exim-4.30/OS/Makefile-Base Wed Jan 7 15:56:48 2004 @@ -247,14 +247,14 @@ # Targets for final binaries; the main one has a build number which is # updated each time. We don't bother with that for the auxiliaries. -OBJ_EXIM = acl.o child.o crypt16.o daemon.o dbfn.o debug.o deliver.o \ +OBJ_EXIM = acl.o child.o crypt16.o daemon.o dbfn.o debug.o deliver.o demime.o \ directory.o dns.o drtables.o enq.o exim.o expand.o filter.o \ filtertest.o globals.o \ - header.o host.o ip.o log.o lss.o match.o moan.o \ + header.o host.o ip.o log.o lss.o malware.o match.o mime.o moan.o \ os.o parse.o queue.o \ - rda.o readconf.o receive.o retry.o rewrite.o rfc2047.o \ - route.o search.o sieve.o smtp_in.o smtp_out.o spool_in.o spool_out.o \ - store.o string.o tls.o tod.o transport.o tree.o verify.o \ + rda.o readconf.o receive.o regex.o retry.o rewrite.o rfc2047.o \ + route.o search.o sieve.o smtp_in.o smtp_out.o spam.o spool_in.o spool_mbox.o spool_out.o \ + store.o string.o tls.o tnef.o tod.o transport.o tree.o verify.o \ local_scan.o $(EXIM_PERL) exim: pcre/libpcre.a lookups/lookups.a auths/auths.a \ @@ -466,6 +466,7 @@ dbfn.o: $(HDRS) dbfn.c debug.o: $(HDRS) debug.c deliver.o: $(HDRS) deliver.c +demime.o: $(HDRS) demime.c directory.o: $(HDRS) directory.c dns.o: $(HDRS) dns.c enq.o: $(HDRS) enq.c @@ -479,7 +480,9 @@ ip.o: $(HDRS) ip.c log.o: $(HDRS) log.c lss.o: $(HDRS) lss.c +malware.o: $(HDRS) malware.c match.o: $(HDRS) match.c +mime.o: $(HDRS) mime.c moan.o: $(HDRS) moan.c os.o: $(HDRS) os.c parse.o: $(HDRS) parse.c @@ -487,6 +490,7 @@ rda.o: $(HDRS) rda.c readconf.o: $(HDRS) readconf.c receive.o: $(HDRS) receive.c +regex.o: $(HDRS) regex.c retry.o: $(HDRS) retry.c rewrite.o: $(HDRS) rewrite.c rfc2047.o: $(HDRS) rfc2047.c @@ -495,11 +499,14 @@ sieve.o: $(HDRS) sieve.c smtp_in.o: $(HDRS) smtp_in.c smtp_out.o: $(HDRS) smtp_out.c +spam.o: $(HDRS) spam.c spool_in.o: $(HDRS) spool_in.c +spool_mbox.o: $(HDRS) spool_mbox.c spool_out.o: $(HDRS) spool_out.c store.o: $(HDRS) store.c string.o: $(HDRS) string.c tls.o: $(HDRS) tls.c tls-gnu.c tls-openssl.c +tnef.o: $(HDRS) tnef.c tod.o: $(HDRS) tod.c transport.o: $(HDRS) transport.c tree.o: $(HDRS) tree.c diff -urN exim-4.30-orig/README.EXISCAN exim-4.30/README.EXISCAN --- exim-4.30-orig/README.EXISCAN Thu Jan 1 01:00:00 1970 +++ exim-4.30/README.EXISCAN Tue Dec 2 12:15:53 2003 @@ -0,0 +1 @@ +Please refer to doc/exiscan-acl-spec.txt diff -urN exim-4.30-orig/doc/exiscan-acl-examples.txt exim-4.30/doc/exiscan-acl-examples.txt --- exim-4.30-orig/doc/exiscan-acl-examples.txt Thu Jan 1 01:00:00 1970 +++ exim-4.30/doc/exiscan-acl-examples.txt Tue Dec 2 12:15:53 2003 @@ -0,0 +1,440 @@ +-------------------------------------------------------------- +exiscan-acl example configurations / FAQ +-------------------------------------------------------------- + +Author: Tom Kistner + +The exiscan website is at http://duncanthrax.net/exiscan/. You +will find the latest patch versions, as well as links to the +mailing list and its archives there. + +This document shows some example configuration snippets: + +1. Basic sitewide virus and spam filtering by rejecting + matching messages after DATA. +2. Adding a cryptographic "checks done" header that will + prevent re-scanning when the message re-visits one of your + mail servers, and the body size did not change. +3. Marking spam-suspicious messages with extra headers and a + tag in the subject. +4. Having more than one spam threshold to act on. +5. Redirecting matching messages to special accounts while + preserving envelope recipient information. +6. A multi-profile configuration for sites where different + "customers" (or users) have different content scanning + preferences. + +These examples serve as a guideline and should give you some +pointers that can help you to create your own configuration. +Please do not copy these examples verbatim. You really need to +know what you are doing. The content scanning topic is really +complex and you can screw up your mail server easily if you do +not get it "right". + +I recommend to read the exiscan documentation on the above +mentioned website before trying to make sense of the following +examples. + +Each example shows part of a DATA ACL definition, unless +otherwise noted. + +-------------------------------------------------------------- +1. Basic setup for simple site-wide filtering +-------------------------------------------------------------- +The following example only shows the most basic use of the +exiscan content filtering features. You should see it as a +base that you can build on. However, it may be all you need +for smaller systems with only a few users. + +/* ----------------- +# Do not scan messages submitted from our own hosts +# and locally submitted messages. Since the DATA ACL +# is not called for messages not submitted via SMTP +# protocols, we do not need to check for an empty +# host field. +accept hosts = 127.0.0.1:+relay_from_hosts + +# Unpack MIME containers and reject file extensions +# used by worms. Note that the extension list may be +# incomplete. +deny message = $found_extension files are not accepted here + demime = com:vbs:bat:pif:scr + +# Reject messages that have serious MIME errors. +# This calls the demime condition again, but it +# will return cached results. +deny message = Serious MIME defect detected ($demime_reason) + demime = * + condition = ${if >{$demime_errorlevel}{2}{1}{0}} + +# Reject messages containing malware. +deny message = This message contains malware ($malware_name) + malware = * + +# Reject spam messages. Remember to tweak your +# site-wide SA profile. Do not spam-scan messages +# larger than eighty kilobytes. +deny message = Classified as spam (score $spam_score) + condition = ${if <{$message_size}{80k}{1}{0}} + spam = nobody + +# Finally accept all other messages that have +# made it to this point +accept +------------------ */ + + + +-------------------------------------------------------------- +2. Adding a cryptographic "scanning done" header +-------------------------------------------------------------- + +If you have a mail setup where the same message may pass your +server twice (redirects from other servers), or you have +multiple mail servers, you may want to make sure that each +message is only checked once, to save processing time. Here is +how to do it: + +At the very beginning of your DATA ACL, put this: + +/* ----------------- +# Check our crytographic header. If it matches, accept +# the message. +accept condition = ${if eq {${hmac{md5}\ + {mysecret}\ + {$body_linecount}}}\ + {$h_X-Scan-Signature:} {1}{0}} +------------------ */ + +At the end, just before the final "accept" verb, put this: + +/* ----------------- +# Add the cryptographic header. +warn message = X-Scan-Signature: ${hmac{md5}{mysecret}\ + {$body_linecount}} +------------------ */ + +Notice the two "mysecret" strings? Replace them with your own +secret, and don't tell anyone :) The hash also includes the +number of lines in the message body, to protect against +message "modifications". + + +-------------------------------------------------------------- +3. Marking Spam messages with extra headers and subject tag +-------------------------------------------------------------- + +Since the false positive rate with spam scanning is high +compared to virus scanning, it is wise to implement a scheme +with two thresholds, where you reject messages with high +scores and just mark messages with lower scores. End users can +then set up filters in their Mail User Agents (MUAs). Since +many MUAs can not filter on custom headers, it can be +necessary to put a "spam tag" in the subject line. Since it is +not (yet) possible to remove headers in Exims DATA ACL, we +must do this in a system filter. Please see the Exim docs on +how to set up a system filter. + +The following example will unconditionally put two spam +information headers in each message, if it is smaller than +eighty kilobytes: + +/* ----------------- +# Always put X-Spam-Score header in the message. +# It looks like this: +# X-Spam-Score: 6.6 (++++++) +# When a MUA cannot match numbers, it can match for an +# equivalent number of '+' signs. +# The 'true' makes sure that the header is always put +# in, no matter what the score. +warn message = X-Spam-Score: $spam_score ($spam_bar) + condition = ${if <{$message_size}{80k}{1}{0}} + spam = nobody:true + +# Always put X-Spam-Report header in the message. +# This is a multiline header that informs the user +# which tests a message has "hit", and how much a +# test has contributed to the score. +warn message = X-Spam-Report: $spam_report + condition = ${if <{$message_size}{80k}{1}{0}} + spam = nobody:true +------------------ */ + +For the subject tag, we prepare a new subject header in the +ACL, then swap it with the original Subject in the system +filter. + +In the DATA ACL, put this: +/* ----------------- +warn message = X-New-Subject: *SPAM* $h_subject: + spam = nobody +------------------ */ + +In the system filter, put this: +/* ----------------- +if "${if def:header_X-New-Subject: {there}}" is there +then + headers remove subject + headers add "Subject: $h_X-New-Subject:" + headers remove X-New-Subject +endif +------------------ */ + + +-------------------------------------------------------------- +4. Defining multiple spam thresholds with different actions +-------------------------------------------------------------- +If you want to mark messages if they exceed your threshold, +but also have a higher "cutoff" threshold where you reject +messages, use the example above, plus this part: + +/* ----------------- +deny message = Spam score too high ($spam_score) + condition = ${if <{$message_size}{80k}{1}{0}} + spam = nobody:true + condition = ${if >{$spam_score_int}{100}{1}{0}} +------------------ */ + +The last condition is only true if the spam score exceeds 10.0 +points (Keep in mind that $spam_score_int is the messages +score multiplied by ten). + + + +-------------------------------------------------------------- +5. Redirect infected or spam messages to special accounts +-------------------------------------------------------------- +Sometimes it is desirable not to reject messages, but to stop +them for inspection, and then decide wether to delete, bounce +or pass them. + +There are multiple ways to achieve this. The simplest way is +to freeze suspicious messages, and then thaw or bounce them +after a review. Here is a simple example that will freeze spam +suspicious messages when they exceed the SA threshold: + +/* ----------------- +warn log_message = frozen by spam scanner, score $spam_score + spam = nobody + control = freeze +------------------ */ + +Another way is to redirect suspicious messages to special +postmaster accounts, where they can be reviewed. This involves +setting up a router for these special accounts that acts on a +header set in the DATA ACL. + +This is the DATA ACL entry: + +/* ----------------- +warn message = X-Redirect-To: spambox@mycompany.com + spam = nobody +------------------ */ + +This puts the target address in a special header, which can in +turn be read with this router: + +/* ----------------- +scan_redirect: + driver = redirect + condition = ${if def:h_X-Redirect-To: {1}{0}} + headers_add = X-Original-Recipient: $local_part@$domain + data = $h_X-Redirect-To: + headers_remove = X-Redirect-To + redirect_router = my_second_router +------------------ */ + +This router should probably be your very first one, and you +need to edit the last line (redirect_router = ) to replace +"my_second_router" with the name of your original first +router. Note that the original message recipient is saved in +the "X-Original-Recipient" header, and the X-Redirect-To +header line is removed. + + +-------------------------------------------------------------- +6. Having multiple content scanning profiles for several + users or domains. +-------------------------------------------------------------- +This is one of the most often asked questions, and it also has +the most complicated answer. To understand the difficulties, +you should first remember that the exiscan facilities are run +in the DATA ACL. This ACL is called ONCE per message, after +the sending server has transmitted the end-of-data marker. +This gives us the very cool possibility to reject unwanted +messages with a 5xx error code in response. The big drawback +is that a message can have multiple recipients, and you can +only reject or accept a message for ALL recipients, not +individual ones. + +I will first sum up the possible solutions to this dilemma: + + a. Make sure that each incoming message can have only one + envelope recipient. This is brutal, but effective and + reliably solves the problem on your end. :) Drawback: + Incoming mail to multiple recipients is slowed down. The + exact time depends on the retry strategies of the sending + hosts. + + b. Offer a limited number of "profiles" that your customers + can subscribe to. Then, similar to a.), only accept + recipients with the same profile in a single "batch", and + defer the others. This does improve on the drawback of + a.) a bit. + + c. Do scanning as usual, but never reject messages in the + DATA ACL. Instead put appropriate information in extra + headers and query those in routers or transports later. + Drawback: You'll have to send bounces yourself, and your + queue will fill up with frozen bounces. Advantage: clean + solution, protocol-wise. + +As you see, you can't have your cake and eat it too. Now lets +get into the details of each possible solution. + +a.) Making sure each incoming message that will be scanned + only has one recipient. + + To use this scheme, you must make sure that you do not use + it on your +relay_from_hosts and authenticated senders. + Both of these may be MUAs who cannot cope with such a + thing. + + Here is a RCPT ACL that implements the behaviour + (shortened, do not copy 1:1!): + + /* ------------ + acl_check_rcpt: + + # accept local, relay-allowed + # and authenticated sources + + accept hosts = : + deny local_parts = ^.*[@%!/|] + accept hosts = 127.0.0.1:+relay_from_hosts + accept authenticated = * + + # the following treat non-local, + # non-authenticated sources + + defer message = only one recipient at a time + condition = ${if def:acl_m0 {1}{0}} + + # [ .. ] + # put RBLs etc. here + # [ .. ] + + accept domains = +local_domains + endpass + message = unknown user + verify = recipient + set acl_m0 = $local_part@$domain + + accept domains = +relay_to_domains + endpass + message = unrouteable address + verify = recipient + set acl_m0 = $domain + + deny message = relay not permitted + ------------ */ + + The lines which contain acl_m0 are the important ones. The + $acl_m0 variable gets set when a remote server + successfully sends one RCPT. Subsequent RCPT commands are + deferred if this variable is set. The $acl_m0 variable now + contains the single recipient domain, which you can use in + the DATA ACL to determine the scanning profile. + + This scheme is only recommended for small servers with a + low number of possible recipients, where recipients do not + belong to the same organization. An example would be a + multiuser shell server. + + +b.) Having several scanning profiles that "customers" can + choose from. + + Suppose you want to offer three profiles. Lets call them + "reject-aggressive", "reject-conservative", and "warn + -only". Customers can select one of the profiles for each + of their domains. So you end up with a mapping like this: + + domain-a.com: reject-aggressive + domain-b.org: warn-only + domain-c.net: reject-aggressive + domain-d.com: reject-conservative + [ .. ] + + Suppose you put that in a file called /etc/exim/scanprefs + + Now we make a scheme similar to a.), but we do allow more + than one recipient if they have the same scanning profile + than the first recipient. + + Here is a RCPT ACL that implements the behaviour + (shortened, do not copy 1:1!): + + /* ------------ + acl_check_rcpt: + + # accept local, relay-allowed and authenticated sources + + accept hosts = : + deny local_parts = ^.*[@%!/|] + accept hosts = 127.0.0.1:+relay_from_hosts + accept authenticated = * + + # the following treat non-local, non-authenticated sources + + defer message = try this address in the next batch + condition = ${if eq {${acl_m0}}\ + {${lookup{$domain}\ + lsearch{/etc/exim/scanprefs}}}\ + {0}{1}} + + # [ .. ] + # put RBLs etc. here + # [ .. ] + + accept domains = +local_domains + endpass + message = unknown user + verify = recipient + set acl_m0 = $local_part@$domain + + accept domains = +relay_to_domains + endpass + message = unrouteable address + verify = recipient + set acl_m0 = ${lookup{$domain}\ + lsearch{/etc/exim/scanprefs}} + + deny message = relay not permitted + ------------ */ + + Now a recipient address get deferred if its scan profile + does not match the current batch profile. The $acl_m0 + variable contains the name of the profile, that can be + used for processing in the DATA ACL. + + This scheme works pretty well if you keep the number of + possible profiles low, since that will prevent + fragmentation of RCPT blocks. + + +c.) Classic content scanning without the possibility of + rejects after DATA. + + This emulates the "classic" content scanning in routers + and transports. The difference is that we still do the + scan in the DATA ACL, but put the outcome of each facility + in message headers, that can the be evaluated in special + routers, individually for each recipient. + + A special approach can be taken for spam scanning, since + the $spam_score_int variable is also available in routers + and transports (it gets written to the spool files), so + you do not need to put that information in a header, but + rather act on $spam_score_int directly. + diff -urN exim-4.30-orig/doc/exiscan-acl-spec.txt exim-4.30/doc/exiscan-acl-spec.txt --- exim-4.30-orig/doc/exiscan-acl-spec.txt Thu Jan 1 01:00:00 1970 +++ exim-4.30/doc/exiscan-acl-spec.txt Mon Jan 19 21:24:51 2004 @@ -0,0 +1,627 @@ +-------------------------------------------------------------- +The exiscan-acl patch for exim4 - Documentation +-------------------------------------------------------------- +(c) Tom Kistner 2003-???? +License: GPL + +The exiscan-acl patch adds content scanning to the exim4 ACL +system. It supports the following scanning features: + + - MIME ACL that is called for all MIME parts in + incoming MIME messages. + - Antivirus using 3rd party scanners. + - Antispam using SpamAssassin. + - Regular expression match against headers, bodies, raw + MIME parts and decoded MIME parts. + +These features are hooked into exim by extending exim's ACL +system. The patch adds expansion variables and ACL conditions. +These conditions are designed to be used in the acl_smtp_data +ACL. It is run when the sending host has completed the DATA +phase and is waiting for our final response to his end-of-data +marker. This allows us to reject messages containing +unwanted content at that stage. + +The default exim configure file contains commented +configuration examples for some features of exiscan-acl. + + +0. Overall concept / Overview +-------------------------------------------------------------- + +The exiscan-acl patch extends Exims with mechanisms to +deal with the message body content. Most of these additions +affect the ACL system. The exiscan patch adds + +- A new ACL, called 'acl_smtp_mime' (Please see detailed + chapter on this one below). +- ACL conditions and modifiers + o malware (attach 3rd party virus/malware scanner) + o spam (attach SpamAssassin) + o regex (match regex against message, linewise) + o decode (decode MIME part to disk) + o mime_regex (match regex against decoded MIME part) + o control = fakereject (reject but really accept a message) +- expansion variables + (see chapters below for names and explanations) +- configuration options in section 1 of Exim's configure file. + o av_scanner (type and options of the AV scanner) + o spamd_address (network address / socket of spamd daemon). + +All facilites work on a MBOX copy of the message that is +temporarily spooled up in a file called: + + /scan//.eml + +The .eml extension is a friendly hint to virus scanners that +they can expect an MBOX-like structure inside that file. The +file is only spooled up once, when the first exiscan facility +is called. Subsequent calls to exiscan conditions will just +open the file again. The directory is recursively removed +when the acl_smtp_data has finished running. When the MIME +ACL decodes files, they will be put into that same folder by +default. + + +1. The acl_smtp_mime MIME ACL +-------------------------------------------------------------- + +Note: if you are not familiar with exims ACL system, please go +read the documentation on it, otherwise this chapter will not +make much sense to you. + +Here are the facts on acl_smtp_mime: + + - It is called once for each MIME part of a message, + including multipart types, in the sequence of their + position in the message. + + - It is called just before the acl_smtp_data ACL. They share + a result code (the one assed to the remote system after + DATA). When a call to acl_smtp_mime does not yield + "accept", ACL processing is aborted and the respective + result code is sent to the remote mailer. This means that + the acl_smtp_data is NOT called any more. + + - It is ONLY called if the message has a MIME-Version header. + + - MIME parts will NOT be dumped to disk by default, you have + to call the "decode" condition to do that (see further + below). + + - For RFC822 attachments (these are messages attached to + messages, with a content-type of 'message/rfc822'), + the ACL is called again in the same manner as + for the "primary" message, only that the $mime_is_rfc822 + expansion variable is set (see below). These messages + are always decoded to disk before being checked, but + the files are unlinked once the check is done. + +To activate acl_smtp_mime, you need to add assign it the name +of an ACL entry in section 1 of the config file, and then +write that ACL in the ACL section, like: + + /* --------------- + + # -- section 1 ---- + [ ... ] + acl_smtp_mime = my_mime_acl + [ ... ] + + # -- acl section ---- + begin acl + + [ ... ] + + my_mime_acl: + + < ACL logic > + + [ ... ] + + ---------------- */ + +The following list describes all expansion variables that are +available in the MIME ACL: + + $mime_content_type + ------------------ + A very important variable. If the MIME part has a "Content + -Type:" header, this variable will contain its value, + lowercased, and WITHOUT any options (like "name" or + "charset", see below for these). Here are some examples of + popular MIME types, as they may appear in this variable: + + text/plain + text/html + application/octet-stream + image/jpeg + audio/midi + + If the MIME part has no "Content-Type:" header, this + variable is the empty string. + + + $mime_filename + -------------- + Another important variable, possibly the most important one. + It contains a proposed filename for an attachment, if one + was found in either the "Content-Type:" or "Content + -Disposition" headers. The filename will be RFC2047 + decoded, however NO additional sanity checks are done. See + instructions on "decode" further below. If no filename was + found, this variable is the empty string. + + + $mime_charset + ------------- + Contains the charset identifier, if one was found in the + "Content-Type:" header. Examples for charset identifiers are + + us-ascii + gb2312 (Chinese) + iso-8859-1 + + Please note that this value will NOT be normalized, so you + should do matches case-insensitively. + + + $mime_boundary + -------------- + If the current part is a multipart (see $mime_is_multipart) + below, it SHOULD have a boundary string. It is stored in + this variable. If the current part has no boundary parameter + in the "Content-Type:" header, this variable contains the + empty string. + + + $mime_content_disposition + ------------------------- + Contains the normalized content of the "Content + -Disposition:" header. You can expect strings like + "attachment" or "inline" here. + + + $mime_content_transfer_encoding + ------------------------------- + Contains the normalized content of the "Content + -transfer-encoding:" header. This is a symbolic name for + an encoding type. Typical values are "base64" and "quoted + -printable". + + + $mime_content_id + ---------------- + Contains the normalized content of the "Content + -ID:" header. This is a unique ID that can be used to + reference a part from another part. + + + $mime_content_description + ------------------------- + Contains the normalized content of the "Content + -Description:" header. It can contain a human-readable + description of the parts content. Some implementations will + repeat the filename for attachments here, but they are + usually only used for display purposes. + + + $mime_part_count + ---------------- + This is a counter that is raised for each processed MIME + part. It starts at zero for the very first part (which is + usually a multipart). The counter is per-message, so it is + reset when processing RFC822 attachments (see + $mime_is_rfc822). The counter stays set after acl_smtp_mime + is complete, so you can use it in the DATA ACL to determine + the number of MIME parts of a message. For non-MIME + messages, this variable will contain the value -1. + + + $mime_is_multipart + ------------------ + A "helper" flag that is true (1) when the current + part has the main type "multipart", for example + "multipart/alternative" or "multipart/mixed". Since + multipart entities only serve as containers for other parts, + you may not want to carry out specific actions on them. + + + $mime_is_rfc822 + --------------- + This flag is true (1) if the current part is NOT a part of + the checked message itself, but part of an attached message. + Attached message decoding is fully recursive. + + + $mime_decoded_filename + ---------------------- + This variable is only set after the "decode" condition (see + below) has been successfully run. It contains the full path + and file name of the file containing the decoded data. + + +The expansion variables only reflect the content of the MIME +headers for each part. To actually decode the part to disk, +you can use the "decode" condition. The general syntax is + +decode = [//] + +The right hand side is expanded before use. After expansion, +the value can + + - be '0' or 'false', in which case no decoding is done. + - be the string 'default'. In that case, the file will be + put in the temporary "default" directory + /scan// + with a sequential file name, consisting of the message id + and a sequence number. The full path and name is available + in $mime_decoded_filename after decoding. + - start with a slash. If the full name is an existing + directory, it will be used as a replacement for the + "default" directory. The filename will then also be + sequentially assigned. If the name does not exist, it will + be used as the full path and file name. + - not start with a slash. It will then be used as the + filename, and the default path will be used. + +You can easily decode a file with its original, proposed +filename using "decode = $mime_filename". However, you should +keep in mind that $mime_filename might contain anything. If +you place files outside of the default path, they will not be +automatically unlinked. + +The MIME ACL also supports the regex= and mime_regex= +conditions. You can use those to match regular expressions +against raw and decoded MIME parts, respectively. Read the +next section for more information on these conditions. + + + +2. Match message or MIME parts against regular expressions +-------------------------------------------------------------- + +The "regex" condition takes one or more regular expressions as +arguments and matches them against the full message (when +called in the DATA ACL) or a raw MIME part (when called in the +MIME ACL). The "regex" condition matches linewise, with a +maximum line length of 32k characters. That means you can't +have multiline matches with the "regex" condition. + +The "mime_regex" can only be called in the MIME ACL. It +matches up to 32k of decoded content (the whole content at +once, not linewise). If the part has not been decoded with the +"decode" condition earlier in the ACL, it is decoded +automatically when "mime_regex" is executed (using default +path and filename values). If the decoded data is larger +than 32k, only the first 32k characters will be +matched. + +The regular expressions are passed as a colon-separated list. +To include a literal colon, you must double it. Since the +whole right-hand side string is expanded before being used, +you must also escape dollar ($) signs with backslashes. + +Here is a simple example: + +/* ---------------------- +deny message = contains blacklisted regex ($regex_match_string) + regex = [Mm]ortgage : URGENT BUSINESS PROPOSAL +----------------------- */ + +The conditions returns true if one of the regular +expressions has matched. The $regex_match_string expansion +variable is then set up and contains the matching regular +expression. + +Warning: With large messages, these conditions can be fairly +CPU-intensive. + + + +3. Antispam measures with SpamAssassin +-------------------------------------------------------------- + +The "spam" ACL condition calls SpamAssassin's "spamd" daemon +to get a spam-score and a report for the message. You must +first install SpamAssassin. You can get it +at http://www.spamassassin.org, or, if you have a working +Perl installation, you can use CPAN by calling + +perl -MCPAN -e 'install Mail::SpamAssassin' + +SpamAssassin has its own set of configuration files. Please +review its documentation to see how you can tweak it. The +default installation should work nicely, however. + +After having installed and configured SpamAssassin, start the +"spamd" daemon. By default, it listens on 127.0.0.1, TCP port +783. If you use another host or port for spamd, you must set +the spamd_address option in Section 1 of the exim +configuration as follows (example): + +spamd_address = 127.0.0.1 783 + +As of version 2.60, spamd also supports communication over UNIX +sockets. If you want to use these, supply spamd_address with +an absolute file name instead of a address/port pair, like: + +spamd_address = /var/run/spamd_socket + +If you use the above mentioned default, you do NOT need to set +this option. + +To use the antispam facility, put the "spam" condition in a +DATA ACL block. Here is a very simple example: + +/* --------------- +deny message = This message was classified as SPAM + spam = joe +---------------- */ + +On the right-hand side of the spam condition, you can put the +username that SpamAssassin should scan for. That allows you to +use per-domain or per-user antispam profiles. The right-hand +side is expanded before being used, so you can put lookups or +conditions there. When the right-hand side evaluates to "0" or +"false", no scanning will be done and the condition will fail +immediately. + +If you do not want to scan for a particular user, but rather +use the SpamAssassin system-wide default profile, you can scan +for an unknown user, or simply use "nobody". + +The "spam" condition will return true if the threshold +specified in the user's SpamAssassin profile has been matched +or exceeded. If you want to use the spam condition for its +side effects (see the variables below), you can make it always +return "true" by appending ":true" to the username. + +When the condition is run, it sets up the following expansion +variables: + + $spam_score The spam score of the message, for example + "3.4" or "30.5". This is useful for + inclusion in log or reject messages. + + $spam_score_int The spam score of the message, multiplied + by ten, as an integer value. For example + "34" or "305". This is useful for numeric + comparisons in conditions. See further + below for a more complicated example. This + variable is special, since it is written + to the spool file, so it can be used + during the whole life of the message on + your exim system, even in routers + or transports. + + $spam_bar A string consisting of a number of '+' or + '-' characters, representing the + spam_score value. A spam score of "4.4" + would have a spam_bar of '++++'. This is + useful for inclusion in warning headers, + since MUAs can match on such strings. + + $spam_report A multiline text table, containing the + full SpamAssassin report for the message. + Useful for inclusion in headers or reject + messages. + +The spam condition caches its results. If you call it again +with the same user name, it will not really scan again, but +rather return the same values as before. + +Finally, here is a commented example on how to use the spam +condition: + +/* ---------------- +# put headers in all messages (no matter if spam or not) +warn message = X-Spam-Score: $spam_score ($spam_bar) + spam = nobody:true +warn message = X-Spam-Report: $spam_report + spam = nobody:true + +# add second subject line with *SPAM* marker when message +# is over threshold +warn message = Subject: *SPAM* $h_Subject + spam = nobody + +# reject spam at high scores (> 12) +deny message = This message scored $spam_score spam points. + spam = nobody:true + condition = ${if >{$spam_score_int}{120}{1}{0}} +----------------- */ + + + +4. The "malware" facility + Scan messages for viruses using an external virus scanner +-------------------------------------------------------------- + +This facility lets you connect virus scanner software to exim. +It supports a "generic" interface to scanners called via the +shell, and specialized interfaces for "daemon" type virus +scanners, who are resident in memory and thus are much faster. + +To use this facility, you MUST set the "av_scanner" option in +section 1 of the exim config file. It specifies the scanner +type to use, and any additional options it needs to run. The +basic syntax is as follows: + + av_scanner = :::[...] + +The following scanner-types are supported in this release: + + sophie Sophie is a daemon that uses Sophos' libsavi + library to scan for viruses. You can get Sophie + at http://www.vanja.com/tools/sophie/. The only + option for this scanner type is the path to the + UNIX socket that Sophie uses for client + communication. The default path is + /var/run/sophie, so if you are using this, you + can omit the option. Example: + + av_scanner = sophie:/tmp/sophie + + + kavdaemon Kapersky's kavdaemon is a daemon-type scanner. + You can get a trial version at + http://www.kapersky.com. This scanner type takes + one option, which is the path to the daemon's + UNIX socket. The default is "/var/run/AvpCtl". + Example: + + av_scanner = kavdaemon:/opt/AVP/AvpCtl + + + clamd Another daemon type scanner, this one is GPL and + free. Get it at http://clamav.elektrapro.com/. + Clamd does not seem to unpack MIME containers, + so it is recommended to use the demime facility + with it. It takes one option: either the path + and name of a UNIX socket file, or a + hostname/port pair, separated by space. If + unset, the default is "/tmp/clamd". Example: + + av_scanner = clamd:192.168.2.100 1234 + or + av_scanner = clamd:/opt/clamd/socket + + + drweb This one is for the DrWeb (http://www.sald.com/) + daemon. It takes one argument, either a full + path to a UNIX socket, or an IP address and port + separated by whitespace. If you omit the + argument, the default + + /usr/local/drweb/run/drwebd.sock + + is used. Example: + + av_scanner = drweb:192.168.2.20 31337 + or + av_scanner = drweb:/var/run/drwebd.sock + + Thanks to Alex Miller for + contributing the code for this scanner. + + + mksd Yet another daemon type scanner, aimed mainly at + Polish users, though some parts of documentation + are now avaliable in English. You can get it at + http://linux.mks.com.pl/. The only option for + this scanner type is the maximum number of + processes used simultaneously to scan the + attachments, provided that the demime facility + is employed and also mksd has been run with + at least the same number of child processes. + You can safely omit this option, the default + value is 1. Example: + + av_scanner = mksd:2 + + + cmdline This is the keyword for the generic command line + scanner interface. It can be used to attach + virus scanners that are invoked on the shell. + This scanner type takes 3 mantadory options: + + - full path and name of the scanner binary, with + all command line options and a placeholder + (%s) for the directory to scan. + + - A regular expression to match against the + STDOUT and STDERR output of the virus scanner. + If the expression matches, a virus was found. + You must make absolutely sure that this + expression only matches on "virus found". This + is called the "trigger" expression. + + - Another regular expression, containing exactly + ONE pair of braces, to match the name of the + virus found in the scanners output. This is + called the "name" expression. + + Example: + + Sophos Sweep reports a virus on a line like + this: + + Virus 'W32/Magistr-B' found in file ./those.bat + + For the "trigger" expression, we just use the + "found" word. For the "name" expression, we want + to get the W32/Magistr-B string, so we can match + for the single quotes left and right of it, + resulting in the regex '(.*)' (WITH the quotes!) + + Altogether, this makes the configuration + setting: + + av_scanner = cmdline:\ + /path/to/sweep -all -rec -archive %s:\ + found:'(.+)' + + +When av_scanner is correcly set, you can use the "malware" +condition in the DATA ACL. The condition takes a right-hand +argument that is expanded before use. It can then be one of + + - "true", "*", or "1", in which case the message is scanned + for viruses. The condition will succeed if a virus was + found, or fail otherwise. This is the recommended usage. + + - "false" or "0", in which case no scanning is done and the + condition will fail immediately. + + - a regular expression, in which case the message is scanned + for viruses. The condition will succeed if a virus found + found and its name matches the regular expression. This + allows you to take special actions on certain types of + viruses. + +When a virus was found, the condition sets up an expansion +variable called $malware_name that contains the name of the +virus found. You should use it in a "message" modifier that +contains the error returned to the sender. + +The malware condition caches its results, so when you use it +multiple times, the actual scanning process is only carried +out once. + +If your virus scanner cannot unpack MIME and TNEF containers +itself, you should use the demime condition prior to the +malware condition. + +Here is a simple example: + +/* ---------------------- +deny message = This message contains malware ($malware_name) + demime = * + malware = * +---------------------- */ + + + +5. The "fakereject" control statement + Reject a message while really accepting it. +-------------------------------------------------------------- + +When you put "control = fakereject" in an ACL statement, the +following will happen: If exim would have accepted the +message, it will tell the remote host that it did not, with a +message of: + +550-FAKE_REJECT id=xxxxxx-xxxxxx-xx +550-Your message has been rejected but is being kept for evaluation. +550 If it was a legit message, it may still be delivered to the target recipient(s). + +But exim will go on to treat the message as if it had accepted +it. This should be used with extreme caution, please look into +the examples document for possible usage. + + + +-------------------------------------------------------------- +End of file +-------------------------------------------------------------- diff -urN exim-4.30-orig/exim_monitor/em_globals.c exim-4.30/exim_monitor/em_globals.c --- exim-4.30-orig/exim_monitor/em_globals.c Mon Dec 1 11:15:41 2003 +++ exim-4.30/exim_monitor/em_globals.c Tue Dec 2 12:15:53 2003 @@ -126,6 +126,8 @@ BOOL deliver_manual_thaw = FALSE; BOOL dont_deliver = FALSE; +BOOL fake_reject = FALSE; + header_line *header_last = NULL; header_line *header_list = NULL; @@ -135,6 +137,7 @@ BOOL local_error_message = FALSE; uschar *local_scan_data = NULL; +uschar *spam_score_int = NULL; BOOL log_timezone = FALSE; int message_age = 0; uschar *message_id; diff -urN exim-4.30-orig/scripts/MakeLinks exim-4.30/scripts/MakeLinks --- exim-4.30-orig/scripts/MakeLinks Mon Dec 1 11:15:41 2003 +++ exim-4.30/scripts/MakeLinks Wed Jan 7 15:57:29 2004 @@ -172,15 +172,19 @@ ln -s ../src/dbfunctions.h dbfunctions.h ln -s ../src/dbstuff.h dbstuff.h +ln -s ../src/demime.h demime.h ln -s ../src/exim.h exim.h ln -s ../src/functions.h functions.h ln -s ../src/globals.h globals.h ln -s ../src/local_scan.h local_scan.h ln -s ../src/macros.h macros.h +ln -s ../src/mime.h mime.h ln -s ../src/mytypes.h mytypes.h ln -s ../src/osfunctions.h osfunctions.h +ln -s ../src/spam.h spam.h ln -s ../src/store.h store.h ln -s ../src/structs.h structs.h +ln -s ../src/tnef.h tnef.h ln -s ../src/acl.c acl.c ln -s ../src/buildconfig.c buildconfig.c @@ -190,6 +194,7 @@ ln -s ../src/dbfn.c dbfn.c ln -s ../src/debug.c debug.c ln -s ../src/deliver.c deliver.c +ln -s ../src/demime.c demime.c ln -s ../src/directory.c directory.c ln -s ../src/dns.c dns.c ln -s ../src/drtables.c drtables.c @@ -208,7 +213,9 @@ ln -s ../src/ip.c ip.c ln -s ../src/log.c log.c ln -s ../src/lss.c lss.c +ln -s ../src/malware.c malware.c ln -s ../src/match.c match.c +ln -s ../src/mime.c mime.c ln -s ../src/moan.c moan.c ln -s ../src/parse.c parse.c ln -s ../src/perl.c perl.c @@ -216,6 +223,7 @@ ln -s ../src/rda.c rda.c ln -s ../src/readconf.c readconf.c ln -s ../src/receive.c receive.c +ln -s ../src/regex.c regex.c ln -s ../src/retry.c retry.c ln -s ../src/rewrite.c rewrite.c ln -s ../src/rfc2047.c rfc2047.c @@ -224,13 +232,16 @@ ln -s ../src/sieve.c sieve.c ln -s ../src/smtp_in.c smtp_in.c ln -s ../src/smtp_out.c smtp_out.c +ln -s ../src/spam.c spam.c ln -s ../src/spool_in.c spool_in.c +ln -s ../src/spool_mbox.c spool_mbox.c ln -s ../src/spool_out.c spool_out.c ln -s ../src/store.c store.c ln -s ../src/string.c string.c ln -s ../src/tls.c tls.c ln -s ../src/tls-gnu.c tls-gnu.c ln -s ../src/tls-openssl.c tls-openssl.c +ln -s ../src/tnef.c tnef.c ln -s ../src/tod.c tod.c ln -s ../src/transport.c transport.c ln -s ../src/tree.c tree.c diff -urN exim-4.30-orig/src/acl.c exim-4.30/src/acl.c --- exim-4.30-orig/src/acl.c Mon Dec 1 11:15:41 2003 +++ exim-4.30/src/acl.c Thu Jan 15 16:50:33 2004 @@ -7,6 +7,8 @@ /* Code for handling Access Control Lists (ACLs) */ +/* This file has been modified by the exiscan-acl patch. */ + #include "exim.h" @@ -32,19 +34,19 @@ /* ACL condition and modifier codes - keep in step with the table that follows. */ -enum { ACLC_ACL, ACLC_AUTHENTICATED, ACLC_CONDITION, ACLC_CONTROL, ACLC_DELAY, +enum { ACLC_ACL, ACLC_AUTHENTICATED, ACLC_CONDITION, ACLC_CONTROL, ACLC_DECODE, ACLC_DELAY, ACLC_DEMIME, ACLC_DNSLISTS, ACLC_DOMAINS, ACLC_ENCRYPTED, ACLC_ENDPASS, ACLC_HOSTS, - ACLC_LOCAL_PARTS, ACLC_LOG_MESSAGE, ACLC_LOGWRITE, ACLC_MESSAGE, - ACLC_RECIPIENTS, ACLC_SENDER_DOMAINS, ACLC_SENDERS, ACLC_SET, ACLC_VERIFY }; + ACLC_LOCAL_PARTS, ACLC_LOG_MESSAGE, ACLC_LOGWRITE, ACLC_MALWARE, ACLC_MESSAGE, ACLC_MIME_REGEX, + ACLC_RECIPIENTS, ACLC_REGEX, ACLC_SENDER_DOMAINS, ACLC_SENDERS, ACLC_SET, ACLC_SPAM, ACLC_VERIFY }; /* ACL conditions/modifiers: "delay", "control", "endpass", "message", "log_message", "logwrite", and "set" are modifiers that look like conditions but always return TRUE. They are used for their side effects. */ static uschar *conditions[] = { US"acl", US"authenticated", US"condition", - US"control", US"delay", US"dnslists", US"domains", US"encrypted", - US"endpass", US"hosts", US"local_parts", US"log_message", US"logwrite", - US"message", US"recipients", US"sender_domains", US"senders", US"set", + US"control", US"decode", US"delay", US"demime", US"dnslists", US"domains", US"encrypted", + US"endpass", US"hosts", US"local_parts", US"log_message", US"logwrite", US"malware", + US"message", US"mime_regex", US"recipients", US"regex", US"sender_domains", US"senders", US"set", US"spam", US"verify" }; /* Flags to indicate for which conditions /modifiers a string expansion is done @@ -56,7 +58,9 @@ FALSE, /* authenticated */ TRUE, /* condition */ TRUE, /* control */ + TRUE, /* decode */ TRUE, /* delay */ + TRUE, /* demime */ TRUE, /* dnslists */ FALSE, /* domains */ FALSE, /* encrypted */ @@ -65,11 +69,15 @@ FALSE, /* local_parts */ TRUE, /* log_message */ TRUE, /* logwrite */ + TRUE, /* malware */ TRUE, /* message */ + TRUE, /* mime_regex */ FALSE, /* recipients */ + TRUE, /* regex */ FALSE, /* sender_domains */ FALSE, /* senders */ TRUE, /* set */ + TRUE, /* spam */ TRUE /* verify */ }; @@ -80,7 +88,9 @@ FALSE, /* authenticated */ FALSE, /* condition */ TRUE, /* control */ + FALSE, /* decode */ TRUE, /* delay */ + FALSE, /* demime */ FALSE, /* dnslists */ FALSE, /* domains */ FALSE, /* encrypted */ @@ -89,11 +99,15 @@ FALSE, /* local_parts */ TRUE, /* log_message */ TRUE, /* log_write */ + FALSE, /* malware */ TRUE, /* message */ + FALSE, /* mime_regex */ FALSE, /* recipients */ + FALSE, /* regex */ FALSE, /* sender_domains */ FALSE, /* senders */ TRUE, /* set */ + FALSE, /* spam */ FALSE /* verify */ }; @@ -102,6 +116,7 @@ static unsigned int cond_forbids[] = { 0, /* acl */ + (1<domain, &arg, 0, &domainlist_anchor, addr->domain_cache, MCL_DOMAIN, TRUE, &deliver_domain_data); @@ -1823,6 +1918,7 @@ if (where != ACL_WHERE_MAIL && where != ACL_WHERE_RCPT && where != ACL_WHERE_DATA && + where != ACL_WHERE_MIME && where != ACL_WHERE_NOTSMTP) { log_write(0, LOG_MAIN|LOG_PANIC, "\"discard\" verb not allowed in %s " diff -urN exim-4.30-orig/src/configure.default exim-4.30/src/configure.default --- exim-4.30-orig/src/configure.default Mon Dec 1 11:15:41 2003 +++ exim-4.30/src/configure.default Mon Jan 19 21:35:23 2004 @@ -108,6 +108,26 @@ # You should not change that setting until you understand how ACLs work. +# The following ACL entries are used if you want to do content scanning with +# the exiscan-acl patch. When you uncomment one of these lines, you must also +# review the respective entries in the ACL section further below. + +# acl_smtp_mime = acl_check_mime +# acl_smtp_data = acl_check_content + +# This configuration variable defines the virus scanner that is used with +# the 'malware' ACL condition of the exiscan acl-patch. If you do not use +# virus scanning, leave it commented. Please read doc/exiscan-acl-readme.txt +# for a list of supported scanners. + +# av_scanner = sophie:/var/run/sophie + +# The following setting is only needed if you use the 'spam' ACL condition +# of the exiscan-acl patch. It specifies on which host and port the SpamAssassin +# "spamd" daemon is listening. If you do not use this condition, or you use +# the default of "127.0.0.1 783", you can omit this option. + +# spamd_address = 127.0.0.1 783 # Specify the domain you want to be added to all unqualified addresses # here. An unqualified address is one that does not contain an "@" character @@ -340,6 +360,44 @@ deny message = relay not permitted +# These access control lists are used for content scanning with the exiscan-acl +# patch. You must also uncomment the entries for acl_smtp_data and acl_smtp_mime +# (scroll up), otherwise the ACLs will not be used. IMPORTANT: the default entries here +# should be treated as EXAMPLES. You MUST read the file doc/exiscan-acl-spec.txt +# to fully understand what you are doing ... + +acl_check_mime: + + # Just decode MIME parts to disk. + warn decode = default + + accept + +acl_check_content: + + # Reject virus infested messages. + deny message = This message contains malware ($malware_name) + malware = * + + # Always add X-Spam-Score and X-Spam-Report headers, using SA system-wide settings + # (user "nobody"), no matter if over threshold or not. + warn message = X-Spam-Score: $spam_score ($spam_bar) + spam = nobody:true + warn message = X-Spam-Report: $spam_report + spam = nobody:true + + # Add X-Spam-Flag if spam is over system-wide threshold + warn message = X-Spam-Flag: YES + spam = nobody + + # Reject spam messages with score over 10, using an extra condition. + deny message = This message scored $spam_score points. Congratulations! + spam = nobody:true + condition = ${if >{$spam_score_int}{100}{1}{0}} + + # finally accept all the rest + accept + ###################################################################### # ROUTERS CONFIGURATION # diff -urN exim-4.30-orig/src/demime.c exim-4.30/src/demime.c --- exim-4.30-orig/src/demime.c Thu Jan 1 01:00:00 1970 +++ exim-4.30/src/demime.c Tue Dec 2 12:15:53 2003 @@ -0,0 +1,1276 @@ +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* This file is part of the exiscan-acl content scanner +patch. It is NOT part of the standard exim distribution. */ + +/* Copyright (c) Tom Kistner 2003-???? */ +/* License: GPL */ + +/* Code for unpacking MIME containers. Called from acl.c. */ + +#include "exim.h" +#include "demime.h" + +uschar demime_reason_buffer[1024]; +struct file_extension *file_extensions = NULL; + +int demime(uschar **listptr) { + int sep = 0; + uschar *list = *listptr; + uschar *option; + uschar option_buffer[64]; + unsigned long long mbox_size; + FILE *mbox_file; + uschar defer_error_buffer[1024]; + int demime_rc = 0; + + /* reset found_extension variable */ + found_extension = NULL; + + /* try to find 1st option */ + if ((option = string_nextinlist(&list, &sep, + option_buffer, + sizeof(option_buffer))) != NULL) { + + /* parse 1st option */ + if ( (Ustrcmp(option,"false") == 0) || (Ustrcmp(option,"0") == 0) ) { + /* explicitly no demimeing */ + return FAIL; + }; + } + else { + /* no options -> no demimeing */ + return FAIL; + }; + + /* make sure the eml mbox file is spooled up */ + mbox_file = spool_mbox(&mbox_size); + + if (mbox_file == NULL) { + /* error while spooling */ + log_write(0, LOG_MAIN|LOG_PANIC, + "demime acl condition: error while creating mbox spool file"); + return DEFER; + }; + + /* call demimer if not already done earlier */ + if (!demime_ok) + demime_rc = mime_demux(mbox_file, defer_error_buffer); + + fclose(mbox_file); + + if (demime_rc == DEFER) { + /* temporary failure (DEFER => DEFER) */ + log_write(0, LOG_MAIN, + "demime acl condition: %s", defer_error_buffer); + return DEFER; + }; + + /* set demime_ok to avoid unpacking again */ + demime_ok = 1; + + /* check for file extensions, if there */ + while (option != NULL) { + struct file_extension *this_extension = file_extensions; + + /* Look for the wildcard. If it is found, we always return true. + The user must then use a custom condition to evaluate demime_errorlevel */ + if (Ustrcmp(option,"*") == 0) { + found_extension = NULL; + return OK; + }; + + /* loop thru extension list */ + while (this_extension != NULL) { + if (strcmpic(option, this_extension->file_extension_string) == 0) { + /* found one */ + found_extension = this_extension->file_extension_string; + return OK; + }; + this_extension = this_extension->next; + }; + + /* grab next extension from option list */ + option = string_nextinlist(&list, &sep, + option_buffer, + sizeof(option_buffer)); + }; + + /* nothing found */ + return FAIL; +} + + +/************************************************* +* unpack TNEF in given directory * +*************************************************/ + +int mime_unpack_tnef(uschar *directory) { + uschar filepath[1024]; + int n; + struct dirent *entry; + DIR *tempdir; + + /* open the dir */ + tempdir = opendir(CS directory); + if (tempdir == NULL) { + return -2; + }; + + /* loop thru dir */ + n = 0; + do { + entry = readdir(tempdir); + /* break on end of list */ + if (entry == NULL) break; + snprintf(CS filepath,1024,"%s/%s",directory,entry->d_name); + if ( (Ustrcmp(entry->d_name,"..") != 0) && (Ustrcmp(entry->d_name,".") != 0) ) { + TNEF_set_path(CS directory); + n = TNEF_main(CS filepath); + }; + } while (1); + + closedir(tempdir); + return 0; +} + + +/************************************************* +* small hex_str -> integer conversion function * +*************************************************/ + +/* needed for quoted-printable +*/ + +unsigned int mime_hstr_i(uschar *cptr) { + unsigned int i, j = 0; + + while (cptr && *cptr && isxdigit(*cptr)) { + i = *cptr++ - '0'; + if (9 < i) i -= 7; + j <<= 4; + j |= (i & 0x0f); + } + + return(j); +} + + +/************************************************* +* decode quoted-printable chars * +*************************************************/ + +/* gets called when we hit a = + returns: new pointer position + result code in c: + -2 - decode error + -1 - soft line break, no char + 0-255 - char to write +*/ + +uschar *mime_decode_qp(uschar *qp_p,int *c) { + uschar hex[] = {0,0,0}; + int nan = 0; + uschar *initial_pos = qp_p; + + /* advance one char */ + qp_p++; + + REPEAT_FIRST: + if ( (*qp_p == '\t') || (*qp_p == ' ') || (*qp_p == '\r') ) { + /* tab or whitespace may follow + just ignore it, but remember + that this is not a valid hex + encoding any more */ + nan = 1; + qp_p++; + goto REPEAT_FIRST; + } + else if ( (('0' <= *qp_p) && (*qp_p <= '9')) || (('A' <= *qp_p) && (*qp_p <= 'F')) || (('a' <= *qp_p) && (*qp_p <= 'f')) ) { + /* this is a valid hex char, if nan is unset */ + if (nan) { + /* this is illegal */ + *c = -2; + return initial_pos; + } + else { + hex[0] = *qp_p; + qp_p++; + }; + } + else if (*qp_p == '\n') { + /* hit soft line break already, continue */ + *c = -1; + return qp_p; + } + else { + /* illegal char here */ + *c = -2; + return initial_pos; + }; + + if ( (('0' <= *qp_p) && (*qp_p <= '9')) || (('A' <= *qp_p) && (*qp_p <= 'F')) || (('a' <= *qp_p) && (*qp_p <= 'f')) ) { + if (hex[0] > 0) { + hex[1] = *qp_p; + /* do hex conversion */ + *c = mime_hstr_i(hex); + qp_p++; + return qp_p; + } + else { + /* huh ? */ + *c = -2; + return initial_pos; + }; + } + else { + /* illegal char */ + *c = -2; + return initial_pos; + }; + +} + + +/************************************************* +* open new dump file * +*************************************************/ + +/* open new dump file + returns: -2 soft error + or file #, FILE * in f +*/ + +int mime_get_dump_file(uschar *extension, FILE **f, uschar *info) { + uschar file_name[1024]; + int result; + unsigned int file_nr; + uschar default_extension[] = ".com"; + uschar *p; + + if (extension == NULL) + extension = default_extension; + + /* scan the proposed extension. + if it is longer than 4 chars, or + contains exotic chars, use the default extension */ + +/* if (Ustrlen(extension) > 4) { + extension = default_extension; + }; +*/ + + p = extension+1; + + while (*p != 0) { + *p = (uschar)tolower((uschar)*p); + if ( (*p < 97) || (*p > 122) ) { + extension = default_extension; + break; + }; + p++; + }; + + /* find a new file to write to */ + file_nr = 0; + do { + struct stat mystat; + + snprintf(CS file_name,1024,"%s/scan/%s/%s-%05u%s",spool_directory,message_id,message_id,file_nr,extension); + file_nr++; + if (file_nr >= MIME_SANITY_MAX_DUMP_FILES) { + /* max parts reached */ + mime_trigger_error(MIME_ERRORLEVEL_TOO_MANY_PARTS); + break; + }; + result = stat(CS file_name,&mystat); + } + while(result != -1); + + *f = fopen(CS file_name,"w+"); + if (*f == NULL) { + /* cannot open new dump file, disk full ? -> soft error */ + snprintf(CS info, 1024,"unable to open dump file"); + return -2; + }; + + return file_nr; +} + + +/************************************************* +* Find a string in a mime header * +*************************************************/ + +/* Find a string in a mime header, and optionally fill in + the value associated with it into *value + + returns: 0 - nothing found + 1 - found param + 2 - found param + value +*/ + +int mime_header_find(uschar *header, uschar *param, uschar **value) { + uschar *needle; + + needle = strstric(header,param,FALSE); + if (needle != NULL) { + if (value != NULL) { + needle += Ustrlen(param); + if (*needle == '=') { + uschar *value_start; + uschar *value_end; + + value_start = needle + 1; + value_end = strstric(value_start,US";",FALSE); + if (value_end != NULL) { + /* allocate mem for value */ + *value = (uschar *)malloc((value_end - value_start)+1); + if (*value == NULL) + return 0; + + Ustrncpy(*value,value_start,(value_end - value_start)); + (*value)[(value_end - value_start)] = '\0'; + return 2; + }; + }; + }; + return 1; + }; + return 0; +} + + +/************************************************* +* Read a line of MIME input * +*************************************************/ +/* returns status code, one of + MIME_READ_LINE_EOF 0 + MIME_READ_LINE_OK 1 + MIME_READ_LINE_OVERFLOW 2 + + In header mode, the line will be "cooked". +*/ + +int mime_read_line(FILE *f, int mime_demux_mode, uschar *buffer, long *num_copied) { + int c = EOF; + int done = 0; + int header_value_mode = 0; + int header_open_brackets = 0; + + *num_copied = 0; + + while(!done) { + + c = fgetc(f); + if (c == EOF) break; + + /* --------- header mode -------------- */ + if (mime_demux_mode == MIME_DEMUX_MODE_MIME_HEADERS) { + + /* always skip CRs */ + if (c == '\r') continue; + + if (c == '\n') { + if ((*num_copied) > 0) { + /* look if next char is '\t' or ' ' */ + c = fgetc(f); + if (c == EOF) break; + if ( (c == '\t') || (c == ' ') ) continue; + ungetc(c,f); + }; + /* end of the header, terminate with ';' */ + c = ';'; + done = 1; + }; + + /* skip control characters */ + if (c < 32) continue; + + /* skip whitespace + tabs */ + if ( (c == ' ') || (c == '\t') ) + continue; + + if (header_value_mode) { + /* --------- value mode ----------- */ + /* skip quotes */ + if (c == '"') continue; + + /* leave value mode on ';' */ + if (c == ';') { + header_value_mode = 0; + }; + /* -------------------------------- */ + } + else { + /* -------- non-value mode -------- */ + if (c == '\\') { + /* quote next char. can be used + to escape brackets. */ + c = fgetc(f); + if (c == EOF) break; + } + else if (c == '(') { + header_open_brackets++; + continue; + } + else if ((c == ')') && header_open_brackets) { + header_open_brackets--; + continue; + } + else if ( (c == '=') && !header_open_brackets ) { + /* enter value mode */ + header_value_mode = 1; + }; + + /* skip chars while we are in a comment */ + if (header_open_brackets > 0) + continue; + /* -------------------------------- */ + }; + } + /* ------------------------------------ */ + else { + /* ----------- non-header mode -------- */ + /* break on '\n' */ + if (c == '\n') + done = 1; + /* ------------------------------------ */ + }; + + /* copy the char to the buffer */ + buffer[*num_copied] = (uschar)c; + /* raise counter */ + (*num_copied)++; + + /* break if buffer is full */ + if (*num_copied > MIME_SANITY_MAX_LINE_LENGTH-1) { + done = 1; + }; + } + + /* 0-terminate */ + buffer[*num_copied] = '\0'; + + if (*num_copied > MIME_SANITY_MAX_LINE_LENGTH-1) + return MIME_READ_LINE_OVERFLOW; + else + if (c == EOF) + return MIME_READ_LINE_EOF; + else + return MIME_READ_LINE_OK; +} + + +/************************************************* +* Check for a MIME boundary * +*************************************************/ + +/* returns: 0 - no boundary found + 1 - start boundary found + 2 - end boundary found +*/ + +int mime_check_boundary(uschar *line, struct boundary *boundaries) { + struct boundary *thisboundary = boundaries; + uschar workbuf[strlen(line)+1]; + unsigned int i,j=0; + + /* check for '--' first */ + if (Ustrncmp(line,"--",2) == 0) { + + /* strip tab and space */ + for (i = 2; i < strlen(line); i++) { + if ((line[i] != ' ') && (line[i] != '\t')) { + workbuf[j] = line[i]; + j++; + }; + }; + workbuf[j+1]='\0'; + + while(thisboundary != NULL) { + if (Ustrncmp(workbuf,thisboundary->boundary_string,Ustrlen(thisboundary->boundary_string)) == 0) { + if (Ustrncmp(&workbuf[Ustrlen(thisboundary->boundary_string)],"--",2) == 0) { + /* final boundary found */ + return 2; + }; + return 1; + }; + thisboundary = thisboundary->next; + }; + }; + + return 0; +} + + +/************************************************* +* Check for start of a UUENCODE block * +*************************************************/ + +/* returns 0 for no hit, + >0 for hit +*/ + +int mime_check_uu_start(uschar *line, uschar *uu_file_extension, int *has_tnef) { + + if ( (strncmpic(line,US"begin ",6) == 0)) { + uschar *uu_filename = &line[6]; + + /* skip perms, if present */ + Ustrtoul(&line[6],&uu_filename,10); + + /* advance one char */ + uu_filename++; + + /* This should be the filename. + Check if winmail.dat is present, + which indicates TNEF. */ + if (strncmpic(uu_filename,US"winmail.dat",11) == 0) { + *has_tnef = 1; + }; + + /* reverse to dot if present, + copy up to 4 chars for the extension */ + if (Ustrrchr(uu_filename,'.') != NULL) + uu_filename = Ustrrchr(uu_filename,'.'); + + return sscanf(CS uu_filename, "%4[.0-9A-Za-z]",CS uu_file_extension); + } + else { + /* nothing found */ + return 0; + }; +} + + +/************************************************* +* Decode a uu line * +*************************************************/ + +/* returns number of decoded bytes + -2 for soft errors +*/ + +int warned_about_uudec_line_sanity_1 = 0; +int warned_about_uudec_line_sanity_2 = 0; +long uu_decode_line(uschar *line, uschar **data, long line_len, uschar *info) { + uschar *p; + long num_decoded = 0; + uschar tmp_c; + uschar *work; + int uu_decoded_line_len, uu_encoded_line_len; + + /* allocate memory for data and work buffer */ + *data = (uschar *)malloc(line_len); + if (*data == NULL) { + snprintf(CS info, 1024,"unable to allocate %lu bytes",line_len); + return -2; + }; + + work = (uschar *)malloc(line_len); + if (work == NULL) { + snprintf(CS info, 1024,"unable to allocate %lu bytes",line_len); + return -2; + }; + + memcpy(work,line,line_len); + + /* First char is line length + This is microsofts way of getting it. Scary. */ + if (work[0] < 32) { + /* ignore this line */ + return 0; + } + else { + uu_decoded_line_len = uudec[work[0]]; + }; + + p = &work[1]; + + while (*p > 32) { + *p = uudec[*p]; + p++; + }; + + uu_encoded_line_len = (p - &work[1]); + p = &work[1]; + + /* check that resulting line length is a multiple of 4 */ + if ( ( uu_encoded_line_len % 4 ) != 0) { + if (!warned_about_uudec_line_sanity_1) { + mime_trigger_error(MIME_ERRORLEVEL_UU_MISALIGNED); + warned_about_uudec_line_sanity_1 = 1; + }; + return -1; + }; + + /* check that the line length matches */ + if ( ( (((uu_encoded_line_len/4)*3)-2) > uu_decoded_line_len ) || (((uu_encoded_line_len/4)*3) < uu_decoded_line_len) ) { + if (!warned_about_uudec_line_sanity_2) { + mime_trigger_error(MIME_ERRORLEVEL_UU_LINE_LENGTH); + warned_about_uudec_line_sanity_2 = 1; + }; + return -1; + }; + + while ( ((p - &work[1]) < uu_encoded_line_len) && (num_decoded < uu_decoded_line_len)) { + + /* byte 0 ---------------------- */ + if ((p - &work[1] + 1) >= uu_encoded_line_len) { + return 0; + } + + (*data)[num_decoded] = *p; + (*data)[num_decoded] <<= 2; + + tmp_c = *(p+1); + tmp_c >>= 4; + (*data)[num_decoded] |= tmp_c; + + num_decoded++; + p++; + + /* byte 1 ---------------------- */ + if ((p - &work[1] + 1) >= uu_encoded_line_len) { + return 0; + } + + (*data)[num_decoded] = *p; + (*data)[num_decoded] <<= 4; + + tmp_c = *(p+1); + tmp_c >>= 2; + (*data)[num_decoded] |= tmp_c; + + num_decoded++; + p++; + + /* byte 2 ---------------------- */ + if ((p - &work[1] + 1) >= uu_encoded_line_len) { + return 0; + } + + (*data)[num_decoded] = *p; + (*data)[num_decoded] <<= 6; + + (*data)[num_decoded] |= *(p+1); + + num_decoded++; + p+=2; + + }; + + return uu_decoded_line_len; +} + + +/************************************************* +* Decode a b64 or qp line * +*************************************************/ + +/* returns number of decoded bytes + -1 for hard errors + -2 for soft errors +*/ + +int warned_about_b64_line_length = 0; +int warned_about_b64_line_sanity = 0; +int warned_about_b64_illegal_char = 0; +int warned_about_qp_line_sanity = 0; +long mime_decode_line(int mime_demux_mode,uschar *line, uschar **data, long max_data_len, uschar *info) { + uschar *p; + long num_decoded = 0; + int offset = 0; + uschar tmp_c; + + /* allocate memory for data */ + *data = (uschar *)malloc(max_data_len); + if (*data == NULL) { + snprintf(CS info, 1024,"unable to allocate %lu bytes",max_data_len); + return -2; + }; + + if (mime_demux_mode == MIME_DEMUX_MODE_BASE64) { + /* ---------------------------------------------- */ + + /* NULL out trailing '\r' and '\n' chars */ + while (Ustrrchr(line,'\r') != NULL) { + *(Ustrrchr(line,'\r')) = '\0'; + }; + while (Ustrrchr(line,'\n') != NULL) { + *(Ustrrchr(line,'\n')) = '\0'; + }; + + /* check maximum base 64 line length */ + if (Ustrlen(line) > MIME_SANITY_MAX_B64_LINE_LENGTH ) { + if (!warned_about_b64_line_length) { + mime_trigger_error(MIME_ERRORLEVEL_B64_LINE_LENGTH); + warned_about_b64_line_length = 1; + }; + }; + + p = line; + offset = 0; + while (*(p+offset) != '\0') { + /* hit illegal char ? */ + if (b64[*(p+offset)] == 128) { + if (!warned_about_b64_illegal_char) { + mime_trigger_error(MIME_ERRORLEVEL_B64_ILLEGAL_CHAR); + warned_about_b64_illegal_char = 1; + }; + offset++; + } + else { + *p = b64[*(p+offset)]; + p++; + }; + }; + *p = 255; + + /* check that resulting line length is a multiple of 4 */ + if ( ( (p - &line[0]) % 4 ) != 0) { + if (!warned_about_b64_line_sanity) { + mime_trigger_error(MIME_ERRORLEVEL_B64_MISALIGNED); + warned_about_b64_line_sanity = 1; + }; + }; + + /* line is translated, start bit shifting */ + p = line; + num_decoded = 0; + + while(*p != 255) { + + /* byte 0 ---------------------- */ + if (*(p+1) == 255) { + break; + } + + (*data)[num_decoded] = *p; + (*data)[num_decoded] <<= 2; + + tmp_c = *(p+1); + tmp_c >>= 4; + (*data)[num_decoded] |= tmp_c; + + num_decoded++; + p++; + + /* byte 1 ---------------------- */ + if (*(p+1) == 255) { + break; + } + + (*data)[num_decoded] = *p; + (*data)[num_decoded] <<= 4; + + tmp_c = *(p+1); + tmp_c >>= 2; + (*data)[num_decoded] |= tmp_c; + + num_decoded++; + p++; + + /* byte 2 ---------------------- */ + if (*(p+1) == 255) { + break; + } + + (*data)[num_decoded] = *p; + (*data)[num_decoded] <<= 6; + + (*data)[num_decoded] |= *(p+1); + + num_decoded++; + p+=2; + + }; + return num_decoded; + /* ---------------------------------------------- */ + } + else if (mime_demux_mode == MIME_DEMUX_MODE_QP) { + /* ---------------------------------------------- */ + p = line; + + while (*p != 0) { + if (*p == '=') { + int decode_qp_result; + + p = mime_decode_qp(p,&decode_qp_result); + + if (decode_qp_result == -2) { + /* Error from decoder. p is unchanged. */ + if (!warned_about_qp_line_sanity) { + mime_trigger_error(MIME_ERRORLEVEL_QP_ILLEGAL_CHAR); + warned_about_qp_line_sanity = 1; + }; + (*data)[num_decoded] = '='; + num_decoded++; + p++; + } + else if (decode_qp_result == -1) { + /* End of the line with soft line break. + Bail out. */ + goto QP_RETURN; + } + else if (decode_qp_result >= 0) { + (*data)[num_decoded] = decode_qp_result; + num_decoded++; + }; + } + else { + (*data)[num_decoded] = *p; + num_decoded++; + p++; + }; + }; + QP_RETURN: + return num_decoded; + /* ---------------------------------------------- */ + }; + + return 0; +} + + + +/************************************************* +* Log demime errors and set mime error level * +*************************************************/ + +/* This sets the global demime_reason expansion +variable and the demime_errorlevel gauge. */ + +void mime_trigger_error(int level, uschar *format, ...) { + char *f; + va_list ap; + + if( (f = malloc(16384+23)) != NULL ) { + /* first log the incident */ + sprintf(f,"demime acl condition: "); + f+=22; + va_start(ap, format); + vsnprintf(f, 16383,(char *)format, ap); + va_end(ap); + f-=22; + log_write(0, LOG_MAIN, f); + /* then copy to demime_reason_buffer if new + level is greater than old level */ + if (level > demime_errorlevel) { + demime_errorlevel = level; + Ustrcpy(demime_reason_buffer, US f); + demime_reason = demime_reason_buffer; + }; + free(f); + }; +} + +/************************************************* +* Demultiplex MIME stream. * +*************************************************/ + +/* We can handle BASE64, QUOTED-PRINTABLE, and UUENCODE. + UUENCODE does not need to have a proper + transfer-encoding header, we detect it with "begin" + + This function will report human parsable errors in + *info. + + returns DEFER -> soft error (see *info) + OK -> EOF hit, all ok +*/ + +int mime_demux(FILE *f, uschar *info) { + int mime_demux_mode = MIME_DEMUX_MODE_MIME_HEADERS; + int uu_mode = MIME_UU_MODE_OFF; + FILE *mime_dump_file = NULL; + FILE *uu_dump_file = NULL; + uschar *line; + int mime_read_line_status = MIME_READ_LINE_OK; + long line_len; + struct boundary *boundaries = NULL; + struct mime_part mime_part_p; + int has_tnef = 0; + int has_rfc822 = 0; + + /* allocate room for our linebuffer */ + line = (uschar *)malloc(MIME_SANITY_MAX_LINE_LENGTH); + if (line == NULL) { + snprintf(CS info, 1024,"unable to allocate %u bytes",MIME_SANITY_MAX_LINE_LENGTH); + return DEFER; + }; + + /* clear MIME header structure */ + memset(&mime_part_p,0,sizeof(mime_part)); + + /* ----------------------- start demux loop --------------------- */ + while (mime_read_line_status == MIME_READ_LINE_OK) { + + /* read a line of input. Depending on the mode we are in, + the returned format will differ. */ + mime_read_line_status = mime_read_line(f,mime_demux_mode,line,&line_len); + + if (mime_read_line_status == MIME_READ_LINE_OVERFLOW) { + mime_trigger_error(MIME_ERRORLEVEL_LONG_LINE); + /* despite the error, continue .. */ + mime_read_line_status = MIME_READ_LINE_OK; + continue; + } + else if (mime_read_line_status == MIME_READ_LINE_EOF) { + break; + }; + + if (mime_demux_mode == MIME_DEMUX_MODE_MIME_HEADERS) { + /* -------------- header mode --------------------- */ + + /* Check for an empty line, which is the end of the headers. + In HEADER mode, the line is returned "cooked", with the + final '\n' replaced by a ';' */ + if (line_len == 1) { + int tmp; + + /* We have reached the end of the headers. Start decoding + with the collected settings. */ + if (mime_part_p.seen_content_transfer_encoding > 1) { + mime_demux_mode = mime_part_p.seen_content_transfer_encoding; + } + else { + /* default to plain mode if no specific encoding type found */ + mime_demux_mode = MIME_DEMUX_MODE_PLAIN; + }; + + /* open new dump file */ + tmp = mime_get_dump_file(mime_part_p.extension, &mime_dump_file, info); + if (tmp < 0) { + return DEFER; + }; + + /* clear out mime_part */ + memset(&mime_part_p,0,sizeof(mime_part)); + } + else { + /* Another header to check for file extensions, + encoding type and boundaries */ + if (strncmpic(US"content-type:",line,Ustrlen("content-type:")) == 0) { + /* ---------------------------- Content-Type header ------------------------------- */ + uschar *value = line; + + /* check for message/partial MIME type and reject it */ + if (mime_header_find(line,US"message/partial",NULL) > 0) + mime_trigger_error(MIME_ERRORLEVEL_MESSAGE_PARTIAL); + + /* check for TNEF content type, remember to unpack TNEF later. */ + if (mime_header_find(line,US"application/ms-tnef",NULL) > 0) + has_tnef = 1; + + /* check for message/rfcxxx attachments */ + if (mime_header_find(line,US"message/rfc822",NULL) > 0) + has_rfc822 = 1; + + /* find the file extension, but do not fill it in + it is already set, since content-disposition has + precedence. */ + if (mime_part_p.extension == NULL) { + if (mime_header_find(line,US"name",&value) == 2) { + if (Ustrlen(value) > MIME_SANITY_MAX_FILENAME) + mime_trigger_error(MIME_ERRORLEVEL_FILENAME_LENGTH); + mime_part_p.extension = value; + mime_part_p.extension = Ustrrchr(value,'.'); + if (mime_part_p.extension == NULL) { + /* file without extension, setting + NULL will use the default extension later */ + mime_part_p.extension = NULL; + } + else { + struct file_extension *this_extension = + (struct file_extension *)malloc(sizeof(file_extension)); + + this_extension->file_extension_string = + (uschar *)malloc(Ustrlen(mime_part_p.extension)+1); + Ustrcpy(this_extension->file_extension_string, + mime_part_p.extension+1); + this_extension->next = file_extensions; + file_extensions = this_extension; + }; + }; + }; + + /* find a boundary and add it to the list, if present */ + value = line; + if (mime_header_find(line,US"boundary",&value) == 2) { + struct boundary *thisboundary; + + if (Ustrlen(value) > MIME_SANITY_MAX_BOUNDARY_LENGTH) { + mime_trigger_error(MIME_ERRORLEVEL_BOUNDARY_LENGTH); + } + else { + thisboundary = (struct boundary*)malloc(sizeof(boundary)); + thisboundary->next = boundaries; + thisboundary->boundary_string = value; + boundaries = thisboundary; + }; + }; + + if (mime_part_p.seen_content_type == 0) { + mime_part_p.seen_content_type = 1; + } + else { + mime_trigger_error(MIME_ERRORLEVEL_DOUBLE_HEADERS); + }; + /* ---------------------------------------------------------------------------- */ + } + else if (strncmpic(US"content-transfer-encoding:",line,Ustrlen("content-transfer-encoding:")) == 0) { + /* ---------------------------- Content-Transfer-Encoding header -------------- */ + + if (mime_part_p.seen_content_transfer_encoding == 0) { + if (mime_header_find(line,US"base64",NULL) > 0) { + mime_part_p.seen_content_transfer_encoding = MIME_DEMUX_MODE_BASE64; + } + else if (mime_header_find(line,US"quoted-printable",NULL) > 0) { + mime_part_p.seen_content_transfer_encoding = MIME_DEMUX_MODE_QP; + } + else { + mime_part_p.seen_content_transfer_encoding = MIME_DEMUX_MODE_PLAIN; + }; + } + else { + mime_trigger_error(MIME_ERRORLEVEL_DOUBLE_HEADERS); + }; + /* ---------------------------------------------------------------------------- */ + } + else if (strncmpic(US"content-disposition:",line,Ustrlen("content-disposition:")) == 0) { + /* ---------------------------- Content-Disposition header -------------------- */ + uschar *value = line; + + if (mime_part_p.seen_content_disposition == 0) { + mime_part_p.seen_content_disposition = 1; + + if (mime_header_find(line,US"filename",&value) == 2) { + if (Ustrlen(value) > MIME_SANITY_MAX_FILENAME) + mime_trigger_error(MIME_ERRORLEVEL_FILENAME_LENGTH); + mime_part_p.extension = value; + mime_part_p.extension = Ustrrchr(value,'.'); + if (mime_part_p.extension == NULL) { + /* file without extension, setting + NULL will use the default extension later */ + mime_part_p.extension = NULL; + } + else { + struct file_extension *this_extension = + (struct file_extension *)malloc(sizeof(file_extension)); + + this_extension->file_extension_string = + (uschar *)malloc(Ustrlen(mime_part_p.extension)+1); + Ustrcpy(this_extension->file_extension_string, + mime_part_p.extension+1); + this_extension->next = file_extensions; + file_extensions = this_extension; + }; + }; + } + else { + mime_trigger_error(MIME_ERRORLEVEL_DOUBLE_HEADERS); + }; + /* ---------------------------------------------------------------------------- */ + }; + }; /* End of header checks */ + /* ------------------------------------------------ */ + } + else { + /* -------------- non-header mode ----------------- */ + int tmp; + + if (uu_mode == MIME_UU_MODE_OFF) { + uschar uu_file_extension[5]; + /* We are not currently decoding UUENCODE + Check for possible UUENCODE start tag. */ + if (mime_check_uu_start(line,uu_file_extension,&has_tnef)) { + /* possible UUENCODING start detected. + Set unconfirmed mode first. */ + uu_mode = MIME_UU_MODE_UNCONFIRMED; + /* open new uu dump file */ + tmp = mime_get_dump_file(uu_file_extension, &uu_dump_file, info); + if (tmp < 0) { + free(line); + return DEFER; + }; + }; + } + else { + uschar *data; + long data_len = 0; + + if (uu_mode == MIME_UU_MODE_UNCONFIRMED) { + /* We are in unconfirmed UUENCODE mode. */ + + data_len = uu_decode_line(line,&data,line_len,info); + + if (data_len == -2) { + /* temp error, turn off uudecode mode */ + if (uu_dump_file != NULL) { + fclose(uu_dump_file); uu_dump_file = NULL; + }; + uu_mode = MIME_UU_MODE_OFF; + return DEFER; + } + else if (data_len == -1) { + if (uu_dump_file != NULL) { + fclose(uu_dump_file); uu_dump_file = NULL; + }; + uu_mode = MIME_UU_MODE_OFF; + data_len = 0; + } + else if (data_len > 0) { + /* we have at least decoded a valid byte + turn on confirmed mode */ + uu_mode = MIME_UU_MODE_CONFIRMED; + }; + } + else if (uu_mode == MIME_UU_MODE_CONFIRMED) { + /* If we are in confirmed UU mode, + check for single "end" tag on line */ + if ((strncmpic(line,US"end",3) == 0) && (line[3] < 32)) { + if (uu_dump_file != NULL) { + fclose(uu_dump_file); uu_dump_file = NULL; + }; + uu_mode = MIME_UU_MODE_OFF; + } + else { + data_len = uu_decode_line(line,&data,line_len,info); + if (data_len == -2) { + /* temp error, turn off uudecode mode */ + if (uu_dump_file != NULL) { + fclose(uu_dump_file); uu_dump_file = NULL; + }; + uu_mode = MIME_UU_MODE_OFF; + return DEFER; + } + else if (data_len == -1) { + /* skip this line */ + data_len = 0; + }; + }; + }; + + /* write data to dump file, if available */ + if (data_len > 0) { + if (fwrite(data,1,data_len,uu_dump_file) < data_len) { + /* short write */ + snprintf(CS info, 1024,"short write on uudecode dump file"); + free(line); + return DEFER; + }; + }; + }; + + if (mime_demux_mode != MIME_DEMUX_MODE_SCANNING) { + /* Non-scanning and Non-header mode. That means + we are currently decoding data to the dump + file. */ + + /* Check for a known boundary. */ + tmp = mime_check_boundary(line,boundaries); + if (tmp == 1) { + /* We have hit a known start boundary. + That will put us back in header mode. */ + mime_demux_mode = MIME_DEMUX_MODE_MIME_HEADERS; + if (mime_dump_file != NULL) { + /* if the attachment was a RFC822 message, recurse into it */ + if (has_rfc822) { + has_rfc822 = 0; + rewind(mime_dump_file); + mime_demux(mime_dump_file,info); + }; + + fclose(mime_dump_file); mime_dump_file = NULL; + }; + } + else if (tmp == 2) { + /* We have hit a known end boundary. + That puts us into scanning mode, which will end when we hit another known start boundary */ + mime_demux_mode = MIME_DEMUX_MODE_SCANNING; + if (mime_dump_file != NULL) { + /* if the attachment was a RFC822 message, recurse into it */ + if (has_rfc822) { + has_rfc822 = 0; + rewind(mime_dump_file); + mime_demux(mime_dump_file,info); + }; + + fclose(mime_dump_file); mime_dump_file = NULL; + }; + } + else { + uschar *data; + long data_len = 0; + + /* decode the line with the appropriate method */ + if (mime_demux_mode == MIME_DEMUX_MODE_PLAIN) { + /* in plain mode, just dump the line */ + data = line; + data_len = line_len; + } + else if ( (mime_demux_mode == MIME_DEMUX_MODE_QP) || (mime_demux_mode == MIME_DEMUX_MODE_BASE64) ) { + data_len = mime_decode_line(mime_demux_mode,line,&data,line_len,info); + if (data_len < 0) { + /* Error reported from the line decoder. */ + data_len = 0; + }; + }; + + /* write data to dump file */ + if (data_len > 0) { + if (fwrite(data,1,data_len,mime_dump_file) < data_len) { + /* short write */ + snprintf(CS info, 1024,"short write on dump file"); + free(line); + return DEFER; + }; + }; + + }; + } + else { + /* Scanning mode. We end up here after a end boundary. + This will usually be at the end of a message or at + the end of a MIME container. + We need to look for another start boundary to get + back into header mode. */ + if (mime_check_boundary(line,boundaries) == 1) { + mime_demux_mode = MIME_DEMUX_MODE_MIME_HEADERS; + }; + + }; + /* ------------------------------------------------ */ + }; + }; + /* ----------------------- end demux loop ----------------------- */ + + /* close files, they could still be open */ + if (mime_dump_file != NULL) + fclose(mime_dump_file); + if (uu_dump_file != NULL) + fclose(uu_dump_file); + + /* release line buffer */ + free(line); + + /* FIXME: release boundary buffers. + Not too much of a problem since + this instance of exim is not resident. */ + + if (has_tnef) { + uschar file_name[1024]; + /* at least one file could be TNEF encoded. + attempt to send all decoded files thru the TNEF decoder */ + + snprintf(CS file_name,1024,"%s/scan/%s",spool_directory,message_id); + mime_unpack_tnef(file_name); + }; + + return 0; +} + diff -urN exim-4.30-orig/src/demime.h exim-4.30/src/demime.h --- exim-4.30-orig/src/demime.h Thu Jan 1 01:00:00 1970 +++ exim-4.30/src/demime.h Tue Dec 2 12:15:53 2003 @@ -0,0 +1,146 @@ +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* This file is part of the exiscan-acl content scanner +patch. It is NOT part of the standard exim distribution. */ + +/* Copyright (c) Tom Kistner 2003-???? */ +/* License: GPL */ + +/* demime defines */ + +#define MIME_DEMUX_MODE_SCANNING 0 +#define MIME_DEMUX_MODE_MIME_HEADERS 1 +#define MIME_DEMUX_MODE_BASE64 2 +#define MIME_DEMUX_MODE_QP 3 +#define MIME_DEMUX_MODE_PLAIN 4 + +#define MIME_UU_MODE_OFF 0 +#define MIME_UU_MODE_UNCONFIRMED 1 +#define MIME_UU_MODE_CONFIRMED 2 + +#define MIME_MAX_EXTENSION 128 + +#define MIME_READ_LINE_EOF 0 +#define MIME_READ_LINE_OK 1 +#define MIME_READ_LINE_OVERFLOW 2 + +#define MIME_SANITY_MAX_LINE_LENGTH 131071 +#define MIME_SANITY_MAX_FILENAME 512 +#define MIME_SANITY_MAX_HEADER_OPTION_VALUE 1024 +#define MIME_SANITY_MAX_B64_LINE_LENGTH 76 +#define MIME_SANITY_MAX_BOUNDARY_LENGTH 1024 +#define MIME_SANITY_MAX_DUMP_FILES 1024 + + + +/* MIME errorlevel settings */ + +#define MIME_ERRORLEVEL_LONG_LINE 3,US"line length in message or single header size exceeds %u bytes",MIME_SANITY_MAX_LINE_LENGTH +#define MIME_ERRORLEVEL_TOO_MANY_PARTS 3,US"too many MIME parts (max %u)",MIME_SANITY_MAX_DUMP_FILES +#define MIME_ERRORLEVEL_MESSAGE_PARTIAL 3,US"'message/partial' MIME type" +#define MIME_ERRORLEVEL_FILENAME_LENGTH 3,US"proposed filename exceeds %u characters",MIME_SANITY_MAX_FILENAME +#define MIME_ERRORLEVEL_BOUNDARY_LENGTH 3,US"boundary length exceeds %u characters",MIME_SANITY_MAX_BOUNDARY_LENGTH +#define MIME_ERRORLEVEL_DOUBLE_HEADERS 2,US"double headers (content-type, content-disposition or content-transfer-encoding)" +#define MIME_ERRORLEVEL_UU_MISALIGNED 1,US"uuencoded line length is not a multiple of 4 characters" +#define MIME_ERRORLEVEL_UU_LINE_LENGTH 1,US"uuencoded line length does not match advertised number of bytes" +#define MIME_ERRORLEVEL_B64_LINE_LENGTH 1,US"base64 line length exceeds %u characters",MIME_SANITY_MAX_B64_LINE_LENGTH +#define MIME_ERRORLEVEL_B64_ILLEGAL_CHAR 2,US"base64 line contains illegal character" +#define MIME_ERRORLEVEL_B64_MISALIGNED 1,US"base64 line length is not a multiple of 4 characters" +#define MIME_ERRORLEVEL_QP_ILLEGAL_CHAR 1,US"quoted-printable encoding contains illegal character" + + +/* demime structures */ + +typedef struct mime_part { + /* true if there was a content-type header */ + int seen_content_type; + /* true if there was a content-transfer-encoding header + contains the encoding type */ + int seen_content_transfer_encoding; + /* true if there was a content-disposition header */ + int seen_content_disposition; + /* pointer to a buffer with the proposed file extension */ + uschar *extension; +} mime_part; + +typedef struct boundary { + struct boundary *next; + uschar *boundary_string; +} boundary; + +typedef struct file_extension { + struct file_extension *next; + uschar *file_extension_string; +} file_extension; + +/* available functions for the TNEF library (tnef.c & tnef.h) */ + +extern int TNEF_main( char *filename ); +extern int TNEF_set_verbosity( int level ); +extern int TNEF_set_debug( int level ); +extern int TNEF_set_syslogging( int level ); +extern int TNEF_set_stderrlogging( int level ); +extern int TNEF_set_path( char *path ); + + + +/* demime.c prototypes */ + +int mime_unpack_tnef(uschar *); +unsigned int mime_hstr_i(uschar *); +uschar *mime_decode_qp(uschar *, int *); +int mime_get_dump_file(uschar *, FILE **, uschar *); +int mime_header_find(uschar *, uschar *, uschar **); +int mime_read_line(FILE *, int, uschar *, long *); +int mime_check_boundary(uschar *, struct boundary *); +int mime_check_uu_start(uschar *, uschar *, int *); +long uu_decode_line(uschar *, uschar **, long, uschar *); +long mime_decode_line(int ,uschar *, uschar **, long, uschar *); +void mime_trigger_error(int, uschar *, ...); +int mime_demux(FILE *, uschar *); + + + +/* BASE64 decoder matrix */ +static unsigned char b64[256]={ +/* 0 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, +/* 16 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, +/* 32 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 62, 128, 128, 128, 63, +/* 48 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 255, 128, 128, +/* 64 */ 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, +/* 80 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 128, 128, 128, 128, 128, +/* 96 */ 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128 +}; + + +/* Microsoft-Style uudecode matrix */ +static unsigned char uudec[256]={ +/* 0 */ 0, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, +/* 16 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, +/* 32 */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +/* 48 */ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, +/* 64 */ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, +/* 80 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, +/* 96 */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +/* 112 */ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, +/* 128 */ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, +/* 144 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, +/* 160 */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +/* 176 */ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, +/* 192 */ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, +/* 208 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, +/* 224 */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +/* 240 */ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 +}; + diff -urN exim-4.30-orig/src/exim.c exim-4.30/src/exim.c --- exim-4.30-orig/src/exim.c Mon Dec 1 11:15:41 2003 +++ exim-4.30/src/exim.c Tue Dec 2 12:15:53 2003 @@ -1500,6 +1500,7 @@ printf("%s\n", CS version_copyright); version_printed = TRUE; show_whats_supported(stdout); + printf("Contains exiscan-acl patch revision %s (c) Tom Kistner [http://duncanthrax.net/exiscan/]\n", exiscan_version_string); } else badarg = TRUE; diff -urN exim-4.30-orig/src/expand.c exim-4.30/src/expand.c --- exim-4.30-orig/src/expand.c Mon Dec 1 11:15:41 2003 +++ exim-4.30/src/expand.c Mon Jan 19 23:23:34 2004 @@ -226,11 +226,14 @@ { "caller_uid", vtype_int, &real_uid }, { "compile_date", vtype_stringptr, &version_date }, { "compile_number", vtype_stringptr, &version_cnumber }, + { "demime_errorlevel", vtype_int, &demime_errorlevel }, + { "demime_reason", vtype_stringptr, &demime_reason }, { "dnslist_domain", vtype_stringptr, &dnslist_domain }, { "dnslist_text", vtype_stringptr, &dnslist_text }, { "dnslist_value", vtype_stringptr, &dnslist_value }, { "domain", vtype_stringptr, &deliver_domain }, { "domain_data", vtype_stringptr, &deliver_domain_data }, + { "found_extension", vtype_stringptr, &found_extension }, { "home", vtype_stringptr, &deliver_home }, { "host", vtype_stringptr, &deliver_host }, { "host_address", vtype_stringptr, &deliver_host_address }, @@ -252,6 +255,7 @@ { "local_user_uid", vtype_uid, &local_user_uid }, { "localhost_number", vtype_int, &host_number }, { "mailstore_basename", vtype_stringptr, &mailstore_basename }, + { "malware_name", vtype_stringptr, &malware_name }, { "message_age", vtype_int, &message_age }, { "message_body", vtype_msgbody, &message_body }, { "message_body_end", vtype_msgbody_end, &message_body_end }, @@ -259,6 +263,20 @@ { "message_headers", vtype_msgheaders, NULL }, { "message_id", vtype_stringptr, &message_id }, { "message_size", vtype_int, &message_size }, + { "mime_anomaly_level", vtype_int, &mime_anomaly_level }, + { "mime_anomaly_text", vtype_stringptr, &mime_anomaly_text }, + { "mime_boundary", vtype_stringptr, &mime_boundary }, + { "mime_charset", vtype_stringptr, &mime_charset }, + { "mime_content_description", vtype_stringptr, &mime_content_description }, + { "mime_content_disposition", vtype_stringptr, &mime_content_disposition }, + { "mime_content_id", vtype_stringptr, &mime_content_id }, + { "mime_content_transfer_encoding",vtype_stringptr, &mime_content_transfer_encoding }, + { "mime_content_type", vtype_stringptr, &mime_content_type }, + { "mime_decoded_filename", vtype_stringptr, &mime_decoded_filename }, + { "mime_filename", vtype_stringptr, &mime_filename }, + { "mime_is_multipart", vtype_int, &mime_is_multipart }, + { "mime_is_rfc822", vtype_int, &mime_is_rfc822 }, + { "mime_part_count", vtype_int, &mime_part_count }, { "n0", vtype_filter_int, &filter_n[0] }, { "n1", vtype_filter_int, &filter_n[1] }, { "n2", vtype_filter_int, &filter_n[2] }, @@ -287,6 +305,7 @@ { "received_protocol", vtype_stringptr, &received_protocol }, { "recipients", vtype_recipients, NULL }, { "recipients_count", vtype_int, &recipients_count }, + { "regex_match_string", vtype_stringptr, ®ex_match_string }, { "reply_address", vtype_reply, NULL }, { "return_path", vtype_stringptr, &return_path }, { "return_size_limit", vtype_int, &bounce_return_size_limit }, @@ -314,6 +333,10 @@ { "sn7", vtype_filter_int, &filter_sn[7] }, { "sn8", vtype_filter_int, &filter_sn[8] }, { "sn9", vtype_filter_int, &filter_sn[9] }, + { "spam_bar", vtype_stringptr, &spam_bar }, + { "spam_report", vtype_stringptr, &spam_report }, + { "spam_score", vtype_stringptr, &spam_score }, + { "spam_score_int", vtype_stringptr, &spam_score_int }, { "spool_directory", vtype_stringptr, &spool_directory }, { "thisaddress", vtype_stringptr, &filter_thisaddress }, { "tls_certificate_verified", vtype_int, &tls_certificate_verified }, diff -urN exim-4.30-orig/src/functions.h exim-4.30/src/functions.h --- exim-4.30-orig/src/functions.h Mon Dec 1 11:15:41 2003 +++ exim-4.30/src/functions.h Tue Dec 2 12:15:53 2003 @@ -66,6 +66,7 @@ extern void deliver_set_expansions(address_item *); extern int deliver_split_address(address_item *); extern void deliver_succeeded(address_item *); +extern int demime(uschar **); extern BOOL directory_make(uschar *, uschar *, int, BOOL); extern dns_address *dns_address_from_rr(dns_answer *, dns_record *); extern void dns_build_reverse(uschar *, uschar *); @@ -118,6 +119,7 @@ extern void log_close_all(void); +extern int malware(uschar **); extern int match_address_list(uschar *, BOOL, uschar **, unsigned int *, int, int); extern int match_check_list(uschar **, int, tree_node **, unsigned int **, @@ -174,6 +176,7 @@ extern BOOL receive_check_set_sender(uschar *); extern BOOL receive_msg(BOOL); extern void receive_swallow_smtp(void); +extern int regex(uschar **); extern BOOL regex_match_and_setup(const pcre *, uschar *, int, int); extern const pcre *regex_must_compile(uschar *, BOOL, BOOL); extern void retry_add_item(address_item *, uschar *, int); @@ -231,6 +234,8 @@ extern BOOL smtp_start_session(void); extern int smtp_ungetc(int); extern int smtp_write_command(smtp_outblock *, BOOL, char *, ...); +extern int spam(uschar **); +extern FILE *spool_mbox(unsigned long long *); extern BOOL spool_move_message(uschar *, uschar *, uschar *, uschar *); extern BOOL spool_open_datafile(uschar *); extern int spool_open_temp(uschar *); @@ -281,6 +286,8 @@ extern tree_node *tree_search(tree_node *, uschar *); extern void tree_write(tree_node *, FILE *); +extern void unspool_mbox(void); + extern int verify_address(address_item *, FILE *, int, int, BOOL *); extern int verify_check_dnsbl(uschar **); extern int verify_check_header_address(uschar **, uschar **, int); diff -urN exim-4.30-orig/src/globals.c exim-4.30/src/globals.c --- exim-4.30-orig/src/globals.c Mon Dec 1 11:15:41 2003 +++ exim-4.30/src/globals.c Fri Jan 16 20:39:49 2004 @@ -159,6 +159,7 @@ uschar *acl_smtp_helo = NULL; uschar *acl_smtp_mail = NULL; uschar *acl_smtp_mailauth = NULL; +uschar *acl_smtp_mime = NULL; uschar *acl_smtp_rcpt = NULL; uschar *acl_smtp_starttls = NULL; uschar *acl_smtp_vrfy = NULL; @@ -179,6 +180,7 @@ US"EHLO or HELO", US"MAIL", US"MAILAUTH", + US"MIME", US"RCPT", US"STARTTLS", US"VRFY", @@ -192,6 +194,7 @@ 550, /* HELO/EHLO */ 550, /* MAIL */ 0, /* MAILAUTH; not relevant */ + 550, /* MIME */ 550, /* RCPT */ 550, /* STARTTLS */ 252, /* VRFY */ @@ -294,6 +297,7 @@ uschar *auth_defer_msg = US"reason not recorded"; uschar *auth_defer_user_msg = US""; int auto_thaw = 0; +uschar *av_scanner = US"sophie:/var/run/sophie"; BOOL background_daemon = TRUE; uschar *base62_chars= @@ -410,6 +414,9 @@ BOOL deliver_selectstring_regex = FALSE; uschar *deliver_selectstring_sender = NULL; BOOL deliver_selectstring_sender_regex = FALSE; +int demime_errorlevel = 0; +int demime_ok = 0; +uschar *demime_reason = NULL; BOOL disable_logging = FALSE; uschar *dns_again_means_nonexist = NULL; @@ -439,6 +446,7 @@ "\0<---------------Space to patch exim_path->"; uid_t exim_uid = EXIM_UID; BOOL exim_uid_set = TRUE; /* This uid is always set */ +uschar *exiscan_version_string = US"??"; int expand_forbid = 0; int expand_nlength[EXPAND_MAXN+1]; int expand_nmax = -1; @@ -448,12 +456,14 @@ BOOL extract_addresses_remove_arguments = TRUE; uschar *extra_local_interfaces = NULL; +BOOL fake_reject = FALSE; int filter_n[FILTER_VARIABLE_COUNT]; BOOL filter_running = FALSE; int filter_sn[FILTER_VARIABLE_COUNT]; uschar *filter_test = NULL; uschar *filter_thisaddress = NULL; int finduser_retries = 0; +uschar *found_extension = NULL; uid_t fixed_never_users[] = { FIXED_NEVER_USERS }; uschar *freeze_tell = NULL; uschar *fudged_queue_times = US""; @@ -591,6 +601,7 @@ macro_item *macros = NULL; uschar *mailstore_basename = NULL; +uschar *malware_name = NULL; int max_username_length = 0; int message_age = 0; uschar *message_body = NULL; @@ -611,6 +622,21 @@ uschar *message_size_limit = US"50M"; uschar message_subdir[2] = { 0, 0 }; uschar *message_reference = NULL; +uschar *mime_anomaly_level = NULL; +uschar *mime_anomaly_text = NULL; +uschar *mime_boundary = NULL; +uschar *mime_charset = NULL; +uschar *mime_content_description = NULL; +uschar *mime_content_disposition = NULL; +uschar *mime_content_id = NULL; +uschar *mime_content_transfer_encoding = NULL; +uschar *mime_content_type = NULL; +uschar *mime_decoded_filename = NULL; +uschar *mime_filename = NULL; +int mime_is_multipart = 0; +int mime_is_rfc822 = 0; +int mime_part_count = -1; + uid_t *never_users = NULL; @@ -710,6 +736,7 @@ const pcre *regex_PIPELINING = NULL; const pcre *regex_SIZE = NULL; const pcre *regex_ismsgid = NULL; +uschar *regex_match_string = NULL; int remote_delivery_count = 0; int remote_max_parallel = 2; uschar *remote_sort_domains = NULL; @@ -886,6 +913,11 @@ int smtp_rlr_threshold = INT_MAX; BOOL smtp_use_pipelining = FALSE; BOOL smtp_use_size = FALSE; +uschar *spamd_address = US"127.0.0.1 783"; +uschar *spam_bar = NULL; +uschar *spam_report = NULL; +uschar *spam_score = NULL; +uschar *spam_score_int = NULL; BOOL split_spool_directory = FALSE; uschar *spool_directory = US SPOOL_DIRECTORY "\0<--------------Space to patch spool_directory->"; diff -urN exim-4.30-orig/src/globals.h exim-4.30/src/globals.h --- exim-4.30-orig/src/globals.h Mon Dec 1 11:15:41 2003 +++ exim-4.30/src/globals.h Fri Jan 16 20:39:55 2004 @@ -101,6 +101,7 @@ extern uschar *acl_smtp_helo; /* ACL run after HELO/EHLO */ extern uschar *acl_smtp_mail; /* ACL run after MAIL */ extern uschar *acl_smtp_mailauth; /* ACL run after MAIL AUTH */ +extern uschar *acl_smtp_mime; /* ACL run after DATA, before acl_smtp_data, for each MIME part */ extern uschar *acl_smtp_rcpt; /* ACL run after RCPT */ extern uschar *acl_smtp_starttls; /* ACL run after STARTTLS */ extern uschar *acl_smtp_vrfy; /* ACL run after VRFY */ @@ -135,6 +136,7 @@ extern uschar *auth_defer_msg; /* Error message for log */ extern uschar *auth_defer_user_msg; /* Error message for user */ extern int auto_thaw; /* Auto-thaw interval */ +extern uschar *av_scanner; /* AntiVirus scanner to use for the malware condition */ extern BOOL background_daemon; /* Set FALSE to keep in foreground */ extern uschar *base62_chars; /* Table of base-62 characters */ @@ -218,6 +220,9 @@ extern BOOL deliver_selectstring_regex; /* String is regex */ extern uschar *deliver_selectstring_sender; /* For selecting by sender */ extern BOOL deliver_selectstring_sender_regex; /* String is regex */ +extern int demime_errorlevel; /* Severity of MIME error */ +extern int demime_ok; /* Nonzero if message has been demimed */ +extern uschar *demime_reason; /* Reason for broken MIME container */ extern BOOL disable_logging; /* Disables log writing when TRUE */ extern uschar *dns_again_means_nonexist; /* Domains that are badly set up */ @@ -247,6 +252,7 @@ extern uschar *exim_path; /* Path to exec exim */ extern uid_t exim_uid; /* Non-root uid for exim */ extern BOOL exim_uid_set; /* TRUE if exim_uid set */ +extern uschar *exiscan_version_string; /* Exiscan version string */ extern int expand_forbid; /* RDO flags for forbidding things */ extern int expand_nlength[]; /* Lengths of numbered strings */ extern int expand_nmax; /* Max numerical value */ @@ -255,6 +261,7 @@ extern BOOL extract_addresses_remove_arguments; /* Controls -t behaviour */ extern uschar *extra_local_interfaces; /* Local, non-listen interfaces */ +extern BOOL fake_reject; /* TRUE if fake reject is to be given */ extern int filter_n[FILTER_VARIABLE_COUNT]; /* filter variables */ extern BOOL filter_running; /* TRUE while running a filter */ extern int filter_sn[FILTER_VARIABLE_COUNT]; /* variables set by system filter */ @@ -262,6 +269,7 @@ extern uschar *filter_thisaddress; /* For address looping */ extern int finduser_retries; /* Retry count for getpwnam() */ extern uid_t fixed_never_users[]; /* Can't be overridden */ +extern uschar *found_extension; /* demime acl condition: file extension found */ extern uschar *freeze_tell; /* Message on (some) freezings */ extern uschar *fudged_queue_times; /* For use in test harness */ @@ -339,6 +347,7 @@ extern macro_item *macros; /* Configuration macros */ extern uschar *mailstore_basename; /* For mailstore deliveries */ +extern uschar *malware_name; /* Name of virus or malware ("W32/Klez-H") */ extern int max_username_length; /* For systems with broken getpwnam() */ extern int message_age; /* In seconds */ extern uschar *message_body; /* Start of message body for filter */ @@ -358,6 +367,21 @@ extern uschar *message_size_limit; /* As it says */ extern uschar message_subdir[]; /* Subdirectory for messages */ extern uschar *message_reference; /* Reference for error messages */ +extern uschar *mime_anomaly_level; +extern uschar *mime_anomaly_text; +extern uschar *mime_boundary; +extern uschar *mime_charset; +extern uschar *mime_content_description; +extern uschar *mime_content_disposition; +extern uschar *mime_content_id; +extern uschar *mime_content_transfer_encoding; +extern uschar *mime_content_type; +extern uschar *mime_decoded_filename; +extern uschar *mime_filename; +extern int mime_is_multipart; +extern int mime_is_rfc822; +extern int mime_part_count; + extern uid_t *never_users; /* List of uids never to be used */ @@ -442,6 +466,7 @@ extern const pcre *regex_PIPELINING; /* For recognizing PIPELINING */ extern const pcre *regex_SIZE; /* For recognizing SIZE settings */ extern const pcre *regex_ismsgid; /* Compiled r.e. for message it */ +extern uschar *regex_match_string; /* regex that matched a line (regex ACL condition) */ extern int remote_delivery_count; /* Number of remote addresses */ extern int remote_max_parallel; /* Maximum parallel delivery */ extern uschar *remote_sort_domains; /* Remote domain sorting order */ @@ -530,6 +555,11 @@ extern BOOL smtp_use_pipelining; /* Global for passed connections */ extern BOOL smtp_use_size; /* Global for passed connections */ extern BOOL split_spool_directory; /* TRUE to use multiple subdirs */ +extern uschar *spamd_address; /* address for the spamassassin daemon */ +extern uschar *spam_bar; /* the spam "bar" (textual representation of spam_score) */ +extern uschar *spam_report; /* the spamd report (multiline) */ +extern uschar *spam_score; /* the spam score (float) */ +extern uschar *spam_score_int; /* spam_score * 10 (int) */ extern uschar *spool_directory; /* Name of spool directory */ extern int string_datestamp_offset;/* After insertion by string_format */ extern BOOL strip_excess_angle_brackets; /* Surrounding route-addrs */ diff -urN exim-4.30-orig/src/macros.h exim-4.30/src/macros.h --- exim-4.30-orig/src/macros.h Mon Dec 1 11:15:41 2003 +++ exim-4.30/src/macros.h Mon Jan 5 15:11:30 2004 @@ -688,7 +688,7 @@ enum { ACL_WHERE_AUTH, ACL_WHERE_CONNECT, ACL_WHERE_DATA, ACL_WHERE_ETRN, ACL_WHERE_EXPN, ACL_WHERE_HELO, ACL_WHERE_MAIL, - ACL_WHERE_MAILAUTH, ACL_WHERE_RCPT, + ACL_WHERE_MAILAUTH, ACL_WHERE_MIME, ACL_WHERE_RCPT, ACL_WHERE_STARTTLS, ACL_WHERE_VRFY, ACL_WHERE_NOTSMTP }; /* Situations for spool_write_header() */ diff -urN exim-4.30-orig/src/malware.c exim-4.30/src/malware.c --- exim-4.30-orig/src/malware.c Thu Jan 1 01:00:00 1970 +++ exim-4.30/src/malware.c Mon Jan 5 14:46:53 2004 @@ -0,0 +1,1181 @@ +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* This file is part of the exiscan-acl content scanner +patch. It is NOT part of the standard exim distribution. */ + +/* Copyright (c) Tom Kistner 2003-???? */ +/* License: GPL */ + +/* Code for calling virus (malware) scanners. Called from acl.c. */ + +#include "exim.h" + +/* declaration of private routines */ +int mksd_scan_packed(int sock); +int mksd_scan_unpacked(int sock, int maxproc); + +/* SHUT_WR seems to be undefined on Unixware ? */ +#ifndef SHUT_WR +#define SHUT_WR 1 +#endif + +#define DRWEBD_SCAN_CMD 0x0001 +#define DRWEBD_RETURN_VIRUSES 0x0001 + +/* Routine to check whether a system is big- or litte-endian. + Ripped from http://www.faqs.org/faqs/graphics/fileformats-faq/part4/section-7.html + Needed for proper kavdaemon implementation. Sigh. */ +#define BIG_MY_ENDIAN 0 +#define LITTLE_MY_ENDIAN 1 +int test_byte_order(void); +int test_byte_order() { + short int word = 0x0001; + char *byte = (char *) &word; + return(byte[0] ? LITTLE_MY_ENDIAN : BIG_MY_ENDIAN); +} + +uschar malware_name_buffer[256]; +int malware_ok = 0; + +int malware(uschar **listptr) { + int sep = 0; + uschar *list = *listptr; + uschar *av_scanner_work = av_scanner; + uschar *scanner_name; + uschar scanner_name_buffer[16]; + uschar *malware_regex; + uschar malware_regex_buffer[64]; + uschar malware_regex_default[] = ".+"; + unsigned long long mbox_size; + FILE *mbox_file; + int roffset; + const pcre *re; + const uschar *rerror; + + /* make sure the eml mbox file is spooled up */ + mbox_file = spool_mbox(&mbox_size); + if (mbox_file == NULL) { + /* error while spooling */ + log_write(0, LOG_MAIN|LOG_PANIC, + "malware acl condition: error while creating mbox spool file"); + return DEFER; + }; + /* none of our current scanners need the mbox + file as a stream, so we can close it right away */ + fclose(mbox_file); + + /* extract the malware regex to match against from the option list */ + if ((malware_regex = string_nextinlist(&list, &sep, + malware_regex_buffer, + sizeof(malware_regex_buffer))) != NULL) { + + /* parse 1st option */ + if ( (strcmpic(malware_regex,US"false") == 0) || + (Ustrcmp(malware_regex,"0") == 0) ) { + /* explicitly no matching */ + return FAIL; + }; + + /* special cases (match anything except empty) */ + if ( (strcmpic(malware_regex,US"true") == 0) || + (Ustrcmp(malware_regex,"*") == 0) || + (Ustrcmp(malware_regex,"1") == 0) ) { + malware_regex = malware_regex_default; + }; + } + else { + /* empty means "don't match anything" */ + return FAIL; + }; + + /* compile the regex, see if it works */ + re = pcre_compile(CS malware_regex, PCRE_COPT, (const char **)&rerror, &roffset, NULL); + if (re == NULL) { + log_write(0, LOG_MAIN|LOG_PANIC, + "malware acl condition: regular expression error in '%s': %s at offset %d", malware_regex, rerror, roffset); + return DEFER; + }; + + /* Do not scan twice. */ + if (malware_ok == 0) { + + /* find the scanner type from the av_scanner option */ + if ((scanner_name = string_nextinlist(&av_scanner_work, &sep, + scanner_name_buffer, + sizeof(scanner_name_buffer))) == NULL) { + /* no scanner given */ + log_write(0, LOG_MAIN|LOG_PANIC, + "malware acl condition: av_scanner configuration variable is empty"); + return DEFER; + }; + + /* "drweb" scanner type ----------------------------------------------- */ + /* v0.1 - added support for tcp sockets */ + /* v0.0 - initial release -- support for unix sockets */ + if (strcmpic(scanner_name,US"drweb") == 0) { + uschar *drweb_options; + uschar drweb_options_buffer[1024]; + uschar drweb_options_default[] = "/usr/local/drweb/run/drwebd.sock"; + struct sockaddr_un server; + int sock, port, result, ovector[30]; + unsigned int fsize; + uschar tmpbuf[1024], *drweb_fbuf; + uschar scanrequest[1024]; + uschar drweb_match_string[128]; + int drweb_rc, drweb_cmd, drweb_flags = 0x0000, drweb_fd, + drweb_vnum, drweb_slen, drweb_fin = 0x0000; + unsigned long bread; + uschar hostname[256]; + struct hostent *he; + struct in_addr in; + pcre *drweb_re; + + if ((drweb_options = string_nextinlist(&av_scanner_work, &sep, + drweb_options_buffer, sizeof(drweb_options_buffer))) == NULL) { + /* no options supplied, use default options */ + drweb_options = drweb_options_default; + }; + + if (*drweb_options != '/') { + + /* extract host and port part */ + if( sscanf(CS drweb_options, "%s %u", hostname, &port) != 2 ) { + log_write(0, LOG_MAIN|LOG_PANIC, + "malware acl condition: drweb: invalid socket '%s'", drweb_options); + return DEFER; + } + + /* Lookup the host */ + if((he = gethostbyname(CS hostname)) == 0) { + log_write(0, LOG_MAIN|LOG_PANIC, + "malware acl condition: drweb: failed to lookup host '%s'", hostname); + return DEFER; + } + + in = *(struct in_addr *) he->h_addr_list[0]; + + /* Open the drwebd TCP socket */ + if ( (sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) { + log_write(0, LOG_MAIN|LOG_PANIC, + "malware acl condition: drweb: unable to acquire socket (%s)", + strerror(errno)); + return DEFER; + } + + if (ip_connect(sock, AF_INET, (uschar*)inet_ntoa(in), port, 5) < 0) { + close(sock); + log_write(0, LOG_MAIN|LOG_PANIC, + "malware acl condition: drweb: connection to %s, port %u failed (%s)", + inet_ntoa(in), port, strerror(errno)); + return DEFER; + } + + /* prepare variables */ + drweb_cmd = htonl(DRWEBD_SCAN_CMD); + drweb_flags = htonl(DRWEBD_RETURN_VIRUSES); + snprintf(CS scanrequest, 1024,CS"%s/scan/%s/%s.eml", + spool_directory, message_id, message_id); + + /* calc file size */ + drweb_fd = open(scanrequest, O_RDONLY); + if (drweb_fd == -1) { + log_write(0, LOG_MAIN|LOG_PANIC, + "malware acl condition: drweb: can't open spool file %s: %s", + scanrequest, strerror(errno)); + return DEFER; + } + fsize = lseek(drweb_fd, 0, SEEK_END); + if (fsize == -1) { + log_write(0, LOG_MAIN|LOG_PANIC, + "malware acl condition: drweb: can't seek spool file %s: %s", + scanre