Initial Commit

This commit is contained in:
Riley Schneider
2025-12-03 16:38:10 +01:00
parent c5e26bf594
commit b732d8d4b5
17680 changed files with 5977495 additions and 2 deletions

View File

@@ -0,0 +1,396 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd::Authbasic
*
* 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: Authbasic.php,v 1.17 2005/03/30 18:33:33 mike Exp $
* @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: 1.17 $
* @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
*
* @access public
* @param string $file path to AuthUserFile
*/
function File_Passwd_Authbasic($file = '.htpasswd')
{
File_Passwd_Authbasic::__construct($file);
}
/**
* 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
*
* 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 $mode des, sha or md5
*/
function staticAuth($file, $user, $pass, $mode)
{
$line = File_Passwd_Common::_auth($file, $user);
if (!$line || PEAR::isError($line)) {
return $line;
}
list(,$real) = explode(':', $line);
$crypted = File_Passwd_Authbasic::_genPass($pass, $real, $mode);
if (PEAR::isError($crypted)) {
return $crypted;
}
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)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_ALREADY
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
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])) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $this->_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);
}
return PEAR::raiseError(
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) {
return PEAR::raiseError(
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'))) {
return PEAR::raiseError(
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);
}
}
?>

View File

@@ -0,0 +1,362 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd::Authdigest
*
* 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: Authdigest.php,v 1.13 2005/09/27 06:26:08 mike Exp $
* @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: 1.13 $
* @access public
*/
class File_Passwd_Authdigest extends File_Passwd_Common
{
/**
* Path to AuthDigestFile
*
* @var string
* @access private
*/
var $_file = '.htdigest';
/**
* Constructor
*
* @access public
* @param string $file path to AuthDigestFile
*/
function File_Passwd_Authdigest($file = '.htdigest')
{
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
*
* @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 || PEAR::isError($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)) {
return PEAR::raiseError(
"User '$user' already exists in realm '$realm'.", 0
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
FILE_PASSWD_E_INVALID_CHARS
);
}
if (!preg_match($this->_pcre, $realm)) {
return PEAR::raiseError(
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)
{
if (PEAR::isError($error = $this->delUserInRealm($user, $realm))) {
return $error;
} else {
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)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
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) {
return PEAR::raiseError(
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);
}
}
?>

View File

@@ -0,0 +1,382 @@
<?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: Common.php,v 1.18 2005/03/30 18:33:33 mike Exp $
* @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: 1.18 $
* @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()
{
return PEAR::raiseError(
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()
{
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_DIR_NOT_CREATED_STR, $dir),
FILE_PASSWD_E_DIR_NOT_CREATED
);
}
if (!is_resource($fh = @fopen($file, $mode))) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_FILE_NOT_OPENED_STR, $file),
FILE_PASSWD_E_FILE_NOT_OPENED
);
}
if (!@flock($fh, $lock)) {
fclose($fh);
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
FILE_PASSWD_E_FILE_NOT_UNLOCKED_STR,
FILE_PASSWD_E_FILE_NOT_UNLOCKED
);
}
if (!@fclose($file_handle)) {
return PEAR::raiseError(
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');
if (PEAR::isError($fh)) {
return $fh;
}
$this->_contents = array();
while ($line = fgets($fh)) {
if (!preg_match('/^\s*#/', $line) && $line = trim($line)) {
$this->_contents[] = $line;
}
}
$e = $this->_close($fh);
if (PEAR::isError($e)) {
return $e;
}
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');
if (PEAR::isError($fh)) {
return $fh;
}
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)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError("File '$file' couldn't be found.", 0);
}
$fh = &File_Passwd_Common::_open('r', $file);
if (PEAR::isError($fh)) {
return $fh;
}
$cmp = $id . $sep;
$len = strlen($cmp);
while ($line = fgets($fh)) {
if (!strncmp($line, $cmp, $len)) {
File_Passwd_Common::_close($fh);
return trim($line);
}
}
$e = File_Passwd_Common::_close($fh);
if (PEAR::isError($e)) {
return $e;
}
return false;
}
}
?>

View File

