0

I'm having trouble handling the uploaded files on my web server.

First the file is uploaded to the temp server by user "daemon" then I copy the file to www/myapp/files folder by the "www-data" user. Everything works fine, I can read and write to the file, but when I try to delete the file I get an error. Because I'm trying to delete the file as www-data user (php script), but the owner of the file is daemon user.

My question is how can I fix this?

I'm not looking for any chmod or chown solutions, I prefer the solution to be through Apache or some other configuration files.

EDIT: As requested file permissions: -rw-r--r-- 1 daemon daemon 41638 Jan 19 08:59 FILE

The parent folder has 0777 permissions

1
  • What 'umask' does the 'daemon' user have ? You might be able to alter this so that created files are dropped with suitable permissions to allow other users to then delete the file...You may also have to take into account system-wide ('default default') umask settings as well... suggest you post here the exact permissions of the file that is being dropped on disk (run a 'ls -l' on the file) - so people might be able to work out why the file cannot be deleted by another user.
    – monojohnny
    Feb 18, 2014 at 12:45

2 Answers 2

1

You can add both users to one group like this:

usermod -a -G groupName userName

And then set up r\w permissions for that group

2
  • still chmod solution. What I prefer of doing is to change the owner of the uploaded file, but again without chmod.
    – D.J.
    Feb 18, 2014 at 12:28
  • @J.D. what is your objection to using chmod to modify file and folder permissions.. it's somewhat illogical. (+1).
    – AD7six
    Feb 18, 2014 at 15:01
1

Avoid assumptions

Everything works fine, I can read and write to the file

This indicates that the file permissions themselves, and ownership, permit current usage. If as you say apache is running as www-data, it directly contradicts this:

As requested file permissions: -rw-r--r-- 1 daemon daemon 41638 Jan 19 08:59 FILE

Which would mean the file is not writable to www-data.

Because I'm trying to delete the file as www-data user (php script), but the owner of the file is daemon user.

The above statement is not true - ownership of a file does not affect who can delete it.

I'm not looking for any chmod or chown solutions, I prefer the solution to be through Apache or some other configuration files.

How about not ruling out solutions until you have a choice =)?

Deleting a file uses directory permissions, not file permissions

This is easily verifiable:

-> pwd
/tmp/so
-> whoami
www-data
-> ls -la
total 8
dr-xr-xr-x 2 www-data www-data 4096 Feb 18 14:34 .
drwxrwxrwt 8 root     root     4096 Feb 18 14:36 ..
-rw-rw-r-- 1 www-data www-data    0 Feb 18 14:34 a-file
-> rm a-file 
rm: cannot remove `a-file': Permission denied

note there is no write permissions to the folder /tmp/so - it's the only permission that matters. Here's another existing answer as a supportive reference.

So given that, the only solution is to ensure that the user attempting to delete a-file has write permission to the containing folder, which means for example:

# assuming daemon is the owner
chmod 7x7 www/myapp/files 
        ^ www-data is not the owner or in the group daemon - so world perms apply

Or

chown www-data:www-data www/myapp/files
chmod 7x7 www/myapp/files 
        ^ daemon needs write permission to the folder too

Or

chown www-data:sharedgroup www/myapp/files
chmod 77x www/myapp/files 
       ^ daemon now reads the group perm, www-data is the owner

(With the upload process running as daemon:sharedgroup)

The above are one-time-only commands that need running; after which there is no need to modify the permissions for any file or folder to permit both www-data and daemon to manipulate files in www/myapp/files.

4
  • Thank you, everything is well explained, but my parent folder has 0777 permissions but I still can't delete the write-protected file. (and you were right, the file is write protected).
    – D.J.
    Feb 19, 2014 at 10:23
  • but my parent folder has 0777 sounds like you have a logic error or misunderstanding - you are not looking at the permissions of the folder that matters, or the error you've getting isn't the one you think it is. (show some code, the error message it generates, and any supportive evidence in the question).
    – AD7six
    Feb 19, 2014 at 10:44
  • drwxrwxrwx 2 daemon daemon 4096 Feb 2 13:54 97 so this is my parent folders permissions (folder name: 97 )
    – D.J.
    Feb 19, 2014 at 11:03
  • you aren't helping yourself (reread my previous comment please) - good luck ;).
    – AD7six
    Feb 19, 2014 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.