I have a CentOS 5.7 linux server and use php5.3.x.
On a pfSense system, you can restart services-that required root permissions using a php web page.
I'm trying to do something similar, I have written some php code to execute shell commands. For example, to restart the sshd service:
<?php
exec('/sbin/service sshd restart');
?>
and I tried to execute that command via exec function, but it needs root permission, but we have a apache user authority.
I have come across a few solutions:
- "run apache with root user" really unsafe. I do not want to do that.
- "apache ALL=NOPASSWD:/sbin/service to /etc/sudoers" I tried but and still have a problem.
Any other solutions? Thanks for answers.
now.. it's interesting. i tried @refp post and it worked my local ubuntu server. But when i tried same at my cenOS vps server. It's not working.and that is apache's error log "rm: cannot remove `/var/lock/subsys/vsftpd': Permission denied"
Allow the www-data user to run to run program1 and program2 with no password:
Add to the contents of the sudoers file:
Save.
from https://askubuntu.com/questions/76920/call-a-shell-script-from-php-run-as-root
Read this whole post before trying it out, there are choices to be made.
Solution using a binary wrapper (with suid bit)
1) Create a script (preferrably
.sh
) that contains what you want to be ran as root.2) This file should be owned by root, and since it will later run with root permissions make sure that only root has permission to write to the file.
3) To run the script as root no matter what user that executes it, we will need a binary wrapper. Create one that will execute our
php_shell.sh
.4) Compile and set proper permissions, including the suid bit (saying that it should run with root privileges):
php_root
will now run with root permissions, and execute the commands specified inphp_shell.sh
.If you don't need to the option to easily change what commands that will be executed I'd recommend you to write the commands directly in
wrapper.c
under step 4. Then you don't need to have a binary executing a external script executing the commands in question.In
wrapper.c
, usesystem ("your shell command here");
to specify what commands you'd like to execute.I recently published a project that allows PHP to obtain and interact with a real Bash shell, it will easily give you a shell logged in as root. Then you can just execute the individual bash commands rather than bundling then in a script. That way you can also handle the return. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
I would not have PHP execute any sudo commands. To me that sounds like asking for trouble. Instead I would create two separate systems.
The first system, in PHP (the web tier), would handle user requests. When a request is made that needs a sudo command I would place this request in some queue. This could be a database of some sort or middle-ware such as ZeroMQ.
The second system (the business tier) would read or receive messages from this queue and would have the ability to execute sudo commands but won't be in the scope of your web-server process.
I know this is a bit vague and it can be solved in different ways with various technologies but I think this is the best and safest way to go.
Solution using a binary wrapper (with suid bit) Some modification of Filip Roséen - refp post.
To execute any command modified wrapper.c
Compile and set proper permissions;
Now call from PHP. Execute any command.