CLI tool for running Playbooks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.4 KiB

1 year ago
  1. using System;
  2. using System.DirectoryServices;
  3. using System.Threading.Tasks;
  4. using TrustedUninstaller.Shared.Tasks;
  5. using YamlDotNet.Serialization;
  6. using System.DirectoryServices.AccountManagement;
  7. using System.Security.Principal;
  8. namespace TrustedUninstaller.Shared.Actions
  9. {
  10. public class UserAction : ITaskAction
  11. {
  12. [YamlMember(typeof(string), Alias = "name")]
  13. public string Username { get; set; } = "";
  14. [YamlMember(typeof(bool), Alias = "admin")]
  15. public bool IsAdmin { get; set; } = false;
  16. [YamlMember(typeof(string), Alias = "weight")]
  17. public int ProgressWeight { get; set; } = 1;
  18. public int GetProgressWeight() => ProgressWeight;
  19. private bool InProgress { get; set; }
  20. public void ResetProgress() => InProgress = false;
  21. public string ErrorString() => $"UserAction failed to change permissions for user {Username}.";
  22. public UninstallTaskStatus GetStatus()
  23. {
  24. using var pc = new PrincipalContext(ContextType.Machine);
  25. var up = UserPrincipal.FindByIdentity(
  26. pc,
  27. IdentityType.SamAccountName,
  28. this.Username);
  29. var userExists = (up != null);
  30. if (!IsAdmin || !userExists) return userExists ? UninstallTaskStatus.Completed : UninstallTaskStatus.ToDo;
  31. var identity = new WindowsIdentity(up.UserPrincipalName);
  32. var principal = new WindowsPrincipal(identity);
  33. var isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
  34. return isAdmin ? UninstallTaskStatus.Completed : UninstallTaskStatus.ToDo;
  35. }
  36. public async Task<bool> RunTask()
  37. {
  38. if (this.GetStatus() != UninstallTaskStatus.ToDo)
  39. {
  40. return false;
  41. }
  42. Console.WriteLine($"Changing permissions for user '{Username}'...");
  43. return await Task.Run(() =>
  44. {
  45. using var pc = new PrincipalContext(ContextType.Machine);
  46. var up = UserPrincipal.FindByIdentity(
  47. pc,
  48. IdentityType.SamAccountName,
  49. this.Username);
  50. var userExists = (up != null);
  51. var ad = new DirectoryEntry("WinNT://" +
  52. Environment.MachineName + ",computer");
  53. if (!userExists)
  54. {
  55. var newUser = ad.Children.Add(this.Username, "user");
  56. newUser.Invoke("SetPassword", "user");
  57. newUser.Invoke("Put", "Description", "Created by the AME Wizard");
  58. newUser.CommitChanges();
  59. if (IsAdmin)
  60. {
  61. var group = ad.Children.Find("Administrators", "group");
  62. group.Invoke("Add", newUser.Path);
  63. group.CommitChanges();
  64. }
  65. }
  66. else
  67. {
  68. if (IsAdmin)
  69. {
  70. var group = ad.Children.Find("Administrators", "group");
  71. group.Invoke("Add", up.UserPrincipalName);
  72. group.CommitChanges();
  73. }
  74. }
  75. return true;
  76. });
  77. }
  78. }
  79. }