Skip to content

Commit bb75d89

Browse files
committed
more progress
1 parent af082c9 commit bb75d89

File tree

5 files changed

+77
-14
lines changed

5 files changed

+77
-14
lines changed

docs/en-US/Get-ADEffectiveAccess.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
external help file: ADEffectiveAccess.dll-Help.xml
33
Module Name: ADEffectiveAccess
4-
online version:
4+
online version: https://github.com/santisq/ADEffectiveAccess/blob/main/docs/en-US/Get-ADEffectiveAccess.md
55
schema: 2.0.0
66
---
77

module/ADEffectiveAccess.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Copyright = '(c) Santiago Squarzon. All rights reserved.'
3131

3232
# Description of the functionality provided by this module
33-
# Description = ''
33+
Description = 'Active Directory friendly ACLs'
3434

3535
# Minimum version of the PowerShell engine required by this module
3636
PowerShellVersion = '5.1'
@@ -123,7 +123,7 @@
123123
} # End of PrivateData hashtable
124124

125125
# HelpInfo URI of this module
126-
HelpInfoURI = ''
126+
HelpInfoURI = 'https://github.com/santisq/ADEffectiveAccess/blob/main/docs/en-US/Get-ADEffectiveAccess.md'
127127

128128
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
129129
# DefaultCommandPrefix = ''
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections.Generic;
2+
3+
namespace ADEffectiveAccess;
4+
5+
internal static class Extensions
6+
{
7+
internal static bool TryAdd<TKey, TValue>(
8+
this IDictionary<TKey, TValue> dictionary,
9+
TKey key,
10+
TValue value)
11+
{
12+
bool result;
13+
if (result = !dictionary.ContainsKey(key))
14+
{
15+
dictionary.Add(key, value);
16+
}
17+
return !result;
18+
}
19+
}

src/ADEffectiveAccess/GetADEffectiveAccessComand.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,30 @@
44

55
namespace ADEffectiveAccess;
66

7-
[Cmdlet(VerbsCommon.Get, "ADEffectiveAccess")]
7+
[Cmdlet(VerbsCommon.Get, "ADEffectiveAccess", DefaultParameterSetName = FilterSet)]
8+
[OutputType(typeof(EffectiveAccessRule), typeof(EffectiveAuditRule))]
89
[Alias("gea", "gacl")]
910
public sealed class GetADEffectiveAccessComand : PSCmdlet
1011
{
1112
private const string SecurityDescriptor = "nTSecurityDescriptor";
1213

14+
private const string FilterSet = "Filter";
15+
16+
private const string IdentitySet = "Identity";
17+
1318
private SchemaMap? _map;
1419

15-
[Parameter(Position = 0)]
20+
[Parameter(Position = 0, ParameterSetName = FilterSet)]
21+
[ValidateNotNullOrEmpty]
1622
public string? LdapFilter { get; set; }
1723

24+
[Parameter(Position = 0, Mandatory = true, ParameterSetName = IdentitySet)]
25+
public string? Identity { get; set; }
26+
1827
[Parameter]
1928
public SwitchParameter Audit { get; set; }
2029

21-
[Parameter]
30+
[Parameter(ParameterSetName = FilterSet)]
2231
[ValidateRange(0, int.MaxValue)]
2332
public int Top { get; set; } = 0;
2433

@@ -28,13 +37,20 @@ public sealed class GetADEffectiveAccessComand : PSCmdlet
2837
[Parameter]
2938
public SearchScope SearchScope { get; set; } = SearchScope.Subtree;
3039

40+
[Parameter]
41+
[ValidateNotNullOrEmpty]
42+
public string? SearchBase { get; set; } = string.Empty;
43+
3144
[Parameter]
3245
[Credential]
3346
public PSCredential? Credential { get; set; }
3447

3548
[Parameter]
3649
public string? Server { get; set; }
3750

51+
[Parameter]
52+
public int PageSize { get; set; } = 1000;
53+
3854
protected override void BeginProcessing()
3955
{
4056
try
@@ -53,11 +69,13 @@ protected override void BeginProcessing()
5369

5470
protected override void EndProcessing()
5571
{
56-
using DirectorySearcher searcher = new(LdapFilter, [SecurityDescriptor])
72+
using DirectoryEntry root = new(SearchBase);
73+
using DirectorySearcher searcher = new(root, LdapFilter, [SecurityDescriptor])
5774
{
5875
SizeLimit = Top,
5976
Tombstone = IncludeDeletedObjects,
6077
SearchScope = SearchScope,
78+
PageSize = PageSize,
6179
SecurityMasks = SecurityMasks.Group |
6280
SecurityMasks.Dacl |
6381
SecurityMasks.Owner

src/ADEffectiveAccess/SchemaMap.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ internal SchemaMap(string? server = null)
1212
{
1313
string path = server is null ? "LDAP://RootDSE" : $"LDAP://{server}/RootDSE";
1414
using DirectoryEntry root = new(path);
15-
string? ctx = root.Properties["schemaNamingContext"][0]?.ToString();
16-
if (ctx is not null) PopulateMap(ctx, _schemaMap);
15+
string? ctxSchema = root.Properties["schemaNamingContext"][0]?.ToString();
16+
string? ctxConfig = root.Properties["configurationNamingContext"][0]?.ToString();
17+
if (ctxSchema is not null) PopulateSchema(ctxSchema, _schemaMap);
18+
if (ctxConfig is not null) PopulateExtendedRights(ctxConfig, _schemaMap);
1719
}
1820

1921
internal string Translate(Guid guid, string defaultValue)
@@ -26,20 +28,44 @@ internal string Translate(Guid guid, string defaultValue)
2628
return guid.ToString();
2729
}
2830

29-
private static void PopulateMap(
31+
private static void PopulateSchema(
3032
string schemaNamingContext,
3133
Dictionary<Guid, string> map)
3234
{
3335
using DirectoryEntry root = new($"LDAP://{schemaNamingContext}");
3436
using DirectorySearcher searcher = new(
3537
searchRoot: root,
36-
filter: "(schemaIDGUID=*)",
37-
propertiesToLoad: ["cn", "schemaIDGuid"]);
38+
filter: "(&(schemaIdGuid=*)(|(objectClass=attributeSchema)(objectClass=classSchema)))",
39+
propertiesToLoad: ["cn", "schemaIdGuid"])
40+
{
41+
PageSize = 1000
42+
};
43+
44+
foreach (SearchResult result in searcher.FindAll())
45+
{
46+
map.TryAdd(
47+
new Guid((byte[])result.Properties["schemaIdGuid"][0]),
48+
result.Properties["cn"][0].ToString());
49+
}
50+
}
51+
52+
private static void PopulateExtendedRights(
53+
string configurationNamingContext,
54+
Dictionary<Guid, string> map)
55+
{
56+
using DirectoryEntry root = new($"LDAP://CN=Extended-Rights,{configurationNamingContext}");
57+
using DirectorySearcher searcher = new(
58+
searchRoot: root,
59+
filter: "(objectClass=controlAccessRight)",
60+
propertiesToLoad: ["cn", "rightsGuid"])
61+
{
62+
PageSize = 1000
63+
};
3864

3965
foreach (SearchResult result in searcher.FindAll())
4066
{
41-
map.Add(
42-
new Guid((byte[])result.Properties["schemaIDGUID"][0]),
67+
map.TryAdd(
68+
Guid.Parse(result.Properties["rightsGuid"][0].ToString()),
4369
result.Properties["cn"][0].ToString());
4470
}
4571
}

0 commit comments

Comments
 (0)