Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Set file permissions using Perl in Windows?

by wilsond (Scribe)
on Jan 10, 2009 at 16:39 UTC ( [id://735405]=perlquestion: print w/replies, xml ) Need Help??

wilsond has asked for the wisdom of the Perl Monks concerning the following question:

Edit: The cacls.exe command line utility included with Windows seems to work very well for me. It works in all of the Windows versions I've tested: Windows 2000, Windows XP, Windows Vista, and Windows 7. If you need a simple ACL update utility, you can just use it as it seems to reliably exist in all Windows versions at least as new as Win2k. Just be sure to use echo y| cacls.exe .... format ("y" touching the pipe) to use it in an automated fashion.


How would one set file permissions in Windows?

I've made an installer that installs a utility written in Perl and "compiled" with ActiveState's PDK. But, it seems, by default the files are "Power Users" writable as well as original installer user writable, which are bad in my case. I'd like to specifically say "System && Admin Group, no one else" writable. I'd even better like a specific file to be non-readable as well. All of this is in one directory within the "Program Files" dir.

I've found Win32::Security::ACL, but it's mega-confusing. The extent of my real knowledge of Windows file ACLs is how to click the buttons in the Security tab. If it were going onto a few boxes, I'd just do that. But in my case, it's going on about 2,000 machines.

edit: This case is in XP, but it will be going onto Win2k and Vista as well, though not as large quantity.

Replies are listed 'Best First'.
Re: Set file permissions using Perl in Windows?
by CountZero (Bishop) on Jan 10, 2009 at 19:50 UTC
    Win32::Security::ACL comes with two scripts: PermDump.pl and PermChg.pl.

    PermDump.pl will output a CSV file with all permission data of your files and PermChg.pl takes the same (format of) CSV file (suitably changed I assume) and applies the permissions in this CSV-file to your files.

    It shouldn't be too difficult to find out how to implement your "System && Admin Group, no one else" writable scheme: just set the permissions for one file by hand (using the buttons in the security tab) and PermDump.pl the permissions for that file. Then apply what you found to the other files with PermChg.pl

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      If cacls.exe fails for me somehow (it's worked great so far in XP), I'll definitely give your recommendation a shot. Thanks for your input!
Re: Set file permissions using Perl in Windows?
by Corion (Patriarch) on Jan 10, 2009 at 16:52 UTC

    I would punt and use cacls.exe (comes with Windows) or maybe the SetACL program (but I haven't used that at all).

      I'd be willing to use cacls.exe assuming it's available in Win2K and Vista (I will test this soon).

      Assuming it is, how would you force it to not ask "Are you sure?"?

      edit: the command I'm calling is this: cacls.exe "C:\Program Files\MyDir" /T /G SYSTEM:F Administrators:F

        Answered my own question: http://support.microsoft.com/kb/135268

        echo y| cacls...

        edit: the y and the pipe must be touching...

Re: Set file permissions using Perl in Windows?
by cdarke (Prior) on Apr 08, 2010 at 11:29 UTC
    As an update, an alternative to the echo y| stuff is to use xcacls.exe from the Windows Resource Kit - this version does not prompt. See http://support.microsoft.com/kb/318754.

    At Windows Vista and Windows 7 the cacls program still works but is deprecated in favour of the more powerful icacls program.
Re: Set file permissions using Perl in Windows?
by hawtin (Prior) on Jan 11, 2009 at 15:37 UTC

    Personally I would use:

    Win32::File::SetAttributes()
      According to the (very short) documentation, Win32::File seems to deal only with the "ARCHIVE COMPRESSED DIRECTORY HIDDEN NORMAL OFFLINE READONLY SYSTEM TEMPORARY" attributes. It could not find anything about file permissions.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://735405]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-19 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found