Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

openldap.py 2.2KB

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