Link to home
Start Free TrialLog in
Avatar of camster123
camster123

asked on

How to port an Discretionary Access Control Lists(DACL) C++ function written for Windows 7 and the Microsoft Visual Studio 2013 C++ compiler so I can compile and link it with Ubuntu Linux g++?

I would like to port the following Discretionary  Access Control Lists(DACL) C++ function written for Windows 7 and the Microsoft Visual Studio 2013 C++ compiler so I can compile and link it using Ubuntu Linux 14.04 g++ . Please suggest how to do this using GNU C/C+ library functions and any custom C++ functions and classes I might have to write.

// The following function initializes the supplied security descriptor
// with a DACL that grants the Authenticated Users group GENERIC_READ,
// GENERIC_WRITE, and GENERIC_EXECUTE access.
//
// The function returns NULL if any of the access control APIs fail.
// Otherwise, it returns a PVOID pointer that should be freed by calling
// FreeRestrictedSD() after the security descriptor has been used to
// create the object.

void* CDataTransferUser::BuildRestrictedSD(PSECURITY_DESCRIPTOR pSD)
{
   DWORD  dwAclLength;

   PSID   pAuthenticatedUsersSID = NULL;

   PACL   pDACL   = NULL;
   BOOL   bResult = FALSE;

   PACCESS_ALLOWED_ACE pACE = NULL;

   SID_IDENTIFIER_AUTHORITY siaNT = SECURITY_NT_AUTHORITY;
   
   SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION;
   
   __try
   {
      // initialize the security descriptor
      if (!InitializeSecurityDescriptor(pSD,
            SECURITY_DESCRIPTOR_REVISION))
        {
         printf("InitializeSecurityDescriptor() failed with error %d\n",
               GetLastError());
         __leave;
      }

      // obtain a sid for the Authenticated Users Group
      if (!AllocateAndInitializeSid(&siaNT, 1,
            SECURITY_AUTHENTICATED_USER_RID, 0, 0, 0, 0, 0, 0, 0,
            &pAuthenticatedUsersSID))
        {
         printf("AllocateAndInitializeSid() failed with error %d\n",
               GetLastError());
         __leave;
      }

      // NOTE:
      //
      // The Authenticated Users group includes all user accounts that
      // have been successfully authenticated by the system. If access
      // must be restricted to a specific user or group other than
      // Authenticated Users, the SID can be constructed using the
      // LookupAccountSid() API based on a user or group name.

      // calculate the DACL length
      dwAclLength = sizeof(ACL)
            // add space for Authenticated Users group ACE
            + sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)
            + GetLengthSid(pAuthenticatedUsersSID);

      // allocate memory for the DACL
      pDACL = (PACL) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
            dwAclLength);
      if (!pDACL)
        {
         printf("HeapAlloc() failed with error %d\n", GetLastError());
         __leave;
      }

      // initialize the DACL
      if (!InitializeAcl(pDACL, dwAclLength, ACL_REVISION))
        {
         printf("InitializeAcl() failed with error %d\n",
               GetLastError());
         __leave;
      }
     
      // add the Authenticated Users group ACE to the DACL with
      // GENERIC_READ, GENERIC_WRITE, and GENERIC_EXECUTE access
      if (!AddAccessAllowedAce(pDACL, ACL_REVISION,
            GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,
            pAuthenticatedUsersSID))
        {
         printf("AddAccessAllowedAce() failed with error %d\n",
               GetLastError());
         __leave;
      }

      // set the DACL in the security descriptor
      if (!SetSecurityDescriptorDacl(pSD, TRUE, pDACL, FALSE))
        {
         printf("SetSecurityDescriptorDacl() failed with error %d\n",
               GetLastError());
         __leave;
      }

      bResult = TRUE;
     
   } __finally
   {
      if (pAuthenticatedUsersSID)
           FreeSid(pAuthenticatedUsersSID);
   }

   if (bResult == FALSE)
   {
      if (pDACL)
              HeapFree(GetProcessHeap(), 0, pDACL);
      pDACL = NULL;
   }

   return (void*) pDACL;
}

Any help is greatly appreciated.
Avatar of gheist
gheist
Flag of Belgium image

You are missing out include files.
Ubuntu does not implement windows security model with RIDS etc.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of camster123
camster123

ASKER

jkr's solution was very useful and complete. Thank you.