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
37 #include "wine/test.h"
39 #ifndef PROCESS_QUERY_LIMITED_INFORMATION
40 #define PROCESS_QUERY_LIMITED_INFORMATION 0x1000
43 /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
44 #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
45 #define PROCESS_ALL_ACCESS_VISTA (PROCESS_ALL_ACCESS | 0xf000)
47 #ifndef EVENT_QUERY_STATE
48 #define EVENT_QUERY_STATE 0x0001
51 #ifndef SEMAPHORE_QUERY_STATE
52 #define SEMAPHORE_QUERY_STATE 0x0001
55 #ifndef THREAD_SET_LIMITED_INFORMATION
56 #define THREAD_SET_LIMITED_INFORMATION 0x0400
57 #define THREAD_QUERY_LIMITED_INFORMATION 0x0800
60 #define THREAD_ALL_ACCESS_NT4 (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3ff)
61 #define THREAD_ALL_ACCESS_VISTA (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xffff)
63 #define expect_eq(expr, value, type, format) { type ret_ = expr; ok((value) == ret_, #expr " expected " format " got " format "\n", (value), (ret_)); }
65 static BOOL (WINAPI
*pAddAccessAllowedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
66 static BOOL (WINAPI
*pAddAccessDeniedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
67 static BOOL (WINAPI
*pAddAuditAccessAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
, BOOL
, BOOL
);
68 static VOID (WINAPI
*pBuildTrusteeWithSidA
)( PTRUSTEEA pTrustee
, PSID pSid
);
69 static VOID (WINAPI
*pBuildTrusteeWithNameA
)( PTRUSTEEA pTrustee
, LPSTR pName
);
70 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndNameA
)( PTRUSTEEA pTrustee
,
71 POBJECTS_AND_NAME_A pObjName
,
72 SE_OBJECT_TYPE ObjectType
,
74 LPSTR InheritedObjectTypeName
,
76 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndSidA
)( PTRUSTEEA pTrustee
,
77 POBJECTS_AND_SID pObjSid
,
79 GUID
* pInheritedObjectGuid
,
81 static LPSTR (WINAPI
*pGetTrusteeNameA
)( PTRUSTEEA pTrustee
);
82 static BOOL (WINAPI
*pMakeSelfRelativeSD
)( PSECURITY_DESCRIPTOR
, PSECURITY_DESCRIPTOR
, LPDWORD
);
83 static BOOL (WINAPI
*pConvertSidToStringSidA
)( PSID pSid
, LPSTR
*str
);
84 static BOOL (WINAPI
*pConvertStringSidToSidA
)( LPCSTR str
, PSID pSid
);
85 static BOOL (WINAPI
*pCheckTokenMembership
)(HANDLE
, PSID
, PBOOL
);
86 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorA
)(LPCSTR
, DWORD
,
87 PSECURITY_DESCRIPTOR
*, PULONG
);
88 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorW
)(LPCWSTR
, DWORD
,
89 PSECURITY_DESCRIPTOR
*, PULONG
);
90 static BOOL (WINAPI
*pConvertSecurityDescriptorToStringSecurityDescriptorA
)(PSECURITY_DESCRIPTOR
, DWORD
,
91 SECURITY_INFORMATION
, LPSTR
*, PULONG
);
92 static BOOL (WINAPI
*pGetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
93 PSECURITY_DESCRIPTOR
, DWORD
, LPDWORD
);
94 static BOOL (WINAPI
*pSetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
95 PSECURITY_DESCRIPTOR
);
96 static DWORD (WINAPI
*pGetNamedSecurityInfoA
)(LPSTR
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
97 PSID
*, PSID
*, PACL
*, PACL
*,
98 PSECURITY_DESCRIPTOR
*);
99 static DWORD (WINAPI
*pSetNamedSecurityInfoA
)(LPSTR
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
100 PSID
, PSID
, PACL
, PACL
);
101 static PDWORD (WINAPI
*pGetSidSubAuthority
)(PSID
, DWORD
);
102 static PUCHAR (WINAPI
*pGetSidSubAuthorityCount
)(PSID
);
103 static BOOL (WINAPI
*pIsValidSid
)(PSID
);
104 static DWORD (WINAPI
*pRtlAdjustPrivilege
)(ULONG
,BOOLEAN
,BOOLEAN
,PBOOLEAN
);
105 static BOOL (WINAPI
*pCreateWellKnownSid
)(WELL_KNOWN_SID_TYPE
,PSID
,PSID
,DWORD
*);
106 static BOOL (WINAPI
*pDuplicateTokenEx
)(HANDLE
,DWORD
,LPSECURITY_ATTRIBUTES
,
107 SECURITY_IMPERSONATION_LEVEL
,TOKEN_TYPE
,PHANDLE
);
109 static NTSTATUS (WINAPI
*pLsaQueryInformationPolicy
)(LSA_HANDLE
,POLICY_INFORMATION_CLASS
,PVOID
*);
110 static NTSTATUS (WINAPI
*pLsaClose
)(LSA_HANDLE
);
111 static NTSTATUS (WINAPI
*pLsaFreeMemory
)(PVOID
);
112 static NTSTATUS (WINAPI
*pLsaOpenPolicy
)(PLSA_UNICODE_STRING
,PLSA_OBJECT_ATTRIBUTES
,ACCESS_MASK
,PLSA_HANDLE
);
113 static NTSTATUS (WINAPI
*pNtQueryObject
)(HANDLE
,OBJECT_INFORMATION_CLASS
,PVOID
,ULONG
,PULONG
);
114 static DWORD (WINAPI
*pSetEntriesInAclW
)(ULONG
, PEXPLICIT_ACCESSW
, PACL
, PACL
*);
115 static DWORD (WINAPI
*pSetEntriesInAclA
)(ULONG
, PEXPLICIT_ACCESSA
, PACL
, PACL
*);
116 static BOOL (WINAPI
*pSetSecurityDescriptorControl
)(PSECURITY_DESCRIPTOR
, SECURITY_DESCRIPTOR_CONTROL
,
117 SECURITY_DESCRIPTOR_CONTROL
);
118 static DWORD (WINAPI
*pGetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
119 PSID
*, PSID
*, PACL
*, PACL
*, PSECURITY_DESCRIPTOR
*);
120 static DWORD (WINAPI
*pSetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
121 PSID
, PSID
, PACL
, PACL
);
122 static NTSTATUS (WINAPI
*pNtAccessCheck
)(PSECURITY_DESCRIPTOR
, HANDLE
, ACCESS_MASK
, PGENERIC_MAPPING
,
123 PPRIVILEGE_SET
, PULONG
, PULONG
, NTSTATUS
*);
124 static BOOL (WINAPI
*pCreateRestrictedToken
)(HANDLE
, DWORD
, DWORD
, PSID_AND_ATTRIBUTES
, DWORD
,
125 PLUID_AND_ATTRIBUTES
, DWORD
, PSID_AND_ATTRIBUTES
, PHANDLE
);
126 static BOOL (WINAPI
*pGetAclInformation
)(PACL
,LPVOID
,DWORD
,ACL_INFORMATION_CLASS
);
127 static BOOL (WINAPI
*pGetAce
)(PACL
,DWORD
,LPVOID
*);
128 static NTSTATUS (WINAPI
*pNtSetSecurityObject
)(HANDLE
,SECURITY_INFORMATION
,PSECURITY_DESCRIPTOR
);
129 static NTSTATUS (WINAPI
*pNtCreateFile
)(PHANDLE
,ACCESS_MASK
,POBJECT_ATTRIBUTES
,PIO_STATUS_BLOCK
,PLARGE_INTEGER
,ULONG
,ULONG
,ULONG
,ULONG
,PVOID
,ULONG
);
130 static BOOL (WINAPI
*pRtlDosPathNameToNtPathName_U
)(LPCWSTR
,PUNICODE_STRING
,PWSTR
*,CURDIR
*);
131 static NTSTATUS (WINAPI
*pRtlAnsiStringToUnicodeString
)(PUNICODE_STRING
,PCANSI_STRING
,BOOLEAN
);
132 static BOOL (WINAPI
*pGetWindowsAccountDomainSid
)(PSID
,PSID
,DWORD
*);
133 static void (WINAPI
*pRtlInitAnsiString
)(PANSI_STRING
,PCSZ
);
134 static NTSTATUS (WINAPI
*pRtlFreeUnicodeString
)(PUNICODE_STRING
);
135 static PSID_IDENTIFIER_AUTHORITY (WINAPI
*pGetSidIdentifierAuthority
)(PSID
);
139 static char** myARGV
;
150 static char debugsid_str
[SID_SLOTS
][256];
151 static int debugsid_index
= 0;
152 static const char* debugstr_sid(PSID sid
)
155 DWORD le
= GetLastError();
156 char* res
= debugsid_str
[debugsid_index
];
157 debugsid_index
= (debugsid_index
+ 1) % SID_SLOTS
;
158 if (!pConvertSidToStringSidA
)
159 strcpy(res
, "missing ConvertSidToStringSidA");
160 else if (!pConvertSidToStringSidA(sid
, &sidstr
))
161 sprintf(res
, "ConvertSidToStringSidA failed le=%u", GetLastError());
162 else if (strlen(sidstr
) > sizeof(*debugsid_str
) - 1)
164 memcpy(res
, sidstr
, sizeof(*debugsid_str
) - 4);
165 strcpy(res
+ sizeof(*debugsid_str
) - 4, "...");
173 /* Restore the last error in case ConvertSidToStringSidA() modified it */
180 SID_IDENTIFIER_AUTHORITY auth
;
184 static void init(void)
188 hntdll
= GetModuleHandleA("ntdll.dll");
189 pNtQueryObject
= (void *)GetProcAddress( hntdll
, "NtQueryObject" );
190 pNtAccessCheck
= (void *)GetProcAddress( hntdll
, "NtAccessCheck" );
191 pNtSetSecurityObject
= (void *)GetProcAddress(hntdll
, "NtSetSecurityObject");
192 pNtCreateFile
= (void *)GetProcAddress(hntdll
, "NtCreateFile");
193 pRtlDosPathNameToNtPathName_U
= (void *)GetProcAddress(hntdll
, "RtlDosPathNameToNtPathName_U");
194 pRtlAnsiStringToUnicodeString
= (void *)GetProcAddress(hntdll
, "RtlAnsiStringToUnicodeString");
195 pRtlInitAnsiString
= (void *)GetProcAddress(hntdll
, "RtlInitAnsiString");
196 pRtlFreeUnicodeString
= (void *)GetProcAddress(hntdll
, "RtlFreeUnicodeString");
198 hmod
= GetModuleHandleA("advapi32.dll");
199 pAddAccessAllowedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessAllowedAceEx");
200 pAddAccessDeniedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessDeniedAceEx");
201 pAddAuditAccessAceEx
= (void *)GetProcAddress(hmod
, "AddAuditAccessAceEx");
202 pCheckTokenMembership
= (void *)GetProcAddress(hmod
, "CheckTokenMembership");
203 pConvertStringSecurityDescriptorToSecurityDescriptorA
=
204 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
205 pConvertStringSecurityDescriptorToSecurityDescriptorW
=
206 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
207 pConvertSecurityDescriptorToStringSecurityDescriptorA
=
208 (void *)GetProcAddress(hmod
, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
209 pGetFileSecurityA
= (void *)GetProcAddress(hmod
, "GetFileSecurityA" );
210 pSetFileSecurityA
= (void *)GetProcAddress(hmod
, "SetFileSecurityA" );
211 pCreateWellKnownSid
= (void *)GetProcAddress( hmod
, "CreateWellKnownSid" );
212 pGetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "GetNamedSecurityInfoA");
213 pSetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "SetNamedSecurityInfoA");
214 pGetSidSubAuthority
= (void *)GetProcAddress(hmod
, "GetSidSubAuthority");
215 pGetSidSubAuthorityCount
= (void *)GetProcAddress(hmod
, "GetSidSubAuthorityCount");
216 pIsValidSid
= (void *)GetProcAddress(hmod
, "IsValidSid");
217 pMakeSelfRelativeSD
= (void *)GetProcAddress(hmod
, "MakeSelfRelativeSD");
218 pSetEntriesInAclW
= (void *)GetProcAddress(hmod
, "SetEntriesInAclW");
219 pSetEntriesInAclA
= (void *)GetProcAddress(hmod
, "SetEntriesInAclA");
220 pSetSecurityDescriptorControl
= (void *)GetProcAddress(hmod
, "SetSecurityDescriptorControl");
221 pGetSecurityInfo
= (void *)GetProcAddress(hmod
, "GetSecurityInfo");
222 pSetSecurityInfo
= (void *)GetProcAddress(hmod
, "SetSecurityInfo");
223 pCreateRestrictedToken
= (void *)GetProcAddress(hmod
, "CreateRestrictedToken");
224 pConvertSidToStringSidA
= (void *)GetProcAddress(hmod
, "ConvertSidToStringSidA");
225 pConvertStringSidToSidA
= (void *)GetProcAddress(hmod
, "ConvertStringSidToSidA");
226 pGetAclInformation
= (void *)GetProcAddress(hmod
, "GetAclInformation");
227 pGetAce
= (void *)GetProcAddress(hmod
, "GetAce");
228 pGetWindowsAccountDomainSid
= (void *)GetProcAddress(hmod
, "GetWindowsAccountDomainSid");
229 pGetSidIdentifierAuthority
= (void *)GetProcAddress(hmod
, "GetSidIdentifierAuthority");
231 myARGC
= winetest_get_mainargs( &myARGV
);
234 static SECURITY_DESCRIPTOR
* test_get_security_descriptor(HANDLE handle
, int line
)
236 /* use HeapFree(GetProcessHeap(), 0, sd); when done */
237 DWORD ret
, length
, needed
;
238 SECURITY_DESCRIPTOR
*sd
;
241 SetLastError(0xdeadbeef);
242 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
244 ok_(__FILE__
, line
)(!ret
, "GetKernelObjectSecurity should fail\n");
245 ok_(__FILE__
, line
)(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
246 ok_(__FILE__
, line
)(needed
!= 0xdeadbeef, "GetKernelObjectSecurity should return required buffer length\n");
249 sd
= HeapAlloc(GetProcessHeap(), 0, length
);
252 SetLastError(0xdeadbeef);
253 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
254 sd
, length
, &needed
);
255 ok_(__FILE__
, line
)(ret
, "GetKernelObjectSecurity error %d\n", GetLastError());
256 ok_(__FILE__
, line
)(needed
== length
|| needed
== 0 /* file, pipe */, "GetKernelObjectSecurity should return %u instead of %u\n", length
, needed
);
260 static void test_owner_equal(HANDLE Handle
, PSID expected
, int line
)
263 SECURITY_DESCRIPTOR
*queriedSD
= NULL
;
265 BOOL owner_defaulted
;
267 queriedSD
= test_get_security_descriptor( Handle
, line
);
269 res
= GetSecurityDescriptorOwner(queriedSD
, &owner
, &owner_defaulted
);
270 ok_(__FILE__
, line
)(res
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
272 ok_(__FILE__
, line
)(EqualSid(owner
, expected
), "Owner SIDs are not equal %s != %s\n",
273 debugstr_sid(owner
), debugstr_sid(expected
));
274 ok_(__FILE__
, line
)(!owner_defaulted
, "Defaulted is true\n");
276 HeapFree(GetProcessHeap(), 0, queriedSD
);
279 static void test_group_equal(HANDLE Handle
, PSID expected
, int line
)
282 SECURITY_DESCRIPTOR
*queriedSD
= NULL
;
284 BOOL group_defaulted
;
286 queriedSD
= test_get_security_descriptor( Handle
, line
);
288 res
= GetSecurityDescriptorGroup(queriedSD
, &group
, &group_defaulted
);
289 ok_(__FILE__
, line
)(res
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
291 ok_(__FILE__
, line
)(EqualSid(group
, expected
), "Group SIDs are not equal %s != %s\n",
292 debugstr_sid(group
), debugstr_sid(expected
));
293 ok_(__FILE__
, line
)(!group_defaulted
, "Defaulted is true\n");
295 HeapFree(GetProcessHeap(), 0, queriedSD
);
298 static void test_sid(void)
300 struct sidRef refs
[] = {
301 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
302 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
303 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
304 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
305 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
306 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
308 struct strsid_entry strsid_table
[] = {
309 {"AO", STRSID_OK
}, {"RU", STRSID_OK
}, {"AN", STRSID_OK
}, {"AU", STRSID_OK
},
310 {"BA", STRSID_OK
}, {"BG", STRSID_OK
}, {"BO", STRSID_OK
}, {"BU", STRSID_OK
},
311 {"CA", STRSID_OPT
}, {"CG", STRSID_OK
}, {"CO", STRSID_OK
}, {"DA", STRSID_OPT
},
312 {"DC", STRSID_OPT
}, {"DD", STRSID_OPT
}, {"DG", STRSID_OPT
}, {"DU", STRSID_OPT
},
313 {"EA", STRSID_OPT
}, {"ED", STRSID_OK
}, {"WD", STRSID_OK
}, {"PA", STRSID_OPT
},
314 {"IU", STRSID_OK
}, {"LA", STRSID_OK
}, {"LG", STRSID_OK
}, {"LS", STRSID_OK
},
315 {"SY", STRSID_OK
}, {"NU", STRSID_OK
}, {"NO", STRSID_OK
}, {"NS", STRSID_OK
},
316 {"PO", STRSID_OK
}, {"PS", STRSID_OK
}, {"PU", STRSID_OK
}, {"RS", STRSID_OPT
},
317 {"RD", STRSID_OK
}, {"RE", STRSID_OK
}, {"RC", STRSID_OK
}, {"SA", STRSID_OPT
},
318 {"SO", STRSID_OK
}, {"SU", STRSID_OK
}};
320 const char noSubAuthStr
[] = "S-1-5";
327 if( !pConvertSidToStringSidA
|| !pConvertStringSidToSidA
)
329 win_skip("ConvertSidToStringSidA or ConvertStringSidToSidA not available\n");
333 r
= pConvertStringSidToSidA( NULL
, NULL
);
334 ok( !r
, "expected failure with NULL parameters\n" );
335 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
337 ok( GetLastError() == ERROR_INVALID_PARAMETER
,
338 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
341 r
= pConvertStringSidToSidA( refs
[0].refStr
, NULL
);
342 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
343 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
346 r
= pConvertStringSidToSidA( NULL
, &str
);
347 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
348 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
351 r
= pConvertStringSidToSidA( noSubAuthStr
, &psid
);
353 "expected failure with no sub authorities\n" );
354 ok( GetLastError() == ERROR_INVALID_SID
,
355 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
358 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
), "ConvertStringSidToSidA failed\n");
360 ok(pisid
->SubAuthorityCount
== 4, "Invalid sub authority count - expected 4, got %d\n", pisid
->SubAuthorityCount
);
361 ok(pisid
->SubAuthority
[0] == 21, "Invalid subauthority 0 - expected 21, got %d\n", pisid
->SubAuthority
[0]);
362 ok(pisid
->SubAuthority
[3] == 4576, "Invalid subauthority 0 - expected 4576, got %d\n", pisid
->SubAuthority
[3]);
366 for( i
= 0; i
< sizeof(refs
) / sizeof(refs
[0]); i
++ )
368 r
= AllocateAndInitializeSid( &refs
[i
].auth
, 1,1,0,0,0,0,0,0,0,
370 ok( r
, "failed to allocate sid\n" );
371 r
= pConvertSidToStringSidA( psid
, &str
);
372 ok( r
, "failed to convert sid\n" );
375 ok( !strcmp( str
, refs
[i
].refStr
),
376 "incorrect sid, expected %s, got %s\n", refs
[i
].refStr
, str
);
382 r
= pConvertStringSidToSidA( refs
[i
].refStr
, &psid
);
383 ok( r
, "failed to parse sid string\n" );
386 !memcmp( pisid
->IdentifierAuthority
.Value
, refs
[i
].auth
.Value
,
387 sizeof(refs
[i
].auth
) ),
388 "string sid %s didn't parse to expected value\n"
389 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
391 MAKEWORD( pisid
->IdentifierAuthority
.Value
[1],
392 pisid
->IdentifierAuthority
.Value
[0] ),
393 MAKELONG( MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
394 pisid
->IdentifierAuthority
.Value
[4] ),
395 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
396 pisid
->IdentifierAuthority
.Value
[2] ) ),
397 MAKEWORD( refs
[i
].auth
.Value
[1], refs
[i
].auth
.Value
[0] ),
398 MAKELONG( MAKEWORD( refs
[i
].auth
.Value
[5], refs
[i
].auth
.Value
[4] ),
399 MAKEWORD( refs
[i
].auth
.Value
[3], refs
[i
].auth
.Value
[2] ) ) );
404 /* string constant format not supported before XP */
405 r
= pConvertStringSidToSidA(strsid_table
[0].str
, &psid
);
408 win_skip("String constant format not supported\n");
413 for(i
= 0; i
< sizeof(strsid_table
) / sizeof(strsid_table
[0]); i
++)
417 SetLastError(0xdeadbeef);
418 r
= pConvertStringSidToSidA(strsid_table
[i
].str
, &psid
);
420 if (!(strsid_table
[i
].flags
& STRSID_OPT
))
422 ok(r
, "%s: got %u\n", strsid_table
[i
].str
, GetLastError());
427 if ((winetest_debug
> 1) && (pConvertSidToStringSidA(psid
, &temp
)))
429 trace(" %s: %s\n", strsid_table
[i
].str
, temp
);
436 if (GetLastError() != ERROR_INVALID_SID
)
437 trace(" %s: couldn't be converted, returned %d\n", strsid_table
[i
].str
, GetLastError());
439 trace(" %s: couldn't be converted\n", strsid_table
[i
].str
);
444 static void test_trustee(void)
446 GUID ObjectType
= {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
447 GUID InheritedObjectType
= {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
449 OBJECTS_AND_NAME_A oan
;
453 char szObjectTypeName
[] = "ObjectTypeName";
454 char szInheritedObjectTypeName
[] = "InheritedObjectTypeName";
455 char szTrusteeName
[] = "szTrusteeName";
456 SID_IDENTIFIER_AUTHORITY auth
= { {0x11,0x22,0,0,0, 0} };
458 memset( &ZeroGuid
, 0x00, sizeof (ZeroGuid
) );
460 pBuildTrusteeWithSidA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithSidA" );
461 pBuildTrusteeWithNameA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithNameA" );
462 pBuildTrusteeWithObjectsAndNameA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndNameA" );
463 pBuildTrusteeWithObjectsAndSidA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndSidA" );
464 pGetTrusteeNameA
= (void *)GetProcAddress (hmod
, "GetTrusteeNameA" );
465 if( !pBuildTrusteeWithSidA
|| !pBuildTrusteeWithNameA
||
466 !pBuildTrusteeWithObjectsAndNameA
|| !pBuildTrusteeWithObjectsAndSidA
||
470 if ( ! AllocateAndInitializeSid( &auth
, 1, 42, 0,0,0,0,0,0,0,&psid
) )
472 trace( "failed to init SID\n" );
476 /* test BuildTrusteeWithSidA */
477 memset( &trustee
, 0xff, sizeof trustee
);
478 pBuildTrusteeWithSidA( &trustee
, psid
);
480 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
481 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
482 "MultipleTrusteeOperation wrong\n");
483 ok( trustee
.TrusteeForm
== TRUSTEE_IS_SID
, "TrusteeForm wrong\n");
484 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
485 ok( trustee
.ptstrName
== psid
, "ptstrName wrong\n" );
487 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
488 memset( &trustee
, 0xff, sizeof trustee
);
489 memset( &oas
, 0xff, sizeof(oas
) );
490 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, &ObjectType
,
491 &InheritedObjectType
, psid
);
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_SID
, "TrusteeForm wrong\n");
496 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
497 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
499 ok(oas
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
500 ok(!memcmp(&oas
.ObjectTypeGuid
, &ObjectType
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
501 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
502 ok(oas
.pSid
== psid
, "pSid wrong\n");
504 /* test GetTrusteeNameA */
505 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oas
, "GetTrusteeName returned wrong value\n");
507 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
508 memset( &trustee
, 0xff, sizeof trustee
);
509 memset( &oas
, 0xff, sizeof(oas
) );
510 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, NULL
,
511 &InheritedObjectType
, psid
);
513 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
514 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
515 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
516 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
517 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
519 ok(oas
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
520 ok(!memcmp(&oas
.ObjectTypeGuid
, &ZeroGuid
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
521 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
522 ok(oas
.pSid
== psid
, "pSid wrong\n");
526 /* test BuildTrusteeWithNameA */
527 memset( &trustee
, 0xff, sizeof trustee
);
528 pBuildTrusteeWithNameA( &trustee
, szTrusteeName
);
530 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
531 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
532 "MultipleTrusteeOperation wrong\n");
533 ok( trustee
.TrusteeForm
== TRUSTEE_IS_NAME
, "TrusteeForm wrong\n");
534 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
535 ok( trustee
.ptstrName
== szTrusteeName
, "ptstrName wrong\n" );
537 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
538 memset( &trustee
, 0xff, sizeof trustee
);
539 memset( &oan
, 0xff, sizeof(oan
) );
540 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
541 szInheritedObjectTypeName
, szTrusteeName
);
543 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
544 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
545 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
546 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
547 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
549 ok(oan
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
550 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
551 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
552 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
554 /* test GetTrusteeNameA */
555 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oan
, "GetTrusteeName returned wrong value\n");
557 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
558 memset( &trustee
, 0xff, sizeof trustee
);
559 memset( &oan
, 0xff, sizeof(oan
) );
560 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, NULL
,
561 szInheritedObjectTypeName
, szTrusteeName
);
563 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
564 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
565 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
566 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
567 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
569 ok(oan
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
570 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
571 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
572 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
574 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
575 memset( &trustee
, 0xff, sizeof trustee
);
576 memset( &oan
, 0xff, sizeof(oan
) );
577 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
578 NULL
, szTrusteeName
);
580 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
581 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
582 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
583 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
584 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
586 ok(oan
.ObjectsPresent
== ACE_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
587 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
588 ok(oan
.InheritedObjectTypeName
== NULL
, "InheritedObjectTypeName wrong\n");
589 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
592 /* If the first isn't defined, assume none is */
593 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
594 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
595 #define SE_CREATE_TOKEN_PRIVILEGE 2L
596 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
597 #define SE_LOCK_MEMORY_PRIVILEGE 4L
598 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
599 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
600 #define SE_TCB_PRIVILEGE 7L
601 #define SE_SECURITY_PRIVILEGE 8L
602 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
603 #define SE_LOAD_DRIVER_PRIVILEGE 10L
604 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
605 #define SE_SYSTEMTIME_PRIVILEGE 12L
606 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
607 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
608 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
609 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
610 #define SE_BACKUP_PRIVILEGE 17L
611 #define SE_RESTORE_PRIVILEGE 18L
612 #define SE_SHUTDOWN_PRIVILEGE 19L
613 #define SE_DEBUG_PRIVILEGE 20L
614 #define SE_AUDIT_PRIVILEGE 21L
615 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
616 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
617 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
618 #define SE_UNDOCK_PRIVILEGE 25L
619 #define SE_SYNC_AGENT_PRIVILEGE 26L
620 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
621 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
622 #define SE_IMPERSONATE_PRIVILEGE 29L
623 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
624 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
625 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
627 static void test_allocateLuid(void)
629 BOOL (WINAPI
*pAllocateLocallyUniqueId
)(PLUID
);
633 pAllocateLocallyUniqueId
= (void*)GetProcAddress(hmod
, "AllocateLocallyUniqueId");
634 if (!pAllocateLocallyUniqueId
) return;
636 ret
= pAllocateLocallyUniqueId(&luid1
);
637 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
641 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
642 ret
= pAllocateLocallyUniqueId(&luid2
);
644 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
645 ok(luid1
.LowPart
> SE_MAX_WELL_KNOWN_PRIVILEGE
|| luid1
.HighPart
!= 0,
646 "AllocateLocallyUniqueId returned a well-known LUID\n");
647 ok(luid1
.LowPart
!= luid2
.LowPart
|| luid1
.HighPart
!= luid2
.HighPart
,
648 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
649 ret
= pAllocateLocallyUniqueId(NULL
);
650 ok( !ret
&& GetLastError() == ERROR_NOACCESS
,
651 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
655 static void test_lookupPrivilegeName(void)
657 BOOL (WINAPI
*pLookupPrivilegeNameA
)(LPCSTR
, PLUID
, LPSTR
, LPDWORD
);
658 char buf
[MAX_PATH
]; /* arbitrary, seems long enough */
659 DWORD cchName
= sizeof(buf
);
660 LUID luid
= { 0, 0 };
664 /* check whether it's available first */
665 pLookupPrivilegeNameA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeNameA");
666 if (!pLookupPrivilegeNameA
) return;
667 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
668 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
669 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
672 /* check with a short buffer */
674 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
675 ret
= pLookupPrivilegeNameA(NULL
, &luid
, NULL
, &cchName
);
676 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
677 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
679 ok(cchName
== strlen("SeCreateTokenPrivilege") + 1,
680 "LookupPrivilegeNameA returned an incorrect required length for\n"
681 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
682 lstrlenA("SeCreateTokenPrivilege") + 1);
683 /* check a known value and its returned length on success */
684 cchName
= sizeof(buf
);
685 ok(pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
) &&
686 cchName
== strlen("SeCreateTokenPrivilege"),
687 "LookupPrivilegeNameA returned an incorrect output length for\n"
688 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
689 (int)strlen("SeCreateTokenPrivilege"));
690 /* check known values */
691 for (i
= SE_MIN_WELL_KNOWN_PRIVILEGE
; i
<= SE_MAX_WELL_KNOWN_PRIVILEGE
; i
++)
694 cchName
= sizeof(buf
);
695 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
696 ok( ret
|| GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
697 "LookupPrivilegeNameA(0.%d) failed: %d\n", i
, GetLastError());
699 /* check a bogus LUID */
700 luid
.LowPart
= 0xdeadbeef;
701 cchName
= sizeof(buf
);
702 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
703 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
704 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
706 /* check on a bogus system */
707 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
708 cchName
= sizeof(buf
);
709 ret
= pLookupPrivilegeNameA("b0gu5.Nam3", &luid
, buf
, &cchName
);
710 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
711 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
712 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
722 static void test_lookupPrivilegeValue(void)
724 static const struct NameToLUID privs
[] = {
725 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE
},
726 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE
},
727 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE
},
728 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE
},
729 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE
},
730 { "SeTcbPrivilege", SE_TCB_PRIVILEGE
},
731 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE
},
732 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE
},
733 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE
},
734 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE
},
735 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE
},
736 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE
},
737 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE
},
738 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE
},
739 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE
},
740 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE
},
741 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE
},
742 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE
},
743 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE
},
744 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE
},
745 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE
},
746 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE
},
747 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE
},
748 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE
},
749 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE
},
750 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE
},
751 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE
},
752 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE
},
753 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE
},
755 BOOL (WINAPI
*pLookupPrivilegeValueA
)(LPCSTR
, LPCSTR
, PLUID
);
760 /* check whether it's available first */
761 pLookupPrivilegeValueA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeValueA");
762 if (!pLookupPrivilegeValueA
) return;
763 ret
= pLookupPrivilegeValueA(NULL
, "SeCreateTokenPrivilege", &luid
);
764 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
767 /* check a bogus system name */
768 ret
= pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid
);
769 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
770 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
771 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
773 /* check a NULL string */
774 ret
= pLookupPrivilegeValueA(NULL
, 0, &luid
);
775 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
776 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
778 /* check a bogus privilege name */
779 ret
= pLookupPrivilegeValueA(NULL
, "SeBogusPrivilege", &luid
);
780 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
781 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
783 /* check case insensitive */
784 ret
= pLookupPrivilegeValueA(NULL
, "sEcREATEtOKENpRIVILEGE", &luid
);
786 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
788 for (i
= 0; i
< sizeof(privs
) / sizeof(privs
[0]); i
++)
790 /* Not all privileges are implemented on all Windows versions, so
791 * don't worry if the call fails
793 if (pLookupPrivilegeValueA(NULL
, privs
[i
].name
, &luid
))
795 ok(luid
.LowPart
== privs
[i
].lowPart
,
796 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
802 static void test_luid(void)
805 test_lookupPrivilegeName();
806 test_lookupPrivilegeValue();
809 static void test_FileSecurity(void)
811 char wintmpdir
[MAX_PATH
];
812 char path
[MAX_PATH
];
813 char file
[MAX_PATH
];
815 DWORD sdSize
, retSize
, rc
, granted
, priv_set_len
;
816 PRIVILEGE_SET priv_set
;
819 GENERIC_MAPPING mapping
= { FILE_READ_DATA
, FILE_WRITE_DATA
, FILE_EXECUTE
, FILE_ALL_ACCESS
};
820 const SECURITY_INFORMATION request
= OWNER_SECURITY_INFORMATION
821 | GROUP_SECURITY_INFORMATION
822 | DACL_SECURITY_INFORMATION
;
824 if (!pGetFileSecurityA
) {
825 win_skip ("GetFileSecurity is not available\n");
829 if (!pSetFileSecurityA
) {
830 win_skip ("SetFileSecurity is not available\n");
834 if (!GetTempPathA (sizeof (wintmpdir
), wintmpdir
)) {
835 win_skip ("GetTempPathA failed\n");
839 /* Create a temporary directory and in it a temporary file */
840 strcat (strcpy (path
, wintmpdir
), "rary");
841 SetLastError(0xdeadbeef);
842 rc
= CreateDirectoryA (path
, NULL
);
843 ok (rc
|| GetLastError() == ERROR_ALREADY_EXISTS
, "CreateDirectoryA "
844 "failed for '%s' with %d\n", path
, GetLastError());
846 strcat (strcpy (file
, path
), "\\ess");
847 SetLastError(0xdeadbeef);
848 fh
= CreateFileA (file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
849 ok (fh
!= INVALID_HANDLE_VALUE
, "CreateFileA "
850 "failed for '%s' with %d\n", file
, GetLastError());
853 /* For the temporary file ... */
855 /* Get size needed */
857 SetLastError(0xdeadbeef);
858 rc
= pGetFileSecurityA (file
, request
, NULL
, 0, &retSize
);
859 if (!rc
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) {
860 win_skip("GetFileSecurityA is not implemented\n");
863 ok (!rc
, "GetFileSecurityA "
864 "was expected to fail for '%s'\n", file
);
865 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
866 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
867 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
870 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
872 /* Get security descriptor for real */
874 SetLastError(0xdeadbeef);
875 rc
= pGetFileSecurityA (file
, request
, sd
, sdSize
, &retSize
);
876 ok (rc
, "GetFileSecurityA "
877 "was not expected to fail '%s': %d\n", file
, GetLastError());
878 ok (retSize
== sdSize
||
879 broken(retSize
== 0), /* NT4 */
880 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
882 /* Use it to set security descriptor */
883 SetLastError(0xdeadbeef);
884 rc
= pSetFileSecurityA (file
, request
, sd
);
885 ok (rc
, "SetFileSecurityA "
886 "was not expected to fail '%s': %d\n", file
, GetLastError());
888 HeapFree (GetProcessHeap (), 0, sd
);
890 /* Repeat for the temporary directory ... */
892 /* Get size needed */
894 SetLastError(0xdeadbeef);
895 rc
= pGetFileSecurityA (path
, request
, NULL
, 0, &retSize
);
896 ok (!rc
, "GetFileSecurityA "
897 "was expected to fail for '%s'\n", path
);
898 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
899 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
900 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
903 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
905 /* Get security descriptor for real */
907 SetLastError(0xdeadbeef);
908 rc
= pGetFileSecurityA (path
, request
, sd
, sdSize
, &retSize
);
909 ok (rc
, "GetFileSecurityA "
910 "was not expected to fail '%s': %d\n", path
, GetLastError());
911 ok (retSize
== sdSize
||
912 broken(retSize
== 0), /* NT4 */
913 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
915 /* Use it to set security descriptor */
916 SetLastError(0xdeadbeef);
917 rc
= pSetFileSecurityA (path
, request
, sd
);
918 ok (rc
, "SetFileSecurityA "
919 "was not expected to fail '%s': %d\n", path
, GetLastError());
920 HeapFree (GetProcessHeap (), 0, sd
);
923 strcpy (wintmpdir
, "\\Should not exist");
924 SetLastError(0xdeadbeef);
925 rc
= pGetFileSecurityA (wintmpdir
, OWNER_SECURITY_INFORMATION
, NULL
, 0, &sdSize
);
926 ok (!rc
, "GetFileSecurityA should fail for not existing directories/files\n");
927 ok (GetLastError() == ERROR_FILE_NOT_FOUND
,
928 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
931 /* Remove temporary file and directory */
933 RemoveDirectoryA(path
);
935 /* Test file access permissions for a file with FILE_ATTRIBUTE_ARCHIVE */
936 SetLastError(0xdeadbeef);
937 rc
= GetTempPathA(sizeof(wintmpdir
), wintmpdir
);
938 ok(rc
, "GetTempPath error %d\n", GetLastError());
940 SetLastError(0xdeadbeef);
941 rc
= GetTempFileNameA(wintmpdir
, "tmp", 0, file
);
942 ok(rc
, "GetTempFileName error %d\n", GetLastError());
944 rc
= GetFileAttributesA(file
);
945 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
946 ok(rc
== FILE_ATTRIBUTE_ARCHIVE
, "expected FILE_ATTRIBUTE_ARCHIVE got %#x\n", rc
);
948 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
950 ok(!rc
, "GetFileSecurity should fail\n");
951 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
952 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
953 ok(sdSize
> sizeof(SECURITY_DESCRIPTOR
), "got sd size %d\n", sdSize
);
955 sd
= HeapAlloc(GetProcessHeap (), 0, sdSize
);
956 retSize
= 0xdeadbeef;
957 SetLastError(0xdeadbeef);
958 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
959 sd
, sdSize
, &retSize
);
960 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
961 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
963 SetLastError(0xdeadbeef);
964 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
965 ok(!rc
, "OpenThreadToken should fail\n");
966 ok(GetLastError() == ERROR_NO_TOKEN
, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
968 SetLastError(0xdeadbeef);
969 rc
= ImpersonateSelf(SecurityIdentification
);
970 ok(rc
, "ImpersonateSelf error %d\n", GetLastError());
972 SetLastError(0xdeadbeef);
973 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
974 ok(rc
, "OpenThreadToken error %d\n", GetLastError());
976 SetLastError(0xdeadbeef);
978 ok(rc
, "RevertToSelf error %d\n", GetLastError());
980 priv_set_len
= sizeof(priv_set
);
981 granted
= 0xdeadbeef;
983 SetLastError(0xdeadbeef);
984 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
985 ok(rc
, "AccessCheck error %d\n", GetLastError());
986 ok(status
== 1, "expected 1, got %d\n", status
);
987 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
989 granted
= 0xdeadbeef;
991 SetLastError(0xdeadbeef);
992 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
993 ok(rc
, "AccessCheck error %d\n", GetLastError());
994 ok(status
== 1, "expected 1, got %d\n", status
);
995 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
997 granted
= 0xdeadbeef;
999 SetLastError(0xdeadbeef);
1000 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1001 ok(rc
, "AccessCheck error %d\n", GetLastError());
1002 ok(status
== 1, "expected 1, got %d\n", status
);
1003 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
1005 granted
= 0xdeadbeef;
1006 status
= 0xdeadbeef;
1007 SetLastError(0xdeadbeef);
1008 rc
= AccessCheck(sd
, token
, DELETE
, &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
== DELETE
, "expected DELETE, got %#x\n", granted
);
1013 granted
= 0xdeadbeef;
1014 status
= 0xdeadbeef;
1015 SetLastError(0xdeadbeef);
1016 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1017 ok(rc
, "AccessCheck error %d\n", GetLastError());
1018 ok(status
== 1, "expected 1, got %d\n", status
);
1019 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
1021 granted
= 0xdeadbeef;
1022 status
= 0xdeadbeef;
1023 SetLastError(0xdeadbeef);
1024 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1025 ok(rc
, "AccessCheck error %d\n", GetLastError());
1026 ok(status
== 1, "expected 1, got %d\n", status
);
1027 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
1029 granted
= 0xdeadbeef;
1030 status
= 0xdeadbeef;
1031 SetLastError(0xdeadbeef);
1032 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1033 ok(rc
, "AccessCheck error %d\n", GetLastError());
1034 ok(status
== 1, "expected 1, got %d\n", status
);
1035 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
1037 SetLastError(0xdeadbeef);
1038 rc
= AccessCheck(sd
, token
, 0xffffffff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1039 ok(!rc
, "AccessCheck should fail\n");
1040 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED
, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
1042 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
1043 SetLastError(0xdeadbeef);
1044 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
1045 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
1046 retSize
= 0xdeadbeef;
1047 SetLastError(0xdeadbeef);
1048 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
1049 ok(!rc
, "WriteFile should fail\n");
1050 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1051 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
1054 rc
= GetFileAttributesA(file
);
1055 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
1057 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
1058 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
1060 SetLastError(0xdeadbeef);
1061 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
1062 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1063 SetLastError(0xdeadbeef);
1064 rc
= DeleteFileA(file
);
1065 ok(rc
, "DeleteFile error %d\n", GetLastError());
1067 SetLastError(0xdeadbeef);
1068 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
1069 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
1070 retSize
= 0xdeadbeef;
1071 SetLastError(0xdeadbeef);
1072 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
1073 ok(!rc
, "WriteFile should fail\n");
1074 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1075 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
1078 rc
= GetFileAttributesA(file
);
1079 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
1080 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
1081 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
1083 retSize
= 0xdeadbeef;
1084 SetLastError(0xdeadbeef);
1085 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
1086 sd
, sdSize
, &retSize
);
1087 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
1088 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
1090 priv_set_len
= sizeof(priv_set
);
1091 granted
= 0xdeadbeef;
1092 status
= 0xdeadbeef;
1093 SetLastError(0xdeadbeef);
1094 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1095 ok(rc
, "AccessCheck error %d\n", GetLastError());
1096 ok(status
== 1, "expected 1, got %d\n", status
);
1097 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
1099 granted
= 0xdeadbeef;
1100 status
= 0xdeadbeef;
1101 SetLastError(0xdeadbeef);
1102 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1103 ok(rc
, "AccessCheck error %d\n", GetLastError());
1105 ok(status
== 1, "expected 1, got %d\n", status
);
1106 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
1108 granted
= 0xdeadbeef;
1109 status
= 0xdeadbeef;
1110 SetLastError(0xdeadbeef);
1111 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1112 ok(rc
, "AccessCheck error %d\n", GetLastError());
1113 ok(status
== 1, "expected 1, got %d\n", status
);
1114 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
1116 granted
= 0xdeadbeef;
1117 status
= 0xdeadbeef;
1118 SetLastError(0xdeadbeef);
1119 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1120 ok(rc
, "AccessCheck error %d\n", GetLastError());
1122 ok(status
== 1, "expected 1, got %d\n", status
);
1123 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
1125 granted
= 0xdeadbeef;
1126 status
= 0xdeadbeef;
1127 SetLastError(0xdeadbeef);
1128 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1129 ok(rc
, "AccessCheck error %d\n", GetLastError());
1131 ok(status
== 1, "expected 1, got %d\n", status
);
1132 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
1134 granted
= 0xdeadbeef;
1135 status
= 0xdeadbeef;
1136 SetLastError(0xdeadbeef);
1137 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1138 ok(rc
, "AccessCheck error %d\n", GetLastError());
1140 ok(status
== 1, "expected 1, got %d\n", status
);
1141 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
1143 granted
= 0xdeadbeef;
1144 status
= 0xdeadbeef;
1145 SetLastError(0xdeadbeef);
1146 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1147 ok(rc
, "AccessCheck error %d\n", GetLastError());
1149 ok(status
== 1, "expected 1, got %d\n", status
);
1150 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
1152 SetLastError(0xdeadbeef);
1153 rc
= DeleteFileA(file
);
1154 ok(!rc
, "DeleteFile should fail\n");
1155 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1156 SetLastError(0xdeadbeef);
1157 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
1158 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1159 SetLastError(0xdeadbeef);
1160 rc
= DeleteFileA(file
);
1161 ok(rc
, "DeleteFile error %d\n", GetLastError());
1164 HeapFree(GetProcessHeap(), 0, sd
);
1167 static void test_AccessCheck(void)
1169 PSID EveryoneSid
= NULL
, AdminSid
= NULL
, UsersSid
= NULL
;
1171 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
1172 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
1173 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1174 GENERIC_MAPPING Mapping
= { KEY_READ
, KEY_WRITE
, KEY_EXECUTE
, KEY_ALL_ACCESS
};
1178 HANDLE ProcessToken
;
1181 PRIVILEGE_SET
*PrivSet
;
1183 HMODULE NtDllModule
;
1186 NTSTATUS ntret
, ntAccessStatus
;
1188 NtDllModule
= GetModuleHandleA("ntdll.dll");
1191 skip("not running on NT, skipping test\n");
1194 pRtlAdjustPrivilege
= (void *)GetProcAddress(NtDllModule
, "RtlAdjustPrivilege");
1195 if (!pRtlAdjustPrivilege
)
1197 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1201 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
1202 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1203 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1205 skip("ACLs not implemented - skipping tests\n");
1206 HeapFree(GetProcessHeap(), 0, Acl
);
1209 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
1211 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
1212 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1214 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1215 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &AdminSid
);
1216 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1218 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1219 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
1220 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1222 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
1224 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
1225 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1227 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1228 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1230 PrivSetLen
= FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[16]);
1231 PrivSet
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, PrivSetLen
);
1232 PrivSet
->PrivilegeCount
= 16;
1234 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &ProcessToken
);
1235 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
1237 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, FALSE
, TRUE
, &Enabled
);
1239 res
= DuplicateToken(ProcessToken
, SecurityImpersonation
, &Token
);
1240 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1242 /* SD without owner/group */
1243 SetLastError(0xdeadbeef);
1244 Access
= AccessStatus
= 0x1abe11ed;
1245 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_QUERY_VALUE
, &Mapping
,
1246 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1247 err
= GetLastError();
1248 ok(!ret
&& err
== ERROR_INVALID_SECURITY_DESCR
, "AccessCheck should have "
1249 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err
);
1250 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1251 "Access and/or AccessStatus were changed!\n");
1253 /* Set owner and group */
1254 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
1255 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1256 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, TRUE
);
1257 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1259 /* Generic access mask */
1260 SetLastError(0xdeadbeef);
1261 Access
= AccessStatus
= 0x1abe11ed;
1262 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1263 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1264 err
= GetLastError();
1265 ok(!ret
&& err
== ERROR_GENERIC_NOT_MAPPED
, "AccessCheck should have failed "
1266 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err
);
1267 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1268 "Access and/or AccessStatus were changed!\n");
1270 /* Generic access mask - no privilegeset buffer */
1271 SetLastError(0xdeadbeef);
1272 Access
= AccessStatus
= 0x1abe11ed;
1273 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1274 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1275 err
= GetLastError();
1276 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1277 "with ERROR_NOACCESS, instead of %d\n", err
);
1278 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1279 "Access and/or AccessStatus were changed!\n");
1281 /* Generic access mask - no returnlength */
1282 SetLastError(0xdeadbeef);
1283 Access
= AccessStatus
= 0x1abe11ed;
1284 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1285 PrivSet
, NULL
, &Access
, &AccessStatus
);
1286 err
= GetLastError();
1287 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1288 "with ERROR_NOACCESS, instead of %d\n", err
);
1289 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1290 "Access and/or AccessStatus were changed!\n");
1292 /* Generic access mask - no privilegeset buffer, no returnlength */
1293 SetLastError(0xdeadbeef);
1294 Access
= AccessStatus
= 0x1abe11ed;
1295 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1296 NULL
, NULL
, &Access
, &AccessStatus
);
1297 err
= GetLastError();
1298 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1299 "with ERROR_NOACCESS, instead of %d\n", err
);
1300 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1301 "Access and/or AccessStatus were changed!\n");
1303 /* sd with no dacl present */
1304 Access
= AccessStatus
= 0x1abe11ed;
1305 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, FALSE
, NULL
, FALSE
);
1306 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1307 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1308 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1309 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1310 ok(AccessStatus
&& (Access
== KEY_READ
),
1311 "AccessCheck failed to grant access with error %d\n",
1314 /* sd with no dacl present - no privilegeset buffer */
1315 SetLastError(0xdeadbeef);
1316 Access
= AccessStatus
= 0x1abe11ed;
1317 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1318 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1319 err
= GetLastError();
1320 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1321 "with ERROR_NOACCESS, instead of %d\n", err
);
1322 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1323 "Access and/or AccessStatus were changed!\n");
1327 /* Generic access mask - no privilegeset buffer */
1328 SetLastError(0xdeadbeef);
1329 Access
= ntAccessStatus
= 0x1abe11ed;
1330 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1331 NULL
, &PrivSetLen
, &Access
, &ntAccessStatus
);
1332 err
= GetLastError();
1333 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1334 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1335 ok(err
== 0xdeadbeef,
1336 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1337 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1338 "Access and/or AccessStatus were changed!\n");
1340 /* Generic access mask - no returnlength */
1341 SetLastError(0xdeadbeef);
1342 Access
= ntAccessStatus
= 0x1abe11ed;
1343 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1344 PrivSet
, NULL
, &Access
, &ntAccessStatus
);
1345 err
= GetLastError();
1346 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1347 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1348 ok(err
== 0xdeadbeef,
1349 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1350 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1351 "Access and/or AccessStatus were changed!\n");
1353 /* Generic access mask - no privilegeset buffer, no returnlength */
1354 SetLastError(0xdeadbeef);
1355 Access
= ntAccessStatus
= 0x1abe11ed;
1356 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1357 NULL
, NULL
, &Access
, &ntAccessStatus
);
1358 err
= GetLastError();
1359 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1360 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1361 ok(err
== 0xdeadbeef,
1362 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1363 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1364 "Access and/or AccessStatus were changed!\n");
1367 win_skip("NtAccessCheck unavailable. Skipping.\n");
1369 /* sd with NULL dacl */
1370 Access
= AccessStatus
= 0x1abe11ed;
1371 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, NULL
, FALSE
);
1372 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1373 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1374 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1375 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1376 ok(AccessStatus
&& (Access
== KEY_READ
),
1377 "AccessCheck failed to grant access with error %d\n",
1380 /* sd with blank dacl */
1381 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1382 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1383 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1384 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1385 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1386 err
= GetLastError();
1387 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1388 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1389 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1391 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, KEY_READ
, EveryoneSid
);
1392 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1394 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, KEY_SET_VALUE
, AdminSid
);
1395 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1398 Access
= AccessStatus
= 0x1abe11ed;
1399 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1400 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1401 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1402 ok(AccessStatus
&& (Access
== KEY_READ
),
1403 "AccessCheck failed to grant access with error %d\n",
1406 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1407 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1408 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1410 "AccessCheck failed to grant any access with error %d\n",
1412 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access
);
1414 /* Access denied by SD */
1415 SetLastError(0xdeadbeef);
1416 Access
= AccessStatus
= 0x1abe11ed;
1417 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_WRITE
, &Mapping
,
1418 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1419 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1420 err
= GetLastError();
1421 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1422 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1423 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1426 PrivSet
->PrivilegeCount
= 16;
1427 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1428 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1429 ok(ret
&& !AccessStatus
&& GetLastError() == ERROR_PRIVILEGE_NOT_HELD
,
1430 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1433 ret
= ImpersonateLoggedOnUser(Token
);
1434 ok(ret
, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1435 ret
= pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, TRUE
, TRUE
, &Enabled
);
1439 PrivSet
->PrivilegeCount
= 16;
1440 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1441 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1442 ok(ret
&& AccessStatus
&& GetLastError() == 0,
1443 "AccessCheck should have succeeded, error %d\n",
1445 ok(Access
== ACCESS_SYSTEM_SECURITY
,
1446 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1450 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1452 ret
= RevertToSelf();
1453 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1455 /* test INHERIT_ONLY_ACE */
1456 ret
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1457 ok(ret
, "InitializeAcl failed with error %d\n", GetLastError());
1459 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1462 if (pAddAccessAllowedAceEx
)
1464 ret
= pAddAccessAllowedAceEx(Acl
, ACL_REVISION
, INHERIT_ONLY_ACE
, KEY_READ
, EveryoneSid
);
1465 ok(ret
, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1468 win_skip("AddAccessAllowedAceEx is not available\n");
1470 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1471 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1472 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1473 err
= GetLastError();
1474 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1475 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1476 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1480 res
= DuplicateToken(ProcessToken
, SecurityAnonymous
, &Token
);
1481 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1483 SetLastError(0xdeadbeef);
1484 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1485 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1486 err
= GetLastError();
1487 ok(!ret
&& err
== ERROR_BAD_IMPERSONATION_LEVEL
, "AccessCheck should have failed "
1488 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err
);
1492 SetLastError(0xdeadbeef);
1493 ret
= AccessCheck(SecurityDescriptor
, ProcessToken
, KEY_READ
, &Mapping
,
1494 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1495 err
= GetLastError();
1496 ok(!ret
&& err
== ERROR_NO_IMPERSONATION_TOKEN
, "AccessCheck should have failed "
1497 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err
);
1499 CloseHandle(ProcessToken
);
1502 FreeSid(EveryoneSid
);
1507 HeapFree(GetProcessHeap(), 0, Acl
);
1508 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
1509 HeapFree(GetProcessHeap(), 0, PrivSet
);
1512 /* test GetTokenInformation for the various attributes */
1513 static void test_token_attr(void)
1515 HANDLE Token
, ImpersonationToken
;
1517 TOKEN_PRIVILEGES
*Privileges
;
1518 TOKEN_GROUPS
*Groups
;
1520 TOKEN_DEFAULT_DACL
*Dacl
;
1524 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1527 /* cygwin-like use case */
1528 SetLastError(0xdeadbeef);
1529 ret
= OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED
, &Token
);
1530 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1532 win_skip("OpenProcessToken is not implemented\n");
1535 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1538 DWORD buf
[256]; /* GetTokenInformation wants a dword-aligned buffer */
1540 ret
= GetTokenInformation(Token
, TokenUser
,(void*)buf
, Size
, &Size
);
1541 ok(ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1542 Size
= sizeof(ImpersonationLevel
);
1543 ret
= GetTokenInformation(Token
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1544 GLE
= GetLastError();
1545 ok(!ret
&& (GLE
== ERROR_INVALID_PARAMETER
), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE
);
1549 if(!pConvertSidToStringSidA
)
1551 win_skip("ConvertSidToStringSidA is not available\n");
1555 SetLastError(0xdeadbeef);
1556 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS
, &Token
);
1557 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1560 /* insufficient buffer length */
1561 SetLastError(0xdeadbeef);
1563 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size2
);
1564 ok(Size2
> 1, "got %d\n", Size2
);
1565 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1566 "%d with error %d\n", ret
, GetLastError());
1568 Groups
= HeapAlloc(GetProcessHeap(), 0, Size2
);
1569 memset(Groups
, 0xcc, Size2
);
1571 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size2
, &Size
);
1572 ok(Size
> 1, "got %d\n", Size
);
1573 ok((!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) || broken(ret
) /* wow64 */,
1574 "%d with error %d\n", ret
, GetLastError());
1576 ok(*((BYTE
*)Groups
) == 0xcc, "buffer altered\n");
1578 HeapFree(GetProcessHeap(), 0, Groups
);
1580 SetLastError(0xdeadbeef);
1581 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size
);
1582 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1583 "GetTokenInformation(TokenGroups) %s with error %d\n",
1584 ret
? "succeeded" : "failed", GetLastError());
1585 Groups
= HeapAlloc(GetProcessHeap(), 0, Size
);
1586 SetLastError(0xdeadbeef);
1587 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size
, &Size
);
1588 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1589 ok(GetLastError() == 0xdeadbeef,
1590 "GetTokenInformation shouldn't have set last error to %d\n",
1592 trace("TokenGroups:\n");
1593 for (i
= 0; i
< Groups
->GroupCount
; i
++)
1595 DWORD NameLength
= 255;
1597 DWORD DomainLength
= 255;
1599 SID_NAME_USE SidNameUse
;
1602 ret
= LookupAccountSidA(NULL
, Groups
->Groups
[i
].Sid
, Name
, &NameLength
, Domain
, &DomainLength
, &SidNameUse
);
1605 pConvertSidToStringSidA(Groups
->Groups
[i
].Sid
, &SidString
);
1606 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString
, Domain
, Name
, SidNameUse
, Groups
->Groups
[i
].Attributes
);
1607 LocalFree(SidString
);
1609 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups
->Groups
[i
].Attributes
, GetLastError());
1611 HeapFree(GetProcessHeap(), 0, Groups
);
1614 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
1615 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1616 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1617 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
1618 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
1620 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1622 pConvertSidToStringSidA(User
->User
.Sid
, &SidString
);
1623 trace("TokenUser: %s attr: 0x%08x\n", SidString
, User
->User
.Attributes
);
1624 LocalFree(SidString
);
1625 HeapFree(GetProcessHeap(), 0, User
);
1628 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
1629 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1630 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1631 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1632 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1634 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1635 trace("TokenPrivileges:\n");
1636 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1639 DWORD NameLen
= sizeof(Name
)/sizeof(Name
[0]);
1640 LookupPrivilegeNameA(NULL
, &Privileges
->Privileges
[i
].Luid
, Name
, &NameLen
);
1641 trace("\t%s, 0x%x\n", Name
, Privileges
->Privileges
[i
].Attributes
);
1643 HeapFree(GetProcessHeap(), 0, Privileges
);
1645 ret
= DuplicateToken(Token
, SecurityAnonymous
, &ImpersonationToken
);
1646 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1648 Size
= sizeof(ImpersonationLevel
);
1649 ret
= GetTokenInformation(ImpersonationToken
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1650 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1651 ok(ImpersonationLevel
== SecurityAnonymous
, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel
);
1653 CloseHandle(ImpersonationToken
);
1656 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0, &Size
);
1657 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1658 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1660 Dacl
= HeapAlloc(GetProcessHeap(), 0, Size
);
1661 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size
);
1662 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1664 SetLastError(0xdeadbeef);
1665 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0);
1666 GLE
= GetLastError();
1667 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1668 ok(GLE
== ERROR_BAD_LENGTH
, "expected ERROR_BAD_LENGTH got %u\n", GLE
);
1670 SetLastError(0xdeadbeef);
1671 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, Size
);
1672 GLE
= GetLastError();
1673 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1674 ok(GLE
== ERROR_NOACCESS
, "expected ERROR_NOACCESS got %u\n", GLE
);
1676 acl
= Dacl
->DefaultDacl
;
1677 Dacl
->DefaultDacl
= NULL
;
1679 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1680 ok(ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1683 Dacl
->DefaultDacl
= (ACL
*)0xdeadbeef;
1684 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1685 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1686 ok(Dacl
->DefaultDacl
== NULL
, "expected NULL, got %p\n", Dacl
->DefaultDacl
);
1687 ok(Size2
== sizeof(TOKEN_DEFAULT_DACL
) || broken(Size2
== 2*sizeof(TOKEN_DEFAULT_DACL
)), /* WoW64 */
1688 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2
);
1690 Dacl
->DefaultDacl
= acl
;
1691 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1692 ok(ret
, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1694 if (Size2
== sizeof(TOKEN_DEFAULT_DACL
)) {
1695 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1696 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1698 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1700 HeapFree(GetProcessHeap(), 0, Dacl
);
1704 typedef union _MAX_SID
1707 char max
[SECURITY_MAX_SID_SIZE
];
1710 static void test_sid_str(PSID
* sid
)
1713 BOOL ret
= pConvertSidToStringSidA(sid
, &str_sid
);
1714 ok(ret
, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1717 char account
[MAX_PATH
], domain
[MAX_PATH
];
1719 DWORD acc_size
= MAX_PATH
;
1720 DWORD dom_size
= MAX_PATH
;
1721 ret
= LookupAccountSidA (NULL
, sid
, account
, &acc_size
, domain
, &dom_size
, &use
);
1722 ok(ret
|| GetLastError() == ERROR_NONE_MAPPED
,
1723 "LookupAccountSid(%s) failed: %d\n", str_sid
, GetLastError());
1725 trace(" %s %s\\%s %d\n", str_sid
, domain
, account
, use
);
1726 else if (GetLastError() == ERROR_NONE_MAPPED
)
1727 trace(" %s couldn't be mapped\n", str_sid
);
1732 static const struct well_known_sid_value
1734 BOOL without_domain
;
1735 const char *sid_string
;
1736 } well_known_sid_values
[] = {
1737 /* 0 */ {TRUE
, "S-1-0-0"}, {TRUE
, "S-1-1-0"}, {TRUE
, "S-1-2-0"}, {TRUE
, "S-1-3-0"},
1738 /* 4 */ {TRUE
, "S-1-3-1"}, {TRUE
, "S-1-3-2"}, {TRUE
, "S-1-3-3"}, {TRUE
, "S-1-5"},
1739 /* 8 */ {FALSE
, "S-1-5-1"}, {TRUE
, "S-1-5-2"}, {TRUE
, "S-1-5-3"}, {TRUE
, "S-1-5-4"},
1740 /* 12 */ {TRUE
, "S-1-5-6"}, {TRUE
, "S-1-5-7"}, {TRUE
, "S-1-5-8"}, {TRUE
, "S-1-5-9"},
1741 /* 16 */ {TRUE
, "S-1-5-10"}, {TRUE
, "S-1-5-11"}, {TRUE
, "S-1-5-12"}, {TRUE
, "S-1-5-13"},
1742 /* 20 */ {TRUE
, "S-1-5-14"}, {FALSE
, NULL
}, {TRUE
, "S-1-5-18"}, {TRUE
, "S-1-5-19"},
1743 /* 24 */ {TRUE
, "S-1-5-20"}, {TRUE
, "S-1-5-32"},
1744 /* 26 */ {FALSE
, "S-1-5-32-544"}, {TRUE
, "S-1-5-32-545"}, {TRUE
, "S-1-5-32-546"},
1745 /* 29 */ {TRUE
, "S-1-5-32-547"}, {TRUE
, "S-1-5-32-548"}, {TRUE
, "S-1-5-32-549"},
1746 /* 32 */ {TRUE
, "S-1-5-32-550"}, {TRUE
, "S-1-5-32-551"}, {TRUE
, "S-1-5-32-552"},
1747 /* 35 */ {TRUE
, "S-1-5-32-554"}, {TRUE
, "S-1-5-32-555"}, {TRUE
, "S-1-5-32-556"},
1748 /* 38 */ {FALSE
, "S-1-5-21-12-23-34-45-56-500"}, {FALSE
, "S-1-5-21-12-23-34-45-56-501"},
1749 /* 40 */ {FALSE
, "S-1-5-21-12-23-34-45-56-502"}, {FALSE
, "S-1-5-21-12-23-34-45-56-512"},
1750 /* 42 */ {FALSE
, "S-1-5-21-12-23-34-45-56-513"}, {FALSE
, "S-1-5-21-12-23-34-45-56-514"},
1751 /* 44 */ {FALSE
, "S-1-5-21-12-23-34-45-56-515"}, {FALSE
, "S-1-5-21-12-23-34-45-56-516"},
1752 /* 46 */ {FALSE
, "S-1-5-21-12-23-34-45-56-517"}, {FALSE
, "S-1-5-21-12-23-34-45-56-518"},
1753 /* 48 */ {FALSE
, "S-1-5-21-12-23-34-45-56-519"}, {FALSE
, "S-1-5-21-12-23-34-45-56-520"},
1754 /* 50 */ {FALSE
, "S-1-5-21-12-23-34-45-56-553"},
1755 /* Added in Windows Server 2003 */
1756 /* 51 */ {TRUE
, "S-1-5-64-10"}, {TRUE
, "S-1-5-64-21"}, {TRUE
, "S-1-5-64-14"},
1757 /* 54 */ {TRUE
, "S-1-5-15"}, {TRUE
, "S-1-5-1000"}, {FALSE
, "S-1-5-32-557"},
1758 /* 57 */ {TRUE
, "S-1-5-32-558"}, {TRUE
, "S-1-5-32-559"}, {TRUE
, "S-1-5-32-560"},
1759 /* 60 */ {TRUE
, "S-1-5-32-561"}, {TRUE
, "S-1-5-32-562"},
1760 /* Added in Windows Vista: */
1761 /* 62 */ {TRUE
, "S-1-5-32-568"},
1762 /* 63 */ {TRUE
, "S-1-5-17"}, {FALSE
, "S-1-5-32-569"}, {TRUE
, "S-1-16-0"},
1763 /* 66 */ {TRUE
, "S-1-16-4096"}, {TRUE
, "S-1-16-8192"}, {TRUE
, "S-1-16-12288"},
1764 /* 69 */ {TRUE
, "S-1-16-16384"}, {TRUE
, "S-1-5-33"}, {TRUE
, "S-1-3-4"},
1765 /* 72 */ {FALSE
, "S-1-5-21-12-23-34-45-56-571"}, {FALSE
, "S-1-5-21-12-23-34-45-56-572"},
1766 /* 74 */ {TRUE
, "S-1-5-22"}, {FALSE
, "S-1-5-21-12-23-34-45-56-521"}, {TRUE
, "S-1-5-32-573"},
1767 /* 77 */ {FALSE
, "S-1-5-21-12-23-34-45-56-498"}, {TRUE
, "S-1-5-32-574"}, {TRUE
, "S-1-16-8448"},
1768 /* 80 */ {FALSE
, NULL
}, {TRUE
, "S-1-2-1"}, {TRUE
, "S-1-5-65-1"}, {FALSE
, NULL
},
1769 /* 84 */ {TRUE
, "S-1-15-2-1"},
1772 static void test_CreateWellKnownSid(void)
1774 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
1775 PSID domainsid
, sid
;
1780 if (!pCreateWellKnownSid
)
1782 win_skip("CreateWellKnownSid not available\n");
1787 SetLastError(0xdeadbeef);
1788 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1789 error
= GetLastError();
1790 ok(!ret
, "CreateWellKnownSid succeeded\n");
1791 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1792 ok(size
, "expected size > 0\n");
1794 SetLastError(0xdeadbeef);
1795 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1796 error
= GetLastError();
1797 ok(!ret
, "CreateWellKnownSid succeeded\n");
1798 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
1800 sid
= HeapAlloc(GetProcessHeap(), 0, size
);
1801 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, sid
, &size
);
1802 ok(ret
, "CreateWellKnownSid failed %u\n", GetLastError());
1803 HeapFree(GetProcessHeap(), 0, sid
);
1805 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1806 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
1808 for (i
= 0; i
< sizeof(well_known_sid_values
)/sizeof(well_known_sid_values
[0]); i
++)
1810 const struct well_known_sid_value
*value
= &well_known_sid_values
[i
];
1811 char sid_buffer
[SECURITY_MAX_SID_SIZE
];
1815 if (value
->sid_string
== NULL
)
1818 /* some SIDs aren't implemented by all Windows versions - detect it */
1819 cb
= sizeof(sid_buffer
);
1820 if (!pCreateWellKnownSid(i
, NULL
, sid_buffer
, &cb
))
1822 skip("Well known SID %u not implemented\n", i
);
1826 cb
= sizeof(sid_buffer
);
1827 ok(pCreateWellKnownSid(i
, value
->without_domain
? NULL
: domainsid
, sid_buffer
, &cb
), "Couldn't create well known sid %u\n", i
);
1828 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1829 ok(IsValidSid(sid_buffer
), "The sid is not valid\n");
1830 ok(pConvertSidToStringSidA(sid_buffer
, &str
), "Couldn't convert SID to string\n");
1831 ok(strcmp(str
, value
->sid_string
) == 0, "%d: SID mismatch - expected %s, got %s\n", i
,
1832 value
->sid_string
, str
);
1835 if (value
->without_domain
)
1837 char buf2
[SECURITY_MAX_SID_SIZE
];
1839 ok(pCreateWellKnownSid(i
, domainsid
, buf2
, &cb
), "Couldn't create well known sid %u with optional domain\n", i
);
1840 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1841 ok(memcmp(buf2
, sid_buffer
, cb
) == 0, "SID create with domain is different than without (%u)\n", i
);
1848 static void test_LookupAccountSid(void)
1850 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1851 CHAR accountA
[MAX_PATH
], domainA
[MAX_PATH
], usernameA
[MAX_PATH
];
1852 DWORD acc_sizeA
, dom_sizeA
, user_sizeA
;
1853 DWORD real_acc_sizeA
, real_dom_sizeA
;
1854 WCHAR accountW
[MAX_PATH
], domainW
[MAX_PATH
];
1855 DWORD acc_sizeW
, dom_sizeW
;
1856 DWORD real_acc_sizeW
, real_dom_sizeW
;
1857 PSID pUsersSid
= NULL
;
1860 DWORD error
, size
, cbti
= 0;
1865 PTOKEN_USER ptiUser
= NULL
;
1867 /* native windows crashes if account size, domain size, or name use is NULL */
1869 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1870 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &pUsersSid
);
1871 ok(ret
|| (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
),
1872 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1874 /* not running on NT so give up */
1875 if (!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1878 real_acc_sizeA
= MAX_PATH
;
1879 real_dom_sizeA
= MAX_PATH
;
1880 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1881 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1883 /* try NULL account */
1884 acc_sizeA
= MAX_PATH
;
1885 dom_sizeA
= MAX_PATH
;
1886 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1887 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1889 /* try NULL domain */
1890 acc_sizeA
= MAX_PATH
;
1891 dom_sizeA
= MAX_PATH
;
1892 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1893 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1895 /* try a small account buffer */
1897 dom_sizeA
= MAX_PATH
;
1899 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1900 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1901 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1902 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1904 /* try a 0 sized account buffer */
1906 dom_sizeA
= MAX_PATH
;
1908 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1909 /* this can fail or succeed depending on OS version but the size will always be returned */
1910 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1911 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1912 real_acc_sizeA
+ 1, acc_sizeA
);
1914 /* try a 0 sized account buffer */
1916 dom_sizeA
= MAX_PATH
;
1917 LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1918 /* this can fail or succeed depending on OS version but the size will always be returned */
1919 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1920 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1921 real_acc_sizeA
+ 1, acc_sizeA
);
1923 /* try a small domain buffer */
1925 acc_sizeA
= MAX_PATH
;
1927 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1928 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1929 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1930 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1932 /* try a 0 sized domain buffer */
1934 acc_sizeA
= MAX_PATH
;
1936 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1937 /* this can fail or succeed depending on OS version but the size will always be returned */
1938 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1939 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1940 real_dom_sizeA
+ 1, dom_sizeA
);
1942 /* try a 0 sized domain buffer */
1944 acc_sizeA
= MAX_PATH
;
1945 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1946 /* this can fail or succeed depending on OS version but the size will always be returned */
1947 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1948 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1949 real_dom_sizeA
+ 1, dom_sizeA
);
1951 real_acc_sizeW
= MAX_PATH
;
1952 real_dom_sizeW
= MAX_PATH
;
1953 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &real_acc_sizeW
, domainW
, &real_dom_sizeW
, &use
);
1954 ok(ret
, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1956 /* try an invalid system name */
1957 real_acc_sizeA
= MAX_PATH
;
1958 real_dom_sizeA
= MAX_PATH
;
1959 ret
= LookupAccountSidA("deepthought", pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1960 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1961 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
1962 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1964 /* native windows crashes if domainW or accountW is NULL */
1966 /* try a small account buffer */
1968 dom_sizeW
= MAX_PATH
;
1970 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1971 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1972 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1973 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1975 /* try a 0 sized account buffer */
1977 dom_sizeW
= MAX_PATH
;
1979 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1980 /* this can fail or succeed depending on OS version but the size will always be returned */
1981 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1982 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1983 real_acc_sizeW
+ 1, acc_sizeW
);
1985 /* try a 0 sized account buffer */
1987 dom_sizeW
= MAX_PATH
;
1988 LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1989 /* this can fail or succeed depending on OS version but the size will always be returned */
1990 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1991 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1992 real_acc_sizeW
+ 1, acc_sizeW
);
1994 /* try a small domain buffer */
1996 acc_sizeW
= MAX_PATH
;
1998 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1999 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
2000 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2001 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
2003 /* try a 0 sized domain buffer */
2005 acc_sizeW
= MAX_PATH
;
2007 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
2008 /* this can fail or succeed depending on OS version but the size will always be returned */
2009 ok(dom_sizeW
== real_dom_sizeW
+ 1,
2010 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
2011 real_dom_sizeW
+ 1, dom_sizeW
);
2013 /* try a 0 sized domain buffer */
2015 acc_sizeW
= MAX_PATH
;
2016 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
2017 /* this can fail or succeed depending on OS version but the size will always be returned */
2018 ok(dom_sizeW
== real_dom_sizeW
+ 1,
2019 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
2020 real_dom_sizeW
+ 1, dom_sizeW
);
2022 acc_sizeW
= dom_sizeW
= use
= 0;
2023 SetLastError(0xdeadbeef);
2024 ret
= LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
2025 error
= GetLastError();
2026 ok(!ret
, "LookupAccountSidW failed %u\n", GetLastError());
2027 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
2028 ok(acc_sizeW
, "expected non-zero account size\n");
2029 ok(dom_sizeW
, "expected non-zero domain size\n");
2030 ok(!use
, "expected zero use %u\n", use
);
2034 /* Test LookupAccountSid with Sid retrieved from token information.
2035 This assumes this process is running under the account of the current user.*/
2036 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &hToken
);
2037 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2038 ret
= GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &cbti
);
2039 ok(!ret
, "GetTokenInformation failed with error %d\n", GetLastError());
2040 ptiUser
= HeapAlloc(GetProcessHeap(), 0, cbti
);
2041 if (GetTokenInformation(hToken
, TokenUser
, ptiUser
, cbti
, &cbti
))
2043 acc_sizeA
= dom_sizeA
= MAX_PATH
;
2044 ret
= LookupAccountSidA(NULL
, ptiUser
->User
.Sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
2045 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
2046 user_sizeA
= MAX_PATH
;
2047 ret
= GetUserNameA(usernameA
, &user_sizeA
);
2048 ok(ret
, "GetUserNameA() Expected TRUE, got FALSE\n");
2049 ok(lstrcmpA(usernameA
, accountA
) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA
, accountA
);
2051 HeapFree(GetProcessHeap(), 0, ptiUser
);
2053 if (pCreateWellKnownSid
&& pConvertSidToStringSidA
)
2055 trace("Well Known SIDs:\n");
2056 for (i
= 0; i
<= 60; i
++)
2058 size
= SECURITY_MAX_SID_SIZE
;
2059 if (pCreateWellKnownSid(i
, NULL
, &max_sid
.sid
, &size
))
2061 if (pConvertSidToStringSidA(&max_sid
.sid
, &str_sidA
))
2063 acc_sizeA
= MAX_PATH
;
2064 dom_sizeA
= MAX_PATH
;
2065 if (LookupAccountSidA(NULL
, &max_sid
.sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
))
2066 trace(" %d: %s %s\\%s %d\n", i
, str_sidA
, domainA
, accountA
, use
);
2067 LocalFree(str_sidA
);
2072 if (GetLastError() != ERROR_INVALID_PARAMETER
)
2073 trace(" CreateWellKnownSid(%d) failed: %d\n", i
, GetLastError());
2075 trace(" %d: not supported\n", i
);
2079 pLsaQueryInformationPolicy
= (void *)GetProcAddress( hmod
, "LsaQueryInformationPolicy");
2080 pLsaOpenPolicy
= (void *)GetProcAddress( hmod
, "LsaOpenPolicy");
2081 pLsaFreeMemory
= (void *)GetProcAddress( hmod
, "LsaFreeMemory");
2082 pLsaClose
= (void *)GetProcAddress( hmod
, "LsaClose");
2084 if (pLsaQueryInformationPolicy
&& pLsaOpenPolicy
&& pLsaFreeMemory
&& pLsaClose
)
2088 LSA_OBJECT_ATTRIBUTES object_attributes
;
2090 ZeroMemory(&object_attributes
, sizeof(object_attributes
));
2091 object_attributes
.Length
= sizeof(object_attributes
);
2093 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_ALL_ACCESS
, &handle
);
2094 ok(status
== STATUS_SUCCESS
|| status
== STATUS_ACCESS_DENIED
,
2095 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status
);
2097 /* try a more restricted access mask if necessary */
2098 if (status
== STATUS_ACCESS_DENIED
) {
2099 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
2100 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_VIEW_LOCAL_INFORMATION
, &handle
);
2101 ok(status
== STATUS_SUCCESS
, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status
);
2104 if (status
== STATUS_SUCCESS
)
2106 PPOLICY_ACCOUNT_DOMAIN_INFO info
;
2107 status
= pLsaQueryInformationPolicy(handle
, PolicyAccountDomainInformation
, (PVOID
*)&info
);
2108 ok(status
== STATUS_SUCCESS
, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status
);
2109 if (status
== STATUS_SUCCESS
)
2111 ok(info
->DomainSid
!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
2112 if (info
->DomainSid
)
2114 int count
= *GetSidSubAuthorityCount(info
->DomainSid
);
2115 CopySid(GetSidLengthRequired(count
), &max_sid
, info
->DomainSid
);
2116 test_sid_str((PSID
)&max_sid
.sid
);
2117 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_ADMIN
;
2118 max_sid
.sid
.SubAuthorityCount
= count
+ 1;
2119 test_sid_str((PSID
)&max_sid
.sid
);
2120 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_GUEST
;
2121 test_sid_str((PSID
)&max_sid
.sid
);
2122 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ADMINS
;
2123 test_sid_str((PSID
)&max_sid
.sid
);
2124 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_USERS
;
2125 test_sid_str((PSID
)&max_sid
.sid
);
2126 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_GUESTS
;
2127 test_sid_str((PSID
)&max_sid
.sid
);
2128 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_COMPUTERS
;
2129 test_sid_str((PSID
)&max_sid
.sid
);
2130 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CONTROLLERS
;
2131 test_sid_str((PSID
)&max_sid
.sid
);
2132 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CERT_ADMINS
;
2133 test_sid_str((PSID
)&max_sid
.sid
);
2134 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_SCHEMA_ADMINS
;
2135 test_sid_str((PSID
)&max_sid
.sid
);
2136 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
;
2137 test_sid_str((PSID
)&max_sid
.sid
);
2138 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_POLICY_ADMINS
;
2139 test_sid_str((PSID
)&max_sid
.sid
);
2140 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_ALIAS_RID_RAS_SERVERS
;
2141 test_sid_str((PSID
)&max_sid
.sid
);
2142 max_sid
.sid
.SubAuthority
[count
] = 1000; /* first user account */
2143 test_sid_str((PSID
)&max_sid
.sid
);
2146 pLsaFreeMemory((LPVOID
)info
);
2149 status
= pLsaClose(handle
);
2150 ok(status
== STATUS_SUCCESS
, "LsaClose() failed, returned 0x%08x\n", status
);
2156 static BOOL
get_sid_info(PSID psid
, LPSTR
*user
, LPSTR
*dom
)
2158 static CHAR account
[UNLEN
+ 1];
2159 static CHAR domain
[UNLEN
+ 1];
2160 DWORD size
, dom_size
;
2166 size
= dom_size
= UNLEN
+ 1;
2169 SetLastError(0xdeadbeef);
2170 return LookupAccountSidA(NULL
, psid
, account
, &size
, domain
, &dom_size
, &use
);
2173 static void check_wellknown_name(const char* name
, WELL_KNOWN_SID_TYPE result
)
2175 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
2176 PSID domainsid
= NULL
;
2177 char wk_sid
[SECURITY_MAX_SID_SIZE
];
2180 DWORD sid_size
, domain_size
;
2181 SID_NAME_USE sid_use
;
2182 LPSTR domain
, account
, sid_domain
, wk_domain
, wk_account
;
2188 ret
= LookupAccountNameA(NULL
, name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2189 ok(!ret
, " %s Should have failed to lookup account name\n", name
);
2190 psid
= HeapAlloc(GetProcessHeap(),0,sid_size
);
2191 domain
= HeapAlloc(GetProcessHeap(),0,domain_size
);
2192 ret
= LookupAccountNameA(NULL
, name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2196 ok(!ret
, " %s Should have failed to lookup account name\n",name
);
2200 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
2201 cb
= sizeof(wk_sid
);
2202 if (!pCreateWellKnownSid(result
, domainsid
, wk_sid
, &cb
))
2204 win_skip("SID %i is not available on the system\n",result
);
2208 ret2
= get_sid_info(wk_sid
, &wk_account
, &wk_domain
);
2209 if (!ret2
&& GetLastError() == ERROR_NONE_MAPPED
)
2211 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name
);
2215 get_sid_info(psid
, &account
, &sid_domain
);
2217 ok(ret
, "Failed to lookup account name %s\n",name
);
2218 ok(sid_size
!= 0, "sid_size was zero\n");
2220 ok(EqualSid(psid
,wk_sid
),"%s Sid %s fails to match well known sid %s!\n",
2221 name
, debugstr_sid(psid
), debugstr_sid(wk_sid
));
2223 ok(!lstrcmpA(account
, wk_account
), "Expected %s , got %s\n", account
, wk_account
);
2224 ok(!lstrcmpA(domain
, wk_domain
), "Expected %s, got %s\n", wk_domain
, domain
);
2225 ok(sid_use
== SidTypeWellKnownGroup
, "Expected Use (5), got %d\n", sid_use
);
2229 HeapFree(GetProcessHeap(),0,psid
);
2230 HeapFree(GetProcessHeap(),0,domain
);
2233 static void test_LookupAccountName(void)
2235 DWORD sid_size
, domain_size
, user_size
;
2236 DWORD sid_save
, domain_save
;
2237 CHAR user_name
[UNLEN
+ 1];
2238 CHAR computer_name
[UNLEN
+ 1];
2239 SID_NAME_USE sid_use
;
2240 LPSTR domain
, account
, sid_dom
;
2244 /* native crashes if (assuming all other parameters correct):
2246 * - Sid is NULL and cbSid is > 0
2247 * - cbSid or cchReferencedDomainName are NULL
2248 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2251 user_size
= UNLEN
+ 1;
2252 SetLastError(0xdeadbeef);
2253 ret
= GetUserNameA(user_name
, &user_size
);
2254 ok(ret
, "Failed to get user name : %d\n", GetLastError());
2259 sid_use
= 0xcafebabe;
2260 SetLastError(0xdeadbeef);
2261 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2262 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2264 win_skip("LookupAccountNameA is not implemented\n");
2267 ok(!ret
, "Expected 0, got %d\n", ret
);
2268 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2269 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2270 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2271 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2272 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2274 sid_save
= sid_size
;
2275 domain_save
= domain_size
;
2277 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2278 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2280 /* try valid account name */
2281 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2282 get_sid_info(psid
, &account
, &sid_dom
);
2283 ok(ret
, "Failed to lookup account name\n");
2284 ok(sid_size
== GetLengthSid(psid
), "Expected %d, got %d\n", GetLengthSid(psid
), sid_size
);
2285 ok(!lstrcmpA(account
, user_name
), "Expected %s, got %s\n", user_name
, account
);
2286 ok(!lstrcmpiA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2287 ok(domain_size
== domain_save
- 1, "Expected %d, got %d\n", domain_save
- 1, domain_size
);
2288 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2289 ok(sid_use
== SidTypeUser
, "Expected SidTypeUser (%d), got %d\n", SidTypeUser
, sid_use
);
2290 domain_size
= domain_save
;
2291 sid_size
= sid_save
;
2293 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2295 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2299 ret
= LookupAccountNameA(NULL
, "Everyone", psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2300 get_sid_info(psid
, &account
, &sid_dom
);
2301 ok(ret
, "Failed to lookup account name\n");
2302 ok(sid_size
!= 0, "sid_size was zero\n");
2303 ok(!lstrcmpA(account
, "Everyone"), "Expected Everyone, got %s\n", account
);
2304 ok(!lstrcmpiA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2305 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2306 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2307 ok(sid_use
== SidTypeWellKnownGroup
, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup
, sid_use
);
2308 domain_size
= domain_save
;
2311 /* NULL Sid with zero sid size */
2312 SetLastError(0xdeadbeef);
2314 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2315 ok(!ret
, "Expected 0, got %d\n", ret
);
2316 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2317 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2318 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2319 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2321 /* try cchReferencedDomainName - 1 */
2322 SetLastError(0xdeadbeef);
2324 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2325 ok(!ret
, "Expected 0, got %d\n", ret
);
2326 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2327 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2328 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2329 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2331 /* NULL ReferencedDomainName with zero domain name size */
2332 SetLastError(0xdeadbeef);
2334 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2335 ok(!ret
, "Expected 0, got %d\n", ret
);
2336 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2337 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2338 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2339 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2341 HeapFree(GetProcessHeap(), 0, psid
);
2342 HeapFree(GetProcessHeap(), 0, domain
);
2344 /* get sizes for NULL account name */
2347 sid_use
= 0xcafebabe;
2348 SetLastError(0xdeadbeef);
2349 ret
= LookupAccountNameA(NULL
, NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2350 if (!ret
&& GetLastError() == ERROR_NONE_MAPPED
)
2351 win_skip("NULL account name doesn't work on NT4\n");
2354 ok(!ret
, "Expected 0, got %d\n", ret
);
2355 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2356 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2357 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2358 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2359 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2361 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2362 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2364 /* try NULL account name */
2365 ret
= LookupAccountNameA(NULL
, NULL
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2366 get_sid_info(psid
, &account
, &sid_dom
);
2367 ok(ret
, "Failed to lookup account name\n");
2368 /* Using a fixed string will not work on different locales */
2369 ok(!lstrcmpiA(account
, domain
),
2370 "Got %s for account and %s for domain, these should be the same\n", account
, domain
);
2371 ok(sid_use
== SidTypeDomain
, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain
, sid_use
);
2373 HeapFree(GetProcessHeap(), 0, psid
);
2374 HeapFree(GetProcessHeap(), 0, domain
);
2377 /* try an invalid account name */
2378 SetLastError(0xdeadbeef);
2381 ret
= LookupAccountNameA(NULL
, "oogabooga", NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2382 ok(!ret
, "Expected 0, got %d\n", ret
);
2383 ok(GetLastError() == ERROR_NONE_MAPPED
||
2384 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
),
2385 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2386 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2387 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2389 /* try an invalid system name */
2390 SetLastError(0xdeadbeef);
2393 ret
= LookupAccountNameA("deepthought", NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2394 ok(!ret
, "Expected 0, got %d\n", ret
);
2395 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
2396 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2397 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2398 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2400 /* try with the computer name as the account name */
2401 domain_size
= sizeof(computer_name
);
2402 GetComputerNameA(computer_name
, &domain_size
);
2405 ret
= LookupAccountNameA(NULL
, computer_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2406 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
||
2407 GetLastError() == ERROR_NONE_MAPPED
/* in a domain */ ||
2408 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE
) ||
2409 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
)),
2410 "LookupAccountNameA failed: %d\n", GetLastError());
2411 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2413 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2414 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2415 ret
= LookupAccountNameA(NULL
, computer_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2416 ok(ret
, "LookupAccountNameA failed: %d\n", GetLastError());
2417 ok(sid_use
== SidTypeDomain
||
2418 (sid_use
== SidTypeUser
&& ! strcmp(computer_name
, user_name
)), "expected SidTypeDomain for %s, got %d\n", computer_name
, sid_use
);
2419 HeapFree(GetProcessHeap(), 0, domain
);
2420 HeapFree(GetProcessHeap(), 0, psid
);
2423 /* Well Known names */
2424 if (!pCreateWellKnownSid
)
2426 win_skip("CreateWellKnownSid not available\n");
2430 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2432 skip("Non-English locale (skipping well known name creation tests)\n");
2436 check_wellknown_name("LocalService", WinLocalServiceSid
);
2437 check_wellknown_name("Local Service", WinLocalServiceSid
);
2439 check_wellknown_name("Local Service", 0);
2440 check_wellknown_name("NetworkService", WinNetworkServiceSid
);
2441 check_wellknown_name("Network Service", WinNetworkServiceSid
);
2443 /* example of some names where the spaces are not optional */
2444 check_wellknown_name("Terminal Server User", WinTerminalServerSid
);
2445 check_wellknown_name("TerminalServer User", 0);
2446 check_wellknown_name("TerminalServerUser", 0);
2447 check_wellknown_name("Terminal ServerUser", 0);
2449 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid
);
2450 check_wellknown_name("enterprisedomain controllers", 0);
2451 check_wellknown_name("enterprise domaincontrollers", 0);
2452 check_wellknown_name("enterprisedomaincontrollers", 0);
2454 /* case insensitivity */
2455 check_wellknown_name("lOCAlServICE", WinLocalServiceSid
);
2457 /* fully qualified account names */
2458 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid
);
2459 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid
);
2460 check_wellknown_name("nt authority test\\Network Service", 0);
2461 check_wellknown_name("Dummy\\Network Service", 0);
2462 check_wellknown_name("ntauthority\\Network Service", 0);
2465 static void test_security_descriptor(void)
2467 SECURITY_DESCRIPTOR sd
;
2470 BOOL isDefault
, isPresent
, ret
;
2474 SetLastError(0xdeadbeef);
2475 ret
= InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
2476 if (ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2478 win_skip("InitializeSecurityDescriptor is not implemented\n");
2482 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2483 expect_eq(psid
, NULL
, PSID
, "%p");
2484 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2485 sd
.Control
|= SE_DACL_PRESENT
| SE_SACL_PRESENT
;
2487 SetLastError(0xdeadbeef);
2489 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), FALSE
, BOOL
, "%d");
2490 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER
, DWORD
, "%u");
2491 ok(size
> 5, "Size not increased\n");
2494 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), TRUE
, BOOL
, "%d");
2495 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2496 expect_eq(psid
, NULL
, PSID
, "%p");
2497 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2498 ok(GetSecurityDescriptorGroup(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorGroup failed\n");
2499 expect_eq(psid
, NULL
, PSID
, "%p");
2500 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2501 ok(GetSecurityDescriptorDacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorDacl failed\n");
2502 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2503 expect_eq(psid
, NULL
, PSID
, "%p");
2504 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2505 ok(GetSecurityDescriptorSacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorSacl failed\n");
2506 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2507 expect_eq(psid
, NULL
, PSID
, "%p");
2508 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2512 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2513 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2514 static void test_granted_access(HANDLE handle
, ACCESS_MASK access
,
2515 ACCESS_MASK alt
, int line
)
2517 OBJECT_BASIC_INFORMATION obj_info
;
2520 if (!pNtQueryObject
)
2522 skip_(__FILE__
, line
)("Not NT platform - skipping tests\n");
2526 status
= pNtQueryObject( handle
, ObjectBasicInformation
, &obj_info
,
2527 sizeof(obj_info
), NULL
);
2528 ok_(__FILE__
, line
)(!status
, "NtQueryObject with err: %08x\n", status
);
2530 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
||
2531 obj_info
.GrantedAccess
== alt
, "Granted access should be 0x%08x "
2532 "or 0x%08x, instead of 0x%08x\n", access
, alt
, obj_info
.GrantedAccess
);
2534 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
, "Granted access should "
2535 "be 0x%08x, instead of 0x%08x\n", access
, obj_info
.GrantedAccess
);
2538 #define CHECK_SET_SECURITY(o,i,e) \
2542 SetLastError( 0xdeadbeef ); \
2543 res_ = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2544 err = GetLastError(); \
2545 if (e == ERROR_SUCCESS) \
2546 ok(res_, "SetKernelObjectSecurity failed with %d\n", err); \
2548 ok(!res_ && err == e, "SetKernelObjectSecurity should have failed " \
2549 "with %s, instead of %d\n", #e, err); \
2552 static void test_process_security(void)
2556 PTOKEN_PRIMARY_GROUP group
;
2557 PSID AdminSid
= NULL
, UsersSid
= NULL
;
2559 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
2560 char buffer
[MAX_PATH
];
2561 PROCESS_INFORMATION info
;
2562 STARTUPINFOA startup
;
2563 SECURITY_ATTRIBUTES psa
;
2564 HANDLE token
, event
;
2566 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2567 PSID EveryoneSid
= NULL
;
2569 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
2570 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
2571 if (!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2573 win_skip("ACLs not implemented - skipping tests\n");
2574 HeapFree(GetProcessHeap(), 0, Acl
);
2577 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2579 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2580 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2582 /* get owner from the token we might be running as a user not admin */
2583 res
= OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED
, &token
);
2584 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
2587 HeapFree(GetProcessHeap(), 0, Acl
);
2591 res
= GetTokenInformation( token
, TokenOwner
, NULL
, 0, &size
);
2592 ok(!res
, "Expected failure, got %d\n", res
);
2593 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2594 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2596 owner
= HeapAlloc(GetProcessHeap(), 0, size
);
2597 res
= GetTokenInformation( token
, TokenOwner
, owner
, size
, &size
);
2598 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2599 AdminSid
= ((TOKEN_OWNER
*)owner
)->Owner
;
2601 res
= GetTokenInformation( token
, TokenPrimaryGroup
, NULL
, 0, &size
);
2602 ok(!res
, "Expected failure, got %d\n", res
);
2603 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2604 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2606 group
= HeapAlloc(GetProcessHeap(), 0, size
);
2607 res
= GetTokenInformation( token
, TokenPrimaryGroup
, group
, size
, &size
);
2608 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2609 UsersSid
= ((TOKEN_PRIMARY_GROUP
*)group
)->PrimaryGroup
;
2611 CloseHandle( token
);
2614 HeapFree(GetProcessHeap(), 0, group
);
2615 HeapFree(GetProcessHeap(), 0, owner
);
2616 HeapFree(GetProcessHeap(), 0, Acl
);
2620 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, PROCESS_VM_READ
, AdminSid
);
2621 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2622 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, PROCESS_ALL_ACCESS
, AdminSid
);
2623 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2625 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
2626 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
2627 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2629 event
= CreateEventA( NULL
, TRUE
, TRUE
, "test_event" );
2630 ok(event
!= NULL
, "CreateEvent %d\n", GetLastError());
2632 SecurityDescriptor
->Revision
= 0;
2633 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_UNKNOWN_REVISION
);
2634 SecurityDescriptor
->Revision
= SECURITY_DESCRIPTOR_REVISION
;
2636 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2637 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2638 CHECK_SET_SECURITY( event
, SACL_SECURITY_INFORMATION
, ERROR_ACCESS_DENIED
);
2639 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2640 /* NULL DACL is valid and means that everyone has access */
2641 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
;
2642 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2644 /* Set owner and group and dacl */
2645 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
2646 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2647 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2648 test_owner_equal( event
, AdminSid
, __LINE__
);
2650 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, EveryoneSid
, FALSE
);
2651 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2652 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2653 test_group_equal( event
, EveryoneSid
, __LINE__
);
2655 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2656 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2657 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2658 /* setting a dacl should not change the owner or group */
2659 test_owner_equal( event
, AdminSid
, __LINE__
);
2660 test_group_equal( event
, EveryoneSid
, __LINE__
);
2662 /* Test again with a different SID in case the previous SID also happens to
2663 * be the one that is incorrectly replacing the group. */
2664 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, FALSE
);
2665 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2666 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2667 test_group_equal( event
, UsersSid
, __LINE__
);
2669 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2670 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2671 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2672 test_group_equal( event
, UsersSid
, __LINE__
);
2674 sprintf(buffer
, "%s tests/security.c test", myARGV
[0]);
2675 memset(&startup
, 0, sizeof(startup
));
2676 startup
.cb
= sizeof(startup
);
2677 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
2678 startup
.wShowWindow
= SW_SHOWNORMAL
;
2680 psa
.nLength
= sizeof(psa
);
2681 psa
.lpSecurityDescriptor
= SecurityDescriptor
;
2682 psa
.bInheritHandle
= TRUE
;
2684 /* Doesn't matter what ACL say we should get full access for ourselves */
2685 res
= CreateProcessA( NULL
, buffer
, &psa
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
2686 ok(res
, "CreateProcess with err:%d\n", GetLastError());
2687 TEST_GRANTED_ACCESS2( info
.hProcess
, PROCESS_ALL_ACCESS_NT4
,
2688 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2689 winetest_wait_child_process( info
.hProcess
);
2691 FreeSid(EveryoneSid
);
2692 CloseHandle( info
.hProcess
);
2693 CloseHandle( info
.hThread
);
2694 CloseHandle( event
);
2695 HeapFree(GetProcessHeap(), 0, group
);
2696 HeapFree(GetProcessHeap(), 0, owner
);
2697 HeapFree(GetProcessHeap(), 0, Acl
);
2698 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
2701 static void test_process_security_child(void)
2703 HANDLE handle
, handle1
;
2707 handle
= OpenProcess( PROCESS_TERMINATE
, FALSE
, GetCurrentProcessId() );
2708 ok(handle
!= NULL
, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2709 TEST_GRANTED_ACCESS( handle
, PROCESS_TERMINATE
);
2711 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2712 &handle1
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2713 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2714 TEST_GRANTED_ACCESS( handle1
, PROCESS_TERMINATE
);
2716 CloseHandle( handle1
);
2718 SetLastError( 0xdeadbeef );
2719 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2720 &handle1
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2721 err
= GetLastError();
2723 ok(!ret
&& err
== ERROR_ACCESS_DENIED
, "duplicating handle should have failed "
2724 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err
);
2726 CloseHandle( handle
);
2728 /* These two should fail - they are denied by ACL */
2729 handle
= OpenProcess( PROCESS_VM_READ
, FALSE
, GetCurrentProcessId() );
2731 ok(handle
== NULL
, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2732 handle
= OpenProcess( PROCESS_ALL_ACCESS
, FALSE
, GetCurrentProcessId() );
2734 ok(handle
== NULL
, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2736 /* Documented privilege elevation */
2737 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2738 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2739 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2740 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2741 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2743 CloseHandle( handle
);
2745 /* Same only explicitly asking for all access rights */
2746 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2747 &handle
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2748 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2749 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2750 PROCESS_ALL_ACCESS
| PROCESS_QUERY_LIMITED_INFORMATION
);
2751 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2752 &handle1
, PROCESS_VM_READ
, TRUE
, 0 );
2753 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2754 TEST_GRANTED_ACCESS( handle1
, PROCESS_VM_READ
);
2755 CloseHandle( handle1
);
2756 CloseHandle( handle
);
2759 static void test_impersonation_level(void)
2761 HANDLE Token
, ProcessToken
;
2764 TOKEN_PRIVILEGES
*Privileges
;
2766 PRIVILEGE_SET
*PrivilegeSet
;
2772 pDuplicateTokenEx
= (void *)GetProcAddress(hmod
, "DuplicateTokenEx");
2773 if( !pDuplicateTokenEx
) {
2774 win_skip("DuplicateTokenEx is not available\n");
2777 SetLastError(0xdeadbeef);
2778 ret
= ImpersonateSelf(SecurityAnonymous
);
2779 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2781 win_skip("ImpersonateSelf is not implemented\n");
2784 ok(ret
, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2785 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2786 ok(!ret
, "OpenThreadToken should have failed\n");
2787 error
= GetLastError();
2788 ok(error
== ERROR_CANT_OPEN_ANONYMOUS
, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2789 /* can't perform access check when opening object against an anonymous impersonation token */
2791 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2792 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_CANT_OPEN_ANONYMOUS
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2793 "RegOpenKeyEx failed with %d\n", error
);
2797 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
2798 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2800 ret
= pDuplicateTokenEx(ProcessToken
,
2801 TOKEN_QUERY
| TOKEN_DUPLICATE
| TOKEN_IMPERSONATE
, NULL
,
2802 SecurityAnonymous
, TokenImpersonation
, &Token
);
2803 ok(ret
, "DuplicateTokenEx failed with error %d\n", GetLastError());
2804 /* can't increase the impersonation level */
2805 ret
= DuplicateToken(Token
, SecurityIdentification
, &Token2
);
2806 error
= GetLastError();
2807 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2808 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2809 /* we can query anything from an anonymous token, including the user */
2810 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
2811 error
= GetLastError();
2812 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2813 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
2814 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
2815 ok(ret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2816 HeapFree(GetProcessHeap(), 0, User
);
2818 /* PrivilegeCheck fails with SecurityAnonymous level */
2819 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
2820 error
= GetLastError();
2821 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2822 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
2823 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
2824 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2826 PrivilegeSet
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[Privileges
->PrivilegeCount
]));
2827 PrivilegeSet
->PrivilegeCount
= Privileges
->PrivilegeCount
;
2828 memcpy(PrivilegeSet
->Privilege
, Privileges
->Privileges
, PrivilegeSet
->PrivilegeCount
* sizeof(PrivilegeSet
->Privilege
[0]));
2829 PrivilegeSet
->Control
= PRIVILEGE_SET_ALL_NECESSARY
;
2830 HeapFree(GetProcessHeap(), 0, Privileges
);
2832 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2833 error
= GetLastError();
2834 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2838 ret
= ImpersonateSelf(SecurityIdentification
);
2839 ok(ret
, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2840 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2841 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2843 /* can't perform access check when opening object against an identification impersonation token */
2844 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2846 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2847 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2849 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2850 ok(ret
, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2854 ret
= ImpersonateSelf(SecurityImpersonation
);
2855 ok(ret
, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2856 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2857 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2858 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2859 ok(error
== ERROR_SUCCESS
, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error
);
2861 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2862 ok(ret
, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2866 CloseHandle(ProcessToken
);
2868 HeapFree(GetProcessHeap(), 0, PrivilegeSet
);
2871 static void test_SetEntriesInAclW(void)
2874 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2875 PACL OldAcl
= NULL
, NewAcl
;
2876 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2877 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2878 EXPLICIT_ACCESSW ExplicitAccess
;
2879 static const WCHAR wszEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2880 static const WCHAR wszCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2882 if (!pSetEntriesInAclW
)
2884 win_skip("SetEntriesInAclW is not available\n");
2888 NewAcl
= (PACL
)0xdeadbeef;
2889 res
= pSetEntriesInAclW(0, NULL
, NULL
, &NewAcl
);
2890 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2892 win_skip("SetEntriesInAclW is not implemented\n");
2895 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2896 ok(NewAcl
== NULL
||
2897 broken(NewAcl
!= NULL
), /* NT4 */
2898 "NewAcl=%p, expected NULL\n", NewAcl
);
2901 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2902 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2903 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2905 win_skip("ACLs not implemented - skipping tests\n");
2906 HeapFree(GetProcessHeap(), 0, OldAcl
);
2909 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2911 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2912 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2914 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2915 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2916 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2918 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2919 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2921 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2922 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2923 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2924 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2925 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2926 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2927 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= 0xDEADBEEF;
2928 ExplicitAccess
.Trustee
.pMultipleTrustee
= (PVOID
)0xDEADBEEF;
2929 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2930 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2931 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2934 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2935 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2936 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2937 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2938 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2939 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2942 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2944 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2948 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2949 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszEveryone
;
2950 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2951 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2952 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2955 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2956 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2957 ok(res
== ERROR_INVALID_PARAMETER
||
2958 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2959 "SetEntriesInAclW failed: %u\n", res
);
2960 ok(NewAcl
== NULL
||
2961 broken(NewAcl
!= NULL
), /* NT4 */
2962 "returned acl wasn't NULL: %p\n", NewAcl
);
2964 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2965 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2966 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2967 ok(res
== ERROR_INVALID_PARAMETER
||
2968 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2969 "SetEntriesInAclW failed: %u\n", res
);
2970 ok(NewAcl
== NULL
||
2971 broken(NewAcl
!= NULL
), /* NT4 */
2972 "returned acl wasn't NULL: %p\n", NewAcl
);
2974 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2975 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2976 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2977 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2978 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2982 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2983 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszCurrentUser
;
2984 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2985 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2986 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2989 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
2990 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2991 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
2992 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2993 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2994 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2998 FreeSid(EveryoneSid
);
2999 HeapFree(GetProcessHeap(), 0, OldAcl
);
3002 static void test_SetEntriesInAclA(void)
3005 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
3006 PACL OldAcl
= NULL
, NewAcl
;
3007 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
3008 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
3009 EXPLICIT_ACCESSA ExplicitAccess
;
3010 static const CHAR szEveryone
[] = {'E','v','e','r','y','o','n','e',0};
3011 static const CHAR szCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
3013 if (!pSetEntriesInAclA
)
3015 win_skip("SetEntriesInAclA is not available\n");
3019 NewAcl
= (PACL
)0xdeadbeef;
3020 res
= pSetEntriesInAclA(0, NULL
, NULL
, &NewAcl
);
3021 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
3023 win_skip("SetEntriesInAclA is not implemented\n");
3026 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3027 ok(NewAcl
== NULL
||
3028 broken(NewAcl
!= NULL
), /* NT4 */
3029 "NewAcl=%p, expected NULL\n", NewAcl
);
3032 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
3033 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
3034 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3036 win_skip("ACLs not implemented - skipping tests\n");
3037 HeapFree(GetProcessHeap(), 0, OldAcl
);
3040 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
3042 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
3043 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3045 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
3046 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
3047 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3049 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
3050 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
3052 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
3053 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
3054 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
3055 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
3056 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
3057 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
3058 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
3059 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
3060 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3061 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3062 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3065 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
3066 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
3067 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
3068 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3069 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3070 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3073 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
3075 skip("Non-English locale (test with hardcoded 'Everyone')\n");
3079 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3080 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szEveryone
;
3081 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3082 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3083 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3086 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
3087 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3088 ok(res
== ERROR_INVALID_PARAMETER
||
3089 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
3090 "SetEntriesInAclA failed: %u\n", res
);
3091 ok(NewAcl
== NULL
||
3092 broken(NewAcl
!= NULL
), /* NT4 */
3093 "returned acl wasn't NULL: %p\n", NewAcl
);
3095 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3096 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
3097 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3098 ok(res
== ERROR_INVALID_PARAMETER
||
3099 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
3100 "SetEntriesInAclA failed: %u\n", res
);
3101 ok(NewAcl
== NULL
||
3102 broken(NewAcl
!= NULL
), /* NT4 */
3103 "returned acl wasn't NULL: %p\n", NewAcl
);
3105 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
3106 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
3107 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3108 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3109 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3113 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3114 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szCurrentUser
;
3115 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3116 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3117 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3120 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
3121 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
3122 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
3123 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3124 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3125 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3129 FreeSid(EveryoneSid
);
3130 HeapFree(GetProcessHeap(), 0, OldAcl
);
3133 /* helper function for test_CreateDirectoryA */
3134 static void get_nt_pathW(const char *name
, UNICODE_STRING
*nameW
)
3136 UNICODE_STRING strW
;
3141 pRtlInitAnsiString(&str
, name
);
3143 status
= pRtlAnsiStringToUnicodeString(&strW
, &str
, TRUE
);
3144 ok(!status
, "RtlAnsiStringToUnicodeString failed with %08x\n", status
);
3146 ret
= pRtlDosPathNameToNtPathName_U(strW
.Buffer
, nameW
, NULL
, NULL
);
3147 ok(ret
, "RtlDosPathNameToNtPathName_U failed\n");
3149 pRtlFreeUnicodeString(&strW
);
3152 static void test_inherited_dacl(PACL dacl
, PSID admin_sid
, PSID user_sid
, DWORD flags
, DWORD mask
,
3153 BOOL todo_count
, BOOL todo_sid
, BOOL todo_flags
, int line
)
3155 ACL_SIZE_INFORMATION acl_size
;
3156 ACCESS_ALLOWED_ACE
*ace
;
3159 bret
= pGetAclInformation(dacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3160 ok_(__FILE__
, line
)(bret
, "GetAclInformation failed\n");
3164 ok_(__FILE__
, line
)(acl_size
.AceCount
== 2,
3165 "GetAclInformation returned unexpected entry count (%d != 2)\n",
3168 ok_(__FILE__
, line
)(acl_size
.AceCount
== 2,
3169 "GetAclInformation returned unexpected entry count (%d != 2)\n",
3172 if (acl_size
.AceCount
> 0)
3174 bret
= pGetAce(dacl
, 0, (VOID
**)&ace
);
3175 ok_(__FILE__
, line
)(bret
, "Failed to get Current User ACE\n");
3177 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3180 ok_(__FILE__
, line
)(bret
, "Current User ACE (%s) != Current User SID (%s)\n", debugstr_sid(&ace
->SidStart
), debugstr_sid(user_sid
));
3182 ok_(__FILE__
, line
)(bret
, "Current User ACE (%s) != Current User SID (%s)\n", debugstr_sid(&ace
->SidStart
), debugstr_sid(user_sid
));
3186 ok_(__FILE__
, line
)(((ACE_HEADER
*)ace
)->AceFlags
== flags
,
3187 "Current User ACE has unexpected flags (0x%x != 0x%x)\n",
3188 ((ACE_HEADER
*)ace
)->AceFlags
, flags
);
3190 ok_(__FILE__
, line
)(((ACE_HEADER
*)ace
)->AceFlags
== flags
,
3191 "Current User ACE has unexpected flags (0x%x != 0x%x)\n",
3192 ((ACE_HEADER
*)ace
)->AceFlags
, flags
);
3194 ok_(__FILE__
, line
)(ace
->Mask
== mask
,
3195 "Current User ACE has unexpected mask (0x%x != 0x%x)\n",
3198 if (acl_size
.AceCount
> 1)
3200 bret
= pGetAce(dacl
, 1, (VOID
**)&ace
);
3201 ok_(__FILE__
, line
)(bret
, "Failed to get Administators Group ACE\n");
3203 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3206 ok_(__FILE__
, line
)(bret
, "Administators Group ACE (%s) != Administators Group SID (%s)\n", debugstr_sid(&ace
->SidStart
), debugstr_sid(admin_sid
));
3208 ok_(__FILE__
, line
)(bret
, "Administators Group ACE (%s) != Administators Group SID (%s)\n", debugstr_sid(&ace
->SidStart
), debugstr_sid(admin_sid
));
3212 ok_(__FILE__
, line
)(((ACE_HEADER
*)ace
)->AceFlags
== flags
,
3213 "Administators Group ACE has unexpected flags (0x%x != 0x%x)\n",
3214 ((ACE_HEADER
*)ace
)->AceFlags
, flags
);
3216 ok_(__FILE__
, line
)(((ACE_HEADER
*)ace
)->AceFlags
== flags
,
3217 "Administators Group ACE has unexpected flags (0x%x != 0x%x)\n",
3218 ((ACE_HEADER
*)ace
)->AceFlags
, flags
);
3220 ok_(__FILE__
, line
)(ace
->Mask
== mask
,
3221 "Administators Group ACE has unexpected mask (0x%x != 0x%x)\n",
3226 static void test_CreateDirectoryA(void)
3228 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3229 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3230 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
3231 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3232 PSECURITY_DESCRIPTOR pSD
= &sd
;
3233 ACL_SIZE_INFORMATION acl_size
;
3234 UNICODE_STRING tmpfileW
;
3235 SECURITY_ATTRIBUTES sa
;
3236 OBJECT_ATTRIBUTES attr
;
3237 char tmpfile
[MAX_PATH
];
3238 char tmpdir
[MAX_PATH
];
3239 HANDLE token
, hTemp
;
3247 if (!pGetSecurityInfo
|| !pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3249 win_skip("Required functions are not available\n");
3253 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3255 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3256 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3260 win_skip("Failed to get current user token\n");
3263 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3264 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3265 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3266 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3267 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3268 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3269 CloseHandle( token
);
3270 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3272 sa
.nLength
= sizeof(sa
);
3273 sa
.lpSecurityDescriptor
= pSD
;
3274 sa
.bInheritHandle
= TRUE
;
3275 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3276 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3277 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3278 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3279 ok(bret
, "Failed to initialize ACL.\n");
3280 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3281 GENERIC_ALL
, user_sid
);
3282 ok(bret
, "Failed to add Current User to ACL.\n");
3283 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3284 GENERIC_ALL
, admin_sid
);
3285 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3286 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3287 ok(bret
, "Failed to add ACL to security desciptor.\n");
3289 GetTempPathA(MAX_PATH
, tmpdir
);
3290 lstrcatA(tmpdir
, "Please Remove Me");
3291 bret
= CreateDirectoryA(tmpdir
, &sa
);
3292 ok(bret
== TRUE
, "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
3293 HeapFree(GetProcessHeap(), 0, pDacl
);
3295 SetLastError(0xdeadbeef);
3296 error
= pGetNamedSecurityInfoA(tmpdir
, SE_FILE_OBJECT
,
3297 OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, (PSID
*)&owner
,
3298 NULL
, &pDacl
, NULL
, &pSD
);
3299 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3301 win_skip("GetNamedSecurityInfoA is not implemented\n");
3304 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3305 test_inherited_dacl(pDacl
, admin_sid
, user_sid
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3306 0x1f01ff, FALSE
, TRUE
, FALSE
, __LINE__
);
3309 /* Test inheritance of ACLs in CreateFile without security descriptor */
3310 strcpy(tmpfile
, tmpdir
);
3311 lstrcatA(tmpfile
, "/tmpfile");
3313 hTemp
= CreateFileA(tmpfile
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3314 CREATE_NEW
, FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
3315 ok(hTemp
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError());
3317 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
,
3318 OWNER_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3319 (PSID
*)&owner
, NULL
, &pDacl
, NULL
, &pSD
);
3320 ok(error
== ERROR_SUCCESS
, "Failed to get permissions on file\n");
3321 test_inherited_dacl(pDacl
, admin_sid
, user_sid
, INHERITED_ACE
,
3322 0x1f01ff, TRUE
, TRUE
, TRUE
, __LINE__
);
3326 /* Test inheritance of ACLs in CreateFile with security descriptor -
3327 * When a security descriptor is set, then inheritance doesn't take effect */
3329 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3330 pDacl
= HeapAlloc(GetProcessHeap(), 0, sizeof(ACL
));
3331 bret
= InitializeAcl(pDacl
, sizeof(ACL
), ACL_REVISION
);
3332 ok(bret
, "Failed to initialize ACL\n");
3333 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3334 ok(bret
, "Failed to add ACL to security desciptor\n");
3336 strcpy(tmpfile
, tmpdir
);
3337 lstrcatA(tmpfile
, "/tmpfile");
3339 sa
.nLength
= sizeof(sa
);
3340 sa
.lpSecurityDescriptor
= pSD
;
3341 sa
.bInheritHandle
= TRUE
;
3342 hTemp
= CreateFileA(tmpfile
, GENERIC_WRITE
, FILE_SHARE_READ
, &sa
,
3343 CREATE_NEW
, FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
3344 ok(hTemp
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError());
3345 HeapFree(GetProcessHeap(), 0, pDacl
);
3347 error
= pGetSecurityInfo(hTemp
, SE_FILE_OBJECT
,
3348 OWNER_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3349 (PSID
*)&owner
, NULL
, &pDacl
, NULL
, &pSD
);
3350 ok(error
== ERROR_SUCCESS
, "GetNamedSecurityInfo failed with error %d\n", error
);
3351 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3352 ok(bret
, "GetAclInformation failed\n");
3354 ok(acl_size
.AceCount
== 0, "GetAclInformation returned unexpected entry count (%d != 0).\n",
3358 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
,
3359 OWNER_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3360 (PSID
*)&owner
, NULL
, &pDacl
, NULL
, &pSD
);
3362 ok(error
== ERROR_SUCCESS
, "GetNamedSecurityInfo failed with error %d\n", error
);
3363 if (error
== ERROR_SUCCESS
)
3365 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3366 ok(bret
, "GetAclInformation failed\n");
3368 ok(acl_size
.AceCount
== 0, "GetAclInformation returned unexpected entry count (%d != 0).\n",
3374 /* Test inheritance of ACLs in NtCreateFile without security descriptor */
3375 strcpy(tmpfile
, tmpdir
);
3376 lstrcatA(tmpfile
, "/tmpfile");
3377 get_nt_pathW(tmpfile
, &tmpfileW
);
3379 attr
.Length
= sizeof(attr
);
3380 attr
.RootDirectory
= 0;
3381 attr
.ObjectName
= &tmpfileW
;
3382 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3383 attr
.SecurityDescriptor
= NULL
;
3384 attr
.SecurityQualityOfService
= NULL
;
3386 status
= pNtCreateFile(&hTemp
, GENERIC_WRITE
| DELETE
, &attr
, &io
, NULL
, 0,
3387 FILE_SHARE_READ
, FILE_CREATE
, FILE_DELETE_ON_CLOSE
, NULL
, 0);
3388 ok(!status
, "NtCreateFile failed with %08x\n", status
);
3389 pRtlFreeUnicodeString(&tmpfileW
);
3391 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
,
3392 OWNER_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3393 (PSID
*)&owner
, NULL
, &pDacl
, NULL
, &pSD
);
3394 ok(error
== ERROR_SUCCESS
, "Failed to get permissions on file\n");
3395 test_inherited_dacl(pDacl
, admin_sid
, user_sid
, INHERITED_ACE
,
3396 0x1f01ff, TRUE
, TRUE
, TRUE
, __LINE__
);
3400 /* Test inheritance of ACLs in NtCreateFile with security descriptor -
3401 * When a security descriptor is set, then inheritance doesn't take effect */
3403 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3404 pDacl
= HeapAlloc(GetProcessHeap(), 0, sizeof(ACL
));
3405 bret
= InitializeAcl(pDacl
, sizeof(ACL
), ACL_REVISION
);
3406 ok(bret
, "Failed to initialize ACL\n");
3407 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3408 ok(bret
, "Failed to add ACL to security desciptor\n");
3410 strcpy(tmpfile
, tmpdir
);
3411 lstrcatA(tmpfile
, "/tmpfile");
3412 get_nt_pathW(tmpfile
, &tmpfileW
);
3414 attr
.Length
= sizeof(attr
);
3415 attr
.RootDirectory
= 0;
3416 attr
.ObjectName
= &tmpfileW
;
3417 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3418 attr
.SecurityDescriptor
= pSD
;
3419 attr
.SecurityQualityOfService
= NULL
;
3421 status
= pNtCreateFile(&hTemp
, GENERIC_WRITE
| DELETE
, &attr
, &io
, NULL
, 0,
3422 FILE_SHARE_READ
, FILE_CREATE
, FILE_DELETE_ON_CLOSE
, NULL
, 0);
3423 ok(!status
, "NtCreateFile failed with %08x\n", status
);
3424 pRtlFreeUnicodeString(&tmpfileW
);
3425 HeapFree(GetProcessHeap(), 0, pDacl
);
3427 error
= pGetSecurityInfo(hTemp
, SE_FILE_OBJECT
,
3428 OWNER_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3429 (PSID
*)&owner
, NULL
, &pDacl
, NULL
, &pSD
);
3430 ok(error
== ERROR_SUCCESS
, "GetNamedSecurityInfo failed with error %d\n", error
);
3431 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3432 ok(bret
, "GetAclInformation failed\n");
3434 ok(acl_size
.AceCount
== 0, "GetAclInformation returned unexpected entry count (%d != 0).\n",
3438 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
,
3439 OWNER_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3440 (PSID
*)&owner
, NULL
, &pDacl
, NULL
, &pSD
);
3442 ok(error
== ERROR_SUCCESS
, "GetNamedSecurityInfo failed with error %d\n", error
);
3443 if (error
== ERROR_SUCCESS
)
3445 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3446 ok(bret
, "GetAclInformation failed\n");
3448 ok(acl_size
.AceCount
== 0, "GetAclInformation returned unexpected entry count (%d != 0).\n",
3455 HeapFree(GetProcessHeap(), 0, user
);
3456 bret
= RemoveDirectoryA(tmpdir
);
3457 ok(bret
== TRUE
, "RemoveDirectoryA should always succeed\n");
3460 static void test_GetNamedSecurityInfoA(void)
3462 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3463 char system_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3464 char users_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3465 PSID admin_sid
= (PSID
) admin_ptr
, users_sid
= (PSID
) users_ptr
;
3466 PSID system_sid
= (PSID
) system_ptr
, user_sid
;
3467 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3468 char invalid_path
[] = "/an invalid file path";
3469 int users_ace_id
= -1, admins_ace_id
= -1, i
;
3470 char software_key
[] = "MACHINE\\Software";
3471 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
+sizeof(void*)];
3472 SECURITY_DESCRIPTOR_CONTROL control
;
3473 ACL_SIZE_INFORMATION acl_size
;
3474 CHAR windows_dir
[MAX_PATH
];
3475 PSECURITY_DESCRIPTOR pSD
;
3476 ACCESS_ALLOWED_ACE
*ace
;
3477 BOOL bret
= TRUE
, isNT4
;
3478 char tmpfile
[MAX_PATH
];
3479 DWORD error
, revision
;
3480 BOOL owner_defaulted
;
3481 BOOL group_defaulted
;
3482 BOOL dacl_defaulted
;
3483 HANDLE token
, hTemp
, h
;
3490 if (!pSetNamedSecurityInfoA
|| !pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3492 win_skip("Required functions are not available\n");
3496 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3498 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3499 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3503 win_skip("Failed to get current user token\n");
3506 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3507 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3508 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3509 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3510 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3511 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3512 CloseHandle( token
);
3513 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3515 bret
= GetWindowsDirectoryA(windows_dir
, MAX_PATH
);
3516 ok(bret
, "GetWindowsDirectory failed with error %d\n", GetLastError());
3518 SetLastError(0xdeadbeef);
3519 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,
3520 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
3521 NULL
, NULL
, NULL
, NULL
, &pSD
);
3522 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3524 win_skip("GetNamedSecurityInfoA is not implemented\n");
3525 HeapFree(GetProcessHeap(), 0, user
);
3528 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3530 bret
= GetSecurityDescriptorControl(pSD
, &control
, &revision
);
3531 ok(bret
, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
3532 ok((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == (SE_SELF_RELATIVE
|SE_DACL_PRESENT
) ||
3533 broken((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
), /* NT4 */
3534 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control
);
3535 ok(revision
== SECURITY_DESCRIPTOR_REVISION1
, "revision was %d instead of 1\n", revision
);
3537 isNT4
= (control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
;
3539 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3540 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3541 ok(owner
!= NULL
, "owner should not be NULL\n");
3543 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3544 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3545 ok(group
!= NULL
, "group should not be NULL\n");
3549 /* NULL descriptor tests */
3552 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
3553 HeapFree(GetProcessHeap(), 0, user
);
3557 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3558 NULL
, NULL
, NULL
, NULL
, NULL
);
3559 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3562 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3563 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3564 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3565 ok(pDacl
!= NULL
, "DACL should not be NULL\n");
3568 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,OWNER_SECURITY_INFORMATION
,
3569 NULL
, NULL
, &pDacl
, NULL
, NULL
);
3570 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3572 /* Test behavior of SetNamedSecurityInfo with an invalid path */
3573 SetLastError(0xdeadbeef);
3574 error
= pSetNamedSecurityInfoA(invalid_path
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3576 ok(error
== ERROR_FILE_NOT_FOUND
, "Unexpected error returned: 0x%x\n", error
);
3577 ok(GetLastError() == 0xdeadbeef, "Expected last error to remain unchanged.\n");
3579 /* Create security descriptor information and test that it comes back the same */
3581 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3582 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3583 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3584 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3585 ok(bret
, "Failed to initialize ACL.\n");
3586 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3587 ok(bret
, "Failed to add Current User to ACL.\n");
3588 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
3589 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3590 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3591 ok(bret
, "Failed to add ACL to security desciptor.\n");
3592 GetTempFileNameA(".", "foo", 0, tmpfile
);
3593 hTemp
= CreateFileA(tmpfile
, WRITE_DAC
|GENERIC_WRITE
, FILE_SHARE_DELETE
|FILE_SHARE_READ
,
3594 NULL
, OPEN_EXISTING
, FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
3595 SetLastError(0xdeadbeef);
3596 error
= pSetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3598 HeapFree(GetProcessHeap(), 0, pDacl
);
3599 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3601 win_skip("SetNamedSecurityInfoA is not implemented\n");
3602 HeapFree(GetProcessHeap(), 0, user
);
3606 ok(!error
, "SetNamedSecurityInfoA failed with error %d\n", error
);
3607 SetLastError(0xdeadbeef);
3608 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3609 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3610 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3612 win_skip("GetNamedSecurityInfoA is not implemented\n");
3613 HeapFree(GetProcessHeap(), 0, user
);
3617 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3619 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3620 ok(bret
, "GetAclInformation failed\n");
3621 if (acl_size
.AceCount
> 0)
3623 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3624 ok(bret
, "Failed to get Current User ACE.\n");
3625 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3626 todo_wine
ok(bret
, "Current User ACE (%s) != Current User SID (%s).\n",
3627 debugstr_sid(&ace
->SidStart
), debugstr_sid(user_sid
));
3628 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3629 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3630 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3633 if (acl_size
.AceCount
> 1)
3635 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3636 ok(bret
, "Failed to get Administators Group ACE.\n");
3637 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3638 todo_wine
ok(bret
|| broken(!bret
) /* win2k */,
3639 "Administators Group ACE (%s) != Administators Group SID (%s).\n",
3640 debugstr_sid(&ace
->SidStart
), debugstr_sid(admin_sid
));
3641 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3642 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3643 ok(ace
->Mask
== 0x1f01ff || broken(ace
->Mask
== GENERIC_ALL
) /* win2k */,
3644 "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n", ace
->Mask
);
3648 /* show that setting empty DACL is not removing all file permissions */
3649 pDacl
= HeapAlloc(GetProcessHeap(), 0, sizeof(ACL
));
3650 bret
= InitializeAcl(pDacl
, sizeof(ACL
), ACL_REVISION
);
3651 ok(bret
, "Failed to initialize ACL.\n");
3652 error
= pSetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3653 NULL
, NULL
, pDacl
, NULL
);
3654 ok(!error
, "SetNamedSecurityInfoA failed with error %d\n", error
);
3655 HeapFree(GetProcessHeap(), 0, pDacl
);
3657 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3658 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3659 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3661 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3662 ok(bret
, "GetAclInformation failed\n");
3663 if (acl_size
.AceCount
> 0)
3665 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3666 ok(bret
, "Failed to get ACE.\n");
3667 todo_wine
ok(((ACE_HEADER
*)ace
)->AceFlags
& INHERITED_ACE
,
3668 "ACE has unexpected flags: 0x%x\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3672 h
= CreateFileA(tmpfile
, GENERIC_READ
, FILE_SHARE_DELETE
|FILE_SHARE_WRITE
|FILE_SHARE_READ
,
3673 NULL
, OPEN_EXISTING
, 0, NULL
);
3674 ok(h
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
3677 /* test setting NULL DACL */
3678 error
= pSetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
,
3679 DACL_SECURITY_INFORMATION
, NULL
, NULL
, NULL
, NULL
);
3680 ok(!error
, "SetNamedSecurityInfoA failed with error %d\n", error
);
3682 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3683 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3684 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3685 todo_wine
ok(!pDacl
, "pDacl != NULL\n");
3688 h
= CreateFileA(tmpfile
, GENERIC_READ
, FILE_SHARE_DELETE
|FILE_SHARE_WRITE
|FILE_SHARE_READ
,
3689 NULL
, OPEN_EXISTING
, 0, NULL
);
3690 ok(h
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
3693 /* NtSetSecurityObject doesn't inherit DACL entries */
3694 pSD
= sd
+sizeof(void*)-((ULONG_PTR
)sd
)%sizeof(void*);
3695 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3696 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3697 bret
= InitializeAcl(pDacl
, sizeof(ACL
), ACL_REVISION
);
3698 ok(bret
, "Failed to initialize ACL.\n");
3699 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3700 ok(bret
, "Failed to add ACL to security desciptor.\n");
3701 status
= pNtSetSecurityObject(hTemp
, DACL_SECURITY_INFORMATION
, pSD
);
3702 ok(status
== ERROR_SUCCESS
, "NtSetSecurityObject returned %x\n", status
);
3704 h
= CreateFileA(tmpfile
, GENERIC_READ
, FILE_SHARE_DELETE
|FILE_SHARE_WRITE
|FILE_SHARE_READ
,
3705 NULL
, OPEN_EXISTING
, 0, NULL
);
3706 ok(h
== INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
3709 pSetSecurityDescriptorControl(pSD
, SE_DACL_AUTO_INHERIT_REQ
, SE_DACL_AUTO_INHERIT_REQ
);
3710 status
= pNtSetSecurityObject(hTemp
, DACL_SECURITY_INFORMATION
, pSD
);
3711 ok(status
== ERROR_SUCCESS
, "NtSetSecurityObject returned %x\n", status
);
3713 h
= CreateFileA(tmpfile
, GENERIC_READ
, FILE_SHARE_DELETE
|FILE_SHARE_WRITE
|FILE_SHARE_READ
,
3714 NULL
, OPEN_EXISTING
, 0, NULL
);
3715 ok(h
== INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
3718 pSetSecurityDescriptorControl(pSD
, SE_DACL_AUTO_INHERIT_REQ
|SE_DACL_AUTO_INHERITED
,
3719 SE_DACL_AUTO_INHERIT_REQ
|SE_DACL_AUTO_INHERITED
);
3720 status
= pNtSetSecurityObject(hTemp
, DACL_SECURITY_INFORMATION
, pSD
);
3721 ok(status
== ERROR_SUCCESS
, "NtSetSecurityObject returned %x\n", status
);
3723 h
= CreateFileA(tmpfile
, GENERIC_READ
, FILE_SHARE_DELETE
|FILE_SHARE_WRITE
|FILE_SHARE_READ
,
3724 NULL
, OPEN_EXISTING
, 0, NULL
);
3725 ok(h
== INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
3728 /* test if DACL is properly mapped to permission */
3729 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3730 ok(bret
, "Failed to initialize ACL.\n");
3731 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3732 ok(bret
, "Failed to add Current User to ACL.\n");
3733 bret
= pAddAccessDeniedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3734 ok(bret
, "Failed to add Current User to ACL.\n");
3735 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3736 ok(bret
, "Failed to add ACL to security desciptor.\n");
3737 status
= pNtSetSecurityObject(hTemp
, DACL_SECURITY_INFORMATION
, pSD
);
3738 ok(status
== ERROR_SUCCESS
, "NtSetSecurityObject returned %x\n", status
);
3740 h
= CreateFileA(tmpfile
, GENERIC_READ
, FILE_SHARE_DELETE
|FILE_SHARE_WRITE
|FILE_SHARE_READ
,
3741 NULL
, OPEN_EXISTING
, 0, NULL
);
3742 ok(h
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
3745 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3746 ok(bret
, "Failed to initialize ACL.\n");
3747 bret
= pAddAccessDeniedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3748 ok(bret
, "Failed to add Current User to ACL.\n");
3749 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3750 ok(bret
, "Failed to add Current User to ACL.\n");
3751 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3752 ok(bret
, "Failed to add ACL to security desciptor.\n");
3753 status
= pNtSetSecurityObject(hTemp
, DACL_SECURITY_INFORMATION
, pSD
);
3754 ok(status
== ERROR_SUCCESS
, "NtSetSecurityObject returned %x\n", status
);
3756 h
= CreateFileA(tmpfile
, GENERIC_READ
, FILE_SHARE_DELETE
|FILE_SHARE_WRITE
|FILE_SHARE_READ
,
3757 NULL
, OPEN_EXISTING
, 0, NULL
);
3758 ok(h
== INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
3759 HeapFree(GetProcessHeap(), 0, pDacl
);
3760 HeapFree(GetProcessHeap(), 0, user
);
3763 /* Test querying the ownership of a built-in registry key */
3764 sid_size
= sizeof(system_ptr
);
3765 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, system_sid
, &sid_size
);
3766 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
,
3767 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
,
3768 NULL
, NULL
, NULL
, NULL
, &pSD
);
3769 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3771 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3772 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3773 ok(owner
!= NULL
, "owner should not be NULL\n");
3774 ok(EqualSid(owner
, admin_sid
), "MACHINE\\Software owner SID (%s) != Administrators SID (%s).\n", debugstr_sid(owner
), debugstr_sid(admin_sid
));
3776 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3777 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3778 ok(group
!= NULL
, "group should not be NULL\n");
3779 ok(EqualSid(group
, admin_sid
) || broken(EqualSid(group
, system_sid
)) /* before Win7 */
3780 || broken(((SID
*)group
)->SubAuthority
[0] == SECURITY_NT_NON_UNIQUE
) /* Vista */,
3781 "MACHINE\\Software group SID (%s) != Local System SID (%s or %s)\n",
3782 debugstr_sid(group
), debugstr_sid(admin_sid
), debugstr_sid(system_sid
));
3785 /* Test querying the DACL of a built-in registry key */
3786 sid_size
= sizeof(users_ptr
);
3787 pCreateWellKnownSid(WinBuiltinUsersSid
, NULL
, users_sid
, &sid_size
);
3788 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
, DACL_SECURITY_INFORMATION
,
3789 NULL
, NULL
, NULL
, NULL
, &pSD
);
3790 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3792 bret
= GetSecurityDescriptorDacl(pSD
, &dacl_present
, &pDacl
, &dacl_defaulted
);
3793 ok(bret
, "GetSecurityDescriptorDacl failed with error %d\n", GetLastError());
3794 ok(dacl_present
, "DACL should be present\n");
3795 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityDescriptorDacl returned invalid DACL.\n");
3796 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3797 ok(bret
, "GetAclInformation failed\n");
3798 ok(acl_size
.AceCount
!= 0, "GetAclInformation returned no ACLs\n");
3799 for (i
=0; i
<acl_size
.AceCount
; i
++)
3801 bret
= pGetAce(pDacl
, i
, (VOID
**)&ace
);
3802 ok(bret
, "Failed to get ACE %d.\n", i
);
3803 bret
= EqualSid(&ace
->SidStart
, users_sid
);
3804 if (bret
) users_ace_id
= i
;
3805 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3806 if (bret
) admins_ace_id
= i
;
3808 ok(users_ace_id
!= -1 || broken(users_ace_id
== -1) /* win2k */,
3809 "Bultin Users ACE not found.\n");
3810 if (users_ace_id
!= -1)
3812 bret
= pGetAce(pDacl
, users_ace_id
, (VOID
**)&ace
);
3813 ok(bret
, "Failed to get Builtin Users ACE.\n");
3814 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3815 ok(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
)
3816 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */,
3817 "Builtin Users ACE has unexpected flags (0x%x != 0x%x)\n", flags
,
3818 INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
);
3819 ok(ace
->Mask
== GENERIC_READ
, "Builtin Users ACE has unexpected mask (0x%x != 0x%x)\n",
3820 ace
->Mask
, GENERIC_READ
);
3822 ok(admins_ace_id
!= -1, "Bultin Admins ACE not found.\n");
3823 if (admins_ace_id
!= -1)
3825 bret
= pGetAce(pDacl
, admins_ace_id
, (VOID
**)&ace
);
3826 ok(bret
, "Failed to get Builtin Admins ACE.\n");
3827 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3829 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */
3830 || broken(flags
== (OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
)), /* win7 */
3831 "Builtin Admins ACE has unexpected flags (0x%x != 0x0)\n", flags
);
3832 ok(ace
->Mask
== KEY_ALL_ACCESS
|| broken(ace
->Mask
== GENERIC_ALL
) /* w2k8 */,
3833 "Builtin Admins ACE has unexpected mask (0x%x != 0x%x)\n", ace
->Mask
, KEY_ALL_ACCESS
);
3838 static void test_ConvertStringSecurityDescriptor(void)
3841 PSECURITY_DESCRIPTOR pSD
;
3842 static const WCHAR Blank
[] = { 0 };
3846 const char *sidstring
;
3853 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE
, ERROR_UNKNOWN_REVISION
},
3854 /* test ACE string type */
3855 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3856 { "D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3857 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_PARAMETER
},
3858 /* test ACE string with spaces */
3859 { " D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3860 { "D: (D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3861 { "D:( D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3862 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, RPC_S_INVALID_STRING_UUID
, ERROR_INVALID_ACL
}, /* Vista+ */
3863 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3864 { "D:(D;; GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3865 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3866 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1
, TRUE
},
3867 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1
, TRUE
},
3868 { "D:(D;;GA;;; WD)", SDDL_REVISION_1
, TRUE
},
3869 { "D:(D;;GA;;;WD )", SDDL_REVISION_1
, TRUE
},
3870 /* test ACE string access rights */
3871 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3872 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1
, TRUE
},
3873 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1
, TRUE
},
3874 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1
, TRUE
},
3875 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1
, TRUE
},
3876 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1
, TRUE
},
3877 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3878 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3879 /* test ACE string access right error case */
3880 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3881 /* test behaviour with empty strings */
3882 { "", SDDL_REVISION_1
, TRUE
},
3883 /* test ACE string SID */
3884 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1
, TRUE
},
3885 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
, ERROR_INVALID_SID
} /* W2K */
3888 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3890 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3894 for (i
= 0; i
< sizeof(cssd
)/sizeof(cssd
[0]); i
++)
3898 SetLastError(0xdeadbeef);
3899 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3900 cssd
[i
].sidstring
, cssd
[i
].revision
, &pSD
, NULL
);
3901 GLE
= GetLastError();
3902 ok(ret
== cssd
[i
].ret
, "(%02u) Expected %s (%d)\n", i
, cssd
[i
].ret
? "success" : "failure", GLE
);
3904 ok(GLE
== cssd
[i
].GLE
||
3905 (cssd
[i
].altGLE
&& GLE
== cssd
[i
].altGLE
),
3906 "(%02u) Unexpected last error %d\n", i
, GLE
);
3911 /* test behaviour with NULL parameters */
3912 SetLastError(0xdeadbeef);
3913 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3914 NULL
, 0xdeadbeef, &pSD
, NULL
);
3916 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3917 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3920 SetLastError(0xdeadbeef);
3921 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3922 NULL
, 0xdeadbeef, &pSD
, NULL
);
3923 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3924 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3927 SetLastError(0xdeadbeef);
3928 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3929 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL
, NULL
);
3930 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3931 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3934 SetLastError(0xdeadbeef);
3935 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3936 "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, NULL
, NULL
);
3937 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3938 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3941 /* test behaviour with empty strings */
3942 SetLastError(0xdeadbeef);
3943 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3944 Blank
, SDDL_REVISION_1
, &pSD
, NULL
);
3945 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
3948 SetLastError(0xdeadbeef);
3949 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3950 "D:P(A;;GRGW;;;BA)(A;;GRGW;;;S-1-5-21-0-0-0-1000)S:(ML;;NWNR;;;S-1-16-12288)", SDDL_REVISION_1
, &pSD
, NULL
);
3951 ok(ret
|| broken(!ret
&& GetLastError() == ERROR_INVALID_DATATYPE
) /* win2k */,
3952 "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %u\n", GetLastError());
3953 if (ret
) LocalFree(pSD
);
3956 static void test_ConvertSecurityDescriptorToString(void)
3958 SECURITY_DESCRIPTOR desc
;
3959 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3968 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA
)
3970 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
3973 if (!pCreateWellKnownSid
)
3975 win_skip("CreateWellKnownSid is not available\n");
3979 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
3980 * don't replicate this feature so we only test len >= strlen+1. */
3981 #define CHECK_RESULT_AND_FREE(exp_str) \
3982 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
3983 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlenA(exp_str) + 1, len); \
3986 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
3987 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); \
3988 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); \
3991 InitializeSecurityDescriptor(&desc
, SECURITY_DESCRIPTOR_REVISION
);
3992 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3993 CHECK_RESULT_AND_FREE("");
3996 pCreateWellKnownSid(WinLocalSid
, NULL
, sid_buf
, &size
);
3997 SetSecurityDescriptorOwner(&desc
, sid_buf
, FALSE
);
3998 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3999 CHECK_RESULT_AND_FREE("O:S-1-2-0");
4001 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
4002 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4003 CHECK_RESULT_AND_FREE("O:S-1-2-0");
4005 size
= sizeof(sid_buf
);
4006 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, sid_buf
, &size
);
4007 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
4008 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4009 CHECK_RESULT_AND_FREE("O:SY");
4011 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
);
4012 SetSecurityDescriptorGroup(&desc
, psid
, TRUE
);
4013 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4014 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
4016 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, GROUP_SECURITY_INFORMATION
, &string
, &len
), "Conversion failed\n");
4017 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
4019 pacl
= (PACL
)acl_buf
;
4020 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
4021 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, TRUE
);
4022 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4023 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
4025 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, FALSE
);
4026 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4027 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
4029 pConvertStringSidToSidA("S-1-5-6", &psid2
);
4030 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, 0xf0000000, psid2
);
4031 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4032 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
4034 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, INHERIT_ONLY_ACE
|INHERITED_ACE
, 0x00000003, psid2
);
4035 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4036 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
4038 pAddAccessDeniedAceEx(pacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
, 0xffffffff, psid
);
4039 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4040 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)");
4043 pacl
= (PACL
)acl_buf
;
4044 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
4045 SetSecurityDescriptorSacl(&desc
, TRUE
, pacl
, FALSE
);
4046 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4047 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
4049 /* fails in win2k */
4050 SetSecurityDescriptorDacl(&desc
, TRUE
, NULL
, FALSE
);
4051 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, VALID_INHERIT_FLAGS
, KEY_READ
|KEY_WRITE
, psid2
, TRUE
, TRUE
);
4052 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
4054 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
4055 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
4058 /* fails in win2k */
4059 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, psid2
, TRUE
, FALSE
);
4060 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
4062 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
4063 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
4070 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec
)
4072 SECURITY_DESCRIPTOR_CONTROL ref
;
4073 SECURITY_DESCRIPTOR_CONTROL test
;
4075 SECURITY_DESCRIPTOR_CONTROL
const mutable
4076 = SE_DACL_AUTO_INHERIT_REQ
| SE_SACL_AUTO_INHERIT_REQ
4077 | SE_DACL_AUTO_INHERITED
| SE_SACL_AUTO_INHERITED
4078 | SE_DACL_PROTECTED
| SE_SACL_PROTECTED
4079 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
4081 SECURITY_DESCRIPTOR_CONTROL
const immutable
4082 = SE_OWNER_DEFAULTED
| SE_GROUP_DEFAULTED
4083 | SE_DACL_PRESENT
| SE_DACL_DEFAULTED
4084 | SE_SACL_PRESENT
| SE_SACL_DEFAULTED
4085 | SE_RM_CONTROL_VALID
| SE_SELF_RELATIVE
4090 LPCSTR fmt
= "Expected error %s, got %u\n";
4092 GetSecurityDescriptorControl (sec
, &ref
, &dwRevision
);
4094 /* The mutable bits are mutable regardless of the truth of
4095 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
4097 /* Check call barfs if any bit-of-interest is immutable */
4098 for (bit
= 0; bit
< 16; ++bit
)
4100 SECURITY_DESCRIPTOR_CONTROL
const bitOfInterest
= 1 << bit
;
4101 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitOfInterest
;
4103 SECURITY_DESCRIPTOR_CONTROL ctrl
;
4105 DWORD dwExpect
= (bitOfInterest
& immutable
)
4106 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
4107 LPCSTR strExpect
= (bitOfInterest
& immutable
)
4108 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
4110 ctrl
= (bitOfInterest
& mutable) ? ref
+ bitOfInterest
: ref
;
4111 setOrClear
^= bitOfInterest
;
4112 SetLastError (0xbebecaca);
4113 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
4114 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
4115 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
4116 expect_eq(test
, ctrl
, int, "%x");
4118 setOrClear
^= bitOfInterest
;
4119 SetLastError (0xbebecaca);
4120 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
4121 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
4122 GetSecurityDescriptorControl (sec
, &test
, &dwRevision
);
4123 expect_eq(test
, ref
, int, "%x");
4126 /* Check call barfs if any bit-to-set is immutable
4127 even when not a bit-of-interest */
4128 for (bit
= 0; bit
< 16; ++bit
)
4130 SECURITY_DESCRIPTOR_CONTROL
const bitsOfInterest
= mutable;
4131 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitsOfInterest
;
4133 SECURITY_DESCRIPTOR_CONTROL ctrl
;
4135 DWORD dwExpect
= ((1 << bit
) & immutable
)
4136 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
4137 LPCSTR strExpect
= ((1 << bit
) & immutable
)
4138 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
4140 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| mutable;
4141 setOrClear
^= bitsOfInterest
;
4142 SetLastError (0xbebecaca);
4143 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
4144 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
4145 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
4146 expect_eq(test
, ctrl
, int, "%x");
4148 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| (1 << bit
);
4149 setOrClear
^= bitsOfInterest
;
4150 SetLastError (0xbebecaca);
4151 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
4152 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
4153 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
4154 expect_eq(test
, ctrl
, int, "%x");
4158 static void test_PrivateObjectSecurity(void)
4160 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
4161 SECURITY_DESCRIPTOR_CONTROL ctrl
;
4162 PSECURITY_DESCRIPTOR sec
;
4168 PSECURITY_DESCRIPTOR buf
;
4171 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
4173 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
4177 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
4179 "G:S-1-5-21-93476-23408-4576"
4180 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
4181 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
4182 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
4183 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
4185 test_SetSecurityDescriptorControl(sec
);
4189 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
4191 "G:S-1-5-21-93476-23408-4576",
4192 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
4194 test_SetSecurityDescriptorControl(sec
);
4198 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
4200 "G:S-1-5-21-93476-23408-4576"
4201 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
4202 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
4203 buf
= HeapAlloc(GetProcessHeap(), 0, dwDescSize
);
4204 pSetSecurityDescriptorControl(sec
, SE_DACL_PROTECTED
, SE_DACL_PROTECTED
);
4205 GetSecurityDescriptorControl(sec
, &ctrl
, &dwRevision
);
4206 expect_eq(ctrl
, 0x9014, int, "%x");
4208 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
4209 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
4210 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
4211 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4212 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
4213 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
4214 expect_eq(ctrl
, 0x8000, int, "%x");
4216 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
4217 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
4218 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
4219 ret
= pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
);
4220 ok(ret
, "Conversion failed err=%u\n", GetLastError());
4221 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)",
4222 "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 */
4223 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
4224 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8004, int, "%x");
4226 ret
= GetPrivateObjectSecurity(sec
, sec_info
, buf
, dwDescSize
, &retSize
);
4227 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
4228 ok(retSize
== dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
4229 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
4230 CHECK_ONE_OF_AND_FREE("O:SY"
4231 "G:S-1-5-21-93476-23408-4576"
4232 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
4233 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
4235 "G:S-1-5-21-93476-23408-4576"
4236 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
4237 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
4238 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
4239 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8014, int, "%x");
4241 SetLastError(0xdeadbeef);
4242 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, 5, &retSize
) == FALSE
, "GetPrivateObjectSecurity should have failed\n");
4243 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
4246 HeapFree(GetProcessHeap(), 0, buf
);
4248 #undef CHECK_RESULT_AND_FREE
4249 #undef CHECK_ONE_OF_AND_FREE
4251 static void test_acls(void)
4254 PACL pAcl
= (PACL
)buffer
;
4257 SetLastError(0xdeadbeef);
4258 ret
= InitializeAcl(pAcl
, sizeof(ACL
) - 1, ACL_REVISION
);
4259 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
4261 win_skip("InitializeAcl is not implemented\n");
4265 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
4267 SetLastError(0xdeadbeef);
4268 ret
= InitializeAcl(pAcl
, 0xffffffff, ACL_REVISION
);
4269 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
4271 SetLastError(0xdeadbeef);
4272 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION1
);
4273 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
4275 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION2
);
4276 ok(ret
, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
4278 ret
= IsValidAcl(pAcl
);
4279 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
4281 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION3
);
4282 ok(ret
, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
4284 ret
= IsValidAcl(pAcl
);
4285 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
4287 SetLastError(0xdeadbeef);
4288 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION4
);
4289 if (GetLastError() != ERROR_INVALID_PARAMETER
)
4291 ok(ret
, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
4293 ret
= IsValidAcl(pAcl
);
4294 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
4297 win_skip("ACL_REVISION4 is not implemented on NT4\n");
4299 SetLastError(0xdeadbeef);
4300 ret
= InitializeAcl(pAcl
, sizeof(buffer
), -1);
4301 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(-1) failed with error %d\n", GetLastError());
4304 static void test_GetSecurityInfo(void)
4306 char b
[sizeof(TOKEN_USER
) + sizeof(SID
) + sizeof(DWORD
)*SID_MAX_SUB_AUTHORITIES
];
4307 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], dacl
[100];
4308 DWORD sid_size
= sizeof(admin_ptr
), l
= sizeof(b
);
4309 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
4310 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
4311 ACL_SIZE_INFORMATION acl_size
;
4312 PSECURITY_DESCRIPTOR pSD
;
4313 ACCESS_ALLOWED_ACE
*ace
;
4320 if (!pGetSecurityInfo
|| !pSetSecurityInfo
)
4322 win_skip("[Get|Set]SecurityInfo is not available\n");
4326 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
4328 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
4329 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
4333 win_skip("Failed to get current user token\n");
4336 bret
= GetTokenInformation(token
, TokenUser
, b
, l
, &l
);
4337 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
4338 CloseHandle( token
);
4339 user_sid
= ((TOKEN_USER
*)b
)->User
.Sid
;
4341 /* Create something. Files have lots of associated security info. */
4342 obj
= CreateFileA(myARGV
[0], GENERIC_READ
|WRITE_DAC
, FILE_SHARE_READ
, NULL
,
4343 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
4344 if (obj
== INVALID_HANDLE_VALUE
)
4346 skip("Couldn't create an object for GetSecurityInfo test\n");
4350 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
4351 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4352 &owner
, &group
, &pDacl
, NULL
, &pSD
);
4353 if (ret
== ERROR_CALL_NOT_IMPLEMENTED
)
4355 win_skip("GetSecurityInfo is not implemented\n");
4359 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
4360 ok(pSD
!= NULL
, "GetSecurityInfo\n");
4361 ok(owner
!= NULL
, "GetSecurityInfo\n");
4362 ok(group
!= NULL
, "GetSecurityInfo\n");
4364 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
4366 win_skip("No ACL information returned\n");
4370 if (!pCreateWellKnownSid
)
4372 win_skip("NULL parameter test would crash on NT4\n");
4377 /* If we don't ask for the security descriptor, Windows will still give us
4378 the other stuff, leaving us no way to free it. */
4379 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
4380 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4381 &owner
, &group
, &pDacl
, NULL
, NULL
);
4382 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
4383 ok(owner
!= NULL
, "GetSecurityInfo\n");
4384 ok(group
!= NULL
, "GetSecurityInfo\n");
4386 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
4388 win_skip("No ACL information returned\n");
4390 /* Create security descriptor information and test that it comes back the same */
4392 pDacl
= (PACL
)&dacl
;
4393 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
4394 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
4395 bret
= InitializeAcl(pDacl
, sizeof(dacl
), ACL_REVISION
);
4396 ok(bret
, "Failed to initialize ACL.\n");
4397 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
4398 ok(bret
, "Failed to add Current User to ACL.\n");
4399 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
4400 ok(bret
, "Failed to add Administrator Group to ACL.\n");
4401 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
4402 ok(bret
, "Failed to add ACL to security desciptor.\n");
4403 ret
= pSetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
4404 NULL
, NULL
, pDacl
, NULL
);
4405 ok(ret
== ERROR_SUCCESS
, "SetSecurityInfo returned %d\n", ret
);
4406 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
4407 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
4408 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
4409 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityInfo returned invalid DACL.\n");
4410 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
4411 ok(bret
, "GetAclInformation failed\n");
4412 if (acl_size
.AceCount
> 0)
4414 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
4415 ok(bret
, "Failed to get Current User ACE.\n");
4416 bret
= EqualSid(&ace
->SidStart
, user_sid
);
4417 todo_wine
ok(bret
, "Current User ACE (%s) != Current User SID (%s).\n",
4418 debugstr_sid(&ace
->SidStart
), debugstr_sid(user_sid
));
4419 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
4420 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
4421 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
4424 if (acl_size
.AceCount
> 1)
4426 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
4427 ok(bret
, "Failed to get Administators Group ACE.\n");
4428 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
4429 todo_wine
ok(bret
, "Administators Group ACE (%s) != Administators Group SID (%s).\n", debugstr_sid(&ace
->SidStart
), debugstr_sid(admin_sid
));
4430 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
4431 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
4432 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
4439 static void test_GetSidSubAuthority(void)
4443 if (!pGetSidSubAuthority
|| !pConvertStringSidToSidA
|| !pIsValidSid
|| !pGetSidSubAuthorityCount
)
4445 win_skip("Some functions not available\n");
4448 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
4449 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
4450 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid
),"ConvertStringSidToSidA failed\n");
4451 ok(pIsValidSid(psid
),"Sid is not valid\n");
4452 SetLastError(0xbebecaca);
4453 ok(*pGetSidSubAuthorityCount(psid
) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid
));
4454 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4455 SetLastError(0xbebecaca);
4456 ok(*pGetSidSubAuthority(psid
,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid
,0));
4457 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4458 SetLastError(0xbebecaca);
4459 ok(*pGetSidSubAuthority(psid
,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid
,1));
4460 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4461 SetLastError(0xbebecaca);
4462 ok(pGetSidSubAuthority(psid
,4) != NULL
,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
4463 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4467 static void test_CheckTokenMembership(void)
4469 PTOKEN_GROUPS token_groups
;
4471 HANDLE process_token
, token
;
4476 if (!pCheckTokenMembership
)
4478 win_skip("CheckTokenMembership is not available\n");
4481 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4482 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
4484 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
4485 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
4488 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4489 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4490 "GetTokenInformation(TokenGroups) %s with error %d\n",
4491 ret
? "succeeded" : "failed", GetLastError());
4492 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4493 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4494 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
4496 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4498 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4502 if (i
== token_groups
->GroupCount
)
4504 HeapFree(GetProcessHeap(), 0, token_groups
);
4506 skip("user not a member of any group\n");
4511 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4512 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4513 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4516 ret
= pCheckTokenMembership(NULL
, token_groups
->Groups
[i
].Sid
, &is_member
);
4517 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4518 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4521 SetLastError(0xdeadbeef);
4522 ret
= pCheckTokenMembership(process_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4523 ok(!ret
&& GetLastError() == ERROR_NO_IMPERSONATION_TOKEN
,
4524 "CheckTokenMembership with process token %s with error %d\n",
4525 ret
? "succeeded" : "failed", GetLastError());
4526 ok(!is_member
, "CheckTokenMembership should have cleared is_member\n");
4528 HeapFree(GetProcessHeap(), 0, token_groups
);
4530 CloseHandle(process_token
);
4533 static void test_EqualSid(void)
4537 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
4538 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
4540 SetLastError(0xdeadbeef);
4541 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4542 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid1
);
4543 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
4545 win_skip("AllocateAndInitializeSid is not implemented\n");
4548 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4549 ok(GetLastError() == 0xdeadbeef,
4550 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
4553 ret
= AllocateAndInitializeSid(&SIDAuthWorld
, 1, SECURITY_WORLD_RID
,
4554 0, 0, 0, 0, 0, 0, 0, &sid2
);
4555 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4557 SetLastError(0xdeadbeef);
4558 ret
= EqualSid(sid1
, sid2
);
4559 ok(!ret
, "World and domain admins sids shouldn't have been equal\n");
4560 ok(GetLastError() == ERROR_SUCCESS
||
4561 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4562 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4565 SetLastError(0xdeadbeef);
4566 sid2
= FreeSid(sid2
);
4567 ok(!sid2
, "FreeSid should have returned NULL instead of %p\n", sid2
);
4568 ok(GetLastError() == 0xdeadbeef,
4569 "FreeSid shouldn't have set last error to %d\n",
4572 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4573 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid2
);
4574 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4576 SetLastError(0xdeadbeef);
4577 ret
= EqualSid(sid1
, sid2
);
4578 ok(ret
, "Same sids should have been equal %s != %s\n",
4579 debugstr_sid(sid1
), debugstr_sid(sid2
));
4580 ok(GetLastError() == ERROR_SUCCESS
||
4581 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4582 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4585 ((SID
*)sid2
)->Revision
= 2;
4586 SetLastError(0xdeadbeef);
4587 ret
= EqualSid(sid1
, sid2
);
4588 ok(!ret
, "EqualSid with invalid sid should have returned FALSE\n");
4589 ok(GetLastError() == ERROR_SUCCESS
||
4590 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4591 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4593 ((SID
*)sid2
)->Revision
= SID_REVISION
;
4599 static void test_GetUserNameA(void)
4601 char buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4602 DWORD required_len
, buffer_len
;
4605 /* Test crashes on Windows. */
4608 SetLastError(0xdeadbeef);
4609 GetUserNameA(NULL
, NULL
);
4612 SetLastError(0xdeadbeef);
4614 ret
= GetUserNameA(NULL
, &required_len
);
4615 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4616 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4617 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4619 SetLastError(0xdeadbeef);
4621 ret
= GetUserNameA(NULL
, &required_len
);
4622 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4623 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4624 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4626 /* Tests crashes on Windows. */
4629 SetLastError(0xdeadbeef);
4630 required_len
= UNLEN
+ 1;
4631 GetUserNameA(NULL
, &required_len
);
4633 SetLastError(0xdeadbeef);
4634 GetUserNameA(buffer
, NULL
);
4637 memset(filler
, 'x', sizeof(filler
));
4639 /* Note that GetUserNameA on XP and newer outputs the number of bytes
4640 * required for a Unicode string, which affects a test in the next block. */
4641 SetLastError(0xdeadbeef);
4642 memcpy(buffer
, filler
, sizeof(filler
));
4644 ret
= GetUserNameA(buffer
, &required_len
);
4645 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4646 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4647 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4648 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4650 SetLastError(0xdeadbeef);
4651 memcpy(buffer
, filler
, sizeof(filler
));
4652 buffer_len
= required_len
;
4653 ret
= GetUserNameA(buffer
, &buffer_len
);
4654 ok(ret
== TRUE
, "GetUserNameA returned %d, last error %u\n", ret
, GetLastError());
4655 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4656 ok(buffer_len
== required_len
||
4657 broken(buffer_len
== required_len
/ sizeof(WCHAR
)), /* XP+ */
4658 "Outputted buffer length was %u\n", buffer_len
);
4660 /* Use the reported buffer size from the last GetUserNameA call and pass
4661 * a length that is one less than the required value. */
4662 SetLastError(0xdeadbeef);
4663 memcpy(buffer
, filler
, sizeof(filler
));
4665 ret
= GetUserNameA(buffer
, &buffer_len
);
4666 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4667 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was untouched\n");
4668 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4669 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4672 static void test_GetUserNameW(void)
4674 WCHAR buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4675 DWORD required_len
, buffer_len
;
4678 /* Test crashes on Windows. */
4681 SetLastError(0xdeadbeef);
4682 GetUserNameW(NULL
, NULL
);
4685 SetLastError(0xdeadbeef);
4687 ret
= GetUserNameW(NULL
, &required_len
);
4688 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4689 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4690 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4692 SetLastError(0xdeadbeef);
4694 ret
= GetUserNameW(NULL
, &required_len
);
4695 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4696 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4697 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4699 /* Tests crash on Windows. */
4702 SetLastError(0xdeadbeef);
4703 required_len
= UNLEN
+ 1;
4704 GetUserNameW(NULL
, &required_len
);
4706 SetLastError(0xdeadbeef);
4707 GetUserNameW(buffer
, NULL
);
4710 memset(filler
, 'x', sizeof(filler
));
4712 SetLastError(0xdeadbeef);
4713 memcpy(buffer
, filler
, sizeof(filler
));
4715 ret
= GetUserNameW(buffer
, &required_len
);
4716 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4717 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4718 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4719 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4721 SetLastError(0xdeadbeef);
4722 memcpy(buffer
, filler
, sizeof(filler
));
4723 buffer_len
= required_len
;
4724 ret
= GetUserNameW(buffer
, &buffer_len
);
4725 ok(ret
== TRUE
, "GetUserNameW returned %d, last error %u\n", ret
, GetLastError());
4726 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4727 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4729 /* GetUserNameW on XP and newer writes a truncated portion of the username string to the buffer. */
4730 SetLastError(0xdeadbeef);
4731 memcpy(buffer
, filler
, sizeof(filler
));
4733 ret
= GetUserNameW(buffer
, &buffer_len
);
4734 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4735 ok(!memcmp(buffer
, filler
, sizeof(filler
)) ||
4736 broken(memcmp(buffer
, filler
, sizeof(filler
)) != 0), /* XP+ */
4737 "Output buffer was altered\n");
4738 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4739 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4742 static void test_CreateRestrictedToken(void)
4744 HANDLE process_token
, token
, r_token
;
4745 PTOKEN_GROUPS token_groups
, groups2
;
4746 SID_AND_ATTRIBUTES sattr
;
4747 SECURITY_IMPERSONATION_LEVEL level
;
4754 if (!pCreateRestrictedToken
)
4756 win_skip("CreateRestrictedToken is not available\n");
4760 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4761 ok(ret
, "got error %d\n", GetLastError());
4763 ret
= DuplicateTokenEx(process_token
, TOKEN_DUPLICATE
|TOKEN_ADJUST_GROUPS
|TOKEN_QUERY
,
4764 NULL
, SecurityImpersonation
, TokenImpersonation
, &token
);
4765 ok(ret
, "got error %d\n", GetLastError());
4768 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4769 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4770 "got %d with error %d\n", ret
, GetLastError());
4771 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4772 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4773 ok(ret
, "got error %d\n", GetLastError());
4775 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4777 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4781 if (i
== token_groups
->GroupCount
)
4783 HeapFree(GetProcessHeap(), 0, token_groups
);
4785 skip("User not a member of any group\n");
4790 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4791 ok(ret
, "got error %d\n", GetLastError());
4792 ok(is_member
, "not a member\n");
4794 /* disable a SID in new token */
4795 sattr
.Sid
= token_groups
->Groups
[i
].Sid
;
4796 sattr
.Attributes
= 0;
4798 ret
= pCreateRestrictedToken(token
, 0, 1, &sattr
, 0, NULL
, 0, NULL
, &r_token
);
4799 ok(ret
, "got error %d\n", GetLastError());
4803 /* check if a SID is enabled */
4805 ret
= pCheckTokenMembership(r_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4806 ok(ret
, "got error %d\n", GetLastError());
4807 todo_wine
ok(!is_member
, "not a member\n");
4809 ret
= GetTokenInformation(r_token
, TokenGroups
, NULL
, 0, &size
);
4810 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "got %d with error %d\n",
4811 ret
, GetLastError());
4812 groups2
= HeapAlloc(GetProcessHeap(), 0, size
);
4813 ret
= GetTokenInformation(r_token
, TokenGroups
, groups2
, size
, &size
);
4814 ok(ret
, "got error %d\n", GetLastError());
4816 for (j
= 0; j
< groups2
->GroupCount
; j
++)
4818 if (EqualSid(groups2
->Groups
[j
].Sid
, token_groups
->Groups
[i
].Sid
))
4822 todo_wine
ok(groups2
->Groups
[j
].Attributes
& SE_GROUP_USE_FOR_DENY_ONLY
,
4823 "got wrong attributes\n");
4824 todo_wine
ok((groups2
->Groups
[j
].Attributes
& SE_GROUP_ENABLED
) == 0,
4825 "got wrong attributes\n");
4827 HeapFree(GetProcessHeap(), 0, groups2
);
4829 size
= sizeof(type
);
4830 ret
= GetTokenInformation(r_token
, TokenType
, &type
, size
, &size
);
4831 ok(ret
, "got error %d\n", GetLastError());
4832 ok(type
== TokenImpersonation
, "got type %u\n", type
);
4834 size
= sizeof(level
);
4835 ret
= GetTokenInformation(r_token
, TokenImpersonationLevel
, &level
, size
, &size
);
4836 ok(ret
, "got error %d\n", GetLastError());
4837 ok(level
== SecurityImpersonation
, "got level %u\n", type
);
4840 HeapFree(GetProcessHeap(), 0, token_groups
);
4841 CloseHandle(r_token
);
4843 CloseHandle(process_token
);
4846 static void validate_default_security_descriptor(SECURITY_DESCRIPTOR
*sd
)
4848 BOOL ret
, present
, defaulted
;
4852 ret
= IsValidSecurityDescriptor(sd
);
4853 ok(ret
, "security descriptor is not valid\n");
4857 acl
= (void *)0xdeadbeef;
4858 SetLastError(0xdeadbeef);
4859 ret
= GetSecurityDescriptorDacl(sd
, &present
, &acl
, &defaulted
);
4860 ok(ret
, "GetSecurityDescriptorDacl error %d\n", GetLastError());
4862 ok(present
== 1, "acl is not present\n");
4864 ok(acl
!= (void *)0xdeadbeef && acl
!= NULL
, "acl pointer is not set\n");
4865 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4868 sid
= (void *)0xdeadbeef;
4869 SetLastError(0xdeadbeef);
4870 ret
= GetSecurityDescriptorOwner(sd
, &sid
, &defaulted
);
4871 ok(ret
, "GetSecurityDescriptorOwner error %d\n", GetLastError());
4873 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4874 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4877 sid
= (void *)0xdeadbeef;
4878 SetLastError(0xdeadbeef);
4879 ret
= GetSecurityDescriptorGroup(sd
, &sid
, &defaulted
);
4880 ok(ret
, "GetSecurityDescriptorGroup error %d\n", GetLastError());
4882 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4883 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4886 static void test_default_handle_security(HANDLE token
, HANDLE handle
, GENERIC_MAPPING
*mapping
)
4888 DWORD ret
, granted
, priv_set_len
;
4890 PRIVILEGE_SET priv_set
;
4891 SECURITY_DESCRIPTOR
*sd
;
4893 sd
= test_get_security_descriptor(handle
, __LINE__
);
4894 validate_default_security_descriptor(sd
);
4896 priv_set_len
= sizeof(priv_set
);
4897 granted
= 0xdeadbeef;
4898 status
= 0xdeadbeef;
4899 SetLastError(0xdeadbeef);
4900 ret
= AccessCheck(sd
, token
, MAXIMUM_ALLOWED
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4902 ok(ret
, "AccessCheck error %d\n", GetLastError());
4903 ok(status
== 1, "expected 1, got %d\n", status
);
4904 ok(granted
== mapping
->GenericAll
, "expected all access %#x, got %#x\n", mapping
->GenericAll
, granted
);
4906 priv_set_len
= sizeof(priv_set
);
4907 granted
= 0xdeadbeef;
4908 status
= 0xdeadbeef;
4909 SetLastError(0xdeadbeef);
4910 ret
= AccessCheck(sd
, token
, 0, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4912 ok(ret
, "AccessCheck error %d\n", GetLastError());
4913 ok(status
== 0 || broken(status
== 1) /* NT4 */, "expected 0, got %d\n", status
);
4914 ok(granted
== 0 || broken(granted
== mapping
->GenericRead
) /* NT4 */, "expected 0, got %#x\n", granted
);
4916 priv_set_len
= sizeof(priv_set
);
4917 granted
= 0xdeadbeef;
4918 status
= 0xdeadbeef;
4919 SetLastError(0xdeadbeef);
4920 ret
= AccessCheck(sd
, token
, ACCESS_SYSTEM_SECURITY
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4922 ok(ret
, "AccessCheck error %d\n", GetLastError());
4923 ok(status
== 0, "expected 0, got %d\n", status
);
4924 ok(granted
== 0, "expected 0, got %#x\n", granted
);
4926 priv_set_len
= sizeof(priv_set
);
4927 granted
= 0xdeadbeef;
4928 status
= 0xdeadbeef;
4929 SetLastError(0xdeadbeef);
4930 ret
= AccessCheck(sd
, token
, mapping
->GenericRead
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4932 ok(ret
, "AccessCheck error %d\n", GetLastError());
4933 ok(status
== 1, "expected 1, got %d\n", status
);
4934 ok(granted
== mapping
->GenericRead
, "expected read access %#x, got %#x\n", mapping
->GenericRead
, granted
);
4936 priv_set_len
= sizeof(priv_set
);
4937 granted
= 0xdeadbeef;
4938 status
= 0xdeadbeef;
4939 SetLastError(0xdeadbeef);
4940 ret
= AccessCheck(sd
, token
, mapping
->GenericWrite
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4942 ok(ret
, "AccessCheck error %d\n", GetLastError());
4943 ok(status
== 1, "expected 1, got %d\n", status
);
4944 ok(granted
== mapping
->GenericWrite
, "expected write access %#x, got %#x\n", mapping
->GenericWrite
, granted
);
4946 priv_set_len
= sizeof(priv_set
);
4947 granted
= 0xdeadbeef;
4948 status
= 0xdeadbeef;
4949 SetLastError(0xdeadbeef);
4950 ret
= AccessCheck(sd
, token
, mapping
->GenericExecute
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4952 ok(ret
, "AccessCheck error %d\n", GetLastError());
4953 ok(status
== 1, "expected 1, got %d\n", status
);
4954 ok(granted
== mapping
->GenericExecute
, "expected execute access %#x, got %#x\n", mapping
->GenericExecute
, granted
);
4956 HeapFree(GetProcessHeap(), 0, sd
);
4959 static ACCESS_MASK
get_obj_access(HANDLE obj
)
4961 OBJECT_BASIC_INFORMATION info
;
4964 if (!pNtQueryObject
) return 0;
4966 status
= pNtQueryObject(obj
, ObjectBasicInformation
, &info
, sizeof(info
), NULL
);
4967 ok(!status
, "NtQueryObject error %#x\n", status
);
4969 return info
.GrantedAccess
;
4972 static void test_mutex_security(HANDLE token
)
4974 DWORD ret
, i
, access
;
4976 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
| SYNCHRONIZE
,
4977 STANDARD_RIGHTS_WRITE
| MUTEX_MODIFY_STATE
| SYNCHRONIZE
,
4978 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4979 STANDARD_RIGHTS_ALL
| MUTEX_ALL_ACCESS
};
4982 int generic
, mapped
;
4986 { GENERIC_READ
, STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
},
4987 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
},
4988 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4989 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| MUTANT_QUERY_STATE
}
4992 SetLastError(0xdeadbeef);
4993 mutex
= OpenMutexA(0, FALSE
, "WineTestMutex");
4994 ok(!mutex
, "mutex should not exist\n");
4995 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4997 SetLastError(0xdeadbeef);
4998 mutex
= CreateMutexA(NULL
, FALSE
, "WineTestMutex");
4999 ok(mutex
!= 0, "CreateMutex error %d\n", GetLastError());
5001 access
= get_obj_access(mutex
);
5002 ok(access
== MUTANT_ALL_ACCESS
, "expected MUTANT_ALL_ACCESS, got %#x\n", access
);
5004 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5006 SetLastError( 0xdeadbeef );
5007 ret
= DuplicateHandle(GetCurrentProcess(), mutex
, GetCurrentProcess(), &dup
,
5008 map
[i
].generic
, FALSE
, 0);
5009 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5011 access
= get_obj_access(dup
);
5012 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5016 SetLastError(0xdeadbeef);
5017 dup
= OpenMutexA(0, FALSE
, "WineTestMutex");
5019 ok(!dup
, "OpenMutex should fail\n");
5021 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
5024 test_default_handle_security(token
, mutex
, &mapping
);
5026 CloseHandle (mutex
);
5029 static void test_event_security(HANDLE token
)
5031 DWORD ret
, i
, access
;
5033 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
| SYNCHRONIZE
,
5034 STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
| SYNCHRONIZE
,
5035 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
5036 STANDARD_RIGHTS_ALL
| EVENT_ALL_ACCESS
};
5039 int generic
, mapped
;
5043 { GENERIC_READ
, STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
},
5044 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
},
5045 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5046 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| EVENT_QUERY_STATE
| EVENT_MODIFY_STATE
}
5049 SetLastError(0xdeadbeef);
5050 event
= OpenEventA(0, FALSE
, "WineTestEvent");
5051 ok(!event
, "event should not exist\n");
5052 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
5054 SetLastError(0xdeadbeef);
5055 event
= CreateEventA(NULL
, FALSE
, FALSE
, "WineTestEvent");
5056 ok(event
!= 0, "CreateEvent error %d\n", GetLastError());
5058 access
= get_obj_access(event
);
5059 ok(access
== EVENT_ALL_ACCESS
, "expected EVENT_ALL_ACCESS, got %#x\n", access
);
5061 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5063 SetLastError( 0xdeadbeef );
5064 ret
= DuplicateHandle(GetCurrentProcess(), event
, GetCurrentProcess(), &dup
,
5065 map
[i
].generic
, FALSE
, 0);
5066 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5068 access
= get_obj_access(dup
);
5069 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5073 SetLastError(0xdeadbeef);
5074 dup
= OpenEventA(0, FALSE
, "WineTestEvent");
5076 ok(!dup
, "OpenEvent should fail\n");
5078 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
5081 test_default_handle_security(token
, event
, &mapping
);
5086 static void test_semaphore_security(HANDLE token
)
5088 DWORD ret
, i
, access
;
5090 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
,
5091 STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
,
5092 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
5093 STANDARD_RIGHTS_ALL
| SEMAPHORE_ALL_ACCESS
};
5096 int generic
, mapped
;
5100 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
},
5101 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
},
5102 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5103 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| SEMAPHORE_QUERY_STATE
| SEMAPHORE_MODIFY_STATE
}
5106 SetLastError(0xdeadbeef);
5107 sem
= OpenSemaphoreA(0, FALSE
, "WineTestSemaphore");
5108 ok(!sem
, "semaphore should not exist\n");
5109 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
5111 SetLastError(0xdeadbeef);
5112 sem
= CreateSemaphoreA(NULL
, 0, 10, "WineTestSemaphore");
5113 ok(sem
!= 0, "CreateSemaphore error %d\n", GetLastError());
5115 access
= get_obj_access(sem
);
5116 ok(access
== SEMAPHORE_ALL_ACCESS
, "expected SEMAPHORE_ALL_ACCESS, got %#x\n", access
);
5118 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5120 SetLastError( 0xdeadbeef );
5121 ret
= DuplicateHandle(GetCurrentProcess(), sem
, GetCurrentProcess(), &dup
,
5122 map
[i
].generic
, FALSE
, 0);
5123 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5125 access
= get_obj_access(dup
);
5126 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5131 test_default_handle_security(token
, sem
, &mapping
);
5136 #define WINE_TEST_PIPE "\\\\.\\pipe\\WineTestPipe"
5137 static void test_named_pipe_security(HANDLE token
)
5139 DWORD ret
, i
, access
;
5140 HANDLE pipe
, file
, dup
;
5141 GENERIC_MAPPING mapping
= { FILE_GENERIC_READ
,
5143 FILE_GENERIC_EXECUTE
,
5144 STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
};
5147 int todo
, generic
, mapped
;
5151 { 1, GENERIC_READ
, FILE_GENERIC_READ
},
5152 { 1, GENERIC_WRITE
, FILE_GENERIC_WRITE
},
5153 { 1, GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
5154 { 1, GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
5160 } creation_access
[] =
5162 { PIPE_ACCESS_INBOUND
, FILE_GENERIC_READ
},
5163 { PIPE_ACCESS_OUTBOUND
, FILE_GENERIC_WRITE
},
5164 { PIPE_ACCESS_DUPLEX
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
},
5165 { PIPE_ACCESS_INBOUND
|WRITE_DAC
, FILE_GENERIC_READ
|WRITE_DAC
},
5166 { PIPE_ACCESS_INBOUND
|WRITE_OWNER
, FILE_GENERIC_READ
|WRITE_OWNER
}
5167 /* ACCESS_SYSTEM_SECURITY is also valid, but will fail with ERROR_PRIVILEGE_NOT_HELD */
5170 /* Test the different security access options for pipes */
5171 for (i
= 0; i
< sizeof(creation_access
)/sizeof(creation_access
[0]); i
++)
5173 SetLastError(0xdeadbeef);
5174 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, creation_access
[i
].open_mode
,
5175 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
, 0, 0,
5176 NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
5177 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe(0x%x) error %d\n",
5178 creation_access
[i
].open_mode
, GetLastError());
5179 access
= get_obj_access(pipe
);
5180 ok(access
== creation_access
[i
].access
,
5181 "CreateNamedPipeA(0x%x) pipe expected access 0x%x (got 0x%x)\n",
5182 creation_access
[i
].open_mode
, creation_access
[i
].access
, access
);
5186 SetLastError(0xdeadbeef);
5187 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_FIRST_PIPE_INSTANCE
,
5188 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
,
5189 0, 0, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
5190 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe error %d\n", GetLastError());
5192 test_default_handle_security(token
, pipe
, &mapping
);
5194 SetLastError(0xdeadbeef);
5195 file
= CreateFileA(WINE_TEST_PIPE
, FILE_ALL_ACCESS
, 0, NULL
, OPEN_EXISTING
, 0, 0);
5196 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5198 access
= get_obj_access(file
);
5199 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
5201 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5203 SetLastError( 0xdeadbeef );
5204 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
5205 map
[i
].generic
, FALSE
, 0);
5206 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5208 access
= get_obj_access(dup
);
5209 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5217 SetLastError(0xdeadbeef);
5218 file
= CreateFileA("\\\\.\\pipe\\", FILE_ALL_ACCESS
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0);
5219 ok(file
!= INVALID_HANDLE_VALUE
|| broken(file
== INVALID_HANDLE_VALUE
) /* before Vista */, "CreateFile error %d\n", GetLastError());
5221 if (file
!= INVALID_HANDLE_VALUE
)
5223 access
= get_obj_access(file
);
5224 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
5226 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5228 SetLastError( 0xdeadbeef );
5229 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
5230 map
[i
].generic
, FALSE
, 0);
5231 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5233 access
= get_obj_access(dup
);
5236 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5238 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5247 static void test_file_security(HANDLE token
)
5249 DWORD ret
, i
, access
, bytes
;
5253 int generic
, mapped
;
5257 { GENERIC_READ
, FILE_GENERIC_READ
},
5258 { GENERIC_WRITE
, FILE_GENERIC_WRITE
},
5259 { GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
5260 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
5262 char temp_path
[MAX_PATH
];
5263 char file_name
[MAX_PATH
];
5266 GetTempPathA(MAX_PATH
, temp_path
);
5267 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
5270 SetLastError(0xdeadbeef);
5271 file
= CreateFileA(file_name
, GENERIC_ALL
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
5272 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5274 access
= get_obj_access(file
);
5275 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
5277 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5279 SetLastError( 0xdeadbeef );
5280 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
5281 map
[i
].generic
, FALSE
, 0);
5282 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5284 access
= get_obj_access(dup
);
5285 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5292 SetLastError(0xdeadbeef);
5293 file
= CreateFileA(file_name
, 0, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
5294 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5296 access
= get_obj_access(file
);
5298 ok(access
== (FILE_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
5301 SetLastError(0xdeadbeef);
5302 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
5303 ok(!ret
, "ReadFile should fail\n");
5304 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
5305 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
5309 SetLastError(0xdeadbeef);
5310 file
= CreateFileA(file_name
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
5311 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5313 access
= get_obj_access(file
);
5315 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
5318 SetLastError(0xdeadbeef);
5319 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
5320 ok(!ret
, "ReadFile should fail\n");
5321 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
5322 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
5325 DeleteFileA(file_name
);
5328 SetLastError(0xdeadbeef);
5329 file
= CreateFileA(temp_path
, GENERIC_ALL
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
5330 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5332 access
= get_obj_access(file
);
5333 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
5335 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5337 SetLastError( 0xdeadbeef );
5338 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
5339 map
[i
].generic
, FALSE
, 0);
5340 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5342 access
= get_obj_access(dup
);
5343 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5350 SetLastError(0xdeadbeef);
5351 file
= CreateFileA(temp_path
, 0, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
5352 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5354 access
= get_obj_access(file
);
5356 ok(access
== (FILE_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
5360 SetLastError(0xdeadbeef);
5361 file
= CreateFileA(temp_path
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
5362 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5364 access
= get_obj_access(file
);
5366 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
5371 static void test_filemap_security(void)
5373 char temp_path
[MAX_PATH
];
5374 char file_name
[MAX_PATH
];
5375 DWORD ret
, i
, access
;
5376 HANDLE file
, mapping
, dup
;
5379 int generic
, mapped
;
5383 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SECTION_QUERY
| SECTION_MAP_READ
},
5384 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SECTION_MAP_WRITE
},
5385 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SECTION_MAP_EXECUTE
},
5386 { GENERIC_ALL
, STANDARD_RIGHTS_REQUIRED
| SECTION_ALL_ACCESS
}
5394 { PAGE_NOACCESS
, 0 },
5395 { PAGE_READONLY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
5396 { PAGE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
},
5397 { PAGE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
5398 { PAGE_EXECUTE
, 0 },
5399 { PAGE_EXECUTE_READ
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
},
5400 { PAGE_EXECUTE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
},
5401 { PAGE_EXECUTE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
}
5404 GetTempPathA(MAX_PATH
, temp_path
);
5405 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
5407 SetLastError(0xdeadbeef);
5408 file
= CreateFileA(file_name
, GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
5409 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
5410 SetFilePointer(file
, 4096, NULL
, FILE_BEGIN
);
5413 for (i
= 0; i
< sizeof(prot_map
)/sizeof(prot_map
[0]); i
++)
5415 SetLastError(0xdeadbeef);
5416 mapping
= CreateFileMappingW(file
, NULL
, prot_map
[i
].prot
, 0, 4096, NULL
);
5417 if (prot_map
[i
].mapped
)
5421 /* NT4 and win2k don't support EXEC on file mappings */
5422 if (prot_map
[i
].prot
== PAGE_EXECUTE_READ
|| prot_map
[i
].prot
== PAGE_EXECUTE_READWRITE
|| prot_map
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
5424 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
5428 ok(mapping
!= 0, "CreateFileMapping(%04x) error %d\n", prot_map
[i
].prot
, GetLastError());
5432 ok(!mapping
, "CreateFileMapping(%04x) should fail\n", prot_map
[i
].prot
);
5433 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
5437 access
= get_obj_access(mapping
);
5438 ok(access
== prot_map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, prot_map
[i
].mapped
, access
);
5440 CloseHandle(mapping
);
5443 SetLastError(0xdeadbeef);
5444 mapping
= CreateFileMappingW(file
, NULL
, PAGE_EXECUTE_READWRITE
, 0, 4096, NULL
);
5447 /* NT4 and win2k don't support EXEC on file mappings */
5448 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
5450 DeleteFileA(file_name
);
5453 ok(mapping
!= 0, "CreateFileMapping error %d\n", GetLastError());
5455 access
= get_obj_access(mapping
);
5456 ok(access
== (STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
),
5457 "expected STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE, got %#x\n", access
);
5459 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5461 SetLastError( 0xdeadbeef );
5462 ret
= DuplicateHandle(GetCurrentProcess(), mapping
, GetCurrentProcess(), &dup
,
5463 map
[i
].generic
, FALSE
, 0);
5464 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5466 access
= get_obj_access(dup
);
5467 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5472 CloseHandle(mapping
);
5474 DeleteFileA(file_name
);
5477 static void test_thread_security(void)
5479 DWORD ret
, i
, access
;
5483 int generic
, mapped
;
5487 { GENERIC_READ
, STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
},
5488 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
| THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
| 0x4 },
5489 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5490 { GENERIC_ALL
, THREAD_ALL_ACCESS_NT4
}
5493 SetLastError(0xdeadbeef);
5494 thread
= CreateThread(NULL
, 0, (void *)0xdeadbeef, NULL
, CREATE_SUSPENDED
, &ret
);
5495 ok(thread
!= 0, "CreateThread error %d\n", GetLastError());
5497 access
= get_obj_access(thread
);
5498 ok(access
== THREAD_ALL_ACCESS_NT4
|| access
== THREAD_ALL_ACCESS_VISTA
, "expected THREAD_ALL_ACCESS, got %#x\n", access
);
5500 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5502 SetLastError( 0xdeadbeef );
5503 ret
= DuplicateHandle(GetCurrentProcess(), thread
, GetCurrentProcess(), &dup
,
5504 map
[i
].generic
, FALSE
, 0);
5505 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5507 access
= get_obj_access(dup
);
5508 switch (map
[i
].generic
)
5511 case GENERIC_EXECUTE
:
5512 ok(access
== map
[i
].mapped
||
5513 access
== (map
[i
].mapped
| THREAD_QUERY_LIMITED_INFORMATION
) /* Vista+ */ ||
5514 access
== (map
[i
].mapped
| THREAD_QUERY_LIMITED_INFORMATION
| THREAD_RESUME
) /* win8 */,
5515 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5519 ok(access
== map
[i
].mapped
||
5520 access
== (map
[i
].mapped
| THREAD_SET_LIMITED_INFORMATION
) /* Vista+ */ ||
5521 access
== (map
[i
].mapped
| THREAD_SET_LIMITED_INFORMATION
| THREAD_RESUME
) /* win8 */,
5522 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5525 ok(access
== map
[i
].mapped
|| access
== THREAD_ALL_ACCESS_VISTA
,
5526 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5529 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5536 SetLastError( 0xdeadbeef );
5537 ret
= DuplicateHandle(GetCurrentProcess(), thread
, GetCurrentProcess(), &dup
,
5538 THREAD_QUERY_INFORMATION
, FALSE
, 0);
5539 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5540 access
= get_obj_access(dup
);
5541 ok(access
== (THREAD_QUERY_INFORMATION
| THREAD_QUERY_LIMITED_INFORMATION
) /* Vista+ */ ||
5542 access
== THREAD_QUERY_INFORMATION
/* before Vista */,
5543 "expected THREAD_QUERY_INFORMATION|THREAD_QUERY_LIMITED_INFORMATION, got %#x\n", access
);
5546 TerminateThread(thread
, 0);
5547 CloseHandle(thread
);
5550 static void test_process_access(void)
5552 DWORD ret
, i
, access
;
5553 HANDLE process
, dup
;
5555 PROCESS_INFORMATION pi
;
5556 char cmdline
[] = "winver.exe";
5559 int generic
, mapped
;
5563 { GENERIC_READ
, STANDARD_RIGHTS_READ
| PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
},
5564 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| PROCESS_SET_QUOTA
| PROCESS_SET_INFORMATION
| PROCESS_SUSPEND_RESUME
|
5565 PROCESS_VM_WRITE
| PROCESS_DUP_HANDLE
| PROCESS_CREATE_PROCESS
| PROCESS_CREATE_THREAD
| PROCESS_VM_OPERATION
},
5566 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5567 { GENERIC_ALL
, PROCESS_ALL_ACCESS_NT4
}
5570 memset(&sti
, 0, sizeof(sti
));
5571 sti
.cb
= sizeof(sti
);
5572 SetLastError(0xdeadbeef);
5573 ret
= CreateProcessA(NULL
, cmdline
, NULL
, NULL
, FALSE
, CREATE_SUSPENDED
, NULL
, NULL
, &sti
, &pi
);
5574 ok(ret
, "CreateProcess() error %d\n", GetLastError());
5576 CloseHandle(pi
.hThread
);
5577 process
= pi
.hProcess
;
5579 access
= get_obj_access(process
);
5580 ok(access
== PROCESS_ALL_ACCESS_NT4
|| access
== PROCESS_ALL_ACCESS_VISTA
, "expected PROCESS_ALL_ACCESS, got %#x\n", access
);
5582 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5584 SetLastError( 0xdeadbeef );
5585 ret
= DuplicateHandle(GetCurrentProcess(), process
, GetCurrentProcess(), &dup
,
5586 map
[i
].generic
, FALSE
, 0);
5587 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5589 access
= get_obj_access(dup
);
5590 switch (map
[i
].generic
)
5593 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
) /* Vista+ */,
5594 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5597 ok(access
== map
[i
].mapped
||
5598 access
== (map
[i
].mapped
| PROCESS_TERMINATE
) /* before Vista */ ||
5599 access
== (map
[i
].mapped
| PROCESS_SET_LIMITED_INFORMATION
) /* win8 */,
5600 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5602 case GENERIC_EXECUTE
:
5603 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
) /* Vista+ */,
5604 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5607 ok(access
== map
[i
].mapped
|| access
== PROCESS_ALL_ACCESS_VISTA
,
5608 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5611 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5618 SetLastError( 0xdeadbeef );
5619 ret
= DuplicateHandle(GetCurrentProcess(), process
, GetCurrentProcess(), &dup
,
5620 PROCESS_QUERY_INFORMATION
, FALSE
, 0);
5621 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5622 access
= get_obj_access(dup
);
5623 ok(access
== (PROCESS_QUERY_INFORMATION
| PROCESS_QUERY_LIMITED_INFORMATION
) /* Vista+ */ ||
5624 access
== PROCESS_QUERY_INFORMATION
/* before Vista */,
5625 "expected PROCESS_QUERY_INFORMATION|PROCESS_QUERY_LIMITED_INFORMATION, got %#x\n", access
);
5628 TerminateProcess(process
, 0);
5629 CloseHandle(process
);
5632 static BOOL
validate_impersonation_token(HANDLE token
, DWORD
*token_type
)
5636 SECURITY_IMPERSONATION_LEVEL sil
;
5640 SetLastError(0xdeadbeef);
5641 ret
= GetTokenInformation(token
, TokenType
, &type
, sizeof(type
), &needed
);
5642 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5643 ok(needed
== sizeof(type
), "GetTokenInformation should return required buffer length\n");
5644 ok(type
== TokenPrimary
|| type
== TokenImpersonation
, "expected TokenPrimary or TokenImpersonation, got %d\n", type
);
5647 if (type
!= TokenImpersonation
) return FALSE
;
5650 SetLastError(0xdeadbeef);
5651 ret
= GetTokenInformation(token
, TokenImpersonationLevel
, &sil
, sizeof(sil
), &needed
);
5652 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5653 ok(needed
== sizeof(sil
), "GetTokenInformation should return required buffer length\n");
5654 ok(sil
== SecurityImpersonation
, "expected SecurityImpersonation, got %d\n", sil
);
5656 needed
= 0xdeadbeef;
5657 SetLastError(0xdeadbeef);
5658 ret
= GetTokenInformation(token
, TokenDefaultDacl
, NULL
, 0, &needed
);
5659 ok(!ret
, "GetTokenInformation should fail\n");
5660 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5661 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5662 ok(needed
> sizeof(TOKEN_DEFAULT_DACL
), "GetTokenInformation returned empty default DACL\n");
5664 needed
= 0xdeadbeef;
5665 SetLastError(0xdeadbeef);
5666 ret
= GetTokenInformation(token
, TokenOwner
, NULL
, 0, &needed
);
5667 ok(!ret
, "GetTokenInformation should fail\n");
5668 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5669 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5670 ok(needed
> sizeof(TOKEN_OWNER
), "GetTokenInformation returned empty token owner\n");
5672 needed
= 0xdeadbeef;
5673 SetLastError(0xdeadbeef);
5674 ret
= GetTokenInformation(token
, TokenPrimaryGroup
, NULL
, 0, &needed
);
5675 ok(!ret
, "GetTokenInformation should fail\n");
5676 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5677 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5678 ok(needed
> sizeof(TOKEN_PRIMARY_GROUP
), "GetTokenInformation returned empty token primary group\n");
5683 static void test_kernel_objects_security(void)
5685 HANDLE token
, process_token
;
5686 DWORD ret
, token_type
;
5688 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
| TOKEN_QUERY
, &process_token
);
5689 ok(ret
, "OpenProcessToken error %d\n", GetLastError());
5691 ret
= validate_impersonation_token(process_token
, &token_type
);
5692 ok(token_type
== TokenPrimary
, "expected TokenPrimary, got %d\n", token_type
);
5693 ok(!ret
, "access token should not be an impersonation token\n");
5695 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
5696 ok(ret
, "DuplicateToken error %d\n", GetLastError());
5698 ret
= validate_impersonation_token(token
, &token_type
);
5699 ok(ret
, "access token should be a valid impersonation token\n");
5700 ok(token_type
== TokenImpersonation
, "expected TokenImpersonation, got %d\n", token_type
);
5702 test_mutex_security(token
);
5703 test_event_security(token
);
5704 test_named_pipe_security(token
);
5705 test_semaphore_security(token
);
5706 test_file_security(token
);
5707 test_filemap_security();
5708 test_thread_security();
5709 test_process_access();
5710 /* FIXME: test other kernel object types */
5712 CloseHandle(process_token
);
5716 static void test_TokenIntegrityLevel(void)
5718 TOKEN_MANDATORY_LABEL
*tml
;
5719 BYTE buffer
[64]; /* using max. 28 byte in win7 x64 */
5723 static SID medium_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5724 {SECURITY_MANDATORY_HIGH_RID
}};
5725 static SID high_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5726 {SECURITY_MANDATORY_MEDIUM_RID
}};
5728 SetLastError(0xdeadbeef);
5729 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &token
);
5730 ok(res
, "got %d with %d (expected TRUE)\n", res
, GetLastError());
5732 SetLastError(0xdeadbeef);
5733 res
= GetTokenInformation(token
, TokenIntegrityLevel
, buffer
, sizeof(buffer
), &size
);
5735 /* not supported before Vista */
5736 if (!res
&& ((GetLastError() == ERROR_INVALID_PARAMETER
) || GetLastError() == ERROR_INVALID_FUNCTION
))
5738 win_skip("TokenIntegrityLevel not supported\n");
5743 ok(res
, "got %u with %u (expected TRUE)\n", res
, GetLastError());
5750 tml
= (TOKEN_MANDATORY_LABEL
*) buffer
;
5751 ok(tml
->Label
.Attributes
== (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
),
5752 "got 0x%x (expected 0x%x)\n", tml
->Label
.Attributes
, (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
));
5754 ok(EqualSid(tml
->Label
.Sid
, &medium_level
) || EqualSid(tml
->Label
.Sid
, &high_level
),
5755 "got %s (expected %s or %s)\n", debugstr_sid(tml
->Label
.Sid
),
5756 debugstr_sid(&medium_level
), debugstr_sid(&high_level
));
5761 static void test_default_dacl_owner_sid(void)
5764 BOOL ret
, defaulted
, present
, found
;
5766 SECURITY_DESCRIPTOR
*sd
;
5767 SECURITY_ATTRIBUTES sa
;
5770 ACCESS_ALLOWED_ACE
*ace
;
5772 sd
= HeapAlloc( GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
5773 ret
= InitializeSecurityDescriptor( sd
, SECURITY_DESCRIPTOR_REVISION
);
5774 ok( ret
, "error %u\n", GetLastError() );
5776 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
5777 sa
.lpSecurityDescriptor
= sd
;
5778 sa
.bInheritHandle
= FALSE
;
5779 handle
= CreateEventA( &sa
, TRUE
, TRUE
, "test_event" );
5780 ok( handle
!= NULL
, "error %u\n", GetLastError() );
5783 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, NULL
, 0, &size
);
5784 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "error %u\n", GetLastError() );
5786 sd
= HeapAlloc( GetProcessHeap(), 0, size
);
5787 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, sd
, size
, &size
);
5788 ok( ret
, "error %u\n", GetLastError() );
5790 owner
= (void *)0xdeadbeef;
5792 ret
= GetSecurityDescriptorOwner( sd
, &owner
, &defaulted
);
5793 ok( ret
, "error %u\n", GetLastError() );
5794 ok( owner
!= (void *)0xdeadbeef, "owner not set\n" );
5795 ok( !defaulted
, "owner defaulted\n" );
5797 dacl
= (void *)0xdeadbeef;
5800 ret
= GetSecurityDescriptorDacl( sd
, &present
, &dacl
, &defaulted
);
5801 ok( ret
, "error %u\n", GetLastError() );
5802 ok( present
, "dacl not present\n" );
5803 ok( dacl
!= (void *)0xdeadbeef, "dacl not set\n" );
5804 ok( !defaulted
, "dacl defaulted\n" );
5808 while (pGetAce( dacl
, index
++, (void **)&ace
))
5810 if (EqualSid( &ace
->SidStart
, owner
)) found
= TRUE
;
5812 ok( found
, "owner sid not found in dacl\n" );
5814 HeapFree( GetProcessHeap(), 0, sa
.lpSecurityDescriptor
);
5815 HeapFree( GetProcessHeap(), 0, sd
);
5816 CloseHandle( handle
);
5819 static void test_AdjustTokenPrivileges(void)
5821 TOKEN_PRIVILEGES tp
, prev
;
5827 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &token
))
5830 if (!LookupPrivilegeValueA(NULL
, SE_BACKUP_NAME
, &luid
))
5836 tp
.PrivilegeCount
= 1;
5837 tp
.Privileges
[0].Luid
= luid
;
5838 tp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
5841 ret
= AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), NULL
, &len
);
5842 ok(ret
, "got %d\n", ret
);
5843 ok(len
== 0xdeadbeef, "got length %d\n", len
);
5846 tp
.PrivilegeCount
= 1;
5847 tp
.Privileges
[0].Luid
= luid
;
5848 tp
.Privileges
[0].Attributes
= 0;
5849 AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), &prev
, NULL
);
5854 static void test_AddAce(void)
5856 static SID
const sidWorld
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
} , { SECURITY_WORLD_RID
} };
5858 char acl_buf
[1024], ace_buf
[256];
5859 ACCESS_ALLOWED_ACE
*ace
= (ACCESS_ALLOWED_ACE
*)ace_buf
;
5860 PACL acl
= (PACL
)acl_buf
;
5863 memset(ace
, 0, sizeof(ace_buf
));
5864 ace
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
5865 ace
->Header
.AceSize
= sizeof(ACCESS_ALLOWED_ACE
)-sizeof(DWORD
)+sizeof(SID
);
5866 memcpy(&ace
->SidStart
, &sidWorld
, sizeof(sidWorld
));
5868 ret
= InitializeAcl(acl
, sizeof(acl_buf
), ACL_REVISION2
);
5869 ok(ret
, "InitializeAcl failed: %d\n", GetLastError());
5871 ret
= AddAce(acl
, ACL_REVISION1
, MAXDWORD
, ace
, ace
->Header
.AceSize
);
5872 ok(ret
, "AddAce failed: %d\n", GetLastError());
5873 ret
= AddAce(acl
, ACL_REVISION2
, MAXDWORD
, ace
, ace
->Header
.AceSize
);
5874 ok(ret
, "AddAce failed: %d\n", GetLastError());
5875 ret
= AddAce(acl
, ACL_REVISION3
, MAXDWORD
, ace
, ace
->Header
.AceSize
);
5876 ok(ret
, "AddAce failed: %d\n", GetLastError());
5877 ok(acl
->AclRevision
== ACL_REVISION3
, "acl->AclRevision = %d\n", acl
->AclRevision
);
5878 ret
= AddAce(acl
, ACL_REVISION4
, MAXDWORD
, ace
, ace
->Header
.AceSize
);
5879 ok(ret
, "AddAce failed: %d\n", GetLastError());
5880 ok(acl
->AclRevision
== ACL_REVISION4
, "acl->AclRevision = %d\n", acl
->AclRevision
);
5881 ret
= AddAce(acl
, ACL_REVISION1
, MAXDWORD
, ace
, ace
->Header
.AceSize
);
5882 ok(ret
, "AddAce failed: %d\n", GetLastError());
5883 ok(acl
->AclRevision
== ACL_REVISION4
, "acl->AclRevision = %d\n", acl
->AclRevision
);
5884 ret
= AddAce(acl
, ACL_REVISION2
, MAXDWORD
, ace
, ace
->Header
.AceSize
);
5885 ok(ret
, "AddAce failed: %d\n", GetLastError());
5887 ret
= AddAce(acl
, MIN_ACL_REVISION
-1, MAXDWORD
, ace
, ace
->Header
.AceSize
);
5888 ok(ret
, "AddAce failed: %d\n", GetLastError());
5889 /* next test succeededs but corrupts ACL */
5890 ret
= AddAce(acl
, MAX_ACL_REVISION
+1, MAXDWORD
, ace
, ace
->Header
.AceSize
);
5891 ok(ret
, "AddAce failed: %d\n", GetLastError());
5892 ok(acl
->AclRevision
== MAX_ACL_REVISION
+1, "acl->AclRevision = %d\n", acl
->AclRevision
);
5893 SetLastError(0xdeadbeef);
5894 ret
= AddAce(acl
, ACL_REVISION1
, MAXDWORD
, ace
, ace
->Header
.AceSize
);
5895 ok(!ret
, "AddAce succeeded\n");
5896 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "GetLastError() = %d\n", GetLastError());
5899 static void test_system_security_access(void)
5901 static const WCHAR testkeyW
[] =
5902 {'S','O','F','T','W','A','R','E','\\','W','i','n','e','\\','S','A','C','L','t','e','s','t',0};
5905 PSECURITY_DESCRIPTOR sd
;
5907 DWORD err
, len
= 128;
5908 TOKEN_PRIVILEGES priv
, *priv_prev
;
5913 if (!OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
|TOKEN_QUERY
, &token
)) return;
5914 if (!LookupPrivilegeValueA( NULL
, SE_SECURITY_NAME
, &luid
))
5916 CloseHandle( token
);
5920 /* ACCESS_SYSTEM_SECURITY requires special privilege */
5921 res
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, testkeyW
, 0, NULL
, 0, KEY_READ
|ACCESS_SYSTEM_SECURITY
, NULL
, &hkey
, NULL
);
5922 if (res
== ERROR_ACCESS_DENIED
)
5924 skip( "unprivileged user\n" );
5925 CloseHandle( token
);
5928 todo_wine
ok( res
== ERROR_PRIVILEGE_NOT_HELD
, "got %d\n", res
);
5930 priv
.PrivilegeCount
= 1;
5931 priv
.Privileges
[0].Luid
= luid
;
5932 priv
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
5934 priv_prev
= HeapAlloc( GetProcessHeap(), 0, len
);
5935 ret
= AdjustTokenPrivileges( token
, FALSE
, &priv
, len
, priv_prev
, &len
);
5936 ok( ret
, "got %u\n", GetLastError());
5938 res
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, testkeyW
, 0, NULL
, 0, KEY_READ
|ACCESS_SYSTEM_SECURITY
, NULL
, &hkey
, NULL
);
5939 if (res
== ERROR_PRIVILEGE_NOT_HELD
)
5941 win_skip( "privilege not held\n" );
5942 HeapFree( GetProcessHeap(), 0, priv_prev
);
5943 CloseHandle( token
);
5946 ok( !res
, "got %d\n", res
);
5948 /* restore privileges */
5949 ret
= AdjustTokenPrivileges( token
, FALSE
, priv_prev
, 0, NULL
, NULL
);
5950 ok( ret
, "got %u\n", GetLastError() );
5951 HeapFree( GetProcessHeap(), 0, priv_prev
);
5953 /* privilege is checked on access */
5954 err
= GetSecurityInfo( hkey
, SE_REGISTRY_KEY
, SACL_SECURITY_INFORMATION
, NULL
, NULL
, NULL
, &sacl
, &sd
);
5955 todo_wine
ok( err
== ERROR_PRIVILEGE_NOT_HELD
, "got %u\n", err
);
5957 priv
.PrivilegeCount
= 1;
5958 priv
.Privileges
[0].Luid
= luid
;
5959 priv
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
5961 priv_prev
= HeapAlloc( GetProcessHeap(), 0, len
);
5962 ret
= AdjustTokenPrivileges( token
, FALSE
, &priv
, len
, priv_prev
, &len
);
5963 ok( ret
, "got %u\n", GetLastError());
5965 err
= GetSecurityInfo( hkey
, SE_REGISTRY_KEY
, SACL_SECURITY_INFORMATION
, NULL
, NULL
, NULL
, &sacl
, &sd
);
5966 ok( err
== ERROR_SUCCESS
, "got %u\n", err
);
5967 RegCloseKey( hkey
);
5970 /* handle created without ACCESS_SYSTEM_SECURITY, privilege held */
5971 res
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, testkeyW
, 0, NULL
, 0, KEY_READ
, NULL
, &hkey
, NULL
);
5972 ok( res
== ERROR_SUCCESS
, "got %d\n", res
);
5975 err
= GetSecurityInfo( hkey
, SE_REGISTRY_KEY
, SACL_SECURITY_INFORMATION
, NULL
, NULL
, NULL
, &sacl
, &sd
);
5976 todo_wine
ok( err
== ERROR_SUCCESS
, "got %u\n", err
);
5977 RegCloseKey( hkey
);
5980 /* restore privileges */
5981 ret
= AdjustTokenPrivileges( token
, FALSE
, priv_prev
, 0, NULL
, NULL
);
5982 ok( ret
, "got %u\n", GetLastError() );
5983 HeapFree( GetProcessHeap(), 0, priv_prev
);
5985 /* handle created without ACCESS_SYSTEM_SECURITY, privilege not held */
5986 res
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, testkeyW
, 0, NULL
, 0, KEY_READ
, NULL
, &hkey
, NULL
);
5987 ok( res
== ERROR_SUCCESS
, "got %d\n", res
);
5989 err
= GetSecurityInfo( hkey
, SE_REGISTRY_KEY
, SACL_SECURITY_INFORMATION
, NULL
, NULL
, NULL
, &sacl
, &sd
);
5990 todo_wine
ok( err
== ERROR_PRIVILEGE_NOT_HELD
, "got %u\n", err
);
5991 RegCloseKey( hkey
);
5993 res
= RegDeleteKeyW( HKEY_LOCAL_MACHINE
, testkeyW
);
5994 ok( !res
, "got %d\n", res
);
5995 CloseHandle( token
);
5998 static void test_GetWindowsAccountDomainSid(void)
6000 char *user
, buffer1
[SECURITY_MAX_SID_SIZE
], buffer2
[SECURITY_MAX_SID_SIZE
];
6001 SID_IDENTIFIER_AUTHORITY domain_ident
= { SECURITY_NT_AUTHORITY
};
6002 PSID domain_sid
= (PSID
*)&buffer1
;
6003 PSID domain_sid2
= (PSID
*)&buffer2
;
6010 if (!pGetWindowsAccountDomainSid
)
6012 win_skip("GetWindowsAccountDomainSid not available\n");
6016 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
6018 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
6019 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
6023 win_skip("Failed to get current user token\n");
6027 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &sid_size
);
6028 ok(!bret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
6029 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
6030 user
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
6031 bret
= GetTokenInformation(token
, TokenUser
, user
, sid_size
, &sid_size
);
6032 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
6034 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
6036 SetLastError(0xdeadbeef);
6037 bret
= pGetWindowsAccountDomainSid(0, 0, 0);
6038 ok(!bret
, "GetWindowsAccountDomainSid succeeded\n");
6039 ok(GetLastError() == ERROR_INVALID_SID
, "expected ERROR_INVALID_SID, got %d\n", GetLastError());
6041 SetLastError(0xdeadbeef);
6042 bret
= pGetWindowsAccountDomainSid(user_sid
, 0, 0);
6043 ok(!bret
, "GetWindowsAccountDomainSid succeeded\n");
6044 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
6046 sid_size
= SECURITY_MAX_SID_SIZE
;
6047 SetLastError(0xdeadbeef);
6048 bret
= pGetWindowsAccountDomainSid(user_sid
, 0, &sid_size
);
6049 ok(!bret
, "GetWindowsAccountDomainSid succeeded\n");
6050 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
6051 ok(sid_size
== GetSidLengthRequired(4), "expected size %d, got %d\n", GetSidLengthRequired(4), sid_size
);
6053 SetLastError(0xdeadbeef);
6054 bret
= pGetWindowsAccountDomainSid(user_sid
, domain_sid
, 0);
6055 ok(!bret
, "GetWindowsAccountDomainSid succeeded\n");
6056 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
6059 SetLastError(0xdeadbeef);
6060 bret
= pGetWindowsAccountDomainSid(user_sid
, domain_sid
, &sid_size
);
6061 ok(!bret
, "GetWindowsAccountDomainSid succeeded\n");
6062 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
6063 ok(sid_size
== GetSidLengthRequired(4), "expected size %d, got %d\n", GetSidLengthRequired(4), sid_size
);
6065 sid_size
= SECURITY_MAX_SID_SIZE
;
6066 bret
= pGetWindowsAccountDomainSid(user_sid
, domain_sid
, &sid_size
);
6067 ok(bret
, "GetWindowsAccountDomainSid failed with error %d\n", GetLastError());
6068 ok(sid_size
== GetSidLengthRequired(4), "expected size %d, got %d\n", GetSidLengthRequired(4), sid_size
);
6069 InitializeSid(domain_sid2
, &domain_ident
, 4);
6070 for (i
= 0; i
< 4; i
++)
6071 *GetSidSubAuthority(domain_sid2
, i
) = *GetSidSubAuthority(user_sid
, i
);
6072 ok(EqualSid(domain_sid
, domain_sid2
), "unexpected domain sid %s != %s\n",
6073 debugstr_sid(domain_sid
), debugstr_sid(domain_sid2
));
6075 HeapFree(GetProcessHeap(), 0, user
);
6078 static void test_GetSidIdentifierAuthority(void)
6080 char buffer
[SECURITY_MAX_SID_SIZE
];
6081 PSID authority_sid
= (PSID
*)buffer
;
6082 PSID_IDENTIFIER_AUTHORITY id
;
6085 if (!pGetSidIdentifierAuthority
)
6087 win_skip("GetSidIdentifierAuthority not available\n");
6091 memset(buffer
, 0xcc, sizeof(buffer
));
6092 ret
= IsValidSid(authority_sid
);
6093 ok(!ret
, "expected FALSE, got %u\n", ret
);
6095 SetLastError(0xdeadbeef);
6096 id
= GetSidIdentifierAuthority(authority_sid
);
6097 ok(id
!= NULL
, "got NULL pointer as identifier authority\n");
6098 ok(GetLastError() == ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %u\n", GetLastError());
6100 SetLastError(0xdeadbeef);
6101 id
= GetSidIdentifierAuthority(NULL
);
6102 ok(id
!= NULL
, "got NULL pointer as identifier authority\n");
6103 ok(GetLastError() == ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %u\n", GetLastError());
6106 static void test_pseudo_tokens(void)
6108 TOKEN_STATISTICS statistics1
, statistics2
;
6113 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &token
);
6114 ok(ret
, "OpenProcessToken failed with error %u\n", GetLastError());
6115 memset(&statistics1
, 0x11, sizeof(statistics1
));
6116 ret
= GetTokenInformation(token
, TokenStatistics
, &statistics1
, sizeof(statistics1
), &retlen
);
6117 ok(ret
, "GetTokenInformation failed with %u\n", GetLastError());
6120 /* test GetCurrentProcessToken() */
6121 SetLastError(0xdeadbeef);
6122 memset(&statistics2
, 0x22, sizeof(statistics2
));
6123 ret
= GetTokenInformation(GetCurrentProcessToken(), TokenStatistics
,
6124 &statistics2
, sizeof(statistics2
), &retlen
);
6125 ok(ret
|| broken(GetLastError() == ERROR_INVALID_HANDLE
),
6126 "GetTokenInformation failed with %u\n", GetLastError());
6128 ok(!memcmp(&statistics1
, &statistics2
, sizeof(statistics1
)), "Token statistics do not match\n");
6130 win_skip("CurrentProcessToken not supported, skipping test\n");
6132 /* test GetCurrentThreadEffectiveToken() */
6133 SetLastError(0xdeadbeef);
6134 memset(&statistics2
, 0x22, sizeof(statistics2
));
6135 ret
= GetTokenInformation(GetCurrentThreadEffectiveToken(), TokenStatistics
,
6136 &statistics2
, sizeof(statistics2
), &retlen
);
6137 ok(ret
|| broken(GetLastError() == ERROR_INVALID_HANDLE
),
6138 "GetTokenInformation failed with %u\n", GetLastError());
6140 ok(!memcmp(&statistics1
, &statistics2
, sizeof(statistics1
)), "Token statistics do not match\n");
6142 win_skip("CurrentThreadEffectiveToken not supported, skipping test\n");
6144 SetLastError(0xdeadbeef);
6145 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
6146 ok(!ret
, "OpenThreadToken should have failed\n");
6147 ok(GetLastError() == ERROR_NO_TOKEN
, "Expected ERROR_NO_TOKEN, got %u\n", GetLastError());
6149 /* test GetCurrentThreadToken() */
6150 SetLastError(0xdeadbeef);
6151 ret
= GetTokenInformation(GetCurrentThreadToken(), TokenStatistics
,
6152 &statistics2
, sizeof(statistics2
), &retlen
);
6153 todo_wine
ok(GetLastError() == ERROR_NO_TOKEN
|| broken(GetLastError() == ERROR_INVALID_HANDLE
),
6154 "Expected ERROR_NO_TOKEN, got %u\n", GetLastError());
6157 START_TEST(security
)
6164 test_process_security_child();
6167 test_kernel_objects_security();
6171 test_CreateWellKnownSid();
6172 test_FileSecurity();
6175 test_LookupAccountSid();
6176 test_LookupAccountName();
6177 test_security_descriptor();
6178 test_process_security();
6179 test_impersonation_level();
6180 test_SetEntriesInAclW();
6181 test_SetEntriesInAclA();
6182 test_CreateDirectoryA();
6183 test_GetNamedSecurityInfoA();
6184 test_ConvertStringSecurityDescriptor();
6185 test_ConvertSecurityDescriptorToString();
6186 test_PrivateObjectSecurity();
6188 test_GetWindowsAccountDomainSid();
6189 test_GetSecurityInfo();
6190 test_GetSidSubAuthority();
6191 test_CheckTokenMembership();
6193 test_GetUserNameA();
6194 test_GetUserNameW();
6195 test_CreateRestrictedToken();
6196 test_TokenIntegrityLevel();
6197 test_default_dacl_owner_sid();
6198 test_AdjustTokenPrivileges();
6200 test_system_security_access();
6201 test_GetSidIdentifierAuthority();
6202 test_pseudo_tokens();