#!/usr/bin/python
#
# A quick hack to filter the ILOVEYOU worm with qmail. Use:
#
# $ cp qmail-filter.py /var/qmail/bin
# $ cd /var/qmail/bin
# $ chmod +x qmail-filter.py
# $ mv qmail-queue qmail-queue-real; ln -s qmail-filter.py qmail-queue
#
# Test:
#
# $ echo "test 1" | mail -s okay myself
# $ echo "test 2" | mail -s ILOVEYOU myself
#
# You might have to modify the Python path at the top. This is a
# temporary fix. Remove it after the dust settles:
#
# $ cd /var/qmail/bin
# $ mv qmail-queue-real qmail-queue
#
# Neil Schemenauer <[EMAIL PROTECTED]>
PATTERN = r"^Subject: ILOVEYOU\s*$"
QMAIL_QUEUE = "/var/qmail/bin/qmail-queue-real"
import re
import string
import sys
import os
import tempfile
def mktemp():
for i in range(10):
tmp = tempfile.mktemp()
try:
fd = os.open(tmp, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
except OSError:
continue
file = os.fdopen(fd, "w+b")
os.unlink(tmp)
return file
return None
try:
mess = mktemp()
if not mess:
os._exit(53) # write error
header = 1
while 1:
line = sys.stdin.readline()
if not line:
break
if line in ("\r\n", "\n"):
header = 0
if header and re.search(PATTERN, line):
os._exit(31) # blocked, permanent error
mess.write(line)
mess.flush()
mess.seek(0)
os.dup2(mess.fileno(), 0)
os.execv(QMAIL_QUEUE, ())
except:
os._exit(81) # internal error