@@ -0,0 +1,593 @@
<?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: Custom.php,v 1.10 2005/03/30 18:33:33 mike Exp $
* @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: 1.10 $
* @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])) {
return PEAR::raiseError('Insufficient options.', 0);
}
$line = File_Passwd_Common::_auth($file, $user, $opts[1]);
if (!$line || PEAR::isError($line)) {
return $line;
}
list(,$real)= explode($opts[1], $line);
$crypted = File_Passwd_Custom::_genPass($pass, $real, $opts[0]);
if (PEAR::isError($crypted)) {
return $crypted;
}
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);
}
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
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) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_ALREADY
);
}
if (!preg_match($this->_pcre, $user) || strstr($user, $this->_delim)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'Property ', $e),
FILE_PASSWD_E_INVALID_CHARS
);
}
}
$pass = $this->_genPass($pass);
if (PEAR::isError($pass)) {
return $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)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
$pass = $this->_genPass($pass);
if (PEAR::isError($pass)) {
return $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)) {
return PEAR::raiseError(
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);
}
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $func),
FILE_PASSWD_E_INVALID_ENC_MODE
);
}
$return = @call_user_func($func, $pass, $salt);
if (is_null($return) || $return === false) {
$return = @call_user_func($func, $pass);
}
return $return;
}
}
?>

View File

@@ -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: Cvs.php,v 1.14 2005/03/30 18:33:33 mike Exp $
* @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: 1.14 $
* @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 || PEAR::isError($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) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_ALREADY
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
if (!preg_match($this->_pcre, $system)) {
return PEAR::raiseError(
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);
}
}
?>

View File

@@ -0,0 +1,426 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd::Smb
*
* 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>
* @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: Smb.php,v 1.18 2005/05/06 10:22:48 mike Exp $
* @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: 1.18 $
* @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
*
* @access public
* @param string $file SMB passwd file
*/
function File_Passwd_Smb($file = 'smbpasswd')
{
File_Passwd_Smb::__construct($file);
}
/**
* 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 || PEAR::isError($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:
return PEAR::raiseError(
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) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_ALREADY
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
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])) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
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)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
if (strstr($this->_users[$user]['flags'], 'D')) {
return PEAR::raiseError("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);
}
}
?>

View File

@@ -0,0 +1,660 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd::Unix
*
* 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: Unix.php,v 1.17 2005/03/30 18:33:33 mike Exp $
* @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: 1.17 $
* @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 || PEAR::isError($line)) {
return $line;
}
list(,$real)= explode(':', $line);
$crypted = File_Passwd_Unix::_genPass($pass, $real, $mode);
if (PEAR::isError($crypted)) {
return $crypted;
}
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) {
return PEAR::raiseError(
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 PEAR_Error if supplied encryption mode is not supported.
*
* @see setMode()
* @see listModes()
*
* @throws PEAR_Error
* @access public
* @return mixed true on succes or PEAR_Error
* @param string $mode encryption mode to use; either md5 or des
*/
function setMode($mode)
{
$mode = strToLower($mode);
if (!isset($this->_modes[$mode])) {
return PEAR::raiseError(
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 PEAR_Error if <var>$map</var> was not of type array.
*
* @see getMap()
* @see useMap()
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function setMap($map = array())
{
if (!is_array($map)) {
return PEAR::raiseError(
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 PEAR_Error will be returned, too.
*
* Returns a PEAR_Error 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 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 to add
* @param array $extra extra properties of user to add
*/
function addUser($user, $pass, $extra = array())
{
if ($this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_ALREADY
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
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, ':')) {
return PEAR::raiseError(
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 (PEAR::isError($pass)) {
return $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 PEAR_Error if passwords are shadowed.
*/
if ($this->_shadowed) {
return PEAR::raiseError(
'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 PEAR_Error if:
* o user doesn't exist
* o any property contains a colon (':')
*
* @see changePasswd()
*
* @throws PEAR_Error
* @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)) {
return PEAR::raiseError(
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, ':')) {
return PEAR::raiseError(
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 passwords are shadowed in another file
* o encryption mode is not supported
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or 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->_shadowed) {
return PEAR::raiseError(
'Passwords of this passwd file are shadowed.',
0
);
}
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
$pass = $this->_genPass($pass);
if (PEAR::isError($pass)) {
return $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
* @access public
* @return mixed true if passwors equal, false if they don't or PEAR_Error
* @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)) {
return PEAR::raiseError(
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
* @access private
* @return mixed the crypted password or PEAR_Error
* @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)) {
return PEAR::raiseError(
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 PEAR_Error 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 PEAR_Error 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)) {
return PEAR::raiseError(
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);
}
}
?>