Execute qpdf in php shell_execute - php

I have installed Lampp-x64-5.6.3 in my OpenSuse 13.2 OS. I have built a program which require the execution of qpdf which I have installed from the OpenSuse Repo itself.
Well when I run the commands (given below) I get no response & nothing works at all whereas I am able to execute other binary files within the /usr/bin/ directory.
$execQuery = "/usr/bin/qpdf --decrypt --stream-data=uncompress --force-version=1.4 ".escapeshellarg('/opt/lampp/htdocs/test/test.pdf')." ". escapeshellarg('/opt/lampp/htdocs/test/temptest.pdf');
shell_exec($execQuery);
#OR
$execQuery = "/usr/bin/qpdf '/opt/lampp/htdocs/test/test.pdf' '/opt/lampp/htdocs/test/temptest.pdf'";
shell_exec($execQuery);
PHP safe_mode is off, shell_exec, exec, system etc are enabled. Still I am unable to run this particular binary (/usr/bin/qpdf).
I am getting output when I run the commands echo or ls -l or dir or even skype in the php shell_execute functions.
The permission for the file is: -rwxr-xr-x 1 root root 85248 Jun 18 10:31 /usr/bin/qpdf
But however I am able to execute qpdf command via Terminal of the OS. and it creates the file perfectly.
The directory /opt/lampp/htdocs/test/ is writable by both qpdf and apache/lampp
I have tried almost all methods mentioned in various forums but still can't get this executable run the file.
Thanks in advance.
UPDATE:
As suggested tried out this one:
$command = "/usr/bin/qpdf --decrypt --stream-data=uncompress --force-version=1.4 ".escapeshellarg('/opt/lampp/htdocs/test/test.pdf')." ". escapeshellarg('/opt/lampp/htdocs/test/temptest.pdf');
shell_exec($command. " > /opt/lampp/htdocs/debug.log 2>&1");
The errors are logged!
......
/opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.9' not found
......
SOLUTION:
I simply had to delete the /usr/lib/libstdc++.so.6 file or rename it.
RUN in terminals:
sudo mv /usr/lib/libstdc++.so.6 /usr/lib/libstdc++.so.6___

I simply had to delete the /usr/lib/libstdc++.so.6 file or rename it.
RUN in terminals:
sudo mv /usr/lib/libstdc++.so.6 /usr/lib/libstdc++.so.6___

Related

Using exec() in php via web

Hello i was trying to use exec() in php via web page to execute a file in the root directory, and for some reason none of my commands work except
ls
witch even when i do
ls /root
it doesn't work seems i can only do ls in the current directory or commands like whoami which returns apache
i've tried setting user permissions for apache, and i've tried setting permissions for file or /var/www/html directory and nothing seems to work any ideas?, my basic code below
<?php
command = "ls /root";
exec($command);
?>
The exec command return only the last line of your command.
Like said in the documentation, you should add a second parameter to get the result or use the function passthru

Run bash script from PHP script

