private void GetDCInfo(string domain)
        {
            CNetlogon.LWNET_DC_INFO DCInfo;

            if (String.IsNullOrEmpty(domain))
            {
                CNetlogon.GetCurrentDomain(out sDomain);
            }
            else
            {
                sDomain = domain;
            }

            uint netlogonError = CNetlogon.GetDCName(sDomain, 0, out DCInfo);

            if (netlogonError == 0 && !String.IsNullOrEmpty(DCInfo.DomainControllerName))
            {
                sDomainController = DCInfo.DomainControllerName;
            }

            if (netlogonError == 0 && !String.IsNullOrEmpty(DCInfo.FullyQualifiedDomainName))
            {
                sDomain = DCInfo.FullyQualifiedDomainName;
            }
        }
Beispiel #2
0
        public uint GetDCName(
            string domainFQDN,
            uint flags,
            out CNetlogon.LWNET_DC_INFO DCInfo
            )
        {
            CNetlogon.LWNET_DC_INFO info = new CNetlogon.LWNET_DC_INFO();
            DCInfo = info;
            IntPtr pDCInfo = IntPtr.Zero;
            uint   error   = 0;

            try
            {
                Logger.Log(String.Format("GetDCName({0}, {1}) called",
                                         domainFQDN,
                                         flags),
                           Logger.NetlogonLogLevel);

                error = NetlogonAPI.LWNetGetDCName(
                    null,
                    domainFQDN,
                    null,
                    flags,
                    out pDCInfo);

                DCInfo = (CNetlogon.LWNET_DC_INFO)Marshal.PtrToStructure(pDCInfo, typeof(CNetlogon.LWNET_DC_INFO));

                Logger.Log(String.Format("GetDCName() returned {0}, error={1}",
                                         CNetlogon.LWNETDCInfoToString(DCInfo), error),
                           Logger.NetlogonLogLevel);
            }

            catch (Exception e)
            {
                Logger.LogException("Netlogon.GetDCName", e);
                if (error == 0)
                {
                    error = CNetlogon.Definitions.LWNET_ERROR_INTEROP_EXCEPTION;
                }
            }

            return(error);
        }
Beispiel #3
0
 private static void GetUserDomain(out string domain)
 {
     domain = string.Empty;
     try
     {
         string domainName = string.Empty;
         CNetlogon.LWNET_DC_INFO DCInfo;
         uint netlogonError = CNetlogon.GetDCName(domainName, 0, out DCInfo);
         if (netlogonError == 0 && !String.IsNullOrEmpty(DCInfo.DomainControllerName))
         {
             domain = DCInfo.FullyQualifiedDomainName;
         }
     }
     catch (Exception ex)
     {
         Logger.Log("Exception occured while getting DCInfo ," + ex.Message);
         return;
     }
 }
