21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

* state information.<br />

*/<br />

hProcess = GetCurrentProcess( );<br />

if (!OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES, &hProcessToken)) {<br />

LocalFree(pNewState);<br />

return FALSE;<br />

}<br />

/* Adjust the privileges, specifying FALSE for DisableAllPrivileges so that the<br />

* NewState argument will be used instead. Don't request information regarding<br />

* the token's previous state by specifying 0 for the last three arguments.<br />

*/<br />

bResult = AdjustTokenPrivileges(hProcessToken, FALSE, pNewState, 0, 0, 0);<br />

/* Cleanup and return the success or failure of the adjustment */<br />

CloseHandle(hProcessToken);<br />

LocalFree(pNewState);<br />

return bResult;<br />

}<br />

Working with SID_AND_ATTRIBUTES structures<br />

A SID_AND_ATTRIBUTES structure contains two fields: Sid and Attributes. The Sid field<br />

is of type PSID, which is a variable-sized object that should never be directly manipulated<br />

by application-level code. The meaning of the Attributes field varies depending<br />

on the use of the structure. When a SID_AND_ATTRIBUTES structure is being used<br />

for disabling SIDs (enabling the “deny” attribute), the Attributes field is ignored.<br />

When a SID_AND_ATTRIBUTES structure is being used for restricting SIDs, the<br />

Attributes field should always be set to 0. In both cases, it’s best to set the<br />

Attributes field to 0.<br />

Initializing the Sid field of a SID_AND_ATTRIBUTES structure can be done in a number<br />

of ways, but perhaps one of the most useful ways is to use LookupAccountName( ) to<br />

obtain the SID for a specific user or group name. The following code demonstrates<br />

how to look up the SID for a name:<br />

#include <br />

PSID SpcLookupSidByName(LPCTSTR lpAccountName, PSID_NAME_USE peUse) {<br />

PSID pSid;<br />

DWORD cbSid, cchReferencedDomainName;<br />

LPTSTR ReferencedDomainName;<br />

SID_NAME_USE eUse;<br />

cbSid = cchReferencedDomainName = 0;<br />

if (!LookupAccountName(0, lpAccountName, 0, &cbSid, 0, &cchReferencedDomainName,<br />

&eUse)) return 0;<br />

if (!(pSid = LocalAlloc(LMEM_FIXED, cbSid))) return 0;<br />

ReferencedDomainName = LocalAlloc(LMEM_FIXED,<br />

(cchReferencedDomainName + 1) * sizeof(TCHAR));<br />

if (!ReferencedDomainName) {<br />

LocalFree(pSid);<br />

14 | Chapter 1: Safe Initialization<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!