views:

184

answers:

2

hey all, I'm very new to linux and php so forgive me if this is a dumb question.

I need to create a php command line script that will add a user to the system, to be used by the ftp server. I have it set up at this point that it creates the account just fine. The problem I'm facing is that even though I pass through the password when I create the account ie. -p $password, I still have to run the "passwd" command on the account and set the password manually before proftpd will let that account log in.

For the life of me I can't figure out how to run the passwd command from within the php. Obviously I can run it, but I have no idea how to programatically enter the password when prompted.

Just for more info, this is the command I run when creating the account. $username and $ password are passed into the function.

shell_exec("useradd ".$username." -g ftpusers -m -p ".$password." -d
/home/ftp/".$username."/ -s /bin/false);

now im looking to add something along the lines of:

shell_exec("passwd ".$username);
echo $password;
echo $password;

Just for the record, this last example is just for illustration.

Any help appreciated.

A: 

Can't guarantee it'll work, but on first thoughts, my solution would be:-

<?php
$fp = popen("/usr/bin/passwd " . $user, "w");

fwrite($fp, $password . "\n");
fwrite($fp, $password . "\n");

fclose($fp);
Mez
+3  A: 

You need to use the hashed password using useradd, e.g.

$password = escapeshellarg( crypt('password') );
shell_exec("useradd username -p $password");

-p, --password PASSWORD The encrypted password, as returned by crypt(3). The default is to disable the account.

(man useradd)

Tom Haigh
Thank you, this did the trick for me. I read about this "crypt" yesterday, I just couldn't get it to work.hehe, anyways, thank you very much.
Craigt