How do I run a C++ binary on OpenShift via PHP? - php

I SSH'd into my OpenShift application and compiled the C++ file using gcc and then downloaded it to my computer (for backup). I added it to git repo and pushed it.
How do I execute it in the current directory? I have tried changing permissions to 777 using chmod. I've tried exec(), shell_exec(), passthru(), system() in PHP with no luck. None of them gives me the output of the program.
Commands I used
Compiling C++: gcc code.cpp -o code.out
Inside run.php: chmod 777 code.out && ./code.out input-file (also tried chmod("code.out", 777);)
input-file is also pushed together with the code.out in same directory.
After a bit of testing, I found that it returns code 126 which is Permission problem or command is not an executable, but the permissions is 777 and it is in fact an executable.
Am I missing something?
(I'm sorry, but I don't have any experience with this)

Change the permission of the file so all can execute.
chmod a+rwx file
I prefer to use this instead of using 0777. That depends on your preference though.

Related

Wget Linux command is downloading files with Read Permissions for only sudo users - how to give Read permissions to all users?

I am running Ubuntu 16.04 and I am programming in PHP. Here is my problem:
I have a need to use the wget linux command to download all files from a website. So I am using PHP's shell_exec() function to execute the command:
$wgetCommand = "wget --recursive --page-requisites --convert-links $url 2>&1";
$wgetCommandOutput = shell_exec($wgetCommand);
This command downloads all files and folders from a website, recursively, to the current working directory (unless otherwise specified). The problem is that when these files and folders are downloaded, I do not have permissions to read them programmatically after that.
But if I do something like sudo chmod 777 -R path/to/downloaded/website/directory after they are downloaded with the wget command and before they are read programmatically, they are read just fine and everything works.
So I need a way to download folders and files from a website using wget command and they should have read permissions for all users, not just sudo.
How can I achieve that?
This sounds like a umask issue with the user running the PHP script.
Normally, Ubuntu would have a default umask of 0002. This would create a file with (-rw-rw-r--).
From the console you can check and set the umask for the PHP user via:
$umask
And inside the PHP script, do a
<?php
umask()
If you are on a running webserver, it would, however be better to alter the files permissions of the downloaded files afterwards, via
<?php
chmod()
The reason is, that the umask handles file creation for all files - not just your script.

How to execute binary with sudo on Raspberry via PHP exec()?

I used this tutorial (sorry, it's German) to switch my 433MHz sockets:
https://tutorials-raspberrypi.de/raspberry-pi-funksteckdosen-433-mhz-steuern/
I compiled a file that switches the sockets on and off by RC code. If I run it directly on the Pi Shell it works fine:
sudo /var/www/html/bin/RPiControl -3313
but if I run it via exec() on my PHP script, it does not:
exec('sudo /var/www/html/bin/RPiControl -3313', $output, $return);
Here's what I tried so far:
There is no return/output value
I'm using lighttpd as webserver on Raspi 3 with default Raspian
The script is located at /var/www/html
The binary is located at /var/www/html/bin (also tried the home directory)
The webserver/php seems to run under the default user "pi" (I'm wondering, on my other linux machines it used to be www-data user)
I tried to gave sudo permissions to the "pi" user (tried www-data as well)
I made the "Pi" User owner of the PHP script(s) and the binary
I chmodded the PHP scripts with 777
I already tried this: sudo in php exec()
I guess it's a permission issue to use "sudo" with PHP execute. If I try sudo la la it's not working as well.
How can I allow the binary to be executed without sudo, or allow PHP to use sudo?
Thanks in advance.
Is there a way to allow the binary to run without sudo?
I tried setting the SUID Bit (chmod u+s) bit it didn't work as well
If you set the SUID bit the executable runs with the same right of the user that owns the executable.
So if the executable file is owned by user hello the executable will run with the access rights of the user hello and if the file is owned by the administrator (root) it runs with administrator rights.
Therefore you first have to change the owner of the executable file before you set the SUID bit (if the SUID bit is already set it will be removed and must be set again):
sudo chown root:root /some/file/name
sudo chmod u+s /some/file/name
If the executable calls another executable (it starts other executable files using exec) the other executable will by default not be executed with changed access rights.
For this reason you cannot use the SUID bit for shell scripts...
This behaviour can be changed using the following line of code in the source code of the file which has the SUID bit set (if the program is written in C or C++):
setreuid(geteuid(), geteuid());
(Which requires the following header #include line:)
#include <unistd.h>

Failed to call dwebp in PHP

I have installed webp package in my server and i want to use "dwebp" command in my PHP application.
My command is simple, it is like
dwebp "/full/path/test.webp" -o "/full/path/test.png"
If i run it from SSH terminal, it is 100% succesful.
But that command can't be run from PHP.
I have tried to use system(), passthru(), exec(). All failed which means the PNG file is not created.
However these commands succesfully called from PHP :
touch. If i use this, the created file is under "apache" owner.
ls -la
I have tried to change the directory permission from 777 / 775 / 755.
Using passthru, the result from calling from PHP is empty string.
In php.ini, disable_functions is empty
No error message, error_reporting is on.

exec uglifyJS with php

I try to exec a simple cmd (which works on my terminal server), but not in my php script.
I use UglifyJS2.
It install in my /home/MY_USER/node_modules/uglify-js/
I have chmod the symlink like here: calling node and uglifyjs from Php context.
But
exec(/usr/bin/node /usr/bin/uglifyjs ...);
or
exec(/usr/bin/uglifyjs ...);
doesn't work.
I haven't chmod my /home/ of course, but it's maybe the reason why doesn't works?
How can I do exec uglify by my php?
After fixing the syntax errors the apache (or web server) user will need exec permissions for the dir and bin. Why not install it in /usr/bin?

Creating subversion shell script on Centos

I have subversion installed on CentOs 6.4 and want to write a script (from my understanding a shell script) to run a couple of commands. My issue here is not writing the shell script but more providing a parameter to the shell script (so a function in a way) to be able to complete the request.
In essence I want to do the following:
Run script with parameter from SSH ("somscript reponame")
Create repo: svnadmin create /var/www/svn/reponame
Change repo owner: chown -R apache.apache /var/www/svn/reponame -R
Do security changes: chcon -R -t httpd_sys_content_t /var/www/svn/reponame/
And chcon -R -t httpd_sys_rw_content_r /var/www/svn/reponame
Create default directories: svn import -m 'Initial import' /tmp/svn-structure-template/ http://domain.com/svn/reponame/ (localhost is not accepted by stackoverflow)
Can anyone offer some guidance or perhaps provide an alternative I can use? Would a PHP script work (so to run it from a browser and use a query string of some sort and would this not cause some security issues as apache is the default owner and some of these may require root / sudo access).
Thank you in advance!
As Fausto said in the comment, standard Bash parameters should work fine. At ProjectLocker, we use scripts similar to what you're describing to provision new Subversion repositories, and you should just be able to reference "$1", "$2", and so on in the script.
Incidentally, you don't have to import to the http:// location if you're running on the machine with the instance, if that makes things harder. You can do:
svn import -m 'Initial import' /tmp/svn-structure-template/ file:///var/www/svn/reponame
although I'd recommend testing that first to make sure that doesn't cause an undesired permissions change. If it does, you can simply run it before the apache permission flip and the lockdown.

Categories