I am running an Apache server on CentOS and need to run some bash scripts from PHP page. Running commands which do NOT need write or execute permission from PHP file works fine (for example shell_exec('ls /var/www/html/scripts/')), but I have problem running the commands that do need write or execute permission. For example this commands does nothing:
<?php
$output = shell_exec('/var/www/html/scripts/test.sh');
echo $output;
?>
I made apache user owner and granted necessary permissions to scripts directory:
drwxr-xr-x. 2 apache apache 21 Jun 3 09:54 scripts
and test.sh file as can be seen, but there was no lock.
-rwxr-xr-x. 1 apache apache 51 Jun 3 09:54 test.sh
I also tried to sudo the command in PHP file and added the line below to the end of Sudoers file, but nothing changed.
apache ALL=NOPASSWD: /var/www/html/scripts/test.sh
Also I checked PHP safe_mode which is off and there is no restriction in php.ini file:
disable_functions =
Your kind help would be highly appreciated.
NOTE:
I edited my bash script and added sudo like below:
#!/bin/bash
echo "Hi from test.sh";
sudo touch /var/www/html/scripts/file.log;
Now when I run the file as apache user using this command, it runs successfully:
su -s /bin/sh apache -c "/var/www/html/scripts/test.sh"
But through the php web page it only runs echo "Hi from test.sh"; line. When I check logs, there are lines below for running command above:
su: pam_unix(su:session): session opened for user apache by root(uid=0)
sudo: apache : TTY=unknown ; PWD=/var/www/html ; USER=root ; COMMAND=/bin/touch fromweb.log
su: pam_unix(su:session): session closed for user apache
And the generated log when running from php web page:
sudo: apache : TTY=unknown ; PWD=/var/www/html/scripts ; USER=root ; COMMAND=/bin/touch fromweb.log
Missing pam_unix(su:session) open and close.
I found what the issue was. It was because of Linux SELinux feature. This feature applies a least-privilege policy and denies any unnecessary command from running on Linux. The bash script is running successfully after disabling this feature. To do so, edit the file /etc/selinux/config and change SELINUX=enforcing to SELINUX=disabled and reboot the system. THIS IS NOT RECOMMENDED FOR SECURITY REASONS, however. You may check the link below to only create some exceptions rather than completely disabling SELinux.
https://wiki.centos.org/HowTos/SELinux

Not able to run wkhtmltopdf commad through shell_exec() function in php but same command works on command line

I'm in trouble and that much confused about a php shell_exec command.
When the command is execute by PHP I have no error but the execution fails. If I use exactly the same command from a terminal it works.
Here's the command :
/usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyxa9otq.html" "/tmp/knplabs_snappyv3pD7h.pdf"
When I lauch this from a terminal :
$ /usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyWG9XTd.html" "/tmp/knplabs_snappyv3pD7h.pdf"
Loading page (1/2)
Printing pages (2/2)
Done
But from my php script :
// Construct the previous command
$command = $this->buildCommand($url, $path);
../..
shell_exec($command);
../..
$content = file_get_contents($path);
../..
I've test the output of shell_exec, it's empty.
The log :
Warning: file_get_contents(/tmp/knplabs_snappyv3pD7h.pdf): failed to open stream: No such file or directory in /*****/lib/snappy/SnappyMedia.class.php on line 64
No permission pb in the /tmp directory :
$ ls -la /tmp
total 448
drwxrwxrwt 16 root root 4096 mars 12 21:51 .
../..
I've tried avec the PHP exec() function to get error informations, I just get an "1" error code in return_var and nothing in output.
For information this issue appear on my test server, my desktop computer but not on my notebook. All the 3 are with sames PHP, Apache, Mysql versions.
I don't understand anything ...
Thanks for any help, I'm loosing my mind.
David.
I've found the solution here : Executing wkhtmltopdf from PHP fails
Thanks to Krzychu.
First to get information from the shell_exec command add " 2>&1" at the end of the command. In that way you will get information in return of the command :
$no_output = shell_exec($command);
echo $no_output; // nothing
$output = shell_exec($command . ' 2>&1');
echo $output; // in my case : "cannot connect to X server"
The solution :
Not use the wkhtmltopdf ubuntu package (0.9.9-4)
Use the official package from the Wkhtmltopdf download page
So no need to install xvfb ! (I've seen this advice many times)
Looks like a user's permissions issue.
When you run the command from the terminal, it is the user account, currently used, which does have the right permissions, to run a command in /usr/bin, and execute the specific file.
When you run it from the php script, it is the http server account on your system, which needs the permission to execute the file in /usr/bin. Usually this is the apache user.
How you should setup permissions depends on your system. Just remember that what is allowed for apache, is allowed for anyone accessing your http server.
I have had this problem for ages and adding . ' 2>&1' after the $command has somehow solved the problem.
this:
$output = shell_exec($command . ' 2>&1');
instead of:
$output = shell_exec($command);
No idea why but it works and I'm grateful.
Is it a shared hosting? It seems like shell_exec is a restricted function. Try running error_reporting(E_ALL); ini_set('display_errors', 1); before calling shell_exec.
I stumbled upon the same Problem, in my case an absolut Path in the exec Command like /var/www did not work, I had to use relative Paths from the point where I executed the php File.
I also wanted to notice, that it did not work using shell_exec, however it worked using normal exec command, not sure wheres the difference here.

How to access root packages through php?

Issue: Initially, Through command line as a root user,I accessed a package called pandoc (/root/.cabal/bin/pandoc) which was installed in root folder. When I try to access that package through php using shell_exec(),it fails.
Question: Is there any limitation for php shell_exec() not to access root packages for security purposes? If so,how to resolve it?
I tried: Gave write permission to root folder then I could access root packages through
command line not as a root user. yet I couldn't to access it through php shell_exec().
php code:
shell_exec("cd /home/quotequadsco/public_html/pandoc_jats ; sudo -u quotequadsco
-S /root/.cabal/bin/pandoc ex.tex --filter /root/.cabal/bin/pandoc-citeproc
-t JATS.lua -o ex.xml");
and also tried,
shell_exec("cd /home/quotequadsco/public_html/pandoc_jats ;/root/.cabal/bin/pandoc
ex.tex --filter /root/.cabal/bin/pandoc-citeproc -t JATS.lua -o ex.xml");
Expectation: I need to execute pandoc root package through shell_exec() in php.
Added the following line in the /etc/sudoer file
#Defaults requiretty //commented this line
usergroup ALL=(ALL) ALL
PHP code,
shell_exec("cd /home/quotequadsco/public_html/pandoc_jats ;echo password | sudo
-S command"); //added a password for sudo command to run as a root user.
I recently published a project that allows PHP to obtain and interact with a real Bash shell (as root if requested), it solves the limitations of exec() and shell_exec(). Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('pandoc (/root/.cabal/bin/pandoc)');
//the return will be a string containing the return of the command
echo $return1;

Executing .exe file using PHP on Linux server

I'm new in using Linux, I'm trying to write a PHP code which can run .exe linux compatible file, I've made a short shell script
hello bash script:
#!/bin/bash
./program.exe file.mp4 // file.mp4 is an an input for .exe
echo "Hello World!"
shell.php:
<?php
$output = exec ("./hello ");
echo "<pre>$output</pre>";
?>
Now when I run shell.php using web browser it shows Hello World! but the .exe doesn't run, however when I run php using terminal command php shell.php, It works fine.
I think I'm having problems with permissions but I'm new with Linux and I don't know how to solve this.
Update:
I ignored the shell script and I used
<?php
$output = shell_exec ("cd /var/www/ && ./program.exe file.mp4 2>& " );
?>
also I granted access to program.exe
chmod 777 program.exe
the error I receive in the browser :could not open debug.bin!
use the absolute path to hello executable exec("sh path/to/the/file")
I'm using something similar to call an app compiled with mono on a remote ubuntu webserver and return it's output to the calling script.
For any of this to work properly wine needs to be already installed.
On Ubuntu systems try:
sudo apt-get -y install wine
You then need to know the owner of the web server process. If you are running the apache web server try the following:
cat /etc/apache2/envvars | grep "RUN"
The output will look something like this:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
Now that you have the name of the process owner, which in this case is www-data you should ensure the file is owned the user and its group:
sudo chown www-data /var/www/program.exe
sudo chgrp www-data /var/www/program.exe
Finally, we can invoke the application from inside our PHP script by passsing it as a parameter to 'wine' and using its full file path.
<?php
$output = shell_exec("wine /var/www/program.exe file.mp4" );
?>
Any output from the above shell command sent to the command line will be saved in the PHP script variable $output.
It looks like you are trying to do some output redirection with your use of program.exe file.mp4 2>& so I've left that off of the example for clairity.
Try using the absolute path, such as exec("sh /path/to/file")
Generally, php is run as www or apache, so make sure that the execute access permission is granted to all user.

Categories