Security Identifiers
Windows uses SIDs to identify users and groups. The program can look up a SID from the account name, which can be a user, group, domain, and so on. The account can be on a remote system. The first step is to determine the SID from an account name.
BOOL LookupAccountName (
LPCTSTR lpSystemName,
LPCTSTR lpAccountName,
PSID Sid,
LPDWORD cbSid,
LPTSTR ReferencedDomainName,
LPDWORD cbReferencedDomainName,
PSID_NAME_USE peUse)
Parameters
lpSystemName and lpAccountName point to the system and account names. Frequently, lpSystemName is NULL to indicate the local system.Sid is the returned information, which is of size *cbSid. The function will fail, returning the required size, if the buffer is not large enough.ReferencedDomainName is a string of length *cbReferencedDomainName characters. The length parameter should be initialized to the buffer size (the usual techniques are used to process failures). The return value shows the domain where the name is found. The account name Administrators will return BUILTIN, whereas a user account name will return that same user name.peUse points to a SID_NAME_USE (enumerated type) variable and can be tested for values such as SidTypeWellKnownGroup, SidTypeUser, SidTypeGroup, and so on.
Getting the Account and User Names
Given a SID, you reverse the process and obtain the account name using LookupAccountSid. Specify the SID and get the name in return. The account name can be any name available to the process. Some names, such as Everyone, are well known.
BOOL LookupAccountSid (
LPCTSTR lpSystemName,
PSID Sid,
LPTSTR lpAccountName,
LPDWORD cbName,
LPTSTR ReferencedDomainName,
LPDWORD cbReferencedDomainName,
PSID_NAME_USE peUse)
Obtain the process's user account name (the logged-in user) with the GetUserName function.
BOOL GetUserName (
LPTSTR lpBuffer,
LPDWORD nSize)
The user name and length are returned in the conventional manner.It is possible to create and manage SIDs using functions such as InitializeSid and AllocateAndInitializeSid. The examples confine themselves, however, to SIDs obtained from account names.Once SIDs are known, they can be entered into an initialized security descriptor.
BOOL SetSecurityDescriptorOwner (
PSECURITY_DESCRIPTOR pSecurityDescriptor,
PSID pOwner,
BOOL bOwnerDefaulted)
BOOL SetSecurityDescriptorGroup (
PSECURITY_DESCRIPTOR pSecurityDescriptor,
PSID pGroup,
BOOL bGroupDefaulted)
pSecurityDescriptor points to the appropriate security descriptor, and pOwner (or pGroup) is the address of the owner's (group's) SID. bOwnerDefaulted (or bGroupDefaulted) indicates, if trUE, that a default mechanism is used to derive the owner (or primary group) information. The SE_OWNER_DEFAULTED and SE_GROUP_DEFAULTED flags within the SECURITY_DESCRIPTOR_CONTROL structure are set according to these two parameters.The similar functions GetSecurityDescriptorOwner and GetSecurityDescriptorGroup return the SID (either owner or group) from a security descriptor.