Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify Windows NTFS permissions in Perl?

I'm using ActiveState Perl on Windows Server 2003.

I want to create a directory on a Windows NTFS partition and then grant a Windows NT security group read access to the folder. Is this possible in Perl? Would I have to use Windows NT commands or is there a Perl module to do it?

A small example would be much appreciated!

like image 308
Mark Allison Avatar asked Nov 19 '08 16:11

Mark Allison


People also ask

What is modify in NTFS?

Modify: Allows users to read and write of files and subfolders; also allows deletion of the folder.

How do I set share and NTFS permissions?

In Windows Explorer, right-click the folder you want to share, and then click Properties. On the Sharing tab, click Advanced Sharing. In User Account Control, click Continue to accept the prompt that Windows needs your permission to perform the action. In the Advanced Sharing dialog box, check Share this folder.


2 Answers

The standard way is to use the Win32::FileSecurity module:

use Win32::FileSecurity qw(Set MakeMask);

my $dir = 'c:/newdir';
mkdir $dir or die $!;
Set($dir, { 'Power Users' 
            => MakeMask( qw( READ GENERIC_READ GENERIC_EXECUTE ) ) });

Note that Set will overwrite the permissions for that directory. If you want to edit the existing permissions, you'd need to Get them first:

my %permissions;
Win32::FileSecurity::Get($dir, \%permissions);
$permissions{'Power Users'}
  = MakeMask( qw( READ GENERIC_READ GENERIC_EXECUTE ) ) });
Win32::FileSecurity::Set($dir, \%permissions);
like image 96
cjm Avatar answered Oct 29 '22 21:10

cjm


Here's a generic permissions package for ActivePerl.

use Win32::Perms;

# Create a new Security Descriptor and auto import permissions
# from the directory
$Dir = new Win32::Perms( 'c:/temp' ) || die;

# One of three ways to remove an ACE
$Dir->Remove('guest');

# Deny access for all attributes (deny read, deny write, etc)
$Dir->Deny( 'joel', FULL );

# Set the directory permissions (no need to specify the
# path since the object was created with it)
$Dir->Set();

# If you are curious about the contents of the SD
# dump the contents to STDOUT $Dir->Dump;
like image 42
Vinko Vrsalovic Avatar answered Oct 29 '22 21:10

Vinko Vrsalovic