fetching the structure / obejctClass defs

Igor Stroh stroh at scan-plus.de
Fri May 17 12:15:37 CEST 2002


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 attribs
> from an openLDAP server using python-ldap?
> 
> 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 = "<admin dn>"
ADMIN_PW = "<admin bind pw>"
# which objectClass(es) should we look for
O_CLASSES = ['person']

# some global RE objects
search_oid          = re.compile("^\\( (?P<oid>\d.*\d) .*")
search_name         = re.compile(".* NAME '(?P<name>[a-zA-Z0-9_]+)' .*")
search_desc         = re.compile(".* DESC '(?P<desc>.*)' .*")
search_must_attr    =
re.compile(".*\s?MUST\s?\\(\s?(?P<must_attr>[a-zA-Z0-9_$\
           ]+)\s?\\)\s?.*")
search_may_attr      =
re.compile(".*\s?MAY\s?\\(\s?(?P<may_attr>[a-zA-Z0-9_$ ]+)\s?\\)\s?.*")

def init_all():
    filter = ".*NAME '%s'.*" % O_CLASSES[0]
    del O_CLASSES[0]
    for cl in O_CLASSES:
        filter += "|.*NAME '%s'.*" % O_CLASSES[0]

    ls = re.compile(filter, re.I)

    con = ldap.open("212.75.33.146")
    con.simple_bind_s(ADMIN_DN,ADMIN_PW)

    entries = con.search_s("cn=Subschema", ldap.SCOPE_BASE, \
                     "objectclass=subschema", ['objectclasses'])
    ocs = entries[0][1]['objectClasses']
    return [oc for oc in ocs if ls.search(oc)]

def parseIt():
    ocs = init_all()
    ret = {}
    for i in ocs:
        key = search_name.sub("\g<name>", i)
        ret[key] = {}
        ret[key]['OID'] = search_oid.sub("\g<oid>", i)
        if search_desc.search(i):
            ret[key]['DESC'] = search_desc.sub("\g<desc>", i)
        else:
            ret[key]['DESC'] = ""
        if search_must_attr.search(i):
            ret[key]['MUST'] = [attr.strip() for attr in \
            string.split(search_must_attr.sub("\g<must_attr>", i), "$")]
        else:
            ret[key]['MUST'] = []
        if search_may_attr.search(i):
            ret[key]['MAY'] = [attr.strip() for attr in \ 
            string.split(search_may_attr.sub("\g<may_attr>", i), "$")]
        else:
            ret[key]['MAY'] = []

    return ret

def getAllAttributes():
    attrs = []
    ocs = parseIt()
    for key in ocs.keys():
            attrs = attrs + ocs[key]['MUST'] + ocs[key]['MAY']
    return attrs


-- 
ScanPlus GmbH NOC Ulm - Germany - Griesbadgasse 7-13 - D 89073 Ulm
TEL +49 731 920 13 100 - FAX +49 731 920 13 290
eMail: support at scan-plus.de
Amtsgericht Ulm - HRB3220 - Geschaeftsfuehrer: Juergen Hoermann
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 232 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-ldap/attachments/20020517/69743c19/attachment.pgp>


More information about the python-ldap mailing list