1"""
2ldap.controls.openldap - classes for OpenLDAP-specific controls
3
4See https://www.python-ldap.org/ for project details.
5"""
6
7import ldap.controls
8from ldap.controls import ValueLessRequestControl,ResponseControl
9
10from pyasn1.type import univ
11from pyasn1.codec.ber import decoder
12
13
14__all__ = [
15  'SearchNoOpControl',
16  'SearchNoOpMixIn',
17]
18
19
20class SearchNoOpControl(ValueLessRequestControl,ResponseControl):
21  """
22  No-op control attached to search operations implementing sort of a
23  count operation
24
25  see https://www.openldap.org/its/index.cgi?findid=6598
26  """
27  controlType = '1.3.6.1.4.1.4203.666.5.18'
28
29  def __init__(self,criticality=False):
30    self.criticality = criticality
31
32  class SearchNoOpControlValue(univ.Sequence):
33    pass
34
35  def decodeControlValue(self,encodedControlValue):
36    decodedValue,_ = decoder.decode(encodedControlValue,asn1Spec=self.SearchNoOpControlValue())
37    self.resultCode = int(decodedValue[0])
38    self.numSearchResults = int(decodedValue[1])
39    self.numSearchContinuations = int(decodedValue[2])
40
41
42ldap.controls.KNOWN_RESPONSE_CONTROLS[SearchNoOpControl.controlType] = SearchNoOpControl
43
44
45class SearchNoOpMixIn:
46  """
47  Mix-in class to be used with class LDAPObject and friends.
48
49  It adds a convenience method noop_search_st() to LDAPObject
50  for easily using the no-op search control.
51  """
52
53  def noop_search_st(self,base,scope=ldap.SCOPE_SUBTREE,filterstr='(objectClass=*)',timeout=-1):
54    try:
55      msg_id = self.search_ext(
56        base,
57        scope,
58        filterstr=filterstr,
59        attrlist=['1.1'],
60        timeout=timeout,
61        serverctrls=[SearchNoOpControl(criticality=True)],
62      )
63      _,_,_,search_response_ctrls = self.result3(msg_id,all=1,timeout=timeout)
64    except (
65      ldap.TIMEOUT,
66      ldap.TIMELIMIT_EXCEEDED,
67      ldap.SIZELIMIT_EXCEEDED,
68      ldap.ADMINLIMIT_EXCEEDED
69    ) as e:
70      self.abandon(msg_id)
71      raise e
72    else:
73      noop_srch_ctrl = [
74        c
75        for c in search_response_ctrls
76        if c.controlType==SearchNoOpControl.controlType
77      ]
78      if noop_srch_ctrl:
79        return noop_srch_ctrl[0].numSearchResults,noop_srch_ctrl[0].numSearchContinuations
80      else:
81        return (None,None)
82