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.

102 lines
3.5 KiB

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