Source for file SMBPasswd.php
Documentation is available at SMBPasswd.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
Copyright (c) 2003, Michael Bretterklieber <michael@bretterklieber.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This code cannot simply be copied and put under the GNU Public License or
any other GPL-like (LGPL, GPL2) License.
$Id: SMBPasswd.php,v 1.4 2005/05/08 09:10:32 mbretter Exp $
require_once 'Crypt/CHAP.php';
* Class to manage SAMBA smbpasswd-style files
* Example 1 (modifying existing file):
* $f = new File_SMBPasswd('./smbpasswd');
* $f->addAccount('sepp3', 12, 'MyPw');
* $f->modAccount('sepp', '', 'MyPw');
* $f->delAccount('karli');
* Example 2 (creating a new file):
* $f = new File_SMBPasswd('./smbpasswdnew');
* $f->addAccount('sepp1', 12, 'MyPw');
* $f->addAccount('sepp3', 1000, 'MyPw');
* Example 3 (authentication):
* $f = new File_SMBPasswd('./smbpasswdnew');
* if ($f->verifyAccount('sepp', 'MyPw')) {
* echo "Account invalid or disabled";
* @author Michael Bretterklieber <mbretter@jawa.at>
* @package File_SMBPasswd
* Multidimensional array of accounts
* Path to the smbpasswd file
* Class who generates the NT-Hash and LAN-Manager-Hash
* Filehandle, of locked File
* @return object File_SMBPasswd
* Load the given smbpasswd file
* @return mixed true on success, PEAR_Error on failure
return $this->raiseError ('Could not open ' . $this->getFile() .
$line = fgets($fd, 4096 );
@list ($user, $userid, $lmhash, $nthash, $flags, $lct, $comment) = explode(':', $line);
'userid' => trim($userid),
'lmhash' => trim($lmhash),
'nthash' => trim($nthash),
'comment' => trim($comment)
* Get the value of file property
* Get the value of accounts property
* Adds an account, with pre-encrypted passwords
* @param $user new username
* @param $userid new userid
* @param $lmhash LAN-Manager-Hash
* @param $comment Comment
* @param $flags Account-flags (see man 5 smbpasswd)
* @return mixed returns PEAR_Error, if the user already exists
function addAccountEncrypted($user, $userid, $lmhash = '', $nthash = '', $comment = '', $flags = '[U ]')
if (empty ($lmhash)) $lmhash = str_repeat('X', 32 );
if (empty ($nthash)) $nthash = str_repeat('X', 32 );
'userid' => trim($userid),
'lmhash' => trim($lmhash),
'nthash' => trim($nthash),
'comment' => trim($comment)
return $this->raiseError ( " Couldn't add user '$user', because the user already exists!" );
* @param $user new username
* @param $userid new userid
* @param $pass Plaintext password
* @param $comment Comment
* @param $flags Account-flags (see man 5 smbpasswd)
* @return mixed returns PEAR_Error, if the user already exists
function addAccount($user, $userid, $pass, $comment = '', $flags = '[U ]')
* @param $user new username
* @param $userid new userid
* @param $pass Plaintext password
* @param $comment Comment
* @return mixed returns PEAR_Error, if the user already exists
function addUser($user, $userid, $pass, $comment = '')
return $this->addAccount($user, $userid, $pass, $comment, '[U ]');
* @param $machine new username
* @param $userid new userid
* @param $comment Comment
* @return mixed returns PEAR_Error, if the user already exists
function addMachine($machine, $userid, $comment = '')
return $this->addAccount($machine . '$', $userid, $machine, $comment, '[W ]');
* Modifies an account with the pre-encrypted Hashes
* @param $user new username
* @param $userid new userid
* @param $lmhash LAN-Manager-Hash
* @param $comment Comment
* @param $flags Account-flags (see man 5 smbpasswd)
* @return mixed returns PEAR_Error, if the user doesen't exists
function modAccountEncrypted($user, $userid = '', $lmhash = '', $nthash = '', $comment = '', $flags = '')
if ($userid === '') $userid = $account['userid'];
if (empty ($lmhash)) $lmhash = $account['lmhash'];
if (empty ($nthash)) $nthash = $account['nthash'];
if (empty ($flags)) $flags = $account['flags'];
if (empty ($comment)) $comment = $account['comment'];
'userid' => trim($userid),
'lmhash' => trim($lmhash),
'nthash' => trim($nthash),
'comment' => trim($comment)
return $this->raiseError ( " Couldn't modify user '$user', because the user doesn't exists!" ) ;
* Modifies an account with given plaintext password
* @param $user new username
* @param $userid new userid
* @param $pass Plaintext password
* @param $comment Comment
* @param $flags Account-flags (see man 5 smbpasswd)
* @return mixed returns PEAR_Error, if the user doesen't exists
function modAccount($user, $userid = '', $pass = '', $comment = '', $flags = '')
* This is an alias for modAccount
* @see File_SMBPasswd::modAccount
function modUser($user, $userid = '', $pass = '', $comment = '', $flags = '')
return $this->modAccount($user, $userid, $pass, $comment, $flags);
* @return mixed returns PEAR_Error, if the user doesn't exists
return $this->raiseError ( " Couldn't delete account '$name', because the account doesn't exists!" ) ;
* This is an alias for delAccount
* @see File_SMBPasswd::delAccount
* Verifies a user's password
* Prefer NT-Hash instead of weak LAN-Manager-Hash
* @param $nthash NT-Hash in hex
* @param $lmhash LAN-Manager-Hash in hex
* @return boolean true if password is ok
// checking wether account is disabled
* Verifies an account with the given plaintext password
* @param $pass The plaintext password
* @return boolean true if password is ok
* @return mixed PEAR_Error, true on succes
return $this->raiseError ('Could not open ' . $this->getFile() .
return $this->raiseError ('Could not open lock file ' . $this->getFile());
* @return mixed PEAR_Error, true on succes
return $this->raiseError ('Could not open unlock file ' . $this->getFile());
* Writes changes to smbpasswd file and locks, unlocks and closes it
* @return mixed returns PEAR_Error, if the file is not writeable
function save($file = '')
foreach ($this->accounts as $user => $userdata) {
* Print all accounts from smbpasswd file
foreach ($this->accounts as $user => $userdata) {
printf("%s:%s:%s:%s:%s:%s:%s\n",
Documentation generated on Mon, 11 Mar 2019 15:28:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|