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
36 #include "wine/test.h"
38 #ifndef PROCESS_QUERY_LIMITED_INFORMATION
39 #define PROCESS_QUERY_LIMITED_INFORMATION 0x1000
42 /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
43 #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
44 #define PROCESS_ALL_ACCESS_VISTA (PROCESS_ALL_ACCESS | 0xf000)
46 #ifndef EVENT_QUERY_STATE
47 #define EVENT_QUERY_STATE 0x0001
50 #ifndef SEMAPHORE_QUERY_STATE
51 #define SEMAPHORE_QUERY_STATE 0x0001
54 #ifndef THREAD_SET_LIMITED_INFORMATION
55 #define THREAD_SET_LIMITED_INFORMATION 0x0400
56 #define THREAD_QUERY_LIMITED_INFORMATION 0x0800
59 #define THREAD_ALL_ACCESS_NT4 (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3ff)
60 #define THREAD_ALL_ACCESS_VISTA (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xffff)
62 /* copied from Wine winternl.h - not included in the Windows SDK */
63 typedef enum _OBJECT_INFORMATION_CLASS
{
64 ObjectBasicInformation
,
65 ObjectNameInformation
,
66 ObjectTypeInformation
,
69 } OBJECT_INFORMATION_CLASS
, *POBJECT_INFORMATION_CLASS
;
71 typedef struct _OBJECT_BASIC_INFORMATION
{
73 ACCESS_MASK GrantedAccess
;
77 ULONG NonPagedPoolUsage
;
79 ULONG NameInformationLength
;
80 ULONG TypeInformationLength
;
81 ULONG SecurityDescriptorLength
;
82 LARGE_INTEGER CreateTime
;
83 } OBJECT_BASIC_INFORMATION
, *POBJECT_BASIC_INFORMATION
;
85 #define expect_eq(expr, value, type, format) { type ret_ = expr; ok((value) == ret_, #expr " expected " format " got " format "\n", (value), (ret_)); }
87 static BOOL (WINAPI
*pAddAccessAllowedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
88 static BOOL (WINAPI
*pAddAccessDeniedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
89 static BOOL (WINAPI
*pAddAuditAccessAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
, BOOL
, BOOL
);
90 static VOID (WINAPI
*pBuildTrusteeWithSidA
)( PTRUSTEEA pTrustee
, PSID pSid
);
91 static VOID (WINAPI
*pBuildTrusteeWithNameA
)( PTRUSTEEA pTrustee
, LPSTR pName
);
92 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndNameA
)( PTRUSTEEA pTrustee
,
93 POBJECTS_AND_NAME_A pObjName
,
94 SE_OBJECT_TYPE ObjectType
,
96 LPSTR InheritedObjectTypeName
,
98 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndSidA
)( PTRUSTEEA pTrustee
,
99 POBJECTS_AND_SID pObjSid
,
101 GUID
* pInheritedObjectGuid
,
103 static LPSTR (WINAPI
*pGetTrusteeNameA
)( PTRUSTEEA pTrustee
);
104 static BOOL (WINAPI
*pMakeSelfRelativeSD
)( PSECURITY_DESCRIPTOR
, PSECURITY_DESCRIPTOR
, LPDWORD
);
105 static BOOL (WINAPI
*pConvertSidToStringSidA
)( PSID pSid
, LPSTR
*str
);
106 static BOOL (WINAPI
*pConvertStringSidToSidA
)( LPCSTR str
, PSID pSid
);
107 static BOOL (WINAPI
*pCheckTokenMembership
)(HANDLE
, PSID
, PBOOL
);
108 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorA
)(LPCSTR
, DWORD
,
109 PSECURITY_DESCRIPTOR
*, PULONG
);
110 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorW
)(LPCWSTR
, DWORD
,
111 PSECURITY_DESCRIPTOR
*, PULONG
);
112 static BOOL (WINAPI
*pConvertSecurityDescriptorToStringSecurityDescriptorA
)(PSECURITY_DESCRIPTOR
, DWORD
,
113 SECURITY_INFORMATION
, LPSTR
*, PULONG
);
114 static BOOL (WINAPI
*pGetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
115 PSECURITY_DESCRIPTOR
, DWORD
, LPDWORD
);
116 static BOOL (WINAPI
*pSetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
117 PSECURITY_DESCRIPTOR
);
118 static DWORD (WINAPI
*pGetNamedSecurityInfoA
)(LPSTR
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
119 PSID
*, PSID
*, PACL
*, PACL
*,
120 PSECURITY_DESCRIPTOR
*);
121 static DWORD (WINAPI
*pSetNamedSecurityInfoA
)(LPSTR
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
122 PSID
, PSID
, PACL
, PACL
);
123 static PDWORD (WINAPI
*pGetSidSubAuthority
)(PSID
, DWORD
);
124 static PUCHAR (WINAPI
*pGetSidSubAuthorityCount
)(PSID
);
125 static BOOL (WINAPI
*pIsValidSid
)(PSID
);
126 static DWORD (WINAPI
*pRtlAdjustPrivilege
)(ULONG
,BOOLEAN
,BOOLEAN
,PBOOLEAN
);
127 static BOOL (WINAPI
*pCreateWellKnownSid
)(WELL_KNOWN_SID_TYPE
,PSID
,PSID
,DWORD
*);
128 static BOOL (WINAPI
*pDuplicateTokenEx
)(HANDLE
,DWORD
,LPSECURITY_ATTRIBUTES
,
129 SECURITY_IMPERSONATION_LEVEL
,TOKEN_TYPE
,PHANDLE
);
131 static NTSTATUS (WINAPI
*pLsaQueryInformationPolicy
)(LSA_HANDLE
,POLICY_INFORMATION_CLASS
,PVOID
*);
132 static NTSTATUS (WINAPI
*pLsaClose
)(LSA_HANDLE
);
133 static NTSTATUS (WINAPI
*pLsaFreeMemory
)(PVOID
);
134 static NTSTATUS (WINAPI
*pLsaOpenPolicy
)(PLSA_UNICODE_STRING
,PLSA_OBJECT_ATTRIBUTES
,ACCESS_MASK
,PLSA_HANDLE
);
135 static NTSTATUS (WINAPI
*pNtQueryObject
)(HANDLE
,OBJECT_INFORMATION_CLASS
,PVOID
,ULONG
,PULONG
);
136 static DWORD (WINAPI
*pSetEntriesInAclW
)(ULONG
, PEXPLICIT_ACCESSW
, PACL
, PACL
*);
137 static DWORD (WINAPI
*pSetEntriesInAclA
)(ULONG
, PEXPLICIT_ACCESSA
, PACL
, PACL
*);
138 static BOOL (WINAPI
*pSetSecurityDescriptorControl
)(PSECURITY_DESCRIPTOR
, SECURITY_DESCRIPTOR_CONTROL
,
139 SECURITY_DESCRIPTOR_CONTROL
);
140 static DWORD (WINAPI
*pGetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
141 PSID
*, PSID
*, PACL
*, PACL
*, PSECURITY_DESCRIPTOR
*);
142 static DWORD (WINAPI
*pSetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
143 PSID
, PSID
, PACL
, PACL
);
144 static NTSTATUS (WINAPI
*pNtAccessCheck
)(PSECURITY_DESCRIPTOR
, HANDLE
, ACCESS_MASK
, PGENERIC_MAPPING
,
145 PPRIVILEGE_SET
, PULONG
, PULONG
, NTSTATUS
*);
146 static BOOL (WINAPI
*pCreateRestrictedToken
)(HANDLE
, DWORD
, DWORD
, PSID_AND_ATTRIBUTES
, DWORD
,
147 PLUID_AND_ATTRIBUTES
, DWORD
, PSID_AND_ATTRIBUTES
, PHANDLE
);
148 static BOOL (WINAPI
*pGetAclInformation
)(PACL
,LPVOID
,DWORD
,ACL_INFORMATION_CLASS
);
149 static BOOL (WINAPI
*pGetAce
)(PACL
,DWORD
,LPVOID
*);
153 static char** myARGV
;
165 SID_IDENTIFIER_AUTHORITY auth
;
169 static void init(void)
173 hntdll
= GetModuleHandleA("ntdll.dll");
174 pNtQueryObject
= (void *)GetProcAddress( hntdll
, "NtQueryObject" );
175 pNtAccessCheck
= (void *)GetProcAddress( hntdll
, "NtAccessCheck" );
177 hmod
= GetModuleHandleA("advapi32.dll");
178 pAddAccessAllowedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessAllowedAceEx");
179 pAddAccessDeniedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessDeniedAceEx");
180 pAddAuditAccessAceEx
= (void *)GetProcAddress(hmod
, "AddAuditAccessAceEx");
181 pCheckTokenMembership
= (void *)GetProcAddress(hmod
, "CheckTokenMembership");
182 pConvertStringSecurityDescriptorToSecurityDescriptorA
=
183 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
184 pConvertStringSecurityDescriptorToSecurityDescriptorW
=
185 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
186 pConvertSecurityDescriptorToStringSecurityDescriptorA
=
187 (void *)GetProcAddress(hmod
, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
188 pGetFileSecurityA
= (void *)GetProcAddress(hmod
, "GetFileSecurityA" );
189 pSetFileSecurityA
= (void *)GetProcAddress(hmod
, "SetFileSecurityA" );
190 pCreateWellKnownSid
= (void *)GetProcAddress( hmod
, "CreateWellKnownSid" );
191 pGetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "GetNamedSecurityInfoA");
192 pSetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "SetNamedSecurityInfoA");
193 pGetSidSubAuthority
= (void *)GetProcAddress(hmod
, "GetSidSubAuthority");
194 pGetSidSubAuthorityCount
= (void *)GetProcAddress(hmod
, "GetSidSubAuthorityCount");
195 pIsValidSid
= (void *)GetProcAddress(hmod
, "IsValidSid");
196 pMakeSelfRelativeSD
= (void *)GetProcAddress(hmod
, "MakeSelfRelativeSD");
197 pSetEntriesInAclW
= (void *)GetProcAddress(hmod
, "SetEntriesInAclW");
198 pSetEntriesInAclA
= (void *)GetProcAddress(hmod
, "SetEntriesInAclA");
199 pSetSecurityDescriptorControl
= (void *)GetProcAddress(hmod
, "SetSecurityDescriptorControl");
200 pGetSecurityInfo
= (void *)GetProcAddress(hmod
, "GetSecurityInfo");
201 pSetSecurityInfo
= (void *)GetProcAddress(hmod
, "SetSecurityInfo");
202 pCreateRestrictedToken
= (void *)GetProcAddress(hmod
, "CreateRestrictedToken");
203 pConvertSidToStringSidA
= (void *)GetProcAddress(hmod
, "ConvertSidToStringSidA");
204 pConvertStringSidToSidA
= (void *)GetProcAddress(hmod
, "ConvertStringSidToSidA");
205 pGetAclInformation
= (void *)GetProcAddress(hmod
, "GetAclInformation");
206 pGetAce
= (void *)GetProcAddress(hmod
, "GetAce");
208 myARGC
= winetest_get_mainargs( &myARGV
);
211 static void test_sid(void)
213 struct sidRef refs
[] = {
214 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
215 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
216 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
217 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
218 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
219 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
221 struct strsid_entry strsid_table
[] = {
222 {"AO", STRSID_OK
}, {"RU", STRSID_OK
}, {"AN", STRSID_OK
}, {"AU", STRSID_OK
},
223 {"BA", STRSID_OK
}, {"BG", STRSID_OK
}, {"BO", STRSID_OK
}, {"BU", STRSID_OK
},
224 {"CA", STRSID_OPT
}, {"CG", STRSID_OK
}, {"CO", STRSID_OK
}, {"DA", STRSID_OPT
},
225 {"DC", STRSID_OPT
}, {"DD", STRSID_OPT
}, {"DG", STRSID_OPT
}, {"DU", STRSID_OPT
},
226 {"EA", STRSID_OPT
}, {"ED", STRSID_OK
}, {"WD", STRSID_OK
}, {"PA", STRSID_OPT
},
227 {"IU", STRSID_OK
}, {"LA", STRSID_OK
}, {"LG", STRSID_OK
}, {"LS", STRSID_OK
},
228 {"SY", STRSID_OK
}, {"NU", STRSID_OK
}, {"NO", STRSID_OK
}, {"NS", STRSID_OK
},
229 {"PO", STRSID_OK
}, {"PS", STRSID_OK
}, {"PU", STRSID_OK
}, {"RS", STRSID_OPT
},
230 {"RD", STRSID_OK
}, {"RE", STRSID_OK
}, {"RC", STRSID_OK
}, {"SA", STRSID_OPT
},
231 {"SO", STRSID_OK
}, {"SU", STRSID_OK
}};
233 const char noSubAuthStr
[] = "S-1-5";
240 if( !pConvertSidToStringSidA
|| !pConvertStringSidToSidA
)
242 win_skip("ConvertSidToStringSidA or ConvertStringSidToSidA not available\n");
246 r
= pConvertStringSidToSidA( NULL
, NULL
);
247 ok( !r
, "expected failure with NULL parameters\n" );
248 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
250 ok( GetLastError() == ERROR_INVALID_PARAMETER
,
251 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
254 r
= pConvertStringSidToSidA( refs
[0].refStr
, NULL
);
255 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
256 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
259 r
= pConvertStringSidToSidA( NULL
, &str
);
260 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
261 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
264 r
= pConvertStringSidToSidA( noSubAuthStr
, &psid
);
266 "expected failure with no sub authorities\n" );
267 ok( GetLastError() == ERROR_INVALID_SID
,
268 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
271 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
), "ConvertStringSidToSidA failed\n");
273 ok(pisid
->SubAuthorityCount
== 4, "Invalid sub authority count - expected 4, got %d\n", pisid
->SubAuthorityCount
);
274 ok(pisid
->SubAuthority
[0] == 21, "Invalid subauthority 0 - expected 21, got %d\n", pisid
->SubAuthority
[0]);
275 ok(pisid
->SubAuthority
[3] == 4576, "Invalid subauthority 0 - expected 4576, got %d\n", pisid
->SubAuthority
[3]);
279 for( i
= 0; i
< sizeof(refs
) / sizeof(refs
[0]); i
++ )
281 r
= AllocateAndInitializeSid( &refs
[i
].auth
, 1,1,0,0,0,0,0,0,0,
283 ok( r
, "failed to allocate sid\n" );
284 r
= pConvertSidToStringSidA( psid
, &str
);
285 ok( r
, "failed to convert sid\n" );
288 ok( !strcmp( str
, refs
[i
].refStr
),
289 "incorrect sid, expected %s, got %s\n", refs
[i
].refStr
, str
);
295 r
= pConvertStringSidToSidA( refs
[i
].refStr
, &psid
);
296 ok( r
, "failed to parse sid string\n" );
299 !memcmp( pisid
->IdentifierAuthority
.Value
, refs
[i
].auth
.Value
,
300 sizeof(refs
[i
].auth
) ),
301 "string sid %s didn't parse to expected value\n"
302 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
304 MAKEWORD( pisid
->IdentifierAuthority
.Value
[1],
305 pisid
->IdentifierAuthority
.Value
[0] ),
306 MAKELONG( MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
307 pisid
->IdentifierAuthority
.Value
[4] ),
308 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
309 pisid
->IdentifierAuthority
.Value
[2] ) ),
310 MAKEWORD( refs
[i
].auth
.Value
[1], refs
[i
].auth
.Value
[0] ),
311 MAKELONG( MAKEWORD( refs
[i
].auth
.Value
[5], refs
[i
].auth
.Value
[4] ),
312 MAKEWORD( refs
[i
].auth
.Value
[3], refs
[i
].auth
.Value
[2] ) ) );
317 /* string constant format not supported before XP */
318 r
= pConvertStringSidToSidA(strsid_table
[0].str
, &psid
);
321 win_skip("String constant format not supported\n");
326 for(i
= 0; i
< sizeof(strsid_table
) / sizeof(strsid_table
[0]); i
++)
330 SetLastError(0xdeadbeef);
331 r
= pConvertStringSidToSidA(strsid_table
[i
].str
, &psid
);
333 if (!(strsid_table
[i
].flags
& STRSID_OPT
))
335 ok(r
, "%s: got %u\n", strsid_table
[i
].str
, GetLastError());
340 if ((winetest_debug
> 1) && (pConvertSidToStringSidA(psid
, &temp
)))
342 trace(" %s: %s\n", strsid_table
[i
].str
, temp
);
349 if (GetLastError() != ERROR_INVALID_SID
)
350 trace(" %s: couldn't be converted, returned %d\n", strsid_table
[i
].str
, GetLastError());
352 trace(" %s: couldn't be converted\n", strsid_table
[i
].str
);
357 static void test_trustee(void)
359 GUID ObjectType
= {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
360 GUID InheritedObjectType
= {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
362 OBJECTS_AND_NAME_A oan
;
366 char szObjectTypeName
[] = "ObjectTypeName";
367 char szInheritedObjectTypeName
[] = "InheritedObjectTypeName";
368 char szTrusteeName
[] = "szTrusteeName";
369 SID_IDENTIFIER_AUTHORITY auth
= { {0x11,0x22,0,0,0, 0} };
371 memset( &ZeroGuid
, 0x00, sizeof (ZeroGuid
) );
373 pBuildTrusteeWithSidA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithSidA" );
374 pBuildTrusteeWithNameA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithNameA" );
375 pBuildTrusteeWithObjectsAndNameA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndNameA" );
376 pBuildTrusteeWithObjectsAndSidA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndSidA" );
377 pGetTrusteeNameA
= (void *)GetProcAddress (hmod
, "GetTrusteeNameA" );
378 if( !pBuildTrusteeWithSidA
|| !pBuildTrusteeWithNameA
||
379 !pBuildTrusteeWithObjectsAndNameA
|| !pBuildTrusteeWithObjectsAndSidA
||
383 if ( ! AllocateAndInitializeSid( &auth
, 1, 42, 0,0,0,0,0,0,0,&psid
) )
385 trace( "failed to init SID\n" );
389 /* test BuildTrusteeWithSidA */
390 memset( &trustee
, 0xff, sizeof trustee
);
391 pBuildTrusteeWithSidA( &trustee
, psid
);
393 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
394 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
395 "MultipleTrusteeOperation wrong\n");
396 ok( trustee
.TrusteeForm
== TRUSTEE_IS_SID
, "TrusteeForm wrong\n");
397 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
398 ok( trustee
.ptstrName
== psid
, "ptstrName wrong\n" );
400 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
401 memset( &trustee
, 0xff, sizeof trustee
);
402 memset( &oas
, 0xff, sizeof(oas
) );
403 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, &ObjectType
,
404 &InheritedObjectType
, psid
);
406 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
407 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
408 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
409 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
410 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
412 ok(oas
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
413 ok(!memcmp(&oas
.ObjectTypeGuid
, &ObjectType
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
414 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
415 ok(oas
.pSid
== psid
, "pSid wrong\n");
417 /* test GetTrusteeNameA */
418 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oas
, "GetTrusteeName returned wrong value\n");
420 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
421 memset( &trustee
, 0xff, sizeof trustee
);
422 memset( &oas
, 0xff, sizeof(oas
) );
423 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, NULL
,
424 &InheritedObjectType
, psid
);
426 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
427 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
428 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
429 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
430 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
432 ok(oas
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
433 ok(!memcmp(&oas
.ObjectTypeGuid
, &ZeroGuid
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
434 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
435 ok(oas
.pSid
== psid
, "pSid wrong\n");
439 /* test BuildTrusteeWithNameA */
440 memset( &trustee
, 0xff, sizeof trustee
);
441 pBuildTrusteeWithNameA( &trustee
, szTrusteeName
);
443 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
444 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
445 "MultipleTrusteeOperation wrong\n");
446 ok( trustee
.TrusteeForm
== TRUSTEE_IS_NAME
, "TrusteeForm wrong\n");
447 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
448 ok( trustee
.ptstrName
== szTrusteeName
, "ptstrName wrong\n" );
450 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
451 memset( &trustee
, 0xff, sizeof trustee
);
452 memset( &oan
, 0xff, sizeof(oan
) );
453 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
454 szInheritedObjectTypeName
, szTrusteeName
);
456 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
457 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
458 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
459 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
460 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
462 ok(oan
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
463 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
464 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
465 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
467 /* test GetTrusteeNameA */
468 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oan
, "GetTrusteeName returned wrong value\n");
470 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
471 memset( &trustee
, 0xff, sizeof trustee
);
472 memset( &oan
, 0xff, sizeof(oan
) );
473 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, NULL
,
474 szInheritedObjectTypeName
, szTrusteeName
);
476 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
477 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
478 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
479 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
480 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
482 ok(oan
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
483 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
484 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
485 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
487 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
488 memset( &trustee
, 0xff, sizeof trustee
);
489 memset( &oan
, 0xff, sizeof(oan
) );
490 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
491 NULL
, szTrusteeName
);
493 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
494 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
495 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
496 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
497 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
499 ok(oan
.ObjectsPresent
== ACE_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
500 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
501 ok(oan
.InheritedObjectTypeName
== NULL
, "InheritedObjectTypeName wrong\n");
502 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
505 /* If the first isn't defined, assume none is */
506 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
507 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
508 #define SE_CREATE_TOKEN_PRIVILEGE 2L
509 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
510 #define SE_LOCK_MEMORY_PRIVILEGE 4L
511 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
512 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
513 #define SE_TCB_PRIVILEGE 7L
514 #define SE_SECURITY_PRIVILEGE 8L
515 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
516 #define SE_LOAD_DRIVER_PRIVILEGE 10L
517 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
518 #define SE_SYSTEMTIME_PRIVILEGE 12L
519 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
520 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
521 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
522 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
523 #define SE_BACKUP_PRIVILEGE 17L
524 #define SE_RESTORE_PRIVILEGE 18L
525 #define SE_SHUTDOWN_PRIVILEGE 19L
526 #define SE_DEBUG_PRIVILEGE 20L
527 #define SE_AUDIT_PRIVILEGE 21L
528 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
529 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
530 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
531 #define SE_UNDOCK_PRIVILEGE 25L
532 #define SE_SYNC_AGENT_PRIVILEGE 26L
533 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
534 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
535 #define SE_IMPERSONATE_PRIVILEGE 29L
536 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
537 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
538 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
540 static void test_allocateLuid(void)
542 BOOL (WINAPI
*pAllocateLocallyUniqueId
)(PLUID
);
546 pAllocateLocallyUniqueId
= (void*)GetProcAddress(hmod
, "AllocateLocallyUniqueId");
547 if (!pAllocateLocallyUniqueId
) return;
549 ret
= pAllocateLocallyUniqueId(&luid1
);
550 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
554 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
555 ret
= pAllocateLocallyUniqueId(&luid2
);
557 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
558 ok(luid1
.LowPart
> SE_MAX_WELL_KNOWN_PRIVILEGE
|| luid1
.HighPart
!= 0,
559 "AllocateLocallyUniqueId returned a well-known LUID\n");
560 ok(luid1
.LowPart
!= luid2
.LowPart
|| luid1
.HighPart
!= luid2
.HighPart
,
561 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
562 ret
= pAllocateLocallyUniqueId(NULL
);
563 ok( !ret
&& GetLastError() == ERROR_NOACCESS
,
564 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
568 static void test_lookupPrivilegeName(void)
570 BOOL (WINAPI
*pLookupPrivilegeNameA
)(LPCSTR
, PLUID
, LPSTR
, LPDWORD
);
571 char buf
[MAX_PATH
]; /* arbitrary, seems long enough */
572 DWORD cchName
= sizeof(buf
);
573 LUID luid
= { 0, 0 };
577 /* check whether it's available first */
578 pLookupPrivilegeNameA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeNameA");
579 if (!pLookupPrivilegeNameA
) return;
580 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
581 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
582 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
585 /* check with a short buffer */
587 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
588 ret
= pLookupPrivilegeNameA(NULL
, &luid
, NULL
, &cchName
);
589 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
590 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
592 ok(cchName
== strlen("SeCreateTokenPrivilege") + 1,
593 "LookupPrivilegeNameA returned an incorrect required length for\n"
594 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
595 lstrlenA("SeCreateTokenPrivilege") + 1);
596 /* check a known value and its returned length on success */
597 cchName
= sizeof(buf
);
598 ok(pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
) &&
599 cchName
== strlen("SeCreateTokenPrivilege"),
600 "LookupPrivilegeNameA returned an incorrect output length for\n"
601 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
602 (int)strlen("SeCreateTokenPrivilege"));
603 /* check known values */
604 for (i
= SE_MIN_WELL_KNOWN_PRIVILEGE
; i
<= SE_MAX_WELL_KNOWN_PRIVILEGE
; i
++)
607 cchName
= sizeof(buf
);
608 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
609 ok( ret
|| GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
610 "LookupPrivilegeNameA(0.%d) failed: %d\n", i
, GetLastError());
612 /* check a bogus LUID */
613 luid
.LowPart
= 0xdeadbeef;
614 cchName
= sizeof(buf
);
615 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
616 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
617 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
619 /* check on a bogus system */
620 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
621 cchName
= sizeof(buf
);
622 ret
= pLookupPrivilegeNameA("b0gu5.Nam3", &luid
, buf
, &cchName
);
623 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
624 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
625 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
635 static void test_lookupPrivilegeValue(void)
637 static const struct NameToLUID privs
[] = {
638 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE
},
639 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE
},
640 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE
},
641 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE
},
642 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE
},
643 { "SeTcbPrivilege", SE_TCB_PRIVILEGE
},
644 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE
},
645 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE
},
646 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE
},
647 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE
},
648 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE
},
649 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE
},
650 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE
},
651 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE
},
652 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE
},
653 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE
},
654 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE
},
655 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE
},
656 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE
},
657 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE
},
658 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE
},
659 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE
},
660 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE
},
661 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE
},
662 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE
},
663 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE
},
664 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE
},
665 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE
},
666 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE
},
668 BOOL (WINAPI
*pLookupPrivilegeValueA
)(LPCSTR
, LPCSTR
, PLUID
);
673 /* check whether it's available first */
674 pLookupPrivilegeValueA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeValueA");
675 if (!pLookupPrivilegeValueA
) return;
676 ret
= pLookupPrivilegeValueA(NULL
, "SeCreateTokenPrivilege", &luid
);
677 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
680 /* check a bogus system name */
681 ret
= pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid
);
682 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
683 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
684 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
686 /* check a NULL string */
687 ret
= pLookupPrivilegeValueA(NULL
, 0, &luid
);
688 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
689 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
691 /* check a bogus privilege name */
692 ret
= pLookupPrivilegeValueA(NULL
, "SeBogusPrivilege", &luid
);
693 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
694 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
696 /* check case insensitive */
697 ret
= pLookupPrivilegeValueA(NULL
, "sEcREATEtOKENpRIVILEGE", &luid
);
699 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
701 for (i
= 0; i
< sizeof(privs
) / sizeof(privs
[0]); i
++)
703 /* Not all privileges are implemented on all Windows versions, so
704 * don't worry if the call fails
706 if (pLookupPrivilegeValueA(NULL
, privs
[i
].name
, &luid
))
708 ok(luid
.LowPart
== privs
[i
].lowPart
,
709 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
715 static void test_luid(void)
718 test_lookupPrivilegeName();
719 test_lookupPrivilegeValue();
722 static void test_FileSecurity(void)
724 char wintmpdir
[MAX_PATH
];
725 char path
[MAX_PATH
];
726 char file
[MAX_PATH
];
728 DWORD sdSize
, retSize
, rc
, granted
, priv_set_len
;
729 PRIVILEGE_SET priv_set
;
732 GENERIC_MAPPING mapping
= { FILE_READ_DATA
, FILE_WRITE_DATA
, FILE_EXECUTE
, FILE_ALL_ACCESS
};
733 const SECURITY_INFORMATION request
= OWNER_SECURITY_INFORMATION
734 | GROUP_SECURITY_INFORMATION
735 | DACL_SECURITY_INFORMATION
;
737 if (!pGetFileSecurityA
) {
738 win_skip ("GetFileSecurity is not available\n");
742 if (!pSetFileSecurityA
) {
743 win_skip ("SetFileSecurity is not available\n");
747 if (!GetTempPathA (sizeof (wintmpdir
), wintmpdir
)) {
748 win_skip ("GetTempPathA failed\n");
752 /* Create a temporary directory and in it a temporary file */
753 strcat (strcpy (path
, wintmpdir
), "rary");
754 SetLastError(0xdeadbeef);
755 rc
= CreateDirectoryA (path
, NULL
);
756 ok (rc
|| GetLastError() == ERROR_ALREADY_EXISTS
, "CreateDirectoryA "
757 "failed for '%s' with %d\n", path
, GetLastError());
759 strcat (strcpy (file
, path
), "\\ess");
760 SetLastError(0xdeadbeef);
761 fh
= CreateFileA (file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
762 ok (fh
!= INVALID_HANDLE_VALUE
, "CreateFileA "
763 "failed for '%s' with %d\n", file
, GetLastError());
766 /* For the temporary file ... */
768 /* Get size needed */
770 SetLastError(0xdeadbeef);
771 rc
= pGetFileSecurityA (file
, request
, NULL
, 0, &retSize
);
772 if (!rc
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) {
773 win_skip("GetFileSecurityA is not implemented\n");
776 ok (!rc
, "GetFileSecurityA "
777 "was expected to fail for '%s'\n", file
);
778 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
779 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
780 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
783 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
785 /* Get security descriptor for real */
787 SetLastError(0xdeadbeef);
788 rc
= pGetFileSecurityA (file
, request
, sd
, sdSize
, &retSize
);
789 ok (rc
, "GetFileSecurityA "
790 "was not expected to fail '%s': %d\n", file
, GetLastError());
791 ok (retSize
== sdSize
||
792 broken(retSize
== 0), /* NT4 */
793 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
795 /* Use it to set security descriptor */
796 SetLastError(0xdeadbeef);
797 rc
= pSetFileSecurityA (file
, request
, sd
);
798 ok (rc
, "SetFileSecurityA "
799 "was not expected to fail '%s': %d\n", file
, GetLastError());
801 HeapFree (GetProcessHeap (), 0, sd
);
803 /* Repeat for the temporary directory ... */
805 /* Get size needed */
807 SetLastError(0xdeadbeef);
808 rc
= pGetFileSecurityA (path
, request
, NULL
, 0, &retSize
);
809 ok (!rc
, "GetFileSecurityA "
810 "was expected to fail for '%s'\n", path
);
811 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
812 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
813 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
816 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
818 /* Get security descriptor for real */
820 SetLastError(0xdeadbeef);
821 rc
= pGetFileSecurityA (path
, request
, sd
, sdSize
, &retSize
);
822 ok (rc
, "GetFileSecurityA "
823 "was not expected to fail '%s': %d\n", path
, GetLastError());
824 ok (retSize
== sdSize
||
825 broken(retSize
== 0), /* NT4 */
826 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
828 /* Use it to set security descriptor */
829 SetLastError(0xdeadbeef);
830 rc
= pSetFileSecurityA (path
, request
, sd
);
831 ok (rc
, "SetFileSecurityA "
832 "was not expected to fail '%s': %d\n", path
, GetLastError());
833 HeapFree (GetProcessHeap (), 0, sd
);
836 strcpy (wintmpdir
, "\\Should not exist");
837 SetLastError(0xdeadbeef);
838 rc
= pGetFileSecurityA (wintmpdir
, OWNER_SECURITY_INFORMATION
, NULL
, 0, &sdSize
);
839 ok (!rc
, "GetFileSecurityA should fail for not existing directories/files\n");
840 ok (GetLastError() == ERROR_FILE_NOT_FOUND
,
841 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
844 /* Remove temporary file and directory */
846 RemoveDirectoryA(path
);
848 /* Test file access permissions for a file with FILE_ATTRIBUTE_ARCHIVE */
849 SetLastError(0xdeadbeef);
850 rc
= GetTempPathA(sizeof(wintmpdir
), wintmpdir
);
851 ok(rc
, "GetTempPath error %d\n", GetLastError());
853 SetLastError(0xdeadbeef);
854 rc
= GetTempFileNameA(wintmpdir
, "tmp", 0, file
);
855 ok(rc
, "GetTempFileName error %d\n", GetLastError());
857 rc
= GetFileAttributesA(file
);
858 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
859 ok(rc
== FILE_ATTRIBUTE_ARCHIVE
, "expected FILE_ATTRIBUTE_ARCHIVE got %#x\n", rc
);
861 retSize
= 0xdeadbeef;
862 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
864 ok(!rc
, "GetFileSecurity should fail\n");
865 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
866 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
867 ok(sdSize
> sizeof(SECURITY_DESCRIPTOR
), "got sd size %d\n", sdSize
);
869 sd
= HeapAlloc(GetProcessHeap (), 0, sdSize
);
870 retSize
= 0xdeadbeef;
871 SetLastError(0xdeadbeef);
872 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
873 sd
, sdSize
, &retSize
);
874 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
875 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
877 SetLastError(0xdeadbeef);
878 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
879 ok(!rc
, "OpenThreadToken should fail\n");
880 ok(GetLastError() == ERROR_NO_TOKEN
, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
882 SetLastError(0xdeadbeef);
883 rc
= ImpersonateSelf(SecurityIdentification
);
884 ok(rc
, "ImpersonateSelf error %d\n", GetLastError());
886 SetLastError(0xdeadbeef);
887 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
888 ok(rc
, "OpenThreadToken error %d\n", GetLastError());
890 SetLastError(0xdeadbeef);
892 ok(rc
, "RevertToSelf error %d\n", GetLastError());
894 priv_set_len
= sizeof(priv_set
);
895 granted
= 0xdeadbeef;
897 SetLastError(0xdeadbeef);
898 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
899 ok(rc
, "AccessCheck error %d\n", GetLastError());
900 ok(status
== 1, "expected 1, got %d\n", status
);
901 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
903 granted
= 0xdeadbeef;
905 SetLastError(0xdeadbeef);
906 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
907 ok(rc
, "AccessCheck error %d\n", GetLastError());
908 ok(status
== 1, "expected 1, got %d\n", status
);
909 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
911 granted
= 0xdeadbeef;
913 SetLastError(0xdeadbeef);
914 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
915 ok(rc
, "AccessCheck error %d\n", GetLastError());
916 ok(status
== 1, "expected 1, got %d\n", status
);
917 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
919 granted
= 0xdeadbeef;
921 SetLastError(0xdeadbeef);
922 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
923 ok(rc
, "AccessCheck error %d\n", GetLastError());
924 ok(status
== 1, "expected 1, got %d\n", status
);
925 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
927 granted
= 0xdeadbeef;
929 SetLastError(0xdeadbeef);
930 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
931 ok(rc
, "AccessCheck error %d\n", GetLastError());
932 ok(status
== 1, "expected 1, got %d\n", status
);
933 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
935 granted
= 0xdeadbeef;
937 SetLastError(0xdeadbeef);
938 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
939 ok(rc
, "AccessCheck error %d\n", GetLastError());
940 ok(status
== 1, "expected 1, got %d\n", status
);
941 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
943 granted
= 0xdeadbeef;
945 SetLastError(0xdeadbeef);
946 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
947 ok(rc
, "AccessCheck error %d\n", GetLastError());
948 ok(status
== 1, "expected 1, got %d\n", status
);
949 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
951 SetLastError(0xdeadbeef);
952 rc
= AccessCheck(sd
, token
, 0xffffffff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
953 ok(!rc
, "AccessCheck should fail\n");
954 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED
, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
956 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
957 SetLastError(0xdeadbeef);
958 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
959 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
960 retSize
= 0xdeadbeef;
961 SetLastError(0xdeadbeef);
962 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
963 ok(!rc
, "WriteFile should fail\n");
964 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
965 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
968 rc
= GetFileAttributesA(file
);
969 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
971 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
972 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
974 SetLastError(0xdeadbeef);
975 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
976 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
977 SetLastError(0xdeadbeef);
978 rc
= DeleteFileA(file
);
979 ok(rc
, "DeleteFile error %d\n", GetLastError());
981 SetLastError(0xdeadbeef);
982 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
983 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
984 retSize
= 0xdeadbeef;
985 SetLastError(0xdeadbeef);
986 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
987 ok(!rc
, "WriteFile should fail\n");
988 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
989 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
992 rc
= GetFileAttributesA(file
);
993 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
994 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
995 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
997 retSize
= 0xdeadbeef;
998 SetLastError(0xdeadbeef);
999 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
1000 sd
, sdSize
, &retSize
);
1001 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
1002 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
1004 priv_set_len
= sizeof(priv_set
);
1005 granted
= 0xdeadbeef;
1006 status
= 0xdeadbeef;
1007 SetLastError(0xdeadbeef);
1008 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1009 ok(rc
, "AccessCheck error %d\n", GetLastError());
1010 ok(status
== 1, "expected 1, got %d\n", status
);
1011 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
1013 granted
= 0xdeadbeef;
1014 status
= 0xdeadbeef;
1015 SetLastError(0xdeadbeef);
1016 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1017 ok(rc
, "AccessCheck error %d\n", GetLastError());
1019 ok(status
== 1, "expected 1, got %d\n", status
);
1020 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
1022 granted
= 0xdeadbeef;
1023 status
= 0xdeadbeef;
1024 SetLastError(0xdeadbeef);
1025 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1026 ok(rc
, "AccessCheck error %d\n", GetLastError());
1027 ok(status
== 1, "expected 1, got %d\n", status
);
1028 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
1030 granted
= 0xdeadbeef;
1031 status
= 0xdeadbeef;
1032 SetLastError(0xdeadbeef);
1033 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1034 ok(rc
, "AccessCheck error %d\n", GetLastError());
1036 ok(status
== 1, "expected 1, got %d\n", status
);
1037 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
1039 granted
= 0xdeadbeef;
1040 status
= 0xdeadbeef;
1041 SetLastError(0xdeadbeef);
1042 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1043 ok(rc
, "AccessCheck error %d\n", GetLastError());
1045 ok(status
== 1, "expected 1, got %d\n", status
);
1046 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
1048 granted
= 0xdeadbeef;
1049 status
= 0xdeadbeef;
1050 SetLastError(0xdeadbeef);
1051 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1052 ok(rc
, "AccessCheck error %d\n", GetLastError());
1054 ok(status
== 1, "expected 1, got %d\n", status
);
1055 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
1057 granted
= 0xdeadbeef;
1058 status
= 0xdeadbeef;
1059 SetLastError(0xdeadbeef);
1060 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1061 ok(rc
, "AccessCheck error %d\n", GetLastError());
1063 ok(status
== 1, "expected 1, got %d\n", status
);
1064 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
1066 SetLastError(0xdeadbeef);
1067 rc
= DeleteFileA(file
);
1068 ok(!rc
, "DeleteFile should fail\n");
1069 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1070 SetLastError(0xdeadbeef);
1071 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
1072 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1073 SetLastError(0xdeadbeef);
1074 rc
= DeleteFileA(file
);
1075 ok(rc
, "DeleteFile error %d\n", GetLastError());
1078 HeapFree(GetProcessHeap(), 0, sd
);
1081 static void test_AccessCheck(void)
1083 PSID EveryoneSid
= NULL
, AdminSid
= NULL
, UsersSid
= NULL
;
1085 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
1086 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
1087 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1088 GENERIC_MAPPING Mapping
= { KEY_READ
, KEY_WRITE
, KEY_EXECUTE
, KEY_ALL_ACCESS
};
1092 HANDLE ProcessToken
;
1095 PRIVILEGE_SET
*PrivSet
;
1097 HMODULE NtDllModule
;
1100 NTSTATUS ntret
, ntAccessStatus
;
1102 NtDllModule
= GetModuleHandleA("ntdll.dll");
1105 skip("not running on NT, skipping test\n");
1108 pRtlAdjustPrivilege
= (void *)GetProcAddress(NtDllModule
, "RtlAdjustPrivilege");
1109 if (!pRtlAdjustPrivilege
)
1111 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1115 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
1116 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1117 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1119 skip("ACLs not implemented - skipping tests\n");
1120 HeapFree(GetProcessHeap(), 0, Acl
);
1123 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
1125 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
1126 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1128 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1129 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &AdminSid
);
1130 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1132 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1133 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
1134 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1136 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
1138 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
1139 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1141 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1142 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1144 PrivSetLen
= FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[16]);
1145 PrivSet
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, PrivSetLen
);
1146 PrivSet
->PrivilegeCount
= 16;
1148 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &ProcessToken
);
1149 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
1151 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, FALSE
, TRUE
, &Enabled
);
1153 res
= DuplicateToken(ProcessToken
, SecurityImpersonation
, &Token
);
1154 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1156 /* SD without owner/group */
1157 SetLastError(0xdeadbeef);
1158 Access
= AccessStatus
= 0x1abe11ed;
1159 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_QUERY_VALUE
, &Mapping
,
1160 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1161 err
= GetLastError();
1162 ok(!ret
&& err
== ERROR_INVALID_SECURITY_DESCR
, "AccessCheck should have "
1163 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err
);
1164 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1165 "Access and/or AccessStatus were changed!\n");
1167 /* Set owner and group */
1168 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
1169 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1170 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, TRUE
);
1171 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1173 /* Generic access mask */
1174 SetLastError(0xdeadbeef);
1175 Access
= AccessStatus
= 0x1abe11ed;
1176 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1177 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1178 err
= GetLastError();
1179 ok(!ret
&& err
== ERROR_GENERIC_NOT_MAPPED
, "AccessCheck should have failed "
1180 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err
);
1181 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1182 "Access and/or AccessStatus were changed!\n");
1184 /* Generic access mask - no privilegeset buffer */
1185 SetLastError(0xdeadbeef);
1186 Access
= AccessStatus
= 0x1abe11ed;
1187 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1188 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1189 err
= GetLastError();
1190 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1191 "with ERROR_NOACCESS, instead of %d\n", err
);
1192 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1193 "Access and/or AccessStatus were changed!\n");
1195 /* Generic access mask - no returnlength */
1196 SetLastError(0xdeadbeef);
1197 Access
= AccessStatus
= 0x1abe11ed;
1198 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1199 PrivSet
, NULL
, &Access
, &AccessStatus
);
1200 err
= GetLastError();
1201 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1202 "with ERROR_NOACCESS, instead of %d\n", err
);
1203 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1204 "Access and/or AccessStatus were changed!\n");
1206 /* Generic access mask - no privilegeset buffer, no returnlength */
1207 SetLastError(0xdeadbeef);
1208 Access
= AccessStatus
= 0x1abe11ed;
1209 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1210 NULL
, NULL
, &Access
, &AccessStatus
);
1211 err
= GetLastError();
1212 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1213 "with ERROR_NOACCESS, instead of %d\n", err
);
1214 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1215 "Access and/or AccessStatus were changed!\n");
1217 /* sd with no dacl present */
1218 Access
= AccessStatus
= 0x1abe11ed;
1219 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, FALSE
, NULL
, FALSE
);
1220 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1221 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1222 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1223 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1224 ok(AccessStatus
&& (Access
== KEY_READ
),
1225 "AccessCheck failed to grant access with error %d\n",
1228 /* sd with no dacl present - no privilegeset buffer */
1229 SetLastError(0xdeadbeef);
1230 Access
= AccessStatus
= 0x1abe11ed;
1231 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1232 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1233 err
= GetLastError();
1234 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1235 "with ERROR_NOACCESS, instead of %d\n", err
);
1236 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1237 "Access and/or AccessStatus were changed!\n");
1241 /* Generic access mask - no privilegeset buffer */
1242 SetLastError(0xdeadbeef);
1243 Access
= ntAccessStatus
= 0x1abe11ed;
1244 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1245 NULL
, &PrivSetLen
, &Access
, &ntAccessStatus
);
1246 err
= GetLastError();
1247 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1248 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1249 ok(err
== 0xdeadbeef,
1250 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1251 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1252 "Access and/or AccessStatus were changed!\n");
1254 /* Generic access mask - no returnlength */
1255 SetLastError(0xdeadbeef);
1256 Access
= ntAccessStatus
= 0x1abe11ed;
1257 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1258 PrivSet
, NULL
, &Access
, &ntAccessStatus
);
1259 err
= GetLastError();
1260 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1261 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1262 ok(err
== 0xdeadbeef,
1263 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1264 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1265 "Access and/or AccessStatus were changed!\n");
1267 /* Generic access mask - no privilegeset buffer, no returnlength */
1268 SetLastError(0xdeadbeef);
1269 Access
= ntAccessStatus
= 0x1abe11ed;
1270 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1271 NULL
, NULL
, &Access
, &ntAccessStatus
);
1272 err
= GetLastError();
1273 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1274 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1275 ok(err
== 0xdeadbeef,
1276 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1277 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1278 "Access and/or AccessStatus were changed!\n");
1281 win_skip("NtAccessCheck unavailable. Skipping.\n");
1283 /* sd with NULL dacl */
1284 Access
= AccessStatus
= 0x1abe11ed;
1285 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, NULL
, FALSE
);
1286 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1287 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1288 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1289 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1290 ok(AccessStatus
&& (Access
== KEY_READ
),
1291 "AccessCheck failed to grant access with error %d\n",
1294 /* sd with blank dacl */
1295 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1296 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1297 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1298 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1299 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1300 err
= GetLastError();
1301 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1302 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1303 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1305 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, KEY_READ
, EveryoneSid
);
1306 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1308 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, KEY_SET_VALUE
, AdminSid
);
1309 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1312 Access
= AccessStatus
= 0x1abe11ed;
1313 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1314 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1315 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1316 ok(AccessStatus
&& (Access
== KEY_READ
),
1317 "AccessCheck failed to grant access with error %d\n",
1320 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1321 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1322 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1324 "AccessCheck failed to grant any access with error %d\n",
1326 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access
);
1328 /* Access denied by SD */
1329 SetLastError(0xdeadbeef);
1330 Access
= AccessStatus
= 0x1abe11ed;
1331 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_WRITE
, &Mapping
,
1332 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1333 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1334 err
= GetLastError();
1335 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1336 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1337 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1340 PrivSet
->PrivilegeCount
= 16;
1341 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1342 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1343 ok(ret
&& !AccessStatus
&& GetLastError() == ERROR_PRIVILEGE_NOT_HELD
,
1344 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1347 ret
= ImpersonateLoggedOnUser(Token
);
1348 ok(ret
, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1349 ret
= pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, TRUE
, TRUE
, &Enabled
);
1353 PrivSet
->PrivilegeCount
= 16;
1354 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1355 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1356 ok(ret
&& AccessStatus
&& GetLastError() == 0,
1357 "AccessCheck should have succeeded, error %d\n",
1359 ok(Access
== ACCESS_SYSTEM_SECURITY
,
1360 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1364 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1366 ret
= RevertToSelf();
1367 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1369 /* test INHERIT_ONLY_ACE */
1370 ret
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1371 ok(ret
, "InitializeAcl failed with error %d\n", GetLastError());
1373 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1376 if (pAddAccessAllowedAceEx
)
1378 ret
= pAddAccessAllowedAceEx(Acl
, ACL_REVISION
, INHERIT_ONLY_ACE
, KEY_READ
, EveryoneSid
);
1379 ok(ret
, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1382 win_skip("AddAccessAllowedAceEx is not available\n");
1384 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1385 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1386 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1387 err
= GetLastError();
1388 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1389 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1390 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1394 res
= DuplicateToken(ProcessToken
, SecurityAnonymous
, &Token
);
1395 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1397 SetLastError(0xdeadbeef);
1398 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1399 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1400 err
= GetLastError();
1401 ok(!ret
&& err
== ERROR_BAD_IMPERSONATION_LEVEL
, "AccessCheck should have failed "
1402 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err
);
1406 SetLastError(0xdeadbeef);
1407 ret
= AccessCheck(SecurityDescriptor
, ProcessToken
, KEY_READ
, &Mapping
,
1408 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1409 err
= GetLastError();
1410 ok(!ret
&& err
== ERROR_NO_IMPERSONATION_TOKEN
, "AccessCheck should have failed "
1411 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err
);
1413 CloseHandle(ProcessToken
);
1416 FreeSid(EveryoneSid
);
1421 HeapFree(GetProcessHeap(), 0, Acl
);
1422 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
1423 HeapFree(GetProcessHeap(), 0, PrivSet
);
1426 /* test GetTokenInformation for the various attributes */
1427 static void test_token_attr(void)
1429 HANDLE Token
, ImpersonationToken
;
1431 TOKEN_PRIVILEGES
*Privileges
;
1432 TOKEN_GROUPS
*Groups
;
1434 TOKEN_DEFAULT_DACL
*Dacl
;
1438 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1441 /* cygwin-like use case */
1442 SetLastError(0xdeadbeef);
1443 ret
= OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED
, &Token
);
1444 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1446 win_skip("OpenProcessToken is not implemented\n");
1449 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1452 DWORD buf
[256]; /* GetTokenInformation wants a dword-aligned buffer */
1454 ret
= GetTokenInformation(Token
, TokenUser
,(void*)buf
, Size
, &Size
);
1455 ok(ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1456 Size
= sizeof(ImpersonationLevel
);
1457 ret
= GetTokenInformation(Token
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1458 GLE
= GetLastError();
1459 ok(!ret
&& (GLE
== ERROR_INVALID_PARAMETER
), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE
);
1463 if(!pConvertSidToStringSidA
)
1465 win_skip("ConvertSidToStringSidA is not available\n");
1469 SetLastError(0xdeadbeef);
1470 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS
, &Token
);
1471 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1474 /* insufficient buffer length */
1475 SetLastError(0xdeadbeef);
1477 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size2
);
1478 ok(Size2
> 1, "got %d\n", Size2
);
1479 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1480 "%d with error %d\n", ret
, GetLastError());
1482 Groups
= HeapAlloc(GetProcessHeap(), 0, Size2
);
1483 memset(Groups
, 0xcc, Size2
);
1485 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size2
, &Size
);
1486 ok(Size
> 1, "got %d\n", Size
);
1487 ok((!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) || broken(ret
) /* wow64 */,
1488 "%d with error %d\n", ret
, GetLastError());
1490 ok(*((BYTE
*)Groups
) == 0xcc, "buffer altered\n");
1492 HeapFree(GetProcessHeap(), 0, Groups
);
1494 SetLastError(0xdeadbeef);
1495 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size
);
1496 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1497 "GetTokenInformation(TokenGroups) %s with error %d\n",
1498 ret
? "succeeded" : "failed", GetLastError());
1499 Groups
= HeapAlloc(GetProcessHeap(), 0, Size
);
1500 SetLastError(0xdeadbeef);
1501 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size
, &Size
);
1502 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1503 ok(GetLastError() == 0xdeadbeef,
1504 "GetTokenInformation shouldn't have set last error to %d\n",
1506 trace("TokenGroups:\n");
1507 for (i
= 0; i
< Groups
->GroupCount
; i
++)
1509 DWORD NameLength
= 255;
1511 DWORD DomainLength
= 255;
1513 SID_NAME_USE SidNameUse
;
1516 ret
= LookupAccountSidA(NULL
, Groups
->Groups
[i
].Sid
, Name
, &NameLength
, Domain
, &DomainLength
, &SidNameUse
);
1519 pConvertSidToStringSidA(Groups
->Groups
[i
].Sid
, &SidString
);
1520 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString
, Domain
, Name
, SidNameUse
, Groups
->Groups
[i
].Attributes
);
1521 LocalFree(SidString
);
1523 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups
->Groups
[i
].Attributes
, GetLastError());
1525 HeapFree(GetProcessHeap(), 0, Groups
);
1528 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
1529 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1530 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1531 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
1532 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
1534 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1536 pConvertSidToStringSidA(User
->User
.Sid
, &SidString
);
1537 trace("TokenUser: %s attr: 0x%08x\n", SidString
, User
->User
.Attributes
);
1538 LocalFree(SidString
);
1539 HeapFree(GetProcessHeap(), 0, User
);
1542 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
1543 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1544 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1545 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1546 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1548 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1549 trace("TokenPrivileges:\n");
1550 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1553 DWORD NameLen
= sizeof(Name
)/sizeof(Name
[0]);
1554 LookupPrivilegeNameA(NULL
, &Privileges
->Privileges
[i
].Luid
, Name
, &NameLen
);
1555 trace("\t%s, 0x%x\n", Name
, Privileges
->Privileges
[i
].Attributes
);
1557 HeapFree(GetProcessHeap(), 0, Privileges
);
1559 ret
= DuplicateToken(Token
, SecurityAnonymous
, &ImpersonationToken
);
1560 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1562 Size
= sizeof(ImpersonationLevel
);
1563 ret
= GetTokenInformation(ImpersonationToken
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1564 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1565 ok(ImpersonationLevel
== SecurityAnonymous
, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel
);
1567 CloseHandle(ImpersonationToken
);
1570 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0, &Size
);
1571 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1572 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1574 Dacl
= HeapAlloc(GetProcessHeap(), 0, Size
);
1575 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size
);
1576 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1578 SetLastError(0xdeadbeef);
1579 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0);
1580 GLE
= GetLastError();
1581 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1582 ok(GLE
== ERROR_BAD_LENGTH
, "expected ERROR_BAD_LENGTH got %u\n", GLE
);
1584 SetLastError(0xdeadbeef);
1585 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, Size
);
1586 GLE
= GetLastError();
1587 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1588 ok(GLE
== ERROR_NOACCESS
, "expected ERROR_NOACCESS got %u\n", GLE
);
1590 acl
= Dacl
->DefaultDacl
;
1591 Dacl
->DefaultDacl
= NULL
;
1593 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1594 ok(ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1597 Dacl
->DefaultDacl
= (ACL
*)0xdeadbeef;
1598 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1599 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1600 ok(Dacl
->DefaultDacl
== NULL
, "expected NULL, got %p\n", Dacl
->DefaultDacl
);
1601 ok(Size2
== sizeof(TOKEN_DEFAULT_DACL
) || broken(Size2
== 2*sizeof(TOKEN_DEFAULT_DACL
)), /* WoW64 */
1602 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2
);
1604 Dacl
->DefaultDacl
= acl
;
1605 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1606 ok(ret
, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1608 if (Size2
== sizeof(TOKEN_DEFAULT_DACL
)) {
1609 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1610 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1612 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1614 HeapFree(GetProcessHeap(), 0, Dacl
);
1618 typedef union _MAX_SID
1621 char max
[SECURITY_MAX_SID_SIZE
];
1624 static void test_sid_str(PSID
* sid
)
1627 BOOL ret
= pConvertSidToStringSidA(sid
, &str_sid
);
1628 ok(ret
, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1631 char account
[MAX_PATH
], domain
[MAX_PATH
];
1633 DWORD acc_size
= MAX_PATH
;
1634 DWORD dom_size
= MAX_PATH
;
1635 ret
= LookupAccountSidA (NULL
, sid
, account
, &acc_size
, domain
, &dom_size
, &use
);
1636 ok(ret
|| (!ret
&& (GetLastError() == ERROR_NONE_MAPPED
)),
1637 "LookupAccountSid(%s) failed: %d\n", str_sid
, GetLastError());
1639 trace(" %s %s\\%s %d\n", str_sid
, domain
, account
, use
);
1640 else if (GetLastError() == ERROR_NONE_MAPPED
)
1641 trace(" %s couldn't be mapped\n", str_sid
);
1646 static const struct well_known_sid_value
1648 BOOL without_domain
;
1649 const char *sid_string
;
1650 } well_known_sid_values
[] = {
1651 /* 0 */ {TRUE
, "S-1-0-0"}, {TRUE
, "S-1-1-0"}, {TRUE
, "S-1-2-0"}, {TRUE
, "S-1-3-0"},
1652 /* 4 */ {TRUE
, "S-1-3-1"}, {TRUE
, "S-1-3-2"}, {TRUE
, "S-1-3-3"}, {TRUE
, "S-1-5"},
1653 /* 8 */ {FALSE
, "S-1-5-1"}, {TRUE
, "S-1-5-2"}, {TRUE
, "S-1-5-3"}, {TRUE
, "S-1-5-4"},
1654 /* 12 */ {TRUE
, "S-1-5-6"}, {TRUE
, "S-1-5-7"}, {TRUE
, "S-1-5-8"}, {TRUE
, "S-1-5-9"},
1655 /* 16 */ {TRUE
, "S-1-5-10"}, {TRUE
, "S-1-5-11"}, {TRUE
, "S-1-5-12"}, {TRUE
, "S-1-5-13"},
1656 /* 20 */ {TRUE
, "S-1-5-14"}, {FALSE
, NULL
}, {TRUE
, "S-1-5-18"}, {TRUE
, "S-1-5-19"},
1657 /* 24 */ {TRUE
, "S-1-5-20"}, {TRUE
, "S-1-5-32"},
1658 /* 26 */ {FALSE
, "S-1-5-32-544"}, {TRUE
, "S-1-5-32-545"}, {TRUE
, "S-1-5-32-546"},
1659 /* 29 */ {TRUE
, "S-1-5-32-547"}, {TRUE
, "S-1-5-32-548"}, {TRUE
, "S-1-5-32-549"},
1660 /* 32 */ {TRUE
, "S-1-5-32-550"}, {TRUE
, "S-1-5-32-551"}, {TRUE
, "S-1-5-32-552"},
1661 /* 35 */ {TRUE
, "S-1-5-32-554"}, {TRUE
, "S-1-5-32-555"}, {TRUE
, "S-1-5-32-556"},
1662 /* 38 */ {FALSE
, "S-1-5-21-12-23-34-45-56-500"}, {FALSE
, "S-1-5-21-12-23-34-45-56-501"},
1663 /* 40 */ {FALSE
, "S-1-5-21-12-23-34-45-56-502"}, {FALSE
, "S-1-5-21-12-23-34-45-56-512"},
1664 /* 42 */ {FALSE
, "S-1-5-21-12-23-34-45-56-513"}, {FALSE
, "S-1-5-21-12-23-34-45-56-514"},
1665 /* 44 */ {FALSE
, "S-1-5-21-12-23-34-45-56-515"}, {FALSE
, "S-1-5-21-12-23-34-45-56-516"},
1666 /* 46 */ {FALSE
, "S-1-5-21-12-23-34-45-56-517"}, {FALSE
, "S-1-5-21-12-23-34-45-56-518"},
1667 /* 48 */ {FALSE
, "S-1-5-21-12-23-34-45-56-519"}, {FALSE
, "S-1-5-21-12-23-34-45-56-520"},
1668 /* 50 */ {FALSE
, "S-1-5-21-12-23-34-45-56-553"},
1669 /* Added in Windows Server 2003 */
1670 /* 51 */ {TRUE
, "S-1-5-64-10"}, {TRUE
, "S-1-5-64-21"}, {TRUE
, "S-1-5-64-14"},
1671 /* 54 */ {TRUE
, "S-1-5-15"}, {TRUE
, "S-1-5-1000"}, {FALSE
, "S-1-5-32-557"},
1672 /* 57 */ {TRUE
, "S-1-5-32-558"}, {TRUE
, "S-1-5-32-559"}, {TRUE
, "S-1-5-32-560"},
1673 /* 60 */ {TRUE
, "S-1-5-32-561"}, {TRUE
, "S-1-5-32-562"},
1674 /* Added in Windows Vista: */
1675 /* 62 */ {TRUE
, "S-1-5-32-568"},
1676 /* 63 */ {TRUE
, "S-1-5-17"}, {FALSE
, "S-1-5-32-569"}, {TRUE
, "S-1-16-0"},
1677 /* 66 */ {TRUE
, "S-1-16-4096"}, {TRUE
, "S-1-16-8192"}, {TRUE
, "S-1-16-12288"},
1678 /* 69 */ {TRUE
, "S-1-16-16384"}, {TRUE
, "S-1-5-33"}, {TRUE
, "S-1-3-4"},
1679 /* 72 */ {FALSE
, "S-1-5-21-12-23-34-45-56-571"}, {FALSE
, "S-1-5-21-12-23-34-45-56-572"},
1680 /* 74 */ {TRUE
, "S-1-5-22"}, {FALSE
, "S-1-5-21-12-23-34-45-56-521"}, {TRUE
, "S-1-5-32-573"},
1681 /* 77 */ {FALSE
, "S-1-5-21-12-23-34-45-56-498"}, {TRUE
, "S-1-5-32-574"}, {TRUE
, "S-1-16-8448"}
1684 static void test_CreateWellKnownSid(void)
1686 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
1687 PSID domainsid
, sid
;
1692 if (!pCreateWellKnownSid
)
1694 win_skip("CreateWellKnownSid not available\n");
1699 SetLastError(0xdeadbeef);
1700 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1701 error
= GetLastError();
1702 ok(!ret
, "CreateWellKnownSid succeeded\n");
1703 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1704 ok(size
, "expected size > 0\n");
1706 SetLastError(0xdeadbeef);
1707 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1708 error
= GetLastError();
1709 ok(!ret
, "CreateWellKnownSid succeeded\n");
1710 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
1712 sid
= HeapAlloc(GetProcessHeap(), 0, size
);
1713 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, sid
, &size
);
1714 ok(ret
, "CreateWellKnownSid failed %u\n", GetLastError());
1715 HeapFree(GetProcessHeap(), 0, sid
);
1717 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1718 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
1720 for (i
= 0; i
< sizeof(well_known_sid_values
)/sizeof(well_known_sid_values
[0]); i
++)
1722 const struct well_known_sid_value
*value
= &well_known_sid_values
[i
];
1723 char sid_buffer
[SECURITY_MAX_SID_SIZE
];
1727 if (value
->sid_string
== NULL
)
1730 if (i
> WinAccountRasAndIasServersSid
)
1732 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1733 cb
= sizeof(sid_buffer
);
1734 if (!pCreateWellKnownSid(i
, domainsid
, sid_buffer
, &cb
))
1736 skip("Well known SIDs starting from %u are not implemented\n", i
);
1741 cb
= sizeof(sid_buffer
);
1742 ok(pCreateWellKnownSid(i
, value
->without_domain
? NULL
: domainsid
, sid_buffer
, &cb
), "Couldn't create well known sid %u\n", i
);
1743 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1744 ok(IsValidSid(sid_buffer
), "The sid is not valid\n");
1745 ok(pConvertSidToStringSidA(sid_buffer
, &str
), "Couldn't convert SID to string\n");
1746 ok(strcmp(str
, value
->sid_string
) == 0, "%d: SID mismatch - expected %s, got %s\n", i
,
1747 value
->sid_string
, str
);
1750 if (value
->without_domain
)
1752 char buf2
[SECURITY_MAX_SID_SIZE
];
1754 ok(pCreateWellKnownSid(i
, domainsid
, buf2
, &cb
), "Couldn't create well known sid %u with optional domain\n", i
);
1755 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1756 ok(memcmp(buf2
, sid_buffer
, cb
) == 0, "SID create with domain is different than without (%u)\n", i
);
1763 static void test_LookupAccountSid(void)
1765 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1766 CHAR accountA
[MAX_PATH
], domainA
[MAX_PATH
], usernameA
[MAX_PATH
];
1767 DWORD acc_sizeA
, dom_sizeA
, user_sizeA
;
1768 DWORD real_acc_sizeA
, real_dom_sizeA
;
1769 WCHAR accountW
[MAX_PATH
], domainW
[MAX_PATH
];
1770 DWORD acc_sizeW
, dom_sizeW
;
1771 DWORD real_acc_sizeW
, real_dom_sizeW
;
1772 PSID pUsersSid
= NULL
;
1775 DWORD error
, size
, cbti
= 0;
1780 PTOKEN_USER ptiUser
= NULL
;
1782 /* native windows crashes if account size, domain size, or name use is NULL */
1784 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1785 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &pUsersSid
);
1786 ok(ret
|| (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
),
1787 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1789 /* not running on NT so give up */
1790 if (!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1793 real_acc_sizeA
= MAX_PATH
;
1794 real_dom_sizeA
= MAX_PATH
;
1795 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1796 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1798 /* try NULL account */
1799 acc_sizeA
= MAX_PATH
;
1800 dom_sizeA
= MAX_PATH
;
1801 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1802 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1804 /* try NULL domain */
1805 acc_sizeA
= MAX_PATH
;
1806 dom_sizeA
= MAX_PATH
;
1807 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1808 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1810 /* try a small account buffer */
1812 dom_sizeA
= MAX_PATH
;
1814 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1815 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1816 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1817 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1819 /* try a 0 sized account buffer */
1821 dom_sizeA
= MAX_PATH
;
1823 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1824 /* this can fail or succeed depending on OS version but the size will always be returned */
1825 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1826 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1827 real_acc_sizeA
+ 1, acc_sizeA
);
1829 /* try a 0 sized account buffer */
1831 dom_sizeA
= MAX_PATH
;
1832 LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1833 /* this can fail or succeed depending on OS version but the size will always be returned */
1834 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1835 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1836 real_acc_sizeA
+ 1, acc_sizeA
);
1838 /* try a small domain buffer */
1840 acc_sizeA
= MAX_PATH
;
1842 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1843 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1844 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1845 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1847 /* try a 0 sized domain buffer */
1849 acc_sizeA
= MAX_PATH
;
1851 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1852 /* this can fail or succeed depending on OS version but the size will always be returned */
1853 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1854 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1855 real_dom_sizeA
+ 1, dom_sizeA
);
1857 /* try a 0 sized domain buffer */
1859 acc_sizeA
= MAX_PATH
;
1860 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1861 /* this can fail or succeed depending on OS version but the size will always be returned */
1862 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1863 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1864 real_dom_sizeA
+ 1, dom_sizeA
);
1866 real_acc_sizeW
= MAX_PATH
;
1867 real_dom_sizeW
= MAX_PATH
;
1868 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &real_acc_sizeW
, domainW
, &real_dom_sizeW
, &use
);
1869 ok(ret
, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1871 /* try an invalid system name */
1872 real_acc_sizeA
= MAX_PATH
;
1873 real_dom_sizeA
= MAX_PATH
;
1874 ret
= LookupAccountSidA("deepthought", pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1875 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1876 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
1877 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1879 /* native windows crashes if domainW or accountW is NULL */
1881 /* try a small account buffer */
1883 dom_sizeW
= MAX_PATH
;
1885 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1886 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1887 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1888 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1890 /* try a 0 sized account buffer */
1892 dom_sizeW
= MAX_PATH
;
1894 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1895 /* this can fail or succeed depending on OS version but the size will always be returned */
1896 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1897 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1898 real_acc_sizeW
+ 1, acc_sizeW
);
1900 /* try a 0 sized account buffer */
1902 dom_sizeW
= MAX_PATH
;
1903 LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1904 /* this can fail or succeed depending on OS version but the size will always be returned */
1905 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1906 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1907 real_acc_sizeW
+ 1, acc_sizeW
);
1909 /* try a small domain buffer */
1911 acc_sizeW
= MAX_PATH
;
1913 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1914 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1915 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1916 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1918 /* try a 0 sized domain buffer */
1920 acc_sizeW
= MAX_PATH
;
1922 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1923 /* this can fail or succeed depending on OS version but the size will always be returned */
1924 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1925 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1926 real_dom_sizeW
+ 1, dom_sizeW
);
1928 /* try a 0 sized domain buffer */
1930 acc_sizeW
= MAX_PATH
;
1931 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1932 /* this can fail or succeed depending on OS version but the size will always be returned */
1933 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1934 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1935 real_dom_sizeW
+ 1, dom_sizeW
);
1937 acc_sizeW
= dom_sizeW
= use
= 0;
1938 SetLastError(0xdeadbeef);
1939 ret
= LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1940 error
= GetLastError();
1941 ok(!ret
, "LookupAccountSidW failed %u\n", GetLastError());
1942 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1943 ok(acc_sizeW
, "expected non-zero account size\n");
1944 ok(dom_sizeW
, "expected non-zero domain size\n");
1945 ok(!use
, "expected zero use %u\n", use
);
1949 /* Test LookupAccountSid with Sid retrieved from token information.
1950 This assumes this process is running under the account of the current user.*/
1951 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &hToken
);
1952 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1953 ret
= GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &cbti
);
1954 ok(!ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1955 ptiUser
= HeapAlloc(GetProcessHeap(), 0, cbti
);
1956 if (GetTokenInformation(hToken
, TokenUser
, ptiUser
, cbti
, &cbti
))
1958 acc_sizeA
= dom_sizeA
= MAX_PATH
;
1959 ret
= LookupAccountSidA(NULL
, ptiUser
->User
.Sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1960 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1961 user_sizeA
= MAX_PATH
;
1962 ret
= GetUserNameA(usernameA
, &user_sizeA
);
1963 ok(ret
, "GetUserNameA() Expected TRUE, got FALSE\n");
1964 ok(lstrcmpA(usernameA
, accountA
) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA
, accountA
);
1966 HeapFree(GetProcessHeap(), 0, ptiUser
);
1968 if (pCreateWellKnownSid
&& pConvertSidToStringSidA
)
1970 trace("Well Known SIDs:\n");
1971 for (i
= 0; i
<= 60; i
++)
1973 size
= SECURITY_MAX_SID_SIZE
;
1974 if (pCreateWellKnownSid(i
, NULL
, &max_sid
.sid
, &size
))
1976 if (pConvertSidToStringSidA(&max_sid
.sid
, &str_sidA
))
1978 acc_sizeA
= MAX_PATH
;
1979 dom_sizeA
= MAX_PATH
;
1980 if (LookupAccountSidA(NULL
, &max_sid
.sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
))
1981 trace(" %d: %s %s\\%s %d\n", i
, str_sidA
, domainA
, accountA
, use
);
1982 LocalFree(str_sidA
);
1987 if (GetLastError() != ERROR_INVALID_PARAMETER
)
1988 trace(" CreateWellKnownSid(%d) failed: %d\n", i
, GetLastError());
1990 trace(" %d: not supported\n", i
);
1994 pLsaQueryInformationPolicy
= (void *)GetProcAddress( hmod
, "LsaQueryInformationPolicy");
1995 pLsaOpenPolicy
= (void *)GetProcAddress( hmod
, "LsaOpenPolicy");
1996 pLsaFreeMemory
= (void *)GetProcAddress( hmod
, "LsaFreeMemory");
1997 pLsaClose
= (void *)GetProcAddress( hmod
, "LsaClose");
1999 if (pLsaQueryInformationPolicy
&& pLsaOpenPolicy
&& pLsaFreeMemory
&& pLsaClose
)
2003 LSA_OBJECT_ATTRIBUTES object_attributes
;
2005 ZeroMemory(&object_attributes
, sizeof(object_attributes
));
2006 object_attributes
.Length
= sizeof(object_attributes
);
2008 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_ALL_ACCESS
, &handle
);
2009 ok(status
== STATUS_SUCCESS
|| status
== STATUS_ACCESS_DENIED
,
2010 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status
);
2012 /* try a more restricted access mask if necessary */
2013 if (status
== STATUS_ACCESS_DENIED
) {
2014 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
2015 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_VIEW_LOCAL_INFORMATION
, &handle
);
2016 ok(status
== STATUS_SUCCESS
, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status
);
2019 if (status
== STATUS_SUCCESS
)
2021 PPOLICY_ACCOUNT_DOMAIN_INFO info
;
2022 status
= pLsaQueryInformationPolicy(handle
, PolicyAccountDomainInformation
, (PVOID
*)&info
);
2023 ok(status
== STATUS_SUCCESS
, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status
);
2024 if (status
== STATUS_SUCCESS
)
2026 ok(info
->DomainSid
!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
2027 if (info
->DomainSid
)
2029 int count
= *GetSidSubAuthorityCount(info
->DomainSid
);
2030 CopySid(GetSidLengthRequired(count
), &max_sid
, info
->DomainSid
);
2031 test_sid_str((PSID
)&max_sid
.sid
);
2032 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_ADMIN
;
2033 max_sid
.sid
.SubAuthorityCount
= count
+ 1;
2034 test_sid_str((PSID
)&max_sid
.sid
);
2035 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_GUEST
;
2036 test_sid_str((PSID
)&max_sid
.sid
);
2037 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ADMINS
;
2038 test_sid_str((PSID
)&max_sid
.sid
);
2039 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_USERS
;
2040 test_sid_str((PSID
)&max_sid
.sid
);
2041 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_GUESTS
;
2042 test_sid_str((PSID
)&max_sid
.sid
);
2043 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_COMPUTERS
;
2044 test_sid_str((PSID
)&max_sid
.sid
);
2045 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CONTROLLERS
;
2046 test_sid_str((PSID
)&max_sid
.sid
);
2047 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CERT_ADMINS
;
2048 test_sid_str((PSID
)&max_sid
.sid
);
2049 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_SCHEMA_ADMINS
;
2050 test_sid_str((PSID
)&max_sid
.sid
);
2051 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
;
2052 test_sid_str((PSID
)&max_sid
.sid
);
2053 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_POLICY_ADMINS
;
2054 test_sid_str((PSID
)&max_sid
.sid
);
2055 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_ALIAS_RID_RAS_SERVERS
;
2056 test_sid_str((PSID
)&max_sid
.sid
);
2057 max_sid
.sid
.SubAuthority
[count
] = 1000; /* first user account */
2058 test_sid_str((PSID
)&max_sid
.sid
);
2061 pLsaFreeMemory((LPVOID
)info
);
2064 status
= pLsaClose(handle
);
2065 ok(status
== STATUS_SUCCESS
, "LsaClose() failed, returned 0x%08x\n", status
);
2071 static BOOL
get_sid_info(PSID psid
, LPSTR
*user
, LPSTR
*dom
)
2073 static CHAR account
[UNLEN
+ 1];
2074 static CHAR domain
[UNLEN
+ 1];
2075 DWORD size
, dom_size
;
2081 size
= dom_size
= UNLEN
+ 1;
2084 SetLastError(0xdeadbeef);
2085 return LookupAccountSidA(NULL
, psid
, account
, &size
, domain
, &dom_size
, &use
);
2088 static void check_wellknown_name(const char* name
, WELL_KNOWN_SID_TYPE result
)
2090 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
2091 PSID domainsid
= NULL
;
2092 char wk_sid
[SECURITY_MAX_SID_SIZE
];
2095 DWORD sid_size
, domain_size
;
2096 SID_NAME_USE sid_use
;
2097 LPSTR domain
, account
, sid_domain
, wk_domain
, wk_account
;
2103 ret
= LookupAccountNameA(NULL
, name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2104 ok(!ret
, " %s Should have failed to lookup account name\n", name
);
2105 psid
= HeapAlloc(GetProcessHeap(),0,sid_size
);
2106 domain
= HeapAlloc(GetProcessHeap(),0,domain_size
);
2107 ret
= LookupAccountNameA(NULL
, name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2111 ok(!ret
, " %s Should have failed to lookup account name\n",name
);
2115 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
2116 cb
= sizeof(wk_sid
);
2117 if (!pCreateWellKnownSid(result
, domainsid
, wk_sid
, &cb
))
2119 win_skip("SID %i is not available on the system\n",result
);
2123 ret2
= get_sid_info(wk_sid
, &wk_account
, &wk_domain
);
2124 if (!ret2
&& GetLastError() == ERROR_NONE_MAPPED
)
2126 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name
);
2130 get_sid_info(psid
, &account
, &sid_domain
);
2132 ok(ret
, "Failed to lookup account name %s\n",name
);
2133 ok(sid_size
!= 0, "sid_size was zero\n");
2135 ok(EqualSid(psid
,wk_sid
),"(%s) Sids fail to match well known sid!\n",name
);
2137 ok(!lstrcmpA(account
, wk_account
), "Expected %s , got %s\n", account
, wk_account
);
2138 ok(!lstrcmpA(domain
, wk_domain
), "Expected %s, got %s\n", wk_domain
, domain
);
2139 ok(sid_use
== SidTypeWellKnownGroup
, "Expected Use (5), got %d\n", sid_use
);
2143 HeapFree(GetProcessHeap(),0,psid
);
2144 HeapFree(GetProcessHeap(),0,domain
);
2147 static void test_LookupAccountName(void)
2149 DWORD sid_size
, domain_size
, user_size
;
2150 DWORD sid_save
, domain_save
;
2151 CHAR user_name
[UNLEN
+ 1];
2152 CHAR computer_name
[UNLEN
+ 1];
2153 SID_NAME_USE sid_use
;
2154 LPSTR domain
, account
, sid_dom
;
2158 /* native crashes if (assuming all other parameters correct):
2160 * - Sid is NULL and cbSid is > 0
2161 * - cbSid or cchReferencedDomainName are NULL
2162 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2165 user_size
= UNLEN
+ 1;
2166 SetLastError(0xdeadbeef);
2167 ret
= GetUserNameA(user_name
, &user_size
);
2168 ok(ret
, "Failed to get user name : %d\n", GetLastError());
2173 sid_use
= 0xcafebabe;
2174 SetLastError(0xdeadbeef);
2175 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2176 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2178 win_skip("LookupAccountNameA is not implemented\n");
2181 ok(!ret
, "Expected 0, got %d\n", ret
);
2182 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2183 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2184 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2185 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2186 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2188 sid_save
= sid_size
;
2189 domain_save
= domain_size
;
2191 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2192 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2194 /* try valid account name */
2195 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2196 get_sid_info(psid
, &account
, &sid_dom
);
2197 ok(ret
, "Failed to lookup account name\n");
2198 ok(sid_size
== GetLengthSid(psid
), "Expected %d, got %d\n", GetLengthSid(psid
), sid_size
);
2199 ok(!lstrcmpA(account
, user_name
), "Expected %s, got %s\n", user_name
, account
);
2200 ok(!lstrcmpA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2201 ok(domain_size
== domain_save
- 1, "Expected %d, got %d\n", domain_save
- 1, domain_size
);
2202 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2203 ok(sid_use
== SidTypeUser
, "Expected SidTypeUser (%d), got %d\n", SidTypeUser
, sid_use
);
2204 domain_size
= domain_save
;
2205 sid_size
= sid_save
;
2207 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2209 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2213 ret
= LookupAccountNameA(NULL
, "Everyone", psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2214 get_sid_info(psid
, &account
, &sid_dom
);
2215 ok(ret
, "Failed to lookup account name\n");
2216 ok(sid_size
!= 0, "sid_size was zero\n");
2217 ok(!lstrcmpA(account
, "Everyone"), "Expected Everyone, got %s\n", account
);
2218 ok(!lstrcmpA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2219 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2220 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2221 ok(sid_use
== SidTypeWellKnownGroup
, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup
, sid_use
);
2222 domain_size
= domain_save
;
2225 /* NULL Sid with zero sid size */
2226 SetLastError(0xdeadbeef);
2228 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2229 ok(!ret
, "Expected 0, got %d\n", ret
);
2230 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2231 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2232 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2233 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2235 /* try cchReferencedDomainName - 1 */
2236 SetLastError(0xdeadbeef);
2238 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2239 ok(!ret
, "Expected 0, got %d\n", ret
);
2240 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2241 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2242 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2243 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2245 /* NULL ReferencedDomainName with zero domain name size */
2246 SetLastError(0xdeadbeef);
2248 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2249 ok(!ret
, "Expected 0, got %d\n", ret
);
2250 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2251 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2252 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2253 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2255 HeapFree(GetProcessHeap(), 0, psid
);
2256 HeapFree(GetProcessHeap(), 0, domain
);
2258 /* get sizes for NULL account name */
2261 sid_use
= 0xcafebabe;
2262 SetLastError(0xdeadbeef);
2263 ret
= LookupAccountNameA(NULL
, NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2264 if (!ret
&& GetLastError() == ERROR_NONE_MAPPED
)
2265 win_skip("NULL account name doesn't work on NT4\n");
2268 ok(!ret
, "Expected 0, got %d\n", ret
);
2269 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2270 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2271 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2272 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2273 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2275 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2276 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2278 /* try NULL account name */
2279 ret
= LookupAccountNameA(NULL
, NULL
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2280 get_sid_info(psid
, &account
, &sid_dom
);
2281 ok(ret
, "Failed to lookup account name\n");
2282 /* Using a fixed string will not work on different locales */
2283 ok(!lstrcmpA(account
, domain
),
2284 "Got %s for account and %s for domain, these should be the same\n",
2286 ok(sid_use
== SidTypeDomain
, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain
, sid_use
);
2288 HeapFree(GetProcessHeap(), 0, psid
);
2289 HeapFree(GetProcessHeap(), 0, domain
);
2292 /* try an invalid account name */
2293 SetLastError(0xdeadbeef);
2296 ret
= LookupAccountNameA(NULL
, "oogabooga", NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2297 ok(!ret
, "Expected 0, got %d\n", ret
);
2298 ok(GetLastError() == ERROR_NONE_MAPPED
||
2299 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
),
2300 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2301 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2302 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2304 /* try an invalid system name */
2305 SetLastError(0xdeadbeef);
2308 ret
= LookupAccountNameA("deepthought", NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2309 ok(!ret
, "Expected 0, got %d\n", ret
);
2310 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
2311 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2312 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2313 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2315 /* try with the computer name as the account name */
2316 domain_size
= sizeof(computer_name
);
2317 GetComputerNameA(computer_name
, &domain_size
);
2320 ret
= LookupAccountNameA(NULL
, computer_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2321 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
||
2322 GetLastError() == ERROR_NONE_MAPPED
/* in a domain */ ||
2323 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE
) ||
2324 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
)),
2325 "LookupAccountNameA failed: %d\n", GetLastError());
2326 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2328 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2329 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2330 ret
= LookupAccountNameA(NULL
, computer_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2331 ok(ret
, "LookupAccountNameA failed: %d\n", GetLastError());
2332 ok(sid_use
== SidTypeDomain
||
2333 (sid_use
== SidTypeUser
&& ! strcmp(computer_name
, user_name
)), "expected SidTypeDomain for %s, got %d\n", computer_name
, sid_use
);
2334 HeapFree(GetProcessHeap(), 0, domain
);
2335 HeapFree(GetProcessHeap(), 0, psid
);
2338 /* Well Known names */
2339 if (!pCreateWellKnownSid
)
2341 win_skip("CreateWellKnownSid not available\n");
2345 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2347 skip("Non-English locale (skipping well known name creation tests)\n");
2351 check_wellknown_name("LocalService", WinLocalServiceSid
);
2352 check_wellknown_name("Local Service", WinLocalServiceSid
);
2354 check_wellknown_name("Local Service", 0);
2355 check_wellknown_name("NetworkService", WinNetworkServiceSid
);
2356 check_wellknown_name("Network Service", WinNetworkServiceSid
);
2358 /* example of some names where the spaces are not optional */
2359 check_wellknown_name("Terminal Server User", WinTerminalServerSid
);
2360 check_wellknown_name("TerminalServer User", 0);
2361 check_wellknown_name("TerminalServerUser", 0);
2362 check_wellknown_name("Terminal ServerUser", 0);
2364 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid
);
2365 check_wellknown_name("enterprisedomain controllers", 0);
2366 check_wellknown_name("enterprise domaincontrollers", 0);
2367 check_wellknown_name("enterprisedomaincontrollers", 0);
2369 /* case insensitivity */
2370 check_wellknown_name("lOCAlServICE", WinLocalServiceSid
);
2372 /* fully qualified account names */
2373 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid
);
2374 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid
);
2375 check_wellknown_name("nt authority test\\Network Service", 0);
2376 check_wellknown_name("Dummy\\Network Service", 0);
2377 check_wellknown_name("ntauthority\\Network Service", 0);
2380 static void test_security_descriptor(void)
2382 SECURITY_DESCRIPTOR sd
;
2385 BOOL isDefault
, isPresent
, ret
;
2389 SetLastError(0xdeadbeef);
2390 ret
= InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
2391 if (ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2393 win_skip("InitializeSecurityDescriptor is not implemented\n");
2397 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2398 expect_eq(psid
, NULL
, PSID
, "%p");
2399 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2400 sd
.Control
|= SE_DACL_PRESENT
| SE_SACL_PRESENT
;
2402 SetLastError(0xdeadbeef);
2404 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), FALSE
, BOOL
, "%d");
2405 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER
, DWORD
, "%u");
2406 ok(size
> 5, "Size not increased\n");
2409 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), TRUE
, BOOL
, "%d");
2410 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2411 expect_eq(psid
, NULL
, PSID
, "%p");
2412 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2413 ok(GetSecurityDescriptorGroup(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorGroup failed\n");
2414 expect_eq(psid
, NULL
, PSID
, "%p");
2415 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2416 ok(GetSecurityDescriptorDacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorDacl failed\n");
2417 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2418 expect_eq(psid
, NULL
, PSID
, "%p");
2419 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2420 ok(GetSecurityDescriptorSacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorSacl failed\n");
2421 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2422 expect_eq(psid
, NULL
, PSID
, "%p");
2423 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2427 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2428 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2429 static void test_granted_access(HANDLE handle
, ACCESS_MASK access
,
2430 ACCESS_MASK alt
, int line
)
2432 OBJECT_BASIC_INFORMATION obj_info
;
2435 if (!pNtQueryObject
)
2437 skip_(__FILE__
, line
)("Not NT platform - skipping tests\n");
2441 status
= pNtQueryObject( handle
, ObjectBasicInformation
, &obj_info
,
2442 sizeof(obj_info
), NULL
);
2443 ok_(__FILE__
, line
)(!status
, "NtQueryObject with err: %08x\n", status
);
2445 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
||
2446 obj_info
.GrantedAccess
== alt
, "Granted access should be 0x%08x "
2447 "or 0x%08x, instead of 0x%08x\n", access
, alt
, obj_info
.GrantedAccess
);
2449 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
, "Granted access should "
2450 "be 0x%08x, instead of 0x%08x\n", access
, obj_info
.GrantedAccess
);
2453 #define CHECK_SET_SECURITY(o,i,e) \
2457 SetLastError( 0xdeadbeef ); \
2458 res_ = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2459 err = GetLastError(); \
2460 if (e == ERROR_SUCCESS) \
2461 ok(res_, "SetKernelObjectSecurity failed with %d\n", err); \
2463 ok(!res_ && err == e, "SetKernelObjectSecurity should have failed " \
2464 "with %s, instead of %d\n", #e, err); \
2467 static void test_process_security(void)
2471 PTOKEN_PRIMARY_GROUP group
;
2472 PSID AdminSid
= NULL
, UsersSid
= NULL
;
2474 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
2475 char buffer
[MAX_PATH
];
2476 PROCESS_INFORMATION info
;
2477 STARTUPINFOA startup
;
2478 SECURITY_ATTRIBUTES psa
;
2479 HANDLE token
, event
;
2482 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
2483 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
2484 if (!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2486 win_skip("ACLs not implemented - skipping tests\n");
2487 HeapFree(GetProcessHeap(), 0, Acl
);
2490 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2492 /* get owner from the token we might be running as a user not admin */
2493 res
= OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED
, &token
);
2494 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
2497 HeapFree(GetProcessHeap(), 0, Acl
);
2501 res
= GetTokenInformation( token
, TokenOwner
, NULL
, 0, &size
);
2502 ok(!res
, "Expected failure, got %d\n", res
);
2503 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2504 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2506 owner
= HeapAlloc(GetProcessHeap(), 0, size
);
2507 res
= GetTokenInformation( token
, TokenOwner
, owner
, size
, &size
);
2508 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2509 AdminSid
= ((TOKEN_OWNER
*)owner
)->Owner
;
2511 res
= GetTokenInformation( token
, TokenPrimaryGroup
, NULL
, 0, &size
);
2512 ok(!res
, "Expected failure, got %d\n", res
);
2513 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2514 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2516 group
= HeapAlloc(GetProcessHeap(), 0, size
);
2517 res
= GetTokenInformation( token
, TokenPrimaryGroup
, group
, size
, &size
);
2518 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2519 UsersSid
= ((TOKEN_PRIMARY_GROUP
*)group
)->PrimaryGroup
;
2521 CloseHandle( token
);
2524 HeapFree(GetProcessHeap(), 0, group
);
2525 HeapFree(GetProcessHeap(), 0, owner
);
2526 HeapFree(GetProcessHeap(), 0, Acl
);
2530 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, PROCESS_VM_READ
, AdminSid
);
2531 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2532 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, PROCESS_ALL_ACCESS
, AdminSid
);
2533 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2535 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
2536 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
2537 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2539 event
= CreateEventA( NULL
, TRUE
, TRUE
, "test_event" );
2540 ok(event
!= NULL
, "CreateEvent %d\n", GetLastError());
2542 SecurityDescriptor
->Revision
= 0;
2543 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_UNKNOWN_REVISION
);
2544 SecurityDescriptor
->Revision
= SECURITY_DESCRIPTOR_REVISION
;
2546 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2547 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2548 CHECK_SET_SECURITY( event
, SACL_SECURITY_INFORMATION
, ERROR_ACCESS_DENIED
);
2549 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2550 /* NULL DACL is valid and means that everyone has access */
2551 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
;
2552 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2554 /* Set owner and group and dacl */
2555 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
2556 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2557 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2558 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, FALSE
);
2559 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2560 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2561 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2562 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2563 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2565 sprintf(buffer
, "%s tests/security.c test", myARGV
[0]);
2566 memset(&startup
, 0, sizeof(startup
));
2567 startup
.cb
= sizeof(startup
);
2568 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
2569 startup
.wShowWindow
= SW_SHOWNORMAL
;
2571 psa
.nLength
= sizeof(psa
);
2572 psa
.lpSecurityDescriptor
= SecurityDescriptor
;
2573 psa
.bInheritHandle
= TRUE
;
2575 /* Doesn't matter what ACL say we should get full access for ourselves */
2576 res
= CreateProcessA( NULL
, buffer
, &psa
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
2577 ok(res
, "CreateProcess with err:%d\n", GetLastError());
2578 TEST_GRANTED_ACCESS2( info
.hProcess
, PROCESS_ALL_ACCESS_NT4
,
2579 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2580 winetest_wait_child_process( info
.hProcess
);
2582 CloseHandle( info
.hProcess
);
2583 CloseHandle( info
.hThread
);
2584 CloseHandle( event
);
2585 HeapFree(GetProcessHeap(), 0, group
);
2586 HeapFree(GetProcessHeap(), 0, owner
);
2587 HeapFree(GetProcessHeap(), 0, Acl
);
2588 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
2591 static void test_process_security_child(void)
2593 HANDLE handle
, handle1
;
2597 handle
= OpenProcess( PROCESS_TERMINATE
, FALSE
, GetCurrentProcessId() );
2598 ok(handle
!= NULL
, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2599 TEST_GRANTED_ACCESS( handle
, PROCESS_TERMINATE
);
2601 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2602 &handle1
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2603 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2604 TEST_GRANTED_ACCESS( handle1
, PROCESS_TERMINATE
);
2606 CloseHandle( handle1
);
2608 SetLastError( 0xdeadbeef );
2609 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2610 &handle1
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2611 err
= GetLastError();
2613 ok(!ret
&& err
== ERROR_ACCESS_DENIED
, "duplicating handle should have failed "
2614 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err
);
2616 CloseHandle( handle
);
2618 /* These two should fail - they are denied by ACL */
2619 handle
= OpenProcess( PROCESS_VM_READ
, FALSE
, GetCurrentProcessId() );
2621 ok(handle
== NULL
, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2622 handle
= OpenProcess( PROCESS_ALL_ACCESS
, FALSE
, GetCurrentProcessId() );
2624 ok(handle
== NULL
, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2626 /* Documented privilege elevation */
2627 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2628 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2629 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2630 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2631 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2633 CloseHandle( handle
);
2635 /* Same only explicitly asking for all access rights */
2636 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2637 &handle
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2638 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2639 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2640 PROCESS_ALL_ACCESS
| PROCESS_QUERY_LIMITED_INFORMATION
);
2641 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2642 &handle1
, PROCESS_VM_READ
, TRUE
, 0 );
2643 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2644 TEST_GRANTED_ACCESS( handle1
, PROCESS_VM_READ
);
2645 CloseHandle( handle1
);
2646 CloseHandle( handle
);
2649 static void test_impersonation_level(void)
2651 HANDLE Token
, ProcessToken
;
2654 TOKEN_PRIVILEGES
*Privileges
;
2656 PRIVILEGE_SET
*PrivilegeSet
;
2662 pDuplicateTokenEx
= (void *)GetProcAddress(hmod
, "DuplicateTokenEx");
2663 if( !pDuplicateTokenEx
) {
2664 win_skip("DuplicateTokenEx is not available\n");
2667 SetLastError(0xdeadbeef);
2668 ret
= ImpersonateSelf(SecurityAnonymous
);
2669 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2671 win_skip("ImpersonateSelf is not implemented\n");
2674 ok(ret
, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2675 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2676 ok(!ret
, "OpenThreadToken should have failed\n");
2677 error
= GetLastError();
2678 ok(error
== ERROR_CANT_OPEN_ANONYMOUS
, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2679 /* can't perform access check when opening object against an anonymous impersonation token */
2681 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2682 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_CANT_OPEN_ANONYMOUS
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2683 "RegOpenKeyEx failed with %d\n", error
);
2687 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
2688 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2690 ret
= pDuplicateTokenEx(ProcessToken
,
2691 TOKEN_QUERY
| TOKEN_DUPLICATE
| TOKEN_IMPERSONATE
, NULL
,
2692 SecurityAnonymous
, TokenImpersonation
, &Token
);
2693 ok(ret
, "DuplicateTokenEx failed with error %d\n", GetLastError());
2694 /* can't increase the impersonation level */
2695 ret
= DuplicateToken(Token
, SecurityIdentification
, &Token2
);
2696 error
= GetLastError();
2697 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2698 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2699 /* we can query anything from an anonymous token, including the user */
2700 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
2701 error
= GetLastError();
2702 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2703 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
2704 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
2705 ok(ret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2706 HeapFree(GetProcessHeap(), 0, User
);
2708 /* PrivilegeCheck fails with SecurityAnonymous level */
2709 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
2710 error
= GetLastError();
2711 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2712 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
2713 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
2714 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2716 PrivilegeSet
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[Privileges
->PrivilegeCount
]));
2717 PrivilegeSet
->PrivilegeCount
= Privileges
->PrivilegeCount
;
2718 memcpy(PrivilegeSet
->Privilege
, Privileges
->Privileges
, PrivilegeSet
->PrivilegeCount
* sizeof(PrivilegeSet
->Privilege
[0]));
2719 PrivilegeSet
->Control
= PRIVILEGE_SET_ALL_NECESSARY
;
2720 HeapFree(GetProcessHeap(), 0, Privileges
);
2722 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2723 error
= GetLastError();
2724 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2728 ret
= ImpersonateSelf(SecurityIdentification
);
2729 ok(ret
, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2730 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2731 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2733 /* can't perform access check when opening object against an identification impersonation token */
2734 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2736 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2737 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2739 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2740 ok(ret
, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2744 ret
= ImpersonateSelf(SecurityImpersonation
);
2745 ok(ret
, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2746 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2747 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2748 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2749 ok(error
== ERROR_SUCCESS
, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error
);
2751 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2752 ok(ret
, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2756 CloseHandle(ProcessToken
);
2758 HeapFree(GetProcessHeap(), 0, PrivilegeSet
);
2761 static void test_SetEntriesInAclW(void)
2764 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2765 PACL OldAcl
= NULL
, NewAcl
;
2766 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2767 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2768 EXPLICIT_ACCESSW ExplicitAccess
;
2769 static const WCHAR wszEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2770 static const WCHAR wszCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2772 if (!pSetEntriesInAclW
)
2774 win_skip("SetEntriesInAclW is not available\n");
2778 NewAcl
= (PACL
)0xdeadbeef;
2779 res
= pSetEntriesInAclW(0, NULL
, NULL
, &NewAcl
);
2780 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2782 win_skip("SetEntriesInAclW is not implemented\n");
2785 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2786 ok(NewAcl
== NULL
||
2787 broken(NewAcl
!= NULL
), /* NT4 */
2788 "NewAcl=%p, expected NULL\n", NewAcl
);
2791 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2792 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2793 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2795 win_skip("ACLs not implemented - skipping tests\n");
2796 HeapFree(GetProcessHeap(), 0, OldAcl
);
2799 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2801 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2802 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2804 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2805 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2806 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2808 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2809 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2811 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2812 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2813 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2814 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2815 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2816 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2817 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= 0xDEADBEEF;
2818 ExplicitAccess
.Trustee
.pMultipleTrustee
= (PVOID
)0xDEADBEEF;
2819 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2820 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2821 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2824 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2825 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2826 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2827 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2828 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2829 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2832 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2834 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2838 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2839 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszEveryone
;
2840 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2841 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2842 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2845 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2846 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2847 ok(res
== ERROR_INVALID_PARAMETER
||
2848 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2849 "SetEntriesInAclW failed: %u\n", res
);
2850 ok(NewAcl
== NULL
||
2851 broken(NewAcl
!= NULL
), /* NT4 */
2852 "returned acl wasn't NULL: %p\n", NewAcl
);
2854 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2855 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2856 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2857 ok(res
== ERROR_INVALID_PARAMETER
||
2858 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2859 "SetEntriesInAclW failed: %u\n", res
);
2860 ok(NewAcl
== NULL
||
2861 broken(NewAcl
!= NULL
), /* NT4 */
2862 "returned acl wasn't NULL: %p\n", NewAcl
);
2864 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2865 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2866 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2867 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2868 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2872 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2873 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszCurrentUser
;
2874 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2875 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2876 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2879 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
2880 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2881 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
2882 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2883 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2884 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2888 FreeSid(EveryoneSid
);
2889 HeapFree(GetProcessHeap(), 0, OldAcl
);
2892 static void test_SetEntriesInAclA(void)
2895 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2896 PACL OldAcl
= NULL
, NewAcl
;
2897 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2898 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2899 EXPLICIT_ACCESSA ExplicitAccess
;
2900 static const CHAR szEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2901 static const CHAR szCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2903 if (!pSetEntriesInAclA
)
2905 win_skip("SetEntriesInAclA is not available\n");
2909 NewAcl
= (PACL
)0xdeadbeef;
2910 res
= pSetEntriesInAclA(0, NULL
, NULL
, &NewAcl
);
2911 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2913 win_skip("SetEntriesInAclA is not implemented\n");
2916 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2917 ok(NewAcl
== NULL
||
2918 broken(NewAcl
!= NULL
), /* NT4 */
2919 "NewAcl=%p, expected NULL\n", NewAcl
);
2922 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2923 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2924 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2926 win_skip("ACLs not implemented - skipping tests\n");
2927 HeapFree(GetProcessHeap(), 0, OldAcl
);
2930 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2932 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2933 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2935 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2936 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2937 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2939 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2940 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2942 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2943 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2944 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2945 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2946 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2947 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2948 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2949 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2950 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2951 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2952 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2955 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2956 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2957 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2958 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2959 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2960 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2963 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2965 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2969 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2970 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szEveryone
;
2971 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2972 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2973 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2976 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2977 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2978 ok(res
== ERROR_INVALID_PARAMETER
||
2979 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2980 "SetEntriesInAclA failed: %u\n", res
);
2981 ok(NewAcl
== NULL
||
2982 broken(NewAcl
!= NULL
), /* NT4 */
2983 "returned acl wasn't NULL: %p\n", NewAcl
);
2985 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2986 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2987 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2988 ok(res
== ERROR_INVALID_PARAMETER
||
2989 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2990 "SetEntriesInAclA failed: %u\n", res
);
2991 ok(NewAcl
== NULL
||
2992 broken(NewAcl
!= NULL
), /* NT4 */
2993 "returned acl wasn't NULL: %p\n", NewAcl
);
2995 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2996 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2997 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2998 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2999 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3003 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3004 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szCurrentUser
;
3005 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3006 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3007 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3010 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
3011 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
3012 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
3013 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3014 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3015 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3019 FreeSid(EveryoneSid
);
3020 HeapFree(GetProcessHeap(), 0, OldAcl
);
3023 static void test_CreateDirectoryA(void)
3025 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3026 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3027 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
3028 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3029 PSECURITY_DESCRIPTOR pSD
= &sd
;
3030 ACL_SIZE_INFORMATION acl_size
;
3031 ACCESS_ALLOWED_ACE
*ace
;
3032 SECURITY_ATTRIBUTES sa
;
3033 char tmpdir
[MAX_PATH
];
3040 if (!pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3042 win_skip("Required functions are not available\n");
3046 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3048 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3049 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3053 win_skip("Failed to get current user token\n");
3056 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3057 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3058 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3059 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3060 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3061 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3062 CloseHandle( token
);
3063 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3065 sa
.nLength
= sizeof(sa
);
3066 sa
.lpSecurityDescriptor
= pSD
;
3067 sa
.bInheritHandle
= TRUE
;
3068 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3069 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3070 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3071 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3072 ok(bret
, "Failed to initialize ACL.\n");
3073 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3074 GENERIC_ALL
, user_sid
);
3075 ok(bret
, "Failed to add Current User to ACL.\n");
3076 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3077 GENERIC_ALL
, admin_sid
);
3078 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3079 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3080 ok(bret
, "Failed to add ACL to security desciptor.\n");
3082 GetTempPathA(MAX_PATH
, tmpdir
);
3083 lstrcatA(tmpdir
, "Please Remove Me");
3084 bret
= CreateDirectoryA(tmpdir
, &sa
);
3085 ok(bret
== TRUE
, "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
3086 HeapFree(GetProcessHeap(), 0, pDacl
);
3088 SetLastError(0xdeadbeef);
3089 error
= pGetNamedSecurityInfoA(tmpdir
, SE_FILE_OBJECT
,
3090 OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, (PSID
*)&owner
,
3091 NULL
, &pDacl
, NULL
, &pSD
);
3092 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3094 win_skip("GetNamedSecurityInfoA is not implemented\n");
3097 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3099 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3100 ok(bret
, "GetAclInformation failed\n");
3101 ok(acl_size
.AceCount
== 2, "GetAclInformation returned unexpected entry count (%d != 2).\n",
3103 if (acl_size
.AceCount
> 0)
3105 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3106 ok(bret
, "Failed to get Current User ACE.\n");
3107 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3108 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
3109 todo_wine
ok(((ACE_HEADER
*)ace
)->AceFlags
== (OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
),
3110 "Current User ACE has unexpected flags (0x%x != 0x03)\n",
3111 ((ACE_HEADER
*)ace
)->AceFlags
);
3112 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3115 if (acl_size
.AceCount
> 1)
3117 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3118 ok(bret
, "Failed to get Administators Group ACE.\n");
3119 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3120 todo_wine
ok(bret
, "Administators Group ACE != Administators Group SID.\n");
3121 todo_wine
ok(((ACE_HEADER
*)ace
)->AceFlags
== (OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
),
3122 "Administators Group ACE has unexpected flags (0x%x != 0x03)\n",
3123 ((ACE_HEADER
*)ace
)->AceFlags
);
3124 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3130 HeapFree(GetProcessHeap(), 0, user
);
3131 bret
= RemoveDirectoryA(tmpdir
);
3132 ok(bret
== TRUE
, "RemoveDirectoryA should always succeed\n");
3135 static void test_GetNamedSecurityInfoA(void)
3137 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3138 char system_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3139 char users_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3140 PSID admin_sid
= (PSID
) admin_ptr
, users_sid
= (PSID
) users_ptr
;
3141 PSID system_sid
= (PSID
) system_ptr
, user_sid
;
3142 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3143 char invalid_path
[] = "/an invalid file path";
3144 int users_ace_id
= -1, admins_ace_id
= -1, i
;
3145 char software_key
[] = "MACHINE\\Software";
3146 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3147 SECURITY_DESCRIPTOR_CONTROL control
;
3148 ACL_SIZE_INFORMATION acl_size
;
3149 CHAR windows_dir
[MAX_PATH
];
3150 PSECURITY_DESCRIPTOR pSD
;
3151 ACCESS_ALLOWED_ACE
*ace
;
3152 BOOL bret
= TRUE
, isNT4
;
3153 char tmpfile
[MAX_PATH
];
3154 DWORD error
, revision
;
3155 BOOL owner_defaulted
;
3156 BOOL group_defaulted
;
3157 BOOL dacl_defaulted
;
3158 HANDLE token
, hTemp
;
3164 if (!pSetNamedSecurityInfoA
|| !pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3166 win_skip("Required functions are not available\n");
3170 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3172 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3173 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3177 win_skip("Failed to get current user token\n");
3180 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3181 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3182 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3183 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3184 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3185 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3186 CloseHandle( token
);
3187 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3189 bret
= GetWindowsDirectoryA(windows_dir
, MAX_PATH
);
3190 ok(bret
, "GetWindowsDirectory failed with error %d\n", GetLastError());
3192 SetLastError(0xdeadbeef);
3193 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,
3194 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
3195 NULL
, NULL
, NULL
, NULL
, &pSD
);
3196 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3198 win_skip("GetNamedSecurityInfoA is not implemented\n");
3199 HeapFree(GetProcessHeap(), 0, user
);
3202 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3204 bret
= GetSecurityDescriptorControl(pSD
, &control
, &revision
);
3205 ok(bret
, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
3206 ok((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == (SE_SELF_RELATIVE
|SE_DACL_PRESENT
) ||
3207 broken((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
), /* NT4 */
3208 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control
);
3209 ok(revision
== SECURITY_DESCRIPTOR_REVISION1
, "revision was %d instead of 1\n", revision
);
3211 isNT4
= (control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
;
3213 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3214 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3215 ok(owner
!= NULL
, "owner should not be NULL\n");
3217 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3218 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3219 ok(group
!= NULL
, "group should not be NULL\n");
3223 /* NULL descriptor tests */
3226 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
3227 HeapFree(GetProcessHeap(), 0, user
);
3231 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3232 NULL
, NULL
, NULL
, NULL
, NULL
);
3233 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3236 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3237 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3238 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3239 ok(pDacl
!= NULL
, "DACL should not be NULL\n");
3242 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,OWNER_SECURITY_INFORMATION
,
3243 NULL
, NULL
, &pDacl
, NULL
, NULL
);
3244 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3246 /* Test behavior of SetNamedSecurityInfo with an invalid path */
3247 SetLastError(0xdeadbeef);
3248 error
= pSetNamedSecurityInfoA(invalid_path
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3250 ok(error
== ERROR_FILE_NOT_FOUND
, "Unexpected error returned: 0x%x\n", error
);
3251 ok(GetLastError() == 0xdeadbeef, "Expected last error to remain unchanged.\n");
3253 /* Create security descriptor information and test that it comes back the same */
3255 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3256 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3257 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3258 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3259 ok(bret
, "Failed to initialize ACL.\n");
3260 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3261 ok(bret
, "Failed to add Current User to ACL.\n");
3262 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
3263 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3264 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3265 ok(bret
, "Failed to add ACL to security desciptor.\n");
3266 GetTempFileNameA(".", "foo", 0, tmpfile
);
3267 hTemp
= CreateFileA(tmpfile
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
3268 FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
3269 SetLastError(0xdeadbeef);
3270 error
= pSetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3272 HeapFree(GetProcessHeap(), 0, pDacl
);
3273 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3275 win_skip("SetNamedSecurityInfoA is not implemented\n");
3276 HeapFree(GetProcessHeap(), 0, user
);
3280 ok(!error
, "SetNamedSecurityInfoA failed with error %d\n", error
);
3281 SetLastError(0xdeadbeef);
3282 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3283 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3284 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3286 win_skip("GetNamedSecurityInfoA is not implemented\n");
3287 HeapFree(GetProcessHeap(), 0, user
);
3291 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3293 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3294 ok(bret
, "GetAclInformation failed\n");
3295 if (acl_size
.AceCount
> 0)
3297 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3298 ok(bret
, "Failed to get Current User ACE.\n");
3299 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3300 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
3301 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3302 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3303 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3306 if (acl_size
.AceCount
> 1)
3308 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3309 ok(bret
, "Failed to get Administators Group ACE.\n");
3310 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3311 todo_wine
ok(bret
|| broken(!bret
) /* win2k */,
3312 "Administators Group ACE != Administators Group SID.\n");
3313 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3314 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3315 ok(ace
->Mask
== 0x1f01ff || broken(ace
->Mask
== GENERIC_ALL
) /* win2k */,
3316 "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n", ace
->Mask
);
3319 HeapFree(GetProcessHeap(), 0, user
);
3322 /* Test querying the ownership of a built-in registry key */
3323 sid_size
= sizeof(system_ptr
);
3324 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, system_sid
, &sid_size
);
3325 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
,
3326 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
,
3327 NULL
, NULL
, NULL
, NULL
, &pSD
);
3328 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3330 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3331 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3332 ok(owner
!= NULL
, "owner should not be NULL\n");
3333 ok(EqualSid(owner
, admin_sid
), "MACHINE\\Software owner SID != Administrators SID.\n");
3335 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3336 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3337 ok(group
!= NULL
, "group should not be NULL\n");
3338 ok(EqualSid(group
, admin_sid
) || broken(EqualSid(group
, system_sid
)) /* before Win7 */
3339 || broken(((SID
*)group
)->SubAuthority
[0] == SECURITY_NT_NON_UNIQUE
) /* Vista */,
3340 "MACHINE\\Software group SID != Local System SID.\n");
3343 /* Test querying the DACL of a built-in registry key */
3344 sid_size
= sizeof(users_ptr
);
3345 pCreateWellKnownSid(WinBuiltinUsersSid
, NULL
, users_sid
, &sid_size
);
3346 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
, DACL_SECURITY_INFORMATION
,
3347 NULL
, NULL
, NULL
, NULL
, &pSD
);
3348 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3350 bret
= GetSecurityDescriptorDacl(pSD
, &dacl_present
, &pDacl
, &dacl_defaulted
);
3351 ok(bret
, "GetSecurityDescriptorDacl failed with error %d\n", GetLastError());
3352 ok(dacl_present
, "DACL should be present\n");
3353 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityDescriptorDacl returned invalid DACL.\n");
3354 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3355 ok(bret
, "GetAclInformation failed\n");
3356 ok(acl_size
.AceCount
!= 0, "GetAclInformation returned no ACLs\n");
3357 for (i
=0; i
<acl_size
.AceCount
; i
++)
3359 bret
= pGetAce(pDacl
, i
, (VOID
**)&ace
);
3360 ok(bret
, "Failed to get ACE %d.\n", i
);
3361 bret
= EqualSid(&ace
->SidStart
, users_sid
);
3362 if (bret
) users_ace_id
= i
;
3363 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3364 if (bret
) admins_ace_id
= i
;
3366 ok(users_ace_id
!= -1 || broken(users_ace_id
== -1) /* win2k */,
3367 "Bultin Users ACE not found.\n");
3368 if (users_ace_id
!= -1)
3370 bret
= pGetAce(pDacl
, users_ace_id
, (VOID
**)&ace
);
3371 ok(bret
, "Failed to get Builtin Users ACE.\n");
3372 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3373 ok(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
)
3374 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */,
3375 "Builtin Users ACE has unexpected flags (0x%x != 0x%x)\n", flags
,
3376 INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
);
3377 ok(ace
->Mask
== GENERIC_READ
, "Builtin Users ACE has unexpected mask (0x%x != 0x%x)\n",
3378 ace
->Mask
, GENERIC_READ
);
3380 ok(admins_ace_id
!= -1, "Bultin Admins ACE not found.\n");
3381 if (admins_ace_id
!= -1)
3383 bret
= pGetAce(pDacl
, admins_ace_id
, (VOID
**)&ace
);
3384 ok(bret
, "Failed to get Builtin Admins ACE.\n");
3385 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3387 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */,
3388 "Builtin Admins ACE has unexpected flags (0x%x != 0x0)\n", flags
);
3389 ok(ace
->Mask
== KEY_ALL_ACCESS
|| broken(ace
->Mask
== GENERIC_ALL
) /* w2k8 */,
3390 "Builtin Admins ACE has unexpected mask (0x%x != 0x%x)\n", ace
->Mask
, KEY_ALL_ACCESS
);
3395 static void test_ConvertStringSecurityDescriptor(void)
3398 PSECURITY_DESCRIPTOR pSD
;
3399 static const WCHAR Blank
[] = { 0 };
3403 const char *sidstring
;
3410 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE
, ERROR_UNKNOWN_REVISION
},
3411 /* test ACE string type */
3412 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3413 { "D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3414 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_PARAMETER
},
3415 /* test ACE string with spaces */
3416 { " D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3417 { "D: (D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3418 { "D:( D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3419 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, RPC_S_INVALID_STRING_UUID
, ERROR_INVALID_ACL
}, /* Vista+ */
3420 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3421 { "D:(D;; GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3422 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3423 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1
, TRUE
},
3424 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1
, TRUE
},
3425 { "D:(D;;GA;;; WD)", SDDL_REVISION_1
, TRUE
},
3426 { "D:(D;;GA;;;WD )", SDDL_REVISION_1
, TRUE
},
3427 /* test ACE string access rights */
3428 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3429 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1
, TRUE
},
3430 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1
, TRUE
},
3431 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1
, TRUE
},
3432 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1
, TRUE
},
3433 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1
, TRUE
},
3434 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3435 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3436 /* test ACE string access right error case */
3437 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3438 /* test behaviour with empty strings */
3439 { "", SDDL_REVISION_1
, TRUE
},
3440 /* test ACE string SID */
3441 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1
, TRUE
},
3442 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
, ERROR_INVALID_SID
} /* W2K */
3445 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3447 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3451 for (i
= 0; i
< sizeof(cssd
)/sizeof(cssd
[0]); i
++)
3455 SetLastError(0xdeadbeef);
3456 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3457 cssd
[i
].sidstring
, cssd
[i
].revision
, &pSD
, NULL
);
3458 GLE
= GetLastError();
3459 ok(ret
== cssd
[i
].ret
, "(%02u) Expected %s (%d)\n", i
, cssd
[i
].ret
? "success" : "failure", GLE
);
3461 ok(GLE
== cssd
[i
].GLE
||
3462 (cssd
[i
].altGLE
&& GLE
== cssd
[i
].altGLE
),
3463 "(%02u) Unexpected last error %d\n", i
, GLE
);
3468 /* test behaviour with NULL parameters */
3469 SetLastError(0xdeadbeef);
3470 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3471 NULL
, 0xdeadbeef, &pSD
, NULL
);
3473 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3474 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3477 SetLastError(0xdeadbeef);
3478 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3479 NULL
, 0xdeadbeef, &pSD
, NULL
);
3480 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3481 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3484 SetLastError(0xdeadbeef);
3485 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3486 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL
, NULL
);
3487 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3488 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3491 SetLastError(0xdeadbeef);
3492 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3493 "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, NULL
, NULL
);
3494 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3495 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3498 /* test behaviour with empty strings */
3499 SetLastError(0xdeadbeef);
3500 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3501 Blank
, SDDL_REVISION_1
, &pSD
, NULL
);
3502 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
3506 static void test_ConvertSecurityDescriptorToString(void)
3508 SECURITY_DESCRIPTOR desc
;
3509 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3518 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA
)
3520 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
3523 if (!pCreateWellKnownSid
)
3525 win_skip("CreateWellKnownSid is not available\n");
3529 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
3530 * don't replicate this feature so we only test len >= strlen+1. */
3531 #define CHECK_RESULT_AND_FREE(exp_str) \
3532 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
3533 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlenA(exp_str) + 1, len); \
3536 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
3537 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); \
3538 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); \
3541 InitializeSecurityDescriptor(&desc
, SECURITY_DESCRIPTOR_REVISION
);
3542 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3543 CHECK_RESULT_AND_FREE("");
3546 pCreateWellKnownSid(WinLocalSid
, NULL
, sid_buf
, &size
);
3547 SetSecurityDescriptorOwner(&desc
, sid_buf
, FALSE
);
3548 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3549 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3551 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3552 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3553 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3555 size
= sizeof(sid_buf
);
3556 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, sid_buf
, &size
);
3557 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3558 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3559 CHECK_RESULT_AND_FREE("O:SY");
3561 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
);
3562 SetSecurityDescriptorGroup(&desc
, psid
, TRUE
);
3563 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3564 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
3566 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, GROUP_SECURITY_INFORMATION
, &string
, &len
), "Conversion failed\n");
3567 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3569 pacl
= (PACL
)acl_buf
;
3570 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3571 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, TRUE
);
3572 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3573 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3575 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, FALSE
);
3576 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3577 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3579 pConvertStringSidToSidA("S-1-5-6", &psid2
);
3580 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, 0xf0000000, psid2
);
3581 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3582 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
3584 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, INHERIT_ONLY_ACE
|INHERITED_ACE
, 0x00000003, psid2
);
3585 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3586 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
3588 pAddAccessDeniedAceEx(pacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
, 0xffffffff, psid
);
3589 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3590 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)");
3593 pacl
= (PACL
)acl_buf
;
3594 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3595 SetSecurityDescriptorSacl(&desc
, TRUE
, pacl
, FALSE
);
3596 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3597 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
3599 /* fails in win2k */
3600 SetSecurityDescriptorDacl(&desc
, TRUE
, NULL
, FALSE
);
3601 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, VALID_INHERIT_FLAGS
, KEY_READ
|KEY_WRITE
, psid2
, TRUE
, TRUE
);
3602 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3604 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
3605 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
3608 /* fails in win2k */
3609 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, psid2
, TRUE
, FALSE
);
3610 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3612 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
3613 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
3620 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec
)
3622 SECURITY_DESCRIPTOR_CONTROL ref
;
3623 SECURITY_DESCRIPTOR_CONTROL test
;
3625 SECURITY_DESCRIPTOR_CONTROL
const mutable
3626 = SE_DACL_AUTO_INHERIT_REQ
| SE_SACL_AUTO_INHERIT_REQ
3627 | SE_DACL_AUTO_INHERITED
| SE_SACL_AUTO_INHERITED
3628 | SE_DACL_PROTECTED
| SE_SACL_PROTECTED
3629 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
3631 SECURITY_DESCRIPTOR_CONTROL
const immutable
3632 = SE_OWNER_DEFAULTED
| SE_GROUP_DEFAULTED
3633 | SE_DACL_PRESENT
| SE_DACL_DEFAULTED
3634 | SE_SACL_PRESENT
| SE_SACL_DEFAULTED
3635 | SE_RM_CONTROL_VALID
| SE_SELF_RELATIVE
3640 LPCSTR fmt
= "Expected error %s, got %u\n";
3642 GetSecurityDescriptorControl (sec
, &ref
, &dwRevision
);
3644 /* The mutable bits are mutable regardless of the truth of
3645 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
3647 /* Check call barfs if any bit-of-interest is immutable */
3648 for (bit
= 0; bit
< 16; ++bit
)
3650 SECURITY_DESCRIPTOR_CONTROL
const bitOfInterest
= 1 << bit
;
3651 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitOfInterest
;
3653 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3655 DWORD dwExpect
= (bitOfInterest
& immutable
)
3656 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3657 LPCSTR strExpect
= (bitOfInterest
& immutable
)
3658 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3660 ctrl
= (bitOfInterest
& mutable) ? ref
+ bitOfInterest
: ref
;
3661 setOrClear
^= bitOfInterest
;
3662 SetLastError (0xbebecaca);
3663 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3664 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3665 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3666 expect_eq(test
, ctrl
, int, "%x");
3668 setOrClear
^= bitOfInterest
;
3669 SetLastError (0xbebecaca);
3670 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3671 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3672 GetSecurityDescriptorControl (sec
, &test
, &dwRevision
);
3673 expect_eq(test
, ref
, int, "%x");
3676 /* Check call barfs if any bit-to-set is immutable
3677 even when not a bit-of-interest */
3678 for (bit
= 0; bit
< 16; ++bit
)
3680 SECURITY_DESCRIPTOR_CONTROL
const bitsOfInterest
= mutable;
3681 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitsOfInterest
;
3683 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3685 DWORD dwExpect
= ((1 << bit
) & immutable
)
3686 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3687 LPCSTR strExpect
= ((1 << bit
) & immutable
)
3688 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3690 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| mutable;
3691 setOrClear
^= bitsOfInterest
;
3692 SetLastError (0xbebecaca);
3693 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3694 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3695 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3696 expect_eq(test
, ctrl
, int, "%x");
3698 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| (1 << bit
);
3699 setOrClear
^= bitsOfInterest
;
3700 SetLastError (0xbebecaca);
3701 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3702 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3703 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3704 expect_eq(test
, ctrl
, int, "%x");
3708 static void test_PrivateObjectSecurity(void)
3710 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3711 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3712 PSECURITY_DESCRIPTOR sec
;
3718 PSECURITY_DESCRIPTOR buf
;
3721 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3723 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3727 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3729 "G:S-1-5-21-93476-23408-4576"
3730 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3731 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3732 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3733 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3735 test_SetSecurityDescriptorControl(sec
);
3739 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3741 "G:S-1-5-21-93476-23408-4576",
3742 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3744 test_SetSecurityDescriptorControl(sec
);
3748 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3750 "G:S-1-5-21-93476-23408-4576"
3751 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3752 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3753 buf
= HeapAlloc(GetProcessHeap(), 0, dwDescSize
);
3754 pSetSecurityDescriptorControl(sec
, SE_DACL_PROTECTED
, SE_DACL_PROTECTED
);
3755 GetSecurityDescriptorControl(sec
, &ctrl
, &dwRevision
);
3756 expect_eq(ctrl
, 0x9014, int, "%x");
3758 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3759 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3760 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3761 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3762 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3763 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3764 expect_eq(ctrl
, 0x8000, int, "%x");
3766 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3767 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3768 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3769 ret
= pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
);
3770 ok(ret
, "Conversion failed err=%u\n", GetLastError());
3771 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)",
3772 "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 */
3773 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3774 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8004, int, "%x");
3776 ret
= GetPrivateObjectSecurity(sec
, sec_info
, buf
, dwDescSize
, &retSize
);
3777 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3778 ok(retSize
== dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3779 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3780 CHECK_ONE_OF_AND_FREE("O:SY"
3781 "G:S-1-5-21-93476-23408-4576"
3782 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3783 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3785 "G:S-1-5-21-93476-23408-4576"
3786 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3787 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3788 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3789 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8014, int, "%x");
3791 SetLastError(0xdeadbeef);
3792 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, 5, &retSize
) == FALSE
, "GetPrivateObjectSecurity should have failed\n");
3793 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3796 HeapFree(GetProcessHeap(), 0, buf
);
3798 #undef CHECK_RESULT_AND_FREE
3799 #undef CHECK_ONE_OF_AND_FREE
3801 static void test_acls(void)
3804 PACL pAcl
= (PACL
)buffer
;
3807 SetLastError(0xdeadbeef);
3808 ret
= InitializeAcl(pAcl
, sizeof(ACL
) - 1, ACL_REVISION
);
3809 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3811 win_skip("InitializeAcl is not implemented\n");
3815 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3817 SetLastError(0xdeadbeef);
3818 ret
= InitializeAcl(pAcl
, 0xffffffff, ACL_REVISION
);
3819 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3821 SetLastError(0xdeadbeef);
3822 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION1
);
3823 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3825 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION2
);
3826 ok(ret
, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3828 ret
= IsValidAcl(pAcl
);
3829 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3831 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION3
);
3832 ok(ret
, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3834 ret
= IsValidAcl(pAcl
);
3835 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3837 SetLastError(0xdeadbeef);
3838 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION4
);
3839 if (GetLastError() != ERROR_INVALID_PARAMETER
)
3841 ok(ret
, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3843 ret
= IsValidAcl(pAcl
);
3844 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3847 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3849 SetLastError(0xdeadbeef);
3850 ret
= InitializeAcl(pAcl
, sizeof(buffer
), -1);
3851 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3854 static void test_GetSecurityInfo(void)
3856 char b
[sizeof(TOKEN_USER
) + sizeof(SID
) + sizeof(DWORD
)*SID_MAX_SUB_AUTHORITIES
];
3857 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], dacl
[100];
3858 DWORD sid_size
= sizeof(admin_ptr
), l
= sizeof(b
);
3859 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
3860 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3861 ACL_SIZE_INFORMATION acl_size
;
3862 PSECURITY_DESCRIPTOR pSD
;
3863 ACCESS_ALLOWED_ACE
*ace
;
3870 if (!pGetSecurityInfo
|| !pSetSecurityInfo
)
3872 win_skip("[Get|Set]SecurityInfo is not available\n");
3876 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3878 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3879 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3883 win_skip("Failed to get current user token\n");
3886 GetTokenInformation(token
, TokenUser
, b
, l
, &l
);
3887 CloseHandle( token
);
3888 user_sid
= ((TOKEN_USER
*)b
)->User
.Sid
;
3890 /* Create something. Files have lots of associated security info. */
3891 obj
= CreateFileA(myARGV
[0], GENERIC_READ
|WRITE_DAC
, FILE_SHARE_READ
, NULL
,
3892 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3893 if (obj
== INVALID_HANDLE_VALUE
)
3895 skip("Couldn't create an object for GetSecurityInfo test\n");
3899 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3900 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3901 &owner
, &group
, &pDacl
, NULL
, &pSD
);
3902 if (ret
== ERROR_CALL_NOT_IMPLEMENTED
)
3904 win_skip("GetSecurityInfo is not implemented\n");
3908 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3909 ok(pSD
!= NULL
, "GetSecurityInfo\n");
3910 ok(owner
!= NULL
, "GetSecurityInfo\n");
3911 ok(group
!= NULL
, "GetSecurityInfo\n");
3913 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
3915 win_skip("No ACL information returned\n");
3919 if (!pCreateWellKnownSid
)
3921 win_skip("NULL parameter test would crash on NT4\n");
3926 /* If we don't ask for the security descriptor, Windows will still give us
3927 the other stuff, leaving us no way to free it. */
3928 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3929 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3930 &owner
, &group
, &pDacl
, NULL
, NULL
);
3931 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3932 ok(owner
!= NULL
, "GetSecurityInfo\n");
3933 ok(group
!= NULL
, "GetSecurityInfo\n");
3935 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
3937 win_skip("No ACL information returned\n");
3939 /* Create security descriptor information and test that it comes back the same */
3941 pDacl
= (PACL
)&dacl
;
3942 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3943 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3944 bret
= InitializeAcl(pDacl
, sizeof(dacl
), ACL_REVISION
);
3945 ok(bret
, "Failed to initialize ACL.\n");
3946 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3947 ok(bret
, "Failed to add Current User to ACL.\n");
3948 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
3949 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3950 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3951 ok(bret
, "Failed to add ACL to security desciptor.\n");
3952 ret
= pSetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3953 NULL
, NULL
, pDacl
, NULL
);
3954 ok(ret
== ERROR_SUCCESS
, "SetSecurityInfo returned %d\n", ret
);
3955 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3956 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3957 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3958 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityInfo returned invalid DACL.\n");
3959 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3960 ok(bret
, "GetAclInformation failed\n");
3961 if (acl_size
.AceCount
> 0)
3963 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3964 ok(bret
, "Failed to get Current User ACE.\n");
3965 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3966 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
3967 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3968 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3969 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3972 if (acl_size
.AceCount
> 1)
3974 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3975 ok(bret
, "Failed to get Administators Group ACE.\n");
3976 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3977 todo_wine
ok(bret
, "Administators Group ACE != Administators Group SID.\n");
3978 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3979 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3980 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3987 static void test_GetSidSubAuthority(void)
3991 if (!pGetSidSubAuthority
|| !pConvertStringSidToSidA
|| !pIsValidSid
|| !pGetSidSubAuthorityCount
)
3993 win_skip("Some functions not available\n");
3996 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
3997 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
3998 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid
),"ConvertStringSidToSidA failed\n");
3999 ok(pIsValidSid(psid
),"Sid is not valid\n");
4000 SetLastError(0xbebecaca);
4001 ok(*pGetSidSubAuthorityCount(psid
) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid
));
4002 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4003 SetLastError(0xbebecaca);
4004 ok(*pGetSidSubAuthority(psid
,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid
,0));
4005 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4006 SetLastError(0xbebecaca);
4007 ok(*pGetSidSubAuthority(psid
,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid
,1));
4008 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4009 SetLastError(0xbebecaca);
4010 ok(pGetSidSubAuthority(psid
,4) != NULL
,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
4011 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4015 static void test_CheckTokenMembership(void)
4017 PTOKEN_GROUPS token_groups
;
4019 HANDLE process_token
, token
;
4024 if (!pCheckTokenMembership
)
4026 win_skip("CheckTokenMembership is not available\n");
4029 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4030 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
4032 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
4033 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
4036 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4037 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4038 "GetTokenInformation(TokenGroups) %s with error %d\n",
4039 ret
? "succeeded" : "failed", GetLastError());
4040 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4041 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4042 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
4044 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4046 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4050 if (i
== token_groups
->GroupCount
)
4052 HeapFree(GetProcessHeap(), 0, token_groups
);
4054 skip("user not a member of any group\n");
4059 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4060 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4061 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4064 ret
= pCheckTokenMembership(NULL
, token_groups
->Groups
[i
].Sid
, &is_member
);
4065 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4066 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4069 SetLastError(0xdeadbeef);
4070 ret
= pCheckTokenMembership(process_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4071 ok(!ret
&& GetLastError() == ERROR_NO_IMPERSONATION_TOKEN
,
4072 "CheckTokenMembership with process token %s with error %d\n",
4073 ret
? "succeeded" : "failed", GetLastError());
4074 ok(!is_member
, "CheckTokenMembership should have cleared is_member\n");
4076 HeapFree(GetProcessHeap(), 0, token_groups
);
4078 CloseHandle(process_token
);
4081 static void test_EqualSid(void)
4085 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
4086 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
4088 SetLastError(0xdeadbeef);
4089 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4090 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid1
);
4091 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
4093 win_skip("AllocateAndInitializeSid is not implemented\n");
4096 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4097 ok(GetLastError() == 0xdeadbeef,
4098 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
4101 ret
= AllocateAndInitializeSid(&SIDAuthWorld
, 1, SECURITY_WORLD_RID
,
4102 0, 0, 0, 0, 0, 0, 0, &sid2
);
4103 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4105 SetLastError(0xdeadbeef);
4106 ret
= EqualSid(sid1
, sid2
);
4107 ok(!ret
, "World and domain admins sids shouldn't have been equal\n");
4108 ok(GetLastError() == ERROR_SUCCESS
||
4109 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4110 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4113 SetLastError(0xdeadbeef);
4114 sid2
= FreeSid(sid2
);
4115 ok(!sid2
, "FreeSid should have returned NULL instead of %p\n", sid2
);
4116 ok(GetLastError() == 0xdeadbeef,
4117 "FreeSid shouldn't have set last error to %d\n",
4120 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4121 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid2
);
4122 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4124 SetLastError(0xdeadbeef);
4125 ret
= EqualSid(sid1
, sid2
);
4126 ok(ret
, "Same sids should have been equal\n");
4127 ok(GetLastError() == ERROR_SUCCESS
||
4128 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4129 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4132 ((SID
*)sid2
)->Revision
= 2;
4133 SetLastError(0xdeadbeef);
4134 ret
= EqualSid(sid1
, sid2
);
4135 ok(!ret
, "EqualSid with invalid sid should have returned FALSE\n");
4136 ok(GetLastError() == ERROR_SUCCESS
||
4137 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4138 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4140 ((SID
*)sid2
)->Revision
= SID_REVISION
;
4146 static void test_GetUserNameA(void)
4148 char buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4149 DWORD required_len
, buffer_len
;
4152 /* Test crashes on Windows. */
4155 SetLastError(0xdeadbeef);
4156 GetUserNameA(NULL
, NULL
);
4159 SetLastError(0xdeadbeef);
4161 ret
= GetUserNameA(NULL
, &required_len
);
4162 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4163 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4164 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4166 SetLastError(0xdeadbeef);
4168 ret
= GetUserNameA(NULL
, &required_len
);
4169 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4170 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4171 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4173 /* Tests crashes on Windows. */
4176 SetLastError(0xdeadbeef);
4177 required_len
= UNLEN
+ 1;
4178 GetUserNameA(NULL
, &required_len
);
4180 SetLastError(0xdeadbeef);
4181 GetUserNameA(buffer
, NULL
);
4184 memset(filler
, 'x', sizeof(filler
));
4186 /* Note that GetUserNameA on XP and newer outputs the number of bytes
4187 * required for a Unicode string, which affects a test in the next block. */
4188 SetLastError(0xdeadbeef);
4189 memcpy(buffer
, filler
, sizeof(filler
));
4191 ret
= GetUserNameA(buffer
, &required_len
);
4192 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4193 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4194 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4195 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4197 SetLastError(0xdeadbeef);
4198 memcpy(buffer
, filler
, sizeof(filler
));
4199 buffer_len
= required_len
;
4200 ret
= GetUserNameA(buffer
, &buffer_len
);
4201 ok(ret
== TRUE
, "GetUserNameA returned %d, last error %u\n", ret
, GetLastError());
4202 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4203 ok(buffer_len
== required_len
||
4204 broken(buffer_len
== required_len
/ sizeof(WCHAR
)), /* XP+ */
4205 "Outputted buffer length was %u\n", buffer_len
);
4207 /* Use the reported buffer size from the last GetUserNameA call and pass
4208 * a length that is one less than the required value. */
4209 SetLastError(0xdeadbeef);
4210 memcpy(buffer
, filler
, sizeof(filler
));
4212 ret
= GetUserNameA(buffer
, &buffer_len
);
4213 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4214 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was untouched\n");
4215 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4216 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4219 static void test_GetUserNameW(void)
4221 WCHAR buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4222 DWORD required_len
, buffer_len
;
4225 /* Test crashes on Windows. */
4228 SetLastError(0xdeadbeef);
4229 GetUserNameW(NULL
, NULL
);
4232 SetLastError(0xdeadbeef);
4234 ret
= GetUserNameW(NULL
, &required_len
);
4235 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4236 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4237 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4239 SetLastError(0xdeadbeef);
4241 ret
= GetUserNameW(NULL
, &required_len
);
4242 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4243 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4244 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4246 /* Tests crash on Windows. */
4249 SetLastError(0xdeadbeef);
4250 required_len
= UNLEN
+ 1;
4251 GetUserNameW(NULL
, &required_len
);
4253 SetLastError(0xdeadbeef);
4254 GetUserNameW(buffer
, NULL
);
4257 memset(filler
, 'x', sizeof(filler
));
4259 SetLastError(0xdeadbeef);
4260 memcpy(buffer
, filler
, sizeof(filler
));
4262 ret
= GetUserNameW(buffer
, &required_len
);
4263 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4264 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4265 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4266 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4268 SetLastError(0xdeadbeef);
4269 memcpy(buffer
, filler
, sizeof(filler
));
4270 buffer_len
= required_len
;
4271 ret
= GetUserNameW(buffer
, &buffer_len
);
4272 ok(ret
== TRUE
, "GetUserNameW returned %d, last error %u\n", ret
, GetLastError());
4273 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4274 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4276 /* GetUserNameW on XP and newer writes a truncated portion of the username string to the buffer. */
4277 SetLastError(0xdeadbeef);
4278 memcpy(buffer
, filler
, sizeof(filler
));
4280 ret
= GetUserNameW(buffer
, &buffer_len
);
4281 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4282 ok(!memcmp(buffer
, filler
, sizeof(filler
)) ||
4283 broken(memcmp(buffer
, filler
, sizeof(filler
)) != 0), /* XP+ */
4284 "Output buffer was altered\n");
4285 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4286 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4289 static void test_CreateRestrictedToken(void)
4291 HANDLE process_token
, token
, r_token
;
4292 PTOKEN_GROUPS token_groups
, groups2
;
4293 SID_AND_ATTRIBUTES sattr
;
4294 SECURITY_IMPERSONATION_LEVEL level
;
4301 if (!pCreateRestrictedToken
)
4303 win_skip("CreateRestrictedToken is not available\n");
4307 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4308 ok(ret
, "got error %d\n", GetLastError());
4310 ret
= DuplicateTokenEx(process_token
, TOKEN_DUPLICATE
|TOKEN_ADJUST_GROUPS
|TOKEN_QUERY
,
4311 NULL
, SecurityImpersonation
, TokenImpersonation
, &token
);
4312 ok(ret
, "got error %d\n", GetLastError());
4315 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4316 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4317 "got %d with error %d\n", ret
, GetLastError());
4318 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4319 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4320 ok(ret
, "got error %d\n", GetLastError());
4322 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4324 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4328 if (i
== token_groups
->GroupCount
)
4330 HeapFree(GetProcessHeap(), 0, token_groups
);
4332 skip("User not a member of any group\n");
4337 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4338 ok(ret
, "got error %d\n", GetLastError());
4339 ok(is_member
, "not a member\n");
4341 /* disable a SID in new token */
4342 sattr
.Sid
= token_groups
->Groups
[i
].Sid
;
4343 sattr
.Attributes
= 0;
4345 ret
= pCreateRestrictedToken(token
, 0, 1, &sattr
, 0, NULL
, 0, NULL
, &r_token
);
4346 ok(ret
, "got error %d\n", GetLastError());
4350 /* check if a SID is enabled */
4352 ret
= pCheckTokenMembership(r_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4353 ok(ret
, "got error %d\n", GetLastError());
4354 todo_wine
ok(!is_member
, "not a member\n");
4356 ret
= GetTokenInformation(r_token
, TokenGroups
, NULL
, 0, &size
);
4357 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "got %d with error %d\n",
4358 ret
, GetLastError());
4359 groups2
= HeapAlloc(GetProcessHeap(), 0, size
);
4360 ret
= GetTokenInformation(r_token
, TokenGroups
, groups2
, size
, &size
);
4361 ok(ret
, "got error %d\n", GetLastError());
4363 for (j
= 0; j
< groups2
->GroupCount
; j
++)
4365 if (EqualSid(groups2
->Groups
[j
].Sid
, token_groups
->Groups
[i
].Sid
))
4369 todo_wine
ok(groups2
->Groups
[j
].Attributes
& SE_GROUP_USE_FOR_DENY_ONLY
,
4370 "got wrong attributes\n");
4371 todo_wine
ok((groups2
->Groups
[j
].Attributes
& SE_GROUP_ENABLED
) == 0,
4372 "got wrong attributes\n");
4374 HeapFree(GetProcessHeap(), 0, groups2
);
4376 size
= sizeof(type
);
4377 ret
= GetTokenInformation(r_token
, TokenType
, &type
, size
, &size
);
4378 ok(ret
, "got error %d\n", GetLastError());
4379 ok(type
== TokenImpersonation
, "got type %u\n", type
);
4381 size
= sizeof(level
);
4382 ret
= GetTokenInformation(r_token
, TokenImpersonationLevel
, &level
, size
, &size
);
4383 ok(ret
, "got error %d\n", GetLastError());
4384 ok(level
== SecurityImpersonation
, "got level %u\n", type
);
4387 HeapFree(GetProcessHeap(), 0, token_groups
);
4388 CloseHandle(r_token
);
4390 CloseHandle(process_token
);
4393 static void validate_default_security_descriptor(SECURITY_DESCRIPTOR
*sd
)
4395 BOOL ret
, present
, defaulted
;
4399 ret
= IsValidSecurityDescriptor(sd
);
4400 ok(ret
, "security descriptor is not valid\n");
4404 acl
= (void *)0xdeadbeef;
4405 SetLastError(0xdeadbeef);
4406 ret
= GetSecurityDescriptorDacl(sd
, &present
, &acl
, &defaulted
);
4407 ok(ret
, "GetSecurityDescriptorDacl error %d\n", GetLastError());
4409 ok(present
== 1, "acl is not present\n");
4411 ok(acl
!= (void *)0xdeadbeef && acl
!= NULL
, "acl pointer is not set\n");
4412 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4415 sid
= (void *)0xdeadbeef;
4416 SetLastError(0xdeadbeef);
4417 ret
= GetSecurityDescriptorOwner(sd
, &sid
, &defaulted
);
4418 ok(ret
, "GetSecurityDescriptorOwner error %d\n", GetLastError());
4420 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4421 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4424 sid
= (void *)0xdeadbeef;
4425 SetLastError(0xdeadbeef);
4426 ret
= GetSecurityDescriptorGroup(sd
, &sid
, &defaulted
);
4427 ok(ret
, "GetSecurityDescriptorGroup error %d\n", GetLastError());
4429 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4430 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4433 static void test_default_handle_security(HANDLE token
, HANDLE handle
, GENERIC_MAPPING
*mapping
)
4435 DWORD ret
, length
, needed
, granted
, priv_set_len
;
4437 PRIVILEGE_SET priv_set
;
4438 SECURITY_DESCRIPTOR
*sd
;
4440 needed
= 0xdeadbeef;
4441 SetLastError(0xdeadbeef);
4442 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4444 ok(!ret
, "GetKernelObjectSecurity should fail\n");
4445 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4446 ok(needed
!= 0xdeadbeef, "GetKernelObjectSecurity should return required buffer length\n");
4449 sd
= HeapAlloc(GetProcessHeap(), 0, length
);
4451 needed
= 0xdeadbeef;
4452 SetLastError(0xdeadbeef);
4453 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4454 sd
, length
, &needed
);
4455 ok(ret
, "GetKernelObjectSecurity error %d\n", GetLastError());
4456 ok(needed
== length
|| needed
== 0 /* file, pipe */, "GetKernelObjectSecurity should return %u instead of %u\n", length
, needed
);
4458 validate_default_security_descriptor(sd
);
4460 priv_set_len
= sizeof(priv_set
);
4461 granted
= 0xdeadbeef;
4462 status
= 0xdeadbeef;
4463 SetLastError(0xdeadbeef);
4464 ret
= AccessCheck(sd
, token
, MAXIMUM_ALLOWED
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4466 ok(ret
, "AccessCheck error %d\n", GetLastError());
4467 ok(status
== 1, "expected 1, got %d\n", status
);
4468 ok(granted
== mapping
->GenericAll
, "expected all access %#x, got %#x\n", mapping
->GenericAll
, granted
);
4470 priv_set_len
= sizeof(priv_set
);
4471 granted
= 0xdeadbeef;
4472 status
= 0xdeadbeef;
4473 SetLastError(0xdeadbeef);
4474 ret
= AccessCheck(sd
, token
, 0, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4476 ok(ret
, "AccessCheck error %d\n", GetLastError());
4477 ok(status
== 0 || broken(status
== 1) /* NT4 */, "expected 0, got %d\n", status
);
4478 ok(granted
== 0 || broken(granted
== mapping
->GenericRead
) /* NT4 */, "expected 0, got %#x\n", granted
);
4480 priv_set_len
= sizeof(priv_set
);
4481 granted
= 0xdeadbeef;
4482 status
= 0xdeadbeef;
4483 SetLastError(0xdeadbeef);
4484 ret
= AccessCheck(sd
, token
, ACCESS_SYSTEM_SECURITY
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4486 ok(ret
, "AccessCheck error %d\n", GetLastError());
4487 ok(status
== 0, "expected 0, got %d\n", status
);
4488 ok(granted
== 0, "expected 0, got %#x\n", granted
);
4490 priv_set_len
= sizeof(priv_set
);
4491 granted
= 0xdeadbeef;
4492 status
= 0xdeadbeef;
4493 SetLastError(0xdeadbeef);
4494 ret
= AccessCheck(sd
, token
, mapping
->GenericRead
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4496 ok(ret
, "AccessCheck error %d\n", GetLastError());
4497 ok(status
== 1, "expected 1, got %d\n", status
);
4498 ok(granted
== mapping
->GenericRead
, "expected read access %#x, got %#x\n", mapping
->GenericRead
, granted
);
4500 priv_set_len
= sizeof(priv_set
);
4501 granted
= 0xdeadbeef;
4502 status
= 0xdeadbeef;
4503 SetLastError(0xdeadbeef);
4504 ret
= AccessCheck(sd
, token
, mapping
->GenericWrite
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4506 ok(ret
, "AccessCheck error %d\n", GetLastError());
4507 ok(status
== 1, "expected 1, got %d\n", status
);
4508 ok(granted
== mapping
->GenericWrite
, "expected write access %#x, got %#x\n", mapping
->GenericWrite
, granted
);
4510 priv_set_len
= sizeof(priv_set
);
4511 granted
= 0xdeadbeef;
4512 status
= 0xdeadbeef;
4513 SetLastError(0xdeadbeef);
4514 ret
= AccessCheck(sd
, token
, mapping
->GenericExecute
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4516 ok(ret
, "AccessCheck error %d\n", GetLastError());
4517 ok(status
== 1, "expected 1, got %d\n", status
);
4518 ok(granted
== mapping
->GenericExecute
, "expected execute access %#x, got %#x\n", mapping
->GenericExecute
, granted
);
4520 HeapFree(GetProcessHeap(), 0, sd
);
4523 static ACCESS_MASK
get_obj_access(HANDLE obj
)
4525 OBJECT_BASIC_INFORMATION info
;
4528 if (!pNtQueryObject
) return 0;
4530 status
= pNtQueryObject(obj
, ObjectBasicInformation
, &info
, sizeof(info
), NULL
);
4531 ok(!status
, "NtQueryObject error %#x\n", status
);
4533 return info
.GrantedAccess
;
4536 static void test_mutex_security(HANDLE token
)
4538 DWORD ret
, i
, access
;
4540 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
| SYNCHRONIZE
,
4541 STANDARD_RIGHTS_WRITE
| MUTEX_MODIFY_STATE
| SYNCHRONIZE
,
4542 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4543 STANDARD_RIGHTS_ALL
| MUTEX_ALL_ACCESS
};
4546 int generic
, mapped
;
4550 { GENERIC_READ
, STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
},
4551 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
},
4552 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4553 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| MUTANT_QUERY_STATE
}
4556 SetLastError(0xdeadbeef);
4557 mutex
= OpenMutexA(0, FALSE
, "WineTestMutex");
4558 ok(!mutex
, "mutex should not exist\n");
4559 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4561 SetLastError(0xdeadbeef);
4562 mutex
= CreateMutexA(NULL
, FALSE
, "WineTestMutex");
4563 ok(mutex
!= 0, "CreateMutex error %d\n", GetLastError());
4565 access
= get_obj_access(mutex
);
4566 ok(access
== MUTANT_ALL_ACCESS
, "expected MUTANT_ALL_ACCESS, got %#x\n", access
);
4568 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4570 SetLastError( 0xdeadbeef );
4571 ret
= DuplicateHandle(GetCurrentProcess(), mutex
, GetCurrentProcess(), &dup
,
4572 map
[i
].generic
, FALSE
, 0);
4573 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4575 access
= get_obj_access(dup
);
4576 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4580 SetLastError(0xdeadbeef);
4581 dup
= OpenMutexA(0, FALSE
, "WineTestMutex");
4583 ok(!dup
, "OpenMutex should fail\n");
4585 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
4588 test_default_handle_security(token
, mutex
, &mapping
);
4590 CloseHandle (mutex
);
4593 static void test_event_security(HANDLE token
)
4595 DWORD ret
, i
, access
;
4597 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
| SYNCHRONIZE
,
4598 STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
| SYNCHRONIZE
,
4599 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4600 STANDARD_RIGHTS_ALL
| EVENT_ALL_ACCESS
};
4603 int generic
, mapped
;
4607 { GENERIC_READ
, STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
},
4608 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
},
4609 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4610 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| EVENT_QUERY_STATE
| EVENT_MODIFY_STATE
}
4613 SetLastError(0xdeadbeef);
4614 event
= OpenEventA(0, FALSE
, "WineTestEvent");
4615 ok(!event
, "event should not exist\n");
4616 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4618 SetLastError(0xdeadbeef);
4619 event
= CreateEventA(NULL
, FALSE
, FALSE
, "WineTestEvent");
4620 ok(event
!= 0, "CreateEvent error %d\n", GetLastError());
4622 access
= get_obj_access(event
);
4623 ok(access
== EVENT_ALL_ACCESS
, "expected EVENT_ALL_ACCESS, got %#x\n", access
);
4625 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4627 SetLastError( 0xdeadbeef );
4628 ret
= DuplicateHandle(GetCurrentProcess(), event
, GetCurrentProcess(), &dup
,
4629 map
[i
].generic
, FALSE
, 0);
4630 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4632 access
= get_obj_access(dup
);
4633 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4637 SetLastError(0xdeadbeef);
4638 dup
= OpenEventA(0, FALSE
, "WineTestEvent");
4640 ok(!dup
, "OpenEvent should fail\n");
4642 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
4645 test_default_handle_security(token
, event
, &mapping
);
4650 static void test_semaphore_security(HANDLE token
)
4652 DWORD ret
, i
, access
;
4654 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
,
4655 STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
,
4656 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4657 STANDARD_RIGHTS_ALL
| SEMAPHORE_ALL_ACCESS
};
4660 int generic
, mapped
;
4664 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
},
4665 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
},
4666 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4667 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| SEMAPHORE_QUERY_STATE
| SEMAPHORE_MODIFY_STATE
}
4670 SetLastError(0xdeadbeef);
4671 sem
= OpenSemaphoreA(0, FALSE
, "WineTestSemaphore");
4672 ok(!sem
, "semaphore should not exist\n");
4673 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4675 SetLastError(0xdeadbeef);
4676 sem
= CreateSemaphoreA(NULL
, 0, 10, "WineTestSemaphore");
4677 ok(sem
!= 0, "CreateSemaphore error %d\n", GetLastError());
4679 access
= get_obj_access(sem
);
4680 ok(access
== SEMAPHORE_ALL_ACCESS
, "expected SEMAPHORE_ALL_ACCESS, got %#x\n", access
);
4682 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4684 SetLastError( 0xdeadbeef );
4685 ret
= DuplicateHandle(GetCurrentProcess(), sem
, GetCurrentProcess(), &dup
,
4686 map
[i
].generic
, FALSE
, 0);
4687 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4689 access
= get_obj_access(dup
);
4690 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4695 test_default_handle_security(token
, sem
, &mapping
);
4700 #define WINE_TEST_PIPE "\\\\.\\pipe\\WineTestPipe"
4701 static void test_named_pipe_security(HANDLE token
)
4703 DWORD ret
, i
, access
;
4704 HANDLE pipe
, file
, dup
;
4705 GENERIC_MAPPING mapping
= { FILE_GENERIC_READ
,
4707 FILE_GENERIC_EXECUTE
,
4708 STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
};
4711 int todo
, generic
, mapped
;
4715 { 1, GENERIC_READ
, FILE_GENERIC_READ
},
4716 { 1, GENERIC_WRITE
, FILE_GENERIC_WRITE
},
4717 { 1, GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
4718 { 1, GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
4724 } creation_access
[] =
4726 { PIPE_ACCESS_INBOUND
, FILE_GENERIC_READ
},
4727 { PIPE_ACCESS_OUTBOUND
, FILE_GENERIC_WRITE
},
4728 { PIPE_ACCESS_DUPLEX
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
},
4729 { PIPE_ACCESS_INBOUND
|WRITE_DAC
, FILE_GENERIC_READ
|WRITE_DAC
},
4730 { PIPE_ACCESS_INBOUND
|WRITE_OWNER
, FILE_GENERIC_READ
|WRITE_OWNER
}
4731 /* ACCESS_SYSTEM_SECURITY is also valid, but will fail with ERROR_PRIVILEGE_NOT_HELD */
4734 /* Test the different security access options for pipes */
4735 for (i
= 0; i
< sizeof(creation_access
)/sizeof(creation_access
[0]); i
++)
4737 SetLastError(0xdeadbeef);
4738 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, creation_access
[i
].open_mode
,
4739 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
, 0, 0,
4740 NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
4741 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe(0x%x) error %d\n",
4742 creation_access
[i
].open_mode
, GetLastError());
4743 access
= get_obj_access(pipe
);
4744 ok(access
== creation_access
[i
].access
,
4745 "CreateNamedPipeA(0x%x) pipe expected access 0x%x (got 0x%x)\n",
4746 creation_access
[i
].open_mode
, creation_access
[i
].access
, access
);
4750 SetLastError(0xdeadbeef);
4751 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_FIRST_PIPE_INSTANCE
,
4752 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
,
4753 0, 0, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
4754 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe error %d\n", GetLastError());
4756 test_default_handle_security(token
, pipe
, &mapping
);
4758 SetLastError(0xdeadbeef);
4759 file
= CreateFileA(WINE_TEST_PIPE
, FILE_ALL_ACCESS
, 0, NULL
, OPEN_EXISTING
, 0, 0);
4760 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4762 access
= get_obj_access(file
);
4763 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4765 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4767 SetLastError( 0xdeadbeef );
4768 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4769 map
[i
].generic
, FALSE
, 0);
4770 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4772 access
= get_obj_access(dup
);
4773 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4781 SetLastError(0xdeadbeef);
4782 file
= CreateFileA("\\\\.\\pipe\\", FILE_ALL_ACCESS
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0);
4783 ok(file
!= INVALID_HANDLE_VALUE
|| broken(file
== INVALID_HANDLE_VALUE
) /* before Vista */, "CreateFile error %d\n", GetLastError());
4785 if (file
!= INVALID_HANDLE_VALUE
)
4787 access
= get_obj_access(file
);
4788 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4790 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4792 SetLastError( 0xdeadbeef );
4793 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4794 map
[i
].generic
, FALSE
, 0);
4795 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4797 access
= get_obj_access(dup
);
4800 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4802 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4811 static void test_file_security(HANDLE token
)
4813 DWORD ret
, i
, access
, bytes
;
4817 int generic
, mapped
;
4821 { GENERIC_READ
, FILE_GENERIC_READ
},
4822 { GENERIC_WRITE
, FILE_GENERIC_WRITE
},
4823 { GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
4824 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
4826 char temp_path
[MAX_PATH
];
4827 char file_name
[MAX_PATH
];
4830 GetTempPathA(MAX_PATH
, temp_path
);
4831 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
4834 SetLastError(0xdeadbeef);
4835 file
= CreateFileA(file_name
, GENERIC_ALL
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
4836 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4838 access
= get_obj_access(file
);
4839 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4841 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4843 SetLastError( 0xdeadbeef );
4844 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4845 map
[i
].generic
, FALSE
, 0);
4846 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4848 access
= get_obj_access(dup
);
4849 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4856 SetLastError(0xdeadbeef);
4857 file
= CreateFileA(file_name
, 0, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
4858 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4860 access
= get_obj_access(file
);
4862 ok(access
== (FILE_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
4865 SetLastError(0xdeadbeef);
4866 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
4867 ok(!ret
, "ReadFile should fail\n");
4868 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4869 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
4873 SetLastError(0xdeadbeef);
4874 file
= CreateFileA(file_name
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
4875 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4877 access
= get_obj_access(file
);
4879 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
4882 SetLastError(0xdeadbeef);
4883 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
4884 ok(!ret
, "ReadFile should fail\n");
4885 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4886 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
4889 DeleteFileA(file_name
);
4892 SetLastError(0xdeadbeef);
4893 file
= CreateFileA(temp_path
, GENERIC_ALL
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
4894 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4896 access
= get_obj_access(file
);
4897 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4899 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4901 SetLastError( 0xdeadbeef );
4902 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4903 map
[i
].generic
, FALSE
, 0);
4904 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4906 access
= get_obj_access(dup
);
4907 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4914 SetLastError(0xdeadbeef);
4915 file
= CreateFileA(temp_path
, 0, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
4916 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4918 access
= get_obj_access(file
);
4920 ok(access
== (FILE_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
4924 SetLastError(0xdeadbeef);
4925 file
= CreateFileA(temp_path
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
4926 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4928 access
= get_obj_access(file
);
4930 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
4935 static void test_filemap_security(void)
4937 char temp_path
[MAX_PATH
];
4938 char file_name
[MAX_PATH
];
4939 DWORD ret
, i
, access
;
4940 HANDLE file
, mapping
, dup
;
4943 int generic
, mapped
;
4947 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SECTION_QUERY
| SECTION_MAP_READ
},
4948 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SECTION_MAP_WRITE
},
4949 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SECTION_MAP_EXECUTE
},
4950 { GENERIC_ALL
, STANDARD_RIGHTS_REQUIRED
| SECTION_ALL_ACCESS
}
4958 { PAGE_NOACCESS
, 0 },
4959 { PAGE_READONLY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
4960 { PAGE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
},
4961 { PAGE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
4962 { PAGE_EXECUTE
, 0 },
4963 { PAGE_EXECUTE_READ
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
},
4964 { PAGE_EXECUTE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
},
4965 { PAGE_EXECUTE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
}
4968 GetTempPathA(MAX_PATH
, temp_path
);
4969 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
4971 SetLastError(0xdeadbeef);
4972 file
= CreateFileA(file_name
, GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
4973 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4974 SetFilePointer(file
, 4096, NULL
, FILE_BEGIN
);
4977 for (i
= 0; i
< sizeof(prot_map
)/sizeof(prot_map
[0]); i
++)
4979 SetLastError(0xdeadbeef);
4980 mapping
= CreateFileMappingW(file
, NULL
, prot_map
[i
].prot
, 0, 4096, NULL
);
4981 if (prot_map
[i
].mapped
)
4985 /* NT4 and win2k don't support EXEC on file mappings */
4986 if (prot_map
[i
].prot
== PAGE_EXECUTE_READ
|| prot_map
[i
].prot
== PAGE_EXECUTE_READWRITE
|| prot_map
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
4988 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
4992 ok(mapping
!= 0, "CreateFileMapping(%04x) error %d\n", prot_map
[i
].prot
, GetLastError());
4996 ok(!mapping
, "CreateFileMapping(%04x) should fail\n", prot_map
[i
].prot
);
4997 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
5001 access
= get_obj_access(mapping
);
5002 ok(access
== prot_map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, prot_map
[i
].mapped
, access
);
5004 CloseHandle(mapping
);
5007 SetLastError(0xdeadbeef);
5008 mapping
= CreateFileMappingW(file
, NULL
, PAGE_EXECUTE_READWRITE
, 0, 4096, NULL
);
5011 /* NT4 and win2k don't support EXEC on file mappings */
5012 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
5014 DeleteFileA(file_name
);
5017 ok(mapping
!= 0, "CreateFileMapping error %d\n", GetLastError());
5019 access
= get_obj_access(mapping
);
5020 ok(access
== (STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
),
5021 "expected STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE, got %#x\n", access
);
5023 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5025 SetLastError( 0xdeadbeef );
5026 ret
= DuplicateHandle(GetCurrentProcess(), mapping
, GetCurrentProcess(), &dup
,
5027 map
[i
].generic
, FALSE
, 0);
5028 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5030 access
= get_obj_access(dup
);
5031 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5036 CloseHandle(mapping
);
5038 DeleteFileA(file_name
);
5041 static void test_thread_security(void)
5043 DWORD ret
, i
, access
;
5047 int generic
, mapped
;
5051 { GENERIC_READ
, STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
},
5052 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
| THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
| 0x4 },
5053 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5054 { GENERIC_ALL
, THREAD_ALL_ACCESS_NT4
}
5057 SetLastError(0xdeadbeef);
5058 thread
= CreateThread(NULL
, 0, (void *)0xdeadbeef, NULL
, CREATE_SUSPENDED
, &ret
);
5059 ok(thread
!= 0, "CreateThread error %d\n", GetLastError());
5061 access
= get_obj_access(thread
);
5062 ok(access
== THREAD_ALL_ACCESS_NT4
|| access
== THREAD_ALL_ACCESS_VISTA
, "expected THREAD_ALL_ACCESS, got %#x\n", access
);
5064 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5066 SetLastError( 0xdeadbeef );
5067 ret
= DuplicateHandle(GetCurrentProcess(), thread
, GetCurrentProcess(), &dup
,
5068 map
[i
].generic
, FALSE
, 0);
5069 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5071 access
= get_obj_access(dup
);
5072 switch (map
[i
].generic
)
5075 case GENERIC_EXECUTE
:
5076 ok(access
== map
[i
].mapped
||
5077 access
== (map
[i
].mapped
| THREAD_QUERY_LIMITED_INFORMATION
) /* Vista+ */ ||
5078 access
== (map
[i
].mapped
| THREAD_QUERY_LIMITED_INFORMATION
| THREAD_RESUME
) /* win8 */,
5079 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5083 ok(access
== map
[i
].mapped
||
5084 access
== (map
[i
].mapped
| THREAD_SET_LIMITED_INFORMATION
) /* Vista+ */ ||
5085 access
== (map
[i
].mapped
| THREAD_SET_LIMITED_INFORMATION
| THREAD_RESUME
) /* win8 */,
5086 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5089 ok(access
== map
[i
].mapped
|| access
== THREAD_ALL_ACCESS_VISTA
,
5090 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5093 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5100 TerminateThread(thread
, 0);
5101 CloseHandle(thread
);
5104 static void test_process_access(void)
5106 DWORD ret
, i
, access
;
5107 HANDLE process
, dup
;
5109 PROCESS_INFORMATION pi
;
5110 char cmdline
[] = "winver.exe";
5113 int generic
, mapped
;
5117 { GENERIC_READ
, STANDARD_RIGHTS_READ
| PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
},
5118 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| PROCESS_SET_QUOTA
| PROCESS_SET_INFORMATION
| PROCESS_SUSPEND_RESUME
|
5119 PROCESS_VM_WRITE
| PROCESS_DUP_HANDLE
| PROCESS_CREATE_PROCESS
| PROCESS_CREATE_THREAD
| PROCESS_VM_OPERATION
},
5120 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5121 { GENERIC_ALL
, PROCESS_ALL_ACCESS_NT4
}
5124 memset(&sti
, 0, sizeof(sti
));
5125 sti
.cb
= sizeof(sti
);
5126 SetLastError(0xdeadbeef);
5127 ret
= CreateProcessA(NULL
, cmdline
, NULL
, NULL
, FALSE
, CREATE_SUSPENDED
, NULL
, NULL
, &sti
, &pi
);
5128 ok(ret
, "CreateProcess() error %d\n", GetLastError());
5130 CloseHandle(pi
.hThread
);
5131 process
= pi
.hProcess
;
5133 access
= get_obj_access(process
);
5134 ok(access
== PROCESS_ALL_ACCESS_NT4
|| access
== PROCESS_ALL_ACCESS_VISTA
, "expected PROCESS_ALL_ACCESS, got %#x\n", access
);
5136 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5138 SetLastError( 0xdeadbeef );
5139 ret
= DuplicateHandle(GetCurrentProcess(), process
, GetCurrentProcess(), &dup
,
5140 map
[i
].generic
, FALSE
, 0);
5141 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5143 access
= get_obj_access(dup
);
5144 switch (map
[i
].generic
)
5147 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
) /* Vista+ */,
5148 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5151 ok(access
== map
[i
].mapped
||
5152 access
== (map
[i
].mapped
| PROCESS_TERMINATE
) /* before Vista */ ||
5153 access
== (map
[i
].mapped
| PROCESS_SET_LIMITED_INFORMATION
) /* win8 */,
5154 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5156 case GENERIC_EXECUTE
:
5157 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
) /* Vista+ */,
5158 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5161 ok(access
== map
[i
].mapped
|| access
== PROCESS_ALL_ACCESS_VISTA
,
5162 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5165 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5172 TerminateProcess(process
, 0);
5173 CloseHandle(process
);
5176 static BOOL
validate_impersonation_token(HANDLE token
, DWORD
*token_type
)
5180 SECURITY_IMPERSONATION_LEVEL sil
;
5184 SetLastError(0xdeadbeef);
5185 ret
= GetTokenInformation(token
, TokenType
, &type
, sizeof(type
), &needed
);
5186 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5187 ok(needed
== sizeof(type
), "GetTokenInformation should return required buffer length\n");
5188 ok(type
== TokenPrimary
|| type
== TokenImpersonation
, "expected TokenPrimary or TokenImpersonation, got %d\n", type
);
5191 if (type
!= TokenImpersonation
) return FALSE
;
5194 SetLastError(0xdeadbeef);
5195 ret
= GetTokenInformation(token
, TokenImpersonationLevel
, &sil
, sizeof(sil
), &needed
);
5196 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5197 ok(needed
== sizeof(sil
), "GetTokenInformation should return required buffer length\n");
5198 ok(sil
== SecurityImpersonation
, "expected SecurityImpersonation, got %d\n", sil
);
5200 needed
= 0xdeadbeef;
5201 SetLastError(0xdeadbeef);
5202 ret
= GetTokenInformation(token
, TokenDefaultDacl
, NULL
, 0, &needed
);
5203 ok(!ret
, "GetTokenInformation should fail\n");
5204 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5205 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5206 ok(needed
> sizeof(TOKEN_DEFAULT_DACL
), "GetTokenInformation returned empty default DACL\n");
5208 needed
= 0xdeadbeef;
5209 SetLastError(0xdeadbeef);
5210 ret
= GetTokenInformation(token
, TokenOwner
, NULL
, 0, &needed
);
5211 ok(!ret
, "GetTokenInformation should fail\n");
5212 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5213 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5214 ok(needed
> sizeof(TOKEN_OWNER
), "GetTokenInformation returned empty token owner\n");
5216 needed
= 0xdeadbeef;
5217 SetLastError(0xdeadbeef);
5218 ret
= GetTokenInformation(token
, TokenPrimaryGroup
, NULL
, 0, &needed
);
5219 ok(!ret
, "GetTokenInformation should fail\n");
5220 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5221 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5222 ok(needed
> sizeof(TOKEN_PRIMARY_GROUP
), "GetTokenInformation returned empty token primary group\n");
5227 static void test_kernel_objects_security(void)
5229 HANDLE token
, process_token
;
5230 DWORD ret
, token_type
;
5232 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
| TOKEN_QUERY
, &process_token
);
5233 ok(ret
, "OpenProcessToken error %d\n", GetLastError());
5235 ret
= validate_impersonation_token(process_token
, &token_type
);
5236 ok(token_type
== TokenPrimary
, "expected TokenPrimary, got %d\n", token_type
);
5237 ok(!ret
, "access token should not be an impersonation token\n");
5239 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
5240 ok(ret
, "DuplicateToken error %d\n", GetLastError());
5242 ret
= validate_impersonation_token(token
, &token_type
);
5243 ok(ret
, "access token should be a valid impersonation token\n");
5244 ok(token_type
== TokenImpersonation
, "expected TokenImpersonation, got %d\n", token_type
);
5246 test_mutex_security(token
);
5247 test_event_security(token
);
5248 test_named_pipe_security(token
);
5249 test_semaphore_security(token
);
5250 test_file_security(token
);
5251 test_filemap_security();
5252 test_thread_security();
5253 test_process_access();
5254 /* FIXME: test other kernel object types */
5256 CloseHandle(process_token
);
5260 static void test_TokenIntegrityLevel(void)
5262 TOKEN_MANDATORY_LABEL
*tml
;
5263 BYTE buffer
[64]; /* using max. 28 byte in win7 x64 */
5267 char *sidname
= NULL
;
5268 static SID medium_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5269 {SECURITY_MANDATORY_HIGH_RID
}};
5270 static SID high_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5271 {SECURITY_MANDATORY_MEDIUM_RID
}};
5273 SetLastError(0xdeadbeef);
5274 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &token
);
5275 ok(res
, "got %d with %d (expected TRUE)\n", res
, GetLastError());
5277 SetLastError(0xdeadbeef);
5278 res
= GetTokenInformation(token
, TokenIntegrityLevel
, buffer
, sizeof(buffer
), &size
);
5280 /* not supported before Vista */
5281 if (!res
&& ((GetLastError() == ERROR_INVALID_PARAMETER
) || GetLastError() == ERROR_INVALID_FUNCTION
))
5283 win_skip("TokenIntegrityLevel not supported\n");
5288 ok(res
, "got %u with %u (expected TRUE)\n", res
, GetLastError());
5295 tml
= (TOKEN_MANDATORY_LABEL
*) buffer
;
5296 ok(tml
->Label
.Attributes
== (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
),
5297 "got 0x%x (expected 0x%x)\n", tml
->Label
.Attributes
, (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
));
5299 SetLastError(0xdeadbeef);
5300 res
= pConvertSidToStringSidA(tml
->Label
.Sid
, &sidname
);
5301 ok(res
, "got %u and %u\n", res
, GetLastError());
5303 ok(EqualSid(tml
->Label
.Sid
, &medium_level
) || EqualSid(tml
->Label
.Sid
, &high_level
),
5304 "got %s (expected 'S-1-16-8192' or 'S-1-16-12288')\n", sidname
);
5310 static void test_default_dacl_owner_sid(void)
5313 BOOL ret
, defaulted
, present
, found
;
5315 SECURITY_DESCRIPTOR
*sd
;
5316 SECURITY_ATTRIBUTES sa
;
5319 ACCESS_ALLOWED_ACE
*ace
;
5321 sd
= HeapAlloc( GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
5322 ret
= InitializeSecurityDescriptor( sd
, SECURITY_DESCRIPTOR_REVISION
);
5323 ok( ret
, "error %u\n", GetLastError() );
5325 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
5326 sa
.lpSecurityDescriptor
= sd
;
5327 sa
.bInheritHandle
= FALSE
;
5328 handle
= CreateEventA( &sa
, TRUE
, TRUE
, "test_event" );
5329 ok( handle
!= NULL
, "error %u\n", GetLastError() );
5332 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, NULL
, 0, &size
);
5333 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "error %u\n", GetLastError() );
5335 sd
= HeapAlloc( GetProcessHeap(), 0, size
);
5336 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, sd
, size
, &size
);
5337 ok( ret
, "error %u\n", GetLastError() );
5339 owner
= (void *)0xdeadbeef;
5341 ret
= GetSecurityDescriptorOwner( sd
, &owner
, &defaulted
);
5342 ok( ret
, "error %u\n", GetLastError() );
5343 ok( owner
!= (void *)0xdeadbeef, "owner not set\n" );
5344 ok( !defaulted
, "owner defaulted\n" );
5346 dacl
= (void *)0xdeadbeef;
5349 ret
= GetSecurityDescriptorDacl( sd
, &present
, &dacl
, &defaulted
);
5350 ok( ret
, "error %u\n", GetLastError() );
5351 ok( present
, "dacl not present\n" );
5352 ok( dacl
!= (void *)0xdeadbeef, "dacl not set\n" );
5353 ok( !defaulted
, "dacl defaulted\n" );
5357 while (pGetAce( dacl
, index
++, (void **)&ace
))
5359 if (EqualSid( &ace
->SidStart
, owner
)) found
= TRUE
;
5361 ok( found
, "owner sid not found in dacl\n" );
5363 HeapFree( GetProcessHeap(), 0, sa
.lpSecurityDescriptor
);
5364 HeapFree( GetProcessHeap(), 0, sd
);
5365 CloseHandle( handle
);
5368 static void test_AdjustTokenPrivileges(void)
5370 TOKEN_PRIVILEGES tp
, prev
;
5376 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &token
))
5379 if (!LookupPrivilegeValueA(NULL
, SE_BACKUP_NAME
, &luid
))
5385 tp
.PrivilegeCount
= 1;
5386 tp
.Privileges
[0].Luid
= luid
;
5387 tp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
5390 ret
= AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), NULL
, &len
);
5391 ok(ret
, "got %d\n", ret
);
5392 ok(len
== 0xdeadbeef, "got length %d\n", len
);
5395 tp
.PrivilegeCount
= 1;
5396 tp
.Privileges
[0].Luid
= luid
;
5397 tp
.Privileges
[0].Attributes
= 0;
5398 AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), &prev
, NULL
);
5403 START_TEST(security
)
5410 test_process_security_child();
5413 test_kernel_objects_security();
5417 test_CreateWellKnownSid();
5418 test_FileSecurity();
5421 test_LookupAccountSid();
5422 test_LookupAccountName();
5423 test_security_descriptor();
5424 test_process_security();
5425 test_impersonation_level();
5426 test_SetEntriesInAclW();
5427 test_SetEntriesInAclA();
5428 test_CreateDirectoryA();
5429 test_GetNamedSecurityInfoA();
5430 test_ConvertStringSecurityDescriptor();
5431 test_ConvertSecurityDescriptorToString();
5432 test_PrivateObjectSecurity();
5434 test_GetSecurityInfo();
5435 test_GetSidSubAuthority();
5436 test_CheckTokenMembership();
5438 test_GetUserNameA();
5439 test_GetUserNameW();
5440 test_CreateRestrictedToken();
5441 test_TokenIntegrityLevel();
5442 test_default_dacl_owner_sid();
5443 test_AdjustTokenPrivileges();