Ejemplo n.º 1
0
 def _detect_site(self, domain):
     """Detect our site using the netlogon protocol."""
     self.m_logger.debug("detecting site")
     query = "_ldap._tcp.%s" % domain.lower()
     answer = self._dns_query(query, "SRV")
     servers = self._order_dns_srv(answer)
     addresses = self._extract_addresses_from_srv(servers)
     replies = []
     netlogon = NetlogonClient()
     for i in range(0, len(addresses), 3):
         for addr in addresses[i : i + 3]:
             self.m_logger.debug("NetLogon query to %s" % addr[0])
             netlogon.query(addr, domain)
         replies += netlogon.call()
         self.m_logger.debug("%d replies" % len(replies))
         if replies >= 3:
             break
     if not replies:
         self.m_logger.error("could not detect site")
         return
     sites = {}
     for reply in replies:
         try:
             sites[reply.client_site] += 1
         except KeyError:
             sites[reply.client_site] = 1
     sites = [(value, key) for key, value in sites.items()]
     sites.sort()
     self.m_logger.debug("site detected as %s" % sites[-1][1])
     return sites[0][1]
Ejemplo n.º 2
0
 def _detect_site(self, domain):
     """Detect our site using the netlogon protocol."""
     self.m_logger.debug('detecting site')
     query = '_ldap._tcp.%s' % domain.lower()
     answer = self._dns_query(query, 'SRV')
     servers = self._order_dns_srv(answer)
     addresses = self._extract_addresses_from_srv(servers)
     replies = []
     netlogon = NetlogonClient()
     for i in range(0, len(addresses), 3):
         for addr in addresses[i:i + 3]:
             self.m_logger.debug('NetLogon query to %s' % addr[0])
             netlogon.query(addr, domain)
         replies += netlogon.call()
         self.m_logger.debug('%d replies' % len(replies))
         if replies >= 3:
             break
     if not replies:
         self.m_logger.error('could not detect site')
         return
     sites = {}
     for reply in replies:
         try:
             sites[reply.client_site] += 1
         except KeyError:
             sites[reply.client_site] = 1
     sites = [(value, key) for key, value in sites.items()]
     sites.sort()
     self.m_logger.debug('site detected as %s' % sites[-1][1])
     return sites[0][1]
Ejemplo n.º 3
0
 def locate_many_ex(self, domain, role=None, maxservers=None):
     """Like locate_many(), but returns a list of netlogon.Reply objects
     instead."""
     if role is None:
         role = 'dc'
     if maxservers is None:
         maxservers = self._maxservers
     if role not in ('dc', 'gc', 'pdc'):
         raise ValueError, 'Role should be one of "dc", "gc" or "pdc".'
     if role == 'pdc':
         maxservers = 1
     domain = domain.upper()
     self.m_logger.debug('locating domain controllers for %s (role %s)' %
                         (domain, role))
     key = (domain, role)
     if key in self.m_cache:
         stamp, nrequested, servers = self.m_cache[key]
         now = time.time()
         if now - stamp < self._timeout and nrequested >= maxservers:
             self.m_logger.debug('domain controllers found in cache')
             return servers
     self.m_logger.debug(
         'domain controllers not in cache, going to network')
     servers = []
     candidates = []
     if self.m_site is None and not self.m_site_detected:
         self.m_site = self._detect_site(domain)
         self.m_site_detected = True
     if self.m_site and role != 'pdc':
         query = '_ldap._tcp.%s._sites.%s._msdcs.%s' % \
                 (self.m_site, role, domain.lower())
         answer = self._dns_query(query, 'SRV')
         candidates += self._order_dns_srv(answer)
     query = '_ldap._tcp.%s._msdcs.%s' % (role, domain.lower())
     answer = self._dns_query(query, 'SRV')
     candidates += self._order_dns_srv(answer)
     addresses = self._extract_addresses_from_srv(candidates)
     addresses = self._remove_duplicates(addresses)
     replies = []
     netlogon = NetlogonClient()
     for i in range(0, len(addresses), maxservers):
         for addr in addresses[i:i + maxservers]:
             addr = (addr[0], LDAP_PORT)  # in case we queried for GC
             netlogon.query(addr, domain)
         replies += netlogon.call()
         if self._sufficient_domain_controllers(replies, role, maxservers):
             break
     servers = self._select_domain_controllers(replies, role, maxservers,
                                               addresses)
     self.m_logger.debug('found %d domain controllers' % len(servers))
     now = time.time()
     self.m_cache[key] = (now, maxservers, servers)
     return servers
Ejemplo n.º 4
0
 def locate_many(self, domain, role=None, maxservers=None):
     """Locate a list of up to `maxservers' of domain controllers."""
     if role is None:
         role = "dc"
     if maxservers is None:
         maxservers = self._maxservers
     if role not in ("dc", "gc", "pdc"):
         raise ValueError, 'Role should be one of "dc", "gc" or "pdc".'
     if role == "pdc":
         maxservers = 1
     domain = domain.upper()
     self.m_logger.debug("locating domain controllers for %s (role %s)" % (domain, role))
     key = (domain, role)
     if key in self.m_cache:
         stamp, nrequested, servers = self.m_cache[key]
         now = time.time()
         if now - stamp < self._timeout and nrequested >= maxservers:
             self.m_logger.debug("domain controllers found in cache")
             return servers
     self.m_logger.debug("domain controllers not in cache, going to network")
     servers = []
     candidates = []
     if self.m_site is None and not self.m_site_detected:
         self.m_site = self._detect_site(domain)
         self.m_site_detected = True
     if self.m_site and role != "pdc":
         query = "_ldap._tcp.%s._sites.%s._msdcs.%s" % (self.m_site, role, domain.lower())
         answer = self._dns_query(query, "SRV")
         candidates += self._order_dns_srv(answer)
     query = "_ldap._tcp.%s._msdcs.%s" % (role, domain.lower())
     answer = self._dns_query(query, "SRV")
     candidates += self._order_dns_srv(answer)
     addresses = self._extract_addresses_from_srv(candidates)
     addresses = self._remove_duplicates(addresses)
     replies = []
     netlogon = NetlogonClient()
     for i in range(0, len(addresses), maxservers):
         for addr in addresses[i : i + maxservers]:
             addr = (addr[0], LDAP_PORT)  # in case we queried for GC
             netlogon.query(addr, domain)
         replies += netlogon.call()
         if self._sufficient_domain_controllers(replies, role, maxservers):
             break
     result = self._select_domain_controllers(replies, role, maxservers, addresses)
     servers = self._extract_addresses_from_netlogon(result)
     self.m_logger.debug("found %d domain controllers" % len(servers))
     now = time.time()
     self.m_cache[key] = (now, maxservers, servers)
     return servers