Skip to content

Instantly share code, notes, and snippets.

@jfautley
Created April 24, 2019 08:46
Show Gist options
  • Save jfautley/4643ab8f6078253a87a2c71489bb85ee to your computer and use it in GitHub Desktop.
Save jfautley/4643ab8f6078253a87a2c71489bb85ee to your computer and use it in GitHub Desktop.
Calculate SSSD ldap_id_mapping ID from AD Domain SID. Likely full of bugs. Doesn't rely on NSS/SSSD being installed, as its a reimplementation of the generation algorithm in Python.
#!/usr/bin/python
import sys
import mmh3
# https://pagure.io/SSSD/sssd/blob/master/f/src/lib/idmap/sss_idmap_private.h
IDMAP_UPPER = 2000200000
IDMAP_LOWER = 200000
IDMAP_RANGE = 200000
max_slices = (IDMAP_UPPER - IDMAP_LOWER) / IDMAP_RANGE
if len(sys.argv) > 1:
sid = sys.argv[1]
else:
print("Usage: %s <SID>" % sys.argv[0])
sys.exit(1)
print("SID : %s" % sid)
splitsid = sid.rpartition('-')
domain = splitsid[0]
rid = int(splitsid[2])
# Generate MurmurHash3 of Domain SID
domain_hash = mmh3.hash(domain, 0xdeadbeef, signed=False)
# Generate slice
slice = domain_hash % max_slices
# Calculate offset
offset = (slice * IDMAP_RANGE) + IDMAP_LOWER
# Print some info
print("Domain Hash : %d" % domain_hash)
print("Domain Slice: %d" % slice)
print("Base offset : %d" % offset)
# Calculate and print ID
print("ID : %d" % (offset + rid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment