C:\xampp\php\pear\File\Passwd\Smb.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
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);
    }
}
?>
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586