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.6 2004-04-26 20:44:36 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 $this->fplock = fopen($this->lockfile, 'w');
93 flock($this->fplock, LOCK_EX);
97 $fp = fopen($file,'r') ;
99 return new PEAR_Error( "Couldn't open '$file'!", 1, PEAR_ERROR_RETURN) ;
102 $line = fgets($fp, 128);
103 $array = explode(':', $line);
104 if (count($array) and strlen(trim($array[0]))) {
105 $user = trim($array[0]);
106 if (in_array(substr($user,0,1),array('#',';'))) continue;
107 if (empty($array[1])) $array[1]='';
108 $this->users[$user] = trim($array[1]);
109 if (count($array) >= 3)
110 $this->cvs[$user] = trim($array[2]);
114 } // end func File_Passwd()
119 * @param $user new user id
120 * @param $pass password for new user
121 * @param $cvs cvs user id (needed for pserver passwd files)
123 * @return mixed returns PEAR_Error, if the user already exists
126 function addUser($user, $pass, $cvsuser = "") {
127 if(!isset($this->users[$user]) && $this->locked) {
128 $this->users[$user] = crypt($pass);
129 $this->cvs[$user] = $cvsuser;
132 return new PEAR_Error( "Couldn't add user '$user', because the user already exists!", 2, PEAR_ERROR_RETURN);
134 } // end func addUser()
139 * @param $user user id
140 * @param $pass new password for user
141 * @param $cvs cvs user id (needed for pserver passwd files)
143 * @return mixed returns PEAR_Error, if the user doesn't exists
147 function modUser($user, $pass, $cvsuser="") {
148 if(isset($this->users[$user]) && $this->locked) {
149 $this->users[$user] = crypt($pass);
150 $this->cvs[$user] = $cvsuser;
153 return new PEAR_Error( "Couldn't modify user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
155 } // end func modUser()
160 * @param $user user id
162 * @return mixed returns PEAR_Error, if the user doesn't exists
166 function delUser($user) {
167 if (isset($this->users[$user]) && $this->locked) {
168 unset($this->users[$user]);
169 unset($this->cvs[$user]);
171 return new PEAR_Error( "Couldn't delete user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
173 } // end func delUser()
176 * Verifies a user's password
178 * @param $user user id
179 * @param $pass password for user
181 * @return boolean true if password is ok
184 function verifyPassword($user, $pass) {
185 //if ($this->users[$user] == crypt($pass, substr($this->users[$user], 0, 2)))
187 if (isset($this->users[$user])) {
188 $stored_password = $this->users[$user];
189 if (function_exists('crypt')) {
190 if (crypt($pass, $stored_password) == $stored_password)
191 return true; // matches encrypted password
195 if ($pass == $stored_password)
196 return true; // matches plaintext password
198 trigger_error(_("The crypt function is not available in this version of PHP."),
208 * Return all users from passwd file
213 function listUsers() {
215 } // end func listUsers()
218 * Writes changes to passwd file and unlocks it
224 foreach($this->users as $user => $pass) {
225 if (isset($this->cvs[$user])) {
226 fputs($this->fplock, "$user:$pass:" . $this->cvs[$user] . "\n");
228 fputs($this->fplock, "$user:$pass\n");
231 @unlink($this->filename.'.bak');
233 // windows doesn't allow renaming of open files
234 flock($this->fplock, LOCK_UN);
235 $this->locked = false;
236 fclose($this->fplock);
237 rename($this->filename,$this->filename.'.bak');
238 rename($this->lockfile, $this->filename);
240 rename($this->filename,$this->filename.'.bak');
241 rename($this->lockfile, $this->filename);
242 flock($this->fplock, LOCK_UN);
243 $this->locked = false;
244 fclose($this->fplock);
246 if (file_exists($this->filename))
247 @unlink($this->filename.'.bak');
249 rename($this->filename.'.bak',$this->filename);
252 } // end func close()
259 // c-hanging-comment-ender-p: nil
260 // indent-tabs-mode: nil