views:

63

answers:

3

Hello

I want to build a php based site that (automate) some commands on my Ubuntu Server

first thing I did was going to the file (sudoers) and add the user www-data so I can execute php commands with root privileges!

# running the web apps with root power!!!
www-data    ALL=(ALL) NOPASSWD: ALL

then my PHP code was

<?php
   $command = "cat /etc/passwd | cut -d\":\" -f1";
   echo 'running the command: <b>'.$command."</b><br />";
   echo exec($command);
?>

it returns only one user (the last user) !!! how to make it return all users?

thank you

+3  A: 

From the PHP manual on exec:

Return Values

The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function. To get the output of the executed command, be sure to set and use the output parameter.

So you have to do something similar to this:

<?php
   $output = array();
   $command = "cat /etc/passwd | cut -d\":\" -f1";
   echo 'running the command: <b>'.$command."</b><br />";
   exec($command, &$output);
   echo implode("<br />\n", $output);
?>
svens
worked perfectly, Thank allot :-)
Data-Base
+1  A: 

Like Matt S said, that's an incredibly bad idea to allow www-data root access on your server. The slightest compromise through your web applications could allow anyone full control of your system.

A better idea would be to make separate scripts for specific accessions then use SUID permissions. This means, a specific user (in this case, www-data) can make small changes to the system through the execution of scripts. Still not a good idea, though. You may be able to work around it with suPHP but security is still a major concern.

Gabriel Evans
but how I can build a page that can do some system changes?like adding/deleting users / change the ip an the host of the system I will always need som admin poer o do that !!!
Data-Base
A: 

/etc/passwd is readable by anyone, so you should be able to execute your command without having any special rights (unless PHP prevents it?).

Benjamin