Skip to content

Commit

Permalink
Added comments to Misc.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
krisdb2009 committed Jan 6, 2020
1 parent 87523cc commit c87b9ad
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
95 changes: 90 additions & 5 deletions SuperGrate/Classes/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using Microsoft.Win32;
using System.IO;
using System.Net.NetworkInformation;
using System.Security.Principal;
using System.Management;
using SuperGrate.UserList;
Expand All @@ -13,8 +12,19 @@ namespace SuperGrate
{
class Misc
{
/// <summary>
/// A list of Local, Non-Domain SIDs on a host set by: GetLocalUsersSIDsFromHost().
/// </summary>
public static Dictionary<string, string> LocalSIDToUser = new Dictionary<string, string>();
/// <summary>
/// If true, will cancel any task pertaining to Remote Profile Deletions.
/// </summary>
private static bool ShouldCancelRemoteProfileDelete = false;
/// <summary>
/// Checks if the host provided is the current machine running Super Grate.
/// </summary>
/// <param name="Host">Hostname to check against.</param>
/// <returns>True if is this machine, false if otherwise.</returns>
public static bool IsHostThisMachine(string Host)
{
if(Host.ToLower() == Environment.MachineName.ToLower())
Expand All @@ -27,6 +37,11 @@ public static bool IsHostThisMachine(string Host)
}
return false;
}
/// <summary>
/// Returns the best path to "ManagementScope" based on the host provided. (If the host provided is the current host then the best path is "\root\cimv2" otherwise it is "\\HOST\root\cimv2".)
/// </summary>
/// <param name="Host">Host to check against.</param>
/// <returns>Either "\root\cimv2" or "\\HOST\root\cimv2".</returns>
public static string GetBestManagementScope(string Host)
{
if (IsHostThisMachine(Host))
Expand All @@ -38,6 +53,11 @@ public static string GetBestManagementScope(string Host)
return @"\\" + Host + @"\root\cimv2";
}
}
/// <summary>
/// Returns the best path to C:\ based on the host provided. (If the host provided is the current host then the best path is "C:\" otherwise it is "\\HOST\C$\".)
/// </summary>
/// <param name="Host">Host to check against.</param>
/// <returns>Either "C:\" or "\\HOST\C$\".</returns>
public static string GetBestPathToC(string Host)
{
if(IsHostThisMachine(Host))
Expand All @@ -49,6 +69,10 @@ public static string GetBestPathToC(string Host)
return @"\\" + Host + @"\C$\";
}
}
/// <summary>
/// Fills the LocalSIDToUser property for other methods in this class with a list of SIDs (Security Identifiers / Local User Profiles (Non-Domain)) from a host.
/// </summary>
/// <param name="Host">Host to get SIDs from.</param>
public static void GetLocalUsersSIDsFromHost(string Host)
{
LocalSIDToUser.Clear();
Expand All @@ -69,6 +93,11 @@ public static void GetLocalUsersSIDsFromHost(string Host)
Logger.Exception(e, "Failed to get a list of Local Users' SIDs from host: " + Host + ".");
}
}
/// <summary>
/// Attempts to resolve an identity (Store ID / Security ID) to a DOMAIN\USERNAME string. If failed, returns the given identity string.
/// </summary>
/// <param name="Identity">A Store ID or SID (Security Identifier) to resolve.</param>
/// <returns>DOMAIN\USERNAME.</returns>
public static string GetUserByIdentity(string Identity)
{
try
Expand All @@ -95,6 +124,13 @@ public static string GetUserByIdentity(string Identity)
return Identity;
}
}
/// <summary>
/// Retrieves a single users properties from a Host.
/// </summary>
/// <param name="TemplateRow">A template row to fill in with information from the host.</param>
/// <param name="Host">A host computer to get the information from.</param>
/// <param name="SID">An SID (Security Identifier) of the user profile on the host.</param>
/// <returns>Filled in UserRow.</returns>
public static Task<UserRow> GetUserFromHost(UserRow TemplateRow, string Host, string SID)
{
return Task.Run(() =>
Expand Down Expand Up @@ -150,6 +186,11 @@ public static Task<UserRow> GetUserFromHost(UserRow TemplateRow, string Host, st
return row;
});
}
/// <summary>
/// Retrieves users' properties from host based on the ULControl.CurrentHeaderRow property.
/// </summary>
/// <param name="Host">Host to get information from.</param>
/// <returns>UserRows.</returns>
public static Task<UserRows> GetUsersFromHost(string Host)
{
return Task.Run(async () =>
Expand Down Expand Up @@ -196,6 +237,11 @@ public static Task<UserRows> GetUsersFromHost(string Host)
}
});
}
/// <summary>
/// Gets SID string from an Object in the Store.
/// </summary>
/// <param name="ID">ID of the Store Object.</param>
/// <returns>String of SID.</returns>
static public Task<string> GetSIDFromStore(string ID)
{
return Task.Run(() => {
Expand All @@ -218,6 +264,12 @@ static public Task<string> GetSIDFromStore(string ID)
}
});
}
/// <summary>
/// Gets a single user from the Backup Store and returns the information in a UserRow object.
/// </summary>
/// <param name="TemplateRow">A template UserRow to fill the values of.</param>
/// <param name="ID">Backup Store ID to retrieve.</param>
/// <returns>UserRow object.</returns>
static public Task<UserRow> GetUserFromStore(UserRow TemplateRow, string ID)
{
Dictionary<ULColumnType, string> Files = new Dictionary<ULColumnType, string>
Expand Down Expand Up @@ -267,6 +319,10 @@ static public Task<UserRow> GetUserFromStore(UserRow TemplateRow, string ID)
}
});
}
/// <summary>
/// Retrieves users from the Backup Store in a UserRows object. (UserRows are based off of the ULControl.CurrentHeaderRow property.)
/// </summary>
/// <returns>A UserRow object.</returns>
static public Task<UserRows> GetUsersFromStore()
{
return Task.Run(async () =>
Expand Down Expand Up @@ -297,6 +353,11 @@ static public Task<UserRows> GetUsersFromStore()
}
});
}
/// <summary>
/// Deletes store items based on their ID.
/// </summary>
/// <param name="IDs">List of IDs (Profile Backups) to delete from the store.</param>
/// <returns>Awaitable Task.</returns>
public static Task DeleteFromStore(string[] IDs)
{
return Task.Run(() =>
Expand All @@ -317,6 +378,12 @@ public static Task DeleteFromStore(string[] IDs)
}
});
}
/// <summary>
/// Deletes a list of SIDs (Security Identifiers (User Profiles)) from a host.
/// </summary>
/// <param name="Host">Host to delete profiles from.</param>
/// <param name="SIDs">List of SIDs that identify the profiles that need to be deleted.</param>
/// <returns>Awaitable Task.</returns>
public static Task DeleteFromSource(string Host, string[] SIDs)
{
ShouldCancelRemoteProfileDelete = false;
Expand Down Expand Up @@ -357,11 +424,15 @@ await Remote.StartProcess(
}
});
}
public static async void CancelRemoteProfileDelete(string Target)
/// <summary>
/// Cancels the agent on the remote computer running the profile delete command.
/// </summary>
/// <param name="Host">Hostname to cancel profile delete agent on.</param>
public static async void CancelRemoteProfileDelete(string Host)
{
ShouldCancelRemoteProfileDelete = true;
Logger.Information("Sending KILL command to remote target...");
if (await Remote.KillProcess(Target, "SuperGratePD.exe"))
if (await Remote.KillProcess(Host, "SuperGratePD.exe"))
{
Logger.Success("KILL command sent.");
}
Expand All @@ -370,6 +441,11 @@ public static async void CancelRemoteProfileDelete(string Target)
Logger.Error("Failed to send KILL command.");
}
}
/// <summary>
/// Gets the CPU architecture from a remote host.
/// </summary>
/// <param name="Host">Host to get the architecture information from.</param>
/// <returns>CPU architecture.</returns>
public static Task<string> GetRemoteArch(string Host)
{
Logger.Information("Reading OS Architecture...");
Expand All @@ -395,10 +471,19 @@ public static Task<string> GetRemoteArch(string Host)
return null;
});
}
public static void MainMenuSetState(MainMenu Menu, bool Enabled)
/// <summary>
/// Enables or disables top level menu items in a MainMenu object.
/// </summary>
/// <param name="Menu">The MainMenu object.</param>
/// <param name="Enabled">Enable or disable the menu items?</param>
/// <param name="Menus">An array of top level menu items' "text" properties to affect.</param>
public static void MainMenuSetState(MainMenu Menu, bool Enabled, string[] Menus = null)
{
foreach(MenuItem mi in Menu.MenuItems)
List<string> lMenus = new List<string>();
if (Menus != null) lMenus.AddRange(Menus);
foreach (MenuItem mi in Menu.MenuItems)
{
if (Menus != null && !lMenus.Contains(mi.Text)) continue;
mi.Enabled = Enabled;
}
}
Expand Down
2 changes: 1 addition & 1 deletion SuperGrate/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private RunningTask Running {
btnStartStop.Text = "Stop";
Cursor = Cursors.AppStarting;
Logger.UpdateProgress(true);
Misc.MainMenuSetState(MainMenu, false);
Misc.MainMenuSetState(MainMenu, false, new string[] { "&Settings", "&View" });
storeRunningTask = value;
imgLoadLogo.Enabled =
btnStartStop.Enabled =
Expand Down
3 changes: 0 additions & 3 deletions SuperGrate/Main.resx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,4 @@
<metadata name="dialogSaveLog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>127, 17</value>
</metadata>
<metadata name="helpProvider.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>258, 17</value>
</metadata>
</root>

0 comments on commit c87b9ad

Please sign in to comment.