@@ -376,39 +376,35 @@ def backup(self, context):
376376 context .log .highlight ("DACL backed up to %s" , self .filename )
377377 self .filename = None
378378
379- # Attempts to retrieve the SID and Distinguisehd Name from the sAMAccountName
380- # Not used for the moment
381- # - samname : a sAMAccountName
382- def get_user_info (self , context , samname ):
383- self .ldap_session .search (
384- searchBase = self .baseDN ,
385- searchFilter = f"(sAMAccountName={ escape_filter_chars (samname )} )" ,
386- attributes = ["objectSid" ],
387- )
379+ def get_user_info (self , context , sAMAccountName ):
380+ """Retrieves the SID and Distinguished Name from a sAMAccountName."""
388381 try :
389- dn = self .ldap_session .entries [0 ].entry_dn
390- sid = format_sid (self .ldap_session .entries [0 ]["objectSid" ].raw_values [0 ])
391- return dn , sid
382+ resp = self .connection .search (
383+ searchFilter = f"(sAMAccountName={ escape_filter_chars (sAMAccountName )} )" ,
384+ attributes = ["distinguishedName" , "objectSid" ],
385+ )
386+ resp_parsed = parse_result_attributes (resp )[0 ]
387+ return resp_parsed ["distinguishedName" ], resp_parsed ["objectSid" ]
392388 except Exception :
393- context .log .fail (f"User not found in LDAP: { samname } " )
389+ context .log .fail (f"User not found in LDAP: { sAMAccountName } " )
394390 return False
395391
396392 def resolveSID (self , sid ):
397393 """Resolves a SID to its corresponding sAMAccountName."""
398394 # Tries to resolve the SID from the well known SIDs
399395 if sid in WELL_KNOWN_SIDS :
400396 return WELL_KNOWN_SIDS [sid ]
397+
401398 # Tries to resolve the SID from the LDAP domain dump
402- else :
403- try :
404- resp = self .connection .search (
405- searchFilter = f"(objectSid={ sid } )" ,
406- attributes = ["sAMAccountName" ],
407- )
408- return parse_result_attributes (resp )[0 ]["sAMAccountName" ]
409- except Exception :
410- self .context .log .debug (f"SID not found in LDAP: { sid } " )
411- return ""
399+ try :
400+ resp = self .connection .search (
401+ searchFilter = f"(objectSid={ sid } )" ,
402+ attributes = ["sAMAccountName" ],
403+ )
404+ return parse_result_attributes (resp )[0 ]["sAMAccountName" ]
405+ except Exception :
406+ self .context .log .debug (f"SID not found in LDAP: { sid } " )
407+ return ""
412408
413409 # Parses a full DACL
414410 # - dacl : the DACL to parse, submitted in a Security Desciptor format
0 commit comments