private void LoadDomains() { SearchRequest request = new SearchRequest(null, "(objectClass=domainDNS)", SearchScope.Subtree, new string[] { "canonicalName", "objectSid" }); PageResultRequestControl pagecookie = new PageResultRequestControl(1000); request.Controls.Add(pagecookie); SearchOptionsControl phantomcookie = new SearchOptionsControl(SearchOption.PhantomRoot); request.Controls.Add(phantomcookie); LdapDirectoryIdentifier ldapid = new LdapDirectoryIdentifier(ForestBase.DefaultDC.Name, 389, true, false); using (LdapConnector ldapcon = new LdapConnector(ldapid)) { if ((ForestBase.GivenCreds != null) && (ForestBase.GivenCreds.HasCreds)) { ldapcon.Credential = ForestBase.GivenCreds.NetCreds; } ldapcon.SessionOptions.ReferralChasing = ReferralChasingOptions.All; ldapcon.Bind(); SearchResponse response = (SearchResponse)ldapcon.SendRequest(request); foreach (SearchResultEntry entry in response.Entries) { if (entry.DistinguishedName.ToLowerInvariant() != ForestBase.RootDomainNamingContext.ToLowerInvariant()) { string domdns = null; entry.GetStringAttributeSafe("canonicalName", out domdns); domdns = domdns.TrimChar("/"); foreach (DomainControllerHelper dchelper in GetDcList(domdns, entry.DistinguishedName)) { StoreDC(dchelper, entry.DistinguishedName); } List <byte[]> bsids; entry.GetAttributeSafe("objectSid", out bsids); SecurityIdentifier dsid = new SecurityIdentifier(bsids[0], 0); ForestBase.DomainSids.AddSafe(entry.DistinguishedName, dsid.ToString()); } } } }
private void Connect(string dc, ReferralChasingOptions referralChasing, bool isAsync = false, bool connectionLess = false) { int port = ForestBase.CurrentPorts.SelectedPort; base.IsConnected = false; string inex = null; LdapConnector tempcon = null; try { LdapDirectoryIdentifier ldapid = new LdapDirectoryIdentifier(dc, port, true, connectionLess); tempcon = GetLdapConnection(ldapid, ForestBase.GivenCreds); tempcon.SessionOptions.ReferralChasing = referralChasing; tempcon.Timeout = TimeSpan.FromSeconds((double)ForestBase.CurrentTimeOut); //new TimeSpan(0, 2, 0); tempcon.SessionOptions.SendTimeout = TimeSpan.FromSeconds((double)ForestBase.CurrentTimeOut); tempcon.ServerTimeOut = TimeSpan.FromSeconds((double)ForestBase.CurrentTimeOut); if (!isAsync) { base.Connection = tempcon; } else { base.AsyncConnection = tempcon; } base.IsConnected = true; } catch (Exception ex) { inex = "Connect LDAP: " + ex.Message; } if (inex != null) { base.SetError(inex); } }
public bool ValidateCreds() { bool ret = false; LdapDirectoryIdentifier ldapid = null; LdapConnector ldapcheck = null; try { ldapid = new LdapDirectoryIdentifier(ForestName, false, false); ldapcheck = new LdapConnector(ldapid); ldapcheck.AutoBind = false; ldapcheck.AuthType = AuthType.Basic; ldapcheck.SessionOptions.FastConcurrentBind(); ldapcheck.Bind(NetCreds); ret = true; } catch (Exception ex) { ErrorMsg = String.Format("{0} ({1}\\{2})", ex.Message, DomainName, UserName); } finally { try { ldapcheck.Dispose(); } catch (Exception ex) { ex.ToDummy(); } } HasError = !ret; return(ret); }
private LdapConnector GetLdapCon(LdapDirectoryIdentifier ldapId, Credentials credInfo) { LdapConnector ret = null; try { if (credInfo.HasCreds) { ret = new LdapConnector(ldapId, credInfo.NetCreds); } else { ret = new LdapConnector(ldapId); } } catch (Exception ex) { base.SetError(String.Format("GetLdapCon: {0} ({1})", ex.Message, ex.GetType().Name)); } return(ret); }
private void LoadNCs(string dcName, string[] attributes) { Success = false; try { LdapDirectoryIdentifier ldapid = new LdapDirectoryIdentifier(dcName, 389, true, true); LdapConnector ldapcon = new LdapConnector(ldapid); ldapcon.Timeout = new TimeSpan(0, 2, 30); SearchRequest request = new SearchRequest("", "(objectClass=*)", SearchScope.Base, attributes); SearchResponse response = (SearchResponse)ldapcon.SendRequest(request); foreach (SearchResultEntry entry in response.Entries) { entry.GetStringAttributeSafe("defaultNamingContext", out defNC); entry.GetStringAttributeSafe("configurationNamingContext", out confNC); entry.GetStringAttributeSafe("schemaNamingContext", out schemaNC); entry.GetStringAttributeSafe("rootDomainNamingContext", out rootNC); entry.GetStringAttributeSafe("namingContexts", out allNCs); } Success = true; } catch (Exception ex) { ErrorString = ex.Message; } }
private bool LoadConfig() { bool ret = false; SearchRequest request = new SearchRequest(ForestBase.ConfigurationNamingContext, "(|(objectClass=nTDSDSA)(objectClass=queryPolicy))", SearchScope.Subtree, new string[] { "objectClass", "whenCreated", "queryPolicyObject", "lDAPAdminLimits" }); LdapDirectoryIdentifier ldapid = new LdapDirectoryIdentifier(ForestBase.DefaultDC.Name, 389, true, false); using (LdapConnector ldapcon = new LdapConnector(ldapid)) { try { if ((ForestBase.GivenCreds != null) && (ForestBase.GivenCreds.HasCreds)) { ldapcon.Credential = ForestBase.GivenCreds.NetCreds; } ldapcon.Bind(); SearchResponse response = (SearchResponse)ldapcon.SendRequest(request); List <SearchResultEntry> ntds = new List <SearchResultEntry> { }; List <QueryPolicy> policies = new List <QueryPolicy> { }; foreach (SearchResultEntry entry in response.Entries) { List <string> classes = new List <string> { }; entry.GetStringAttributeSafe("objectClass", out classes); if (classes.Contains("nTDSDSA")) { ntds.Add(entry); } else if (classes.Contains("queryPolicy")) { policies.Add(new QueryPolicy(entry)); } } policies.OrderByField("WhenCreated", false); foreach (QueryPolicy pol in policies) { ForestBase.QueryPolicies.AddSafe(pol.DN, pol); } foreach (SearchResultEntry entry in ntds) { string qpol = String.Empty; entry.GetStringAttributeSafe("queryPolicyObject", out qpol); if (qpol != String.Empty) { ForestBase.NTDSSettings.AddSafe(entry.DistinguishedName, qpol); } else { ForestBase.NTDSSettings.AddSafe(entry.DistinguishedName, policies[0].DN); } } ret = true; } catch (Exception ex) { SetError(ex.Message); } } return(ret); }
private bool LoadForestName() { bool ret = false; string domdns = null; SearchRequest request = new SearchRequest(ForestBase.RootDomainNamingContext, "(objectClass=*)", SearchScope.Base, new string[] { "canonicalName", "objectSid" }); LdapDirectoryIdentifier ldapid = new LdapDirectoryIdentifier(ForestBase.DefaultDC.Name, 389, true, false); using (LdapConnector ldapcon = new LdapConnector(ldapid)) { if ((ForestBase.GivenCreds != null) && (ForestBase.GivenCreds.HasCreds)) { ldapcon.Credential = ForestBase.GivenCreds.NetCreds; } try { ldapcon.Bind(); SearchResponse response = (SearchResponse)ldapcon.SendRequest(request); foreach (SearchResultEntry entry in response.Entries) { domdns = null; entry.GetStringAttributeSafe("canonicalName", out domdns); ForestBase.ForestName = domdns.TrimChar("/"); List <DomainControllerHelper> dclist = GetDcList(ForestBase.ForestName, entry.DistinguishedName); if ((dclist.Count == 1) && (dclist[0].Success == false)) { string temp = domdns; if (temp == null) { temp = "current domain"; } base.SetError(String.Format("LoadForestName: Could not detect a DC in {0}", temp)); } else { foreach (DomainControllerHelper dchelper in dclist) { if (dchelper.Success) { StoreDC(dchelper, entry.DistinguishedName); ret = true; } } List <byte[]> bsids; entry.GetAttributeSafe("objectSid", out bsids); SecurityIdentifier dsid = new SecurityIdentifier(bsids[0], 0); ForestBase.DomainSids.AddSafe(entry.DistinguishedName, dsid.ToString()); } } } catch (Exception ex) { base.SetError(String.Format("LoadForestName: {0} ({1})", ex.Message, ex.GetType().Name)); } } if (ret && (ForestBase.DCList.Count == 0)) { base.SetError("LoadForestName: Could not detect a DC in current forest"); ret = false; GlobalEventHandler.RaiseFinishedDiscovering(); } return(ret); }
public void Load(string[] attributes) { Success = false; UDPPinged = true; try { LdapDirectoryIdentifier ldapid = new LdapDirectoryIdentifier(Name, 389, true, true); LdapConnector ldapcon = new LdapConnector(ldapid); ldapcon.Timeout = new TimeSpan(0, 2, 30); SearchRequest request = new SearchRequest("", "(objectClass=*)", SearchScope.Base, attributes); SearchResponse response = (SearchResponse)ldapcon.SendRequest(request); foreach (SearchResultEntry entry in response.Entries) { entry.GetStringAttributeSafe("defaultNamingContext", out defNC); entry.GetStringAttributeSafe("configurationNamingContext", out confNC); entry.GetStringAttributeSafe("schemaNamingContext", out schemaNC); entry.GetStringAttributeSafe("rootDomainNamingContext", out rootNC); entry.GetStringAttributeSafe("namingContexts", out allNCs); entry.GetStringAttributeSafe("dnsHostName", out name); entry.GetStringAttributeSafe("dsServiceName", out ntds); GetQueryPolicy(); entry.GetStringAttributeSafe("serverName", out sitesrv); entry.GetStringAttributeSafe("supportedControl", out controls); string gcstring = String.Empty; entry.GetStringAttributeSafe("isGlobalCatalogReady", out gcstring); bool.TryParse(gcstring, out gc); entry.GetStringAttributeSafe("ldapServiceName", out domdns); if (domdns.Length > 0) { domdns = domdns.Split(new char[] { '@' })[1]; } } PingResolve(name); Success = true; UDPPingSuccess = true; } catch (Exception ex) { ErrorString = ex.Message; } }