From 1c11becf59171949a3f50e13610c39f559ebdb35 Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 20 May 2021 16:01:30 -0400 Subject: [PATCH] Updated ActiveDirectoryListUsersOfGroup to allow for TAB exporting --- .../Commands/ActiveDirectoryListBase.cs | 65 +++++++++++++++ .../Commands/ActiveDirectoryListComputers.cs | 26 ++++++ .../Commands/ActiveDirectoryListGroups.cs | 26 ++++++ .../Commands/ActiveDirectoryListObjects.cs | 80 ------------------- .../Commands/ActiveDirectoryListUsers.cs | 26 ++++++ .../ActiveDirectoryListUsersOfGroup.cs | 50 ++++++++++++ 6 files changed, 193 insertions(+), 80 deletions(-) create mode 100644 HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListBase.cs create mode 100644 HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListComputers.cs create mode 100644 HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListGroups.cs create mode 100644 HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListUsers.cs create mode 100644 HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListUsersOfGroup.cs diff --git a/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListBase.cs b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListBase.cs new file mode 100644 index 0000000..e271f23 --- /dev/null +++ b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListBase.cs @@ -0,0 +1,65 @@ +/* +Copyright (c) 2021 Steven Foster (steven.d.foster@gmail.com) + +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()); + } + + + + 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); + } +} diff --git a/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListComputers.cs b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListComputers.cs new file mode 100644 index 0000000..4b86663 --- /dev/null +++ b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListComputers.cs @@ -0,0 +1,26 @@ +/* +Copyright (c) 2021 Steven Foster (steven.d.foster@gmail.com) + +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; + } +} diff --git a/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListGroups.cs b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListGroups.cs new file mode 100644 index 0000000..2b7c80d --- /dev/null +++ b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListGroups.cs @@ -0,0 +1,26 @@ +/* +Copyright (c) 2021 Steven Foster (steven.d.foster@gmail.com) + +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; + } +} diff --git a/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListObjects.cs b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListObjects.cs index 5b517fc..db38b43 100644 --- a/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListObjects.cs +++ b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListObjects.cs @@ -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(); - 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; - } } diff --git a/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListUsers.cs b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListUsers.cs new file mode 100644 index 0000000..181b4ee --- /dev/null +++ b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListUsers.cs @@ -0,0 +1,26 @@ +/* +Copyright (c) 2021 Steven Foster (steven.d.foster@gmail.com) + +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; + } +} diff --git a/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListUsersOfGroup.cs b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListUsersOfGroup.cs new file mode 100644 index 0000000..fbf0604 --- /dev/null +++ b/HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListUsersOfGroup.cs @@ -0,0 +1,50 @@ +/* +Copyright (c) 2021 Steven Foster (steven.d.foster@gmail.com) + +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(); + 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); + } + + } +}