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.
 
 

138 lines
4.5 KiB

  1. using System.Management.Automation;
  2. using Security2;
  3. using System.IO;
  4. using System.Linq;
  5. using System;
  6. using System.Security.AccessControl;
  7. namespace NTFSSecurity
  8. {
  9. #region Enable-Privileges
  10. [Cmdlet(VerbsLifecycle.Enable, "Privileges")]
  11. [OutputType(typeof(ProcessPrivileges.PrivilegeAndAttributes))]
  12. public class EnablePrivileges : BaseCmdletWithPrivControl
  13. {
  14. private bool enablePrivileges = false;
  15. private SwitchParameter passThru;
  16. public string[] Path { get; set; }
  17. [Parameter]
  18. public SwitchParameter PassThru
  19. {
  20. get { return passThru; }
  21. set { passThru = value; }
  22. }
  23. protected override void BeginProcessing()
  24. {
  25. base.BeginProcessing();
  26. }
  27. protected override void ProcessRecord()
  28. {
  29. var privateData = (System.Collections.Hashtable)this.MyInvocation.MyCommand.Module.PrivateData;
  30. var psCallStack = (CallStackFrame)this.InvokeCommand.InvokeScript("Get-PSCallStack")[1].BaseObject;
  31. try
  32. {
  33. enablePrivileges = (bool)privateData["EnablePrivileges"];
  34. }
  35. catch (Exception ex)
  36. {
  37. throw new ParseException("Could not parse the module's PrivateData field in the module's psd1 file. Please refer to the documentation for further details", ex);
  38. }
  39. //if the command is called from NTFSSecurity.Init.ps1 and EnablePrivileges is set to true in the NTFSSecurity.psd1 or if the cmdlet is called from somewhere else
  40. if ((psCallStack.InvocationInfo.MyCommand.Name == "NTFSSecurity.Init.ps1" && enablePrivileges == true))
  41. {
  42. this.EnableFileSystemPrivileges(false);
  43. }
  44. else if (psCallStack.InvocationInfo.MyCommand.Name != "NTFSSecurity.Init.ps1")
  45. {
  46. this.EnableFileSystemPrivileges(false);
  47. }
  48. if (passThru)
  49. {
  50. this.WriteObject(this.privControl.GetPrivileges());
  51. }
  52. }
  53. protected override void EndProcessing()
  54. {
  55. //nothing as we want to keep the privileges enabled
  56. }
  57. }
  58. #endregion Enable-Privileges
  59. #region Disable-Privileges
  60. [Cmdlet(VerbsLifecycle.Disable, "Privileges")]
  61. [OutputType(typeof(ProcessPrivileges.PrivilegeAndAttributes))]
  62. public class DisablePrivileges : BaseCmdletWithPrivControl
  63. {
  64. private SwitchParameter passThru;
  65. public string[] Path { get; set; }
  66. [Parameter]
  67. public SwitchParameter PassThru
  68. {
  69. get { return passThru; }
  70. set { passThru = value; }
  71. }
  72. protected override void BeginProcessing()
  73. {
  74. base.BeginProcessing();
  75. }
  76. protected override void ProcessRecord()
  77. {
  78. if (this.privControl.GetPrivileges()
  79. .Where(p => p.PrivilegeState == ProcessPrivileges.PrivilegeState.Enabled)
  80. .Where(p => (
  81. p.Privilege == ProcessPrivileges.Privilege.TakeOwnership) |
  82. (p.Privilege == ProcessPrivileges.Privilege.Restore) |
  83. (p.Privilege == ProcessPrivileges.Privilege.Backup))
  84. .Count() == 0)
  85. {
  86. this.WriteError(new ErrorRecord(new AdjustPriviledgeException("Privileges are not enabled"), "Disable Privilege Error", ErrorCategory.SecurityError, null));
  87. return;
  88. }
  89. this.DisableFileSystemPrivileges();
  90. this.WriteVerbose("The privileges 'TakeOwnership', 'Restore' and 'Backup' are now enabled.");
  91. if (passThru)
  92. {
  93. this.WriteObject(this.privControl.GetPrivileges());
  94. }
  95. }
  96. protected override void EndProcessing()
  97. {
  98. //nothing as priviliges should already been cleaned up
  99. }
  100. }
  101. #endregion Enable-Privileges
  102. #region Get-Privileges
  103. [Cmdlet(VerbsCommon.Get, "Privileges")]
  104. [OutputType(typeof(ProcessPrivileges.PrivilegeAndAttributes))]
  105. public class GetPrivileges : BaseCmdlet
  106. {
  107. public string[] Path { get; set; }
  108. protected override void BeginProcessing()
  109. {
  110. base.BeginProcessing();
  111. }
  112. protected override void ProcessRecord()
  113. {
  114. var privControl = new PrivilegeControl();
  115. this.WriteObject(privControl.GetPrivileges(), true);
  116. }
  117. }
  118. #endregion Get-Privileges
  119. }