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 # If set, then no backup are produced in case of error.
128 g_backupDisabled = False
131 # Where to find rules about each user.
133 # Filename for user 'joe' will be named 'joe.mf' in that
134 # directory. If the file doesn't exist, no filtering is
135 # done (not even spam/virus filtering.)
137 g_directoryRules = '/var/mail.filter/rules/'
139 #--[ External commands ]------------------------------------------------------
142 # Path to Cyrus's deliver binary.
144 g_pathCyrusDeliver = '/usr/lib/cyrus/deliver'
147 # Path to spamprobe binary.
149 g_pathSpamProbe = '/usr/bin/spamprobe'
151 g_pathSpamProbeDb = '/var/spamprobe/db'
154 # Path to ClamAV binary.
156 # Could point either to 'clamdscan' or 'clamscan'.
158 # The first one is *HIGHLY* recommended since
159 # it will use the ClamAV daemon.
161 g_pathClamdscan = '/usr/bin/clamdscan'
163 #--[ Global variables ]-------------------------------------------------------
166 # Should the log be also printed on stdout ?
168 g_copyLogToStdout = False
171 # Don't actually feed the mail to Cyrus.
176 # The user name of the recipient.
181 # The current mail as string (as read from stdin.)
186 # The current mail as email.Message.Message object.
190 #-----------------------------------------------------------------------------
193 # check if predicate is True for all items in list 'lst'.
195 def all( lst , predicate ) :
198 if not predicate( item ) :
203 # check if predicate is True for at least one item in list 'lst'.
205 def some( lst , predicate ) :
208 if predicate( item ) :
213 # Remove leading and trailing blank, and replace any
214 # blank character sequence by one space character.
216 def normalizeBlank( s ) :
218 return ' '.join( s.split() )
220 #-----------------------------------------------------------------------------
223 # Utility function to return traceback as string from most recent
228 import traceback, sys
229 return ''.join( traceback.format_exception( *sys.exc_info() ) )
232 # Return (returnCode, stdout, stderr)
234 def pipe( cmd , input ) :
236 p = subprocess.Popen( cmd ,
237 stdin = subprocess.PIPE ,
238 stdout = subprocess.PIPE ,
239 stderr = subprocess.PIPE )
241 # much faster than passing 'input' to communicate directly..
242 p.stdin.write( input )
246 return p.returncode , r[ 0 ] , r[ 1 ]
249 # Return an ISO-8661 date representation for the UTC
252 # timestamp( 0 ) => 1970-01-01T00:00:00Z
257 return '%04d-%02d-%02dT%02d:%02d:%02dZ' % t[ : 6 ]
262 def logMessage( msg ) :
264 if not logMessage.logFile and not g_testMode :
266 # If log file is not yet open, try to open it.
269 logMessage.logFile = open( g_pathLog , 'a+' )
271 if not g_copyLogToStdout :
274 msg = msg.splitlines()
275 prefix = timestamp() + ' [%s] ' % os.getpid()
278 # Output to log file.
280 if logMessage.logFile :
285 logMessage.logFile.write( line + '\n' )
286 logMessage.logFile.flush()
291 # Output to standard output.
293 if g_copyLogToStdout :
297 sys.stdout.write( line + '\n' )
300 logMessage.logFile = None
303 # Make a backup of the mail (in case it's impossible
304 # to store the mail to Cyrus.)
306 def backup( filenamePrefix = None ) :
309 logMessage( 'TEST MODE: Backup of the mail requested.' )
312 if g_backupDisabled :
313 logMessage( 'Backup requested, but disabled.' )
317 # Ensure directory exist
319 os.makedirs( g_directoryBackup )
325 basename += filenamePrefix + '-'
327 # Append current unix time as suffix
329 basename += '%.3f' % time.time()
331 fn = g_directoryBackup + '/' + basename
333 f = open( fn , 'a+' )
334 f.write( g_mailText )
337 logMessage( 'PANIC: Unable to write backup to %s.' % fn )
339 logMessage( 'Message appended to backup directory as `%s\'.' % basename )
341 #-----------------------------------------------------------------------------
345 class NullAction( Action ) :
347 def __repr__( self ) :
349 return '<NullAction>'
351 class FileToFolderAction( Action ) :
353 def __init__( self , folder ) :
357 def __repr__( self ) :
359 return '<FileToFolderAction %r>' % ( self.folder , )
361 class CustomErrorCodeAction( Action ) :
363 def __init__( self , code ) :
367 def __repr__( self ) :
369 return '<NullAction %r>' % ( self.code , )
371 #-----------------------------------------------------------------------------
376 # Packet payload contains:
378 # <username> + char( 0 ) + <foldername> + char( 0 )
380 def notifyDeliver( user , folder ) :
382 if user not in g_userNotificationFilter :
386 s = socket.socket( socket.AF_INET , socket.SOCK_DGRAM )
387 msg = user + chr( 0 ) + folder + chr( 0 )
388 for address in g_notificationAddresses :
389 s.sendto( msg , ( address , g_notificationPort ) )
394 # Deliver a mail to Cyrus for user 'username' in
395 # folder 'folderName' (or default folder if not
398 def deliverTo( username , folderName = None ) :
401 pseudoFolderName = 'INBOX'
402 folderName = 'user.' + username
404 pseudoFolderName = 'INBOX.' + folderName
405 folderName = 'user.' + username + '.' + folderName
408 # Build the command line for running deliver.
410 cmd = [ g_pathCyrusDeliver ]
411 cmd += [ '-a' , username ]
412 cmd += [ '-m' , folderName ]
415 logMessage( 'TEST MODE: Delivering mail in `%s\' requested.' % ( folderName , ) )
416 logMessage( 'TEST MODE: Command: %r.' % cmd )
420 rc , stdout , stderr = pipe( cmd , g_mailText )
422 logMessage( 'Error running `%s\': %s.' % ( cmd[ 0 ] , e[ 1 ] ) )
426 logMessage( 'Message delivered in folder `%s\'.' % folderName )
427 notifyDeliver( username , pseudoFolderName )
429 errorMessage = stdout.rstrip()
431 # Extract raw error message
435 # +user.fred: Message contains invalid header
437 m = errorMessage.split( ': ' , 1 )
443 if m == 'Message contains invalid header' :
446 elif m == 'Mailbox does not exist' :
450 # FIXME: DATAERR ok here ?
452 logMessage( 'Refused by Cyrus: [%s] `%s\'.' % ( rcMsg , errorMessage ) )
455 #--[ Antivirus ]--------------------------------------------------------------
458 # Return virus list from the output of ClamAV.
460 def extractVirusList( clamdOutput ) :
463 for line in clamdOutput.splitlines() :
464 r = extractVirusList.reClamdVirus.search( line.rstrip() )
465 if r == None : continue
466 res.append( r.group( 1 ) )
469 extractVirusList.reClamdVirus = re.compile( r'^[^:]+: (\S+) FOUND$' )
474 # Return True if mail is clean.
476 def antivirusScan() :
478 cmd = [ g_pathClamdscan , '-' ]
481 logMessage( 'TEST MODE: Virus scan requested.' )
482 logMessage( 'TEST MODE: Command: %r.' % cmd )
485 rc , stdout , stderr = pipe( cmd , g_mailText )
486 output = stderr or ''
487 #logMessage( 'clamdscan returned %s' % rc )
489 raise 'Unable to scan for viruses (%s)' % cmd
493 viruses = extractVirusList( output )
495 msg += ' [%s]' % ' '.join( viruses )
499 #--[ Antispam ]---------------------------------------------------------------
504 # Return True if mail is correct.
508 if not g_user : return True
510 cmd = [ g_pathSpamProbe ]
511 cmd += [ '-d' , g_pathSpamProbeDb + '/' + g_user + '/' ]
515 logMessage( 'TEST MODE: Spam scan requested.' )
516 logMessage( 'TEST MODE: Command: %r.' % cmd )
519 rc , stdout , stderr = pipe( cmd , g_mailText )
520 r = ( stdout or '' ).split()
521 return r[ 0 ] != 'SPAM'
523 #-----------------------------------------------------------------------------
525 def errorNameToErrorCode( code ) :
528 if code == 'nouser' :
530 elif code == 'tempfail' :
532 elif code == 'dataerr' :
540 #-----------------------------------------------------------------------------
543 # FIXME: I think it could be better to cache the parsed
544 # configuration, and also to cache the result of the validator
545 # so that we don't run the test each time this script is run !
547 def readUserRules( user ) :
549 filename = g_directoryRules + '/' + user + '.mf'
552 # Read the configuration.
555 return confparser.readConfiguration( filename )
558 except Exception , e :
559 logMessage( 'Error in file %r. See option -c to check this file.' % ( filename , ) )
561 #-----------------------------------------------------------------------------
564 # Test a match rule against a particular header.
566 def ruleMatch( header , matchType , text ) :
568 if matchType == 'match' :
570 return re.search( text , header , re.I ) != None
572 logMessage( 'Error with regex `%s\' from %s\'s user configuration.' % ( text , g_user ) )
574 elif matchType == 'is' :
575 return header.strip().lower() == text.strip().lower()
576 elif matchType == 'contains' :
577 return header.lower().find( text.strip().lower() ) != -1
579 logMessage( 'Unknown match type `%s\' from %s\'s user configuration.' % ( matchType , g_user ) )
583 # Test rule 'rule' against the mail.
585 def testRule( rule ) :
590 return all( rule[ 2 ] , testRule )
593 return some( rule[ 2 ] , testRule )
596 return not some( rule[ 2 ] , testRule )
605 headerName , matchType , text = args
606 headers = map( normalizeBlank , g_mail.get_all( headerName ) or [] )
607 return some( headers , lambda header : ruleMatch( header , matchType , text ) )
613 return g_mail == None
618 if cmd == 'infected' :
619 return g_mail != None and not antivirusScan()
625 return g_mail != None and not spamScan()
630 logMessage( 'Unknown rule name `%s\'.' % ( cmd , ) )
633 #-----------------------------------------------------------------------------
636 # Find the destination folder for user 'user' according to rules defined for
637 # him/her against the current mail.
641 def checkUserRules( user ) :
643 action = FileToFolderAction( None )
645 conf = readUserRules( user )
649 logMessage( 'No rules defined for user `%s\'.' % user )
653 logMessage( 'Empty rules set or syntax error encountered for user `%s\'.' % user )
657 for item in conf[ 2 ] :
658 actionName , args , subs = item[ : 3 ]
660 if some( subs , testRule ) :
661 if actionName == 'folder' :
662 action = FileToFolderAction( args[ 0 ] )
663 elif actionName == 'reject' :
664 action = CustomErrorCodeAction( errorNameToErrorCode( args[ 0 ] ) )
666 logMessage( 'Unknown action `%s\'.' % actionName )
671 #-----------------------------------------------------------------------------
674 # Read mail from standard input.
681 # FIXME: Should we be reading the mail by block, so that
682 # we can at least read and backup a part of the standard input
683 # in case an error occur ? (broken pipe for example)
685 # If error occur, and since we can't backup the mail,
686 # we ask sendmail to retry later.
689 g_mailText = sys.stdin.read()
691 logMessage( getTraceBack() )
692 sys.exit( EX_TEMPFAIL )
695 # Check if the mail is bigger than a predefined amount.
697 def checkForLargeMail() :
699 if len( g_mailText ) > g_maxMailSize :
700 logMessage( 'Message too big (%s bytes). Not filtering it.' % len( g_mailText ) )
703 rc = deliverTo( g_user )
705 logMessage( 'Unable to deliver it to user `%s\'.' % g_user )
711 # Check if user was specified of command line.
719 logMessage( 'No user specified.' )
724 # Parse the mail using email python standard module.
734 g_mail = email.message_from_string( g_mailText , strict = False )
736 logMessage( getTraceBack() )
739 # Dispatch the mail to the correct folder (deduced from rules.)
741 # Return an error code.
745 action = checkUserRules( g_user )
748 # If custom error code is returned, stop processing
749 # here (mail is not saved.)
751 if isinstance( action , CustomErrorCodeAction ) :
752 logMessage( 'Custom exit code is %d.' % r.code )
756 # File the mail into the specified folder.
758 if isinstance( action , FileToFolderAction ) :
760 # We got a folder name (or None for default folder.)
762 folder = action.folder
764 if folder : pseudoName += '.' + folder
767 # Try to deliver in the named folder
769 rc = deliverTo( g_user , folder )
771 # If we get an error code, then we deliver the mail to default folder,
772 # except if the error was "data error" or if we already tried to deliver
773 # it to default folder.
775 if rc not in [ EX_OK , EX_DATAERR ] and folder != None :
776 logMessage( 'Error delivering to folder %s of user `%s\'.' % ( pseudoName , g_user ) )
777 logMessage( 'Mail will go into default folder.' )
778 rc = deliverTo( g_user )
782 # Here we also handle the case of EX_DATAERR not handled above.
785 logMessage( 'Error delivering to default folder of user `%s\'.' % ( g_user , ) )
787 # Since it's still not ok, backup the mail.
791 # All errors code different from "data error" are translated to
792 # "no user" error code.
796 if rc != EX_DATAERR :
806 raise Exception( 'Unknown action type' )
818 return dispatchMail()
822 logMessage( getTraceBack() )
825 #-----------------------------------------------------------------------------
827 def checkConfiguration( filename ) :
830 confparser.readConfiguration( filename )
831 except Exception , e :
836 #-----------------------------------------------------------------------------
840 print '''Usage: mail.filter [OPTIONS] username < EMAIL
842 -h, --help Print this help.
843 -v, --verbose Verbose mode, output log to stdout.
844 -t, --test Test mode. Don't feed the mail to Cyrus, don't
845 do backup, and don't write anything into log file.
846 -l, --log=FILENAME Set log filename.
847 -r, --rules=DIRECTORY Directory where are located users rules.
848 -c, --check-config=FILENAME
849 Check syntax and structure of configuration file
851 --disable-backup Disable backup.
854 print 'Current paths are:\n'
855 print ' spamprobe : %s' % g_pathSpamProbe
856 print ' clamd : %s' % g_pathClamdscan
857 print ' deliver : %s' % g_pathCyrusDeliver
858 print ' log : %s' % g_pathLog
860 print 'Current directories are:\n'
861 print ' spamprobedb: %s' % g_pathSpamProbeDb
862 print ' rules : %s' % g_directoryRules
864 Latest version is available from:
866 arch://arch.intra.tuxee.net/2004/mail-filter
868 Report bugs to <fj@tuxee.net>.'''
872 global g_user, g_mail, g_mailText, g_copyLogToStdout, g_pathLog, g_directoryRules , g_testMode, g_backupDisabled
874 #--[ Command line ]-------------------------------------------------------
878 _getopt = getopt.gnu_getopt
880 _getopt = getopt.getopt
883 options , parameters = \
884 _getopt( sys.argv[ 1 : ] ,
886 ( 'help' , 'verbose' , 'test' , 'log=' , 'rules=' , 'check-config=' , 'disable-backup' ) )
887 except getopt.GetoptError , e :
888 myName = sys.argv[ 0 ].split( '/' )[ -1 ]
889 print '%s: %s' % ( myName , e[ 0 ] )
890 print 'Try `%s --help\' for more information.' % myName
893 for option , argument in options :
894 if option in [ '-h' , '--help' ] :
897 elif option in [ '-v' , '--verbose' ] :
898 g_copyLogToStdout = True
899 elif option in [ '-t' , '--test' ] :
901 elif option in [ '-l' , '--log' ] :
903 elif option in [ '-r' , '--rules' ] :
904 g_directoryRules = argument
905 elif option in [ '-c' , '--check-config' ] :
906 checkConfiguration( argument )
908 elif option in [ '--disable-backup' ] :
909 g_backupDisabled = True
912 # At most one parameter expected.
914 if len( parameters ) > 1 :
916 # We just log a error message. We continue to proceed
917 # to not lost the mail !
919 logMessage( 'Warning: Expected only one user name.' )
922 g_user = parameters[ 0 ]
924 #--[ Core ]---------------------------------------------------------------
926 logMessage( 'Running mail.filter for user `%s\'.' % g_user )
930 if __name__ == '__main__' :