2020-05-22 02:34:56 +00:00
|
|
|
from servicebase import ServiceBase
|
|
|
|
import socket
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceDelegate(ServiceBase) :
|
|
|
|
|
|
|
|
def get_arguments(cls) :
|
|
|
|
"""Returns an array of information used to construct an argumentparser argument."""
|
|
|
|
return [ '-vm', '--vm', 'store_true', "Return DNS resolution about the subject (dns)" ]
|
|
|
|
|
|
|
|
def startup(self) :
|
|
|
|
self.resolver = dns.resolver.Resolver()
|
2020-05-26 18:26:01 +00:00
|
|
|
if 'resolver' in self._config :
|
|
|
|
self.resolver.nameservers.append(self._config['resolver'])
|
2020-05-22 02:34:56 +00:00
|
|
|
|
|
|
|
def lookup(self,subject) :
|
|
|
|
|
|
|
|
# Note : hostname lookups always use system resolver.
|
|
|
|
try :
|
|
|
|
subjecttuple = socket.gethostbyname_ex(subject)
|
|
|
|
# Can't make an IP of the subject
|
|
|
|
except ValueError :
|
|
|
|
try :
|
|
|
|
# Lookup with system
|
|
|
|
subjecttuple = socket.gethostbyname_ex(subject)
|
|
|
|
except socket.gaierror :
|
|
|
|
# Not a valid host or IP
|
2020-05-26 18:26:01 +00:00
|
|
|
self._error.append("Unable to resolve " + subject)
|
2020-05-22 02:34:56 +00:00
|
|
|
subjecttuple = [subject,[],['']]
|
|
|
|
# Can't make IPv4 out of it
|
|
|
|
except socket.gaierror :
|
2020-05-26 18:26:01 +00:00
|
|
|
self._error.append("Unable to resolve " + subject)
|
2020-05-22 02:34:56 +00:00
|
|
|
subjecttuple = [subject,[],['']]
|
|
|
|
# Get additional records
|
|
|
|
return_dict = {}
|
|
|
|
mx = self.get_records(subjecttuple[0],'MX')
|
|
|
|
ns = self.get_records(subjecttuple[0],'NS')
|
|
|
|
txt = self.get_records(subjecttuple[0],'TXT')
|
|
|
|
mx.sort()
|
|
|
|
ns.sort()
|
|
|
|
txt.sort()
|
|
|
|
if mx and len(mx) > 0 :
|
|
|
|
return_dict.update({'mx':mx})
|
|
|
|
if ns and len(ns) > 0 :
|
|
|
|
return_dict.update({'ns':ns})
|
|
|
|
if txt and len(txt) > 0 :
|
|
|
|
return_dict.update({'txt':txt})
|
|
|
|
# Write final dictionary
|
|
|
|
if len(subjecttuple[2]) > 0 and subjecttuple[2][0] != '':
|
|
|
|
return_dict['addresses'] = list(subjecttuple[2])
|
|
|
|
if subjecttuple[0] and subjecttuple[0] != '' :
|
|
|
|
return_dict['name'] = subjecttuple[0]
|
|
|
|
if len(subjecttuple[1]) > 0 :
|
|
|
|
return_dict['aliases'] = list(subjecttuple[1])
|
|
|
|
return return_dict
|
|
|
|
|
|
|
|
|
|
|
|
def get_records(self,subject,recordtype) :
|
|
|
|
recs = []
|
|
|
|
try :
|
|
|
|
results = self.resolver.query(subject,recordtype)
|
|
|
|
for result in results :
|
|
|
|
recs.append(result.to_text())
|
|
|
|
return recs
|
|
|
|
except Exception as exception :
|
|
|
|
return recs
|