Exemplo n.º 1
0
        public static DsNameResultItem CrackNames(DsNameFormat formatOffered, DsNameFormat formatDesired, string name, string dnsDomainName = null, int referralLevel = 0)
        {
            IntPtr hds = IntPtr.Zero;

            try
            {
                int result = NativeMethods.DsBind(null, dnsDomainName, out hds);
                if (result != 0)
                {
                    throw new DirectoryException("DsBind failed", new Win32Exception(result));
                }

                DsNameResultItem nameResult = NativeMethods.CrackNames(hds, DsNameFlags.DS_NAME_FLAG_TRUST_REFERRAL, formatOffered, formatDesired, name);

                switch (nameResult.Status)
                {
                case DsNameError.None:
                    return(nameResult);

                case DsNameError.NoMapping:
                    throw new NameMappingException($"The object name {name} was found in the global catalog, but could not be mapped to a DN. DsCrackNames returned NO_MAPPING");

                case DsNameError.TrustReferral:
                case DsNameError.DomainOnly:
                    if (!string.IsNullOrWhiteSpace(nameResult.Domain))
                    {
                        if (referralLevel < NativeMethods.DirectoryReferralLimit)
                        {
                            return(NativeMethods.CrackNames(formatOffered, formatDesired, name, nameResult.Domain, ++referralLevel));
                        }

                        throw new ReferralLimitExceededException("The referral limit exceeded the maximum configured value");
                    }

                    throw new ReferralFailedException($"A referral to the object name {name} was received from the global catalog, but no referral information was provided. DsNameError: {nameResult.Status}");

                case DsNameError.NotFound:
                    throw new ObjectNotFoundException($"The object name {name} was not found in the global catalog");

                case DsNameError.NotUnique:
                    throw new AmbiguousNameException($"There was more than one object with the name {name} in the global catalog");

                case DsNameError.Resolving:
                    throw new NameMappingException($"The object name {name} was not able to be resolved in the global catalog. DsCrackNames returned RESOLVING");

                case DsNameError.NoSyntacticalMapping:
                    throw new NameMappingException($"DsCrackNames unexpectedly returned DS_NAME_ERROR_NO_SYNTACTICAL_MAPPING for name {name}");

                default:
                    throw new NameMappingException($"An unexpected status was returned from DsCrackNames {nameResult.Status}");
                }
            }
            finally
            {
                if (hds != IntPtr.Zero)
                {
                    NativeMethods.DsUnBind(hds);
                }
            }
        }
Exemplo n.º 2
0
        private static DsNameResultItem[] CrackNames(
            SafeDsHandle hDS,
            DsNameFlags flags,
            DsNameFormat formatOffered,
            DsNameFormat formatDesired,
            string[] names)
        {
            PscxArgumentException.ThrowIfIsNullOrEmpty(names);

            IntPtr ptr = IntPtr.Zero;

            try
            {
                NativeMethods.EnforceSuccess(NativeMethods.DsCrackNames(
                                                 hDS,
                                                 flags,
                                                 formatOffered,
                                                 formatDesired,
                                                 names.Length,
                                                 names,
                                                 out ptr));

                DsNameResult result = Utils.PtrToStructure <DsNameResult>(ptr);

                IEnumerable <DsNameResultItem> items =
                    Utils.ReadNativeArray <DsNameResultItem>(result.rItems, result.cItems);

                return(new List <DsNameResultItem>(items).ToArray());
            }
            finally
            {
                NativeMethods.DsFreeNameResult(ptr);
            }
        }
Exemplo n.º 3
0
 public static extern int DsCrackNames(
     SafeDsHandle hDS,
     DsNameFlags flags,
     DsNameFormat formatOffered,
     DsNameFormat formatDesired,
     int cNames,
     string[] rpNames,
     out IntPtr ppResult //pointer to pointer of DS_NAME_RESULT
 );
Exemplo n.º 4
0
 public static extern int DsCrackNames(
     SafeDsHandle hDS,
     DsNameFlags flags,
     DsNameFormat formatOffered,
     DsNameFormat formatDesired,
     int cNames,
     string[] rpNames,
     out IntPtr ppResult //pointer to pointer of DS_NAME_RESULT
     );
Exemplo n.º 5
0
        private DirectoryEntry GetDirectoryEntry(string nameToFind, DsNameFormat nameFormat)
        {
            return(this.discoveryServices.FindDcAndExecuteWithRetry(dc =>
            {
                var result = NativeMethods.CrackNames(nameFormat, DsNameFormat.DistinguishedName, nameToFind, dc, null);

                return this.discoveryServices.FindDcAndExecuteWithRetry(result.Domain, dc2 =>
                {
                    var de = new DirectoryEntry($"LDAP://{dc2}/{result.Name}");
                    _ = de.Guid;
                    return de;
                });
            }));
        }
