예제 #1
0
        public void StoreDC(DomainControllerHelper dcHelper, string domainDN, bool force = false)
        {
            if (dcHelper.Success)
            {
                ForestBase.DCList.AddSafe(dcHelper.Name, dcHelper, force);

                if ((!ForestBase.DCList[dcHelper.Name].UDPPingSuccess) && (dcHelper.UDPPingSuccess))
                {
                    ForestBase.DCList[dcHelper.Name] = dcHelper;
                }

                ForestBase.DomainDCs.AddSafe(domainDN, new List <string> {
                });

                ForestBase.DomainDCs[domainDN].AddSafe(dcHelper.Name, force);

                if (dcHelper.UDPPingSuccess)
                {
                    foreach (string nc in dcHelper.NamingContexts)
                    {
                        ForestBase.NCsToDCs.AddSafe(nc, new List <string> {
                        });

                        ForestBase.NCsToDCs[nc].AddSafe(dcHelper.Name, force);
                    }

                    ForestBase.NCsToDCs = ForestBase.NCsToDCs.OrderByKeyLengthSafe();
                }
            }

            else
            { /* tbd fire fatal event */
            }
        }
예제 #2
0
        private static void HandleError(int rc, ref DomainControllerHelper dch, string domainDNS, string siteName, string flags)
        {
            dch.Success = false;

            dch.ErrorString = "HResult: " + rc;

            switch (rc)
            {
            case 8:
                dch.ErrorString = string.Format("{0} (Not enough storage is available to process this command!)", dch.ErrorString);
                break;

            case 997:
                dch.ErrorString = string.Format("{0} (No DC in domain: {1}, site: {2} answered!)", dch.ErrorString, Convert.ToString(domainDNS), Convert.ToString(siteName));
                break;

            case 1004:
                dch.ErrorString = string.Format("{0} (Invalid flags: {1}!", dch.ErrorString, flags);
                break;

            case 1211:
                dch.ErrorString = string.Format("{0} (The format of the specified domain name is invalid: {1}!)", dch.ErrorString, Convert.ToString(domainDNS));
                break;

            case 1355:
                dch.ErrorString = string.Format("{0} (The specified domain either does not exist or could not be contacted: {1}!)", dch.ErrorString, Convert.ToString(domainDNS));
                break;

            default:
                dch.ErrorString = string.Format("{0} (Unknown error!)", dch.ErrorString);
                break;
            }
        }
예제 #3
0
        public Dictionary <string, string> SubNCsToDCs(string nc, string currentDC)
        {
            Dictionary <string, string> ret = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase)
            {
            };

            Dictionary <string, DomainControllerHelper> temp = new Dictionary <string, DomainControllerHelper>(ForestBase.DCList);

            foreach (DomainControllerHelper dc in temp.Values)
            {
                if (!dc.UDPPingSuccess)
                {
                    DomainControllerHelper tempdc = DomainControllerHelper.UDPPing(dc.Name);

                    StoreDC(tempdc, tempdc.DefaultNamingContext);
                }
            }

            foreach (KeyValuePair <string, List <string> > ncinfo in ForestBase.NCsToDCs.Where(n => n.Key.ToLowerInvariant().EndsWith(nc.ToLowerInvariant())))
            {
                if (ncinfo.Value.Contains(currentDC))
                {
                    ret.AddSafe(ncinfo.Key, currentDC);
                }

                else
                {
                    ret.AddSafe(ncinfo.Key, ncinfo.Value[0]);
                }
            }

            return(ret);
        }
예제 #4
0
        public static DomainControllerHelper GetPDC(string domainDNS)
        {
            DomainControllerHelper scRet = new DomainControllerHelper();

            scRet = GetDC(domainDNS, null, NetApi32.DS_FLAGS.DS_FORCE_REDISCOVERY | NetApi32.DS_FLAGS.DS_DIRECTORY_SERVICE_REQUIRED | NetApi32.DS_FLAGS.DS_RETURN_DNS_NAME | NetApi32.DS_FLAGS.DS_WRITABLE_REQUIRED | NetApi32.DS_FLAGS.DS_PDC_REQUIRED);

            return(scRet);
        }
