Is_writeable fails but I am the owner - php

I have the below code running and will give the CHMOD and CHOWN values below. But for some reason is_writable keeps failing.
if (!is_writeable($this->path)) {
echo 'Current script owner: ' . get_current_user();
echo '<br />';
echo $this->path;
echo '<br />';
print_r(posix_getpwuid(fileowner($this->path)));
}
The CHMOD values of the directory is 775 and the owner is User1. The output from above is
Current script owner: User1
path/to/directory
Array ( [name] => User1 [passwd] => x [uid] => 111 [gid] => 111 [gecos] => [dir] =>
/path/to/user [shell] => /bin/false )
The only thing that doesn't match is the owner / group of the file is 111/1 so the groups might be different but the owner is identical.
Why would is_writeable fail?

Are you the owner or the webserver?
Everything you execute with the webserver should run as www, _www or www-data (depending on the configuration; default values for different OS). So the webserver user is not in your group which causes that the file is not writeable by the webserver.
(P.s.: get_current_user() is the script owner (e.g. what you set by chown), not the script running user. Current script running user data: var_dump(posix_getpwuid(posix_getuid()));)

Related

Unable to execute system commands from PHP

I am trying to execute a couple of scripts by using a remote interface. The environment is Raspbian on a Raspberry Pi (although I will be using Debian later as well) running LAMP.
The files are test.php and test.sh in the root directory of the webserver (say example.com)
test.sh
#!/bin/bash
sudo pkill chromium-browse
sudo reboot
test.php
<?php
$output=null;
$resultCode=null;
exec("./test.sh", $output, $resultCode);
// $ouptut = shell_exec('./test.sh 2>&1'); //tried this too
// echo shell_exec("./test.sh"); // as well as this
echo "Returned with status $resultCode and output:\n";
print_r($output);
?>
Initially, I had used
chmod u+x test.sh
but got an error code of 126. So I did this:
chmod 777 test.sh
Now I get an error code of 1, but it still doesn't execute. I have also tried
sudo visudo
then added
pi ALL=(ALL) NOPASSWD: ALL
(pi is the current loggedin user)
Currently I am getting this:
Array
(
[0] =>
[1] => We trust you have received the usual lecture from the local System
[2] => Administrator. It usually boils down to these three things:
[3] =>
[4] => #1) Respect the privacy of others.
[5] => #2) Think before you type.
[6] => #3) With great power comes great responsibility.
[7] =>
[8] => sudo: no tty present and no askpass program specified
)
Note: I use sudo all the time at the command line without being asked for a password.
I do have another php file in the same directory that executes an actual system command successfully. It has this line:
$uptime = exec("uptime");
which works just fine, so I know system commands are possible. Is there any way to do this? I have seen other similar questions on SO and other sites, but none of those answers have worked for me.
Any help appreciated.

PHP Exec sudo mount throwing fancy error message

I'm using Centos7 OS, I have this script that mount a folder from windows PC
exec('sudo mount -t cifs -o username="'.$user.'",password="'.$pass.'",dir_mode=0777,file_mode=0777
//'.$ip_address.'/c/kitpos/update /mnt 2>&1', $output, $results);
And I got this fancy error message that looks like a permission issue.
Array
(
[0] =>
[1] => We trust you have received the usual lecture from the local System
[2] => Administrator. It usually boils down to these three things:
[3] =>
[4] => #1) Respect the privacy of others.
[5] => #2) Think before you type.
[6] => #3) With great power comes great responsibility.
[7] =>
[8] => sudo: no tty present and no askpass program specified
)
My /mnt folder ownership and permission is root and 777 but even if I change it www-data:www-data which is my php running as, still got the same error.
777 drwxrwxrwx 2 root root 4096 mnt
I also have an Ubuntu server but it works fine in their with that same code. Does anyone have an Idea?

PHP doesnt have permission to change file name

I want to change file name in subdirectory under /usr/local/MyFolder/overlay.png to overlay2.png with php, but php raises Permission below Error:
( [type] => 2 [message] => rename(/usr/local/MyFolder/content/overlay.png,/usr/local/MyFolder/overlay2.png): Permission denied [file] => /var/www/html/2.php [line] => 6 )
How to solve it?
RUn the following command from Terminal:
You can get to the terminal by Pressing Ctrl+alt+T on your keyboard
Run the following code:
sudo chmod 777 "/usr/local/MyFolder/"
sudo chmod 777 "/usr/local/MyFolder/content/"
Now your script should have permissions to access those folders
Mind you, doing these gives permission to everyone and every programs to access and modify things in the folders above

"Warning: mkdir(): Permission denied in" while running as owner of parent

I'm running this script:
$dir = 'images';
if (!file_exists($dir) ) {
mkdir ($dir, 0644);
}
It returns this error:
Warning: mkdir(): Permission denied in
/opt/bitnami/apache2/htdocs/file.php on line xx.
Apperently i do not have the rights to create the folder. I found the following:
htdocs has chmod set to 755
The owner of htdocs is daemon (lrwxrwxrwx 1 daemon daemon 27 Aug 1 12:16 htdocs -> /opt/bitnami/apache2/htdocs)
I created a file with php while temp. using chmod 777 and then used this script: https://stackoverflow.com/a/7771686/1139465 to figure out the owner, that was daemon (Array ( [name] => daemon [passwd] => x [uid] => 1 [gid] => 1 [gecos] => daemon [dir] => /usr/sbin [shell] => /bin/sh ) done)
I have an intallation of the Bitnami LAMP solution.

php mkdir() permissions denied mac

This seems to be a common problem, but I can't seem to find the exact answer I'm looking for.
I'm running XAMPP on OSX, and in my web app am trying to allow an admin to create a directory if none exists for a new year. So, under htdocs/mywebapp/images, if a directory doesn't exist for, say, 2012, then mkdir() and begin uploading files to that directory.
Running ls -l from the mywebapp directory shows that the images directory is created as
drwxr-xr-x 18 myusername admin 612 Feb 12 17:32 images
so the first thing I tried was
sudo chmod 0775 images
which didn't help. Looking at the httpd.config file shows that the user/group is defined as www:www so I tried changing the owner/group to that:
sudo chown www:www images
which results in, as expected:
drwxrwxr-x 18 _www _www 612 Feb 12 17:32 images
but, again, I get the Permission denied error. So, I'm at a loss as to what my next step is.
The php code I'm using to get to this point is here:
$directory_self = dirname(__FILE__);
$base_image_path = $directory_self . '/images/'; // outputs "/Applications/XAMPP/xamppfiles/htdocs/mywebapp/images/"
$year = '2012';
$image_path = $base_image_path . $year . '/'; // outputs "/Applications/XAMPP/xamppfiles/htdocs/mywebapp/images/2012/"
if(!file_exists($image_path)){
mkdir($image_path, 0775);
}
Thanks for your help.
try to check real process owner:
$processUser = posix_getpwuid(posix_geteuid());
echo($processUser['name']);

Categories