Using shell_exec ('passwd') to change user password

I need to be able to change the user password through a web page (in a controlled environment). So, for this I use 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 does not change the password for the user "testUser". What am I doing wrong?

thanks

+3
source share
9 answers

Another option is to have a shell script, for example called passwd_change.sh, that looks like this:

 #!/usr/bin/expect -f set username [lindex $argv 0] set password [lindex $argv 1] spawn passwd $username expect "(current) UNIX password: " send "$password\r" expect "Enter new UNIX password: " send "$password\r" expect "Retype new UNIX password: " send "$password\r" expect eof 

Then in your php code do:

 <?php shell_exec("sudo -u root /path/to/passwd_change.sh testUser testUserPass"); ?> 
+3
source

I'm not familiar with PHP enough to tell you how to fix it, but your problem is that the two shell_exec commands shell_exec completely separate. It seems you are trying to use the second command to enter input in the first, but that is not possible. The first command should not be returned until this process is completed, when you run the second, it will try to run the dummyPassword program, which we may expect to crash.

+2
source

Use proc_open , which will allow you to interact with the stdin process.

See this comment in particular in the manual: http://www.php.net/manual/en/function.proc-open.php#58044

+2
source

The first answer is correct. You probably want to use popen() or some other function that will return a channel that you can write in the same way as a file opened with fopen() or file() .

 <?php $pipe = popen("sudo -u dummy passwd testUser testUserPassword", 'r'); fwrite($pipe, "dummyPasswd\r\n"); pclose($pipe); echo "done"; ?> 

I have not tested this, but it is a general idea of ​​what you seem to be going to do. You will notice that this setting does not provide output from executed commands. To do this, you need to use proc_open() , which is a bit more difficult to work with, but provides bi-directional support.

+2
source

Use chpasswd:

 $tmpfname = tempnam('/tmp/', 'chpasswd'); $handle = fopen($tmpfname, "w"); fwrite($handle, "$username:".crypt($password)."\n"); fclose($handle); shell_exec("sudo sh -c \"chpasswd -e < $tmpfname\""); 

Caution! If someone gains control of $ username, then he can change any password in the system.

+2
source

You must use the crypt () function to encrypt the password. You can then call the usermod program as follows usermod --password username encryptedpassword .

The most common way to encrypt a password for UNIX login:

crypt ('password', '$ 1 $ salt1234 $')

( Where salt1234 is an eight-letter salt)

0
source

Easy I know and works (at least for Debian 4.0r5):

 #!/bin/bash USER="root" NEWPASS="bullsheit123" echo $USER:$NEWPASS | chpasswd echo $? 

Just adapt this to php script and it should work fine.

0
source

I'm too late, but this is for people who are still looking for an answer. This is what we use. Extremely simple.

  file_put_contents("passd", "$pass\n$pass\n"); echo "$uname: $pass\n"; `passwd $uname --stdin < passd`; `rm -rf passd`; 
0
source

Source: https://habr.com/ru/post/921228/


All Articles