+

:4736 Function create_function() is deprecated [8192]

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 sys
  4. import ldap
  5. import ldap.modlist as modlist
  6. import logging
  7.  
  8. class LdapServer(object):
  9.  
  10. uri = None
  11. dn = None
  12. pwd = None
  13. v2 = None
  14.  
  15. con = 0
  16.  
  17. def __init__(self,uri,dn=None,pwd=None,v2=None):
  18. self.uri = uri
  19. self.dn = dn
  20. self.pwd = pwd
  21. if v2:
  22. self.v2=True
  23.  
  24. def connect(self):
  25. if self.con == 0:
  26. try:
  27. con = ldap.initialize(self.uri)
  28. if self.v2:
  29. con.protocol_version = ldap.VERSION2
  30. else:
  31. con.protocol_version = ldap.VERSION3
  32.  
  33. if self.dn:
  34. con.simple_bind_s(self.dn,self.pwd)
  35.  
  36. self.con = con
  37. except ldap.LDAPError, e:
  38. logging.critical('LdapServer - Error connecting and binding to LDAP server : %s' % e)
  39. sys.exit(1)
  40.  
  41. def search(self,basedn,filter,attrs,sizelimit=0):
  42. res_id = self.con.search(basedn,ldap.SCOPE_SUBTREE,filter,attrs)
  43. ret = {}
  44. c=0
  45. while 1:
  46. res_type, res_data = self.con.result(res_id,0)
  47. if res_data == [] or sizelimit!=0 and c>sizelimit:
  48. break
  49. else:
  50. if res_type == ldap.RES_SEARCH_ENTRY:
  51. ret[res_data[0][0]]=res_data[0][1]
  52. c=c+1
  53. return ret
  54.  
  55. def add_object(self,dn,attrs):
  56. ldif = modlist.addModlist(attrs)
  57. try:
  58. logging.debug("LdapServer - Add %s" % dn)
  59. self.con.add_s(dn,ldif)
  60. return True
  61. except ldap.LDAPError, e:
  62. logging.warning("LdapServer - Error adding %s : %s" % (dn,e))
  63.  
  64. return False
  65.  
  66. def update_object(self,dn,old,new):
  67. ldif = modlist.modifyModlist(old,new)
  68. if ldif == []:
  69. #logging.debug("LdapServer - No change for %s" % dn)
  70. return True
  71. try:
  72. #logging.debug("LdapServer - Update %s" % dn)
  73. self.con.modify_s(dn,ldif)
  74. return True
  75. except ldap.LDAPError, e:
  76. logging.warning("LdapServer - Error updating %s : %s" % (dn,e))
  77. return False
  78.  
  79. def drop_object(self,dn):
  80. try:
  81. logging.debug("LdapServer - Delete %s" % dn)
  82. self.con.delete_s(dn)
  83. return True
  84. except ldap.LDAPError, e:
  85. logging.warning("LdapServer - Error deleting %s : %s" % (dn,e))
  86.  
  87. return False
  88.  
  89. def get_dn(self,obj):
  90. return obj[0][0]
  91.  
  92. def get_attr(self,obj,attr,all=None):
  93. if all is not None:
  94. if attr in obj:
  95. return obj[attr]
  96. else:
  97. return []
  98. else:
  99. if attr in obj:
  100. return obj[attr][0]
  101. else:
  102. return None