博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Starting an Interactive Client Process in C++
阅读量:6904 次
发布时间:2019-06-27

本文共 11829 字,大约阅读时间需要 39 分钟。

The following example uses the LogonUser function to start a new logon session for a client. The example gets the logon SID from the client's access token, and uses it to add access control entries (ACEs) to the discretionary access control list (DACL) of the interactive window station and desktop. The ACEs allow the client access to the interactive desktop for the duration of the logon session. Next, the example calls the function to ensure that it has access to the client's executable file. A call to the CreateProcessAsUser function creates the client's process, specifying that it run in the interactive desktop. Note that your process must have the SE_ASSIGNPRIMARYTOKEN_NAME and SE_INCREASE_QUOTA_NAME privileges for successful execution of CreateProcessAsUser. Before the function returns, it calls the  function to end the caller's impersonation of the client.

This example calls the GetLogonSID and FreeLogonSID functions described in .

None.gif 
#define  DESKTOP_ALL (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | \ 
None.gifDESKTOP_CREATEMENU  |  DESKTOP_HOOKCONTROL  |  DESKTOP_JOURNALRECORD  |  \
None.gifDESKTOP_JOURNALPLAYBACK  |  DESKTOP_ENUMERATE  |  DESKTOP_WRITEOBJECTS  |  \
None.gifDESKTOP_SWITCHDESKTOP  |  STANDARD_RIGHTS_REQUIRED)
None.gif
None.gif 
#define  WINSTA_ALL (WINSTA_ENUMDESKTOPS | WINSTA_READATTRIBUTES | \ 
None.gifWINSTA_ACCESSCLIPBOARD  |  WINSTA_CREATEDESKTOP  |  WINSTA_WRITEATTRIBUTES  |  \
None.gifWINSTA_ACCESSGLOBALATOMS  |  WINSTA_EXITWINDOWS  |  WINSTA_ENUMERATE  |  \
None.gifWINSTA_READSCREEN  |  STANDARD_RIGHTS_REQUIRED)
None.gif
None.gif 
#define  GENERIC_ACCESS (GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | \ 
None.gifGENERIC_ALL)
None.gif
None.gifBOOL AddAceToWindowStation(HWINSTA hwinsta, PSID psid);
None.gif
None.gifBOOL AddAceToDesktop(HDESK hdesk, PSID psid);
None.gif
None.gifBOOL StartInteractiveClientProcess (
None.gif    LPTSTR lpszUsername,     
// 
 client to log on 
None.gif 
    LPTSTR lpszDomain,       
// 
 domain of client's account 
None.gif 
    LPTSTR lpszPassword,     
// 
 client's password 
None.gif 
    LPTSTR lpCommandLine     
// 
 command line to execute 
None.gif 
ExpandedBlockStart.gif  {
InBlock.gif   HANDLE      hToken;
InBlock.gif   HDESK       hdesk  =  NULL;
InBlock.gif   HWINSTA     hwinsta  =  NULL, hwinstaSave  =  NULL;
InBlock.gif   PROCESS_INFORMATION pi;
InBlock.gif   PSID pSid  =  NULL;
InBlock.gif   STARTUPINFO si;
InBlock.gif   BOOL bResult  =  FALSE;
InBlock.gif
InBlock.gif 
// 
 Log the client on to the local computer. 
InBlock.gif 
InBlock.gif    
if  ( ! LogonUser(
InBlock.gif           lpszUsername,
InBlock.gif           lpszDomain,
InBlock.gif           lpszPassword,
InBlock.gif           LOGON32_LOGON_INTERACTIVE,
InBlock.gif           LOGON32_PROVIDER_DEFAULT,
InBlock.gif            & hToken) ) 
ExpandedSubBlockStart.gif    {
InBlock.gif       
goto  Cleanup;
ExpandedSubBlockEnd.gif   } 
InBlock.gif 
InBlock.gif 
// 
 Save a handle to the caller's current window station. 
InBlock.gif 
InBlock.gif    
if  ( (hwinstaSave  =  GetProcessWindowStation() )  ==  NULL)
InBlock.gif       
goto  Cleanup;
InBlock.gif
InBlock.gif 
// 
 Get a handle to the interactive window station. 
InBlock.gif 
InBlock.gif   hwinsta  =  OpenWindowStation(
InBlock.gif        " winsta0 " ,                    
// 
 the interactive window station  
InBlock.gif 
       FALSE,                        
// 
 handle is not inheritable 
InBlock.gif 
       READ_CONTROL  |  WRITE_DAC);    
// 
 rights to read/write the DACL 
InBlock.gif 
InBlock.gif    
if  (hwinsta  ==  NULL) 
InBlock.gif       
goto  Cleanup;
InBlock.gif
InBlock.gif 
// 
 To get the correct default desktop, set the caller's 
InBlock.gif
 
// 
 window station to the interactive window station. 
InBlock.gif 
InBlock.gif    
if  ( ! SetProcessWindowStation(hwinsta))
InBlock.gif       
goto  Cleanup;
InBlock.gif
InBlock.gif 
// 
 Get a handle to the interactive desktop. 
InBlock.gif 
InBlock.gif   hdesk  =  OpenDesktop(
InBlock.gif       " default " ,      
// 
 the interactive window station  
InBlock.gif 
       0 ,              
// 
 no interaction with other desktop processes 
InBlock.gif 
      FALSE,          
// 
 handle is not inheritable 
InBlock.gif 
      READ_CONTROL  |   
// 
 request the rights to read and write the DACL 
InBlock.gif 
      WRITE_DAC  |  
InBlock.gif      DESKTOP_WRITEOBJECTS  |  
InBlock.gif      DESKTOP_READOBJECTS);
InBlock.gif
InBlock.gif 
// 
 Restore the caller's window station. 
InBlock.gif 
InBlock.gif    
if  ( ! SetProcessWindowStation(hwinstaSave)) 
InBlock.gif       
goto  Cleanup;
InBlock.gif
InBlock.gif    
if  (hdesk  ==  NULL) 
InBlock.gif       
goto  Cleanup;
InBlock.gif
InBlock.gif 
// 
 Get the SID for the client's logon session. 
InBlock.gif 
InBlock.gif    
if  ( ! GetLogonSID(hToken,  & pSid)) 
InBlock.gif       
goto  Cleanup;
InBlock.gif
InBlock.gif 
// 
 Allow logon SID full access to interactive window station. 
InBlock.gif 
InBlock.gif    
if  ( !  AddAceToWindowStation(hwinsta, pSid) ) 
InBlock.gif       
goto  Cleanup;
InBlock.gif
InBlock.gif 
// 
 Allow logon SID full access to interactive desktop. 
InBlock.gif 
InBlock.gif    
if  ( !  AddAceToDesktop(hdesk, pSid) ) 
InBlock.gif       
goto  Cleanup;
InBlock.gif
InBlock.gif 
// 
 Impersonate client to ensure access to executable file. 
InBlock.gif 
InBlock.gif    
if  ( !  ImpersonateLoggedOnUser(hToken) ) 
InBlock.gif       
goto  Cleanup;
InBlock.gif
InBlock.gif 
// 
 Initialize the STARTUPINFO structure.
InBlock.gif
 
// 
 Specify that the process runs in the interactive desktop. 
InBlock.gif 
InBlock.gif   ZeroMemory( & si,  
sizeof (STARTUPINFO));
InBlock.gif   si.cb =   
sizeof (STARTUPINFO);
InBlock.gif   si.lpDesktop  =  TEXT( " winsta0\\default " );
InBlock.gif
InBlock.gif 
// 
 Launch the process in the client's logon session. 
InBlock.gif 
InBlock.gif   bResult  =  CreateProcessAsUser(
InBlock.gif      hToken,             
// 
 client's access token 
InBlock.gif 
      NULL,               
// 
 file to execute 
InBlock.gif 
      lpCommandLine,      
// 
 command line 
InBlock.gif 
      NULL,               
// 
 pointer to process SECURITY_ATTRIBUTES 
InBlock.gif 
      NULL,               
// 
 pointer to thread SECURITY_ATTRIBUTES 
InBlock.gif 
      FALSE,              
// 
 handles are not inheritable 
InBlock.gif 
      NORMAL_PRIORITY_CLASS  |  CREATE_NEW_CONSOLE,    
// 
 creation flags 
InBlock.gif 
      NULL,               
// 
 pointer to new environment block  
InBlock.gif 
      NULL,               
// 
 name of current directory  
InBlock.gif 
       & si,                
// 
 pointer to STARTUPINFO structure 
InBlock.gif 
       & pi                 
// 
 receives information about new process 
InBlock.gif 
   ); 
InBlock.gif
InBlock.gif 
// 
 End impersonation of client. 
InBlock.gif 
InBlock.gif   RevertToSelf();
InBlock.gif
InBlock.gif    
if  (bResult  &&  pi.hProcess  !=  INVALID_HANDLE_VALUE) 
ExpandedSubBlockStart.gif    { 
InBlock.gif      WaitForSingleObject(pi.hProcess, INFINITE); 
InBlock.gif      CloseHandle(pi.hProcess); 
ExpandedSubBlockEnd.gif   }  
InBlock.gif
InBlock.gif    
if  (pi.hThread  !=  INVALID_HANDLE_VALUE)
InBlock.gif      CloseHandle(pi.hThread);  
InBlock.gif
InBlock.gifCleanup: 
InBlock.gif
InBlock.gif    
if  (hwinstaSave  !=  NULL)
InBlock.gif      SetProcessWindowStation (hwinstaSave);
InBlock.gif
InBlock.gif 
// 
 Free the buffer for the logon SID. 
InBlock.gif 
InBlock.gif    
if  (pSid)
InBlock.gif      FreeLogonSID( & pSid);
InBlock.gif
InBlock.gif 
// 
 Close the handles to the interactive window station and desktop. 
InBlock.gif 
InBlock.gif    
if  (hwinsta)
InBlock.gif      CloseWindowStation(hwinsta);
InBlock.gif
InBlock.gif    
if  (hdesk)
InBlock.gif      CloseDesktop(hdesk);
InBlock.gif
InBlock.gif 
// 
 Close the handle to the client's access token. 
InBlock.gif 
InBlock.gif    
if  (hToken  !=  INVALID_HANDLE_VALUE)
InBlock.gif      CloseHandle(hToken);  
InBlock.gif
InBlock.gif    
return  bResult;
ExpandedBlockEnd.gif
None.gif 
None.gifBOOL AddAceToWindowStation(HWINSTA hwinsta, PSID psid)
ExpandedBlockStart.gif  {
InBlock.gif   ACCESS_ALLOWED_ACE    * pace;
InBlock.gif   ACL_SIZE_INFORMATION aclSizeInfo;
InBlock.gif   BOOL                 bDaclExist;
InBlock.gif   BOOL                 bDaclPresent;
InBlock.gif   BOOL                 bSuccess  =  FALSE;
InBlock.gif   DWORD                dwNewAclSize;
InBlock.gif   DWORD                dwSidSize  =   0 ;
InBlock.gif   DWORD                dwSdSizeNeeded;
InBlock.gif   PACL                 pacl;
InBlock.gif   PACL                 pNewAcl;
InBlock.gif   PSECURITY_DESCRIPTOR psd  =  NULL;
InBlock.gif   PSECURITY_DESCRIPTOR psdNew  =  NULL;
InBlock.gif   PVOID                pTempAce;
InBlock.gif   SECURITY_INFORMATION si  =  DACL_SECURITY_INFORMATION;
InBlock.gif   unsigned  
int          i;
InBlock.gif
InBlock.gif   __try
ExpandedSubBlockStart.gif    {
InBlock.gif       
// 
 Obtain the DACL for the window station. 
InBlock.gif 
InBlock.gif       
if  ( ! GetUserObjectSecurity(
InBlock.gif             hwinsta,
InBlock.gif              & si,
InBlock.gif             psd,
InBlock.gif             dwSidSize,
InBlock.gif              & dwSdSizeNeeded)
InBlock.gif      )
InBlock.gif       
if  (GetLastError()  ==  ERROR_INSUFFICIENT_BUFFER)
ExpandedSubBlockStart.gif       {
InBlock.gif         psd  =  (PSECURITY_DESCRIPTOR)HeapAlloc(
InBlock.gif               GetProcessHeap(),
InBlock.gif               HEAP_ZERO_MEMORY,
InBlock.gif               dwSdSizeNeeded);
InBlock.gif
InBlock.gif          
if  (psd  ==  NULL)
InBlock.gif            __leave;
InBlock.gif
InBlock.gif         psdNew  =  (PSECURITY_DESCRIPTOR)HeapAlloc(
InBlock.gif               GetProcessHeap(),
InBlock.gif               HEAP_ZERO_MEMORY,
InBlock.gif               dwSdSizeNeeded);
InBlock.gif
InBlock.gif          
if  (psdNew  ==  NULL)
InBlock.gif            __leave;
InBlock.gif
InBlock.gif         dwSidSize  =  dwSdSizeNeeded;
InBlock.gif
InBlock.gif          
if  ( ! GetUserObjectSecurity(
InBlock.gif               hwinsta,
InBlock.gif                & si,
InBlock.gif               psd,
InBlock.gif               dwSidSize,
InBlock.gif                & dwSdSizeNeeded)
InBlock.gif         )
InBlock.gif            __leave;
ExpandedSubBlockEnd.gif      } 
InBlock.gif       
else 
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Create a new DACL. 
InBlock.gif 
InBlock.gif       
if  ( ! InitializeSecurityDescriptor(
InBlock.gif            psdNew,
InBlock.gif            SECURITY_DESCRIPTOR_REVISION)
InBlock.gif      )
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Get the DACL from the security descriptor. 
InBlock.gif 
InBlock.gif       
if  ( ! GetSecurityDescriptorDacl(
InBlock.gif            psd,
InBlock.gif             & bDaclPresent,
InBlock.gif             & pacl,
InBlock.gif             & bDaclExist)
InBlock.gif      )
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Initialize the ACL. 
InBlock.gif 
InBlock.gif      ZeroMemory( & aclSizeInfo,  
sizeof (ACL_SIZE_INFORMATION));
InBlock.gif      aclSizeInfo.AclBytesInUse  =   
sizeof (ACL);
InBlock.gif
InBlock.gif       
// 
 Call only if the DACL is not NULL. 
InBlock.gif 
InBlock.gif       
if  (pacl  !=  NULL)
ExpandedSubBlockStart.gif       {
InBlock.gif          
// 
 get the file ACL size info 
InBlock.gif 
          
if  ( ! GetAclInformation(
InBlock.gif               pacl,
InBlock.gif               (LPVOID) & aclSizeInfo,
InBlock.gif                
sizeof (ACL_SIZE_INFORMATION),
InBlock.gif               AclSizeInformation)
InBlock.gif         )
InBlock.gif            __leave;
ExpandedSubBlockEnd.gif      } 
InBlock.gif 
InBlock.gif       
// 
 Compute the size of the new ACL. 
InBlock.gif 
InBlock.gif      dwNewAclSize  =  aclSizeInfo.AclBytesInUse  +  ( 2 * 
sizeof (ACCESS_ALLOWED_ACE))  +  
InBlock.gif( 2 * GetLengthSid(psid))  -  ( 2 * 
sizeof (DWORD));
InBlock.gif
InBlock.gif       
// 
 Allocate memory for the new ACL. 
InBlock.gif 
InBlock.gif      pNewAcl  =  (PACL)HeapAlloc(
InBlock.gif            GetProcessHeap(),
InBlock.gif            HEAP_ZERO_MEMORY,
InBlock.gif            dwNewAclSize);
InBlock.gif
InBlock.gif       
if  (pNewAcl  ==  NULL)
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Initialize the new DACL. 
InBlock.gif 
InBlock.gif       
if  ( ! InitializeAcl(pNewAcl, dwNewAclSize, ACL_REVISION))
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 If DACL is present, copy it to a new DACL. 
InBlock.gif 
InBlock.gif       
if  (bDaclPresent)
ExpandedSubBlockStart.gif       {
InBlock.gif          
// 
 Copy the ACEs to the new ACL. 
InBlock.gif 
          
if  (aclSizeInfo.AceCount)
ExpandedSubBlockStart.gif          {
InBlock.gif             
for  (i = 0 ; i  <  aclSizeInfo.AceCount; i ++ )
ExpandedSubBlockStart.gif             {
InBlock.gif                
// 
 Get an ACE. 
InBlock.gif 
                
if  ( ! GetAce(pacl, i,  & pTempAce))
InBlock.gif                  __leave;
InBlock.gif
InBlock.gif                
// 
 Add the ACE to the new ACL. 
InBlock.gif 
                
if  ( ! AddAce(
InBlock.gif                     pNewAcl,
InBlock.gif                     ACL_REVISION,
InBlock.gif                     MAXDWORD,
InBlock.gif                     pTempAce,
InBlock.gif                    ((PACE_HEADER)pTempAce) -> AceSize)
InBlock.gif               )
InBlock.gif                  __leave;
ExpandedSubBlockEnd.gif            } 
ExpandedSubBlockEnd.gif         } 
ExpandedSubBlockEnd.gif      } 
InBlock.gif 
InBlock.gif       
// 
 Add the first ACE to the window station. 
InBlock.gif 
InBlock.gif      pace  =  (ACCESS_ALLOWED_ACE  * )HeapAlloc(
InBlock.gif            GetProcessHeap(),
InBlock.gif            HEAP_ZERO_MEMORY,
InBlock.gif             
sizeof (ACCESS_ALLOWED_ACE)  +  GetLengthSid(psid)  - 
InBlock.gif                   
sizeof (DWORD));
InBlock.gif
InBlock.gif       
if  (pace  ==  NULL)
InBlock.gif         __leave;
InBlock.gif
InBlock.gif      pace -> Header.AceType   =  ACCESS_ALLOWED_ACE_TYPE;
InBlock.gif      pace -> Header.AceFlags  =  CONTAINER_INHERIT_ACE  | 
InBlock.gif                   INHERIT_ONLY_ACE  |  OBJECT_INHERIT_ACE;
InBlock.gif      pace -> Header.AceSize   =   
sizeof (ACCESS_ALLOWED_ACE)  + 
InBlock.gif                   GetLengthSid(psid)  -   
sizeof (DWORD);
InBlock.gif      pace -> Mask             =  GENERIC_ACCESS;
InBlock.gif
InBlock.gif       
if  ( ! CopySid(GetLengthSid(psid),  & pace -> SidStart, psid))
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
if  ( ! AddAce(
InBlock.gif            pNewAcl,
InBlock.gif            ACL_REVISION,
InBlock.gif            MAXDWORD,
InBlock.gif            (LPVOID)pace,
InBlock.gif            pace -> Header.AceSize)
InBlock.gif      )
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Add the second ACE to the window station. 
InBlock.gif 
InBlock.gif      pace -> Header.AceFlags  =  NO_PROPAGATE_INHERIT_ACE;
InBlock.gif      pace -> Mask             =  WINSTA_ALL;
InBlock.gif
InBlock.gif       
if  ( ! AddAce(
InBlock.gif            pNewAcl,
InBlock.gif            ACL_REVISION,
InBlock.gif            MAXDWORD,
InBlock.gif            (LPVOID)pace,
InBlock.gif            pace -> Header.AceSize)
InBlock.gif      )
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Set a new DACL for the security descriptor. 
InBlock.gif 
InBlock.gif       
if  ( ! SetSecurityDescriptorDacl(
InBlock.gif            psdNew,
InBlock.gif            TRUE,
InBlock.gif            pNewAcl,
InBlock.gif            FALSE)
InBlock.gif      )
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Set the new security descriptor for the window station. 
InBlock.gif 
InBlock.gif       
if  ( ! SetUserObjectSecurity(hwinsta,  & si, psdNew))
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Indicate success. 
InBlock.gif 
InBlock.gif      bSuccess  =  TRUE;
ExpandedSubBlockEnd.gif   } 
InBlock.gif   __finally
ExpandedSubBlockStart.gif    {
InBlock.gif       
// 
 Free the allocated buffers. 
InBlock.gif 
InBlock.gif       
if  (pace  !=  NULL)
InBlock.gif         HeapFree(GetProcessHeap(),  0 , (LPVOID)pace);
InBlock.gif
InBlock.gif       
if  (pNewAcl  !=  NULL)
InBlock.gif         HeapFree(GetProcessHeap(),  0 , (LPVOID)pNewAcl);
InBlock.gif
InBlock.gif       
if  (psd  !=  NULL)
InBlock.gif         HeapFree(GetProcessHeap(),  0 , (LPVOID)psd);
InBlock.gif
InBlock.gif       
if  (psdNew  !=  NULL)
InBlock.gif         HeapFree(GetProcessHeap(),  0 , (LPVOID)psdNew);
ExpandedSubBlockEnd.gif   } 
InBlock.gif 
InBlock.gif    
return  bSuccess;
InBlock.gif
ExpandedBlockEnd.gif
None.gif 
None.gifBOOL AddAceToDesktop(HDESK hdesk, PSID psid)
ExpandedBlockStart.gif  {
InBlock.gif   ACL_SIZE_INFORMATION aclSizeInfo;
InBlock.gif   BOOL                 bDaclExist;
InBlock.gif   BOOL                 bDaclPresent;
InBlock.gif   BOOL                 bSuccess  =  FALSE;
InBlock.gif   DWORD                dwNewAclSize;
InBlock.gif   DWORD                dwSidSize  =   0 ;
InBlock.gif   DWORD                dwSdSizeNeeded;
InBlock.gif   PACL                 pacl;
InBlock.gif   PACL                 pNewAcl;
InBlock.gif   PSECURITY_DESCRIPTOR psd  =  NULL;
InBlock.gif   PSECURITY_DESCRIPTOR psdNew  =  NULL;
InBlock.gif   PVOID                pTempAce;
InBlock.gif   SECURITY_INFORMATION si  =  DACL_SECURITY_INFORMATION;
InBlock.gif   unsigned  
int          i;
InBlock.gif
InBlock.gif   __try
ExpandedSubBlockStart.gif    {
InBlock.gif       
// 
 Obtain the security descriptor for the desktop object. 
InBlock.gif 
InBlock.gif       
if  ( ! GetUserObjectSecurity(
InBlock.gif            hdesk,
InBlock.gif             & si,
InBlock.gif            psd,
InBlock.gif            dwSidSize,
InBlock.gif             & dwSdSizeNeeded))
ExpandedSubBlockStart.gif       {
InBlock.gif          
if  (GetLastError()  ==  ERROR_INSUFFICIENT_BUFFER)
ExpandedSubBlockStart.gif          {
InBlock.gif            psd  =  (PSECURITY_DESCRIPTOR)HeapAlloc(
InBlock.gif                  GetProcessHeap(),
InBlock.gif                  HEAP_ZERO_MEMORY,
InBlock.gif                  dwSdSizeNeeded );
InBlock.gif
InBlock.gif             
if  (psd  ==  NULL)
InBlock.gif               __leave;
InBlock.gif
InBlock.gif            psdNew  =  (PSECURITY_DESCRIPTOR)HeapAlloc(
InBlock.gif                  GetProcessHeap(),
InBlock.gif                  HEAP_ZERO_MEMORY,
InBlock.gif                  dwSdSizeNeeded);
InBlock.gif
InBlock.gif             
if  (psdNew  ==  NULL)
InBlock.gif               __leave;
InBlock.gif
InBlock.gif            dwSidSize  =  dwSdSizeNeeded;
InBlock.gif
InBlock.gif             
if  ( ! GetUserObjectSecurity(
InBlock.gif                  hdesk,
InBlock.gif                   & si,
InBlock.gif                  psd,
InBlock.gif                  dwSidSize,
InBlock.gif                   & dwSdSizeNeeded)
InBlock.gif            )
InBlock.gif               __leave;
ExpandedSubBlockEnd.gif         } 
InBlock.gif          
else 
InBlock.gif            __leave;
ExpandedSubBlockEnd.gif      } 
InBlock.gif 
InBlock.gif       
// 
 Create a new security descriptor. 
InBlock.gif 
InBlock.gif       
if  ( ! InitializeSecurityDescriptor(
InBlock.gif            psdNew,
InBlock.gif            SECURITY_DESCRIPTOR_REVISION)
InBlock.gif      )
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Obtain the DACL from the security descriptor. 
InBlock.gif 
InBlock.gif       
if  ( ! GetSecurityDescriptorDacl(
InBlock.gif            psd,
InBlock.gif             & bDaclPresent,
InBlock.gif             & pacl,
InBlock.gif             & bDaclExist)
InBlock.gif      )
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Initialize. 
InBlock.gif 
InBlock.gif      ZeroMemory( & aclSizeInfo,  
sizeof (ACL_SIZE_INFORMATION));
InBlock.gif      aclSizeInfo.AclBytesInUse  =   
sizeof (ACL);
InBlock.gif
InBlock.gif       
// 
 Call only if NULL DACL. 
InBlock.gif 
InBlock.gif       
if  (pacl  !=  NULL)
ExpandedSubBlockStart.gif       {
InBlock.gif          
// 
 Determine the size of the ACL information. 
InBlock.gif 
InBlock.gif          
if  ( ! GetAclInformation(
InBlock.gif               pacl,
InBlock.gif               (LPVOID) & aclSizeInfo,
InBlock.gif                
sizeof (ACL_SIZE_INFORMATION),
InBlock.gif               AclSizeInformation)
InBlock.gif         )
InBlock.gif            __leave;
ExpandedSubBlockEnd.gif      } 
InBlock.gif 
InBlock.gif       
// 
 Compute the size of the new ACL. 
InBlock.gif 
InBlock.gif      dwNewAclSize  =  aclSizeInfo.AclBytesInUse  + 
InBlock.gif             
sizeof (ACCESS_ALLOWED_ACE)  + 
InBlock.gif            GetLengthSid(psid)  -   
sizeof (DWORD);
InBlock.gif
InBlock.gif       
// 
 Allocate buffer for the new ACL. 
InBlock.gif 
InBlock.gif      pNewAcl  =  (PACL)HeapAlloc(
InBlock.gif            GetProcessHeap(),
InBlock.gif            HEAP_ZERO_MEMORY,
InBlock.gif            dwNewAclSize);
InBlock.gif
InBlock.gif       
if  (pNewAcl  ==  NULL)
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Initialize the new ACL. 
InBlock.gif 
InBlock.gif       
if  ( ! InitializeAcl(pNewAcl, dwNewAclSize, ACL_REVISION))
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 If DACL is present, copy it to a new DACL. 
InBlock.gif 
InBlock.gif       
if  (bDaclPresent)
ExpandedSubBlockStart.gif       {
InBlock.gif          
// 
 Copy the ACEs to the new ACL. 
InBlock.gif 
          
if  (aclSizeInfo.AceCount)
ExpandedSubBlockStart.gif          {
InBlock.gif             
for  (i = 0 ; i  <  aclSizeInfo.AceCount; i ++ )
ExpandedSubBlockStart.gif             {
InBlock.gif                
// 
 Get an ACE. 
InBlock.gif 
                
if  ( ! GetAce(pacl, i,  & pTempAce))
InBlock.gif                  __leave;
InBlock.gif
InBlock.gif                
// 
 Add the ACE to the new ACL. 
InBlock.gif 
                
if  ( ! AddAce(
InBlock.gif                  pNewAcl,
InBlock.gif                  ACL_REVISION,
InBlock.gif                  MAXDWORD,
InBlock.gif                  pTempAce,
InBlock.gif                  ((PACE_HEADER)pTempAce) -> AceSize)
InBlock.gif               )
InBlock.gif                  __leave;
ExpandedSubBlockEnd.gif            } 
ExpandedSubBlockEnd.gif         } 
ExpandedSubBlockEnd.gif      } 
InBlock.gif 
InBlock.gif       
// 
 Add ACE to the DACL. 
InBlock.gif 
InBlock.gif       
if  ( ! AddAccessAllowedAce(
InBlock.gif            pNewAcl,
InBlock.gif            ACL_REVISION,
InBlock.gif            DESKTOP_ALL,
InBlock.gif            psid)
InBlock.gif      )
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Set new DACL to the new security descriptor. 
InBlock.gif 
InBlock.gif       
if  ( ! SetSecurityDescriptorDacl(
InBlock.gif            psdNew,
InBlock.gif            TRUE,
InBlock.gif            pNewAcl,
InBlock.gif            FALSE)
InBlock.gif      )
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Set the new security descriptor for the desktop object. 
InBlock.gif 
InBlock.gif       
if  ( ! SetUserObjectSecurity(hdesk,  & si, psdNew))
InBlock.gif         __leave;
InBlock.gif
InBlock.gif       
// 
 Indicate success. 
InBlock.gif 
InBlock.gif      bSuccess  =  TRUE;
ExpandedSubBlockEnd.gif   } 
InBlock.gif   __finally
ExpandedSubBlockStart.gif    {
InBlock.gif       
// 
 Free buffers. 
InBlock.gif 
InBlock.gif       
if  (pNewAcl  !=  NULL)
InBlock.gif         HeapFree(GetProcessHeap(),  0 , (LPVOID)pNewAcl);
InBlock.gif
InBlock.gif       
if  (psd  !=  NULL)
InBlock.gif         HeapFree(GetProcessHeap(),  0 , (LPVOID)psd);
InBlock.gif
InBlock.gif       
if  (psdNew  !=  NULL)
InBlock.gif         HeapFree(GetProcessHeap(),  0 , (LPVOID)psdNew);
ExpandedSubBlockEnd.gif   } 
InBlock.gif 
InBlock.gif    
return  bSuccess;
ExpandedBlockEnd.gif

转载地址:http://xqmdl.baihongyu.com/

你可能感兴趣的文章
刷前端面经笔记(九)
查看>>
98. Validate Binary Search Tree
查看>>
《Java编程思想》读书笔记-对象导论
查看>>
mysql 的delete from where 子查询的一些限制
查看>>
【Android】Retrofit 2.0 的使用
查看>>
Java程序员幽默爆笑锦集
查看>>
工作中常用到的ES6语法
查看>>
SAPGUI系统登录页面配置的SAProuter有什么用
查看>>
小程序hover-class点击态效果——小程序体验
查看>>
安装chrome插件
查看>>
Vue 中给组件绑定原生事件
查看>>
Vue CLI3.0 中使用jQuery 和 Bootstrap
查看>>
this 改变this的指向
查看>>
测试格式
查看>>
gcc版本升级
查看>>
个人愚见: React 和 Vue 区别
查看>>
深入理解Docker架构与实现
查看>>
「译」代码优化策略 — Idle Until Urgent
查看>>
机器学习项目失败的9个原因,你中招了吗?
查看>>
前端:单页面应用和多页面应用
查看>>