Création d’un DACL

La création d’une liste de contrôle d’accès discrétionnaire (DACL) appropriée est une partie nécessaire et importante du développement d’applications. Étant donné qu’une liste DACL NULL autorise tous les types d’accès à tous les utilisateurs, n’utilisez pas de DLL NULL .

L’exemple suivant montre comment créer correctement une liste DACL. L’exemple contient une fonction, CreateMyDACL, qui utilise le langage de définition de descripteur de sécurité (SDDL) pour définir le contrôle d’accès accordé et refusé dans une liste DACL. Pour fournir un accès différent aux objets de votre application, modifiez la fonction CreateMyDACL si nécessaire.

Dans l’exemple :

  1. La fonction main transmet l’adresse d’une structure SECURITY_ATTRIBUTES à la fonction CreateMyDACL.

  2. La fonction CreateMyDACL utilise des chaînes SDDL pour :

    • Refuser l’accès aux utilisateurs invités et anonymes.
    • Autoriser l’accès en lecture/écriture/exécution aux utilisateurs authentifiés.
    • Autorisez le contrôle total aux administrateurs.

    Pour plus d’informations sur les formats de chaîne SDDL, consultez Security Descriptor String Format.

  3. La fonction CreateMyDACL appelle la fonction ConvertStringSecurityDescriptorToSecurityDescriptor pour convertir les chaînes SDDL en descripteur de sécurité. Le descripteur de sécurité est désigné par le membre lpSecurityDescriptor de la structure SECURITY_ATTRIBUTES . CreateMyDACL envoie la valeur de retour de ConvertStringSecurityDescriptorToSecurityDescriptor à la fonction main.

  4. La fonction main utilise la structure de SECURITY_ATTRIBUTES mise à jour pour spécifier la liste DACL d’un nouveau dossier créé par la fonction CreateDirectory.

  5. Lorsque la fonction main est terminée à l’aide de la structure SECURITY_ATTRIBUTES, la fonction main libère la mémoire allouée au membre lpSecurityDescriptor en appelant la fonction LocalFree.

Notes

Pour compiler correctement des fonctions SDDL telles que ConvertStringSecurityDescriptorToSecurityDescriptor, vous devez définir la constante _WIN32_WINNT comme 0x0500 ou supérieure.

 

#define _WIN32_WINNT 0x0500

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

#pragma comment(lib, "advapi32.lib")

BOOL CreateMyDACL(SECURITY_ATTRIBUTES *);

void main()
{
     SECURITY_ATTRIBUTES  sa;
      
     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
     sa.bInheritHandle = FALSE;  

     // Call function to set the DACL. The DACL
     // is set in the SECURITY_ATTRIBUTES 
     // lpSecurityDescriptor member.
     if (!CreateMyDACL(&sa))
     {
         // Error encountered; generate message and exit.
         printf("Failed CreateMyDACL\n");
         exit(1);
     }

     // Use the updated SECURITY_ATTRIBUTES to specify
     // security attributes for securable objects.
     // This example uses security attributes during
     // creation of a new directory.
     if (0 == CreateDirectory(TEXT("C:\\MyFolder"), &sa))
     {
         // Error encountered; generate message and exit.
         printf("Failed CreateDirectory\n");
         exit(1);
     }

     // Free the memory allocated for the SECURITY_DESCRIPTOR.
     if (NULL != LocalFree(sa.lpSecurityDescriptor))
     {
         // Error encountered; generate message and exit.
         printf("Failed LocalFree\n");
         exit(1);
     }
}


// CreateMyDACL.
//    Create a security descriptor that contains the DACL 
//    you want.
//    This function uses SDDL to make Deny and Allow ACEs.
//
// Parameter:
//    SECURITY_ATTRIBUTES * pSA
//    Pointer to a SECURITY_ATTRIBUTES structure. It is your
//    responsibility to properly initialize the 
//    structure and to free the structure's 
//    lpSecurityDescriptor member when you have
//    finished using it. To free the structure's 
//    lpSecurityDescriptor member, call the 
//    LocalFree function.
// 
// Return value:
//    FALSE if the address to the structure is NULL. 
//    Otherwise, this function returns the value from the
//    ConvertStringSecurityDescriptorToSecurityDescriptor 
//    function.
BOOL CreateMyDACL(SECURITY_ATTRIBUTES * pSA)
{
     // Define the SDDL for the DACL. This example sets 
     // the following access:
     //     Built-in guests are denied all access.
     //     Anonymous logon is denied all access.
     //     Authenticated users are allowed 
     //     read/write/execute access.
     //     Administrators are allowed full control.
     // Modify these values as needed to generate the proper
     // DACL for your application. 
     TCHAR * szSD = TEXT("D:")       // Discretionary ACL
        TEXT("(D;OICI;GA;;;BG)")     // Deny access to 
                                     // built-in guests
        TEXT("(D;OICI;GA;;;AN)")     // Deny access to 
                                     // anonymous logon
        TEXT("(A;OICI;GRGWGX;;;AU)") // Allow 
                                     // read/write/execute 
                                     // to authenticated 
                                     // users
        TEXT("(A;OICI;GA;;;BA)");    // Allow full control 
                                     // to administrators

    if (NULL == pSA)
        return FALSE;

     return ConvertStringSecurityDescriptorToSecurityDescriptor(
                szSD,
                SDDL_REVISION_1,
                &(pSA->lpSecurityDescriptor),
                NULL);
}