Merge pull request #375 from atomia/master
[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                 public bool RemoveAccess (AccessControlType accessType,
76                                           SecurityIdentifier sid,
77                                           int accessMask,
78                                           InheritanceFlags inheritanceFlags,
79                                           PropagationFlags propagationFlags)
80                 {
81                         throw new NotImplementedException ();
82                 }
83                 
84                 public bool RemoveAccess (AccessControlType accessType,
85                                           SecurityIdentifier sid,
86                                           int accessMask,
87                                           InheritanceFlags inheritanceFlags,
88                                           PropagationFlags propagationFlags,
89                                           ObjectAceFlags objectFlags,
90                                           Guid objectType,
91                                           Guid inheritedObjectType)
92                 {
93                         throw new NotImplementedException ();
94                 }
95                 
96                 public void RemoveAccessSpecific (AccessControlType accessType,
97                                                   SecurityIdentifier sid,
98                                                   int accessMask,
99                                                   InheritanceFlags inheritanceFlags,
100                                                   PropagationFlags propagationFlags)
101                 {
102                         RemoveAceSpecific (GetAceQualifier (accessType), sid, accessMask,
103                                            inheritanceFlags, propagationFlags, AuditFlags.None);
104                 }
105                 
106                 public void RemoveAccessSpecific (AccessControlType accessType,
107                                                   SecurityIdentifier sid,
108                                                   int accessMask,
109                                                   InheritanceFlags inheritanceFlags,
110                                                   PropagationFlags propagationFlags,
111                                                   ObjectAceFlags objectFlags,
112                                                   Guid objectType,
113                                                   Guid inheritedObjectType)
114                 {
115                         RemoveAceSpecific (GetAceQualifier (accessType), sid, accessMask,
116                                            inheritanceFlags, propagationFlags, AuditFlags.None,
117                                            objectFlags, objectType, inheritedObjectType);
118                 }
119                 
120                 public void SetAccess (AccessControlType accessType,
121                                        SecurityIdentifier sid,
122                                        int accessMask,
123                                        InheritanceFlags inheritanceFlags,
124                                        PropagationFlags propagationFlags)
125                 {
126                         SetAce (GetAceQualifier (accessType), sid, accessMask,
127                                 inheritanceFlags, propagationFlags, AuditFlags.None);
128                 }
129                 
130                 public void SetAccess (AccessControlType accessType,
131                                        SecurityIdentifier sid,
132                                        int accessMask,
133                                        InheritanceFlags inheritanceFlags,
134                                        PropagationFlags propagationFlags,
135                                        ObjectAceFlags objectFlags,
136                                        Guid objectType,
137                                        Guid inheritedObjectType)
138                 {
139                         SetAce (GetAceQualifier (accessType), sid, accessMask,
140                                 inheritanceFlags, propagationFlags, AuditFlags.None,
141                                 objectFlags, objectType, inheritedObjectType);
142                 }
143                 
144                 internal override void ApplyCanonicalSortToExplicitAces ()
145                 {
146                         int explicitCount = GetCanonicalExplicitAceCount ();
147                         int explicitDenys = GetCanonicalExplicitDenyAceCount ();
148
149                         ApplyCanonicalSortToExplicitAces (0, explicitDenys);
150                         ApplyCanonicalSortToExplicitAces (explicitDenys, explicitCount - explicitDenys);
151                 }
152                 
153                 internal override int GetAceInsertPosition (AceQualifier aceQualifier)
154                 {
155                         // Canonical order for DACLs is explicit deny, explicit allow, inherited.
156                         if (AceQualifier.AccessAllowed == aceQualifier)
157                                 return GetCanonicalExplicitDenyAceCount ();
158                         else
159                                 return 0;
160                 }
161                 
162                 static AceQualifier GetAceQualifier (AccessControlType accessType)
163                 {
164                         if (AccessControlType.Allow == accessType)
165                                 return AceQualifier.AccessAllowed;
166                         else if (AccessControlType.Deny == accessType)
167                                 return AceQualifier.AccessDenied;
168                         else
169                                 throw new ArgumentOutOfRangeException ("accessType");
170                 }
171                 
172                 internal override bool IsAceMeaningless (GenericAce ace)
173                 {
174                         if (base.IsAceMeaningless (ace)) return true;
175                         if (AuditFlags.None != ace.AuditFlags) return true;
176                         
177                         QualifiedAce qace = ace as QualifiedAce;
178                         if (null != qace) {
179                                 if (!(AceQualifier.AccessAllowed == qace.AceQualifier ||
180                                       AceQualifier.AccessDenied  == qace.AceQualifier)) return true;
181                         }
182
183                         return false;
184                 }
185         }
186 }
187