File: File_Passwd.php

package info (click to toggle)
phpwiki 1.3.12p3-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,956 kB
  • ctags: 21,608
  • sloc: php: 82,335; xml: 3,840; sh: 1,522; sql: 1,198; perl: 625; makefile: 562; awk: 28
file content (273 lines) | stat: -rw-r--r-- 8,875 bytes parent folder | download | duplicates (4)
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
<?php
/* vim: set ts=4 sw=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Author: Rasmus Lerdorf <rasmus@php.net>                              |
// +----------------------------------------------------------------------+
//
// $Id: File_Passwd.php,v 1.9 2004/06/03 18:06:29 rurban Exp $
//
// Manipulate standard UNIX passwd,.htpasswd and CVS pserver passwd files

require_once 'PEAR.php' ;

/**
* Class to manage passwd-style files
*
* @author Rasmus Lerdorf <rasmus@php.net>
*/
class File_Passwd {

    /**
    * Passwd file
    * @var string
    */
    var $filename ;

    /**
    * Hash list of users
    * @var array
    */
    var $users ;
    
    /**
    * hash list of csv-users
    * @var array
    */
    var $cvs ;
    
    /**
    * filehandle for lockfile
    * @var int
    */
    var $fplock ;
    
    /**
    * locking state
    * @var boolean
    */
    var $locked ;
    
    /**
    * name of the lockfile
    * @var string    
    */ 
    var $lockfile = './passwd.lock';

    /**
    * Constructor
    * Requires the name of the passwd file. This functions opens the file and read it.
    * Changes to this file will written first in the lock file, so it is still possible
    * to access the passwd file by another programs. The lock parameter controls the locking
    * oft the lockfile, not of the passwd file! ( Swapping $lock and $lockfile would
    * breaks bc to v1.3 and smaller).
    * Don't forget to call close() to save changes!
    * 
    * @param $file		name of the passwd file
    * @param $lock		if 'true' $lockfile will be locked
    * @param $lockfile	name of the temp file, where changes are saved
    *
    * @access public
    * @see close() 
    */

    function File_Passwd($file, $lock = 0, $lockfile = "") {
        $this->filename = $file;
        if( !empty( $lockfile) ) {
            $this->lockfile = $lockfile;
        }

        if ($lock) {
            //check if already locked, on some error or race condition or other user.
            //FIXME: implement timeout as with dba
            if (!empty($this->lockfile) and file_exists($this->lockfile)) {
            	if (isset($GLOBALS['HTTP_GET_VARS']['force_unlock'])) {
            	    $this->fplock = fopen($this->lockfile, 'w');
            	    flock($this->fplock, LOCK_UN);
            	    fclose($this->fplock);
            	} else {
                    trigger_error('File_Passwd lock conflict: Try &force_unlock=1',E_USER_NOTICE);
            	}
            }
            $this->fplock = fopen($this->lockfile, 'w');
            flock($this->fplock, LOCK_EX);
            $this->locked = true;
        }
    
        $fp = fopen($file,'r') ;
        if( !$fp ) {
            return new PEAR_Error( "Couldn't open '$file'!", 1, PEAR_ERROR_RETURN) ;
        }
        while(!feof($fp)) {
            $line = fgets($fp, 128);
            $array = explode(':', $line);
            if (count($array) and strlen(trim($array[0]))) {
                $user = trim($array[0]);
                if (in_array(substr($user,0,1),array('#',';'))) continue;
                if (empty($array[1])) $array[1]='';
                $this->users[$user] = trim($array[1]);
                if (count($array) >= 3)
                    $this->cvs[$user] = trim($array[2]);	
            }
        }
        fclose($fp);
    }

    /**
    * Adds a user
    *
    * @param $user new user id
    * @param $pass password for new user
    * @param $cvs  cvs user id (needed for pserver passwd files)
    *
    * @return mixed returns PEAR_Error, if the user already exists
    * @access public
    */
    function addUser($user, $pass, $cvsuser = "") {
        if(!isset($this->users[$user]) && $this->locked) {
            $this->users[$user] = crypt($pass);
            $this->cvs[$user] = $cvsuser;
            return true;
        } else {
            return new PEAR_Error( "Couldn't add user '$user', because the user already exists!", 2, PEAR_ERROR_RETURN);
        }
    } // end func addUser()

    /**
    * Modifies a user
    *
    * @param $user user id
    * @param $pass new password for user
    * @param $cvs  cvs user id (needed for pserver passwd files)
    *
    * @return mixed returns PEAR_Error, if the user doesn't exists
    * @access public
    */

    function modUser($user, $pass, $cvsuser="") {
        if(isset($this->users[$user]) && $this->locked) {
            $this->users[$user] = crypt($pass);
            $this->cvs[$user] = $cvsuser;
            return true;
        } else {
            return new PEAR_Error( "Couldn't modify user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
        }
    } // end func modUser()

    /**
    * Deletes a user
    *
    * @param $user user id
    *
    * @return mixed returns PEAR_Error, if the user doesn't exists
    * @access public	
    */
    
    function delUser($user) {
        if (isset($this->users[$user]) && $this->locked) {
            unset($this->users[$user]);
            unset($this->cvs[$user]);
        } else {
            return new PEAR_Error( "Couldn't delete user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ; 
        }
    } // end func delUser()

    /**
    * Verifies a user's password
    *
    * @param $user user id
    * @param $pass password for user
    *
    * @return boolean true if password is ok
    * @access public		
    */
    function verifyPassword($user, $pass) {
        //if ($this->users[$user] == crypt($pass, substr($this->users[$user], 0, 2)))
        //  return true;
        if (isset($this->users[$user])) {
            $stored_password = $this->users[$user];
            if (function_exists('crypt')) {
                if (crypt($pass, $stored_password) == $stored_password)
                    return true; // matches encrypted password
                else
                    return false;
            } else {
                if ($pass == $stored_password)
                    return true; // matches plaintext password
                else {
                    trigger_error(_("The crypt function is not available in this version of PHP."),
                                  E_USER_WARNING);
                    return false;
                }
            }
        }
        return false;
    }

    /**
    * Return all users from passwd file
    *
    * @access public
    * @return array
    */
    function listUsers() {
        return $this->users;
    } // end func listUsers()

    /**
    * Writes changes to passwd file and unlocks it
    *
    * @access public
    */
    function close() {
        if ($this->locked) {
            foreach($this->users as $user => $pass) {
                if (isset($this->cvs[$user])) {
                    fputs($this->fplock, "$user:$pass:" . $this->cvs[$user] . "\n");
                } else {
                    fputs($this->fplock, "$user:$pass\n");
                }
            }
            @unlink($this->filename.'.bak');
            if (isWindows()) {
              // windows doesn't allow renaming of open files
              flock($this->fplock, LOCK_UN);
              $this->locked = false;
              fclose($this->fplock);
              rename($this->filename,$this->filename.'.bak');
              rename($this->lockfile, $this->filename);
            } else {
              rename($this->filename,$this->filename.'.bak');
              rename($this->lockfile, $this->filename);
              flock($this->fplock, LOCK_UN);
              $this->locked = false;
              fclose($this->fplock);
            }
            if (file_exists($this->filename))
                @unlink($this->filename.'.bak');
            else {
                rename($this->filename.'.bak',$this->filename);
            }
        }
    } // end func close()
}

// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>