Getting the Logon SID in C++

简介:

logon security identifier (SID) identifies the logon session associated with an access token. A typical use of a logon SID is in an ACE that allows access for the duration of a client's logon session. For example, a Windows service can use the LogonUser function to start a new logon session. The LogonUser function returns an access token from which the service can extract the logon SID. The service can then use the SID in an ACE that allows the client's logon session to access the interactive window station and desktop.

The following example gets the logon SID from an access token. It uses the GetTokenInformation function to fill a TOKEN_GROUPS buffer with an array of the group SIDs from an access token. This array includes the logon SID, which is identified by the SE_GROUP_LOGON_ID attribute. The example function allocates a buffer for the logon SID; it is the caller's responsibility to free the buffer.

None.gif BOOL GetLogonSID (HANDLE hToken, PSID  * ppsid) 
ExpandedBlockStart.gif  {
InBlock.gif   BOOL bSuccess  =  FALSE;
InBlock.gif   DWORD dwIndex;
InBlock.gif   DWORD dwLength  =   0 ;
InBlock.gif   PTOKEN_GROUPS ptg  =  NULL;
InBlock.gif
InBlock.gif //  Verify the parameter passed in is not NULL. 
InBlock.gif 
     if  (NULL  ==  ppsid)
InBlock.gif         goto  Cleanup;
InBlock.gif
InBlock.gif //  Get required buffer size and allocate the TOKEN_GROUPS buffer. 
InBlock.gif 

InBlock.gif    if  ( ! GetTokenInformation(
InBlock.gif         hToken,          //  handle to the access token 
InBlock.gif 
         TokenGroups,     //  get information about the token's groups  
InBlock.gif 
         (LPVOID) ptg,    //  pointer to TOKEN_GROUPS buffer 
InBlock.gif 
          0 ,               //  size of buffer 
InBlock.gif 
          & dwLength        //  receives required buffer size 
InBlock.gif 
      )) 
ExpandedSubBlockStart.gif    {
InBlock.gif       if  (GetLastError()  !=  ERROR_INSUFFICIENT_BUFFER) 
InBlock.gif          goto  Cleanup;
InBlock.gif
InBlock.gif      ptg  =  (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(),
InBlock.gif         HEAP_ZERO_MEMORY, dwLength);
InBlock.gif
InBlock.gif       if  (ptg  ==  NULL)
InBlock.gif          goto  Cleanup;
ExpandedSubBlockEnd.gif   } 

InBlock.gif 
InBlock.gif //  Get the token group information from the access token. 
InBlock.gif 

InBlock.gif    if  ( ! GetTokenInformation(
InBlock.gif         hToken,          //  handle to the access token 
InBlock.gif 
         TokenGroups,     //  get information about the token's groups  
InBlock.gif 
         (LPVOID) ptg,    //  pointer to TOKEN_GROUPS buffer 
InBlock.gif 
         dwLength,        //  size of buffer 
InBlock.gif 
          & dwLength        //  receives required buffer size 
InBlock.gif 
         )) 
ExpandedSubBlockStart.gif    {
InBlock.gif       goto  Cleanup;
ExpandedSubBlockEnd.gif   } 

InBlock.gif 
InBlock.gif //  Loop through the groups to find the logon SID. 
InBlock.gif 

InBlock.gif    for  (dwIndex  =   0 ; dwIndex  <  ptg -> GroupCount; dwIndex ++ ) 
InBlock.gif       if  ((ptg -> Groups[dwIndex].Attributes  &  SE_GROUP_LOGON_ID)
InBlock.gif              ==   SE_GROUP_LOGON_ID) 
ExpandedSubBlockStart.gif       {
InBlock.gif       //  Found the logon SID; make a copy of it. 
InBlock.gif 

InBlock.gif         dwLength  =  GetLengthSid(ptg -> Groups[dwIndex].Sid);
InBlock.gif          * ppsid  =  (PSID) HeapAlloc(GetProcessHeap(),
InBlock.gif                     HEAP_ZERO_MEMORY, dwLength);
InBlock.gif          if  ( * ppsid  ==  NULL)
InBlock.gif              goto  Cleanup;
InBlock.gif          if  ( ! CopySid(dwLength,  * ppsid, ptg -> Groups[dwIndex].Sid)) 
ExpandedSubBlockStart.gif          {
InBlock.gif             HeapFree(GetProcessHeap(),  0 , (LPVOID) * ppsid);
InBlock.gif              goto  Cleanup;
ExpandedSubBlockEnd.gif         } 

InBlock.gif          break ;
ExpandedSubBlockEnd.gif      } 

InBlock.gif 
InBlock.gif   bSuccess  =  TRUE;
InBlock.gif
InBlock.gifCleanup: 
InBlock.gif
InBlock.gif //  Free the buffer for the token groups. 
InBlock.gif 

InBlock.gif    if  (ptg  !=  NULL)
InBlock.gif      HeapFree(GetProcessHeap(),  0 , (LPVOID)ptg);
InBlock.gif
InBlock.gif    return  bSuccess;
ExpandedBlockEnd.gif

None.gif

The following function frees the buffer allocated by the  GetLogonSID  example function.
None.gifVOID FreeLogonSID (PSID *ppsid) 
ExpandedBlockStart.gif {
InBlock.gif    HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid);
ExpandedBlockEnd.gif}
目录
相关文章
|
Oracle 关系型数据库 数据安全/隐私保护
在Oracle中,ORA-01017 invalid username password; logon denied原因有哪些
在Oracle中,ORA-01017 invalid username password; logon denied原因有哪些
1713 0
|
Oracle 关系型数据库 数据库
【DG】搭建DG时,报错:ORA-01017 invalid username password logon denied
【DG】搭建DG时,报错:ORA-01017 invalid username password logon denied
733 0
|
SQL
ORA-00030: User session ID does not exist.
同事在Toad里面执行SQL语句时,突然无线网络中断了,让我检查一下具体情况,如下所示(有些信息,用xxx替换,因为是在处理那些历史归档数据,使用的一个特殊用户,所以可以用下面SQL找到对应的会话信息): SQL> SELECT B.
1876 0
|
SQL 测试技术 数据库
0131 ORA-00942 and AUTHID CURRENT_USER
[20180131]ORA-00942 and AUTHID CURRENT_USER.txt --//偶尔写一个存储过程调用一些系统视图.经常遇到一些ORA-00942,有时候很烦.
1125 0
|
安全 Windows Shell