包含:登录失败锁定、90天密码有效期、30分钟会话超时、 强制改密、登录审计日志、屏幕水印、企业背景图、 备案信息固定底部、favicon、登录页JS修复等全部改动
This commit is contained in:
@@ -0,0 +1,411 @@
|
||||
<?php
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* File::Passwd
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category FileFormats
|
||||
* @package File_Passwd
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @copyright 2003-2005 Michael Wallner
|
||||
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
||||
* @version CVS: $Id$
|
||||
* @link http://pear.php.net/package/File_Passwd
|
||||
*/
|
||||
|
||||
require_once 'File/Passwd/Exception.php';
|
||||
|
||||
/**
|
||||
* Encryption constants.
|
||||
*/
|
||||
// SHA encryption.
|
||||
define('FILE_PASSWD_SHA', 'sha');
|
||||
// MD5 encryption
|
||||
define('FILE_PASSWD_MD5', 'md5');
|
||||
// DES encryption
|
||||
define('FILE_PASSWD_DES', 'des');
|
||||
// NT hash encryption.
|
||||
define('FILE_PASSWD_NT', 'nt');
|
||||
// LM hash encryption.
|
||||
define('FILE_PASSWD_LM', 'lm');
|
||||
// PLAIN (no encryption)
|
||||
define('FILE_PASSWD_PLAIN', 'plain');
|
||||
|
||||
/**
|
||||
* Error constants.
|
||||
*/
|
||||
// Undefined error.
|
||||
define('FILE_PASSWD_E_UNDEFINED', 0);
|
||||
// Invalid file format.
|
||||
define('FILE_PASSWD_E_INVALID_FORMAT', 1);
|
||||
define('FILE_PASSWD_E_INVALID_FORMAT_STR', 'Passwd file has invalid format.');
|
||||
// Invalid extra property.
|
||||
define('FILE_PASSWD_E_INVALID_PROPERTY', 2);
|
||||
define('FILE_PASSWD_E_INVALID_PROPERTY_STR', 'Invalid property \'%s\'.');
|
||||
// Invalid characters.
|
||||
define('FILE_PASSWD_E_INVALID_CHARS', 3);
|
||||
define('FILE_PASSWD_E_INVALID_CHARS_STR', '%s\'%s\' contains illegal characters.');
|
||||
// Invalid encryption mode.
|
||||
define('FILE_PASSWD_E_INVALID_ENC_MODE', 4);
|
||||
define('FILE_PASSWD_E_INVALID_ENC_MODE_STR', 'Encryption mode \'%s\' not supported.');
|
||||
// Exists already.
|
||||
define('FILE_PASSWD_E_EXISTS_ALREADY', 5);
|
||||
define('FILE_PASSWD_E_EXISTS_ALREADY_STR', '%s\'%s\' already exists.');
|
||||
// Exists not.
|
||||
define('FILE_PASSWD_E_EXISTS_NOT', 6);
|
||||
define('FILE_PASSWD_E_EXISTS_NOT_STR', '%s\'%s\' doesn\'t exist.');
|
||||
// User not in group.
|
||||
define('FILE_PASSWD_E_USER_NOT_IN_GROUP', 7);
|
||||
define('FILE_PASSWD_E_USER_NOT_IN_GROUP_STR', 'User \'%s\' doesn\'t exist in group \'%s\'.');
|
||||
// User not in realm.
|
||||
define('FILE_PASSWD_E_USER_NOT_IN_REALM', 8);
|
||||
define('FILE_PASSWD_E_USER_NOT_IN_REALM_STR', 'User \'%s\' doesn\'t exist in realm \'%s\'.');
|
||||
// Parameter must be of type array.
|
||||
define('FILE_PASSWD_E_PARAM_MUST_BE_ARRAY', 9);
|
||||
define('FILE_PASSWD_E_PARAM_MUST_BE_ARRAY_STR', 'Parameter %s must be of type array.');
|
||||
// Method not implemented.
|
||||
define('FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED', 10);
|
||||
define('FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED_STR', 'Method \'%s()\' not implemented.');
|
||||
// Directory couldn't be created.
|
||||
define('FILE_PASSWD_E_DIR_NOT_CREATED', 11);
|
||||
define('FILE_PASSWD_E_DIR_NOT_CREATED_STR', 'Couldn\'t create directory \'%s\'.');
|
||||
// File couldn't be opened.
|
||||
define('FILE_PASSWD_E_FILE_NOT_OPENED', 12);
|
||||
define('FILE_PASSWD_E_FILE_NOT_OPENED_STR', 'Couldn\'t open file \'%s\'.');
|
||||
// File coudn't be locked.
|
||||
define('FILE_PASSWD_E_FILE_NOT_LOCKED', 13);
|
||||
define('FILE_PASSWD_E_FILE_NOT_LOCKED_STR', 'Couldn\'t lock file \'%s\'.');
|
||||
// File couldn't be unlocked.
|
||||
define('FILE_PASSWD_E_FILE_NOT_UNLOCKED', 14);
|
||||
define('FILE_PASSWD_E_FILE_NOT_UNLOCKED_STR', 'Couldn\'t unlock file.');
|
||||
// File couldn't be closed.
|
||||
define('FILE_PASSWD_E_FILE_NOT_CLOSED', 15);
|
||||
define('FILE_PASSWD_E_FILE_NOT_CLOSED_STR', 'Couldn\'t close file.');
|
||||
|
||||
/**
|
||||
* Allowed 64 chars for salts
|
||||
*/
|
||||
$GLOBALS['_FILE_PASSWD_64'] =
|
||||
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
||||
|
||||
/**
|
||||
* The package File_Passwd provides classes and methods
|
||||
* to handle many different kinds of passwd files.
|
||||
*
|
||||
* The File_Passwd class in certain is a factory container for its special
|
||||
* purpose extension classes, each handling a specific passwd file format.
|
||||
* It also provides a static method for reasonable fast user authentication.
|
||||
* Beside that it offers some encryption methods used by the extensions.
|
||||
*
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @version $Revision$
|
||||
*
|
||||
* Usage Example:
|
||||
* <code>
|
||||
* $passwd = &File_Passwd::factory('Unix');
|
||||
* </code>
|
||||
*/
|
||||
class File_Passwd
|
||||
{
|
||||
/**
|
||||
* Get API version
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return string API version
|
||||
*/
|
||||
function apiVersion()
|
||||
{
|
||||
return '1.0.0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate salt
|
||||
*
|
||||
* @access public
|
||||
* @return mixed
|
||||
* @param int $length salt length
|
||||
* @return string the salt
|
||||
*/
|
||||
function salt($length = 2)
|
||||
{
|
||||
$salt = '';
|
||||
$length = (int) $length;
|
||||
$length < 2 && $length = 2;
|
||||
for($i = 0; $i < $length; $i++) {
|
||||
$salt .= $GLOBALS['_FILE_PASSWD_64'][rand(0, 63)];
|
||||
}
|
||||
return $salt;
|
||||
}
|
||||
|
||||
/**
|
||||
* No encryption (plaintext)
|
||||
*
|
||||
* @access public
|
||||
* @return string plaintext input
|
||||
* @param string plaintext passwd
|
||||
*/
|
||||
function crypt_plain($plain)
|
||||
{
|
||||
return $plain;
|
||||
}
|
||||
|
||||
/**
|
||||
* DES encryption
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return string crypted text
|
||||
* @param string $plain plaintext to encrypt
|
||||
* @param string $salt the salt to use for encryption (2 chars)
|
||||
*/
|
||||
function crypt_des($plain, $salt = null)
|
||||
{
|
||||
(is_null($salt) || strlen($salt) < 2) && $salt = File_Passwd::salt(2);
|
||||
return crypt($plain, $salt);
|
||||
}
|
||||
|
||||
/**
|
||||
* MD5 encryption
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return string crypted text
|
||||
* @param string $plain plaintext to encrypt
|
||||
* @param string $salt the salt to use for encryption
|
||||
* (>2 chars starting with $1$)
|
||||
*/
|
||||
function crypt_md5($plain, $salt = null)
|
||||
{
|
||||
if (
|
||||
is_null($salt) ||
|
||||
strlen($salt) < 3 ||
|
||||
!preg_match('/^\$1\$/', $salt))
|
||||
{
|
||||
$salt = '$1$' . File_Passwd::salt(8);
|
||||
}
|
||||
return crypt($plain, $salt);
|
||||
}
|
||||
|
||||
/**
|
||||
* SHA1 encryption
|
||||
*
|
||||
* Returns a PEAR_Error if sha1() is not available (PHP<4.3).
|
||||
*
|
||||
* @static
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed crypted string or PEAR_Error
|
||||
* @param string $plain plaintext to encrypt
|
||||
*/
|
||||
function crypt_sha($plain)
|
||||
{
|
||||
if (!function_exists('sha1')) {
|
||||
throw new File_Passwd_Exception(
|
||||
'SHA1 encryption is not available (PHP < 4.3).',
|
||||
FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
);
|
||||
}
|
||||
$hash = PEAR_ZE2 ? sha1($plain, true) : pack('H40', sha1($plain));
|
||||
return '{SHA}' . base64_encode($hash);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* APR compatible MD5 encryption
|
||||
*
|
||||
* @access public
|
||||
* @return mixed
|
||||
* @param string $plain plaintext to crypt
|
||||
* @param string $salt the salt to use for encryption
|
||||
*/
|
||||
function crypt_apr_md5($plain, $salt = null)
|
||||
{
|
||||
if (is_null($salt)) {
|
||||
$salt = File_Passwd::salt(8);
|
||||
} elseif (preg_match('/^\$apr1\$/', $salt)) {
|
||||
$salt = preg_replace('/^\$apr1\$([^$]+)\$.*/', '\\1', $salt);
|
||||
} else {
|
||||
$salt = substr($salt, 0,8);
|
||||
}
|
||||
|
||||
$length = strlen($plain);
|
||||
$context = $plain . '$apr1$' . $salt;
|
||||
|
||||
if (PEAR_ZE2) {
|
||||
$binary = md5($plain . $salt . $plain, true);
|
||||
} else {
|
||||
$binary = pack('H32', md5($plain . $salt . $plain));
|
||||
}
|
||||
|
||||
for ($i = $length; $i > 0; $i -= 16) {
|
||||
$context .= substr($binary, 0, min(16 , $i));
|
||||
}
|
||||
for ( $i = $length; $i > 0; $i >>= 1) {
|
||||
$context .= ($i & 1) ? chr(0) : $plain[0];
|
||||
}
|
||||
|
||||
$binary = PEAR_ZE2 ? md5($context, true) : pack('H32', md5($context));
|
||||
|
||||
for ($i = 0; $i < 1000; $i++) {
|
||||
$new = ($i & 1) ? $plain : $binary;
|
||||
if ($i % 3) {
|
||||
$new .= $salt;
|
||||
}
|
||||
if ($i % 7) {
|
||||
$new .= $plain;
|
||||
}
|
||||
$new .= ($i & 1) ? $binary : $plain;
|
||||
$binary = PEAR_ZE2 ? md5($new, true) : pack('H32', md5($new));
|
||||
}
|
||||
|
||||
$p = array();
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
$k = $i + 6;
|
||||
$j = $i + 12;
|
||||
if ($j == 16) {
|
||||
$j = 5;
|
||||
}
|
||||
$p[] = File_Passwd::_64(
|
||||
(ord($binary[$i]) << 16) |
|
||||
(ord($binary[$k]) << 8) |
|
||||
(ord($binary[$j])),
|
||||
5
|
||||
);
|
||||
}
|
||||
|
||||
return
|
||||
'$apr1$' . $salt . '$' . implode($p) .
|
||||
File_Passwd::_64(ord($binary[11]), 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert hexadecimal string to binary data
|
||||
*
|
||||
* @static
|
||||
* @access private
|
||||
* @return mixed
|
||||
* @param string $hex
|
||||
*/
|
||||
function _hexbin($hex)
|
||||
{
|
||||
$rs = '';
|
||||
$ln = strlen($hex);
|
||||
for($i = 0; $i < $ln; $i += 2) {
|
||||
$rs .= chr(hexdec($hex{$i} . $hex{$i+1}));
|
||||
}
|
||||
return $rs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to allowed 64 characters for encryption
|
||||
*
|
||||
* @static
|
||||
* @access private
|
||||
* @return string
|
||||
* @param string $value
|
||||
* @param int $count
|
||||
*/
|
||||
function _64($value, $count)
|
||||
{
|
||||
$result = '';
|
||||
while($count > 0 && --$count) {
|
||||
$result .= $GLOBALS['_FILE_PASSWD_64'][$value & 0x3f];
|
||||
$value >>= 6;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory for new extensions
|
||||
*
|
||||
* o Unix for standard Unix passwd files
|
||||
* o CVS for CVS pserver passwd files
|
||||
* o SMB for SMB server passwd files
|
||||
* o Authbasic for AuthUserFiles
|
||||
* o Authdigest for AuthDigestFiles
|
||||
* o Custom for custom formatted passwd files
|
||||
*
|
||||
* Returns a PEAR_Error if the desired class/file couldn't be loaded.
|
||||
*
|
||||
* @static use &File_Passwd::factory() for instantiating your passwd object
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return object File_Passwd_$class - desired Passwd object
|
||||
* @param string $class the desired subclass of File_Passwd
|
||||
*/
|
||||
function factory($class)
|
||||
{
|
||||
$class = ucFirst(strToLower($class));
|
||||
if (!@include_once "File/Passwd/$class.php") {
|
||||
throw new File_Passwd_Exception("Couldn't load file Passwd/$class.php", 0);
|
||||
}
|
||||
$class = 'File_Passwd_'.$class;
|
||||
if (!class_exists($class)) {
|
||||
throw new File_Passwd_Exception("Couldn't load class $class.", 0);
|
||||
}
|
||||
return new $class();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast authentication of a certain user
|
||||
*
|
||||
* Though this approach should be reasonable fast, it is NOT
|
||||
* with APR compatible MD5 encryption used for htpasswd style
|
||||
* password files encrypted in MD5. Generating one MD5 password
|
||||
* takes about 0.3 seconds!
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o file doesn't exist
|
||||
* o file couldn't be opened in read mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked (only if auth fails)
|
||||
* o file couldn't be closed (only if auth fails)
|
||||
* o invalid <var>$type</var> was provided
|
||||
* o invalid <var>$opt</var> was provided
|
||||
*
|
||||
* Depending on <var>$type</var>, <var>$opt</var> should be:
|
||||
* o Smb: encryption method (NT or LM)
|
||||
* o Unix: encryption method (des or md5)
|
||||
* o Authbasic: encryption method (des, sha or md5)
|
||||
* o Authdigest: the realm the user is in
|
||||
* o Cvs: n/a (empty)
|
||||
* o Custom: array of 2 elements: encryption function and delimiter
|
||||
*
|
||||
* @static call this method statically for a reasonable fast authentication
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return return mixed true if authenticated,
|
||||
* false if not or PEAR_error
|
||||
*
|
||||
* @param string $type Unix, Cvs, Smb, Authbasic or Authdigest
|
||||
* @param string $file path to passwd file
|
||||
* @param string $user the user to authenticate
|
||||
* @param string $pass the plaintext password
|
||||
* @param mixed $opt o Smb: NT or LM
|
||||
* o Unix: des or md5
|
||||
* o Authbasic des, sha or md5
|
||||
* o Authdigest realm the user is in
|
||||
* o Custom encryption function and
|
||||
* delimiter character
|
||||
*/
|
||||
function staticAuth($type, $file, $user, $pass, $opt = '')
|
||||
{
|
||||
$type = ucFirst(strToLower($type));
|
||||
if (!@include_once "File/Passwd/$type.php") {
|
||||
throw new File_Passwd_Exception("Couldn't load file Passwd/$type.php", 0);
|
||||
}
|
||||
$func = array('File_Passwd_' . $type, 'staticAuth');
|
||||
return call_user_func($func, $file, $user, $pass, $opt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,381 @@
|
||||
<?php
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* File::Passwd::Authbasic
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category FileFormats
|
||||
* @package File_Passwd
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @copyright 2003-2005 Michael Wallner
|
||||
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
||||
* @version CVS: $Id$
|
||||
* @link http://pear.php.net/package/File_Passwd
|
||||
*/
|
||||
|
||||
/**
|
||||
* Requires File::Passwd::Common
|
||||
*/
|
||||
require_once 'File/Passwd/Common.php';
|
||||
|
||||
/**
|
||||
* Manipulate AuthUserFiles as used for HTTP Basic Authentication.
|
||||
*
|
||||
* <kbd><u>
|
||||
* Usage Example:
|
||||
* </u></kbd>
|
||||
* <code>
|
||||
* $htp = &File_Passwd::factory('AuthBasic');
|
||||
* $htp->setMode('sha');
|
||||
* $htp->setFile('/www/mike/auth/.htpasswd');
|
||||
* $htp->load();
|
||||
* $htp->addUser('mike', 'secret');
|
||||
* $htp->save();
|
||||
* </code>
|
||||
*
|
||||
* <kbd><u>
|
||||
* Output of listUser()
|
||||
* </u></kbd>
|
||||
* <pre>
|
||||
* array
|
||||
* + user => crypted_passwd
|
||||
* + user => crypted_passwd
|
||||
* </pre>
|
||||
*
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @package File_Passwd
|
||||
* @version $Revision$
|
||||
* @access public
|
||||
*/
|
||||
class File_Passwd_Authbasic extends File_Passwd_Common
|
||||
{
|
||||
/**
|
||||
* Path to AuthUserFile
|
||||
*
|
||||
* @var string
|
||||
* @access private
|
||||
*/
|
||||
var $_file = '.htpasswd';
|
||||
|
||||
/**
|
||||
* Actual encryption mode
|
||||
*
|
||||
* @var string
|
||||
* @access private
|
||||
*/
|
||||
var $_mode = 'sha';
|
||||
|
||||
/**
|
||||
* Supported encryption modes
|
||||
*
|
||||
* @var array
|
||||
* @access private
|
||||
*/
|
||||
var $_modes = array('md5' => 'm', 'des' => 'd', 'sha' => 's');
|
||||
|
||||
/**
|
||||
* Constructor (ZE2)
|
||||
*
|
||||
* Rewritten because DES encryption is not
|
||||
* supportet by the Win32 httpd.
|
||||
*
|
||||
* @access protected
|
||||
* @param string $file path to AuthUserFile
|
||||
*/
|
||||
function __construct($file = '.htpasswd')
|
||||
{
|
||||
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
|
||||
unset($this->_modes['des']);
|
||||
}
|
||||
$this->setFile($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast authentication of a certain user
|
||||
*
|
||||
* Raises exception if:
|
||||
* o file doesn't exist
|
||||
* o file couldn't be opened in read mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked (only if auth fails)
|
||||
* o file couldn't be closed (only if auth fails)
|
||||
*
|
||||
* @static call this method statically for a reasonable fast authentication
|
||||
*
|
||||
* @throws File_Passwd_Exception
|
||||
* @access public
|
||||
* @return bool
|
||||
* @param string $file path to passwd file
|
||||
* @param string $user user to authenticate
|
||||
* @param string $pass plaintext password
|
||||
* @param string $mode des, sha or md5
|
||||
*/
|
||||
function staticAuth($file, $user, $pass, $mode)
|
||||
{
|
||||
$line = File_Passwd_Common::_auth($file, $user);
|
||||
if (!$line) {
|
||||
return $line;
|
||||
}
|
||||
list(,$real) = explode(':', $line);
|
||||
$crypted = File_Passwd_Authbasic::_genPass($pass, $real, $mode);
|
||||
return ($real === $crypted);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply changes and rewrite AuthUserFile
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o directory in which the file should reside couldn't be created
|
||||
* o file couldn't be opened in write mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked
|
||||
* o file couldn't be closed
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function save()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->_users as $user => $pass) {
|
||||
$content .= $user . ':' . $pass . "\n";
|
||||
}
|
||||
return $this->_save($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an user
|
||||
*
|
||||
* The username must start with an alphabetical character and must NOT
|
||||
* contain any other characters than alphanumerics, the underline and dash.
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user already exists
|
||||
* o user contains illegal characters
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
* @param string $user
|
||||
* @param string $pass
|
||||
*/
|
||||
function addUser($user, $pass)
|
||||
{
|
||||
if ($this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_ALREADY
|
||||
);
|
||||
}
|
||||
if (!preg_match($this->_pcre, $user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
$this->_users[$user] = $this->_genPass($pass);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the password of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if user doesn't exist.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or a PEAR_Error
|
||||
* @param string $user the user whose password should be changed
|
||||
* @param string $pass the new plaintext password
|
||||
*/
|
||||
function changePasswd($user, $pass)
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
$this->_users[$user] = $this->_genPass($pass);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify password
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user doesn't exist
|
||||
* o an invalid encryption mode was supplied
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true if passwords equal, false if they don't, or PEAR_Error
|
||||
* @param string $user the user whose password should be verified
|
||||
* @param string $pass the plaintext password to verify
|
||||
*/
|
||||
function verifyPasswd($user, $pass)
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
$real = $this->_users[$user];
|
||||
return ($real === $this->_genPass($pass, $real));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get actual encryption mode
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function getMode()
|
||||
{
|
||||
return $this->_mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get supported encryption modes
|
||||
*
|
||||
* <pre>
|
||||
* array
|
||||
* + md5
|
||||
* + sha
|
||||
* + des
|
||||
* </pre>
|
||||
*
|
||||
* ATTN: DES encryption not available on Win32!
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
function listModes()
|
||||
{
|
||||
return array_keys($this->_modes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the encryption mode
|
||||
*
|
||||
* You can choose one of md5, sha or des.
|
||||
*
|
||||
* ATTN: DES encryption not available on Win32!
|
||||
*
|
||||
* Returns a PEAR_Error if a specific encryption mode is not supported.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on succes or PEAR_Error
|
||||
* @param string $mode
|
||||
*/
|
||||
function setMode($mode)
|
||||
{
|
||||
$mode = strToLower($mode);
|
||||
if (!isset($this->_modes[$mode])) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
|
||||
FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
);
|
||||
}
|
||||
$this->_mode = $mode;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate password with htpasswd executable
|
||||
*
|
||||
* @access private
|
||||
* @return string the crypted password
|
||||
* @param string $pass the plaintext password
|
||||
* @param string $salt the salt to use
|
||||
* @param string $mode encyption mode, usually determined from
|
||||
* <var>$this->_mode</var>
|
||||
*/
|
||||
function _genPass($pass, $salt = null, $mode = null)
|
||||
{
|
||||
$mode = is_null($mode) ? strToLower($this->_mode) : strToLower($mode);
|
||||
|
||||
if ($mode == 'md5') {
|
||||
return File_Passwd::crypt_apr_md5($pass, $salt);
|
||||
} elseif ($mode == 'des') {
|
||||
return File_Passwd::crypt_des($pass, $salt);
|
||||
} elseif ($mode == 'sha') {
|
||||
return File_Passwd::crypt_sha($pass, $salt);
|
||||
}
|
||||
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
|
||||
FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the AuthUserFile
|
||||
*
|
||||
* Returns a PEAR_Error if AuthUserFile has invalid format.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_error
|
||||
*/
|
||||
function parse()
|
||||
{
|
||||
$this->_users = array();
|
||||
foreach ($this->_contents as $line) {
|
||||
$user = explode(':', $line);
|
||||
if (count($user) != 2) {
|
||||
throw new File_Passwd_Exception(
|
||||
FILE_PASSWD_E_INVALID_FORMAT_STR,
|
||||
FILE_PASSWD_E_INVALID_FORMAT
|
||||
);
|
||||
}
|
||||
$this->_users[$user[0]] = trim($user[1]);
|
||||
}
|
||||
$this->_contents = array();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Password
|
||||
*
|
||||
* Returns PEAR_Error FILE_PASSD_E_INVALID_ENC_MODE if the supplied
|
||||
* encryption mode is not supported.
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return mixed The crypted password on success or PEAR_Error on failure.
|
||||
* @param string $pass The plaintext password.
|
||||
* @param string $mode The encryption mode to use (des|md5|sha).
|
||||
* @param string $salt The salt to use.
|
||||
*/
|
||||
function generatePasswd($pass, $mode = FILE_PASSWD_DES, $salt = null)
|
||||
{
|
||||
if (!in_array(strToLower($mode), array('des', 'md5', 'sha'))) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
|
||||
FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
);
|
||||
}
|
||||
return File_Passwd_Authbasic::_genPass($pass, $salt, $mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
* @deprecated
|
||||
*/
|
||||
function generatePassword($pass, $mode = FILE_PASSWD_DES, $salt = null)
|
||||
{
|
||||
return File_Passwd_Authbasic::generatePasswd($pass, $mode, $salt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
<?php
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* File::Passwd::Authdigest
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category FileFormats
|
||||
* @package File_Passwd
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @copyright 2003-2005 Michael Wallner
|
||||
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
||||
* @version CVS: $Id$
|
||||
* @link http://pear.php.net/package/File_Passwd
|
||||
*/
|
||||
|
||||
/**
|
||||
* Requires File::Passwd::Common
|
||||
*/
|
||||
require_once 'File/Passwd/Common.php';
|
||||
|
||||
/**
|
||||
* Manipulate AuthDigestFiles as used for HTTP Digest Authentication.
|
||||
*
|
||||
* <kbd><u>
|
||||
* Usage Example:
|
||||
* </u></kbd>
|
||||
* <code>
|
||||
* $htd = &File_Passwd::factory('Authdigest');
|
||||
* $htd->setFile('/www/mike/auth/.htdigest');
|
||||
* $htd->load();
|
||||
* $htd->addUser('mike', 'myRealm', 'secret');
|
||||
* $htd->save();
|
||||
* </code>
|
||||
*
|
||||
* <kbd><u>
|
||||
* Output of listUser()
|
||||
* </u></kbd>
|
||||
* <pre>
|
||||
* array
|
||||
* + user => array
|
||||
* + realm => crypted_passwd
|
||||
* + realm => crypted_passwd
|
||||
* + user => array
|
||||
* + realm => crypted_passwd
|
||||
* </pre>
|
||||
*
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @package File_Passwd
|
||||
* @version $Revision$
|
||||
* @access public
|
||||
*/
|
||||
class File_Passwd_Authdigest extends File_Passwd_Common
|
||||
{
|
||||
/**
|
||||
* Path to AuthDigestFile
|
||||
*
|
||||
* @var string
|
||||
* @access private
|
||||
*/
|
||||
var $_file = '.htdigest';
|
||||
|
||||
/**
|
||||
* Fast authentication of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o file doesn't exist
|
||||
* o file couldn't be opened in read mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked (only if auth fails)
|
||||
* o file couldn't be closed (only if auth fails)
|
||||
*
|
||||
* @static call this method statically for a reasonable fast authentication
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true if authenticated, false if not or PEAR_Error
|
||||
* @param string $file path to passwd file
|
||||
* @param string $user user to authenticate
|
||||
* @param string $pass plaintext password
|
||||
* @param string $realm the realm the user is in
|
||||
*/
|
||||
function staticAuth($file, $user, $pass, $realm)
|
||||
{
|
||||
$line = File_Passwd_Common::_auth($file, $user.':'.$realm);
|
||||
if (!$line) {
|
||||
return $line;
|
||||
}
|
||||
@list(,,$real)= explode(':', $line);
|
||||
return (md5("$user:$realm:$pass") === $real);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply changes and rewrite AuthDigestFile
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o directory in which the file should reside couldn't be created
|
||||
* o file couldn't be opened in write mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked
|
||||
* o file couldn't be closed
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or a PEAR_Error
|
||||
*/
|
||||
function save()
|
||||
{
|
||||
$content = '';
|
||||
if (count($this->_users)) {
|
||||
foreach ($this->_users as $user => $realm) {
|
||||
foreach ($realm as $r => $pass){
|
||||
$content .= "$user:$r:$pass\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->_save($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an user
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o the user already exists in the supplied realm
|
||||
* o the user or realm contain illegal characters
|
||||
*
|
||||
* $user and $realm must start with an alphabetical charachter and must NOT
|
||||
* contain any other characters than alphanumerics, the underline and dash.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or a PEAR_Error
|
||||
* @param string $user the user to add
|
||||
* @param string $realm the realm the user should be in
|
||||
* @param string $pass the plaintext password
|
||||
*/
|
||||
function addUser($user, $realm, $pass)
|
||||
{
|
||||
if ($this->userInRealm($user, $realm)) {
|
||||
throw new File_Passwd_Exception(
|
||||
"User '$user' already exists in realm '$realm'.", 0
|
||||
);
|
||||
}
|
||||
if (!preg_match($this->_pcre, $user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
if (!preg_match($this->_pcre, $realm)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'Realm ', $realm),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
$this->_users[$user][$realm] = md5("$user:$realm:$pass");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* List all user of (a | all) realm(s)
|
||||
*
|
||||
* Returns:
|
||||
* o associative array of users of ONE realm if $inRealm was supplied
|
||||
* <pre>
|
||||
* realm1
|
||||
* + user1 => pass
|
||||
* + user2 => pass
|
||||
* + user3 => pass
|
||||
* </pre>
|
||||
* o associative array of all realms with all users
|
||||
* <pre>
|
||||
* array
|
||||
* + realm1 => array
|
||||
* + user1 => pass
|
||||
* + user2 => pass
|
||||
* + user3 => pass
|
||||
* + realm2 => array
|
||||
* + user3 => pass
|
||||
* + realm3 => array
|
||||
* + user1 => pass
|
||||
* + user2 => pass
|
||||
* </pre>
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
* @param string $inRealm the realm to list users of;
|
||||
* if omitted, you'll get all realms
|
||||
*/
|
||||
function listUserInRealm($inRealm = '')
|
||||
{
|
||||
$result = array();
|
||||
foreach ($this->_users as $user => $realms){
|
||||
foreach ($realms as $realm => $pass){
|
||||
if (!empty($inRealm) && ($inRealm !== $realm)) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($result[$realm])) {
|
||||
$result[$realm] = array();
|
||||
}
|
||||
$result[$realm][$user] = $pass;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the password of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user doesn't exist in the supplied realm
|
||||
* o user or realm contains illegal characters
|
||||
*
|
||||
* This method in fact adds the user whith the new password
|
||||
* after deleting the user.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or a PEAR_Error
|
||||
* @param string $user the user whose password should be changed
|
||||
* @param string $realm the realm the user is in
|
||||
* @param string $pass the new plaintext password
|
||||
*/
|
||||
function changePasswd($user, $realm, $pass)
|
||||
{
|
||||
$this->delUserInRealm($user, $realm);
|
||||
|
||||
return $this->addUser($user, $realm, $pass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifiy password
|
||||
*
|
||||
* Returns a PEAR_Error if the user doesn't exist in the supplied realm.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true if passwords equal, false if they don't, or PEAR_Error
|
||||
* @param string $user the user whose password should be verified
|
||||
* @param string $realm the realm the user is in
|
||||
* @param string $pass the plaintext password to verify
|
||||
*/
|
||||
function verifyPasswd($user, $realm, $pass)
|
||||
{
|
||||
if (!$this->userInRealm($user, $realm)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_USER_NOT_IN_REALM_STR, $user, $realm),
|
||||
FILE_PASSWD_E_USER_NOT_IN_REALM
|
||||
);
|
||||
}
|
||||
return ($this->_users[$user][$realm] === md5("$user:$realm:$pass"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ckeck if a certain user is in a specific realm
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return boolean
|
||||
* @param string $user the user to check
|
||||
* @param string $realm the realm the user shuold be in
|
||||
*/
|
||||
function userInRealm($user, $realm)
|
||||
{
|
||||
return (isset($this->_users[$user][$realm]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a certain user in a specific realm
|
||||
*
|
||||
* Returns a PEAR_Error if <var>$user</var> doesn't exist <var>$inRealm</var>.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
* @param string $user the user to remove
|
||||
* @param string $inRealm the realm the user should be in
|
||||
*/
|
||||
function delUserInRealm($user, $inRealm)
|
||||
{
|
||||
if (!$this->userInRealm($user, $inRealm)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_USER_NOT_IN_REALM_STR, $user, $inRealm),
|
||||
FILE_PASSWD_E_USER_NOT_IN_REALM
|
||||
);
|
||||
}
|
||||
unset($this->_users[$user][$inRealm]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the AuthDigestFile
|
||||
*
|
||||
* Returns a PEAR_Error if AuthDigestFile has invalid format.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function parse()
|
||||
{
|
||||
$this->_users = array();
|
||||
foreach ($this->_contents as $line) {
|
||||
$user = explode(':', $line);
|
||||
if (count($user) != 3) {
|
||||
throw new File_Passwd_Exception(
|
||||
FILE_PASSWD_E_INVALID_FORMAT_STR,
|
||||
FILE_PASSWD_E_INVALID_FORMAT
|
||||
);
|
||||
}
|
||||
list($user, $realm, $pass) = $user;
|
||||
$this->_users[$user][$realm] = trim($pass);
|
||||
}
|
||||
$this->_contents = array();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Password
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return string The crypted password.
|
||||
* @param string $user The username.
|
||||
* @param string $realm The realm the user is in.
|
||||
* @param string $pass The plaintext password.
|
||||
*/
|
||||
function generatePasswd($user, $realm, $pass)
|
||||
{
|
||||
return md5("$user:$realm:$pass");
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
* @deprecated
|
||||
*/
|
||||
function generatePassword($user, $realm, $pass)
|
||||
{
|
||||
return File_Passwd_Authdigest::generatePasswd($user, $realm, $pass);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,370 @@
|
||||
<?php
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* File::Passwd::Common
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category FileFormats
|
||||
* @package File_Passwd
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @copyright 2003-2005 Michael Wallner
|
||||
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
||||
* @version CVS: $Id$
|
||||
* @link http://pear.php.net/package/File_Passwd
|
||||
*/
|
||||
|
||||
/**
|
||||
* Requires System
|
||||
*/
|
||||
require_once 'System.php';
|
||||
/**
|
||||
* Requires File::Passwd
|
||||
*/
|
||||
require_once 'File/Passwd.php';
|
||||
|
||||
/**
|
||||
* Baseclass for File_Passwd_* classes.
|
||||
*
|
||||
* <kbd><u>
|
||||
* Provides basic operations:
|
||||
* </u></kbd>
|
||||
* o opening & closing
|
||||
* o locking & unlocking
|
||||
* o loading & saving
|
||||
* o check if user exist
|
||||
* o delete a certain user
|
||||
* o list users
|
||||
*
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @package File_Passwd
|
||||
* @version $Revision$
|
||||
* @access protected
|
||||
* @internal extend this class for your File_Passwd_* class
|
||||
*/
|
||||
class File_Passwd_Common
|
||||
{
|
||||
/**
|
||||
* passwd file
|
||||
*
|
||||
* @var string
|
||||
* @access protected
|
||||
*/
|
||||
var $_file = 'passwd';
|
||||
|
||||
/**
|
||||
* file content
|
||||
*
|
||||
* @var aray
|
||||
* @access protected
|
||||
*/
|
||||
var $_contents = array();
|
||||
|
||||
/**
|
||||
* users
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
var $_users = array();
|
||||
|
||||
/**
|
||||
* PCRE for valid chars
|
||||
*
|
||||
* @var string
|
||||
* @access protected
|
||||
*/
|
||||
var $_pcre = '/^[a-z]+[a-z0-9_-]*$/i';
|
||||
|
||||
/**
|
||||
* Constructor (ZE2)
|
||||
*
|
||||
* @access protected
|
||||
* @param string $file path to passwd file
|
||||
*/
|
||||
function __construct($file = 'passwd')
|
||||
{
|
||||
$this->setFile($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the content of the file
|
||||
*
|
||||
* You must overwrite this method in your File_Passwd_* class.
|
||||
*
|
||||
* @abstract
|
||||
* @internal
|
||||
* @access public
|
||||
* @return object PEAR_Error
|
||||
*/
|
||||
function parse()
|
||||
{
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED_STR, 'parse'),
|
||||
FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply changes and rewrite passwd file
|
||||
*
|
||||
* You must overwrite this method in your File_Passwd_* class.
|
||||
*
|
||||
* @abstract
|
||||
* @internal
|
||||
* @access public
|
||||
* @return object PEAR_Error
|
||||
*/
|
||||
function save()
|
||||
{
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED_STR, 'save'),
|
||||
FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a file, locks it exclusively and returns the filehandle
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o directory in which the file should reside couldn't be created
|
||||
* o file couldn't be opened in the desired mode
|
||||
* o file couldn't be locked exclusively
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access protected
|
||||
* @return mixed resource of type file handle or PEAR_Error
|
||||
* @param string $mode the mode to open the file with
|
||||
*/
|
||||
function &_open($mode, $file = null)
|
||||
{
|
||||
isset($file) or $file = $this->_file;
|
||||
$dir = dirname($file);
|
||||
$lock = strstr($mode, 'r') ? LOCK_SH : LOCK_EX;
|
||||
if (!is_dir($dir) && !System::mkDir('-p -m 0755 ' . $dir)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_DIR_NOT_CREATED_STR, $dir),
|
||||
FILE_PASSWD_E_DIR_NOT_CREATED
|
||||
);
|
||||
}
|
||||
if (!is_resource($fh = @fopen($file, $mode))) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_FILE_NOT_OPENED_STR, $file),
|
||||
FILE_PASSWD_E_FILE_NOT_OPENED
|
||||
);
|
||||
}
|
||||
if (!@flock($fh, $lock)) {
|
||||
fclose($fh);
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_FILE_NOT_LOCKED_STR, $file),
|
||||
FILE_PASSWD_E_FILE_NOT_LOCKED
|
||||
);
|
||||
}
|
||||
return $fh;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes a prior opened and locked file handle
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o file couldn't be unlocked
|
||||
* o file couldn't be closed
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access protected
|
||||
* @return mixed true on success or PEAR_Error
|
||||
* @param resource $file_handle the file handle to operate on
|
||||
*/
|
||||
function _close($file_handle)
|
||||
{
|
||||
if (!@flock($file_handle, LOCK_UN)) {
|
||||
throw new File_Passwd_Exception(
|
||||
FILE_PASSWD_E_FILE_NOT_UNLOCKED_STR,
|
||||
FILE_PASSWD_E_FILE_NOT_UNLOCKED
|
||||
);
|
||||
}
|
||||
if (!@fclose($file_handle)) {
|
||||
throw new File_Passwd_Exception(
|
||||
FILE_PASSWD_E_FILE_NOT_CLOSED_STR,
|
||||
FILE_PASSWD_E_FILE_NOT_CLOSED
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the file
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o directory in which the file should reside couldn't be created
|
||||
* o file couldn't be opened in read mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked
|
||||
* o file couldn't be closed
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function load()
|
||||
{
|
||||
$fh = $this->_open('r');
|
||||
|
||||
$this->_contents = array();
|
||||
while ($line = fgets($fh)) {
|
||||
if (!preg_match('/^\s*#/', $line) && $line = trim($line)) {
|
||||
$this->_contents[] = $line;
|
||||
}
|
||||
}
|
||||
$this->_close($fh);
|
||||
|
||||
return $this->parse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the modified content to the passwd file
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o directory in which the file should reside couldn't be created
|
||||
* o file couldn't be opened in write mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked
|
||||
* o file couldn't be closed
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access protected
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function _save($content)
|
||||
{
|
||||
$fh = $this->_open('w');
|
||||
|
||||
fputs($fh, $content);
|
||||
return $this->_close($fh);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set path to passwd file
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function setFile($file)
|
||||
{
|
||||
$this->_file = $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get path of passwd file
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function getFile()
|
||||
{
|
||||
return $this->_file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a certain user already exists
|
||||
*
|
||||
* @access public
|
||||
* @return bool
|
||||
* @param string $user the name of the user to check if already exists
|
||||
*/
|
||||
function userExists($user)
|
||||
{
|
||||
return isset($this->_users[$user]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if user doesn't exist.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
* @param string
|
||||
*/
|
||||
function delUser($user)
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
unset($this->_users[$user]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* List user
|
||||
*
|
||||
* Returns a PEAR_Error if <var>$user</var> doesn't exist.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed array of a/all user(s) or PEAR_Error
|
||||
* @param string $user the user to list or all users if empty
|
||||
*/
|
||||
function listUser($user = '')
|
||||
{
|
||||
if (empty($user)) {
|
||||
return $this->_users;
|
||||
}
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
return $this->_users[$user];
|
||||
}
|
||||
|
||||
/**
|
||||
* Base method for File_Passwd::staticAuth()
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o file doesn't exist
|
||||
* o file couldn't be opened in read mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked (only if auth fails)
|
||||
* o file couldn't be closed (only if auth fails)
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access protected
|
||||
* @return mixed line of passwd file containing <var>$id</var>,
|
||||
* false if <var>$id</var> wasn't found or PEAR_Error
|
||||
* @param string $file path to passwd file
|
||||
* @param string $id user_id to search for
|
||||
* @param string $sep field separator
|
||||
*/
|
||||
function _auth($file, $id, $sep = ':')
|
||||
{
|
||||
$file = realpath($file);
|
||||
if (!is_file($file)) {
|
||||
throw new File_Passwd_Exception("File '$file' couldn't be found.", 0);
|
||||
}
|
||||
$fh = File_Passwd_Common::_open('r', $file);
|
||||
|
||||
$cmp = $id . $sep;
|
||||
$len = strlen($cmp);
|
||||
while ($line = fgets($fh)) {
|
||||
if (!strncmp($line, $cmp, $len)) {
|
||||
File_Passwd_Common::_close($fh);
|
||||
return trim($line);
|
||||
}
|
||||
}
|
||||
File_Passwd_Common::_close($fh);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,587 @@
|
||||
<?php
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* File::Passwd::Custom
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category FileFormats
|
||||
* @package File_Passwd
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @copyright 2003-2005 Michael Wallner
|
||||
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
||||
* @version CVS: $Id$
|
||||
* @link http://pear.php.net/package/File_Passwd
|
||||
*/
|
||||
|
||||
/**
|
||||
* Requires File::Passwd::Common
|
||||
*/
|
||||
require_once 'File/Passwd/Common.php';
|
||||
|
||||
/**
|
||||
* Manipulate custom formatted passwd files
|
||||
*
|
||||
* Usage Example:
|
||||
* <code>
|
||||
* $cust = &File_Passwd::factory('Custom');
|
||||
* $cust->setDelim('|');
|
||||
* $cust->load();
|
||||
* $cust->setEncFunc(array('File_Passwd', 'crypt_apr_md5'));
|
||||
* $cust->addUser('mike', 'pass');
|
||||
* $cust->save();
|
||||
* </code>
|
||||
*
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @version $Revision$
|
||||
* @access public
|
||||
*/
|
||||
class File_Passwd_Custom extends File_Passwd_Common
|
||||
{
|
||||
/**
|
||||
* Delimiter
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
var $_delim = ':';
|
||||
|
||||
/**
|
||||
* Encryption function
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
var $_enc = array('File_Passwd', 'crypt_md5');
|
||||
|
||||
/**
|
||||
* 'name map'
|
||||
*
|
||||
* @access private
|
||||
* @var array
|
||||
*/
|
||||
var $_map = array();
|
||||
|
||||
/**
|
||||
* Whether to use the 'name map' or not
|
||||
*
|
||||
* @var boolean
|
||||
* @access private
|
||||
*/
|
||||
var $_usemap = false;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access protected
|
||||
* @return object
|
||||
*/
|
||||
function File_Passwd_Custom($file = 'passwd')
|
||||
{
|
||||
$this->__construct($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast authentication of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o file doesn't exist
|
||||
* o file couldn't be opened in read mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked (only if auth fails)
|
||||
* o file couldn't be closed (only if auth fails)
|
||||
* o invalid encryption function <var>$opts[0]</var>,
|
||||
* or no delimiter character <var>$opts[1]</var> was provided
|
||||
*
|
||||
* @throws PEAR_Error FILE_PASSWD_E_UNDEFINED |
|
||||
* FILE_PASSWD_E_FILE_NOT_OPENED |
|
||||
* FILE_PASSWD_E_FILE_NOT_LOCKED |
|
||||
* FILE_PASSWD_E_FILE_NOT_UNLOCKED |
|
||||
* FILE_PASSWD_E_FILE_NOT_CLOSED |
|
||||
* FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
* @static call this method statically for a reasonable fast authentication
|
||||
* @access public
|
||||
* @return mixed Returns &true; if authenticated, &false; if not or
|
||||
* <classname>PEAR_Error</classname> on failure.
|
||||
* @param string $file path to passwd file
|
||||
* @param string $user user to authenticate
|
||||
* @param string $pass plaintext password
|
||||
* @param array $otps encryption function and delimiter charachter
|
||||
* (in this order)
|
||||
*/
|
||||
function staticAuth($file, $user, $pass, $opts)
|
||||
{
|
||||
setType($opts, 'array');
|
||||
if (count($opts) != 2 || empty($opts[1])) {
|
||||
throw new File_Passwd_Exception('Insufficient options.', 0);
|
||||
}
|
||||
|
||||
$line = File_Passwd_Common::_auth($file, $user, $opts[1]);
|
||||
|
||||
if (!$line) {
|
||||
return $line;
|
||||
}
|
||||
|
||||
list(,$real)= explode($opts[1], $line);
|
||||
$crypted = File_Passwd_Custom::_genPass($pass, $real, $opts[0]);
|
||||
|
||||
return ($crypted === $real);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set delimiter
|
||||
*
|
||||
* You can set a custom char to delimit the columns of a data set.
|
||||
* Defaults to a colon (':'). Be aware that this char mustn't be
|
||||
* in the values of your data sets.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
* @param string $delim custom delimiting character
|
||||
*/
|
||||
function setDelim($delim = ':')
|
||||
{
|
||||
@setType($delim, 'string');
|
||||
if (empty($delim)) {
|
||||
$this->_delim = ':';
|
||||
} else {
|
||||
$this->_delim = $delim{0};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom delimiter
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function getDelim()
|
||||
{
|
||||
return $this->_delim;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set encryption function
|
||||
*
|
||||
* You can set a custom encryption function to use.
|
||||
* The supplied function will be called by php's call_user_function(),
|
||||
* so you can supply an array with a method of a class/object, too
|
||||
* (i.e. array('File_Passwd', 'crypt_apr_md5').
|
||||
*
|
||||
*
|
||||
* @throws PEAR_Error FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
* @access public
|
||||
* @return mixed Returns &true; on success or
|
||||
* <classname>PEAR_Error</classname> on failure.
|
||||
* @param mixed $function callable encryption function
|
||||
*/
|
||||
function setEncFunc($function = array('File_Passwd', 'crypt_md5'))
|
||||
{
|
||||
if (!is_callable($function)) {
|
||||
if (is_array($function)) {
|
||||
$function = implode('::', $function);
|
||||
}
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $function),
|
||||
FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
);
|
||||
}
|
||||
|
||||
$this->_enc = $function;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current custom encryption method
|
||||
*
|
||||
* Possible return values (examples):
|
||||
* o 'md5'
|
||||
* o 'File_Passwd::crypt_md5'
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function getEncFunc()
|
||||
{
|
||||
if (is_array($this->_enc)) {
|
||||
return implode('::', $this->_enc);
|
||||
}
|
||||
return $this->_enc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to use the 'name map' of the extra properties or not
|
||||
*
|
||||
* @see File_Passwd_Custom::useMap()
|
||||
* @see setMap()
|
||||
* @see getMap()
|
||||
*
|
||||
* @access public
|
||||
* @return boolean always true if you set a value (true/false) OR
|
||||
* the actual value if called without param
|
||||
*
|
||||
* @param boolean $bool whether to use the 'name map' or not
|
||||
*/
|
||||
function useMap($bool = null)
|
||||
{
|
||||
if (is_null($bool)) {
|
||||
return $this->_usemap;
|
||||
}
|
||||
$this->_usemap = (bool) $bool;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the 'name map' to use with the extra properties of the user
|
||||
*
|
||||
* This map is used for naming the associative array of the extra properties.
|
||||
*
|
||||
* Returns a PEAR_Error if <var>$map</var> was not of type array.
|
||||
*
|
||||
* @see getMap()
|
||||
* @see useMap()
|
||||
*
|
||||
* @throws PEAR_Error FILE_PASSWD_E_PARAM_MUST_BE_ARRAY
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function setMap($map = array())
|
||||
{
|
||||
if (!is_array($map)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_PARAM_MUST_BE_ARRAY_STR, '$map'),
|
||||
FILE_PASSWD_E_PARAM_MUST_BE_ARRAY
|
||||
);
|
||||
}
|
||||
$this->_map = $map;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the 'name map' which is used for the extra properties of the user
|
||||
*
|
||||
* @see setMap()
|
||||
* @see useMap()
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
function getMap()
|
||||
{
|
||||
return $this->_map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply changes an rewrite passwd file
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o directory in which the file should reside couldn't be created
|
||||
* o file couldn't be opened in write mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked
|
||||
* o file couldn't be closed
|
||||
*
|
||||
* @throws PEAR_Error FILE_PASSWD_E_FILE_NOT_OPENED |
|
||||
* FILE_PASSWD_E_FILE_NOT_LOCKED |
|
||||
* FILE_PASSWD_E_FILE_NOT_UNLOCKED |
|
||||
* FILE_PASSWD_E_FILE_NOT_CLOSED
|
||||
* @access public
|
||||
* @return mixed Returns &true; on success or
|
||||
* <classname>PEAR_Error</classname> on failure.
|
||||
*/
|
||||
function save()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->_users as $user => $array){
|
||||
$pass = array_shift($array);
|
||||
$extra = implode($this->_delim, $array);
|
||||
$content .= $user . $this->_delim . $pass;
|
||||
if (!empty($extra)) {
|
||||
$content .= $this->_delim . $extra;
|
||||
}
|
||||
$content .= "\n";
|
||||
}
|
||||
return $this->_save($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the Custom password file
|
||||
*
|
||||
* Returns a PEAR_Error if passwd file has invalid format.
|
||||
*
|
||||
* @throws PEAR_Error FILE_PASSWD_E_INVALID_FORMAT
|
||||
* @access public
|
||||
* @return mixed Returns &true; on success or
|
||||
* <classname>PEAR_Error</classname> on failure.
|
||||
*/
|
||||
function parse()
|
||||
{
|
||||
$this->_users = array();
|
||||
foreach ($this->_contents as $line){
|
||||
$parts = explode($this->_delim, $line);
|
||||
if (count($parts) < 2) {
|
||||
throw new File_Passwd_Exception(
|
||||
FILE_PASSWD_E_INVALID_FORMAT_STR,
|
||||
FILE_PASSWD_E_INVALID_FORMAT
|
||||
);
|
||||
}
|
||||
$user = array_shift($parts);
|
||||
$pass = array_shift($parts);
|
||||
$values = array();
|
||||
if ($this->_usemap) {
|
||||
$values['pass'] = $pass;
|
||||
foreach ($parts as $i => $value){
|
||||
if (isset($this->_map[$i])) {
|
||||
$values[$this->_map[$i]] = $value;
|
||||
} else {
|
||||
$values[$i+1] = $value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$values = array_merge(array($pass), $parts);
|
||||
}
|
||||
$this->_users[$user] = $values;
|
||||
|
||||
}
|
||||
$this->_contents = array();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an user
|
||||
*
|
||||
* The username must start with an alphabetical character and must NOT
|
||||
* contain any other characters than alphanumerics, the underline and dash.
|
||||
*
|
||||
* If you use the 'name map' you should also use these naming in
|
||||
* the supplied extra array, because your values would get mixed up
|
||||
* if they are in the wrong order, which is always true if you
|
||||
* DON'T use the 'name map'!
|
||||
*
|
||||
* So be warned and USE the 'name map'!
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user already exists
|
||||
* o user contains illegal characters
|
||||
* o encryption mode is not supported
|
||||
* o any element of the <var>$extra</var> array contains the delimiter char
|
||||
*
|
||||
* @throws PEAR_Error FILE_PASSWD_E_EXISTS_ALREADY |
|
||||
* FILE_PASSWD_E_INVALID_ENC_MODE |
|
||||
* FILE_PASSWD_E_INVALID_CHARS
|
||||
* @access public
|
||||
* @return mixed Returns &true; on success or
|
||||
* <classname>PEAR_Error</classname> on failure.
|
||||
* @param string $user the name of the user to add
|
||||
* @param string $pass the password of the user to add
|
||||
* @param array $extra extra properties of user to add
|
||||
*/
|
||||
function addUser($user, $pass, $extra = array())
|
||||
{
|
||||
if ($this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_ALREADY
|
||||
);
|
||||
}
|
||||
if (!preg_match($this->_pcre, $user) || strstr($user, $this->_delim)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
if (!is_array($extra)) {
|
||||
setType($extra, 'array');
|
||||
}
|
||||
foreach ($extra as $e){
|
||||
if (strstr($e, $this->_delim)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'Property ', $e),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$pass = $this->_genPass($pass);
|
||||
|
||||
/**
|
||||
* If you don't use the 'name map' the user array will be numeric.
|
||||
*/
|
||||
if (!$this->_usemap) {
|
||||
array_unshift($extra, $pass);
|
||||
$this->_users[$user] = $extra;
|
||||
} else {
|
||||
$map = $this->_map;
|
||||
array_unshift($map, 'pass');
|
||||
$extra['pass'] = $pass;
|
||||
foreach ($map as $key){
|
||||
$this->_users[$user][$key] = @$extra[$key];
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify properties of a certain user
|
||||
*
|
||||
* # DON'T MODIFY THE PASSWORD WITH THIS METHOD!
|
||||
*
|
||||
* You should use this method only if the 'name map' is used, too.
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user doesn't exist
|
||||
* o any property contains the custom delimiter character
|
||||
*
|
||||
* @see changePasswd()
|
||||
*
|
||||
* @throws PEAR_Error FILE_PASSWD_E_EXISTS_NOT |
|
||||
* FILE_PASSWD_E_INVALID_CHARS
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
* @param string $user the user to modify
|
||||
* @param array $properties an associative array of
|
||||
* properties to modify
|
||||
*/
|
||||
function modUser($user, $properties = array())
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
|
||||
if (!is_array($properties)) {
|
||||
setType($properties, 'array');
|
||||
}
|
||||
|
||||
foreach ($properties as $key => $value){
|
||||
if (strstr($value, $this->_delim)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
$this->_users[$user][$key] = $value;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the password of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user doesn't exists
|
||||
* o encryption mode is not supported
|
||||
*
|
||||
* @throws PEAR_Error FILE_PASSWD_E_EXISTS_NOT |
|
||||
* FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
* @access public
|
||||
* @return mixed Returns &true; on success or
|
||||
* <classname>PEAR_Error</classname> on failure.
|
||||
* @param string $user the user whose password should be changed
|
||||
* @param string $pass the new plaintext password
|
||||
*/
|
||||
function changePasswd($user, $pass)
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
|
||||
$pass = $this->_genPass($pass);
|
||||
|
||||
if ($this->_usemap) {
|
||||
$this->_users[$user]['pass'] = $pass;
|
||||
} else {
|
||||
$this->_users[$user][0] = $pass;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the password of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user doesn't exist
|
||||
* o encryption mode is not supported
|
||||
*
|
||||
* @throws PEAR_Error FILE_PASSWD_E_EXISTS_NOT |
|
||||
* FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
* @access public
|
||||
* @return mixed Returns &true; if passwors equal, &false; if they don't
|
||||
* or <classname>PEAR_Error</classname> on fialure.
|
||||
* @param string $user the user whose password should be verified
|
||||
* @param string $pass the password to verify
|
||||
*/
|
||||
function verifyPasswd($user, $pass)
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
$real =
|
||||
$this->_usemap ?
|
||||
$this->_users[$user]['pass'] :
|
||||
$this->_users[$user][0]
|
||||
;
|
||||
return ($real === $this->_genPass($pass, $real));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate crypted password from the plaintext password
|
||||
*
|
||||
* Returns a PEAR_Error if actual encryption mode is not supported.
|
||||
*
|
||||
* @throws PEAR_Error FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
* @access private
|
||||
* @return mixed Returns the crypted password or
|
||||
* <classname>PEAR_Error</classname>
|
||||
* @param string $pass the plaintext password
|
||||
* @param string $salt the crypted password from which to gain the salt
|
||||
* @param string $func the encryption function to use
|
||||
*/
|
||||
function _genPass($pass, $salt = null, $func = null)
|
||||
{
|
||||
if (is_null($func)) {
|
||||
$func = $this->_enc;
|
||||
}
|
||||
|
||||
if (!is_callable($func)) {
|
||||
if (is_array($func)) {
|
||||
$func = implode('::', $func);
|
||||
}
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $func),
|
||||
FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
);
|
||||
}
|
||||
|
||||
if ($func === 'md5') {
|
||||
$salt = null;
|
||||
}
|
||||
|
||||
$return = @call_user_func($func, $pass, $salt);
|
||||
|
||||
if (is_null($return) || $return === false) {
|
||||
$return = @call_user_func($func, $pass);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,304 @@
|
||||
<?php
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* File::Passwd::Cvs
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category FileFormats
|
||||
* @package File_Passwd
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @copyright 2003-2005 Michael Wallner
|
||||
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
||||
* @version CVS: $Id$
|
||||
* @link http://pear.php.net/package/File_Passwd
|
||||
*/
|
||||
|
||||
/**
|
||||
* Requires File::Passwd::Common
|
||||
*/
|
||||
require_once 'File/Passwd/Common.php';
|
||||
|
||||
/**
|
||||
* Manipulate CVS pserver passwd files.
|
||||
*
|
||||
* <kbd><u>
|
||||
* A line of a CVS pserver passwd file consists of 2 to 3 colums:
|
||||
* </u></kbd>
|
||||
* <pre>
|
||||
* user1:1HCoDDWxK9tbM:sys_user1
|
||||
* user2:0O0DYYdzjCVxs
|
||||
* user3:MIW9UUoifhqRo:sys_user2
|
||||
* </pre>
|
||||
*
|
||||
* If the third column is specified, the CVS user named in the first column is
|
||||
* mapped to the corresponding system user named in the third column.
|
||||
* That doesn't really affect us - just for your interest :)
|
||||
*
|
||||
* <kbd><u>Output of listUser()</u></kbd>
|
||||
* <pre>
|
||||
* array
|
||||
* + user => array
|
||||
* + passwd => crypted_passwd
|
||||
* + system => system_user
|
||||
* + user => array
|
||||
* + passwd => crypted_passwd
|
||||
* + system => system_user
|
||||
* </pre>
|
||||
*
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @package File_Passwd
|
||||
* @version $Revision$
|
||||
* @access public
|
||||
*/
|
||||
class File_Passwd_Cvs extends File_Passwd_Common
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function File_Passwd_Cvs($file = 'passwd')
|
||||
{
|
||||
parent::__construct($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast authentication of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o file doesn't exist
|
||||
* o file couldn't be opened in read mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked (only if auth fails)
|
||||
* o file couldn't be closed (only if auth fails)
|
||||
*
|
||||
* @static call this method statically for a reasonable fast authentication
|
||||
* @access public
|
||||
* @return mixed true if authenticated, false if not or PEAR_Error
|
||||
* @param string $file path to passwd file
|
||||
* @param string $user user to authenticate
|
||||
* @param string $pass plaintext password
|
||||
*/
|
||||
function staticAuth($file, $user, $pass)
|
||||
{
|
||||
$line = File_Passwd_Common::_auth($file, $user);
|
||||
if (!$line) {
|
||||
return $line;
|
||||
}
|
||||
@list(,$real) = explode(':', $line);
|
||||
return (File_Passwd_Cvs::generatePassword($pass, $real) === $real);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply changes and rewrite CVS passwd file
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o directory in which the file should reside couldn't be created
|
||||
* o file couldn't be opened in write mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked
|
||||
* o file couldn't be closed
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function save()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->_users as $user => $v){
|
||||
$content .= $user . ':' . $v['passwd'];
|
||||
if (isset($v['system']) && !empty($v['system'])) {
|
||||
$content .= ':' . $v['system'];
|
||||
}
|
||||
$content .= "\n";
|
||||
}
|
||||
return $this->_save($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the CVS passwd file
|
||||
*
|
||||
* Returns a PEAR_Error if passwd file has invalid format.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function parse()
|
||||
{
|
||||
$this->_users = array();
|
||||
foreach ($this->_contents as $line) {
|
||||
$user = explode(':', $line);
|
||||
if (count($user) < 2) {
|
||||
throw new File_Passwd_Exception(
|
||||
FILE_PASSWD_E_INVALID_FORMAT_STR,
|
||||
FILE_PASSWD_E_INVALID_FORMAT
|
||||
);
|
||||
}
|
||||
@list($user, $pass, $system) = $user;
|
||||
$this->_users[$user]['passwd'] = $pass;
|
||||
if (!empty($system)) {
|
||||
$this->_users[$user]['system'] = $system;
|
||||
}
|
||||
}
|
||||
$this->_contents = array();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an user
|
||||
*
|
||||
* The username must start with an alphabetical character and must NOT
|
||||
* contain any other characters than alphanumerics, the underline and dash.
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user already exists
|
||||
* o user or system_user contains illegal characters
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
* @param string $user the name of the user to add
|
||||
* @param string $pass the password of the user tot add
|
||||
* @param string $system_user the systemuser this user maps to
|
||||
*/
|
||||
function addUser($user, $pass, $system_user = '')
|
||||
{
|
||||
if ($this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_ALREADY
|
||||
);
|
||||
}
|
||||
if (!preg_match($this->_pcre, $user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
@setType($system_user, 'string');
|
||||
if (!empty($system_user) && !preg_match($this->_pcre, $system_user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(
|
||||
FILE_PASSWD_E_INVALID_CHARS_STR,
|
||||
'System user ',
|
||||
$system_user
|
||||
),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
$this->_users[$user]['passwd'] = $this->generatePassword($pass);
|
||||
$this->_users[$user]['system'] = $system_user;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the password of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if the user doesn't exist.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true if passwords equal, false ifthe don't or PEAR_Error
|
||||
* @param string $user user whose password should be verified
|
||||
* @param string $pass the plaintext password that should be verified
|
||||
*/
|
||||
function verifyPasswd($user, $pass)
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
$real = $this->_users[$user]['passwd'];
|
||||
return ($real === $this->generatePassword($pass, $real));
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the password of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if user doesn't exist.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function changePasswd($user, $pass)
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
$this->_users[$user]['passwd'] = $this->generatePassword($pass);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the corresponding system user of a certain cvs user
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user doesn't exist
|
||||
* o system user contains illegal characters
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function changeSysUser($user, $system)
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
if (!preg_match($this->_pcre, $system)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(
|
||||
FILE_PASSWD_E_INVALID_CHARS_STR,
|
||||
'System user ',
|
||||
$system_user
|
||||
),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
$this->_users[$user]['system'] = $system;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate crypted password
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return string the crypted password
|
||||
* @param string $pass new plaintext password
|
||||
* @param string $salt new crypted password from which to gain the salt
|
||||
*/
|
||||
function generatePasswd($pass, $salt = null)
|
||||
{
|
||||
return File_Passwd::crypt_des($pass, $salt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
* @deprecated
|
||||
*/
|
||||
function generatePassword($pass, $salt = null)
|
||||
{
|
||||
return File_Passwd_Cvs::generatePasswd($pass, $salt);
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
class File_Passwd_Exception extends Exception {}
|
||||
@@ -0,0 +1,415 @@
|
||||
<?php
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* File::Passwd::Smb
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category FileFormats
|
||||
* @package File_Passwd
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @author Michael Bretterklieber <michael@bretterklieber.com>
|
||||
* @copyright 2003-2005 Michael Wallner
|
||||
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
||||
* @version CVS: $Id$
|
||||
* @link http://pear.php.net/package/File_Passwd
|
||||
*/
|
||||
|
||||
/**
|
||||
* Requires File::Passwd::Common
|
||||
*/
|
||||
require_once 'File/Passwd/Common.php';
|
||||
|
||||
/**
|
||||
* Requires Crypt::CHAP
|
||||
*/
|
||||
require_once 'Crypt/CHAP.php';
|
||||
|
||||
/**
|
||||
* Manipulate SMB server passwd files.
|
||||
*
|
||||
* # Usage Example 1 (modifying existing file):
|
||||
* <code>
|
||||
* $f = &File_Passwd::factory('SMB');
|
||||
* $f->setFile('./smbpasswd');
|
||||
* $f->load();
|
||||
* $f->addUser('sepp3', 'MyPw', array('userid' => 12));
|
||||
* $f->changePasswd('sepp', 'MyPw');
|
||||
* $f->delUser('karli');
|
||||
* foreach($f->listUser() as $user => $data) {
|
||||
* echo $user . ':' . implode(':', $data) ."\n";
|
||||
* }
|
||||
* $f->save();
|
||||
* </code>
|
||||
*
|
||||
* # Usage Example 2 (creating a new file):
|
||||
* <code>
|
||||
* $f = &File_Passwd::factory('SMB');
|
||||
* $f->setFile('./smbpasswd');
|
||||
* $f->addUser('sepp1', 'MyPw', array('userid'=> 12));
|
||||
* $f->addUser('sepp3', 'MyPw', array('userid' => 1000));
|
||||
* $f->save();
|
||||
* </code>
|
||||
*
|
||||
* # Usage Example 3 (authentication):
|
||||
* <code>
|
||||
* $f = &File_Passwd::factory('SMB');
|
||||
* $f->setFile('./smbpasswd');
|
||||
* $f->load();
|
||||
* if (true === $f->verifyPasswd('sepp', 'MyPw')) {
|
||||
* echo "User valid";
|
||||
* } else {
|
||||
* echo "User invalid or disabled";
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @author Michael Bretterklieber <michael@bretterklieber.com>
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @package File_Passwd
|
||||
* @version $Revision$
|
||||
* @access public
|
||||
*/
|
||||
class File_Passwd_Smb extends File_Passwd_Common
|
||||
{
|
||||
/**
|
||||
* Object which generates the NT-Hash and LAN-Manager-Hash passwds
|
||||
*
|
||||
* @access protected
|
||||
* @var object
|
||||
*/
|
||||
var $msc;
|
||||
|
||||
/**
|
||||
* Constructor (ZE2)
|
||||
*
|
||||
* Rewritten because we want to init our crypt engine.
|
||||
*
|
||||
* @access public
|
||||
* @param string $file SMB passwd file
|
||||
*/
|
||||
function __construct($file = 'smbpasswd')
|
||||
{
|
||||
$this->setFile($file);
|
||||
$this->msc = new Crypt_CHAP_MSv1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast authentication of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o file doesn't exist
|
||||
* o file couldn't be opened in read mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked (only if auth fails)
|
||||
* o file couldn't be closed (only if auth fails)
|
||||
* o invalid encryption method <var>$nt_or_lm</var> was provided
|
||||
*
|
||||
* @static call this method statically for a reasonable fast authentication
|
||||
* @access public
|
||||
* @return mixed true if authenticated, false if not or PEAR_Error
|
||||
* @param string $file path to passwd file
|
||||
* @param string $user user to authenticate
|
||||
* @param string $pass plaintext password
|
||||
* @param string $nt_or_lm encryption mode to use (NT or LM hash)
|
||||
*/
|
||||
function staticAuth($file, $user, $pass, $nt_or_lm = 'nt')
|
||||
{
|
||||
$line = File_Passwd_Common::_auth($file, $user);
|
||||
if (!$line) {
|
||||
return $line;
|
||||
}
|
||||
@list(,,$lm,$nt) = explode(':', $line);
|
||||
$chap = new Crypt_CHAP_MSv1;
|
||||
|
||||
switch(strToLower($nt_or_lm)){
|
||||
case FILE_PASSWD_NT:
|
||||
$real = $nt;
|
||||
$crypted = $chap->ntPasswordHash($pass);
|
||||
break;
|
||||
case FILE_PASSWD_LM:
|
||||
$real = $lm;
|
||||
$crypted = $chap->lmPasswordHash($pass);
|
||||
break;
|
||||
default:
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $nt_or_lm),
|
||||
FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
);
|
||||
}
|
||||
return (strToUpper(bin2hex($crypted)) === $real);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse smbpasswd file
|
||||
*
|
||||
* Returns a PEAR_Error if passwd file has invalid format.
|
||||
*
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function parse()
|
||||
{
|
||||
foreach ($this->_contents as $line){
|
||||
$info = explode(':', $line);
|
||||
if (count($info) < 4) {
|
||||
throw new File_Passwd_Exception(
|
||||
FILE_PASSWD_E_INVALID_FORMAT_STR,
|
||||
FILE_PASSWD_E_INVALID_FORMAT
|
||||
);
|
||||
}
|
||||
$user = array_shift($info);
|
||||
if (!empty($user)) {
|
||||
array_walk($info, 'trim');
|
||||
$this->_users[$user] = @array(
|
||||
'userid' => $info[0],
|
||||
'lmhash' => $info[1],
|
||||
'nthash' => $info[2],
|
||||
'flags' => $info[3],
|
||||
'lct' => $info[4],
|
||||
'comment' => $info[5]
|
||||
);
|
||||
}
|
||||
}
|
||||
$this->_contents = array();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a user
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user already exists
|
||||
* o user contains illegal characters
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @return mixed true on success or PEAR_Error
|
||||
* @access public
|
||||
* @param string $user the user to add
|
||||
* @param string $pass the new plaintext password
|
||||
* @param array $params additional properties of user
|
||||
* + userid
|
||||
* + comment
|
||||
* @param boolean $isMachine whether to add an machine account
|
||||
*/
|
||||
function addUser($user, $pass, $params, $isMachine = false)
|
||||
{
|
||||
if ($this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_ALREADY
|
||||
);
|
||||
}
|
||||
if (!preg_match($this->_pcre, $user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
if ($isMachine) {
|
||||
$flags = '[W ]';
|
||||
$user .= '$';
|
||||
} else {
|
||||
$flags = '[U ]';
|
||||
}
|
||||
$this->_users[$user] = array(
|
||||
'flags' => $flags,
|
||||
'userid' => (int)@$params['userid'],
|
||||
'comment' => trim(@$params['comment']),
|
||||
'lct' => 'LCT-' . strToUpper(dechex(time()))
|
||||
);
|
||||
return $this->changePasswd($user, $pass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify a certain user
|
||||
*
|
||||
* <b>You should not modify the password with this method
|
||||
* unless it is already encrypted as nthash and lmhash!</b>
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user doesn't exist
|
||||
* o an invalid property was supplied
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
* @param string $user the user to modify
|
||||
* @param array $params an associative array of properties to change
|
||||
*/
|
||||
function modUser($user, $params)
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
if (!preg_match($this->_pcre, $user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
foreach ($params as $key => $value){
|
||||
$key = strToLower($key);
|
||||
if (!isset($this->_users[$user][$key])) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_PROPERTY_STR, $key),
|
||||
FILE_PASSWD_E_INVALID_PROPERTY
|
||||
);
|
||||
}
|
||||
$this->_users[$user][$key] = trim($value);
|
||||
$this->_users[$user]['lct']= 'LCT-' . strToUpper(dechex(time()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the passwd of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if <var>$user</var> doesn't exist.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
* @param string $user the user whose passwd should be changed
|
||||
* @param string $pass the new plaintext passwd
|
||||
*/
|
||||
function changePasswd($user, $pass)
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
if (empty($pass)) {
|
||||
$nthash = $lmhash = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
|
||||
} else {
|
||||
$nthash = strToUpper(bin2hex($this->msc->ntPasswordHash($pass)));
|
||||
$lmhash = strToUpper(bin2hex($this->msc->lmPasswordHash($pass)));
|
||||
}
|
||||
$this->_users[$user]['nthash'] = $nthash;
|
||||
$this->_users[$user]['lmhash'] = $lmhash;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies a user's password
|
||||
*
|
||||
* Prefer NT-Hash instead of weak LAN-Manager-Hash
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user doesn't exist
|
||||
* o user is disabled
|
||||
*
|
||||
* @return mixed true if passwds equal, false if they don't or PEAR_Error
|
||||
* @access public
|
||||
* @param string $user username
|
||||
* @param string $nthash NT-Hash in hex
|
||||
* @param string $lmhash LAN-Manager-Hash in hex
|
||||
*/
|
||||
function verifyEncryptedPasswd($user, $nthash, $lmhash = '')
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
if (strstr($this->_users[$user]['flags'], 'D')) {
|
||||
throw new File_Passwd_Exception("User '$user' is disabled.", 0);
|
||||
}
|
||||
if (!empty($nthash)) {
|
||||
return $this->_users[$user]['nthash'] === strToUpper($nthash);
|
||||
}
|
||||
if (!empty($lmhash)) {
|
||||
return $this->_users[$user]['lm'] === strToUpper($lmhash);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies an account with the given plaintext password
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o user doesn't exist
|
||||
* o user is disabled
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @return mixed true if passwds equal, false if they don't or PEAR_Error
|
||||
* @access public
|
||||
* @param string $user username
|
||||
* @param string $pass the plaintext password
|
||||
*/
|
||||
function verifyPasswd($user, $pass)
|
||||
{
|
||||
$nthash = bin2hex($this->msc->ntPasswordHash($pass));
|
||||
$lmhash = bin2hex($this->msc->lmPasswordHash($pass));
|
||||
return $this->verifyEncryptedPasswd($user, $nthash, $lmhash);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply changes and rewrite CVS passwd file
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o directory in which the file should reside couldn't be created
|
||||
* o file couldn't be opened in write mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked
|
||||
* o file couldn't be closed
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function save()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->_users as $user => $userdata) {
|
||||
$content .= $user . ':' .
|
||||
$userdata['userid'] . ':' .
|
||||
$userdata['lmhash'] . ':' .
|
||||
$userdata['nthash'] . ':' .
|
||||
$userdata['flags'] . ':' .
|
||||
$userdata['lct'] . ':' .
|
||||
$userdata['comment']. "\n";
|
||||
}
|
||||
return $this->_save($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Password
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return string The crypted password.
|
||||
* @param string $pass The plaintext password.
|
||||
* @param string $mode The encryption mode to use (nt|lm).
|
||||
*/
|
||||
function generatePasswd($pass, $mode = 'nt')
|
||||
{
|
||||
$chap = &new Crypt_CHAP_MSv1;
|
||||
$hash = strToLower($mode) == 'nt' ?
|
||||
$chap->ntPasswordHash($pass) :
|
||||
$chap->lmPasswordHash($pass);
|
||||
return strToUpper(bin2hex($hash));
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
* @deprecated
|
||||
*/
|
||||
function generatePassword($pass, $mode = 'nt')
|
||||
{
|
||||
return File_Passwd_Smb::generatePasswd($pass, $mode);
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,652 @@
|
||||
<?php
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* File::Passwd::Unix
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category FileFormats
|
||||
* @package File_Passwd
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @copyright 2003-2005 Michael Wallner
|
||||
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
||||
* @version CVS: $Id$
|
||||
* @link http://pear.php.net/package/File_Passwd
|
||||
*/
|
||||
|
||||
/**
|
||||
* Requires File::Passwd::Common
|
||||
*/
|
||||
require_once 'File/Passwd/Common.php';
|
||||
|
||||
/**
|
||||
* Manipulate standard Unix passwd files.
|
||||
*
|
||||
* <kbd><u>Usage Example:</u></kbd>
|
||||
* <code>
|
||||
* $passwd = &File_Passwd::factory('Unix');
|
||||
* $passwd->setFile('/my/passwd/file');
|
||||
* $passwd->load();
|
||||
* $passwd->addUser('mike', 'secret');
|
||||
* $passwd->save();
|
||||
* </code>
|
||||
*
|
||||
*
|
||||
* <kbd><u>Output of listUser()</u></kbd>
|
||||
* # using the 'name map':
|
||||
* <pre>
|
||||
* array
|
||||
* + user => array
|
||||
* + pass => crypted_passwd or 'x' if shadowed
|
||||
* + uid => user id
|
||||
* + gid => group id
|
||||
* + gecos => comments
|
||||
* + home => home directory
|
||||
* + shell => standard shell
|
||||
* </pre>
|
||||
* # without 'name map':
|
||||
* <pre>
|
||||
* array
|
||||
* + user => array
|
||||
* + 0 => crypted_passwd
|
||||
* + 1 => ...
|
||||
* + 2 => ...
|
||||
* </pre>
|
||||
*
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @package File_Passwd
|
||||
* @version $Revision$
|
||||
* @access public
|
||||
*/
|
||||
class File_Passwd_Unix extends File_Passwd_Common
|
||||
{
|
||||
/**
|
||||
* A 'name map' wich refer to the extra properties
|
||||
*
|
||||
* @var array
|
||||
* @access private
|
||||
*/
|
||||
var $_map = array('uid', 'gid', 'gecos', 'home', 'shell');
|
||||
|
||||
/**
|
||||
* Whether to use the 'name map' or not
|
||||
*
|
||||
* @var boolean
|
||||
* @access private
|
||||
*/
|
||||
var $_usemap = true;
|
||||
|
||||
/**
|
||||
* Whether the passwords of this passwd file are shadowed in another file
|
||||
*
|
||||
* @var boolean
|
||||
* @access private
|
||||
*/
|
||||
var $_shadowed = false;
|
||||
|
||||
/**
|
||||
* Encryption mode, either md5 or des
|
||||
*
|
||||
* @var string
|
||||
* @access private
|
||||
*/
|
||||
var $_mode = 'des';
|
||||
|
||||
/**
|
||||
* Supported encryption modes
|
||||
*
|
||||
* @var array
|
||||
* @access private
|
||||
*/
|
||||
var $_modes = array('md5' => 'md5', 'des' => 'des');
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
* @param string $file path to passwd file
|
||||
*/
|
||||
function File_Passwd_Unix($file = 'passwd')
|
||||
{
|
||||
parent::__construct($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast authentication of a certain user
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o file doesn't exist
|
||||
* o file couldn't be opened in read mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked (only if auth fails)
|
||||
* o file couldn't be closed (only if auth fails)
|
||||
* o invalid encryption mode <var>$mode</var> was provided
|
||||
*
|
||||
* @static call this method statically for a reasonable fast authentication
|
||||
* @access public
|
||||
* @return mixed true if authenticated, false if not or PEAR_Error
|
||||
* @param string $file path to passwd file
|
||||
* @param string $user user to authenticate
|
||||
* @param string $pass plaintext password
|
||||
* @param string $mode encryption mode to use (des or md5)
|
||||
*/
|
||||
function staticAuth($file, $user, $pass, $mode)
|
||||
{
|
||||
$line = File_Passwd_Common::_auth($file, $user);
|
||||
if (!$line) {
|
||||
return $line;
|
||||
}
|
||||
list(,$real)= explode(':', $line);
|
||||
$crypted = File_Passwd_Unix::_genPass($pass, $real, $mode);
|
||||
|
||||
return ($crypted === $real);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply changes an rewrite passwd file
|
||||
*
|
||||
* Returns a PEAR_Error if:
|
||||
* o directory in which the file should reside couldn't be created
|
||||
* o file couldn't be opened in write mode
|
||||
* o file couldn't be locked exclusively
|
||||
* o file couldn't be unlocked
|
||||
* o file couldn't be closed
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function save()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->_users as $user => $array){
|
||||
$pass = array_shift($array);
|
||||
$extra = implode(':', $array);
|
||||
$content .= $user . ':' . $pass;
|
||||
if (!empty($extra)) {
|
||||
$content .= ':' . $extra;
|
||||
}
|
||||
$content .= "\n";
|
||||
}
|
||||
return $this->_save($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the Unix password file
|
||||
*
|
||||
* Returns a PEAR_Error if passwd file has invalid format.
|
||||
*
|
||||
* @throws PEAR_Error
|
||||
* @access public
|
||||
* @return mixed true on success or PEAR_Error
|
||||
*/
|
||||
function parse()
|
||||
{
|
||||
$this->_users = array();
|
||||
foreach ($this->_contents as $line){
|
||||
$parts = explode(':', $line);
|
||||
if (count($parts) < 2) {
|
||||
throw new File_Passwd_Exception(
|
||||
FILE_PASSWD_E_INVALID_FORMAT_STR,
|
||||
FILE_PASSWD_E_INVALID_FORMAT
|
||||
);
|
||||
}
|
||||
$user = array_shift($parts);
|
||||
$pass = array_shift($parts);
|
||||
if ($pass == 'x') {
|
||||
$this->_shadowed = true;
|
||||
}
|
||||
$values = array();
|
||||
if ($this->_usemap) {
|
||||
$values['pass'] = $pass;
|
||||
foreach ($parts as $i => $value){
|
||||
if (isset($this->_map[$i])) {
|
||||
$values[$this->_map[$i]] = $value;
|
||||
} else {
|
||||
$values[$i+1] = $value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$values = array_merge(array($pass), $parts);
|
||||
}
|
||||
$this->_users[$user] = $values;
|
||||
|
||||
}
|
||||
$this->_contents = array();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the encryption mode
|
||||
*
|
||||
* Supported encryption modes are des and md5.
|
||||
*
|
||||
* Returns a File_Passwd_Exception if supplied encryption mode is not supported.
|
||||
*
|
||||
* @see setMode()
|
||||
* @see listModes()
|
||||
*
|
||||
* @throws File_Passwd_Exception
|
||||
* @access public
|
||||
* @return mixed true on succes or File_Passwd_Exception
|
||||
* @param string $mode encryption mode to use; either md5 or des
|
||||
*/
|
||||
function setMode($mode)
|
||||
{
|
||||
$mode = strToLower($mode);
|
||||
if (!isset($this->_modes[$mode])) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
|
||||
FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
);
|
||||
}
|
||||
$this->_mode = $mode;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get supported encryption modes
|
||||
*
|
||||
* <pre>
|
||||
* array
|
||||
* + md5
|
||||
* + des
|
||||
* </pre>
|
||||
*
|
||||
* @see setMode()
|
||||
* @see getMode()
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
function listModes()
|
||||
{
|
||||
return $this->_modes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get actual encryption mode
|
||||
*
|
||||
* @see listModes()
|
||||
* @see setMode()
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function getMode()
|
||||
{
|
||||
return $this->_mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to use the 'name map' of the extra properties or not
|
||||
*
|
||||
* Default Unix passwd files look like:
|
||||
* <pre>
|
||||
* user:password:user_id:group_id:gecos:home_dir:shell
|
||||
* </pre>
|
||||
*
|
||||
* The default 'name map' for properties except user and password looks like:
|
||||
* o uid
|
||||
* o gid
|
||||
* o gecos
|
||||
* o home
|
||||
* o shell
|
||||
*
|
||||
* If you want to change the naming of the standard map use
|
||||
* File_Passwd_Unix::setMap(array()).
|
||||
*
|
||||
* @see setMap()
|
||||
* @see getMap()
|
||||
*
|
||||
* @access public
|
||||
* @return boolean always true if you set a value (true/false) OR
|
||||
* the actual value if called without param
|
||||
*
|
||||
* @param boolean $bool whether to use the 'name map' or not
|
||||
*/
|
||||
function useMap($bool = null)
|
||||
{
|
||||
if (is_null($bool)) {
|
||||
return $this->_usemap;
|
||||
}
|
||||
$this->_usemap = (bool) $bool;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the 'name map' to use with the extra properties of the user
|
||||
*
|
||||
* This map is used for naming the associative array of the extra properties.
|
||||
*
|
||||
* Returns a File_Passwd_Exception if <var>$map</var> was not of type array.
|
||||
*
|
||||
* @see getMap()
|
||||
* @see useMap()
|
||||
*
|
||||
* @throws File_Passwd_Exception
|
||||
* @access public
|
||||
* @return mixed true on success or File_Passwd_Exception
|
||||
*/
|
||||
function setMap($map = array())
|
||||
{
|
||||
if (!is_array($map)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_PARAM_MUST_BE_ARRAY_STR, '$map'),
|
||||
FILE_PASSWD_E_PARAM_MUST_BE_ARRAY
|
||||
);
|
||||
}
|
||||
$this->_map = $map;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the 'name map' which is used for the extra properties of the user
|
||||
*
|
||||
* @see setMap()
|
||||
* @see useMap()
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
function getMap()
|
||||
{
|
||||
return $this->_map;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the passwords of this passwd file are shadowed in another file.
|
||||
*
|
||||
* @access public
|
||||
* @return boolean
|
||||
*/
|
||||
function isShadowed()
|
||||
{
|
||||
return $this->_shadowed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an user
|
||||
*
|
||||
* The username must start with an alphabetical character and must NOT
|
||||
* contain any other characters than alphanumerics, the underline and dash.
|
||||
*
|
||||
* If you use the 'name map' you should also use these naming in
|
||||
* the supplied extra array, because your values would get mixed up
|
||||
* if they are in the wrong order, which is always true if you
|
||||
* DON'T use the 'name map'!
|
||||
*
|
||||
* So be warned and USE the 'name map'!
|
||||
*
|
||||
* If the passwd file is shadowed, the user will be added though, but
|
||||
* with an 'x' as password, and a File_Passwd_Exception will be returned, too.
|
||||
*
|
||||
* Returns a File_Passwd_Exception if:
|
||||
* o user already exists
|
||||
* o user contains illegal characters
|
||||
* o encryption mode is not supported
|
||||
* o passwords are shadowed in another file
|
||||
* o any element of the <var>$extra</var> array contains a colon (':')
|
||||
*
|
||||
* @throws File_Passwd_Exception
|
||||
* @access public
|
||||
* @return mixed true on success or File_Passwd_Exception
|
||||
* @param string $user the name of the user to add
|
||||
* @param string $pass the password of the user to add
|
||||
* @param array $extra extra properties of user to add
|
||||
*/
|
||||
function addUser($user, $pass, $extra = array())
|
||||
{
|
||||
if ($this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_ALREADY
|
||||
);
|
||||
}
|
||||
if (!preg_match($this->_pcre, $user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
if (!is_array($extra)) {
|
||||
setType($extra, 'array');
|
||||
}
|
||||
foreach ($extra as $e){
|
||||
if (strstr($e, ':')) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'Property ', $e),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If passwords of the passwd file are shadowed,
|
||||
* the password of the user will be set to 'x'.
|
||||
*/
|
||||
if ($this->_shadowed) {
|
||||
$pass = 'x';
|
||||
} else {
|
||||
$pass = $this->_genPass($pass);
|
||||
}
|
||||
|
||||
/**
|
||||
* If you don't use the 'name map' the user array will be numeric.
|
||||
*/
|
||||
if (!$this->_usemap) {
|
||||
array_unshift($extra, $pass);
|
||||
$this->_users[$user] = $extra;
|
||||
} else {
|
||||
$map = $this->_map;
|
||||
array_unshift($map, 'pass');
|
||||
$extra['pass'] = $pass;
|
||||
foreach ($map as $key){
|
||||
$this->_users[$user][$key] = @$extra[$key];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Raise a File_Passwd_Exception if passwords are shadowed.
|
||||
*/
|
||||
if ($this->_shadowed) {
|
||||
throw new File_Passwd_Exception(
|
||||
'Password has been set to \'x\' because they are '.
|
||||
'shadowed in another file.', 0
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify properties of a certain user
|
||||
*
|
||||
* # DON'T MODIFY THE PASSWORD WITH THIS METHOD!
|
||||
*
|
||||
* You should use this method only if the 'name map' is used, too.
|
||||
*
|
||||
* Returns a File_Passwd_Exception if:
|
||||
* o user doesn't exist
|
||||
* o any property contains a colon (':')
|
||||
*
|
||||
* @see changePasswd()
|
||||
*
|
||||
* @throws File_Passwd_Exception
|
||||
* @access public
|
||||
* @return mixed true on success or File_Passwd_Exception
|
||||
* @param string $user the user to modify
|
||||
* @param array $properties an associative array of
|
||||
* properties to modify
|
||||
*/
|
||||
function modUser($user, $properties = array())
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
|
||||
if (!is_array($properties)) {
|
||||
setType($properties, 'array');
|
||||
}
|
||||
|
||||
foreach ($properties as $key => $value){
|
||||
if (strstr($value, ':')) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_INVALID_CHARS
|
||||
);
|
||||
}
|
||||
$this->_users[$user][$key] = $value;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the password of a certain user
|
||||
*
|
||||
* Returns a File_Passwd_Exception if:
|
||||
* o user doesn't exists
|
||||
* o passwords are shadowed in another file
|
||||
* o encryption mode is not supported
|
||||
*
|
||||
* @throws File_Passwd_Exception
|
||||
* @access public
|
||||
* @return mixed true on success or File_Passwd_Exception
|
||||
* @param string $user the user whose password should be changed
|
||||
* @param string $pass the new plaintext password
|
||||
*/
|
||||
function changePasswd($user, $pass)
|
||||
{
|
||||
if ($this->_shadowed) {
|
||||
throw new File_Passwd_Exception(
|
||||
'Passwords of this passwd file are shadowed.',
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
|
||||
$pass = $this->_genPass($pass);
|
||||
|
||||
if ($this->_usemap) {
|
||||
$this->_users[$user]['pass'] = $pass;
|
||||
} else {
|
||||
$this->_users[$user][0] = $pass;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the password of a certain user
|
||||
*
|
||||
* Returns a File_Passwd_Exception if:
|
||||
* o user doesn't exist
|
||||
* o encryption mode is not supported
|
||||
*
|
||||
* @throws File_Passwd_Exception
|
||||
* @access public
|
||||
* @return mixed true if passwors equal, false if they don't or File_Passwd_Exception
|
||||
* @param string $user the user whose password should be verified
|
||||
* @param string $pass the password to verify
|
||||
*/
|
||||
function verifyPasswd($user, $pass)
|
||||
{
|
||||
if (!$this->userExists($user)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
||||
FILE_PASSWD_E_EXISTS_NOT
|
||||
);
|
||||
}
|
||||
$real =
|
||||
$this->_usemap ?
|
||||
$this->_users[$user]['pass'] :
|
||||
$this->_users[$user][0]
|
||||
;
|
||||
return ($real === $this->_genPass($pass, $real));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate crypted password from the plaintext password
|
||||
*
|
||||
* Returns a File_Passwd_Exception if actual encryption mode is not supported.
|
||||
*
|
||||
* @throws File_Passwd_Exception
|
||||
* @access private
|
||||
* @return mixed the crypted password or File_Passwd_Exception
|
||||
* @param string $pass the plaintext password
|
||||
* @param string $salt the crypted password from which to gain the salt
|
||||
* @param string $mode the encryption mode to use; don't set, because
|
||||
* it's usually taken from File_Passwd_Unix::_mode
|
||||
*/
|
||||
function _genPass($pass, $salt = null, $mode = null)
|
||||
{
|
||||
static $crypters;
|
||||
if (!isset($crypters)) {
|
||||
$crypters = get_class_methods('File_Passwd');
|
||||
}
|
||||
|
||||
$mode = !isset($mode) ? strToLower($this->_mode) : strToLower($mode);
|
||||
$func = 'crypt_' . $mode;
|
||||
|
||||
if (!in_array($func, $crypters)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
|
||||
FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
);
|
||||
}
|
||||
|
||||
return call_user_func(array('File_Passwd', $func), $pass, $salt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Password
|
||||
*
|
||||
* Returns File_Passwd_Exception FILE_PASSD_E_INVALID_ENC_MODE if the supplied
|
||||
* encryption mode is not supported.
|
||||
*
|
||||
* @see File_Passwd
|
||||
* @static
|
||||
* @access public
|
||||
* @return mixed The crypted password on success or File_Passwd_Exception on failure.
|
||||
* @param string $pass The plaintext password.
|
||||
* @param string $mode The encryption mode to use.
|
||||
* @param string $salt The salt to use.
|
||||
*/
|
||||
function generatePasswd($pass, $mode = FILE_PASSWD_MD5, $salt = null)
|
||||
{
|
||||
if (!isset($mode)) {
|
||||
throw new File_Passwd_Exception(
|
||||
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, '<NULL>'),
|
||||
FILE_PASSWD_E_INVALID_ENC_MODE
|
||||
);
|
||||
}
|
||||
return File_Passwd_Unix::_genPass($pass, $salt, $mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
* @deprecated
|
||||
*/
|
||||
function generatePassword($pass, $mode = FILE_PASSWD_MD5, $salt = null)
|
||||
{
|
||||
return File_Passwd_Unix::generatePasswd($pass, $mode, $salt);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user