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
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
Which OS? Is it UN*X?
ASKER
Oh sorry, missed probably the most vital part :)
Win NT
Win NT
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.
SetFileAttribute("C:\\FILE
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?
tgemini - could you elaborate on the difficulties you're encountering?
You're right. I misundestood. Good thing I didn't answer. :-)
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" :)
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" :)
ASKER
Adjusted points to 500
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_DESCRI PTOR)NewSD ;
// Temporary ACE.
PVOID pTempAce;
UINT CurrentAceIndex;
// STEP 2: Get SID (parameter).
// STEP 3: Get security descriptor (SD) for key.
if(ERROR_SUCCESS!=RegGetKe ySecurity( hObject,
(SECURITY_INFORMATION)(DAC L_SECURITY _INFORMATI ON),
pSD,
&dwSDLengthNeeded))
{
printf("Error %d:RegGetKeySecurity\n",Ge tLastError ());
return(FALSE);
}
// STEP 4: Initialize new SD.
if(!InitializeSecurityDesc riptor
(psdNewSD,SECURITY_DESCRIP TOR_REVISI ON))
{
printf("Error %d:InitializeSecurityDescr iptor\n",G etLastErro r());
return(FALSE);
}
// STEP 5: Get DACL from SD.
if (!GetSecurityDescriptorDac l(pSD,
&bDaclPresent,
&pACL,
&bDaclDefaulted))
{
printf("Error %d:GetSecurityDescriptorDa cl\n",GetL astError() );
return(FALSE);
}
// STEP 6: Get key ACL size information.
if(!GetAclInformation(pACL ,&AclInfo, sizeof(ACL _SIZE_INFO RMATION),
AclSizeInformation))
{
printf("Error %d:GetAclInformation\n",Ge tLastError ());
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",GetLas tError());
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,CurrentAce Index,&pTe mpAce))
{
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)->A ceSize))
{
printf("Error %d:AddAce\n",GetLastError( ));
LocalFree((HLOCAL) pNewACL);
return(FALSE);
}
}
}
}
// STEP 14: Add the access-allowed ACE to the new DACL.
if(!AddAccessAllowedAce(pN ewACL,ACL_ REVISION,d wAcessMask , pSID))
{
printf("Error %d:AddAccessAllowedAce",Ge tLastError ());
LocalFree((HLOCAL) pNewACL);
return(FALSE);
}
// STEP 15: Set our new DACL to the file SD.
if (!SetSecurityDescriptorDac l(psdNewSD ,
TRUE,
pNewACL,
FALSE))
{
printf("Error %d:SetSecurityDescriptorDa cl",GetLas tError());
LocalFree((HLOCAL) pNewACL);
return(FALSE);
}
// STEP 16: Set the SD to the File.
if (ERROR_SUCCESS!=RegSetKeyS ecurity(hO bject, DACL_SECURITY_INFORMATION, psdNewSD))
{
printf("Error %d:RegSetKeySecurity\n",Ge tLastError ());
LocalFree((HLOCAL) pNewACL);
return(FALSE);
}
// STEP 17: Free the memory allocated for the new ACL.
LocalFree((HLOCAL) pNewACL);
return(TRUE);
}
BOOL AddAccessRights(HKEY hObject, PSID pSID, DWORD dwAcessMask)
{
// SD variables.
UCHAR ucSDbuf[SD_SIZE];
PSECURITY_DESCRIPTOR pSD=(PSECURITY_DESCRIPTOR)
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_
PSECURITY_DESCRIPTOR psdNewSD=(PSECURITY_DESCRI
// Temporary ACE.
PVOID pTempAce;
UINT CurrentAceIndex;
// STEP 2: Get SID (parameter).
// STEP 3: Get security descriptor (SD) for key.
if(ERROR_SUCCESS!=RegGetKe
(SECURITY_INFORMATION)(DAC
pSD,
&dwSDLengthNeeded))
{
printf("Error %d:RegGetKeySecurity\n",Ge
return(FALSE);
}
// STEP 4: Initialize new SD.
if(!InitializeSecurityDesc
(psdNewSD,SECURITY_DESCRIP
{
printf("Error %d:InitializeSecurityDescr
return(FALSE);
}
// STEP 5: Get DACL from SD.
if (!GetSecurityDescriptorDac
&bDaclPresent,
&pACL,
&bDaclDefaulted))
{
printf("Error %d:GetSecurityDescriptorDa
return(FALSE);
}
// STEP 6: Get key ACL size information.
if(!GetAclInformation(pACL
AclSizeInformation))
{
printf("Error %d:GetAclInformation\n",Ge
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,
{
printf("Error %d:InitializeAcl\n",GetLas
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,CurrentAce
{
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)->A
{
printf("Error %d:AddAce\n",GetLastError(
LocalFree((HLOCAL) pNewACL);
return(FALSE);
}
}
}
}
// STEP 14: Add the access-allowed ACE to the new DACL.
if(!AddAccessAllowedAce(pN
{
printf("Error %d:AddAccessAllowedAce",Ge
LocalFree((HLOCAL) pNewACL);
return(FALSE);
}
// STEP 15: Set our new DACL to the file SD.
if (!SetSecurityDescriptorDac
TRUE,
pNewACL,
FALSE))
{
printf("Error %d:SetSecurityDescriptorDa
LocalFree((HLOCAL) pNewACL);
return(FALSE);
}
// STEP 16: Set the SD to the File.
if (ERROR_SUCCESS!=RegSetKeyS
{
printf("Error %d:RegSetKeySecurity\n",Ge
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
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
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?