Accueil > > > CHANGER LES DROITS NTFS D'UN DOSSIER ET/OU FICHIER...
CHANGER LES DROITS NTFS D'UN DOSSIER ET/OU FICHIER...
Information sur la source
Description
Voilà, c'est un code que j'ai piqué sur un site du net, les commentaires sont en anglais, mais je crois que ça peut servir à pas mal de personne, car j'ai trouvé pas mal de posts à ce sujet. Enfin voilà. Donnez m'en des nouvelles. Ciao
Source
- 'Example from MSDN (Q240176)
- 'The following code changes permissions on a folder to Add & Read or Change.
- 'The folder needs to be created on an NTFS partition.
- 'You need to be an Administrator on the machine in question and have read/write
- '(READ_CONTROL and WRITE_DAC) access to the file or directory.
-
- '1. Create a Standard EXE project in Visual Basic. Form1 is created by default.
- '2. Add two Textboxes (Text1 and Text2) and two CommandButtons (Command1 and Command2) to Form1.
- '3. Add the following code to the form and the module
- '4. Run the application.
- '5. In the Test1 TextBox, enter the name of the folder you want to change permissions on. (D:\test is entered by default.)
- ' In the Test2 Textbox, enter the name of the user you want to give these permissions to.
- '6. Click the Add & Read permissions button to give Add & Read permissions to the folder, or click the Change Permissions
- ' button to give Change permissions to the folder.
- '7. To check the permissions on the folder, right-click Explorer. Select the Properties menu item, and click the Security
- ' Tab of the Properties dialog box. On the Security tab, click the Permissions button. The specific account should say
- ' Add & Read or Change depending on which button you clicked in the preceding sample.
-
- 'Add this code to the form
- Private Sub Command1_Click()
- Dim sUserName As String
- Dim sFolderName As String
- sUserName = Trim$(CStr(Text2.Text))
- sFolderName = Trim$(CStr(Text1.Text))
- SetAccess sUserName, sFolderName, GENERIC_READ Or GENERIC_EXECUTE Or DELETE Or GENERIC_WRITE
- End Sub
- Private Sub Command2_Click()
- Dim sUserName As String
- Dim sFolderName As String
- sUserName = Trim$(Text2.Text)
- sFolderName = Trim$(Text1.Text)
- SetAccess sUserName, sFolderName, GENERIC_EXECUTE Or GENERIC_READ
- End Sub
- Private Sub Form_Load()
- Text1.Text = "enter folder name"
- Text2.Text = "enter username"
- Command1.Caption = "Change"
- Command2.Caption = "Read && Add"
- End Sub
-
- 'Add this code to a module
-
- ' Constants used within our API calls. Refer to the MSDN for more
- ' information on how/what these constants are used for.
-
- ' Memory constants used through various memory API calls.
- Public Const GMEM_MOVEABLE = &H2
- Public Const LMEM_FIXED = &H0
- Public Const LMEM_ZEROINIT = &H40
- Public Const LPTR = (LMEM_FIXED + LMEM_ZEROINIT)
- Public Const GENERIC_READ = &H80000000
- Public Const GENERIC_ALL = &H10000000
- Public Const GENERIC_EXECUTE = &H20000000
- Public Const GENERIC_WRITE = &H40000000
-
- ' The file/security API call constants.
- ' Refer to the MSDN for more information on how/what these constants
- ' are used for.
- Public Const DACL_SECURITY_INFORMATION = &H4
- Public Const SECURITY_DESCRIPTOR_REVISION = 1
- Public Const SECURITY_DESCRIPTOR_MIN_LENGTH = 20
- Public Const SD_SIZE = (65536 + SECURITY_DESCRIPTOR_MIN_LENGTH)
- Public Const ACL_REVISION2 = 2
- Public Const ACL_REVISION = 2
- Public Const MAXDWORD = &HFFFFFFFF
- Public Const SidTypeUser = 1
- Public Const AclSizeInformation = 2
-
- ' The following are the inherit flags that go into the AceFlags field
- ' of an Ace header.
-
- Public Const OBJECT_INHERIT_ACE = &H1
- Public Const CONTAINER_INHERIT_ACE = &H2
- Public Const NO_PROPAGATE_INHERIT_ACE = &H4
- Public Const INHERIT_ONLY_ACE = &H8
- Public Const INHERITED_ACE = &H10
- Public Const VALID_INHERIT_FLAGS = &H1F
- Public Const DELETE = &H10000
-
- ' Structures used by our API calls.
- ' Refer to the MSDN for more information on how/what these
- ' structures are used for.
- Type ACE_HEADER
- AceType As Byte
- AceFlags As Byte
- AceSize As Integer
- End Type
-
-
- Public Type ACCESS_DENIED_ACE
- Header As ACE_HEADER
- Mask As Long
- SidStart As Long
- End Type
-
- Type ACCESS_ALLOWED_ACE
- Header As ACE_HEADER
- Mask As Long
- SidStart As Long
- End Type
-
- Type ACL
- AclRevision As Byte
- Sbz1 As Byte
- AclSize As Integer
- AceCount As Integer
- Sbz2 As Integer
- End Type
-
- Type ACL_SIZE_INFORMATION
- AceCount As Long
- AclBytesInUse As Long
- AclBytesFree As Long
- End Type
-
- Type SECURITY_DESCRIPTOR
- Revision As Byte
- Sbz1 As Byte
- Control As Long
- Owner As Long
- Group As Long
- sACL As ACL
- Dacl As ACL
- End Type
-
- ' API calls used within this sample. Refer to the MSDN for more
- ' information on how/what these APIs do.
-
- Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
- Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
- Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (lpSystemName As String, ByVal lpAccountName As String, sid As Any, cbSid As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Long) As Long
- Declare Function InitializeSecurityDescriptor Lib "advapi32.dll" (pSecurityDescriptor As SECURITY_DESCRIPTOR, ByVal dwRevision As Long) As Long
- Declare Function GetSecurityDescriptorDacl Lib "advapi32.dll" (pSecurityDescriptor As Byte, lpbDaclPresent As Long, pDacl As Long, lpbDaclDefaulted As Long) As Long
- Declare Function GetFileSecurityN Lib "advapi32.dll" Alias "GetFileSecurityA" (ByVal lpFileName As String, ByVal RequestedInformation As Long, ByVal pSecurityDescriptor As Long, ByVal nLength As Long, lpnLengthNeeded As Long) As Long
- Declare Function GetFileSecurity Lib "advapi32.dll" Alias "GetFileSecurityA" (ByVal lpFileName As String, ByVal RequestedInformation As Long, pSecurityDescriptor As Byte, ByVal nLength As Long, lpnLengthNeeded As Long) As Long
- Declare Function GetAclInformation Lib "advapi32.dll" (ByVal pAcl As Long, pAclInformation As Any, ByVal nAclInformationLength As Long, ByVal dwAclInformationClass As Long) As Long
- Public Declare Function EqualSid Lib "advapi32.dll" (pSid1 As Byte, ByVal pSid2 As Long) As Long
- Declare Function GetLengthSid Lib "advapi32.dll" (pSid As Any) As Long
- Declare Function InitializeAcl Lib "advapi32.dll" (pAcl As Byte, ByVal nAclLength As Long, ByVal dwAclRevision As Long) As Long
- Declare Function GetAce Lib "advapi32.dll" (ByVal pAcl As Long, ByVal dwAceIndex As Long, pace As Any) As Long
- Declare Function AddAce Lib "advapi32.dll" (ByVal pAcl As Long, ByVal dwAceRevision As Long, ByVal dwStartingAceIndex As Long, ByVal pAceList As Long, ByVal nAceListLength As Long) As Long
- Declare Function AddAccessAllowedAce Lib "advapi32.dll" (pAcl As Byte, ByVal dwAceRevision As Long, ByVal AccessMask As Long, pSid As Byte) As Long
- Public Declare Function AddAccessDeniedAce Lib "advapi32.dll" (pAcl As Byte, ByVal dwAceRevision As Long, ByVal AccessMask As Long, pSid As Byte) As Long
- Declare Function SetSecurityDescriptorDacl Lib "advapi32.dll" (pSecurityDescriptor As SECURITY_DESCRIPTOR, ByVal bDaclPresent As Long, pDacl As Byte, ByVal bDaclDefaulted As Long) As Long
- Declare Function SetFileSecurity Lib "advapi32.dll" Alias "SetFileSecurityA" (ByVal lpFileName As String, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR) As Long
- Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
- Public Sub SetAccess(sUserName As String, sFileName As String, lMask As Long)
- Dim lResult As Long ' Result of various API calls.
- Dim I As Integer ' Used in looping.
- Dim bUserSid(255) As Byte ' This will contain your SID.
- Dim bTempSid(255) As Byte ' This will contain the Sid of each ACE in the ACL .
- Dim sSystemName As String ' Name of this computer system.
-
- Dim lSystemNameLength As Long ' Length of string that contains
- ' the name of this system.
-
- Dim lLengthUserName As Long ' Max length of user name.
-
- 'Dim sUserName As String * 255 ' String to hold the current user
- ' name.
-
-
- Dim lUserSID As Long ' Used to hold the SID of the
- ' current user.
-
- Dim lTempSid As Long ' Used to hold the SID of each ACE in the ACL
- Dim lUserSIDSize As Long ' Size of the SID.
- Dim sDomainName As String * 255 ' Domain the user belongs to.
- Dim lDomainNameLength As Long ' Length of domain name needed.
-
- Dim lSIDType As Long ' The type of SID info we are
- ' getting back.
-
- Dim sFileSD As SECURITY_DESCRIPTOR ' SD of the file we want.
-
- Dim bSDBuf() As Byte ' Buffer that holds the security
- ' descriptor for this file.
-
- Dim lFileSDSize As Long ' Size of the File SD.
- Dim lSizeNeeded As Long ' Size needed for SD for file.
-
-
- Dim sNewSD As SECURITY_DESCRIPTOR ' New security descriptor.
-
- Dim sACL As ACL ' Used in grabbing the DACL from
- ' the File SD.
-
- Dim lDaclPresent As Long ' Used in grabbing the DACL from
- ' the File SD.
-
- Dim lDaclDefaulted As Long ' Used in grabbing the DACL from
- ' the File SD.
-
- Dim sACLInfo As ACL_SIZE_INFORMATION ' Used in grabbing the ACL
- ' from the File SD.
-
- Dim lACLSize As Long ' Size of the ACL structure used
- ' to get the ACL from the File SD.
-
- Dim pAcl As Long ' Current ACL for this file.
- Dim lNewACLSize As Long ' Size of new ACL to create.
- Dim bNewACL() As Byte ' Buffer to hold new ACL.
-
- Dim sCurrentACE As ACCESS_ALLOWED_ACE ' Current ACE.
- Dim pCurrentAce As Long ' Our current ACE.
-
- Dim nRecordNumber As Long
-
- ' Get the SID of the user. (Refer to the MSDN for more information on SIDs
- ' and their function/purpose in the operating system.) Get the SID of this
- ' user by using the LookupAccountName API. In order to use the SID
- ' of the current user account, call the LookupAccountName API
- ' twice. The first time is to get the required sizes of the SID
- ' and the DomainName string. The second call is to actually get
- ' the desired information.
-
- lResult = LookupAccountName(vbNullString, sUserName, _
- bUserSid(0), 255, sDomainName, lDomainNameLength, _
- lSIDType)
-
- ' Now set the sDomainName string buffer to its proper size before
- ' calling the API again.
- sDomainName = Space(lDomainNameLength)
-
- ' Call the LookupAccountName again to get the actual SID for user.
- lResult = LookupAccountName(vbNullString, sUserName, _
- bUserSid(0), 255, sDomainName, lDomainNameLength, _
- lSIDType)
-
- ' Return value of zero means the call to LookupAccountName failed;
- ' test for this before you continue.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Lookup the Current User Account: " _
- & sUserName
- Exit Sub
- End If
-
- ' You now have the SID for the user who is logged on.
- ' The SID is of interest since it will get the security descriptor
- ' for the file that the user is interested in.
- ' The GetFileSecurity API will retrieve the Security Descriptor
- ' for the file. However, you must call this API twice: once to get
- ' the proper size for the Security Descriptor and once to get the
- ' actual Security Descriptor information.
-
- lResult = GetFileSecurityN(sFileName, DACL_SECURITY_INFORMATION, _
- 0, 0, lSizeNeeded)
-
- ' Redimension the Security Descriptor buffer to the proper size.
- ReDim bSDBuf(lSizeNeeded)
-
- ' Now get the actual Security Descriptor for the file.
- lResult = GetFileSecurity(sFileName, DACL_SECURITY_INFORMATION, _
- bSDBuf(0), lSizeNeeded, lSizeNeeded)
-
- ' A return code of zero means the call failed; test for this
- ' before continuing.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Get the File Security Descriptor"
- Exit Sub
- End If
-
- ' Call InitializeSecurityDescriptor to build a new SD for the
- ' file.
- lResult = InitializeSecurityDescriptor(sNewSD, _
- SECURITY_DESCRIPTOR_REVISION)
-
- ' A return code of zero means the call failed; test for this
- ' before continuing.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Initialize New Security Descriptor"
- Exit Sub
- End If
-
- ' You now have the file's SD and a new Security Descriptor
- ' that will replace the current one. Next, pull the DACL from
- ' the SD. To do so, call the GetSecurityDescriptorDacl API
- ' function.
-
- lResult = GetSecurityDescriptorDacl(bSDBuf(0), lDaclPresent, _
- pAcl, lDaclDefaulted)
-
- ' A return code of zero means the call failed; test for this
- ' before continuing.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Get DACL from File Security " _
- & "Descriptor"
- Exit Sub
- End If
-
- ' You have the file's SD, and want to now pull the ACL from the
- ' SD. To do so, call the GetACLInformation API function.
- ' See if ACL exists for this file before getting the ACL
- ' information.
- If (lDaclPresent = False) Then
- MsgBox "Error: No ACL Information Available for this File"
- Exit Sub
- End If
-
- ' Attempt to get the ACL from the file's Security Descriptor.
- lResult = GetAclInformation(pAcl, sACLInfo, Len(sACLInfo), 2&)
-
- ' A return code of zero means the call failed; test for this
- ' before continuing.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Get ACL from File Security Descriptor"
- Exit Sub
- End If
-
- ' Now that you have the ACL information, compute the new ACL size
- ' requirements.
- lNewACLSize = sACLInfo.AclBytesInUse + (Len(sCurrentACE) + _
- GetLengthSid(bUserSid(0))) * 2 - 4
-
- ' Resize our new ACL buffer to its proper size.
- ReDim bNewACL(lNewACLSize)
-
- ' Use the InitializeAcl API function call to initialize the new
- ' ACL.
- lResult = InitializeAcl(bNewACL(0), lNewACLSize, ACL_REVISION)
-
- ' A return code of zero means the call failed; test for this
- ' before continuing.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Initialize New ACL"
- Exit Sub
- End If
-
- ' If a DACL is present, copy it to a new DACL.
- If (lDaclPresent) Then
-
- ' Copy the ACEs from the file to the new ACL.
- If (sACLInfo.AceCount > 0) Then
-
- ' Grab each ACE and stuff them into the new ACL.
- nRecordNumber = 0
- For I = 0 To (sACLInfo.AceCount - 1)
-
- ' Attempt to grab the next ACE.
- lResult = GetAce(pAcl, I, pCurrentAce)
-
- ' Make sure you have the current ACE under question.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Obtain ACE (" & I & ")"
- Exit Sub
- End If
-
- ' You have a pointer to the ACE. Place it
- ' into a structure, so you can get at its size.
- CopyMemory sCurrentACE, pCurrentAce, LenB(sCurrentACE)
-
- 'Skip adding the ACE to the ACL if this is same usersid
- lTempSid = pCurrentAce + 8
- If EqualSid(bUserSid(0), lTempSid) = 0 Then
-
- ' Now that you have the ACE, add it to the new ACL.
- lResult = AddAce(VarPtr(bNewACL(0)), ACL_REVISION, _
- MAXDWORD, pCurrentAce, _
- sCurrentACE.Header.AceSize)
-
- ' Make sure you have the current ACE under question.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Add ACE to New ACL"
- Exit Sub
- End If
- nRecordNumber = nRecordNumber + 1
- End If
-
- Next I
-
- ' You have now rebuilt a new ACL and want to add it to
- ' the newly created DACL.
- lResult = AddAccessAllowedAce(bNewACL(0), ACL_REVISION, _
- lMask, bUserSid(0))
-
- ' Make sure added the ACL to the DACL.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Add ACL to DACL"
- Exit Sub
- End If
-
- 'If it's directory, we need to add inheritance staff.
- If GetAttr(sFileName) And vbDirectory Then
-
- ' Attempt to grab the next ACE which is what we just added.
- lResult = GetAce(VarPtr(bNewACL(0)), nRecordNumber, pCurrentAce)
-
- ' Make sure you have the current ACE under question.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Obtain ACE (" & I & ")"
- Exit Sub
- End If
- ' You have a pointer to the ACE. Place it
- ' into a structure, so you can get at its size.
- CopyMemory sCurrentACE, pCurrentAce, LenB(sCurrentACE)
- sCurrentACE.Header.AceFlags = OBJECT_INHERIT_ACE + INHERIT_ONLY_ACE
- CopyMemory ByVal pCurrentAce, VarPtr(sCurrentACE), LenB(sCurrentACE)
-
- 'add another ACE for files
- lResult = AddAccessAllowedAce(bNewACL(0), ACL_REVISION, _
- lMask, bUserSid(0))
-
- ' Make sure added the ACL to the DACL.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Add ACL to DACL"
- Exit Sub
- End If
-
- ' Attempt to grab the next ACE.
- lResult = GetAce(VarPtr(bNewACL(0)), nRecordNumber + 1, pCurrentAce)
-
- ' Make sure you have the current ACE under question.
- If (lResult = 0) Then
- MsgBox "Error: Unable to Obtain ACE (" & I & ")"
- Exit Sub
- End If
-
- CopyMemory sCurrentACE, pCurrentAce, LenB(sCurrentACE)
- sCurrentACE.Header.AceFlags = CONTAINER_INHERIT_ACE
- CopyMemory ByVal pCurrentAce, VarPtr(sCurrentACE), LenB(sCurrentACE)
- End If
-
-
- ' Set the file's Security Descriptor to the new DACL.
- lResult = SetSecurityDescriptorDacl(sNewSD, 1, _
- bNewACL(0), 0)
-
- ' Make sure you set the SD to the new DACL.
- If (lResult = 0) Then
- MsgBox "Error: " & _
- "Unable to Set New DACL to Security Descriptor"
- Exit Sub
- End If
-
- ' The final step is to add the Security Descriptor back to
- ' the file!
- lResult = SetFileSecurity(sFileName, _
- DACL_SECURITY_INFORMATION, sNewSD)
-
- ' Make sure you added the Security Descriptor to the file!
- If (lResult = 0) Then
- MsgBox "Error: Unable to Set New Security Descriptor " _
- & " to File : " & sFileName
- MsgBox Err.LastDllError
- Else
- MsgBox "Updated Security Descriptor on File: " _
- & sFileName
- End If
-
- End If
-
- End If
-
- End Sub
-
-
-
'Example from MSDN (Q240176)
'The following code changes permissions on a folder to Add & Read or Change.
'The folder needs to be created on an NTFS partition.
'You need to be an Administrator on the machine in question and have read/write
'(READ_CONTROL and WRITE_DAC) access to the file or directory.
'1. Create a Standard EXE project in Visual Basic. Form1 is created by default.
'2. Add two Textboxes (Text1 and Text2) and two CommandButtons (Command1 and Command2) to Form1.
'3. Add the following code to the form and the module
'4. Run the application.
'5. In the Test1 TextBox, enter the name of the folder you want to change permissions on. (D:\test is entered by default.)
' In the Test2 Textbox, enter the name of the user you want to give these permissions to.
'6. Click the Add & Read permissions button to give Add & Read permissions to the folder, or click the Change Permissions
' button to give Change permissions to the folder.
'7. To check the permissions on the folder, right-click Explorer. Select the Properties menu item, and click the Security
' Tab of the Properties dialog box. On the Security tab, click the Permissions button. The specific account should say
' Add & Read or Change depending on which button you clicked in the preceding sample.
'Add this code to the form
Private Sub Command1_Click()
Dim sUserName As String
Dim sFolderName As String
sUserName = Trim$(CStr(Text2.Text))
sFolderName = Trim$(CStr(Text1.Text))
SetAccess sUserName, sFolderName, GENERIC_READ Or GENERIC_EXECUTE Or DELETE Or GENERIC_WRITE
End Sub
Private Sub Command2_Click()
Dim sUserName As String
Dim sFolderName As String
sUserName = Trim$(Text2.Text)
sFolderName = Trim$(Text1.Text)
SetAccess sUserName, sFolderName, GENERIC_EXECUTE Or GENERIC_READ
End Sub
Private Sub Form_Load()
Text1.Text = "enter folder name"
Text2.Text = "enter username"
Command1.Caption = "Change"
Command2.Caption = "Read && Add"
End Sub
'Add this code to a module
' Constants used within our API calls. Refer to the MSDN for more
' information on how/what these constants are used for.
' Memory constants used through various memory API calls.
Public Const GMEM_MOVEABLE = &H2
Public Const LMEM_FIXED = &H0
Public Const LMEM_ZEROINIT = &H40
Public Const LPTR = (LMEM_FIXED + LMEM_ZEROINIT)
Public Const GENERIC_READ = &H80000000
Public Const GENERIC_ALL = &H10000000
Public Const GENERIC_EXECUTE = &H20000000
Public Const GENERIC_WRITE = &H40000000
' The file/security API call constants.
' Refer to the MSDN for more information on how/what these constants
' are used for.
Public Const DACL_SECURITY_INFORMATION = &H4
Public Const SECURITY_DESCRIPTOR_REVISION = 1
Public Const SECURITY_DESCRIPTOR_MIN_LENGTH = 20
Public Const SD_SIZE = (65536 + SECURITY_DESCRIPTOR_MIN_LENGTH)
Public Const ACL_REVISION2 = 2
Public Const ACL_REVISION = 2
Public Const MAXDWORD = &HFFFFFFFF
Public Const SidTypeUser = 1
Public Const AclSizeInformation = 2
' The following are the inherit flags that go into the AceFlags field
' of an Ace header.
Public Const OBJECT_INHERIT_ACE = &H1
Public Const CONTAINER_INHERIT_ACE = &H2
Public Const NO_PROPAGATE_INHERIT_ACE = &H4
Public Const INHERIT_ONLY_ACE = &H8
Public Const INHERITED_ACE = &H10
Public Const VALID_INHERIT_FLAGS = &H1F
Public Const DELETE = &H10000
' Structures used by our API calls.
' Refer to the MSDN for more information on how/what these
' structures are used for.
Type ACE_HEADER
AceType As Byte
AceFlags As Byte
AceSize As Integer
End Type
Public Type ACCESS_DENIED_ACE
Header As ACE_HEADER
Mask As Long
SidStart As Long
End Type
Type ACCESS_ALLOWED_ACE
Header As ACE_HEADER
Mask As Long
SidStart As Long
End Type
Type ACL
AclRevision As Byte
Sbz1 As Byte
AclSize As Integer
AceCount As Integer
Sbz2 As Integer
End Type
Type ACL_SIZE_INFORMATION
AceCount As Long
AclBytesInUse As Long
AclBytesFree As Long
End Type
Type SECURITY_DESCRIPTOR
Revision As Byte
Sbz1 As Byte
Control As Long
Owner As Long
Group As Long
sACL As ACL
Dacl As ACL
End Type
' API calls used within this sample. Refer to the MSDN for more
' information on how/what these APIs do.
Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (lpSystemName As String, ByVal lpAccountName As String, sid As Any, cbSid As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Long) As Long
Declare Function InitializeSecurityDescriptor Lib "advapi32.dll" (pSecurityDescriptor As SECURITY_DESCRIPTOR, ByVal dwRevision As Long) As Long
Declare Function GetSecurityDescriptorDacl Lib "advapi32.dll" (pSecurityDescriptor As Byte, lpbDaclPresent As Long, pDacl As Long, lpbDaclDefaulted As Long) As Long
Declare Function GetFileSecurityN Lib "advapi32.dll" Alias "GetFileSecurityA" (ByVal lpFileName As String, ByVal RequestedInformation As Long, ByVal pSecurityDescriptor As Long, ByVal nLength As Long, lpnLengthNeeded As Long) As Long
Declare Function GetFileSecurity Lib "advapi32.dll" Alias "GetFileSecurityA" (ByVal lpFileName As String, ByVal RequestedInformation As Long, pSecurityDescriptor As Byte, ByVal nLength As Long, lpnLengthNeeded As Long) As Long
Declare Function GetAclInformation Lib "advapi32.dll" (ByVal pAcl As Long, pAclInformation As Any, ByVal nAclInformationLength As Long, ByVal dwAclInformationClass As Long) As Long
Public Declare Function EqualSid Lib "advapi32.dll" (pSid1 As Byte, ByVal pSid2 As Long) As Long
Declare Function GetLengthSid Lib "advapi32.dll" (pSid As Any) As Long
Declare Function InitializeAcl Lib "advapi32.dll" (pAcl As Byte, ByVal nAclLength As Long, ByVal dwAclRevision As Long) As Long
Declare Function GetAce Lib "advapi32.dll" (ByVal pAcl As Long, ByVal dwAceIndex As Long, pace As Any) As Long
Declare Function AddAce Lib "advapi32.dll" (ByVal pAcl As Long, ByVal dwAceRevision As Long, ByVal dwStartingAceIndex As Long, ByVal pAceList As Long, ByVal nAceListLength As Long) As Long
Declare Function AddAccessAllowedAce Lib "advapi32.dll" (pAcl As Byte, ByVal dwAceRevision As Long, ByVal AccessMask As Long, pSid As Byte) As Long
Public Declare Function AddAccessDeniedAce Lib "advapi32.dll" (pAcl As Byte, ByVal dwAceRevision As Long, ByVal AccessMask As Long, pSid As Byte) As Long
Declare Function SetSecurityDescriptorDacl Lib "advapi32.dll" (pSecurityDescriptor As SECURITY_DESCRIPTOR, ByVal bDaclPresent As Long, pDacl As Byte, ByVal bDaclDefaulted As Long) As Long
Declare Function SetFileSecurity Lib "advapi32.dll" Alias "SetFileSecurityA" (ByVal lpFileName As String, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Public Sub SetAccess(sUserName As String, sFileName As String, lMask As Long)
Dim lResult As Long ' Result of various API calls.
Dim I As Integer ' Used in looping.
Dim bUserSid(255) As Byte ' This will contain your SID.
Dim bTempSid(255) As Byte ' This will contain the Sid of each ACE in the ACL .
Dim sSystemName As String ' Name of this computer system.
Dim lSystemNameLength As Long ' Length of string that contains
' the name of this system.
Dim lLengthUserName As Long ' Max length of user name.
'Dim sUserName As String * 255 ' String to hold the current user
' name.
Dim lUserSID As Long ' Used to hold the SID of the
' current user.
Dim lTempSid As Long ' Used to hold the SID of each ACE in the ACL
Dim lUserSIDSize As Long ' Size of the SID.
Dim sDomainName As String * 255 ' Domain the user belongs to.
Dim lDomainNameLength As Long ' Length of domain name needed.
Dim lSIDType As Long ' The type of SID info we are
' getting back.
Dim sFileSD As SECURITY_DESCRIPTOR ' SD of the file we want.
Dim bSDBuf() As Byte ' Buffer that holds the security
' descriptor for this file.
Dim lFileSDSize As Long ' Size of the File SD.
Dim lSizeNeeded As Long ' Size needed for SD for file.
Dim sNewSD As SECURITY_DESCRIPTOR ' New security descriptor.
Dim sACL As ACL ' Used in grabbing the DACL from
' the File SD.
Dim lDaclPresent As Long ' Used in grabbing the DACL from
' the File SD.
Dim lDaclDefaulted As Long ' Used in grabbing the DACL from
' the File SD.
Dim sACLInfo As ACL_SIZE_INFORMATION ' Used in grabbing the ACL
' from the File SD.
Dim lACLSize As Long ' Size of the ACL structure used
' to get the ACL from the File SD.
Dim pAcl As Long ' Current ACL for this file.
Dim lNewACLSize As Long ' Size of new ACL to create.
Dim bNewACL() As Byte ' Buffer to hold new ACL.
Dim sCurrentACE As ACCESS_ALLOWED_ACE ' Current ACE.
Dim pCurrentAce As Long ' Our current ACE.
Dim nRecordNumber As Long
' Get the SID of the user. (Refer to the MSDN for more information on SIDs
' and their function/purpose in the operating system.) Get the SID of this
' user by using the LookupAccountName API. In order to use the SID
' of the current user account, call the LookupAccountName API
' twice. The first time is to get the required sizes of the SID
' and the DomainName string. The second call is to actually get
' the desired information.
lResult = LookupAccountName(vbNullString, sUserName, _
bUserSid(0), 255, sDomainName, lDomainNameLength, _
lSIDType)
' Now set the sDomainName string buffer to its proper size before
' calling the API again.
sDomainName = Space(lDomainNameLength)
' Call the LookupAccountName again to get the actual SID for user.
lResult = LookupAccountName(vbNullString, sUserName, _
bUserSid(0), 255, sDomainName, lDomainNameLength, _
lSIDType)
' Return value of zero means the call to LookupAccountName failed;
' test for this before you continue.
If (lResult = 0) Then
MsgBox "Error: Unable to Lookup the Current User Account: " _
& sUserName
Exit Sub
End If
' You now have the SID for the user who is logged on.
' The SID is of interest since it will get the security descriptor
' for the file that the user is interested in.
' The GetFileSecurity API will retrieve the Security Descriptor
' for the file. However, you must call this API twice: once to get
' the proper size for the Security Descriptor and once to get the
' actual Security Descriptor information.
lResult = GetFileSecurityN(sFileName, DACL_SECURITY_INFORMATION, _
0, 0, lSizeNeeded)
' Redimension the Security Descriptor buffer to the proper size.
ReDim bSDBuf(lSizeNeeded)
' Now get the actual Security Descriptor for the file.
lResult = GetFileSecurity(sFileName, DACL_SECURITY_INFORMATION, _
bSDBuf(0), lSizeNeeded, lSizeNeeded)
' A return code of zero means the call failed; test for this
' before continuing.
If (lResult = 0) Then
MsgBox "Error: Unable to Get the File Security Descriptor"
Exit Sub
End If
' Call InitializeSecurityDescriptor to build a new SD for the
' file.
lResult = InitializeSecurityDescriptor(sNewSD, _
SECURITY_DESCRIPTOR_REVISION)
' A return code of zero means the call failed; test for this
' before continuing.
If (lResult = 0) Then
MsgBox "Error: Unable to Initialize New Security Descriptor"
Exit Sub
End If
' You now have the file's SD and a new Security Descriptor
' that will replace the current one. Next, pull the DACL from
' the SD. To do so, call the GetSecurityDescriptorDacl API
' function.
lResult = GetSecurityDescriptorDacl(bSDBuf(0), lDaclPresent, _
pAcl, lDaclDefaulted)
' A return code of zero means the call failed; test for this
' before continuing.
If (lResult = 0) Then
MsgBox "Error: Unable to Get DACL from File Security " _
& "Descriptor"
Exit Sub
End If
' You have the file's SD, and want to now pull the ACL from the
' SD. To do so, call the GetACLInformation API function.
' See if ACL exists for this file before getting the ACL
' information.
If (lDaclPresent = False) Then
MsgBox "Error: No ACL Information Available for this File"
Exit Sub
End If
' Attempt to get the ACL from the file's Security Descriptor.
lResult = GetAclInformation(pAcl, sACLInfo, Len(sACLInfo), 2&)
' A return code of zero means the call failed; test for this
' before continuing.
If (lResult = 0) Then
MsgBox "Error: Unable to Get ACL from File Security Descriptor"
Exit Sub
End If
' Now that you have the ACL information, compute the new ACL size
' requirements.
lNewACLSize = sACLInfo.AclBytesInUse + (Len(sCurrentACE) + _
GetLengthSid(bUserSid(0))) * 2 - 4
' Resize our new ACL buffer to its proper size.
ReDim bNewACL(lNewACLSize)
' Use the InitializeAcl API function call to initialize the new
' ACL.
lResult = InitializeAcl(bNewACL(0), lNewACLSize, ACL_REVISION)
' A return code of zero means the call failed; test for this
' before continuing.
If (lResult = 0) Then
MsgBox "Error: Unable to Initialize New ACL"
Exit Sub
End If
' If a DACL is present, copy it to a new DACL.
If (lDaclPresent) Then
' Copy the ACEs from the file to the new ACL.
If (sACLInfo.AceCount > 0) Then
' Grab each ACE and stuff them into the new ACL.
nRecordNumber = 0
For I = 0 To (sACLInfo.AceCount - 1)
' Attempt to grab the next ACE.
lResult = GetAce(pAcl, I, pCurrentAce)
' Make sure you have the current ACE under question.
If (lResult = 0) Then
MsgBox "Error: Unable to Obtain ACE (" & I & ")"
Exit Sub
End If
' You have a pointer to the ACE. Place it
' into a structure, so you can get at its size.
CopyMemory sCurrentACE, pCurrentAce, LenB(sCurrentACE)
'Skip adding the ACE to the ACL if this is same usersid
lTempSid = pCurrentAce + 8
If EqualSid(bUserSid(0), lTempSid) = 0 Then
' Now that you have the ACE, add it to the new ACL.
lResult = AddAce(VarPtr(bNewACL(0)), ACL_REVISION, _
MAXDWORD, pCurrentAce, _
sCurrentACE.Header.AceSize)
' Make sure you have the current ACE under question.
If (lResult = 0) Then
MsgBox "Error: Unable to Add ACE to New ACL"
Exit Sub
End If
nRecordNumber = nRecordNumber + 1
End If
Next I
' You have now rebuilt a new ACL and want to add it to
' the newly created DACL.
lResult = AddAccessAllowedAce(bNewACL(0), ACL_REVISION, _
lMask, bUserSid(0))
' Make sure added the ACL to the DACL.
If (lResult = 0) Then
MsgBox "Error: Unable to Add ACL to DACL"
Exit Sub
End If
'If it's directory, we need to add inheritance staff.
If GetAttr(sFileName) And vbDirectory Then
' Attempt to grab the next ACE which is what we just added.
lResult = GetAce(VarPtr(bNewACL(0)), nRecordNumber, pCurrentAce)
' Make sure you have the current ACE under question.
If (lResult = 0) Then
MsgBox "Error: Unable to Obtain ACE (" & I & ")"
Exit Sub
End If
' You have a pointer to the ACE. Place it
' into a structure, so you can get at its size.
CopyMemory sCurrentACE, pCurrentAce, LenB(sCurrentACE)
sCurrentACE.Header.AceFlags = OBJECT_INHERIT_ACE + INHERIT_ONLY_ACE
CopyMemory ByVal pCurrentAce, VarPtr(sCurrentACE), LenB(sCurrentACE)
'add another ACE for files
lResult = AddAccessAllowedAce(bNewACL(0), ACL_REVISION, _
lMask, bUserSid(0))
' Make sure added the ACL to the DACL.
If (lResult = 0) Then
MsgBox "Error: Unable to Add ACL to DACL"
Exit Sub
End If
' Attempt to grab the next ACE.
lResult = GetAce(VarPtr(bNewACL(0)), nRecordNumber + 1, pCurrentAce)
' Make sure you have the current ACE under question.
If (lResult = 0) Then
MsgBox "Error: Unable to Obtain ACE (" & I & ")"
Exit Sub
End If
CopyMemory sCurrentACE, pCurrentAce, LenB(sCurrentACE)
sCurrentACE.Header.AceFlags = CONTAINER_INHERIT_ACE
CopyMemory ByVal pCurrentAce, VarPtr(sCurrentACE), LenB(sCurrentACE)
End If
' Set the file's Security Descriptor to the new DACL.
lResult = SetSecurityDescriptorDacl(sNewSD, 1, _
bNewACL(0), 0)
' Make sure you set the SD to the new DACL.
If (lResult = 0) Then
MsgBox "Error: " & _
"Unable to Set New DACL to Security Descriptor"
Exit Sub
End If
' The final step is to add the Security Descriptor back to
' the file!
lResult = SetFileSecurity(sFileName, _
DACL_SECURITY_INFORMATION, sNewSD)
' Make sure you added the Security Descriptor to the file!
If (lResult = 0) Then
MsgBox "Error: Unable to Set New Security Descriptor " _
& " to File : " & sFileName
MsgBox Err.LastDllError
Else
MsgBox "Updated Security Descriptor on File: " _
& sFileName
End If
End If
End If
End Sub
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
NTFS Acces [ par agparchitecture ]
Bonjour à tous,Je suis en train de developpez pour moi un treeview de listage de dossier et je rencontre un problème.En effet, lorsque je liste les do
Droit NTFS [ par youil ]
J'ai bloqué des utilisateurs sur un dossier avec des droits ntfs.Comment faire pour que mon logiciel est accès a ce dossier sans touché au droit ntfs.
Comment changer les droit d'un fichier par FTP avec Wininet ? [ par petros ]
Y a t'il une fonction qui me permet de modifier les droits en utilisant wininet.dll avec ftp, je fait un programe d'installation de script sur un serv
IMPORTER des données de plusieurs DOSSIERS de manière AUTOMATISEE [ par freimensch ]
Bonjour :-), J'ai un projet à faire dans lequel des données (des valeurs numériques, en fait) écrites dans des fichiers en .xml doivent être importée
protection fichier [ par routch33 ]
bonjour a tous j'ai un gros problèmes avec plusieurs fichiers photos que je ne peut plus ouvrir. j'avais crypté ces fichiers avec secret protector sur
Ouverture de fichiers excel selon conditions [ par clem74170 ]
Bonjour à tous, Je suis débutant en VBA, je vais essayer de vous expliquer mon pb le plus simplement possible, si c'est possible. A partir d'une mac
Création et transfert de fichier Excel [ par thomasf007 ]
Bonsoir à tous, J'ai de nouveau un soucis... Voilà, lors d'une première utilisation, mon programme détecte s'il y a deux dossier essentiel au fonctio
Comunication et envoi de fichier entre serveur [ par Nagasashi ]
Bonjour à tous, Je viens d'attaquer ma période de stage, et l'entreprise me demande de réaliser des taches planifiées (jusque là, tous va bien). En re
[déplacé VB.NET -> VBS] Copie de fichier specifique dans un dossier [ par coolboy2008 ]
Bonjour, j'aimerai cree un script vbs j'aimerai chercher des fichier dans un dossier specifique auquelle je l'ai en liste txt ou n'importe, puis les
création programme gestion, classement et récupération d'information de fichier [ par robinou5913 ]
bonjour,voilà, dans le cadre de mes études, j'apprends à utiliser le Visual Basic 6 et j'ai soudain eu une idée. j'ai beaucoup de films et j'aimerais
|
Derniers Blogs
[WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
LIST GENERICS 2LIST GENERICS 2 par JLuc01
Cliquez pour lire la suite par JLuc01
Logiciels
Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|