|
From: Igor S. <st...@sc...> - 2002-05-17 10:15:46
|
Heya,
On Fre, 2002-05-17 at 11:45, Tjabo Kloppenburg wrote:
> is it possible to fetch a list of all supported objectClasses and their a=
ttribs
> from an openLDAP server using python-ldap?
>=20
> how?
actually it is, but the representation type of attributeTypes and
objectClasses is 'string', so you get a list of strings that you first
have to parse somehow if you want to utilize it... see [1], it works for
me, but I'm pretty sure I left some schema attributes (like STRUCTURAL
etc.) out, so I might just as well fail to return correct values for
your own objectClass... it parses 'person' pretty neat though...
HTH,
Igor
[1]:
#!/usr/bin/python2.1
import ldap
import string
import re
# bind credentials
ADMIN_DN =3D "<admin dn>"
ADMIN_PW =3D "<admin bind pw>"
# which objectClass(es) should we look for
O_CLASSES =3D ['person']
# some global RE objects
search_oid =3D re.compile("^\\( (?P<oid>\d.*\d) .*")
search_name =3D re.compile(".* NAME '(?P<name>[a-zA-Z0-9_]+)' .*")
search_desc =3D re.compile(".* DESC '(?P<desc>.*)' .*")
search_must_attr =3D
re.compile(".*\s?MUST\s?\\(\s?(?P<must_attr>[a-zA-Z0-9_$\
]+)\s?\\)\s?.*")
search_may_attr =3D
re.compile(".*\s?MAY\s?\\(\s?(?P<may_attr>[a-zA-Z0-9_$ ]+)\s?\\)\s?.*")
def init_all():
filter =3D ".*NAME '%s'.*" % O_CLASSES[0]
del O_CLASSES[0]
for cl in O_CLASSES:
filter +=3D "|.*NAME '%s'.*" % O_CLASSES[0]
ls =3D re.compile(filter, re.I)
con =3D ldap.open("212.75.33.146")
con.simple_bind_s(ADMIN_DN,ADMIN_PW)
entries =3D con.search_s("cn=3DSubschema", ldap.SCOPE_BASE, \
"objectclass=3Dsubschema", ['objectclasses'])
ocs =3D entries[0][1]['objectClasses']
return [oc for oc in ocs if ls.search(oc)]
def parseIt():
ocs =3D init_all()
ret =3D {}
for i in ocs:
key =3D search_name.sub("\g<name>", i)
ret[key] =3D {}
ret[key]['OID'] =3D search_oid.sub("\g<oid>", i)
if search_desc.search(i):
ret[key]['DESC'] =3D search_desc.sub("\g<desc>", i)
else:
ret[key]['DESC'] =3D ""
if search_must_attr.search(i):
ret[key]['MUST'] =3D [attr.strip() for attr in \
string.split(search_must_attr.sub("\g<must_attr>", i), "$")]
else:
ret[key]['MUST'] =3D []
if search_may_attr.search(i):
ret[key]['MAY'] =3D [attr.strip() for attr in \=20
string.split(search_may_attr.sub("\g<may_attr>", i), "$")]
else:
ret[key]['MAY'] =3D []
return ret
def getAllAttributes():
attrs =3D []
ocs =3D parseIt()
for key in ocs.keys():
attrs =3D attrs + ocs[key]['MUST'] + ocs[key]['MAY']
return attrs
--=20
ScanPlus GmbH NOC Ulm - Germany - Griesbadgasse 7-13 - D 89073 Ulm
TEL +49 731 920 13 100 - FAX +49 731 920 13 290
eMail: su...@sc...
Amtsgericht Ulm - HRB3220 - Geschaeftsfuehrer: Juergen Hoermann
|