Change NTFS Permissions or run shell script

Discussion in 'ASP .Net Security' started by Shawn H. Mesiatowsky, Feb 3, 2005.

  1. I am trying to Create a web app that creates a directory, and assigns
    permissions to the directory. Is there a way to do this, or even run a shell
    comand (like cacls.exe) to alter the NTFS permissions on the directory?
    Thanks for your help

    Shawn H. Mesiatowsky
    Shawn H. Mesiatowsky, Feb 3, 2005
    #1
    1. Advertising

  2. Look at the System.IO namespace and the Directory class.

    It's not easy to set the permissions if you do not use .NET 2.0 yet. There
    is a Win32 wrapper class around gotDotNet.com you can try.

    The namespace System.Diagnostics holds a class called ProcessInfo witch can
    be used to run "calcs.exe" for example.

    --
    Daniel Fisher(lennybacon)
    MCP ASP.NET C#
    Blog: http://www.lennybacon.com/


    "Shawn H. Mesiatowsky" <smesiatowsky@spam_no_perfectfit-ind.com> wrote in
    message news:%...
    >I am trying to Create a web app that creates a directory, and assigns
    >permissions to the directory. Is there a way to do this, or even run a
    >shell comand (like cacls.exe) to alter the NTFS permissions on the
    >directory? Thanks for your help
    >
    > Shawn H. Mesiatowsky
    >
    Daniel Fisher\(lennybacon\), Feb 4, 2005
    #2
    1. Advertising

  3. Shawn H. Mesiatowsky

    Vladimir Guest

    RE: Change NTFS Permissions

    ' Setting NTFS permissions

    ' Creating Access Control Entry (ACE) object
    Function SetACE(AccessMask, AceFlags, AceType, objTrustee)
    Set objACE = getObject("Winmgmts:Win32_Ace").Spawninstance_
    objACE.AccessMask = AccessMask
    objACE.AceFlags = AceFlags
    objACE.AceType = AceType
    objACE.Trustee = objTrustee
    Set SetACE = objACE
    End Function

    Wscript.Echo "Script running ..."

    Set objs = GetObject("Winmgmts:").InstancesOf("Win32_AccountSID")

    For Each obj In objs
    strValue = obj.Properties_("Element") ' object refrence
    Set objElement = GetObject("Winmgmts:"+strValue) ' getting object
    strName = objElement.Properties_("Name")
    If strName = "TinaTurner" Then ' that's it
    Exit For
    End If
    Next

    ' Getting SID
    strValue = obj.Properties_("Setting")
    Set objSid = GetObject("Winmgmts:"+strValue)
    BinaryRepresentationOfSid = objSid.Properties_("BinaryRepresentation")

    ' Group "All"
    Set objTrusteeAll = getObject("Winmgmts:Win32_Trustee").SpawnInstance_
    objTrusteeAll.SID = Array(1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0)

    ' User GeorgeSmith
    Set objTrusteeTT = GetObject("Winmgmts:Win32_Trustee").SpawnInstance_
    objTrusteeTT.SID = BinaryRepresentationOfSid

    ' Setting NTFS permissions ...
    Set Ace = SetACE(2032127, 3, 0, objTrusteeAll) ' full access

    Set objSecDescriptor =
    GetObject("Winmgmts:Win32_SecurityDescriptor").SpawnInstance_
    objSecDescriptor.DACL = Array(Ace)

    ' One folder
    folderName = "C:\Data\"
    Set obj = GetObject("Winmgmts:Win32_Directory='" & folderName & "'")
    Set objClass = GetObject("Winmgmts:Win32_Directory")
    Set objInParam =
    obj.Methods_("ChangeSecurityPermissions").inParameters.SpawnInstance_
    objInParam.Option = 4 'DACL
    objInParam.SecurityDescriptor = objSecDescriptor
    Set objOutParams = obj.ExecMethod_("ChangeSecurityPermissions", objInParam)

    Wscript.Echo folderName & " ..."
    If objOutParams.ReturnValue = 0 Then
    str = "... changed"
    Else
    str = "Error!" & vbCrLf & " ReturnValue = " & objOutParams.ReturnValue
    End if
    Wscript.Echo str

    ' Another folder
    Set Ace1 = SetACE(1179817, 3, 0, objTrusteeAll) ' read
    Set Ace2 = SetACE(2032127, 3, 0, objTrusteeTT) ' full

    Set objSecDescriptor =
    GetObject("Winmgmts:Win32_SecurityDescriptor").SpawnInstance_
    objSecDescriptor.DACL = Array(Ace1, Ace2)

    folderName = "C:\Data\Accounting"
    Set obj = GetObject("Winmgmts:Win32_Directory='" & folderName & "'")
    Set objClass = GetObject("Winmgmts:Win32_Directory")
    Set objInParam =
    obj.Methods_("ChangeSecurityPermissions").inParameters.SpawnInstance_
    objInParam.Option = 4 'DACL
    objInParam.SecurityDescriptor = objSecDescriptor
    Set objOutParams = obj.ExecMethod_("ChangeSecurityPermissions", objInParam)

    Wscript.Echo folderName & " ..."
    If objOutParams.ReturnValue = 0 Then
    str = "... changed"
    Else
    str = "Error!" & vbCrLf & " ReturnValue = " & objOutParams.ReturnValue
    End if
    Wscript.Echo str

    Wscript.Echo "Finish"
    Vladimir, Apr 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shawn H. Mesiatowsky

    Change NTFS Permissions or run shell script

    Shawn H. Mesiatowsky, Feb 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    473
    Shawn H. Mesiatowsky
    Feb 3, 2005
  2. Gaetan
    Replies:
    0
    Views:
    611
    Gaetan
    Jan 20, 2006
  3. Morten V. Pedersen

    NTFS permissions for ASP.NET user

    Morten V. Pedersen, Oct 22, 2003, in forum: ASP .Net Security
    Replies:
    4
    Views:
    118
    Jacob Yang [MSFT]
    Oct 29, 2003
  4. Alex Maghen
    Replies:
    0
    Views:
    164
    Alex Maghen
    Feb 3, 2005
  5. Firas Assaad

    How to change NTFS Permissions programmitcally

    Firas Assaad, Jan 13, 2006, in forum: ASP General
    Replies:
    6
    Views:
    148
    Stefan
    Apr 17, 2006
Loading...

Share This Page