http://qs321.pair.com?node_id=700718

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

Fellow Monasterians,

I comfortable working on the command line to do this, but I've created Web app that uses CGI's upload method to upload a file from the browser. On the way, if the subdirectory 'foobar' doesn't exist, my script makes it.

I have the permissions working, but I'm getting the desired ownership of:

drwxrwx--- 2 www-data sftp 4096 2008-07-28 22:18 foobar

instead I'm ending up with:

drwxrwx--- 2 www-data www-data 4096 2008-07-28 22:18 foobar

Likewise, the file ownership itself is the same:

-rwxrwx--- 1 www-data www-data 27034 2008-07-28 22:32 wilma.jpg

Here's my script:

mkdir('foobar') or die...; my $mode = 0770; chmod $mode, 'foobar' or die...; chown 'www-data', 'sftp', 'foobar' or die...; my $upload_filehandle = $query->upload($upload); open UPLOADFILE, ">$upload_dir/$filename"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE; $mode = 0770; chmod $mode, "$upload_dir/$filename";

Is this even possible? Thanks.

Update: Fixed typo

Update 2: per comments of the good monks, I changed chown 'www-data', 'sftp', 'foobar' or die; to chown -1, 1003, 'foobar' or die; (1003 being the numeric equivalent for the group) for success.

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: Script not setting ownship of new directory
by pc88mxer (Vicar) on Jul 29, 2008 at 03:17 UTC
    You have to supply numeric uid and gids to chown.

    See the documentation for chown for an example of how to translate user and group names to their numeric equivalents.

Re: Script not setting ownship of new directory
by sgifford (Prior) on Jul 29, 2008 at 05:16 UTC
    In addition to using numeric IDs (as pc88mxer suggests), you will need to make sure the user your Web app is running as is in the sftp group. The list of groups is stored in the $( variable, and this will print all of the groups you are in so you can check:
    print join( " ",$( );

    Also, using -1 for the first argument, to avoid changing the owner and only change the group, may be simpler.

      Thanks sgifford, pc88mxer, et al! I changed the user id to -1 and went with numeric equivalent for the group and it worked as advertised. And hopefully without anything that would jeopardize the system as graff warned against.

      chown -1, 1003, 'foobar' or die;

      Again, the Monastery comes through.

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Script not setting ownship of new directory
by graff (Chancellor) on Jul 29, 2008 at 04:51 UTC
    As you must know, "chown" on any unix or linux box is strictly a root-user activity. There is simply no way to provide chown access without also providing root access. If you want your web server to run a cgi process that does chown, it must run as root, or else you must violate a basic principle of OS security by creating a set-uid script that does chown. In either case, you will be fully entitled to all the risks and disasters that this could conceivably support.

    It would be better for the web transaction to create some sort of signal or other symptom that could be picked up by a separate, root-owned process (not directly accessible via http), so that this root-owned process could do the right thing in terms of changing ownership on specific files, so that a web process does not need to do this.

    Or, you could run a process under your "sftp" user account that copies the uploaded files and thereby assumes ownership of the copies. If you want to keep the files where they are with original names intact, and the "sftp" user is able to have write access in the directory where the "www-data" user puts uploaded files (e.g. if they are both included in a given group, and there's group write access on the directory), "sftp" can rename the uploaded files, copy them to the original names (thereby taking ownership) and then delete the www-owned originals. "Look, ma! No root privileges!" (Generally better and certainly safer that way, IMHO.)

      chown will allow a user to change the group of a file to any group that the user is member of. But, in general, you need to have effective root privs to change the owner.
        Right. It seems I misunderstood the OP's question. I thought (mistakenly) he was talking about changing user ownership. If it's just a matter of changing group ownership, then yes, chown should work fine without root privilege and without problems, so long as the current user is included in the target group.
      As you must know, "chown" on any unix or linux box is strictly a root-user activity.

      That's not entirely true. Sure, (with the exception already pointed out), you have to be root on most unix boxes, but there have been OS versions (older versions of SunOS/Solaris for instance) that allowed users to "give away" their files. That is, they could "chown" their own files to some other user. While I've used that feature once or twice, it's not something I really miss.

        HP-UX also supports it.

        --MidLifeXis

Re: Script not setting ownship of new directory
by Anonymous Monk on Jul 29, 2008 at 03:09 UTC
    Is this even possible? Thanks.
    If you have permission it is.
Re: Script not setting ownership of new directory
by swampyankee (Parson) on Jul 29, 2008 at 14:30 UTC

    For FreeBSD, man pages on the chown "the ownership of a file may only be altered by a super-user for obvious security reasons." If you're just changing group ownership, you could try the chgrp command, where the user executing it "must belong to the specified group and be the owner of the file, or be the super-user." Other *ix may vary, but I've not seen one that permits ordinary users to change file ownership for about 15 years.


    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc