Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Included in my thesis is to create an administration interface, in which an administrator can approve users whom have asked for access to use OpenNebula.

Upon approval the users should be added to OpenNebula (i.e. it corresponds to the command oneadmin createuser username password). (More information of the interface (section 4.2).)

I am trying to perform this with an execution of a shell command in a PHP file. Why doesn't this work (below is just a specific example - not the general code that I am using)?

$execQuery = "/bin/su oneadmin postgres -c '/usr/bin/oneuser create test10 test10'";
shell_exec ($execQuery); //execute query in shell.

Nor does this work (even though I am logged in as oneadmin, when the script is executed by PHP):

shell_exec ("/usr/bin/oneuser create test10 test10");

It works when I type that command in the shell. I have tried to execute other shell commands through php and those work fine, so I wonder where the problem lies.

I have given the file /usr/bin/oneuser rwx rights for all users.

share|improve this question
1  
What happens when you run your code? Are there any visible effects (output, log messages, …)? What happens if you put id or sleep 10 as the command? –  Gilles Jun 19 '11 at 11:20
    
Thanks for the reply! I get no output. Shell_exec ("sleep 10") and many other shell-commands works. –  Anders Branderud Jun 19 '11 at 12:32
    
shell_exec returns the output as a string. Does the oneuser command produce any output? Try printing out the return value instead of discarding it. Does the command produce any error output? If you run shell_exec("echo 1>&2 this is an error"), where does the error message end up? –  Gilles Jun 19 '11 at 12:40
    
Is it just me or is the commands shows here really confusing. Su is running a program called oneadmin and being told to run postgres with a command that starts with a full string to another program that creates a user? No wonder to poor guy can't figure out which part of the chain is broken. Or does it not go together like that? –  Caleb Jun 19 '11 at 19:01

2 Answers 2

On the php manual for shell_exec, it shows that the function returns the output as a string. If you expect output from the program you launch, you need to capture this like so:

$execQuery = "echo -n test_command";
$output = shell_exeeec($execQuery);
echo $output;

Your question doesn't show trying to capture any data. If you also make sure to connect stdout and stderr when you run your command, you should get a better idea is what is going on. To use your example:

$out = shell_exec("/usr/bin/oneuser create test10 test10 2>&1");
var_dump($out);

That should help you see what is going on. As Shadur suggests, it seems likely that these programs expect an interactive terminal that can enter passwords in order to run. Even if don't need input, they might expect interactive shells. And he's right that su doesn't play nice in this context. There is, however, a correct tool for the job.

You can setup sudo to such that your http user can execute your program as username without a password but NOT be able to do anything else by running visudo or whatever you use to edit your sudoers file and adding this line:

http    ALL=(username) /usr/bin/oneadmin

Then in php your command would look something like this:

$execQuery = "sudo -u username /usr/bin/oneadmin postgres -c '/usr/bin/oneuser create test10 test10'";
$out = shell_exec ("$execQuery 2>&1");
echo $out
share|improve this answer
    
Thanks for your help! I know get this error: "testsudo: no tty present and no askpass program specified" –  Anders Branderud Jun 21 '11 at 10:35

In the first case, su generally asks for a password when you run it; shell_exec doesn't handle interactivity well.

I don't know oneuser, maybe it's trying to ask for some password as well?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.