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

I wrote a perl script that needs to write a temporary file. I want to create a directory with appropriate permissions.

Now I understand there is Read/Write/Execute permissions and also User/Group/Other.

Read/Write/Execute seem resonably straight forward. But what is the differences between User / Groups / Other for directory permissions?

I created a temp folder, but for the script to write to it successfully from the cgi folder "Other" seemed to need Write & Execute privilages. This seems kind of unsafe.

What is the best practice for permission for a directory which I wish a perl script to write and then delete a temporary file?

Thanks for the help

Replies are listed 'Best First'.
Re: CHMOD permissions for Perl Script
by revdiablo (Prior) on Dec 29, 2004 at 23:00 UTC
    I created a temp folder, but for the script to write to it successfully from the cgi folder "Other" seemed to need Write & Execute privilages.

    Without any additional information, I'm guessing it's because the directory's user and group ownership were set to something other than what the CGI script was running as. When a web server executes one of your CGI scripts, it runs as the same user as the web server process, unless the web server explicitly changes it. Even if the web server does so, your directory probably does not have the correct user/group ownership.

    One solution is to look at the user/group ownership on the file created by your CGI script. This will tell you what user/group the CGI process is running as. Use the same value on your temp directory, then you should be able to set that directory's permissions to "rwx------".

Re: CHMOD permissions for Perl Script
by tcf03 (Deacon) on Dec 29, 2004 at 21:07 UTC
    rwxrwxrwx = read write execute for user group and world

    the first three rwx are for user perms
    the second three rwx are for group perms
    the last three are world perms


    r=read
    w=write
    x=execute


    rwxr--r-- = user has read write execute, group has read, and the world has read perms...

    as for your script it depends on what you ae doing - how safe does it ned to be, what uid si your script running as, etc...

      I will be getting a string from a textarea tag that was posted to the script.

      That posted string will contain the entire HTML of the file I want to create.

      So I am writing that information to a file in a temporary directory. Then I use Net::FTP to copy the file from the temporary directory to the main directory which has very restrictive permissions but permits FTP transfers. After that I delete the temporary file.

      What I want to do is give the TEMP directory sufficient permission so that the script can write the file, and then eventualy delete it. But not enough permission that a random person could upload a script and run it.

      At present I seem to have to give "Other" or world permission to write & execute for the script to be able to write the file without error even through the script that is creating the file is in the regular CGI-BIN directory. I would have thought that execute permission would not be neccessary for a file write if the executing file was in a different directory (my CGI-BIN) but this does not seem to be the case.

        perldoc IO::File is your friend
Re: CHMOD permissions for Perl Script
by eyepopslikeamosquito (Archbishop) on Dec 29, 2004 at 22:05 UTC
Re: CHMOD permissions for Perl Script
by Anonymous Monk on Dec 30, 2004 at 17:17 UTC
    Based on the content of your question, I assume your program is running on a Unix system. Considering that any Unix already has a directory specially for the purpose of writing temporary files to, namely /tmp, why would you want to create a directory of your own?

    And if you want to make it platform independent, just use:

    my $tmpdir = do { require 'File::Spec'; File::Spec->tmpdir(); };
    And there are even module that will let you write to a temporary file - but I can't judge from your question whether that will be appropriate for your needs.
Re: CHMOD permissions for Perl Script
by TedPride (Priest) on Dec 29, 2004 at 22:16 UTC
    You shouldn't need write permissions for Other for it to work. I'm using rwxrwxr-x on my temporary file directory and it works just fine. I don't know about the other permissions, however - I'd have to remove them one at a time and see what happens.