|
| 1 | +// Copyright 2020 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using NtApiDotNet; |
| 16 | +using System; |
| 17 | +using System.Management.Automation; |
| 18 | + |
| 19 | +namespace NtObjectManager.Cmdlets.Object |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// <para type="synopsis">Set the Default DACL for a Token.</para> |
| 23 | + /// <para type="description">This cmdlet sets the Default DACL for a Token.</para> |
| 24 | + /// </summary> |
| 25 | + /// <example> |
| 26 | + /// <code>Set-NtTokenDefaultDacl -DefaultDacl $dacl</code> |
| 27 | + /// <para>Set current effective token's Default DACL.</para> |
| 28 | + /// </example> |
| 29 | + /// <example> |
| 30 | + /// <code>Set-NtTokenDefaultDacl -SecurityDescriptor $sd</code> |
| 31 | + /// <para>Set current effective token's Default DACL from a Security Descriptor.</para> |
| 32 | + /// </example> |
| 33 | + /// <example> |
| 34 | + /// <code>Set-NtTokenDefaultDacl -DefaultDacl $dacl -Token $token</code> |
| 35 | + /// <para>Set Default DACL for a Token.</para> |
| 36 | + /// </example> |
| 37 | + [Cmdlet(VerbsCommon.Set, "NtTokenDefaultDacl", DefaultParameterSetName = "FromAcl")] |
| 38 | + public class SetNtTokenDefaultDacl : PSCmdlet |
| 39 | + { |
| 40 | + private NtToken GetToken() |
| 41 | + { |
| 42 | + if (Token != null) |
| 43 | + return Token.Duplicate(); |
| 44 | + return NtToken.OpenEffectiveToken(NtThread.Current, true, false, TokenAccessRights.AdjustDefault); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// <para type="description">Specify the default DACL.</para> |
| 49 | + /// </summary> |
| 50 | + [Parameter(ParameterSetName = "FromAcl", Position = 1, Mandatory = true)] |
| 51 | + [AllowEmptyCollection] |
| 52 | + public Acl DefaultDacl { get; set; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// <para type="description">Specify the default DACL as a Security Descriptor.</para> |
| 56 | + /// </summary> |
| 57 | + [Parameter(ParameterSetName = "FromSD", Position = 1, Mandatory = true)] |
| 58 | + public SecurityDescriptor SecurityDescriptor { get; set; } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// <para type="description">Specify the token to set the default DACL.</para> |
| 62 | + /// </summary> |
| 63 | + [Parameter(Position = 1)] |
| 64 | + public NtToken Token { get; set; } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Overridden ProcessRecord method. |
| 68 | + /// </summary> |
| 69 | + protected override void ProcessRecord() |
| 70 | + { |
| 71 | + |
| 72 | + Acl default_dacl = null; |
| 73 | + |
| 74 | + switch (ParameterSetName) |
| 75 | + { |
| 76 | + case "FromAcl": |
| 77 | + default_dacl = DefaultDacl; |
| 78 | + break; |
| 79 | + case "FromSD": |
| 80 | + default_dacl = SecurityDescriptor.Dacl; |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + if (default_dacl == null) |
| 85 | + { |
| 86 | + throw new ArgumentNullException(nameof(DefaultDacl)); |
| 87 | + } |
| 88 | + |
| 89 | + using (var token = GetToken()) |
| 90 | + { |
| 91 | + token.SetDefaultDacl(default_dacl); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments