private SearchResultEntryCollection DirSyncQuery(ref SearchRequestExtender reqStore) { SearchResultEntryCollection ret = null; try { SearchResponse sresponse = (SearchResponse)base.Connection.SendRequest(reqStore.Request); DirectoryControl dscontrol = GetControl(sresponse.Controls, reqStore.DirSyncControl.Type); reqStore.UpdateDirSyncCookie(dscontrol); ret = sresponse.Entries; } catch (LdapException ldapEx) { reqStore.HasError = true; reqStore.ErrorMSG = ldapEx.Message; return(null); } catch (Exception ex) { reqStore.HasError = true; reqStore.ErrorMSG = ex.Message; return(null); } return(ret); }
public SearchRequestExtender Copy() { SearchRequestExtender ret = new SearchRequestExtender(); ret.PagingCookie = PagingCookie; ret.PageCount = PageCount; ret.PageControl = PageControl; ret.CurrentPageSize = CurrentPageSize; ret.RetreiveStatistics = RetreiveStatistics; ret.Statistics = Statistics; ret.Attributes = Attributes; ret.CurrentAttributesToDisplay = CurrentAttributesToDisplay; ret.CurrentRangeRetrieval = CurrentRangeRetrieval; ret.HasError = HasError; ret.ErrorMSG = ErrorMSG; ret.Request = Request; return(ret); }
public static void RaiseQueryCompleted(List <SearchResultEntry> entries, SearchRequestExtender infoStore, QUERY_RESULT_EVENT_TYPE resultType) { if (QueryCompleted != null) { QueryCompleted(infoStore, new GlobalEventArgs() { Entries = entries, ResultEventType = resultType }); } }
private void QuerySingleObjectPaths(object infoStore, GlobalEventArgs args) { if ((args.ResultEventType & QUERY_RESULT_EVENT_TYPE.FROM_SINGLE_OBJECT_PATH) != QUERY_RESULT_EVENT_TYPE.FROM_SINGLE_OBJECT_PATH) { return; } if ((args.ResultEventType & QUERY_RESULT_EVENT_TYPE.IS_COMPLETED) == QUERY_RESULT_EVENT_TYPE.IS_COMPLETED) { GlobalEventHandler.QueryCompleted -= QuerySingleObjectPaths; } SearchRequestExtender reqstore = (SearchRequestExtender)infoStore; if (!reqstore.HasError) { GlobalEventHandler.RaiseErrorOccured(String.Format("Found {0} paths to handle", args.Entries.Count)); } reqstore.QueryInfo.CurrentResultEventType = QUERY_RESULT_EVENT_TYPE.IS_PARTIAL; int cnt = 0; foreach (SearchResultEntry entry in args.Entries) { cnt++; if ((cnt == args.Entries.Count) && (!reqstore.MoreData)) { reqstore.QueryInfo.CurrentResultEventType = QUERY_RESULT_EVENT_TYPE.NONE; } Query(reqstore.DC, entry.DistinguishedName, reqstore.LdapFilter, reqstore.Attributes, SearchScope.Base, reqstore.ReferralChasing, reqstore.QueryInfo); GlobalEventHandler.RaiseErrorOccured(String.Format("Subsequent query: #{0}", cnt)); } }
public string DecodePrimaryGroupId(DirectoryAttribute attrib, bool mustDecode) { string ret = String.Empty; bool isbad = false; if (mustDecode) { ret = DecodeStringData(attrib, ActiveDirectorySyntax.CaseIgnoreString, out isbad, true)[0]; if (ForestBase.DomainSids.ContainsKey(ForestBase.CurrentDomain)) { string psid = ForestBase.DomainSids[ForestBase.CurrentDomain] + "-" + ret; SearchRequestExtender srex = new SearchRequestExtender(); string[] attribs = new string[] { "distinguishedName" }; List <SearchResultEntry> results = Query(ForestBase.CurrentDC, "<SID=" + psid + ">", "(objectClass=*)", attribs, SearchScope.Base, ReferralChasingOptions.None, new QueryControl() { CurrentResultEventType = QUERY_RESULT_EVENT_TYPE.FROM_DECODING }, returnResults: true); DirectoryAttribute dadn = results[0].Attributes["distinguishedName"]; string dn = DecodeStringData(dadn, ActiveDirectorySyntax.DirectoryString, out isbad, true)[0]; ret = string.Format("\t\t<{0}: {1}>", ret, dn); } } else { ret = DecodeStringData(attrib, ActiveDirectorySyntax.CaseIgnoreString, out isbad)[0]; } return(ret); }
public List <string> DecodeSearchResults(List <SearchResultEntry> result, SearchRequestExtender infoStore) { List <string> ret = new List <string> { }; foreach (SearchResultEntry sre in result) { ret.AddFormatted("DN: <{0}>", sre.DistinguishedName); if (sre.Attributes.Values.Count == 0) { ret.Add("\t<no such attribute(s)>"); } else { if (infoStore.Attributes == null) { Array.Resize(ref infoStore.Attributes, sre.Attributes.AttributeNames.Count); sre.Attributes.AttributeNames.CopyTo(infoStore.Attributes, 0); } foreach (string attribname in infoStore.Attributes) { List <string> tempret = new List <string> { }; bool isempty = false; tempret.AddFormatted("\t<{0}>:", attribname); if (!(sre.Attributes.Contains(attribname))) { tempret.Add("\t\t<not present>"); isempty = true; } else { DirectoryAttribute deAttrib = sre.Attributes[attribname]; if (deAttrib.Count == 0) { tempret.Add("\t\t<not set>"); isempty = true; } else { bool isbad; ActiveDirectorySyntax asyntax = GetAttributeSyntax(attribname, out isbad); if (isbad) { tempret.AddFormatted("\t\t<UnKnown syntax> {0}", deAttrib.Name); } else { switch (asyntax) { case ActiveDirectorySyntax.OctetString: case ActiveDirectorySyntax.Sid: tempret.AddRange(DecodeByteData(deAttrib, asyntax, out isbad)); break; case ActiveDirectorySyntax.Int64: case ActiveDirectorySyntax.GeneralizedTime: case ActiveDirectorySyntax.UtcTime: string plaind = null; tempret.AddRange(DecodeInt64Data(deAttrib, asyntax, out isbad, out plaind)); break; case ActiveDirectorySyntax.SecurityDescriptor: tempret.AddRange(DecodeSDData(deAttrib, asyntax)); break; case ActiveDirectorySyntax.Int: tempret.AddRange(DecodeIntData(deAttrib, asyntax, out isbad)); break; default: tempret.AddRange(DecodeStringData(deAttrib, asyntax, out isbad)); break; } if (isbad) { MarkAttributeAsBad(ForestBase.ForestName, attribname); } } } } if ((!isempty) || (isempty && !MainBase.UserSettings.IgnoreEmpty)) { ret.AddRange(tempret); } } } } if (ForestBase.SchemaCacheIsDirty) { SaveCache(); } return(ret); }
private bool RunParallelQueries(string dc, string searchBase, string ldapFilter, ref string[] propertiesToLoad, SearchScope scope, ReferralChasingOptions referralChasing, QueryControl queryInfo, out SearchRequestExtender reqStore) { bool ret = false; reqStore = null; if ((searchBase == null) || (searchBase.Length == 0)) { return(ret); } reqStore = new SearchRequestExtender(searchBase, ldapFilter, propertiesToLoad, scope, null, queryInfo); if ((scope == SearchScope.Subtree) && (queryInfo.PerformPagedQuery) && ((referralChasing & ReferralChasingOptions.Subordinate) == ReferralChasingOptions.Subordinate)) { ret = true; } if (!ret) { return(ret); } Dictionary <string, string> querylist = SubNCsToDCs(searchBase, dc); base.ParallelRuns = querylist.Count; if (base.ParallelRuns == 0) { return(ret); } ret = true; referralChasing = ReferralChasingOptions.None; string[] attributes = propertiesToLoad; foreach (KeyValuePair <string, string> kvp in querylist) { base.RunningParallel = true; Thread thquery = new Thread(() => { AsyncQuery(kvp.Value, kvp.Key, ldapFilter, attributes, scope, referralChasing, queryInfo); }); thquery.Start(); } return(ret); }
private SearchResultEntryCollection PagedQuery(ref SearchRequestExtender reqStore) { SearchResultEntryCollection ret = null; SearchResponse sresponse = null; bool goon = false; try { if ((base.IsConnected == false) || (base.Connection == null)) { Connect(reqStore.DC, reqStore.ReferralChasing, connectionLess: reqStore.QueryInfo.RootDse); } sresponse = (SearchResponse)base.Connection.SendRequest(reqStore.Request); goon = true; } catch (LdapException ldapEx) { base.SetError(ldapEx.Message); reqStore.HasError = true; reqStore.ErrorMSG = ldapEx.Message; return(null); } catch (DirectoryOperationException direx) { if (direx.Response != null) { if (direx.Response.ResultCode == ResultCode.SizeLimitExceeded) { if (reqStore.QueryInfo.AutoPage) { GlobalEventHandler.RaiseErrorOccured(String.Format("Non-PagedQuery: {0} - switched to PagedQuery", direx.Response.ResultCode.ToString())); reqStore.AddMessage(String.Format("Non-PagedQuery: {0} - switched to PagedQuery", direx.Response.ResultCode.ToString())); reqStore.PageCookie(((SearchResponse)direx.Response).Entries.Count); reqStore.MoreData = true; } else { sresponse = (SearchResponse)direx.Response; GlobalEventHandler.RaiseErrorOccured(String.Format("\tNon-PagedQuery: {0} - returned first {1} entries", direx.Response.ResultCode.ToString(), sresponse.Entries.Count)); reqStore.AddMessage(String.Format("Non-PagedQuery: {0} - returned first {1} entries", direx.Response.ResultCode.ToString(), sresponse.Entries.Count)); } goon = true; } else if ((direx.Response.ResultCode == ResultCode.UnavailableCriticalExtension) && direx.Response.ErrorMessage.StartsWith("00002040") && (reqStore.PageControl != null) && (reqStore.ReferralChasing != ReferralChasingOptions.None)) { reqStore.PageCount--; string msg = "Multiple page cookies from referrals."; msg = String.Format("{0} ({1})", msg, direx.Message); if (direx.Response.ErrorMessage != null) { msg = String.Format("{0}: ({1})", msg, direx.Response.ErrorMessage); } base.SetError(msg); reqStore.HasError = true; reqStore.ErrorMSG = base.ErrorMSG; //goon = true; } else { string msg = direx.Message; if (direx.Response.ErrorMessage != null) { msg = String.Format("{0}: ({1})", msg, direx.Response.ErrorMessage); } base.SetError(msg); reqStore.HasError = true; reqStore.ErrorMSG = base.ErrorMSG; } } else // if (!goon) { base.SetError(direx.Message); reqStore.HasError = true; reqStore.ErrorMSG = base.ErrorMSG; } } catch (Exception ex) { base.SetError(ex.Message); reqStore.HasError = true; reqStore.ErrorMSG = ex.Message; return(null); } if (goon) { if (sresponse != null) { if (reqStore.RetreiveStatistics) { DirectoryControl dcStats = GetControl(sresponse.Controls, SearchRequestExtender.STATISTCS_CONTROL_OID); if (dcStats != null) { reqStore.Statistics.Add(new StatsData(dcStats.GetValue())); } else { GlobalEventHandler.RaiseErrorOccured("WARNING: No Query Statistics data returned"); } } if (reqStore.PageControl != null) { DirectoryControl pageRespControl = GetControl(sresponse.Controls, reqStore.PageControl.Type); reqStore.UpdatePagingCookie(pageRespControl, sresponse.Entries.Count); } ret = sresponse.Entries; } } return(ret); }
public List <SearchResultEntry> Query(string dc, string searchBase, string ldapFilter, string[] propertiesToLoad, SearchScope scope, ReferralChasingOptions referralChasing, QueryControl queryInfo, string[] attributesRemebered = null, bool returnResults = false) { CancelToken = false; GlobalEventHandler.ClearSearchCancelled(); GlobalEventHandler.SearchCancelled += ReceivedCancellation; List <SearchResultEntry> ret = new List <SearchResultEntry> { }; List <SearchResultEntry> fire = new List <SearchResultEntry> { }; QueryHasConstructedAttribute(propertiesToLoad, scope, ref queryInfo); if (queryInfo.MustGetSingleObjectPath) { GetSingleObjectPaths(dc, searchBase, ldapFilter, propertiesToLoad, scope, referralChasing, queryInfo); return(ret); } ForestBase.CurrentPorts.SelectedPort = queryInfo.Port; byte[] pagingCookie = null; SearchRequestExtender reqstore = new SearchRequestExtender(searchBase, ldapFilter, propertiesToLoad, scope, pagingCookie, queryInfo); reqstore.DC = dc; reqstore.ReferralChasing = referralChasing; propertiesToLoad = reqstore.Attributes; while (true) { if (!reqstore.HasError) { reqstore.PageCount++; } SearchResultEntryCollection colresult = null; if (!CancelToken) { if (!queryInfo.PerformDirSync) { DateTime starttime = DateTime.Now; colresult = PagedQuery(ref reqstore); if (reqstore.CurrentPageSize > 0) { GlobalEventHandler.RaiseMessageOccured(String.Format("Page: {0} ({1}) [{2}] ms]", reqstore.PageCount, reqstore.CurrentPageSize, DateTime.Now.Subtract(starttime).TotalMilliseconds)); } } else { colresult = DirSyncQuery(ref reqstore); } } else { break; } if (reqstore.HasError) { break; } if ((colresult != null) && (colresult.Count > 0) && ((colresult[0].DistinguishedName.Length != 0) || (searchBase == ""))) { SearchResultEntry[] temp = new SearchResultEntry[colresult.Count]; colresult.CopyTo(temp, 0); fire = temp.ToList(); if (returnResults) { ret.AddRange(fire); } } if ((queryInfo.CurrentResultEventType == QUERY_RESULT_EVENT_TYPE.FROM_SINGLE_OBJECT_PATH) && (attributesRemebered != null)) { reqstore.HandleAttributes(attributesRemebered); } if (reqstore.MoreData) { if ((fire.Count > 0) && !returnResults) { GlobalEventHandler.RaiseQueryCompleted(fire, reqstore, QUERY_RESULT_EVENT_TYPE.IS_PARTIAL | queryInfo.CurrentResultEventType); } if ((queryInfo.CurrentResultEventType == QUERY_RESULT_EVENT_TYPE.FROM_SINGLE_OBJECT_PATH) && (attributesRemebered != null)) { reqstore.HandleAttributes(propertiesToLoad); } } else { break; } } if (reqstore.DoPaging) { reqstore.AddMessage(String.Format("PageSize: {0}", reqstore.CurrentPageSize)); reqstore.AddMessage(String.Format("Pages: {0}", reqstore.PageCount)); } if (!returnResults) { GlobalEventHandler.RaiseQueryCompleted(fire, reqstore, QUERY_RESULT_EVENT_TYPE.IS_COMPLETED | queryInfo.CurrentResultEventType); } Disconnect(); return(ret); }