Link to home
Start Free TrialLog in
Avatar of tgemini
tgemini

asked on

Directory Permissions

Hello,

I am writing a procedure used to set permissions on a file/directory to the standard ones in the dropdown box on a files permissions (ie Read, Change, Full, Add etc). I have it all sorted on files, except for the bitmask for No Access, but my problem is that I cant figure how the set the two properties for a directory. I can set the first ok, but cant figure out or find anywhere how to set the second set of attributes, and as such am continually getting attributes like (RX) (Not Specified). Can anyone help me out on how to set the second attribute?

Kind Regards,
Bob
Avatar of jkr
jkr
Flag of Germany image

Which OS? Is it UN*X?
Avatar of tgemini
tgemini

ASKER

Oh sorry, missed probably the most vital part :)
Win NT
Avatar of tgemini

ASKER

Oh yeah, and at this stage I am using the AddAccessAllowedAce(), coding on Visual Studio 6
You need to combine the attriibutes you want using the bitwise OR operator  ("|"), like

SetFileAttribute("C:\\FILE.TXT",FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)

to set both the hidden and system attributes.

Let me know if you have any questions.
Todd - attributes have nothing to do with permissions on NT.

tgemini - could you elaborate on the difficulties you're encountering?
You're right. I misundestood.  Good thing I didn't answer.  :-)
Avatar of tgemini

ASKER

Sorry it took me so long to respond, I had problems with my login.

I can set the permissions on a file with a single call to AddAccessAllowedAce(), with the appropriate SID and permissions bitmask passed. When I try to apply permissions to a directory, I can set the first set of permissions easily, as they are set exactly the same as a file, however I cannot set the second set. I also note that the defines for permissions bitmask on a directory have the same bitmasks as those for a file, so I conclude that you do not set this second set just with a different bitmask, but by applying a second ACE to the ACL with something else different. However, I am unable to find any information on exactly what you are supposed to do differently to set the second set of permissions. Every web site I have come across seems to have nothing more to say than "This is very poorly documented, and we dont know how to do it" :)
Avatar of tgemini

ASKER

Adjusted points to 500
Avatar of tgemini

ASKER

