+

Last commit for LdapServer.py: f5771ca6983e060118cfd7f33bb619c4e8ede45b

LdapServer :: update_object() : add ignore_attrs parameter

Benjamin Renard [2018-02-16 09:52:30]
LdapServer :: update_object() : add ignore_attrs parameter
  1. #!/usr/bin/python
  2.  
  3. import ldap
  4. import ldap.modlist as modlist
  5. import logging
  6.  
  7. class LdapServer(object):
  8.  
  9. uri = None
  10. dn = None
  11. pwd = None
  12. v2 = None
  13.  
  14. con = 0
  15.  
  16. def __init__(self,uri,dn=None,pwd=None,v2=None,raiseOnError=False):
  17. self.uri = uri
  18. self.dn = dn
  19. self.pwd = pwd
  20. self.raiseOnError = raiseOnError
  21. if v2:
  22. self.v2=True
  23.  
  24. def _error(self,error,level=logging.WARNING):
  25. if self.raiseOnError:
  26. raise LdapServerException(error)
  27. else:
  28. logging.log(level,error)
  29.  
  30. def connect(self):
  31. if self.con == 0:
  32. try:
  33. con = ldap.initialize(self.uri)
  34. if self.v2:
  35. con.protocol_version = ldap.VERSION2
  36. else:
  37. con.protocol_version = ldap.VERSION3
  38.  
  39. if self.dn:
  40. con.simple_bind_s(self.dn,self.pwd)
  41.  
  42. self.con = con
  43. return True
  44. except ldap.LDAPError, e:
  45. self._error('LdapServer - Error connecting and binding to LDAP server : %s' % e,logging.CRITICAL)
  46. return False
  47. return True
  48.  
  49. def search(self,basedn,filter,attrs,sizelimit=0):
  50. res_id = self.con.search(basedn,ldap.SCOPE_SUBTREE,filter,attrs)
  51. ret = {}
  52. c=0
  53. while 1:
  54. res_type, res_data = self.con.result(res_id,0)
  55. if res_data == [] or sizelimit!=0 and c>sizelimit:
  56. break
  57. else:
  58. if res_type == ldap.RES_SEARCH_ENTRY:
  59. ret[res_data[0][0]]=res_data[0][1]
  60. c=c+1
  61. return ret
  62.  
  63. def add_object(self,dn,attrs):
  64. ldif = modlist.addModlist(attrs)
  65. try:
  66. logging.debug("LdapServer - Add %s" % dn)
  67. self.con.add_s(dn,ldif)
  68. return True
  69. except ldap.LDAPError, e:
  70. self._error("LdapServer - Error adding %s : %s" % (dn,e), logging.error)
  71.  
  72. return False
  73.  
  74. def update_object(self,dn,old,new):
  75. ldif = modlist.modifyModlist(old,new)
  76. if ldif == []:
  77. return True
  78. try:
  79. self.con.modify_s(dn,ldif)
  80. return True
  81. except ldap.LDAPError, e:
  82. self._error("LdapServer - Error updating %s : %s\nOld : %s\nNew : %s" % (dn, e, old, new), logging.error)
  83. return False
  84.  
  85. def rename_object(self,dn,new_rdn):
  86. try:
  87. logging.debug("LdapServer - Rename %s in %s" % (dn,new_rdn))
  88. self.con.rename_s(dn,new_rdn)
  89. return True
  90. except ldap.LDAPError, e:
  91. self._error("LdapServer - Error renaming %s in %s : %s" % (dn,new_rdn,e), logging.error)
  92.  
  93. return False
  94.  
  95. def drop_object(self,dn):
  96. try:
  97. logging.debug("LdapServer - Delete %s" % dn)
  98. self.con.delete_s(dn)
  99. return True
  100. except ldap.LDAPError, e:
  101. self._error("LdapServer - Error deleting %s : %s" % (dn,e), logging.error)
  102.  
  103. return False
  104.  
  105. def get_dn(self,obj):
  106. return obj[0][0]
  107.  
  108. def get_attr(self,obj,attr,all=None,default=None):
  109. if attr not in obj:
  110. for k in obj:
  111. if k.lower() == attr.lower():
  112. attr = k
  113. break
  114. if all is not None:
  115. if attr in obj:
  116. return obj[attr]
  117. else:
  118. return default or []
  119. else:
  120. if attr in obj:
  121. return obj[attr][0]
  122. else:
  123. return default
  124.  
  125. class LdapServerException(BaseException):
  126. def __init__(self,msg):
  127. BaseException.__init__(self, msg)