Last active
October 23, 2017 19:17
-
-
Save rvazarkar/58647d21d777cc2db59ebd44627436ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public SearchRequest GetSearchRequest(string filter, SearchScope scope, string[] attribs, string domainName = null, string adsPath = null) | |
{ | |
Domain targetDomain; | |
try | |
{ | |
targetDomain = GetDomain(domainName); | |
} | |
catch | |
{ | |
return null; | |
} | |
domainName = targetDomain.Name; | |
adsPath = adsPath?.Replace("LDAP://", "") ?? $"DC={domainName.Replace(".", ",DC=")}"; | |
var request = new SearchRequest(adsPath, filter, scope, attribs); | |
var soc = new SearchOptionsControl(SearchOption.DomainScope); | |
request.Controls.Add(soc); | |
return request; | |
} | |
public LdapConnection GetLdapConnection(string domainName = null) | |
{ | |
Domain targetDomain; | |
try | |
{ | |
targetDomain = GetDomain(domainName); | |
} | |
catch | |
{ | |
return null; | |
} | |
var domainController = _options.DomainController ?? targetDomain.Name; | |
var identifier = _options.SecureLdap | |
? new LdapDirectoryIdentifier(domainController, 636, false, false) | |
: new LdapDirectoryIdentifier(domainController, false, false); | |
var connection = new LdapConnection(identifier); | |
//Add LdapSessionOptions | |
var lso = connection.SessionOptions; | |
if (!_options.DisableKerbSigning) | |
{ | |
lso.Signing = true; | |
lso.Sealing = true; | |
} | |
if (_options.SecureLdap) | |
{ | |
lso.ProtocolVersion = 3; | |
lso.SecureSocketLayer = true; | |
if (_options.IgnoreLdapCert) | |
connection.SessionOptions.VerifyServerCertificate = (con, cer) => true; | |
} | |
lso.ReferralChasing = ReferralChasingOptions.None; | |
return connection; | |
} | |
public IEnumerable<SearchResultEntry> DoSearch(string filter, SearchScope scope, string[] props, | |
string domainName = null, string adsPath = null, bool useGc = false) | |
{ | |
//Get an LDAP Connection | |
using (var conn = useGc ? GetGcConnection(domainName) : GetLdapConnection(domainName)) | |
{ | |
if (conn == null) | |
{ | |
yield break; | |
} | |
//Get a SearchRequest with our options | |
var request = GetSearchRequest(filter, scope, props, domainName, adsPath); | |
if (request == null) | |
{ | |
yield break; | |
} | |
//Add a page control to get 500 entries at a time | |
var prc = new PageResultRequestControl(500); | |
request.Controls.Add(prc); | |
if (_options.CurrentCollectionMethod.Equals(CollectionMethod.ACL)) | |
{ | |
var sdfc = | |
new SecurityDescriptorFlagControl { SecurityMasks = SecurityMasks.Dacl | SecurityMasks.Owner }; | |
request.Controls.Add(sdfc); | |
} | |
PageResultResponseControl pageResponse = null; | |
//Loop to keep getting results | |
while (true) | |
{ | |
SearchResponse response; | |
try | |
{ | |
response = (SearchResponse)conn.SendRequest(request); | |
if (response != null) | |
{ | |
pageResponse = (PageResultResponseControl)response.Controls[0]; | |
} | |
} | |
catch | |
{ | |
yield break; | |
} | |
if (response == null || pageResponse == null) continue; | |
foreach (SearchResultEntry entry in response.Entries) | |
{ | |
yield return entry; | |
} | |
//Exit the loop when our page response indicates we're out of data | |
if (pageResponse.Cookie.Length == 0 || response.Entries.Count == 0) | |
{ | |
yield break; | |
} | |
prc.Cookie = pageResponse.Cookie; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment