views:

724

answers:

4

I need to do some command lines through a browser. What I need to do in a command-line would be:

$login
<login name>
<password>
$passwd
<old password>
<new password>
<retype new password>

So, how can I do this using the proc_open function? Or should I use another function to do this?

Thanks

+1  A: 

You don't need to issue login to change the password for a user, just give their name to passwd. Using popen (as we don't need to read in this tiny example), something like

$pp = popen("passwd ${user}", "w");
fwrite($pp, $oldPassword . '\n');
fwrite($pp, $newPassword . '\n');
fwrite($pp, $newPassword . '\n');
pclose($pp);

If you want to read the responses, use proc_open, and just read from the stdout handle you're given.

I hope this is all well secured, and that you have a lot of sanitisation on the username.

Adam Wright
This will only work when the php interpreter runs with superuser privileges (only root can change the passwd of an other user). If you don't care about security, you can either set the superuser bit of passwd or use sudo.
jk
+1  A: 

http://pecl.php.net/package/PAM might be something you can use.
It has a function bool pam_chpass(string $username, string $oldpassword, string $newpassword [, string &$error ]).
But I've never used it.

VolkerK
Doesn't seem to work on my Ubuntu 10.04 box.
wag2639
A: 

Adam Wright, I've tried your example, but I just can't change users password. do I need to do any other things in the script (besides defining $user, $userPassword and $newPassword)?

Thanks

Armadillo
+1  A: 

Found this Change Linux or UNIX system password using PHP script in the internet.

A very detailed description on how to create a website where a user can change his own system password.

jk