Entry
How can I execute local programs as root from PHP? I want to use UNIX commands adduser and passwd.
Mar 1st, 2008 06:43
dman, K Muma, Jon Bjorseth, Matt Rudderham, http://www.ttnr.org
executed commands from PHP is ran as the web-server (like httpd e.g.)
You'd prolly have to make your web-server run as root to be able to run
commands as root
Or you could setup a cron job running as root to check a file every so
often and do the updates from there.
You can also make an executable with the user set to root and the s-bit
set (which will allow the program to run with root permissions).
Here is a simple C program to do that.
run.c:
#include <stdio.h>
#include <unistd.h>
#define RUN_CMD "cmd" //the command to be run
int main(){
setreuid(geteuid(),geteuid());
system(RUN_CMD);
return(1);
}
Replace cmd with the command to run, then compile and change the
permissions etc.
gcc run.c
chown root a.out
chmod 4755 a.out
Of course if you want to do more then just run the same command all the
time the c program will need to be modified.