2 * Unit tests for security functions
4 * Copyright (c) 2004 Mike McCormack
5 * Copyright (c) 2011 Dmitry Timoshkov
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.
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.
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
26 #define WIN32_NO_STATUS
27 #define WIN32_LEAN_AND_MEAN
39 #include "wine/test.h"
41 #ifndef PROCESS_QUERY_LIMITED_INFORMATION
42 #define PROCESS_QUERY_LIMITED_INFORMATION 0x1000
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)
49 #ifndef EVENT_QUERY_STATE
50 #define EVENT_QUERY_STATE 0x0001
53 #ifndef SEMAPHORE_QUERY_STATE
54 #define SEMAPHORE_QUERY_STATE 0x0001
57 #ifndef THREAD_SET_LIMITED_INFORMATION
58 #define THREAD_SET_LIMITED_INFORMATION 0x0400
59 #define THREAD_QUERY_LIMITED_INFORMATION 0x0800
62 #define THREAD_ALL_ACCESS_NT4 (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3ff)
63 #define THREAD_ALL_ACCESS_VISTA (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xffff)
65 /* copied from Wine winternl.h - not included in the Windows SDK */
66 typedef enum _OBJECT_INFORMATION_CLASS
{
67 ObjectBasicInformation
,
68 ObjectNameInformation
,
69 ObjectTypeInformation
,
72 } OBJECT_INFORMATION_CLASS
, *POBJECT_INFORMATION_CLASS
;
74 typedef struct _OBJECT_BASIC_INFORMATION
{
76 ACCESS_MASK GrantedAccess
;
80 ULONG NonPagedPoolUsage
;
82 ULONG NameInformationLength
;
83 ULONG TypeInformationLength
;
84 ULONG SecurityDescriptorLength
;
85 LARGE_INTEGER CreateTime
;
86 } OBJECT_BASIC_INFORMATION
, *POBJECT_BASIC_INFORMATION
;
88 #define expect_eq(expr, value, type, format) { type ret_ = expr; ok((value) == ret_, #expr " expected " format " got " format "\n", (value), (ret_)); }
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
,
99 LPSTR InheritedObjectTypeName
,
101 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndSidA
)( PTRUSTEEA pTrustee
,
102 POBJECTS_AND_SID pObjSid
,
104 GUID
* pInheritedObjectGuid
,
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
);
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
*);
156 static char** myARGV
;
168 SID_IDENTIFIER_AUTHORITY auth
;
172 static void init(void)
176 hntdll
= GetModuleHandleA("ntdll.dll");
177 pNtQueryObject
= (void *)GetProcAddress( hntdll
, "NtQueryObject" );
178 pNtAccessCheck
= (void *)GetProcAddress( hntdll
, "NtAccessCheck" );
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");
211 myARGC
= winetest_get_mainargs( &myARGV
);
214 static SECURITY_DESCRIPTOR
* test_get_security_descriptor(HANDLE handle
, int line
)
216 /* use HeapFree(GetProcessHeap(), 0, sd); when done */
217 DWORD ret
, length
, needed
;
218 SECURITY_DESCRIPTOR
*sd
;
221 SetLastError(0xdeadbeef);
222 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
224 ok_(__FILE__
, line
)(!ret
, "GetKernelObjectSecurity should fail\n");
225 ok_(__FILE__
, line
)(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
226 ok_(__FILE__
, line
)(needed
!= 0xdeadbeef, "GetKernelObjectSecurity should return required buffer length\n");
229 sd
= HeapAlloc(GetProcessHeap(), 0, length
);
232 SetLastError(0xdeadbeef);
233 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
234 sd
, length
, &needed
);
235 ok_(__FILE__
, line
)(ret
, "GetKernelObjectSecurity error %d\n", GetLastError());
236 ok_(__FILE__
, line
)(needed
== length
|| needed
== 0 /* file, pipe */, "GetKernelObjectSecurity should return %u instead of %u\n", length
, needed
);
240 static void test_owner_equal(HANDLE Handle
, PSID expected
, int line
)
243 SECURITY_DESCRIPTOR
*queriedSD
= NULL
;
245 BOOL owner_defaulted
;
247 queriedSD
= test_get_security_descriptor( Handle
, line
);
249 res
= GetSecurityDescriptorOwner(queriedSD
, &owner
, &owner_defaulted
);
250 ok_(__FILE__
, line
)(res
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
252 ok_(__FILE__
, line
)(EqualSid(owner
, expected
), "Owner SIDs are not equal\n");
253 ok_(__FILE__
, line
)(!owner_defaulted
, "Defaulted is true\n");
255 HeapFree(GetProcessHeap(), 0, queriedSD
);
258 static void test_group_equal(HANDLE Handle
, PSID expected
, int line
)
261 SECURITY_DESCRIPTOR
*queriedSD
= NULL
;
263 BOOL group_defaulted
;
265 queriedSD
= test_get_security_descriptor( Handle
, line
);
267 res
= GetSecurityDescriptorGroup(queriedSD
, &group
, &group_defaulted
);
268 ok_(__FILE__
, line
)(res
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
270 ok_(__FILE__
, line
)(EqualSid(group
, expected
), "Group SIDs are not equal\n");
271 ok_(__FILE__
, line
)(!group_defaulted
, "Defaulted is true\n");
273 HeapFree(GetProcessHeap(), 0, queriedSD
);
276 static void test_sid(void)
278 struct sidRef refs
[] = {
279 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
280 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
281 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
282 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
283 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
284 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
286 struct strsid_entry strsid_table
[] = {
287 {"AO", STRSID_OK
}, {"RU", STRSID_OK
}, {"AN", STRSID_OK
}, {"AU", STRSID_OK
},
288 {"BA", STRSID_OK
}, {"BG", STRSID_OK
}, {"BO", STRSID_OK
}, {"BU", STRSID_OK
},
289 {"CA", STRSID_OPT
}, {"CG", STRSID_OK
}, {"CO", STRSID_OK
}, {"DA", STRSID_OPT
},
290 {"DC", STRSID_OPT
}, {"DD", STRSID_OPT
}, {"DG", STRSID_OPT
}, {"DU", STRSID_OPT
},
291 {"EA", STRSID_OPT
}, {"ED", STRSID_OK
}, {"WD", STRSID_OK
}, {"PA", STRSID_OPT
},
292 {"IU", STRSID_OK
}, {"LA", STRSID_OK
}, {"LG", STRSID_OK
}, {"LS", STRSID_OK
},
293 {"SY", STRSID_OK
}, {"NU", STRSID_OK
}, {"NO", STRSID_OK
}, {"NS", STRSID_OK
},
294 {"PO", STRSID_OK
}, {"PS", STRSID_OK
}, {"PU", STRSID_OK
}, {"RS", STRSID_OPT
},
295 {"RD", STRSID_OK
}, {"RE", STRSID_OK
}, {"RC", STRSID_OK
}, {"SA", STRSID_OPT
},
296 {"SO", STRSID_OK
}, {"SU", STRSID_OK
}};
298 const char noSubAuthStr
[] = "S-1-5";
305 if( !pConvertSidToStringSidA
|| !pConvertStringSidToSidA
)
307 win_skip("ConvertSidToStringSidA or ConvertStringSidToSidA not available\n");
311 r
= pConvertStringSidToSidA( NULL
, NULL
);
312 ok( !r
, "expected failure with NULL parameters\n" );
313 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
315 ok( GetLastError() == ERROR_INVALID_PARAMETER
,
316 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
319 r
= pConvertStringSidToSidA( refs
[0].refStr
, NULL
);
320 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
321 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
324 r
= pConvertStringSidToSidA( NULL
, &str
);
325 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
326 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
329 r
= pConvertStringSidToSidA( noSubAuthStr
, &psid
);
331 "expected failure with no sub authorities\n" );
332 ok( GetLastError() == ERROR_INVALID_SID
,
333 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
336 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
), "ConvertStringSidToSidA failed\n");
338 ok(pisid
->SubAuthorityCount
== 4, "Invalid sub authority count - expected 4, got %d\n", pisid
->SubAuthorityCount
);
339 ok(pisid
->SubAuthority
[0] == 21, "Invalid subauthority 0 - expected 21, got %d\n", pisid
->SubAuthority
[0]);
340 ok(pisid
->SubAuthority
[3] == 4576, "Invalid subauthority 0 - expected 4576, got %d\n", pisid
->SubAuthority
[3]);
344 for( i
= 0; i
< sizeof(refs
) / sizeof(refs
[0]); i
++ )
346 r
= AllocateAndInitializeSid( &refs
[i
].auth
, 1,1,0,0,0,0,0,0,0,
348 ok( r
, "failed to allocate sid\n" );
349 r
= pConvertSidToStringSidA( psid
, &str
);
350 ok( r
, "failed to convert sid\n" );
353 ok( !strcmp( str
, refs
[i
].refStr
),
354 "incorrect sid, expected %s, got %s\n", refs
[i
].refStr
, str
);
360 r
= pConvertStringSidToSidA( refs
[i
].refStr
, &psid
);
361 ok( r
, "failed to parse sid string\n" );
364 !memcmp( pisid
->IdentifierAuthority
.Value
, refs
[i
].auth
.Value
,
365 sizeof(refs
[i
].auth
) ),
366 "string sid %s didn't parse to expected value\n"
367 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
369 MAKEWORD( pisid
->IdentifierAuthority
.Value
[1],
370 pisid
->IdentifierAuthority
.Value
[0] ),
371 MAKELONG( MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
372 pisid
->IdentifierAuthority
.Value
[4] ),
373 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
374 pisid
->IdentifierAuthority
.Value
[2] ) ),
375 MAKEWORD( refs
[i
].auth
.Value
[1], refs
[i
].auth
.Value
[0] ),
376 MAKELONG( MAKEWORD( refs
[i
].auth
.Value
[5], refs
[i
].auth
.Value
[4] ),
377 MAKEWORD( refs
[i
].auth
.Value
[3], refs
[i
].auth
.Value
[2] ) ) );
382 /* string constant format not supported before XP */
383 r
= pConvertStringSidToSidA(strsid_table
[0].str
, &psid
);
386 win_skip("String constant format not supported\n");
391 for(i
= 0; i
< sizeof(strsid_table
) / sizeof(strsid_table
[0]); i
++)
395 SetLastError(0xdeadbeef);
396 r
= pConvertStringSidToSidA(strsid_table
[i
].str
, &psid
);
398 if (!(strsid_table
[i
].flags
& STRSID_OPT
))
400 ok(r
, "%s: got %u\n", strsid_table
[i
].str
, GetLastError());
405 if ((winetest_debug
> 1) && (pConvertSidToStringSidA(psid
, &temp
)))
407 trace(" %s: %s\n", strsid_table
[i
].str
, temp
);
414 if (GetLastError() != ERROR_INVALID_SID
)
415 trace(" %s: couldn't be converted, returned %d\n", strsid_table
[i
].str
, GetLastError());
417 trace(" %s: couldn't be converted\n", strsid_table
[i
].str
);
422 static void test_trustee(void)
424 GUID ObjectType
= {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
425 GUID InheritedObjectType
= {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
427 OBJECTS_AND_NAME_A oan
;
431 char szObjectTypeName
[] = "ObjectTypeName";
432 char szInheritedObjectTypeName
[] = "InheritedObjectTypeName";
433 char szTrusteeName
[] = "szTrusteeName";
434 SID_IDENTIFIER_AUTHORITY auth
= { {0x11,0x22,0,0,0, 0} };
436 memset( &ZeroGuid
, 0x00, sizeof (ZeroGuid
) );
438 pBuildTrusteeWithSidA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithSidA" );
439 pBuildTrusteeWithNameA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithNameA" );
440 pBuildTrusteeWithObjectsAndNameA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndNameA" );
441 pBuildTrusteeWithObjectsAndSidA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndSidA" );
442 pGetTrusteeNameA
= (void *)GetProcAddress (hmod
, "GetTrusteeNameA" );
443 if( !pBuildTrusteeWithSidA
|| !pBuildTrusteeWithNameA
||
444 !pBuildTrusteeWithObjectsAndNameA
|| !pBuildTrusteeWithObjectsAndSidA
||
448 if ( ! AllocateAndInitializeSid( &auth
, 1, 42, 0,0,0,0,0,0,0,&psid
) )
450 trace( "failed to init SID\n" );
454 /* test BuildTrusteeWithSidA */
455 memset( &trustee
, 0xff, sizeof trustee
);
456 pBuildTrusteeWithSidA( &trustee
, psid
);
458 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
459 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
460 "MultipleTrusteeOperation wrong\n");
461 ok( trustee
.TrusteeForm
== TRUSTEE_IS_SID
, "TrusteeForm wrong\n");
462 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
463 ok( trustee
.ptstrName
== psid
, "ptstrName wrong\n" );
465 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
466 memset( &trustee
, 0xff, sizeof trustee
);
467 memset( &oas
, 0xff, sizeof(oas
) );
468 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, &ObjectType
,
469 &InheritedObjectType
, psid
);
471 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
472 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
473 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
474 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
475 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
477 ok(oas
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
478 ok(!memcmp(&oas
.ObjectTypeGuid
, &ObjectType
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
479 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
480 ok(oas
.pSid
== psid
, "pSid wrong\n");
482 /* test GetTrusteeNameA */
483 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oas
, "GetTrusteeName returned wrong value\n");
485 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
486 memset( &trustee
, 0xff, sizeof trustee
);
487 memset( &oas
, 0xff, sizeof(oas
) );
488 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, NULL
,
489 &InheritedObjectType
, psid
);
491 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
492 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
493 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
494 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
495 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
497 ok(oas
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
498 ok(!memcmp(&oas
.ObjectTypeGuid
, &ZeroGuid
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
499 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
500 ok(oas
.pSid
== psid
, "pSid wrong\n");
504 /* test BuildTrusteeWithNameA */
505 memset( &trustee
, 0xff, sizeof trustee
);
506 pBuildTrusteeWithNameA( &trustee
, szTrusteeName
);
508 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
509 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
510 "MultipleTrusteeOperation wrong\n");
511 ok( trustee
.TrusteeForm
== TRUSTEE_IS_NAME
, "TrusteeForm wrong\n");
512 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
513 ok( trustee
.ptstrName
== szTrusteeName
, "ptstrName wrong\n" );
515 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
516 memset( &trustee
, 0xff, sizeof trustee
);
517 memset( &oan
, 0xff, sizeof(oan
) );
518 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
519 szInheritedObjectTypeName
, szTrusteeName
);
521 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
522 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
523 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
524 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
525 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
527 ok(oan
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
528 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
529 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
530 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
532 /* test GetTrusteeNameA */
533 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oan
, "GetTrusteeName returned wrong value\n");
535 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
536 memset( &trustee
, 0xff, sizeof trustee
);
537 memset( &oan
, 0xff, sizeof(oan
) );
538 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, NULL
,
539 szInheritedObjectTypeName
, szTrusteeName
);
541 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
542 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
543 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
544 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
545 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
547 ok(oan
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
548 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
549 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
550 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
552 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
553 memset( &trustee
, 0xff, sizeof trustee
);
554 memset( &oan
, 0xff, sizeof(oan
) );
555 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
556 NULL
, szTrusteeName
);
558 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
559 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
560 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
561 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
562 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
564 ok(oan
.ObjectsPresent
== ACE_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
565 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
566 ok(oan
.InheritedObjectTypeName
== NULL
, "InheritedObjectTypeName wrong\n");
567 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
570 /* If the first isn't defined, assume none is */
571 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
572 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
573 #define SE_CREATE_TOKEN_PRIVILEGE 2L
574 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
575 #define SE_LOCK_MEMORY_PRIVILEGE 4L
576 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
577 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
578 #define SE_TCB_PRIVILEGE 7L
579 #define SE_SECURITY_PRIVILEGE 8L
580 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
581 #define SE_LOAD_DRIVER_PRIVILEGE 10L
582 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
583 #define SE_SYSTEMTIME_PRIVILEGE 12L
584 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
585 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
586 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
587 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
588 #define SE_BACKUP_PRIVILEGE 17L
589 #define SE_RESTORE_PRIVILEGE 18L
590 #define SE_SHUTDOWN_PRIVILEGE 19L
591 #define SE_DEBUG_PRIVILEGE 20L
592 #define SE_AUDIT_PRIVILEGE 21L
593 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
594 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
595 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
596 #define SE_UNDOCK_PRIVILEGE 25L
597 #define SE_SYNC_AGENT_PRIVILEGE 26L
598 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
599 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
600 #define SE_IMPERSONATE_PRIVILEGE 29L
601 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
602 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
603 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
605 static void test_allocateLuid(void)
607 BOOL (WINAPI
*pAllocateLocallyUniqueId
)(PLUID
);
611 pAllocateLocallyUniqueId
= (void*)GetProcAddress(hmod
, "AllocateLocallyUniqueId");
612 if (!pAllocateLocallyUniqueId
) return;
614 ret
= pAllocateLocallyUniqueId(&luid1
);
615 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
619 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
620 ret
= pAllocateLocallyUniqueId(&luid2
);
622 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
623 ok(luid1
.LowPart
> SE_MAX_WELL_KNOWN_PRIVILEGE
|| luid1
.HighPart
!= 0,
624 "AllocateLocallyUniqueId returned a well-known LUID\n");
625 ok(luid1
.LowPart
!= luid2
.LowPart
|| luid1
.HighPart
!= luid2
.HighPart
,
626 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
627 ret
= pAllocateLocallyUniqueId(NULL
);
628 ok( !ret
&& GetLastError() == ERROR_NOACCESS
,
629 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
633 static void test_lookupPrivilegeName(void)
635 BOOL (WINAPI
*pLookupPrivilegeNameA
)(LPCSTR
, PLUID
, LPSTR
, LPDWORD
);
636 char buf
[MAX_PATH
]; /* arbitrary, seems long enough */
637 DWORD cchName
= sizeof(buf
);
638 LUID luid
= { 0, 0 };
642 /* check whether it's available first */
643 pLookupPrivilegeNameA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeNameA");
644 if (!pLookupPrivilegeNameA
) return;
645 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
646 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
647 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
650 /* check with a short buffer */
652 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
653 ret
= pLookupPrivilegeNameA(NULL
, &luid
, NULL
, &cchName
);
654 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
655 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
657 ok(cchName
== strlen("SeCreateTokenPrivilege") + 1,
658 "LookupPrivilegeNameA returned an incorrect required length for\n"
659 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
660 lstrlenA("SeCreateTokenPrivilege") + 1);
661 /* check a known value and its returned length on success */
662 cchName
= sizeof(buf
);
663 ok(pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
) &&
664 cchName
== strlen("SeCreateTokenPrivilege"),
665 "LookupPrivilegeNameA returned an incorrect output length for\n"
666 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
667 (int)strlen("SeCreateTokenPrivilege"));
668 /* check known values */
669 for (i
= SE_MIN_WELL_KNOWN_PRIVILEGE
; i
<= SE_MAX_WELL_KNOWN_PRIVILEGE
; i
++)
672 cchName
= sizeof(buf
);
673 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
674 ok( ret
|| GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
675 "LookupPrivilegeNameA(0.%d) failed: %d\n", i
, GetLastError());
677 /* check a bogus LUID */
678 luid
.LowPart
= 0xdeadbeef;
679 cchName
= sizeof(buf
);
680 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
681 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
682 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
684 /* check on a bogus system */
685 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
686 cchName
= sizeof(buf
);
687 ret
= pLookupPrivilegeNameA("b0gu5.Nam3", &luid
, buf
, &cchName
);
688 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
689 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
690 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
700 static void test_lookupPrivilegeValue(void)
702 static const struct NameToLUID privs
[] = {
703 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE
},
704 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE
},
705 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE
},
706 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE
},
707 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE
},
708 { "SeTcbPrivilege", SE_TCB_PRIVILEGE
},
709 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE
},
710 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE
},
711 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE
},
712 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE
},
713 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE
},
714 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE
},
715 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE
},
716 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE
},
717 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE
},
718 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE
},
719 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE
},
720 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE
},
721 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE
},
722 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE
},
723 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE
},
724 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE
},
725 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE
},
726 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE
},
727 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE
},
728 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE
},
729 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE
},
730 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE
},
731 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE
},
733 BOOL (WINAPI
*pLookupPrivilegeValueA
)(LPCSTR
, LPCSTR
, PLUID
);
738 /* check whether it's available first */
739 pLookupPrivilegeValueA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeValueA");
740 if (!pLookupPrivilegeValueA
) return;
741 ret
= pLookupPrivilegeValueA(NULL
, "SeCreateTokenPrivilege", &luid
);
742 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
745 /* check a bogus system name */
746 ret
= pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid
);
747 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
748 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
749 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
751 /* check a NULL string */
752 ret
= pLookupPrivilegeValueA(NULL
, 0, &luid
);
753 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
754 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
756 /* check a bogus privilege name */
757 ret
= pLookupPrivilegeValueA(NULL
, "SeBogusPrivilege", &luid
);
758 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
759 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
761 /* check case insensitive */
762 ret
= pLookupPrivilegeValueA(NULL
, "sEcREATEtOKENpRIVILEGE", &luid
);
764 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
766 for (i
= 0; i
< sizeof(privs
) / sizeof(privs
[0]); i
++)
768 /* Not all privileges are implemented on all Windows versions, so
769 * don't worry if the call fails
771 if (pLookupPrivilegeValueA(NULL
, privs
[i
].name
, &luid
))
773 ok(luid
.LowPart
== privs
[i
].lowPart
,
774 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
780 static void test_luid(void)
783 test_lookupPrivilegeName();
784 test_lookupPrivilegeValue();
787 static void test_FileSecurity(void)
789 char wintmpdir
[MAX_PATH
];
790 char path
[MAX_PATH
];
791 char file
[MAX_PATH
];
793 DWORD sdSize
, retSize
, rc
, granted
, priv_set_len
;
794 PRIVILEGE_SET priv_set
;
797 GENERIC_MAPPING mapping
= { FILE_READ_DATA
, FILE_WRITE_DATA
, FILE_EXECUTE
, FILE_ALL_ACCESS
};
798 const SECURITY_INFORMATION request
= OWNER_SECURITY_INFORMATION
799 | GROUP_SECURITY_INFORMATION
800 | DACL_SECURITY_INFORMATION
;
802 if (!pGetFileSecurityA
) {
803 win_skip ("GetFileSecurity is not available\n");
807 if (!pSetFileSecurityA
) {
808 win_skip ("SetFileSecurity is not available\n");
812 if (!GetTempPathA (sizeof (wintmpdir
), wintmpdir
)) {
813 win_skip ("GetTempPathA failed\n");
817 /* Create a temporary directory and in it a temporary file */
818 strcat (strcpy (path
, wintmpdir
), "rary");
819 SetLastError(0xdeadbeef);
820 rc
= CreateDirectoryA (path
, NULL
);
821 ok (rc
|| GetLastError() == ERROR_ALREADY_EXISTS
, "CreateDirectoryA "
822 "failed for '%s' with %d\n", path
, GetLastError());
824 strcat (strcpy (file
, path
), "\\ess");
825 SetLastError(0xdeadbeef);
826 fh
= CreateFileA (file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
827 ok (fh
!= INVALID_HANDLE_VALUE
, "CreateFileA "
828 "failed for '%s' with %d\n", file
, GetLastError());
831 /* For the temporary file ... */
833 /* Get size needed */
835 SetLastError(0xdeadbeef);
836 rc
= pGetFileSecurityA (file
, request
, NULL
, 0, &retSize
);
837 if (!rc
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) {
838 win_skip("GetFileSecurityA is not implemented\n");
841 ok (!rc
, "GetFileSecurityA "
842 "was expected to fail for '%s'\n", file
);
843 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
844 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
845 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
848 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
850 /* Get security descriptor for real */
852 SetLastError(0xdeadbeef);
853 rc
= pGetFileSecurityA (file
, request
, sd
, sdSize
, &retSize
);
854 ok (rc
, "GetFileSecurityA "
855 "was not expected to fail '%s': %d\n", file
, GetLastError());
856 ok (retSize
== sdSize
||
857 broken(retSize
== 0), /* NT4 */
858 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
860 /* Use it to set security descriptor */
861 SetLastError(0xdeadbeef);
862 rc
= pSetFileSecurityA (file
, request
, sd
);
863 ok (rc
, "SetFileSecurityA "
864 "was not expected to fail '%s': %d\n", file
, GetLastError());
866 HeapFree (GetProcessHeap (), 0, sd
);
868 /* Repeat for the temporary directory ... */
870 /* Get size needed */
872 SetLastError(0xdeadbeef);
873 rc
= pGetFileSecurityA (path
, request
, NULL
, 0, &retSize
);
874 ok (!rc
, "GetFileSecurityA "
875 "was expected to fail for '%s'\n", path
);
876 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
877 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
878 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
881 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
883 /* Get security descriptor for real */
885 SetLastError(0xdeadbeef);
886 rc
= pGetFileSecurityA (path
, request
, sd
, sdSize
, &retSize
);
887 ok (rc
, "GetFileSecurityA "
888 "was not expected to fail '%s': %d\n", path
, GetLastError());
889 ok (retSize
== sdSize
||
890 broken(retSize
== 0), /* NT4 */
891 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
893 /* Use it to set security descriptor */
894 SetLastError(0xdeadbeef);
895 rc
= pSetFileSecurityA (path
, request
, sd
);
896 ok (rc
, "SetFileSecurityA "
897 "was not expected to fail '%s': %d\n", path
, GetLastError());
898 HeapFree (GetProcessHeap (), 0, sd
);
901 strcpy (wintmpdir
, "\\Should not exist");
902 SetLastError(0xdeadbeef);
903 rc
= pGetFileSecurityA (wintmpdir
, OWNER_SECURITY_INFORMATION
, NULL
, 0, &sdSize
);
904 ok (!rc
, "GetFileSecurityA should fail for not existing directories/files\n");
905 ok (GetLastError() == ERROR_FILE_NOT_FOUND
,
906 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
909 /* Remove temporary file and directory */
911 RemoveDirectoryA(path
);
913 /* Test file access permissions for a file with FILE_ATTRIBUTE_ARCHIVE */
914 SetLastError(0xdeadbeef);
915 rc
= GetTempPathA(sizeof(wintmpdir
), wintmpdir
);
916 ok(rc
, "GetTempPath error %d\n", GetLastError());
918 SetLastError(0xdeadbeef);
919 rc
= GetTempFileNameA(wintmpdir
, "tmp", 0, file
);
920 ok(rc
, "GetTempFileName error %d\n", GetLastError());
922 rc
= GetFileAttributesA(file
);
923 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
924 ok(rc
== FILE_ATTRIBUTE_ARCHIVE
, "expected FILE_ATTRIBUTE_ARCHIVE got %#x\n", rc
);
926 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
928 ok(!rc
, "GetFileSecurity should fail\n");
929 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
930 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
931 ok(sdSize
> sizeof(SECURITY_DESCRIPTOR
), "got sd size %d\n", sdSize
);
933 sd
= HeapAlloc(GetProcessHeap (), 0, sdSize
);
934 retSize
= 0xdeadbeef;
935 SetLastError(0xdeadbeef);
936 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
937 sd
, sdSize
, &retSize
);
938 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
939 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
941 SetLastError(0xdeadbeef);
942 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
943 ok(!rc
, "OpenThreadToken should fail\n");
944 ok(GetLastError() == ERROR_NO_TOKEN
, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
946 SetLastError(0xdeadbeef);
947 rc
= ImpersonateSelf(SecurityIdentification
);
948 ok(rc
, "ImpersonateSelf error %d\n", GetLastError());
950 SetLastError(0xdeadbeef);
951 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
952 ok(rc
, "OpenThreadToken error %d\n", GetLastError());
954 SetLastError(0xdeadbeef);
956 ok(rc
, "RevertToSelf error %d\n", GetLastError());
958 priv_set_len
= sizeof(priv_set
);
959 granted
= 0xdeadbeef;
961 SetLastError(0xdeadbeef);
962 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
963 ok(rc
, "AccessCheck error %d\n", GetLastError());
964 ok(status
== 1, "expected 1, got %d\n", status
);
965 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
967 granted
= 0xdeadbeef;
969 SetLastError(0xdeadbeef);
970 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
971 ok(rc
, "AccessCheck error %d\n", GetLastError());
972 ok(status
== 1, "expected 1, got %d\n", status
);
973 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
975 granted
= 0xdeadbeef;
977 SetLastError(0xdeadbeef);
978 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
979 ok(rc
, "AccessCheck error %d\n", GetLastError());
980 ok(status
== 1, "expected 1, got %d\n", status
);
981 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
983 granted
= 0xdeadbeef;
985 SetLastError(0xdeadbeef);
986 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
987 ok(rc
, "AccessCheck error %d\n", GetLastError());
988 ok(status
== 1, "expected 1, got %d\n", status
);
989 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
991 granted
= 0xdeadbeef;
993 SetLastError(0xdeadbeef);
994 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
995 ok(rc
, "AccessCheck error %d\n", GetLastError());
996 ok(status
== 1, "expected 1, got %d\n", status
);
997 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
999 granted
= 0xdeadbeef;
1000 status
= 0xdeadbeef;
1001 SetLastError(0xdeadbeef);
1002 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1003 ok(rc
, "AccessCheck error %d\n", GetLastError());
1004 ok(status
== 1, "expected 1, got %d\n", status
);
1005 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
1007 granted
= 0xdeadbeef;
1008 status
= 0xdeadbeef;
1009 SetLastError(0xdeadbeef);
1010 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1011 ok(rc
, "AccessCheck error %d\n", GetLastError());
1012 ok(status
== 1, "expected 1, got %d\n", status
);
1013 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
1015 SetLastError(0xdeadbeef);
1016 rc
= AccessCheck(sd
, token
, 0xffffffff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1017 ok(!rc
, "AccessCheck should fail\n");
1018 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED
, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
1020 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
1021 SetLastError(0xdeadbeef);
1022 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
1023 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
1024 retSize
= 0xdeadbeef;
1025 SetLastError(0xdeadbeef);
1026 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
1027 ok(!rc
, "WriteFile should fail\n");
1028 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1029 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
1032 rc
= GetFileAttributesA(file
);
1033 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
1035 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
1036 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
1038 SetLastError(0xdeadbeef);
1039 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
1040 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1041 SetLastError(0xdeadbeef);
1042 rc
= DeleteFileA(file
);
1043 ok(rc
, "DeleteFile error %d\n", GetLastError());
1045 SetLastError(0xdeadbeef);
1046 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
1047 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
1048 retSize
= 0xdeadbeef;
1049 SetLastError(0xdeadbeef);
1050 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
1051 ok(!rc
, "WriteFile should fail\n");
1052 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1053 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
1056 rc
= GetFileAttributesA(file
);
1057 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
1058 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
1059 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
1061 retSize
= 0xdeadbeef;
1062 SetLastError(0xdeadbeef);
1063 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
1064 sd
, sdSize
, &retSize
);
1065 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
1066 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
1068 priv_set_len
= sizeof(priv_set
);
1069 granted
= 0xdeadbeef;
1070 status
= 0xdeadbeef;
1071 SetLastError(0xdeadbeef);
1072 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1073 ok(rc
, "AccessCheck error %d\n", GetLastError());
1074 ok(status
== 1, "expected 1, got %d\n", status
);
1075 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
1077 granted
= 0xdeadbeef;
1078 status
= 0xdeadbeef;
1079 SetLastError(0xdeadbeef);
1080 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1081 ok(rc
, "AccessCheck error %d\n", GetLastError());
1083 ok(status
== 1, "expected 1, got %d\n", status
);
1084 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
1086 granted
= 0xdeadbeef;
1087 status
= 0xdeadbeef;
1088 SetLastError(0xdeadbeef);
1089 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1090 ok(rc
, "AccessCheck error %d\n", GetLastError());
1091 ok(status
== 1, "expected 1, got %d\n", status
);
1092 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
1094 granted
= 0xdeadbeef;
1095 status
= 0xdeadbeef;
1096 SetLastError(0xdeadbeef);
1097 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1098 ok(rc
, "AccessCheck error %d\n", GetLastError());
1100 ok(status
== 1, "expected 1, got %d\n", status
);
1101 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
1103 granted
= 0xdeadbeef;
1104 status
= 0xdeadbeef;
1105 SetLastError(0xdeadbeef);
1106 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1107 ok(rc
, "AccessCheck error %d\n", GetLastError());
1109 ok(status
== 1, "expected 1, got %d\n", status
);
1110 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
1112 granted
= 0xdeadbeef;
1113 status
= 0xdeadbeef;
1114 SetLastError(0xdeadbeef);
1115 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1116 ok(rc
, "AccessCheck error %d\n", GetLastError());
1118 ok(status
== 1, "expected 1, got %d\n", status
);
1119 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
1121 granted
= 0xdeadbeef;
1122 status
= 0xdeadbeef;
1123 SetLastError(0xdeadbeef);
1124 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1125 ok(rc
, "AccessCheck error %d\n", GetLastError());
1127 ok(status
== 1, "expected 1, got %d\n", status
);
1128 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
1130 SetLastError(0xdeadbeef);
1131 rc
= DeleteFileA(file
);
1132 ok(!rc
, "DeleteFile should fail\n");
1133 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1134 SetLastError(0xdeadbeef);
1135 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
1136 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1137 SetLastError(0xdeadbeef);
1138 rc
= DeleteFileA(file
);
1139 ok(rc
, "DeleteFile error %d\n", GetLastError());
1142 HeapFree(GetProcessHeap(), 0, sd
);
1145 static void test_AccessCheck(void)
1147 PSID EveryoneSid
= NULL
, AdminSid
= NULL
, UsersSid
= NULL
;
1149 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
1150 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
1151 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1152 GENERIC_MAPPING Mapping
= { KEY_READ
, KEY_WRITE
, KEY_EXECUTE
, KEY_ALL_ACCESS
};
1156 HANDLE ProcessToken
;
1159 PRIVILEGE_SET
*PrivSet
;
1161 HMODULE NtDllModule
;
1164 NTSTATUS ntret
, ntAccessStatus
;
1166 NtDllModule
= GetModuleHandleA("ntdll.dll");
1169 skip("not running on NT, skipping test\n");
1172 pRtlAdjustPrivilege
= (void *)GetProcAddress(NtDllModule
, "RtlAdjustPrivilege");
1173 if (!pRtlAdjustPrivilege
)
1175 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1179 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
1180 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1181 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1183 skip("ACLs not implemented - skipping tests\n");
1184 HeapFree(GetProcessHeap(), 0, Acl
);
1187 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
1189 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
1190 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1192 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1193 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &AdminSid
);
1194 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1196 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1197 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
1198 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1200 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
1202 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
1203 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1205 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1206 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1208 PrivSetLen
= FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[16]);
1209 PrivSet
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, PrivSetLen
);
1210 PrivSet
->PrivilegeCount
= 16;
1212 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &ProcessToken
);
1213 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
1215 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, FALSE
, TRUE
, &Enabled
);
1217 res
= DuplicateToken(ProcessToken
, SecurityImpersonation
, &Token
);
1218 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1220 /* SD without owner/group */
1221 SetLastError(0xdeadbeef);
1222 Access
= AccessStatus
= 0x1abe11ed;
1223 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_QUERY_VALUE
, &Mapping
,
1224 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1225 err
= GetLastError();
1226 ok(!ret
&& err
== ERROR_INVALID_SECURITY_DESCR
, "AccessCheck should have "
1227 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err
);
1228 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1229 "Access and/or AccessStatus were changed!\n");
1231 /* Set owner and group */
1232 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
1233 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1234 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, TRUE
);
1235 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1237 /* Generic access mask */
1238 SetLastError(0xdeadbeef);
1239 Access
= AccessStatus
= 0x1abe11ed;
1240 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1241 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1242 err
= GetLastError();
1243 ok(!ret
&& err
== ERROR_GENERIC_NOT_MAPPED
, "AccessCheck should have failed "
1244 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err
);
1245 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1246 "Access and/or AccessStatus were changed!\n");
1248 /* Generic access mask - no privilegeset buffer */
1249 SetLastError(0xdeadbeef);
1250 Access
= AccessStatus
= 0x1abe11ed;
1251 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1252 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1253 err
= GetLastError();
1254 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1255 "with ERROR_NOACCESS, instead of %d\n", err
);
1256 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1257 "Access and/or AccessStatus were changed!\n");
1259 /* Generic access mask - no returnlength */
1260 SetLastError(0xdeadbeef);
1261 Access
= AccessStatus
= 0x1abe11ed;
1262 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1263 PrivSet
, NULL
, &Access
, &AccessStatus
);
1264 err
= GetLastError();
1265 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1266 "with ERROR_NOACCESS, instead of %d\n", err
);
1267 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1268 "Access and/or AccessStatus were changed!\n");
1270 /* Generic access mask - no privilegeset buffer, no returnlength */
1271 SetLastError(0xdeadbeef);
1272 Access
= AccessStatus
= 0x1abe11ed;
1273 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1274 NULL
, NULL
, &Access
, &AccessStatus
);
1275 err
= GetLastError();
1276 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1277 "with ERROR_NOACCESS, instead of %d\n", err
);
1278 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1279 "Access and/or AccessStatus were changed!\n");
1281 /* sd with no dacl present */
1282 Access
= AccessStatus
= 0x1abe11ed;
1283 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, FALSE
, NULL
, FALSE
);
1284 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1285 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1286 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1287 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1288 ok(AccessStatus
&& (Access
== KEY_READ
),
1289 "AccessCheck failed to grant access with error %d\n",
1292 /* sd with no dacl present - no privilegeset buffer */
1293 SetLastError(0xdeadbeef);
1294 Access
= AccessStatus
= 0x1abe11ed;
1295 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1296 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1297 err
= GetLastError();
1298 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1299 "with ERROR_NOACCESS, instead of %d\n", err
);
1300 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1301 "Access and/or AccessStatus were changed!\n");
1305 /* Generic access mask - no privilegeset buffer */
1306 SetLastError(0xdeadbeef);
1307 Access
= ntAccessStatus
= 0x1abe11ed;
1308 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1309 NULL
, &PrivSetLen
, &Access
, &ntAccessStatus
);
1310 err
= GetLastError();
1311 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1312 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1313 ok(err
== 0xdeadbeef,
1314 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1315 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1316 "Access and/or AccessStatus were changed!\n");
1318 /* Generic access mask - no returnlength */
1319 SetLastError(0xdeadbeef);
1320 Access
= ntAccessStatus
= 0x1abe11ed;
1321 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1322 PrivSet
, NULL
, &Access
, &ntAccessStatus
);
1323 err
= GetLastError();
1324 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1325 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1326 ok(err
== 0xdeadbeef,
1327 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1328 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1329 "Access and/or AccessStatus were changed!\n");
1331 /* Generic access mask - no privilegeset buffer, no returnlength */
1332 SetLastError(0xdeadbeef);
1333 Access
= ntAccessStatus
= 0x1abe11ed;
1334 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1335 NULL
, NULL
, &Access
, &ntAccessStatus
);
1336 err
= GetLastError();
1337 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1338 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1339 ok(err
== 0xdeadbeef,
1340 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1341 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1342 "Access and/or AccessStatus were changed!\n");
1345 win_skip("NtAccessCheck unavailable. Skipping.\n");
1347 /* sd with NULL dacl */
1348 Access
= AccessStatus
= 0x1abe11ed;
1349 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, NULL
, FALSE
);
1350 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1351 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1352 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1353 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1354 ok(AccessStatus
&& (Access
== KEY_READ
),
1355 "AccessCheck failed to grant access with error %d\n",
1358 /* sd with blank dacl */
1359 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1360 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1361 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1362 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1363 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1364 err
= GetLastError();
1365 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1366 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1367 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1369 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, KEY_READ
, EveryoneSid
);
1370 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1372 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, KEY_SET_VALUE
, AdminSid
);
1373 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1376 Access
= AccessStatus
= 0x1abe11ed;
1377 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1378 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1379 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1380 ok(AccessStatus
&& (Access
== KEY_READ
),
1381 "AccessCheck failed to grant access with error %d\n",
1384 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1385 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1386 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1388 "AccessCheck failed to grant any access with error %d\n",
1390 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access
);
1392 /* Access denied by SD */
1393 SetLastError(0xdeadbeef);
1394 Access
= AccessStatus
= 0x1abe11ed;
1395 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_WRITE
, &Mapping
,
1396 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1397 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1398 err
= GetLastError();
1399 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1400 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1401 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1404 PrivSet
->PrivilegeCount
= 16;
1405 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1406 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1407 ok(ret
&& !AccessStatus
&& GetLastError() == ERROR_PRIVILEGE_NOT_HELD
,
1408 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1411 ret
= ImpersonateLoggedOnUser(Token
);
1412 ok(ret
, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1413 ret
= pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, TRUE
, TRUE
, &Enabled
);
1417 PrivSet
->PrivilegeCount
= 16;
1418 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1419 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1420 ok(ret
&& AccessStatus
&& GetLastError() == 0,
1421 "AccessCheck should have succeeded, error %d\n",
1423 ok(Access
== ACCESS_SYSTEM_SECURITY
,
1424 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1428 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1430 ret
= RevertToSelf();
1431 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1433 /* test INHERIT_ONLY_ACE */
1434 ret
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1435 ok(ret
, "InitializeAcl failed with error %d\n", GetLastError());
1437 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1440 if (pAddAccessAllowedAceEx
)
1442 ret
= pAddAccessAllowedAceEx(Acl
, ACL_REVISION
, INHERIT_ONLY_ACE
, KEY_READ
, EveryoneSid
);
1443 ok(ret
, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1446 win_skip("AddAccessAllowedAceEx is not available\n");
1448 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1449 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1450 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1451 err
= GetLastError();
1452 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1453 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1454 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1458 res
= DuplicateToken(ProcessToken
, SecurityAnonymous
, &Token
);
1459 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1461 SetLastError(0xdeadbeef);
1462 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1463 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1464 err
= GetLastError();
1465 ok(!ret
&& err
== ERROR_BAD_IMPERSONATION_LEVEL
, "AccessCheck should have failed "
1466 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err
);
1470 SetLastError(0xdeadbeef);
1471 ret
= AccessCheck(SecurityDescriptor
, ProcessToken
, KEY_READ
, &Mapping
,
1472 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1473 err
= GetLastError();
1474 ok(!ret
&& err
== ERROR_NO_IMPERSONATION_TOKEN
, "AccessCheck should have failed "
1475 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err
);
1477 CloseHandle(ProcessToken
);
1480 FreeSid(EveryoneSid
);
1485 HeapFree(GetProcessHeap(), 0, Acl
);
1486 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
1487 HeapFree(GetProcessHeap(), 0, PrivSet
);
1490 /* test GetTokenInformation for the various attributes */
1491 static void test_token_attr(void)
1493 HANDLE Token
, ImpersonationToken
;
1495 TOKEN_PRIVILEGES
*Privileges
;
1496 TOKEN_GROUPS
*Groups
;
1498 TOKEN_DEFAULT_DACL
*Dacl
;
1502 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1505 /* cygwin-like use case */
1506 SetLastError(0xdeadbeef);
1507 ret
= OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED
, &Token
);
1508 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1510 win_skip("OpenProcessToken is not implemented\n");
1513 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1516 DWORD buf
[256]; /* GetTokenInformation wants a dword-aligned buffer */
1518 ret
= GetTokenInformation(Token
, TokenUser
,(void*)buf
, Size
, &Size
);
1519 ok(ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1520 Size
= sizeof(ImpersonationLevel
);
1521 ret
= GetTokenInformation(Token
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1522 GLE
= GetLastError();
1523 ok(!ret
&& (GLE
== ERROR_INVALID_PARAMETER
), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE
);
1527 if(!pConvertSidToStringSidA
)
1529 win_skip("ConvertSidToStringSidA is not available\n");
1533 SetLastError(0xdeadbeef);
1534 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS
, &Token
);
1535 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1538 /* insufficient buffer length */
1539 SetLastError(0xdeadbeef);
1541 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size2
);
1542 ok(Size2
> 1, "got %d\n", Size2
);
1543 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1544 "%d with error %d\n", ret
, GetLastError());
1546 Groups
= HeapAlloc(GetProcessHeap(), 0, Size2
);
1547 memset(Groups
, 0xcc, Size2
);
1549 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size2
, &Size
);
1550 ok(Size
> 1, "got %d\n", Size
);
1551 ok((!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) || broken(ret
) /* wow64 */,
1552 "%d with error %d\n", ret
, GetLastError());
1554 ok(*((BYTE
*)Groups
) == 0xcc, "buffer altered\n");
1556 HeapFree(GetProcessHeap(), 0, Groups
);
1558 SetLastError(0xdeadbeef);
1559 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size
);
1560 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1561 "GetTokenInformation(TokenGroups) %s with error %d\n",
1562 ret
? "succeeded" : "failed", GetLastError());
1563 Groups
= HeapAlloc(GetProcessHeap(), 0, Size
);
1564 SetLastError(0xdeadbeef);
1565 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size
, &Size
);
1566 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1567 ok(GetLastError() == 0xdeadbeef,
1568 "GetTokenInformation shouldn't have set last error to %d\n",
1570 trace("TokenGroups:\n");
1571 for (i
= 0; i
< Groups
->GroupCount
; i
++)
1573 DWORD NameLength
= 255;
1575 DWORD DomainLength
= 255;
1577 SID_NAME_USE SidNameUse
;
1580 ret
= LookupAccountSidA(NULL
, Groups
->Groups
[i
].Sid
, Name
, &NameLength
, Domain
, &DomainLength
, &SidNameUse
);
1583 pConvertSidToStringSidA(Groups
->Groups
[i
].Sid
, &SidString
);
1584 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString
, Domain
, Name
, SidNameUse
, Groups
->Groups
[i
].Attributes
);
1585 LocalFree(SidString
);
1587 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups
->Groups
[i
].Attributes
, GetLastError());
1589 HeapFree(GetProcessHeap(), 0, Groups
);
1592 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
1593 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1594 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1595 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
1596 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
1598 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1600 pConvertSidToStringSidA(User
->User
.Sid
, &SidString
);
1601 trace("TokenUser: %s attr: 0x%08x\n", SidString
, User
->User
.Attributes
);
1602 LocalFree(SidString
);
1603 HeapFree(GetProcessHeap(), 0, User
);
1606 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
1607 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1608 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1609 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1610 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1612 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1613 trace("TokenPrivileges:\n");
1614 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1617 DWORD NameLen
= sizeof(Name
)/sizeof(Name
[0]);
1618 LookupPrivilegeNameA(NULL
, &Privileges
->Privileges
[i
].Luid
, Name
, &NameLen
);
1619 trace("\t%s, 0x%x\n", Name
, Privileges
->Privileges
[i
].Attributes
);
1621 HeapFree(GetProcessHeap(), 0, Privileges
);
1623 ret
= DuplicateToken(Token
, SecurityAnonymous
, &ImpersonationToken
);
1624 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1626 Size
= sizeof(ImpersonationLevel
);
1627 ret
= GetTokenInformation(ImpersonationToken
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1628 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1629 ok(ImpersonationLevel
== SecurityAnonymous
, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel
);
1631 CloseHandle(ImpersonationToken
);
1634 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0, &Size
);
1635 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1636 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1638 Dacl
= HeapAlloc(GetProcessHeap(), 0, Size
);
1639 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size
);
1640 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1642 SetLastError(0xdeadbeef);
1643 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0);
1644 GLE
= GetLastError();
1645 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1646 ok(GLE
== ERROR_BAD_LENGTH
, "expected ERROR_BAD_LENGTH got %u\n", GLE
);
1648 SetLastError(0xdeadbeef);
1649 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, Size
);
1650 GLE
= GetLastError();
1651 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1652 ok(GLE
== ERROR_NOACCESS
, "expected ERROR_NOACCESS got %u\n", GLE
);
1654 acl
= Dacl
->DefaultDacl
;
1655 Dacl
->DefaultDacl
= NULL
;
1657 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1658 ok(ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1661 Dacl
->DefaultDacl
= (ACL
*)0xdeadbeef;
1662 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1663 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1664 ok(Dacl
->DefaultDacl
== NULL
, "expected NULL, got %p\n", Dacl
->DefaultDacl
);
1665 ok(Size2
== sizeof(TOKEN_DEFAULT_DACL
) || broken(Size2
== 2*sizeof(TOKEN_DEFAULT_DACL
)), /* WoW64 */
1666 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2
);
1668 Dacl
->DefaultDacl
= acl
;
1669 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1670 ok(ret
, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1672 if (Size2
== sizeof(TOKEN_DEFAULT_DACL
)) {
1673 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1674 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1676 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1678 HeapFree(GetProcessHeap(), 0, Dacl
);
1682 typedef union _MAX_SID
1685 char max
[SECURITY_MAX_SID_SIZE
];
1688 static void test_sid_str(PSID
* sid
)
1691 BOOL ret
= pConvertSidToStringSidA(sid
, &str_sid
);
1692 ok(ret
, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1695 char account
[MAX_PATH
], domain
[MAX_PATH
];
1697 DWORD acc_size
= MAX_PATH
;
1698 DWORD dom_size
= MAX_PATH
;
1699 ret
= LookupAccountSidA (NULL
, sid
, account
, &acc_size
, domain
, &dom_size
, &use
);
1700 ok(ret
|| (!ret
&& (GetLastError() == ERROR_NONE_MAPPED
)),
1701 "LookupAccountSid(%s) failed: %d\n", str_sid
, GetLastError());
1703 trace(" %s %s\\%s %d\n", str_sid
, domain
, account
, use
);
1704 else if (GetLastError() == ERROR_NONE_MAPPED
)
1705 trace(" %s couldn't be mapped\n", str_sid
);
1710 static const struct well_known_sid_value
1712 BOOL without_domain
;
1713 const char *sid_string
;
1714 } well_known_sid_values
[] = {
1715 /* 0 */ {TRUE
, "S-1-0-0"}, {TRUE
, "S-1-1-0"}, {TRUE
, "S-1-2-0"}, {TRUE
, "S-1-3-0"},
1716 /* 4 */ {TRUE
, "S-1-3-1"}, {TRUE
, "S-1-3-2"}, {TRUE
, "S-1-3-3"}, {TRUE
, "S-1-5"},
1717 /* 8 */ {FALSE
, "S-1-5-1"}, {TRUE
, "S-1-5-2"}, {TRUE
, "S-1-5-3"}, {TRUE
, "S-1-5-4"},
1718 /* 12 */ {TRUE
, "S-1-5-6"}, {TRUE
, "S-1-5-7"}, {TRUE
, "S-1-5-8"}, {TRUE
, "S-1-5-9"},
1719 /* 16 */ {TRUE
, "S-1-5-10"}, {TRUE
, "S-1-5-11"}, {TRUE
, "S-1-5-12"}, {TRUE
, "S-1-5-13"},
1720 /* 20 */ {TRUE
, "S-1-5-14"}, {FALSE
, NULL
}, {TRUE
, "S-1-5-18"}, {TRUE
, "S-1-5-19"},
1721 /* 24 */ {TRUE
, "S-1-5-20"}, {TRUE
, "S-1-5-32"},
1722 /* 26 */ {FALSE
, "S-1-5-32-544"}, {TRUE
, "S-1-5-32-545"}, {TRUE
, "S-1-5-32-546"},
1723 /* 29 */ {TRUE
, "S-1-5-32-547"}, {TRUE
, "S-1-5-32-548"}, {TRUE
, "S-1-5-32-549"},
1724 /* 32 */ {TRUE
, "S-1-5-32-550"}, {TRUE
, "S-1-5-32-551"}, {TRUE
, "S-1-5-32-552"},
1725 /* 35 */ {TRUE
, "S-1-5-32-554"}, {TRUE
, "S-1-5-32-555"}, {TRUE
, "S-1-5-32-556"},
1726 /* 38 */ {FALSE
, "S-1-5-21-12-23-34-45-56-500"}, {FALSE
, "S-1-5-21-12-23-34-45-56-501"},
1727 /* 40 */ {FALSE
, "S-1-5-21-12-23-34-45-56-502"}, {FALSE
, "S-1-5-21-12-23-34-45-56-512"},
1728 /* 42 */ {FALSE
, "S-1-5-21-12-23-34-45-56-513"}, {FALSE
, "S-1-5-21-12-23-34-45-56-514"},
1729 /* 44 */ {FALSE
, "S-1-5-21-12-23-34-45-56-515"}, {FALSE
, "S-1-5-21-12-23-34-45-56-516"},
1730 /* 46 */ {FALSE
, "S-1-5-21-12-23-34-45-56-517"}, {FALSE
, "S-1-5-21-12-23-34-45-56-518"},
1731 /* 48 */ {FALSE
, "S-1-5-21-12-23-34-45-56-519"}, {FALSE
, "S-1-5-21-12-23-34-45-56-520"},
1732 /* 50 */ {FALSE
, "S-1-5-21-12-23-34-45-56-553"},
1733 /* Added in Windows Server 2003 */
1734 /* 51 */ {TRUE
, "S-1-5-64-10"}, {TRUE
, "S-1-5-64-21"}, {TRUE
, "S-1-5-64-14"},
1735 /* 54 */ {TRUE
, "S-1-5-15"}, {TRUE
, "S-1-5-1000"}, {FALSE
, "S-1-5-32-557"},
1736 /* 57 */ {TRUE
, "S-1-5-32-558"}, {TRUE
, "S-1-5-32-559"}, {TRUE
, "S-1-5-32-560"},
1737 /* 60 */ {TRUE
, "S-1-5-32-561"}, {TRUE
, "S-1-5-32-562"},
1738 /* Added in Windows Vista: */
1739 /* 62 */ {TRUE
, "S-1-5-32-568"},
1740 /* 63 */ {TRUE
, "S-1-5-17"}, {FALSE
, "S-1-5-32-569"}, {TRUE
, "S-1-16-0"},
1741 /* 66 */ {TRUE
, "S-1-16-4096"}, {TRUE
, "S-1-16-8192"}, {TRUE
, "S-1-16-12288"},
1742 /* 69 */ {TRUE
, "S-1-16-16384"}, {TRUE
, "S-1-5-33"}, {TRUE
, "S-1-3-4"},
1743 /* 72 */ {FALSE
, "S-1-5-21-12-23-34-45-56-571"}, {FALSE
, "S-1-5-21-12-23-34-45-56-572"},
1744 /* 74 */ {TRUE
, "S-1-5-22"}, {FALSE
, "S-1-5-21-12-23-34-45-56-521"}, {TRUE
, "S-1-5-32-573"},
1745 /* 77 */ {FALSE
, "S-1-5-21-12-23-34-45-56-498"}, {TRUE
, "S-1-5-32-574"}, {TRUE
, "S-1-16-8448"}
1748 static void test_CreateWellKnownSid(void)
1750 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
1751 PSID domainsid
, sid
;
1756 if (!pCreateWellKnownSid
)
1758 win_skip("CreateWellKnownSid not available\n");
1763 SetLastError(0xdeadbeef);
1764 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1765 error
= GetLastError();
1766 ok(!ret
, "CreateWellKnownSid succeeded\n");
1767 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1768 ok(size
, "expected size > 0\n");
1770 SetLastError(0xdeadbeef);
1771 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1772 error
= GetLastError();
1773 ok(!ret
, "CreateWellKnownSid succeeded\n");
1774 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
1776 sid
= HeapAlloc(GetProcessHeap(), 0, size
);
1777 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, sid
, &size
);
1778 ok(ret
, "CreateWellKnownSid failed %u\n", GetLastError());
1779 HeapFree(GetProcessHeap(), 0, sid
);
1781 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1782 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
1784 for (i
= 0; i
< sizeof(well_known_sid_values
)/sizeof(well_known_sid_values
[0]); i
++)
1786 const struct well_known_sid_value
*value
= &well_known_sid_values
[i
];
1787 char sid_buffer
[SECURITY_MAX_SID_SIZE
];
1791 if (value
->sid_string
== NULL
)
1794 if (i
> WinAccountRasAndIasServersSid
)
1796 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1797 cb
= sizeof(sid_buffer
);
1798 if (!pCreateWellKnownSid(i
, domainsid
, sid_buffer
, &cb
))
1800 skip("Well known SIDs starting from %u are not implemented\n", i
);
1805 cb
= sizeof(sid_buffer
);
1806 ok(pCreateWellKnownSid(i
, value
->without_domain
? NULL
: domainsid
, sid_buffer
, &cb
), "Couldn't create well known sid %u\n", i
);
1807 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1808 ok(IsValidSid(sid_buffer
), "The sid is not valid\n");
1809 ok(pConvertSidToStringSidA(sid_buffer
, &str
), "Couldn't convert SID to string\n");
1810 ok(strcmp(str
, value
->sid_string
) == 0, "%d: SID mismatch - expected %s, got %s\n", i
,
1811 value
->sid_string
, str
);
1814 if (value
->without_domain
)
1816 char buf2
[SECURITY_MAX_SID_SIZE
];
1818 ok(pCreateWellKnownSid(i
, domainsid
, buf2
, &cb
), "Couldn't create well known sid %u with optional domain\n", i
);
1819 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1820 ok(memcmp(buf2
, sid_buffer
, cb
) == 0, "SID create with domain is different than without (%u)\n", i
);
1827 static void test_LookupAccountSid(void)
1829 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1830 CHAR accountA
[MAX_PATH
], domainA
[MAX_PATH
], usernameA
[MAX_PATH
];
1831 DWORD acc_sizeA
, dom_sizeA
, user_sizeA
;
1832 DWORD real_acc_sizeA
, real_dom_sizeA
;
1833 WCHAR accountW
[MAX_PATH
], domainW
[MAX_PATH
];
1834 DWORD acc_sizeW
, dom_sizeW
;
1835 DWORD real_acc_sizeW
, real_dom_sizeW
;
1836 PSID pUsersSid
= NULL
;
1839 DWORD error
, size
, cbti
= 0;
1844 PTOKEN_USER ptiUser
= NULL
;
1846 /* native windows crashes if account size, domain size, or name use is NULL */
1848 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1849 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &pUsersSid
);
1850 ok(ret
|| (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
),
1851 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1853 /* not running on NT so give up */
1854 if (!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1857 real_acc_sizeA
= MAX_PATH
;
1858 real_dom_sizeA
= MAX_PATH
;
1859 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1860 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1862 /* try NULL account */
1863 acc_sizeA
= MAX_PATH
;
1864 dom_sizeA
= MAX_PATH
;
1865 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1866 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1868 /* try NULL domain */
1869 acc_sizeA
= MAX_PATH
;
1870 dom_sizeA
= MAX_PATH
;
1871 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1872 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1874 /* try a small account buffer */
1876 dom_sizeA
= MAX_PATH
;
1878 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1879 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1880 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1881 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1883 /* try a 0 sized account buffer */
1885 dom_sizeA
= MAX_PATH
;
1887 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1888 /* this can fail or succeed depending on OS version but the size will always be returned */
1889 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1890 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1891 real_acc_sizeA
+ 1, acc_sizeA
);
1893 /* try a 0 sized account buffer */
1895 dom_sizeA
= MAX_PATH
;
1896 LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1897 /* this can fail or succeed depending on OS version but the size will always be returned */
1898 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1899 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1900 real_acc_sizeA
+ 1, acc_sizeA
);
1902 /* try a small domain buffer */
1904 acc_sizeA
= MAX_PATH
;
1906 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1907 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1908 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1909 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1911 /* try a 0 sized domain buffer */
1913 acc_sizeA
= MAX_PATH
;
1915 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1916 /* this can fail or succeed depending on OS version but the size will always be returned */
1917 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1918 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1919 real_dom_sizeA
+ 1, dom_sizeA
);
1921 /* try a 0 sized domain buffer */
1923 acc_sizeA
= MAX_PATH
;
1924 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1925 /* this can fail or succeed depending on OS version but the size will always be returned */
1926 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1927 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1928 real_dom_sizeA
+ 1, dom_sizeA
);
1930 real_acc_sizeW
= MAX_PATH
;
1931 real_dom_sizeW
= MAX_PATH
;
1932 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &real_acc_sizeW
, domainW
, &real_dom_sizeW
, &use
);
1933 ok(ret
, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1935 /* try an invalid system name */
1936 real_acc_sizeA
= MAX_PATH
;
1937 real_dom_sizeA
= MAX_PATH
;
1938 ret
= LookupAccountSidA("deepthought", pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1939 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1940 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
1941 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1943 /* native windows crashes if domainW or accountW is NULL */
1945 /* try a small account buffer */
1947 dom_sizeW
= MAX_PATH
;
1949 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1950 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1951 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1952 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1954 /* try a 0 sized account buffer */
1956 dom_sizeW
= MAX_PATH
;
1958 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1959 /* this can fail or succeed depending on OS version but the size will always be returned */
1960 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1961 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1962 real_acc_sizeW
+ 1, acc_sizeW
);
1964 /* try a 0 sized account buffer */
1966 dom_sizeW
= MAX_PATH
;
1967 LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1968 /* this can fail or succeed depending on OS version but the size will always be returned */
1969 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1970 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1971 real_acc_sizeW
+ 1, acc_sizeW
);
1973 /* try a small domain buffer */
1975 acc_sizeW
= MAX_PATH
;
1977 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1978 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1979 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1980 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1982 /* try a 0 sized domain buffer */
1984 acc_sizeW
= MAX_PATH
;
1986 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1987 /* this can fail or succeed depending on OS version but the size will always be returned */
1988 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1989 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1990 real_dom_sizeW
+ 1, dom_sizeW
);
1992 /* try a 0 sized domain buffer */
1994 acc_sizeW
= MAX_PATH
;
1995 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1996 /* this can fail or succeed depending on OS version but the size will always be returned */
1997 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1998 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1999 real_dom_sizeW
+ 1, dom_sizeW
);
2001 acc_sizeW
= dom_sizeW
= use
= 0;
2002 SetLastError(0xdeadbeef);
2003 ret
= LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
2004 error
= GetLastError();
2005 ok(!ret
, "LookupAccountSidW failed %u\n", GetLastError());
2006 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
2007 ok(acc_sizeW
, "expected non-zero account size\n");
2008 ok(dom_sizeW
, "expected non-zero domain size\n");
2009 ok(!use
, "expected zero use %u\n", use
);
2013 /* Test LookupAccountSid with Sid retrieved from token information.
2014 This assumes this process is running under the account of the current user.*/
2015 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &hToken
);
2016 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2017 ret
= GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &cbti
);
2018 ok(!ret
, "GetTokenInformation failed with error %d\n", GetLastError());
2019 ptiUser
= HeapAlloc(GetProcessHeap(), 0, cbti
);
2020 if (GetTokenInformation(hToken
, TokenUser
, ptiUser
, cbti
, &cbti
))
2022 acc_sizeA
= dom_sizeA
= MAX_PATH
;
2023 ret
= LookupAccountSidA(NULL
, ptiUser
->User
.Sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
2024 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
2025 user_sizeA
= MAX_PATH
;
2026 ret
= GetUserNameA(usernameA
, &user_sizeA
);
2027 ok(ret
, "GetUserNameA() Expected TRUE, got FALSE\n");
2028 ok(lstrcmpA(usernameA
, accountA
) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA
, accountA
);
2030 HeapFree(GetProcessHeap(), 0, ptiUser
);
2032 if (pCreateWellKnownSid
&& pConvertSidToStringSidA
)
2034 trace("Well Known SIDs:\n");
2035 for (i
= 0; i
<= 60; i
++)
2037 size
= SECURITY_MAX_SID_SIZE
;
2038 if (pCreateWellKnownSid(i
, NULL
, &max_sid
.sid
, &size
))
2040 if (pConvertSidToStringSidA(&max_sid
.sid
, &str_sidA
))
2042 acc_sizeA
= MAX_PATH
;
2043 dom_sizeA
= MAX_PATH
;
2044 if (LookupAccountSidA(NULL
, &max_sid
.sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
))
2045 trace(" %d: %s %s\\%s %d\n", i
, str_sidA
, domainA
, accountA
, use
);
2046 LocalFree(str_sidA
);
2051 if (GetLastError() != ERROR_INVALID_PARAMETER
)
2052 trace(" CreateWellKnownSid(%d) failed: %d\n", i
, GetLastError());
2054 trace(" %d: not supported\n", i
);
2058 pLsaQueryInformationPolicy
= (void *)GetProcAddress( hmod
, "LsaQueryInformationPolicy");
2059 pLsaOpenPolicy
= (void *)GetProcAddress( hmod
, "LsaOpenPolicy");
2060 pLsaFreeMemory
= (void *)GetProcAddress( hmod
, "LsaFreeMemory");
2061 pLsaClose
= (void *)GetProcAddress( hmod
, "LsaClose");
2063 if (pLsaQueryInformationPolicy
&& pLsaOpenPolicy
&& pLsaFreeMemory
&& pLsaClose
)
2067 LSA_OBJECT_ATTRIBUTES object_attributes
;
2069 ZeroMemory(&object_attributes
, sizeof(object_attributes
));
2070 object_attributes
.Length
= sizeof(object_attributes
);
2072 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_ALL_ACCESS
, &handle
);
2073 ok(status
== STATUS_SUCCESS
|| status
== STATUS_ACCESS_DENIED
,
2074 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status
);
2076 /* try a more restricted access mask if necessary */
2077 if (status
== STATUS_ACCESS_DENIED
) {
2078 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
2079 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_VIEW_LOCAL_INFORMATION
, &handle
);
2080 ok(status
== STATUS_SUCCESS
, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status
);
2083 if (status
== STATUS_SUCCESS
)
2085 PPOLICY_ACCOUNT_DOMAIN_INFO info
;
2086 status
= pLsaQueryInformationPolicy(handle
, PolicyAccountDomainInformation
, (PVOID
*)&info
);
2087 ok(status
== STATUS_SUCCESS
, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status
);
2088 if (status
== STATUS_SUCCESS
)
2090 ok(info
->DomainSid
!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
2091 if (info
->DomainSid
)
2093 int count
= *GetSidSubAuthorityCount(info
->DomainSid
);
2094 CopySid(GetSidLengthRequired(count
), &max_sid
, info
->DomainSid
);
2095 test_sid_str((PSID
)&max_sid
.sid
);
2096 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_ADMIN
;
2097 max_sid
.sid
.SubAuthorityCount
= count
+ 1;
2098 test_sid_str((PSID
)&max_sid
.sid
);
2099 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_GUEST
;
2100 test_sid_str((PSID
)&max_sid
.sid
);
2101 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ADMINS
;
2102 test_sid_str((PSID
)&max_sid
.sid
);
2103 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_USERS
;
2104 test_sid_str((PSID
)&max_sid
.sid
);
2105 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_GUESTS
;
2106 test_sid_str((PSID
)&max_sid
.sid
);
2107 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_COMPUTERS
;
2108 test_sid_str((PSID
)&max_sid
.sid
);
2109 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CONTROLLERS
;
2110 test_sid_str((PSID
)&max_sid
.sid
);
2111 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CERT_ADMINS
;
2112 test_sid_str((PSID
)&max_sid
.sid
);
2113 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_SCHEMA_ADMINS
;
2114 test_sid_str((PSID
)&max_sid
.sid
);
2115 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
;
2116 test_sid_str((PSID
)&max_sid
.sid
);
2117 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_POLICY_ADMINS
;
2118 test_sid_str((PSID
)&max_sid
.sid
);
2119 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_ALIAS_RID_RAS_SERVERS
;
2120 test_sid_str((PSID
)&max_sid
.sid
);
2121 max_sid
.sid
.SubAuthority
[count
] = 1000; /* first user account */
2122 test_sid_str((PSID
)&max_sid
.sid
);
2125 pLsaFreeMemory((LPVOID
)info
);
2128 status
= pLsaClose(handle
);
2129 ok(status
== STATUS_SUCCESS
, "LsaClose() failed, returned 0x%08x\n", status
);
2135 static BOOL
get_sid_info(PSID psid
, LPSTR
*user
, LPSTR
*dom
)
2137 static CHAR account
[UNLEN
+ 1];
2138 static CHAR domain
[UNLEN
+ 1];
2139 DWORD size
, dom_size
;
2145 size
= dom_size
= UNLEN
+ 1;
2148 SetLastError(0xdeadbeef);
2149 return LookupAccountSidA(NULL
, psid
, account
, &size
, domain
, &dom_size
, &use
);
2152 static void check_wellknown_name(const char* name
, WELL_KNOWN_SID_TYPE result
)
2154 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
2155 PSID domainsid
= NULL
;
2156 char wk_sid
[SECURITY_MAX_SID_SIZE
];
2159 DWORD sid_size
, domain_size
;
2160 SID_NAME_USE sid_use
;
2161 LPSTR domain
, account
, sid_domain
, wk_domain
, wk_account
;
2167 ret
= LookupAccountNameA(NULL
, name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2168 ok(!ret
, " %s Should have failed to lookup account name\n", name
);
2169 psid
= HeapAlloc(GetProcessHeap(),0,sid_size
);
2170 domain
= HeapAlloc(GetProcessHeap(),0,domain_size
);
2171 ret
= LookupAccountNameA(NULL
, name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2175 ok(!ret
, " %s Should have failed to lookup account name\n",name
);
2179 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
2180 cb
= sizeof(wk_sid
);
2181 if (!pCreateWellKnownSid(result
, domainsid
, wk_sid
, &cb
))
2183 win_skip("SID %i is not available on the system\n",result
);
2187 ret2
= get_sid_info(wk_sid
, &wk_account
, &wk_domain
);
2188 if (!ret2
&& GetLastError() == ERROR_NONE_MAPPED
)
2190 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name
);
2194 get_sid_info(psid
, &account
, &sid_domain
);
2196 ok(ret
, "Failed to lookup account name %s\n",name
);
2197 ok(sid_size
!= 0, "sid_size was zero\n");
2199 ok(EqualSid(psid
,wk_sid
),"(%s) Sids fail to match well known sid!\n",name
);
2201 ok(!lstrcmpA(account
, wk_account
), "Expected %s , got %s\n", account
, wk_account
);
2202 ok(!lstrcmpA(domain
, wk_domain
), "Expected %s, got %s\n", wk_domain
, domain
);
2203 ok(sid_use
== SidTypeWellKnownGroup
, "Expected Use (5), got %d\n", sid_use
);
2207 HeapFree(GetProcessHeap(),0,psid
);
2208 HeapFree(GetProcessHeap(),0,domain
);
2211 static void test_LookupAccountName(void)
2213 DWORD sid_size
, domain_size
, user_size
;
2214 DWORD sid_save
, domain_save
;
2215 CHAR user_name
[UNLEN
+ 1];
2216 CHAR computer_name
[UNLEN
+ 1];
2217 SID_NAME_USE sid_use
;
2218 LPSTR domain
, account
, sid_dom
;
2222 /* native crashes if (assuming all other parameters correct):
2224 * - Sid is NULL and cbSid is > 0
2225 * - cbSid or cchReferencedDomainName are NULL
2226 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2229 user_size
= UNLEN
+ 1;
2230 SetLastError(0xdeadbeef);
2231 ret
= GetUserNameA(user_name
, &user_size
);
2232 ok(ret
, "Failed to get user name : %d\n", GetLastError());
2237 sid_use
= 0xcafebabe;
2238 SetLastError(0xdeadbeef);
2239 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2240 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2242 win_skip("LookupAccountNameA is not implemented\n");
2245 ok(!ret
, "Expected 0, got %d\n", ret
);
2246 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2247 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2248 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2249 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2250 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2252 sid_save
= sid_size
;
2253 domain_save
= domain_size
;
2255 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2256 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2258 /* try valid account name */
2259 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2260 get_sid_info(psid
, &account
, &sid_dom
);
2261 ok(ret
, "Failed to lookup account name\n");
2262 ok(sid_size
== GetLengthSid(psid
), "Expected %d, got %d\n", GetLengthSid(psid
), sid_size
);
2263 ok(!lstrcmpA(account
, user_name
), "Expected %s, got %s\n", user_name
, account
);
2264 ok(!lstrcmpA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2265 ok(domain_size
== domain_save
- 1, "Expected %d, got %d\n", domain_save
- 1, domain_size
);
2266 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2267 ok(sid_use
== SidTypeUser
, "Expected SidTypeUser (%d), got %d\n", SidTypeUser
, sid_use
);
2268 domain_size
= domain_save
;
2269 sid_size
= sid_save
;
2271 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2273 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2277 ret
= LookupAccountNameA(NULL
, "Everyone", psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2278 get_sid_info(psid
, &account
, &sid_dom
);
2279 ok(ret
, "Failed to lookup account name\n");
2280 ok(sid_size
!= 0, "sid_size was zero\n");
2281 ok(!lstrcmpA(account
, "Everyone"), "Expected Everyone, got %s\n", account
);
2282 ok(!lstrcmpA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2283 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2284 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2285 ok(sid_use
== SidTypeWellKnownGroup
, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup
, sid_use
);
2286 domain_size
= domain_save
;
2289 /* NULL Sid with zero sid size */
2290 SetLastError(0xdeadbeef);
2292 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2293 ok(!ret
, "Expected 0, got %d\n", ret
);
2294 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2295 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2296 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2297 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2299 /* try cchReferencedDomainName - 1 */
2300 SetLastError(0xdeadbeef);
2302 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2303 ok(!ret
, "Expected 0, got %d\n", ret
);
2304 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2305 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2306 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2307 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2309 /* NULL ReferencedDomainName with zero domain name size */
2310 SetLastError(0xdeadbeef);
2312 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2313 ok(!ret
, "Expected 0, got %d\n", ret
);
2314 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2315 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2316 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2317 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2319 HeapFree(GetProcessHeap(), 0, psid
);
2320 HeapFree(GetProcessHeap(), 0, domain
);
2322 /* get sizes for NULL account name */
2325 sid_use
= 0xcafebabe;
2326 SetLastError(0xdeadbeef);
2327 ret
= LookupAccountNameA(NULL
, NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2328 if (!ret
&& GetLastError() == ERROR_NONE_MAPPED
)
2329 win_skip("NULL account name doesn't work on NT4\n");
2332 ok(!ret
, "Expected 0, got %d\n", ret
);
2333 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2334 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2335 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2336 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2337 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2339 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2340 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2342 /* try NULL account name */
2343 ret
= LookupAccountNameA(NULL
, NULL
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2344 get_sid_info(psid
, &account
, &sid_dom
);
2345 ok(ret
, "Failed to lookup account name\n");
2346 /* Using a fixed string will not work on different locales */
2347 ok(!lstrcmpA(account
, domain
),
2348 "Got %s for account and %s for domain, these should be the same\n",
2350 ok(sid_use
== SidTypeDomain
, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain
, sid_use
);
2352 HeapFree(GetProcessHeap(), 0, psid
);
2353 HeapFree(GetProcessHeap(), 0, domain
);
2356 /* try an invalid account name */
2357 SetLastError(0xdeadbeef);
2360 ret
= LookupAccountNameA(NULL
, "oogabooga", NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2361 ok(!ret
, "Expected 0, got %d\n", ret
);
2362 ok(GetLastError() == ERROR_NONE_MAPPED
||
2363 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
),
2364 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2365 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2366 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2368 /* try an invalid system name */
2369 SetLastError(0xdeadbeef);
2372 ret
= LookupAccountNameA("deepthought", NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2373 ok(!ret
, "Expected 0, got %d\n", ret
);
2374 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
2375 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2376 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2377 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2379 /* try with the computer name as the account name */
2380 domain_size
= sizeof(computer_name
);
2381 GetComputerNameA(computer_name
, &domain_size
);
2384 ret
= LookupAccountNameA(NULL
, computer_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2385 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
||
2386 GetLastError() == ERROR_NONE_MAPPED
/* in a domain */ ||
2387 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE
) ||
2388 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
)),
2389 "LookupAccountNameA failed: %d\n", GetLastError());
2390 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2392 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2393 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2394 ret
= LookupAccountNameA(NULL
, computer_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2395 ok(ret
, "LookupAccountNameA failed: %d\n", GetLastError());
2396 ok(sid_use
== SidTypeDomain
||
2397 (sid_use
== SidTypeUser
&& ! strcmp(computer_name
, user_name
)), "expected SidTypeDomain for %s, got %d\n", computer_name
, sid_use
);
2398 HeapFree(GetProcessHeap(), 0, domain
);
2399 HeapFree(GetProcessHeap(), 0, psid
);
2402 /* Well Known names */
2403 if (!pCreateWellKnownSid
)
2405 win_skip("CreateWellKnownSid not available\n");
2409 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2411 skip("Non-English locale (skipping well known name creation tests)\n");
2415 check_wellknown_name("LocalService", WinLocalServiceSid
);
2416 check_wellknown_name("Local Service", WinLocalServiceSid
);
2418 check_wellknown_name("Local Service", 0);
2419 check_wellknown_name("NetworkService", WinNetworkServiceSid
);
2420 check_wellknown_name("Network Service", WinNetworkServiceSid
);
2422 /* example of some names where the spaces are not optional */
2423 check_wellknown_name("Terminal Server User", WinTerminalServerSid
);
2424 check_wellknown_name("TerminalServer User", 0);
2425 check_wellknown_name("TerminalServerUser", 0);
2426 check_wellknown_name("Terminal ServerUser", 0);
2428 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid
);
2429 check_wellknown_name("enterprisedomain controllers", 0);
2430 check_wellknown_name("enterprise domaincontrollers", 0);
2431 check_wellknown_name("enterprisedomaincontrollers", 0);
2433 /* case insensitivity */
2434 check_wellknown_name("lOCAlServICE", WinLocalServiceSid
);
2436 /* fully qualified account names */
2437 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid
);
2438 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid
);
2439 check_wellknown_name("nt authority test\\Network Service", 0);
2440 check_wellknown_name("Dummy\\Network Service", 0);
2441 check_wellknown_name("ntauthority\\Network Service", 0);
2444 static void test_security_descriptor(void)
2446 SECURITY_DESCRIPTOR sd
;
2449 BOOL isDefault
, isPresent
, ret
;
2453 SetLastError(0xdeadbeef);
2454 ret
= InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
2455 if (ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2457 win_skip("InitializeSecurityDescriptor is not implemented\n");
2461 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2462 expect_eq(psid
, NULL
, PSID
, "%p");
2463 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2464 sd
.Control
|= SE_DACL_PRESENT
| SE_SACL_PRESENT
;
2466 SetLastError(0xdeadbeef);
2468 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), FALSE
, BOOL
, "%d");
2469 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER
, DWORD
, "%u");
2470 ok(size
> 5, "Size not increased\n");
2473 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), TRUE
, BOOL
, "%d");
2474 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2475 expect_eq(psid
, NULL
, PSID
, "%p");
2476 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2477 ok(GetSecurityDescriptorGroup(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorGroup failed\n");
2478 expect_eq(psid
, NULL
, PSID
, "%p");
2479 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2480 ok(GetSecurityDescriptorDacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorDacl failed\n");
2481 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2482 expect_eq(psid
, NULL
, PSID
, "%p");
2483 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2484 ok(GetSecurityDescriptorSacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorSacl failed\n");
2485 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2486 expect_eq(psid
, NULL
, PSID
, "%p");
2487 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2491 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2492 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2493 static void test_granted_access(HANDLE handle
, ACCESS_MASK access
,
2494 ACCESS_MASK alt
, int line
)
2496 OBJECT_BASIC_INFORMATION obj_info
;
2499 if (!pNtQueryObject
)
2501 skip_(__FILE__
, line
)("Not NT platform - skipping tests\n");
2505 status
= pNtQueryObject( handle
, ObjectBasicInformation
, &obj_info
,
2506 sizeof(obj_info
), NULL
);
2507 ok_(__FILE__
, line
)(!status
, "NtQueryObject with err: %08x\n", status
);
2509 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
||
2510 obj_info
.GrantedAccess
== alt
, "Granted access should be 0x%08x "
2511 "or 0x%08x, instead of 0x%08x\n", access
, alt
, obj_info
.GrantedAccess
);
2513 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
, "Granted access should "
2514 "be 0x%08x, instead of 0x%08x\n", access
, obj_info
.GrantedAccess
);
2517 #define CHECK_SET_SECURITY(o,i,e) \
2521 SetLastError( 0xdeadbeef ); \
2522 res_ = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2523 err = GetLastError(); \
2524 if (e == ERROR_SUCCESS) \
2525 ok(res_, "SetKernelObjectSecurity failed with %d\n", err); \
2527 ok(!res_ && err == e, "SetKernelObjectSecurity should have failed " \
2528 "with %s, instead of %d\n", #e, err); \
2531 static void test_process_security(void)
2535 PTOKEN_PRIMARY_GROUP group
;
2536 PSID AdminSid
= NULL
, UsersSid
= NULL
;
2537 PACL Acl
= NULL
, ThreadAcl
= NULL
;
2538 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
, *ThreadSecurityDescriptor
= NULL
;
2539 char buffer
[MAX_PATH
];
2540 PROCESS_INFORMATION info
;
2541 STARTUPINFOA startup
;
2542 SECURITY_ATTRIBUTES psa
, tsa
;
2543 HANDLE token
, event
;
2545 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2546 PSID EveryoneSid
= NULL
;
2548 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
2549 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
2550 if (!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2552 win_skip("ACLs not implemented - skipping tests\n");
2553 HeapFree(GetProcessHeap(), 0, Acl
);
2556 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2558 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2559 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2561 /* get owner from the token we might be running as a user not admin */
2562 res
= OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED
, &token
);
2563 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
2566 HeapFree(GetProcessHeap(), 0, Acl
);
2570 res
= GetTokenInformation( token
, TokenOwner
, NULL
, 0, &size
);
2571 ok(!res
, "Expected failure, got %d\n", res
);
2572 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2573 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2575 owner
= HeapAlloc(GetProcessHeap(), 0, size
);
2576 res
= GetTokenInformation( token
, TokenOwner
, owner
, size
, &size
);
2577 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2578 AdminSid
= ((TOKEN_OWNER
*)owner
)->Owner
;
2580 res
= GetTokenInformation( token
, TokenPrimaryGroup
, NULL
, 0, &size
);
2581 ok(!res
, "Expected failure, got %d\n", res
);
2582 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2583 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2585 group
= HeapAlloc(GetProcessHeap(), 0, size
);
2586 res
= GetTokenInformation( token
, TokenPrimaryGroup
, group
, size
, &size
);
2587 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2588 UsersSid
= ((TOKEN_PRIMARY_GROUP
*)group
)->PrimaryGroup
;
2590 CloseHandle( token
);
2593 HeapFree(GetProcessHeap(), 0, group
);
2594 HeapFree(GetProcessHeap(), 0, owner
);
2595 HeapFree(GetProcessHeap(), 0, Acl
);
2599 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, PROCESS_VM_READ
, AdminSid
);
2600 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2601 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, PROCESS_ALL_ACCESS
, AdminSid
);
2602 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2604 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
2605 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
2606 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2608 event
= CreateEventA( NULL
, TRUE
, TRUE
, "test_event" );
2609 ok(event
!= NULL
, "CreateEvent %d\n", GetLastError());
2611 SecurityDescriptor
->Revision
= 0;
2612 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_UNKNOWN_REVISION
);
2613 SecurityDescriptor
->Revision
= SECURITY_DESCRIPTOR_REVISION
;
2615 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2616 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2617 CHECK_SET_SECURITY( event
, SACL_SECURITY_INFORMATION
, ERROR_ACCESS_DENIED
);
2618 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2619 /* NULL DACL is valid and means that everyone has access */
2620 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
;
2621 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2623 /* Set owner and group and dacl */
2624 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
2625 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2626 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2627 test_owner_equal( event
, AdminSid
, __LINE__
);
2629 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, EveryoneSid
, FALSE
);
2630 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2631 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2632 test_group_equal( event
, EveryoneSid
, __LINE__
);
2634 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2635 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2636 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2637 /* setting a dacl should not change the owner or group */
2638 test_owner_equal( event
, AdminSid
, __LINE__
);
2639 test_group_equal( event
, EveryoneSid
, __LINE__
);
2641 /* Test again with a different SID in case the previous SID also happens to
2642 * be the one that is incorrectly replacing the group. */
2643 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, FALSE
);
2644 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2645 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2646 test_group_equal( event
, UsersSid
, __LINE__
);
2648 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2649 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2650 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2651 test_group_equal( event
, UsersSid
, __LINE__
);
2653 sprintf(buffer
, "%s tests/security.c test", myARGV
[0]);
2654 memset(&startup
, 0, sizeof(startup
));
2655 startup
.cb
= sizeof(startup
);
2656 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
2657 startup
.wShowWindow
= SW_SHOWNORMAL
;
2659 psa
.nLength
= sizeof(psa
);
2660 psa
.lpSecurityDescriptor
= SecurityDescriptor
;
2661 psa
.bInheritHandle
= TRUE
;
2663 ThreadSecurityDescriptor
= HeapAlloc( GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
2664 res
= InitializeSecurityDescriptor( ThreadSecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
2665 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2667 ThreadAcl
= HeapAlloc( GetProcessHeap(), 0, 256 );
2668 res
= InitializeAcl( ThreadAcl
, 256, ACL_REVISION
);
2669 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2670 res
= AddAccessDeniedAce( ThreadAcl
, ACL_REVISION
, THREAD_SET_THREAD_TOKEN
, AdminSid
);
2671 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError() );
2672 res
= AddAccessAllowedAce( ThreadAcl
, ACL_REVISION
, THREAD_ALL_ACCESS
, AdminSid
);
2673 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2675 res
= SetSecurityDescriptorOwner( ThreadSecurityDescriptor
, AdminSid
, FALSE
);
2676 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2677 res
= SetSecurityDescriptorGroup( ThreadSecurityDescriptor
, UsersSid
, FALSE
);
2678 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2679 res
= SetSecurityDescriptorDacl( ThreadSecurityDescriptor
, TRUE
, ThreadAcl
, FALSE
);
2680 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2682 tsa
.nLength
= sizeof(tsa
);
2683 tsa
.lpSecurityDescriptor
= ThreadSecurityDescriptor
;
2684 tsa
.bInheritHandle
= TRUE
;
2686 /* Doesn't matter what ACL say we should get full access for ourselves */
2687 res
= CreateProcessA( NULL
, buffer
, &psa
, &tsa
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
2688 ok(res
, "CreateProcess with err:%d\n", GetLastError());
2689 TEST_GRANTED_ACCESS2( info
.hProcess
, PROCESS_ALL_ACCESS_NT4
,
2690 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2691 TEST_GRANTED_ACCESS2( info
.hThread
, THREAD_ALL_ACCESS_NT4
,
2692 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2693 winetest_wait_child_process( info
.hProcess
);
2695 FreeSid(EveryoneSid
);
2696 CloseHandle( info
.hProcess
);
2697 CloseHandle( info
.hThread
);
2698 CloseHandle( event
);
2699 HeapFree(GetProcessHeap(), 0, group
);
2700 HeapFree(GetProcessHeap(), 0, owner
);
2701 HeapFree(GetProcessHeap(), 0, Acl
);
2702 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
2703 HeapFree(GetProcessHeap(), 0, ThreadAcl
);
2704 HeapFree(GetProcessHeap(), 0, ThreadSecurityDescriptor
);
2707 static void test_process_security_child(void)
2709 HANDLE handle
, handle1
;
2713 handle
= OpenProcess( PROCESS_TERMINATE
, FALSE
, GetCurrentProcessId() );
2714 ok(handle
!= NULL
, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2715 TEST_GRANTED_ACCESS( handle
, PROCESS_TERMINATE
);
2717 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2718 &handle1
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2719 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2720 TEST_GRANTED_ACCESS( handle1
, PROCESS_TERMINATE
);
2722 CloseHandle( handle1
);
2724 SetLastError( 0xdeadbeef );
2725 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2726 &handle1
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2727 err
= GetLastError();
2728 ok(!ret
&& err
== ERROR_ACCESS_DENIED
, "duplicating handle should have failed "
2729 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err
);
2731 CloseHandle( handle
);
2733 /* These two should fail - they are denied by ACL */
2734 handle
= OpenProcess( PROCESS_VM_READ
, FALSE
, GetCurrentProcessId() );
2735 ok(handle
== NULL
, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2736 handle
= OpenProcess( PROCESS_ALL_ACCESS
, FALSE
, GetCurrentProcessId() );
2737 ok(handle
== NULL
, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2739 /* Documented privilege elevation */
2740 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2741 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2742 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2743 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2744 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2746 CloseHandle( handle
);
2748 /* Same only explicitly asking for all access rights */
2749 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2750 &handle
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2751 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2752 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2753 PROCESS_ALL_ACCESS
| PROCESS_QUERY_LIMITED_INFORMATION
);
2754 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2755 &handle1
, PROCESS_VM_READ
, TRUE
, 0 );
2756 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2757 TEST_GRANTED_ACCESS( handle1
, PROCESS_VM_READ
);
2758 CloseHandle( handle1
);
2759 CloseHandle( handle
);
2761 /* Test thread security */
2762 handle
= OpenThread( THREAD_TERMINATE
, FALSE
, GetCurrentThreadId() );
2763 ok(handle
!= NULL
, "OpenThread(THREAD_TERMINATE) with err:%d\n", GetLastError());
2764 TEST_GRANTED_ACCESS( handle
, PROCESS_TERMINATE
);
2765 CloseHandle( handle
);
2767 handle
= OpenThread( THREAD_SET_THREAD_TOKEN
, FALSE
, GetCurrentThreadId() );
2768 ok(handle
== NULL
, "OpenThread(THREAD_SET_THREAD_TOKEN) should have failed\n");
2771 static void test_impersonation_level(void)
2773 HANDLE Token
, ProcessToken
;
2776 TOKEN_PRIVILEGES
*Privileges
;
2778 PRIVILEGE_SET
*PrivilegeSet
;
2784 pDuplicateTokenEx
= (void *)GetProcAddress(hmod
, "DuplicateTokenEx");
2785 if( !pDuplicateTokenEx
) {
2786 win_skip("DuplicateTokenEx is not available\n");
2789 SetLastError(0xdeadbeef);
2790 ret
= ImpersonateSelf(SecurityAnonymous
);
2791 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2793 win_skip("ImpersonateSelf is not implemented\n");
2796 ok(ret
, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2797 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2798 ok(!ret
, "OpenThreadToken should have failed\n");
2799 error
= GetLastError();
2800 ok(error
== ERROR_CANT_OPEN_ANONYMOUS
, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2801 /* can't perform access check when opening object against an anonymous impersonation token */
2803 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2804 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_CANT_OPEN_ANONYMOUS
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2805 "RegOpenKeyEx failed with %d\n", error
);
2809 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
2810 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2812 ret
= pDuplicateTokenEx(ProcessToken
,
2813 TOKEN_QUERY
| TOKEN_DUPLICATE
| TOKEN_IMPERSONATE
, NULL
,
2814 SecurityAnonymous
, TokenImpersonation
, &Token
);
2815 ok(ret
, "DuplicateTokenEx failed with error %d\n", GetLastError());
2816 /* can't increase the impersonation level */
2817 ret
= DuplicateToken(Token
, SecurityIdentification
, &Token2
);
2818 error
= GetLastError();
2819 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2820 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2821 /* we can query anything from an anonymous token, including the user */
2822 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
2823 error
= GetLastError();
2824 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2825 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
2826 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
2827 ok(ret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2828 HeapFree(GetProcessHeap(), 0, User
);
2830 /* PrivilegeCheck fails with SecurityAnonymous level */
2831 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
2832 error
= GetLastError();
2833 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2834 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
2835 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
2836 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2838 PrivilegeSet
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[Privileges
->PrivilegeCount
]));
2839 PrivilegeSet
->PrivilegeCount
= Privileges
->PrivilegeCount
;
2840 memcpy(PrivilegeSet
->Privilege
, Privileges
->Privileges
, PrivilegeSet
->PrivilegeCount
* sizeof(PrivilegeSet
->Privilege
[0]));
2841 PrivilegeSet
->Control
= PRIVILEGE_SET_ALL_NECESSARY
;
2842 HeapFree(GetProcessHeap(), 0, Privileges
);
2844 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2845 error
= GetLastError();
2846 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2850 ret
= ImpersonateSelf(SecurityIdentification
);
2851 ok(ret
, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2852 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2853 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2855 /* can't perform access check when opening object against an identification impersonation token */
2856 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2858 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2859 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2861 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2862 ok(ret
, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2866 ret
= ImpersonateSelf(SecurityImpersonation
);
2867 ok(ret
, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2868 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2869 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2870 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2871 ok(error
== ERROR_SUCCESS
, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error
);
2873 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2874 ok(ret
, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2878 CloseHandle(ProcessToken
);
2880 HeapFree(GetProcessHeap(), 0, PrivilegeSet
);
2883 static void test_SetEntriesInAclW(void)
2886 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2887 PACL OldAcl
= NULL
, NewAcl
;
2888 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2889 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2890 EXPLICIT_ACCESSW ExplicitAccess
;
2891 static const WCHAR wszEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2892 static const WCHAR wszCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2894 if (!pSetEntriesInAclW
)
2896 win_skip("SetEntriesInAclW is not available\n");
2900 NewAcl
= (PACL
)0xdeadbeef;
2901 res
= pSetEntriesInAclW(0, NULL
, NULL
, &NewAcl
);
2902 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2904 win_skip("SetEntriesInAclW is not implemented\n");
2907 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2908 ok(NewAcl
== NULL
||
2909 broken(NewAcl
!= NULL
), /* NT4 */
2910 "NewAcl=%p, expected NULL\n", NewAcl
);
2913 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2914 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2915 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2917 win_skip("ACLs not implemented - skipping tests\n");
2918 HeapFree(GetProcessHeap(), 0, OldAcl
);
2921 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2923 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2924 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2926 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2927 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2928 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2930 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2931 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2933 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2934 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2935 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2936 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2937 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2938 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2939 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= 0xDEADBEEF;
2940 ExplicitAccess
.Trustee
.pMultipleTrustee
= (PVOID
)0xDEADBEEF;
2941 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2942 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2943 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2946 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2947 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2948 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2949 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2950 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2951 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2954 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2956 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2960 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2961 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszEveryone
;
2962 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2963 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2964 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2967 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2968 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2969 ok(res
== ERROR_INVALID_PARAMETER
||
2970 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2971 "SetEntriesInAclW failed: %u\n", res
);
2972 ok(NewAcl
== NULL
||
2973 broken(NewAcl
!= NULL
), /* NT4 */
2974 "returned acl wasn't NULL: %p\n", NewAcl
);
2976 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2977 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2978 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2979 ok(res
== ERROR_INVALID_PARAMETER
||
2980 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2981 "SetEntriesInAclW failed: %u\n", res
);
2982 ok(NewAcl
== NULL
||
2983 broken(NewAcl
!= NULL
), /* NT4 */
2984 "returned acl wasn't NULL: %p\n", NewAcl
);
2986 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2987 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2988 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2989 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2990 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2994 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2995 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszCurrentUser
;
2996 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2997 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2998 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3001 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
3002 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
3003 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
3004 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3005 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
3006 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3010 FreeSid(EveryoneSid
);
3011 HeapFree(GetProcessHeap(), 0, OldAcl
);
3014 static void test_SetEntriesInAclA(void)
3017 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
3018 PACL OldAcl
= NULL
, NewAcl
;
3019 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
3020 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
3021 EXPLICIT_ACCESSA ExplicitAccess
;
3022 static const CHAR szEveryone
[] = {'E','v','e','r','y','o','n','e',0};
3023 static const CHAR szCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
3025 if (!pSetEntriesInAclA
)
3027 win_skip("SetEntriesInAclA is not available\n");
3031 NewAcl
= (PACL
)0xdeadbeef;
3032 res
= pSetEntriesInAclA(0, NULL
, NULL
, &NewAcl
);
3033 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
3035 win_skip("SetEntriesInAclA is not implemented\n");
3038 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3039 ok(NewAcl
== NULL
||
3040 broken(NewAcl
!= NULL
), /* NT4 */
3041 "NewAcl=%p, expected NULL\n", NewAcl
);
3044 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
3045 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
3046 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3048 win_skip("ACLs not implemented - skipping tests\n");
3049 HeapFree(GetProcessHeap(), 0, OldAcl
);
3052 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
3054 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
3055 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3057 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
3058 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
3059 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3061 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
3062 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
3064 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
3065 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
3066 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
3067 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
3068 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
3069 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
3070 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
3071 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
3072 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3073 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3074 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3077 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
3078 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
3079 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
3080 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3081 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3082 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3085 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
3087 skip("Non-English locale (test with hardcoded 'Everyone')\n");
3091 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3092 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szEveryone
;
3093 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3094 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3095 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3098 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
3099 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3100 ok(res
== ERROR_INVALID_PARAMETER
||
3101 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
3102 "SetEntriesInAclA failed: %u\n", res
);
3103 ok(NewAcl
== NULL
||
3104 broken(NewAcl
!= NULL
), /* NT4 */
3105 "returned acl wasn't NULL: %p\n", NewAcl
);
3107 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3108 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
3109 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3110 ok(res
== ERROR_INVALID_PARAMETER
||
3111 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
3112 "SetEntriesInAclA failed: %u\n", res
);
3113 ok(NewAcl
== NULL
||
3114 broken(NewAcl
!= NULL
), /* NT4 */
3115 "returned acl wasn't NULL: %p\n", NewAcl
);
3117 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
3118 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
3119 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3120 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3121 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3125 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3126 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szCurrentUser
;
3127 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3128 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3129 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3132 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
3133 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
3134 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
3135 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3136 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3137 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3141 FreeSid(EveryoneSid
);
3142 HeapFree(GetProcessHeap(), 0, OldAcl
);
3145 static void test_CreateDirectoryA(void)
3147 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3148 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3149 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
3150 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3151 PSECURITY_DESCRIPTOR pSD
= &sd
;
3152 ACL_SIZE_INFORMATION acl_size
;
3153 ACCESS_ALLOWED_ACE
*ace
;
3154 SECURITY_ATTRIBUTES sa
;
3155 char tmpfile
[MAX_PATH
];
3156 char tmpdir
[MAX_PATH
];
3157 HANDLE token
, hTemp
;
3163 if (!pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3165 win_skip("Required functions are not available\n");
3169 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3171 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3172 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3176 win_skip("Failed to get current user token\n");
3179 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3180 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3181 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3182 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3183 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3184 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3185 CloseHandle( token
);
3186 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3188 sa
.nLength
= sizeof(sa
);
3189 sa
.lpSecurityDescriptor
= pSD
;
3190 sa
.bInheritHandle
= TRUE
;
3191 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3192 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3193 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3194 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3195 ok(bret
, "Failed to initialize ACL.\n");
3196 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3197 GENERIC_ALL
, user_sid
);
3198 ok(bret
, "Failed to add Current User to ACL.\n");
3199 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3200 GENERIC_ALL
, admin_sid
);
3201 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3202 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3203 ok(bret
, "Failed to add ACL to security desciptor.\n");
3205 GetTempPathA(MAX_PATH
, tmpdir
);
3206 lstrcatA(tmpdir
, "Please Remove Me");
3207 bret
= CreateDirectoryA(tmpdir
, &sa
);
3208 ok(bret
== TRUE
, "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
3209 HeapFree(GetProcessHeap(), 0, pDacl
);
3211 SetLastError(0xdeadbeef);
3212 error
= pGetNamedSecurityInfoA(tmpdir
, SE_FILE_OBJECT
,
3213 OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, (PSID
*)&owner
,
3214 NULL
, &pDacl
, NULL
, &pSD
);
3215 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3217 win_skip("GetNamedSecurityInfoA is not implemented\n");
3220 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3222 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3223 ok(bret
, "GetAclInformation failed\n");
3224 ok(acl_size
.AceCount
== 2, "GetAclInformation returned unexpected entry count (%d != 2).\n",
3226 if (acl_size
.AceCount
> 0)
3228 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3229 ok(bret
, "Failed to get Current User ACE.\n");
3230 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3231 ok(bret
, "Current User ACE != Current User SID.\n");
3232 ok(((ACE_HEADER
*)ace
)->AceFlags
== (OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
),
3233 "Current User ACE has unexpected flags (0x%x != 0x03)\n",
3234 ((ACE_HEADER
*)ace
)->AceFlags
);
3235 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3238 if (acl_size
.AceCount
> 1)
3240 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3241 ok(bret
, "Failed to get Administators Group ACE.\n");
3242 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3243 ok(bret
, "Administators Group ACE != Administators Group SID.\n");
3244 ok(((ACE_HEADER
*)ace
)->AceFlags
== (OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
),
3245 "Administators Group ACE has unexpected flags (0x%x != 0x03)\n",
3246 ((ACE_HEADER
*)ace
)->AceFlags
);
3247 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3252 /* Test inheritance of ACLs */
3253 strcpy(tmpfile
, tmpdir
);
3254 lstrcatA(tmpfile
, "/tmpfile");
3255 hTemp
= CreateFileA(tmpfile
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, CREATE_NEW
,
3256 FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
3257 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
,
3258 OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, (PSID
*)&owner
,
3259 NULL
, &pDacl
, NULL
, &pSD
);
3260 ok(error
== ERROR_SUCCESS
, "Failed to get permissions on file.\n");
3261 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3262 ok(bret
, "GetAclInformation failed\n");
3263 ok(acl_size
.AceCount
== 2, "GetAclInformation returned unexpected entry count (%d != 2).\n",
3265 if (acl_size
.AceCount
> 0)
3267 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3268 ok(bret
, "Inherited Failed to get Current User ACE.\n");
3269 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3270 ok(bret
, "Inherited Current User ACE != Current User SID.\n");
3271 ok(((ACE_HEADER
*)ace
)->AceFlags
== INHERITED_ACE
,
3272 "Inherited Current User ACE has unexpected flags (0x%x != 0x10)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3273 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3276 if (acl_size
.AceCount
> 1)
3278 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3279 ok(bret
, "Inherited Failed to get Administators Group ACE.\n");
3280 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3281 ok(bret
, "Inherited Administators Group ACE != Administators Group SID.\n");
3282 ok(((ACE_HEADER
*)ace
)->AceFlags
== INHERITED_ACE
,
3283 "Inherited Administators Group ACE has unexpected flags (0x%x != 0x10)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3284 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3290 HeapFree(GetProcessHeap(), 0, user
);
3291 bret
= RemoveDirectoryA(tmpdir
);
3292 ok(bret
== TRUE
, "RemoveDirectoryA should always succeed\n");
3295 static void test_GetNamedSecurityInfoA(void)
3297 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3298 char system_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3299 char users_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3300 PSID admin_sid
= (PSID
) admin_ptr
, users_sid
= (PSID
) users_ptr
;
3301 PSID system_sid
= (PSID
) system_ptr
, user_sid
;
3302 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3303 char invalid_path
[] = "/an invalid file path";
3304 int users_ace_id
= -1, admins_ace_id
= -1, i
;
3305 char software_key
[] = "MACHINE\\Software";
3306 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3307 SECURITY_DESCRIPTOR_CONTROL control
;
3308 ACL_SIZE_INFORMATION acl_size
;
3309 CHAR windows_dir
[MAX_PATH
];
3310 PSECURITY_DESCRIPTOR pSD
;
3311 ACCESS_ALLOWED_ACE
*ace
;
3312 BOOL bret
= TRUE
, isNT4
;
3313 char tmpfile
[MAX_PATH
];
3314 DWORD error
, revision
;
3315 BOOL owner_defaulted
;
3316 BOOL group_defaulted
;
3317 BOOL dacl_defaulted
;
3318 HANDLE token
, hTemp
;
3324 if (!pSetNamedSecurityInfoA
|| !pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3326 win_skip("Required functions are not available\n");
3330 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3332 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3333 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3337 win_skip("Failed to get current user token\n");
3340 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3341 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3342 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3343 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3344 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3345 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3346 CloseHandle( token
);
3347 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3349 bret
= GetWindowsDirectoryA(windows_dir
, MAX_PATH
);
3350 ok(bret
, "GetWindowsDirectory failed with error %d\n", GetLastError());
3352 SetLastError(0xdeadbeef);
3353 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,
3354 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
3355 NULL
, NULL
, NULL
, NULL
, &pSD
);
3356 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3358 win_skip("GetNamedSecurityInfoA is not implemented\n");
3359 HeapFree(GetProcessHeap(), 0, user
);
3362 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3364 bret
= GetSecurityDescriptorControl(pSD
, &control
, &revision
);
3365 ok(bret
, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
3366 ok((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == (SE_SELF_RELATIVE
|SE_DACL_PRESENT
) ||
3367 broken((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
), /* NT4 */
3368 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control
);
3369 ok(revision
== SECURITY_DESCRIPTOR_REVISION1
, "revision was %d instead of 1\n", revision
);
3371 isNT4
= (control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
;
3373 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3374 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3375 ok(owner
!= NULL
, "owner should not be NULL\n");
3377 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3378 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3379 ok(group
!= NULL
, "group should not be NULL\n");
3383 /* NULL descriptor tests */
3386 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
3387 HeapFree(GetProcessHeap(), 0, user
);
3391 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3392 NULL
, NULL
, NULL
, NULL
, NULL
);
3393 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3396 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3397 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3398 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3399 ok(pDacl
!= NULL
, "DACL should not be NULL\n");
3402 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,OWNER_SECURITY_INFORMATION
,
3403 NULL
, NULL
, &pDacl
, NULL
, NULL
);
3404 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3406 /* Test behavior of SetNamedSecurityInfo with an invalid path */
3407 SetLastError(0xdeadbeef);
3408 error
= pSetNamedSecurityInfoA(invalid_path
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3410 ok(error
== ERROR_FILE_NOT_FOUND
, "Unexpected error returned: 0x%x\n", error
);
3411 ok(GetLastError() == 0xdeadbeef, "Expected last error to remain unchanged.\n");
3413 /* Create security descriptor information and test that it comes back the same */
3415 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3416 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3417 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3418 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3419 ok(bret
, "Failed to initialize ACL.\n");
3420 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3421 ok(bret
, "Failed to add Current User to ACL.\n");
3422 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
3423 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3424 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3425 ok(bret
, "Failed to add ACL to security desciptor.\n");
3426 GetTempFileNameA(".", "foo", 0, tmpfile
);
3427 hTemp
= CreateFileA(tmpfile
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
3428 FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
3429 SetLastError(0xdeadbeef);
3430 error
= pSetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3432 HeapFree(GetProcessHeap(), 0, pDacl
);
3433 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3435 win_skip("SetNamedSecurityInfoA is not implemented\n");
3436 HeapFree(GetProcessHeap(), 0, user
);
3440 ok(!error
, "SetNamedSecurityInfoA failed with error %d\n", error
);
3441 SetLastError(0xdeadbeef);
3442 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3443 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3444 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3446 win_skip("GetNamedSecurityInfoA is not implemented\n");
3447 HeapFree(GetProcessHeap(), 0, user
);
3451 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3453 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3454 ok(bret
, "GetAclInformation failed\n");
3455 if (acl_size
.AceCount
> 0)
3457 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3458 ok(bret
, "Failed to get Current User ACE.\n");
3459 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3460 ok(bret
, "Current User ACE != Current User SID.\n");
3461 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3462 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3463 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3466 if (acl_size
.AceCount
> 1)
3468 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3469 ok(bret
, "Failed to get Administators Group ACE.\n");
3470 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3471 ok(bret
|| broken(!bret
) /* win2k */, "Administators Group ACE != Administators Group SID.\n");
3472 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3473 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3474 ok(ace
->Mask
== 0x1f01ff || broken(ace
->Mask
== GENERIC_ALL
) /* win2k */,
3475 "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n", ace
->Mask
);
3480 /* Create security descriptor with no inheritance and test that it comes back the same */
3482 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3483 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3484 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3485 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3486 ok(bret
, "Failed to initialize ACL.\n");
3487 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3488 ok(bret
, "Failed to add Current User to ACL.\n");
3489 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
3490 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3491 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3492 ok(bret
, "Failed to add ACL to security desciptor.\n");
3493 GetTempFileNameA(".", "foo", 0, tmpfile
);
3494 hTemp
= CreateFileA(tmpfile
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
3495 FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
3496 SetLastError(0xdeadbeef);
3497 error
= pSetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
,
3498 DACL_SECURITY_INFORMATION
|PROTECTED_DACL_SECURITY_INFORMATION
,
3499 NULL
, NULL
, pDacl
, NULL
);
3500 HeapFree(GetProcessHeap(), 0, pDacl
);
3501 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3503 win_skip("SetNamedSecurityInfoA is not implemented\n");
3504 HeapFree(GetProcessHeap(), 0, user
);
3508 ok(!error
, "SetNamedSecurityInfoA failed with error %d\n", error
);
3509 SetLastError(0xdeadbeef);
3510 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3511 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3512 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3514 win_skip("GetNamedSecurityInfoA is not implemented\n");
3515 HeapFree(GetProcessHeap(), 0, user
);
3519 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3521 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3522 ok(bret
, "GetAclInformation failed\n");
3523 if (acl_size
.AceCount
> 0)
3525 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3526 ok(bret
, "Failed to get Current User ACE.\n");
3527 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3528 ok(bret
, "Current User ACE != Current User SID.\n");
3529 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3530 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3531 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3534 if (acl_size
.AceCount
> 1)
3536 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3537 ok(bret
, "Failed to get Administators Group ACE.\n");
3538 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3539 ok(bret
|| broken(!bret
) /* win2k */, "Administators Group ACE != Administators Group SID.\n");
3540 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3541 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3542 ok(ace
->Mask
== 0x1f01ff || broken(ace
->Mask
== GENERIC_ALL
) /* win2k */,
3543 "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n", ace
->Mask
);
3546 HeapFree(GetProcessHeap(), 0, user
);
3549 /* Test querying the ownership of a built-in registry key */
3550 sid_size
= sizeof(system_ptr
);
3551 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, system_sid
, &sid_size
);
3552 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
,
3553 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
,
3554 NULL
, NULL
, NULL
, NULL
, &pSD
);
3555 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3557 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3558 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3559 ok(owner
!= NULL
, "owner should not be NULL\n");
3560 ok(EqualSid(owner
, admin_sid
), "MACHINE\\Software owner SID != Administrators SID.\n");
3562 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3563 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3564 ok(group
!= NULL
, "group should not be NULL\n");
3565 ok(EqualSid(group
, admin_sid
) || broken(EqualSid(group
, system_sid
)) /* before Win7 */
3566 || broken(((SID
*)group
)->SubAuthority
[0] == SECURITY_NT_NON_UNIQUE
) /* Vista */,
3567 "MACHINE\\Software group SID != Local System SID.\n");
3570 /* Test querying the DACL of a built-in registry key */
3571 sid_size
= sizeof(users_ptr
);
3572 pCreateWellKnownSid(WinBuiltinUsersSid
, NULL
, users_sid
, &sid_size
);
3573 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
, DACL_SECURITY_INFORMATION
,
3574 NULL
, NULL
, NULL
, NULL
, &pSD
);
3575 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3577 bret
= GetSecurityDescriptorDacl(pSD
, &dacl_present
, &pDacl
, &dacl_defaulted
);
3578 ok(bret
, "GetSecurityDescriptorDacl failed with error %d\n", GetLastError());
3579 ok(dacl_present
, "DACL should be present\n");
3580 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityDescriptorDacl returned invalid DACL.\n");
3581 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3582 ok(bret
, "GetAclInformation failed\n");
3583 ok(acl_size
.AceCount
!= 0, "GetAclInformation returned no ACLs\n");
3584 for (i
=0; i
<acl_size
.AceCount
; i
++)
3586 bret
= pGetAce(pDacl
, i
, (VOID
**)&ace
);
3587 ok(bret
, "Failed to get ACE %d.\n", i
);
3588 bret
= EqualSid(&ace
->SidStart
, users_sid
);
3589 if (bret
) users_ace_id
= i
;
3590 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3591 if (bret
) admins_ace_id
= i
;
3593 ok(users_ace_id
!= -1 || broken(users_ace_id
== -1) /* win2k */,
3594 "Bultin Users ACE not found.\n");
3595 if (users_ace_id
!= -1)
3597 bret
= pGetAce(pDacl
, users_ace_id
, (VOID
**)&ace
);
3598 ok(bret
, "Failed to get Builtin Users ACE.\n");
3599 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3600 ok(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
)
3601 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */,
3602 "Builtin Users ACE has unexpected flags (0x%x != 0x%x)\n", flags
,
3603 INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
);
3604 ok(ace
->Mask
== GENERIC_READ
, "Builtin Users ACE has unexpected mask (0x%x != 0x%x)\n",
3605 ace
->Mask
, GENERIC_READ
);
3607 ok(admins_ace_id
!= -1, "Bultin Admins ACE not found.\n");
3608 if (admins_ace_id
!= -1)
3610 bret
= pGetAce(pDacl
, admins_ace_id
, (VOID
**)&ace
);
3611 ok(bret
, "Failed to get Builtin Admins ACE.\n");
3612 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3614 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */,
3615 "Builtin Admins ACE has unexpected flags (0x%x != 0x0)\n", flags
);
3616 ok(ace
->Mask
== KEY_ALL_ACCESS
|| broken(ace
->Mask
== GENERIC_ALL
) /* w2k8 */,
3617 "Builtin Admins ACE has unexpected mask (0x%x != 0x%x)\n", ace
->Mask
, KEY_ALL_ACCESS
);
3622 static void test_ConvertStringSecurityDescriptor(void)
3625 PSECURITY_DESCRIPTOR pSD
;
3626 static const WCHAR Blank
[] = { 0 };
3630 const char *sidstring
;
3637 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE
, ERROR_UNKNOWN_REVISION
},
3638 /* test ACE string type */
3639 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3640 { "D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3641 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_PARAMETER
},
3642 /* test ACE string with spaces */
3643 { " D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3644 { "D: (D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3645 { "D:( D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3646 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, RPC_S_INVALID_STRING_UUID
, ERROR_INVALID_ACL
}, /* Vista+ */
3647 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3648 { "D:(D;; GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3649 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3650 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1
, TRUE
},
3651 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1
, TRUE
},
3652 { "D:(D;;GA;;; WD)", SDDL_REVISION_1
, TRUE
},
3653 { "D:(D;;GA;;;WD )", SDDL_REVISION_1
, TRUE
},
3654 /* test ACE string access rights */
3655 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3656 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1
, TRUE
},
3657 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1
, TRUE
},
3658 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1
, TRUE
},
3659 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1
, TRUE
},
3660 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1
, TRUE
},
3661 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3662 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3663 /* test ACE string access right error case */
3664 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3665 /* test behaviour with empty strings */
3666 { "", SDDL_REVISION_1
, TRUE
},
3667 /* test ACE string SID */
3668 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1
, TRUE
},
3669 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
, ERROR_INVALID_SID
} /* W2K */
3672 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3674 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3678 for (i
= 0; i
< sizeof(cssd
)/sizeof(cssd
[0]); i
++)
3682 SetLastError(0xdeadbeef);
3683 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3684 cssd
[i
].sidstring
, cssd
[i
].revision
, &pSD
, NULL
);
3685 GLE
= GetLastError();
3686 ok(ret
== cssd
[i
].ret
, "(%02u) Expected %s (%d)\n", i
, cssd
[i
].ret
? "success" : "failure", GLE
);
3688 ok(GLE
== cssd
[i
].GLE
||
3689 (cssd
[i
].altGLE
&& GLE
== cssd
[i
].altGLE
),
3690 "(%02u) Unexpected last error %d\n", i
, GLE
);
3695 /* test behaviour with NULL parameters */
3696 SetLastError(0xdeadbeef);
3697 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3698 NULL
, 0xdeadbeef, &pSD
, NULL
);
3700 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3701 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3704 SetLastError(0xdeadbeef);
3705 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3706 NULL
, 0xdeadbeef, &pSD
, NULL
);
3707 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3708 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3711 SetLastError(0xdeadbeef);
3712 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3713 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL
, NULL
);
3714 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3715 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3718 SetLastError(0xdeadbeef);
3719 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3720 "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, NULL
, NULL
);
3721 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3722 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3725 /* test behaviour with empty strings */
3726 SetLastError(0xdeadbeef);
3727 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3728 Blank
, SDDL_REVISION_1
, &pSD
, NULL
);
3729 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
3733 static void test_ConvertSecurityDescriptorToString(void)
3735 SECURITY_DESCRIPTOR desc
;
3736 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3745 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA
)
3747 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
3750 if (!pCreateWellKnownSid
)
3752 win_skip("CreateWellKnownSid is not available\n");
3756 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
3757 * don't replicate this feature so we only test len >= strlen+1. */
3758 #define CHECK_RESULT_AND_FREE(exp_str) \
3759 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
3760 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlenA(exp_str) + 1, len); \
3763 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
3764 ok(strcmp(string, (exp_str1)) == 0 || strcmp(string, (exp_str2)) == 0, "String mismatch (expected\n\"%s\" or\n\"%s\", got\n\"%s\")\n", (exp_str1), (exp_str2), string); \
3765 ok(len >= (strlen(exp_str1) + 1) || len >= (strlen(exp_str2) + 1), "Length mismatch (expected %d or %d, got %d)\n", lstrlenA(exp_str1) + 1, lstrlenA(exp_str2) + 1, len); \
3768 InitializeSecurityDescriptor(&desc
, SECURITY_DESCRIPTOR_REVISION
);
3769 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3770 CHECK_RESULT_AND_FREE("");
3773 pCreateWellKnownSid(WinLocalSid
, NULL
, sid_buf
, &size
);
3774 SetSecurityDescriptorOwner(&desc
, sid_buf
, FALSE
);
3775 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3776 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3778 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3779 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3780 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3782 size
= sizeof(sid_buf
);
3783 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, sid_buf
, &size
);
3784 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3785 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3786 CHECK_RESULT_AND_FREE("O:SY");
3788 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
);
3789 SetSecurityDescriptorGroup(&desc
, psid
, TRUE
);
3790 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3791 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
3793 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, GROUP_SECURITY_INFORMATION
, &string
, &len
), "Conversion failed\n");
3794 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3796 pacl
= (PACL
)acl_buf
;
3797 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3798 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, TRUE
);
3799 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3800 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3802 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, FALSE
);
3803 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3804 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3806 pConvertStringSidToSidA("S-1-5-6", &psid2
);
3807 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, 0xf0000000, psid2
);
3808 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3809 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
3811 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, INHERIT_ONLY_ACE
|INHERITED_ACE
, 0x00000003, psid2
);
3812 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3813 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
3815 pAddAccessDeniedAceEx(pacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
, 0xffffffff, psid
);
3816 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3817 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)");
3820 pacl
= (PACL
)acl_buf
;
3821 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3822 SetSecurityDescriptorSacl(&desc
, TRUE
, pacl
, FALSE
);
3823 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3824 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
3826 /* fails in win2k */
3827 SetSecurityDescriptorDacl(&desc
, TRUE
, NULL
, FALSE
);
3828 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, VALID_INHERIT_FLAGS
, KEY_READ
|KEY_WRITE
, psid2
, TRUE
, TRUE
);
3829 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3831 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
3832 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
3835 /* fails in win2k */
3836 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, psid2
, TRUE
, FALSE
);
3837 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3839 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
3840 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
3847 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec
)
3849 SECURITY_DESCRIPTOR_CONTROL ref
;
3850 SECURITY_DESCRIPTOR_CONTROL test
;
3852 SECURITY_DESCRIPTOR_CONTROL
const mutable
3853 = SE_DACL_AUTO_INHERIT_REQ
| SE_SACL_AUTO_INHERIT_REQ
3854 | SE_DACL_AUTO_INHERITED
| SE_SACL_AUTO_INHERITED
3855 | SE_DACL_PROTECTED
| SE_SACL_PROTECTED
3856 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
3858 SECURITY_DESCRIPTOR_CONTROL
const immutable
3859 = SE_OWNER_DEFAULTED
| SE_GROUP_DEFAULTED
3860 | SE_DACL_PRESENT
| SE_DACL_DEFAULTED
3861 | SE_SACL_PRESENT
| SE_SACL_DEFAULTED
3862 | SE_RM_CONTROL_VALID
| SE_SELF_RELATIVE
3867 LPCSTR fmt
= "Expected error %s, got %u\n";
3869 GetSecurityDescriptorControl (sec
, &ref
, &dwRevision
);
3871 /* The mutable bits are mutable regardless of the truth of
3872 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
3874 /* Check call barfs if any bit-of-interest is immutable */
3875 for (bit
= 0; bit
< 16; ++bit
)
3877 SECURITY_DESCRIPTOR_CONTROL
const bitOfInterest
= 1 << bit
;
3878 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitOfInterest
;
3880 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3882 DWORD dwExpect
= (bitOfInterest
& immutable
)
3883 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3884 LPCSTR strExpect
= (bitOfInterest
& immutable
)
3885 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3887 ctrl
= (bitOfInterest
& mutable) ? ref
+ bitOfInterest
: ref
;
3888 setOrClear
^= bitOfInterest
;
3889 SetLastError (0xbebecaca);
3890 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3891 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3892 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3893 expect_eq(test
, ctrl
, int, "%x");
3895 setOrClear
^= bitOfInterest
;
3896 SetLastError (0xbebecaca);
3897 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3898 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3899 GetSecurityDescriptorControl (sec
, &test
, &dwRevision
);
3900 expect_eq(test
, ref
, int, "%x");
3903 /* Check call barfs if any bit-to-set is immutable
3904 even when not a bit-of-interest */
3905 for (bit
= 0; bit
< 16; ++bit
)
3907 SECURITY_DESCRIPTOR_CONTROL
const bitsOfInterest
= mutable;
3908 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitsOfInterest
;
3910 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3912 DWORD dwExpect
= ((1 << bit
) & immutable
)
3913 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3914 LPCSTR strExpect
= ((1 << bit
) & immutable
)
3915 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3917 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| mutable;
3918 setOrClear
^= bitsOfInterest
;
3919 SetLastError (0xbebecaca);
3920 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3921 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3922 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3923 expect_eq(test
, ctrl
, int, "%x");
3925 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| (1 << bit
);
3926 setOrClear
^= bitsOfInterest
;
3927 SetLastError (0xbebecaca);
3928 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3929 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3930 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3931 expect_eq(test
, ctrl
, int, "%x");
3935 static void test_PrivateObjectSecurity(void)
3937 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3938 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3939 PSECURITY_DESCRIPTOR sec
;
3945 PSECURITY_DESCRIPTOR buf
;
3948 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3950 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3954 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3956 "G:S-1-5-21-93476-23408-4576"
3957 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3958 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3959 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3960 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3962 test_SetSecurityDescriptorControl(sec
);
3966 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3968 "G:S-1-5-21-93476-23408-4576",
3969 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3971 test_SetSecurityDescriptorControl(sec
);
3975 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3977 "G:S-1-5-21-93476-23408-4576"
3978 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3979 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3980 buf
= HeapAlloc(GetProcessHeap(), 0, dwDescSize
);
3981 pSetSecurityDescriptorControl(sec
, SE_DACL_PROTECTED
, SE_DACL_PROTECTED
);
3982 GetSecurityDescriptorControl(sec
, &ctrl
, &dwRevision
);
3983 expect_eq(ctrl
, 0x9014, int, "%x");
3985 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3986 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3987 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3988 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3989 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3990 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3991 expect_eq(ctrl
, 0x8000, int, "%x");
3993 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3994 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3995 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3996 ret
= pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
);
3997 ok(ret
, "Conversion failed err=%u\n", GetLastError());
3998 CHECK_ONE_OF_AND_FREE("G:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)",
3999 "G:S-1-5-21-93476-23408-4576D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"); /* Win7 */
4000 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
4001 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8004, int, "%x");
4003 ret
= GetPrivateObjectSecurity(sec
, sec_info
, buf
, dwDescSize
, &retSize
);
4004 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
4005 ok(retSize
== dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
4006 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4007 CHECK_ONE_OF_AND_FREE("O:SY"
4008 "G:S-1-5-21-93476-23408-4576"
4009 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
4010 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
4012 "G:S-1-5-21-93476-23408-4576"
4013 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
4014 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
4015 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
4016 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8014, int, "%x");
4018 SetLastError(0xdeadbeef);
4019 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, 5, &retSize
) == FALSE
, "GetPrivateObjectSecurity should have failed\n");
4020 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
4023 HeapFree(GetProcessHeap(), 0, buf
);
4025 #undef CHECK_RESULT_AND_FREE
4026 #undef CHECK_ONE_OF_AND_FREE
4028 static void test_acls(void)
4031 PACL pAcl
= (PACL
)buffer
;
4034 SetLastError(0xdeadbeef);
4035 ret
= InitializeAcl(pAcl
, sizeof(ACL
) - 1, ACL_REVISION
);
4036 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
4038 win_skip("InitializeAcl is not implemented\n");
4042 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
4044 SetLastError(0xdeadbeef);
4045 ret
= InitializeAcl(pAcl
, 0xffffffff, ACL_REVISION
);
4046 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
4048 SetLastError(0xdeadbeef);
4049 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION1
);
4050 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
4052 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION2
);
4053 ok(ret
, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
4055 ret
= IsValidAcl(pAcl
);
4056 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
4058 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION3
);
4059 ok(ret
, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
4061 ret
= IsValidAcl(pAcl
);
4062 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
4064 SetLastError(0xdeadbeef);
4065 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION4
);
4066 if (GetLastError() != ERROR_INVALID_PARAMETER
)
4068 ok(ret
, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
4070 ret
= IsValidAcl(pAcl
);
4071 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
4074 win_skip("ACL_REVISION4 is not implemented on NT4\n");
4076 SetLastError(0xdeadbeef);
4077 ret
= InitializeAcl(pAcl
, sizeof(buffer
), -1);
4078 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(-1) failed with error %d\n", GetLastError());
4081 static void test_GetSecurityInfo(void)
4083 char domain_users_ptr
[sizeof(TOKEN_USER
) + sizeof(SID
) + sizeof(DWORD
)*SID_MAX_SUB_AUTHORITIES
];
4084 char b
[sizeof(TOKEN_USER
) + sizeof(SID
) + sizeof(DWORD
)*SID_MAX_SUB_AUTHORITIES
];
4085 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], dacl
[100];
4086 PSID domain_users_sid
= (PSID
) domain_users_ptr
, domain_sid
;
4087 SID_IDENTIFIER_AUTHORITY sia
= { SECURITY_NT_AUTHORITY
};
4088 int domain_users_ace_id
= -1, admins_ace_id
= -1, i
;
4089 DWORD sid_size
= sizeof(admin_ptr
), l
= sizeof(b
);
4090 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
4091 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
4092 BOOL owner_defaulted
, group_defaulted
;
4093 BOOL dacl_defaulted
, dacl_present
;
4094 ACL_SIZE_INFORMATION acl_size
;
4095 PSECURITY_DESCRIPTOR pSD
;
4096 ACCESS_ALLOWED_ACE
*ace
;
4104 if (!pGetSecurityInfo
|| !pSetSecurityInfo
)
4106 win_skip("[Get|Set]SecurityInfo is not available\n");
4110 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
4112 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
4113 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
4117 win_skip("Failed to get current user token\n");
4120 GetTokenInformation(token
, TokenUser
, b
, l
, &l
);
4121 CloseHandle( token
);
4122 user_sid
= ((TOKEN_USER
*)b
)->User
.Sid
;
4124 /* Create something. Files have lots of associated security info. */
4125 obj
= CreateFileA(myARGV
[0], GENERIC_READ
|WRITE_DAC
, FILE_SHARE_READ
, NULL
,
4126 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
4127 if (obj
== INVALID_HANDLE_VALUE
)
4129 skip("Couldn't create an object for GetSecurityInfo test\n");
4133 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
4134 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4135 &owner
, &group
, &pDacl
, NULL
, &pSD
);
4136 if (ret
== ERROR_CALL_NOT_IMPLEMENTED
)
4138 win_skip("GetSecurityInfo is not implemented\n");
4142 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
4143 ok(pSD
!= NULL
, "GetSecurityInfo\n");
4144 ok(owner
!= NULL
, "GetSecurityInfo\n");
4145 ok(group
!= NULL
, "GetSecurityInfo\n");
4147 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
4149 win_skip("No ACL information returned\n");
4153 if (!pCreateWellKnownSid
)
4155 win_skip("NULL parameter test would crash on NT4\n");
4160 /* If we don't ask for the security descriptor, Windows will still give us
4161 the other stuff, leaving us no way to free it. */
4162 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
4163 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4164 &owner
, &group
, &pDacl
, NULL
, NULL
);
4165 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
4166 ok(owner
!= NULL
, "GetSecurityInfo\n");
4167 ok(group
!= NULL
, "GetSecurityInfo\n");
4169 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
4171 win_skip("No ACL information returned\n");
4173 /* Create security descriptor information and test that it comes back the same */
4175 pDacl
= (PACL
)&dacl
;
4176 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
4177 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
4178 bret
= InitializeAcl(pDacl
, sizeof(dacl
), ACL_REVISION
);
4179 ok(bret
, "Failed to initialize ACL.\n");
4180 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
4181 ok(bret
, "Failed to add Current User to ACL.\n");
4182 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
4183 ok(bret
, "Failed to add Administrator Group to ACL.\n");
4184 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
4185 ok(bret
, "Failed to add ACL to security desciptor.\n");
4186 ret
= pSetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
4187 NULL
, NULL
, pDacl
, NULL
);
4188 ok(ret
== ERROR_SUCCESS
, "SetSecurityInfo returned %d\n", ret
);
4189 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
4190 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
4191 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
4192 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityInfo returned invalid DACL.\n");
4193 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
4194 ok(bret
, "GetAclInformation failed\n");
4195 if (acl_size
.AceCount
> 0)
4197 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
4198 ok(bret
, "Failed to get Current User ACE.\n");
4199 bret
= EqualSid(&ace
->SidStart
, user_sid
);
4200 ok(bret
, "Current User ACE != Current User SID.\n");
4201 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
4202 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
4203 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
4206 if (acl_size
.AceCount
> 1)
4208 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
4209 ok(bret
, "Failed to get Administators Group ACE.\n");
4210 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
4211 ok(bret
, "Administators Group ACE != Administators Group SID.\n");
4212 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
4213 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
4214 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
4220 /* Obtain the "domain users" SID from the user SID */
4221 if (!AllocateAndInitializeSid(&sia
, 4, *GetSidSubAuthority(user_sid
, 0),
4222 *GetSidSubAuthority(user_sid
, 1),
4223 *GetSidSubAuthority(user_sid
, 2),
4224 *GetSidSubAuthority(user_sid
, 3), 0, 0, 0, 0, &domain_sid
))
4226 win_skip("Failed to get current domain SID\n");
4229 sid_size
= sizeof(domain_users_ptr
);
4230 pCreateWellKnownSid(WinAccountDomainUsersSid
, domain_sid
, domain_users_sid
, &sid_size
);
4231 FreeSid(domain_sid
);
4233 /* Test querying the ownership of a process */
4234 ret
= pGetSecurityInfo(GetCurrentProcess(), SE_KERNEL_OBJECT
,
4235 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
,
4236 NULL
, NULL
, NULL
, NULL
, &pSD
);
4237 ok(!ret
, "GetNamedSecurityInfo failed with error %d\n", ret
);
4239 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
4240 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
4241 ok(owner
!= NULL
, "owner should not be NULL\n");
4242 ok(EqualSid(owner
, admin_sid
) || EqualSid(owner
, user_sid
),
4243 "Process owner SID != Administrators SID.\n");
4245 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
4246 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
4247 ok(group
!= NULL
, "group should not be NULL\n");
4248 ok(EqualSid(group
, domain_users_sid
), "Process group SID != Domain Users SID.\n");
4251 /* Test querying the DACL of a process */
4252 ret
= pGetSecurityInfo(GetCurrentProcess(), SE_KERNEL_OBJECT
, DACL_SECURITY_INFORMATION
,
4253 NULL
, NULL
, NULL
, NULL
, &pSD
);
4254 ok(!ret
, "GetSecurityInfo failed with error %d\n", ret
);
4256 bret
= GetSecurityDescriptorDacl(pSD
, &dacl_present
, &pDacl
, &dacl_defaulted
);
4257 ok(bret
, "GetSecurityDescriptorDacl failed with error %d\n", GetLastError());
4258 ok(dacl_present
, "DACL should be present\n");
4259 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityDescriptorDacl returned invalid DACL.\n");
4260 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
4261 ok(bret
, "GetAclInformation failed\n");
4262 ok(acl_size
.AceCount
!= 0, "GetAclInformation returned no ACLs\n");
4263 for (i
=0; i
<acl_size
.AceCount
; i
++)
4265 bret
= pGetAce(pDacl
, i
, (VOID
**)&ace
);
4266 ok(bret
, "Failed to get ACE %d.\n", i
);
4267 bret
= EqualSid(&ace
->SidStart
, domain_users_sid
);
4268 if (bret
) domain_users_ace_id
= i
;
4269 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
4270 if (bret
) admins_ace_id
= i
;
4272 ok(domain_users_ace_id
!= -1 || broken(domain_users_ace_id
== -1) /* win2k */,
4273 "Domain Users ACE not found.\n");
4274 if (domain_users_ace_id
!= -1)
4276 bret
= pGetAce(pDacl
, domain_users_ace_id
, (VOID
**)&ace
);
4277 ok(bret
, "Failed to get Domain Users ACE.\n");
4278 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
4279 ok(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
),
4280 "Domain Users ACE has unexpected flags (0x%x != 0x%x)\n", flags
,
4281 INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
);
4282 ok(ace
->Mask
== GENERIC_READ
, "Domain Users ACE has unexpected mask (0x%x != 0x%x)\n",
4283 ace
->Mask
, GENERIC_READ
);
4285 ok(admins_ace_id
!= -1 || broken(admins_ace_id
== -1) /* xp */,
4286 "Builtin Admins ACE not found.\n");
4287 if (admins_ace_id
!= -1)
4289 bret
= pGetAce(pDacl
, admins_ace_id
, (VOID
**)&ace
);
4290 ok(bret
, "Failed to get Builtin Admins ACE.\n");
4291 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
4292 ok(flags
== 0x0, "Builtin Admins ACE has unexpected flags (0x%x != 0x0)\n", flags
);
4293 ok(ace
->Mask
== PROCESS_ALL_ACCESS
|| broken(ace
->Mask
== 0x1f0fff) /* win2k */,
4294 "Builtin Admins ACE has unexpected mask (0x%x != 0x%x)\n", ace
->Mask
, PROCESS_ALL_ACCESS
);
4299 static void test_GetSidSubAuthority(void)
4303 if (!pGetSidSubAuthority
|| !pConvertStringSidToSidA
|| !pIsValidSid
|| !pGetSidSubAuthorityCount
)
4305 win_skip("Some functions not available\n");
4308 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
4309 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
4310 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid
),"ConvertStringSidToSidA failed\n");
4311 ok(pIsValidSid(psid
),"Sid is not valid\n");
4312 SetLastError(0xbebecaca);
4313 ok(*pGetSidSubAuthorityCount(psid
) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid
));
4314 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4315 SetLastError(0xbebecaca);
4316 ok(*pGetSidSubAuthority(psid
,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid
,0));
4317 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4318 SetLastError(0xbebecaca);
4319 ok(*pGetSidSubAuthority(psid
,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid
,1));
4320 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4321 SetLastError(0xbebecaca);
4322 ok(pGetSidSubAuthority(psid
,4) != NULL
,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
4323 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4327 static void test_CheckTokenMembership(void)
4329 PTOKEN_GROUPS token_groups
;
4331 HANDLE process_token
, token
;
4336 if (!pCheckTokenMembership
)
4338 win_skip("CheckTokenMembership is not available\n");
4341 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4342 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
4344 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
4345 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
4348 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4349 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4350 "GetTokenInformation(TokenGroups) %s with error %d\n",
4351 ret
? "succeeded" : "failed", GetLastError());
4352 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4353 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4354 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
4356 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4358 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4362 if (i
== token_groups
->GroupCount
)
4364 HeapFree(GetProcessHeap(), 0, token_groups
);
4366 skip("user not a member of any group\n");
4371 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4372 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4373 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4376 ret
= pCheckTokenMembership(NULL
, token_groups
->Groups
[i
].Sid
, &is_member
);
4377 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4378 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4381 SetLastError(0xdeadbeef);
4382 ret
= pCheckTokenMembership(process_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4383 ok(!ret
&& GetLastError() == ERROR_NO_IMPERSONATION_TOKEN
,
4384 "CheckTokenMembership with process token %s with error %d\n",
4385 ret
? "succeeded" : "failed", GetLastError());
4386 ok(!is_member
, "CheckTokenMembership should have cleared is_member\n");
4388 HeapFree(GetProcessHeap(), 0, token_groups
);
4390 CloseHandle(process_token
);
4393 static void test_EqualSid(void)
4397 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
4398 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
4400 SetLastError(0xdeadbeef);
4401 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4402 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid1
);
4403 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
4405 win_skip("AllocateAndInitializeSid is not implemented\n");
4408 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4409 ok(GetLastError() == 0xdeadbeef,
4410 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
4413 ret
= AllocateAndInitializeSid(&SIDAuthWorld
, 1, SECURITY_WORLD_RID
,
4414 0, 0, 0, 0, 0, 0, 0, &sid2
);
4415 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4417 SetLastError(0xdeadbeef);
4418 ret
= EqualSid(sid1
, sid2
);
4419 ok(!ret
, "World and domain admins sids shouldn't have been equal\n");
4420 ok(GetLastError() == ERROR_SUCCESS
||
4421 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4422 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4425 SetLastError(0xdeadbeef);
4426 sid2
= FreeSid(sid2
);
4427 ok(!sid2
, "FreeSid should have returned NULL instead of %p\n", sid2
);
4428 ok(GetLastError() == 0xdeadbeef,
4429 "FreeSid shouldn't have set last error to %d\n",
4432 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4433 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid2
);
4434 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4436 SetLastError(0xdeadbeef);
4437 ret
= EqualSid(sid1
, sid2
);
4438 ok(ret
, "Same sids should have been equal\n");
4439 ok(GetLastError() == ERROR_SUCCESS
||
4440 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4441 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4444 ((SID
*)sid2
)->Revision
= 2;
4445 SetLastError(0xdeadbeef);
4446 ret
= EqualSid(sid1
, sid2
);
4447 ok(!ret
, "EqualSid with invalid sid should have returned FALSE\n");
4448 ok(GetLastError() == ERROR_SUCCESS
||
4449 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4450 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4452 ((SID
*)sid2
)->Revision
= SID_REVISION
;
4458 static void test_GetUserNameA(void)
4460 char buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4461 DWORD required_len
, buffer_len
;
4464 /* Test crashes on Windows. */
4467 SetLastError(0xdeadbeef);
4468 GetUserNameA(NULL
, NULL
);
4471 SetLastError(0xdeadbeef);
4473 ret
= GetUserNameA(NULL
, &required_len
);
4474 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4475 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4476 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4478 SetLastError(0xdeadbeef);
4480 ret
= GetUserNameA(NULL
, &required_len
);
4481 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4482 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4483 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4485 /* Tests crashes on Windows. */
4488 SetLastError(0xdeadbeef);
4489 required_len
= UNLEN
+ 1;
4490 GetUserNameA(NULL
, &required_len
);
4492 SetLastError(0xdeadbeef);
4493 GetUserNameA(buffer
, NULL
);
4496 memset(filler
, 'x', sizeof(filler
));
4498 /* Note that GetUserNameA on XP and newer outputs the number of bytes
4499 * required for a Unicode string, which affects a test in the next block. */
4500 SetLastError(0xdeadbeef);
4501 memcpy(buffer
, filler
, sizeof(filler
));
4503 ret
= GetUserNameA(buffer
, &required_len
);
4504 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4505 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4506 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4507 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4509 SetLastError(0xdeadbeef);
4510 memcpy(buffer
, filler
, sizeof(filler
));
4511 buffer_len
= required_len
;
4512 ret
= GetUserNameA(buffer
, &buffer_len
);
4513 ok(ret
== TRUE
, "GetUserNameA returned %d, last error %u\n", ret
, GetLastError());
4514 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4515 ok(buffer_len
== required_len
||
4516 broken(buffer_len
== required_len
/ sizeof(WCHAR
)), /* XP+ */
4517 "Outputted buffer length was %u\n", buffer_len
);
4519 /* Use the reported buffer size from the last GetUserNameA call and pass
4520 * a length that is one less than the required value. */
4521 SetLastError(0xdeadbeef);
4522 memcpy(buffer
, filler
, sizeof(filler
));
4524 ret
= GetUserNameA(buffer
, &buffer_len
);
4525 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4526 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was untouched\n");
4527 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4528 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4531 static void test_GetUserNameW(void)
4533 WCHAR buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4534 DWORD required_len
, buffer_len
;
4537 /* Test crashes on Windows. */
4540 SetLastError(0xdeadbeef);
4541 GetUserNameW(NULL
, NULL
);
4544 SetLastError(0xdeadbeef);
4546 ret
= GetUserNameW(NULL
, &required_len
);
4547 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4548 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4549 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4551 SetLastError(0xdeadbeef);
4553 ret
= GetUserNameW(NULL
, &required_len
);
4554 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4555 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4556 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4558 /* Tests crash on Windows. */
4561 SetLastError(0xdeadbeef);
4562 required_len
= UNLEN
+ 1;
4563 GetUserNameW(NULL
, &required_len
);
4565 SetLastError(0xdeadbeef);
4566 GetUserNameW(buffer
, NULL
);
4569 memset(filler
, 'x', sizeof(filler
));
4571 SetLastError(0xdeadbeef);
4572 memcpy(buffer
, filler
, sizeof(filler
));
4574 ret
= GetUserNameW(buffer
, &required_len
);
4575 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4576 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4577 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4578 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4580 SetLastError(0xdeadbeef);
4581 memcpy(buffer
, filler
, sizeof(filler
));
4582 buffer_len
= required_len
;
4583 ret
= GetUserNameW(buffer
, &buffer_len
);
4584 ok(ret
== TRUE
, "GetUserNameW returned %d, last error %u\n", ret
, GetLastError());
4585 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4586 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4588 /* GetUserNameW on XP and newer writes a truncated portion of the username string to the buffer. */
4589 SetLastError(0xdeadbeef);
4590 memcpy(buffer
, filler
, sizeof(filler
));
4592 ret
= GetUserNameW(buffer
, &buffer_len
);
4593 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4594 ok(!memcmp(buffer
, filler
, sizeof(filler
)) ||
4595 broken(memcmp(buffer
, filler
, sizeof(filler
)) != 0), /* XP+ */
4596 "Output buffer was altered\n");
4597 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4598 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4601 static void test_CreateRestrictedToken(void)
4603 HANDLE process_token
, token
, r_token
;
4604 PTOKEN_GROUPS token_groups
, groups2
;
4605 SID_AND_ATTRIBUTES sattr
;
4606 SECURITY_IMPERSONATION_LEVEL level
;
4613 if (!pCreateRestrictedToken
)
4615 win_skip("CreateRestrictedToken is not available\n");
4619 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4620 ok(ret
, "got error %d\n", GetLastError());
4622 ret
= DuplicateTokenEx(process_token
, TOKEN_DUPLICATE
|TOKEN_ADJUST_GROUPS
|TOKEN_QUERY
,
4623 NULL
, SecurityImpersonation
, TokenImpersonation
, &token
);
4624 ok(ret
, "got error %d\n", GetLastError());
4627 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4628 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4629 "got %d with error %d\n", ret
, GetLastError());
4630 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4631 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4632 ok(ret
, "got error %d\n", GetLastError());
4634 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4636 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4640 if (i
== token_groups
->GroupCount
)
4642 HeapFree(GetProcessHeap(), 0, token_groups
);
4644 skip("User not a member of any group\n");
4649 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4650 ok(ret
, "got error %d\n", GetLastError());
4651 ok(is_member
, "not a member\n");
4653 /* disable a SID in new token */
4654 sattr
.Sid
= token_groups
->Groups
[i
].Sid
;
4655 sattr
.Attributes
= 0;
4657 ret
= pCreateRestrictedToken(token
, 0, 1, &sattr
, 0, NULL
, 0, NULL
, &r_token
);
4658 ok(ret
, "got error %d\n", GetLastError());
4662 /* check if a SID is enabled */
4664 ret
= pCheckTokenMembership(r_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4665 ok(ret
, "got error %d\n", GetLastError());
4666 todo_wine
ok(!is_member
, "not a member\n");
4668 ret
= GetTokenInformation(r_token
, TokenGroups
, NULL
, 0, &size
);
4669 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "got %d with error %d\n",
4670 ret
, GetLastError());
4671 groups2
= HeapAlloc(GetProcessHeap(), 0, size
);
4672 ret
= GetTokenInformation(r_token
, TokenGroups
, groups2
, size
, &size
);
4673 ok(ret
, "got error %d\n", GetLastError());
4675 for (j
= 0; j
< groups2
->GroupCount
; j
++)
4677 if (EqualSid(groups2
->Groups
[j
].Sid
, token_groups
->Groups
[i
].Sid
))
4681 todo_wine
ok(groups2
->Groups
[j
].Attributes
& SE_GROUP_USE_FOR_DENY_ONLY
,
4682 "got wrong attributes\n");
4683 todo_wine
ok((groups2
->Groups
[j
].Attributes
& SE_GROUP_ENABLED
) == 0,
4684 "got wrong attributes\n");
4686 HeapFree(GetProcessHeap(), 0, groups2
);
4688 size
= sizeof(type
);
4689 ret
= GetTokenInformation(r_token
, TokenType
, &type
, size
, &size
);
4690 ok(ret
, "got error %d\n", GetLastError());
4691 ok(type
== TokenImpersonation
, "got type %u\n", type
);
4693 size
= sizeof(level
);
4694 ret
= GetTokenInformation(r_token
, TokenImpersonationLevel
, &level
, size
, &size
);
4695 ok(ret
, "got error %d\n", GetLastError());
4696 ok(level
== SecurityImpersonation
, "got level %u\n", type
);
4699 HeapFree(GetProcessHeap(), 0, token_groups
);
4700 CloseHandle(r_token
);
4702 CloseHandle(process_token
);
4705 static void validate_default_security_descriptor(SECURITY_DESCRIPTOR
*sd
)
4707 BOOL ret
, present
, defaulted
;
4711 ret
= IsValidSecurityDescriptor(sd
);
4712 ok(ret
, "security descriptor is not valid\n");
4716 acl
= (void *)0xdeadbeef;
4717 SetLastError(0xdeadbeef);
4718 ret
= GetSecurityDescriptorDacl(sd
, &present
, &acl
, &defaulted
);
4719 ok(ret
, "GetSecurityDescriptorDacl error %d\n", GetLastError());
4721 ok(present
== 1, "acl is not present\n");
4723 ok(acl
!= (void *)0xdeadbeef && acl
!= NULL
, "acl pointer is not set\n");
4724 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4727 sid
= (void *)0xdeadbeef;
4728 SetLastError(0xdeadbeef);
4729 ret
= GetSecurityDescriptorOwner(sd
, &sid
, &defaulted
);
4730 ok(ret
, "GetSecurityDescriptorOwner error %d\n", GetLastError());
4732 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4733 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4736 sid
= (void *)0xdeadbeef;
4737 SetLastError(0xdeadbeef);
4738 ret
= GetSecurityDescriptorGroup(sd
, &sid
, &defaulted
);
4739 ok(ret
, "GetSecurityDescriptorGroup error %d\n", GetLastError());
4741 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4742 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4745 static void test_default_handle_security(HANDLE token
, HANDLE handle
, GENERIC_MAPPING
*mapping
)
4747 DWORD ret
, granted
, priv_set_len
;
4749 PRIVILEGE_SET priv_set
;
4750 SECURITY_DESCRIPTOR
*sd
;
4752 sd
= test_get_security_descriptor(handle
, __LINE__
);
4753 validate_default_security_descriptor(sd
);
4755 priv_set_len
= sizeof(priv_set
);
4756 granted
= 0xdeadbeef;
4757 status
= 0xdeadbeef;
4758 SetLastError(0xdeadbeef);
4759 ret
= AccessCheck(sd
, token
, MAXIMUM_ALLOWED
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4761 ok(ret
, "AccessCheck error %d\n", GetLastError());
4762 ok(status
== 1, "expected 1, got %d\n", status
);
4763 ok(granted
== mapping
->GenericAll
, "expected all access %#x, got %#x\n", mapping
->GenericAll
, granted
);
4765 priv_set_len
= sizeof(priv_set
);
4766 granted
= 0xdeadbeef;
4767 status
= 0xdeadbeef;
4768 SetLastError(0xdeadbeef);
4769 ret
= AccessCheck(sd
, token
, 0, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4771 ok(ret
, "AccessCheck error %d\n", GetLastError());
4772 ok(status
== 0 || broken(status
== 1) /* NT4 */, "expected 0, got %d\n", status
);
4773 ok(granted
== 0 || broken(granted
== mapping
->GenericRead
) /* NT4 */, "expected 0, got %#x\n", granted
);
4775 priv_set_len
= sizeof(priv_set
);
4776 granted
= 0xdeadbeef;
4777 status
= 0xdeadbeef;
4778 SetLastError(0xdeadbeef);
4779 ret
= AccessCheck(sd
, token
, ACCESS_SYSTEM_SECURITY
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4781 ok(ret
, "AccessCheck error %d\n", GetLastError());
4782 ok(status
== 0, "expected 0, got %d\n", status
);
4783 ok(granted
== 0, "expected 0, got %#x\n", granted
);
4785 priv_set_len
= sizeof(priv_set
);
4786 granted
= 0xdeadbeef;
4787 status
= 0xdeadbeef;
4788 SetLastError(0xdeadbeef);
4789 ret
= AccessCheck(sd
, token
, mapping
->GenericRead
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4791 ok(ret
, "AccessCheck error %d\n", GetLastError());
4792 ok(status
== 1, "expected 1, got %d\n", status
);
4793 ok(granted
== mapping
->GenericRead
, "expected read access %#x, got %#x\n", mapping
->GenericRead
, granted
);
4795 priv_set_len
= sizeof(priv_set
);
4796 granted
= 0xdeadbeef;
4797 status
= 0xdeadbeef;
4798 SetLastError(0xdeadbeef);
4799 ret
= AccessCheck(sd
, token
, mapping
->GenericWrite
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4801 ok(ret
, "AccessCheck error %d\n", GetLastError());
4802 ok(status
== 1, "expected 1, got %d\n", status
);
4803 ok(granted
== mapping
->GenericWrite
, "expected write access %#x, got %#x\n", mapping
->GenericWrite
, granted
);
4805 priv_set_len
= sizeof(priv_set
);
4806 granted
= 0xdeadbeef;
4807 status
= 0xdeadbeef;
4808 SetLastError(0xdeadbeef);
4809 ret
= AccessCheck(sd
, token
, mapping
->GenericExecute
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4811 ok(ret
, "AccessCheck error %d\n", GetLastError());
4812 ok(status
== 1, "expected 1, got %d\n", status
);
4813 ok(granted
== mapping
->GenericExecute
, "expected execute access %#x, got %#x\n", mapping
->GenericExecute
, granted
);
4815 HeapFree(GetProcessHeap(), 0, sd
);
4818 static ACCESS_MASK
get_obj_access(HANDLE obj
)
4820 OBJECT_BASIC_INFORMATION info
;
4823 if (!pNtQueryObject
) return 0;
4825 status
= pNtQueryObject(obj
, ObjectBasicInformation
, &info
, sizeof(info
), NULL
);
4826 ok(!status
, "NtQueryObject error %#x\n", status
);
4828 return info
.GrantedAccess
;
4831 static void test_mutex_security(HANDLE token
)
4833 DWORD ret
, i
, access
;
4835 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
| SYNCHRONIZE
,
4836 STANDARD_RIGHTS_WRITE
| MUTEX_MODIFY_STATE
| SYNCHRONIZE
,
4837 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4838 STANDARD_RIGHTS_ALL
| MUTEX_ALL_ACCESS
};
4841 int generic
, mapped
;
4845 { GENERIC_READ
, STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
},
4846 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
},
4847 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4848 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| MUTANT_QUERY_STATE
}
4851 SetLastError(0xdeadbeef);
4852 mutex
= OpenMutexA(0, FALSE
, "WineTestMutex");
4853 ok(!mutex
, "mutex should not exist\n");
4854 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4856 SetLastError(0xdeadbeef);
4857 mutex
= CreateMutexA(NULL
, FALSE
, "WineTestMutex");
4858 ok(mutex
!= 0, "CreateMutex error %d\n", GetLastError());
4860 access
= get_obj_access(mutex
);
4861 ok(access
== MUTANT_ALL_ACCESS
, "expected MUTANT_ALL_ACCESS, got %#x\n", access
);
4863 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4865 SetLastError( 0xdeadbeef );
4866 ret
= DuplicateHandle(GetCurrentProcess(), mutex
, GetCurrentProcess(), &dup
,
4867 map
[i
].generic
, FALSE
, 0);
4868 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4870 access
= get_obj_access(dup
);
4871 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4875 SetLastError(0xdeadbeef);
4876 dup
= OpenMutexA(0, FALSE
, "WineTestMutex");
4878 ok(!dup
, "OpenMutex should fail\n");
4880 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
4883 test_default_handle_security(token
, mutex
, &mapping
);
4885 CloseHandle (mutex
);
4888 static void test_event_security(HANDLE token
)
4890 DWORD ret
, i
, access
;
4892 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
| SYNCHRONIZE
,
4893 STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
| SYNCHRONIZE
,
4894 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4895 STANDARD_RIGHTS_ALL
| EVENT_ALL_ACCESS
};
4898 int generic
, mapped
;
4902 { GENERIC_READ
, STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
},
4903 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
},
4904 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4905 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| EVENT_QUERY_STATE
| EVENT_MODIFY_STATE
}
4908 SetLastError(0xdeadbeef);
4909 event
= OpenEventA(0, FALSE
, "WineTestEvent");
4910 ok(!event
, "event should not exist\n");
4911 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4913 SetLastError(0xdeadbeef);
4914 event
= CreateEventA(NULL
, FALSE
, FALSE
, "WineTestEvent");
4915 ok(event
!= 0, "CreateEvent error %d\n", GetLastError());
4917 access
= get_obj_access(event
);
4918 ok(access
== EVENT_ALL_ACCESS
, "expected EVENT_ALL_ACCESS, got %#x\n", access
);
4920 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4922 SetLastError( 0xdeadbeef );
4923 ret
= DuplicateHandle(GetCurrentProcess(), event
, GetCurrentProcess(), &dup
,
4924 map
[i
].generic
, FALSE
, 0);
4925 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4927 access
= get_obj_access(dup
);
4928 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4932 SetLastError(0xdeadbeef);
4933 dup
= OpenEventA(0, FALSE
, "WineTestEvent");
4935 ok(!dup
, "OpenEvent should fail\n");
4937 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
4940 test_default_handle_security(token
, event
, &mapping
);
4945 static void test_semaphore_security(HANDLE token
)
4947 DWORD ret
, i
, access
;
4949 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
,
4950 STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
,
4951 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4952 STANDARD_RIGHTS_ALL
| SEMAPHORE_ALL_ACCESS
};
4955 int generic
, mapped
;
4959 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
},
4960 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
},
4961 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4962 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| SEMAPHORE_QUERY_STATE
| SEMAPHORE_MODIFY_STATE
}
4965 SetLastError(0xdeadbeef);
4966 sem
= OpenSemaphoreA(0, FALSE
, "WineTestSemaphore");
4967 ok(!sem
, "semaphore should not exist\n");
4968 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4970 SetLastError(0xdeadbeef);
4971 sem
= CreateSemaphoreA(NULL
, 0, 10, "WineTestSemaphore");
4972 ok(sem
!= 0, "CreateSemaphore error %d\n", GetLastError());
4974 access
= get_obj_access(sem
);
4975 ok(access
== SEMAPHORE_ALL_ACCESS
, "expected SEMAPHORE_ALL_ACCESS, got %#x\n", access
);
4977 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4979 SetLastError( 0xdeadbeef );
4980 ret
= DuplicateHandle(GetCurrentProcess(), sem
, GetCurrentProcess(), &dup
,
4981 map
[i
].generic
, FALSE
, 0);
4982 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4984 access
= get_obj_access(dup
);
4985 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4990 test_default_handle_security(token
, sem
, &mapping
);
4995 #define WINE_TEST_PIPE "\\\\.\\pipe\\WineTestPipe"
4996 static void test_named_pipe_security(HANDLE token
)
4998 DWORD ret
, i
, access
;
4999 HANDLE pipe
, file
, dup
;
5000 GENERIC_MAPPING mapping
= { FILE_GENERIC_READ
,
5002 FILE_GENERIC_EXECUTE
,
5003 STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
};
5006 int todo
, generic
, mapped
;
5010 { 1, GENERIC_READ
, FILE_GENERIC_READ
},
5011 { 1, GENERIC_WRITE
, FILE_GENERIC_WRITE
},
5012 { 1, GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
5013 { 1, GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
5019 } creation_access
[] =
5021 { PIPE_ACCESS_INBOUND
, FILE_GENERIC_READ
},
5022 { PIPE_ACCESS_OUTBOUND
, FILE_GENERIC_WRITE
},
5023 { PIPE_ACCESS_DUPLEX
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
},
5024 { PIPE_ACCESS_INBOUND
|WRITE_DAC
, FILE_GENERIC_READ
|WRITE_DAC
},
5025 { PIPE_ACCESS_INBOUND
|WRITE_OWNER
, FILE_GENERIC_READ
|WRITE_OWNER
}
5026 /* ACCESS_SYSTEM_SECURITY is also valid, but will fail with ERROR_PRIVILEGE_NOT_HELD */
5029 /* Test the different security access options for pipes */
5030 for (i
= 0; i
< sizeof(creation_access
)/sizeof(creation_access
[0]); i
++)
5032 SetLastError(0xdeadbeef);
5033 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, creation_access
[i
].open_mode
,
5034 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
, 0, 0,
5035 NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
5036 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe(0x%x) error %d\n",
5037 creation_access
[i
].open_mode
, GetLastError());
5038 access
= get_obj_access(pipe
);
5039 ok(access
== creation_access
[i
].access
,
5040 "CreateNamedPipeA(0x%x) pipe expected access 0x%x (got 0x%x)\n",
5041 creation_access
[i
].open_mode
, creation_access
[i
].access
, access
);
5045 SetLastError(0xdeadbeef);
5046 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_FIRST_PIPE_INSTANCE
,
5047 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
,
5048 0, 0, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
5049 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe error %d\n", GetLastError());
5051 test_default_handle_security(token
, pipe
, &mapping
);
5053 SetLastError(0xdeadbeef);
5054 file
= CreateFileA(WINE_TEST_PIPE
, FILE_ALL_ACCESS
, 0, NULL
, OPEN_EXISTING
, 0, 0);
5055 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5057 access
= get_obj_access(file
);
5058 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
5060 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5062 SetLastError( 0xdeadbeef );
5063 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
5064 map
[i
].generic
, FALSE
, 0);
5065 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5067 access
= get_obj_access(dup
);
5068 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5076 SetLastError(0xdeadbeef);
5077 file
= CreateFileA("\\\\.\\pipe\\", FILE_ALL_ACCESS
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0);
5078 ok(file
!= INVALID_HANDLE_VALUE
|| broken(file
== INVALID_HANDLE_VALUE
) /* before Vista */, "CreateFile error %d\n", GetLastError());
5080 if (file
!= INVALID_HANDLE_VALUE
)
5082 access
= get_obj_access(file
);
5083 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
5085 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5087 SetLastError( 0xdeadbeef );
5088 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
5089 map
[i
].generic
, FALSE
, 0);
5090 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5092 access
= get_obj_access(dup
);
5095 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5097 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5106 static void test_file_security(HANDLE token
)
5108 DWORD ret
, i
, access
, bytes
;
5112 int generic
, mapped
;
5116 { GENERIC_READ
, FILE_GENERIC_READ
},
5117 { GENERIC_WRITE
, FILE_GENERIC_WRITE
},
5118 { GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
5119 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
5121 char temp_path
[MAX_PATH
];
5122 char file_name
[MAX_PATH
];
5125 GetTempPathA(MAX_PATH
, temp_path
);
5126 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
5129 SetLastError(0xdeadbeef);
5130 file
= CreateFileA(file_name
, GENERIC_ALL
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
5131 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5133 access
= get_obj_access(file
);
5134 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
5136 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5138 SetLastError( 0xdeadbeef );
5139 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
5140 map
[i
].generic
, FALSE
, 0);
5141 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5143 access
= get_obj_access(dup
);
5144 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5151 SetLastError(0xdeadbeef);
5152 file
= CreateFileA(file_name
, 0, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
5153 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5155 access
= get_obj_access(file
);
5157 ok(access
== (FILE_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
5160 SetLastError(0xdeadbeef);
5161 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
5162 ok(!ret
, "ReadFile should fail\n");
5163 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
5164 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
5168 SetLastError(0xdeadbeef);
5169 file
= CreateFileA(file_name
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
5170 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5172 access
= get_obj_access(file
);
5174 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
5177 SetLastError(0xdeadbeef);
5178 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
5179 ok(!ret
, "ReadFile should fail\n");
5180 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
5181 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
5184 DeleteFileA(file_name
);
5187 SetLastError(0xdeadbeef);
5188 file
= CreateFileA(temp_path
, GENERIC_ALL
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
5189 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5191 access
= get_obj_access(file
);
5192 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
5194 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5196 SetLastError( 0xdeadbeef );
5197 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
5198 map
[i
].generic
, FALSE
, 0);
5199 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5201 access
= get_obj_access(dup
);
5202 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5209 SetLastError(0xdeadbeef);
5210 file
= CreateFileA(temp_path
, 0, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
5211 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5213 access
= get_obj_access(file
);
5215 ok(access
== (FILE_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
5219 SetLastError(0xdeadbeef);
5220 file
= CreateFileA(temp_path
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
5221 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5223 access
= get_obj_access(file
);
5225 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
5230 static void test_filemap_security(void)
5232 char temp_path
[MAX_PATH
];
5233 char file_name
[MAX_PATH
];
5234 DWORD ret
, i
, access
;
5235 HANDLE file
, mapping
, dup
;
5238 int generic
, mapped
;
5242 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SECTION_QUERY
| SECTION_MAP_READ
},
5243 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SECTION_MAP_WRITE
},
5244 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SECTION_MAP_EXECUTE
},
5245 { GENERIC_ALL
, STANDARD_RIGHTS_REQUIRED
| SECTION_ALL_ACCESS
}
5253 { PAGE_NOACCESS
, 0 },
5254 { PAGE_READONLY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
5255 { PAGE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
},
5256 { PAGE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
5257 { PAGE_EXECUTE
, 0 },
5258 { PAGE_EXECUTE_READ
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
},
5259 { PAGE_EXECUTE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
},
5260 { PAGE_EXECUTE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
}
5263 GetTempPathA(MAX_PATH
, temp_path
);
5264 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
5266 SetLastError(0xdeadbeef);
5267 file
= CreateFileA(file_name
, GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
5268 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5269 SetFilePointer(file
, 4096, NULL
, FILE_BEGIN
);
5272 for (i
= 0; i
< sizeof(prot_map
)/sizeof(prot_map
[0]); i
++)
5274 SetLastError(0xdeadbeef);
5275 mapping
= CreateFileMappingW(file
, NULL
, prot_map
[i
].prot
, 0, 4096, NULL
);
5276 if (prot_map
[i
].mapped
)
5280 /* NT4 and win2k don't support EXEC on file mappings */
5281 if (prot_map
[i
].prot
== PAGE_EXECUTE_READ
|| prot_map
[i
].prot
== PAGE_EXECUTE_READWRITE
|| prot_map
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
5283 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
5287 ok(mapping
!= 0, "CreateFileMapping(%04x) error %d\n", prot_map
[i
].prot
, GetLastError());
5291 ok(!mapping
, "CreateFileMapping(%04x) should fail\n", prot_map
[i
].prot
);
5292 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
5296 access
= get_obj_access(mapping
);
5297 ok(access
== prot_map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, prot_map
[i
].mapped
, access
);
5299 CloseHandle(mapping
);
5302 SetLastError(0xdeadbeef);
5303 mapping
= CreateFileMappingW(file
, NULL
, PAGE_EXECUTE_READWRITE
, 0, 4096, NULL
);
5306 /* NT4 and win2k don't support EXEC on file mappings */
5307 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
5309 DeleteFileA(file_name
);
5312 ok(mapping
!= 0, "CreateFileMapping error %d\n", GetLastError());
5314 access
= get_obj_access(mapping
);
5315 ok(access
== (STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
),
5316 "expected STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE, got %#x\n", access
);
5318 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5320 SetLastError( 0xdeadbeef );
5321 ret
= DuplicateHandle(GetCurrentProcess(), mapping
, GetCurrentProcess(), &dup
,
5322 map
[i
].generic
, FALSE
, 0);
5323 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5325 access
= get_obj_access(dup
);
5326 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5331 CloseHandle(mapping
);
5333 DeleteFileA(file_name
);
5336 static void test_thread_security(void)
5338 DWORD ret
, i
, access
;
5342 int generic
, mapped
;
5346 { GENERIC_READ
, STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
},
5347 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
| THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
| 0x4 },
5348 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5349 { GENERIC_ALL
, THREAD_ALL_ACCESS_NT4
}
5352 SetLastError(0xdeadbeef);
5353 thread
= CreateThread(NULL
, 0, (void *)0xdeadbeef, NULL
, CREATE_SUSPENDED
, &ret
);
5354 ok(thread
!= 0, "CreateThread error %d\n", GetLastError());
5356 access
= get_obj_access(thread
);
5357 ok(access
== THREAD_ALL_ACCESS_NT4
|| access
== THREAD_ALL_ACCESS_VISTA
, "expected THREAD_ALL_ACCESS, got %#x\n", access
);
5359 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5361 SetLastError( 0xdeadbeef );
5362 ret
= DuplicateHandle(GetCurrentProcess(), thread
, GetCurrentProcess(), &dup
,
5363 map
[i
].generic
, FALSE
, 0);
5364 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5366 access
= get_obj_access(dup
);
5367 switch (map
[i
].generic
)
5370 case GENERIC_EXECUTE
:
5371 ok(access
== map
[i
].mapped
||
5372 access
== (map
[i
].mapped
| THREAD_QUERY_LIMITED_INFORMATION
) /* Vista+ */ ||
5373 access
== (map
[i
].mapped
| THREAD_QUERY_LIMITED_INFORMATION
| THREAD_RESUME
) /* win8 */,
5374 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5378 ok(access
== map
[i
].mapped
||
5379 access
== (map
[i
].mapped
| THREAD_SET_LIMITED_INFORMATION
) /* Vista+ */ ||
5380 access
== (map
[i
].mapped
| THREAD_SET_LIMITED_INFORMATION
| THREAD_RESUME
) /* win8 */,
5381 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5384 ok(access
== map
[i
].mapped
|| access
== THREAD_ALL_ACCESS_VISTA
,
5385 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5388 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5395 TerminateThread(thread
, 0);
5396 CloseHandle(thread
);
5399 static void test_process_access(void)
5401 DWORD ret
, i
, access
;
5402 HANDLE process
, dup
;
5404 PROCESS_INFORMATION pi
;
5405 char cmdline
[] = "winver.exe";
5408 int generic
, mapped
;
5412 { GENERIC_READ
, STANDARD_RIGHTS_READ
| PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
},
5413 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| PROCESS_SET_QUOTA
| PROCESS_SET_INFORMATION
| PROCESS_SUSPEND_RESUME
|
5414 PROCESS_VM_WRITE
| PROCESS_DUP_HANDLE
| PROCESS_CREATE_PROCESS
| PROCESS_CREATE_THREAD
| PROCESS_VM_OPERATION
},
5415 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5416 { GENERIC_ALL
, PROCESS_ALL_ACCESS_NT4
}
5419 memset(&sti
, 0, sizeof(sti
));
5420 sti
.cb
= sizeof(sti
);
5421 SetLastError(0xdeadbeef);
5422 ret
= CreateProcessA(NULL
, cmdline
, NULL
, NULL
, FALSE
, CREATE_SUSPENDED
, NULL
, NULL
, &sti
, &pi
);
5423 ok(ret
, "CreateProcess() error %d\n", GetLastError());
5425 CloseHandle(pi
.hThread
);
5426 process
= pi
.hProcess
;
5428 access
= get_obj_access(process
);
5429 ok(access
== PROCESS_ALL_ACCESS_NT4
|| access
== PROCESS_ALL_ACCESS_VISTA
, "expected PROCESS_ALL_ACCESS, got %#x\n", access
);
5431 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5433 SetLastError( 0xdeadbeef );
5434 ret
= DuplicateHandle(GetCurrentProcess(), process
, GetCurrentProcess(), &dup
,
5435 map
[i
].generic
, FALSE
, 0);
5436 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5438 access
= get_obj_access(dup
);
5439 switch (map
[i
].generic
)
5442 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
) /* Vista+ */,
5443 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5446 ok(access
== map
[i
].mapped
||
5447 access
== (map
[i
].mapped
| PROCESS_TERMINATE
) /* before Vista */ ||
5448 access
== (map
[i
].mapped
| PROCESS_SET_LIMITED_INFORMATION
) /* win8 */,
5449 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5451 case GENERIC_EXECUTE
:
5452 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
) /* Vista+ */,
5453 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5456 ok(access
== map
[i
].mapped
|| access
== PROCESS_ALL_ACCESS_VISTA
,
5457 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5460 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5467 TerminateProcess(process
, 0);
5468 CloseHandle(process
);
5471 static BOOL
validate_impersonation_token(HANDLE token
, DWORD
*token_type
)
5475 SECURITY_IMPERSONATION_LEVEL sil
;
5479 SetLastError(0xdeadbeef);
5480 ret
= GetTokenInformation(token
, TokenType
, &type
, sizeof(type
), &needed
);
5481 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5482 ok(needed
== sizeof(type
), "GetTokenInformation should return required buffer length\n");
5483 ok(type
== TokenPrimary
|| type
== TokenImpersonation
, "expected TokenPrimary or TokenImpersonation, got %d\n", type
);
5486 if (type
!= TokenImpersonation
) return FALSE
;
5489 SetLastError(0xdeadbeef);
5490 ret
= GetTokenInformation(token
, TokenImpersonationLevel
, &sil
, sizeof(sil
), &needed
);
5491 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5492 ok(needed
== sizeof(sil
), "GetTokenInformation should return required buffer length\n");
5493 ok(sil
== SecurityImpersonation
, "expected SecurityImpersonation, got %d\n", sil
);
5495 needed
= 0xdeadbeef;
5496 SetLastError(0xdeadbeef);
5497 ret
= GetTokenInformation(token
, TokenDefaultDacl
, NULL
, 0, &needed
);
5498 ok(!ret
, "GetTokenInformation should fail\n");
5499 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5500 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5501 ok(needed
> sizeof(TOKEN_DEFAULT_DACL
), "GetTokenInformation returned empty default DACL\n");
5503 needed
= 0xdeadbeef;
5504 SetLastError(0xdeadbeef);
5505 ret
= GetTokenInformation(token
, TokenOwner
, NULL
, 0, &needed
);
5506 ok(!ret
, "GetTokenInformation should fail\n");
5507 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5508 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5509 ok(needed
> sizeof(TOKEN_OWNER
), "GetTokenInformation returned empty token owner\n");
5511 needed
= 0xdeadbeef;
5512 SetLastError(0xdeadbeef);
5513 ret
= GetTokenInformation(token
, TokenPrimaryGroup
, NULL
, 0, &needed
);
5514 ok(!ret
, "GetTokenInformation should fail\n");
5515 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5516 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5517 ok(needed
> sizeof(TOKEN_PRIMARY_GROUP
), "GetTokenInformation returned empty token primary group\n");
5522 static void test_kernel_objects_security(void)
5524 HANDLE token
, process_token
;
5525 DWORD ret
, token_type
;
5527 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
| TOKEN_QUERY
, &process_token
);
5528 ok(ret
, "OpenProcessToken error %d\n", GetLastError());
5530 ret
= validate_impersonation_token(process_token
, &token_type
);
5531 ok(token_type
== TokenPrimary
, "expected TokenPrimary, got %d\n", token_type
);
5532 ok(!ret
, "access token should not be an impersonation token\n");
5534 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
5535 ok(ret
, "DuplicateToken error %d\n", GetLastError());
5537 ret
= validate_impersonation_token(token
, &token_type
);
5538 ok(ret
, "access token should be a valid impersonation token\n");
5539 ok(token_type
== TokenImpersonation
, "expected TokenImpersonation, got %d\n", token_type
);
5541 test_mutex_security(token
);
5542 test_event_security(token
);
5543 test_named_pipe_security(token
);
5544 test_semaphore_security(token
);
5545 test_file_security(token
);
5546 test_filemap_security();
5547 test_thread_security();
5548 test_process_access();
5549 /* FIXME: test other kernel object types */
5551 CloseHandle(process_token
);
5555 static void test_TokenIntegrityLevel(void)
5557 TOKEN_MANDATORY_LABEL
*tml
;
5558 BYTE buffer
[64]; /* using max. 28 byte in win7 x64 */
5562 char *sidname
= NULL
;
5563 static SID medium_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5564 {SECURITY_MANDATORY_HIGH_RID
}};
5565 static SID high_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5566 {SECURITY_MANDATORY_MEDIUM_RID
}};
5568 SetLastError(0xdeadbeef);
5569 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &token
);
5570 ok(res
, "got %d with %d (expected TRUE)\n", res
, GetLastError());
5572 SetLastError(0xdeadbeef);
5573 res
= GetTokenInformation(token
, TokenIntegrityLevel
, buffer
, sizeof(buffer
), &size
);
5575 /* not supported before Vista */
5576 if (!res
&& ((GetLastError() == ERROR_INVALID_PARAMETER
) || GetLastError() == ERROR_INVALID_FUNCTION
))
5578 win_skip("TokenIntegrityLevel not supported\n");
5583 ok(res
, "got %u with %u (expected TRUE)\n", res
, GetLastError());
5590 tml
= (TOKEN_MANDATORY_LABEL
*) buffer
;
5591 ok(tml
->Label
.Attributes
== (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
),
5592 "got 0x%x (expected 0x%x)\n", tml
->Label
.Attributes
, (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
));
5594 SetLastError(0xdeadbeef);
5595 res
= pConvertSidToStringSidA(tml
->Label
.Sid
, &sidname
);
5596 ok(res
, "got %u and %u\n", res
, GetLastError());
5598 ok(EqualSid(tml
->Label
.Sid
, &medium_level
) || EqualSid(tml
->Label
.Sid
, &high_level
),
5599 "got %s (expected 'S-1-16-8192' or 'S-1-16-12288')\n", sidname
);
5605 static void test_default_dacl_owner_sid(void)
5608 BOOL ret
, defaulted
, present
, found
;
5610 SECURITY_DESCRIPTOR
*sd
;
5611 SECURITY_ATTRIBUTES sa
;
5614 ACCESS_ALLOWED_ACE
*ace
;
5616 sd
= HeapAlloc( GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
5617 ret
= InitializeSecurityDescriptor( sd
, SECURITY_DESCRIPTOR_REVISION
);
5618 ok( ret
, "error %u\n", GetLastError() );
5620 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
5621 sa
.lpSecurityDescriptor
= sd
;
5622 sa
.bInheritHandle
= FALSE
;
5623 handle
= CreateEventA( &sa
, TRUE
, TRUE
, "test_event" );
5624 ok( handle
!= NULL
, "error %u\n", GetLastError() );
5627 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, NULL
, 0, &size
);
5628 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "error %u\n", GetLastError() );
5630 sd
= HeapAlloc( GetProcessHeap(), 0, size
);
5631 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, sd
, size
, &size
);
5632 ok( ret
, "error %u\n", GetLastError() );
5634 owner
= (void *)0xdeadbeef;
5636 ret
= GetSecurityDescriptorOwner( sd
, &owner
, &defaulted
);
5637 ok( ret
, "error %u\n", GetLastError() );
5638 ok( owner
!= (void *)0xdeadbeef, "owner not set\n" );
5639 ok( !defaulted
, "owner defaulted\n" );
5641 dacl
= (void *)0xdeadbeef;
5644 ret
= GetSecurityDescriptorDacl( sd
, &present
, &dacl
, &defaulted
);
5645 ok( ret
, "error %u\n", GetLastError() );
5646 ok( present
, "dacl not present\n" );
5647 ok( dacl
!= (void *)0xdeadbeef, "dacl not set\n" );
5648 ok( !defaulted
, "dacl defaulted\n" );
5652 while (pGetAce( dacl
, index
++, (void **)&ace
))
5654 if (EqualSid( &ace
->SidStart
, owner
)) found
= TRUE
;
5656 ok( found
, "owner sid not found in dacl\n" );
5658 HeapFree( GetProcessHeap(), 0, sa
.lpSecurityDescriptor
);
5659 HeapFree( GetProcessHeap(), 0, sd
);
5660 CloseHandle( handle
);
5663 static void test_AdjustTokenPrivileges(void)
5665 TOKEN_PRIVILEGES tp
, prev
;
5671 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &token
))
5674 if (!LookupPrivilegeValueA(NULL
, SE_BACKUP_NAME
, &luid
))
5680 tp
.PrivilegeCount
= 1;
5681 tp
.Privileges
[0].Luid
= luid
;
5682 tp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
5685 ret
= AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), NULL
, &len
);
5686 ok(ret
, "got %d\n", ret
);
5687 ok(len
== 0xdeadbeef, "got length %d\n", len
);
5690 tp
.PrivilegeCount
= 1;
5691 tp
.Privileges
[0].Luid
= luid
;
5692 tp
.Privileges
[0].Attributes
= 0;
5693 AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), &prev
, NULL
);
5698 START_TEST(security
)
5705 test_process_security_child();
5708 test_kernel_objects_security();
5712 test_CreateWellKnownSid();
5713 test_FileSecurity();
5716 test_LookupAccountSid();
5717 test_LookupAccountName();
5718 test_security_descriptor();
5719 test_process_security();
5720 test_impersonation_level();
5721 test_SetEntriesInAclW();
5722 test_SetEntriesInAclA();
5723 test_CreateDirectoryA();
5724 test_GetNamedSecurityInfoA();
5725 test_ConvertStringSecurityDescriptor();
5726 test_ConvertSecurityDescriptorToString();
5727 test_PrivateObjectSecurity();
5729 test_GetSecurityInfo();
5730 test_GetSidSubAuthority();
5731 test_CheckTokenMembership();
5733 test_GetUserNameA();
5734 test_GetUserNameW();
5735 test_CreateRestrictedToken();
5736 test_TokenIntegrityLevel();
5737 test_default_dacl_owner_sid();
5738 test_AdjustTokenPrivileges();