简体   繁体   中英

Get nested LDAP group members with python-ldap

I'm trying to find the best way to get a list of all LDAP user accounts that belong to groups which are members of a groupOfNames using python-ldap. This is on an OpenLDAP server, not AD. I wrote the function below, which does the job but takes forever to run. I'm hoping either python-ldap has some builtin function that I'm not aware of, or there's something I can modify to make this run more quickly. If not, hopefully someone else will find this code useful. Thanks in advance for any help!

def get_nested_members(con, dn):
    """
    Parameters
    ----------
    con : LDAPObject
        An authenticated python-ldap connection object
    dn : string
        The dn of the groupOfNames to be checked

    Returns
    -------
    members : list
        A list of all accounts that are members of the given dn
    """

    members = []
    searched = []
    to_search = [dn]

    while len(to_search) > 0:
        current_dn = to_search.pop()
        cn = current_dn.split(',')[0]
        r = con.search_s(base_dn, ldap.SCOPE_SUBTREE, cn, [])[0][1]
        if 'groupOfNames' in r['objectClass']:
            if 'member' in r:
                for i in r['member']:
                    if((i != current_dn) and (i not in searched)):
                        to_search.append(i)
            searched.append(current_dn)
        elif 'posixGroup' in r['objectClass']:
            if 'memberUid' in r:
                for i in r['memberUid']:
                    members.append(i)
            searched.append(current_dn)
        elif 'posixAccount' in r['objectClass']:
            if 'uid' in r:
                members.append(r['uid'][0])
        else:
            print('ERROR: encountered record of unknown type:')
            pprint(str([current_dn, r]))
    return list(set(members))

I realized that running ldapsearch repeatedly was the limiting factor, so I made a new version which builds a dictionary of ALL group and groupOfNames records first. It takes up a bit more memory than the old solution, but is less taxing on the LDAP server and runs significantly faster (down from ~15 minutes to <1 second for my application). I'll leave the original code below the new version for a reference of what not to do. Credit for the merge_dicts() function goes to Aaron Hall .

import ldap

def merge_dicts(*dict_args):
    """Given any number of dicts, shallow copy and merge into a new dict,
    precedence goes to key value pairs in latter dicts.
    """

    result = {}
    for dictionary in dict_args:
        result.update(dictionary)
    return result


def get_nested_members(con, dn, base_dn='dc=example'):
    """Search a groupOfNames and return all posixAccount members from all its subgroups

    Parameters
    ----------
    con: LDAPObject
        An authenticated LDAP connection object
    dn: string
        The dn of the groupOfNames to be searched for members
    (optional) base_dn: string
        The base dn to search on.  Make sure to change the default value to fit your LDAP server

    Returns
    -------
    members: list
        A list of all nested members from the provided groupOfNames
    """

    logging.info('Getting nested members of ' + str(dn))
    print('Getting nested members of ' + str(dn))

    if type(dn) is list:
        to_search = [] + dn
    elif type(dn) is str:
        to_search = [dn]
    else:
        print('ERROR: Invalid dn value.  Please supply either a sting or list of strings.')
        return []

    members = []
    searched = []

    groupOfNames_list = con.search_s(base_dn, ldap.SCOPE_SUBTREE, 'objectClass=groupOfNames', ['dn', 'member', 'cn'])
    groupOfNames_dict = {}
    for g in range(len(groupOfNames_list)):
        groupOfNames_dict[groupOfNames_list[g][0]] = groupOfNames_list[g][1]
    groupOfNames_list = None    #To free up memory

    group_list = con.search_s(base_dn, ldap.SCOPE_SUBTREE, 'objectClass=posixGroup', ['dn', 'memberUid', 'cn'])
    group_dict = {}
    for g in range(len(group_list)):
        group_dict[group_list[g][0]] = group_list[g][1]
    group_list = None   #To free up memory

    all_groups = merge_dicts(groupOfNames_dict, group_dict)
    group_dict = None   #To free up memory
    groupOfNamesdict = None #To free up memory

    while len(to_search) > 0:
        search_dn = to_search.pop()
        try:
            g = all_groups[search_dn]
            if 'memberUid' in g:
                members += g['memberUid']
                searched.append(search_dn)
            elif 'member' in g:
                m = g['member']
                for i in m:
                    if i.startswith('uid='):
                        members.append((i.split(',')[0]).split('=')[1])
                    elif i.startswith('cn='):
                        if i not in searched:
                            to_search.append(i)
                searched.append(search_dn)
            else:
                searched.append(search_dn)
        except:
            searched.append(search_dn)
    return list(set(members))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM