2 # -*- coding: iso-8859-1 -*-
5 # MailFilter - Mail filter to replace procmail.
6 # Copyright (C) 2004 Frédéric Jolliton <frederic@jolliton.com>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 # Policy when error are encountered:
26 # We backup the mail in a special directory. It will be
27 # at the admin discretion to feed it again to this program
28 # (or may be script that.)
34 # [ ] Define precisely what return code use for each possible case.
46 from os import EX_USAGE, EX_OK, EX_NOUSER, EX_TEMPFAIL, EX_DATAERR
48 # EX_USAGE 64 command line usage error
49 # EX_DATAERR 65 data format error
50 # EX_NOINPUT 66 cannot open input
51 # EX_NOUSER 67 addressee unknown
52 # EX_NOHOST 68 host name unknown
53 # EX_UNAVAILABLE 69 service unavailable
54 # EX_SOFTWARE 70 internal software error
55 # EX_OSERR 71 system error (e.g., can't fork)
56 # EX_OSFILE 72 critical OS file missing
57 # EX_CANTCREAT 73 can't create (user) output file
58 # EX_IOERR 74 input/output error
59 # EX_TEMPFAIL 75 temp failure; user is invited to retry
60 # EX_PROTOCOL 76 remote error in protocol
61 # EX_NOPERM 77 permission denied
62 # EX_CONFIG 78 configuration error
65 # Path to subprocess module. Ideally not needed if subprocess
66 # (formerly popen5) is installed into /site-packages/ directory.
68 #sys.path.insert( 0 , '/usr/local/lib/python/' )
71 # subprocess (formerly popen5) - See PEP 324
72 # http://www.lysator.liu.se/~astrand/popen5/
74 # >>> cat = subprocess.Popen( 'cat' , stdin = subprocess.PIPE , stdout = subprocess.PIPE )
75 # >>> cat.communicate( 'bla' )
84 import popen5 as subprocess
86 print 'Please install subprocess module.'
87 print 'See http://www.lysator.liu.se/~astrand/popen5/.'
90 #--[ Configuration variables ]------------------------------------------------
93 # Filename where to put log.
95 g_pathLog = '/var/log/mail.filter.log'
98 # For which users receiving a mail should we send a UDP packet.
100 g_userNotificationFilter = [ 'fred' ]
103 # For which IP address should we send the notification.
104 # (Can include broadcast address.)
106 g_notificationAddresses = [ '192.168.1.255' ]
109 # On which port should we send the notification.
111 g_notificationPort = 23978
114 # Max mail size to be processed by this script.
116 # Larger mail are just not filtered.
118 g_maxMailSize = 2 * 1024 * 1024
121 # Where to save copy of mail in case of error.
123 g_directoryBackup = '/var/mail.filter/recovery/'
126 # Where to find rules about each user.
128 # Filename for user 'joe' will be named 'joe.mf' in that
129 # directory. If the file doesn't exist, no filtering is
130 # done (not even spam/virus filtering.)
132 g_directoryRules = '/var/mail.filter/rules/'
134 #--[ External commands ]------------------------------------------------------
137 # Path to Cyrus's deliver binary.
139 g_pathCyrusDeliver = '/usr/cyrus/bin/deliver'
142 # Path to spamprobe binary.
144 g_pathSpamProbe = '/usr/bin/spamprobe'
146 g_pathSpamProbeDb = '/var/spamprobe/db'
149 # Path to ClamAV binary.
151 # Could point either to 'clamdscan' or 'clamscan'.
153 # The first one is *HIGHLY* recommended since
154 # it will use the ClamAV daemon.
156 g_pathClamdscan = '/usr/bin/clamdscan'
158 #--[ Global variables ]-------------------------------------------------------
161 # Should the log be also printed on stdout ?
163 g_copyLogToStdout = False
166 # Don't actually feed the mail to Cyrus.
171 # The user name of the recipient.
176 # The current mail as string (as read from stdin.)
181 # The current mail as email.Message.Message object.
185 #-----------------------------------------------------------------------------
188 # check if predicate is True for all items in list 'lst'.
190 def all( lst , predicate ) :
193 if not predicate( item ) :
198 # check if predicate is True for at least one item in list 'lst'.
200 def some( lst , predicate ) :
203 if predicate( item ) :
208 # Remove leading and trailing blank, and replace any
209 # blank character sequence by one space character.
211 def normalizeBlank( s ) :
213 return ' '.join( s.split() )
215 #-----------------------------------------------------------------------------
218 # Utility function to return traceback as string from most recent
223 import traceback, sys
224 return ''.join( traceback.format_exception( *sys.exc_info() ) )
227 # Return (returnCode, stdout, stderr)
229 def pipe( cmd , input ) :
231 p = subprocess.Popen( cmd ,
232 stdin = subprocess.PIPE ,
233 stdout = subprocess.PIPE ,
234 stderr = subprocess.PIPE )
236 # much faster than passing 'input' to communicate directly..
237 p.stdin.write( input )
241 return p.returncode , r[ 0 ] , r[ 1 ]
244 # Return an ISO-8661 date representation for the UTC
247 # timestamp( 0 ) => 1970-01-01T00:00:00Z
252 return '%04d-%02d-%02dT%02d:%02d:%02dZ' % t[ : 6 ]
257 def logMessage( msg ) :
259 if not logMessage.logFile and not g_testMode :
261 # If log file is not yet open, try to open it.
264 logMessage.logFile = open( g_pathLog , 'a+' )
266 if not g_copyLogToStdout :
269 msg = msg.splitlines()
270 prefix = timestamp() + ' [%s] ' % os.getpid()
273 # Output to log file.
275 if logMessage.logFile :
280 logMessage.logFile.write( line + '\n' )
281 logMessage.logFile.flush()
286 # Output to standard output.
288 if g_copyLogToStdout :
292 sys.stdout.write( line + '\n' )
295 logMessage.logFile = None
298 # Make a backup of the mail (in case it's impossible
299 # to store the mail to Cyrus.)
301 def backup( filenamePrefix = None ) :
304 logMessage( 'TEST MODE: Backup of the mail requested.' )
308 # Ensure directory exist
310 os.makedirs( g_directoryBackup )
316 basename += filenamePrefix + '-'
318 # Append current unix time as suffix
320 basename += '%.3f' % time.time()
322 fn = g_directoryBackup + '/' + basename
324 f = open( fn , 'a+' )
325 f.write( g_mailText )
328 logMessage( 'PANIC: Unable to write backup to %s.' % fn )
330 logMessage( 'Message appended to backup directory as `%s\'.' % basename )
332 #-----------------------------------------------------------------------------
336 class NullAction( Action ) :
338 def __repr__( self ) :
340 return '<NullAction>'
342 class FileToFolderAction( Action ) :
344 def __init__( self , folder ) :
348 def __repr__( self ) :
350 return '<FileToFolderAction %r>' % ( self.folder , )
352 class CustomErrorCodeAction( Action ) :
354 def __init__( self , code ) :
358 def __repr__( self ) :
360 return '<NullAction %r>' % ( self.code , )
362 #-----------------------------------------------------------------------------
367 # Packet payload contains:
369 # <username> + char( 0 ) + <foldername> + char( 0 )
371 def notifyDeliver( user , folder ) :
373 if user not in g_userNotificationFilter :
377 s = socket.socket( socket.AF_INET , socket.SOCK_DGRAM )
378 msg = user + chr( 0 ) + folder + chr( 0 )
379 for address in g_notificationAddresses :
380 s.sendto( msg , ( address , g_notificationPort ) )
385 # Deliver a mail to Cyrus for user 'username' in
386 # folder 'folderName' (or default folder if not
389 def deliverTo( username , folderName = None ) :
392 pseudoFolderName = 'INBOX'
393 folderName = 'user.' + username
395 pseudoFolderName = 'INBOX.' + folderName
396 folderName = 'user.' + username + '.' + folderName
399 # Build the command line for running deliver.
401 cmd = [ g_pathCyrusDeliver ]
402 cmd += [ '-a' , username ]
403 cmd += [ '-m' , folderName ]
406 logMessage( 'TEST MODE: Delivering mail in `%s\' requested.' % ( folderName , ) )
407 logMessage( 'TEST MODE: Command: %r.' % cmd )
411 rc , stdout , stderr = pipe( cmd , g_mailText )
413 logMessage( 'Error running `%s\': %s.' % ( cmd[ 0 ] , e[ 1 ] ) )
417 logMessage( 'Message delivered in folder `%s\'.' % folderName )
418 notifyDeliver( username , pseudoFolderName )
420 errorMessage = stdout.rstrip()
422 # Extract raw error message
426 # +user.fred: Message contains invalid header
428 m = errorMessage.split( ': ' , 1 )
434 if m == 'Message contains invalid header' :
437 elif m == 'Mailbox does not exist' :
441 # FIXME: DATAERR ok here ?
443 logMessage( 'Refused by Cyrus: [%s] `%s\'.' % ( rcMsg , errorMessage ) )
446 #--[ Antivirus ]--------------------------------------------------------------
449 # Return virus list from the output of ClamAV.
451 def extractVirusList( clamdOutput ) :
454 for line in clamdOutput.splitlines() :
455 r = extractVirusList.reClamdVirus.search( line.rstrip() )
456 if r == None : continue
457 res.append( r.group( 1 ) )
460 extractVirusList.reClamdVirus = re.compile( r'^[^:]+: (\S+) FOUND$' )
465 # Return True if mail is clean.
467 def antivirusScan() :
469 cmd = [ g_pathClamdscan , '-' ]
472 logMessage( 'TEST MODE: Virus scan requested.' )
473 logMessage( 'TEST MODE: Command: %r.' % cmd )
476 rc , stdout , stderr = pipe( cmd , g_mailText )
477 output = stderr or ''
478 #logMessage( 'clamdscan returned %s' % rc )
480 raise 'Unable to scan for viruses (%s)' % cmd
484 viruses = extractVirusList( output )
486 msg += ' [%s]' % ' '.join( viruses )
490 #--[ Antispam ]---------------------------------------------------------------
495 # Return True if mail is correct.
499 if not g_user : return True
501 cmd = [ g_pathSpamProbe ]
502 cmd += [ '-d' , g_pathSpamProbeDb + '/' + g_user + '/' ]
506 logMessage( 'TEST MODE: Spam scan requested.' )
507 logMessage( 'TEST MODE: Command: %r.' % cmd )
510 rc , stdout , stderr = pipe( cmd , g_mailText )
511 r = ( stdout or '' ).split()
512 return r[ 0 ] != 'SPAM'
514 #-----------------------------------------------------------------------------
516 def errorNameToErrorCode( code ) :
519 if code == 'nouser' :
521 elif code == 'tempfail' :
523 elif code == 'dataerr' :
531 #-----------------------------------------------------------------------------
534 # FIXME: I think it could be better to cache the parsed
535 # configuration, and also to cache the result of the validator
536 # so that we don't run the test each time this script is run !
538 def readUserRules( user ) :
540 filename = g_directoryRules + '/' + user + '.mf'
543 # Read the configuration.
546 return confparser.readConfiguration( filename )
549 except Exception , e :
550 logMessage( 'Error in file %r. See option -c to check this file.' % ( filename , ) )
552 #-----------------------------------------------------------------------------
555 # Test a match rule against a particular header.
557 def ruleMatch( header , matchType , text ) :
559 if matchType == 'match' :
561 return re.search( text , header , re.I ) != None
563 logMessage( 'Error with regex `%s\' from %s\'s user configuration.' % ( text , g_user ) )
565 elif matchType == 'is' :
566 return header.strip().lower() == text.strip().lower()
567 elif matchType == 'contains' :
568 return header.lower().find( text.strip().lower() ) != -1
570 logMessage( 'Unknown match type `%s\' from %s\'s user configuration.' % ( matchType , g_user ) )
574 # Test rule 'rule' against the mail.
576 def testRule( rule ) :
581 return all( rule[ 2 ] , testRule )
584 return some( rule[ 2 ] , testRule )
587 return not some( rule[ 2 ] , testRule )
596 headerName , matchType , text = args
597 headers = map( normalizeBlank , g_mail.get_all( headerName ) or [] )
598 return some( headers , lambda header : ruleMatch( header , matchType , text ) )
604 return g_mail == None
609 if cmd == 'infected' :
610 return g_mail != None and not antivirusScan()
616 return g_mail != None and not spamScan()
621 logMessage( 'Unknown rule name `%s\'.' % ( cmd , ) )
624 #-----------------------------------------------------------------------------
627 # Find the destination folder for user 'user' according to rules defined for
628 # him/her against the current mail.
632 def checkUserRules( user ) :
634 action = FileToFolderAction( None )
636 conf = readUserRules( user )
640 logMessage( 'No rules defined for user `%s\'.' % user )
644 logMessage( 'Empty rules set or syntax error encountered for user `%s\'.' % user )
648 for item in conf[ 2 ] :
649 actionName , args , subs = item[ : 3 ]
651 if some( subs , testRule ) :
652 if actionName == 'folder' :
653 action = FileToFolderAction( args[ 0 ] )
654 elif actionName == 'reject' :
655 action = CustomErrorCodeAction( errorNameToErrorCode( args[ 0 ] ) )
657 logMessage( 'Unknown action `%s\'.' % actionName )
662 #-----------------------------------------------------------------------------
665 # Read mail from standard input.
672 # FIXME: Should we be reading the mail by block, so that
673 # we can at least read and backup a part of the standard input
674 # in case an error occur ? (broken pipe for example)
676 # If error occur, and since we can't backup the mail,
677 # we ask sendmail to retry later.
680 g_mailText = sys.stdin.read()
682 logMessage( getTraceBack() )
683 sys.exit( EX_TEMPFAIL )
686 # Check if the mail is bigger than a predefined amount.
688 def checkForLargeMail() :
690 if len( g_mailText ) > g_maxMailSize :
691 logMessage( 'Message too big (%s bytes). Not filtering it.' % len( g_mailText ) )
694 rc = deliverTo( g_user )
696 logMessage( 'Unable to deliver it to user `%s\'.' % g_user )
702 # Check if user was specified of command line.
710 logMessage( 'No user specified.' )
715 # Parse the mail using email python standard module.
725 g_mail = email.message_from_string( g_mailText , strict = False )
727 logMessage( getTraceBack() )
730 # Dispatch the mail to the correct folder (deduced from rules.)
732 # Return an error code.
736 action = checkUserRules( g_user )
739 # If custom error code is returned, stop processing
740 # here (mail is not saved.)
742 if isinstance( action , CustomErrorCodeAction ) :
743 logMessage( 'Custom exit code is %d.' % r.code )
747 # File the mail into the specified folder.
749 if isinstance( action , FileToFolderAction ) :
751 # We got a folder name (or None for default folder.)
753 folder = action.folder
755 if folder : pseudoName += '.' + folder
758 # Try to deliver in the named folder
760 rc = deliverTo( g_user , folder )
762 # If we get an error code, then we deliver the mail to default folder,
763 # except if the error was "data error" or if we already tried to deliver
764 # it to default folder.
766 if rc not in [ EX_OK , EX_DATAERR ] and folder != None :
767 logMessage( 'Error delivering to folder %s of user `%s\'.' % ( pseudoName , g_user ) )
768 logMessage( 'Mail will go into default folder.' )
769 rc = deliverTo( g_user )
773 # Here we also handle the case of EX_DATAERR not handled above.
776 logMessage( 'Error delivering to default folder of user `%s\'.' % ( g_user , ) )
778 # Since it's still not ok, backup the mail.
782 # All errors code different from "data error" are translated to
783 # "no user" error code.
787 if rc != EX_DATAERR :
797 raise Exception( 'Unknown action type' )
809 return dispatchMail()
811 logMessage( getTraceBack() )
814 #-----------------------------------------------------------------------------
816 def checkConfiguration( filename ) :
819 confparser.readConfiguration( filename )
820 except Exception , e :
823 #-----------------------------------------------------------------------------
827 print '''Usage: mail.filter [OPTIONS] username < EMAIL
829 -h, --help Print this help.
830 -v, --verbose Verbose mode, output log to stdout.
831 -t, --test Test mode. Don't feed the mail to Cyrus, don't
832 do backup, and don't write anything into log file.
833 -l, --log=FILENAME Set log filename.
834 -r, --rules=DIRECTORY Directory where are located users rules.
835 -c, --check-config=FILENAME
836 Check syntax and structure of configuration file
840 print 'Current paths are:\n'
841 print ' spamprobe : %s' % g_pathSpamProbe
842 print ' clamd : %s' % g_pathClamdscan
843 print ' deliver : %s' % g_pathCyrusDeliver
844 print ' log : %s' % g_pathLog
846 print 'Current directories are:\n'
847 print ' spamprobedb: %s' % g_pathSpamProbeDb
848 print ' rules : %s' % g_directoryRules
850 Latest version is available from:
852 arch://arch.intra.tuxee.net/2004/mail-filter
854 Report bugs to <fj@tuxee.net>.'''
858 global g_user, g_mail, g_mailText, g_copyLogToStdout, g_pathLog, g_directoryRules , g_testMode
860 #--[ Command line ]-------------------------------------------------------
864 _getopt = getopt.gnu_getopt
866 _getopt = getopt.getopt
869 options , parameters = \
870 _getopt( sys.argv[ 1 : ] ,
872 ( 'help' , 'verbose' , 'test' , 'log=' , 'rules=' , 'check-config=' ) )
873 except getopt.GetoptError , e :
874 myName = sys.argv[ 0 ].split( '/' )[ -1 ]
875 print '%s: %s' % ( myName , e[ 0 ] )
876 print 'Try `%s --help\' for more information.' % myName
879 for option , argument in options :
880 if option in [ '-h' , '--help' ] :
883 elif option in [ '-v' , '--verbose' ] :
884 g_copyLogToStdout = True
885 elif option in [ '-t' , '--test' ] :
887 elif option in [ '-l' , '--log' ] :
889 elif option in [ '-r' , '--rules' ] :
890 g_directoryRules = argument
891 elif option in [ '-c' , '--check-config' ] :
892 checkConfiguration( argument )
896 # At most one parameter expected.
898 if len( parameters ) > 1 :
900 # We just log a error message. We continue to proceed
901 # to not lost the mail !
903 logMessage( 'Warning: Expected only one user name.' )
906 g_user = parameters[ 0 ]
908 #--[ Core ]---------------------------------------------------------------
910 logMessage( 'Running mail.filter for user `%s\'.' % g_user )
914 if __name__ == '__main__' :