예제 #5
0
        public static DomainControllerHelper GetClostesGC(string domainDNS)
        {
            DomainControllerHelper scRet = new DomainControllerHelper();

            scRet = GetDC(domainDNS, null, NetApi32.DS_FLAGS.DS_FORCE_REDISCOVERY | NetApi32.DS_FLAGS.DS_DIRECTORY_SERVICE_REQUIRED | NetApi32.DS_FLAGS.DS_RETURN_DNS_NAME | NetApi32.DS_FLAGS.DS_TRY_NEXTCLOSEST_SITE | NetApi32.DS_FLAGS.DS_GC_SERVER_REQUIRED);

            return(scRet);
        }
예제 #6
0
        public static DomainControllerHelper GetCachedWDC(string domainDNS)
        {
            DomainControllerHelper scRet = new DomainControllerHelper();

            scRet = GetDC(domainDNS, null, NetApi32.DS_FLAGS.DS_BACKGROUND_ONLY | NetApi32.DS_FLAGS.DS_DIRECTORY_SERVICE_REQUIRED | NetApi32.DS_FLAGS.DS_RETURN_DNS_NAME | NetApi32.DS_FLAGS.DS_WRITABLE_REQUIRED);

            return(scRet);
        }
예제 #7
0
        public DomainControllerHelper GetDC(string domainDNS)
        {
            DomainControllerHelper ret = default(DomainControllerHelper);

            ret = DCLocator.GetClostesDC(domainDNS);

            return(ret);
        }
        public static DomainControllerHelper FromStruct(NetApi32.DOMAIN_CONTROLLER_INFO dcInfo)
        {
            DomainControllerHelper ret = new DomainControllerHelper()
            {
                Name = dcInfo.DomainControllerName.Replace("\\\\", null), FromUDPPing = false
            };

            //ret.Load(UDPAttributes);

            return(ret);
        }
        public static DomainControllerHelper UDPPing(string dcName = null)
        {
            DomainControllerHelper ret = new DomainControllerHelper()
            {
                Name = dcName, FromUDPPing = true
            };

            ret.Load(UDPAttributes);

            return(ret);
        }
예제 #10
0
        public static DomainControllerHelper GetDC(string domainDNS, string siteName, NetApi32.DS_FLAGS flags)
        {
            DomainControllerHelper ret = new DomainControllerHelper()
            {
                Success = false
            };

            int    iret   = -1;
            IntPtr ipinfo = IntPtr.Zero;

            NetApi32.DOMAIN_CONTROLLER_INFO stcinfo = default(NetApi32.DOMAIN_CONTROLLER_INFO);

            if (domainDNS == null)
            {
                domainDNS = String.Empty;
            }

            try
            { iret = NetApi32.DsGetDcName(null, domainDNS, new Guid(), siteName, flags, ref ipinfo); }

            catch (Win32Exception Win32Ex)
            { ret.ErrorString = Win32Ex.ErrorCode + " (Win32: " + Win32Ex.Message + ")"; }

            catch (Exception ex)
            { ret.ErrorString = ex.Message; }


            if (iret == 0)
            {
                ret.Success = true;

                stcinfo = (NetApi32.DOMAIN_CONTROLLER_INFO)Marshal.PtrToStructure(ipinfo, typeof(NetApi32.DOMAIN_CONTROLLER_INFO));

                NetApi32.NetApiBufferFree(ipinfo);

                ret = DomainControllerHelper.FromStruct(stcinfo);
            }

            else
            {
                HandleError(iret, ref ret, domainDNS, siteName, flags.ToString());
            }

            return(ret);
        }
