Esempio n. 1
0
        bool LocateSomething(string intput, out string domain, out string forest, uint flag)
        {
            IntPtr DomainInfoResolution;

            domain = null;
            forest = null;
            Trace.WriteLine("Trying to solve " + intput + "(" + DateTime.Now.ToString("u") + ")");

            int ret = NativeMethods.DsGetDcName(Server, intput, IntPtr.Zero, null, flag, out DomainInfoResolution);

            if (ret == 0)
            {
                Trace.WriteLine("DsGetDcName for " + intput + " succeeded (" + DateTime.Now.ToString("u") + ")");
                NativeMethods.DOMAIN_CONTROLLER_INFO di = (NativeMethods.DOMAIN_CONTROLLER_INFO)Marshal.PtrToStructure(DomainInfoResolution, typeof(NativeMethods.DOMAIN_CONTROLLER_INFO));
                domain = di.DomainName.ToLowerInvariant();
                forest = di.DnsForestName.ToLowerInvariant();
                NativeMethods.NetApiBufferFree(DomainInfoResolution);
                return(true);
            }
            else if (ret == 0x0000054B)
            {
                Trace.WriteLine("DsGetDcName for " + intput + " domain not found (" + DateTime.Now.ToString("u") + ")");
            }
            else
            {
                Trace.WriteLine("DsGetDcName for " + intput + " failed 0x" + ret.ToString("x") + " (" + DateTime.Now.ToString("u") + ")");
            }
            return(false);
        }
        /// <summary>
        /// Determines the domain and the LDAP paths for Central Policy and Central Access Rule in AD
        /// to facilitate subsequent queries.
        /// </summary>
        /// <param name="target">Name of the computer from which to query</param>
        public AvailableCentralPolicies(string target)
        {
            PDOMAIN_CONTROLLER_INFO dsInfoPtr = PDOMAIN_CONTROLLER_INFO.Zero;

            try
            {
                DWORD result = NativeMethods.DsGetDcName(target,
                                                         null,
                                                         PGUID.Zero,
                                                         null,
                                                         NativeMethods.DsGetDcNameFlags.DirectoryServiceRequired,
                                                         out dsInfoPtr);
                if (result != Win32Error.ERROR_SUCCESS)
                {
                    throw new Win32Exception((int)result);
                }

                NativeMethods.DOMAIN_CONTROLLER_INFO dsInfo = (NativeMethods.DOMAIN_CONTROLLER_INFO)
                                                              Marshal.PtrToStructure(
                    dsInfoPtr,
                    typeof(NativeMethods.DOMAIN_CONTROLLER_INFO));

                string domainDN = "DC=" + string.Join(",DC=", dsInfo.domainName.Split('.'));

                capContainerDN = "CN=Central Access Policies," +
                                 "CN=Claims Configuration," +
                                 "CN=Services," +
                                 "CN=Configuration," +
                                 domainDN;

                availableCaps = GetAvailableCaps(target);
            }
            finally
            {
                NativeMethods.NetApiBufferFree(dsInfoPtr);
                dsInfoPtr = PDOMAIN_CONTROLLER_INFO.Zero;
            }
        }