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.2 2004-01-25 04:04:34 rurban Exp $
21 // Manipulate standard UNIX passwd,.htpasswd and CVS pserver passwd files
23 require_once 'lib/pear/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 list($user, $pass, $cvsuser) = explode(':', $line);
105 $this->users[$user] = trim($pass);
106 $this->cvs[$user] = trim($cvsuser);
110 } // end func File_Passwd()
115 * @param $user new user id
116 * @param $pass password for new user
117 * @param $cvs cvs user id (needed for pserver passwd files)
119 * @return mixed returns PEAR_Error, if the user already exists
122 function addUser($user, $pass, $cvsuser = "") {
123 if(!isset($this->users[$user]) && $this->locked) {
124 $this->users[$user] = crypt($pass);
125 $this->cvs[$user] = $cvsuser;
128 return new PEAR_Error( "Couldn't add user '$user', because the user already exists!", 2, PEAR_ERROR_RETURN);
130 } // end func addUser()
135 * @param $user user id
136 * @param $pass new password for user
137 * @param $cvs cvs user id (needed for pserver passwd files)
139 * @return mixed returns PEAR_Error, if the user doesn't exists
143 function modUser($user, $pass, $cvsuser="") {
144 if(isset($this->users[$user]) && $this->locked) {
145 $this->users[$user] = crypt($pass);
146 $this->cvs[$user] = $cvsuser;
149 return new PEAR_Error( "Couldn't modify user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
151 } // end func modUser()
156 * @param $user user id
158 * @return mixed returns PEAR_Error, if the user doesn't exists
162 function delUser($user) {
163 if(isset($this->users[$user]) && $this->locked) {
164 unset($this->users[$user]);
165 unset($this->cvs[$user]);
167 return new PEAR_Error( "Couldn't delete user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
169 } // end func delUser()
172 * Verifies a user's password
174 * @param $user user id
175 * @param $pass password for user
177 * @return boolean true if password is ok
180 function verifyPassword($user, $pass) {
181 if(isset($this->users[$user])) {
182 if($this->users[$user] == crypt($pass, substr($this->users[$user], 0, 2))) return true;
185 } // end func verifyPassword()
188 * Return all users from passwd file
193 function listUsers() {
195 } // end func listUsers()
198 * Writes changes to passwd file and unlocks it
204 foreach($this->users as $user => $pass) {
205 if($this->cvs[$user]) {
206 fputs($this->fplock, "$user:$pass:" . $this->cvs[$user] . "\n");
208 fputs($this->fplock, "$user:$pass\n");
211 rename($this->lockfile, $this->filename);
212 flock($this->fplock, LOCK_UN);
213 $this->locked = false;
214 fclose($this->fplock);
216 } // end func close()