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

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>

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 do I run a C++ binary on OpenShift via 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.

Mounting a drive in debian from php code

I've been at this for two days now and haven't been able to find any way (good or bad) of doing that to work.
I have to be able of dynamically mounting drives over network from my website's pages (that part is inevitable).
I have no problems doing it directly on the console with the following command
mount -t cifs //IP-REMOTE-MACHINE/Folder -o username=username,password=password /mnt/share
Obviously trying to just do a shell_exec() of this command wouldn't work with no root rights.
I tried to shell_exec() a script in which I would switch to root user (via su or sudo mycommand) but both of them wouldn't work (never been able to succeed in doing a script who would automatically switch my user to root even with the root pwd hard coded (even if that feels an extremely bad idea I could have accepted that atm).
After that I tried to use pmountbut never found a way to access to a remote shared file (don't think it's even possible but I may have missed something here?)
All that is running on a Debian machine with apache2.
I have a wild idea...
You could set a cron to run as root that checks for mount commands from your script. The script would simply set a mount command to be processed, and when the cron gets to it, runs the mount, marks the command as processed, and writes to a log file which you could then display.
It's not safe to run sudo commands with www-data (the user for web servers in Debian).
But if you want to run sudo [command] in a php script, you must add the user www-data in sudoers: http://www.pendrivelinux.com/how-to-add-a-user-to-the-sudoers-list/
And then you can exec: sudo mount ...
EDIT: It's safer to add in visudo:
www-data ALL= NOPASSWD: /bin/mount
To allow www-data to use only sudo /bin/mount

PHP let www-data run a command as if it were a different user

So I want to execute the following command in my php script:
exec("/path/to/command");
Because it is the www-data user who runs php scripts, i currently can not run this command.
I have read something about suexec being able to run a command as if it was a different user. I find it rather difficult to understand how this works.
I have already installed suexec and edited the /etc/apache2/suexec/www-data file and added:
/home/user_to_run_command/script.php
I have also edited /etc/apache2/sites-enabled/000-default and added:
SuexecUserGroup user_to_run_command user_to_run_command
Am I missing anything?
suEXEC will work only when PHP is executed in CGI mode but not if PHP is running as an apache2
module. I guess you are running it as a module.
An alternative might be to transfer the ownership to the desired user and then set the suid bit:
chown desired_user your.program
chmod u+s your.program
Now when executing your.program it has permissions as if it where executed by it's owner. Follow the wiki article that I've linked for more information.
Side note: This will work with binaries only (not with shell scripts as they where executed by the shell binary which has no suid bit set)
I had the same problem and finally found a solution which as far a I can see is both safe and simple. A disadvantage of this method is that you have to take care of security updates when they are published.
What we are gonna do is make our own special shell which we chown and SUID to the user which we want the task to perform. To remain safe this user should be just an ordinary user without extensive system rights and place the script somewhere others are not allowed. Now we let php execute a script which uses this special shell and all command within this script will be executed as the chosen user.
In practice:
sudo mkdir /usr/lib/specialshell
sudo chown user_who_may_run_command:root /usr/lib/specialshell
sudo chmod 700 /usr/lib/specialshell
sudo cp /bin/perl specialperl
sudo chown user_to_run_command:usergroup_to_run_command specialperl
sudo u+s specialperl
sudo mv specialperl /usr/lib/specialshell
Now we make a script named command.script containing:
#!/usr/lib/specialshell/specialperl
$ENV{"PATH"} = "/usr/bin";
system("/path/to/command");
and from php code we use:
exec("/path/to/command.script");
et voila, no code change, just the name of command in php.
edit: works only with perl as shell, so changed bash to perl and put the shell somewhere safe

Executing a bash file from a php page with root-only commands (Ubuntu)

I need to execute a bash file from a php page, with exec() function. The problem is that in this bash file, there's the command "adduser" ... Witch is a sudo command. I had the idea of modifying the sudoers so the user that run the script would have access to it, but who is this user ? I know apache2 is executated with www-data user...
Thanks!
You can find out which user PHP is running as by using system to run the command 'whoami' and display the output.
system('whoami');
That seems like a rather bad plan, giving the www-user sudo access. But yes, its www-data (by default, depending on linux flavor) that apache runs under.

Categories