noirello / bonsai

Simple Python 3 module for LDAP, using libldap2 and winldap C libraries.
MIT License
117 stars 33 forks source link

Paging the result for attributes #32

Closed reach4bawer closed 5 years ago

reach4bawer commented 5 years ago

I am trying to get members of a group and find their email address. The AD group contains 50k+ results. When I query the AD group I can only get 1500 results. I tried the acquire_next_page flag but couldn't get the next set of attributes.

noirello commented 5 years ago

There's no built-in solution for getting all the values from an Active Directory.

But according to this article you can do something like this:

import bonsai
from collections import defaultdict

...

res = conn.search("cn=your_entry_with_many_values,dc=test", 0)
attrs = defaultdict(list)
while True:
    next_ranges = []
    for key in res[0].keys():
        grps = re.match("([^;]+);range=([0-9]+)-([0-9]+)", key)
        if grps:
            name, _, upper_bound = grps.groups()
            attrs[name].extend(res[0][key])
            next_ranges.append("{name};range={idx}-*".format(name=name, idx=(int(upper_bound) + 1)))
    res = conn.search(res[0].dn, 0, attrlist=next_ranges)
    if all("-*" in k for k in res[0].keys() if k != "dn"):
        for k in next_ranges:
            key = k.split(";")[0]
            attrs[key].extend(res[0][k])
        break;

The attrs dictionary will have all the assigned values for your many valued attributes.

reach4bawer commented 5 years ago

Thank you this seems to help.