File permission problems with PHP - php

I am trying to create a folder and fopen a file inside that folder.
mkdir works fine for creating the folder (as the parent folder has 777 permission), but the resulting folder is owned by user id 99 while the script itself is running under cpanel with user id 32024. Now when I try to create a file inside this folder, I get:
SAFE MODE Restriction in effect. The script whose uid is 32024 is not allowed to access /<path_of_file>/<folder_created_by_php> owned by uid 99
This error is fully justifiable as I am trying to write to a folder owned by someone else but how can a script running under 32024 create a folder with owner as 99? Anything wrong with the way server is behaving? Any ideas?

Your problem is related to the safe mode setting on php:
Check this out for more information: http://www.serverschool.com/dedicated-servers/what-is-php-safe-mode/ You'll have all the information to understand what is safe mode actually...
Just ask for your developers NOT to use safe_mode as it is a features unrecommended on all servers these days...

You can try to include both users 99 and 32024 to the same group and chmod('yourfilename', 0775) after you create that file

Related

PHP - Grant a user permissions to file owned by root in ubuntu

I am using Jasperreports for generating the reports. When I am generating the new reports it will be own by root with permission of 644. So other users dont have permission to view this report.I want to change the ownership of the file or change the permission.So everyone can view or download the reports.
I tried below php functions
chmod($item, 0777);
chown($path, 'www-data');
It gives
error: dont have permission to do this
. Because its own by root and current user is www-data.
Anyone please help me,
Actually, based on what you're saying, all users have permissions to view that file. 644 means owner can read and write, and group and others can only read. If your script is getting an error reading that file, it might be because of the permissions of the directories in is path, but not the file itself.
If you could change the owner or permissions of a file owned by root like that, it would subvert the whole concept of unix file permissions. Think about it.
You can always change the user running these reports though, or add logic on the report generation side to move or change the permissions on the file as the user who owns it.
As an aside, chmod 777 is an ugly kludge used only by those who have little knowledge of unix permissions . Professionals don't do it. You should bump your understanding of unix file permissions to the next level:
https://www.tutorialspoint.com/unix/unix-file-permission.htm looks promising.
According to the manual, the owner and the supersuer have the right to do this.
And you only chage the file mod or owner, will not do. You have also to change the path.
chown
Attempts to change the owner of the file filename to user user. Only
the superuser may change the owner of a file.
Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.
Note: When safe mode is enabled, PHP checks whether the files or directories being operated upon have the same UID (owner) as the
script that is being executed.
chmod
Attempts to change the mode of the specified file to that given in
mode. Note: The current user is the user under which PHP runs. It is
probably not the same user you use for normal shell or FTP access. The
mode can be changed only by user who owns the file on most systems.
Note: This function will not work on remote files as the file to be
examined must be accessible via the server's filesystem.
Note: When safe mode is enabled, PHP checks whether the files or
directories you are about to operate on have the same UID (owner) as
the script that is being executed. In addition, you cannot set the
SUID, SGID and sticky bits.

LAMP's is_file returns false for files in symlinked directory

My problem is: I have a huge data directory (400GB) from a website and have a second website enabled for the new design, where I have symlinked the data directory.
Now there are some file checks using is_file(), but they return false and if remove the file check, it renders the background-image for example, I can also access all the files in the browser via file listing using the non-original URL.
I've searched trough google, found some things about permissions, users, and groups. so I tried to get the user and group of the PHP-process using
$processUser = posix_getpwuid(posix_geteuid());
the whole thing is running on an ispconfig3 server and both sites are on the same server.
The user and group of the original, hard directory are web1:client1.
the user and group of the web directory with the symlink are web14:client1.
The running process is from user web14, which has its permissions set to 775 for the parent directory, where the symlink remains, and the original directory.
As I understand both users are in the same group and group has RWX or what could be the problem?
So, I solved the problem. My fault was that I was searching for errors from bottom layer to the top, cause I thought it was a permissions problem.
After many try and fails I found the not very obvious "option" panel in ISPConfig, where you could set some openbase-dir values for the clients webspace, which was chaining php to the directory. Now I just added the "main" webspace holding the 500GB data directory to the "child" webspace with the symlinked directory and its working like a charm.
Warning: my PHP was writing out with umask(022), so the group was only able to read, if you write out on the child "webspace" you have to set the umask to (002), so group is allowed to write to.

What permissions are required to make a directory writable by php?

What permissions do I need to set up on a directory in order to make it writable by php?
By "writable", I mean copying and creation of new files within that directory automatically by php itself.
I'm testing this on a free host, and the default permissions are 755.
When I try executing a php script, that attempts to create another subfolder of that directory, and copy certain files in it, and it fails.
If I set it up to 777, it works fine, but I assume that doesn't work on all Apache versions because of security reasons?
Also, when creating new files, does php act as the "owner"?
Whatever process that runs the PHP interpreter should should have a user account associated with it. Only that user needs write permission in the directory. So to answer your last question, it's usually www-data or apache that is the owner of that file.
Permission of 777 will work because it allows everyone to read, write and execute that directory but depending on your application this might be a security hole.

What is the most secure way to create upload directories?

I have an image upload script. I ran into some trouble with permission errors so for the last little while the upload directory has had permissions 0777. Dangerous, I know.
For some reason, it was the only permission that would allow the files to upload. I have now realised that the reason a safer permission didn't work was because of the owner of the directory.
I've been creating my upload directories using FTP. I thought this would be okay. But from what I understand FTP and HTTP aren't in the same group?
I've started creating the directories using PHPs mkdir() allows me to set a safer permission that works with my script.
But before I possibly get into another bad habit. Can someone please confirm that this is the correct way to do it? Is there a better way?
The owner of the directory should be the user which runs your PHP script - on Ubuntu this would be www-data. Shortly, creating folders with PHP mkdir() is okay. Then you should set permissions. 0700 is the most secure but if other user needs to read from or write to this directory, you should add this user to the main group of user which runs your PHP script and set permissions to 0750 or 0770 respectively. On Ubuntu this group is also www-data.

Safe mode.. mkdir

I have a script that creates a directory "createddir" and sets the chmod to 777 (just for this test really..)
I then want to create subfolders, but then the script fails. because of safe mode.
Why was I able to create that first folder then? Is it because the folder now has different perms different from the gallery.php file which was uploaded via ftp?
Is there any way around this? This is a script for sale, I cannot ask people to simply disable safe mode as their host may not allow that.
Warning: mkdir() [function.mkdir]: SAFE MODE Restriction in effect.
The script whose uid is 10005 is not allowed to access
/var/www/vhosts/yyy/httpdocs/zzz/files/createddir owned by uid 33 in
/var/www/vhosts/yyy/httpdocs/zzz/files/gallery.php on line 254
there is no way to solve it.
Either turn safe mode off or create these folders from FTP client
As i can see and understand you don't have access to that folder. Try chown.
For help: man chown
EDIT:
I did not tested this:
http://www.php.net/manual/en/function.chown.php
Take a look.

Categories