Exemplo n.º 6
0
        private static DsNameResultItem[] CrackNames(IntPtr hds, DsNameFlags flags, DsNameFormat formatOffered, DsNameFormat formatDesired, string[] namesToCrack)
        {
            IntPtr pDsNameResult = IntPtr.Zero;

            DsNameResultItem[] resultItems;

            try
            {
                uint namesToCrackCount = (uint)namesToCrack.Length;

                int result = NativeMethods.DsCrackNames(hds, flags, formatOffered, formatDesired, namesToCrackCount, namesToCrack, out pDsNameResult);

                if (result != 0)
                {
                    throw new DirectoryException("DsCrackNames failed", new Win32Exception(result));
                }

                DsNameResult dsNameResult = (DsNameResult)Marshal.PtrToStructure(pDsNameResult, typeof(DsNameResult));

                if (dsNameResult.cItems == 0)
                {
                    throw new DirectoryException("DsCrackNames returned an unexpected result");
                }

                resultItems = new DsNameResultItem[dsNameResult.cItems];
                IntPtr pItem = dsNameResult.rItems;

                for (int idx = 0; idx < dsNameResult.cItems; idx++)
                {
                    resultItems[idx] = (DsNameResultItem)Marshal.PtrToStructure(pItem, typeof(DsNameResultItem));
                    pItem            = IntPtr.Add(pItem, Marshal.SizeOf(resultItems[idx]));
                }
            }
            finally
            {
                if (pDsNameResult != IntPtr.Zero)
                {
                    NativeMethods.DsFreeNameResult(pDsNameResult);
                }
            }

            return(resultItems);
        }
Exemplo n.º 7
0
 private static DsNameResultItem CrackNames(IntPtr hds, DsNameFlags flags, DsNameFormat formatOffered, DsNameFormat formatDesired, string name)
 {
     DsNameResultItem[] resultItems = NativeMethods.CrackNames(hds, flags, formatOffered, formatDesired, new[] { name });
     return(resultItems[0]);
 }
Exemplo n.º 8
0
 private static extern int DsCrackNames(IntPtr hds, DsNameFlags flags, DsNameFormat formatOffered, DsNameFormat formatDesired, uint cNames, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPTStr, SizeParamIndex = 4)] string[] rpNames, out IntPtr ppResult);
Exemplo n.º 9
0
        public static DirectoryEntry GetDirectoryEntry(string nameToFind, DsNameFormat nameFormat, string server)
        {
            var result = CrackNames(nameFormat, DsNameFormat.DistinguishedName, nameToFind, server);

            return(new DirectoryEntry($"LDAP://{result.Domain}/{result.Name}"));
        }
Exemplo n.º 10
0
 public static DirectoryEntry GetDirectoryEntry(string nameToFind, DsNameFormat nameFormat)
 {
     return(GetDirectoryEntry(nameToFind, nameFormat, null));
 }
Exemplo n.º 11
0
        public static string GetDn(string nameToFind, DsNameFormat nameFormat, string server)
        {
            var result = CrackNames(nameFormat, DsNameFormat.DistinguishedName, nameToFind, server);

            return(result.Name);
        }
Exemplo n.º 12
0
 public static string GetDn(string nameToFind, DsNameFormat nameFormat)
 {
     return(GetDn(nameToFind, nameFormat, null));
 }
Exemplo n.º 13
0
 public string TranslateName(string name, DsNameFormat nameFormat, DsNameFormat requiredFormat, string dnsDomainName)
 {
     return(this.discoveryServices.FindDcAndExecuteWithRetry(dnsDomainName, dc => NativeMethods.CrackNames(nameFormat, requiredFormat, name, dc, dnsDomainName).Name));
 }
Exemplo n.º 14
0
 public string TranslateName(string name, DsNameFormat nameFormat, DsNameFormat requiredFormat)
 {
     return(this.TranslateName(name, nameFormat, requiredFormat, null));
 }
Exemplo n.º 15
0
 public string TranslateName(string name, DsNameFormat nameFormat, DsNameFormat requiredFormat, string dnsDomainName)
 {
     return(NativeMethods.CrackNames(nameFormat, requiredFormat, name, dnsDomainName).Name);
 }
Exemplo n.º 16
0
        private static DsNameResultItem[] CrackNames(
            SafeDsHandle hDS,
            DsNameFlags flags,
            DsNameFormat formatOffered,
            DsNameFormat formatDesired,
            string[] names)
        {
            PscxArgumentException.ThrowIfIsNullOrEmpty(names);

            IntPtr ptr = IntPtr.Zero;

            try
            {
                NativeMethods.EnforceSuccess(NativeMethods.DsCrackNames(
                    hDS,
                    flags,
                    formatOffered,
                    formatDesired,
                    names.Length,
                    names,
                    out ptr));

                DsNameResult result = Utils.PtrToStructure<DsNameResult>(ptr);

                IEnumerable<DsNameResultItem> items =
                    Utils.ReadNativeArray<DsNameResultItem>(result.rItems, result.cItems);

                return new List<DsNameResultItem>(items).ToArray();
            }
            finally
            {
                NativeMethods.DsFreeNameResult(ptr);
            }
        }