Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Win32.cs
Go to the documentation of this file.
3
5
6internal static class Win32
7{
8 internal static int ConvertSdToSddl(byte[] binaryForm, int requestedRevision, SecurityInfos si, out string resultSddl)
9 {
10 uint resultStringLength = 0u;
11 if (!global::Interop.Advapi32.ConvertSdToStringSd(binaryForm, (uint)requestedRevision, (uint)si, out var resultString, ref resultStringLength))
12 {
13 int lastWin32Error = Marshal.GetLastWin32Error();
14 resultSddl = null;
15 if (lastWin32Error == 8)
16 {
17 throw new OutOfMemoryException();
18 }
19 return lastWin32Error;
20 }
21 resultSddl = Marshal.PtrToStringUni(resultString);
22 Marshal.FreeHGlobal(resultString);
23 return 0;
24 }
25
26 internal static int GetSecurityInfo(ResourceType resourceType, string name, SafeHandle handle, AccessControlSections accessControlSections, out RawSecurityDescriptor resultSd)
27 {
28 resultSd = null;
29 SecurityInfos securityInfos = (SecurityInfos)0;
30 Privilege privilege = null;
31 if ((accessControlSections & AccessControlSections.Owner) != 0)
32 {
33 securityInfos |= SecurityInfos.Owner;
34 }
35 if ((accessControlSections & AccessControlSections.Group) != 0)
36 {
37 securityInfos |= SecurityInfos.Group;
38 }
39 if ((accessControlSections & AccessControlSections.Access) != 0)
40 {
41 securityInfos |= SecurityInfos.DiscretionaryAcl;
42 }
43 if ((accessControlSections & AccessControlSections.Audit) != 0)
44 {
45 securityInfos |= SecurityInfos.SystemAcl;
46 privilege = new Privilege("SeSecurityPrivilege");
47 }
48 int num;
49 IntPtr securityDescriptor;
50 try
51 {
52 if (privilege != null)
53 {
54 try
55 {
56 privilege.Enable();
57 }
59 {
60 }
61 }
62 IntPtr sidOwner;
63 IntPtr sidGroup;
64 IntPtr dacl;
65 IntPtr sacl;
66 if (name != null)
67 {
68 num = (int)global::Interop.Advapi32.GetSecurityInfoByName(name, (uint)resourceType, (uint)securityInfos, out sidOwner, out sidGroup, out dacl, out sacl, out securityDescriptor);
69 }
70 else
71 {
72 if (handle == null)
73 {
74 throw new ArgumentException();
75 }
76 if (handle.IsInvalid)
77 {
79 }
80 num = (int)global::Interop.Advapi32.GetSecurityInfoByHandle(handle, (uint)resourceType, (uint)securityInfos, out sidOwner, out sidGroup, out dacl, out sacl, out securityDescriptor);
81 }
82 if (num == 0 && IntPtr.Zero.Equals(securityDescriptor))
83 {
85 }
86 switch (num)
87 {
88 case 1300:
89 case 1314:
90 throw new PrivilegeNotHeldException("SeSecurityPrivilege");
91 case 5:
92 case 1347:
94 case 0:
95 break;
96 default:
97 goto IL_013f;
98 }
99 }
100 catch
101 {
102 privilege?.Revert();
103 throw;
104 }
105 finally
106 {
107 privilege?.Revert();
108 }
109 uint securityDescriptorLength = global::Interop.Advapi32.GetSecurityDescriptorLength(securityDescriptor);
110 byte[] array = new byte[securityDescriptorLength];
111 Marshal.Copy(securityDescriptor, array, 0, (int)securityDescriptorLength);
112 Marshal.FreeHGlobal(securityDescriptor);
113 resultSd = new RawSecurityDescriptor(array, 0);
114 return 0;
115 IL_013f:
116 if (num == 8)
117 {
118 throw new OutOfMemoryException();
119 }
120 return num;
121 }
122
123 internal static int SetSecurityInfo(ResourceType type, string name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl)
124 {
125 byte[] array = null;
126 byte[] array2 = null;
127 byte[] array3 = null;
128 byte[] array4 = null;
129 Privilege privilege = null;
130 if (owner != null)
131 {
132 int binaryLength = owner.BinaryLength;
133 array = new byte[binaryLength];
134 owner.GetBinaryForm(array, 0);
135 }
136 if (group != null)
137 {
138 int binaryLength = group.BinaryLength;
139 array2 = new byte[binaryLength];
140 group.GetBinaryForm(array2, 0);
141 }
142 if (dacl != null)
143 {
144 int binaryLength = dacl.BinaryLength;
145 array4 = new byte[binaryLength];
146 dacl.GetBinaryForm(array4, 0);
147 }
148 if (sacl != null)
149 {
150 int binaryLength = sacl.BinaryLength;
151 array3 = new byte[binaryLength];
152 sacl.GetBinaryForm(array3, 0);
153 }
154 if ((securityInformation & SecurityInfos.SystemAcl) != 0)
155 {
156 privilege = new Privilege("SeSecurityPrivilege");
157 }
158 int num;
159 try
160 {
161 if (privilege != null)
162 {
163 try
164 {
165 privilege.Enable();
166 }
168 {
169 }
170 }
171 if (name != null)
172 {
173 num = (int)global::Interop.Advapi32.SetSecurityInfoByName(name, (uint)type, (uint)securityInformation, array, array2, array4, array3);
174 }
175 else
176 {
177 if (handle == null)
178 {
179 throw new ArgumentException();
180 }
181 if (handle.IsInvalid)
182 {
184 }
185 num = (int)global::Interop.Advapi32.SetSecurityInfoByHandle(handle, (uint)type, (uint)securityInformation, array, array2, array4, array3);
186 }
187 switch (num)
188 {
189 case 1300:
190 case 1314:
191 throw new PrivilegeNotHeldException("SeSecurityPrivilege");
192 case 5:
193 case 1347:
194 throw new UnauthorizedAccessException();
195 case 0:
196 break;
197 default:
198 goto IL_0145;
199 }
200 }
201 catch
202 {
203 privilege?.Revert();
204 throw;
205 }
206 finally
207 {
208 privilege?.Revert();
209 }
210 return 0;
211 IL_0145:
212 if (num == 8)
213 {
214 throw new OutOfMemoryException();
215 }
216 return num;
217 }
218}
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static unsafe? string PtrToStringUni(IntPtr ptr)
Definition Marshal.cs:652
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Definition Marshal.cs:800
static string Argument_InvalidSafeHandle
Definition SR.cs:62
static string InvalidOperation_NoSecurityDescriptor
Definition SR.cs:92
Definition SR.cs:7
void GetBinaryForm(byte[] binaryForm, int offset)
static int GetSecurityInfo(ResourceType resourceType, string name, SafeHandle handle, AccessControlSections accessControlSections, out RawSecurityDescriptor resultSd)
Definition Win32.cs:26
static int SetSecurityInfo(ResourceType type, string name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl)
Definition Win32.cs:123
static int ConvertSdToSddl(byte[] binaryForm, int requestedRevision, SecurityInfos si, out string resultSddl)
Definition Win32.cs:8
void GetBinaryForm(byte[] binaryForm, int offset)
static readonly IntPtr Zero
Definition IntPtr.cs:18