Initial commit.
This commit is contained in:
114
lib/php/monica/newpass.inc.php
Normal file
114
lib/php/monica/newpass.inc.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
// File name: newpass.inc.php
|
||||
// Description: PHP subroutine to generate a new password
|
||||
// Date: 2004-03-31
|
||||
// Author: imacat <imacat@pristine.com.tw>
|
||||
// Copyright: Copyright (C) 2004-2009 Pristine Communications
|
||||
|
||||
// Set the include path
|
||||
if (!defined("INCPATH_SET")) {
|
||||
require_once dirname(__FILE__) . "/incpath.inc.php";
|
||||
}
|
||||
// Referenced subroutines
|
||||
require_once "monica/cracklib.inc.php";
|
||||
require_once "monica/runcmd.inc.php";
|
||||
|
||||
// Settings
|
||||
define("NEWPASS_PRONOUNCABLE", 0);
|
||||
define("NEWPASS_RANDOM", 1);
|
||||
|
||||
define("_NEWPASS_RANDOM_LEN", 8);
|
||||
$_NEWPASS_APG = array("/usr/bin/apg", "-n", "1", "-d");
|
||||
$_NEWPASS_BAD_CHARS = array("0", "O", "1", "l", "I", "|");
|
||||
|
||||
|
||||
// newpass: Generate a new password
|
||||
function newpass($mode = NEWPASS_PRONOUNCABLE)
|
||||
{
|
||||
switch ($mode) {
|
||||
// A random password
|
||||
case NEWPASS_RANDOM:
|
||||
return _newpass_random();
|
||||
break;
|
||||
|
||||
// A pronouncable password
|
||||
case NEWPASS_PRONOUNCABLE:
|
||||
default:
|
||||
return _newpass_pronouncable();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// _newpass_pronouncable: Generate a new pronouncable password
|
||||
function _newpass_pronouncable()
|
||||
{
|
||||
global $_NEWPASS_BAD_CHARS, $_NEWPASS_APG;
|
||||
// Loop until we get a good password
|
||||
while (true) {
|
||||
$passwd = xruncmd($_NEWPASS_APG, "");
|
||||
// Avoid bad characters
|
||||
if (_newpass_bad_char($passwd)) {
|
||||
continue;
|
||||
}
|
||||
// Check the password strength with Cracklib
|
||||
if (!crack_check($passwd)) {
|
||||
continue;
|
||||
}
|
||||
return $passwd;
|
||||
}
|
||||
}
|
||||
|
||||
// _newpass_random: Generate a new random password
|
||||
function _newpass_random()
|
||||
{
|
||||
global $_NEWPASS_BAD_CHARS;
|
||||
// Loop until we get a good password
|
||||
while (true) {
|
||||
// Generate a password
|
||||
$passwd = "";
|
||||
while (strlen($passwd) < _NEWPASS_RANDOM_LEN) {
|
||||
$type = mt_rand(0, 2);
|
||||
switch ($type) {
|
||||
// Number
|
||||
case 0:
|
||||
$c = mt_rand(0, 9);
|
||||
break;
|
||||
// Lower-case character
|
||||
case 1:
|
||||
$c = chr(ord("a") + mt_rand(0, 25));
|
||||
break;
|
||||
// Upper-case character
|
||||
case 2:
|
||||
$c = chr(ord("A") + mt_rand(0, 25));
|
||||
break;
|
||||
}
|
||||
// Avoid bad characters
|
||||
if (in_array($c, $_NEWPASS_BAD_CHARS)) {
|
||||
continue;
|
||||
}
|
||||
$passwd .= $c;
|
||||
}
|
||||
// Check the password strength with Cracklib
|
||||
if (!crack_check($passwd)) {
|
||||
continue;
|
||||
}
|
||||
return $passwd;
|
||||
}
|
||||
}
|
||||
|
||||
// _newpass_bad_char: If the new password contains a bad character
|
||||
function _newpass_bad_char($passwd)
|
||||
{
|
||||
global $_NEWPASS_BAD_CHARS;
|
||||
for ($i = 0; $i < strlen($passwd); $i++) {
|
||||
$c = substr($passwd, $i, 1);
|
||||
// Found
|
||||
if (in_array($c, $_NEWPASS_BAD_CHARS)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Not found
|
||||
return false;
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user