PHP Exec sudo mount throwing fancy error message - php

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?

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.

exec() command in PHP file reporting process not found

I'm trying to run this command within a PHP file on a localhost webpage that runs on apache and PHP:
echo exec("/usr/bin/killall mplayer 2>&1", $result);
But it does not work, this command print_r($result); gives this output:
mplayer: no process found
Array ( [0] => mplayer(5003): Operation not permitted [1] => mplayer(5004): Operation not permitted [2] => mplayer: no process found )
Things I have tried are adding this to the /etc/sudoers
www-data ALL=(ALL:ALL) ALL
%www-data ALL=(ALL:ALL) ALL
This is a device that runs on an internal network in my home, so not a security risk.
I'm pretty sure it is a file/permission/ownership issue, but not sure what to do to allow it to run.
Any ideas? Thanks.

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

Is_writeable fails but I am the owner

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()));)

exec doesn't found a file

I'm having problems with the exec php function.
When I do:
ls -al /dev/sdf1
This doesn't found the archive (ls: cannot access /dev/sdf1: No such file or directory), but if I run this command, in console that show me the information.
What can I do?
That happen even running the php script as a root!!
$mountcommand = "ls -al /dev/$unit 2>&1";
exec("$mountcommand", &$mountoutput, &$mountresult);
print_r($mountoutput);
echo "\n$mountcommand\n\n";
This is in a forked process
Works for me.
The disks:
frayser#gentoo ~/doc/Answers/src/PHP $ disk
sda 111.790 GB
sdb 233.762 GB
sdc 233.762 GB
sdd 233.762 GB
sde 279.481 GB
------------------
Total: 1092.558 GB
Using hde:
frayser#gentoo ~/doc/Answers/src/PHP $ l /dev/sde
brw-rw---- 1 root disk 8, 64 Dec 2 13:36 /dev/sde
Using PHP:
frayser#gentoo ~/doc/Answers/src/PHP $ php lsdev.php
Z: brw-rw---- 1 root disk 8, 64 Dec 2 13:36 /dev/sde
The script:
frayser#gentoo ~/doc/Answers/src/PHP $ cat lsdev.php
<?php
$z=exec("ls -lr /dev/sde");
print "Z: " . $z . "\n";
?>
Update
It also works with ls -al.
Please show your script.
Update 2
Along the lines of the chroot suggestion by ircmaxell: Supposing that /dev, as seen in the PHP process, was a special chroot facsimile of the real one. And supposing that /dev/sdf1 is a removable device. The when sdf1(the media) is inserted the system automatically creates the device in the real /dev; but the chroot version isn't updated. So, to detect this situation look for differences between /dev as seen from PHP and from the commandline.
One test is ls -id on /dev(from PHP and the commandline). This prints the inode of /dev. Is there a mismatch in inode numbers?
Is /dev/sdf1 removable? Is it mounted? If it is mounted; does the PHP process see the mounted filesystem: ls $mount_point from PHP it.
There are other chroot tests listed on Stack Overflow and elsewhere; but I haven found many good ones: If a chroot is done correctly, it is hard to detect.
Update 3
This may be what is happening: The device (/dev/sdf1) takes a while to appear after it is created; so it is necessary to pause between creating the device and attempting to mount it:
Before: The device is created; but it isn't there...
Array
(
[0] => Logging out of session [sid: 4, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
[1] => Logout of [sid: 4, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
[2] => Logging in to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
[3] => Login to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
[4] => ls: cannot access /dev/sdg: No such file or directory
)
{ test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u; sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l; ls -al /dev/sdg ;} 2>&1
After: Added a 1-second pause, and the device (sdg) is available...
Array
(
[0] => Logging out of session [sid: 5, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
[1] => Logout of [sid: 5, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
[2] => Logging in to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
[3] => Login to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
[4] => brw-rw---- 1 root disk 8, 96 Dec 18 05:27 /dev/sdg
)
{ test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u; sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l; sleep 1; ls -al /dev/sdg ;} 2>&1
The PHP with the pause ...
<pre>
<?php
$unit='sdg';
$reset="test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u";
$connect="sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l";
$test="ls -al /dev/$unit";
$mountcommand = "{ $reset; $connect; sleep 1; $test ;} 2>&1";
exec("$mountcommand", $mountoutput, $mountresult);
print_r($mountoutput);
echo "\n$mountcommand\n\n";
?>
</pre>
The /etc/sudoers is configured the same as reported in the previous question about mount failing from PHP.
So add a second or two of sleep() after iscsiadm creates the device. The sleep can be done in PHP instead of in the exec() of the shell.
I fix it with a sleep between this code and the code that gave me the "unit"

Categories