Provide login password from php script to shell command

Viewed 618

I have a custom authentication application that requires the user password to be provided via STDIN. For example I can run this on the command line (in linux):

echo "P455w0rd" | authenticate -A username

The exit status is 0 or 1 and I need to use that to determine if the authentication passed or failed. The 'authenticate' app blocks on read from STDIN for the password. I can run the authenticate app from PHP with (edit):

$username = $_POST['username'];
$password = $_POST['password'];
// I need to run this shell command and provide the password via STDIN (how?)    
exec("authenticate -A $username", $output, $retval);

The exec blocks while the app waits for STDIN. How to I provide the password to the app from the PHP script? Will this work?

$username = $_POST['username'];
$password = $_POST['password'];
// I need to run this shell command and provide the password via STDIN (how?)
// Can I exec a background process and then fwrite to STDOUT?  
exec("authenticate -A $username &", $output, $retval);
fwrite(STDOUT, $password);
2 Answers

Your some php code is which as example in file t.php:

<?php
echo "pwd";
?>

So you need to pass output as an argument for some command... Ok, you may do this in shell like these:

shell$: php t.php | xargs echo

Output:

pwd

I started working on changing the code to use a Unix Domain socket and hit on the idea of just providing the password through an environment variable. I ended up changing the authentication executable to look for an environment variable and changed the php code to provide one via putenv(). So now it looks like this:

putenv("THE_PW=$password");
$returnval = exec("authenticate -A $username -n THE_PW", $output, $retval);
Related