Package easyLDAP :: Module easyLDAP_class_tree
[frames] | no frames]

Source Code for Module easyLDAP.easyLDAP_class_tree

  1  # -*- coding: utf-8 -*- 
  2   
  3  #    easyLDAP - a python library that makes LDAP management easier than before... 
  4  #    Copyright (C) 2004-2010,2020 Mike Gabriel <m.gabriel@das-netzwerkteam.de> 
  5  # 
  6  #    This program is free software: you can redistribute it and/or modify 
  7  #    it under the terms of the GNU General Public License as published by 
  8  #    the Free Software Foundation, either version 3 of the License, or 
  9  #    (at your option) any later version. 
 10  # 
 11  #    This program is distributed in the hope that it will be useful, 
 12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  #    GNU General Public License for more details. 
 15  # 
 16  #    You should have received a copy of the GNU General Public License 
 17  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 18  # 
 19   
 20  # these modules appear in python-ldap 
 21  import ldap, ldap.schema 
 22  import time 
 23   
 24  from easyLDAP_defaults import * 
 25  from easyLDAP_exceptions import * 
 26  from easyLDAP_utils import * 
 27  from easyLDAP_bind import * 
 28  from easyLDAP_class_base import * 
 29  from easyLDAP_class_object_base import * 
 30   
 31   
