?
Solved

Directory Permissions

Posted on 2000-02-10
16
Medium Priority
?
265 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:tgemini
  • 7
  • 4
  • 3
  • +1
16 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 2509997
Which OS? Is it UN*X?
0
 

Author Comment

by:tgemini
ID: 2510095
Oh sorry, missed probably the most vital part :)
Win NT
0
 

Author Comment

by:tgemini
ID: 2510105
Oh yeah, and at this stage I am using the AddAccessAllowedAce(), coding on Visual Studio 6
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 22

Expert Comment

by:nietod
ID: 2510257
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 2511213
Todd - attributes have nothing to do with permissions on NT.

tgemini - could you elaborate on the difficulties you're encountering?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2511244
You're right. I misundestood.  Good thing I didn't answer.  :-)
0
 

Author Comment

by:tgemini
ID: 2520450
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" :)
0
 

Author Comment

by:tgemini
ID: 2524938
Adjusted points to 500
0
 

Author Comment

by:tgemini
ID: 2525476
Adjusted points to 1000
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2529906
Could you describe your problem more clearly?
0
 
LVL 86

Expert Comment

by:jkr
ID: 2531080
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);
   }
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2532982
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
0
 

Author Comment

by:tgemini
ID: 2540570
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?
0
 
LVL 15

Accepted Solution

by:
NickRepin earned 2000 total points
ID: 2540651
Probably, MSDN is a heap of garbage, nevertheless it describes the solution of your problem in the article <HOWTO: Specify Access Control on Window NT Container Objects>
(my comment from Feb18).
0
 

Author Comment

by:tgemini
ID: 2540677
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 2540766
Not that I want to object, just out of curiosity: Did you try the above code?
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question