Verifica dell'accesso client con aCL in C++

Nell'esempio seguente viene illustrato come un server potrebbe controllare i diritti di accesso che un descrittore di sicurezza consente a un client. L'esempio usa la funzione ImpersonateNamedPipeClient ; tuttavia, funzionerebbe lo stesso usando una qualsiasi delle altre funzioni di rappresentazione. Dopo aver rappresentato il client, nell'esempio viene chiamata la funzione OpenThreadToken per ottenere il token di rappresentazione. Chiama quindi la funzione MapGenericMask per convertire tutti i diritti di accesso generici ai diritti specifici e standard corrispondenti in base al mapping specificato nella struttura GENERIC_MAPPING .

La funzione AccessCheck controlla i diritti di accesso richiesti per i diritti consentiti per il client nell'elenco dati del descrittore di sicurezza. Per controllare l'accesso e generare una voce nel registro eventi di sicurezza, usare la funzione AccessCheckAndAuditAlarm .

#include <windows.h>
#pragma comment(lib, "advapi32.lib")

BOOL ImpersonateAndCheckAccess(
  HANDLE hNamedPipe,               // handle of pipe to impersonate
  PSECURITY_DESCRIPTOR pSD,        // security descriptor to check
  DWORD dwAccessDesired,           // access rights to check
  PGENERIC_MAPPING pGeneric,       // generic mapping for object
  PDWORD pdwAccessAllowed          // returns allowed access rights
) 
{
   HANDLE hToken;
   PRIVILEGE_SET PrivilegeSet;
   DWORD dwPrivSetSize = sizeof( PRIVILEGE_SET );
   BOOL fAccessGranted=FALSE;

// Impersonate the client.

   if (! ImpersonateNamedPipeClient(hNamedPipe) ) 
      return FALSE;

// Get an impersonation token with the client's security context.

   if (! OpenThreadToken( GetCurrentThread(), TOKEN_ALL_ACCESS,
         TRUE, &hToken ))
   {
      goto Cleanup;
   }

// Use the GENERIC_MAPPING structure to convert any 
// generic access rights to object-specific access rights.

   MapGenericMask( &dwAccessDesired, pGeneric );

// Check the client's access rights.

   if( !AccessCheck( 
      pSD,                 // security descriptor to check
      hToken,              // impersonation token
      dwAccessDesired,     // requested access rights
      pGeneric,            // pointer to GENERIC_MAPPING
      &PrivilegeSet,       // receives privileges used in check
      &dwPrivSetSize,      // size of PrivilegeSet buffer
      pdwAccessAllowed,    // receives mask of allowed access rights
      &fAccessGranted ))   // receives results of access check
   {
      goto Cleanup;
   }

Cleanup:

   RevertToSelf();

   if (hToken != INVALID_HANDLE_VALUE)
      CloseHandle(hToken);  

   return fAccessGranted;
}