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
tgeminiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Which OS? Is it UN*X?
0
tgeminiAuthor Commented:
Oh sorry, missed probably the most vital part :)
Win NT
0
tgeminiAuthor Commented:
Oh yeah, and at this stage I am using the AddAccessAllowedAce(), coding on Visual Studio 6
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

nietodCommented:
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
jkrCommented:
Todd - attributes have nothing to do with permissions on NT.

tgemini - could you elaborate on the difficulties you're encountering?
0
nietodCommented:
You're right. I misundestood.  Good thing I didn't answer.  :-)
0
tgeminiAuthor Commented:
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
tgeminiAuthor Commented:
Adjusted points to 500
0
tgeminiAuthor Commented:
Adjusted points to 1000
0
NickRepinCommented:
Could you describe your problem more clearly?
0
jkrCommented:
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
NickRepinCommented:
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
tgeminiAuthor Commented:
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
NickRepinCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tgeminiAuthor Commented:
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
jkrCommented:
Not that I want to object, just out of curiosity: Did you try the above code?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.