[corlib] Hunting down rare Task.WaitAll race
[mono.git] / mcs / class / corlib / System.Security.AccessControl / DiscretionaryAcl.cs
1 //
2 // System.Security.AccessControl.DiscretionaryAcl implementation
3 //
4 // Authors:
5 //      Dick Porter  <dick@ximian.com>
6 //      Atsushi Enomoto  <atsushi@ximian.com>
7 //      James Bellinger  <jfb@zer7.com>
8 //
9 // Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2012      James Bellinger
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Security.Principal;
33
34 namespace System.Security.AccessControl
35 {
36         public sealed class DiscretionaryAcl : CommonAcl
37         {
38                 public DiscretionaryAcl (bool isContainer, bool isDS, int capacity)
39                         : base (isContainer, isDS, capacity)
40                 {
41                 }
42                 
43                 public DiscretionaryAcl (bool isContainer, bool isDS, RawAcl rawAcl)
44                         : base (isContainer, isDS, rawAcl)
45                 {
46                 }
47                 
48                 public DiscretionaryAcl (bool isContainer, bool isDS, byte revision, int capacity)
49                         : base (isContainer, isDS, revision, capacity)
50                 {
51                 }
52
53                 public void AddAccess (AccessControlType accessType,
54                                        SecurityIdentifier sid, int accessMask,
55                                        InheritanceFlags inheritanceFlags,
56                                        PropagationFlags propagationFlags)
57                 {
58                         AddAce (GetAceQualifier (accessType), sid, accessMask,
59                                 inheritanceFlags, propagationFlags, AuditFlags.None);
60                 }
61                 
62                 public void AddAccess (AccessControlType accessType,
63                                        SecurityIdentifier sid, int accessMask,
64                                        InheritanceFlags inheritanceFlags,
65                                        PropagationFlags propagationFlags,
66                                        ObjectAceFlags objectFlags,
67                                        Guid objectType,
68                                        Guid inheritedObjectType)
69                 {
70                         AddAce (GetAceQualifier (accessType), sid, accessMask,
71                                 inheritanceFlags, propagationFlags, AuditFlags.None,
72                                 objectFlags, objectType, inheritedObjectType);
73                 }
74                 
75                 [MonoTODO]
76                 public bool RemoveAccess (AccessControlType accessType,
77                                           SecurityIdentifier sid,
78                                           int accessMask,
79                                           InheritanceFlags inheritanceFlags,
80                                           PropagationFlags propagationFlags)
81                 {
82                         throw new NotImplementedException ();
83                 }
84                 
85                 [MonoTODO]
86                 public bool RemoveAccess (AccessControlType accessType,
87                                           SecurityIdentifier sid,
88                                           int accessMask,
89                                           InheritanceFlags inheritanceFlags,
90                                           PropagationFlags propagationFlags,
91                                           ObjectAceFlags objectFlags,
92                                           Guid objectType,
93                                           Guid inheritedObjectType)
94                 {
95                         throw new NotImplementedException ();
96                 }
97                 
98                 public void RemoveAccessSpecific (AccessControlType accessType,
99                                                   SecurityIdentifier sid,
100                                                   int accessMask,
101                                                   InheritanceFlags inheritanceFlags,
102                                                   PropagationFlags propagationFlags)
103                 {
104                         RemoveAceSpecific (GetAceQualifier (accessType), sid, accessMask,
105                                            inheritanceFlags, propagationFlags, AuditFlags.None);
106                 }
107                 
108                 public void RemoveAccessSpecific (AccessControlType accessType,
109                                                   SecurityIdentifier sid,
110                                                   int accessMask,
111                                                   InheritanceFlags inheritanceFlags,
112                                                   PropagationFlags propagationFlags,
113                                                   ObjectAceFlags objectFlags,
114                                                   Guid objectType,
115                                                   Guid inheritedObjectType)
116                 {
117                         RemoveAceSpecific (GetAceQualifier (accessType), sid, accessMask,
118                                            inheritanceFlags, propagationFlags, AuditFlags.None,
119                                            objectFlags, objectType, inheritedObjectType);
120                 }
121                 
122                 public void SetAccess (AccessControlType accessType,
123                                        SecurityIdentifier sid,
124                                        int accessMask,
125                                        InheritanceFlags inheritanceFlags,
126                                        PropagationFlags propagationFlags)
127                 {
128                         SetAce (GetAceQualifier (accessType), sid, accessMask,
129                                 inheritanceFlags, propagationFlags, AuditFlags.None);
130                 }
131                 
132                 public void SetAccess (AccessControlType accessType,
133                                        SecurityIdentifier sid,
134                                        int accessMask,
135                                        InheritanceFlags inheritanceFlags,
136                                        PropagationFlags propagationFlags,
137                                        ObjectAceFlags objectFlags,
138                                        Guid objectType,
139                                        Guid inheritedObjectType)
140                 {
141                         SetAce (GetAceQualifier (accessType), sid, accessMask,
142                                 inheritanceFlags, propagationFlags, AuditFlags.None,
143                                 objectFlags, objectType, inheritedObjectType);
144                 }
145                 
146                 internal override void ApplyCanonicalSortToExplicitAces ()
147                 {
148                         int explicitCount = GetCanonicalExplicitAceCount ();
149                         int explicitDenys = GetCanonicalExplicitDenyAceCount ();
150
151                         ApplyCanonicalSortToExplicitAces (0, explicitDenys);
152                         ApplyCanonicalSortToExplicitAces (explicitDenys, explicitCount - explicitDenys);
153                 }
154                 
155                 internal override int GetAceInsertPosition (AceQualifier aceQualifier)
156                 {
157                         // Canonical order for DACLs is explicit deny, explicit allow, inherited.
158                         if (AceQualifier.AccessAllowed == aceQualifier)
159                                 return GetCanonicalExplicitDenyAceCount ();
160                         else
161                                 return 0;
162                 }
163                 
164                 static AceQualifier GetAceQualifier (AccessControlType accessType)
165                 {
166                         if (AccessControlType.Allow == accessType)
167                                 return AceQualifier.AccessAllowed;
168                         else if (AccessControlType.Deny == accessType)
169                                 return AceQualifier.AccessDenied;
170                         else
171                                 throw new ArgumentOutOfRangeException ("accessType");
172                 }
173                 
174                 internal override bool IsAceMeaningless (GenericAce ace)
175                 {
176                         if (base.IsAceMeaningless (ace)) return true;
177                         if (AuditFlags.None != ace.AuditFlags) return true;
178                         
179                         QualifiedAce qace = ace as QualifiedAce;
180                         if (null != qace) {
181                                 if (!(AceQualifier.AccessAllowed == qace.AceQualifier ||
182                                       AceQualifier.AccessDenied  == qace.AceQualifier)) return true;
183                         }
184
185                         return false;
186                 }
187         }
188 }
189