Results 1 to 35 of 35

Thread: Setting permissions on shared folders?

Hybrid View

  1. #1
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Well I'm still working on it and I'll post the results here if I do get it working, here's what I've got currently for anyone else that wants to try and get it working or offer any suggestions all of the API calls work fine and seem to populate the relevant structures correctly and dont return any error codes, right up until the call to NetShareAdd at the end which returns ERROR_INVALID_PARAMETER and sets the parm_err argument to 501 (this is supposed to point to which member of the SHARE_INFO_502 structure is causing the invalid parameter error but there are only 10 members so 501 isnt much use!).

    vb Code:
    1. #Region "Constants"
    2.  
    3.     Const STYPE_DISKTREE As UInteger = 0
    4.     Const SECURITY_DESCRIPTOR_REVISION As UInteger = 1
    5.     Const ACL_REVISION As UInteger = 2
    6.     Const NO_INHERITANCE As UInteger = 0
    7.     Const ACCESS_READ As Integer = &H1
    8.     Const ACCESS_WRITE As Integer = &H2
    9.     Const ACCESS_CREATE As Integer = &H4
    10.     Const ACCESS_EXEC As Integer = &H8
    11.     Const ACCESS_DELETE As Integer = &H10
    12.     Const ACCESS_ATRIB As Integer = &H20
    13.     Const ACCESS_PERM As Integer = &H40
    14.     Const ACCESS_ALL As Integer = &H7F
    15. #End Region
    16.  
    17.  
    18. #Region "Enums"
    19.  
    20.     Public Enum NET_API_STATUS As Integer
    21.         NERR_Success = 0
    22.         ERROR_ACCESS_DENIED = 5
    23.         ERROR_INVALID_PARAMETER = 87
    24.         ERROR_INVALID_NAME = 123
    25.         ERROR_INVALID_LEVEL = 124
    26.         NERR_UnknownDevDir = 2116
    27.         NERR_RedirectedPath = 2117
    28.         NERR_DuplicateShare = 2118
    29.         NERR_BufTooSmall = 2123
    30.     End Enum
    31.  
    32.     Public Enum ACCESS_MODE As UInteger
    33.         NOT_USED_ACCESS = 0
    34.         GRANT_ACCESS = 1
    35.         SET_ACCESS = 2
    36.         DENY_ACCESS = 3
    37.         REVOKE_ACCESS = 4
    38.         SET_AUDIT_SUCCESS = 5
    39.         SET_AUDIT_FAILURE = 6
    40.     End Enum
    41.  
    42.     Public Enum MULTIPLE_TRUSTEE_OPERATION As UInteger
    43.         NO_MULTIPLE_TRUSTEE = 0
    44.         TRUSTEE_IS_IMPERSONATE = 1
    45.     End Enum
    46.  
    47.     Public Enum TRUSTEE_FORM As UInteger
    48.         TRUSTEE_IS_SID = 0
    49.         TRUSTEE_IS_NAME = 1
    50.         TRUSTEE_BAD_FORM = 2
    51.         TRUSTEE_IS_OBJECTS_AND_SID = 3
    52.         TRUSTEE_IS_OBJECTS_AND_NAME = 4
    53.     End Enum
    54.  
    55.     Public Enum TRUSTEE_TYPE As UInteger
    56.         TRUSTEE_IS_UNKNOWN = 0
    57.         TRUSTEE_IS_USER = 1
    58.         TRUSTEE_IS_GROUP = 2
    59.         TRUSTEE_IS_DOMAIN = 3
    60.         TRUSTEE_IS_ALIAS = 4
    61.         TRUSTEE_IS_WELL_KNOWN_GROUP = 5
    62.         TRUSTEE_IS_DELETED = 6
    63.         TRUSTEE_IS_INVALID = 7
    64.         TRUSTEE_IS_COMPUTER = 8
    65.     End Enum
    66.  
    67. #End Region
    68.  
    69.  
    70. #Region "Structures"
    71.  
    72.     <StructLayoutAttribute(LayoutKind.Sequential)> _
    73.     Public Structure SHARE_INFO_502
    74.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_netname As String
    75.         Public shi502_type As UInteger
    76.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_remark As String
    77.         Public shi502_permissions As Integer
    78.         Public shi502_max_uses As Integer
    79.         Public shi502_current_uses As Integer
    80.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_path As String
    81.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_passwd As String
    82.         Public shi502_reserved As Integer
    83.         Public shi502_security_descriptor As SECURITY_DESCRIPTOR
    84.     End Structure
    85.  
    86.     <StructLayoutAttribute(LayoutKind.Sequential)> _
    87.     Public Structure SECURITY_DESCRIPTOR
    88.         Public Revision As Byte
    89.         Public Sbz1 As Byte
    90.         Public Control As UShort
    91.         Public Owner As IntPtr
    92.         Public Group As IntPtr
    93.         Public Sacl As IntPtr
    94.         Public Dacl As IntPtr
    95.     End Structure
    96.  
    97.     <StructLayoutAttribute(LayoutKind.Sequential)> _
    98.     Public Structure ACL
    99.         Public AclRevision As Byte
    100.         Public Sbz1 As Byte
    101.         Public AclSize As UShort
    102.         Public AceCount As UShort
    103.         Public Sbz2 As UShort
    104.     End Structure
    105.  
    106.     <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Pack:=0)> _
    107.     Public Structure EXPLICIT_ACCESS
    108.         Public grfAccessPermissions As UInteger
    109.         Public grfAccessMode As ACCESS_MODE
    110.         Public grfInheritance As UInteger
    111.         Public Trustee As TRUSTEE
    112.     End Structure
    113.  
    114.     <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Pack:=0)> _
    115.     Public Structure TRUSTEE
    116.         Public pMultipleTrustee As UInteger
    117.         Public MultipleTrusteeOperation As MULTIPLE_TRUSTEE_OPERATION
    118.         Public TrusteeForm As TRUSTEE_FORM
    119.         Public TrusteeType As TRUSTEE_TYPE
    120.         <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)> _
    121.         Public ptstrName As String
    122.     End Structure
    123.  
    124. #End Region
    125.  
    126.  
    127. #Region "Native Methods"
    128.  
    129.     <DllImportAttribute("netapi32.dll", EntryPoint:="NetShareAdd")> _
    130.     Public Shared Function NetShareAdd(<InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal servername As String, ByVal level As UInteger, <InAttribute()> ByRef buf As SHARE_INFO_502, <OutAttribute()> ByRef parm_err As Integer) As NET_API_STATUS
    131.     End Function
    132.  
    133.     <System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint:="InitializeSecurityDescriptor")> _
    134.     Public Shared Function InitializeSecurityDescriptor(ByRef pSecurityDescriptor As SECURITY_DESCRIPTOR, ByVal dwRevision As UInteger) As UInteger
    135.     End Function
    136.  
    137.     <System.Runtime.InteropServices.DllImportAttribute("Advapi32.dll", EntryPoint:="SetEntriesInAclW")> _
    138.     Public Shared Function SetEntriesInAcl(ByVal cCountOfExplicitEntries As Integer, _
    139.                                            <System.Runtime.InteropServices.InAttribute()> ByRef pListOfExplicitEntries As EXPLICIT_ACCESS, <System.Runtime.InteropServices.InAttribute()> ByVal OldAcl As System.IntPtr, ByRef NewAcl As System.IntPtr) As UInteger
    140.     End Function
    141.  
    142.     <System.Runtime.InteropServices.DllImportAttribute("Advapi32.dll", EntryPoint:="BuildExplicitAccessWithNameW")> _
    143.     Public Shared Sub BuildExplicitAccessWithName(ByRef pExplicitAccess As EXPLICIT_ACCESS, <InAttribute()> ByVal pTrusteeName As IntPtr, ByVal AccessPermissions As UInteger, ByVal AccessMode As UInteger, ByVal Inheritance As UInteger)
    144.     End Sub
    145.  
    146.     <System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint:="SetSecurityDescriptorDacl")> _
    147.     Public Shared Function SetSecurityDescriptorDacl(ByRef pSecurityDescriptor As SECURITY_DESCRIPTOR, <MarshalAsAttribute(UnmanagedType.Bool)> _
    148.                                                      ByVal bDaclPresent As Boolean, <InAttribute()> ByVal pDacl As System.IntPtr, <MarshalAsAttribute(UnmanagedType.Bool)> ByVal bDaclDefaulted As Boolean) As UInteger
    149.     End Function
    150.  
    151.     <DllImportAttribute("advapi32.dll", EntryPoint:="IsValidSecurityDescriptor")> _
    152.     Public Shared Function IsValidSecurityDesctiptor(ByRef pSecurityDescriptor As SECURITY_DESCRIPTOR) As UInteger
    153.     End Function
    154.  
    155. #End Region
    156.  
    157.  
    158. #Region "Managed Methods"
    159.  
    160.     Private Sub CreateShare(ByVal FullUsername As String)
    161.         Dim ea As EXPLICIT_ACCESS = Nothing
    162.         Dim AccountNamePtr As IntPtr = Marshal.StringToHGlobalUni(FullUsername)
    163.         BuildExplicitAccessWithName(ea, AccountNamePtr, ACCESS_READ, ACCESS_MODE.SET_ACCESS, NO_INHERITANCE)
    164.  
    165.         Dim AclPtr As IntPtr
    166.         Dim SetEntriesResult As UInteger = SetEntriesInAcl(1, ea, Nothing, AclPtr)
    167.         MessageBox.Show("SetEntries = " & SetEntriesResult)
    168.  
    169.         Dim SecDesc As SECURITY_DESCRIPTOR
    170.         Dim DecriptorInitResult As UInteger = InitializeSecurityDescriptor(SecDesc, SECURITY_DESCRIPTOR_REVISION)
    171.         MessageBox.Show("InitSecurityDescriptor = " & DecriptorInitResult)
    172.  
    173.         Dim SetSecurityResult As UInteger = SetSecurityDescriptorDacl(SecDesc, True, AclPtr, False)
    174.         MessageBox.Show("SetSecurityDescriptorDacl = " & SetSecurityResult)
    175.  
    176.         MessageBox.Show("Is Valid Descriptor = " & IsValidSecurityDesctiptor(SecDesc))
    177.  
    178.         Dim ShareInfo As New SHARE_INFO_502
    179.         With ShareInfo
    180.             .shi502_netname = "test"
    181.             .shi502_type = STYPE_DISKTREE
    182.             .shi502_remark = "Testing"
    183.             .shi502_permissions = 0
    184.             .shi502_max_uses = -1
    185.             .shi502_current_uses = 0
    186.             .shi502_path = "C:\TestingFolder"
    187.             .shi502_passwd = Nothing
    188.             .shi502_reserved = 0
    189.             .shi502_security_descriptor = SecDesc
    190.         End With
    191.  
    192.         'Dim Dacl As ACL = DirectCast(Marshal.PtrToStructure(SecDesc.Dacl, GetType(ACL)), ACL)
    193.         'Dim ShareInfoSize As Integer = Marshal.SizeOf(ShareInfo)
    194.         'Dim SharePtr As IntPtr = Marshal.AllocCoTaskMem(ShareInfoSize)
    195.         'Marshal.StructureToPtr(ShareInfo, SharePtr, False)
    196.  
    197.         Dim ParameterError As Integer = 0
    198.         Dim Result As String = NetShareAdd(Nothing, 502, ShareInfo, ParameterError).ToString
    199.  
    200.         MessageBox.Show("NetShareAdd result = " & Result & ", param error = " & ParameterError)
    201.     End Sub
    202.  
    203. #End Region
    204.  
    205.  
    206. #Region "Event Handlers"
    207.  
    208.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    209.         CreateShare(Environment.UserDomainName & "\" & Environment.UserName)
    210.     End Sub
    211.  
    212. #End Region

    The problem definitely lies somewhere with the security descriptor because if I comment out this line then the code completes successfully and shares the folder (it just doesnt set the share permissions obviously) :
    vb Code:
    1. .shi502_security_descriptor = SecDesc
    Last edited by chris128; May 26th, 2010 at 05:06 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width