예제 #11
0
        public static DomainControllerHelper GetSiteWDC(string callInfo)
        {
            DomainControllerHelper scRet = new DomainControllerHelper();
            string domainDNS             = null;
            string SiteName = null;


            if (callInfo.IndexOf(" ") > 0)
            {
                SiteName = callInfo.Split(Convert.ToChar(" "))[0];

                domainDNS = callInfo.Split(Convert.ToChar(" "))[1];
            }

            else
            {
                SiteName = callInfo;
            }

            scRet = GetDC(domainDNS, SiteName, NetApi32.DS_FLAGS.DS_FORCE_REDISCOVERY | NetApi32.DS_FLAGS.DS_FORCE_REDISCOVERY | NetApi32.DS_FLAGS.DS_DIRECTORY_SERVICE_REQUIRED | NetApi32.DS_FLAGS.DS_RETURN_DNS_NAME | NetApi32.DS_FLAGS.DS_WRITABLE_REQUIRED);

            return(scRet);
        }
예제 #12
0
        public static List <DomainControllerHelper> DnsGetDCs(string domainDNS, string siteName, string forestName, NetApi32.DS_OPEN_FLAGS flags, NetApi32.DS_OPTION_FLAGS siteOptions, bool doUdpPing = false)
        {
            List <DomainControllerHelper> ret = new List <DomainControllerHelper> {
            };

            DomainControllerHelper errdc = new DomainControllerHelper()
            {
                Success = false
            };

            int iret = -1;

            IntPtr iphandle = IntPtr.Zero;

            try
            {
                if (domainDNS == null)
                {
                    domainDNS = Environment.ExpandEnvironmentVariables("%USERDNSDOMAIN%");
                }

                iret = NetApi32.DsGetDcOpen(domainDNS, siteOptions, siteName, IntPtr.Zero, forestName, flags, out iphandle);
            }

            catch (Win32Exception Win32Ex)
            { errdc.ErrorString = Win32Ex.ErrorCode + " (Win32: " + Win32Ex.Message + ")"; }

            catch (Exception ex)
            { errdc.ErrorString = ex.Message; }

            if (iret == 0)
            {
                string dnsName = null;

                IntPtr socketcnt = IntPtr.Zero;
                IntPtr sockets   = IntPtr.Zero;

                while ((iret == (int)NetApi32.ERROR_LIST.ERROR_SUCCESS) || (iret == (int)NetApi32.ERROR_LIST.ERROR_FILEMARK_DETECTED))
                {
                    errdc = new DomainControllerHelper()
                    {
                        Success = false
                    };

                    try
                    { iret = NetApi32.DsGetDcNext(iphandle, ref socketcnt, out sockets, out dnsName); }

                    catch (Win32Exception Win32Ex)
                    { errdc.ErrorString = Win32Ex.ErrorCode + " (Win32: " + Win32Ex.Message + ")"; }

                    catch (Exception ex)
                    { errdc.ErrorString = ex.Message; }

                    if ((iret == (int)NetApi32.ERROR_LIST.ERROR_SUCCESS) || (iret == (int)NetApi32.ERROR_LIST.ERROR_FILEMARK_DETECTED))
                    {
                        if (doUdpPing)
                        {
                            DateTime start = DateTime.Now;

                            GlobalEventHandler.RaiseMessageOccured(String.Format("UDPPing against {0} ({1})", dnsName, domainDNS));

                            ret.Add(DomainControllerHelper.UDPPing(dnsName));

                            double dur = DateTime.Now.Subtract(start).TotalMilliseconds;

                            GlobalEventHandler.RaiseMessageOccured(String.Format("UDPPing {0} in {1} ms", dnsName, dur.ToString()));
                        }

                        else
                        {
                            ret.Add(new DomainControllerHelper(domainDNS, dnsName));
                        }
                    }

                    else
                    {
                        if (ret.Count == 0)
                        {
                            HandleError(iret, ref errdc, domainDNS, siteName, flags.ToString());

                            ret.Add(errdc);
                        }
                    }
                }
            }

            else
            {
                if (ret.Count == 0)
                {
                    HandleError(iret, ref errdc, domainDNS, siteName, flags.ToString());

                    ret.Add(errdc);
                }
            }

            if (iphandle != IntPtr.Zero)
            {
                NetApi32.DsGetDcClose(iphandle);
            }

            return(ret);
        }