def search(self, searchBase=None, scope=None, derefAliases=None, sizeLimit=0, timeLimit=0, typesOnly=False, searchFilter='(objectClass=*)', attributes=None, searchControls=None, perRecordCallback=None): if searchBase is None: searchBase = self._baseDN if scope is None: scope = Scope('wholeSubtree') if derefAliases is None: derefAliases = DerefAliases('neverDerefAliases') if attributes is None: attributes = [] searchRequest = SearchRequest() searchRequest['baseObject'] = searchBase searchRequest['scope'] = scope searchRequest['derefAliases'] = derefAliases searchRequest['sizeLimit'] = sizeLimit searchRequest['timeLimit'] = timeLimit searchRequest['typesOnly'] = typesOnly searchRequest['filter'] = self._parseFilter(searchFilter) searchRequest['attributes'].setComponents(*attributes) done = False answers = [] # We keep asking records until we get a SearchResultDone packet and all controls are handled while not done: response = self.sendReceive(searchRequest, searchControls) for message in response: searchResult = message['protocolOp'].getComponent() if searchResult.isSameTypeWith(SearchResultDone()): if searchResult['resultCode'] == ResultCode('success'): done = self._handleControls(searchControls, message['controls']) else: raise LDAPSearchError( error=int(searchResult['resultCode']), errorString='Error in searchRequest -> %s: %s' % (searchResult['resultCode'].prettyPrint(), searchResult['diagnosticMessage']), answers=answers) else: if perRecordCallback is None: answers.append(searchResult) else: perRecordCallback(searchResult) return answers
def search(self, searchBase=None, searchFilter=u'(objectClass=*)', scope=SCOPE_SUB, attributes=None, derefAliases=DEREF_NEVER, sizeLimit=0, searchControls=None): if searchBase is None: searchBase = self._baseDN searchRequest = SearchRequest() searchRequest['baseObject'] = LDAPDN(searchBase) searchRequest['scope'] = Scope(scope) searchRequest['derefAliases'] = DeRefAliases(derefAliases) searchRequest['sizeLimit'] = IntegerPositive(sizeLimit) searchRequest['timeLimit'] = IntegerPositive(0) searchRequest['typesOnly'] = Boolean(False) searchRequest['filter'] = self._parseFilter(searchFilter) searchRequest['attributes'] = AttributeSelection() if attributes is not None: searchRequest['attributes'].setComponents(*attributes) done = False answers = [] # We keep asking records until we get a searchResDone packet while not done: response = self.sendReceive('searchRequest', searchRequest, searchControls) for message in response: protocolOp = message['protocolOp'] if protocolOp.getName() == 'searchResDone': if protocolOp['searchResDone']['resultCode'] == ResultCode( 'success'): done = self._handleControls(searchControls, message['controls']) else: raise LDAPSearchError( error=int( protocolOp['searchResDone']['resultCode']), errorString='Error in searchRequest -> %s:%s' % (protocolOp['searchResDone'] ['resultCode'].prettyPrint(), protocolOp['searchResDone']['diagnosticMessage']), answers=answers) else: answers.append(message['protocolOp'][protocolOp.getName()]) return answers # ToDo: sorted(answers) ?
def search(self, searchBase=None, searchFilter=None, scope=SCOPE_SUB, attributes=None, derefAliases=DEREF_NEVER, sizeLimit=0): # ToDo: For now we need to specify a filter as a Filter instance, meaning, we have to programmatically build it # ToDo: We have to create functions to parse and compile a text searchFilter into a Filter instance. if searchBase is None: searchBase = self._baseDN searchRequest = SearchRequest() searchRequest['baseObject'] = LDAPDN(searchBase) searchRequest['scope'] = Scope(scope) searchRequest['derefAliases'] = DeRefAliases(derefAliases) searchRequest['sizeLimit'] = IntegerPositive(sizeLimit) searchRequest['timeLimit'] = IntegerPositive(0) searchRequest['typesOnly'] = Boolean(False) searchRequest[ 'filter'] = searchFilter # could do self.parseFilter(searchFilter) or leave it for the user searchRequest['attributes'] = AttributeSelection() if attributes is not None: for i, item in enumerate(attributes): searchRequest['attributes'][i] = item done = False answers = [] # We keep asking records until we get a searchResDone packet while not done: resp = self.sendReceive('searchRequest', searchRequest) for item in resp: protocolOp = item['protocolOp'] if protocolOp.getName() == 'searchResDone': done = True if protocolOp['searchResDone']['resultCode'] != 0: raise LDAPSearchError( error=int( protocolOp['searchResDone']['resultCode']), errorString='Error in searchRequest -> %s:%s' % (protocolOp['searchResDone'] ['resultCode'].prettyPrint(), protocolOp['searchResDone']['diagnosticMessage']), answers=answers) else: answers.append(item['protocolOp'][protocolOp.getName()]) return answers