def wipe_address(self,*args): if len(args)!=1: print "usage: wipe-address <address>" sys.exit(1) config=get_config() cache=MySQLCache(config) rowcount=cache.wipe_address(args[0]) print "Wiped %s records"%rowcount
def unblacklist(self,*args): if len(args)<1: print "usage: unblacklist <relay or domain>" sys.exit(1) relay=args[0] config=get_config() cache=MySQLCache(config) count=cache.unblacklist(relay) print "%s entries removed from call-ahead blacklist"%count
def test_config(self,*args): logging.basicConfig(level=logging.INFO) if len(args)!=1: print "usage: test-config <address>" sys.exit(1) address=args[0] domain=extract_domain(address) config=get_config() domainconfig=MySQLConfigBackend(config).get_domain_config_all(domain) print "Checking address cache..." cache=MySQLCache(config) entry=cache.get_address(address) if entry is not None: (positive,message)=entry tp="negative" if positive: tp="positive" print "We have %s cache entry for %s: %s"%(tp,address,message) else: print "No cache entry for %s"%address test=SMTPTest(config) relays=test.get_relays(domain,domainconfig) # type: list if relays is None: print "No relay for domain %s found!"%domain sys.exit(1) print "Relays for domain %s are %s"%(domain,relays) for relay in relays: print "Testing relay %s"%relay if cache.is_blacklisted(domain, relay): print "%s is currently blacklisted for call-aheads"%relay else: print "%s not blacklisted for call-aheads"%relay print "Checking if server supports verification...." sender=test.get_domain_config(domain, 'sender', domainconfig, {'bounce':'','originalfrom':''}) testaddress=test.maketestaddress(domain) result=test.smtptest(relay,[address,testaddress],mailfrom=sender) if result.state!=SMTPTestResult.TEST_OK: print "There was a problem testing this server:" print result continue addrstate,code,msg=result.rcptoreplies[testaddress] if addrstate==SMTPTestResult.ADDRESS_OK: print "Server accepts any recipient" elif addrstate==SMTPTestResult.ADDRESS_TEMPFAIL: print "Temporary problem / greylisting detected" elif addrstate==SMTPTestResult.ADDRESS_DOES_NOT_EXIST: print "Server supports recipient verification" print result
def show_blacklist(self,*args): if len(args)>0: print "usage: show-blackist" sys.exit(1) config=get_config() cache=MySQLCache(config) rows=cache.get_blacklist() # type: list print "Call-ahead blacklist (domain/relay/reason):" for row in rows: domain,relay,reason,exp=row print "%s\t%s\t%s"%(domain,relay,reason) total=len(rows) print "Total %s blacklisted relays"%total
def devshell(self): """Drop into a python shell for debugging""" import readline import code logging.basicConfig(level=logging.DEBUG) cli=self from postomaat.shared import get_config config=get_config('../../../conf/postomaat.conf.dist', '../../../conf/conf.d') config.read('../../../conf/conf.d/call-ahead.conf.dist') sqlcache=MySQLCache(config) plugin=AddressCheck(config) print "cli : Command line interface class" print "sqlcache : SQL cache backend" print "plugin: AddressCheck Plugin" terp=code.InteractiveConsole(locals()) terp.interact("")
def show_domain(self,*args): if len(args)!=1: print "usage: show-domain <domain>" sys.exit(1) config=get_config() cache=MySQLCache(config) domain=args[0] rows=cache.get_all_addresses(domain) # type: list print "Cache for domain %s (-: negative entry, +: positive entry)"%domain for row in rows: email,positive=row if positive: print "+ ",email else: print "- ",email total=len(rows) print "Total %s cache entries for domain %s"%(total,domain)
def wipe_domain(self,*args): if len(args)<1: print "usage: wipe-domain <domain> [positive|negative|all (default)]" sys.exit(1) domain=args[0] pos=None strpos='' if len(args)>1: strpos=args[1].lower() assert strpos in ['positive','negative','all'],"Additional argument must be 'positive', 'negative' or 'all'" if strpos=='positive': pos=True elif strpos=='negative': pos=False else: pos=None strpos='' config=get_config() cache=MySQLCache(config) rowcount=cache.wipe_domain(domain,pos) print "Wiped %s %s records"%(rowcount,strpos)
def update(self,*args): logging.basicConfig(level=logging.INFO) if len(args)!=1: print "usage: update <address>" sys.exit(1) address=args[0] domain=extract_domain(address) config=get_config() domainconfig=MySQLConfigBackend(config).get_domain_config_all(domain) cache=MySQLCache(config) test=SMTPTest(config) relays=test.get_relays(domain,domainconfig) if relays is None: print "No relay for domain %s found!"%domain sys.exit(1) print "Relays for domain %s are %s"%(domain,relays) relay=relays[0] sender=test.get_domain_config(domain, 'sender', domainconfig, {'bounce':'','originalfrom':''}) testaddress=test.maketestaddress(domain) result=test.smtptest(relay,[address,testaddress],mailfrom=sender) servercachetime=test.get_domain_config(domain, 'test_server_interval', domainconfig) if result.state!=SMTPTestResult.TEST_OK: print "There was a problem testing this server:" print result print "putting server on blacklist" cache.blacklist(domain, relay, servercachetime, result.stage, result.errormessage) return DUNNO,None addrstate,code,msg=result.rcptoreplies[testaddress] recverificationsupport=None if addrstate==SMTPTestResult.ADDRESS_OK: recverificationsupport=False elif addrstate==SMTPTestResult.ADDRESS_TEMPFAIL: recverificationsupport=False elif addrstate==SMTPTestResult.ADDRESS_DOES_NOT_EXIST: recverificationsupport=True if recverificationsupport: if cache.is_blacklisted(domain, relay): print "Server was blacklisted - removing from blacklist" cache.unblacklist(relay) cache.unblacklist(domain) addrstate,code,msg=result.rcptoreplies[address] positive=True cachetime=test.get_domain_config(domain, 'positive_cache_time', domainconfig) if addrstate==SMTPTestResult.ADDRESS_DOES_NOT_EXIST: positive=False cachetime=test.get_domain_config(domain, 'negative_cache_time', domainconfig) cache.put_address(address,cachetime,positive,msg) neg="" if not positive: neg="negative" print "%s cached %s for %s seconds"%(neg,address,cachetime) else: print "Server accepts any recipient" if config.getboolean('AddressCheck','always_assume_rec_verification_support'): print "blacklistings disabled in config- not blacklisting" else: cache.blacklist(domain, relay, servercachetime, result.stage, 'accepts any recipient') print "Server blacklisted"
def cleanup(self,*args): config=get_config() (poscount,negcount,blcount)=MySQLCache(config).cleanup() if 'verbose' in args: print "Removed %s positive,%s negative records from history data"%(poscount,negcount) print "Removed %s expired relays from call-ahead blacklist"%blcount