Adjusted points to 1000
Could you describe your problem more clearly?
Sorry for not responding, but somwhow I didn't receive any notifications - is this what you want to do?

   BOOL AddAccessRights(HKEY hObject, PSID pSID,      DWORD dwAcessMask)
   {

      //  SD variables.

      UCHAR          ucSDbuf[SD_SIZE];
      PSECURITY_DESCRIPTOR pSD=(PSECURITY_DESCRIPTOR)ucSDbuf;
      DWORD          dwSDLengthNeeded      =      SD_SIZE;

      // ACL variables.

      PACL           pACL;
      BOOL           bDaclPresent;
      BOOL           bDaclDefaulted;
      ACL_SIZE_INFORMATION AclInfo;

      // New ACL variables.

      PACL           pNewACL;
      DWORD          dwNewACLSize;

      // New SD variables.

      UCHAR                NewSD[SECURITY_DESCRIPTOR_MIN_LENGTH];
      PSECURITY_DESCRIPTOR psdNewSD=(PSECURITY_DESCRIPTOR)NewSD;

      // Temporary ACE.

      PVOID          pTempAce;
      UINT           CurrentAceIndex;

      // STEP 2: Get SID (parameter).

      // STEP 3: Get security descriptor (SD) for key.

      if(ERROR_SUCCESS!=RegGetKeySecurity(hObject,
                    (SECURITY_INFORMATION)(DACL_SECURITY_INFORMATION),
                    pSD,
                    &dwSDLengthNeeded))
      {
         printf("Error %d:RegGetKeySecurity\n",GetLastError());
         return(FALSE);
      }

      // STEP 4: Initialize new SD.

      if(!InitializeSecurityDescriptor
         (psdNewSD,SECURITY_DESCRIPTOR_REVISION))
      {
         printf("Error %d:InitializeSecurityDescriptor\n",GetLastError());
         return(FALSE);
      }

      // STEP 5: Get DACL from SD.

      if (!GetSecurityDescriptorDacl(pSD,
                       &bDaclPresent,
                       &pACL,
                       &bDaclDefaulted))
      {
         printf("Error %d:GetSecurityDescriptorDacl\n",GetLastError());
         return(FALSE);
      }

      // STEP 6: Get key ACL size information.

      if(!GetAclInformation(pACL,&AclInfo,sizeof(ACL_SIZE_INFORMATION),
         AclSizeInformation))
      {
         printf("Error %d:GetAclInformation\n",GetLastError());
         return(FALSE);
      }

      // STEP 7: Compute size needed for the new ACL.

      dwNewACLSize = AclInfo.AclBytesInUse +
                     sizeof(ACCESS_ALLOWED_ACE) +
                     GetLengthSid(pSID) - sizeof(DWORD);

      // STEP 8: Allocate memory for new ACL.

      pNewACL = (PACL)LocalAlloc(LPTR, dwNewACLSize);

      // STEP 9: Initialize the new ACL.

      if(!InitializeAcl(pNewACL, dwNewACLSize, ACL_REVISION2))
      {
         printf("Error %d:InitializeAcl\n",GetLastError());
         LocalFree((HLOCAL) pNewACL);
         return(FALSE);
      }

      // STEP 10: If DACL is present, copy it to a new DACL.

      if(bDaclPresent)  // Only copy if DACL was present.
      {
         // STEP 11: Copy the file's ACEs to our new ACL.

         if(AclInfo.AceCount)
         {

                  for(CurrentAceIndex = 0; CurrentAceIndex < AclInfo.AceCount;
               CurrentAceIndex++)
            {
               // STEP 12: Get an ACE.

               if(!GetAce(pACL,CurrentAceIndex,&pTempAce))
               {
                 printf("Error %d: GetAce\n",GetLastError());
                 LocalFree((HLOCAL) pNewACL);
                 return(FALSE);
               }

                // STEP 13: Add the ACE to the new ACL.

               if(!AddAce(pNewACL, ACL_REVISION, MAXDWORD, pTempAce,
                  ((PACE_HEADER)pTempAce)->AceSize))
               {
                  printf("Error %d:AddAce\n",GetLastError());
                  LocalFree((HLOCAL) pNewACL);
                  return(FALSE);
               }

             }
         }
      }

      // STEP 14: Add the access-allowed ACE to the new DACL.

      if(!AddAccessAllowedAce(pNewACL,ACL_REVISION,dwAcessMask, pSID))
      {
         printf("Error %d:AddAccessAllowedAce",GetLastError());
         LocalFree((HLOCAL) pNewACL);
         return(FALSE);
      }

      // STEP 15: Set our new DACL to the file SD.

      if (!SetSecurityDescriptorDacl(psdNewSD,
                        TRUE,
                        pNewACL,
                        FALSE))
      {
         printf("Error %d:SetSecurityDescriptorDacl",GetLastError());
         LocalFree((HLOCAL) pNewACL);
         return(FALSE);
      }

      // STEP 16: Set the SD to the File.

      if (ERROR_SUCCESS!=RegSetKeySecurity(hObject, DACL_SECURITY_INFORMATION,psdNewSD))
      {
         printf("Error %d:RegSetKeySecurity\n",GetLastError());
         LocalFree((HLOCAL) pNewACL);
         return(FALSE);
      }

      // STEP 17: Free the memory allocated for the new ACL.

      LocalFree((HLOCAL) pNewACL);
      return(TRUE);
   }
HOWTO: Specify Access Control on Window NT Container Objects
http://support.microsoft.com/support/kb/articles/Q188/7/60.ASP

HOWTO: Add an Access-Allowed ACE to a File
http://support.microsoft.com/support/kb/articles/Q102/1/02.ASP
Avatar of tgemini

ASKER

I already had that working, but if you run that piece of code trying to change the permissions on a directory it will only change the first set of permissions on it, and the second set will remain unspecified.

I finally found out how to do it myself. It requires two ACEs being put in the ACL for the directory. The two ACEs are distinguised from each other by the inheritance flags in the ACE, with one having the inheritance flag INHERIT_ONLY_ACE | OBJECT_INHERIT_ACE, and the other having the inheritance flag CONTAINER_INHERIT_ACE. The AddAccessAllowedAce function doesnt let you specify the inheritance of the ACE that you are adding to the ACL, so you have to add it, then find where it is in the ACL using GetAce, then set the inheritance flags manually. This appears to be a really big downfall in the whole security scheme, and from reading the MSDN (which I personally believe is the biggest heap of garbage I have ever seen) I finally found that the ability to set inheritance flags was implimented in a function called AddAccessAllowedAceEx, which you have to have NT5 for (I have NT4). If you are interested to see the code I used let me know.

Also, since I actually answered this myself, is there any way that this question can become PAQ so that it is still searchable without accepting any answers?
ASKER CERTIFIED SOLUTION
Avatar of NickRepin
NickRepin

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 tgemini

ASKER

Would you believe that I actually read that article ages ago, but couldnt make sense of it? You are right, the information is in there, just very cryptically hidden. I guess the points are yours then, have fun.
Not that I want to object, just out of curiosity: Did you try the above code?