Repository navigation
Expand file tree
/
Copy pathAccessControlList.cs
More file actions
188 lines (154 loc) · 8.42 KB
/
Copy pathAccessControlList.cs
File metadata and controls
188 lines (154 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Security.Permissions;
using System.Security;
using System.Collections;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Text;
using System.Messaging.Interop;
namespace System.Messaging {
/// <include file='doc\AccessControlList.uex' path='docs/doc[@for="AccessControlList"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1039:ListsAreStronglyTyped")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
public class AccessControlList : CollectionBase {
internal static readonly int UnknownEnvironment = 0;
internal static readonly int W2kEnvironment = 1;
internal static readonly int NtEnvironment = 2;
internal static readonly int NonNtEnvironment = 3;
// Double-checked locking pattern requires volatile for read/write synchronization
private static volatile int environment = UnknownEnvironment;
private static object staticLock = new object();
/// <include file='doc\AccessControlList.uex' path='docs/doc[@for="AccessControlList.AccessControlList"]/*' />
public AccessControlList() {
}
internal static int CurrentEnvironment {
get {
if (environment == UnknownEnvironment) {
lock (AccessControlList.staticLock) {
if (environment == UnknownEnvironment) {
//SECREVIEW: jruiz- need to assert Environment permissions here
// the environment check is not exposed as a public
// method
EnvironmentPermission environmentPermission = new EnvironmentPermission(PermissionState.Unrestricted);
environmentPermission.Assert();
try {
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
if (Environment.OSVersion.Version.Major >= 5)
environment = W2kEnvironment;
else
environment = NtEnvironment;
}
else
environment = NonNtEnvironment;
}
finally {
EnvironmentPermission.RevertAssert();
}
}
}
}
return environment;
}
}
/// <include file='doc\AccessControlList.uex' path='docs/doc[@for="AccessControlList.Add"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public int Add(AccessControlEntry entry) {
return List.Add(entry);
}
/// <include file='doc\AccessControlList.uex' path='docs/doc[@for="AccessControlList.Insert"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void Insert(int index, AccessControlEntry entry) {
List.Insert(index, entry);
}
/// <include file='doc\AccessControlList.uex' path='docs/doc[@for="AccessControlList.IndexOf"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public int IndexOf(AccessControlEntry entry) {
return List.IndexOf(entry);
}
internal static void CheckEnvironment() {
if (CurrentEnvironment == NonNtEnvironment)
throw new PlatformNotSupportedException(Res.GetString(Res.WinNTRequired));
}
/// <include file='doc\AccessControlList.uex' path='docs/doc[@for="AccessControlList.Contains"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public bool Contains(AccessControlEntry entry) {
return List.Contains(entry);
}
/// <include file='doc\AccessControlList.uex' path='docs/doc[@for="AccessControlList.Remove"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void Remove(AccessControlEntry entry) {
List.Remove(entry);
}
/// <include file='doc\AccessControlList.uex' path='docs/doc[@for="AccessControlList.CopyTo"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void CopyTo(AccessControlEntry[] array, int index) {
List.CopyTo(array, index);
}
internal IntPtr MakeAcl(IntPtr oldAcl) {
CheckEnvironment();
int ACECount = List.Count;
IntPtr newAcl;
NativeMethods.ExplicitAccess[] entries = new NativeMethods.ExplicitAccess[ACECount];
GCHandle mem = GCHandle.Alloc(entries, GCHandleType.Pinned);
try {
for (int i = 0; i < ACECount; i++) {
int sidSize = 0;
int sidtype;
int domainSize = 0;
AccessControlEntry ace = (AccessControlEntry)List[i];
if (ace.Trustee == null)
throw new InvalidOperationException(Res.GetString(Res.InvalidTrustee));
string name = ace.Trustee.Name;
if (name == null)
throw new InvalidOperationException(Res.GetString(Res.InvalidTrusteeName));
if ((ace.Trustee.TrusteeType == TrusteeType.Computer) && !name.EndsWith("$"))
name += "$";
if (!UnsafeNativeMethods.LookupAccountName(ace.Trustee.SystemName, name, (IntPtr)0, ref sidSize, null, ref domainSize, out sidtype)) {
int errval = Marshal.GetLastWin32Error();
if (errval != 122)
throw new InvalidOperationException(Res.GetString(Res.CouldntResolve, ace.Trustee.Name, errval));
}
entries[i].data = (IntPtr)Marshal.AllocHGlobal(sidSize);
StringBuilder domainName = new StringBuilder(domainSize);
if (!UnsafeNativeMethods.LookupAccountName(ace.Trustee.SystemName, name, entries[i].data, ref sidSize, domainName, ref domainSize, out sidtype))
throw new InvalidOperationException(Res.GetString(Res.CouldntResolveName, ace.Trustee.Name));
entries[i].grfAccessPermissions = ace.accessFlags;
entries[i].grfAccessMode = (int)ace.EntryType;
entries[i].grfInheritance = 0;
entries[i].pMultipleTrustees = (IntPtr)0;
entries[i].MultipleTrusteeOperation = NativeMethods.NO_MULTIPLE_TRUSTEE;
entries[i].TrusteeForm = NativeMethods.TRUSTEE_IS_SID;
entries[i].TrusteeType = (int)ace.Trustee.TrusteeType;
}
int err = SafeNativeMethods.SetEntriesInAclW(ACECount, (IntPtr)mem.AddrOfPinnedObject(), oldAcl, out newAcl);
if (err != NativeMethods.ERROR_SUCCESS)
throw new Win32Exception(err);
}
finally {
mem.Free();
for (int i = 0; i < ACECount; i++)
if (entries[i].data != (IntPtr)0)
Marshal.FreeHGlobal(entries[i].data);
}
return newAcl;
}
internal static void FreeAcl(IntPtr acl) {
SafeNativeMethods.LocalFree(acl);
}
}
}