2 /* vim: set ts=4 sw=4: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.0 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Author: Rasmus Lerdorf <rasmus@php.net> |
17 // +----------------------------------------------------------------------+
19 // $Id: File_Passwd.php,v 1.9 2004-06-03 18:06:29 rurban Exp $
21 // Manipulate standard UNIX passwd,.htpasswd and CVS pserver passwd files
23 require_once 'PEAR.php' ;
26 * Class to manage passwd-style files
28 * @author Rasmus Lerdorf <rasmus@php.net>
45 * hash list of csv-users
51 * filehandle for lockfile
63 * name of the lockfile
66 var $lockfile = './passwd.lock';
70 * Requires the name of the passwd file. This functions opens the file and read it.
71 * Changes to this file will written first in the lock file, so it is still possible
72 * to access the passwd file by another programs. The lock parameter controls the locking
73 * oft the lockfile, not of the passwd file! ( Swapping $lock and $lockfile would
74 * breaks bc to v1.3 and smaller).
75 * Don't forget to call close() to save changes!
77 * @param $file name of the passwd file
78 * @param $lock if 'true' $lockfile will be locked
79 * @param $lockfile name of the temp file, where changes are saved
85 function File_Passwd($file, $lock = 0, $lockfile = "") {
86 $this->filename = $file;
87 if( !empty( $lockfile) ) {
88 $this->lockfile = $lockfile;
92 //check if already locked, on some error or race condition or other user.
93 //FIXME: implement timeout as with dba
94 if (!empty($this->lockfile) and file_exists($this->lockfile)) {
95 if (isset($GLOBALS['HTTP_GET_VARS']['force_unlock'])) {
96 $this->fplock = fopen($this->lockfile, 'w');
97 flock($this->fplock, LOCK_UN);
98 fclose($this->fplock);
100 trigger_error('File_Passwd lock conflict: Try &force_unlock=1',E_USER_NOTICE);
103 $this->fplock = fopen($this->lockfile, 'w');
104 flock($this->fplock, LOCK_EX);
105 $this->locked = true;
108 $fp = fopen($file,'r') ;
110 return new PEAR_Error( "Couldn't open '$file'!", 1, PEAR_ERROR_RETURN) ;
113 $line = fgets($fp, 128);
114 $array = explode(':', $line);
115 if (count($array) and strlen(trim($array[0]))) {
116 $user = trim($array[0]);
117 if (in_array(substr($user,0,1),array('#',';'))) continue;
118 if (empty($array[1])) $array[1]='';
119 $this->users[$user] = trim($array[1]);
120 if (count($array) >= 3)
121 $this->cvs[$user] = trim($array[2]);
130 * @param $user new user id
131 * @param $pass password for new user
132 * @param $cvs cvs user id (needed for pserver passwd files)
134 * @return mixed returns PEAR_Error, if the user already exists
137 function addUser($user, $pass, $cvsuser = "") {
138 if(!isset($this->users[$user]) && $this->locked) {
139 $this->users[$user] = crypt($pass);
140 $this->cvs[$user] = $cvsuser;
143 return new PEAR_Error( "Couldn't add user '$user', because the user already exists!", 2, PEAR_ERROR_RETURN);
145 } // end func addUser()
150 * @param $user user id
151 * @param $pass new password for user
152 * @param $cvs cvs user id (needed for pserver passwd files)
154 * @return mixed returns PEAR_Error, if the user doesn't exists
158 function modUser($user, $pass, $cvsuser="") {
159 if(isset($this->users[$user]) && $this->locked) {
160 $this->users[$user] = crypt($pass);
161 $this->cvs[$user] = $cvsuser;
164 return new PEAR_Error( "Couldn't modify user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
166 } // end func modUser()
171 * @param $user user id
173 * @return mixed returns PEAR_Error, if the user doesn't exists
177 function delUser($user) {
178 if (isset($this->users[$user]) && $this->locked) {
179 unset($this->users[$user]);
180 unset($this->cvs[$user]);
182 return new PEAR_Error( "Couldn't delete user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
184 } // end func delUser()
187 * Verifies a user's password
189 * @param $user user id
190 * @param $pass password for user
192 * @return boolean true if password is ok
195 function verifyPassword($user, $pass) {
196 //if ($this->users[$user] == crypt($pass, substr($this->users[$user], 0, 2)))
198 if (isset($this->users[$user])) {
199 $stored_password = $this->users[$user];
200 if (function_exists('crypt')) {
201 if (crypt($pass, $stored_password) == $stored_password)
202 return true; // matches encrypted password
206 if ($pass == $stored_password)
207 return true; // matches plaintext password
209 trigger_error(_("The crypt function is not available in this version of PHP."),
219 * Return all users from passwd file
224 function listUsers() {
226 } // end func listUsers()
229 * Writes changes to passwd file and unlocks it
235 foreach($this->users as $user => $pass) {
236 if (isset($this->cvs[$user])) {
237 fputs($this->fplock, "$user:$pass:" . $this->cvs[$user] . "\n");
239 fputs($this->fplock, "$user:$pass\n");
242 @unlink($this->filename.'.bak');
244 // windows doesn't allow renaming of open files
245 flock($this->fplock, LOCK_UN);
246 $this->locked = false;
247 fclose($this->fplock);
248 rename($this->filename,$this->filename.'.bak');
249 rename($this->lockfile, $this->filename);
251 rename($this->filename,$this->filename.'.bak');
252 rename($this->lockfile, $this->filename);
253 flock($this->fplock, LOCK_UN);
254 $this->locked = false;
255 fclose($this->fplock);
257 if (file_exists($this->filename))
258 @unlink($this->filename.'.bak');
260 rename($this->filename.'.bak',$this->filename);
263 } // end func close()
270 // c-hanging-comment-ender-p: nil
271 // indent-tabs-mode: nil