def is_dn_within_base_dn_scope(base_dn, dn): """ check to see if provided DN is within the base DN scope :param base_dn: str :param dn: str :return: bool """ split_base_dn = ldap.explode_dn(base_dn.lower()) split_dn = ldap.explode_dn(dn.lower()) if split_base_dn == split_dn[-len(split_base_dn):]: return True return False
def _add_s(self, dn, record): self._check_valid_dn(dn) entry = {} dn = str(dn) for item in record: entry[item[0]] = list(item[1]) try: self.directory[dn] raise ldap.ALREADY_EXISTS except KeyError: self.directory[dn.lower()] = entry return (105, [], len(self.methods_called()), [])
def _search_s(self, base, scope, filterstr, attrlist, attrsonly): from .filter import parse, UnsupportedOp self._check_valid_dn(base) if base not in self.directory: raise ldap.NO_SUCH_OBJECT # Find directory entries within the requested scope base_parts = ldap.dn.explode_dn(base.lower()) base_len = len(base_parts) dn_parts = {dn: ldap.dn.explode_dn(dn.lower()) for dn in self.directory.keys()} if scope == ldap.SCOPE_BASE: dns = (dn for dn, parts in dn_parts.items() if parts == base_parts) elif scope == ldap.SCOPE_ONELEVEL: dns = (dn for dn, parts in dn_parts.items() if parts[1:] == base_parts) elif scope == ldap.SCOPE_SUBTREE: dns = (dn for dn, parts in dn_parts.items() if parts[-base_len:] == base_parts) else: raise ValueError(u"Unrecognized scope: {0}".format(scope)) # Apply the filter expression try: filter_expr = parse(filterstr) except UnsupportedOp as e: raise SeedRequired(e) results = ((dn, self.directory[dn]) for dn in dns if filter_expr.matches(dn, self.directory[dn])) # Apply attribute filtering, if any if attrlist is not None: results = ((dn, {attr: values for attr, values in attrs.items() if attr in attrlist}) for dn, attrs in results) if attrsonly: results = ((dn, {attr: [] for attr in attrs.keys()}) for dn, attrs in results) results_list = list(results) for result in results_list: if '_referral' in result[1]: referral_info = { 'info': 'Referral:\n' + result[1]['_referral'], 'desc': 'Referral' } raise ldap.REFERRAL(referral_info) return results_list
def _add_s(self, dn, record): self._check_valid_dn(dn) entry = {} dn = str(dn) for item in record: if any(not isinstance(_, bytes) for _ in item[1]): raise TypeError("expected a byte string in the list") entry[item[0]] = list(item[1]) try: self.directory[dn] raise ldap.ALREADY_EXISTS except KeyError: self.directory[dn.lower()] = entry return (105, [], len(self.methods_called()), [])