Package dkim :: Module dnsplug
[hide private]
[frames] | no frames]

Source Code for Module dkim.dnsplug

 1  # This software is provided 'as-is', without any express or implied 
 2  # warranty.  In no event will the author be held liable for any damages 
 3  # arising from the use of this software. 
 4  # 
 5  # Permission is granted to anyone to use this software for any purpose, 
 6  # including commercial applications, and to alter it and redistribute it 
 7  # freely, subject to the following restrictions: 
 8  # 
 9  # 1. The origin of this software must not be misrepresented; you must not 
10  #    claim that you wrote the original software. If you use this software 
11  #    in a product, an acknowledgment in the product documentation would be 
12  #    appreciated but is not required. 
13  # 2. Altered source versions must be plainly marked as such, and must not be 
14  #    misrepresented as being the original software. 
15  # 3. This notice may not be removed or altered from any source distribution. 
16  # 
17  # Copyright (c) 2008 Greg Hewgill http://hewgill.com 
18  # 
19  # This has been modified from the original software. 
20  # Copyright (c) 2011 William Grant <me@williamgrant.id.au> 
21   
22   
23  __all__ = [ 
24      'get_txt' 
25      ] 
26   
27   
28 -def get_txt_dnspython(name):
29 """Return a TXT record associated with a DNS name.""" 30 try: 31 a = dns.resolver.query(name, dns.rdatatype.TXT,raise_on_no_answer=False) 32 for r in a.response.answer: 33 if r.rdtype == dns.rdatatype.TXT: 34 return b"".join(r.items[0].strings) 35 except dns.resolver.NXDOMAIN: pass 36 return None
37 38
39 -def get_txt_pydns(name):
40 """Return a TXT record associated with a DNS name.""" 41 # Older pydns releases don't like a trailing dot. 42 if name.endswith('.'): 43 name = name[:-1] 44 response = DNS.DnsRequest(name, qtype='txt').req() 45 if not response.answers: 46 return None 47 return b''.join(response.answers[0]['data'])
48
49 -def get_txt_Milter_dns(name):
50 """Return a TXT record associated with a DNS name.""" 51 # Older pydns releases don't like a trailing dot. 52 if name.endswith('.'): 53 name = name[:-1] 54 sess = Session() 55 a = sess.dns(name,'TXT') 56 if a: return b''.join(a[0]) 57 return None
58 59 # Prefer dnspython if it's there, otherwise use pydns. 60 try: 61 import dns.resolver 62 _get_txt = get_txt_dnspython 63 except ImportError: 64 try: 65 from Milter.dns import Session 66 _get_txt = get_txt_Milter_dns 67 except ImportError: 68 import DNS 69 DNS.DiscoverNameServers() 70 _get_txt = get_txt_pydns 71
72 -def get_txt(name):
73 """Return a TXT record associated with a DNS name. 74 75 @param name: The bytestring domain name to look up. 76 """ 77 # pydns needs Unicode, but DKIM's d= is ASCII (already punycoded). 78 try: 79 unicode_name = name.decode('ascii') 80 except UnicodeDecodeError: 81 return None 82 txt = _get_txt(unicode_name) 83 if type(txt) is str: 84 txt = txt.encode('utf-8') 85 return txt
86