report.py :  » Email » Python-Milter » milter-0.8.13 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Email » Python Milter 
Python Milter » milter 0.8.13 » report.py
# Analyze milter log to find abusers
import traceback
import sys

def parse_addr(a):
  beg = a.find('<')
  end = a.find('>')
  if beg >= 0:
    if end > beg: return a[beg+1:end]
  return a

class Connection(object):
  def __init__(self,dt,tm,id,ip=None,conn=None):
    self.dt = dt
    self.tm = tm
    self.id = id
    if ip:
      _,self.host,self.ip = ip.split(None,2)
    elif conn:
      self.ip = conn.ip
      self.host = conn.host
      self.helo = conn.helo
    self.subject = None
    self.rcpt = []
    self.mfrom = None
    self.helo = None
    self.innoc = []
    self.whitelist = False

def connections(fp):
  conndict = {}
  termdict = {}
  for line in fp:
    if line.startswith('{'): continue
    a = line.split(None,4)
    if len(a) < 4: continue
    dt,tm,id,op = a[:4]
    if (id,op) == ('bms','milter'):
      # FIXME: optionally yield all partial connections in conndict
      conndict = {}
      termdict = {}
      continue
    if id[0] == '[' and id[-1] == ']':
      try:
  key = int(id[1:-1])
      except:
        print >>sys.stderr,'bad id:',line.rstrip()
  continue
    else: continue
    if op == 'connect':
      ip = a[4].rstrip()
      conn = Connection(dt,tm,id,ip=ip)
      conndict[key] = conn
    elif op in (
  'DISCARD:','TAG:','CBV:','Large','No',
  'NOTE:','From:','Sender:','TRAIN:'):
      continue
    else:
      op = op.lower()
      try:
  conn = conndict[key]
      except KeyError:
        try:
    conn = termdict[key]
    del termdict[key]
    conndict[key] = conn
  except KeyError:
    print >>sys.stderr,'key error:',line.rstrip()
    continue
      try:
  if op == 'subject:':
    if len(a) > 4:
      conn.subject = a[4].rstrip()
  elif op == 'innoc:':
    conn.innoc.append(a[4].rstrip())
  elif op == 'whitelist':
    conn.whitelist = True
  elif op == 'x-mailer:':
    if len(a) > 4:
      conn.mailer = a[4].rstrip()
        elif op == 'x-guessed-spf:':
          conn.spfguess = a[4]
  elif op == 'received-spf:':
    conn.spfres,conn.spfmsg = a[4].rstrip().split(None,1)
  elif op == 'received:':
    conn.received = a[4].rstrip()
  elif op == 'temp':
    _,conn.tempfile = a[4].rstrip().split(None,1)
  elif op == 'srs':
    _,conn.srsrcpt = a[4].rstrip().split(None,1)
  elif op == 'mail':
    _,conn.mfrom = a[4].rstrip().split(None,1)
  elif op == 'rcpt':
    _,rcpt = a[4].rstrip().split(None,1)
    conn.rcpt.append(rcpt)
  elif op == 'hello':
    _,conn.helo = a[4].rstrip().split(None,1)
  elif op in ('eom','dspam','abort'):
    del conndict[key]
    conn.enddt = dt
    conn.endtm = tm
    conn.result = op
    yield conn
    termdict[key] = Connection(conn.dt,conn.tm,conn.id,conn=conn)
  elif op in ('reject:','dspam:','tempfail:','reject','fail:','honeypot:'):
    del conndict[key]
    conn.enddt = dt
    conn.endtm = tm
    conn.result = op
    conn.resmsg = a[4].rstrip()
    yield conn
    termdict[key] = Connection(conn.dt,conn.tm,conn.id,conn=conn)
  elif op in ('fp:','spam:'):
    del conndict[key]
    termdict[key] = Connection(conn.dt,conn.tm,conn.id,conn=conn)
  else:
    print >>sys.stderr,'unknown op:',line.rstrip()
      except Exception:
  print >>sys.stderr,'error:',line.rstrip()
        traceback.print_exc()

if __name__ == '__main__':
  import gzip
  for fn in sys.argv[1:]:
    if fn.endswith('.gz'):
      fp = gzip.open(fn)
    else:
      fp = open(fn)
    for conn in connections(fp):
      if conn.rcpt and conn.mfrom:
        for r in conn.rcpt:
    if r.lower().find('iancarter') > 0: break
  else:
    if conn.mfrom.lower().find('iancarter') < 0: continue
  print >>sys.stderr,conn.result,conn.dt,conn.tm,conn.id,conn.subject,parse_addr(conn.mfrom),
  for a in conn.rcpt:
    print parse_addr(a),
  print
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.