Skip to content

Commit

Permalink
chore: code formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
rvazarkar committed Dec 9, 2024
1 parent 076f41d commit 9a5eb07
Showing 1 changed file with 82 additions and 136 deletions.
218 changes: 82 additions & 136 deletions src/CommonLib/LdapUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public IAsyncEnumerable<LdapResult<IDirectoryObject>> PagedQuery(LdapQueryParame
} catch {
//pass
}


return (false, Label.Base);
}
Expand Down Expand Up @@ -227,7 +227,7 @@ public IAsyncEnumerable<LdapResult<IDirectoryObject>> PagedQuery(LdapQueryParame
} catch {
//pass
}


return (false, Label.Base);
}
Expand Down Expand Up @@ -361,7 +361,7 @@ public IAsyncEnumerable<LdapResult<IDirectoryObject>> PagedQuery(LdapQueryParame
} catch {
//pass
}


return (false, string.Empty);
}
Expand Down Expand Up @@ -904,7 +904,6 @@ public async Task<bool> IsDomainController(string computerObjectId, string domai
_unresolvablePrincipals.Add(distinguishedName);
return (false, default);
}

}

public async Task<(bool Success, string DSHeuristics)> GetDSHueristics(string domain, string dn) {
Expand Down Expand Up @@ -961,7 +960,7 @@ public async IAsyncEnumerable<OutputBase> GetWellKnownPrincipalOutput() {
yield return entdc;
}
}

