faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

31 of 56 people (55%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How can I execute local programs as root from PHP? I want to use UNIX commands adduser and passwd.

Nov 28th, 2008 07:12
healthy man, dman, K Muma, Jon Bjorseth, Matt Rudderham,


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.
http://www.healthdothealth.com
http://www.healthxhealth.com
http://www.mesotheliomainmesothelioma.com
http://www.healthfirewall.com
http://www.flathealth.com
http://www.healthbodyguard.com