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 retSize
= 0xdeadbeef;
927 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
929 ok(!rc
, "GetFileSecurity should fail\n");
930 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
931 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
932 ok(sdSize
> sizeof(SECURITY_DESCRIPTOR
), "got sd size %d\n", sdSize
);
934 sd
= HeapAlloc(GetProcessHeap (), 0, sdSize
);
935 retSize
= 0xdeadbeef;
936 SetLastError(0xdeadbeef);
937 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
938 sd
, sdSize
, &retSize
);
939 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
940 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
942 SetLastError(0xdeadbeef);
943 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
944 ok(!rc
, "OpenThreadToken should fail\n");
945 ok(GetLastError() == ERROR_NO_TOKEN
, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
947 SetLastError(0xdeadbeef);
948 rc
= ImpersonateSelf(SecurityIdentification
);
949 ok(rc
, "ImpersonateSelf error %d\n", GetLastError());
951 SetLastError(0xdeadbeef);
952 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
953 ok(rc
, "OpenThreadToken error %d\n", GetLastError());
955 SetLastError(0xdeadbeef);
957 ok(rc
, "RevertToSelf error %d\n", GetLastError());
959 priv_set_len
= sizeof(priv_set
);
960 granted
= 0xdeadbeef;
962 SetLastError(0xdeadbeef);
963 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
964 ok(rc
, "AccessCheck error %d\n", GetLastError());
965 ok(status
== 1, "expected 1, got %d\n", status
);
966 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
968 granted
= 0xdeadbeef;
970 SetLastError(0xdeadbeef);
971 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
972 ok(rc
, "AccessCheck error %d\n", GetLastError());
973 ok(status
== 1, "expected 1, got %d\n", status
);
974 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
976 granted
= 0xdeadbeef;
978 SetLastError(0xdeadbeef);
979 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
980 ok(rc
, "AccessCheck error %d\n", GetLastError());
981 ok(status
== 1, "expected 1, got %d\n", status
);
982 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
984 granted
= 0xdeadbeef;
986 SetLastError(0xdeadbeef);
987 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
988 ok(rc
, "AccessCheck error %d\n", GetLastError());
989 ok(status
== 1, "expected 1, got %d\n", status
);
990 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
992 granted
= 0xdeadbeef;
994 SetLastError(0xdeadbeef);
995 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
996 ok(rc
, "AccessCheck error %d\n", GetLastError());
997 ok(status
== 1, "expected 1, got %d\n", status
);
998 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
1000 granted
= 0xdeadbeef;
1001 status
= 0xdeadbeef;
1002 SetLastError(0xdeadbeef);
1003 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1004 ok(rc
, "AccessCheck error %d\n", GetLastError());
1005 ok(status
== 1, "expected 1, got %d\n", status
);
1006 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
1008 granted
= 0xdeadbeef;
1009 status
= 0xdeadbeef;
1010 SetLastError(0xdeadbeef);
1011 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1012 ok(rc
, "AccessCheck error %d\n", GetLastError());
1013 ok(status
== 1, "expected 1, got %d\n", status
);
1014 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
1016 SetLastError(0xdeadbeef);
1017 rc
= AccessCheck(sd
, token
, 0xffffffff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1018 ok(!rc
, "AccessCheck should fail\n");
1019 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED
, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
1021 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
1022 SetLastError(0xdeadbeef);
1023 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
1024 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
1025 retSize
= 0xdeadbeef;
1026 SetLastError(0xdeadbeef);
1027 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
1028 ok(!rc
, "WriteFile should fail\n");
1029 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1030 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
1033 rc
= GetFileAttributesA(file
);
1034 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
1036 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
1037 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
1039 SetLastError(0xdeadbeef);
1040 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
1041 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1042 SetLastError(0xdeadbeef);
1043 rc
= DeleteFileA(file
);
1044 ok(rc
, "DeleteFile error %d\n", GetLastError());
1046 SetLastError(0xdeadbeef);
1047 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
1048 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
1049 retSize
= 0xdeadbeef;
1050 SetLastError(0xdeadbeef);
1051 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
1052 ok(!rc
, "WriteFile should fail\n");
1053 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1054 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
1057 rc
= GetFileAttributesA(file
);
1058 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
1059 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
1060 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
1062 retSize
= 0xdeadbeef;
1063 SetLastError(0xdeadbeef);
1064 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
1065 sd
, sdSize
, &retSize
);
1066 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
1067 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
1069 priv_set_len
= sizeof(priv_set
);
1070 granted
= 0xdeadbeef;
1071 status
= 0xdeadbeef;
1072 SetLastError(0xdeadbeef);
1073 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1074 ok(rc
, "AccessCheck error %d\n", GetLastError());
1075 ok(status
== 1, "expected 1, got %d\n", status
);
1076 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
1078 granted
= 0xdeadbeef;
1079 status
= 0xdeadbeef;
1080 SetLastError(0xdeadbeef);
1081 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1082 ok(rc
, "AccessCheck error %d\n", GetLastError());
1084 ok(status
== 1, "expected 1, got %d\n", status
);
1085 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
1087 granted
= 0xdeadbeef;
1088 status
= 0xdeadbeef;
1089 SetLastError(0xdeadbeef);
1090 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1091 ok(rc
, "AccessCheck error %d\n", GetLastError());
1092 ok(status
== 1, "expected 1, got %d\n", status
);
1093 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
1095 granted
= 0xdeadbeef;
1096 status
= 0xdeadbeef;
1097 SetLastError(0xdeadbeef);
1098 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1099 ok(rc
, "AccessCheck error %d\n", GetLastError());
1101 ok(status
== 1, "expected 1, got %d\n", status
);
1102 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
1104 granted
= 0xdeadbeef;
1105 status
= 0xdeadbeef;
1106 SetLastError(0xdeadbeef);
1107 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1108 ok(rc
, "AccessCheck error %d\n", GetLastError());
1110 ok(status
== 1, "expected 1, got %d\n", status
);
1111 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
1113 granted
= 0xdeadbeef;
1114 status
= 0xdeadbeef;
1115 SetLastError(0xdeadbeef);
1116 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1117 ok(rc
, "AccessCheck error %d\n", GetLastError());
1119 ok(status
== 1, "expected 1, got %d\n", status
);
1120 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
1122 granted
= 0xdeadbeef;
1123 status
= 0xdeadbeef;
1124 SetLastError(0xdeadbeef);
1125 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1126 ok(rc
, "AccessCheck error %d\n", GetLastError());
1128 ok(status
== 1, "expected 1, got %d\n", status
);
1129 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
1131 SetLastError(0xdeadbeef);
1132 rc
= DeleteFileA(file
);
1133 ok(!rc
, "DeleteFile should fail\n");
1134 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1135 SetLastError(0xdeadbeef);
1136 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
1137 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1138 SetLastError(0xdeadbeef);
1139 rc
= DeleteFileA(file
);
1140 ok(rc
, "DeleteFile error %d\n", GetLastError());
1143 HeapFree(GetProcessHeap(), 0, sd
);
1146 static void test_AccessCheck(void)
1148 PSID EveryoneSid
= NULL
, AdminSid
= NULL
, UsersSid
= NULL
;
1150 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
1151 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
1152 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1153 GENERIC_MAPPING Mapping
= { KEY_READ
, KEY_WRITE
, KEY_EXECUTE
, KEY_ALL_ACCESS
};
1157 HANDLE ProcessToken
;
1160 PRIVILEGE_SET
*PrivSet
;
1162 HMODULE NtDllModule
;
1165 NTSTATUS ntret
, ntAccessStatus
;
1167 NtDllModule
= GetModuleHandleA("ntdll.dll");
1170 skip("not running on NT, skipping test\n");
1173 pRtlAdjustPrivilege
= (void *)GetProcAddress(NtDllModule
, "RtlAdjustPrivilege");
1174 if (!pRtlAdjustPrivilege
)
1176 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1180 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
1181 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1182 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1184 skip("ACLs not implemented - skipping tests\n");
1185 HeapFree(GetProcessHeap(), 0, Acl
);
1188 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
1190 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
1191 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1193 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1194 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &AdminSid
);
1195 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1197 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1198 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
1199 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1201 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
1203 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
1204 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1206 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1207 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1209 PrivSetLen
= FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[16]);
1210 PrivSet
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, PrivSetLen
);
1211 PrivSet
->PrivilegeCount
= 16;
1213 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &ProcessToken
);
1214 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
1216 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, FALSE
, TRUE
, &Enabled
);
1218 res
= DuplicateToken(ProcessToken
, SecurityImpersonation
, &Token
);
1219 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1221 /* SD without owner/group */
1222 SetLastError(0xdeadbeef);
1223 Access
= AccessStatus
= 0x1abe11ed;
1224 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_QUERY_VALUE
, &Mapping
,
1225 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1226 err
= GetLastError();
1227 ok(!ret
&& err
== ERROR_INVALID_SECURITY_DESCR
, "AccessCheck should have "
1228 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err
);
1229 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1230 "Access and/or AccessStatus were changed!\n");
1232 /* Set owner and group */
1233 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
1234 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1235 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, TRUE
);
1236 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1238 /* Generic access mask */
1239 SetLastError(0xdeadbeef);
1240 Access
= AccessStatus
= 0x1abe11ed;
1241 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1242 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1243 err
= GetLastError();
1244 ok(!ret
&& err
== ERROR_GENERIC_NOT_MAPPED
, "AccessCheck should have failed "
1245 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err
);
1246 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1247 "Access and/or AccessStatus were changed!\n");
1249 /* Generic access mask - no privilegeset buffer */
1250 SetLastError(0xdeadbeef);
1251 Access
= AccessStatus
= 0x1abe11ed;
1252 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1253 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1254 err
= GetLastError();
1255 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1256 "with ERROR_NOACCESS, instead of %d\n", err
);
1257 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1258 "Access and/or AccessStatus were changed!\n");
1260 /* Generic access mask - no returnlength */
1261 SetLastError(0xdeadbeef);
1262 Access
= AccessStatus
= 0x1abe11ed;
1263 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1264 PrivSet
, NULL
, &Access
, &AccessStatus
);
1265 err
= GetLastError();
1266 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1267 "with ERROR_NOACCESS, instead of %d\n", err
);
1268 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1269 "Access and/or AccessStatus were changed!\n");
1271 /* Generic access mask - no privilegeset buffer, no returnlength */
1272 SetLastError(0xdeadbeef);
1273 Access
= AccessStatus
= 0x1abe11ed;
1274 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1275 NULL
, NULL
, &Access
, &AccessStatus
);
1276 err
= GetLastError();
1277 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1278 "with ERROR_NOACCESS, instead of %d\n", err
);
1279 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1280 "Access and/or AccessStatus were changed!\n");
1282 /* sd with no dacl present */
1283 Access
= AccessStatus
= 0x1abe11ed;
1284 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, FALSE
, NULL
, FALSE
);
1285 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1286 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1287 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1288 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1289 ok(AccessStatus
&& (Access
== KEY_READ
),
1290 "AccessCheck failed to grant access with error %d\n",
1293 /* sd with no dacl present - no privilegeset buffer */
1294 SetLastError(0xdeadbeef);
1295 Access
= AccessStatus
= 0x1abe11ed;
1296 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1297 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1298 err
= GetLastError();
1299 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1300 "with ERROR_NOACCESS, instead of %d\n", err
);
1301 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1302 "Access and/or AccessStatus were changed!\n");
1306 /* Generic access mask - no privilegeset buffer */
1307 SetLastError(0xdeadbeef);
1308 Access
= ntAccessStatus
= 0x1abe11ed;
1309 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1310 NULL
, &PrivSetLen
, &Access
, &ntAccessStatus
);
1311 err
= GetLastError();
1312 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1313 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1314 ok(err
== 0xdeadbeef,
1315 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1316 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1317 "Access and/or AccessStatus were changed!\n");
1319 /* Generic access mask - no returnlength */
1320 SetLastError(0xdeadbeef);
1321 Access
= ntAccessStatus
= 0x1abe11ed;
1322 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1323 PrivSet
, NULL
, &Access
, &ntAccessStatus
);
1324 err
= GetLastError();
1325 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1326 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1327 ok(err
== 0xdeadbeef,
1328 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1329 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1330 "Access and/or AccessStatus were changed!\n");
1332 /* Generic access mask - no privilegeset buffer, no returnlength */
1333 SetLastError(0xdeadbeef);
1334 Access
= ntAccessStatus
= 0x1abe11ed;
1335 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1336 NULL
, NULL
, &Access
, &ntAccessStatus
);
1337 err
= GetLastError();
1338 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1339 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1340 ok(err
== 0xdeadbeef,
1341 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1342 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1343 "Access and/or AccessStatus were changed!\n");
1346 win_skip("NtAccessCheck unavailable. Skipping.\n");
1348 /* sd with NULL dacl */
1349 Access
= AccessStatus
= 0x1abe11ed;
1350 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, NULL
, FALSE
);
1351 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1352 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1353 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1354 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1355 ok(AccessStatus
&& (Access
== KEY_READ
),
1356 "AccessCheck failed to grant access with error %d\n",
1359 /* sd with blank dacl */
1360 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1361 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1362 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1363 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1364 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1365 err
= GetLastError();
1366 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1367 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1368 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1370 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, KEY_READ
, EveryoneSid
);
1371 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1373 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, KEY_SET_VALUE
, AdminSid
);
1374 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1377 Access
= AccessStatus
= 0x1abe11ed;
1378 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1379 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1380 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1381 ok(AccessStatus
&& (Access
== KEY_READ
),
1382 "AccessCheck failed to grant access with error %d\n",
1385 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1386 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1387 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1389 "AccessCheck failed to grant any access with error %d\n",
1391 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access
);
1393 /* Access denied by SD */
1394 SetLastError(0xdeadbeef);
1395 Access
= AccessStatus
= 0x1abe11ed;
1396 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_WRITE
, &Mapping
,
1397 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1398 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1399 err
= GetLastError();
1400 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1401 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1402 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1405 PrivSet
->PrivilegeCount
= 16;
1406 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1407 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1408 ok(ret
&& !AccessStatus
&& GetLastError() == ERROR_PRIVILEGE_NOT_HELD
,
1409 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1412 ret
= ImpersonateLoggedOnUser(Token
);
1413 ok(ret
, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1414 ret
= pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, TRUE
, TRUE
, &Enabled
);
1418 PrivSet
->PrivilegeCount
= 16;
1419 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1420 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1421 ok(ret
&& AccessStatus
&& GetLastError() == 0,
1422 "AccessCheck should have succeeded, error %d\n",
1424 ok(Access
== ACCESS_SYSTEM_SECURITY
,
1425 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1429 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1431 ret
= RevertToSelf();
1432 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1434 /* test INHERIT_ONLY_ACE */
1435 ret
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1436 ok(ret
, "InitializeAcl failed with error %d\n", GetLastError());
1438 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1441 if (pAddAccessAllowedAceEx
)
1443 ret
= pAddAccessAllowedAceEx(Acl
, ACL_REVISION
, INHERIT_ONLY_ACE
, KEY_READ
, EveryoneSid
);
1444 ok(ret
, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1447 win_skip("AddAccessAllowedAceEx is not available\n");
1449 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1450 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1451 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1452 err
= GetLastError();
1453 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1454 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1455 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1459 res
= DuplicateToken(ProcessToken
, SecurityAnonymous
, &Token
);
1460 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1462 SetLastError(0xdeadbeef);
1463 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1464 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1465 err
= GetLastError();
1466 ok(!ret
&& err
== ERROR_BAD_IMPERSONATION_LEVEL
, "AccessCheck should have failed "
1467 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err
);
1471 SetLastError(0xdeadbeef);
1472 ret
= AccessCheck(SecurityDescriptor
, ProcessToken
, KEY_READ
, &Mapping
,
1473 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1474 err
= GetLastError();
1475 ok(!ret
&& err
== ERROR_NO_IMPERSONATION_TOKEN
, "AccessCheck should have failed "
1476 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err
);
1478 CloseHandle(ProcessToken
);
1481 FreeSid(EveryoneSid
);
1486 HeapFree(GetProcessHeap(), 0, Acl
);
1487 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
1488 HeapFree(GetProcessHeap(), 0, PrivSet
);
1491 /* test GetTokenInformation for the various attributes */
1492 static void test_token_attr(void)
1494 HANDLE Token
, ImpersonationToken
;
1496 TOKEN_PRIVILEGES
*Privileges
;
1497 TOKEN_GROUPS
*Groups
;
1499 TOKEN_DEFAULT_DACL
*Dacl
;
1503 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1506 /* cygwin-like use case */
1507 SetLastError(0xdeadbeef);
1508 ret
= OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED
, &Token
);
1509 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1511 win_skip("OpenProcessToken is not implemented\n");
1514 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1517 DWORD buf
[256]; /* GetTokenInformation wants a dword-aligned buffer */
1519 ret
= GetTokenInformation(Token
, TokenUser
,(void*)buf
, Size
, &Size
);
1520 ok(ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1521 Size
= sizeof(ImpersonationLevel
);
1522 ret
= GetTokenInformation(Token
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1523 GLE
= GetLastError();
1524 ok(!ret
&& (GLE
== ERROR_INVALID_PARAMETER
), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE
);
1528 if(!pConvertSidToStringSidA
)
1530 win_skip("ConvertSidToStringSidA is not available\n");
1534 SetLastError(0xdeadbeef);
1535 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS
, &Token
);
1536 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1539 /* insufficient buffer length */
1540 SetLastError(0xdeadbeef);
1542 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size2
);
1543 ok(Size2
> 1, "got %d\n", Size2
);
1544 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1545 "%d with error %d\n", ret
, GetLastError());
1547 Groups
= HeapAlloc(GetProcessHeap(), 0, Size2
);
1548 memset(Groups
, 0xcc, Size2
);
1550 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size2
, &Size
);
1551 ok(Size
> 1, "got %d\n", Size
);
1552 ok((!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) || broken(ret
) /* wow64 */,
1553 "%d with error %d\n", ret
, GetLastError());
1555 ok(*((BYTE
*)Groups
) == 0xcc, "buffer altered\n");
1557 HeapFree(GetProcessHeap(), 0, Groups
);
1559 SetLastError(0xdeadbeef);
1560 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size
);
1561 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1562 "GetTokenInformation(TokenGroups) %s with error %d\n",
1563 ret
? "succeeded" : "failed", GetLastError());
1564 Groups
= HeapAlloc(GetProcessHeap(), 0, Size
);
1565 SetLastError(0xdeadbeef);
1566 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size
, &Size
);
1567 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1568 ok(GetLastError() == 0xdeadbeef,
1569 "GetTokenInformation shouldn't have set last error to %d\n",
1571 trace("TokenGroups:\n");
1572 for (i
= 0; i
< Groups
->GroupCount
; i
++)
1574 DWORD NameLength
= 255;
1576 DWORD DomainLength
= 255;
1578 SID_NAME_USE SidNameUse
;
1581 ret
= LookupAccountSidA(NULL
, Groups
->Groups
[i
].Sid
, Name
, &NameLength
, Domain
, &DomainLength
, &SidNameUse
);
1584 pConvertSidToStringSidA(Groups
->Groups
[i
].Sid
, &SidString
);
1585 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString
, Domain
, Name
, SidNameUse
, Groups
->Groups
[i
].Attributes
);
1586 LocalFree(SidString
);
1588 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups
->Groups
[i
].Attributes
, GetLastError());
1590 HeapFree(GetProcessHeap(), 0, Groups
);
1593 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
1594 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1595 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1596 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
1597 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
1599 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1601 pConvertSidToStringSidA(User
->User
.Sid
, &SidString
);
1602 trace("TokenUser: %s attr: 0x%08x\n", SidString
, User
->User
.Attributes
);
1603 LocalFree(SidString
);
1604 HeapFree(GetProcessHeap(), 0, User
);
1607 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
1608 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1609 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1610 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1611 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1613 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1614 trace("TokenPrivileges:\n");
1615 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1618 DWORD NameLen
= sizeof(Name
)/sizeof(Name
[0]);
1619 LookupPrivilegeNameA(NULL
, &Privileges
->Privileges
[i
].Luid
, Name
, &NameLen
);
1620 trace("\t%s, 0x%x\n", Name
, Privileges
->Privileges
[i
].Attributes
);
1622 HeapFree(GetProcessHeap(), 0, Privileges
);
1624 ret
= DuplicateToken(Token
, SecurityAnonymous
, &ImpersonationToken
);
1625 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1627 Size
= sizeof(ImpersonationLevel
);
1628 ret
= GetTokenInformation(ImpersonationToken
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1629 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1630 ok(ImpersonationLevel
== SecurityAnonymous
, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel
);
1632 CloseHandle(ImpersonationToken
);
1635 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0, &Size
);
1636 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1637 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1639 Dacl
= HeapAlloc(GetProcessHeap(), 0, Size
);
1640 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size
);
1641 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1643 SetLastError(0xdeadbeef);
1644 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0);
1645 GLE
= GetLastError();
1646 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1647 ok(GLE
== ERROR_BAD_LENGTH
, "expected ERROR_BAD_LENGTH got %u\n", GLE
);
1649 SetLastError(0xdeadbeef);
1650 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, Size
);
1651 GLE
= GetLastError();
1652 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1653 ok(GLE
== ERROR_NOACCESS
, "expected ERROR_NOACCESS got %u\n", GLE
);
1655 acl
= Dacl
->DefaultDacl
;
1656 Dacl
->DefaultDacl
= NULL
;
1658 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1659 ok(ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1662 Dacl
->DefaultDacl
= (ACL
*)0xdeadbeef;
1663 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1664 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1665 ok(Dacl
->DefaultDacl
== NULL
, "expected NULL, got %p\n", Dacl
->DefaultDacl
);
1666 ok(Size2
== sizeof(TOKEN_DEFAULT_DACL
) || broken(Size2
== 2*sizeof(TOKEN_DEFAULT_DACL
)), /* WoW64 */
1667 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2
);
1669 Dacl
->DefaultDacl
= acl
;
1670 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1671 ok(ret
, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1673 if (Size2
== sizeof(TOKEN_DEFAULT_DACL
)) {
1674 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1675 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1677 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1679 HeapFree(GetProcessHeap(), 0, Dacl
);
1683 typedef union _MAX_SID
1686 char max
[SECURITY_MAX_SID_SIZE
];
1689 static void test_sid_str(PSID
* sid
)
1692 BOOL ret
= pConvertSidToStringSidA(sid
, &str_sid
);
1693 ok(ret
, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1696 char account
[MAX_PATH
], domain
[MAX_PATH
];
1698 DWORD acc_size
= MAX_PATH
;
1699 DWORD dom_size
= MAX_PATH
;
1700 ret
= LookupAccountSidA (NULL
, sid
, account
, &acc_size
, domain
, &dom_size
, &use
);
1701 ok(ret
|| (!ret
&& (GetLastError() == ERROR_NONE_MAPPED
)),
1702 "LookupAccountSid(%s) failed: %d\n", str_sid
, GetLastError());
1704 trace(" %s %s\\%s %d\n", str_sid
, domain
, account
, use
);
1705 else if (GetLastError() == ERROR_NONE_MAPPED
)
1706 trace(" %s couldn't be mapped\n", str_sid
);
1711 static const struct well_known_sid_value
1713 BOOL without_domain
;
1714 const char *sid_string
;
1715 } well_known_sid_values
[] = {
1716 /* 0 */ {TRUE
, "S-1-0-0"}, {TRUE
, "S-1-1-0"}, {TRUE
, "S-1-2-0"}, {TRUE
, "S-1-3-0"},
1717 /* 4 */ {TRUE
, "S-1-3-1"}, {TRUE
, "S-1-3-2"}, {TRUE
, "S-1-3-3"}, {TRUE
, "S-1-5"},
1718 /* 8 */ {FALSE
, "S-1-5-1"}, {TRUE
, "S-1-5-2"}, {TRUE
, "S-1-5-3"}, {TRUE
, "S-1-5-4"},
1719 /* 12 */ {TRUE
, "S-1-5-6"}, {TRUE
, "S-1-5-7"}, {TRUE
, "S-1-5-8"}, {TRUE
, "S-1-5-9"},
1720 /* 16 */ {TRUE
, "S-1-5-10"}, {TRUE
, "S-1-5-11"}, {TRUE
, "S-1-5-12"}, {TRUE
, "S-1-5-13"},
1721 /* 20 */ {TRUE
, "S-1-5-14"}, {FALSE
, NULL
}, {TRUE
, "S-1-5-18"}, {TRUE
, "S-1-5-19"},
1722 /* 24 */ {TRUE
, "S-1-5-20"}, {TRUE
, "S-1-5-32"},
1723 /* 26 */ {FALSE
, "S-1-5-32-544"}, {TRUE
, "S-1-5-32-545"}, {TRUE
, "S-1-5-32-546"},
1724 /* 29 */ {TRUE
, "S-1-5-32-547"}, {TRUE
, "S-1-5-32-548"}, {TRUE
, "S-1-5-32-549"},
1725 /* 32 */ {TRUE
, "S-1-5-32-550"}, {TRUE
, "S-1-5-32-551"}, {TRUE
, "S-1-5-32-552"},
1726 /* 35 */ {TRUE
, "S-1-5-32-554"}, {TRUE
, "S-1-5-32-555"}, {TRUE
, "S-1-5-32-556"},
1727 /* 38 */ {FALSE
, "S-1-5-21-12-23-34-45-56-500"}, {FALSE
, "S-1-5-21-12-23-34-45-56-501"},
1728 /* 40 */ {FALSE
, "S-1-5-21-12-23-34-45-56-502"}, {FALSE
, "S-1-5-21-12-23-34-45-56-512"},
1729 /* 42 */ {FALSE
, "S-1-5-21-12-23-34-45-56-513"}, {FALSE
, "S-1-5-21-12-23-34-45-56-514"},
1730 /* 44 */ {FALSE
, "S-1-5-21-12-23-34-45-56-515"}, {FALSE
, "S-1-5-21-12-23-34-45-56-516"},
1731 /* 46 */ {FALSE
, "S-1-5-21-12-23-34-45-56-517"}, {FALSE
, "S-1-5-21-12-23-34-45-56-518"},
1732 /* 48 */ {FALSE
, "S-1-5-21-12-23-34-45-56-519"}, {FALSE
, "S-1-5-21-12-23-34-45-56-520"},
1733 /* 50 */ {FALSE
, "S-1-5-21-12-23-34-45-56-553"},
1734 /* Added in Windows Server 2003 */
1735 /* 51 */ {TRUE
, "S-1-5-64-10"}, {TRUE
, "S-1-5-64-21"}, {TRUE
, "S-1-5-64-14"},
1736 /* 54 */ {TRUE
, "S-1-5-15"}, {TRUE
, "S-1-5-1000"}, {FALSE
, "S-1-5-32-557"},
1737 /* 57 */ {TRUE
, "S-1-5-32-558"}, {TRUE
, "S-1-5-32-559"}, {TRUE
, "S-1-5-32-560"},
1738 /* 60 */ {TRUE
, "S-1-5-32-561"}, {TRUE
, "S-1-5-32-562"},
1739 /* Added in Windows Vista: */
1740 /* 62 */ {TRUE
, "S-1-5-32-568"},
1741 /* 63 */ {TRUE
, "S-1-5-17"}, {FALSE
, "S-1-5-32-569"}, {TRUE
, "S-1-16-0"},
1742 /* 66 */ {TRUE
, "S-1-16-4096"}, {TRUE
, "S-1-16-8192"}, {TRUE
, "S-1-16-12288"},
1743 /* 69 */ {TRUE
, "S-1-16-16384"}, {TRUE
, "S-1-5-33"}, {TRUE
, "S-1-3-4"},
1744 /* 72 */ {FALSE
, "S-1-5-21-12-23-34-45-56-571"}, {FALSE
, "S-1-5-21-12-23-34-45-56-572"},
1745 /* 74 */ {TRUE
, "S-1-5-22"}, {FALSE
, "S-1-5-21-12-23-34-45-56-521"}, {TRUE
, "S-1-5-32-573"},
1746 /* 77 */ {FALSE
, "S-1-5-21-12-23-34-45-56-498"}, {TRUE
, "S-1-5-32-574"}, {TRUE
, "S-1-16-8448"}
1749 static void test_CreateWellKnownSid(void)
1751 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
1752 PSID domainsid
, sid
;
1757 if (!pCreateWellKnownSid
)
1759 win_skip("CreateWellKnownSid not available\n");
1764 SetLastError(0xdeadbeef);
1765 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1766 error
= GetLastError();
1767 ok(!ret
, "CreateWellKnownSid succeeded\n");
1768 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1769 ok(size
, "expected size > 0\n");
1771 SetLastError(0xdeadbeef);
1772 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1773 error
= GetLastError();
1774 ok(!ret
, "CreateWellKnownSid succeeded\n");
1775 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
1777 sid
= HeapAlloc(GetProcessHeap(), 0, size
);
1778 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, sid
, &size
);
1779 ok(ret
, "CreateWellKnownSid failed %u\n", GetLastError());
1780 HeapFree(GetProcessHeap(), 0, sid
);
1782 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1783 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
1785 for (i
= 0; i
< sizeof(well_known_sid_values
)/sizeof(well_known_sid_values
[0]); i
++)
1787 const struct well_known_sid_value
*value
= &well_known_sid_values
[i
];
1788 char sid_buffer
[SECURITY_MAX_SID_SIZE
];
1792 if (value
->sid_string
== NULL
)
1795 if (i
> WinAccountRasAndIasServersSid
)
1797 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1798 cb
= sizeof(sid_buffer
);
1799 if (!pCreateWellKnownSid(i
, domainsid
, sid_buffer
, &cb
))
1801 skip("Well known SIDs starting from %u are not implemented\n", i
);
1806 cb
= sizeof(sid_buffer
);
1807 ok(pCreateWellKnownSid(i
, value
->without_domain
? NULL
: domainsid
, sid_buffer
, &cb
), "Couldn't create well known sid %u\n", i
);
1808 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1809 ok(IsValidSid(sid_buffer
), "The sid is not valid\n");
1810 ok(pConvertSidToStringSidA(sid_buffer
, &str
), "Couldn't convert SID to string\n");
1811 ok(strcmp(str
, value
->sid_string
) == 0, "%d: SID mismatch - expected %s, got %s\n", i
,
1812 value
->sid_string
, str
);
1815 if (value
->without_domain
)
1817 char buf2
[SECURITY_MAX_SID_SIZE
];
1819 ok(pCreateWellKnownSid(i
, domainsid
, buf2
, &cb
), "Couldn't create well known sid %u with optional domain\n", i
);
1820 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1821 ok(memcmp(buf2
, sid_buffer
, cb
) == 0, "SID create with domain is different than without (%u)\n", i
);
1828 static void test_LookupAccountSid(void)
1830 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1831 CHAR accountA
[MAX_PATH
], domainA
[MAX_PATH
], usernameA
[MAX_PATH
];
1832 DWORD acc_sizeA
, dom_sizeA
, user_sizeA
;
1833 DWORD real_acc_sizeA
, real_dom_sizeA
;
1834 WCHAR accountW
[MAX_PATH
], domainW
[MAX_PATH
];
1835 DWORD acc_sizeW
, dom_sizeW
;
1836 DWORD real_acc_sizeW
, real_dom_sizeW
;
1837 PSID pUsersSid
= NULL
;
1840 DWORD error
, size
, cbti
= 0;
1845 PTOKEN_USER ptiUser
= NULL
;
1847 /* native windows crashes if account size, domain size, or name use is NULL */
1849 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1850 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &pUsersSid
);
1851 ok(ret
|| (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
),
1852 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1854 /* not running on NT so give up */
1855 if (!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1858 real_acc_sizeA
= MAX_PATH
;
1859 real_dom_sizeA
= MAX_PATH
;
1860 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1861 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1863 /* try NULL account */
1864 acc_sizeA
= MAX_PATH
;
1865 dom_sizeA
= MAX_PATH
;
1866 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1867 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1869 /* try NULL domain */
1870 acc_sizeA
= MAX_PATH
;
1871 dom_sizeA
= MAX_PATH
;
1872 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1873 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1875 /* try a small account buffer */
1877 dom_sizeA
= MAX_PATH
;
1879 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1880 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1881 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1882 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1884 /* try a 0 sized account buffer */
1886 dom_sizeA
= MAX_PATH
;
1888 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1889 /* this can fail or succeed depending on OS version but the size will always be returned */
1890 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1891 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1892 real_acc_sizeA
+ 1, acc_sizeA
);
1894 /* try a 0 sized account buffer */
1896 dom_sizeA
= MAX_PATH
;
1897 LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1898 /* this can fail or succeed depending on OS version but the size will always be returned */
1899 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1900 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1901 real_acc_sizeA
+ 1, acc_sizeA
);
1903 /* try a small domain buffer */
1905 acc_sizeA
= MAX_PATH
;
1907 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1908 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1909 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1910 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1912 /* try a 0 sized domain buffer */
1914 acc_sizeA
= MAX_PATH
;
1916 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1917 /* this can fail or succeed depending on OS version but the size will always be returned */
1918 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1919 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1920 real_dom_sizeA
+ 1, dom_sizeA
);
1922 /* try a 0 sized domain buffer */
1924 acc_sizeA
= MAX_PATH
;
1925 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1926 /* this can fail or succeed depending on OS version but the size will always be returned */
1927 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1928 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1929 real_dom_sizeA
+ 1, dom_sizeA
);
1931 real_acc_sizeW
= MAX_PATH
;
1932 real_dom_sizeW
= MAX_PATH
;
1933 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &real_acc_sizeW
, domainW
, &real_dom_sizeW
, &use
);
1934 ok(ret
, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1936 /* try an invalid system name */
1937 real_acc_sizeA
= MAX_PATH
;
1938 real_dom_sizeA
= MAX_PATH
;
1939 ret
= LookupAccountSidA("deepthought", pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1940 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1941 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
1942 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1944 /* native windows crashes if domainW or accountW is NULL */
1946 /* try a small account buffer */
1948 dom_sizeW
= MAX_PATH
;
1950 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1951 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1952 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1953 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1955 /* try a 0 sized account buffer */
1957 dom_sizeW
= MAX_PATH
;
1959 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1960 /* this can fail or succeed depending on OS version but the size will always be returned */
1961 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1962 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1963 real_acc_sizeW
+ 1, acc_sizeW
);
1965 /* try a 0 sized account buffer */
1967 dom_sizeW
= MAX_PATH
;
1968 LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1969 /* this can fail or succeed depending on OS version but the size will always be returned */
1970 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1971 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1972 real_acc_sizeW
+ 1, acc_sizeW
);
1974 /* try a small domain buffer */
1976 acc_sizeW
= MAX_PATH
;
1978 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1979 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1980 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1981 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1983 /* try a 0 sized domain buffer */
1985 acc_sizeW
= MAX_PATH
;
1987 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1988 /* this can fail or succeed depending on OS version but the size will always be returned */
1989 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1990 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1991 real_dom_sizeW
+ 1, dom_sizeW
);
1993 /* try a 0 sized domain buffer */
1995 acc_sizeW
= MAX_PATH
;
1996 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1997 /* this can fail or succeed depending on OS version but the size will always be returned */
1998 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1999 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
2000 real_dom_sizeW
+ 1, dom_sizeW
);
2002 acc_sizeW
= dom_sizeW
= use
= 0;
2003 SetLastError(0xdeadbeef);
2004 ret
= LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
2005 error
= GetLastError();
2006 ok(!ret
, "LookupAccountSidW failed %u\n", GetLastError());
2007 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
2008 ok(acc_sizeW
, "expected non-zero account size\n");
2009 ok(dom_sizeW
, "expected non-zero domain size\n");
2010 ok(!use
, "expected zero use %u\n", use
);
2014 /* Test LookupAccountSid with Sid retrieved from token information.
2015 This assumes this process is running under the account of the current user.*/
2016 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &hToken
);
2017 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2018 ret
= GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &cbti
);
2019 ok(!ret
, "GetTokenInformation failed with error %d\n", GetLastError());
2020 ptiUser
= HeapAlloc(GetProcessHeap(), 0, cbti
);
2021 if (GetTokenInformation(hToken
, TokenUser
, ptiUser
, cbti
, &cbti
))
2023 acc_sizeA
= dom_sizeA
= MAX_PATH
;
2024 ret
= LookupAccountSidA(NULL
, ptiUser
->User
.Sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
2025 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
2026 user_sizeA
= MAX_PATH
;
2027 ret
= GetUserNameA(usernameA
, &user_sizeA
);
2028 ok(ret
, "GetUserNameA() Expected TRUE, got FALSE\n");
2029 ok(lstrcmpA(usernameA
, accountA
) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA
, accountA
);
2031 HeapFree(GetProcessHeap(), 0, ptiUser
);
2033 if (pCreateWellKnownSid
&& pConvertSidToStringSidA
)
2035 trace("Well Known SIDs:\n");
2036 for (i
= 0; i
<= 60; i
++)
2038 size
= SECURITY_MAX_SID_SIZE
;
2039 if (pCreateWellKnownSid(i
, NULL
, &max_sid
.sid
, &size
))
2041 if (pConvertSidToStringSidA(&max_sid
.sid
, &str_sidA
))
2043 acc_sizeA
= MAX_PATH
;
2044 dom_sizeA
= MAX_PATH
;
2045 if (LookupAccountSidA(NULL
, &max_sid
.sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
))
2046 trace(" %d: %s %s\\%s %d\n", i
, str_sidA
, domainA
, accountA
, use
);
2047 LocalFree(str_sidA
);
2052 if (GetLastError() != ERROR_INVALID_PARAMETER
)
2053 trace(" CreateWellKnownSid(%d) failed: %d\n", i
, GetLastError());
2055 trace(" %d: not supported\n", i
);
2059 pLsaQueryInformationPolicy
= (void *)GetProcAddress( hmod
, "LsaQueryInformationPolicy");
2060 pLsaOpenPolicy
= (void *)GetProcAddress( hmod
, "LsaOpenPolicy");
2061 pLsaFreeMemory
= (void *)GetProcAddress( hmod
, "LsaFreeMemory");
2062 pLsaClose
= (void *)GetProcAddress( hmod
, "LsaClose");
2064 if (pLsaQueryInformationPolicy
&& pLsaOpenPolicy
&& pLsaFreeMemory
&& pLsaClose
)
2068 LSA_OBJECT_ATTRIBUTES object_attributes
;
2070 ZeroMemory(&object_attributes
, sizeof(object_attributes
));
2071 object_attributes
.Length
= sizeof(object_attributes
);
2073 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_ALL_ACCESS
, &handle
);
2074 ok(status
== STATUS_SUCCESS
|| status
== STATUS_ACCESS_DENIED
,
2075 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status
);
2077 /* try a more restricted access mask if necessary */
2078 if (status
== STATUS_ACCESS_DENIED
) {
2079 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
2080 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_VIEW_LOCAL_INFORMATION
, &handle
);
2081 ok(status
== STATUS_SUCCESS
, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status
);
2084 if (status
== STATUS_SUCCESS
)
2086 PPOLICY_ACCOUNT_DOMAIN_INFO info
;
2087 status
= pLsaQueryInformationPolicy(handle
, PolicyAccountDomainInformation
, (PVOID
*)&info
);
2088 ok(status
== STATUS_SUCCESS
, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status
);
2089 if (status
== STATUS_SUCCESS
)
2091 ok(info
->DomainSid
!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
2092 if (info
->DomainSid
)
2094 int count
= *GetSidSubAuthorityCount(info
->DomainSid
);
2095 CopySid(GetSidLengthRequired(count
), &max_sid
, info
->DomainSid
);
2096 test_sid_str((PSID
)&max_sid
.sid
);
2097 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_ADMIN
;
2098 max_sid
.sid
.SubAuthorityCount
= count
+ 1;
2099 test_sid_str((PSID
)&max_sid
.sid
);
2100 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_GUEST
;
2101 test_sid_str((PSID
)&max_sid
.sid
);
2102 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ADMINS
;
2103 test_sid_str((PSID
)&max_sid
.sid
);
2104 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_USERS
;
2105 test_sid_str((PSID
)&max_sid
.sid
);
2106 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_GUESTS
;
2107 test_sid_str((PSID
)&max_sid
.sid
);
2108 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_COMPUTERS
;
2109 test_sid_str((PSID
)&max_sid
.sid
);
2110 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CONTROLLERS
;
2111 test_sid_str((PSID
)&max_sid
.sid
);
2112 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CERT_ADMINS
;
2113 test_sid_str((PSID
)&max_sid
.sid
);
2114 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_SCHEMA_ADMINS
;
2115 test_sid_str((PSID
)&max_sid
.sid
);
2116 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
;
2117 test_sid_str((PSID
)&max_sid
.sid
);
2118 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_POLICY_ADMINS
;
2119 test_sid_str((PSID
)&max_sid
.sid
);
2120 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_ALIAS_RID_RAS_SERVERS
;
2121 test_sid_str((PSID
)&max_sid
.sid
);
2122 max_sid
.sid
.SubAuthority
[count
] = 1000; /* first user account */
2123 test_sid_str((PSID
)&max_sid
.sid
);
2126 pLsaFreeMemory((LPVOID
)info
);
2129 status
= pLsaClose(handle
);
2130 ok(status
== STATUS_SUCCESS
, "LsaClose() failed, returned 0x%08x\n", status
);
2136 static BOOL
get_sid_info(PSID psid
, LPSTR
*user
, LPSTR
*dom
)
2138 static CHAR account
[UNLEN
+ 1];
2139 static CHAR domain
[UNLEN
+ 1];
2140 DWORD size
, dom_size
;
2146 size
= dom_size
= UNLEN
+ 1;
2149 SetLastError(0xdeadbeef);
2150 return LookupAccountSidA(NULL
, psid
, account
, &size
, domain
, &dom_size
, &use
);
2153 static void check_wellknown_name(const char* name
, WELL_KNOWN_SID_TYPE result
)
2155 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
2156 PSID domainsid
= NULL
;
2157 char wk_sid
[SECURITY_MAX_SID_SIZE
];
2160 DWORD sid_size
, domain_size
;
2161 SID_NAME_USE sid_use
;
2162 LPSTR domain
, account
, sid_domain
, wk_domain
, wk_account
;
2168 ret
= LookupAccountNameA(NULL
, name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2169 ok(!ret
, " %s Should have failed to lookup account name\n", name
);
2170 psid
= HeapAlloc(GetProcessHeap(),0,sid_size
);
2171 domain
= HeapAlloc(GetProcessHeap(),0,domain_size
);
2172 ret
= LookupAccountNameA(NULL
, name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2176 ok(!ret
, " %s Should have failed to lookup account name\n",name
);
2180 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
2181 cb
= sizeof(wk_sid
);
2182 if (!pCreateWellKnownSid(result
, domainsid
, wk_sid
, &cb
))
2184 win_skip("SID %i is not available on the system\n",result
);
2188 ret2
= get_sid_info(wk_sid
, &wk_account
, &wk_domain
);
2189 if (!ret2
&& GetLastError() == ERROR_NONE_MAPPED
)
2191 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name
);
2195 get_sid_info(psid
, &account
, &sid_domain
);
2197 ok(ret
, "Failed to lookup account name %s\n",name
);
2198 ok(sid_size
!= 0, "sid_size was zero\n");
2200 ok(EqualSid(psid
,wk_sid
),"(%s) Sids fail to match well known sid!\n",name
);
2202 ok(!lstrcmpA(account
, wk_account
), "Expected %s , got %s\n", account
, wk_account
);
2203 ok(!lstrcmpA(domain
, wk_domain
), "Expected %s, got %s\n", wk_domain
, domain
);
2204 ok(sid_use
== SidTypeWellKnownGroup
, "Expected Use (5), got %d\n", sid_use
);
2208 HeapFree(GetProcessHeap(),0,psid
);
2209 HeapFree(GetProcessHeap(),0,domain
);
2212 static void test_LookupAccountName(void)
2214 DWORD sid_size
, domain_size
, user_size
;
2215 DWORD sid_save
, domain_save
;
2216 CHAR user_name
[UNLEN
+ 1];
2217 CHAR computer_name
[UNLEN
+ 1];
2218 SID_NAME_USE sid_use
;
2219 LPSTR domain
, account
, sid_dom
;
2223 /* native crashes if (assuming all other parameters correct):
2225 * - Sid is NULL and cbSid is > 0
2226 * - cbSid or cchReferencedDomainName are NULL
2227 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2230 user_size
= UNLEN
+ 1;
2231 SetLastError(0xdeadbeef);
2232 ret
= GetUserNameA(user_name
, &user_size
);
2233 ok(ret
, "Failed to get user name : %d\n", GetLastError());
2238 sid_use
= 0xcafebabe;
2239 SetLastError(0xdeadbeef);
2240 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2241 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2243 win_skip("LookupAccountNameA is not implemented\n");
2246 ok(!ret
, "Expected 0, got %d\n", ret
);
2247 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2248 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2249 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2250 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2251 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2253 sid_save
= sid_size
;
2254 domain_save
= domain_size
;
2256 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2257 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2259 /* try valid account name */
2260 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2261 get_sid_info(psid
, &account
, &sid_dom
);
2262 ok(ret
, "Failed to lookup account name\n");
2263 ok(sid_size
== GetLengthSid(psid
), "Expected %d, got %d\n", GetLengthSid(psid
), sid_size
);
2264 ok(!lstrcmpA(account
, user_name
), "Expected %s, got %s\n", user_name
, account
);
2265 ok(!lstrcmpA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2266 ok(domain_size
== domain_save
- 1, "Expected %d, got %d\n", domain_save
- 1, domain_size
);
2267 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2268 ok(sid_use
== SidTypeUser
, "Expected SidTypeUser (%d), got %d\n", SidTypeUser
, sid_use
);
2269 domain_size
= domain_save
;
2270 sid_size
= sid_save
;
2272 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2274 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2278 ret
= LookupAccountNameA(NULL
, "Everyone", psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2279 get_sid_info(psid
, &account
, &sid_dom
);
2280 ok(ret
, "Failed to lookup account name\n");
2281 ok(sid_size
!= 0, "sid_size was zero\n");
2282 ok(!lstrcmpA(account
, "Everyone"), "Expected Everyone, got %s\n", account
);
2283 ok(!lstrcmpA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2284 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2285 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2286 ok(sid_use
== SidTypeWellKnownGroup
, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup
, sid_use
);
2287 domain_size
= domain_save
;
2290 /* NULL Sid with zero sid size */
2291 SetLastError(0xdeadbeef);
2293 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2294 ok(!ret
, "Expected 0, got %d\n", ret
);
2295 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2296 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2297 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2298 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2300 /* try cchReferencedDomainName - 1 */
2301 SetLastError(0xdeadbeef);
2303 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2304 ok(!ret
, "Expected 0, got %d\n", ret
);
2305 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2306 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2307 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2308 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2310 /* NULL ReferencedDomainName with zero domain name size */
2311 SetLastError(0xdeadbeef);
2313 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2314 ok(!ret
, "Expected 0, got %d\n", ret
);
2315 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2316 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2317 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2318 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2320 HeapFree(GetProcessHeap(), 0, psid
);
2321 HeapFree(GetProcessHeap(), 0, domain
);
2323 /* get sizes for NULL account name */
2326 sid_use
= 0xcafebabe;
2327 SetLastError(0xdeadbeef);
2328 ret
= LookupAccountNameA(NULL
, NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2329 if (!ret
&& GetLastError() == ERROR_NONE_MAPPED
)
2330 win_skip("NULL account name doesn't work on NT4\n");
2333 ok(!ret
, "Expected 0, got %d\n", ret
);
2334 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2335 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2336 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2337 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2338 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2340 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2341 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2343 /* try NULL account name */
2344 ret
= LookupAccountNameA(NULL
, NULL
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2345 get_sid_info(psid
, &account
, &sid_dom
);
2346 ok(ret
, "Failed to lookup account name\n");
2347 /* Using a fixed string will not work on different locales */
2348 ok(!lstrcmpA(account
, domain
),
2349 "Got %s for account and %s for domain, these should be the same\n",
2351 ok(sid_use
== SidTypeDomain
, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain
, sid_use
);
2353 HeapFree(GetProcessHeap(), 0, psid
);
2354 HeapFree(GetProcessHeap(), 0, domain
);
2357 /* try an invalid account name */
2358 SetLastError(0xdeadbeef);
2361 ret
= LookupAccountNameA(NULL
, "oogabooga", NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2362 ok(!ret
, "Expected 0, got %d\n", ret
);
2363 ok(GetLastError() == ERROR_NONE_MAPPED
||
2364 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
),
2365 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2366 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2367 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2369 /* try an invalid system name */
2370 SetLastError(0xdeadbeef);
2373 ret
= LookupAccountNameA("deepthought", NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2374 ok(!ret
, "Expected 0, got %d\n", ret
);
2375 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
2376 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2377 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2378 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2380 /* try with the computer name as the account name */
2381 domain_size
= sizeof(computer_name
);
2382 GetComputerNameA(computer_name
, &domain_size
);
2385 ret
= LookupAccountNameA(NULL
, computer_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2386 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
||
2387 GetLastError() == ERROR_NONE_MAPPED
/* in a domain */ ||
2388 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE
) ||
2389 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
)),
2390 "LookupAccountNameA failed: %d\n", GetLastError());
2391 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2393 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2394 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2395 ret
= LookupAccountNameA(NULL
, computer_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2396 ok(ret
, "LookupAccountNameA failed: %d\n", GetLastError());
2397 ok(sid_use
== SidTypeDomain
||
2398 (sid_use
== SidTypeUser
&& ! strcmp(computer_name
, user_name
)), "expected SidTypeDomain for %s, got %d\n", computer_name
, sid_use
);
2399 HeapFree(GetProcessHeap(), 0, domain
);
2400 HeapFree(GetProcessHeap(), 0, psid
);
2403 /* Well Known names */
2404 if (!pCreateWellKnownSid
)
2406 win_skip("CreateWellKnownSid not available\n");
2410 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2412 skip("Non-English locale (skipping well known name creation tests)\n");
2416 check_wellknown_name("LocalService", WinLocalServiceSid
);
2417 check_wellknown_name("Local Service", WinLocalServiceSid
);
2419 check_wellknown_name("Local Service", 0);
2420 check_wellknown_name("NetworkService", WinNetworkServiceSid
);
2421 check_wellknown_name("Network Service", WinNetworkServiceSid
);
2423 /* example of some names where the spaces are not optional */
2424 check_wellknown_name("Terminal Server User", WinTerminalServerSid
);
2425 check_wellknown_name("TerminalServer User", 0);
2426 check_wellknown_name("TerminalServerUser", 0);
2427 check_wellknown_name("Terminal ServerUser", 0);
2429 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid
);
2430 check_wellknown_name("enterprisedomain controllers", 0);
2431 check_wellknown_name("enterprise domaincontrollers", 0);
2432 check_wellknown_name("enterprisedomaincontrollers", 0);
2434 /* case insensitivity */
2435 check_wellknown_name("lOCAlServICE", WinLocalServiceSid
);
2437 /* fully qualified account names */
2438 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid
);
2439 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid
);
2440 check_wellknown_name("nt authority test\\Network Service", 0);
2441 check_wellknown_name("Dummy\\Network Service", 0);
2442 check_wellknown_name("ntauthority\\Network Service", 0);
2445 static void test_security_descriptor(void)
2447 SECURITY_DESCRIPTOR sd
;
2450 BOOL isDefault
, isPresent
, ret
;
2454 SetLastError(0xdeadbeef);
2455 ret
= InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
2456 if (ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2458 win_skip("InitializeSecurityDescriptor is not implemented\n");
2462 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2463 expect_eq(psid
, NULL
, PSID
, "%p");
2464 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2465 sd
.Control
|= SE_DACL_PRESENT
| SE_SACL_PRESENT
;
2467 SetLastError(0xdeadbeef);
2469 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), FALSE
, BOOL
, "%d");
2470 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER
, DWORD
, "%u");
2471 ok(size
> 5, "Size not increased\n");
2474 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), TRUE
, BOOL
, "%d");
2475 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2476 expect_eq(psid
, NULL
, PSID
, "%p");
2477 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2478 ok(GetSecurityDescriptorGroup(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorGroup failed\n");
2479 expect_eq(psid
, NULL
, PSID
, "%p");
2480 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2481 ok(GetSecurityDescriptorDacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorDacl failed\n");
2482 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2483 expect_eq(psid
, NULL
, PSID
, "%p");
2484 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2485 ok(GetSecurityDescriptorSacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorSacl failed\n");
2486 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2487 expect_eq(psid
, NULL
, PSID
, "%p");
2488 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2492 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2493 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2494 static void test_granted_access(HANDLE handle
, ACCESS_MASK access
,
2495 ACCESS_MASK alt
, int line
)
2497 OBJECT_BASIC_INFORMATION obj_info
;
2500 if (!pNtQueryObject
)
2502 skip_(__FILE__
, line
)("Not NT platform - skipping tests\n");
2506 status
= pNtQueryObject( handle
, ObjectBasicInformation
, &obj_info
,
2507 sizeof(obj_info
), NULL
);
2508 ok_(__FILE__
, line
)(!status
, "NtQueryObject with err: %08x\n", status
);
2510 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
||
2511 obj_info
.GrantedAccess
== alt
, "Granted access should be 0x%08x "
2512 "or 0x%08x, instead of 0x%08x\n", access
, alt
, obj_info
.GrantedAccess
);
2514 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
, "Granted access should "
2515 "be 0x%08x, instead of 0x%08x\n", access
, obj_info
.GrantedAccess
);
2518 #define CHECK_SET_SECURITY(o,i,e) \
2522 SetLastError( 0xdeadbeef ); \
2523 res_ = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2524 err = GetLastError(); \
2525 if (e == ERROR_SUCCESS) \
2526 ok(res_, "SetKernelObjectSecurity failed with %d\n", err); \
2528 ok(!res_ && err == e, "SetKernelObjectSecurity should have failed " \
2529 "with %s, instead of %d\n", #e, err); \
2532 static void test_process_security(void)
2536 PTOKEN_PRIMARY_GROUP group
;
2537 PSID AdminSid
= NULL
, UsersSid
= NULL
;
2539 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
2540 char buffer
[MAX_PATH
];
2541 PROCESS_INFORMATION info
;
2542 STARTUPINFOA startup
;
2543 SECURITY_ATTRIBUTES psa
;
2544 HANDLE token
, event
;
2546 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2547 PSID EveryoneSid
= NULL
;
2549 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
2550 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
2551 if (!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2553 win_skip("ACLs not implemented - skipping tests\n");
2554 HeapFree(GetProcessHeap(), 0, Acl
);
2557 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2559 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2560 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2562 /* get owner from the token we might be running as a user not admin */
2563 res
= OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED
, &token
);
2564 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
2567 HeapFree(GetProcessHeap(), 0, Acl
);
2571 res
= GetTokenInformation( token
, TokenOwner
, NULL
, 0, &size
);
2572 ok(!res
, "Expected failure, got %d\n", res
);
2573 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2574 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2576 owner
= HeapAlloc(GetProcessHeap(), 0, size
);
2577 res
= GetTokenInformation( token
, TokenOwner
, owner
, size
, &size
);
2578 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2579 AdminSid
= ((TOKEN_OWNER
*)owner
)->Owner
;
2581 res
= GetTokenInformation( token
, TokenPrimaryGroup
, NULL
, 0, &size
);
2582 ok(!res
, "Expected failure, got %d\n", res
);
2583 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2584 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2586 group
= HeapAlloc(GetProcessHeap(), 0, size
);
2587 res
= GetTokenInformation( token
, TokenPrimaryGroup
, group
, size
, &size
);
2588 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2589 UsersSid
= ((TOKEN_PRIMARY_GROUP
*)group
)->PrimaryGroup
;
2591 CloseHandle( token
);
2594 HeapFree(GetProcessHeap(), 0, group
);
2595 HeapFree(GetProcessHeap(), 0, owner
);
2596 HeapFree(GetProcessHeap(), 0, Acl
);
2600 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, PROCESS_VM_READ
, AdminSid
);
2601 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2602 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, PROCESS_ALL_ACCESS
, AdminSid
);
2603 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2605 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
2606 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
2607 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2609 event
= CreateEventA( NULL
, TRUE
, TRUE
, "test_event" );
2610 ok(event
!= NULL
, "CreateEvent %d\n", GetLastError());
2612 SecurityDescriptor
->Revision
= 0;
2613 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_UNKNOWN_REVISION
);
2614 SecurityDescriptor
->Revision
= SECURITY_DESCRIPTOR_REVISION
;
2616 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2617 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2618 CHECK_SET_SECURITY( event
, SACL_SECURITY_INFORMATION
, ERROR_ACCESS_DENIED
);
2619 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2620 /* NULL DACL is valid and means that everyone has access */
2621 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
;
2622 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2624 /* Set owner and group and dacl */
2625 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
2626 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2627 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2628 test_owner_equal( event
, AdminSid
, __LINE__
);
2630 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, EveryoneSid
, FALSE
);
2631 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2632 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2633 test_group_equal( event
, EveryoneSid
, __LINE__
);
2635 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2636 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2637 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2638 /* setting a dacl should not change the owner or group */
2639 test_owner_equal( event
, AdminSid
, __LINE__
);
2640 test_group_equal( event
, EveryoneSid
, __LINE__
);
2642 /* Test again with a different SID in case the previous SID also happens to
2643 * be the one that is incorrectly replacing the group. */
2644 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, FALSE
);
2645 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2646 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2647 test_group_equal( event
, UsersSid
, __LINE__
);
2649 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2650 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2651 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2652 test_group_equal( event
, UsersSid
, __LINE__
);
2654 sprintf(buffer
, "%s tests/security.c test", myARGV
[0]);
2655 memset(&startup
, 0, sizeof(startup
));
2656 startup
.cb
= sizeof(startup
);
2657 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
2658 startup
.wShowWindow
= SW_SHOWNORMAL
;
2660 psa
.nLength
= sizeof(psa
);
2661 psa
.lpSecurityDescriptor
= SecurityDescriptor
;
2662 psa
.bInheritHandle
= TRUE
;
2664 /* Doesn't matter what ACL say we should get full access for ourselves */
2665 res
= CreateProcessA( NULL
, buffer
, &psa
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
2666 ok(res
, "CreateProcess with err:%d\n", GetLastError());
2667 TEST_GRANTED_ACCESS2( info
.hProcess
, PROCESS_ALL_ACCESS_NT4
,
2668 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2669 winetest_wait_child_process( info
.hProcess
);
2671 FreeSid(EveryoneSid
);
2672 CloseHandle( info
.hProcess
);
2673 CloseHandle( info
.hThread
);
2674 CloseHandle( event
);
2675 HeapFree(GetProcessHeap(), 0, group
);
2676 HeapFree(GetProcessHeap(), 0, owner
);
2677 HeapFree(GetProcessHeap(), 0, Acl
);
2678 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
2681 static void test_process_security_child(void)
2683 HANDLE handle
, handle1
;
2687 handle
= OpenProcess( PROCESS_TERMINATE
, FALSE
, GetCurrentProcessId() );
2688 ok(handle
!= NULL
, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2689 TEST_GRANTED_ACCESS( handle
, PROCESS_TERMINATE
);
2691 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2692 &handle1
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2693 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2694 TEST_GRANTED_ACCESS( handle1
, PROCESS_TERMINATE
);
2696 CloseHandle( handle1
);
2698 SetLastError( 0xdeadbeef );
2699 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2700 &handle1
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2701 err
= GetLastError();
2703 ok(!ret
&& err
== ERROR_ACCESS_DENIED
, "duplicating handle should have failed "
2704 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err
);
2706 CloseHandle( handle
);
2708 /* These two should fail - they are denied by ACL */
2709 handle
= OpenProcess( PROCESS_VM_READ
, FALSE
, GetCurrentProcessId() );
2711 ok(handle
== NULL
, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2712 handle
= OpenProcess( PROCESS_ALL_ACCESS
, FALSE
, GetCurrentProcessId() );
2714 ok(handle
== NULL
, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2716 /* Documented privilege elevation */
2717 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2718 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2719 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2720 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2721 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2723 CloseHandle( handle
);
2725 /* Same only explicitly asking for all access rights */
2726 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2727 &handle
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2728 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2729 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2730 PROCESS_ALL_ACCESS
| PROCESS_QUERY_LIMITED_INFORMATION
);
2731 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2732 &handle1
, PROCESS_VM_READ
, TRUE
, 0 );
2733 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2734 TEST_GRANTED_ACCESS( handle1
, PROCESS_VM_READ
);
2735 CloseHandle( handle1
);
2736 CloseHandle( handle
);
2739 static void test_impersonation_level(void)
2741 HANDLE Token
, ProcessToken
;
2744 TOKEN_PRIVILEGES
*Privileges
;
2746 PRIVILEGE_SET
*PrivilegeSet
;
2752 pDuplicateTokenEx
= (void *)GetProcAddress(hmod
, "DuplicateTokenEx");
2753 if( !pDuplicateTokenEx
) {
2754 win_skip("DuplicateTokenEx is not available\n");
2757 SetLastError(0xdeadbeef);
2758 ret
= ImpersonateSelf(SecurityAnonymous
);
2759 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2761 win_skip("ImpersonateSelf is not implemented\n");
2764 ok(ret
, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2765 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2766 ok(!ret
, "OpenThreadToken should have failed\n");
2767 error
= GetLastError();
2768 ok(error
== ERROR_CANT_OPEN_ANONYMOUS
, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2769 /* can't perform access check when opening object against an anonymous impersonation token */
2771 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2772 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_CANT_OPEN_ANONYMOUS
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2773 "RegOpenKeyEx failed with %d\n", error
);
2777 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
2778 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2780 ret
= pDuplicateTokenEx(ProcessToken
,
2781 TOKEN_QUERY
| TOKEN_DUPLICATE
| TOKEN_IMPERSONATE
, NULL
,
2782 SecurityAnonymous
, TokenImpersonation
, &Token
);
2783 ok(ret
, "DuplicateTokenEx failed with error %d\n", GetLastError());
2784 /* can't increase the impersonation level */
2785 ret
= DuplicateToken(Token
, SecurityIdentification
, &Token2
);
2786 error
= GetLastError();
2787 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2788 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2789 /* we can query anything from an anonymous token, including the user */
2790 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
2791 error
= GetLastError();
2792 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2793 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
2794 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
2795 ok(ret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2796 HeapFree(GetProcessHeap(), 0, User
);
2798 /* PrivilegeCheck fails with SecurityAnonymous level */
2799 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
2800 error
= GetLastError();
2801 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2802 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
2803 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
2804 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2806 PrivilegeSet
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[Privileges
->PrivilegeCount
]));
2807 PrivilegeSet
->PrivilegeCount
= Privileges
->PrivilegeCount
;
2808 memcpy(PrivilegeSet
->Privilege
, Privileges
->Privileges
, PrivilegeSet
->PrivilegeCount
* sizeof(PrivilegeSet
->Privilege
[0]));
2809 PrivilegeSet
->Control
= PRIVILEGE_SET_ALL_NECESSARY
;
2810 HeapFree(GetProcessHeap(), 0, Privileges
);
2812 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2813 error
= GetLastError();
2814 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2818 ret
= ImpersonateSelf(SecurityIdentification
);
2819 ok(ret
, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2820 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2821 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2823 /* can't perform access check when opening object against an identification impersonation token */
2824 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2826 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2827 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2829 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2830 ok(ret
, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2834 ret
= ImpersonateSelf(SecurityImpersonation
);
2835 ok(ret
, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2836 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2837 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2838 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2839 ok(error
== ERROR_SUCCESS
, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error
);
2841 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2842 ok(ret
, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2846 CloseHandle(ProcessToken
);
2848 HeapFree(GetProcessHeap(), 0, PrivilegeSet
);
2851 static void test_SetEntriesInAclW(void)
2854 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2855 PACL OldAcl
= NULL
, NewAcl
;
2856 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2857 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2858 EXPLICIT_ACCESSW ExplicitAccess
;
2859 static const WCHAR wszEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2860 static const WCHAR wszCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2862 if (!pSetEntriesInAclW
)
2864 win_skip("SetEntriesInAclW is not available\n");
2868 NewAcl
= (PACL
)0xdeadbeef;
2869 res
= pSetEntriesInAclW(0, NULL
, NULL
, &NewAcl
);
2870 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2872 win_skip("SetEntriesInAclW is not implemented\n");
2875 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2876 ok(NewAcl
== NULL
||
2877 broken(NewAcl
!= NULL
), /* NT4 */
2878 "NewAcl=%p, expected NULL\n", NewAcl
);
2881 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2882 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2883 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2885 win_skip("ACLs not implemented - skipping tests\n");
2886 HeapFree(GetProcessHeap(), 0, OldAcl
);
2889 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2891 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2892 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2894 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2895 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2896 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2898 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2899 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2901 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2902 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2903 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2904 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2905 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2906 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2907 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= 0xDEADBEEF;
2908 ExplicitAccess
.Trustee
.pMultipleTrustee
= (PVOID
)0xDEADBEEF;
2909 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2910 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2911 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2914 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2915 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2916 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2917 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2918 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2919 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2922 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2924 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2928 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2929 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszEveryone
;
2930 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2931 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2932 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2935 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2936 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2937 ok(res
== ERROR_INVALID_PARAMETER
||
2938 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2939 "SetEntriesInAclW failed: %u\n", res
);
2940 ok(NewAcl
== NULL
||
2941 broken(NewAcl
!= NULL
), /* NT4 */
2942 "returned acl wasn't NULL: %p\n", NewAcl
);
2944 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2945 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2946 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2947 ok(res
== ERROR_INVALID_PARAMETER
||
2948 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2949 "SetEntriesInAclW failed: %u\n", res
);
2950 ok(NewAcl
== NULL
||
2951 broken(NewAcl
!= NULL
), /* NT4 */
2952 "returned acl wasn't NULL: %p\n", NewAcl
);
2954 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2955 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2956 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2957 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2958 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2962 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2963 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszCurrentUser
;
2964 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2965 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2966 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2969 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
2970 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2971 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
2972 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2973 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2974 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2978 FreeSid(EveryoneSid
);
2979 HeapFree(GetProcessHeap(), 0, OldAcl
);
2982 static void test_SetEntriesInAclA(void)
2985 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2986 PACL OldAcl
= NULL
, NewAcl
;
2987 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2988 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2989 EXPLICIT_ACCESSA ExplicitAccess
;
2990 static const CHAR szEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2991 static const CHAR szCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2993 if (!pSetEntriesInAclA
)
2995 win_skip("SetEntriesInAclA is not available\n");
2999 NewAcl
= (PACL
)0xdeadbeef;
3000 res
= pSetEntriesInAclA(0, NULL
, NULL
, &NewAcl
);
3001 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
3003 win_skip("SetEntriesInAclA is not implemented\n");
3006 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3007 ok(NewAcl
== NULL
||
3008 broken(NewAcl
!= NULL
), /* NT4 */
3009 "NewAcl=%p, expected NULL\n", NewAcl
);
3012 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
3013 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
3014 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3016 win_skip("ACLs not implemented - skipping tests\n");
3017 HeapFree(GetProcessHeap(), 0, OldAcl
);
3020 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
3022 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
3023 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3025 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
3026 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
3027 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3029 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
3030 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
3032 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
3033 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
3034 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
3035 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
3036 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
3037 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
3038 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
3039 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
3040 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3041 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3042 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3045 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
3046 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
3047 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
3048 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3049 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3050 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3053 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
3055 skip("Non-English locale (test with hardcoded 'Everyone')\n");
3059 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3060 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szEveryone
;
3061 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3062 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3063 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3066 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
3067 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3068 ok(res
== ERROR_INVALID_PARAMETER
||
3069 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
3070 "SetEntriesInAclA failed: %u\n", res
);
3071 ok(NewAcl
== NULL
||
3072 broken(NewAcl
!= NULL
), /* NT4 */
3073 "returned acl wasn't NULL: %p\n", NewAcl
);
3075 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3076 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
3077 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3078 ok(res
== ERROR_INVALID_PARAMETER
||
3079 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
3080 "SetEntriesInAclA failed: %u\n", res
);
3081 ok(NewAcl
== NULL
||
3082 broken(NewAcl
!= NULL
), /* NT4 */
3083 "returned acl wasn't NULL: %p\n", NewAcl
);
3085 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
3086 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
3087 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3088 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3089 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3093 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3094 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szCurrentUser
;
3095 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3096 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3097 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3100 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
3101 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
3102 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
3103 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3104 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3105 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3109 FreeSid(EveryoneSid
);
3110 HeapFree(GetProcessHeap(), 0, OldAcl
);
3113 static void test_CreateDirectoryA(void)
3115 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3116 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3117 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
3118 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3119 PSECURITY_DESCRIPTOR pSD
= &sd
;
3120 ACL_SIZE_INFORMATION acl_size
;
3121 ACCESS_ALLOWED_ACE
*ace
;
3122 SECURITY_ATTRIBUTES sa
;
3123 char tmpdir
[MAX_PATH
];
3130 if (!pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3132 win_skip("Required functions are not available\n");
3136 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3138 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3139 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3143 win_skip("Failed to get current user token\n");
3146 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3147 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3148 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3149 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3150 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3151 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3152 CloseHandle( token
);
3153 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3155 sa
.nLength
= sizeof(sa
);
3156 sa
.lpSecurityDescriptor
= pSD
;
3157 sa
.bInheritHandle
= TRUE
;
3158 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3159 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3160 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3161 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3162 ok(bret
, "Failed to initialize ACL.\n");
3163 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3164 GENERIC_ALL
, user_sid
);
3165 ok(bret
, "Failed to add Current User to ACL.\n");
3166 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3167 GENERIC_ALL
, admin_sid
);
3168 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3169 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3170 ok(bret
, "Failed to add ACL to security desciptor.\n");
3172 GetTempPathA(MAX_PATH
, tmpdir
);
3173 lstrcatA(tmpdir
, "Please Remove Me");
3174 bret
= CreateDirectoryA(tmpdir
, &sa
);
3175 ok(bret
== TRUE
, "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
3176 HeapFree(GetProcessHeap(), 0, pDacl
);
3178 SetLastError(0xdeadbeef);
3179 error
= pGetNamedSecurityInfoA(tmpdir
, SE_FILE_OBJECT
,
3180 OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, (PSID
*)&owner
,
3181 NULL
, &pDacl
, NULL
, &pSD
);
3182 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3184 win_skip("GetNamedSecurityInfoA is not implemented\n");
3187 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3189 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3190 ok(bret
, "GetAclInformation failed\n");
3191 ok(acl_size
.AceCount
== 2, "GetAclInformation returned unexpected entry count (%d != 2).\n",
3193 if (acl_size
.AceCount
> 0)
3195 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3196 ok(bret
, "Failed to get Current User ACE.\n");
3197 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3198 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
3199 todo_wine
ok(((ACE_HEADER
*)ace
)->AceFlags
== (OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
),
3200 "Current User ACE has unexpected flags (0x%x != 0x03)\n",
3201 ((ACE_HEADER
*)ace
)->AceFlags
);
3202 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3205 if (acl_size
.AceCount
> 1)
3207 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3208 ok(bret
, "Failed to get Administators Group ACE.\n");
3209 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3210 todo_wine
ok(bret
, "Administators Group ACE != Administators Group SID.\n");
3211 todo_wine
ok(((ACE_HEADER
*)ace
)->AceFlags
== (OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
),
3212 "Administators Group ACE has unexpected flags (0x%x != 0x03)\n",
3213 ((ACE_HEADER
*)ace
)->AceFlags
);
3214 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3220 HeapFree(GetProcessHeap(), 0, user
);
3221 bret
= RemoveDirectoryA(tmpdir
);
3222 ok(bret
== TRUE
, "RemoveDirectoryA should always succeed\n");
3225 static void test_GetNamedSecurityInfoA(void)
3227 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3228 char system_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3229 char users_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3230 PSID admin_sid
= (PSID
) admin_ptr
, users_sid
= (PSID
) users_ptr
;
3231 PSID system_sid
= (PSID
) system_ptr
, user_sid
;
3232 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3233 char invalid_path
[] = "/an invalid file path";
3234 int users_ace_id
= -1, admins_ace_id
= -1, i
;
3235 char software_key
[] = "MACHINE\\Software";
3236 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3237 SECURITY_DESCRIPTOR_CONTROL control
;
3238 ACL_SIZE_INFORMATION acl_size
;
3239 CHAR windows_dir
[MAX_PATH
];
3240 PSECURITY_DESCRIPTOR pSD
;
3241 ACCESS_ALLOWED_ACE
*ace
;
3242 BOOL bret
= TRUE
, isNT4
;
3243 char tmpfile
[MAX_PATH
];
3244 DWORD error
, revision
;
3245 BOOL owner_defaulted
;
3246 BOOL group_defaulted
;
3247 BOOL dacl_defaulted
;
3248 HANDLE token
, hTemp
;
3254 if (!pSetNamedSecurityInfoA
|| !pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3256 win_skip("Required functions are not available\n");
3260 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3262 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3263 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3267 win_skip("Failed to get current user token\n");
3270 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3271 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3272 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3273 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3274 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3275 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3276 CloseHandle( token
);
3277 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3279 bret
= GetWindowsDirectoryA(windows_dir
, MAX_PATH
);
3280 ok(bret
, "GetWindowsDirectory failed with error %d\n", GetLastError());
3282 SetLastError(0xdeadbeef);
3283 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,
3284 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
3285 NULL
, NULL
, NULL
, NULL
, &pSD
);
3286 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3288 win_skip("GetNamedSecurityInfoA is not implemented\n");
3289 HeapFree(GetProcessHeap(), 0, user
);
3292 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3294 bret
= GetSecurityDescriptorControl(pSD
, &control
, &revision
);
3295 ok(bret
, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
3296 ok((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == (SE_SELF_RELATIVE
|SE_DACL_PRESENT
) ||
3297 broken((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
), /* NT4 */
3298 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control
);
3299 ok(revision
== SECURITY_DESCRIPTOR_REVISION1
, "revision was %d instead of 1\n", revision
);
3301 isNT4
= (control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
;
3303 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3304 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3305 ok(owner
!= NULL
, "owner should not be NULL\n");
3307 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3308 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3309 ok(group
!= NULL
, "group should not be NULL\n");
3313 /* NULL descriptor tests */
3316 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
3317 HeapFree(GetProcessHeap(), 0, user
);
3321 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3322 NULL
, NULL
, NULL
, NULL
, NULL
);
3323 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3326 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3327 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3328 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3329 ok(pDacl
!= NULL
, "DACL should not be NULL\n");
3332 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,OWNER_SECURITY_INFORMATION
,
3333 NULL
, NULL
, &pDacl
, NULL
, NULL
);
3334 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3336 /* Test behavior of SetNamedSecurityInfo with an invalid path */
3337 SetLastError(0xdeadbeef);
3338 error
= pSetNamedSecurityInfoA(invalid_path
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3340 ok(error
== ERROR_FILE_NOT_FOUND
, "Unexpected error returned: 0x%x\n", error
);
3341 ok(GetLastError() == 0xdeadbeef, "Expected last error to remain unchanged.\n");
3343 /* Create security descriptor information and test that it comes back the same */
3345 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3346 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3347 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3348 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3349 ok(bret
, "Failed to initialize ACL.\n");
3350 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3351 ok(bret
, "Failed to add Current User to ACL.\n");
3352 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
3353 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3354 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3355 ok(bret
, "Failed to add ACL to security desciptor.\n");
3356 GetTempFileNameA(".", "foo", 0, tmpfile
);
3357 hTemp
= CreateFileA(tmpfile
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
3358 FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
3359 SetLastError(0xdeadbeef);
3360 error
= pSetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3362 HeapFree(GetProcessHeap(), 0, pDacl
);
3363 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3365 win_skip("SetNamedSecurityInfoA is not implemented\n");
3366 HeapFree(GetProcessHeap(), 0, user
);
3370 ok(!error
, "SetNamedSecurityInfoA failed with error %d\n", error
);
3371 SetLastError(0xdeadbeef);
3372 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3373 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3374 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3376 win_skip("GetNamedSecurityInfoA is not implemented\n");
3377 HeapFree(GetProcessHeap(), 0, user
);
3381 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3383 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3384 ok(bret
, "GetAclInformation failed\n");
3385 if (acl_size
.AceCount
> 0)
3387 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3388 ok(bret
, "Failed to get Current User ACE.\n");
3389 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3390 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
3391 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3392 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3393 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3396 if (acl_size
.AceCount
> 1)
3398 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3399 ok(bret
, "Failed to get Administators Group ACE.\n");
3400 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3401 todo_wine
ok(bret
|| broken(!bret
) /* win2k */,
3402 "Administators Group ACE != Administators Group SID.\n");
3403 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3404 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3405 ok(ace
->Mask
== 0x1f01ff || broken(ace
->Mask
== GENERIC_ALL
) /* win2k */,
3406 "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n", ace
->Mask
);
3409 HeapFree(GetProcessHeap(), 0, user
);
3412 /* Test querying the ownership of a built-in registry key */
3413 sid_size
= sizeof(system_ptr
);
3414 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, system_sid
, &sid_size
);
3415 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
,
3416 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
,
3417 NULL
, NULL
, NULL
, NULL
, &pSD
);
3418 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3420 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3421 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3422 ok(owner
!= NULL
, "owner should not be NULL\n");
3423 ok(EqualSid(owner
, admin_sid
), "MACHINE\\Software owner SID != Administrators SID.\n");
3425 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3426 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3427 ok(group
!= NULL
, "group should not be NULL\n");
3428 ok(EqualSid(group
, admin_sid
) || broken(EqualSid(group
, system_sid
)) /* before Win7 */
3429 || broken(((SID
*)group
)->SubAuthority
[0] == SECURITY_NT_NON_UNIQUE
) /* Vista */,
3430 "MACHINE\\Software group SID != Local System SID.\n");
3433 /* Test querying the DACL of a built-in registry key */
3434 sid_size
= sizeof(users_ptr
);
3435 pCreateWellKnownSid(WinBuiltinUsersSid
, NULL
, users_sid
, &sid_size
);
3436 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
, DACL_SECURITY_INFORMATION
,
3437 NULL
, NULL
, NULL
, NULL
, &pSD
);
3438 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3440 bret
= GetSecurityDescriptorDacl(pSD
, &dacl_present
, &pDacl
, &dacl_defaulted
);
3441 ok(bret
, "GetSecurityDescriptorDacl failed with error %d\n", GetLastError());
3442 ok(dacl_present
, "DACL should be present\n");
3443 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityDescriptorDacl returned invalid DACL.\n");
3444 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3445 ok(bret
, "GetAclInformation failed\n");
3446 ok(acl_size
.AceCount
!= 0, "GetAclInformation returned no ACLs\n");
3447 for (i
=0; i
<acl_size
.AceCount
; i
++)
3449 bret
= pGetAce(pDacl
, i
, (VOID
**)&ace
);
3450 ok(bret
, "Failed to get ACE %d.\n", i
);
3451 bret
= EqualSid(&ace
->SidStart
, users_sid
);
3452 if (bret
) users_ace_id
= i
;
3453 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3454 if (bret
) admins_ace_id
= i
;
3456 ok(users_ace_id
!= -1 || broken(users_ace_id
== -1) /* win2k */,
3457 "Bultin Users ACE not found.\n");
3458 if (users_ace_id
!= -1)
3460 bret
= pGetAce(pDacl
, users_ace_id
, (VOID
**)&ace
);
3461 ok(bret
, "Failed to get Builtin Users ACE.\n");
3462 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3463 ok(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
)
3464 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */,
3465 "Builtin Users ACE has unexpected flags (0x%x != 0x%x)\n", flags
,
3466 INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
);
3467 ok(ace
->Mask
== GENERIC_READ
, "Builtin Users ACE has unexpected mask (0x%x != 0x%x)\n",
3468 ace
->Mask
, GENERIC_READ
);
3470 ok(admins_ace_id
!= -1, "Bultin Admins ACE not found.\n");
3471 if (admins_ace_id
!= -1)
3473 bret
= pGetAce(pDacl
, admins_ace_id
, (VOID
**)&ace
);
3474 ok(bret
, "Failed to get Builtin Admins ACE.\n");
3475 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3477 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */,
3478 "Builtin Admins ACE has unexpected flags (0x%x != 0x0)\n", flags
);
3479 ok(ace
->Mask
== KEY_ALL_ACCESS
|| broken(ace
->Mask
== GENERIC_ALL
) /* w2k8 */,
3480 "Builtin Admins ACE has unexpected mask (0x%x != 0x%x)\n", ace
->Mask
, KEY_ALL_ACCESS
);
3485 static void test_ConvertStringSecurityDescriptor(void)
3488 PSECURITY_DESCRIPTOR pSD
;
3489 static const WCHAR Blank
[] = { 0 };
3493 const char *sidstring
;
3500 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE
, ERROR_UNKNOWN_REVISION
},
3501 /* test ACE string type */
3502 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3503 { "D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3504 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_PARAMETER
},
3505 /* test ACE string with spaces */
3506 { " D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3507 { "D: (D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3508 { "D:( D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3509 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, RPC_S_INVALID_STRING_UUID
, ERROR_INVALID_ACL
}, /* Vista+ */
3510 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3511 { "D:(D;; GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3512 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3513 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1
, TRUE
},
3514 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1
, TRUE
},
3515 { "D:(D;;GA;;; WD)", SDDL_REVISION_1
, TRUE
},
3516 { "D:(D;;GA;;;WD )", SDDL_REVISION_1
, TRUE
},
3517 /* test ACE string access rights */
3518 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3519 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1
, TRUE
},
3520 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1
, TRUE
},
3521 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1
, TRUE
},
3522 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1
, TRUE
},
3523 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1
, TRUE
},
3524 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3525 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3526 /* test ACE string access right error case */
3527 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3528 /* test behaviour with empty strings */
3529 { "", SDDL_REVISION_1
, TRUE
},
3530 /* test ACE string SID */
3531 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1
, TRUE
},
3532 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
, ERROR_INVALID_SID
} /* W2K */
3535 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3537 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3541 for (i
= 0; i
< sizeof(cssd
)/sizeof(cssd
[0]); i
++)
3545 SetLastError(0xdeadbeef);
3546 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3547 cssd
[i
].sidstring
, cssd
[i
].revision
, &pSD
, NULL
);
3548 GLE
= GetLastError();
3549 ok(ret
== cssd
[i
].ret
, "(%02u) Expected %s (%d)\n", i
, cssd
[i
].ret
? "success" : "failure", GLE
);
3551 ok(GLE
== cssd
[i
].GLE
||
3552 (cssd
[i
].altGLE
&& GLE
== cssd
[i
].altGLE
),
3553 "(%02u) Unexpected last error %d\n", i
, GLE
);
3558 /* test behaviour with NULL parameters */
3559 SetLastError(0xdeadbeef);
3560 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3561 NULL
, 0xdeadbeef, &pSD
, NULL
);
3563 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3564 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3567 SetLastError(0xdeadbeef);
3568 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3569 NULL
, 0xdeadbeef, &pSD
, NULL
);
3570 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3571 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3574 SetLastError(0xdeadbeef);
3575 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3576 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL
, NULL
);
3577 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3578 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3581 SetLastError(0xdeadbeef);
3582 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3583 "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, NULL
, NULL
);
3584 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3585 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3588 /* test behaviour with empty strings */
3589 SetLastError(0xdeadbeef);
3590 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3591 Blank
, SDDL_REVISION_1
, &pSD
, NULL
);
3592 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
3596 static void test_ConvertSecurityDescriptorToString(void)
3598 SECURITY_DESCRIPTOR desc
;
3599 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3608 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA
)
3610 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
3613 if (!pCreateWellKnownSid
)
3615 win_skip("CreateWellKnownSid is not available\n");
3619 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
3620 * don't replicate this feature so we only test len >= strlen+1. */
3621 #define CHECK_RESULT_AND_FREE(exp_str) \
3622 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
3623 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlenA(exp_str) + 1, len); \
3626 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
3627 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); \
3628 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); \
3631 InitializeSecurityDescriptor(&desc
, SECURITY_DESCRIPTOR_REVISION
);
3632 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3633 CHECK_RESULT_AND_FREE("");
3636 pCreateWellKnownSid(WinLocalSid
, NULL
, sid_buf
, &size
);
3637 SetSecurityDescriptorOwner(&desc
, sid_buf
, FALSE
);
3638 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3639 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3641 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3642 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3643 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3645 size
= sizeof(sid_buf
);
3646 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, sid_buf
, &size
);
3647 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3648 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3649 CHECK_RESULT_AND_FREE("O:SY");
3651 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
);
3652 SetSecurityDescriptorGroup(&desc
, psid
, TRUE
);
3653 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3654 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
3656 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, GROUP_SECURITY_INFORMATION
, &string
, &len
), "Conversion failed\n");
3657 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3659 pacl
= (PACL
)acl_buf
;
3660 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3661 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, TRUE
);
3662 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3663 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3665 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, FALSE
);
3666 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3667 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3669 pConvertStringSidToSidA("S-1-5-6", &psid2
);
3670 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, 0xf0000000, psid2
);
3671 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3672 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
3674 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, INHERIT_ONLY_ACE
|INHERITED_ACE
, 0x00000003, psid2
);
3675 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3676 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
3678 pAddAccessDeniedAceEx(pacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
, 0xffffffff, psid
);
3679 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3680 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)");
3683 pacl
= (PACL
)acl_buf
;
3684 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3685 SetSecurityDescriptorSacl(&desc
, TRUE
, pacl
, FALSE
);
3686 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3687 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
3689 /* fails in win2k */
3690 SetSecurityDescriptorDacl(&desc
, TRUE
, NULL
, FALSE
);
3691 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, VALID_INHERIT_FLAGS
, KEY_READ
|KEY_WRITE
, psid2
, TRUE
, TRUE
);
3692 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3694 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
3695 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
3698 /* fails in win2k */
3699 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, psid2
, TRUE
, FALSE
);
3700 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3702 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
3703 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
3710 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec
)
3712 SECURITY_DESCRIPTOR_CONTROL ref
;
3713 SECURITY_DESCRIPTOR_CONTROL test
;
3715 SECURITY_DESCRIPTOR_CONTROL
const mutable
3716 = SE_DACL_AUTO_INHERIT_REQ
| SE_SACL_AUTO_INHERIT_REQ
3717 | SE_DACL_AUTO_INHERITED
| SE_SACL_AUTO_INHERITED
3718 | SE_DACL_PROTECTED
| SE_SACL_PROTECTED
3719 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
3721 SECURITY_DESCRIPTOR_CONTROL
const immutable
3722 = SE_OWNER_DEFAULTED
| SE_GROUP_DEFAULTED
3723 | SE_DACL_PRESENT
| SE_DACL_DEFAULTED
3724 | SE_SACL_PRESENT
| SE_SACL_DEFAULTED
3725 | SE_RM_CONTROL_VALID
| SE_SELF_RELATIVE
3730 LPCSTR fmt
= "Expected error %s, got %u\n";
3732 GetSecurityDescriptorControl (sec
, &ref
, &dwRevision
);
3734 /* The mutable bits are mutable regardless of the truth of
3735 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
3737 /* Check call barfs if any bit-of-interest is immutable */
3738 for (bit
= 0; bit
< 16; ++bit
)
3740 SECURITY_DESCRIPTOR_CONTROL
const bitOfInterest
= 1 << bit
;
3741 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitOfInterest
;
3743 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3745 DWORD dwExpect
= (bitOfInterest
& immutable
)
3746 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3747 LPCSTR strExpect
= (bitOfInterest
& immutable
)
3748 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3750 ctrl
= (bitOfInterest
& mutable) ? ref
+ bitOfInterest
: ref
;
3751 setOrClear
^= bitOfInterest
;
3752 SetLastError (0xbebecaca);
3753 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3754 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3755 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3756 expect_eq(test
, ctrl
, int, "%x");
3758 setOrClear
^= bitOfInterest
;
3759 SetLastError (0xbebecaca);
3760 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3761 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3762 GetSecurityDescriptorControl (sec
, &test
, &dwRevision
);
3763 expect_eq(test
, ref
, int, "%x");
3766 /* Check call barfs if any bit-to-set is immutable
3767 even when not a bit-of-interest */
3768 for (bit
= 0; bit
< 16; ++bit
)
3770 SECURITY_DESCRIPTOR_CONTROL
const bitsOfInterest
= mutable;
3771 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitsOfInterest
;
3773 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3775 DWORD dwExpect
= ((1 << bit
) & immutable
)
3776 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3777 LPCSTR strExpect
= ((1 << bit
) & immutable
)
3778 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3780 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| mutable;
3781 setOrClear
^= bitsOfInterest
;
3782 SetLastError (0xbebecaca);
3783 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3784 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3785 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3786 expect_eq(test
, ctrl
, int, "%x");
3788 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| (1 << bit
);
3789 setOrClear
^= bitsOfInterest
;
3790 SetLastError (0xbebecaca);
3791 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3792 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3793 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3794 expect_eq(test
, ctrl
, int, "%x");
3798 static void test_PrivateObjectSecurity(void)
3800 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3801 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3802 PSECURITY_DESCRIPTOR sec
;
3808 PSECURITY_DESCRIPTOR buf
;
3811 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3813 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3817 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3819 "G:S-1-5-21-93476-23408-4576"
3820 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3821 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3822 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3823 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3825 test_SetSecurityDescriptorControl(sec
);
3829 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3831 "G:S-1-5-21-93476-23408-4576",
3832 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3834 test_SetSecurityDescriptorControl(sec
);
3838 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3840 "G:S-1-5-21-93476-23408-4576"
3841 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3842 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3843 buf
= HeapAlloc(GetProcessHeap(), 0, dwDescSize
);
3844 pSetSecurityDescriptorControl(sec
, SE_DACL_PROTECTED
, SE_DACL_PROTECTED
);
3845 GetSecurityDescriptorControl(sec
, &ctrl
, &dwRevision
);
3846 expect_eq(ctrl
, 0x9014, int, "%x");
3848 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3849 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3850 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3851 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3852 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3853 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3854 expect_eq(ctrl
, 0x8000, int, "%x");
3856 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3857 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3858 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3859 ret
= pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
);
3860 ok(ret
, "Conversion failed err=%u\n", GetLastError());
3861 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)",
3862 "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 */
3863 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3864 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8004, int, "%x");
3866 ret
= GetPrivateObjectSecurity(sec
, sec_info
, buf
, dwDescSize
, &retSize
);
3867 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3868 ok(retSize
== dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3869 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3870 CHECK_ONE_OF_AND_FREE("O:SY"
3871 "G:S-1-5-21-93476-23408-4576"
3872 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3873 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3875 "G:S-1-5-21-93476-23408-4576"
3876 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3877 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3878 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3879 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8014, int, "%x");
3881 SetLastError(0xdeadbeef);
3882 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, 5, &retSize
) == FALSE
, "GetPrivateObjectSecurity should have failed\n");
3883 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3886 HeapFree(GetProcessHeap(), 0, buf
);
3888 #undef CHECK_RESULT_AND_FREE
3889 #undef CHECK_ONE_OF_AND_FREE
3891 static void test_acls(void)
3894 PACL pAcl
= (PACL
)buffer
;
3897 SetLastError(0xdeadbeef);
3898 ret
= InitializeAcl(pAcl
, sizeof(ACL
) - 1, ACL_REVISION
);
3899 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3901 win_skip("InitializeAcl is not implemented\n");
3905 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3907 SetLastError(0xdeadbeef);
3908 ret
= InitializeAcl(pAcl
, 0xffffffff, ACL_REVISION
);
3909 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3911 SetLastError(0xdeadbeef);
3912 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION1
);
3913 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3915 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION2
);
3916 ok(ret
, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3918 ret
= IsValidAcl(pAcl
);
3919 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3921 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION3
);
3922 ok(ret
, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3924 ret
= IsValidAcl(pAcl
);
3925 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3927 SetLastError(0xdeadbeef);
3928 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION4
);
3929 if (GetLastError() != ERROR_INVALID_PARAMETER
)
3931 ok(ret
, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3933 ret
= IsValidAcl(pAcl
);
3934 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3937 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3939 SetLastError(0xdeadbeef);
3940 ret
= InitializeAcl(pAcl
, sizeof(buffer
), -1);
3941 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3944 static void test_GetSecurityInfo(void)
3946 char b
[sizeof(TOKEN_USER
) + sizeof(SID
) + sizeof(DWORD
)*SID_MAX_SUB_AUTHORITIES
];
3947 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], dacl
[100];
3948 DWORD sid_size
= sizeof(admin_ptr
), l
= sizeof(b
);
3949 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
3950 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3951 ACL_SIZE_INFORMATION acl_size
;
3952 PSECURITY_DESCRIPTOR pSD
;
3953 ACCESS_ALLOWED_ACE
*ace
;
3960 if (!pGetSecurityInfo
|| !pSetSecurityInfo
)
3962 win_skip("[Get|Set]SecurityInfo is not available\n");
3966 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3968 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3969 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3973 win_skip("Failed to get current user token\n");
3976 GetTokenInformation(token
, TokenUser
, b
, l
, &l
);
3977 CloseHandle( token
);
3978 user_sid
= ((TOKEN_USER
*)b
)->User
.Sid
;
3980 /* Create something. Files have lots of associated security info. */
3981 obj
= CreateFileA(myARGV
[0], GENERIC_READ
|WRITE_DAC
, FILE_SHARE_READ
, NULL
,
3982 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3983 if (obj
== INVALID_HANDLE_VALUE
)
3985 skip("Couldn't create an object for GetSecurityInfo test\n");
3989 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3990 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3991 &owner
, &group
, &pDacl
, NULL
, &pSD
);
3992 if (ret
== ERROR_CALL_NOT_IMPLEMENTED
)
3994 win_skip("GetSecurityInfo is not implemented\n");
3998 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3999 ok(pSD
!= NULL
, "GetSecurityInfo\n");
4000 ok(owner
!= NULL
, "GetSecurityInfo\n");
4001 ok(group
!= NULL
, "GetSecurityInfo\n");
4003 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
4005 win_skip("No ACL information returned\n");
4009 if (!pCreateWellKnownSid
)
4011 win_skip("NULL parameter test would crash on NT4\n");
4016 /* If we don't ask for the security descriptor, Windows will still give us
4017 the other stuff, leaving us no way to free it. */
4018 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
4019 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4020 &owner
, &group
, &pDacl
, NULL
, NULL
);
4021 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
4022 ok(owner
!= NULL
, "GetSecurityInfo\n");
4023 ok(group
!= NULL
, "GetSecurityInfo\n");
4025 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
4027 win_skip("No ACL information returned\n");
4029 /* Create security descriptor information and test that it comes back the same */
4031 pDacl
= (PACL
)&dacl
;
4032 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
4033 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
4034 bret
= InitializeAcl(pDacl
, sizeof(dacl
), ACL_REVISION
);
4035 ok(bret
, "Failed to initialize ACL.\n");
4036 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
4037 ok(bret
, "Failed to add Current User to ACL.\n");
4038 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
4039 ok(bret
, "Failed to add Administrator Group to ACL.\n");
4040 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
4041 ok(bret
, "Failed to add ACL to security desciptor.\n");
4042 ret
= pSetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
4043 NULL
, NULL
, pDacl
, NULL
);
4044 ok(ret
== ERROR_SUCCESS
, "SetSecurityInfo returned %d\n", ret
);
4045 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
4046 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
4047 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
4048 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityInfo returned invalid DACL.\n");
4049 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
4050 ok(bret
, "GetAclInformation failed\n");
4051 if (acl_size
.AceCount
> 0)
4053 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
4054 ok(bret
, "Failed to get Current User ACE.\n");
4055 bret
= EqualSid(&ace
->SidStart
, user_sid
);
4056 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
4057 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
4058 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
4059 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
4062 if (acl_size
.AceCount
> 1)
4064 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
4065 ok(bret
, "Failed to get Administators Group ACE.\n");
4066 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
4067 todo_wine
ok(bret
, "Administators Group ACE != Administators Group SID.\n");
4068 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
4069 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
4070 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
4077 static void test_GetSidSubAuthority(void)
4081 if (!pGetSidSubAuthority
|| !pConvertStringSidToSidA
|| !pIsValidSid
|| !pGetSidSubAuthorityCount
)
4083 win_skip("Some functions not available\n");
4086 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
4087 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
4088 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid
),"ConvertStringSidToSidA failed\n");
4089 ok(pIsValidSid(psid
),"Sid is not valid\n");
4090 SetLastError(0xbebecaca);
4091 ok(*pGetSidSubAuthorityCount(psid
) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid
));
4092 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4093 SetLastError(0xbebecaca);
4094 ok(*pGetSidSubAuthority(psid
,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid
,0));
4095 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4096 SetLastError(0xbebecaca);
4097 ok(*pGetSidSubAuthority(psid
,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid
,1));
4098 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4099 SetLastError(0xbebecaca);
4100 ok(pGetSidSubAuthority(psid
,4) != NULL
,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
4101 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4105 static void test_CheckTokenMembership(void)
4107 PTOKEN_GROUPS token_groups
;
4109 HANDLE process_token
, token
;
4114 if (!pCheckTokenMembership
)
4116 win_skip("CheckTokenMembership is not available\n");
4119 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4120 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
4122 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
4123 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
4126 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4127 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4128 "GetTokenInformation(TokenGroups) %s with error %d\n",
4129 ret
? "succeeded" : "failed", GetLastError());
4130 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4131 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4132 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
4134 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4136 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4140 if (i
== token_groups
->GroupCount
)
4142 HeapFree(GetProcessHeap(), 0, token_groups
);
4144 skip("user not a member of any group\n");
4149 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4150 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4151 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4154 ret
= pCheckTokenMembership(NULL
, token_groups
->Groups
[i
].Sid
, &is_member
);
4155 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4156 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4159 SetLastError(0xdeadbeef);
4160 ret
= pCheckTokenMembership(process_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4161 ok(!ret
&& GetLastError() == ERROR_NO_IMPERSONATION_TOKEN
,
4162 "CheckTokenMembership with process token %s with error %d\n",
4163 ret
? "succeeded" : "failed", GetLastError());
4164 ok(!is_member
, "CheckTokenMembership should have cleared is_member\n");
4166 HeapFree(GetProcessHeap(), 0, token_groups
);
4168 CloseHandle(process_token
);
4171 static void test_EqualSid(void)
4175 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
4176 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
4178 SetLastError(0xdeadbeef);
4179 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4180 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid1
);
4181 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
4183 win_skip("AllocateAndInitializeSid is not implemented\n");
4186 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4187 ok(GetLastError() == 0xdeadbeef,
4188 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
4191 ret
= AllocateAndInitializeSid(&SIDAuthWorld
, 1, SECURITY_WORLD_RID
,
4192 0, 0, 0, 0, 0, 0, 0, &sid2
);
4193 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4195 SetLastError(0xdeadbeef);
4196 ret
= EqualSid(sid1
, sid2
);
4197 ok(!ret
, "World and domain admins sids shouldn't have been equal\n");
4198 ok(GetLastError() == ERROR_SUCCESS
||
4199 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4200 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4203 SetLastError(0xdeadbeef);
4204 sid2
= FreeSid(sid2
);
4205 ok(!sid2
, "FreeSid should have returned NULL instead of %p\n", sid2
);
4206 ok(GetLastError() == 0xdeadbeef,
4207 "FreeSid shouldn't have set last error to %d\n",
4210 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4211 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid2
);
4212 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4214 SetLastError(0xdeadbeef);
4215 ret
= EqualSid(sid1
, sid2
);
4216 ok(ret
, "Same sids should have been equal\n");
4217 ok(GetLastError() == ERROR_SUCCESS
||
4218 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4219 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4222 ((SID
*)sid2
)->Revision
= 2;
4223 SetLastError(0xdeadbeef);
4224 ret
= EqualSid(sid1
, sid2
);
4225 ok(!ret
, "EqualSid with invalid sid should have returned FALSE\n");
4226 ok(GetLastError() == ERROR_SUCCESS
||
4227 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4228 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4230 ((SID
*)sid2
)->Revision
= SID_REVISION
;
4236 static void test_GetUserNameA(void)
4238 char buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4239 DWORD required_len
, buffer_len
;
4242 /* Test crashes on Windows. */
4245 SetLastError(0xdeadbeef);
4246 GetUserNameA(NULL
, NULL
);
4249 SetLastError(0xdeadbeef);
4251 ret
= GetUserNameA(NULL
, &required_len
);
4252 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4253 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4254 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4256 SetLastError(0xdeadbeef);
4258 ret
= GetUserNameA(NULL
, &required_len
);
4259 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4260 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4261 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4263 /* Tests crashes on Windows. */
4266 SetLastError(0xdeadbeef);
4267 required_len
= UNLEN
+ 1;
4268 GetUserNameA(NULL
, &required_len
);
4270 SetLastError(0xdeadbeef);
4271 GetUserNameA(buffer
, NULL
);
4274 memset(filler
, 'x', sizeof(filler
));
4276 /* Note that GetUserNameA on XP and newer outputs the number of bytes
4277 * required for a Unicode string, which affects a test in the next block. */
4278 SetLastError(0xdeadbeef);
4279 memcpy(buffer
, filler
, sizeof(filler
));
4281 ret
= GetUserNameA(buffer
, &required_len
);
4282 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4283 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4284 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4285 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4287 SetLastError(0xdeadbeef);
4288 memcpy(buffer
, filler
, sizeof(filler
));
4289 buffer_len
= required_len
;
4290 ret
= GetUserNameA(buffer
, &buffer_len
);
4291 ok(ret
== TRUE
, "GetUserNameA returned %d, last error %u\n", ret
, GetLastError());
4292 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4293 ok(buffer_len
== required_len
||
4294 broken(buffer_len
== required_len
/ sizeof(WCHAR
)), /* XP+ */
4295 "Outputted buffer length was %u\n", buffer_len
);
4297 /* Use the reported buffer size from the last GetUserNameA call and pass
4298 * a length that is one less than the required value. */
4299 SetLastError(0xdeadbeef);
4300 memcpy(buffer
, filler
, sizeof(filler
));
4302 ret
= GetUserNameA(buffer
, &buffer_len
);
4303 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4304 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was untouched\n");
4305 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4306 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4309 static void test_GetUserNameW(void)
4311 WCHAR buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4312 DWORD required_len
, buffer_len
;
4315 /* Test crashes on Windows. */
4318 SetLastError(0xdeadbeef);
4319 GetUserNameW(NULL
, NULL
);
4322 SetLastError(0xdeadbeef);
4324 ret
= GetUserNameW(NULL
, &required_len
);
4325 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4326 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4327 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4329 SetLastError(0xdeadbeef);
4331 ret
= GetUserNameW(NULL
, &required_len
);
4332 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4333 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4334 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4336 /* Tests crash on Windows. */
4339 SetLastError(0xdeadbeef);
4340 required_len
= UNLEN
+ 1;
4341 GetUserNameW(NULL
, &required_len
);
4343 SetLastError(0xdeadbeef);
4344 GetUserNameW(buffer
, NULL
);
4347 memset(filler
, 'x', sizeof(filler
));
4349 SetLastError(0xdeadbeef);
4350 memcpy(buffer
, filler
, sizeof(filler
));
4352 ret
= GetUserNameW(buffer
, &required_len
);
4353 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4354 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4355 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4356 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4358 SetLastError(0xdeadbeef);
4359 memcpy(buffer
, filler
, sizeof(filler
));
4360 buffer_len
= required_len
;
4361 ret
= GetUserNameW(buffer
, &buffer_len
);
4362 ok(ret
== TRUE
, "GetUserNameW returned %d, last error %u\n", ret
, GetLastError());
4363 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4364 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4366 /* GetUserNameW on XP and newer writes a truncated portion of the username string to the buffer. */
4367 SetLastError(0xdeadbeef);
4368 memcpy(buffer
, filler
, sizeof(filler
));
4370 ret
= GetUserNameW(buffer
, &buffer_len
);
4371 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4372 ok(!memcmp(buffer
, filler
, sizeof(filler
)) ||
4373 broken(memcmp(buffer
, filler
, sizeof(filler
)) != 0), /* XP+ */
4374 "Output buffer was altered\n");
4375 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4376 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4379 static void test_CreateRestrictedToken(void)
4381 HANDLE process_token
, token
, r_token
;
4382 PTOKEN_GROUPS token_groups
, groups2
;
4383 SID_AND_ATTRIBUTES sattr
;
4384 SECURITY_IMPERSONATION_LEVEL level
;
4391 if (!pCreateRestrictedToken
)
4393 win_skip("CreateRestrictedToken is not available\n");
4397 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4398 ok(ret
, "got error %d\n", GetLastError());
4400 ret
= DuplicateTokenEx(process_token
, TOKEN_DUPLICATE
|TOKEN_ADJUST_GROUPS
|TOKEN_QUERY
,
4401 NULL
, SecurityImpersonation
, TokenImpersonation
, &token
);
4402 ok(ret
, "got error %d\n", GetLastError());
4405 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4406 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4407 "got %d with error %d\n", ret
, GetLastError());
4408 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4409 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4410 ok(ret
, "got error %d\n", GetLastError());
4412 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4414 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4418 if (i
== token_groups
->GroupCount
)
4420 HeapFree(GetProcessHeap(), 0, token_groups
);
4422 skip("User not a member of any group\n");
4427 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4428 ok(ret
, "got error %d\n", GetLastError());
4429 ok(is_member
, "not a member\n");
4431 /* disable a SID in new token */
4432 sattr
.Sid
= token_groups
->Groups
[i
].Sid
;
4433 sattr
.Attributes
= 0;
4435 ret
= pCreateRestrictedToken(token
, 0, 1, &sattr
, 0, NULL
, 0, NULL
, &r_token
);
4436 ok(ret
, "got error %d\n", GetLastError());
4440 /* check if a SID is enabled */
4442 ret
= pCheckTokenMembership(r_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4443 ok(ret
, "got error %d\n", GetLastError());
4444 todo_wine
ok(!is_member
, "not a member\n");
4446 ret
= GetTokenInformation(r_token
, TokenGroups
, NULL
, 0, &size
);
4447 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "got %d with error %d\n",
4448 ret
, GetLastError());
4449 groups2
= HeapAlloc(GetProcessHeap(), 0, size
);
4450 ret
= GetTokenInformation(r_token
, TokenGroups
, groups2
, size
, &size
);
4451 ok(ret
, "got error %d\n", GetLastError());
4453 for (j
= 0; j
< groups2
->GroupCount
; j
++)
4455 if (EqualSid(groups2
->Groups
[j
].Sid
, token_groups
->Groups
[i
].Sid
))
4459 todo_wine
ok(groups2
->Groups
[j
].Attributes
& SE_GROUP_USE_FOR_DENY_ONLY
,
4460 "got wrong attributes\n");
4461 todo_wine
ok((groups2
->Groups
[j
].Attributes
& SE_GROUP_ENABLED
) == 0,
4462 "got wrong attributes\n");
4464 HeapFree(GetProcessHeap(), 0, groups2
);
4466 size
= sizeof(type
);
4467 ret
= GetTokenInformation(r_token
, TokenType
, &type
, size
, &size
);
4468 ok(ret
, "got error %d\n", GetLastError());
4469 ok(type
== TokenImpersonation
, "got type %u\n", type
);
4471 size
= sizeof(level
);
4472 ret
= GetTokenInformation(r_token
, TokenImpersonationLevel
, &level
, size
, &size
);
4473 ok(ret
, "got error %d\n", GetLastError());
4474 ok(level
== SecurityImpersonation
, "got level %u\n", type
);
4477 HeapFree(GetProcessHeap(), 0, token_groups
);
4478 CloseHandle(r_token
);
4480 CloseHandle(process_token
);
4483 static void validate_default_security_descriptor(SECURITY_DESCRIPTOR
*sd
)
4485 BOOL ret
, present
, defaulted
;
4489 ret
= IsValidSecurityDescriptor(sd
);
4490 ok(ret
, "security descriptor is not valid\n");
4494 acl
= (void *)0xdeadbeef;
4495 SetLastError(0xdeadbeef);
4496 ret
= GetSecurityDescriptorDacl(sd
, &present
, &acl
, &defaulted
);
4497 ok(ret
, "GetSecurityDescriptorDacl error %d\n", GetLastError());
4499 ok(present
== 1, "acl is not present\n");
4501 ok(acl
!= (void *)0xdeadbeef && acl
!= NULL
, "acl pointer is not set\n");
4502 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4505 sid
= (void *)0xdeadbeef;
4506 SetLastError(0xdeadbeef);
4507 ret
= GetSecurityDescriptorOwner(sd
, &sid
, &defaulted
);
4508 ok(ret
, "GetSecurityDescriptorOwner error %d\n", GetLastError());
4510 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4511 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4514 sid
= (void *)0xdeadbeef;
4515 SetLastError(0xdeadbeef);
4516 ret
= GetSecurityDescriptorGroup(sd
, &sid
, &defaulted
);
4517 ok(ret
, "GetSecurityDescriptorGroup error %d\n", GetLastError());
4519 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4520 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4523 static void test_default_handle_security(HANDLE token
, HANDLE handle
, GENERIC_MAPPING
*mapping
)
4525 DWORD ret
, granted
, priv_set_len
;
4527 PRIVILEGE_SET priv_set
;
4528 SECURITY_DESCRIPTOR
*sd
;
4530 sd
= test_get_security_descriptor(handle
, __LINE__
);
4531 validate_default_security_descriptor(sd
);
4533 priv_set_len
= sizeof(priv_set
);
4534 granted
= 0xdeadbeef;
4535 status
= 0xdeadbeef;
4536 SetLastError(0xdeadbeef);
4537 ret
= AccessCheck(sd
, token
, MAXIMUM_ALLOWED
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4539 ok(ret
, "AccessCheck error %d\n", GetLastError());
4540 ok(status
== 1, "expected 1, got %d\n", status
);
4541 ok(granted
== mapping
->GenericAll
, "expected all access %#x, got %#x\n", mapping
->GenericAll
, granted
);
4543 priv_set_len
= sizeof(priv_set
);
4544 granted
= 0xdeadbeef;
4545 status
= 0xdeadbeef;
4546 SetLastError(0xdeadbeef);
4547 ret
= AccessCheck(sd
, token
, 0, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4549 ok(ret
, "AccessCheck error %d\n", GetLastError());
4550 ok(status
== 0 || broken(status
== 1) /* NT4 */, "expected 0, got %d\n", status
);
4551 ok(granted
== 0 || broken(granted
== mapping
->GenericRead
) /* NT4 */, "expected 0, got %#x\n", granted
);
4553 priv_set_len
= sizeof(priv_set
);
4554 granted
= 0xdeadbeef;
4555 status
= 0xdeadbeef;
4556 SetLastError(0xdeadbeef);
4557 ret
= AccessCheck(sd
, token
, ACCESS_SYSTEM_SECURITY
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4559 ok(ret
, "AccessCheck error %d\n", GetLastError());
4560 ok(status
== 0, "expected 0, got %d\n", status
);
4561 ok(granted
== 0, "expected 0, got %#x\n", granted
);
4563 priv_set_len
= sizeof(priv_set
);
4564 granted
= 0xdeadbeef;
4565 status
= 0xdeadbeef;
4566 SetLastError(0xdeadbeef);
4567 ret
= AccessCheck(sd
, token
, mapping
->GenericRead
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4569 ok(ret
, "AccessCheck error %d\n", GetLastError());
4570 ok(status
== 1, "expected 1, got %d\n", status
);
4571 ok(granted
== mapping
->GenericRead
, "expected read access %#x, got %#x\n", mapping
->GenericRead
, granted
);
4573 priv_set_len
= sizeof(priv_set
);
4574 granted
= 0xdeadbeef;
4575 status
= 0xdeadbeef;
4576 SetLastError(0xdeadbeef);
4577 ret
= AccessCheck(sd
, token
, mapping
->GenericWrite
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4579 ok(ret
, "AccessCheck error %d\n", GetLastError());
4580 ok(status
== 1, "expected 1, got %d\n", status
);
4581 ok(granted
== mapping
->GenericWrite
, "expected write access %#x, got %#x\n", mapping
->GenericWrite
, granted
);
4583 priv_set_len
= sizeof(priv_set
);
4584 granted
= 0xdeadbeef;
4585 status
= 0xdeadbeef;
4586 SetLastError(0xdeadbeef);
4587 ret
= AccessCheck(sd
, token
, mapping
->GenericExecute
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4589 ok(ret
, "AccessCheck error %d\n", GetLastError());
4590 ok(status
== 1, "expected 1, got %d\n", status
);
4591 ok(granted
== mapping
->GenericExecute
, "expected execute access %#x, got %#x\n", mapping
->GenericExecute
, granted
);
4593 HeapFree(GetProcessHeap(), 0, sd
);
4596 static ACCESS_MASK
get_obj_access(HANDLE obj
)
4598 OBJECT_BASIC_INFORMATION info
;
4601 if (!pNtQueryObject
) return 0;
4603 status
= pNtQueryObject(obj
, ObjectBasicInformation
, &info
, sizeof(info
), NULL
);
4604 ok(!status
, "NtQueryObject error %#x\n", status
);
4606 return info
.GrantedAccess
;
4609 static void test_mutex_security(HANDLE token
)
4611 DWORD ret
, i
, access
;
4613 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
| SYNCHRONIZE
,
4614 STANDARD_RIGHTS_WRITE
| MUTEX_MODIFY_STATE
| SYNCHRONIZE
,
4615 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4616 STANDARD_RIGHTS_ALL
| MUTEX_ALL_ACCESS
};
4619 int generic
, mapped
;
4623 { GENERIC_READ
, STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
},
4624 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
},
4625 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4626 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| MUTANT_QUERY_STATE
}
4629 SetLastError(0xdeadbeef);
4630 mutex
= OpenMutexA(0, FALSE
, "WineTestMutex");
4631 ok(!mutex
, "mutex should not exist\n");
4632 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4634 SetLastError(0xdeadbeef);
4635 mutex
= CreateMutexA(NULL
, FALSE
, "WineTestMutex");
4636 ok(mutex
!= 0, "CreateMutex error %d\n", GetLastError());
4638 access
= get_obj_access(mutex
);
4639 ok(access
== MUTANT_ALL_ACCESS
, "expected MUTANT_ALL_ACCESS, got %#x\n", access
);
4641 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4643 SetLastError( 0xdeadbeef );
4644 ret
= DuplicateHandle(GetCurrentProcess(), mutex
, GetCurrentProcess(), &dup
,
4645 map
[i
].generic
, FALSE
, 0);
4646 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4648 access
= get_obj_access(dup
);
4649 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4653 SetLastError(0xdeadbeef);
4654 dup
= OpenMutexA(0, FALSE
, "WineTestMutex");
4656 ok(!dup
, "OpenMutex should fail\n");
4658 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
4661 test_default_handle_security(token
, mutex
, &mapping
);
4663 CloseHandle (mutex
);
4666 static void test_event_security(HANDLE token
)
4668 DWORD ret
, i
, access
;
4670 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
| SYNCHRONIZE
,
4671 STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
| SYNCHRONIZE
,
4672 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4673 STANDARD_RIGHTS_ALL
| EVENT_ALL_ACCESS
};
4676 int generic
, mapped
;
4680 { GENERIC_READ
, STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
},
4681 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
},
4682 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4683 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| EVENT_QUERY_STATE
| EVENT_MODIFY_STATE
}
4686 SetLastError(0xdeadbeef);
4687 event
= OpenEventA(0, FALSE
, "WineTestEvent");
4688 ok(!event
, "event should not exist\n");
4689 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4691 SetLastError(0xdeadbeef);
4692 event
= CreateEventA(NULL
, FALSE
, FALSE
, "WineTestEvent");
4693 ok(event
!= 0, "CreateEvent error %d\n", GetLastError());
4695 access
= get_obj_access(event
);
4696 ok(access
== EVENT_ALL_ACCESS
, "expected EVENT_ALL_ACCESS, got %#x\n", access
);
4698 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4700 SetLastError( 0xdeadbeef );
4701 ret
= DuplicateHandle(GetCurrentProcess(), event
, GetCurrentProcess(), &dup
,
4702 map
[i
].generic
, FALSE
, 0);
4703 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4705 access
= get_obj_access(dup
);
4706 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4710 SetLastError(0xdeadbeef);
4711 dup
= OpenEventA(0, FALSE
, "WineTestEvent");
4713 ok(!dup
, "OpenEvent should fail\n");
4715 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
4718 test_default_handle_security(token
, event
, &mapping
);
4723 static void test_semaphore_security(HANDLE token
)
4725 DWORD ret
, i
, access
;
4727 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
,
4728 STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
,
4729 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4730 STANDARD_RIGHTS_ALL
| SEMAPHORE_ALL_ACCESS
};
4733 int generic
, mapped
;
4737 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
},
4738 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
},
4739 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4740 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| SEMAPHORE_QUERY_STATE
| SEMAPHORE_MODIFY_STATE
}
4743 SetLastError(0xdeadbeef);
4744 sem
= OpenSemaphoreA(0, FALSE
, "WineTestSemaphore");
4745 ok(!sem
, "semaphore should not exist\n");
4746 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4748 SetLastError(0xdeadbeef);
4749 sem
= CreateSemaphoreA(NULL
, 0, 10, "WineTestSemaphore");
4750 ok(sem
!= 0, "CreateSemaphore error %d\n", GetLastError());
4752 access
= get_obj_access(sem
);
4753 ok(access
== SEMAPHORE_ALL_ACCESS
, "expected SEMAPHORE_ALL_ACCESS, got %#x\n", access
);
4755 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4757 SetLastError( 0xdeadbeef );
4758 ret
= DuplicateHandle(GetCurrentProcess(), sem
, GetCurrentProcess(), &dup
,
4759 map
[i
].generic
, FALSE
, 0);
4760 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4762 access
= get_obj_access(dup
);
4763 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4768 test_default_handle_security(token
, sem
, &mapping
);
4773 #define WINE_TEST_PIPE "\\\\.\\pipe\\WineTestPipe"
4774 static void test_named_pipe_security(HANDLE token
)
4776 DWORD ret
, i
, access
;
4777 HANDLE pipe
, file
, dup
;
4778 GENERIC_MAPPING mapping
= { FILE_GENERIC_READ
,
4780 FILE_GENERIC_EXECUTE
,
4781 STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
};
4784 int todo
, generic
, mapped
;
4788 { 1, GENERIC_READ
, FILE_GENERIC_READ
},
4789 { 1, GENERIC_WRITE
, FILE_GENERIC_WRITE
},
4790 { 1, GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
4791 { 1, GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
4797 } creation_access
[] =
4799 { PIPE_ACCESS_INBOUND
, FILE_GENERIC_READ
},
4800 { PIPE_ACCESS_OUTBOUND
, FILE_GENERIC_WRITE
},
4801 { PIPE_ACCESS_DUPLEX
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
},
4802 { PIPE_ACCESS_INBOUND
|WRITE_DAC
, FILE_GENERIC_READ
|WRITE_DAC
},
4803 { PIPE_ACCESS_INBOUND
|WRITE_OWNER
, FILE_GENERIC_READ
|WRITE_OWNER
}
4804 /* ACCESS_SYSTEM_SECURITY is also valid, but will fail with ERROR_PRIVILEGE_NOT_HELD */
4807 /* Test the different security access options for pipes */
4808 for (i
= 0; i
< sizeof(creation_access
)/sizeof(creation_access
[0]); i
++)
4810 SetLastError(0xdeadbeef);
4811 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, creation_access
[i
].open_mode
,
4812 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
, 0, 0,
4813 NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
4814 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe(0x%x) error %d\n",
4815 creation_access
[i
].open_mode
, GetLastError());
4816 access
= get_obj_access(pipe
);
4817 ok(access
== creation_access
[i
].access
,
4818 "CreateNamedPipeA(0x%x) pipe expected access 0x%x (got 0x%x)\n",
4819 creation_access
[i
].open_mode
, creation_access
[i
].access
, access
);
4823 SetLastError(0xdeadbeef);
4824 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_FIRST_PIPE_INSTANCE
,
4825 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
,
4826 0, 0, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
4827 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe error %d\n", GetLastError());
4829 test_default_handle_security(token
, pipe
, &mapping
);
4831 SetLastError(0xdeadbeef);
4832 file
= CreateFileA(WINE_TEST_PIPE
, FILE_ALL_ACCESS
, 0, NULL
, OPEN_EXISTING
, 0, 0);
4833 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4835 access
= get_obj_access(file
);
4836 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4838 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4840 SetLastError( 0xdeadbeef );
4841 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4842 map
[i
].generic
, FALSE
, 0);
4843 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4845 access
= get_obj_access(dup
);
4846 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4854 SetLastError(0xdeadbeef);
4855 file
= CreateFileA("\\\\.\\pipe\\", FILE_ALL_ACCESS
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0);
4856 ok(file
!= INVALID_HANDLE_VALUE
|| broken(file
== INVALID_HANDLE_VALUE
) /* before Vista */, "CreateFile error %d\n", GetLastError());
4858 if (file
!= INVALID_HANDLE_VALUE
)
4860 access
= get_obj_access(file
);
4861 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4863 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4865 SetLastError( 0xdeadbeef );
4866 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4867 map
[i
].generic
, FALSE
, 0);
4868 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4870 access
= get_obj_access(dup
);
4873 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4875 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4884 static void test_file_security(HANDLE token
)
4886 DWORD ret
, i
, access
, bytes
;
4890 int generic
, mapped
;
4894 { GENERIC_READ
, FILE_GENERIC_READ
},
4895 { GENERIC_WRITE
, FILE_GENERIC_WRITE
},
4896 { GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
4897 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
4899 char temp_path
[MAX_PATH
];
4900 char file_name
[MAX_PATH
];
4903 GetTempPathA(MAX_PATH
, temp_path
);
4904 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
4907 SetLastError(0xdeadbeef);
4908 file
= CreateFileA(file_name
, GENERIC_ALL
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
4909 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4911 access
= get_obj_access(file
);
4912 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4914 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4916 SetLastError( 0xdeadbeef );
4917 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4918 map
[i
].generic
, FALSE
, 0);
4919 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4921 access
= get_obj_access(dup
);
4922 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4929 SetLastError(0xdeadbeef);
4930 file
= CreateFileA(file_name
, 0, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
4931 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4933 access
= get_obj_access(file
);
4935 ok(access
== (FILE_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
4938 SetLastError(0xdeadbeef);
4939 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
4940 ok(!ret
, "ReadFile should fail\n");
4941 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4942 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
4946 SetLastError(0xdeadbeef);
4947 file
= CreateFileA(file_name
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
4948 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4950 access
= get_obj_access(file
);
4952 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
4955 SetLastError(0xdeadbeef);
4956 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
4957 ok(!ret
, "ReadFile should fail\n");
4958 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4959 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
4962 DeleteFileA(file_name
);
4965 SetLastError(0xdeadbeef);
4966 file
= CreateFileA(temp_path
, GENERIC_ALL
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
4967 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4969 access
= get_obj_access(file
);
4970 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4972 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4974 SetLastError( 0xdeadbeef );
4975 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4976 map
[i
].generic
, FALSE
, 0);
4977 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4979 access
= get_obj_access(dup
);
4980 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4987 SetLastError(0xdeadbeef);
4988 file
= CreateFileA(temp_path
, 0, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
4989 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4991 access
= get_obj_access(file
);
4993 ok(access
== (FILE_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
4997 SetLastError(0xdeadbeef);
4998 file
= CreateFileA(temp_path
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
4999 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5001 access
= get_obj_access(file
);
5003 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
5008 static void test_filemap_security(void)
5010 char temp_path
[MAX_PATH
];
5011 char file_name
[MAX_PATH
];
5012 DWORD ret
, i
, access
;
5013 HANDLE file
, mapping
, dup
;
5016 int generic
, mapped
;
5020 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SECTION_QUERY
| SECTION_MAP_READ
},
5021 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SECTION_MAP_WRITE
},
5022 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SECTION_MAP_EXECUTE
},
5023 { GENERIC_ALL
, STANDARD_RIGHTS_REQUIRED
| SECTION_ALL_ACCESS
}
5031 { PAGE_NOACCESS
, 0 },
5032 { PAGE_READONLY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
5033 { PAGE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
},
5034 { PAGE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
5035 { PAGE_EXECUTE
, 0 },
5036 { PAGE_EXECUTE_READ
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
},
5037 { PAGE_EXECUTE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
},
5038 { PAGE_EXECUTE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
}
5041 GetTempPathA(MAX_PATH
, temp_path
);
5042 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
5044 SetLastError(0xdeadbeef);
5045 file
= CreateFileA(file_name
, GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
5046 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5047 SetFilePointer(file
, 4096, NULL
, FILE_BEGIN
);
5050 for (i
= 0; i
< sizeof(prot_map
)/sizeof(prot_map
[0]); i
++)
5052 SetLastError(0xdeadbeef);
5053 mapping
= CreateFileMappingW(file
, NULL
, prot_map
[i
].prot
, 0, 4096, NULL
);
5054 if (prot_map
[i
].mapped
)
5058 /* NT4 and win2k don't support EXEC on file mappings */
5059 if (prot_map
[i
].prot
== PAGE_EXECUTE_READ
|| prot_map
[i
].prot
== PAGE_EXECUTE_READWRITE
|| prot_map
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
5061 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
5065 ok(mapping
!= 0, "CreateFileMapping(%04x) error %d\n", prot_map
[i
].prot
, GetLastError());
5069 ok(!mapping
, "CreateFileMapping(%04x) should fail\n", prot_map
[i
].prot
);
5070 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
5074 access
= get_obj_access(mapping
);
5075 ok(access
== prot_map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, prot_map
[i
].mapped
, access
);
5077 CloseHandle(mapping
);
5080 SetLastError(0xdeadbeef);
5081 mapping
= CreateFileMappingW(file
, NULL
, PAGE_EXECUTE_READWRITE
, 0, 4096, NULL
);
5084 /* NT4 and win2k don't support EXEC on file mappings */
5085 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
5087 DeleteFileA(file_name
);
5090 ok(mapping
!= 0, "CreateFileMapping error %d\n", GetLastError());
5092 access
= get_obj_access(mapping
);
5093 ok(access
== (STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
),
5094 "expected STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE, got %#x\n", access
);
5096 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5098 SetLastError( 0xdeadbeef );
5099 ret
= DuplicateHandle(GetCurrentProcess(), mapping
, GetCurrentProcess(), &dup
,
5100 map
[i
].generic
, FALSE
, 0);
5101 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5103 access
= get_obj_access(dup
);
5104 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5109 CloseHandle(mapping
);
5111 DeleteFileA(file_name
);
5114 static void test_thread_security(void)
5116 DWORD ret
, i
, access
;
5120 int generic
, mapped
;
5124 { GENERIC_READ
, STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
},
5125 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
| THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
| 0x4 },
5126 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5127 { GENERIC_ALL
, THREAD_ALL_ACCESS_NT4
}
5130 SetLastError(0xdeadbeef);
5131 thread
= CreateThread(NULL
, 0, (void *)0xdeadbeef, NULL
, CREATE_SUSPENDED
, &ret
);
5132 ok(thread
!= 0, "CreateThread error %d\n", GetLastError());
5134 access
= get_obj_access(thread
);
5135 ok(access
== THREAD_ALL_ACCESS_NT4
|| access
== THREAD_ALL_ACCESS_VISTA
, "expected THREAD_ALL_ACCESS, got %#x\n", access
);
5137 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5139 SetLastError( 0xdeadbeef );
5140 ret
= DuplicateHandle(GetCurrentProcess(), thread
, GetCurrentProcess(), &dup
,
5141 map
[i
].generic
, FALSE
, 0);
5142 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5144 access
= get_obj_access(dup
);
5145 switch (map
[i
].generic
)
5148 case GENERIC_EXECUTE
:
5149 ok(access
== map
[i
].mapped
||
5150 access
== (map
[i
].mapped
| THREAD_QUERY_LIMITED_INFORMATION
) /* Vista+ */ ||
5151 access
== (map
[i
].mapped
| THREAD_QUERY_LIMITED_INFORMATION
| THREAD_RESUME
) /* win8 */,
5152 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5156 ok(access
== map
[i
].mapped
||
5157 access
== (map
[i
].mapped
| THREAD_SET_LIMITED_INFORMATION
) /* Vista+ */ ||
5158 access
== (map
[i
].mapped
| THREAD_SET_LIMITED_INFORMATION
| THREAD_RESUME
) /* win8 */,
5159 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5162 ok(access
== map
[i
].mapped
|| access
== THREAD_ALL_ACCESS_VISTA
,
5163 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5166 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5173 TerminateThread(thread
, 0);
5174 CloseHandle(thread
);
5177 static void test_process_access(void)
5179 DWORD ret
, i
, access
;
5180 HANDLE process
, dup
;
5182 PROCESS_INFORMATION pi
;
5183 char cmdline
[] = "winver.exe";
5186 int generic
, mapped
;
5190 { GENERIC_READ
, STANDARD_RIGHTS_READ
| PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
},
5191 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| PROCESS_SET_QUOTA
| PROCESS_SET_INFORMATION
| PROCESS_SUSPEND_RESUME
|
5192 PROCESS_VM_WRITE
| PROCESS_DUP_HANDLE
| PROCESS_CREATE_PROCESS
| PROCESS_CREATE_THREAD
| PROCESS_VM_OPERATION
},
5193 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5194 { GENERIC_ALL
, PROCESS_ALL_ACCESS_NT4
}
5197 memset(&sti
, 0, sizeof(sti
));
5198 sti
.cb
= sizeof(sti
);
5199 SetLastError(0xdeadbeef);
5200 ret
= CreateProcessA(NULL
, cmdline
, NULL
, NULL
, FALSE
, CREATE_SUSPENDED
, NULL
, NULL
, &sti
, &pi
);
5201 ok(ret
, "CreateProcess() error %d\n", GetLastError());
5203 CloseHandle(pi
.hThread
);
5204 process
= pi
.hProcess
;
5206 access
= get_obj_access(process
);
5207 ok(access
== PROCESS_ALL_ACCESS_NT4
|| access
== PROCESS_ALL_ACCESS_VISTA
, "expected PROCESS_ALL_ACCESS, got %#x\n", access
);
5209 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5211 SetLastError( 0xdeadbeef );
5212 ret
= DuplicateHandle(GetCurrentProcess(), process
, GetCurrentProcess(), &dup
,
5213 map
[i
].generic
, FALSE
, 0);
5214 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5216 access
= get_obj_access(dup
);
5217 switch (map
[i
].generic
)
5220 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
) /* Vista+ */,
5221 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5224 ok(access
== map
[i
].mapped
||
5225 access
== (map
[i
].mapped
| PROCESS_TERMINATE
) /* before Vista */ ||
5226 access
== (map
[i
].mapped
| PROCESS_SET_LIMITED_INFORMATION
) /* win8 */,
5227 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5229 case GENERIC_EXECUTE
:
5230 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
) /* Vista+ */,
5231 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5234 ok(access
== map
[i
].mapped
|| access
== PROCESS_ALL_ACCESS_VISTA
,
5235 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5238 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5245 TerminateProcess(process
, 0);
5246 CloseHandle(process
);
5249 static BOOL
validate_impersonation_token(HANDLE token
, DWORD
*token_type
)
5253 SECURITY_IMPERSONATION_LEVEL sil
;
5257 SetLastError(0xdeadbeef);
5258 ret
= GetTokenInformation(token
, TokenType
, &type
, sizeof(type
), &needed
);
5259 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5260 ok(needed
== sizeof(type
), "GetTokenInformation should return required buffer length\n");
5261 ok(type
== TokenPrimary
|| type
== TokenImpersonation
, "expected TokenPrimary or TokenImpersonation, got %d\n", type
);
5264 if (type
!= TokenImpersonation
) return FALSE
;
5267 SetLastError(0xdeadbeef);
5268 ret
= GetTokenInformation(token
, TokenImpersonationLevel
, &sil
, sizeof(sil
), &needed
);
5269 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5270 ok(needed
== sizeof(sil
), "GetTokenInformation should return required buffer length\n");
5271 ok(sil
== SecurityImpersonation
, "expected SecurityImpersonation, got %d\n", sil
);
5273 needed
= 0xdeadbeef;
5274 SetLastError(0xdeadbeef);
5275 ret
= GetTokenInformation(token
, TokenDefaultDacl
, NULL
, 0, &needed
);
5276 ok(!ret
, "GetTokenInformation should fail\n");
5277 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5278 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5279 ok(needed
> sizeof(TOKEN_DEFAULT_DACL
), "GetTokenInformation returned empty default DACL\n");
5281 needed
= 0xdeadbeef;
5282 SetLastError(0xdeadbeef);
5283 ret
= GetTokenInformation(token
, TokenOwner
, NULL
, 0, &needed
);
5284 ok(!ret
, "GetTokenInformation should fail\n");
5285 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5286 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5287 ok(needed
> sizeof(TOKEN_OWNER
), "GetTokenInformation returned empty token owner\n");
5289 needed
= 0xdeadbeef;
5290 SetLastError(0xdeadbeef);
5291 ret
= GetTokenInformation(token
, TokenPrimaryGroup
, NULL
, 0, &needed
);
5292 ok(!ret
, "GetTokenInformation should fail\n");
5293 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5294 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5295 ok(needed
> sizeof(TOKEN_PRIMARY_GROUP
), "GetTokenInformation returned empty token primary group\n");
5300 static void test_kernel_objects_security(void)
5302 HANDLE token
, process_token
;
5303 DWORD ret
, token_type
;
5305 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
| TOKEN_QUERY
, &process_token
);
5306 ok(ret
, "OpenProcessToken error %d\n", GetLastError());
5308 ret
= validate_impersonation_token(process_token
, &token_type
);
5309 ok(token_type
== TokenPrimary
, "expected TokenPrimary, got %d\n", token_type
);
5310 ok(!ret
, "access token should not be an impersonation token\n");
5312 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
5313 ok(ret
, "DuplicateToken error %d\n", GetLastError());
5315 ret
= validate_impersonation_token(token
, &token_type
);
5316 ok(ret
, "access token should be a valid impersonation token\n");
5317 ok(token_type
== TokenImpersonation
, "expected TokenImpersonation, got %d\n", token_type
);
5319 test_mutex_security(token
);
5320 test_event_security(token
);
5321 test_named_pipe_security(token
);
5322 test_semaphore_security(token
);
5323 test_file_security(token
);
5324 test_filemap_security();
5325 test_thread_security();
5326 test_process_access();
5327 /* FIXME: test other kernel object types */
5329 CloseHandle(process_token
);
5333 static void test_TokenIntegrityLevel(void)
5335 TOKEN_MANDATORY_LABEL
*tml
;
5336 BYTE buffer
[64]; /* using max. 28 byte in win7 x64 */
5340 char *sidname
= NULL
;
5341 static SID medium_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5342 {SECURITY_MANDATORY_HIGH_RID
}};
5343 static SID high_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5344 {SECURITY_MANDATORY_MEDIUM_RID
}};
5346 SetLastError(0xdeadbeef);
5347 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &token
);
5348 ok(res
, "got %d with %d (expected TRUE)\n", res
, GetLastError());
5350 SetLastError(0xdeadbeef);
5351 res
= GetTokenInformation(token
, TokenIntegrityLevel
, buffer
, sizeof(buffer
), &size
);
5353 /* not supported before Vista */
5354 if (!res
&& ((GetLastError() == ERROR_INVALID_PARAMETER
) || GetLastError() == ERROR_INVALID_FUNCTION
))
5356 win_skip("TokenIntegrityLevel not supported\n");
5361 ok(res
, "got %u with %u (expected TRUE)\n", res
, GetLastError());
5368 tml
= (TOKEN_MANDATORY_LABEL
*) buffer
;
5369 ok(tml
->Label
.Attributes
== (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
),
5370 "got 0x%x (expected 0x%x)\n", tml
->Label
.Attributes
, (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
));
5372 SetLastError(0xdeadbeef);
5373 res
= pConvertSidToStringSidA(tml
->Label
.Sid
, &sidname
);
5374 ok(res
, "got %u and %u\n", res
, GetLastError());
5376 ok(EqualSid(tml
->Label
.Sid
, &medium_level
) || EqualSid(tml
->Label
.Sid
, &high_level
),
5377 "got %s (expected 'S-1-16-8192' or 'S-1-16-12288')\n", sidname
);
5383 static void test_default_dacl_owner_sid(void)
5386 BOOL ret
, defaulted
, present
, found
;
5388 SECURITY_DESCRIPTOR
*sd
;
5389 SECURITY_ATTRIBUTES sa
;
5392 ACCESS_ALLOWED_ACE
*ace
;
5394 sd
= HeapAlloc( GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
5395 ret
= InitializeSecurityDescriptor( sd
, SECURITY_DESCRIPTOR_REVISION
);
5396 ok( ret
, "error %u\n", GetLastError() );
5398 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
5399 sa
.lpSecurityDescriptor
= sd
;
5400 sa
.bInheritHandle
= FALSE
;
5401 handle
= CreateEventA( &sa
, TRUE
, TRUE
, "test_event" );
5402 ok( handle
!= NULL
, "error %u\n", GetLastError() );
5405 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, NULL
, 0, &size
);
5406 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "error %u\n", GetLastError() );
5408 sd
= HeapAlloc( GetProcessHeap(), 0, size
);
5409 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, sd
, size
, &size
);
5410 ok( ret
, "error %u\n", GetLastError() );
5412 owner
= (void *)0xdeadbeef;
5414 ret
= GetSecurityDescriptorOwner( sd
, &owner
, &defaulted
);
5415 ok( ret
, "error %u\n", GetLastError() );
5416 ok( owner
!= (void *)0xdeadbeef, "owner not set\n" );
5417 ok( !defaulted
, "owner defaulted\n" );
5419 dacl
= (void *)0xdeadbeef;
5422 ret
= GetSecurityDescriptorDacl( sd
, &present
, &dacl
, &defaulted
);
5423 ok( ret
, "error %u\n", GetLastError() );
5424 ok( present
, "dacl not present\n" );
5425 ok( dacl
!= (void *)0xdeadbeef, "dacl not set\n" );
5426 ok( !defaulted
, "dacl defaulted\n" );
5430 while (pGetAce( dacl
, index
++, (void **)&ace
))
5432 if (EqualSid( &ace
->SidStart
, owner
)) found
= TRUE
;
5434 ok( found
, "owner sid not found in dacl\n" );
5436 HeapFree( GetProcessHeap(), 0, sa
.lpSecurityDescriptor
);
5437 HeapFree( GetProcessHeap(), 0, sd
);
5438 CloseHandle( handle
);
5441 static void test_AdjustTokenPrivileges(void)
5443 TOKEN_PRIVILEGES tp
, prev
;
5449 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &token
))
5452 if (!LookupPrivilegeValueA(NULL
, SE_BACKUP_NAME
, &luid
))
5458 tp
.PrivilegeCount
= 1;
5459 tp
.Privileges
[0].Luid
= luid
;
5460 tp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
5463 ret
= AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), NULL
, &len
);
5464 ok(ret
, "got %d\n", ret
);
5465 ok(len
== 0xdeadbeef, "got length %d\n", len
);
5468 tp
.PrivilegeCount
= 1;
5469 tp
.Privileges
[0].Luid
= luid
;
5470 tp
.Privileges
[0].Attributes
= 0;
5471 AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), &prev
, NULL
);
5476 START_TEST(security
)
5483 test_process_security_child();
5486 test_kernel_objects_security();
5490 test_CreateWellKnownSid();
5491 test_FileSecurity();
5494 test_LookupAccountSid();
5495 test_LookupAccountName();
5496 test_security_descriptor();
5497 test_process_security();
5498 test_impersonation_level();
5499 test_SetEntriesInAclW();
5500 test_SetEntriesInAclA();
5501 test_CreateDirectoryA();
5502 test_GetNamedSecurityInfoA();
5503 test_ConvertStringSecurityDescriptor();
5504 test_ConvertSecurityDescriptorToString();
5505 test_PrivateObjectSecurity();
5507 test_GetSecurityInfo();
5508 test_GetSidSubAuthority();
5509 test_CheckTokenMembership();
5511 test_GetUserNameA();
5512 test_GetUserNameW();
5513 test_CreateRestrictedToken();
5514 test_TokenIntegrityLevel();
5515 test_default_dacl_owner_sid();
5516 test_AdjustTokenPrivileges();