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

I'm looking for a way to run my perl script as a user other than nobody. With nobody, creating files and then later attempting to modify them brings complications. I've tried umask 002, which set the new file's permissions to -rw-rw-rw, but I still can not modify them (Permission Denied).

I've just read about a new approach that involves chmod 4755 to set the s-bit. As far as I know, this should allow my script to execute under my ownership instead of nobody. I've tried this several different ways, but I don't think it works. I read somewhere that it doesn't work with scripting languages like perl.

Does anyone have an experience with this. I need to have my script create files that are editable by both my username and the username nobody. There has to be a way...thanks in advance.

Replies are listed 'Best First'.
Re: running cgi as user other than nobody
by belg4mit (Prior) on Apr 01, 2002 at 23:36 UTC
    There are many ways, some are ugly, and none especially perl.
  • have your webserver run as root, you can then make the files whatever you want ;-)
     This is of course, a Bad Idea.
  • clever use of the Group directive
  • http://stein.cshl.org/WWW/software/sbox/
  • http://cgiwrap.sourceforge.net/
  • http://www.snert.com/Software/mod_become/index.shtml
  • UPDATE: I forgot to mention (memory spurred by tangential remark by jeffa), setting the directory to setgid. Thus if nobody and user are in a group, and the directory is group writable and setgid group all files created within will be in the group and editable by both. I have done this before, it can get a little hairy though. This works best with a RedHat type system, since each user has their own group. All that is required is to add nobody to each of these groups.

    Achh, I can't believe I missed suexec! Zaxo++. (Suexec can be a major pain though, most notably the DocumentRoot gets locked in at compile time) I actually used both suexec and the setgid trick together. The reason why I needed the setgid supplement is that I was also using mod_dav. With WebDAV files are created by the webserver *proper*...

    PS> umask 002 could not give you a file that is a+w (or even o+w to be more specific).

    --
    perl -pe "s/\b;([st])/'\1/mg"

      I've read about some of these options, but are there any that don't involve installing extra server-side software?
        Ahh that is an additional stipulation you did not initally make. Two of the options I gave meet this new criterion, also see the update I have added. However, IMO this is much like somebody coming here asking to implement some given functionality and saying "no modules". There is a very good reason this software(or modules) exist, and that is to meet a need and make the solution available for others, no reinventing the wheel.

        --
        perl -pe "s/\b;([st])/'\1/mg"

Re: running cgi as user other than nobody
by Zaxo (Archbishop) on Apr 02, 2002 at 00:09 UTC

    My favorite method is to run (on Apache) with the suEXEC option set. That has your cgi scripts run as you. There are dangers similar to running suid, but you have much better use of unix native filesystenm security.

    After Compline,
    Zaxo

Re: running cgi as user other than nobody
by dws (Chancellor) on Apr 02, 2002 at 00:48 UTC
    Before heading off into setuid land, I suggest figuring out why you're having permission problems.

    As long as you're writing into a directory that's 777, your CGI should be able to chmod. Check your directory permission, and double-check that the file path you're passing to chmod is exactly the same as the file you just created.

Re: running cgi as user other than nobody
by theguvnor (Chaplain) on Apr 02, 2002 at 00:54 UTC

    emilford,

    I have used the setuid (chmod 4755) method extensively with Perl scripts, so not sure where you read that it wouldn't work. One caveat is that a properly configured *nix system will reset the s-bit anytime the file is modified (e.g. you upload an improved version), so you have to specifically re-chmod it everytime you make an update. This is a security feature.

    I need to have my script create files that are editable by both my username and the username nobody.

    On a slightly pedantic note that might have been overlooked, if you run your script setuid, then the script is running as you, not "nobody". And that is probably what you're looking for, since the point is that you want your scripts to be able to modify the files, and still be able to login via Telnet/SSH/FTP/whatever and modify them.

    Hope this helps... Jon

Re: running cgi as user other than nobody
by Desdinova (Friar) on Apr 02, 2002 at 00:01 UTC
    My trick for this has been to use chmod to alter the file permissions before i exit the script so the next instance can open it again. That combined chown to set the group to yours should allow the access
Re: running cgi as user other than nobody
by archen (Pilgrim) on Apr 02, 2002 at 00:10 UTC
    Out of curiosity, what OS is this running on? As a sort of blind stab in the dark, I'd say having permissions at 0666 should be good enough, but I've run into similar problems with permissions myself. If the script is called by Apache, it's probably run as nobody - which will first require that nobody can write to the target directory. You might also consider explicitly chmod'ing the files to 0766 after you've written them - although this shouldn't be neccesary since generally who/whatever creates a file should own it.
      I believe the script is being called by Apache, but then again, I'm not 100% sure. I have the directory set (for testing only) to 777 and the file I'm trying to modify is set to 666. I'm still not able to make any changes...always get a Permission Denied. I also get a Permission Denied error when trying to chmod the file from within the script. Could I be doing this wrong?
        well if you can't chmod the file within the same script then I'd say you're overlooking something (hard to say without looking at the code). I've never actually seen an instance where something like this doesn't work:
        ... open(FILE, ">$file") || die $!; ... [stuff] close(FILE); chmod 0666 $file;
        Opening the file later might still be another issue...
Re: running cgi as user other than nobody
by Anonymous Monk on Apr 02, 2002 at 06:19 UTC
    Thanks for your feedback. I'll take another look at everything in the morning and double check to make sure that I'm not just doing something dumb. Thanks again.
Re: running cgi as user other than nobody
by pizza_milkshake (Monk) on Apr 02, 2002 at 14:02 UTC
    I think setuid/setgid is what you're looking for

    perl -MLWP::Simple -e'getprint "http://parseerror.com/p"' |less