note 95779 modified in function.crack-check by danbrown
| From: | danbrown@php.net | Date: | Thu, 21 Jan 2010 15:30:33 +0000 |
| Subject: | note 95779 modified in function.crack-check by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165171@lists.php.net to get a copy of this message | ||
If you need to test a password with cracklib but don't have the necessary module available in
PHP, you can use a function like this.
It requires the command line cracklib-check binary in /usr/sbin, but changing its location is
trivial.
The $message variable will contain cracklib's complaint (if there is one)
You'll want to wrap your invocation of this function in a try...catch block.
<?php
function cracklibCheck($password, &$message)
{
// Clean up password
$password=str_replace("\r", "", $password);
$password=str_replace("\n", "", $password);
// Run password through cracklib-check
exec("echo ".escapeshellarg($password)." | /usr/sbin/cracklib-check
2>/dev/null", $output, $return_var);
// Check it ran properly
if($return_var==0)
{
if(preg_match("/^.*\: ([^:]+)$/", $output[0], $matches))
{
// Check response
if(strtoupper($matches[1])=="OK")
{
// Password is strong
$message="";
return(true);
}
else
{
// Cracklib doesn't like it
$message=$matches[1];
return(false);
}
}
else
{
// Badly formatted response from cracklib-check.
throw new Exception("Didn't understand cracklib-check response.");
}
}
else
{
// Some sort of execution error
throw new Exception("Failed to run cracklib-check.");
}
}
?>
--was--
If you need to test a password with cracklib but don't have the necessary module available in
PHP, you can use a function like this.
It requires the command line cracklib-check binary in /usr/sbin, but changing its location is
trivial.
The $message variable will contain cracklib's complaint (if there is one)
You'll want to wrap your invocation of this function in a try...catch block.
function cracklibCheck($password, &$message)
{
// Clean up password
$password=str_replace("\r", "", $password);
$password=str_replace("\n", "", $password);
// Run password through cracklib-check
exec("echo ".escapeshellarg($password)." | /usr/sbin/cracklib-check
2>/dev/null", $output, $return_var);
// Check it ran properly
if($return_var==0)
{
if(preg_match("/^.*\: ([^:]+)$/", $output[0], $matches))
{
// Check response
if(strtoupper($matches[1])=="OK")
{
// Password is strong
$message="";
return(true);
}
else
{
// Cracklib doesn't like it
$message=$matches[1];
return(false);
}
}
else
{
// Badly formatted response from cracklib-check.
throw new Exception("Didn't understand cracklib-check response.");
}
}
else
{
// Some sort of execution error
throw new Exception("Failed to run cracklib-check.");
}
}
http://php.net/manual/en/function.crack-check.php