Beispiel #4
0
        public static void ReadRemoteHostFQDN(string hostname, out string hostFQDN)
        {
            hostFQDN = string.Empty; string domain = string.Empty;

            uint error = CNetlogon.GetCurrentDomain(out domain);

            if (error != 0 && String.IsNullOrEmpty(domain))
            {
                return;
            }

            string[] rootDNcom = domain.Split('.');

            string rootDN = ""; string errorMessage = "";

            foreach (string str in rootDNcom)
            {
                string temp = string.Concat("dc=", str, ",");
                rootDN = string.Concat(rootDN, temp);
            }
            rootDN = rootDN.Substring(0, rootDN.Length - 1);

            try
            {
                DirectoryContext dirContext = DirectoryContext.CreateDirectoryContext
                                                  (domain,
                                                  rootDN,
                                                  null,
                                                  null,
                                                  389,
                                                  false,
                                                  out errorMessage);

                if (!String.IsNullOrEmpty(errorMessage))
                {
                    Logger.ShowUserError(errorMessage);
                }

                if (dirContext == null)
                {
                    return;
                }

                List <LdapEntry> ldapEntries = new List <LdapEntry>();

                string[] attrs = { "name", "dNSHostName", null };

                int ret = dirContext.ListChildEntriesSynchronous
                              (rootDN,
                              LdapAPI.LDAPSCOPE.SUB_TREE,
                              string.Format("(&(objectClass=computer)(cn={0}))", hostname),
                              attrs,
                              false,
                              out ldapEntries);

                if (ldapEntries == null)
                {
                    return;
                }

                LdapEntry ldapNextEntry = ldapEntries[0];

                string[] attrsList = ldapNextEntry.GetAttributeNames();

                Logger.Log("The number of attributes are " + attrsList.Length, Logger.ldapLogLevel);

                if (attrsList != null)
                {
                    foreach (string attr in attrsList)
                    {
                        if (attr.Trim().Equals("dNSHostName"))
                        {
                            LdapValue[] attrValues = ldapNextEntry.GetAttributeValues(attr, dirContext);
                            if (attrValues != null && attrValues.Length > 0)
                            {
                                hostFQDN = attrValues[0].stringData;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                hostFQDN = string.Empty;
                Logger.LogException("EventAPI.ReadRemoteHostFQDN", ex);
            }
        }
        public uint GetDCName(
            string domainFQDN,
            uint flags,
            out CNetlogon.LWNET_DC_INFO DCInfo
            )
        {
            uint error = 0;

            CNetlogon.LWNET_DC_INFO info = new CNetlogon.LWNET_DC_INFO();
            NetlogonWindowsAPI.DOMAIN_CONTROLLER_INFO DCInfoWindows;
            DCInfo = info;
            IntPtr pDCInfo = IntPtr.Zero;

            try
            {
                Logger.Log(String.Format("GetDCNameWindows({0}, {1}) called",
                                         domainFQDN,
                                         flags),
                           Logger.NetlogonLogLevel);

                error = (uint)NetlogonWindowsAPI.DsGetDcName(
                    "",
                    "",
                    IntPtr.Zero,
                    "",
                    (int)flags,
                    out pDCInfo);

                if (error != 0)
                {
                    return((uint)error);
                }

                DCInfoWindows = (NetlogonWindowsAPI.DOMAIN_CONTROLLER_INFO)Marshal.PtrToStructure(pDCInfo, typeof(NetlogonWindowsAPI.DOMAIN_CONTROLLER_INFO));

                Logger.Log(String.Format("DsGetDcName() returned {0}, error={1}",
                                         DCINFOToString(DCInfoWindows), error),
                           Logger.NetlogonLogLevel);

                DCInfo.pucDomainGUID = new byte[CNetlogon.Definitions.LWNET_GUID_SIZE];

                DCInfo.DomainControllerName    = DCInfoWindows.DomainControllerName;
                DCInfo.DomainControllerAddress = DCInfoWindows.DomainControllerAddress;
                DCInfoWindows.DomainGuid.ToByteArray().CopyTo(DCInfo.pucDomainGUID, 0);
                DCInfo.FullyQualifiedDomainName = DCInfoWindows.DomainName;
                DCInfo.DnsForestName            = DCInfoWindows.DnsForestName;
                DCInfo.Flags          = (uint)DCInfoWindows.Flags;
                DCInfo.DCSiteName     = DCInfoWindows.DcSiteName;
                DCInfo.ClientSiteName = DCInfoWindows.ClientSiteName;


                if (DCInfo.DomainControllerName.StartsWith(@"\\"))
                {
                    DCInfo.DomainControllerName = DCInfo.DomainControllerName.Substring(2);
                }

                if (DCInfo.DomainControllerAddress.StartsWith(@"\\"))
                {
                    DCInfo.DomainControllerAddress = DCInfo.DomainControllerAddress.Substring(2);
                }

                Logger.Log(String.Format("GetDCNameWindows() returning {0}, error={1}",
                                         CNetlogon.LWNETDCInfoToString(DCInfo), error),
                           Logger.LogLevel.Debug);


                error = NetlogonWindowsAPI.NetApiBufferFree(pDCInfo);

                if (error != 0)
                {
                    Logger.Log("NetApiBufferFree() in GetDCNameWindows() failed!",
                               Logger.LogLevel.Error);
                }
            }
            catch (Exception e)
            {
                Logger.LogException("Netlogon.GetDCName", e);
                if (error == 0)
                {
                    error = CNetlogon.Definitions.LWNET_ERROR_INTEROP_EXCEPTION;
                }
                if (!IntPtr.Zero.Equals(pDCInfo))
                {
                    uint error2 = NetlogonWindowsAPI.NetApiBufferFree(pDCInfo);
                    if (error2 != 0)
                    {
                        Logger.Log("Netlogon.DCName: NetAPiBufferFree failed: " + error2);
                    }
                }
            }

            return(error);
        }