21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sid<br />

Buffer containing the SID to look up. This argument may not be specified as<br />

NULL.<br />

Name<br />

Buffer into which the name will be written. Initially, you may specify this argument<br />

as NULL to determine how large a buffer is required to hold the name.<br />

cbName<br />

Pointer to an integer that both specifies the size of the buffer to receive the name,<br />

and receives the size of the buffer required for the name.<br />

ReferencedDomainName<br />

Buffer into which the domain name where the SID was found is to be written.<br />

Initially, you may specify this argument as NULL to determine how large a buffer<br />

is required to hold the domain name.<br />

cbReferencedDomainName<br />

Pointer to an integer that both specifies the size of the buffer to receive the<br />

domain name, and receives the size of the buffer required for the domain name.<br />

peUse<br />

Pointer to an enumeration that receives the type of SID to which the looked-up<br />

SID corresponds. The most commonly returned values are SidTypeUser (1) and<br />

SidTypeGroup (2).<br />

The following function, SpcLookupSid( ), is essentially a wrapper around<br />

LookupAccountSid( ). It handles the nuances of performing SID lookup, including<br />

allocating the necessary buffers and error conditions. If the SID is successfully found,<br />

the return will be a pointer to a dynamically allocated buffer containing the user or<br />

group name, which you must later free using LocalFree( ). If the SID could not be<br />

found, NULL will be returned, and GetLastError( ) will return ERROR_NONE_MAPPED. If<br />

any other kind of error occurs, SpcLookupSid( ) will return NULL, and GetLastError( )<br />

will return the relevant error code.<br />

#include <br />

LPTSTR SpcLookupSid(LPCTSTR lpszSystemName, PSID Sid) {<br />

DWORD cbName, cbReferencedDomainName;<br />

LPTSTR lpszName, ReferencedDomainName;<br />

SID_NAME_USE eUse;<br />

cbName = cbReferencedDomainName = 0;<br />

if (LookupAccountSid(lpszSystemName, Sid, 0, &cbName,<br />

0, &cbReferencedDomainName, &eUse)) {<br />

SetLastError(ERROR_NONE_MAPPED);<br />

return 0;<br />

}<br />

if (GetLastError( ) != ERROR_INSUFFICIENT_BUFFER) return 0;<br />

if (!(lpszName = (LPTSTR)LocalAlloc(LMEM_FIXED, cbName))) return 0;<br />

378 | Chapter 8: Authentication and Key Exchange<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!