4485746789fea9b5c657d7c82ae84234a73962b2
[reactos.git] / rostests / winetests / advapi32 / security.c
1 /*
2 * Unit tests for security functions
3 *
4 * Copyright (c) 2004 Mike McCormack
5 * Copyright (c) 2011 Dmitry Timoshkov
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <stdarg.h>
23 #include <stdio.h>
24
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #define WIN32_LEAN_AND_MEAN
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "aclapi.h"
32 #include "winnt.h"
33 #include "sddl.h"
34 #include "ntsecapi.h"
35 #include "lmcons.h"
36
37 #include <winsvc.h>
38
39 #include "wine/test.h"
40
41 #ifndef PROCESS_QUERY_LIMITED_INFORMATION
42 #define PROCESS_QUERY_LIMITED_INFORMATION 0x1000
43 #endif
44
45 /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
46 #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
47 #define PROCESS_ALL_ACCESS_VISTA (PROCESS_ALL_ACCESS | 0xf000)
48
49 #ifndef EVENT_QUERY_STATE
50 #define EVENT_QUERY_STATE 0x0001
51 #endif
52
53 #ifndef SEMAPHORE_QUERY_STATE
54 #define SEMAPHORE_QUERY_STATE 0x0001
55 #endif
56
57 #ifndef THREAD_SET_LIMITED_INFORMATION
58 #define THREAD_SET_LIMITED_INFORMATION 0x0400
59 #define THREAD_QUERY_LIMITED_INFORMATION 0x0800
60 #endif
61
62 #define THREAD_ALL_ACCESS_NT4 (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3ff)
63 #define THREAD_ALL_ACCESS_VISTA (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xffff)
64
65 /* copied from Wine winternl.h - not included in the Windows SDK */
66 typedef enum _OBJECT_INFORMATION_CLASS {
67 ObjectBasicInformation,
68 ObjectNameInformation,
69 ObjectTypeInformation,
70 ObjectAllInformation,
71 ObjectDataInformation
72 } OBJECT_INFORMATION_CLASS, *POBJECT_INFORMATION_CLASS;
73
74 typedef struct _OBJECT_BASIC_INFORMATION {
75 ULONG Attributes;
76 ACCESS_MASK GrantedAccess;
77 ULONG HandleCount;
78 ULONG PointerCount;
79 ULONG PagedPoolUsage;
80 ULONG NonPagedPoolUsage;
81 ULONG Reserved[3];
82 ULONG NameInformationLength;
83 ULONG TypeInformationLength;
84 ULONG SecurityDescriptorLength;
85 LARGE_INTEGER CreateTime;
86 } OBJECT_BASIC_INFORMATION, *POBJECT_BASIC_INFORMATION;
87
88 #define expect_eq(expr, value, type, format) { type ret_ = expr; ok((value) == ret_, #expr " expected " format " got " format "\n", (value), (ret_)); }
89
90 static BOOL (WINAPI *pAddAccessAllowedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
91 static BOOL (WINAPI *pAddAccessDeniedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
92 static BOOL (WINAPI *pAddAuditAccessAceEx)(PACL, DWORD, DWORD, DWORD, PSID, BOOL, BOOL);
93 static VOID (WINAPI *pBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
94 static VOID (WINAPI *pBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
95 static VOID (WINAPI *pBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
96 POBJECTS_AND_NAME_A pObjName,
97 SE_OBJECT_TYPE ObjectType,
98 LPSTR ObjectTypeName,
99 LPSTR InheritedObjectTypeName,
100 LPSTR Name );
101 static VOID (WINAPI *pBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
102 POBJECTS_AND_SID pObjSid,
103 GUID* pObjectGuid,
104 GUID* pInheritedObjectGuid,
105 PSID pSid );
106 static LPSTR (WINAPI *pGetTrusteeNameA)( PTRUSTEEA pTrustee );
107 static BOOL (WINAPI *pMakeSelfRelativeSD)( PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD );
108 static BOOL (WINAPI *pConvertSidToStringSidA)( PSID pSid, LPSTR *str );
109 static BOOL (WINAPI *pConvertStringSidToSidA)( LPCSTR str, PSID pSid );
110 static BOOL (WINAPI *pCheckTokenMembership)(HANDLE, PSID, PBOOL);
111 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR, DWORD,
112 PSECURITY_DESCRIPTOR*, PULONG );
113 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorW)(LPCWSTR, DWORD,
114 PSECURITY_DESCRIPTOR*, PULONG );
115 static BOOL (WINAPI *pConvertSecurityDescriptorToStringSecurityDescriptorA)(PSECURITY_DESCRIPTOR, DWORD,
116 SECURITY_INFORMATION, LPSTR *, PULONG );
117 static BOOL (WINAPI *pGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
118 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
119 static BOOL (WINAPI *pSetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
120 PSECURITY_DESCRIPTOR);
121 static DWORD (WINAPI *pGetNamedSecurityInfoA)(LPSTR, SE_OBJECT_TYPE, SECURITY_INFORMATION,
122 PSID*, PSID*, PACL*, PACL*,
123 PSECURITY_DESCRIPTOR*);
124 static DWORD (WINAPI *pSetNamedSecurityInfoA)(LPSTR, SE_OBJECT_TYPE, SECURITY_INFORMATION,
125 PSID, PSID, PACL, PACL);
126 static PDWORD (WINAPI *pGetSidSubAuthority)(PSID, DWORD);
127 static PUCHAR (WINAPI *pGetSidSubAuthorityCount)(PSID);
128 static BOOL (WINAPI *pIsValidSid)(PSID);
129 static DWORD (WINAPI *pRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
130 static BOOL (WINAPI *pCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
131 static BOOL (WINAPI *pDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
132 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
133
134 static NTSTATUS (WINAPI *pLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
135 static NTSTATUS (WINAPI *pLsaClose)(LSA_HANDLE);
136 static NTSTATUS (WINAPI *pLsaFreeMemory)(PVOID);
137 static NTSTATUS (WINAPI *pLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
138 static NTSTATUS (WINAPI *pNtQueryObject)(HANDLE,OBJECT_INFORMATION_CLASS,PVOID,ULONG,PULONG);
139 static DWORD (WINAPI *pSetEntriesInAclW)(ULONG, PEXPLICIT_ACCESSW, PACL, PACL*);
140 static DWORD (WINAPI *pSetEntriesInAclA)(ULONG, PEXPLICIT_ACCESSA, PACL, PACL*);
141 static BOOL (WINAPI *pSetSecurityDescriptorControl)(PSECURITY_DESCRIPTOR, SECURITY_DESCRIPTOR_CONTROL,
142 SECURITY_DESCRIPTOR_CONTROL);
143 static DWORD (WINAPI *pGetSecurityInfo)(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
144 PSID*, PSID*, PACL*, PACL*, PSECURITY_DESCRIPTOR*);
145 static DWORD (WINAPI *pSetSecurityInfo)(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
146 PSID, PSID, PACL, PACL);
147 static NTSTATUS (WINAPI *pNtAccessCheck)(PSECURITY_DESCRIPTOR, HANDLE, ACCESS_MASK, PGENERIC_MAPPING,
148 PPRIVILEGE_SET, PULONG, PULONG, NTSTATUS*);
149 static BOOL (WINAPI *pCreateRestrictedToken)(HANDLE, DWORD, DWORD, PSID_AND_ATTRIBUTES, DWORD,
150 PLUID_AND_ATTRIBUTES, DWORD, PSID_AND_ATTRIBUTES, PHANDLE);
151 static BOOL (WINAPI *pGetAclInformation)(PACL,LPVOID,DWORD,ACL_INFORMATION_CLASS);
152 static BOOL (WINAPI *pGetAce)(PACL,DWORD,LPVOID*);
153
154 static HMODULE hmod;
155 static int myARGC;
156 static char** myARGV;
157
158 struct strsid_entry
159 {
160 const char *str;
161 DWORD flags;
162 };
163 #define STRSID_OK 0
164 #define STRSID_OPT 1
165
166 struct sidRef
167 {
168 SID_IDENTIFIER_AUTHORITY auth;
169 const char *refStr;
170 };
171
172 static void init(void)
173 {
174 HMODULE hntdll;
175
176 hntdll = GetModuleHandleA("ntdll.dll");
177 pNtQueryObject = (void *)GetProcAddress( hntdll, "NtQueryObject" );
178 pNtAccessCheck = (void *)GetProcAddress( hntdll, "NtAccessCheck" );
179
180 hmod = GetModuleHandleA("advapi32.dll");
181 pAddAccessAllowedAceEx = (void *)GetProcAddress(hmod, "AddAccessAllowedAceEx");
182 pAddAccessDeniedAceEx = (void *)GetProcAddress(hmod, "AddAccessDeniedAceEx");
183 pAddAuditAccessAceEx = (void *)GetProcAddress(hmod, "AddAuditAccessAceEx");
184 pCheckTokenMembership = (void *)GetProcAddress(hmod, "CheckTokenMembership");
185 pConvertStringSecurityDescriptorToSecurityDescriptorA =
186 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
187 pConvertStringSecurityDescriptorToSecurityDescriptorW =
188 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
189 pConvertSecurityDescriptorToStringSecurityDescriptorA =
190 (void *)GetProcAddress(hmod, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
191 pGetFileSecurityA = (void *)GetProcAddress(hmod, "GetFileSecurityA" );
192 pSetFileSecurityA = (void *)GetProcAddress(hmod, "SetFileSecurityA" );
193 pCreateWellKnownSid = (void *)GetProcAddress( hmod, "CreateWellKnownSid" );
194 pGetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "GetNamedSecurityInfoA");
195 pSetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "SetNamedSecurityInfoA");
196 pGetSidSubAuthority = (void *)GetProcAddress(hmod, "GetSidSubAuthority");
197 pGetSidSubAuthorityCount = (void *)GetProcAddress(hmod, "GetSidSubAuthorityCount");
198 pIsValidSid = (void *)GetProcAddress(hmod, "IsValidSid");
199 pMakeSelfRelativeSD = (void *)GetProcAddress(hmod, "MakeSelfRelativeSD");
200 pSetEntriesInAclW = (void *)GetProcAddress(hmod, "SetEntriesInAclW");
201 pSetEntriesInAclA = (void *)GetProcAddress(hmod, "SetEntriesInAclA");
202 pSetSecurityDescriptorControl = (void *)GetProcAddress(hmod, "SetSecurityDescriptorControl");
203 pGetSecurityInfo = (void *)GetProcAddress(hmod, "GetSecurityInfo");
204 pSetSecurityInfo = (void *)GetProcAddress(hmod, "SetSecurityInfo");
205 pCreateRestrictedToken = (void *)GetProcAddress(hmod, "CreateRestrictedToken");
206 pConvertSidToStringSidA = (void *)GetProcAddress(hmod, "ConvertSidToStringSidA");
207 pConvertStringSidToSidA = (void *)GetProcAddress(hmod, "ConvertStringSidToSidA");
208 pGetAclInformation = (void *)GetProcAddress(hmod, "GetAclInformation");
209 pGetAce = (void *)GetProcAddress(hmod, "GetAce");
210
211 myARGC = winetest_get_mainargs( &myARGV );
212 }
213
214 static void test_sid(void)
215 {
216 struct sidRef refs[] = {
217 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
218 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
219 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
220 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
221 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
222 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
223 };
224 struct strsid_entry strsid_table[] = {
225 {"AO", STRSID_OK}, {"RU", STRSID_OK}, {"AN", STRSID_OK}, {"AU", STRSID_OK},
226 {"BA", STRSID_OK}, {"BG", STRSID_OK}, {"BO", STRSID_OK}, {"BU", STRSID_OK},
227 {"CA", STRSID_OPT}, {"CG", STRSID_OK}, {"CO", STRSID_OK}, {"DA", STRSID_OPT},
228 {"DC", STRSID_OPT}, {"DD", STRSID_OPT}, {"DG", STRSID_OPT}, {"DU", STRSID_OPT},
229 {"EA", STRSID_OPT}, {"ED", STRSID_OK}, {"WD", STRSID_OK}, {"PA", STRSID_OPT},
230 {"IU", STRSID_OK}, {"LA", STRSID_OK}, {"LG", STRSID_OK}, {"LS", STRSID_OK},
231 {"SY", STRSID_OK}, {"NU", STRSID_OK}, {"NO", STRSID_OK}, {"NS", STRSID_OK},
232 {"PO", STRSID_OK}, {"PS", STRSID_OK}, {"PU", STRSID_OK}, {"RS", STRSID_OPT},
233 {"RD", STRSID_OK}, {"RE", STRSID_OK}, {"RC", STRSID_OK}, {"SA", STRSID_OPT},
234 {"SO", STRSID_OK}, {"SU", STRSID_OK}};
235
236 const char noSubAuthStr[] = "S-1-5";
237 unsigned int i;
238 PSID psid = NULL;
239 SID *pisid;
240 BOOL r;
241 LPSTR str = NULL;
242
243 if( !pConvertSidToStringSidA || !pConvertStringSidToSidA )
244 {
245 win_skip("ConvertSidToStringSidA or ConvertStringSidToSidA not available\n");
246 return;
247 }
248
249 r = pConvertStringSidToSidA( NULL, NULL );
250 ok( !r, "expected failure with NULL parameters\n" );
251 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
252 return;
253 ok( GetLastError() == ERROR_INVALID_PARAMETER,
254 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
255 GetLastError() );
256
257 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
258 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
259 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
260 GetLastError() );
261
262 r = pConvertStringSidToSidA( NULL, &str );
263 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
264 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
265 GetLastError() );
266
267 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
268 ok( !r,
269 "expected failure with no sub authorities\n" );
270 ok( GetLastError() == ERROR_INVALID_SID,
271 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
272 GetLastError() );
273
274 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid), "ConvertStringSidToSidA failed\n");
275 pisid = psid;
276 ok(pisid->SubAuthorityCount == 4, "Invalid sub authority count - expected 4, got %d\n", pisid->SubAuthorityCount);
277 ok(pisid->SubAuthority[0] == 21, "Invalid subauthority 0 - expected 21, got %d\n", pisid->SubAuthority[0]);
278 ok(pisid->SubAuthority[3] == 4576, "Invalid subauthority 0 - expected 4576, got %d\n", pisid->SubAuthority[3]);
279 LocalFree(str);
280 LocalFree(psid);
281
282 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
283 {
284 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
285 &psid );
286 ok( r, "failed to allocate sid\n" );
287 r = pConvertSidToStringSidA( psid, &str );
288 ok( r, "failed to convert sid\n" );
289 if (r)
290 {
291 ok( !strcmp( str, refs[i].refStr ),
292 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
293 LocalFree( str );
294 }
295 if( psid )
296 FreeSid( psid );
297
298 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
299 ok( r, "failed to parse sid string\n" );
300 pisid = psid;
301 ok( pisid &&
302 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
303 sizeof(refs[i].auth) ),
304 "string sid %s didn't parse to expected value\n"
305 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
306 refs[i].refStr,
307 MAKEWORD( pisid->IdentifierAuthority.Value[1],
308 pisid->IdentifierAuthority.Value[0] ),
309 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
310 pisid->IdentifierAuthority.Value[4] ),
311 MAKEWORD( pisid->IdentifierAuthority.Value[3],
312 pisid->IdentifierAuthority.Value[2] ) ),
313 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
314 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
315 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
316 if( psid )
317 LocalFree( psid );
318 }
319
320 /* string constant format not supported before XP */
321 r = pConvertStringSidToSidA(strsid_table[0].str, &psid);
322 if(!r)
323 {
324 win_skip("String constant format not supported\n");
325 return;
326 }
327 LocalFree(psid);
328
329 for(i = 0; i < sizeof(strsid_table) / sizeof(strsid_table[0]); i++)
330 {
331 char *temp;
332
333 SetLastError(0xdeadbeef);
334 r = pConvertStringSidToSidA(strsid_table[i].str, &psid);
335
336 if (!(strsid_table[i].flags & STRSID_OPT))
337 {
338 ok(r, "%s: got %u\n", strsid_table[i].str, GetLastError());
339 }
340
341 if (r)
342 {
343 if ((winetest_debug > 1) && (pConvertSidToStringSidA(psid, &temp)))
344 {
345 trace(" %s: %s\n", strsid_table[i].str, temp);
346 LocalFree(temp);
347 }
348 LocalFree(psid);
349 }
350 else
351 {
352 if (GetLastError() != ERROR_INVALID_SID)
353 trace(" %s: couldn't be converted, returned %d\n", strsid_table[i].str, GetLastError());
354 else
355 trace(" %s: couldn't be converted\n", strsid_table[i].str);
356 }
357 }
358 }
359
360 static void test_trustee(void)
361 {
362 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
363 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
364 GUID ZeroGuid;
365 OBJECTS_AND_NAME_A oan;
366 OBJECTS_AND_SID oas;
367 TRUSTEEA trustee;
368 PSID psid;
369 char szObjectTypeName[] = "ObjectTypeName";
370 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
371 char szTrusteeName[] = "szTrusteeName";
372 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
373
374 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
375
376 pBuildTrusteeWithSidA = (void *)GetProcAddress( hmod, "BuildTrusteeWithSidA" );
377 pBuildTrusteeWithNameA = (void *)GetProcAddress( hmod, "BuildTrusteeWithNameA" );
378 pBuildTrusteeWithObjectsAndNameA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
379 pBuildTrusteeWithObjectsAndSidA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
380 pGetTrusteeNameA = (void *)GetProcAddress (hmod, "GetTrusteeNameA" );
381 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
382 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
383 !pGetTrusteeNameA )
384 return;
385
386 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
387 {
388 trace( "failed to init SID\n" );
389 return;
390 }
391
392 /* test BuildTrusteeWithSidA */
393 memset( &trustee, 0xff, sizeof trustee );
394 pBuildTrusteeWithSidA( &trustee, psid );
395
396 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
397 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
398 "MultipleTrusteeOperation wrong\n");
399 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
400 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
401 ok( trustee.ptstrName == psid, "ptstrName wrong\n" );
402
403 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
404 memset( &trustee, 0xff, sizeof trustee );
405 memset( &oas, 0xff, sizeof(oas) );
406 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
407 &InheritedObjectType, psid);
408
409 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
410 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
411 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
412 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
413 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
414
415 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
416 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
417 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
418 ok(oas.pSid == psid, "pSid wrong\n");
419
420 /* test GetTrusteeNameA */
421 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
422
423 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
424 memset( &trustee, 0xff, sizeof trustee );
425 memset( &oas, 0xff, sizeof(oas) );
426 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
427 &InheritedObjectType, psid);
428
429 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
430 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
431 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
432 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
433 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
434
435 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
436 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
437 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
438 ok(oas.pSid == psid, "pSid wrong\n");
439
440 FreeSid( psid );
441
442 /* test BuildTrusteeWithNameA */
443 memset( &trustee, 0xff, sizeof trustee );
444 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
445
446 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
447 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
448 "MultipleTrusteeOperation wrong\n");
449 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
450 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
451 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
452
453 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
454 memset( &trustee, 0xff, sizeof trustee );
455 memset( &oan, 0xff, sizeof(oan) );
456 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
457 szInheritedObjectTypeName, szTrusteeName);
458
459 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
460 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
461 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
462 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
463 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
464
465 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
466 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
467 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
468 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
469
470 /* test GetTrusteeNameA */
471 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
472
473 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
474 memset( &trustee, 0xff, sizeof trustee );
475 memset( &oan, 0xff, sizeof(oan) );
476 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
477 szInheritedObjectTypeName, szTrusteeName);
478
479 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
480 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
481 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
482 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
483 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
484
485 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
486 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
487 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
488 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
489
490 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
491 memset( &trustee, 0xff, sizeof trustee );
492 memset( &oan, 0xff, sizeof(oan) );
493 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
494 NULL, szTrusteeName);
495
496 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
497 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
498 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
499 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
500 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
501
502 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
503 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
504 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
505 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
506 }
507
508 /* If the first isn't defined, assume none is */
509 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
510 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
511 #define SE_CREATE_TOKEN_PRIVILEGE 2L
512 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
513 #define SE_LOCK_MEMORY_PRIVILEGE 4L
514 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
515 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
516 #define SE_TCB_PRIVILEGE 7L
517 #define SE_SECURITY_PRIVILEGE 8L
518 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
519 #define SE_LOAD_DRIVER_PRIVILEGE 10L
520 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
521 #define SE_SYSTEMTIME_PRIVILEGE 12L
522 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
523 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
524 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
525 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
526 #define SE_BACKUP_PRIVILEGE 17L
527 #define SE_RESTORE_PRIVILEGE 18L
528 #define SE_SHUTDOWN_PRIVILEGE 19L
529 #define SE_DEBUG_PRIVILEGE 20L
530 #define SE_AUDIT_PRIVILEGE 21L
531 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
532 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
533 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
534 #define SE_UNDOCK_PRIVILEGE 25L
535 #define SE_SYNC_AGENT_PRIVILEGE 26L
536 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
537 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
538 #define SE_IMPERSONATE_PRIVILEGE 29L
539 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
540 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
541 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
542
543 static void test_allocateLuid(void)
544 {
545 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
546 LUID luid1, luid2;
547 BOOL ret;
548
549 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
550 if (!pAllocateLocallyUniqueId) return;
551
552 ret = pAllocateLocallyUniqueId(&luid1);
553 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
554 return;
555
556 ok(ret,
557 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
558 ret = pAllocateLocallyUniqueId(&luid2);
559 ok( ret,
560 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
561 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
562 "AllocateLocallyUniqueId returned a well-known LUID\n");
563 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
564 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
565 ret = pAllocateLocallyUniqueId(NULL);
566 ok( !ret && GetLastError() == ERROR_NOACCESS,
567 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
568 GetLastError());
569 }
570
571 static void test_lookupPrivilegeName(void)
572 {
573 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
574 char buf[MAX_PATH]; /* arbitrary, seems long enough */
575 DWORD cchName = sizeof(buf);
576 LUID luid = { 0, 0 };
577 LONG i;
578 BOOL ret;
579
580 /* check whether it's available first */
581 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
582 if (!pLookupPrivilegeNameA) return;
583 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
584 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
585 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
586 return;
587
588 /* check with a short buffer */
589 cchName = 0;
590 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
591 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
592 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
593 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
594 GetLastError());
595 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
596 "LookupPrivilegeNameA returned an incorrect required length for\n"
597 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
598 lstrlenA("SeCreateTokenPrivilege") + 1);
599 /* check a known value and its returned length on success */
600 cchName = sizeof(buf);
601 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
602 cchName == strlen("SeCreateTokenPrivilege"),
603 "LookupPrivilegeNameA returned an incorrect output length for\n"
604 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
605 (int)strlen("SeCreateTokenPrivilege"));
606 /* check known values */
607 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i <= SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
608 {
609 luid.LowPart = i;
610 cchName = sizeof(buf);
611 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
612 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
613 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
614 }
615 /* check a bogus LUID */
616 luid.LowPart = 0xdeadbeef;
617 cchName = sizeof(buf);
618 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
619 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
620 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
621 GetLastError());
622 /* check on a bogus system */
623 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
624 cchName = sizeof(buf);
625 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
626 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
627 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
628 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
629 GetLastError());
630 }
631
632 struct NameToLUID
633 {
634 const char *name;
635 DWORD lowPart;
636 };
637
638 static void test_lookupPrivilegeValue(void)
639 {
640 static const struct NameToLUID privs[] = {
641 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
642 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
643 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
644 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
645 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
646 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
647 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
648 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
649 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
650 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
651 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
652 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
653 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
654 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
655 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
656 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
657 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
658 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
659 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
660 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
661 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
662 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE },
663 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
664 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
665 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
666 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
667 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
668 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
669 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
670 };
671 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
672 unsigned int i;
673 LUID luid;
674 BOOL ret;
675
676 /* check whether it's available first */
677 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
678 if (!pLookupPrivilegeValueA) return;
679 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
680 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
681 return;
682
683 /* check a bogus system name */
684 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
685 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
686 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
687 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
688 GetLastError());
689 /* check a NULL string */
690 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
691 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
692 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
693 GetLastError());
694 /* check a bogus privilege name */
695 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
696 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
697 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
698 GetLastError());
699 /* check case insensitive */
700 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
701 ok( ret,
702 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
703 GetLastError());
704 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
705 {
706 /* Not all privileges are implemented on all Windows versions, so
707 * don't worry if the call fails
708 */
709 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
710 {
711 ok(luid.LowPart == privs[i].lowPart,
712 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
713 privs[i].name);
714 }
715 }
716 }
717
718 static void test_luid(void)
719 {
720 test_allocateLuid();
721 test_lookupPrivilegeName();
722 test_lookupPrivilegeValue();
723 }
724
725 static void test_FileSecurity(void)
726 {
727 char wintmpdir [MAX_PATH];
728 char path [MAX_PATH];
729 char file [MAX_PATH];
730 HANDLE fh, token;
731 DWORD sdSize, retSize, rc, granted, priv_set_len;
732 PRIVILEGE_SET priv_set;
733 BOOL status;
734 BYTE *sd;
735 GENERIC_MAPPING mapping = { FILE_READ_DATA, FILE_WRITE_DATA, FILE_EXECUTE, FILE_ALL_ACCESS };
736 const SECURITY_INFORMATION request = OWNER_SECURITY_INFORMATION
737 | GROUP_SECURITY_INFORMATION
738 | DACL_SECURITY_INFORMATION;
739
740 if (!pGetFileSecurityA) {
741 win_skip ("GetFileSecurity is not available\n");
742 return;
743 }
744
745 if (!pSetFileSecurityA) {
746 win_skip ("SetFileSecurity is not available\n");
747 return;
748 }
749
750 if (!GetTempPathA (sizeof (wintmpdir), wintmpdir)) {
751 win_skip ("GetTempPathA failed\n");
752 return;
753 }
754
755 /* Create a temporary directory and in it a temporary file */
756 strcat (strcpy (path, wintmpdir), "rary");
757 SetLastError(0xdeadbeef);
758 rc = CreateDirectoryA (path, NULL);
759 ok (rc || GetLastError() == ERROR_ALREADY_EXISTS, "CreateDirectoryA "
760 "failed for '%s' with %d\n", path, GetLastError());
761
762 strcat (strcpy (file, path), "\\ess");
763 SetLastError(0xdeadbeef);
764 fh = CreateFileA (file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
765 ok (fh != INVALID_HANDLE_VALUE, "CreateFileA "
766 "failed for '%s' with %d\n", file, GetLastError());
767 CloseHandle (fh);
768
769 /* For the temporary file ... */
770
771 /* Get size needed */
772 retSize = 0;
773 SetLastError(0xdeadbeef);
774 rc = pGetFileSecurityA (file, request, NULL, 0, &retSize);
775 if (!rc && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
776 win_skip("GetFileSecurityA is not implemented\n");
777 goto cleanup;
778 }
779 ok (!rc, "GetFileSecurityA "
780 "was expected to fail for '%s'\n", file);
781 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
782 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
783 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
784
785 sdSize = retSize;
786 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
787
788 /* Get security descriptor for real */
789 retSize = -1;
790 SetLastError(0xdeadbeef);
791 rc = pGetFileSecurityA (file, request, sd, sdSize, &retSize);
792 ok (rc, "GetFileSecurityA "
793 "was not expected to fail '%s': %d\n", file, GetLastError());
794 ok (retSize == sdSize ||
795 broken(retSize == 0), /* NT4 */
796 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
797
798 /* Use it to set security descriptor */
799 SetLastError(0xdeadbeef);
800 rc = pSetFileSecurityA (file, request, sd);
801 ok (rc, "SetFileSecurityA "
802 "was not expected to fail '%s': %d\n", file, GetLastError());
803
804 HeapFree (GetProcessHeap (), 0, sd);
805
806 /* Repeat for the temporary directory ... */
807
808 /* Get size needed */
809 retSize = 0;
810 SetLastError(0xdeadbeef);
811 rc = pGetFileSecurityA (path, request, NULL, 0, &retSize);
812 ok (!rc, "GetFileSecurityA "
813 "was expected to fail for '%s'\n", path);
814 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
815 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
816 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
817
818 sdSize = retSize;
819 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
820
821 /* Get security descriptor for real */
822 retSize = -1;
823 SetLastError(0xdeadbeef);
824 rc = pGetFileSecurityA (path, request, sd, sdSize, &retSize);
825 ok (rc, "GetFileSecurityA "
826 "was not expected to fail '%s': %d\n", path, GetLastError());
827 ok (retSize == sdSize ||
828 broken(retSize == 0), /* NT4 */
829 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
830
831 /* Use it to set security descriptor */
832 SetLastError(0xdeadbeef);
833 rc = pSetFileSecurityA (path, request, sd);
834 ok (rc, "SetFileSecurityA "
835 "was not expected to fail '%s': %d\n", path, GetLastError());
836 HeapFree (GetProcessHeap (), 0, sd);
837
838 /* Old test */
839 strcpy (wintmpdir, "\\Should not exist");
840 SetLastError(0xdeadbeef);
841 rc = pGetFileSecurityA (wintmpdir, OWNER_SECURITY_INFORMATION, NULL, 0, &sdSize);
842 ok (!rc, "GetFileSecurityA should fail for not existing directories/files\n");
843 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
844 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
845
846 cleanup:
847 /* Remove temporary file and directory */
848 DeleteFileA(file);
849 RemoveDirectoryA(path);
850
851 /* Test file access permissions for a file with FILE_ATTRIBUTE_ARCHIVE */
852 SetLastError(0xdeadbeef);
853 rc = GetTempPathA(sizeof(wintmpdir), wintmpdir);
854 ok(rc, "GetTempPath error %d\n", GetLastError());
855
856 SetLastError(0xdeadbeef);
857 rc = GetTempFileNameA(wintmpdir, "tmp", 0, file);
858 ok(rc, "GetTempFileName error %d\n", GetLastError());
859
860 rc = GetFileAttributesA(file);
861 rc &= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED|FILE_ATTRIBUTE_COMPRESSED);
862 ok(rc == FILE_ATTRIBUTE_ARCHIVE, "expected FILE_ATTRIBUTE_ARCHIVE got %#x\n", rc);
863
864 retSize = 0xdeadbeef;
865 rc = GetFileSecurityA(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
866 NULL, 0, &sdSize);
867 ok(!rc, "GetFileSecurity should fail\n");
868 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
869 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
870 ok(sdSize > sizeof(SECURITY_DESCRIPTOR), "got sd size %d\n", sdSize);
871
872 sd = HeapAlloc(GetProcessHeap (), 0, sdSize);
873 retSize = 0xdeadbeef;
874 SetLastError(0xdeadbeef);
875 rc = GetFileSecurityA(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
876 sd, sdSize, &retSize);
877 ok(rc, "GetFileSecurity error %d\n", GetLastError());
878 ok(retSize == sdSize || broken(retSize == 0) /* NT4 */, "expected %d, got %d\n", sdSize, retSize);
879
880 SetLastError(0xdeadbeef);
881 rc = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &token);
882 ok(!rc, "OpenThreadToken should fail\n");
883 ok(GetLastError() == ERROR_NO_TOKEN, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
884
885 SetLastError(0xdeadbeef);
886 rc = ImpersonateSelf(SecurityIdentification);
887 ok(rc, "ImpersonateSelf error %d\n", GetLastError());
888
889 SetLastError(0xdeadbeef);
890 rc = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &token);
891 ok(rc, "OpenThreadToken error %d\n", GetLastError());
892
893 SetLastError(0xdeadbeef);
894 rc = RevertToSelf();
895 ok(rc, "RevertToSelf error %d\n", GetLastError());
896
897 priv_set_len = sizeof(priv_set);
898 granted = 0xdeadbeef;
899 status = 0xdeadbeef;
900 SetLastError(0xdeadbeef);
901 rc = AccessCheck(sd, token, FILE_READ_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
902 ok(rc, "AccessCheck error %d\n", GetLastError());
903 ok(status == 1, "expected 1, got %d\n", status);
904 ok(granted == FILE_READ_DATA, "expected FILE_READ_DATA, got %#x\n", granted);
905
906 granted = 0xdeadbeef;
907 status = 0xdeadbeef;
908 SetLastError(0xdeadbeef);
909 rc = AccessCheck(sd, token, FILE_WRITE_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
910 ok(rc, "AccessCheck error %d\n", GetLastError());
911 ok(status == 1, "expected 1, got %d\n", status);
912 ok(granted == FILE_WRITE_DATA, "expected FILE_WRITE_DATA, got %#x\n", granted);
913
914 granted = 0xdeadbeef;
915 status = 0xdeadbeef;
916 SetLastError(0xdeadbeef);
917 rc = AccessCheck(sd, token, FILE_EXECUTE, &mapping, &priv_set, &priv_set_len, &granted, &status);
918 ok(rc, "AccessCheck error %d\n", GetLastError());
919 ok(status == 1, "expected 1, got %d\n", status);
920 ok(granted == FILE_EXECUTE, "expected FILE_EXECUTE, got %#x\n", granted);
921
922 granted = 0xdeadbeef;
923 status = 0xdeadbeef;
924 SetLastError(0xdeadbeef);
925 rc = AccessCheck(sd, token, DELETE, &mapping, &priv_set, &priv_set_len, &granted, &status);
926 ok(rc, "AccessCheck error %d\n", GetLastError());
927 ok(status == 1, "expected 1, got %d\n", status);
928 ok(granted == DELETE, "expected DELETE, got %#x\n", granted);
929
930 granted = 0xdeadbeef;
931 status = 0xdeadbeef;
932 SetLastError(0xdeadbeef);
933 rc = AccessCheck(sd, token, FILE_DELETE_CHILD, &mapping, &priv_set, &priv_set_len, &granted, &status);
934 ok(rc, "AccessCheck error %d\n", GetLastError());
935 ok(status == 1, "expected 1, got %d\n", status);
936 ok(granted == FILE_DELETE_CHILD, "expected FILE_DELETE_CHILD, got %#x\n", granted);
937
938 granted = 0xdeadbeef;
939 status = 0xdeadbeef;
940 SetLastError(0xdeadbeef);
941 rc = AccessCheck(sd, token, 0x1ff, &mapping, &priv_set, &priv_set_len, &granted, &status);
942 ok(rc, "AccessCheck error %d\n", GetLastError());
943 ok(status == 1, "expected 1, got %d\n", status);
944 ok(granted == 0x1ff, "expected 0x1ff, got %#x\n", granted);
945
946 granted = 0xdeadbeef;
947 status = 0xdeadbeef;
948 SetLastError(0xdeadbeef);
949 rc = AccessCheck(sd, token, FILE_ALL_ACCESS, &mapping, &priv_set, &priv_set_len, &granted, &status);
950 ok(rc, "AccessCheck error %d\n", GetLastError());
951 ok(status == 1, "expected 1, got %d\n", status);
952 ok(granted == FILE_ALL_ACCESS, "expected FILE_ALL_ACCESS, got %#x\n", granted);
953
954 SetLastError(0xdeadbeef);
955 rc = AccessCheck(sd, token, 0xffffffff, &mapping, &priv_set, &priv_set_len, &granted, &status);
956 ok(!rc, "AccessCheck should fail\n");
957 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
958
959 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
960 SetLastError(0xdeadbeef);
961 fh = CreateFileA(file, FILE_READ_DATA, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_READONLY, 0);
962 ok(fh != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
963 retSize = 0xdeadbeef;
964 SetLastError(0xdeadbeef);
965 rc = WriteFile(fh, "1", 1, &retSize, NULL);
966 ok(!rc, "WriteFile should fail\n");
967 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
968 ok(retSize == 0, "expected 0, got %d\n", retSize);
969 CloseHandle(fh);
970
971 rc = GetFileAttributesA(file);
972 rc &= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED|FILE_ATTRIBUTE_COMPRESSED);
973 todo_wine
974 ok(rc == (FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY),
975 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc);
976
977 SetLastError(0xdeadbeef);
978 rc = SetFileAttributesA(file, FILE_ATTRIBUTE_ARCHIVE);
979 ok(rc, "SetFileAttributes error %d\n", GetLastError());
980 SetLastError(0xdeadbeef);
981 rc = DeleteFileA(file);
982 ok(rc, "DeleteFile error %d\n", GetLastError());
983
984 SetLastError(0xdeadbeef);
985 fh = CreateFileA(file, FILE_READ_DATA, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_READONLY, 0);
986 ok(fh != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
987 retSize = 0xdeadbeef;
988 SetLastError(0xdeadbeef);
989 rc = WriteFile(fh, "1", 1, &retSize, NULL);
990 ok(!rc, "WriteFile should fail\n");
991 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
992 ok(retSize == 0, "expected 0, got %d\n", retSize);
993 CloseHandle(fh);
994
995 rc = GetFileAttributesA(file);
996 rc &= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED|FILE_ATTRIBUTE_COMPRESSED);
997 ok(rc == (FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY),
998 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc);
999
1000 retSize = 0xdeadbeef;
1001 SetLastError(0xdeadbeef);
1002 rc = GetFileSecurityA(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
1003 sd, sdSize, &retSize);
1004 ok(rc, "GetFileSecurity error %d\n", GetLastError());
1005 ok(retSize == sdSize || broken(retSize == 0) /* NT4 */, "expected %d, got %d\n", sdSize, retSize);
1006
1007 priv_set_len = sizeof(priv_set);
1008 granted = 0xdeadbeef;
1009 status = 0xdeadbeef;
1010 SetLastError(0xdeadbeef);
1011 rc = AccessCheck(sd, token, FILE_READ_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
1012 ok(rc, "AccessCheck error %d\n", GetLastError());
1013 ok(status == 1, "expected 1, got %d\n", status);
1014 ok(granted == FILE_READ_DATA, "expected FILE_READ_DATA, got %#x\n", granted);
1015
1016 granted = 0xdeadbeef;
1017 status = 0xdeadbeef;
1018 SetLastError(0xdeadbeef);
1019 rc = AccessCheck(sd, token, FILE_WRITE_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
1020 ok(rc, "AccessCheck error %d\n", GetLastError());
1021 todo_wine {
1022 ok(status == 1, "expected 1, got %d\n", status);
1023 ok(granted == FILE_WRITE_DATA, "expected FILE_WRITE_DATA, got %#x\n", granted);
1024 }
1025 granted = 0xdeadbeef;
1026 status = 0xdeadbeef;
1027 SetLastError(0xdeadbeef);
1028 rc = AccessCheck(sd, token, FILE_EXECUTE, &mapping, &priv_set, &priv_set_len, &granted, &status);
1029 ok(rc, "AccessCheck error %d\n", GetLastError());
1030 ok(status == 1, "expected 1, got %d\n", status);
1031 ok(granted == FILE_EXECUTE, "expected FILE_EXECUTE, got %#x\n", granted);
1032
1033 granted = 0xdeadbeef;
1034 status = 0xdeadbeef;
1035 SetLastError(0xdeadbeef);
1036 rc = AccessCheck(sd, token, DELETE, &mapping, &priv_set, &priv_set_len, &granted, &status);
1037 ok(rc, "AccessCheck error %d\n", GetLastError());
1038 todo_wine {
1039 ok(status == 1, "expected 1, got %d\n", status);
1040 ok(granted == DELETE, "expected DELETE, got %#x\n", granted);
1041 }
1042 granted = 0xdeadbeef;
1043 status = 0xdeadbeef;
1044 SetLastError(0xdeadbeef);
1045 rc = AccessCheck(sd, token, FILE_DELETE_CHILD, &mapping, &priv_set, &priv_set_len, &granted, &status);
1046 ok(rc, "AccessCheck error %d\n", GetLastError());
1047 todo_wine {
1048 ok(status == 1, "expected 1, got %d\n", status);
1049 ok(granted == FILE_DELETE_CHILD, "expected FILE_DELETE_CHILD, got %#x\n", granted);
1050 }
1051 granted = 0xdeadbeef;
1052 status = 0xdeadbeef;
1053 SetLastError(0xdeadbeef);
1054 rc = AccessCheck(sd, token, 0x1ff, &mapping, &priv_set, &priv_set_len, &granted, &status);
1055 ok(rc, "AccessCheck error %d\n", GetLastError());
1056 todo_wine {
1057 ok(status == 1, "expected 1, got %d\n", status);
1058 ok(granted == 0x1ff, "expected 0x1ff, got %#x\n", granted);
1059 }
1060 granted = 0xdeadbeef;
1061 status = 0xdeadbeef;
1062 SetLastError(0xdeadbeef);
1063 rc = AccessCheck(sd, token, FILE_ALL_ACCESS, &mapping, &priv_set, &priv_set_len, &granted, &status);
1064 ok(rc, "AccessCheck error %d\n", GetLastError());
1065 todo_wine {
1066 ok(status == 1, "expected 1, got %d\n", status);
1067 ok(granted == FILE_ALL_ACCESS, "expected FILE_ALL_ACCESS, got %#x\n", granted);
1068 }
1069 SetLastError(0xdeadbeef);
1070 rc = DeleteFileA(file);
1071 ok(!rc, "DeleteFile should fail\n");
1072 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1073 SetLastError(0xdeadbeef);
1074 rc = SetFileAttributesA(file, FILE_ATTRIBUTE_ARCHIVE);
1075 ok(rc, "SetFileAttributes error %d\n", GetLastError());
1076 SetLastError(0xdeadbeef);
1077 rc = DeleteFileA(file);
1078 ok(rc, "DeleteFile error %d\n", GetLastError());
1079
1080 CloseHandle(token);
1081 HeapFree(GetProcessHeap(), 0, sd);
1082 }
1083
1084 static void test_AccessCheck(void)
1085 {
1086 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
1087 PACL Acl = NULL;
1088 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
1089 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
1090 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1091 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
1092 ACCESS_MASK Access;
1093 BOOL AccessStatus;
1094 HANDLE Token;
1095 HANDLE ProcessToken;
1096 BOOL ret;
1097 DWORD PrivSetLen;
1098 PRIVILEGE_SET *PrivSet;
1099 BOOL res;
1100 HMODULE NtDllModule;
1101 BOOLEAN Enabled;
1102 DWORD err;
1103 NTSTATUS ntret, ntAccessStatus;
1104
1105 NtDllModule = GetModuleHandleA("ntdll.dll");
1106 if (!NtDllModule)
1107 {
1108 skip("not running on NT, skipping test\n");
1109 return;
1110 }
1111 pRtlAdjustPrivilege = (void *)GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
1112 if (!pRtlAdjustPrivilege)
1113 {
1114 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1115 return;
1116 }
1117
1118 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
1119 res = InitializeAcl(Acl, 256, ACL_REVISION);
1120 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1121 {
1122 skip("ACLs not implemented - skipping tests\n");
1123 HeapFree(GetProcessHeap(), 0, Acl);
1124 return;
1125 }
1126 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
1127
1128 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
1129 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1130
1131 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1132 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
1133 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1134
1135 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1136 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
1137 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1138
1139 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1140
1141 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1142 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1143
1144 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1145 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1146
1147 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
1148 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
1149 PrivSet->PrivilegeCount = 16;
1150
1151 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
1152 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
1153
1154 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
1155
1156 res = DuplicateToken(ProcessToken, SecurityImpersonation, &Token);
1157 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1158
1159 /* SD without owner/group */
1160 SetLastError(0xdeadbeef);
1161 Access = AccessStatus = 0x1abe11ed;
1162 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
1163 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1164 err = GetLastError();
1165 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
1166 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
1167 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1168 "Access and/or AccessStatus were changed!\n");
1169
1170 /* Set owner and group */
1171 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
1172 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1173 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
1174 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1175
1176 /* Generic access mask */
1177 SetLastError(0xdeadbeef);
1178 Access = AccessStatus = 0x1abe11ed;
1179 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1180 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1181 err = GetLastError();
1182 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
1183 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
1184 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1185 "Access and/or AccessStatus were changed!\n");
1186
1187 /* Generic access mask - no privilegeset buffer */
1188 SetLastError(0xdeadbeef);
1189 Access = AccessStatus = 0x1abe11ed;
1190 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1191 NULL, &PrivSetLen, &Access, &AccessStatus);
1192 err = GetLastError();
1193 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1194 "with ERROR_NOACCESS, instead of %d\n", err);
1195 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1196 "Access and/or AccessStatus were changed!\n");
1197
1198 /* Generic access mask - no returnlength */
1199 SetLastError(0xdeadbeef);
1200 Access = AccessStatus = 0x1abe11ed;
1201 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1202 PrivSet, NULL, &Access, &AccessStatus);
1203 err = GetLastError();
1204 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1205 "with ERROR_NOACCESS, instead of %d\n", err);
1206 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1207 "Access and/or AccessStatus were changed!\n");
1208
1209 /* Generic access mask - no privilegeset buffer, no returnlength */
1210 SetLastError(0xdeadbeef);
1211 Access = AccessStatus = 0x1abe11ed;
1212 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1213 NULL, NULL, &Access, &AccessStatus);
1214 err = GetLastError();
1215 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1216 "with ERROR_NOACCESS, instead of %d\n", err);
1217 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1218 "Access and/or AccessStatus were changed!\n");
1219
1220 /* sd with no dacl present */
1221 Access = AccessStatus = 0x1abe11ed;
1222 ret = SetSecurityDescriptorDacl(SecurityDescriptor, FALSE, NULL, FALSE);
1223 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1224 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1225 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1226 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1227 ok(AccessStatus && (Access == KEY_READ),
1228 "AccessCheck failed to grant access with error %d\n",
1229 GetLastError());
1230
1231 /* sd with no dacl present - no privilegeset buffer */
1232 SetLastError(0xdeadbeef);
1233 Access = AccessStatus = 0x1abe11ed;
1234 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1235 NULL, &PrivSetLen, &Access, &AccessStatus);
1236 err = GetLastError();
1237 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1238 "with ERROR_NOACCESS, instead of %d\n", err);
1239 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1240 "Access and/or AccessStatus were changed!\n");
1241
1242 if(pNtAccessCheck)
1243 {
1244 /* Generic access mask - no privilegeset buffer */
1245 SetLastError(0xdeadbeef);
1246 Access = ntAccessStatus = 0x1abe11ed;
1247 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1248 NULL, &PrivSetLen, &Access, &ntAccessStatus);
1249 err = GetLastError();
1250 ok(ntret == STATUS_ACCESS_VIOLATION,
1251 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1252 ok(err == 0xdeadbeef,
1253 "NtAccessCheck shouldn't set last error, got %d\n", err);
1254 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1255 "Access and/or AccessStatus were changed!\n");
1256
1257 /* Generic access mask - no returnlength */
1258 SetLastError(0xdeadbeef);
1259 Access = ntAccessStatus = 0x1abe11ed;
1260 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1261 PrivSet, NULL, &Access, &ntAccessStatus);
1262 err = GetLastError();
1263 ok(ntret == STATUS_ACCESS_VIOLATION,
1264 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1265 ok(err == 0xdeadbeef,
1266 "NtAccessCheck shouldn't set last error, got %d\n", err);
1267 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1268 "Access and/or AccessStatus were changed!\n");
1269
1270 /* Generic access mask - no privilegeset buffer, no returnlength */
1271 SetLastError(0xdeadbeef);
1272 Access = ntAccessStatus = 0x1abe11ed;
1273 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1274 NULL, NULL, &Access, &ntAccessStatus);
1275 err = GetLastError();
1276 ok(ntret == STATUS_ACCESS_VIOLATION,
1277 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1278 ok(err == 0xdeadbeef,
1279 "NtAccessCheck shouldn't set last error, got %d\n", err);
1280 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1281 "Access and/or AccessStatus were changed!\n");
1282 }
1283 else
1284 win_skip("NtAccessCheck unavailable. Skipping.\n");
1285
1286 /* sd with NULL dacl */
1287 Access = AccessStatus = 0x1abe11ed;
1288 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, NULL, FALSE);
1289 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1290 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1291 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1292 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1293 ok(AccessStatus && (Access == KEY_READ),
1294 "AccessCheck failed to grant access with error %d\n",
1295 GetLastError());
1296
1297 /* sd with blank dacl */
1298 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1299 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1300 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1301 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1302 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1303 err = GetLastError();
1304 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1305 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1306 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1307
1308 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
1309 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1310
1311 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
1312 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1313
1314 /* sd with dacl */
1315 Access = AccessStatus = 0x1abe11ed;
1316 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1317 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1318 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1319 ok(AccessStatus && (Access == KEY_READ),
1320 "AccessCheck failed to grant access with error %d\n",
1321 GetLastError());
1322
1323 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1324 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1325 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1326 ok(AccessStatus,
1327 "AccessCheck failed to grant any access with error %d\n",
1328 GetLastError());
1329 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
1330
1331 /* Access denied by SD */
1332 SetLastError(0xdeadbeef);
1333 Access = AccessStatus = 0x1abe11ed;
1334 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
1335 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1336 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1337 err = GetLastError();
1338 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1339 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1340 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1341
1342 SetLastError(0);
1343 PrivSet->PrivilegeCount = 16;
1344 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1345 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1346 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
1347 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1348 GetLastError());
1349
1350 ret = ImpersonateLoggedOnUser(Token);
1351 ok(ret, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1352 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
1353 if (!ret)
1354 {
1355 SetLastError(0);
1356 PrivSet->PrivilegeCount = 16;
1357 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1358 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1359 ok(ret && AccessStatus && GetLastError() == 0,
1360 "AccessCheck should have succeeded, error %d\n",
1361 GetLastError());
1362 ok(Access == ACCESS_SYSTEM_SECURITY,
1363 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1364 Access);
1365 }
1366 else
1367 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1368 ret);
1369 ret = RevertToSelf();
1370 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1371
1372 /* test INHERIT_ONLY_ACE */
1373 ret = InitializeAcl(Acl, 256, ACL_REVISION);
1374 ok(ret, "InitializeAcl failed with error %d\n", GetLastError());
1375
1376 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1377 * the next ones.
1378 */
1379 if (pAddAccessAllowedAceEx)
1380 {
1381 ret = pAddAccessAllowedAceEx(Acl, ACL_REVISION, INHERIT_ONLY_ACE, KEY_READ, EveryoneSid);
1382 ok(ret, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1383 }
1384 else
1385 win_skip("AddAccessAllowedAceEx is not available\n");
1386
1387 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1388 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1389 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1390 err = GetLastError();
1391 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1392 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1393 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1394
1395 CloseHandle(Token);
1396
1397 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
1398 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1399
1400 SetLastError(0xdeadbeef);
1401 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1402 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1403 err = GetLastError();
1404 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
1405 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
1406
1407 CloseHandle(Token);
1408
1409 SetLastError(0xdeadbeef);
1410 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
1411 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1412 err = GetLastError();
1413 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
1414 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
1415
1416 CloseHandle(ProcessToken);
1417
1418 if (EveryoneSid)
1419 FreeSid(EveryoneSid);
1420 if (AdminSid)
1421 FreeSid(AdminSid);
1422 if (UsersSid)
1423 FreeSid(UsersSid);
1424 HeapFree(GetProcessHeap(), 0, Acl);
1425 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1426 HeapFree(GetProcessHeap(), 0, PrivSet);
1427 }
1428
1429 /* test GetTokenInformation for the various attributes */
1430 static void test_token_attr(void)
1431 {
1432 HANDLE Token, ImpersonationToken;
1433 DWORD Size, Size2;
1434 TOKEN_PRIVILEGES *Privileges;
1435 TOKEN_GROUPS *Groups;
1436 TOKEN_USER *User;
1437 TOKEN_DEFAULT_DACL *Dacl;
1438 BOOL ret;
1439 DWORD i, GLE;
1440 LPSTR SidString;
1441 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1442 ACL *acl;
1443
1444 /* cygwin-like use case */
1445 SetLastError(0xdeadbeef);
1446 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
1447 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1448 {
1449 win_skip("OpenProcessToken is not implemented\n");
1450 return;
1451 }
1452 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1453 if (ret)
1454 {
1455 DWORD buf[256]; /* GetTokenInformation wants a dword-aligned buffer */
1456 Size = sizeof(buf);
1457 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
1458 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
1459 Size = sizeof(ImpersonationLevel);
1460 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1461 GLE = GetLastError();
1462 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
1463 CloseHandle(Token);
1464 }
1465
1466 if(!pConvertSidToStringSidA)
1467 {
1468 win_skip("ConvertSidToStringSidA is not available\n");
1469 return;
1470 }
1471
1472 SetLastError(0xdeadbeef);
1473 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &Token);
1474 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1475
1476 /* groups */
1477 /* insufficient buffer length */
1478 SetLastError(0xdeadbeef);
1479 Size2 = 0;
1480 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size2);
1481 ok(Size2 > 1, "got %d\n", Size2);
1482 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1483 "%d with error %d\n", ret, GetLastError());
1484 Size2 -= 1;
1485 Groups = HeapAlloc(GetProcessHeap(), 0, Size2);
1486 memset(Groups, 0xcc, Size2);
1487 Size = 0;
1488 ret = GetTokenInformation(Token, TokenGroups, Groups, Size2, &Size);
1489 ok(Size > 1, "got %d\n", Size);
1490 ok((!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER) || broken(ret) /* wow64 */,
1491 "%d with error %d\n", ret, GetLastError());
1492 if(!ret)
1493 ok(*((BYTE*)Groups) == 0xcc, "buffer altered\n");
1494
1495 HeapFree(GetProcessHeap(), 0, Groups);
1496
1497 SetLastError(0xdeadbeef);
1498 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
1499 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1500 "GetTokenInformation(TokenGroups) %s with error %d\n",
1501 ret ? "succeeded" : "failed", GetLastError());
1502 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
1503 SetLastError(0xdeadbeef);
1504 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
1505 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1506 ok(GetLastError() == 0xdeadbeef,
1507 "GetTokenInformation shouldn't have set last error to %d\n",
1508 GetLastError());
1509 trace("TokenGroups:\n");
1510 for (i = 0; i < Groups->GroupCount; i++)
1511 {
1512 DWORD NameLength = 255;
1513 CHAR Name[255];
1514 DWORD DomainLength = 255;
1515 CHAR Domain[255];
1516 SID_NAME_USE SidNameUse;
1517 Name[0] = '\0';
1518 Domain[0] = '\0';
1519 ret = LookupAccountSidA(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
1520 if (ret)
1521 {
1522 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
1523 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
1524 LocalFree(SidString);
1525 }
1526 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups->Groups[i].Attributes, GetLastError());
1527 }
1528 HeapFree(GetProcessHeap(), 0, Groups);
1529
1530 /* user */
1531 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1532 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1533 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1534 User = HeapAlloc(GetProcessHeap(), 0, Size);
1535 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1536 ok(ret,
1537 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1538
1539 pConvertSidToStringSidA(User->User.Sid, &SidString);
1540 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
1541 LocalFree(SidString);
1542 HeapFree(GetProcessHeap(), 0, User);
1543
1544 /* privileges */
1545 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1546 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1547 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1548 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1549 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1550 ok(ret,
1551 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1552 trace("TokenPrivileges:\n");
1553 for (i = 0; i < Privileges->PrivilegeCount; i++)
1554 {
1555 CHAR Name[256];
1556 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
1557 LookupPrivilegeNameA(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
1558 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
1559 }
1560 HeapFree(GetProcessHeap(), 0, Privileges);
1561
1562 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
1563 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1564
1565 Size = sizeof(ImpersonationLevel);
1566 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1567 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1568 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
1569
1570 CloseHandle(ImpersonationToken);
1571
1572 /* default dacl */
1573 ret = GetTokenInformation(Token, TokenDefaultDacl, NULL, 0, &Size);
1574 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1575 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1576
1577 Dacl = HeapAlloc(GetProcessHeap(), 0, Size);
1578 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size);
1579 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1580
1581 SetLastError(0xdeadbeef);
1582 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, 0);
1583 GLE = GetLastError();
1584 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1585 ok(GLE == ERROR_BAD_LENGTH, "expected ERROR_BAD_LENGTH got %u\n", GLE);
1586
1587 SetLastError(0xdeadbeef);
1588 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, Size);
1589 GLE = GetLastError();
1590 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1591 ok(GLE == ERROR_NOACCESS, "expected ERROR_NOACCESS got %u\n", GLE);
1592
1593 acl = Dacl->DefaultDacl;
1594 Dacl->DefaultDacl = NULL;
1595
1596 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1597 ok(ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1598
1599 Size2 = 0;
1600 Dacl->DefaultDacl = (ACL *)0xdeadbeef;
1601 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1602 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1603 ok(Dacl->DefaultDacl == NULL, "expected NULL, got %p\n", Dacl->DefaultDacl);
1604 ok(Size2 == sizeof(TOKEN_DEFAULT_DACL) || broken(Size2 == 2*sizeof(TOKEN_DEFAULT_DACL)), /* WoW64 */
1605 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2);
1606
1607 Dacl->DefaultDacl = acl;
1608 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1609 ok(ret, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1610
1611 if (Size2 == sizeof(TOKEN_DEFAULT_DACL)) {
1612 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1613 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1614 } else
1615 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1616
1617 HeapFree(GetProcessHeap(), 0, Dacl);
1618 CloseHandle(Token);
1619 }
1620
1621 typedef union _MAX_SID
1622 {
1623 SID sid;
1624 char max[SECURITY_MAX_SID_SIZE];
1625 } MAX_SID;
1626
1627 static void test_sid_str(PSID * sid)
1628 {
1629 char *str_sid;
1630 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
1631 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1632 if (ret)
1633 {
1634 char account[MAX_PATH], domain[MAX_PATH];
1635 SID_NAME_USE use;
1636 DWORD acc_size = MAX_PATH;
1637 DWORD dom_size = MAX_PATH;
1638 ret = LookupAccountSidA (NULL, sid, account, &acc_size, domain, &dom_size, &use);
1639 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
1640 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
1641 if (ret)
1642 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
1643 else if (GetLastError() == ERROR_NONE_MAPPED)
1644 trace(" %s couldn't be mapped\n", str_sid);
1645 LocalFree(str_sid);
1646 }
1647 }
1648
1649 static const struct well_known_sid_value
1650 {
1651 BOOL without_domain;
1652 const char *sid_string;
1653 } well_known_sid_values[] = {
1654 /* 0 */ {TRUE, "S-1-0-0"}, {TRUE, "S-1-1-0"}, {TRUE, "S-1-2-0"}, {TRUE, "S-1-3-0"},
1655 /* 4 */ {TRUE, "S-1-3-1"}, {TRUE, "S-1-3-2"}, {TRUE, "S-1-3-3"}, {TRUE, "S-1-5"},
1656 /* 8 */ {FALSE, "S-1-5-1"}, {TRUE, "S-1-5-2"}, {TRUE, "S-1-5-3"}, {TRUE, "S-1-5-4"},
1657 /* 12 */ {TRUE, "S-1-5-6"}, {TRUE, "S-1-5-7"}, {TRUE, "S-1-5-8"}, {TRUE, "S-1-5-9"},
1658 /* 16 */ {TRUE, "S-1-5-10"}, {TRUE, "S-1-5-11"}, {TRUE, "S-1-5-12"}, {TRUE, "S-1-5-13"},
1659 /* 20 */ {TRUE, "S-1-5-14"}, {FALSE, NULL}, {TRUE, "S-1-5-18"}, {TRUE, "S-1-5-19"},
1660 /* 24 */ {TRUE, "S-1-5-20"}, {TRUE, "S-1-5-32"},
1661 /* 26 */ {FALSE, "S-1-5-32-544"}, {TRUE, "S-1-5-32-545"}, {TRUE, "S-1-5-32-546"},
1662 /* 29 */ {TRUE, "S-1-5-32-547"}, {TRUE, "S-1-5-32-548"}, {TRUE, "S-1-5-32-549"},
1663 /* 32 */ {TRUE, "S-1-5-32-550"}, {TRUE, "S-1-5-32-551"}, {TRUE, "S-1-5-32-552"},
1664 /* 35 */ {TRUE, "S-1-5-32-554"}, {TRUE, "S-1-5-32-555"}, {TRUE, "S-1-5-32-556"},
1665 /* 38 */ {FALSE, "S-1-5-21-12-23-34-45-56-500"}, {FALSE, "S-1-5-21-12-23-34-45-56-501"},
1666 /* 40 */ {FALSE, "S-1-5-21-12-23-34-45-56-502"}, {FALSE, "S-1-5-21-12-23-34-45-56-512"},
1667 /* 42 */ {FALSE, "S-1-5-21-12-23-34-45-56-513"}, {FALSE, "S-1-5-21-12-23-34-45-56-514"},
1668 /* 44 */ {FALSE, "S-1-5-21-12-23-34-45-56-515"}, {FALSE, "S-1-5-21-12-23-34-45-56-516"},
1669 /* 46 */ {FALSE, "S-1-5-21-12-23-34-45-56-517"}, {FALSE, "S-1-5-21-12-23-34-45-56-518"},
1670 /* 48 */ {FALSE, "S-1-5-21-12-23-34-45-56-519"}, {FALSE, "S-1-5-21-12-23-34-45-56-520"},
1671 /* 50 */ {FALSE, "S-1-5-21-12-23-34-45-56-553"},
1672 /* Added in Windows Server 2003 */
1673 /* 51 */ {TRUE, "S-1-5-64-10"}, {TRUE, "S-1-5-64-21"}, {TRUE, "S-1-5-64-14"},
1674 /* 54 */ {TRUE, "S-1-5-15"}, {TRUE, "S-1-5-1000"}, {FALSE, "S-1-5-32-557"},
1675 /* 57 */ {TRUE, "S-1-5-32-558"}, {TRUE, "S-1-5-32-559"}, {TRUE, "S-1-5-32-560"},
1676 /* 60 */ {TRUE, "S-1-5-32-561"}, {TRUE, "S-1-5-32-562"},
1677 /* Added in Windows Vista: */
1678 /* 62 */ {TRUE, "S-1-5-32-568"},
1679 /* 63 */ {TRUE, "S-1-5-17"}, {FALSE, "S-1-5-32-569"}, {TRUE, "S-1-16-0"},
1680 /* 66 */ {TRUE, "S-1-16-4096"}, {TRUE, "S-1-16-8192"}, {TRUE, "S-1-16-12288"},
1681 /* 69 */ {TRUE, "S-1-16-16384"}, {TRUE, "S-1-5-33"}, {TRUE, "S-1-3-4"},
1682 /* 72 */ {FALSE, "S-1-5-21-12-23-34-45-56-571"}, {FALSE, "S-1-5-21-12-23-34-45-56-572"},
1683 /* 74 */ {TRUE, "S-1-5-22"}, {FALSE, "S-1-5-21-12-23-34-45-56-521"}, {TRUE, "S-1-5-32-573"},
1684 /* 77 */ {FALSE, "S-1-5-21-12-23-34-45-56-498"}, {TRUE, "S-1-5-32-574"}, {TRUE, "S-1-16-8448"}
1685 };
1686
1687 static void test_CreateWellKnownSid(void)
1688 {
1689 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1690 PSID domainsid, sid;
1691 DWORD size, error;
1692 BOOL ret;
1693 unsigned int i;
1694
1695 if (!pCreateWellKnownSid)
1696 {
1697 win_skip("CreateWellKnownSid not available\n");
1698 return;
1699 }
1700
1701 size = 0;
1702 SetLastError(0xdeadbeef);
1703 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1704 error = GetLastError();
1705 ok(!ret, "CreateWellKnownSid succeeded\n");
1706 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1707 ok(size, "expected size > 0\n");
1708
1709 SetLastError(0xdeadbeef);
1710 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1711 error = GetLastError();
1712 ok(!ret, "CreateWellKnownSid succeeded\n");
1713 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
1714
1715 sid = HeapAlloc(GetProcessHeap(), 0, size);
1716 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, sid, &size);
1717 ok(ret, "CreateWellKnownSid failed %u\n", GetLastError());
1718 HeapFree(GetProcessHeap(), 0, sid);
1719
1720 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1721 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1722
1723 for (i = 0; i < sizeof(well_known_sid_values)/sizeof(well_known_sid_values[0]); i++)
1724 {
1725 const struct well_known_sid_value *value = &well_known_sid_values[i];
1726 char sid_buffer[SECURITY_MAX_SID_SIZE];
1727 LPSTR str;
1728 DWORD cb;
1729
1730 if (value->sid_string == NULL)
1731 continue;
1732
1733 if (i > WinAccountRasAndIasServersSid)
1734 {
1735 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1736 cb = sizeof(sid_buffer);
1737 if (!pCreateWellKnownSid(i, domainsid, sid_buffer, &cb))
1738 {
1739 skip("Well known SIDs starting from %u are not implemented\n", i);
1740 break;
1741 }
1742 }
1743
1744 cb = sizeof(sid_buffer);
1745 ok(pCreateWellKnownSid(i, value->without_domain ? NULL : domainsid, sid_buffer, &cb), "Couldn't create well known sid %u\n", i);
1746 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1747 ok(IsValidSid(sid_buffer), "The sid is not valid\n");
1748 ok(pConvertSidToStringSidA(sid_buffer, &str), "Couldn't convert SID to string\n");
1749 ok(strcmp(str, value->sid_string) == 0, "%d: SID mismatch - expected %s, got %s\n", i,
1750 value->sid_string, str);
1751 LocalFree(str);
1752
1753 if (value->without_domain)
1754 {
1755 char buf2[SECURITY_MAX_SID_SIZE];
1756 cb = sizeof(buf2);
1757 ok(pCreateWellKnownSid(i, domainsid, buf2, &cb), "Couldn't create well known sid %u with optional domain\n", i);
1758 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1759 ok(memcmp(buf2, sid_buffer, cb) == 0, "SID create with domain is different than without (%u)\n", i);
1760 }
1761 }
1762
1763 FreeSid(domainsid);
1764 }
1765
1766 static void test_LookupAccountSid(void)
1767 {
1768 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1769 CHAR accountA[MAX_PATH], domainA[MAX_PATH], usernameA[MAX_PATH];
1770 DWORD acc_sizeA, dom_sizeA, user_sizeA;
1771 DWORD real_acc_sizeA, real_dom_sizeA;
1772 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
1773 DWORD acc_sizeW, dom_sizeW;
1774 DWORD real_acc_sizeW, real_dom_sizeW;
1775 PSID pUsersSid = NULL;
1776 SID_NAME_USE use;
1777 BOOL ret;
1778 DWORD error, size, cbti = 0;
1779 MAX_SID max_sid;
1780 CHAR *str_sidA;
1781 int i;
1782 HANDLE hToken;
1783 PTOKEN_USER ptiUser = NULL;
1784
1785 /* native windows crashes if account size, domain size, or name use is NULL */
1786
1787 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1788 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1789 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1790 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1791
1792 /* not running on NT so give up */
1793 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1794 return;
1795
1796 real_acc_sizeA = MAX_PATH;
1797 real_dom_sizeA = MAX_PATH;
1798 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1799 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1800
1801 /* try NULL account */
1802 acc_sizeA = MAX_PATH;
1803 dom_sizeA = MAX_PATH;
1804 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1805 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1806
1807 /* try NULL domain */
1808 acc_sizeA = MAX_PATH;
1809 dom_sizeA = MAX_PATH;
1810 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1811 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1812
1813 /* try a small account buffer */
1814 acc_sizeA = 1;
1815 dom_sizeA = MAX_PATH;
1816 accountA[0] = 0;
1817 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1818 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1819 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1820 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1821
1822 /* try a 0 sized account buffer */
1823 acc_sizeA = 0;
1824 dom_sizeA = MAX_PATH;
1825 accountA[0] = 0;
1826 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1827 /* this can fail or succeed depending on OS version but the size will always be returned */
1828 ok(acc_sizeA == real_acc_sizeA + 1,
1829 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1830 real_acc_sizeA + 1, acc_sizeA);
1831
1832 /* try a 0 sized account buffer */
1833 acc_sizeA = 0;
1834 dom_sizeA = MAX_PATH;
1835 LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1836 /* this can fail or succeed depending on OS version but the size will always be returned */
1837 ok(acc_sizeA == real_acc_sizeA + 1,
1838 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1839 real_acc_sizeA + 1, acc_sizeA);
1840
1841 /* try a small domain buffer */
1842 dom_sizeA = 1;
1843 acc_sizeA = MAX_PATH;
1844 accountA[0] = 0;
1845 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1846 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1847 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1848 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1849
1850 /* try a 0 sized domain buffer */
1851 dom_sizeA = 0;
1852 acc_sizeA = MAX_PATH;
1853 accountA[0] = 0;
1854 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1855 /* this can fail or succeed depending on OS version but the size will always be returned */
1856 ok(dom_sizeA == real_dom_sizeA + 1,
1857 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1858 real_dom_sizeA + 1, dom_sizeA);
1859
1860 /* try a 0 sized domain buffer */
1861 dom_sizeA = 0;
1862 acc_sizeA = MAX_PATH;
1863 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1864 /* this can fail or succeed depending on OS version but the size will always be returned */
1865 ok(dom_sizeA == real_dom_sizeA + 1,
1866 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1867 real_dom_sizeA + 1, dom_sizeA);
1868
1869 real_acc_sizeW = MAX_PATH;
1870 real_dom_sizeW = MAX_PATH;
1871 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1872 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1873
1874 /* try an invalid system name */
1875 real_acc_sizeA = MAX_PATH;
1876 real_dom_sizeA = MAX_PATH;
1877 ret = LookupAccountSidA("deepthought", pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1878 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1879 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
1880 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1881
1882 /* native windows crashes if domainW or accountW is NULL */
1883
1884 /* try a small account buffer */
1885 acc_sizeW = 1;
1886 dom_sizeW = MAX_PATH;
1887 accountW[0] = 0;
1888 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1889 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1890 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1891 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1892
1893 /* try a 0 sized account buffer */
1894 acc_sizeW = 0;
1895 dom_sizeW = MAX_PATH;
1896 accountW[0] = 0;
1897 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1898 /* this can fail or succeed depending on OS version but the size will always be returned */
1899 ok(acc_sizeW == real_acc_sizeW + 1,
1900 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1901 real_acc_sizeW + 1, acc_sizeW);
1902
1903 /* try a 0 sized account buffer */
1904 acc_sizeW = 0;
1905 dom_sizeW = MAX_PATH;
1906 LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1907 /* this can fail or succeed depending on OS version but the size will always be returned */
1908 ok(acc_sizeW == real_acc_sizeW + 1,
1909 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1910 real_acc_sizeW + 1, acc_sizeW);
1911
1912 /* try a small domain buffer */
1913 dom_sizeW = 1;
1914 acc_sizeW = MAX_PATH;
1915 accountW[0] = 0;
1916 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1917 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1918 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1919 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1920
1921 /* try a 0 sized domain buffer */
1922 dom_sizeW = 0;
1923 acc_sizeW = MAX_PATH;
1924 accountW[0] = 0;
1925 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1926 /* this can fail or succeed depending on OS version but the size will always be returned */
1927 ok(dom_sizeW == real_dom_sizeW + 1,
1928 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1929 real_dom_sizeW + 1, dom_sizeW);
1930
1931 /* try a 0 sized domain buffer */
1932 dom_sizeW = 0;
1933 acc_sizeW = MAX_PATH;
1934 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1935 /* this can fail or succeed depending on OS version but the size will always be returned */
1936 ok(dom_sizeW == real_dom_sizeW + 1,
1937 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1938 real_dom_sizeW + 1, dom_sizeW);
1939
1940 acc_sizeW = dom_sizeW = use = 0;
1941 SetLastError(0xdeadbeef);
1942 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, NULL, &dom_sizeW, &use);
1943 error = GetLastError();
1944 ok(!ret, "LookupAccountSidW failed %u\n", GetLastError());
1945 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1946 ok(acc_sizeW, "expected non-zero account size\n");
1947 ok(dom_sizeW, "expected non-zero domain size\n");
1948 ok(!use, "expected zero use %u\n", use);
1949
1950 FreeSid(pUsersSid);
1951
1952 /* Test LookupAccountSid with Sid retrieved from token information.
1953 This assumes this process is running under the account of the current user.*/
1954 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &hToken);
1955 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1956 ret = GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti);
1957 ok(!ret, "GetTokenInformation failed with error %d\n", GetLastError());
1958 ptiUser = HeapAlloc(GetProcessHeap(), 0, cbti);
1959 if (GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti))
1960 {
1961 acc_sizeA = dom_sizeA = MAX_PATH;
1962 ret = LookupAccountSidA(NULL, ptiUser->User.Sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1963 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1964 user_sizeA = MAX_PATH;
1965 ret = GetUserNameA(usernameA , &user_sizeA);
1966 ok(ret, "GetUserNameA() Expected TRUE, got FALSE\n");
1967 ok(lstrcmpA(usernameA, accountA) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA, accountA );
1968 }
1969 HeapFree(GetProcessHeap(), 0, ptiUser);
1970
1971 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1972 {
1973 trace("Well Known SIDs:\n");
1974 for (i = 0; i <= 60; i++)
1975 {
1976 size = SECURITY_MAX_SID_SIZE;
1977 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1978 {
1979 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1980 {
1981 acc_sizeA = MAX_PATH;
1982 dom_sizeA = MAX_PATH;
1983 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1984 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1985 LocalFree(str_sidA);
1986 }
1987 }
1988 else
1989 {
1990 if (GetLastError() != ERROR_INVALID_PARAMETER)
1991 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1992 else
1993 trace(" %d: not supported\n", i);
1994 }
1995 }
1996
1997 pLsaQueryInformationPolicy = (void *)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1998 pLsaOpenPolicy = (void *)GetProcAddress( hmod, "LsaOpenPolicy");
1999 pLsaFreeMemory = (void *)GetProcAddress( hmod, "LsaFreeMemory");
2000 pLsaClose = (void *)GetProcAddress( hmod, "LsaClose");
2001
2002 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
2003 {
2004 NTSTATUS status;
2005 LSA_HANDLE handle;
2006 LSA_OBJECT_ATTRIBUTES object_attributes;
2007
2008 ZeroMemory(&object_attributes, sizeof(object_attributes));
2009 object_attributes.Length = sizeof(object_attributes);
2010
2011 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
2012 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
2013 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
2014
2015 /* try a more restricted access mask if necessary */
2016 if (status == STATUS_ACCESS_DENIED) {
2017 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
2018 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
2019 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
2020 }
2021
2022 if (status == STATUS_SUCCESS)
2023 {
2024 PPOLICY_ACCOUNT_DOMAIN_INFO info;
2025 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
2026 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
2027 if (status == STATUS_SUCCESS)
2028 {
2029 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
2030 if (info->DomainSid)
2031 {
2032 int count = *GetSidSubAuthorityCount(info->DomainSid);
2033 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
2034 test_sid_str((PSID)&max_sid.sid);
2035 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
2036 max_sid.sid.SubAuthorityCount = count + 1;
2037 test_sid_str((PSID)&max_sid.sid);
2038 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
2039 test_sid_str((PSID)&max_sid.sid);
2040 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
2041 test_sid_str((PSID)&max_sid.sid);
2042 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
2043 test_sid_str((PSID)&max_sid.sid);
2044 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
2045 test_sid_str((PSID)&max_sid.sid);
2046 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
2047 test_sid_str((PSID)&max_sid.sid);
2048 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
2049 test_sid_str((PSID)&max_sid.sid);
2050 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
2051 test_sid_str((PSID)&max_sid.sid);
2052 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
2053 test_sid_str((PSID)&max_sid.sid);
2054 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
2055 test_sid_str((PSID)&max_sid.sid);
2056 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
2057 test_sid_str((PSID)&max_sid.sid);
2058 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
2059 test_sid_str((PSID)&max_sid.sid);
2060 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
2061 test_sid_str((PSID)&max_sid.sid);
2062 }
2063
2064 pLsaFreeMemory((LPVOID)info);
2065 }
2066
2067 status = pLsaClose(handle);
2068 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
2069 }
2070 }
2071 }
2072 }
2073
2074 static BOOL get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
2075 {
2076 static CHAR account[UNLEN + 1];
2077 static CHAR domain[UNLEN + 1];
2078 DWORD size, dom_size;
2079 SID_NAME_USE use;
2080
2081 *user = account;
2082 *dom = domain;
2083
2084 size = dom_size = UNLEN + 1;
2085 account[0] = '\0';
2086 domain[0] = '\0';
2087 SetLastError(0xdeadbeef);
2088 return LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
2089 }
2090
2091 static void check_wellknown_name(const char* name, WELL_KNOWN_SID_TYPE result)
2092 {
2093 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
2094 PSID domainsid = NULL;
2095 char wk_sid[SECURITY_MAX_SID_SIZE];
2096 DWORD cb;
2097
2098 DWORD sid_size, domain_size;
2099 SID_NAME_USE sid_use;
2100 LPSTR domain, account, sid_domain, wk_domain, wk_account;
2101 PSID psid;
2102 BOOL ret ,ret2;
2103
2104 sid_size = 0;
2105 domain_size = 0;
2106 ret = LookupAccountNameA(NULL, name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2107 ok(!ret, " %s Should have failed to lookup account name\n", name);
2108 psid = HeapAlloc(GetProcessHeap(),0,sid_size);
2109 domain = HeapAlloc(GetProcessHeap(),0,domain_size);
2110 ret = LookupAccountNameA(NULL, name, psid, &sid_size, domain, &domain_size, &sid_use);
2111
2112 if (!result)
2113 {
2114 ok(!ret, " %s Should have failed to lookup account name\n",name);
2115 goto cleanup;
2116 }
2117
2118 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
2119 cb = sizeof(wk_sid);
2120 if (!pCreateWellKnownSid(result, domainsid, wk_sid, &cb))
2121 {
2122 win_skip("SID %i is not available on the system\n",result);
2123 goto cleanup;
2124 }
2125
2126 ret2 = get_sid_info(wk_sid, &wk_account, &wk_domain);
2127 if (!ret2 && GetLastError() == ERROR_NONE_MAPPED)
2128 {
2129 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name);
2130 goto cleanup;
2131 }
2132
2133 get_sid_info(psid, &account, &sid_domain);
2134
2135 ok(ret, "Failed to lookup account name %s\n",name);
2136 ok(sid_size != 0, "sid_size was zero\n");
2137
2138 ok(EqualSid(psid,wk_sid),"(%s) Sids fail to match well known sid!\n",name);
2139
2140 ok(!lstrcmpA(account, wk_account), "Expected %s , got %s\n", account, wk_account);
2141 ok(!lstrcmpA(domain, wk_domain), "Expected %s, got %s\n", wk_domain, domain);
2142 ok(sid_use == SidTypeWellKnownGroup , "Expected Use (5), got %d\n", sid_use);
2143
2144 cleanup:
2145 FreeSid(domainsid);
2146 HeapFree(GetProcessHeap(),0,psid);
2147 HeapFree(GetProcessHeap(),0,domain);
2148 }
2149
2150 static void test_LookupAccountName(void)
2151 {
2152 DWORD sid_size, domain_size, user_size;
2153 DWORD sid_save, domain_save;
2154 CHAR user_name[UNLEN + 1];
2155 CHAR computer_name[UNLEN + 1];
2156 SID_NAME_USE sid_use;
2157 LPSTR domain, account, sid_dom;
2158 PSID psid;
2159 BOOL ret;
2160
2161 /* native crashes if (assuming all other parameters correct):
2162 * - peUse is NULL
2163 * - Sid is NULL and cbSid is > 0
2164 * - cbSid or cchReferencedDomainName are NULL
2165 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2166 */
2167
2168 user_size = UNLEN + 1;
2169 SetLastError(0xdeadbeef);
2170 ret = GetUserNameA(user_name, &user_size);
2171 ok(ret, "Failed to get user name : %d\n", GetLastError());
2172
2173 /* get sizes */
2174 sid_size = 0;
2175 domain_size = 0;
2176 sid_use = 0xcafebabe;
2177 SetLastError(0xdeadbeef);
2178 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2179 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2180 {
2181 win_skip("LookupAccountNameA is not implemented\n");
2182 return;
2183 }
2184 ok(!ret, "Expected 0, got %d\n", ret);
2185 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2186 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2187 ok(sid_size != 0, "Expected non-zero sid size\n");
2188 ok(domain_size != 0, "Expected non-zero domain size\n");
2189 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
2190
2191 sid_save = sid_size;
2192 domain_save = domain_size;
2193
2194 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2195 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2196
2197 /* try valid account name */
2198 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
2199 get_sid_info(psid, &account, &sid_dom);
2200 ok(ret, "Failed to lookup account name\n");
2201 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
2202 ok(!lstrcmpA(account, user_name), "Expected %s, got %s\n", user_name, account);
2203 ok(!lstrcmpA(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
2204 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
2205 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlenA(domain), domain_size);
2206 ok(sid_use == SidTypeUser, "Expected SidTypeUser (%d), got %d\n", SidTypeUser, sid_use);
2207 domain_size = domain_save;
2208 sid_size = sid_save;
2209
2210 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2211 {
2212 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2213 }
2214 else
2215 {
2216 ret = LookupAccountNameA(NULL, "Everyone", psid, &sid_size, domain, &domain_size, &sid_use);
2217 get_sid_info(psid, &account, &sid_dom);
2218 ok(ret, "Failed to lookup account name\n");
2219 ok(sid_size != 0, "sid_size was zero\n");
2220 ok(!lstrcmpA(account, "Everyone"), "Expected Everyone, got %s\n", account);
2221 ok(!lstrcmpA(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
2222 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2223 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlenA(domain), domain_size);
2224 ok(sid_use == SidTypeWellKnownGroup, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup, sid_use);
2225 domain_size = domain_save;
2226 }
2227
2228 /* NULL Sid with zero sid size */
2229 SetLastError(0xdeadbeef);
2230 sid_size = 0;
2231 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
2232 ok(!ret, "Expected 0, got %d\n", ret);
2233 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2234 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2235 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2236 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2237
2238 /* try cchReferencedDomainName - 1 */
2239 SetLastError(0xdeadbeef);
2240 domain_size--;
2241 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
2242 ok(!ret, "Expected 0, got %d\n", ret);
2243 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2244 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2245 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2246 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2247
2248 /* NULL ReferencedDomainName with zero domain name size */
2249 SetLastError(0xdeadbeef);
2250 domain_size = 0;
2251 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
2252 ok(!ret, "Expected 0, got %d\n", ret);
2253 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2254 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2255 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2256 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2257
2258 HeapFree(GetProcessHeap(), 0, psid);
2259 HeapFree(GetProcessHeap(), 0, domain);
2260
2261 /* get sizes for NULL account name */
2262 sid_size = 0;
2263 domain_size = 0;
2264 sid_use = 0xcafebabe;
2265 SetLastError(0xdeadbeef);
2266 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2267 if (!ret && GetLastError() == ERROR_NONE_MAPPED)
2268 win_skip("NULL account name doesn't work on NT4\n");
2269 else
2270 {
2271 ok(!ret, "Expected 0, got %d\n", ret);
2272 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2273 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2274 ok(sid_size != 0, "Expected non-zero sid size\n");
2275 ok(domain_size != 0, "Expected non-zero domain size\n");
2276 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
2277
2278 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2279 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2280
2281 /* try NULL account name */
2282 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
2283 get_sid_info(psid, &account, &sid_dom);
2284 ok(ret, "Failed to lookup account name\n");
2285 /* Using a fixed string will not work on different locales */
2286 ok(!lstrcmpA(account, domain),
2287 "Got %s for account and %s for domain, these should be the same\n",
2288 account, domain);
2289 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain, sid_use);
2290
2291 HeapFree(GetProcessHeap(), 0, psid);
2292 HeapFree(GetProcessHeap(), 0, domain);
2293 }
2294
2295 /* try an invalid account name */
2296 SetLastError(0xdeadbeef);
2297 sid_size = 0;
2298 domain_size = 0;
2299 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
2300 ok(!ret, "Expected 0, got %d\n", ret);
2301 ok(GetLastError() == ERROR_NONE_MAPPED ||
2302 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE),
2303 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2304 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2305 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2306
2307 /* try an invalid system name */
2308 SetLastError(0xdeadbeef);
2309 sid_size = 0;
2310 domain_size = 0;
2311 ret = LookupAccountNameA("deepthought", NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2312 ok(!ret, "Expected 0, got %d\n", ret);
2313 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
2314 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2315 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2316 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2317
2318 /* try with the computer name as the account name */
2319 domain_size = sizeof(computer_name);
2320 GetComputerNameA(computer_name, &domain_size);
2321 sid_size = 0;
2322 domain_size = 0;
2323 ret = LookupAccountNameA(NULL, computer_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2324 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
2325 GetLastError() == ERROR_NONE_MAPPED /* in a domain */ ||
2326 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE) ||
2327 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE)),
2328 "LookupAccountNameA failed: %d\n", GetLastError());
2329 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2330 {
2331 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2332 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2333 ret = LookupAccountNameA(NULL, computer_name, psid, &sid_size, domain, &domain_size, &sid_use);
2334 ok(ret, "LookupAccountNameA failed: %d\n", GetLastError());
2335 ok(sid_use == SidTypeDomain ||
2336 (sid_use == SidTypeUser && ! strcmp(computer_name, user_name)), "expected SidTypeDomain for %s, got %d\n", computer_name, sid_use);
2337 HeapFree(GetProcessHeap(), 0, domain);
2338 HeapFree(GetProcessHeap(), 0, psid);
2339 }
2340
2341 /* Well Known names */
2342 if (!pCreateWellKnownSid)
2343 {
2344 win_skip("CreateWellKnownSid not available\n");
2345 return;
2346 }
2347
2348 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2349 {
2350 skip("Non-English locale (skipping well known name creation tests)\n");
2351 return;
2352 }
2353
2354 check_wellknown_name("LocalService", WinLocalServiceSid);
2355 check_wellknown_name("Local Service", WinLocalServiceSid);
2356 /* 2 spaces */
2357 check_wellknown_name("Local Service", 0);
2358 check_wellknown_name("NetworkService", WinNetworkServiceSid);
2359 check_wellknown_name("Network Service", WinNetworkServiceSid);
2360
2361 /* example of some names where the spaces are not optional */
2362 check_wellknown_name("Terminal Server User", WinTerminalServerSid);
2363 check_wellknown_name("TerminalServer User", 0);
2364 check_wellknown_name("TerminalServerUser", 0);
2365 check_wellknown_name("Terminal ServerUser", 0);
2366
2367 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid);
2368 check_wellknown_name("enterprisedomain controllers", 0);
2369 check_wellknown_name("enterprise domaincontrollers", 0);
2370 check_wellknown_name("enterprisedomaincontrollers", 0);
2371
2372 /* case insensitivity */
2373 check_wellknown_name("lOCAlServICE", WinLocalServiceSid);
2374
2375 /* fully qualified account names */
2376 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid);
2377 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid);
2378 check_wellknown_name("nt authority test\\Network Service", 0);
2379 check_wellknown_name("Dummy\\Network Service", 0);
2380 check_wellknown_name("ntauthority\\Network Service", 0);
2381 }
2382
2383 static void test_security_descriptor(void)
2384 {
2385 SECURITY_DESCRIPTOR sd;
2386 char buf[8192];
2387 DWORD size;
2388 BOOL isDefault, isPresent, ret;
2389 PACL pacl;
2390 PSID psid;
2391
2392 SetLastError(0xdeadbeef);
2393 ret = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
2394 if (ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2395 {
2396 win_skip("InitializeSecurityDescriptor is not implemented\n");
2397 return;
2398 }
2399
2400 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2401 expect_eq(psid, NULL, PSID, "%p");
2402 expect_eq(isDefault, FALSE, BOOL, "%d");
2403 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
2404
2405 SetLastError(0xdeadbeef);
2406 size = 5;
2407 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
2408 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
2409 ok(size > 5, "Size not increased\n");
2410 if (size <= 8192)
2411 {
2412 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
2413 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2414 expect_eq(psid, NULL, PSID, "%p");
2415 expect_eq(isDefault, FALSE, BOOL, "%d");
2416 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorGroup failed\n");
2417 expect_eq(psid, NULL, PSID, "%p");
2418 expect_eq(isDefault, FALSE, BOOL, "%d");
2419 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorDacl failed\n");
2420 expect_eq(isPresent, TRUE, BOOL, "%d");
2421 expect_eq(psid, NULL, PSID, "%p");
2422 expect_eq(isDefault, FALSE, BOOL, "%d");
2423 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorSacl failed\n");
2424 expect_eq(isPresent, TRUE, BOOL, "%d");
2425 expect_eq(psid, NULL, PSID, "%p");
2426 expect_eq(isDefault, FALSE, BOOL, "%d");
2427 }
2428 }
2429
2430 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2431 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2432 static void test_granted_access(HANDLE handle, ACCESS_MASK access,
2433 ACCESS_MASK alt, int line)
2434 {
2435 OBJECT_BASIC_INFORMATION obj_info;
2436 NTSTATUS status;
2437
2438 if (!pNtQueryObject)
2439 {
2440 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
2441 return;
2442 }
2443
2444 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
2445 sizeof(obj_info), NULL );
2446 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
2447 if (alt)
2448 ok_(__FILE__, line)(obj_info.GrantedAccess == access ||
2449 obj_info.GrantedAccess == alt, "Granted access should be 0x%08x "
2450 "or 0x%08x, instead of 0x%08x\n", access, alt, obj_info.GrantedAccess);
2451 else
2452 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
2453 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
2454 }
2455
2456 #define CHECK_SET_SECURITY(o,i,e) \
2457 do{ \
2458 BOOL res_; \
2459 DWORD err; \
2460 SetLastError( 0xdeadbeef ); \
2461 res_ = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2462 err = GetLastError(); \
2463 if (e == ERROR_SUCCESS) \
2464 ok(res_, "SetKernelObjectSecurity failed with %d\n", err); \
2465 else \
2466 ok(!res_ && err == e, "SetKernelObjectSecurity should have failed " \
2467 "with %s, instead of %d\n", #e, err); \
2468 }while(0)
2469
2470 static void test_process_security(void)
2471 {
2472 BOOL res;
2473 PTOKEN_OWNER owner;
2474 PTOKEN_PRIMARY_GROUP group;
2475 PSID AdminSid = NULL, UsersSid = NULL;
2476 PACL Acl = NULL;
2477 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
2478 char buffer[MAX_PATH];
2479 PROCESS_INFORMATION info;
2480 STARTUPINFOA startup;
2481 SECURITY_ATTRIBUTES psa;
2482 HANDLE token, event;
2483 DWORD size;
2484
2485 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
2486 res = InitializeAcl(Acl, 256, ACL_REVISION);
2487 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2488 {
2489 win_skip("ACLs not implemented - skipping tests\n");
2490 HeapFree(GetProcessHeap(), 0, Acl);
2491 return;
2492 }
2493 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2494
2495 /* get owner from the token we might be running as a user not admin */
2496 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
2497 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
2498 if (!res)
2499 {
2500 HeapFree(GetProcessHeap(), 0, Acl);
2501 return;
2502 }
2503
2504 res = GetTokenInformation( token, TokenOwner, NULL, 0, &size );
2505 ok(!res, "Expected failure, got %d\n", res);
2506 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2507 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2508
2509 owner = HeapAlloc(GetProcessHeap(), 0, size);
2510 res = GetTokenInformation( token, TokenOwner, owner, size, &size );
2511 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2512 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
2513
2514 res = GetTokenInformation( token, TokenPrimaryGroup, NULL, 0, &size );
2515 ok(!res, "Expected failure, got %d\n", res);
2516 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2517 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2518
2519 group = HeapAlloc(GetProcessHeap(), 0, size);
2520 res = GetTokenInformation( token, TokenPrimaryGroup, group, size, &size );
2521 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2522 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
2523
2524 CloseHandle( token );
2525 if (!res)
2526 {
2527 HeapFree(GetProcessHeap(), 0, group);
2528 HeapFree(GetProcessHeap(), 0, owner);
2529 HeapFree(GetProcessHeap(), 0, Acl);
2530 return;
2531 }
2532
2533 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
2534 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2535 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
2536 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2537
2538 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
2539 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
2540 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2541
2542 event = CreateEventA( NULL, TRUE, TRUE, "test_event" );
2543 ok(event != NULL, "CreateEvent %d\n", GetLastError());
2544
2545 SecurityDescriptor->Revision = 0;
2546 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
2547 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
2548
2549 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2550 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2551 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
2552 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2553 /* NULL DACL is valid and means default DACL from token */
2554 SecurityDescriptor->Control |= SE_DACL_PRESENT;
2555 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2556
2557 /* Set owner and group and dacl */
2558 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
2559 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2560 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
2561 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
2562 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2563 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
2564 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
2565 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2566 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2567
2568 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
2569 memset(&startup, 0, sizeof(startup));
2570 startup.cb = sizeof(startup);
2571 startup.dwFlags = STARTF_USESHOWWINDOW;
2572 startup.wShowWindow = SW_SHOWNORMAL;
2573
2574 psa.nLength = sizeof(psa);
2575 psa.lpSecurityDescriptor = SecurityDescriptor;
2576 psa.bInheritHandle = TRUE;
2577
2578 /* Doesn't matter what ACL say we should get full access for ourselves */
2579 res = CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info );
2580 ok(res, "CreateProcess with err:%d\n", GetLastError());
2581 TEST_GRANTED_ACCESS2( info.hProcess, PROCESS_ALL_ACCESS_NT4,
2582 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2583 winetest_wait_child_process( info.hProcess );
2584
2585 CloseHandle( info.hProcess );
2586 CloseHandle( info.hThread );
2587 CloseHandle( event );
2588 HeapFree(GetProcessHeap(), 0, group);
2589 HeapFree(GetProcessHeap(), 0, owner);
2590 HeapFree(GetProcessHeap(), 0, Acl);
2591 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
2592 }
2593
2594 static void test_process_security_child(void)
2595 {
2596 HANDLE handle, handle1;
2597 BOOL ret;
2598 DWORD err;
2599
2600 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
2601 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2602 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
2603
2604 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2605 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS );
2606 ok(ret, "duplicating handle err:%d\n", GetLastError());
2607 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
2608
2609 CloseHandle( handle1 );
2610
2611 SetLastError( 0xdeadbeef );
2612 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2613 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
2614 err = GetLastError();
2615 todo_wine
2616 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
2617 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
2618
2619 CloseHandle( handle );
2620
2621 /* These two should fail - they are denied by ACL */
2622 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
2623 todo_wine
2624 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2625 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
2626 todo_wine
2627 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2628
2629 /* Documented privilege elevation */
2630 ret = DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2631 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS );
2632 ok(ret, "duplicating handle err:%d\n", GetLastError());
2633 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS_NT4,
2634 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2635
2636 CloseHandle( handle );
2637
2638 /* Same only explicitly asking for all access rights */
2639 ret = DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2640 &handle, PROCESS_ALL_ACCESS, TRUE, 0 );
2641 ok(ret, "duplicating handle err:%d\n", GetLastError());
2642 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS_NT4,
2643 PROCESS_ALL_ACCESS | PROCESS_QUERY_LIMITED_INFORMATION );
2644 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2645 &handle1, PROCESS_VM_READ, TRUE, 0 );
2646 ok(ret, "duplicating handle err:%d\n", GetLastError());
2647 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
2648 CloseHandle( handle1 );
2649 CloseHandle( handle );
2650 }
2651
2652 static void test_impersonation_level(void)
2653 {
2654 HANDLE Token, ProcessToken;
2655 HANDLE Token2;
2656 DWORD Size;
2657 TOKEN_PRIVILEGES *Privileges;
2658 TOKEN_USER *User;
2659 PRIVILEGE_SET *PrivilegeSet;
2660 BOOL AccessGranted;
2661 BOOL ret;
2662 HKEY hkey;
2663 DWORD error;
2664
2665 pDuplicateTokenEx = (void *)GetProcAddress(hmod, "DuplicateTokenEx");
2666 if( !pDuplicateTokenEx ) {
2667 win_skip("DuplicateTokenEx is not available\n");
2668 return;
2669 }
2670 SetLastError(0xdeadbeef);
2671 ret = ImpersonateSelf(SecurityAnonymous);
2672 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2673 {
2674 win_skip("ImpersonateSelf is not implemented\n");
2675 return;
2676 }
2677 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2678 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2679 ok(!ret, "OpenThreadToken should have failed\n");
2680 error = GetLastError();
2681 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2682 /* can't perform access check when opening object against an anonymous impersonation token */
2683 todo_wine {
2684 error = RegOpenKeyExA(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2685 ok(error == ERROR_INVALID_HANDLE || error == ERROR_CANT_OPEN_ANONYMOUS || error == ERROR_BAD_IMPERSONATION_LEVEL,
2686 "RegOpenKeyEx failed with %d\n", error);
2687 }
2688 RevertToSelf();
2689
2690 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
2691 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
2692
2693 ret = pDuplicateTokenEx(ProcessToken,
2694 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
2695 SecurityAnonymous, TokenImpersonation, &Token);
2696 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
2697 /* can't increase the impersonation level */
2698 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
2699 error = GetLastError();
2700 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
2701 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2702 /* we can query anything from an anonymous token, including the user */
2703 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
2704 error = GetLastError();
2705 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2706 User = HeapAlloc(GetProcessHeap(), 0, Size);
2707 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
2708 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2709 HeapFree(GetProcessHeap(), 0, User);
2710
2711 /* PrivilegeCheck fails with SecurityAnonymous level */
2712 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
2713 error = GetLastError();
2714 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2715 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
2716 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
2717 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2718
2719 PrivilegeSet = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
2720 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
2721 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
2722 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
2723 HeapFree(GetProcessHeap(), 0, Privileges);
2724
2725 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2726 error = GetLastError();
2727 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2728
2729 CloseHandle(Token);
2730
2731 ret = ImpersonateSelf(SecurityIdentification);
2732 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2733 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2734 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2735
2736 /* can't perform access check when opening object against an identification impersonation token */
2737 error = RegOpenKeyExA(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2738 todo_wine {
2739 ok(error == ERROR_INVALID_HANDLE || error == ERROR_BAD_IMPERSONATION_LEVEL,
2740 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2741 }
2742 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2743 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2744 CloseHandle(Token);
2745 RevertToSelf();
2746
2747 ret = ImpersonateSelf(SecurityImpersonation);
2748 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2749 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2750 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2751 error = RegOpenKeyExA(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2752 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
2753 RegCloseKey(hkey);
2754 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2755 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2756 RevertToSelf();
2757
2758 CloseHandle(Token);
2759 CloseHandle(ProcessToken);
2760
2761 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
2762 }
2763
2764 static void test_SetEntriesInAclW(void)
2765 {
2766 DWORD res;
2767 PSID EveryoneSid = NULL, UsersSid = NULL;
2768 PACL OldAcl = NULL, NewAcl;
2769 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2770 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2771 EXPLICIT_ACCESSW ExplicitAccess;
2772 static const WCHAR wszEveryone[] = {'E','v','e','r','y','o','n','e',0};
2773 static const WCHAR wszCurrentUser[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2774
2775 if (!pSetEntriesInAclW)
2776 {
2777 win_skip("SetEntriesInAclW is not available\n");
2778 return;
2779 }
2780
2781 NewAcl = (PACL)0xdeadbeef;
2782 res = pSetEntriesInAclW(0, NULL, NULL, &NewAcl);
2783 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2784 {
2785 win_skip("SetEntriesInAclW is not implemented\n");
2786 return;
2787 }
2788 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2789 ok(NewAcl == NULL ||
2790 broken(NewAcl != NULL), /* NT4 */
2791 "NewAcl=%p, expected NULL\n", NewAcl);
2792 LocalFree(NewAcl);
2793
2794 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2795 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2796 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2797 {
2798 win_skip("ACLs not implemented - skipping tests\n");
2799 HeapFree(GetProcessHeap(), 0, OldAcl);
2800 return;
2801 }
2802 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2803
2804 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2805 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2806
2807 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2808 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2809 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2810
2811 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2812 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2813
2814 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2815 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2816 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2817 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
2818 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2819 ExplicitAccess.Trustee.ptstrName = EveryoneSid;
2820 ExplicitAccess.Trustee.MultipleTrusteeOperation = 0xDEADBEEF;
2821 ExplicitAccess.Trustee.pMultipleTrustee = (PVOID)0xDEADBEEF;
2822 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2823 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2824 ok(NewAcl != NULL, "returned acl was NULL\n");
2825 LocalFree(NewAcl);
2826
2827 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2828 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2829 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2830 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2831 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2832 ok(NewAcl != NULL, "returned acl was NULL\n");
2833 LocalFree(NewAcl);
2834
2835 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2836 {
2837 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2838 }
2839 else
2840 {
2841 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2842 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszEveryone;
2843 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2844 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2845 ok(NewAcl != NULL, "returned acl was NULL\n");
2846 LocalFree(NewAcl);
2847
2848 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_BAD_FORM;
2849 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2850 ok(res == ERROR_INVALID_PARAMETER ||
2851 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2852 "SetEntriesInAclW failed: %u\n", res);
2853 ok(NewAcl == NULL ||
2854 broken(NewAcl != NULL), /* NT4 */
2855 "returned acl wasn't NULL: %p\n", NewAcl);
2856
2857 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2858 ExplicitAccess.Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATE;
2859 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2860 ok(res == ERROR_INVALID_PARAMETER ||
2861 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2862 "SetEntriesInAclW failed: %u\n", res);
2863 ok(NewAcl == NULL ||
2864 broken(NewAcl != NULL), /* NT4 */
2865 "returned acl wasn't NULL: %p\n", NewAcl);
2866
2867 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2868 ExplicitAccess.grfAccessMode = SET_ACCESS;
2869 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2870 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2871 ok(NewAcl != NULL, "returned acl was NULL\n");
2872 LocalFree(NewAcl);
2873 }
2874
2875 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2876 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszCurrentUser;
2877 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2878 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2879 ok(NewAcl != NULL, "returned acl was NULL\n");
2880 LocalFree(NewAcl);
2881
2882 ExplicitAccess.grfAccessMode = REVOKE_ACCESS;
2883 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2884 ExplicitAccess.Trustee.ptstrName = UsersSid;
2885 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2886 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2887 ok(NewAcl != NULL, "returned acl was NULL\n");
2888 LocalFree(NewAcl);
2889
2890 FreeSid(UsersSid);
2891 FreeSid(EveryoneSid);
2892 HeapFree(GetProcessHeap(), 0, OldAcl);
2893 }
2894
2895 static void test_SetEntriesInAclA(void)
2896 {
2897 DWORD res;
2898 PSID EveryoneSid = NULL, UsersSid = NULL;
2899 PACL OldAcl = NULL, NewAcl;
2900 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2901 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2902 EXPLICIT_ACCESSA ExplicitAccess;
2903 static const CHAR szEveryone[] = {'E','v','e','r','y','o','n','e',0};
2904 static const CHAR szCurrentUser[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2905
2906 if (!pSetEntriesInAclA)
2907 {
2908 win_skip("SetEntriesInAclA is not available\n");
2909 return;
2910 }
2911
2912 NewAcl = (PACL)0xdeadbeef;
2913 res = pSetEntriesInAclA(0, NULL, NULL, &NewAcl);
2914 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2915 {
2916 win_skip("SetEntriesInAclA is not implemented\n");
2917 return;
2918 }
2919 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2920 ok(NewAcl == NULL ||
2921 broken(NewAcl != NULL), /* NT4 */
2922 "NewAcl=%p, expected NULL\n", NewAcl);
2923 LocalFree(NewAcl);
2924
2925 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2926 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2927 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2928 {
2929 win_skip("ACLs not implemented - skipping tests\n");
2930 HeapFree(GetProcessHeap(), 0, OldAcl);
2931 return;
2932 }
2933 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2934
2935 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2936 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2937
2938 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2939 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2940 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2941
2942 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2943 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2944
2945 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2946 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2947 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2948 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
2949 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2950 ExplicitAccess.Trustee.ptstrName = EveryoneSid;
2951 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2952 ExplicitAccess.Trustee<