Share via


Modifica degli ACL di un oggetto in C++

Nell'esempio seguente viene aggiunta una voce di controllo di accesso (ACE) all'elenco di controllo di accesso discrezionale (DACL) di un oggetto .

Il parametro AccessMode determina il tipo di nuovo ACE e il modo in cui il nuovo ACE viene combinato con gli ACL esistenti per il trustee specificato. Usare i flag GRANT_ACCESS, SET_ACCESS, DENY_ACCESS o REVOKE_ACCESS nel parametro AccessMode . Per informazioni su questi flag, vedere ACCESS_MODE.

È possibile usare codice simile per usare un elenco di controllo di accesso di sistema (SACL). Specificare SACL_SECURITY_INFORMATION nelle funzioni GetNamedSecurityInfo e SetNamedSecurityInfo per ottenere e impostare sacl per l'oggetto. Usare i flag SET_AUDIT_SUCCESS, SET_AUDIT_FAILURE e REVOKE_ACCESS nel parametro AccessMode . Per informazioni su questi flag, vedere ACCESS_MODE.

Utilizzare questo codice per aggiungere un ace specifico dell'oggetto all'elenco DACL di un oggetto servizio directory. Per specificare i GUID in un ace specifico dell'oggetto, impostare il parametro TrusteeForm su TRUSTEE_IS_OBJECTS_AND_NAME o TRUSTEE_IS_OBJECTS_AND_SID e impostare il parametro pszTrustee come puntatore a una struttura OBJECTS_AND_NAME o OBJECTS_AND_SID .

In questo esempio viene usata la funzione GetNamedSecurityInfo per ottenere l'elenco DACL esistente. Riempie quindi una struttura EXPLICIT_ACCESS con informazioni su un ACE e usa la funzione SetEntriesInAcl per unire il nuovo ACE con qualsiasi ACL esistente nell'elenco DACL. Infine, l'esempio chiama la funzione SetNamedSecurityInfo per collegare il nuovo DACL al descrittore di sicurezza dell'oggetto.

#include <windows.h>
#include <stdio.h>

DWORD AddAceToObjectsSecurityDescriptor (
    LPTSTR pszObjName,          // name of object
    SE_OBJECT_TYPE ObjectType,  // type of object
    LPTSTR pszTrustee,          // trustee for new ACE
    TRUSTEE_FORM TrusteeForm,   // format of trustee structure
    DWORD dwAccessRights,       // access mask for new ACE
    ACCESS_MODE AccessMode,     // type of ACE
    DWORD dwInheritance         // inheritance flags for new ACE
) 
{
    DWORD dwRes = 0;
    PACL pOldDACL = NULL, pNewDACL = NULL;
    PSECURITY_DESCRIPTOR pSD = NULL;
    EXPLICIT_ACCESS ea;

    if (NULL == pszObjName) 
        return ERROR_INVALID_PARAMETER;

    // Get a pointer to the existing DACL.

    dwRes = GetNamedSecurityInfo(pszObjName, ObjectType, 
          DACL_SECURITY_INFORMATION,
          NULL, NULL, &pOldDACL, NULL, &pSD);
    if (ERROR_SUCCESS != dwRes) {
        printf( "GetNamedSecurityInfo Error %u\n", dwRes );
        goto Cleanup; 
    }  

    // Initialize an EXPLICIT_ACCESS structure for the new ACE. 

    ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
    ea.grfAccessPermissions = dwAccessRights;
    ea.grfAccessMode = AccessMode;
    ea.grfInheritance= dwInheritance;
    ea.Trustee.TrusteeForm = TrusteeForm;
    ea.Trustee.ptstrName = pszTrustee;

    // Create a new ACL that merges the new ACE
    // into the existing DACL.

    dwRes = SetEntriesInAcl(1, &ea, pOldDACL, &pNewDACL);
    if (ERROR_SUCCESS != dwRes)  {
        printf( "SetEntriesInAcl Error %u\n", dwRes );
        goto Cleanup; 
    }  

    // Attach the new ACL as the object's DACL.

    dwRes = SetNamedSecurityInfo(pszObjName, ObjectType, 
          DACL_SECURITY_INFORMATION,
          NULL, NULL, pNewDACL, NULL);
    if (ERROR_SUCCESS != dwRes)  {
        printf( "SetNamedSecurityInfo Error %u\n", dwRes );
        goto Cleanup; 
    }  

    Cleanup:

        if(pSD != NULL) 
            LocalFree((HLOCAL) pSD); 
        if(pNewDACL != NULL) 
            LocalFree((HLOCAL) pNewDACL); 

        return dwRes;
}