[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1106
  • Last Modified:

useradd via PHP

Hi,

having some problems adding users to linux from a php script:

I have a PHP script that is triggered by a CRON job having root rights.
This scripts is feeded with usernames from a database and has
to add the users to the local users on the webserver via

useradd [accountname] -p [passwordhash] -s /bin/false -m

the problem is the second parameter [passwordhash]

how to generate a valid password hash in PHP and how to
give it to useradd via PHP or in other words:

HOW TO ADD A USER/PASSWORD FROM A PHP SCRIPT???

0
JtR
Asked:
JtR
1 Solution
 
spotxCommented:
Hi  JtR

Createing an password string is pretty easy using the crypt function
returns a string (ie passwordhash) crypt ( string str [, string salt] )
Check out http://au.php.net/manual/en/function.crypt.php for details.
It will default to creating a DES string if you don't give it a salt

It's pretty straight forward if you want if you want to create a MD5 it needs a 12 char length string starting with $1$ and ending with $
You can use something like
$salt = '$1$';
$chars =  array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0" );
for ($i = 0 ; $i < 8; $i ++)  
  $salt .= $chars[rand (0,(count($chars)-1))];
$salt .= '$';
$passwdhash = crypt ( $passwd,$salt);
Hope that helps

0
 
JtRAuthor Commented:
Yes you are right! Have solved it some hours later!

The second parameter $salt tells the stupid crypt function what algorithm to use.
If it starts with to characters (I took "AB") then it throws DES crypted strings.
So the solution is easy:

$pw_hash = crypt( $password, "AB" );
system( "useradd $username -p $pw_hash -s /bin/false -m" );

So bad that your comment was so late! It had saved me some hours!
So what! Thank you! :)

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now