32 -class easyLDAP_tree(easyLDAP):
33 34 _ldap_cachetree = None 35 _init_dn = '' 36 SearchFilter = 'objectClass=*' 37
38 - def _method_aliases(self):
39 40 easyLDAP._method_aliases(self) 41 42 self.get_cache_basedn = self.get_tree_basedn 43 self.get_cachebasedn = self.get_tree_basedn 44 self.load_cache = self.refresh_cache 45 self.show = self.show_cache 46 self.push = self.push_cache_history
47 48
49 - def __init__(self, dn_or_cachetree, bind_dn=None, bind_pw=None, filter='objectClass=*', config_defaults=EASY_LDAP, update_schema_cache=False, use_cache_history=True, ldapsession=None):
50 51 easyLDAP.__init__(self, bind_dn=bind_dn, bind_pw=bind_pw, config_defaults=config_defaults, update_schema_cache=update_schema_cache, ldapsession=ldapsession) 52 53 self.use_cache_history=use_cache_history 54 self.SearchFilter = filter 55 if isinstance(dn_or_cachetree, easyLDAP_cachetree): 56 self._ldap_cachetree = dn_or_cachetree 57 dn = self._ldap_cachetree.get_cachetree_basedn() 58 self._init_dn = dn 59 else: 60 dn = dn_or_cachetree 61 self._init_dn = dn 62 self.refresh_cache() 63 64 self._method_aliases()
65 66
67 - def __len__(self):
68 return len(self._ldap_cachetree)
69 70
71 - def __call__(self, **kwargs):
72 73 self._ldap_cachetree(**kwargs)
74
75 - def __deepcopy__(self,memo):
76 """Creates a deepcopy of the easyLDAP tree class.""" 77 result = self.__class__(self.get_tree_basedn()) 78 memo[id(self)] = result 79 80 result._ldap_cachetree = copy.deepcopy(self._ldap_cachetree,memo) 81 82 result.LDAPuri = self.LDAPuri 83 result.LDAPserver = self.LDAPserver 84 result.BindDN = self.BindDN 85 result.BindPW = self.BindPW 86 result.BaseDN = self.BaseDN 87 result.AdminDN = self.AdminDN 88 result.PeopleBaseDN = self.PeopleBaseDN 89 result.GroupBaseDN = self.GroupsBaseDN 90 result.HostsBaseDN = self.HostsBaseDN 91 result.MailAliasesBaseDN = self.MailAliasesBaseDN 92 result.AutomountBaseDN = self.AutomountBaseDN 93 result.LDAPServerCharsetEncoding = self.LDAPServerCharsetEncoding 94 result.posix_availableUidNumbers = self.posix_availableUidNumbers 95 result.posix_availableGidNumbers = self.posix_availableGidNumbers 96 if (self.BindDN != 'anonymous') and self.has_dn(self.BindDN): 97 result.bind_norefresh(self.BindDN, self.BindPW) 98 99 result.objectClasses=self.objectClasses 100 result.attributeTypes=self.attributeTypes 101 result.matchingRules=self.matchingRules 102 result.ldapSyntaxes=self.ldapSyntaxes 103 104 return result
105 106
107 - def __delitem__(self, dn):
108 """ 109 """ 110 del self._ldap_cachetree[dn]
111 112
113 - def refresh_cache(self):
114 try: 115 ldap_import=ldapDictionaryDecode(self.ldapsession.search_s(self.get_tree_basedn(), ldap.SCOPE_SUBTREE, self.SearchFilter), config_defaults=self.CONFIG) 116 if self.is_ldaptree(ldap_import): 117 self._ldap_cachetree = easyLDAP_cachetree(ldap_import, config_defaults=self.CONFIG, use_cache_history=self.use_cache_history) 118 else: 119 raise easyLDAP_exceptions.NOT_AN_LDAPTREE 120 except ldap.SERVER_DOWN: 121 raise easyLDAP_exceptions.SERVER_UNAVAILABLE 122 except ldap.NO_SUCH_OBJECT: 123 raise easyLDAP_exceptions.NO_SUCH_DN_ON_SERVER
124 125
126 - def _cachetree_to_tree_wrapper(cachetree):
127 """ 128 """ 129 print "Not implemented yet" 130 pass
131 132
133 - def get_tree(self, linear=False, retrieve_data=False, hashed=False):
134 """ 135 get_tree('DN') 136 137 returns the data of a single ldap object from the 138 cached easyLDAP tree. 139 """ 140 if not linear and not hashed: 141 142 ### TODO!!! 143 144 return self._cachetree_to_tree_wrapper(self._ldap_cachetree.get_cachetree()) 145 146 elif linear and not retrieve_data: 147 return [ (dn,easyLDAP_object(co, bind_dn=self.BindDN, bind_pw=self.BindPW, config_defaults=self.CONFIG, ldapsession=self.ldapsession)) for (dn, co) in self._ldap_cachetree.get_cachetree(linear=True) ] 148 elif linear and retrieve_data: 149 return self._ldap_cachetree.get_cachetree_pyldap() 150 elif hashed and not retrieve_data: 151 this_hash = {} 152 for (dn, co) in self._ldap_cachetree.get_cachetree(linear=True): 153 this_hash[dn] = easyLDAP_object(co, bind_dn=self.BindDN, bind_pw=self.BindPW, config_defaults=self.CONFIG, ldapsession=self.ldapsession) 154 return this_hash 155 elif hashed and retrieve_data: 156 return self._ldap_cachetree.get_cachetree(hashed=True, retrieve_data=True) 157 158 else: 159 return None
160
161 - def get_object_from_tree(self, dn):
162 """ 163 """ 164 ldap_dataobj = self._ldap_cachetree.get_cacheobject_from_cachetree(dn) 165 return easyLDAP_object(ldap_dataobj, bind_dn=self.BindDN, bind_pw=self.BindPW, config_defaults=self.CONFIG, use_cache_history=False, ldapsession=self.ldapsession)
166 167
168 - def has_dn(self, dn):
169 170 return self._ldap_cachetree.has_dn(dn)
171 172
173 - def get_tree_basedn(self):
174 """ 175 thisClass.get_tree_basedn() 176 177 returns the base DN of the cached easyLDAPtree. 178 """ 179 return ((self._ldap_cachetree and self._ldap_cachetree.get_cachetree_basedn()) or self._init_dn)
180 181
182 - def get_tree_depth(self):
183 """ 184 """ 185 return self._ldap_cachetree.get_cachetree_depth()
186 187
188 - def get_tree_dnlist(self):
189 """ 190 """ 191 return self._ldap_cachetree.get_cachetree_dnlist()
192 193
194 - def get_tree_hash(self):
195 """ 196 """ 197 return self._ldap_cachetree.get_cachetree_hash()
198
199 - def get_tree_pyldap(self):
200 """ 201 """ 202 return self._ldap_cachetree.get_cachetree_pyldap()
203 204
205 - def get_subtree_data(self, dn):
206 """ 207 """ 208 return self._ldap_cachetree.get_cachetree_subtree_data(dn)
209 210
211 - def get_subtree_cachetree(self, dn):
212 """ 213 """ 214 return self._ldap_cachetree.get_cachetree_subtree(dn)
215 216
217 - def get_subtree(self, dn):
218 """ 219 """ 220 return easyLDAP_tree(self._ldap_cachetree.get_cachetree_subtree(dn), bind_dn=self.BindDN, bind_pw=self.BindPW, config_defaults=self.CONFIG, ldapsession=self.ldapsession)
221 222
223 - def has_parent_dn(self, dn):
224 """ 225 """ 226 return self._ldap_cachetree.has_parent_dn(dn)
227 228
229 - def new_object(self, object_dn, object_data=None):
230 231 if object_data is None: 232 _attr_dict = { 'objectClass': ['top',], } 233 elif is_pyldapobject(object_data): 234 _attr_dict = object_data[0][1] 235 elif is_pyldapobject([object_data]): 236 _attr_dict = object_data[1] 237 else: 238 _attr_dict = object_data 239 240 object_data = [[object_dn, _attr_dict]] 241 242 if not is_pyldapobject(object_data): 243 raise easyLDAP_exceptions.NOT_A_PYTHON_LDAPOBJECT 244 245 object_data[0][0] = object_data[0][0].lower() 246 object_dn = object_data[0][0] 247 if self.has_parent_dn(object_dn): 248 249 cacheobject = self._ldap_cachetree.new_cachetree_object(object_data) 250 return self.get_object_from_tree(object_dn) 251 else: 252 raise easyLDAP_exceptions.NO_PARENTOBJECT_FOR_GIVEN_DN
253 254
255 - def remove_object(self, object_dn):
258 259 # def new_account(self, dn): 260 # 261 # # move into class -> easyLDAP_object??? 262 # 263 # object_data = [[dn, self.CONFIG['Template_ACCOUNT']]] 264 # naming_attr, naming_attr_value = get_naming_attribute_value(dn) 265 # 266 # # do a attr -> OID -> attr conversion 267 # object_data[0][1][naming_attr] = [naming_attr_value] 268 # self.new_object(object_data) 269 270
271 - def clear_cache(self):
272 273 dn = self.get_tree_basedn() 274 self._ldap_cachetree = easyLDAP_cachetree(dn, config_defaults=self.CONFIG)
275 276
277 - def show_cache(self):
278 279 self._ldap_cachetree()
280 281
282 - def flush_cache(self):
283 284 reference = self.get_cache_reference(self.get_tree_basedn(), ldap.SCOPE_SUBTREE) 285 reference_dnlist = [ obj[0].lower() for obj in reference ] 286 tree_dnlist = self.get_tree_dnlist() 287 (sorted_dnlist, orphans) = get_sorted_dnlist(tree_dnlist) 288 if orphans: 289 raise easyLDAP_exceptions.ORPHANED_OBJECTS_IN_TREE 290 else: 291 292 for dn in sorted_dnlist: 293 294 obj = self.get_object_from_tree(dn) 295 obj() 296 obj.flush_cache() 297 298 to_be_deleted_dnlist = [] 299 for dn in reference_dnlist: 300 if dn not in tree_dnlist and dn not in to_be_deleted_dnlist: 301 to_be_deleted_dnlist.append(dn) 302 (to_be_deleted_dnlist, orphans) = get_sorted_dnlist(to_be_deleted_dnlist) 303 to_be_deleted_dnlist.reverse() 304 to_be_deleted_dnlist.extend(orphans) 305 306 for dn in to_be_deleted_dnlist: 307 del_obj = easyLDAP_object(dn, bind_dn=self.BindDN, bind_pw=self.BindPW, ldapsession=self.ldapsession, use_cache_history=False) 308 del_obj.clear_cache() 309 del_obj.show() 310 del_obj.map_diff2modlist() 311 del_obj.flush_cache()
312 313
314 - def push_cache_history(self):
315 316 tree = self._ldap_cachetree._push_cache_history()
317 318
319 - def undo(self):
320 321 self._ldap_cachetree.undo()
322 323
324 - def redo(self):
325 326 self._ldap_cachetree.redo()
327
328 - def find_newUidNumber(self, min_uidNumber=None, max_uidNumber=None, searchbase=None):
329 """ 330 """ 331 if searchbase is None: 332 searchbase = self.PeopleBaseDN 333 334 search_subtree = self.get_subtree(searchbase) 335 cached_searchresult = [ (obj[0], obj[1]._get_object_data()) for obj in search_subtree.get_tree(linear=True) if obj[1].has_attrdesc_set('uidNumber') ] 336 return easyLDAP.find_newUidNumber(self, min_uidNumber=min_uidNumber, max_uidNumber=max_uidNumber, searchbase=searchbase, cached_searchresult=cached_searchresult)
337
338 - def find_newGidNumber(self, min_gidNumber=None, max_gidNumber=None, searchbase=None):
339 """ 340 """ 341 if searchbase is None: 342 searchbase = self.GroupsBaseDN 343 344 search_subtree = self.get_subtree(searchbase) 345 cached_searchresult = [ (obj[0], obj[1]._get_object_data()) for obj in search_subtree.get_tree(linear=True) if obj[1].has_objectclass_set('posixGroup') and obj[1].has_attrdesc_set('gidNumber') ] 346 return easyLDAP.find_newGidNumber(self, min_gidNumber=min_gidNumber, max_gidNumber=max_gidNumber, searchbase=searchbase, cached_searchresult=cached_searchresult)
347 348 349 # for backwards compatitbility 350 easyLDAPtree = easyLDAP_tree 351 352 ############################ 353 # 354 # main part only for testing 355 # 356 ############################ 357 358 if __name__=='__main__': 359 pass 360