private async IAsyncEnumerable<Group> GetEnterpriseDCGroups() {
var grouped = new ConcurrentDictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
var forestSidToName = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Expand All @@ -985,6 +984,7 @@ await GetDomainSidFromDomainName(forestName) is (true, var forestDomainSid)) {
if (!forestSidToName.TryGetValue(f.Key, out var forestName)) {
continue;
}

var group = new Group { ObjectIdentifier = $"{forestName}-S-1-5-9" };
group.Properties.Add("name", $"ENTERPRISE DOMAIN CONTROLLERS@{forestName}".ToUpper());
group.Properties.Add("domainsid", f.Key);
Expand Down Expand Up @@ -1143,10 +1143,8 @@ internal static bool ResolveLabel(string objectIdentifier, string distinguishedN
}

public static async Task<(bool Success, ResolvedSearchResult ResolvedResult)> ResolveSearchResult(
IDirectoryObject directoryObject, ILdapUtils utils)
{
if (!directoryObject.GetObjectIdentifier(out var objectIdentifier))
{
IDirectoryObject directoryObject, ILdapUtils utils) {
if (!directoryObject.GetObjectIdentifier(out var objectIdentifier)) {
return (false, default);
}

Expand All @@ -1155,66 +1153,50 @@ internal static bool ResolveLabel(string objectIdentifier, string distinguishedN
};

//If the object is deleted, we can short circuit the rest of this logic as we don't really care about anything else
if (directoryObject.IsDeleted())
{
if (directoryObject.IsDeleted()) {
res.Deleted = true;
return (true, res);
}

if (directoryObject.TryGetLongProperty(LDAPProperties.UserAccountControl, out var rawUac))
{
if (directoryObject.TryGetLongProperty(LDAPProperties.UserAccountControl, out var rawUac)) {
var flags = (UacFlags)rawUac;
if (flags.HasFlag(UacFlags.ServerTrustAccount))
{
if (flags.HasFlag(UacFlags.ServerTrustAccount)) {
res.IsDomainController = true;
utils.AddDomainController(objectIdentifier);
}
}

string domain;

if (directoryObject.TryGetDistinguishedName(out var distinguishedName))
{
if (directoryObject.TryGetDistinguishedName(out var distinguishedName)) {
domain = Helpers.DistinguishedNameToDomain(distinguishedName);
}
else
{
} else {
if (objectIdentifier.StartsWith("S-1-5") &&
await utils.GetDomainNameFromSid(objectIdentifier) is (true, var domainName))
{
await utils.GetDomainNameFromSid(objectIdentifier) is (true, var domainName)) {
domain = domainName;
}
else
{
} else {
return (false, default);
}
}

string domainSid;
var match = SIDRegex.Match(objectIdentifier);
if (match.Success)
{
if (match.Success) {
domainSid = match.Groups[1].Value;
}
else if (await utils.GetDomainSidFromDomainName(domain) is (true, var sid))
{
} else if (await utils.GetDomainSidFromDomainName(domain) is (true, var sid)) {
domainSid = sid;
}
else
{
} else {
Logging.Logger.LogWarning("Failed to resolve domain sid for object {Identifier}", objectIdentifier);
domainSid = null;
}

res.Domain = domain;
res.DomainSid = domainSid;

if (WellKnownPrincipal.GetWellKnownPrincipal(objectIdentifier, out var wellKnownPrincipal))
{
if (WellKnownPrincipal.GetWellKnownPrincipal(objectIdentifier, out var wellKnownPrincipal)) {
res.DisplayName = $"{wellKnownPrincipal.ObjectIdentifier}@{domain}";
res.ObjectType = wellKnownPrincipal.ObjectType;
if (await utils.GetWellKnownPrincipal(objectIdentifier, domain) is (true, var convertedPrincipal))
{
if (await utils.GetWellKnownPrincipal(objectIdentifier, domain) is (true, var convertedPrincipal)) {
res.ObjectId = convertedPrincipal.ObjectIdentifier;
}

Expand All @@ -1228,136 +1210,100 @@ await utils.GetDomainNameFromSid(objectIdentifier) is (true, var domainName))
return (true, res);
}

private static async Task<Label> ComputeLabel(IDirectoryObject directoryObject, string objectIdentifier, string domain, ILdapUtils utils)
{
if (!directoryObject.GetLabel(out var label))
{
if (await utils.ResolveIDAndType(objectIdentifier, domain) is (true, var typedPrincipal))
{
private static async Task<Label> ComputeLabel(IDirectoryObject directoryObject, string objectIdentifier,
string domain, ILdapUtils utils) {
if (!directoryObject.GetLabel(out var label)) {
if (await utils.ResolveIDAndType(objectIdentifier, domain) is (true, var typedPrincipal)) {
label = typedPrincipal.ObjectType;
}
}

if (directoryObject.IsMSA() || directoryObject.IsGMSA())
{
if (directoryObject.IsMSA() || directoryObject.IsGMSA()) {
label = Label.User;
}

return label;
}

private static string ComputeDisplayName(IDirectoryObject directoryObject, string domain, Label label, string samAccountName)
{
private static string ComputeDisplayName(IDirectoryObject directoryObject, string domain, Label label,
string samAccountName) {
string displayName;
switch (label)
{
switch (label) {
case Label.User:
case Label.Group:
case Label.Base:
displayName = $"{samAccountName}@{domain}";
break;
case Label.Computer:
{
var shortName = samAccountName?.TrimEnd('$');
if (directoryObject.TryGetProperty(LDAPProperties.DNSHostName, out var dns))
{
displayName = dns;
}
else if (!string.IsNullOrWhiteSpace(shortName))
{
displayName = $"{shortName}.{domain}";
}
else if (directoryObject.TryGetProperty(LDAPProperties.CanonicalName,
out var canonicalName))
{
displayName = $"{canonicalName}.{domain}";
}
else if (directoryObject.TryGetProperty(LDAPProperties.Name, out var name))
{
displayName = $"{name}.{domain}";
}
else
{
displayName = $"UNKNOWN.{domain}";
}

break;
case Label.Computer: {
var shortName = samAccountName?.TrimEnd('$');
if (directoryObject.TryGetProperty(LDAPProperties.DNSHostName, out var dns)) {
displayName = dns;
} else if (!string.IsNullOrWhiteSpace(shortName)) {
displayName = $"{shortName}.{domain}";
} else if (directoryObject.TryGetProperty(LDAPProperties.CanonicalName,
out var canonicalName)) {
displayName = $"{canonicalName}.{domain}";
} else if (directoryObject.TryGetProperty(LDAPProperties.Name, out var name)) {
displayName = $"{name}.{domain}";
} else {
displayName = $"UNKNOWN.{domain}";
}
case Label.GPO:
case Label.IssuancePolicy:
{
if (directoryObject.TryGetProperty(LDAPProperties.DisplayName, out var ldapDisplayName))
{
displayName = $"{ldapDisplayName}@{domain}";
}
else if (directoryObject.TryGetProperty(LDAPProperties.CanonicalName,
out var canonicalName))
{
displayName = $"{canonicalName}@{domain}";
}
else
{
displayName = $"UNKNOWN@{domain}";
}

break;
break;
}
case Label.GPO:
case Label.IssuancePolicy: {
if (directoryObject.TryGetProperty(LDAPProperties.DisplayName, out var ldapDisplayName)) {
displayName = $"{ldapDisplayName}@{domain}";
} else if (directoryObject.TryGetProperty(LDAPProperties.CanonicalName,
out var canonicalName)) {
displayName = $"{canonicalName}@{domain}";
} else {
displayName = $"UNKNOWN@{domain}";
}

break;
}
case Label.Domain:
displayName = domain;
break;
case Label.OU:
{
if (directoryObject.TryGetProperty(LDAPProperties.Name, out var name))
{
displayName = $"{name}@{domain}";
}
else if (directoryObject.TryGetProperty(LDAPProperties.OU, out var ou))
{
displayName = $"{ou}@{domain}";
}
else
{
displayName = $"UNKNOWN@{domain}";
}

break;
case Label.OU: {
if (directoryObject.TryGetProperty(LDAPProperties.Name, out var name)) {
displayName = $"{name}@{domain}";
} else if (directoryObject.TryGetProperty(LDAPProperties.OU, out var ou)) {
displayName = $"{ou}@{domain}";
} else {
displayName = $"UNKNOWN@{domain}";
}
case Label.Container:
{
if (directoryObject.TryGetProperty(LDAPProperties.Name, out var name))
{
displayName = $"{name}@{domain}";
}
else if (directoryObject.TryGetProperty(LDAPProperties.CanonicalName,
out var canonicalName))
{
displayName = $"{canonicalName}@{domain}";
}
else
{
displayName = $"UNKNOWN@{domain}";
}

break;
break;
}
case Label.Container: {
if (directoryObject.TryGetProperty(LDAPProperties.Name, out var name)) {
displayName = $"{name}@{domain}";
} else if (directoryObject.TryGetProperty(LDAPProperties.CanonicalName,
out var canonicalName)) {
displayName = $"{canonicalName}@{domain}";
} else {
displayName = $"UNKNOWN@{domain}";
}

break;
}
case Label.Configuration:
case Label.RootCA:
case Label.AIACA:
case Label.NTAuthStore:
case Label.EnterpriseCA:
case Label.CertTemplate:
{
if (directoryObject.TryGetProperty(LDAPProperties.Name, out var name))
{
displayName = $"{name}@{domain}";
}
else
{
displayName = $"UNKNOWN@{domain}";
}

break;
case Label.CertTemplate: {
if (directoryObject.TryGetProperty(LDAPProperties.Name, out var name)) {
displayName = $"{name}@{domain}";
} else {
displayName = $"UNKNOWN@{domain}";
}

break;
}
default:
throw new ArgumentOutOfRangeException();
}
Expand Down

0 comments on commit 9a5eb07

Please sign in to comment.