Search code examples
pythonldappython-ldap

How to retrieve all the attributes of LDAP database


I am using ldap module of python to connect to ldap server. I am able to query the database but I dont know how to retrieve the fields present in the database, so that I can notify the user in advance to quering the database, telling him that the field he is trying to access is not in the database.

For example if the fields present are just

cn
memberOf

and if the user tries to query the database with filter

cn and memberOf and notcontained

I should be able to know that the notcontained attribute is not in the dabase schema.

How can I accomplish this.

Thanks.


Solution

  • I am using ldap module of python to connect to ldap server. I am able to query the database but I dont know how to retrieve the fields present in the database, so that I can notify the user in advance to quering the database, telling him that the field he is trying to access is not in the database.

    A simple solution would be to search and then print a list of the keys from the result.

    import ldap
    
    # connect to your ldap server
    
    some_dn = '...' # Your base dn
    some_lookup = '...' # your lookup attr
    
    result = conn.search_s(some_dn,ldap.SCOPE_SUBTREE,some_lookup)
    result[0][1].keys()
    

    For example, against my AD server it returns the following:

    ['mailNickname',
     'publicDelegatesBL',
     'logonCount',
     'cn',
     'countryCode',
     'dSCorePropagationData',
     'objectClass',
     # ... many many more
    'telephoneNumber',
    'physicalDeliveryOfficeName',
    'name',
    'memberOf',
    'codePage',
    'userAccountControl',
    'msExchMDBRulesQuota',
    'lastLogon',
    'protocolSettings',
    'uSNChanged',
    'sn',
    'msExchVersion',
    'mDBUseDefaults',
    'givenName',
    'msExchMailboxGuid',
    'lastLogoff']