* Copyright (c) 2006 VMware, Inc. All rights reserved.
* **********************************************************/
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of VMware, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#include "tools.h"
#include <windows.h>
* or aren't present on NT [ConvertSidToStringSid()] or 2000 [CreateWellKnownSid()]
*/
* ConvertStringSidToSid are not supported. Use the following code to
* convert a SID to string format.
*/
BOOL
GetTextualSid(PSID pSid,
LPTSTR TextualSid,
LPDWORD lpdwBufferLen
)
{
PSID_IDENTIFIER_AUTHORITY psia;
DWORD dwSubAuthorities;
DWORD dwSidRev = SID_REVISION;
DWORD dwCounter;
DWORD dwSidSize;
if (!IsValidSid(pSid))
return FALSE;
psia = GetSidIdentifierAuthority(pSid);
dwSubAuthorities = *GetSidSubAuthorityCount(pSid);
dwSidSize = (15 + 12 + (12 * dwSubAuthorities) + 1) * sizeof(TCHAR);
if (*lpdwBufferLen < dwSidSize) {
*lpdwBufferLen = dwSidSize;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}
dwSidSize = wsprintf(TextualSid, TEXT("S-%lu-"), dwSidRev);
if ((psia->Value[0] != 0) || (psia->Value[1] != 0)) {
dwSidSize += wsprintf(
TextualSid + lstrlen(TextualSid), TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
(USHORT)psia->Value[0], (USHORT)psia->Value[1], (USHORT)psia->Value[2],
(USHORT)psia->Value[3], (USHORT)psia->Value[4], (USHORT)psia->Value[5]);
} else {
dwSidSize +=
wsprintf(TextualSid + lstrlen(TextualSid), TEXT("%lu"),
(ULONG)(psia->Value[5]) + (ULONG)(psia->Value[4] << 8) +
(ULONG)(psia->Value[3] << 16) + (ULONG)(psia->Value[2] << 24));
}
for (dwCounter = 0; dwCounter < dwSubAuthorities; dwCounter++) {
dwSidSize += wsprintf(TextualSid + dwSidSize, TEXT("-%lu"),
*GetSidSubAuthority(pSid, dwCounter));
}
return TRUE;
}
void
test_sid(void)
{
TCHAR buf[100];
DWORD size = sizeof(buf);
PSID psid;
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthz/security/searching_for_a_sid_in_an_access_token_in_c__.asp
*/
SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
BOOL ok;
if (!AllocateAndInitializeSid(&SIDAuth, 2, SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psid)) {
print("AllocateAndInitializeSid Error %u\n", GetLastError());
return;
}
ok = GetTextualSid(psid, buf, &size);
if (ok) {
print("BUILTIN\\Administrators: %s\n", buf);
} else {
print("FAILED!\n", buf);
if (!IsValidSid(psid))
print("invalid SID!\n", buf);
}
}
int __declspec(dllexport) make_a_lib(int arg)
{
test_sid();
return 0;
}
BOOL APIENTRY
DllMain(HANDLE hModule, DWORD reason_for_call, LPVOID Reserved)
{
switch (reason_for_call) {
case DLL_PROCESS_ATTACH: print("in delay bind dll\n"); break;
}
return TRUE;
}