I need to be able to change the users' password through a web page (in a controlled environment). So, for that, I'm using this code:
<?php
$output = shell_exec("sudo -u dummy passwd testUser testUserPassword");
$output2 = shell_exec("dummyPassword");
echo $output;
echo $output2;
echo "done";
?>
My problem is that this script is not changing the password for the user "testUser". What am I doing wrong?
Thanks
An easy I know and which works (at least for Debian 4.0r5) is:
Just adapt this to the php script and it should work fine.
I'm not familiar enough with PHP to tell you how to fix it, but your problem is that the two
shell_exec
commands are entirely separate. It appears as though you're trying to use the second command to pipe input to the first one, but that's not possible. The first command shouldn't return until after that process has executed, when you run the second one it will attempt to run the programdummyPassword
, which we can probably expect to fail.You should use the crypt() function to encrypt the password. Then you can call the
usermod
program like thisusermod --password username encryptedpassword
.The most common way to encrypt a UNIX login password is like this:
(
Where salt1234
is an eight letter salt)Use chpasswd:
Beware! If somebody will get control on $username then he can change any password on a system.
I it is way too late but this is for people still searching answer. This is what we use. Extremely simple.
Another option is to have a shell script, say called passwd_change.sh somewhere that looks like this:
Then in your php code do: