//find the DN of given the groupsid public static string SearchByGuid(string objectGuid, DirectoryContext dirContext) { int ret = -1; string searchFilter = objectGuid; searchFilter = string.Concat("(objectGuid=", objectGuid, ")"); List <LdapEntry> ldapEntries = null; ret = dirContext.ListChildEntriesSynchronous( dirContext.RootDN, LdapAPI.LDAPSCOPE.SUB_TREE, searchFilter, null, false, out ldapEntries); if (ldapEntries == null || ldapEntries.Count == 0) { Logger.Log("ldapEntries.Count == 0"); return(null); } LdapEntry ldapNextEntry = ldapEntries[0]; if (ldapNextEntry != null) { string[] attrsList = ldapNextEntry.GetAttributeNames(); if (attrsList != null) { foreach (string attr in attrsList) { if (attr.Equals("distinguishedName", StringComparison.InvariantCultureIgnoreCase)) { LdapValue[] attrValues = ldapNextEntry.GetAttributeValues(attr, dirContext); if (attrValues != null && attrValues.Length > 0) { return(attrValues[0].stringData); } } } } } return(null); }
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 static LDAPSchemaCache Build(DirectoryContext ldapContext) { DateTime BuildTimer = DateTime.Now; LDAPSchemaCache result = null; List <LdapEntry> ldapEntries = new List <LdapEntry>(); Dictionary <string, LdapEntry> ldapEntriesDic = new Dictionary <string, LdapEntry>(); string[] search_attrs = { null }; DateTime timer = Logger.StartTimer(); Logger.Log( "Started building schema cache", Logger.ldapLogLevel); int ret = ldapContext.ListChildEntriesSynchronous( string.Concat("cn=Schema,", ldapContext.ConfigurationNamingContext), LdapAPI.LDAPSCOPE.SUB_TREE, "(objectClass=*)", search_attrs, false, out ldapEntries); if (ldapEntries != null && ldapEntries.Count > 0) { Logger.TimerMsg(ref timer, String.Format( "schema cache: {0} entries read from LDAP", ldapEntries.Count)); result = new LDAPSchemaCache(); result.rootDN = ldapContext.RootDN; int entryIndex = 0; foreach (LdapEntry entry in ldapEntries) { LdapValue[] attrName = entry.GetAttributeValues("lDAPDisplayName", ldapContext); if (attrName != null) { ldapEntriesDic.Add(attrName[0].stringData, entry); } } foreach (LdapEntry ldapNextEntry in ldapEntries) { AttributeMap attrmap = new AttributeMap(); string dn = ldapNextEntry.GetDN(); Logger.Log(String.Format( "LDAPSchemaCache.Build: ldapEntry[{0}]: DN = " + dn, entryIndex++), Logger.ldapLogLevel); //Getting 'objectClasss' value instead 'subClassOf' attribute //becuase 'subClassOf' attribute is not giving the all mandatory attribute for the selected object. LdapValue[] objectClasses = ldapNextEntry.GetAttributeValues("objectClass", ldapContext); foreach (LdapValue Oclass in objectClasses) { if (ldapEntriesDic.ContainsKey(Oclass.stringData)) { attrmap.ObjectClasses.Add(Oclass.stringData, ldapEntriesDic[Oclass.stringData]); } } attrmap.ldapContext = ldapContext; attrmap.Tag = ldapNextEntry; string[] attrs = ldapNextEntry.GetAttributeNames(); foreach (string attr in attrs) { LdapValue[] values = ldapNextEntry.GetAttributeValues(attr, ldapContext); attrmap.Add(attr, values); } result.AddSchemaType(attrmap); } Logger.TimerMsg(ref timer, String.Format( "LDAPSchemaCache.Build({0})-- finished building the schemaCache", ldapContext.RootDN)); result.reportSchemaCache(Logger.ldapLogLevel); } return(result); }
//- RootDSE -> configurationNamingContext //- cn=partition, "configurationNamingContext" -> netBIOSName //Using the value of "netBIOSName" as shortDomain name. public string GetnetBiosName(string domain) { int ret = -1; string configurationName = null; string netbiosName = null; string baseDn = ""; string[] search_attrs = { null }; DirectoryContext dircontext = null; if (exisitngDirContext != null && exisitngDirContext.Count > 0) { foreach (DirectoryContext context in exisitngDirContext) { if (context.DomainName.Equals(domain, StringComparison.InvariantCultureIgnoreCase)) { dircontext = context; } } } //searching with baseDn="" allows ldap to access the domain “RootDSE”. //Without passing that, it cannot access the configurationNamingContext List <LdapEntry> ldapEntries = null; ret = dircontext.ListChildEntriesSynchronous (baseDn, LdapAPI.LDAPSCOPE.BASE, "(objectClass=*)", search_attrs, false, out ldapEntries); if (ldapEntries != null && ldapEntries.Count > 0) { LdapEntry ldapNextEntry = ldapEntries[0]; LdapValue[] values = ldapNextEntry.GetAttributeValues("configurationNamingContext", dircontext); if (values != null && values.Length > 0) { configurationName = values[0].stringData; } } //by default, if we couldn't find configurateName we use CN=configuration + rootDN as one if (configurationName == null) { configurationName = "CN=configuration,"; configurationName = string.Concat(configurationName, dircontext.RootDN); } string partitionDn = "CN=Partitions,"; partitionDn = string.Concat(partitionDn, configurationName); string sFilter = "(&(objectcategory=Crossref)(dnsRoot=" + domain.ToLower() + ")(netBIOSName=*))"; List <LdapEntry> ldapEntries1 = null; ret = dircontext.ListChildEntriesSynchronous (partitionDn, LdapAPI.LDAPSCOPE.SUB_TREE, sFilter, search_attrs, false, out ldapEntries1); if (ldapEntries1 != null && ldapEntries1.Count > 0) { LdapEntry ldapNextEntry = ldapEntries1[0]; LdapValue[] values = ldapNextEntry.GetAttributeValues("netBIOSName", dircontext); netbiosName = values[0].stringData; } //by default, if we couldn't find netbiosName we use the first portion of rootDn as one if (netbiosName == null) { string[] rootDns = dircontext.RootDN.Split(','); netbiosName = rootDns[0].Substring(3).ToUpper(); } return(netbiosName); }