views:

863

answers:

2

Hi there,

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!

+6  A: 

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;
Vinko Vrsalovic
+7  A: 

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);
cjm

related questions