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

I've written a script that now generates files based on information submitted through an HTML form. Everything works fine, but I can never seem to get the file permissions set correctly. I think my problem is that the script creates the file as user "99". I've tried to make changes to the files manually as "myusername" but I always get a permission denied.
My next guess was to use the chmod command in the script, so that hopefully this command would be issued as user "99".

chmod 0666, file.txt; #tried this way system("chmod 666 file.txt"); #and this way
Neither one of these change the permissions. Basically, I'm not sure how to change the file permissions from within the script. There has to be a simple way and I'm just missing something.

One other thing...Is there a way to create a file as a different user, so that I don't have to deal with trying to change the permissions through the script? If so, how do I find out what my uid (etc) is? When ever files are created as user "99" I have to call up and get the root user to remove the files since my user doesn't have permissions. Big pain! Any suggestions? TIA

Replies are listed 'Best First'.
Re: create file - permission problems
by webadept (Pilgrim) on Apr 01, 2002 at 07:45 UTC
    This is just a quick answer to the user question. Generally someone on a web page is a user called "nobody" but this changes with different ISP's, and you'll need to check with yours to see what that user is. The Nobody person is restricted in many ways by the webserver software engine. Most of my experience is with Apache servers by the way.

    If you are going to create files that need to be accessed by someone on a web page, then that's the user id the file needs to be created on. CHOWN will do that for you.

    666 generally works with most files, make sure that the file is in a directory that is accessable by the web user, not just the script accessing it. This directory should be under the area the web page runs from. Otherwise you will run into access denide errors.

    If you create a directory for this, make sure the permissions are okay on the directory itself. 777 is generally okay for directories, but never for files accessed by web users. I would suggest 744 for files that don't require writing acces (and none of them should) on a web site. But that's just my personal belief system there, and there are reasons for alterations.

    Hope that helps, probably some of the other monks around here will send some pointers to where you can find more information on file setting and such. I know there is a good CGI write up on the subject.

    and you might want to take a look at this site CGI Tutorial

    webadept

    My Husband ran off with my shawman, but they love me as I am -- Tori Amos
Re: create file - permission problems
by rjray (Chaplain) on Apr 01, 2002 at 07:56 UTC

    You are at a disadvantage, in that you are limited to the permissions of the UID under which your script runs. In addition to that, you probably aren't looking at the setting of umask for the UID, either. You can create a file, then change the ownership of it with the chown system call, which is documented alongside chmod.

    The problem here is one of the permissions allotted to the UID under which the script runs. You probably do not have the permissions necessary to run chown. You should start by setting the umask to something like, well, 0. Read the manual page on it, either at perlfunc:umask or in the manual page for most shells that run on a UNIX system, as there is a command in these shells that manipulates the mask, as well. Odds are very good that your script is running in an environment where the umask has been set (probably in the server configuration) to a fairly restrictive default value.

    --rjray

Re: create file - permission problems
by mla (Beadle) on Apr 01, 2002 at 16:13 UTC
    chmod 0666, file.txt; #tried this way system("chmod 666 file.txt"); #and this way

    Did you check the return value and log the error message?

    chmod 0666, $file or die "chmod failed on '$file': $!";

    Note that you really shouldn't depend on your current working directory being anywhere in particular (unless you do a chdir yourself)? Better to use a fully qualified path.

    You might also want to see if your ISP will support something like Apache's suEXEC so that your scripts will run under your UID.

    If you must use very lax permissions like 666, consider turning off group and world read permission on your home directory (or at least on the directory containing your data files) to help hide its contents. You only need the execute bits enabled to allow the web server to traverse the directory.

Re: create file - permission problems
by emilford (Friar) on Apr 01, 2002 at 21:32 UTC
    I've been toying around with the umask command, but still have yet gotten the file permissions to work how I want them to. I took your advice and looked up some info on umask. The internet site I found said to issue the command umask 002 in order to set the permissions to -rw-rw-rw-. After creating the file with my script, the permissions are indeed set to -rw-rw-rw- and the owner is "99". How come I still can't edit this file through my script or through my own username? Any suggestions?