/usr/lib/python2.7/dist-packages/dhm/ldapObject.py is in python-dhm 0.6-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | # ldapObject.py
#
# Copyright 2000,2001,2003 Wichert Akkerman <wichert@deephackmode.org>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Calculate shared library dependencies
"""LDAP object helper
This python module implements the ldapObject class, which represents an entry
in a LDAP directory. You can use an ldapObject as a normal map, when you are
ready to commit your changes to the LDAP directory the object will perform the
necessary actions itself.
Creating a new entry in a directory
===================================
Adding a new entry to a directory is simple: first you create a ldapObject
with the right dn, then you fill it with the necessary data and finally you
commit it to the directory using the modify() function.
A simple example::
import ldap, ldapObject
# Open a connection to the LDAP directory
db=ldap.open("ldap", 389)
db.simple_bind("user", "password")
# Create a new object
obj=ldapObject.ldapObject("sn=John Doe,ou=People,dc=domain")
obj["objectClass"].append("inetOrgPerson")
obj["sn"]="John Doe"
obj["givenName"]="John"
# Commit changes to the directory
try:
obj.modify(db)
except ldap.LDAPError, e:
print "Error adding entry %s: %s" % (obj.dn, str(e))
Turning LDAP search results into ldapObjects
============================================
Since ldapObject aims to be a minimal class it does not have specific
functionality to search in a LDAP directory. However you can easily convert
the results from ldap.search_s into a sequence of ldapObjects using map().
An example::
import ldap, ldapObject
# Open a connection to the LDAP directory
db=ldap.open("ldap", 389)
db.simple_bind("user", "password")
# Perform a search for all objects with a uid attribute
results=db.search_s("ou=People,dc=domain", ldap.SCOPE_SUBTREE, "uid=*")
results=map(ldapObject.ldapObject, results)
Deriving from ldapObject
========================
You might want to dervice a new class from ldapObject if you want to add new
functions or change the sorting order.
Simple derived class
--------------------
This example shows a new class that represents a companies office::
class Office(ldapObject.ldapObject):
"A simple class to represent an office"
ObjectClasses = [ "locality", "organizationalUnit" ]
def __init__(self, init=""):
"Initialize the Office object"
ldapObject.ldapObject.__init__(self, init)
# Add the essential objectClasses
for oc in self.ObjectClases:
if not oc in self.attr["ObjectClass"]:
self.attr["ObjectClass"].append(oc)
# Snapshot the current state of the object
self._snapshot()
def name(self):
'''Return a string with a name for this office. The name is
assembled from the location, state and country.'''
name=[]
if self.attr.has_key("l"):
name.append(self.attr["l"][0])
if self.attr.has_key("st"):
name.append(self.attr["st"][0])
if self.attr.has_key("c"):
name.append(data.Countries[self.attr["c"][0]])
if len(name):
name=", ".join(name)
else:
name=self.attr["ou"]
return name
Changing the sort order
-----------------------
The sorting order for classes can be changed by modifying the SortOrder
variable. SortOder is a list for tuples containing the attribute to sort on
and the direction (one of ldapObject.SORT_ASCENDING or
ldapObject.SORT_DESCENDING).
An example::
class Office(ldapObject.ldapObject):
"A simple class to represent an office"
SortOrder = [
("c", ldapObject.SORT_ASCENDING), # Sort by country first
("st", ldapObject.SORT_ASCENDING), # Then by state
("l", ldapObject.SORT_ASCENDING), # And finally sort by city
]
@var SORT_ASCENDING: use ascending sort order, used for ldapObject.SortOrder.
@var SORT_DESCENDING: use ascending sort order, used for ldapObject.SortOrder.
"""
__docformat__ = "epytext en"
# Import system packages we need
import copy, types, sys
import ldap, ldap.modlist, ldif
# Sort orders
SORT_ASCENDING = 0
SORT_DESCENDING = 1
class ldapObjectException(Exception):
"""ldapObject exception class
@ivar reason: reason for raising this exception
@type reason: string
"""
def __init__(self, reason):
"""Constructor.
@param reason: reason for raising this exception
@type reason: string
"""
self.reason=reason
def __str__(self):
return self.reason
class ldapObject:
"""
@cvar ObjectClasses: list of object classes this class is a member of
@type ObjectClasses: sequence of strings
@cvar SortOrder: Sorting order used when sorting ldapObjects.
@type SortOrder: sequence of
(attribute, SORT_ASCENDING|SORT_DESCENDING) pairs
"""
SortOrder = [ ]
ObjectClasses = [ "top" ]
def __init__(self,init=""):
"""ldapObject constructor.
There are three methods to initialize a ldapObject:
1. Using a DN
2. Using a tuple as returned by a LDAP search
3. Using another ldapObject
"""
if type(init)==types.StringType:
# String initializer, assume it is a DN
self.dn=init
self.attr={ }
elif type(init)==types.TupleType and \
((type(init[0])==types.StringType) and (type(init[1])==types.DictionaryType)):
# This looks like the result of a LDAP search
self.dn=init[0]
self.attr=init[1]
elif type(init)==types.InstanceType and \
(init.__dict__.has_key("orgAttr") and init.__dict__.has_key("attr") and init.__dict__.has_key("dn")):
# This looks more like another ldapObject
self.dn=other.dn
self.attr=copy.deepcopy(other.attr)
else:
# Can't determine what type init is.. lets abort
raise ldapObjectException, "Trying to initialize object with unknown initializer"
# Having a objectClass is mandatory
if not self.attr.has_key("objectClass"):
self.attr["objectClass"]=["top"]
for oc in self.ObjectClasses:
if oc not in self.attr["objectClass"]:
self.attr["objectClass"].append(oc)
self._snapshot()
def delete(self,db):
"""Delete this object from the database.
@param db: database to store data in
@type db: LDAP connection
"""
db.delete_s(self.dn)
def load(self,db):
"""Load this object from the database.
@param db: database to store data in
@type db: LDAP connection
"""
(self.dn,self.attr)=db.search_s(self.dn, ldap.SCOPE_BASE, self._classfilter())[0]
self._snapshot()
def _snapshot(self):
"""Snapshot current state.
Assume we have an untouched object: initialize orgAttr and
reset changes. For internal use only!"""
self.orgAttr=copy.deepcopy(self.attr)
self.changes=[]
def update(self,db):
"""Commit changes to directory.
Build a list of changes made to our attributes and try to
commit them to the database.
@param db: database to store data in
@type db: LDAP connection
"""
try:
todo=ldap.modlist.modifyModlist(self.orgAttr, self.attr)
db.modify_s(self.dn, todo)
except ldap.NO_SUCH_OBJECT:
todo=ldap.modlist.addModlist(self.attr)
db.add_s(self.dn, todo)
# We have to reload to make sure self.attr and self.orgAttr
# are up to date.
self.load(db)
def ldif(self, fd=sys.stdout):
"""Output the object in LDIF format.
@param fd: File to output LDIF data to
@type fd: file object
@return: LDIF data
@rtype: string
"""
writer=ldif.LDIFWriter(fd)
writer.unparse(self.dn, self.attr)
def _classfilter(self):
"""Return a LDAP search filter for our ObjectClasses"""
return "".join(map(lambda x: "(objectClass=%s)" % x,
self.ObjectClasses))
def __str__(self):
return self.ldif()
def _getkey(self,key):
"""Return the sorting-key used for sorting. Used by __cmp__
to simplify our soring code. If an attribute is not found
an empty list is returned instead"""
if self.attr.has_key(key):
return self.attr[key]
else:
return []
def __cmp__(self, other):
"""Compare ourself to another ldapObject class using the
sorting options defined in SortOrder. Take into account
that we are actually comparing lists of items."""
for key in self.SortOrder:
(a,b)=(self._getkey(key[0]), other._getkey(key[0]))
if key[1]==SORT_ASCENDING:
aWins=1
else:
aWins=-1
for i in range(min(len(a), len(b))):
(x,y)=(a[i].lower(),b[i].lower())
if (x>y):
return aWins
elif (x<y):
return -aWins
if len(a)<len(b):
return aWins
elif len(a)>len(b):
return -aWins
return 0
# Set of members to emulate a mapping
def clear(self):
self.attr.clear()
self.orgAttr.clear()
def has_key(self,key):
return self.attr.has_key(key)
def items(self):
return self.attr.items()
def keys(self):
return self.attr.keys()
def __len__(self):
return len(self.attr)
def __delitem__(self, key):
del self.attr[key]
def __getitem__(self, key):
return self.attr[key]
def __setitem__(self, key, value):
self.attr[key]=value
|