Skip to content

Commit

Permalink
Updated ActiveDirectoryListUsersOfGroup to allow for TAB exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven-D-Foster committed May 20, 2021
1 parent b5c6e9c commit 1c11bec
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright (c) 2021 Steven Foster ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;
using System.Linq;
using HavokMultimedia.Utilities.Console.External;

namespace HavokMultimedia.Utilities.Console.Commands
{
public abstract class ActiveDirectoryListBase : ActiveDirectoryBase
{
protected enum Format { Display, TAB }
protected abstract string Summary { get; }
protected virtual string Example => "-h=192.168.1.5 -u=administrator -p=testpass";
protected override void CreateHelp(CommandHelpBuilder help)
{
base.CreateHelp(help);
help.AddSummary(Summary);
help.AddExample(Example);
help.AddParameter("format", "f", "Format of the data (" + nameof(Format.Display) + ") " + DisplayEnumOptions<Format>());
}



protected override void ExecuteInternal()
{
base.ExecuteInternal();
var format = GetArgParameterOrConfigEnum("format", "f", Format.Display);

using (var ad = GetActiveDirectory())
{
var objects = ad.GetAll().OrEmpty();
foreach (var obj in objects.OrderBy(o => o.DistinguishedName, StringComparer.OrdinalIgnoreCase))
{
if (IsValidObject(obj)) log.Info(Display(obj, format));
}
}

}

protected virtual string Display(ActiveDirectoryObject obj, Format format)
{
if (format == Format.Display) return ObjName(obj) + " --> " + obj.DistinguishedName;
if (format == Format.TAB) return ObjName(obj) + "\t" + obj.DistinguishedName;
throw new NotImplementedException($"format [{format}] is not implemented");
}

protected string ObjName(ActiveDirectoryObject obj) => obj.LogonNamePreWindows2000 ?? obj.LogonName ?? obj.Name;

protected abstract bool IsValidObject(ActiveDirectoryObject obj);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright (c) 2021 Steven Foster ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using HavokMultimedia.Utilities.Console.External;

namespace HavokMultimedia.Utilities.Console.Commands
{
public class ActiveDirectoryListComputers : ActiveDirectoryListBase
{
protected override string Summary => "Lists all computer names in an ActiveDirectory";
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsComputer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright (c) 2021 Steven Foster ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using HavokMultimedia.Utilities.Console.External;

namespace HavokMultimedia.Utilities.Console.Commands
{
public class ActiveDirectoryListGroups : ActiveDirectoryListBase
{
protected override string Summary => "Lists all group names in an ActiveDirectory";
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsGroup;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,95 +14,15 @@ You may obtain a copy of the License at
limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HavokMultimedia.Utilities.Console.External;

namespace HavokMultimedia.Utilities.Console.Commands
{
public abstract class ActiveDirectoryListBase : ActiveDirectoryBase
{
protected abstract string Summary { get; }
protected virtual string Example => "-h=192.168.1.5 -u=administrator -p=testpass";
protected override void CreateHelp(CommandHelpBuilder help)
{
base.CreateHelp(help);
help.AddSummary(Summary);
help.AddExample(Example);
}

protected override void ExecuteInternal()
{
base.ExecuteInternal();

using (var ad = GetActiveDirectory())
{
var objects = ad.GetAll().OrEmpty();
foreach (var obj in objects.OrderBy(o => o.DistinguishedName, StringComparer.OrdinalIgnoreCase))
{
if (IsValidObject(obj)) log.Info(Display(obj));
}
}

}

protected virtual string Display(ActiveDirectoryObject obj)
{
return (obj.LogonNamePreWindows2000 ?? obj.LogonName ?? obj.Name) + " --> " + obj.DistinguishedName;
}

protected abstract bool IsValidObject(ActiveDirectoryObject obj);
}

public class ActiveDirectoryListObjects : ActiveDirectoryListBase
{
protected override string Summary => "Lists all object names in an ActiveDirectory";
protected override bool IsValidObject(ActiveDirectoryObject obj) => true;
}

public class ActiveDirectoryListUsers : ActiveDirectoryListBase
{
protected override string Summary => "Lists all user names in an ActiveDirectory";
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsUser;
}

public class ActiveDirectoryListUsersOfGroup : ActiveDirectoryListBase
{
protected override string Summary => "Lists all user names that are members of the specified group in an ActiveDirectory";
protected override string Example => base.Example + " M?Group*";
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsUser && obj.MemberOfNames.Any(o => o.EqualsWildcard(group, true));
private string group;
protected override void ExecuteInternal()
{
group = GetArgValueTrimmed(0);
log.Debug($"{nameof(group)}: {group}");
if (group == null) throw new ArgsException(nameof(group), $"No {nameof(group)} specified");

base.ExecuteInternal();
}

protected override string Display(ActiveDirectoryObject obj)
{
var matchedGroups = new List<string>();
foreach (var m in obj.MemberOfNames)
{
if (m.EqualsWildcard(group, true)) matchedGroups.Add(m);
}
return (obj.LogonNamePreWindows2000 ?? obj.LogonName ?? obj.Name) + "," + obj.DistinguishedName + "," + matchedGroups.ToStringDelimited("|");
}
}

public class ActiveDirectoryListGroups : ActiveDirectoryListBase
{
protected override string Summary => "Lists all group names in an ActiveDirectory";
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsGroup;
}

public class ActiveDirectoryListComputers : ActiveDirectoryListBase
{
protected override string Summary => "Lists all computer names in an ActiveDirectory";
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsComputer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright (c) 2021 Steven Foster ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using HavokMultimedia.Utilities.Console.External;

namespace HavokMultimedia.Utilities.Console.Commands
{
public class ActiveDirectoryListUsers : ActiveDirectoryListBase
{
protected override string Summary => "Lists all user names in an ActiveDirectory";
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsUser;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright (c) 2021 Steven Foster ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System.Collections.Generic;
using System.Linq;
using HavokMultimedia.Utilities.Console.External;

namespace HavokMultimedia.Utilities.Console.Commands
{
public class ActiveDirectoryListUsersOfGroup : ActiveDirectoryListBase
{
protected override string Summary => "Lists all user names that are members of the specified group in an ActiveDirectory";
protected override string Example => base.Example + " M?Group*";
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsUser && obj.MemberOfNames.Any(o => o.EqualsWildcard(group, true));
private string group;
protected override void ExecuteInternal()
{
group = GetArgValueTrimmed(0);
log.Debug($"{nameof(group)}: {group}");
if (group == null) throw new ArgsException(nameof(group), $"No {nameof(group)} specified");

base.ExecuteInternal();
}
protected override string Display(ActiveDirectoryObject obj, Format format)
{
var matchedGroups = new List<string>();
foreach (var m in obj.MemberOfNames)
{
if (m.EqualsWildcard(group, true)) matchedGroups.Add(m);
}
if (format == Format.Display) return base.Display(obj, format) + " (" + matchedGroups.ToStringDelimited(",") + ")";
if (format == Format.TAB) return base.Display(obj, format) + "\t" + matchedGroups.ToStringDelimited(",");
return base.Display(obj, format);
}

}
}

0 comments on commit 1c11bec

Please sign in to comment.