using System;
using System.Runtime;
using System.Security;
using System.Security.AccessControl;
using System.Security.Principal;
namespace Microsoft.Win32.TaskScheduler
{
///
/// Specifies the access control rights that can be applied to Task Scheduler tasks.
///
[Flags]
public enum TaskRights
{
/// Specifies the right to exert full control over a task folder or task, and to modify access control and audit rules. This value represents the right to do anything with a task and is the combination of all rights in this enumeration.
FullControl = 0x1f01ff,
/// Specifies the right to create tasks and folders, and to add or remove data from tasks. This right includes the following rights: .
Write = 0x120116,
/// Specifies the right to open and copy folders or tasks as read-only. This right includes the following rights: .
Read = 0x120089,
/// Specifies the right run tasks. This right includes the following rights: .
Execute = 0x120089,
/// The right to wait on a task.
Synchronize = 0x100000,
/// The right to change the owner of a task.
TakeOwnership = 0x80000,
/// Specifies the right to change the security and audit rules associated with a task or folder.
ChangePermissions = 0x40000,
/// The right to open and copy the access rules and audit rules for a task.
ReadPermissions = 0x20000,
/// The right to delete a folder or task.
Delete = 0x10000,
/// Specifies the right to open and write file system attributes to a folder or file. This does not include the ability to write data, extended attributes, or access and audit rules.
WriteAttributes = 0x100,
/// Specifies the right to open and copy file system attributes from a folder or task. For example, this value specifies the right to view the file creation or modified date. This does not include the right to read data, extended file system attributes, or access and audit rules.
ReadAttributes = 0x80,
/// Specifies the right to delete a folder and any tasks contained within that folder.
DeleteChild = 0x40,
/// Specifies the right to run a task.
ExecuteFile = 0x20,
/// Specifies the right to open and write extended file system attributes to a folder or file. This does not include the ability to write data, attributes, or access and audit rules.
WriteExtendedAttributes = 0x10,
/// Specifies the right to open and copy extended system attributes from a folder or task. For example, this value specifies the right to view author and content information. This does not include the right to read data, system attributes, or access and audit rules.
ReadExtendedAttributes = 8,
/// Specifies the right to append data to the end of a file.
AppendData = 4,
/// Specifies the right to open and write to a file or folder. This does not include the right to open and write file system attributes, extended file system attributes, or access and audit rules.
WriteData = 2,
/// Specifies the right to open and copy a task or folder. This does not include the right to read file system attributes, extended file system attributes, or access and audit rules.
ReadData = 1,
}
///
/// Represents a set of access rights allowed or denied for a user or group. This class cannot be inherited.
///
public sealed class TaskAccessRule : AccessRule
{
///
/// Initializes a new instance of the class, specifying the user or group the rule applies to, the access rights, and whether the specified access rights are allowed or denied.
///
/// The user or group the rule applies to. Must be of type or a type such as that can be converted to type .
/// A bitwise combination of values specifying the rights allowed or denied.
/// One of the values specifying whether the rights are allowed or denied.
public TaskAccessRule(IdentityReference identity, TaskRights eventRights, AccessControlType type)
: this(identity, (int)eventRights, false, InheritanceFlags.None, PropagationFlags.None, type)
{
}
///
/// Initializes a new instance of the class, specifying the name of the user or group the rule applies to, the access rights, and whether the specified access rights are allowed or denied.
///
/// The name of the user or group the rule applies to.
/// A bitwise combination of values specifying the rights allowed or denied.
/// One of the values specifying whether the rights are allowed or denied.
public TaskAccessRule(string identity, TaskRights eventRights, AccessControlType type)
: this(new NTAccount(identity), (int)eventRights, false, InheritanceFlags.None, PropagationFlags.None, type)
{
}
internal TaskAccessRule(IdentityReference identity, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
: base(identity, accessMask, isInherited, inheritanceFlags, propagationFlags, type)
{
}
///
/// Gets the rights allowed or denied by the access rule.
///
///
/// A bitwise combination of values indicating the rights allowed or denied by the access rule.
///
public TaskRights TaskRights
{
get { return (TaskRights)base.AccessMask; }
}
}
///
/// Represents a set of access rights to be audited for a user or group. This class cannot be inherited.
///
public sealed class TaskAuditRule : AuditRule
{
///
/// Initializes a new instance of the class, specifying the user or group to audit, the rights to audit, and whether to audit success, failure, or both.
///
/// The user or group the rule applies to. Must be of type or a type such as that can be converted to type .
/// A bitwise combination of values specifying the kinds of access to audit.
/// The audit flags.
public TaskAuditRule(IdentityReference identity, TaskRights eventRights, AuditFlags flags)
: this(identity, (int)eventRights, false, InheritanceFlags.None, PropagationFlags.None, flags)
{
}
internal TaskAuditRule(IdentityReference identity, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags)
: base(identity, accessMask, isInherited, inheritanceFlags, propagationFlags, flags)
{
}
///
/// Gets the access rights affected by the audit rule.
///
///
/// A bitwise combination of values that indicates the rights affected by the audit rule.
///
/// objects are immutable. You can create a new audit rule representing a different user, different rights, or a different combination of AuditFlags values, but you cannot modify an existing audit rule.
public TaskRights TaskRights
{
get { return (TaskRights)base.AccessMask; }
}
}
///
/// Represents the Windows access control security for a Task Scheduler task. This class cannot be inherited.
///
///
/// A TaskSecurity object specifies access rights for a Task Scheduler task, and also specifies how access attempts are audited. Access rights to the task are expressed as rules, with each access rule represented by a object. Each auditing rule is represented by a object.
/// This mirrors the underlying Windows security system, in which each securable object has at most one discretionary access control list (DACL) that controls access to the secured object, and at most one system access control list (SACL) that specifies which access attempts are audited. The DACL and SACL are ordered lists of access control entries (ACE) that specify access and auditing for users and groups. A or object might represent more than one ACE.
/// Note
/// A object can represent a local task or a Task Scheduler task. Windows access control security is meaningful only for Task Scheduler tasks.
/// The TaskSecurity, , and classes hide the implementation details of ACLs and ACEs. They allow you to ignore the seventeen different ACE types and the complexity of correctly maintaining inheritance and propagation of access rights. These objects are also designed to prevent the following common access control errors:
///
/// - Creating a security descriptor with a null DACL. A null reference to a DACL allows any user to add access rules to an object, potentially creating a denial-of-service attack. A new TaskSecurity object always starts with an empty DACL, which denies all access for all users.
/// - Violating the canonical ordering of ACEs. If the ACE list in the DACL is not kept in the canonical order, users might inadvertently be given access to the secured object. For example, denied access rights must always appear before allowed access rights. TaskSecurity objects maintain the correct order internally.
/// - Manipulating security descriptor flags, which should be under resource manager control only.
/// - Creating invalid combinations of ACE flags.
/// - Manipulating inherited ACEs. Inheritance and propagation are handled by the resource manager, in response to changes you make to access and audit rules.
/// - Inserting meaningless ACEs into ACLs.
///
/// The only capabilities not supported by the .NET security objects are dangerous activities that should be avoided by the majority of application developers, such as the following:
///
/// - Low-level tasks that are normally performed by the resource manager.
/// - Adding or removing access control entries in ways that do not maintain the canonical ordering.
///
/// To modify Windows access control security for a task, use the method to get the TaskSecurity object. Modify the security object by adding and removing rules, and then use the method to reattach it.
/// Important: Changes you make to a TaskSecurity object do not affect the access levels of the task until you call the method to assign the altered security object to the task.
/// To copy access control security from one task to another, use the method to get a TaskSecurity object representing the access and audit rules for the first task, then use the method, or a constructor that accepts a TaskSecurity object, to assign those rules to the second task.
/// Users with an investment in the security descriptor definition language (SDDL) can use the method to set access rules for a task, and the method to obtain a string that represents the access rules in SDDL format. This is not recommended for new development.
///
public sealed class TaskSecurity : CommonObjectSecurity
{
///
/// Initializes a new instance of the class with default values.
///
public TaskSecurity()
: base(false)
{
}
///
/// Initializes a new instance of the class with the specified sections of the access control security rules from the specified task.
///
/// The task.
/// The sections of the ACL to retrieve.
public TaskSecurity(Task task, AccessControlSections sections = System.Security.AccessControl.AccessControlSections.Access | System.Security.AccessControl.AccessControlSections.Owner | System.Security.AccessControl.AccessControlSections.Group)
: base(false)
{
this.SetSecurityDescriptorSddlForm(task.GetSecurityDescriptorSddlForm(TaskSecurity.Convert(sections)));
}
///
/// Initializes a new instance of the class with the specified sections of the access control security rules from the specified task.
///
/// The folder.
/// The sections of the ACL to retrieve.
public TaskSecurity(TaskFolder folder, AccessControlSections sections = System.Security.AccessControl.AccessControlSections.Access | System.Security.AccessControl.AccessControlSections.Owner | System.Security.AccessControl.AccessControlSections.Group)
: base(false)
{
this.SetSecurityDescriptorSddlForm(folder.GetSecurityDescriptorSddlForm(TaskSecurity.Convert(sections)));
}
///
/// Gets the enumeration that the class uses to represent access rights.
///
/// A object representing the enumeration.
public override Type AccessRightType
{
get { return typeof(TaskRights); }
}
///
/// Gets the type that the TaskSecurity class uses to represent access rules.
///
/// A object representing the class.
public override Type AccessRuleType
{
get { return typeof(TaskAccessRule); }
}
///
/// Gets the type that the TaskSecurity class uses to represent audit rules.
///
/// A object representing the class.
public override Type AuditRuleType
{
get { return typeof(TaskAuditRule); }
}
///
/// Creates a new access control rule for the specified user, with the specified access rights, access control, and flags.
///
/// An that identifies the user or group the rule applies to.
/// A bitwise combination of values specifying the access rights to allow or deny, cast to an integer.
/// Meaningless for tasks, because they have no hierarchy.
/// Meaningless for tasks, because they have no hierarchy.
/// Meaningless for tasks, because they have no hierarchy.
/// One of the values specifying whether the rights are allowed or denied.
///
/// The object that this method creates.
///
public override AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
{
return new TaskAccessRule(identityReference, (TaskRights)accessMask, type);
}
///
/// Searches for a matching rule with which the new rule can be merged. If none are found, adds the new rule.
///
/// The access control rule to add.
public void AddAccessRule(TaskAccessRule rule)
{
base.AddAccessRule(rule);
}
///
/// Searches for an audit rule with which the new rule can be merged. If none are found, adds the new rule.
///
/// The audit rule to add. The user specified by this rule determines the search.
public void AddAuditRule(TaskAuditRule rule)
{
base.AddAuditRule(rule);
}
///
/// Creates a new audit rule, specifying the user the rule applies to, the access rights to audit, and the outcome that triggers the audit rule.
///
/// An that identifies the user or group the rule applies to.
/// A bitwise combination of values specifying the access rights to audit, cast to an integer.
/// Meaningless for tasks, because they have no hierarchy.
/// Meaningless for tasks, because they have no hierarchy.
/// Meaningless for tasks, because they have no hierarchy.
/// One of the values specifying whether to audit successful access, failed access, or both.
///
/// A object representing the specified audit rule for the specified user. The return type of the method is the base class, , but the return value can be cast safely to the derived class.
///
public override AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags)
{
return new TaskAuditRule(identityReference, accessMask, isInherited, inheritanceFlags, propagationFlags, flags);
}
///
/// Searches for an access control rule with the same user and (allow or deny) as the specified rule, and with compatible inheritance and propagation flags; if such a rule is found, the rights contained in the specified access rule are removed from it.
///
/// A that specifies the user and to search for, and a set of inheritance and propagation flags that a matching rule, if found, must be compatible with. Specifies the rights to remove from the compatible rule, if found.
/// true if a compatible rule is found; otherwise false.
public bool RemoveAccessRule(TaskAccessRule rule)
{
return base.RemoveAccessRule(rule);
}
///
/// Searches for all access control rules with the same user and (allow or deny) as the specified rule and, if found, removes them.
///
/// A that specifies the user and to search for, and a set of inheritance and propagation flags that a matching rule, if found, must be compatible with. Any rights specified by this rule are ignored.
public void RemoveAccessRuleAll(TaskAccessRule rule)
{
base.RemoveAccessRuleAll(rule);
}
///
/// Searches for an access control rule that exactly matches the specified rule and, if found, removes it.
///
/// The to remove.
public void RemoveAccessRuleSpecific(TaskAccessRule rule)
{
base.RemoveAccessRuleSpecific(rule);
}
///
/// Searches for an audit control rule with the same user as the specified rule, and with compatible inheritance and propagation flags; if a compatible rule is found, the rights contained in the specified rule are removed from it.
///
/// A that specifies the user to search for, and a set of inheritance and propagation flags that a matching rule, if found, must be compatible with. Specifies the rights to remove from the compatible rule, if found.
/// true if a compatible rule is found; otherwise false.
public bool RemoveAuditRule(TaskAuditRule rule)
{
return base.RemoveAuditRule(rule);
}
///
/// Searches for all audit rules with the same user as the specified rule and, if found, removes them.
///
/// A that specifies the user to search for. Any rights specified by this rule are ignored.
public void RemoveAuditRuleAll(TaskAuditRule rule)
{
base.RemoveAuditRuleAll(rule);
}
///
/// Searches for an audit rule that exactly matches the specified rule and, if found, removes it.
///
/// The to remove.
public void RemoveAuditRuleSpecific(TaskAuditRule rule)
{
base.RemoveAuditRuleSpecific(rule);
}
///
/// Removes all access control rules with the same user as the specified rule, regardless of , and then adds the specified rule.
///
/// The to add. The user specified by this rule determines the rules to remove before this rule is added.
public void ResetAccessRule(TaskAccessRule rule)
{
base.ResetAccessRule(rule);
}
///
/// Removes all access control rules with the same user and (allow or deny) as the specified rule, and then adds the specified rule.
///
/// The to add. The user and of this rule determine the rules to remove before this rule is added.
public void SetAccessRule(TaskAccessRule rule)
{
base.SetAccessRule(rule);
}
///
/// Removes all audit rules with the same user as the specified rule, regardless of the value, and then adds the specified rule.
///
/// The to add. The user specified by this rule determines the rules to remove before this rule is added.
public void SetAuditRule(TaskAuditRule rule)
{
base.SetAuditRule(rule);
}
internal static System.Security.AccessControl.SecurityInfos Convert(System.Security.AccessControl.AccessControlSections si)
{
System.Security.AccessControl.SecurityInfos ret = 0;
if ((si & System.Security.AccessControl.AccessControlSections.Audit) != 0)
ret |= System.Security.AccessControl.SecurityInfos.SystemAcl;
if ((si & System.Security.AccessControl.AccessControlSections.Access) != 0)
ret |= System.Security.AccessControl.SecurityInfos.DiscretionaryAcl;
if ((si & System.Security.AccessControl.AccessControlSections.Group) != 0)
ret |= System.Security.AccessControl.SecurityInfos.Group;
if ((si & System.Security.AccessControl.AccessControlSections.Owner) != 0)
ret |= System.Security.AccessControl.SecurityInfos.Owner;
return ret;
}
internal static System.Security.AccessControl.AccessControlSections Convert(System.Security.AccessControl.SecurityInfos si)
{
System.Security.AccessControl.AccessControlSections ret = System.Security.AccessControl.AccessControlSections.None;
if ((si & System.Security.AccessControl.SecurityInfos.SystemAcl) != 0)
ret |= System.Security.AccessControl.AccessControlSections.Audit;
if ((si & System.Security.AccessControl.SecurityInfos.DiscretionaryAcl) != 0)
ret |= System.Security.AccessControl.AccessControlSections.Access;
if ((si & System.Security.AccessControl.SecurityInfos.Group) != 0)
ret |= System.Security.AccessControl.AccessControlSections.Group;
if ((si & System.Security.AccessControl.SecurityInfos.Owner) != 0)
ret |= System.Security.AccessControl.AccessControlSections.Owner;
return ret;
}
internal AccessControlSections GetAccessControlSectionsFromChanges()
{
AccessControlSections none = AccessControlSections.None;
if (base.AccessRulesModified)
{
none = AccessControlSections.Access;
}
if (base.AuditRulesModified)
{
none |= AccessControlSections.Audit;
}
if (base.OwnerModified)
{
none |= AccessControlSections.Owner;
}
if (base.GroupModified)
{
none |= AccessControlSections.Group;
}
return none;
}
///
/// Saves the specified sections of the security descriptor associated with this object to permanent storage. We recommend that the values of the parameters passed to the constructor and persist methods be identical.
///
/// The task used to retrieve the persisted information.
/// One of the enumeration values that specifies the sections of the security descriptor (access rules, audit rules, owner, primary group) of the securable object to save.
[SecurityCritical]
internal void Persist(Task task, AccessControlSections includeSections = AccessControlSections.Access | AccessControlSections.Group | AccessControlSections.Owner)
{
base.WriteLock();
try
{
AccessControlSections accessControlSectionsFromChanges = this.GetAccessControlSectionsFromChanges();
if (accessControlSectionsFromChanges != AccessControlSections.None)
{
task.SetSecurityDescriptorSddlForm(this.GetSecurityDescriptorSddlForm(includeSections), TaskSecurity.Convert(includeSections));
base.OwnerModified = base.GroupModified = base.AccessRulesModified = base.AuditRulesModified = false;
}
}
finally
{
base.WriteUnlock();
}
}
///
/// Saves the specified sections of the security descriptor associated with this object to permanent storage. We recommend that the values of the parameters passed to the constructor and persist methods be identical.
///
/// The task folder used to retrieve the persisted information.
/// One of the enumeration values that specifies the sections of the security descriptor (access rules, audit rules, owner, primary group) of the securable object to save.
[SecurityCritical]
internal void Persist(TaskFolder folder, AccessControlSections includeSections = AccessControlSections.Access | AccessControlSections.Group | AccessControlSections.Owner)
{
base.WriteLock();
try
{
AccessControlSections accessControlSectionsFromChanges = this.GetAccessControlSectionsFromChanges();
if (accessControlSectionsFromChanges != AccessControlSections.None)
{
folder.SetSecurityDescriptorSddlForm(this.GetSecurityDescriptorSddlForm(includeSections), TaskSecurity.Convert(includeSections));
base.OwnerModified = base.GroupModified = base.AccessRulesModified = base.AuditRulesModified = false;
}
}
finally
{
base.WriteUnlock();
}
}
///
/// Saves the specified sections of the security descriptor associated with this object to permanent storage. We recommend that the values of the parameters passed to the constructor and persist methods be identical. For more information, see Remarks.
///
/// The name used to retrieve the persisted information.
/// One of the enumeration values that specifies the sections of the security descriptor (access rules, audit rules, owner, primary group) of the securable object to save.
protected override void Persist(string name, AccessControlSections includeSections = AccessControlSections.Access | AccessControlSections.Group | AccessControlSections.Owner)
{
using (var ts = new TaskService())
{
var task = ts.GetTask(name);
Persist(task, includeSections);
}
}
}
}