private static NtResult <IEnumerable <SidName> > LookupSids2(string system_name, Sid[] sids, LsaLookupOptions options, bool throw_on_error) { using (var policy = SafeLsaHandle.OpenPolicy(system_name, Policy.LsaPolicyAccessRights.LookupNames, throw_on_error)) { if (!policy.IsSuccess) { return(policy.Cast <IEnumerable <SidName> >()); } using (var list = new DisposableList()) { var sid_ptrs = sids.Select(s => list.AddSid(s).DangerousGetHandle()).ToArray(); var status = SecurityNativeMethods.LsaLookupSids2(policy.Result, options, sid_ptrs.Length, sid_ptrs, out SafeLsaMemoryBuffer domains, out SafeLsaMemoryBuffer names); if (!status.IsSuccess()) { if (status == NtStatus.STATUS_NONE_MAPPED) { list.Add(domains); list.Add(names); } return(status.CreateResultFromError <IEnumerable <SidName> >(throw_on_error)); } return(GetSidNames(sids, domains, names).CreateResult()); } } }
private static NtResult <IReadOnlyList <SidName> > LookupSids(IEnumerable <Sid> sids, LookupSidsDelegate func, bool throw_on_error) { using (var list = new DisposableList()) { var sid_ptrs = sids.Select(s => list.AddSid(s).DangerousGetHandle()).ToArray(); var status = func(sid_ptrs, out SafeLsaMemoryBuffer domains, out SafeLsaMemoryBuffer names); if (!status.IsSuccess()) { if (status == NtStatus.STATUS_NONE_MAPPED) { list.Add(domains); list.Add(names); } return(status.CreateResultFromError <IReadOnlyList <SidName> >(throw_on_error)); } list.Add(domains); list.Add(names); return(GetSidNames(sids.ToArray(), domains, names).CreateResult()); } }
/// <summary> /// Get alias membership for a set of SIDs. /// </summary> /// <param name="sids">The SIDs to check.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The alias enumeration.</returns> public NtResult <IReadOnlyList <SamRidEnumeration> > GetAliasMembership(IEnumerable <Sid> sids, bool throw_on_error) { using (var list = new DisposableList()) { var alias_list = EnumerateAliases(throw_on_error); if (!alias_list.IsSuccess) { return(alias_list); } var sid_ptrs = sids.Select(s => list.AddSid(s).DangerousGetHandle()).ToArray(); return(SecurityNativeMethods.SamGetAliasMembership(Handle, sid_ptrs.Length, sid_ptrs, out int count, out SafeSamMemoryBuffer aliases).CreateResult <IReadOnlyList <SamRidEnumeration> >(throw_on_error, () => { using (aliases) { aliases.Initialize <uint>((uint)count); var membership = new HashSet <uint>(aliases.ReadArray <uint>(0, count)); return alias_list.Result.Where(m => membership.Contains(m.RelativeId)).ToList().AsReadOnly(); } })); } }