Debian Codegraph @

dkim/dnsplug.py

raw symbols SCIP
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 <[email protected]>
21
22
23 __all__ = [
24 'get_txt'
25 ]
26
27
28 def get_txt_dnspython(name, timeout=5):
29 """Return a TXT record associated with a DNS name."""
30 import dkim
31 try:
32 a = dns.resolver.resolve(name, dns.rdatatype.TXT,raise_on_no_answer=False, lifetime=timeout, search=True)
33 for r in a.response.answer:
34 if r.rdtype == dns.rdatatype.TXT:
35 return b"".join(list(r.items)[0].strings)
36 except dns.resolver.NXDOMAIN: pass
37 except dns.resolver.NoNameservers: pass
38 except dns.resolver.NoResolverConfiguration as e:
39 raise dkim.DnsTimeoutError('dns.resolver.NoResolverConfiguration: {0}'.format(e))
40 except dns.exception.Timeout as e:
41 raise dkim.DnsTimeoutError('dns.exception.Timeout: {0}'.format(e))
42 return None
43
44
45 def get_txt_pydns(name, timeout=5):
46 """Return a TXT record associated with a DNS name."""
47 # Older pydns releases don't like a trailing dot.
48 if name.endswith('.'):
49 name = name[:-1]
50 response = DNS.DnsRequest(name, qtype='txt', timeout=timeout).req()
51 if not response.answers:
52 return None
53 for answer in response.answers:
54 if answer['typename'].lower() == 'txt':
55 return b''.join(answer['data'])
56 return None
57
58
59 # No longer used since it doesn't support timeout
60 def get_txt_Milter_dns(name, timeout=5):
61 """Return a TXT record associated with a DNS name."""
62 # Older pydns releases don't like a trailing dot.
63 if name.endswith('.'):
64 name = name[:-1]
65 sess = Session()
66 a = sess.dns(name.encode('idna'),'TXT')
67 if a: return b''.join(a[0])
68 return None
69
70
71 # Prefer dnspython if it's there, otherwise use pydns.
72 try:
73 import dns.resolver
74 _get_txt = get_txt_dnspython
75 except ImportError:
76 try:
77 import DNS
78 DNS.DiscoverNameServers()
79 _get_txt = get_txt_pydns
80 except:
81 raise
82
83
84 def get_txt(name, timeout=5):
85 """Return a TXT record associated with a DNS name.
86
87 @param name: The bytestring domain name to look up.
88 """
89 # pydns needs Unicode, but DKIM's d= is ASCII (already punycoded).
90 try:
91 unicode_name = name.decode('UTF-8')
92 except UnicodeDecodeError:
93 return None
94 txt = _get_txt(unicode_name, timeout)
95 if type(txt) is str:
96 txt = txt.encode('utf-8')
97 return txt