Created
August 23, 2019 13:23
-
-
Save abbra/33f5ac59c5cae750ecdb3974978d9cec to your computer and use it in GitHub Desktop.
Verifying DNA and ID ranges in FreeIPA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ipaserver.install import replication | |
def check_ids_in_modified_range(ldap, old_base, old_size, new_base, new_size): | |
from ipalib import errors | |
if new_base is None and new_size is None: | |
# nothing to check | |
return (0, []) | |
if new_base is None: | |
new_base = old_base | |
if new_size is None: | |
new_size = old_size | |
old_interval = (old_base, old_base + old_size - 1) | |
new_interval = (new_base, new_base + new_size - 1) | |
checked_intervals = [] | |
low_diff = new_interval[0] - old_interval[0] | |
if low_diff > 0: | |
checked_intervals.append((old_interval[0], | |
min(old_interval[1], new_interval[0] - 1))) | |
high_diff = old_interval[1] - new_interval[1] | |
if high_diff > 0: | |
checked_intervals.append((max(old_interval[0], new_interval[1] + 1), | |
old_interval[1])) | |
if not checked_intervals: | |
# range is equal or covers the entire old range, nothing to check | |
return (0, []) | |
id_filter_base = ["(objectclass=posixAccount)", | |
"(objectclass=posixGroup)", | |
"(objectclass=ipaIDObject)"] | |
id_filter_ids = [] | |
for id_low, id_high in checked_intervals: | |
id_filter_ids.append("(&(uidNumber>=%(low)d)(uidNumber<=%(high)d))" | |
% dict(low=id_low, high=id_high)) | |
id_filter_ids.append("(&(gidNumber>=%(low)d)(gidNumber<=%(high)d))" | |
% dict(low=id_low, high=id_high)) | |
id_filter = ldap.combine_filters( | |
[ldap.combine_filters(id_filter_base, "|"), | |
ldap.combine_filters(id_filter_ids, "|")], | |
"&") | |
try: | |
num, entries = ldap.search(filter=id_filter, | |
attrs_list=['uidNumber', 'gidNumber', 'dn', 'objectclass', | |
'ipaNTSecurityIdentifier'], | |
base_dn=api.env.container_accounts + api.env.basedn) | |
except errors.NotFound: | |
# no objects in this range found, allow the command | |
return (0, []) | |
return num, entries | |
r = replication.ReplicationManager(api.env.realm, api.env.host, starttls=True, port=389) | |
r_next, r_max = r.get_DNA_range(api.env.host) | |
ldap = api.Backend.ldap2 | |
add_new_idrange = True | |
idranges = api.Command.idrange_find(iparangetype=u'ipa-local')['result'] | |
for idrange in idranges: | |
ipabaseid = int(idrange.get(u'ipabaseid', ['0'])[0]) | |
ipaidrangesize = int(idrange.get(u'ipaidrangesize', ['0'])[0]) | |
rangename = idrange.get(u'cn')[0] | |
# Check whether this range corresponds to our DNA range already | |
if r_next >= ipabaseid and r_max <= ipabaseid + ipaidrangesize - 1: | |
print("Range '{}' ({}-{}) fits " | |
"DNA range ({}-{})".format( | |
rangename, ipabaseid, ipabaseid + ipaidrangesize - 1, r_next, r_max)) | |
add_new_idrange = False | |
continue | |
num, entries = check_ids_in_modified_range(ldap, ipabaseid, ipaidrangesize, 0, 0) | |
if num == 0: | |
print("Range '{}' has no allocated entries and can be removed".format(rangename)) | |
continue | |
print("Range '{}' has IDs in following entries:".format(rangename)) | |
for e in entries: | |
attrs = [] | |
for a in (u'uidnumber', u'gidnumber', u'ipantsecurityidentifier'): | |
v = e.get(a) | |
if v: | |
attrs.append("{}: {}".format(a, ",".join(v))) | |
print("\t{} [{}]".format(e.dn, ", ".join(attrs))) | |
if add_new_idrange: | |
print("A new ID range needs to be created for DNA range {}-{}".format(r_next, r_max)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When FreeIPA replica is installed, DNA range is not allocated to it automatically. The range is only cut from the master's DNA range when a replica really needs to get the IDs allocated. E.g. when a user or a group is being created. When this new DNA range appears on replica side, we ideally need to allocate ID range with the same parameters to be able to issue SIDs for the objects which received IDs from DNA plugin. But that is not happening, so admins need to allocate ID ranges manually.
When such situation arises, it is good to see what DNA ranges aren't mapped to ID ranges yet and which objects are there in additional ID ranges already.
Basic deployment use:
If several ID ranges were added:
And users created utilizing on of those ranges:
Then running the script will produce information like this: